blob: aa552dcdcea4b27b2e5ff160b1a7f9d92b7309d0 [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
Davide Italiano85121bb2015-09-25 03:56:11 +000013#include "Config.h"
14
Rui Ueyamaa0752a52016-03-13 20:28:29 +000015#include "lld/Core/LLVM.h"
Simon Atanasyand2980d32016-03-29 14:07:22 +000016#include "llvm/ADT/SmallPtrSet.h"
Rui Ueyamaa0752a52016-03-13 20:28:29 +000017#include "llvm/MC/StringTableBuilder.h"
18#include "llvm/Object/ELF.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000019
20namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000021namespace elf {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000022
23class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000024template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000025template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000026template <class ELFT> class StringTableSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000027template <class ELFT> class EHInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000028template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000029template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000030template <class ELFT> class MergeInputSection;
Simon Atanasyan1d7df402015-12-20 10:57:34 +000031template <class ELFT> class MipsReginfoInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000032template <class ELFT> class OutputSection;
33template <class ELFT> class ObjectFile;
34template <class ELFT> class DefinedRegular;
35
Rafael Espindola5805c4f2015-09-21 21:38:08 +000036template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000037static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000038 return 0;
39}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040
41template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000042static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000043 return Rel.r_addend;
44}
45
George Rimar12737b72016-02-25 08:40:26 +000046bool isValidCIdentifier(StringRef S);
47
Rafael Espindola71675852015-09-22 00:16:19 +000048// This represents a section in an output file.
49// Different sub classes represent different types of sections. Some contain
50// input sections, others are created by the linker.
51// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000052// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000053template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000055 typedef typename ELFT::uint uintX_t;
56 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057
George Rimar9bec24a2016-02-18 14:20:08 +000058 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059 void setVA(uintX_t VA) { Header.sh_addr = VA; }
60 uintX_t getVA() const { return Header.sh_addr; }
61 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000062 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000063 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000064 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000065
Rui Ueyama40845e62015-12-26 05:51:07 +000066 virtual void addSection(InputSectionBase<ELFT> *C) {}
67
Rui Ueyama2317d0d2015-10-15 20:55:22 +000068 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
70 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000071 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072 void setSize(uintX_t Val) { Header.sh_size = Val; }
73 uintX_t getFlags() { return Header.sh_flags; }
74 uintX_t getFileOff() { return Header.sh_offset; }
75 uintX_t getAlign() {
76 // The ELF spec states that a value of 0 means the section has no alignment
77 // constraits.
78 return std::max<uintX_t>(Header.sh_addralign, 1);
79 }
80 uint32_t getType() { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000081 void updateAlign(uintX_t Align) {
82 if (Align > Header.sh_addralign)
83 Header.sh_addralign = Align;
84 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085
Rui Ueyama47091902016-03-30 19:41:51 +000086 // If true, this section will be page aligned on disk.
87 // Typically the first section of each PT_LOAD segment has this flag.
88 bool PageAlign = false;
89
Rafael Espindola5805c4f2015-09-21 21:38:08 +000090 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000091 virtual void
92 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
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;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000103 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104
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);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000110 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000111 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000112 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000113 uintX_t getMipsLocalFullAddr(const SymbolBody &B);
114 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000115 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000116 uintX_t getGlobalDynOffset(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; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000130 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000131
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000132private:
133 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000134 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000135 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000136 // Output sections referenced by MIPS GOT relocations.
137 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000138 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
139
140 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000141};
142
George Rimar648a2c32015-10-20 08:54:27 +0000143template <class ELFT>
144class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000145 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000146
147public:
148 GotPltSection();
149 void finalize() override;
150 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000151 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000152 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000153
154private:
155 std::vector<const SymbolBody *> Entries;
156};
157
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000158template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
159 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000160 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000161
162public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000163 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000164 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000166 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000167 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168
169private:
George Rimar77b77792015-11-25 22:15:01 +0000170 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000171};
172
173template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000174 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000175 uint32_t Type;
176
Rafael Espindolade9857e2016-02-04 21:33:05 +0000177 SymbolBody *Sym = nullptr;
Rafael Espindola56004c52016-04-07 14:22:09 +0000178 OutputSectionBase<ELFT> *OffsetSec = nullptr;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000179 uintX_t OffsetInSec = 0;
180 bool UseSymVA = false;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000181 uintX_t Addend = 0;
182
Rafael Espindola56004c52016-04-07 14:22:09 +0000183 DynamicReloc(uint32_t Type, OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000184 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
185 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000186 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
187 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000188};
189
190template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000191class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000192public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000193 typedef typename ELFT::Shdr Elf_Shdr;
194 typedef typename ELFT::Sym Elf_Sym;
195 typedef typename ELFT::SymRange Elf_Sym_Range;
196 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000197 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000198 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199
Rui Ueyama0db335f2015-10-07 16:58:54 +0000200 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000201 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000202 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000203 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000204 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000205
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000206 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000207 return Symbols;
208 }
209
210 unsigned NumLocals = 0;
211 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000212
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000213private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000214 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000215 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000216
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000217 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000218 static uint8_t getSymbolBinding(SymbolBody *Body);
219
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000220 SymbolTable<ELFT> &Table;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000221
222 // A vector of symbols and their string table offsets.
223 std::vector<std::pair<SymbolBody *, size_t>> Symbols;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000224};
225
226template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000227class RelocationSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000228 typedef typename ELFT::Rel Elf_Rel;
229 typedef typename ELFT::Rela Elf_Rela;
230 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000231
232public:
Rui Ueyama6c5638b2016-03-13 20:10:20 +0000233 RelocationSection(StringRef Name);
Rafael Espindolad30eb7d2016-02-05 15:03:10 +0000234 void addReloc(const DynamicReloc<ELFT> &Reloc);
George Rimar77b77792015-11-25 22:15:01 +0000235 unsigned getRelocOffset();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000236 void finalize() override;
237 void writeTo(uint8_t *Buf) override;
238 bool hasRelocs() const { return !Relocs.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000239
George Rimara07ff662015-12-21 10:12:06 +0000240 bool Static = false;
241
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000242private:
243 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000244};
245
246template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000247class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000248public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000249 typedef typename ELFT::Shdr Elf_Shdr;
250 typedef typename ELFT::Sym Elf_Sym;
251 typedef typename ELFT::Rel Elf_Rel;
252 typedef typename ELFT::Rela Elf_Rela;
253 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000254 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rui Ueyama40845e62015-12-26 05:51:07 +0000255 void addSection(InputSectionBase<ELFT> *C) override;
Rui Ueyama5af83682016-02-11 23:41:38 +0000256 void sortInitFini();
257 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000258 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000259 void finalize() override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000260 void
261 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola71675852015-09-22 00:16:19 +0000262 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000263};
264
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000265template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000266class MergeOutputSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000267 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000268
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000269 bool shouldTailMerge() const;
270
Rafael Espindolac159c962015-10-19 21:00:02 +0000271public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000272 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
273 uintX_t Alignment);
Rui Ueyama40845e62015-12-26 05:51:07 +0000274 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000275 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000276 unsigned getOffset(StringRef Val);
277 void finalize() override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000278
279private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000280 llvm::StringTableBuilder Builder;
Rafael Espindolac159c962015-10-19 21:00:02 +0000281};
282
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000283// FDE or CIE
284template <class ELFT> struct EHRegion {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000285 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000286 EHRegion(EHInputSection<ELFT> *S, unsigned Index);
287 StringRef data() const;
288 EHInputSection<ELFT> *S;
289 unsigned Index;
290};
291
292template <class ELFT> struct Cie : public EHRegion<ELFT> {
293 Cie(EHInputSection<ELFT> *S, unsigned Index);
294 std::vector<EHRegion<ELFT>> Fdes;
George Rimarf6bc65a2016-01-15 13:34:52 +0000295 uint8_t FdeEncoding;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000296};
297
298template <class ELFT>
299class EHOutputSection final : public OutputSectionBase<ELFT> {
300public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000301 typedef typename ELFT::uint uintX_t;
302 typedef typename ELFT::Shdr Elf_Shdr;
303 typedef typename ELFT::Rel Elf_Rel;
304 typedef typename ELFT::Rela Elf_Rela;
George Rimar9bec24a2016-02-18 14:20:08 +0000305 EHOutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000306 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000307 void finalize() override;
308 void
309 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000310
Rui Ueyamafc467e72016-03-13 05:06:50 +0000311 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000312 void addSectionAux(EHInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000313
Rui Ueyama40845e62015-12-26 05:51:07 +0000314 void addSection(InputSectionBase<ELFT> *S) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000315
316private:
George Rimarf6bc65a2016-01-15 13:34:52 +0000317 uint8_t getFdeEncoding(ArrayRef<uint8_t> D);
George Rimar003be4f2015-12-17 09:23:40 +0000318
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000319 std::vector<EHInputSection<ELFT> *> Sections;
320 std::vector<Cie<ELFT>> Cies;
321
322 // Maps CIE content + personality to a index in Cies.
Rafael Espindola156ed8d2016-02-10 13:19:32 +0000323 llvm::DenseMap<std::pair<StringRef, SymbolBody *>, unsigned> CieMap;
Rafael Espindola56004c52016-04-07 14:22:09 +0000324 bool Finalized = false;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000325};
326
Rafael Espindolac159c962015-10-19 21:00:02 +0000327template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000328class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000329public:
330 InterpSection();
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000331 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000332};
333
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000334template <class ELFT>
335class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000336public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000337 typedef typename ELFT::uint uintX_t;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000338 StringTableSection(StringRef Name, bool Dynamic);
Rafael Espindolae2c24612016-01-29 01:24:25 +0000339 unsigned addString(StringRef S, bool HashIt = true);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000340 void writeTo(uint8_t *Buf) override;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000341 unsigned getSize() const { return Size; }
Rui Ueyama76c00632016-01-07 02:35:32 +0000342 void finalize() override { this->Header.sh_size = getSize(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000343 bool isDynamic() const { return Dynamic; }
344
345private:
346 const bool Dynamic;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000347 llvm::DenseMap<StringRef, unsigned> StringMap;
Rui Ueyama76c00632016-01-07 02:35:32 +0000348 std::vector<StringRef> Strings;
Rafael Espindolae2c24612016-01-29 01:24:25 +0000349 unsigned Size = 1; // ELF string tables start with a NUL byte, so 1.
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000350};
351
352template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000353class HashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000354 typedef typename ELFT::Word Elf_Word;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000355
356public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000357 HashTableSection();
Rui Ueyama0db335f2015-10-07 16:58:54 +0000358 void finalize() override;
359 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000360};
361
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000362// Outputs GNU Hash section. For detailed explanation see:
363// https://blogs.oracle.com/ali/entry/gnu_hash_elf_sections
364template <class ELFT>
365class GnuHashTableSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000366 typedef typename ELFT::Off Elf_Off;
367 typedef typename ELFT::Word Elf_Word;
368 typedef typename ELFT::uint uintX_t;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000369
370public:
371 GnuHashTableSection();
372 void finalize() override;
373 void writeTo(uint8_t *Buf) override;
374
Igor Kudrinf1d60292015-10-28 07:05:56 +0000375 // Adds symbols to the hash table.
376 // Sorts the input to satisfy GNU hash section requirements.
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000377 void addSymbols(std::vector<std::pair<SymbolBody *, size_t>> &Symbols);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000378
379private:
Igor Kudrinf1d60292015-10-28 07:05:56 +0000380 static unsigned calcNBuckets(unsigned NumHashed);
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000381 static unsigned calcMaskWords(unsigned NumHashed);
382
383 void writeHeader(uint8_t *&Buf);
384 void writeBloomFilter(uint8_t *&Buf);
385 void writeHashTable(uint8_t *Buf);
386
Rui Ueyama861c7312016-02-17 05:40:03 +0000387 struct SymbolData {
Igor Kudrinf1d60292015-10-28 07:05:56 +0000388 SymbolBody *Body;
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000389 size_t STName;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000390 uint32_t Hash;
391 };
392
Rui Ueyama861c7312016-02-17 05:40:03 +0000393 std::vector<SymbolData> Symbols;
Igor Kudrinf1d60292015-10-28 07:05:56 +0000394
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000395 unsigned MaskWords;
396 unsigned NBuckets;
397 unsigned Shift2;
398};
399
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000400template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000401class DynamicSection final : public OutputSectionBase<ELFT> {
402 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000403 typedef typename ELFT::Dyn Elf_Dyn;
404 typedef typename ELFT::Rel Elf_Rel;
405 typedef typename ELFT::Rela Elf_Rela;
406 typedef typename ELFT::Shdr Elf_Shdr;
407 typedef typename ELFT::Sym Elf_Sym;
408 typedef typename ELFT::uint uintX_t;
Rafael Espindolade069362016-01-25 21:32:04 +0000409
Rui Ueyama909cc682016-02-02 03:11:27 +0000410 // The .dynamic section contains information for the dynamic linker.
411 // The section consists of fixed size entries, which consist of
412 // type and value fields. Value are one of plain integers, symbol
413 // addresses, or section addresses. This struct represents the entry.
Rafael Espindolade069362016-01-25 21:32:04 +0000414 struct Entry {
415 int32_t Tag;
416 union {
417 OutputSectionBase<ELFT> *OutSec;
418 uint64_t Val;
419 const SymbolBody *Sym;
420 };
421 enum KindT { SecAddr, SymAddr, PlainInt } Kind;
422 Entry(int32_t Tag, OutputSectionBase<ELFT> *OutSec)
423 : Tag(Tag), OutSec(OutSec), Kind(SecAddr) {}
424 Entry(int32_t Tag, uint64_t Val) : Tag(Tag), Val(Val), Kind(PlainInt) {}
425 Entry(int32_t Tag, const SymbolBody *Sym)
426 : Tag(Tag), Sym(Sym), Kind(SymAddr) {}
427 };
Rui Ueyama909cc682016-02-02 03:11:27 +0000428
429 // finalize() fills this vector with the section contents. finalize()
430 // cannot directly create final section contents because when the
431 // function is called, symbol or section addresses are not fixed yet.
Rafael Espindolade069362016-01-25 21:32:04 +0000432 std::vector<Entry> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000433
434public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000435 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000436 void finalize() override;
437 void writeTo(uint8_t *Buf) override;
438
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000439 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
440 OutputSectionBase<ELFT> *InitArraySec = nullptr;
441 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000442
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000443private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000444 SymbolTable<ELFT> &SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000445};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000446
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000447template <class ELFT>
448class MipsReginfoOutputSection final : public OutputSectionBase<ELFT> {
449 typedef llvm::object::Elf_Mips_RegInfo<ELFT> Elf_Mips_RegInfo;
450
451public:
452 MipsReginfoOutputSection();
453 void writeTo(uint8_t *Buf) override;
Rui Ueyama40845e62015-12-26 05:51:07 +0000454 void addSection(InputSectionBase<ELFT> *S) override;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000455
456private:
Rui Ueyama70eed362016-01-06 22:42:43 +0000457 uint32_t GprMask = 0;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000458};
459
George Rimarf6bc65a2016-01-15 13:34:52 +0000460// --eh-frame-hdr option tells linker to construct a header for all the
461// .eh_frame sections. This header is placed to a section named .eh_frame_hdr
462// and also to a PT_GNU_EH_FRAME segment.
463// At runtime the unwinder then can find all the PT_GNU_EH_FRAME segments by
464// calling dl_iterate_phdr.
465// This section contains a lookup table for quick binary search of FDEs.
466// Detailed info about internals can be found in Ian Lance Taylor's blog:
467// http://www.airs.com/blog/archives/460 (".eh_frame")
468// http://www.airs.com/blog/archives/462 (".eh_frame_hdr")
469template <class ELFT>
470class EhFrameHeader final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000471 typedef typename ELFT::uint uintX_t;
George Rimarf6bc65a2016-01-15 13:34:52 +0000472
473public:
474 EhFrameHeader();
475 void writeTo(uint8_t *Buf) override;
476
477 void addFde(uint8_t Enc, size_t Off, uint8_t *PCRel);
478 void assignEhFrame(EHOutputSection<ELFT> *Sec);
479 void reserveFde();
480
481 bool Live = false;
482
Rafael Espindola56004c52016-04-07 14:22:09 +0000483 EHOutputSection<ELFT> *Sec = nullptr;
484
George Rimarf6bc65a2016-01-15 13:34:52 +0000485private:
486 struct FdeData {
487 uint8_t Enc;
488 size_t Off;
489 uint8_t *PCRel;
490 };
491
492 uintX_t getFdePc(uintX_t EhVA, const FdeData &F);
493
George Rimarf6bc65a2016-01-15 13:34:52 +0000494 std::vector<FdeData> FdeList;
495};
496
Rui Ueyama634ddf02016-03-11 20:51:53 +0000497template <class ELFT>
498class BuildIdSection final : public OutputSectionBase<ELFT> {
499public:
500 BuildIdSection();
501 void writeTo(uint8_t *Buf) override;
502 void update(ArrayRef<uint8_t> Buf);
503 void writeBuildId();
504
505private:
506 uint64_t Hash = 0xcbf29ce484222325; // FNV1 hash basis
507 uint8_t *HashBuf;
508};
509
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000510// All output sections that are hadnled by the linker specially are
511// globally accessible. Writer initializes them, so don't use them
512// until Writer is initialized.
513template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000514 typedef typename ELFT::uint uintX_t;
515 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000516 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000517 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000518 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000519 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000520 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000521 static GotSection<ELFT> *Got;
522 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000523 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000524 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000525 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000526 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000527 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000528 static PltSection<ELFT> *Plt;
529 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000530 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000531 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000532 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000533 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000534 static SymbolTableSection<ELFT> *DynSymTab;
535 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000536 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000537 static OutputSectionBase<ELFT> *ElfHeader;
538 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000539};
Rui Ueyamad888d102015-10-09 19:34:55 +0000540
Rui Ueyama634ddf02016-03-11 20:51:53 +0000541template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000542template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000543template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000544template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000545template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000546template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
547template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000548template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000549template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000550template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000551template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000552template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000553template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
554template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000555template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000556template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000557template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000558template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000559template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
560template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000561template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000562template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
563template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000564
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000565} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000566} // namespace lld
567
568#endif // LLD_ELF_OUTPUT_SECTIONS_H