Merge pull request #385 from aeflores/always-parse-section-relocs

parse section relocations but do not parse dynamic relocations twice.
This commit is contained in:
Romain 2020-04-07 13:56:54 +02:00 committed by GitHub
commit 5d45f8c6cb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 16 deletions

View File

@ -430,23 +430,26 @@ void Parser::parse_binary(void) {
}
// Try to parse using sections
if (this->binary_->relocations_.size() == 0) {
for (const Section& section : this->binary_->sections()) {
try {
if (section.type() == ELF_SECTION_TYPES::SHT_REL) {
this->parse_section_relocations<ELF_T, typename ELF_T::Elf_Rel>(section);
}
else if (section.type() == ELF_SECTION_TYPES::SHT_RELA) {
this->parse_section_relocations<ELF_T, typename ELF_T::Elf_Rela>(section);
}
} catch (const exception& e) {
LOG(WARNING) << "Unable to parse relocations from section '"
<< section.name() << "'"
<< " (" << e.what() << ")";
// If we don't have any relocations, we parse all relocation sections
// otherwise, only the non-allocated sections to avoid parsing dynamic
// relocations (or plt relocations) twice.
bool skip_allocated_sections = this->binary_->relocations_.size() > 0;
for (const Section& section : this->binary_->sections()) {
if(skip_allocated_sections && section.has(ELF_SECTION_FLAGS::SHF_ALLOC)){
continue;
}
try {
if (section.type() == ELF_SECTION_TYPES::SHT_REL) {
this->parse_section_relocations<ELF_T, typename ELF_T::Elf_Rel>(section);
}
else if (section.type() == ELF_SECTION_TYPES::SHT_RELA) {
this->parse_section_relocations<ELF_T, typename ELF_T::Elf_Rela>(section);
}
} catch (const exception& e) {
LOG(WARNING) << "Unable to parse relocations from section '"
<< section.name() << "'"
<< " (" << e.what() << ")";
}
}

View File

@ -220,7 +220,24 @@ class TestTiny(TestCase):
self.assertEqual(segment.virtual_size, 0x5a)
self.assertEqual(int(segment.flags), lief.ELF.SEGMENT_FLAGS.R | lief.ELF.SEGMENT_FLAGS.X)
class TestAllRelocs(TestCase):
"""
Test binary generated with all relocation sections.
"""
def setUp(self):
self.logger = logging.getLogger(__name__)
self.bin_with_relocs = lief.parse(get_sample('ELF/ELF64_x86-64_hello-with-relocs.bin'))
def test_relocations(self):
relocations = self.bin_with_relocs.relocations
self.assertEqual(len(relocations), 37)
# check relocation from .rela.text
self.assertEqual(relocations[12].symbol.name,"main")
self.assertEqual(relocations[12].address,0x1064)
# check relocation from .rela.eh_frame
self.assertTrue(relocations[30].has_section)
self.assertEqual(relocations[30].address,0x2068)
if __name__ == '__main__':