2 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
22 static const char *reg_name(unsigned int reg)
39 case ORC_REG_BP_INDIRECT:
41 case ORC_REG_SP_INDIRECT:
48 static const char *orc_type_name(unsigned int type)
55 case ORC_TYPE_REGS_IRET:
62 static void print_reg(unsigned int reg, int offset)
64 if (reg == ORC_REG_BP_INDIRECT)
65 printf("(bp%+d)", offset);
66 else if (reg == ORC_REG_SP_INDIRECT)
67 printf("(sp%+d)", offset);
68 else if (reg == ORC_REG_UNDEFINED)
71 printf("%s%+d", reg_name(reg), offset);
74 int orc_dump(const char *_objname)
76 int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
77 struct orc_entry *orc = NULL;
80 Elf64_Addr orc_ip_addr = 0;
81 size_t shstrtab_idx, strtab_idx = 0;
87 Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
92 elf_version(EV_CURRENT);
94 fd = open(objname, O_RDONLY);
100 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
102 WARN_ELF("elf_begin");
106 if (elf_getshdrnum(elf, &nr_sections)) {
107 WARN_ELF("elf_getshdrnum");
111 if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
112 WARN_ELF("elf_getshdrstrndx");
116 for (i = 0; i < nr_sections; i++) {
117 scn = elf_getscn(elf, i);
119 WARN_ELF("elf_getscn");
123 if (!gelf_getshdr(scn, &sh)) {
124 WARN_ELF("gelf_getshdr");
128 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
130 WARN_ELF("elf_strptr");
134 data = elf_getdata(scn, NULL);
136 WARN_ELF("elf_getdata");
140 if (!strcmp(name, ".symtab")) {
142 } else if (!strcmp(name, ".strtab")) {
144 } else if (!strcmp(name, ".orc_unwind")) {
146 orc_size = sh.sh_size;
147 } else if (!strcmp(name, ".orc_unwind_ip")) {
148 orc_ip = data->d_buf;
149 orc_ip_addr = sh.sh_addr;
150 } else if (!strcmp(name, ".rela.orc_unwind_ip")) {
155 if (!symtab || !strtab_idx || !orc || !orc_ip)
158 if (orc_size % sizeof(*orc) != 0) {
159 WARN("bad .orc_unwind section size");
163 nr_entries = orc_size / sizeof(*orc);
164 for (i = 0; i < nr_entries; i++) {
166 if (!gelf_getrela(rela_orc_ip, i, &rela)) {
167 WARN_ELF("gelf_getrela");
171 if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
172 WARN_ELF("gelf_getsym");
176 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
177 scn = elf_getscn(elf, sym.st_shndx);
179 WARN_ELF("elf_getscn");
183 if (!gelf_getshdr(scn, &sh)) {
184 WARN_ELF("gelf_getshdr");
188 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
190 WARN_ELF("elf_strptr");
194 name = elf_strptr(elf, strtab_idx, sym.st_name);
196 WARN_ELF("elf_strptr");
201 printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
204 printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
210 print_reg(orc[i].sp_reg, orc[i].sp_offset);
214 print_reg(orc[i].bp_reg, orc[i].bp_offset);
216 printf(" type:%s\n", orc_type_name(orc[i].type));