blob: af81de0c7db713bde535efe1059a626c0733223a [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);
Davide Italiano6993ba42015-09-26 00:47:56 +000047bool shouldKeepInSymtab(StringRef SymName);
Davide Italiano85121bb2015-09-25 03:56:11 +000048
Rafael Espindola71675852015-09-22 00:16:19 +000049// This represents a section in an output file.
50// Different sub classes represent different types of sections. Some contain
51// input sections, others are created by the linker.
52// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000053// non-overlapping file offsets and VAs.
54template <bool Is64Bits> class OutputSectionBase {
55public:
56 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
57 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
58 llvm::ELF::Elf32_Shdr>::type HeaderT;
59
60 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
61 void setVA(uintX_t VA) { Header.sh_addr = VA; }
62 uintX_t getVA() const { return Header.sh_addr; }
63 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
64 template <llvm::object::endianness E>
65 void writeHeaderTo(typename llvm::object::ELFFile<
66 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
67 StringRef getName() { return Name; }
68 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
69
70 unsigned getSectionIndex() const { return SectionIndex; }
71 void setSectionIndex(unsigned I) { SectionIndex = I; }
72
73 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000074 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075 void setSize(uintX_t Val) { Header.sh_size = Val; }
76 uintX_t getFlags() { return Header.sh_flags; }
77 uintX_t getFileOff() { return Header.sh_offset; }
78 uintX_t getAlign() {
79 // The ELF spec states that a value of 0 means the section has no alignment
80 // constraits.
81 return std::max<uintX_t>(Header.sh_addralign, 1);
82 }
83 uint32_t getType() { return Header.sh_type; }
84
85 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
86
87 virtual void finalize() {}
88 virtual void writeTo(uint8_t *Buf) = 0;
89
90protected:
91 StringRef Name;
92 HeaderT Header;
93 unsigned SectionIndex;
94 ~OutputSectionBase() = default;
95};
96
97template <class ELFT>
98class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
99 typedef OutputSectionBase<ELFT::Is64Bits> Base;
100 typedef typename Base::uintX_t uintX_t;
101
102public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000103 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104 void finalize() override {
105 this->Header.sh_size = Entries.size() * this->getAddrSize();
106 }
107 void writeTo(uint8_t *Buf) override {}
108 void addEntry(SymbolBody *Sym);
109 bool empty() const { return Entries.empty(); }
110 uintX_t getEntryAddr(const SymbolBody &B) const;
111
112private:
113 std::vector<const SymbolBody *> Entries;
114};
115
116template <class ELFT>
117class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
118 typedef OutputSectionBase<ELFT::Is64Bits> Base;
119 typedef typename Base::uintX_t uintX_t;
120
121public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000122 PltSection(const GotSection<ELFT> &GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 void finalize() override {
124 this->Header.sh_size = Entries.size() * EntrySize;
125 }
126 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;
130 static const unsigned EntrySize = 8;
131
132private:
133 std::vector<const SymbolBody *> Entries;
134 const GotSection<ELFT> &GotSec;
135};
136
137template <class ELFT> struct DynamicReloc {
138 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
139 const InputSection<ELFT> &C;
140 const Elf_Rel &RI;
141};
142
143template <class ELFT>
144class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
145public:
146 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
147 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
148 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
149 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
150 SymbolTableSection(SymbolTable &Table,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000151 StringTableSection<ELFT::Is64Bits> &StrTabSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000152 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153
154 void finalize() override {
155 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
156 this->Header.sh_link = StrTabSec.getSectionIndex();
157 this->Header.sh_info = NumLocals + 1;
158 }
159
160 void writeTo(uint8_t *Buf) override;
161
Rafael Espindola0e604f92015-09-25 18:56:53 +0000162 SymbolTable &getSymTable() const { return Table; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000163
164 void addSymbol(StringRef Name, bool isLocal = false) {
165 StrTabSec.add(Name);
166 ++NumVisible;
167 if (isLocal)
168 ++NumLocals;
169 }
170
Davide Italianoe44456b2015-09-23 01:50:53 +0000171 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173
174private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000175 void writeLocalSymbols(uint8_t *&Buf);
176 void writeGlobalSymbols(uint8_t *&Buf);
177
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178 SymbolTable &Table;
179 StringTableSection<ELFT::Is64Bits> &StrTabSec;
180 unsigned NumVisible = 0;
181 unsigned NumLocals = 0;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000182 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000183};
184
185template <class ELFT>
186class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
187 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
188 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
189
190public:
191 RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000192 const GotSection<ELFT> &GotSec, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
194 void finalize() override;
195 void writeTo(uint8_t *Buf) override;
196 bool hasRelocs() const { return !Relocs.empty(); }
197 bool isRela() const { return IsRela; }
198
199private:
200 std::vector<DynamicReloc<ELFT>> Relocs;
201 SymbolTableSection<ELFT> &DynSymSec;
202 const GotSection<ELFT> &GotSec;
203 const bool IsRela;
204};
205
206template <class ELFT>
207class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
208public:
209 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
210 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
211 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
212 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
213 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
214 OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000215 const OutputSection<ELFT> &BssSec, StringRef Name,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000216 uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000217 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 void writeTo(uint8_t *Buf) override;
219
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000220private:
Rafael Espindola71675852015-09-22 00:16:19 +0000221 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222 const PltSection<ELFT> &PltSec;
223 const GotSection<ELFT> &GotSec;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000224 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225};
226
227template <bool Is64Bits>
228class InterpSection final : public OutputSectionBase<Is64Bits> {
229public:
230 InterpSection();
231
232 void writeTo(uint8_t *Buf);
233};
234
235template <bool Is64Bits>
236class StringTableSection final : public OutputSectionBase<Is64Bits> {
237public:
238 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000239 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240 void add(StringRef S) { StrTabBuilder.add(S); }
241 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
242 StringRef data() const { return StrTabBuilder.data(); }
243 void writeTo(uint8_t *Buf) override;
244
245 void finalize() override {
246 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
247 this->Header.sh_size = StrTabBuilder.data().size();
248 }
249
250 bool isDynamic() const { return Dynamic; }
251
252private:
253 const bool Dynamic;
254 llvm::StringTableBuilder StrTabBuilder;
255};
256
257template <class ELFT>
258class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
259 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
260
261public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000262 HashTableSection(SymbolTableSection<ELFT> &DynSymSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000263 void addSymbol(SymbolBody *S);
264
265 void finalize() override {
266 this->Header.sh_link = DynSymSec.getSectionIndex();
267
268 assert(DynSymSec.getNumSymbols() == Hashes.size() + 1);
269 unsigned NumEntries = 2; // nbucket and nchain.
270 NumEntries += DynSymSec.getNumSymbols(); // The chain entries.
271
272 // Create as many buckets as there are symbols.
273 // FIXME: This is simplistic. We can try to optimize it, but implementing
274 // support for SHT_GNU_HASH is probably even more profitable.
275 NumEntries += DynSymSec.getNumSymbols();
276 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
277 }
278
279 void writeTo(uint8_t *Buf) override {
280 unsigned NumSymbols = DynSymSec.getNumSymbols();
281 auto *P = reinterpret_cast<Elf_Word *>(Buf);
282 *P++ = NumSymbols; // nbucket
283 *P++ = NumSymbols; // nchain
284
285 Elf_Word *Buckets = P;
286 Elf_Word *Chains = P + NumSymbols;
287
288 for (unsigned I = 1; I < NumSymbols; ++I) {
289 uint32_t Hash = Hashes[I - 1] % NumSymbols;
290 Chains[I] = Buckets[Hash];
291 Buckets[Hash] = I;
292 }
293 }
294
295 SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; }
296
297private:
298 uint32_t hash(StringRef Name) {
299 uint32_t H = 0;
300 for (char C : Name) {
301 H = (H << 4) + C;
302 uint32_t G = H & 0xf0000000;
303 if (G)
304 H ^= G >> 24;
305 H &= ~G;
306 }
307 return H;
308 }
309 SymbolTableSection<ELFT> &DynSymSec;
310 std::vector<uint32_t> Hashes;
311};
312
313template <class ELFT>
314class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
315 typedef OutputSectionBase<ELFT::Is64Bits> Base;
316 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000317 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
318 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
319 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000320 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000321
322public:
323 DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000324 RelocationSection<ELFT> &RelaDynSec,
325 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326 void finalize() override;
327 void writeTo(uint8_t *Buf) override;
328
Rafael Espindola77572242015-10-02 19:37:55 +0000329 OutputSection<ELFT> *PreInitArraySec = nullptr;
330 OutputSection<ELFT> *InitArraySec = nullptr;
331 OutputSection<ELFT> *FiniArraySec = nullptr;
332
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333private:
334 HashTableSection<ELFT> &HashSec;
335 SymbolTableSection<ELFT> &DynSymSec;
336 StringTableSection<ELFT::Is64Bits> &DynStrSec;
337 RelocationSection<ELFT> &RelaDynSec;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000338 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339 SymbolTable &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000340 const ELFSymbolBody<ELFT> *InitSym = nullptr;
341 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342};
343}
344}
345#endif