ElfReader: Clean up elf loader code.
* Don't claim to support any features we don't, like relocation * Actually zero-out BSS sections, as memory might not be already zeroed. * Deleted commented out code. * Removed GetPointer, updated to more modern interface methods. * Updated pointer types style from "u32 *x" to "u32* x"
This commit is contained in:
parent
1e5762b163
commit
2bf052d8b9
|
@ -65,7 +65,7 @@ bool CBoot::Boot_ELF(const std::string& filename)
|
|||
|
||||
// Load ELF into GameCube Memory
|
||||
ElfReader reader(elf.get());
|
||||
reader.LoadInto(0x80000000);
|
||||
reader.LoadIntoMemory();
|
||||
|
||||
if (!reader.LoadSymbols())
|
||||
{
|
||||
|
|
|
@ -61,7 +61,7 @@ static void byteswapSection(Elf32_Shdr &sec)
|
|||
ElfReader::ElfReader(void *ptr)
|
||||
{
|
||||
base = (char*)ptr;
|
||||
base32 = (u32 *)ptr;
|
||||
base32 = (u32*)ptr;
|
||||
header = (Elf32_Ehdr*)ptr;
|
||||
byteswapHeader(*header);
|
||||
|
||||
|
@ -80,13 +80,13 @@ ElfReader::ElfReader(void *ptr)
|
|||
entryPoint = header->e_entry;
|
||||
}
|
||||
|
||||
const char *ElfReader::GetSectionName(int section) const
|
||||
const char* ElfReader::GetSectionName(int section) const
|
||||
{
|
||||
if (sections[section].sh_type == SHT_NULL)
|
||||
return nullptr;
|
||||
|
||||
int nameOffset = sections[section].sh_name;
|
||||
char *ptr = (char*)GetSectionDataPtr(header->e_shstrndx);
|
||||
char* ptr = (char*)GetSectionDataPtr(header->e_shstrndx);
|
||||
|
||||
if (ptr)
|
||||
return ptr + nameOffset;
|
||||
|
@ -94,84 +94,45 @@ const char *ElfReader::GetSectionName(int section) const
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
bool ElfReader::LoadInto(u32 vaddr)
|
||||
// This is just a simple elf loader, good enough to load elfs generated by devkitPPC
|
||||
bool ElfReader::LoadIntoMemory()
|
||||
{
|
||||
DEBUG_LOG(MASTER_LOG,"String section: %i", header->e_shstrndx);
|
||||
|
||||
// sectionOffsets = new u32[GetNumSections()];
|
||||
// sectionAddrs = new u32[GetNumSections()];
|
||||
|
||||
// Should we relocate?
|
||||
bRelocate = (header->e_type != ET_EXEC);
|
||||
|
||||
if (bRelocate)
|
||||
{
|
||||
DEBUG_LOG(MASTER_LOG,"Relocatable module");
|
||||
entryPoint += vaddr;
|
||||
}
|
||||
else
|
||||
{
|
||||
DEBUG_LOG(MASTER_LOG,"Prerelocated executable");
|
||||
PanicAlert("Error: Dolphin doesn't know to load a relocatable elf.");
|
||||
return false;
|
||||
}
|
||||
|
||||
INFO_LOG(MASTER_LOG,"%i segments:", header->e_phnum);
|
||||
|
||||
// First pass : Get the bits into RAM
|
||||
u32 segmentVAddr[32];
|
||||
|
||||
u32 baseAddress = bRelocate?vaddr:0;
|
||||
// Copy segments into ram.
|
||||
for (int i = 0; i < header->e_phnum; i++)
|
||||
{
|
||||
Elf32_Phdr *p = segments + i;
|
||||
Elf32_Phdr* p = segments + i;
|
||||
|
||||
INFO_LOG(MASTER_LOG, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ", p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz);
|
||||
INFO_LOG(MASTER_LOG, "Type: %i Vaddr: %08x Filesz: %i Memsz: %i ",
|
||||
p->p_type, p->p_vaddr, p->p_filesz, p->p_memsz);
|
||||
|
||||
if (p->p_type == PT_LOAD)
|
||||
{
|
||||
segmentVAddr[i] = baseAddress + p->p_vaddr;
|
||||
u32 writeAddr = segmentVAddr[i];
|
||||
|
||||
const u8 *src = GetSegmentPtr(i);
|
||||
u8 *dst = Memory::GetPointer(writeAddr);
|
||||
u32 writeAddr = p->p_vaddr;
|
||||
const u8* src = GetSegmentPtr(i);
|
||||
u32 srcSize = p->p_filesz;
|
||||
u32 dstSize = p->p_memsz;
|
||||
u32 *s = (u32*)src;
|
||||
u32 *d = (u32*)dst;
|
||||
for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
|
||||
{
|
||||
*d++ = /*_byteswap_ulong*/(*s++);
|
||||
}
|
||||
|
||||
Memory::CopyToEmu(writeAddr, src, srcSize);
|
||||
if (srcSize < dstSize)
|
||||
{
|
||||
//memset(dst + srcSize, 0, dstSize-srcSize); //zero out bss
|
||||
}
|
||||
Memory::Memset(writeAddr + srcSize, 0, dstSize-srcSize); //zero out bss
|
||||
|
||||
INFO_LOG(MASTER_LOG,"Loadable Segment Copied to %08x, size %08x", writeAddr, p->p_memsz);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
LOG(MASTER_LOG,"%i sections:", header->e_shnum);
|
||||
|
||||
for (int i=0; i<GetNumSections(); i++)
|
||||
{
|
||||
Elf32_Shdr *s = §ions[i];
|
||||
const char *name = GetSectionName(i);
|
||||
|
||||
u32 writeAddr = s->sh_addr + baseAddress;
|
||||
sectionOffsets[i] = writeAddr - vaddr;
|
||||
sectionAddrs[i] = writeAddr;
|
||||
|
||||
if (s->sh_flags & SHF_ALLOC)
|
||||
{
|
||||
LOG(MASTER_LOG,"Data Section found: %s Sitting at %08x, size %08x", name, writeAddr, s->sh_size);
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG(MASTER_LOG,"NonData Section found: %s Ignoring (size=%08x) (flags=%08x)", name, s->sh_size, s->sh_flags);
|
||||
}
|
||||
}
|
||||
*/
|
||||
INFO_LOG(MASTER_LOG,"Done loading.");
|
||||
return true;
|
||||
}
|
||||
|
@ -180,7 +141,7 @@ SectionID ElfReader::GetSectionByName(const char *name, int firstSection) const
|
|||
{
|
||||
for (int i = firstSection; i < header->e_shnum; i++)
|
||||
{
|
||||
const char *secname = GetSectionName(i);
|
||||
const char* secname = GetSectionName(i);
|
||||
|
||||
if (secname != nullptr && strcmp(name, secname) == 0)
|
||||
return i;
|
||||
|
@ -195,10 +156,10 @@ bool ElfReader::LoadSymbols()
|
|||
if (sec != -1)
|
||||
{
|
||||
int stringSection = sections[sec].sh_link;
|
||||
const char *stringBase = (const char *)GetSectionDataPtr(stringSection);
|
||||
const char* stringBase = (const char *)GetSectionDataPtr(stringSection);
|
||||
|
||||
//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);
|
||||
for (int sym = 0; sym < numSymbols; sym++)
|
||||
{
|
||||
|
@ -210,7 +171,7 @@ bool ElfReader::LoadSymbols()
|
|||
int type = symtab[sym].st_info & 0xF;
|
||||
int sectionIndex = Common::swap16(symtab[sym].st_shndx);
|
||||
int value = Common::swap32(symtab[sym].st_value);
|
||||
const char *name = stringBase + Common::swap32(symtab[sym].st_name);
|
||||
const char* name = stringBase + Common::swap32(symtab[sym].st_name);
|
||||
if (bRelocate)
|
||||
value += sectionAddrs[sectionIndex];
|
||||
|
||||
|
|
|
@ -19,19 +19,19 @@ typedef int SectionID;
|
|||
class ElfReader
|
||||
{
|
||||
private:
|
||||
char *base;
|
||||
u32 *base32;
|
||||
char* base;
|
||||
u32* base32;
|
||||
|
||||
Elf32_Ehdr *header;
|
||||
Elf32_Phdr *segments;
|
||||
Elf32_Shdr *sections;
|
||||
Elf32_Ehdr* header;
|
||||
Elf32_Phdr* segments;
|
||||
Elf32_Shdr* sections;
|
||||
|
||||
u32 *sectionAddrs;
|
||||
bool bRelocate;
|
||||
u32 entryPoint;
|
||||
|
||||
public:
|
||||
ElfReader(void *ptr);
|
||||
ElfReader(void* ptr);
|
||||
~ElfReader() { }
|
||||
|
||||
u32 Read32(int off) const { return base32[off>>2]; }
|
||||
|
@ -41,13 +41,13 @@ public:
|
|||
ElfMachine GetMachine() const { return (ElfMachine)(header->e_machine); }
|
||||
u32 GetEntryPoint() const { return entryPoint; }
|
||||
u32 GetFlags() const { return (u32)(header->e_flags); }
|
||||
bool LoadInto(u32 vaddr);
|
||||
bool LoadIntoMemory();
|
||||
bool LoadSymbols();
|
||||
|
||||
int GetNumSegments() const { return (int)(header->e_phnum); }
|
||||
int GetNumSections() const { return (int)(header->e_shnum); }
|
||||
const u8 *GetPtr(int offset) const { return (u8*)base + offset; }
|
||||
const char *GetSectionName(int section) const;
|
||||
const u8* GetPtr(int offset) const { return (u8*)base + offset; }
|
||||
const char* GetSectionName(int section) const;
|
||||
const u8 *GetSectionDataPtr(int section) const
|
||||
{
|
||||
if (section < 0 || section >= header->e_shnum)
|
||||
|
@ -61,13 +61,13 @@ public:
|
|||
{
|
||||
return sections[section].sh_type == SHT_PROGBITS;
|
||||
}
|
||||
const u8 *GetSegmentPtr(int segment)
|
||||
const u8* GetSegmentPtr(int segment)
|
||||
{
|
||||
return GetPtr(segments[segment].p_offset);
|
||||
}
|
||||
u32 GetSectionAddr(SectionID section) const { return sectionAddrs[section]; }
|
||||
int GetSectionSize(SectionID section) const { return sections[section].sh_size; }
|
||||
SectionID GetSectionByName(const char *name, int firstSection = 0) const; //-1 for not found
|
||||
SectionID GetSectionByName(const char* name, int firstSection = 0) const; //-1 for not found
|
||||
|
||||
bool DidRelocate()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue