Coverage Report

Created: 2017-10-03 07:32

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