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, const ld::VersionSet& platforms,
127 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 uint8_t swiftVersion() const override final { return _swiftVersion; }
134 virtual ld::Bitcode* getBitcode() const override final { return _bitcode.get(); }
137 // overrides of ld::dylib::File
138 virtual void processIndirectLibraries(ld::dylib::File::DylibHandler*, bool addImplicitDylibs) override;
139 virtual bool providedExportAtom() const override final { return _providedAtom; }
140 virtual const char* parentUmbrella() const override final { return _parentUmbrella; }
141 virtual const std::vector<const char*>* allowableClients() const override final { return _allowableClients.empty() ? nullptr : &_allowableClients; }
142 virtual const std::vector<const char*>& rpaths() const override final { return _rpaths; }
143 virtual bool hasWeakExternals() const override final { return _hasWeakExports; }
144 virtual bool deadStrippable() const override final { return _deadStrippable; }
145 virtual bool hasWeakDefinition(const char* name) const override final;
146 virtual bool hasPublicInstallName() const override final { return _hasPublicInstallName; }
147 virtual bool allSymbolsAreWeakImported() const override final;
148 virtual bool installPathVersionSpecific() const override final { return _installPathOverride; }
149 virtual bool appExtensionSafe() const override final { return _appExtensionSafe; };
152 bool wrongOS() const { return _wrongOS; }
155 using pint_t = typename A::P::uint_t;
157 friend class ExportAtom<A>;
158 friend class ImportAtom<A>;
161 std::size_t operator()(const char* __s) const {
162 unsigned long __h = 0;
164 __h = 5 * __h + *__s;
170 struct AtomAndWeak { ld::Atom* atom; bool weakDef; bool tlv; pint_t address; };
176 Dependent(const char* path, bool reExport)
177 : path(path), dylib(nullptr), reExport(reExport) {}
179 struct ReExportChain { ReExportChain* prev; const File* file; };
182 using NameToAtomMap = std::unordered_map<const char*, AtomAndWeak, ld::CStringHash, ld::CStringEquals>;
183 using NameSet = std::unordered_set<const char*, CStringHash, ld::CStringEquals>;
185 std::pair<bool, bool> hasWeakDefinitionImpl(const char* name) const;
186 bool containsOrReExports(const char* name, bool& weakDef, bool& tlv, pint_t& addr) const;
187 void assertNoReExportCycles(ReExportChain*) const;
190 bool isPublicLocation(const char* path) const;
193 ld::Section _importProxySection;
194 ld::Section _flatDummySection;
195 mutable bool _providedAtom;
196 bool _indirectDylibsProcessed;
199 mutable NameToAtomMap _atoms;
200 NameSet _ignoreExports;
201 std::vector<Dependent> _dependentDylibs;
202 ImportAtom<A>* _importAtom;
203 std::vector<const char*> _allowableClients;
204 std::vector<const char*> _rpaths;
205 const char* _parentUmbrella;
206 std::unique_ptr<ld::Bitcode> _bitcode;
207 ld::VersionSet _platforms;
208 uint8_t _swiftVersion;
212 bool _explictReExportFound;
213 bool _implicitlyLinkPublicDylibs;
214 bool _installPathOverride;
215 bool _hasWeakExports;
216 bool _deadStrippable;
217 bool _hasPublicInstallName;
218 bool _appExtensionSafe;
219 const bool _allowWeakImports;
220 const bool _allowSimToMacOSXLinking;
221 const bool _addVersionLoadCommand;
223 static bool _s_logHashtable;
226 template <typename A>
227 bool File<A>::_s_logHashtable = false;
229 template <typename A>
230 File<A>::File(const char* path, time_t mTime, ld::File::Ordinal ord, const ld::VersionSet& platforms,
231 bool allowWeakImports, bool linkingFlatNamespace,
232 bool hoistImplicitPublicDylibs,
233 bool allowSimToMacOSX, bool addVers)
234 : ld::dylib::File(path, mTime, ord),
235 _importProxySection("__TEXT", "__import", ld::Section::typeImportProxies, true),
236 _flatDummySection("__LINKEDIT", "__flat_dummy", ld::Section::typeLinkEdit, true),
237 _providedAtom(false),
238 _indirectDylibsProcessed(false),
239 _importAtom(nullptr),
240 _parentUmbrella(nullptr),
241 _platforms(platforms),
244 _linkingFlat(linkingFlatNamespace),
246 _explictReExportFound(false),
247 _implicitlyLinkPublicDylibs(hoistImplicitPublicDylibs),
248 _installPathOverride(false),
249 _hasWeakExports(false),
250 _deadStrippable(false),
251 _hasPublicInstallName(false),
252 _appExtensionSafe(false),
253 _allowWeakImports(allowWeakImports),
254 _allowSimToMacOSXLinking(allowSimToMacOSX),
255 _addVersionLoadCommand(addVers)
259 template <typename A>
260 std::pair<bool, bool> File<A>::hasWeakDefinitionImpl(const char* name) const
262 const auto pos = _atoms.find(name);
263 if ( pos != this->_atoms.end() )
264 return std::make_pair(true, pos->second.weakDef);
266 // look in re-exported libraries.
267 for (const auto &dep : _dependentDylibs) {
268 if ( dep.reExport ) {
269 auto ret = dep.dylib->hasWeakDefinitionImpl(name);
274 return std::make_pair(false, false);
277 template <typename A>
278 bool File<A>::hasWeakDefinition(const char* name) const
280 // If we are supposed to ignore this export, then pretend we don't have it.
281 if ( _ignoreExports.count(name) != 0 )
284 return hasWeakDefinitionImpl(name).second;
287 template <typename A>
288 bool File<A>::containsOrReExports(const char* name, bool& weakDef, bool& tlv, pint_t& addr) const
290 if ( _ignoreExports.count(name) != 0 )
294 const auto pos = _atoms.find(name);
295 if ( pos != _atoms.end() ) {
296 weakDef = pos->second.weakDef;
297 tlv = pos->second.tlv;
298 addr = pos->second.address;
302 // check dylibs I re-export
303 for (const auto& dep : _dependentDylibs) {
304 if ( dep.reExport && !dep.dylib->implicitlyLinked() ) {
305 if ( dep.dylib->containsOrReExports(name, weakDef, tlv, addr) )
313 template <typename A>
314 bool File<A>::forEachAtom(ld::File::AtomHandler& handler) const
316 handler.doFile(*this);
318 // if doing flatnamespace and need all this dylib's imports resolve
319 // add atom which references alls undefines in this dylib
320 if ( _importAtom != nullptr ) {
321 handler.doAtom(*_importAtom);
327 template <typename A>
328 bool File<A>::justInTimeforEachAtom(const char* name, ld::File::AtomHandler& handler) const
330 // If we are supposed to ignore this export, then pretend we don't have it.
331 if ( _ignoreExports.count(name) != 0 )
336 if ( containsOrReExports(name, bucket.weakDef, bucket.tlv, bucket.address) ) {
337 bucket.atom = new ExportAtom<A>(*this, name, bucket.weakDef, bucket.tlv, bucket.address);
338 _atoms[name] = bucket;
339 _providedAtom = true;
340 if ( _s_logHashtable )
341 fprintf(stderr, "getJustInTimeAtomsFor: %s found in %s\n", name, this->path());
342 // call handler with new export atom
343 handler.doAtom(*bucket.atom);
350 template <typename A>
351 void File<A>::assertNoReExportCycles(ReExportChain* prev) const
353 // recursively check my re-exported dylibs
354 ReExportChain chain = { prev, this };
355 for (const auto &dep : _dependentDylibs) {
356 if ( dep.reExport ) {
357 auto* child = dep.dylib;
358 // check child is not already in chain
359 for (auto* p = prev; p != nullptr; p = p->prev) {
360 if ( p->file == child ) {
361 throwf("cycle in dylib re-exports with %s and %s", child->path(), this->path());
364 if ( dep.dylib != nullptr )
365 dep.dylib->assertNoReExportCycles(&chain);
370 template <typename A>
371 void File<A>::processIndirectLibraries(ld::dylib::File::DylibHandler* handler, bool addImplicitDylibs)
374 if ( _indirectDylibsProcessed )
377 const static bool log = false;
379 fprintf(stderr, "processIndirectLibraries(%s)\n", this->installPath());
380 if ( _linkingFlat ) {
381 for (auto &dep : _dependentDylibs)
382 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, false);
384 else if ( _noRexports ) {
385 // MH_NO_REEXPORTED_DYLIBS bit set, then nothing to do
388 // two-level, might have re-exports
389 for (auto &dep : this->_dependentDylibs) {
390 if ( dep.reExport ) {
392 fprintf(stderr, "processIndirectLibraries() parent=%s, child=%s\n", this->installPath(), dep.path);
393 // a LC_REEXPORT_DYLIB, LC_SUB_UMBRELLA or LC_SUB_LIBRARY says we re-export this child
394 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, this->speculativelyLoaded());
395 if ( dep.dylib->hasPublicInstallName() && !dep.dylib->wrongOS() ) {
396 // promote this child to be automatically added as a direct dependent if this already is
397 if ( (this->explicitlyLinked() || this->implicitlyLinked()) && (strcmp(dep.path, dep.dylib->installPath()) == 0) ) {
399 fprintf(stderr, "processIndirectLibraries() implicitly linking %s\n", dep.dylib->installPath());
400 dep.dylib->setImplicitlyLinked();
402 else if ( dep.dylib->explicitlyLinked() || dep.dylib->implicitlyLinked() ) {
404 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, but child is, so no need to re-export child\n");
408 fprintf(stderr, "processIndirectLibraries() parent is not directly linked, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
412 // add all child's symbols to me
414 fprintf(stderr, "processIndirectLibraries() child is not public, so parent=%s will re-export child=%s\n", this->installPath(), dep.path);
417 else if ( !_explictReExportFound ) {
418 // see if child contains LC_SUB_FRAMEWORK with my name
419 dep.dylib = (File<A>*)handler->findDylib(dep.path, this, this->speculativelyLoaded());
420 const char* parentUmbrellaName = dep.dylib->parentUmbrella();
421 if ( parentUmbrellaName != nullptr ) {
422 const char* parentName = this->path();
423 const char* lastSlash = strrchr(parentName, '/');
424 if ( (lastSlash != nullptr) && (strcmp(&lastSlash[1], parentUmbrellaName) == 0) ) {
425 // add all child's symbols to me
428 fprintf(stderr, "processIndirectLibraries() umbrella=%s will re-export child=%s\n", this->installPath(), dep.path);
435 // check for re-export cycles
436 ReExportChain chain = { nullptr, this };
437 this->assertNoReExportCycles(&chain);
439 _indirectDylibsProcessed = true;
442 template <typename A>
443 bool File<A>::isPublicLocation(const char* path) const
445 // -no_implicit_dylibs disables this optimization
446 if ( ! _implicitlyLinkPublicDylibs )
449 // /usr/lib is a public location
450 if ( (strncmp(path, "/usr/lib/", 9) == 0) && (strchr(&path[9], '/') == nullptr) )
453 // /System/Library/Frameworks/ is a public location
454 if ( strncmp(path, "/System/Library/Frameworks/", 27) == 0 ) {
455 const char* frameworkDot = strchr(&path[27], '.');
456 // but only top level framework
457 // /System/Library/Frameworks/Foo.framework/Versions/A/Foo ==> true
458 // /System/Library/Frameworks/Foo.framework/Resources/libBar.dylib ==> false
459 // /System/Library/Frameworks/Foo.framework/Frameworks/Bar.framework/Bar ==> false
460 // /System/Library/Frameworks/Foo.framework/Frameworks/Xfoo.framework/XFoo ==> false
461 if ( frameworkDot != nullptr ) {
462 int frameworkNameLen = frameworkDot - &path[27];
463 if ( strncmp(&path[strlen(path)-frameworkNameLen-1], &path[26], frameworkNameLen+1) == 0 )
471 // <rdar://problem/5529626> If only weak_import symbols are used, linker should use LD_LOAD_WEAK_DYLIB
472 template <typename A>
473 bool File<A>::allSymbolsAreWeakImported() const
475 bool foundNonWeakImport = false;
476 bool foundWeakImport = false;
477 //fprintf(stderr, "%s:\n", this->path());
478 for (const auto &it : _atoms) {
479 auto* atom = it.second.atom;
480 if ( atom != nullptr ) {
481 if ( atom->weakImported() )
482 foundWeakImport = true;
484 foundNonWeakImport = true;
485 //fprintf(stderr, " weak_import=%d, name=%s\n", atom->weakImported(), it->first);
489 // don't automatically weak link dylib with no imports
490 // so at least one weak import symbol and no non-weak-imported symbols must be found
491 return foundWeakImport && !foundNonWeakImport;
495 } // end namespace dylib
496 } // end namespace generic
498 #endif // __GENERIC_DYLIB_FILE_H__