Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/lib/ReaderWriter/MachO/StubsPass.cpp
Line
Count
Source (jump to first uncovered line)
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:
36
  LazyPointerAtom(const File &file, bool is64)
37
23
    : SimpleDefinedAtom(file), _is64(is64) { }
38
39
23
  ~LazyPointerAtom() override = default;
40
41
211
  ContentType contentType() const override {
42
211
    return DefinedAtom::typeLazyPointer;
43
211
  }
44
45
23
  Alignment alignment() const override {
46
23
    return _is64 ? 
818
:
45
;
47
23
  }
48
49
115
  uint64_t size() const override {
50
115
    return _is64 ? 
890
:
425
;
51
115
  }
52
53
0
  ContentPermissions permissions() const override {
54
0
    return DefinedAtom::permRW_;
55
0
  }
56
57
46
  ArrayRef<uint8_t> rawContent() const override {
58
46
    static const uint8_t zeros[] =
59
46
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
60
46
    return llvm::makeArrayRef(zeros, size());
61
46
  }
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:
72
  NonLazyPointerAtom(const File &file, bool is64, ContentType contentType)
73
34
    : SimpleDefinedAtom(file), _is64(is64), _contentType(contentType) { }
74
75
34
  ~NonLazyPointerAtom() override = default;
76
77
290
  ContentType contentType() const override {
78
290
    return _contentType;
79
290
  }
80
81
34
  Alignment alignment() const override {
82
34
    return _is64 ? 
824
:
410
;
83
34
  }
84
85
170
  uint64_t size() const override {
86
170
    return _is64 ? 
8120
:
450
;
87
170
  }
88
89
0
  ContentPermissions permissions() const override {
90
0
    return DefinedAtom::permRW_;
91
0
  }
92
93
68
  ArrayRef<uint8_t> rawContent() const override {
94
68
    static const uint8_t zeros[] =
95
68
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
96
68
    return llvm::makeArrayRef(zeros, size());
97
68
  }
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:
109
  StubAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
110
23
      : SimpleDefinedAtom(file), _stubInfo(stubInfo){ }
111
112
23
  ~StubAtom() override = default;
113
114
211
  ContentType contentType() const override {
115
211
    return DefinedAtom::typeStub;
116
211
  }
117
118
23
  Alignment alignment() const override {
119
23
    return 1 << _stubInfo.codeAlignment;
120
23
  }
121
122
69
  uint64_t size() const override {
123
69
    return _stubInfo.stubSize;
124
69
  }
125
126
0
  ContentPermissions permissions() const override {
127
0
    return DefinedAtom::permR_X;
128
0
  }
129
130
46
  ArrayRef<uint8_t> rawContent() const override {
131
46
    return llvm::makeArrayRef(_stubInfo.stubBytes, _stubInfo.stubSize);
132
46
  }
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:
143
  StubHelperAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
144
23
      : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
145
146
23
  ~StubHelperAtom() override = default;
147
148
211
  ContentType contentType() const override {
149
211
    return DefinedAtom::typeStubHelper;
150
211
  }
151
152
23
  Alignment alignment() const override {
153
23
    return 1 << _stubInfo.codeAlignment;
154
23
  }
155
156
69
  uint64_t size() const override {
157
69
    return _stubInfo.stubHelperSize;
158
69
  }
159
160
0
  ContentPermissions permissions() const override {
161
0
    return DefinedAtom::permR_X;
162
0
  }
163
164
46
  ArrayRef<uint8_t> rawContent() const override {
165
46
    return llvm::makeArrayRef(_stubInfo.stubHelperBytes,
166
46
                              _stubInfo.stubHelperSize);
167
46
  }
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:
178
  StubHelperCommonAtom(const File &file, const ArchHandler::StubInfo &stubInfo)
179
17
      : SimpleDefinedAtom(file), _stubInfo(stubInfo) { }
180
181
17
  ~StubHelperCommonAtom() override = default;
182
183
145
  ContentType contentType() const override {
184
145
    return DefinedAtom::typeStubHelper;
185
145
  }
186
187
17
  Alignment alignment() const override {
188
17
    return 1 << _stubInfo.stubHelperCommonAlignment;
189
17
  }
190
191
51
  uint64_t size() const override {
192
51
    return _stubInfo.stubHelperCommonSize;
193
51
  }
