David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015-2017 Josh Poimboeuf <jpoimboe@redhat.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [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 | |
| 16 | #include <subcmd/parse-options.h> |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 17 | #include <string.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 18 | #include "builtin.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 19 | #include "objtool.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 21 | bool no_fp, no_unreachable, retpoline, module, backtrace, uaccess, stats, |
| 22 | validate_dup, vmlinux, sls, unret, rethunk; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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"), |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 33 | OPT_BOOLEAN(0, "rethunk", &rethunk, "validate and annotate rethunk usage"), |
| 34 | OPT_BOOLEAN(0, "unret", &unret, "validate entry unret placement"), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 35 | OPT_BOOLEAN('m', "module", &module, "Indicates the object will be part of a kernel module"), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 36 | OPT_BOOLEAN('b', "backtrace", &backtrace, "unwind on error"), |
| 37 | OPT_BOOLEAN('a', "uaccess", &uaccess, "enable uaccess checking"), |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 38 | OPT_BOOLEAN('s', "stats", &stats, "print statistics"), |
| 39 | OPT_BOOLEAN('d', "duplicate", &validate_dup, "duplicate validation for vmlinux.o"), |
| 40 | OPT_BOOLEAN('l', "vmlinux", &vmlinux, "vmlinux.o validation"), |
Olivier Deprez | 92d4c21 | 2022-12-06 15:05:30 +0100 | [diff] [blame^] | 41 | OPT_BOOLEAN('S', "sls", &sls, "validate straight-line-speculation"), |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 42 | OPT_END(), |
| 43 | }; |
| 44 | |
| 45 | int cmd_check(int argc, const char **argv) |
| 46 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 47 | const char *objname, *s; |
| 48 | struct objtool_file *file; |
| 49 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 50 | |
| 51 | argc = parse_options(argc, argv, check_options, check_usage, 0); |
| 52 | |
| 53 | if (argc != 1) |
| 54 | usage_with_options(check_usage, check_options); |
| 55 | |
| 56 | objname = argv[0]; |
| 57 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame] | 58 | s = strstr(objname, "vmlinux.o"); |
| 59 | if (s && !s[9]) |
| 60 | vmlinux = true; |
| 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; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 74 | } |