blob: 31f021d504ef793cba8ba14a27f6bd31e3fe47d7 [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
George Rimarbfb7bf72015-12-21 10:00:12 +000039// Flag to force GOT to be in output if we have relocations
40// that relies on its address.
41extern bool HasGotOffRel;
42
Rafael Espindola5805c4f2015-09-21 21:38:08 +000043template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000044static inline typename llvm::object::ELFFile<ELFT>::uintX_t
45getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Rel) {
46 return 0;
47}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048
49template <class ELFT>
Rafael Espindola932efcf2015-10-19 20:24:44 +000050static inline typename llvm::object::ELFFile<ELFT>::uintX_t
51getAddend(const typename llvm::object::ELFFile<ELFT>::Elf_Rela &Rel) {
52 return Rel.r_addend;
53}
54
55template <class ELFT>
56typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
57
58template <class ELFT, bool IsRela>
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000060getLocalRelTarget(const ObjectFile<ELFT> &File,
George Rimar0b8ed1d2015-12-21 10:37:33 +000061 const llvm::object::Elf_Rel_Impl<ELFT, IsRela> &Rel,
62 typename llvm::object::ELFFile<ELFT>::uintX_t Addend);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000063
Rui Ueyama89f4ec72015-12-25 07:01:09 +000064bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000065
66template <class ELFT>
67bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000068 const ObjectFile<ELFT> &File, StringRef Name,
69 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000070
Rafael Espindola71675852015-09-22 00:16:19 +000071// This represents a section in an output file.
72// Different sub classes represent different types of sections. Some contain
73// input sections, others are created by the linker.
74// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000076template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000078 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
79 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000080
81 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
82 void setVA(uintX_t VA) { Header.sh_addr = VA; }
83 uintX_t getVA() const { return Header.sh_addr; }
84 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000085 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rui Ueyama40845e62015-12-26 05:51:07 +000088 virtual void addSection(InputSectionBase<ELFT> *C) {}
89
Rui Ueyama2317d0d2015-10-15 20:55:22 +000090 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091
92 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000093 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000094 void setSize(uintX_t Val) { Header.sh_size = Val; }
95 uintX_t getFlags() { return Header.sh_flags; }
96 uintX_t getFileOff() { return Header.sh_offset; }
97 uintX_t getAlign() {
98 // The ELF spec states that a value of 0 means the section has no alignment
99 // constraits.
100 return std::max<uintX_t>(Header.sh_addralign, 1);
101 }
102 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +0000103 void updateAlign(uintX_t Align) {
104 if (Align > Header.sh_addralign)
105 Header.sh_addralign = Align;
106 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000108 virtual void finalize() {}
109 virtual void writeTo(uint8_t *Buf) = 0;
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +0000110 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000111
112protected:
113 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000114 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000115};
116
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000117template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
118 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000119 typedef typename Base::uintX_t uintX_t;
120
121public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000122 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000123 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000124 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000125 void addEntry(SymbolBody *Sym);
George Rimar90cd0a82015-12-01 19:20:26 +0000126 bool addDynTlsEntry(SymbolBody *Sym);
George Rimar95c1a582015-12-07 08:02:20 +0000127 bool addCurrentModuleTlsIndex();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000128 bool empty() const { return Entries.empty(); }
129 uintX_t getEntryAddr(const SymbolBody &B) const;
George Rimar90cd0a82015-12-01 19:20:26 +0000130 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000131 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000132
Igor Kudrin304860a2015-11-12 04:39:49 +0000133 // Returns the symbol which corresponds to the first entry of the global part
134 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
135 // table properties.
136 // Returns nullptr if the global part is empty.
137 const SymbolBody *getMipsFirstGlobalEntry() const;
138
139 // Returns the number of entries in the local part of GOT including
140 // the number of reserved entries. This method is MIPS-specific.
141 unsigned getMipsLocalEntriesNum() const;
142
George Rimarb17f7392015-12-01 18:24:07 +0000143 uint32_t getLocalTlsIndexVA() { return Base::getVA() + LocalTlsIndexOff; }
144
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000145private:
146 std::vector<const SymbolBody *> Entries;
George Rimarb17f7392015-12-01 18:24:07 +0000147 uint32_t LocalTlsIndexOff = -1;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000148};
149
George Rimar648a2c32015-10-20 08:54:27 +0000150template <class ELFT>
151class GotPltSection final : public OutputSectionBase<ELFT> {
152 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
153
154public:
155 GotPltSection();
156 void finalize() override;
157 void writeTo(uint8_t *Buf) override;
158 void addEntry(SymbolBody *Sym);
159 bool empty() const;
160 uintX_t getEntryAddr(const SymbolBody &B) const;
161
162private:
163 std::vector<const SymbolBody *> Entries;
164};
165
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000166template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
167 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 typedef typename Base::uintX_t uintX_t;
169
170public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000171 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000172 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000173 void writeTo(uint8_t *Buf) override;
174 void addEntry(SymbolBody *Sym);
175 bool empty() const { return Entries.empty(); }
176 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000177
178private:
George Rimar77b77792015-11-25 22:15:01 +0000179 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000180};
181
182template <class ELFT> struct DynamicReloc {
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
Michael J. Spencer627ae702015-11-13 00:28:34 +0000184 InputSectionBase<ELFT> *C;
185 const Elf_Rel *RI;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000186};
187
188template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000189class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000190public:
191 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
192 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
193 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000194 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000195 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000196 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197
Rui Ueyama0db335f2015-10-07 16:58:54 +0000198 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000200 void addLocalSymbol(StringRef Name);
201 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000202 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000203 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204
Igor Kudrinf1d60292015-10-28 07:05:56 +0000205 ArrayRef<SymbolBody *> getSymbols() const { return Symbols; }
Igor Kudrinab665fc2015-10-20 21:47:58 +0000206
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000208 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000209 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000210
Igor Kudrin853b88d2015-10-20 20:52:14 +0000211 static uint8_t getSymbolBinding(SymbolBody *Body);
212
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000213 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000214 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000215 std::vector<SymbolBody *> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000216 unsigned NumVisible = 0;
217 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000218};
219
220template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000221class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000222 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
223 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000224 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000225
226public:
George Rimar648a2c32015-10-20 08:54:27 +0000227 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000228 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
George Rimar77b77792015-11-25 22:15:01 +0000229 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230 void finalize() override;
231 void writeTo(uint8_t *Buf) override;
232 bool hasRelocs() const { return !Relocs.empty(); }
233 bool isRela() const { return IsRela; }
234
George Rimara07ff662015-12-21 10:12:06 +0000235 bool Static = false;
236
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000237private:
George Rimar5828c232015-11-30 17:49:19 +0000238 bool applyTlsDynamicReloc(SymbolBody *Body, uint32_t Type, Elf_Rel *P,
239 Elf_Rel *N);
240
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000241 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242 const bool IsRela;
243};
244
245template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000246class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
249 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
250 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
251 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000252 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000253 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000254 void addSection(InputSectionBase<ELFT> *C) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000255 void writeTo(uint8_t *Buf) override;
256
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000257private:
Rafael Espindola71675852015-09-22 00:16:19 +0000258 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259};
260
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000261template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000262class MergeOutputSection final : public OutputSectionBase<ELFT> {
263 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
264
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000265 bool shouldTailMerge() const;
266
Rafael Espindolac159c962015-10-19 21:00:02 +0000267public:
268 MergeOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000269 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000270 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000271 unsigned getOffset(StringRef Val);
272 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000273
274private:
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000275 llvm::StringTableBuilder Builder{llvm::StringTableBuilder::RAW};
Rafael Espindolac159c962015-10-19 21:00:02 +0000276};
277
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000278// FDE or CIE
279template <class ELFT> struct EHRegion {
280 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
281 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
282 StringRef data() const;
283 EHInputSection<ELFT> *S;
284 unsigned Index;
285};
286
287template <class ELFT> struct Cie : public EHRegion<ELFT> {
288 Cie(EHInputSection<ELFT> *S, unsigned Index);
289 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000290 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000291};
292
293template <class ELFT>
294class EHOutputSection final : public OutputSectionBase<ELFT> {
295public:
296 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
297 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
298 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
299 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
300 EHOutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
301 void writeTo(uint8_t *Buf) override;
302
303 template <bool IsRela>
304 void addSectionAux(
305 EHInputSection<ELFT> *S,
306 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, IsRela> *>
307 Rels);
308
Rui Ueyama40845e62015-12-26 05:51:07 +0000309 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000310
311private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000312 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000313 uintX_t readEntryLength(ArrayRef<uint8_t> D);
314
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315 std::vector<EHInputSection<ELFT> *> Sections;
316 std::vector<Cie<ELFT>> Cies;
317
318 // Maps CIE content + personality to a index in Cies.
319 llvm::DenseMap<std::pair<StringRef, StringRef>, unsigned> CieMap;
320};
321
Rafael Espindolac159c962015-10-19 21:00:02 +0000322template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000323class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000324public:
325 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000326 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000327};
328
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000329template <class ELFT>
330class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000331public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000332 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000333 StringTableSection(StringRef Name, bool Dynamic);
Rui Ueyama76c00632016-01-07 02:35:32 +0000334 void reserve(StringRef S);
335 size_t addString(StringRef S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336 void writeTo(uint8_t *Buf) override;
Rui Ueyama76c00632016-01-07 02:35:32 +0000337 size_t getSize() const { return Used + Reserved; }
338 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000339 bool isDynamic() const { return Dynamic; }
340
341private:
342 const bool Dynamic;
Rui Ueyama76c00632016-01-07 02:35:32 +0000343 std::vector<StringRef> Strings;
344 size_t Used = 1; // ELF string tables start with a NUL byte, so 1.
345 size_t Reserved = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000346};
347
348template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000349class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
351
352public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000353 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000354 void finalize() override;
355 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356};
357
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000358// Outputs GNU Hash section. For detailed explanation see:
359// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
360template <class ELFT>
361class GnuHashTableSection final : public OutputSectionBase<ELFT> {
362 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
363 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
364 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
365
366public:
367 GnuHashTableSection();
368 void finalize() override;
369 void writeTo(uint8_t *Buf) override;
370
Igor Kudrinf1d60292015-10-28 07:05:56 +0000371 // Adds symbols to the hash table.
372 // Sorts the input to satisfy GNU hash section requirements.
373 void addSymbols(std::vector<SymbolBody *> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000374
375private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000376 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000377 static unsigned calcMaskWords(unsigned NumHashed);
378
379 void writeHeader(uint8_t *&Buf);
380 void writeBloomFilter(uint8_t *&Buf);
381 void writeHashTable(uint8_t *Buf);
382
Igor Kudrinf1d60292015-10-28 07:05:56 +0000383 struct HashedSymbolData {
384 SymbolBody *Body;
385 uint32_t Hash;
386 };
387
388 std::vector<HashedSymbolData> HashedSymbols;
389
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000390 unsigned MaskWords;
391 unsigned NBuckets;
392 unsigned Shift2;
393};
394
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000395template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000396class DynamicSection final : public OutputSectionBase<ELFT> {
397 typedef OutputSectionBase<ELFT> Base;
398 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000399 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
400 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000401 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000402 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000403
404public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000405 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000406 void finalize() override;
407 void writeTo(uint8_t *Buf) override;
408
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000409 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
410 OutputSectionBase<ELFT> *InitArraySec = nullptr;
411 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000412
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000413private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000414 SymbolTable<ELFT> &SymTab;
Rafael Espindola167e62f2015-12-21 20:59:29 +0000415 const SymbolBody *InitSym = nullptr;
416 const SymbolBody *FiniSym = nullptr;
Rui Ueyamac96d0dd2015-10-21 17:47:10 +0000417 uint32_t DtFlags = 0;
418 uint32_t DtFlags1 = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000419};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000420
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000421template <class ELFT>
422class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
423 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
424
425public:
426 MipsReginfoOutputSection();
427 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000428 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000429
430private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000431 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000432};
433
George Rimarf6bc65a2016-01-15 13:34:52 +0000434// --eh-frame-hdr option tells linker to construct a header for all the
435// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
436// and also to a PT_GNU_EH_FRAME segment.
437// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
438// calling dl_iterate_phdr.
439// This section contains a lookup table for quick binary search of FDEs.
440// Detailed info about internals can be found in Ian Lance Taylor's blog:
441// http://www.airs.com/blog/archives/460 (".eh_frame")
442// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
443template <class ELFT>
444class EhFrameHeader final : public OutputSectionBase<ELFT> {
445 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
446
447public:
448 EhFrameHeader();
449 void writeTo(uint8_t *Buf) override;
450
451 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
452 void assignEhFrame(EHOutputSection<ELFT> *Sec);
453 void reserveFde();
454
455 bool Live = false;
456
457private:
458 struct FdeData {
459 uint8_t Enc;
460 size_t Off;
461 uint8_t *PCRel;
462 };
463
464 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
465
466 EHOutputSection<ELFT> *Sec = nullptr;
467 std::vector<FdeData> FdeList;
468};
469
470inline uint64_t align(uint64_t Value, uint64_t Align) {
471 return llvm::RoundUpToAlignment(Value, Align);
472}
473
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000474// All output sections that are hadnled by the linker specially are
475// globally accessible. Writer initializes them, so don't use them
476// until Writer is initialized.
477template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000478 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000479 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000480 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000481 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000482 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000483 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000484 static GotSection<ELFT> *Got;
485 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000486 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000487 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000488 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000489 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000490 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000491 static PltSection<ELFT> *Plt;
492 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000493 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000494 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000495 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000496 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000497 static SymbolTableSection<ELFT> *DynSymTab;
498 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000499 static Elf_Phdr *TlsPhdr;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000500};
Rui Ueyamad888d102015-10-09 19:34:55 +0000501
502template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000503template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000504template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000505template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000506template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
507template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000508template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000509template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000510template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000511template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000512template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000513template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
514template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000515template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000516template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000517template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000518template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000519template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
520template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000521template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000522
523} // namespace elf2
524} // namespace lld
525
526#endif // LLD_ELF_OUTPUT_SECTIONS_H