blob: e36ac9eee7c898627bc7e3931d45000f0adca0c9 [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.
Rafael Espindola77572242015-10-02 19:37:55 +000073 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 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:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000174 void writeLocalSymbols(uint8_t *&Buf);
175 void writeGlobalSymbols(uint8_t *&Buf);
176
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000177 SymbolTable &Table;
178 StringTableSection<ELFT::Is64Bits> &StrTabSec;
179 unsigned NumVisible = 0;
180 unsigned NumLocals = 0;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000181 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000182};
183
184template <class ELFT>
185class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
186 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
187 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
188
189public:
190 RelocationSection(SymbolTableSection<ELFT> &DynSymSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000191 const GotSection<ELFT> &GotSec, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
193 void finalize() override;
194 void writeTo(uint8_t *Buf) override;
195 bool hasRelocs() const { return !Relocs.empty(); }
196 bool isRela() const { return IsRela; }
197
198private:
199 std::vector<DynamicReloc<ELFT>> Relocs;
200 SymbolTableSection<ELFT> &DynSymSec;
201 const GotSection<ELFT> &GotSec;
202 const bool IsRela;
203};
204
205template <class ELFT>
206class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
207public:
208 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
209 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
210 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
211 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
212 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
213 OutputSection(const PltSection<ELFT> &PltSec, const GotSection<ELFT> &GotSec,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000214 const OutputSection<ELFT> &BssSec, StringRef Name,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000215 uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000216 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217 void writeTo(uint8_t *Buf) override;
218
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219private:
Rafael Espindola71675852015-09-22 00:16:19 +0000220 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 const PltSection<ELFT> &PltSec;
222 const GotSection<ELFT> &GotSec;
Rafael Espindolac2d21192015-09-23 18:25:05 +0000223 const OutputSection<ELFT> &BssSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224};
225
226template <bool Is64Bits>
227class InterpSection final : public OutputSectionBase<Is64Bits> {
228public:
229 InterpSection();
230
231 void writeTo(uint8_t *Buf);
232};
233
234template <bool Is64Bits>
235class StringTableSection final : public OutputSectionBase<Is64Bits> {
236public:
237 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000238 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239 void add(StringRef S) { StrTabBuilder.add(S); }
240 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
241 StringRef data() const { return StrTabBuilder.data(); }
242 void writeTo(uint8_t *Buf) override;
243
244 void finalize() override {
245 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
246 this->Header.sh_size = StrTabBuilder.data().size();
247 }
248
249 bool isDynamic() const { return Dynamic; }
250
251private:
252 const bool Dynamic;
253 llvm::StringTableBuilder StrTabBuilder;
254};
255
256template <class ELFT>
257class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
258 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
259
260public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000261 HashTableSection(SymbolTableSection<ELFT> &DynSymSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262 void addSymbol(SymbolBody *S);
263
264 void finalize() override {
265 this->Header.sh_link = DynSymSec.getSectionIndex();
266
267 assert(DynSymSec.getNumSymbols() == Hashes.size() + 1);
268 unsigned NumEntries = 2; // nbucket and nchain.
269 NumEntries += DynSymSec.getNumSymbols(); // The chain entries.
270
271 // Create as many buckets as there are symbols.
272 // FIXME: This is simplistic. We can try to optimize it, but implementing
273 // support for SHT_GNU_HASH is probably even more profitable.
274 NumEntries += DynSymSec.getNumSymbols();
275 this->Header.sh_size = NumEntries * sizeof(Elf_Word);
276 }
277
278 void writeTo(uint8_t *Buf) override {
279 unsigned NumSymbols = DynSymSec.getNumSymbols();
280 auto *P = reinterpret_cast<Elf_Word *>(Buf);
281 *P++ = NumSymbols; // nbucket
282 *P++ = NumSymbols; // nchain
283
284 Elf_Word *Buckets = P;
285 Elf_Word *Chains = P + NumSymbols;
286
287 for (unsigned I = 1; I < NumSymbols; ++I) {
288 uint32_t Hash = Hashes[I - 1] % NumSymbols;
289 Chains[I] = Buckets[Hash];
290 Buckets[Hash] = I;
291 }
292 }
293
294 SymbolTableSection<ELFT> &getDynSymSec() { return DynSymSec; }
295
296private:
297 uint32_t hash(StringRef Name) {
298 uint32_t H = 0;
299 for (char C : Name) {
300 H = (H << 4) + C;
301 uint32_t G = H & 0xf0000000;
302 if (G)
303 H ^= G >> 24;
304 H &= ~G;
305 }
306 return H;
307 }
308 SymbolTableSection<ELFT> &DynSymSec;
309 std::vector<uint32_t> Hashes;
310};
311
312template <class ELFT>
313class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
314 typedef OutputSectionBase<ELFT::Is64Bits> Base;
315 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000316 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
317 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
318 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000319 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000320
321public:
322 DynamicSection(SymbolTable &SymTab, HashTableSection<ELFT> &HashSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000323 RelocationSection<ELFT> &RelaDynSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324 void finalize() override;
325 void writeTo(uint8_t *Buf) override;
326
Rafael Espindola77572242015-10-02 19:37:55 +0000327 OutputSection<ELFT> *PreInitArraySec = nullptr;
328 OutputSection<ELFT> *InitArraySec = nullptr;
329 OutputSection<ELFT> *FiniArraySec = nullptr;
330
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331private:
332 HashTableSection<ELFT> &HashSec;
333 SymbolTableSection<ELFT> &DynSymSec;
334 StringTableSection<ELFT::Is64Bits> &DynStrSec;
335 RelocationSection<ELFT> &RelaDynSec;
336 SymbolTable &SymTab;
337};
338}
339}
340#endif