1 //===- lib/ReaderWriter/MachO/StubsPass.cpp ---------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This linker pass updates call-sites which have references to shared library
10 // atoms to instead have a reference to a stub (PLT entry) for the specified
11 // symbol. Each file format defines a subclass of StubsPass which implements
12 // the abstract methods for creating the file format specific StubAtoms.
14 //===----------------------------------------------------------------------===//
16 #include "ArchHandler.h"
18 #include "MachOPasses.h"
19 #include "lld/Common/LLVM.h"
20 #include "lld/Core/DefinedAtom.h"
21 #include "lld/Core/File.h"
22 #include "lld/Core/Reference.h"
23 #include "lld/Core/Simple.h"
24 #include "lld/ReaderWriter/MachOLinkingContext.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/SmallVector.h"
32 // Lazy Pointer Atom created by the stubs pass.
34 class LazyPointerAtom : public SimpleDefinedAtom {
36 LazyPointerAtom(const File &file, bool is64)
37 : SimpleDefinedAtom(file), _is64(is64) { }
39 ~LazyPointerAtom() override = default;
41 ContentType contentType() const override {
42 return DefinedAtom::typeLazyPointer;
45 Alignment alignment() const override {
49 uint64_t size() const override {
53 ContentPermissions permissions() const override {
54 return DefinedAtom::permRW_;
57 ArrayRef<uint8_t> rawContent() const override {
58 static const uint8_t zeros[] =
59 { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60 return llvm::makeArrayRef(zeros, size());
68 // NonLazyPointer (GOT) Atom created by the stubs pass.
70 class NonLazyPointerAtom : public SimpleDefinedAtom {
72 NonLazyPointerAtom(const File &file, bool is64, ContentType contentType)
73 : SimpleDefinedAtom(file), _is64(is64), _contentType(contentType) { }
75 ~NonLazyPointerAtom() override = default;
77 ContentType contentType() const override {
81 Alignment alignment() const override {
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());
101 const ContentType _contentType;
105 // Stub Atom created by the stubs pass.
107 class StubAtom : public SimpleDefinedAtom {
109 StubAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
110 : SimpleDefinedAtom(file), _stubInfo(stubInfo){ }
112 ~StubAtom() override = default;
114 ContentType contentType() const override {
115 return DefinedAtom::typeStub;
118 Alignment alignment() const override {
119 return 1 << _stubInfo.codeAlignment;
122 uint64_t size() const override {
123 return _stubInfo.stubSize;
126 ContentPermissions permissions() const override {
127 return DefinedAtom::permR_X;
130 ArrayRef<uint8_t> rawContent() const override {
131 return llvm::makeArrayRef(_stubInfo.stubBytes, _stubInfo.stubSize);
135 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 ~StubHelperAtom() override = default;
148 ContentType contentType() const override {
149 return DefinedAtom::typeStubHelper;
152 Alignment alignment() const override {
153 return 1 << _stubInfo.codeAlignment;
156 uint64_t size() const override {
157 return _stubInfo.stubHelperSize;
160 ContentPermissions permissions() const override {
161 return DefinedAtom::permR_X;
164 ArrayRef<uint8_t> rawContent() const override {
165 return llvm::makeArrayRef(_stubInfo.stubHelperBytes,
166 _stubInfo.stubHelperSize);
170 const ArchHandler::StubInfo &_stubInfo;
174 // Stub Helper Common Atom created by the stubs pass.
176 class StubHelperCommonAtom : public SimpleDefinedAtom {
178 StubHelperCommonAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
179 : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
181 ~StubHelperCommonAtom() override = default;
183 ContentType contentType() const override {
184 return DefinedAtom::typeStubHelper;
187 Alignment alignment() const override {
188 return 1 << _stubInfo.stubHelperCommonAlignment;
191 uint64_t size() const override {
192 return _stubInfo.stubHelperCommonSize;
195 ContentPermissions permissions() const override {
196 return DefinedAtom::permR_X;
199 ArrayRef<uint8_t> rawContent() const override {
200 return llvm::makeArrayRef(_stubInfo.stubHelperCommonBytes,
201 _stubInfo.stubHelperCommonSize);
205 const ArchHandler::StubInfo &_stubInfo;
208 class StubsPass : public Pass {
210 StubsPass(const MachOLinkingContext &context)
211 : _ctx(context), _archHandler(_ctx.archHandler()),
212 _stubInfo(_archHandler.stubInfo()),
213 _file(*_ctx.make_file<MachOFile>("<mach-o Stubs pass>")) {
214 _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
217 llvm::Error perform(SimpleFile &mergedFile) override {
218 // Skip this pass if output format uses text relocations instead of stubs.
219 if (!this->noTextRelocs())
220 return llvm::Error::success();
222 // Scan all references in all atoms.
223 for (const DefinedAtom *atom : mergedFile.defined()) {
224 for (const Reference *ref : *atom) {
225 // Look at call-sites.
226 if (!this->isCallSite(*ref))
228 const Atom *target = ref->target();
229 assert(target != nullptr);
230 if (isa<SharedLibraryAtom>(target)) {
231 // Calls to shared libraries go through stubs.
232 _targetToUses[target].push_back(ref);
235 const DefinedAtom *defTarget = dyn_cast<DefinedAtom>(target);
236 if (defTarget && defTarget->interposable() != DefinedAtom::interposeNo){
237 // Calls to interposable functions in same linkage unit must also go
239 assert(defTarget->scope() != DefinedAtom::scopeTranslationUnit);
240 _targetToUses[target].push_back(ref);
245 // Exit early if no stubs needed.
246 if (_targetToUses.empty())
247 return llvm::Error::success();
249 // First add help-common and GOT slots used by lazy binding.
250 SimpleDefinedAtom *helperCommonAtom =
251 new (_file.allocator()) StubHelperCommonAtom(_file, _stubInfo);
252 SimpleDefinedAtom *helperCacheNLPAtom =
253 new (_file.allocator()) NonLazyPointerAtom(_file, _ctx.is64Bit(),
254 _stubInfo.stubHelperImageCacheContentType);
255 SimpleDefinedAtom *helperBinderNLPAtom =
256 new (_file.allocator()) NonLazyPointerAtom(_file, _ctx.is64Bit(),
257 _stubInfo.stubHelperImageCacheContentType);
258 addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
261 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
262 _stubInfo.optStubHelperCommonReferenceToCache, helperCacheNLPAtom);
263 addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
264 helperBinderNLPAtom);
266 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
267 _stubInfo.optStubHelperCommonReferenceToBinder, helperBinderNLPAtom);
268 mergedFile.addAtom(*helperCommonAtom);
269 mergedFile.addAtom(*helperBinderNLPAtom);
270 mergedFile.addAtom(*helperCacheNLPAtom);
272 // Add reference to dyld_stub_binder in libSystem.dylib
273 auto I = llvm::find_if(
274 mergedFile.sharedLibrary(), [&](const SharedLibraryAtom *atom) {
275 return atom->name().equals(_stubInfo.binderSymbolName);
277 assert(I != mergedFile.sharedLibrary().end() &&
278 "dyld_stub_binder not found");
279 addReference(helperBinderNLPAtom, _stubInfo.nonLazyPointerReferenceToBinder, *I);
281 // Sort targets by name, so stubs and lazy pointers are consistent
282 std::vector<const Atom *> targetsNeedingStubs;
283 for (auto it : _targetToUses)
284 targetsNeedingStubs.push_back(it.first);
285 std::sort(targetsNeedingStubs.begin(), targetsNeedingStubs.end(),
286 [](const Atom * left, const Atom * right) {
287 return (left->name().compare(right->name()) < 0);
290 // Make and append stubs, lazy pointers, and helpers in alphabetical order.
291 unsigned lazyOffset = 0;
292 for (const Atom *target : targetsNeedingStubs) {
293 auto *stub = new (_file.allocator()) StubAtom(_file, _stubInfo);
295 new (_file.allocator()) LazyPointerAtom(_file, _ctx.is64Bit());
296 auto *helper = new (_file.allocator()) StubHelperAtom(_file, _stubInfo);
298 addReference(stub, _stubInfo.stubReferenceToLP, lp);
299 addOptReference(stub, _stubInfo.stubReferenceToLP,
300 _stubInfo.optStubReferenceToLP, lp);
301 addReference(lp, _stubInfo.lazyPointerReferenceToHelper, helper);
302 addReference(lp, _stubInfo.lazyPointerReferenceToFinal, target);
303 addReference(helper, _stubInfo.stubHelperReferenceToImm, helper);
304 addReferenceAddend(helper, _stubInfo.stubHelperReferenceToImm, helper,
306 addReference(helper, _stubInfo.stubHelperReferenceToHelperCommon,
309 mergedFile.addAtom(*stub);
310 mergedFile.addAtom(*lp);
311 mergedFile.addAtom(*helper);
313 // Update each reference to use stub.
314 for (const Reference *ref : _targetToUses[target]) {
315 assert(ref->target() == target);
316 // Switch call site to reference stub atom instead.
317 const_cast<Reference *>(ref)->setTarget(stub);
320 // Calculate new offset
321 lazyOffset += target->name().size() + 12;
324 return llvm::Error::success();
328 bool noTextRelocs() {
332 bool isCallSite(const Reference &ref) {
333 return _archHandler.isCallSite(ref);
336 void addReference(SimpleDefinedAtom* atom,
337 const ArchHandler::ReferenceInfo &refInfo,
338 const lld::Atom* target) {
339 atom->addReference(Reference::KindNamespace::mach_o,
340 refInfo.arch, refInfo.kind, refInfo.offset,
341 target, refInfo.addend);
344 void addReferenceAddend(SimpleDefinedAtom *atom,
345 const ArchHandler::ReferenceInfo &refInfo,
346 const lld::Atom *target, uint64_t addend) {
347 atom->addReference(Reference::KindNamespace::mach_o, refInfo.arch,
348 refInfo.kind, refInfo.offset, target, addend);
351 void addOptReference(SimpleDefinedAtom* atom,
352 const ArchHandler::ReferenceInfo &refInfo,
353 const ArchHandler::OptionalRefInfo &optRef,
354 const lld::Atom* target) {
357 atom->addReference(Reference::KindNamespace::mach_o,
358 refInfo.arch, optRef.kind, optRef.offset,
359 target, optRef.addend);
362 typedef llvm::DenseMap<const Atom*,
363 llvm::SmallVector<const Reference *, 8>> TargetToUses;
365 const MachOLinkingContext &_ctx;
366 mach_o::ArchHandler &_archHandler;
367 const ArchHandler::StubInfo &_stubInfo;
369 TargetToUses _targetToUses;
372 void addStubsPass(PassManager &pm, const MachOLinkingContext &ctx) {
373 pm.add(std::unique_ptr<Pass>(new StubsPass(ctx)));
376 } // end namespace mach_o
377 } // end namespace lld