blob: dc843500b38be8355551abc1a4f7d00c654d7ebe [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);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000046
Davide Italianob558b792015-09-25 04:22:13 +000047inline bool shouldKeepInSymtab(StringRef SymName) {
Davide Italiano85121bb2015-09-25 03:56:11 +000048 if (Config->DiscardNone)
49 return true;
50
51 // ELF defines dynamic locals as symbols which name starts with ".L".
52 return !(Config->DiscardLocals && SymName.startswith(".L"));
53}
54
Rafael Espindola71675852015-09-22 00:16:19 +000055// This represents a section in an output file.
56// Different sub classes represent different types of sections. Some contain
57// input sections, others are created by the linker.
58// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059// non-overlapping file offsets and VAs.
60template <bool Is64Bits> class OutputSectionBase {
61public:
62 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
63 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
64 llvm::ELF::Elf32_Shdr>::type HeaderT;
65
66 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
67 void setVA(uintX_t VA) { Header.sh_addr = VA; }
68 uintX_t getVA() const { return Header.sh_addr; }
69 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
70 template <llvm::object::endianness E>
71 void writeHeaderTo(typename llvm::object::ELFFile<
72 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
73 StringRef getName() { return Name; }
74 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
75
76 unsigned getSectionIndex() const { return SectionIndex; }
77 void setSectionIndex(unsigned I) { SectionIndex = I; }
78
79 // Returns the size of the section in the output file.
80 uintX_t getSize() { return Header.sh_size; }
81 void setSize(uintX_t Val) { Header.sh_size = Val; }
82 uintX_t getFlags() { return Header.sh_flags; }
83 uintX_t getFileOff() { return Header.sh_offset; }
84 uintX_t getAlign() {
85 // The ELF spec states that a value of 0 means the section has no alignment
86 // constraits.
87 return std::max<uintX_t>(Header.sh_addralign, 1);
88 }
89 uint32_t getType() { return Header.sh_type; }
90
91 static unsigned getAddrSize() { return Is64Bits ? 8 : 4; }
92
93 virtual void finalize() {}
94 virtual void writeTo(uint8_t *Buf) = 0;
95
96protected:
97 StringRef Name;
98 HeaderT Header;
99 unsigned SectionIndex;
100 ~OutputSectionBase() = default;
101};
102
103template <class ELFT>
104class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
105 typedef OutputSectionBase<ELFT::Is64Bits> Base;
106 typedef typename Base::uintX_t uintX_t;
107
108public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000109 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 void finalize() override {
111 this->Header.sh_size = Entries.size() * this->getAddrSize();
112 }
113 void writeTo(uint8_t *Buf) override {}
114 void addEntry(SymbolBody *Sym);
115 bool empty() const { return Entries.empty(); }
116 uintX_t getEntryAddr(const SymbolBody &B) const;
117
118private:
119 std::vector<const SymbolBody *> Entries;
120};
121
122template <class ELFT>
123class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
124 typedef OutputSectionBase<ELFT::Is64Bits> Base;
125 typedef typename Base::uintX_t uintX_t;
126
127public:
Rafael Espindola35c6af32015-09-25 17:19:10 +0000128 PltSection(const GotSection<ELFT> &GotSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000129 void finalize() override {
130 this->Header.sh_size = Entries.size() * EntrySize;
131 }
132 void writeTo(uint8_t *Buf) override;
133 void addEntry(SymbolBody *Sym);
134 bool empty() const { return Entries.empty(); }
135 uintX_t getEntryAddr(const SymbolBody &B) const;
136 static const unsigned EntrySize = 8;
137
138private:
139 std::vector<const SymbolBody *> Entries;
140 const GotSection<ELFT> &GotSec;
141};
142
143template <class ELFT> struct DynamicReloc {
144 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
145 const InputSection<ELFT> &C;
146 const Elf_Rel &RI;
147};
148
149template <class ELFT>
150class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
151public:
152 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
153 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
154 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
155 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
156 SymbolTableSection(SymbolTable &Table,
Rafael Espindolac2d21192015-09-23 18:25:05 +0000157 StringTableSection<ELFT::Is64Bits> &StrTabSec,
Rafael Espindola35c6af32015-09-25 17:19:10 +0000158 const OutputSection<ELFT> &BssSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159
160 void finalize() override {
161 this->Header.sh_size = getNumSymbols() * sizeof(Elf_Sym);
162 this->Header.sh_link = StrTabSec.getSectionIndex();
163 this->Header.sh_info = NumLocals + 1;
164 }
165
166 void writeTo(uint8_t *Buf) override;
167
168 const SymbolTable &getSymTable() const { return Table; }
169
170 void addSymbol(StringRef Name, bool isLocal = false) {
171 StrTabSec.add(Name);
172 ++NumVisible;
173 if (isLocal)
174 ++NumLocals;
175 }
176
Davide Italianoe44456b2015-09-23 01:50:53 +0000177 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000179
180private:
181 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;
320
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
327private:
328 HashTableSection<ELFT> &HashSec;
329 SymbolTableSection<ELFT> &DynSymSec;
330 StringTableSection<ELFT::Is64Bits> &DynStrSec;
331 RelocationSection<ELFT> &RelaDynSec;
332 SymbolTable &SymTab;
333};
334}
335}
336#endif