xref: /openbmc/linux/tools/objtool/orc_dump.c (revision 0646c28b417b7fe307c9da72ca1c508e43b57dc0)
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>
7ee819aedSJulien Thierry #include <linux/objtool.h>
80decf1f8SMatt Helsley #include <asm/orc_types.h>
97786032eSVasily Gorbik #include <objtool/objtool.h>
107786032eSVasily Gorbik #include <objtool/warn.h>
117786032eSVasily Gorbik #include <objtool/endianness.h>
12627fce14SJosh Poimboeuf 
13627fce14SJosh Poimboeuf static const char *reg_name(unsigned int reg)
14627fce14SJosh Poimboeuf {
15627fce14SJosh Poimboeuf 	switch (reg) {
16627fce14SJosh Poimboeuf 	case ORC_REG_PREV_SP:
17627fce14SJosh Poimboeuf 		return "prevsp";
18627fce14SJosh Poimboeuf 	case ORC_REG_DX:
19627fce14SJosh Poimboeuf 		return "dx";
20627fce14SJosh Poimboeuf 	case ORC_REG_DI:
21627fce14SJosh Poimboeuf 		return "di";
22627fce14SJosh Poimboeuf 	case ORC_REG_BP:
23627fce14SJosh Poimboeuf 		return "bp";
24627fce14SJosh Poimboeuf 	case ORC_REG_SP:
25627fce14SJosh Poimboeuf 		return "sp";
26627fce14SJosh Poimboeuf 	case ORC_REG_R10:
27627fce14SJosh Poimboeuf 		return "r10";
28627fce14SJosh Poimboeuf 	case ORC_REG_R13:
29627fce14SJosh Poimboeuf 		return "r13";
30627fce14SJosh Poimboeuf 	case ORC_REG_BP_INDIRECT:
31627fce14SJosh Poimboeuf 		return "bp(ind)";
32627fce14SJosh Poimboeuf 	case ORC_REG_SP_INDIRECT:
33627fce14SJosh Poimboeuf 		return "sp(ind)";
34627fce14SJosh Poimboeuf 	default:
35627fce14SJosh Poimboeuf 		return "?";
36627fce14SJosh Poimboeuf 	}
37627fce14SJosh Poimboeuf }
38627fce14SJosh Poimboeuf 
39627fce14SJosh Poimboeuf static const char *orc_type_name(unsigned int type)
40627fce14SJosh Poimboeuf {
41627fce14SJosh Poimboeuf 	switch (type) {
42ee819aedSJulien Thierry 	case UNWIND_HINT_TYPE_CALL:
43627fce14SJosh Poimboeuf 		return "call";
44ee819aedSJulien Thierry 	case UNWIND_HINT_TYPE_REGS:
45627fce14SJosh Poimboeuf 		return "regs";
46ee819aedSJulien Thierry 	case UNWIND_HINT_TYPE_REGS_PARTIAL:
47ee819aedSJulien Thierry 		return "regs (partial)";
48627fce14SJosh Poimboeuf 	default:
49627fce14SJosh Poimboeuf 		return "?";
50627fce14SJosh Poimboeuf 	}
51627fce14SJosh Poimboeuf }
52627fce14SJosh Poimboeuf 
53627fce14SJosh Poimboeuf static void print_reg(unsigned int reg, int offset)
54627fce14SJosh Poimboeuf {
55627fce14SJosh Poimboeuf 	if (reg == ORC_REG_BP_INDIRECT)
56627fce14SJosh Poimboeuf 		printf("(bp%+d)", offset);
57627fce14SJosh Poimboeuf 	else if (reg == ORC_REG_SP_INDIRECT)
5887ccc826SPeter Zijlstra 		printf("(sp)%+d", offset);
59627fce14SJosh Poimboeuf 	else if (reg == ORC_REG_UNDEFINED)
60627fce14SJosh Poimboeuf 		printf("(und)");
61627fce14SJosh Poimboeuf 	else
62627fce14SJosh Poimboeuf 		printf("%s%+d", reg_name(reg), offset);
63627fce14SJosh Poimboeuf }
64627fce14SJosh Poimboeuf 
65627fce14SJosh Poimboeuf int orc_dump(const char *_objname)
66627fce14SJosh Poimboeuf {
67627fce14SJosh Poimboeuf 	int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
68627fce14SJosh Poimboeuf 	struct orc_entry *orc = NULL;
69627fce14SJosh Poimboeuf 	char *name;
7014c47b54SMikulas Patocka 	size_t nr_sections;
7114c47b54SMikulas Patocka 	Elf64_Addr orc_ip_addr = 0;
728782e7caSJosh Poimboeuf 	size_t shstrtab_idx, strtab_idx = 0;
73627fce14SJosh Poimboeuf 	Elf *elf;
74627fce14SJosh Poimboeuf 	Elf_Scn *scn;
75627fce14SJosh Poimboeuf 	GElf_Shdr sh;
76627fce14SJosh Poimboeuf 	GElf_Rela rela;
77627fce14SJosh Poimboeuf 	GElf_Sym sym;
78627fce14SJosh Poimboeuf 	Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
79*0646c28bSChristophe Leroy 	struct elf dummy_elf = {};
80627fce14SJosh Poimboeuf 
81627fce14SJosh Poimboeuf 
82627fce14SJosh Poimboeuf 	objname = _objname;
83627fce14SJosh Poimboeuf 
84627fce14SJosh Poimboeuf 	elf_version(EV_CURRENT);
85627fce14SJosh Poimboeuf 
86627fce14SJosh Poimboeuf 	fd = open(objname, O_RDONLY);
87627fce14SJosh Poimboeuf 	if (fd == -1) {
88627fce14SJosh Poimboeuf 		perror("open");
89627fce14SJosh Poimboeuf 		return -1;
90627fce14SJosh Poimboeuf 	}
91627fce14SJosh Poimboeuf 
92627fce14SJosh Poimboeuf 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
93627fce14SJosh Poimboeuf 	if (!elf) {
94627fce14SJosh Poimboeuf 		WARN_ELF("elf_begin");
95627fce14SJosh Poimboeuf 		return -1;
96627fce14SJosh Poimboeuf 	}
97627fce14SJosh Poimboeuf 
98*0646c28bSChristophe Leroy 	if (!elf64_getehdr(elf)) {
99*0646c28bSChristophe Leroy 		WARN_ELF("elf64_getehdr");
100*0646c28bSChristophe Leroy 		return -1;
101*0646c28bSChristophe Leroy 	}
102*0646c28bSChristophe Leroy 	memcpy(&dummy_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
103*0646c28bSChristophe Leroy 
104627fce14SJosh Poimboeuf 	if (elf_getshdrnum(elf, &nr_sections)) {
105627fce14SJosh Poimboeuf 		WARN_ELF("elf_getshdrnum");
106627fce14SJosh Poimboeuf 		return -1;
107627fce14SJosh Poimboeuf 	}
108627fce14SJosh Poimboeuf 
109627fce14SJosh Poimboeuf 	if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
110627fce14SJosh Poimboeuf 		WARN_ELF("elf_getshdrstrndx");
111627fce14SJosh Poimboeuf 		return -1;
112627fce14SJosh Poimboeuf 	}
113627fce14SJosh Poimboeuf 
114627fce14SJosh Poimboeuf 	for (i = 0; i < nr_sections; i++) {
115627fce14SJosh Poimboeuf 		scn = elf_getscn(elf, i);
116627fce14SJosh Poimboeuf 		if (!scn) {
117627fce14SJosh Poimboeuf 			WARN_ELF("elf_getscn");
118627fce14SJosh Poimboeuf 			return -1;
119627fce14SJosh Poimboeuf 		}
120627fce14SJosh Poimboeuf 
121627fce14SJosh Poimboeuf 		if (!gelf_getshdr(scn, &sh)) {
122627fce14SJosh Poimboeuf 			WARN_ELF("gelf_getshdr");
123627fce14SJosh Poimboeuf 			return -1;
124627fce14SJosh Poimboeuf 		}
125627fce14SJosh Poimboeuf 
126627fce14SJosh Poimboeuf 		name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
127627fce14SJosh Poimboeuf 		if (!name) {
128627fce14SJosh Poimboeuf 			WARN_ELF("elf_strptr");
129627fce14SJosh Poimboeuf 			return -1;
130627fce14SJosh Poimboeuf 		}
131627fce14SJosh Poimboeuf 
132627fce14SJosh Poimboeuf 		data = elf_getdata(scn, NULL);
133627fce14SJosh Poimboeuf 		if (!data) {
134627fce14SJosh Poimboeuf 			WARN_ELF("elf_getdata");
135627fce14SJosh Poimboeuf 			return -1;
136627fce14SJosh Poimboeuf 		}
137627fce14SJosh Poimboeuf 
138627fce14SJosh Poimboeuf 		if (!strcmp(name, ".symtab")) {
139627fce14SJosh Poimboeuf 			symtab = data;
1408782e7caSJosh Poimboeuf 		} else if (!strcmp(name, ".strtab")) {
1418782e7caSJosh Poimboeuf 			strtab_idx = i;
142627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".orc_unwind")) {
143627fce14SJosh Poimboeuf 			orc = data->d_buf;
144627fce14SJosh Poimboeuf 			orc_size = sh.sh_size;
145627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".orc_unwind_ip")) {
146627fce14SJosh Poimboeuf 			orc_ip = data->d_buf;
147627fce14SJosh Poimboeuf 			orc_ip_addr = sh.sh_addr;
148627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".rela.orc_unwind_ip")) {
149627fce14SJosh Poimboeuf 			rela_orc_ip = data;
150627fce14SJosh Poimboeuf 		}
151627fce14SJosh Poimboeuf 	}
152627fce14SJosh Poimboeuf 
1538782e7caSJosh Poimboeuf 	if (!symtab || !strtab_idx || !orc || !orc_ip)
154627fce14SJosh Poimboeuf 		return 0;
155627fce14SJosh Poimboeuf 
156627fce14SJosh Poimboeuf 	if (orc_size % sizeof(*orc) != 0) {
157627fce14SJosh Poimboeuf 		WARN("bad .orc_unwind section size");
158627fce14SJosh Poimboeuf 		return -1;
159627fce14SJosh Poimboeuf 	}
160627fce14SJosh Poimboeuf 
161627fce14SJosh Poimboeuf 	nr_entries = orc_size / sizeof(*orc);
162627fce14SJosh Poimboeuf 	for (i = 0; i < nr_entries; i++) {
163627fce14SJosh Poimboeuf 		if (rela_orc_ip) {
164627fce14SJosh Poimboeuf 			if (!gelf_getrela(rela_orc_ip, i, &rela)) {
165627fce14SJosh Poimboeuf 				WARN_ELF("gelf_getrela");
166627fce14SJosh Poimboeuf 				return -1;
167627fce14SJosh Poimboeuf 			}
168627fce14SJosh Poimboeuf 
169627fce14SJosh Poimboeuf 			if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
170627fce14SJosh Poimboeuf 				WARN_ELF("gelf_getsym");
171627fce14SJosh Poimboeuf 				return -1;
172627fce14SJosh Poimboeuf 			}
173627fce14SJosh Poimboeuf 
1748782e7caSJosh Poimboeuf 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
175627fce14SJosh Poimboeuf 				scn = elf_getscn(elf, sym.st_shndx);
176627fce14SJosh Poimboeuf 				if (!scn) {
177627fce14SJosh Poimboeuf 					WARN_ELF("elf_getscn");
178627fce14SJosh Poimboeuf 					return -1;
179627fce14SJosh Poimboeuf 				}
180627fce14SJosh Poimboeuf 
181627fce14SJosh Poimboeuf 				if (!gelf_getshdr(scn, &sh)) {
182627fce14SJosh Poimboeuf 					WARN_ELF("gelf_getshdr");
183627fce14SJosh Poimboeuf 					return -1;
184627fce14SJosh Poimboeuf 				}
185627fce14SJosh Poimboeuf 
186627fce14SJosh Poimboeuf 				name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
1878782e7caSJosh Poimboeuf 				if (!name) {
188627fce14SJosh Poimboeuf 					WARN_ELF("elf_strptr");
189627fce14SJosh Poimboeuf 					return -1;
190627fce14SJosh Poimboeuf 				}
1918782e7caSJosh Poimboeuf 			} else {
1928782e7caSJosh Poimboeuf 				name = elf_strptr(elf, strtab_idx, sym.st_name);
1938782e7caSJosh Poimboeuf 				if (!name) {
1948782e7caSJosh Poimboeuf 					WARN_ELF("elf_strptr");
1958782e7caSJosh Poimboeuf 					return -1;
1968782e7caSJosh Poimboeuf 				}
1978782e7caSJosh Poimboeuf 			}
198627fce14SJosh Poimboeuf 
19914c47b54SMikulas Patocka 			printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
200627fce14SJosh Poimboeuf 
201627fce14SJosh Poimboeuf 		} else {
20214c47b54SMikulas Patocka 			printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
203627fce14SJosh Poimboeuf 		}
204627fce14SJosh Poimboeuf 
205627fce14SJosh Poimboeuf 
206627fce14SJosh Poimboeuf 		printf(" sp:");
207627fce14SJosh Poimboeuf 
208*0646c28bSChristophe Leroy 		print_reg(orc[i].sp_reg, bswap_if_needed(&dummy_elf, orc[i].sp_offset));
209627fce14SJosh Poimboeuf 
210627fce14SJosh Poimboeuf 		printf(" bp:");
211627fce14SJosh Poimboeuf 
212*0646c28bSChristophe Leroy 		print_reg(orc[i].bp_reg, bswap_if_needed(&dummy_elf, orc[i].bp_offset));
213627fce14SJosh Poimboeuf 
214d31a5802SJosh Poimboeuf 		printf(" type:%s end:%d\n",
215d31a5802SJosh Poimboeuf 		       orc_type_name(orc[i].type), orc[i].end);
216627fce14SJosh Poimboeuf 	}
217627fce14SJosh Poimboeuf 
218627fce14SJosh Poimboeuf 	elf_end(elf);
219627fce14SJosh Poimboeuf 	close(fd);
220627fce14SJosh Poimboeuf 
221627fce14SJosh Poimboeuf 	return 0;
222627fce14SJosh Poimboeuf }
223