Commit e0bd7f09 authored by Robin Cheng's avatar Robin Cheng Committed by Facebook GitHub Bot

Make folly/experimental/symbolizer/test:elf_tests work if compiled as PIE.

Summary: If the binary is compiled as PIE, such as in the case of TSAN, the kStringValue symbol's value, which is an address that in turn points to the "coconuts" character array, may be stored as 0 in the binary file, and rely on the dynamic linker to relocate at loading time. The corresponding relocation entry (in the .rela.dyn section) contains the relative offset instead. We do not seem to have implemented parsing of the relocation section, so this diff changes the test to be more lenient, and upon seeing a zero will just verify the address of the symbol against the live binary.

Reviewed By: yfeldblum

Differential Revision: D22782080

fbshipit-source-id: ffbfc7c23b84865fb29bbfb782feb896d687594b
parent 45cd2ae5
......@@ -18,6 +18,7 @@
#include <folly/experimental/TestUtil.h>
#include <folly/experimental/symbolizer/Elf.h>
#include <folly/portability/GTest.h>
#include <sys/auxv.h>
using folly::symbolizer::ElfFile;
......@@ -41,8 +42,24 @@ TEST_F(ElfTest, PointerValue) {
auto sym = elfFile_.getSymbolByName("kStringValue");
EXPECT_NE(nullptr, sym.first) << "Failed to look up symbol kStringValue";
ElfW(Addr) addr = elfFile_.getSymbolValue<ElfW(Addr)>(sym.second);
// Let's check the address for the symbol against our own copy of
// kStringValue.
auto binaryBase = getauxval(AT_PHDR) - 0x40; // address of our own image
EXPECT_EQ(
static_cast<const void*>(&kStringValue),
reinterpret_cast<const void*>(
binaryBase +
(sym.second->st_value - sym.first->sh_addr + sym.first->sh_offset)));
if (addr != 0) {
// Only do this check if we have a non-PIE. For the PIE case, the compiler
// could put a 0 in the .data section for kStringValue, and then rely on
// the dynamic linker to fill in the actual pointer to the .rodata section
// via relocation; the actual offset of the string is encoded in the
// .rela.dyn section, which isn't parsed in the current implementation of
// ElfFile.
const char* str = &elfFile_.getAddressValue<const char>(addr);
EXPECT_STREQ(kStringValue, str);
}
}
TEST_F(ElfTest, iterateProgramHeaders) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment