Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/lib/ReaderWriter/MachO/ExecutableAtoms.h
Line
Count
Source (jump to first uncovered line)
1
//===- lib/ReaderWriter/MachO/ExecutableAtoms.h ---------------------------===//
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
#ifndef LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H
10
#define LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H
11
12
#include "Atoms.h"
13
#include "File.h"
14
15
#include "llvm/BinaryFormat/MachO.h"
16
17
#include "lld/Core/DefinedAtom.h"
18
#include "lld/Core/File.h"
19
#include "lld/Core/LinkingContext.h"
20
#include "lld/Core/Reference.h"
21
#include "lld/Core/Simple.h"
22
#include "lld/Core/UndefinedAtom.h"
23
#include "lld/ReaderWriter/MachOLinkingContext.h"
24
25
namespace lld {
26
namespace mach_o {
27
28
29
//
30
// CEntryFile adds an UndefinedAtom for "_main" so that the Resolving
31
// phase will fail if "_main" is undefined.
32
//
33
class CEntryFile : public SimpleFile {
34
public:
35
  CEntryFile(const MachOLinkingContext &context)
36
      : SimpleFile("C entry", kindCEntryObject),
37
54
       _undefMain(*this, context.entrySymbolName()) {
38
54
    this->addAtom(_undefMain);
39
54
  }
40
41
private:
42
  SimpleUndefinedAtom   _undefMain;
43
};
44
45
46
//
47
// StubHelperFile adds an UndefinedAtom for "dyld_stub_binder" so that
48
// the Resolveing phase will fail if "dyld_stub_binder" is undefined.
49
//
50
class StubHelperFile : public SimpleFile {
51
public:
52
  StubHelperFile(const MachOLinkingContext &context)
53
      : SimpleFile("stub runtime", kindStubHelperObject),
54
102
        _undefBinder(*this, context.binderSymbolName()) {
55
102
    this->addAtom(_undefBinder);
56
102
  }
57
58
private:
59
  SimpleUndefinedAtom   _undefBinder;
60
};
61
62
63
//
64
// MachHeaderAliasFile lazily instantiates the magic symbols that mark the start
65
// of the mach_header for final linked images.
66
//
67
class MachHeaderAliasFile : public SimpleFile {
68
public:
69
  MachHeaderAliasFile(const MachOLinkingContext &context)
70
102
    : SimpleFile("mach_header symbols", kindHeaderObject) {
71
102
    StringRef machHeaderSymbolName;
72
102
    DefinedAtom::Scope symbolScope = DefinedAtom::scopeLinkageUnit;
73
102
    StringRef dsoHandleName;
74
102
    switch (context.outputMachOType()) {
75
102
    case llvm::MachO::MH_OBJECT:
76
0
      machHeaderSymbolName = "__mh_object_header";
77
0
      break;
78
102
    case llvm::MachO::MH_EXECUTE:
79
54
      machHeaderSymbolName = "__mh_execute_header";
80
54
      symbolScope = DefinedAtom::scopeGlobal;
81
54
      dsoHandleName = "___dso_handle";
82
54
      break;
83
102
    case llvm::MachO::MH_FVMLIB:
84
0
      llvm_unreachable("no mach_header symbol for file type");
85
102
    case llvm::MachO::MH_CORE:
86
0
      llvm_unreachable("no mach_header symbol for file type");
87
102
    case llvm::MachO::MH_PRELOAD:
88
0
      llvm_unreachable("no mach_header symbol for file type");
89
102
    case llvm::MachO::MH_DYLIB:
90
45
      machHeaderSymbolName = "__mh_dylib_header";
91
45
      dsoHandleName = "___dso_handle";
92
45
      break;
93
102
    case llvm::MachO::MH_DYLINKER:
94
0
      machHeaderSymbolName = "__mh_dylinker_header";
95
0
      dsoHandleName = "___dso_handle";
96
0
      break;
97
102
    case llvm::MachO::MH_BUNDLE:
98
3
      machHeaderSymbolName = "__mh_bundle_header";
99
3
      dsoHandleName = "___dso_handle";
100
3
      break;
101
102
    case llvm::MachO::MH_DYLIB_STUB:
102
0
      llvm_unreachable("no mach_header symbol for file type");
103
102
    case llvm::MachO::MH_DSYM:
104
0
      llvm_unreachable("no mach_header symbol for file type");
105
102
    case llvm::MachO::MH_KEXT_BUNDLE:
106
0
      dsoHandleName = "___dso_handle";
107
0
      break;
108
102
    }
109
102
    if (!machHeaderSymbolName.empty())
110
102
      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
111
102
          *this, machHeaderSymbolName, symbolScope,
112
102
          DefinedAtom::typeMachHeader, DefinedAtom::mergeNo, false,
113
102
          true /* noDeadStrip */,
114
102
          ArrayRef<uint8_t>(), DefinedAtom::Alignment(4096)));
115
102
116
102
    if (!dsoHandleName.empty())
117
102
      _definedAtoms.push_back(new (allocator()) MachODefinedAtom(
118
102
          *this, dsoHandleName, DefinedAtom::scopeLinkageUnit,
119
102
          DefinedAtom::typeDSOHandle, DefinedAtom::mergeNo, false,
120
102
          true /* noDeadStrip */,
121
102
          ArrayRef<uint8_t>(), DefinedAtom::Alignment(1)));
122
102
  }
123
124
102
  const AtomRange<DefinedAtom> defined() const override {
125
102
    return _definedAtoms;
126
102
  }
127
102
  const AtomRange<UndefinedAtom> undefined() const override {
128
102
    return _noUndefinedAtoms;
129
102
  }
130
131
102
  const AtomRange<SharedLibraryAtom> sharedLibrary() const override {
132
102
    return _noSharedLibraryAtoms;
133
102
  }
134
135
102
  const AtomRange<AbsoluteAtom> absolute() const override {
136
102
    return _noAbsoluteAtoms;
137
102
  }
138
139
102
  void clearAtoms() override {
140
102
    _definedAtoms.clear();
141
102
    _noUndefinedAtoms.clear();
142
102
    _noSharedLibraryAtoms.clear();
143
102
    _noAbsoluteAtoms.clear();
144
102
  }
145
146
147
private:
148
  mutable AtomVector<DefinedAtom> _definedAtoms;
149
};
150
151
} // namespace mach_o
152
} // namespace lld
153
154
#endif // LLD_READER_WRITER_MACHO_EXECUTABLE_ATOMS_H