194
195
0
  ContentPermissions permissions() const override {
196
0
    return DefinedAtom::permR_X;
197
0
  }
198
199
34
  ArrayRef<uint8_t> rawContent() const override {
200
34
    return llvm::makeArrayRef(_stubInfo.stubHelperCommonBytes,
201
34
                        _stubInfo.stubHelperCommonSize);
202
34
  }
203
204
private:
205
  const ArchHandler::StubInfo   &_stubInfo;
206
};
207
208
class StubsPass : public Pass {
209
public:
210
  StubsPass(const MachOLinkingContext &context)
211
      : _ctx(context), _archHandler(_ctx.archHandler()),
212
        _stubInfo(_archHandler.stubInfo()),
213
97
        _file(*_ctx.make_file<MachOFile>("<mach-o Stubs pass>")) {
214
97
    _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
215
97
  }
216
217
97
  llvm::Error perform(SimpleFile &mergedFile) override {
218
97
    // Skip this pass if output format uses text relocations instead of stubs.
219
97
    if (!this->noTextRelocs())
220
0
      return llvm::Error::success();
221
97
222
97
    // Scan all references in all atoms.
223
443
    
for (const DefinedAtom *atom : mergedFile.defined())97
{
224
443
      for (const Reference *ref : *atom) {
225
180
        // Look at call-sites.
226
180
        if (!this->isCallSite(*ref))
227
129
          continue;
228
51
        const Atom *target = ref->target();
229
51
        assert(target != nullptr);
230
51
        if (isa<SharedLibraryAtom>(target)) {
231
25
          // Calls to shared libraries go through stubs.
232
25
          _targetToUses[target].push_back(ref);
233
25
          continue;
234
25
        }
235
26
        const DefinedAtom *defTarget = dyn_cast<DefinedAtom>(target);
236
26
        if (defTarget && defTarget->interposable() != DefinedAtom::interposeNo){
237
0
          // Calls to interposable functions in same linkage unit must also go
238
0
          // through a stub.
239
0
          assert(defTarget->scope() != DefinedAtom::scopeTranslationUnit);
240
0
          _targetToUses[target].push_back(ref);
241
0
        }
242
26
      }
243
443
    }
244
97
245
97
    // Exit early if no stubs needed.
246
97
    if (_targetToUses.empty())
247
80
      return llvm::Error::success();
248
17
249
17
    // First add help-common and GOT slots used by lazy binding.
250
17
    SimpleDefinedAtom *helperCommonAtom =
251
17
        new (_file.allocator()) StubHelperCommonAtom(_file, _stubInfo);
252
17
    SimpleDefinedAtom *helperCacheNLPAtom =
253
17
        new (_file.allocator()) NonLazyPointerAtom(_file, _ctx.is64Bit(),
254
17
                                    _stubInfo.stubHelperImageCacheContentType);
255
17
    SimpleDefinedAtom *helperBinderNLPAtom =
256
17
        new (_file.allocator()) NonLazyPointerAtom(_file, _ctx.is64Bit(),
257
17
                                    _stubInfo.stubHelperImageCacheContentType);
258
17
    addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
259
17
                 helperCacheNLPAtom);
260
17
    addOptReference(
261
17
        helperCommonAtom, _stubInfo.stubHelperCommonReferenceToCache,
262
17
        _stubInfo.optStubHelperCommonReferenceToCache, helperCacheNLPAtom);
263
17
    addReference(helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
264
17
                 helperBinderNLPAtom);
265
17
    addOptReference(
266
17
        helperCommonAtom, _stubInfo.stubHelperCommonReferenceToBinder,
267
17
        _stubInfo.optStubHelperCommonReferenceToBinder, helperBinderNLPAtom);
268
17
    mergedFile.addAtom(*helperCommonAtom);
269
17
    mergedFile.addAtom(*helperBinderNLPAtom);
270
17
    mergedFile.addAtom(*helperCacheNLPAtom);
271
17
272
17
    // Add reference to dyld_stub_binder in libSystem.dylib
273
17
    auto I = llvm::find_if(
274
32
        mergedFile.sharedLibrary(), [&](const SharedLibraryAtom *atom) {
275
32
          return atom->name().equals(_stubInfo.binderSymbolName);
276
32
        });
277
17
    assert(I != mergedFile.sharedLibrary().end() &&
278
17
           "dyld_stub_binder not found");
279
17
    addReference(helperBinderNLPAtom, _stubInfo.nonLazyPointerReferenceToBinder, *I);
280
17
281
17
    // Sort targets by name, so stubs and lazy pointers are consistent
282
17
    std::vector<const Atom *> targetsNeedingStubs;
283
17
    for (auto it : _targetToUses)
284
23
      targetsNeedingStubs.push_back(it.first);
285
17
    std::sort(targetsNeedingStubs.begin(), targetsNeedingStubs.end(),
286
17
              [](const Atom * left, const Atom * right) {
287
7
      return (left->name().compare(right->name()) < 0);
288
7
    });
289
17
290
17
    // Make and append stubs, lazy pointers, and helpers in alphabetical order.
291
17
    unsigned lazyOffset = 0;
292
23
    for (const Atom *target : targetsNeedingStubs) {
293
23
      auto *stub = new (_file.allocator()) StubAtom(_file, _stubInfo);
294
23
      auto *lp =
295
23
          new (_file.allocator()) LazyPointerAtom(_file, _ctx.is64Bit());
296
23
      auto *helper = new (_file.allocator()) StubHelperAtom(_file, _stubInfo);
297
23
298
23
      addReference(stub, _stubInfo.stubReferenceToLP, lp);
299
23
      addOptReference(stub, _stubInfo.stubReferenceToLP,
300
23
                      _stubInfo.optStubReferenceToLP, lp);
301
23
      addReference(lp, _stubInfo.lazyPointerReferenceToHelper, helper);
302
23
      addReference(lp, _stubInfo.lazyPointerReferenceToFinal, target);
303
23
      addReference(helper, _stubInfo.stubHelperReferenceToImm, helper);
304
23
      addReferenceAddend(helper, _stubInfo.stubHelperReferenceToImm, helper,
305
23
                         lazyOffset);
306
23
      addReference(helper, _stubInfo.stubHelperReferenceToHelperCommon,
307
23
                   helperCommonAtom);
308
23
309
23
      mergedFile.addAtom(*stub);
310
23
      mergedFile.addAtom(*lp);
311
23
      mergedFile.addAtom(*helper);
312
23
313
23
      // Update each reference to use stub.
314
25
      for (const Reference *ref : _targetToUses[target]) {
315
25
        assert(ref->target() == target);
316
25
        // Switch call site to reference stub atom instead.
317
25
        const_cast<Reference *>(ref)->setTarget(stub);
318
25
      }
319
23
320
23
      // Calculate new offset
321
23
      lazyOffset += target->name().size() + 12;
322
23
    }
323
17
324
17
    return llvm::Error::success();
325
17
  }
