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