blob: f6903552269383340d1212ad79eacaa3d15b2284 [file] [log] [blame]
Thomas Gleixner1ccea772019-05-19 15:51:43 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06002/*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06004 */
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>
Arnaldo Carvalho de Melo00614592017-04-17 11:58:55 -030022#include <linux/kernel.h>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060023
24#include "builtin.h"
Julien Thierry6545eb02020-08-25 13:47:39 +010025#include "objtool.h"
26#include "warn.h"
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060027
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060028struct cmd_struct {
29 const char *name;
30 int (*fn)(int, const char **);
31 const char *help;
32};
33
34static const char objtool_usage_string[] =
Josh Poimboeuf627fce12017-07-11 10:33:42 -050035 "objtool COMMAND [ARGS]";
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060036
37static struct cmd_struct objtool_cmds[] = {
38 {"check", cmd_check, "Perform stack metadata validation on an object file" },
Josh Poimboeuf627fce12017-07-11 10:33:42 -050039 {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060040};
41
42bool help;
43
Julien Thierry6545eb02020-08-25 13:47:39 +010044const char *objname;
45static struct objtool_file file;
46
47struct objtool_file *objtool_open_read(const char *_objname)
48{
49 if (objname) {
50 if (strcmp(objname, _objname)) {
51 WARN("won't handle more than one file at a time");
52 return NULL;
53 }
54 return &file;
55 }
56 objname = _objname;
57
58 file.elf = elf_open_read(objname, O_RDWR);
59 if (!file.elf)
60 return NULL;
61
62 INIT_LIST_HEAD(&file.insn_list);
63 hash_init(file.insn_hash);
Peter Zijlstra33092b42021-03-26 16:12:12 +010064 INIT_LIST_HEAD(&file.retpoline_call_list);
Peter Zijlstra8bdb25f2022-06-14 23:15:38 +020065 INIT_LIST_HEAD(&file.return_thunk_list);
Julien Thierry6545eb02020-08-25 13:47:39 +010066 INIT_LIST_HEAD(&file.static_call_list);
Peter Zijlstra7dcfcd42020-08-06 15:14:09 -070067 INIT_LIST_HEAD(&file.mcount_loc_list);
Julien Thierry6545eb02020-08-25 13:47:39 +010068 file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
69 file.ignore_unreachables = no_unreachable;
70 file.hints = false;
71
72 return &file;
73}
74
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060075static void cmd_usage(void)
76{
77 unsigned int i, longest = 0;
78
79 printf("\n usage: %s\n\n", objtool_usage_string);
80
81 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
82 if (longest < strlen(objtool_cmds[i].name))
83 longest = strlen(objtool_cmds[i].name);
84 }
85
86 puts(" Commands:");
87 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
88 printf(" %-*s ", longest, objtool_cmds[i].name);
89 puts(objtool_cmds[i].help);
90 }
91
92 printf("\n");
93
Matt Helsleyf15c6482020-05-19 13:55:31 -070094 if (!help)
95 exit(129);
96 exit(0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060097}
98
99static void handle_options(int *argc, const char ***argv)
100{
101 while (*argc > 0) {
102 const char *cmd = (*argv)[0];
103
104 if (cmd[0] != '-')
105 break;
106
107 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
108 help = true;
109 break;
110 } else {
111 fprintf(stderr, "Unknown option: %s\n", cmd);
Kamalesh Babulal6a93bb72017-10-14 20:17:54 +0530112 cmd_usage();
Josh Poimboeuf442f04c2016-02-28 22:22:41 -0600113 }
114
115 (*argv)++;
116 (*argc)--;
117 }
118}
119
120static void handle_internal_command(int argc, const char **argv)
121{
122 const char *cmd = argv[0];
123 unsigned int i, ret;
124
125 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
126 struct cmd_struct *p = objtool_cmds+i;
127
128 if (strcmp(p->name, cmd))
129 continue;
130
131 ret = p->fn(argc, argv);
132
133 exit(ret);
134 }
135
136 cmd_usage();
137}
138
139int main(int argc, const char **argv)
140{
141 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
142
143 /* libsubcmd init */
144 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
145 pager_init(UNUSED);
146
147 argv++;
148 argc--;
149 handle_options(&argc, &argv);
150
151 if (!argc || help)
152 cmd_usage();
153
154 handle_internal_command(argc, argv);
155
156 return 0;
157}