Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/lib/ReaderWriter/MachO/File.h
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/MachO/File.h ----------------------------*- 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
#ifndef LLD_READER_WRITER_MACHO_FILE_H
11
#define LLD_READER_WRITER_MACHO_FILE_H
12
13
#include "Atoms.h"
14
#include "DebugInfo.h"
15
#include "MachONormalizedFile.h"
16
#include "lld/Core/SharedLibraryFile.h"
17
#include "lld/Core/Simple.h"
18
#include "llvm/ADT/DenseMap.h"
19
#include "llvm/ADT/StringMap.h"
20
#include "llvm/Support/Format.h"
21
#include <unordered_map>
22
23
namespace lld {
24
namespace mach_o {
25
26
using lld::mach_o::normalized::Section;
27
28
class MachOFile : public SimpleFile {
29
public:
30
31
  /// Real file constructor - for on-disk files.
32
  MachOFile(std::unique_ptr<MemoryBuffer> mb, MachOLinkingContext *ctx)
33
    : SimpleFile(mb->getBufferIdentifier(), File::kindMachObject),
34
27
      _mb(std::move(mb)), _ctx(ctx) {}
35
36
  /// Dummy file constructor - for virtual files.
37
  MachOFile(StringRef path)
38
526
    : SimpleFile(path, File::kindMachObject) {}
39
40
  void addDefinedAtom(StringRef name, Atom::Scope scope,
41
                      DefinedAtom::ContentType type, DefinedAtom::Merge merge,
42
                      uint64_t sectionOffset, uint64_t contentSize, bool thumb,
43
                      bool noDeadStrip, bool copyRefs,
44
469
                      const Section *inSection) {
45
469
    assert(sectionOffset+contentSize <= inSection->content.size());
46
469
    ArrayRef<uint8_t> content = inSection->content.slice(sectionOffset,
47
469
                                                        contentSize);
48
469
    if (
copyRefs469
) {
49
364
      // Make a copy of the atom's name and content that is owned by this file.
50
364
      name = name.copy(allocator());
51
364
      content = content.copy(allocator());
52
364
    }
53
469
    DefinedAtom::Alignment align(
54
469
        inSection->alignment,
55
469
        sectionOffset % inSection->alignment);
56
469
    auto *atom =
57
469
        new (allocator()) MachODefinedAtom(*this, name, scope, type, merge,
58
469
                                           thumb, noDeadStrip, content, align);
59
469
    addAtomForSection(inSection, atom, sectionOffset);
60
469
  }
61
62
  void addDefinedAtomInCustomSection(StringRef name, Atom::Scope scope,
63
                      DefinedAtom::ContentType type, DefinedAtom::Merge merge,
64
                      bool thumb, bool noDeadStrip, uint64_t sectionOffset,
65
                      uint64_t contentSize, StringRef sectionName,
66
23
                      bool copyRefs, const Section *inSection) {
67
23
    assert(sectionOffset+contentSize <= inSection->content.size());
68
23
    ArrayRef<uint8_t> content = inSection->content.slice(sectionOffset,
69
23
                                                        contentSize);
70
23
   if (
copyRefs23
) {
71
23
      // Make a copy of the atom's name and content that is owned by this file.
72
23
      name = name.copy(allocator());
73
23
      content = content.copy(allocator());
74
23
      sectionName = sectionName.copy(allocator());
75
23
    }
76
23
    DefinedAtom::Alignment align(
77
23
        inSection->alignment,
78
23
        sectionOffset % inSection->alignment);
79
23
    auto *atom =
80
23
        new (allocator()) MachODefinedCustomSectionAtom(*this, name, scope, type,
81
23
                                                        merge, thumb,
82
23
                                                        noDeadStrip, content,
83
23
                                                        sectionName, align);
84
23
    addAtomForSection(inSection, atom, sectionOffset);
85
23
  }
86
87
  void addZeroFillDefinedAtom(StringRef name, Atom::Scope scope,
88
                              uint64_t sectionOffset, uint64_t size,
89
                              bool noDeadStrip, bool copyRefs,
90
3
                              const Section *inSection) {
91
3
    if (
copyRefs3
) {
92
3
      // Make a copy of the atom's name and content that is owned by this file.
93
3
      name = name.copy(allocator());
94
3
    }
95
3
    DefinedAtom::Alignment align(
96
3
        inSection->alignment,
97
3
        sectionOffset % inSection->alignment);
98
3
99
3
    DefinedAtom::ContentType type = DefinedAtom::typeUnknown;
100
3
    switch (inSection->type) {
101
1
    case llvm::MachO::S_ZEROFILL:
102
1
      type = DefinedAtom::typeZeroFill;
103
1
      break;
104
2
    case llvm::MachO::S_THREAD_LOCAL_ZEROFILL:
105
2
      type = DefinedAtom::typeTLVInitialZeroFill;
106
2
      break;
107
0
    default:
108
0
      llvm_unreachable("Unrecognized zero-fill section");
109
3
    }
110
3
111
3
    auto *atom =
112
3
        new (allocator()) MachODefinedAtom(*this, name, scope, type, size,
113
3
                                           noDeadStrip, align);
114
3
    addAtomForSection(inSection, atom, sectionOffset);
115
3
  }
116
117
110
  void addUndefinedAtom(StringRef name, bool copyRefs) {
118
110
    if (
copyRefs110
) {
119
94
      // Make a copy of the atom's name that is owned by this file.
120
94
      name = name.copy(allocator());
121
94
    }
122
110
    auto *atom = new (allocator()) SimpleUndefinedAtom(*this, name);
123
110
    addAtom(*atom);
124
110
    _undefAtoms[name] = atom;
125
110
  }
126
127
  void addTentativeDefAtom(StringRef name, Atom::Scope scope, uint64_t size,
128
5
                           DefinedAtom::Alignment align, bool copyRefs) {
129
5
    if (
copyRefs5
) {
130
5
      // Make a copy of the atom's name that is owned by this file.
131
5
      name = name.copy(allocator());
132
5
    }
133
5
    auto *atom =
134
5
        new (allocator()) MachOTentativeDefAtom(*this, name, scope, size, align);
135
5
    addAtom(*atom);
136
5
    _undefAtoms[name] = atom;
137
5
  }
138
139
  /// Search this file for an the atom from 'section' that covers
140
  /// 'offsetInSect'.  Returns nullptr is no atom found.
141
  MachODefinedAtom *findAtomCoveringAddress(const Section &section,
142
                                            uint64_t offsetInSect,
143
929
                                            uint32_t *foundOffsetAtom=nullptr) {
144
929
    const auto &pos = _sectionAtoms.find(&section);
145
929
    if (pos == _sectionAtoms.end())
146
0
      return nullptr;
147
929
    const auto &vec = pos->second;
148
929
    assert(offsetInSect < section.content.size());
149
929
    // Vector of atoms for section are already sorted, so do binary search.
150
929
    const auto &atomPos = std::lower_bound(vec.begin(), vec.end(), offsetInSect,
151
929
        [offsetInSect](const SectionOffsetAndAtom &ao,
152
1.54k
                       uint64_t targetAddr) -> bool {
153
1.54k
          // Each atom has a start offset of its slice of the
154
1.54k
          // section's content. This compare function must return true
155
1.54k
          // iff the atom's range is before the offset being searched for.
156
1.54k
          uint64_t atomsEndOffset = ao.offset+ao.atom->rawContent().size();
157
1.54k
          return (atomsEndOffset <= offsetInSect);
158
1.54k
        });
159
929
    if (atomPos == vec.end())
160
0
      return nullptr;
161
929
    
if (929
foundOffsetAtom929
)
162
827
      *foundOffsetAtom = offsetInSect - atomPos->offset;
163
929
    return atomPos->atom;
164
929
  }
165
166
  /// Searches this file for an UndefinedAtom named 'name'. Returns
167
  /// nullptr is no such atom found.
168
189
  const lld::Atom *findUndefAtom(StringRef name) {
169
189
    auto pos = _undefAtoms.find(name);
170
189
    if (pos == _undefAtoms.end())
171
0
      return nullptr;
172
189
    return pos->second;
173
189
  }
174
175
  typedef std::function<void (MachODefinedAtom* atom)> DefinedAtomVisitor;
176
177
177
  void eachDefinedAtom(DefinedAtomVisitor vistor) {
178
295
    for (auto &sectAndAtoms : _sectionAtoms) {
179
491
      for (auto &offAndAtom : sectAndAtoms.second) {
180
491
        vistor(offAndAtom.atom);
181
491
      }
182
295
    }
183
177
  }
184
185
  typedef std::function<void(MachODefinedAtom *atom, uint64_t offset)>
186
      SectionAtomVisitor;
187
188
40
  void eachAtomInSection(const Section &section, SectionAtomVisitor visitor) {
189
40
    auto pos = _sectionAtoms.find(&section);
190
40
    if (pos == _sectionAtoms.end())
191
0
      return;
192
40
    auto vec = pos->second;
193
40
194
40
    for (auto &offAndAtom : vec)
195
75
      visitor(offAndAtom.atom, offAndAtom.offset);
196
40
  }
197
198
346
  MachOLinkingContext::Arch arch() const { return _arch; }
199
177
  void setArch(MachOLinkingContext::Arch arch) { _arch = arch; }
200
201
175
  MachOLinkingContext::OS OS() const { return _os; }
202
177
  void setOS(MachOLinkingContext::OS os) { _os = os; }
203
204
172
  MachOLinkingContext::ObjCConstraint objcConstraint() const {
205
172
    return _objcConstraint;
206
172
  }
207
6
  void setObjcConstraint(MachOLinkingContext::ObjCConstraint v) {
208
6
    _objcConstraint = v;
209
6
  }
210
211
596
  uint32_t minVersion() const { return _minVersion; }
212
177
  void setMinVersion(uint32_t v) { _minVersion = v; }
213
214
585
  LoadCommandType minVersionLoadCommandKind() const {
215
585
    return _minVersionLoadCommandKind;
216
585
  }
217
177
  void setMinVersionLoadCommandKind(LoadCommandType v) {
218
177
    _minVersionLoadCommandKind = v;
219
177
  }
220
221
171
  uint32_t swiftVersion() const { return _swiftVersion; }
222
6
  void setSwiftVersion(uint32_t v) { _swiftVersion = v; }
223
224
596
  bool subsectionsViaSymbols() const {
225
596
    return _flags & llvm::MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
226
596
  }
227
177
  void setFlags(normalized::FileFlags v) { _flags = v; }
228
229
  /// Methods for support type inquiry through isa, cast, and dyn_cast:
230
2.14k
  static inline bool classof(const File *F) {
231
2.14k
    return F->kind() == File::kindMachObject;
232
2.14k
  }
233
234
1
  void setDebugInfo(std::unique_ptr<DebugInfo> debugInfo) {
235
1
    _debugInfo = std::move(debugInfo);
236
1
  }
237
238
543
  DebugInfo* debugInfo() const { return _debugInfo.get(); }
239
0
  std::unique_ptr<DebugInfo> takeDebugInfo() { return std::move(_debugInfo); }
240
241
protected:
242
27
  std::error_code doParse() override {
243
27
    // Convert binary file to normalized mach-o.
244
27
    auto normFile = normalized::readBinary(_mb, _ctx->arch());
245
27
    if (auto ec = normFile.takeError())
246
0
      return llvm::errorToErrorCode(std::move(ec));
247
27
    // Convert normalized mach-o to atoms.
248
27
    
if (auto 27
ec27
= normalized::normalizedObjectToAtoms(this, **normFile, false))
249
0
      return llvm::errorToErrorCode(std::move(ec));
250
27
    return std::error_code();
251
27
  }
252
253
private:
254
  struct SectionOffsetAndAtom { uint64_t offset;  MachODefinedAtom *atom; };
255
256
  void addAtomForSection(const Section *inSection, MachODefinedAtom* atom,
257
495
                         uint64_t sectionOffset) {
258
495
    SectionOffsetAndAtom offAndAtom;
259
495
    offAndAtom.offset = sectionOffset;
260
495
    offAndAtom.atom   = atom;
261
495
     _sectionAtoms[inSection].push_back(offAndAtom);
262
495
    addAtom(*atom);
263
495
  }
264
265
  typedef llvm::DenseMap<const normalized::Section *,
266
                         std::vector<SectionOffsetAndAtom>>  SectionToAtoms;
267
  typedef llvm::StringMap<const lld::Atom *> NameToAtom;
268
269
  std::unique_ptr<MemoryBuffer> _mb;
270
  MachOLinkingContext          *_ctx;
271
  SectionToAtoms                _sectionAtoms;
272
  NameToAtom                     _undefAtoms;
273
  MachOLinkingContext::Arch      _arch = MachOLinkingContext::arch_unknown;
274
  MachOLinkingContext::OS        _os = MachOLinkingContext::OS::unknown;
275
  uint32_t                       _minVersion = 0;
276
  LoadCommandType               _minVersionLoadCommandKind = (LoadCommandType)0;
277
  MachOLinkingContext::ObjCConstraint _objcConstraint =
278
      MachOLinkingContext::objc_unknown;
279
  uint32_t                       _swiftVersion = 0;
280
  normalized::FileFlags        _flags = llvm::MachO::MH_SUBSECTIONS_VIA_SYMBOLS;
281
  std::unique_ptr<DebugInfo>   _debugInfo;
282
};
283
284
class MachODylibFile : public SharedLibraryFile {
285
public:
286
  MachODylibFile(std::unique_ptr<MemoryBuffer> mb, MachOLinkingContext *ctx)
287
      : SharedLibraryFile(mb->getBufferIdentifier()),
288
4
        _mb(std::move(mb)), _ctx(ctx) {}
289
290
112
  MachODylibFile(StringRef path) : SharedLibraryFile(path) {}
291
292
159
  OwningAtomPtr<SharedLibraryAtom> exports(StringRef name) const override {
293
159
    // Pass down _installName so that if this requested symbol
294
159
    // is re-exported through this dylib, the SharedLibraryAtom's loadName()
295
159
    // is this dylib installName and not the implementation dylib's.
296
159
    // NOTE: isData is not needed for dylibs (it matters for static libs).
297
159
    return exports(name, _installName);
298
159
  }
299
300
  /// Adds symbol name that this dylib exports. The corresponding
301
  /// SharedLibraryAtom is created lazily (since most symbols are not used).
302
161
  void addExportedSymbol(StringRef name, bool weakDef, bool copyRefs) {
303
161
    if (
copyRefs161
) {
304
161
      name = name.copy(allocator());
305
161
    }
306
161
    AtomAndFlags info(weakDef);
307
161
    _nameToAtom[name] = info;
308
161
  }
309
310
1
  void addReExportedDylib(StringRef dylibPath) {
311
1
    _reExportedDylibs.emplace_back(dylibPath);
312
1
  }
313
314
565
  StringRef installName() const { return _installName; }
315
110
  uint32_t currentVersion() { return _currentVersion; }
316
110
  uint32_t compatVersion() { return _compatVersion; }
317
318
116
  void setInstallName(StringRef name) { _installName = name; }
319
116
  void setCompatVersion(uint32_t version) { _compatVersion = version; }
320
116
  void setCurrentVersion(uint32_t version) { _currentVersion = version; }
321
322
  typedef std::function<MachODylibFile *(StringRef)> FindDylib;
323
324
116
  void loadReExportedDylibs(FindDylib find) {
325
1
    for (ReExportedDylib &entry : _reExportedDylibs) {
326
1
      entry.file = find(entry.path);
327
1
    }
328
116
  }
329
330
0
  StringRef getDSOName() const override { return _installName; }
331
332
4
  std::error_code doParse() override {
333
4
    // Convert binary file to normalized mach-o.
334
4
    auto normFile = normalized::readBinary(_mb, _ctx->arch());
335
4
    if (auto ec = normFile.takeError())
336
0
      return llvm::errorToErrorCode(std::move(ec));
337
4
    // Convert normalized mach-o to atoms.
338
4
    
if (auto 4
ec4
= normalized::normalizedDylibToAtoms(this, **normFile, false))
339
0
      return llvm::errorToErrorCode(std::move(ec));
340
4
    return std::error_code();
341
4
  }
342
343
private:
344
  OwningAtomPtr<SharedLibraryAtom> exports(StringRef name,
345
161
                                   StringRef installName) const {
346
161
    // First, check if requested symbol is directly implemented by this dylib.
347
161
    auto entry = _nameToAtom.find(name);
348
161
    if (
entry != _nameToAtom.end()161
) {
349
132
      // FIXME: Make this map a set and only used in assert builds.
350
132
      // Note, its safe to assert here as the resolver is the only client of
351
132
      // this API and it only requests exports for undefined symbols.
352
132
      // If we return from here we are no longer undefined so we should never
353
132
      // get here again.
354
132
      assert(!entry->second.atom && "Duplicate shared library export");
355
132
      bool weakDef = entry->second.weakDef;
356
132
      auto *atom = new (allocator()) MachOSharedLibraryAtom(*this, name,
357
132
                                                            installName,
358
132
                                                            weakDef);
359
132
      entry->second.atom = atom;
360
132
      return atom;
361
132
    }
362
161
363
161
    // Next, check if symbol is implemented in some re-exported dylib.
364
29
    
for (const ReExportedDylib &dylib : _reExportedDylibs) 29
{
365
2
      assert(dylib.file);
366
2
      auto atom = dylib.file->exports(name, installName);
367
2
      if (atom.get())
368
1
        return atom;
369
2
    }
370
29
371
29
    // Symbol not exported or re-exported by this dylib.
372
28
    return nullptr;
373
161
  }
374
375
  struct ReExportedDylib {
376
1
    ReExportedDylib(StringRef p) : path(p), file(nullptr) { }
377
    StringRef       path;
378
    MachODylibFile *file;
379
  };
380
381
  struct AtomAndFlags {
382
161
    AtomAndFlags() : atom(nullptr), weakDef(false) { }
383
161
    AtomAndFlags(bool weak) : atom(nullptr), weakDef(weak) { }
384
    const SharedLibraryAtom  *atom;
385
    bool                      weakDef;
386
  };
387
388
  std::unique_ptr<MemoryBuffer>              _mb;
389
  MachOLinkingContext                       *_ctx;
390
  StringRef                                  _installName;
391
  uint32_t                                   _currentVersion;
392
  uint32_t                                   _compatVersion;
393
  std::vector<ReExportedDylib>               _reExportedDylibs;
394
  mutable std::unordered_map<StringRef, AtomAndFlags> _nameToAtom;
395
};
396
397
} // end namespace mach_o
398
} // end namespace lld
399
400
#endif // LLD_READER_WRITER_MACHO_FILE_H