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