Merge pull request #1463 from phire/cleanup_ELFloader

Cleanup ElfLoader and BootElf
This commit is contained in:
Ryan Houdek
2015-01-23 21:46:55 -06:00
6 changed files with 70 additions and 129 deletions

View File

@ -324,58 +324,34 @@ bool CBoot::BootUp()
// ELF // ELF
case SCoreStartupParameter::BOOT_ELF: case SCoreStartupParameter::BOOT_ELF:
{ {
if (!File::Exists(_StartupPara.m_strFilename))
{
PanicAlertT("The file you specified (%s) does not exist",
_StartupPara.m_strFilename.c_str());
return false;
}
// Check if we have gotten a Wii file or not
bool elfWii = IsElfWii(_StartupPara.m_strFilename);
if (elfWii != _StartupPara.bWii)
{
PanicAlertT("Warning - starting ELF in wrong console mode!");
}
bool BS2Success = false;
if (elfWii)
{
BS2Success = EmulatedBS2(elfWii);
}
else if (!VolumeHandler::IsWiiDisc() && !_StartupPara.m_strDefaultISO.empty())
{
VolumeHandler::SetVolumeName(_StartupPara.m_strDefaultISO);
BS2Success = EmulatedBS2(elfWii);
}
// load image or create virtual drive from directory // load image or create virtual drive from directory
if (!_StartupPara.m_strDVDRoot.empty()) if (!_StartupPara.m_strDVDRoot.empty())
{ {
NOTICE_LOG(BOOT, "Setting DVDRoot %s", _StartupPara.m_strDVDRoot.c_str()); NOTICE_LOG(BOOT, "Setting DVDRoot %s", _StartupPara.m_strDVDRoot.c_str());
// TODO: auto-convert elf to dol, so we can load them :) VolumeHandler::SetVolumeDirectory(_StartupPara.m_strDVDRoot, _StartupPara.bWii);
VolumeHandler::SetVolumeDirectory(_StartupPara.m_strDVDRoot, elfWii);
BS2Success = EmulatedBS2(elfWii);
} }
else if (!_StartupPara.m_strDefaultISO.empty()) else if (!_StartupPara.m_strDefaultISO.empty())
{ {
NOTICE_LOG(BOOT, "Loading default ISO %s", _StartupPara.m_strDefaultISO.c_str()); NOTICE_LOG(BOOT, "Loading default ISO %s", _StartupPara.m_strDefaultISO.c_str());
VolumeHandler::SetVolumeName(_StartupPara.m_strDefaultISO); VolumeHandler::SetVolumeName(_StartupPara.m_strDefaultISO);
} }
else VolumeHandler::SetVolumeDirectory(_StartupPara.m_strFilename, elfWii); else
{
VolumeHandler::SetVolumeDirectory(_StartupPara.m_strFilename, _StartupPara.bWii);
}
DVDInterface::SetDiscInside(VolumeHandler::IsValid()); DVDInterface::SetDiscInside(VolumeHandler::IsValid());
if (BS2Success) // Poor man's bootup
{ if(_StartupPara.bWii)
HLE::PatchFunctions(); SetupWiiMemory(DiscIO::IVolume::COUNTRY_UNKNOWN);
} else
else // Poor man's bootup EmulatedBS2_GC(true);
{
Load_FST(elfWii); Load_FST(_StartupPara.bWii);
Boot_ELF(_StartupPara.m_strFilename); if(!Boot_ELF(_StartupPara.m_strFilename))
} return false;
UpdateDebugger_MapLoaded(); UpdateDebugger_MapLoaded();
Dolphin_Debugger::AddAutoBreakpoints(); Dolphin_Debugger::AddAutoBreakpoints();
break; break;

View File

@ -52,7 +52,7 @@ private:
static bool Boot_ELF(const std::string& filename); static bool Boot_ELF(const std::string& filename);
static bool Boot_WiiWAD(const std::string& filename); static bool Boot_WiiWAD(const std::string& filename);
static bool EmulatedBS2_GC(); static bool EmulatedBS2_GC(bool skipAppLoader = false);
static bool EmulatedBS2_Wii(); static bool EmulatedBS2_Wii();
static bool EmulatedBS2(bool _bIsWii); static bool EmulatedBS2(bool _bIsWii);
static bool Load_BS2(const std::string& _rBootROMFilename); static bool Load_BS2(const std::string& _rBootROMFilename);

View File

@ -34,7 +34,7 @@ void CBoot::RunFunction(u32 _iAddr)
// GameCube Bootstrap 2 HLE: // GameCube Bootstrap 2 HLE:
// copy the apploader to 0x81200000 // copy the apploader to 0x81200000
// execute the apploader, function by function, using the above utility. // execute the apploader, function by function, using the above utility.
bool CBoot::EmulatedBS2_GC() bool CBoot::EmulatedBS2_GC(bool skipAppLoader)
{ {
INFO_LOG(BOOT, "Faking GC BS2..."); INFO_LOG(BOOT, "Faking GC BS2...");
@ -84,7 +84,7 @@ bool CBoot::EmulatedBS2_GC()
u32 iAppLoaderOffset = 0x2440; u32 iAppLoaderOffset = 0x2440;
u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10, false); u32 iAppLoaderEntry = VolumeHandler::Read32(iAppLoaderOffset + 0x10, false);
u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14, false) + VolumeHandler::Read32(iAppLoaderOffset + 0x18, false); u32 iAppLoaderSize = VolumeHandler::Read32(iAppLoaderOffset + 0x14, false) + VolumeHandler::Read32(iAppLoaderOffset + 0x18, false);
if ((iAppLoaderEntry == (u32)-1) || (iAppLoaderSize == (u32)-1)) if ((iAppLoaderEntry == (u32)-1) || (iAppLoaderSize == (u32)-1) || skipAppLoader)
{ {
INFO_LOG(BOOT, "GC BS2: Not running apploader!"); INFO_LOG(BOOT, "GC BS2: Not running apploader!");
return false; return false;

View File

@ -3,6 +3,7 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include "Common/FileUtil.h" #include "Common/FileUtil.h"
#include "Common/StdMakeUnique.h"
#include "Core/Boot/Boot.h" #include "Core/Boot/Boot.h"
#include "Core/Boot/ElfReader.h" #include "Core/Boot/ElfReader.h"
@ -14,12 +15,12 @@ bool CBoot::IsElfWii(const std::string& filename)
/* We already check if filename existed before we called this function, so /* We already check if filename existed before we called this function, so
there is no need for another check, just read the file right away */ there is no need for another check, just read the file right away */
const u64 filesize = File::GetSize(filename); size_t filesize = File::GetSize(filename);
std::vector<u8> mem((size_t)filesize); auto elf = std::make_unique<u8 []>(filesize);
{ {
File::IOFile f(filename, "rb"); File::IOFile f(filename, "rb");
f.ReadBytes(mem.data(), (size_t)filesize); f.ReadBytes(elf.get(), filesize);
} }
// Use the same method as the DOL loader uses: search for mfspr from HID4, // Use the same method as the DOL loader uses: search for mfspr from HID4,
@ -28,24 +29,23 @@ bool CBoot::IsElfWii(const std::string& filename)
// Likely to have some false positives/negatives, patches implementing a // Likely to have some false positives/negatives, patches implementing a
// better heuristic are welcome. // better heuristic are welcome.
u32 HID4_pattern = 0x7c13fba6; // Swap these once, instead of swapping every word in the file.
u32 HID4_mask = 0xfc1fffff; u32 HID4_pattern = Common::swap32(0x7c13fba6);
ElfReader reader(mem.data()); u32 HID4_mask = Common::swap32(0xfc1fffff);
ElfReader reader(elf.get());
for (int i = 0; i < reader.GetNumSections(); ++i) for (int i = 0; i < reader.GetNumSections(); ++i)
{ {
if (reader.IsCodeSection(i)) if (reader.IsCodeSection(i))
{ {
for (unsigned int j = 0; j < reader.GetSectionSize(i) / sizeof (u32); ++j) u32* code = (u32*)reader.GetSectionDataPtr(i);
{ for (u32 j = 0; j < reader.GetSectionSize(i) / sizeof(u32); ++j)
u32 word = Common::swap32(((u32*)reader.GetSectionDataPtr(i))[j]);
if ((word & HID4_mask) == HID4_pattern)
{ {
if ((code[j] & HID4_mask) == HID4_pattern)
return true; return true;
} }
} }
} }
}
return false; return false;
} }
@ -53,16 +53,20 @@ bool CBoot::IsElfWii(const std::string& filename)
bool CBoot::Boot_ELF(const std::string& filename) bool CBoot::Boot_ELF(const std::string& filename)
{ {
const u64 filesize = File::GetSize(filename); // Read ELF from file
std::vector<u8> mem((size_t)filesize); size_t filesize = File::GetSize(filename);
auto elf = std::make_unique<u8 []>(filesize);
{ {
File::IOFile f(filename, "rb"); File::IOFile f(filename, "rb");
f.ReadBytes(mem.data(), (size_t)filesize); f.ReadBytes(elf.get(), filesize);
} }
ElfReader reader(mem.data()); // Load ELF into GameCube Memory
reader.LoadInto(0x80000000); ElfReader reader(elf.get());
if(!reader.LoadIntoMemory())
return false;
if (!reader.LoadSymbols()) if (!reader.LoadSymbols())
{ {
if (LoadMapFromFilename()) if (LoadMapFromFilename())

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 how 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); }