diff --git a/tools/elf2rpl/main.cpp b/tools/elf2rpl/main.cpp index 610fe5c..c1024bc 100644 --- a/tools/elf2rpl/main.cpp +++ b/tools/elf2rpl/main.cpp @@ -928,7 +928,74 @@ write(ElfFile &file, const std::string &filename) auto shoff = align_up(sizeof(elf::Header), 64); auto dataOffset = align_up(shoff + outSections.size() * sizeof(elf::SectionHeader), 64); + // Add CRC and FileInfo sections first for (auto §ion : outSections) { + if (section->header.type != elf::SHT_RPL_CRCS && section->header.type != elf::SHT_RPL_FILEINFO) { + continue; + } + + if (section->header.type != elf::SHT_NOBITS) { + section->header.size = section->data.size(); + } + + if (!section->data.empty()) { + section->header.offset = dataOffset; + dataOffset = align_up(section->header.offset + section->data.size(), 64); + } else { + section->header.offset = 0; + } + } + + // Add data sections next + for (auto §ion : outSections) { + if(section->header.offset != -1) { + continue; + } + + if (section->header.addr < DataAddress || section->header.addr >= WiiuLoadAddress) { + continue; + } + + if (section->header.type != elf::SHT_NOBITS) { + section->header.size = section->data.size(); + } + + if (!section->data.empty()) { + section->header.offset = dataOffset; + dataOffset = align_up(section->header.offset + section->data.size(), 64); + } else { + section->header.offset = 0; + } + } + + // Add load sections next + for (auto §ion : outSections) { + if(section->header.offset != -1) { + continue; + } + + if (section->header.addr < WiiuLoadAddress) { + continue; + } + + if (section->header.type != elf::SHT_NOBITS) { + section->header.size = section->data.size(); + } + + if (!section->data.empty()) { + section->header.offset = dataOffset; + dataOffset = align_up(section->header.offset + section->data.size(), 64); + } else { + section->header.offset = 0; + } + } + + // Everything else + for (auto §ion : outSections) { + if(section->header.offset != -1) { + continue; + } + if (section->header.type != elf::SHT_NOBITS) { section->header.size = section->data.size(); } @@ -945,17 +1012,6 @@ write(ElfFile &file, const std::string &filename) std::ofstream out { filename, std::ofstream::binary }; std::vector padding; - auto padFileToOffset = [&out, &padding](auto offset) { - auto pos = static_cast(out.tellp()); - - if (pos < offset) { - padding.resize(offset - pos, 0); - out.write(padding.data(), padding.size()); - } else if (pos > offset) { - throw std::logic_error("You dumb person"); - } - }; - if (!out.is_open()) { std::cout << "Could not open " << filename << " for writing" << std::endl; return false; @@ -984,7 +1040,7 @@ write(ElfFile &file, const std::string &filename) out.write(reinterpret_cast(&header), sizeof(elf::Header)); // Write section headers - padFileToOffset(header.shoff); + out.seekp(header.shoff.value()); for (auto §ion : outSections) { out.write(reinterpret_cast(§ion->header), sizeof(elf::SectionHeader)); @@ -993,7 +1049,7 @@ write(ElfFile &file, const std::string &filename) // Write section data for (auto §ion : outSections) { if (!section->data.empty()) { - padFileToOffset(section->header.offset); + out.seekp(section->header.offset.value()); out.write(section->data.data(), section->data.size()); } }