Coverage Report

Created: 2017-10-03 07:32

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/ReaderWriter/MachO/GOTPass.cpp
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/MachO/GOTPass.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
/// \file
11
/// This linker pass transforms all GOT kind references to real references.
12
/// That is, in assembly you can write something like:
13
///     movq foo@GOTPCREL(%rip), %rax
14
/// which means you want to load a pointer to "foo" out of the GOT (global
15
/// Offsets Table). In the object file, the Atom containing this instruction
16
/// has a Reference whose target is an Atom named "foo" and the Reference
17
/// kind is a GOT load.  The linker needs to instantiate a pointer sized
18
/// GOT entry.  This is done be creating a GOT Atom to represent that pointer
19
/// sized data in this pass, and altering the Atom graph so the Reference now
20
/// points to the GOT Atom entry (corresponding to "foo") and changing the
21
/// Reference Kind to reflect it is now pointing to a GOT entry (rather
22
/// then needing a GOT entry).
23
///
24
/// There is one optimization the linker can do here.  If the target of the GOT
25
/// is in the same linkage unit and does not need to be interposable, and
26
/// the GOT use is just a load (not some other operation), this pass can
27
/// transform that load into an LEA (add).  This optimizes away one memory load
28
/// which at runtime that could stall the pipeline.  This optimization only
29
/// works for architectures in which a (GOT) load instruction can be change to
30
/// an LEA instruction that is the same size.  The method isGOTAccess() should
31
/// only return true for "canBypassGOT" if this optimization is supported.
32
///
33
//===----------------------------------------------------------------------===//
34
35
#include "ArchHandler.h"
36
#include "File.h"
37
#include "MachOPasses.h"
38
#include "lld/Core/DefinedAtom.h"
39
#include "lld/Core/File.h"
40
#include "lld/Core/LLVM.h"
41
#include "lld/Core/Reference.h"
42
#include "lld/Core/Simple.h"
43
#include "llvm/ADT/DenseMap.h"
44
#include "llvm/ADT/STLExtras.h"
45
46
namespace lld {
47
namespace mach_o {
48
49
//
50
//  GOT Entry Atom created by the GOT pass.
51
//
52
class GOTEntryAtom : public SimpleDefinedAtom {
53
public:
54
  GOTEntryAtom(const File &file, bool is64, StringRef name)
55
15
    : SimpleDefinedAtom(file), _is64(is64), _name(name) { }
56
57
15
  ~GOTEntryAtom() override = default;
58
59
104
  ContentType contentType() const override {
60
104
    return DefinedAtom::typeGOT;
61
104
  }
62
63
15
  Alignment alignment() const override {
64
15
    return _is64 ? 
815
:
40
;
65
15
  }
66
67
75
  uint64_t size() const override {
68
75
    return _is64 ? 
875
:
40
;
69
75
  }
70
71
0
  ContentPermissions permissions() const override {
72
0
    return DefinedAtom::permRW_;
73
0
  }
74
75
30
  ArrayRef<uint8_t> rawContent() const override {
76
30
    static const uint8_t zeros[] =
77
30
        { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
78
30
    return llvm::makeArrayRef(zeros, size());
79
30
  }
80
81
28
  StringRef slotName() const {
82
28
    return _name;
83
28
  }
84
85
private:
86
  const bool _is64;
87
  StringRef _name;
88
};
89
90
/// Pass for instantiating and optimizing GOT slots.
91
///
92
class GOTPass : public Pass {
93
public:
94
  GOTPass(const MachOLinkingContext &context)
95
      : _ctx(context), _archHandler(_ctx.archHandler()),
96
86
        _file(*_ctx.make_file<MachOFile>("<mach-o GOT Pass>")) {
97
86
    _file.setOrdinal(_ctx.getNextOrdinalAndIncrement());
98
86
  }
99
100
private:
101
86
  llvm::Error perform(SimpleFile &mergedFile) override {
102
86
    // Scan all references in all atoms.
103
460
    for (const DefinedAtom *atom : mergedFile.defined()) {
104
285
      for (const Reference *ref : *atom) {
105
285
        // Look at instructions accessing the GOT.
106
285
        bool canBypassGOT;
107
285
        if (!_archHandler.isGOTAccess(*ref, canBypassGOT))
108
265
          continue;
109
20
        const Atom *target = ref->target();
110
20
        assert(target != nullptr);
111
20
112
20
        if (
!shouldReplaceTargetWithGOTAtom(target, canBypassGOT)20
) {
113
0
          // Update reference kind to reflect that target is a direct accesss.
114
0
          _archHandler.updateReferenceToGOT(ref, false);
115
20
        } else {
116
20
          // Replace the target with a reference to a GOT entry.
117
20
          const DefinedAtom *gotEntry = makeGOTEntry(target);
118
20
          const_cast<Reference *>(ref)->setTarget(gotEntry);
119
20
          // Update reference kind to reflect that target is now a GOT entry.
120
20
          _archHandler.updateReferenceToGOT(ref, true);
121
20
        }
122
285
      }
123
460
    }
124
86
125
86
    // Sort and add all created GOT Atoms to master file
126
86
    std::vector<const GOTEntryAtom *> entries;
127
86
    entries.reserve(_targetToGOT.size());
128
86
    for (auto &it : _targetToGOT)
129
15
      entries.push_back(it.second);
130
86
    std::sort(entries.begin(), entries.end(),
131
14
              [](const GOTEntryAtom *left, const GOTEntryAtom *right) {
132
14
      return (left->slotName().compare(right->slotName()) < 0);
133
14
    });
134
86
    for (const GOTEntryAtom *slot : entries)
135
15
      mergedFile.addAtom(*slot);
136
86
137
86
    return llvm::Error::success();
138
86
  }
139
140
20
  bool shouldReplaceTargetWithGOTAtom(const Atom *target, bool canBypassGOT) {
141
20
    // Accesses to shared library symbols must go through GOT.
142
20
    if (isa<SharedLibraryAtom>(target))
143
19
      return true;
144
1
    // Accesses to interposable symbols in same linkage unit must also go
145
1
    // through GOT.
146
1
    const DefinedAtom *defTarget = dyn_cast<DefinedAtom>(target);
147
1
    if (defTarget != nullptr &&
148
1
        
defTarget->interposable() != DefinedAtom::interposeNo1
) {
149
0
      assert(defTarget->scope() != DefinedAtom::scopeTranslationUnit);
150
0
      return true;
151
0
    }
152
1
    // Target does not require indirection.  So, if instruction allows GOT to be
153
1
    // by-passed, do that optimization and don't create GOT entry.
154
1
    return !canBypassGOT;
155
1
  }
156
157
20
  const DefinedAtom *makeGOTEntry(const Atom *target) {
158
20
    auto pos = _targetToGOT.find(target);
159
20
    if (
pos == _targetToGOT.end()20
) {
160
15
      auto *gotEntry = new (_file.allocator())
161
15
          GOTEntryAtom(_file, _ctx.is64Bit(), target->name());
162
15
      _targetToGOT[target] = gotEntry;
163
15
      const ArchHandler::ReferenceInfo &nlInfo = _archHandler.stubInfo().
164
15
                                                nonLazyPointerReferenceToBinder;
165
15
      gotEntry->addReference(Reference::KindNamespace::mach_o, nlInfo.arch,
166
15
                             nlInfo.kind, 0, target, 0);
167
15
      return gotEntry;
168
15
    }
169
5
    return pos->second;
170
5
  }
171
172
  const MachOLinkingContext &_ctx;
173
  mach_o::ArchHandler                             &_archHandler;
174
  MachOFile                                       &_file;
175
  llvm::DenseMap<const Atom*, const GOTEntryAtom*> _targetToGOT;
176
};
177
178
86
void addGOTPass(PassManager &pm, const MachOLinkingContext &ctx) {
179
86
  assert(ctx.needsGOTPass());
180
86
  pm.add(llvm::make_unique<GOTPass>(ctx));
181
86
}
182
183
} // end namesapce mach_o
184
} // end namesapce lld