blob: ee115c335864199303e674aa5b5ddc037d4385a6 [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
44bool includeInSymtab(const SymbolBody &B);
Rafael Espindola05a3dd22015-09-22 23:38:23 +000045bool includeInDynamicSymtab(const SymbolBody &B);
Davide Italiano6993ba42015-09-26 00:47:56 +000046bool shouldKeepInSymtab(StringRef SymName);
Davide Italiano85121bb2015-09-25 03:56:11 +000047
Rafael Espindola71675852015-09-22 00:16:19 +000048// This represents a section in an output file.
49// Different sub classes represent different types of sections. Some contain
50// input sections, others are created by the linker.
51// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000052// non-overlapping file offsets and VAs.
53template <bool Is64Bits> class OutputSectionBase {
54public:
55 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
56 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
57 llvm::ELF::Elf32_Shdr>::type HeaderT;
58
59 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
60 void setVA(uintX_t VA) { Header.sh_addr = VA; }
61 uintX_t getVA() const { return Header.sh_addr; }
62 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
63 template <llvm::object::endianness E>
64 void writeHeaderTo(typename llvm::object::ELFFile<
65 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
66 StringRef getName() { return Name; }
67 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
68
69 unsigned getSectionIndex() const { return SectionIndex; }
70 void setSectionIndex(unsigned I) { SectionIndex = I; }
71
72 // Returns the size of the section in the output file.
73 uintX_t getSize() { return Header.sh_size; }
74 void setSize(uintX_t Val) { Header.sh_size = Val; }
75 uintX_t getFlags() { return Header.sh_flags; }
76 uintX_t getFileOff() { return Header.sh_offset; }
77 uintX_t getAlign() {
78 // The ELF spec states that a value of 0 means the section has no alignment
79 // constraits.
80 return std::max<uintX_t>(Header.sh_addralign, 1);
81 }
82 uint32_t getType() { return Header.sh_type; }
83
84 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
85
86 virtual void finalize() {}
87 virtual void writeTo(uint8_t *Buf) = 0;
88
89protected:
90 StringRef Name;
91 HeaderT Header;
92 unsigned SectionIndex;
93 ~OutputSectionBase() = default;
94};
95
96template <class ELFT>
97class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
98 typedef OutputSectionBase<ELFT::Is64Bits> Base;
99 typedef typename Base::uintX_t uintX_t;
100
101public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000102 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103 void finalize() override {
104 this->Header.sh_size = Entries.size() * this->getAddrSize();
105 }
106 void writeTo(uint8_t *Buf) override {}
107 void addEntry(SymbolBody *Sym);
108 bool empty() const { return Entries.empty(); }
109 uintX_t getEntryAddr(const SymbolBody &B) const;
110
111private:
112 std::vector<const SymbolBody *> Entries;
113};
114
115template <class ELFT>
116class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
117 typedef OutputSectionBase<ELFT::Is64Bits> Base;
118 typedef typename Base::uintX_t uintX_t;
119
120public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000121 PltSection(const GotSection<ELFT> &GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122 void finalize() override {
123 this->Header.sh_size = Entries.size() * EntrySize;
124 }
125 void writeTo(uint8_t *Buf) override;
126 void addEntry(SymbolBody *Sym);
127 bool empty() const { return Entries.empty(); }
128 uintX_t getEntryAddr(const SymbolBody &B) const;
129 static const unsigned EntrySize = 8;
130
131private:
132 std::vector<const SymbolBody *> Entries;
133 const GotSection<ELFT> &GotSec;
134};
135
136template <class ELFT> struct DynamicReloc {
137 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
138 const InputSection<ELFT> &C;
139 const Elf_Rel &RI;
140};
141
142template <class ELFT>
143class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
144public:
145 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
146 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
147 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
148 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
149 SymbolTableSection(SymbolTable &Table,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000150 StringTableSection<ELFT::Is64Bits> &StrTabSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000151 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000152
153 void finalize() override {
154 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
155 this->Header.sh_link = StrTabSec.getSectionIndex();
156 this->Header.sh_info = NumLocals + 1;
157 }
158
159 void writeTo(uint8_t *Buf) override;
160
Rafael Espindola0e604f92015-09-25 18:56:53 +0000161 SymbolTable &getSymTable() const { return Table; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162
163 void addSymbol(StringRef Name, bool isLocal = false) {
164 StrTabSec.add(Name);
165 ++NumVisible;
166 if (isLocal)
167 ++NumLocals;
168 }
169
Davide Italianoe44456b2015-09-23 01:50:53 +0000170 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172
173private:
174 SymbolTable &Table;
175 StringTableSection<ELFT::Is64Bits> &StrTabSec;
176 unsigned NumVisible = 0;
177 unsigned NumLocals = 0;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000178 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179};
180
181template <class ELFT>
182class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
184 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
185
186public:
187 RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000188 const GotSection<ELFT> &GotSec, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
190 void finalize() override;
191 void writeTo(uint8_t *Buf) override;
192 bool hasRelocs() const { return !Relocs.empty(); }
193 bool isRela() const { return IsRela; }
194
195private:
196 std::vector<DynamicReloc<ELFT>> Relocs;
197 SymbolTableSection<ELFT> &DynSymSec;
198 const GotSection<ELFT> &GotSec;
199 const bool IsRela;
200};
201
202template <class ELFT>
203class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
204public:
205 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
206 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
207 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
208 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
209 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
210 OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000211 const OutputSection<ELFT> &BssSec, StringRef Name,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000212 uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000213 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214 void writeTo(uint8_t *Buf) override;
215
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216private:
Rafael Espindola71675852015-09-22 00:16:19 +0000217 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218 const PltSection<ELFT> &PltSec;
219 const GotSection<ELFT> &GotSec;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000220 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221};
222
223template <bool Is64Bits>
224class InterpSection final : public OutputSectionBase<Is64Bits> {
225public:
226 InterpSection();
227
228 void writeTo(uint8_t *Buf);
229};
230
231template <bool Is64Bits>
232class StringTableSection final : public OutputSectionBase<Is64Bits> {
233public:
234 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000235 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236 void add(StringRef S) { StrTabBuilder.add(S); }
237 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
238 StringRef data() const { return StrTabBuilder.data(); }
239 void writeTo(uint8_t *Buf) override;
240
241 void finalize() override {
242 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
243 this->Header.sh_size = StrTabBuilder.data().size();
244 }
245
246 bool isDynamic() const { return Dynamic; }
247
248private:
249 const bool Dynamic;
250 llvm::StringTableBuilder StrTabBuilder;
251};
252
253template <class ELFT>
254class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
255 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
256
257public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000258 HashTableSection(SymbolTableSection<ELFT> &DynSymSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259 void addSymbol(SymbolBody *S);
260
261 void finalize() override {
262 this->Header.sh_link = DynSymSec.getSectionIndex();
263
264 assert(DynSymSec.getNumSymbols() == Hashes.size() + 1);
265 unsigned NumEntries = 2; // nbucket and nchain.
266 NumEntries += DynSymSec.getNumSymbols(); // The chain entries.
267
268 // Create as many buckets as there are symbols.
269 // FIXME: This is simplistic. We can try to optimize it, but implementing
270 // support for SHT_GNU_HASH is probably even more profitable.
271 NumEntries += DynSymSec.getNumSymbols();
272 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
273 }
274
275 void writeTo(uint8_t *Buf) override {
276 unsigned NumSymbols = DynSymSec.getNumSymbols();
277 auto *P = reinterpret_cast<Elf_Word *>(Buf);
278 *P++ = NumSymbols; // nbucket
279 *P++ = NumSymbols; // nchain
280
281 Elf_Word *Buckets = P;
282 Elf_Word *Chains = P + NumSymbols;
283
284 for (unsigned I = 1; I < NumSymbols; ++I) {
285 uint32_t Hash = Hashes[I - 1] % NumSymbols;
286 Chains[I] = Buckets[Hash];
287 Buckets[Hash] = I;
288 }
289 }
290
291 SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; }
292
293private:
294 uint32_t hash(StringRef Name) {
295 uint32_t H = 0;
296 for (char C : Name) {
297 H = (H << 4) + C;
298 uint32_t G = H & 0xf0000000;
299 if (G)
300 H ^= G >> 24;
301 H &= ~G;
302 }
303 return H;
304 }
305 SymbolTableSection<ELFT> &DynSymSec;
306 std::vector<uint32_t> Hashes;
307};
308
309template <class ELFT>
310class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
311 typedef OutputSectionBase<ELFT::Is64Bits> Base;
312 typedef typename Base::HeaderT HeaderT;
313
314public:
315 DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000316 RelocationSection<ELFT> &RelaDynSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000317 void finalize() override;
318 void writeTo(uint8_t *Buf) override;
319
320private:
321 HashTableSection<ELFT> &HashSec;
322 SymbolTableSection<ELFT> &DynSymSec;
323 StringTableSection<ELFT::Is64Bits> &DynStrSec;
324 RelocationSection<ELFT> &RelaDynSec;
325 SymbolTable &SymTab;
326};
327}
328}
329#endif