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.
20 #include <subcmd/exec-cmd.h>
21 #include <subcmd/pager.h>
22 #include <linux/kernel.h>
24 #include <objtool/builtin.h>
25 #include <objtool/objtool.h>
26 #include <objtool/warn.h>
30 int (*fn)(int, const char **);
34 static const char objtool_usage_string[] =
35 "objtool COMMAND [ARGS]";
37 static struct cmd_struct objtool_cmds[] = {
38 {"check", cmd_check, "Perform stack metadata validation on an object file" },
39 {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
45 static struct objtool_file file;
47 struct objtool_file *objtool_open_read(const char *_objname)
50 if (strcmp(objname, _objname)) {
51 WARN("won't handle more than one file at a time");
58 file.elf = elf_open_read(objname, O_RDWR);
62 INIT_LIST_HEAD(&file.insn_list);
63 hash_init(file.insn_hash);
64 INIT_LIST_HEAD(&file.retpoline_call_list);
65 INIT_LIST_HEAD(&file.static_call_list);
66 INIT_LIST_HEAD(&file.mcount_loc_list);
67 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
68 file.ignore_unreachables = no_unreachable;
74 static void cmd_usage(void)
76 unsigned int i, longest = 0;
78 printf("\n usage: %s\n\n", objtool_usage_string);
80 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
81 if (longest < strlen(objtool_cmds[i].name))
82 longest = strlen(objtool_cmds[i].name);
86 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
87 printf(" %-*s ", longest, objtool_cmds[i].name);
88 puts(objtool_cmds[i].help);
98 static void handle_options(int *argc, const char ***argv)
101 const char *cmd = (*argv)[0];
106 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
110 fprintf(stderr, "Unknown option: %s\n", cmd);
119 static void handle_internal_command(int argc, const char **argv)
121 const char *cmd = argv[0];
124 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
125 struct cmd_struct *p = objtool_cmds+i;
127 if (strcmp(p->name, cmd))
130 ret = p->fn(argc, argv);
138 int main(int argc, const char **argv)
140 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
143 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
148 handle_options(&argc, &argv);
153 handle_internal_command(argc, argv);