xref: /openbmc/linux/tools/objtool/orc_dump.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
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>
70decf1f8SMatt Helsley #include <asm/orc_types.h>
87786032eSVasily Gorbik #include <objtool/objtool.h>
97786032eSVasily Gorbik #include <objtool/warn.h>
107786032eSVasily Gorbik #include <objtool/endianness.h>
11627fce14SJosh Poimboeuf 
reg_name(unsigned int reg)12627fce14SJosh Poimboeuf static const char *reg_name(unsigned int reg)
13627fce14SJosh Poimboeuf {
14627fce14SJosh Poimboeuf 	switch (reg) {
15627fce14SJosh Poimboeuf 	case ORC_REG_PREV_SP:
16627fce14SJosh Poimboeuf 		return "prevsp";
17627fce14SJosh Poimboeuf 	case ORC_REG_DX:
18627fce14SJosh Poimboeuf 		return "dx";
19627fce14SJosh Poimboeuf 	case ORC_REG_DI:
20627fce14SJosh Poimboeuf 		return "di";
21627fce14SJosh Poimboeuf 	case ORC_REG_BP:
22627fce14SJosh Poimboeuf 		return "bp";
23627fce14SJosh Poimboeuf 	case ORC_REG_SP:
24627fce14SJosh Poimboeuf 		return "sp";
25627fce14SJosh Poimboeuf 	case ORC_REG_R10:
26627fce14SJosh Poimboeuf 		return "r10";
27627fce14SJosh Poimboeuf 	case ORC_REG_R13:
28627fce14SJosh Poimboeuf 		return "r13";
29627fce14SJosh Poimboeuf 	case ORC_REG_BP_INDIRECT:
30627fce14SJosh Poimboeuf 		return "bp(ind)";
31627fce14SJosh Poimboeuf 	case ORC_REG_SP_INDIRECT:
32627fce14SJosh Poimboeuf 		return "sp(ind)";
33627fce14SJosh Poimboeuf 	default:
34627fce14SJosh Poimboeuf 		return "?";
35627fce14SJosh Poimboeuf 	}
36627fce14SJosh Poimboeuf }
37627fce14SJosh Poimboeuf 
orc_type_name(unsigned int type)38627fce14SJosh Poimboeuf static const char *orc_type_name(unsigned int type)
39627fce14SJosh Poimboeuf {
40627fce14SJosh Poimboeuf 	switch (type) {
41*fb799447SJosh Poimboeuf 	case ORC_TYPE_UNDEFINED:
42*fb799447SJosh Poimboeuf 		return "(und)";
43*fb799447SJosh Poimboeuf 	case ORC_TYPE_END_OF_STACK:
44*fb799447SJosh Poimboeuf 		return "end";
45f902cfddSJosh Poimboeuf 	case ORC_TYPE_CALL:
46627fce14SJosh Poimboeuf 		return "call";
47f902cfddSJosh Poimboeuf 	case ORC_TYPE_REGS:
48627fce14SJosh Poimboeuf 		return "regs";
49f902cfddSJosh Poimboeuf 	case ORC_TYPE_REGS_PARTIAL:
50ee819aedSJulien Thierry 		return "regs (partial)";
51627fce14SJosh Poimboeuf 	default:
52627fce14SJosh Poimboeuf 		return "?";
53627fce14SJosh Poimboeuf 	}
54627fce14SJosh Poimboeuf }
55627fce14SJosh Poimboeuf 
print_reg(unsigned int reg,int offset)56627fce14SJosh Poimboeuf static void print_reg(unsigned int reg, int offset)
57627fce14SJosh Poimboeuf {
58627fce14SJosh Poimboeuf 	if (reg == ORC_REG_BP_INDIRECT)
59627fce14SJosh Poimboeuf 		printf("(bp%+d)", offset);
60627fce14SJosh Poimboeuf 	else if (reg == ORC_REG_SP_INDIRECT)
6187ccc826SPeter Zijlstra 		printf("(sp)%+d", offset);
62627fce14SJosh Poimboeuf 	else if (reg == ORC_REG_UNDEFINED)
63627fce14SJosh Poimboeuf 		printf("(und)");
64627fce14SJosh Poimboeuf 	else
65627fce14SJosh Poimboeuf 		printf("%s%+d", reg_name(reg), offset);
66627fce14SJosh Poimboeuf }
67627fce14SJosh Poimboeuf 
orc_dump(const char * _objname)68627fce14SJosh Poimboeuf int orc_dump(const char *_objname)
69627fce14SJosh Poimboeuf {
70627fce14SJosh Poimboeuf 	int fd, nr_entries, i, *orc_ip = NULL, orc_size = 0;
71627fce14SJosh Poimboeuf 	struct orc_entry *orc = NULL;
72627fce14SJosh Poimboeuf 	char *name;
7314c47b54SMikulas Patocka 	size_t nr_sections;
7414c47b54SMikulas Patocka 	Elf64_Addr orc_ip_addr = 0;
758782e7caSJosh Poimboeuf 	size_t shstrtab_idx, strtab_idx = 0;
76627fce14SJosh Poimboeuf 	Elf *elf;
77627fce14SJosh Poimboeuf 	Elf_Scn *scn;
78627fce14SJosh Poimboeuf 	GElf_Shdr sh;
79627fce14SJosh Poimboeuf 	GElf_Rela rela;
80627fce14SJosh Poimboeuf 	GElf_Sym sym;
81627fce14SJosh Poimboeuf 	Elf_Data *data, *symtab = NULL, *rela_orc_ip = NULL;
820646c28bSChristophe Leroy 	struct elf dummy_elf = {};
83627fce14SJosh Poimboeuf 
84627fce14SJosh Poimboeuf 
85627fce14SJosh Poimboeuf 	objname = _objname;
86627fce14SJosh Poimboeuf 
87627fce14SJosh Poimboeuf 	elf_version(EV_CURRENT);
88627fce14SJosh Poimboeuf 
89627fce14SJosh Poimboeuf 	fd = open(objname, O_RDONLY);
90627fce14SJosh Poimboeuf 	if (fd == -1) {
91627fce14SJosh Poimboeuf 		perror("open");
92627fce14SJosh Poimboeuf 		return -1;
93627fce14SJosh Poimboeuf 	}
94627fce14SJosh Poimboeuf 
95627fce14SJosh Poimboeuf 	elf = elf_begin(fd, ELF_C_READ_MMAP, NULL);
96627fce14SJosh Poimboeuf 	if (!elf) {
97627fce14SJosh Poimboeuf 		WARN_ELF("elf_begin");
98627fce14SJosh Poimboeuf 		return -1;
99627fce14SJosh Poimboeuf 	}
100627fce14SJosh Poimboeuf 
1010646c28bSChristophe Leroy 	if (!elf64_getehdr(elf)) {
1020646c28bSChristophe Leroy 		WARN_ELF("elf64_getehdr");
1030646c28bSChristophe Leroy 		return -1;
1040646c28bSChristophe Leroy 	}
1050646c28bSChristophe Leroy 	memcpy(&dummy_elf.ehdr, elf64_getehdr(elf), sizeof(dummy_elf.ehdr));
1060646c28bSChristophe Leroy 
107627fce14SJosh Poimboeuf 	if (elf_getshdrnum(elf, &nr_sections)) {
108627fce14SJosh Poimboeuf 		WARN_ELF("elf_getshdrnum");
109627fce14SJosh Poimboeuf 		return -1;
110627fce14SJosh Poimboeuf 	}
111627fce14SJosh Poimboeuf 
112627fce14SJosh Poimboeuf 	if (elf_getshdrstrndx(elf, &shstrtab_idx)) {
113627fce14SJosh Poimboeuf 		WARN_ELF("elf_getshdrstrndx");
114627fce14SJosh Poimboeuf 		return -1;
115627fce14SJosh Poimboeuf 	}
116627fce14SJosh Poimboeuf 
117627fce14SJosh Poimboeuf 	for (i = 0; i < nr_sections; i++) {
118627fce14SJosh Poimboeuf 		scn = elf_getscn(elf, i);
119627fce14SJosh Poimboeuf 		if (!scn) {
120627fce14SJosh Poimboeuf 			WARN_ELF("elf_getscn");
121627fce14SJosh Poimboeuf 			return -1;
122627fce14SJosh Poimboeuf 		}
123627fce14SJosh Poimboeuf 
124627fce14SJosh Poimboeuf 		if (!gelf_getshdr(scn, &sh)) {
125627fce14SJosh Poimboeuf 			WARN_ELF("gelf_getshdr");
126627fce14SJosh Poimboeuf 			return -1;
127627fce14SJosh Poimboeuf 		}
128627fce14SJosh Poimboeuf 
129627fce14SJosh Poimboeuf 		name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
130627fce14SJosh Poimboeuf 		if (!name) {
131627fce14SJosh Poimboeuf 			WARN_ELF("elf_strptr");
132627fce14SJosh Poimboeuf 			return -1;
133627fce14SJosh Poimboeuf 		}
134627fce14SJosh Poimboeuf 
135627fce14SJosh Poimboeuf 		data = elf_getdata(scn, NULL);
136627fce14SJosh Poimboeuf 		if (!data) {
137627fce14SJosh Poimboeuf 			WARN_ELF("elf_getdata");
138627fce14SJosh Poimboeuf 			return -1;
139627fce14SJosh Poimboeuf 		}
140627fce14SJosh Poimboeuf 
141627fce14SJosh Poimboeuf 		if (!strcmp(name, ".symtab")) {
142627fce14SJosh Poimboeuf 			symtab = data;
1438782e7caSJosh Poimboeuf 		} else if (!strcmp(name, ".strtab")) {
1448782e7caSJosh Poimboeuf 			strtab_idx = i;
145627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".orc_unwind")) {
146627fce14SJosh Poimboeuf 			orc = data->d_buf;
147627fce14SJosh Poimboeuf 			orc_size = sh.sh_size;
148627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".orc_unwind_ip")) {
149627fce14SJosh Poimboeuf 			orc_ip = data->d_buf;
150627fce14SJosh Poimboeuf 			orc_ip_addr = sh.sh_addr;
151627fce14SJosh Poimboeuf 		} else if (!strcmp(name, ".rela.orc_unwind_ip")) {
152627fce14SJosh Poimboeuf 			rela_orc_ip = data;
153627fce14SJosh Poimboeuf 		}
154627fce14SJosh Poimboeuf 	}
155627fce14SJosh Poimboeuf 
1568782e7caSJosh Poimboeuf 	if (!symtab || !strtab_idx || !orc || !orc_ip)
157627fce14SJosh Poimboeuf 		return 0;
158627fce14SJosh Poimboeuf 
159627fce14SJosh Poimboeuf 	if (orc_size % sizeof(*orc) != 0) {
160627fce14SJosh Poimboeuf 		WARN("bad .orc_unwind section size");
161627fce14SJosh Poimboeuf 		return -1;
162627fce14SJosh Poimboeuf 	}
163627fce14SJosh Poimboeuf 
164627fce14SJosh Poimboeuf 	nr_entries = orc_size / sizeof(*orc);
165627fce14SJosh Poimboeuf 	for (i = 0; i < nr_entries; i++) {
166627fce14SJosh Poimboeuf 		if (rela_orc_ip) {
167627fce14SJosh Poimboeuf 			if (!gelf_getrela(rela_orc_ip, i, &rela)) {
168627fce14SJosh Poimboeuf 				WARN_ELF("gelf_getrela");
169627fce14SJosh Poimboeuf 				return -1;
170627fce14SJosh Poimboeuf 			}
171627fce14SJosh Poimboeuf 
172627fce14SJosh Poimboeuf 			if (!gelf_getsym(symtab, GELF_R_SYM(rela.r_info), &sym)) {
173627fce14SJosh Poimboeuf 				WARN_ELF("gelf_getsym");
174627fce14SJosh Poimboeuf 				return -1;
175627fce14SJosh Poimboeuf 			}
176627fce14SJosh Poimboeuf 
1778782e7caSJosh Poimboeuf 			if (GELF_ST_TYPE(sym.st_info) == STT_SECTION) {
178627fce14SJosh Poimboeuf 				scn = elf_getscn(elf, sym.st_shndx);
179627fce14SJosh Poimboeuf 				if (!scn) {
180627fce14SJosh Poimboeuf 					WARN_ELF("elf_getscn");
181627fce14SJosh Poimboeuf 					return -1;
182627fce14SJosh Poimboeuf 				}
183627fce14SJosh Poimboeuf 
184627fce14SJosh Poimboeuf 				if (!gelf_getshdr(scn, &sh)) {
185627fce14SJosh Poimboeuf 					WARN_ELF("gelf_getshdr");
186627fce14SJosh Poimboeuf 					return -1;
187627fce14SJosh Poimboeuf 				}
188627fce14SJosh Poimboeuf 
189627fce14SJosh Poimboeuf 				name = elf_strptr(elf, shstrtab_idx, sh.sh_name);
1908782e7caSJosh Poimboeuf 				if (!name) {
191627fce14SJosh Poimboeuf 					WARN_ELF("elf_strptr");
192627fce14SJosh Poimboeuf 					return -1;
193627fce14SJosh Poimboeuf 				}
1948782e7caSJosh Poimboeuf 			} else {
1958782e7caSJosh Poimboeuf 				name = elf_strptr(elf, strtab_idx, sym.st_name);
1968782e7caSJosh Poimboeuf 				if (!name) {
1978782e7caSJosh Poimboeuf 					WARN_ELF("elf_strptr");
1988782e7caSJosh Poimboeuf 					return -1;
1998782e7caSJosh Poimboeuf 				}
2008782e7caSJosh Poimboeuf 			}
201627fce14SJosh Poimboeuf 
20214c47b54SMikulas Patocka 			printf("%s+%llx:", name, (unsigned long long)rela.r_addend);
203627fce14SJosh Poimboeuf 
204627fce14SJosh Poimboeuf 		} else {
20514c47b54SMikulas Patocka 			printf("%llx:", (unsigned long long)(orc_ip_addr + (i * sizeof(int)) + orc_ip[i]));
206627fce14SJosh Poimboeuf 		}
207627fce14SJosh Poimboeuf 
208*fb799447SJosh Poimboeuf 		printf("type:%s", orc_type_name(orc[i].type));
209627fce14SJosh Poimboeuf 
210627fce14SJosh Poimboeuf 		printf(" sp:");
211627fce14SJosh Poimboeuf 
2120646c28bSChristophe Leroy 		print_reg(orc[i].sp_reg, bswap_if_needed(&dummy_elf, orc[i].sp_offset));
213627fce14SJosh Poimboeuf 
214627fce14SJosh Poimboeuf 		printf(" bp:");
215627fce14SJosh Poimboeuf 
2160646c28bSChristophe Leroy 		print_reg(orc[i].bp_reg, bswap_if_needed(&dummy_elf, orc[i].bp_offset));
217627fce14SJosh Poimboeuf 
218*fb799447SJosh Poimboeuf 		printf(" signal:%d\n", orc[i].signal);
219627fce14SJosh Poimboeuf 	}
220627fce14SJosh Poimboeuf 
221627fce14SJosh Poimboeuf 	elf_end(elf);
222627fce14SJosh Poimboeuf 	close(fd);
223627fce14SJosh Poimboeuf 
224627fce14SJosh Poimboeuf 	return 0;
225627fce14SJosh Poimboeuf }
226