summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2014-03-25 10:47:12 -0400
committerGravatar bunnei2014-03-25 10:47:12 -0400
commit872d9ae87f8cd97d9f7993bc0f27a7b88f7a2f39 (patch)
tree8cecc65defc35206a29c8e71b3e11897ea7dee57 /src
parentrenamed FILETYPE_CTR_DIRECTORY to FILETYPE_DIRECTORY_CXI (diff)
downloadyuzu-872d9ae87f8cd97d9f7993bc0f27a7b88f7a2f39.tar.gz
yuzu-872d9ae87f8cd97d9f7993bc0f27a7b88f7a2f39.tar.xz
yuzu-872d9ae87f8cd97d9f7993bc0f27a7b88f7a2f39.zip
added Dolphin's ELF reader to the project
Diffstat (limited to 'src')
-rw-r--r--src/core/src/elf/elf_reader.cpp238
-rw-r--r--src/core/src/elf/elf_reader.h75
-rw-r--r--src/core/src/elf/elf_types.h281
3 files changed, 594 insertions, 0 deletions
diff --git a/src/core/src/elf/elf_reader.cpp b/src/core/src/elf/elf_reader.cpp
new file mode 100644
index 000000000..21d8aea67
--- /dev/null
+++ b/src/core/src/elf/elf_reader.cpp
@@ -0,0 +1,238 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#include <string>
6
7#include "common.h"
8#include "mem_map.h"
9
10#include "elf/elf_reader.h"
11//#include "Core/Debugger/Debugger_SymbolMap.h"
12//#include "Core/HW/Memmap.h"
13//#include "Core/PowerPC/PPCSymbolDB.h"
14
15//void bswap(Elf32_Word &w) {w = Common::swap32(w);}
16//void bswap(Elf32_Half &w) {w = Common::swap16(w);}
17
18#define bswap(w) w // Dirty bswap disable for now... 3DS is little endian, anyway
19
20static void byteswapHeader(Elf32_Ehdr &ELF_H)
21{
22 bswap(ELF_H.e_type);
23 bswap(ELF_H.e_machine);
24 bswap(ELF_H.e_ehsize);
25 bswap(ELF_H.e_phentsize);
26 bswap(ELF_H.e_phnum);
27 bswap(ELF_H.e_shentsize);
28 bswap(ELF_H.e_shnum);
29 bswap(ELF_H.e_shstrndx);
30 bswap(ELF_H.e_version);
31 bswap(ELF_H.e_entry);
32 bswap(ELF_H.e_phoff);
33 bswap(ELF_H.e_shoff);
34 bswap(ELF_H.e_flags);
35}
36
37static void byteswapSegment(Elf32_Phdr &sec)
38{
39 bswap(sec.p_align);
40 bswap(sec.p_filesz);
41 bswap(sec.p_flags);
42 bswap(sec.p_memsz);
43 bswap(sec.p_offset);
44 bswap(sec.p_paddr);
45 bswap(sec.p_vaddr);
46 bswap(sec.p_type);
47}
48
49static void byteswapSection(Elf32_Shdr &sec)
50{
51 bswap(sec.sh_addr);
52 bswap(sec.sh_addralign);
53 bswap(sec.sh_entsize);
54 bswap(sec.sh_flags);
55 bswap(sec.sh_info);
56 bswap(sec.sh_link);
57 bswap(sec.sh_name);
58 bswap(sec.sh_offset);
59 bswap(sec.sh_size);
60 bswap(sec.sh_type);
61}
62
63ElfReader::ElfReader(void *ptr)
64{
65 base = (char*)ptr;
66 base32 = (u32 *)ptr;
67 header = (Elf32_Ehdr*)ptr;
68 byteswapHeader(*header);
69
70 segments = (Elf32_Phdr *)(base + header->e_phoff);
71 sections = (Elf32_Shdr *)(base + header->e_shoff);
72
73 for (int i = 0; i < GetNumSegments(); i++)
74 {
75 byteswapSegment(segments[i]);
76 }
77
78 for (int i = 0; i < GetNumSections(); i++)
79 {
80 byteswapSection(sections[i]);
81 }
82 entryPoint = header->e_entry;
83}
84
85const char *ElfReader::GetSectionName(int section) const
86{
87 if (sections[section].sh_type == SHT_NULL)
88 return nullptr;
89
90 int nameOffset = sections[section].sh_name;
91 char *ptr = (char*)GetSectionDataPtr(header->e_shstrndx);
92
93 if (ptr)
94 return ptr + nameOffset;
95 else
96 return nullptr;
97}
98
99bool ElfReader::LoadInto(u32 vaddr)
100{
101 DEBUG_LOG(MASTER_LOG,"String section: %i", header->e_shstrndx);
102
103// sectionOffsets = new u32[GetNumSections()];
104// sectionAddrs = new u32[GetNumSections()];
105
106 // Should we relocate?
107 bRelocate = (header->e_type != ET_EXEC);
108
109 if (bRelocate)
110 {
111 DEBUG_LOG(MASTER_LOG,"Relocatable module");
112 entryPoint += vaddr;
113 }
114 else
115 {
116 DEBUG_LOG(MASTER_LOG,"Prerelocated executable");
117 }
118
119 INFO_LOG(MASTER_LOG,"%i segments:", header->e_phnum);
120
121 // First pass : Get the bits into RAM
122 u32 segmentVAddr[32];
123
124 u32 baseAddress = bRelocate?vaddr:0;
125 for (int i = 0; i < header->e_phnum; i++)
126 {
127 Elf32_Phdr *p = segments + i;
128
129 INFO_LOG(MASTER_LOG, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz);
130
131 if (p->p_type == PT_LOAD)
132 {
133 segmentVAddr[i] = baseAddress + p->p_vaddr;
134 u32 writeAddr = segmentVAddr[i];
135
136 const u8 *src = GetSegmentPtr(i);
137 u8 *dst = Memory::GetPointer(writeAddr);
138 u32 srcSize = p->p_filesz;
139 u32 dstSize = p->p_memsz;
140 u32 *s = (u32*)src;
141 u32 *d = (u32*)dst;
142 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
143 {
144 *d++ = /*_byteswap_ulong*/(*s++);
145 }
146 if (srcSize < dstSize)
147 {
148 //memset(dst + srcSize, 0, dstSize-srcSize); //zero out bss
149 }
150 INFO_LOG(MASTER_LOG,"Loadable Segment Copied to %08x, size %08x", writeAddr, p->p_memsz);
151 }
152 }
153
154 /*
155 LOG(MASTER_LOG,"%i sections:", header->e_shnum);
156
157 for (int i=0; i<GetNumSections(); i++)
158 {
159 Elf32_Shdr *s = &sections[i];
160 const char *name = GetSectionName(i);
161
162 u32 writeAddr = s->sh_addr + baseAddress;
163 sectionOffsets[i] = writeAddr - vaddr;
164 sectionAddrs[i] = writeAddr;
165
166 if (s->sh_flags & SHF_ALLOC)
167 {
168 LOG(MASTER_LOG,"Data Section found: %s Sitting at %08x, size %08x", name, writeAddr, s->sh_size);
169
170 }
171 else
172 {
173 LOG(MASTER_LOG,"NonData Section found: %s Ignoring (size=%08x) (flags=%08x)", name, s->sh_size, s->sh_flags);
174 }
175 }
176*/
177 INFO_LOG(MASTER_LOG,"Done loading.");
178 return true;
179}
180
181SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const
182{
183 for (int i = firstSection; i < header->e_shnum; i++)
184 {
185 const char *secname = GetSectionName(i);
186
187 if (secname != nullptr && strcmp(name, secname) == 0)
188 return i;
189 }
190 return -1;
191}
192
193/* TODO(bunnei): The following is verbatim from Dolphin - needs to be updated for this project:
194
195bool ElfReader::LoadSymbols()
196{
197 bool hasSymbols = false;
198 SectionID sec = GetSectionByName(".symtab");
199 if (sec != -1)
200 {
201 int stringSection = sections[sec].sh_link;
202 const char *stringBase = (const char *)GetSectionDataPtr(stringSection);
203
204 //We have a symbol table!
205 Elf32_Sym *symtab = (Elf32_Sym *)(GetSectionDataPtr(sec));
206 int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym);
207 for (int sym = 0; sym < numSymbols; sym++)
208 {
209 int size = Common::swap32(symtab[sym].st_size);
210 if (size == 0)
211 continue;
212
213 // int bind = symtab[sym].st_info >> 4;
214 int type = symtab[sym].st_info & 0xF;
215 int sectionIndex = Common::swap16(symtab[sym].st_shndx);
216 int value = Common::swap32(symtab[sym].st_value);
217 const char *name = stringBase + Common::swap32(symtab[sym].st_name);
218 if (bRelocate)
219 value += sectionAddrs[sectionIndex];
220
221 int symtype = Symbol::SYMBOL_DATA;
222 switch (type)
223 {
224 case STT_OBJECT:
225 symtype = Symbol::SYMBOL_DATA; break;
226 case STT_FUNC:
227 symtype = Symbol::SYMBOL_FUNCTION; break;
228 default:
229 continue;
230 }
231 g_symbolDB.AddKnownSymbol(value, size, name, symtype);
232 hasSymbols = true;
233 }
234 }
235 g_symbolDB.Index();
236 return hasSymbols;
237}
238*/
diff --git a/src/core/src/elf/elf_reader.h b/src/core/src/elf/elf_reader.h
new file mode 100644
index 000000000..9393a589d
--- /dev/null
+++ b/src/core/src/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
9enum KnownElfTypes
10{
11 KNOWNELF_PSP = 0,
12 KNOWNELF_DS = 1,
13 KNOWNELF_GBA = 2,
14 KNOWNELF_GC = 3,
15};
16
17typedef int SectionID;
18
19class ElfReader
20{
21private:
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
33public:
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};
diff --git a/src/core/src/elf/elf_types.h b/src/core/src/elf/elf_types.h
new file mode 100644
index 000000000..f1bf3db72
--- /dev/null
+++ b/src/core/src/elf/elf_types.h
@@ -0,0 +1,281 @@
1// Copyright 2013 Dolphin Emulator Project
2// Licensed under GPLv2
3// Refer to the license.txt file included.
4
5#pragma once
6
7// ELF Header Constants
8
9// File type
10enum ElfType
11{
12 ET_NONE = 0,
13 ET_REL = 1,
14 ET_EXEC = 2,
15 ET_DYN = 3,
16 ET_CORE = 4,
17 ET_LOPROC = 0xFF00,
18 ET_HIPROC = 0xFFFF,
19};
20
21// Machine/Architecture
22enum ElfMachine
23{
24 EM_NONE = 0,
25 EM_M32 = 1,
26 EM_SPARC = 2,
27 EM_386 = 3,
28 EM_68K = 4,
29 EM_88K = 5,
30 EM_860 = 7,
31 EM_MIPS = 8
32};
33
34// File version
35#define EV_NONE 0
36#define EV_CURRENT 1
37
38// Identification index
39#define EI_MAG0 0
40#define EI_MAG1 1
41#define EI_MAG2 2
42#define EI_MAG3 3
43#define EI_CLASS 4
44#define EI_DATA 5
45#define EI_VERSION 6
46#define EI_PAD 7
47#define EI_NIDENT 16
48
49// Magic number
50#define ELFMAG0 0x7F
51#define ELFMAG1 'E'
52#define ELFMAG2 'L'
53#define ELFMAG3 'F'
54
55// File class
56#define ELFCLASSNONE 0
57#define ELFCLASS32 1
58#define ELFCLASS64 2
59
60// Encoding
61#define ELFDATANONE 0
62#define ELFDATA2LSB 1
63#define ELFDATA2MSB 2
64
65
66
67// Sections constants
68
69// Section indexes
70#define SHN_UNDEF 0
71#define SHN_LORESERVE 0xFF00
72#define SHN_LOPROC 0xFF00
73#define SHN_HIPROC 0xFF1F
74#define SHN_ABS 0xFFF1
75#define SHN_COMMON 0xFFF2
76#define SHN_HIRESERVE 0xFFFF
77
78// Section types
79#define SHT_NULL 0
80#define SHT_PROGBITS 1
81#define SHT_SYMTAB 2
82#define SHT_STRTAB 3
83#define SHT_RELA 4
84#define SHT_HASH 5
85#define SHT_DYNAMIC 6
86#define SHT_NOTE 7
87#define SHT_NOBITS 8
88#define SHT_REL 9
89#define SHT_SHLIB 10
90#define SHT_DYNSYM 11
91#define SHT_LOPROC 0x70000000
92#define SHT_HIPROC 0x7FFFFFFF
93#define SHT_LOUSER 0x80000000
94#define SHT_HIUSER 0xFFFFFFFF
95
96// Custom section types
97#define SHT_PSPREL 0x700000a0
98
99
100// Section flags
101enum ElfSectionFlags
102{
103 SHF_WRITE = 0x1,
104 SHF_ALLOC = 0x2,
105 SHF_EXECINSTR = 0x4,
106 SHF_MASKPROC = 0xF0000000,
107};
108
109// Symbol binding
110#define STB_LOCAL 0
111#define STB_GLOBAL 1
112#define STB_WEAK 2
113#define STB_LOPROC 13
114#define STB_HIPROC 15
115
116// Symbol types
117#define STT_NOTYPE 0
118#define STT_OBJECT 1
119#define STT_FUNC 2
120#define STT_SECTION 3
121#define STT_FILE 4
122#define STT_LOPROC 13
123#define STT_HIPROC 15
124
125// Undefined name
126#define STN_UNDEF 0
127
128// Relocation types
129#define R_386_NONE 0
130#define R_386_32 1
131#define R_386_PC32 2
132#define R_386_GOT32 3
133#define R_386_PLT32 4
134#define R_386_COPY 5
135#define R_386_GLOB_DAT 6
136#define R_386_JMP_SLOT 7
137#define R_386_RELATIVE 8
138#define R_386_GOTOFF 9
139#define R_386_GOTPC 10
140
141// Segment types
142#define PT_NULL 0
143#define PT_LOAD 1
144#define PT_DYNAMIC 2
145#define PT_INTERP 3
146#define PT_NOTE 4
147#define PT_SHLIB 5
148#define PT_PHDR 6
149#define PT_LOPROC 0x70000000
150#define PT_HIPROC 0x7FFFFFFF
151
152// Segment flags
153#define PF_X 1
154#define PF_W 2
155#define PF_R 4
156
157// Dynamic Array Tags
158#define DT_NULL 0
159#define DT_NEEDED 1
160#define DT_PLTRELSZ 2
161#define DT_PLTGOT 3
162#define DT_HASH 4
163#define DT_STRTAB 5
164#define DT_SYMTAB 6
165#define DT_RELA 7
166#define DT_RELASZ 8
167#define DT_RELAENT 9
168#define DT_STRSZ 10
169#define DT_SYMENT 11
170#define DT_INIT 12
171#define DT_FINI 13
172#define DT_SONAME 14
173#define DT_RPATH 15
174#define DT_SYMBOLIC 16
175#define DT_REL 17
176#define DT_RELSZ 18
177#define DT_RELENT 19
178#define DT_PLTREL 20
179#define DT_DEBUG 21
180#define DT_TEXTREL 22
181#define DT_JMPREL 23
182#define DT_LOPROC 0x70000000
183#define DT_HIPROC 0x7FFFFFFF
184
185typedef unsigned int Elf32_Addr;
186typedef unsigned short Elf32_Half;
187typedef unsigned int Elf32_Off;
188typedef signed int Elf32_Sword;
189typedef unsigned int Elf32_Word;
190
191
192// ELF file header
193struct Elf32_Ehdr
194{
195 unsigned char e_ident[EI_NIDENT];
196 Elf32_Half e_type;
197 Elf32_Half e_machine;
198 Elf32_Word e_version;
199 Elf32_Addr e_entry;
200 Elf32_Off e_phoff;
201 Elf32_Off e_shoff;
202 Elf32_Word e_flags;
203 Elf32_Half e_ehsize;
204 Elf32_Half e_phentsize;
205 Elf32_Half e_phnum;
206 Elf32_Half e_shentsize;
207 Elf32_Half e_shnum;
208 Elf32_Half e_shstrndx;
209};
210
211// Section header
212struct Elf32_Shdr
213{
214 Elf32_Word sh_name;
215 Elf32_Word sh_type;
216 Elf32_Word sh_flags;
217 Elf32_Addr sh_addr;
218 Elf32_Off sh_offset;
219 Elf32_Word sh_size;
220 Elf32_Word sh_link;
221 Elf32_Word sh_info;
222 Elf32_Word sh_addralign;
223 Elf32_Word sh_entsize;
224};
225
226// Segment header
227struct Elf32_Phdr
228{
229 Elf32_Word p_type;
230 Elf32_Off p_offset;
231 Elf32_Addr p_vaddr;
232 Elf32_Addr p_paddr;
233 Elf32_Word p_filesz;
234 Elf32_Word p_memsz;
235 Elf32_Word p_flags;
236 Elf32_Word p_align;
237};
238
239// Symbol table entry
240struct Elf32_Sym
241{
242 Elf32_Word st_name;
243 Elf32_Addr st_value;
244 Elf32_Word st_size;
245 unsigned char st_info;
246 unsigned char st_other;
247 Elf32_Half st_shndx;
248};
249
250#define ELF32_ST_BIND(i) ((i)>>4)
251#define ELF32_ST_TYPE(i) ((i)&0xf)
252#define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
253
254// Relocation entries
255struct Elf32_Rel
256{
257 Elf32_Addr r_offset;
258 Elf32_Word r_info;
259};
260
261struct Elf32_Rela
262{
263 Elf32_Addr r_offset;
264 Elf32_Word r_info;
265 Elf32_Sword r_addend;
266};
267
268#define ELF32_R_SYM(i) ((i)>>8)
269#define ELF32_R_TYPE(i) ((unsigned char)(i))
270#define ELF32_R_INFO(s,t) (((s)<<8 )+(unsigned char)(t))
271
272
273struct Elf32_Dyn
274{
275 Elf32_Sword d_tag;
276 union
277 {
278 Elf32_Word d_val;
279 Elf32_Addr d_ptr;
280 } d_un;
281};