1 /* -*- mode: C++; c-basic-offset: 4; tab-width: 4 -*-
3 * Copyright (c) 2015 Apple Inc. All rights reserved.
5 * @APPLE_LICENSE_HEADER_START@
7 * This file contains Original Code and/or Modifications of Original Code
8 * as defined in and that are subject to the Apple Public Source License
9 * Version 2.0 (the 'License'). You may not use this file except in
10 * compliance with the License. Please obtain a copy of the License at
11 * http://www.opensource.apple.com/apsl/ and read it before using this
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
19 * Please see the License for the specific language governing rights and
20 * limitations under the License.
22 * @APPLE_LICENSE_HEADER_END@
25 #ifndef __GENERIC_DYLIB_FILE_H__
26 #define __GENERIC_DYLIB_FILE_H__
30 #include <unordered_map>
31 #include <unordered_set>
37 template <typename A> class File;
40 // An ExportAtom has no content. It exists so that the linker can track which
41 // imported symbols came from which dynamic libraries.
44 class ExportAtom final : public ld::Atom
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)),
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 { }
66 virtual void setScope(Scope) { }
69 using pint_t = typename A::P::uint_t;
71 virtual ~ExportAtom() {}
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
84 class ImportAtom final : public ld::Atom
87 ImportAtom(File<A>& f, std::vector<const char*>& imports);
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 { }
98 virtual void setScope(Scope) { }
101 virtual ~ImportAtom() {}
104 mutable std::vector<ld::Fixup> _undefs;
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)),
114 for(auto *name : imports)
115 _undefs.emplace_back(0, ld::Fixup::k1of1, ld::Fixup::kindNone, false, strdup(name));
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.
122 template <typename A>
123 class File : public ld::dylib::File
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);
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 platformLoadCommand() const override final { return _platformInDylib; }
137 virtual ld::Bitcode* getBitcode() const override final { return _bitcode.get(); }
140 // overrides of ld::dylib::File
141 virtual void processIndirectLibraries(ld::dylib::File::DylibHandler*, bool addImplicitDylibs) override final;
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; };
155 bool wrongOS() const { return _wrongOS; }
158 using pint_t = typename A::P::uint_t;
160 friend class ExportAtom<A>;
161 friend class ImportAtom<A>;
164 std::size_t operator()(const char* __s) const {
165 unsigned long __h = 0;
167 __h = 5 * __h + *__s;
173 struct AtomAndWeak { ld::Atom* atom; bool weakDef; bool tlv; pint_t address; };
179 Dependent(const char* path, bool reExport)
180 : path(path), dylib(nullptr), reExport(reExport) {}
182 struct ReExportChain { ReExportChain* prev; const File* file; };
185 using NameToAtomMap = std::unordered_map<const char*, AtomAndWeak, ld::CStringHash, ld::CStringEquals>;
186 using NameSet = std::unordered_set<const char*, CStringHash, ld::CStringEquals>;
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;
193 bool isPublicLocation(const char* path) const;
196 ld::Section _importProxySection;
197 ld::Section _flatDummySection;
198 mutable bool _providedAtom;
199 bool _indirectDylibsProcessed;
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;
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;
230 static bool _s_logHashtable;
233 template <typename A>
234 bool File<A>::_s_logHashtable = false;
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),
249 _objcConstraint(ld::File::objcConstraintNone),
250 _linkMinOSVersion(linkMinOSVersion),
251 _minVersionInDylib(0),
252 _platformInDylib(Options::kPlatformUnknown),
255 _linkingFlat(linkingFlatNamespace),
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)
270 template <typename A>
271 std::pair<bool, bool> File<A>::hasWeakDefinitionImpl(const char* name) const
273 const auto pos = _atoms.find(name);
274 if ( pos != this->_atoms.end() )
275 return std::make_pair(true, pos->second.weakDef);
277 // look in re-exported libraries.
278 for (const auto &dep : _dependentDylibs) {
279 if ( dep.reExport ) {
280 auto ret = dep.dylib->hasWeakDefinitionImpl(name);
285 return std::make_pair(false, false);
288 template <typename A>
289 bool File<A>::hasWeakDefinition(const char* name) const
291 // If we are supposed to ignore this export, then pretend we don't have it.
292 if ( _ignoreExports.count(name) != 0 )
295 return hasWeakDefinitionImpl(name).second;
298 template <typename A>
299 bool File<A>::containsOrReExports(const char* name, bool& weakDef, bool& tlv, pint_t& addr) const
301 if ( _ignoreExports.count(name) != 0 )
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;
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) )
324 template <typename A>
325 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
327 handler.doFile(*this);
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);
338 template <typename A>
339 bool File<A>::justInTimeforEachAtom(const char* name, ld::File::AtomHandler& handler) const
341 // If we are supposed to ignore this export, then pretend we don't have it.
342 if ( _ignoreExports.count(name) != 0 )
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);
361 template <typename A>
362 void File<A>::assertNoReExportCycles(ReExportChain* prev) const
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());
375 if ( dep.dylib != nullptr )
376 dep.dylib->assertNoReExportCycles(&chain);
381 template <typename A>
382 void File<A>::processIndirectLibraries(ld::dylib::File::DylibHandler* handler, bool addImplicitDylibs)
385 if ( _indirectDylibsProcessed )
388 const static bool log = false;
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);
395 else if ( _noRexports ) {
396 // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do
399 // two-level, might have re-exports
400 for (auto &dep : this->_dependentDylibs) {
401 if ( dep.reExport ) {
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) ) {
410 fprintf(stderr, "processIndirectLibraries() implicitly linking %s\n", dep.dylib->installPath());
411 dep.dylib->setImplicitlyLinked();
413 else if ( dep.dylib->explicitlyLinked() || dep.dylib->implicitlyLinked() ) {
415 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, but child is, so no need to re-export child\n");
419 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
423 // add all child's symbols to me
425 fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
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
439 fprintf(stderr, "processIndirectLibraries() umbrella=%s will re-export child=%s\n", this->installPath(), dep.path);
446 // check for re-export cycles
447 ReExportChain chain = { nullptr, this };
448 this->assertNoReExportCycles(&chain);
450 _indirectDylibsProcessed = true;
453 template <typename A>
454 bool File<A>::isPublicLocation(const char* path) const
456 // -no_implicit_dylibs disables this optimization
457 if ( ! _implicitlyLinkPublicDylibs )
460 // /usr/lib is a public location
461 if ( (strncmp(path, "/usr/lib/", 9) == 0) && (strchr(&path[9], '/') == nullptr) )
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 )
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
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;
495 foundNonWeakImport = true;
496 //fprintf(stderr, " weak_import=%d, name=%s\n", atom->weakImported(), it->first);
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;
506 } // end namespace dylib
507 } // end namespace generic
509 #endif // __GENERIC_DYLIB_FILE_H__