|
|
@ -54,12 +54,6 @@ enum ElfMachine { |
|
|
#define EI_PAD 7
|
|
|
#define EI_PAD 7
|
|
|
#define EI_NIDENT 16
|
|
|
#define EI_NIDENT 16
|
|
|
|
|
|
|
|
|
// Magic number
|
|
|
|
|
|
#define ELFMAG0 0x7F
|
|
|
|
|
|
#define ELFMAG1 'E'
|
|
|
|
|
|
#define ELFMAG2 'L'
|
|
|
|
|
|
#define ELFMAG3 'F'
|
|
|
|
|
|
|
|
|
|
|
|
// Sections constants
|
|
|
// Sections constants
|
|
|
|
|
|
|
|
|
// Section types
|
|
|
// Section types
|
|
|
@ -188,7 +182,6 @@ private: |
|
|
|
|
|
|
|
|
public: |
|
|
public: |
|
|
ElfReader(void *ptr); |
|
|
ElfReader(void *ptr); |
|
|
~ElfReader() { } |
|
|
|
|
|
|
|
|
|
|
|
u32 Read32(int off) const { return base32[off >> 2]; } |
|
|
u32 Read32(int off) const { return base32[off >> 2]; } |
|
|
|
|
|
|
|
|
@ -197,7 +190,7 @@ public: |
|
|
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } |
|
|
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); } |
|
|
u32 GetEntryPoint() const { return entryPoint; } |
|
|
u32 GetEntryPoint() const { return entryPoint; } |
|
|
u32 GetFlags() const { return (u32)(header->e_flags); } |
|
|
u32 GetFlags() const { return (u32)(header->e_flags); } |
|
|
bool LoadInto(u32 vaddr); |
|
|
|
|
|
|
|
|
void LoadInto(u32 vaddr); |
|
|
bool LoadSymbols(); |
|
|
bool LoadSymbols(); |
|
|
|
|
|
|
|
|
int GetNumSegments() const { return (int)(header->e_phnum); } |
|
|
int GetNumSegments() const { return (int)(header->e_phnum); } |
|
|
@ -245,7 +238,7 @@ const char *ElfReader::GetSectionName(int section) const { |
|
|
return nullptr; |
|
|
return nullptr; |
|
|
|
|
|
|
|
|
int name_offset = sections[section].sh_name; |
|
|
int name_offset = sections[section].sh_name; |
|
|
char *ptr = (char*)GetSectionDataPtr(header->e_shstrndx); |
|
|
|
|
|
|
|
|
const char* ptr = (char*)GetSectionDataPtr(header->e_shstrndx); |
|
|
|
|
|
|
|
|
if (ptr) |
|
|
if (ptr) |
|
|
return ptr + name_offset; |
|
|
return ptr + name_offset; |
|
|
@ -253,7 +246,7 @@ const char *ElfReader::GetSectionName(int section) const { |
|
|
return nullptr; |
|
|
return nullptr; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
bool ElfReader::LoadInto(u32 vaddr) { |
|
|
|
|
|
|
|
|
void ElfReader::LoadInto(u32 vaddr) { |
|
|
LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx); |
|
|
LOG_DEBUG(Loader, "String section: %i", header->e_shstrndx); |
|
|
|
|
|
|
|
|
// Should we relocate?
|
|
|
// Should we relocate?
|
|
|
@ -271,7 +264,7 @@ bool ElfReader::LoadInto(u32 vaddr) { |
|
|
u32 segment_addr[32]; |
|
|
u32 segment_addr[32]; |
|
|
u32 base_addr = relocate ? vaddr : 0; |
|
|
u32 base_addr = relocate ? vaddr : 0; |
|
|
|
|
|
|
|
|
for (int i = 0; i < header->e_phnum; i++) { |
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < header->e_phnum; i++) { |
|
|
Elf32_Phdr* p = segments + i; |
|
|
Elf32_Phdr* p = segments + i; |
|
|
LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, |
|
|
LOG_DEBUG(Loader, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, |
|
|
p->p_filesz, p->p_memsz); |
|
|
p->p_filesz, p->p_memsz); |
|
|
@ -284,7 +277,6 @@ bool ElfReader::LoadInto(u32 vaddr) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
LOG_DEBUG(Loader, "Done loading."); |
|
|
LOG_DEBUG(Loader, "Done loading."); |
|
|
return true; |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const { |
|
|
SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const { |
|
|
@ -307,7 +299,7 @@ bool ElfReader::LoadSymbols() { |
|
|
//We have a symbol table!
|
|
|
//We have a symbol table!
|
|
|
Elf32_Sym* symtab = (Elf32_Sym *)(GetSectionDataPtr(sec)); |
|
|
Elf32_Sym* symtab = (Elf32_Sym *)(GetSectionDataPtr(sec)); |
|
|
int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym); |
|
|
int numSymbols = sections[sec].sh_size / sizeof(Elf32_Sym); |
|
|
for (int sym = 0; sym < numSymbols; sym++) { |
|
|
|
|
|
|
|
|
for (unsigned sym = 0; sym < numSymbols; sym++) { |
|
|
int size = symtab[sym].st_size; |
|
|
int size = symtab[sym].st_size; |
|
|
if (size == 0) |
|
|
if (size == 0) |
|
|
continue; |
|
|
continue; |
|
|
@ -354,7 +346,8 @@ ResultStatus AppLoader_ELF::Load() { |
|
|
|
|
|
|
|
|
u32 size = static_cast<u32>(file->GetSize()); |
|
|
u32 size = static_cast<u32>(file->GetSize()); |
|
|
std::unique_ptr<u8[]> buffer(new u8[size]); |
|
|
std::unique_ptr<u8[]> buffer(new u8[size]); |
|
|
file->ReadBytes(&buffer[0], size); |
|
|
|
|
|
|
|
|
if (file->ReadBytes(&buffer[0], size) != size) |
|
|
|
|
|
return ResultStatus::Error; |
|
|
|
|
|
|
|
|
ElfReader elf_reader(&buffer[0]); |
|
|
ElfReader elf_reader(&buffer[0]); |
|
|
elf_reader.LoadInto(0x00100000); |
|
|
elf_reader.LoadInto(0x00100000); |
|
|
|