]>
Commit | Line | Data |
---|---|---|
d696c285 A |
1 | /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*- |
2 | * | |
a61fdf0a | 3 | * Copyright (c) 2005-2007 Apple Inc. All rights reserved. |
d696c285 A |
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" | |
55e3d2f6 | 40 | #include "MachOTrie.hpp" |
d696c285 A |
41 | #include "ObjectFile.h" |
42 | ||
43 | // | |
44 | // | |
45 | // To implement architecture xxx, you must write template specializations for the following method: | |
46 | // Reader<xxx>::validFile() | |
47 | // | |
48 | // | |
49 | ||
50 | ||
51 | ||
52 | ||
53 | namespace mach_o { | |
54 | namespace dylib { | |
55 | ||
56 | ||
57 | // forward reference | |
58 | template <typename A> class Reader; | |
59 | ||
60 | ||
61 | class Segment : public ObjectFile::Segment | |
62 | { | |
63 | public: | |
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; } | |
69 | private: | |
70 | const char* fName; | |
71 | }; | |
72 | ||
73 | ||
74 | // | |
75 | // An ExportAtom has no content. It exists so that the linker can track which imported | |
a61fdf0a | 76 | // symbols came from which dynamic libraries. |
d696c285 A |
77 | // |
78 | template <typename A> | |
79 | class ExportAtom : public ObjectFile::Atom | |
80 | { | |
81 | public: | |
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; } | |
69a49097 | 89 | virtual bool dontDeadStrip() const { return false; } |
d696c285 | 90 | virtual bool isZeroFill() const { return false; } |
2f2f92e4 | 91 | virtual bool isThumb() const { return false; } |
d696c285 A |
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; } | |
d696c285 | 97 | virtual ObjectFile::Atom& getFollowOnAtom() const { return *((ObjectFile::Atom*)NULL); } |
a61fdf0a | 98 | virtual uint32_t getOrdinal() const { return fOrdinal; } |
d696c285 | 99 | virtual std::vector<ObjectFile::LineInfo>* getLineInfo() const { return NULL; } |
74cfe461 | 100 | virtual ObjectFile::Alignment getAlignment() const { return ObjectFile::Alignment(0); } |
d696c285 A |
101 | virtual void copyRawContent(uint8_t buffer[]) const {} |
102 | ||
103 | virtual void setScope(Scope) { } | |
104 | ||
105 | protected: | |
106 | friend class Reader<A>; | |
107 | typedef typename A::P P; | |
108 | ||
a61fdf0a A |
109 | ExportAtom(ObjectFile::Reader& owner, const char* name, bool weak, uint32_t ordinal) |
110 | : fOwner(owner), fName(name), fOrdinal(ordinal), fWeakDefinition(weak) {} | |
d696c285 A |
111 | virtual ~ExportAtom() {} |
112 | ||
113 | ObjectFile::Reader& fOwner; | |
114 | const char* fName; | |
a61fdf0a | 115 | uint32_t fOrdinal; |
d696c285 A |
116 | bool fWeakDefinition; |
117 | ||
118 | static std::vector<ObjectFile::Reference*> fgEmptyReferenceList; | |
119 | static Segment fgImportSegment; | |
120 | }; | |
121 | ||
122 | template <typename A> | |
123 | Segment ExportAtom<A>::fgImportSegment("__LINKEDIT"); | |
124 | ||
125 | template <typename A> | |
126 | std::vector<ObjectFile::Reference*> ExportAtom<A>::fgEmptyReferenceList; | |
127 | ||
128 | ||
a61fdf0a A |
129 | |
130 | class ImportReference : public ObjectFile::Reference | |
131 | { | |
132 | public: | |
133 | ImportReference(const char* name) | |
134 | : fTarget(NULL), fTargetName(strdup(name)) {} | |
135 | virtual ~ImportReference() {} | |
136 | ||
137 | ||
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"; } | |
151 | ||
152 | private: | |
153 | const ObjectFile::Atom* fTarget; | |
154 | const char* fTargetName; | |
155 | }; | |
156 | ||
157 | ||
158 | // | |
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 | |
161 | // | |
162 | template <typename A> | |
163 | class ImportAtom : public ObjectFile::Atom | |
164 | { | |
165 | public: | |
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; } | |
2f2f92e4 | 175 | virtual bool isThumb() const { return false; } |
a61fdf0a A |
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 {} | |
186 | ||
187 | virtual void setScope(Scope) { } | |
188 | ||
189 | protected: | |
190 | friend class Reader<A>; | |
191 | typedef typename A::P P; | |
192 | ||
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)); | |
199 | } | |
200 | } | |
201 | ||
202 | ||
203 | ObjectFile::Reader& fOwner; | |
204 | uint32_t fOrdinal; | |
205 | std::vector<ObjectFile::Reference*> fReferences; | |
206 | ||
207 | static Segment fgImportSegment; | |
208 | }; | |
209 | ||
210 | template <typename A> | |
211 | Segment ImportAtom<A>::fgImportSegment("__LINKEDIT"); | |
212 | ||
213 | ||
214 | ||
215 | ||
d696c285 A |
216 | // |
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. | |
220 | // | |
221 | template <typename A> | |
222 | class Reader : public ObjectFile::Reader | |
223 | { | |
224 | public: | |
69a49097 | 225 | static bool validFile(const uint8_t* fileContent, bool executableOrDylib); |
a61fdf0a | 226 | Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, |
55e3d2f6 | 227 | const LibraryOptions& dylibOptions, const ObjectFile::ReaderOptions& options, |
a61fdf0a | 228 | uint32_t ordinalBase); |
69a49097 | 229 | virtual ~Reader() {} |
d696c285 A |
230 | |
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; } | |
a61fdf0a | 237 | virtual ObjectFile::Reader::ObjcConstraint getObjCConstraint() { return fObjcContraint; } |
d696c285 A |
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; } | |
a61fdf0a A |
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; } | |
d696c285 | 248 | virtual std::vector<const char*>* getAllowableClients(); |
2f2f92e4 | 249 | virtual bool hasWeakExternals() { return fHasWeakExports; } |
55e3d2f6 | 250 | virtual bool deadStrippable() { return fDeadStrippable; } |
2f2f92e4 | 251 | virtual bool isLazyLoadedDylib() { return fLazyLoaded; } |
d696c285 | 252 | |
a61fdf0a A |
253 | virtual void setImplicitlyLinked() { fImplicitlyLinked = true; } |
254 | ||
d696c285 | 255 | protected: |
a61fdf0a A |
256 | |
257 | struct ReExportChain { ReExportChain* prev; Reader<A>* reader; }; | |
258 | ||
2f2f92e4 | 259 | void assertNoReExportCycles(ReExportChain*); |
d696c285 A |
260 | |
261 | private: | |
262 | typedef typename A::P P; | |
263 | typedef typename A::P::E E; | |
264 | ||
265 | class CStringEquals | |
266 | { | |
267 | public: | |
268 | bool operator()(const char* left, const char* right) const { return (strcmp(left, right) == 0); } | |
269 | }; | |
a61fdf0a | 270 | struct AtomAndWeak { ObjectFile::Atom* atom; bool weak; uint32_t ordinal; }; |
d696c285 | 271 | typedef __gnu_cxx::hash_map<const char*, AtomAndWeak, __gnu_cxx::hash<const char*>, CStringEquals> NameToAtomMap; |
a61fdf0a | 272 | typedef __gnu_cxx::hash_set<const char*, __gnu_cxx::hash<const char*>, CStringEquals> NameSet; |
d696c285 A |
273 | typedef typename NameToAtomMap::iterator NameToAtomMapIterator; |
274 | ||
275 | struct PathAndFlag { const char* path; bool reExport; }; | |
276 | ||
a61fdf0a | 277 | bool isPublicLocation(const char* path); |
55e3d2f6 A |
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); | |
d696c285 A |
285 | |
286 | const char* fPath; | |
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; | |
a61fdf0a | 293 | uint32_t fReExportedOrdinal; |
d696c285 A |
294 | std::vector<PathAndFlag> fDependentLibraryPaths; |
295 | NameToAtomMap fAtoms; | |
a61fdf0a A |
296 | NameSet fIgnoreExports; |
297 | bool fNoRexports; | |
2f2f92e4 | 298 | bool fHasWeakExports; |
55e3d2f6 | 299 | bool fDeadStrippable; |
a61fdf0a A |
300 | const bool fLinkingFlat; |
301 | const bool fLinkingMainExecutable; | |
302 | bool fExplictReExportFound; | |
303 | bool fExplicitlyLinked; | |
304 | bool fImplicitlyLinked; | |
305 | bool fProvidedAtom; | |
2f2f92e4 A |
306 | bool fImplicitlyLinkPublicDylibs; |
307 | bool fLazyLoaded; | |
a61fdf0a A |
308 | ObjectFile::Reader::ObjcConstraint fObjcContraint; |
309 | std::vector<ObjectFile::Reader*> fReExportedChildren; | |
55e3d2f6 | 310 | const ObjectFile::ReaderOptions::MacVersionMin fDeploymentVersionMin; |
a61fdf0a | 311 | std::vector<class ObjectFile::Atom*> fFlatImports; |
d696c285 A |
312 | |
313 | static bool fgLogHashtable; | |
314 | static std::vector<class ObjectFile::Atom*> fgEmptyAtomList; | |
315 | }; | |
316 | ||
317 | template <typename A> | |
318 | std::vector<class ObjectFile::Atom*> Reader<A>::fgEmptyAtomList; | |
319 | template <typename A> | |
320 | bool Reader<A>::fgLogHashtable = false; | |
321 | ||
322 | ||
323 | template <typename A> | |
2f2f92e4 | 324 | Reader<A>::Reader(const uint8_t* fileContent, uint64_t fileLength, const char* path, |
55e3d2f6 | 325 | const LibraryOptions& dylibOptions, |
a61fdf0a A |
326 | const ObjectFile::ReaderOptions& options, uint32_t ordinalBase) |
327 | : fParentUmbrella(NULL), fDylibInstallPath(NULL), fDylibTimeStamp(0), fDylibtCurrentVersion(0), | |
55e3d2f6 | 328 | fDylibCompatibilityVersion(0), fReExportedOrdinal(ordinalBase), fLinkingFlat(options.fFlatNamespace), |
a61fdf0a | 329 | fLinkingMainExecutable(options.fLinkingMainExecutable), fExplictReExportFound(false), |
2f2f92e4 A |
330 | fExplicitlyLinked(false), fImplicitlyLinked(false), fProvidedAtom(false), |
331 | fImplicitlyLinkPublicDylibs(options.fImplicitlyLinkPublicDylibs), fLazyLoaded(dylibOptions.fLazyLoad), | |
332 | fObjcContraint(ObjectFile::Reader::kObjcNone), | |
55e3d2f6 | 333 | fDeploymentVersionMin(options.fMacVersionMin) |
d696c285 A |
334 | { |
335 | // sanity check | |
2f2f92e4 | 336 | if ( ! validFile(fileContent, dylibOptions.fBundleLoader) ) |
d696c285 A |
337 | throw "not a valid mach-o object file"; |
338 | ||
339 | fPath = strdup(path); | |
340 | ||
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>)); | |
2f2f92e4 | 344 | const macho_load_command<P>* const cmdsEnd = (macho_load_command<P>*)((char*)header + sizeof(macho_header<P>) + header->sizeofcmds()); |
d696c285 | 345 | |
a61fdf0a A |
346 | // write out path for -whatsloaded option |
347 | if ( options.fLogAllFiles ) | |
348 | printf("%s\n", path); | |
349 | ||
350 | if ( options.fRootSafe && ((header->flags() & MH_ROOT_SAFE) == 0) ) | |
2f2f92e4 | 351 | warning("using -root_safe but linking against %s which is not root safe", path); |
a61fdf0a A |
352 | |
353 | if ( options.fSetuidSafe && ((header->flags() & MH_SETUID_SAFE) == 0) ) | |
2f2f92e4 | 354 | warning("using -setuid_safe but linking against %s which is not setuid safe", path); |
a61fdf0a | 355 | |
d696c285 A |
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); | |
360 | return; | |
361 | } | |
362 | ||
a61fdf0a A |
363 | |
364 | // optimize the case where we know there is no reason to look at indirect dylibs | |
365 | fNoRexports = (header->flags() & MH_NO_REEXPORTED_DYLIBS); | |
2f2f92e4 | 366 | fHasWeakExports = (header->flags() & MH_WEAK_DEFINES); |
55e3d2f6 | 367 | fDeadStrippable = (header->flags() & MH_DEAD_STRIPPABLE_DYLIB); |
a61fdf0a A |
368 | bool trackDependentLibraries = !fNoRexports || options.fFlatNamespace; |
369 | ||
d696c285 A |
370 | // pass 1 builds list of all dependent libraries |
371 | const macho_load_command<P>* cmd = cmds; | |
a61fdf0a A |
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 | |
378 | case LC_LOAD_DYLIB: | |
379 | case LC_LOAD_WEAK_DYLIB: | |
380 | PathAndFlag entry; | |
381 | entry.path = strdup(((struct macho_dylib_command<P>*)cmd)->name()); | |
382 | entry.reExport = (cmd->cmd() == LC_REEXPORT_DYLIB); | |
383 | fDependentLibraryPaths.push_back(entry); | |
384 | break; | |
385 | } | |
386 | cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize()); | |
2f2f92e4 A |
387 | if ( cmd > cmdsEnd ) |
388 | throwf("malformed dylb, load command #%d is outside size of load commands in %s", i, path); | |
d696c285 | 389 | } |
d696c285 | 390 | } |
a61fdf0a | 391 | |
d696c285 A |
392 | // pass 2 determines re-export info |
393 | const macho_dysymtab_command<P>* dynamicInfo = NULL; | |
55e3d2f6 | 394 | const macho_dyld_info_command<P>* dyldInfo = NULL; |
d696c285 A |
395 | const macho_nlist<P>* symbolTable = NULL; |
396 | const char* strings = NULL; | |
397 | cmd = cmds; | |
398 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
399 | switch (cmd->cmd()) { | |
400 | case LC_SYMTAB: | |
401 | { | |
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(); | |
405 | } | |
406 | break; | |
407 | case LC_DYSYMTAB: | |
408 | dynamicInfo = (macho_dysymtab_command<P>*)cmd; | |
409 | break; | |
55e3d2f6 A |
410 | case LC_DYLD_INFO: |
411 | case LC_DYLD_INFO_ONLY: | |
412 | dyldInfo = (macho_dyld_info_command<P>*)cmd; | |
413 | break; | |
d696c285 | 414 | case LC_ID_DYLIB: |
a61fdf0a | 415 | { |
d696c285 A |
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(); | |
a61fdf0a | 421 | } |
d696c285 A |
422 | break; |
423 | case LC_SUB_UMBRELLA: | |
a61fdf0a | 424 | if ( trackDependentLibraries ) { |
d696c285 A |
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) ) | |
430 | it->reExport = true; | |
431 | } | |
432 | } | |
433 | break; | |
434 | case LC_SUB_LIBRARY: | |
a61fdf0a | 435 | if ( trackDependentLibraries) { |
d696c285 A |
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 ) | |
448 | it->reExport = true; | |
449 | } | |
450 | } | |
451 | break; | |
452 | case LC_SUB_FRAMEWORK: | |
453 | fParentUmbrella = strdup(((macho_sub_framework_command<P>*)cmd)->umbrella()); | |
454 | break; | |
a61fdf0a A |
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 | |
465 | // uint32_t flags; | |
466 | // }; | |
467 | // #define OBJC_IMAGE_SUPPORTS_GC 2 | |
468 | // #define OBJC_IMAGE_GC_ONLY 4 | |
469 | // | |
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; | |
477 | else | |
478 | fObjcContraint = ObjectFile::Reader::kObjcRetainRelease; | |
479 | } | |
480 | else if ( sect->size() > 0 ) { | |
2f2f92e4 | 481 | warning("can't parse __OBJC/__image_info section in %s", fPath); |
a61fdf0a A |
482 | } |
483 | } | |
484 | } | |
485 | } | |
d696c285 A |
486 | } |
487 | ||
488 | cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize()); | |
2f2f92e4 A |
489 | if ( cmd > cmdsEnd ) |
490 | throwf("malformed dylb, load command #%d is outside size of load commands in %s", i, path); | |
d696c285 | 491 | } |
a61fdf0a | 492 | |
d696c285 A |
493 | // Process the rest of the commands here. |
494 | cmd = cmds; | |
495 | for (uint32_t i = 0; i < cmd_count; ++i) { | |
496 | switch (cmd->cmd()) { | |
497 | case LC_SUB_CLIENT: | |
498 | const char *temp = strdup(((macho_sub_client_command<P>*)cmd)->client()); | |
d696c285 A |
499 | fAllowableClients.push_back(temp); |
500 | break; | |
501 | } | |
d696c285 A |
502 | cmd = (const macho_load_command<P>*)(((char*)cmd)+cmd->cmdsize()); |
503 | } | |
504 | ||
505 | // validate minimal load commands | |
2f2f92e4 A |
506 | if ( (fDylibInstallPath == NULL) && ((header->filetype() == MH_DYLIB) || (header->filetype() == MH_DYLIB_STUB)) ) |
507 | throwf("dylib %s missing LC_ID_DYLIB load command", path); | |
d696c285 | 508 | if ( symbolTable == NULL ) |
2f2f92e4 | 509 | throw "binary missing LC_SYMTAB load command"; |
d696c285 | 510 | if ( dynamicInfo == NULL ) |
2f2f92e4 | 511 | throw "binary missing LC_DYSYMTAB load command"; |
d696c285 | 512 | |
a61fdf0a A |
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()]); | |
521 | } | |
55e3d2f6 | 522 | fFlatImports.push_back(new ImportAtom<A>(*this, fReExportedOrdinal++, importNames)); |
a61fdf0a | 523 | } |
55e3d2f6 | 524 | |
d696c285 | 525 | // build hash table |
55e3d2f6 A |
526 | if ( dyldInfo != NULL ) |
527 | buildExportHashTableFromExportInfo(dyldInfo, fileContent); | |
528 | else | |
529 | buildExportHashTableFromSymbolTable(dynamicInfo, symbolTable, strings, fileContent); | |
530 | ||
531 | // special case libSystem | |
532 | if ( (fDylibInstallPath != NULL) && (strcmp(fDylibInstallPath, "/usr/lib/libSystem.B.dylib") == 0) ) | |
533 | addDyldFastStub(); | |
534 | ||
535 | // unmap file | |
536 | munmap((caddr_t)fileContent, fileLength); | |
537 | } | |
538 | ||
539 | ||
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) | |
544 | { | |
d696c285 | 545 | if ( dynamicInfo->tocoff() == 0 ) { |
55e3d2f6 | 546 | if ( fgLogHashtable ) fprintf(stderr, "ld: building hashtable of %u toc entries for %s\n", dynamicInfo->nextdefsym(), this->getPath()); |
d696c285 A |
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 | |
55e3d2f6 A |
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); | |
d696c285 A |
552 | } |
553 | } | |
554 | else { | |
555 | int32_t count = dynamicInfo->ntoc(); | |
556 | fAtoms.resize(count); // set initial bucket count | |
55e3d2f6 A |
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()); | |
d696c285 A |
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]; | |
55e3d2f6 | 562 | this->addSymbol(&strings[sym->n_strx()], (sym->n_desc() & N_WEAK_DEF) != 0); |
d696c285 A |
563 | } |
564 | } | |
55e3d2f6 | 565 | } |
d696c285 | 566 | |
55e3d2f6 A |
567 | |
568 | template <typename A> | |
569 | void Reader<A>::buildExportHashTableFromExportInfo(const macho_dyld_info_command<P>* dyldInfo, | |
570 | const uint8_t* fileContent) | |
571 | { | |
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); | |
580 | } | |
d696c285 A |
581 | } |
582 | ||
a61fdf0a | 583 | |
55e3d2f6 A |
584 | template <> |
585 | void Reader<x86_64>::addDyldFastStub() | |
586 | { | |
587 | addSymbol("dyld_stub_binder", false); | |
588 | } | |
589 | ||
590 | template <> | |
591 | void Reader<x86>::addDyldFastStub() | |
592 | { | |
593 | addSymbol("dyld_stub_binder", false); | |
594 | } | |
a61fdf0a A |
595 | |
596 | template <typename A> | |
55e3d2f6 | 597 | void Reader<A>::addDyldFastStub() |
a61fdf0a | 598 | { |
55e3d2f6 A |
599 | // do nothing |
600 | } | |
601 | ||
602 | template <typename A> | |
603 | void Reader<A>::addSymbol(const char* name, bool weakDef) | |
604 | { | |
605 | //fprintf(stderr, "addSymbol() %s\n", name); | |
a61fdf0a A |
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 ) { | |
55e3d2f6 | 613 | ObjectFile::ReaderOptions::MacVersionMin symVersionCondition = ObjectFile::ReaderOptions::kMinMacVersionUnset; |
a61fdf0a A |
614 | if ( (strncmp(symCond, "$os10.", 6) == 0) && isdigit(symCond[6]) && (symCond[7] == '$') ) { |
615 | switch ( symCond[6] - '0' ) { | |
616 | case 0: | |
617 | case 1: | |
618 | symVersionCondition = ObjectFile::ReaderOptions::k10_1; | |
619 | break; | |
620 | case 2: | |
621 | symVersionCondition = ObjectFile::ReaderOptions::k10_2; | |
622 | break; | |
623 | case 3: | |
624 | symVersionCondition = ObjectFile::ReaderOptions::k10_3; | |
625 | break; | |
626 | case 4: | |
627 | symVersionCondition = ObjectFile::ReaderOptions::k10_4; | |
628 | break; | |
629 | case 5: | |
630 | symVersionCondition = ObjectFile::ReaderOptions::k10_5; | |
631 | break; | |
2f2f92e4 A |
632 | case 6: |
633 | symVersionCondition = ObjectFile::ReaderOptions::k10_6; | |
634 | break; | |
a61fdf0a A |
635 | } |
636 | const char* symName = strchr(&symCond[1], '$'); | |
637 | if ( symName != NULL ) { | |
638 | ++symName; | |
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)); | |
643 | return; | |
644 | } | |
645 | else if ( strncmp(symAction, "add$", 4) == 0 ) { | |
55e3d2f6 | 646 | this->addSymbol(symName, weakDef); |
a61fdf0a A |
647 | return; |
648 | } | |
649 | else { | |
2f2f92e4 | 650 | warning("bad symbol action: %s in dylib %s", name, this->getPath()); |
a61fdf0a A |
651 | } |
652 | } | |
653 | } | |
654 | else { | |
2f2f92e4 | 655 | warning("bad symbol name: %s in dylib %s", name, this->getPath()); |
a61fdf0a A |
656 | } |
657 | } | |
658 | else { | |
2f2f92e4 | 659 | warning("bad symbol version: %s in dylib %s", name, this->getPath()); |
a61fdf0a A |
660 | } |
661 | } | |
662 | else { | |
2f2f92e4 | 663 | warning("bad symbol condition: %s in dylib %s", name, this->getPath()); |
a61fdf0a A |
664 | } |
665 | } | |
666 | ||
667 | // add symbol as possible export if we are not supposed to ignore it | |
668 | if ( fIgnoreExports.count(name) == 0 ) { | |
669 | AtomAndWeak bucket; | |
670 | bucket.atom = NULL; | |
55e3d2f6 A |
671 | bucket.weak = weakDef; |
672 | bucket.ordinal = fReExportedOrdinal++; | |
a61fdf0a A |
673 | if ( fgLogHashtable ) fprintf(stderr, " adding %s to hash table for %s\n", name, this->getPath()); |
674 | fAtoms[strdup(name)] = bucket; | |
675 | } | |
676 | } | |
677 | ||
678 | ||
d696c285 A |
679 | template <typename A> |
680 | std::vector<class ObjectFile::Atom*>& Reader<A>::getAtoms() | |
681 | { | |
a61fdf0a | 682 | return fFlatImports; |
d696c285 A |
683 | } |
684 | ||
685 | ||
686 | template <typename A> | |
687 | std::vector<class ObjectFile::Atom*>* Reader<A>::getJustInTimeAtomsFor(const char* name) | |
688 | { | |
55e3d2f6 A |
689 | // if supposed to ignore this export, then pretend I don't have it |
690 | if ( fIgnoreExports.count(name) != 0 ) | |
691 | return NULL; | |
692 | ||
d696c285 | 693 | std::vector<class ObjectFile::Atom*>* atoms = NULL; |
d696c285 A |
694 | NameToAtomMapIterator pos = fAtoms.find(name); |
695 | if ( pos != fAtoms.end() ) { | |
696 | if ( pos->second.atom == NULL ) { | |
697 | // instantiate atom and update hash table | |
a61fdf0a A |
698 | pos->second.atom = new ExportAtom<A>(*this, name, pos->second.weak, pos->second.ordinal); |
699 | fProvidedAtom = true; | |
d696c285 A |
700 | if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->getPath()); |
701 | } | |
702 | // return a vector of one atom | |
703 | atoms = new std::vector<class ObjectFile::Atom*>; | |
704 | atoms->push_back(pos->second.atom); | |
705 | } | |
706 | else { | |
707 | if ( fgLogHashtable ) fprintf(stderr, "getJustInTimeAtomsFor: %s NOT found in %s\n", name, this->getPath()); | |
55e3d2f6 A |
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); | |
720 | delete childAtoms; | |
721 | return atoms; | |
a61fdf0a A |
722 | } |
723 | } | |
d696c285 A |
724 | } |
725 | return atoms; | |
726 | } | |
727 | ||
728 | ||
729 | ||
730 | template <typename A> | |
a61fdf0a | 731 | bool Reader<A>::isPublicLocation(const char* path) |
d696c285 | 732 | { |
2f2f92e4 A |
733 | // -no_implicit_dylibs disables this optimization |
734 | if ( ! fImplicitlyLinkPublicDylibs ) | |
735 | return false; | |
736 | ||
a61fdf0a A |
737 | // /usr/lib is a public location |
738 | if ( (strncmp(path, "/usr/lib/", 9) == 0) && (strchr(&path[9], '/') == NULL) ) | |
739 | return true; | |
740 | ||
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 ) | |
752 | return true; | |
753 | } | |
d696c285 | 754 | } |
a61fdf0a A |
755 | |
756 | return false; | |
d696c285 A |
757 | } |
758 | ||
759 | template <typename A> | |
a61fdf0a | 760 | void Reader<A>::processIndirectLibraries(DylibHander* handler) |
d696c285 | 761 | { |
a61fdf0a A |
762 | if ( fLinkingFlat ) { |
763 | for (typename std::vector<PathAndFlag>::iterator it = fDependentLibraryPaths.begin(); it != fDependentLibraryPaths.end(); it++) { | |
764 | handler->findDylib(it->path, this->getPath()); | |
765 | } | |
d696c285 | 766 | } |
a61fdf0a A |
767 | else if ( fNoRexports ) { |
768 | // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do | |
769 | } | |
770 | else { | |
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(); | |
782 | } | |
fb24a050 A |
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"); | |
785 | } | |
786 | else { | |
a61fdf0a | 787 | fReExportedChildren.push_back(child); |
fb24a050 A |
788 | //fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->getInstallPath(), it->path); |
789 | } | |
a61fdf0a A |
790 | } |
791 | else { | |
792 | // add all child's symbols to me | |
793 | fReExportedChildren.push_back(child); | |
fb24a050 | 794 | //fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->getInstallPath(), it->path); |
a61fdf0a A |
795 | } |
796 | } | |
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); | |
808 | } | |
809 | } | |
810 | } | |
811 | } | |
812 | } | |
813 | ||
814 | // check for re-export cycles | |
a61fdf0a A |
815 | ReExportChain chain; |
816 | chain.prev = NULL; | |
817 | chain.reader = this; | |
2f2f92e4 | 818 | this->assertNoReExportCycles(&chain); |
d696c285 A |
819 | } |
820 | ||
821 | template <typename A> | |
2f2f92e4 | 822 | void Reader<A>::assertNoReExportCycles(ReExportChain* prev) |
d696c285 | 823 | { |
2f2f92e4 | 824 | // recursively check my re-exported dylibs |
a61fdf0a A |
825 | ReExportChain chain; |
826 | chain.prev = prev; | |
827 | chain.reader = this; | |
828 | for (std::vector<ObjectFile::Reader*>::iterator it = fReExportedChildren.begin(); it != fReExportedChildren.end(); it++) { | |
2f2f92e4 A |
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()); | |
834 | } | |
835 | } | |
836 | ((Reader<A>*)(*it))->assertNoReExportCycles(&chain); | |
d696c285 | 837 | } |
a61fdf0a | 838 | } |
d696c285 | 839 | |
a61fdf0a A |
840 | |
841 | template <typename A> | |
842 | std::vector<const char*>* Reader<A>::getAllowableClients() | |
843 | { | |
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(); | |
847 | it++) { | |
848 | result->push_back(*it); | |
849 | } | |
850 | return (fAllowableClients.size() != 0 ? result : NULL); | |
d696c285 A |
851 | } |
852 | ||
853 | template <> | |
2f2f92e4 | 854 | bool Reader<ppc>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle) |
d696c285 A |
855 | { |
856 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
857 | if ( header->magic() != MH_MAGIC ) | |
858 | return false; | |
859 | if ( header->cputype() != CPU_TYPE_POWERPC ) | |
860 | return false; | |
69a49097 A |
861 | switch ( header->filetype() ) { |
862 | case MH_DYLIB: | |
863 | case MH_DYLIB_STUB: | |
864 | return true; | |
2f2f92e4 A |
865 | case MH_BUNDLE: |
866 | if ( executableOrDyliborBundle ) | |
867 | return true; | |
868 | else | |
869 | throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"; | |
69a49097 | 870 | case MH_EXECUTE: |
2f2f92e4 A |
871 | if ( executableOrDyliborBundle ) |
872 | return true; | |
873 | else | |
874 | throw "can't link with a main executable"; | |
69a49097 A |
875 | default: |
876 | return false; | |
877 | } | |
d696c285 A |
878 | } |
879 | ||
880 | template <> | |
2f2f92e4 | 881 | bool Reader<ppc64>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle) |
d696c285 A |
882 | { |
883 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
884 | if ( header->magic() != MH_MAGIC_64 ) | |
885 | return false; | |
886 | if ( header->cputype() != CPU_TYPE_POWERPC64 ) | |
887 | return false; | |
69a49097 A |
888 | switch ( header->filetype() ) { |
889 | case MH_DYLIB: | |
890 | case MH_DYLIB_STUB: | |
891 | return true; | |
2f2f92e4 A |
892 | case MH_BUNDLE: |
893 | if ( executableOrDyliborBundle ) | |
894 | return true; | |
895 | else | |
896 | throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"; | |
69a49097 | 897 | case MH_EXECUTE: |
2f2f92e4 A |
898 | if ( executableOrDyliborBundle ) |
899 | return true; | |
900 | else | |
901 | throw "can't link with a main executable"; | |
69a49097 A |
902 | default: |
903 | return false; | |
904 | } | |
d696c285 A |
905 | } |
906 | ||
907 | template <> | |
2f2f92e4 | 908 | bool Reader<x86>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle) |
d696c285 A |
909 | { |
910 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
911 | if ( header->magic() != MH_MAGIC ) | |
912 | return false; | |
913 | if ( header->cputype() != CPU_TYPE_I386 ) | |
914 | return false; | |
69a49097 A |
915 | switch ( header->filetype() ) { |
916 | case MH_DYLIB: | |
917 | case MH_DYLIB_STUB: | |
918 | return true; | |
2f2f92e4 A |
919 | case MH_BUNDLE: |
920 | if ( executableOrDyliborBundle ) | |
921 | return true; | |
922 | else | |
923 | throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"; | |
69a49097 | 924 | case MH_EXECUTE: |
2f2f92e4 A |
925 | if ( executableOrDyliborBundle ) |
926 | return true; | |
927 | else | |
928 | throw "can't link with a main executable"; | |
69a49097 A |
929 | default: |
930 | return false; | |
931 | } | |
d696c285 A |
932 | } |
933 | ||
69a49097 | 934 | template <> |
2f2f92e4 | 935 | bool Reader<x86_64>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle) |
69a49097 A |
936 | { |
937 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
938 | if ( header->magic() != MH_MAGIC_64 ) | |
939 | return false; | |
940 | if ( header->cputype() != CPU_TYPE_X86_64 ) | |
941 | return false; | |
942 | switch ( header->filetype() ) { | |
943 | case MH_DYLIB: | |
944 | case MH_DYLIB_STUB: | |
945 | return true; | |
2f2f92e4 A |
946 | case MH_BUNDLE: |
947 | if ( executableOrDyliborBundle ) | |
948 | return true; | |
949 | else | |
950 | throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"; | |
69a49097 | 951 | case MH_EXECUTE: |
2f2f92e4 A |
952 | if ( executableOrDyliborBundle ) |
953 | return true; | |
954 | else | |
955 | throw "can't link with a main executable"; | |
69a49097 A |
956 | default: |
957 | return false; | |
958 | } | |
959 | } | |
d696c285 | 960 | |
2f2f92e4 A |
961 | template <> |
962 | bool Reader<arm>::validFile(const uint8_t* fileContent, bool executableOrDyliborBundle) | |
963 | { | |
964 | const macho_header<P>* header = (const macho_header<P>*)fileContent; | |
965 | if ( header->magic() != MH_MAGIC ) | |
966 | return false; | |
967 | if ( header->cputype() != CPU_TYPE_ARM ) | |
968 | return false; | |
969 | switch ( header->filetype() ) { | |
970 | case MH_DYLIB: | |
971 | case MH_DYLIB_STUB: | |
972 | return true; | |
973 | case MH_BUNDLE: | |
974 | if ( executableOrDyliborBundle ) | |
975 | return true; | |
976 | else | |
977 | throw "can't link with bundle (MH_BUNDLE) only dylibs (MH_DYLIB)"; | |
978 | case MH_EXECUTE: | |
979 | if ( executableOrDyliborBundle ) | |
980 | return true; | |
981 | else | |
982 | throw "can't link with a main executable"; | |
983 | default: | |
984 | return false; | |
985 | } | |
986 | } | |
86b84c30 | 987 | |
d696c285 A |
988 | }; // namespace dylib |
989 | }; // namespace mach_o | |
990 | ||
991 | ||
992 | #endif // __OBJECT_FILE_DYLIB_MACH_O__ |