2 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
4 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
6 * @APPLE_LICENSE_HEADER_START@
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
28 #include <sys/types.h>
31 #include <sys/sysctl.h>
36 #include <mach/mach_time.h>
37 #include <mach/vm_statistics.h>
38 #include <mach/mach_init.h>
39 #include <mach/mach_host.h>
41 #include <mach-o/dyld.h>
42 #include <mach-o/fat.h>
43 #include <sys/sysctl.h>
44 #include <libkern/OSAtomic.h>
54 #include <AvailabilityMacros.h>
58 #include "InputFiles.h"
59 #include "macho_relocatable_file.h"
60 #include "macho_dylib_file.h"
61 #include "archive_file.h"
63 #include "opaque_section_file.h"
64 #include "MachOFileAbstraction.hpp"
67 const bool _s_logPThreads
= false;
72 class IgnoredFile
: public ld::File
{
74 IgnoredFile(const char* pth
, time_t modTime
, Ordinal ord
, Type type
) : ld::File(pth
, modTime
, ord
, type
) {};
75 virtual bool forEachAtom(AtomHandler
&) const { return false; };
76 virtual bool justInTimeforEachAtom(const char* name
, AtomHandler
&) const { return false; };
80 class DSOHandleAtom
: public ld::Atom
{
82 DSOHandleAtom(const char* nm
, ld::Atom::Scope sc
,
83 ld::Atom::SymbolTableInclusion inc
, ld::Section
& sect
=_s_section
)
84 : ld::Atom(sect
, ld::Atom::definitionRegular
,
85 (sect
== _s_section_text
) ? ld::Atom::combineByName
: ld::Atom::combineNever
,
86 // make "weak def" so that link succeeds even if app defines __dso_handle
87 sc
, ld::Atom::typeUnclassified
, inc
, true, false, false,
88 ld::Atom::Alignment(1)), _name(nm
) {}
90 virtual ld::File
* file() const { return NULL
; }
91 virtual const char* name() const { return _name
; }
92 virtual uint64_t size() const { return 0; }
93 virtual uint64_t objectAddress() const { return 0; }
94 virtual void copyRawContent(uint8_t buffer
[]) const
96 virtual void setScope(Scope
) { }
98 virtual ~DSOHandleAtom() {}
100 static ld::Section _s_section
;
101 static ld::Section _s_section_preload
;
102 static ld::Section _s_section_text
;
103 static DSOHandleAtom _s_atomAll
;
104 static DSOHandleAtom _s_atomExecutable
;
105 static DSOHandleAtom _s_atomDylib
;
106 static DSOHandleAtom _s_atomBundle
;
107 static DSOHandleAtom _s_atomDyld
;
108 static DSOHandleAtom _s_atomObjectFile
;
109 static DSOHandleAtom _s_atomPreload
;
110 static DSOHandleAtom _s_atomPreloadDSO
;
114 ld::Section
DSOHandleAtom::_s_section("__TEXT", "__mach_header", ld::Section::typeMachHeader
, true);
115 ld::Section
DSOHandleAtom::_s_section_preload("__HEADER", "__mach_header", ld::Section::typeMachHeader
, true);
116 ld::Section
DSOHandleAtom::_s_section_text("__TEXT", "__text", ld::Section::typeCode
, false);
117 DSOHandleAtom
DSOHandleAtom::_s_atomAll("___dso_handle", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
);
118 DSOHandleAtom
DSOHandleAtom::_s_atomExecutable("__mh_execute_header", ld::Atom::scopeGlobal
, ld::Atom::symbolTableInAndNeverStrip
);
119 DSOHandleAtom
DSOHandleAtom::_s_atomDylib("__mh_dylib_header", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
);
120 DSOHandleAtom
DSOHandleAtom::_s_atomBundle("__mh_bundle_header", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
);
121 DSOHandleAtom
DSOHandleAtom::_s_atomDyld("__mh_dylinker_header", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
);
122 DSOHandleAtom
DSOHandleAtom::_s_atomObjectFile("__mh_object_header", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
);
123 DSOHandleAtom
DSOHandleAtom::_s_atomPreload("__mh_preload_header", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
, _s_section_preload
);
124 DSOHandleAtom
DSOHandleAtom::_s_atomPreloadDSO("___dso_handle", ld::Atom::scopeLinkageUnit
, ld::Atom::symbolTableNotIn
, _s_section_text
);
128 class PageZeroAtom
: public ld::Atom
{
130 PageZeroAtom(uint64_t sz
)
131 : ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
132 ld::Atom::scopeTranslationUnit
, ld::Atom::typeZeroFill
,
133 symbolTableNotIn
, true, false, false, ld::Atom::Alignment(12)),
136 virtual ld::File
* file() const { return NULL
; }
137 virtual const char* name() const { return "page zero"; }
138 virtual uint64_t size() const { return _size
; }
139 virtual uint64_t objectAddress() const { return 0; }
140 virtual void copyRawContent(uint8_t buffer
[]) const
142 virtual void setScope(Scope
) { }
144 virtual ~PageZeroAtom() {}
146 static ld::Section _s_section
;
147 static DSOHandleAtom _s_atomAll
;
151 ld::Section
PageZeroAtom::_s_section("__PAGEZERO", "__pagezero", ld::Section::typePageZero
, true);
154 class CustomStackAtom
: public ld::Atom
{
156 CustomStackAtom(uint64_t sz
)
157 : ld::Atom(_s_section
, ld::Atom::definitionRegular
, ld::Atom::combineNever
,
158 ld::Atom::scopeTranslationUnit
, ld::Atom::typeZeroFill
,
159 symbolTableNotIn
, false, false, false, ld::Atom::Alignment(12)),
162 virtual ld::File
* file() const { return NULL
; }
163 virtual const char* name() const { return "custom stack"; }
164 virtual uint64_t size() const { return _size
; }
165 virtual uint64_t objectAddress() const { return 0; }
166 virtual void copyRawContent(uint8_t buffer
[]) const
168 virtual void setScope(Scope
) { }
170 virtual ~CustomStackAtom() {}
174 static ld::Section _s_section
;
176 ld::Section
CustomStackAtom::_s_section("__UNIXSTACK", "__stack", ld::Section::typeStack
, true);
180 const char* InputFiles::fileArch(const uint8_t* p
, unsigned len
)
182 const char* result
= mach_o::relocatable::archName(p
);
183 if ( result
!= NULL
)
186 result
= mach_o::dylib::archName(p
);
187 if ( result
!= NULL
)
190 result
= lto::archName(p
, len
);
191 if ( result
!= NULL
)
194 if ( strncmp((const char*)p
, "!<arch>\n", 8) == 0 )
197 char *unsupported
= (char *)malloc(128);
198 strcpy(unsupported
, "unsupported file format (");
199 for (unsigned i
=0; i
<len
&& i
< 16; i
++) {
201 sprintf(buf
, " 0x%02X", p
[i
]);
202 strcat(unsupported
, buf
);
204 strcat(unsupported
, " )");
209 ld::File
* InputFiles::makeFile(const Options::FileInfo
& info
, bool indirectDylib
)
212 uint64_t len
= info
.fileLen
;
213 int fd
= ::open(info
.path
, O_RDONLY
, 0);
215 throwf("can't open file, errno=%d", errno
);
216 if ( info
.fileLen
< 20 )
217 throwf("file too small (length=%llu)", info
.fileLen
);
219 uint8_t* p
= (uint8_t*)::mmap(NULL
, info
.fileLen
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, fd
, 0);
220 if ( p
== (uint8_t*)(-1) )
221 throwf("can't map file, errno=%d", errno
);
223 // if fat file, skip to architecture we want
224 // Note: fat header is always big-endian
225 bool isFatFile
= false;
226 uint32_t sliceToUse
, sliceCount
;
227 const fat_header
* fh
= (fat_header
*)p
;
228 if ( fh
->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
230 const struct fat_arch
* archs
= (struct fat_arch
*)(p
+ sizeof(struct fat_header
));
231 bool sliceFound
= false;
232 sliceCount
= OSSwapBigToHostInt32(fh
->nfat_arch
);
233 if ( _options
.preferSubArchitecture() ) {
234 // first try to find a slice that match cpu-type and cpu-sub-type
235 for (uint32_t i
=0; i
< sliceCount
; ++i
) {
236 if ( (OSSwapBigToHostInt32(archs
[i
].cputype
) == (uint32_t)_options
.architecture())
237 && (OSSwapBigToHostInt32(archs
[i
].cpusubtype
) == (uint32_t)_options
.subArchitecture()) ) {
245 // look for any slice that matches just cpu-type
246 for (uint32_t i
=0; i
< sliceCount
; ++i
) {
247 if ( OSSwapBigToHostInt32(archs
[i
].cputype
) == (uint32_t)_options
.architecture() ) {
255 uint32_t fileOffset
= OSSwapBigToHostInt32(archs
[sliceToUse
].offset
);
256 len
= OSSwapBigToHostInt32(archs
[sliceToUse
].size
);
257 if ( fileOffset
+len
> info
.fileLen
) {
258 // <rdar://problem/17593430> file size was read awhile ago. If file is being written, wait a second to see if big enough now
260 uint64_t newFileLen
= info
.fileLen
;
261 struct stat statBuffer
;
262 if ( stat(info
.path
, &statBuffer
) == 0 ) {
263 newFileLen
= statBuffer
.st_size
;
265 if ( fileOffset
+len
> newFileLen
) {
266 throwf("truncated fat file. Slice from %u to %llu is past end of file with length %llu",
267 fileOffset
, fileOffset
+len
, info
.fileLen
);
270 // if requested architecture is page aligned within fat file, then remap just that portion of file
271 if ( (fileOffset
& 0x00000FFF) == 0 ) {
273 munmap((caddr_t
)p
, info
.fileLen
);
274 // re-map just part we need
275 p
= (uint8_t*)::mmap(NULL
, len
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, fd
, fileOffset
);
276 if ( p
== (uint8_t*)(-1) )
277 throwf("can't re-map file, errno=%d", errno
);
286 // see if it is an object file
287 mach_o::relocatable::ParserOptions objOpts
;
288 objOpts
.architecture
= _options
.architecture();
289 objOpts
.objSubtypeMustMatch
= !_options
.allowSubArchitectureMismatches();
290 objOpts
.logAllFiles
= _options
.logAllFiles();
291 objOpts
.warnUnwindConversionProblems
= _options
.needsUnwindInfoSection();
292 objOpts
.keepDwarfUnwind
= _options
.keepDwarfUnwind();
293 objOpts
.forceDwarfConversion
= (_options
.outputKind() == Options::kDyld
);
294 objOpts
.neverConvertDwarf
= !_options
.needsUnwindInfoSection();
295 objOpts
.verboseOptimizationHints
= _options
.verboseOptimizationHints();
296 objOpts
.armUsesZeroCostExceptions
= _options
.armUsesZeroCostExceptions();
297 objOpts
.subType
= _options
.subArchitecture();
298 ld::relocatable::File
* objResult
= mach_o::relocatable::parse(p
, len
, info
.path
, info
.modTime
, info
.ordinal
, objOpts
);
299 if ( objResult
!= NULL
) {
300 OSAtomicAdd64(len
, &_totalObjectSize
);
301 OSAtomicIncrement32(&_totalObjectLoaded
);
305 // see if it is an llvm object file
306 objResult
= lto::parse(p
, len
, info
.path
, info
.modTime
, info
.ordinal
, _options
.architecture(), _options
.subArchitecture(), _options
.logAllFiles(), _options
.verboseOptimizationHints());
307 if ( objResult
!= NULL
) {
308 OSAtomicAdd64(len
, &_totalObjectSize
);
309 OSAtomicIncrement32(&_totalObjectLoaded
);
313 // see if it is a dynamic library
314 ld::dylib::File
* dylibResult
;
315 bool dylibsNotAllowed
= false;
316 switch ( _options
.outputKind() ) {
317 case Options::kDynamicExecutable
:
318 case Options::kDynamicLibrary
:
319 case Options::kDynamicBundle
:
320 dylibResult
= mach_o::dylib::parse(p
, len
, info
.path
, info
.modTime
, _options
, info
.ordinal
, info
.options
.fBundleLoader
, indirectDylib
);
321 if ( dylibResult
!= NULL
) {
325 case Options::kStaticExecutable
:
327 case Options::kPreload
:
328 case Options::kObjectFile
:
329 case Options::kKextBundle
:
330 dylibsNotAllowed
= true;
335 // see if it is a static library
336 ::archive::ParserOptions archOpts
;
337 archOpts
.objOpts
= objOpts
;
338 archOpts
.forceLoadThisArchive
= info
.options
.fForceLoad
;
339 archOpts
.forceLoadAll
= _options
.fullyLoadArchives();
340 archOpts
.forceLoadObjC
= _options
.loadAllObjcObjectsFromArchives();
341 archOpts
.objcABI2
= _options
.objCABIVersion2POverride();
342 archOpts
.verboseLoad
= _options
.whyLoad();
343 archOpts
.logAllFiles
= _options
.logAllFiles();
344 ld::archive::File
* archiveResult
= ::archive::parse(p
, len
, info
.path
, info
.modTime
, info
.ordinal
, archOpts
);
345 if ( archiveResult
!= NULL
) {
346 OSAtomicAdd64(len
, &_totalArchiveSize
);
347 OSAtomicIncrement32(&_totalArchivesLoaded
);
348 return archiveResult
;
351 // does not seem to be any valid linker input file, check LTO misconfiguration problems
352 if ( lto::archName((uint8_t*)p
, len
) != NULL
) {
353 if ( lto::libLTOisLoaded() ) {
354 throwf("lto file was built for %s which is not the architecture being linked (%s): %s", fileArch(p
, len
), _options
.architectureName(), info
.path
);
357 const char* libLTO
= "libLTO.dylib";
358 char ldPath
[PATH_MAX
];
359 char tmpPath
[PATH_MAX
];
360 char libLTOPath
[PATH_MAX
];
361 uint32_t bufSize
= PATH_MAX
;
362 if ( _options
.overridePathlibLTO() != NULL
) {
363 libLTO
= _options
.overridePathlibLTO();
365 else if ( _NSGetExecutablePath(ldPath
, &bufSize
) != -1 ) {
366 if ( realpath(ldPath
, tmpPath
) != NULL
) {
367 char* lastSlash
= strrchr(tmpPath
, '/');
368 if ( lastSlash
!= NULL
)
369 strcpy(lastSlash
, "/../lib/libLTO.dylib");
371 if ( realpath(tmpPath
, libLTOPath
) != NULL
)
375 throwf("could not process llvm bitcode object file, because %s could not be loaded", libLTO
);
379 if ( dylibsNotAllowed
) {
382 if ( mach_o::dylib::isDylibFile(p
, &dummy1
, &dummy2
) )
383 throw "ignoring unexpected dylib file";
387 if ( ((fat_header
*)p
)->magic
== OSSwapBigToHostInt32(FAT_MAGIC
) ) {
388 throwf("missing required architecture %s in file %s (%u slices)", _options
.architectureName(), info
.path
, sliceCount
);
392 throwf("file is universal (%u slices) but does not contain a(n) %s slice: %s", sliceCount
, _options
.architectureName(), info
.path
);
394 throwf("file was built for %s which is not the architecture being linked (%s): %s", fileArch(p
, len
), _options
.architectureName(), info
.path
);
398 void InputFiles::logDylib(ld::File
* file
, bool indirect
)
400 if ( _options
.traceDylibs() ) {
401 const char* fullPath
= file
->path();
402 char realName
[MAXPATHLEN
];
403 if ( realpath(fullPath
, realName
) != NULL
)
405 const ld::dylib::File
* dylib
= dynamic_cast<const ld::dylib::File
*>(file
);
406 if ( (dylib
!= NULL
) && dylib
->willBeUpwardDylib() ) {
407 // don't log upward dylibs when XBS is computing dependencies
408 logTraceInfo("[Logging for XBS] Used upward dynamic library: %s\n", fullPath
);
412 logTraceInfo("[Logging for XBS] Used indirect dynamic library: %s\n", fullPath
);
414 logTraceInfo("[Logging for XBS] Used dynamic library: %s\n", fullPath
);
418 if ( _options
.dumpDependencyInfo() ) {
419 const ld::dylib::File
* dylib
= dynamic_cast<const ld::dylib::File
*>(file
);
420 if ( file
== _bundleLoader
) {
421 _options
.dumpDependency(Options::depBundleLoader
, file
->path());
423 else if ( (dylib
!= NULL
) && dylib
->willBeUpwardDylib() ) {
425 _options
.dumpDependency(Options::depUpwardIndirectDylib
, file
->path());
427 _options
.dumpDependency(Options::depUpwardDirectDylib
, file
->path());
431 _options
.dumpDependency(Options::depIndirectDylib
, file
->path());
433 _options
.dumpDependency(Options::depDirectDylib
, file
->path());
438 void InputFiles::logArchive(ld::File
* file
) const
440 if ( _options
.traceArchives() && (_archiveFilesLogged
.count(file
) == 0) ) {
441 // <rdar://problem/4947347> LD_TRACE_ARCHIVES should only print out when a .o is actually used from an archive
442 _archiveFilesLogged
.insert(file
);
443 const char* fullPath
= file
->path();
444 char realName
[MAXPATHLEN
];
445 if ( realpath(fullPath
, realName
) != NULL
)
447 logTraceInfo("[Logging for XBS] Used static archive: %s\n", fullPath
);
452 void InputFiles::logTraceInfo(const char* format
, ...) const
454 // one time open() of custom LD_TRACE_FILE
455 static int trace_file
= -1;
456 if ( trace_file
== -1 ) {
457 const char *trace_file_path
= _options
.traceOutputFile();
458 if ( trace_file_path
!= NULL
) {
459 trace_file
= open(trace_file_path
, O_WRONLY
| O_APPEND
| O_CREAT
, 0666);
460 if ( trace_file
== -1 )
461 throwf("Could not open or create trace file (errno=%d): %s", errno
, trace_file_path
);
464 trace_file
= fileno(stderr
);
468 char trace_buffer
[MAXPATHLEN
* 2];
470 va_start(ap
, format
);
471 int length
= vsnprintf(trace_buffer
, sizeof(trace_buffer
), format
, ap
);
473 char* buffer_ptr
= trace_buffer
;
476 ssize_t amount_written
= write(trace_file
, buffer_ptr
, length
);
477 if(amount_written
== -1)
478 /* Failure to write shouldn't fail the build. */
480 buffer_ptr
+= amount_written
;
481 length
-= amount_written
;
486 ld::dylib::File
* InputFiles::findDylib(const char* installPath
, const char* fromPath
)
488 //fprintf(stderr, "findDylib(%s, %s)\n", installPath, fromPath);
489 InstallNameToDylib::iterator pos
= _installPathToDylibs
.find(installPath
);
490 if ( pos
!= _installPathToDylibs
.end() ) {
494 // allow -dylib_path option to override indirect library to use
495 for (std::vector
<Options::DylibOverride
>::const_iterator dit
= _options
.dylibOverrides().begin(); dit
!= _options
.dylibOverrides().end(); ++dit
) {
496 if ( strcmp(dit
->installName
,installPath
) == 0 ) {
498 Options::FileInfo info
= _options
.findFile(dit
->useInstead
);
499 _indirectDylibOrdinal
= _indirectDylibOrdinal
.nextIndirectDylibOrdinal();
500 info
.ordinal
= _indirectDylibOrdinal
;
501 info
.options
.fIndirectDylib
= true;
502 ld::File
* reader
= this->makeFile(info
, true);
503 ld::dylib::File
* dylibReader
= dynamic_cast<ld::dylib::File
*>(reader
);
504 if ( dylibReader
!= NULL
) {
505 addDylib(dylibReader
, info
);
506 //_installPathToDylibs[strdup(installPath)] = dylibReader;
507 this->logDylib(dylibReader
, true);
511 throwf("indirect dylib at %s is not a dylib", dit
->useInstead
);
513 catch (const char* msg
) {
514 warning("ignoring -dylib_file option, %s", msg
);
518 char newPath
[MAXPATHLEN
];
519 // handle @loader_path
520 if ( strncmp(installPath
, "@loader_path/", 13) == 0 ) {
521 strcpy(newPath
, fromPath
);
522 char* addPoint
= strrchr(newPath
,'/');
523 if ( addPoint
!= NULL
)
524 strcpy(&addPoint
[1], &installPath
[13]);
526 strcpy(newPath
, &installPath
[13]);
527 installPath
= newPath
;
529 // note: @executable_path case is handled inside findFileUsingPaths()
530 // search for dylib using -F and -L paths
531 Options::FileInfo info
= _options
.findFileUsingPaths(installPath
);
532 _indirectDylibOrdinal
= _indirectDylibOrdinal
.nextIndirectDylibOrdinal();
533 info
.ordinal
= _indirectDylibOrdinal
;
534 info
.options
.fIndirectDylib
= true;
536 ld::File
* reader
= this->makeFile(info
, true);
537 ld::dylib::File
* dylibReader
= dynamic_cast<ld::dylib::File
*>(reader
);
538 if ( dylibReader
!= NULL
) {
539 //assert(_installPathToDylibs.find(installPath) != _installPathToDylibs.end());
540 //_installPathToDylibs[strdup(installPath)] = dylibReader;
541 addDylib(dylibReader
, info
);
542 this->logDylib(dylibReader
, true);
546 throwf("indirect dylib at %s is not a dylib", info
.path
);
548 catch (const char* msg
) {
549 throwf("in '%s', %s", info
.path
, msg
);
555 // mark all dylibs initially specified as required, and check if they can be used
556 void InputFiles::markExplicitlyLinkedDylibs()
558 for (InstallNameToDylib::iterator it
=_installPathToDylibs
.begin(); it
!= _installPathToDylibs
.end(); it
++) {
559 it
->second
->setExplicitlyLinked();
560 this->checkDylibClientRestrictions(it
->second
);
564 bool InputFiles::libraryAlreadyLoaded(const char* path
)
566 for (std::vector
<ld::File
*>::const_iterator it
= _inputFiles
.begin(); it
!= _inputFiles
.end(); ++it
) {
567 if ( strcmp(path
, (*it
)->path()) == 0 )
574 void InputFiles::addLinkerOptionLibraries(ld::Internal
& state
, ld::File::AtomHandler
& handler
)
576 if ( _options
.outputKind() == Options::kObjectFile
)
579 // process frameworks specified in .o linker options
580 for (CStringSet::const_iterator it
= state
.linkerOptionFrameworks
.begin(); it
!= state
.linkerOptionFrameworks
.end(); ++it
) {
581 const char* frameworkName
= *it
;
582 Options::FileInfo info
= _options
.findFramework(frameworkName
);
583 if ( ! this->libraryAlreadyLoaded(info
.path
) ) {
584 info
.ordinal
= _linkerOptionOrdinal
.nextLinkerOptionOrdinal();
586 ld::File
* reader
= this->makeFile(info
, true);
587 ld::dylib::File
* dylibReader
= dynamic_cast<ld::dylib::File
*>(reader
);
588 if ( dylibReader
!= NULL
) {
589 if ( ! dylibReader
->installPathVersionSpecific() ) {
590 dylibReader
->setImplicitlyLinked();
591 this->addDylib(dylibReader
, info
);
595 throwf("framework linker option at %s is not a dylib", info
.path
);
598 catch (const char* msg
) {
599 warning("Auto-Linking supplied '%s', %s", info
.path
, msg
);
603 // process libraries specified in .o linker options
604 for (CStringSet::const_iterator it
= state
.linkerOptionLibraries
.begin(); it
!= state
.linkerOptionLibraries
.end(); ++it
) {
605 const char* libName
= *it
;
606 Options::FileInfo info
= _options
.findLibrary(libName
);
607 if ( ! this->libraryAlreadyLoaded(info
.path
) ) {
608 info
.ordinal
= _linkerOptionOrdinal
.nextLinkerOptionOrdinal();
610 //<rdar://problem/17787306> -force_load_swift_libs
611 info
.options
.fForceLoad
= _options
.forceLoadSwiftLibs() && (strncmp(libName
, "swift", 5) == 0);
612 ld::File
* reader
= this->makeFile(info
, true);
613 ld::dylib::File
* dylibReader
= dynamic_cast<ld::dylib::File
*>(reader
);
614 ld::archive::File
* archiveReader
= dynamic_cast<ld::archive::File
*>(reader
);
615 if ( dylibReader
!= NULL
) {
616 dylibReader
->setImplicitlyLinked();
617 this->addDylib(dylibReader
, info
);
619 else if ( archiveReader
!= NULL
) {
620 _searchLibraries
.push_back(LibraryInfo(archiveReader
));
621 if ( _options
.dumpDependencyInfo() )
622 _options
.dumpDependency(Options::depArchive
, archiveReader
->path());
623 //<rdar://problem/17787306> -force_load_swift_libs
624 if (info
.options
.fForceLoad
) {
625 archiveReader
->forEachAtom(handler
);
629 throwf("linker option dylib at %s is not a dylib", info
.path
);
632 catch (const char* msg
) {
633 warning("Auto-Linking supplied '%s', %s", info
.path
, msg
);
639 void InputFiles::createIndirectDylibs()
641 // keep processing dylibs until no more dylibs are added
642 unsigned long lastMapSize
= 0;
643 std::set
<ld::dylib::File
*> dylibsProcessed
;
644 while ( lastMapSize
!= _allDylibs
.size() ) {
645 lastMapSize
= _allDylibs
.size();
646 // can't iterator _installPathToDylibs while modifying it, so use temp buffer
647 std::vector
<ld::dylib::File
*> unprocessedDylibs
;
648 for (std::set
<ld::dylib::File
*>::iterator it
=_allDylibs
.begin(); it
!= _allDylibs
.end(); it
++) {
649 if ( dylibsProcessed
.count(*it
) == 0 )
650 unprocessedDylibs
.push_back(*it
);
652 for (std::vector
<ld::dylib::File
*>::iterator it
=unprocessedDylibs
.begin(); it
!= unprocessedDylibs
.end(); it
++) {
653 dylibsProcessed
.insert(*it
);
654 (*it
)->processIndirectLibraries(this, _options
.implicitlyLinkIndirectPublicDylibs());
658 // go back over original dylibs and mark sub frameworks as re-exported
659 if ( _options
.outputKind() == Options::kDynamicLibrary
) {
660 const char* myLeaf
= strrchr(_options
.installPath(), '/');
661 if ( myLeaf
!= NULL
) {
662 for (std::vector
<class ld::File
*>::const_iterator it
=_inputFiles
.begin(); it
!= _inputFiles
.end(); it
++) {
663 ld::dylib::File
* dylibReader
= dynamic_cast<ld::dylib::File
*>(*it
);
664 if ( dylibReader
!= NULL
) {
665 const char* childParent
= dylibReader
->parentUmbrella();
666 if ( childParent
!= NULL
) {
667 if ( strcmp(childParent
, &myLeaf
[1]) == 0 ) {
668 // mark that this dylib will be re-exported
669 dylibReader
->setWillBeReExported();
679 void InputFiles::createOpaqueFileSections()
681 // extra command line sections always at end
682 for (Options::ExtraSection::const_iterator it
=_options
.extraSectionsBegin(); it
!= _options
.extraSectionsEnd(); ++it
) {
683 _inputFiles
.push_back(opaque_section::parse(it
->segmentName
, it
->sectionName
, it
->path
, it
->data
, it
->dataLen
));
684 if ( _options
.dumpDependencyInfo() )
685 _options
.dumpDependency(Options::depSection
, it
->path
);
691 void InputFiles::checkDylibClientRestrictions(ld::dylib::File
* dylib
)
693 // Check for any restrictions on who can link with this dylib
694 const char* dylibParentName
= dylib
->parentUmbrella() ;
695 const std::vector
<const char*>* clients
= dylib
->allowableClients();
696 if ( (dylibParentName
!= NULL
) || (clients
!= NULL
) ) {
697 // only dylibs that are in an umbrella or have a client list need verification
698 const char* installName
= _options
.installPath();
699 const char* installNameLastSlash
= strrchr(installName
, '/');
700 bool isParent
= false;
701 bool isSibling
= false;
702 bool isAllowableClient
= false;
703 // There are three cases:
704 if ( (dylibParentName
!= NULL
) && (installNameLastSlash
!= NULL
) ) {
705 // starts after last slash
706 const char* myName
= &installNameLastSlash
[1];
707 unsigned int myNameLen
= strlen(myName
);
708 if ( strncmp(myName
, "lib", 3) == 0 )
711 const char* firstDot
= strchr(myName
, '.');
712 if ( firstDot
!= NULL
)
713 myNameLen
= firstDot
- myName
;
714 // up to first underscore
715 const char* firstUnderscore
= strchr(myName
, '_');
716 if ( (firstUnderscore
!= NULL
) && ((firstUnderscore
- myName
) < (int)myNameLen
) )
717 myNameLen
= firstUnderscore
- myName
;
719 // case 1) The dylib has a parent umbrella, and we are creating the parent umbrella
720 isParent
= ( (strlen(dylibParentName
) == myNameLen
) && (strncmp(myName
, dylibParentName
, myNameLen
) == 0) );
722 // case 2) The dylib has a parent umbrella, and we are creating a sibling with the same parent
723 isSibling
= ( (_options
.umbrellaName() != NULL
) && (strcmp(_options
.umbrellaName(), dylibParentName
) == 0) );
726 if ( !isParent
&& !isSibling
&& (clients
!= NULL
) ) {
727 // case 3) the dylib has a list of allowable clients, and we are creating one of them
728 const char* clientName
= _options
.clientName();
729 int clientNameLen
= 0;
730 if ( clientName
!= NULL
) {
731 // use client name as specified on command line
732 clientNameLen
= strlen(clientName
);
735 // infer client name from output path (e.g. xxx/libfoo_variant.A.dylib --> foo, Bar.framework/Bar_variant --> Bar)
736 clientName
= installName
;
737 clientNameLen
= strlen(clientName
);
738 // starts after last slash
739 if ( installNameLastSlash
!= NULL
)
740 clientName
= &installNameLastSlash
[1];
741 if ( strncmp(clientName
, "lib", 3) == 0 )
742 clientName
= &clientName
[3];
744 const char* firstDot
= strchr(clientName
, '.');
745 if ( firstDot
!= NULL
)
746 clientNameLen
= firstDot
- clientName
;
747 // up to first underscore
748 const char* firstUnderscore
= strchr(clientName
, '_');
749 if ( (firstUnderscore
!= NULL
) && ((firstUnderscore
- clientName
) < clientNameLen
) )
750 clientNameLen
= firstUnderscore
- clientName
;
753 // Use clientName to check if this dylib is able to link against the allowable clients.
754 for (std::vector
<const char*>::const_iterator it
= clients
->begin(); it
!= clients
->end(); it
++) {
755 if ( strncmp(*it
, clientName
, clientNameLen
) == 0 )
756 isAllowableClient
= true;
760 if ( !isParent
&& !isSibling
&& !isAllowableClient
) {
761 if ( dylibParentName
!= NULL
) {
762 throwf("cannot link directly with %s. Link against the umbrella framework '%s.framework' instead.",
763 dylib
->path(), dylibParentName
);
766 throwf("cannot link directly with %s", dylib
->path());
773 void InputFiles::inferArchitecture(Options
& opts
, const char** archName
)
775 _inferredArch
= true;
776 // scan all input files, looking for a thin .o file.
777 // the first one found is presumably the architecture to link
778 uint8_t buffer
[sizeof(mach_header_64
)];
779 const std::vector
<Options::FileInfo
>& files
= opts
.getInputFiles();
780 for (std::vector
<Options::FileInfo
>::const_iterator it
= files
.begin(); it
!= files
.end(); ++it
) {
781 int fd
= ::open(it
->path
, O_RDONLY
, 0);
783 ssize_t amount
= read(fd
, buffer
, sizeof(buffer
));
785 if ( amount
>= (ssize_t
)sizeof(buffer
) ) {
787 cpu_subtype_t subtype
;
788 if ( mach_o::relocatable::isObjectFile(buffer
, &type
, &subtype
) ) {
789 opts
.setArchitecture(type
, subtype
);
790 *archName
= opts
.architectureName();
797 // no thin .o files found, so default to same architecture this tool was built as
798 warning("-arch not specified");
800 opts
.setArchitecture(CPU_TYPE_I386
, CPU_SUBTYPE_X86_ALL
);
802 opts
.setArchitecture(CPU_TYPE_X86_64
, CPU_SUBTYPE_X86_64_ALL
);
804 opts
.setArchitecture(CPU_TYPE_ARM
, CPU_SUBTYPE_ARM_V6
);
806 #error unknown default architecture
808 *archName
= opts
.architectureName();
812 InputFiles::InputFiles(Options
& opts
, const char** archName
)
813 : _totalObjectSize(0), _totalArchiveSize(0),
814 _totalObjectLoaded(0), _totalArchivesLoaded(0), _totalDylibsLoaded(0),
815 _options(opts
), _bundleLoader(NULL
),
816 _inferredArch(false),
818 _indirectDylibOrdinal(ld::File::Ordinal::indirectDylibBase()),
819 _linkerOptionOrdinal(ld::File::Ordinal::linkeOptionBase())
821 // fStartCreateReadersTime = mach_absolute_time();
822 if ( opts
.architecture() == 0 ) {
823 // command line missing -arch, so guess arch
824 inferArchitecture(opts
, archName
);
827 pthread_mutex_init(&_parseLock
, NULL
);
828 pthread_cond_init(&_parseWorkReady
, NULL
);
829 pthread_cond_init(&_newFileAvailable
, NULL
);
831 const std::vector
<Options::FileInfo
>& files
= _options
.getInputFiles();
832 if ( files
.size() == 0 )
833 throw "no object files specified";
835 _inputFiles
.reserve(files
.size());
837 unsigned int inputFileSlot
= 0;
838 _availableInputFiles
= 0;
841 Options::FileInfo
* entry
;
842 for (std::vector
<Options::FileInfo
>::const_iterator it
= files
.begin(); it
!= files
.end(); ++it
) {
843 entry
= (Options::FileInfo
*)&(*it
);
845 // Assign input file slots to all the FileInfos.
846 // Also chain all FileInfos into one big list to set up for worker threads to do parsing.
847 entry
->inputFileSlot
= inputFileSlot
;
848 entry
->readyToParse
= !entry
->fromFileList
|| !_options
.pipelineEnabled();
849 if (entry
->readyToParse
)
850 _availableInputFiles
++;
851 _inputFiles
.push_back(NULL
);
854 // In the non-threaded case just parse the file now.
855 _inputFiles
.push_back(makeFile(*entry
, false));
860 _remainingInputFiles
= files
.size();
862 // initialize info for parsing input files on worker threads
865 size_t len
= sizeof(ncpus
);
868 if (sysctl(mib
, 2, &ncpus
, &len
, NULL
, 0) != 0) {
871 _availableWorkers
= MIN(ncpus
, files
.size()); // max # workers we permit
874 if (_options
.pipelineEnabled()) {
875 // start up a thread to listen for available input files
876 startThread(InputFiles::waitForInputFiles
);
879 // Start up one parser thread. More start on demand as parsed input files get consumed.
880 startThread(InputFiles::parseWorkerThread
);
883 if (_options
.pipelineEnabled()) {
884 throwf("pipelined linking not supported on this platform");
891 void InputFiles::startThread(void (*threadFunc
)(InputFiles
*)) const {
894 pthread_attr_init(&attr
);
895 // set a nice big stack (same as main thread) because some code uses potentially large stack buffers
896 pthread_attr_setstacksize(&attr
, 8 * 1024 * 1024);
897 pthread_create(&thread
, &attr
, (void *(*)(void*))threadFunc
, (void *)this);
898 pthread_detach(thread
);
899 pthread_attr_destroy(&attr
);
902 // Work loop for input file parsing threads
903 void InputFiles::parseWorkerThread() {
905 const char *exception
= NULL
;
906 pthread_mutex_lock(&_parseLock
);
907 const std::vector
<Options::FileInfo
>& files
= _options
.getInputFiles();
908 if (_s_logPThreads
) printf("worker starting\n");
910 if (_availableInputFiles
== 0) {
912 pthread_cond_wait(&_parseWorkReady
, &_parseLock
);
915 int slot
= _parseCursor
;
916 while (slot
< (int)files
.size() && (_inputFiles
[slot
] != NULL
|| !files
[slot
].readyToParse
))
918 assert(slot
< (int)files
.size());
919 Options::FileInfo
& entry
= (Options::FileInfo
&)files
[slot
];
920 _parseCursor
= slot
+1;
921 _availableInputFiles
--;
922 entry
.readyToParse
= false; // to avoid multiple threads finding this file
923 pthread_mutex_unlock(&_parseLock
);
924 if (_s_logPThreads
) printf("parsing index %u\n", slot
);
926 file
= makeFile(entry
, false);
928 catch (const char *msg
) {
929 if ( (strstr(msg
, "architecture") != NULL
) && !_options
.errorOnOtherArchFiles() ) {
930 if ( _options
.ignoreOtherArchInputFiles() ) {
931 // ignore, because this is about an architecture not in use
934 warning("ignoring file %s, %s", entry
.path
, msg
);
937 else if ( strstr(msg
, "ignoring unexpected") != NULL
) {
938 warning("%s, %s", entry
.path
, msg
);
941 asprintf((char**)&exception
, "%s file '%s'", msg
, entry
.path
);
943 file
= new IgnoredFile(entry
.path
, entry
.modTime
, entry
.ordinal
, ld::File::Other
);
945 pthread_mutex_lock(&_parseLock
);
946 if (_remainingInputFiles
> 0)
947 _remainingInputFiles
--;
948 if (_s_logPThreads
) printf("done with index %u, %d remaining\n", slot
, _remainingInputFiles
);
950 // We are about to die, so set to zero to stop other threads from doing unneeded work.
951 _remainingInputFiles
= 0;
952 _exception
= exception
;
955 _inputFiles
[slot
] = file
;
956 if (_neededFileSlot
== slot
)
957 pthread_cond_signal(&_newFileAvailable
);
960 } while (_remainingInputFiles
);
961 if (_s_logPThreads
) printf("worker exiting\n");
962 pthread_cond_broadcast(&_parseWorkReady
);
963 pthread_cond_signal(&_newFileAvailable
);
964 pthread_mutex_unlock(&_parseLock
);
968 void InputFiles::parseWorkerThread(InputFiles
*inputFiles
) {
969 inputFiles
->parseWorkerThread();
974 ld::File
* InputFiles::addDylib(ld::dylib::File
* reader
, const Options::FileInfo
& info
)
976 _allDylibs
.insert(reader
);
978 if ( (reader
->installPath() == NULL
) && !info
.options
.fBundleLoader
) {
979 // this is a "blank" stub
980 // silently ignore it
983 // store options about how dylib will be used in dylib itself
984 if ( info
.options
.fWeakImport
)
985 reader
->setForcedWeakLinked();
986 if ( info
.options
.fReExport
)
987 reader
->setWillBeReExported();
988 if ( info
.options
.fUpward
) {
989 if ( _options
.outputKind() == Options::kDynamicLibrary
)
990 reader
->setWillBeUpwardDylib();
992 warning("ignoring upward dylib option for %s\n", info
.path
);
994 if ( info
.options
.fLazyLoad
)
995 reader
->setWillBeLazyLoadedDylb();
997 // add to map of loaded dylibs
998 const char* installPath
= reader
->installPath();
999 if ( installPath
!= NULL
) {
1000 InstallNameToDylib::iterator pos
= _installPathToDylibs
.find(installPath
);
1001 if ( pos
== _installPathToDylibs
.end() ) {
1002 _installPathToDylibs
[strdup(installPath
)] = reader
;
1005 bool dylibOnCommandLineTwice
= ( strcmp(pos
->second
->path(), reader
->path()) == 0 );
1006 bool isSymlink
= false;
1007 // ignore if this is a symlink to a dylib we've already loaded
1008 if ( !dylibOnCommandLineTwice
) {
1009 char existingDylibPath
[PATH_MAX
];
1010 if ( realpath(pos
->second
->path(), existingDylibPath
) != NULL
) {
1011 char newDylibPath
[PATH_MAX
];
1012 if ( realpath(reader
->path(), newDylibPath
) != NULL
) {
1013 isSymlink
= ( strcmp(existingDylibPath
, newDylibPath
) == 0 );
1017 // remove warning for <rdar://problem/10860629> Same install name for CoreServices and CFNetwork?
1018 //if ( !dylibOnCommandLineTwice && !isSymlink )
1019 // warning("dylibs with same install name: %p %s and %p %s", pos->second, pos->second->path(), reader, reader->path());
1022 else if ( info
.options
.fBundleLoader
)
1023 _bundleLoader
= reader
;
1025 // log direct readers
1026 if ( ! info
.options
.fIndirectDylib
)
1027 this->logDylib(reader
, false);
1030 _totalDylibsLoaded
++;
1032 // just add direct libraries to search-first list
1033 if ( ! info
.options
.fIndirectDylib
)
1034 _searchLibraries
.push_back(LibraryInfo(reader
));
1041 // Called during pipelined linking to listen for available input files.
1042 // Available files are enqueued for parsing.
1043 void InputFiles::waitForInputFiles()
1045 if (_s_logPThreads
) printf("starting pipeline listener\n");
1047 const char *fifo
= _options
.pipelineFifo();
1049 std::map
<const char *, const Options::FileInfo
*, strcompclass
> fileMap
;
1050 const std::vector
<Options::FileInfo
>& files
= _options
.getInputFiles();
1051 for (std::vector
<Options::FileInfo
>::const_iterator it
= files
.begin(); it
!= files
.end(); ++it
) {
1052 const Options::FileInfo
& entry
= *it
;
1053 if (entry
.fromFileList
) {
1054 fileMap
[entry
.path
] = &entry
;
1057 FILE *fileStream
= fopen(fifo
, "r");
1059 throwf("pipelined linking error - failed to open stream. fopen() returns %s for \"%s\"\n", strerror(errno
), fifo
);
1060 while (fileMap
.size() > 0) {
1061 char path_buf
[PATH_MAX
+1];
1062 if (fgets(path_buf
, PATH_MAX
, fileStream
) == NULL
)
1063 throwf("pipelined linking error - %lu missing input files", fileMap
.size());
1064 int len
= strlen(path_buf
);
1065 if (path_buf
[len
-1] == '\n')
1066 path_buf
[len
-1] = 0;
1067 std::map
<const char *, const Options::FileInfo
*, strcompclass
>::iterator it
= fileMap
.find(path_buf
);
1068 if (it
== fileMap
.end())
1069 throwf("pipelined linking error - not in file list: %s\n", path_buf
);
1070 Options::FileInfo
* inputInfo
= (Options::FileInfo
*)it
->second
;
1071 if (!inputInfo
->checkFileExists(_options
))
1072 throwf("pipelined linking error - file does not exist: %s\n", inputInfo
->path
);
1073 pthread_mutex_lock(&_parseLock
);
1075 pthread_cond_signal(&_parseWorkReady
);
1076 inputInfo
->readyToParse
= true;
1077 if (_parseCursor
> inputInfo
->inputFileSlot
)
1078 _parseCursor
= inputInfo
->inputFileSlot
;
1079 _availableInputFiles
++;
1080 if (_s_logPThreads
) printf("pipeline listener: %s slot=%d, _parseCursor=%d, _availableInputFiles = %d remaining = %ld\n", path_buf
, inputInfo
->inputFileSlot
, _parseCursor
, _availableInputFiles
, fileMap
.size()-1);
1081 pthread_mutex_unlock(&_parseLock
);
1084 } catch (const char *msg
) {
1085 pthread_mutex_lock(&_parseLock
);
1087 pthread_cond_signal(&_newFileAvailable
);
1088 pthread_mutex_unlock(&_parseLock
);
1093 void InputFiles::waitForInputFiles(InputFiles
*inputFiles
) {
1094 inputFiles
->waitForInputFiles();
1099 void InputFiles::forEachInitialAtom(ld::File::AtomHandler
& handler
, ld::Internal
& state
)
1101 // add all direct object, archives, and dylibs
1102 const std::vector
<Options::FileInfo
>& files
= _options
.getInputFiles();
1104 for (fileIndex
=0; fileIndex
<_inputFiles
.size(); fileIndex
++) {
1107 pthread_mutex_lock(&_parseLock
);
1109 // this loop waits for the needed file to be ready (parsed by worker thread)
1110 while (_inputFiles
[fileIndex
] == NULL
&& _exception
== NULL
) {
1111 // We are starved for input. If there are still files to parse and we have
1112 // not maxed out the worker thread count start a new worker thread.
1113 if (_availableInputFiles
> 0 && _availableWorkers
> 0) {
1114 if (_s_logPThreads
) printf("starting worker\n");
1115 startThread(InputFiles::parseWorkerThread
);
1116 _availableWorkers
--;
1118 _neededFileSlot
= fileIndex
;
1119 if (_s_logPThreads
) printf("consumer blocking for %lu: %s\n", fileIndex
, files
[fileIndex
].path
);
1120 pthread_cond_wait(&_newFileAvailable
, &_parseLock
);
1126 // The input file is parsed. Assimilate it and call its atom iterator.
1127 if (_s_logPThreads
) printf("consuming slot %lu\n", fileIndex
);
1128 file
= _inputFiles
[fileIndex
];
1129 pthread_mutex_unlock(&_parseLock
);
1131 file
= _inputFiles
[fileIndex
];
1133 const Options::FileInfo
& info
= files
[fileIndex
];
1134 switch (file
->type()) {
1135 case ld::File::Reloc
:
1137 ld::relocatable::File
* reloc
= (ld::relocatable::File
*)file
;
1138 _options
.snapshot().recordObjectFile(reloc
->path());
1139 if ( _options
.dumpDependencyInfo() )
1140 _options
.dumpDependency(Options::depObjectFile
, reloc
->path());
1143 case ld::File::Dylib
:
1145 ld::dylib::File
* dylib
= (ld::dylib::File
*)file
;
1146 addDylib(dylib
, info
);
1149 case ld::File::Archive
:
1151 ld::archive::File
* archive
= (ld::archive::File
*)file
;
1152 // <rdar://problem/9740166> force loaded archives should be in LD_TRACE
1153 if ( (info
.options
.fForceLoad
|| _options
.fullyLoadArchives()) && _options
.traceArchives() )
1154 logArchive(archive
);
1155 _searchLibraries
.push_back(LibraryInfo(archive
));
1156 if ( _options
.dumpDependencyInfo() )
1157 _options
.dumpDependency(Options::depArchive
, archive
->path());
1160 case ld::File::Other
:
1164 throwf("Unknown file type for %s", file
->path());
1168 file
->forEachAtom(handler
);
1171 markExplicitlyLinkedDylibs();
1172 addLinkerOptionLibraries(state
, handler
);
1173 createIndirectDylibs();
1174 createOpaqueFileSections();
1176 while (fileIndex
< _inputFiles
.size()) {
1177 ld::File
*file
= _inputFiles
[fileIndex
];
1178 file
->forEachAtom(handler
);
1182 switch ( _options
.outputKind() ) {
1183 case Options::kStaticExecutable
:
1184 case Options::kDynamicExecutable
:
1185 // add implicit __dso_handle label
1186 handler
.doAtom(DSOHandleAtom::_s_atomExecutable
);
1187 handler
.doAtom(DSOHandleAtom::_s_atomAll
);
1188 if ( _options
.pageZeroSize() != 0 )
1189 handler
.doAtom(*new PageZeroAtom(_options
.pageZeroSize()));
1190 if ( _options
.hasCustomStack() && !_options
.needsEntryPointLoadCommand() )
1191 handler
.doAtom(*new CustomStackAtom(_options
.customStackSize()));
1193 case Options::kDynamicLibrary
:
1194 // add implicit __dso_handle label
1195 handler
.doAtom(DSOHandleAtom::_s_atomDylib
);
1196 handler
.doAtom(DSOHandleAtom::_s_atomAll
);
1198 case Options::kDynamicBundle
:
1199 // add implicit __dso_handle label
1200 handler
.doAtom(DSOHandleAtom::_s_atomBundle
);
1201 handler
.doAtom(DSOHandleAtom::_s_atomAll
);
1203 case Options::kDyld
:
1204 // add implicit __dso_handle label
1205 handler
.doAtom(DSOHandleAtom::_s_atomDyld
);
1206 handler
.doAtom(DSOHandleAtom::_s_atomAll
);
1208 case Options::kPreload
:
1209 // add implicit __mh_preload_header label
1210 handler
.doAtom(DSOHandleAtom::_s_atomPreload
);
1211 // add implicit __dso_handle label, but put it in __text section because
1212 // with -preload the mach_header is no in the address space.
1213 handler
.doAtom(DSOHandleAtom::_s_atomPreloadDSO
);
1215 case Options::kObjectFile
:
1216 handler
.doAtom(DSOHandleAtom::_s_atomObjectFile
);
1218 case Options::kKextBundle
:
1219 // add implicit __dso_handle label
1220 handler
.doAtom(DSOHandleAtom::_s_atomAll
);
1226 bool InputFiles::searchLibraries(const char* name
, bool searchDylibs
, bool searchArchives
, bool dataSymbolOnly
, ld::File::AtomHandler
& handler
) const
1228 // Check each input library.
1229 for (std::vector
<LibraryInfo
>::const_iterator it
=_searchLibraries
.begin(); it
!= _searchLibraries
.end(); ++it
) {
1230 LibraryInfo lib
= *it
;
1231 if (lib
.isDylib()) {
1233 ld::dylib::File
*dylibFile
= lib
.dylib();
1234 //fprintf(stderr, "searchLibraries(%s), looking in linked %s\n", name, dylibFile->path() );
1235 if ( dylibFile
->justInTimeforEachAtom(name
, handler
) ) {
1236 // we found a definition in this dylib
1237 // done, unless it is a weak definition in which case we keep searching
1238 _options
.snapshot().recordDylibSymbol(dylibFile
, name
);
1239 if ( !dylibFile
->hasWeakExternals() || !dylibFile
->hasWeakDefinition(name
)) {
1242 // else continue search for a non-weak definition
1246 if (searchArchives
) {
1247 ld::archive::File
*archiveFile
= lib
.archive();
1248 if ( dataSymbolOnly
) {
1249 if ( archiveFile
->justInTimeDataOnlyforEachAtom(name
, handler
) ) {
1250 if ( _options
.traceArchives() )
1251 logArchive(archiveFile
);
1252 _options
.snapshot().recordArchive(archiveFile
->path());
1253 // found data definition in static library, done
1258 if ( archiveFile
->justInTimeforEachAtom(name
, handler
) ) {
1259 if ( _options
.traceArchives() )
1260 logArchive(archiveFile
);
1261 _options
.snapshot().recordArchive(archiveFile
->path());
1262 // found definition in static library, done
1270 // search indirect dylibs
1271 if ( searchDylibs
) {
1272 for (InstallNameToDylib::const_iterator it
=_installPathToDylibs
.begin(); it
!= _installPathToDylibs
.end(); ++it
) {
1273 ld::dylib::File
* dylibFile
= it
->second
;
1274 bool searchThisDylib
= false;
1275 if ( _options
.nameSpace() == Options::kTwoLevelNameSpace
) {
1276 // for two level namesapce, just check all implicitly linked dylibs
1277 searchThisDylib
= dylibFile
->implicitlyLinked() && !dylibFile
->explicitlyLinked();
1280 // for flat namespace, check all indirect dylibs
1281 searchThisDylib
= ! dylibFile
->explicitlyLinked();
1283 if ( searchThisDylib
) {
1284 //fprintf(stderr, "searchLibraries(%s), looking in implicitly linked %s\n", name, dylibFile->path() );
1285 if ( dylibFile
->justInTimeforEachAtom(name
, handler
) ) {
1286 // we found a definition in this dylib
1287 // done, unless it is a weak definition in which case we keep searching
1288 _options
.snapshot().recordDylibSymbol(dylibFile
, name
);
1289 if ( !dylibFile
->hasWeakExternals() || !dylibFile
->hasWeakDefinition(name
)) {
1292 // else continue search for a non-weak definition
1302 bool InputFiles::searchWeakDefInDylib(const char* name
) const
1304 // search all relevant dylibs to see if any have a weak-def with this name
1305 for (InstallNameToDylib::const_iterator it
=_installPathToDylibs
.begin(); it
!= _installPathToDylibs
.end(); ++it
) {
1306 ld::dylib::File
* dylibFile
= it
->second
;
1307 if ( dylibFile
->implicitlyLinked() || dylibFile
->explicitlyLinked() ) {
1308 if ( dylibFile
->hasWeakExternals() && dylibFile
->hasWeakDefinition(name
) ) {
1316 static bool vectorContains(const std::vector
<ld::dylib::File
*>& vec
, ld::dylib::File
* key
)
1318 return std::find(vec
.begin(), vec
.end(), key
) != vec
.end();
1321 struct DylibByInstallNameSorter
1323 bool operator()(const ld::dylib::File
* left
, const ld::dylib::File
* right
)
1325 return (strcmp(left
->installPath(), right
->installPath()) < 0);
1329 void InputFiles::dylibs(ld::Internal
& state
)
1331 bool dylibsOK
= false;
1332 switch ( _options
.outputKind() ) {
1333 case Options::kDynamicExecutable
:
1334 case Options::kDynamicLibrary
:
1335 case Options::kDynamicBundle
:
1338 case Options::kStaticExecutable
:
1339 case Options::kDyld
:
1340 case Options::kPreload
:
1341 case Options::kObjectFile
:
1342 case Options::kKextBundle
:
1347 // add command line dylibs in order
1348 for (std::vector
<ld::File
*>::const_iterator it
=_inputFiles
.begin(); it
!= _inputFiles
.end(); ++it
) {
1349 ld::dylib::File
* dylibFile
= dynamic_cast<ld::dylib::File
*>(*it
);
1350 // only add dylibs that are not "blank" dylib stubs
1351 if ( (dylibFile
!= NULL
) && ((dylibFile
->installPath() != NULL
) || (dylibFile
== _bundleLoader
)) ) {
1353 if ( ! vectorContains(state
.dylibs
, dylibFile
) ) {
1354 state
.dylibs
.push_back(dylibFile
);
1358 warning("unexpected dylib (%s) on link line", dylibFile
->path());
1361 // add implicitly linked dylibs
1362 if ( _options
.nameSpace() == Options::kTwoLevelNameSpace
) {
1363 std::vector
<ld::dylib::File
*> implicitDylibs
;
1364 for (InstallNameToDylib::const_iterator it
=_installPathToDylibs
.begin(); it
!= _installPathToDylibs
.end(); ++it
) {
1365 ld::dylib::File
* dylibFile
= it
->second
;
1366 if ( dylibFile
->implicitlyLinked() && dylibsOK
) {
1367 if ( ! vectorContains(implicitDylibs
, dylibFile
) ) {
1368 implicitDylibs
.push_back(dylibFile
);
1372 // <rdar://problem/15002251> make implicit dylib order be deterministic by sorting by install_name
1373 std::sort(implicitDylibs
.begin(), implicitDylibs
.end(), DylibByInstallNameSorter());
1374 state
.dylibs
.insert(state
.dylibs
.end(), implicitDylibs
.begin(), implicitDylibs
.end());
1377 //fprintf(stderr, "all dylibs:\n");
1378 //for(std::vector<ld::dylib::File*>::iterator it=state.dylibs.begin(); it != state.dylibs.end(); ++it) {
1379 // const ld::dylib::File* dylib = *it;
1380 // fprintf(stderr, " %p %s\n", dylib, dylib->path());
1383 // and -bundle_loader
1384 state
.bundleLoader
= _bundleLoader
;
1386 // <rdar://problem/10807040> give an error when -nostdlib is used and libSystem is missing
1387 if ( (state
.dylibs
.size() == 0) && _options
.needsEntryPointLoadCommand() )
1388 throw "dynamic main executables must link with libSystem.dylib";