treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / tools / objtool / objtool.c
blob0b3528f05053fa5214e7392ecf4c3c1851a923ae
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
6 /*
7 * objtool:
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.
13 * For more information, see tools/objtool/Documentation/stack-validation.txt.
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>
24 #include "builtin.h"
26 struct cmd_struct {
27 const char *name;
28 int (*fn)(int, const char **);
29 const char *help;
32 static const char objtool_usage_string[] =
33 "objtool COMMAND [ARGS]";
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" },
40 bool help;
42 static void cmd_usage(void)
44 unsigned int i, longest = 0;
46 printf("\n usage: %s\n\n", objtool_usage_string);
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);
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);
59 printf("\n");
61 exit(129);
64 static void handle_options(int *argc, const char ***argv)
66 while (*argc > 0) {
67 const char *cmd = (*argv)[0];
69 if (cmd[0] != '-')
70 break;
72 if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
73 help = true;
74 break;
75 } else {
76 fprintf(stderr, "Unknown option: %s\n", cmd);
77 cmd_usage();
80 (*argv)++;
81 (*argc)--;
85 static void handle_internal_command(int argc, const char **argv)
87 const char *cmd = argv[0];
88 unsigned int i, ret;
90 for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
91 struct cmd_struct *p = objtool_cmds+i;
93 if (strcmp(p->name, cmd))
94 continue;
96 ret = p->fn(argc, argv);
98 exit(ret);
101 cmd_usage();
104 int main(int argc, const char **argv)
106 static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
108 /* libsubcmd init */
109 exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
110 pager_init(UNUSED);
112 argv++;
113 argc--;
114 handle_options(&argc, &argv);
116 if (!argc || help)
117 cmd_usage();
119 handle_internal_command(argc, argv);
121 return 0;