1 //===- lib/ReaderWriter/MachO/StubsPass.cpp -------------------------------===//
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This linker pass updates call-sites which have references to shared library
11 // atoms to instead have a reference to a stub (PLT entry) for the specified
12 // symbol. Each file format defines a subclass of StubsPass which implements
13 // the abstract methods for creating the file format specific StubAtoms.
15 //===----------------------------------------------------------------------===//
17 #include "ArchHandler.h"
19 #include "MachOPasses.h"
20 #include "lld/Core/DefinedAtom.h"
21 #include "lld/Core/File.h"
22 #include "lld/Core/LLVM.h"
23 #include "lld/Core/Reference.h"
24 #include "lld/Core/Simple.h"
25 #include "lld/ReaderWriter/MachOLinkingContext.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/SmallVector.h"
35 // Lazy Pointer Atom created by the stubs pass.
37 class LazyPointerAtom : public SimpleDefinedAtom {
39 LazyPointerAtom(const File &file, bool is64)
40 : SimpleDefinedAtom(file), _is64(is64) { }
42 ContentType contentType() const override {
43 return DefinedAtom::typeLazyPointer;
46 Alignment alignment() const override {
47 return Alignment(_is64 ? 3 : 2);
50 uint64_t size() const override {
54 ContentPermissions permissions() const override {
55 return DefinedAtom::permRW_;
58 ArrayRef<uint8_t> rawContent() const override {
59 static const uint8_t zeros[] =
60 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
61 return llvm::makeArrayRef(zeros, size());
70 // NonLazyPointer (GOT) Atom created by the stubs pass.
72 class NonLazyPointerAtom : public SimpleDefinedAtom {
74 NonLazyPointerAtom(const File &file, bool is64)
75 : SimpleDefinedAtom(file), _is64(is64) { }
77 ContentType contentType() const override {
78 return DefinedAtom::typeGOT;
81 Alignment alignment() const override {
82 return Alignment(_is64 ? 3 : 2);
85 uint64_t size() const override {
89 ContentPermissions permissions() const override {
90 return DefinedAtom::permRW_;
93 ArrayRef<uint8_t> rawContent() const override {
94 static const uint8_t zeros[] =
95 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
96 return llvm::makeArrayRef(zeros, size());
106 // Stub Atom created by the stubs pass.
108 class StubAtom : public SimpleDefinedAtom {
110 StubAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
111 : SimpleDefinedAtom(file), _stubInfo(stubInfo){ }
113 ContentType contentType() const override {
114 return DefinedAtom::typeStub;
117 Alignment alignment() const override {
118 return Alignment(_stubInfo.codeAlignment);
121 uint64_t size() const override {
122 return _stubInfo.stubSize;
125 ContentPermissions permissions() const override {
126 return DefinedAtom::permR_X;
129 ArrayRef<uint8_t> rawContent() const override {
130 return llvm::makeArrayRef(_stubInfo.stubBytes, _stubInfo.stubSize);
134 const ArchHandler::StubInfo &_stubInfo;
139 // Stub Helper Atom created by the stubs pass.
141 class StubHelperAtom : public SimpleDefinedAtom {
143 StubHelperAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
144 : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
146 ContentType contentType() const override {
147 return DefinedAtom::typeStubHelper;
150 Alignment alignment() const override {
151 return Alignment(_stubInfo.codeAlignment);
154 uint64_t size() const override {
155 return _stubInfo.stubHelperSize;
158 ContentPermissions permissions() const override {
159 return DefinedAtom::permR_X;
162 ArrayRef<uint8_t> rawContent() const override {
163 return llvm::makeArrayRef(_stubInfo.stubHelperBytes,
164 _stubInfo.stubHelperSize);
168 const ArchHandler::StubInfo &_stubInfo;
173 // Stub Helper Common Atom created by the stubs pass.
175 class StubHelperCommonAtom : public SimpleDefinedAtom {
177 StubHelperCommonAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
178 : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
180 ContentType contentType() const override {
181 return DefinedAtom::typeStubHelper;
184 Alignment alignment() const override {
185 return Alignment(_stubInfo.codeAlignment);
188 uint64_t size() const override {
189 return _stubInfo.stubHelperCommonSize;
192 ContentPermissions permissions() const override {
193 return DefinedAtom::permR_X;
196 ArrayRef<uint8_t> rawContent() const override {
197 return llvm::makeArrayRef(_stubInfo.stubHelperCommonBytes,
198 _stubInfo.stubHelperCommonSize);
202 const ArchHandler::StubInfo &_stubInfo;
206 class StubsPass : public Pass {
208 StubsPass(const MachOLinkingContext &context)
209 : _context(context), _archHandler(_context.archHandler()),
210 _stubInfo(_archHandler.stubInfo()), _file("<mach-o Stubs pass>") { }
213 void perform(std::unique_ptr<MutableFile> &mergedFile) override {
214 // Skip this pass if output format uses text relocations instead of stubs.
215 if (!this->noTextRelocs())
218 // Scan all references in all atoms.
219 for (const DefinedAtom *atom : mergedFile->defined()) {
220 for (const Reference *ref : *atom) {
221 // Look at call-sites.
222 if (!this->isCallSite(*ref))
224 const Atom *target = ref->target();
225 assert(target != nullptr);
226 if (isa<SharedLibraryAtom>(target)) {
227 // Calls to shared libraries go through stubs.
228 _targetToUses[target].push_back(ref);
231 const DefinedAtom *defTarget = dyn_cast<DefinedAtom>(target);
232 if (defTarget && defTarget->interposable() != DefinedAtom::interposeNo){
233 // Calls to interposable functions in same linkage unit must also go
235 assert(defTarget->scope() != DefinedAtom::scopeTranslationUnit);
236 _targetToUses[target].push_back(ref);
241 // Exit early if no stubs needed.
242 if (_targetToUses.empty())
245 // First add help-common and GOT slots used by lazy binding.
246 SimpleDefinedAtom *helperCommonAtom =
247 new (_file.allocator()) StubHelperCommonAtom(_file, _stubInfo);
248 SimpleDefinedAtom *helperCacheNLPAtom =
249 new (_file.allocator()) NonLazyPointerAtom(_file, _context.is64Bit());
250 SimpleDefinedAtom *helperBinderNLPAtom =
251 new (_file.allocator()) NonLazyPointerAtom(_file, _context.is64Bit());
252 addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
255 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
256 _stubInfo.optStubHelperCommonReferenceToCache, helperCacheNLPAtom);
257 addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
258 helperBinderNLPAtom);
260 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
261 _stubInfo.optStubHelperCommonReferenceToBinder, helperBinderNLPAtom);
262 mergedFile->addAtom(*helperCommonAtom);
263 mergedFile->addAtom(*helperBinderNLPAtom);
264 mergedFile->addAtom(*helperCacheNLPAtom);
266 // Add reference to dyld_stub_binder in libSystem.dylib
267 auto I = std::find_if(
268 mergedFile->sharedLibrary().begin(), mergedFile->sharedLibrary().end(),
269 [&](const SharedLibraryAtom *atom) {
270 return atom->name().equals(_stubInfo.binderSymbolName);
272 assert(I != mergedFile->sharedLibrary().end() && "dyld_stub_binder not found");
273 addReference(helperBinderNLPAtom, _stubInfo.nonLazyPointerReferenceToBinder, *I);
275 // Sort targets by name, so stubs and lazy pointers are consistent
276 std::vector<const Atom *> targetsNeedingStubs;
277 for (auto it : _targetToUses)
278 targetsNeedingStubs.push_back(it.first);
279 std::sort(targetsNeedingStubs.begin(), targetsNeedingStubs.end(),
280 [](const Atom * left, const Atom * right) {
281 return (left->name().compare(right->name()) < 0);
284 // Make and append stubs, lazy pointers, and helpers in alphabetical order.
285 unsigned lazyOffset = 0;
286 for (const Atom *target : targetsNeedingStubs) {
287 StubAtom *stub = new (_file.allocator()) StubAtom(_file, _stubInfo);
288 LazyPointerAtom *lp =
289 new (_file.allocator()) LazyPointerAtom(_file, _context.is64Bit());
290 StubHelperAtom *helper =
291 new (_file.allocator()) StubHelperAtom(_file, _stubInfo);
293 addReference(stub, _stubInfo.stubReferenceToLP, lp);
294 addOptReference(stub, _stubInfo.stubReferenceToLP,
295 _stubInfo.optStubReferenceToLP, lp);
296 addReference(lp, _stubInfo.lazyPointerReferenceToHelper, helper);
297 addReference(lp, _stubInfo.lazyPointerReferenceToFinal, target);
298 addReference(helper, _stubInfo.stubHelperReferenceToImm, helper);
299 addReferenceAddend(helper, _stubInfo.stubHelperReferenceToImm, helper,
301 addReference(helper, _stubInfo.stubHelperReferenceToHelperCommon,
304 mergedFile->addAtom(*stub);
305 mergedFile->addAtom(*lp);
306 mergedFile->addAtom(*helper);
308 // Update each reference to use stub.
309 for (const Reference *ref : _targetToUses[target]) {
310 assert(ref->target() == target);
311 // Switch call site to reference stub atom instead.
312 const_cast<Reference *>(ref)->setTarget(stub);
315 // Calculate new offset
316 lazyOffset += target->name().size() + 12;
322 bool noTextRelocs() {
326 bool isCallSite(const Reference &ref) {
327 return _archHandler.isCallSite(ref);
330 void addReference(SimpleDefinedAtom* atom,
331 const ArchHandler::ReferenceInfo &refInfo,
332 const lld::Atom* target) {
333 atom->addReference(Reference::KindNamespace::mach_o,
334 refInfo.arch, refInfo.kind, refInfo.offset,
335 target, refInfo.addend);
338 void addReferenceAddend(SimpleDefinedAtom *atom,
339 const ArchHandler::ReferenceInfo &refInfo,
340 const lld::Atom *target, uint64_t addend) {
341 atom->addReference(Reference::KindNamespace::mach_o, refInfo.arch,
342 refInfo.kind, refInfo.offset, target, addend);
345 void addOptReference(SimpleDefinedAtom* atom,
346 const ArchHandler::ReferenceInfo &refInfo,
347 const ArchHandler::OptionalRefInfo &optRef,
348 const lld::Atom* target) {
351 atom->addReference(Reference::KindNamespace::mach_o,
352 refInfo.arch, optRef.kind, optRef.offset,
353 target, optRef.addend);
356 typedef llvm::DenseMap<const Atom*,
357 llvm::SmallVector<const Reference *, 8>> TargetToUses;
359 const MachOLinkingContext &_context;
360 mach_o::ArchHandler &_archHandler;
361 const ArchHandler::StubInfo &_stubInfo;
363 TargetToUses _targetToUses;
368 void addStubsPass(PassManager &pm, const MachOLinkingContext &ctx) {
369 pm.add(std::unique_ptr<Pass>(new StubsPass(ctx)));
372 } // end namespace mach_o
373 } // end namespace lld