blob: 93bd997c3b302dba4c130253d98080f2779b62d6 [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;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000026template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000031template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000032template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000033template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class OutputSection;
35template <class ELFT> class ObjectFile;
36template <class ELFT> class DefinedRegular;
37
Rafael Espindola5805c4f2015-09-21 21:38:08 +000038template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000039static inline typename llvm::object::ELFFile<ELFT>::uintX_t
40getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
41 return 0;
42}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043
44template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000045static inline typename llvm::object::ELFFile<ELFT>::uintX_t
46getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
47 return Rel.r_addend;
48}
49
50template <class ELFT>
51typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
52
53template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000055getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000056 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
57 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000058
Rui Ueyama89f4ec72015-12-25 07:01:09 +000059bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000060
Rafael Espindola71675852015-09-22 00:16:19 +000061// This represents a section in an output file.
62// Different sub classes represent different types of sections. Some contain
63// input sections, others are created by the linker.
64// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000066template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000068 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
69 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
71 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
72 void setVA(uintX_t VA) { Header.sh_addr = VA; }
73 uintX_t getVA() const { return Header.sh_addr; }
74 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000075 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000076 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078
Rui Ueyama40845e62015-12-26 05:51:07 +000079 virtual void addSection(InputSectionBase<ELFT> *C) {}
80
Rui Ueyama2317d0d2015-10-15 20:55:22 +000081 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082
83 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000084 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085 void setSize(uintX_t Val) { Header.sh_size = Val; }
86 uintX_t getFlags() { return Header.sh_flags; }
87 uintX_t getFileOff() { return Header.sh_offset; }
88 uintX_t getAlign() {
89 // The ELF spec states that a value of 0 means the section has no alignment
90 // constraits.
91 return std::max<uintX_t>(Header.sh_addralign, 1);
92 }
93 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000094 void updateAlign(uintX_t Align) {
95 if (Align > Header.sh_addralign)
96 Header.sh_addralign = Align;
97 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000098
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099 virtual void finalize() {}
100 virtual void writeTo(uint8_t *Buf) = 0;
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000101 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102
103protected:
104 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000105 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106};
107
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000108template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
109 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000110 typedef typename Base::uintX_t uintX_t;
111
112public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000113 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000114 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000115 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116 void addEntry(SymbolBody *Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000117 void addMipsLocalEntry();
George Rimar90cd0a82015-12-01 19:20:26 +0000118 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000119 bool addCurrentModuleTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000120 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000121 uintX_t getEntryAddr(const SymbolBody &B) const;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000122 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
123 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000124 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000125 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126
Igor Kudrin304860a2015-11-12 04:39:49 +0000127 // Returns the symbol which corresponds to the first entry of the global part
128 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
129 // table properties.
130 // Returns nullptr if the global part is empty.
131 const SymbolBody *getMipsFirstGlobalEntry() const;
132
133 // Returns the number of entries in the local part of GOT including
134 // the number of reserved entries. This method is MIPS-specific.
135 unsigned getMipsLocalEntriesNum() const;
136
George Rimarb17f7392015-12-01 18:24:07 +0000137 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
138
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000139private:
140 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000141 uint32_t LocalTlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000142 uint32_t MipsLocalEntries = 0;
143 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
144
145 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000146};
147
George Rimar648a2c32015-10-20 08:54:27 +0000148template <class ELFT>
149class GotPltSection final : public OutputSectionBase<ELFT> {
150 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
151
152public:
153 GotPltSection();
154 void finalize() override;
155 void writeTo(uint8_t *Buf) override;
156 void addEntry(SymbolBody *Sym);
157 bool empty() const;
158 uintX_t getEntryAddr(const SymbolBody &B) const;
159
160private:
161 std::vector<const SymbolBody *> Entries;
162};
163
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000164template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
165 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 typedef typename Base::uintX_t uintX_t;
167
168public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000169 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000170 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171 void writeTo(uint8_t *Buf) override;
172 void addEntry(SymbolBody *Sym);
173 bool empty() const { return Entries.empty(); }
174 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000175
176private:
George Rimar77b77792015-11-25 22:15:01 +0000177 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178};
179
180template <class ELFT> struct DynamicReloc {
181 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000182 InputSectionBase<ELFT> *C;
183 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000184};
185
186template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000187class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188public:
189 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
190 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
191 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000193 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000194 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195
Rui Ueyama0db335f2015-10-07 16:58:54 +0000196 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000198 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000199 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000200 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201
Rafael Espindolae2c24612016-01-29 01:24:25 +0000202 ArrayRef<std::pair<SymbolBody *, unsigned>> getSymbols() const {
203 return Symbols;
204 }
205
206 unsigned NumLocals = 0;
207 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000208
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000209private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000210 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000211 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000212
Igor Kudrin853b88d2015-10-20 20:52:14 +0000213 static uint8_t getSymbolBinding(SymbolBody *Body);
214
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000215 SymbolTable<ELFT> &Table;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000216 std::vector<std::pair<SymbolBody *, unsigned>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217};
218
219template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000220class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
222 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000223 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224
225public:
George Rimar648a2c32015-10-20 08:54:27 +0000226 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000228 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229 void finalize() override;
230 void writeTo(uint8_t *Buf) override;
231 bool hasRelocs() const { return !Relocs.empty(); }
232 bool isRela() const { return IsRela; }
233
George Rimara07ff662015-12-21 10:12:06 +0000234 bool Static = false;
235
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236private:
George Rimar5828c232015-11-30 17:49:19 +0000237 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
238 Elf_Rel *N);
239
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 const bool IsRela;
242};
243
244template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000245class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
248 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
249 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
250 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000251 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000252 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000253 void addSection(InputSectionBase<ELFT> *C) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254 void writeTo(uint8_t *Buf) override;
255
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256private:
Rafael Espindola71675852015-09-22 00:16:19 +0000257 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258};
259
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000260template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000261class MergeOutputSection final : public OutputSectionBase<ELFT> {
262 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
263
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000264 bool shouldTailMerge() const;
265
Rafael Espindolac159c962015-10-19 21:00:02 +0000266public:
267 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000268 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000269 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000270 unsigned getOffset(StringRef Val);
271 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000272
273private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000274 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000275};
276
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000277// FDE or CIE
278template <class ELFT> struct EHRegion {
279 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
280 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
281 StringRef data() const;
282 EHInputSection<ELFT> *S;
283 unsigned Index;
284};
285
286template <class ELFT> struct Cie : public EHRegion<ELFT> {
287 Cie(EHInputSection<ELFT> *S, unsigned Index);
288 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000289 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000290};
291
292template <class ELFT>
293class EHOutputSection final : public OutputSectionBase<ELFT> {
294public:
295 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
296 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
297 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
298 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
299 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
300 void writeTo(uint8_t *Buf) override;
301
302 template <bool IsRela>
303 void addSectionAux(
304 EHInputSection<ELFT> *S,
305 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
306 Rels);
307
Rui Ueyama40845e62015-12-26 05:51:07 +0000308 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000309
310private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000311 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000312 uintX_t readEntryLength(ArrayRef<uint8_t> D);
313
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000314 std::vector<EHInputSection<ELFT> *> Sections;
315 std::vector<Cie<ELFT>> Cies;
316
317 // Maps CIE content + personality to a index in Cies.
318 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
319};
320
Rafael Espindolac159c962015-10-19 21:00:02 +0000321template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000322class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000323public:
324 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000325 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000326};
327
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000328template <class ELFT>
329class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000330public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000331 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000332 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000333 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000334 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000335 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000336 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000337 bool isDynamic() const { return Dynamic; }
338
339private:
340 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000341 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000342 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000343 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000344};
345
346template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000347class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
349
350public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000351 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000352 void finalize() override;
353 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000354};
355
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000356// Outputs GNU Hash section. For detailed explanation see:
357// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
358template <class ELFT>
359class GnuHashTableSection final : public OutputSectionBase<ELFT> {
360 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
361 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
362 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
363
364public:
365 GnuHashTableSection();
366 void finalize() override;
367 void writeTo(uint8_t *Buf) override;
368
Igor Kudrinf1d60292015-10-28 07:05:56 +0000369 // Adds symbols to the hash table.
370 // Sorts the input to satisfy GNU hash section requirements.
Rafael Espindolae2c24612016-01-29 01:24:25 +0000371 void addSymbols(std::vector<std::pair<SymbolBody *, unsigned>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000372
373private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000374 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000375 static unsigned calcMaskWords(unsigned NumHashed);
376
377 void writeHeader(uint8_t *&Buf);
378 void writeBloomFilter(uint8_t *&Buf);
379 void writeHashTable(uint8_t *Buf);
380
Igor Kudrinf1d60292015-10-28 07:05:56 +0000381 struct HashedSymbolData {
382 SymbolBody *Body;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000383 unsigned STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000384 uint32_t Hash;
385 };
386
387 std::vector<HashedSymbolData> HashedSymbols;
388
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000389 unsigned MaskWords;
390 unsigned NBuckets;
391 unsigned Shift2;
392};
393
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000394template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000395class DynamicSection final : public OutputSectionBase<ELFT> {
396 typedef OutputSectionBase<ELFT> Base;
397 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000398 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
399 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000400 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000401 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000402 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
403
404 struct Entry {
405 int32_t Tag;
406 union {
407 OutputSectionBase<ELFT> *OutSec;
408 uint64_t Val;
409 const SymbolBody *Sym;
410 };
411 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
412 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
413 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
414 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
415 Entry(int32_t Tag, const SymbolBody *Sym)
416 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
417 };
418 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419
420public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000421 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000422 void finalize() override;
423 void writeTo(uint8_t *Buf) override;
424
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000425 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
426 OutputSectionBase<ELFT> *InitArraySec = nullptr;
427 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000428
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000429private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000430 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000431};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000432
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000433template <class ELFT>
434class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
435 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
436
437public:
438 MipsReginfoOutputSection();
439 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000440 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000441
442private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000443 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000444};
445
George Rimarf6bc65a2016-01-15 13:34:52 +0000446// --eh-frame-hdr option tells linker to construct a header for all the
447// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
448// and also to a PT_GNU_EH_FRAME segment.
449// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
450// calling dl_iterate_phdr.
451// This section contains a lookup table for quick binary search of FDEs.
452// Detailed info about internals can be found in Ian Lance Taylor's blog:
453// http://www.airs.com/blog/archives/460 (".eh_frame")
454// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
455template <class ELFT>
456class EhFrameHeader final : public OutputSectionBase<ELFT> {
457 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
458
459public:
460 EhFrameHeader();
461 void writeTo(uint8_t *Buf) override;
462
463 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
464 void assignEhFrame(EHOutputSection<ELFT> *Sec);
465 void reserveFde();
466
467 bool Live = false;
468
469private:
470 struct FdeData {
471 uint8_t Enc;
472 size_t Off;
473 uint8_t *PCRel;
474 };
475
476 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
477
478 EHOutputSection<ELFT> *Sec = nullptr;
479 std::vector<FdeData> FdeList;
480};
481
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000482// All output sections that are hadnled by the linker specially are
483// globally accessible. Writer initializes them, so don't use them
484// until Writer is initialized.
485template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000486 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000487 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000488 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000489 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000490 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000491 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000492 static GotSection<ELFT> *Got;
493 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000494 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000495 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000496 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000497 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000498 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000499 static PltSection<ELFT> *Plt;
500 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000501 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000502 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000503 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000504 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000505 static SymbolTableSection<ELFT> *DynSymTab;
506 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000507 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000508};
Rui Ueyamad888d102015-10-09 19:34:55 +0000509
510template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000511template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000512template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000513template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000514template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
515template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000516template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000517template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000518template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000519template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000520template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000521template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
522template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000523template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000524template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000525template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000526template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000527template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
528template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000529template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000530
531} // namespace elf2
532} // namespace lld
533
534#endif // LLD_ELF_OUTPUT_SECTIONS_H