wut/tools/readrpl/main.cpp

272 lines
7.5 KiB
C++
Raw Normal View History

2018-05-25 00:32:08 +02:00
#include "elf.h"
#include "print.h"
#include "verify.h"
2018-05-23 13:35:24 +02:00
#include <excmd.h>
#include <fmt/format.h>
#include <fstream>
#include <iostream>
#include <vector>
#include <zlib.h>
2018-05-25 00:32:08 +02:00
uint32_t
getSectionIndex(const Rpl &rpl,
const Section &section)
2018-05-23 13:35:24 +02:00
{
2018-05-25 00:32:08 +02:00
return static_cast<uint32_t>(&section - &rpl.sections[0]);
2018-05-23 13:35:24 +02:00
}
2018-05-25 00:32:08 +02:00
bool
readSection(std::ifstream &fh,
Section &section,
size_t i)
2018-05-23 13:35:24 +02:00
{
// Read section header
2018-05-25 00:32:08 +02:00
fh.read(reinterpret_cast<char*>(&section.header), sizeof(elf::SectionHeader));
2018-05-23 13:35:24 +02:00
2018-05-25 00:32:08 +02:00
if (section.header.type == elf::SHT_NOBITS || !section.header.size) {
2018-05-23 13:35:24 +02:00
return true;
}
// Read section data
2018-05-25 00:32:08 +02:00
if (section.header.flags & elf::SHF_DEFLATED) {
2018-05-23 13:35:24 +02:00
auto stream = z_stream {};
auto ret = Z_OK;
// Read the original size
uint32_t size = 0;
2018-05-25 00:32:08 +02:00
fh.seekg(section.header.offset.value());
2018-05-23 13:35:24 +02:00
fh.read(reinterpret_cast<char *>(&size), sizeof(uint32_t));
size = byte_swap(size);
2018-05-25 00:32:08 +02:00
section.data.resize(size);
2018-05-23 13:35:24 +02:00
// Inflate
memset(&stream, 0, sizeof(stream));
stream.zalloc = Z_NULL;
stream.zfree = Z_NULL;
stream.opaque = Z_NULL;
ret = inflateInit(&stream);
if (ret != Z_OK) {
fmt::print("Couldn't decompress .rpx section because inflateInit returned {}\n", ret);
2018-05-25 00:32:08 +02:00
section.data.clear();
2018-05-23 13:35:24 +02:00
return false;
} else {
std::vector<char> temp;
2018-05-25 00:32:08 +02:00
temp.resize(section.header.size-sizeof(uint32_t));
2018-05-23 13:35:24 +02:00
fh.read(temp.data(), temp.size());
2018-05-25 00:32:08 +02:00
stream.avail_in = section.header.size;
stream.next_in = reinterpret_cast<Bytef *>(temp.data());
stream.avail_out = static_cast<uInt>(section.data.size());
stream.next_out = reinterpret_cast<Bytef *>(section.data.data());
2018-05-23 13:35:24 +02:00
ret = inflate(&stream, Z_FINISH);
if (ret != Z_OK && ret != Z_STREAM_END) {
fmt::print("Couldn't decompress .rpx section because inflate returned {}\n", ret);
2018-05-25 00:32:08 +02:00
section.data.clear();
2018-05-23 13:35:24 +02:00
return false;
}
inflateEnd(&stream);
}
} else {
2018-05-25 00:32:08 +02:00
section.data.resize(section.header.size);
fh.seekg(section.header.offset.value());
fh.read(section.data.data(), section.header.size);
2018-05-23 13:35:24 +02:00
}
return true;
}
int main(int argc, char **argv)
{
excmd::parser parser;
excmd::option_state options;
using excmd::description;
using excmd::value;
try {
parser.global_options()
.add_option("H,help",
description { "Show help." })
.add_option("a,all",
description { "Equivalent to: -h -S -s -r -i -x -c -f" })
.add_option("h,file-header",
description { "Display the ELF file header" })
.add_option("S,sections",
description { "Display the sections' header" })
.add_option("s,symbols",
description { "Display the symbol table" })
.add_option("r,relocs",
description { "Display the relocations" })
.add_option("i,imports",
description { "Display the RPL imports" })
.add_option("x,exports",
description { "Display the RPL exports" })
.add_option("c,crc",
description { "Display the RPL crc" })
.add_option("f,file-info",
description { "Display the RPL file info" });
parser.default_command()
.add_argument("path",
description { "Path to RPL file" },
value<std::string> {});
options = parser.parse(argc, argv);
} catch (excmd::exception ex) {
std::cout << "Error parsing options: " << ex.what() << std::endl;
return -1;
}
if (options.empty() || options.has("help") || !options.has("path")) {
fmt::print("{} <options> path\n", argv[0]);
fmt::print("{}\n", parser.format_help(argv[0]));
return 0;
}
auto all = options.has("all");
auto dumpElfHeader = all || options.has("file-header");
auto dumpSectionSummary = all || options.has("sections");
auto dumpSectionRela = all || options.has("relocs");
auto dumpSectionSymtab = all || options.has("symbols");
auto dumpSectionRplExports = all || options.has("exports");
auto dumpSectionRplImports = all || options.has("imports");
auto dumpSectionRplCrcs = all || options.has("crc");
auto dumpSectionRplFileinfo = all || options.has("file-info");
auto path = options.get<std::string>("path");
// If no options are set (other than "path"), let's default to a summary
if (options.set_options.size() == 1) {
dumpElfHeader = true;
dumpSectionSummary = true;
dumpSectionRplFileinfo = true;
}
// Read file
std::ifstream fh { path, std::ifstream::binary };
if (!fh.is_open()) {
fmt::print("Could not open {} for reading\n", path);
return -1;
}
2018-05-25 00:32:08 +02:00
Rpl rpl;
fh.read(reinterpret_cast<char*>(&rpl.header), sizeof(elf::Header));
2018-05-23 13:35:24 +02:00
2018-05-25 00:32:08 +02:00
if (rpl.header.magic != elf::HeaderMagic) {
2018-05-23 13:35:24 +02:00
fmt::print("Invalid ELF magic header\n");
return -1;
}
// Read sections
2018-05-25 00:32:08 +02:00
for (auto i = 0u; i < rpl.header.shnum; ++i) {
2018-05-23 13:35:24 +02:00
Section section;
2018-05-25 00:32:08 +02:00
fh.seekg(rpl.header.shoff + rpl.header.shentsize * i);
2018-05-23 13:35:24 +02:00
2018-05-25 00:32:08 +02:00
if (!readSection(fh, section, i)) {
2018-05-23 13:35:24 +02:00
fmt::print("Error reading section {}", i);
return -1;
}
2018-05-25 00:32:08 +02:00
rpl.sections.push_back(section);
2018-05-23 13:35:24 +02:00
}
2018-05-25 00:32:08 +02:00
// Set section names
auto shStrTab = reinterpret_cast<const char *>(rpl.sections[rpl.header.shstrndx].data.data());
for (auto &section : rpl.sections) {
section.name = shStrTab + section.header.name;
}
// Verify rpl format
verifyFile(rpl);
verifyCrcs(rpl);
verifyFileBounds(rpl);
verifyRelocationTypes(rpl);
verifySectionAlignment(rpl);
verifySectionOrder(rpl);
2018-05-23 13:35:24 +02:00
// Format shit
if (dumpElfHeader) {
2018-05-25 00:32:08 +02:00
printHeader(rpl);
2018-05-23 13:35:24 +02:00
}
if (dumpSectionSummary) {
2018-05-25 00:32:08 +02:00
printSectionSummary(rpl);
2018-05-23 13:35:24 +02:00
}
// Print section data
2018-05-25 00:32:08 +02:00
for (auto i = 0u; i < rpl.sections.size(); ++i) {
auto &section = rpl.sections[i];
2018-05-23 13:35:24 +02:00
auto printSectionHeader = [&](){
fmt::print(
"Section {}: {}, {}, {} bytes\n",
2018-05-25 00:32:08 +02:00
i, formatSHT(section.header.type), section.name, section.data.size());
2018-05-23 13:35:24 +02:00
};
switch (section.header.type) {
case elf::SHT_NULL:
case elf::SHT_NOBITS:
// Print nothing
break;
case elf::SHT_RELA:
if (!dumpSectionRela) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printRela(rpl, section);
2018-05-23 13:35:24 +02:00
break;
case elf::SHT_SYMTAB:
if (!dumpSectionSymtab) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printSymTab(rpl, section);
2018-05-23 13:35:24 +02:00
break;
case elf::SHT_STRTAB:
break;
case elf::SHT_PROGBITS:
break;
case elf::SHT_RPL_EXPORTS:
if (!dumpSectionRplExports) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printRplExports(rpl, section);
2018-05-23 13:35:24 +02:00
break;
case elf::SHT_RPL_IMPORTS:
if (!dumpSectionRplImports) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printRplImports(rpl, section);
2018-05-23 13:35:24 +02:00
break;
case elf::SHT_RPL_CRCS:
if (!dumpSectionRplCrcs) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printRplCrcs(rpl, section);
2018-05-23 13:35:24 +02:00
break;
case elf::SHT_RPL_FILEINFO:
if (!dumpSectionRplFileinfo) {
continue;
}
printSectionHeader();
2018-05-25 00:32:08 +02:00
printFileInfo(rpl, section);
2018-05-23 13:35:24 +02:00
break;
}
}
return 0;
}