Skip to content

Commit dc2cf4c

Browse files
MaskRayacmel
authored andcommitted
perf unwind: Fix segbase for ld.lld linked objects
segbase is the address of .eh_frame_hdr and table_data is segbase plus the header size. find_proc_info computes segbase as `map->start + segbase - map->pgoff` which is wrong when * .eh_frame_hdr and .text are in different PT_LOAD program headers * and their p_vaddr difference does not equal their p_offset difference Since 10.0, ld.lld's default --rosegment -z noseparate-code layout has such R and RX PT_LOAD program headers. ld.lld (default) => perf report fails to unwind `perf record --call-graph dwarf` recorded data ld.lld --no-rosegment => ok (trivial, no R PT_LOAD) ld.lld -z separate-code => ok but by luck: there are two PT_LOAD but their p_vaddr difference equals p_offset difference ld.bfd -z noseparate-code => ok (trivial, no R PT_LOAD) ld.bfd -z separate-code (default for Linux/x86) => ok but by luck: there are two PT_LOAD but their p_vaddr difference equals p_offset difference To fix the issue, compute segbase as dso's base address plus PT_GNU_EH_FRAME's p_vaddr. The base address is computed by iterating over all dso-associated maps and then subtract the first PT_LOAD p_vaddr (the minimum guaranteed by generic ABI) from the minimum address. In libunwind, find_proc_info transitively called by unw_step is cached, so the iteration overhead is acceptable. Reported-by: Sebastian Ullrich <[email protected]> Reviewed-by: Ian Rogers <[email protected]> Signed-off-by: Fangrui Song <[email protected]> Cc: Ingo Molnar <[email protected]> Cc: Peter Zijlstra <[email protected]> Cc: [email protected] Link: ClangBuiltLinux#1646 Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Arnaldo Carvalho de Melo <[email protected]>
1 parent 4f52ca1 commit dc2cf4c

File tree

2 files changed

+77
-30
lines changed

2 files changed

+77
-30
lines changed

tools/perf/util/dso.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,9 @@ struct dso {
196196
u32 status_seen;
197197
u64 file_size;
198198
struct list_head open_entry;
199+
u64 elf_base_addr;
199200
u64 debug_frame_offset;
201+
u64 eh_frame_hdr_addr;
200202
u64 eh_frame_hdr_offset;
201203
} data;
202204
/* bpf prog information */

tools/perf/util/unwind-libunwind-local.c

Lines changed: 75 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -169,29 +169,63 @@ static int __dw_read_encoded_value(u8 **p, u8 *end, u64 *val,
169169
__v; \
170170
})
171171

172-
static u64 elf_section_offset(int fd, const char *name)
172+
static int elf_section_address_and_offset(int fd, const char *name, u64 *address, u64 *offset)
173173
{
174174
Elf *elf;
175175
GElf_Ehdr ehdr;
176176
GElf_Shdr shdr;
177-
u64 offset = 0;
177+
int ret;
178178

179179
elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
180180
if (elf == NULL)
181+
return -1;
182+
183+
if (gelf_getehdr(elf, &ehdr) == NULL)
184+
goto out_err;
185+
186+
if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL))
187+
goto out_err;
188+
189+
*address = shdr.sh_addr;
190+
*offset = shdr.sh_offset;
191+
ret = 0;
192+
out_err:
193+
elf_end(elf);
194+
return ret;
195+
}
196+
197+
#ifndef NO_LIBUNWIND_DEBUG_FRAME
198+
static u64 elf_section_offset(int fd, const char *name)
199+
{
200+
u64 address, offset;
201+
202+
if (elf_section_address_and_offset(fd, name, &address, &offset))
181203
return 0;
182204

183-
do {
184-
if (gelf_getehdr(elf, &ehdr) == NULL)
185-
break;
205+
return offset;
206+
}
207+
#endif
186208

187-
if (!elf_section_by_name(elf, &ehdr, &shdr, name, NULL))
188-
break;
209+
static u64 elf_base_address(int fd)
210+
{
211+
Elf *elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL);
212+
GElf_Phdr phdr;
213+
u64 retval = 0;
214+
size_t i, phdrnum = 0;
189215

190-
offset = shdr.sh_offset;
191-
} while (0);
216+
if (elf == NULL)
217+
return 0;
218+
(void)elf_getphdrnum(elf, &phdrnum);
219+
/* PT_LOAD segments are sorted by p_vaddr, so the first has the minimum p_vaddr. */
220+
for (i = 0; i < phdrnum; i++) {
221+
if (gelf_getphdr(elf, i, &phdr) && phdr.p_type == PT_LOAD) {
222+
retval = phdr.p_vaddr & -getpagesize();
223+
break;
224+
}
225+
}
192226

