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 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
132 file.ignore_unreachables = no_unreachable;
138 static void cmd_usage(void)
140 unsigned int i, longest = 0;
142 printf("\n usage: %s\n\n", objtool_usage_string);
144 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
145 if (longest < strlen(objtool_cmds[i].name))
146 longest = strlen(objtool_cmds[i].name);
150 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
151 printf(" %-*s ", longest, objtool_cmds[i].name);
152 puts(objtool_cmds[i].help);
162 static void handle_options(int *argc, const char ***argv)
165 const char *cmd = (*argv)[0];
170 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
174 fprintf(stderr, "Unknown option: %s\n", cmd);
183 static void handle_internal_command(int argc, const char **argv)
185 const char *cmd = argv[0];
188 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
189 struct cmd_struct *p = objtool_cmds+i;
191 if (strcmp(p->name, cmd))
194 ret = p->fn(argc, argv);
202 int main(int argc, const char **argv)
204 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
207 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
212 handle_options(&argc, &argv);
217 handle_internal_command(argc, argv);