blob: 58fdda510653bc216afbb9fad8584860103d9ae5 [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"
25
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060026struct cmd_struct {
27 const char *name;
28 int (*fn)(int, const char **);
29 const char *help;
30};
31
32static const char objtool_usage_string[] =
Josh Poimboeuf627fce12017-07-11 10:33:42 -050033 "objtool COMMAND [ARGS]";
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060034
35static struct cmd_struct objtool_cmds[] = {
36 {"check", cmd_check, "Perform stack metadata validation on an object file" },
Josh Poimboeuf627fce12017-07-11 10:33:42 -050037 {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060038};
39
40bool help;
41
42static 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
Matt Helsleyf15c6482020-05-19 13:55:31 -070061 if (!help)
62 exit(129);
63 exit(0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060064}
65
66static 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);
Kamalesh Babulal6a93bb72017-10-14 20:17:54 +053079 cmd_usage();
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060080 }
81
82 (*argv)++;
83 (*argc)--;
84 }
85}
86
87static 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
106int 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}