xref: /openbmc/linux/tools/objtool/builtin-check.c (revision a2f605f9ff57397d05a8e2f282b78a69f574d305)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 /*
7  * objtool check:
8  *
9  * This command analyzes every .o file and ensures the validity of its stack
10  * 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 <subcmd/parse-options.h>
17 #include <string.h>
18 #include <objtool/builtin.h>
19 #include <objtool/objtool.h>
20 
21 bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
22      validate_dup, vmlinux, mcount, noinstr, backup;
23 
24 static const char * const check_usage[] = {
25 	"objtool check [<options>] file.o",
26 	NULL,
27 };
28 
29 const struct option check_options[] = {
30 	OPT_BOOLEAN('f', "no-fp", &no_fp, "Skip frame pointer validation"),
31 	OPT_BOOLEAN('u', "no-unreachable", &no_unreachable, "Skip 'unreachable instruction' warnings"),
32 	OPT_BOOLEAN('r', "retpoline", &retpoline, "Validate retpoline assumptions"),
33 	OPT_BOOLEAN('m', "module", &module, "Indicates the object will be part of a kernel module"),
34 	OPT_BOOLEAN('b', "backtrace", &backtrace, "unwind on error"),
35 	OPT_BOOLEAN('a', "uaccess", &uaccess, "enable uaccess checking"),
36 	OPT_BOOLEAN('s', "stats", &stats, "print statistics"),
37 	OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"),
38 	OPT_BOOLEAN('n', "noinstr", &noinstr, "noinstr validation for vmlinux.o"),
39 	OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
40 	OPT_BOOLEAN('M', "mcount", &mcount, "generate __mcount_loc"),
41 	OPT_BOOLEAN('B', "backup", &backup, "create .orig files before modification"),
42 	OPT_END(),
43 };
44 
45 int cmd_parse_options(int argc, const char **argv, const char * const usage[])
46 {
47 	argc = parse_options(argc, argv, check_options, usage, 0);
48 	if (argc != 1)
49 		usage_with_options(usage, check_options);
50 	return argc;
51 }
52 
53 int cmd_check(int argc, const char **argv)
54 {
55 	const char *objname;
56 	struct objtool_file *file;
57 	int ret;
58 
59 	argc = cmd_parse_options(argc, argv, check_usage);
60 	objname = argv[0];
61 
62 	file = objtool_open_read(objname);
63 	if (!file)
64 		return 1;
65 
66 	ret = check(file);
67 	if (ret)
68 		return ret;
69 
70 	if (file->elf->changed)
71 		return elf_write(file->elf);
72 
73 	return 0;
74 }
75