1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
27 #include <sys/types.h>
30 #include <sys/sysctl.h>
35 #include <mach/mach_time.h>
36 #include <mach/vm_statistics.h>
37 #include <mach/mach_init.h>
38 #include <mach/mach_host.h>
40 #include <mach-o/dyld.h>
41 #include <mach-o/fat.h>
51 #include <AvailabilityMacros.h>
55 #include "Bitcode.hpp"
56 #include "InputFiles.h"
57 #include "SymbolTable.h"
59 #include "parsers/lto_file.h"
61 #include "configure.h"
64 #define STRINGIFY(x) VAL(x)
71 // An ExportAtom has no content. It exists so that the linker can track which imported
72 // symbols came from which dynamic libraries.
74 class UndefinedProxyAtom
: public ld::Atom
77 UndefinedProxyAtom(const char* nm
)
78 : ld::Atom(_s_section
, ld::Atom::definitionProxy
,
79 ld::Atom::combineNever
, ld::Atom::scopeLinkageUnit
,
80 ld::Atom::typeUnclassified
,
81 ld::Atom::symbolTableIn
, false, false, false, ld::Atom::Alignment(0)),
83 // overrides of ld::Atom
84 virtual const ld::File
* file() const { return NULL
; }
85 virtual const char* name() const { return _name
; }
86 virtual uint64_t size() const { return 0; }
87 virtual uint64_t objectAddress() const { return 0; }
88 virtual void copyRawContent(uint8_t buffer
[]) const { }
89 virtual void setScope(Scope
) { }
93 virtual ~UndefinedProxyAtom() {}
97 static ld::Section _s_section
;
100 ld::Section
UndefinedProxyAtom::_s_section("__TEXT", "__import", ld::Section::typeImportProxies
, true);
105 class AliasAtom
: public ld::Atom
108 AliasAtom(const ld::Atom
& target
, const char* nm
) :
109 ld::Atom(target
.section(), target
.definition(), ld::Atom::combineNever
,
110 ld::Atom::scopeGlobal
, target
.contentType(),
111 target
.symbolTableInclusion(), target
.dontDeadStrip(),
112 target
.isThumb(), true, target
.alignment()),
115 _fixup(0, ld::Fixup::k1of1
, ld::Fixup::kindNoneFollowOn
, &target
) { }
117 // overrides of ld::Atom
118 virtual const ld::File
* file() const { return _aliasOf
.file(); }
119 virtual const char* translationUnitSource() const
120 { return _aliasOf
.translationUnitSource(); }
121 virtual const char* name() const { return _name
; }
122 virtual uint64_t size() const { return 0; }
123 virtual uint64_t objectAddress() const { return _aliasOf
.objectAddress(); }
124 virtual void copyRawContent(uint8_t buffer
[]) const { }
125 virtual const uint8_t* rawContentPointer() const { return NULL
; }
126 virtual unsigned long contentHash(const class ld::IndirectBindingTable
& ibt
) const
127 { return _aliasOf
.contentHash(ibt
); }
128 virtual bool canCoalesceWith(const ld::Atom
& rhs
, const class ld::IndirectBindingTable
& ibt
) const
129 { return _aliasOf
.canCoalesceWith(rhs
,ibt
); }
130 virtual ld::Fixup::iterator
fixupsBegin() const { return (ld::Fixup
*)&_fixup
; }
131 virtual ld::Fixup::iterator
fixupsEnd() const { return &((ld::Fixup
*)&_fixup
)[1]; }
132 virtual ld::Atom::UnwindInfo::iterator
beginUnwind() const { return NULL
; }
133 virtual ld::Atom::UnwindInfo::iterator
endUnwind() const { return NULL
; }
134 virtual ld::Atom::LineInfo::iterator
beginLineInfo() const { return NULL
; }
135 virtual ld::Atom::LineInfo::iterator
endLineInfo() const { return NULL
; }
137 void setFinalAliasOf() const {
138 (const_cast<AliasAtom
*>(this))->setAttributesFromAtom(_aliasOf
);
139 (const_cast<AliasAtom
*>(this))->setScope(ld::Atom::scopeGlobal
);
144 const ld::Atom
& _aliasOf
;
150 class SectionBoundaryAtom
: public ld::Atom
153 static SectionBoundaryAtom
* makeSectionBoundaryAtom(const char* name
, bool start
, const char* segSectName
);
154 static SectionBoundaryAtom
* makeOldSectionBoundaryAtom(const char* name
, bool start
);
156 // overrides of ld::Atom
157 virtual const ld::File
* file() const { return NULL
; }
158 virtual const char* name() const { return _name
; }
159 virtual uint64_t size() const { return 0; }
160 virtual void copyRawContent(uint8_t buffer
[]) const { }
161 virtual const uint8_t* rawContentPointer() const { return NULL
; }
162 virtual uint64_t objectAddress() const { return 0; }
166 SectionBoundaryAtom(const char* nm
, const ld::Section
& sect
,
167 ld::Atom::ContentType cont
) :
169 ld::Atom::definitionRegular
,
170 ld::Atom::combineNever
,
171 ld::Atom::scopeLinkageUnit
,
173 ld::Atom::symbolTableNotIn
,
174 false, false, true, ld::Atom::Alignment(0)),
180 SectionBoundaryAtom
* SectionBoundaryAtom::makeSectionBoundaryAtom(const char* name
, bool start
, const char* segSectName
)
183 const char* segSectDividor
= strrchr(segSectName
, '$');
184 if ( segSectDividor
== NULL
)
185 throwf("malformed section$ symbol name: %s", name
);
186 const char* sectionName
= segSectDividor
+ 1;
187 int segNameLen
= segSectDividor
- segSectName
;
188 if ( segNameLen
> 16 )
189 throwf("malformed section$ symbol name: %s", name
);
191 strlcpy(segName
, segSectName
, segNameLen
+1);
193 const ld::Section
* section
= new ld::Section(strdup(segName
), sectionName
, ld::Section::typeUnclassified
);
194 return new SectionBoundaryAtom(name
, *section
, (start
? ld::Atom::typeSectionStart
: typeSectionEnd
));
197 SectionBoundaryAtom
* SectionBoundaryAtom::makeOldSectionBoundaryAtom(const char* name
, bool start
)
199 // e.g. __DATA__bss__begin
201 strlcpy(segName
, name
, 7);
204 int nameLen
= strlen(name
);
205 strlcpy(sectName
, &name
[6], (start
? nameLen
-12 : nameLen
-10));
206 warning("grandfathering in old symbol '%s' as alias for 'section$%s$%s$%s'", name
, start
? "start" : "end", segName
, sectName
);
207 const ld::Section
* section
= new ld::Section(strdup(segName
), strdup(sectName
), ld::Section::typeUnclassified
);
208 return new SectionBoundaryAtom(name
, *section
, (start
? ld::Atom::typeSectionStart
: typeSectionEnd
));
214 class SegmentBoundaryAtom
: public ld::Atom
217 static SegmentBoundaryAtom
* makeSegmentBoundaryAtom(const char* name
, bool start
, const char* segName
);
218 static SegmentBoundaryAtom
* makeOldSegmentBoundaryAtom(const char* name
, bool start
);
220 // overrides of ld::Atom
221 virtual const ld::File
* file() const { return NULL
; }
222 virtual const char* name() const { return _name
; }
223 virtual uint64_t size() const { return 0; }
224 virtual void copyRawContent(uint8_t buffer
[]) const { }
225 virtual const uint8_t* rawContentPointer() const { return NULL
; }
226 virtual uint64_t objectAddress() const { return 0; }
230 SegmentBoundaryAtom(const char* nm
, const ld::Section
& sect
,
231 ld::Atom::ContentType cont
) :
233 ld::Atom::definitionRegular
,
234 ld::Atom::combineNever
,
235 ld::Atom::scopeLinkageUnit
,
237 ld::Atom::symbolTableNotIn
,
238 false, false, true, ld::Atom::Alignment(0)),
244 SegmentBoundaryAtom
* SegmentBoundaryAtom::makeSegmentBoundaryAtom(const char* name
, bool start
, const char* segName
)
246 if ( *segName
== '\0' )
247 throwf("malformed segment$ symbol name: %s", name
);
248 if ( strlen(segName
) > 16 )
249 throwf("malformed segment$ symbol name: %s", name
);
252 const ld::Section
* section
= new ld::Section(segName
, "__start", ld::Section::typeFirstSection
, true);
253 return new SegmentBoundaryAtom(name
, *section
, ld::Atom::typeSectionStart
);
256 const ld::Section
* section
= new ld::Section(segName
, "__end", ld::Section::typeLastSection
, true);
257 return new SegmentBoundaryAtom(name
, *section
, ld::Atom::typeSectionEnd
);
261 SegmentBoundaryAtom
* SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(const char* name
, bool start
)
263 // e.g. __DATA__begin
265 strlcpy(temp
, name
, 7);
266 char* segName
= strdup(temp
);
268 warning("grandfathering in old symbol '%s' as alias for 'segment$%s$%s'", name
, start
? "start" : "end", segName
);
271 const ld::Section
* section
= new ld::Section(segName
, "__start", ld::Section::typeFirstSection
, true);
272 return new SegmentBoundaryAtom(name
, *section
, ld::Atom::typeSectionStart
);
275 const ld::Section
* section
= new ld::Section(segName
, "__end", ld::Section::typeLastSection
, true);
276 return new SegmentBoundaryAtom(name
, *section
, ld::Atom::typeSectionEnd
);
280 void Resolver::initializeState()
282 // set initial objc constraint based on command line options
283 if ( _options
.objcGc() )
284 _internal
.objcObjectConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
285 else if ( _options
.objcGcOnly() )
286 _internal
.objcObjectConstraint
= ld::File::objcConstraintGC
;
288 _internal
.cpuSubType
= _options
.subArchitecture();
289 _internal
.minOSVersion
= _options
.minOSversion();
290 _internal
.derivedPlatform
= 0;
292 // In -r mode, look for -linker_option additions
293 if ( _options
.outputKind() == Options::kObjectFile
) {
294 ld::relocatable::File::LinkerOptionsList lo
= _options
.linkerOptions();
295 for (relocatable::File::LinkerOptionsList::const_iterator it
=lo
.begin(); it
!= lo
.end(); ++it
) {
296 doLinkerOption(*it
, "command line");
299 #ifdef LD64_VERSION_NUM
300 uint32_t packedNum
= Options::parseVersionNumber32(STRINGIFY(LD64_VERSION_NUM
));
301 uint64_t combined
= (uint64_t)TOOL_LD
<< 32 | packedNum
;
302 _internal
.toolsVersions
.insert(combined
);
306 void Resolver::buildAtomList()
308 // each input files contributes initial atoms
309 _atoms
.reserve(1024);
310 _inputFiles
.forEachInitialAtom(*this, _internal
);
312 _completedInitialObjectFiles
= true;
314 //_symbolTable.printStatistics();
318 void Resolver::doLinkerOption(const std::vector
<const char*>& linkerOption
, const char* fileName
)
320 if ( linkerOption
.size() == 1 ) {
321 const char* lo1
= linkerOption
.front();
322 if ( strncmp(lo1
, "-l", 2) == 0) {
323 if (_internal
.linkerOptionLibraries
.count(&lo1
[2]) == 0) {
324 _internal
.unprocessedLinkerOptionLibraries
.insert(&lo1
[2]);
328 warning("unknown linker option from object file ignored: '%s' in %s", lo1
, fileName
);
331 else if ( linkerOption
.size() == 2 ) {
332 const char* lo2a
= linkerOption
[0];
333 const char* lo2b
= linkerOption
[1];
334 if ( strcmp(lo2a
, "-framework") == 0) {
335 if (_internal
.linkerOptionFrameworks
.count(lo2b
) == 0) {
336 _internal
.unprocessedLinkerOptionFrameworks
.insert(lo2b
);
340 warning("unknown linker option from object file ignored: '%s' '%s' from %s", lo2a
, lo2b
, fileName
);
344 warning("unknown linker option from object file ignored, starting with: '%s' from %s", linkerOption
.front(), fileName
);
349 void Resolver::doFile(const ld::File
& file
)
351 const ld::relocatable::File
* objFile
= dynamic_cast<const ld::relocatable::File
*>(&file
);
352 const ld::dylib::File
* dylibFile
= dynamic_cast<const ld::dylib::File
*>(&file
);
354 if ( objFile
!= NULL
) {
355 // if file has linker options, process them
356 ld::relocatable::File::LinkerOptionsList
* lo
= objFile
->linkerOptions();
357 if ( lo
!= NULL
&& !_options
.ignoreAutoLink() ) {
358 for (relocatable::File::LinkerOptionsList::const_iterator it
=lo
->begin(); it
!= lo
->end(); ++it
) {
359 this->doLinkerOption(*it
, file
.path());
361 // <rdar://problem/23053404> process any additional linker-options introduced by this new archive member being loaded
362 if ( _completedInitialObjectFiles
) {
363 _inputFiles
.addLinkerOptionLibraries(_internal
, *this);
364 _inputFiles
.createIndirectDylibs();
367 // Resolve bitcode section in the object file
368 if ( _options
.bundleBitcode() ) {
369 if ( objFile
->getBitcode() == NULL
) {
370 // Handle the special case for compiler_rt objects. Add the file to the list to be process.
371 if ( objFile
->sourceKind() == ld::relocatable::File::kSourceCompilerArchive
) {
372 _internal
.filesFromCompilerRT
.push_back(objFile
);
373 } else if (objFile
->sourceKind() != ld::relocatable::File::kSourceLTO
) {
374 // No bitcode section, figure out if the object file comes from LTO/compiler static library
375 switch ( _options
.platform() ) {
376 case Options::kPlatformOSX
:
377 case Options::kPlatform_bridgeOS
:
378 case Options::kPlatformUnknown
:
379 warning("all bitcode will be dropped because '%s' was built without bitcode. "
380 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target. ", file
.path());
381 _internal
.filesWithBitcode
.clear();
382 _internal
.dropAllBitcode
= true;
384 case Options::kPlatformiOS
:
385 throwf("'%s' does not contain bitcode. "
386 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target.", file
.path());
388 case Options::kPlatformWatchOS
:
390 case Options::kPlatform_tvOS
:
392 throwf("'%s' does not contain bitcode. "
393 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) or obtain an updated library from the vendor", file
.path());
398 // contains bitcode, check if it is just a marker
399 if ( objFile
->getBitcode()->isMarker() ) {
400 // if -bitcode_verify_bundle is used, check if all the object files participate in the linking have full bitcode embedded.
401 // error on any marker encountered.
402 if ( _options
.verifyBitcode() )
403 throwf("bitcode bundle could not be generated because '%s' was built without full bitcode. "
404 "All object files and libraries for bitcode must be generated from Xcode Archive or Install build",
406 // update the internal state that marker is encountered.
407 _internal
.embedMarkerOnly
= true;
408 _internal
.filesWithBitcode
.clear();
409 _internal
.dropAllBitcode
= true;
410 } else if ( !_internal
.dropAllBitcode
)
411 _internal
.filesWithBitcode
.push_back(objFile
);
415 // update which form of ObjC is being used
416 switch ( file
.objCConstraint() ) {
417 case ld::File::objcConstraintNone
:
419 case ld::File::objcConstraintRetainRelease
:
420 if ( _internal
.objcObjectConstraint
== ld::File::objcConstraintGC
)
421 throwf("%s built with incompatible Garbage Collection settings to link with previous .o files", file
.path());
422 if ( _options
.objcGcOnly() )
423 throwf("command line specified -objc_gc_only, but file is retain/release based: %s", file
.path());
424 if ( _options
.objcGc() )
425 throwf("command line specified -objc_gc, but file is retain/release based: %s", file
.path());
426 if ( !_options
.targetIOSSimulator() && (_internal
.objcObjectConstraint
!= ld::File::objcConstraintRetainReleaseForSimulator
) )
427 _internal
.objcObjectConstraint
= ld::File::objcConstraintRetainRelease
;
429 case ld::File::objcConstraintRetainReleaseOrGC
:
430 if ( _internal
.objcObjectConstraint
== ld::File::objcConstraintNone
)
431 _internal
.objcObjectConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
432 if ( _options
.targetIOSSimulator() )
433 warning("linking ObjC for iOS Simulator, but object file (%s) was compiled for MacOSX", file
.path());
435 case ld::File::objcConstraintGC
:
436 if ( _internal
.objcObjectConstraint
== ld::File::objcConstraintRetainRelease
)
437 throwf("%s built with incompatible Garbage Collection settings to link with previous .o files", file
.path());
438 _internal
.objcObjectConstraint
= ld::File::objcConstraintGC
;
439 if ( _options
.targetIOSSimulator() )
440 warning("linking ObjC for iOS Simulator, but object file (%s) was compiled for MacOSX", file
.path());
442 case ld::File::objcConstraintRetainReleaseForSimulator
:
443 if ( _internal
.objcObjectConstraint
== ld::File::objcConstraintNone
) {
444 if ( !_options
.targetIOSSimulator() && (_options
.outputKind() != Options::kObjectFile
) )
445 warning("ObjC object file (%s) was compiled for iOS Simulator, but linking for MacOSX", file
.path());
446 _internal
.objcObjectConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
448 else if ( _internal
.objcObjectConstraint
!= ld::File::objcConstraintRetainReleaseForSimulator
) {
449 _internal
.objcObjectConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
454 // verify all files use same version of Swift language
455 if ( file
.swiftVersion() != 0 ) {
456 if ( _internal
.swiftVersion
== 0 ) {
457 _internal
.swiftVersion
= file
.swiftVersion();
459 else if ( file
.swiftVersion() != _internal
.swiftVersion
) {
460 char fileVersion
[64];
461 char otherVersion
[64];
462 Options::userReadableSwiftVersion(file
.swiftVersion(), fileVersion
);
463 Options::userReadableSwiftVersion(_internal
.swiftVersion
, otherVersion
);
464 if ( file
.swiftVersion() > _internal
.swiftVersion
) {
465 if ( _options
.warnOnSwiftABIVersionMismatches() ) {
466 warning("%s compiled with newer version of Swift language (%s) than previous files (%s)",
467 file
.path(), fileVersion
, otherVersion
);
469 throwf("%s compiled with newer version of Swift language (%s) than previous files (%s)",
470 file
.path(), fileVersion
, otherVersion
);
474 if ( _options
.warnOnSwiftABIVersionMismatches() ) {
475 warning("%s compiled with older version of Swift language (%s) than previous files (%s)",
476 file
.path(), fileVersion
, otherVersion
);
478 throwf("%s compiled with older version of Swift language (%s) than previous files (%s)",
479 file
.path(), fileVersion
, otherVersion
);
485 // in -r mode, if any .o files have dwarf then add UUID to output .o file
486 if ( objFile
->debugInfo() == ld::relocatable::File::kDebugInfoDwarf
)
487 _internal
.someObjectFileHasDwarf
= true;
489 // remember if any .o file did not have MH_SUBSECTIONS_VIA_SYMBOLS bit set
490 if ( ! objFile
->canScatterAtoms() )
491 _internal
.allObjectFilesScatterable
= false;
493 // remember if building for profiling (so we don't warn about initializers)
494 if ( objFile
->hasllvmProfiling() )
495 _havellvmProfiling
= true;
497 // update minOSVersion off all .o files
498 uint32_t objMinOS
= objFile
->minOSVersion();
500 _internal
.objectFileFoundWithNoVersion
= true;
501 if ( (_options
.outputKind() == Options::kObjectFile
) && (objMinOS
> _internal
.minOSVersion
) )
502 _internal
.minOSVersion
= objMinOS
;
504 uint32_t objPlatform
= objFile
->platform();
505 if ( (objPlatform
!= 0) && (_options
.outputKind() == Options::kObjectFile
) && (_internal
.derivedPlatform
== 0) )
506 _internal
.derivedPlatform
= objPlatform
;
508 // update set of known tools used
509 for (const std::pair
<uint32_t,uint32_t>& entry
: objFile
->toolVersions()) {
510 uint64_t combined
= (uint64_t)entry
.first
<< 32 | entry
.second
;
511 _internal
.toolsVersions
.insert(combined
);
514 // update cpu-sub-type
515 cpu_subtype_t nextObjectSubType
= file
.cpuSubType();
516 switch ( _options
.architecture() ) {
518 if ( _options
.subArchitecture() != nextObjectSubType
) {
519 if ( (_options
.subArchitecture() == CPU_SUBTYPE_ARM_ALL
) && _options
.forceCpuSubtypeAll() ) {
520 // hack to support gcc multillib build that tries to make sub-type-all slice
522 else if ( nextObjectSubType
== CPU_SUBTYPE_ARM_ALL
) {
523 warning("CPU_SUBTYPE_ARM_ALL subtype is deprecated: %s", file
.path());
525 else if ( _options
.allowSubArchitectureMismatches() ) {
526 //warning("object file %s was built for different arm sub-type (%d) than link command line (%d)",
527 // file.path(), nextObjectSubType, _options.subArchitecture());
530 throwf("object file %s was built for different arm sub-type (%d) than link command line (%d)",
531 file
.path(), nextObjectSubType
, _options
.subArchitecture());
537 if ( _options
.subArchitecture() != nextObjectSubType
) {
538 if ( _options
.allowSubArchitectureMismatches() ) {
539 warning("object file %s was built for different arm64 sub-type (%d) than link command line (%d)",
540 file
.path(), nextObjectSubType
, _options
.subArchitecture());
543 throwf("object file %s was built for different arm64 sub-type (%d) than link command line (%d)",
544 file
.path(), nextObjectSubType
, _options
.subArchitecture());
550 _internal
.cpuSubType
= CPU_SUBTYPE_I386_ALL
;
553 case CPU_TYPE_X86_64
:
554 if ( _options
.subArchitecture() != nextObjectSubType
) {
555 if ( _options
.allowSubArchitectureMismatches() ) {
556 warning("object file %s was built for different x86_64 sub-type (%d) than link command line (%d)",
557 file
.path(), nextObjectSubType
, _options
.subArchitecture());
560 throwf("object file %s was built for different x86_64 sub-type (%d) than link command line (%d)",
561 file
.path(), nextObjectSubType
, _options
.subArchitecture());
567 if ( dylibFile
!= NULL
) {
568 // Check dylib for bitcode, if the library install path is relative path or @rpath, it has to contain bitcode
569 if ( _options
.bundleBitcode() ) {
570 bool isSystemFramework
= ( dylibFile
->installPath() != NULL
) && ( dylibFile
->installPath()[0] == '/' );
571 if ( dylibFile
->getBitcode() == NULL
&& !isSystemFramework
) {
572 // Check if the dylib is from toolchain by checking the path
573 char tcLibPath
[PATH_MAX
];
574 char ldPath
[PATH_MAX
];
575 char tempPath
[PATH_MAX
];
576 uint32_t bufSize
= PATH_MAX
;
577 // toolchain library path should pointed to *.xctoolchain/usr/lib
578 if ( _NSGetExecutablePath(ldPath
, &bufSize
) != -1 ) {
579 if ( realpath(ldPath
, tempPath
) != NULL
) {
580 char* lastSlash
= strrchr(tempPath
, '/');
581 if ( lastSlash
!= NULL
)
582 strcpy(lastSlash
, "/../lib");
585 // Compare toolchain library path to the dylib path
586 if ( realpath(tempPath
, tcLibPath
) == NULL
||
587 realpath(dylibFile
->path(), tempPath
) == NULL
||
588 strncmp(tcLibPath
, tempPath
, strlen(tcLibPath
)) != 0 ) {
589 switch ( _options
.platform() ) {
590 case Options::kPlatformOSX
:
591 case Options::kPlatform_bridgeOS
:
592 case Options::kPlatformUnknown
:
593 warning("all bitcode will be dropped because '%s' was built without bitcode. "
594 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target.", file
.path());
595 _internal
.filesWithBitcode
.clear();
596 _internal
.dropAllBitcode
= true;
598 case Options::kPlatformiOS
:
599 throwf("'%s' does not contain bitcode. "
600 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE), obtain an updated library from the vendor, or disable bitcode for this target.", file
.path());
602 case Options::kPlatformWatchOS
:
604 case Options::kPlatform_tvOS
:
606 throwf("'%s' does not contain bitcode. "
607 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) or obtain an updated library from the vendor", file
.path());
612 // Error on bitcode marker in non-system frameworks if -bitcode_verify is used
613 if ( _options
.verifyBitcode() && !isSystemFramework
&&
614 dylibFile
->getBitcode() != NULL
&& dylibFile
->getBitcode()->isMarker() )
615 throwf("bitcode bundle could not be generated because '%s' was built without full bitcode. "
616 "All frameworks and dylibs for bitcode must be generated from Xcode Archive or Install build",
620 // update which form of ObjC dylibs are being linked
621 switch ( dylibFile
->objCConstraint() ) {
622 case ld::File::objcConstraintNone
:
624 case ld::File::objcConstraintRetainRelease
:
625 if ( _internal
.objcDylibConstraint
== ld::File::objcConstraintGC
)
626 throwf("%s built with incompatible Garbage Collection settings to link with previous dylibs", file
.path());
627 if ( _options
.objcGcOnly() )
628 throwf("command line specified -objc_gc_only, but dylib is retain/release based: %s", file
.path());
629 if ( _options
.objcGc() )
630 throwf("command line specified -objc_gc, but dylib is retain/release based: %s", file
.path());
631 if ( _options
.targetIOSSimulator() )
632 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file
.path());
633 _internal
.objcDylibConstraint
= ld::File::objcConstraintRetainRelease
;
635 case ld::File::objcConstraintRetainReleaseOrGC
:
636 if ( _internal
.objcDylibConstraint
== ld::File::objcConstraintNone
)
637 _internal
.objcDylibConstraint
= ld::File::objcConstraintRetainReleaseOrGC
;
638 if ( _options
.targetIOSSimulator() )
639 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file
.path());
641 case ld::File::objcConstraintGC
:
642 if ( _internal
.objcDylibConstraint
== ld::File::objcConstraintRetainRelease
)
643 throwf("%s built with incompatible Garbage Collection settings to link with previous dylibs", file
.path());
644 if ( _options
.targetIOSSimulator() )
645 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file
.path());
646 _internal
.objcDylibConstraint
= ld::File::objcConstraintGC
;
648 case ld::File::objcConstraintRetainReleaseForSimulator
:
649 if ( _internal
.objcDylibConstraint
== ld::File::objcConstraintNone
)
650 _internal
.objcDylibConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
651 else if ( _internal
.objcDylibConstraint
!= ld::File::objcConstraintRetainReleaseForSimulator
) {
652 warning("ObjC dylib (%s) was compiled for iOS Simulator, but dylibs others were compiled for MacOSX", file
.path());
653 _internal
.objcDylibConstraint
= ld::File::objcConstraintRetainReleaseForSimulator
;
658 // <rdar://problem/25680358> verify dylibs use same version of Swift language
659 if ( file
.swiftVersion() != 0 ) {
660 if ( _internal
.swiftVersion
== 0 ) {
661 _internal
.swiftVersion
= file
.swiftVersion();
663 else if ( file
.swiftVersion() != _internal
.swiftVersion
) {
664 char fileVersion
[64];
665 char otherVersion
[64];
666 Options::userReadableSwiftVersion(file
.swiftVersion(), fileVersion
);
667 Options::userReadableSwiftVersion(_internal
.swiftVersion
, otherVersion
);
668 if ( file
.swiftVersion() > _internal
.swiftVersion
) {
669 if ( _options
.warnOnSwiftABIVersionMismatches() ) {
670 warning("%s compiled with newer version of Swift language (%s) than previous files (%s)",
671 file
.path(), fileVersion
, otherVersion
);
673 throwf("%s compiled with newer version of Swift language (%s) than previous files (%s)",
674 file
.path(), fileVersion
, otherVersion
);
678 if ( _options
.warnOnSwiftABIVersionMismatches() ) {
679 warning("%s compiled with older version of Swift language (%s) than previous files (%s)",
680 file
.path(), fileVersion
, otherVersion
);
682 throwf("%s compiled with older version of Swift language (%s) than previous files (%s)",
683 file
.path(), fileVersion
, otherVersion
);
689 if ( _options
.checkDylibsAreAppExtensionSafe() && !dylibFile
->appExtensionSafe() ) {
690 warning("linking against a dylib which is not safe for use in application extensions: %s", file
.path());
692 const char* depInstallName
= dylibFile
->installPath();
693 // <rdar://problem/17229513> embedded frameworks are only supported on iOS 8 and later
694 if ( (depInstallName
!= NULL
) && (depInstallName
[0] != '/') ) {
695 if ( (_options
.iOSVersionMin() != iOSVersionUnset
) && (_options
.iOSVersionMin() < iOS_8_0
) ) {
696 // <rdar://problem/17598404> only warn about linking against embedded dylib if it is built for iOS 8 or later
697 if ( dylibFile
->minOSVersion() >= iOS_8_0
)
698 throwf("embedded dylibs/frameworks are only supported on iOS 8.0 and later (%s)", depInstallName
);
701 if ( _options
.sharedRegionEligible() ) {
702 assert(depInstallName
!= NULL
);
703 if ( depInstallName
[0] == '@' ) {
704 warning("invalid -install_name (%s) in dependent dylib (%s). Dylibs/frameworks which might go in dyld shared cache "
705 "cannot link with dylib that uses @rpath, @loader_path, etc.", depInstallName
, dylibFile
->path());
706 } else if ( (strncmp(depInstallName
, "/usr/lib/", 9) != 0) && (strncmp(depInstallName
, "/System/Library/", 16) != 0) ) {
707 warning("invalid -install_name (%s) in dependent dylib (%s). Dylibs/frameworks which might go in dyld shared cache "
708 "cannot link with dylibs that won't be in the shared cache", depInstallName
, dylibFile
->path());
715 void Resolver::doAtom(const ld::Atom
& atom
)
717 //fprintf(stderr, "Resolver::doAtom(%p), name=%s, sect=%s, scope=%d\n", &atom, atom.name(), atom.section().sectionName(), atom.scope());
718 if ( _ltoCodeGenFinished
&& (atom
.contentType() == ld::Atom::typeLTOtemporary
) && (atom
.scope() != ld::Atom::scopeTranslationUnit
) )
719 warning("'%s' is implemented in bitcode, but it was loaded too late", atom
.name());
721 // add to list of known atoms
722 _atoms
.push_back(&atom
);
725 if ( _options
.hasExportRestrictList() || _options
.hasReExportList() ) {
726 const char* name
= atom
.name();
727 switch ( atom
.scope() ) {
728 case ld::Atom::scopeTranslationUnit
:
730 case ld::Atom::scopeLinkageUnit
:
731 if ( _options
.hasExportMaskList() && _options
.shouldExport(name
) ) {
732 // <rdar://problem/5062685> ld does not report error when -r is used and exported symbols are not defined.
733 if ( _options
.outputKind() == Options::kObjectFile
)
734 throwf("cannot export hidden symbol %s", name
);
735 // .objc_class_name_* symbols are special
736 if ( atom
.section().type() != ld::Section::typeObjC1Classes
) {
737 if ( atom
.definition() == ld::Atom::definitionProxy
) {
738 // .exp file says to export a symbol, but that symbol is in some dylib being linked
739 if ( _options
.canReExportSymbols() ) {
740 // marking proxy atom as global triggers the re-export
741 (const_cast<ld::Atom
*>(&atom
))->setScope(ld::Atom::scopeGlobal
);
743 else if ( _options
.outputKind() == Options::kDynamicLibrary
) {
744 if ( atom
.file() != NULL
)
745 warning("target OS does not support re-exporting symbol %s from %s\n", _options
.demangleSymbol(name
), atom
.safeFilePath());
747 warning("target OS does not support re-exporting symbol %s\n", _options
.demangleSymbol(name
));
751 if ( atom
.file() != NULL
)
752 warning("cannot export hidden symbol %s from %s", _options
.demangleSymbol(name
), atom
.safeFilePath());
754 warning("cannot export hidden symbol %s", _options
.demangleSymbol(name
));
758 else if ( _options
.shouldReExport(name
) && _options
.canReExportSymbols() ) {
759 if ( atom
.definition() == ld::Atom::definitionProxy
) {
760 // marking proxy atom as global triggers the re-export
761 (const_cast<ld::Atom
*>(&atom
))->setScope(ld::Atom::scopeGlobal
);
764 throwf("requested re-export symbol %s is not from a dylib, but from %s\n", _options
.demangleSymbol(name
), atom
.safeFilePath());
768 case ld::Atom::scopeGlobal
:
769 // check for globals that are downgraded to hidden
770 if ( ! _options
.shouldExport(name
) ) {
771 (const_cast<ld::Atom
*>(&atom
))->setScope(ld::Atom::scopeLinkageUnit
);
772 //fprintf(stderr, "demote %s to hidden\n", name);
774 if ( _options
.canReExportSymbols() && _options
.shouldReExport(name
) ) {
775 throwf("requested re-export symbol %s is not from a dylib, but from %s\n", _options
.demangleSymbol(name
), atom
.safeFilePath());
781 // work around for kernel that uses 'l' labels in assembly code
782 if ( (atom
.symbolTableInclusion() == ld::Atom::symbolTableNotInFinalLinkedImages
)
783 && (atom
.name()[0] == 'l') && (_options
.outputKind() == Options::kStaticExecutable
)
784 && (strncmp(atom
.name(), "ltmp", 4) != 0) )
785 (const_cast<ld::Atom
*>(&atom
))->setSymbolTableInclusion(ld::Atom::symbolTableIn
);
788 // tell symbol table about non-static atoms
789 if ( atom
.scope() != ld::Atom::scopeTranslationUnit
) {
790 _symbolTable
.add(atom
, _options
.deadCodeStrip() && (_completedInitialObjectFiles
|| _options
.allowDeadDuplicates()));
792 // add symbol aliases defined on the command line
793 if ( _options
.haveCmdLineAliases() ) {
794 const std::vector
<Options::AliasPair
>& aliases
= _options
.cmdLineAliases();
795 for (std::vector
<Options::AliasPair
>::const_iterator it
=aliases
.begin(); it
!= aliases
.end(); ++it
) {
796 if ( strcmp(it
->realName
, atom
.name()) == 0 ) {
797 if ( strcmp(it
->realName
, it
->alias
) == 0 ) {
798 warning("ignoring alias of itself '%s'", it
->realName
);
801 const AliasAtom
* alias
= new AliasAtom(atom
, it
->alias
);
802 _aliasesFromCmdLine
.push_back(alias
);
803 this->doAtom(*alias
);
810 // convert references by-name or by-content to by-slot
811 this->convertReferencesToIndirect(atom
);
813 // remember if any atoms are proxies that require LTO
814 if ( atom
.contentType() == ld::Atom::typeLTOtemporary
)
815 _haveLLVMObjs
= true;
817 // remember if any atoms are aliases
818 if ( atom
.section().type() == ld::Section::typeTempAlias
)
821 // error or warn about initializers
822 if ( (atom
.section().type() == ld::Section::typeInitializerPointers
) && !_havellvmProfiling
) {
823 switch ( _options
.initializersTreatment() ) {
824 case Options::kError
:
825 throwf("static initializer found in '%s'",atom
.safeFilePath());
826 case Options::kWarning
:
827 warning("static initializer found in '%s'. Use -no_inits to make this an error. Use -no_warn_inits to suppress warning",atom
.safeFilePath());
834 if ( _options
.deadCodeStrip() ) {
835 // add to set of dead-strip-roots, all symbols that the compiler marks as don't strip
836 if ( atom
.dontDeadStrip() )
837 _deadStripRoots
.insert(&atom
);
838 else if ( atom
.dontDeadStripIfReferencesLive() )
839 _dontDeadStripIfReferencesLive
.push_back(&atom
);
841 if ( atom
.scope() == ld::Atom::scopeGlobal
) {
842 // <rdar://problem/5524973> -exported_symbols_list that has wildcards and -dead_strip
843 // in dylibs, every global atom in initial .o files is a root
844 if ( _options
.hasWildCardExportRestrictList() || _options
.allGlobalsAreDeadStripRoots() ) {
845 if ( _options
.shouldExport(atom
.name()) )
846 _deadStripRoots
.insert(&atom
);
852 bool Resolver::isDtraceProbe(ld::Fixup::Kind kind
)
855 case ld::Fixup::kindStoreX86DtraceCallSiteNop
:
856 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear
:
857 case ld::Fixup::kindStoreARMDtraceCallSiteNop
:
858 case ld::Fixup::kindStoreARMDtraceIsEnableSiteClear
:
859 case ld::Fixup::kindStoreARM64DtraceCallSiteNop
:
860 case ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear
:
861 case ld::Fixup::kindStoreThumbDtraceCallSiteNop
:
862 case ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear
:
863 case ld::Fixup::kindDtraceExtra
:
871 void Resolver::convertReferencesToIndirect(const ld::Atom
& atom
)
873 // convert references by-name or by-content to by-slot
874 SymbolTable::IndirectBindingSlot slot
;
875 const ld::Atom
* dummy
;
876 ld::Fixup::iterator end
= atom
.fixupsEnd();
877 for (ld::Fixup::iterator fit
=atom
.fixupsBegin(); fit
!= end
; ++fit
) {
878 if ( fit
->kind
== ld::Fixup::kindLinkerOptimizationHint
)
879 _internal
.someObjectHasOptimizationHints
= true;
880 switch ( fit
->binding
) {
881 case ld::Fixup::bindingByNameUnbound
:
882 if ( isDtraceProbe(fit
->kind
) && (_options
.outputKind() != Options::kObjectFile
) ) {
883 // in final linked images, remove reference
884 fit
->binding
= ld::Fixup::bindingNone
;
887 slot
= _symbolTable
.findSlotForName(fit
->u
.name
);
888 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
889 fit
->u
.bindingIndex
= slot
;
892 case ld::Fixup::bindingByContentBound
:
893 switch ( fit
->u
.target
->combine() ) {
894 case ld::Atom::combineNever
:
895 case ld::Atom::combineByName
:
896 assert(0 && "wrong combine type for bind by content");
898 case ld::Atom::combineByNameAndContent
:
899 slot
= _symbolTable
.findSlotForContent(fit
->u
.target
, &dummy
);
900 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
901 fit
->u
.bindingIndex
= slot
;
903 case ld::Atom::combineByNameAndReferences
:
904 slot
= _symbolTable
.findSlotForReferences(fit
->u
.target
, &dummy
);
905 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
906 fit
->u
.bindingIndex
= slot
;
910 case ld::Fixup::bindingNone
:
911 case ld::Fixup::bindingDirectlyBound
:
912 case ld::Fixup::bindingsIndirectlyBound
:
919 void Resolver::addInitialUndefines()
921 // add initial undefines from -u option
922 for (Options::UndefinesIterator it
=_options
.initialUndefinesBegin(); it
!= _options
.initialUndefinesEnd(); ++it
) {
923 _symbolTable
.findSlotForName(*it
);
927 void Resolver::resolveUndefines()
929 // keep looping until no more undefines were added in last loop
930 unsigned int undefineGenCount
= 0xFFFFFFFF;
931 while ( undefineGenCount
!= _symbolTable
.updateCount() ) {
932 undefineGenCount
= _symbolTable
.updateCount();
933 std::vector
<const char*> undefineNames
;
934 _symbolTable
.undefines(undefineNames
);
935 for(std::vector
<const char*>::iterator it
= undefineNames
.begin(); it
!= undefineNames
.end(); ++it
) {
936 const char* undef
= *it
;
937 // load for previous undefine may also have loaded this undefine, so check again
938 if ( ! _symbolTable
.hasName(undef
) ) {
939 _inputFiles
.searchLibraries(undef
, true, true, false, *this);
940 if ( !_symbolTable
.hasName(undef
) && (_options
.outputKind() != Options::kObjectFile
) ) {
941 if ( strncmp(undef
, "section$", 8) == 0 ) {
942 if ( strncmp(undef
, "section$start$", 14) == 0 ) {
943 this->doAtom(*SectionBoundaryAtom::makeSectionBoundaryAtom(undef
, true, &undef
[14]));
945 else if ( strncmp(undef
, "section$end$", 12) == 0 ) {
946 this->doAtom(*SectionBoundaryAtom::makeSectionBoundaryAtom(undef
, false, &undef
[12]));
949 else if ( strncmp(undef
, "segment$", 8) == 0 ) {
950 if ( strncmp(undef
, "segment$start$", 14) == 0 ) {
951 this->doAtom(*SegmentBoundaryAtom::makeSegmentBoundaryAtom(undef
, true, &undef
[14]));
953 else if ( strncmp(undef
, "segment$end$", 12) == 0 ) {
954 this->doAtom(*SegmentBoundaryAtom::makeSegmentBoundaryAtom(undef
, false, &undef
[12]));
957 else if ( _options
.outputKind() == Options::kPreload
) {
958 // for iBoot grandfather in old style section labels
959 int undefLen
= strlen(undef
);
960 if ( strcmp(&undef
[undefLen
-7], "__begin") == 0 ) {
962 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef
, true));
964 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef
, true));
966 else if ( strcmp(&undef
[undefLen
-5], "__end") == 0 ) {
968 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef
, false));
970 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef
, false));
976 // <rdar://problem/5894163> need to search archives for overrides of common symbols
977 if ( _symbolTable
.hasExternalTentativeDefinitions() ) {
978 bool searchDylibs
= (_options
.commonsMode() == Options::kCommonsOverriddenByDylibs
);
979 std::vector
<const char*> tents
;
980 _symbolTable
.tentativeDefs(tents
);
981 for(std::vector
<const char*>::iterator it
= tents
.begin(); it
!= tents
.end(); ++it
) {
982 // load for previous tentative may also have loaded this tentative, so check again
983 const ld::Atom
* curAtom
= _symbolTable
.atomForSlot(_symbolTable
.findSlotForName(*it
));
984 assert(curAtom
!= NULL
);
985 if ( curAtom
->definition() == ld::Atom::definitionTentative
) {
986 _inputFiles
.searchLibraries(*it
, searchDylibs
, true, true, *this);
992 // Use linker options to resolve any remaining undefined symbols
993 if ( !_internal
.linkerOptionLibraries
.empty() || !_internal
.linkerOptionFrameworks
.empty() ) {
994 std::vector
<const char*> undefineNames
;
995 _symbolTable
.undefines(undefineNames
);
996 if ( undefineNames
.size() != 0 ) {
997 for (std::vector
<const char*>::iterator it
= undefineNames
.begin(); it
!= undefineNames
.end(); ++it
) {
998 const char* undef
= *it
;
999 if ( ! _symbolTable
.hasName(undef
) ) {
1000 _inputFiles
.searchLibraries(undef
, true, true, false, *this);
1006 // create proxies as needed for undefined symbols
1007 if ( (_options
.undefinedTreatment() != Options::kUndefinedError
) || (_options
.outputKind() == Options::kObjectFile
) ) {
1008 std::vector
<const char*> undefineNames
;
1009 _symbolTable
.undefines(undefineNames
);
1010 for(std::vector
<const char*>::iterator it
= undefineNames
.begin(); it
!= undefineNames
.end(); ++it
) {
1011 const char* undefName
= *it
;
1012 // <rdar://problem/14547001> "ld -r -exported_symbol _foo" has wrong error message if _foo is undefined
1013 bool makeProxy
= true;
1014 if ( (_options
.outputKind() == Options::kObjectFile
) && _options
.hasExportMaskList() && _options
.shouldExport(undefName
) )
1018 this->doAtom(*new UndefinedProxyAtom(undefName
));
1022 // support -U option
1023 if ( _options
.someAllowedUndefines() ) {
1024 std::vector
<const char*> undefineNames
;
1025 _symbolTable
.undefines(undefineNames
);
1026 for(std::vector
<const char*>::iterator it
= undefineNames
.begin(); it
!= undefineNames
.end(); ++it
) {
1027 if ( _options
.allowedUndefined(*it
) ) {
1029 this->doAtom(*new UndefinedProxyAtom(*it
));
1034 // After resolving all the undefs within the linkageUnit, record all the remaining undefs and all the proxies.
1035 if (_options
.bundleBitcode() && _options
.hideSymbols())
1036 _symbolTable
.mustPreserveForBitcode(_internal
.allUndefProxies
);
1041 void Resolver::markLive(const ld::Atom
& atom
, WhyLiveBackChain
* previous
)
1043 //fprintf(stderr, "markLive(%p) %s\n", &atom, atom.name());
1044 // if -why_live cares about this symbol, then dump chain
1045 if ( (previous
->referer
!= NULL
) && _options
.printWhyLive(atom
.name()) ) {
1046 fprintf(stderr
, "%s from %s\n", atom
.name(), atom
.safeFilePath());
1048 for(WhyLiveBackChain
* p
= previous
; p
!= NULL
; p
= p
->previous
, ++depth
) {
1049 for(int i
=depth
; i
> 0; --i
)
1050 fprintf(stderr
, " ");
1051 fprintf(stderr
, "%s from %s\n", p
->referer
->name(), p
->referer
->safeFilePath());
1055 // if already marked live, then done (stop recursion)
1059 // mark this atom is live
1060 (const_cast<ld::Atom
*>(&atom
))->setLive();
1062 // mark all atoms it references as live
1063 WhyLiveBackChain thisChain
;
1064 thisChain
.previous
= previous
;
1065 thisChain
.referer
= &atom
;
1066 for (ld::Fixup::iterator fit
= atom
.fixupsBegin(), end
=atom
.fixupsEnd(); fit
!= end
; ++fit
) {
1067 const ld::Atom
* target
;
1068 switch ( fit
->kind
) {
1069 case ld::Fixup::kindNone
:
1070 case ld::Fixup::kindNoneFollowOn
:
1071 case ld::Fixup::kindNoneGroupSubordinate
:
1072 case ld::Fixup::kindNoneGroupSubordinateFDE
:
1073 case ld::Fixup::kindNoneGroupSubordinateLSDA
:
1074 case ld::Fixup::kindNoneGroupSubordinatePersonality
:
1075 case ld::Fixup::kindSetTargetAddress
:
1076 case ld::Fixup::kindSubtractTargetAddress
:
1077 case ld::Fixup::kindStoreTargetAddressLittleEndian32
:
1078 case ld::Fixup::kindStoreTargetAddressLittleEndian64
:
1079 case ld::Fixup::kindStoreTargetAddressBigEndian32
:
1080 case ld::Fixup::kindStoreTargetAddressBigEndian64
:
1081 case ld::Fixup::kindStoreTargetAddressX86PCRel32
:
1082 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32
:
1083 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad
:
1084 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA
:
1085 case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad
:
1086 case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoadNowLEA
:
1087 case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad
:
1088 case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoadNowLEA
:
1089 case ld::Fixup::kindStoreTargetAddressARMBranch24
:
1090 case ld::Fixup::kindStoreTargetAddressThumbBranch22
:
1091 #if SUPPORT_ARCH_arm64
1092 case ld::Fixup::kindStoreTargetAddressARM64Branch26
:
1093 case ld::Fixup::kindStoreTargetAddressARM64Page21
:
1094 case ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21
:
1095 case ld::Fixup::kindStoreTargetAddressARM64GOTLeaPage21
:
1096 case ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21
:
1097 case ld::Fixup::kindStoreTargetAddressARM64TLVPLoadNowLeaPage21
:
1099 if ( fit
->binding
== ld::Fixup::bindingByContentBound
) {
1100 // normally this was done in convertReferencesToIndirect()
1101 // but a archive loaded .o file may have a forward reference
1102 SymbolTable::IndirectBindingSlot slot
;
1103 const ld::Atom
* dummy
;
1104 switch ( fit
->u
.target
->combine() ) {
1105 case ld::Atom::combineNever
:
1106 case ld::Atom::combineByName
:
1107 assert(0 && "wrong combine type for bind by content");
1109 case ld::Atom::combineByNameAndContent
:
1110 slot
= _symbolTable
.findSlotForContent(fit
->u
.target
, &dummy
);
1111 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
1112 fit
->u
.bindingIndex
= slot
;
1114 case ld::Atom::combineByNameAndReferences
:
1115 slot
= _symbolTable
.findSlotForReferences(fit
->u
.target
, &dummy
);
1116 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
1117 fit
->u
.bindingIndex
= slot
;
1121 switch ( fit
->binding
) {
1122 case ld::Fixup::bindingDirectlyBound
:
1123 markLive(*(fit
->u
.target
), &thisChain
);
1125 case ld::Fixup::bindingByNameUnbound
:
1126 // doAtom() did not convert to indirect in dead-strip mode, so that now
1127 fit
->u
.bindingIndex
= _symbolTable
.findSlotForName(fit
->u
.name
);
1128 fit
->binding
= ld::Fixup::bindingsIndirectlyBound
;
1129 // fall into next case
1130 case ld::Fixup::bindingsIndirectlyBound
:
1131 target
= _internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
1132 if ( target
== NULL
) {
1133 const char* targetName
= _symbolTable
.indirectName(fit
->u
.bindingIndex
);
1134 _inputFiles
.searchLibraries(targetName
, true, true, false, *this);
1135 target
= _internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
1137 if ( target
!= NULL
) {
1138 if ( target
->definition() == ld::Atom::definitionTentative
) {
1139 // <rdar://problem/5894163> need to search archives for overrides of common symbols
1140 bool searchDylibs
= (_options
.commonsMode() == Options::kCommonsOverriddenByDylibs
);
1141 _inputFiles
.searchLibraries(target
->name(), searchDylibs
, true, true, *this);
1142 // recompute target since it may have been overridden by searchLibraries()
1143 target
= _internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
1145 this->markLive(*target
, &thisChain
);
1148 _atomsWithUnresolvedReferences
.push_back(&atom
);
1152 assert(0 && "bad binding during dead stripping");
1164 bool operator()(const ld::Atom
* atom
) const {
1165 if (atom
->live() || atom
->dontDeadStrip() )
1167 // don't kill combinable atoms in first pass
1168 switch ( atom
->combine() ) {
1169 case ld::Atom::combineByNameAndContent
:
1170 case ld::Atom::combineByNameAndReferences
:
1178 void Resolver::deadStripOptimize(bool force
)
1180 // only do this optimization with -dead_strip
1181 if ( ! _options
.deadCodeStrip() )
1184 // add entry point (main) to live roots
1185 const ld::Atom
* entry
= this->entryPoint(true);
1186 if ( entry
!= NULL
)
1187 _deadStripRoots
.insert(entry
);
1189 // add -exported_symbols_list, -init, and -u entries to live roots
1190 for (Options::UndefinesIterator uit
=_options
.initialUndefinesBegin(); uit
!= _options
.initialUndefinesEnd(); ++uit
) {
1191 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName(*uit
);
1192 if ( _internal
.indirectBindingTable
[slot
] == NULL
) {
1193 _inputFiles
.searchLibraries(*uit
, false, true, false, *this);
1195 if ( _internal
.indirectBindingTable
[slot
] != NULL
)
1196 _deadStripRoots
.insert(_internal
.indirectBindingTable
[slot
]);
1199 // this helper is only referenced by synthesize stubs, assume it will be used
1200 if ( _internal
.classicBindingHelper
!= NULL
)
1201 _deadStripRoots
.insert(_internal
.classicBindingHelper
);
1203 // this helper is only referenced by synthesize stubs, assume it will be used
1204 if ( _internal
.compressedFastBinderProxy
!= NULL
)
1205 _deadStripRoots
.insert(_internal
.compressedFastBinderProxy
);
1207 // this helper is only referenced by synthesized lazy stubs, assume it will be used
1208 if ( _internal
.lazyBindingHelper
!= NULL
)
1209 _deadStripRoots
.insert(_internal
.lazyBindingHelper
);
1211 // add all dont-dead-strip atoms as roots
1212 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1213 const ld::Atom
* atom
= *it
;
1214 if ( atom
->dontDeadStrip() ) {
1215 //fprintf(stderr, "dont dead strip: %p %s %s\n", atom, atom->section().sectionName(), atom->name());
1216 _deadStripRoots
.insert(atom
);
1217 // unset liveness, so markLive() will recurse
1218 (const_cast<ld::Atom
*>(atom
))->setLive(0);
1222 // mark all roots as live, and all atoms they reference
1223 for (std::set
<const ld::Atom
*>::iterator it
=_deadStripRoots
.begin(); it
!= _deadStripRoots
.end(); ++it
) {
1224 WhyLiveBackChain rootChain
;
1225 rootChain
.previous
= NULL
;
1226 rootChain
.referer
= *it
;
1227 this->markLive(**it
, &rootChain
);
1230 // special case atoms that need to be live if they reference something live
1231 if ( ! _dontDeadStripIfReferencesLive
.empty() ) {
1232 for (std::vector
<const ld::Atom
*>::iterator it
=_dontDeadStripIfReferencesLive
.begin(); it
!= _dontDeadStripIfReferencesLive
.end(); ++it
) {
1233 const Atom
* liveIfRefLiveAtom
= *it
;
1234 //fprintf(stderr, "live-if-live atom: %s\n", liveIfRefLiveAtom->name());
1235 if ( liveIfRefLiveAtom
->live() )
1237 bool hasLiveRef
= false;
1238 for (ld::Fixup::iterator fit
=liveIfRefLiveAtom
->fixupsBegin(); fit
!= liveIfRefLiveAtom
->fixupsEnd(); ++fit
) {
1239 const Atom
* target
= NULL
;
1240 switch ( fit
->binding
) {
1241 case ld::Fixup::bindingDirectlyBound
:
1242 target
= fit
->u
.target
;
1244 case ld::Fixup::bindingsIndirectlyBound
:
1245 target
= _internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
1250 if ( (target
!= NULL
) && target
->live() )
1254 WhyLiveBackChain rootChain
;
1255 rootChain
.previous
= NULL
;
1256 rootChain
.referer
= liveIfRefLiveAtom
;
1257 this->markLive(*liveIfRefLiveAtom
, &rootChain
);
1262 // now remove all non-live atoms from _atoms
1263 const bool log
= false;
1265 fprintf(stderr
, "deadStripOptimize() all %ld atoms with liveness:\n", _atoms
.size());
1266 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1267 const ld::File
* file
= (*it
)->file();
1268 fprintf(stderr
, " live=%d atom=%p name=%s from=%s\n", (*it
)->live(), *it
, (*it
)->name(), (file
? file
->path() : "<internal>"));
1272 if ( _haveLLVMObjs
&& !force
) {
1273 std::copy_if(_atoms
.begin(), _atoms
.end(), std::back_inserter(_internal
.deadAtoms
), NotLiveLTO() );
1274 // <rdar://problem/9777977> don't remove combinable atoms, they may come back in lto output
1275 _atoms
.erase(std::remove_if(_atoms
.begin(), _atoms
.end(), NotLiveLTO()), _atoms
.end());
1276 _symbolTable
.removeDeadAtoms();
1279 std::copy_if(_atoms
.begin(), _atoms
.end(), std::back_inserter(_internal
.deadAtoms
), NotLive() );
1280 _atoms
.erase(std::remove_if(_atoms
.begin(), _atoms
.end(), NotLive()), _atoms
.end());
1284 fprintf(stderr
, "deadStripOptimize() %ld remaining atoms\n", _atoms
.size());
1285 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1286 fprintf(stderr
, " live=%d atom=%p name=%s\n", (*it
)->live(), *it
, (*it
)->name());
1292 // This is called when LTO is used but -dead_strip is not used.
1293 // Some undefines were eliminated by LTO, but others were not.
1294 void Resolver::remainingUndefines(std::vector
<const char*>& undefs
)
1297 // search all atoms for references that are unbound
1298 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1299 const ld::Atom
* atom
= *it
;
1300 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
1301 switch ( (ld::Fixup::TargetBinding
)fit
->binding
) {
1302 case ld::Fixup::bindingByNameUnbound
:
1303 assert(0 && "should not be by-name this late");
1304 undefSet
.insert(fit
->u
.name
);
1306 case ld::Fixup::bindingsIndirectlyBound
:
1307 if ( _internal
.indirectBindingTable
[fit
->u
.bindingIndex
] == NULL
) {
1308 undefSet
.insert(_symbolTable
.indirectName(fit
->u
.bindingIndex
));
1311 case ld::Fixup::bindingByContentBound
:
1312 case ld::Fixup::bindingNone
:
1313 case ld::Fixup::bindingDirectlyBound
:
1318 // look for any initial undefines that are still undefined
1319 for (Options::UndefinesIterator uit
=_options
.initialUndefinesBegin(); uit
!= _options
.initialUndefinesEnd(); ++uit
) {
1320 if ( ! _symbolTable
.hasName(*uit
) ) {
1321 undefSet
.insert(*uit
);
1325 // copy set to vector
1326 for (StringSet::const_iterator it
=undefSet
.begin(); it
!= undefSet
.end(); ++it
) {
1327 fprintf(stderr
, "undef: %s\n", *it
);
1328 undefs
.push_back(*it
);
1332 void Resolver::liveUndefines(std::vector
<const char*>& undefs
)
1335 // search all live atoms for references that are unbound
1336 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1337 const ld::Atom
* atom
= *it
;
1338 if ( ! atom
->live() )
1340 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
1341 switch ( (ld::Fixup::TargetBinding
)fit
->binding
) {
1342 case ld::Fixup::bindingByNameUnbound
:
1343 assert(0 && "should not be by-name this late");
1344 undefSet
.insert(fit
->u
.name
);
1346 case ld::Fixup::bindingsIndirectlyBound
:
1347 if ( _internal
.indirectBindingTable
[fit
->u
.bindingIndex
] == NULL
) {
1348 undefSet
.insert(_symbolTable
.indirectName(fit
->u
.bindingIndex
));
1351 case ld::Fixup::bindingByContentBound
:
1352 case ld::Fixup::bindingNone
:
1353 case ld::Fixup::bindingDirectlyBound
:
1358 // look for any initial undefines that are still undefined
1359 for (Options::UndefinesIterator uit
=_options
.initialUndefinesBegin(); uit
!= _options
.initialUndefinesEnd(); ++uit
) {
1360 if ( ! _symbolTable
.hasName(*uit
) ) {
1361 undefSet
.insert(*uit
);
1365 // copy set to vector
1366 for (StringSet::const_iterator it
=undefSet
.begin(); it
!= undefSet
.end(); ++it
) {
1367 undefs
.push_back(*it
);
1373 // <rdar://problem/8252819> warn when .objc_class_name_* symbol missing
1374 class ExportedObjcClass
1377 ExportedObjcClass(const Options
& opt
) : _options(opt
) {}
1379 bool operator()(const char* name
) const {
1380 if ( (strncmp(name
, ".objc_class_name_", 17) == 0) && _options
.shouldExport(name
) ) {
1381 warning("ignoring undefined symbol %s from -exported_symbols_list", name
);
1384 const char* s
= strstr(name
, "CLASS_$_");
1386 char temp
[strlen(name
)+16];
1387 strcpy(temp
, ".objc_class_name_");
1388 strcat(temp
, &s
[8]);
1389 if ( _options
.wasRemovedExport(temp
) ) {
1390 warning("ignoring undefined symbol %s from -exported_symbols_list", temp
);
1397 const Options
& _options
;
1401 // temp hack for undefined aliases
1402 class UndefinedAlias
1405 UndefinedAlias(const Options
& opt
) : _aliases(opt
.cmdLineAliases()) {}
1407 bool operator()(const char* name
) const {
1408 for (std::vector
<Options::AliasPair
>::const_iterator it
=_aliases
.begin(); it
!= _aliases
.end(); ++it
) {
1409 if ( strcmp(it
->realName
, name
) == 0 ) {
1410 warning("undefined base symbol '%s' for alias '%s'", name
, it
->alias
);
1417 const std::vector
<Options::AliasPair
>& _aliases
;
1422 static const char* pathLeafName(const char* path
)
1424 const char* shortPath
= strrchr(path
, '/');
1425 if ( shortPath
== NULL
)
1428 return &shortPath
[1];
1431 bool Resolver::printReferencedBy(const char* name
, SymbolTable::IndirectBindingSlot slot
)
1433 unsigned foundReferenceCount
= 0;
1434 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1435 const ld::Atom
* atom
= *it
;
1436 for (ld::Fixup::iterator fit
=atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
1437 if ( fit
->binding
== ld::Fixup::bindingsIndirectlyBound
) {
1438 if ( fit
->u
.bindingIndex
== slot
) {
1439 if ( atom
->contentType() == ld::Atom::typeNonLazyPointer
) {
1440 const ld::Atom
* existingAtom
;
1441 unsigned int nlSlot
= _symbolTable
.findSlotForReferences(atom
, &existingAtom
);
1442 if ( printReferencedBy(name
, nlSlot
) )
1443 ++foundReferenceCount
;
1445 else if ( atom
->contentType() == ld::Atom::typeCFI
) {
1446 fprintf(stderr
, " Dwarf Exception Unwind Info (__eh_frame) in %s\n", pathLeafName(atom
->safeFilePath()));
1447 ++foundReferenceCount
;
1450 fprintf(stderr
, " %s in %s\n", _options
.demangleSymbol(atom
->name()), pathLeafName(atom
->safeFilePath()));
1451 ++foundReferenceCount
;
1452 break; // if undefined used twice in a function, only show first
1457 if ( foundReferenceCount
> 6 ) {
1458 fprintf(stderr
, " ...\n");
1459 break; // only show first six uses of undefined symbol
1462 return (foundReferenceCount
!= 0);
1465 void Resolver::checkUndefines(bool force
)
1467 // when using LTO, undefines are checked after bitcode is optimized
1468 if ( _haveLLVMObjs
&& !force
)
1471 // error out on any remaining undefines
1472 bool doPrint
= true;
1473 bool doError
= true;
1474 switch ( _options
.undefinedTreatment() ) {
1475 case Options::kUndefinedError
:
1477 case Options::kUndefinedDynamicLookup
:
1480 case Options::kUndefinedWarning
:
1483 case Options::kUndefinedSuppress
:
1488 std::vector
<const char*> unresolvableUndefines
;
1489 if ( _options
.deadCodeStrip() )
1490 this->liveUndefines(unresolvableUndefines
);
1491 else if( _haveLLVMObjs
)
1492 this->remainingUndefines(unresolvableUndefines
); // <rdar://problem/10052396> LTO may have eliminated need for some undefines
1494 _symbolTable
.undefines(unresolvableUndefines
);
1496 // <rdar://problem/8252819> assert when .objc_class_name_* symbol missing
1497 if ( _options
.hasExportMaskList() ) {
1498 unresolvableUndefines
.erase(std::remove_if(unresolvableUndefines
.begin(), unresolvableUndefines
.end(), ExportedObjcClass(_options
)), unresolvableUndefines
.end());
1501 // hack to temporarily make missing aliases a warning
1502 if ( _options
.haveCmdLineAliases() ) {
1503 unresolvableUndefines
.erase(std::remove_if(unresolvableUndefines
.begin(), unresolvableUndefines
.end(), UndefinedAlias(_options
)), unresolvableUndefines
.end());
1506 const int unresolvableCount
= unresolvableUndefines
.size();
1507 int unresolvableExportsCount
= 0;
1508 if ( unresolvableCount
!= 0 ) {
1510 if ( _options
.printArchPrefix() )
1511 fprintf(stderr
, "Undefined symbols for architecture %s:\n", _options
.architectureName());
1513 fprintf(stderr
, "Undefined symbols:\n");
1514 for (int i
=0; i
< unresolvableCount
; ++i
) {
1515 const char* name
= unresolvableUndefines
[i
];
1516 unsigned int slot
= _symbolTable
.findSlotForName(name
);
1517 fprintf(stderr
, " \"%s\", referenced from:\n", _options
.demangleSymbol(name
));
1518 // scan all atoms for references
1519 bool foundAtomReference
= printReferencedBy(name
, slot
);
1520 // scan command line options
1521 if ( !foundAtomReference
) {
1522 // might be from -init command line option
1523 if ( (_options
.initFunctionName() != NULL
) && (strcmp(name
, _options
.initFunctionName()) == 0) ) {
1524 fprintf(stderr
, " -init command line option\n");
1526 // or might be from exported symbol option
1527 else if ( _options
.hasExportMaskList() && _options
.shouldExport(name
) ) {
1528 fprintf(stderr
, " -exported_symbol[s_list] command line option\n");
1530 // or might be from re-exported symbol option
1531 else if ( _options
.hasReExportList() && _options
.shouldReExport(name
) ) {
1532 fprintf(stderr
, " -reexported_symbols_list command line option\n");
1534 else if ( (_options
.outputKind() == Options::kDynamicExecutable
)
1535 && (_options
.entryName() != NULL
) && (strcmp(name
, _options
.entryName()) == 0) ) {
1536 fprintf(stderr
, " implicit entry/start for main executable\n");
1539 bool isInitialUndefine
= false;
1540 for (Options::UndefinesIterator uit
=_options
.initialUndefinesBegin(); uit
!= _options
.initialUndefinesEnd(); ++uit
) {
1541 if ( strcmp(*uit
, name
) == 0 ) {
1542 isInitialUndefine
= true;
1546 if ( isInitialUndefine
)
1547 fprintf(stderr
, " -u command line option\n");
1549 ++unresolvableExportsCount
;
1551 // be helpful and check for typos
1552 bool printedStart
= false;
1553 for (SymbolTable::byNameIterator sit
=_symbolTable
.begin(); sit
!= _symbolTable
.end(); sit
++) {
1554 const ld::Atom
* atom
= *sit
;
1555 if ( (atom
!= NULL
) && (atom
->symbolTableInclusion() == ld::Atom::symbolTableIn
) && (strstr(atom
->name(), name
) != NULL
) ) {
1556 if ( ! printedStart
) {
1557 fprintf(stderr
, " (maybe you meant: %s", atom
->name());
1558 printedStart
= true;
1561 fprintf(stderr
, ", %s ", atom
->name());
1566 fprintf(stderr
, ")\n");
1567 // <rdar://problem/8989530> Add comment to error message when __ZTV symbols are undefined
1568 if ( strncmp(name
, "__ZTV", 5) == 0 ) {
1569 fprintf(stderr
, " NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.\n");
1574 throw "symbol(s) not found";
1581 void Resolver::checkDylibSymbolCollisions()
1583 for (SymbolTable::byNameIterator it
=_symbolTable
.begin(); it
!= _symbolTable
.end(); it
++) {
1584 const ld::Atom
* atom
= *it
;
1587 if ( atom
->scope() == ld::Atom::scopeGlobal
) {
1588 // <rdar://problem/5048861> No warning about tentative definition conflicting with dylib definition
1589 // for each tentative definition in symbol table look for dylib that exports same symbol name
1590 if ( atom
->definition() == ld::Atom::definitionTentative
) {
1591 _inputFiles
.searchLibraries(atom
->name(), true, false, false, *this);
1593 // record any overrides of weak symbols in any linked dylib
1594 if ( (atom
->definition() == ld::Atom::definitionRegular
) && (atom
->symbolTableInclusion() == ld::Atom::symbolTableIn
) ) {
1595 if ( _inputFiles
.searchWeakDefInDylib(atom
->name()) )
1596 (const_cast<ld::Atom
*>(atom
))->setOverridesDylibsWeakDef();
1603 const ld::Atom
* Resolver::entryPoint(bool searchArchives
)
1605 const char* symbolName
= NULL
;
1606 bool makingDylib
= false;
1607 switch ( _options
.outputKind() ) {
1608 case Options::kDynamicExecutable
:
1609 case Options::kStaticExecutable
:
1610 case Options::kDyld
:
1611 case Options::kPreload
:
1612 symbolName
= _options
.entryName();
1614 case Options::kDynamicLibrary
:
1615 symbolName
= _options
.initFunctionName();
1618 case Options::kObjectFile
:
1619 case Options::kDynamicBundle
:
1620 case Options::kKextBundle
:
1624 if ( symbolName
!= NULL
) {
1625 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName(symbolName
);
1626 if ( (_internal
.indirectBindingTable
[slot
] == NULL
) && searchArchives
) {
1627 // <rdar://problem/7043256> ld64 can not find a -e entry point from an archive
1628 _inputFiles
.searchLibraries(symbolName
, false, true, false, *this);
1630 if ( _internal
.indirectBindingTable
[slot
] == NULL
) {
1631 if ( strcmp(symbolName
, "start") == 0 )
1632 throwf("entry point (%s) undefined. Usually in crt1.o", symbolName
);
1634 throwf("entry point (%s) undefined.", symbolName
);
1636 else if ( _internal
.indirectBindingTable
[slot
]->definition() == ld::Atom::definitionProxy
) {
1638 throwf("-init function (%s) found in linked dylib, must be in dylib being linked", symbolName
);
1640 return _internal
.indirectBindingTable
[slot
];
1646 void Resolver::fillInHelpersInInternalState()
1648 // look up well known atoms
1649 bool needsStubHelper
= true;
1650 switch ( _options
.outputKind() ) {
1651 case Options::kDynamicExecutable
:
1652 case Options::kDynamicLibrary
:
1653 case Options::kDynamicBundle
:
1654 needsStubHelper
= true;
1656 case Options::kDyld
:
1657 case Options::kKextBundle
:
1658 case Options::kObjectFile
:
1659 case Options::kStaticExecutable
:
1660 case Options::kPreload
:
1661 needsStubHelper
= false;
1665 _internal
.classicBindingHelper
= NULL
;
1666 if ( needsStubHelper
&& !_options
.makeCompressedDyldInfo() ) {
1667 // "dyld_stub_binding_helper" comes from .o file, so should already exist in symbol table
1668 if ( _symbolTable
.hasName("dyld_stub_binding_helper") ) {
1669 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName("dyld_stub_binding_helper");
1670 _internal
.classicBindingHelper
= _internal
.indirectBindingTable
[slot
];
1674 _internal
.lazyBindingHelper
= NULL
;
1675 if ( _options
.usingLazyDylibLinking() ) {
1676 // "dyld_lazy_dylib_stub_binding_helper" comes from lazydylib1.o file, so should already exist in symbol table
1677 if ( _symbolTable
.hasName("dyld_lazy_dylib_stub_binding_helper") ) {
1678 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName("dyld_lazy_dylib_stub_binding_helper");
1679 _internal
.lazyBindingHelper
= _internal
.indirectBindingTable
[slot
];
1681 if ( _internal
.lazyBindingHelper
== NULL
)
1682 throw "symbol dyld_lazy_dylib_stub_binding_helper not defined (usually in lazydylib1.o)";
1685 _internal
.compressedFastBinderProxy
= NULL
;
1686 if ( needsStubHelper
&& _options
.makeCompressedDyldInfo() ) {
1687 // "dyld_stub_binder" comes from libSystem.dylib so will need to manually resolve
1688 if ( !_symbolTable
.hasName("dyld_stub_binder") ) {
1689 _inputFiles
.searchLibraries("dyld_stub_binder", true, false, false, *this);
1691 if ( _symbolTable
.hasName("dyld_stub_binder") ) {
1692 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName("dyld_stub_binder");
1693 _internal
.compressedFastBinderProxy
= _internal
.indirectBindingTable
[slot
];
1695 if ( _internal
.compressedFastBinderProxy
== NULL
) {
1696 if ( _options
.undefinedTreatment() != Options::kUndefinedError
) {
1698 _internal
.compressedFastBinderProxy
= new UndefinedProxyAtom("dyld_stub_binder");
1699 this->doAtom(*_internal
.compressedFastBinderProxy
);
1706 void Resolver::fillInInternalState()
1708 // store atoms into their final section
1709 for (std::vector
<const ld::Atom
*>::iterator it
= _atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1710 _internal
.addAtom(**it
);
1713 // <rdar://problem/7783918> make sure there is a __text section so that codesigning works
1714 if ( (_options
.outputKind() == Options::kDynamicLibrary
) || (_options
.outputKind() == Options::kDynamicBundle
) )
1715 _internal
.getFinalSection(*new ld::Section("__TEXT", "__text", ld::Section::typeCode
));
1718 void Resolver::fillInEntryPoint()
1720 _internal
.entryPoint
= this->entryPoint(true);
1723 void Resolver::syncAliases()
1725 if ( !_haveAliases
|| (_options
.outputKind() == Options::kObjectFile
) )
1728 // Set attributes of alias to match its found target
1729 for (std::vector
<const ld::Atom
*>::iterator it
= _atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1730 const ld::Atom
* atom
= *it
;
1731 if ( atom
->section().type() == ld::Section::typeTempAlias
) {
1732 assert(atom
->fixupsBegin() != atom
->fixupsEnd());
1733 for (ld::Fixup::iterator fit
= atom
->fixupsBegin(); fit
!= atom
->fixupsEnd(); ++fit
) {
1734 const ld::Atom
* target
;
1735 ld::Atom::Scope scope
;
1736 assert(fit
->kind
== ld::Fixup::kindNoneFollowOn
);
1737 switch ( fit
->binding
) {
1738 case ld::Fixup::bindingByNameUnbound
:
1740 case ld::Fixup::bindingsIndirectlyBound
:
1741 target
= _internal
.indirectBindingTable
[fit
->u
.bindingIndex
];
1742 assert(target
!= NULL
);
1743 scope
= atom
->scope();
1744 (const_cast<Atom
*>(atom
))->setAttributesFromAtom(*target
);
1745 // alias has same attributes as target, except for scope
1746 (const_cast<Atom
*>(atom
))->setScope(scope
);
1749 assert(0 && "internal error: unexpected alias binding");
1756 void Resolver::removeCoalescedAwayAtoms()
1758 const bool log
= false;
1760 fprintf(stderr
, "removeCoalescedAwayAtoms() starts with %lu atoms\n", _atoms
.size());
1762 _atoms
.erase(std::remove_if(_atoms
.begin(), _atoms
.end(), AtomCoalescedAway()), _atoms
.end());
1764 fprintf(stderr
, "removeCoalescedAwayAtoms() after removing coalesced atoms, %lu remain\n", _atoms
.size());
1765 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1766 fprintf(stderr
, " atom=%p %s\n", *it
, (*it
)->name());
1771 void Resolver::linkTimeOptimize()
1773 // only do work here if some llvm obj files where loaded
1774 if ( ! _haveLLVMObjs
)
1777 // <rdar://problem/15314161> LTO: Symbol multiply defined error should specify exactly where the symbol is found
1778 _symbolTable
.checkDuplicateSymbols();
1780 // run LLVM lto code-gen
1781 lto::OptimizeOptions optOpt
;
1782 optOpt
.outputFilePath
= _options
.outputFilePath();
1783 optOpt
.tmpObjectFilePath
= _options
.tempLtoObjectPath();
1784 optOpt
.ltoCachePath
= _options
.ltoCachePath();
1785 optOpt
.ltoPruneInterval
= _options
.ltoPruneInterval();
1786 optOpt
.ltoPruneAfter
= _options
.ltoPruneAfter();
1787 optOpt
.ltoMaxCacheSize
= _options
.ltoMaxCacheSize();
1788 optOpt
.preserveAllGlobals
= _options
.allGlobalsAreDeadStripRoots() || _options
.hasExportRestrictList();
1789 optOpt
.verbose
= _options
.verbose();
1790 optOpt
.saveTemps
= _options
.saveTempFiles();
1791 optOpt
.ltoCodegenOnly
= _options
.ltoCodegenOnly();
1792 optOpt
.pie
= _options
.positionIndependentExecutable();
1793 optOpt
.mainExecutable
= _options
.linkingMainExecutable();;
1794 optOpt
.staticExecutable
= (_options
.outputKind() == Options::kStaticExecutable
);
1795 optOpt
.preload
= (_options
.outputKind() == Options::kPreload
);
1796 optOpt
.relocatable
= (_options
.outputKind() == Options::kObjectFile
);
1797 optOpt
.allowTextRelocs
= _options
.allowTextRelocs();
1798 optOpt
.linkerDeadStripping
= _options
.deadCodeStrip();
1799 optOpt
.needsUnwindInfoSection
= _options
.needsUnwindInfoSection();
1800 optOpt
.keepDwarfUnwind
= _options
.keepDwarfUnwind();
1801 optOpt
.verboseOptimizationHints
= _options
.verboseOptimizationHints();
1802 optOpt
.armUsesZeroCostExceptions
= _options
.armUsesZeroCostExceptions();
1803 optOpt
.simulator
= _options
.targetIOSSimulator();
1804 optOpt
.ignoreMismatchPlatform
= ((_options
.outputKind() == Options::kPreload
) || (_options
.outputKind() == Options::kStaticExecutable
));
1805 optOpt
.bitcodeBundle
= (_options
.bundleBitcode() && (_options
.bitcodeKind() != Options::kBitcodeMarker
));
1806 optOpt
.maxDefaultCommonAlignment
= _options
.maxDefaultCommonAlign();
1807 optOpt
.arch
= _options
.architecture();
1808 optOpt
.mcpu
= _options
.mcpuLTO();
1809 optOpt
.platform
= _options
.platform();
1810 optOpt
.minOSVersion
= _options
.minOSversion();
1811 optOpt
.llvmOptions
= &_options
.llvmOptions();
1812 optOpt
.initialUndefines
= &_options
.initialUndefines();
1814 std::vector
<const ld::Atom
*> newAtoms
;
1815 std::vector
<const char*> additionalUndefines
;
1816 if ( ! lto::optimize(_atoms
, _internal
, optOpt
, *this, newAtoms
, additionalUndefines
) )
1817 return; // if nothing done
1818 _ltoCodeGenFinished
= true;
1820 // add all newly created atoms to _atoms and update symbol table
1821 for(std::vector
<const ld::Atom
*>::iterator it
= newAtoms
.begin(); it
!= newAtoms
.end(); ++it
)
1824 // some atoms might have been optimized way (marked coalesced), remove them
1825 this->removeCoalescedAwayAtoms();
1827 // run through all atoms again and make sure newly codegened atoms have references bound
1828 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
)
1829 this->convertReferencesToIndirect(**it
);
1831 // adjust section of any new
1832 for (std::vector
<const AliasAtom
*>::const_iterator it
=_aliasesFromCmdLine
.begin(); it
!= _aliasesFromCmdLine
.end(); ++it
) {
1833 const AliasAtom
* aliasAtom
= *it
;
1834 // update fields in AliasAtom to match newly constructed mach-o atom
1835 aliasAtom
->setFinalAliasOf();
1838 // <rdar://problem/14609792> add any auto-link libraries requested by LTO output to dylibs to search
1839 _inputFiles
.addLinkerOptionLibraries(_internal
, *this);
1840 _inputFiles
.createIndirectDylibs();
1842 // resolve new undefines (e.g calls to _malloc and _memcpy that llvm compiler conjures up)
1843 for(std::vector
<const char*>::iterator uit
= additionalUndefines
.begin(); uit
!= additionalUndefines
.end(); ++uit
) {
1844 const char *targetName
= *uit
;
1845 // these symbols may or may not already be in linker's symbol table
1846 if ( ! _symbolTable
.hasName(targetName
) ) {
1847 _inputFiles
.searchLibraries(targetName
, true, true, false, *this);
1851 // if -dead_strip on command line
1852 if ( _options
.deadCodeStrip() ) {
1853 // run through all atoms again and make live_section LTO atoms are preserved from dead_stripping if needed
1854 _dontDeadStripIfReferencesLive
.clear();
1855 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1856 const ld::Atom
* atom
= *it
;
1857 if ( atom
->dontDeadStripIfReferencesLive() ) {
1858 _dontDeadStripIfReferencesLive
.push_back(atom
);
1861 // clear liveness bit
1862 (const_cast<ld::Atom
*>(*it
))->setLive((*it
)->dontDeadStrip());
1864 // and re-compute dead code
1865 this->deadStripOptimize(true);
1868 // <rdar://problem/12386559> if -exported_symbols_list on command line, re-force scope
1869 if ( _options
.hasExportMaskList() ) {
1870 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1871 const ld::Atom
* atom
= *it
;
1872 if ( atom
->scope() == ld::Atom::scopeGlobal
) {
1873 if ( !_options
.shouldExport(atom
->name()) ) {
1874 (const_cast<ld::Atom
*>(atom
))->setScope(ld::Atom::scopeLinkageUnit
);
1880 if ( _options
.outputKind() == Options::kObjectFile
) {
1881 // if -r mode, add proxies for new undefines (e.g. ___stack_chk_fail)
1882 this->resolveUndefines();
1885 // last chance to check for undefines
1886 this->resolveUndefines();
1887 this->checkUndefines(true);
1889 // check new code does not override some dylib
1890 this->checkDylibSymbolCollisions();
1892 // <rdar://problem/33853815> remove undefs from LTO objects that gets optimized away
1893 std::unordered_set
<const ld::Atom
*> mustPreserve
;
1894 if ( _internal
.classicBindingHelper
!= NULL
)
1895 mustPreserve
.insert(_internal
.classicBindingHelper
);
1896 if ( _internal
.compressedFastBinderProxy
!= NULL
)
1897 mustPreserve
.insert(_internal
.compressedFastBinderProxy
);
1898 if ( _internal
.lazyBindingHelper
!= NULL
)
1899 mustPreserve
.insert(_internal
.lazyBindingHelper
);
1900 if ( const ld::Atom
* entry
= this->entryPoint(true) )
1901 mustPreserve
.insert(entry
);
1902 for (Options::UndefinesIterator uit
=_options
.initialUndefinesBegin(); uit
!= _options
.initialUndefinesEnd(); ++uit
) {
1903 SymbolTable::IndirectBindingSlot slot
= _symbolTable
.findSlotForName(*uit
);
1904 if ( _internal
.indirectBindingTable
[slot
] != NULL
)
1905 mustPreserve
.insert(_internal
.indirectBindingTable
[slot
]);
1907 _symbolTable
.removeDeadUndefs(_atoms
, mustPreserve
);
1912 void Resolver::tweakWeakness()
1914 // <rdar://problem/7977374> Add command line options to control symbol weak-def bit on exported symbols
1915 if ( _options
.hasWeakBitTweaks() ) {
1916 for (std::vector
<ld::Internal::FinalSection
*>::iterator sit
= _internal
.sections
.begin(); sit
!= _internal
.sections
.end(); ++sit
) {
1917 ld::Internal::FinalSection
* sect
= *sit
;
1918 for (std::vector
<const ld::Atom
*>::iterator ait
= sect
->atoms
.begin(); ait
!= sect
->atoms
.end(); ++ait
) {
1919 const ld::Atom
* atom
= *ait
;
1920 if ( atom
->definition() != ld::Atom::definitionRegular
)
1922 const char* name
= atom
->name();
1923 if ( atom
->scope() == ld::Atom::scopeGlobal
) {
1924 if ( atom
->combine() == ld::Atom::combineNever
) {
1925 if ( _options
.forceWeak(name
) )
1926 (const_cast<ld::Atom
*>(atom
))->setCombine(ld::Atom::combineByName
);
1928 else if ( atom
->combine() == ld::Atom::combineByName
) {
1929 if ( _options
.forceNotWeak(name
) )
1930 (const_cast<ld::Atom
*>(atom
))->setCombine(ld::Atom::combineNever
);
1934 if ( _options
.forceWeakNonWildCard(name
) )
1935 warning("cannot force to be weak, non-external symbol %s", name
);
1936 else if ( _options
.forceNotWeakNonWildcard(name
) )
1937 warning("cannot force to be not-weak, non-external symbol %s", name
);
1944 void Resolver::buildArchivesList()
1946 // Determine which archives were linked and update the internal state.
1947 _inputFiles
.archives(_internal
);
1950 void Resolver::dumpAtoms()
1952 fprintf(stderr
, "Resolver all atoms:\n");
1953 for (std::vector
<const ld::Atom
*>::const_iterator it
=_atoms
.begin(); it
!= _atoms
.end(); ++it
) {
1954 const ld::Atom
* atom
= *it
;
1955 fprintf(stderr
, " %p name=%s, def=%d\n", atom
, atom
->name(), atom
->definition());
1959 void Resolver::resolve()
1961 this->initializeState();
1962 this->buildAtomList();
1963 this->addInitialUndefines();
1964 this->fillInHelpersInInternalState();
1965 this->resolveUndefines();
1966 this->deadStripOptimize();
1967 this->checkUndefines();
1968 this->checkDylibSymbolCollisions();
1969 this->syncAliases();
1970 this->removeCoalescedAwayAtoms();
1971 this->fillInEntryPoint();
1972 this->linkTimeOptimize();
1973 this->fillInInternalState();
1974 this->tweakWeakness();
1975 _symbolTable
.checkDuplicateSymbols();
1976 this->buildArchivesList();