diff --git a/tools/elf2rpl/main.cpp b/tools/elf2rpl/main.cpp index c211fef0..8acd2072 100644 --- a/tools/elf2rpl/main.cpp +++ b/tools/elf2rpl/main.cpp @@ -150,145 +150,6 @@ readElf(ElfFile &file, const std::string &filename) } -/** - * Reorder sections index. - * - * Expected order: - * NULL section - * > .syscall > .text - * > .fexports - * > .rodata > .data > .module_id > .bss - * > .rela.fexports > .rela.text > .rela.rodata > .rela.data - * > {.fimport, .dimport } - * > .symtab > .strtab > .shstrtab - */ -static bool -reorderSectionIndex(ElfFile &file) -{ - // Create a map of new index -> old index - std::vector sectionMap; - sectionMap.push_back(0); - - // Code sections - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_PROGBITS && - (file.sections[i]->header.flags & elf::SHF_EXECINSTR)) { - sectionMap.push_back(i); - } - } - - // RPL exports - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_RPL_EXPORTS) { - sectionMap.push_back(i); - } - } - - // Read only data - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_PROGBITS && - !(file.sections[i]->header.flags & elf::SHF_EXECINSTR) && - !(file.sections[i]->header.flags & elf::SHF_WRITE)) { - sectionMap.push_back(i); - } - } - - // Writable data - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_PROGBITS && - !(file.sections[i]->header.flags & elf::SHF_EXECINSTR) && - (file.sections[i]->header.flags & elf::SHF_WRITE)) { - sectionMap.push_back(i); - } - } - - // BSS - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_NOBITS) { - sectionMap.push_back(i); - } - } - - // Relocations - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_REL || - file.sections[i]->header.type == elf::SHT_RELA) { - sectionMap.push_back(i); - } - } - - // RPL imports - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_RPL_IMPORTS) { - sectionMap.push_back(i); - } - } - - // Symtab and strtab - for (auto i = 0u; i < file.sections.size(); ++i) { - if (file.sections[i]->header.type == elf::SHT_SYMTAB || - file.sections[i]->header.type == elf::SHT_STRTAB) { - sectionMap.push_back(i); - } - } - - if (sectionMap.size() != file.sections.size()) { - fmt::print("Invalid section in elf file\n"); - return false; - } - - // Apply the new ordering - std::vector> newSections; - for (auto idx : sectionMap) { - newSections.emplace_back(std::move(file.sections[idx])); - } - - file.sections = std::move(newSections); - - // Now generate a reverse map, old index -> new index - std::vector mapOldToNew; - mapOldToNew.resize(file.sections.size()); - for (auto i = 0u; i < sectionMap.size(); ++i) { - mapOldToNew[sectionMap[i]] = static_cast(i); - } - - // Map file header.shstrndx - file.header.shstrndx = mapOldToNew[file.header.shstrndx]; - - // Map section header.link - for (auto §ion : file.sections) { - section->header.link = mapOldToNew[section->header.link]; - } - - // Map relocation sections header.info - for (auto §ion : file.sections) { - if (section->header.type != elf::SHT_RELA) { - continue; - } - - section->header.info = mapOldToNew[section->header.info]; - } - - // Map symbol.shndx - for (auto §ion : file.sections) { - if (section->header.type != elf::SHT_SYMTAB) { - continue; - } - - auto symbols = reinterpret_cast(section->data.data()); - auto numSymbols = section->data.size() / sizeof(elf::Symbol); - for (auto i = 0u; i < numSymbols; ++i) { - auto shndx = symbols[i].shndx; - if (shndx < elf::SHN_LORESERVE) { - symbols[i].shndx = mapOldToNew[shndx]; - } - } - } - - return true; -} - - /** * Generate SHT_RPL_FILEINFO section. */ @@ -908,11 +769,6 @@ int main(int argc, char **argv) return -1; } - if (!reorderSectionIndex(elf)) { - fmt::print("ERROR: reorderSectionIndex failed.\n"); - return -1; - } - if (!fixRelocations(elf)) { fmt::print("ERROR: fixRelocations failed.\n"); return -1;