11ccea77eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 2627fce14SJosh Poimboeuf /* 3627fce14SJosh Poimboeuf * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com> 4627fce14SJosh Poimboeuf */ 5627fce14SJosh Poimboeuf 6627fce14SJosh Poimboeuf #include <unistd.h> 7*ee819aedSJulien Thierry #include <linux/objtool.h> 80decf1f8SMatt Helsley #include <asm/orc_types.h> 90decf1f8SMatt Helsley #include "objtool.h" 10627fce14SJosh Poimboeuf #include "warn.h" 11627fce14SJosh Poimboeuf 12627fce14SJosh Poimboeuf static const char *reg_name(unsigned int reg) 13627fce14SJosh Poimboeuf { 14627fce14SJosh Poimboeuf switch (reg) { 15627fce14SJosh Poimboeuf case ORC_REG_PREV_SP: 16627fce14SJosh Poimboeuf return "prevsp"; 17627fce14SJosh Poimboeuf case ORC_REG_DX: 18627fce14SJosh Poimboeuf return "dx"; 19627fce14SJosh Poimboeuf case ORC_REG_DI: 20627fce14SJosh Poimboeuf return "di"; 21627fce14SJosh Poimboeuf case ORC_REG_BP: 22627fce14SJosh Poimboeuf return "bp"; 23627fce14SJosh Poimboeuf case ORC_REG_SP: 24627fce14SJosh Poimboeuf return "sp"; 25627fce14SJosh Poimboeuf case ORC_REG_R10: 26627fce14SJosh Poimboeuf return "r10"; 27627fce14SJosh Poimboeuf case ORC_REG_R13: 28627fce14SJosh Poimboeuf return "r13"; 29627fce14SJosh Poimboeuf case ORC_REG_BP_INDIRECT: 30627fce14SJosh Poimboeuf return "bp(ind)"; 31627fce14SJosh Poimboeuf case ORC_REG_SP_INDIRECT: 32627fce14SJosh Poimboeuf return "sp(ind)"; 33627fce14SJosh Poimboeuf default: 34627fce14SJosh Poimboeuf return "?"; 35627fce14SJosh Poimboeuf } 36627fce14SJosh Poimboeuf } 37627fce14SJosh Poimboeuf 38627fce14SJosh Poimboeuf static const char *orc_type_name(unsigned int type) 39627fce14SJosh Poimboeuf { 40627fce14SJosh Poimboeuf switch (type) { 41*ee819aedSJulien Thierry case UNWIND_HINT_TYPE_CALL: 42627fce14SJosh Poimboeuf return "call"; 43*ee819aedSJulien Thierry case UNWIND_HINT_TYPE_REGS: 44627fce14SJosh Poimboeuf return "regs"; 45*ee819aedSJulien Thierry case UNWIND_HINT_TYPE_REGS_PARTIAL: 46*ee819aedSJulien Thierry return "regs (partial)"; 47627fce14SJosh Poimboeuf default: 48627fce14SJosh Poimboeuf return "?"; 49627fce14SJosh Poimboeuf } 50627fce14SJosh Poimboeuf } 51627fce14SJosh Poimboeuf 52627fce14SJosh Poimboeuf static void print_reg(unsigned int reg, int offset) 53627fce14SJosh Poimboeuf { 54627fce14SJosh Poimboeuf if (reg == ORC_REG_BP_INDIRECT) 55627fce14SJosh Poimboeuf printf("(bp%+d)", offset); 56627fce14SJosh Poimboeuf else if (reg == ORC_REG_SP_INDIRECT) 57627fce14SJosh Poimboeuf printf("(sp%+d)", offset); 58627fce14SJosh Poimboeuf else if (reg == ORC_REG_UNDEFINED) 59627fce14SJosh Poimboeuf printf("(und)"); 60627fce14SJosh Poimboeuf else 61627fce14SJosh Poimboeuf printf("%s%+d", reg_name(reg), offset); 62627fce14SJosh Poimboeuf } 63627fce14SJosh Poimboeuf 64627fce14SJosh Poimboeuf int orc_dump(const char *_objname) 65627fce14SJosh Poimboeuf { 66627fce14SJosh Poimboeuf int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0; 67627fce14SJosh Poimboeuf struct orc_entry *orc = NULL; 68627fce14SJosh Poimboeuf char *name; 6914c47b54SMikulas Patocka size_t nr_sections; 7014c47b54SMikulas Patocka Elf64_Addr orc_ip_addr = 0; 718782e7caSJosh Poimboeuf size_t shstrtab_idx, strtab_idx = 0; 72627fce14SJosh Poimboeuf Elf *elf; 73627fce14SJosh Poimboeuf Elf_Scn *scn; 74627fce14SJosh Poimboeuf GElf_Shdr sh; 75627fce14SJosh Poimboeuf GElf_Rela rela; 76627fce14SJosh Poimboeuf GElf_Sym sym; 77627fce14SJosh Poimboeuf Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL; 78627fce14SJosh Poimboeuf 79627fce14SJosh Poimboeuf 80627fce14SJosh Poimboeuf objname = _objname; 81627fce14SJosh Poimboeuf 82627fce14SJosh Poimboeuf elf_version(EV_CURRENT); 83627fce14SJosh Poimboeuf 84627fce14SJosh Poimboeuf fd = open(objname, O_RDONLY); 85627fce14SJosh Poimboeuf if (fd == -1) { 86627fce14SJosh Poimboeuf perror("open"); 87627fce14SJosh Poimboeuf return -1; 88627fce14SJosh Poimboeuf } 89627fce14SJosh Poimboeuf 90627fce14SJosh Poimboeuf elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); 91627fce14SJosh Poimboeuf if (!elf) { 92627fce14SJosh Poimboeuf WARN_ELF("elf_begin"); 93627fce14SJosh Poimboeuf return -1; 94627fce14SJosh Poimboeuf } 95627fce14SJosh Poimboeuf 96627fce14SJosh Poimboeuf if (elf_getshdrnum(elf, &nr_sections)) { 97627fce14SJosh Poimboeuf WARN_ELF("elf_getshdrnum"); 98627fce14SJosh Poimboeuf return -1; 99627fce14SJosh Poimboeuf } 100627fce14SJosh Poimboeuf 101627fce14SJosh Poimboeuf if (elf_getshdrstrndx(elf, &shstrtab_idx)) { 102627fce14SJosh Poimboeuf WARN_ELF("elf_getshdrstrndx"); 103627fce14SJosh Poimboeuf return -1; 104627fce14SJosh Poimboeuf } 105627fce14SJosh Poimboeuf 106627fce14SJosh Poimboeuf for (i = 0; i < nr_sections; i++) { 107627fce14SJosh Poimboeuf scn = elf_getscn(elf, i); 108627fce14SJosh Poimboeuf if (!scn) { 109627fce14SJosh Poimboeuf WARN_ELF("elf_getscn"); 110627fce14SJosh Poimboeuf return -1; 111627fce14SJosh Poimboeuf } 112627fce14SJosh Poimboeuf 113627fce14SJosh Poimboeuf if (!gelf_getshdr(scn, &sh)) { 114627fce14SJosh Poimboeuf WARN_ELF("gelf_getshdr"); 115627fce14SJosh Poimboeuf return -1; 116627fce14SJosh Poimboeuf } 117627fce14SJosh Poimboeuf 118627fce14SJosh Poimboeuf name = elf_strptr(elf, shstrtab_idx, sh.sh_name); 119627fce14SJosh Poimboeuf if (!name) { 120627fce14SJosh Poimboeuf WARN_ELF("elf_strptr"); 121627fce14SJosh Poimboeuf return -1; 122627fce14SJosh Poimboeuf } 123627fce14SJosh Poimboeuf 124627fce14SJosh Poimboeuf data = elf_getdata(scn, NULL); 125627fce14SJosh Poimboeuf if (!data) { 126627fce14SJosh Poimboeuf WARN_ELF("elf_getdata"); 127627fce14SJosh Poimboeuf return -1; 128627fce14SJosh Poimboeuf } 129627fce14SJosh Poimboeuf 130627fce14SJosh Poimboeuf if (!strcmp(name, ".symtab")) { 131627fce14SJosh Poimboeuf symtab = data; 1328782e7caSJosh Poimboeuf } else if (!strcmp(name, ".strtab")) { 1338782e7caSJosh Poimboeuf strtab_idx = i; 134627fce14SJosh Poimboeuf } else if (!strcmp(name, ".orc_unwind")) { 135627fce14SJosh Poimboeuf orc = data->d_buf; 136627fce14SJosh Poimboeuf orc_size = sh.sh_size; 137627fce14SJosh Poimboeuf } else if (!strcmp(name, ".orc_unwind_ip")) { 138627fce14SJosh Poimboeuf orc_ip = data->d_buf; 139627fce14SJosh Poimboeuf orc_ip_addr = sh.sh_addr; 140627fce14SJosh Poimboeuf } else if (!strcmp(name, ".rela.orc_unwind_ip")) { 141627fce14SJosh Poimboeuf rela_orc_ip = data; 142627fce14SJosh Poimboeuf } 143627fce14SJosh Poimboeuf } 144627fce14SJosh Poimboeuf 1458782e7caSJosh Poimboeuf if (!symtab || !strtab_idx || !orc || !orc_ip) 146627fce14SJosh Poimboeuf return 0; 147627fce14SJosh Poimboeuf 148627fce14SJosh Poimboeuf if (orc_size % sizeof(*orc) != 0) { 149627fce14SJosh Poimboeuf WARN("bad .orc_unwind section size"); 150627fce14SJosh Poimboeuf return -1; 151627fce14SJosh Poimboeuf } 152627fce14SJosh Poimboeuf 153627fce14SJosh Poimboeuf nr_entries = orc_size / sizeof(*orc); 154627fce14SJosh Poimboeuf for (i = 0; i < nr_entries; i++) { 155627fce14SJosh Poimboeuf if (rela_orc_ip) { 156627fce14SJosh Poimboeuf if (!gelf_getrela(rela_orc_ip, i, &rela)) { 157627fce14SJosh Poimboeuf WARN_ELF("gelf_getrela"); 158627fce14SJosh Poimboeuf return -1; 159627fce14SJosh Poimboeuf } 160627fce14SJosh Poimboeuf 161627fce14SJosh Poimboeuf if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) { 162627fce14SJosh Poimboeuf WARN_ELF("gelf_getsym"); 163627fce14SJosh Poimboeuf return -1; 164627fce14SJosh Poimboeuf } 165627fce14SJosh Poimboeuf 1668782e7caSJosh Poimboeuf if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) { 167627fce14SJosh Poimboeuf scn = elf_getscn(elf, sym.st_shndx); 168627fce14SJosh Poimboeuf if (!scn) { 169627fce14SJosh Poimboeuf WARN_ELF("elf_getscn"); 170627fce14SJosh Poimboeuf return -1; 171627fce14SJosh Poimboeuf } 172627fce14SJosh Poimboeuf 173627fce14SJosh Poimboeuf if (!gelf_getshdr(scn, &sh)) { 174627fce14SJosh Poimboeuf WARN_ELF("gelf_getshdr"); 175627fce14SJosh Poimboeuf return -1; 176627fce14SJosh Poimboeuf } 177627fce14SJosh Poimboeuf 178627fce14SJosh Poimboeuf name = elf_strptr(elf, shstrtab_idx, sh.sh_name); 1798782e7caSJosh Poimboeuf if (!name) { 180627fce14SJosh Poimboeuf WARN_ELF("elf_strptr"); 181627fce14SJosh Poimboeuf return -1; 182627fce14SJosh Poimboeuf } 1838782e7caSJosh Poimboeuf } else { 1848782e7caSJosh Poimboeuf name = elf_strptr(elf, strtab_idx, sym.st_name); 1858782e7caSJosh Poimboeuf if (!name) { 1868782e7caSJosh Poimboeuf WARN_ELF("elf_strptr"); 1878782e7caSJosh Poimboeuf return -1; 1888782e7caSJosh Poimboeuf } 1898782e7caSJosh Poimboeuf } 190627fce14SJosh Poimboeuf 19114c47b54SMikulas Patocka printf("%s+%llx:", name, (unsigned long long)rela.r_addend); 192627fce14SJosh Poimboeuf 193627fce14SJosh Poimboeuf } else { 19414c47b54SMikulas Patocka printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i])); 195627fce14SJosh Poimboeuf } 196627fce14SJosh Poimboeuf 197627fce14SJosh Poimboeuf 198627fce14SJosh Poimboeuf printf(" sp:"); 199627fce14SJosh Poimboeuf 200627fce14SJosh Poimboeuf print_reg(orc[i].sp_reg, orc[i].sp_offset); 201627fce14SJosh Poimboeuf 202627fce14SJosh Poimboeuf printf(" bp:"); 203627fce14SJosh Poimboeuf 204627fce14SJosh Poimboeuf print_reg(orc[i].bp_reg, orc[i].bp_offset); 205627fce14SJosh Poimboeuf 206d31a5802SJosh Poimboeuf printf(" type:%s end:%d\n", 207d31a5802SJosh Poimboeuf orc_type_name(orc[i].type), orc[i].end); 208627fce14SJosh Poimboeuf } 209627fce14SJosh Poimboeuf 210627fce14SJosh Poimboeuf elf_end(elf); 211627fce14SJosh Poimboeuf close(fd); 212627fce14SJosh Poimboeuf 213627fce14SJosh Poimboeuf return 0; 214627fce14SJosh Poimboeuf } 215