summaryrefslogtreecommitdiff
path: root/src/core/elf
diff options
context:
space:
mode:
authorGravatar bunnei2014-06-25 09:46:14 -0400
committerGravatar bunnei2014-06-25 09:46:14 -0400
commit469fe42fad01fc45e454e6acfa413eeae92e587e (patch)
tree4cf876688cc2d03d34512f8f1a25bc26d853f1fb /src/core/elf
parentMerge pull request #7 from archshift/travis-osx (diff)
parentLoader: Refactored loading functions to only read data from binary if called. (diff)
downloadyuzu-469fe42fad01fc45e454e6acfa413eeae92e587e.tar.gz
yuzu-469fe42fad01fc45e454e6acfa413eeae92e587e.tar.xz
yuzu-469fe42fad01fc45e454e6acfa413eeae92e587e.zip
Merge pull request #22 from bunnei/loader-improvements
Refactor loader code and add preliminary NCCH support
Diffstat (limited to 'src/core/elf')
-rw-r--r--src/core/elf/elf_reader.cpp190
-rw-r--r--src/core/elf/elf_reader.h75
-rw-r--r--src/core/elf/elf_types.h281
3 files changed, 0 insertions, 546 deletions
diff --git a/src/core/elf/elf_reader.cpp b/src/core/elf/elf_reader.cpp
deleted file mode 100644
index c62332cec..000000000
--- a/src/core/elf/elf_reader.cpp
+++ /dev/null
@@ -1,190 +0,0 @@
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/common.h"
8
9#include "common/symbols.h"
10#include "core/mem_map.h"
11#include "core/elf/elf_reader.h"
12
13//void bswap(Elf32_Word &w) {w = Common::swap32(w);}
14//void bswap(Elf32_Half &w) {w = Common::swap16(w);}
15
16#define bswap(w) w // Dirty bswap disable for now... 3DS is little endian, anyway
17
18static void byteswapHeader(Elf32_Ehdr &ELF_H)
19{
20 bswap(ELF_H.e_type);
21 bswap(ELF_H.e_machine);
22 bswap(ELF_H.e_ehsize);
23 bswap(ELF_H.e_phentsize);
24 bswap(ELF_H.e_phnum);
25 bswap(ELF_H.e_shentsize);
26 bswap(ELF_H.e_shnum);
27 bswap(ELF_H.e_shstrndx);
28 bswap(ELF_H.e_version);
29 bswap(ELF_H.e_entry);
30 bswap(ELF_H.e_phoff);
31 bswap(ELF_H.e_shoff);
32 bswap(ELF_H.e_flags);
33}
34
35static void byteswapSegment(Elf32_Phdr &sec)
36{
37 bswap(sec.p_align);
38 bswap(sec.p_filesz);
39 bswap(sec.p_flags);
40 bswap(sec.p_memsz);
41 bswap(sec.p_offset);
42 bswap(sec.p_paddr);
43 bswap(sec.p_vaddr);
44 bswap(sec.p_type);
45}
46
47static void byteswapSection(Elf32_Shdr &sec)
48{
49 bswap(sec.sh_addr);
50 bswap(sec.sh_addralign);
51 bswap(sec.sh_entsize);
52 bswap(sec.sh_flags);
53 bswap(sec.sh_info);
54 bswap(sec.sh_link);
55 bswap(sec.sh_name);
56 bswap(sec.sh_offset);
57 bswap(sec.sh_size);
58 bswap(sec.sh_type);
59}
60
61ElfReader::ElfReader(void *ptr)
62{
63 base = (char*)ptr;
64 base32 = (u32 *)ptr;
65 header = (Elf32_Ehdr*)ptr;
66 byteswapHeader(*header);
67
68 segments = (Elf32_Phdr *)(base + header->e_phoff);
69 sections = (Elf32_Shdr *)(base + header->e_shoff);
70
71 entryPoint = header->e_entry;
72
73 LoadSymbols();
74}
75
76const char *ElfReader::GetSectionName(int section) const
77{
78 if (sections[section].sh_type == SHT_NULL)
79 return nullptr;
80
81 int nameOffset = sections[section].sh_name;
82 char *ptr = (char*)GetSectionDataPtr(header->e_shstrndx);
83
84 if (ptr)
85 return ptr + nameOffset;
86 else
87 return nullptr;
88}
89
90bool ElfReader::LoadInto(u32 vaddr)
91{
92 DEBUG_LOG(MASTER_LOG,"String section: %i", header->e_shstrndx);
93
94 // Should we relocate?
95 bRelocate = (header->e_type != ET_EXEC);
96
97 if (bRelocate)
98 {
99 DEBUG_LOG(MASTER_LOG,"Relocatable module");
100 entryPoint += vaddr;
101 }
102 else
103 {
104 DEBUG_LOG(MASTER_LOG,"Prerelocated executable");
105 }
106
107 INFO_LOG(MASTER_LOG,"%i segments:", header->e_phnum);
108
109 // First pass : Get the bits into RAM
110 u32 segmentVAddr[32];
111
112 u32 baseAddress = bRelocate?vaddr:0;
113
114 for (int i = 0; i < header->e_phnum; i++)
115 {
116 Elf32_Phdr *p = segments + i;
117
118 INFO_LOG(MASTER_LOG, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz);
119
120 if (p->p_type == PT_LOAD)
121 {
122 segmentVAddr[i] = baseAddress + p->p_vaddr;
123 u32 writeAddr = segmentVAddr[i];
124
125 const u8 *src = GetSegmentPtr(i);
126 u8 *dst = Memory::GetPointer(writeAddr);
127 u32 srcSize = p->p_filesz;
128 u32 dstSize = p->p_memsz;
129 u32 *s = (u32*)src;
130 u32 *d = (u32*)dst;
131 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
132 {
133 *d++ = /*_byteswap_ulong*/(*s++);
134 }
135 if (srcSize < dstSize)
136 {
137 //memset(dst + srcSize, 0, dstSize-srcSize); //zero out bss
138 }
139 INFO_LOG(MASTER_LOG,"Loadable Segment Copied to %08x, size %08x", writeAddr, p->p_memsz);
140 }
141 }
142
143
144 INFO_LOG(MASTER_LOG,"Done loading.");
145 return true;
146}
147
148SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const
149{
150 for (int i = firstSection; i < header->e_shnum; i++)
151 {
152 const char *secname = GetSectionName(i);
153
154 if (secname != nullptr && strcmp(name, secname) == 0)
155 return i;
156 }
157 return -1;
158}
159
160bool ElfReader::LoadSymbols()
161{
162 bool hasSymbols = false;
163 SectionID sec = GetSectionByName(".symtab");
164 if (sec != -1)
165 {
166 int stringSection = sections[sec].sh_link;
167 const char *stringBase = (const char *)GetSectionDataPtr(stringSection);
168
169 //We have a symbol table!
170 Elf32_Sym *symtab = (Elf32_Sym *)(GetSectionDataPtr(sec));
171 int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym);
172 for (int sym = 0; sym < numSymbols; sym++)
173 {
174 int size = symtab[sym].st_size;
175 if (size == 0)
176 continue;
177
178 // int bind = symtab[sym].st_info >> 4;
179 int type = symtab[sym].st_info & 0xF;
180
181 const char *name = stringBase + symtab[sym].st_name;
182
183 Symbols::Add(symtab[sym].st_value, name, size, type);
184
185 hasSymbols = true;
186 }
187 }
188
189 return hasSymbols;
190}
diff --git a/src/core/elf/elf_reader.h b/src/core/elf/elf_reader.h
deleted file mode 100644
index 3e2869f87..000000000
--- a/src/core/elf/elf_reader.h
+++ /dev/null
@@ -1,75 +0,0 @@
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 "core/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/elf/elf_types.h b/src/core/elf/elf_types.h
deleted file mode 100644
index f1bf3db72..000000000
--- a/src/core/elf/elf_types.h
+++ /dev/null
@@ -1,281 +0,0 @@
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};