X-Git-Url: https://git.saurik.com/apple/ld64.git/blobdiff_plain/2a0ed0a31db9961dcb7c87964091b22401c4d69b..0a8dc3df050bd5c0a70486b9ebdb9dccce439f3e:/src/ld/parsers/lto_file.cpp diff --git a/src/ld/parsers/lto_file.cpp b/src/ld/parsers/lto_file.cpp index f0f5f10..a5ab821 100644 --- a/src/ld/parsers/lto_file.cpp +++ b/src/ld/parsers/lto_file.cpp @@ -36,6 +36,8 @@ #include #include #include +#include +#include #include "MachOFileAbstraction.hpp" #include "Architectures.hpp" @@ -63,14 +65,13 @@ class InternalAtom : public ld::Atom public: InternalAtom(class File& f); // overrides of ld::Atom - virtual ld::File* file() const { return &_file; } - virtual const char* name() const { return "import-atom"; } - virtual uint64_t size() const { return 0; } - virtual uint64_t objectAddress() const { return 0; } - virtual void copyRawContent(uint8_t buffer[]) const { } - virtual void setScope(Scope) { } - virtual ld::Fixup::iterator fixupsBegin() const { return &_undefs[0]; } - virtual ld::Fixup::iterator fixupsEnd() const { return &_undefs[_undefs.size()]; } + ld::File* file() const override { return &_file; } + const char* name() const override { return "import-atom"; } + uint64_t size() const override { return 0; } + uint64_t objectAddress() const override { return 0; } + void copyRawContent(uint8_t buffer[]) const override { } + ld::Fixup::iterator fixupsBegin() const override { return &_undefs[0]; } + ld::Fixup::iterator fixupsEnd() const override { return &_undefs[_undefs.size()]; } // for adding references to symbols outside bitcode file void addReference(const char* nm) @@ -91,24 +92,25 @@ class File : public ld::relocatable::File public: File(const char* path, time_t mTime, ld::File::Ordinal ordinal, const uint8_t* content, uint32_t contentLength, cpu_type_t arch); - virtual ~File(); + ~File() override; // overrides of ld::File - virtual bool forEachAtom(ld::File::AtomHandler&) const; - virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const + bool forEachAtom(ld::File::AtomHandler&) const override; + bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const override { return false; } - virtual uint32_t cpuSubType() const { return _cpuSubType; } + uint32_t cpuSubType() const override { return _cpuSubType; } // overrides of ld::relocatable::File - virtual DebugInfoKind debugInfo() const { return _debugInfo; } - virtual const char* debugInfoPath() const { return _debugInfoPath; } - virtual time_t debugInfoModificationTime() const + DebugInfoKind debugInfo() const override { return _debugInfo; } + const char* debugInfoPath() const override { return _debugInfoPath; } + time_t debugInfoModificationTime() const override { return _debugInfoModTime; } - virtual const std::vector* stabs() const { return NULL; } - virtual bool canScatterAtoms() const { return true; } - virtual LinkerOptionsList* linkerOptions() const { return NULL; } - - + const std::vector* stabs() const override { return NULL; } + bool canScatterAtoms() const override { return true; } + LinkerOptionsList* linkerOptions() const override { return NULL; } + bool isThinLTO() const { return _isThinLTO; } + void setIsThinLTO(bool ThinLTO) { _isThinLTO = ThinLTO; } + // fixme rdar://24734472 objCConstraint() and objcHasCategoryClassProperties() void release(); lto_module_t module() { return _module; } class InternalAtom& internalAtom() { return _internalAtom; } @@ -122,11 +124,15 @@ public: static bool sSupportsLocalContext; static bool sHasTriedLocalContext; bool mergeIntoGenerator(lto_code_gen_t generator, bool useSetModule); +#if LTO_API_VERSION >= 18 + void addToThinGenerator(thinlto_code_gen_t generator); +#endif private: friend class Atom; friend class InternalAtom; friend class Parser; - + + bool _isThinLTO; cpu_type_t _architecture; class InternalAtom _internalAtom; class Atom* _atomArray; @@ -157,31 +163,32 @@ public: ld::Atom::Definition d, ld::Atom::Combine c, ld::Atom::Alignment a, bool ah); // overrides of ld::Atom - virtual ld::File* file() const { return &_file; } - virtual const char* translationUnitSource() const + const ld::File* file() const override { return (_compiledAtom ? _compiledAtom->file() : &_file ); } + const ld::File* originalFile() const override { return &_file; } + const char* translationUnitSource() const override { return (_compiledAtom ? _compiledAtom->translationUnitSource() : NULL); } - virtual const char* name() const { return _name; } - virtual uint64_t size() const { return (_compiledAtom ? _compiledAtom->size() : 0); } - virtual uint64_t objectAddress() const { return (_compiledAtom ? _compiledAtom->objectAddress() : 0); } - virtual void copyRawContent(uint8_t buffer[]) const + const char* name() const override { return _name; } + uint64_t size() const override { return (_compiledAtom ? _compiledAtom->size() : 0); } + uint64_t objectAddress() const override { return (_compiledAtom ? _compiledAtom->objectAddress() : 0); } + void copyRawContent(uint8_t buffer[]) const override { if (_compiledAtom) _compiledAtom->copyRawContent(buffer); } - virtual const uint8_t* rawContentPointer() const + const uint8_t* rawContentPointer() const override { return (_compiledAtom ? _compiledAtom->rawContentPointer() : NULL); } - virtual unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const + unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const override { return (_compiledAtom ? _compiledAtom->contentHash(ibt) : 0); } - virtual bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const + bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const override { return (_compiledAtom ? _compiledAtom->canCoalesceWith(rhs,ibt) : false); } - virtual ld::Fixup::iterator fixupsBegin() const + ld::Fixup::iterator fixupsBegin() const override { return (_compiledAtom ? _compiledAtom->fixupsBegin() : (ld::Fixup*)&_file._fixupToInternal); } - virtual ld::Fixup::iterator fixupsEnd() const + ld::Fixup::iterator fixupsEnd() const override { return (_compiledAtom ? _compiledAtom->fixupsEnd() : &((ld::Fixup*)&_file._fixupToInternal)[1]); } - virtual ld::Atom::UnwindInfo::iterator beginUnwind() const + ld::Atom::UnwindInfo::iterator beginUnwind() const override { return (_compiledAtom ? _compiledAtom->beginUnwind() : NULL); } - virtual ld::Atom::UnwindInfo::iterator endUnwind() const + ld::Atom::UnwindInfo::iterator endUnwind() const override { return (_compiledAtom ? _compiledAtom->endUnwind() : NULL); } - virtual ld::Atom::LineInfo::iterator beginLineInfo() const + ld::Atom::LineInfo::iterator beginLineInfo() const override { return (_compiledAtom ? _compiledAtom->beginLineInfo() : NULL); } - virtual ld::Atom::LineInfo::iterator endLineInfo() const + ld::Atom::LineInfo::iterator endLineInfo() const override { return (_compiledAtom ? _compiledAtom->endLineInfo() : NULL); } const ld::Atom* compiledAtom() { return _compiledAtom; } @@ -216,11 +223,13 @@ public: std::vector& newAtoms, std::vector& additionalUndefines); - static const char* ltoVersion() { return ::lto_get_version(); } + static const char* ltoVersion() { return ::lto_get_version(); } private: + static const char* tripletPrefixForArch(cpu_type_t arch); - static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options); + static ld::relocatable::File* parseMachOFile(const uint8_t* p, size_t len, const std::string &path, const OptimizeOptions& options, + ld::File::Ordinal ordinal); #if LTO_API_VERSION >= 7 static void ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t, const char*, void*); #endif @@ -231,23 +240,70 @@ private: class AtomSyncer : public ld::File::AtomHandler { public: AtomSyncer(std::vector& a, std::vector&na, - CStringToAtom la, CStringToAtom dla, const OptimizeOptions& options) : - _options(options), _additionalUndefines(a), _newAtoms(na), _llvmAtoms(la), _deadllvmAtoms(dla) { } - virtual void doAtom(const class ld::Atom&); - virtual void doFile(const class ld::File&) { } + const CStringToAtom &la, const CStringToAtom &dla, const OptimizeOptions& options) : + _options(options), _additionalUndefines(a), _newAtoms(na), _llvmAtoms(la), _deadllvmAtoms(dla), _lastProxiedAtom(NULL), _lastProxiedFile(NULL) {} + void doAtom(const class ld::Atom&) override; + void doFile(const class ld::File&) override { } - const OptimizeOptions& _options; std::vector& _additionalUndefines; std::vector& _newAtoms; - CStringToAtom _llvmAtoms; - CStringToAtom _deadllvmAtoms; + const CStringToAtom &_llvmAtoms; + const CStringToAtom &_deadllvmAtoms; + const ld::Atom* _lastProxiedAtom; + const ld::File* _lastProxiedFile; }; + static void setPreservedSymbols(const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + CStringToAtom &deadllvmAtoms, + CStringToAtom &llvmAtoms, + lto_code_gen_t generator); + + static std::tuple codegen(const OptimizeOptions& options, + ld::Internal& state, + lto_code_gen_t generator, + std::string& object_path); + + + static void loadMachO(ld::relocatable::File* machoFile, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines, + CStringToAtom &llvmAtoms, + CStringToAtom &deadllvmAtoms); + + static bool optimizeLTO(const std::vector files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines); + + static bool optimizeThinLTO(const std::vector& Files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines); + + static thinlto_code_gen_t init_thinlto_codegen(const std::vector& files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + CStringToAtom& deadllvmAtoms, + CStringToAtom& llvmAtoms); + static std::vector _s_files; + static bool _s_llvmOptionsProcessed; }; std::vector Parser::_s_files; +bool Parser::_s_llvmOptionsProcessed = false; bool Parser::validFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t architecture, cpu_subtype_t subarch) @@ -298,7 +354,8 @@ File* Parser::parse(const uint8_t* fileContent, uint64_t fileLength, const char* } -ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const OptimizeOptions& options) +ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, const std::string &path, const OptimizeOptions& options, + ld::File::Ordinal ordinal) { mach_o::relocatable::ParserOptions objOpts; objOpts.architecture = options.arch; @@ -313,23 +370,23 @@ ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, cons objOpts.simulator = options.simulator; objOpts.ignoreMismatchPlatform = options.ignoreMismatchPlatform; objOpts.platform = options.platform; + objOpts.minOSVersion = options.minOSVersion; objOpts.subType = 0; objOpts.srcKind = ld::relocatable::File::kSourceLTO; objOpts.treateBitcodeAsData = false; objOpts.usingBitcode = options.bitcodeBundle; objOpts.maxDefaultCommonAlignment = options.maxDefaultCommonAlignment; - // mach-o parsing is done in-memory, but need path for debug notes - const char* path = "/tmp/lto.o"; + const char *object_path = path.c_str(); + if (path.empty()) + object_path = "/tmp/lto.o"; + time_t modTime = 0; - if ( options.tmpObjectFilePath != NULL ) { - path = options.tmpObjectFilePath; - struct stat statBuffer; - if ( stat(options.tmpObjectFilePath, &statBuffer) == 0 ) - modTime = statBuffer.st_mtime; - } - - ld::relocatable::File* result = mach_o::relocatable::parse(p, len, path, modTime, ld::File::Ordinal::LTOOrdinal(), objOpts); + struct stat statBuffer; + if ( stat(object_path, &statBuffer) == 0 ) + modTime = statBuffer.st_mtime; + + ld::relocatable::File* result = mach_o::relocatable::parse(p, len, strdup(object_path), modTime, ordinal, objOpts); if ( result != NULL ) return result; throw "LLVM LTO, file is not of required architecture"; @@ -338,7 +395,7 @@ ld::relocatable::File* Parser::parseMachOFile(const uint8_t* p, size_t len, cons File::File(const char* pth, time_t mTime, ld::File::Ordinal ordinal, const uint8_t* content, uint32_t contentLength, cpu_type_t arch) - : ld::relocatable::File(pth,mTime,ordinal), _architecture(arch), _internalAtom(*this), + : ld::relocatable::File(pth,mTime,ordinal), _isThinLTO(false), _architecture(arch), _internalAtom(*this), _atomArray(NULL), _atomArrayCount(0), _module(NULL), _path(pth), _content(content), _contentLength(contentLength), _debugInfoPath(pth), _section("__TEXT_", "__tmp_lto", ld::Section::typeTempLTO), @@ -367,7 +424,11 @@ File::File(const char* pth, time_t mTime, ld::File::Ordinal ordinal, const uint8 throwf("could not parse object file %s: '%s', using libLTO version '%s'", pth, ::lto_get_error_message(), ::lto_get_version()); if ( log ) fprintf(stderr, "bitcode file: %s\n", pth); - + +#if LTO_API_VERSION >= 18 + _isThinLTO = ::lto_module_is_thinlto(_module); +#endif + // create atom for each global symbol in module uint32_t count = ::lto_module_get_num_symbols(_module); _atomArray = (Atom*)malloc(sizeof(Atom)*count); @@ -481,6 +542,13 @@ bool File::mergeIntoGenerator(lto_code_gen_t generator, bool useSetModule) { return false; } +#if LTO_API_VERSION >= 18 +void File::addToThinGenerator(thinlto_code_gen_t generator) { + assert(!_module && "Expected module to be disposed"); + ::thinlto_codegen_add_module(generator, _path, (const char *)_content, _contentLength); +} +#endif + void File::release() { if ( _module != NULL ) @@ -556,79 +624,27 @@ void Parser::ltoDiagnosticHandler(lto_codegen_diagnostic_severity_t severity, co } #endif -bool Parser::optimize( const std::vector& allAtoms, - ld::Internal& state, - const OptimizeOptions& options, - ld::File::AtomHandler& handler, - std::vector& newAtoms, - std::vector& additionalUndefines) -{ - const bool logMustPreserve = false; - const bool logExtraOptions = false; - const bool logBitcodeFiles = false; - const bool logAtomsBeforeSync = false; - - // exit quickly if nothing to do - if ( _s_files.size() == 0 ) - return false; - - // print out LTO version string if -v was used - if ( options.verbose ) - fprintf(stderr, "%s\n", ::lto_get_version()); - - // create optimizer and add each Reader - lto_code_gen_t generator = NULL; -#if LTO_API_VERSION >= 11 - if ( File::sSupportsLocalContext ) - generator = ::lto_codegen_create_in_local_context(); - else -#endif - generator = ::lto_codegen_create(); -#if LTO_API_VERSION >= 7 - lto_codegen_set_diagnostic_handler(generator, ltoDiagnosticHandler, NULL); -#endif - - // The order that files are merged must match command line order - std::sort(_s_files.begin(), _s_files.end(), CommandLineOrderFileSorter()); - ld::File::Ordinal lastOrdinal; - - // When flto_codegen_only is on and we have a single .bc file, use lto_codegen_set_module instead of - // lto_codegen_add_module, to make sure the the destination module will be the same as the input .bc file. - bool useSetModule = false; -#if LTO_API_VERSION >= 13 - useSetModule = (_s_files.size() == 1) && options.ltoCodegenOnly && (::lto_api_version() >= 13); -#endif - for (std::vector::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { - File* f = *it; - assert(f->ordinal() > lastOrdinal); - if ( logBitcodeFiles && !useSetModule) fprintf(stderr, "lto_codegen_add_module(%s)\n", f->path()); - if ( logBitcodeFiles && useSetModule) fprintf(stderr, "lto_codegen_set_module(%s)\n", f->path()); - if ( f->mergeIntoGenerator(generator, useSetModule) ) - throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", f->path(), ::lto_get_error_message(), ::lto_get_version()); - lastOrdinal = f->ordinal(); - } - - // add any -mllvm command line options - for (std::vector::const_iterator it=options.llvmOptions->begin(); it != options.llvmOptions->end(); ++it) { - if ( logExtraOptions ) fprintf(stderr, "passing option to llvm: %s\n", *it); - ::lto_codegen_debug_options(generator, *it); - } - // Need a way for LTO to get cpu variants (until that info is in bitcode) - if ( options.mcpu != NULL ) - ::lto_codegen_set_cpu(generator, options.mcpu); +/// Instruct libLTO about the list of symbols to preserve, compute deadllvmAtoms and llvmAtoms +void Parser::setPreservedSymbols( const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + CStringToAtom &deadllvmAtoms, + CStringToAtom &llvmAtoms, + lto_code_gen_t generator) { + const bool logMustPreserve = false; - // The atom graph uses directed edges (references). Collect all references where - // originating atom is not part of any LTO Reader. This allows optimizer to optimize an - // external (i.e. not originated from same .o file) reference if all originating atoms are also + // The atom graph uses directed edges (references). Collect all references where + // originating atom is not part of any LTO Reader. This allows optimizer to optimize an + // external (i.e. not originated from same .o file) reference if all originating atoms are also // defined in llvm bitcode file. CStringSet nonLLVMRefs; - CStringToAtom llvmAtoms; - bool hasNonllvmAtoms = false; + bool hasNonllvmAtoms = false; for (std::vector::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) { const ld::Atom* atom = *it; - // only look at references that come from an atom that is not an llvm atom - if ( atom->contentType() != ld::Atom::typeLTOtemporary ) { + // only look at references that come from an atom that is not an LTO atom + if (atom->contentType() != ld::Atom::typeLTOtemporary || + ((lto::File *)atom->file())->isThinLTO()) { if ( (atom->section().type() != ld::Section::typeMachHeader) && (atom->definition() != ld::Atom::definitionProxy) ) { hasNonllvmAtoms = true; } @@ -637,7 +653,7 @@ bool Parser::optimize( const std::vector& allAtoms, switch ( fit->binding ) { case ld::Fixup::bindingDirectlyBound: // that reference an llvm atom - if ( fit->u.target->contentType() == ld::Atom::typeLTOtemporary ) + if ( fit->u.target->contentType() == ld::Atom::typeLTOtemporary ) nonLLVMRefs.insert(fit->u.target->name()); break; case ld::Fixup::bindingsIndirectlyBound: @@ -655,15 +671,14 @@ bool Parser::optimize( const std::vector& allAtoms, } // if entry point is in a llvm bitcode file, it must be preserved by LTO if ( state.entryPoint!= NULL ) { - if ( state.entryPoint->contentType() == ld::Atom::typeLTOtemporary ) + if ( state.entryPoint->contentType() == ld::Atom::typeLTOtemporary ) nonLLVMRefs.insert(state.entryPoint->name()); } - + // deadAtoms are the atoms that the linker coalesced. For instance weak or tentative definitions // overriden by another atom. If any of these deadAtoms are llvm atoms and they were replaced - // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead + // with a mach-o atom, we need to tell the lto engine to preserve (not optimize away) its dead // atom so that the linker can replace it with the mach-o one later. - CStringToAtom deadllvmAtoms; for (std::vector::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) { const ld::Atom* atom = *it; if ( atom->coalescedAway() && (atom->contentType() == ld::Atom::typeLTOtemporary) ) { @@ -680,7 +695,7 @@ bool Parser::optimize( const std::vector& allAtoms, if ( llvmAtom->coalescedAway() ) { const char* name = llvmAtom->name(); if ( deadllvmAtoms.find(name) == deadllvmAtoms.end() ) { - if ( logMustPreserve ) + if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name); ::lto_codegen_add_must_preserve_symbol(generator, name); deadllvmAtoms[name] = (Atom*)llvmAtom; @@ -692,7 +707,7 @@ bool Parser::optimize( const std::vector& allAtoms, } } } - + // tell code generator about symbols that must be preserved for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) { const char* name = it->first; @@ -701,7 +716,7 @@ bool Parser::optimize( const std::vector& allAtoms, // 1 - atom scope is global (and not linkage unit). // 2 - included in nonLLVMRefs set. // If a symbol is not listed in exportList then LTO is free to optimize it away. - if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) { + if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) { if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name); ::lto_codegen_add_must_preserve_symbol(generator, name); } @@ -715,15 +730,15 @@ bool Parser::optimize( const std::vector& allAtoms, ::lto_codegen_add_must_preserve_symbol(generator, name); } } - + // tell code generator to preserve initial undefines for( std::vector::const_iterator it=options.initialUndefines->begin(); it != options.initialUndefines->end(); ++it) { if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because it is an initial undefine\n", *it); ::lto_codegen_add_must_preserve_symbol(generator, *it); } - // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o) - if ( options.relocatable && !hasNonllvmAtoms ) { + // special case running ld -r on all bitcode files to produce another bitcode file (instead of mach-o) + if ( options.relocatable && !hasNonllvmAtoms ) { #if LTO_API_VERSION >= 15 ::lto_codegen_set_should_embed_uselists(generator, false); #endif @@ -732,44 +747,56 @@ bool Parser::optimize( const std::vector& allAtoms, exit(0); } warning("could not produce merged bitcode file"); - } - - // set code-gen model - lto_codegen_model model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; + } + +} + +// Retrieve the codegen model from the options +static lto_codegen_model getCodeModel(const OptimizeOptions& options) { if ( options.mainExecutable ) { if ( options.staticExecutable ) { // x86_64 "static" or any "-static -pie" is really dynamic code model if ( (options.arch == CPU_TYPE_X86_64) || options.pie ) - model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; + return LTO_CODEGEN_PIC_MODEL_DYNAMIC; else - model = LTO_CODEGEN_PIC_MODEL_STATIC; + return LTO_CODEGEN_PIC_MODEL_STATIC; } else { if ( options.pie ) - model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; + return LTO_CODEGEN_PIC_MODEL_DYNAMIC; else - model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; + return LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; } } else { if ( options.allowTextRelocs ) - model = LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; + return LTO_CODEGEN_PIC_MODEL_DYNAMIC_NO_PIC; else - model = LTO_CODEGEN_PIC_MODEL_DYNAMIC; + return LTO_CODEGEN_PIC_MODEL_DYNAMIC; } - if ( ::lto_codegen_set_pic_model(generator, model) ) + +} + +std::tuple Parser::codegen(const OptimizeOptions& options, + ld::Internal& state, + lto_code_gen_t generator, + std::string& object_path) { + uint8_t *machOFile; + size_t machOFileLen; + + if ( ::lto_codegen_set_pic_model(generator, getCodeModel(options)) ) throwf("could not create set codegen model: %s", lto_get_error_message()); - // if requested, save off merged bitcode file - if ( options.saveTemps ) { - char tempBitcodePath[MAXPATHLEN]; - strcpy(tempBitcodePath, options.outputFilePath); - strcat(tempBitcodePath, ".lto.bc"); + // if requested, save off merged bitcode file + if ( options.saveTemps ) { + char tempBitcodePath[MAXPATHLEN]; + strcpy(tempBitcodePath, options.outputFilePath); + strcat(tempBitcodePath, ".lto.bc"); #if LTO_API_VERSION >= 15 - ::lto_codegen_set_should_embed_uselists(generator, true); + ::lto_codegen_set_should_embed_uselists(generator, true); #endif - ::lto_codegen_write_merged_modules(generator, tempBitcodePath); - } + ::lto_codegen_write_merged_modules(generator, tempBitcodePath); + } #if LTO_API_VERSION >= 3 // find assembler next to linker @@ -794,13 +821,11 @@ bool Parser::optimize( const std::vector& allAtoms, useSplitAPI = true; #endif - size_t machOFileLen = 0; - const uint8_t* machOFile = NULL; if ( useSplitAPI) { #if LTO_API_VERSION >= 12 #if LTO_API_VERSION >= 14 - if ( ::lto_api_version() >= 14 && options.ltoCodegenOnly) - lto_codegen_set_should_internalize(generator, false); + if ( ::lto_api_version() >= 14 && options.ltoCodegenOnly) + lto_codegen_set_should_internalize(generator, false); #endif // run optimizer if ( !options.ltoCodegenOnly && ::lto_codegen_optimize(generator) ) @@ -816,7 +841,7 @@ bool Parser::optimize( const std::vector& allAtoms, #endif ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath); if ( options.bitcodeBundle ) - state.ltoBitcodePath = tempOptBitcodePath; + state.ltoBitcodePath.push_back(tempOptBitcodePath); } // run code generator @@ -841,13 +866,13 @@ bool Parser::optimize( const std::vector& allAtoms, ::lto_codegen_write_merged_modules(generator, tempOptBitcodePath); } } - - // if requested, save off temp mach-o file - if ( options.saveTemps ) { - char tempMachoPath[MAXPATHLEN]; - strcpy(tempMachoPath, options.outputFilePath); - strcat(tempMachoPath, ".lto.o"); - int fd = ::open(tempMachoPath, O_CREAT | O_WRONLY | O_TRUNC, 0666); + + // if requested, save off temp mach-o file + if ( options.saveTemps ) { + char tempMachoPath[MAXPATHLEN]; + strcpy(tempMachoPath, options.outputFilePath); + strcat(tempMachoPath, ".lto.o"); + int fd = ::open(tempMachoPath, O_CREAT | O_WRONLY | O_TRUNC, 0666); if ( fd != -1) { ::write(fd, machOFile, machOFileLen); ::close(fd); @@ -855,20 +880,30 @@ bool Parser::optimize( const std::vector& allAtoms, } // if needed, save temp mach-o file to specific location - if ( options.tmpObjectFilePath != NULL ) { - int fd = ::open(options.tmpObjectFilePath, O_CREAT | O_WRONLY | O_TRUNC, 0666); + if ( !object_path.empty() ) { + int fd = ::open(object_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); if ( fd != -1) { ::write(fd, machOFile, machOFileLen); ::close(fd); } else { - warning("could not write LTO temp file '%s', errno=%d", options.tmpObjectFilePath, errno); + warning("could not write LTO temp file '%s', errno=%d", object_path.c_str(), errno); } } - - // parse generated mach-o file into a MachOReader - ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, options); - + return std::make_tuple(machOFile, machOFileLen); +} + +/// Load the MachO located in buffer \p machOFile with size \p machOFileLen. +/// The loaded atoms are sync'ed using all the supplied lists. +void Parser::loadMachO(ld::relocatable::File* machoFile, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines, + CStringToAtom &llvmAtoms, + CStringToAtom &deadllvmAtoms) { + const bool logAtomsBeforeSync = false; + // sync generated mach-o atoms with existing atoms ld knows about if ( logAtomsBeforeSync ) { fprintf(stderr, "llvmAtoms:\n"); @@ -886,11 +921,96 @@ bool Parser::optimize( const std::vector& allAtoms, } AtomSyncer syncer(additionalUndefines, newAtoms, llvmAtoms, deadllvmAtoms, options); machoFile->forEachAtom(syncer); - - // Remove InternalAtoms from ld - for (std::vector::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { - (*it)->internalAtom().setCoalescedAway(); + + // notify about file level attributes + handler.doFile(*machoFile); +} + +// Full LTO processing +bool Parser::optimizeLTO(const std::vector files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines) { + const bool logExtraOptions = false; + const bool logBitcodeFiles = false; + + if (files.empty()) + return true; + + // create optimizer and add each Reader + lto_code_gen_t generator = NULL; +#if LTO_API_VERSION >= 11 + if ( File::sSupportsLocalContext ) + generator = ::lto_codegen_create_in_local_context(); + else +#endif + generator = ::lto_codegen_create(); +#if LTO_API_VERSION >= 7 + lto_codegen_set_diagnostic_handler(generator, ltoDiagnosticHandler, NULL); +#endif + + ld::File::Ordinal lastOrdinal; + + // When flto_codegen_only is on and we have a single .bc file, use lto_codegen_set_module instead of + // lto_codegen_add_module, to make sure the the destination module will be the same as the input .bc file. + bool useSetModule = false; +#if LTO_API_VERSION >= 13 + useSetModule = (files.size() == 1) && options.ltoCodegenOnly && (::lto_api_version() >= 13); +#endif + for (auto *f : files) { + assert(f->ordinal() > lastOrdinal); + if ( logBitcodeFiles && !useSetModule ) fprintf(stderr, "lto_codegen_add_module(%s)\n", f->path()); + if ( logBitcodeFiles && useSetModule ) fprintf(stderr, "lto_codegen_set_module(%s)\n", f->path()); + if ( f->mergeIntoGenerator(generator, useSetModule) ) + throwf("lto: could not merge in %s because '%s', using libLTO version '%s'", f->path(), ::lto_get_error_message(), ::lto_get_version()); + lastOrdinal = f->ordinal(); + } + + // add any -mllvm command line options + if ( !_s_llvmOptionsProcessed ) { + for (const char* opt : *options.llvmOptions) { + if ( logExtraOptions ) fprintf(stderr, "passing option to llvm: %s\n", opt); + ::lto_codegen_debug_options(generator, opt); + } + _s_llvmOptionsProcessed = true; } + + // Need a way for LTO to get cpu variants (until that info is in bitcode) + if ( options.mcpu != NULL ) + ::lto_codegen_set_cpu(generator, options.mcpu); + + // Compute the preserved symbols + CStringToAtom deadllvmAtoms, llvmAtoms; + setPreservedSymbols(allAtoms, state, options, deadllvmAtoms, llvmAtoms, generator); + + size_t machOFileLen = 0; + const uint8_t* machOFile = NULL; + + // mach-o parsing is done in-memory, but need path for debug notes + std::string object_path; + if ( options.tmpObjectFilePath != NULL ) { + object_path = options.tmpObjectFilePath; + // If the path exists and is a directory (for instance if some files + // were processed with ThinLTO before), we create the LTO file inside + // the directory. + struct stat statBuffer; + if( stat(object_path.c_str(), &statBuffer) == 0 && S_ISDIR(statBuffer.st_mode) ) { + object_path += "/lto.o"; + } + } + + // Codegen Now + std::tie(machOFile, machOFileLen) = codegen(options, state, generator, object_path); + + // parse generated mach-o file into a MachOReader + ld::relocatable::File* machoFile = parseMachOFile(machOFile, machOFileLen, object_path, options, ld::File::Ordinal::LTOOrdinal()); + + // Load the generated MachO file + loadMachO(machoFile, options, handler, newAtoms, additionalUndefines, llvmAtoms, deadllvmAtoms); + // Remove Atoms from ld if code generator optimized them away for (CStringToAtom::iterator li = llvmAtoms.begin(), le = llvmAtoms.end(); li != le; ++li) { // check if setRealAtom() called on this Atom @@ -899,49 +1019,407 @@ bool Parser::optimize( const std::vector& allAtoms, li->second->setCoalescedAway(); } } - - // notify about file level attributes - handler.doFile(*machoFile); - + // if final mach-o file has debug info, update original bitcode files to match - for (std::vector::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { - (*it)->setDebugInfo(machoFile->debugInfo(), machoFile->path(), - machoFile->modificationTime(), machoFile->cpuSubType()); + for (auto *f : files) { + f->setDebugInfo(machoFile->debugInfo(), machoFile->path(), machoFile->modificationTime(), machoFile->cpuSubType()); } - + return true; } +// Create the ThinLTO codegenerator +thinlto_code_gen_t Parser::init_thinlto_codegen(const std::vector& files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + CStringToAtom& deadllvmAtoms, + CStringToAtom& llvmAtoms) { + const bool logMustPreserve = false; + + thinlto_code_gen_t thingenerator = ::thinlto_create_codegen(); + + // Caching control + if (options.ltoCachePath && !options.bitcodeBundle) { + struct stat statBuffer; + if( stat(options.ltoCachePath, &statBuffer) != 0 || !S_ISDIR(statBuffer.st_mode) ) { + if ( mkdir(options.ltoCachePath, 0700) !=0 ) { + warning("unable to create ThinLTO cache directory: %s", options.ltoCachePath); + } + } + thinlto_codegen_set_cache_dir(thingenerator, options.ltoCachePath); + thinlto_codegen_set_cache_pruning_interval(thingenerator, options.ltoPruneInterval); + thinlto_codegen_set_cache_entry_expiration(thingenerator, options.ltoPruneAfter); + thinlto_codegen_set_final_cache_size_relative_to_available_space(thingenerator, options.ltoMaxCacheSize); + } + + // if requested, ask the code generator to save off intermediate bitcode files + if ( options.saveTemps ) { + std::string tempPath = options.outputFilePath; + tempPath += ".thinlto.bcs/"; + struct stat statBuffer; + if( stat(tempPath.c_str(), &statBuffer) != 0 || !S_ISDIR(statBuffer.st_mode) ) { + if ( mkdir(tempPath.c_str(), 0700) !=0 ) { + warning("unable to create ThinLTO output directory for temporary bitcode files: %s", tempPath.c_str()); + } + } + thinlto_codegen_set_savetemps_dir(thingenerator, tempPath.c_str()); + } + + // Set some codegen options + if ( thinlto_codegen_set_pic_model(thingenerator, getCodeModel(options)) ) + throwf("could not create set codegen model: %s", lto_get_error_message()); + + // Expose reachability informations for internalization in LTO + + // The atom graph uses directed edges (references). Collect all references where + // originating atom is not part of any LTO Reader. This allows optimizer to optimize an + // external (i.e. not originated from same .o file) reference if all originating atoms are also + // defined in llvm bitcode file. + CStringSet nonLLVMRefs; + CStringSet LLVMRefs; + for (std::vector::const_iterator it = allAtoms.begin(); it != allAtoms.end(); ++it) { + const ld::Atom* atom = *it; + const ld::Atom* target; + for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) { + switch ( fit->binding ) { + case ld::Fixup::bindingDirectlyBound: + // that reference a ThinLTO llvm atom + target = fit->u.target; + if ( target->contentType() == ld::Atom::typeLTOtemporary && + ((lto::File *)target->file())->isThinLTO() && + atom->file() != target->file() + ) { + if (atom->contentType() != ld::Atom::typeLTOtemporary || + !((lto::File *)atom->file())->isThinLTO()) + nonLLVMRefs.insert(target->name()); + else + LLVMRefs.insert(target->name()); + if ( logMustPreserve ) + fprintf(stderr, "Found a reference from %s -> %s\n", atom->name(), target->name()); + } + break; + case ld::Fixup::bindingsIndirectlyBound: + target = state.indirectBindingTable[fit->u.bindingIndex]; + if ( (target != NULL) && (target->contentType() == ld::Atom::typeLTOtemporary) && + ((lto::File *)target->file())->isThinLTO() && + atom->file() != target->file() + ) { + if (atom->contentType() != ld::Atom::typeLTOtemporary || + !((lto::File *)atom->file())->isThinLTO()) + nonLLVMRefs.insert(target->name()); + else + LLVMRefs.insert(target->name()); + if ( logMustPreserve ) + fprintf(stderr, "Found a reference from %s -> %s\n", atom->name(), target->name()); + } + default: + break; + } + } + if (atom->contentType() == ld::Atom::typeLTOtemporary && + ((lto::File *)atom->file())->isThinLTO()) { + llvmAtoms[atom->name()] = (Atom*)atom; + } + } + // if entry point is in a llvm bitcode file, it must be preserved by LTO + if ( state.entryPoint != NULL ) { + if ( state.entryPoint->contentType() == ld::Atom::typeLTOtemporary ) + nonLLVMRefs.insert(state.entryPoint->name()); + } + for (auto file : files) { + for(uint32_t i=0; i < file->_atomArrayCount; ++i) { + Atom* llvmAtom = &file->_atomArray[i]; + if ( llvmAtom->coalescedAway() ) { + const char* name = llvmAtom->name(); + if ( deadllvmAtoms.find(name) == deadllvmAtoms.end() ) { + if ( logMustPreserve ) + fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because linker coalesce away and replace with a mach-o atom\n", name); + ::thinlto_codegen_add_must_preserve_symbol(thingenerator, name, strlen(name)); + deadllvmAtoms[name] = (Atom*)llvmAtom; + } + } + else if ( options.linkerDeadStripping && !llvmAtom->live() ) { + const char* name = llvmAtom->name(); + deadllvmAtoms[name] = (Atom*)llvmAtom; + } + } + } + + // tell code generator about symbols that must be preserved + for (CStringToAtom::iterator it = llvmAtoms.begin(); it != llvmAtoms.end(); ++it) { + const char* name = it->first; + Atom* atom = it->second; + // Include llvm Symbol in export list if it meets one of following two conditions + // 1 - atom scope is global (and not linkage unit). + // 2 - included in nonLLVMRefs set. + // If a symbol is not listed in exportList then LTO is free to optimize it away. + if ( (atom->scope() == ld::Atom::scopeGlobal) && options.preserveAllGlobals ) { + if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because global symbol\n", name); + ::thinlto_codegen_add_must_preserve_symbol(thingenerator, name, strlen(name)); + } + else if ( nonLLVMRefs.find(name) != nonLLVMRefs.end() ) { + if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because referenced from outside of ThinLTO\n", name); + ::thinlto_codegen_add_must_preserve_symbol(thingenerator, name, strlen(name)); + } + else if ( LLVMRefs.find(name) != LLVMRefs.end() ) { + if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because referenced from another file\n", name); + ::thinlto_codegen_add_cross_referenced_symbol(thingenerator, name, strlen(name)); + } else { + if ( logMustPreserve ) fprintf(stderr, "NOT preserving(%s)\n", name); + } +// FIXME: to be implemented +// else if ( options.relocatable && hasNonllvmAtoms ) { +// // ld -r mode but merging in some mach-o files, so need to keep libLTO from optimizing away anything +// if ( logMustPreserve ) fprintf(stderr, "lto_codegen_add_must_preserve_symbol(%s) because -r mode disable LTO dead stripping\n", name); +// ::thinlto_codegen_add_must_preserve_symbol(thingenerator, name, strlen(name)); +// } + } + + return thingenerator; +} + +// Full LTO processing +bool Parser::optimizeThinLTO(const std::vector& files, + const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines) { + const bool logBitcodeFiles = false; + + if (files.empty()) + return true; + +#if LTO_API_VERSION >= 18 + + if (::lto_api_version() < 18) + throwf("lto: could not use -thinlto because libLTO is too old (version '%d', >=18 is required)", ::lto_api_version()); + + // Handle -mllvm options + if ( !_s_llvmOptionsProcessed ) { + thinlto_debug_options(options.llvmOptions->data(), options.llvmOptions->size()); + _s_llvmOptionsProcessed = true; + } + + // Create the ThinLTO codegenerator + CStringToAtom deadllvmAtoms; + CStringToAtom llvmAtoms; + thinlto_code_gen_t thingenerator = init_thinlto_codegen(files, allAtoms, state, options, deadllvmAtoms, llvmAtoms); + + + ld::File::Ordinal lastOrdinal; + for (auto *f : files) { + if ( logBitcodeFiles) fprintf(stderr, "thinlto_codegen_add_module(%s)\n", f->path()); + f->addToThinGenerator(thingenerator); + lastOrdinal = f->ordinal(); + } + +#if LTO_API_VERSION >= 19 + // In the bitcode bundle case, we first run the generator with codegen disabled + // and get the bitcode output. These files are added for later bundling, and a + // new codegenerator is setup with these as input, and the optimizer disabled. + if (options.bitcodeBundle) { + // Bitcode Bundle case + thinlto_codegen_disable_codegen(thingenerator, true); + // Process the optimizer only + thinlto_codegen_process(thingenerator); + auto numObjects = thinlto_module_get_num_objects(thingenerator); + // Save the codegenerator + thinlto_code_gen_t bitcode_generator = thingenerator; + // Create a new codegen generator for the codegen part. + thingenerator = init_thinlto_codegen(files, allAtoms, state, options, deadllvmAtoms, llvmAtoms); + // Disable the optimizer + thinlto_codegen_set_codegen_only(thingenerator, true); + + // Save bitcode files for later, and add them to the codegen generator. + for (unsigned bufID = 0; bufID < numObjects; ++bufID) { + auto machOFile = thinlto_module_get_object(bitcode_generator, bufID); + std::string tempMachoPath = options.outputFilePath; + tempMachoPath += "."; + tempMachoPath += std::to_string(bufID); + tempMachoPath += ".thinlto.o.bc"; + state.ltoBitcodePath.push_back(tempMachoPath); + int fd = ::open(tempMachoPath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); + if ( fd != -1 ) { + ::write(fd, machOFile.Buffer, machOFile.Size); + ::close(fd); + } else { + throwf("unable to write temporary ThinLTO output: %s", tempMachoPath.c_str()); + } + + // Add the optimized bitcode to the codegen generator now. + ::thinlto_codegen_add_module(thingenerator, tempMachoPath.c_str(), (const char *)machOFile.Buffer, machOFile.Size); + } + } + + if (options.ltoCodegenOnly) + // Disable the optimizer + thinlto_codegen_set_codegen_only(thingenerator, true); +#endif + + // run code generator + thinlto_codegen_process(thingenerator); + auto numObjects = thinlto_module_get_num_objects(thingenerator); + if (!numObjects) + throwf("could not do ThinLTO codegen (thinlto_codegen_process didn't produce any object): '%s', using libLTO version '%s'", ::lto_get_error_message(), ::lto_get_version()); + + // if requested, save off objects files + if ( options.saveTemps ) { + for (unsigned bufID = 0; bufID < numObjects; ++bufID) { + auto machOFile = thinlto_module_get_object(thingenerator, bufID); + std::string tempMachoPath = options.outputFilePath; + tempMachoPath += "."; + tempMachoPath += std::to_string(bufID); + tempMachoPath += ".thinlto.o"; + int fd = ::open(tempMachoPath.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); + if ( fd != -1 ) { + ::write(fd, machOFile.Buffer, machOFile.Size); + ::close(fd); + } else { + warning("unable to write temporary ThinLTO output: %s", tempMachoPath.c_str()); + } + } + } + + + // mach-o parsing is done in-memory, but need path for debug notes + std::string macho_dirpath = "/tmp/thinlto.o"; + if ( options.tmpObjectFilePath != NULL ) { + macho_dirpath = options.tmpObjectFilePath; + struct stat statBuffer; + if( stat(macho_dirpath.c_str(), &statBuffer) != 0 || !S_ISDIR(statBuffer.st_mode) ) { + if ( mkdir(macho_dirpath.c_str(), 0700) !=0 ) { + warning("unable to create ThinLTO output directory for temporary object files: %s", macho_dirpath.c_str()); + } + } + } + + auto ordinal = ld::File::Ordinal::LTOOrdinal().nextFileListOrdinal(); + for (unsigned bufID = 0; bufID < numObjects; ++bufID) { + auto machOFile = thinlto_module_get_object(thingenerator, bufID); + if (!machOFile.Size) { + warning("Ignoring empty buffer generated by ThinLTO"); + continue; + } + + // mach-o parsing is done in-memory, but need path for debug notes + std::string tmp_path = macho_dirpath + "/" + std::to_string(bufID) + ".o"; + + // parse generated mach-o file into a MachOReader + ld::relocatable::File* machoFile = parseMachOFile((const uint8_t *)machOFile.Buffer, machOFile.Size, tmp_path, options, ordinal); + ordinal = ordinal.nextFileListOrdinal(); + + // if needed, save temp mach-o file to specific location + if ( options.tmpObjectFilePath != NULL ) { + int fd = ::open(tmp_path.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0666); + if ( fd != -1) { + ::write(fd, (const uint8_t *)machOFile.Buffer, machOFile.Size); + ::close(fd); + } + else { + warning("could not write ThinLTO temp file '%s', errno=%d", tmp_path.c_str(), errno); + } + } + + // Load the generated MachO file + loadMachO(machoFile, options, handler, newAtoms, additionalUndefines, llvmAtoms, deadllvmAtoms); + } + + // Remove Atoms from ld if code generator optimized them away + for (CStringToAtom::iterator li = llvmAtoms.begin(), le = llvmAtoms.end(); li != le; ++li) { + // check if setRealAtom() called on this Atom + if ( li->second->compiledAtom() == NULL ) { + //fprintf(stderr, "llvm optimized away %p %s\n", li->second, li->second->name()); + li->second->setCoalescedAway(); + } + } + + return true; +#else // ! (LTO_API_VERSION >= 18) + throwf("lto: could not use -thinlto because ld was built against a version of libLTO too old (version '%d', >=18 is required)", LTO_API_VERSION); +#endif +} + +bool Parser::optimize( const std::vector& allAtoms, + ld::Internal& state, + const OptimizeOptions& options, + ld::File::AtomHandler& handler, + std::vector& newAtoms, + std::vector& additionalUndefines) +{ + + // exit quickly if nothing to do + if ( _s_files.size() == 0 ) + return false; + + // print out LTO version string if -v was used + if ( options.verbose ) + fprintf(stderr, "%s\n", ::lto_get_version()); + + // The order that files are merged must match command line order + std::sort(_s_files.begin(), _s_files.end(), CommandLineOrderFileSorter()); + +#if LTO_API_VERSION >= 19 + // If ltoCodegenOnly is set, we don't want to merge any bitcode files and perform FullLTO + // we just take the ThinLTO path (optimization will be disabled anyway). + if (options.ltoCodegenOnly) { + for (auto *file : _s_files) { + file->setIsThinLTO(true); + } + } +#endif + + std::vector theLTOFiles; + std::vector theThinLTOFiles; + for (auto *file : _s_files) { + if (file->isThinLTO()) { + theThinLTOFiles.push_back(file); + } else { + theLTOFiles.push_back(file); + } + } + + auto result = optimizeThinLTO(theThinLTOFiles, allAtoms, state, options, handler, newAtoms, additionalUndefines) && + optimizeLTO(theLTOFiles, allAtoms, state, options, handler, newAtoms, additionalUndefines); + + // Remove InternalAtoms from ld + for (std::vector::iterator it=_s_files.begin(); it != _s_files.end(); ++it) { + (*it)->internalAtom().setCoalescedAway(); + } + + return result; +} + void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom) { static const bool log = false; - static const ld::Atom* lastProxiedAtom = NULL; - static const ld::File* lastProxiedFile = NULL; // update proxy atoms to point to real atoms and find new atoms const char* name = machoAtom.name(); - CStringToAtom::iterator pos = _llvmAtoms.find(name); + CStringToAtom::const_iterator pos = _llvmAtoms.find(name); if ( pos != _llvmAtoms.end() ) { // turn Atom into a proxy for this mach-o atom pos->second->setCompiledAtom(machoAtom); - lastProxiedAtom = &machoAtom; - lastProxiedFile = pos->second->file(); + _lastProxiedAtom = &machoAtom; + _lastProxiedFile = pos->second->file(); if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p synced to lto atom %p (name=%s)\n", &machoAtom, pos->second, machoAtom.name()); } else { // an atom of this name was not in the allAtoms list the linker gave us - if ( _deadllvmAtoms.find(name) != _deadllvmAtoms.end() ) { + auto llvmAtom = _deadllvmAtoms.find(name); + if ( llvmAtom != _deadllvmAtoms.end() ) { // this corresponding to an atom that the linker coalesced away or marked not-live if ( _options.linkerDeadStripping ) { // llvm seems to want this atom and -dead_strip is enabled, so it will be deleted if not needed, so add back - Atom* llvmAtom = _deadllvmAtoms[name]; - llvmAtom->setCompiledAtom(machoAtom); + llvmAtom->second->setCompiledAtom(machoAtom); _newAtoms.push_back(&machoAtom); - if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p but adding back (name=%s)\n", &machoAtom, llvmAtom, machoAtom.name()); + if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p but adding back (name=%s)\n", &machoAtom, llvmAtom->second, machoAtom.name()); } else { // Don't pass it back as a new atom - if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p (name=%s)\n", &machoAtom, _deadllvmAtoms[name], machoAtom.name()); + if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p matches dead lto atom %p (name=%s)\n", &machoAtom, llvmAtom->second, machoAtom.name()); } } else @@ -949,9 +1427,10 @@ void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom) // this is something new that lto conjured up, tell ld its new _newAtoms.push_back(&machoAtom); // if new static atom in same section as previous non-static atom, assign to same file as previous - if ( (lastProxiedAtom != NULL) && (lastProxiedAtom->section() == machoAtom.section()) ) { + if ( (_lastProxiedAtom != NULL) && (_lastProxiedAtom->section() == machoAtom.section()) ) { ld::Atom* ma = const_cast(&machoAtom); - ma->setFile(lastProxiedFile); + ma->setFile(_lastProxiedFile); + if (log) fprintf(stderr, "AtomSyncer, mach-o atom %s is proxied to %s (path=%s)\n", machoAtom.name(), _lastProxiedAtom->name(), _lastProxiedFile->path()); } if (log) fprintf(stderr, "AtomSyncer, mach-o atom %p is totally new (name=%s)\n", &machoAtom, machoAtom.name()); } @@ -975,7 +1454,7 @@ void Parser::AtomSyncer::doAtom(const ld::Atom& machoAtom) // llvm symbol references always go through Atom proxy. { const char* targetName = fit->u.target->name(); - CStringToAtom::iterator post = _llvmAtoms.find(targetName); + CStringToAtom::const_iterator post = _llvmAtoms.find(targetName); if ( post != _llvmAtoms.end() ) { const ld::Atom* t = post->second; if (log) fprintf(stderr, " updating direct reference to %p to be ref to %p: %s\n", fit->u.target, t, targetName); @@ -1023,6 +1502,21 @@ bool isObjectFile(const uint8_t* fileContent, uint64_t fileLength, cpu_type_t ar return Parser::validFile(fileContent, fileLength, architecture, subarch); } +// +// Used by archive reader to see if member defines a Category (for -ObjC semantics) +// +bool hasObjCCategory(const uint8_t* fileContent, uint64_t fileLength) +{ +#if LTO_API_VERSION >= 20 + // note: if run with older libLTO.dylib that does not implement + // lto_module_has_objc_category, the call will return 0 which is "false" + return lto_module_has_objc_category(fileContent, fileLength); +#else + return false; +#endif +} + + static ld::relocatable::File *parseImpl( const uint8_t *fileContent, uint64_t fileLength, const char *path, time_t modTime, ld::File::Ordinal ordinal, cpu_type_t architecture,