blob: 536aabdc1340d4a1f2d59b402d57f7596ab7fc8f [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
Rafael Espindola932efcf2015-10-19 20:24:44 +000050template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000052getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000053 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
54 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000055
Rui Ueyama89f4ec72015-12-25 07:01:09 +000056bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000057
Rafael Espindola71675852015-09-22 00:16:19 +000058// This represents a section in an output file.
59// Different sub classes represent different types of sections. Some contain
60// input sections, others are created by the linker.
61// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000063template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000065 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
66 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067
68 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
69 void setVA(uintX_t VA) { Header.sh_addr = VA; }
70 uintX_t getVA() const { return Header.sh_addr; }
71 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000072 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000073 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075
Rui Ueyama40845e62015-12-26 05:51:07 +000076 virtual void addSection(InputSectionBase<ELFT> *C) {}
77
Rui Ueyama2317d0d2015-10-15 20:55:22 +000078 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079
80 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000081 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 void setSize(uintX_t Val) { Header.sh_size = Val; }
83 uintX_t getFlags() { return Header.sh_flags; }
84 uintX_t getFileOff() { return Header.sh_offset; }
85 uintX_t getAlign() {
86 // The ELF spec states that a value of 0 means the section has no alignment
87 // constraits.
88 return std::max<uintX_t>(Header.sh_addralign, 1);
89 }
90 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000091 void updateAlign(uintX_t Align) {
92 if (Align > Header.sh_addralign)
93 Header.sh_addralign = Align;
94 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096 virtual void finalize() {}
97 virtual void writeTo(uint8_t *Buf) = 0;
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000098 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099
100protected:
101 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000102 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103};
104
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000105template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
106 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 typedef typename Base::uintX_t uintX_t;
108
109public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000110 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000111 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000112 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000113 void addEntry(SymbolBody *Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 void addMipsLocalEntry();
George Rimar90cd0a82015-12-01 19:20:26 +0000115 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000116 bool addCurrentModuleTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000117 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000118 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
119 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000120 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000121 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000122
Igor Kudrin304860a2015-11-12 04:39:49 +0000123 // Returns the symbol which corresponds to the first entry of the global part
124 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
125 // table properties.
126 // Returns nullptr if the global part is empty.
127 const SymbolBody *getMipsFirstGlobalEntry() const;
128
129 // Returns the number of entries in the local part of GOT including
130 // the number of reserved entries. This method is MIPS-specific.
131 unsigned getMipsLocalEntriesNum() const;
132
George Rimarb17f7392015-12-01 18:24:07 +0000133 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
134
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000135private:
136 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000137 uint32_t LocalTlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000138 uint32_t MipsLocalEntries = 0;
139 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
140
141 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142};
143
George Rimar648a2c32015-10-20 08:54:27 +0000144template <class ELFT>
145class GotPltSection final : public OutputSectionBase<ELFT> {
146 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
147
148public:
149 GotPltSection();
150 void finalize() override;
151 void writeTo(uint8_t *Buf) override;
152 void addEntry(SymbolBody *Sym);
153 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000154
155private:
156 std::vector<const SymbolBody *> Entries;
157};
158
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000159template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
160 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161 typedef typename Base::uintX_t uintX_t;
162
163public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000164 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000165 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 void writeTo(uint8_t *Buf) override;
167 void addEntry(SymbolBody *Sym);
168 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169
170private:
George Rimar77b77792015-11-25 22:15:01 +0000171 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172};
173
174template <class ELFT> struct DynamicReloc {
175 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000176 InputSectionBase<ELFT> *C;
177 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000178};
179
180template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000181class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000182public:
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
184 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
185 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000186 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000187 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000188 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189
Rui Ueyama0db335f2015-10-07 16:58:54 +0000190 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000191 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000192 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000193 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000194 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195
Rafael Espindolae2c24612016-01-29 01:24:25 +0000196 ArrayRef<std::pair<SymbolBody *, unsigned>> getSymbols() const {
197 return Symbols;
198 }
199
200 unsigned NumLocals = 0;
201 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000202
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000204 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000205 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000206
Igor Kudrin853b88d2015-10-20 20:52:14 +0000207 static uint8_t getSymbolBinding(SymbolBody *Body);
208
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000209 SymbolTable<ELFT> &Table;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000210 std::vector<std::pair<SymbolBody *, unsigned>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000211};
212
213template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000214class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
216 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000217 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218
219public:
George Rimar648a2c32015-10-20 08:54:27 +0000220 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000222 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000223 void finalize() override;
224 void writeTo(uint8_t *Buf) override;
225 bool hasRelocs() const { return !Relocs.empty(); }
226 bool isRela() const { return IsRela; }
227
George Rimara07ff662015-12-21 10:12:06 +0000228 bool Static = false;
229
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230private:
George Rimar5828c232015-11-30 17:49:19 +0000231 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
232 Elf_Rel *N);
233
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000234 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 const bool IsRela;
236};
237
238template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000239class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
242 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
243 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
244 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000245 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000246 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000247 void addSection(InputSectionBase<ELFT> *C) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248 void writeTo(uint8_t *Buf) override;
249
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000250private:
Rafael Espindola71675852015-09-22 00:16:19 +0000251 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252};
253
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000254template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000255class MergeOutputSection final : public OutputSectionBase<ELFT> {
256 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
257
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000258 bool shouldTailMerge() const;
259
Rafael Espindolac159c962015-10-19 21:00:02 +0000260public:
261 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000262 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000263 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000264 unsigned getOffset(StringRef Val);
265 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000266
267private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000268 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000269};
270
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000271// FDE or CIE
272template <class ELFT> struct EHRegion {
273 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
274 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
275 StringRef data() const;
276 EHInputSection<ELFT> *S;
277 unsigned Index;
278};
279
280template <class ELFT> struct Cie : public EHRegion<ELFT> {
281 Cie(EHInputSection<ELFT> *S, unsigned Index);
282 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000283 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000284};
285
286template <class ELFT>
287class EHOutputSection final : public OutputSectionBase<ELFT> {
288public:
289 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
290 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
291 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
292 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
293 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
294 void writeTo(uint8_t *Buf) override;
295
296 template <bool IsRela>
297 void addSectionAux(
298 EHInputSection<ELFT> *S,
299 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
300 Rels);
301
Rui Ueyama40845e62015-12-26 05:51:07 +0000302 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000303
304private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000305 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000306 uintX_t readEntryLength(ArrayRef<uint8_t> D);
307
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000308 std::vector<EHInputSection<ELFT> *> Sections;
309 std::vector<Cie<ELFT>> Cies;
310
311 // Maps CIE content + personality to a index in Cies.
312 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
313};
314
Rafael Espindolac159c962015-10-19 21:00:02 +0000315template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000316class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000317public:
318 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000319 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000320};
321
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000322template <class ELFT>
323class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000325 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000326 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000327 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000328 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000329 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000330 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331 bool isDynamic() const { return Dynamic; }
332
333private:
334 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000335 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000336 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000337 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000338};
339
340template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000341class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000342 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
343
344public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000345 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000346 void finalize() override;
347 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348};
349
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000350// Outputs GNU Hash section. For detailed explanation see:
351// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
352template <class ELFT>
353class GnuHashTableSection final : public OutputSectionBase<ELFT> {
354 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
355 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
356 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
357
358public:
359 GnuHashTableSection();
360 void finalize() override;
361 void writeTo(uint8_t *Buf) override;
362
Igor Kudrinf1d60292015-10-28 07:05:56 +0000363 // Adds symbols to the hash table.
364 // Sorts the input to satisfy GNU hash section requirements.
Rafael Espindolae2c24612016-01-29 01:24:25 +0000365 void addSymbols(std::vector<std::pair<SymbolBody *, unsigned>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000366
367private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000368 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000369 static unsigned calcMaskWords(unsigned NumHashed);
370
371 void writeHeader(uint8_t *&Buf);
372 void writeBloomFilter(uint8_t *&Buf);
373 void writeHashTable(uint8_t *Buf);
374
Igor Kudrinf1d60292015-10-28 07:05:56 +0000375 struct HashedSymbolData {
376 SymbolBody *Body;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000377 unsigned STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000378 uint32_t Hash;
379 };
380
381 std::vector<HashedSymbolData> HashedSymbols;
382
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000383 unsigned MaskWords;
384 unsigned NBuckets;
385 unsigned Shift2;
386};
387
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000388template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000389class DynamicSection final : public OutputSectionBase<ELFT> {
390 typedef OutputSectionBase<ELFT> Base;
391 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000392 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
393 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000394 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000395 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000396 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
397
Rui Ueyama909cc682016-02-02 03:11:27 +0000398 // The .dynamic section contains information for the dynamic linker.
399 // The section consists of fixed size entries, which consist of
400 // type and value fields. Value are one of plain integers, symbol
401 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000402 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 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000416
417 // finalize() fills this vector with the section contents. finalize()
418 // cannot directly create final section contents because when the
419 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000420 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000421
422public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000423 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000424 void finalize() override;
425 void writeTo(uint8_t *Buf) override;
426
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000427 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
428 OutputSectionBase<ELFT> *InitArraySec = nullptr;
429 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000430
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000431private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000432 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000433};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000434
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000435template <class ELFT>
436class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
437 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
438
439public:
440 MipsReginfoOutputSection();
441 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000442 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000443
444private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000445 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000446};
447
George Rimarf6bc65a2016-01-15 13:34:52 +0000448// --eh-frame-hdr option tells linker to construct a header for all the
449// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
450// and also to a PT_GNU_EH_FRAME segment.
451// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
452// calling dl_iterate_phdr.
453// This section contains a lookup table for quick binary search of FDEs.
454// Detailed info about internals can be found in Ian Lance Taylor's blog:
455// http://www.airs.com/blog/archives/460 (".eh_frame")
456// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
457template <class ELFT>
458class EhFrameHeader final : public OutputSectionBase<ELFT> {
459 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
460
461public:
462 EhFrameHeader();
463 void writeTo(uint8_t *Buf) override;
464
465 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
466 void assignEhFrame(EHOutputSection<ELFT> *Sec);
467 void reserveFde();
468
469 bool Live = false;
470
471private:
472 struct FdeData {
473 uint8_t Enc;
474 size_t Off;
475 uint8_t *PCRel;
476 };
477
478 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
479
480 EHOutputSection<ELFT> *Sec = nullptr;
481 std::vector<FdeData> FdeList;
482};
483
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000484// All output sections that are hadnled by the linker specially are
485// globally accessible. Writer initializes them, so don't use them
486// until Writer is initialized.
487template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000488 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000489 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000490 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000491 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000492 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000493 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000494 static GotSection<ELFT> *Got;
495 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000496 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000497 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000498 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000499 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000500 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000501 static PltSection<ELFT> *Plt;
502 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000503 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000504 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000505 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000506 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000507 static SymbolTableSection<ELFT> *DynSymTab;
508 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000509 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000510};
Rui Ueyamad888d102015-10-09 19:34:55 +0000511
512template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000513template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000514template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000515template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000516template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
517template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000518template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000519template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000520template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000521template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000522template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000523template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
524template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000525template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000526template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000527template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000528template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000529template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
530template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000531template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000532
533} // namespace elf2
534} // namespace lld
535
536#endif // LLD_ELF_OUTPUT_SECTIONS_H