xref: /openbmc/linux/tools/objtool/objtool.c (revision 43d5430ad74ef5156353af7aec352426ec7a8e57)
11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2442f04c3SJosh Poimboeuf /*
3442f04c3SJosh Poimboeuf  * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4442f04c3SJosh Poimboeuf  */
5442f04c3SJosh Poimboeuf 
6442f04c3SJosh Poimboeuf /*
7442f04c3SJosh Poimboeuf  * objtool:
8442f04c3SJosh Poimboeuf  *
9442f04c3SJosh Poimboeuf  * The 'check' subcmd analyzes every .o file and ensures the validity of its
10442f04c3SJosh Poimboeuf  * stack trace metadata.  It enforces a set of rules on asm code and C inline
11442f04c3SJosh Poimboeuf  * assembly code so that stack traces can be reliable.
12442f04c3SJosh Poimboeuf  *
13442f04c3SJosh Poimboeuf  * For more information, see tools/objtool/Documentation/stack-validation.txt.
14442f04c3SJosh Poimboeuf  */
15442f04c3SJosh Poimboeuf 
16442f04c3SJosh Poimboeuf #include <stdio.h>
17442f04c3SJosh Poimboeuf #include <stdbool.h>
18442f04c3SJosh Poimboeuf #include <string.h>
19442f04c3SJosh Poimboeuf #include <stdlib.h>
20442f04c3SJosh Poimboeuf #include <subcmd/exec-cmd.h>
21442f04c3SJosh Poimboeuf #include <subcmd/pager.h>
2200614597SArnaldo Carvalho de Melo #include <linux/kernel.h>
23442f04c3SJosh Poimboeuf 
247786032eSVasily Gorbik #include <objtool/builtin.h>
257786032eSVasily Gorbik #include <objtool/objtool.h>
267786032eSVasily Gorbik #include <objtool/warn.h>
27442f04c3SJosh Poimboeuf 
28442f04c3SJosh Poimboeuf struct cmd_struct {
29442f04c3SJosh Poimboeuf 	const char *name;
30442f04c3SJosh Poimboeuf 	int (*fn)(int, const char **);
31442f04c3SJosh Poimboeuf 	const char *help;
32442f04c3SJosh Poimboeuf };
33442f04c3SJosh Poimboeuf 
34442f04c3SJosh Poimboeuf static const char objtool_usage_string[] =
35627fce14SJosh Poimboeuf 	"objtool COMMAND [ARGS]";
36442f04c3SJosh Poimboeuf 
37442f04c3SJosh Poimboeuf static struct cmd_struct objtool_cmds[] = {
38442f04c3SJosh Poimboeuf 	{"check",	cmd_check,	"Perform stack metadata validation on an object file" },
39627fce14SJosh Poimboeuf 	{"orc",		cmd_orc,	"Generate in-place ORC unwind tables for an object file" },
40442f04c3SJosh Poimboeuf };
41442f04c3SJosh Poimboeuf 
42442f04c3SJosh Poimboeuf bool help;
43442f04c3SJosh Poimboeuf 
446545eb03SJulien Thierry const char *objname;
456545eb03SJulien Thierry static struct objtool_file file;
466545eb03SJulien Thierry 
476545eb03SJulien Thierry struct objtool_file *objtool_open_read(const char *_objname)
486545eb03SJulien Thierry {
496545eb03SJulien Thierry 	if (objname) {
506545eb03SJulien Thierry 		if (strcmp(objname, _objname)) {
516545eb03SJulien Thierry 			WARN("won't handle more than one file at a time");
526545eb03SJulien Thierry 			return NULL;
536545eb03SJulien Thierry 		}
546545eb03SJulien Thierry 		return &file;
556545eb03SJulien Thierry 	}
566545eb03SJulien Thierry 	objname = _objname;
576545eb03SJulien Thierry 
586545eb03SJulien Thierry 	file.elf = elf_open_read(objname, O_RDWR);
596545eb03SJulien Thierry 	if (!file.elf)
606545eb03SJulien Thierry 		return NULL;
616545eb03SJulien Thierry 
626545eb03SJulien Thierry 	INIT_LIST_HEAD(&file.insn_list);
636545eb03SJulien Thierry 	hash_init(file.insn_hash);
64*43d5430aSPeter Zijlstra 	INIT_LIST_HEAD(&file.retpoline_call_list);
656545eb03SJulien Thierry 	INIT_LIST_HEAD(&file.static_call_list);
6699d00215SPeter Zijlstra 	INIT_LIST_HEAD(&file.mcount_loc_list);
676545eb03SJulien Thierry 	file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
686545eb03SJulien Thierry 	file.ignore_unreachables = no_unreachable;
696545eb03SJulien Thierry 	file.hints = false;
706545eb03SJulien Thierry 
716545eb03SJulien Thierry 	return &file;
726545eb03SJulien Thierry }
736545eb03SJulien Thierry 
74442f04c3SJosh Poimboeuf static void cmd_usage(void)
75442f04c3SJosh Poimboeuf {
76442f04c3SJosh Poimboeuf 	unsigned int i, longest = 0;
77442f04c3SJosh Poimboeuf 
78442f04c3SJosh Poimboeuf 	printf("\n usage: %s\n\n", objtool_usage_string);
79442f04c3SJosh Poimboeuf 
80442f04c3SJosh Poimboeuf 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
81442f04c3SJosh Poimboeuf 		if (longest < strlen(objtool_cmds[i].name))
82442f04c3SJosh Poimboeuf 			longest = strlen(objtool_cmds[i].name);
83442f04c3SJosh Poimboeuf 	}
84442f04c3SJosh Poimboeuf 
85442f04c3SJosh Poimboeuf 	puts(" Commands:");
86442f04c3SJosh Poimboeuf 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
87442f04c3SJosh Poimboeuf 		printf("   %-*s   ", longest, objtool_cmds[i].name);
88442f04c3SJosh Poimboeuf 		puts(objtool_cmds[i].help);
89442f04c3SJosh Poimboeuf 	}
90442f04c3SJosh Poimboeuf 
91442f04c3SJosh Poimboeuf 	printf("\n");
92442f04c3SJosh Poimboeuf 
93f15c648fSMatt Helsley 	if (!help)
946a93bb7eSKamalesh Babulal 		exit(129);
95f15c648fSMatt Helsley 	exit(0);
96442f04c3SJosh Poimboeuf }
97442f04c3SJosh Poimboeuf 
98442f04c3SJosh Poimboeuf static void handle_options(int *argc, const char ***argv)
99442f04c3SJosh Poimboeuf {
100442f04c3SJosh Poimboeuf 	while (*argc > 0) {
101442f04c3SJosh Poimboeuf 		const char *cmd = (*argv)[0];
102442f04c3SJosh Poimboeuf 
103442f04c3SJosh Poimboeuf 		if (cmd[0] != '-')
104442f04c3SJosh Poimboeuf 			break;
105442f04c3SJosh Poimboeuf 
106442f04c3SJosh Poimboeuf 		if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
107442f04c3SJosh Poimboeuf 			help = true;
108442f04c3SJosh Poimboeuf 			break;
109442f04c3SJosh Poimboeuf 		} else {
110442f04c3SJosh Poimboeuf 			fprintf(stderr, "Unknown option: %s\n", cmd);
1116a93bb7eSKamalesh Babulal 			cmd_usage();
112442f04c3SJosh Poimboeuf 		}
113442f04c3SJosh Poimboeuf 
114442f04c3SJosh Poimboeuf 		(*argv)++;
115442f04c3SJosh Poimboeuf 		(*argc)--;
116442f04c3SJosh Poimboeuf 	}
117442f04c3SJosh Poimboeuf }
118442f04c3SJosh Poimboeuf 
119442f04c3SJosh Poimboeuf static void handle_internal_command(int argc, const char **argv)
120442f04c3SJosh Poimboeuf {
121442f04c3SJosh Poimboeuf 	const char *cmd = argv[0];
122442f04c3SJosh Poimboeuf 	unsigned int i, ret;
123442f04c3SJosh Poimboeuf 
124442f04c3SJosh Poimboeuf 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
125442f04c3SJosh Poimboeuf 		struct cmd_struct *p = objtool_cmds+i;
126442f04c3SJosh Poimboeuf 
127442f04c3SJosh Poimboeuf 		if (strcmp(p->name, cmd))
128442f04c3SJosh Poimboeuf 			continue;
129442f04c3SJosh Poimboeuf 
130442f04c3SJosh Poimboeuf 		ret = p->fn(argc, argv);
131442f04c3SJosh Poimboeuf 
132442f04c3SJosh Poimboeuf 		exit(ret);
133442f04c3SJosh Poimboeuf 	}
134442f04c3SJosh Poimboeuf 
135442f04c3SJosh Poimboeuf 	cmd_usage();
136442f04c3SJosh Poimboeuf }
137442f04c3SJosh Poimboeuf 
138442f04c3SJosh Poimboeuf int main(int argc, const char **argv)
139442f04c3SJosh Poimboeuf {
140442f04c3SJosh Poimboeuf 	static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
141442f04c3SJosh Poimboeuf 
142442f04c3SJosh Poimboeuf 	/* libsubcmd init */
143442f04c3SJosh Poimboeuf 	exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
144442f04c3SJosh Poimboeuf 	pager_init(UNUSED);
145442f04c3SJosh Poimboeuf 
146442f04c3SJosh Poimboeuf 	argv++;
147442f04c3SJosh Poimboeuf 	argc--;
148442f04c3SJosh Poimboeuf 	handle_options(&argc, &argv);
149442f04c3SJosh Poimboeuf 
150442f04c3SJosh Poimboeuf 	if (!argc || help)
151442f04c3SJosh Poimboeuf 		cmd_usage();
152442f04c3SJosh Poimboeuf 
153442f04c3SJosh Poimboeuf 	handle_internal_command(argc, argv);
154442f04c3SJosh Poimboeuf 
155442f04c3SJosh Poimboeuf 	return 0;
156442f04c3SJosh Poimboeuf }
157