xref: /OK3568_Linux_fs/kernel/tools/objtool/builtin-check.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-or-later
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun /*
7*4882a593Smuzhiyun  * objtool check:
8*4882a593Smuzhiyun  *
9*4882a593Smuzhiyun  * This command analyzes every .o file and ensures the validity of its stack
10*4882a593Smuzhiyun  * trace metadata.  It enforces a set of rules on asm code and C inline
11*4882a593Smuzhiyun  * assembly code so that stack traces can be reliable.
12*4882a593Smuzhiyun  *
13*4882a593Smuzhiyun  * For more information, see tools/objtool/Documentation/stack-validation.txt.
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <subcmd/parse-options.h>
17*4882a593Smuzhiyun #include <string.h>
18*4882a593Smuzhiyun #include "builtin.h"
19*4882a593Smuzhiyun #include "objtool.h"
20*4882a593Smuzhiyun 
21*4882a593Smuzhiyun bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats,
22*4882a593Smuzhiyun      validate_dup, vmlinux, mcount, noinstr, sls, unret, rethunk;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun static const char * const check_usage[] = {
25*4882a593Smuzhiyun 	"objtool check [<options>] file.o",
26*4882a593Smuzhiyun 	NULL,
27*4882a593Smuzhiyun };
28*4882a593Smuzhiyun 
29*4882a593Smuzhiyun const struct option check_options[] = {
30*4882a593Smuzhiyun 	OPT_BOOLEAN('f', "no-fp", &no_fp, "Skip frame pointer validation"),
31*4882a593Smuzhiyun 	OPT_BOOLEAN('u', "no-unreachable", &no_unreachable, "Skip 'unreachable instruction' warnings"),
32*4882a593Smuzhiyun 	OPT_BOOLEAN('r', "retpoline", &retpoline, "Validate retpoline assumptions"),
33*4882a593Smuzhiyun 	OPT_BOOLEAN(0,   "rethunk", &rethunk, "validate and annotate rethunk usage"),
34*4882a593Smuzhiyun 	OPT_BOOLEAN(0,   "unret", &unret, "validate entry unret placement"),
35*4882a593Smuzhiyun 	OPT_BOOLEAN('m', "module", &module, "Indicates the object will be part of a kernel module"),
36*4882a593Smuzhiyun 	OPT_BOOLEAN('b', "backtrace", &backtrace, "unwind on error"),
37*4882a593Smuzhiyun 	OPT_BOOLEAN('a', "uaccess", &uaccess, "enable uaccess checking"),
38*4882a593Smuzhiyun 	OPT_BOOLEAN('s', "stats", &stats, "print statistics"),
39*4882a593Smuzhiyun 	OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"),
40*4882a593Smuzhiyun 	OPT_BOOLEAN('n', "noinstr", &noinstr, "noinstr validation for vmlinux.o"),
41*4882a593Smuzhiyun 	OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
42*4882a593Smuzhiyun 	OPT_BOOLEAN('M', "mcount", &mcount, "generate __mcount_loc"),
43*4882a593Smuzhiyun 	OPT_BOOLEAN('S', "sls", &sls, "validate straight-line-speculation"),
44*4882a593Smuzhiyun 	OPT_END(),
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun 
cmd_check(int argc,const char ** argv)47*4882a593Smuzhiyun int cmd_check(int argc, const char **argv)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	const char *objname;
50*4882a593Smuzhiyun 	struct objtool_file *file;
51*4882a593Smuzhiyun 	int ret;
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	argc = parse_options(argc, argv, check_options, check_usage, 0);
54*4882a593Smuzhiyun 
55*4882a593Smuzhiyun 	if (argc != 1)
56*4882a593Smuzhiyun 		usage_with_options(check_usage, check_options);
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	objname = argv[0];
59*4882a593Smuzhiyun 
60*4882a593Smuzhiyun 	file = objtool_open_read(objname);
61*4882a593Smuzhiyun 	if (!file)
62*4882a593Smuzhiyun 		return 1;
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	ret = check(file);
65*4882a593Smuzhiyun 	if (ret)
66*4882a593Smuzhiyun 		return ret;
67*4882a593Smuzhiyun 
68*4882a593Smuzhiyun 	if (file->elf->changed)
69*4882a593Smuzhiyun 		return elf_write(file->elf);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	return 0;
72*4882a593Smuzhiyun }
73