blob: c22c7d73a350b00e7663e98b75ccde742f198369 [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 Espindolacd076f02015-09-25 18:19:03 +000037getSymVA(const ELFSymbolBody<ELFT> &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);
43
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 Espindola35c6af32015-09-25 17:19:10 +0000106 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 void finalize() override {
108 this->Header.sh_size = Entries.size() * this->getAddrSize();
109 }
110 void writeTo(uint8_t *Buf) override {}
111 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;
117};
118
119template <class ELFT>
120class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
121 typedef OutputSectionBase<ELFT::Is64Bits> Base;
122 typedef typename Base::uintX_t uintX_t;
123
124public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000125 PltSection(const GotSection<ELFT> &GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126 void finalize() override {
127 this->Header.sh_size = Entries.size() * EntrySize;
128 }
129 void writeTo(uint8_t *Buf) override;
130 void addEntry(SymbolBody *Sym);
131 bool empty() const { return Entries.empty(); }
132 uintX_t getEntryAddr(const SymbolBody &B) const;
133 static const unsigned EntrySize = 8;
134
135private:
136 std::vector<const SymbolBody *> Entries;
137 const GotSection<ELFT> &GotSec;
138};
139
140template <class ELFT> struct DynamicReloc {
141 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
142 const InputSection<ELFT> &C;
143 const Elf_Rel &RI;
144};
145
146template <class ELFT>
147class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
148public:
149 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
150 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
151 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
152 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
153 SymbolTableSection(SymbolTable &Table,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000154 StringTableSection<ELFT::Is64Bits> &StrTabSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000155 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156
157 void finalize() override {
158 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
159 this->Header.sh_link = StrTabSec.getSectionIndex();
160 this->Header.sh_info = NumLocals + 1;
161 }
162
163 void writeTo(uint8_t *Buf) override;
164
Rafael Espindola0e604f92015-09-25 18:56:53 +0000165 SymbolTable &getSymTable() const { return Table; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166
167 void addSymbol(StringRef Name, bool isLocal = false) {
168 StrTabSec.add(Name);
169 ++NumVisible;
170 if (isLocal)
171 ++NumLocals;
172 }
173
Davide Italianoe44456b2015-09-23 01:50:53 +0000174 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176
177private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000178 void writeLocalSymbols(uint8_t *&Buf);
179 void writeGlobalSymbols(uint8_t *&Buf);
180
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000181 SymbolTable &Table;
182 StringTableSection<ELFT::Is64Bits> &StrTabSec;
183 unsigned NumVisible = 0;
184 unsigned NumLocals = 0;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000185 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186};
187
188template <class ELFT>
189class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
190 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
191 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
192
193public:
194 RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000195 const GotSection<ELFT> &GotSec, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000196 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
197 void finalize() override;
198 void writeTo(uint8_t *Buf) override;
199 bool hasRelocs() const { return !Relocs.empty(); }
200 bool isRela() const { return IsRela; }
201
202private:
203 std::vector<DynamicReloc<ELFT>> Relocs;
204 SymbolTableSection<ELFT> &DynSymSec;
205 const GotSection<ELFT> &GotSec;
206 const bool IsRela;
207};
208
209template <class ELFT>
210class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
211public:
212 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
213 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
214 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
215 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
216 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
217 OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000218 const OutputSection<ELFT> &BssSec, StringRef Name,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000219 uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000220 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 void writeTo(uint8_t *Buf) override;
222
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223private:
Rafael Espindola71675852015-09-22 00:16:19 +0000224 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225 const PltSection<ELFT> &PltSec;
226 const GotSection<ELFT> &GotSec;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000227 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228};
229
230template <bool Is64Bits>
231class InterpSection final : public OutputSectionBase<Is64Bits> {
232public:
233 InterpSection();
234
235 void writeTo(uint8_t *Buf);
236};
237
238template <bool Is64Bits>
239class StringTableSection final : public OutputSectionBase<Is64Bits> {
240public:
241 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000242 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000243 void add(StringRef S) { StrTabBuilder.add(S); }
244 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
245 StringRef data() const { return StrTabBuilder.data(); }
246 void writeTo(uint8_t *Buf) override;
247
248 void finalize() override {
249 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
250 this->Header.sh_size = StrTabBuilder.data().size();
251 }
252
253 bool isDynamic() const { return Dynamic; }
254
255private:
256 const bool Dynamic;
257 llvm::StringTableBuilder StrTabBuilder;
258};
259
260template <class ELFT>
261class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
262 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
263
264public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000265 HashTableSection(SymbolTableSection<ELFT> &DynSymSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000266 void addSymbol(SymbolBody *S);
267
268 void finalize() override {
269 this->Header.sh_link = DynSymSec.getSectionIndex();
270
271 assert(DynSymSec.getNumSymbols() == Hashes.size() + 1);
272 unsigned NumEntries = 2; // nbucket and nchain.
273 NumEntries += DynSymSec.getNumSymbols(); // The chain entries.
274
275 // Create as many buckets as there are symbols.
276 // FIXME: This is simplistic. We can try to optimize it, but implementing
277 // support for SHT_GNU_HASH is probably even more profitable.
278 NumEntries += DynSymSec.getNumSymbols();
279 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
280 }
281
282 void writeTo(uint8_t *Buf) override {
283 unsigned NumSymbols = DynSymSec.getNumSymbols();
284 auto *P = reinterpret_cast<Elf_Word *>(Buf);
285 *P++ = NumSymbols; // nbucket
286 *P++ = NumSymbols; // nchain
287
288 Elf_Word *Buckets = P;
289 Elf_Word *Chains = P + NumSymbols;
290
291 for (unsigned I = 1; I < NumSymbols; ++I) {
292 uint32_t Hash = Hashes[I - 1] % NumSymbols;
293 Chains[I] = Buckets[Hash];
294 Buckets[Hash] = I;
295 }
296 }
297
298 SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; }
299
300private:
301 uint32_t hash(StringRef Name) {
302 uint32_t H = 0;
303 for (char C : Name) {
304 H = (H << 4) + C;
305 uint32_t G = H & 0xf0000000;
306 if (G)
307 H ^= G >> 24;
308 H &= ~G;
309 }
310 return H;
311 }
312 SymbolTableSection<ELFT> &DynSymSec;
313 std::vector<uint32_t> Hashes;
314};
315
316template <class ELFT>
317class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
318 typedef OutputSectionBase<ELFT::Is64Bits> Base;
319 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000320 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
321 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
322 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000323 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324
325public:
326 DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000327 RelocationSection<ELFT> &RelaDynSec,
328 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000329 void finalize() override;
330 void writeTo(uint8_t *Buf) override;
331
Rafael Espindola77572242015-10-02 19:37:55 +0000332 OutputSection<ELFT> *PreInitArraySec = nullptr;
333 OutputSection<ELFT> *InitArraySec = nullptr;
334 OutputSection<ELFT> *FiniArraySec = nullptr;
335
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336private:
337 HashTableSection<ELFT> &HashSec;
338 SymbolTableSection<ELFT> &DynSymSec;
339 StringTableSection<ELFT::Is64Bits> &DynStrSec;
340 RelocationSection<ELFT> &RelaDynSec;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000341 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342 SymbolTable &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000343 const ELFSymbolBody<ELFT> *InitSym = nullptr;
344 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345};
346}
347}
348#endif