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 isZeroFill() const { return false; }
89 virtual uint64_t getSize() const { return 0; }
90 virtual std::vector<ObjectFile::Reference*>& getReferences() const { return fgEmptyReferenceList; }
91 virtual bool mustRemainInSection() const { return false; }
92 virtual const char* getSectionName() const { return "._imports"; }
93 virtual Segment& getSegment() const { return fgImportSegment; }
94 virtual bool requiresFollowOnAtom() const{ return false; }
95 virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); }
96 virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; }
97 virtual uint8_t getAlignment() const { return 0; }
98 virtual void copyRawContent(uint8_t buffer[]) const {}
100 virtual void setScope(Scope) { }
103 friend class Reader<A>;
104 typedef typename A::P P;
106 ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak)
107 : fOwner(owner), fName(name), fWeakDefinition(weak) {}
108 virtual ~ExportAtom() {}
110 ObjectFile::Reader& fOwner;
112 bool fWeakDefinition;
114 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
115 static Segment fgImportSegment;
118 template <typename A>
119 Segment ExportAtom<A>::fgImportSegment("__LINKEDIT");
121 template <typename A>
122 std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList;
126 // The reader for a dylib extracts all exported symbols names from the memory-mapped
127 // dylib, builds a hash table, then unmaps the file. This is an important memory
128 // savings for large dylibs.
130 template <typename A>
131 class Reader : public ObjectFile::Reader
134 static bool validFile(const uint8_t* fileContent);
135 static Reader<A>* make(const uint8_t* fileContent, uint64_t fileLength, const char* path, const ObjectFile::ReaderOptions& options)
136 { return new Reader<A>(fileContent, fileLength, path, options); }
139 virtual const char* getPath() { return fPath; }
140 virtual time_t getModificationTime() { return 0; }
141 virtual DebugInfoKind getDebugInfoKind() { return ObjectFile::Reader::kDebugInfoNone; }
142 virtual std::vector<class ObjectFile::Atom*>& getAtoms();
143 virtual std::vector<class ObjectFile::Atom*>* getJustInTimeAtomsFor(const char* name);
144 virtual std::vector<Stab>* getStabs() { return NULL; }
145 virtual const char* getInstallPath() { return fDylibInstallPath; }
146 virtual uint32_t getTimestamp() { return fDylibTimeStamp; }
147 virtual uint32_t getCurrentVersion() { return fDylibtCurrentVersion; }
148 virtual uint32_t getCompatibilityVersion() { return fDylibCompatibilityVersion; }
149 virtual std::vector<const char*>* getDependentLibraryPaths();
150 virtual bool reExports(ObjectFile::Reader*);
151 virtual std::vector<const char*>* getAllowableClients();
154 const char* parentUmbrella() { return fParentUmbrella; }
157 typedef typename A::P P;
158 typedef typename A::P::E E;
163 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
165 struct AtomAndWeak { ObjectFile::Atom* atom; bool weak; };
166 typedef __gnu_cxx::hash_map<const char*, AtomAndWeak, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtomMap;
167 typedef typename NameToAtomMap::iterator NameToAtomMapIterator;
169 struct PathAndFlag { const char* path; bool reExport; };
171 Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, const ObjectFile::ReaderOptions& options);
174 const char* fParentUmbrella;
175 std::vector<const char*> fAllowableClients;
176 const char* fDylibInstallPath;
177 uint32_t fDylibTimeStamp;
178 uint32_t fDylibtCurrentVersion;
179 uint32_t fDylibCompatibilityVersion;
180 std::vector<PathAndFlag> fDependentLibraryPaths;
181 NameToAtomMap fAtoms;
183 static bool fgLogHashtable;
184 static std::vector<class ObjectFile::Atom*> fgEmptyAtomList;
187 template <typename A>
188 std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList;
189 template <typename A>
190 bool Reader<A>::fgLogHashtable = false;
193 template <typename A>
194 Reader<A>::Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, const ObjectFile::ReaderOptions& options)
195 : fParentUmbrella(NULL), fDylibInstallPath(NULL), fDylibTimeStamp(0), fDylibtCurrentVersion(0), fDylibCompatibilityVersion(0)
198 if ( ! validFile(fileContent) )
199 throw "not a valid mach-o object file";
201 fPath = strdup(path);
203 const macho_header<P>* header = (const macho_header<P>*)fileContent;
204 const uint32_t cmd_count = header->ncmds();
205 const macho_load_command<P>* const cmds = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>));
207 // a "blank" stub has zero load commands
208 if ( (header->filetype() == MH_DYLIB_STUB) && (cmd_count == 0) ) {
209 // no further processing needed
210 munmap((caddr_t)fileContent, fileLength);
214 // pass 1 builds list of all dependent libraries
215 const macho_load_command<P>* cmd = cmds;
216 for (uint32_t i = 0; i < cmd_count; ++i) {
217 switch (cmd->cmd()) {
219 case LC_LOAD_WEAK_DYLIB:
221 entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name());
222 entry.reExport = false;
223 fDependentLibraryPaths.push_back(entry);
226 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
229 // pass 2 determines re-export info
230 const macho_dysymtab_command<P>* dynamicInfo = NULL;
231 const macho_nlist<P>* symbolTable = NULL;
232 const char* strings = NULL;
234 for (uint32_t i = 0; i < cmd_count; ++i) {
235 switch (cmd->cmd()) {
238 const macho_symtab_command<P>* symtab = (macho_symtab_command<P>*)cmd;
239 symbolTable = (const macho_nlist<P>*)((char*)header + symtab->symoff());
240 strings = (char*)header + symtab->stroff();
244 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
247 macho_dylib_command<P>* dylibID = (macho_dylib_command<P>*)cmd;
248 fDylibInstallPath = strdup(dylibID->name());
249 fDylibTimeStamp = dylibID->timestamp();
250 fDylibtCurrentVersion = dylibID->current_version();
251 fDylibCompatibilityVersion = dylibID->compatibility_version();
253 case LC_SUB_UMBRELLA:
254 if ( !options.fFlatNamespace ) {
255 const char* frameworkLeafName = ((macho_sub_umbrella_command<P>*)cmd)->sub_umbrella();
256 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
257 const char* dylibName = it->path;
258 const char* lastSlash = strrchr(dylibName, '/');
259 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], frameworkLeafName) == 0) )
265 if ( !options.fFlatNamespace ) {
266 const char* dylibBaseName = ((macho_sub_library_command<P>*)cmd)->sub_library();
267 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
268 const char* dylibName = it->path;
269 const char* lastSlash = strrchr(dylibName, '/');
270 const char* leafStart = &lastSlash[1];
271 if ( lastSlash == NULL )
272 leafStart = dylibName;
273 const char* firstDot = strchr(leafStart, '.');
274 int len = strlen(leafStart);
275 if ( firstDot != NULL )
276 len = firstDot - leafStart;
277 if ( strncmp(leafStart, dylibBaseName, len) == 0 )
282 case LC_SUB_FRAMEWORK:
283 fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
287 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
289 // Process the rest of the commands here.
291 for (uint32_t i = 0; i < cmd_count; ++i) {
292 switch (cmd->cmd()) {
294 const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client());
296 fAllowableClients.push_back(temp);
300 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
303 // validate minimal load commands
304 if ( fDylibInstallPath == NULL )
305 throw "dylib missing LC_ID_DYLIB load command";
306 if ( symbolTable == NULL )
307 throw "dylib missing LC_SYMTAB load command";
308 if ( dynamicInfo == NULL )
309 throw "dylib missing LC_DYSYMTAB load command";
312 if ( dynamicInfo->tocoff() == 0 ) {
313 if ( fgLogHashtable ) fprintf(stderr, "ld64: building hashtable of %u toc entries for %s\n", dynamicInfo->nextdefsym(), path);
314 const macho_nlist<P>* start = &symbolTable[dynamicInfo->iextdefsym()];
315 const macho_nlist<P>* end = &start[dynamicInfo->nextdefsym()];
316 fAtoms.resize(dynamicInfo->nextdefsym()); // set initial bucket count
317 for (const macho_nlist<P>* sym=start; sym < end; ++sym) {
320 bucket.weak = ((sym->n_desc() & N_WEAK_DEF) != 0);
321 const char* name = strdup(&strings[sym->n_strx()]);
322 if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath());
323 fAtoms[name] = bucket;
327 int32_t count = dynamicInfo->ntoc();
328 fAtoms.resize(count); // set initial bucket count
329 if ( fgLogHashtable ) fprintf(stderr, "ld64: building hashtable of %u entries for %s\n", count, path);
330 const struct dylib_table_of_contents* toc = (dylib_table_of_contents*)((char*)header + dynamicInfo->tocoff());
331 for (int32_t i = 0; i < count; ++i) {
332 const uint32_t index = E::get32(toc[i].symbol_index);
333 const macho_nlist<P>* sym = &symbolTable[index];
336 bucket.weak = ((sym->n_desc() & N_WEAK_DEF) != 0);
337 const char* name = strdup(&strings[sym->n_strx()]);
338 if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath());
339 fAtoms[name] = bucket;
344 munmap((caddr_t)fileContent, fileLength);
347 template <typename A>
348 std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms()
350 // TO DO: for flat-namespace libraries, when linking flat_namespace
351 // we need to create an atom which references all undefines
352 return fgEmptyAtomList;
356 template <typename A>
357 std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name)
359 std::vector<class ObjectFile::Atom*>* atoms = NULL;
361 NameToAtomMapIterator pos = fAtoms.find(name);
362 if ( pos != fAtoms.end() ) {
363 if ( pos->second.atom == NULL ) {
364 // instantiate atom and update hash table
365 pos->second.atom = new ExportAtom<A>(*this, name, pos->second.weak);
366 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->getPath());
368 // return a vector of one atom
369 atoms = new std::vector<class ObjectFile::Atom*>;
370 atoms->push_back(pos->second.atom);
373 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath());
380 template <typename A>
381 std::vector<const char*>* Reader<A>::getDependentLibraryPaths()
383 std::vector<const char*>* result = new std::vector<const char*>;
384 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
385 result->push_back(it->path);
390 template <typename A>
391 std::vector<const char*>* Reader<A>::getAllowableClients()
393 std::vector<const char*>* result = new std::vector<const char*>;
394 for (typename std::vector<const char*>::iterator it = fAllowableClients.begin();
395 it != fAllowableClients.end();
397 result->push_back(*it);
399 return (fAllowableClients.size() != 0 ? result : NULL);
402 template <typename A>
403 bool Reader<A>::reExports(ObjectFile::Reader* child)
405 // A dependent dylib is re-exported under two conditions:
406 // 1) parent contains LC_SUB_UMBRELLA or LC_SUB_LIBRARY with child name
407 for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) {
408 if ( it->reExport && (strcmp(it->path, child->getPath()) == 0) )
412 // 2) child contains LC_SUB_FRAMEWORK with parent name
413 const char* parentUmbrellaName = ((Reader<A>*)child)->parentUmbrella();
414 if ( parentUmbrellaName != NULL ) {
415 const char* parentName = this->getPath();
416 const char* lastSlash = strrchr(parentName, '/');
417 if ( (lastSlash != NULL) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) )
425 bool Reader<ppc>::validFile(const uint8_t* fileContent)
427 const macho_header<P>* header = (const macho_header<P>*)fileContent;
428 if ( header->magic() != MH_MAGIC )
430 if ( header->cputype() != CPU_TYPE_POWERPC )
432 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
438 bool Reader<ppc64>::validFile(const uint8_t* fileContent)
440 const macho_header<P>* header = (const macho_header<P>*)fileContent;
441 if ( header->magic() != MH_MAGIC_64 )
443 if ( header->cputype() != CPU_TYPE_POWERPC64 )
445 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
451 bool Reader<x86>::validFile(const uint8_t* fileContent)
453 const macho_header<P>* header = (const macho_header<P>*)fileContent;
454 if ( header->magic() != MH_MAGIC )
456 if ( header->cputype() != CPU_TYPE_I386 )
458 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
466 }; // namespace dylib
467 }; // namespace mach_o
470 #endif // __OBJECT_FILE_DYLIB_MACH_O__