summaryrefslogtreecommitdiff
path: root/src/core/loader/elf.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2014-06-16 23:05:10 -0400
committerGravatar bunnei2014-06-16 23:43:32 -0400
commit1da361c7ab55e1dbe6709a738e228bfab5a5bb78 (patch)
tree487be34416c624106a01edf8b075866fb2cb9d33 /src/core/loader/elf.cpp
parentLoader: Added support for booting NCCH executables. (diff)
downloadyuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.gz
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.tar.xz
yuzu-1da361c7ab55e1dbe6709a738e228bfab5a5bb78.zip
Elf: Renamed modules to be consistent with new loader naming, fixed tabs -> spaces.
Diffstat (limited to 'src/core/loader/elf.cpp')
-rw-r--r--src/core/loader/elf.cpp190
1 files changed, 190 insertions, 0 deletions
diff --git a/src/core/loader/elf.cpp b/src/core/loader/elf.cpp
new file mode 100644
index 000000000..153c30f51
--- /dev/null
+++ b/src/core/loader/elf.cpp
@@ -0,0 +1,190 @@
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/loader/elf.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}