blob: 2cc339c718fe8f94b395a39c5debccf1f71c73dd [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 {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024
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 Espindola67d72c02016-03-11 12:06:30 +000050bool canBePreempted(const SymbolBody &Body);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000051
George Rimar12737b72016-02-25 08:40:26 +000052bool isValidCIdentifier(StringRef S);
53
Rafael Espindola71675852015-09-22 00:16:19 +000054// This represents a section in an output file.
55// Different sub classes represent different types of sections. Some contain
56// input sections, others are created by the linker.
57// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000058// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000059template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000060public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000061 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
62 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063
George Rimar9bec24a2016-02-18 14:20:08 +000064 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065 void setVA(uintX_t VA) { Header.sh_addr = VA; }
66 uintX_t getVA() const { return Header.sh_addr; }
67 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000068 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000069 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071
Rui Ueyama40845e62015-12-26 05:51:07 +000072 virtual void addSection(InputSectionBase<ELFT> *C) {}
73
Rui Ueyama2317d0d2015-10-15 20:55:22 +000074 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075
76 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000077 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078 void setSize(uintX_t Val) { Header.sh_size = Val; }
79 uintX_t getFlags() { return Header.sh_flags; }
80 uintX_t getFileOff() { return Header.sh_offset; }
81 uintX_t getAlign() {
82 // The ELF spec states that a value of 0 means the section has no alignment
83 // constraits.
84 return std::max<uintX_t>(Header.sh_addralign, 1);
85 }
86 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000087 void updateAlign(uintX_t Align) {
88 if (Align > Header.sh_addralign)
89 Header.sh_addralign = Align;
90 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092 virtual void finalize() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000093 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000094 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000095
96protected:
97 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000098 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099};
100
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000101template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
102 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103 typedef typename Base::uintX_t uintX_t;
104
105public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000106 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000107 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000108 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000109 void addEntry(SymbolBody &Sym);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000110 void addMipsLocalEntry();
Rafael Espindola67d72c02016-03-11 12:06:30 +0000111 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000112 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000113 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
115 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000116 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000117 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118
Igor Kudrin304860a2015-11-12 04:39:49 +0000119 // Returns the symbol which corresponds to the first entry of the global part
120 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
121 // table properties.
122 // Returns nullptr if the global part is empty.
123 const SymbolBody *getMipsFirstGlobalEntry() const;
124
125 // Returns the number of entries in the local part of GOT including
126 // the number of reserved entries. This method is MIPS-specific.
127 unsigned getMipsLocalEntriesNum() const;
128
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000129 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000130
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000131private:
132 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000133 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000134 uint32_t MipsLocalEntries = 0;
135 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
136
137 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000138};
139
George Rimar648a2c32015-10-20 08:54:27 +0000140template <class ELFT>
141class GotPltSection final : public OutputSectionBase<ELFT> {
142 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
143
144public:
145 GotPltSection();
146 void finalize() override;
147 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000148 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000149 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000150
151private:
152 std::vector<const SymbolBody *> Entries;
153};
154
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000155template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
156 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000157 typedef typename Base::uintX_t uintX_t;
158
159public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000160 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000161 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000163 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000164 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165
166private:
George Rimar77b77792015-11-25 22:15:01 +0000167 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168};
169
170template <class ELFT> struct DynamicReloc {
Rafael Espindolade9857e2016-02-04 21:33:05 +0000171 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
172 uint32_t Type;
173
174 // Where the relocation is.
175 enum OffsetKind {
176 Off_Got, // The got entry of Sym.
177 Off_GotPlt, // The got.plt entry of Sym.
178 Off_Bss, // The bss entry of Sym (copy reloc).
179 Off_Sec, // The final position of the given input section and offset.
180 Off_LTlsIndex, // The local tls index.
181 Off_GTlsIndex, // The global tls index of Sym.
182 Off_GTlsOffset // The global tls offset of Sym.
183 } OKind;
184
185 SymbolBody *Sym = nullptr;
186 InputSectionBase<ELFT> *OffsetSec = nullptr;
187 uintX_t OffsetInSec = 0;
188 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000189 uintX_t Addend = 0;
190
191 DynamicReloc(uint32_t Type, OffsetKind OKind, SymbolBody *Sym)
192 : Type(Type), OKind(OKind), Sym(Sym) {}
193
194 DynamicReloc(uint32_t Type, OffsetKind OKind, bool UseSymVA, SymbolBody *Sym)
195 : Type(Type), OKind(OKind), Sym(Sym), UseSymVA(UseSymVA) {}
196
197 DynamicReloc(uint32_t Type, InputSectionBase<ELFT> *OffsetSec,
198 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
199 uintX_t Addend)
200 : Type(Type), OKind(Off_Sec), Sym(Sym), OffsetSec(OffsetSec),
201 OffsetInSec(OffsetInSec), UseSymVA(UseSymVA), Addend(Addend) {}
202
Rui Ueyamaa2b1f452016-02-17 06:08:42 +0000203 uintX_t getOffset() const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204};
205
206template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000207class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000208public:
209 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
210 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
211 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000212 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000213 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000214 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000215
Rui Ueyama0db335f2015-10-07 16:58:54 +0000216 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000217 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000218 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000219 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000220 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000221
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000222 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000223 return Symbols;
224 }
225
226 unsigned NumLocals = 0;
227 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000228
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000229private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000230 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000231 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000232
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000233 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000234 static uint8_t getSymbolBinding(SymbolBody *Body);
235
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000236 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000237
238 // A vector of symbols and their string table offsets.
239 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000240};
241
242template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000243class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
245 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000246 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000247
248public:
George Rimar648a2c32015-10-20 08:54:27 +0000249 RelocationSection(StringRef Name, bool IsRela);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000250 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000251 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000252 void finalize() override;
253 void writeTo(uint8_t *Buf) override;
254 bool hasRelocs() const { return !Relocs.empty(); }
255 bool isRela() const { return IsRela; }
256
George Rimara07ff662015-12-21 10:12:06 +0000257 bool Static = false;
258
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000259private:
260 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000261 const bool IsRela;
262};
263
264template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000265class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000266public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000267 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
268 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
269 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
270 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000271 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000272 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000273 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000274 void sortInitFini();
275 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000277 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000278
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000279private:
Rui Ueyama5af83682016-02-11 23:41:38 +0000280 void reassignOffsets();
Rafael Espindola71675852015-09-22 00:16:19 +0000281 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000282};
283
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000284template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000285class MergeOutputSection final : public OutputSectionBase<ELFT> {
286 typedef typename OutputSectionBase<ELFT>::uintX_t uintX_t;
287
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000288 bool shouldTailMerge() const;
289
Rafael Espindolac159c962015-10-19 21:00:02 +0000290public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000291 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
292 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000293 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000294 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000295 unsigned getOffset(StringRef Val);
296 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000297
298private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000299 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000300};
301
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000302// FDE or CIE
303template <class ELFT> struct EHRegion {
304 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
305 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
306 StringRef data() const;
307 EHInputSection<ELFT> *S;
308 unsigned Index;
309};
310
311template <class ELFT> struct Cie : public EHRegion<ELFT> {
312 Cie(EHInputSection<ELFT> *S, unsigned Index);
313 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000314 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315};
316
317template <class ELFT>
318class EHOutputSection final : public OutputSectionBase<ELFT> {
319public:
320 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
321 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
322 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
323 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000324 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000325 void writeTo(uint8_t *Buf) override;
326
Rui Ueyamafc467e72016-03-13 05:06:50 +0000327 template <class RelTy>
328 void addSectionAux(EHInputSection<ELFT> *S,
329 llvm::iterator_range<const RelTy *> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000330
Rui Ueyama40845e62015-12-26 05:51:07 +0000331 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000332
333private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000334 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000335
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000336 std::vector<EHInputSection<ELFT> *> Sections;
337 std::vector<Cie<ELFT>> Cies;
338
339 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000340 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000341};
342
Rafael Espindolac159c962015-10-19 21:00:02 +0000343template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000344class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000345public:
346 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000347 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000348};
349
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000350template <class ELFT>
351class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000352public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000353 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000354 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000355 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000356 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000357 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000358 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000359 bool isDynamic() const { return Dynamic; }
360
361private:
362 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000363 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000364 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000365 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000366};
367
368template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000369class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000370 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
371
372public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000373 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000374 void finalize() override;
375 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000376};
377
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000378// Outputs GNU Hash section. For detailed explanation see:
379// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
380template <class ELFT>
381class GnuHashTableSection final : public OutputSectionBase<ELFT> {
382 typedef typename llvm::object::ELFFile<ELFT>::Elf_Off Elf_Off;
383 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
384 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
385
386public:
387 GnuHashTableSection();
388 void finalize() override;
389 void writeTo(uint8_t *Buf) override;
390
Igor Kudrinf1d60292015-10-28 07:05:56 +0000391 // Adds symbols to the hash table.
392 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000393 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000394
395private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000396 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000397 static unsigned calcMaskWords(unsigned NumHashed);
398
399 void writeHeader(uint8_t *&Buf);
400 void writeBloomFilter(uint8_t *&Buf);
401 void writeHashTable(uint8_t *Buf);
402
Rui Ueyama861c7312016-02-17 05:40:03 +0000403 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000404 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000405 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000406 uint32_t Hash;
407 };
408
Rui Ueyama861c7312016-02-17 05:40:03 +0000409 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000410
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000411 unsigned MaskWords;
412 unsigned NBuckets;
413 unsigned Shift2;
414};
415
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000416template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000417class DynamicSection final : public OutputSectionBase<ELFT> {
418 typedef OutputSectionBase<ELFT> Base;
419 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000420 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
421 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000422 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000423 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolade069362016-01-25 21:32:04 +0000424 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
425
Rui Ueyama909cc682016-02-02 03:11:27 +0000426 // The .dynamic section contains information for the dynamic linker.
427 // The section consists of fixed size entries, which consist of
428 // type and value fields. Value are one of plain integers, symbol
429 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000430 struct Entry {
431 int32_t Tag;
432 union {
433 OutputSectionBase<ELFT> *OutSec;
434 uint64_t Val;
435 const SymbolBody *Sym;
436 };
437 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
438 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
439 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
440 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
441 Entry(int32_t Tag, const SymbolBody *Sym)
442 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
443 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000444
445 // finalize() fills this vector with the section contents. finalize()
446 // cannot directly create final section contents because when the
447 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000448 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000449
450public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000451 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000452 void finalize() override;
453 void writeTo(uint8_t *Buf) override;
454
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000455 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
456 OutputSectionBase<ELFT> *InitArraySec = nullptr;
457 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000458
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000459private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000460 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000461};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000462
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000463template <class ELFT>
464class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
465 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
466
467public:
468 MipsReginfoOutputSection();
469 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000470 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000471
472private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000473 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000474};
475
George Rimarf6bc65a2016-01-15 13:34:52 +0000476// --eh-frame-hdr option tells linker to construct a header for all the
477// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
478// and also to a PT_GNU_EH_FRAME segment.
479// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
480// calling dl_iterate_phdr.
481// This section contains a lookup table for quick binary search of FDEs.
482// Detailed info about internals can be found in Ian Lance Taylor's blog:
483// http://www.airs.com/blog/archives/460 (".eh_frame")
484// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
485template <class ELFT>
486class EhFrameHeader final : public OutputSectionBase<ELFT> {
487 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
488
489public:
490 EhFrameHeader();
491 void writeTo(uint8_t *Buf) override;
492
493 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
494 void assignEhFrame(EHOutputSection<ELFT> *Sec);
495 void reserveFde();
496
497 bool Live = false;
498
499private:
500 struct FdeData {
501 uint8_t Enc;
502 size_t Off;
503 uint8_t *PCRel;
504 };
505
506 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
507
508 EHOutputSection<ELFT> *Sec = nullptr;
509 std::vector<FdeData> FdeList;
510};
511
Rui Ueyama634ddf02016-03-11 20:51:53 +0000512template <class ELFT>
513class BuildIdSection final : public OutputSectionBase<ELFT> {
514public:
515 BuildIdSection();
516 void writeTo(uint8_t *Buf) override;
517 void update(ArrayRef<uint8_t> Buf);
518 void writeBuildId();
519
520private:
521 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
522 uint8_t *HashBuf;
523};
524
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000525// All output sections that are hadnled by the linker specially are
526// globally accessible. Writer initializes them, so don't use them
527// until Writer is initialized.
528template <class ELFT> struct Out {
Michael J. Spencerd77f0d22015-11-03 22:39:09 +0000529 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000530 typedef typename llvm::object::ELFFile<ELFT>::Elf_Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000531 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000532 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000533 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000534 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000535 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000536 static GotSection<ELFT> *Got;
537 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000538 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000539 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000540 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000541 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000542 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000543 static PltSection<ELFT> *Plt;
544 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000545 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000546 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000547 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000548 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000549 static SymbolTableSection<ELFT> *DynSymTab;
550 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000551 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000552 static OutputSectionBase<ELFT> *ElfHeader;
553 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000554};
Rui Ueyamad888d102015-10-09 19:34:55 +0000555
Rui Ueyama634ddf02016-03-11 20:51:53 +0000556template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000557template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000558template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000559template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000560template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000561template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
562template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000563template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000564template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000565template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000566template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000567template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000568template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
569template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000570template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000571template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000572template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000573template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000574template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
575template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000576template <class ELFT> typename Out<ELFT>::Elf_Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000577template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
578template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000579
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000580} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000581} // namespace lld
582
583#endif // LLD_ELF_OUTPUT_SECTIONS_H