Thomas Gleixner | 1ccea77 | 2019-05-19 15:51:43 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com> |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | /* |
| 7 | * objtool orc: |
| 8 | * |
| 9 | * This command analyzes a .o file and adds .orc_unwind and .orc_unwind_ip |
| 10 | * sections to it, which is used by the in-kernel ORC unwinder. |
| 11 | * |
| 12 | * This command is a superset of "objtool check". |
| 13 | */ |
| 14 | |
| 15 | #include <string.h> |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 16 | #include "builtin.h" |
Matt Helsley | 0decf1f | 2020-05-19 13:55:33 -0700 | [diff] [blame] | 17 | #include "objtool.h" |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 18 | |
| 19 | static const char *orc_usage[] = { |
| 20 | "objtool orc generate [<options>] file.o", |
| 21 | "objtool orc dump file.o", |
| 22 | NULL, |
| 23 | }; |
| 24 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 25 | int cmd_orc(int argc, const char **argv) |
| 26 | { |
| 27 | const char *objname; |
| 28 | |
| 29 | argc--; argv++; |
Simon Ser | d89e426 | 2017-12-30 14:43:31 -0600 | [diff] [blame] | 30 | if (argc <= 0) |
| 31 | usage_with_options(orc_usage, check_options); |
| 32 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 33 | if (!strncmp(argv[0], "gen", 3)) { |
Julien Thierry | 6545eb0 | 2020-08-25 13:47:39 +0100 | [diff] [blame] | 34 | struct objtool_file *file; |
Julien Thierry | d44becb | 2020-08-25 13:47:40 +0100 | [diff] [blame] | 35 | int ret; |
Julien Thierry | 6545eb0 | 2020-08-25 13:47:39 +0100 | [diff] [blame] | 36 | |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 37 | argc = parse_options(argc, argv, check_options, orc_usage, 0); |
| 38 | if (argc != 1) |
| 39 | usage_with_options(orc_usage, check_options); |
| 40 | |
| 41 | objname = argv[0]; |
| 42 | |
Julien Thierry | 6545eb0 | 2020-08-25 13:47:39 +0100 | [diff] [blame] | 43 | file = objtool_open_read(objname); |
| 44 | if (!file) |
| 45 | return 1; |
| 46 | |
Julien Thierry | d44becb | 2020-08-25 13:47:40 +0100 | [diff] [blame] | 47 | ret = check(file); |
| 48 | if (ret) |
| 49 | return ret; |
| 50 | |
| 51 | if (list_empty(&file->insn_list)) |
| 52 | return 0; |
| 53 | |
Josh Poimboeuf | 1d516bd | 2020-12-17 15:02:42 -0600 | [diff] [blame] | 54 | ret = orc_create(file); |
Julien Thierry | d44becb | 2020-08-25 13:47:40 +0100 | [diff] [blame] | 55 | if (ret) |
| 56 | return ret; |
| 57 | |
| 58 | if (!file->elf->changed) |
| 59 | return 0; |
| 60 | |
| 61 | return elf_write(file->elf); |
Josh Poimboeuf | 627fce1 | 2017-07-11 10:33:42 -0500 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | if (!strcmp(argv[0], "dump")) { |
| 65 | if (argc != 2) |
| 66 | usage_with_options(orc_usage, check_options); |
| 67 | |
| 68 | objname = argv[1]; |
| 69 | |
| 70 | return orc_dump(objname); |
| 71 | } |
| 72 | |
| 73 | usage_with_options(orc_usage, check_options); |
| 74 | |
| 75 | return 0; |
| 76 | } |