Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 2 | /* |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 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 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 16 | #include <subcmd/parse-options.h> |
Peter Zijlstra | c4a3393 | 2020-03-10 18:57:41 +0100 | [diff] [blame] | 17 | #include <string.h> |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 18 | #include "builtin.h" |
Matt Helsley | 0decf1f | 2020-05-19 13:55:33 -0700 | [diff] [blame] | 19 | #include "objtool.h" |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 20 | |
Peter Zijlstra | c4a3393 | 2020-03-10 18:57:41 +0100 | [diff] [blame] | 21 | bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, validate_dup, vmlinux; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 22 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 23 | static const char * const check_usage[] = { |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 24 | "objtool check [<options>] file.o", |
| 25 | NULL, |
| 26 | }; |
| 27 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 28 | const struct option check_options[] = { |
Josh Poimboeuf | 867ac9d | 2017-07-24 18:34:14 -0500 | [diff] [blame] | 29 | OPT_BOOLEAN('f', "no-fp", &no_fp, "Skip frame pointer validation"), |
| 30 | OPT_BOOLEAN('u', "no-unreachable", &no_unreachable, "Skip 'unreachable instruction' warnings"), |
Peter Zijlstra | b5bc223 | 2018-01-16 10:24:06 +0100 | [diff] [blame] | 31 | OPT_BOOLEAN('r', "retpoline", &retpoline, "Validate retpoline assumptions"), |
Peter Zijlstra | ca41b97 | 2018-01-31 10:18:28 +0100 | [diff] [blame] | 32 | OPT_BOOLEAN('m', "module", &module, "Indicates the object will be part of a kernel module"), |
Peter Zijlstra | 7697eee | 2019-03-01 11:15:49 +0100 | [diff] [blame] | 33 | OPT_BOOLEAN('b', "backtrace", &backtrace, "unwind on error"), |
Peter Zijlstra | ea24213 | 2019-02-25 12:50:09 +0100 | [diff] [blame] | 34 | OPT_BOOLEAN('a', "uaccess", &uaccess, "enable uaccess checking"), |
Peter Zijlstra | 1e11f3f | 2020-03-12 09:26:29 +0100 | [diff] [blame] | 35 | OPT_BOOLEAN('s', "stats", &stats, "print statistics"), |
Peter Zijlstra | c4a3393 | 2020-03-10 18:57:41 +0100 | [diff] [blame] | 36 | OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"), |
| 37 | OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"), |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 38 | OPT_END(), |
| 39 | }; |
| 40 | |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 41 | int cmd_check(int argc, const char **argv) |
| 42 | { |
Peter Zijlstra | c4a3393 | 2020-03-10 18:57:41 +0100 | [diff] [blame] | 43 | const char *objname, *s; |
Julien Thierry | 6545eb0 | 2020-08-25 13:47:39 +0100 | [diff] [blame] | 44 | struct objtool_file *file; |
Julien Thierry | d44becb | 2020-08-25 13:47:40 +0100 | [diff] [blame] | 45 | int ret; |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 46 | |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 47 | argc = parse_options(argc, argv, check_options, check_usage, 0); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 48 | |
| 49 | if (argc != 1) |
Josh Poimboeuf | dcc914f | 2017-06-28 10:11:05 -0500 | [diff] [blame] | 50 | usage_with_options(check_usage, check_options); |
Josh Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 51 | |
| 52 | objname = argv[0]; |
| 53 | |
Peter Zijlstra | c4a3393 | 2020-03-10 18:57:41 +0100 | [diff] [blame] | 54 | s = strstr(objname, "vmlinux.o"); |
| 55 | if (s && !s[9]) |
| 56 | vmlinux = true; |
| 57 | |
Julien Thierry | 6545eb0 | 2020-08-25 13:47:39 +0100 | [diff] [blame] | 58 | file = objtool_open_read(objname); |
| 59 | if (!file) |
| 60 | return 1; |
| 61 | |
Julien Thierry | d44becb | 2020-08-25 13:47:40 +0100 | [diff] [blame] | 62 | 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 Poimboeuf | 442f04c | 2016-02-28 22:22:41 -0600 | [diff] [blame] | 70 | } |