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:
Scott Mansell 2014-11-01 22:48:23 +13:00
parent 1e5762b163
commit 2bf052d8b9
3 changed files with 33 additions and 72 deletions

View File

@ -65,7 +65,7 @@ bool CBoot::Boot_ELF(const std::string& filename)
// Load ELF into GameCube Memory // Load ELF into GameCube Memory
ElfReader reader(elf.get()); ElfReader reader(elf.get());
reader.LoadInto(0x80000000); reader.LoadIntoMemory();
if (!reader.LoadSymbols()) if (!reader.LoadSymbols())
{ {

View File

@ -94,84 +94,45 @@ const char *ElfReader::GetSectionName(int section) const
return nullptr; 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); DEBUG_LOG(MASTER_LOG,"String section: %i", header->e_shstrndx);
// sectionOffsets = new u32[GetNumSections()];
// sectionAddrs = new u32[GetNumSections()];
// Should we relocate? // Should we relocate?
bRelocate = (header->e_type != ET_EXEC); bRelocate = (header->e_type != ET_EXEC);
if (bRelocate) if (bRelocate)
{ {
DEBUG_LOG(MASTER_LOG,"Relocatable module"); PanicAlert("Error: Dolphin doesn't know to load a relocatable elf.");
entryPoint += vaddr; return false;
}
else
{
DEBUG_LOG(MASTER_LOG,"Prerelocated executable");
} }
INFO_LOG(MASTER_LOG,"%i segments:", header->e_phnum); INFO_LOG(MASTER_LOG,"%i segments:", header->e_phnum);
// First pass : Get the bits into RAM // Copy segments into ram.
u32 segmentVAddr[32];
u32 baseAddress = bRelocate?vaddr:0;
for (int i = 0; i < header->e_phnum; i++) 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) if (p->p_type == PT_LOAD)
{ {
segmentVAddr[i] = baseAddress + p->p_vaddr; u32 writeAddr = p->p_vaddr;
u32 writeAddr = segmentVAddr[i];
const u8* src = GetSegmentPtr(i); const u8* src = GetSegmentPtr(i);
u8 *dst = Memory::GetPointer(writeAddr);
u32 srcSize = p->p_filesz; u32 srcSize = p->p_filesz;
u32 dstSize = p->p_memsz; u32 dstSize = p->p_memsz;
u32 *s = (u32*)src;
u32 *d = (u32*)dst; Memory::CopyToEmu(writeAddr, src, srcSize);
for (int j = 0; j < (int)(srcSize + 3) / 4; j++)
{
*d++ = /*_byteswap_ulong*/(*s++);
}
if (srcSize < dstSize) if (srcSize < dstSize)
{ Memory::Memset(writeAddr + srcSize, 0, dstSize-srcSize); //zero out bss
//memset(dst + srcSize, 0, dstSize-srcSize); //zero out bss
}
INFO_LOG(MASTER_LOG,"Loadable Segment Copied to %08x, size %08x", writeAddr, p->p_memsz); 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 = &sections[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."); INFO_LOG(MASTER_LOG,"Done loading.");
return true; return true;
} }

View File

@ -41,7 +41,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); bool LoadIntoMemory();
bool LoadSymbols(); bool LoadSymbols();
int GetNumSegments() const { return (int)(header->e_phnum); } int GetNumSegments() const { return (int)(header->e_phnum); }