^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * objtool:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * The 'check' subcmd analyzes every .o file and ensures the validity of its
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * stack trace metadata. It enforces a set of rules on asm code and C inline
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * assembly code so that stack traces can be reliable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * For more information, see tools/objtool/Documentation/stack-validation.txt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <stdbool.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <subcmd/exec-cmd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <subcmd/pager.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include "builtin.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include "objtool.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include "warn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) struct cmd_struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) int (*fn)(int, const char **);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) const char *help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static const char objtool_usage_string[] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) "objtool COMMAND [ARGS]";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct cmd_struct objtool_cmds[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) {"check", cmd_check, "Perform stack metadata validation on an object file" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bool help;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) const char *objname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) static struct objtool_file file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct objtool_file *objtool_open_read(const char *_objname)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (objname) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) if (strcmp(objname, _objname)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) WARN("won't handle more than one file at a time");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return &file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) objname = _objname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) file.elf = elf_open_read(objname, O_RDWR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (!file.elf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) INIT_LIST_HEAD(&file.insn_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) hash_init(file.insn_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) INIT_LIST_HEAD(&file.static_call_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) INIT_LIST_HEAD(&file.mcount_loc_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) file.ignore_unreachables = no_unreachable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) file.hints = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) return &file;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) static void cmd_usage(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned int i, longest = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) printf("\n usage: %s\n\n", objtool_usage_string);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (longest < strlen(objtool_cmds[i].name))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) longest = strlen(objtool_cmds[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) puts(" Commands:");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) printf(" %-*s ", longest, objtool_cmds[i].name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) puts(objtool_cmds[i].help);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) printf("\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (!help)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) exit(129);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) exit(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void handle_options(int *argc, const char ***argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) while (*argc > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) const char *cmd = (*argv)[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (cmd[0] != '-')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) help = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) fprintf(stderr, "Unknown option: %s\n", cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) cmd_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) (*argv)++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) (*argc)--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) static void handle_internal_command(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) const char *cmd = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) unsigned int i, ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct cmd_struct *p = objtool_cmds+i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) if (strcmp(p->name, cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) ret = p->fn(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) exit(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) cmd_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) int main(int argc, const char **argv)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* libsubcmd init */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) pager_init(UNUSED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) argv++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) argc--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) handle_options(&argc, &argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!argc || help)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) cmd_usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) handle_internal_command(argc, argv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }