Commit 95918c0f authored by Mark Williams's avatar Mark Williams Committed by Facebook Github Bot

Fix a bug in ElfFile::getSectionByName

Summary:
It first looks for the name, then looks for a section with that
offset. But strings can be overlaid; eg .rela.debug_info and .debug_info
can be overlapped, so we won't find .debug_info at all.

Instead, just iterate the sections checking to see if the name matches
the one we're looking for.

Reviewed By: ricklavoie, palmtenor

Differential Revision: D9809817

fbshipit-source-id: b165719c6f9004ecc964096677114bce357ece30
parent 6cc710c4
......@@ -353,22 +353,15 @@ const ElfShdr* ElfFile::getSectionByName(const char* name) const {
return nullptr; // no section name string table
}
// Find offset in the section name string table of the requested name
const ElfShdr& sectionNames = *getSectionByIndex(elfHeader().e_shstrndx);
const char* foundName = iterateStrings(
sectionNames, [&](const char* s) { return !strcmp(name, s); });
if (foundName == nullptr) {
return nullptr;
}
size_t offset = foundName - (file_ + sectionNames.sh_offset);
const char* start = file_ + sectionNames.sh_offset;
// Find section with the appropriate sh_name offset
const ElfShdr* foundSection = iterateSections([&](const ElfShdr& sh) {
if (sh.sh_name == offset) {
return true;
}
if (sh.sh_name >= sectionNames.sh_size) {
return false;
}
return !strcmp(start + sh.sh_name, name);
});
return foundSection;
}
......
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