Coverage Report

Created: 2019-05-19 14:56

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/lld/ELF/OutputSections.h
Line
Count
Source
1
//===- OutputSections.h -----------------------------------------*- C++ -*-===//
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_ELF_OUTPUT_SECTIONS_H
10
#define LLD_ELF_OUTPUT_SECTIONS_H
11
12
#include "Config.h"
13
#include "InputSection.h"
14
#include "LinkerScript.h"
15
#include "Relocations.h"
16
#include "lld/Common/LLVM.h"
17
#include "llvm/MC/StringTableBuilder.h"
18
#include "llvm/Object/ELF.h"
19
#include <array>
20
21
namespace lld {
22
namespace elf {
23
24
struct PhdrEntry;
25
class InputSection;
26
class InputSectionBase;
27
28
// This represents a section in an output file.
29
// It is composed of multiple InputSections.
30
// The writer creates multiple OutputSections and assign them unique,
31
// non-overlapping file offsets and VAs.
32
class OutputSection final : public BaseCommand, public SectionBase {
33
public:
34
  OutputSection(StringRef Name, uint32_t Type, uint64_t Flags);
35
36
150
  static bool classof(const SectionBase *S) {
37
150
    return S->kind() == SectionBase::Output;
38
150
  }
39
40
  static bool classof(const BaseCommand *C);
41
42
21.3k
  uint64_t getLMA() const { return PtLoad ? 
Addr + PtLoad->LMAOffset21.1k
:
Addr236
; }
43
  template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
44
45
  uint32_t SectionIndex = UINT32_MAX;
46
  unsigned SortRank;
47
48
  uint32_t getPhdrFlags() const;
49
50
  // Pointer to the PT_LOAD segment, which this section resides in. This field
51
  // is used to correctly compute file offset of a section. When two sections
52
  // share the same load segment, difference between their file offsets should
53
  // be equal to difference between their virtual addresses. To compute some
54
  // section offset we use the following formula: Off = Off_first + VA -
55
  // VA_first, where Off_first and VA_first is file offset and VA of first
56
  // section in PT_LOAD.
57
  PhdrEntry *PtLoad = nullptr;
58
59
  // Pointer to a relocation section for this section. Usually nullptr because
60
  // we consume relocations, but if --emit-relocs is specified (which is rare),
61
  // it may have a non-null value.
62
  OutputSection *RelocationSection = nullptr;
63
64
  // Initially this field is the number of InputSections that have been added to
65
  // the OutputSection so far. Later on, after a call to assignAddresses, it
66
  // corresponds to the Elf_Shdr member.
67
  uint64_t Size = 0;
68
69
  // The following fields correspond to Elf_Shdr members.
70
  uint64_t Offset = 0;
71
  uint64_t Addr = 0;
72
  uint32_t ShName = 0;
73
74
  void addSection(InputSection *IS);
75
76
  // The following members are normally only used in linker scripts.
77
  MemoryRegion *MemRegion = nullptr;
78
  MemoryRegion *LMARegion = nullptr;
79
  Expr AddrExpr;
80
  Expr AlignExpr;
81
  Expr LMAExpr;
82
  Expr SubalignExpr;
83
  std::vector<BaseCommand *> SectionCommands;
84
  std::vector<StringRef> Phdrs;
85
  llvm::Optional<std::array<uint8_t, 4>> Filler;
86
  ConstraintKind Constraint = ConstraintKind::NoConstraint;
87
  std::string Location;
88
  std::string MemoryRegionName;
89
  std::string LMARegionName;
90
  bool NonAlloc = false;
91
  bool Noload = false;
92
  bool ExpressionsUseSymbols = false;
93
  bool UsedInExpression = false;
94
  bool InOverlay = false;
95
96
  void finalize();
97
  template <class ELFT> void writeTo(uint8_t *Buf);
98
  template <class ELFT> void maybeCompress();
99
100
  void sort(llvm::function_ref<int(InputSectionBase *S)> Order);
101
  void sortInitFini();
102
  void sortCtorsDtors();
103
104
private:
105
  // Used for implementation of --compress-debug-sections option.
106
  std::vector<uint8_t> ZDebugHeader;
107
  llvm::SmallVector<char, 1> CompressedData;
108
109
  std::array<uint8_t, 4> getFiller();
110
};
111
112
int getPriority(StringRef S);
113
114
std::vector<InputSection *> getInputSections(OutputSection* OS);
115
116
// All output sections that are handled by the linker specially are
117
// globally accessible. Writer initializes them, so don't use them
118
// until Writer is initialized.
119
struct Out {
120
  static uint8_t *BufferStart;
121
  static uint8_t First;
122
  static PhdrEntry *TlsPhdr;
123
  static OutputSection *ElfHeader;
124
  static OutputSection *ProgramHeaders;
125
  static OutputSection *PreinitArray;
126
  static OutputSection *InitArray;
127
  static OutputSection *FiniArray;
128
};
129
130
} // namespace elf
131
} // namespace lld
132
133
namespace lld {
134
namespace elf {
135
136
uint64_t getHeaderSize();
137
138
extern std::vector<OutputSection *> OutputSections;
139
} // namespace elf
140
} // namespace lld
141
142
#endif