diff options
Diffstat (limited to 'src/core/elf/elf_reader.h')
| -rw-r--r-- | src/core/elf/elf_reader.h | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/core/elf/elf_reader.h b/src/core/elf/elf_reader.h new file mode 100644 index 000000000..9393a589d --- /dev/null +++ b/src/core/elf/elf_reader.h | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | // Copyright 2013 Dolphin Emulator Project | ||
| 2 | // Licensed under GPLv2 | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include "elf/elf_types.h" | ||
| 8 | |||
| 9 | enum KnownElfTypes | ||
| 10 | { | ||
| 11 | KNOWNELF_PSP = 0, | ||
| 12 | KNOWNELF_DS = 1, | ||
| 13 | KNOWNELF_GBA = 2, | ||
| 14 | KNOWNELF_GC = 3, | ||
| 15 | }; | ||
| 16 | |||
| 17 | typedef int SectionID; | ||
| 18 | |||
| 19 | class ElfReader | ||
| 20 | { | ||
| 21 | private: | ||
| 22 | char *base; | ||
| 23 | u32 *base32; | ||
| 24 | |||
| 25 | Elf32_Ehdr *header; | ||
| 26 | Elf32_Phdr *segments; | ||
| 27 | Elf32_Shdr *sections; | ||
| 28 | |||
| 29 | u32 *sectionAddrs; | ||
| 30 | bool bRelocate; | ||
| 31 | u32 entryPoint; | ||
| 32 | |||
| 33 | public: | ||
| 34 | ElfReader(void *ptr); | ||
| 35 | ~ElfReader() { } | ||
| 36 | |||
| 37 | u32 Read32(int off) const { return base32[off>>2]; } | ||
| 38 | |||
| 39 | // Quick accessors | ||
| 40 | ElfType GetType() const { return (ElfType)(header->e_type); } | ||
| 41 | ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } | ||
| 42 | u32 GetEntryPoint() const { return entryPoint; } | ||
| 43 | u32 GetFlags() const { return (u32)(header->e_flags); } | ||
| 44 | bool LoadInto(u32 vaddr); | ||
| 45 | bool LoadSymbols(); | ||
| 46 | |||
| 47 | int GetNumSegments() const { return (int)(header->e_phnum); } | ||
| 48 | int GetNumSections() const { return (int)(header->e_shnum); } | ||
| 49 | const u8 *GetPtr(int offset) const { return (u8*)base + offset; } | ||
| 50 | const char *GetSectionName(int section) const; | ||
| 51 | const u8 *GetSectionDataPtr(int section) const | ||
| 52 | { | ||
| 53 | if (section < 0 || section >= header->e_shnum) | ||
| 54 | return nullptr; | ||
| 55 | if (sections[section].sh_type != SHT_NOBITS) | ||
| 56 | return GetPtr(sections[section].sh_offset); | ||
| 57 | else | ||
| 58 | return nullptr; | ||
| 59 | } | ||
| 60 | bool IsCodeSection(int section) const | ||
| 61 | { | ||
| 62 | return sections[section].sh_type == SHT_PROGBITS; | ||
| 63 | } | ||
| 64 | const u8 *GetSegmentPtr(int segment) | ||
| 65 | { | ||
| 66 | return GetPtr(segments[segment].p_offset); | ||
| 67 | } | ||
| 68 | u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; } | ||
| 69 | int GetSectionSize(SectionID section) const { return sections[section].sh_size; } | ||
| 70 | SectionID GetSectionByName(const char *name, int firstSection = 0) const; //-1 for not found | ||
| 71 | |||
| 72 | bool DidRelocate() { | ||
| 73 | return bRelocate; | ||
| 74 | } | ||
| 75 | }; | ||