]> git.saurik.com Git - apple/ld64.git/blob - src/ld/InputFiles.cpp
595b5d19cdc7d53fa500324914584c39536e6a64
[apple/ld64.git] / src / ld / InputFiles.cpp
1
2 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-*
3 *
4 * Copyright (c) 2009-2011 Apple Inc. All rights reserved.
5 *
6 * @APPLE_LICENSE_HEADER_START@
7 *
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
13 * file.
14 *
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.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25
26
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/sysctl.h>
32 #include <fcntl.h>
33 #include <errno.h>
34 #include <limits.h>
35 #include <unistd.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>
40 #include <dlfcn.h>
41 #include <mach-o/dyld.h>
42 #include <mach-o/fat.h>
43 #include <sys/sysctl.h>
44 #include <libkern/OSAtomic.h>
45
46 #include <string>
47 #include <map>
48 #include <set>
49 #include <string>
50 #include <vector>
51 #include <list>
52 #include <algorithm>
53 #include <dlfcn.h>
54 #include <AvailabilityMacros.h>
55
56 #include "Options.h"
57
58 #include "InputFiles.h"
59 #include "macho_relocatable_file.h"
60 #include "macho_dylib_file.h"
61 #include "archive_file.h"
62 #include "lto_file.h"
63 #include "opaque_section_file.h"
64 #include "MachOFileAbstraction.hpp"
65 #include "Snapshot.h"
66
67 const bool _s_logPThreads = false;
68
69 namespace ld {
70 namespace tool {
71
72 class IgnoredFile : public ld::File {
73 public:
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; };
77 };
78
79
80 class DSOHandleAtom : public ld::Atom {
81 public:
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) {}
89
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
95 { }
96 virtual void setScope(Scope) { }
97
98 virtual ~DSOHandleAtom() {}
99
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;
111 private:
112 const char* _name;
113 };
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);
125
126
127
128 class PageZeroAtom : public ld::Atom {
129 public:
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)),
134 _size(sz) {}
135
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
141 { }
142 virtual void setScope(Scope) { }
143
144 virtual ~PageZeroAtom() {}
145
146 static ld::Section _s_section;
147 static DSOHandleAtom _s_atomAll;
148 private:
149 uint64_t _size;
150 };
151 ld::Section PageZeroAtom::_s_section("__PAGEZERO", "__pagezero", ld::Section::typePageZero, true);
152
153
154 class CustomStackAtom : public ld::Atom {
155 public:
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)),
160 _size(sz) {}
161
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
167 { }
168 virtual void setScope(Scope) { }
169
170 virtual ~CustomStackAtom() {}
171
172 private:
173 uint64_t _size;
174 static ld::Section _s_section;
175 };
176 ld::Section CustomStackAtom::_s_section("__UNIXSTACK", "__stack", ld::Section::typeStack, true);
177
178
179
180 const char* InputFiles::fileArch(const uint8_t* p, unsigned len)
181 {
182 const char* result = mach_o::relocatable::archName(p);
183 if ( result != NULL )
184 return result;
185
186 result = mach_o::dylib::archName(p);
187 if ( result != NULL )
188 return result;
189
190 result = lto::archName(p, len);
191 if ( result != NULL )
192 return result;
193
194 if ( strncmp((const char*)p, "!<arch>\n", 8) == 0 )
195 return "archive";
196
197 char *unsupported = (char *)malloc(128);
198 strcpy(unsupported, "unsupported file format (");
199 for (unsigned i=0; i<len && i < 16; i++) {
200 char buf[8];
201 sprintf(buf, " 0x%02X", p[i]);
202 strcat(unsupported, buf);
203 }
204 strcat(unsupported, " )");
205 return unsupported;
206 }
207
208
209 ld::File* InputFiles::makeFile(const Options::FileInfo& info, bool indirectDylib)
210 {
211 // map in whole file
212 uint64_t len = info.fileLen;
213 int fd = ::open(info.path, O_RDONLY, 0);
214 if ( fd == -1 )
215 throwf("can't open file, errno=%d", errno);
216 if ( info.fileLen < 20 )
217 throwf("file too small (length=%llu)", info.fileLen);
218
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);
222
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) ) {
229 isFatFile = true;
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()) ) {
238 sliceToUse = i;
239 sliceFound = true;
240 break;
241 }
242 }
243 }
244 if ( !sliceFound ) {
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() ) {
248 sliceToUse = i;
249 sliceFound = true;
250 break;
251 }
252 }
253 }
254 if ( sliceFound ) {
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
259 sleep(1);
260 uint64_t newFileLen = info.fileLen;
261 struct stat statBuffer;
262 if ( stat(info.path, &statBuffer) == 0 ) {
263 newFileLen = statBuffer.st_size;
264 }
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);
268 }
269 }
270 // if requested architecture is page aligned within fat file, then remap just that portion of file
271 if ( (fileOffset & 0x00000FFF) == 0 ) {
272 // unmap whole file
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);
278 }
279 else {
280 p = &p[fileOffset];
281 }
282 }
283 }
284 ::close(fd);
285
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.subType = _options.subArchitecture();
297 ld::relocatable::File* objResult = mach_o::relocatable::parse(p, len, info.path, info.modTime, info.ordinal, objOpts);
298 if ( objResult != NULL ) {
299 OSAtomicAdd64(len, &_totalObjectSize);
300 OSAtomicIncrement32(&_totalObjectLoaded);
301 return objResult;
302 }
303
304 // see if it is an llvm object file
305 objResult = lto::parse(p, len, info.path, info.modTime, info.ordinal, _options.architecture(), _options.subArchitecture(), _options.logAllFiles(), _options.verboseOptimizationHints());
306 if ( objResult != NULL ) {
307 OSAtomicAdd64(len, &_totalObjectSize);
308 OSAtomicIncrement32(&_totalObjectLoaded);
309 return objResult;
310 }
311
312 // see if it is a dynamic library
313 ld::dylib::File* dylibResult;
314 bool dylibsNotAllowed = false;
315 switch ( _options.outputKind() ) {
316 case Options::kDynamicExecutable:
317 case Options::kDynamicLibrary:
318 case Options::kDynamicBundle:
319 dylibResult = mach_o::dylib::parse(p, len, info.path, info.modTime, _options, info.ordinal, info.options.fBundleLoader, indirectDylib);
320 if ( dylibResult != NULL ) {
321 return dylibResult;
322 }
323 break;
324 case Options::kStaticExecutable:
325 case Options::kDyld:
326 case Options::kPreload:
327 case Options::kObjectFile:
328 case Options::kKextBundle:
329 dylibsNotAllowed = true;
330 break;
331 }
332
333
334 // see if it is a static library
335 ::archive::ParserOptions archOpts;
336 archOpts.objOpts = objOpts;
337 archOpts.forceLoadThisArchive = info.options.fForceLoad;
338 archOpts.forceLoadAll = _options.fullyLoadArchives();
339 archOpts.forceLoadObjC = _options.loadAllObjcObjectsFromArchives();
340 archOpts.objcABI2 = _options.objCABIVersion2POverride();
341 archOpts.verboseLoad = _options.whyLoad();
342 archOpts.logAllFiles = _options.logAllFiles();
343 ld::archive::File* archiveResult = ::archive::parse(p, len, info.path, info.modTime, info.ordinal, archOpts);
344 if ( archiveResult != NULL ) {
345 OSAtomicAdd64(len, &_totalArchiveSize);
346 OSAtomicIncrement32(&_totalArchivesLoaded);
347 return archiveResult;
348 }
349
350 // does not seem to be any valid linker input file, check LTO misconfiguration problems
351 if ( lto::archName((uint8_t*)p, len) != NULL ) {
352 if ( lto::libLTOisLoaded() ) {
353 throwf("lto file was built for %s which is not the architecture being linked (%s): %s", fileArch(p, len), _options.architectureName(), info.path);
354 }
355 else {
356 const char* libLTO = "libLTO.dylib";
357 char ldPath[PATH_MAX];
358 char tmpPath[PATH_MAX];
359 char libLTOPath[PATH_MAX];
360 uint32_t bufSize = PATH_MAX;
361 if ( _options.overridePathlibLTO() != NULL ) {
362 libLTO = _options.overridePathlibLTO();
363 }
364 else if ( _NSGetExecutablePath(ldPath, &bufSize) != -1 ) {
365 if ( realpath(ldPath, tmpPath) != NULL ) {
366 char* lastSlash = strrchr(tmpPath, '/');
367 if ( lastSlash != NULL )
368 strcpy(lastSlash, "/../lib/libLTO.dylib");
369 libLTO = tmpPath;
370 if ( realpath(tmpPath, libLTOPath) != NULL )
371 libLTO = libLTOPath;
372 }
373 }
374 throwf("could not process llvm bitcode object file, because %s could not be loaded", libLTO);
375 }
376 }
377
378 if ( dylibsNotAllowed ) {
379 cpu_type_t dummy1;
380 cpu_type_t dummy2;
381 if ( mach_o::dylib::isDylibFile(p, &dummy1, &dummy2) )
382 throw "ignoring unexpected dylib file";
383 }
384
385 // error handling
386 if ( ((fat_header*)p)->magic == OSSwapBigToHostInt32(FAT_MAGIC) ) {
387 throwf("missing required architecture %s in file %s (%u slices)", _options.architectureName(), info.path, sliceCount);
388 }
389 else {
390 if ( isFatFile )
391 throwf("file is universal (%u slices) but does not contain a(n) %s slice: %s", sliceCount, _options.architectureName(), info.path);
392 else
393 throwf("file was built for %s which is not the architecture being linked (%s): %s", fileArch(p, len), _options.architectureName(), info.path);
394 }
395 }
396
397 void InputFiles::logDylib(ld::File* file, bool indirect)
398 {
399 if ( _options.traceDylibs() ) {
400 const char* fullPath = file->path();
401 char realName[MAXPATHLEN];
402 if ( realpath(fullPath, realName) != NULL )
403 fullPath = realName;
404 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(file);
405 if ( (dylib != NULL ) && dylib->willBeUpwardDylib() ) {
406 // don't log upward dylibs when XBS is computing dependencies
407 logTraceInfo("[Logging for XBS] Used upward dynamic library: %s\n", fullPath);
408 }
409 else {
410 if ( indirect )
411 logTraceInfo("[Logging for XBS] Used indirect dynamic library: %s\n", fullPath);
412 else
413 logTraceInfo("[Logging for XBS] Used dynamic library: %s\n", fullPath);
414 }
415 }
416
417 if ( _options.dumpDependencyInfo() ) {
418 const ld::dylib::File* dylib = dynamic_cast<const ld::dylib::File*>(file);
419 if ( file == _bundleLoader ) {
420 _options.dumpDependency(Options::depBundleLoader, file->path());
421 }
422 else if ( (dylib != NULL ) && dylib->willBeUpwardDylib() ) {
423 if ( indirect )
424 _options.dumpDependency(Options::depUpwardIndirectDylib, file->path());
425 else
426 _options.dumpDependency(Options::depUpwardDirectDylib, file->path());
427 }
428 else {
429 if ( indirect )
430 _options.dumpDependency(Options::depIndirectDylib, file->path());
431 else
432 _options.dumpDependency(Options::depDirectDylib, file->path());
433 }
434 }
435 }
436
437 void InputFiles::logArchive(ld::File* file) const
438 {
439 if ( _options.traceArchives() && (_archiveFilesLogged.count(file) == 0) ) {
440 // <rdar://problem/4947347> LD_TRACE_ARCHIVES should only print out when a .o is actually used from an archive
441 _archiveFilesLogged.insert(file);
442 const char* fullPath = file->path();
443 char realName[MAXPATHLEN];
444 if ( realpath(fullPath, realName) != NULL )
445 fullPath = realName;
446 logTraceInfo("[Logging for XBS] Used static archive: %s\n", fullPath);
447 }
448 }
449
450
451 void InputFiles::logTraceInfo(const char* format, ...) const
452 {
453 // one time open() of custom LD_TRACE_FILE
454 static int trace_file = -1;
455 if ( trace_file == -1 ) {
456 const char *trace_file_path = _options.traceOutputFile();
457 if ( trace_file_path != NULL ) {
458 trace_file = open(trace_file_path, O_WRONLY | O_APPEND | O_CREAT, 0666);
459 if ( trace_file == -1 )
460 throwf("Could not open or create trace file (errno=%d): %s", errno, trace_file_path);
461 }
462 else {
463 trace_file = fileno(stderr);
464 }
465 }
466
467 char trace_buffer[MAXPATHLEN * 2];
468 va_list ap;
469 va_start(ap, format);
470 int length = vsnprintf(trace_buffer, sizeof(trace_buffer), format, ap);
471 va_end(ap);
472 char* buffer_ptr = trace_buffer;
473
474 while (length > 0) {
475 ssize_t amount_written = write(trace_file, buffer_ptr, length);
476 if(amount_written == -1)
477 /* Failure to write shouldn't fail the build. */
478 return;
479 buffer_ptr += amount_written;
480 length -= amount_written;
481 }
482 }
483
484
485 ld::dylib::File* InputFiles::findDylib(const char* installPath, const char* fromPath)
486 {
487 //fprintf(stderr, "findDylib(%s, %s)\n", installPath, fromPath);
488 InstallNameToDylib::iterator pos = _installPathToDylibs.find(installPath);
489 if ( pos != _installPathToDylibs.end() ) {
490 return pos->second;
491 }
492 else {
493 // allow -dylib_path option to override indirect library to use
494 for (std::vector<Options::DylibOverride>::const_iterator dit = _options.dylibOverrides().begin(); dit != _options.dylibOverrides().end(); ++dit) {
495 if ( strcmp(dit->installName,installPath) == 0 ) {
496 try {
497 Options::FileInfo info = _options.findFile(dit->useInstead);
498 _indirectDylibOrdinal = _indirectDylibOrdinal.nextIndirectDylibOrdinal();
499 info.ordinal = _indirectDylibOrdinal;
500 info.options.fIndirectDylib = true;
501 ld::File* reader = this->makeFile(info, true);
502 ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(reader);
503 if ( dylibReader != NULL ) {
504 addDylib(dylibReader, info);
505 //_installPathToDylibs[strdup(installPath)] = dylibReader;
506 this->logDylib(dylibReader, true);
507 return dylibReader;
508 }
509 else
510 throwf("indirect dylib at %s is not a dylib", dit->useInstead);
511 }
512 catch (const char* msg) {
513 warning("ignoring -dylib_file option, %s", msg);
514 }
515 }
516 }
517 char newPath[MAXPATHLEN];
518 // handle @loader_path
519 if ( strncmp(installPath, "@loader_path/", 13) == 0 ) {
520 strcpy(newPath, fromPath);
521 char* addPoint = strrchr(newPath,'/');
522 if ( addPoint != NULL )
523 strcpy(&addPoint[1], &installPath[13]);
524 else
525 strcpy(newPath, &installPath[13]);
526 installPath = newPath;
527 }
528 // note: @executable_path case is handled inside findFileUsingPaths()
529 // search for dylib using -F and -L paths
530 Options::FileInfo info = _options.findFileUsingPaths(installPath);
531 _indirectDylibOrdinal = _indirectDylibOrdinal.nextIndirectDylibOrdinal();
532 info.ordinal = _indirectDylibOrdinal;
533 info.options.fIndirectDylib = true;
534 try {
535 ld::File* reader = this->makeFile(info, true);
536 ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(reader);
537 if ( dylibReader != NULL ) {
538 //assert(_installPathToDylibs.find(installPath) != _installPathToDylibs.end());
539 //_installPathToDylibs[strdup(installPath)] = dylibReader;
540 addDylib(dylibReader, info);
541 this->logDylib(dylibReader, true);
542 return dylibReader;
543 }
544 else
545 throwf("indirect dylib at %s is not a dylib", info.path);
546 }
547 catch (const char* msg) {
548 throwf("in '%s', %s", info.path, msg);
549 }
550 }
551 }
552
553
554 // mark all dylibs initially specified as required, and check if they can be used
555 void InputFiles::markExplicitlyLinkedDylibs()
556 {
557 for (InstallNameToDylib::iterator it=_installPathToDylibs.begin(); it != _installPathToDylibs.end(); it++) {
558 it->second->setExplicitlyLinked();
559 this->checkDylibClientRestrictions(it->second);
560 }
561 }
562
563 bool InputFiles::libraryAlreadyLoaded(const char* path)
564 {
565 for (std::vector<ld::File*>::const_iterator it = _inputFiles.begin(); it != _inputFiles.end(); ++it) {
566 if ( strcmp(path, (*it)->path()) == 0 )
567 return true;
568 }
569 return false;
570 }
571
572
573 void InputFiles::addLinkerOptionLibraries(ld::Internal& state, ld::File::AtomHandler& handler)
574 {
575 if ( _options.outputKind() == Options::kObjectFile )
576 return;
577
578 // process frameworks specified in .o linker options
579 for (CStringSet::const_iterator it = state.linkerOptionFrameworks.begin(); it != state.linkerOptionFrameworks.end(); ++it) {
580 const char* frameworkName = *it;
581 Options::FileInfo info = _options.findFramework(frameworkName);
582 if ( ! this->libraryAlreadyLoaded(info.path) ) {
583 info.ordinal = _linkerOptionOrdinal.nextLinkerOptionOrdinal();
584 try {
585 ld::File* reader = this->makeFile(info, true);
586 ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(reader);
587 if ( dylibReader != NULL ) {
588 if ( ! dylibReader->installPathVersionSpecific() ) {
589 dylibReader->forEachAtom(handler);
590 dylibReader->setImplicitlyLinked();
591 this->addDylib(dylibReader, info);
592 }
593 }
594 else {
595 throwf("framework linker option at %s is not a dylib", info.path);
596 }
597 }
598 catch (const char* msg) {
599 warning("Auto-Linking supplied '%s', %s", info.path, msg);
600 }
601 }
602 }
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();
609 try {
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->forEachAtom(handler);
617 dylibReader->setImplicitlyLinked();
618 this->addDylib(dylibReader, info);
619 }
620 else if ( archiveReader != NULL ) {
621 _searchLibraries.push_back(LibraryInfo(archiveReader));
622 if ( _options.dumpDependencyInfo() )
623 _options.dumpDependency(Options::depArchive, archiveReader->path());
624 //<rdar://problem/17787306> -force_load_swift_libs
625 if (info.options.fForceLoad) {
626 archiveReader->forEachAtom(handler);
627 }
628 }
629 else {
630 throwf("linker option dylib at %s is not a dylib", info.path);
631 }
632 }
633 catch (const char* msg) {
634 warning("Auto-Linking supplied '%s', %s", info.path, msg);
635 }
636 }
637 }
638 }
639
640 void InputFiles::createIndirectDylibs()
641 {
642 // keep processing dylibs until no more dylibs are added
643 unsigned long lastMapSize = 0;
644 std::set<ld::dylib::File*> dylibsProcessed;
645 while ( lastMapSize != _allDylibs.size() ) {
646 lastMapSize = _allDylibs.size();
647 // can't iterator _installPathToDylibs while modifying it, so use temp buffer
648 std::vector<ld::dylib::File*> unprocessedDylibs;
649 for (std::set<ld::dylib::File*>::iterator it=_allDylibs.begin(); it != _allDylibs.end(); it++) {
650 if ( dylibsProcessed.count(*it) == 0 )
651 unprocessedDylibs.push_back(*it);
652 }
653 for (std::vector<ld::dylib::File*>::iterator it=unprocessedDylibs.begin(); it != unprocessedDylibs.end(); it++) {
654 dylibsProcessed.insert(*it);
655 (*it)->processIndirectLibraries(this, _options.implicitlyLinkIndirectPublicDylibs());
656 }
657 }
658
659 // go back over original dylibs and mark sub frameworks as re-exported
660 if ( _options.outputKind() == Options::kDynamicLibrary ) {
661 const char* myLeaf = strrchr(_options.installPath(), '/');
662 if ( myLeaf != NULL ) {
663 for (std::vector<class ld::File*>::const_iterator it=_inputFiles.begin(); it != _inputFiles.end(); it++) {
664 ld::dylib::File* dylibReader = dynamic_cast<ld::dylib::File*>(*it);
665 if ( dylibReader != NULL ) {
666 const char* childParent = dylibReader->parentUmbrella();
667 if ( childParent != NULL ) {
668 if ( strcmp(childParent, &myLeaf[1]) == 0 ) {
669 // mark that this dylib will be re-exported
670 dylibReader->setWillBeReExported();
671 }
672 }
673 }
674 }
675 }
676 }
677
678 }
679
680 void InputFiles::createOpaqueFileSections()
681 {
682 // extra command line sections always at end
683 for (Options::ExtraSection::const_iterator it=_options.extraSectionsBegin(); it != _options.extraSectionsEnd(); ++it) {
684 _inputFiles.push_back(opaque_section::parse(it->segmentName, it->sectionName, it->path, it->data, it->dataLen));
685 if ( _options.dumpDependencyInfo() )
686 _options.dumpDependency(Options::depSection, it->path);
687 }
688
689 }
690
691
692 void InputFiles::checkDylibClientRestrictions(ld::dylib::File* dylib)
693 {
694 // Check for any restrictions on who can link with this dylib
695 const char* dylibParentName = dylib->parentUmbrella() ;
696 const std::vector<const char*>* clients = dylib->allowableClients();
697 if ( (dylibParentName != NULL) || (clients != NULL) ) {
698 // only dylibs that are in an umbrella or have a client list need verification
699 const char* installName = _options.installPath();
700 const char* installNameLastSlash = strrchr(installName, '/');
701 bool isParent = false;
702 bool isSibling = false;
703 bool isAllowableClient = false;
704 // There are three cases:
705 if ( (dylibParentName != NULL) && (installNameLastSlash != NULL) ) {
706 // starts after last slash
707 const char* myName = &installNameLastSlash[1];
708 unsigned int myNameLen = strlen(myName);
709 if ( strncmp(myName, "lib", 3) == 0 )
710 myName = &myName[3];
711 // up to first dot
712 const char* firstDot = strchr(myName, '.');
713 if ( firstDot != NULL )
714 myNameLen = firstDot - myName;
715 // up to first underscore
716 const char* firstUnderscore = strchr(myName, '_');
717 if ( (firstUnderscore != NULL) && ((firstUnderscore - myName) < (int)myNameLen) )
718 myNameLen = firstUnderscore - myName;
719
720 // case 1) The dylib has a parent umbrella, and we are creating the parent umbrella
721 isParent = ( (strlen(dylibParentName) == myNameLen) && (strncmp(myName, dylibParentName, myNameLen) == 0) );
722
723 // case 2) The dylib has a parent umbrella, and we are creating a sibling with the same parent
724 isSibling = ( (_options.umbrellaName() != NULL) && (strcmp(_options.umbrellaName(), dylibParentName) == 0) );
725 }
726
727 if ( !isParent && !isSibling && (clients != NULL) ) {
728 // case 3) the dylib has a list of allowable clients, and we are creating one of them
729 const char* clientName = _options.clientName();
730 int clientNameLen = 0;
731 if ( clientName != NULL ) {
732 // use client name as specified on command line
733 clientNameLen = strlen(clientName);
734 }
735 else {
736 // infer client name from output path (e.g. xxx/libfoo_variant.A.dylib --> foo, Bar.framework/Bar_variant --> Bar)
737 clientName = installName;
738 clientNameLen = strlen(clientName);
739 // starts after last slash
740 if ( installNameLastSlash != NULL )
741 clientName = &installNameLastSlash[1];
742 if ( strncmp(clientName, "lib", 3) == 0 )
743 clientName = &clientName[3];
744 // up to first dot
745 const char* firstDot = strchr(clientName, '.');
746 if ( firstDot != NULL )
747 clientNameLen = firstDot - clientName;
748 // up to first underscore
749 const char* firstUnderscore = strchr(clientName, '_');
750 if ( (firstUnderscore != NULL) && ((firstUnderscore - clientName) < clientNameLen) )
751 clientNameLen = firstUnderscore - clientName;
752 }
753
754 // Use clientName to check if this dylib is able to link against the allowable clients.
755 for (std::vector<const char*>::const_iterator it = clients->begin(); it != clients->end(); it++) {
756 if ( strncmp(*it, clientName, clientNameLen) == 0 )
757 isAllowableClient = true;
758 }
759 }
760
761 if ( !isParent && !isSibling && !isAllowableClient ) {
762 if ( dylibParentName != NULL ) {
763 throwf("cannot link directly with %s. Link against the umbrella framework '%s.framework' instead.",
764 dylib->path(), dylibParentName);
765 }
766 else {
767 throwf("cannot link directly with %s", dylib->path());
768 }
769 }
770 }
771 }
772
773
774 void InputFiles::inferArchitecture(Options& opts, const char** archName)
775 {
776 _inferredArch = true;
777 // scan all input files, looking for a thin .o file.
778 // the first one found is presumably the architecture to link
779 uint8_t buffer[sizeof(mach_header_64)];
780 const std::vector<Options::FileInfo>& files = opts.getInputFiles();
781 for (std::vector<Options::FileInfo>::const_iterator it = files.begin(); it != files.end(); ++it) {
782 int fd = ::open(it->path, O_RDONLY, 0);
783 if ( fd != -1 ) {
784 ssize_t amount = read(fd, buffer, sizeof(buffer));
785 ::close(fd);
786 if ( amount >= (ssize_t)sizeof(buffer) ) {
787 cpu_type_t type;
788 cpu_subtype_t subtype;
789 if ( mach_o::relocatable::isObjectFile(buffer, &type, &subtype) ) {
790 opts.setArchitecture(type, subtype);
791 *archName = opts.architectureName();
792 return;
793 }
794 }
795 }
796 }
797
798 // no thin .o files found, so default to same architecture this tool was built as
799 warning("-arch not specified");
800 #if __i386__
801 opts.setArchitecture(CPU_TYPE_I386, CPU_SUBTYPE_X86_ALL);
802 #elif __x86_64__
803 opts.setArchitecture(CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
804 #elif __arm__
805 opts.setArchitecture(CPU_TYPE_ARM, CPU_SUBTYPE_ARM_V6);
806 #else
807 #error unknown default architecture
808 #endif
809 *archName = opts.architectureName();
810 }
811
812
813 InputFiles::InputFiles(Options& opts, const char** archName)
814 : _totalObjectSize(0), _totalArchiveSize(0),
815 _totalObjectLoaded(0), _totalArchivesLoaded(0), _totalDylibsLoaded(0),
816 _options(opts), _bundleLoader(NULL),
817 _inferredArch(false),
818 _exception(NULL),
819 _indirectDylibOrdinal(ld::File::Ordinal::indirectDylibBase()),
820 _linkerOptionOrdinal(ld::File::Ordinal::linkeOptionBase())
821 {
822 // fStartCreateReadersTime = mach_absolute_time();
823 if ( opts.architecture() == 0 ) {
824 // command line missing -arch, so guess arch
825 inferArchitecture(opts, archName);
826 }
827 #if HAVE_PTHREADS
828 pthread_mutex_init(&_parseLock, NULL);
829 pthread_cond_init(&_parseWorkReady, NULL);
830 pthread_cond_init(&_newFileAvailable, NULL);
831 #endif
832 const std::vector<Options::FileInfo>& files = _options.getInputFiles();
833 if ( files.size() == 0 )
834 throw "no object files specified";
835
836 _inputFiles.reserve(files.size());
837 #if HAVE_PTHREADS
838 unsigned int inputFileSlot = 0;
839 _availableInputFiles = 0;
840 _parseCursor = 0;
841 #endif
842 Options::FileInfo* entry;
843 for (std::vector<Options::FileInfo>::const_iterator it = files.begin(); it != files.end(); ++it) {
844 entry = (Options::FileInfo*)&(*it);
845 #if HAVE_PTHREADS
846 // Assign input file slots to all the FileInfos.
847 // Also chain all FileInfos into one big list to set up for worker threads to do parsing.
848 entry->inputFileSlot = inputFileSlot;
849 entry->readyToParse = !entry->fromFileList || !_options.pipelineEnabled();
850 if (entry->readyToParse)
851 _availableInputFiles++;
852 _inputFiles.push_back(NULL);
853 inputFileSlot++;
854 #else
855 // In the non-threaded case just parse the file now.
856 _inputFiles.push_back(makeFile(*entry, false));
857 #endif
858 }
859
860 #if HAVE_PTHREADS
861 _remainingInputFiles = files.size();
862
863 // initialize info for parsing input files on worker threads
864 unsigned int ncpus;
865 int mib[2];
866 size_t len = sizeof(ncpus);
867 mib[0] = CTL_HW;
868 mib[1] = HW_NCPU;
869 if (sysctl(mib, 2, &ncpus, &len, NULL, 0) != 0) {
870 ncpus = 1;
871 }
872 _availableWorkers = MIN(ncpus, files.size()); // max # workers we permit
873 _idleWorkers = 0;
874
875 if (_options.pipelineEnabled()) {
876 // start up a thread to listen for available input files
877 startThread(InputFiles::waitForInputFiles);
878 }
879
880 // Start up one parser thread. More start on demand as parsed input files get consumed.
881 startThread(InputFiles::parseWorkerThread);
882 _availableWorkers--;
883 #else
884 if (_options.pipelineEnabled()) {
885 throwf("pipelined linking not supported on this platform");
886 }
887 #endif
888 }
889
890
891 #if HAVE_PTHREADS
892 void InputFiles::startThread(void (*threadFunc)(InputFiles *)) const {
893 pthread_t thread;
894 pthread_attr_t attr;
895 pthread_attr_init(&attr);
896 // set a nice big stack (same as main thread) because some code uses potentially large stack buffers
897 pthread_attr_setstacksize(&attr, 8 * 1024 * 1024);
898 pthread_create(&thread, &attr, (void *(*)(void*))threadFunc, (void *)this);
899 pthread_detach(thread);
900 pthread_attr_destroy(&attr);
901 }
902
903 // Work loop for input file parsing threads
904 void InputFiles::parseWorkerThread() {
905 ld::File *file;
906 const char *exception = NULL;
907 pthread_mutex_lock(&_parseLock);
908 const std::vector<Options::FileInfo>& files = _options.getInputFiles();
909 if (_s_logPThreads) printf("worker starting\n");
910 do {
911 if (_availableInputFiles == 0) {
912 _idleWorkers++;
913 pthread_cond_wait(&_parseWorkReady, &_parseLock);
914 _idleWorkers--;
915 } else {
916 int slot = _parseCursor;
917 while (slot < (int)files.size() && (_inputFiles[slot] != NULL || !files[slot].readyToParse))
918 slot++;
919 assert(slot < (int)files.size());
920 Options::FileInfo& entry = (Options::FileInfo&)files[slot];
921 _parseCursor = slot+1;
922 _availableInputFiles--;
923 entry.readyToParse = false; // to avoid multiple threads finding this file
924 pthread_mutex_unlock(&_parseLock);
925 if (_s_logPThreads) printf("parsing index %u\n", slot);
926 try {
927 file = makeFile(entry, false);
928 }
929 catch (const char *msg) {
930 if ( (strstr(msg, "architecture") != NULL) && !_options.errorOnOtherArchFiles() ) {
931 if ( _options.ignoreOtherArchInputFiles() ) {
932 // ignore, because this is about an architecture not in use
933 }
934 else {
935 warning("ignoring file %s, %s", entry.path, msg);
936 }
937 }
938 else if ( strstr(msg, "ignoring unexpected") != NULL ) {
939 warning("%s, %s", entry.path, msg);
940 }
941 else {
942 asprintf((char**)&exception, "%s file '%s'", msg, entry.path);
943 }
944 file = new IgnoredFile(entry.path, entry.modTime, entry.ordinal, ld::File::Other);
945 }
946 pthread_mutex_lock(&_parseLock);
947 if (_remainingInputFiles > 0)
948 _remainingInputFiles--;
949 if (_s_logPThreads) printf("done with index %u, %d remaining\n", slot, _remainingInputFiles);
950 if (exception) {
951 // We are about to die, so set to zero to stop other threads from doing unneeded work.
952 _remainingInputFiles = 0;
953 _exception = exception;
954 }
955 else {
956 _inputFiles[slot] = file;
957 if (_neededFileSlot == slot)
958 pthread_cond_signal(&_newFileAvailable);
959 }
960 }
961 } while (_remainingInputFiles);
962 if (_s_logPThreads) printf("worker exiting\n");
963 pthread_cond_broadcast(&_parseWorkReady);
964 pthread_cond_signal(&_newFileAvailable);
965 pthread_mutex_unlock(&_parseLock);
966 }
967
968
969 void InputFiles::parseWorkerThread(InputFiles *inputFiles) {
970 inputFiles->parseWorkerThread();
971 }
972 #endif
973
974
975 ld::File* InputFiles::addDylib(ld::dylib::File* reader, const Options::FileInfo& info)
976 {
977 _allDylibs.insert(reader);
978
979 if ( (reader->installPath() == NULL) && !info.options.fBundleLoader ) {
980 // this is a "blank" stub
981 // silently ignore it
982 return reader;
983 }
984 // store options about how dylib will be used in dylib itself
985 if ( info.options.fWeakImport )
986 reader->setForcedWeakLinked();
987 if ( info.options.fReExport )
988 reader->setWillBeReExported();
989 if ( info.options.fUpward ) {
990 if ( _options.outputKind() == Options::kDynamicLibrary )
991 reader->setWillBeUpwardDylib();
992 else
993 warning("ignoring upward dylib option for %s\n", info.path);
994 }
995 if ( info.options.fLazyLoad )
996 reader->setWillBeLazyLoadedDylb();
997
998 // add to map of loaded dylibs
999 const char* installPath = reader->installPath();
1000 if ( installPath != NULL ) {
1001 InstallNameToDylib::iterator pos = _installPathToDylibs.find(installPath);
1002 if ( pos == _installPathToDylibs.end() ) {
1003 _installPathToDylibs[strdup(installPath)] = reader;
1004 }
1005 else {
1006 bool dylibOnCommandLineTwice = ( strcmp(pos->second->path(), reader->path()) == 0 );
1007 bool isSymlink = false;
1008 // ignore if this is a symlink to a dylib we've already loaded
1009 if ( !dylibOnCommandLineTwice ) {
1010 char existingDylibPath[PATH_MAX];
1011 if ( realpath(pos->second->path(), existingDylibPath) != NULL ) {
1012 char newDylibPath[PATH_MAX];
1013 if ( realpath(reader->path(), newDylibPath) != NULL ) {
1014 isSymlink = ( strcmp(existingDylibPath, newDylibPath) == 0 );
1015 }
1016 }
1017 }
1018 // remove warning for <rdar://problem/10860629> Same install name for CoreServices and CFNetwork?
1019 //if ( !dylibOnCommandLineTwice && !isSymlink )
1020 // warning("dylibs with same install name: %p %s and %p %s", pos->second, pos->second->path(), reader, reader->path());
1021 }
1022 }
1023 else if ( info.options.fBundleLoader )
1024 _bundleLoader = reader;
1025
1026 // log direct readers
1027 if ( ! info.options.fIndirectDylib )
1028 this->logDylib(reader, false);
1029
1030 // update stats
1031 _totalDylibsLoaded++;
1032
1033 // just add direct libraries to search-first list
1034 if ( ! info.options.fIndirectDylib )
1035 _searchLibraries.push_back(LibraryInfo(reader));
1036
1037 return reader;
1038 }
1039
1040
1041 #if HAVE_PTHREADS
1042 // Called during pipelined linking to listen for available input files.
1043 // Available files are enqueued for parsing.
1044 void InputFiles::waitForInputFiles()
1045 {
1046 if (_s_logPThreads) printf("starting pipeline listener\n");
1047 try {
1048 const char *fifo = _options.pipelineFifo();
1049 assert(fifo);
1050 std::map<const char *, const Options::FileInfo*, strcompclass> fileMap;
1051 const std::vector<Options::FileInfo>& files = _options.getInputFiles();
1052 for (std::vector<Options::FileInfo>::const_iterator it = files.begin(); it != files.end(); ++it) {
1053 const Options::FileInfo& entry = *it;
1054 if (entry.fromFileList) {
1055 fileMap[entry.path] = &entry;
1056 }
1057 }
1058 FILE *fileStream = fopen(fifo, "r");
1059 if (!fileStream)
1060 throwf("pipelined linking error - failed to open stream. fopen() returns %s for \"%s\"\n", strerror(errno), fifo);
1061 while (fileMap.size() > 0) {
1062 char path_buf[PATH_MAX+1];
1063 if (fgets(path_buf, PATH_MAX, fileStream) == NULL)
1064 throwf("pipelined linking error - %lu missing input files", fileMap.size());
1065 int len = strlen(path_buf);
1066 if (path_buf[len-1] == '\n')
1067 path_buf[len-1] = 0;
1068 std::map<const char *, const Options::FileInfo*, strcompclass>::iterator it = fileMap.find(path_buf);
1069 if (it == fileMap.end())
1070 throwf("pipelined linking error - not in file list: %s\n", path_buf);
1071 Options::FileInfo* inputInfo = (Options::FileInfo*)it->second;
1072 if (!inputInfo->checkFileExists(_options))
1073 throwf("pipelined linking error - file does not exist: %s\n", inputInfo->path);
1074 pthread_mutex_lock(&_parseLock);
1075 if (_idleWorkers)
1076 pthread_cond_signal(&_parseWorkReady);
1077 inputInfo->readyToParse = true;
1078 if (_parseCursor > inputInfo->inputFileSlot)
1079 _parseCursor = inputInfo->inputFileSlot;
1080 _availableInputFiles++;
1081 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);
1082 pthread_mutex_unlock(&_parseLock);
1083 fileMap.erase(it);
1084 }
1085 } catch (const char *msg) {
1086 pthread_mutex_lock(&_parseLock);
1087 _exception = msg;
1088 pthread_cond_signal(&_newFileAvailable);
1089 pthread_mutex_unlock(&_parseLock);
1090 }
1091 }
1092
1093
1094 void InputFiles::waitForInputFiles(InputFiles *inputFiles) {
1095 inputFiles->waitForInputFiles();
1096 }
1097 #endif
1098
1099
1100 void InputFiles::forEachInitialAtom(ld::File::AtomHandler& handler, ld::Internal& state)
1101 {
1102 // add all direct object, archives, and dylibs
1103 const std::vector<Options::FileInfo>& files = _options.getInputFiles();
1104 size_t fileIndex;
1105 for (fileIndex=0; fileIndex<_inputFiles.size(); fileIndex++) {
1106 ld::File *file;
1107 #if HAVE_PTHREADS
1108 pthread_mutex_lock(&_parseLock);
1109
1110 // this loop waits for the needed file to be ready (parsed by worker thread)
1111 while (_inputFiles[fileIndex] == NULL && _exception == NULL) {
1112 // We are starved for input. If there are still files to parse and we have
1113 // not maxed out the worker thread count start a new worker thread.
1114 if (_availableInputFiles > 0 && _availableWorkers > 0) {
1115 if (_s_logPThreads) printf("starting worker\n");
1116 startThread(InputFiles::parseWorkerThread);
1117 _availableWorkers--;
1118 }
1119 _neededFileSlot = fileIndex;
1120 if (_s_logPThreads) printf("consumer blocking for %lu: %s\n", fileIndex, files[fileIndex].path);
1121 pthread_cond_wait(&_newFileAvailable, &_parseLock);
1122 }
1123
1124 if (_exception)
1125 throw _exception;
1126
1127 // The input file is parsed. Assimilate it and call its atom iterator.
1128 if (_s_logPThreads) printf("consuming slot %lu\n", fileIndex);
1129 file = _inputFiles[fileIndex];
1130 pthread_mutex_unlock(&_parseLock);
1131 #else
1132 file = _inputFiles[fileIndex];
1133 #endif
1134 const Options::FileInfo& info = files[fileIndex];
1135 switch (file->type()) {
1136 case ld::File::Reloc:
1137 {
1138 ld::relocatable::File* reloc = (ld::relocatable::File*)file;
1139 _options.snapshot().recordObjectFile(reloc->path());
1140 if ( _options.dumpDependencyInfo() )
1141 _options.dumpDependency(Options::depObjectFile, reloc->path());
1142 }
1143 break;
1144 case ld::File::Dylib:
1145 {
1146 ld::dylib::File* dylib = (ld::dylib::File*)file;
1147 addDylib(dylib, info);
1148 }
1149 break;
1150 case ld::File::Archive:
1151 {
1152 ld::archive::File* archive = (ld::archive::File*)file;
1153 // <rdar://problem/9740166> force loaded archives should be in LD_TRACE
1154 if ( (info.options.fForceLoad || _options.fullyLoadArchives()) && _options.traceArchives() )
1155 logArchive(archive);
1156 _searchLibraries.push_back(LibraryInfo(archive));
1157 if ( _options.dumpDependencyInfo() )
1158 _options.dumpDependency(Options::depArchive, archive->path());
1159 }
1160 break;
1161 case ld::File::Other:
1162 break;
1163 default:
1164 {
1165 throwf("Unknown file type for %s", file->path());
1166 }
1167 break;
1168 }
1169 file->forEachAtom(handler);
1170 }
1171
1172 markExplicitlyLinkedDylibs();
1173 addLinkerOptionLibraries(state, handler);
1174 createIndirectDylibs();
1175 createOpaqueFileSections();
1176
1177 while (fileIndex < _inputFiles.size()) {
1178 ld::File *file = _inputFiles[fileIndex];
1179 file->forEachAtom(handler);
1180 fileIndex++;
1181 }
1182
1183 switch ( _options.outputKind() ) {
1184 case Options::kStaticExecutable:
1185 case Options::kDynamicExecutable:
1186 // add implicit __dso_handle label
1187 handler.doAtom(DSOHandleAtom::_s_atomExecutable);
1188 handler.doAtom(DSOHandleAtom::_s_atomAll);
1189 if ( _options.pageZeroSize() != 0 )
1190 handler.doAtom(*new PageZeroAtom(_options.pageZeroSize()));
1191 if ( _options.hasCustomStack() && !_options.needsEntryPointLoadCommand() )
1192 handler.doAtom(*new CustomStackAtom(_options.customStackSize()));
1193 break;
1194 case Options::kDynamicLibrary:
1195 // add implicit __dso_handle label
1196 handler.doAtom(DSOHandleAtom::_s_atomDylib);
1197 handler.doAtom(DSOHandleAtom::_s_atomAll);
1198 break;
1199 case Options::kDynamicBundle:
1200 // add implicit __dso_handle label
1201 handler.doAtom(DSOHandleAtom::_s_atomBundle);
1202 handler.doAtom(DSOHandleAtom::_s_atomAll);
1203 break;
1204 case Options::kDyld:
1205 // add implicit __dso_handle label
1206 handler.doAtom(DSOHandleAtom::_s_atomDyld);
1207 handler.doAtom(DSOHandleAtom::_s_atomAll);
1208 break;
1209 case Options::kPreload:
1210 // add implicit __mh_preload_header label
1211 handler.doAtom(DSOHandleAtom::_s_atomPreload);
1212 // add implicit __dso_handle label, but put it in __text section because
1213 // with -preload the mach_header is no in the address space.
1214 handler.doAtom(DSOHandleAtom::_s_atomPreloadDSO);
1215 break;
1216 case Options::kObjectFile:
1217 handler.doAtom(DSOHandleAtom::_s_atomObjectFile);
1218 break;
1219 case Options::kKextBundle:
1220 // add implicit __dso_handle label
1221 handler.doAtom(DSOHandleAtom::_s_atomAll);
1222 break;
1223 }
1224 }
1225
1226
1227 bool InputFiles::searchLibraries(const char* name, bool searchDylibs, bool searchArchives, bool dataSymbolOnly, ld::File::AtomHandler& handler) const
1228 {
1229 // Check each input library.
1230 for (std::vector<LibraryInfo>::const_iterator it=_searchLibraries.begin(); it != _searchLibraries.end(); ++it) {
1231 LibraryInfo lib = *it;
1232 if (lib.isDylib()) {
1233 if (searchDylibs) {
1234 ld::dylib::File *dylibFile = lib.dylib();
1235 //fprintf(stderr, "searchLibraries(%s), looking in linked %s\n", name, dylibFile->path() );
1236 if ( dylibFile->justInTimeforEachAtom(name, handler) ) {
1237 // we found a definition in this dylib
1238 // done, unless it is a weak definition in which case we keep searching
1239 _options.snapshot().recordDylibSymbol(dylibFile, name);
1240 if ( !dylibFile->hasWeakExternals() || !dylibFile->hasWeakDefinition(name)) {
1241 return true;
1242 }
1243 // else continue search for a non-weak definition
1244 }
1245 }
1246 } else {
1247 if (searchArchives) {
1248 ld::archive::File *archiveFile = lib.archive();
1249 if ( dataSymbolOnly ) {
1250 if ( archiveFile->justInTimeDataOnlyforEachAtom(name, handler) ) {
1251 if ( _options.traceArchives() )
1252 logArchive(archiveFile);
1253 _options.snapshot().recordArchive(archiveFile->path());
1254 // found data definition in static library, done
1255 return true;
1256 }
1257 }
1258 else {
1259 if ( archiveFile->justInTimeforEachAtom(name, handler) ) {
1260 if ( _options.traceArchives() )
1261 logArchive(archiveFile);
1262 _options.snapshot().recordArchive(archiveFile->path());
1263 // found definition in static library, done
1264 return true;
1265 }
1266 }
1267 }
1268 }
1269 }
1270
1271 // search indirect dylibs
1272 if ( searchDylibs ) {
1273 for (InstallNameToDylib::const_iterator it=_installPathToDylibs.begin(); it != _installPathToDylibs.end(); ++it) {
1274 ld::dylib::File* dylibFile = it->second;
1275 bool searchThisDylib = false;
1276 if ( _options.nameSpace() == Options::kTwoLevelNameSpace ) {
1277 // for two level namesapce, just check all implicitly linked dylibs
1278 searchThisDylib = dylibFile->implicitlyLinked() && !dylibFile->explicitlyLinked();
1279 }
1280 else {
1281 // for flat namespace, check all indirect dylibs
1282 searchThisDylib = ! dylibFile->explicitlyLinked();
1283 }
1284 if ( searchThisDylib ) {
1285 //fprintf(stderr, "searchLibraries(%s), looking in implicitly linked %s\n", name, dylibFile->path() );
1286 if ( dylibFile->justInTimeforEachAtom(name, handler) ) {
1287 // we found a definition in this dylib
1288 // done, unless it is a weak definition in which case we keep searching
1289 _options.snapshot().recordDylibSymbol(dylibFile, name);
1290 if ( !dylibFile->hasWeakExternals() || !dylibFile->hasWeakDefinition(name)) {
1291 return true;
1292 }
1293 // else continue search for a non-weak definition
1294 }
1295 }
1296 }
1297 }
1298
1299 return false;
1300 }
1301
1302
1303 bool InputFiles::searchWeakDefInDylib(const char* name) const
1304 {
1305 // search all relevant dylibs to see if any have a weak-def with this name
1306 for (InstallNameToDylib::const_iterator it=_installPathToDylibs.begin(); it != _installPathToDylibs.end(); ++it) {
1307 ld::dylib::File* dylibFile = it->second;
1308 if ( dylibFile->implicitlyLinked() || dylibFile->explicitlyLinked() ) {
1309 if ( dylibFile->hasWeakExternals() && dylibFile->hasWeakDefinition(name) ) {
1310 return true;
1311 }
1312 }
1313 }
1314 return false;
1315 }
1316
1317 static bool vectorContains(const std::vector<ld::dylib::File*>& vec, ld::dylib::File* key)
1318 {
1319 return std::find(vec.begin(), vec.end(), key) != vec.end();
1320 }
1321
1322 struct DylibByInstallNameSorter
1323 {
1324 bool operator()(const ld::dylib::File* left, const ld::dylib::File* right)
1325 {
1326 return (strcmp(left->installPath(), right->installPath()) < 0);
1327 }
1328 };
1329
1330 void InputFiles::dylibs(ld::Internal& state)
1331 {
1332 bool dylibsOK = false;
1333 switch ( _options.outputKind() ) {
1334 case Options::kDynamicExecutable:
1335 case Options::kDynamicLibrary:
1336 case Options::kDynamicBundle:
1337 dylibsOK = true;
1338 break;
1339 case Options::kStaticExecutable:
1340 case Options::kDyld:
1341 case Options::kPreload:
1342 case Options::kObjectFile:
1343 case Options::kKextBundle:
1344 dylibsOK = false;
1345 break;
1346 }
1347
1348 // add command line dylibs in order
1349 for (std::vector<ld::File*>::const_iterator it=_inputFiles.begin(); it != _inputFiles.end(); ++it) {
1350 ld::dylib::File* dylibFile = dynamic_cast<ld::dylib::File*>(*it);
1351 // only add dylibs that are not "blank" dylib stubs
1352 if ( (dylibFile != NULL) && ((dylibFile->installPath() != NULL) || (dylibFile == _bundleLoader)) ) {
1353 if ( dylibsOK ) {
1354 if ( ! vectorContains(state.dylibs, dylibFile) ) {
1355 state.dylibs.push_back(dylibFile);
1356 }
1357 }
1358 else
1359 warning("unexpected dylib (%s) on link line", dylibFile->path());
1360 }
1361 }
1362 // add implicitly linked dylibs
1363 if ( _options.nameSpace() == Options::kTwoLevelNameSpace ) {
1364 std::vector<ld::dylib::File*> implicitDylibs;
1365 for (InstallNameToDylib::const_iterator it=_installPathToDylibs.begin(); it != _installPathToDylibs.end(); ++it) {
1366 ld::dylib::File* dylibFile = it->second;
1367 if ( dylibFile->implicitlyLinked() && dylibsOK ) {
1368 if ( ! vectorContains(implicitDylibs, dylibFile) ) {
1369 implicitDylibs.push_back(dylibFile);
1370 }
1371 }
1372 }
1373 // <rdar://problem/15002251> make implicit dylib order be deterministic by sorting by install_name
1374 std::sort(implicitDylibs.begin(), implicitDylibs.end(), DylibByInstallNameSorter());
1375 state.dylibs.insert(state.dylibs.end(), implicitDylibs.begin(), implicitDylibs.end());
1376 }
1377
1378 //fprintf(stderr, "all dylibs:\n");
1379 //for(std::vector<ld::dylib::File*>::iterator it=state.dylibs.begin(); it != state.dylibs.end(); ++it) {
1380 // const ld::dylib::File* dylib = *it;
1381 // fprintf(stderr, " %p %s\n", dylib, dylib->path());
1382 //}
1383
1384 // and -bundle_loader
1385 state.bundleLoader = _bundleLoader;
1386
1387 // <rdar://problem/10807040> give an error when -nostdlib is used and libSystem is missing
1388 if ( (state.dylibs.size() == 0) && _options.needsEntryPointLoadCommand() )
1389 throw "dynamic main executables must link with libSystem.dylib";
1390 }
1391
1392
1393 } // namespace tool
1394 } // namespace ld
1395