xref: /linux/tools/objtool/objtool.c (revision 95298d63c67673c654c08952672d016212b26054)
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 
26 struct cmd_struct {
27 	const char *name;
28 	int (*fn)(int, const char **);
29 	const char *help;
30 };
31 
32 static const char objtool_usage_string[] =
33 	"objtool COMMAND [ARGS]";
34 
35 static struct cmd_struct objtool_cmds[] = {
36 	{"check",	cmd_check,	"Perform stack metadata validation on an object file" },
37 	{"orc",		cmd_orc,	"Generate in-place ORC unwind tables for an object file" },
38 };
39 
40 bool help;
41 
42 static void cmd_usage(void)
43 {
44 	unsigned int i, longest = 0;
45 
46 	printf("\n usage: %s\n\n", objtool_usage_string);
47 
48 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
49 		if (longest < strlen(objtool_cmds[i].name))
50 			longest = strlen(objtool_cmds[i].name);
51 	}
52 
53 	puts(" Commands:");
54 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
55 		printf("   %-*s   ", longest, objtool_cmds[i].name);
56 		puts(objtool_cmds[i].help);
57 	}
58 
59 	printf("\n");
60 
61 	if (!help)
62 		exit(129);
63 	exit(0);
64 }
65 
66 static void handle_options(int *argc, const char ***argv)
67 {
68 	while (*argc > 0) {
69 		const char *cmd = (*argv)[0];
70 
71 		if (cmd[0] != '-')
72 			break;
73 
74 		if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
75 			help = true;
76 			break;
77 		} else {
78 			fprintf(stderr, "Unknown option: %s\n", cmd);
79 			cmd_usage();
80 		}
81 
82 		(*argv)++;
83 		(*argc)--;
84 	}
85 }
86 
87 static void handle_internal_command(int argc, const char **argv)
88 {
89 	const char *cmd = argv[0];
90 	unsigned int i, ret;
91 
92 	for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
93 		struct cmd_struct *p = objtool_cmds+i;
94 
95 		if (strcmp(p->name, cmd))
96 			continue;
97 
98 		ret = p->fn(argc, argv);
99 
100 		exit(ret);
101 	}
102 
103 	cmd_usage();
104 }
105 
106 int main(int argc, const char **argv)
107 {
108 	static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
109 
110 	/* libsubcmd init */
111 	exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
112 	pager_init(UNUSED);
113 
114 	argv++;
115 	argc--;
116 	handle_options(&argc, &argv);
117 
118 	if (!argc || help)
119 		cmd_usage();
120 
121 	handle_internal_command(argc, argv);
122 
123 	return 0;
124 }
125