1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2007 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
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
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.
22 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __OBJECT_FILE_DYLIB_MACH_O__
26 #define __OBJECT_FILE_DYLIB_MACH_O__
31 #include <sys/param.h>
37 #include <ext/hash_map>
39 #include "MachOFileAbstraction.hpp"
40 #include "MachOTrie.hpp"
41 #include "ObjectFile.h"
45 // To implement architecture xxx, you must write template specializations for the following method:
46 // Reader<xxx>::validFile()
58 template <typename A> class Reader;
61 class Segment : public ObjectFile::Segment
64 Segment(const char* name) { fName = name; }
65 virtual const char* getName() const { return fName; }
66 virtual bool isContentReadable() const { return true; }
67 virtual bool isContentWritable() const { return false; }
68 virtual bool isContentExecutable() const { return false; }
75 // An ExportAtom has no content. It exists so that the linker can track which imported
76 // symbols came from which dynamic libraries.
79 class ExportAtom : public ObjectFile::Atom
82 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
83 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
84 virtual const char* getName() const { return fName; }
85 virtual const char* getDisplayName() const { return fName; }
86 virtual Scope getScope() const { return ObjectFile::Atom::scopeGlobal; }
87 virtual DefinitionKind getDefinitionKind() const { return fWeakDefinition ? kExternalWeakDefinition : kExternalDefinition; }
88 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableIn; }
89 virtual bool dontDeadStrip() const { return false; }
90 virtual bool isZeroFill() const { return false; }
91 virtual bool isThumb() const { return false; }
92 virtual uint64_t getSize() const { return 0; }
93 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
94 virtual bool mustRemainInSection() const { return false; }
95 virtual const char* getSectionName() const { return "._imports"; }
96 virtual Segment& getSegment() const { return fgImportSegment; }
97 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
98 virtual uint32_t getOrdinal() const { return fOrdinal; }
99 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
100 virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(0); }
101 virtual void copyRawContent(uint8_t buffer[]) const {}
103 virtual void setScope(Scope) { }
106 friend class Reader<A>;
107 typedef typename A::P P;
109 ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak, uint32_t ordinal)
110 : fOwner(owner), fName(name), fOrdinal(ordinal), fWeakDefinition(weak) {}
111 virtual ~ExportAtom() {}
113 ObjectFile::Reader& fOwner;
116 bool fWeakDefinition;
118 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
119 static Segment fgImportSegment;
122 template <typename A>
123 Segment ExportAtom<A>::fgImportSegment("__LINKEDIT");
125 template <typename A>
126 std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList;
130 class ImportReference : public ObjectFile::Reference
133 ImportReference(const char* name)
134 : fTarget(NULL), fTargetName(strdup(name)) {}
135 virtual ~ImportReference() {}
138 virtual ObjectFile::Reference::TargetBinding getTargetBinding() const { return (fTarget==NULL) ? ObjectFile::Reference::kUnboundByName : ObjectFile::Reference::kBoundByName; }
139 virtual ObjectFile::Reference::TargetBinding getFromTargetBinding() const{ return ObjectFile::Reference::kDontBind; }
140 virtual uint8_t getKind() const { return 0; }
141 virtual uint64_t getFixUpOffset() const { return 0; }
142 virtual const char* getTargetName() const { return fTargetName; }
143 virtual ObjectFile::Atom& getTarget() const { return *((ObjectFile::Atom*)fTarget); }
144 virtual uint64_t getTargetOffset() const { return 0; }
145 virtual ObjectFile::Atom& getFromTarget() const { return *((ObjectFile::Atom*)NULL); }
146 virtual const char* getFromTargetName() const { return NULL; }
147 virtual uint64_t getFromTargetOffset() const { return 0; }
148 virtual void setTarget(ObjectFile::Atom& atom, uint64_t offset) { fTarget = &atom; }
149 virtual void setFromTarget(ObjectFile::Atom&) { throw "can't set from target"; }
150 virtual const char* getDescription() const { return "dylib import reference"; }
153 const ObjectFile::Atom* fTarget;
154 const char* fTargetName;
159 // An ImportAtom has no content. It exists so that when linking a main executable flat-namespace
160 // the imports of all flat dylibs are checked
162 template <typename A>
163 class ImportAtom : public ObjectFile::Atom
166 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
167 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
168 virtual const char* getName() const { return "flat-imports"; }
169 virtual const char* getDisplayName() const { return "flat_namespace undefines"; }
170 virtual Scope getScope() const { return ObjectFile::Atom::scopeTranslationUnit; }
171 virtual DefinitionKind getDefinitionKind() const { return kRegularDefinition; }
172 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableNotIn; }
173 virtual bool dontDeadStrip() const { return false; }
174 virtual bool isZeroFill() const { return false; }
175 virtual bool isThumb() const { return false; }
176 virtual uint64_t getSize() const { return 0; }
177 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return (std::vector<ObjectFile::Reference*>&)(fReferences); }
178 virtual bool mustRemainInSection() const { return false; }
179 virtual const char* getSectionName() const { return "._imports"; }
180 virtual Segment& getSegment() const { return fgImportSegment; }
181 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
182 virtual uint32_t getOrdinal() const { return fOrdinal; }
183 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
184 virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(0); }
185 virtual void copyRawContent(uint8_t buffer[]) const {}
187 virtual void setScope(Scope) { }
190 friend class Reader<A>;
191 typedef typename A::P P;
193 ImportAtom(ObjectFile::Reader& owner, uint32_t ordinal, std::vector<const char*>& imports)
194 : fOwner(owner), fOrdinal(ordinal) { makeReferences(imports); }
195 virtual ~ImportAtom() {}
196 void makeReferences(std::vector<const char*>& imports) {
197 for (std::vector<const char*>::iterator it=imports.begin(); it != imports.end(); ++it) {
198 fReferences.push_back(new ImportReference(*it));
203 ObjectFile::Reader& fOwner;
205 std::vector<ObjectFile::Reference*> fReferences;
207 static Segment fgImportSegment;
210 template <typename A>
211 Segment ImportAtom<A>::fgImportSegment("__LINKEDIT");
217 // The reader for a dylib extracts all exported symbols names from the memory-mapped
218 // dylib, builds a hash table, then unmaps the file. This is an important memory
219 // savings for large dylibs.
221 template <typename A>
222 class Reader : public ObjectFile::Reader
225 static bool validFile(const uint8_t* fileContent, bool executableOrDylib);
226 Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path,
227 const LibraryOptions& dylibOptions, const ObjectFile::ReaderOptions& options,
228 uint32_t ordinalBase);
231 virtual const char* getPath() { return fPath; }
232 virtual time_t getModificationTime() { return 0; }
233 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
234 virtual std::vector<class ObjectFile::Atom*>& getAtoms();
235 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name);
236 virtual std::vector<Stab>* getStabs() { return NULL; }
237 virtual ObjectFile::Reader::ObjcConstraint getObjCConstraint() { return fObjcContraint; }
238 virtual const char* getInstallPath() { return fDylibInstallPath; }
239 virtual uint32_t getTimestamp() { return fDylibTimeStamp; }
240 virtual uint32_t getCurrentVersion() { return fDylibtCurrentVersion; }
241 virtual uint32_t getCompatibilityVersion() { return fDylibCompatibilityVersion; }
242 virtual void processIndirectLibraries(DylibHander* handler);
243 virtual void setExplicitlyLinked() { fExplicitlyLinked = true; }
244 virtual bool explicitlyLinked() { return fExplicitlyLinked; }
245 virtual bool implicitlyLinked() { return fImplicitlyLinked; }
246 virtual bool providedExportAtom() { return fProvidedAtom; }
247 virtual const char* parentUmbrella() { return fParentUmbrella; }
248 virtual std::vector<const char*>* getAllowableClients();
249 virtual bool hasWeakExternals() { return fHasWeakExports; }
250 virtual bool deadStrippable() { return fDeadStrippable; }
251 virtual bool isLazyLoadedDylib() { return fLazyLoaded; }
253 virtual void setImplicitlyLinked() { fImplicitlyLinked = true; }
257 struct ReExportChain { ReExportChain* prev; Reader<A>* reader; };
259 void assertNoReExportCycles(ReExportChain*);
262 typedef typename A::P P;
263 typedef typename A::P::E E;
268 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
270 struct AtomAndWeak { ObjectFile::Atom* atom; bool weak; uint32_t ordinal; };
271 typedef __gnu_cxx::hash_map<const char*, AtomAndWeak, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtomMap;
272 typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet;
273 typedef typename NameToAtomMap::iterator NameToAtomMapIterator;
275 struct PathAndFlag { const char* path; bool reExport; };
277 bool isPublicLocation(const char* path);
278 void addSymbol(const char* name, bool weak);
279 void addDyldFastStub();
280 void buildExportHashTableFromExportInfo(const macho_dyld_info_command<P>* dyldInfo,
281 const uint8_t* fileContent);
282 void buildExportHashTableFromSymbolTable(const macho_dysymtab_command<P>* dynamicInfo,
283 const macho_nlist<P>* symbolTable, const char* strings,
284 const uint8_t* fileContent);
287 const char* fParentUmbrella;
288 std::vector<const char*> fAllowableClients;
289 const char* fDylibInstallPath;
290 uint32_t fDylibTimeStamp;
291 uint32_t fDylibtCurrentVersion;
292 uint32_t fDylibCompatibilityVersion;
293 uint32_t fReExportedOrdinal;
294 std::vector<PathAndFlag> fDependentLibraryPaths;
295 NameToAtomMap fAtoms;
296 NameSet fIgnoreExports;
298 bool fHasWeakExports;
299 bool fDeadStrippable;
300 const bool fLinkingFlat;
301 const bool fLinkingMainExecutable;
302 bool fExplictReExportFound;
303 bool fExplicitlyLinked;
304 bool fImplicitlyLinked;
306 bool fImplicitlyLinkPublicDylibs;
308 ObjectFile::Reader::ObjcConstraint fObjcContraint;
309 std::vector<ObjectFile::Reader*> fReExportedChildren;
310 const ObjectFile::ReaderOptions::MacVersionMin fDeploymentVersionMin;
311 std::vector<class ObjectFile::Atom*> fFlatImports;
313 static bool fgLogHashtable;
314 static std::vector<class ObjectFile::Atom*> fgEmptyAtomList;
317 template <typename A>
318 std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList;
319 template <typename A>
320 bool Reader<A>::fgLogHashtable = false;
323 template <typename A>
324 Reader<A>::Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path,
325 const LibraryOptions& dylibOptions,
326 const ObjectFile::ReaderOptions& options, uint32_t ordinalBase)
327 : fParentUmbrella(NULL), fDylibInstallPath(NULL), fDylibTimeStamp(0), fDylibtCurrentVersion(0),
328 fDylibCompatibilityVersion(0), fReExportedOrdinal(ordinalBase), fLinkingFlat(options.fFlatNamespace),
329 fLinkingMainExecutable(options.fLinkingMainExecutable), fExplictReExportFound(false),
330 fExplicitlyLinked(false), fImplicitlyLinked(false), fProvidedAtom(false),
331 fImplicitlyLinkPublicDylibs(options.fImplicitlyLinkPublicDylibs), fLazyLoaded(dylibOptions.fLazyLoad),
332 fObjcContraint(ObjectFile::Reader::kObjcNone),
333 fDeploymentVersionMin(options.fMacVersionMin)
336 if ( ! validFile(fileContent, dylibOptions.fBundleLoader) )
337 throw "not a valid mach-o object file";
339 fPath = strdup(path);
341 const macho_header<P>* header = (const macho_header<P>*)fileContent;
342 const uint32_t cmd_count = header->ncmds();
343 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
344 const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds());
346 // write out path for -whatsloaded option
347 if ( options.fLogAllFiles )
348 printf("%s\n", path);
350 if ( options.fRootSafe && ((header->flags() & MH_ROOT_SAFE) == 0) )
351 warning("using -root_safe but linking against %s which is not root safe", path);
353 if ( options.fSetuidSafe && ((header->flags() & MH_SETUID_SAFE) == 0) )
354 warning("using -setuid_safe but linking against %s which is not setuid safe", path);
356 // a "blank" stub has zero load commands
357 if ( (header->filetype() == MH_DYLIB_STUB) && (cmd_count == 0) ) {
358 // no further processing needed
359 munmap((caddr_t)fileContent, fileLength);
364 // optimize the case where we know there is no reason to look at indirect dylibs
365 fNoRexports = (header->flags() & MH_NO_REEXPORTED_DYLIBS);
366 fHasWeakExports = (header->flags() & MH_WEAK_DEFINES);
367 fDeadStrippable = (header->flags() & MH_DEAD_STRIPPABLE_DYLIB);
368 bool trackDependentLibraries = !fNoRexports || options.fFlatNamespace;
370 // pass 1 builds list of all dependent libraries
371 const macho_load_command<P>* cmd = cmds;
372 if ( trackDependentLibraries ) {
373 for (uint32_t i = 0; i < cmd_count; ++i) {
374 switch (cmd->cmd()) {
375 case LC_REEXPORT_DYLIB:
376 fExplictReExportFound = true;
377 // fall into next case
379 case LC_LOAD_WEAK_DYLIB:
381 entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name());
382 entry.reExport = (cmd->cmd() == LC_REEXPORT_DYLIB);
383 fDependentLibraryPaths.push_back(entry);
386 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
388 throwf("malformed dylb, load command #%d is outside size of load commands in %s", i, path);
392 // pass 2 determines re-export info
393 const macho_dysymtab_command<P>* dynamicInfo = NULL;
394 const macho_dyld_info_command<P>* dyldInfo = NULL;
395 const macho_nlist<P>* symbolTable = NULL;
396 const char* strings = NULL;
398 for (uint32_t i = 0; i < cmd_count; ++i) {
399 switch (cmd->cmd()) {
402 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
403 symbolTable = (const macho_nlist<P>*)((char*)header + symtab->symoff());
404 strings = (char*)header + symtab->stroff();
408 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
411 case LC_DYLD_INFO_ONLY:
412 dyldInfo = (macho_dyld_info_command<P>*)cmd;
416 macho_dylib_command<P>* dylibID = (macho_dylib_command<P>*)cmd;
417 fDylibInstallPath = strdup(dylibID->name());
418 fDylibTimeStamp = dylibID->timestamp();
419 fDylibtCurrentVersion = dylibID->current_version();
420 fDylibCompatibilityVersion = dylibID->compatibility_version();
423 case LC_SUB_UMBRELLA:
424 if ( trackDependentLibraries ) {
425 const char* frameworkLeafName = ((macho_sub_umbrella_command<P>*)cmd)->sub_umbrella();
426 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
427 const char* dylibName = it->path;
428 const char* lastSlash = strrchr(dylibName, '/');
429 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], frameworkLeafName) == 0) )
435 if ( trackDependentLibraries) {
436 const char* dylibBaseName = ((macho_sub_library_command<P>*)cmd)->sub_library();
437 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
438 const char* dylibName = it->path;
439 const char* lastSlash = strrchr(dylibName, '/');
440 const char* leafStart = &lastSlash[1];
441 if ( lastSlash == NULL )
442 leafStart = dylibName;
443 const char* firstDot = strchr(leafStart, '.');
444 int len = strlen(leafStart);
445 if ( firstDot != NULL )
446 len = firstDot - leafStart;
447 if ( strncmp(leafStart, dylibBaseName, len) == 0 )
452 case LC_SUB_FRAMEWORK:
453 fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
455 case macho_segment_command<P>::CMD:
456 // check for Objective-C info
457 if ( strcmp(((macho_segment_command<P>*)cmd)->segname(), "__OBJC") == 0 ) {
458 const macho_segment_command<P>* segment = (macho_segment_command<P>*)cmd;
459 const macho_section<P>* const sectionsStart = (macho_section<P>*)((char*)segment + sizeof(macho_segment_command<P>));
460 const macho_section<P>* const sectionsEnd = §ionsStart[segment->nsects()];
461 for (const macho_section<P>* sect=sectionsStart; sect < sectionsEnd; ++sect) {
462 if ( strcmp(sect->sectname(), "__image_info") == 0 ) {
463 // struct objc_image_info {
464 // uint32_t version; // initially 0
467 // #define OBJC_IMAGE_SUPPORTS_GC 2
468 // #define OBJC_IMAGE_GC_ONLY 4
470 const uint32_t* contents = (uint32_t*)(&fileContent[sect->offset()]);
471 if ( (sect->size() >= 8) && (contents[0] == 0) ) {
472 uint32_t flags = E::get32(contents[1]);
473 if ( (flags & 4) == 4 )
474 fObjcContraint = ObjectFile::Reader::kObjcGC;
475 else if ( (flags & 2) == 2 )
476 fObjcContraint = ObjectFile::Reader::kObjcRetainReleaseOrGC;
478 fObjcContraint = ObjectFile::Reader::kObjcRetainRelease;
480 else if ( sect->size() > 0 ) {
481 warning("can't parse __OBJC/__image_info section in %s", fPath);
488 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
490 throwf("malformed dylb, load command #%d is outside size of load commands in %s", i, path);
493 // Process the rest of the commands here.
495 for (uint32_t i = 0; i < cmd_count; ++i) {
496 switch (cmd->cmd()) {
498 const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client());
499 fAllowableClients.push_back(temp);
502 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
505 // validate minimal load commands
506 if ( (fDylibInstallPath == NULL) && ((header->filetype() == MH_DYLIB) || (header->filetype() == MH_DYLIB_STUB)) )
507 throwf("dylib %s missing LC_ID_DYLIB load command", path);
508 if ( symbolTable == NULL )
509 throw "binary missing LC_SYMTAB load command";
510 if ( dynamicInfo == NULL )
511 throw "binary missing LC_DYSYMTAB load command";
513 // if linking flat and this is a flat dylib, create one atom that references all imported symbols
514 if ( fLinkingFlat && fLinkingMainExecutable && ((header->flags() & MH_TWOLEVEL) == 0) ) {
515 std::vector<const char*> importNames;
516 importNames.reserve(dynamicInfo->nundefsym());
517 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iundefsym()];
518 const macho_nlist<P>* end = &start[dynamicInfo->nundefsym()];
519 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
520 importNames.push_back(&strings[sym->n_strx()]);
522 fFlatImports.push_back(new ImportAtom<A>(*this, fReExportedOrdinal++, importNames));
526 if ( dyldInfo != NULL )
527 buildExportHashTableFromExportInfo(dyldInfo, fileContent);
529 buildExportHashTableFromSymbolTable(dynamicInfo, symbolTable, strings, fileContent);
531 // special case libSystem
532 if ( (fDylibInstallPath != NULL) && (strcmp(fDylibInstallPath, "/usr/lib/libSystem.B.dylib") == 0) )
536 munmap((caddr_t)fileContent, fileLength);
540 template <typename A>
541 void Reader<A>::buildExportHashTableFromSymbolTable(const macho_dysymtab_command<P>* dynamicInfo,
542 const macho_nlist<P>* symbolTable, const char* strings,
543 const uint8_t* fileContent)
545 if ( dynamicInfo->tocoff() == 0 ) {
546 if ( fgLogHashtable ) fprintf(stderr, "ld: building hashtable of %u toc entries for %s\n", dynamicInfo->nextdefsym(), this->getPath());
547 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iextdefsym()];
548 const macho_nlist<P>* end = &start[dynamicInfo->nextdefsym()];
549 fAtoms.resize(dynamicInfo->nextdefsym()); // set initial bucket count
550 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
551 this->addSymbol(&strings[sym->n_strx()], (sym->n_desc() & N_WEAK_DEF) != 0);
555 int32_t count = dynamicInfo->ntoc();
556 fAtoms.resize(count); // set initial bucket count
557 if ( fgLogHashtable ) fprintf(stderr, "ld: building hashtable of %u entries for %s\n", count, this->getPath());
558 const struct dylib_table_of_contents* toc = (dylib_table_of_contents*)(fileContent + dynamicInfo->tocoff());
559 for (int32_t i = 0; i < count; ++i) {
560 const uint32_t index = E::get32(toc[i].symbol_index);
561 const macho_nlist<P>* sym = &symbolTable[index];
562 this->addSymbol(&strings[sym->n_strx()], (sym->n_desc() & N_WEAK_DEF) != 0);
568 template <typename A>
569 void Reader<A>::buildExportHashTableFromExportInfo(const macho_dyld_info_command<P>* dyldInfo,
570 const uint8_t* fileContent)
572 if ( fgLogHashtable ) fprintf(stderr, "ld: building hashtable from export info in %s\n", this->getPath());
573 if ( dyldInfo->export_size() > 0 ) {
574 const uint8_t* start = fileContent + dyldInfo->export_off();
575 const uint8_t* end = &start[dyldInfo->export_size()];
576 std::vector<mach_o::trie::Entry> list;
577 parseTrie(start, end, list);
578 for (std::vector<mach_o::trie::Entry>::iterator it=list.begin(); it != list.end(); ++it)
579 this->addSymbol(it->name, it->flags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION);
585 void Reader<x86_64>::addDyldFastStub()
587 addSymbol("dyld_stub_binder", false);
591 void Reader<x86>::addDyldFastStub()
593 addSymbol("dyld_stub_binder", false);
596 template <typename A>
597 void Reader<A>::addDyldFastStub()
602 template <typename A>
603 void Reader<A>::addSymbol(const char* name, bool weakDef)
605 //fprintf(stderr, "addSymbol() %s\n", name);
606 // symbols that start with $ld$ are meta-data to the static linker
607 // <rdar://problem/5182537> need way for ld and dyld to see different exported symbols in a dylib
608 if ( strncmp(name, "$ld$", 4) == 0 ) {
609 // $ld$ <action> $ <condition> $ <symbol-name>
610 const char* symAction = &name[4];
611 const char* symCond = strchr(symAction, '$');
612 if ( symCond != NULL ) {
613 ObjectFile::ReaderOptions::MacVersionMin symVersionCondition = ObjectFile::ReaderOptions::kMinMacVersionUnset;
614 if ( (strncmp(symCond, "$os10.", 6) == 0) && isdigit(symCond[6]) && (symCond[7] == '$') ) {
615 switch ( symCond[6] - '0' ) {
618 symVersionCondition = ObjectFile::ReaderOptions::k10_1;
621 symVersionCondition = ObjectFile::ReaderOptions::k10_2;
624 symVersionCondition = ObjectFile::ReaderOptions::k10_3;
627 symVersionCondition = ObjectFile::ReaderOptions::k10_4;
630 symVersionCondition = ObjectFile::ReaderOptions::k10_5;
633 symVersionCondition = ObjectFile::ReaderOptions::k10_6;
636 const char* symName = strchr(&symCond[1], '$');
637 if ( symName != NULL ) {
639 if ( fDeploymentVersionMin == symVersionCondition ) {
640 if ( strncmp(symAction, "hide$", 5) == 0 ) {
641 if ( fgLogHashtable ) fprintf(stderr, " adding %s to ignore set for %s\n", symName, this->getPath());
642 fIgnoreExports.insert(strdup(symName));
645 else if ( strncmp(symAction, "add$", 4) == 0 ) {
646 this->addSymbol(symName, weakDef);
650 warning("bad symbol action: %s in dylib %s", name, this->getPath());
655 warning("bad symbol name: %s in dylib %s", name, this->getPath());
659 warning("bad symbol version: %s in dylib %s", name, this->getPath());
663 warning("bad symbol condition: %s in dylib %s", name, this->getPath());
667 // add symbol as possible export if we are not supposed to ignore it
668 if ( fIgnoreExports.count(name) == 0 ) {
671 bucket.weak = weakDef;
672 bucket.ordinal = fReExportedOrdinal++;
673 if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath());
674 fAtoms[strdup(name)] = bucket;
679 template <typename A>
680 std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms()
686 template <typename A>
687 std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name)
689 // if supposed to ignore this export, then pretend I don't have it
690 if ( fIgnoreExports.count(name) != 0 )
693 std::vector<class ObjectFile::Atom*>* atoms = NULL;
694 NameToAtomMapIterator pos = fAtoms.find(name);
695 if ( pos != fAtoms.end() ) {
696 if ( pos->second.atom == NULL ) {
697 // instantiate atom and update hash table
698 pos->second.atom = new ExportAtom<A>(*this, name, pos->second.weak, pos->second.ordinal);
699 fProvidedAtom = true;
700 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->getPath());
702 // return a vector of one atom
703 atoms = new std::vector<class ObjectFile::Atom*>;
704 atoms->push_back(pos->second.atom);
707 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath());
708 // look in children that I re-export
709 for (std::vector<ObjectFile::Reader*>::iterator it = fReExportedChildren.begin(); it != fReExportedChildren.end(); it++) {
710 //fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s, looking in child %s\n", name, this->getPath(), (*it)->getInstallPath());
711 std::vector<class ObjectFile::Atom*>* childAtoms = (*it)->getJustInTimeAtomsFor(name);
712 if ( childAtoms != NULL ) {
713 // make a new atom that says this reader is the owner
714 bool isWeakDef = (childAtoms->at(0)->getDefinitionKind() == ObjectFile::Atom::kExternalWeakDefinition);
715 // return a vector of one atom
716 ExportAtom<A>* newAtom = new ExportAtom<A>(*this, name, isWeakDef, fReExportedOrdinal++);
717 fProvidedAtom = true;
718 atoms = new std::vector<class ObjectFile::Atom*>;
719 atoms->push_back(newAtom);
730 template <typename A>
731 bool Reader<A>::isPublicLocation(const char* path)
733 // -no_implicit_dylibs disables this optimization
734 if ( ! fImplicitlyLinkPublicDylibs )
737 // /usr/lib is a public location
738 if ( (strncmp(path, "/usr/lib/", 9) == 0) && (strchr(&path[9], '/') == NULL) )
741 // /System/Library/Frameworks/ is a public location
742 if ( strncmp(path, "/System/Library/Frameworks/", 27) == 0 ) {
743 const char* frameworkDot = strchr(&path[27], '.');
744 // but only top level framework
745 // /System/Library/Frameworks/Foo.framework/Versions/A/Foo ==> true
746 // /System/Library/Frameworks/Foo.framework/Resources/libBar.dylib ==> false
747 // /System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Bar ==> false
748 // /System/Library/Frameworks/Foo.framework/Frameworks/Xfoo.framework/XFoo ==> false
749 if ( frameworkDot != NULL ) {
750 int frameworkNameLen = frameworkDot - &path[27];
751 if ( strncmp(&path[strlen(path)-frameworkNameLen-1], &path[26], frameworkNameLen+1) == 0 )
759 template <typename A>
760 void Reader<A>::processIndirectLibraries(DylibHander* handler)
762 if ( fLinkingFlat ) {
763 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
764 handler->findDylib(it->path, this->getPath());
767 else if ( fNoRexports ) {
768 // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do
771 // two-level, might have re-exports
772 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
773 if ( it->reExport ) {
774 //fprintf(stderr, "processIndirectLibraries() parent=%s, child=%s\n", this->getInstallPath(), it->path);
775 // a LC_REEXPORT_DYLIB, LC_SUB_UMBRELLA or LC_SUB_LIBRARY says we re-export this child
776 ObjectFile::Reader* child = handler->findDylib(it->path, this->getPath());
777 if ( isPublicLocation(child->getInstallPath()) ) {
778 // promote this child to be automatically added as a direct dependent if this already is
779 if ( this->explicitlyLinked() || this->implicitlyLinked() ) {
780 //fprintf(stderr, "processIndirectLibraries() implicitly linking %s\n", child->getInstallPath());
781 ((Reader<A>*)child)->setImplicitlyLinked();
783 else if ( child->explicitlyLinked() || child->implicitlyLinked() ) {
784 //fprintf(stderr, "processIndirectLibraries() parent is not directly linked, but child is, so no need to re-export child\n");
787 fReExportedChildren.push_back(child);
788 //fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->getInstallPath(), it->path);
792 // add all child's symbols to me
793 fReExportedChildren.push_back(child);
794 //fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->getInstallPath(), it->path);
797 else if ( !fExplictReExportFound ) {
798 // see if child contains LC_SUB_FRAMEWORK with my name
799 ObjectFile::Reader* child = handler->findDylib(it->path, this->getPath());
800 const char* parentUmbrellaName = ((Reader<A>*)child)->parentUmbrella();
801 if ( parentUmbrellaName != NULL ) {
802 const char* parentName = this->getPath();
803 const char* lastSlash = strrchr(parentName, '/');
804 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) ) {
805 // add all child's symbols to me
806 fReExportedChildren.push_back(child);
807 //fprintf(stderr, "processIndirectLibraries() umbrella=%s will re-export child=%s\n", this->getInstallPath(), it->path);
814 // check for re-export cycles
818 this->assertNoReExportCycles(&chain);
821 template <typename A>
822 void Reader<A>::assertNoReExportCycles(ReExportChain* prev)
824 // recursively check my re-exported dylibs
828 for (std::vector<ObjectFile::Reader*>::iterator it = fReExportedChildren.begin(); it != fReExportedChildren.end(); it++) {
829 ObjectFile::Reader* child = *it;
830 // check child is not already in chain
831 for (ReExportChain* p = prev; p != NULL; p = p->prev) {
832 if ( p->reader == child ) {
833 throwf("cycle in dylib re-exports with %s", child->getPath());
836 ((Reader<A>*)(*it))->assertNoReExportCycles(&chain);
841 template <typename A>
842 std::vector<const char*>* Reader<A>::getAllowableClients()
844 std::vector<const char*>* result = new std::vector<const char*>;
845 for (typename std::vector<const char*>::iterator it = fAllowableClients.begin();
846 it != fAllowableClients.end();
848 result->push_back(*it);
850 return (fAllowableClients.size() != 0 ? result : NULL);
854 bool Reader<ppc>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
856 const macho_header<P>* header = (const macho_header<P>*)fileContent;
857 if ( header->magic() != MH_MAGIC )
859 if ( header->cputype() != CPU_TYPE_POWERPC )
861 switch ( header->filetype() ) {
866 if ( executableOrDyliborBundle )
869 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
871 if ( executableOrDyliborBundle )
874 throw "can't link with a main executable";
881 bool Reader<ppc64>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
883 const macho_header<P>* header = (const macho_header<P>*)fileContent;
884 if ( header->magic() != MH_MAGIC_64 )
886 if ( header->cputype() != CPU_TYPE_POWERPC64 )
888 switch ( header->filetype() ) {
893 if ( executableOrDyliborBundle )
896 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
898 if ( executableOrDyliborBundle )
901 throw "can't link with a main executable";
908 bool Reader<x86>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
910 const macho_header<P>* header = (const macho_header<P>*)fileContent;
911 if ( header->magic() != MH_MAGIC )
913 if ( header->cputype() != CPU_TYPE_I386 )
915 switch ( header->filetype() ) {
920 if ( executableOrDyliborBundle )
923 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
925 if ( executableOrDyliborBundle )
928 throw "can't link with a main executable";
935 bool Reader<x86_64>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
937 const macho_header<P>* header = (const macho_header<P>*)fileContent;
938 if ( header->magic() != MH_MAGIC_64 )
940 if ( header->cputype() != CPU_TYPE_X86_64 )
942 switch ( header->filetype() ) {
947 if ( executableOrDyliborBundle )
950 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
952 if ( executableOrDyliborBundle )
955 throw "can't link with a main executable";
962 bool Reader<arm>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle)
964 const macho_header<P>* header = (const macho_header<P>*)fileContent;
965 if ( header->magic() != MH_MAGIC )
967 if ( header->cputype() != CPU_TYPE_ARM )
969 switch ( header->filetype() ) {
974 if ( executableOrDyliborBundle )
977 throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)";
979 if ( executableOrDyliborBundle )
982 throw "can't link with a main executable";
988 }; // namespace dylib
989 }; // namespace mach_o
992 #endif // __OBJECT_FILE_DYLIB_MACH_O__