blob: 8e789566e2f68b12ebf61f505126ff9919a16287 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- 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 "lld/Core/LLVM.h"
14
15#include "llvm/MC/StringTableBuilder.h"
16#include "llvm/Object/ELF.h"
17
Davide Italiano85121bb2015-09-25 03:56:11 +000018#include "Config.h"
19
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020#include <type_traits>
21
22namespace lld {
23namespace elf2 {
24
25class SymbolBody;
26class SymbolTable;
27template <class ELFT> class SymbolTableSection;
28template <bool Is64Bits> class StringTableSection;
29template <class ELFT> class InputSection;
30template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
32template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000033template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000036typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
38template <class ELFT>
39typename llvm::object::ELFFile<ELFT>::uintX_t
40getLocalSymVA(const typename llvm::object::ELFFile<ELFT>::Elf_Sym *Sym,
41 const ObjectFile<ELFT> &File);
Rafael Espindolaa6627382015-10-06 23:56:53 +000042bool canBePreempted(const SymbolBody *Body);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000043template <class ELFT> bool includeInSymtab(const SymbolBody &B);
44
Rafael Espindola05a3dd22015-09-22 23:38:23 +000045bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000046
47template <class ELFT>
48bool shouldKeepInSymtab(
49 StringRef Name, const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000050
Rafael Espindola71675852015-09-22 00:16:19 +000051// This represents a section in an output file.
52// Different sub classes represent different types of sections. Some contain
53// input sections, others are created by the linker.
54// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055// non-overlapping file offsets and VAs.
56template <bool Is64Bits> class OutputSectionBase {
57public:
58 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
59 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
60 llvm::ELF::Elf32_Shdr>::type HeaderT;
61
62 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
63 void setVA(uintX_t VA) { Header.sh_addr = VA; }
64 uintX_t getVA() const { return Header.sh_addr; }
65 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
66 template <llvm::object::endianness E>
67 void writeHeaderTo(typename llvm::object::ELFFile<
68 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
69 StringRef getName() { return Name; }
70 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
71
72 unsigned getSectionIndex() const { return SectionIndex; }
73 void setSectionIndex(unsigned I) { SectionIndex = I; }
74
75 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000076 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 void setSize(uintX_t Val) { Header.sh_size = Val; }
78 uintX_t getFlags() { return Header.sh_flags; }
79 uintX_t getFileOff() { return Header.sh_offset; }
80 uintX_t getAlign() {
81 // The ELF spec states that a value of 0 means the section has no alignment
82 // constraits.
83 return std::max<uintX_t>(Header.sh_addralign, 1);
84 }
85 uint32_t getType() { return Header.sh_type; }
86
87 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
88
89 virtual void finalize() {}
90 virtual void writeTo(uint8_t *Buf) = 0;
91
92protected:
93 StringRef Name;
94 HeaderT Header;
95 unsigned SectionIndex;
96 ~OutputSectionBase() = default;
97};
98
99template <class ELFT>
100class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
101 typedef OutputSectionBase<ELFT::Is64Bits> Base;
102 typedef typename Base::uintX_t uintX_t;
103
104public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000105 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106 void finalize() override {
107 this->Header.sh_size = Entries.size() * this->getAddrSize();
108 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000109 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 void addEntry(SymbolBody *Sym);
111 bool empty() const { return Entries.empty(); }
112 uintX_t getEntryAddr(const SymbolBody &B) const;
113
114private:
115 std::vector<const SymbolBody *> Entries;
116};
117
118template <class ELFT>
119class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
120 typedef OutputSectionBase<ELFT::Is64Bits> Base;
121 typedef typename Base::uintX_t uintX_t;
122
123public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000124 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000125 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126 void writeTo(uint8_t *Buf) override;
127 void addEntry(SymbolBody *Sym);
128 bool empty() const { return Entries.empty(); }
129 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130
131private:
132 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000133};
134
135template <class ELFT> struct DynamicReloc {
136 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
137 const InputSection<ELFT> &C;
138 const Elf_Rel &RI;
139};
140
141template <class ELFT>
142class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
143public:
144 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
145 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
146 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
147 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
148 SymbolTableSection(SymbolTable &Table,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000149 StringTableSection<ELFT::Is64Bits> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000150
Rui Ueyama0db335f2015-10-07 16:58:54 +0000151 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000152 void writeTo(uint8_t *Buf) override;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000153 void addSymbol(StringRef Name, bool isLocal = false);
Davide Italianoe44456b2015-09-23 01:50:53 +0000154 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000155 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156
157private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000158 void writeLocalSymbols(uint8_t *&Buf);
159 void writeGlobalSymbols(uint8_t *&Buf);
160
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 SymbolTable &Table;
162 StringTableSection<ELFT::Is64Bits> &StrTabSec;
163 unsigned NumVisible = 0;
164 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165};
166
167template <class ELFT>
168class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
169 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
170 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000171 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172
173public:
Rafael Espindolad5409192015-10-09 14:25:49 +0000174 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
176 void finalize() override;
177 void writeTo(uint8_t *Buf) override;
178 bool hasRelocs() const { return !Relocs.empty(); }
179 bool isRela() const { return IsRela; }
180
181private:
182 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000183 const bool IsRela;
184};
185
186template <class ELFT>
187class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
188public:
189 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
190 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
191 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
192 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
193 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000194 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000195 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196 void writeTo(uint8_t *Buf) override;
197
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000198private:
Rafael Espindola71675852015-09-22 00:16:19 +0000199 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200};
201
202template <bool Is64Bits>
203class InterpSection final : public OutputSectionBase<Is64Bits> {
204public:
205 InterpSection();
206
207 void writeTo(uint8_t *Buf);
208};
209
210template <bool Is64Bits>
211class StringTableSection final : public OutputSectionBase<Is64Bits> {
212public:
213 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000214 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 void add(StringRef S) { StrTabBuilder.add(S); }
216 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
217 StringRef data() const { return StrTabBuilder.data(); }
218 void writeTo(uint8_t *Buf) override;
219
220 void finalize() override {
221 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
222 this->Header.sh_size = StrTabBuilder.data().size();
223 }
224
225 bool isDynamic() const { return Dynamic; }
226
227private:
228 const bool Dynamic;
229 llvm::StringTableBuilder StrTabBuilder;
230};
231
232template <class ELFT>
233class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
234 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
235
236public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000237 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000239 void finalize() override;
240 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241
242private:
243 uint32_t hash(StringRef Name) {
244 uint32_t H = 0;
245 for (char C : Name) {
246 H = (H << 4) + C;
247 uint32_t G = H & 0xf0000000;
248 if (G)
249 H ^= G >> 24;
250 H &= ~G;
251 }
252 return H;
253 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254 std::vector<uint32_t> Hashes;
255};
256
257template <class ELFT>
258class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
259 typedef OutputSectionBase<ELFT::Is64Bits> Base;
260 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000261 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
262 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
263 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000264 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265
266public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000267 DynamicSection(SymbolTable &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000268 void finalize() override;
269 void writeTo(uint8_t *Buf) override;
270
Rafael Espindola77572242015-10-02 19:37:55 +0000271 OutputSection<ELFT> *PreInitArraySec = nullptr;
272 OutputSection<ELFT> *InitArraySec = nullptr;
273 OutputSection<ELFT> *FiniArraySec = nullptr;
274
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000275private:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 SymbolTable &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000277 const ELFSymbolBody<ELFT> *InitSym = nullptr;
278 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000280
281// All output sections that are hadnled by the linker specially are
282// globally accessible. Writer initializes them, so don't use them
283// until Writer is initialized.
284template <class ELFT> struct Out {
285 static DynamicSection<ELFT> *Dynamic;
286 static GotSection<ELFT> *Got;
287 static HashTableSection<ELFT> *HashTab;
288 static InterpSection<ELFT::Is64Bits> *Interp;
289 static OutputSection<ELFT> *Bss;
290 static PltSection<ELFT> *Plt;
291 static RelocationSection<ELFT> *RelaDyn;
292 static StringTableSection<ELFT::Is64Bits> *DynStrTab;
293 static StringTableSection<ELFT::Is64Bits> *StrTab;
294 static SymbolTableSection<ELFT> *DynSymTab;
295 static SymbolTableSection<ELFT> *SymTab;
296};
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000297}
298}
299#endif