1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
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.
13 * For more information, see tools/objtool/Documentation/stack-validation.txt.
21 #include <subcmd/exec-cmd.h>
22 #include <subcmd/pager.h>
23 #include <linux/kernel.h>
25 #include <objtool/builtin.h>
26 #include <objtool/objtool.h>
27 #include <objtool/warn.h>
31 int (*fn)(int, const char **);
35 static const char objtool_usage_string[] =
36 "objtool COMMAND [ARGS]";
38 static struct cmd_struct objtool_cmds[] = {
39 {"check", cmd_check, "Perform stack metadata validation on an object file" },
40 {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
46 static struct objtool_file file;
48 static bool objtool_create_backup(const char *_objname)
50 int len = strlen(_objname);
51 char *buf, *base, *name = malloc(len+6);
55 perror("failed backup name malloc");
59 strcpy(name, _objname);
60 strcpy(name + len, ".orig");
62 d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644);
64 perror("failed to create backup file");
68 s = open(_objname, O_RDONLY);
70 perror("failed to open orig file");
76 perror("failed backup data malloc");
80 while ((l = read(s, buf, 4096)) > 0) {
83 t = write(d, base, l);
85 perror("failed backup write");
94 perror("failed backup read");
106 struct objtool_file *objtool_open_read(const char *_objname)
109 if (strcmp(objname, _objname)) {
110 WARN("won't handle more than one file at a time");
117 file.elf = elf_open_read(objname, O_RDWR);
121 if (backup && !objtool_create_backup(objname)) {
122 WARN("can't create backup file");
126 INIT_LIST_HEAD(&file.insn_list);
127 hash_init(file.insn_hash);
128 INIT_LIST_HEAD(&file.retpoline_call_list);
129 INIT_LIST_HEAD(&file.static_call_list);
130 INIT_LIST_HEAD(&file.mcount_loc_list);
131 INIT_LIST_HEAD(&file.endbr_list);
132 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
133 file.ignore_unreachables = no_unreachable;
139 void objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func)
145 WARN("paravirt confusion");
150 * These functions will be patched into native code,
151 * see paravirt_patch().
153 if (!strcmp(func->name, "_paravirt_nop") ||
154 !strcmp(func->name, "_paravirt_ident_64"))
157 /* already added this function */
158 if (!list_empty(&func->pv_target))
161 list_add(&func->pv_target, &f->pv_ops[idx].targets);
162 f->pv_ops[idx].clean = false;
165 static void cmd_usage(void)
167 unsigned int i, longest = 0;
169 printf("\n usage: %s\n\n", objtool_usage_string);
171 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
172 if (longest < strlen(objtool_cmds[i].name))
173 longest = strlen(objtool_cmds[i].name);
177 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
178 printf(" %-*s ", longest, objtool_cmds[i].name);
179 puts(objtool_cmds[i].help);
189 static void handle_options(int *argc, const char ***argv)
192 const char *cmd = (*argv)[0];
197 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
201 fprintf(stderr, "Unknown option: %s\n", cmd);
210 static void handle_internal_command(int argc, const char **argv)
212 const char *cmd = argv[0];
215 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
216 struct cmd_struct *p = objtool_cmds+i;
218 if (strcmp(p->name, cmd))
221 ret = p->fn(argc, argv);
229 int main(int argc, const char **argv)
231 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
234 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
239 handle_options(&argc, &argv);
244 handle_internal_command(argc, argv);