[libunwind][Android] Improve workaround for PIE zero-dlpi_addr bug

The workaround added in https://reviews.llvm.org/rL299575 appears to be
working around a bug in Android JB 4.1.x and 4.2.x (API 16 and 17).

Starting in API 16, Android added support for PIE binaries, but the
dynamic linker failed to initialize dlpi_addr to the address that the
executable was loaded at. The bug was fixed in Android JB 4.3.x (API 18).

Improve the true load bias calculation:

 * The code was assuming that the first segment would be the PT_PHDR
   segment. I think it's better to be explicit and search for PT_PHDR. (It
   will be almost as fast in practice.)

 * It's more correct to use p_vaddr rather than p_offset. If a PIE
   executable is linked with a non-zero image base (e.g. lld's
   -Wl,--image-base=xxxx), then we must use p_vaddr here.

The "phdr->p_vaddr < image_base" condition seems unnecessary and maybe
slightly wrong. If the OS were to load a binary at an address smaller than
a vaddr in the binary, we would still want to do this workaround.

The workaround is safe when the linker bug isn't present, because it
should calculate an image_base equal to dlpi_addr. Note that with API 21
and up, this workaround should never activate for dynamically-linked
objects, because non-PIE executables aren't allowed.

Consolidate the fix into a single block of code that calculates the true
image base, and make it clear that the fix no longer applies after API 18.

See https://github.com/android/ndk/issues/505 for details.

Reviewers: mclow.lists, srhines, danalbert, compnerd

Reviewed By: compnerd

Subscribers: srhines, krytarowski, christof, libcxx-commits

Tags: #libc

Differential Revision: https://reviews.llvm.org/D68971

git-svn-id: https://llvm.org/svn/llvm-project/libunwind/trunk@374969 91177308-0d34-0410-b5e6-96231b3b80d8
1 file changed