Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6#include <stdio.h>
7#include <stdbool.h>
8#include <string.h>
9#include <stdlib.h>
10#include <unistd.h>
11#include <subcmd/exec-cmd.h>
12#include <subcmd/pager.h>
13#include <linux/kernel.h>
14
15#include <objtool/builtin.h>
16#include <objtool/objtool.h>
17#include <objtool/warn.h>
18
19bool help;
20
21const char *objname;
22static struct objtool_file file;
23
24static bool objtool_create_backup(const char *_objname)
25{
26 int len = strlen(_objname);
27 char *buf, *base, *name = malloc(len+6);
28 int s, d, l, t;
29
30 if (!name) {
31 perror("failed backup name malloc");
32 return false;
33 }
34
35 strcpy(name, _objname);
36 strcpy(name + len, ".orig");
37
38 d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
39 if (d < 0) {
40 perror("failed to create backup file");
41 return false;
42 }
43
44 s = open(_objname, O_RDONLY);
45 if (s < 0) {
46 perror("failed to open orig file");
47 return false;
48 }
49
50 buf = malloc(4096);
51 if (!buf) {
52 perror("failed backup data malloc");
53 return false;
54 }
55
56 while ((l = read(s, buf, 4096)) > 0) {
57 base = buf;
58 do {
59 t = write(d, base, l);
60 if (t < 0) {
61 perror("failed backup write");
62 return false;
63 }
64 base += t;
65 l -= t;
66 } while (l);
67 }
68
69 if (l < 0) {
70 perror("failed backup read");
71 return false;
72 }
73
74 free(name);
75 free(buf);
76 close(d);
77 close(s);
78
79 return true;
80}
81
82struct objtool_file *objtool_open_read(const char *_objname)
83{
84 if (objname) {
85 if (strcmp(objname, _objname)) {
86 WARN("won't handle more than one file at a time");
87 return NULL;
88 }
89 return &file;
90 }
91 objname = _objname;
92
93 file.elf = elf_open_read(objname, O_RDWR);
94 if (!file.elf)
95 return NULL;
96
97 if (opts.backup && !objtool_create_backup(objname)) {
98 WARN("can't create backup file");
99 return NULL;
100 }
101
102 hash_init(file.insn_hash);
103 INIT_LIST_HEAD(&file.retpoline_call_list);
104 INIT_LIST_HEAD(&file.return_thunk_list);
105 INIT_LIST_HEAD(&file.static_call_list);
106 INIT_LIST_HEAD(&file.mcount_loc_list);
107 INIT_LIST_HEAD(&file.endbr_list);
108 INIT_LIST_HEAD(&file.call_list);
109 file.ignore_unreachables = opts.no_unreachable;
110 file.hints = false;
111
112 return &file;
113}
114
115void objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func)
116{
117 if (!opts.noinstr)
118 return;
119
120 if (!f->pv_ops) {
121 WARN("paravirt confusion");
122 return;
123 }
124
125 /*
126 * These functions will be patched into native code,
127 * see paravirt_patch().
128 */
129 if (!strcmp(func->name, "_paravirt_nop") ||
130 !strcmp(func->name, "_paravirt_ident_64"))
131 return;
132
133 /* already added this function */
134 if (!list_empty(&func->pv_target))
135 return;
136
137 list_add(&func->pv_target, &f->pv_ops[idx].targets);
138 f->pv_ops[idx].clean = false;
139}
140
141int main(int argc, const char **argv)
142{
143 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
144
145 /* libsubcmd init */
146 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
147 pager_init(UNUSED);
148
149 return objtool_run(argc, argv);
150}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6/*
7 * objtool:
8 *
9 * The 'check' subcmd analyzes every .o file and ensures the validity of its
10 * stack trace metadata. It enforces a set of rules on asm code and C inline
11 * assembly code so that stack traces can be reliable.
12 *
13 * For more information, see tools/objtool/Documentation/stack-validation.txt.
14 */
15
16#include <stdio.h>
17#include <stdbool.h>
18#include <string.h>
19#include <stdlib.h>
20#include <subcmd/exec-cmd.h>
21#include <subcmd/pager.h>
22#include <linux/kernel.h>
23
24#include "builtin.h"
25
26struct cmd_struct {
27 const char *name;
28 int (*fn)(int, const char **);
29 const char *help;
30};
31
32static const char objtool_usage_string[] =
33 "objtool COMMAND [ARGS]";
34
35static struct cmd_struct objtool_cmds[] = {
36 {"check", cmd_check, "Perform stack metadata validation on an object file" },
37 {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
38};
39
40bool help;
41
42static void cmd_usage(void)
43{
44 unsigned int i, longest = 0;
45
46 printf("\n usage: %s\n\n", objtool_usage_string);
47
48 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
49 if (longest < strlen(objtool_cmds[i].name))
50 longest = strlen(objtool_cmds[i].name);
51 }
52
53 puts(" Commands:");
54 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
55 printf(" %-*s ", longest, objtool_cmds[i].name);
56 puts(objtool_cmds[i].help);
57 }
58
59 printf("\n");
60
61 if (!help)
62 exit(129);
63 exit(0);
64}
65
66static void handle_options(int *argc, const char ***argv)
67{
68 while (*argc > 0) {
69 const char *cmd = (*argv)[0];
70
71 if (cmd[0] != '-')
72 break;
73
74 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
75 help = true;
76 break;
77 } else {
78 fprintf(stderr, "Unknown option: %s\n", cmd);
79 cmd_usage();
80 }
81
82 (*argv)++;
83 (*argc)--;
84 }
85}
86
87static void handle_internal_command(int argc, const char **argv)
88{
89 const char *cmd = argv[0];
90 unsigned int i, ret;
91
92 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
93 struct cmd_struct *p = objtool_cmds+i;
94
95 if (strcmp(p->name, cmd))
96 continue;
97
98 ret = p->fn(argc, argv);
99
100 exit(ret);
101 }
102
103 cmd_usage();
104}
105
106int main(int argc, const char **argv)
107{
108 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
109
110 /* libsubcmd init */
111 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
112 pager_init(UNUSED);
113
114 argv++;
115 argc--;
116 handle_options(&argc, &argv);
117
118 if (!argc || help)
119 cmd_usage();
120
121 handle_internal_command(argc, argv);
122
123 return 0;
124}