summaryrefslogtreecommitdiffstats
path: root/src/core/loader/elf.h
diff options
context:
space:
mode:
authorbunnei <ericbunnie@gmail.com>2014-06-17 05:05:10 +0200
committerbunnei <ericbunnie@gmail.com>2014-06-17 05:43:32 +0200
commit1da361c7ab55e1dbe6709a738e228bfab5a5bb78 (patch)
tree487be34416c624106a01edf8b075866fb2cb9d33 /src/core/loader/elf.h
parentLoader: Added support for booting NCCH executables. (diff)
downloadyuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.gz
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.bz2
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.lz
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.xz
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.zst
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.zip
Diffstat (limited to '')
-rw-r--r--src/core/loader/elf.h (renamed from src/core/loader/elf_types.h)232
1 files changed, 141 insertions, 91 deletions
diff --git a/src/core/loader/elf_types.h b/src/core/loader/elf.h
index f1bf3db72..2e6b80982 100644
--- a/src/core/loader/elf_types.h
+++ b/src/core/loader/elf.h
@@ -1,34 +1,34 @@
-// Copyright 2013 Dolphin Emulator Project
+// Copyright 2013 Dolphin Emulator Project / Citra Emulator Project
// Licensed under GPLv2
// Refer to the license.txt file included.
#pragma once
+#include "common/common.h"
+
// ELF Header Constants
// File type
-enum ElfType
-{
- ET_NONE = 0,
- ET_REL = 1,
- ET_EXEC = 2,
- ET_DYN = 3,
- ET_CORE = 4,
- ET_LOPROC = 0xFF00,
- ET_HIPROC = 0xFFFF,
+enum ElfType {
+ ET_NONE = 0,
+ ET_REL = 1,
+ ET_EXEC = 2,
+ ET_DYN = 3,
+ ET_CORE = 4,
+ ET_LOPROC = 0xFF00,
+ ET_HIPROC = 0xFFFF,
};
// Machine/Architecture
-enum ElfMachine
-{
- EM_NONE = 0,
- EM_M32 = 1,
- EM_SPARC = 2,
- EM_386 = 3,
- EM_68K = 4,
- EM_88K = 5,
- EM_860 = 7,
- EM_MIPS = 8
+enum ElfMachine {
+ EM_NONE = 0,
+ EM_M32 = 1,
+ EM_SPARC = 2,
+ EM_386 = 3,
+ EM_68K = 4,
+ EM_88K = 5,
+ EM_860 = 7,
+ EM_MIPS = 8
};
// File version
@@ -62,8 +62,6 @@ enum ElfMachine
#define ELFDATA2LSB 1
#define ELFDATA2MSB 2
-
-
// Sections constants
// Section indexes
@@ -96,14 +94,13 @@ enum ElfMachine
// Custom section types
#define SHT_PSPREL 0x700000a0
-
// Section flags
enum ElfSectionFlags
{
- SHF_WRITE = 0x1,
- SHF_ALLOC = 0x2,
- SHF_EXECINSTR = 0x4,
- SHF_MASKPROC = 0xF0000000,
+ SHF_WRITE = 0x1,
+ SHF_ALLOC = 0x2,
+ SHF_EXECINSTR = 0x4,
+ SHF_MASKPROC = 0xF0000000,
};
// Symbol binding
@@ -188,63 +185,58 @@ typedef unsigned int Elf32_Off;
typedef signed int Elf32_Sword;
typedef unsigned int Elf32_Word;
-
// ELF file header
-struct Elf32_Ehdr
-{
- unsigned char e_ident[EI_NIDENT];
- Elf32_Half e_type;
- Elf32_Half e_machine;
- Elf32_Word e_version;
- Elf32_Addr e_entry;
- Elf32_Off e_phoff;
- Elf32_Off e_shoff;
- Elf32_Word e_flags;
- Elf32_Half e_ehsize;
- Elf32_Half e_phentsize;
- Elf32_Half e_phnum;
- Elf32_Half e_shentsize;
- Elf32_Half e_shnum;
- Elf32_Half e_shstrndx;
+struct Elf32_Ehdr {
+ unsigned char e_ident[EI_NIDENT];
+ Elf32_Half e_type;
+ Elf32_Half e_machine;
+ Elf32_Word e_version;
+ Elf32_Addr e_entry;
+ Elf32_Off e_phoff;
+ Elf32_Off e_shoff;
+ Elf32_Word e_flags;
+ Elf32_Half e_ehsize;
+ Elf32_Half e_phentsize;
+ Elf32_Half e_phnum;
+ Elf32_Half e_shentsize;
+ Elf32_Half e_shnum;
+ Elf32_Half e_shstrndx;
};
// Section header
-struct Elf32_Shdr
-{
- Elf32_Word sh_name;
- Elf32_Word sh_type;
- Elf32_Word sh_flags;
- Elf32_Addr sh_addr;
- Elf32_Off sh_offset;
- Elf32_Word sh_size;
- Elf32_Word sh_link;
- Elf32_Word sh_info;
- Elf32_Word sh_addralign;
- Elf32_Word sh_entsize;
+struct Elf32_Shdr {
+ Elf32_Word sh_name;
+ Elf32_Word sh_type;
+ Elf32_Word sh_flags;
+ Elf32_Addr sh_addr;
+ Elf32_Off sh_offset;
+ Elf32_Word sh_size;
+ Elf32_Word sh_link;
+ Elf32_Word sh_info;
+ Elf32_Word sh_addralign;
+ Elf32_Word sh_entsize;
};
// Segment header
-struct Elf32_Phdr
-{
- Elf32_Word p_type;
- Elf32_Off p_offset;
- Elf32_Addr p_vaddr;
- Elf32_Addr p_paddr;
- Elf32_Word p_filesz;
- Elf32_Word p_memsz;
- Elf32_Word p_flags;
- Elf32_Word p_align;
+struct Elf32_Phdr {
+ Elf32_Word p_type;
+ Elf32_Off p_offset;
+ Elf32_Addr p_vaddr;
+ Elf32_Addr p_paddr;
+ Elf32_Word p_filesz;
+ Elf32_Word p_memsz;
+ Elf32_Word p_flags;
+ Elf32_Word p_align;
};
// Symbol table entry
-struct Elf32_Sym
-{
- Elf32_Word st_name;
- Elf32_Addr st_value;
- Elf32_Word st_size;
- unsigned char st_info;
- unsigned char st_other;
- Elf32_Half st_shndx;
+struct Elf32_Sym {
+ Elf32_Word st_name;
+ Elf32_Addr st_value;
+ Elf32_Word st_size;
+ unsigned char st_info;
+ unsigned char st_other;
+ Elf32_Half st_shndx;
};
#define ELF32_ST_BIND(i) ((i)>>4)
@@ -252,30 +244,88 @@ struct Elf32_Sym
#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
// Relocation entries
-struct Elf32_Rel
-{
- Elf32_Addr r_offset;
- Elf32_Word r_info;
+struct Elf32_Rel {
+ Elf32_Addr r_offset;
+ Elf32_Word r_info;
};
-struct Elf32_Rela
-{
- Elf32_Addr r_offset;
- Elf32_Word r_info;
- Elf32_Sword r_addend;
+struct Elf32_Rela {
+ Elf32_Addr r_offset;
+ Elf32_Word r_info;
+ Elf32_Sword r_addend;
};
#define ELF32_R_SYM(i) ((i)>>8)
#define ELF32_R_TYPE(i) ((unsigned char)(i))
#define ELF32_R_INFO(s,t) (((s)<<8 )+(unsigned char)(t))
+struct Elf32_Dyn {
+ Elf32_Sword d_tag;
+ union {
+ Elf32_Word d_val;
+ Elf32_Addr d_ptr;
+ } d_un;
+};
-struct Elf32_Dyn
-{
- Elf32_Sword d_tag;
- union
- {
- Elf32_Word d_val;
- Elf32_Addr d_ptr;
- } d_un;
+enum KnownElfTypes {
+ KNOWNELF_PSP = 0,
+ KNOWNELF_DS = 1,
+ KNOWNELF_GBA = 2,
+ KNOWNELF_GC = 3,
+};
+
+typedef int SectionID;
+
+class ElfReader {
+private:
+ char *base;
+ u32 *base32;
+
+ Elf32_Ehdr *header;
+ Elf32_Phdr *segments;
+ Elf32_Shdr *sections;
+
+ u32 *sectionAddrs;
+ bool bRelocate;
+ u32 entryPoint;
+
+public:
+ ElfReader(void *ptr);
+ ~ElfReader() { }
+
+ u32 Read32(int off) const { return base32[off >> 2]; }
+
+ // Quick accessors
+ ElfType GetType() const { return (ElfType)(header->e_type); }
+ ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
+ u32 GetEntryPoint() const { return entryPoint; }
+ u32 GetFlags() const { return (u32)(header->e_flags); }
+ bool LoadInto(u32 vaddr);
+ bool LoadSymbols();
+
+ int GetNumSegments() const { return (int)(header->e_phnum); }
+ int GetNumSections() const { return (int)(header->e_shnum); }
+ const u8 *GetPtr(int offset) const { return (u8*)base + offset; }
+ const char *GetSectionName(int section) const;
+ const u8 *GetSectionDataPtr(int section) const {
+ if (section < 0 || section >= header->e_shnum)
+ return nullptr;
+ if (sections[section].sh_type != SHT_NOBITS)
+ return GetPtr(sections[section].sh_offset);
+ else
+ return nullptr;
+ }
+ bool IsCodeSection(int section) const {
+ return sections[section].sh_type == SHT_PROGBITS;
+ }
+ const u8 *GetSegmentPtr(int segment) {
+ return GetPtr(segments[segment].p_offset);
+ }
+ u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; }
+ int GetSectionSize(SectionID section) const { return sections[section].sh_size; }
+ SectionID GetSectionByName(const char *name, int firstSection = 0) const; //-1 for not found
+
+ bool DidRelocate() {
+ return bRelocate;
+ }
};