]> git.saurik.com Git - apple/ld64.git/blame - src/MachOReaderDylib.hpp
ld64-47.2.tar.gz
[apple/ld64.git] / src / MachOReaderDylib.hpp
CommitLineData
d696c285
A
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
52namespace mach_o {
53namespace dylib {
54
55
56// forward reference
57template <typename A> class Reader;
58
59
60class Segment : public ObjectFile::Segment
61{
62public:
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; }
68private:
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//
77template <typename A>
78class ExportAtom : public ObjectFile::Atom
79{
80public:
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 {}
99
100 virtual void setScope(Scope) { }
101
102protected:
103 friend class Reader<A>;
104 typedef typename A::P P;
105
106 ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak)
107 : fOwner(owner), fName(name), fWeakDefinition(weak) {}
108 virtual ~ExportAtom() {}
109
110 ObjectFile::Reader& fOwner;
111 const char* fName;
112 bool fWeakDefinition;
113
114 static std::vector<ObjectFile::Reference*> fgEmptyReferenceList;
115 static Segment fgImportSegment;
116};
117
118template <typename A>
119Segment ExportAtom<A>::fgImportSegment("__LINKEDIT");
120
121template <typename A>
122std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList;
123
124
125//
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.
129//
130template <typename A>
131class Reader : public ObjectFile::Reader
132{
133public:
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); }
137 virtual ~Reader() {}
138
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();
152
153protected:
154 const char* parentUmbrella() { return fParentUmbrella; }
155
156private:
157 typedef typename A::P P;
158 typedef typename A::P::E E;
159
160 class CStringEquals
161 {
162 public:
163 bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); }
164 };
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;
168
169 struct PathAndFlag { const char* path; bool reExport; };
170
171 Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, const ObjectFile::ReaderOptions& options);
172
173 const char* fPath;
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;
182
183 static bool fgLogHashtable;
184 static std::vector<class ObjectFile::Atom*> fgEmptyAtomList;
185};
186
187template <typename A>
188std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList;
189template <typename A>
190bool Reader<A>::fgLogHashtable = false;
191
192
193template <typename A>
194Reader<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)
196{
197 // sanity check
198 if ( ! validFile(fileContent) )
199 throw "not a valid mach-o object file";
200
201 fPath = strdup(path);
202
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>));
206
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);
211 return;
212 }
213
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()) {
218 case LC_LOAD_DYLIB:
219 case LC_LOAD_WEAK_DYLIB:
220 PathAndFlag entry;
221 entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name());
222 entry.reExport = false;
223 fDependentLibraryPaths.push_back(entry);
224 break;
225 }
226 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
227 }
228
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;
233 cmd = cmds;
234 for (uint32_t i = 0; i < cmd_count; ++i) {
235 switch (cmd->cmd()) {
236 case LC_SYMTAB:
237 {
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();
241 }
242 break;
243 case LC_DYSYMTAB:
244 dynamicInfo = (macho_dysymtab_command<P>*)cmd;
245 break;
246 case LC_ID_DYLIB:
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();
252 break;
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) )
260 it->reExport = true;
261 }
262 }
263 break;
264 case LC_SUB_LIBRARY:
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 )
278 it->reExport = true;
279 }
280 }
281 break;
282 case LC_SUB_FRAMEWORK:
283 fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella());
284 break;
285 }
286
287 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
288 }
289 // Process the rest of the commands here.
290 cmd = cmds;
291 for (uint32_t i = 0; i < cmd_count; ++i) {
292 switch (cmd->cmd()) {
293 case LC_SUB_CLIENT:
294 const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client());
295
296 fAllowableClients.push_back(temp);
297 break;
298 }
299
300 cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize());
301 }
302
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";
310
311 // build hash table
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) {
318 AtomAndWeak bucket;
319 bucket.atom = NULL;
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;
324 }
325 }
326 else {
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];
334 AtomAndWeak bucket;
335 bucket.atom = NULL;
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;
340 }
341 }
342
343 // unmap file
344 munmap((caddr_t)fileContent, fileLength);
345}
346
347template <typename A>
348std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms()
349{
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;
353}
354
355
356template <typename A>
357std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name)
358{
359 std::vector<class ObjectFile::Atom*>* atoms = NULL;
360
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());
367 }
368 // return a vector of one atom
369 atoms = new std::vector<class ObjectFile::Atom*>;
370 atoms->push_back(pos->second.atom);
371 }
372 else {
373 if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath());
374 }
375 return atoms;
376}
377
378
379
380template <typename A>
381std::vector<const char*>* Reader<A>::getDependentLibraryPaths()
382{
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);
386 }
387 return result;
388}
389
390template <typename A>
391std::vector<const char*>* Reader<A>::getAllowableClients()
392{
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();
396 it++) {
397 result->push_back(*it);
398 }
399 return (fAllowableClients.size() != 0 ? result : NULL);
400}
401
402template <typename A>
403bool Reader<A>::reExports(ObjectFile::Reader* child)
404{
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) )
409 return true;
410 }
411
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) )
418 return true;
419 }
420
421 return false;
422}
423
424template <>
425bool Reader<ppc>::validFile(const uint8_t* fileContent)
426{
427 const macho_header<P>* header = (const macho_header<P>*)fileContent;
428 if ( header->magic() != MH_MAGIC )
429 return false;
430 if ( header->cputype() != CPU_TYPE_POWERPC )
431 return false;
432 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
433 return false;
434 return true;
435}
436
437template <>
438bool Reader<ppc64>::validFile(const uint8_t* fileContent)
439{
440 const macho_header<P>* header = (const macho_header<P>*)fileContent;
441 if ( header->magic() != MH_MAGIC_64 )
442 return false;
443 if ( header->cputype() != CPU_TYPE_POWERPC64 )
444 return false;
445 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
446 return false;
447 return true;
448}
449
450template <>
451bool Reader<x86>::validFile(const uint8_t* fileContent)
452{
453 const macho_header<P>* header = (const macho_header<P>*)fileContent;
454 if ( header->magic() != MH_MAGIC )
455 return false;
456 if ( header->cputype() != CPU_TYPE_I386 )
457 return false;
458 if ( (header->filetype() != MH_DYLIB) && (header->filetype() != MH_DYLIB_STUB) )
459 return false;
460 return true;
461}
462
463
464
465
466}; // namespace dylib
467}; // namespace mach_o
468
469
470#endif // __OBJECT_FILE_DYLIB_MACH_O__