1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
4 */
5
6 #include <unistd.h>
7 #include "orc.h"
8 #include "warn.h"
9
reg_name(unsigned int reg)10 static const char *reg_name(unsigned int reg)
11 {
12 switch (reg) {
13 case ORC_REG_PREV_SP:
14 return "prevsp";
15 case ORC_REG_DX:
16 return "dx";
17 case ORC_REG_DI:
18 return "di";
19 case ORC_REG_BP:
20 return "bp";
21 case ORC_REG_SP:
22 return "sp";
23 case ORC_REG_R10:
24 return "r10";
25 case ORC_REG_R13:
26 return "r13";
27 case ORC_REG_BP_INDIRECT:
28 return "bp(ind)";
29 case ORC_REG_SP_INDIRECT:
30 return "sp(ind)";
31 default:
32 return "?";
33 }
34 }
35
orc_type_name(unsigned int type)36 static const char *orc_type_name(unsigned int type)
37 {
38 switch (type) {
39 case ORC_TYPE_CALL:
40 return "call";
41 case ORC_TYPE_REGS:
42 return "regs";
43 case ORC_TYPE_REGS_IRET:
44 return "iret";
45 default:
46 return "?";
47 }
48 }
49
print_reg(unsigned int reg,int offset)50 static void print_reg(unsigned int reg, int offset)
51 {
52 if (reg == ORC_REG_BP_INDIRECT)
53 printf("(bp%+d)", offset);
54 else if (reg == ORC_REG_SP_INDIRECT)
55 printf("(sp%+d)", offset);
56 else if (reg == ORC_REG_UNDEFINED)
57 printf("(und)");
58 else
59 printf("%s%+d", reg_name(reg), offset);
60 }
61
orc_dump(const char * _objname)62 int orc_dump(const char *_objname)
63 {
64 int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
65 struct orc_entry *orc = NULL;
66 char *name;
67 size_t nr_sections;
68 Elf64_Addr orc_ip_addr = 0;
69 size_t shstrtab_idx, strtab_idx = 0;
70 Elf *elf;
71 Elf_Scn *scn;
72 GElf_Shdr sh;
73 GElf_Rela rela;
74 GElf_Sym sym;
75 Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
76
77
78 objname = _objname;
79
80 elf_version(EV_CURRENT);
81
82 fd = open(objname, O_RDONLY);
83 if (fd == -1) {
84 perror("open");
85 return -1;
86 }
87
88 elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
89 if (!elf) {
90 WARN_ELF("elf_begin");
91 return -1;
92 }
93
94 if (elf_getshdrnum(elf, &nr_sections)) {
95 WARN_ELF("elf_getshdrnum");
96 return -1;
97 }
98
99 if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
100 WARN_ELF("elf_getshdrstrndx");
101 return -1;
102 }
103
104 for (i = 0; i < nr_sections; i++) {
105 scn = elf_getscn(elf, i);
106 if (!scn) {
107 WARN_ELF("elf_getscn");
108 return -1;
109 }
110
111 if (!gelf_getshdr(scn, &sh)) {
112 WARN_ELF("gelf_getshdr");
113 return -1;
114 }
115
116 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
117 if (!name) {
118 WARN_ELF("elf_strptr");
119 return -1;
120 }
121
122 data = elf_getdata(scn, NULL);
123 if (!data) {
124 WARN_ELF("elf_getdata");
125 return -1;
126 }
127
128 if (!strcmp(name, ".symtab")) {
129 symtab = data;
130 } else if (!strcmp(name, ".strtab")) {
131 strtab_idx = i;
132 } else if (!strcmp(name, ".orc_unwind")) {
133 orc = data->d_buf;
134 orc_size = sh.sh_size;
135 } else if (!strcmp(name, ".orc_unwind_ip")) {
136 orc_ip = data->d_buf;
137 orc_ip_addr = sh.sh_addr;
138 } else if (!strcmp(name, ".rela.orc_unwind_ip")) {
139 rela_orc_ip = data;
140 }
141 }
142
143 if (!symtab || !strtab_idx || !orc || !orc_ip)
144 return 0;
145
146 if (orc_size % sizeof(*orc) != 0) {
147 WARN("bad .orc_unwind section size");
148 return -1;
149 }
150
151 nr_entries = orc_size / sizeof(*orc);
152 for (i = 0; i < nr_entries; i++) {
153 if (rela_orc_ip) {
154 if (!gelf_getrela(rela_orc_ip, i, &rela)) {
155 WARN_ELF("gelf_getrela");
156 return -1;
157 }
158
159 if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
160 WARN_ELF("gelf_getsym");
161 return -1;
162 }
163
164 if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
165 scn = elf_getscn(elf, sym.st_shndx);
166 if (!scn) {
167 WARN_ELF("elf_getscn");
168 return -1;
169 }
170
171 if (!gelf_getshdr(scn, &sh)) {
172 WARN_ELF("gelf_getshdr");
173 return -1;
174 }
175
176 name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
177 if (!name) {
178 WARN_ELF("elf_strptr");
179 return -1;
180 }
181 } else {
182 name = elf_strptr(elf, strtab_idx, sym.st_name);
183 if (!name) {
184 WARN_ELF("elf_strptr");
185 return -1;
186 }
187 }
188
189 printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
190
191 } else {
192 printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
193 }
194
195
196 printf(" sp:");
197
198 print_reg(orc[i].sp_reg, orc[i].sp_offset);
199
200 printf(" bp:");
201
202 print_reg(orc[i].bp_reg, orc[i].bp_offset);
203
204 printf(" type:%s end:%d\n",
205 orc_type_name(orc[i].type), orc[i].end);
206 }
207
208 elf_end(elf);
209 close(fd);
210
211 return 0;
212 }
213