blob: c6d199bfd0ae2a725826d7f2950aec98ca3dda83 [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/*
Josh Poimboeufdcc914f2017-06-28 10:11:05 -05003 * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -06004 */
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
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060016#include <subcmd/parse-options.h>
Peter Zijlstrac4a33932020-03-10 18:57:41 +010017#include <string.h>
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060018#include "builtin.h"
Matt Helsley0decf1f2020-05-19 13:55:33 -070019#include "objtool.h"
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060020
Peter Zijlstrac4a33932020-03-10 18:57:41 +010021bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060022
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050023static const char * const check_usage[] = {
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060024 "objtool check [<options>] file.o",
25 NULL,
26};
27
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050028const struct option check_options[] = {
Josh Poimboeuf867ac9d2017-07-24 18:34:14 -050029 OPT_BOOLEAN('f', "no-fp", &no_fp, "Skip frame pointer validation"),
30 OPT_BOOLEAN('u', "no-unreachable", &no_unreachable, "Skip 'unreachable instruction' warnings"),
Peter Zijlstrab5bc2232018-01-16 10:24:06 +010031 OPT_BOOLEAN('r', "retpoline", &retpoline, "Validate retpoline assumptions"),
Peter Zijlstraca41b972018-01-31 10:18:28 +010032 OPT_BOOLEAN('m', "module", &module, "Indicates the object will be part of a kernel module"),
Peter Zijlstra7697eee2019-03-01 11:15:49 +010033 OPT_BOOLEAN('b', "backtrace", &backtrace, "unwind on error"),
Peter Zijlstraea242132019-02-25 12:50:09 +010034 OPT_BOOLEAN('a', "uaccess", &uaccess, "enable uaccess checking"),
Peter Zijlstra1e11f3f2020-03-12 09:26:29 +010035 OPT_BOOLEAN('s', "stats", &stats, "print statistics"),
Peter Zijlstrac4a33932020-03-10 18:57:41 +010036 OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"),
37 OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"),
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050038 OPT_END(),
39};
40
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060041int cmd_check(int argc, const char **argv)
42{
Peter Zijlstrac4a33932020-03-10 18:57:41 +010043 const char *objname, *s;
Julien Thierry6545eb02020-08-25 13:47:39 +010044 struct objtool_file *file;
Julien Thierryd44becb2020-08-25 13:47:40 +010045 int ret;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060046
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050047 argc = parse_options(argc, argv, check_options, check_usage, 0);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060048
49 if (argc != 1)
Josh Poimboeufdcc914f2017-06-28 10:11:05 -050050 usage_with_options(check_usage, check_options);
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060051
52 objname = argv[0];
53
Peter Zijlstrac4a33932020-03-10 18:57:41 +010054 s = strstr(objname, "vmlinux.o");
55 if (s && !s[9])
56 vmlinux = true;
57
Julien Thierry6545eb02020-08-25 13:47:39 +010058 file = objtool_open_read(objname);
59 if (!file)
60 return 1;
61
Julien Thierryd44becb2020-08-25 13:47:40 +010062 ret = check(file);
63 if (ret)
64 return ret;
65
66 if (file->elf->changed)
67 return elf_write(file->elf);
68
69 return 0;
Josh Poimboeuf442f04c2016-02-28 22:22:41 -060070}