]> git.saurik.com Git - apple/ld64.git/blame - src/ld/parsers/macho_dylib_file.cpp
ld64-128.2.tar.gz
[apple/ld64.git] / src / ld / parsers / macho_dylib_file.cpp
CommitLineData
a645023d
A
1/* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
afe874b1 3 * Copyright (c) 2005-2011 Apple Inc. All rights reserved.
a645023d
A
4 *
5 * @APPLE_LICENSE_HEADER_START@
6 *
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
12 * file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24
25
26#include <stdint.h>
27#include <math.h>
28#include <unistd.h>
29#include <sys/param.h>
30#include <sys/mman.h>
31
32
33#include <vector>
34#include <set>
35#include <algorithm>
36#include <ext/hash_map>
37#include <ext/hash_set>
38
39#include "Architectures.hpp"
40#include "MachOFileAbstraction.hpp"
41#include "MachOTrie.hpp"
42#include "macho_dylib_file.h"
43
44
45namespace mach_o {
46namespace dylib {
47
48
49// forward reference
50template <typename A> class File;
51
52
53//
54// An ExportAtom has no content. It exists so that the linker can track which imported
55// symbols came from which dynamic libraries.
56//
57template <typename A>
58class ExportAtom : public ld::Atom
59{
60public:
61 ExportAtom(const File<A>& f, const char* nm, bool weakDef,
62 bool tlv, typename A::P::uint_t address)
63 : ld::Atom(f._importProxySection, ld::Atom::definitionProxy,
64 (weakDef? ld::Atom::combineByName : ld::Atom::combineNever),
65 ld::Atom::scopeLinkageUnit,
66 (tlv ? ld::Atom::typeTLV : ld::Atom::typeUnclassified),
67 symbolTableNotIn, false, false, false, ld::Atom::Alignment(0)),
68 _file(f), _name(nm), _address(address) {}
69 // overrides of ld::Atom
70 virtual const ld::File* file() const { return &_file; }
71 virtual bool translationUnitSource(const char** dir, const char** nm) const
72 { return false; }
73 virtual const char* name() const { return _name; }
74 virtual uint64_t size() const { return 0; }
75 virtual uint64_t objectAddress() const { return _address; }
76 virtual void copyRawContent(uint8_t buffer[]) const { }
77 virtual void setScope(Scope) { }
78
79protected:
80 typedef typename A::P P;
81 typedef typename A::P::uint_t pint_t;
82
83 virtual ~ExportAtom() {}
84
85 const File<A>& _file;
86 const char* _name;
87 pint_t _address;
88};
89
90
91
92//
93// An ImportAtom has no content. It exists so that when linking a main executable flat-namespace
94// the imports of all flat dylibs are checked
95//
96template <typename A>
97class ImportAtom : public ld::Atom
98{
99public:
100 ImportAtom(File<A>& f, std::vector<const char*>& imports);
101
102 // overrides of ld::Atom
103 virtual ld::File* file() const { return &_file; }
104 virtual bool translationUnitSource(const char** dir, const char** nm) const
105 { return false; }
106 virtual const char* name() const { return "import-atom"; }
107 virtual uint64_t size() const { return 0; }
108 virtual uint64_t objectAddress() const { return 0; }
109 virtual void copyRawContent(uint8_t buffer[]) const { }
110 virtual void setScope(Scope) { }
111 virtual ld::Fixup::iterator fixupsBegin() const { return &_undefs[0]; }
112 virtual ld::Fixup::iterator fixupsEnd() const { return &_undefs[_undefs.size()]; }
113
114protected:
115 typedef typename A::P P;
116
117 virtual ~ImportAtom() {}
118
119
120 File<A>& _file;
121 mutable std::vector<ld::Fixup> _undefs;
122};
123
124template <typename A>
125ImportAtom<A>::ImportAtom(File<A>& f, std::vector<const char*>& imports)
126: ld::Atom(f._flatDummySection, ld::Atom::definitionRegular, ld::Atom::combineNever, ld::Atom::scopeTranslationUnit,
127 ld::Atom::typeUnclassified, symbolTableNotIn, false, false, false, ld::Atom::Alignment(0)), _file(f)
128{
129 for(std::vector<const char*>::iterator it=imports.begin(); it != imports.end(); ++it) {
130 _undefs.push_back(ld::Fixup(0, ld::Fixup::k1of1, ld::Fixup::kindNone, false, strdup(*it)));
131 }
132}
133
134
135
136//
137// The reader for a dylib extracts all exported symbols names from the memory-mapped
138// dylib, builds a hash table, then unmaps the file. This is an important memory
139// savings for large dylibs.
140//
141template <typename A>
142class File : public ld::dylib::File
143{
144public:
145 static bool validFile(const uint8_t* fileContent, bool executableOrDylib);
146 File(const uint8_t* fileContent, uint64_t fileLength, const char* path,
147 time_t mTime, uint32_t ordinal, bool linkingFlatNamespace,
148 bool linkingMainExecutable, bool hoistImplicitPublicDylibs,
afe874b1
A
149 ld::MacVersionMin macMin, ld::IOSVersionMin iPhoneMin, bool addVers,
150 bool logAllFiles, const char* installPath, bool indirectDylib);
a645023d
A
151 virtual ~File() {}
152
153 // overrides of ld::File
154 virtual bool forEachAtom(ld::File::AtomHandler&) const;
155 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const;
156 virtual ld::File::ObjcConstraint objCConstraint() const { return _objcContraint; }
157
158 // overrides of ld::dylib::File
159 virtual void processIndirectLibraries(ld::dylib::File::DylibHandler*, bool);
160 virtual bool providedExportAtom() const { return _providedAtom; }
161 virtual const char* parentUmbrella() const { return _parentUmbrella; }
162 virtual const std::vector<const char*>* allowableClients() const { return _allowableClients.size() != 0 ? &_allowableClients : NULL; }
163 virtual bool hasWeakExternals() const { return _hasWeakExports; }
164 virtual bool deadStrippable() const { return _deadStrippable; }
165 virtual bool hasPublicInstallName() const{ return _hasPublicInstallName; }
166 virtual bool hasWeakDefinition(const char* name) const;
afe874b1 167 virtual bool allSymbolsAreWeakImported() const;
a645023d
A
168
169
170protected:
171
172 struct ReExportChain { ReExportChain* prev; File<A>* file; };
173
174 void assertNoReExportCycles(ReExportChain*);
175
176private:
177 typedef typename A::P P;
178 typedef typename A::P::E E;
179 typedef typename A::P::uint_t pint_t;
180
181 friend class ExportAtom<A>;
182 friend class ImportAtom<A>;
183
184 class CStringEquals
185 {
186 public:
187 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
188 };
afe874b1 189 struct AtomAndWeak { ld::Atom* atom; bool weakDef; bool tlv; pint_t address; };
a645023d
A
190 typedef __gnu_cxx::hash_map<const char*, AtomAndWeak, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtomMap;
191 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet;
192
193 struct Dependent { const char* path; File<A>* dylib; bool reExport; };
194
195 bool containsOrReExports(const char* name, bool* weakDef, bool* tlv, pint_t* defAddress) const;
196 bool isPublicLocation(const char* pth);
197 void addSymbol(const char* name, bool weak, bool tlv, pint_t address);
198 void addDyldFastStub();
199 void buildExportHashTableFromExportInfo(const macho_dyld_info_command<P>* dyldInfo,
200 const uint8_t* fileContent);
201 void buildExportHashTableFromSymbolTable(const macho_dysymtab_command<P>* dynamicInfo,
202 const macho_nlist<P>* symbolTable, const char* strings,
203 const uint8_t* fileContent);
204 static const char* objCInfoSegmentName();
205 static const char* objCInfoSectionName();
206
207 const ld::MacVersionMin _macVersionMin;
afe874b1
A
208 const ld::IOSVersionMin _iOSVersionMin;
209 const bool _addVersionLoadCommand;
a645023d
A
210 bool _linkingFlat;
211 bool _implicitlyLinkPublicDylibs;
212 ld::File::ObjcConstraint _objcContraint;
213 ld::Section _importProxySection;
214 ld::Section _flatDummySection;
215 std::vector<Dependent> _dependentDylibs;
216 std::vector<const char*> _allowableClients;
217 mutable NameToAtomMap _atoms;
218 NameSet _ignoreExports;
219 const char* _parentUmbrella;
220 ImportAtom<A>* _importAtom;
221 bool _noRexports;
222 bool _hasWeakExports;
223 bool _deadStrippable;
224 bool _hasPublicInstallName;
225 mutable bool _providedAtom;
226 bool _explictReExportFound;
227
228 static bool _s_logHashtable;
229};
230
231template <typename A>
232bool File<A>::_s_logHashtable = false;
233
234template <> const char* File<x86_64>::objCInfoSegmentName() { return "__DATA"; }
235template <> const char* File<arm>::objCInfoSegmentName() { return "__DATA"; }
236template <typename A> const char* File<A>::objCInfoSegmentName() { return "__OBJC"; }
237
238template <> const char* File<x86_64>::objCInfoSectionName() { return "__objc_imageinfo"; }
239template <> const char* File<arm>::objCInfoSectionName() { return "__objc_imageinfo"; }
240template <typename A> const char* File<A>::objCInfoSectionName() { return "__image_info"; }
241
242template <typename A>
243File<A>::File(const uint8_t* fileContent, uint64_t fileLength, const char* pth, time_t mTime, uint32_t ord,
244 bool linkingFlatNamespace, bool linkingMainExecutable, bool hoistImplicitPublicDylibs,
afe874b1
A
245 ld::MacVersionMin macMin, ld::IOSVersionMin iOSMin, bool addVers,
246 bool logAllFiles, const char* targetInstallPath, bool indirectDylib)
a645023d 247 : ld::dylib::File(strdup(pth), mTime, ord),
afe874b1 248 _macVersionMin(macMin), _iOSVersionMin(iOSMin), _addVersionLoadCommand(addVers),
a645023d
A
249 _linkingFlat(linkingFlatNamespace), _implicitlyLinkPublicDylibs(hoistImplicitPublicDylibs),
250 _objcContraint(ld::File::objcConstraintNone),
251 _importProxySection("__TEXT", "__import", ld::Section::typeImportProxies, true),
252 _flatDummySection("__LINKEDIT", "__flat_dummy", ld::Section::typeLinkEdit, true),
253 _parentUmbrella(NULL), _importAtom(NULL), _noRexports(false), _hasWeakExports(false),
254 _deadStrippable(false), _hasPublicInstallName(false),
255 _providedAtom(false), _explictReExportFound(false)
256{
257 const macho_header<P>* header = (const macho_header<P>*)fileContent;
258 const uint32_t cmd_count = header->ncmds();
259 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
260 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
261
262 // write out path for -t option
263 if ( logAllFiles )
264 printf("%s\n", pth);
265
266 // a "blank" stub has zero load commands
267 if ( (header->filetype() == MH_DYLIB_STUB) && (cmd_count == 0) ) {
268 // no further processing needed
269 munmap((caddr_t)fileContent, fileLength);
270 return;
271 }
272
273
274 // optimize the case where we know there is no reason to look at indirect dylibs
275 _noRexports = (header->flags() & MH_NO_REEXPORTED_DYLIBS)
276 || (header->filetype() == MH_BUNDLE)
277 || (header->filetype() == MH_EXECUTE); // bundles and exectuables can be used via -bundle_loader
278 _hasWeakExports = (header->flags() & MH_WEAK_DEFINES);
279 _deadStrippable = (header->flags() & MH_DEAD_STRIPPABLE_DYLIB);
280
281 // pass 1: get pointers, and see if this dylib uses compressed LINKEDIT format
282 const macho_dysymtab_command<P>* dynamicInfo = NULL;
283 const macho_dyld_info_command<P>* dyldInfo = NULL;
284 const macho_nlist<P>* symbolTable = NULL;
285 const char* strings = NULL;
286 bool compressedLinkEdit = false;
287 uint32_t dependentLibCount = 0;
288 const macho_load_command<P>* cmd = cmds;
289 for (uint32_t i = 0; i < cmd_count; ++i) {
290 macho_dylib_command<P>* dylibID;
291 const macho_symtab_command<P>* symtab;
292 switch (cmd->cmd()) {
293 case LC_SYMTAB:
294 symtab = (macho_symtab_command<P>*)cmd;
295 symbolTable = (const macho_nlist<P>*)((char*)header + symtab->symoff());
296 strings = (char*)header + symtab->stroff();
297 break;
298 case LC_DYSYMTAB:
299 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
300 break;
301 case LC_DYLD_INFO:
302 case LC_DYLD_INFO_ONLY:
303 dyldInfo = (macho_dyld_info_command<P>*)cmd;
304 compressedLinkEdit = true;
305 break;
306 case LC_ID_DYLIB:
307 dylibID = (macho_dylib_command<P>*)cmd;
308 _dylibInstallPath = strdup(dylibID->name());
309 _dylibTimeStamp = dylibID->timestamp();
310 _dylibCurrentVersion = dylibID->current_version();
311 _dylibCompatibilityVersion = dylibID->compatibility_version();
312 _hasPublicInstallName = isPublicLocation(_dylibInstallPath);
313 break;
314 case LC_LOAD_DYLIB:
315 case LC_LOAD_WEAK_DYLIB:
316 ++dependentLibCount;
317 break;
318 case LC_REEXPORT_DYLIB:
319 _explictReExportFound = true;
320 ++dependentLibCount;
321 break;
322 case LC_SUB_FRAMEWORK:
323 _parentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
324 break;
325 case LC_SUB_CLIENT:
326 _allowableClients.push_back(strdup(((macho_sub_client_command<P>*)cmd)->client()));
327 break;
afe874b1
A
328 case LC_VERSION_MIN_MACOSX:
329 if ( _addVersionLoadCommand && !indirectDylib && (_iOSVersionMin != ld::iOSVersionUnset) )
330 warning("building for iOS, but linking against dylib built for MacOSX: %s", pth);
331 break;
332 case LC_VERSION_MIN_IPHONEOS:
333 if ( _addVersionLoadCommand && !indirectDylib && (_macVersionMin != ld::macVersionUnset) )
334 warning("building for MacOSX, but linking against dylib built for iOS: %s", pth);
335 break;
a645023d
A
336 case macho_segment_command<P>::CMD:
337 // check for Objective-C info
338 if ( strcmp(((macho_segment_command<P>*)cmd)->segname(), objCInfoSegmentName()) == 0 ) {
339 const macho_segment_command<P>* segment = (macho_segment_command<P>*)cmd;
340 const macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
341 const macho_section<P>* const sectionsEnd = &sectionsStart[segment->nsects()];
342 for (const macho_section<P>* sect=sectionsStart; sect < sectionsEnd; ++sect) {
343 if ( strncmp(sect->sectname(), objCInfoSectionName(), strlen(objCInfoSectionName())) == 0 ) {
344 // struct objc_image_info {
345 // uint32_t version; // initially 0
346 // uint32_t flags;
347 // };
348 // #define OBJC_IMAGE_SUPPORTS_GC 2
349 // #define OBJC_IMAGE_GC_ONLY 4
350 //
351 const uint32_t* contents = (uint32_t*)(&fileContent[sect->offset()]);
352 if ( (sect->size() >= 8) && (contents[0] == 0) ) {
353 uint32_t flags = E::get32(contents[1]);
354 if ( (flags & 4) == 4 )
355 _objcContraint = ld::File::objcConstraintGC;
356 else if ( (flags & 2) == 2 )
357 _objcContraint = ld::File::objcConstraintRetainReleaseOrGC;
358 else
359 _objcContraint = ld::File::objcConstraintRetainRelease;
360 }
361 else if ( sect->size() > 0 ) {
362 warning("can't parse %s/%s section in %s", objCInfoSegmentName(), objCInfoSectionName(), this->path());
363 }
364 }
365 }
366 }
367 }
368 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
369 if ( cmd > cmdsEnd )
370 throwf("malformed dylb, load command #%d is outside size of load commands in %s", i, pth);
371 }
372
373 // figure out if we need to examine dependent dylibs
374 // with compressed LINKEDIT format, MH_NO_REEXPORTED_DYLIBS can be trusted
375 bool processDependentLibraries = true;
376 if ( compressedLinkEdit && _noRexports && !linkingFlatNamespace)
377 processDependentLibraries = false;
378
379 if ( processDependentLibraries ) {
380 // pass 2 builds list of all dependent libraries
381 _dependentDylibs.reserve(dependentLibCount);
382 cmd = cmds;
383 for (uint32_t i = 0; i < cmd_count; ++i) {
384 switch (cmd->cmd()) {
385 case LC_LOAD_DYLIB:
386 case LC_LOAD_WEAK_DYLIB:
387 // with new linkedit format only care about LC_REEXPORT_DYLIB
388 if ( compressedLinkEdit && !linkingFlatNamespace )
389 break;
390 case LC_REEXPORT_DYLIB:
391 Dependent entry;
392 entry.path = strdup(((macho_dylib_command<P>*)cmd)->name());
393 entry.dylib = NULL;
394 entry.reExport = (cmd->cmd() == LC_REEXPORT_DYLIB);
afe874b1
A
395 if ( (targetInstallPath == NULL) || (strcmp(targetInstallPath, entry.path) != 0) )
396 _dependentDylibs.push_back(entry);
a645023d
A
397 break;
398 }
399 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
400 }
401 // verify MH_NO_REEXPORTED_DYLIBS bit was correct
402 if ( compressedLinkEdit && !linkingFlatNamespace ) {
403 assert(_dependentDylibs.size() != 0);
404 }
405 // pass 3 add re-export info
406 cmd = cmds;
407 for (uint32_t i = 0; i < cmd_count; ++i) {
408 const char* frameworkLeafName;
409 const char* dylibBaseName;
410 switch (cmd->cmd()) {
411 case LC_SUB_UMBRELLA:
412 frameworkLeafName = ((macho_sub_umbrella_command<P>*)cmd)->sub_umbrella();
413 for (typename std::vector<Dependent>::iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); ++it) {
414 const char* dylibName = it->path;
415 const char* lastSlash = strrchr(dylibName, '/');
416 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], frameworkLeafName) == 0) )
417 it->reExport = true;
418 }
419 break;
420 case LC_SUB_LIBRARY:
421 dylibBaseName = ((macho_sub_library_command<P>*)cmd)->sub_library();
422 for (typename std::vector<Dependent>::iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); ++it) {
423 const char* dylibName = it->path;
424 const char* lastSlash = strrchr(dylibName, '/');
425 const char* leafStart = &lastSlash[1];
426 if ( lastSlash == NULL )
427 leafStart = dylibName;
428 const char* firstDot = strchr(leafStart, '.');
429 int len = strlen(leafStart);
430 if ( firstDot != NULL )
431 len = firstDot - leafStart;
432 if ( strncmp(leafStart, dylibBaseName, len) == 0 )
433 it->reExport = true;
434 }
435 break;
436 }
437 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
438 }
439 }
440
441 // validate minimal load commands
442 if ( (_dylibInstallPath == NULL) && ((header->filetype() == MH_DYLIB) || (header->filetype() == MH_DYLIB_STUB)) )
443 throwf("dylib %s missing LC_ID_DYLIB load command", pth);
444 if ( dyldInfo == NULL ) {
445 if ( symbolTable == NULL )
446 throw "binary missing LC_SYMTAB load command";
447 if ( dynamicInfo == NULL )
448 throw "binary missing LC_DYSYMTAB load command";
449 }
450
451 // if linking flat and this is a flat dylib, create one atom that references all imported symbols
452 if ( linkingFlatNamespace && linkingMainExecutable && ((header->flags() & MH_TWOLEVEL) == 0) ) {
453 std::vector<const char*> importNames;
454 importNames.reserve(dynamicInfo->nundefsym());
455 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iundefsym()];
456 const macho_nlist<P>* end = &start[dynamicInfo->nundefsym()];
457 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
458 importNames.push_back(&strings[sym->n_strx()]);
459 }
460 _importAtom = new ImportAtom<A>(*this, importNames);
461 }
462
463 // build hash table
464 if ( dyldInfo != NULL )
465 buildExportHashTableFromExportInfo(dyldInfo, fileContent);
466 else
467 buildExportHashTableFromSymbolTable(dynamicInfo, symbolTable, strings, fileContent);
468
469 // unmap file
470 munmap((caddr_t)fileContent, fileLength);
471}
472
473
474template <typename A>
475void File<A>::buildExportHashTableFromSymbolTable(const macho_dysymtab_command<P>* dynamicInfo,
476 const macho_nlist<P>* symbolTable, const char* strings,
477 const uint8_t* fileContent)
478{
479 if ( dynamicInfo->tocoff() == 0 ) {
480 if ( _s_logHashtable ) fprintf(stderr, "ld: building hashtable of %u toc entries for %s\n", dynamicInfo->nextdefsym(), this->path());
481 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iextdefsym()];
482 const macho_nlist<P>* end = &start[dynamicInfo->nextdefsym()];
483 _atoms.resize(dynamicInfo->nextdefsym()); // set initial bucket count
484 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
485 this->addSymbol(&strings[sym->n_strx()], (sym->n_desc() & N_WEAK_DEF) != 0, false, sym->n_value());
486 }
487 }
488 else {
489 int32_t count = dynamicInfo->ntoc();
490 _atoms.resize(count); // set initial bucket count
491 if ( _s_logHashtable ) fprintf(stderr, "ld: building hashtable of %u entries for %s\n", count, this->path());
492 const struct dylib_table_of_contents* toc = (dylib_table_of_contents*)(fileContent + dynamicInfo->tocoff());
493 for (int32_t i = 0; i < count; ++i) {
494 const uint32_t index = E::get32(toc[i].symbol_index);
495 const macho_nlist<P>* sym = &symbolTable[index];
496 this->addSymbol(&strings[sym->n_strx()], (sym->n_desc() & N_WEAK_DEF) != 0, false, sym->n_value());
497 }
498 }
499
500 // special case old libSystem
501 if ( (_dylibInstallPath != NULL) && (strcmp(_dylibInstallPath, "/usr/lib/libSystem.B.dylib") == 0) )
502 addDyldFastStub();
503}
504
505
506template <typename A>
507void File<A>::buildExportHashTableFromExportInfo(const macho_dyld_info_command<P>* dyldInfo,
508 const uint8_t* fileContent)
509{
510 if ( _s_logHashtable ) fprintf(stderr, "ld: building hashtable from export info in %s\n", this->path());
511 if ( dyldInfo->export_size() > 0 ) {
512 const uint8_t* start = fileContent + dyldInfo->export_off();
513 const uint8_t* end = &start[dyldInfo->export_size()];
514 std::vector<mach_o::trie::Entry> list;
515 parseTrie(start, end, list);
516 for (std::vector<mach_o::trie::Entry>::iterator it=list.begin(); it != list.end(); ++it)
517 this->addSymbol(it->name,
518 it->flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION,
519 (it->flags & EXPORT_SYMBOL_FLAGS_KIND_MASK) == EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL,
520 it->address);
521 }
522}
523
524
525template <>
526void File<x86_64>::addDyldFastStub()
527{
528 addSymbol("dyld_stub_binder", false, false, 0);
529}
530
531template <>
532void File<x86>::addDyldFastStub()
533{
534 addSymbol("dyld_stub_binder", false, false, 0);
535}
536
537template <typename A>
538void File<A>::addDyldFastStub()
539{
540 // do nothing
541}
542
543template <typename A>
544void File<A>::addSymbol(const char* name, bool weakDef, bool tlv, pint_t address)
545{
546 if ( weakDef ) {
547 assert(_hasWeakExports);
548 }
549 //fprintf(stderr, "addSymbol() %s\n", name);
550 // symbols that start with $ld$ are meta-data to the static linker
551 // <rdar://problem/5182537> need way for ld and dyld to see different exported symbols in a dylib
552 if ( strncmp(name, "$ld$", 4) == 0 ) {
553 // $ld$ <action> $ <condition> $ <symbol-name>
554 const char* symAction = &name[4];
555 const char* symCond = strchr(symAction, '$');
556 if ( symCond != NULL ) {
557 char curOSVers[16];
558 if ( _macVersionMin != ld::macVersionUnset ) {
559 sprintf(curOSVers, "$os%d.%d$", (_macVersionMin >> 16), ((_macVersionMin >> 8) & 0xFF));
560 }
afe874b1
A
561 else if ( _iOSVersionMin != ld::iOSVersionUnset ) {
562 sprintf(curOSVers, "$os%d.%d$", (_iOSVersionMin >> 16), ((_iOSVersionMin >> 8) & 0xFF));
a645023d
A
563 }
564 else {
565 assert(0 && "targeting neither macosx nor iphoneos");
566 }
567 if ( strncmp(symCond, curOSVers, strlen(curOSVers)) == 0 ) {
568 const char* symName = strchr(&symCond[1], '$');
569 if ( symName != NULL ) {
570 ++symName;
571 if ( strncmp(symAction, "hide$", 5) == 0 ) {
572 if ( _s_logHashtable ) fprintf(stderr, " adding %s to ignore set for %s\n", symName, this->path());
573 _ignoreExports.insert(strdup(symName));
574 return;
575 }
576 else if ( strncmp(symAction, "add$", 4) == 0 ) {
577 this->addSymbol(symName, weakDef, false, 0);
578 return;
579 }
580 else {
581 warning("bad symbol action: %s in dylib %s", name, this->path());
582 }
583 }
584 }
585 }
586 else {
587 warning("bad symbol condition: %s in dylib %s", name, this->path());
588 }
589 }
590
591 // add symbol as possible export if we are not supposed to ignore it
592 if ( _ignoreExports.count(name) == 0 ) {
593 AtomAndWeak bucket;
594 bucket.atom = NULL;
afe874b1 595 bucket.weakDef = weakDef;
a645023d
A
596 bucket.tlv = tlv;
597 bucket.address = address;
598 if ( _s_logHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->path());
599 _atoms[strdup(name)] = bucket;
600 }
601}
602
603
604template <typename A>
605bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
606{
607 handler.doFile(*this);
608 // if doing flatnamespace and need all this dylib's imports resolve
609 // add atom which references alls undefines in this dylib
610 if ( _importAtom != NULL ) {
611 handler.doAtom(*_importAtom);
612 return true;
613 }
614 return false;
615}
616
617template <typename A>
618bool File<A>::hasWeakDefinition(const char* name) const
619{
620 // if supposed to ignore this export, then pretend I don't have it
621 if ( _ignoreExports.count(name) != 0 )
622 return false;
623
624 typename NameToAtomMap::const_iterator pos = _atoms.find(name);
625 if ( pos != _atoms.end() ) {
afe874b1 626 return pos->second.weakDef;
a645023d
A
627 }
628 else {
629 // look in children that I re-export
630 for (typename std::vector<Dependent>::const_iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); ++it) {
631 if ( it->reExport ) {
632 //fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s, looking in child %s\n", name, this->path(), (*it)->getInstallPath());
633 typename NameToAtomMap::iterator cpos = it->dylib->_atoms.find(name);
634 if ( cpos != it->dylib->_atoms.end() )
afe874b1 635 return cpos->second.weakDef;
a645023d
A
636 }
637 }
638 }
639 return false;
640}
641
afe874b1
A
642
643// <rdar://problem/5529626> If only weak_import symbols are used, linker should use LD_LOAD_WEAK_DYLIB
644template <typename A>
645bool File<A>::allSymbolsAreWeakImported() const
646{
647 bool foundNonWeakImport = false;
648 bool foundWeakImport = false;
649 //fprintf(stderr, "%s:\n", this->path());
650 for (typename NameToAtomMap::const_iterator it = _atoms.begin(); it != _atoms.end(); ++it) {
651 const ld::Atom* atom = it->second.atom;
652 if ( atom != NULL ) {
653 if ( atom->weakImported() )
654 foundWeakImport = true;
655 else
656 foundNonWeakImport = true;
657 //fprintf(stderr, " weak_import=%d, name=%s\n", atom->weakImported(), it->first);
658 }
659 }
660
661 // don't automatically weak link dylib with no imports
662 // so at least one weak import symbol and no non-weak-imported symbols must be found
663 return foundWeakImport && !foundNonWeakImport;
664}
665
666
a645023d
A
667template <typename A>
668bool File<A>::containsOrReExports(const char* name, bool* weakDef, bool* tlv, pint_t* defAddress) const
669{
afe874b1
A
670 if ( _ignoreExports.count(name) != 0 )
671 return false;
672
673// check myself
a645023d
A
674 typename NameToAtomMap::iterator pos = _atoms.find(name);
675 if ( pos != _atoms.end() ) {
afe874b1 676 *weakDef = pos->second.weakDef;
a645023d
A
677 *tlv = pos->second.tlv;
678 *defAddress = pos->second.address;
679 return true;
680 }
681
682 // check dylibs I re-export
683 for (typename std::vector<Dependent>::const_iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); ++it) {
684 if ( it->reExport && !it->dylib->implicitlyLinked() ) {
685 if ( it->dylib->containsOrReExports(name, weakDef, tlv, defAddress) )
686 return true;
687 }
688 }
689
690 return false;
691}
692
693
694template <typename A>
695bool File<A>::justInTimeforEachAtom(const char* name, ld::File::AtomHandler& handler) const
696{
697 // if supposed to ignore this export, then pretend I don't have it
698 if ( _ignoreExports.count(name) != 0 )
699 return false;
700
701
702 AtomAndWeak bucket;
afe874b1
A
703 if ( this->containsOrReExports(name, &bucket.weakDef, &bucket.tlv, &bucket.address) ) {
704 bucket.atom = new ExportAtom<A>(*this, name, bucket.weakDef, bucket.tlv, bucket.address);
a645023d
A
705 _atoms[name] = bucket;
706 _providedAtom = true;
707 if ( _s_logHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->path());
708 // call handler with new export atom
709 handler.doAtom(*bucket.atom);
710 return true;
711 }
712
713 return false;
714}
715
716
717
718template <typename A>
719bool File<A>::isPublicLocation(const char* pth)
720{
721 // -no_implicit_dylibs disables this optimization
722 if ( ! _implicitlyLinkPublicDylibs )
723 return false;
724
725 // /usr/lib is a public location
726 if ( (strncmp(pth, "/usr/lib/", 9) == 0) && (strchr(&pth[9], '/') == NULL) )
727 return true;
728
729 // /System/Library/Frameworks/ is a public location
730 if ( strncmp(pth, "/System/Library/Frameworks/", 27) == 0 ) {
731 const char* frameworkDot = strchr(&pth[27], '.');
732 // but only top level framework
733 // /System/Library/Frameworks/Foo.framework/Versions/A/Foo ==> true
734 // /System/Library/Frameworks/Foo.framework/Resources/libBar.dylib ==> false
735 // /System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Bar ==> false
736 // /System/Library/Frameworks/Foo.framework/Frameworks/Xfoo.framework/XFoo ==> false
737 if ( frameworkDot != NULL ) {
738 int frameworkNameLen = frameworkDot - &pth[27];
739 if ( strncmp(&pth[strlen(pth)-frameworkNameLen-1], &pth[26], frameworkNameLen+1) == 0 )
740 return true;
741 }
742 }
743
744 return false;
745}
746
747template <typename A>
748void File<A>::processIndirectLibraries(ld::dylib::File::DylibHandler* handler, bool addImplicitDylibs)
749{
750 const static bool log = false;
751 if ( log ) fprintf(stderr, "processIndirectLibraries(%s)\n", this->installPath());
752 if ( _linkingFlat ) {
753 for (typename std::vector<Dependent>::iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); it++) {
754 it->dylib = (File<A>*)handler->findDylib(it->path, this->path());
755 }
756 }
757 else if ( _noRexports ) {
758 // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do
759 }
760 else {
761 // two-level, might have re-exports
762 for (typename std::vector<Dependent>::iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); it++) {
763 if ( it->reExport ) {
764 if ( log ) fprintf(stderr, "processIndirectLibraries() parent=%s, child=%s\n", this->installPath(), it->path);
765 // a LC_REEXPORT_DYLIB, LC_SUB_UMBRELLA or LC_SUB_LIBRARY says we re-export this child
766 it->dylib = (File<A>*)handler->findDylib(it->path, this->path());
767 if ( it->dylib->hasPublicInstallName() ) {
768 // promote this child to be automatically added as a direct dependent if this already is
769 if ( (this->explicitlyLinked() || this->implicitlyLinked()) && (strcmp(it->path,it->dylib->installPath()) == 0) ) {
770 if ( log ) fprintf(stderr, "processIndirectLibraries() implicitly linking %s\n", it->dylib->installPath());
771 it->dylib->setImplicitlyLinked();
772 }
773 else if ( it->dylib->explicitlyLinked() || it->dylib->implicitlyLinked() ) {
774 if ( log ) fprintf(stderr, "processIndirectLibraries() parent is not directly linked, but child is, so no need to re-export child\n");
775 }
776 else {
777 if ( log ) fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->installPath(), it->path);
778 }
779 }
780 else {
781 // add all child's symbols to me
782 if ( log ) fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->installPath(), it->path);
783 }
784 }
785 else if ( !_explictReExportFound ) {
786 // see if child contains LC_SUB_FRAMEWORK with my name
787 it->dylib = (File<A>*)handler->findDylib(it->path, this->path());
788 const char* parentUmbrellaName = it->dylib->parentUmbrella();
789 if ( parentUmbrellaName != NULL ) {
790 const char* parentName = this->path();
791 const char* lastSlash = strrchr(parentName, '/');
792 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) ) {
793 // add all child's symbols to me
794 it->reExport = true;
795 if ( log ) fprintf(stderr, "processIndirectLibraries() umbrella=%s will re-export child=%s\n", this->installPath(), it->path);
796 }
797 }
798 }
799 }
800 }
801
802 // check for re-export cycles
803 ReExportChain chain;
804 chain.prev = NULL;
805 chain.file = this;
806 this->assertNoReExportCycles(&chain);
807}
808
809template <typename A>
810void File<A>::assertNoReExportCycles(ReExportChain* prev)
811{
812 // recursively check my re-exported dylibs
813 ReExportChain chain;
814 chain.prev = prev;
815 chain.file = this;
816 for (typename std::vector<Dependent>::iterator it = _dependentDylibs.begin(); it != _dependentDylibs.end(); it++) {
817 if ( it->reExport ) {
818 ld::File* child = it->dylib;
819 // check child is not already in chain
820 for (ReExportChain* p = prev; p != NULL; p = p->prev) {
821 if ( p->file == child ) {
822 throwf("cycle in dylib re-exports with %s and %s", child->path(), this->path());
823 }
824 }
825 if ( it->dylib != NULL )
826 it->dylib->assertNoReExportCycles(&chain);
827 }
828 }
829}
830
831
a645023d
A
832template <typename A>
833class Parser
834{
835public:
836 typedef typename A::P P;
837
838 static bool validFile(const uint8_t* fileContent, bool executableOrDyliborBundle);
839 static ld::dylib::File* parse(const uint8_t* fileContent, uint64_t fileLength,
840 const char* path, time_t mTime,
afe874b1 841 uint32_t ordinal, const Options& opts, bool indirectDylib) {
a645023d
A
842 return new File<A>(fileContent, fileLength, path, mTime,
843 ordinal, opts.flatNamespace(),
844 opts.linkingMainExecutable(),
845 opts.implicitlyLinkIndirectPublicDylibs(),
846 opts.macosxVersionMin(),
afe874b1
A
847 opts.iOSVersionMin(),
848 opts.addVersionLoadCommand(),
849 opts.logAllFiles(),
850 opts.installPath(),
851 indirectDylib);
a645023d
A
852 }
853
854};
855
856
857
a645023d
A
858template <>
859bool Parser<x86>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
860{
861 const macho_header<P>* header = (const macho_header<P>*)fileContent;
862 if ( header->magic() != MH_MAGIC )
863 return false;
864 if ( header->cputype() != CPU_TYPE_I386 )
865 return false;
866 switch ( header->filetype() ) {
867 case MH_DYLIB:
868 case MH_DYLIB_STUB:
869 return true;
870 case MH_BUNDLE:
871 if ( executableOrDyliborBundle )
872 return true;
873 else
874 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
875 case MH_EXECUTE:
876 if ( executableOrDyliborBundle )
877 return true;
878 else
879 throw "can't link with a main executable";
880 default:
881 return false;
882 }
883}
884
885template <>
886bool Parser<x86_64>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
887{
888 const macho_header<P>* header = (const macho_header<P>*)fileContent;
889 if ( header->magic() != MH_MAGIC_64 )
890 return false;
891 if ( header->cputype() != CPU_TYPE_X86_64 )
892 return false;
893 switch ( header->filetype() ) {
894 case MH_DYLIB:
895 case MH_DYLIB_STUB:
896 return true;
897 case MH_BUNDLE:
898 if ( executableOrDyliborBundle )
899 return true;
900 else
901 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
902 case MH_EXECUTE:
903 if ( executableOrDyliborBundle )
904 return true;
905 else
906 throw "can't link with a main executable";
907 default:
908 return false;
909 }
910}
911
912template <>
913bool Parser<arm>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
914{
915 const macho_header<P>* header = (const macho_header<P>*)fileContent;
916 if ( header->magic() != MH_MAGIC )
917 return false;
918 if ( header->cputype() != CPU_TYPE_ARM )
919 return false;
920 switch ( header->filetype() ) {
921 case MH_DYLIB:
922 case MH_DYLIB_STUB:
923 return true;
924 case MH_BUNDLE:
925 if ( executableOrDyliborBundle )
926 return true;
927 else
928 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
929 case MH_EXECUTE:
930 if ( executableOrDyliborBundle )
931 return true;
932 else
933 throw "can't link with a main executable";
934 default:
935 return false;
936 }
937}
938
939
940
941//
942// main function used by linker to instantiate ld::Files
943//
944ld::dylib::File* parse(const uint8_t* fileContent, uint64_t fileLength,
afe874b1
A
945 const char* path, time_t modTime, const Options& opts, uint32_t ordinal,
946 bool bundleLoader, bool indirectDylib)
a645023d
A
947{
948 switch ( opts.architecture() ) {
949 case CPU_TYPE_X86_64:
950 if ( Parser<x86_64>::validFile(fileContent, bundleLoader) )
afe874b1 951 return Parser<x86_64>::parse(fileContent, fileLength, path, modTime, ordinal, opts, indirectDylib);
a645023d
A
952 break;
953 case CPU_TYPE_I386:
954 if ( Parser<x86>::validFile(fileContent, bundleLoader) )
afe874b1 955 return Parser<x86>::parse(fileContent, fileLength, path, modTime, ordinal, opts, indirectDylib);
a645023d
A
956 break;
957 case CPU_TYPE_ARM:
958 if ( Parser<arm>::validFile(fileContent, bundleLoader) )
afe874b1 959 return Parser<arm>::parse(fileContent, fileLength, path, modTime, ordinal, opts, indirectDylib);
a645023d 960 break;
a645023d
A
961 }
962 return NULL;
963}
964
965
966}; // namespace dylib
967}; // namespace mach_o
968
969