summaryrefslogtreecommitdiff
path: root/src/core/elf/elf_reader.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/elf/elf_reader.cpp')
-rw-r--r--src/core/elf/elf_reader.cpp239
1 files changed, 239 insertions, 0 deletions
diff --git a/src/core/elf/elf_reader.cpp b/src/core/elf/elf_reader.cpp
new file mode 100644
index 000000000..aef7c13e9
--- /dev/null
+++ b/src/core/elf/elf_reader.cpp
@@ -0,0 +1,239 @@
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
126 for (int i = 0; i < header->e_phnum; i++)
127 {
128 Elf32_Phdr *p = segments + i;
129
130 INFO_LOG(MASTER_LOG, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz);
131
132 if (p->p_type == PT_LOAD)
133 {
134 segmentVAddr[i] = baseAddress + p->p_vaddr;
135 u32 writeAddr = segmentVAddr[i];
136
137 const u8 *src = GetSegmentPtr(i);
138 u8 *dst = Memory::GetPointer(writeAddr);
139 u32 srcSize = p->p_filesz;
140 u32 dstSize = p->p_memsz;
141 u32 *s = (u32*)src;
142 u32 *d = (u32*)dst;
143 for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
144 {
145 *d++ = /*_byteswap_ulong*/(*s++);
146 }
147 if (srcSize < dstSize)
148 {
149 //memset(dst + srcSize, 0, dstSize-srcSize); //zero out bss
150 }
151 INFO_LOG(MASTER_LOG,"Loadable Segment Copied to %08x, size %08x", writeAddr, p->p_memsz);
152 }
153 }
154
155 /*
156 LOG(MASTER_LOG,"%i sections:", header->e_shnum);
157
158 for (int i=0; i<GetNumSections(); i++)
159 {
160 Elf32_Shdr *s = &sections[i];
161 const char *name = GetSectionName(i);
162
163 u32 writeAddr = s->sh_addr + baseAddress;
164 sectionOffsets[i] = writeAddr - vaddr;
165 sectionAddrs[i] = writeAddr;
166
167 if (s->sh_flags & SHF_ALLOC)
168 {
169 LOG(MASTER_LOG,"Data Section found: %s Sitting at %08x, size %08x", name, writeAddr, s->sh_size);
170
171 }
172 else
173 {
174 LOG(MASTER_LOG,"NonData Section found: %s Ignoring (size=%08x) (flags=%08x)", name, s->sh_size, s->sh_flags);
175 }
176 }
177*/
178 INFO_LOG(MASTER_LOG,"Done loading.");
179 return true;
180}
181
182SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const
183{
184 for (int i = firstSection; i < header->e_shnum; i++)
185 {
186 const char *secname = GetSectionName(i);
187
188 if (secname != nullptr && strcmp(name, secname) == 0)
189 return i;
190 }
191 return -1;
192}
193
194/* TODO(bunnei): The following is verbatim from Dolphin - needs to be updated for this project:
195
196bool ElfReader::LoadSymbols()
197{
198 bool hasSymbols = false;
199 SectionID sec = GetSectionByName(".symtab");
200 if (sec != -1)
201 {
202 int stringSection = sections[sec].sh_link;
203 const char *stringBase = (const char *)GetSectionDataPtr(stringSection);
204
205 //We have a symbol table!
206 Elf32_Sym *symtab = (Elf32_Sym *)(GetSectionDataPtr(sec));
207 int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym);
208 for (int sym = 0; sym < numSymbols; sym++)
209 {
210 int size = Common::swap32(symtab[sym].st_size);
211 if (size == 0)
212 continue;
213
214 // int bind = symtab[sym].st_info >> 4;
215 int type = symtab[sym].st_info & 0xF;
216 int sectionIndex = Common::swap16(symtab[sym].st_shndx);
217 int value = Common::swap32(symtab[sym].st_value);
218 const char *name = stringBase + Common::swap32(symtab[sym].st_name);
219 if (bRelocate)
220 value += sectionAddrs[sectionIndex];
221
222 int symtype = Symbol::SYMBOL_DATA;
223 switch (type)
224 {
225 case STT_OBJECT:
226 symtype = Symbol::SYMBOL_DATA; break;
227 case STT_FUNC:
228 symtype = Symbol::SYMBOL_FUNCTION; break;
229 default:
230 continue;
231 }
232 g_symbolDB.AddKnownSymbol(value, size, name, symtype);
233 hasSymbols = true;
234 }
235 }
236 g_symbolDB.Index();
237 return hasSymbols;
238}
239*/