11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2442f04c3SJosh Poimboeuf /* 3442f04c3SJosh Poimboeuf * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com> 4442f04c3SJosh Poimboeuf */ 5442f04c3SJosh Poimboeuf 6442f04c3SJosh Poimboeuf /* 7442f04c3SJosh Poimboeuf * objtool: 8442f04c3SJosh Poimboeuf * 9442f04c3SJosh Poimboeuf * The 'check' subcmd analyzes every .o file and ensures the validity of its 10442f04c3SJosh Poimboeuf * stack trace metadata. It enforces a set of rules on asm code and C inline 11442f04c3SJosh Poimboeuf * assembly code so that stack traces can be reliable. 12442f04c3SJosh Poimboeuf * 13442f04c3SJosh Poimboeuf * For more information, see tools/objtool/Documentation/stack-validation.txt. 14442f04c3SJosh Poimboeuf */ 15442f04c3SJosh Poimboeuf 16442f04c3SJosh Poimboeuf #include <stdio.h> 17442f04c3SJosh Poimboeuf #include <stdbool.h> 18442f04c3SJosh Poimboeuf #include <string.h> 19442f04c3SJosh Poimboeuf #include <stdlib.h> 208ad15c69SPeter Zijlstra #include <unistd.h> 21442f04c3SJosh Poimboeuf #include <subcmd/exec-cmd.h> 22442f04c3SJosh Poimboeuf #include <subcmd/pager.h> 2300614597SArnaldo Carvalho de Melo #include <linux/kernel.h> 24442f04c3SJosh Poimboeuf 257786032eSVasily Gorbik #include <objtool/builtin.h> 267786032eSVasily Gorbik #include <objtool/objtool.h> 277786032eSVasily Gorbik #include <objtool/warn.h> 28442f04c3SJosh Poimboeuf 29442f04c3SJosh Poimboeuf struct cmd_struct { 30442f04c3SJosh Poimboeuf const char *name; 31442f04c3SJosh Poimboeuf int (*fn)(int, const char **); 32442f04c3SJosh Poimboeuf const char *help; 33442f04c3SJosh Poimboeuf }; 34442f04c3SJosh Poimboeuf 35442f04c3SJosh Poimboeuf static const char objtool_usage_string[] = 36627fce14SJosh Poimboeuf "objtool COMMAND [ARGS]"; 37442f04c3SJosh Poimboeuf 38442f04c3SJosh Poimboeuf static struct cmd_struct objtool_cmds[] = { 39442f04c3SJosh Poimboeuf {"check", cmd_check, "Perform stack metadata validation on an object file" }, 40627fce14SJosh Poimboeuf {"orc", cmd_orc, "Generate in-place ORC unwind tables for an object file" }, 41442f04c3SJosh Poimboeuf }; 42442f04c3SJosh Poimboeuf 43442f04c3SJosh Poimboeuf bool help; 44442f04c3SJosh Poimboeuf 456545eb03SJulien Thierry const char *objname; 466545eb03SJulien Thierry static struct objtool_file file; 476545eb03SJulien Thierry 488ad15c69SPeter Zijlstra static bool objtool_create_backup(const char *_objname) 498ad15c69SPeter Zijlstra { 508ad15c69SPeter Zijlstra int len = strlen(_objname); 518ad15c69SPeter Zijlstra char *buf, *base, *name = malloc(len+6); 528ad15c69SPeter Zijlstra int s, d, l, t; 538ad15c69SPeter Zijlstra 548ad15c69SPeter Zijlstra if (!name) { 558ad15c69SPeter Zijlstra perror("failed backup name malloc"); 568ad15c69SPeter Zijlstra return false; 578ad15c69SPeter Zijlstra } 588ad15c69SPeter Zijlstra 598ad15c69SPeter Zijlstra strcpy(name, _objname); 608ad15c69SPeter Zijlstra strcpy(name + len, ".orig"); 618ad15c69SPeter Zijlstra 628ad15c69SPeter Zijlstra d = open(name, O_CREAT|O_WRONLY|O_TRUNC, 0644); 638ad15c69SPeter Zijlstra if (d < 0) { 648ad15c69SPeter Zijlstra perror("failed to create backup file"); 658ad15c69SPeter Zijlstra return false; 668ad15c69SPeter Zijlstra } 678ad15c69SPeter Zijlstra 688ad15c69SPeter Zijlstra s = open(_objname, O_RDONLY); 698ad15c69SPeter Zijlstra if (s < 0) { 708ad15c69SPeter Zijlstra perror("failed to open orig file"); 718ad15c69SPeter Zijlstra return false; 728ad15c69SPeter Zijlstra } 738ad15c69SPeter Zijlstra 748ad15c69SPeter Zijlstra buf = malloc(4096); 758ad15c69SPeter Zijlstra if (!buf) { 768ad15c69SPeter Zijlstra perror("failed backup data malloc"); 778ad15c69SPeter Zijlstra return false; 788ad15c69SPeter Zijlstra } 798ad15c69SPeter Zijlstra 808ad15c69SPeter Zijlstra while ((l = read(s, buf, 4096)) > 0) { 818ad15c69SPeter Zijlstra base = buf; 828ad15c69SPeter Zijlstra do { 838ad15c69SPeter Zijlstra t = write(d, base, l); 848ad15c69SPeter Zijlstra if (t < 0) { 858ad15c69SPeter Zijlstra perror("failed backup write"); 868ad15c69SPeter Zijlstra return false; 878ad15c69SPeter Zijlstra } 888ad15c69SPeter Zijlstra base += t; 898ad15c69SPeter Zijlstra l -= t; 908ad15c69SPeter Zijlstra } while (l); 918ad15c69SPeter Zijlstra } 928ad15c69SPeter Zijlstra 938ad15c69SPeter Zijlstra if (l < 0) { 948ad15c69SPeter Zijlstra perror("failed backup read"); 958ad15c69SPeter Zijlstra return false; 968ad15c69SPeter Zijlstra } 978ad15c69SPeter Zijlstra 988ad15c69SPeter Zijlstra free(name); 998ad15c69SPeter Zijlstra free(buf); 1008ad15c69SPeter Zijlstra close(d); 1018ad15c69SPeter Zijlstra close(s); 1028ad15c69SPeter Zijlstra 1038ad15c69SPeter Zijlstra return true; 1048ad15c69SPeter Zijlstra } 1058ad15c69SPeter Zijlstra 1066545eb03SJulien Thierry struct objtool_file *objtool_open_read(const char *_objname) 1076545eb03SJulien Thierry { 1086545eb03SJulien Thierry if (objname) { 1096545eb03SJulien Thierry if (strcmp(objname, _objname)) { 1106545eb03SJulien Thierry WARN("won't handle more than one file at a time"); 1116545eb03SJulien Thierry return NULL; 1126545eb03SJulien Thierry } 1136545eb03SJulien Thierry return &file; 1146545eb03SJulien Thierry } 1156545eb03SJulien Thierry objname = _objname; 1166545eb03SJulien Thierry 1176545eb03SJulien Thierry file.elf = elf_open_read(objname, O_RDWR); 1186545eb03SJulien Thierry if (!file.elf) 1196545eb03SJulien Thierry return NULL; 1206545eb03SJulien Thierry 1218ad15c69SPeter Zijlstra if (backup && !objtool_create_backup(objname)) { 1228ad15c69SPeter Zijlstra WARN("can't create backup file"); 1238ad15c69SPeter Zijlstra return NULL; 1248ad15c69SPeter Zijlstra } 1258ad15c69SPeter Zijlstra 1266545eb03SJulien Thierry INIT_LIST_HEAD(&file.insn_list); 1276545eb03SJulien Thierry hash_init(file.insn_hash); 12843d5430aSPeter Zijlstra INIT_LIST_HEAD(&file.retpoline_call_list); 1296545eb03SJulien Thierry INIT_LIST_HEAD(&file.static_call_list); 13099d00215SPeter Zijlstra INIT_LIST_HEAD(&file.mcount_loc_list); 1316545eb03SJulien Thierry file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment"); 1326545eb03SJulien Thierry file.ignore_unreachables = no_unreachable; 1336545eb03SJulien Thierry file.hints = false; 1346545eb03SJulien Thierry 1356545eb03SJulien Thierry return &file; 1366545eb03SJulien Thierry } 1376545eb03SJulien Thierry 138*db2b0c5dSPeter Zijlstra void objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func) 139*db2b0c5dSPeter Zijlstra { 140*db2b0c5dSPeter Zijlstra if (!noinstr) 141*db2b0c5dSPeter Zijlstra return; 142*db2b0c5dSPeter Zijlstra 143*db2b0c5dSPeter Zijlstra if (!f->pv_ops) { 144*db2b0c5dSPeter Zijlstra WARN("paravirt confusion"); 145*db2b0c5dSPeter Zijlstra return; 146*db2b0c5dSPeter Zijlstra } 147*db2b0c5dSPeter Zijlstra 148*db2b0c5dSPeter Zijlstra /* 149*db2b0c5dSPeter Zijlstra * These functions will be patched into native code, 150*db2b0c5dSPeter Zijlstra * see paravirt_patch(). 151*db2b0c5dSPeter Zijlstra */ 152*db2b0c5dSPeter Zijlstra if (!strcmp(func->name, "_paravirt_nop") || 153*db2b0c5dSPeter Zijlstra !strcmp(func->name, "_paravirt_ident_64")) 154*db2b0c5dSPeter Zijlstra return; 155*db2b0c5dSPeter Zijlstra 156*db2b0c5dSPeter Zijlstra list_add(&func->pv_target, &f->pv_ops[idx].targets); 157*db2b0c5dSPeter Zijlstra f->pv_ops[idx].clean = false; 158*db2b0c5dSPeter Zijlstra } 159*db2b0c5dSPeter Zijlstra 160442f04c3SJosh Poimboeuf static void cmd_usage(void) 161442f04c3SJosh Poimboeuf { 162442f04c3SJosh Poimboeuf unsigned int i, longest = 0; 163442f04c3SJosh Poimboeuf 164442f04c3SJosh Poimboeuf printf("\n usage: %s\n\n", objtool_usage_string); 165442f04c3SJosh Poimboeuf 166442f04c3SJosh Poimboeuf for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) { 167442f04c3SJosh Poimboeuf if (longest < strlen(objtool_cmds[i].name)) 168442f04c3SJosh Poimboeuf longest = strlen(objtool_cmds[i].name); 169442f04c3SJosh Poimboeuf } 170442f04c3SJosh Poimboeuf 171442f04c3SJosh Poimboeuf puts(" Commands:"); 172442f04c3SJosh Poimboeuf for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) { 173442f04c3SJosh Poimboeuf printf(" %-*s ", longest, objtool_cmds[i].name); 174442f04c3SJosh Poimboeuf puts(objtool_cmds[i].help); 175442f04c3SJosh Poimboeuf } 176442f04c3SJosh Poimboeuf 177442f04c3SJosh Poimboeuf printf("\n"); 178442f04c3SJosh Poimboeuf 179f15c648fSMatt Helsley if (!help) 1806a93bb7eSKamalesh Babulal exit(129); 181f15c648fSMatt Helsley exit(0); 182442f04c3SJosh Poimboeuf } 183442f04c3SJosh Poimboeuf 184442f04c3SJosh Poimboeuf static void handle_options(int *argc, const char ***argv) 185442f04c3SJosh Poimboeuf { 186442f04c3SJosh Poimboeuf while (*argc > 0) { 187442f04c3SJosh Poimboeuf const char *cmd = (*argv)[0]; 188442f04c3SJosh Poimboeuf 189442f04c3SJosh Poimboeuf if (cmd[0] != '-') 190442f04c3SJosh Poimboeuf break; 191442f04c3SJosh Poimboeuf 192442f04c3SJosh Poimboeuf if (!strcmp(cmd, "--help") || !strcmp(cmd, "-h")) { 193442f04c3SJosh Poimboeuf help = true; 194442f04c3SJosh Poimboeuf break; 195442f04c3SJosh Poimboeuf } else { 196442f04c3SJosh Poimboeuf fprintf(stderr, "Unknown option: %s\n", cmd); 1976a93bb7eSKamalesh Babulal cmd_usage(); 198442f04c3SJosh Poimboeuf } 199442f04c3SJosh Poimboeuf 200442f04c3SJosh Poimboeuf (*argv)++; 201442f04c3SJosh Poimboeuf (*argc)--; 202442f04c3SJosh Poimboeuf } 203442f04c3SJosh Poimboeuf } 204442f04c3SJosh Poimboeuf 205442f04c3SJosh Poimboeuf static void handle_internal_command(int argc, const char **argv) 206442f04c3SJosh Poimboeuf { 207442f04c3SJosh Poimboeuf const char *cmd = argv[0]; 208442f04c3SJosh Poimboeuf unsigned int i, ret; 209442f04c3SJosh Poimboeuf 210442f04c3SJosh Poimboeuf for (i = 0; i < ARRAY_SIZE(objtool_cmds); i++) { 211442f04c3SJosh Poimboeuf struct cmd_struct *p = objtool_cmds+i; 212442f04c3SJosh Poimboeuf 213442f04c3SJosh Poimboeuf if (strcmp(p->name, cmd)) 214442f04c3SJosh Poimboeuf continue; 215442f04c3SJosh Poimboeuf 216442f04c3SJosh Poimboeuf ret = p->fn(argc, argv); 217442f04c3SJosh Poimboeuf 218442f04c3SJosh Poimboeuf exit(ret); 219442f04c3SJosh Poimboeuf } 220442f04c3SJosh Poimboeuf 221442f04c3SJosh Poimboeuf cmd_usage(); 222442f04c3SJosh Poimboeuf } 223442f04c3SJosh Poimboeuf 224442f04c3SJosh Poimboeuf int main(int argc, const char **argv) 225442f04c3SJosh Poimboeuf { 226442f04c3SJosh Poimboeuf static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED"; 227442f04c3SJosh Poimboeuf 228442f04c3SJosh Poimboeuf /* libsubcmd init */ 229442f04c3SJosh Poimboeuf exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED); 230442f04c3SJosh Poimboeuf pager_init(UNUSED); 231442f04c3SJosh Poimboeuf 232442f04c3SJosh Poimboeuf argv++; 233442f04c3SJosh Poimboeuf argc--; 234442f04c3SJosh Poimboeuf handle_options(&argc, &argv); 235442f04c3SJosh Poimboeuf 236442f04c3SJosh Poimboeuf if (!argc || help) 237442f04c3SJosh Poimboeuf cmd_usage(); 238442f04c3SJosh Poimboeuf 239442f04c3SJosh Poimboeuf handle_internal_command(argc, argv); 240442f04c3SJosh Poimboeuf 241442f04c3SJosh Poimboeuf return 0; 242442f04c3SJosh Poimboeuf } 243