1 //===- lib/ReaderWriter/MachO/StubsPass.cpp ---------------------*- C++ -*-===//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
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.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #include "ArchHandler.h"
17 #include "File.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"
27
28 namespace lld {
29 namespace mach_o {
30
31 //
32 // Lazy Pointer Atom created by the stubs pass.
33 //
34 class LazyPointerAtom : public SimpleDefinedAtom {
35 public:
LazyPointerAtom(const File & file,bool is64)36 LazyPointerAtom(const File &file, bool is64)
37 : SimpleDefinedAtom(file), _is64(is64) { }
38
39 ~LazyPointerAtom() override = default;
40
contentType() const41 ContentType contentType() const override {
42 return DefinedAtom::typeLazyPointer;
43 }
44
alignment() const45 Alignment alignment() const override {
46 return _is64 ? 8 : 4;
47 }
48
size() const49 uint64_t size() const override {
50 return _is64 ? 8 : 4;
51 }
52
permissions() const53 ContentPermissions permissions() const override {
54 return DefinedAtom::permRW_;
55 }
56
rawContent() const57 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());
61 }
62
63 private:
64 const bool _is64;
65 };
66
67 //
68 // NonLazyPointer (GOT) Atom created by the stubs pass.
69 //
70 class NonLazyPointerAtom : public SimpleDefinedAtom {
71 public:
NonLazyPointerAtom(const File & file,bool is64,ContentType contentType)72 NonLazyPointerAtom(const File &file, bool is64, ContentType contentType)
73 : SimpleDefinedAtom(file), _is64(is64), _contentType(contentType) { }
74
75 ~NonLazyPointerAtom() override = default;
76
contentType() const77 ContentType contentType() const override {
78 return _contentType;
79 }
80
alignment() const81 Alignment alignment() const override {
82 return _is64 ? 8 : 4;
83 }
84
size() const85 uint64_t size() const override {
86 return _is64 ? 8 : 4;
87 }
88
permissions() const89 ContentPermissions permissions() const override {
90 return DefinedAtom::permRW_;
91 }
92
rawContent() const93 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());
97 }
98
99 private:
100 const bool _is64;
101 const ContentType _contentType;
102 };
103
104 //
105 // Stub Atom created by the stubs pass.
106 //
107 class StubAtom : public SimpleDefinedAtom {
108 public:
StubAtom(const File & file,const ArchHandler::StubInfo & stubInfo)109 StubAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
110 : SimpleDefinedAtom(file), _stubInfo(stubInfo){ }
111
112 ~StubAtom() override = default;
113
contentType() const114 ContentType contentType() const override {
115 return DefinedAtom::typeStub;
116 }
117
alignment() const118 Alignment alignment() const override {
119 return 1 << _stubInfo.codeAlignment;
120 }
121
size() const122 uint64_t size() const override {
123 return _stubInfo.stubSize;
124 }
125
permissions() const126 ContentPermissions permissions() const override {
127 return DefinedAtom::permR_X;
128 }
129
rawContent() const130 ArrayRef<uint8_t> rawContent() const override {
131 return llvm::makeArrayRef(_stubInfo.stubBytes, _stubInfo.stubSize);
132 }
133
134 private:
135 const ArchHandler::StubInfo &_stubInfo;
136 };
137
138 //
139 // Stub Helper Atom created by the stubs pass.
140 //
141 class StubHelperAtom : public SimpleDefinedAtom {
142 public:
StubHelperAtom(const File & file,const ArchHandler::StubInfo & stubInfo)143 StubHelperAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
144 : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
145
146 ~StubHelperAtom() override = default;
147
contentType() const148 ContentType contentType() const override {
149 return DefinedAtom::typeStubHelper;
150 }
151
alignment() const152 Alignment alignment() const override {
153 return 1 << _stubInfo.codeAlignment;
154 }
155
size() const156 uint64_t size() const override {
157 return _stubInfo.stubHelperSize;
158 }
159
permissions() const160 ContentPermissions permissions() const override {
161 return DefinedAtom::permR_X;
162 }
163
rawContent() const164 ArrayRef<uint8_t> rawContent() const override {
165 return llvm::makeArrayRef(_stubInfo.stubHelperBytes,
166 _stubInfo.stubHelperSize);
167 }
168
169 private:
170 const ArchHandler::StubInfo &_stubInfo;
171 };
172
173 //
174 // Stub Helper Common Atom created by the stubs pass.
175 //
176 class StubHelperCommonAtom : public SimpleDefinedAtom {
177 public:
StubHelperCommonAtom(const File & file,const ArchHandler::StubInfo & stubInfo)178 StubHelperCommonAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
179 : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
180
181 ~StubHelperCommonAtom() override = default;
182
contentType() const183 ContentType contentType() const override {
184 return DefinedAtom::typeStubHelper;
185 }
186
alignment() const187 Alignment alignment() const override {
188 return 1 << _stubInfo.stubHelperCommonAlignment;
189 }
190
size() const191 uint64_t size() const override {
192 return _stubInfo.stubHelperCommonSize;
193 }
194
permissions() const195 ContentPermissions permissions() const override {
196 return DefinedAtom::permR_X;
197 }
198
rawContent() const199 ArrayRef<uint8_t> rawContent() const override {
200 return llvm::makeArrayRef(_stubInfo.stubHelperCommonBytes,
201 _stubInfo.stubHelperCommonSize);
202 }
203
204 private:
205 const ArchHandler::StubInfo &_stubInfo;
206 };
207
208 class StubsPass : public Pass {
209 public:
StubsPass(const MachOLinkingContext & context)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());
215 }
216
perform(SimpleFile & mergedFile)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();
221
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))
227 continue;
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);
233 continue;
234 }
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
238 // through a stub.
239 assert(defTarget->scope() != DefinedAtom::scopeTranslationUnit);
240 _targetToUses[target].push_back(ref);
241 }
242 }
243 }
244
245 // Exit early if no stubs needed.
246 if (_targetToUses.empty())
247 return llvm::Error::success();
248
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,
259 helperCacheNLPAtom);
260 addOptReference(
261 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
262 _stubInfo.optStubHelperCommonReferenceToCache, helperCacheNLPAtom);
263 addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
264 helperBinderNLPAtom);
265 addOptReference(
266 helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
267 _stubInfo.optStubHelperCommonReferenceToBinder, helperBinderNLPAtom);
268 mergedFile.addAtom(*helperCommonAtom);
269 mergedFile.addAtom(*helperBinderNLPAtom);
270 mergedFile.addAtom(*helperCacheNLPAtom);
271
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);
276 });
277 assert(I != mergedFile.sharedLibrary().end() &&
278 "dyld_stub_binder not found");
279 addReference(helperBinderNLPAtom, _stubInfo.nonLazyPointerReferenceToBinder, *I);
280
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);
288 });
289
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);
294 auto *lp =
295 new (_file.allocator()) LazyPointerAtom(_file, _ctx.is64Bit());
296 auto *helper = new (_file.allocator()) StubHelperAtom(_file, _stubInfo);
297
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,
305 lazyOffset);
306 addReference(helper, _stubInfo.stubHelperReferenceToHelperCommon,
307 helperCommonAtom);
308
309 mergedFile.addAtom(*stub);
310 mergedFile.addAtom(*lp);
311 mergedFile.addAtom(*helper);
312
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);
318 }
319
320 // Calculate new offset
321 lazyOffset += target->name().size() + 12;
322 }
323
324 return llvm::Error::success();
325 }
326
327 private:
noTextRelocs()328 bool noTextRelocs() {
329 return true;
330 }
331
isCallSite(const Reference & ref)332 bool isCallSite(const Reference &ref) {
333 return _archHandler.isCallSite(ref);
334 }
335
addReference(SimpleDefinedAtom * atom,const ArchHandler::ReferenceInfo & refInfo,const lld::Atom * target)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);
342 }
343
addReferenceAddend(SimpleDefinedAtom * atom,const ArchHandler::ReferenceInfo & refInfo,const lld::Atom * target,uint64_t 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);
349 }
350
addOptReference(SimpleDefinedAtom * atom,const ArchHandler::ReferenceInfo & refInfo,const ArchHandler::OptionalRefInfo & optRef,const lld::Atom * target)351 void addOptReference(SimpleDefinedAtom* atom,
352 const ArchHandler::ReferenceInfo &refInfo,
353 const ArchHandler::OptionalRefInfo &optRef,
354 const lld::Atom* target) {
355 if (!optRef.used)
356 return;
357 atom->addReference(Reference::KindNamespace::mach_o,
358 refInfo.arch, optRef.kind, optRef.offset,
359 target, optRef.addend);
360 }
361
362 typedef llvm::DenseMap<const Atom*,
363 llvm::SmallVector<const Reference *, 8>> TargetToUses;
364
365 const MachOLinkingContext &_ctx;
366 mach_o::ArchHandler &_archHandler;
367 const ArchHandler::StubInfo &_stubInfo;
368 MachOFile &_file;
369 TargetToUses _targetToUses;
370 };
371
addStubsPass(PassManager & pm,const MachOLinkingContext & ctx)372 void addStubsPass(PassManager &pm, const MachOLinkingContext &ctx) {
373 pm.add(std::unique_ptr<Pass>(new StubsPass(ctx)));
374 }
375
376 } // end namespace mach_o
377 } // end namespace lld
378