diff --git a/Elf2Mac/Elf2Mac.cc b/Elf2Mac/Elf2Mac.cc index 0611c194db..e7010c991e 100644 --- a/Elf2Mac/Elf2Mac.cc +++ b/Elf2Mac/Elf2Mac.cc @@ -95,6 +95,7 @@ int main(int argc, char *argv[]) if(boost::algorithm::ends_with(argv[0], "ld")) { string outputFile = "a.out"; + string entryPoint = "_start"; bool elf2mac = false; bool flatoutput = false; bool segments = true; @@ -116,10 +117,21 @@ int main(int argc, char *argv[]) { outputFile = (*p).substr(2); } - else if(*p == "-elf2mac") + else if(*p == "-elf2mac" || *p == "--elf2mac") { elf2mac = true; } + else if(*p == "-e") + { + ++p; + if(p == e) + errx(EXIT_FAILURE, "-e missing argument"); + entryPoint = *p; + } + else if(boost::algorithm::starts_with(*p, "-e")) + { + entryPoint = (*p).substr(2); + } else if(*p == "--mac-flat") { elf2mac = true; @@ -163,12 +175,11 @@ int main(int argc, char *argv[]) ofstream out(tmpfile); if(segments) { - segmentMap.CreateLdScript(out, stripMacsbug); - segmentMap.CreateLdScript(std::cout, stripMacsbug); + segmentMap.CreateLdScript(out, entryPoint, stripMacsbug); } else { - CreateFlatLdScript(out, stripMacsbug); + CreateFlatLdScript(out, entryPoint, stripMacsbug); } } diff --git a/Elf2Mac/Elf2Mac.h b/Elf2Mac/Elf2Mac.h index a46e8d9d47..3602ae139a 100644 --- a/Elf2Mac/Elf2Mac.h +++ b/Elf2Mac/Elf2Mac.h @@ -21,7 +21,8 @@ #define ELF2MAC_H #include +#include -void CreateFlatLdScript(std::ostream& out, bool stripMacsbug); +void CreateFlatLdScript(std::ostream& out, std::string entryPoint, bool stripMacsbug); #endif // ELF2MAC_H diff --git a/Elf2Mac/LdScript.cc b/Elf2Mac/LdScript.cc index 05c0dfc8bf..146bfb3152 100644 --- a/Elf2Mac/LdScript.cc +++ b/Elf2Mac/LdScript.cc @@ -28,7 +28,7 @@ using std::string; const char * scriptStart = R"ld(/* ld script for Elf2Mac */ -ENTRY( _start ) +ENTRY( @entryPoint@ ) SECTIONS { )ld"; @@ -45,7 +45,7 @@ const char * textSection = R"ld(/* ld script for Elf2Mac */ SHORT(DEFINED(__break_on_entry) ? 0xA9FF : 0x4e71); LONG(0x61000002); /* bsr *+2 */ SHORT(0x0697); /* addi.l #_, (a7) */ - LONG(_start - _entry_trampoline - 6); + LONG(@entryPoint@ - _entry_trampoline - 6); PROVIDE(_start = .); /* fallback entry point to a safe spot - needed for libretro bootstrap */ Retro68InitMultisegApp = .; /* override this for the single-segment case */ @@ -181,16 +181,16 @@ const char * scriptEnd = R"ld( )ld"; -void CreateFlatLdScript(std::ostream& out, bool stripMacsbug) +void CreateFlatLdScript(std::ostream& out, string entryPoint, bool stripMacsbug) { out << "_MULTISEG_APP = 0;\n"; - out << scriptStart; + out << boost::replace_all_copy(scriptStart, "@entryPoint@", entryPoint); if(stripMacsbug) { out << "\t.strippedmacsbugnames 0 (NOLOAD) : { *(.text.*.macsbug) }\n"; out << "\t. = 0;\n"; } - out << textSection << scriptEnd; + out << boost::replace_all_copy(textSection, "@entryPoint@", entryPoint) << scriptEnd; } @@ -211,13 +211,13 @@ void SegmentInfo::WriteFiltersKeep(std::ostream &out, string section) } } -void SegmentInfo::CreateLdScript(std::ostream &out) +void SegmentInfo::CreateLdScript(std::ostream &out, string entryPoint) { out << "\t.code" << id << " : {\n"; out << "\t\tFILL(0x4E71);\n"; if(id == 1) { - out << R"ld( + out << boost::replace_all_copy(R"ld( _stext = .; FILL(0x4E71); PROVIDE(_rsrc_start = .); @@ -226,14 +226,14 @@ void SegmentInfo::CreateLdScript(std::ostream &out) SHORT(DEFINED(__break_on_entry) ? 0xA9FF : 0x4e71); LONG(0x61000002); /* bsr *+2 */ SHORT(0x0697); /* addi.l #_, (a7) */ - LONG(_start - _entry_trampoline - 6); + LONG(@entryPoint@ - _entry_trampoline - 6); PROVIDE(_start = .); /* fallback entry point to a safe spot - needed for libretro bootstrap */ SHORT(0x4e75); /* rts */ FILL(0); *(.relocvars) FILL(0x4E71); -)ld"; +)ld", "@entryPoint@", entryPoint); } WriteFilters(out, ".text"); @@ -284,10 +284,10 @@ void SegmentInfo::CreateLdScript(std::ostream &out) } -void SegmentMap::CreateLdScript(std::ostream &out, bool stripMacsbug) +void SegmentMap::CreateLdScript(std::ostream &out, string entryPoint, bool stripMacsbug) { out << "_MULTISEG_APP = 1;\n"; - out << scriptStart; + out << boost::replace_all_copy(scriptStart, "@entryPoint@", entryPoint); if(stripMacsbug) { out << "\t.strippedmacsbugnames 0 (NOLOAD) : { *(.text.*.macsbug) }\n"; @@ -295,7 +295,7 @@ void SegmentMap::CreateLdScript(std::ostream &out, bool stripMacsbug) } for(SegmentInfo& seg: segments) { - seg.CreateLdScript(out); + seg.CreateLdScript(out, entryPoint); } out << scriptEnd; diff --git a/Elf2Mac/SegmentMap.h b/Elf2Mac/SegmentMap.h index 4dcc05e6a7..4e4e21b3ad 100644 --- a/Elf2Mac/SegmentMap.h +++ b/Elf2Mac/SegmentMap.h @@ -39,7 +39,7 @@ public: void WriteFilters(std::ostream& out, std::string section); void WriteFiltersKeep(std::ostream& out, std::string section); - void CreateLdScript(std::ostream& out); + void CreateLdScript(std::ostream& out, std::string entryPoint); }; class SegmentMap @@ -49,7 +49,7 @@ public: SegmentMap(); SegmentMap(std::string filename); - void CreateLdScript(std::ostream& out, bool stripMacsbug); + void CreateLdScript(std::ostream& out, std::string entryPoint, bool stripMacsbug); std::string GetSegmentName(int id); };