diff --git a/Elf2Mac/Elf2Mac.cc b/Elf2Mac/Elf2Mac.cc index 11d88ade61..836576fdbb 100644 --- a/Elf2Mac/Elf2Mac.cc +++ b/Elf2Mac/Elf2Mac.cc @@ -98,6 +98,7 @@ int main(int argc, char *argv[]) bool elf2mac = false; bool flatoutput = false; bool segments = true; + bool stripMacsbug = false; vector args2; for(auto p = args.begin(), e = args.end(); p != e; ++p) @@ -133,6 +134,10 @@ int main(int argc, char *argv[]) errx(EXIT_FAILURE, "--mac-segments missing argument"); //segmentMapFile = *p; } + else if(*p == "--mac-strip-macsbug") + { + stripMacsbug = true; + } else { args2.push_back(*p); @@ -151,16 +156,11 @@ int main(int argc, char *argv[]) ofstream out(tmpfile); if(segments) { - segmentMap.CreateLdScript(out); - segmentMap.CreateLdScript(std::cout); - - ofstream f("/tmp/foo.ld"); - segmentMap.CreateLdScript(f); + segmentMap.CreateLdScript(out, stripMacsbug); } else { - CreateFlatLdScript(out); - CreateFlatLdScript(std::cout); + CreateFlatLdScript(out, stripMacsbug); } } @@ -194,5 +194,3 @@ int main(int argc, char *argv[]) } return 0; } - - diff --git a/Elf2Mac/Elf2Mac.h b/Elf2Mac/Elf2Mac.h index 683c2c395a..a46e8d9d47 100644 --- a/Elf2Mac/Elf2Mac.h +++ b/Elf2Mac/Elf2Mac.h @@ -22,6 +22,6 @@ #include -void CreateFlatLdScript(std::ostream& out); +void CreateFlatLdScript(std::ostream& out, bool stripMacsbug); #endif // ELF2MAC_H diff --git a/Elf2Mac/LdScript.cc b/Elf2Mac/LdScript.cc index 8467172f26..05c0dfc8bf 100644 --- a/Elf2Mac/LdScript.cc +++ b/Elf2Mac/LdScript.cc @@ -181,10 +181,16 @@ const char * scriptEnd = R"ld( )ld"; -void CreateFlatLdScript(std::ostream& out) +void CreateFlatLdScript(std::ostream& out, bool stripMacsbug) { out << "_MULTISEG_APP = 0;\n"; - out << scriptStart << textSection << scriptEnd; + out << scriptStart; + if(stripMacsbug) + { + out << "\t.strippedmacsbugnames 0 (NOLOAD) : { *(.text.*.macsbug) }\n"; + out << "\t. = 0;\n"; + } + out << textSection << scriptEnd; } @@ -278,11 +284,15 @@ void SegmentInfo::CreateLdScript(std::ostream &out) } -void SegmentMap::CreateLdScript(std::ostream &out) +void SegmentMap::CreateLdScript(std::ostream &out, bool stripMacsbug) { out << "_MULTISEG_APP = 1;\n"; out << scriptStart; - + if(stripMacsbug) + { + out << "\t.strippedmacsbugnames 0 (NOLOAD) : { *(.text.*.macsbug) }\n"; + out << "\t. = 0;\n"; + } for(SegmentInfo& seg: segments) { seg.CreateLdScript(out); diff --git a/Elf2Mac/Object.cc b/Elf2Mac/Object.cc index c731a76fde..b812441fb5 100644 --- a/Elf2Mac/Object.cc +++ b/Elf2Mac/Object.cc @@ -49,8 +49,6 @@ Object::~Object() { } - - Object::Object(string input) { if(elf_version ( EV_CURRENT ) == EV_NONE) @@ -86,15 +84,14 @@ Object::Object(string input) gelf_getshdr(scn, &shdr); std::string name = elf_strptr(elf, sectionHeaderStringTableIdx, shdr.sh_name); //std::cout << "section #" << idx << ": " << name << std::endl; + //std::cout << " =" << shdr.sh_addr << " + " << shdr.sh_size << std::endl; if(shdr.sh_type == SHT_SYMTAB && !symtab) { symtab.reset(new Symtab(*this, scn)); } - - if(shdr.sh_type == SHT_RELA - && !bssSection) // ignore everything after bss, that's just debug info + if(shdr.sh_type == SHT_RELA) { if(boost::algorithm::starts_with(name,".rela.")) { @@ -103,10 +100,15 @@ Object::Object(string input) sections[progbitsName]->SetRela(scn); } } - if(shdr.sh_type == SHT_PROGBITS - && !bssSection) // ignore everything after bss, that's just debug info + if(shdr.sh_type == SHT_PROGBITS && (shdr.sh_flags & SHF_ALLOC)) { - SectionKind kind = name == ".data" ? SectionKind::data : SectionKind::code; + SectionKind kind = SectionKind::code; + + if(name == ".data") + kind = SectionKind::data; + if(name == ".strippedmacsbugnames") + kind = SectionKind::undefined; + auto section = make_shared
(*this, name, idx, kind, scn); sections[name] = sectionsByElfIndex[idx] = section; @@ -117,10 +119,6 @@ Object::Object(string input) } if(shdr.sh_type == SHT_NOBITS) { - // Currently, the bss section is used - // to know when to start skipping debug info sections. - // (What's the official way to distinguish a debug info section from a "real" section?) - bssSection = sections[name] = sectionsByElfIndex[idx] = make_shared
(*this, name, idx, SectionKind::bss, scn); } diff --git a/Elf2Mac/SegmentMap.h b/Elf2Mac/SegmentMap.h index 4c24ec5da2..7d9e4b97b8 100644 --- a/Elf2Mac/SegmentMap.h +++ b/Elf2Mac/SegmentMap.h @@ -48,7 +48,7 @@ class SegmentMap public: SegmentMap(); - void CreateLdScript(std::ostream& out); + void CreateLdScript(std::ostream& out, bool stripMacsbug); std::string GetSegmentName(int id); };