]> git.saurik.com Git - apple/ld64.git/blob - src/ld/parsers/generic_dylib_file.hpp
0b781bec7091957e5bbc75e1087a6a1564dcfc11
[apple/ld64.git] / src / ld / parsers / generic_dylib_file.hpp
1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
2 *
3 * Copyright (c) 2015 Apple 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 __GENERIC_DYLIB_FILE_H__
26 #define __GENERIC_DYLIB_FILE_H__
27
28 #include "ld.hpp"
29 #include "Options.h"
30 #include <unordered_map>
31 #include <unordered_set>
32
33 namespace generic {
34 namespace dylib {
35
36 // forward reference
37 template <typename A> class File;
38
39 //
40 // An ExportAtom has no content. It exists so that the linker can track which
41 // imported symbols came from which dynamic libraries.
42 //
43 template <typename A>
44 class ExportAtom final : public ld::Atom
45 {
46 public:
47 ExportAtom(const File<A>& f, const char* nm, bool weakDef, bool tlv,
48 typename A::P::uint_t address)
49 : ld::Atom(f._importProxySection, ld::Atom::definitionProxy,
50 (weakDef ? ld::Atom::combineByName : ld::Atom::combineNever),
51 ld::Atom::scopeLinkageUnit,
52 (tlv ? ld::Atom::typeTLV : ld::Atom::typeUnclassified),
53 symbolTableNotIn, false, false, false, ld::Atom::Alignment(0)),
54 _file(f),
55 _name(nm),
56 _address(address)
57 {}
58
59 // overrides of ld::Atom
60 virtual const ld::File* file() const override final { return &_file; }
61 virtual const char* name() const override final { return _name; }
62 virtual uint64_t size() const override final { return 0; }
63 virtual uint64_t objectAddress() const override final { return _address; }
64 virtual void copyRawContent(uint8_t buffer[]) const override final { }
65
66 virtual void setScope(Scope) { }
67
68 private:
69 using pint_t = typename A::P::uint_t;
70
71 virtual ~ExportAtom() {}
72
73 const File<A>& _file;
74 const char* _name;
75 pint_t _address;
76 };
77
78
79 //
80 // An ImportAtom has no content. It exists so that when linking a main executable flat-namespace
81 // the imports of all flat dylibs are checked
82 //
83 template <typename A>
84 class ImportAtom final : public ld::Atom
85 {
86 public:
87 ImportAtom(File<A>& f, std::vector<const char*>& imports);
88
89 // overrides of ld::Atom
90 virtual ld::File* file() const override final { return &_file; }
91 virtual const char* name() const override final { return "import-atom"; }
92 virtual uint64_t size() const override final { return 0; }
93 virtual uint64_t objectAddress() const override final { return 0; }
94 virtual ld::Fixup::iterator fixupsBegin() const override final { return &_undefs[0]; }
95 virtual ld::Fixup::iterator fixupsEnd() const override final { return &_undefs[_undefs.size()]; }
96 virtual void copyRawContent(uint8_t buffer[]) const override final { }
97
98 virtual void setScope(Scope) { }
99
100 private:
101 virtual ~ImportAtom() {}
102
103 File<A>& _file;
104 mutable std::vector<ld::Fixup> _undefs;
105 };
106
107 template <typename A>
108 ImportAtom<A>::ImportAtom(File<A>& f, std::vector<const char*>& imports)
109 : ld::Atom(f._flatDummySection, ld::Atom::definitionRegular, ld::Atom::combineNever,
110 ld::Atom::scopeTranslationUnit, ld::Atom::typeUnclassified, symbolTableNotIn, false,
111 false, false, ld::Atom::Alignment(0)),
112 _file(f)
113 {
114 for(auto *name : imports)
115 _undefs.emplace_back(0, ld::Fixup::k1of1, ld::Fixup::kindNone, false, strdup(name));
116 }
117
118 //
119 // A generic representation for the dynamic library files we support (Mach-O and text-based stubs).
120 // Common state and functionality is consolidated in this class.
121 //
122 template <typename A>
123 class File : public ld::dylib::File
124 {
125 public:
126 File(const char* path, time_t mTime, ld::File::Ordinal ordinal, Options::Platform platform,
127 uint32_t linkMinOSVersion, bool allowWeakImports, bool linkingFlatNamespace, bool hoistImplicitPublicDylibs,
128 bool allowSimToMacOSX, bool addVers);
129
130 // overrides of ld::File
131 virtual bool forEachAtom(ld::File::AtomHandler&) const override final;
132 virtual bool justInTimeforEachAtom(const char* name, ld::File::AtomHandler&) const override final;
133 virtual ld::File::ObjcConstraint objCConstraint() const override final { return _objcConstraint; }
134 virtual uint8_t swiftVersion() const override final { return _swiftVersion; }
135 virtual uint32_t minOSVersion() const override final { return _minVersionInDylib; }
136 virtual uint32_t platform() const override final { return _platformInDylib; }
137 virtual ld::Bitcode* getBitcode() const override final { return _bitcode.get(); }
138
139
140 // overrides of ld::dylib::File
141 virtual void processIndirectLibraries(ld::dylib::File::DylibHandler*, bool addImplicitDylibs) override;
142 virtual bool providedExportAtom() const override final { return _providedAtom; }
143 virtual const char* parentUmbrella() const override final { return _parentUmbrella; }
144 virtual const std::vector<const char*>* allowableClients() const override final { return _allowableClients.empty() ? nullptr : &_allowableClients; }
145 virtual const std::vector<const char*>& rpaths() const override final { return _rpaths; }
146 virtual bool hasWeakExternals() const override final { return _hasWeakExports; }
147 virtual bool deadStrippable() const override final { return _deadStrippable; }
148 virtual bool hasWeakDefinition(const char* name) const override final;
149 virtual bool hasPublicInstallName() const override final { return _hasPublicInstallName; }
150 virtual bool allSymbolsAreWeakImported() const override final;
151 virtual bool installPathVersionSpecific() const override final { return _installPathOverride; }
152 virtual bool appExtensionSafe() const override final { return _appExtensionSafe; };
153
154
155 bool wrongOS() const { return _wrongOS; }
156
157 private:
158 using pint_t = typename A::P::uint_t;
159
160 friend class ExportAtom<A>;
161 friend class ImportAtom<A>;
162
163 struct CStringHash {
164 std::size_t operator()(const char* __s) const {
165 unsigned long __h = 0;
166 for ( ; *__s; ++__s)
167 __h = 5 * __h + *__s;
168 return size_t(__h);
169 };
170 };
171
172 protected:
173 struct AtomAndWeak { ld::Atom* atom; bool weakDef; bool tlv; pint_t address; };
174 struct Dependent {
175 const char* path;
176 File<A>* dylib;
177 bool reExport;
178
179 Dependent(const char* path, bool reExport)
180 : path(path), dylib(nullptr), reExport(reExport) {}
181 };
182 struct ReExportChain { ReExportChain* prev; const File* file; };
183
184 private:
185 using NameToAtomMap = std::unordered_map<const char*, AtomAndWeak, ld::CStringHash, ld::CStringEquals>;
186 using NameSet = std::unordered_set<const char*, CStringHash, ld::CStringEquals>;
187
188 std::pair<bool, bool> hasWeakDefinitionImpl(const char* name) const;
189 bool containsOrReExports(const char* name, bool& weakDef, bool& tlv, pint_t& addr) const;
190 void assertNoReExportCycles(ReExportChain*) const;
191
192 protected:
193 bool isPublicLocation(const char* path) const;
194
195 private:
196 ld::Section _importProxySection;
197 ld::Section _flatDummySection;
198 mutable bool _providedAtom;
199 bool _indirectDylibsProcessed;
200
201 protected:
202 mutable NameToAtomMap _atoms;
203 NameSet _ignoreExports;
204 std::vector<Dependent> _dependentDylibs;
205 ImportAtom<A>* _importAtom;
206 std::vector<const char*> _allowableClients;
207 std::vector<const char*> _rpaths;
208 const char* _parentUmbrella;
209 std::unique_ptr<ld::Bitcode> _bitcode;
210 const Options::Platform _platform;
211 ld::File::ObjcConstraint _objcConstraint;
212 const uint32_t _linkMinOSVersion;
213 uint32_t _minVersionInDylib;
214 uint32_t _platformInDylib;
215 uint8_t _swiftVersion;
216 bool _wrongOS;
217 bool _linkingFlat;
218 bool _noRexports;
219 bool _explictReExportFound;
220 bool _implicitlyLinkPublicDylibs;
221 bool _installPathOverride;
222 bool _hasWeakExports;
223 bool _deadStrippable;
224 bool _hasPublicInstallName;
225 bool _appExtensionSafe;
226 const bool _allowWeakImports;
227 const bool _allowSimToMacOSXLinking;
228 const bool _addVersionLoadCommand;
229
230 static bool _s_logHashtable;
231 };
232
233 template <typename A>
234 bool File<A>::_s_logHashtable = false;
235
236 template <typename A>
237 File<A>::File(const char* path, time_t mTime, ld::File::Ordinal ord, Options::Platform platform,
238 uint32_t linkMinOSVersion, bool allowWeakImports, bool linkingFlatNamespace,
239 bool hoistImplicitPublicDylibs,
240 bool allowSimToMacOSX, bool addVers)
241 : ld::dylib::File(path, mTime, ord),
242 _importProxySection("__TEXT", "__import", ld::Section::typeImportProxies, true),
243 _flatDummySection("__LINKEDIT", "__flat_dummy", ld::Section::typeLinkEdit, true),
244 _providedAtom(false),
245 _indirectDylibsProcessed(false),
246 _importAtom(nullptr),
247 _parentUmbrella(nullptr),
248 _platform(platform),
249 _objcConstraint(ld::File::objcConstraintNone),
250 _linkMinOSVersion(linkMinOSVersion),
251 _minVersionInDylib(0),
252 _platformInDylib(Options::kPlatformUnknown),
253 _swiftVersion(0),
254 _wrongOS(false),
255 _linkingFlat(linkingFlatNamespace),
256 _noRexports(false),
257 _explictReExportFound(false),
258 _implicitlyLinkPublicDylibs(hoistImplicitPublicDylibs),
259 _installPathOverride(false),
260 _hasWeakExports(false),
261 _deadStrippable(false),
262 _hasPublicInstallName(false),
263 _appExtensionSafe(false),
264 _allowWeakImports(allowWeakImports),
265 _allowSimToMacOSXLinking(allowSimToMacOSX),
266 _addVersionLoadCommand(addVers)
267 {
268 }
269
270 template <typename A>
271 std::pair<bool, bool> File<A>::hasWeakDefinitionImpl(const char* name) const
272 {
273 const auto pos = _atoms.find(name);
274 if ( pos != this->_atoms.end() )
275 return std::make_pair(true, pos->second.weakDef);
276
277 // look in re-exported libraries.
278 for (const auto &dep : _dependentDylibs) {
279 if ( dep.reExport ) {
280 auto ret = dep.dylib->hasWeakDefinitionImpl(name);
281 if ( ret.first )
282 return ret;
283 }
284 }
285 return std::make_pair(false, false);
286 }
287
288 template <typename A>
289 bool File<A>::hasWeakDefinition(const char* name) const
290 {
291 // If we are supposed to ignore this export, then pretend we don't have it.
292 if ( _ignoreExports.count(name) != 0 )
293 return false;
294
295 return hasWeakDefinitionImpl(name).second;
296 }
297
298 template <typename A>
299 bool File<A>::containsOrReExports(const char* name, bool& weakDef, bool& tlv, pint_t& addr) const
300 {
301 if ( _ignoreExports.count(name) != 0 )
302 return false;
303
304 // check myself
305 const auto pos = _atoms.find(name);
306 if ( pos != _atoms.end() ) {
307 weakDef = pos->second.weakDef;
308 tlv = pos->second.tlv;
309 addr = pos->second.address;
310 return true;
311 }
312
313 // check dylibs I re-export
314 for (const auto& dep : _dependentDylibs) {
315 if ( dep.reExport && !dep.dylib->implicitlyLinked() ) {
316 if ( dep.dylib->containsOrReExports(name, weakDef, tlv, addr) )
317 return true;
318 }
319 }
320
321 return false;
322 }
323
324 template <typename A>
325 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
326 {
327 handler.doFile(*this);
328
329 // if doing flatnamespace and need all this dylib's imports resolve
330 // add atom which references alls undefines in this dylib
331 if ( _importAtom != nullptr ) {
332 handler.doAtom(*_importAtom);
333 return true;
334 }
335 return false;
336 }
337
338 template <typename A>
339 bool File<A>::justInTimeforEachAtom(const char* name, ld::File::AtomHandler& handler) const
340 {
341 // If we are supposed to ignore this export, then pretend we don't have it.
342 if ( _ignoreExports.count(name) != 0 )
343 return false;
344
345
346 AtomAndWeak bucket;
347 if ( containsOrReExports(name, bucket.weakDef, bucket.tlv, bucket.address) ) {
348 bucket.atom = new ExportAtom<A>(*this, name, bucket.weakDef, bucket.tlv, bucket.address);
349 _atoms[name] = bucket;
350 _providedAtom = true;
351 if ( _s_logHashtable )
352 fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->path());
353 // call handler with new export atom
354 handler.doAtom(*bucket.atom);
355 return true;
356 }
357
358 return false;
359 }
360
361 template <typename A>
362 void File<A>::assertNoReExportCycles(ReExportChain* prev) const
363 {
364 // recursively check my re-exported dylibs
365 ReExportChain chain = { prev, this };
366 for (const auto &dep : _dependentDylibs) {
367 if ( dep.reExport ) {
368 auto* child = dep.dylib;
369 // check child is not already in chain
370 for (auto* p = prev; p != nullptr; p = p->prev) {
371 if ( p->file == child ) {
372 throwf("cycle in dylib re-exports with %s and %s", child->path(), this->path());
373 }
374 }
375 if ( dep.dylib != nullptr )
376 dep.dylib->assertNoReExportCycles(&chain);
377 }
378 }
379 }
380
381 template <typename A>
382 void File<A>::processIndirectLibraries(ld::dylib::File::DylibHandler* handler, bool addImplicitDylibs)
383 {
384 // only do this once
385 if ( _indirectDylibsProcessed )
386 return;
387
388 const static bool log = false;
389 if ( log )
390 fprintf(stderr, "processIndirectLibraries(%s)\n", this->installPath());
391 if ( _linkingFlat ) {
392 for (auto &dep : _dependentDylibs)
393 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, false);
394 }
395 else if ( _noRexports ) {
396 // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do
397 }
398 else {
399 // two-level, might have re-exports
400 for (auto &dep : this->_dependentDylibs) {
401 if ( dep.reExport ) {
402 if ( log )
403 fprintf(stderr, "processIndirectLibraries() parent=%s, child=%s\n", this->installPath(), dep.path);
404 // a LC_REEXPORT_DYLIB, LC_SUB_UMBRELLA or LC_SUB_LIBRARY says we re-export this child
405 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, this->speculativelyLoaded());
406 if ( dep.dylib->hasPublicInstallName() && !dep.dylib->wrongOS() ) {
407 // promote this child to be automatically added as a direct dependent if this already is
408 if ( (this->explicitlyLinked() || this->implicitlyLinked()) && (strcmp(dep.path, dep.dylib->installPath()) == 0) ) {
409 if ( log )
410 fprintf(stderr, "processIndirectLibraries() implicitly linking %s\n", dep.dylib->installPath());
411 dep.dylib->setImplicitlyLinked();
412 }
413 else if ( dep.dylib->explicitlyLinked() || dep.dylib->implicitlyLinked() ) {
414 if ( log )
415 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, but child is, so no need to re-export child\n");
416 }
417 else {
418 if ( log )
419 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
420 }
421 }
422 else {
423 // add all child's symbols to me
424 if ( log )
425 fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
426 }
427 }
428 else if ( !_explictReExportFound ) {
429 // see if child contains LC_SUB_FRAMEWORK with my name
430 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, this->speculativelyLoaded());
431 const char* parentUmbrellaName = dep.dylib->parentUmbrella();
432 if ( parentUmbrellaName != nullptr ) {
433 const char* parentName = this->path();
434 const char* lastSlash = strrchr(parentName, '/');
435 if ( (lastSlash != nullptr) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) ) {
436 // add all child's symbols to me
437 dep.reExport = true;
438 if ( log )
439 fprintf(stderr, "processIndirectLibraries() umbrella=%s will re-export child=%s\n", this->installPath(), dep.path);
440 }
441 }
442 }
443 }
444 }
445
446 // check for re-export cycles
447 ReExportChain chain = { nullptr, this };
448 this->assertNoReExportCycles(&chain);
449
450 _indirectDylibsProcessed = true;
451 }
452
453 template <typename A>
454 bool File<A>::isPublicLocation(const char* path) const
455 {
456 // -no_implicit_dylibs disables this optimization
457 if ( ! _implicitlyLinkPublicDylibs )
458 return false;
459
460 // /usr/lib is a public location
461 if ( (strncmp(path, "/usr/lib/", 9) == 0) && (strchr(&path[9], '/') == nullptr) )
462 return true;
463
464 // /System/Library/Frameworks/ is a public location
465 if ( strncmp(path, "/System/Library/Frameworks/", 27) == 0 ) {
466 const char* frameworkDot = strchr(&path[27], '.');
467 // but only top level framework
468 // /System/Library/Frameworks/Foo.framework/Versions/A/Foo ==> true
469 // /System/Library/Frameworks/Foo.framework/Resources/libBar.dylib ==> false
470 // /System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Bar ==> false
471 // /System/Library/Frameworks/Foo.framework/Frameworks/Xfoo.framework/XFoo ==> false
472 if ( frameworkDot != nullptr ) {
473 int frameworkNameLen = frameworkDot - &path[27];
474 if ( strncmp(&path[strlen(path)-frameworkNameLen-1], &path[26], frameworkNameLen+1) == 0 )
475 return true;
476 }
477 }
478
479 return false;
480 }
481
482 // <rdar://problem/5529626> If only weak_import symbols are used, linker should use LD_LOAD_WEAK_DYLIB
483 template <typename A>
484 bool File<A>::allSymbolsAreWeakImported() const
485 {
486 bool foundNonWeakImport = false;
487 bool foundWeakImport = false;
488 //fprintf(stderr, "%s:\n", this->path());
489 for (const auto &it : _atoms) {
490 auto* atom = it.second.atom;
491 if ( atom != nullptr ) {
492 if ( atom->weakImported() )
493 foundWeakImport = true;
494 else
495 foundNonWeakImport = true;
496 //fprintf(stderr, " weak_import=%d, name=%s\n", atom->weakImported(), it->first);
497 }
498 }
499
500 // don't automatically weak link dylib with no imports
501 // so at least one weak import symbol and no non-weak-imported symbols must be found
502 return foundWeakImport && !foundNonWeakImport;
503 }
504
505
506 } // end namespace dylib
507 } // end namespace generic
508
509 #endif // __GENERIC_DYLIB_FILE_H__