From c98c7e39e4e99324260dfdd10e7a5d94a5e8e3a8 Mon Sep 17 00:00:00 2001 From: James Benton Date: Wed, 30 May 2018 17:29:10 +0100 Subject: [PATCH] elf2rpl: Check relocation section index before performing relocations. --- tools/elf2rpl/main.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/tools/elf2rpl/main.cpp b/tools/elf2rpl/main.cpp index ef7c8235..7d4a0626 100644 --- a/tools/elf2rpl/main.cpp +++ b/tools/elf2rpl/main.cpp @@ -636,6 +636,7 @@ fixRoDataFlags(ElfFile &file) static bool relocateSection(ElfFile &file, ElfFile::Section §ion, + uint32_t sectionIndex, uint32_t newSectionAddress) { auto sectionSize = section.data.size() ? section.data.size() : static_cast(section.header.size); @@ -669,7 +670,8 @@ relocateSection(ElfFile &file, // Relocate relocations pointing into this section for (auto &relaSection : file.sections) { - if (relaSection->header.type != elf::SectionType::SHT_RELA) { + if (relaSection->header.type != elf::SectionType::SHT_RELA || + relaSection->header.info != sectionIndex) { continue; } @@ -703,27 +705,30 @@ fixLoaderVirtualAddresses(ElfFile &file) { auto addr = LoadBaseAddress; - for (auto §ion : file.sections) { + for (auto i = 0u; i < file.sections.size(); ++i) { + auto §ion = file.sections[i]; if (section->header.type == elf::SHT_RPL_EXPORTS) { - relocateSection(file, *section, + relocateSection(file, *section, i, align_up(addr, section->header.addralign)); addr += section->data.size(); } } - for (auto §ion : file.sections) { + for (auto i = 0u; i < file.sections.size(); ++i) { + auto §ion = file.sections[i]; if (section->header.type == elf::SHT_SYMTAB || section->header.type == elf::SHT_STRTAB) { - relocateSection(file, *section, + relocateSection(file, *section, i, align_up(addr, section->header.addralign)); section->header.flags |= elf::SHF_ALLOC; addr += section->data.size(); } } - for (auto §ion : file.sections) { + for (auto i = 0u; i < file.sections.size(); ++i) { + auto §ion = file.sections[i]; if (section->header.type == elf::SHT_RPL_IMPORTS) { - relocateSection(file, *section, + relocateSection(file, *section, i, align_up(addr, section->header.addralign)); addr += section->data.size(); }