Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
DirtyHairy committed Jul 4, 2024
1 parent be80e6b commit 192870d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion src/emucore/CartELF.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ CartridgeELF::CartridgeELF(const ByteBuffer& image, size_t size, string_view md5
try {
elfParser.parse(image.get(), size);
} catch (ElfParser::EInvalidElf& e) {
throw runtime_error("failed to initialize ELF: " + e.getReason());
throw runtime_error("failed to initialize ELF: " + string(e.what()));
}

myImage = make_unique<uInt8[]>(size);
Expand Down
28 changes: 12 additions & 16 deletions src/emucore/elf/ElfParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ namespace {
constexpr uInt8 ELF_VERSION = 1;
} // namespace

ElfParser::EInvalidElf::EInvalidElf(const string &reason) : reason(reason) {}

const string &ElfParser::EInvalidElf::getReason() const { return reason; }

void ElfParser::parse(const uInt8 *elfData, size_t size) {
data = elfData;
this->size = size;

sections.resize(0);

try {
if (read32(0x00) != ELF_MAGIC) throw EInvalidElf("bad magic");
if (read8(0x04) != ELF_CLASS_32) throw EInvalidElf("not 32bit ELF");
if (read8(0x06) != ELF_VERSION) throw EInvalidElf("invalid ELF version");
if (read32(0x00) != ELF_MAGIC) EInvalidElf::raise("bad magic");
if (read8(0x04) != ELF_CLASS_32) EInvalidElf::raise("not 32bit ELF");
if (read8(0x06) != ELF_VERSION) EInvalidElf::raise("invalid ELF version");

header.endianess = read8(0x05);
bigEndian = header.endianess == ENDIAN_BIG_ENDIAN;

if (read32(0x14) != ELF_VERSION) throw EInvalidElf("inconsistent ELF version");
if (read32(0x14) != ELF_VERSION) EInvalidElf::raise("inconsistent ELF version");

header.type = read16(0x10);
header.arch = read16(0x12);
Expand All @@ -36,7 +32,7 @@ void ElfParser::parse(const uInt8 *elfData, size_t size) {
header.shNum = read16(0x30);
header.shstrIndex = read16(0x32);

if (header.shstrIndex >= header.shNum) throw EInvalidElf(".shstrtab out of range");
if (header.shstrIndex >= header.shNum) EInvalidElf::raise(".shstrtab out of range");

sections.reserve(header.shNum);

Expand All @@ -46,14 +42,14 @@ void ElfParser::parse(const uInt8 *elfData, size_t size) {

const Section &shrstrtab(sections[header.shstrIndex]);

if (shrstrtab.type != SHT_STRTAB) throw new EInvalidElf(".shstrtab has wrong type");
if (shrstrtab.type != SHT_STRTAB) EInvalidElf::raise(".shstrtab has wrong type");


for (Section &section : sections)
section.name = getName(shrstrtab, section.nameOffset);

} catch (const EInvalidElf &e) {
throw EInvalidElf("failed to parse ELF: " + e.getReason());
EInvalidElf::raise("failed to parse ELF: " + string(e.what()));
}
}

Expand All @@ -76,7 +72,7 @@ ElfParser::getSection(const string &name) const {

uInt8 ElfParser::read8(uInt32 offset) {
if (offset >= size)
throw EInvalidElf("reference beyond bounds");
EInvalidElf::raise("reference beyond bounds");

return data[offset];
}
Expand Down Expand Up @@ -106,23 +102,23 @@ ElfParser::Section ElfParser::readSection(uInt32 offset) {
section.align = read32(offset + 0x20);

if (section.offset + section.size >= size)
throw EInvalidElf("section exceeds bounds");
EInvalidElf::raise("section exceeds bounds");
} catch (const EInvalidElf &e) {
throw EInvalidElf("failed to parse section: " + e.getReason());
EInvalidElf::raise("failed to parse section: " + string(e.what()));
}

return section;
}

const char* ElfParser::getName(const Section& section, uInt32 offset)
{
if (offset >= section.size) throw EInvalidElf("name out of bounds");
if (offset >= section.size) EInvalidElf::raise("name out of bounds");
const uInt32 imageOffset = offset + section.offset;

const char *name = reinterpret_cast<const char *>(data + imageOffset);

if (data[imageOffset + strnlen(name, section.size - offset)] != '\0')
throw new EInvalidElf("unterminated section name");
EInvalidElf::raise("unterminated section name");

return name;
}
12 changes: 8 additions & 4 deletions src/emucore/elf/ElfParser.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,21 @@

class ElfParser {
public:
class EInvalidElf {
class EInvalidElf : public std::exception {
friend ElfParser;

public:
const std::string &getReason() const;
const char* what() const noexcept override { return myReason.c_str(); }

[[noreturn]] static void raise(string_view message) {
throw EInvalidElf(message);
}

private:
EInvalidElf(const std::string &reason);
explicit EInvalidElf(string_view reason) : myReason(reason) {}

private:
std::string reason;
const string myReason;
};

struct Header {
Expand Down

0 comments on commit 192870d

Please sign in to comment.