]>
Commit | Line | Data |
---|---|---|
d696c285 | 1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
6e880c60 | 2 | * |
afe874b1 | 3 | * Copyright (c) 2005-2011 Apple Inc. All rights reserved. |
c2646906 A |
4 | * |
5 | * @APPLE_LICENSE_HEADER_START@ | |
d696c285 | 6 | * |
c2646906 A |
7 | * This file contains Original Code and/or Modifications of Original Code |
8 | * as defined in and that are subject to the Apple Public Source License | |
9 | * Version 2.0 (the 'License'). You may not use this file except in | |
10 | * compliance with the License. Please obtain a copy of the License at | |
11 | * http://www.opensource.apple.com/apsl/ and read it before using this | |
12 | * file. | |
d696c285 | 13 | * |
c2646906 A |
14 | * The Original Code and all software distributed under the License are |
15 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
16 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
17 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. | |
19 | * Please see the License for the specific language governing rights and | |
20 | * limitations under the License. | |
d696c285 | 21 | * |
c2646906 A |
22 | * @APPLE_LICENSE_HEADER_END@ |
23 | */ | |
24 | ||
25 | ||
26 | #include <sys/types.h> | |
27 | #include <sys/stat.h> | |
a61fdf0a | 28 | #include <mach/vm_prot.h> |
ebf6f434 | 29 | #include <sys/sysctl.h> |
55e3d2f6 | 30 | #include <mach-o/dyld.h> |
c2646906 | 31 | #include <fcntl.h> |
ebf6f434 A |
32 | #include <errno.h> |
33 | #include <string.h> | |
34 | #include <spawn.h> | |
35 | #include <cxxabi.h> | |
36 | #include <Availability.h> | |
a645023d | 37 | |
6e880c60 | 38 | #include <vector> |
c2646906 | 39 | |
c2646906 | 40 | #include "Options.h" |
2f2f92e4 A |
41 | #include "Architectures.hpp" |
42 | #include "MachOFileAbstraction.hpp" | |
ebf6f434 | 43 | #include "Snapshot.h" |
2f2f92e4 | 44 | |
a645023d A |
45 | // upward dependency on lto::version() |
46 | namespace lto { | |
47 | extern const char* version(); | |
48 | } | |
2f2f92e4 | 49 | |
55e3d2f6 | 50 | // magic to place command line in crash reports |
a645023d | 51 | const int crashreporterBufferSize = 2000; |
a645023d | 52 | static char crashreporterBuffer[crashreporterBufferSize]; |
ebf6f434 A |
53 | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
54 | #include <CrashReporterClient.h> | |
55 | // hack until ld does not need to build on 10.6 anymore | |
56 | struct crashreporter_annotations_t gCRAnnotations | |
57 | __attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) | |
58 | = { CRASHREPORTER_ANNOTATIONS_VERSION, 0, 0, 0, 0, 0, 0 }; | |
59 | #else | |
60 | extern "C" char* __crashreporter_info__; | |
61 | __attribute__((used)) | |
62 | char* __crashreporter_info__ = crashreporterBuffer; | |
63 | #endif | |
64 | ||
2f2f92e4 A |
65 | |
66 | static bool sEmitWarnings = true; | |
b2fa67a8 | 67 | static bool sFatalWarnings = false; |
2f2f92e4 A |
68 | static const char* sWarningsSideFilePath = NULL; |
69 | static FILE* sWarningsSideFile = NULL; | |
b2fa67a8 | 70 | static int sWarningsCount = 0; |
2f2f92e4 A |
71 | |
72 | void warning(const char* format, ...) | |
73 | { | |
b2fa67a8 | 74 | ++sWarningsCount; |
2f2f92e4 A |
75 | if ( sEmitWarnings ) { |
76 | va_list list; | |
77 | if ( sWarningsSideFilePath != NULL ) { | |
78 | if ( sWarningsSideFile == NULL ) | |
79 | sWarningsSideFile = fopen(sWarningsSideFilePath, "a"); | |
80 | } | |
81 | va_start(list, format); | |
55e3d2f6 | 82 | fprintf(stderr, "ld: warning: "); |
2f2f92e4 A |
83 | vfprintf(stderr, format, list); |
84 | fprintf(stderr, "\n"); | |
85 | if ( sWarningsSideFile != NULL ) { | |
55e3d2f6 | 86 | fprintf(sWarningsSideFile, "ld: warning: "); |
2f2f92e4 A |
87 | vfprintf(sWarningsSideFile, format, list); |
88 | fprintf(sWarningsSideFile, "\n"); | |
89 | fflush(sWarningsSideFile); | |
90 | } | |
91 | va_end(list); | |
92 | } | |
93 | } | |
c2646906 | 94 | |
d696c285 | 95 | void throwf(const char* format, ...) |
c2646906 A |
96 | { |
97 | va_list list; | |
98 | char* p; | |
99 | va_start(list, format); | |
100 | vasprintf(&p, format, list); | |
101 | va_end(list); | |
d696c285 | 102 | |
c2646906 A |
103 | const char* t = p; |
104 | throw t; | |
105 | } | |
106 | ||
ebf6f434 A |
107 | bool Options::FileInfo::checkFileExists(const char *p) |
108 | { | |
109 | struct stat statBuffer; | |
110 | if (p == NULL) p = path; | |
111 | if ( stat(p, &statBuffer) == 0 ) { | |
112 | if (p != path) path = strdup(p); | |
113 | fileLen = statBuffer.st_size; | |
114 | modTime = statBuffer.st_mtime; | |
115 | return true; | |
116 | } | |
117 | return false; | |
118 | } | |
119 | ||
c2646906 | 120 | Options::Options(int argc, const char* argv[]) |
a645023d | 121 | : fOutputFile("a.out"), fArchitecture(0), fSubArchitecture(0), fArchitectureName("unknown"), fOutputKind(kDynamicExecutable), |
afe874b1 | 122 | fHasPreferredSubType(false), fArchSupportsThumb2(false), fPrebind(false), fBindAtLoad(false), fKeepPrivateExterns(false), |
55e3d2f6 | 123 | fNeedsModuleTable(false), fIgnoreOtherArchFiles(false), fErrorOnOtherArchFiles(false), fForceSubtypeAll(false), |
a645023d A |
124 | fInterposeMode(kInterposeNone), fDeadStrip(false), fNameSpace(kTwoLevelNameSpace), |
125 | fDylibCompatVersion(0), fDylibCurrentVersion(0), fDylibInstallName(NULL), fFinalName(NULL), fEntryName("start"), | |
126 | fBaseAddress(0), fMaxAddress(0x7FFFFFFFFFFFFFFFLL), | |
a61fdf0a | 127 | fBaseWritableAddress(0), fSplitSegs(false), |
a645023d A |
128 | fExportMode(kExportDefault), fLibrarySearchMode(kSearchDylibAndArchiveInEachDir), |
129 | fUndefinedTreatment(kUndefinedError), fMessagesPrefixedWithArchitecture(true), | |
a61fdf0a A |
130 | fWeakReferenceMismatchTreatment(kWeakReferenceMismatchNonWeak), |
131 | fClientName(NULL), | |
132 | fUmbrellaName(NULL), fInitFunctionName(NULL), fDotOutputFile(NULL), fExecutablePath(NULL), | |
133 | fBundleLoader(NULL), fDtraceScriptName(NULL), fSegAddrTablePath(NULL), fMapPath(NULL), | |
ebf6f434 A |
134 | fDyldInstallPath("/usr/lib/dyld"), fTempLtoObjectPath(NULL), fOverridePathlibLTO(NULL), |
135 | fZeroPageSize(ULLONG_MAX), fStackSize(0), fStackAddr(0), fSourceVersion(0), fSDKVersion(0), fExecutableStack(false), | |
a645023d | 136 | fNonExecutableHeap(false), fDisableNonExecutableHeap(false), |
55e3d2f6 | 137 | fMinimumHeaderPad(32), fSegmentAlignment(4096), |
a61fdf0a A |
138 | fCommonsMode(kCommonsIgnoreDylibs), fUUIDMode(kUUIDContent), fLocalSymbolHandling(kLocalSymbolsAll), fWarnCommons(false), |
139 | fVerbose(false), fKeepRelocations(false), fWarnStabs(false), | |
69a49097 | 140 | fTraceDylibSearching(false), fPause(false), fStatistics(false), fPrintOptions(false), |
a61fdf0a | 141 | fSharedRegionEligible(false), fPrintOrderFileStatistics(false), |
a645023d | 142 | fReadOnlyx86Stubs(false), fPositionIndependentExecutable(false), fPIEOnCommandLine(false), |
d9246299 | 143 | fDisablePositionIndependentExecutable(false), fMaxMinimumHeaderPad(false), |
ebf6f434 | 144 | fDeadStripDylibs(false), fAllowTextRelocs(false), fWarnTextRelocs(false), fKextsUseStubs(false), |
4be885f6 A |
145 | fUsingLazyDylibLinking(false), fEncryptable(true), |
146 | fOrderData(true), fMarkDeadStrippableDylib(false), | |
a645023d A |
147 | fMakeCompressedDyldInfo(true), fMakeCompressedDyldInfoForceOff(false), fNoEHLabels(false), |
148 | fAllowCpuSubtypeMismatches(false), fUseSimplifiedDylibReExports(false), | |
149 | fObjCABIVersion2Override(false), fObjCABIVersion1Override(false), fCanUseUpwardDylib(false), | |
150 | fFullyLoadArchives(false), fLoadAllObjcObjectsFromArchives(false), fFlatNamespace(false), | |
151 | fLinkingMainExecutable(false), fForFinalLinkedImage(false), fForStatic(false), | |
152 | fForDyld(false), fMakeTentativeDefinitionsReal(false), fWhyLoad(false), fRootSafe(false), | |
153 | fSetuidSafe(false), fImplicitlyLinkPublicDylibs(true), fAddCompactUnwindEncoding(true), | |
154 | fWarnCompactUnwind(false), fRemoveDwarfUnwindIfCompactExists(false), | |
afe874b1 | 155 | fAutoOrderInitializers(true), fOptimizeZeroFill(true), fMergeZeroFill(false), fLogObjectFiles(false), |
a645023d A |
156 | fLogAllFiles(false), fTraceDylibs(false), fTraceIndirectDylibs(false), fTraceArchives(false), |
157 | fOutputSlidable(false), fWarnWeakExports(false), | |
158 | fObjcGcCompaction(false), fObjCGc(false), fObjCGcOnly(false), | |
159 | fDemangle(false), fTLVSupport(false), | |
afe874b1 A |
160 | fVersionLoadCommand(false), fVersionLoadCommandForcedOn(false), |
161 | fVersionLoadCommandForcedOff(false), fFunctionStartsLoadCommand(false), | |
162 | fFunctionStartsForcedOn(false), fFunctionStartsForcedOff(false), | |
b1f7435d | 163 | fDataInCodeInfoLoadCommand(false), fDataInCodeInfoLoadCommandForcedOn(false), fDataInCodeInfoLoadCommandForcedOff(false), |
afe874b1 | 164 | fCanReExportSymbols(false), fObjcCategoryMerging(true), fPageAlignDataAtoms(false), |
ebf6f434 A |
165 | fNeedsThreadLoadCommand(false), fEntryPointLoadCommand(false), |
166 | fEntryPointLoadCommandForceOn(false), fEntryPointLoadCommandForceOff(false), | |
167 | fSourceVersionLoadCommand(false), | |
168 | fSourceVersionLoadCommandForceOn(false), fSourceVersionLoadCommandForceOff(false), | |
169 | fDependentDRInfo(false), fDependentDRInfoForcedOn(false), fDependentDRInfoForcedOff(false), | |
a645023d | 170 | fDebugInfoStripping(kDebugInfoMinimal), fTraceOutputFile(NULL), |
afe874b1 | 171 | fMacVersionMin(ld::macVersionUnset), fIOSVersionMin(ld::iOSVersionUnset), |
ebf6f434 | 172 | fSaveTempFiles(false), fSnapshotRequested(false), fPipelineFifo(NULL) |
c2646906 | 173 | { |
a61fdf0a | 174 | this->checkForClassic(argc, argv); |
c2646906 A |
175 | this->parsePreCommandLineEnvironmentSettings(); |
176 | this->parse(argc, argv); | |
177 | this->parsePostCommandLineEnvironmentSettings(); | |
69a49097 | 178 | this->reconfigureDefaults(); |
c2646906 A |
179 | this->checkIllegalOptionCombinations(); |
180 | } | |
181 | ||
182 | Options::~Options() | |
183 | { | |
184 | } | |
185 | ||
b2fa67a8 A |
186 | bool Options::errorBecauseOfWarnings() const |
187 | { | |
188 | return (sFatalWarnings && (sWarningsCount > 0)); | |
189 | } | |
c2646906 | 190 | |
c2646906 | 191 | |
a645023d | 192 | const char* Options::installPath() const |
c2646906 | 193 | { |
d696c285 | 194 | if ( fDylibInstallName != NULL ) |
c2646906 | 195 | return fDylibInstallName; |
a61fdf0a A |
196 | else if ( fFinalName != NULL ) |
197 | return fFinalName; | |
c2646906 A |
198 | else |
199 | return fOutputFile; | |
200 | } | |
201 | ||
c2646906 | 202 | |
a645023d | 203 | bool Options::interposable(const char* name) const |
c2646906 | 204 | { |
2f2f92e4 A |
205 | switch ( fInterposeMode ) { |
206 | case kInterposeNone: | |
207 | return false; | |
208 | case kInterposeAllExternal: | |
209 | return true; | |
210 | case kInterposeSome: | |
211 | return fInterposeList.contains(name); | |
212 | } | |
213 | throw "internal error"; | |
c2646906 A |
214 | } |
215 | ||
c2646906 | 216 | |
a645023d | 217 | bool Options::printWhyLive(const char* symbolName) const |
69a49097 A |
218 | { |
219 | return ( fWhyLive.find(symbolName) != fWhyLive.end() ); | |
220 | } | |
221 | ||
d696c285 | 222 | |
d696c285 A |
223 | const char* Options::dotOutputFile() |
224 | { | |
225 | return fDotOutputFile; | |
226 | } | |
227 | ||
c2646906 | 228 | |
a645023d | 229 | bool Options::hasWildCardExportRestrictList() const |
2f2f92e4 | 230 | { |
a645023d A |
231 | // has -exported_symbols_list which contains some wildcards |
232 | return ((fExportMode == kExportSome) && fExportSymbols.hasWildCards()); | |
2f2f92e4 A |
233 | } |
234 | ||
a645023d | 235 | bool Options::hasWeakBitTweaks() const |
2f2f92e4 A |
236 | { |
237 | // has -exported_symbols_list which contains some wildcards | |
a645023d | 238 | return (!fForceWeakSymbols.empty() || !fForceNotWeakSymbols.empty()); |
2f2f92e4 A |
239 | } |
240 | ||
a645023d | 241 | bool Options::allGlobalsAreDeadStripRoots() const |
69a49097 A |
242 | { |
243 | // -exported_symbols_list means globals are not exported by default | |
a61fdf0a | 244 | if ( fExportMode == kExportSome ) |
69a49097 A |
245 | return false; |
246 | // | |
247 | switch ( fOutputKind ) { | |
248 | case Options::kDynamicExecutable: | |
249 | case Options::kStaticExecutable: | |
55e3d2f6 | 250 | case Options::kPreload: |
69a49097 A |
251 | // by default unused globals in a main executable are stripped |
252 | return false; | |
253 | case Options::kDynamicLibrary: | |
254 | case Options::kDynamicBundle: | |
255 | case Options::kObjectFile: | |
256 | case Options::kDyld: | |
55e3d2f6 | 257 | case Options::kKextBundle: |
69a49097 A |
258 | return true; |
259 | } | |
260 | return false; | |
261 | } | |
262 | ||
a645023d A |
263 | |
264 | bool Options::keepRelocations() | |
c2646906 | 265 | { |
a645023d | 266 | return fKeepRelocations; |
c2646906 A |
267 | } |
268 | ||
a645023d | 269 | bool Options::warnStabs() |
c2646906 | 270 | { |
a645023d | 271 | return fWarnStabs; |
c2646906 A |
272 | } |
273 | ||
a645023d | 274 | const char* Options::executablePath() |
c2646906 | 275 | { |
a645023d | 276 | return fExecutablePath; |
c2646906 A |
277 | } |
278 | ||
a645023d A |
279 | |
280 | uint32_t Options::initialSegProtection(const char* segName) const | |
c2646906 | 281 | { |
a645023d A |
282 | for(std::vector<Options::SegmentProtect>::const_iterator it = fCustomSegmentProtections.begin(); it != fCustomSegmentProtections.end(); ++it) { |
283 | if ( strcmp(it->name, segName) == 0 ) { | |
284 | return it->init; | |
285 | } | |
286 | } | |
287 | if ( strcmp(segName, "__PAGEZERO") == 0 ) { | |
288 | return 0; | |
289 | } | |
290 | else if ( strcmp(segName, "__TEXT") == 0 ) { | |
291 | return VM_PROT_READ | VM_PROT_EXECUTE; | |
292 | } | |
293 | else if ( strcmp(segName, "__LINKEDIT") == 0 ) { | |
294 | return VM_PROT_READ; | |
295 | } | |
296 | ||
297 | // all others default to read-write | |
298 | return VM_PROT_READ | VM_PROT_WRITE; | |
c2646906 A |
299 | } |
300 | ||
a645023d | 301 | uint32_t Options::maxSegProtection(const char* segName) const |
c2646906 | 302 | { |
a645023d | 303 | // iPhoneOS always uses same protection for max and initial |
b1f7435d A |
304 | // <rdar://problem/11663436> simulator apps need to use MacOSX max-prot |
305 | if ( (fIOSVersionMin != ld::iOSVersionUnset) && (fArchitecture != CPU_TYPE_I386) ) | |
a645023d A |
306 | return initialSegProtection(segName); |
307 | ||
308 | for(std::vector<Options::SegmentProtect>::const_iterator it = fCustomSegmentProtections.begin(); it != fCustomSegmentProtections.end(); ++it) { | |
309 | if ( strcmp(it->name, segName) == 0 ) { | |
310 | return it->max; | |
311 | } | |
312 | } | |
313 | if ( strcmp(segName, "__PAGEZERO") == 0 ) { | |
314 | return 0; | |
315 | } | |
316 | // all others default to all | |
317 | return VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; | |
318 | } | |
319 | ||
320 | uint64_t Options::segPageSize(const char* segName) const | |
321 | { | |
322 | for(std::vector<SegmentSize>::const_iterator it=fCustomSegmentSizes.begin(); it != fCustomSegmentSizes.end(); ++it) { | |
323 | if ( strcmp(it->name, segName) == 0 ) | |
324 | return it->size; | |
325 | } | |
326 | return fSegmentAlignment; | |
c2646906 A |
327 | } |
328 | ||
a645023d | 329 | uint64_t Options::customSegmentAddress(const char* segName) const |
d696c285 | 330 | { |
a645023d A |
331 | for(std::vector<SegmentStart>::const_iterator it=fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { |
332 | if ( strcmp(it->name, segName) == 0 ) | |
333 | return it->address; | |
334 | } | |
335 | // if custom stack in use, model as segment with custom address | |
336 | if ( (fStackSize != 0) && (strcmp("__UNIXSTACK", segName) == 0) ) | |
337 | return fStackAddr - fStackSize; | |
338 | return 0; | |
d696c285 A |
339 | } |
340 | ||
a645023d | 341 | bool Options::hasCustomSegmentAddress(const char* segName) const |
d696c285 | 342 | { |
a645023d A |
343 | for(std::vector<SegmentStart>::const_iterator it=fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { |
344 | if ( strcmp(it->name, segName) == 0 ) | |
345 | return true; | |
346 | } | |
347 | // if custom stack in use, model as segment with custom address | |
348 | if ( (fStackSize != 0) && (strcmp("__UNIXSTACK", segName) == 0) ) | |
349 | return true; | |
350 | return false; | |
d696c285 A |
351 | } |
352 | ||
a645023d | 353 | bool Options::hasCustomSectionAlignment(const char* segName, const char* sectName) const |
d696c285 | 354 | { |
a645023d A |
355 | for (std::vector<SectionAlignment>::const_iterator it = fSectionAlignments.begin(); it != fSectionAlignments.end(); ++it) { |
356 | if ( (strcmp(it->segmentName, segName) == 0) && (strcmp(it->sectionName, sectName) == 0) ) | |
357 | return true; | |
358 | } | |
359 | return false; | |
d696c285 A |
360 | } |
361 | ||
a645023d | 362 | uint8_t Options::customSectionAlignment(const char* segName, const char* sectName) const |
d696c285 | 363 | { |
a645023d A |
364 | for (std::vector<SectionAlignment>::const_iterator it = fSectionAlignments.begin(); it != fSectionAlignments.end(); ++it) { |
365 | if ( (strcmp(it->segmentName, segName) == 0) && (strcmp(it->sectionName, sectName) == 0) ) | |
366 | return it->alignment; | |
367 | } | |
368 | return 0; | |
d696c285 A |
369 | } |
370 | ||
a645023d | 371 | |
55e3d2f6 A |
372 | bool Options::hasExportedSymbolOrder() |
373 | { | |
374 | return (fExportSymbolsOrder.size() > 0); | |
375 | } | |
376 | ||
a645023d | 377 | bool Options::exportedSymbolOrder(const char* sym, unsigned int* order) const |
55e3d2f6 | 378 | { |
a645023d | 379 | NameToOrder::const_iterator pos = fExportSymbolsOrder.find(sym); |
55e3d2f6 A |
380 | if ( pos != fExportSymbolsOrder.end() ) { |
381 | *order = pos->second; | |
382 | return true; | |
383 | } | |
384 | else { | |
385 | *order = 0xFFFFFFFF; | |
386 | return false; | |
387 | } | |
388 | } | |
389 | ||
390 | void Options::loadSymbolOrderFile(const char* fileOfExports, NameToOrder& orderMapping) | |
391 | { | |
392 | // read in whole file | |
393 | int fd = ::open(fileOfExports, O_RDONLY, 0); | |
394 | if ( fd == -1 ) | |
395 | throwf("can't open -exported_symbols_order file: %s", fileOfExports); | |
396 | struct stat stat_buf; | |
397 | ::fstat(fd, &stat_buf); | |
398 | char* p = (char*)malloc(stat_buf.st_size); | |
399 | if ( p == NULL ) | |
400 | throwf("can't process -exported_symbols_order file: %s", fileOfExports); | |
401 | ||
402 | if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) | |
403 | throwf("can't read -exported_symbols_order file: %s", fileOfExports); | |
404 | ||
405 | ::close(fd); | |
406 | ||
407 | // parse into symbols and add to hash_set | |
408 | unsigned int count = 0; | |
409 | char * const end = &p[stat_buf.st_size]; | |
410 | enum { lineStart, inSymbol, inComment } state = lineStart; | |
411 | char* symbolStart = NULL; | |
412 | for (char* s = p; s < end; ++s ) { | |
413 | switch ( state ) { | |
414 | case lineStart: | |
415 | if ( *s =='#' ) { | |
416 | state = inComment; | |
417 | } | |
418 | else if ( !isspace(*s) ) { | |
419 | state = inSymbol; | |
420 | symbolStart = s; | |
421 | } | |
422 | break; | |
423 | case inSymbol: | |
424 | if ( (*s == '\n') || (*s == '\r') ) { | |
425 | *s = '\0'; | |
426 | // removing any trailing spaces | |
427 | char* last = s-1; | |
428 | while ( isspace(*last) ) { | |
429 | *last = '\0'; | |
430 | --last; | |
431 | } | |
432 | orderMapping[symbolStart] = ++count; | |
433 | symbolStart = NULL; | |
434 | state = lineStart; | |
435 | } | |
436 | break; | |
437 | case inComment: | |
438 | if ( (*s == '\n') || (*s == '\r') ) | |
439 | state = lineStart; | |
440 | break; | |
441 | } | |
442 | } | |
443 | if ( state == inSymbol ) { | |
444 | warning("missing line-end at end of file \"%s\"", fileOfExports); | |
445 | int len = end-symbolStart+1; | |
446 | char* temp = new char[len]; | |
447 | strlcpy(temp, symbolStart, len); | |
448 | ||
449 | // remove any trailing spaces | |
450 | char* last = &temp[len-2]; | |
451 | while ( isspace(*last) ) { | |
452 | *last = '\0'; | |
453 | --last; | |
454 | } | |
455 | orderMapping[temp] = ++count; | |
456 | } | |
457 | ||
458 | // Note: we do not free() the malloc buffer, because the strings are used by the export-set hash table | |
459 | } | |
460 | ||
a645023d A |
461 | bool Options::forceWeak(const char* symbolName) const |
462 | { | |
463 | return fForceWeakSymbols.contains(symbolName); | |
464 | } | |
465 | ||
466 | bool Options::forceNotWeak(const char* symbolName) const | |
467 | { | |
468 | return fForceNotWeakSymbols.contains(symbolName); | |
469 | } | |
470 | ||
471 | bool Options::forceWeakNonWildCard(const char* symbolName) const | |
472 | { | |
473 | return fForceWeakSymbols.containsNonWildcard(symbolName); | |
474 | } | |
475 | ||
476 | bool Options::forceNotWeakNonWildcard(const char* symbolName) const | |
477 | { | |
478 | return fForceNotWeakSymbols.containsNonWildcard(symbolName); | |
479 | } | |
480 | ||
55e3d2f6 | 481 | |
a645023d | 482 | bool Options::shouldExport(const char* symbolName) const |
c2646906 A |
483 | { |
484 | switch (fExportMode) { | |
485 | case kExportSome: | |
a61fdf0a | 486 | return fExportSymbols.contains(symbolName); |
c2646906 | 487 | case kDontExportSome: |
a61fdf0a | 488 | return ! fDontExportSymbols.contains(symbolName); |
c2646906 A |
489 | case kExportDefault: |
490 | return true; | |
491 | } | |
492 | throw "internal error"; | |
493 | } | |
494 | ||
a645023d A |
495 | bool Options::shouldReExport(const char* symbolName) const |
496 | { | |
497 | return fReExportSymbols.contains(symbolName); | |
498 | } | |
499 | ||
500 | bool Options::keepLocalSymbol(const char* symbolName) const | |
a61fdf0a A |
501 | { |
502 | switch (fLocalSymbolHandling) { | |
503 | case kLocalSymbolsAll: | |
504 | return true; | |
505 | case kLocalSymbolsNone: | |
506 | return false; | |
507 | case kLocalSymbolsSelectiveInclude: | |
508 | return fLocalSymbolsIncluded.contains(symbolName); | |
509 | case kLocalSymbolsSelectiveExclude: | |
510 | return ! fLocalSymbolsExcluded.contains(symbolName); | |
511 | } | |
512 | throw "internal error"; | |
513 | } | |
514 | ||
a645023d | 515 | void Options::setArchitecture(cpu_type_t type, cpu_subtype_t subtype) |
c2646906 | 516 | { |
ebf6f434 A |
517 | for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { |
518 | if ( (type == t->cpuType) && (subtype == t->cpuSubType) ) { | |
519 | fArchitecture = type; | |
520 | fSubArchitecture = subtype; | |
521 | fArchitectureName = t->archName; | |
522 | fHasPreferredSubType = t->isSubType; | |
523 | fArchSupportsThumb2 = t->supportsThumb2; | |
524 | switch ( type ) { | |
525 | case CPU_TYPE_I386: | |
526 | case CPU_TYPE_X86_64: | |
527 | if ( (fMacVersionMin == ld::macVersionUnset) && (fIOSVersionMin == ld::iOSVersionUnset) && (fOutputKind != Options::kObjectFile) ) { | |
528 | #ifdef DEFAULT_MACOSX_MIN_VERSION | |
529 | warning("-macosx_version_min not specified, assuming " DEFAULT_MACOSX_MIN_VERSION); | |
530 | setMacOSXVersionMin(DEFAULT_MACOSX_MIN_VERSION); | |
531 | #else | |
532 | warning("-macosx_version_min not specified, assuming 10.6"); | |
533 | fMacVersionMin = ld::mac10_6; | |
534 | #endif | |
535 | } | |
536 | if ( !fMakeCompressedDyldInfo && minOS(ld::mac10_6, ld::iOS_3_1) && !fMakeCompressedDyldInfoForceOff ) | |
537 | fMakeCompressedDyldInfo = true; | |
538 | break; | |
539 | case CPU_TYPE_ARM: | |
540 | if ( (fMacVersionMin == ld::macVersionUnset) && (fIOSVersionMin == ld::iOSVersionUnset) && (fOutputKind != Options::kObjectFile) ) { | |
541 | #if defined(DEFAULT_IPHONEOS_MIN_VERSION) | |
542 | warning("-ios_version_min not specified, assuming " DEFAULT_IPHONEOS_MIN_VERSION); | |
543 | setIOSVersionMin(DEFAULT_IPHONEOS_MIN_VERSION); | |
544 | #elif defined(DEFAULT_MACOSX_MIN_VERSION) | |
545 | warning("-macosx_version_min not specified, assuming " DEFAULT_MACOSX_MIN_VERSION); | |
546 | setMacOSXVersionMin(DEFAULT_MACOSX_MIN_VERSION); | |
547 | #else | |
548 | warning("-macosx_version_min not specified, assuming 10.6"); | |
549 | fMacVersionMin = ld::mac10_6; | |
550 | #endif | |
551 | } | |
552 | if ( !fMakeCompressedDyldInfo && minOS(ld::mac10_6, ld::iOS_3_1) && !fMakeCompressedDyldInfoForceOff ) | |
553 | fMakeCompressedDyldInfo = true; | |
554 | break; | |
afe874b1 | 555 | } |
ebf6f434 A |
556 | fLinkSnapshot.recordArch(fArchitectureName); |
557 | return; | |
a645023d | 558 | } |
a645023d | 559 | } |
ebf6f434 | 560 | fArchitectureName = "unknown architecture"; |
a645023d A |
561 | } |
562 | ||
563 | void Options::parseArch(const char* arch) | |
564 | { | |
565 | if ( arch == NULL ) | |
c2646906 | 566 | throw "-arch must be followed by an architecture string"; |
ebf6f434 A |
567 | for (const ArchInfo* t=archInfoArray; t->archName != NULL; ++t) { |
568 | if ( strcmp(t->archName,arch) == 0 ) { | |
569 | fArchitectureName = arch; | |
570 | fArchitecture = t->cpuType; | |
571 | fSubArchitecture = t->cpuSubType; | |
572 | fHasPreferredSubType = t->isSubType; | |
573 | fArchSupportsThumb2 = t->supportsThumb2; | |
574 | return; | |
afe874b1 | 575 | } |
afe874b1 | 576 | } |
ebf6f434 | 577 | throwf("unknown/unsupported architecture name for: -arch %s", arch); |
c2646906 A |
578 | } |
579 | ||
a645023d | 580 | bool Options::checkForFile(const char* format, const char* dir, const char* rootName, FileInfo& result) const |
c2646906 | 581 | { |
d696c285 | 582 | char possiblePath[strlen(dir)+strlen(rootName)+strlen(format)+8]; |
c2646906 | 583 | sprintf(possiblePath, format, dir, rootName); |
ebf6f434 | 584 | bool found = result.checkFileExists(possiblePath); |
d696c285 A |
585 | if ( fTraceDylibSearching ) |
586 | printf("[Logging for XBS]%sfound library: '%s'\n", (found ? " " : " not "), possiblePath); | |
ebf6f434 | 587 | return found; |
d696c285 | 588 | } |
c2646906 A |
589 | |
590 | ||
2f2f92e4 | 591 | Options::FileInfo Options::findLibrary(const char* rootName, bool dylibsOnly) |
c2646906 A |
592 | { |
593 | FileInfo result; | |
594 | const int rootNameLen = strlen(rootName); | |
595 | // if rootName ends in .o there is no .a vs .dylib choice | |
596 | if ( (rootNameLen > 3) && (strcmp(&rootName[rootNameLen-2], ".o") == 0) ) { | |
d696c285 A |
597 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); |
598 | it != fLibrarySearchPaths.end(); | |
599 | it++) { | |
c2646906 A |
600 | const char* dir = *it; |
601 | if ( checkForFile("%s/%s", dir, rootName, result) ) | |
602 | return result; | |
603 | } | |
604 | } | |
605 | else { | |
606 | bool lookForDylibs = ( fOutputKind != Options::kDyld); | |
607 | switch ( fLibrarySearchMode ) { | |
d696c285 | 608 | case kSearchAllDirsForDylibsThenAllDirsForArchives: |
c2646906 A |
609 | // first look in all directories for just for dylibs |
610 | if ( lookForDylibs ) { | |
d696c285 A |
611 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); |
612 | it != fLibrarySearchPaths.end(); | |
613 | it++) { | |
c2646906 A |
614 | const char* dir = *it; |
615 | if ( checkForFile("%s/lib%s.dylib", dir, rootName, result) ) | |
616 | return result; | |
617 | } | |
a61fdf0a A |
618 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); |
619 | it != fLibrarySearchPaths.end(); | |
620 | it++) { | |
621 | const char* dir = *it; | |
622 | if ( checkForFile("%s/lib%s.so", dir, rootName, result) ) | |
623 | return result; | |
624 | } | |
c2646906 A |
625 | } |
626 | // next look in all directories for just for archives | |
2f2f92e4 A |
627 | if ( !dylibsOnly ) { |
628 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); | |
629 | it != fLibrarySearchPaths.end(); | |
630 | it++) { | |
631 | const char* dir = *it; | |
632 | if ( checkForFile("%s/lib%s.a", dir, rootName, result) ) | |
633 | return result; | |
634 | } | |
c2646906 A |
635 | } |
636 | break; | |
637 | ||
638 | case kSearchDylibAndArchiveInEachDir: | |
639 | // look in each directory for just for a dylib then for an archive | |
d696c285 A |
640 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); |
641 | it != fLibrarySearchPaths.end(); | |
642 | it++) { | |
c2646906 A |
643 | const char* dir = *it; |
644 | if ( lookForDylibs && checkForFile("%s/lib%s.dylib", dir, rootName, result) ) | |
645 | return result; | |
a61fdf0a A |
646 | if ( lookForDylibs && checkForFile("%s/lib%s.so", dir, rootName, result) ) |
647 | return result; | |
2f2f92e4 | 648 | if ( !dylibsOnly && checkForFile("%s/lib%s.a", dir, rootName, result) ) |
c2646906 A |
649 | return result; |
650 | } | |
651 | break; | |
652 | } | |
653 | } | |
654 | throwf("library not found for -l%s", rootName); | |
655 | } | |
656 | ||
74cfe461 A |
657 | Options::FileInfo Options::findFramework(const char* frameworkName) |
658 | { | |
659 | if ( frameworkName == NULL ) | |
a61fdf0a | 660 | throw "-framework missing next argument"; |
74cfe461 A |
661 | char temp[strlen(frameworkName)+1]; |
662 | strcpy(temp, frameworkName); | |
663 | const char* name = temp; | |
664 | const char* suffix = NULL; | |
665 | char* comma = strchr(temp, ','); | |
666 | if ( comma != NULL ) { | |
667 | *comma = '\0'; | |
668 | suffix = &comma[1]; | |
669 | } | |
670 | return findFramework(name, suffix); | |
671 | } | |
c2646906 | 672 | |
74cfe461 | 673 | Options::FileInfo Options::findFramework(const char* rootName, const char* suffix) |
c2646906 | 674 | { |
d696c285 A |
675 | for (std::vector<const char*>::iterator it = fFrameworkSearchPaths.begin(); |
676 | it != fFrameworkSearchPaths.end(); | |
677 | it++) { | |
678 | // ??? Shouldn't we be using String here and just initializing it? | |
679 | // ??? Use str.c_str () to pull out the string for the stat call. | |
c2646906 | 680 | const char* dir = *it; |
74cfe461 | 681 | char possiblePath[PATH_MAX]; |
c2646906 A |
682 | strcpy(possiblePath, dir); |
683 | strcat(possiblePath, "/"); | |
684 | strcat(possiblePath, rootName); | |
685 | strcat(possiblePath, ".framework/"); | |
686 | strcat(possiblePath, rootName); | |
74cfe461 A |
687 | if ( suffix != NULL ) { |
688 | char realPath[PATH_MAX]; | |
689 | // no symlink in framework to suffix variants, so follow main symlink | |
690 | if ( realpath(possiblePath, realPath) != NULL ) { | |
691 | strcpy(possiblePath, realPath); | |
692 | strcat(possiblePath, suffix); | |
693 | } | |
694 | } | |
ebf6f434 A |
695 | FileInfo result; |
696 | bool found = result.checkFileExists(possiblePath); | |
d696c285 A |
697 | if ( fTraceDylibSearching ) |
698 | printf("[Logging for XBS]%sfound framework: '%s'\n", | |
699 | (found ? " " : " not "), possiblePath); | |
700 | if ( found ) { | |
c2646906 A |
701 | return result; |
702 | } | |
703 | } | |
74cfe461 | 704 | // try without suffix |
a61fdf0a | 705 | if ( suffix != NULL ) |
74cfe461 A |
706 | return findFramework(rootName, NULL); |
707 | else | |
708 | throwf("framework not found %s", rootName); | |
c2646906 A |
709 | } |
710 | ||
a645023d | 711 | Options::FileInfo Options::findFile(const char* path) const |
c2646906 | 712 | { |
6e880c60 | 713 | FileInfo result; |
d696c285 | 714 | |
6e880c60 A |
715 | // if absolute path and not a .o file, the use SDK prefix |
716 | if ( (path[0] == '/') && (strcmp(&path[strlen(path)-2], ".o") != 0) ) { | |
717 | const int pathLen = strlen(path); | |
a645023d | 718 | for (std::vector<const char*>::const_iterator it = fSDKPaths.begin(); it != fSDKPaths.end(); it++) { |
d696c285 | 719 | // ??? Shouldn't we be using String here? |
6e880c60 A |
720 | const char* sdkPathDir = *it; |
721 | const int sdkPathDirLen = strlen(sdkPathDir); | |
722 | char possiblePath[sdkPathDirLen+pathLen+4]; | |
723 | strcpy(possiblePath, sdkPathDir); | |
724 | if ( possiblePath[sdkPathDirLen-1] == '/' ) | |
725 | possiblePath[sdkPathDirLen-1] = '\0'; | |
726 | strcat(possiblePath, path); | |
ebf6f434 | 727 | if ( result.checkFileExists(possiblePath) ) { |
6e880c60 A |
728 | return result; |
729 | } | |
730 | } | |
731 | } | |
732 | // try raw path | |
ebf6f434 | 733 | if ( result.checkFileExists(path) ) { |
c2646906 A |
734 | return result; |
735 | } | |
d696c285 A |
736 | |
737 | // try @executable_path substitution | |
738 | if ( (strncmp(path, "@executable_path/", 17) == 0) && (fExecutablePath != NULL) ) { | |
739 | char newPath[strlen(fExecutablePath) + strlen(path)]; | |
740 | strcpy(newPath, fExecutablePath); | |
741 | char* addPoint = strrchr(newPath,'/'); | |
742 | if ( addPoint != NULL ) | |
743 | strcpy(&addPoint[1], &path[17]); | |
744 | else | |
745 | strcpy(newPath, &path[17]); | |
ebf6f434 | 746 | if ( result.checkFileExists(newPath) ) { |
d696c285 A |
747 | return result; |
748 | } | |
749 | } | |
750 | ||
6e880c60 A |
751 | // not found |
752 | throwf("file not found: %s", path); | |
c2646906 A |
753 | } |
754 | ||
a645023d | 755 | Options::FileInfo Options::findFileUsingPaths(const char* path) const |
a61fdf0a A |
756 | { |
757 | FileInfo result; | |
758 | ||
759 | const char* lastSlash = strrchr(path, '/'); | |
760 | const char* leafName = (lastSlash == NULL) ? path : &lastSlash[1]; | |
761 | ||
762 | // Is this in a framework? | |
763 | // /path/Foo.framework/Foo ==> true (Foo) | |
764 | // /path/Foo.framework/Frameworks/Bar.framework/Bar ==> true (Bar) | |
765 | // /path/Foo.framework/Resources/Bar ==> false | |
766 | bool isFramework = false; | |
767 | if ( lastSlash != NULL ) { | |
768 | char frameworkDir[strlen(leafName) + 20]; | |
769 | strcpy(frameworkDir, "/"); | |
770 | strcat(frameworkDir, leafName); | |
771 | strcat(frameworkDir, ".framework/"); | |
772 | if ( strstr(path, frameworkDir) != NULL ) | |
773 | isFramework = true; | |
774 | } | |
775 | ||
776 | // These are abbreviated versions of the routines findFramework and findLibrary above | |
777 | // because we already know the final name of the file that we're looking for and so | |
778 | // don't need to try variations, just paths. We do need to add the additional bits | |
779 | // onto the framework path though. | |
780 | if ( isFramework ) { | |
a645023d | 781 | for (std::vector<const char*>::const_iterator it = fFrameworkSearchPaths.begin(); |
a61fdf0a A |
782 | it != fFrameworkSearchPaths.end(); |
783 | it++) { | |
784 | const char* dir = *it; | |
785 | char possiblePath[PATH_MAX]; | |
786 | strcpy(possiblePath, dir); | |
787 | strcat(possiblePath, "/"); | |
788 | strcat(possiblePath, leafName); | |
789 | strcat(possiblePath, ".framework"); | |
790 | ||
791 | //fprintf(stderr,"Finding Framework: %s/%s, leafName=%s\n", possiblePath, leafName, leafName); | |
792 | if ( checkForFile("%s/%s", possiblePath, leafName, result) ) | |
793 | return result; | |
794 | } | |
795 | } | |
796 | else { | |
2f2f92e4 A |
797 | // if this is a .dylib inside a framework, do not search -L paths |
798 | // <rdar://problem/5427952> ld64's re-export cycle detection logic prevents use of X11 libGL on Leopard | |
799 | int leafLen = strlen(leafName); | |
800 | bool embeddedDylib = ( (leafLen > 6) | |
801 | && (strcmp(&leafName[leafLen-6], ".dylib") == 0) | |
802 | && (strstr(path, ".framework/") != NULL) ); | |
803 | if ( !embeddedDylib ) { | |
a645023d | 804 | for (std::vector<const char*>::const_iterator it = fLibrarySearchPaths.begin(); |
2f2f92e4 A |
805 | it != fLibrarySearchPaths.end(); |
806 | it++) { | |
807 | const char* dir = *it; | |
808 | //fprintf(stderr,"Finding Library: %s/%s\n", dir, leafName); | |
809 | if ( checkForFile("%s/%s", dir, leafName, result) ) | |
810 | return result; | |
811 | } | |
a61fdf0a A |
812 | } |
813 | } | |
814 | ||
815 | // If we didn't find it fall back to findFile. | |
816 | return findFile(path); | |
817 | } | |
818 | ||
819 | ||
a645023d | 820 | void Options::parseSegAddrTable(const char* segAddrPath, const char* installPth) |
a61fdf0a A |
821 | { |
822 | FILE* file = fopen(segAddrPath, "r"); | |
823 | if ( file == NULL ) { | |
2f2f92e4 | 824 | warning("-seg_addr_table file cannot be read: %s", segAddrPath); |
a61fdf0a A |
825 | return; |
826 | } | |
827 | ||
828 | char path[PATH_MAX]; | |
829 | uint64_t firstColumAddress = 0; | |
830 | uint64_t secondColumAddress = 0; | |
831 | bool hasSecondColumn = false; | |
832 | while ( fgets(path, PATH_MAX, file) != NULL ) { | |
833 | path[PATH_MAX-1] = '\0'; | |
834 | char* eol = strchr(path, '\n'); | |
835 | if ( eol != NULL ) | |
836 | *eol = '\0'; | |
837 | // ignore lines not starting with 0x number | |
838 | if ( (path[0] == '0') && (path[1] == 'x') ) { | |
839 | char* p; | |
840 | firstColumAddress = strtoull(path, &p, 16); | |
841 | while ( isspace(*p) ) | |
842 | ++p; | |
843 | // see if second column is a number | |
844 | if ( (p[0] == '0') && (p[1] == 'x') ) { | |
845 | secondColumAddress = strtoull(p, &p, 16); | |
846 | hasSecondColumn = true; | |
847 | while ( isspace(*p) ) | |
848 | ++p; | |
849 | } | |
850 | while ( isspace(*p) ) | |
851 | ++p; | |
852 | if ( p[0] == '/' ) { | |
853 | // remove any trailing whitespace | |
854 | for(char* end = eol-1; (end > p) && isspace(*end); --end) | |
855 | *end = '\0'; | |
856 | // see if this line is for the dylib being linked | |
a645023d | 857 | if ( strcmp(p, installPth) == 0 ) { |
a61fdf0a A |
858 | fBaseAddress = firstColumAddress; |
859 | if ( hasSecondColumn ) { | |
860 | fBaseWritableAddress = secondColumAddress; | |
861 | fSplitSegs = true; | |
862 | } | |
863 | break; // out of while loop | |
864 | } | |
865 | } | |
866 | } | |
867 | } | |
868 | ||
869 | fclose(file); | |
870 | } | |
6e880c60 | 871 | |
ebf6f434 | 872 | void Options::loadFileList(const char* fileOfPaths, ld::File::Ordinal baseOrdinal) |
c2646906 | 873 | { |
69a49097 A |
874 | FILE* file; |
875 | const char* comma = strrchr(fileOfPaths, ','); | |
876 | const char* prefix = NULL; | |
877 | if ( comma != NULL ) { | |
55e3d2f6 A |
878 | // <rdar://problem/5907981> -filelist fails with comma in path |
879 | file = fopen(fileOfPaths, "r"); | |
880 | if ( file == NULL ) { | |
881 | prefix = comma+1; | |
882 | int realFileOfPathsLen = comma-fileOfPaths; | |
883 | char realFileOfPaths[realFileOfPathsLen+1]; | |
884 | strncpy(realFileOfPaths,fileOfPaths, realFileOfPathsLen); | |
885 | realFileOfPaths[realFileOfPathsLen] = '\0'; | |
886 | file = fopen(realFileOfPaths, "r"); | |
887 | if ( file == NULL ) | |
ebf6f434 | 888 | throwf("-filelist file '%s' could not be opened, errno=%d (%s)\n", realFileOfPaths, errno, strerror(errno)); |
55e3d2f6 | 889 | } |
69a49097 A |
890 | } |
891 | else { | |
892 | file = fopen(fileOfPaths, "r"); | |
893 | if ( file == NULL ) | |
ebf6f434 | 894 | throwf("-filelist file '%s' could not be opened, errno=%d (%s)\n", fileOfPaths, errno, strerror(errno)); |
69a49097 | 895 | } |
d696c285 | 896 | |
69a49097 | 897 | char path[PATH_MAX]; |
ebf6f434 | 898 | ld::File::Ordinal previousOrdinal = baseOrdinal; |
a61fdf0a | 899 | while ( fgets(path, PATH_MAX, file) != NULL ) { |
69a49097 | 900 | path[PATH_MAX-1] = '\0'; |
c2646906 A |
901 | char* eol = strchr(path, '\n'); |
902 | if ( eol != NULL ) | |
903 | *eol = '\0'; | |
69a49097 A |
904 | if ( prefix != NULL ) { |
905 | char builtPath[strlen(prefix)+strlen(path)+2]; | |
906 | strcpy(builtPath, prefix); | |
907 | strcat(builtPath, "/"); | |
908 | strcat(builtPath, path); | |
ebf6f434 A |
909 | if (fPipelineFifo != NULL) { |
910 | FileInfo info = FileInfo(builtPath); | |
911 | info.ordinal = previousOrdinal.nextFileListOrdinal(); | |
912 | previousOrdinal = info.ordinal; | |
913 | info.fromFileList = true; | |
914 | fInputFiles.push_back(info); | |
915 | } else { | |
916 | FileInfo info = findFile(builtPath); | |
917 | info.ordinal = previousOrdinal.nextFileListOrdinal(); | |
918 | previousOrdinal = info.ordinal; | |
919 | info.fromFileList = true; | |
920 | fInputFiles.push_back(info); | |
921 | } | |
69a49097 A |
922 | } |
923 | else { | |
ebf6f434 A |
924 | if (fPipelineFifo != NULL) { |
925 | FileInfo info = FileInfo(path); | |
926 | info.ordinal = previousOrdinal.nextFileListOrdinal(); | |
927 | previousOrdinal = info.ordinal; | |
928 | info.fromFileList = true; | |
929 | fInputFiles.push_back(info); | |
930 | } else { | |
931 | FileInfo info = findFile(path); | |
932 | info.ordinal = previousOrdinal.nextFileListOrdinal(); | |
933 | previousOrdinal = info.ordinal; | |
934 | info.fromFileList = true; | |
935 | fInputFiles.push_back(info); | |
936 | } | |
69a49097 | 937 | } |
c2646906 A |
938 | } |
939 | fclose(file); | |
940 | } | |
941 | ||
a645023d A |
942 | |
943 | void Options::SetWithWildcards::remove(const NameSet& toBeRemoved) | |
944 | { | |
945 | for(NameSet::const_iterator it=toBeRemoved.begin(); it != toBeRemoved.end(); ++it) { | |
946 | const char* symbolName = *it; | |
947 | NameSet::iterator pos = fRegular.find(symbolName); | |
948 | if ( pos != fRegular.end() ) | |
949 | fRegular.erase(pos); | |
950 | } | |
951 | } | |
952 | ||
953 | bool Options::SetWithWildcards::hasWildCards(const char* symbol) | |
a61fdf0a A |
954 | { |
955 | // an exported symbol name containing *, ?, or [ requires wildcard matching | |
956 | return ( strpbrk(symbol, "*?[") != NULL ); | |
957 | } | |
958 | ||
959 | void Options::SetWithWildcards::insert(const char* symbol) | |
960 | { | |
961 | if ( hasWildCards(symbol) ) | |
962 | fWildCard.push_back(symbol); | |
963 | else | |
964 | fRegular.insert(symbol); | |
965 | } | |
966 | ||
a645023d | 967 | bool Options::SetWithWildcards::contains(const char* symbol) const |
a61fdf0a A |
968 | { |
969 | // first look at hash table on non-wildcard symbols | |
970 | if ( fRegular.find(symbol) != fRegular.end() ) | |
971 | return true; | |
972 | // next walk list of wild card symbols looking for a match | |
a645023d | 973 | for(std::vector<const char*>::const_iterator it = fWildCard.begin(); it != fWildCard.end(); ++it) { |
a61fdf0a A |
974 | if ( wildCardMatch(*it, symbol) ) |
975 | return true; | |
976 | } | |
977 | return false; | |
978 | } | |
979 | ||
a645023d A |
980 | bool Options::SetWithWildcards::containsNonWildcard(const char* symbol) const |
981 | { | |
982 | // look at hash table on non-wildcard symbols | |
983 | return ( fRegular.find(symbol) != fRegular.end() ); | |
984 | } | |
985 | ||
986 | ||
a61fdf0a | 987 | |
a645023d | 988 | bool Options::SetWithWildcards::inCharRange(const char*& p, unsigned char c) const |
a61fdf0a A |
989 | { |
990 | ++p; // find end | |
991 | const char* b = p; | |
992 | while ( *p != '\0' ) { | |
993 | if ( *p == ']') { | |
994 | const char* e = p; | |
995 | // found beginining [ and ending ] | |
996 | unsigned char last = '\0'; | |
997 | for ( const char* s = b; s < e; ++s ) { | |
998 | if ( *s == '-' ) { | |
999 | unsigned char next = *(++s); | |
1000 | if ( (last <= c) && (c <= next) ) | |
1001 | return true; | |
1002 | ++s; | |
1003 | } | |
1004 | else { | |
1005 | if ( *s == c ) | |
1006 | return true; | |
1007 | last = *s; | |
1008 | } | |
1009 | } | |
1010 | return false; | |
1011 | } | |
1012 | ++p; | |
1013 | } | |
1014 | return false; | |
1015 | } | |
1016 | ||
a645023d | 1017 | bool Options::SetWithWildcards::wildCardMatch(const char* pattern, const char* symbol) const |
a61fdf0a A |
1018 | { |
1019 | const char* s = symbol; | |
1020 | for (const char* p = pattern; *p != '\0'; ++p) { | |
1021 | switch ( *p ) { | |
1022 | case '*': | |
1023 | if ( p[1] == '\0' ) | |
1024 | return true; | |
1025 | for (const char* t = s; *t != '\0'; ++t) { | |
1026 | if ( wildCardMatch(&p[1], t) ) | |
1027 | return true; | |
1028 | } | |
1029 | return false; | |
1030 | case '?': | |
1031 | if ( *s == '\0' ) | |
1032 | return false; | |
1033 | ++s; | |
1034 | break; | |
1035 | case '[': | |
1036 | if ( ! inCharRange(p, *s) ) | |
1037 | return false; | |
1038 | ++s; | |
1039 | break; | |
1040 | default: | |
1041 | if ( *s != *p ) | |
1042 | return false; | |
1043 | ++s; | |
1044 | } | |
1045 | } | |
1046 | return (*s == '\0'); | |
1047 | } | |
1048 | ||
c2646906 | 1049 | |
a61fdf0a | 1050 | void Options::loadExportFile(const char* fileOfExports, const char* option, SetWithWildcards& set) |
c2646906 | 1051 | { |
a645023d A |
1052 | if ( fileOfExports == NULL ) |
1053 | throwf("missing file after %s", option); | |
c2646906 A |
1054 | // read in whole file |
1055 | int fd = ::open(fileOfExports, O_RDONLY, 0); | |
1056 | if ( fd == -1 ) | |
1057 | throwf("can't open %s file: %s", option, fileOfExports); | |
1058 | struct stat stat_buf; | |
1059 | ::fstat(fd, &stat_buf); | |
1060 | char* p = (char*)malloc(stat_buf.st_size); | |
1061 | if ( p == NULL ) | |
1062 | throwf("can't process %s file: %s", option, fileOfExports); | |
d696c285 | 1063 | |
c2646906 A |
1064 | if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) |
1065 | throwf("can't read %s file: %s", option, fileOfExports); | |
d696c285 | 1066 | |
c2646906 | 1067 | ::close(fd); |
d696c285 | 1068 | |
c2646906 A |
1069 | // parse into symbols and add to hash_set |
1070 | char * const end = &p[stat_buf.st_size]; | |
1071 | enum { lineStart, inSymbol, inComment } state = lineStart; | |
1072 | char* symbolStart = NULL; | |
1073 | for (char* s = p; s < end; ++s ) { | |
1074 | switch ( state ) { | |
d696c285 A |
1075 | case lineStart: |
1076 | if ( *s =='#' ) { | |
1077 | state = inComment; | |
1078 | } | |
1079 | else if ( !isspace(*s) ) { | |
1080 | state = inSymbol; | |
1081 | symbolStart = s; | |
1082 | } | |
1083 | break; | |
1084 | case inSymbol: | |
a61fdf0a | 1085 | if ( (*s == '\n') || (*s == '\r') ) { |
d696c285 A |
1086 | *s = '\0'; |
1087 | // removing any trailing spaces | |
1088 | char* last = s-1; | |
1089 | while ( isspace(*last) ) { | |
1090 | *last = '\0'; | |
1091 | --last; | |
c2646906 | 1092 | } |
d696c285 A |
1093 | set.insert(symbolStart); |
1094 | symbolStart = NULL; | |
1095 | state = lineStart; | |
1096 | } | |
1097 | break; | |
1098 | case inComment: | |
a61fdf0a | 1099 | if ( (*s == '\n') || (*s == '\r') ) |
d696c285 A |
1100 | state = lineStart; |
1101 | break; | |
1102 | } | |
1103 | } | |
1104 | if ( state == inSymbol ) { | |
2f2f92e4 | 1105 | warning("missing line-end at end of file \"%s\"", fileOfExports); |
d696c285 A |
1106 | int len = end-symbolStart+1; |
1107 | char* temp = new char[len]; | |
1108 | strlcpy(temp, symbolStart, len); | |
1109 | ||
1110 | // remove any trailing spaces | |
1111 | char* last = &temp[len-2]; | |
1112 | while ( isspace(*last) ) { | |
1113 | *last = '\0'; | |
1114 | --last; | |
c2646906 | 1115 | } |
d696c285 | 1116 | set.insert(temp); |
c2646906 | 1117 | } |
d696c285 | 1118 | |
c2646906 A |
1119 | // Note: we do not free() the malloc buffer, because the strings are used by the export-set hash table |
1120 | } | |
1121 | ||
a61fdf0a A |
1122 | void Options::parseAliasFile(const char* fileOfAliases) |
1123 | { | |
1124 | // read in whole file | |
1125 | int fd = ::open(fileOfAliases, O_RDONLY, 0); | |
1126 | if ( fd == -1 ) | |
1127 | throwf("can't open alias file: %s", fileOfAliases); | |
1128 | struct stat stat_buf; | |
1129 | ::fstat(fd, &stat_buf); | |
1130 | char* p = (char*)malloc(stat_buf.st_size+1); | |
1131 | if ( p == NULL ) | |
1132 | throwf("can't process alias file: %s", fileOfAliases); | |
1133 | ||
1134 | if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) | |
1135 | throwf("can't read alias file: %s", fileOfAliases); | |
1136 | p[stat_buf.st_size] = '\n'; | |
1137 | ::close(fd); | |
1138 | ||
1139 | // parse into symbols and add to fAliases | |
a645023d | 1140 | AliasPair pair; |
a61fdf0a A |
1141 | char * const end = &p[stat_buf.st_size+1]; |
1142 | enum { lineStart, inRealName, inBetween, inAliasName, inComment } state = lineStart; | |
1143 | int lineNumber = 1; | |
1144 | for (char* s = p; s < end; ++s ) { | |
1145 | switch ( state ) { | |
1146 | case lineStart: | |
1147 | if ( *s =='#' ) { | |
1148 | state = inComment; | |
1149 | } | |
1150 | else if ( !isspace(*s) ) { | |
1151 | state = inRealName; | |
1152 | pair.realName = s; | |
1153 | } | |
1154 | break; | |
1155 | case inRealName: | |
1156 | if ( *s == '\n' ) { | |
2f2f92e4 | 1157 | warning("line needs two symbols but has only one at line #%d in \"%s\"", lineNumber, fileOfAliases); |
a61fdf0a A |
1158 | ++lineNumber; |
1159 | state = lineStart; | |
1160 | } | |
1161 | else if ( isspace(*s) ) { | |
1162 | *s = '\0'; | |
1163 | state = inBetween; | |
1164 | } | |
1165 | break; | |
1166 | case inBetween: | |
1167 | if ( *s == '\n' ) { | |
2f2f92e4 | 1168 | warning("line needs two symbols but has only one at line #%d in \"%s\"", lineNumber, fileOfAliases); |
a61fdf0a A |
1169 | ++lineNumber; |
1170 | state = lineStart; | |
1171 | } | |
1172 | else if ( ! isspace(*s) ) { | |
1173 | state = inAliasName; | |
1174 | pair.alias = s; | |
1175 | } | |
1176 | break; | |
1177 | case inAliasName: | |
1178 | if ( *s =='#' ) { | |
1179 | *s = '\0'; | |
1180 | // removing any trailing spaces | |
1181 | char* last = s-1; | |
1182 | while ( isspace(*last) ) { | |
1183 | *last = '\0'; | |
1184 | --last; | |
1185 | } | |
a645023d | 1186 | fAliases.push_back(pair); |
a61fdf0a A |
1187 | state = inComment; |
1188 | } | |
1189 | else if ( *s == '\n' ) { | |
1190 | *s = '\0'; | |
1191 | // removing any trailing spaces | |
1192 | char* last = s-1; | |
1193 | while ( isspace(*last) ) { | |
1194 | *last = '\0'; | |
1195 | --last; | |
1196 | } | |
a645023d | 1197 | fAliases.push_back(pair); |
a61fdf0a A |
1198 | state = lineStart; |
1199 | } | |
1200 | break; | |
1201 | case inComment: | |
1202 | if ( *s == '\n' ) | |
1203 | state = lineStart; | |
1204 | break; | |
1205 | } | |
1206 | } | |
1207 | ||
1208 | // Note: we do not free() the malloc buffer, because the strings therein are used by fAliases | |
1209 | } | |
1210 | ||
1211 | ||
1212 | ||
c2646906 A |
1213 | void Options::setUndefinedTreatment(const char* treatment) |
1214 | { | |
d696c285 | 1215 | if ( treatment == NULL ) |
c2646906 A |
1216 | throw "-undefined missing [ warning | error | suppress | dynamic_lookup ]"; |
1217 | ||
1218 | if ( strcmp(treatment, "warning") == 0 ) | |
1219 | fUndefinedTreatment = kUndefinedWarning; | |
1220 | else if ( strcmp(treatment, "error") == 0 ) | |
1221 | fUndefinedTreatment = kUndefinedError; | |
1222 | else if ( strcmp(treatment, "suppress") == 0 ) | |
1223 | fUndefinedTreatment = kUndefinedSuppress; | |
1224 | else if ( strcmp(treatment, "dynamic_lookup") == 0 ) | |
1225 | fUndefinedTreatment = kUndefinedDynamicLookup; | |
1226 | else | |
1227 | throw "invalid option to -undefined [ warning | error | suppress | dynamic_lookup ]"; | |
1228 | } | |
1229 | ||
d696c285 | 1230 | Options::Treatment Options::parseTreatment(const char* treatment) |
c2646906 | 1231 | { |
d696c285 A |
1232 | if ( treatment == NULL ) |
1233 | return kNULL; | |
c2646906 A |
1234 | |
1235 | if ( strcmp(treatment, "warning") == 0 ) | |
d696c285 A |
1236 | return kWarning; |
1237 | else if ( strcmp(treatment, "error") == 0 ) | |
1238 | return kError; | |
c2646906 | 1239 | else if ( strcmp(treatment, "suppress") == 0 ) |
d696c285 A |
1240 | return kSuppress; |
1241 | else | |
1242 | return kInvalid; | |
c2646906 A |
1243 | } |
1244 | ||
2f2f92e4 | 1245 | void Options::setMacOSXVersionMin(const char* version) |
d696c285 A |
1246 | { |
1247 | if ( version == NULL ) | |
1248 | throw "-macosx_version_min argument missing"; | |
1249 | ||
69a49097 | 1250 | if ( (strncmp(version, "10.", 3) == 0) && isdigit(version[3]) ) { |
a645023d A |
1251 | unsigned int minorVersion = version[3] - '0'; |
1252 | fMacVersionMin = (ld::MacVersionMin)(0x000A0000 | (minorVersion << 8)); | |
69a49097 A |
1253 | } |
1254 | else { | |
2f2f92e4 A |
1255 | warning("unknown option to -macosx_version_min, not 10.x"); |
1256 | } | |
1257 | } | |
1258 | ||
afe874b1 | 1259 | void Options::setIOSVersionMin(const char* version) |
2f2f92e4 A |
1260 | { |
1261 | if ( version == NULL ) | |
a645023d | 1262 | throw "-ios_version_min argument missing"; |
fb24a050 | 1263 | if ( ! isdigit(version[0]) ) |
a645023d | 1264 | throw "-ios_version_min argument is not a number"; |
fb24a050 | 1265 | if ( version[1] != '.' ) |
a645023d | 1266 | throw "-ios_version_min argument is missing period as second character"; |
fb24a050 | 1267 | if ( ! isdigit(version[2]) ) |
a645023d A |
1268 | throw "-ios_version_min argument is not a number"; |
1269 | ||
1270 | unsigned int majorVersion = version[0] - '0'; | |
1271 | unsigned int minorVersion = version[2] - '0'; | |
afe874b1 | 1272 | fIOSVersionMin = (ld::IOSVersionMin)((majorVersion << 16) | (minorVersion << 8)); |
55e3d2f6 A |
1273 | } |
1274 | ||
afe874b1 | 1275 | bool Options::minOS(ld::MacVersionMin requiredMacMin, ld::IOSVersionMin requirediPhoneOSMin) |
55e3d2f6 | 1276 | { |
a645023d A |
1277 | if ( fMacVersionMin != ld::macVersionUnset ) { |
1278 | return ( fMacVersionMin >= requiredMacMin ); | |
55e3d2f6 A |
1279 | } |
1280 | else { | |
afe874b1 | 1281 | return ( fIOSVersionMin >= requirediPhoneOSMin); |
69a49097 | 1282 | } |
c2646906 A |
1283 | } |
1284 | ||
55e3d2f6 | 1285 | |
c2646906 A |
1286 | void Options::setWeakReferenceMismatchTreatment(const char* treatment) |
1287 | { | |
d696c285 | 1288 | if ( treatment == NULL ) |
c2646906 A |
1289 | throw "-weak_reference_mismatches missing [ error | weak | non-weak ]"; |
1290 | ||
1291 | if ( strcmp(treatment, "error") == 0 ) | |
1292 | fWeakReferenceMismatchTreatment = kWeakReferenceMismatchError; | |
1293 | else if ( strcmp(treatment, "weak") == 0 ) | |
1294 | fWeakReferenceMismatchTreatment = kWeakReferenceMismatchWeak; | |
1295 | else if ( strcmp(treatment, "non-weak") == 0 ) | |
1296 | fWeakReferenceMismatchTreatment = kWeakReferenceMismatchNonWeak; | |
1297 | else | |
1298 | throw "invalid option to -weak_reference_mismatches [ error | weak | non-weak ]"; | |
1299 | } | |
1300 | ||
1301 | Options::CommonsMode Options::parseCommonsTreatment(const char* mode) | |
1302 | { | |
d696c285 | 1303 | if ( mode == NULL ) |
c2646906 A |
1304 | throw "-commons missing [ ignore_dylibs | use_dylibs | error ]"; |
1305 | ||
1306 | if ( strcmp(mode, "ignore_dylibs") == 0 ) | |
1307 | return kCommonsIgnoreDylibs; | |
1308 | else if ( strcmp(mode, "use_dylibs") == 0 ) | |
1309 | return kCommonsOverriddenByDylibs; | |
1310 | else if ( strcmp(mode, "error") == 0 ) | |
1311 | return kCommonsConflictsDylibsError; | |
1312 | else | |
1313 | throw "invalid option to -commons [ ignore_dylibs | use_dylibs | error ]"; | |
1314 | } | |
1315 | ||
a61fdf0a | 1316 | void Options::addDylibOverride(const char* paths) |
c2646906 | 1317 | { |
a61fdf0a A |
1318 | if ( paths == NULL ) |
1319 | throw "-dylib_file must followed by two colon separated paths"; | |
1320 | const char* colon = strchr(paths, ':'); | |
1321 | if ( colon == NULL ) | |
1322 | throw "-dylib_file must followed by two colon separated paths"; | |
1323 | int len = colon-paths; | |
1324 | char* target = new char[len+2]; | |
1325 | strncpy(target, paths, len); | |
1326 | target[len] = '\0'; | |
1327 | DylibOverride entry; | |
1328 | entry.installName = target; | |
1329 | entry.useInstead = &colon[1]; | |
1330 | fDylibOverrides.push_back(entry); | |
c2646906 A |
1331 | } |
1332 | ||
c2646906 A |
1333 | uint64_t Options::parseAddress(const char* addr) |
1334 | { | |
1335 | char* endptr; | |
1336 | uint64_t result = strtoull(addr, &endptr, 16); | |
1337 | return result; | |
1338 | } | |
1339 | ||
a61fdf0a A |
1340 | uint32_t Options::parseProtection(const char* prot) |
1341 | { | |
1342 | uint32_t result = 0; | |
1343 | for(const char* p = prot; *p != '\0'; ++p) { | |
1344 | switch(tolower(*p)) { | |
1345 | case 'r': | |
1346 | result |= VM_PROT_READ; | |
1347 | break; | |
1348 | case 'w': | |
1349 | result |= VM_PROT_WRITE; | |
1350 | break; | |
1351 | case 'x': | |
1352 | result |= VM_PROT_EXECUTE; | |
1353 | break; | |
1354 | case '-': | |
1355 | break; | |
1356 | default: | |
1357 | throwf("unknown -segprot lettter in %s", prot); | |
1358 | } | |
1359 | } | |
1360 | return result; | |
1361 | } | |
1362 | ||
1363 | ||
b2fa67a8 A |
1364 | // |
1365 | // Parses number of form A[.B[.B[.D[.E]]]] into a uint64_t where the bits are a24.b10.c10.d10.e10 | |
1366 | // | |
1367 | uint64_t Options::parseVersionNumber64(const char* versionString) | |
1368 | { | |
1369 | uint64_t a = 0; | |
1370 | uint64_t b = 0; | |
1371 | uint64_t c = 0; | |
1372 | uint64_t d = 0; | |
1373 | uint64_t e = 0; | |
1374 | char* end; | |
1375 | a = strtoul(versionString, &end, 10); | |
1376 | if ( *end == '.' ) { | |
1377 | b = strtoul(&end[1], &end, 10); | |
1378 | if ( *end == '.' ) { | |
1379 | c = strtoul(&end[1], &end, 10); | |
1380 | if ( *end == '.' ) { | |
1381 | d = strtoul(&end[1], &end, 10); | |
1382 | if ( *end == '.' ) { | |
1383 | e = strtoul(&end[1], &end, 10); | |
1384 | } | |
1385 | } | |
1386 | } | |
1387 | } | |
1388 | if ( (*end != '\0') || (a > 0xFFFFFF) || (b > 0x3FF) || (c > 0x3FF) || (d > 0x3FF) || (e > 0x3FF) ) | |
1389 | throwf("malformed 64-bit a.b.c.d.e version number: %s", versionString); | |
1390 | ||
1391 | return (a << 40) | ( b << 30 ) | ( c << 20 ) | ( d << 10 ) | e; | |
1392 | } | |
1393 | ||
1394 | ||
1395 | uint32_t Options::currentVersion32() const | |
1396 | { | |
1397 | // warn if it does not fit into 32 bit vers number | |
1398 | uint32_t a = (fDylibCurrentVersion >> 40) & 0xFFFF; | |
1399 | uint32_t b = (fDylibCurrentVersion >> 30) & 0xFF; | |
1400 | uint32_t c = (fDylibCurrentVersion >> 20) & 0xFF; | |
1401 | uint64_t rep32 = ((uint64_t)a << 40) | ((uint64_t)b << 30) | ((uint64_t)c << 20); | |
1402 | if ( rep32 != fDylibCurrentVersion ) { | |
1403 | warning("truncating -current_version to fit in 32-bit space used by old mach-o format"); | |
1404 | a = (fDylibCurrentVersion >> 40) & 0xFFFFFF; | |
1405 | if ( a > 0xFFFF ) | |
1406 | a = 0xFFFF; | |
1407 | b = (fDylibCurrentVersion >> 30) & 0x3FF; | |
1408 | if ( b > 0xFF ) | |
1409 | b = 0xFF; | |
1410 | c = (fDylibCurrentVersion >> 20) & 0x3FF; | |
1411 | if ( c > 0xFF ) | |
1412 | c = 0xFF; | |
1413 | } | |
1414 | return (a << 16) | ( b << 8 ) | c; | |
1415 | } | |
a61fdf0a | 1416 | |
c2646906 | 1417 | // |
d696c285 | 1418 | // Parses number of form X[.Y[.Z]] into a uint32_t where the nibbles are xxxx.yy.zz |
c2646906 | 1419 | // |
b2fa67a8 | 1420 | uint32_t Options::parseVersionNumber32(const char* versionString) |
c2646906 | 1421 | { |
b2fa67a8 A |
1422 | uint32_t x = 0; |
1423 | uint32_t y = 0; | |
1424 | uint32_t z = 0; | |
c2646906 A |
1425 | char* end; |
1426 | x = strtoul(versionString, &end, 10); | |
1427 | if ( *end == '.' ) { | |
1428 | y = strtoul(&end[1], &end, 10); | |
1429 | if ( *end == '.' ) { | |
1430 | z = strtoul(&end[1], &end, 10); | |
1431 | } | |
1432 | } | |
1433 | if ( (*end != '\0') || (x > 0xffff) || (y > 0xff) || (z > 0xff) ) | |
b2fa67a8 | 1434 | throwf("malformed 32-bit x.y.z version number: %s", versionString); |
c2646906 A |
1435 | |
1436 | return (x << 16) | ( y << 8 ) | z; | |
1437 | } | |
1438 | ||
a61fdf0a A |
1439 | static const char* cstringSymbolName(const char* orderFileString) |
1440 | { | |
1441 | char* result; | |
1442 | asprintf(&result, "cstring=%s", orderFileString); | |
1443 | // convert escaped characters | |
1444 | char* d = result; | |
1445 | for(const char* s=result; *s != '\0'; ++s, ++d) { | |
1446 | if ( *s == '\\' ) { | |
1447 | ++s; | |
1448 | switch ( *s ) { | |
1449 | case 'n': | |
1450 | *d = '\n'; | |
1451 | break; | |
1452 | case 't': | |
1453 | *d = '\t'; | |
1454 | break; | |
1455 | case 'v': | |
1456 | *d = '\v'; | |
1457 | break; | |
1458 | case 'b': | |
1459 | *d = '\b'; | |
1460 | break; | |
1461 | case 'r': | |
1462 | *d = '\r'; | |
1463 | break; | |
1464 | case 'f': | |
1465 | *d = '\f'; | |
1466 | break; | |
1467 | case 'a': | |
1468 | *d = '\a'; | |
1469 | break; | |
1470 | case '\\': | |
1471 | *d = '\\'; | |
1472 | break; | |
1473 | case '?': | |
1474 | *d = '\?'; | |
1475 | break; | |
1476 | case '\'': | |
1477 | *d = '\r'; | |
1478 | break; | |
1479 | case '\"': | |
1480 | *d = '\"'; | |
1481 | break; | |
1482 | case 'x': | |
1483 | // hexadecimal value of char | |
1484 | { | |
1485 | ++s; | |
1486 | char value = 0; | |
1487 | while ( isxdigit(*s) ) { | |
1488 | value *= 16; | |
1489 | if ( isdigit(*s) ) | |
1490 | value += (*s-'0'); | |
1491 | else | |
1492 | value += ((toupper(*s)-'A') + 10); | |
1493 | ++s; | |
1494 | } | |
1495 | *d = value; | |
1496 | } | |
1497 | break; | |
1498 | default: | |
1499 | if ( isdigit(*s) ) { | |
1500 | // octal value of char | |
1501 | char value = 0; | |
1502 | while ( isdigit(*s) ) { | |
1503 | value = (value << 3) + (*s-'0'); | |
1504 | ++s; | |
1505 | } | |
1506 | *d = value; | |
1507 | } | |
1508 | } | |
1509 | } | |
1510 | else { | |
1511 | *d = *s; | |
1512 | } | |
1513 | } | |
1514 | *d = '\0'; | |
1515 | return result; | |
1516 | } | |
1517 | ||
1518 | void Options::parseOrderFile(const char* path, bool cstring) | |
1519 | { | |
55e3d2f6 | 1520 | // order files override auto-ordering |
a645023d | 1521 | fAutoOrderInitializers = false; |
55e3d2f6 | 1522 | |
a61fdf0a A |
1523 | // read in whole file |
1524 | int fd = ::open(path, O_RDONLY, 0); | |
1525 | if ( fd == -1 ) | |
1526 | throwf("can't open order file: %s", path); | |
1527 | struct stat stat_buf; | |
1528 | ::fstat(fd, &stat_buf); | |
1529 | char* p = (char*)malloc(stat_buf.st_size+1); | |
1530 | if ( p == NULL ) | |
1531 | throwf("can't process order file: %s", path); | |
1532 | if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) | |
1533 | throwf("can't read order file: %s", path); | |
1534 | ::close(fd); | |
1535 | p[stat_buf.st_size] = '\n'; | |
1536 | ||
1537 | // parse into vector of pairs | |
1538 | char * const end = &p[stat_buf.st_size+1]; | |
1539 | enum { lineStart, inSymbol, inComment } state = lineStart; | |
1540 | char* symbolStart = NULL; | |
1541 | for (char* s = p; s < end; ++s ) { | |
1542 | switch ( state ) { | |
1543 | case lineStart: | |
1544 | if ( *s =='#' ) { | |
1545 | state = inComment; | |
1546 | } | |
1547 | else if ( !isspace(*s) || cstring ) { | |
1548 | state = inSymbol; | |
1549 | symbolStart = s; | |
1550 | } | |
1551 | break; | |
1552 | case inSymbol: | |
1553 | if ( (*s == '\n') || (!cstring && (*s == '#')) ) { | |
1554 | bool wasComment = (*s == '#'); | |
1555 | *s = '\0'; | |
1556 | // removing any trailing spaces | |
1557 | char* last = s-1; | |
1558 | while ( isspace(*last) ) { | |
1559 | *last = '\0'; | |
1560 | --last; | |
1561 | } | |
b2fa67a8 | 1562 | // if there is an architecture prefix, only use this symbol it if matches current arch |
a61fdf0a | 1563 | if ( strncmp(symbolStart, "ppc:", 4) == 0 ) { |
b2fa67a8 | 1564 | symbolStart = NULL; |
a61fdf0a | 1565 | } |
a61fdf0a | 1566 | else if ( strncmp(symbolStart, "ppc64:", 6) == 0 ) { |
b2fa67a8 | 1567 | symbolStart = NULL; |
a61fdf0a A |
1568 | } |
1569 | else if ( strncmp(symbolStart, "i386:", 5) == 0 ) { | |
1570 | if ( fArchitecture == CPU_TYPE_I386 ) | |
1571 | symbolStart = &symbolStart[5]; | |
1572 | else | |
1573 | symbolStart = NULL; | |
1574 | } | |
1575 | else if ( strncmp(symbolStart, "x86_64:", 7) == 0 ) { | |
1576 | if ( fArchitecture == CPU_TYPE_X86_64 ) | |
1577 | symbolStart = &symbolStart[7]; | |
1578 | else | |
1579 | symbolStart = NULL; | |
1580 | } | |
2f2f92e4 A |
1581 | else if ( strncmp(symbolStart, "arm:", 4) == 0 ) { |
1582 | if ( fArchitecture == CPU_TYPE_ARM ) | |
1583 | symbolStart = &symbolStart[4]; | |
1584 | else | |
1585 | symbolStart = NULL; | |
1586 | } | |
a61fdf0a A |
1587 | if ( symbolStart != NULL ) { |
1588 | char* objFileName = NULL; | |
1589 | char* colon = strstr(symbolStart, ".o:"); | |
1590 | if ( colon != NULL ) { | |
1591 | colon[2] = '\0'; | |
1592 | objFileName = symbolStart; | |
1593 | symbolStart = &colon[3]; | |
1594 | } | |
1595 | // trim leading spaces | |
1596 | while ( isspace(*symbolStart) ) | |
1597 | ++symbolStart; | |
1598 | Options::OrderedSymbol pair; | |
1599 | if ( cstring ) | |
1600 | pair.symbolName = cstringSymbolName(symbolStart); | |
1601 | else | |
1602 | pair.symbolName = symbolStart; | |
1603 | pair.objectFileName = objFileName; | |
1604 | fOrderedSymbols.push_back(pair); | |
1605 | } | |
1606 | symbolStart = NULL; | |
1607 | if ( wasComment ) | |
1608 | state = inComment; | |
1609 | else | |
1610 | state = lineStart; | |
1611 | } | |
1612 | break; | |
1613 | case inComment: | |
1614 | if ( *s == '\n' ) | |
1615 | state = lineStart; | |
1616 | break; | |
1617 | } | |
1618 | } | |
1619 | // Note: we do not free() the malloc buffer, because the strings are used by the fOrderedSymbols | |
1620 | } | |
1621 | ||
c2646906 A |
1622 | void Options::parseSectionOrderFile(const char* segment, const char* section, const char* path) |
1623 | { | |
a61fdf0a A |
1624 | if ( (strcmp(section, "__cstring") == 0) && (strcmp(segment, "__TEXT") == 0) ) { |
1625 | parseOrderFile(path, true); | |
1626 | } | |
1627 | else if ( (strncmp(section, "__literal",9) == 0) && (strcmp(segment, "__TEXT") == 0) ) { | |
2f2f92e4 | 1628 | warning("sorting of __literal[4,8,16] sections not supported"); |
a61fdf0a A |
1629 | } |
1630 | else { | |
1631 | // ignore section information and append all symbol names to global order file | |
1632 | parseOrderFile(path, false); | |
1633 | } | |
c2646906 A |
1634 | } |
1635 | ||
1636 | void Options::addSection(const char* segment, const char* section, const char* path) | |
1637 | { | |
1638 | if ( strlen(segment) > 16 ) | |
1639 | throw "-seccreate segment name max 16 chars"; | |
d696c285 A |
1640 | if ( strlen(section) > 16 ) { |
1641 | char* tmp = strdup(section); | |
1642 | tmp[16] = '\0'; | |
2f2f92e4 | 1643 | warning("-seccreate section name (%s) truncated to 16 chars (%s)\n", section, tmp); |
d696c285 A |
1644 | section = tmp; |
1645 | } | |
c2646906 A |
1646 | |
1647 | // read in whole file | |
1648 | int fd = ::open(path, O_RDONLY, 0); | |
1649 | if ( fd == -1 ) | |
1650 | throwf("can't open -sectcreate file: %s", path); | |
1651 | struct stat stat_buf; | |
1652 | ::fstat(fd, &stat_buf); | |
1653 | char* p = (char*)malloc(stat_buf.st_size); | |
1654 | if ( p == NULL ) | |
1655 | throwf("can't process -sectcreate file: %s", path); | |
1656 | if ( read(fd, p, stat_buf.st_size) != stat_buf.st_size ) | |
1657 | throwf("can't read -sectcreate file: %s", path); | |
1658 | ::close(fd); | |
d696c285 | 1659 | |
c2646906 A |
1660 | // record section to create |
1661 | ExtraSection info = { segment, section, path, (uint8_t*)p, stat_buf.st_size }; | |
1662 | fExtraSections.push_back(info); | |
1663 | } | |
1664 | ||
1665 | void Options::addSectionAlignment(const char* segment, const char* section, const char* alignmentStr) | |
1666 | { | |
1667 | if ( strlen(segment) > 16 ) | |
1668 | throw "-sectalign segment name max 16 chars"; | |
1669 | if ( strlen(section) > 16 ) | |
1670 | throw "-sectalign section name max 16 chars"; | |
1671 | ||
d696c285 | 1672 | // argument to -sectalign is a hexadecimal number |
c2646906 A |
1673 | char* endptr; |
1674 | unsigned long value = strtoul(alignmentStr, &endptr, 16); | |
1675 | if ( *endptr != '\0') | |
1676 | throw "argument for -sectalign is not a hexadecimal number"; | |
1677 | if ( value > 0x8000 ) | |
1678 | throw "argument for -sectalign must be less than or equal to 0x8000"; | |
d696c285 | 1679 | if ( value == 0 ) { |
2f2f92e4 | 1680 | warning("zero is not a valid -sectalign"); |
d696c285 A |
1681 | value = 1; |
1682 | } | |
a61fdf0a | 1683 | |
d696c285 | 1684 | // alignment is power of 2 (e.g. page alignment = 12) |
a61fdf0a | 1685 | uint8_t alignment = (uint8_t)__builtin_ctz(value); |
d696c285 | 1686 | if ( (unsigned long)(1 << alignment) != value ) { |
2f2f92e4 | 1687 | warning("alignment for -sectalign %s %s is not a power of two, using 0x%X", |
d696c285 A |
1688 | segment, section, 1 << alignment); |
1689 | } | |
c2646906 A |
1690 | |
1691 | SectionAlignment info = { segment, section, alignment }; | |
1692 | fSectionAlignments.push_back(info); | |
1693 | } | |
1694 | ||
a61fdf0a A |
1695 | void Options::addLibrary(const FileInfo& info) |
1696 | { | |
1697 | // if this library has already been added, don't add again (archives are automatically repeatedly searched) | |
1698 | for (std::vector<Options::FileInfo>::iterator fit = fInputFiles.begin(); fit != fInputFiles.end(); fit++) { | |
1699 | if ( strcmp(info.path, fit->path) == 0 ) { | |
1700 | // if dylib is specified again but weak, record that it should be weak | |
1701 | if ( info.options.fWeakImport ) | |
1702 | fit->options.fWeakImport = true; | |
1703 | return; | |
1704 | } | |
1705 | } | |
1706 | // add to list | |
1707 | fInputFiles.push_back(info); | |
1708 | } | |
1709 | ||
1710 | void Options::warnObsolete(const char* arg) | |
1711 | { | |
2f2f92e4 | 1712 | warning("option %s is obsolete and being ignored", arg); |
a61fdf0a A |
1713 | } |
1714 | ||
1715 | ||
1716 | ||
1717 | ||
d696c285 A |
1718 | // |
1719 | // Process all command line arguments. | |
1720 | // | |
1721 | // The only error checking done here is that each option is valid and if it has arguments | |
1722 | // that they too are valid. | |
1723 | // | |
1724 | // The general rule is "last option wins", i.e. if both -bundle and -dylib are specified, | |
1725 | // whichever was last on the command line is used. | |
1726 | // | |
1727 | // Error check for invalid combinations of options is done in checkIllegalOptionCombinations() | |
1728 | // | |
c2646906 A |
1729 | void Options::parse(int argc, const char* argv[]) |
1730 | { | |
ebf6f434 A |
1731 | // Store the original args in the link snapshot. |
1732 | fLinkSnapshot.recordRawArgs(argc, argv); | |
1733 | ||
c2646906 A |
1734 | // pass one builds search list from -L and -F options |
1735 | this->buildSearchPaths(argc, argv); | |
d696c285 A |
1736 | |
1737 | // reduce re-allocations | |
1738 | fInputFiles.reserve(32); | |
1739 | ||
c2646906 A |
1740 | // pass two parse all other options |
1741 | for(int i=1; i < argc; ++i) { | |
1742 | const char* arg = argv[i]; | |
d696c285 | 1743 | |
c2646906 | 1744 | if ( arg[0] == '-' ) { |
ebf6f434 A |
1745 | // by default, copy one arg to the snapshot link command, and do no file copying |
1746 | int snapshotArgIndex = i; | |
1747 | int snapshotArgCount = -1; // -1 means compute count based on change in index | |
1748 | int snapshotFileArgIndex = -1; // -1 means no data file parameter to arg | |
d696c285 A |
1749 | |
1750 | // Since we don't care about the files passed, just the option names, we do this here. | |
1751 | if (fPrintOptions) | |
1752 | fprintf (stderr, "[Logging ld64 options]\t%s\n", arg); | |
1753 | ||
c2646906 | 1754 | if ( (arg[1] == 'L') || (arg[1] == 'F') ) { |
ebf6f434 | 1755 | snapshotArgCount = 0; // stripped out of link snapshot |
a645023d A |
1756 | if (arg[2] == '\0') |
1757 | ++i; | |
6e880c60 | 1758 | // previously handled by buildSearchPaths() |
c2646906 | 1759 | } |
a61fdf0a A |
1760 | // The one gnu style option we have to keep compatibility |
1761 | // with gcc. Might as well have the single hyphen one as well. | |
1762 | else if ( (strcmp(arg, "--help") == 0) | |
1763 | || (strcmp(arg, "-help") == 0)) { | |
1764 | fprintf (stdout, "ld64: For information on command line options please use 'man ld'.\n"); | |
1765 | exit (0); | |
1766 | } | |
c2646906 A |
1767 | else if ( strcmp(arg, "-arch") == 0 ) { |
1768 | parseArch(argv[++i]); | |
1769 | } | |
1770 | else if ( strcmp(arg, "-dynamic") == 0 ) { | |
1771 | // default | |
1772 | } | |
1773 | else if ( strcmp(arg, "-static") == 0 ) { | |
a645023d A |
1774 | fForStatic = true; |
1775 | if ( (fOutputKind != kObjectFile) && (fOutputKind != kKextBundle) ) { | |
55e3d2f6 A |
1776 | fOutputKind = kStaticExecutable; |
1777 | } | |
c2646906 A |
1778 | } |
1779 | else if ( strcmp(arg, "-dylib") == 0 ) { | |
1780 | fOutputKind = kDynamicLibrary; | |
1781 | } | |
1782 | else if ( strcmp(arg, "-bundle") == 0 ) { | |
1783 | fOutputKind = kDynamicBundle; | |
1784 | } | |
1785 | else if ( strcmp(arg, "-dylinker") == 0 ) { | |
1786 | fOutputKind = kDyld; | |
1787 | } | |
1788 | else if ( strcmp(arg, "-execute") == 0 ) { | |
1789 | if ( fOutputKind != kStaticExecutable ) | |
1790 | fOutputKind = kDynamicExecutable; | |
1791 | } | |
55e3d2f6 A |
1792 | else if ( strcmp(arg, "-preload") == 0 ) { |
1793 | fOutputKind = kPreload; | |
1794 | } | |
c2646906 A |
1795 | else if ( strcmp(arg, "-r") == 0 ) { |
1796 | fOutputKind = kObjectFile; | |
1797 | } | |
55e3d2f6 A |
1798 | else if ( strcmp(arg, "-kext") == 0 ) { |
1799 | fOutputKind = kKextBundle; | |
1800 | } | |
c2646906 | 1801 | else if ( strcmp(arg, "-o") == 0 ) { |
ebf6f434 | 1802 | snapshotArgCount = 0; |
c2646906 | 1803 | fOutputFile = argv[++i]; |
ebf6f434 | 1804 | fLinkSnapshot.setSnapshotName(fOutputFile); |
c2646906 | 1805 | } |
a645023d | 1806 | else if ( strncmp(arg, "-lazy-l", 7) == 0 ) { |
ebf6f434 | 1807 | snapshotArgCount = 0; |
a645023d A |
1808 | FileInfo info = findLibrary(&arg[7], true); |
1809 | info.options.fLazyLoad = true; | |
ebf6f434 | 1810 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a645023d A |
1811 | addLibrary(info); |
1812 | fUsingLazyDylibLinking = true; | |
1813 | } | |
ebf6f434 A |
1814 | else if ( strcmp(arg, "-lto_library") == 0 ) { |
1815 | snapshotFileArgIndex = 1; | |
1816 | fOverridePathlibLTO = argv[++i]; | |
1817 | if ( fOverridePathlibLTO == NULL ) | |
1818 | throw "missing argument to -lto_library"; | |
1819 | } | |
2f2f92e4 | 1820 | else if ( (arg[1] == 'l') && (strncmp(arg,"-lazy_",6) !=0) ) { |
ebf6f434 A |
1821 | snapshotArgCount = 0; |
1822 | FileInfo info = findLibrary(&arg[2]); | |
1823 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); | |
1824 | addLibrary(info); | |
c2646906 | 1825 | } |
d696c285 A |
1826 | // This causes a dylib to be weakly bound at |
1827 | // link time. This corresponds to weak_import. | |
1828 | else if ( strncmp(arg, "-weak-l", 7) == 0 ) { | |
ebf6f434 A |
1829 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
1830 | snapshotArgCount = 0; | |
d696c285 | 1831 | FileInfo info = findLibrary(&arg[7]); |
c2646906 | 1832 | info.options.fWeakImport = true; |
ebf6f434 | 1833 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a | 1834 | addLibrary(info); |
c2646906 | 1835 | } |
d696c285 | 1836 | // Avoid lazy binding. |
c2646906 A |
1837 | else if ( strcmp(arg, "-bind_at_load") == 0 ) { |
1838 | fBindAtLoad = true; | |
1839 | } | |
1840 | else if ( strcmp(arg, "-twolevel_namespace") == 0 ) { | |
1841 | fNameSpace = kTwoLevelNameSpace; | |
1842 | } | |
1843 | else if ( strcmp(arg, "-flat_namespace") == 0 ) { | |
1844 | fNameSpace = kFlatNameSpace; | |
1845 | } | |
d696c285 A |
1846 | // Also sets a bit to ensure dyld causes everything |
1847 | // in the namespace to be flat. | |
1848 | // ??? Deprecate | |
c2646906 A |
1849 | else if ( strcmp(arg, "-force_flat_namespace") == 0 ) { |
1850 | fNameSpace = kForceFlatNameSpace; | |
1851 | } | |
d696c285 | 1852 | // Similar to --whole-archive. |
c2646906 | 1853 | else if ( strcmp(arg, "-all_load") == 0 ) { |
a645023d | 1854 | fFullyLoadArchives = true; |
c2646906 | 1855 | } |
a61fdf0a A |
1856 | else if ( strcmp(arg, "-noall_load") == 0) { |
1857 | warnObsolete(arg); | |
1858 | } | |
1859 | // Similar to -all_load | |
c2646906 | 1860 | else if ( strcmp(arg, "-ObjC") == 0 ) { |
a645023d | 1861 | fLoadAllObjcObjectsFromArchives = true; |
c2646906 | 1862 | } |
55e3d2f6 A |
1863 | // Similar to -all_load, but for the following archive only. |
1864 | else if ( strcmp(arg, "-force_load") == 0 ) { | |
1865 | FileInfo info = findFile(argv[++i]); | |
1866 | info.options.fForceLoad = true; | |
ebf6f434 | 1867 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
55e3d2f6 A |
1868 | addLibrary(info); |
1869 | } | |
d696c285 | 1870 | // Library versioning. |
a61fdf0a A |
1871 | else if ( (strcmp(arg, "-dylib_compatibility_version") == 0) |
1872 | || (strcmp(arg, "-compatibility_version") == 0)) { | |
1873 | const char* vers = argv[++i]; | |
1874 | if ( vers == NULL ) | |
1875 | throw "-dylib_compatibility_version missing <version>"; | |
b2fa67a8 | 1876 | fDylibCompatVersion = parseVersionNumber32(vers); |
a61fdf0a A |
1877 | } |
1878 | else if ( (strcmp(arg, "-dylib_current_version") == 0) | |
1879 | || (strcmp(arg, "-current_version") == 0)) { | |
1880 | const char* vers = argv[++i]; | |
1881 | if ( vers == NULL ) | |
1882 | throw "-dylib_current_version missing <version>"; | |
b2fa67a8 | 1883 | fDylibCurrentVersion = parseVersionNumber64(vers); |
c2646906 A |
1884 | } |
1885 | else if ( strcmp(arg, "-sectorder") == 0 ) { | |
a61fdf0a A |
1886 | if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) |
1887 | throw "-sectorder missing <segment> <section> <file-path>"; | |
ebf6f434 | 1888 | snapshotFileArgIndex = 3; |
c2646906 A |
1889 | parseSectionOrderFile(argv[i+1], argv[i+2], argv[i+3]); |
1890 | i += 3; | |
1891 | } | |
a61fdf0a | 1892 | else if ( strcmp(arg, "-order_file") == 0 ) { |
ebf6f434 | 1893 | snapshotFileArgIndex = 1; |
a61fdf0a A |
1894 | parseOrderFile(argv[++i], false); |
1895 | } | |
1896 | else if ( strcmp(arg, "-order_file_statistics") == 0 ) { | |
1897 | fPrintOrderFileStatistics = true; | |
1898 | } | |
d696c285 A |
1899 | // ??? Deprecate segcreate. |
1900 | // -sectcreate puts whole files into a section in the output. | |
c2646906 | 1901 | else if ( (strcmp(arg, "-sectcreate") == 0) || (strcmp(arg, "-segcreate") == 0) ) { |
a61fdf0a A |
1902 | if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) |
1903 | throw "-sectcreate missing <segment> <section> <file-path>"; | |
ebf6f434 | 1904 | snapshotFileArgIndex = 3; |
c2646906 A |
1905 | addSection(argv[i+1], argv[i+2], argv[i+3]); |
1906 | i += 3; | |
1907 | } | |
d696c285 | 1908 | // Since we have a full path in binary/library names we need to be able to override it. |
a61fdf0a A |
1909 | else if ( (strcmp(arg, "-dylib_install_name") == 0) |
1910 | || (strcmp(arg, "-dylinker_install_name") == 0) | |
1911 | || (strcmp(arg, "-install_name") == 0)) { | |
c2646906 | 1912 | fDylibInstallName = argv[++i]; |
ebf6f434 | 1913 | if ( fDylibInstallName == NULL ) |
a61fdf0a | 1914 | throw "-install_name missing <path>"; |
c2646906 | 1915 | } |
d696c285 | 1916 | // Sets the base address of the output. |
69a49097 | 1917 | else if ( (strcmp(arg, "-seg1addr") == 0) || (strcmp(arg, "-image_base") == 0) ) { |
a61fdf0a A |
1918 | const char* address = argv[++i]; |
1919 | if ( address == NULL ) | |
1920 | throwf("%s missing <address>", arg); | |
1921 | fBaseAddress = parseAddress(address); | |
55e3d2f6 | 1922 | uint64_t temp = ((fBaseAddress+fSegmentAlignment-1) & (-fSegmentAlignment)); |
2f2f92e4 | 1923 | if ( fBaseAddress != temp ) { |
55e3d2f6 | 1924 | warning("-seg1addr not %lld byte aligned, rounding up", fSegmentAlignment); |
2f2f92e4 A |
1925 | fBaseAddress = temp; |
1926 | } | |
c2646906 A |
1927 | } |
1928 | else if ( strcmp(arg, "-e") == 0 ) { | |
1929 | fEntryName = argv[++i]; | |
1930 | } | |
d696c285 | 1931 | // Same as -@ from the FSF linker. |
c2646906 | 1932 | else if ( strcmp(arg, "-filelist") == 0 ) { |
ebf6f434 | 1933 | snapshotArgCount = 0; |
a61fdf0a A |
1934 | const char* path = argv[++i]; |
1935 | if ( (path == NULL) || (path[0] == '-') ) | |
1936 | throw "-filelist missing <path>"; | |
ebf6f434 A |
1937 | ld::File::Ordinal baseOrdinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
1938 | loadFileList(path, baseOrdinal); | |
c2646906 A |
1939 | } |
1940 | else if ( strcmp(arg, "-keep_private_externs") == 0 ) { | |
1941 | fKeepPrivateExterns = true; | |
1942 | } | |
1943 | else if ( strcmp(arg, "-final_output") == 0 ) { | |
a61fdf0a | 1944 | fFinalName = argv[++i]; |
c2646906 | 1945 | } |
d696c285 A |
1946 | // Ensure that all calls to exported symbols go through lazy pointers. Multi-module |
1947 | // just ensures that this happens for cross object file boundaries. | |
c2646906 | 1948 | else if ( (strcmp(arg, "-interposable") == 0) || (strcmp(arg, "-multi_module") == 0)) { |
2f2f92e4 A |
1949 | switch ( fInterposeMode ) { |
1950 | case kInterposeNone: | |
1951 | case kInterposeAllExternal: | |
1952 | fInterposeMode = kInterposeAllExternal; | |
1953 | break; | |
1954 | case kInterposeSome: | |
1955 | // do nothing, -interposable_list overrides -interposable" | |
1956 | break; | |
1957 | } | |
1958 | } | |
1959 | else if ( strcmp(arg, "-interposable_list") == 0 ) { | |
ebf6f434 | 1960 | snapshotFileArgIndex = 1; |
2f2f92e4 A |
1961 | fInterposeMode = kInterposeSome; |
1962 | loadExportFile(argv[++i], "-interposable_list", fInterposeList); | |
c2646906 | 1963 | } |
d696c285 | 1964 | // Default for -interposable/-multi_module/-single_module. |
c2646906 | 1965 | else if ( strcmp(arg, "-single_module") == 0 ) { |
2f2f92e4 | 1966 | fInterposeMode = kInterposeNone; |
c2646906 A |
1967 | } |
1968 | else if ( strcmp(arg, "-exported_symbols_list") == 0 ) { | |
ebf6f434 | 1969 | snapshotFileArgIndex = 1; |
c2646906 A |
1970 | if ( fExportMode == kDontExportSome ) |
1971 | throw "can't use -exported_symbols_list and -unexported_symbols_list"; | |
1972 | fExportMode = kExportSome; | |
1973 | loadExportFile(argv[++i], "-exported_symbols_list", fExportSymbols); | |
1974 | } | |
1975 | else if ( strcmp(arg, "-unexported_symbols_list") == 0 ) { | |
ebf6f434 | 1976 | snapshotFileArgIndex = 1; |
c2646906 | 1977 | if ( fExportMode == kExportSome ) |
a61fdf0a | 1978 | throw "can't use -unexported_symbols_list and -exported_symbols_list"; |
c2646906 A |
1979 | fExportMode = kDontExportSome; |
1980 | loadExportFile(argv[++i], "-unexported_symbols_list", fDontExportSymbols); | |
1981 | } | |
a61fdf0a A |
1982 | else if ( strcmp(arg, "-exported_symbol") == 0 ) { |
1983 | if ( fExportMode == kDontExportSome ) | |
1984 | throw "can't use -exported_symbol and -unexported_symbols"; | |
1985 | fExportMode = kExportSome; | |
1986 | fExportSymbols.insert(argv[++i]); | |
1987 | } | |
1988 | else if ( strcmp(arg, "-unexported_symbol") == 0 ) { | |
1989 | if ( fExportMode == kExportSome ) | |
1990 | throw "can't use -unexported_symbol and -exported_symbol"; | |
1991 | fExportMode = kDontExportSome; | |
1992 | fDontExportSymbols.insert(argv[++i]); | |
1993 | } | |
1994 | else if ( strcmp(arg, "-non_global_symbols_no_strip_list") == 0 ) { | |
ebf6f434 | 1995 | snapshotFileArgIndex = 1; |
a61fdf0a A |
1996 | if ( fLocalSymbolHandling == kLocalSymbolsSelectiveExclude ) |
1997 | throw "can't use -non_global_symbols_no_strip_list and -non_global_symbols_strip_list"; | |
1998 | fLocalSymbolHandling = kLocalSymbolsSelectiveInclude; | |
1999 | loadExportFile(argv[++i], "-non_global_symbols_no_strip_list", fLocalSymbolsIncluded); | |
2000 | } | |
2001 | else if ( strcmp(arg, "-non_global_symbols_strip_list") == 0 ) { | |
ebf6f434 | 2002 | snapshotFileArgIndex = 1; |
a61fdf0a A |
2003 | if ( fLocalSymbolHandling == kLocalSymbolsSelectiveInclude ) |
2004 | throw "can't use -non_global_symbols_no_strip_list and -non_global_symbols_strip_list"; | |
2005 | fLocalSymbolHandling = kLocalSymbolsSelectiveExclude; | |
2006 | loadExportFile(argv[++i], "-non_global_symbols_strip_list", fLocalSymbolsExcluded); | |
2007 | } | |
d696c285 | 2008 | // ??? Deprecate |
c2646906 A |
2009 | else if ( strcmp(arg, "-no_arch_warnings") == 0 ) { |
2010 | fIgnoreOtherArchFiles = true; | |
2011 | } | |
2012 | else if ( strcmp(arg, "-force_cpusubtype_ALL") == 0 ) { | |
afe874b1 A |
2013 | fForceSubtypeAll = true; |
2014 | fAllowCpuSubtypeMismatches = true; | |
c2646906 | 2015 | } |
d696c285 | 2016 | // Similar to -weak-l but uses the absolute path name to the library. |
c2646906 | 2017 | else if ( strcmp(arg, "-weak_library") == 0 ) { |
ebf6f434 A |
2018 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2019 | snapshotArgCount = 0; | |
6e880c60 | 2020 | FileInfo info = findFile(argv[++i]); |
c2646906 | 2021 | info.options.fWeakImport = true; |
ebf6f434 | 2022 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a | 2023 | addLibrary(info); |
c2646906 | 2024 | } |
2f2f92e4 | 2025 | else if ( strcmp(arg, "-lazy_library") == 0 ) { |
ebf6f434 A |
2026 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2027 | snapshotArgCount = 0; | |
2f2f92e4 A |
2028 | FileInfo info = findFile(argv[++i]); |
2029 | info.options.fLazyLoad = true; | |
ebf6f434 | 2030 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
2f2f92e4 A |
2031 | addLibrary(info); |
2032 | fUsingLazyDylibLinking = true; | |
2033 | } | |
c2646906 | 2034 | else if ( strcmp(arg, "-framework") == 0 ) { |
ebf6f434 A |
2035 | snapshotArgCount = 0; |
2036 | FileInfo info = findFramework(argv[++i]); | |
2037 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); | |
2038 | addLibrary(info); | |
c2646906 A |
2039 | } |
2040 | else if ( strcmp(arg, "-weak_framework") == 0 ) { | |
ebf6f434 A |
2041 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2042 | snapshotArgCount = 0; | |
c2646906 A |
2043 | FileInfo info = findFramework(argv[++i]); |
2044 | info.options.fWeakImport = true; | |
ebf6f434 | 2045 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a | 2046 | addLibrary(info); |
c2646906 | 2047 | } |
2f2f92e4 | 2048 | else if ( strcmp(arg, "-lazy_framework") == 0 ) { |
ebf6f434 A |
2049 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2050 | snapshotArgCount = 0; | |
2f2f92e4 A |
2051 | FileInfo info = findFramework(argv[++i]); |
2052 | info.options.fLazyLoad = true; | |
ebf6f434 | 2053 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
2f2f92e4 A |
2054 | addLibrary(info); |
2055 | fUsingLazyDylibLinking = true; | |
2056 | } | |
c2646906 | 2057 | else if ( strcmp(arg, "-search_paths_first") == 0 ) { |
a61fdf0a | 2058 | // previously handled by buildSearchPaths() |
c2646906 | 2059 | } |
a645023d A |
2060 | else if ( strcmp(arg, "-search_dylibs_first") == 0 ) { |
2061 | // previously handled by buildSearchPaths() | |
2062 | } | |
c2646906 | 2063 | else if ( strcmp(arg, "-undefined") == 0 ) { |
ebf6f434 | 2064 | setUndefinedTreatment(argv[++i]); |
c2646906 | 2065 | } |
d696c285 | 2066 | // Debugging output flag. |
c2646906 A |
2067 | else if ( strcmp(arg, "-arch_multiple") == 0 ) { |
2068 | fMessagesPrefixedWithArchitecture = true; | |
2069 | } | |
d696c285 A |
2070 | // Specify what to do with relocations in read only |
2071 | // sections like .text. Could be errors, warnings, | |
2072 | // or suppressed. Currently we do nothing with the | |
2073 | // flag. | |
c2646906 | 2074 | else if ( strcmp(arg, "-read_only_relocs") == 0 ) { |
2f2f92e4 A |
2075 | switch ( parseTreatment(argv[++i]) ) { |
2076 | case kNULL: | |
2077 | case kInvalid: | |
2078 | throw "-read_only_relocs missing [ warning | error | suppress ]"; | |
2079 | case kWarning: | |
2080 | fWarnTextRelocs = true; | |
2081 | fAllowTextRelocs = true; | |
2082 | break; | |
2083 | case kSuppress: | |
2084 | fWarnTextRelocs = false; | |
2085 | fAllowTextRelocs = true; | |
2086 | break; | |
2087 | case kError: | |
2088 | fWarnTextRelocs = false; | |
2089 | fAllowTextRelocs = false; | |
2090 | break; | |
2091 | } | |
c2646906 A |
2092 | } |
2093 | else if ( strcmp(arg, "-sect_diff_relocs") == 0 ) { | |
a61fdf0a A |
2094 | warnObsolete(arg); |
2095 | ++i; | |
c2646906 | 2096 | } |
d696c285 A |
2097 | // Warn, error or make strong a mismatch between weak |
2098 | // and non-weak references. | |
c2646906 | 2099 | else if ( strcmp(arg, "-weak_reference_mismatches") == 0 ) { |
ebf6f434 | 2100 | setWeakReferenceMismatchTreatment(argv[++i]); |
c2646906 | 2101 | } |
d696c285 A |
2102 | // For a deployment target of 10.3 and earlier ld64 will |
2103 | // prebind an executable with 0s in all addresses that | |
2104 | // are prebound. This can then be fixed up by update_prebinding | |
2105 | // later. Prebinding is less useful on 10.4 and greater. | |
c2646906 | 2106 | else if ( strcmp(arg, "-prebind") == 0 ) { |
d696c285 | 2107 | fPrebind = true; |
c2646906 A |
2108 | } |
2109 | else if ( strcmp(arg, "-noprebind") == 0 ) { | |
a61fdf0a | 2110 | warnObsolete(arg); |
d696c285 | 2111 | fPrebind = false; |
c2646906 A |
2112 | } |
2113 | else if ( strcmp(arg, "-prebind_allow_overlap") == 0 ) { | |
a61fdf0a | 2114 | warnObsolete(arg); |
c2646906 A |
2115 | } |
2116 | else if ( strcmp(arg, "-prebind_all_twolevel_modules") == 0 ) { | |
a61fdf0a | 2117 | warnObsolete(arg); |
c2646906 A |
2118 | } |
2119 | else if ( strcmp(arg, "-noprebind_all_twolevel_modules") == 0 ) { | |
a61fdf0a | 2120 | warnObsolete(arg); |
c2646906 A |
2121 | } |
2122 | else if ( strcmp(arg, "-nofixprebinding") == 0 ) { | |
a61fdf0a | 2123 | warnObsolete(arg); |
c2646906 | 2124 | } |
d696c285 A |
2125 | // This should probably be deprecated when we respect -L and -F |
2126 | // when searching for libraries. | |
c2646906 | 2127 | else if ( strcmp(arg, "-dylib_file") == 0 ) { |
ebf6f434 A |
2128 | // ignore for snapshot because a stub dylib will be created in the snapshot |
2129 | snapshotArgCount = 0; | |
a61fdf0a | 2130 | addDylibOverride(argv[++i]); |
c2646906 | 2131 | } |
a61fdf0a | 2132 | // What to expand @executable_path to if found in dependent dylibs |
c2646906 | 2133 | else if ( strcmp(arg, "-executable_path") == 0 ) { |
d696c285 A |
2134 | fExecutablePath = argv[++i]; |
2135 | if ( (fExecutablePath == NULL) || (fExecutablePath[0] == '-') ) | |
2136 | throw "-executable_path missing <path>"; | |
a61fdf0a A |
2137 | // if a directory was passed, add / to end |
2138 | // <rdar://problem/5171880> ld64 can't find @executable _path relative dylibs from our umbrella frameworks | |
2139 | struct stat statBuffer; | |
2140 | if ( stat(fExecutablePath, &statBuffer) == 0 ) { | |
2141 | if ( (statBuffer.st_mode & S_IFMT) == S_IFDIR ) { | |
2142 | char* pathWithSlash = new char[strlen(fExecutablePath)+2]; | |
2143 | strcpy(pathWithSlash, fExecutablePath); | |
2144 | strcat(pathWithSlash, "/"); | |
2145 | fExecutablePath = pathWithSlash; | |
2146 | } | |
2147 | } | |
c2646906 | 2148 | } |
d696c285 | 2149 | // Aligns all segments to the power of 2 boundary specified. |
c2646906 | 2150 | else if ( strcmp(arg, "-segalign") == 0 ) { |
55e3d2f6 A |
2151 | const char* size = argv[++i]; |
2152 | if ( size == NULL ) | |
2153 | throw "-segalign missing <size>"; | |
2154 | fSegmentAlignment = parseAddress(size); | |
2155 | uint8_t alignment = (uint8_t)__builtin_ctz(fSegmentAlignment); | |
2156 | uint32_t p2aligned = (1 << alignment); | |
2157 | if ( p2aligned != fSegmentAlignment ) { | |
2158 | warning("alignment for -segalign %s is not a power of two, using 0x%X", size, p2aligned); | |
2159 | fSegmentAlignment = p2aligned; | |
2160 | } | |
c2646906 | 2161 | } |
d696c285 A |
2162 | // Puts a specified segment at a particular address that must |
2163 | // be a multiple of the segment alignment. | |
c2646906 | 2164 | else if ( strcmp(arg, "-segaddr") == 0 ) { |
a61fdf0a A |
2165 | SegmentStart seg; |
2166 | seg.name = argv[++i]; | |
2167 | if ( (seg.name == NULL) || (argv[i+1] == NULL) ) | |
2168 | throw "-segaddr missing segName Adddress"; | |
2169 | seg.address = parseAddress(argv[++i]); | |
b2fa67a8 A |
2170 | uint64_t temp = ((seg.address+fSegmentAlignment-1) & (-fSegmentAlignment)); |
2171 | if ( seg.address != temp ) | |
2172 | warning("-segaddr %s not %lld byte aligned", seg.name, fSegmentAlignment); | |
a61fdf0a | 2173 | fCustomSegmentAddresses.push_back(seg); |
c2646906 | 2174 | } |
d696c285 | 2175 | // ??? Deprecate when we deprecate split-seg. |
c2646906 | 2176 | else if ( strcmp(arg, "-segs_read_only_addr") == 0 ) { |
a61fdf0a | 2177 | fBaseAddress = parseAddress(argv[++i]); |
c2646906 | 2178 | } |
d696c285 | 2179 | // ??? Deprecate when we deprecate split-seg. |
c2646906 | 2180 | else if ( strcmp(arg, "-segs_read_write_addr") == 0 ) { |
a61fdf0a A |
2181 | fBaseWritableAddress = parseAddress(argv[++i]); |
2182 | fSplitSegs = true; | |
c2646906 | 2183 | } |
d696c285 | 2184 | // ??? Deprecate when we get rid of basing at build time. |
c2646906 | 2185 | else if ( strcmp(arg, "-seg_addr_table") == 0 ) { |
ebf6f434 | 2186 | snapshotFileArgIndex = 1; |
a61fdf0a A |
2187 | const char* name = argv[++i]; |
2188 | if ( name == NULL ) | |
2189 | throw "-seg_addr_table missing argument"; | |
2190 | fSegAddrTablePath = name; | |
c2646906 A |
2191 | } |
2192 | else if ( strcmp(arg, "-seg_addr_table_filename") == 0 ) { | |
a61fdf0a | 2193 | warnObsolete(arg); |
d696c285 | 2194 | ++i; |
c2646906 A |
2195 | } |
2196 | else if ( strcmp(arg, "-segprot") == 0 ) { | |
a61fdf0a A |
2197 | SegmentProtect seg; |
2198 | seg.name = argv[++i]; | |
2199 | if ( (seg.name == NULL) || (argv[i+1] == NULL) || (argv[i+2] == NULL) ) | |
2200 | throw "-segprot missing segName max-prot init-prot"; | |
2201 | seg.max = parseProtection(argv[++i]); | |
2202 | seg.init = parseProtection(argv[++i]); | |
2203 | fCustomSegmentProtections.push_back(seg); | |
c2646906 A |
2204 | } |
2205 | else if ( strcmp(arg, "-pagezero_size") == 0 ) { | |
a61fdf0a A |
2206 | const char* size = argv[++i]; |
2207 | if ( size == NULL ) | |
2208 | throw "-pagezero_size missing <size>"; | |
2209 | fZeroPageSize = parseAddress(size); | |
69a49097 | 2210 | uint64_t temp = fZeroPageSize & (-4096); // page align |
2f2f92e4 A |
2211 | if ( (fZeroPageSize != temp) ) |
2212 | warning("-pagezero_size not page aligned, rounding down"); | |
69a49097 | 2213 | fZeroPageSize = temp; |
c2646906 A |
2214 | } |
2215 | else if ( strcmp(arg, "-stack_addr") == 0 ) { | |
a61fdf0a A |
2216 | const char* address = argv[++i]; |
2217 | if ( address == NULL ) | |
2218 | throw "-stack_addr missing <address>"; | |
2219 | fStackAddr = parseAddress(address); | |
c2646906 A |
2220 | } |
2221 | else if ( strcmp(arg, "-stack_size") == 0 ) { | |
a61fdf0a A |
2222 | const char* size = argv[++i]; |
2223 | if ( size == NULL ) | |
2224 | throw "-stack_size missing <address>"; | |
2225 | fStackSize = parseAddress(size); | |
2226 | uint64_t temp = fStackSize & (-4096); // page align | |
2f2f92e4 A |
2227 | if ( (fStackSize != temp) ) |
2228 | warning("-stack_size not page aligned, rounding down"); | |
c2646906 | 2229 | } |
d696c285 A |
2230 | else if ( strcmp(arg, "-allow_stack_execute") == 0 ) { |
2231 | fExecutableStack = true; | |
2232 | } | |
a645023d A |
2233 | else if ( strcmp(arg, "-allow_heap_execute") == 0 ) { |
2234 | fDisableNonExecutableHeap = true; | |
2235 | } | |
c2646906 | 2236 | else if ( strcmp(arg, "-sectalign") == 0 ) { |
a61fdf0a A |
2237 | if ( (argv[i+1]==NULL) || (argv[i+2]==NULL) || (argv[i+3]==NULL) ) |
2238 | throw "-sectalign missing <segment> <section> <file-path>"; | |
c2646906 A |
2239 | addSectionAlignment(argv[i+1], argv[i+2], argv[i+3]); |
2240 | i += 3; | |
2241 | } | |
2242 | else if ( strcmp(arg, "-sectorder_detail") == 0 ) { | |
a61fdf0a | 2243 | warnObsolete(arg); |
c2646906 A |
2244 | } |
2245 | else if ( strcmp(arg, "-sectobjectsymbols") == 0 ) { | |
a61fdf0a | 2246 | warnObsolete(arg); |
c2646906 A |
2247 | i += 2; |
2248 | } | |
2249 | else if ( strcmp(arg, "-bundle_loader") == 0 ) { | |
ebf6f434 | 2250 | snapshotFileArgIndex = 1; |
69a49097 A |
2251 | fBundleLoader = argv[++i]; |
2252 | if ( (fBundleLoader == NULL) || (fBundleLoader[0] == '-') ) | |
2253 | throw "-bundle_loader missing <path>"; | |
2254 | FileInfo info = findFile(fBundleLoader); | |
ebf6f434 | 2255 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
69a49097 A |
2256 | info.options.fBundleLoader = true; |
2257 | fInputFiles.push_back(info); | |
c2646906 A |
2258 | } |
2259 | else if ( strcmp(arg, "-private_bundle") == 0 ) { | |
a61fdf0a | 2260 | warnObsolete(arg); |
c2646906 A |
2261 | } |
2262 | else if ( strcmp(arg, "-twolevel_namespace_hints") == 0 ) { | |
2263 | // FIX FIX | |
2264 | } | |
d696c285 | 2265 | // Use this flag to set default behavior for deployement targets. |
c7f24d34 | 2266 | else if ( strcmp(arg, "-macosx_version_min") == 0 ) { |
2f2f92e4 A |
2267 | setMacOSXVersionMin(argv[++i]); |
2268 | } | |
afe874b1 A |
2269 | else if ( (strcmp(arg, "-ios_version_min") == 0) || (strcmp(arg, "-iphoneos_version_min") == 0) ) { |
2270 | setIOSVersionMin(argv[++i]); | |
2271 | } | |
2272 | else if ( strcmp(arg, "-ios_simulator_version_min") == 0 ) { | |
2273 | setIOSVersionMin(argv[++i]); | |
c7f24d34 | 2274 | } |
c2646906 | 2275 | else if ( strcmp(arg, "-multiply_defined") == 0 ) { |
a61fdf0a A |
2276 | //warnObsolete(arg); |
2277 | ++i; | |
c2646906 A |
2278 | } |
2279 | else if ( strcmp(arg, "-multiply_defined_unused") == 0 ) { | |
a61fdf0a A |
2280 | warnObsolete(arg); |
2281 | ++i; | |
c2646906 A |
2282 | } |
2283 | else if ( strcmp(arg, "-nomultidefs") == 0 ) { | |
a61fdf0a | 2284 | warnObsolete(arg); |
c2646906 | 2285 | } |
d696c285 A |
2286 | // Display each file in which the argument symbol appears and whether |
2287 | // the file defines or references it. This option takes an argument | |
2288 | // as -y<symbol> note that there is no space. | |
2289 | else if ( strncmp(arg, "-y", 2) == 0 ) { | |
a61fdf0a | 2290 | warnObsolete("-y"); |
c2646906 | 2291 | } |
d696c285 | 2292 | // Same output as -y, but output <arg> number of undefined symbols only. |
c2646906 | 2293 | else if ( strcmp(arg, "-Y") == 0 ) { |
a61fdf0a A |
2294 | //warnObsolete(arg); |
2295 | ++i; | |
c2646906 | 2296 | } |
d696c285 | 2297 | // This option affects all objects linked into the final result. |
c2646906 | 2298 | else if ( strcmp(arg, "-m") == 0 ) { |
a61fdf0a | 2299 | warnObsolete(arg); |
c2646906 | 2300 | } |
69a49097 | 2301 | else if ( (strcmp(arg, "-why_load") == 0) || (strcmp(arg, "-whyload") == 0) ) { |
a645023d | 2302 | fWhyLoad = true; |
69a49097 A |
2303 | } |
2304 | else if ( strcmp(arg, "-why_live") == 0 ) { | |
2305 | const char* name = argv[++i]; | |
2306 | if ( name == NULL ) | |
2307 | throw "-why_live missing symbol name argument"; | |
2308 | fWhyLive.insert(name); | |
c2646906 A |
2309 | } |
2310 | else if ( strcmp(arg, "-u") == 0 ) { | |
2311 | const char* name = argv[++i]; | |
2312 | if ( name == NULL ) | |
2313 | throw "-u missing argument"; | |
2314 | fInitialUndefines.push_back(name); | |
2315 | } | |
c2646906 | 2316 | else if ( strcmp(arg, "-U") == 0 ) { |
a61fdf0a A |
2317 | const char* name = argv[++i]; |
2318 | if ( name == NULL ) | |
2319 | throw "-U missing argument"; | |
2320 | fAllowedUndefined.insert(name); | |
c2646906 A |
2321 | } |
2322 | else if ( strcmp(arg, "-s") == 0 ) { | |
a61fdf0a A |
2323 | warnObsolete(arg); |
2324 | fLocalSymbolHandling = kLocalSymbolsNone; | |
a645023d | 2325 | fDebugInfoStripping = Options::kDebugInfoNone; |
c2646906 A |
2326 | } |
2327 | else if ( strcmp(arg, "-x") == 0 ) { | |
a61fdf0a | 2328 | fLocalSymbolHandling = kLocalSymbolsNone; |
c2646906 A |
2329 | } |
2330 | else if ( strcmp(arg, "-S") == 0 ) { | |
a645023d | 2331 | fDebugInfoStripping = Options::kDebugInfoNone; |
c2646906 A |
2332 | } |
2333 | else if ( strcmp(arg, "-X") == 0 ) { | |
a61fdf0a | 2334 | warnObsolete(arg); |
c2646906 A |
2335 | } |
2336 | else if ( strcmp(arg, "-Si") == 0 ) { | |
a61fdf0a | 2337 | warnObsolete(arg); |
a645023d | 2338 | fDebugInfoStripping = Options::kDebugInfoFull; |
c2646906 A |
2339 | } |
2340 | else if ( strcmp(arg, "-b") == 0 ) { | |
a61fdf0a | 2341 | warnObsolete(arg); |
c2646906 A |
2342 | } |
2343 | else if ( strcmp(arg, "-Sn") == 0 ) { | |
a61fdf0a | 2344 | warnObsolete(arg); |
a645023d | 2345 | fDebugInfoStripping = Options::kDebugInfoFull; |
d696c285 A |
2346 | } |
2347 | else if ( strcmp(arg, "-Sp") == 0 ) { | |
a61fdf0a | 2348 | warnObsolete(arg); |
c2646906 A |
2349 | } |
2350 | else if ( strcmp(arg, "-dead_strip") == 0 ) { | |
a645023d | 2351 | fDeadStrip = true; |
d696c285 A |
2352 | } |
2353 | else if ( strcmp(arg, "-no_dead_strip_inits_and_terms") == 0 ) { | |
a645023d | 2354 | fDeadStrip = true; |
c2646906 | 2355 | } |
c2646906 | 2356 | else if ( strcmp(arg, "-w") == 0 ) { |
a61fdf0a | 2357 | // previously handled by buildSearchPaths() |
c2646906 | 2358 | } |
b2fa67a8 A |
2359 | else if ( strcmp(arg, "-fatal_warnings") == 0 ) { |
2360 | // previously handled by buildSearchPaths() | |
2361 | } | |
c2646906 | 2362 | else if ( strcmp(arg, "-arch_errors_fatal") == 0 ) { |
55e3d2f6 | 2363 | fErrorOnOtherArchFiles = true; |
c2646906 A |
2364 | } |
2365 | else if ( strcmp(arg, "-M") == 0 ) { | |
2366 | // FIX FIX | |
2367 | } | |
c2646906 A |
2368 | else if ( strcmp(arg, "-headerpad") == 0 ) { |
2369 | const char* size = argv[++i]; | |
2370 | if ( size == NULL ) | |
2371 | throw "-headerpad missing argument"; | |
2372 | fMinimumHeaderPad = parseAddress(size); | |
2373 | } | |
2374 | else if ( strcmp(arg, "-headerpad_max_install_names") == 0 ) { | |
a61fdf0a | 2375 | fMaxMinimumHeaderPad = true; |
c2646906 A |
2376 | } |
2377 | else if ( strcmp(arg, "-t") == 0 ) { | |
a645023d | 2378 | fLogAllFiles = true; |
a61fdf0a A |
2379 | } |
2380 | else if ( strcmp(arg, "-whatsloaded") == 0 ) { | |
a645023d | 2381 | fLogObjectFiles = true; |
c2646906 A |
2382 | } |
2383 | else if ( strcmp(arg, "-A") == 0 ) { | |
a61fdf0a | 2384 | warnObsolete(arg); |
d696c285 | 2385 | ++i; |
c2646906 A |
2386 | } |
2387 | else if ( strcmp(arg, "-umbrella") == 0 ) { | |
2388 | const char* name = argv[++i]; | |
2389 | if ( name == NULL ) | |
2390 | throw "-umbrella missing argument"; | |
2391 | fUmbrellaName = name; | |
2392 | } | |
2393 | else if ( strcmp(arg, "-allowable_client") == 0 ) { | |
d696c285 A |
2394 | const char* name = argv[++i]; |
2395 | ||
2396 | if ( name == NULL ) | |
2397 | throw "-allowable_client missing argument"; | |
2398 | ||
2399 | fAllowableClients.push_back(name); | |
c2646906 A |
2400 | } |
2401 | else if ( strcmp(arg, "-client_name") == 0 ) { | |
d696c285 A |
2402 | const char* name = argv[++i]; |
2403 | ||
2404 | if ( name == NULL ) | |
2405 | throw "-client_name missing argument"; | |
2406 | ||
2407 | fClientName = name; | |
c2646906 A |
2408 | } |
2409 | else if ( strcmp(arg, "-sub_umbrella") == 0 ) { | |
2410 | const char* name = argv[++i]; | |
2411 | if ( name == NULL ) | |
2412 | throw "-sub_umbrella missing argument"; | |
2413 | fSubUmbellas.push_back(name); | |
2414 | } | |
2415 | else if ( strcmp(arg, "-sub_library") == 0 ) { | |
2416 | const char* name = argv[++i]; | |
2417 | if ( name == NULL ) | |
2418 | throw "-sub_library missing argument"; | |
2419 | fSubLibraries.push_back(name); | |
2420 | } | |
2421 | else if ( strcmp(arg, "-init") == 0 ) { | |
2422 | const char* name = argv[++i]; | |
2423 | if ( name == NULL ) | |
2424 | throw "-init missing argument"; | |
2425 | fInitFunctionName = name; | |
2426 | } | |
d696c285 A |
2427 | else if ( strcmp(arg, "-dot") == 0 ) { |
2428 | const char* name = argv[++i]; | |
2429 | if ( name == NULL ) | |
2430 | throw "-dot missing argument"; | |
2431 | fDotOutputFile = name; | |
2432 | } | |
c2646906 A |
2433 | else if ( strcmp(arg, "-warn_commons") == 0 ) { |
2434 | fWarnCommons = true; | |
2435 | } | |
2436 | else if ( strcmp(arg, "-commons") == 0 ) { | |
2437 | fCommonsMode = parseCommonsTreatment(argv[++i]); | |
2438 | } | |
d696c285 A |
2439 | else if ( strcmp(arg, "-keep_relocs") == 0 ) { |
2440 | fKeepRelocations = true; | |
2441 | } | |
2442 | else if ( strcmp(arg, "-warn_stabs") == 0 ) { | |
2443 | fWarnStabs = true; | |
2444 | } | |
2445 | else if ( strcmp(arg, "-pause") == 0 ) { | |
2446 | fPause = true; | |
2447 | } | |
2448 | else if ( strcmp(arg, "-print_statistics") == 0 ) { | |
2449 | fStatistics = true; | |
2450 | } | |
69a49097 | 2451 | else if ( strcmp(arg, "-d") == 0 ) { |
a645023d | 2452 | fMakeTentativeDefinitionsReal = true; |
69a49097 | 2453 | } |
6e880c60 A |
2454 | else if ( strcmp(arg, "-v") == 0 ) { |
2455 | // previously handled by buildSearchPaths() | |
2456 | } | |
2457 | else if ( strcmp(arg, "-Z") == 0 ) { | |
2458 | // previously handled by buildSearchPaths() | |
2459 | } | |
2460 | else if ( strcmp(arg, "-syslibroot") == 0 ) { | |
ebf6f434 | 2461 | snapshotArgCount = 0; |
6e880c60 A |
2462 | ++i; |
2463 | // previously handled by buildSearchPaths() | |
2464 | } | |
d696c285 | 2465 | else if ( strcmp(arg, "-no_uuid") == 0 ) { |
a61fdf0a A |
2466 | fUUIDMode = kUUIDNone; |
2467 | } | |
2468 | else if ( strcmp(arg, "-random_uuid") == 0 ) { | |
2469 | fUUIDMode = kUUIDRandom; | |
2470 | } | |
2471 | else if ( strcmp(arg, "-dtrace") == 0 ) { | |
ebf6f434 | 2472 | snapshotFileArgIndex = 1; |
a61fdf0a A |
2473 | const char* name = argv[++i]; |
2474 | if ( name == NULL ) | |
2475 | throw "-dtrace missing argument"; | |
2476 | fDtraceScriptName = name; | |
2477 | } | |
2478 | else if ( strcmp(arg, "-root_safe") == 0 ) { | |
a645023d | 2479 | fRootSafe = true; |
a61fdf0a A |
2480 | } |
2481 | else if ( strcmp(arg, "-setuid_safe") == 0 ) { | |
a645023d | 2482 | fSetuidSafe = true; |
a61fdf0a A |
2483 | } |
2484 | else if ( strcmp(arg, "-alias") == 0 ) { | |
a645023d | 2485 | Options::AliasPair pair; |
a61fdf0a A |
2486 | pair.realName = argv[++i]; |
2487 | if ( pair.realName == NULL ) | |
2488 | throw "missing argument to -alias"; | |
2489 | pair.alias = argv[++i]; | |
2490 | if ( pair.alias == NULL ) | |
2491 | throw "missing argument to -alias"; | |
a645023d | 2492 | fAliases.push_back(pair); |
a61fdf0a A |
2493 | } |
2494 | else if ( strcmp(arg, "-alias_list") == 0 ) { | |
ebf6f434 | 2495 | snapshotFileArgIndex = 1; |
a61fdf0a | 2496 | parseAliasFile(argv[++i]); |
d696c285 A |
2497 | } |
2498 | // put this last so that it does not interfer with other options starting with 'i' | |
2499 | else if ( strncmp(arg, "-i", 2) == 0 ) { | |
a61fdf0a A |
2500 | const char* colon = strchr(arg, ':'); |
2501 | if ( colon == NULL ) | |
2502 | throwf("unknown option: %s", arg); | |
a645023d | 2503 | Options::AliasPair pair; |
a61fdf0a A |
2504 | char* temp = new char[colon-arg]; |
2505 | strlcpy(temp, &arg[2], colon-arg-1); | |
2506 | pair.realName = &colon[1]; | |
2507 | pair.alias = temp; | |
a645023d | 2508 | fAliases.push_back(pair); |
a61fdf0a A |
2509 | } |
2510 | else if ( strcmp(arg, "-save-temps") == 0 ) { | |
2511 | fSaveTempFiles = true; | |
2512 | } | |
2513 | else if ( strcmp(arg, "-rpath") == 0 ) { | |
2514 | const char* path = argv[++i]; | |
2515 | if ( path == NULL ) | |
2516 | throw "missing argument to -rpath"; | |
2517 | fRPaths.push_back(path); | |
2518 | } | |
2519 | else if ( strcmp(arg, "-read_only_stubs") == 0 ) { | |
2520 | fReadOnlyx86Stubs = true; | |
2521 | } | |
2f2f92e4 | 2522 | else if ( strcmp(arg, "-slow_stubs") == 0 ) { |
55e3d2f6 | 2523 | warnObsolete(arg); |
2f2f92e4 | 2524 | } |
a61fdf0a A |
2525 | else if ( strcmp(arg, "-map") == 0 ) { |
2526 | fMapPath = argv[++i]; | |
2527 | if ( fMapPath == NULL ) | |
2528 | throw "missing argument to -map"; | |
2529 | } | |
2530 | else if ( strcmp(arg, "-pie") == 0 ) { | |
2531 | fPositionIndependentExecutable = true; | |
a645023d | 2532 | fPIEOnCommandLine = true; |
a61fdf0a | 2533 | } |
d9246299 A |
2534 | else if ( strcmp(arg, "-no_pie") == 0 ) { |
2535 | fDisablePositionIndependentExecutable = true; | |
2536 | } | |
a61fdf0a | 2537 | else if ( strncmp(arg, "-reexport-l", 11) == 0 ) { |
ebf6f434 A |
2538 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2539 | snapshotArgCount = 0; | |
2f2f92e4 | 2540 | FileInfo info = findLibrary(&arg[11], true); |
a61fdf0a | 2541 | info.options.fReExport = true; |
ebf6f434 | 2542 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a A |
2543 | addLibrary(info); |
2544 | } | |
2545 | else if ( strcmp(arg, "-reexport_library") == 0 ) { | |
ebf6f434 A |
2546 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2547 | snapshotArgCount = 0; | |
a61fdf0a A |
2548 | FileInfo info = findFile(argv[++i]); |
2549 | info.options.fReExport = true; | |
ebf6f434 | 2550 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a A |
2551 | addLibrary(info); |
2552 | } | |
2553 | else if ( strcmp(arg, "-reexport_framework") == 0 ) { | |
ebf6f434 A |
2554 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2555 | snapshotArgCount = 0; | |
a61fdf0a A |
2556 | FileInfo info = findFramework(argv[++i]); |
2557 | info.options.fReExport = true; | |
ebf6f434 | 2558 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a A |
2559 | addLibrary(info); |
2560 | } | |
a645023d | 2561 | else if ( strncmp(arg, "-upward-l", 9) == 0 ) { |
ebf6f434 A |
2562 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2563 | snapshotArgCount = 0; | |
a645023d A |
2564 | FileInfo info = findLibrary(&arg[9], true); |
2565 | info.options.fUpward = true; | |
ebf6f434 | 2566 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a645023d A |
2567 | addLibrary(info); |
2568 | } | |
2569 | else if ( strcmp(arg, "-upward_library") == 0 ) { | |
ebf6f434 A |
2570 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2571 | snapshotArgCount = 0; | |
a645023d A |
2572 | FileInfo info = findFile(argv[++i]); |
2573 | info.options.fUpward = true; | |
ebf6f434 | 2574 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a645023d A |
2575 | addLibrary(info); |
2576 | } | |
2577 | else if ( strcmp(arg, "-upward_framework") == 0 ) { | |
ebf6f434 A |
2578 | // SNAPSHOT FIXME: what should we do for link snapshots? (ignore for now) |
2579 | snapshotArgCount = 0; | |
a645023d A |
2580 | FileInfo info = findFramework(argv[++i]); |
2581 | info.options.fUpward = true; | |
ebf6f434 | 2582 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a645023d A |
2583 | addLibrary(info); |
2584 | } | |
a61fdf0a A |
2585 | else if ( strcmp(arg, "-dead_strip_dylibs") == 0 ) { |
2586 | fDeadStripDylibs = true; | |
2587 | } | |
2f2f92e4 | 2588 | else if ( strcmp(arg, "-no_implicit_dylibs") == 0 ) { |
a645023d | 2589 | fImplicitlyLinkPublicDylibs = false; |
2f2f92e4 | 2590 | } |
a61fdf0a A |
2591 | else if ( strcmp(arg, "-new_linker") == 0 ) { |
2592 | // ignore | |
d696c285 | 2593 | } |
2f2f92e4 A |
2594 | else if ( strcmp(arg, "-no_encryption") == 0 ) { |
2595 | fEncryptable = false; | |
2596 | } | |
55e3d2f6 | 2597 | else if ( strcmp(arg, "-no_compact_unwind") == 0 ) { |
a645023d | 2598 | fAddCompactUnwindEncoding = false; |
55e3d2f6 A |
2599 | } |
2600 | else if ( strcmp(arg, "-mllvm") == 0 ) { | |
2601 | const char* opts = argv[++i]; | |
2602 | if ( opts == NULL ) | |
2603 | throw "missing argument to -mllvm"; | |
2604 | fLLVMOptions.push_back(opts); | |
2605 | } | |
2606 | else if ( strcmp(arg, "-no_order_inits") == 0 ) { | |
a645023d | 2607 | fAutoOrderInitializers = false; |
55e3d2f6 A |
2608 | } |
2609 | else if ( strcmp(arg, "-no_order_data") == 0 ) { | |
2610 | fOrderData = false; | |
2611 | } | |
2612 | else if ( strcmp(arg, "-seg_page_size") == 0 ) { | |
2613 | SegmentSize seg; | |
2614 | seg.name = argv[++i]; | |
2615 | if ( (seg.name == NULL) || (argv[i+1] == NULL) ) | |
2616 | throw "-seg_page_size missing segName Adddress"; | |
2617 | seg.size = parseAddress(argv[++i]); | |
2618 | uint64_t temp = seg.size & (-4096); // page align | |
2619 | if ( (seg.size != temp) ) | |
2620 | warning("-seg_page_size %s not 4K aligned, rounding down", seg.name); | |
2621 | fCustomSegmentSizes.push_back(seg); | |
2622 | } | |
2623 | else if ( strcmp(arg, "-mark_dead_strippable_dylib") == 0 ) { | |
2624 | fMarkDeadStrippableDylib = true; | |
2625 | } | |
2626 | else if ( strcmp(arg, "-exported_symbols_order") == 0 ) { | |
ebf6f434 | 2627 | snapshotFileArgIndex = 1; |
55e3d2f6 A |
2628 | loadSymbolOrderFile(argv[++i], fExportSymbolsOrder); |
2629 | } | |
2630 | else if ( strcmp(arg, "-no_compact_linkedit") == 0 ) { | |
afe874b1 | 2631 | warnObsolete("-no_compact_linkedit"); |
55e3d2f6 A |
2632 | } |
2633 | else if ( strcmp(arg, "-no_eh_labels") == 0 ) { | |
a645023d | 2634 | fNoEHLabels = true; |
55e3d2f6 A |
2635 | } |
2636 | else if ( strcmp(arg, "-warn_compact_unwind") == 0 ) { | |
a645023d | 2637 | fWarnCompactUnwind = true; |
55e3d2f6 A |
2638 | } |
2639 | else if ( strcmp(arg, "-allow_sub_type_mismatches") == 0 ) { | |
2640 | fAllowCpuSubtypeMismatches = true; | |
2641 | } | |
4be885f6 | 2642 | else if ( strcmp(arg, "-no_zero_fill_sections") == 0 ) { |
a645023d | 2643 | fOptimizeZeroFill = false; |
4be885f6 | 2644 | } |
afe874b1 A |
2645 | else if ( strcmp(arg, "-merge_zero_fill_sections") == 0 ) { |
2646 | fMergeZeroFill = true; | |
2647 | } | |
d9246299 A |
2648 | else if ( strcmp(arg, "-objc_abi_version") == 0 ) { |
2649 | const char* version = argv[++i]; | |
2650 | if ( version == NULL ) | |
2651 | throw "-objc_abi_version missing version number"; | |
a645023d A |
2652 | if ( strcmp(version, "2") == 0 ) { |
2653 | fObjCABIVersion1Override = false; | |
2654 | fObjCABIVersion2Override = true; | |
2655 | } | |
2656 | else if ( strcmp(version, "1") == 0 ) { | |
2657 | fObjCABIVersion1Override = true; | |
2658 | fObjCABIVersion2Override = false; | |
2659 | } | |
d9246299 A |
2660 | else |
2661 | warning("ignoring unrecognized argument (%s) to -objc_abi_version", version); | |
2662 | } | |
a645023d A |
2663 | else if ( strcmp(arg, "-warn_weak_exports") == 0 ) { |
2664 | fWarnWeakExports = true; | |
2665 | } | |
2666 | else if ( strcmp(arg, "-objc_gc_compaction") == 0 ) { | |
2667 | fObjcGcCompaction = true; | |
2668 | } | |
2669 | else if ( strcmp(arg, "-objc_gc") == 0 ) { | |
2670 | fObjCGc = true; | |
2671 | if ( fObjCGcOnly ) { | |
2672 | warning("-objc_gc overriding -objc_gc_only"); | |
2673 | fObjCGcOnly = false; | |
2674 | } | |
2675 | } | |
2676 | else if ( strcmp(arg, "-objc_gc_only") == 0 ) { | |
2677 | fObjCGcOnly = true; | |
2678 | if ( fObjCGc ) { | |
2679 | warning("-objc_gc_only overriding -objc_gc"); | |
2680 | fObjCGc = false; | |
2681 | } | |
2682 | } | |
07feaf2c | 2683 | else if ( strcmp(arg, "-demangle") == 0 ) { |
a645023d A |
2684 | fDemangle = true; |
2685 | } | |
2686 | else if ( strcmp(arg, "-version_load_command") == 0 ) { | |
afe874b1 A |
2687 | fVersionLoadCommandForcedOn = true; |
2688 | fVersionLoadCommandForcedOff = false; | |
a645023d A |
2689 | } |
2690 | else if ( strcmp(arg, "-no_version_load_command") == 0 ) { | |
afe874b1 A |
2691 | fVersionLoadCommandForcedOff = true; |
2692 | fVersionLoadCommandForcedOn = false; | |
a645023d A |
2693 | } |
2694 | else if ( strcmp(arg, "-function_starts") == 0 ) { | |
afe874b1 A |
2695 | fFunctionStartsForcedOn = true; |
2696 | fFunctionStartsForcedOff = false; | |
a645023d A |
2697 | } |
2698 | else if ( strcmp(arg, "-no_function_starts") == 0 ) { | |
afe874b1 A |
2699 | fFunctionStartsForcedOff = true; |
2700 | fFunctionStartsForcedOn = false; | |
a645023d | 2701 | } |
ebf6f434 | 2702 | else if ( strcmp(arg, "-no_data_in_code_info") == 0 ) { |
b1f7435d A |
2703 | fDataInCodeInfoLoadCommandForcedOff = true; |
2704 | fDataInCodeInfoLoadCommandForcedOn = false; | |
ebf6f434 A |
2705 | } |
2706 | else if ( strcmp(arg, "-data_in_code_info") == 0 ) { | |
b1f7435d A |
2707 | fDataInCodeInfoLoadCommandForcedOn = true; |
2708 | fDataInCodeInfoLoadCommandForcedOff = false; | |
ebf6f434 | 2709 | } |
a645023d A |
2710 | else if ( strcmp(arg, "-object_path_lto") == 0 ) { |
2711 | fTempLtoObjectPath = argv[++i]; | |
2712 | if ( fTempLtoObjectPath == NULL ) | |
2713 | throw "missing argument to -object_path_lto"; | |
2714 | } | |
2715 | else if ( strcmp(arg, "-no_objc_category_merging") == 0 ) { | |
2716 | fObjcCategoryMerging = false; | |
2717 | } | |
2718 | else if ( strcmp(arg, "-force_symbols_weak_list") == 0 ) { | |
ebf6f434 | 2719 | snapshotFileArgIndex = 1; |
a645023d A |
2720 | loadExportFile(argv[++i], "-force_symbols_weak_list", fForceWeakSymbols); |
2721 | } | |
2722 | else if ( strcmp(arg, "-force_symbols_not_weak_list") == 0 ) { | |
ebf6f434 | 2723 | snapshotFileArgIndex = 1; |
a645023d A |
2724 | loadExportFile(argv[++i], "-force_symbols_not_weak_list", fForceNotWeakSymbols); |
2725 | } | |
2726 | else if ( strcmp(arg, "-force_symbol_weak") == 0 ) { | |
2727 | const char* symbol = argv[++i]; | |
2728 | if ( symbol == NULL ) | |
2729 | throw "-force_symbol_weak missing <symbol>"; | |
2730 | fForceWeakSymbols.insert(symbol); | |
2731 | } | |
2732 | else if ( strcmp(arg, "-force_symbol_not_weak") == 0 ) { | |
2733 | const char* symbol = argv[++i]; | |
2734 | if ( symbol == NULL ) | |
2735 | throw "-force_symbol_not_weak missing <symbol>"; | |
2736 | fForceNotWeakSymbols.insert(symbol); | |
2737 | } | |
2738 | else if ( strcmp(arg, "-reexported_symbols_list") == 0 ) { | |
ebf6f434 | 2739 | snapshotFileArgIndex = 1; |
a645023d A |
2740 | if ( fExportMode == kExportSome ) |
2741 | throw "can't use -exported_symbols_list and -reexported_symbols_list"; | |
2742 | loadExportFile(argv[++i], "-reexported_symbols_list", fReExportSymbols); | |
2743 | } | |
2744 | else if ( strcmp(arg, "-dyld_env") == 0 ) { | |
2745 | const char* envarg = argv[++i]; | |
2746 | if ( envarg == NULL ) | |
2747 | throw "-dyld_env missing ENV=VALUE"; | |
2748 | if ( strchr(envarg, '=') == NULL ) | |
2749 | throw "-dyld_env missing ENV=VALUE"; | |
2750 | fDyldEnvironExtras.push_back(envarg); | |
07feaf2c | 2751 | } |
afe874b1 A |
2752 | else if ( strcmp(arg, "-page_align_data_atoms") == 0 ) { |
2753 | fPageAlignDataAtoms = true; | |
ebf6f434 A |
2754 | } |
2755 | else if (strcmp(arg, "-debug_snapshot") == 0) { | |
2756 | fLinkSnapshot.setSnapshotMode(Snapshot::SNAPSHOT_DEBUG); | |
2757 | fSnapshotRequested = true; | |
2758 | } | |
2759 | else if ( strcmp(arg, "-new_main") == 0 ) { | |
2760 | fEntryPointLoadCommandForceOn = true; | |
2761 | } | |
2762 | else if ( strcmp(arg, "-no_new_main") == 0 ) { | |
2763 | fEntryPointLoadCommandForceOff = true; | |
2764 | } | |
2765 | else if ( strcmp(arg, "-source_version") == 0 ) { | |
2766 | const char* vers = argv[++i]; | |
2767 | if ( vers == NULL ) | |
2768 | throw "-source_version missing <version>"; | |
2769 | fSourceVersion = parseVersionNumber64(vers); | |
2770 | } | |
2771 | else if ( strcmp(arg, "-add_source_version") == 0 ) { | |
2772 | fSourceVersionLoadCommandForceOn = true; | |
2773 | } | |
2774 | else if ( strcmp(arg, "-no_source_version") == 0 ) { | |
2775 | fSourceVersionLoadCommandForceOff = true; | |
2776 | } | |
2777 | else if ( strcmp(arg, "-sdk_version") == 0 ) { | |
2778 | const char* vers = argv[++i]; | |
2779 | if ( vers == NULL ) | |
2780 | throw "-sdk_version missing <version>"; | |
2781 | fSDKVersion = parseVersionNumber32(vers); | |
2782 | } | |
2783 | else if ( strcmp(arg, "-dependent_dr_info") == 0 ) { | |
2784 | fDependentDRInfoForcedOn = true; | |
2785 | } | |
2786 | else if ( strcmp(arg, "-no_dependent_dr_info") == 0 ) { | |
2787 | fDependentDRInfoForcedOff = true; | |
2788 | } | |
2789 | else if ( strcmp(arg, "-kexts_use_stubs") == 0 ) { | |
2790 | fKextsUseStubs = true; | |
afe874b1 | 2791 | } |
c2646906 | 2792 | else { |
d696c285 | 2793 | throwf("unknown option: %s", arg); |
c2646906 | 2794 | } |
ebf6f434 A |
2795 | |
2796 | if (snapshotArgCount == -1) | |
2797 | snapshotArgCount = i-snapshotArgIndex+1; | |
2798 | if (snapshotArgCount > 0) | |
2799 | fLinkSnapshot.addSnapshotLinkArg(snapshotArgIndex, snapshotArgCount, snapshotFileArgIndex); | |
c2646906 A |
2800 | } |
2801 | else { | |
a61fdf0a | 2802 | FileInfo info = findFile(arg); |
ebf6f434 | 2803 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)i); |
a61fdf0a A |
2804 | if ( strcmp(&info.path[strlen(info.path)-2], ".a") == 0 ) |
2805 | addLibrary(info); | |
2806 | else | |
2807 | fInputFiles.push_back(info); | |
c2646906 A |
2808 | } |
2809 | } | |
2f2f92e4 A |
2810 | |
2811 | // if a -lazy option was used, implicitly link in lazydylib1.o | |
2812 | if ( fUsingLazyDylibLinking ) { | |
ebf6f434 A |
2813 | FileInfo info = findLibrary("lazydylib1.o"); |
2814 | info.ordinal = ld::File::Ordinal::makeArgOrdinal((uint16_t)argc); | |
2815 | addLibrary(info); | |
2f2f92e4 | 2816 | } |
ebf6f434 A |
2817 | |
2818 | if (fSnapshotRequested) | |
2819 | fLinkSnapshot.createSnapshot(); | |
c2646906 A |
2820 | } |
2821 | ||
69a49097 A |
2822 | |
2823 | ||
d696c285 A |
2824 | // |
2825 | // -syslibroot <path> is used for SDK support. | |
6e880c60 A |
2826 | // The rule is that all search paths (both explicit and default) are |
2827 | // checked to see if they exist in the SDK. If so, that path is | |
2828 | // replaced with the sdk prefixed path. If not, that search path | |
2829 | // is used as is. If multiple -syslibroot options are specified | |
2830 | // their directory structures are logically overlayed and files | |
2831 | // from sdks specified earlier on the command line used before later ones. | |
d696c285 | 2832 | |
c2646906 A |
2833 | void Options::buildSearchPaths(int argc, const char* argv[]) |
2834 | { | |
2835 | bool addStandardLibraryDirectories = true; | |
6e880c60 A |
2836 | std::vector<const char*> libraryPaths; |
2837 | std::vector<const char*> frameworkPaths; | |
d696c285 A |
2838 | libraryPaths.reserve(10); |
2839 | frameworkPaths.reserve(10); | |
6e880c60 | 2840 | // scan through argv looking for -L, -F, -Z, and -syslibroot options |
c2646906 | 2841 | for(int i=0; i < argc; ++i) { |
c211e7c9 A |
2842 | if ( (argv[i][0] == '-') && (argv[i][1] == 'L') ) { |
2843 | const char* libSearchDir = &argv[i][2]; | |
a645023d A |
2844 | // Allow either "-L{path}" or "-L {path}". |
2845 | if (argv[i][2] == '\0') { | |
2846 | // -L {path}. Make sure there is an argument following this. | |
2847 | const char* path = argv[++i]; | |
2848 | if ( path == NULL ) | |
2849 | throw "-L missing argument"; | |
2850 | libSearchDir = path; | |
2851 | } | |
c211e7c9 A |
2852 | if ( libSearchDir[0] == '\0' ) |
2853 | throw "-L must be immediately followed by a directory path (no space)"; | |
2854 | struct stat statbuf; | |
2855 | if ( stat(libSearchDir, &statbuf) == 0 ) { | |
2856 | if ( statbuf.st_mode & S_IFDIR ) | |
2857 | libraryPaths.push_back(libSearchDir); | |
2858 | else | |
2859 | warning("path '%s' following -L not a directory", libSearchDir); | |
2860 | } | |
2861 | else { | |
a645023d | 2862 | warning("directory not found for option '-L%s'", libSearchDir); |
c211e7c9 A |
2863 | } |
2864 | } | |
2865 | else if ( (argv[i][0] == '-') && (argv[i][1] == 'F') ) { | |
2866 | const char* frameworkSearchDir = &argv[i][2]; | |
a645023d A |
2867 | // Allow either "-F{path}" or "-F {path}". |
2868 | if (argv[i][2] == '\0') { | |
2869 | // -F {path}. Make sure there is an argument following this. | |
2870 | const char* path = argv[++i]; | |
2871 | if ( path == NULL ) | |
2872 | throw "-F missing argument"; | |
2873 | frameworkSearchDir = path; | |
2874 | } | |
c211e7c9 A |
2875 | if ( frameworkSearchDir[0] == '\0' ) |
2876 | throw "-F must be immediately followed by a directory path (no space)"; | |
2877 | struct stat statbuf; | |
2878 | if ( stat(frameworkSearchDir, &statbuf) == 0 ) { | |
2879 | if ( statbuf.st_mode & S_IFDIR ) | |
2880 | frameworkPaths.push_back(frameworkSearchDir); | |
2881 | else | |
2882 | warning("path '%s' following -F not a directory", frameworkSearchDir); | |
2883 | } | |
2884 | else { | |
a645023d | 2885 | warning("directory not found for option '-F%s'", frameworkSearchDir); |
c211e7c9 A |
2886 | } |
2887 | } | |
c2646906 A |
2888 | else if ( strcmp(argv[i], "-Z") == 0 ) |
2889 | addStandardLibraryDirectories = false; | |
6e880c60 A |
2890 | else if ( strcmp(argv[i], "-v") == 0 ) { |
2891 | fVerbose = true; | |
a61fdf0a A |
2892 | extern const char ldVersionString[]; |
2893 | fprintf(stderr, "%s", ldVersionString); | |
ebf6f434 | 2894 | fprintf(stderr, "configured to support archs: %s\n", ALL_SUPPORTED_ARCHS); |
6e880c60 | 2895 | // if only -v specified, exit cleanly |
2f2f92e4 | 2896 | if ( argc == 2 ) { |
a645023d A |
2897 | const char* ltoVers = lto::version(); |
2898 | if ( ltoVers != NULL ) | |
ebf6f434 | 2899 | fprintf(stderr, "LTO support using: %s\n", ltoVers); |
6e880c60 | 2900 | exit(0); |
2f2f92e4 | 2901 | } |
6e880c60 A |
2902 | } |
2903 | else if ( strcmp(argv[i], "-syslibroot") == 0 ) { | |
2904 | const char* path = argv[++i]; | |
2905 | if ( path == NULL ) | |
2906 | throw "-syslibroot missing argument"; | |
2907 | fSDKPaths.push_back(path); | |
2908 | } | |
a61fdf0a | 2909 | else if ( strcmp(argv[i], "-search_paths_first") == 0 ) { |
a61fdf0a A |
2910 | fLibrarySearchMode = kSearchDylibAndArchiveInEachDir; |
2911 | } | |
a645023d A |
2912 | else if ( strcmp(argv[i], "-search_dylibs_first") == 0 ) { |
2913 | fLibrarySearchMode = kSearchAllDirsForDylibsThenAllDirsForArchives; | |
2914 | } | |
a61fdf0a | 2915 | else if ( strcmp(argv[i], "-w") == 0 ) { |
2f2f92e4 | 2916 | sEmitWarnings = false; |
a61fdf0a | 2917 | } |
b2fa67a8 A |
2918 | else if ( strcmp(argv[i], "-fatal_warnings") == 0 ) { |
2919 | sFatalWarnings = true; | |
2920 | } | |
c2646906 | 2921 | } |
55e3d2f6 A |
2922 | int standardLibraryPathsStartIndex = libraryPaths.size(); |
2923 | int standardFrameworkPathsStartIndex = frameworkPaths.size(); | |
c2646906 | 2924 | if ( addStandardLibraryDirectories ) { |
6e880c60 A |
2925 | libraryPaths.push_back("/usr/lib"); |
2926 | libraryPaths.push_back("/usr/local/lib"); | |
d696c285 | 2927 | |
6e880c60 | 2928 | frameworkPaths.push_back("/Library/Frameworks/"); |
6e880c60 | 2929 | frameworkPaths.push_back("/System/Library/Frameworks/"); |
55e3d2f6 | 2930 | // <rdar://problem/5433882> remove /Network/Library/Frameworks from default search path |
6e880c60 | 2931 | } |
d696c285 | 2932 | |
77cc3118 A |
2933 | // <rdar://problem/5829579> Support for configure based hacks |
2934 | // if last -syslibroot is /, then ignore all syslibroots | |
2935 | if ( fSDKPaths.size() > 0 ) { | |
2936 | if ( strcmp(fSDKPaths.back(), "/") == 0 ) { | |
2937 | fSDKPaths.clear(); | |
2938 | } | |
2939 | } | |
2940 | ||
6e880c60 | 2941 | // now merge sdk and library paths to make real search paths |
d696c285 | 2942 | fLibrarySearchPaths.reserve(libraryPaths.size()*(fSDKPaths.size()+1)); |
55e3d2f6 A |
2943 | int libIndex = 0; |
2944 | for (std::vector<const char*>::iterator it = libraryPaths.begin(); it != libraryPaths.end(); ++it, ++libIndex) { | |
6e880c60 A |
2945 | const char* libDir = *it; |
2946 | bool sdkOverride = false; | |
2947 | if ( libDir[0] == '/' ) { | |
2948 | char betterLibDir[PATH_MAX]; | |
2949 | if ( strstr(libDir, "/..") != NULL ) { | |
2950 | if ( realpath(libDir, betterLibDir) != NULL ) | |
d696c285 | 2951 | libDir = strdup(betterLibDir); |
6e880c60 A |
2952 | } |
2953 | const int libDirLen = strlen(libDir); | |
2954 | for (std::vector<const char*>::iterator sdkit = fSDKPaths.begin(); sdkit != fSDKPaths.end(); sdkit++) { | |
2955 | const char* sdkDir = *sdkit; | |
2956 | const int sdkDirLen = strlen(sdkDir); | |
2957 | char newPath[libDirLen + sdkDirLen+4]; | |
2958 | strcpy(newPath, sdkDir); | |
2959 | if ( newPath[sdkDirLen-1] == '/' ) | |
2960 | newPath[sdkDirLen-1] = '\0'; | |
2961 | strcat(newPath, libDir); | |
2962 | struct stat statBuffer; | |
2963 | if ( stat(newPath, &statBuffer) == 0 ) { | |
2964 | fLibrarySearchPaths.push_back(strdup(newPath)); | |
2965 | sdkOverride = true; | |
2966 | } | |
2967 | } | |
2968 | } | |
55e3d2f6 A |
2969 | if ( !sdkOverride ) { |
2970 | if ( (libIndex >= standardLibraryPathsStartIndex) && (fSDKPaths.size() == 1) ) { | |
2971 | // <rdar://problem/6438270> -syslibroot should skip standard search paths not in the SDK | |
2972 | // if one SDK is specified and a standard library path is not in the SDK, don't use it | |
2973 | } | |
2974 | else { | |
2975 | fLibrarySearchPaths.push_back(libDir); | |
2976 | } | |
2977 | } | |
6e880c60 | 2978 | } |
d696c285 | 2979 | |
6e880c60 | 2980 | // now merge sdk and framework paths to make real search paths |
d696c285 | 2981 | fFrameworkSearchPaths.reserve(frameworkPaths.size()*(fSDKPaths.size()+1)); |
55e3d2f6 A |
2982 | int frameIndex = 0; |
2983 | for (std::vector<const char*>::iterator it = frameworkPaths.begin(); it != frameworkPaths.end(); ++it, ++frameIndex) { | |
6e880c60 A |
2984 | const char* frameworkDir = *it; |
2985 | bool sdkOverride = false; | |
2986 | if ( frameworkDir[0] == '/' ) { | |
2987 | char betterFrameworkDir[PATH_MAX]; | |
2988 | if ( strstr(frameworkDir, "/..") != NULL ) { | |
2989 | if ( realpath(frameworkDir, betterFrameworkDir) != NULL ) | |
d696c285 | 2990 | frameworkDir = strdup(betterFrameworkDir); |
6e880c60 A |
2991 | } |
2992 | const int frameworkDirLen = strlen(frameworkDir); | |
2993 | for (std::vector<const char*>::iterator sdkit = fSDKPaths.begin(); sdkit != fSDKPaths.end(); sdkit++) { | |
2994 | const char* sdkDir = *sdkit; | |
2995 | const int sdkDirLen = strlen(sdkDir); | |
2996 | char newPath[frameworkDirLen + sdkDirLen+4]; | |
2997 | strcpy(newPath, sdkDir); | |
2998 | if ( newPath[sdkDirLen-1] == '/' ) | |
2999 | newPath[sdkDirLen-1] = '\0'; | |
3000 | strcat(newPath, frameworkDir); | |
3001 | struct stat statBuffer; | |
3002 | if ( stat(newPath, &statBuffer) == 0 ) { | |
3003 | fFrameworkSearchPaths.push_back(strdup(newPath)); | |
3004 | sdkOverride = true; | |
3005 | } | |
3006 | } | |
3007 | } | |
55e3d2f6 A |
3008 | if ( !sdkOverride ) { |
3009 | if ( (frameIndex >= standardFrameworkPathsStartIndex) && (fSDKPaths.size() == 1) ) { | |
3010 | // <rdar://problem/6438270> -syslibroot should skip standard search paths not in the SDK | |
3011 | // if one SDK is specified and a standard library path is not in the SDK, don't use it | |
3012 | } | |
3013 | else { | |
3014 | fFrameworkSearchPaths.push_back(frameworkDir); | |
3015 | } | |
3016 | } | |
6e880c60 | 3017 | } |
d696c285 | 3018 | |
6e880c60 A |
3019 | if ( fVerbose ) { |
3020 | fprintf(stderr,"Library search paths:\n"); | |
d696c285 A |
3021 | for (std::vector<const char*>::iterator it = fLibrarySearchPaths.begin(); |
3022 | it != fLibrarySearchPaths.end(); | |
3023 | it++) | |
6e880c60 A |
3024 | fprintf(stderr,"\t%s\n", *it); |
3025 | fprintf(stderr,"Framework search paths:\n"); | |
d696c285 A |
3026 | for (std::vector<const char*>::iterator it = fFrameworkSearchPaths.begin(); |
3027 | it != fFrameworkSearchPaths.end(); | |
3028 | it++) | |
6e880c60 | 3029 | fprintf(stderr,"\t%s\n", *it); |
c2646906 A |
3030 | } |
3031 | } | |
3032 | ||
3033 | // this is run before the command line is parsed | |
3034 | void Options::parsePreCommandLineEnvironmentSettings() | |
3035 | { | |
d696c285 A |
3036 | if ((getenv("LD_TRACE_ARCHIVES") != NULL) |
3037 | || (getenv("RC_TRACE_ARCHIVES") != NULL)) | |
a645023d | 3038 | fTraceArchives = true; |
d696c285 A |
3039 | |
3040 | if ((getenv("LD_TRACE_DYLIBS") != NULL) | |
3041 | || (getenv("RC_TRACE_DYLIBS") != NULL)) { | |
a645023d A |
3042 | fTraceDylibs = true; |
3043 | fTraceIndirectDylibs = true; | |
c2646906 | 3044 | } |
d696c285 A |
3045 | |
3046 | if (getenv("RC_TRACE_DYLIB_SEARCHING") != NULL) { | |
3047 | fTraceDylibSearching = true; | |
3048 | } | |
3049 | ||
3050 | if (getenv("LD_PRINT_OPTIONS") != NULL) | |
3051 | fPrintOptions = true; | |
3052 | ||
a645023d A |
3053 | if (fTraceDylibs || fTraceArchives) |
3054 | fTraceOutputFile = getenv("LD_TRACE_FILE"); | |
a61fdf0a A |
3055 | |
3056 | if (getenv("LD_PRINT_ORDER_FILE_STATISTICS") != NULL) | |
3057 | fPrintOrderFileStatistics = true; | |
2f2f92e4 A |
3058 | |
3059 | if (getenv("LD_SPLITSEGS_NEW_LIBRARIES") != NULL) | |
3060 | fSplitSegs = true; | |
3061 | ||
3062 | if (getenv("LD_NO_ENCRYPT") != NULL) | |
3063 | fEncryptable = false; | |
55e3d2f6 A |
3064 | |
3065 | if (getenv("LD_ALLOW_CPU_SUBTYPE_MISMATCHES") != NULL) | |
3066 | fAllowCpuSubtypeMismatches = true; | |
3067 | ||
2f2f92e4 | 3068 | sWarningsSideFilePath = getenv("LD_WARN_FILE"); |
a645023d A |
3069 | |
3070 | const char* customDyldPath = getenv("LD_DYLD_PATH"); | |
3071 | if ( customDyldPath != NULL ) | |
3072 | fDyldInstallPath = customDyldPath; | |
ebf6f434 A |
3073 | |
3074 | const char* debugArchivePath = getenv("LD_DEBUG_SNAPSHOT"); | |
3075 | if (debugArchivePath != NULL) { | |
3076 | fLinkSnapshot.setSnapshotMode(Snapshot::SNAPSHOT_DEBUG); | |
3077 | if (strlen(debugArchivePath) > 0) | |
3078 | fLinkSnapshot.setSnapshotPath(debugArchivePath); | |
3079 | fSnapshotRequested = true; | |
3080 | } | |
3081 | ||
3082 | const char* pipeFdString = getenv("LD_PIPELINE_FIFO"); | |
3083 | if (pipeFdString != NULL) { | |
3084 | fPipelineFifo = pipeFdString; | |
3085 | } | |
c2646906 A |
3086 | } |
3087 | ||
2f2f92e4 | 3088 | |
c2646906 A |
3089 | // this is run after the command line is parsed |
3090 | void Options::parsePostCommandLineEnvironmentSettings() | |
3091 | { | |
d696c285 A |
3092 | // when building a dynamic main executable, default any use of @executable_path to output path |
3093 | if ( fExecutablePath == NULL && (fOutputKind == kDynamicExecutable) ) { | |
3094 | fExecutablePath = fOutputFile; | |
3095 | } | |
a61fdf0a A |
3096 | |
3097 | // allow build system to set default seg_addr_table | |
3098 | if ( fSegAddrTablePath == NULL ) | |
3099 | fSegAddrTablePath = getenv("LD_SEG_ADDR_TABLE"); | |
3100 | ||
3101 | // allow build system to turn on prebinding | |
3102 | if ( !fPrebind ) { | |
3103 | fPrebind = ( getenv("LD_PREBIND") != NULL ); | |
3104 | } | |
2f2f92e4 A |
3105 | |
3106 | // allow build system to force on dead-code-stripping | |
a645023d | 3107 | if ( !fDeadStrip ) { |
2f2f92e4 A |
3108 | if ( getenv("LD_DEAD_STRIP") != NULL ) { |
3109 | switch (fOutputKind) { | |
3110 | case Options::kDynamicLibrary: | |
3111 | case Options::kDynamicExecutable: | |
3112 | case Options::kDynamicBundle: | |
a645023d | 3113 | fDeadStrip = true; |
2f2f92e4 | 3114 | break; |
55e3d2f6 | 3115 | case Options::kPreload: |
2f2f92e4 A |
3116 | case Options::kObjectFile: |
3117 | case Options::kDyld: | |
3118 | case Options::kStaticExecutable: | |
55e3d2f6 | 3119 | case Options::kKextBundle: |
2f2f92e4 A |
3120 | break; |
3121 | } | |
3122 | } | |
3123 | } | |
3124 | ||
3125 | // allow build system to force on -warn_commons | |
3126 | if ( getenv("LD_WARN_COMMONS") != NULL ) | |
3127 | fWarnCommons = true; | |
ebf6f434 A |
3128 | |
3129 | // allow B&I to set default -source_version | |
3130 | if ( fSourceVersion == 0 ) { | |
3131 | const char* vers = getenv("RC_ProjectSourceVersion"); | |
3132 | if ( vers != NULL ) | |
3133 | fSourceVersion = parseVersionNumber64(vers); | |
3134 | } | |
4be885f6 | 3135 | |
69a49097 A |
3136 | } |
3137 | ||
3138 | void Options::reconfigureDefaults() | |
3139 | { | |
3140 | // sync reader options | |
3141 | switch ( fOutputKind ) { | |
3142 | case Options::kObjectFile: | |
a645023d | 3143 | fForFinalLinkedImage = false; |
69a49097 | 3144 | break; |
a61fdf0a | 3145 | case Options::kDyld: |
a645023d A |
3146 | fForDyld = true; |
3147 | fForFinalLinkedImage = true; | |
3148 | fNoEHLabels = true; | |
a61fdf0a | 3149 | break; |
69a49097 A |
3150 | case Options::kDynamicLibrary: |
3151 | case Options::kDynamicBundle: | |
55e3d2f6 | 3152 | case Options::kKextBundle: |
a645023d A |
3153 | fForFinalLinkedImage = true; |
3154 | fNoEHLabels = true; | |
a61fdf0a A |
3155 | break; |
3156 | case Options::kDynamicExecutable: | |
3157 | case Options::kStaticExecutable: | |
55e3d2f6 | 3158 | case Options::kPreload: |
a645023d A |
3159 | fLinkingMainExecutable = true; |
3160 | fForFinalLinkedImage = true; | |
3161 | fNoEHLabels = true; | |
69a49097 A |
3162 | break; |
3163 | } | |
3164 | ||
3165 | // set default min OS version | |
a645023d | 3166 | if ( (fMacVersionMin == ld::macVersionUnset) |
afe874b1 | 3167 | && (fIOSVersionMin == ld::iOSVersionUnset) ) { |
55e3d2f6 A |
3168 | // if neither -macosx_version_min nor -iphoneos_version_min used, try environment variables |
3169 | const char* macVers = getenv("MACOSX_DEPLOYMENT_TARGET"); | |
3170 | const char* iPhoneVers = getenv("IPHONEOS_DEPLOYMENT_TARGET"); | |
a645023d | 3171 | const char* iOSVers = getenv("IOS_DEPLOYMENT_TARGET"); |
afe874b1 | 3172 | const char* iOSSimulatorVers = getenv("IOS_SIMULATOR_DEPLOYMENT_TARGET"); |
55e3d2f6 A |
3173 | if ( macVers != NULL ) |
3174 | setMacOSXVersionMin(macVers); | |
3175 | else if ( iPhoneVers != NULL ) | |
afe874b1 | 3176 | setIOSVersionMin(iPhoneVers); |
a645023d | 3177 | else if ( iOSVers != NULL ) |
afe874b1 A |
3178 | setIOSVersionMin(iOSVers); |
3179 | else if ( iOSSimulatorVers != NULL ) | |
3180 | setIOSVersionMin(iOSSimulatorVers); | |
55e3d2f6 A |
3181 | else { |
3182 | // if still nothing, set default based on architecture | |
3183 | switch ( fArchitecture ) { | |
3184 | case CPU_TYPE_I386: | |
3185 | case CPU_TYPE_X86_64: | |
a645023d A |
3186 | if ( (fOutputKind != Options::kObjectFile) && (fOutputKind != Options::kPreload) ) { |
3187 | #ifdef DEFAULT_MACOSX_MIN_VERSION | |
ebf6f434 | 3188 | warning("-macosx_version_min not specified, assuming " DEFAULT_MACOSX_MIN_VERSION); |
a645023d A |
3189 | setMacOSXVersionMin(DEFAULT_MACOSX_MIN_VERSION); |
3190 | #else | |
ebf6f434 | 3191 | warning("-macosx_version_min not specified, assuming 10.6"); |
a645023d A |
3192 | fMacVersionMin = ld::mac10_6; |
3193 | #endif | |
3194 | } | |
55e3d2f6 A |
3195 | break; |
3196 | case CPU_TYPE_ARM: | |
a645023d A |
3197 | if ( (fOutputKind != Options::kObjectFile) && (fOutputKind != Options::kPreload) ) { |
3198 | #if defined(DEFAULT_IPHONEOS_MIN_VERSION) | |
ebf6f434 | 3199 | warning("-ios_version_min not specified, assuming " DEFAULT_IPHONEOS_MIN_VERSION); |
afe874b1 | 3200 | setIOSVersionMin(DEFAULT_IPHONEOS_MIN_VERSION); |
a645023d | 3201 | #elif defined(DEFAULT_MACOSX_MIN_VERSION) |
ebf6f434 | 3202 | warning("-macosx_version_min not specified, assuming " DEFAULT_MACOSX_MIN_VERSION); |
a645023d A |
3203 | setMacOSXVersionMin(DEFAULT_MACOSX_MIN_VERSION); |
3204 | #else | |
ebf6f434 | 3205 | warning("-macosx_version_min not specified, assuming 10.6"); |
a645023d A |
3206 | fMacVersionMin = ld::mac10_6; |
3207 | #endif | |
3208 | } | |
3209 | break; | |
3210 | default: | |
3211 | // architecture will be infered ;ater by examining .o files | |
55e3d2f6 A |
3212 | break; |
3213 | } | |
3214 | } | |
69a49097 A |
3215 | } |
3216 | ||
55e3d2f6 | 3217 | |
69a49097 A |
3218 | // adjust min based on architecture |
3219 | switch ( fArchitecture ) { | |
3220 | case CPU_TYPE_I386: | |
afe874b1 | 3221 | if ( (fMacVersionMin < ld::mac10_4) && (fIOSVersionMin == ld::iOSVersionUnset) ) { |
2f2f92e4 | 3222 | //warning("-macosx_version_min should be 10.4 or later for i386"); |
a645023d | 3223 | fMacVersionMin = ld::mac10_4; |
69a49097 A |
3224 | } |
3225 | break; | |
69a49097 | 3226 | case CPU_TYPE_X86_64: |
ebf6f434 | 3227 | if ( (fMacVersionMin < ld::mac10_4) && (fIOSVersionMin == ld::iOSVersionUnset) ) { |
2f2f92e4 | 3228 | //warning("-macosx_version_min should be 10.4 or later for x86_64"); |
a645023d | 3229 | fMacVersionMin = ld::mac10_4; |
69a49097 A |
3230 | } |
3231 | break; | |
3232 | } | |
55e3d2f6 A |
3233 | |
3234 | // adjust kext type based on architecture | |
3235 | if ( fOutputKind == kKextBundle ) { | |
3236 | switch ( fArchitecture ) { | |
3237 | case CPU_TYPE_X86_64: | |
3238 | // x86_64 uses new MH_KEXT_BUNDLE type | |
55e3d2f6 | 3239 | fMakeCompressedDyldInfo = false; |
a645023d | 3240 | fMakeCompressedDyldInfoForceOff = true; |
55e3d2f6 A |
3241 | fAllowTextRelocs = true; |
3242 | fUndefinedTreatment = kUndefinedDynamicLookup; | |
3243 | break; | |
afe874b1 A |
3244 | case CPU_TYPE_ARM: |
3245 | if ( fIOSVersionMin >= ld::iOS_5_0 ) { | |
3246 | // iOS 5.0 and later use new MH_KEXT_BUNDLE type | |
3247 | fMakeCompressedDyldInfo = false; | |
3248 | fMakeCompressedDyldInfoForceOff = true; | |
b1f7435d A |
3249 | // kexts are PIC in iOS 6.0 and later |
3250 | fAllowTextRelocs = (fIOSVersionMin < ld::iOS_6_0); | |
ebf6f434 | 3251 | fKextsUseStubs = !fAllowTextRelocs; |
afe874b1 A |
3252 | fUndefinedTreatment = kUndefinedDynamicLookup; |
3253 | break; | |
3254 | } | |
3255 | // else use object file | |
55e3d2f6 | 3256 | case CPU_TYPE_I386: |
55e3d2f6 A |
3257 | // use .o files |
3258 | fOutputKind = kObjectFile; | |
3259 | break; | |
3260 | } | |
3261 | } | |
a61fdf0a | 3262 | |
55e3d2f6 | 3263 | // disable implicit dylibs when targeting 10.3 |
2f2f92e4 | 3264 | // <rdar://problem/5451987> add option to disable implicit load commands for indirectly used public dylibs |
afe874b1 | 3265 | if ( !minOS(ld::mac10_4, ld::iOS_2_0) ) |
a645023d | 3266 | fImplicitlyLinkPublicDylibs = false; |
2f2f92e4 A |
3267 | |
3268 | ||
55e3d2f6 A |
3269 | // allow build system to force linker to ignore -prebind |
3270 | if ( getenv("LD_FORCE_NO_PREBIND") != NULL ) | |
3271 | fPrebind = false; | |
a61fdf0a | 3272 | |
55e3d2f6 | 3273 | // allow build system to force linker to ignore -seg_addr_table |
a61fdf0a A |
3274 | if ( getenv("LD_FORCE_NO_SEG_ADDR_TABLE") != NULL ) |
3275 | fSegAddrTablePath = NULL; | |
3276 | ||
3277 | // check for base address specified externally | |
2f2f92e4 | 3278 | if ( (fSegAddrTablePath != NULL) && (fOutputKind == Options::kDynamicLibrary) ) { |
a61fdf0a | 3279 | parseSegAddrTable(fSegAddrTablePath, this->installPath()); |
2f2f92e4 A |
3280 | // HACK to support seg_addr_table entries that are physical paths instead of install paths |
3281 | if ( fBaseAddress == 0 ) { | |
55e3d2f6 | 3282 | if ( strcmp(this->installPath(), "/usr/lib/libstdc++.6.dylib") == 0 ) { |
2f2f92e4 | 3283 | parseSegAddrTable(fSegAddrTablePath, "/usr/lib/libstdc++.6.0.4.dylib"); |
55e3d2f6 A |
3284 | if ( fBaseAddress == 0 ) |
3285 | parseSegAddrTable(fSegAddrTablePath, "/usr/lib/libstdc++.6.0.9.dylib"); | |
3286 | } | |
2f2f92e4 A |
3287 | |
3288 | else if ( strcmp(this->installPath(), "/usr/lib/libz.1.dylib") == 0 ) | |
3289 | parseSegAddrTable(fSegAddrTablePath, "/usr/lib/libz.1.2.3.dylib"); | |
3290 | ||
3291 | else if ( strcmp(this->installPath(), "/usr/lib/libutil.dylib") == 0 ) | |
3292 | parseSegAddrTable(fSegAddrTablePath, "/usr/lib/libutil1.0.dylib"); | |
3293 | } | |
3294 | } | |
a61fdf0a | 3295 | |
a61fdf0a A |
3296 | // split segs only allowed for dylibs |
3297 | if ( fSplitSegs ) { | |
b2fa67a8 | 3298 | // split seg only supported for i386, and arm. |
2f2f92e4 | 3299 | switch ( fArchitecture ) { |
2f2f92e4 A |
3300 | case CPU_TYPE_I386: |
3301 | if ( fOutputKind != Options::kDynamicLibrary ) | |
3302 | fSplitSegs = false; | |
3303 | // make sure read and write segments are proper distance apart | |
3304 | if ( fSplitSegs && (fBaseWritableAddress-fBaseAddress != 0x10000000) ) | |
3305 | fBaseWritableAddress = fBaseAddress + 0x10000000; | |
3306 | break; | |
3307 | case CPU_TYPE_ARM: | |
3308 | if ( fOutputKind != Options::kDynamicLibrary ) { | |
3309 | fSplitSegs = false; | |
3310 | } | |
3311 | else { | |
3312 | // make sure read and write segments are proper distance apart | |
3313 | if ( fSplitSegs && (fBaseWritableAddress-fBaseAddress != 0x08000000) ) | |
3314 | fBaseWritableAddress = fBaseAddress + 0x08000000; | |
3315 | } | |
3316 | break; | |
3317 | default: | |
3318 | fSplitSegs = false; | |
3319 | fBaseAddress = 0; | |
3320 | fBaseWritableAddress = 0; | |
a61fdf0a A |
3321 | } |
3322 | } | |
3323 | ||
a645023d A |
3324 | // set too-large size |
3325 | switch ( fArchitecture ) { | |
a645023d A |
3326 | case CPU_TYPE_I386: |
3327 | fMaxAddress = 0xFFFFFFFF; | |
3328 | break; | |
a645023d A |
3329 | case CPU_TYPE_X86_64: |
3330 | break; | |
3331 | case CPU_TYPE_ARM: | |
3332 | switch ( fOutputKind ) { | |
3333 | case Options::kDynamicExecutable: | |
3334 | case Options::kDynamicLibrary: | |
3335 | case Options::kDynamicBundle: | |
3336 | // user land code is limited to low 1GB | |
3337 | fMaxAddress = 0x2FFFFFFF; | |
3338 | break; | |
3339 | case Options::kStaticExecutable: | |
3340 | case Options::kObjectFile: | |
3341 | case Options::kDyld: | |
3342 | case Options::kPreload: | |
3343 | case Options::kKextBundle: | |
3344 | fMaxAddress = 0xFFFFFFFF; | |
3345 | break; | |
3346 | } | |
3347 | // range check -seg1addr for ARM | |
3348 | if ( fBaseAddress > fMaxAddress ) { | |
3349 | warning("ignoring -seg1addr 0x%08llX. Address out of range.", fBaseAddress); | |
3350 | fBaseAddress = 0; | |
3351 | } | |
3352 | break; | |
3353 | } | |
3354 | ||
55e3d2f6 A |
3355 | // <rdar://problem/6138961> -r implies no prebinding for all architectures |
3356 | if ( fOutputKind == Options::kObjectFile ) | |
3357 | fPrebind = false; | |
3358 | ||
a61fdf0a A |
3359 | // disable prebinding depending on arch and min OS version |
3360 | if ( fPrebind ) { | |
3361 | switch ( fArchitecture ) { | |
a61fdf0a | 3362 | case CPU_TYPE_I386: |
a645023d | 3363 | if ( fMacVersionMin == ld::mac10_4 ) { |
2f2f92e4 | 3364 | // in 10.4 only split seg dylibs are prebound |
a61fdf0a A |
3365 | if ( (fOutputKind != Options::kDynamicLibrary) || ! fSplitSegs ) |
3366 | fPrebind = false; | |
3367 | } | |
a645023d | 3368 | else if ( fMacVersionMin >= ld::mac10_5 ) { |
2f2f92e4 A |
3369 | // in 10.5 nothing is prebound |
3370 | fPrebind = false; | |
3371 | } | |
afe874b1 A |
3372 | else if ( fIOSVersionMin != ld::iOSVersionUnset ) { |
3373 | // nothing in simulator is prebound | |
3374 | fPrebind = false; | |
3375 | } | |
2f2f92e4 A |
3376 | else { |
3377 | // in 10.3 and earlier only dylibs and main executables could be prebound | |
3378 | switch ( fOutputKind ) { | |
3379 | case Options::kDynamicExecutable: | |
3380 | case Options::kDynamicLibrary: | |
3381 | // only main executables and dylibs can be prebound | |
3382 | break; | |
3383 | case Options::kStaticExecutable: | |
3384 | case Options::kDynamicBundle: | |
3385 | case Options::kObjectFile: | |
3386 | case Options::kDyld: | |
55e3d2f6 A |
3387 | case Options::kPreload: |
3388 | case Options::kKextBundle: | |
2f2f92e4 A |
3389 | // disable prebinding for everything else |
3390 | fPrebind = false; | |
3391 | break; | |
3392 | } | |
3393 | } | |
a61fdf0a | 3394 | break; |
a61fdf0a A |
3395 | case CPU_TYPE_X86_64: |
3396 | fPrebind = false; | |
3397 | break; | |
2f2f92e4 A |
3398 | case CPU_TYPE_ARM: |
3399 | switch ( fOutputKind ) { | |
3400 | case Options::kDynamicExecutable: | |
3401 | case Options::kDynamicLibrary: | |
3402 | // only main executables and dylibs can be prebound | |
3403 | break; | |
3404 | case Options::kStaticExecutable: | |
3405 | case Options::kDynamicBundle: | |
3406 | case Options::kObjectFile: | |
3407 | case Options::kDyld: | |
55e3d2f6 A |
3408 | case Options::kPreload: |
3409 | case Options::kKextBundle: | |
2f2f92e4 A |
3410 | // disable prebinding for everything else |
3411 | fPrebind = false; | |
3412 | break; | |
3413 | } | |
3414 | break; | |
a61fdf0a A |
3415 | } |
3416 | } | |
3417 | ||
2f2f92e4 A |
3418 | // only prebound images can be split-seg |
3419 | if ( fSplitSegs && !fPrebind ) | |
3420 | fSplitSegs = false; | |
a61fdf0a | 3421 | |
55e3d2f6 A |
3422 | // determine if info for shared region should be added |
3423 | if ( fOutputKind == Options::kDynamicLibrary ) { | |
afe874b1 | 3424 | if ( minOS(ld::mac10_5, ld::iOS_3_1) ) |
55e3d2f6 A |
3425 | if ( !fPrebind ) |
3426 | if ( (strncmp(this->installPath(), "/usr/lib/", 9) == 0) | |
3427 | || (strncmp(this->installPath(), "/System/Library/", 16) == 0) ) | |
3428 | fSharedRegionEligible = true; | |
3429 | } | |
b2fa67a8 A |
3430 | else if ( fOutputKind == Options::kDyld ) { |
3431 | // <rdar://problem/10111122> Enable dyld to be put into the dyld shared cache | |
3432 | fSharedRegionEligible = true; | |
3433 | } | |
3434 | ||
a61fdf0a A |
3435 | // figure out if module table is needed for compatibility with old ld/dyld |
3436 | if ( fOutputKind == Options::kDynamicLibrary ) { | |
3437 | switch ( fArchitecture ) { | |
afe874b1 A |
3438 | case CPU_TYPE_I386: |
3439 | if ( fIOSVersionMin != ld::iOSVersionUnset ) // simulator never needs modules | |
3440 | break; | |
2f2f92e4 | 3441 | case CPU_TYPE_ARM: |
55e3d2f6 A |
3442 | if ( fPrebind ) |
3443 | fNeedsModuleTable = true; // redo_prebinding requires a module table | |
2f2f92e4 | 3444 | break; |
a61fdf0a A |
3445 | } |
3446 | } | |
3447 | ||
2f2f92e4 A |
3448 | // <rdar://problem/5366363> -r -x implies -S |
3449 | if ( (fOutputKind == Options::kObjectFile) && (fLocalSymbolHandling == kLocalSymbolsNone) ) | |
a645023d | 3450 | fDebugInfoStripping = Options::kDebugInfoNone; |
55e3d2f6 A |
3451 | |
3452 | // choose how to process unwind info | |
3453 | switch ( fArchitecture ) { | |
3454 | case CPU_TYPE_I386: | |
3455 | case CPU_TYPE_X86_64: | |
3456 | switch ( fOutputKind ) { | |
3457 | case Options::kObjectFile: | |
3458 | case Options::kStaticExecutable: | |
3459 | case Options::kPreload: | |
3460 | case Options::kKextBundle: | |
a645023d | 3461 | fAddCompactUnwindEncoding = false; |
55e3d2f6 A |
3462 | break; |
3463 | case Options::kDyld: | |
3464 | case Options::kDynamicLibrary: | |
3465 | case Options::kDynamicBundle: | |
3466 | case Options::kDynamicExecutable: | |
a645023d A |
3467 | //if ( fAddCompactUnwindEncoding && (fVersionMin >= ld::mac10_6) ) |
3468 | // fRemoveDwarfUnwindIfCompactExists = true; | |
55e3d2f6 A |
3469 | break; |
3470 | } | |
3471 | break; | |
55e3d2f6 | 3472 | case CPU_TYPE_ARM: |
a645023d A |
3473 | fAddCompactUnwindEncoding = false; |
3474 | fRemoveDwarfUnwindIfCompactExists = false; | |
55e3d2f6 A |
3475 | break; |
3476 | case 0: | |
3477 | // if -arch is missing, assume we don't want compact unwind info | |
a645023d | 3478 | fAddCompactUnwindEncoding = false; |
55e3d2f6 A |
3479 | break; |
3480 | } | |
3481 | ||
2f2f92e4 A |
3482 | // only ARM main executables can be encrypted |
3483 | if ( fOutputKind != Options::kDynamicExecutable ) | |
3484 | fEncryptable = false; | |
3485 | if ( fArchitecture != CPU_TYPE_ARM ) | |
3486 | fEncryptable = false; | |
4be885f6 | 3487 | |
55e3d2f6 A |
3488 | // don't move inits in dyld because dyld wants certain |
3489 | // entries point at stable locations at the start of __text | |
3490 | if ( fOutputKind == Options::kDyld ) | |
a645023d | 3491 | fAutoOrderInitializers = false; |
55e3d2f6 A |
3492 | |
3493 | ||
3494 | // disable __data ordering for some output kinds | |
3495 | switch ( fOutputKind ) { | |
3496 | case Options::kObjectFile: | |
3497 | case Options::kDyld: | |
3498 | case Options::kStaticExecutable: | |
3499 | case Options::kPreload: | |
3500 | case Options::kKextBundle: | |
3501 | fOrderData = false; | |
3502 | break; | |
3503 | case Options::kDynamicExecutable: | |
3504 | case Options::kDynamicLibrary: | |
3505 | case Options::kDynamicBundle: | |
3506 | break; | |
3507 | } | |
3508 | ||
a645023d A |
3509 | // only use compressed LINKEDIT for final linked images |
3510 | switch ( fOutputKind ) { | |
3511 | case Options::kDynamicExecutable: | |
3512 | case Options::kDynamicLibrary: | |
3513 | case Options::kDynamicBundle: | |
3514 | break; | |
3515 | case Options::kPreload: | |
3516 | case Options::kStaticExecutable: | |
3517 | case Options::kObjectFile: | |
3518 | case Options::kDyld: | |
3519 | case Options::kKextBundle: | |
3520 | fMakeCompressedDyldInfoForceOff = true; | |
3521 | break; | |
3522 | } | |
3523 | if ( fMakeCompressedDyldInfoForceOff ) | |
3524 | fMakeCompressedDyldInfo = false; | |
3525 | ||
3526 | ||
fb24a050 | 3527 | // only use compressed LINKEDIT for: |
ebf6f434 A |
3528 | // Mac OS X 10.6 or later |
3529 | // iOS 3.1 or later | |
55e3d2f6 | 3530 | if ( fMakeCompressedDyldInfo ) { |
ebf6f434 A |
3531 | if ( !minOS(ld::mac10_6, ld::iOS_3_1) ) |
3532 | fMakeCompressedDyldInfo = false; | |
55e3d2f6 A |
3533 | } |
3534 | ||
ebf6f434 | 3535 | |
55e3d2f6 A |
3536 | // only ARM enforces that cpu-sub-types must match |
3537 | if ( fArchitecture != CPU_TYPE_ARM ) | |
3538 | fAllowCpuSubtypeMismatches = true; | |
4be885f6 A |
3539 | |
3540 | // only final linked images can not optimize zero fill sections | |
3541 | if ( fOutputKind == Options::kObjectFile ) | |
a645023d A |
3542 | fOptimizeZeroFill = true; |
3543 | ||
3544 | // all undefines in -r mode | |
3545 | // if ( fOutputKind == Options::kObjectFile ) | |
3546 | // fUndefinedTreatment = kUndefinedSuppress; | |
fb24a050 A |
3547 | |
3548 | // only dynamic final linked images should warn about use of commmons | |
3549 | if ( fWarnCommons ) { | |
3550 | switch ( fOutputKind ) { | |
3551 | case Options::kDynamicExecutable: | |
3552 | case Options::kDynamicLibrary: | |
3553 | case Options::kDynamicBundle: | |
3554 | break; | |
3555 | case Options::kPreload: | |
3556 | case Options::kStaticExecutable: | |
3557 | case Options::kObjectFile: | |
3558 | case Options::kDyld: | |
3559 | case Options::kKextBundle: | |
3560 | fWarnCommons = false; | |
3561 | break; | |
3562 | } | |
3563 | } | |
3564 | ||
3565 | // Mac OS X 10.5 and iPhoneOS 2.0 support LC_REEXPORT_DYLIB | |
afe874b1 | 3566 | if ( minOS(ld::mac10_5, ld::iOS_2_0) ) |
fb24a050 | 3567 | fUseSimplifiedDylibReExports = true; |
a645023d A |
3568 | |
3569 | // Mac OS X 10.7 and iOS 4.2 support LC_LOAD_UPWARD_DYLIB | |
afe874b1 | 3570 | if ( minOS(ld::mac10_7, ld::iOS_4_2) && (fOutputKind == kDynamicLibrary) ) |
a645023d | 3571 | fCanUseUpwardDylib = true; |
d9246299 | 3572 | |
afe874b1 | 3573 | // MacOSX 10.7 defaults to PIE |
60ce07c1 A |
3574 | if ( ((fArchitecture == CPU_TYPE_X86_64) || (fArchitecture == CPU_TYPE_I386)) |
3575 | && (fOutputKind == kDynamicExecutable) | |
3576 | && (fMacVersionMin >= ld::mac10_7) ) { | |
3577 | fPositionIndependentExecutable = true; | |
d9246299 | 3578 | } |
a645023d A |
3579 | |
3580 | // armv7 for iOS4.3 defaults to PIE | |
3581 | if ( (fArchitecture == CPU_TYPE_ARM) | |
afe874b1 | 3582 | && fArchSupportsThumb2 |
a645023d | 3583 | && (fOutputKind == kDynamicExecutable) |
afe874b1 | 3584 | && (fIOSVersionMin >= ld::iOS_4_3) ) { |
a645023d A |
3585 | fPositionIndependentExecutable = true; |
3586 | } | |
3587 | ||
3588 | // -no_pie anywhere on command line disable PIE | |
3589 | if ( fDisablePositionIndependentExecutable ) | |
3590 | fPositionIndependentExecutable = false; | |
3591 | ||
3592 | // set fOutputSlidable | |
3593 | switch ( fOutputKind ) { | |
3594 | case Options::kObjectFile: | |
a645023d A |
3595 | fOutputSlidable = false; |
3596 | break; | |
ebf6f434 | 3597 | case Options::kStaticExecutable: |
a645023d A |
3598 | case Options::kDynamicExecutable: |
3599 | fOutputSlidable = fPositionIndependentExecutable; | |
3600 | break; | |
3601 | case Options::kPreload: | |
3602 | fOutputSlidable = fPIEOnCommandLine; | |
3603 | break; | |
3604 | case Options::kDyld: | |
3605 | case Options::kDynamicLibrary: | |
3606 | case Options::kDynamicBundle: | |
3607 | case Options::kKextBundle: | |
3608 | fOutputSlidable = true; | |
3609 | break; | |
3610 | } | |
3611 | ||
3612 | // let linker know if thread local variables are supported | |
3613 | if ( fMacVersionMin >= ld::mac10_7 ) { | |
3614 | fTLVSupport = true; | |
3615 | } | |
3616 | ||
afe874b1 | 3617 | // default to adding version load command for dynamic code, static code must opt-in |
a645023d A |
3618 | switch ( fOutputKind ) { |
3619 | case Options::kObjectFile: | |
afe874b1 A |
3620 | fVersionLoadCommand = false; |
3621 | break; | |
a645023d A |
3622 | case Options::kStaticExecutable: |
3623 | case Options::kPreload: | |
3624 | case Options::kKextBundle: | |
afe874b1 A |
3625 | if ( fVersionLoadCommandForcedOn ) |
3626 | fVersionLoadCommand = true; | |
a645023d A |
3627 | break; |
3628 | case Options::kDynamicExecutable: | |
3629 | case Options::kDyld: | |
3630 | case Options::kDynamicLibrary: | |
3631 | case Options::kDynamicBundle: | |
afe874b1 A |
3632 | if ( !fVersionLoadCommandForcedOff ) |
3633 | fVersionLoadCommand = true; | |
3634 | // <rdar://problem/9945513> for now, don't create version load commands for iOS simulator builds | |
3635 | if ( fVersionLoadCommand && (fArchitecture == CPU_TYPE_I386) ) { | |
3636 | for (std::vector<const char*>::iterator sdkit = fSDKPaths.begin(); sdkit != fSDKPaths.end(); sdkit++) { | |
3637 | if ( strstr(*sdkit, "/iPhoneSimulator.platform/") != NULL ) | |
3638 | fVersionLoadCommand = false; | |
3639 | } | |
3640 | } | |
a645023d A |
3641 | break; |
3642 | } | |
3643 | ||
afe874b1 A |
3644 | // default to adding functions start for dynamic code, static code must opt-in |
3645 | switch ( fOutputKind ) { | |
afe874b1 A |
3646 | case Options::kPreload: |
3647 | case Options::kStaticExecutable: | |
3648 | case Options::kKextBundle: | |
b1f7435d A |
3649 | if ( fDataInCodeInfoLoadCommandForcedOn ) |
3650 | fDataInCodeInfoLoadCommand = true; | |
afe874b1 A |
3651 | if ( fFunctionStartsForcedOn ) |
3652 | fFunctionStartsLoadCommand = true; | |
3653 | break; | |
b1f7435d | 3654 | case Options::kObjectFile: |
afe874b1 A |
3655 | case Options::kDynamicExecutable: |
3656 | case Options::kDyld: | |
3657 | case Options::kDynamicLibrary: | |
3658 | case Options::kDynamicBundle: | |
b1f7435d A |
3659 | if ( !fDataInCodeInfoLoadCommandForcedOff ) |
3660 | fDataInCodeInfoLoadCommand = true; | |
afe874b1 A |
3661 | if ( !fFunctionStartsForcedOff ) |
3662 | fFunctionStartsLoadCommand = true; | |
3663 | break; | |
3664 | } | |
3665 | ||
a645023d | 3666 | // support re-export of individual symbols in MacOSX 10.7 and iOS 4.2 |
afe874b1 | 3667 | if ( (fOutputKind == kDynamicLibrary) && minOS(ld::mac10_7, ld::iOS_4_2) ) |
a645023d A |
3668 | fCanReExportSymbols = true; |
3669 | ||
3670 | // ObjC optimization is only in dynamic final linked images | |
3671 | switch ( fOutputKind ) { | |
3672 | case Options::kObjectFile: | |
3673 | case Options::kStaticExecutable: | |
3674 | case Options::kPreload: | |
3675 | case Options::kKextBundle: | |
3676 | case Options::kDyld: | |
3677 | fObjcCategoryMerging = false; | |
3678 | break; | |
3679 | case Options::kDynamicExecutable: | |
3680 | case Options::kDynamicLibrary: | |
3681 | case Options::kDynamicBundle: | |
3682 | break; | |
3683 | } | |
3684 | ||
3685 | // i386 main executables linked on Mac OS X 10.7 default to NX heap | |
3686 | // regardless of target unless overriden with -allow_heap_execute anywhere | |
3687 | // on the command line | |
3688 | if ( (fArchitecture == CPU_TYPE_I386) && (fOutputKind == kDynamicExecutable) && !fDisableNonExecutableHeap) | |
3689 | fNonExecutableHeap = true; | |
ebf6f434 A |
3690 | |
3691 | // Use LC_MAIN instead of LC_UNIXTHREAD for newer OSs | |
3692 | switch ( fOutputKind ) { | |
3693 | case Options::kDynamicExecutable: | |
3694 | if ( fEntryPointLoadCommandForceOn ) { | |
3695 | fEntryPointLoadCommand = true; | |
3696 | fEntryName = "_main"; | |
3697 | } | |
3698 | else if ( fEntryPointLoadCommandForceOff ) { | |
3699 | fNeedsThreadLoadCommand = true; | |
3700 | } | |
3701 | else { | |
b1f7435d A |
3702 | if ( (fIOSVersionMin != ld::iOSVersionUnset) && (fArchitecture == CPU_TYPE_I386) ) { |
3703 | // don't use LC_MAIN for simulator until min host OS is 10.8 for simulator | |
3704 | fNeedsThreadLoadCommand = true; | |
3705 | fEntryPointLoadCommand = false; | |
3706 | } | |
3707 | else if ( minOS(ld::mac10_8, ld::iOS_6_0) ) { | |
ebf6f434 A |
3708 | fEntryPointLoadCommand = true; |
3709 | fEntryName = "_main"; | |
3710 | } | |
3711 | else | |
3712 | fNeedsThreadLoadCommand = true; | |
3713 | } | |
3714 | break; | |
3715 | case Options::kObjectFile: | |
3716 | case Options::kKextBundle: | |
3717 | case Options::kDynamicLibrary: | |
3718 | case Options::kDynamicBundle: | |
3719 | break; | |
3720 | ||
3721 | case Options::kStaticExecutable: | |
3722 | case Options::kPreload: | |
3723 | case Options::kDyld: | |
3724 | fNeedsThreadLoadCommand = true; | |
3725 | break; | |
3726 | } | |
3727 | ||
3728 | // add LC_SOURCE_VERSION | |
3729 | switch ( fOutputKind ) { | |
3730 | case Options::kDynamicExecutable: | |
3731 | case Options::kKextBundle: | |
3732 | case Options::kDynamicLibrary: | |
3733 | case Options::kDynamicBundle: | |
3734 | case Options::kDyld: | |
3735 | case Options::kStaticExecutable: | |
3736 | if ( fSourceVersionLoadCommandForceOn ) { | |
3737 | fSourceVersionLoadCommand = true; | |
3738 | } | |
3739 | else if ( fSourceVersionLoadCommandForceOff ) { | |
3740 | fSourceVersionLoadCommand = false; | |
3741 | } | |
3742 | else { | |
b1f7435d | 3743 | if ( minOS(ld::mac10_8, ld::iOS_6_0) ) { |
ebf6f434 A |
3744 | fSourceVersionLoadCommand = true; |
3745 | } | |
3746 | else | |
3747 | fSourceVersionLoadCommand = false; | |
3748 | } | |
3749 | break; | |
3750 | case Options::kObjectFile: | |
3751 | case Options::kPreload: | |
3752 | fSourceVersionLoadCommand = false; | |
3753 | break; | |
3754 | } | |
3755 | ||
3756 | ||
3757 | // add LC_DYLIB_CODE_SIGN_DRS | |
3758 | switch ( fOutputKind ) { | |
3759 | case Options::kDynamicExecutable: | |
3760 | case Options::kDynamicLibrary: | |
3761 | case Options::kDynamicBundle: | |
3762 | if ( fDependentDRInfoForcedOn ) { | |
3763 | fDependentDRInfo = true; | |
3764 | } | |
3765 | else if ( fDependentDRInfoForcedOff ) { | |
3766 | fDependentDRInfo = false; | |
3767 | } | |
3768 | else { | |
b1f7435d | 3769 | if ( minOS(ld::mac10_8, ld::iOS_6_0) ) |
ebf6f434 A |
3770 | fDependentDRInfo = true; |
3771 | else | |
3772 | fDependentDRInfo = false; | |
3773 | } | |
3774 | break; | |
3775 | case Options::kKextBundle: | |
3776 | case Options::kDyld: | |
3777 | case Options::kStaticExecutable: | |
3778 | case Options::kObjectFile: | |
3779 | case Options::kPreload: | |
3780 | fDependentDRInfo = false; | |
3781 | break; | |
3782 | } | |
3783 | ||
3784 | // if -sdk_version not on command line, infer from -syslibroot | |
3785 | if ( (fSDKVersion == 0) && (fSDKPaths.size() > 0) ) { | |
3786 | const char* sdkPath = fSDKPaths.front(); | |
3787 | const char* end = &sdkPath[strlen(sdkPath)-1]; | |
3788 | while ( !isdigit(*end) && (end > sdkPath) ) | |
3789 | --end; | |
3790 | const char* start = end-1; | |
3791 | while ( (isdigit(*start) || (*start == '.')) && (start > sdkPath)) | |
3792 | --start; | |
3793 | char sdkVersionStr[32]; | |
3794 | int len = end-start+1; | |
3795 | if ( len > 2 ) { | |
3796 | strlcpy(sdkVersionStr, start+1, len); | |
3797 | fSDKVersion = parseVersionNumber32(sdkVersionStr); | |
3798 | } | |
3799 | } | |
3800 | ||
3801 | // if -sdk_version and -syslibroot not used, but targeting MacOSX, use current OS version | |
3802 | if ( (fSDKVersion == 0) && (fMacVersionMin != ld::macVersionUnset) ) { | |
3803 | // special case if RC_ProjectName and MACOSX_DEPLOYMENT_TARGET are both set that sdkversion=minos | |
3804 | if ( getenv("RC_ProjectName") && getenv("MACOSX_DEPLOYMENT_TARGET") ) { | |
3805 | fSDKVersion = fMacVersionMin; | |
3806 | } | |
3807 | else { | |
3808 | int mib[2] = { CTL_KERN, KERN_OSRELEASE }; | |
3809 | char kernVersStr[100]; | |
3810 | size_t strlen = sizeof(kernVersStr); | |
3811 | if ( sysctl(mib, 2, kernVersStr, &strlen, NULL, 0) != -1 ) { | |
3812 | uint32_t kernVers = parseVersionNumber32(kernVersStr); | |
3813 | int minor = (kernVers >> 16) - 4; // kernel major version is 4 ahead of x in 10.x | |
3814 | fSDKVersion = 0x000A0000 + (minor << 8); | |
3815 | } | |
3816 | } | |
3817 | } | |
3818 | ||
c2646906 A |
3819 | } |
3820 | ||
3821 | void Options::checkIllegalOptionCombinations() | |
3822 | { | |
3823 | // check -undefined setting | |
3824 | switch ( fUndefinedTreatment ) { | |
3825 | case kUndefinedError: | |
3826 | case kUndefinedDynamicLookup: | |
3827 | // always legal | |
3828 | break; | |
3829 | case kUndefinedWarning: | |
3830 | case kUndefinedSuppress: | |
3831 | // requires flat namespace | |
3832 | if ( fNameSpace == kTwoLevelNameSpace ) | |
3833 | throw "can't use -undefined warning or suppress with -twolevel_namespace"; | |
3834 | break; | |
3835 | } | |
d696c285 | 3836 | |
c2646906 A |
3837 | // unify -sub_umbrella with dylibs |
3838 | for (std::vector<const char*>::iterator it = fSubUmbellas.begin(); it != fSubUmbellas.end(); it++) { | |
3839 | const char* subUmbrella = *it; | |
3840 | bool found = false; | |
3841 | for (std::vector<Options::FileInfo>::iterator fit = fInputFiles.begin(); fit != fInputFiles.end(); fit++) { | |
3842 | Options::FileInfo& info = *fit; | |
3843 | const char* lastSlash = strrchr(info.path, '/'); | |
3844 | if ( lastSlash == NULL ) | |
3845 | lastSlash = info.path - 1; | |
3846 | if ( strcmp(&lastSlash[1], subUmbrella) == 0 ) { | |
3847 | info.options.fReExport = true; | |
3848 | found = true; | |
ebf6f434 | 3849 | fLinkSnapshot.recordSubUmbrella(info.path); |
c2646906 A |
3850 | break; |
3851 | } | |
3852 | } | |
2f2f92e4 A |
3853 | if ( ! found ) |
3854 | warning("-sub_umbrella %s does not match a supplied dylib", subUmbrella); | |
c2646906 | 3855 | } |
d696c285 | 3856 | |
c2646906 A |
3857 | // unify -sub_library with dylibs |
3858 | for (std::vector<const char*>::iterator it = fSubLibraries.begin(); it != fSubLibraries.end(); it++) { | |
3859 | const char* subLibrary = *it; | |
3860 | bool found = false; | |
3861 | for (std::vector<Options::FileInfo>::iterator fit = fInputFiles.begin(); fit != fInputFiles.end(); fit++) { | |
3862 | Options::FileInfo& info = *fit; | |
3863 | const char* lastSlash = strrchr(info.path, '/'); | |
3864 | if ( lastSlash == NULL ) | |
3865 | lastSlash = info.path - 1; | |
69a49097 | 3866 | const char* dot = strchr(&lastSlash[1], '.'); |
c2646906 A |
3867 | if ( dot == NULL ) |
3868 | dot = &lastSlash[strlen(lastSlash)]; | |
3869 | if ( strncmp(&lastSlash[1], subLibrary, dot-lastSlash-1) == 0 ) { | |
3870 | info.options.fReExport = true; | |
3871 | found = true; | |
ebf6f434 | 3872 | fLinkSnapshot.recordSubLibrary(info.path); |
c2646906 A |
3873 | break; |
3874 | } | |
3875 | } | |
2f2f92e4 A |
3876 | if ( ! found ) |
3877 | warning("-sub_library %s does not match a supplied dylib", subLibrary); | |
c2646906 | 3878 | } |
d696c285 | 3879 | |
c2646906 A |
3880 | // sync reader options |
3881 | if ( fNameSpace != kTwoLevelNameSpace ) | |
a645023d | 3882 | fFlatNamespace = true; |
c2646906 A |
3883 | |
3884 | // check -stack_addr | |
d696c285 | 3885 | if ( fStackAddr != 0 ) { |
c2646906 A |
3886 | switch (fArchitecture) { |
3887 | case CPU_TYPE_I386: | |
2f2f92e4 | 3888 | case CPU_TYPE_ARM: |
d696c285 | 3889 | if ( fStackAddr > 0xFFFFFFFF ) |
c2646906 A |
3890 | throw "-stack_addr must be < 4G for 32-bit processes"; |
3891 | break; | |
69a49097 | 3892 | case CPU_TYPE_X86_64: |
c2646906 A |
3893 | break; |
3894 | } | |
3895 | if ( (fStackAddr & -4096) != fStackAddr ) | |
3896 | throw "-stack_addr must be multiples of 4K"; | |
3897 | if ( fStackSize == 0 ) | |
3898 | throw "-stack_addr must be used with -stack_size"; | |
3899 | } | |
d696c285 | 3900 | |
c2646906 | 3901 | // check -stack_size |
d696c285 | 3902 | if ( fStackSize != 0 ) { |
c2646906 A |
3903 | switch (fArchitecture) { |
3904 | case CPU_TYPE_I386: | |
d696c285 | 3905 | if ( fStackSize > 0xFFFFFFFF ) |
c2646906 A |
3906 | throw "-stack_size must be < 4G for 32-bit processes"; |
3907 | if ( fStackAddr == 0 ) { | |
c2646906 A |
3908 | fStackAddr = 0xC0000000; |
3909 | } | |
2f2f92e4 A |
3910 | if ( (fStackAddr > 0xB0000000) && ((fStackAddr-fStackSize) < 0xB0000000) ) |
3911 | warning("custom stack placement overlaps and will disable shared region"); | |
c2646906 | 3912 | break; |
2f2f92e4 | 3913 | case CPU_TYPE_ARM: |
fb24a050 A |
3914 | if ( fStackSize > 0x2F000000 ) |
3915 | throw "-stack_size must be < 752MB"; | |
2f2f92e4 | 3916 | if ( fStackAddr == 0 ) |
fb24a050 A |
3917 | fStackAddr = 0x2F000000; |
3918 | if ( fStackAddr > 0x30000000) | |
3919 | throw "-stack_addr must be < 0x30000000 for arm"; | |
69a49097 | 3920 | case CPU_TYPE_X86_64: |
c2646906 | 3921 | if ( fStackAddr == 0 ) { |
a61fdf0a | 3922 | fStackAddr = 0x00007FFF5C000000LL; |
c2646906 A |
3923 | } |
3924 | break; | |
3925 | } | |
3926 | if ( (fStackSize & -4096) != fStackSize ) | |
3927 | throw "-stack_size must be multiples of 4K"; | |
3928 | switch ( fOutputKind ) { | |
3929 | case Options::kDynamicExecutable: | |
3930 | case Options::kStaticExecutable: | |
d696c285 | 3931 | // custom stack size only legal when building main executable |
c2646906 A |
3932 | break; |
3933 | case Options::kDynamicLibrary: | |
3934 | case Options::kDynamicBundle: | |
3935 | case Options::kObjectFile: | |
3936 | case Options::kDyld: | |
55e3d2f6 A |
3937 | case Options::kPreload: |
3938 | case Options::kKextBundle: | |
c2646906 A |
3939 | throw "-stack_size option can only be used when linking a main executable"; |
3940 | } | |
a645023d A |
3941 | if ( fStackSize > fStackAddr ) |
3942 | throwf("-stack_size (0x%08llX) must be smaller than -stack_addr (0x%08llX)", fStackSize, fStackAddr); | |
c2646906 | 3943 | } |
d696c285 A |
3944 | |
3945 | // check that -allow_stack_execute is only used with main executables | |
3946 | if ( fExecutableStack ) { | |
3947 | switch ( fOutputKind ) { | |
3948 | case Options::kDynamicExecutable: | |
3949 | case Options::kStaticExecutable: | |
3950 | // -allow_stack_execute size only legal when building main executable | |
3951 | break; | |
3952 | case Options::kDynamicLibrary: | |
3953 | case Options::kDynamicBundle: | |
3954 | case Options::kObjectFile: | |
3955 | case Options::kDyld: | |
55e3d2f6 A |
3956 | case Options::kPreload: |
3957 | case Options::kKextBundle: | |
d696c285 A |
3958 | throw "-allow_stack_execute option can only be used when linking a main executable"; |
3959 | } | |
3960 | } | |
3961 | ||
a645023d A |
3962 | // check that -allow_heap_execute is only used with i386 main executables |
3963 | if ( fDisableNonExecutableHeap ) { | |
3964 | if ( fArchitecture != CPU_TYPE_I386 ) | |
3965 | throw "-allow_heap_execute option can only be used when linking for i386"; | |
3966 | switch ( fOutputKind ) { | |
3967 | case Options::kDynamicExecutable: | |
3968 | // -allow_heap_execute only legal when building main executable | |
3969 | break; | |
3970 | case Options::kStaticExecutable: | |
3971 | case Options::kDynamicLibrary: | |
3972 | case Options::kDynamicBundle: | |
3973 | case Options::kObjectFile: | |
3974 | case Options::kDyld: | |
3975 | case Options::kPreload: | |
3976 | case Options::kKextBundle: | |
3977 | throw "-allow_heap_execute option can only be used when linking a main executable"; | |
3978 | } | |
3979 | } | |
3980 | ||
a61fdf0a A |
3981 | // check -client_name is only used when making a bundle or main executable |
3982 | if ( fClientName != NULL ) { | |
3983 | switch ( fOutputKind ) { | |
3984 | case Options::kDynamicExecutable: | |
3985 | case Options::kDynamicBundle: | |
3986 | break; | |
3987 | case Options::kStaticExecutable: | |
3988 | case Options::kDynamicLibrary: | |
3989 | case Options::kObjectFile: | |
3990 | case Options::kDyld: | |
55e3d2f6 A |
3991 | case Options::kPreload: |
3992 | case Options::kKextBundle: | |
a61fdf0a A |
3993 | throw "-client_name can only be used with -bundle"; |
3994 | } | |
3995 | } | |
3996 | ||
c2646906 A |
3997 | // check -init is only used when building a dylib |
3998 | if ( (fInitFunctionName != NULL) && (fOutputKind != Options::kDynamicLibrary) ) | |
3999 | throw "-init can only be used with -dynamiclib"; | |
c2646906 | 4000 | |
69a49097 A |
4001 | // check -bundle_loader only used with -bundle |
4002 | if ( (fBundleLoader != NULL) && (fOutputKind != Options::kDynamicBundle) ) | |
4003 | throw "-bundle_loader can only be used with -bundle"; | |
4004 | ||
a61fdf0a A |
4005 | // check -dtrace not used with -r |
4006 | if ( (fDtraceScriptName != NULL) && (fOutputKind == Options::kObjectFile) ) | |
4007 | throw "-dtrace can only be used when creating final linked images"; | |
4008 | ||
69a49097 | 4009 | // check -d can only be used with -r |
a645023d | 4010 | if ( fMakeTentativeDefinitionsReal && (fOutputKind != Options::kObjectFile) ) |
69a49097 | 4011 | throw "-d can only be used with -r"; |
a61fdf0a A |
4012 | |
4013 | // check that -root_safe is not used with -r | |
a645023d | 4014 | if ( fRootSafe && (fOutputKind == Options::kObjectFile) ) |
a61fdf0a A |
4015 | throw "-root_safe cannot be used with -r"; |
4016 | ||
4017 | // check that -setuid_safe is not used with -r | |
a645023d | 4018 | if ( fSetuidSafe && (fOutputKind == Options::kObjectFile) ) |
a61fdf0a A |
4019 | throw "-setuid_safe cannot be used with -r"; |
4020 | ||
a645023d A |
4021 | // rdar://problem/4718189 map ObjC class names to new runtime names |
4022 | bool alterObjC1ClassNamesToObjC2 = false; | |
4023 | switch (fArchitecture) { | |
4024 | case CPU_TYPE_I386: | |
4025 | // i386 only uses new symbols when using objc2 ABI | |
4026 | if ( fObjCABIVersion2Override ) | |
4027 | alterObjC1ClassNamesToObjC2 = true; | |
4028 | break; | |
a645023d A |
4029 | case CPU_TYPE_X86_64: |
4030 | case CPU_TYPE_ARM: | |
4031 | alterObjC1ClassNamesToObjC2 = true; | |
4032 | break; | |
4033 | } | |
4034 | ||
d696c285 | 4035 | // make sure all required exported symbols exist |
a61fdf0a | 4036 | std::vector<const char*> impliedExports; |
a645023d | 4037 | for (NameSet::iterator it=fExportSymbols.regularBegin(); it != fExportSymbols.regularEnd(); ++it) { |
d696c285 | 4038 | const char* name = *it; |
a61fdf0a | 4039 | const int len = strlen(name); |
a645023d A |
4040 | if ( (strcmp(&name[len-3], ".eh") == 0) || (strncmp(name, ".objc_category_name_", 20) == 0) ) { |
4041 | // never export .eh symbols | |
2f2f92e4 | 4042 | warning("ignoring %s in export list", name); |
a645023d A |
4043 | } |
4044 | else if ( (fArchitecture == CPU_TYPE_I386) && !fObjCABIVersion2Override && (strncmp(name, "_OBJC_CLASS_$", 13) == 0) ) { | |
4045 | warning("ignoring Objc2 Class symbol %s in i386 export list", name); | |
4046 | fRemovedExports.insert(name); | |
4047 | } | |
4048 | else if ( alterObjC1ClassNamesToObjC2 && (strncmp(name, ".objc_class_name_", 17) == 0) ) { | |
4049 | // linking ObjC2 ABI, but have ObjC1 ABI name in export list. Change it to intended name | |
4050 | fRemovedExports.insert(name); | |
4051 | char* temp; | |
4052 | asprintf(&temp, "_OBJC_CLASS_$_%s", &name[17]); | |
4053 | impliedExports.push_back(temp); | |
4054 | asprintf(&temp, "_OBJC_METACLASS_$_%s", &name[17]); | |
4055 | impliedExports.push_back(temp); | |
4056 | } | |
4057 | else { | |
d696c285 | 4058 | fInitialUndefines.push_back(name); |
a61fdf0a | 4059 | } |
d696c285 | 4060 | } |
a645023d A |
4061 | fExportSymbols.remove(fRemovedExports); |
4062 | for (std::vector<const char*>::iterator it=impliedExports.begin(); it != impliedExports.end(); ++it) { | |
a61fdf0a A |
4063 | const char* name = *it; |
4064 | fExportSymbols.insert(name); | |
4065 | fInitialUndefines.push_back(name); | |
4066 | } | |
4067 | ||
a645023d A |
4068 | // make sure all required re-exported symbols exist |
4069 | for (NameSet::iterator it=fReExportSymbols.regularBegin(); it != fReExportSymbols.regularEnd(); ++it) { | |
4070 | fInitialUndefines.push_back(*it); | |
4071 | } | |
4072 | ||
afe874b1 | 4073 | // make sure that -init symbol exists |
69a49097 A |
4074 | if ( fInitFunctionName != NULL ) |
4075 | fInitialUndefines.push_back(fInitFunctionName); | |
4076 | ||
afe874b1 A |
4077 | // make sure that entry symbol exists |
4078 | switch ( fOutputKind ) { | |
4079 | case Options::kDynamicExecutable: | |
4080 | case Options::kStaticExecutable: | |
4081 | case Options::kDyld: | |
4082 | case Options::kPreload: | |
4083 | fInitialUndefines.push_back(fEntryName); | |
4084 | break; | |
4085 | case Options::kDynamicLibrary: | |
4086 | case Options::kDynamicBundle: | |
4087 | case Options::kObjectFile: | |
4088 | case Options::kKextBundle: | |
4089 | break; | |
4090 | } | |
4091 | ||
a645023d A |
4092 | // make sure every alias base exists |
4093 | for (std::vector<AliasPair>::iterator it=fAliases.begin(); it != fAliases.end(); ++it) { | |
4094 | fInitialUndefines.push_back(it->realName); | |
4095 | } | |
4096 | ||
a61fdf0a A |
4097 | // check custom segments |
4098 | if ( fCustomSegmentAddresses.size() != 0 ) { | |
4099 | // verify no segment is in zero page | |
4100 | if ( fZeroPageSize != ULLONG_MAX ) { | |
4101 | for (std::vector<SegmentStart>::iterator it = fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { | |
a645023d | 4102 | if ( it->address < fZeroPageSize ) |
fb24a050 | 4103 | throwf("-segaddr %s 0x%llX conflicts with -pagezero_size", it->name, it->address); |
a61fdf0a A |
4104 | } |
4105 | } | |
4106 | // verify no duplicates | |
4107 | for (std::vector<SegmentStart>::iterator it = fCustomSegmentAddresses.begin(); it != fCustomSegmentAddresses.end(); ++it) { | |
4108 | for (std::vector<SegmentStart>::iterator it2 = fCustomSegmentAddresses.begin(); it2 != fCustomSegmentAddresses.end(); ++it2) { | |
4109 | if ( (it->address == it2->address) && (it != it2) ) | |
4110 | throwf("duplicate -segaddr addresses for %s and %s", it->name, it2->name); | |
4111 | } | |
4112 | // a custom segment address of zero will disable the use of a zero page | |
4113 | if ( it->address == 0 ) | |
4114 | fZeroPageSize = 0; | |
4115 | } | |
4116 | } | |
4117 | ||
69a49097 A |
4118 | if ( fZeroPageSize == ULLONG_MAX ) { |
4119 | // zero page size not specified on command line, set default | |
4120 | switch (fArchitecture) { | |
4121 | case CPU_TYPE_I386: | |
2f2f92e4 | 4122 | case CPU_TYPE_ARM: |
69a49097 A |
4123 | // first 4KB for 32-bit architectures |
4124 | fZeroPageSize = 0x1000; | |
4125 | break; | |
69a49097 A |
4126 | case CPU_TYPE_X86_64: |
4127 | // first 4GB for x86_64 on all OS's | |
4128 | fZeroPageSize = 0x100000000ULL; | |
4129 | break; | |
4130 | default: | |
4131 | // if -arch not used, default to 4K zero-page | |
4132 | fZeroPageSize = 0x1000; | |
4133 | } | |
4134 | } | |
4135 | else { | |
4136 | switch ( fOutputKind ) { | |
4137 | case Options::kDynamicExecutable: | |
4138 | case Options::kStaticExecutable: | |
4139 | // -pagezero_size size only legal when building main executable | |
4140 | break; | |
4141 | case Options::kDynamicLibrary: | |
4142 | case Options::kDynamicBundle: | |
4143 | case Options::kObjectFile: | |
4144 | case Options::kDyld: | |
55e3d2f6 A |
4145 | case Options::kPreload: |
4146 | case Options::kKextBundle: | |
a61fdf0a A |
4147 | if ( fZeroPageSize != 0 ) |
4148 | throw "-pagezero_size option can only be used when linking a main executable"; | |
4149 | } | |
69a49097 A |
4150 | } |
4151 | ||
a645023d A |
4152 | // if main executable with custom base address, model zero page as custom segment |
4153 | if ( (fOutputKind == Options::kDynamicExecutable) && (fBaseAddress != 0) && (fZeroPageSize != 0) ) { | |
4154 | SegmentStart seg; | |
4155 | seg.name = "__PAGEZERO"; | |
4156 | seg.address = 0;; | |
4157 | fCustomSegmentAddresses.push_back(seg); | |
4158 | } | |
4159 | ||
69a49097 | 4160 | // -dead_strip and -r are incompatible |
a645023d | 4161 | if ( fDeadStrip && (fOutputKind == Options::kObjectFile) ) |
2f2f92e4 | 4162 | throw "-r and -dead_strip cannot be used together"; |
c2646906 | 4163 | |
a61fdf0a A |
4164 | // can't use -rpath unless targeting 10.5 or later |
4165 | if ( fRPaths.size() > 0 ) { | |
afe874b1 | 4166 | if ( !minOS(ld::mac10_5, ld::iOS_2_0) ) |
2f2f92e4 | 4167 | throw "-rpath can only be used when targeting Mac OS X 10.5 or later"; |
a61fdf0a A |
4168 | switch ( fOutputKind ) { |
4169 | case Options::kDynamicExecutable: | |
4170 | case Options::kDynamicLibrary: | |
4171 | case Options::kDynamicBundle: | |
4172 | break; | |
4173 | case Options::kStaticExecutable: | |
4174 | case Options::kObjectFile: | |
4175 | case Options::kDyld: | |
55e3d2f6 A |
4176 | case Options::kPreload: |
4177 | case Options::kKextBundle: | |
a61fdf0a A |
4178 | throw "-rpath can only be used when creating a dynamic final linked image"; |
4179 | } | |
4180 | } | |
4181 | ||
a61fdf0a | 4182 | if ( fPositionIndependentExecutable ) { |
55e3d2f6 A |
4183 | switch ( fOutputKind ) { |
4184 | case Options::kDynamicExecutable: | |
ebf6f434 | 4185 | // check -pie is only used when building a dynamic main executable for 10.5 |
afe874b1 A |
4186 | if ( !minOS(ld::mac10_5, ld::iOS_4_2) ) { |
4187 | if ( fIOSVersionMin == ld::iOSVersionUnset ) | |
a645023d A |
4188 | throw "-pie can only be used when targeting Mac OS X 10.5 or later"; |
4189 | else | |
4190 | throw "-pie can only be used when targeting iOS 4.2 or later"; | |
4191 | } | |
d9246299 | 4192 | break; |
ebf6f434 | 4193 | case Options::kStaticExecutable: |
55e3d2f6 | 4194 | case Options::kPreload: |
ebf6f434 | 4195 | // -pie is ok with -static or -preload |
55e3d2f6 A |
4196 | break; |
4197 | case Options::kDynamicLibrary: | |
4198 | case Options::kDynamicBundle: | |
4199 | warning("-pie being ignored. It is only used when linking a main executable"); | |
a645023d | 4200 | fPositionIndependentExecutable = false; |
55e3d2f6 | 4201 | break; |
55e3d2f6 A |
4202 | case Options::kObjectFile: |
4203 | case Options::kDyld: | |
4204 | case Options::kKextBundle: | |
4205 | throw "-pie can only be used when linking a main executable"; | |
4206 | } | |
a61fdf0a | 4207 | } |
55e3d2f6 A |
4208 | |
4209 | // check -read_only_relocs is not used with x86_64 | |
4210 | if ( fAllowTextRelocs ) { | |
4211 | if ( (fArchitecture == CPU_TYPE_X86_64) && (fOutputKind != kKextBundle) ) { | |
4212 | warning("-read_only_relocs cannot be used with x86_64"); | |
4213 | fAllowTextRelocs = false; | |
4214 | } | |
4215 | } | |
4216 | ||
4217 | // check -mark_auto_dead_strip is only used with dylibs | |
4218 | if ( fMarkDeadStrippableDylib ) { | |
4219 | if ( fOutputKind != Options::kDynamicLibrary ) { | |
4220 | warning("-mark_auto_dead_strip can only be used when creating a dylib"); | |
4221 | fMarkDeadStrippableDylib = false; | |
4222 | } | |
4223 | } | |
4224 | ||
4be885f6 A |
4225 | // -force_cpusubtype_ALL is not supported for ARM |
4226 | if ( fForceSubtypeAll ) { | |
4227 | if ( fArchitecture == CPU_TYPE_ARM ) { | |
4228 | warning("-force_cpusubtype_ALL will become unsupported for ARM architectures"); | |
4229 | } | |
4230 | } | |
a645023d A |
4231 | |
4232 | // -reexported_symbols_list can only be used with -dynamiclib | |
4233 | if ( !fReExportSymbols.empty() ) { | |
4234 | if ( fOutputKind != Options::kDynamicLibrary ) | |
4235 | throw "-reexported_symbols_list can only used used when created dynamic libraries"; | |
afe874b1 | 4236 | if ( !minOS(ld::mac10_7, ld::iOS_4_2) ) |
a645023d A |
4237 | throw "targeted OS version does not support -reexported_symbols_list"; |
4238 | } | |
4239 | ||
4240 | // -dyld_env can only be used with main executables | |
4241 | if ( (fOutputKind != Options::kDynamicExecutable) && (fDyldEnvironExtras.size() != 0) ) | |
4242 | throw "-dyld_env can only used used when created main executables"; | |
55e3d2f6 | 4243 | } |
74cfe461 A |
4244 | |
4245 | ||
a61fdf0a A |
4246 | void Options::checkForClassic(int argc, const char* argv[]) |
4247 | { | |
4248 | // scan options | |
4249 | bool archFound = false; | |
4250 | bool staticFound = false; | |
4251 | bool dtraceFound = false; | |
55e3d2f6 | 4252 | bool kextFound = false; |
a61fdf0a A |
4253 | bool rFound = false; |
4254 | bool creatingMachKernel = false; | |
4255 | bool newLinker = false; | |
55e3d2f6 A |
4256 | |
4257 | // build command line buffer in case ld crashes | |
ebf6f434 A |
4258 | #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 |
4259 | CRSetCrashLogMessage(crashreporterBuffer); | |
4260 | #endif | |
a645023d A |
4261 | const char* srcRoot = getenv("SRCROOT"); |
4262 | if ( srcRoot != NULL ) { | |
4263 | strlcpy(crashreporterBuffer, "SRCROOT=", crashreporterBufferSize); | |
4264 | strlcat(crashreporterBuffer, srcRoot, crashreporterBufferSize); | |
4265 | strlcat(crashreporterBuffer, "\n", crashreporterBufferSize); | |
4266 | } | |
4267 | #ifdef LD_VERS | |
4268 | strlcat(crashreporterBuffer, LD_VERS, crashreporterBufferSize); | |
4269 | strlcat(crashreporterBuffer, "\n", crashreporterBufferSize); | |
4270 | #endif | |
4271 | strlcat(crashreporterBuffer, "ld ", crashreporterBufferSize); | |
55e3d2f6 | 4272 | for(int i=1; i < argc; ++i) { |
a645023d A |
4273 | strlcat(crashreporterBuffer, argv[i], crashreporterBufferSize); |
4274 | strlcat(crashreporterBuffer, " ", crashreporterBufferSize); | |
55e3d2f6 | 4275 | } |
74cfe461 | 4276 | |
a61fdf0a A |
4277 | for(int i=0; i < argc; ++i) { |
4278 | const char* arg = argv[i]; | |
4279 | if ( arg[0] == '-' ) { | |
4280 | if ( strcmp(arg, "-arch") == 0 ) { | |
4281 | parseArch(argv[++i]); | |
4282 | archFound = true; | |
4283 | } | |
4284 | else if ( strcmp(arg, "-static") == 0 ) { | |
4285 | staticFound = true; | |
4286 | } | |
55e3d2f6 A |
4287 | else if ( strcmp(arg, "-kext") == 0 ) { |
4288 | kextFound = true; | |
4289 | } | |
a61fdf0a A |
4290 | else if ( strcmp(arg, "-dtrace") == 0 ) { |
4291 | dtraceFound = true; | |
4292 | } | |
4293 | else if ( strcmp(arg, "-r") == 0 ) { | |
4294 | rFound = true; | |
4295 | } | |
4296 | else if ( strcmp(arg, "-new_linker") == 0 ) { | |
4297 | newLinker = true; | |
4298 | } | |
4299 | else if ( strcmp(arg, "-classic_linker") == 0 ) { | |
4300 | // ld_classic does not understand this option, so remove it | |
4301 | for(int j=i; j < argc; ++j) | |
4302 | argv[j] = argv[j+1]; | |
b2fa67a8 | 4303 | warning("using ld_classic"); |
a61fdf0a A |
4304 | this->gotoClassicLinker(argc-1, argv); |
4305 | } | |
4306 | else if ( strcmp(arg, "-o") == 0 ) { | |
4307 | const char* outfile = argv[++i]; | |
4308 | if ( (outfile != NULL) && (strstr(outfile, "/mach_kernel") != NULL) ) | |
4309 | creatingMachKernel = true; | |
4310 | } | |
4311 | } | |
4312 | } | |
a61fdf0a A |
4313 | } |
4314 | ||
4315 | void Options::gotoClassicLinker(int argc, const char* argv[]) | |
4316 | { | |
4317 | argv[0] = "ld_classic"; | |
afe874b1 A |
4318 | // ld_classic does not support -iphoneos_version_min, so change |
4319 | for(int j=0; j < argc; ++j) { | |
4320 | if ( (strcmp(argv[j], "-iphoneos_version_min") == 0) || (strcmp(argv[j], "-ios_version_min") == 0) ) { | |
4321 | argv[j] = "-macosx_version_min"; | |
4322 | if ( j < argc-1 ) | |
4323 | argv[j+1] = "10.5"; | |
4324 | break; | |
4325 | } | |
4326 | } | |
4327 | // ld classic does not understand -kext (change to -static -r) | |
4328 | for(int j=0; j < argc; ++j) { | |
4329 | if ( strcmp(argv[j], "-kext") == 0) | |
4330 | argv[j] = "-r"; | |
4331 | else if ( strcmp(argv[j], "-dynamic") == 0) | |
4332 | argv[j] = "-static"; | |
4333 | } | |
4334 | // ld classic does not understand -demangle | |
4335 | for(int j=0; j < argc; ++j) { | |
4336 | if ( strcmp(argv[j], "-demangle") == 0) | |
4337 | argv[j] = "-noprebind"; | |
4338 | } | |
a645023d A |
4339 | // in -v mode, print command line passed to ld_classic |
4340 | for(int i=0; i < argc; ++i) { | |
4341 | if ( strcmp(argv[i], "-v") == 0 ) { | |
4342 | for(int j=0; j < argc; ++j) | |
4343 | printf("%s ", argv[j]); | |
4344 | printf("\n"); | |
4345 | break; | |
4346 | } | |
4347 | } | |
fb24a050 | 4348 | char rawPath[PATH_MAX]; |
55e3d2f6 A |
4349 | char path[PATH_MAX]; |
4350 | uint32_t bufSize = PATH_MAX; | |
fb24a050 A |
4351 | if ( _NSGetExecutablePath(rawPath, &bufSize) != -1 ) { |
4352 | if ( realpath(rawPath, path) != NULL ) { | |
4353 | char* lastSlash = strrchr(path, '/'); | |
4354 | if ( lastSlash != NULL ) { | |
4355 | strcpy(lastSlash+1, "ld_classic"); | |
4356 | argv[0] = path; | |
4357 | execvp(path, (char**)argv); | |
4358 | } | |
55e3d2f6 A |
4359 | } |
4360 | } | |
4361 | // in case of error in above, try searching for ld_classic via PATH | |
a61fdf0a A |
4362 | execvp(argv[0], (char**)argv); |
4363 | fprintf(stderr, "can't exec ld_classic\n"); | |
4364 | exit(1); | |
4365 | } | |
ebf6f434 A |
4366 | |
4367 | ||
4368 | // Note, returned string buffer is own by this function. | |
4369 | // It should not be freed | |
4370 | // It will be reused, so clients need to strdup() if they want | |
4371 | // to use it long term. | |
4372 | const char* Options::demangleSymbol(const char* sym) const | |
4373 | { | |
4374 | // only try to demangle symbols if -demangle on command line | |
4375 | if ( !fDemangle ) | |
4376 | return sym; | |
4377 | ||
4378 | // only try to demangle symbols that look like C++ symbols | |
4379 | if ( strncmp(sym, "__Z", 3) != 0 ) | |
4380 | return sym; | |
4381 | ||
4382 | static size_t size = 1024; | |
4383 | static char* buff = (char*)malloc(size); | |
4384 | int status; | |
4385 | ||
4386 | char* result = abi::__cxa_demangle(&sym[1], buff, &size, &status); | |
4387 | if ( result != NULL ) { | |
4388 | // if demangling successful, keep buffer for next demangle | |
4389 | buff = result; | |
4390 | return buff; | |
4391 | } | |
4392 | return sym; | |
4393 | } | |
4394 |