Skip to content
This repository was archived by the owner on Feb 12, 2026. It is now read-only.

Commit c04afe8

Browse files
committed
add: dl_iterate_phdr fallback for finding lib base
This commit adds "dl_iterate_phdr" fallback, a more robust approach, to find a library base. This fixes the issue where due to previous methods not being able to find the library base in some systems, causes the failure of its (ReLSPosed) operation.
1 parent 0f5859e commit c04afe8

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

core/src/main/jni/src/elf_util.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,47 @@ bool ElfImg::findModuleBase() {
454454
}
455455
}
456456

457+
// Step 3 (Final Fallback): If still not found, use dl_iterate_phdr to get the base address.
458+
/* TODO: Why not just use dl_iterate_phdr..? */
459+
if (!found_block) {
460+
LOGD("No `r-xp` block found. Falling back to dl_iterate_phdr.");
461+
462+
struct DlIterateData {
463+
std::string target_path;
464+
uintptr_t base_address = 0;
465+
} data{
466+
std::string(elf),
467+
0
468+
};
469+
470+
auto callback = [](struct dl_phdr_info *info, size_t, void *data_ptr) -> int {
471+
auto *data = reinterpret_cast<DlIterateData *>(data_ptr);
472+
if (info->dlpi_name) {
473+
std::string_view name(info->dlpi_name);
474+
475+
if (name.find(data->target_path) != std::string_view::npos) {
476+
data->base_address = info->dlpi_addr;
477+
data->target_path = info->dlpi_name;
478+
479+
return 1;
480+
}
481+
}
482+
483+
return 0;
484+
};
485+
486+
dl_iterate_phdr(callback, &data);
487+
488+
if (data.base_address != 0) {
489+
base = reinterpret_cast<void *>(data.base_address);
490+
elf = data.target_path;
491+
492+
LOGD("get module base {}: {:#x} via dl_iterate_phdr", elf, data.base_address);
493+
494+
return true;
495+
}
496+
}
497+
457498
if (!found_block) {
458499
LOGE("Fatal: Could not determine a base address for {}", elf.data());
459500
return false;

0 commit comments

Comments
 (0)