• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "objtool.h"
26 #include "warn.h"
27 
28 struct cmd_struct {
29 	const char *name;
30 	int (*fn)(int, const char **);
31 	const char *help;
32 };
33 
34 static const char objtool_usage_string[] =
35 	"objtool COMMAND [ARGS]";
36 
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" },
40 };
41 
42 bool help;
43 
44 const char *objname;
45 static struct objtool_file file;
46 
objtool_open_read(const char * _objname)47 struct 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);
64 	INIT_LIST_HEAD(&file.retpoline_call_list);
65 	INIT_LIST_HEAD(&file.return_thunk_list);
66 	INIT_LIST_HEAD(&file.static_call_list);
67 	INIT_LIST_HEAD(&file.mcount_loc_list);
68 	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 
cmd_usage(void)75 static 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 
94 	if (!help)
95 		exit(129);
96 	exit(0);
97 }
98 
handle_options(int * argc,const char *** argv)99 static 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);
112 			cmd_usage();
113 		}
114 
115 		(*argv)++;
116 		(*argc)--;
117 	}
118 }
119 
handle_internal_command(int argc,const char ** argv)120 static 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 
main(int argc,const char ** argv)139 int 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 }
158