X-Git-Url: https://git.saurik.com/apple/ld64.git/blobdiff_plain/fb24a05017baddaa8cdc205852b134120bcc54ad..9543cb2f21e50a417dc8cf37eb7173f353536979:/src/ld/Options.cpp diff --git a/src/ld/Options.cpp b/src/ld/Options.cpp index 6521abe..057fad9 100644 --- a/src/ld/Options.cpp +++ b/src/ld/Options.cpp @@ -1,6 +1,6 @@ /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- * - * Copyright (c) 2005-2008 Apple Inc. All rights reserved. + * Copyright (c) 2005-2011 Apple Inc. All rights reserved. * * @APPLE_LICENSE_HEADER_START@ * @@ -26,28 +26,52 @@ #include #include #include +#include #include #include +#include +#include +#include +#include +#include + #include -#include "configure.h" #include "Options.h" #include "Architectures.hpp" #include "MachOFileAbstraction.hpp" +#include "Snapshot.h" -extern void printLTOVersion(Options &opts); +// upward dependency on lto::version() +namespace lto { + extern const char* version(); +} // magic to place command line in crash reports -extern "C" char* __crashreporter_info__; -static char crashreporterBuffer[1000]; -char* __crashreporter_info__ = crashreporterBuffer; +const int crashreporterBufferSize = 2000; +static char crashreporterBuffer[crashreporterBufferSize]; +#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 + #include + // hack until ld does not need to build on 10.6 anymore + struct crashreporter_annotations_t gCRAnnotations + __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) + = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 }; +#else + extern "C" char* __crashreporter_info__; + __attribute__((used)) + char* __crashreporter_info__ = crashreporterBuffer; +#endif + static bool sEmitWarnings = true; +static bool sFatalWarnings = false; static const char* sWarningsSideFilePath = NULL; static FILE* sWarningsSideFile = NULL; +static int sWarningsCount = 0; void warning(const char* format, ...) { + ++sWarningsCount; if ( sEmitWarnings ) { va_list list; if ( sWarningsSideFilePath != NULL ) { @@ -80,31 +104,83 @@ void throwf(const char* format, ...) throw t; } + +bool Options::FileInfo::checkFileExists(const Options& options, const char *p) +{ + struct stat statBuffer; + if (p == NULL) + p = path; + if ( stat(p, &statBuffer) == 0 ) { + if (p != path) path = strdup(p); + fileLen = statBuffer.st_size; + modTime = statBuffer.st_mtime; + return true; + } + if ( options.dumpDependencyInfo() ) + options.dumpDependency(Options::depNotFound, p); + return false; +} + + Options::Options(int argc, const char* argv[]) - : fOutputFile("a.out"), fArchitecture(0), fSubArchitecture(0), fOutputKind(kDynamicExecutable), - fHasPreferredSubType(false), fPrebind(false), fBindAtLoad(false), fKeepPrivateExterns(false), + : fOutputFile("a.out"), fArchitecture(0), fSubArchitecture(0), fArchitectureName("unknown"), fOutputKind(kDynamicExecutable), + fHasPreferredSubType(false), fArchSupportsThumb2(false), fPrebind(false), fBindAtLoad(false), fKeepPrivateExterns(false), fNeedsModuleTable(false), fIgnoreOtherArchFiles(false), fErrorOnOtherArchFiles(false), fForceSubtypeAll(false), - fInterposeMode(kInterposeNone), fDeadStrip(kDeadStripOff), fNameSpace(kTwoLevelNameSpace), - fDylibCompatVersion(0), fDylibCurrentVersion(0), fDylibInstallName(NULL), fFinalName(NULL), fEntryName("start"), fBaseAddress(0), + fInterposeMode(kInterposeNone), fDeadStrip(false), fNameSpace(kTwoLevelNameSpace), + fDylibCompatVersion(0), fDylibCurrentVersion(0), fDylibInstallName(NULL), fFinalName(NULL), fEntryName("start"), + fBaseAddress(0), fMaxAddress(0x7FFFFFFFFFFFFFFFLL), fBaseWritableAddress(0), fSplitSegs(false), - fExportMode(kExportDefault), fLibrarySearchMode(kSearchAllDirsForDylibsThenAllDirsForArchives), - fUndefinedTreatment(kUndefinedError), fMessagesPrefixedWithArchitecture(false), + fExportMode(kExportDefault), fLibrarySearchMode(kSearchDylibAndArchiveInEachDir), + fUndefinedTreatment(kUndefinedError), fMessagesPrefixedWithArchitecture(true), fWeakReferenceMismatchTreatment(kWeakReferenceMismatchNonWeak), fClientName(NULL), fUmbrellaName(NULL), fInitFunctionName(NULL), fDotOutputFile(NULL), fExecutablePath(NULL), fBundleLoader(NULL), fDtraceScriptName(NULL), fSegAddrTablePath(NULL), fMapPath(NULL), - fZeroPageSize(ULLONG_MAX), fStackSize(0), fStackAddr(0), fExecutableStack(false), + fDyldInstallPath("/usr/lib/dyld"), fTempLtoObjectPath(NULL), fOverridePathlibLTO(NULL), fLtoCpu(NULL), + fZeroPageSize(ULLONG_MAX), fStackSize(0), fStackAddr(0), fSourceVersion(0), fSDKVersion(0), fExecutableStack(false), + fNonExecutableHeap(false), fDisableNonExecutableHeap(false), fMinimumHeaderPad(32), fSegmentAlignment(4096), fCommonsMode(kCommonsIgnoreDylibs), fUUIDMode(kUUIDContent), fLocalSymbolHandling(kLocalSymbolsAll), fWarnCommons(false), fVerbose(false), fKeepRelocations(false), fWarnStabs(false), fTraceDylibSearching(false), fPause(false), fStatistics(false), fPrintOptions(false), fSharedRegionEligible(false), fPrintOrderFileStatistics(false), - fReadOnlyx86Stubs(false), fPositionIndependentExecutable(false), fMaxMinimumHeaderPad(false), - fDeadStripDylibs(false), fAllowTextRelocs(false), fWarnTextRelocs(false), + fReadOnlyx86Stubs(false), fPositionIndependentExecutable(false), fPIEOnCommandLine(false), + fDisablePositionIndependentExecutable(false), fMaxMinimumHeaderPad(false), + fDeadStripDylibs(false), fAllowTextRelocs(false), fWarnTextRelocs(false), fKextsUseStubs(false), fUsingLazyDylibLinking(false), fEncryptable(true), fOrderData(true), fMarkDeadStrippableDylib(false), - fMakeClassicDyldInfo(true), fMakeCompressedDyldInfo(true), fAllowCpuSubtypeMismatches(false), - fUseSimplifiedDylibReExports(false), fSaveTempFiles(false) + fMakeCompressedDyldInfo(true), fMakeCompressedDyldInfoForceOff(false), fNoEHLabels(false), + fAllowCpuSubtypeMismatches(false), fUseSimplifiedDylibReExports(false), + fObjCABIVersion2Override(false), fObjCABIVersion1Override(false), fCanUseUpwardDylib(false), + fFullyLoadArchives(false), fLoadAllObjcObjectsFromArchives(false), fFlatNamespace(false), + fLinkingMainExecutable(false), fForFinalLinkedImage(false), fForStatic(false), + fForDyld(false), fMakeTentativeDefinitionsReal(false), fWhyLoad(false), fRootSafe(false), + fSetuidSafe(false), fImplicitlyLinkPublicDylibs(true), fAddCompactUnwindEncoding(true), + fWarnCompactUnwind(false), fRemoveDwarfUnwindIfCompactExists(false), + fAutoOrderInitializers(true), fOptimizeZeroFill(true), fMergeZeroFill(false), fLogObjectFiles(false), + fLogAllFiles(false), fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false), + fOutputSlidable(false), fWarnWeakExports(false), + fObjcGcCompaction(false), fObjCGc(false), fObjCGcOnly(false), + fDemangle(false), fTLVSupport(false), + fVersionLoadCommand(false), fVersionLoadCommandForcedOn(false), + fVersionLoadCommandForcedOff(false), fFunctionStartsLoadCommand(false), + fFunctionStartsForcedOn(false), fFunctionStartsForcedOff(false), + fDataInCodeInfoLoadCommand(false), fDataInCodeInfoLoadCommandForcedOn(false), fDataInCodeInfoLoadCommandForcedOff(false), + fCanReExportSymbols(false), fObjcCategoryMerging(true), fPageAlignDataAtoms(false), + fNeedsThreadLoadCommand(false), fEntryPointLoadCommand(false), + fEntryPointLoadCommandForceOn(false), fEntryPointLoadCommandForceOff(false), + fSourceVersionLoadCommand(false), + fSourceVersionLoadCommandForceOn(false), fSourceVersionLoadCommandForceOff(false), + fDependentDRInfo(false), fDependentDRInfoForcedOn(false), fDependentDRInfoForcedOff(false), + fTargetIOSSimulator(false), fExportDynamic(false), fAbsoluteSymbols(false), + fAllowSimulatorToLinkWithMacOSX(false), fKeepDwarfUnwind(true), + fKeepDwarfUnwindForcedOn(false), fKeepDwarfUnwindForcedOff(false), + fVerboseOptimizationHints(false), fIgnoreOptimizationHints(false), + fGenerateDtraceDOF(true), fAllowBranchIslands(true), + fDebugInfoStripping(kDebugInfoMinimal), fTraceOutputFile(NULL), + fMacVersionMin(ld::macVersionUnset), fIOSVersionMin(ld::iOSVersionUnset), + fSaveTempFiles(false), fSnapshotRequested(false), fPipelineFifo(NULL), + fDependencyInfoPath(NULL), fDependencyFileDescriptor(-1) { this->checkForClassic(argc, argv); this->parsePreCommandLineEnvironmentSettings(); @@ -112,54 +188,27 @@ Options::Options(int argc, const char* argv[]) this->parsePostCommandLineEnvironmentSettings(); this->reconfigureDefaults(); this->checkIllegalOptionCombinations(); + + if ( this->dumpDependencyInfo() ) { + this->dumpDependency(depOutputFile, fOutputFile); + if ( fMapPath != NULL ) + this->dumpDependency(depOutputFile, fMapPath); + } } Options::~Options() { + if ( fDependencyFileDescriptor != -1 ) + ::close(fDependencyFileDescriptor); } -const ObjectFile::ReaderOptions& Options::readerOptions() -{ - return fReaderOptions; -} - - -const char* Options::getOutputFilePath() -{ - return fOutputFile; -} - -std::vector& Options::getInputFiles() -{ - return fInputFiles; -} - -Options::OutputKind Options::outputKind() -{ - return fOutputKind; -} - -bool Options::bindAtLoad() -{ - return fBindAtLoad; -} - -bool Options::prebind() -{ - return fPrebind; -} - -bool Options::fullyLoadArchives() +bool Options::errorBecauseOfWarnings() const { - return fReaderOptions.fFullyLoadArchives; + return (sFatalWarnings && (sWarningsCount > 0)); } -Options::NameSpace Options::nameSpace() -{ - return fNameSpace; -} -const char* Options::installPath() +const char* Options::installPath() const { if ( fDylibInstallName != NULL ) return fDylibInstallName; @@ -169,32 +218,8 @@ const char* Options::installPath() return fOutputFile; } -uint32_t Options::currentVersion() -{ - return fDylibCurrentVersion; -} - -uint32_t Options::compatibilityVersion() -{ - return fDylibCompatVersion; -} - -const char* Options::entryName() -{ - return fEntryName; -} - -uint64_t Options::baseAddress() -{ - return fBaseAddress; -} - -bool Options::keepPrivateExterns() -{ - return fKeepPrivateExterns; -} -bool Options::interposable(const char* name) +bool Options::interposable(const char* name) const { switch ( fInterposeMode ) { case kInterposeNone: @@ -207,121 +232,32 @@ bool Options::interposable(const char* name) throw "internal error"; } -bool Options::needsModuleTable() -{ - return fNeedsModuleTable; -} - -bool Options::ignoreOtherArchInputFiles() -{ - return fIgnoreOtherArchFiles; -} - -bool Options::forceCpuSubtypeAll() -{ - return fForceSubtypeAll; -} - -bool Options::traceDylibs() -{ - return fReaderOptions.fTraceDylibs; -} - -bool Options::traceArchives() -{ - return fReaderOptions.fTraceArchives; -} - -Options::UndefinedTreatment Options::undefinedTreatment() -{ - return fUndefinedTreatment; -} - -Options::WeakReferenceMismatchTreatment Options::weakReferenceMismatchTreatment() -{ - return fWeakReferenceMismatchTreatment; -} - -const char* Options::umbrellaName() -{ - return fUmbrellaName; -} - -std::vector& Options::allowableClients() -{ - return fAllowableClients; -} - -const char* Options::clientName() -{ - return fClientName; -} - -uint64_t Options::zeroPageSize() -{ - return fZeroPageSize; -} - -bool Options::hasCustomStack() -{ - return (fStackSize != 0); -} - -uint64_t Options::customStackSize() -{ - return fStackSize; -} - -uint64_t Options::customStackAddr() -{ - return fStackAddr; -} - -bool Options::hasExecutableStack() -{ - return fExecutableStack; -} - -std::vector& Options::initialUndefines() -{ - return fInitialUndefines; -} -bool Options::printWhyLive(const char* symbolName) +bool Options::printWhyLive(const char* symbolName) const { return ( fWhyLive.find(symbolName) != fWhyLive.end() ); } -const char* Options::initFunctionName() -{ - return fInitFunctionName; -} - const char* Options::dotOutputFile() { return fDotOutputFile; } -bool Options::hasExportRestrictList() -{ - return (fExportMode != kExportDefault); -} -bool Options::hasExportMaskList() +bool Options::hasWildCardExportRestrictList() const { - return (fExportMode == kExportSome); + // has -exported_symbols_list which contains some wildcards + return ((fExportMode == kExportSome) && fExportSymbols.hasWildCards()); } - -bool Options::hasWildCardExportRestrictList() +bool Options::hasWeakBitTweaks() const { // has -exported_symbols_list which contains some wildcards - return ((fExportMode == kExportSome) && fExportSymbols.hasWildCards()); + return (!fForceWeakSymbols.empty() || !fForceNotWeakSymbols.empty()); } - -bool Options::allGlobalsAreDeadStripRoots() +bool Options::allGlobalsAreDeadStripRoots() const { // -exported_symbols_list means globals are not exported by default if ( fExportMode == kExportSome ) @@ -329,7 +265,11 @@ bool Options::allGlobalsAreDeadStripRoots() // switch ( fOutputKind ) { case Options::kDynamicExecutable: + // Add the -export_dynamic flag + return fExportDynamic; case Options::kStaticExecutable: + // Support the -export_dynamic flag for xnu + return fExportDynamic; case Options::kPreload: // by default unused globals in a main executable are stripped return false; @@ -343,59 +283,122 @@ bool Options::allGlobalsAreDeadStripRoots() return false; } -uint32_t Options::minimumHeaderPad() + +bool Options::keepRelocations() { - return fMinimumHeaderPad; + return fKeepRelocations; } -std::vector& Options::extraSections() +bool Options::warnStabs() { - return fExtraSections; + return fWarnStabs; } -std::vector& Options::sectionAlignments() +const char* Options::executablePath() { - return fSectionAlignments; + return fExecutablePath; } -Options::CommonsMode Options::commonsMode() +uint32_t Options::initialSegProtection(const char* segName) const { - return fCommonsMode; + for(std::vector::const_iterator it = fCustomSegmentProtections.begin(); it != fCustomSegmentProtections.end(); ++it) { + if ( strcmp(it->name, segName) == 0 ) { + return it->init; + } + } + if ( strcmp(segName, "__PAGEZERO") == 0 ) { + return 0; + } + else if ( strcmp(segName, "__TEXT") == 0 ) { + return VM_PROT_READ | VM_PROT_EXECUTE; + } + else if ( strcmp(segName, "__LINKEDIT") == 0 ) { + return VM_PROT_READ; + } + + // all others default to read-write + return VM_PROT_READ | VM_PROT_WRITE; } -bool Options::warnCommons() +uint32_t Options::maxSegProtection(const char* segName) const +{ + // iPhoneOS always uses same protection for max and initial + // simulator apps need to use MacOSX max-prot + if ( (fIOSVersionMin != ld::iOSVersionUnset) && (fArchitecture != CPU_TYPE_I386) ) + return initialSegProtection(segName); + + for(std::vector::const_iterator it = fCustomSegmentProtections.begin(); it != fCustomSegmentProtections.end(); ++it) { + if ( strcmp(it->name, segName) == 0 ) { + return it->max; + } + } + if ( strcmp(segName, "__PAGEZERO") == 0 ) { + return 0; + } + // all others default to all + return VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; +} + +uint64_t Options::segPageSize(const char* segName) const { - return fWarnCommons; + for(std::vector::const_iterator it=fCustomSegmentSizes.begin(); it != fCustomSegmentSizes.end(); ++it) { + if ( strcmp(it->name, segName) == 0 ) + return it->size; + } + return fSegmentAlignment; } -bool Options::keepRelocations() +uint64_t Options::customSegmentAddress(const char* segName) const { - return fKeepRelocations; + for(std::vector::const_iterator it=fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { + if ( strcmp(it->name, segName) == 0 ) + return it->address; + } + // if custom stack in use, model as segment with custom address + if ( (fStackSize != 0) && (strcmp("__UNIXSTACK", segName) == 0) ) + return fStackAddr - fStackSize; + return 0; } -bool Options::warnStabs() +bool Options::hasCustomSegmentAddress(const char* segName) const { - return fWarnStabs; + for(std::vector::const_iterator it=fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { + if ( strcmp(it->name, segName) == 0 ) + return true; + } + // if custom stack in use, model as segment with custom address + if ( (fStackSize != 0) && (strcmp("__UNIXSTACK", segName) == 0) ) + return true; + return false; } -const char* Options::executablePath() +bool Options::hasCustomSectionAlignment(const char* segName, const char* sectName) const { - return fExecutablePath; + for (std::vector::const_iterator it = fSectionAlignments.begin(); it != fSectionAlignments.end(); ++it) { + if ( (strcmp(it->segmentName, segName) == 0) && (strcmp(it->sectionName, sectName) == 0) ) + return true; + } + return false; } -Options::DeadStripMode Options::deadStrip() +uint8_t Options::customSectionAlignment(const char* segName, const char* sectName) const { - return fDeadStrip; + for (std::vector::const_iterator it = fSectionAlignments.begin(); it != fSectionAlignments.end(); ++it) { + if ( (strcmp(it->segmentName, segName) == 0) && (strcmp(it->sectionName, sectName) == 0) ) + return it->alignment; + } + return 0; } + bool Options::hasExportedSymbolOrder() { return (fExportSymbolsOrder.size() > 0); } -bool Options::exportedSymbolOrder(const char* sym, unsigned int* order) +bool Options::exportedSymbolOrder(const char* sym, unsigned int* order) const { - NameToOrder::iterator pos = fExportSymbolsOrder.find(sym); + NameToOrder::const_iterator pos = fExportSymbolsOrder.find(sym); if ( pos != fExportSymbolsOrder.end() ) { *order = pos->second; return true; @@ -423,7 +426,7 @@ void Options::loadSymbolOrderFile(const char* fileOfExports, NameToOrder& orderM ::close(fd); - // parse into symbols and add to hash_set + // parse into symbols and add to unordered_set unsigned int count = 0; char * const end = &p[stat_buf.st_size]; enum { lineStart, inSymbol, inComment } state = lineStart; @@ -477,8 +480,33 @@ void Options::loadSymbolOrderFile(const char* fileOfExports, NameToOrder& orderM // Note: we do not free() the malloc buffer, because the strings are used by the export-set hash table } +bool Options::forceWeak(const char* symbolName) const +{ + return fForceWeakSymbols.contains(symbolName); +} + +bool Options::forceNotWeak(const char* symbolName) const +{ + return fForceNotWeakSymbols.contains(symbolName); +} + +bool Options::forceWeakNonWildCard(const char* symbolName) const +{ + return fForceWeakSymbols.containsNonWildcard(symbolName); +} + +bool Options::forceNotWeakNonWildcard(const char* symbolName) const +{ + return fForceNotWeakSymbols.containsNonWildcard(symbolName); +} + +bool Options::forceCoalesce(const char* symbolName) const +{ + return fForceCoalesceSymbols.contains(symbolName); +} + -bool Options::shouldExport(const char* symbolName) +bool Options::shouldExport(const char* symbolName) const { switch (fExportMode) { case kExportSome: @@ -491,7 +519,12 @@ bool Options::shouldExport(const char* symbolName) throw "internal error"; } -bool Options::keepLocalSymbol(const char* symbolName) +bool Options::shouldReExport(const char* symbolName) const +{ + return fReExportSymbols.contains(symbolName); +} + +bool Options::keepLocalSymbol(const char* symbolName) const { switch (fLocalSymbolHandling) { case kLocalSymbolsAll: @@ -506,105 +539,91 @@ bool Options::keepLocalSymbol(const char* symbolName) throw "internal error"; } -void Options::parseArch(const char* architecture) +void Options::setArchitecture(cpu_type_t type, cpu_subtype_t subtype) { - if ( architecture == NULL ) + for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { + if ( (type == t->cpuType) && (subtype == t->cpuSubType) ) { + fArchitecture = type; + fSubArchitecture = subtype; + fArchitectureName = t->archName; + fHasPreferredSubType = t->isSubType; + fArchSupportsThumb2 = t->supportsThumb2; + switch ( type ) { + case CPU_TYPE_I386: + case CPU_TYPE_X86_64: + if ( (fMacVersionMin == ld::macVersionUnset) && (fIOSVersionMin == ld::iOSVersionUnset) && (fOutputKind != Options::kObjectFile) ) { + #ifdef DEFAULT_MACOSX_MIN_VERSION + warning("-macosx_version_min not specified, assuming " DEFAULT_MACOSX_MIN_VERSION); + setMacOSXVersionMin(DEFAULT_MACOSX_MIN_VERSION); + #else + warning("-macosx_version_min not specified, assuming 10.6"); + fMacVersionMin = ld::mac10_6; + #endif + } + break; + case CPU_TYPE_ARM: + case CPU_TYPE_ARM64: + if ( (fMacVersionMin == ld::macVersionUnset) && (fIOSVersionMin == ld::iOSVersionUnset) && (fOutputKind != Options::kObjectFile) ) { + #if defined(DEFAULT_IPHONEOS_MIN_VERSION) + warning("-ios_version_min not specified, assuming " DEFAULT_IPHONEOS_MIN_VERSION); + setIOSVersionMin(DEFAULT_IPHONEOS_MIN_VERSION); + #else + warning("-ios_version_min not specified, assuming 6.0"); + setIOSVersionMin("6.0"); + #endif + } + break; + } + fLinkSnapshot.recordArch(fArchitectureName); + // only use compressed LINKEDIT for: + // Mac OS X 10.6 or later + // iOS 3.1 or later + if ( !fMakeCompressedDyldInfo && minOS(ld::mac10_6, ld::iOS_3_1) && !fMakeCompressedDyldInfoForceOff ) + fMakeCompressedDyldInfo = true; + // Mac OS X 10.5 and iPhoneOS 2.0 support LC_REEXPORT_DYLIB + if ( minOS(ld::mac10_5, ld::iOS_2_0) ) + fUseSimplifiedDylibReExports = true; + return; + } + } + fArchitectureName = "unknown architecture"; +} + +void Options::parseArch(const char* arch) +{ + if ( arch == NULL ) throw "-arch must be followed by an architecture string"; - if ( strcmp(architecture, "ppc") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC; - fSubArchitecture = CPU_SUBTYPE_POWERPC_ALL; - } - else if ( strcmp(architecture, "ppc64") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC64; - fSubArchitecture = CPU_SUBTYPE_POWERPC_ALL; - } - else if ( strcmp(architecture, "i386") == 0 ) { - fArchitecture = CPU_TYPE_I386; - fSubArchitecture = CPU_SUBTYPE_I386_ALL; - } - else if ( strcmp(architecture, "x86_64") == 0 ) { - fArchitecture = CPU_TYPE_X86_64; - fSubArchitecture = CPU_SUBTYPE_X86_64_ALL; - } - else if ( strcmp(architecture, "arm") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_ALL; - } - // compatibility support for cpu-sub-types - else if ( strcmp(architecture, "ppc750") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC; - fSubArchitecture = CPU_SUBTYPE_POWERPC_750; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "ppc7400") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC; - fSubArchitecture = CPU_SUBTYPE_POWERPC_7400; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "ppc7450") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC; - fSubArchitecture = CPU_SUBTYPE_POWERPC_7450; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "ppc970") == 0 ) { - fArchitecture = CPU_TYPE_POWERPC; - fSubArchitecture = CPU_SUBTYPE_POWERPC_970; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "armv6") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_V6; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "armv5") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_V5TEJ; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "armv4t") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_V4T; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "xscale") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_XSCALE; - fHasPreferredSubType = true; - } - else if ( strcmp(architecture, "armv7") == 0 ) { - fArchitecture = CPU_TYPE_ARM; - fSubArchitecture = CPU_SUBTYPE_ARM_V7; - fHasPreferredSubType = true; + for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { + if ( strcmp(t->archName,arch) == 0 ) { + fArchitectureName = arch; + fArchitecture = t->cpuType; + fSubArchitecture = t->cpuSubType; + fHasPreferredSubType = t->isSubType; + fArchSupportsThumb2 = t->supportsThumb2; + return; + } } - else - throwf("unknown/unsupported architecture name for: -arch %s", architecture); + throwf("unknown/unsupported architecture name for: -arch %s", arch); } -bool Options::checkForFile(const char* format, const char* dir, const char* rootName, FileInfo& result) +bool Options::checkForFile(const char* format, const char* dir, const char* rootName, FileInfo& result) const { - struct stat statBuffer; char possiblePath[strlen(dir)+strlen(rootName)+strlen(format)+8]; sprintf(possiblePath, format, dir, rootName); - bool found = (stat(possiblePath, &statBuffer) == 0); + bool found = result.checkFileExists(*this, possiblePath); if ( fTraceDylibSearching ) printf("[Logging for XBS]%sfound library: '%s'\n", (found ? " " : " not "), possiblePath); - if ( found ) { - result.path = strdup(possiblePath); - result.fileLen = statBuffer.st_size; - result.modTime = statBuffer.st_mtime; - return true; - } - return false; + return found; } -Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) +Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) const { FileInfo result; const int rootNameLen = strlen(rootName); // if rootName ends in .o there is no .a vs .dylib choice if ( (rootNameLen > 3) && (strcmp(&rootName[rootNameLen-2], ".o") == 0) ) { - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; @@ -613,19 +632,33 @@ Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) } } else { - bool lookForDylibs = ( fOutputKind != Options::kDyld); + bool lookForDylibs = false; + switch ( fOutputKind ) { + case Options::kDynamicExecutable: + case Options::kDynamicLibrary: + case Options::kDynamicBundle: + case Options::kObjectFile: // + lookForDylibs = true; + break; + case Options::kStaticExecutable: + case Options::kDyld: + case Options::kPreload: + case Options::kKextBundle: + lookForDylibs = false; + break; + } switch ( fLibrarySearchMode ) { case kSearchAllDirsForDylibsThenAllDirsForArchives: // first look in all directories for just for dylibs if ( lookForDylibs ) { - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; if ( checkForFile("%s/lib%s.dylib", dir, rootName, result) ) return result; } - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; @@ -635,7 +668,7 @@ Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) } // next look in all directories for just for archives if ( !dylibsOnly ) { - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; @@ -647,7 +680,7 @@ Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) case kSearchDylibAndArchiveInEachDir: // look in each directory for just for a dylib then for an archive - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; @@ -664,7 +697,7 @@ Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) throwf("library not found for -l%s", rootName); } -Options::FileInfo Options::findFramework(const char* frameworkName) +Options::FileInfo Options::findFramework(const char* frameworkName) const { if ( frameworkName == NULL ) throw "-framework missing next argument"; @@ -680,10 +713,9 @@ Options::FileInfo Options::findFramework(const char* frameworkName) return findFramework(name, suffix); } -Options::FileInfo Options::findFramework(const char* rootName, const char* suffix) +Options::FileInfo Options::findFramework(const char* rootName, const char* suffix) const { - struct stat statBuffer; - for (std::vector::iterator it = fFrameworkSearchPaths.begin(); + for (std::vector::const_iterator it = fFrameworkSearchPaths.begin(); it != fFrameworkSearchPaths.end(); it++) { // ??? Shouldn't we be using String here and just initializing it? @@ -703,15 +735,12 @@ Options::FileInfo Options::findFramework(const char* rootName, const char* suffi strcat(possiblePath, suffix); } } - bool found = (stat(possiblePath, &statBuffer) == 0); + FileInfo result; + bool found = result.checkFileExists(*this, possiblePath); if ( fTraceDylibSearching ) printf("[Logging for XBS]%sfound framework: '%s'\n", (found ? " " : " not "), possiblePath); if ( found ) { - FileInfo result; - result.path = strdup(possiblePath); - result.fileLen = statBuffer.st_size; - result.modTime = statBuffer.st_mtime; return result; } } @@ -722,15 +751,14 @@ Options::FileInfo Options::findFramework(const char* rootName, const char* suffi throwf("framework not found %s", rootName); } -Options::FileInfo Options::findFile(const char* path) +Options::FileInfo Options::findFile(const char* path) const { FileInfo result; - struct stat statBuffer; // if absolute path and not a .o file, the use SDK prefix if ( (path[0] == '/') && (strcmp(&path[strlen(path)-2], ".o") != 0) ) { const int pathLen = strlen(path); - for (std::vector::iterator it = fSDKPaths.begin(); it != fSDKPaths.end(); it++) { + for (std::vector::const_iterator it = fSDKPaths.begin(); it != fSDKPaths.end(); it++) { // ??? Shouldn't we be using String here? const char* sdkPathDir = *it; const int sdkPathDirLen = strlen(sdkPathDir); @@ -739,19 +767,13 @@ Options::FileInfo Options::findFile(const char* path) if ( possiblePath[sdkPathDirLen-1] == '/' ) possiblePath[sdkPathDirLen-1] = '\0'; strcat(possiblePath, path); - if ( stat(possiblePath, &statBuffer) == 0 ) { - result.path = strdup(possiblePath); - result.fileLen = statBuffer.st_size; - result.modTime = statBuffer.st_mtime; + if ( result.checkFileExists(*this, possiblePath) ) { return result; } } } // try raw path - if ( stat(path, &statBuffer) == 0 ) { - result.path = strdup(path); - result.fileLen = statBuffer.st_size; - result.modTime = statBuffer.st_mtime; + if ( result.checkFileExists(*this, path) ) { return result; } @@ -764,10 +786,7 @@ Options::FileInfo Options::findFile(const char* path) strcpy(&addPoint[1], &path[17]); else strcpy(newPath, &path[17]); - if ( stat(newPath, &statBuffer) == 0 ) { - result.path = strdup(newPath); - result.fileLen = statBuffer.st_size; - result.modTime = statBuffer.st_mtime; + if ( result.checkFileExists(*this, newPath) ) { return result; } } @@ -776,7 +795,7 @@ Options::FileInfo Options::findFile(const char* path) throwf("file not found: %s", path); } -Options::FileInfo Options::findFileUsingPaths(const char* path) +Options::FileInfo Options::findFileUsingPaths(const char* path) const { FileInfo result; @@ -802,7 +821,7 @@ Options::FileInfo Options::findFileUsingPaths(const char* path) // don't need to try variations, just paths. We do need to add the additional bits // onto the framework path though. if ( isFramework ) { - for (std::vector::iterator it = fFrameworkSearchPaths.begin(); + for (std::vector::const_iterator it = fFrameworkSearchPaths.begin(); it != fFrameworkSearchPaths.end(); it++) { const char* dir = *it; @@ -825,7 +844,7 @@ Options::FileInfo Options::findFileUsingPaths(const char* path) && (strcmp(&leafName[leafLen-6], ".dylib") == 0) && (strstr(path, ".framework/") != NULL) ); if ( !embeddedDylib ) { - for (std::vector::iterator it = fLibrarySearchPaths.begin(); + for (std::vector::const_iterator it = fLibrarySearchPaths.begin(); it != fLibrarySearchPaths.end(); it++) { const char* dir = *it; @@ -839,9 +858,10 @@ Options::FileInfo Options::findFileUsingPaths(const char* path) // If we didn't find it fall back to findFile. return findFile(path); } - -void Options::parseSegAddrTable(const char* segAddrPath, const char* installPath) + + +void Options::parseSegAddrTable(const char* segAddrPath, const char* installPth) { FILE* file = fopen(segAddrPath, "r"); if ( file == NULL ) { @@ -878,7 +898,7 @@ void Options::parseSegAddrTable(const char* segAddrPath, const char* installPath for(char* end = eol-1; (end > p) && isspace(*end); --end) *end = '\0'; // see if this line is for the dylib being linked - if ( strcmp(p, installPath) == 0 ) { + if ( strcmp(p, installPth) == 0 ) { fBaseAddress = firstColumAddress; if ( hasSecondColumn ) { fBaseWritableAddress = secondColumAddress; @@ -893,7 +913,7 @@ void Options::parseSegAddrTable(const char* segAddrPath, const char* installPath fclose(file); } -void Options::loadFileList(const char* fileOfPaths) +void Options::loadFileList(const char* fileOfPaths, ld::File::Ordinal baseOrdinal) { FILE* file; const char* comma = strrchr(fileOfPaths, ','); @@ -909,16 +929,21 @@ void Options::loadFileList(const char* fileOfPaths) realFileOfPaths[realFileOfPathsLen] = '\0'; file = fopen(realFileOfPaths, "r"); if ( file == NULL ) - throwf("-filelist file not found: %s\n", realFileOfPaths); + throwf("-filelist file '%s' could not be opened, errno=%d (%s)\n", realFileOfPaths, errno, strerror(errno)); + if ( this->dumpDependencyInfo() ) + this->dumpDependency(Options::depFileList, realFileOfPaths); } } else { file = fopen(fileOfPaths, "r"); if ( file == NULL ) - throwf("-filelist file not found: %s\n", fileOfPaths); + throwf("-filelist file '%s' could not be opened, errno=%d (%s)\n", fileOfPaths, errno, strerror(errno)); + if ( this->dumpDependencyInfo() ) + this->dumpDependency(Options::depFileList, fileOfPaths); } char path[PATH_MAX]; + ld::File::Ordinal previousOrdinal = baseOrdinal; while ( fgets(path, PATH_MAX, file) != NULL ) { path[PATH_MAX-1] = '\0'; char* eol = strchr(path, '\n'); @@ -929,16 +954,51 @@ void Options::loadFileList(const char* fileOfPaths) strcpy(builtPath, prefix); strcat(builtPath, "/"); strcat(builtPath, path); - fInputFiles.push_back(findFile(builtPath)); + if (fPipelineFifo != NULL) { + FileInfo info = FileInfo(builtPath); + info.ordinal = previousOrdinal.nextFileListOrdinal(); + previousOrdinal = info.ordinal; + info.fromFileList = true; + fInputFiles.push_back(info); + } else { + FileInfo info = findFile(builtPath); + info.ordinal = previousOrdinal.nextFileListOrdinal(); + previousOrdinal = info.ordinal; + info.fromFileList = true; + fInputFiles.push_back(info); + } } else { - fInputFiles.push_back(findFile(path)); + if (fPipelineFifo != NULL) { + FileInfo info = FileInfo(path); + info.ordinal = previousOrdinal.nextFileListOrdinal(); + previousOrdinal = info.ordinal; + info.fromFileList = true; + fInputFiles.push_back(info); + } else { + FileInfo info = findFile(path); + info.ordinal = previousOrdinal.nextFileListOrdinal(); + previousOrdinal = info.ordinal; + info.fromFileList = true; + fInputFiles.push_back(info); + } } } fclose(file); } -bool Options::SetWithWildcards::hasWildCards(const char* symbol) + +void Options::SetWithWildcards::remove(const NameSet& toBeRemoved) +{ + for(NameSet::const_iterator it=toBeRemoved.begin(); it != toBeRemoved.end(); ++it) { + const char* symbolName = *it; + NameSet::iterator pos = fRegular.find(symbolName); + if ( pos != fRegular.end() ) + fRegular.erase(pos); + } +} + +bool Options::SetWithWildcards::hasWildCards(const char* symbol) { // an exported symbol name containing *, ?, or [ requires wildcard matching return ( strpbrk(symbol, "*?[") != NULL ); @@ -952,21 +1012,28 @@ void Options::SetWithWildcards::insert(const char* symbol) fRegular.insert(symbol); } -bool Options::SetWithWildcards::contains(const char* symbol) +bool Options::SetWithWildcards::contains(const char* symbol) const { // first look at hash table on non-wildcard symbols if ( fRegular.find(symbol) != fRegular.end() ) return true; // next walk list of wild card symbols looking for a match - for(std::vector::iterator it = fWildCard.begin(); it != fWildCard.end(); ++it) { + for(std::vector::const_iterator it = fWildCard.begin(); it != fWildCard.end(); ++it) { if ( wildCardMatch(*it, symbol) ) return true; } return false; } +bool Options::SetWithWildcards::containsNonWildcard(const char* symbol) const +{ + // look at hash table on non-wildcard symbols + return ( fRegular.find(symbol) != fRegular.end() ); +} + -bool Options::SetWithWildcards::inCharRange(const char*& p, unsigned char c) + +bool Options::SetWithWildcards::inCharRange(const char*& p, unsigned char c) const { ++p; // find end const char* b = p; @@ -995,7 +1062,7 @@ bool Options::SetWithWildcards::inCharRange(const char*& p, unsigned char c) return false; } -bool Options::SetWithWildcards::wildCardMatch(const char* pattern, const char* symbol) +bool Options::SetWithWildcards::wildCardMatch(const char* pattern, const char* symbol) const { const char* s = symbol; for (const char* p = pattern; *p != '\0'; ++p) { @@ -1030,6 +1097,8 @@ bool Options::SetWithWildcards::wildCardMatch(const char* pattern, const char* s void Options::loadExportFile(const char* fileOfExports, const char* option, SetWithWildcards& set) { + if ( fileOfExports == NULL ) + throwf("missing file after %s", option); // read in whole file int fd = ::open(fileOfExports, O_RDONLY, 0); if ( fd == -1 ) @@ -1043,9 +1112,12 @@ void Options::loadExportFile(const char* fileOfExports, const char* option, SetW if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) throwf("can't read %s file: %s", option, fileOfExports); + if ( this->dumpDependencyInfo() ) + this->dumpDependency(Options::depMisc, fileOfExports); + ::close(fd); - // parse into symbols and add to hash_set + // parse into symbols and add to unordered_set char * const end = &p[stat_buf.st_size]; enum { lineStart, inSymbol, inComment } state = lineStart; char* symbolStart = NULL; @@ -1114,9 +1186,11 @@ void Options::parseAliasFile(const char* fileOfAliases) throwf("can't read alias file: %s", fileOfAliases); p[stat_buf.st_size] = '\n'; ::close(fd); + if ( this->dumpDependencyInfo() ) + this->dumpDependency(Options::depMisc, fileOfAliases); // parse into symbols and add to fAliases - ObjectFile::ReaderOptions::AliasPair pair; + AliasPair pair; char * const end = &p[stat_buf.st_size+1]; enum { lineStart, inRealName, inBetween, inAliasName, inComment } state = lineStart; int lineNumber = 1; @@ -1162,7 +1236,7 @@ void Options::parseAliasFile(const char* fileOfAliases) *last = '\0'; --last; } - fReaderOptions.fAliases.push_back(pair); + fAliases.push_back(pair); state = inComment; } else if ( *s == '\n' ) { @@ -1173,7 +1247,7 @@ void Options::parseAliasFile(const char* fileOfAliases) *last = '\0'; --last; } - fReaderOptions.fAliases.push_back(pair); + fAliases.push_back(pair); state = lineStart; } break; @@ -1227,69 +1301,44 @@ void Options::setMacOSXVersionMin(const char* version) throw "-macosx_version_min argument missing"; if ( (strncmp(version, "10.", 3) == 0) && isdigit(version[3]) ) { - int num = version[3] - '0'; - switch ( num ) { - case 0: - case 1: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_1; - break; - case 2: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_2; - break; - case 3: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_3; - break; - case 4: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_4; - break; - case 5: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_5; - break; - case 6: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_6; - break; - default: - fReaderOptions.fMacVersionMin = ObjectFile::ReaderOptions::k10_6; - break; - } + unsigned int minorVersion = 0; + for (int i=3; isdigit(version[i]); ++i) { + minorVersion = minorVersion*10 + (version[i] - '0'); + } + if ( minorVersion > 255 ) { + warning("Mac OS X minor version > 255 in '%s'", version); + minorVersion = 255; + } + fMacVersionMin = (ld::MacVersionMin)(0x000A0000 | (minorVersion << 8)); } else { warning("unknown option to -macosx_version_min, not 10.x"); } } -void Options::setIPhoneVersionMin(const char* version) +void Options::setIOSVersionMin(const char* version) { if ( version == NULL ) - throw "-iphoneos_version_min argument missing"; + throw "-ios_version_min argument missing"; if ( ! isdigit(version[0]) ) - throw "-iphoneos_version_min argument is not a number"; + throw "-ios_version_min argument is not a number"; if ( version[1] != '.' ) - throw "-iphoneos_version_min argument is missing period as second character"; + throw "-ios_version_min argument is missing period as second character"; if ( ! isdigit(version[2]) ) - throw "-iphoneos_version_min argument is not a number"; - - if ( version[0] == '2' ) - fReaderOptions.fIPhoneVersionMin = ObjectFile::ReaderOptions::k2_0; - else if ( (version[0] == '3') && (version[2] == '0') ) - fReaderOptions.fIPhoneVersionMin = ObjectFile::ReaderOptions::k2_0; - else if ( (version[0] == '3') && (version[2] >= '1') ) - fReaderOptions.fIPhoneVersionMin = ObjectFile::ReaderOptions::k3_1; - else if ( (version[0] >= '4') ) - fReaderOptions.fIPhoneVersionMin = ObjectFile::ReaderOptions::k3_1; - else { - fReaderOptions.fIPhoneVersionMin = ObjectFile::ReaderOptions::k2_0; - warning("unknown option to -iphoneos_version_min, not 2.x, 3.x, or 4.x"); - } + throw "-ios_version_min argument is not a number"; + + unsigned int majorVersion = version[0] - '0'; + unsigned int minorVersion = version[2] - '0'; + fIOSVersionMin = (ld::IOSVersionMin)((majorVersion << 16) | (minorVersion << 8)); } -bool Options::minOS(ObjectFile::ReaderOptions::MacVersionMin requiredMacMin, ObjectFile::ReaderOptions::IPhoneVersionMin requirediPhoneOSMin) +bool Options::minOS(ld::MacVersionMin requiredMacMin, ld::IOSVersionMin requirediPhoneOSMin) { - if ( fReaderOptions.fMacVersionMin != ObjectFile::ReaderOptions::kMinMacVersionUnset ) { - return ( fReaderOptions.fMacVersionMin >= requiredMacMin ); + if ( fMacVersionMin != ld::macVersionUnset ) { + return ( fMacVersionMin >= requiredMacMin ); } else { - return ( fReaderOptions.fIPhoneVersionMin >= requirediPhoneOSMin); + return ( fIOSVersionMin >= requirediPhoneOSMin); } } @@ -1372,16 +1421,67 @@ uint32_t Options::parseProtection(const char* prot) } +// +// Parses number of form A[.B[.B[.D[.E]]]] into a uint64_t where the bits are a24.b10.c10.d10.e10 +// +uint64_t Options::parseVersionNumber64(const char* versionString) +{ + uint64_t a = 0; + uint64_t b = 0; + uint64_t c = 0; + uint64_t d = 0; + uint64_t e = 0; + char* end; + a = strtoul(versionString, &end, 10); + if ( *end == '.' ) { + b = strtoul(&end[1], &end, 10); + if ( *end == '.' ) { + c = strtoul(&end[1], &end, 10); + if ( *end == '.' ) { + d = strtoul(&end[1], &end, 10); + if ( *end == '.' ) { + e = strtoul(&end[1], &end, 10); + } + } + } + } + if ( (*end != '\0') || (a > 0xFFFFFF) || (b > 0x3FF) || (c > 0x3FF) || (d > 0x3FF) || (e > 0x3FF) ) + throwf("malformed 64-bit a.b.c.d.e version number: %s", versionString); + + return (a << 40) | ( b << 30 ) | ( c << 20 ) | ( d << 10 ) | e; +} + + +uint32_t Options::currentVersion32() const +{ + // warn if it does not fit into 32 bit vers number + uint32_t a = (fDylibCurrentVersion >> 40) & 0xFFFF; + uint32_t b = (fDylibCurrentVersion >> 30) & 0xFF; + uint32_t c = (fDylibCurrentVersion >> 20) & 0xFF; + uint64_t rep32 = ((uint64_t)a << 40) | ((uint64_t)b << 30) | ((uint64_t)c << 20); + if ( rep32 != fDylibCurrentVersion ) { + warning("truncating -current_version to fit in 32-bit space used by old mach-o format"); + a = (fDylibCurrentVersion >> 40) & 0xFFFFFF; + if ( a > 0xFFFF ) + a = 0xFFFF; + b = (fDylibCurrentVersion >> 30) & 0x3FF; + if ( b > 0xFF ) + b = 0xFF; + c = (fDylibCurrentVersion >> 20) & 0x3FF; + if ( c > 0xFF ) + c = 0xFF; + } + return (a << 16) | ( b << 8 ) | c; +} // // Parses number of form X[.Y[.Z]] into a uint32_t where the nibbles are xxxx.yy.zz // -// -uint32_t Options::parseVersionNumber(const char* versionString) +uint32_t Options::parseVersionNumber32(const char* versionString) { - unsigned long x = 0; - unsigned long y = 0; - unsigned long z = 0; + uint32_t x = 0; + uint32_t y = 0; + uint32_t z = 0; char* end; x = strtoul(versionString, &end, 10); if ( *end == '.' ) { @@ -1391,7 +1491,7 @@ uint32_t Options::parseVersionNumber(const char* versionString) } } if ( (*end != '\0') || (x > 0xffff) || (y > 0xff) || (z > 0xff) ) - throwf("malformed version number: %s", versionString); + throwf("malformed 32-bit x.y.z version number: %s", versionString); return (x << 16) | ( y << 8 ) | z; } @@ -1478,7 +1578,7 @@ static const char* cstringSymbolName(const char* orderFileString) void Options::parseOrderFile(const char* path, bool cstring) { // order files override auto-ordering - fReaderOptions.fAutoOrderInitializers = false; + fAutoOrderInitializers = false; // read in whole file int fd = ::open(path, O_RDONLY, 0); @@ -1493,6 +1593,8 @@ void Options::parseOrderFile(const char* path, bool cstring) throwf("can't read order file: %s", path); ::close(fd); p[stat_buf.st_size] = '\n'; + if ( this->dumpDependencyInfo() ) + this->dumpDependency(Options::depMisc, path); // parse into vector of pairs char * const end = &p[stat_buf.st_size+1]; @@ -1519,18 +1621,12 @@ void Options::parseOrderFile(const char* path, bool cstring) *last = '\0'; --last; } + // if there is an architecture prefix, only use this symbol it if matches current arch if ( strncmp(symbolStart, "ppc:", 4) == 0 ) { - if ( fArchitecture == CPU_TYPE_POWERPC ) - symbolStart = &symbolStart[4]; - else - symbolStart = NULL; + symbolStart = NULL; } - // if there is an architecture prefix, only use this symbol it if matches current arch else if ( strncmp(symbolStart, "ppc64:", 6) == 0 ) { - if ( fArchitecture == CPU_TYPE_POWERPC64 ) - symbolStart = &symbolStart[6]; - else - symbolStart = NULL; + symbolStart = NULL; } else if ( strncmp(symbolStart, "i386:", 5) == 0 ) { if ( fArchitecture == CPU_TYPE_I386 ) @@ -1558,6 +1654,14 @@ void Options::parseOrderFile(const char* path, bool cstring) objFileName = symbolStart; symbolStart = &colon[3]; } + else { + colon = strstr(symbolStart, ".o):"); + if ( colon != NULL ) { + colon[3] = '\0'; + objFileName = symbolStart; + symbolStart = &colon[4]; + } + } // trim leading spaces while ( isspace(*symbolStart) ) ++symbolStart; @@ -1628,6 +1732,27 @@ void Options::addSection(const char* segment, const char* section, const char* p fExtraSections.push_back(info); } +void Options::addSectionRename(const char* srcSegment, const char* srcSection, const char* dstSegment, const char* dstSection) +{ + if ( strlen(srcSegment) > 16 ) + throw "-rename_section segment name max 16 chars"; + if ( strlen(srcSection) > 16 ) + throw "-rename_section section name max 16 chars"; + if ( strlen(dstSegment) > 16 ) + throw "-rename_section segment name max 16 chars"; + if ( strlen(dstSection) > 16 ) + throw "-rename_section section name max 16 chars"; + + SectionRename info; + info.fromSegment = srcSegment; + info.fromSection = srcSection; + info.toSegment = dstSegment; + info.toSection = dstSection; + + fSectionRenames.push_back(info); +} + + void Options::addSectionAlignment(const char* segment, const char* section, const char* alignmentStr) { if ( strlen(segment) > 16 ) @@ -1694,6 +1819,9 @@ void Options::warnObsolete(const char* arg) // void Options::parse(int argc, const char* argv[]) { + // Store the original args in the link snapshot. + fLinkSnapshot.recordRawArgs(argc, argv); + // pass one builds search list from -L and -F options this->buildSearchPaths(argc, argv); @@ -1705,12 +1833,19 @@ void Options::parse(int argc, const char* argv[]) const char* arg = argv[i]; if ( arg[0] == '-' ) { + // by default, copy one arg to the snapshot link command, and do no file copying + int snapshotArgIndex = i; + int snapshotArgCount = -1; // -1 means compute count based on change in index + int snapshotFileArgIndex = -1; // -1 means no data file parameter to arg // Since we don't care about the files passed, just the option names, we do this here. if (fPrintOptions) fprintf (stderr, "[Logging ld64 options]\t%s\n", arg); if ( (arg[1] == 'L') || (arg[1] == 'F') ) { + snapshotArgCount = 0; // stripped out of link snapshot + if (arg[2] == '\0') + ++i; // previously handled by buildSearchPaths() } // The one gnu style option we have to keep compatibility @@ -1727,8 +1862,8 @@ void Options::parse(int argc, const char* argv[]) // default } else if ( strcmp(arg, "-static") == 0 ) { - fReaderOptions.fForStatic = true; - if ( fOutputKind != kObjectFile ) { + fForStatic = true; + if ( (fOutputKind != kObjectFile) && (fOutputKind != kKextBundle) ) { fOutputKind = kStaticExecutable; } } @@ -1755,26 +1890,41 @@ void Options::parse(int argc, const char* argv[]) fOutputKind = kKextBundle; } else if ( strcmp(arg, "-o") == 0 ) { + snapshotArgCount = 0; fOutputFile = argv[++i]; - } - else if ( (arg[1] == 'l') && (strncmp(arg,"-lazy_",6) !=0) ) { - addLibrary(findLibrary(&arg[2])); - } - // This causes a dylib to be weakly bound at - // link time. This corresponds to weak_import. - else if ( strncmp(arg, "-weak-l", 7) == 0 ) { - FileInfo info = findLibrary(&arg[7]); - info.options.fWeakImport = true; - addLibrary(info); + fLinkSnapshot.setSnapshotName(fOutputFile); } else if ( strncmp(arg, "-lazy-l", 7) == 0 ) { + snapshotArgCount = 0; FileInfo info = findLibrary(&arg[7], true); info.options.fLazyLoad = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); fUsingLazyDylibLinking = true; } + else if ( strcmp(arg, "-lto_library") == 0 ) { + snapshotFileArgIndex = 1; + fOverridePathlibLTO = argv[++i]; + if ( fOverridePathlibLTO == NULL ) + throw "missing argument to -lto_library"; + } + else if ( (arg[1] == 'l') && (strncmp(arg,"-lazy_",6) !=0) ) { + snapshotArgCount = 0; + FileInfo info = findLibrary(&arg[2]); + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); + } + // This causes a dylib to be weakly bound at + // link time. This corresponds to weak_import. + else if ( strncmp(arg, "-weak-l", 7) == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; + FileInfo info = findLibrary(&arg[7]); + info.options.fWeakImport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); + } // Avoid lazy binding. - // ??? Deprecate. else if ( strcmp(arg, "-bind_at_load") == 0 ) { fBindAtLoad = true; } @@ -1792,19 +1942,20 @@ void Options::parse(int argc, const char* argv[]) } // Similar to --whole-archive. else if ( strcmp(arg, "-all_load") == 0 ) { - fReaderOptions.fFullyLoadArchives = true; + fFullyLoadArchives = true; } else if ( strcmp(arg, "-noall_load") == 0) { warnObsolete(arg); } // Similar to -all_load else if ( strcmp(arg, "-ObjC") == 0 ) { - fReaderOptions.fLoadAllObjcObjectsFromArchives = true; + fLoadAllObjcObjectsFromArchives = true; } // Similar to -all_load, but for the following archive only. else if ( strcmp(arg, "-force_load") == 0 ) { FileInfo info = findFile(argv[++i]); info.options.fForceLoad = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } // Library versioning. @@ -1813,22 +1964,24 @@ void Options::parse(int argc, const char* argv[]) const char* vers = argv[++i]; if ( vers == NULL ) throw "-dylib_compatibility_version missing "; - fDylibCompatVersion = parseVersionNumber(vers); + fDylibCompatVersion = parseVersionNumber32(vers); } else if ( (strcmp(arg, "-dylib_current_version") == 0) || (strcmp(arg, "-current_version") == 0)) { const char* vers = argv[++i]; if ( vers == NULL ) throw "-dylib_current_version missing "; - fDylibCurrentVersion = parseVersionNumber(vers); + fDylibCurrentVersion = parseVersionNumber64(vers); } else if ( strcmp(arg, "-sectorder") == 0 ) { if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) throw "-sectorder missing
"; + snapshotFileArgIndex = 3; parseSectionOrderFile(argv[i+1], argv[i+2], argv[i+3]); i += 3; } else if ( strcmp(arg, "-order_file") == 0 ) { + snapshotFileArgIndex = 1; parseOrderFile(argv[++i], false); } else if ( strcmp(arg, "-order_file_statistics") == 0 ) { @@ -1839,6 +1992,7 @@ void Options::parse(int argc, const char* argv[]) else if ( (strcmp(arg, "-sectcreate") == 0) || (strcmp(arg, "-segcreate") == 0) ) { if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) throw "-sectcreate missing
"; + snapshotFileArgIndex = 3; addSection(argv[i+1], argv[i+2], argv[i+3]); i += 3; } @@ -1847,7 +2001,7 @@ void Options::parse(int argc, const char* argv[]) || (strcmp(arg, "-dylinker_install_name") == 0) || (strcmp(arg, "-install_name") == 0)) { fDylibInstallName = argv[++i]; - if ( fDylibInstallName == NULL ) + if ( fDylibInstallName == NULL ) throw "-install_name missing "; } // Sets the base address of the output. @@ -1867,10 +2021,12 @@ void Options::parse(int argc, const char* argv[]) } // Same as -@ from the FSF linker. else if ( strcmp(arg, "-filelist") == 0 ) { + snapshotArgCount = 0; const char* path = argv[++i]; if ( (path == NULL) || (path[0] == '-') ) throw "-filelist missing "; - loadFileList(path); + ld::File::Ordinal baseOrdinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + loadFileList(path, baseOrdinal); } else if ( strcmp(arg, "-keep_private_externs") == 0 ) { fKeepPrivateExterns = true; @@ -1892,6 +2048,7 @@ void Options::parse(int argc, const char* argv[]) } } else if ( strcmp(arg, "-interposable_list") == 0 ) { + snapshotFileArgIndex = 1; fInterposeMode = kInterposeSome; loadExportFile(argv[++i], "-interposable_list", fInterposeList); } @@ -1900,12 +2057,14 @@ void Options::parse(int argc, const char* argv[]) fInterposeMode = kInterposeNone; } else if ( strcmp(arg, "-exported_symbols_list") == 0 ) { + snapshotFileArgIndex = 1; if ( fExportMode == kDontExportSome ) throw "can't use -exported_symbols_list and -unexported_symbols_list"; fExportMode = kExportSome; loadExportFile(argv[++i], "-exported_symbols_list", fExportSymbols); } else if ( strcmp(arg, "-unexported_symbols_list") == 0 ) { + snapshotFileArgIndex = 1; if ( fExportMode == kExportSome ) throw "can't use -unexported_symbols_list and -exported_symbols_list"; fExportMode = kDontExportSome; @@ -1924,12 +2083,14 @@ void Options::parse(int argc, const char* argv[]) fDontExportSymbols.insert(argv[++i]); } else if ( strcmp(arg, "-non_global_symbols_no_strip_list") == 0 ) { + snapshotFileArgIndex = 1; if ( fLocalSymbolHandling == kLocalSymbolsSelectiveExclude ) throw "can't use -non_global_symbols_no_strip_list and -non_global_symbols_strip_list"; fLocalSymbolHandling = kLocalSymbolsSelectiveInclude; loadExportFile(argv[++i], "-non_global_symbols_no_strip_list", fLocalSymbolsIncluded); } else if ( strcmp(arg, "-non_global_symbols_strip_list") == 0 ) { + snapshotFileArgIndex = 1; if ( fLocalSymbolHandling == kLocalSymbolsSelectiveInclude ) throw "can't use -non_global_symbols_no_strip_list and -non_global_symbols_strip_list"; fLocalSymbolHandling = kLocalSymbolsSelectiveExclude; @@ -1940,39 +2101,58 @@ void Options::parse(int argc, const char* argv[]) fIgnoreOtherArchFiles = true; } else if ( strcmp(arg, "-force_cpusubtype_ALL") == 0 ) { - fForceSubtypeAll = true; + fForceSubtypeAll = true; + fAllowCpuSubtypeMismatches = true; } // Similar to -weak-l but uses the absolute path name to the library. else if ( strcmp(arg, "-weak_library") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFile(argv[++i]); info.options.fWeakImport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } else if ( strcmp(arg, "-lazy_library") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFile(argv[++i]); info.options.fLazyLoad = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); fUsingLazyDylibLinking = true; } else if ( strcmp(arg, "-framework") == 0 ) { - addLibrary(findFramework(argv[++i])); + snapshotArgCount = 0; + FileInfo info = findFramework(argv[++i]); + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); } else if ( strcmp(arg, "-weak_framework") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFramework(argv[++i]); info.options.fWeakImport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } else if ( strcmp(arg, "-lazy_framework") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFramework(argv[++i]); info.options.fLazyLoad = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); fUsingLazyDylibLinking = true; } else if ( strcmp(arg, "-search_paths_first") == 0 ) { // previously handled by buildSearchPaths() } + else if ( strcmp(arg, "-search_dylibs_first") == 0 ) { + // previously handled by buildSearchPaths() + } else if ( strcmp(arg, "-undefined") == 0 ) { - setUndefinedTreatment(argv[++i]); + setUndefinedTreatment(argv[++i]); } // Debugging output flag. else if ( strcmp(arg, "-arch_multiple") == 0 ) { @@ -2008,7 +2188,7 @@ void Options::parse(int argc, const char* argv[]) // Warn, error or make strong a mismatch between weak // and non-weak references. else if ( strcmp(arg, "-weak_reference_mismatches") == 0 ) { - setWeakReferenceMismatchTreatment(argv[++i]); + setWeakReferenceMismatchTreatment(argv[++i]); } // For a deployment target of 10.3 and earlier ld64 will // prebind an executable with 0s in all addresses that @@ -2036,6 +2216,8 @@ void Options::parse(int argc, const char* argv[]) // This should probably be deprecated when we respect -L and -F // when searching for libraries. else if ( strcmp(arg, "-dylib_file") == 0 ) { + // ignore for snapshot because a stub dylib will be created in the snapshot + snapshotArgCount = 0; addDylibOverride(argv[++i]); } // What to expand @executable_path to if found in dependent dylibs @@ -2076,9 +2258,9 @@ void Options::parse(int argc, const char* argv[]) if ( (seg.name == NULL) || (argv[i+1] == NULL) ) throw "-segaddr missing segName Adddress"; seg.address = parseAddress(argv[++i]); - uint64_t temp = seg.address & (-4096); // page align - if ( (seg.address != temp) ) - warning("-segaddr %s not page aligned, rounding down", seg.name); + uint64_t temp = ((seg.address+fSegmentAlignment-1) & (-fSegmentAlignment)); + if ( seg.address != temp ) + warning("-segaddr %s not %lld byte aligned", seg.name, fSegmentAlignment); fCustomSegmentAddresses.push_back(seg); } // ??? Deprecate when we deprecate split-seg. @@ -2092,6 +2274,7 @@ void Options::parse(int argc, const char* argv[]) } // ??? Deprecate when we get rid of basing at build time. else if ( strcmp(arg, "-seg_addr_table") == 0 ) { + snapshotFileArgIndex = 1; const char* name = argv[++i]; if ( name == NULL ) throw "-seg_addr_table missing argument"; @@ -2138,6 +2321,9 @@ void Options::parse(int argc, const char* argv[]) else if ( strcmp(arg, "-allow_stack_execute") == 0 ) { fExecutableStack = true; } + else if ( strcmp(arg, "-allow_heap_execute") == 0 ) { + fDisableNonExecutableHeap = true; + } else if ( strcmp(arg, "-sectalign") == 0 ) { if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) throw "-sectalign missing
"; @@ -2152,10 +2338,12 @@ void Options::parse(int argc, const char* argv[]) i += 2; } else if ( strcmp(arg, "-bundle_loader") == 0 ) { + snapshotFileArgIndex = 1; fBundleLoader = argv[++i]; if ( (fBundleLoader == NULL) || (fBundleLoader[0] == '-') ) throw "-bundle_loader missing "; FileInfo info = findFile(fBundleLoader); + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); info.options.fBundleLoader = true; fInputFiles.push_back(info); } @@ -2167,10 +2355,34 @@ void Options::parse(int argc, const char* argv[]) } // Use this flag to set default behavior for deployement targets. else if ( strcmp(arg, "-macosx_version_min") == 0 ) { - setMacOSXVersionMin(argv[++i]); + const char* macVers = argv[++i]; + const char* envMacVers = getenv("MACOSX_DEPLOYMENT_TARGET"); + const char* enviPhoneVers = getenv("IPHONEOS_DEPLOYMENT_TARGET"); + if ( (envMacVers != NULL) && (enviPhoneVers != NULL) ) { + // when conflicting deployments set, break tie by looking at syslibroot + warning("both MACOSX_DEPLOYMENT_TARGET and IPHONEOS_DEPLOYMENT_TARGET are set"); + if ( !fSDKPaths.empty() ) { + const char* sysrootPath = fSDKPaths.back(); + const char* lastSlash = strrchr(sysrootPath, '/'); + if ( strstr(lastSlash, "Simulator") != NULL ) + setIOSVersionMin(enviPhoneVers); + else + setMacOSXVersionMin(macVers); + } + else { + setMacOSXVersionMin(macVers); + } + } + else { + setMacOSXVersionMin(macVers); + } } - else if ( strcmp(arg, "-iphoneos_version_min") == 0 ) { - setIPhoneVersionMin(argv[++i]); + else if ( (strcmp(arg, "-ios_version_min") == 0) || (strcmp(arg, "-iphoneos_version_min") == 0) ) { + setIOSVersionMin(argv[++i]); + } + else if ( strcmp(arg, "-ios_simulator_version_min") == 0 ) { + setIOSVersionMin(argv[++i]); + fTargetIOSSimulator = true; } else if ( strcmp(arg, "-multiply_defined") == 0 ) { //warnObsolete(arg); @@ -2199,7 +2411,7 @@ void Options::parse(int argc, const char* argv[]) warnObsolete(arg); } else if ( (strcmp(arg, "-why_load") == 0) || (strcmp(arg, "-whyload") == 0) ) { - fReaderOptions.fWhyLoad = true; + fWhyLoad = true; } else if ( strcmp(arg, "-why_live") == 0 ) { const char* name = argv[++i]; @@ -2222,40 +2434,43 @@ void Options::parse(int argc, const char* argv[]) else if ( strcmp(arg, "-s") == 0 ) { warnObsolete(arg); fLocalSymbolHandling = kLocalSymbolsNone; - fReaderOptions.fDebugInfoStripping = ObjectFile::ReaderOptions::kDebugInfoNone; + fDebugInfoStripping = Options::kDebugInfoNone; } else if ( strcmp(arg, "-x") == 0 ) { fLocalSymbolHandling = kLocalSymbolsNone; } else if ( strcmp(arg, "-S") == 0 ) { - fReaderOptions.fDebugInfoStripping = ObjectFile::ReaderOptions::kDebugInfoNone; + fDebugInfoStripping = Options::kDebugInfoNone; } else if ( strcmp(arg, "-X") == 0 ) { warnObsolete(arg); } else if ( strcmp(arg, "-Si") == 0 ) { warnObsolete(arg); - fReaderOptions.fDebugInfoStripping = ObjectFile::ReaderOptions::kDebugInfoFull; + fDebugInfoStripping = Options::kDebugInfoFull; } else if ( strcmp(arg, "-b") == 0 ) { warnObsolete(arg); } else if ( strcmp(arg, "-Sn") == 0 ) { warnObsolete(arg); - fReaderOptions.fDebugInfoStripping = ObjectFile::ReaderOptions::kDebugInfoFull; + fDebugInfoStripping = Options::kDebugInfoFull; } else if ( strcmp(arg, "-Sp") == 0 ) { warnObsolete(arg); } else if ( strcmp(arg, "-dead_strip") == 0 ) { - fDeadStrip = kDeadStripOnPlusUnusedInits; + fDeadStrip = true; } else if ( strcmp(arg, "-no_dead_strip_inits_and_terms") == 0 ) { - fDeadStrip = kDeadStripOn; + fDeadStrip = true; } else if ( strcmp(arg, "-w") == 0 ) { // previously handled by buildSearchPaths() } + else if ( strcmp(arg, "-fatal_warnings") == 0 ) { + // previously handled by buildSearchPaths() + } else if ( strcmp(arg, "-arch_errors_fatal") == 0 ) { fErrorOnOtherArchFiles = true; } @@ -2272,10 +2487,10 @@ void Options::parse(int argc, const char* argv[]) fMaxMinimumHeaderPad = true; } else if ( strcmp(arg, "-t") == 0 ) { - fReaderOptions.fLogAllFiles = true; + fLogAllFiles = true; } else if ( strcmp(arg, "-whatsloaded") == 0 ) { - fReaderOptions.fLogObjectFiles = true; + fLogObjectFiles = true; } else if ( strcmp(arg, "-A") == 0 ) { warnObsolete(arg); @@ -2346,7 +2561,7 @@ void Options::parse(int argc, const char* argv[]) fStatistics = true; } else if ( strcmp(arg, "-d") == 0 ) { - fReaderOptions.fMakeTentativeDefinitionsReal = true; + fMakeTentativeDefinitionsReal = true; } else if ( strcmp(arg, "-v") == 0 ) { // previously handled by buildSearchPaths() @@ -2355,6 +2570,7 @@ void Options::parse(int argc, const char* argv[]) // previously handled by buildSearchPaths() } else if ( strcmp(arg, "-syslibroot") == 0 ) { + snapshotArgCount = 0; ++i; // previously handled by buildSearchPaths() } @@ -2365,42 +2581,32 @@ void Options::parse(int argc, const char* argv[]) fUUIDMode = kUUIDRandom; } else if ( strcmp(arg, "-dtrace") == 0 ) { + snapshotFileArgIndex = 1; const char* name = argv[++i]; if ( name == NULL ) throw "-dtrace missing argument"; fDtraceScriptName = name; } else if ( strcmp(arg, "-root_safe") == 0 ) { - fReaderOptions.fRootSafe = true; + fRootSafe = true; } else if ( strcmp(arg, "-setuid_safe") == 0 ) { - fReaderOptions.fSetuidSafe = true; + fSetuidSafe = true; } else if ( strcmp(arg, "-alias") == 0 ) { - ObjectFile::ReaderOptions::AliasPair pair; + Options::AliasPair pair; pair.realName = argv[++i]; if ( pair.realName == NULL ) throw "missing argument to -alias"; pair.alias = argv[++i]; if ( pair.alias == NULL ) throw "missing argument to -alias"; - fReaderOptions.fAliases.push_back(pair); + fAliases.push_back(pair); } else if ( strcmp(arg, "-alias_list") == 0 ) { + snapshotFileArgIndex = 1; parseAliasFile(argv[++i]); } - // put this last so that it does not interfer with other options starting with 'i' - else if ( strncmp(arg, "-i", 2) == 0 ) { - const char* colon = strchr(arg, ':'); - if ( colon == NULL ) - throwf("unknown option: %s", arg); - ObjectFile::ReaderOptions::AliasPair pair; - char* temp = new char[colon-arg]; - strlcpy(temp, &arg[2], colon-arg-1); - pair.realName = &colon[1]; - pair.alias = temp; - fReaderOptions.fAliases.push_back(pair); - } else if ( strcmp(arg, "-save-temps") == 0 ) { fSaveTempFiles = true; } @@ -2423,27 +2629,64 @@ void Options::parse(int argc, const char* argv[]) } else if ( strcmp(arg, "-pie") == 0 ) { fPositionIndependentExecutable = true; + fPIEOnCommandLine = true; + } + else if ( strcmp(arg, "-no_pie") == 0 ) { + fDisablePositionIndependentExecutable = true; } else if ( strncmp(arg, "-reexport-l", 11) == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findLibrary(&arg[11], true); info.options.fReExport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } else if ( strcmp(arg, "-reexport_library") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFile(argv[++i]); info.options.fReExport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } else if ( strcmp(arg, "-reexport_framework") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; FileInfo info = findFramework(argv[++i]); info.options.fReExport = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); + } + else if ( strncmp(arg, "-upward-l", 9) == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; + FileInfo info = findLibrary(&arg[9], true); + info.options.fUpward = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); + } + else if ( strcmp(arg, "-upward_library") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; + FileInfo info = findFile(argv[++i]); + info.options.fUpward = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); + addLibrary(info); + } + else if ( strcmp(arg, "-upward_framework") == 0 ) { + // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) + snapshotArgCount = 0; + FileInfo info = findFramework(argv[++i]); + info.options.fUpward = true; + info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); addLibrary(info); } else if ( strcmp(arg, "-dead_strip_dylibs") == 0 ) { fDeadStripDylibs = true; } else if ( strcmp(arg, "-no_implicit_dylibs") == 0 ) { - fReaderOptions.fImplicitlyLinkPublicDylibs = false; + fImplicitlyLinkPublicDylibs = false; } else if ( strcmp(arg, "-new_linker") == 0 ) { // ignore @@ -2452,7 +2695,7 @@ void Options::parse(int argc, const char* argv[]) fEncryptable = false; } else if ( strcmp(arg, "-no_compact_unwind") == 0 ) { - fReaderOptions.fAddCompactUnwindEncoding = false; + fAddCompactUnwindEncoding = false; } else if ( strcmp(arg, "-mllvm") == 0 ) { const char* opts = argv[++i]; @@ -2460,8 +2703,14 @@ void Options::parse(int argc, const char* argv[]) throw "missing argument to -mllvm"; fLLVMOptions.push_back(opts); } + else if ( strcmp(arg, "-mcpu") == 0 ) { + const char* cpu = argv[++i]; + if ( cpu == NULL ) + throw "missing argument to -mcpu"; + fLtoCpu = cpu; + } else if ( strcmp(arg, "-no_order_inits") == 0 ) { - fReaderOptions.fAutoOrderInitializers = false; + fAutoOrderInitializers = false; } else if ( strcmp(arg, "-no_order_data") == 0 ) { fOrderData = false; @@ -2481,29 +2730,265 @@ void Options::parse(int argc, const char* argv[]) fMarkDeadStrippableDylib = true; } else if ( strcmp(arg, "-exported_symbols_order") == 0 ) { + snapshotFileArgIndex = 1; loadSymbolOrderFile(argv[++i], fExportSymbolsOrder); } else if ( strcmp(arg, "-no_compact_linkedit") == 0 ) { - fMakeCompressedDyldInfo = false; + warnObsolete("-no_compact_linkedit"); } else if ( strcmp(arg, "-no_eh_labels") == 0 ) { - fReaderOptions.fNoEHLabels = true; + fNoEHLabels = true; } else if ( strcmp(arg, "-warn_compact_unwind") == 0 ) { - fReaderOptions.fWarnCompactUnwind = true; + fWarnCompactUnwind = true; } else if ( strcmp(arg, "-allow_sub_type_mismatches") == 0 ) { fAllowCpuSubtypeMismatches = true; } else if ( strcmp(arg, "-no_zero_fill_sections") == 0 ) { - fReaderOptions.fOptimizeZeroFill = false; + fOptimizeZeroFill = false; + } + else if ( strcmp(arg, "-merge_zero_fill_sections") == 0 ) { + fMergeZeroFill = true; + } + else if ( strcmp(arg, "-objc_abi_version") == 0 ) { + const char* version = argv[++i]; + if ( version == NULL ) + throw "-objc_abi_version missing version number"; + if ( strcmp(version, "2") == 0 ) { + fObjCABIVersion1Override = false; + fObjCABIVersion2Override = true; + } + else if ( strcmp(version, "1") == 0 ) { + fObjCABIVersion1Override = true; + fObjCABIVersion2Override = false; + } + else + warning("ignoring unrecognized argument (%s) to -objc_abi_version", version); + } + else if ( strcmp(arg, "-warn_weak_exports") == 0 ) { + fWarnWeakExports = true; + } + else if ( strcmp(arg, "-objc_gc_compaction") == 0 ) { + fObjcGcCompaction = true; + } + else if ( strcmp(arg, "-objc_gc") == 0 ) { + fObjCGc = true; + if ( fObjCGcOnly ) { + warning("-objc_gc overriding -objc_gc_only"); + fObjCGcOnly = false; + } + } + else if ( strcmp(arg, "-objc_gc_only") == 0 ) { + fObjCGcOnly = true; + if ( fObjCGc ) { + warning("-objc_gc_only overriding -objc_gc"); + fObjCGc = false; + } + } + else if ( strcmp(arg, "-demangle") == 0 ) { + fDemangle = true; + } + else if ( strcmp(arg, "-version_load_command") == 0 ) { + fVersionLoadCommandForcedOn = true; + fVersionLoadCommandForcedOff = false; + } + else if ( strcmp(arg, "-no_version_load_command") == 0 ) { + fVersionLoadCommandForcedOff = true; + fVersionLoadCommandForcedOn = false; + } + else if ( strcmp(arg, "-function_starts") == 0 ) { + fFunctionStartsForcedOn = true; + fFunctionStartsForcedOff = false; + } + else if ( strcmp(arg, "-no_function_starts") == 0 ) { + fFunctionStartsForcedOff = true; + fFunctionStartsForcedOn = false; + } + else if ( strcmp(arg, "-no_data_in_code_info") == 0 ) { + fDataInCodeInfoLoadCommandForcedOff = true; + fDataInCodeInfoLoadCommandForcedOn = false; + } + else if ( strcmp(arg, "-data_in_code_info") == 0 ) { + fDataInCodeInfoLoadCommandForcedOn = true; + fDataInCodeInfoLoadCommandForcedOff = false; + } + else if ( strcmp(arg, "-object_path_lto") == 0 ) { + fTempLtoObjectPath = argv[++i]; + if ( fTempLtoObjectPath == NULL ) + throw "missing argument to -object_path_lto"; + } + else if ( strcmp(arg, "-no_objc_category_merging") == 0 ) { + fObjcCategoryMerging = false; + } + else if ( strcmp(arg, "-force_symbols_weak_list") == 0 ) { + snapshotFileArgIndex = 1; + loadExportFile(argv[++i], "-force_symbols_weak_list", fForceWeakSymbols); + } + else if ( strcmp(arg, "-force_symbols_not_weak_list") == 0 ) { + snapshotFileArgIndex = 1; + loadExportFile(argv[++i], "-force_symbols_not_weak_list", fForceNotWeakSymbols); + } + else if ( strcmp(arg, "-force_symbol_weak") == 0 ) { + const char* symbol = argv[++i]; + if ( symbol == NULL ) + throw "-force_symbol_weak missing "; + fForceWeakSymbols.insert(symbol); + } + else if ( strcmp(arg, "-force_symbol_not_weak") == 0 ) { + const char* symbol = argv[++i]; + if ( symbol == NULL ) + throw "-force_symbol_not_weak missing "; + fForceNotWeakSymbols.insert(symbol); + } + else if ( strcmp(arg, "-reexported_symbols_list") == 0 ) { + snapshotFileArgIndex = 1; + if ( fExportMode == kExportSome ) + throw "can't use -exported_symbols_list and -reexported_symbols_list"; + loadExportFile(argv[++i], "-reexported_symbols_list", fReExportSymbols); + } + else if ( strcmp(arg, "-dyld_env") == 0 ) { + const char* envarg = argv[++i]; + if ( envarg == NULL ) + throw "-dyld_env missing ENV=VALUE"; + if ( strchr(envarg, '=') == NULL ) + throw "-dyld_env missing ENV=VALUE"; + fDyldEnvironExtras.push_back(envarg); + } + else if ( strcmp(arg, "-page_align_data_atoms") == 0 ) { + fPageAlignDataAtoms = true; + } + else if (strcmp(arg, "-debug_snapshot") == 0) { + fLinkSnapshot.setSnapshotMode(Snapshot::SNAPSHOT_DEBUG); + fSnapshotRequested = true; + } + else if (strcmp(arg, "-snapshot_dir") == 0) { + const char* path = argv[++i]; + if ( path == NULL ) + throw "-snapshot_dir missing path"; + fLinkSnapshot.setSnapshotMode(Snapshot::SNAPSHOT_DEBUG); + fLinkSnapshot.setSnapshotPath(path); + fSnapshotRequested = true; + } + else if ( strcmp(arg, "-new_main") == 0 ) { + fEntryPointLoadCommandForceOn = true; + } + else if ( strcmp(arg, "-no_new_main") == 0 ) { + fEntryPointLoadCommandForceOff = true; + } + else if ( strcmp(arg, "-source_version") == 0 ) { + const char* vers = argv[++i]; + if ( vers == NULL ) + throw "-source_version missing "; + fSourceVersion = parseVersionNumber64(vers); + } + else if ( strcmp(arg, "-add_source_version") == 0 ) { + fSourceVersionLoadCommandForceOn = true; + } + else if ( strcmp(arg, "-no_source_version") == 0 ) { + fSourceVersionLoadCommandForceOff = true; + } + else if ( strcmp(arg, "-sdk_version") == 0 ) { + const char* vers = argv[++i]; + if ( vers == NULL ) + throw "-sdk_version missing "; + fSDKVersion = parseVersionNumber32(vers); + } + else if ( strcmp(arg, "-dependent_dr_info") == 0 ) { + fDependentDRInfoForcedOn = true; + } + else if ( strcmp(arg, "-no_dependent_dr_info") == 0 ) { + fDependentDRInfoForcedOff = true; + } + else if ( strcmp(arg, "-kexts_use_stubs") == 0 ) { + fKextsUseStubs = true; + } + else if ( strcmp(argv[i], "-dependency_info") == 0 ) { + ++i; + // previously handled by buildSearchPaths() + } + else if ( strcmp(arg, "-export_dynamic") == 0 ) { + fExportDynamic = true; + } + else if ( strcmp(arg, "-force_symbols_coalesce_list") == 0 ) { + snapshotFileArgIndex = 1; + loadExportFile(argv[++i], "-force_symbols_coalesce_list", fForceCoalesceSymbols); + } + else if ( strcmp(arg, "-add_linker_option") == 0 ) { + // ex: -add_linker_option '-framework Foundation' + const char* optString = argv[++i]; + if ( optString == NULL ) + throw "-add_linker_option missing