Coverage Report

Created: 2017-09-19 22:28

/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/tools/lld/ELF/OutputSections.h
Line
Count
Source (jump to first uncovered line)
1
//===- OutputSections.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_ELF_OUTPUT_SECTIONS_H
11
#define LLD_ELF_OUTPUT_SECTIONS_H
12
13
#include "Config.h"
14
#include "InputSection.h"
15
#include "LinkerScript.h"
16
#include "Relocations.h"
17
18
#include "lld/Core/LLVM.h"
19
#include "llvm/MC/StringTableBuilder.h"
20
#include "llvm/Object/ELF.h"
21
22
namespace lld {
23
namespace elf {
24
25
struct PhdrEntry;
26
class SymbolBody;
27
struct EhSectionPiece;
28
class EhInputSection;
29
class InputSection;
30
class InputSectionBase;
31
class MergeInputSection;
32
class OutputSection;
33
template <class ELFT> class ObjFile;
34
template <class ELFT> class SharedFile;
35
class SharedSymbol;
36
class DefinedRegular;
37
38
// This represents a section in an output file.
39
// It is composed of multiple InputSections.
40
// The writer creates multiple OutputSections and assign them unique,
41
// non-overlapping file offsets and VAs.
42
class OutputSection final : public BaseCommand, public SectionBase {
43
public:
44
  OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
45
46
0
  static bool classof(const SectionBase *S) {
47
0
    return S->kind() == SectionBase::Output;
48
0
  }
49
  static bool classof(const BaseCommand *C);
50
51
6.00k
  uint64_t getLMA() const { return Addr + LMAOffset; }
52
  template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
53
54
  unsigned SectionIndex;
55
  unsigned SortRank;
56
57
  uint32_t getPhdrFlags() const;
58
59
229k
  void updateAlignment(uint32_t Val) {
60
229k
    if (Val > Alignment)
61
16.9k
      Alignment = Val;
62
229k
  }
63
64
  // Pointer to the PT_LOAD segment, which this section resides in. This field
65
  // is used to correctly compute file offset of a section. When two sections
66
  // share the same load segment, difference between their file offsets should
67
  // be equal to difference between their virtual addresses. To compute some
68
  // section offset we use the following formula: Off = Off_first + VA -
69
  // VA_first, where Off_first and VA_first is file offset and VA of first
70
  // section in PT_LOAD.
71
  PhdrEntry *PtLoad = nullptr;
72
73
  // Pointer to a relocation section for this section. Usually nullptr because
74
  // we consume relocations, but if --emit-relocs is specified (which is rare),
75
  // it may have a non-null value.
76
  OutputSection *RelocationSection = nullptr;
77
78
  // The following fields correspond to Elf_Shdr members.
79
  uint64_t Size = 0;
80
  uint64_t Offset = 0;
81
  uint64_t LMAOffset = 0;
82
  uint64_t Addr = 0;
83
  uint32_t ShName = 0;
84
85
  void addSection(InputSection *S);
86
87
  // Used for implementation of --compress-debug-sections option.
88
  std::vector<uint8_t> ZDebugHeader;
89
  llvm::SmallVector<char, 1> CompressedData;
90
91
  // Location in the output buffer.
92
  uint8_t *Loc = nullptr;
93
94
  // The following members are normally only used in linker scripts.
95
  MemoryRegion *MemRegion = nullptr;
96
  Expr AddrExpr;
97
  Expr AlignExpr;
98
  Expr LMAExpr;
99
  Expr SubalignExpr;
100
  std::vector<BaseCommand *> Commands;
101
  std::vector<StringRef> Phdrs;
102
  llvm::Optional<uint32_t> Filler;
103
  ConstraintKind Constraint = ConstraintKind::NoConstraint;
104
  std::string Location;
105
  std::string MemoryRegionName;
106
  bool Noload = false;
107
108
  template <class ELFT> void finalize();
109
  template <class ELFT> void writeTo(uint8_t *Buf);
110
  template <class ELFT> void maybeCompress();
111
  uint32_t getFiller();
112
113
  void sort(std::function<int(InputSectionBase *S)> Order);
114
  void sortInitFini();
115
  void sortCtorsDtors();
116
};
117
118
int getPriority(StringRef S);
119
120
// All output sections that are handled by the linker specially are
121
// globally accessible. Writer initializes them, so don't use them
122
// until Writer is initialized.
123
struct Out {
124
  static uint8_t First;
125
  static OutputSection *Opd;
126
  static uint8_t *OpdBuf;
127
  static PhdrEntry *TlsPhdr;
128
  static OutputSection *DebugInfo;
129
  static OutputSection *ElfHeader;
130
  static OutputSection *ProgramHeaders;
131
  static OutputSection *PreinitArray;
132
  static OutputSection *InitArray;
133
  static OutputSection *FiniArray;
134
};
135
136
struct SectionKey {
137
  StringRef Name;
138
  uint64_t Flags;
139
  uint32_t Alignment;
140
};
141
} // namespace elf
142
} // namespace lld
143
namespace llvm {
144
template <> struct DenseMapInfo<lld::elf::SectionKey> {
145
  static lld::elf::SectionKey getEmptyKey();
146
  static lld::elf::SectionKey getTombstoneKey();
147
  static unsigned getHashValue(const lld::elf::SectionKey &Val);
148
  static bool isEqual(const lld::elf::SectionKey &LHS,
149
                      const lld::elf::SectionKey &RHS);
150
};
151
} // namespace llvm
152
namespace lld {
153
namespace elf {
154
155
// This class knows how to create an output section for a given
156
// input section. Output section type is determined by various
157
// factors, including input section's sh_flags, sh_type and
158
// linker scripts.
159
class OutputSectionFactory {
160
public:
161
  OutputSectionFactory();
162
  ~OutputSectionFactory();
163
164
  void addInputSec(InputSectionBase *IS, StringRef OutsecName,
165
                   OutputSection *OS = nullptr);
166
167
private:
168
  llvm::SmallDenseMap<SectionKey, OutputSection *> Map;
169
};
170
171
uint64_t getHeaderSize();
172
void reportDiscarded(InputSectionBase *IS);
173
void sortByOrder(llvm::MutableArrayRef<InputSection *> In,
174
                 std::function<int(InputSectionBase *S)> Order);
175
176
extern std::vector<OutputSection *> OutputSections;
177
} // namespace elf
178
} // namespace lld
179
180
#endif