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;
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
, ".orc_unwind")) {
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")) {
153 if (!symtab
|| !orc
|| !orc_ip
)
156 if (orc_size
% sizeof(*orc
) != 0) {
157 WARN("bad .orc_unwind section size");
161 nr_entries
= orc_size
/ sizeof(*orc
);
162 for (i
= 0; i
< nr_entries
; i
++) {
164 if (!gelf_getrela(rela_orc_ip
, i
, &rela
)) {
165 WARN_ELF("gelf_getrela");
169 if (!gelf_getsym(symtab
, GELF_R_SYM(rela
.r_info
), &sym
)) {
170 WARN_ELF("gelf_getsym");
174 scn
= elf_getscn(elf
, sym
.st_shndx
);
176 WARN_ELF("elf_getscn");
180 if (!gelf_getshdr(scn
, &sh
)) {
181 WARN_ELF("gelf_getshdr");
185 name
= elf_strptr(elf
, shstrtab_idx
, sh
.sh_name
);
186 if (!name
|| !*name
) {
187 WARN_ELF("elf_strptr");
191 printf("%s+%llx:", name
, (unsigned long long)rela
.r_addend
);
194 printf("%llx:", (unsigned long long)(orc_ip_addr
+ (i
* sizeof(int)) + orc_ip
[i
]));
200 print_reg(orc
[i
].sp_reg
, orc
[i
].sp_offset
);
204 print_reg(orc
[i
].bp_reg
, orc
[i
].bp_offset
);
206 printf(" type:%s\n", orc_type_name(orc
[i
].type
));