blob: 5f98daa31b8b9b25d1d418feb8661c99e6817206 [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"
Rui Ueyama3a41be22016-04-07 22:49:21 +000019#include "llvm/Support/MD5.h"
Rui Ueyamad86ec302016-04-07 23:51:56 +000020#include "llvm/Support/SHA1.h"
Rafael Espindola5805c4f2015-09-21 21:38:08 +000021
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>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000039static inline typename ELFT::uint getAddend(const typename ELFT::Rel &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000040 return 0;
41}
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042
43template <class ELFT>
Rui Ueyama9328b2c2016-03-14 23:16:09 +000044static inline typename ELFT::uint getAddend(const typename ELFT::Rela &Rel) {
Rafael Espindola932efcf2015-10-19 20:24:44 +000045 return Rel.r_addend;
46}
47
George Rimar12737b72016-02-25 08:40:26 +000048bool isValidCIdentifier(StringRef S);
49
Rafael Espindola71675852015-09-22 00:16:19 +000050// This represents a section in an output file.
51// Different sub classes represent different types of sections. Some contain
52// input sections, others are created by the linker.
53// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000055template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000057 typedef typename ELFT::uint uintX_t;
58 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059
George Rimar9bec24a2016-02-18 14:20:08 +000060 OutputSectionBase(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000061 void setVA(uintX_t VA) { Header.sh_addr = VA; }
62 uintX_t getVA() const { return Header.sh_addr; }
63 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rafael Espindolae2c24612016-01-29 01:24:25 +000064 void setSHName(unsigned Val) { Header.sh_name = Val; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000065 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066 StringRef getName() { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000067
Rui Ueyama40845e62015-12-26 05:51:07 +000068 virtual void addSection(InputSectionBase<ELFT> *C) {}
69
Rui Ueyama2317d0d2015-10-15 20:55:22 +000070 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000071
72 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000073 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074 void setSize(uintX_t Val) { Header.sh_size = Val; }
Rafael Espindola571452c2016-04-11 13:44:05 +000075 uintX_t getFlags() const { return Header.sh_flags; }
76 uintX_t getFileOff() const { return Header.sh_offset; }
77 uintX_t getAlign() const {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000078 // The ELF spec states that a value of 0 means the section has no alignment
79 // constraits.
80 return std::max<uintX_t>(Header.sh_addralign, 1);
81 }
Rafael Espindola571452c2016-04-11 13:44:05 +000082 uint32_t getType() const { return Header.sh_type; }
Rafael Espindola115f0f32015-11-03 14:13:40 +000083 void updateAlign(uintX_t Align) {
84 if (Align > Header.sh_addralign)
85 Header.sh_addralign = Align;
86 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087
Rui Ueyama47091902016-03-30 19:41:51 +000088 // If true, this section will be page aligned on disk.
89 // Typically the first section of each PT_LOAD segment has this flag.
90 bool PageAlign = false;
91
Rafael Espindola5805c4f2015-09-21 21:38:08 +000092 virtual void finalize() {}
Rafael Espindola56004c52016-04-07 14:22:09 +000093 virtual void
94 forEachInputSection(std::function<void(InputSectionBase<ELFT> *)> F) {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000095 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000096 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000097
98protected:
99 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000100 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101};
102
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000103template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
104 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000105 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000106
107public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000108 GotSection();
Igor Kudrin15cd9ff2015-11-06 07:43:03 +0000109 void finalize() override;
Rafael Espindolaa6627382015-10-06 23:56:53 +0000110 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000111 void addEntry(SymbolBody &Sym);
Rafael Espindola67d72c02016-03-11 12:06:30 +0000112 bool addDynTlsEntry(SymbolBody &Sym);
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000113 bool addTlsIndex();
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000114 bool empty() const { return MipsLocalEntries == 0 && Entries.empty(); }
Rafael Espindola22ef9562016-04-13 01:40:19 +0000115 uintX_t getMipsLocalEntryAddr(uintX_t EntryValue);
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000116 uintX_t getMipsLocalPageAddr(uintX_t Addr);
George Rimar90cd0a82015-12-01 19:20:26 +0000117 uintX_t getGlobalDynAddr(const SymbolBody &B) const;
Rafael Espindola74031ba2016-04-07 15:20:56 +0000118 uintX_t getGlobalDynOffset(const SymbolBody &B) const;
George Rimar9db204a2015-12-02 09:58:20 +0000119 uintX_t getNumEntries() const { return Entries.size(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000120
Igor Kudrin304860a2015-11-12 04:39:49 +0000121 // Returns the symbol which corresponds to the first entry of the global part
122 // of GOT on MIPS platform. It is required to fill up MIPS-specific dynamic
123 // table properties.
124 // Returns nullptr if the global part is empty.
125 const SymbolBody *getMipsFirstGlobalEntry() const;
126
127 // Returns the number of entries in the local part of GOT including
128 // the number of reserved entries. This method is MIPS-specific.
129 unsigned getMipsLocalEntriesNum() const;
130
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000131 uintX_t getTlsIndexVA() { return Base::getVA() + TlsIndexOff; }
Rafael Espindola74031ba2016-04-07 15:20:56 +0000132 uint32_t getTlsIndexOff() { return TlsIndexOff; }
George Rimarb17f7392015-12-01 18:24:07 +0000133
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000134private:
135 std::vector<const SymbolBody *> Entries;
Rui Ueyama0e53c7d2016-02-05 00:10:02 +0000136 uint32_t TlsIndexOff = -1;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000137 uint32_t MipsLocalEntries = 0;
Simon Atanasyand2980d32016-03-29 14:07:22 +0000138 // Output sections referenced by MIPS GOT relocations.
139 llvm::SmallPtrSet<const OutputSectionBase<ELFT> *, 10> MipsOutSections;
Simon Atanasyan56ab5f02016-01-21 05:33:23 +0000140 llvm::DenseMap<uintX_t, size_t> MipsLocalGotPos;
141
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000142};
143
George Rimar648a2c32015-10-20 08:54:27 +0000144template <class ELFT>
145class GotPltSection final : public OutputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000146 typedef typename ELFT::uint uintX_t;
George Rimar648a2c32015-10-20 08:54:27 +0000147
148public:
149 GotPltSection();
150 void finalize() override;
151 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000152 void addEntry(SymbolBody &Sym);
George Rimar648a2c32015-10-20 08:54:27 +0000153 bool empty() const;
George Rimar648a2c32015-10-20 08:54:27 +0000154
155private:
156 std::vector<const SymbolBody *> Entries;
157};
158
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000159template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
160 typedef OutputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000161 typedef typename ELFT::uint uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162
163public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000164 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000165 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000166 void writeTo(uint8_t *Buf) override;
Rafael Espindola67d72c02016-03-11 12:06:30 +0000167 void addEntry(SymbolBody &Sym);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 bool empty() const { return Entries.empty(); }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169
170private:
George Rimar77b77792015-11-25 22:15:01 +0000171 std::vector<std::pair<const SymbolBody *, unsigned>> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172};
173
174template <class ELFT> struct DynamicReloc {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000175 typedef typename ELFT::uint uintX_t;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000176 uint32_t Type;
177
Rafael Espindolaac568f92016-04-11 13:47:35 +0000178 SymbolBody *Sym;
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000179 const OutputSectionBase<ELFT> *OffsetSec;
Rafael Espindolaac568f92016-04-11 13:47:35 +0000180 uintX_t OffsetInSec;
181 bool UseSymVA;
182 uintX_t Addend;
Rafael Espindolade9857e2016-02-04 21:33:05 +0000183
Rafael Espindola34ffcbb2016-04-11 13:51:23 +0000184 DynamicReloc(uint32_t Type, const OutputSectionBase<ELFT> *OffsetSec,
Rafael Espindolade9857e2016-02-04 21:33:05 +0000185 uintX_t OffsetInSec, bool UseSymVA, SymbolBody *Sym,
186 uintX_t Addend)
Rafael Espindola74031ba2016-04-07 15:20:56 +0000187 : Type(Type), Sym(Sym), OffsetSec(OffsetSec), OffsetInSec(OffsetInSec),
188 UseSymVA(UseSymVA), Addend(Addend) {}
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189};
190
191template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000192class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000194 typedef typename ELFT::Shdr Elf_Shdr;
195 typedef typename ELFT::Sym Elf_Sym;
196 typedef typename ELFT::SymRange Elf_Sym_Range;
197 typedef typename ELFT::uint uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000198 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000199 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000200
Rui Ueyama0db335f2015-10-07 16:58:54 +0000201 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000202 void writeTo(uint8_t *Buf) override;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000203 void addSymbol(SymbolBody *Body);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000204 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola0e92f242016-01-27 16:41:24 +0000205 unsigned getNumSymbols() const { return NumLocals + Symbols.size() + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000206
Rui Ueyamac2e863a2016-02-17 05:06:40 +0000207 ArrayRef<std::pair<SymbolBody *, size_t>> getSymbols() const {
Rafael Espindolae2c24612016-01-29 01:24:25 +0000208 return Symbols;
209 }
210
211 unsigned NumLocals = 0;
212 StringTableSection<ELFT> &StrTabSec;
Igor Kudrinab665fc2015-10-20 21:47:58 +0000213
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000214private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000215 void writeLocalSymbols(uint8_t *&Buf);
Igor Kudrinea6a8352015-10-19 08:01:51 +0000216 void writeGlobalSymbols(uint8_t *Buf);
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000217
Rui Ueyama874e7ae2016-02-17 04:56:44 +0000218 const OutputSectionBase<ELFT> *getOutputSection(SymbolBody *Sym);
Igor Kudrin853b88d2015-10-20 20:52:14 +0000219
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 Ueyama3a41be22016-04-07 22:49:21 +0000497template <class ELFT> class BuildIdSection : public OutputSectionBase<ELFT> {
Rui Ueyama634ddf02016-03-11 20:51:53 +0000498public:
Rui Ueyama634ddf02016-03-11 20:51:53 +0000499 void writeTo(uint8_t *Buf) override;
Rui Ueyama3a41be22016-04-07 22:49:21 +0000500 virtual void update(ArrayRef<uint8_t> Buf) = 0;
501 virtual void writeBuildId() = 0;
502
503protected:
504 BuildIdSection(size_t HashSize);
505 size_t HashSize;
506 uint8_t *HashBuf = nullptr;
507};
508
509template <class ELFT> class BuildIdFnv1 final : public BuildIdSection<ELFT> {
510public:
511 BuildIdFnv1() : BuildIdSection<ELFT>(8) {}
512 void update(ArrayRef<uint8_t> Buf) override;
513 void writeBuildId() override;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000514
515private:
Rui Ueyama3a41be22016-04-07 22:49:21 +0000516 // 64-bit FNV-1 initial value
517 uint64_t Hash = 0xcbf29ce484222325;
518};
519
520template <class ELFT> class BuildIdMd5 final : public BuildIdSection<ELFT> {
521public:
522 BuildIdMd5() : BuildIdSection<ELFT>(16) {}
523 void update(ArrayRef<uint8_t> Buf) override;
524 void writeBuildId() override;
525
526private:
527 llvm::MD5 Hash;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000528};
529
Rui Ueyamad86ec302016-04-07 23:51:56 +0000530template <class ELFT> class BuildIdSha1 final : public BuildIdSection<ELFT> {
531public:
532 BuildIdSha1() : BuildIdSection<ELFT>(20) {}
533 void update(ArrayRef<uint8_t> Buf) override;
534 void writeBuildId() override;
535
536private:
537 llvm::SHA1 Hash;
538};
539
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000540// All output sections that are hadnled by the linker specially are
541// globally accessible. Writer initializes them, so don't use them
542// until Writer is initialized.
543template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000544 typedef typename ELFT::uint uintX_t;
545 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyama634ddf02016-03-11 20:51:53 +0000546 static BuildIdSection<ELFT> *BuildId;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000547 static DynamicSection<ELFT> *Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000548 static EhFrameHeader<ELFT> *EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000549 static GnuHashTableSection<ELFT> *GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000550 static GotPltSection<ELFT> *GotPlt;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000551 static GotSection<ELFT> *Got;
552 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000553 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000554 static OutputSection<ELFT> *Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000555 static OutputSection<ELFT> *MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000556 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000557 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000558 static PltSection<ELFT> *Plt;
559 static RelocationSection<ELFT> *RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000560 static RelocationSection<ELFT> *RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000561 static StringTableSection<ELFT> *DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000562 static StringTableSection<ELFT> *ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000563 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000564 static SymbolTableSection<ELFT> *DynSymTab;
565 static SymbolTableSection<ELFT> *SymTab;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000566 static Elf_Phdr *TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000567 static OutputSectionBase<ELFT> *ElfHeader;
568 static OutputSectionBase<ELFT> *ProgramHeaders;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000569};
Rui Ueyamad888d102015-10-09 19:34:55 +0000570
Rui Ueyama634ddf02016-03-11 20:51:53 +0000571template <class ELFT> BuildIdSection<ELFT> *Out<ELFT>::BuildId;
Rui Ueyamad888d102015-10-09 19:34:55 +0000572template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
George Rimarf6bc65a2016-01-15 13:34:52 +0000573template <class ELFT> EhFrameHeader<ELFT> *Out<ELFT>::EhFrameHdr;
Igor Kudrin1b0d7062015-10-22 08:21:35 +0000574template <class ELFT> GnuHashTableSection<ELFT> *Out<ELFT>::GnuHashTab;
George Rimar648a2c32015-10-20 08:54:27 +0000575template <class ELFT> GotPltSection<ELFT> *Out<ELFT>::GotPlt;
Rui Ueyamad888d102015-10-09 19:34:55 +0000576template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
577template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000578template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000579template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Igor Kudrin304860a2015-11-12 04:39:49 +0000580template <class ELFT> OutputSection<ELFT> *Out<ELFT>::MipsRldMap;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000581template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000582template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000583template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
584template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
George Rimar648a2c32015-10-20 08:54:27 +0000585template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaPlt;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000586template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
George Rimar0f5ac9f2015-10-20 17:21:35 +0000587template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::ShStrTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000588template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000589template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
590template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000591template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindola4fc60442016-02-10 22:43:13 +0000592template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ElfHeader;
593template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::ProgramHeaders;
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000594
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000595} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000596} // namespace lld
597
598#endif // LLD_ELF_OUTPUT_SECTIONS_H