blob: daeed4c670fdefc11626ec7cc7771c2be18e00ae [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>
36typename llvm::object::ELFFile<ELFT>::uintX_t
Rafael Espindola8614c562015-10-06 14:33:58 +000037getSymVA(const SymbolBody &S, const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038
39template <class ELFT>
40typename llvm::object::ELFFile<ELFT>::uintX_t
41getLocalSymVA(const typename llvm::object::ELFFile<ELFT>::Elf_Sym *Sym,
42 const ObjectFile<ELFT> &File);
Rafael Espindolaa6627382015-10-06 23:56:53 +000043bool canBePreempted(const SymbolBody *Body);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000044template <class ELFT> bool includeInSymtab(const SymbolBody &B);
45
Rafael Espindola05a3dd22015-09-22 23:38:23 +000046bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000047
48template <class ELFT>
49bool shouldKeepInSymtab(
50 StringRef Name, const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000051
Rafael Espindola71675852015-09-22 00:16:19 +000052// This represents a section in an output file.
53// Different sub classes represent different types of sections. Some contain
54// input sections, others are created by the linker.
55// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056// non-overlapping file offsets and VAs.
57template <bool Is64Bits> class OutputSectionBase {
58public:
59 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
60 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
61 llvm::ELF::Elf32_Shdr>::type HeaderT;
62
63 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
64 void setVA(uintX_t VA) { Header.sh_addr = VA; }
65 uintX_t getVA() const { return Header.sh_addr; }
66 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
67 template <llvm::object::endianness E>
68 void writeHeaderTo(typename llvm::object::ELFFile<
69 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
70 StringRef getName() { return Name; }
71 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
72
73 unsigned getSectionIndex() const { return SectionIndex; }
74 void setSectionIndex(unsigned I) { SectionIndex = I; }
75
76 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000077 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078 void setSize(uintX_t Val) { Header.sh_size = Val; }
79 uintX_t getFlags() { return Header.sh_flags; }
80 uintX_t getFileOff() { return Header.sh_offset; }
81 uintX_t getAlign() {
82 // The ELF spec states that a value of 0 means the section has no alignment
83 // constraits.
84 return std::max<uintX_t>(Header.sh_addralign, 1);
85 }
86 uint32_t getType() { return Header.sh_type; }
87
88 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
89
90 virtual void finalize() {}
91 virtual void writeTo(uint8_t *Buf) = 0;
92
93protected:
94 StringRef Name;
95 HeaderT Header;
96 unsigned SectionIndex;
97 ~OutputSectionBase() = default;
98};
99
100template <class ELFT>
101class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
102 typedef OutputSectionBase<ELFT::Is64Bits> Base;
103 typedef typename Base::uintX_t uintX_t;
104
105public:
Rafael Espindolaa6627382015-10-06 23:56:53 +0000106 GotSection(const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 void finalize() override {
108 this->Header.sh_size = Entries.size() * this->getAddrSize();
109 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000110 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111 void addEntry(SymbolBody *Sym);
112 bool empty() const { return Entries.empty(); }
113 uintX_t getEntryAddr(const SymbolBody &B) const;
114
115private:
116 std::vector<const SymbolBody *> Entries;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000117 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118};
119
120template <class ELFT>
121class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
122 typedef OutputSectionBase<ELFT::Is64Bits> Base;
123 typedef typename Base::uintX_t uintX_t;
124
125public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000126 PltSection(const GotSection<ELFT> &GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000127 void finalize() override {
128 this->Header.sh_size = Entries.size() * EntrySize;
129 }
130 void writeTo(uint8_t *Buf) override;
131 void addEntry(SymbolBody *Sym);
132 bool empty() const { return Entries.empty(); }
133 uintX_t getEntryAddr(const SymbolBody &B) const;
134 static const unsigned EntrySize = 8;
135
136private:
137 std::vector<const SymbolBody *> Entries;
138 const GotSection<ELFT> &GotSec;
139};
140
141template <class ELFT> struct DynamicReloc {
142 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
143 const InputSection<ELFT> &C;
144 const Elf_Rel &RI;
145};
146
147template <class ELFT>
148class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
149public:
150 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
151 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
152 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
153 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
154 SymbolTableSection(SymbolTable &Table,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000155 StringTableSection<ELFT::Is64Bits> &StrTabSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000156 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157
Rui Ueyama0db335f2015-10-07 16:58:54 +0000158 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 void writeTo(uint8_t *Buf) override;
Rafael Espindola0e604f92015-09-25 18:56:53 +0000160 SymbolTable &getSymTable() const { return Table; }
Rui Ueyama0db335f2015-10-07 16:58:54 +0000161 void addSymbol(StringRef Name, bool isLocal = false);
Davide Italianoe44456b2015-09-23 01:50:53 +0000162 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164
165private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000166 void writeLocalSymbols(uint8_t *&Buf);
167 void writeGlobalSymbols(uint8_t *&Buf);
168
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169 SymbolTable &Table;
170 StringTableSection<ELFT::Is64Bits> &StrTabSec;
171 unsigned NumVisible = 0;
172 unsigned NumLocals = 0;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000173 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000174};
175
176template <class ELFT>
177class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
178 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
179 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000180 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000181
182public:
183 RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
Rafael Espindola9c3e4d22015-10-05 21:23:08 +0000184 const GotSection<ELFT> &GotSec,
185 const OutputSection<ELFT> &BssSec, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
187 void finalize() override;
188 void writeTo(uint8_t *Buf) override;
189 bool hasRelocs() const { return !Relocs.empty(); }
190 bool isRela() const { return IsRela; }
191
192private:
193 std::vector<DynamicReloc<ELFT>> Relocs;
194 SymbolTableSection<ELFT> &DynSymSec;
195 const GotSection<ELFT> &GotSec;
Rafael Espindola9c3e4d22015-10-05 21:23:08 +0000196 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197 const bool IsRela;
198};
199
200template <class ELFT>
201class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
202public:
203 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
204 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
205 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
206 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
207 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
208 OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000209 const OutputSection<ELFT> &BssSec, StringRef Name,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000210 uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000211 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 void writeTo(uint8_t *Buf) override;
213
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214private:
Rafael Espindola71675852015-09-22 00:16:19 +0000215 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216 const PltSection<ELFT> &PltSec;
217 const GotSection<ELFT> &GotSec;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000218 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219};
220
221template <bool Is64Bits>
222class InterpSection final : public OutputSectionBase<Is64Bits> {
223public:
224 InterpSection();
225
226 void writeTo(uint8_t *Buf);
227};
228
229template <bool Is64Bits>
230class StringTableSection final : public OutputSectionBase<Is64Bits> {
231public:
232 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000233 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234 void add(StringRef S) { StrTabBuilder.add(S); }
235 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
236 StringRef data() const { return StrTabBuilder.data(); }
237 void writeTo(uint8_t *Buf) override;
238
239 void finalize() override {
240 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
241 this->Header.sh_size = StrTabBuilder.data().size();
242 }
243
244 bool isDynamic() const { return Dynamic; }
245
246private:
247 const bool Dynamic;
248 llvm::StringTableBuilder StrTabBuilder;
249};
250
251template <class ELFT>
252class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
253 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
254
255public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000256 HashTableSection(SymbolTableSection<ELFT> &DynSymSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000258 void finalize() override;
259 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260 SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; }
261
262private:
263 uint32_t hash(StringRef Name) {
264 uint32_t H = 0;
265 for (char C : Name) {
266 H = (H << 4) + C;
267 uint32_t G = H & 0xf0000000;
268 if (G)
269 H ^= G >> 24;
270 H &= ~G;
271 }
272 return H;
273 }
274 SymbolTableSection<ELFT> &DynSymSec;
275 std::vector<uint32_t> Hashes;
276};
277
278template <class ELFT>
279class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
280 typedef OutputSectionBase<ELFT::Is64Bits> Base;
281 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000282 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
283 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
284 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000285 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000286
287public:
288 DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000289 RelocationSection<ELFT> &RelaDynSec,
290 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000291 void finalize() override;
292 void writeTo(uint8_t *Buf) override;
293
Rafael Espindola77572242015-10-02 19:37:55 +0000294 OutputSection<ELFT> *PreInitArraySec = nullptr;
295 OutputSection<ELFT> *InitArraySec = nullptr;
296 OutputSection<ELFT> *FiniArraySec = nullptr;
297
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000298private:
299 HashTableSection<ELFT> &HashSec;
300 SymbolTableSection<ELFT> &DynSymSec;
301 StringTableSection<ELFT::Is64Bits> &DynStrSec;
302 RelocationSection<ELFT> &RelaDynSec;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000303 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000304 SymbolTable &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000305 const ELFSymbolBody<ELFT> *InitSym = nullptr;
306 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000307};
308}
309}
310#endif