blob: 10f55a9cddc4c7a3e23edd1587784f6ba7a1c319 [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
Rafael Espindolac159c962015-10-19 21:00:02 +000015#include "llvm/ADT/MapVector.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000016#include "llvm/MC/StringTableBuilder.h"
17#include "llvm/Object/ELF.h"
18
Davide Italiano85121bb2015-09-25 03:56:11 +000019#include "Config.h"
20
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021#include <type_traits>
22
23namespace lld {
24namespace elf2 {
25
26class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000027template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000029template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000030template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000031template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000032template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000033template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000034template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT> class OutputSection;
36template <class ELFT> class ObjectFile;
37template <class ELFT> class DefinedRegular;
38
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000040static inline typename llvm::object::ELFFile<ELFT>::uintX_t
41getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
42 return 0;
43}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000044
45template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000046static inline typename llvm::object::ELFFile<ELFT>::uintX_t
47getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
48 return Rel.r_addend;
49}
50
51template <class ELFT>
52typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
53
54template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000056getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000057 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
58 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000059
Rui Ueyama89f4ec72015-12-25 07:01:09 +000060bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000061
Rafael Espindola71675852015-09-22 00:16:19 +000062// This represents a section in an output file.
63// Different sub classes represent different types of sections. Some contain
64// input sections, others are created by the linker.
65// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000067template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000069 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
70 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071
72 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
73 void setVA(uintX_t VA) { Header.sh_addr = VA; }
74 uintX_t getVA() const { return Header.sh_addr; }
75 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
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 addLocalSymbol(StringRef Name);
199 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000200 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000201 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202
Igor Kudrinf1d60292015-10-28 07:05:56 +0000203 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000204
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000206 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000207 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000208
Igor Kudrin853b88d2015-10-20 20:52:14 +0000209 static uint8_t getSymbolBinding(SymbolBody *Body);
210
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000211 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000212 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000213 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215};
216
217template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000218class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000219 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
220 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000221 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222
223public:
George Rimar648a2c32015-10-20 08:54:27 +0000224 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000226 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000227 void finalize() override;
228 void writeTo(uint8_t *Buf) override;
229 bool hasRelocs() const { return !Relocs.empty(); }
230 bool isRela() const { return IsRela; }
231
George Rimara07ff662015-12-21 10:12:06 +0000232 bool Static = false;
233
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234private:
George Rimar5828c232015-11-30 17:49:19 +0000235 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
236 Elf_Rel *N);
237
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239 const bool IsRela;
240};
241
242template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000243class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000245 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
246 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
247 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
248 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000249 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000250 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000251 void addSection(InputSectionBase<ELFT> *C) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252 void writeTo(uint8_t *Buf) override;
253
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000254private:
Rafael Espindola71675852015-09-22 00:16:19 +0000255 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256};
257
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000258template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000259class MergeOutputSection final : public OutputSectionBase<ELFT> {
260 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
261
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000262 bool shouldTailMerge() const;
263
Rafael Espindolac159c962015-10-19 21:00:02 +0000264public:
265 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000266 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000267 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000268 unsigned getOffset(StringRef Val);
269 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000270
271private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000272 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000273};
274
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000275// FDE or CIE
276template <class ELFT> struct EHRegion {
277 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
278 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
279 StringRef data() const;
280 EHInputSection<ELFT> *S;
281 unsigned Index;
282};
283
284template <class ELFT> struct Cie : public EHRegion<ELFT> {
285 Cie(EHInputSection<ELFT> *S, unsigned Index);
286 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000287 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000288};
289
290template <class ELFT>
291class EHOutputSection final : public OutputSectionBase<ELFT> {
292public:
293 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
294 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
295 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
296 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
297 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
298 void writeTo(uint8_t *Buf) override;
299
300 template <bool IsRela>
301 void addSectionAux(
302 EHInputSection<ELFT> *S,
303 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
304 Rels);
305
Rui Ueyama40845e62015-12-26 05:51:07 +0000306 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000307
308private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000309 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000310 uintX_t readEntryLength(ArrayRef<uint8_t> D);
311
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000312 std::vector<EHInputSection<ELFT> *> Sections;
313 std::vector<Cie<ELFT>> Cies;
314
315 // Maps CIE content + personality to a index in Cies.
316 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
317};
318
Rafael Espindolac159c962015-10-19 21:00:02 +0000319template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000320class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000321public:
322 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000323 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324};
325
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000326template <class ELFT>
327class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000329 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000330 StringTableSection(StringRef Name, bool Dynamic);
Rui Ueyama76c00632016-01-07 02:35:32 +0000331 void reserve(StringRef S);
332 size_t addString(StringRef S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000333 void writeTo(uint8_t *Buf) override;
Rui Ueyama76c00632016-01-07 02:35:32 +0000334 size_t getSize() const { return Used + Reserved; }
335 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336 bool isDynamic() const { return Dynamic; }
337
338private:
339 const bool Dynamic;
Rui Ueyama76c00632016-01-07 02:35:32 +0000340 std::vector<StringRef> Strings;
341 size_t Used = 1; // ELF string tables start with a NUL byte, so 1.
342 size_t Reserved = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343};
344
345template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000346class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000347 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
348
349public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000350 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000351 void finalize() override;
352 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000353};
354
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000355// Outputs GNU Hash section. For detailed explanation see:
356// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
357template <class ELFT>
358class GnuHashTableSection final : public OutputSectionBase<ELFT> {
359 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
360 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
361 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
362
363public:
364 GnuHashTableSection();
365 void finalize() override;
366 void writeTo(uint8_t *Buf) override;
367
Igor Kudrinf1d60292015-10-28 07:05:56 +0000368 // Adds symbols to the hash table.
369 // Sorts the input to satisfy GNU hash section requirements.
370 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000371
372private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000373 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000374 static unsigned calcMaskWords(unsigned NumHashed);
375
376 void writeHeader(uint8_t *&Buf);
377 void writeBloomFilter(uint8_t *&Buf);
378 void writeHashTable(uint8_t *Buf);
379
Igor Kudrinf1d60292015-10-28 07:05:56 +0000380 struct HashedSymbolData {
381 SymbolBody *Body;
382 uint32_t Hash;
383 };
384
385 std::vector<HashedSymbolData> HashedSymbols;
386
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000387 unsigned MaskWords;
388 unsigned NBuckets;
389 unsigned Shift2;
390};
391
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000392template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000393class DynamicSection final : public OutputSectionBase<ELFT> {
394 typedef OutputSectionBase<ELFT> Base;
395 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000396 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
397 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000398 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000399 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000400 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
401
402 struct Entry {
403 int32_t Tag;
404 union {
405 OutputSectionBase<ELFT> *OutSec;
406 uint64_t Val;
407 const SymbolBody *Sym;
408 };
409 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
410 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
411 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
412 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
413 Entry(int32_t Tag, const SymbolBody *Sym)
414 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
415 };
416 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000417
418public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000419 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000420 void finalize() override;
421 void writeTo(uint8_t *Buf) override;
422
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000423 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
424 OutputSectionBase<ELFT> *InitArraySec = nullptr;
425 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000426
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000427private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000428 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000429};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000430
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000431template <class ELFT>
432class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
433 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
434
435public:
436 MipsReginfoOutputSection();
437 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000438 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000439
440private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000441 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000442};
443
George Rimarf6bc65a2016-01-15 13:34:52 +0000444// --eh-frame-hdr option tells linker to construct a header for all the
445// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
446// and also to a PT_GNU_EH_FRAME segment.
447// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
448// calling dl_iterate_phdr.
449// This section contains a lookup table for quick binary search of FDEs.
450// Detailed info about internals can be found in Ian Lance Taylor's blog:
451// http://www.airs.com/blog/archives/460 (".eh_frame")
452// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
453template <class ELFT>
454class EhFrameHeader final : public OutputSectionBase<ELFT> {
455 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
456
457public:
458 EhFrameHeader();
459 void writeTo(uint8_t *Buf) override;
460
461 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
462 void assignEhFrame(EHOutputSection<ELFT> *Sec);
463 void reserveFde();
464
465 bool Live = false;
466
467private:
468 struct FdeData {
469 uint8_t Enc;
470 size_t Off;
471 uint8_t *PCRel;
472 };
473
474 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
475
476 EHOutputSection<ELFT> *Sec = nullptr;
477 std::vector<FdeData> FdeList;
478};
479
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000480// All output sections that are hadnled by the linker specially are
481// globally accessible. Writer initializes them, so don't use them
482// until Writer is initialized.
483template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000484 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000485 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000486 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000487 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000488 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000489 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000490 static GotSection<ELFT> *Got;
491 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000492 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000493 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000494 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000495 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000496 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000497 static PltSection<ELFT> *Plt;
498 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000499 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000500 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000501 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000502 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000503 static SymbolTableSection<ELFT> *DynSymTab;
504 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000505 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000506};
Rui Ueyamad888d102015-10-09 19:34:55 +0000507
508template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000509template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000510template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000511template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000512template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
513template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000514template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000515template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000516template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000517template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000518template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000519template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
520template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000521template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000522template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000523template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000524template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000525template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
526template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000527template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000528
529} // namespace elf2
530} // namespace lld
531
532#endif // LLD_ELF_OUTPUT_SECTIONS_H