elf2rpl: Remove reorderSectionIndex.

Not needed.
This commit is contained in:
James Benton 2018-05-30 22:43:41 +01:00
parent 0ef7dcfa32
commit 6a116a8e41

View File

@ -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<std::size_t> 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<std::unique_ptr<ElfFile::Section>> 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<uint16_t> mapOldToNew;
mapOldToNew.resize(file.sections.size());
for (auto i = 0u; i < sectionMap.size(); ++i) {
mapOldToNew[sectionMap[i]] = static_cast<uint16_t>(i);
}
// Map file header.shstrndx
file.header.shstrndx = mapOldToNew[file.header.shstrndx];
// Map section header.link
for (auto &section : file.sections) {
section->header.link = mapOldToNew[section->header.link];
}
// Map relocation sections header.info
for (auto &section : file.sections) {
if (section->header.type != elf::SHT_RELA) {
continue;
}
section->header.info = mapOldToNew[section->header.info];
}
// Map symbol.shndx
for (auto &section : file.sections) {
if (section->header.type != elf::SHT_SYMTAB) {
continue;
}
auto symbols = reinterpret_cast<elf::Symbol *>(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;