]> git.saurik.com Git - apple/ld64.git/blob - src/ld/Resolver.cpp
51afe8dfca9b8ac3482228c50d9c347ff28abb2a
[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 #include "ld.hpp"
55 #include "Bitcode.hpp"
56 #include "InputFiles.h"
57 #include "SymbolTable.h"
58 #include "Resolver.h"
59 #include "parsers/lto_file.h"
60
61 #include "configure.h"
62
63 #define VAL(x) #x
64 #define STRINGIFY(x) VAL(x)
65
66 namespace ld {
67 namespace tool {
68
69
70 //
71 // An ExportAtom has no content. It exists so that the linker can track which imported
72 // symbols came from which dynamic libraries.
73 //
74 class UndefinedProxyAtom : public ld::Atom
75 {
76 public:
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)),
82 _name(nm) {}
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) { }
90
91 protected:
92
93 virtual ~UndefinedProxyAtom() {}
94
95 const char* _name;
96
97 static ld::Section _s_section;
98 };
99
100 ld::Section UndefinedProxyAtom::_s_section("__TEXT", "__import", ld::Section::typeImportProxies, true);
101
102
103
104
105 class AliasAtom : public ld::Atom
106 {
107 public:
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()),
113 _name(nm),
114 _aliasOf(target),
115 _fixup(0, ld::Fixup::k1of1, ld::Fixup::kindNoneFollowOn, &target) { }
116
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; }
136
137 void setFinalAliasOf() const {
138 (const_cast<AliasAtom*>(this))->setAttributesFromAtom(_aliasOf);
139 (const_cast<AliasAtom*>(this))->setScope(ld::Atom::scopeGlobal);
140 }
141
142 private:
143 const char* _name;
144 const ld::Atom& _aliasOf;
145 ld::Fixup _fixup;
146 };
147
148
149
150 class SectionBoundaryAtom : public ld::Atom
151 {
152 public:
153 static SectionBoundaryAtom* makeSectionBoundaryAtom(const char* name, bool start, const char* segSectName);
154 static SectionBoundaryAtom* makeOldSectionBoundaryAtom(const char* name, bool start);
155
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; }
163
164 private:
165
166 SectionBoundaryAtom(const char* nm, const ld::Section& sect,
167 ld::Atom::ContentType cont) :
168 ld::Atom(sect,
169 ld::Atom::definitionRegular,
170 ld::Atom::combineNever,
171 ld::Atom::scopeLinkageUnit,
172 cont,
173 ld::Atom::symbolTableNotIn,
174 false, false, true, ld::Atom::Alignment(0)),
175 _name(nm) { }
176
177 const char* _name;
178 };
179
180 SectionBoundaryAtom* SectionBoundaryAtom::makeSectionBoundaryAtom(const char* name, bool start, const char* segSectName)
181 {
182
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);
190 char segName[18];
191 strlcpy(segName, segSectName, segNameLen+1);
192
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));
195 }
196
197 SectionBoundaryAtom* SectionBoundaryAtom::makeOldSectionBoundaryAtom(const char* name, bool start)
198 {
199 // e.g. __DATA__bss__begin
200 char segName[18];
201 strlcpy(segName, name, 7);
202
203 char sectName[18];
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));
209 }
210
211
212
213
214 class SegmentBoundaryAtom : public ld::Atom
215 {
216 public:
217 static SegmentBoundaryAtom* makeSegmentBoundaryAtom(const char* name, bool start, const char* segName);
218 static SegmentBoundaryAtom* makeOldSegmentBoundaryAtom(const char* name, bool start);
219
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; }
227
228 private:
229
230 SegmentBoundaryAtom(const char* nm, const ld::Section& sect,
231 ld::Atom::ContentType cont) :
232 ld::Atom(sect,
233 ld::Atom::definitionRegular,
234 ld::Atom::combineNever,
235 ld::Atom::scopeLinkageUnit,
236 cont,
237 ld::Atom::symbolTableNotIn,
238 false, false, true, ld::Atom::Alignment(0)),
239 _name(nm) { }
240
241 const char* _name;
242 };
243
244 SegmentBoundaryAtom* SegmentBoundaryAtom::makeSegmentBoundaryAtom(const char* name, bool start, const char* segName)
245 {
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);
250
251 if ( start ) {
252 const ld::Section* section = new ld::Section(segName, "__start", ld::Section::typeFirstSection, true);
253 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionStart);
254 }
255 else {
256 const ld::Section* section = new ld::Section(segName, "__end", ld::Section::typeLastSection, true);
257 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionEnd);
258 }
259 }
260
261 SegmentBoundaryAtom* SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(const char* name, bool start)
262 {
263 // e.g. __DATA__begin
264 char temp[18];
265 strlcpy(temp, name, 7);
266 char* segName = strdup(temp);
267
268 warning("grandfathering in old symbol '%s' as alias for 'segment$%s$%s'", name, start ? "start" : "end", segName);
269
270 if ( start ) {
271 const ld::Section* section = new ld::Section(segName, "__start", ld::Section::typeFirstSection, true);
272 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionStart);
273 }
274 else {
275 const ld::Section* section = new ld::Section(segName, "__end", ld::Section::typeLastSection, true);
276 return new SegmentBoundaryAtom(name, *section, ld::Atom::typeSectionEnd);
277 }
278 }
279
280 void Resolver::initializeState()
281 {
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;
287
288 _internal.cpuSubType = _options.subArchitecture();
289 _internal.minOSVersion = _options.minOSversion();
290 _internal.derivedPlatform = 0;
291
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");
297 }
298 }
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);
303 #endif
304 }
305
306 void Resolver::buildAtomList()
307 {
308 // each input files contributes initial atoms
309 _atoms.reserve(1024);
310 _inputFiles.forEachInitialAtom(*this, _internal);
311
312 _completedInitialObjectFiles = true;
313
314 //_symbolTable.printStatistics();
315 }
316
317
318 void Resolver::doLinkerOption(const std::vector<const char*>& linkerOption, const char* fileName)
319 {
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]);
325 }
326 }
327 else {
328 warning("unknown linker option from object file ignored: '%s' in %s", lo1, fileName);
329 }
330 }
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);
337 }
338 }
339 else {
340 warning("unknown linker option from object file ignored: '%s' '%s' from %s", lo2a, lo2b, fileName);
341 }
342 }
343 else {
344 warning("unknown linker option from object file ignored, starting with: '%s' from %s", linkerOption.front(), fileName);
345 }
346 }
347
348
349 void Resolver::doFile(const ld::File& file)
350 {
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);
353
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());
360 }
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();
365 }
366 }
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;
383 break;
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());
387 break;
388 case Options::kPlatformWatchOS:
389 #if SUPPORT_APPLE_TV
390 case Options::kPlatform_tvOS:
391 #endif
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());
394 break;
395 }
396 }
397 } else {
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",
405 objFile->path());
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);
412 }
413 }
414
415 // update which form of ObjC is being used
416 switch ( file.objCConstraint() ) {
417 case ld::File::objcConstraintNone:
418 break;
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;
428 break;
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());
434 break;
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());
441 break;
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;
447 }
448 else if ( _internal.objcObjectConstraint != ld::File::objcConstraintRetainReleaseForSimulator ) {
449 _internal.objcObjectConstraint = ld::File::objcConstraintRetainReleaseForSimulator;
450 }
451 break;
452 }
453
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();
458 }
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);
468 } else {
469 throwf("%s compiled with newer version of Swift language (%s) than previous files (%s)",
470 file.path(), fileVersion, otherVersion);
471 }
472 }
473 else {
474 if ( _options.warnOnSwiftABIVersionMismatches() ) {
475 warning("%s compiled with older version of Swift language (%s) than previous files (%s)",
476 file.path(), fileVersion, otherVersion);
477 } else {
478 throwf("%s compiled with older version of Swift language (%s) than previous files (%s)",
479 file.path(), fileVersion, otherVersion);
480 }
481 }
482 }
483 }
484
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;
488
489 // remember if any .o file did not have MH_SUBSECTIONS_VIA_SYMBOLS bit set
490 if ( ! objFile->canScatterAtoms() )
491 _internal.allObjectFilesScatterable = false;
492
493 // remember if building for profiling (so we don't warn about initializers)
494 if ( objFile->hasllvmProfiling() )
495 _havellvmProfiling = true;
496
497 // update minOSVersion off all .o files
498 uint32_t objMinOS = objFile->minOSVersion();
499 if ( !objMinOS )
500 _internal.objectFileFoundWithNoVersion = true;
501 if ( (_options.outputKind() == Options::kObjectFile) && (objMinOS > _internal.minOSVersion) )
502 _internal.minOSVersion = objMinOS;
503
504 uint32_t objPlatform = objFile->platform();
505 if ( (objPlatform != 0) && (_options.outputKind() == Options::kObjectFile) && (_internal.derivedPlatform == 0) )
506 _internal.derivedPlatform = objPlatform;
507
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);
512 }
513
514 // update cpu-sub-type
515 cpu_subtype_t nextObjectSubType = file.cpuSubType();
516 switch ( _options.architecture() ) {
517 case CPU_TYPE_ARM:
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
521 }
522 else if ( nextObjectSubType == CPU_SUBTYPE_ARM_ALL ) {
523 warning("CPU_SUBTYPE_ARM_ALL subtype is deprecated: %s", file.path());
524 }
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());
528 }
529 else {
530 throwf("object file %s was built for different arm sub-type (%d) than link command line (%d)",
531 file.path(), nextObjectSubType, _options.subArchitecture());
532 }
533 }
534 break;
535
536 case CPU_TYPE_ARM64:
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());
541 }
542 else {
543 throwf("object file %s was built for different arm64 sub-type (%d) than link command line (%d)",
544 file.path(), nextObjectSubType, _options.subArchitecture());
545 }
546 }
547 break;
548
549 case CPU_TYPE_I386:
550 _internal.cpuSubType = CPU_SUBTYPE_I386_ALL;
551 break;
552
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());
558 }
559 else {
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());
562 }
563 }
564 break;
565 }
566 }
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");
583 }
584 }
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;
597 break;
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());
601 break;
602 case Options::kPlatformWatchOS:
603 #if SUPPORT_APPLE_TV
604 case Options::kPlatform_tvOS:
605 #endif
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());
608 break;
609 }
610 }
611 }
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",
617 dylibFile->path());
618 }
619
620 // update which form of ObjC dylibs are being linked
621 switch ( dylibFile->objCConstraint() ) {
622 case ld::File::objcConstraintNone:
623 break;
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;
634 break;
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());
640 break;
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;
647 break;
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;
654 }
655 break;
656 }
657
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();
662 }
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);
672 } else {
673 throwf("%s compiled with newer version of Swift language (%s) than previous files (%s)",
674 file.path(), fileVersion, otherVersion);
675 }
676 }
677 else {
678 if ( _options.warnOnSwiftABIVersionMismatches() ) {
679 warning("%s compiled with older version of Swift language (%s) than previous files (%s)",
680 file.path(), fileVersion, otherVersion);
681 } else {
682 throwf("%s compiled with older version of Swift language (%s) than previous files (%s)",
683 file.path(), fileVersion, otherVersion);
684 }
685 }
686 }
687 }
688
689 if ( _options.checkDylibsAreAppExtensionSafe() && !dylibFile->appExtensionSafe() ) {
690 warning("linking against a dylib which is not safe for use in application extensions: %s", file.path());
691 }
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);
699 }
700 }
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());
709 }
710 }
711 }
712
713 }
714
715 void Resolver::doAtom(const ld::Atom& atom)
716 {
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());
720
721 // add to list of known atoms
722 _atoms.push_back(&atom);
723
724 // adjust scope
725 if ( _options.hasExportRestrictList() || _options.hasReExportList() ) {
726 const char* name = atom.name();
727 switch ( atom.scope() ) {
728 case ld::Atom::scopeTranslationUnit:
729 break;
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);
742 }
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());
746 else
747 warning("target OS does not support re-exporting symbol %s\n", _options.demangleSymbol(name));
748 }
749 }
750 else {
751 if ( atom.file() != NULL )
752 warning("cannot export hidden symbol %s from %s", _options.demangleSymbol(name), atom.safeFilePath());
753 else
754 warning("cannot export hidden symbol %s", _options.demangleSymbol(name));
755 }
756 }
757 }
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);
762 }
763 else {
764 throwf("requested re-export symbol %s is not from a dylib, but from %s\n", _options.demangleSymbol(name), atom.safeFilePath());
765 }
766 }
767 break;
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);
773 }
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());
776 }
777 break;
778 }
779 }
780
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);
786
787
788 // tell symbol table about non-static atoms
789 if ( atom.scope() != ld::Atom::scopeTranslationUnit ) {
790 _symbolTable.add(atom, _options.deadCodeStrip() && (_completedInitialObjectFiles || _options.allowDeadDuplicates()));
791
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);
799 }
800 else {
801 const AliasAtom* alias = new AliasAtom(atom, it->alias);
802 _aliasesFromCmdLine.push_back(alias);
803 this->doAtom(*alias);
804 }
805 }
806 }
807 }
808 }
809
810 // convert references by-name or by-content to by-slot
811 this->convertReferencesToIndirect(atom);
812
813 // remember if any atoms are proxies that require LTO
814 if ( atom.contentType() == ld::Atom::typeLTOtemporary )
815 _haveLLVMObjs = true;
816
817 // remember if any atoms are aliases
818 if ( atom.section().type() == ld::Section::typeTempAlias )
819 _haveAliases = true;
820
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());
828 break;
829 default:
830 break;
831 }
832 }
833
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);
840
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);
847 }
848 }
849 }
850 }
851
852 bool Resolver::isDtraceProbe(ld::Fixup::Kind kind)
853 {
854 switch (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:
864 return true;
865 default:
866 break;
867 }
868 return false;
869 }
870
871 void Resolver::convertReferencesToIndirect(const ld::Atom& atom)
872 {
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;
885 }
886 else {
887 slot = _symbolTable.findSlotForName(fit->u.name);
888 fit->binding = ld::Fixup::bindingsIndirectlyBound;
889 fit->u.bindingIndex = slot;
890 }
891 break;
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");
897 break;
898 case ld::Atom::combineByNameAndContent:
899 slot = _symbolTable.findSlotForContent(fit->u.target, &dummy);
900 fit->binding = ld::Fixup::bindingsIndirectlyBound;
901 fit->u.bindingIndex = slot;
902 break;
903 case ld::Atom::combineByNameAndReferences:
904 slot = _symbolTable.findSlotForReferences(fit->u.target, &dummy);
905 fit->binding = ld::Fixup::bindingsIndirectlyBound;
906 fit->u.bindingIndex = slot;
907 break;
908 }
909 break;
910 case ld::Fixup::bindingNone:
911 case ld::Fixup::bindingDirectlyBound:
912 case ld::Fixup::bindingsIndirectlyBound:
913 break;
914 }
915 }
916 }
917
918
919 void Resolver::addInitialUndefines()
920 {
921 // add initial undefines from -u option
922 for (Options::UndefinesIterator it=_options.initialUndefinesBegin(); it != _options.initialUndefinesEnd(); ++it) {
923 _symbolTable.findSlotForName(*it);
924 }
925 }
926
927 void Resolver::resolveUndefines()
928 {
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]));
944 }
945 else if ( strncmp(undef, "section$end$", 12) == 0 ) {
946 this->doAtom(*SectionBoundaryAtom::makeSectionBoundaryAtom(undef, false, &undef[12]));
947 }
948 }
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]));
952 }
953 else if ( strncmp(undef, "segment$end$", 12) == 0 ) {
954 this->doAtom(*SegmentBoundaryAtom::makeSegmentBoundaryAtom(undef, false, &undef[12]));
955 }
956 }
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 ) {
961 if ( undefLen > 13 )
962 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef, true));
963 else
964 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef, true));
965 }
966 else if ( strcmp(&undef[undefLen-5], "__end") == 0 ) {
967 if ( undefLen > 11 )
968 this->doAtom(*SectionBoundaryAtom::makeOldSectionBoundaryAtom(undef, false));
969 else
970 this->doAtom(*SegmentBoundaryAtom::makeOldSegmentBoundaryAtom(undef, false));
971 }
972 }
973 }
974 }
975 }
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);
987 }
988 }
989 }
990 }
991
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);
1001 }
1002 }
1003 }
1004 }
1005
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) )
1015 makeProxy = false;
1016
1017 if ( makeProxy )
1018 this->doAtom(*new UndefinedProxyAtom(undefName));
1019 }
1020 }
1021
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) ) {
1028 // make proxy
1029 this->doAtom(*new UndefinedProxyAtom(*it));
1030 }
1031 }
1032 }
1033
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);
1037
1038 }
1039
1040
1041 void Resolver::markLive(const ld::Atom& atom, WhyLiveBackChain* previous)
1042 {
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());
1047 int depth = 1;
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());
1052 }
1053 }
1054
1055 // if already marked live, then done (stop recursion)
1056 if ( atom.live() )
1057 return;
1058
1059 // mark this atom is live
1060 (const_cast<ld::Atom*>(&atom))->setLive();
1061
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:
1098 #endif
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");
1108 break;
1109 case ld::Atom::combineByNameAndContent:
1110 slot = _symbolTable.findSlotForContent(fit->u.target, &dummy);
1111 fit->binding = ld::Fixup::bindingsIndirectlyBound;
1112 fit->u.bindingIndex = slot;
1113 break;
1114 case ld::Atom::combineByNameAndReferences:
1115 slot = _symbolTable.findSlotForReferences(fit->u.target, &dummy);
1116 fit->binding = ld::Fixup::bindingsIndirectlyBound;
1117 fit->u.bindingIndex = slot;
1118 break;
1119 }
1120 }
1121 switch ( fit->binding ) {
1122 case ld::Fixup::bindingDirectlyBound:
1123 markLive(*(fit->u.target), &thisChain);
1124 break;
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];
1136 }
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];
1144 }
1145 this->markLive(*target, &thisChain);
1146 }
1147 else {
1148 _atomsWithUnresolvedReferences.push_back(&atom);
1149 }
1150 break;
1151 default:
1152 assert(0 && "bad binding during dead stripping");
1153 }
1154 break;
1155 default:
1156 break;
1157 }
1158 }
1159
1160 }
1161
1162 class NotLiveLTO {
1163 public:
1164 bool operator()(const ld::Atom* atom) const {
1165 if (atom->live() || atom->dontDeadStrip() )
1166 return false;
1167 // don't kill combinable atoms in first pass
1168 switch ( atom->combine() ) {
1169 case ld::Atom::combineByNameAndContent:
1170 case ld::Atom::combineByNameAndReferences:
1171 return false;
1172 default:
1173 return true;
1174 }
1175 }
1176 };
1177
1178 void Resolver::deadStripOptimize(bool force)
1179 {
1180 // only do this optimization with -dead_strip
1181 if ( ! _options.deadCodeStrip() )
1182 return;
1183
1184 // add entry point (main) to live roots
1185 const ld::Atom* entry = this->entryPoint(true);
1186 if ( entry != NULL )
1187 _deadStripRoots.insert(entry);
1188
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);
1194 }
1195 if ( _internal.indirectBindingTable[slot] != NULL )
1196 _deadStripRoots.insert(_internal.indirectBindingTable[slot]);
1197 }
1198
1199 // this helper is only referenced by synthesize stubs, assume it will be used
1200 if ( _internal.classicBindingHelper != NULL )
1201 _deadStripRoots.insert(_internal.classicBindingHelper);
1202
1203 // this helper is only referenced by synthesize stubs, assume it will be used
1204 if ( _internal.compressedFastBinderProxy != NULL )
1205 _deadStripRoots.insert(_internal.compressedFastBinderProxy);
1206
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);
1210
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);
1219 }
1220 }
1221
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);
1228 }
1229
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() )
1236 continue;
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;
1243 break;
1244 case ld::Fixup::bindingsIndirectlyBound:
1245 target = _internal.indirectBindingTable[fit->u.bindingIndex];
1246 break;
1247 default:
1248 break;
1249 }
1250 if ( (target != NULL) && target->live() )
1251 hasLiveRef = true;
1252 }
1253 if ( hasLiveRef ) {
1254 WhyLiveBackChain rootChain;
1255 rootChain.previous = NULL;
1256 rootChain.referer = liveIfRefLiveAtom;
1257 this->markLive(*liveIfRefLiveAtom, &rootChain);
1258 }
1259 }
1260 }
1261
1262 // now remove all non-live atoms from _atoms
1263 const bool log = false;
1264 if ( log ) {
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>"));
1269 }
1270 }
1271
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();
1277 }
1278 else {
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());
1281 }
1282
1283 if ( log ) {
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());
1287 }
1288 }
1289 }
1290
1291
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)
1295 {
1296 StringSet undefSet;
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);
1305 break;
1306 case ld::Fixup::bindingsIndirectlyBound:
1307 if ( _internal.indirectBindingTable[fit->u.bindingIndex] == NULL ) {
1308 undefSet.insert(_symbolTable.indirectName(fit->u.bindingIndex));
1309 }
1310 break;
1311 case ld::Fixup::bindingByContentBound:
1312 case ld::Fixup::bindingNone:
1313 case ld::Fixup::bindingDirectlyBound:
1314 break;
1315 }
1316 }
1317 }
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);
1322 }
1323 }
1324
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);
1329 }
1330 }
1331
1332 void Resolver::liveUndefines(std::vector<const char*>& undefs)
1333 {
1334 StringSet undefSet;
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() )
1339 continue;
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);
1345 break;
1346 case ld::Fixup::bindingsIndirectlyBound:
1347 if ( _internal.indirectBindingTable[fit->u.bindingIndex] == NULL ) {
1348 undefSet.insert(_symbolTable.indirectName(fit->u.bindingIndex));
1349 }
1350 break;
1351 case ld::Fixup::bindingByContentBound:
1352 case ld::Fixup::bindingNone:
1353 case ld::Fixup::bindingDirectlyBound:
1354 break;
1355 }
1356 }
1357 }
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);
1362 }
1363 }
1364
1365 // copy set to vector
1366 for (StringSet::const_iterator it=undefSet.begin(); it != undefSet.end(); ++it) {
1367 undefs.push_back(*it);
1368 }
1369 }
1370
1371
1372
1373 // <rdar://problem/8252819> warn when .objc_class_name_* symbol missing
1374 class ExportedObjcClass
1375 {
1376 public:
1377 ExportedObjcClass(const Options& opt) : _options(opt) {}
1378
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);
1382 return true;
1383 }
1384 const char* s = strstr(name, "CLASS_$_");
1385 if ( s != NULL ) {
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);
1391 return true;
1392 }
1393 }
1394 return false;
1395 }
1396 private:
1397 const Options& _options;
1398 };
1399
1400
1401 // temp hack for undefined aliases
1402 class UndefinedAlias
1403 {
1404 public:
1405 UndefinedAlias(const Options& opt) : _aliases(opt.cmdLineAliases()) {}
1406
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);
1411 return true;
1412 }
1413 }
1414 return false;
1415 }
1416 private:
1417 const std::vector<Options::AliasPair>& _aliases;
1418 };
1419
1420
1421
1422 static const char* pathLeafName(const char* path)
1423 {
1424 const char* shortPath = strrchr(path, '/');
1425 if ( shortPath == NULL )
1426 return path;
1427 else
1428 return &shortPath[1];
1429 }
1430
1431 bool Resolver::printReferencedBy(const char* name, SymbolTable::IndirectBindingSlot slot)
1432 {
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;
1444 }
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;
1448 }
1449 else {
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
1453 }
1454 }
1455 }
1456 }
1457 if ( foundReferenceCount > 6 ) {
1458 fprintf(stderr, " ...\n");
1459 break; // only show first six uses of undefined symbol
1460 }
1461 }
1462 return (foundReferenceCount != 0);
1463 }
1464
1465 void Resolver::checkUndefines(bool force)
1466 {
1467 // when using LTO, undefines are checked after bitcode is optimized
1468 if ( _haveLLVMObjs && !force )
1469 return;
1470
1471 // error out on any remaining undefines
1472 bool doPrint = true;
1473 bool doError = true;
1474 switch ( _options.undefinedTreatment() ) {
1475 case Options::kUndefinedError:
1476 break;
1477 case Options::kUndefinedDynamicLookup:
1478 doError = false;
1479 break;
1480 case Options::kUndefinedWarning:
1481 doError = false;
1482 break;
1483 case Options::kUndefinedSuppress:
1484 doError = false;
1485 doPrint = false;
1486 break;
1487 }
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
1493 else
1494 _symbolTable.undefines(unresolvableUndefines);
1495
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());
1499 }
1500
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());
1504 }
1505
1506 const int unresolvableCount = unresolvableUndefines.size();
1507 int unresolvableExportsCount = 0;
1508 if ( unresolvableCount != 0 ) {
1509 if ( doPrint ) {
1510 if ( _options.printArchPrefix() )
1511 fprintf(stderr, "Undefined symbols for architecture %s:\n", _options.architectureName());
1512 else
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");
1525 }
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");
1529 }
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");
1533 }
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");
1537 }
1538 else {
1539 bool isInitialUndefine = false;
1540 for (Options::UndefinesIterator uit=_options.initialUndefinesBegin(); uit != _options.initialUndefinesEnd(); ++uit) {
1541 if ( strcmp(*uit, name) == 0 ) {
1542 isInitialUndefine = true;
1543 break;
1544 }
1545 }
1546 if ( isInitialUndefine )
1547 fprintf(stderr, " -u command line option\n");
1548 }
1549 ++unresolvableExportsCount;
1550 }
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;
1559 }
1560 else {
1561 fprintf(stderr, ", %s ", atom->name());
1562 }
1563 }
1564 }
1565 if ( printedStart )
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");
1570 }
1571 }
1572 }
1573 if ( doError )
1574 throw "symbol(s) not found";
1575 }
1576
1577 }
1578
1579
1580
1581 void Resolver::checkDylibSymbolCollisions()
1582 {
1583 for (SymbolTable::byNameIterator it=_symbolTable.begin(); it != _symbolTable.end(); it++) {
1584 const ld::Atom* atom = *it;
1585 if ( atom == NULL )
1586 continue;
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);
1592 }
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();
1597 }
1598 }
1599 }
1600 }
1601
1602
1603 const ld::Atom* Resolver::entryPoint(bool searchArchives)
1604 {
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();
1613 break;
1614 case Options::kDynamicLibrary:
1615 symbolName = _options.initFunctionName();
1616 makingDylib = true;
1617 break;
1618 case Options::kObjectFile:
1619 case Options::kDynamicBundle:
1620 case Options::kKextBundle:
1621 return NULL;
1622 break;
1623 }
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);
1629 }
1630 if ( _internal.indirectBindingTable[slot] == NULL ) {
1631 if ( strcmp(symbolName, "start") == 0 )
1632 throwf("entry point (%s) undefined. Usually in crt1.o", symbolName);
1633 else
1634 throwf("entry point (%s) undefined.", symbolName);
1635 }
1636 else if ( _internal.indirectBindingTable[slot]->definition() == ld::Atom::definitionProxy ) {
1637 if ( makingDylib )
1638 throwf("-init function (%s) found in linked dylib, must be in dylib being linked", symbolName);
1639 }
1640 return _internal.indirectBindingTable[slot];
1641 }
1642 return NULL;
1643 }
1644
1645
1646 void Resolver::fillInHelpersInInternalState()
1647 {
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;
1655 break;
1656 case Options::kDyld:
1657 case Options::kKextBundle:
1658 case Options::kObjectFile:
1659 case Options::kStaticExecutable:
1660 case Options::kPreload:
1661 needsStubHelper = false;
1662 break;
1663 }
1664
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];
1671 }
1672 }
1673
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];
1680 }
1681 if ( _internal.lazyBindingHelper == NULL )
1682 throw "symbol dyld_lazy_dylib_stub_binding_helper not defined (usually in lazydylib1.o)";
1683 }
1684
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);
1690 }
1691 if ( _symbolTable.hasName("dyld_stub_binder") ) {
1692 SymbolTable::IndirectBindingSlot slot = _symbolTable.findSlotForName("dyld_stub_binder");
1693 _internal.compressedFastBinderProxy = _internal.indirectBindingTable[slot];
1694 }
1695 if ( _internal.compressedFastBinderProxy == NULL ) {
1696 if ( _options.undefinedTreatment() != Options::kUndefinedError ) {
1697 // make proxy
1698 _internal.compressedFastBinderProxy = new UndefinedProxyAtom("dyld_stub_binder");
1699 this->doAtom(*_internal.compressedFastBinderProxy);
1700 }
1701 }
1702 }
1703 }
1704
1705
1706 void Resolver::fillInInternalState()
1707 {
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);
1711 }
1712
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));
1716 }
1717
1718 void Resolver::fillInEntryPoint()
1719 {
1720 _internal.entryPoint = this->entryPoint(true);
1721 }
1722
1723 void Resolver::syncAliases()
1724 {
1725 if ( !_haveAliases || (_options.outputKind() == Options::kObjectFile) )
1726 return;
1727
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:
1739 break;
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);
1747 break;
1748 default:
1749 assert(0 && "internal error: unexpected alias binding");
1750 }
1751 }
1752 }
1753 }
1754 }
1755
1756 void Resolver::removeCoalescedAwayAtoms()
1757 {
1758 const bool log = false;
1759 if ( log ) {
1760 fprintf(stderr, "removeCoalescedAwayAtoms() starts with %lu atoms\n", _atoms.size());
1761 }
1762 _atoms.erase(std::remove_if(_atoms.begin(), _atoms.end(), AtomCoalescedAway()), _atoms.end());
1763 if ( log ) {
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());
1767 }
1768 }
1769 }
1770
1771 void Resolver::linkTimeOptimize()
1772 {
1773 // only do work here if some llvm obj files where loaded
1774 if ( ! _haveLLVMObjs )
1775 return;
1776
1777 // <rdar://problem/15314161> LTO: Symbol multiply defined error should specify exactly where the symbol is found
1778 _symbolTable.checkDuplicateSymbols();
1779
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();
1813
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;
1819
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)
1822 this->doAtom(**it);
1823
1824 // some atoms might have been optimized way (marked coalesced), remove them
1825 this->removeCoalescedAwayAtoms();
1826
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);
1830
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();
1836 }
1837
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();
1841
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);
1848 }
1849 }
1850
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);
1859 }
1860
1861 // clear liveness bit
1862 (const_cast<ld::Atom*>(*it))->setLive((*it)->dontDeadStrip());
1863 }
1864 // and re-compute dead code
1865 this->deadStripOptimize(true);
1866 }
1867
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);
1875 }
1876 }
1877 }
1878 }
1879
1880 if ( _options.outputKind() == Options::kObjectFile ) {
1881 // if -r mode, add proxies for new undefines (e.g. ___stack_chk_fail)
1882 this->resolveUndefines();
1883 }
1884 else {
1885 // last chance to check for undefines
1886 this->resolveUndefines();
1887 this->checkUndefines(true);
1888
1889 // check new code does not override some dylib
1890 this->checkDylibSymbolCollisions();
1891
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]);
1906 }
1907 _symbolTable.removeDeadUndefs(_atoms, mustPreserve);
1908 }
1909 }
1910
1911
1912 void Resolver::tweakWeakness()
1913 {
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 )
1921 continue;
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);
1927 }
1928 else if ( atom->combine() == ld::Atom::combineByName ) {
1929 if ( _options.forceNotWeak(name) )
1930 (const_cast<ld::Atom*>(atom))->setCombine(ld::Atom::combineNever);
1931 }
1932 }
1933 else {
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);
1938 }
1939 }
1940 }
1941 }
1942 }
1943
1944 void Resolver::buildArchivesList()
1945 {
1946 // Determine which archives were linked and update the internal state.
1947 _inputFiles.archives(_internal);
1948 }
1949
1950 void Resolver::dumpAtoms()
1951 {
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());
1956 }
1957 }
1958
1959 void Resolver::resolve()
1960 {
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();
1977 }
1978
1979
1980
1981 } // namespace tool
1982 } // namespace ld
1983
1984
1985