]> git.saurik.com Git - apple/ld64.git/blob - src/MachOReaderDylib.hpp
9cadfbbe3c7446486ce33078bbb6ca03c610254e
[apple/ld64.git] / src / MachOReaderDylib.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2005-2006 Apple Computer, Inc. All rights reserved.
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 #ifndef __OBJECT_FILE_DYLIB_MACH_O__
26 #define __OBJECT_FILE_DYLIB_MACH_O__
27
28 #include <stdint.h>
29 #include <math.h>
30 #include <unistd.h>
31 #include <sys/param.h>
32
33
34 #include <vector>
35 #include <set>
36 #include <algorithm>
37 #include <ext/hash_map>
38
39 #include "MachOFileAbstraction.hpp"
40 #include "ObjectFile.h"
41
42 //
43 //
44 // To implement architecture xxx, you must write template specializations for the following method:
45 // Reader<xxx>::validFile()
46 //
47 //
48
49
50
51
52 namespace mach_o {
53 namespace dylib {
54
55
56 // forward reference
57 template <typename A> class Reader;
58
59
60 class Segment : public ObjectFile::Segment
61 {
62 public:
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; }
68 private:
69 const char* fName;
70 };
71
72
73 //
74 // An ExportAtom has no content. It exists so that the linker can track which imported
75 // symbols can from which dynamic libraries.
76 //
77 template <typename A>
78 class ExportAtom : public ObjectFile::Atom
79 {
80 public:
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 {}
100
101 virtual void setScope(Scope) { }
102
103 protected:
104 friend class Reader<A>;
105 typedef typename A::P P;
106
107 ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak)
108 : fOwner(owner), fName(name), fWeakDefinition(weak) {}
109 virtual ~ExportAtom() {}
110
111 ObjectFile::Reader& fOwner;
112 const char* fName;
113 bool fWeakDefinition;
114
115 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
116 static Segment fgImportSegment;
117 };
118
119 template <typename A>
120 Segment ExportAtom<A>::fgImportSegment("__LINKEDIT");
121
122 template <typename A>
123 std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList;
124
125
126 //
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.
130 //
131 template <typename A>
132 class Reader : public ObjectFile::Reader
133 {
134 public:
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); }
139 virtual ~Reader() {}
140
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();
154
155 protected:
156 const char* parentUmbrella() { return fParentUmbrella; }
157
158 private:
159 typedef typename A::P P;
160 typedef typename A::P::E E;
161
162 class CStringEquals
163 {
164 public:
165 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
166 };
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;
170
171 struct PathAndFlag { const char* path; bool reExport; };
172
173 Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path,
174 bool executableOrDylib, const ObjectFile::ReaderOptions& options);
175
176 const char* fPath;
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;
185
186 static bool fgLogHashtable;
187 static std::vector<class ObjectFile::Atom*> fgEmptyAtomList;
188 };
189
190 template <typename A>
191 std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList;
192 template <typename A>
193 bool Reader<A>::fgLogHashtable = false;
194
195
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)
199 {
200 // sanity check
201 if ( ! validFile(fileContent, executableOrDylib) )
202 throw "not a valid mach-o object file";
203
204 fPath = strdup(path);
205
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>));
209
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);
214 return;
215 }
216
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()) {
221 case LC_LOAD_DYLIB:
222 case LC_LOAD_WEAK_DYLIB:
223 PathAndFlag entry;
224 entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name());
225 entry.reExport = false;
226 fDependentLibraryPaths.push_back(entry);
227 break;
228 }
229 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
230 }
231
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;
236 cmd = cmds;
237 for (uint32_t i = 0; i < cmd_count; ++i) {
238 switch (cmd->cmd()) {
239 case LC_SYMTAB:
240 {
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();
244 }
245 break;
246 case LC_DYSYMTAB:
247 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
248 break;
249 case LC_ID_DYLIB:
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();
255 break;
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) )
263 it->reExport = true;
264 }
265 }
266 break;
267 case LC_SUB_LIBRARY:
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 )
281 it->reExport = true;
282 }
283 }
284 break;
285 case LC_SUB_FRAMEWORK:
286 fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
287 break;
288 }
289
290 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
291 }
292 // Process the rest of the commands here.
293 cmd = cmds;
294 for (uint32_t i = 0; i < cmd_count; ++i) {
295 switch (cmd->cmd()) {
296 case LC_SUB_CLIENT:
297 const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client());
298
299 fAllowableClients.push_back(temp);
300 break;
301 }
302
303 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
304 }
305
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";
313
314 // build hash table
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) {
321 AtomAndWeak bucket;
322 bucket.atom = NULL;
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;
327 }
328 }
329 else {
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];
337 AtomAndWeak bucket;
338 bucket.atom = NULL;
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;
343 }
344 }
345
346 // unmap file
347 munmap((caddr_t)fileContent, fileLength);
348 }
349
350 template <typename A>
351 std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms()
352 {
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;
356 }
357
358
359 template <typename A>
360 std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name)
361 {
362 std::vector<class ObjectFile::Atom*>* atoms = NULL;
363
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());
370 }
371 // return a vector of one atom
372 atoms = new std::vector<class ObjectFile::Atom*>;
373 atoms->push_back(pos->second.atom);
374 }
375 else {
376 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath());
377 }
378 return atoms;
379 }
380
381
382
383 template <typename A>
384 std::vector<const char*>* Reader<A>::getDependentLibraryPaths()
385 {
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);
389 }
390 return result;
391 }
392
393 template <typename A>
394 std::vector<const char*>* Reader<A>::getAllowableClients()
395 {
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();
399 it++) {
400 result->push_back(*it);
401 }
402 return (fAllowableClients.size() != 0 ? result : NULL);
403 }
404
405 template <typename A>
406 bool Reader<A>::reExports(ObjectFile::Reader* child)
407 {
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))) )
413 return true;
414 }
415
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) )
422 return true;
423 }
424
425 return false;
426 }
427
428 template <>
429 bool Reader<ppc>::validFile(const uint8_t* fileContent, bool executableOrDylib)
430 {
431 const macho_header<P>* header = (const macho_header<P>*)fileContent;
432 if ( header->magic() != MH_MAGIC )
433 return false;
434 if ( header->cputype() != CPU_TYPE_POWERPC )
435 return false;
436 switch ( header->filetype() ) {
437 case MH_DYLIB:
438 case MH_DYLIB_STUB:
439 return true;
440 case MH_EXECUTE:
441 return executableOrDylib;
442 default:
443 return false;
444 }
445 }
446
447 template <>
448 bool Reader<ppc64>::validFile(const uint8_t* fileContent, bool executableOrDylib)
449 {
450 const macho_header<P>* header = (const macho_header<P>*)fileContent;
451 if ( header->magic() != MH_MAGIC_64 )
452 return false;
453 if ( header->cputype() != CPU_TYPE_POWERPC64 )
454 return false;
455 switch ( header->filetype() ) {
456 case MH_DYLIB:
457 case MH_DYLIB_STUB:
458 return true;
459 case MH_EXECUTE:
460 return executableOrDylib;
461 default:
462 return false;
463 }
464 }
465
466 template <>
467 bool Reader<x86>::validFile(const uint8_t* fileContent, bool executableOrDylib)
468 {
469 const macho_header<P>* header = (const macho_header<P>*)fileContent;
470 if ( header->magic() != MH_MAGIC )
471 return false;
472 if ( header->cputype() != CPU_TYPE_I386 )
473 return false;
474 switch ( header->filetype() ) {
475 case MH_DYLIB:
476 case MH_DYLIB_STUB:
477 return true;
478 case MH_EXECUTE:
479 return executableOrDylib;
480 default:
481 return false;
482 }
483 }
484
485 template <>
486 bool Reader<x86_64>::validFile(const uint8_t* fileContent, bool executableOrDylib)
487 {
488 const macho_header<P>* header = (const macho_header<P>*)fileContent;
489 if ( header->magic() != MH_MAGIC_64 )
490 return false;
491 if ( header->cputype() != CPU_TYPE_X86_64 )
492 return false;
493 switch ( header->filetype() ) {
494 case MH_DYLIB:
495 case MH_DYLIB_STUB:
496 return true;
497 case MH_EXECUTE:
498 return executableOrDylib;
499 default:
500 return false;
501 }
502 }
503
504
505
506 }; // namespace dylib
507 }; // namespace mach_o
508
509
510 #endif // __OBJECT_FILE_DYLIB_MACH_O__