193227
elf_end(elf);
194-
return offset;
228+
return retval;
195229
}
196230

197231
#ifndef NO_LIBUNWIND_DEBUG_FRAME
@@ -248,8 +282,7 @@ struct eh_frame_hdr {
248282
} __packed;
249283

250284
static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
251-
u64 offset, u64 *table_data, u64 *segbase,
252-
u64 *fde_count)
285+
u64 offset, u64 *table_data_offset, u64 *fde_count)
253286
{
254287
struct eh_frame_hdr hdr;
255288
u8 *enc = (u8 *) &hdr.enc;
@@ -265,35 +298,47 @@ static int unwind_spec_ehframe(struct dso *dso, struct machine *machine,
265298
dw_read_encoded_value(enc, end, hdr.eh_frame_ptr_enc);
266299

267300
*fde_count = dw_read_encoded_value(enc, end, hdr.fde_count_enc);
268-
*segbase = offset;
269-
*table_data = (enc - (u8 *) &hdr) + offset;
301+
*table_data_offset = enc - (u8 *) &hdr;
270302
return 0;
271303
}
272304

273-
static int read_unwind_spec_eh_frame(struct dso *dso, struct machine *machine,
305+
static int read_unwind_spec_eh_frame(struct dso *dso, struct unwind_info *ui,
274306
u64 *table_data, u64 *segbase,
275307
u64 *fde_count)
276308
{
277-
int ret = -EINVAL, fd;
278-
u64 offset = dso->data.eh_frame_hdr_offset;
309+
struct map *map;
310+
u64 base_addr = UINT64_MAX;
311+
int ret, fd;
279312

280-
if (offset == 0) {
281-
fd = dso__data_get_fd(dso, machine);
313+
if (dso->data.eh_frame_hdr_offset == 0) {
314+
fd = dso__data_get_fd(dso, ui->machine);
282315
if (fd < 0)
283316
return -EINVAL;
284317

285318
/* Check the .eh_frame section for unwinding info */
286-
offset = elf_section_offset(fd, ".eh_frame_hdr");
287-
dso->data.eh_frame_hdr_offset = offset;
319+
ret = elf_section_address_and_offset(fd, ".eh_frame_hdr",
320+
&dso->data.eh_frame_hdr_addr,
321+
&dso->data.eh_frame_hdr_offset);
322+
dso->data.elf_base_addr = elf_base_address(fd);
288323
dso__data_put_fd(dso);
324+
if (ret || dso->data.eh_frame_hdr_offset == 0)
325+
return -EINVAL;
289326
}
290327

291-
if (offset)
292-
ret = unwind_spec_ehframe(dso, machine, offset,
293-
table_data, segbase,
294-
fde_count);
295-
296-
return ret;
328+
maps__for_each_entry(ui->thread->maps, map) {
329+
if (map->dso == dso && map->start < base_addr)
330+
base_addr = map->start;
331+
}
332+
base_addr -= dso->data.elf_base_addr;
333+
/* Address of .eh_frame_hdr */
334+
*segbase = base_addr + dso->data.eh_frame_hdr_addr;
335+
ret = unwind_spec_ehframe(dso, ui->machine, dso->data.eh_frame_hdr_offset,
336+
table_data, fde_count);
337+
if (ret)
338+
return ret;
339+
/* binary_search_table offset plus .eh_frame_hdr address */
340+
*table_data += *segbase;
341+
return 0;
297342
}
298343

299344
#ifndef NO_LIBUNWIND_DEBUG_FRAME
@@ -388,14 +433,14 @@ find_proc_info(unw_addr_space_t as, unw_word_t ip, unw_proc_info_t *pi,
388433
pr_debug("unwind: find_proc_info dso %s\n", map->dso->name);
389434

390435
/* Check the .eh_frame section for unwinding info */
391-
if (!read_unwind_spec_eh_frame(map->dso, ui->machine,
436+
if (!read_unwind_spec_eh_frame(map->dso, ui,
392437
&table_data, &segbase, &fde_count)) {
393438
memset(&di, 0, sizeof(di));
394439
di.format = UNW_INFO_FORMAT_REMOTE_TABLE;
395440
di.start_ip = map->start;
396441
di.end_ip = map->end;
397-
di.u.rti.segbase = map->start + segbase - map->pgoff;
398-
di.u.rti.table_data = map->start + table_data - map->pgoff;
442+
di.u.rti.segbase = segbase;
443+
di.u.rti.table_data = table_data;
399444
di.u.rti.table_len = fde_count * sizeof(struct table_entry)
400445
/ sizeof(unw_word_t);
401446
ret = dwarf_search_unwind_table(as, ip, &di, pi,

0 commit comments

Comments
 (0)