326
327
private:
328
97
  bool noTextRelocs() {
329
97
    return true;
330
97
  }
331
332
180
  bool isCallSite(const Reference &ref) {
333
180
    return _archHandler.isCallSite(ref);
334
180
  }
335
336
  void addReference(SimpleDefinedAtom* atom,
337
                    const ArchHandler::ReferenceInfo &refInfo,
338
166
                    const lld::Atom* target) {
339
166
    atom->addReference(Reference::KindNamespace::mach_o,
340
166
                      refInfo.arch, refInfo.kind, refInfo.offset,
341
166
                      target, refInfo.addend);
342
166
  }
343
344
  void addReferenceAddend(SimpleDefinedAtom *atom,
345
                          const ArchHandler::ReferenceInfo &refInfo,
346
23
                          const lld::Atom *target, uint64_t addend) {
347
23
    atom->addReference(Reference::KindNamespace::mach_o, refInfo.arch,
348
23
                       refInfo.kind, refInfo.offset, target, addend);
349
23
  }
350
351
   void addOptReference(SimpleDefinedAtom* atom,
352
                    const ArchHandler::ReferenceInfo &refInfo,
353
                    const ArchHandler::OptionalRefInfo &optRef,
354
57
                    const lld::Atom* target) {
355
57
      if (!optRef.used)
356
44
        return;
357
13
    atom->addReference(Reference::KindNamespace::mach_o,
358
13
                      refInfo.arch, optRef.kind, optRef.offset,
359
13
                      target, optRef.addend);
360
13
  }
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
372
97
void addStubsPass(PassManager &pm, const MachOLinkingContext &ctx) {
373
97
  pm.add(std::unique_ptr<Pass>(new StubsPass(ctx)));
374
97
}
375
376
} // end namespace mach_o
377
} // end namespace lld