1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
7 #include <asm/orc_types.h>
8 #include <objtool/objtool.h>
9 #include <objtool/warn.h>
10 #include <objtool/endianness.h>
12 static const char *reg_name(unsigned int reg)
29 case ORC_REG_BP_INDIRECT:
31 case ORC_REG_SP_INDIRECT:
38 static const char *orc_type_name(unsigned int type)
41 case ORC_TYPE_UNDEFINED:
43 case ORC_TYPE_END_OF_STACK:
49 case ORC_TYPE_REGS_PARTIAL:
50 return "regs (partial)";
56 static void print_reg(unsigned int reg, int offset)
58 if (reg == ORC_REG_BP_INDIRECT)
59 printf("(bp%+d)", offset);
60 else if (reg == ORC_REG_SP_INDIRECT)
61 printf("(sp)%+d", offset);
62 else if (reg == ORC_REG_UNDEFINED)
65 printf("%s%+d", reg_name(reg), offset);
68 int orc_dump(const char *_objname)
70 int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
71 struct orc_entry *orc = NULL;
74 Elf64_Addr orc_ip_addr = 0;
75 size_t shstrtab_idx, strtab_idx = 0;
81 Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
82 struct elf dummy_elf = {};
87 elf_version(EV_CURRENT);
89 fd = open(objname, O_RDONLY);
95 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
97 WARN_ELF("elf_begin");
101 if (!elf64_getehdr(elf)) {
102 WARN_ELF("elf64_getehdr");
105 memcpy(&dummy_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
107 if (elf_getshdrnum(elf, &nr_sections)) {
108 WARN_ELF("elf_getshdrnum");
112 if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
113 WARN_ELF("elf_getshdrstrndx");
117 for (i = 0; i < nr_sections; i++) {
118 scn = elf_getscn(elf, i);
120 WARN_ELF("elf_getscn");
124 if (!gelf_getshdr(scn, &sh)) {
125 WARN_ELF("gelf_getshdr");
129 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
131 WARN_ELF("elf_strptr");
135 data = elf_getdata(scn, NULL);
137 WARN_ELF("elf_getdata");
141 if (!strcmp(name, ".symtab")) {
143 } else if (!strcmp(name, ".strtab")) {
145 } else if (!strcmp(name, ".orc_unwind")) {
147 orc_size = sh.sh_size;
148 } else if (!strcmp(name, ".orc_unwind_ip")) {
149 orc_ip = data->d_buf;
150 orc_ip_addr = sh.sh_addr;
151 } else if (!strcmp(name, ".rela.orc_unwind_ip")) {
156 if (!symtab || !strtab_idx || !orc || !orc_ip)
159 if (orc_size % sizeof(*orc) != 0) {
160 WARN("bad .orc_unwind section size");
164 nr_entries = orc_size / sizeof(*orc);
165 for (i = 0; i < nr_entries; i++) {
167 if (!gelf_getrela(rela_orc_ip, i, &rela)) {
168 WARN_ELF("gelf_getrela");
172 if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
173 WARN_ELF("gelf_getsym");
177 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
178 scn = elf_getscn(elf, sym.st_shndx);
180 WARN_ELF("elf_getscn");
184 if (!gelf_getshdr(scn, &sh)) {
185 WARN_ELF("gelf_getshdr");
189 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
191 WARN_ELF("elf_strptr");
195 name = elf_strptr(elf, strtab_idx, sym.st_name);
197 WARN_ELF("elf_strptr");
202 printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
205 printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
208 printf("type:%s", orc_type_name(orc[i].type));
212 print_reg(orc[i].sp_reg, bswap_if_needed(&dummy_elf, orc[i].sp_offset));
216 print_reg(orc[i].bp_reg, bswap_if_needed(&dummy_elf, orc[i].bp_offset));
218 printf(" signal:%d\n", orc[i].signal);