Fix static GetLoadBias function.
The load bias value set in ReadProgramHeaders is out of sync with the
algorithm used in the static GetLoadBias function.
Sync the two and add tests to verify that they stay in sync.
Test: Unit tests pass.
Change-Id: I20ac0104970a22a92a5314a41dcadad0c9c22e64
diff --git a/libunwindstack/ElfInterface.cpp b/libunwindstack/ElfInterface.cpp
index bdfee01..be1f092 100644
--- a/libunwindstack/ElfInterface.cpp
+++ b/libunwindstack/ElfInterface.cpp
@@ -187,8 +187,13 @@
if (!memory->ReadFully(offset, &phdr, sizeof(phdr))) {
return 0;
}
- if (phdr.p_type == PT_LOAD && phdr.p_offset == 0) {
- return phdr.p_vaddr;
+
+ // Find the first executable load when looking for the load bias.
+ if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X)) {
+ if (phdr.p_vaddr > phdr.p_offset) {
+ return phdr.p_vaddr - phdr.p_offset;
+ }
+ break;
}
}
return 0;