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

Fix a previously introduced bug in ElfTests related to PIE and addresses.

Summary:
This failed on mode/opt, because the `- sh_addr + sh_offset` was actually using the address and offset of the *symbol table section*, not the *data section* (where the kStringValue variable actually is). It still worked, because for mapped sections, the `offset - address` just happens to be the same for all sections, so it didn't matter what section we used; in debug mode, we use dynamic linking, so the symbol section is called .dynsym and is loaded in memory, but in opt mode, the symbol section is called .symtab and is *not* loaded, so sh_addr is zero, and that formula no longer works.

The goal there was really just to subtract the binary base address encoded in the ELF file. ElfFile already provides this via getBaseAddress(), and that's also how we can identify whether the loaded ELF (in this case, the same as the current executable) is PIE; so switched both the calculation and the if condition to use this result.

Reviewed By: yfeldblum

Differential Revision: D23020998

fbshipit-source-id: f7e66bb554c62aa13d7d2fc1017cb31542507be9
parent 38bbc80e
......@@ -39,6 +39,7 @@ TEST_F(ElfTest, IntegerValue) {
}
TEST_F(ElfTest, PointerValue) {
auto rawBaseAddress = elfFile_.getBaseAddress();
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);
......@@ -48,9 +49,8 @@ TEST_F(ElfTest, PointerValue) {
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) {
binaryBase + (sym.second->st_value - rawBaseAddress)));
if (rawBaseAddress != 0) { // non-PIE
// 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
......
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