Skip to content

Commit eb755df

Browse files
Split findUnwindSectionsByPhdr into target-specific functions.
Summary: This further cleans up the control flow and makes it easier to optimize and replace portions in a subsequent patch. This should be NFC, but given the amount of #ifdeffing here, it may not be. So will watch the buildbots closely. Also, as this is purely moving existing code around, I plan to ignore the lint errors. Reviewers: compnerd, miyuki, mstorsjo Subscribers: libcxx-commits Tags: #libc Differential Revision: https://reviews.llvm.org/D75705
1 parent f0f4d41 commit eb755df

File tree

1 file changed

+44
-23
lines changed

1 file changed

+44
-23
lines changed

libunwind/src/AddressSpace.hpp

Lines changed: 44 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -409,24 +409,6 @@ LocalAddressSpace::getEncodedP(pint_t &addr, pint_t end, uint8_t encoding,
409409
// that don't help at all.
410410
#elif defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
411411

412-
struct _LIBUNWIND_HIDDEN dl_iterate_cb_data {
413-
LocalAddressSpace *addressSpace;
414-
UnwindInfoSections *sects;
415-
uintptr_t targetAddr;
416-
};
417-
418-
int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
419-
auto cbdata = static_cast<dl_iterate_cb_data *>(data);
420-
bool found_obj = false;
421-
bool found_hdr = false;
422-
423-
assert(cbdata);
424-
assert(cbdata->sects);
425-
426-
if (cbdata->targetAddr < pinfo->dlpi_addr) {
427-
return false;
428-
}
429-
430412
#if !defined(Elf_Half)
431413
typedef ElfW(Half) Elf_Half;
432414
#endif
@@ -437,8 +419,8 @@ int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
437419
typedef ElfW(Addr) Elf_Addr;
438420
#endif
439421

422+
static Elf_Addr calculateImageBase(struct dl_phdr_info *pinfo) {
440423
Elf_Addr image_base = pinfo->dlpi_addr;
441-
442424
#if defined(__ANDROID__) && __ANDROID_API__ < 18
443425
if (image_base == 0) {
444426
// Normally, an image base of 0 indicates a non-PIE executable. On
@@ -456,11 +438,32 @@ int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
456438
}
457439
}
458440
#endif
441+
return image_base;
442+
}
459443

460-
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
444+
struct _LIBUNWIND_HIDDEN dl_iterate_cb_data {
445+
LocalAddressSpace *addressSpace;
446+
UnwindInfoSections *sects;
447+
uintptr_t targetAddr;
448+
};
449+
450+
#if defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
461451
#if !defined(_LIBUNWIND_SUPPORT_DWARF_INDEX)
462-
#error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
452+
#error "_LIBUNWIND_SUPPORT_DWARF_UNWIND requires _LIBUNWIND_SUPPORT_DWARF_INDEX on this platform."
463453
#endif
454+
455+
int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
456+
auto *cbdata = static_cast<dl_iterate_cb_data *>(data);
457+
bool found_obj = false;
458+
bool found_hdr = false;
459+
460+
assert(cbdata);
461+
assert(cbdata->sects);
462+
463+
if (cbdata->targetAddr < pinfo->dlpi_addr)
464+
return 0;
465+
466+
Elf_Addr image_base = calculateImageBase(pinfo);
464467
size_t object_length;
465468

466469
for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
@@ -492,7 +495,25 @@ int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
492495
} else {
493496
return false;
494497
}
495-
#else // defined(_LIBUNWIND_ARM_EHABI)
498+
}
499+
500+
#else // defined(LIBUNWIND_SUPPORT_DWARF_UNWIND)
501+
// Given all the #ifdef's above, the code here is for
502+
// defined(LIBUNWIND_ARM_EHABI)
503+
504+
int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
505+
auto *cbdata = static_cast<dl_iterate_cb_data *>(data);
506+
bool found_obj = false;
507+
bool found_hdr = false;
508+
509+
assert(cbdata);
510+
assert(cbdata->sects);
511+
512+
if (cbdata->targetAddr < pinfo->dlpi_addr)
513+
return 0;
514+
515+
Elf_Addr image_base = calculateImageBase(pinfo);
516+
496517
for (Elf_Half i = 0; i < pinfo->dlpi_phnum; i++) {
497518
const Elf_Phdr *phdr = &pinfo->dlpi_phdr[i];
498519
if (phdr->p_type == PT_LOAD) {
@@ -508,8 +529,8 @@ int findUnwindSectionsByPhdr(struct dl_phdr_info *pinfo, size_t, void *data) {
508529
}
509530
}
510531
return found_obj && found_hdr;
511-
#endif
512532
}
533+
#endif // defined(LIBUNWIND_SUPPORT_DWARF_UNWIND)
513534
#endif // defined(_LIBUNWIND_ARM_EHABI) || defined(_LIBUNWIND_SUPPORT_DWARF_UNWIND)
514535

515536

0 commit comments

Comments
 (0)