1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2005-2006 Apple Computer, 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 "ObjectFile.h"
44 // To implement architecture xxx, you must write template specializations for the following method:
45 // Reader<xxx>::validFile()
57 template <typename A> class Reader;
60 class Segment : public ObjectFile::Segment
63 Segment(const char* name) { fName = name; }
64 virtual const char* getName() const { return fName; }
65 virtual bool isContentReadable() const { return true; }
66 virtual bool isContentWritable() const { return false; }
67 virtual bool isContentExecutable() const { return false; }
74 // An ExportAtom has no content. It exists so that the linker can track which imported
75 // symbols can from which dynamic libraries.
78 class ExportAtom : public ObjectFile::Atom
81 virtual ObjectFile::Reader* getFile() const { return &fOwner; }
82 virtual bool getTranslationUnitSource(const char** dir, const char** name) const { return false; }
83 virtual const char* getName() const { return fName; }
84 virtual const char* getDisplayName() const { return fName; }
85 virtual Scope getScope() const { return ObjectFile::Atom::scopeGlobal; }
86 virtual DefinitionKind getDefinitionKind() const { return fWeakDefinition ? kExternalWeakDefinition : kExternalDefinition; }
87 virtual SymbolTableInclusion getSymbolTableInclusion() const { return ObjectFile::Atom::kSymbolTableIn; }
88 virtual bool dontDeadStrip() const { return false; }
89 virtual bool isZeroFill() const { return false; }
90 virtual uint64_t getSize() const { return 0; }
91 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
92 virtual bool mustRemainInSection() const { return false; }
93 virtual const char* getSectionName() const { return "._imports"; }
94 virtual Segment& getSegment() const { return fgImportSegment; }
95 virtual bool requiresFollowOnAtom() const{ return false; }
96 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
97 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
98 virtual uint8_t getAlignment() const { return 0; }
99 virtual void copyRawContent(uint8_t buffer[]) const {}
101 virtual void setScope(Scope) { }
104 friend class Reader<A>;
105 typedef typename A::P P;
107 ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak)
108 : fOwner(owner), fName(name), fWeakDefinition(weak) {}
109 virtual ~ExportAtom() {}
111 ObjectFile::Reader& fOwner;
113 bool fWeakDefinition;
115 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
116 static Segment fgImportSegment;
119 template <typename A>
120 Segment ExportAtom<A>::fgImportSegment("__LINKEDIT");
122 template <typename A>
123 std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList;
127 // The reader for a dylib extracts all exported symbols names from the memory-mapped
128 // dylib, builds a hash table, then unmaps the file. This is an important memory
129 // savings for large dylibs.
131 template <typename A>
132 class Reader : public ObjectFile::Reader
135 static bool validFile(const uint8_t* fileContent, bool executableOrDylib);
136 static Reader<A>* make(const uint8_t* fileContent, uint64_t fileLength, const char* path,
137 bool executableOrDylib, const ObjectFile::ReaderOptions& options)
138 { return new Reader<A>(fileContent, fileLength, path, executableOrDylib, options); }
141 virtual const char* getPath() { return fPath; }
142 virtual time_t getModificationTime() { return 0; }
143 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
144 virtual std::vector<class ObjectFile::Atom*>& getAtoms();
145 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name);
146 virtual std::vector<Stab>* getStabs() { return NULL; }
147 virtual const char* getInstallPath() { return fDylibInstallPath; }
148 virtual uint32_t getTimestamp() { return fDylibTimeStamp; }
149 virtual uint32_t getCurrentVersion() { return fDylibtCurrentVersion; }
150 virtual uint32_t getCompatibilityVersion() { return fDylibCompatibilityVersion; }
151 virtual std::vector<const char*>* getDependentLibraryPaths();
152 virtual bool reExports(ObjectFile::Reader*);
153 virtual std::vector<const char*>* getAllowableClients();
156 const char* parentUmbrella() { return fParentUmbrella; }
159 typedef typename A::P P;
160 typedef typename A::P::E E;
165 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
167 struct AtomAndWeak { ObjectFile::Atom* atom; bool weak; };
168 typedef __gnu_cxx::hash_map<const char*, AtomAndWeak, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtomMap;
169 typedef typename NameToAtomMap::iterator NameToAtomMapIterator;
171 struct PathAndFlag { const char* path; bool reExport; };
173 Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path,
174 bool executableOrDylib, const ObjectFile::ReaderOptions& options);
177 const char* fParentUmbrella;
178 std::vector<const char*> fAllowableClients;
179 const char* fDylibInstallPath;
180 uint32_t fDylibTimeStamp;
181 uint32_t fDylibtCurrentVersion;
182 uint32_t fDylibCompatibilityVersion;
183 std::vector<PathAndFlag> fDependentLibraryPaths;
184 NameToAtomMap fAtoms;
186 static bool fgLogHashtable;
187 static std::vector<class ObjectFile::Atom*> fgEmptyAtomList;
190 template <typename A>
191 std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList;
192 template <typename A>
193 bool Reader<A>::fgLogHashtable = false;
196 template <typename A>
197 Reader<A>::Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, bool executableOrDylib, const ObjectFile::ReaderOptions& options)
198 : fParentUmbrella(NULL), fDylibInstallPath(NULL), fDylibTimeStamp(0), fDylibtCurrentVersion(0), fDylibCompatibilityVersion(0)
201 if ( ! validFile(fileContent, executableOrDylib) )
202 throw "not a valid mach-o object file";
204 fPath = strdup(path);
206 const macho_header<P>* header = (const macho_header<P>*)fileContent;
207 const uint32_t cmd_count = header->ncmds();
208 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
210 // a "blank" stub has zero load commands
211 if ( (header->filetype() == MH_DYLIB_STUB) && (cmd_count == 0) ) {
212 // no further processing needed
213 munmap((caddr_t)fileContent, fileLength);
217 // pass 1 builds list of all dependent libraries
218 const macho_load_command<P>* cmd = cmds;
219 for (uint32_t i = 0; i < cmd_count; ++i) {
220 switch (cmd->cmd()) {
222 case LC_LOAD_WEAK_DYLIB:
224 entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name());
225 entry.reExport = false;
226 fDependentLibraryPaths.push_back(entry);
229 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
232 // pass 2 determines re-export info
233 const macho_dysymtab_command<P>* dynamicInfo = NULL;
234 const macho_nlist<P>* symbolTable = NULL;
235 const char* strings = NULL;
237 for (uint32_t i = 0; i < cmd_count; ++i) {
238 switch (cmd->cmd()) {
241 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
242 symbolTable = (const macho_nlist<P>*)((char*)header + symtab->symoff());
243 strings = (char*)header + symtab->stroff();
247 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
250 macho_dylib_command<P>* dylibID = (macho_dylib_command<P>*)cmd;
251 fDylibInstallPath = strdup(dylibID->name());
252 fDylibTimeStamp = dylibID->timestamp();
253 fDylibtCurrentVersion = dylibID->current_version();
254 fDylibCompatibilityVersion = dylibID->compatibility_version();
256 case LC_SUB_UMBRELLA:
257 if ( !options.fFlatNamespace ) {
258 const char* frameworkLeafName = ((macho_sub_umbrella_command<P>*)cmd)->sub_umbrella();
259 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
260 const char* dylibName = it->path;
261 const char* lastSlash = strrchr(dylibName, '/');
262 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], frameworkLeafName) == 0) )
268 if ( !options.fFlatNamespace ) {
269 const char* dylibBaseName = ((macho_sub_library_command<P>*)cmd)->sub_library();
270 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
271 const char* dylibName = it->path;
272 const char* lastSlash = strrchr(dylibName, '/');
273 const char* leafStart = &lastSlash[1];
274 if ( lastSlash == NULL )
275 leafStart = dylibName;
276 const char* firstDot = strchr(leafStart, '.');
277 int len = strlen(leafStart);
278 if ( firstDot != NULL )
279 len = firstDot - leafStart;
280 if ( strncmp(leafStart, dylibBaseName, len) == 0 )
285 case LC_SUB_FRAMEWORK:
286 fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
290 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
292 // Process the rest of the commands here.
294 for (uint32_t i = 0; i < cmd_count; ++i) {
295 switch (cmd->cmd()) {
297 const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client());
299 fAllowableClients.push_back(temp);
303 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
306 // validate minimal load commands
307 if ( (fDylibInstallPath == NULL) && (header->filetype() != MH_EXECUTE) )
308 throw "dylib missing LC_ID_DYLIB load command";
309 if ( symbolTable == NULL )
310 throw "dylib missing LC_SYMTAB load command";
311 if ( dynamicInfo == NULL )
312 throw "dylib missing LC_DYSYMTAB load command";
315 if ( dynamicInfo->tocoff() == 0 ) {
316 if ( fgLogHashtable ) fprintf(stderr, "ld64: building hashtable of %u toc entries for %s\n", dynamicInfo->nextdefsym(), path);
317 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iextdefsym()];
318 const macho_nlist<P>* end = &start[dynamicInfo->nextdefsym()];
319 fAtoms.resize(dynamicInfo->nextdefsym()); // set initial bucket count
320 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
323 bucket.weak = ((sym->n_desc() & N_WEAK_DEF) != 0);
324 const char* name = strdup(&strings[sym->n_strx()]);
325 if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath());
326 fAtoms[name] = bucket;
330 int32_t count = dynamicInfo->ntoc();
331 fAtoms.resize(count); // set initial bucket count
332 if ( fgLogHashtable ) fprintf(stderr, "ld64: building hashtable of %u entries for %s\n", count, path);
333 const struct dylib_table_of_contents* toc = (dylib_table_of_contents*)((char*)header + dynamicInfo->tocoff());
334 for (int32_t i = 0; i < count; ++i) {
335 const uint32_t index = E::get32(toc[i].symbol_index);
336 const macho_nlist<P>* sym = &symbolTable[index];
339 bucket.weak = ((sym->n_desc() & N_WEAK_DEF) != 0);
340 const char* name = strdup(&strings[sym->n_strx()]);
341 if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath());
342 fAtoms[name] = bucket;
347 munmap((caddr_t)fileContent, fileLength);
350 template <typename A>
351 std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms()
353 // TO DO: for flat-namespace libraries, when linking flat_namespace
354 // we need to create an atom which references all undefines
355 return fgEmptyAtomList;
359 template <typename A>
360 std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name)
362 std::vector<class ObjectFile::Atom*>* atoms = NULL;
364 NameToAtomMapIterator pos = fAtoms.find(name);
365 if ( pos != fAtoms.end() ) {
366 if ( pos->second.atom == NULL ) {
367 // instantiate atom and update hash table
368 pos->second.atom = new ExportAtom<A>(*this, name, pos->second.weak);
369 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->getPath());
371 // return a vector of one atom
372 atoms = new std::vector<class ObjectFile::Atom*>;
373 atoms->push_back(pos->second.atom);
376 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath());
383 template <typename A>
384 std::vector<const char*>* Reader<A>::getDependentLibraryPaths()
386 std::vector<const char*>* result = new std::vector<const char*>;
387 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
388 result->push_back(it->path);
393 template <typename A>
394 std::vector<const char*>* Reader<A>::getAllowableClients()
396 std::vector<const char*>* result = new std::vector<const char*>;
397 for (typename std::vector<const char*>::iterator it = fAllowableClients.begin();
398 it != fAllowableClients.end();
400 result->push_back(*it);
402 return (fAllowableClients.size() != 0 ? result : NULL);
405 template <typename A>
406 bool Reader<A>::reExports(ObjectFile::Reader* child)
408 // A dependent dylib is re-exported under two conditions:
409 // 1) parent contains LC_SUB_UMBRELLA or LC_SUB_LIBRARY with child name
410 const char* childInstallPath = child->getInstallPath();
411 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
412 if ( it->reExport && ((strcmp(it->path, child->getPath()) == 0) || ((childInstallPath!=NULL) && (strcmp(it->path, childInstallPath)==0))) )
416 // 2) child contains LC_SUB_FRAMEWORK with parent name
417 const char* parentUmbrellaName = ((Reader<A>*)child)->parentUmbrella();
418 if ( parentUmbrellaName != NULL ) {
419 const char* parentName = this->getPath();
420 const char* lastSlash = strrchr(parentName, '/');
421 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) )
429 bool Reader<ppc>::validFile(const uint8_t* fileContent, bool executableOrDylib)
431 const macho_header<P>* header = (const macho_header<P>*)fileContent;
432 if ( header->magic() != MH_MAGIC )
434 if ( header->cputype() != CPU_TYPE_POWERPC )
436 switch ( header->filetype() ) {
441 return executableOrDylib;
448 bool Reader<ppc64>::validFile(const uint8_t* fileContent, bool executableOrDylib)
450 const macho_header<P>* header = (const macho_header<P>*)fileContent;
451 if ( header->magic() != MH_MAGIC_64 )
453 if ( header->cputype() != CPU_TYPE_POWERPC64 )
455 switch ( header->filetype() ) {
460 return executableOrDylib;
467 bool Reader<x86>::validFile(const uint8_t* fileContent, bool executableOrDylib)
469 const macho_header<P>* header = (const macho_header<P>*)fileContent;
470 if ( header->magic() != MH_MAGIC )
472 if ( header->cputype() != CPU_TYPE_I386 )
474 switch ( header->filetype() ) {
479 return executableOrDylib;
486 bool Reader<x86_64>::validFile(const uint8_t* fileContent, bool executableOrDylib)
488 const macho_header<P>* header = (const macho_header<P>*)fileContent;
489 if ( header->magic() != MH_MAGIC_64 )
491 if ( header->cputype() != CPU_TYPE_X86_64 )
493 switch ( header->filetype() ) {
498 return executableOrDylib;
506 }; // namespace dylib
507 }; // namespace mach_o
510 #endif // __OBJECT_FILE_DYLIB_MACH_O__