1
// Copyright (c) 2012- PPSSPP Project.
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
18
#include "Core/MemMap.h"
19
#include "Core/Reporting.h"
20
#include "Core/MIPS/MIPSTables.h"
21
#include "ElfReader.h"
22
#include "Core/Debugger/Breakpoints.h"
23
#include "Core/Debugger/SymbolMap.h"
24
#include "Core/HLE/sceKernelMemory.h"
25
#include "Core/HLE/sceKernelModule.h"
31
const char *ElfReader::GetSectionName(int section) const {
32
if (sections[section].sh_type == SHT_NULL)
35
int nameOffset = sections[section].sh_name;
36
const char *ptr = (const char *)GetSectionDataPtr(header->e_shstrndx);
39
return ptr + nameOffset;
46
void addrToHiLo(u32 addr, u16 &hi, s16 &lo)
49
u32 naddr = addr - lo;
51
u32 test = (hi<<16) + lo;
54
WARN_LOG_REPORT(LOADER, "HI16/LO16 relocation failure?");
58
bool ElfReader::LoadRelocations(Elf32_Rel *rels, int numRelocs)
61
DEBUG_LOG(LOADER, "Loading %i relocations...", numRelocs);
62
for (int r = 0; r < numRelocs; r++)
64
// INFO_LOG(LOADER, "Loading reloc %i (%p)...", r, rels + r);
65
u32 info = rels[r].r_info;
66
u32 addr = rels[r].r_offset;
68
int type = info & 0xf;
70
int readwrite = (info>>8) & 0xff;
71
int relative = (info>>16) & 0xff;
76
if (readwrite >= (int)ARRAY_SIZE(segmentVAddr)) {
78
ERROR_LOG_REPORT(LOADER, "Bad segment number %i", readwrite);
84
addr += segmentVAddr[readwrite];
86
// It appears that misaligned relocations are allowed.
87
// Will they work correctly on big-endian?
89
if (((addr & 3) && type != R_MIPS_32) || !Memory::IsValidAddress(addr)) {
91
WARN_LOG_REPORT(LOADER, "Suspicious address %08x, skipping reloc, type = %d", addr, type);
92
} else if (numErrors == 10) {
93
WARN_LOG(LOADER, "Too many bad relocations, skipping logging");
99
u32 op = Memory::Read_Instruction(addr, true).encoding;
101
const bool log = false;
104
DEBUG_LOG(LOADER,"rel at: %08x info: %08x type: %i",addr, info, type);
106
u32 relocateTo = segmentVAddr[relative];
112
DEBUG_LOG(LOADER,"Full address reloc %08x", addr);
113
//full address, no problemo
117
case R_MIPS_26: //j, jal
118
//add on to put in correct address space
120
DEBUG_LOG(LOADER,"j/jal reloc %08x", addr);
121
op = (op & 0xFC000000) | (((op&0x03FFFFFF)+(relocateTo>>2))&0x03FFFFFF);
124
case R_MIPS_HI16: //lui part of lui-addiu pairs
127
DEBUG_LOG(LOADER,"HI reloc %08x", addr);
129
u32 cur = (op & 0xFFFF) << 16;
132
for (int t = r + 1; t<numRelocs; t++)
134
if ((rels[t].r_info & 0xF) == R_MIPS_LO16)
136
u32 corrLoAddr = rels[t].r_offset + segmentVAddr[readwrite];
138
DEBUG_LOG(LOADER,"Corresponding lo found at %08x", corrLoAddr);
140
if (Memory::IsValidAddress(corrLoAddr)) {
141
s16 lo = (s32)(s16)(u16)(Memory::ReadUnchecked_U32(corrLoAddr) & 0xFFFF); //signed??
144
addrToHiLo(cur, hi, lo);
148
ERROR_LOG(LOADER, "Bad corrLoAddr %08x", corrLoAddr);
153
ERROR_LOG_REPORT(LOADER, "R_MIPS_HI16: could not find R_MIPS_LO16");
155
op = (op & 0xFFFF0000) | (hi);
159
case R_MIPS_LO16: //addiu part of lui-addiu pairs
162
DEBUG_LOG(LOADER,"LO reloc %08x", addr);
163
u32 cur = op & 0xFFFF;
166
op = (op & 0xFFFF0000) | cur;
170
case R_MIPS_GPREL16: //gp
171
// It seems safe to ignore this, almost a notification of a gp-relative operation?
177
op = (op & 0xFFFF0000) | (((int)(op & 0xFFFF) + (int)relocateTo) & 0xFFFF);
178
MIPSDisAsm(MIPSOpcode(op), 0, temp);
183
// This shouldn't matter, not sure the purpose of it.
189
MIPSDisAsm(MIPSOpcode(op), 0, temp);
190
ERROR_LOG_REPORT(LOADER,"ARGH IT'S AN UNKNOWN RELOCATION!!!!!!!! %08x, type=%d : %s", addr, type, temp);
194
Memory::Write_U32(op, addr);
197
WARN_LOG(LOADER, "%i bad relocations found!!!", numErrors);
199
return numErrors == 0;
203
void ElfReader::LoadRelocations2(int rel_seg)
206
u8 *buf, *end, *flag_table, *type_table;
207
int flag_table_size, type_table_size;
208
int flag_bits, seg_bits, type_bits;
209
int cmd, flag, seg, type;
210
int off_seg = 0, addr_seg, rel_base, rel_offset;
211
int relocate_to, last_type, lo16 = 0;
215
ph = segments + rel_seg;
218
buf = (u8*)GetSegmentPtr(rel_seg);
219
end = buf+ph->p_filesz;
225
while((1<<seg_bits)<rel_seg)
231
flag_table_size = flag_table[0];
232
buf += flag_table_size;
235
type_table_size = type_table[0];
236
buf += type_table_size;
244
flag = ( cmd<<(16-flag_bits))&0xffff;
245
flag = (flag>>(16-flag_bits))&0xffff;
246
flag = flag_table[flag];
248
seg = (cmd<<(16-seg_bits-flag_bits))&0xffff;
249
seg = (seg>>(16-seg_bits))&0xffff;
251
type = ( cmd<<(16-type_bits-seg_bits-flag_bits))&0xffff;
252
type = (type>>(16-type_bits))&0xffff;
253
type = type_table[type];
258
rel_base = cmd>>(seg_bits+flag_bits);
259
}else if((flag&0x06)==4){
260
rel_base = buf[0] | (buf[1]<<8) | (buf[2]<<16) | (buf[3]<<24);
263
ERROR_LOG_REPORT(LOADER, "Rel2: invalid size flag! %x", flag);
268
relocate_to = segmentVAddr[addr_seg];
270
if((flag&0x06)==0x00){
273
rel_offset |= 0xffff0000;
274
rel_offset >>= type_bits+seg_bits+flag_bits;
275
rel_offset |= 0xffff0000;
277
rel_offset >>= type_bits+seg_bits+flag_bits;
279
rel_base += rel_offset;
280
}else if((flag&0x06)==0x02){
283
rel_offset |= 0xffff0000;
284
rel_offset >>= type_bits+seg_bits+flag_bits;
285
rel_offset = (rel_offset<<16) | (buf[0]) | (buf[1]<<8);
287
rel_base += rel_offset;
288
}else if((flag&0x06)==0x04){
289
rel_base = buf[0] | (buf[1]<<8) | (buf[2]<<16) | (buf[3]<<24);
292
ERROR_LOG_REPORT(LOADER, "Rel2: invalid relocat size flag! %x", flag);
296
rel_offset = rel_base+segmentVAddr[off_seg];
298
if((flag&0x38)==0x00){
300
}else if((flag&0x38)==0x08){
303
}else if((flag&0x38)==0x10){
304
lo16 = (buf[0]) | (buf[1]<<8);
309
ERROR_LOG_REPORT(LOADER, "Rel2: invalid lo16 type! %x", flag);
312
op = Memory::Read_Instruction(rel_offset, true).encoding;
313
DEBUG_LOG(LOADER, "Rel2: %5d: CMD=0x%04X flag=%x type=%d off_seg=%d offset=%08x addr_seg=%d op=%08x\n", rcount, cmd, flag, type, off_seg, rel_base, addr_seg, op);
322
case 6: // R_MIPS_J26
323
case 7: // R_MIPS_JAL26
324
op = (op&0xFC000000) | (((op&0x03FFFFFF)+(relocate_to>>2))&0x03FFFFFF);
325
// To be safe, let's force it to the specified jump.
327
op = (op & ~0xFC000000) | 0x08000000;
329
op = (op & ~0xFC000000) | 0x0C000000;
331
case 4: // R_MIPS_HI16
332
addr = ((op<<16)+lo16)+relocate_to;
335
op = (op&0xffff0000) | (addr>>16 );
338
case 5: // R_MIPS_LO16
339
op = (op&0xffff0000) | (((op&0xffff)+relocate_to)&0xffff);
342
ERROR_LOG_REPORT(LOADER, "Rel2: unexpected relocation type! %x", type);
346
Memory::Write_U32(op, rel_offset);
354
int ElfReader::LoadInto(u32 loadAddress, bool fromTop)
356
DEBUG_LOG(LOADER,"String section: %i", header->e_shstrndx);
358
if (header->e_ident[0] != ELFMAG0 || header->e_ident[1] != ELFMAG1
359
|| header->e_ident[2] != ELFMAG2 || header->e_ident[3] != ELFMAG3)
360
return SCE_KERNEL_ERROR_UNSUPPORTED_PRX_TYPE;
362
// technically ELFCLASSNONE would freeze the system, but that's not really desireable
363
if (header->e_ident[EI_CLASS] != ELFCLASS32) {
364
if (header->e_ident[EI_CLASS] != 0) {
365
return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED;
368
ERROR_LOG(LOADER, "Bad ELF, EI_CLASS (fifth byte) is 0x00, should be 0x01 - would lock up a PSP.");
371
if (header->e_ident[EI_DATA] != ELFDATA2LSB)
372
return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED;
374
// e_ident[EI_VERSION] is ignored
376
sectionOffsets = new u32[GetNumSections()];
377
sectionAddrs = new u32[GetNumSections()];
379
// Should we relocate?
380
bRelocate = (header->e_type != ET_EXEC);
382
// Look for the module info - we need to know whether this is kernel or user.
383
const PspModuleInfo *modInfo = 0;
384
for (int i = 0; i < GetNumSections(); i++) {
385
Elf32_Shdr *s = §ions[i];
386
const char *name = GetSectionName(i);
387
if (name && !strcmp(name, ".rodata.sceModuleInfo")) {
388
modInfo = (const PspModuleInfo *)GetPtr(s->sh_offset);
391
if (!modInfo && GetNumSegments() >= 1) {
392
modInfo = (const PspModuleInfo *)GetPtr(segments[0].p_paddr & 0x7FFFFFFF);
395
bool kernelModule = modInfo ? (modInfo->moduleAttrs & 0x1000) != 0 : false;
397
std::string modName = "ELF";
399
size_t n = strnlen(modInfo->name, 28);
400
modName = "ELF/" + std::string(modInfo->name, n);
403
entryPoint = header->e_entry;
404
u32 totalStart = 0xFFFFFFFF;
406
for (int i = 0; i < header->e_phnum; i++) {
407
Elf32_Phdr *p = &segments[i];
408
if (p->p_type == PT_LOAD) {
409
if (p->p_vaddr < totalStart)
410
totalStart = p->p_vaddr;
411
if (p->p_vaddr + p->p_memsz > totalEnd)
412
totalEnd = p->p_vaddr + p->p_memsz;
415
totalSize = totalEnd - totalStart;
417
// If a load address is specified that's in regular RAM, override kernel module status
418
bool inUser = totalStart >= PSP_GetUserMemoryBase();
419
BlockAllocator &memblock = (kernelModule && !inUser) ? kernelMemory : userMemory;
423
// Binary is prerelocated, load it where the first segment starts
424
vaddr = memblock.AllocAt(totalStart, totalSize, modName.c_str());
426
else if (loadAddress)
428
// Binary needs to be relocated: add loadAddress to the binary start address
429
vaddr = memblock.AllocAt(loadAddress + totalStart, totalSize, modName.c_str());
433
// Just put it where there is room
434
vaddr = memblock.Alloc(totalSize, fromTop, modName.c_str());
437
if (vaddr == (u32)-1) {
438
ERROR_LOG_REPORT(LOADER, "Failed to allocate memory for ELF!");
439
return SCE_KERNEL_ERROR_MEMBLOCK_ALLOC_FAILED;
443
DEBUG_LOG(LOADER,"Relocatable module");
446
DEBUG_LOG(LOADER,"Prerelocated executable");
449
DEBUG_LOG(LOADER,"%i segments:", header->e_phnum);
451
// First pass : Get the damn bits into RAM
452
u32 baseAddress = bRelocate?vaddr:0;
454
for (int i=0; i<header->e_phnum; i++)
456
Elf32_Phdr *p = segments + i;
457
DEBUG_LOG(LOADER, "Type: %08x Vaddr: %08x Filesz: %08x Memsz: %08x ", (int)p->p_type, (u32)p->p_vaddr, (int)p->p_filesz, (int)p->p_memsz);
459
if (p->p_type == PT_LOAD)
461
segmentVAddr[i] = baseAddress + p->p_vaddr;
462
u32 writeAddr = segmentVAddr[i];
464
u8 *src = GetSegmentPtr(i);
465
u8 *dst = Memory::GetPointer(writeAddr);
466
u32 srcSize = p->p_filesz;
467
u32 dstSize = p->p_memsz;
469
if (srcSize < dstSize)
471
memset(dst + srcSize, 0, dstSize - srcSize); //zero out bss
474
memcpy(dst, src, srcSize);
475
CBreakPoints::ExecMemCheck(writeAddr, true, dstSize, currentMIPS->pc);
476
DEBUG_LOG(LOADER,"Loadable Segment Copied to %08x, size %08x", writeAddr, (u32)p->p_memsz);
479
memblock.ListBlocks();
481
DEBUG_LOG(LOADER,"%i sections:", header->e_shnum);
483
for (int i = 0; i < GetNumSections(); i++)
485
Elf32_Shdr *s = §ions[i];
486
const char *name = GetSectionName(i);
488
u32 writeAddr = s->sh_addr + baseAddress;
489
sectionOffsets[i] = writeAddr - vaddr;
490
sectionAddrs[i] = writeAddr;
492
if (s->sh_flags & SHF_ALLOC)
494
DEBUG_LOG(LOADER,"Data Section found: %s Sitting at %08x, size %08x", name, writeAddr, (u32)s->sh_size);
498
DEBUG_LOG(LOADER,"NonData Section found: %s Ignoring (size=%08x) (flags=%08x)", name, (u32)s->sh_size, (u32)s->sh_flags);
502
DEBUG_LOG(LOADER,"Relocations:");
504
// Second pass: Do necessary relocations
505
for (int i = 0; i < GetNumSections(); i++)
507
Elf32_Shdr *s = §ions[i];
508
const char *name = GetSectionName(i);
510
if (s->sh_type == SHT_PSPREL)
512
//We have a relocation table!
513
int sectionToModify = s->sh_info;
514
if (sectionToModify >= 0)
516
if (!(sections[sectionToModify].sh_flags & SHF_ALLOC))
518
ERROR_LOG_REPORT(LOADER, "Trying to relocate non-loaded section %s", GetSectionName(sectionToModify));
522
int numRelocs = s->sh_size / sizeof(Elf32_Rel);
524
Elf32_Rel *rels = (Elf32_Rel *)GetSectionDataPtr(i);
526
DEBUG_LOG(LOADER,"%s: Performing %i relocations on %s : offset = %08x", name, numRelocs, GetSectionName(sectionToModify), sections[i].sh_offset);
527
if (!LoadRelocations(rels, numRelocs)) {
528
WARN_LOG(LOADER, "LoadInto: Relocs failed, trying anyway");
533
WARN_LOG_REPORT(LOADER, "sectionToModify = %i - ignoring PSP relocation sector %i", sectionToModify, i);
536
else if (s->sh_type == SHT_REL)
538
DEBUG_LOG(LOADER, "Traditional relocation section found.");
541
DEBUG_LOG(LOADER, "Binary is prerelocated. Skipping relocations.");
545
//We have a relocation table!
546
int sectionToModify = s->sh_info;
547
if (sectionToModify >= 0)
549
if (!(sections[sectionToModify].sh_flags & SHF_ALLOC))
551
ERROR_LOG_REPORT(LOADER, "Trying to relocate non-loaded section %s, ignoring", GetSectionName(sectionToModify));
557
WARN_LOG_REPORT(LOADER, "sectionToModify = %i - ignoring relocation sector %i", sectionToModify, i);
559
ERROR_LOG_REPORT(LOADER, "Traditional relocations unsupported.");
564
// Segment relocations (a few games use them)
565
if (GetNumSections() == 0) {
566
for (int i = 0; i < header->e_phnum; i++)
568
Elf32_Phdr *p = &segments[i];
569
if (p->p_type == PT_PSPREL1) {
570
INFO_LOG(LOADER,"Loading segment relocations");
571
int numRelocs = p->p_filesz / sizeof(Elf32_Rel);
573
Elf32_Rel *rels = (Elf32_Rel *)GetSegmentPtr(i);
574
if (!LoadRelocations(rels, numRelocs)) {
575
ERROR_LOG(LOADER, "LoadInto: Relocs failed, trying anyway (2)");
577
} else if (p->p_type == PT_PSPREL2) {
578
INFO_LOG(LOADER,"Loading segment relocations2");
584
return SCE_KERNEL_ERROR_OK;
588
SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const
590
for (int i = firstSection; i < header->e_shnum; i++)
592
const char *secname = GetSectionName(i);
594
if (secname != 0 && strcmp(name, secname) == 0)
602
u32 ElfReader::GetTotalTextSize() const {
604
for (int i = 0; i < GetNumSections(); ++i) {
605
if (!(sections[i].sh_flags & SHF_WRITE) && (sections[i].sh_flags & SHF_ALLOC) && !(sections[i].sh_flags & SHF_STRINGS)) {
606
total += sections[i].sh_size;
612
u32 ElfReader::GetTotalDataSize() const {
614
for (int i = 0; i < GetNumSections(); ++i) {
615
if ((sections[i].sh_flags & SHF_WRITE) && (sections[i].sh_flags & SHF_ALLOC) && !(sections[i].sh_flags & SHF_MASKPROC)) {
616
total += sections[i].sh_size;
622
u32 ElfReader::GetTotalSectionSizeByPrefix(const std::string &prefix) const {
624
for (int i = 0; i < GetNumSections(); ++i) {
625
const char *secname = GetSectionName(i);
626
if (secname && !strncmp(secname, prefix.c_str(), prefix.length())) {
627
total += sections[i].sh_size;
633
bool ElfReader::LoadSymbols()
635
bool hasSymbols = false;
636
SectionID sec = GetSectionByName(".symtab");
639
int stringSection = sections[sec].sh_link;
641
const char *stringBase = (const char*)GetSectionDataPtr(stringSection);
643
//We have a symbol table!
644
Elf32_Sym *symtab = (Elf32_Sym *)(GetSectionDataPtr(sec));
646
int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym);
648
for (int sym = 0; sym<numSymbols; sym++)
650
int size = symtab[sym].st_size;
654
int bind = symtab[sym].st_info >> 4;
655
int type = symtab[sym].st_info & 0xF;
656
int sectionIndex = symtab[sym].st_shndx;
657
int value = symtab[sym].st_value;
658
const char *name = stringBase + symtab[sym].st_name;
661
value += sectionAddrs[sectionIndex];
666
g_symbolMap->AddData(value,size,DATATYPE_BYTE);
669
g_symbolMap->AddFunction(name,value,size);