]> git.saurik.com Git - apple/ld64.git/blob - src/ld/Resolver.cpp
ld64-264.3.102.tar.gz
[apple/ld64.git] / src / ld / Resolver.cpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
2 *
3 * Copyright (c) 2009-2010 Apple Inc. All rights reserved.
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
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.
13 *
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.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26 #include <stdlib.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <sys/mman.h>
30 #include <sys/sysctl.h>
31 #include <fcntl.h>
32 #include <errno.h>
33 #include <limits.h>
34 #include <unistd.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>
39 #include <dlfcn.h>
40 #include <mach-o/dyld.h>
41 #include <mach-o/fat.h>
42
43 #include <string>
44 #include <map>
45 #include <set>
46 #include <string>
47 #include <vector>
48 #include <list>
49 #include <algorithm>
50 #include <dlfcn.h>
51 #include <AvailabilityMacros.h>
52
53 #include "Options.h"
54
55 #include "ld.hpp"
56 #include "Bitcode.hpp"
57 #include "InputFiles.h"
58 #include "SymbolTable.h"
59 #include "Resolver.h"
60 #include "parsers/lto_file.h"
61
62
63 namespace ld {
64 namespace tool {
65
66
67 //
68 // An ExportAtom has no content. It exists so that the linker can track which imported
69 // symbols came from which dynamic libraries.
70 //
71 class UndefinedProxyAtom : public ld::Atom
72 {
73 public:
74 UndefinedProxyAtom(const char* nm)
75 : ld::Atom(_s_section, ld::Atom::definitionProxy,
76 ld::Atom::combineNever, ld::Atom::scopeLinkageUnit,
77 ld::Atom::typeUnclassified,
78 ld::Atom::symbolTableIn, false, false, false, ld::Atom::Alignment(0)),
79 _name(nm) {}
80 // overrides of ld::Atom
81 virtual const ld::File* file() const { return NULL; }
82 virtual const char* name() const { return _name; }
83 virtual uint64_t size() const { return 0; }
84 virtual uint64_t objectAddress() const { return 0; }
85 virtual void copyRawContent(uint8_t buffer[]) const { }
86 virtual void setScope(Scope) { }
87
88 protected:
89
90 virtual ~UndefinedProxyAtom() {}
91
92 const char* _name;
93
94 static ld::Section _s_section;
95 };
96
97 ld::Section UndefinedProxyAtom::_s_section("__TEXT", "__import", ld::Section::typeImportProxies, true);
98
99
100
101
102 class AliasAtom : public ld::Atom
103 {
104 public:
105 AliasAtom(const ld::Atom& target, const char* nm) :
106 ld::Atom(target.section(), target.definition(), ld::Atom::combineNever,
107 ld::Atom::scopeGlobal, target.contentType(),
108 target.symbolTableInclusion(), target.dontDeadStrip(),
109 target.isThumb(), true, target.alignment()),
110 _name(nm),
111 _aliasOf(target),
112 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, &target) { }
113
114 // overrides of ld::Atom
115 virtual const ld::File* file() const { return _aliasOf.file(); }
116 virtual const char* translationUnitSource() const
117 { return _aliasOf.translationUnitSource(); }
118 virtual const char* name() const { return _name; }
119 virtual uint64_t size() const { return 0; }
120 virtual uint64_t objectAddress() const { return _aliasOf.objectAddress(); }
121 virtual void copyRawContent(uint8_t buffer[]) const { }
122 virtual const uint8_t* rawContentPointer() const { return NULL; }
123 virtual unsigned long contentHash(const class ld::IndirectBindingTable& ibt) const
124 { return _aliasOf.contentHash(ibt); }
125 virtual bool canCoalesceWith(const ld::Atom& rhs, const class ld::IndirectBindingTable& ibt) const
126 { return _aliasOf.canCoalesceWith(rhs,ibt); }
127 virtual ld::Fixup::iterator fixupsBegin() const { return (ld::Fixup*)&_fixup; }
128 virtual ld::Fixup::iterator fixupsEnd() const { return &((ld::Fixup*)&_fixup)[1]; }
129 virtual ld::Atom::UnwindInfo::iterator beginUnwind() const { return NULL; }
130 virtual ld::Atom::UnwindInfo::iterator endUnwind() const { return NULL; }
131 virtual ld::Atom::LineInfo::iterator beginLineInfo() const { return NULL; }
132 virtual ld::Atom::LineInfo::iterator endLineInfo() const { return NULL; }
133
134 void setFinalAliasOf() const {
135 (const_cast<AliasAtom*>(this))->setAttributesFromAtom(_aliasOf);
136 (const_cast<AliasAtom*>(this))->setScope(ld::Atom::scopeGlobal);
137 }
138
139 private:
140 const char* _name;
141 const ld::Atom& _aliasOf;
142 ld::Fixup _fixup;
143 };
144
145
146
147 class SectionBoundaryAtom : public ld::Atom
148 {
149 public:
150 static SectionBoundaryAtom* makeSectionBoundaryAtom(const char* name, bool start, const char* segSectName);
151 static SectionBoundaryAtom* makeOldSectionBoundaryAtom(const char* name, bool start);
152
153 // overrides of ld::Atom
154 virtual const ld::File* file() const { return NULL; }
155 virtual const char* name() const { return _name; }
156 virtual uint64_t size() const { return 0; }
157 virtual void copyRawContent(uint8_t buffer[]) const { }
158 virtual const uint8_t* rawContentPointer() const { return NULL; }
159 virtual uint64_t objectAddress() const { return 0; }
160
161 private:
162
163 SectionBoundaryAtom(const char* nm, const ld::Section& sect,
164 ld::Atom::ContentType cont) :
165 ld::Atom(sect,
166 ld::Atom::definitionRegular,
167 ld::Atom::combineNever,
168 ld::Atom::scopeLinkageUnit,
169 cont,
170 ld::Atom::symbolTableNotIn,
171 false, false, true, ld::Atom::Alignment(0)),
172 _name(nm) { }
173
174 const char* _name;
175 };
176
177 SectionBoundaryAtom* SectionBoundaryAtom::makeSectionBoundaryAtom(const char* name, bool start, const char* segSectName)
178 {
179
180 const char* segSectDividor = strrchr(segSectName, '$');
181 if ( segSectDividor == NULL )
182 throwf("malformed section$ symbol name: %s", name);
183 const char* sectionName = segSectDividor + 1;
184 int segNameLen = segSectDividor - segSectName;
185 if ( segNameLen > 16 )
186 throwf("malformed section$ symbol name: %s", name);
187 char segName[18];
188 strlcpy(segName, segSectName, segNameLen+1);
189
190 const ld::Section* section = new ld::Section(strdup(segName), sectionName, ld::Section::typeUnclassified);
191 return new SectionBoundaryAtom(name, *section, (start ? ld::Atom::typeSectionStart : typeSectionEnd));
192 }
193
194 SectionBoundaryAtom* SectionBoundaryAtom::makeOldSectionBoundaryAtom(const char* name, bool start)
195 {
196 // e.g. __DATA__bss__begin
197 char segName[18];
198 strlcpy(segName, name, 7);
199
200 char sectName[18];
201 int nameLen = strlen(name);
202 strlcpy(sectName, &name[6], (start ? nameLen-12 : nameLen-10));
203 warning("grandfathering in old symbol '%s' as alias for 'section$%s$%s$%s'", name, start ? "start" : "end", segName, sectName);
204 const ld::Section* section = new ld::Section(strdup(segName), strdup(sectName), ld::Section::typeUnclassified);
205 return new SectionBoundaryAtom(name, *section, (start ? ld::Atom::typeSectionStart : typeSectionEnd));
206 }
207
208
209
210
211 class SegmentBoundaryAtom : public ld::Atom
212 {
213 public:
214 static SegmentBoundaryAtom* makeSegmentBoundaryAtom(const char* name, bool start, const char* segName);
215 static SegmentBoundaryAtom* makeOldSegmentBoundaryAtom(const char* name, bool start);
216
217 // overrides of ld::Atom
218 virtual const ld::File* file() const { return NULL; }
219 virtual const char* name() const { return _name; }
220 virtual uint64_t size() const { return 0; }
221 virtual void copyRawContent(uint8_t buffer[]) const { }
222 virtual const uint8_t* rawContentPointer() const { return NULL; }
223 virtual uint64_t objectAddress() const { return 0; }
224
225 private:
226
227 SegmentBoundaryAtom(const char* nm, const ld::Section& sect,
228 ld::Atom::ContentType cont) :
229 ld::Atom(sect,
230 ld::Atom::definitionRegular,
231 ld::Atom::combineNever,
232 ld::Atom::scopeLinkageUnit,
233 cont,
234 ld::Atom::symbolTableNotIn,
235 false, false, true, ld::Atom::Alignment(0)),
236 _name(nm) { }
237
238 const char* _name;
239 };
240
241 SegmentBoundaryAtom* SegmentBoundaryAtom::makeSegmentBoundaryAtom(const char* name, bool start, const char* segName)
242 {
243 if ( *segName == '\0' )
244 throwf("malformed segment$ symbol name: %s", name);
245 if ( strlen(segName) > 16 )
246 throwf("malformed segment$ symbol name: %s", name);
247
248 if ( start ) {
249 const ld::Section* section = new ld::Section(segName, "__start", ld::Section::typeFirstSection, true);
250 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionStart);
251 }
252 else {
253 const ld::Section* section = new ld::Section(segName, "__end", ld::Section::typeLastSection, true);
254 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionEnd);
255 }
256 }
257
258 SegmentBoundaryAtom* SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(const char* name, bool start)
259 {
260 // e.g. __DATA__begin
261 char temp[18];
262 strlcpy(temp, name, 7);
263 char* segName = strdup(temp);
264
265 warning("grandfathering in old symbol '%s' as alias for 'segment$%s$%s'", name, start ? "start" : "end", segName);
266
267 if ( start ) {
268 const ld::Section* section = new ld::Section(segName, "__start", ld::Section::typeFirstSection, true);
269 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionStart);
270 }
271 else {
272 const ld::Section* section = new ld::Section(segName, "__end", ld::Section::typeLastSection, true);
273 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionEnd);
274 }
275 }
276
277 void Resolver::initializeState()
278 {
279 // set initial objc constraint based on command line options
280 if ( _options.objcGc() )
281 _internal.objcObjectConstraint = ld::File::objcConstraintRetainReleaseOrGC;
282 else if ( _options.objcGcOnly() )
283 _internal.objcObjectConstraint = ld::File::objcConstraintGC;
284
285 _internal.cpuSubType = _options.subArchitecture();
286 _internal.minOSVersion = _options.minOSversion();
287 _internal.derivedPlatformLoadCommand = 0;
288
289 // In -r mode, look for -linker_option additions
290 if ( _options.outputKind() == Options::kObjectFile ) {
291 ld::relocatable::File::LinkerOptionsList lo = _options.linkerOptions();
292 for (relocatable::File::LinkerOptionsList::const_iterator it=lo.begin(); it != lo.end(); ++it) {
293 doLinkerOption(*it, "command line");
294 }
295 }
296 }
297
298 void Resolver::buildAtomList()
299 {
300 // each input files contributes initial atoms
301 _atoms.reserve(1024);
302 _inputFiles.forEachInitialAtom(*this, _internal);
303
304 _completedInitialObjectFiles = true;
305
306 //_symbolTable.printStatistics();
307 }
308
309
310 void Resolver::doLinkerOption(const std::vector<const char*>& linkerOption, const char* fileName)
311 {
312 if ( linkerOption.size() == 1 ) {
313 const char* lo1 = linkerOption.front();
314 if ( strncmp(lo1, "-l", 2) == 0 ) {
315 _internal.linkerOptionLibraries.insert(&lo1[2]);
316 }
317 else {
318 warning("unknown linker option from object file ignored: '%s' in %s", lo1, fileName);
319 }
320 }
321 else if ( linkerOption.size() == 2 ) {
322 const char* lo2a = linkerOption[0];
323 const char* lo2b = linkerOption[1];
324 if ( strcmp(lo2a, "-framework") == 0 ) {
325 _internal.linkerOptionFrameworks.insert(lo2b);
326 }
327 else {
328 warning("unknown linker option from object file ignored: '%s' '%s' from %s", lo2a, lo2b, fileName);
329 }
330 }
331 else {
332 warning("unknown linker option from object file ignored, starting with: '%s' from %s", linkerOption.front(), fileName);
333 }
334 }
335
336 static void userReadableSwiftVersion(uint8_t value, char versionString[64])
337 {
338 switch (value) {
339 case 1:
340 strcpy(versionString, "1.0");
341 break;
342 case 2:
343 strcpy(versionString, "1.1");
344 break;
345 case 3:
346 strcpy(versionString, "2.0");
347 break;
348 default:
349 sprintf(versionString, "unknown ABI version 0x%02X", value);
350 }
351 }
352
353 void Resolver::doFile(const ld::File& file)
354 {
355 const ld::relocatable::File* objFile = dynamic_cast<const ld::relocatable::File*>(&file);
356 const ld::dylib::File* dylibFile = dynamic_cast<const ld::dylib::File*>(&file);
357
358 if ( objFile != NULL ) {
359 // if file has linker options, process them
360 ld::relocatable::File::LinkerOptionsList* lo = objFile->linkerOptions();
361 if ( lo != NULL && !_options.ignoreAutoLink() ) {
362 for (relocatable::File::LinkerOptionsList::const_iterator it=lo->begin(); it != lo->end(); ++it) {
363 this->doLinkerOption(*it, file.path());
364 }
365 // <rdar://problem/23053404> process any additional linker-options introduced by this new archive member being loaded
366 if ( _completedInitialObjectFiles ) {
367 _inputFiles.addLinkerOptionLibraries(_internal, *this);
368 _inputFiles.createIndirectDylibs();
369 }
370 }
371 // Resolve bitcode section in the object file
372 if ( _options.bundleBitcode() ) {
373 if ( objFile->getBitcode() == NULL ) {
374 // No bitcode section, figure out if the object file comes from LTO/compiler static library
375 if (objFile->sourceKind() != ld::relocatable::File::kSourceLTO &&
376 objFile->sourceKind() != ld::relocatable::File::kSourceCompilerArchive ) {
377 switch ( _options.platform() ) {
378 case Options::kPlatformOSX:
379 case Options::kPlatformUnknown:
380 warning("all bitcode will be dropped because '%s' was built without bitcode. "
381 "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());
382 _internal.filesWithBitcode.clear();
383 _internal.dropAllBitcode = true;
384 break;
385 case Options::kPlatformiOS:
386 throwf("'%s' does not contain bitcode. "
387 "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 break;
389 case Options::kPlatformWatchOS:
390 #if SUPPORT_APPLE_TV
391 case Options::kPlatform_tvOS:
392 #endif
393 throwf("'%s' does not contain bitcode. "
394 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) or obtain an updated library from the vendor", file.path());
395 break;
396 }
397 }
398 } else {
399 // contains bitcode, check if it is just a marker
400 if ( objFile->getBitcode()->isMarker() ) {
401 // if -bitcode_verify_bundle is used, check if all the object files participate in the linking have full bitcode embedded.
402 // error on any marker encountered.
403 if ( _options.verifyBitcode() )
404 throwf("bitcode bundle could not be generated because '%s' was built without full bitcode. "
405 "All object files and libraries for bitcode must be generated from Xcode Archive or Install build",
406 objFile->path());
407 // update the internal state that marker is encountered.
408 _internal.embedMarkerOnly = true;
409 _internal.filesWithBitcode.clear();
410 _internal.dropAllBitcode = true;
411 } else if ( !_internal.dropAllBitcode )
412 _internal.filesWithBitcode.push_back(objFile);
413 }
414 }
415
416 // update which form of ObjC is being used
417 switch ( file.objCConstraint() ) {
418 case ld::File::objcConstraintNone:
419 break;
420 case ld::File::objcConstraintRetainRelease:
421 if ( _internal.objcObjectConstraint == ld::File::objcConstraintGC )
422 throwf("%s built with incompatible Garbage Collection settings to link with previous .o files", file.path());
423 if ( _options.objcGcOnly() )
424 throwf("command line specified -objc_gc_only, but file is retain/release based: %s", file.path());
425 if ( _options.objcGc() )
426 throwf("command line specified -objc_gc, but file is retain/release based: %s", file.path());
427 if ( !_options.targetIOSSimulator() && (_internal.objcObjectConstraint != ld::File::objcConstraintRetainReleaseForSimulator) )
428 _internal.objcObjectConstraint = ld::File::objcConstraintRetainRelease;
429 break;
430 case ld::File::objcConstraintRetainReleaseOrGC:
431 if ( _internal.objcObjectConstraint == ld::File::objcConstraintNone )
432 _internal.objcObjectConstraint = ld::File::objcConstraintRetainReleaseOrGC;
433 if ( _options.targetIOSSimulator() )
434 warning("linking ObjC for iOS Simulator, but object file (%s) was compiled for MacOSX", file.path());
435 break;
436 case ld::File::objcConstraintGC:
437 if ( _internal.objcObjectConstraint == ld::File::objcConstraintRetainRelease )
438 throwf("%s built with incompatible Garbage Collection settings to link with previous .o files", file.path());
439 _internal.objcObjectConstraint = ld::File::objcConstraintGC;
440 if ( _options.targetIOSSimulator() )
441 warning("linking ObjC for iOS Simulator, but object file (%s) was compiled for MacOSX", file.path());
442 break;
443 case ld::File::objcConstraintRetainReleaseForSimulator:
444 if ( _internal.objcObjectConstraint == ld::File::objcConstraintNone ) {
445 if ( !_options.targetIOSSimulator() && (_options.outputKind() != Options::kObjectFile) )
446 warning("ObjC object file (%s) was compiled for iOS Simulator, but linking for MacOSX", file.path());
447 _internal.objcObjectConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
448 }
449 else if ( _internal.objcObjectConstraint != ld::File::objcConstraintRetainReleaseForSimulator ) {
450 _internal.objcObjectConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
451 }
452 break;
453 }
454
455 // verify all files use same version of Swift language
456 if ( file.swiftVersion() != 0 ) {
457 if ( _internal.swiftVersion == 0 ) {
458 _internal.swiftVersion = file.swiftVersion();
459 }
460 else if ( file.swiftVersion() != _internal.swiftVersion ) {
461 char fileVersion[64];
462 char otherVersion[64];
463 userReadableSwiftVersion(file.swiftVersion(), fileVersion);
464 userReadableSwiftVersion(_internal.swiftVersion, otherVersion);
465 if ( file.swiftVersion() > _internal.swiftVersion ) {
466 throwf("%s compiled with newer version of Swift language (%s) than previous files (%s)",
467 file.path(), fileVersion, otherVersion);
468 }
469 else {
470 throwf("%s compiled with older version of Swift language (%s) than previous files (%s)",
471 file.path(), fileVersion, otherVersion);
472 }
473 }
474 }
475
476 // in -r mode, if any .o files have dwarf then add UUID to output .o file
477 if ( objFile->debugInfo() == ld::relocatable::File::kDebugInfoDwarf )
478 _internal.someObjectFileHasDwarf = true;
479
480 // remember if any .o file did not have MH_SUBSECTIONS_VIA_SYMBOLS bit set
481 if ( ! objFile->canScatterAtoms() )
482 _internal.allObjectFilesScatterable = false;
483
484 // update minOSVersion off all .o files
485 uint32_t objMinOS = objFile->minOSVersion();
486 if ( !objMinOS )
487 _internal.objectFileFoundWithNoVersion = true;
488
489 uint32_t objPlatformLC = objFile->platformLoadCommand();
490 if ( (objPlatformLC != 0) && (_internal.derivedPlatformLoadCommand == 0) && (_options.outputKind() == Options::kObjectFile) )
491 _internal.derivedPlatformLoadCommand = objPlatformLC;
492
493 if ( (_options.outputKind() == Options::kObjectFile) && (objMinOS > _internal.minOSVersion) )
494 _internal.minOSVersion = objMinOS;
495
496 // update cpu-sub-type
497 cpu_subtype_t nextObjectSubType = file.cpuSubType();
498 switch ( _options.architecture() ) {
499 case CPU_TYPE_ARM:
500 if ( _options.subArchitecture() != nextObjectSubType ) {
501 if ( (_options.subArchitecture() == CPU_SUBTYPE_ARM_ALL) && _options.forceCpuSubtypeAll() ) {
502 // hack to support gcc multillib build that tries to make sub-type-all slice
503 }
504 else if ( nextObjectSubType == CPU_SUBTYPE_ARM_ALL ) {
505 warning("CPU_SUBTYPE_ARM_ALL subtype is deprecated: %s", file.path());
506 }
507 else if ( _options.allowSubArchitectureMismatches() ) {
508 //warning("object file %s was built for different arm sub-type (%d) than link command line (%d)",
509 // file.path(), nextObjectSubType, _options.subArchitecture());
510 }
511 else {
512 throwf("object file %s was built for different arm sub-type (%d) than link command line (%d)",
513 file.path(), nextObjectSubType, _options.subArchitecture());
514 }
515 }
516 break;
517
518 case CPU_TYPE_I386:
519 _internal.cpuSubType = CPU_SUBTYPE_I386_ALL;
520 break;
521
522 case CPU_TYPE_X86_64:
523 if ( _options.subArchitecture() != nextObjectSubType ) {
524 if ( _options.allowSubArchitectureMismatches() ) {
525 warning("object file %s was built for different x86_64 sub-type (%d) than link command line (%d)",
526 file.path(), nextObjectSubType, _options.subArchitecture());
527 }
528 else {
529 throwf("object file %s was built for different x86_64 sub-type (%d) than link command line (%d)",
530 file.path(), nextObjectSubType, _options.subArchitecture());
531 }
532 }
533 break;
534 }
535 }
536 if ( dylibFile != NULL ) {
537 // Check dylib for bitcode, if the library install path is relative path or @rpath, it has to contain bitcode
538 if ( _options.bundleBitcode() ) {
539 bool isSystemFramework = ( dylibFile->installPath() != NULL ) && ( dylibFile->installPath()[0] == '/' );
540 if ( dylibFile->getBitcode() == NULL && !isSystemFramework ) {
541 // Check if the dylib is from toolchain by checking the path
542 char tcLibPath[PATH_MAX];
543 char ldPath[PATH_MAX];
544 char tempPath[PATH_MAX];
545 uint32_t bufSize = PATH_MAX;
546 // toolchain library path should pointed to *.xctoolchain/usr/lib
547 if ( _NSGetExecutablePath(ldPath, &bufSize) != -1 ) {
548 if ( realpath(ldPath, tempPath) != NULL ) {
549 char* lastSlash = strrchr(tempPath, '/');
550 if ( lastSlash != NULL )
551 strcpy(lastSlash, "/../lib");
552 }
553 }
554 // Compare toolchain library path to the dylib path
555 if ( realpath(tempPath, tcLibPath) == NULL ||
556 realpath(dylibFile->path(), tempPath) == NULL ||
557 strncmp(tcLibPath, tempPath, strlen(tcLibPath)) != 0 ) {
558 switch ( _options.platform() ) {
559 case Options::kPlatformOSX:
560 case Options::kPlatformUnknown:
561 warning("all bitcode will be dropped because '%s' was built without bitcode. "
562 "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());
563 _internal.filesWithBitcode.clear();
564 _internal.dropAllBitcode = true;
565 break;
566 case Options::kPlatformiOS:
567 throwf("'%s' does not contain bitcode. "
568 "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());
569 break;
570 case Options::kPlatformWatchOS:
571 #if SUPPORT_APPLE_TV
572 case Options::kPlatform_tvOS:
573 #endif
574 throwf("'%s' does not contain bitcode. "
575 "You must rebuild it with bitcode enabled (Xcode setting ENABLE_BITCODE) or obtain an updated library from the vendor", file.path());
576 break;
577 }
578 }
579 }
580 // Error on bitcode marker in non-system frameworks if -bitcode_verify is used
581 if ( _options.verifyBitcode() && !isSystemFramework &&
582 dylibFile->getBitcode() != NULL && dylibFile->getBitcode()->isMarker() )
583 throwf("bitcode bundle could not be generated because '%s' was built without full bitcode. "
584 "All frameworks and dylibs for bitcode must be generated from Xcode Archive or Install build",
585 dylibFile->path());
586 }
587
588 // update which form of ObjC dylibs are being linked
589 switch ( dylibFile->objCConstraint() ) {
590 case ld::File::objcConstraintNone:
591 break;
592 case ld::File::objcConstraintRetainRelease:
593 if ( _internal.objcDylibConstraint == ld::File::objcConstraintGC )
594 throwf("%s built with incompatible Garbage Collection settings to link with previous dylibs", file.path());
595 if ( _options.objcGcOnly() )
596 throwf("command line specified -objc_gc_only, but dylib is retain/release based: %s", file.path());
597 if ( _options.objcGc() )
598 throwf("command line specified -objc_gc, but dylib is retain/release based: %s", file.path());
599 if ( _options.targetIOSSimulator() )
600 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file.path());
601 _internal.objcDylibConstraint = ld::File::objcConstraintRetainRelease;
602 break;
603 case ld::File::objcConstraintRetainReleaseOrGC:
604 if ( _internal.objcDylibConstraint == ld::File::objcConstraintNone )
605 _internal.objcDylibConstraint = ld::File::objcConstraintRetainReleaseOrGC;
606 if ( _options.targetIOSSimulator() )
607 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file.path());
608 break;
609 case ld::File::objcConstraintGC:
610 if ( _internal.objcDylibConstraint == ld::File::objcConstraintRetainRelease )
611 throwf("%s built with incompatible Garbage Collection settings to link with previous dylibs", file.path());
612 if ( _options.targetIOSSimulator() )
613 warning("linking ObjC for iOS Simulator, but dylib (%s) was compiled for MacOSX", file.path());
614 _internal.objcDylibConstraint = ld::File::objcConstraintGC;
615 break;
616 case ld::File::objcConstraintRetainReleaseForSimulator:
617 if ( _internal.objcDylibConstraint == ld::File::objcConstraintNone )
618 _internal.objcDylibConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
619 else if ( _internal.objcDylibConstraint != ld::File::objcConstraintRetainReleaseForSimulator ) {
620 warning("ObjC dylib (%s) was compiled for iOS Simulator, but dylibs others were compiled for MacOSX", file.path());
621 _internal.objcDylibConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
622 }
623 break;
624 }
625 if ( _options.checkDylibsAreAppExtensionSafe() && !dylibFile->appExtensionSafe() ) {
626 warning("linking against a dylib which is not safe for use in application extensions: %s", file.path());
627 }
628 const char* depInstallName = dylibFile->installPath();
629 // <rdar://problem/17229513> embedded frameworks are only supported on iOS 8 and later
630 if ( (depInstallName != NULL) && (depInstallName[0] != '/') ) {
631 if ( (_options.iOSVersionMin() != iOSVersionUnset) && (_options.iOSVersionMin() < iOS_8_0) ) {
632 // <rdar://problem/17598404> only warn about linking against embedded dylib if it is built for iOS 8 or later
633 if ( dylibFile->minOSVersion() >= iOS_8_0 )
634 throwf("embedded dylibs/frameworks are only supported on iOS 8.0 and later (%s)", depInstallName);
635 }
636 }
637 if ( _options.sharedRegionEligible() ) {
638 assert(depInstallName != NULL);
639 if ( depInstallName[0] == '@' ) {
640 warning("invalid -install_name (%s) in dependent dylib (%s). Dylibs/frameworks which might go in dyld shared cache "
641 "cannot link with dylib that uses @rpath, @loader_path, etc.", depInstallName, dylibFile->path());
642 } else if ( (strncmp(depInstallName, "/usr/lib/", 9) != 0) && (strncmp(depInstallName, "/System/Library/", 16) != 0) ) {
643 warning("invalid -install_name (%s) in dependent dylib (%s). Dylibs/frameworks which might go in dyld shared cache "
644 "cannot link with dylibs that won't be in the shared cache", depInstallName, dylibFile->path());
645 }
646 }
647 }
648
649 }
650
651 void Resolver::doAtom(const ld::Atom& atom)
652 {
653 //fprintf(stderr, "Resolver::doAtom(%p), name=%s, sect=%s, scope=%d\n", &atom, atom.name(), atom.section().sectionName(), atom.scope());
654 if ( _ltoCodeGenFinished && (atom.contentType() == ld::Atom::typeLTOtemporary) && (atom.scope() != ld::Atom::scopeTranslationUnit) )
655 warning("'%s' is implemented in bitcode, but it was loaded too late", atom.name());
656
657 // add to list of known atoms
658 _atoms.push_back(&atom);
659
660 // adjust scope
661 if ( _options.hasExportRestrictList() || _options.hasReExportList() ) {
662 const char* name = atom.name();
663 switch ( atom.scope() ) {
664 case ld::Atom::scopeTranslationUnit:
665 break;
666 case ld::Atom::scopeLinkageUnit:
667 if ( _options.hasExportMaskList() && _options.shouldExport(name) ) {
668 // <rdar://problem/5062685> ld does not report error when -r is used and exported symbols are not defined.
669 if ( _options.outputKind() == Options::kObjectFile )
670 throwf("cannot export hidden symbol %s", name);
671 // .objc_class_name_* symbols are special
672 if ( atom.section().type() != ld::Section::typeObjC1Classes ) {
673 if ( atom.definition() == ld::Atom::definitionProxy ) {
674 // .exp file says to export a symbol, but that symbol is in some dylib being linked
675 if ( _options.canReExportSymbols() ) {
676 // marking proxy atom as global triggers the re-export
677 (const_cast<ld::Atom*>(&atom))->setScope(ld::Atom::scopeGlobal);
678 }
679 else if ( _options.outputKind() == Options::kDynamicLibrary ) {
680 if ( atom.file() != NULL )
681 warning("target OS does not support re-exporting symbol %s from %s\n", _options.demangleSymbol(name), atom.file()->path());
682 else
683 warning("target OS does not support re-exporting symbol %s\n", _options.demangleSymbol(name));
684 }
685 }
686 else {
687 if ( atom.file() != NULL )
688 warning("cannot export hidden symbol %s from %s", _options.demangleSymbol(name), atom.file()->path());
689 else
690 warning("cannot export hidden symbol %s", _options.demangleSymbol(name));
691 }
692 }
693 }
694 else if ( _options.shouldReExport(name) && _options.canReExportSymbols() ) {
695 if ( atom.definition() == ld::Atom::definitionProxy ) {
696 // marking proxy atom as global triggers the re-export
697 (const_cast<ld::Atom*>(&atom))->setScope(ld::Atom::scopeGlobal);
698 }
699 else {
700 throwf("requested re-export symbol %s is not from a dylib, but from %s\n", _options.demangleSymbol(name), atom.file()->path());
701 }
702 }
703 break;
704 case ld::Atom::scopeGlobal:
705 // check for globals that are downgraded to hidden
706 if ( ! _options.shouldExport(name) ) {
707 (const_cast<ld::Atom*>(&atom))->setScope(ld::Atom::scopeLinkageUnit);
708 //fprintf(stderr, "demote %s to hidden\n", name);
709 }
710 if ( _options.canReExportSymbols() && _options.shouldReExport(name) ) {
711 throwf("requested re-export symbol %s is not from a dylib, but from %s\n", _options.demangleSymbol(name), atom.file()->path());
712 }
713 break;
714 }
715 }
716
717 // work around for kernel that uses 'l' labels in assembly code
718 if ( (atom.symbolTableInclusion() == ld::Atom::symbolTableNotInFinalLinkedImages)
719 && (atom.name()[0] == 'l') && (_options.outputKind() == Options::kStaticExecutable)
720 && (strncmp(atom.name(), "ltmp", 4) != 0) )
721 (const_cast<ld::Atom*>(&atom))->setSymbolTableInclusion(ld::Atom::symbolTableIn);
722
723
724 // tell symbol table about non-static atoms
725 if ( atom.scope() != ld::Atom::scopeTranslationUnit ) {
726 _symbolTable.add(atom, _options.deadCodeStrip() && (_completedInitialObjectFiles || _options.allowDeadDuplicates()));
727
728 // add symbol aliases defined on the command line
729 if ( _options.haveCmdLineAliases() ) {
730 const std::vector<Options::AliasPair>& aliases = _options.cmdLineAliases();
731 for (std::vector<Options::AliasPair>::const_iterator it=aliases.begin(); it != aliases.end(); ++it) {
732 if ( strcmp(it->realName, atom.name()) == 0 ) {
733 const AliasAtom* alias = new AliasAtom(atom, it->alias);
734 _aliasesFromCmdLine.push_back(alias);
735 this->doAtom(*alias);
736 }
737 }
738 }
739 }
740
741 // convert references by-name or by-content to by-slot
742 this->convertReferencesToIndirect(atom);
743
744 // remember if any atoms are proxies that require LTO
745 if ( atom.contentType() == ld::Atom::typeLTOtemporary )
746 _haveLLVMObjs = true;
747
748 // remember if any atoms are aliases
749 if ( atom.section().type() == ld::Section::typeTempAlias )
750 _haveAliases = true;
751
752 if ( _options.deadCodeStrip() ) {
753 // add to set of dead-strip-roots, all symbols that the compiler marks as don't strip
754 if ( atom.dontDeadStrip() )
755 _deadStripRoots.insert(&atom);
756 else if ( atom.dontDeadStripIfReferencesLive() )
757 _dontDeadStripIfReferencesLive.push_back(&atom);
758
759 if ( atom.scope() == ld::Atom::scopeGlobal ) {
760 // <rdar://problem/5524973> -exported_symbols_list that has wildcards and -dead_strip
761 // in dylibs, every global atom in initial .o files is a root
762 if ( _options.hasWildCardExportRestrictList() || _options.allGlobalsAreDeadStripRoots() ) {
763 if ( _options.shouldExport(atom.name()) )
764 _deadStripRoots.insert(&atom);
765 }
766 }
767 }
768 }
769
770 bool Resolver::isDtraceProbe(ld::Fixup::Kind kind)
771 {
772 switch (kind) {
773 case ld::Fixup::kindStoreX86DtraceCallSiteNop:
774 case ld::Fixup::kindStoreX86DtraceIsEnableSiteClear:
775 case ld::Fixup::kindStoreARMDtraceCallSiteNop:
776 case ld::Fixup::kindStoreARMDtraceIsEnableSiteClear:
777 case ld::Fixup::kindStoreARM64DtraceCallSiteNop:
778 case ld::Fixup::kindStoreARM64DtraceIsEnableSiteClear:
779 case ld::Fixup::kindStoreThumbDtraceCallSiteNop:
780 case ld::Fixup::kindStoreThumbDtraceIsEnableSiteClear:
781 case ld::Fixup::kindDtraceExtra:
782 return true;
783 default:
784 break;
785 }
786 return false;
787 }
788
789 void Resolver::convertReferencesToIndirect(const ld::Atom& atom)
790 {
791 // convert references by-name or by-content to by-slot
792 SymbolTable::IndirectBindingSlot slot;
793 const ld::Atom* dummy;
794 ld::Fixup::iterator end = atom.fixupsEnd();
795 for (ld::Fixup::iterator fit=atom.fixupsBegin(); fit != end; ++fit) {
796 if ( fit->kind == ld::Fixup::kindLinkerOptimizationHint )
797 _internal.someObjectHasOptimizationHints = true;
798 switch ( fit->binding ) {
799 case ld::Fixup::bindingByNameUnbound:
800 if ( isDtraceProbe(fit->kind) && (_options.outputKind() != Options::kObjectFile ) ) {
801 // in final linked images, remove reference
802 fit->binding = ld::Fixup::bindingNone;
803 }
804 else {
805 slot = _symbolTable.findSlotForName(fit->u.name);
806 fit->binding = ld::Fixup::bindingsIndirectlyBound;
807 fit->u.bindingIndex = slot;
808 }
809 break;
810 case ld::Fixup::bindingByContentBound:
811 switch ( fit->u.target->combine() ) {
812 case ld::Atom::combineNever:
813 case ld::Atom::combineByName:
814 assert(0 && "wrong combine type for bind by content");
815 break;
816 case ld::Atom::combineByNameAndContent:
817 slot = _symbolTable.findSlotForContent(fit->u.target, &dummy);
818 fit->binding = ld::Fixup::bindingsIndirectlyBound;
819 fit->u.bindingIndex = slot;
820 break;
821 case ld::Atom::combineByNameAndReferences:
822 slot = _symbolTable.findSlotForReferences(fit->u.target, &dummy);
823 fit->binding = ld::Fixup::bindingsIndirectlyBound;
824 fit->u.bindingIndex = slot;
825 break;
826 }
827 break;
828 case ld::Fixup::bindingNone:
829 case ld::Fixup::bindingDirectlyBound:
830 case ld::Fixup::bindingsIndirectlyBound:
831 break;
832 }
833 }
834 }
835
836
837 void Resolver::addInitialUndefines()
838 {
839 // add initial undefines from -u option
840 for (Options::UndefinesIterator it=_options.initialUndefinesBegin(); it != _options.initialUndefinesEnd(); ++it) {
841 _symbolTable.findSlotForName(*it);
842 }
843 }
844
845 void Resolver::resolveUndefines()
846 {
847 // keep looping until no more undefines were added in last loop
848 unsigned int undefineGenCount = 0xFFFFFFFF;
849 while ( undefineGenCount != _symbolTable.updateCount() ) {
850 undefineGenCount = _symbolTable.updateCount();
851 std::vector<const char*> undefineNames;
852 _symbolTable.undefines(undefineNames);
853 for(std::vector<const char*>::iterator it = undefineNames.begin(); it != undefineNames.end(); ++it) {
854 const char* undef = *it;
855 // load for previous undefine may also have loaded this undefine, so check again
856 if ( ! _symbolTable.hasName(undef) ) {
857 _inputFiles.searchLibraries(undef, true, true, false, *this);
858 if ( !_symbolTable.hasName(undef) && (_options.outputKind() != Options::kObjectFile) ) {
859 if ( strncmp(undef, "section$", 8) == 0 ) {
860 if ( strncmp(undef, "section$start$", 14) == 0 ) {
861 this->doAtom(*SectionBoundaryAtom::makeSectionBoundaryAtom(undef, true, &undef[14]));
862 }
863 else if ( strncmp(undef, "section$end$", 12) == 0 ) {
864 this->doAtom(*SectionBoundaryAtom::makeSectionBoundaryAtom(undef, false, &undef[12]));
865 }
866 }
867 else if ( strncmp(undef, "segment$", 8) == 0 ) {
868 if ( strncmp(undef, "segment$start$", 14) == 0 ) {
869 this->doAtom(*SegmentBoundaryAtom::makeSegmentBoundaryAtom(undef, true, &undef[14]));
870 }
871 else if ( strncmp(undef, "segment$end$", 12) == 0 ) {
872 this->doAtom(*SegmentBoundaryAtom::makeSegmentBoundaryAtom(undef, false, &undef[12]));
873 }
874 }
875 else if ( _options.outputKind() == Options::kPreload ) {
876 // for iBoot grandfather in old style section labels
877 int undefLen = strlen(undef);
878 if ( strcmp(&undef[undefLen-7], "__begin") == 0 ) {
879 if ( undefLen > 13 )
880 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef, true));
881 else
882 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef, true));
883 }
884 else if ( strcmp(&undef[undefLen-5], "__end") == 0 ) {
885 if ( undefLen > 11 )
886 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef, false));
887 else
888 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef, false));
889 }
890 }
891 }
892 }
893 }
894 // <rdar://problem/5894163> need to search archives for overrides of common symbols
895 if ( _symbolTable.hasExternalTentativeDefinitions() ) {
896 bool searchDylibs = (_options.commonsMode() == Options::kCommonsOverriddenByDylibs);
897 std::vector<const char*> tents;
898 _symbolTable.tentativeDefs(tents);
899 for(std::vector<const char*>::iterator it = tents.begin(); it != tents.end(); ++it) {
900 // load for previous tentative may also have loaded this tentative, so check again
901 const ld::Atom* curAtom = _symbolTable.atomForSlot(_symbolTable.findSlotForName(*it));
902 assert(curAtom != NULL);
903 if ( curAtom->definition() == ld::Atom::definitionTentative ) {
904 _inputFiles.searchLibraries(*it, searchDylibs, true, true, *this);
905 }
906 }
907 }
908 }
909
910 // Use linker options to resolve any remaining undefined symbols
911 if ( !_internal.linkerOptionLibraries.empty() || !_internal.linkerOptionFrameworks.empty() ) {
912 std::vector<const char*> undefineNames;
913 _symbolTable.undefines(undefineNames);
914 if ( undefineNames.size() != 0 ) {
915 for (std::vector<const char*>::iterator it = undefineNames.begin(); it != undefineNames.end(); ++it) {
916 const char* undef = *it;
917 if ( ! _symbolTable.hasName(undef) ) {
918 _inputFiles.searchLibraries(undef, true, true, false, *this);
919 }
920 }
921 }
922 }
923
924 // create proxies as needed for undefined symbols
925 if ( (_options.undefinedTreatment() != Options::kUndefinedError) || (_options.outputKind() == Options::kObjectFile) ) {
926 std::vector<const char*> undefineNames;
927 _symbolTable.undefines(undefineNames);
928 for(std::vector<const char*>::iterator it = undefineNames.begin(); it != undefineNames.end(); ++it) {
929 const char* undefName = *it;
930 // <rdar://problem/14547001> "ld -r -exported_symbol _foo" has wrong error message if _foo is undefined
931 bool makeProxy = true;
932 if ( (_options.outputKind() == Options::kObjectFile) && _options.hasExportMaskList() && _options.shouldExport(undefName) )
933 makeProxy = false;
934
935 if ( makeProxy )
936 this->doAtom(*new UndefinedProxyAtom(undefName));
937 }
938 }
939
940 // support -U option
941 if ( _options.someAllowedUndefines() ) {
942 std::vector<const char*> undefineNames;
943 _symbolTable.undefines(undefineNames);
944 for(std::vector<const char*>::iterator it = undefineNames.begin(); it != undefineNames.end(); ++it) {
945 if ( _options.allowedUndefined(*it) ) {
946 // make proxy
947 this->doAtom(*new UndefinedProxyAtom(*it));
948 }
949 }
950 }
951
952 // After resolving all the undefs within the linkageUnit, record all the remaining undefs and all the proxies.
953 if (_options.bundleBitcode() && _options.hideSymbols())
954 _symbolTable.mustPreserveForBitcode(_internal.allUndefProxies);
955
956 }
957
958
959 void Resolver::markLive(const ld::Atom& atom, WhyLiveBackChain* previous)
960 {
961 //fprintf(stderr, "markLive(%p) %s\n", &atom, atom.name());
962 // if -why_live cares about this symbol, then dump chain
963 if ( (previous->referer != NULL) && _options.printWhyLive(atom.name()) ) {
964 fprintf(stderr, "%s from %s\n", atom.name(), atom.file()->path());
965 int depth = 1;
966 for(WhyLiveBackChain* p = previous; p != NULL; p = p->previous, ++depth) {
967 for(int i=depth; i > 0; --i)
968 fprintf(stderr, " ");
969 fprintf(stderr, "%s from %s\n", p->referer->name(), p->referer->file()->path());
970 }
971 }
972
973 // if already marked live, then done (stop recursion)
974 if ( atom.live() )
975 return;
976
977 // mark this atom is live
978 (const_cast<ld::Atom*>(&atom))->setLive();
979
980 // mark all atoms it references as live
981 WhyLiveBackChain thisChain;
982 thisChain.previous = previous;
983 thisChain.referer = &atom;
984 for (ld::Fixup::iterator fit = atom.fixupsBegin(), end=atom.fixupsEnd(); fit != end; ++fit) {
985 const ld::Atom* target;
986 switch ( fit->kind ) {
987 case ld::Fixup::kindNone:
988 case ld::Fixup::kindNoneFollowOn:
989 case ld::Fixup::kindNoneGroupSubordinate:
990 case ld::Fixup::kindNoneGroupSubordinateFDE:
991 case ld::Fixup::kindNoneGroupSubordinateLSDA:
992 case ld::Fixup::kindNoneGroupSubordinatePersonality:
993 case ld::Fixup::kindSetTargetAddress:
994 case ld::Fixup::kindSubtractTargetAddress:
995 case ld::Fixup::kindStoreTargetAddressLittleEndian32:
996 case ld::Fixup::kindStoreTargetAddressLittleEndian64:
997 case ld::Fixup::kindStoreTargetAddressBigEndian32:
998 case ld::Fixup::kindStoreTargetAddressBigEndian64:
999 case ld::Fixup::kindStoreTargetAddressX86PCRel32:
1000 case ld::Fixup::kindStoreTargetAddressX86BranchPCRel32:
1001 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoad:
1002 case ld::Fixup::kindStoreTargetAddressX86PCRel32GOTLoadNowLEA:
1003 case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoad:
1004 case ld::Fixup::kindStoreTargetAddressX86PCRel32TLVLoadNowLEA:
1005 case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoad:
1006 case ld::Fixup::kindStoreTargetAddressX86Abs32TLVLoadNowLEA:
1007 case ld::Fixup::kindStoreTargetAddressARMBranch24:
1008 case ld::Fixup::kindStoreTargetAddressThumbBranch22:
1009 #if SUPPORT_ARCH_arm64
1010 case ld::Fixup::kindStoreTargetAddressARM64Branch26:
1011 case ld::Fixup::kindStoreTargetAddressARM64Page21:
1012 case ld::Fixup::kindStoreTargetAddressARM64GOTLoadPage21:
1013 case ld::Fixup::kindStoreTargetAddressARM64GOTLeaPage21:
1014 case ld::Fixup::kindStoreTargetAddressARM64TLVPLoadPage21:
1015 case ld::Fixup::kindStoreTargetAddressARM64TLVPLoadNowLeaPage21:
1016 #endif
1017 if ( fit->binding == ld::Fixup::bindingByContentBound ) {
1018 // normally this was done in convertReferencesToIndirect()
1019 // but a archive loaded .o file may have a forward reference
1020 SymbolTable::IndirectBindingSlot slot;
1021 const ld::Atom* dummy;
1022 switch ( fit->u.target->combine() ) {
1023 case ld::Atom::combineNever:
1024 case ld::Atom::combineByName:
1025 assert(0 && "wrong combine type for bind by content");
1026 break;
1027 case ld::Atom::combineByNameAndContent:
1028 slot = _symbolTable.findSlotForContent(fit->u.target, &dummy);
1029 fit->binding = ld::Fixup::bindingsIndirectlyBound;
1030 fit->u.bindingIndex = slot;
1031 break;
1032 case ld::Atom::combineByNameAndReferences:
1033 slot = _symbolTable.findSlotForReferences(fit->u.target, &dummy);
1034 fit->binding = ld::Fixup::bindingsIndirectlyBound;
1035 fit->u.bindingIndex = slot;
1036 break;
1037 }
1038 }
1039 switch ( fit->binding ) {
1040 case ld::Fixup::bindingDirectlyBound:
1041 markLive(*(fit->u.target), &thisChain);
1042 break;
1043 case ld::Fixup::bindingByNameUnbound:
1044 // doAtom() did not convert to indirect in dead-strip mode, so that now
1045 fit->u.bindingIndex = _symbolTable.findSlotForName(fit->u.name);
1046 fit->binding = ld::Fixup::bindingsIndirectlyBound;
1047 // fall into next case
1048 case ld::Fixup::bindingsIndirectlyBound:
1049 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1050 if ( target == NULL ) {
1051 const char* targetName = _symbolTable.indirectName(fit->u.bindingIndex);
1052 _inputFiles.searchLibraries(targetName, true, true, false, *this);
1053 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1054 }
1055 if ( target != NULL ) {
1056 if ( target->definition() == ld::Atom::definitionTentative ) {
1057 // <rdar://problem/5894163> need to search archives for overrides of common symbols
1058 bool searchDylibs = (_options.commonsMode() == Options::kCommonsOverriddenByDylibs);
1059 _inputFiles.searchLibraries(target->name(), searchDylibs, true, true, *this);
1060 // recompute target since it may have been overridden by searchLibraries()
1061 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1062 }
1063 this->markLive(*target, &thisChain);
1064 }
1065 else {
1066 _atomsWithUnresolvedReferences.push_back(&atom);
1067 }
1068 break;
1069 default:
1070 assert(0 && "bad binding during dead stripping");
1071 }
1072 break;
1073 default:
1074 break;
1075 }
1076 }
1077
1078 }
1079
1080 class NotLiveLTO {
1081 public:
1082 bool operator()(const ld::Atom* atom) const {
1083 if (atom->live() || atom->dontDeadStrip() )
1084 return false;
1085 // don't kill combinable atoms in first pass
1086 switch ( atom->combine() ) {
1087 case ld::Atom::combineByNameAndContent:
1088 case ld::Atom::combineByNameAndReferences:
1089 return false;
1090 default:
1091 return true;
1092 }
1093 }
1094 };
1095
1096 void Resolver::deadStripOptimize(bool force)
1097 {
1098 // only do this optimization with -dead_strip
1099 if ( ! _options.deadCodeStrip() )
1100 return;
1101
1102 // add entry point (main) to live roots
1103 const ld::Atom* entry = this->entryPoint(true);
1104 if ( entry != NULL )
1105 _deadStripRoots.insert(entry);
1106
1107 // add -exported_symbols_list, -init, and -u entries to live roots
1108 for (Options::UndefinesIterator uit=_options.initialUndefinesBegin(); uit != _options.initialUndefinesEnd(); ++uit) {
1109 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName(*uit);
1110 if ( _internal.indirectBindingTable[slot] == NULL ) {
1111 _inputFiles.searchLibraries(*uit, false, true, false, *this);
1112 }
1113 if ( _internal.indirectBindingTable[slot] != NULL )
1114 _deadStripRoots.insert(_internal.indirectBindingTable[slot]);
1115 }
1116
1117 // this helper is only referenced by synthesize stubs, assume it will be used
1118 if ( _internal.classicBindingHelper != NULL )
1119 _deadStripRoots.insert(_internal.classicBindingHelper);
1120
1121 // this helper is only referenced by synthesize stubs, assume it will be used
1122 if ( _internal.compressedFastBinderProxy != NULL )
1123 _deadStripRoots.insert(_internal.compressedFastBinderProxy);
1124
1125 // this helper is only referenced by synthesized lazy stubs, assume it will be used
1126 if ( _internal.lazyBindingHelper != NULL )
1127 _deadStripRoots.insert(_internal.lazyBindingHelper);
1128
1129 // add all dont-dead-strip atoms as roots
1130 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1131 const ld::Atom* atom = *it;
1132 if ( atom->dontDeadStrip() ) {
1133 //fprintf(stderr, "dont dead strip: %p %s %s\n", atom, atom->section().sectionName(), atom->name());
1134 _deadStripRoots.insert(atom);
1135 // unset liveness, so markLive() will recurse
1136 (const_cast<ld::Atom*>(atom))->setLive(0);
1137 }
1138 }
1139
1140 // mark all roots as live, and all atoms they reference
1141 for (std::set<const ld::Atom*>::iterator it=_deadStripRoots.begin(); it != _deadStripRoots.end(); ++it) {
1142 WhyLiveBackChain rootChain;
1143 rootChain.previous = NULL;
1144 rootChain.referer = *it;
1145 this->markLive(**it, &rootChain);
1146 }
1147
1148 // special case atoms that need to be live if they reference something live
1149 if ( ! _dontDeadStripIfReferencesLive.empty() ) {
1150 for (std::vector<const ld::Atom*>::iterator it=_dontDeadStripIfReferencesLive.begin(); it != _dontDeadStripIfReferencesLive.end(); ++it) {
1151 const Atom* liveIfRefLiveAtom = *it;
1152 //fprintf(stderr, "live-if-live atom: %s\n", liveIfRefLiveAtom->name());
1153 if ( liveIfRefLiveAtom->live() )
1154 continue;
1155 bool hasLiveRef = false;
1156 for (ld::Fixup::iterator fit=liveIfRefLiveAtom->fixupsBegin(); fit != liveIfRefLiveAtom->fixupsEnd(); ++fit) {
1157 const Atom* target = NULL;
1158 switch ( fit->binding ) {
1159 case ld::Fixup::bindingDirectlyBound:
1160 target = fit->u.target;
1161 break;
1162 case ld::Fixup::bindingsIndirectlyBound:
1163 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1164 break;
1165 default:
1166 break;
1167 }
1168 if ( (target != NULL) && target->live() )
1169 hasLiveRef = true;
1170 }
1171 if ( hasLiveRef ) {
1172 WhyLiveBackChain rootChain;
1173 rootChain.previous = NULL;
1174 rootChain.referer = liveIfRefLiveAtom;
1175 this->markLive(*liveIfRefLiveAtom, &rootChain);
1176 }
1177 }
1178 }
1179
1180 // now remove all non-live atoms from _atoms
1181 const bool log = false;
1182 if ( log ) {
1183 fprintf(stderr, "deadStripOptimize() all %ld atoms with liveness:\n", _atoms.size());
1184 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1185 const ld::File* file = (*it)->file();
1186 fprintf(stderr, " live=%d atom=%p name=%s from=%s\n", (*it)->live(), *it, (*it)->name(), (file ? file->path() : "<internal>"));
1187 }
1188 }
1189
1190 if ( _haveLLVMObjs && !force ) {
1191 // <rdar://problem/9777977> don't remove combinable atoms, they may come back in lto output
1192 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), NotLiveLTO()), _atoms.end());
1193 _symbolTable.removeDeadAtoms();
1194 }
1195 else {
1196 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), NotLive()), _atoms.end());
1197 }
1198
1199 if ( log ) {
1200 fprintf(stderr, "deadStripOptimize() %ld remaining atoms\n", _atoms.size());
1201 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1202 fprintf(stderr, " live=%d atom=%p name=%s\n", (*it)->live(), *it, (*it)->name());
1203 }
1204 }
1205 }
1206
1207
1208 // This is called when LTO is used but -dead_strip is not used.
1209 // Some undefines were eliminated by LTO, but others were not.
1210 void Resolver::remainingUndefines(std::vector<const char*>& undefs)
1211 {
1212 StringSet undefSet;
1213 // search all atoms for references that are unbound
1214 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1215 const ld::Atom* atom = *it;
1216 for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
1217 switch ( (ld::Fixup::TargetBinding)fit->binding ) {
1218 case ld::Fixup::bindingByNameUnbound:
1219 assert(0 && "should not be by-name this late");
1220 undefSet.insert(fit->u.name);
1221 break;
1222 case ld::Fixup::bindingsIndirectlyBound:
1223 if ( _internal.indirectBindingTable[fit->u.bindingIndex] == NULL ) {
1224 undefSet.insert(_symbolTable.indirectName(fit->u.bindingIndex));
1225 }
1226 break;
1227 case ld::Fixup::bindingByContentBound:
1228 case ld::Fixup::bindingNone:
1229 case ld::Fixup::bindingDirectlyBound:
1230 break;
1231 }
1232 }
1233 }
1234 // look for any initial undefines that are still undefined
1235 for (Options::UndefinesIterator uit=_options.initialUndefinesBegin(); uit != _options.initialUndefinesEnd(); ++uit) {
1236 if ( ! _symbolTable.hasName(*uit) ) {
1237 undefSet.insert(*uit);
1238 }
1239 }
1240
1241 // copy set to vector
1242 for (StringSet::const_iterator it=undefSet.begin(); it != undefSet.end(); ++it) {
1243 fprintf(stderr, "undef: %s\n", *it);
1244 undefs.push_back(*it);
1245 }
1246 }
1247
1248 void Resolver::liveUndefines(std::vector<const char*>& undefs)
1249 {
1250 StringSet undefSet;
1251 // search all live atoms for references that are unbound
1252 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1253 const ld::Atom* atom = *it;
1254 if ( ! atom->live() )
1255 continue;
1256 for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
1257 switch ( (ld::Fixup::TargetBinding)fit->binding ) {
1258 case ld::Fixup::bindingByNameUnbound:
1259 assert(0 && "should not be by-name this late");
1260 undefSet.insert(fit->u.name);
1261 break;
1262 case ld::Fixup::bindingsIndirectlyBound:
1263 if ( _internal.indirectBindingTable[fit->u.bindingIndex] == NULL ) {
1264 undefSet.insert(_symbolTable.indirectName(fit->u.bindingIndex));
1265 }
1266 break;
1267 case ld::Fixup::bindingByContentBound:
1268 case ld::Fixup::bindingNone:
1269 case ld::Fixup::bindingDirectlyBound:
1270 break;
1271 }
1272 }
1273 }
1274 // look for any initial undefines that are still undefined
1275 for (Options::UndefinesIterator uit=_options.initialUndefinesBegin(); uit != _options.initialUndefinesEnd(); ++uit) {
1276 if ( ! _symbolTable.hasName(*uit) ) {
1277 undefSet.insert(*uit);
1278 }
1279 }
1280
1281 // copy set to vector
1282 for (StringSet::const_iterator it=undefSet.begin(); it != undefSet.end(); ++it) {
1283 undefs.push_back(*it);
1284 }
1285 }
1286
1287
1288
1289 // <rdar://problem/8252819> warn when .objc_class_name_* symbol missing
1290 class ExportedObjcClass
1291 {
1292 public:
1293 ExportedObjcClass(const Options& opt) : _options(opt) {}
1294
1295 bool operator()(const char* name) const {
1296 if ( (strncmp(name, ".objc_class_name_", 17) == 0) && _options.shouldExport(name) ) {
1297 warning("ignoring undefined symbol %s from -exported_symbols_list", name);
1298 return true;
1299 }
1300 const char* s = strstr(name, "CLASS_$_");
1301 if ( s != NULL ) {
1302 char temp[strlen(name)+16];
1303 strcpy(temp, ".objc_class_name_");
1304 strcat(temp, &s[8]);
1305 if ( _options.wasRemovedExport(temp) ) {
1306 warning("ignoring undefined symbol %s from -exported_symbols_list", temp);
1307 return true;
1308 }
1309 }
1310 return false;
1311 }
1312 private:
1313 const Options& _options;
1314 };
1315
1316
1317 // temp hack for undefined aliases
1318 class UndefinedAlias
1319 {
1320 public:
1321 UndefinedAlias(const Options& opt) : _aliases(opt.cmdLineAliases()) {}
1322
1323 bool operator()(const char* name) const {
1324 for (std::vector<Options::AliasPair>::const_iterator it=_aliases.begin(); it != _aliases.end(); ++it) {
1325 if ( strcmp(it->realName, name) == 0 ) {
1326 warning("undefined base symbol '%s' for alias '%s'", name, it->alias);
1327 return true;
1328 }
1329 }
1330 return false;
1331 }
1332 private:
1333 const std::vector<Options::AliasPair>& _aliases;
1334 };
1335
1336
1337
1338 static const char* pathLeafName(const char* path)
1339 {
1340 const char* shortPath = strrchr(path, '/');
1341 if ( shortPath == NULL )
1342 return path;
1343 else
1344 return &shortPath[1];
1345 }
1346
1347 bool Resolver::printReferencedBy(const char* name, SymbolTable::IndirectBindingSlot slot)
1348 {
1349 unsigned foundReferenceCount = 0;
1350 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1351 const ld::Atom* atom = *it;
1352 for (ld::Fixup::iterator fit=atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
1353 if ( fit->binding == ld::Fixup::bindingsIndirectlyBound ) {
1354 if ( fit->u.bindingIndex == slot ) {
1355 if ( atom->contentType() == ld::Atom::typeNonLazyPointer ) {
1356 const ld::Atom* existingAtom;
1357 unsigned int nlSlot = _symbolTable.findSlotForReferences(atom, &existingAtom);
1358 if ( printReferencedBy(name, nlSlot) )
1359 ++foundReferenceCount;
1360 }
1361 else if ( atom->contentType() == ld::Atom::typeCFI ) {
1362 fprintf(stderr, " Dwarf Exception Unwind Info (__eh_frame) in %s\n", pathLeafName(atom->file()->path()));
1363 ++foundReferenceCount;
1364 }
1365 else {
1366 fprintf(stderr, " %s in %s\n", _options.demangleSymbol(atom->name()), pathLeafName(atom->file()->path()));
1367 ++foundReferenceCount;
1368 break; // if undefined used twice in a function, only show first
1369 }
1370 }
1371 }
1372 }
1373 if ( foundReferenceCount > 6 ) {
1374 fprintf(stderr, " ...\n");
1375 break; // only show first six uses of undefined symbol
1376 }
1377 }
1378 return (foundReferenceCount != 0);
1379 }
1380
1381 void Resolver::checkUndefines(bool force)
1382 {
1383 // when using LTO, undefines are checked after bitcode is optimized
1384 if ( _haveLLVMObjs && !force )
1385 return;
1386
1387 // error out on any remaining undefines
1388 bool doPrint = true;
1389 bool doError = true;
1390 switch ( _options.undefinedTreatment() ) {
1391 case Options::kUndefinedError:
1392 break;
1393 case Options::kUndefinedDynamicLookup:
1394 doError = false;
1395 break;
1396 case Options::kUndefinedWarning:
1397 doError = false;
1398 break;
1399 case Options::kUndefinedSuppress:
1400 doError = false;
1401 doPrint = false;
1402 break;
1403 }
1404 std::vector<const char*> unresolvableUndefines;
1405 if ( _options.deadCodeStrip() )
1406 this->liveUndefines(unresolvableUndefines);
1407 else if( _haveLLVMObjs )
1408 this->remainingUndefines(unresolvableUndefines); // <rdar://problem/10052396> LTO may have eliminated need for some undefines
1409 else
1410 _symbolTable.undefines(unresolvableUndefines);
1411
1412 // <rdar://problem/8252819> assert when .objc_class_name_* symbol missing
1413 if ( _options.hasExportMaskList() ) {
1414 unresolvableUndefines.erase(std::remove_if(unresolvableUndefines.begin(), unresolvableUndefines.end(), ExportedObjcClass(_options)), unresolvableUndefines.end());
1415 }
1416
1417 // hack to temporarily make missing aliases a warning
1418 if ( _options.haveCmdLineAliases() ) {
1419 unresolvableUndefines.erase(std::remove_if(unresolvableUndefines.begin(), unresolvableUndefines.end(), UndefinedAlias(_options)), unresolvableUndefines.end());
1420 }
1421
1422 const int unresolvableCount = unresolvableUndefines.size();
1423 int unresolvableExportsCount = 0;
1424 if ( unresolvableCount != 0 ) {
1425 if ( doPrint ) {
1426 if ( _options.printArchPrefix() )
1427 fprintf(stderr, "Undefined symbols for architecture %s:\n", _options.architectureName());
1428 else
1429 fprintf(stderr, "Undefined symbols:\n");
1430 for (int i=0; i < unresolvableCount; ++i) {
1431 const char* name = unresolvableUndefines[i];
1432 unsigned int slot = _symbolTable.findSlotForName(name);
1433 fprintf(stderr, " \"%s\", referenced from:\n", _options.demangleSymbol(name));
1434 // scan all atoms for references
1435 bool foundAtomReference = printReferencedBy(name, slot);
1436 // scan command line options
1437 if ( !foundAtomReference ) {
1438 // might be from -init command line option
1439 if ( (_options.initFunctionName() != NULL) && (strcmp(name, _options.initFunctionName()) == 0) ) {
1440 fprintf(stderr, " -init command line option\n");
1441 }
1442 // or might be from exported symbol option
1443 else if ( _options.hasExportMaskList() && _options.shouldExport(name) ) {
1444 fprintf(stderr, " -exported_symbol[s_list] command line option\n");
1445 }
1446 // or might be from re-exported symbol option
1447 else if ( _options.hasReExportList() && _options.shouldReExport(name) ) {
1448 fprintf(stderr, " -reexported_symbols_list command line option\n");
1449 }
1450 else if ( (_options.outputKind() == Options::kDynamicExecutable)
1451 && (_options.entryName() != NULL) && (strcmp(name, _options.entryName()) == 0) ) {
1452 fprintf(stderr, " implicit entry/start for main executable\n");
1453 }
1454 else {
1455 bool isInitialUndefine = false;
1456 for (Options::UndefinesIterator uit=_options.initialUndefinesBegin(); uit != _options.initialUndefinesEnd(); ++uit) {
1457 if ( strcmp(*uit, name) == 0 ) {
1458 isInitialUndefine = true;
1459 break;
1460 }
1461 }
1462 if ( isInitialUndefine )
1463 fprintf(stderr, " -u command line option\n");
1464 }
1465 ++unresolvableExportsCount;
1466 }
1467 // be helpful and check for typos
1468 bool printedStart = false;
1469 for (SymbolTable::byNameIterator sit=_symbolTable.begin(); sit != _symbolTable.end(); sit++) {
1470 const ld::Atom* atom = *sit;
1471 if ( (atom != NULL) && (atom->symbolTableInclusion() == ld::Atom::symbolTableIn) && (strstr(atom->name(), name) != NULL) ) {
1472 if ( ! printedStart ) {
1473 fprintf(stderr, " (maybe you meant: %s", atom->name());
1474 printedStart = true;
1475 }
1476 else {
1477 fprintf(stderr, ", %s ", atom->name());
1478 }
1479 }
1480 }
1481 if ( printedStart )
1482 fprintf(stderr, ")\n");
1483 // <rdar://problem/8989530> Add comment to error message when __ZTV symbols are undefined
1484 if ( strncmp(name, "__ZTV", 5) == 0 ) {
1485 fprintf(stderr, " NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.\n");
1486 }
1487 }
1488 }
1489 if ( doError )
1490 throw "symbol(s) not found";
1491 }
1492
1493 }
1494
1495
1496
1497 void Resolver::checkDylibSymbolCollisions()
1498 {
1499 for (SymbolTable::byNameIterator it=_symbolTable.begin(); it != _symbolTable.end(); it++) {
1500 const ld::Atom* atom = *it;
1501 if ( atom == NULL )
1502 continue;
1503 if ( atom->scope() == ld::Atom::scopeGlobal ) {
1504 // <rdar://problem/5048861> No warning about tentative definition conflicting with dylib definition
1505 // for each tentative definition in symbol table look for dylib that exports same symbol name
1506 if ( atom->definition() == ld::Atom::definitionTentative ) {
1507 _inputFiles.searchLibraries(atom->name(), true, false, false, *this);
1508 }
1509 // record any overrides of weak symbols in any linked dylib
1510 if ( (atom->definition() == ld::Atom::definitionRegular) && (atom->symbolTableInclusion() == ld::Atom::symbolTableIn) ) {
1511 if ( _inputFiles.searchWeakDefInDylib(atom->name()) )
1512 (const_cast<ld::Atom*>(atom))->setOverridesDylibsWeakDef();
1513 }
1514 }
1515 }
1516 }
1517
1518
1519 const ld::Atom* Resolver::entryPoint(bool searchArchives)
1520 {
1521 const char* symbolName = NULL;
1522 bool makingDylib = false;
1523 switch ( _options.outputKind() ) {
1524 case Options::kDynamicExecutable:
1525 case Options::kStaticExecutable:
1526 case Options::kDyld:
1527 case Options::kPreload:
1528 symbolName = _options.entryName();
1529 break;
1530 case Options::kDynamicLibrary:
1531 symbolName = _options.initFunctionName();
1532 makingDylib = true;
1533 break;
1534 case Options::kObjectFile:
1535 case Options::kDynamicBundle:
1536 case Options::kKextBundle:
1537 return NULL;
1538 break;
1539 }
1540 if ( symbolName != NULL ) {
1541 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName(symbolName);
1542 if ( (_internal.indirectBindingTable[slot] == NULL) && searchArchives ) {
1543 // <rdar://problem/7043256> ld64 can not find a -e entry point from an archive
1544 _inputFiles.searchLibraries(symbolName, false, true, false, *this);
1545 }
1546 if ( _internal.indirectBindingTable[slot] == NULL ) {
1547 if ( strcmp(symbolName, "start") == 0 )
1548 throwf("entry point (%s) undefined. Usually in crt1.o", symbolName);
1549 else
1550 throwf("entry point (%s) undefined.", symbolName);
1551 }
1552 else if ( _internal.indirectBindingTable[slot]->definition() == ld::Atom::definitionProxy ) {
1553 if ( makingDylib )
1554 throwf("-init function (%s) found in linked dylib, must be in dylib being linked", symbolName);
1555 }
1556 return _internal.indirectBindingTable[slot];
1557 }
1558 return NULL;
1559 }
1560
1561
1562 void Resolver::fillInHelpersInInternalState()
1563 {
1564 // look up well known atoms
1565 bool needsStubHelper = true;
1566 switch ( _options.outputKind() ) {
1567 case Options::kDynamicExecutable:
1568 case Options::kDynamicLibrary:
1569 case Options::kDynamicBundle:
1570 needsStubHelper = true;
1571 break;
1572 case Options::kDyld:
1573 case Options::kKextBundle:
1574 case Options::kObjectFile:
1575 case Options::kStaticExecutable:
1576 case Options::kPreload:
1577 needsStubHelper = false;
1578 break;
1579 }
1580
1581 _internal.classicBindingHelper = NULL;
1582 if ( needsStubHelper && !_options.makeCompressedDyldInfo() ) {
1583 // "dyld_stub_binding_helper" comes from .o file, so should already exist in symbol table
1584 if ( _symbolTable.hasName("dyld_stub_binding_helper") ) {
1585 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName("dyld_stub_binding_helper");
1586 _internal.classicBindingHelper = _internal.indirectBindingTable[slot];
1587 }
1588 }
1589
1590 _internal.lazyBindingHelper = NULL;
1591 if ( _options.usingLazyDylibLinking() ) {
1592 // "dyld_lazy_dylib_stub_binding_helper" comes from lazydylib1.o file, so should already exist in symbol table
1593 if ( _symbolTable.hasName("dyld_lazy_dylib_stub_binding_helper") ) {
1594 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName("dyld_lazy_dylib_stub_binding_helper");
1595 _internal.lazyBindingHelper = _internal.indirectBindingTable[slot];
1596 }
1597 if ( _internal.lazyBindingHelper == NULL )
1598 throw "symbol dyld_lazy_dylib_stub_binding_helper not defined (usually in lazydylib1.o)";
1599 }
1600
1601 _internal.compressedFastBinderProxy = NULL;
1602 if ( needsStubHelper && _options.makeCompressedDyldInfo() ) {
1603 // "dyld_stub_binder" comes from libSystem.dylib so will need to manually resolve
1604 if ( !_symbolTable.hasName("dyld_stub_binder") ) {
1605 _inputFiles.searchLibraries("dyld_stub_binder", true, false, false, *this);
1606 }
1607 if ( _symbolTable.hasName("dyld_stub_binder") ) {
1608 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName("dyld_stub_binder");
1609 _internal.compressedFastBinderProxy = _internal.indirectBindingTable[slot];
1610 }
1611 if ( _internal.compressedFastBinderProxy == NULL ) {
1612 if ( _options.undefinedTreatment() != Options::kUndefinedError ) {
1613 // make proxy
1614 _internal.compressedFastBinderProxy = new UndefinedProxyAtom("dyld_stub_binder");
1615 this->doAtom(*_internal.compressedFastBinderProxy);
1616 }
1617 }
1618 }
1619 }
1620
1621
1622 void Resolver::fillInInternalState()
1623 {
1624 // store atoms into their final section
1625 for (std::vector<const ld::Atom*>::iterator it = _atoms.begin(); it != _atoms.end(); ++it) {
1626 _internal.addAtom(**it);
1627 }
1628
1629 // <rdar://problem/7783918> make sure there is a __text section so that codesigning works
1630 if ( (_options.outputKind() == Options::kDynamicLibrary) || (_options.outputKind() == Options::kDynamicBundle) )
1631 _internal.getFinalSection(*new ld::Section("__TEXT", "__text", ld::Section::typeCode));
1632 }
1633
1634 void Resolver::fillInEntryPoint()
1635 {
1636 _internal.entryPoint = this->entryPoint(true);
1637 }
1638
1639 void Resolver::syncAliases()
1640 {
1641 if ( !_haveAliases || (_options.outputKind() == Options::kObjectFile) )
1642 return;
1643
1644 // Set attributes of alias to match its found target
1645 for (std::vector<const ld::Atom*>::iterator it = _atoms.begin(); it != _atoms.end(); ++it) {
1646 const ld::Atom* atom = *it;
1647 if ( atom->section().type() == ld::Section::typeTempAlias ) {
1648 assert(atom->fixupsBegin() != atom->fixupsEnd());
1649 for (ld::Fixup::iterator fit = atom->fixupsBegin(); fit != atom->fixupsEnd(); ++fit) {
1650 const ld::Atom* target;
1651 ld::Atom::Scope scope;
1652 assert(fit->kind == ld::Fixup::kindNoneFollowOn);
1653 switch ( fit->binding ) {
1654 case ld::Fixup::bindingByNameUnbound:
1655 break;
1656 case ld::Fixup::bindingsIndirectlyBound:
1657 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1658 assert(target != NULL);
1659 scope = atom->scope();
1660 (const_cast<Atom*>(atom))->setAttributesFromAtom(*target);
1661 // alias has same attributes as target, except for scope
1662 (const_cast<Atom*>(atom))->setScope(scope);
1663 break;
1664 default:
1665 assert(0 && "internal error: unexpected alias binding");
1666 }
1667 }
1668 }
1669 }
1670 }
1671
1672 void Resolver::removeCoalescedAwayAtoms()
1673 {
1674 const bool log = false;
1675 if ( log ) {
1676 fprintf(stderr, "removeCoalescedAwayAtoms() starts with %lu atoms\n", _atoms.size());
1677 }
1678 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), AtomCoalescedAway()), _atoms.end());
1679 if ( log ) {
1680 fprintf(stderr, "removeCoalescedAwayAtoms() after removing coalesced atoms, %lu remain\n", _atoms.size());
1681 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1682 fprintf(stderr, " atom=%p %s\n", *it, (*it)->name());
1683 }
1684 }
1685 }
1686
1687 void Resolver::linkTimeOptimize()
1688 {
1689 // only do work here if some llvm obj files where loaded
1690 if ( ! _haveLLVMObjs )
1691 return;
1692
1693 // <rdar://problem/15314161> LTO: Symbol multiply defined error should specify exactly where the symbol is found
1694 _symbolTable.checkDuplicateSymbols();
1695
1696 // run LLVM lto code-gen
1697 lto::OptimizeOptions optOpt;
1698 optOpt.outputFilePath = _options.outputFilePath();
1699 optOpt.tmpObjectFilePath = _options.tempLtoObjectPath();
1700 optOpt.preserveAllGlobals = _options.allGlobalsAreDeadStripRoots() || _options.hasExportRestrictList();
1701 optOpt.verbose = _options.verbose();
1702 optOpt.saveTemps = _options.saveTempFiles();
1703 optOpt.ltoCodegenOnly = _options.ltoCodegenOnly();
1704 optOpt.pie = _options.positionIndependentExecutable();
1705 optOpt.mainExecutable = _options.linkingMainExecutable();;
1706 optOpt.staticExecutable = (_options.outputKind() == Options::kStaticExecutable);
1707 optOpt.relocatable = (_options.outputKind() == Options::kObjectFile);
1708 optOpt.allowTextRelocs = _options.allowTextRelocs();
1709 optOpt.linkerDeadStripping = _options.deadCodeStrip();
1710 optOpt.needsUnwindInfoSection = _options.needsUnwindInfoSection();
1711 optOpt.keepDwarfUnwind = _options.keepDwarfUnwind();
1712 optOpt.verboseOptimizationHints = _options.verboseOptimizationHints();
1713 optOpt.armUsesZeroCostExceptions = _options.armUsesZeroCostExceptions();
1714 optOpt.simulator = _options.targetIOSSimulator();
1715 optOpt.ignoreMismatchPlatform = ((_options.outputKind() == Options::kPreload) || (_options.outputKind() == Options::kStaticExecutable));
1716 optOpt.bitcodeBundle = _options.bundleBitcode();
1717 optOpt.maxDefaultCommonAlignment = _options.maxDefaultCommonAlign();
1718 optOpt.arch = _options.architecture();
1719 optOpt.mcpu = _options.mcpuLTO();
1720 optOpt.platform = _options.platform();
1721 optOpt.llvmOptions = &_options.llvmOptions();
1722 optOpt.initialUndefines = &_options.initialUndefines();
1723
1724 std::vector<const ld::Atom*> newAtoms;
1725 std::vector<const char*> additionalUndefines;
1726 if ( ! lto::optimize(_atoms, _internal, optOpt, *this, newAtoms, additionalUndefines) )
1727 return; // if nothing done
1728 _ltoCodeGenFinished = true;
1729
1730 // add all newly created atoms to _atoms and update symbol table
1731 for(std::vector<const ld::Atom*>::iterator it = newAtoms.begin(); it != newAtoms.end(); ++it)
1732 this->doAtom(**it);
1733
1734 // some atoms might have been optimized way (marked coalesced), remove them
1735 this->removeCoalescedAwayAtoms();
1736
1737 // run through all atoms again and make sure newly codegened atoms have references bound
1738 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it)
1739 this->convertReferencesToIndirect(**it);
1740
1741 // adjust section of any new
1742 for (std::vector<const AliasAtom*>::const_iterator it=_aliasesFromCmdLine.begin(); it != _aliasesFromCmdLine.end(); ++it) {
1743 const AliasAtom* aliasAtom = *it;
1744 // update fields in AliasAtom to match newly constructed mach-o atom
1745 aliasAtom->setFinalAliasOf();
1746 }
1747
1748 // <rdar://problem/14609792> add any auto-link libraries requested by LTO output to dylibs to search
1749 _inputFiles.addLinkerOptionLibraries(_internal, *this);
1750 _inputFiles.createIndirectDylibs();
1751
1752 // resolve new undefines (e.g calls to _malloc and _memcpy that llvm compiler conjures up)
1753 for(std::vector<const char*>::iterator uit = additionalUndefines.begin(); uit != additionalUndefines.end(); ++uit) {
1754 const char *targetName = *uit;
1755 // these symbols may or may not already be in linker's symbol table
1756 if ( ! _symbolTable.hasName(targetName) ) {
1757 _inputFiles.searchLibraries(targetName, true, true, false, *this);
1758 }
1759 }
1760
1761 // if -dead_strip on command line
1762 if ( _options.deadCodeStrip() ) {
1763 // clear liveness bit
1764 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1765 (const_cast<ld::Atom*>(*it))->setLive((*it)->dontDeadStrip());
1766 }
1767 // and re-compute dead code
1768 this->deadStripOptimize(true);
1769 }
1770
1771 // <rdar://problem/12386559> if -exported_symbols_list on command line, re-force scope
1772 if ( _options.hasExportMaskList() ) {
1773 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1774 const ld::Atom* atom = *it;
1775 if ( atom->scope() == ld::Atom::scopeGlobal ) {
1776 if ( !_options.shouldExport(atom->name()) ) {
1777 (const_cast<ld::Atom*>(atom))->setScope(ld::Atom::scopeLinkageUnit);
1778 }
1779 }
1780 }
1781 }
1782
1783 if ( _options.outputKind() == Options::kObjectFile ) {
1784 // if -r mode, add proxies for new undefines (e.g. ___stack_chk_fail)
1785 this->resolveUndefines();
1786 }
1787 else {
1788 // last chance to check for undefines
1789 this->resolveUndefines();
1790 this->checkUndefines(true);
1791
1792 // check new code does not override some dylib
1793 this->checkDylibSymbolCollisions();
1794 }
1795 }
1796
1797
1798 void Resolver::tweakWeakness()
1799 {
1800 // <rdar://problem/7977374> Add command line options to control symbol weak-def bit on exported symbols
1801 if ( _options.hasWeakBitTweaks() ) {
1802 for (std::vector<ld::Internal::FinalSection*>::iterator sit = _internal.sections.begin(); sit != _internal.sections.end(); ++sit) {
1803 ld::Internal::FinalSection* sect = *sit;
1804 for (std::vector<const ld::Atom*>::iterator ait = sect->atoms.begin(); ait != sect->atoms.end(); ++ait) {
1805 const ld::Atom* atom = *ait;
1806 if ( atom->definition() != ld::Atom::definitionRegular )
1807 continue;
1808 const char* name = atom->name();
1809 if ( atom->scope() == ld::Atom::scopeGlobal ) {
1810 if ( atom->combine() == ld::Atom::combineNever ) {
1811 if ( _options.forceWeak(name) )
1812 (const_cast<ld::Atom*>(atom))->setCombine(ld::Atom::combineByName);
1813 }
1814 else if ( atom->combine() == ld::Atom::combineByName ) {
1815 if ( _options.forceNotWeak(name) )
1816 (const_cast<ld::Atom*>(atom))->setCombine(ld::Atom::combineNever);
1817 }
1818 }
1819 else {
1820 if ( _options.forceWeakNonWildCard(name) )
1821 warning("cannot force to be weak, non-external symbol %s", name);
1822 else if ( _options.forceNotWeakNonWildcard(name) )
1823 warning("cannot force to be not-weak, non-external symbol %s", name);
1824 }
1825 }
1826 }
1827 }
1828 }
1829
1830 void Resolver::dumpAtoms()
1831 {
1832 fprintf(stderr, "Resolver all atoms:\n");
1833 for (std::vector<const ld::Atom*>::const_iterator it=_atoms.begin(); it != _atoms.end(); ++it) {
1834 const ld::Atom* atom = *it;
1835 fprintf(stderr, " %p name=%s, def=%d\n", atom, atom->name(), atom->definition());
1836 }
1837 }
1838
1839 void Resolver::resolve()
1840 {
1841 this->initializeState();
1842 this->buildAtomList();
1843 this->addInitialUndefines();
1844 this->fillInHelpersInInternalState();
1845 this->resolveUndefines();
1846 this->deadStripOptimize();
1847 this->checkUndefines();
1848 this->checkDylibSymbolCollisions();
1849 this->syncAliases();
1850 this->removeCoalescedAwayAtoms();
1851 this->fillInEntryPoint();
1852 this->linkTimeOptimize();
1853 this->fillInInternalState();
1854 this->tweakWeakness();
1855 _symbolTable.checkDuplicateSymbols();
1856 }
1857
1858
1859
1860 } // namespace tool
1861 } // namespace ld
1862
1863
1864