blob: 4c62f70d86332560b9ecac781a44189b9af62002 [file] [log] [blame]
Rafael Espindola5805c4f2015-09-21 21:38:08 +00001//===- OutputSections.h -----------------------------------------*- C++ -*-===//
2//
3// The LLVM Linker
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef LLD_ELF_OUTPUT_SECTIONS_H
11#define LLD_ELF_OUTPUT_SECTIONS_H
12
13#include "lld/Core/LLVM.h"
14
15#include "llvm/MC/StringTableBuilder.h"
16#include "llvm/Object/ELF.h"
17
Davide Italiano85121bb2015-09-25 03:56:11 +000018#include "Config.h"
19
Rafael Espindola5805c4f2015-09-21 21:38:08 +000020#include <type_traits>
21
22namespace lld {
23namespace elf2 {
24
25class SymbolBody;
Rui Ueyama3ce825e2015-10-09 21:07:25 +000026template <class ELFT> class SymbolTable;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class SymbolTableSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000028template <class ELFT> class StringTableSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class InputSection;
30template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
32template <class ELFT> class DefinedRegular;
Rafael Espindolacd076f02015-09-25 18:19:03 +000033template <class ELFT> class ELFSymbolBody;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034
Rafael Espindola5805c4f2015-09-21 21:38:08 +000035template <class ELFT>
Rui Ueyama15ef5e12015-10-07 19:18:16 +000036typename llvm::object::ELFFile<ELFT>::uintX_t getSymVA(const SymbolBody &S);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000037
38template <class ELFT>
39typename llvm::object::ELFFile<ELFT>::uintX_t
Rui Ueyama126d08f2015-10-12 20:28:22 +000040getLocalRelTarget(const ObjectFile<ELFT> &File,
Hal Finkel6f97c2b2015-10-16 21:55:40 +000041 const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Sym,
42 uint32_t Type);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000043bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000044template <class ELFT> bool includeInSymtab(const SymbolBody &B);
45
Rafael Espindola05a3dd22015-09-22 23:38:23 +000046bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000047
48template <class ELFT>
49bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000050 const ObjectFile<ELFT> &File, StringRef Name,
51 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000052
Rafael Espindola71675852015-09-22 00:16:19 +000053// This represents a section in an output file.
54// Different sub classes represent different types of sections. Some contain
55// input sections, others are created by the linker.
56// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000057// non-overlapping file offsets and VAs.
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000058template <class ELFT> class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000060 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
61 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062
63 OutputSectionBase(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
64 void setVA(uintX_t VA) { Header.sh_addr = VA; }
65 uintX_t getVA() const { return Header.sh_addr; }
66 void setFileOffset(uintX_t Off) { Header.sh_offset = Off; }
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000067 void writeHeaderTo(Elf_Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000068 StringRef getName() { return Name; }
69 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
70
Rui Ueyama2317d0d2015-10-15 20:55:22 +000071 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000072
73 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000074 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000075 void setSize(uintX_t Val) { Header.sh_size = Val; }
76 uintX_t getFlags() { return Header.sh_flags; }
77 uintX_t getFileOff() { return Header.sh_offset; }
78 uintX_t getAlign() {
79 // The ELF spec states that a value of 0 means the section has no alignment
80 // constraits.
81 return std::max<uintX_t>(Header.sh_addralign, 1);
82 }
83 uint32_t getType() { return Header.sh_type; }
84
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085 virtual void finalize() {}
86 virtual void writeTo(uint8_t *Buf) = 0;
87
88protected:
89 StringRef Name;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000090 Elf_Shdr Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000091 ~OutputSectionBase() = default;
92};
93
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000094template <class ELFT> class GotSection final : public OutputSectionBase<ELFT> {
95 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000096 typedef typename Base::uintX_t uintX_t;
97
98public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +000099 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000100 void finalize() override {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000101 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000102 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000103 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000104 void addEntry(SymbolBody *Sym);
105 bool empty() const { return Entries.empty(); }
106 uintX_t getEntryAddr(const SymbolBody &B) const;
107
108private:
109 std::vector<const SymbolBody *> Entries;
110};
111
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000112template <class ELFT> class PltSection final : public OutputSectionBase<ELFT> {
113 typedef OutputSectionBase<ELFT> Base;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000114 typedef typename Base::uintX_t uintX_t;
115
116public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000117 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000118 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000119 void writeTo(uint8_t *Buf) override;
120 void addEntry(SymbolBody *Sym);
121 bool empty() const { return Entries.empty(); }
122 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123
124private:
125 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126};
127
128template <class ELFT> struct DynamicReloc {
129 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
130 const InputSection<ELFT> &C;
131 const Elf_Rel &RI;
132};
133
134template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000135class SymbolTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000136public:
137 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
138 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
139 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000140 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000141 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000142 StringTableSection<ELFT> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000143
Rui Ueyama0db335f2015-10-07 16:58:54 +0000144 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000145 void writeTo(uint8_t *Buf) override;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000146 void addSymbol(StringRef Name, bool isLocal = false);
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000147 StringTableSection<ELFT> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000148 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000149
150private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000151 void writeLocalSymbols(uint8_t *&Buf);
152 void writeGlobalSymbols(uint8_t *&Buf);
153
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000154 SymbolTable<ELFT> &Table;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000155 StringTableSection<ELFT> &StrTabSec;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000156 unsigned NumVisible = 0;
157 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000158};
159
160template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000161class RelocationSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
163 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000164 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000165
166public:
Rui Ueyamac58656c2015-10-13 16:59:30 +0000167 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000168 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
169 void finalize() override;
170 void writeTo(uint8_t *Buf) override;
171 bool hasRelocs() const { return !Relocs.empty(); }
172 bool isRela() const { return IsRela; }
173
174private:
175 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000176 const bool IsRela;
177};
178
179template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000180class OutputSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000181public:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000182 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
183 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
184 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
185 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000186 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000187 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000188 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000189 void writeTo(uint8_t *Buf) override;
190
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000191private:
Rafael Espindola71675852015-09-22 00:16:19 +0000192 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193};
194
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000195template <class ELFT>
196class InterpSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197public:
198 InterpSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000199 void writeTo(uint8_t *Buf);
200};
201
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000202template <class ELFT>
203class StringTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000204public:
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000205 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000206 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000207 void add(StringRef S) { StrTabBuilder.add(S); }
208 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
209 StringRef data() const { return StrTabBuilder.data(); }
210 void writeTo(uint8_t *Buf) override;
211
212 void finalize() override {
213 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
214 this->Header.sh_size = StrTabBuilder.data().size();
215 }
216
217 bool isDynamic() const { return Dynamic; }
218
219private:
220 const bool Dynamic;
221 llvm::StringTableBuilder StrTabBuilder;
222};
223
224template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000225class HashTableSection final : public OutputSectionBase<ELFT> {
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000226 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
227
228public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000229 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000230 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000231 void finalize() override;
232 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000233
234private:
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 std::vector<uint32_t> Hashes;
236};
237
238template <class ELFT>
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000239class DynamicSection final : public OutputSectionBase<ELFT> {
240 typedef OutputSectionBase<ELFT> Base;
241 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000242 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
243 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000244 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000245 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000246
247public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000248 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000249 void finalize() override;
250 void writeTo(uint8_t *Buf) override;
251
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000252 OutputSectionBase<ELFT> *PreInitArraySec = nullptr;
253 OutputSectionBase<ELFT> *InitArraySec = nullptr;
254 OutputSectionBase<ELFT> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000255
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000256private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000257 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000258 const ELFSymbolBody<ELFT> *InitSym = nullptr;
259 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000260};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000261
262// All output sections that are hadnled by the linker specially are
263// globally accessible. Writer initializes them, so don't use them
264// until Writer is initialized.
265template <class ELFT> struct Out {
266 static DynamicSection<ELFT> *Dynamic;
267 static GotSection<ELFT> *Got;
268 static HashTableSection<ELFT> *HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000269 static InterpSection<ELFT> *Interp;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000270 static OutputSection<ELFT> *Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000271 static OutputSectionBase<ELFT> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000272 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000273 static PltSection<ELFT> *Plt;
274 static RelocationSection<ELFT> *RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000275 static StringTableSection<ELFT> *DynStrTab;
276 static StringTableSection<ELFT> *StrTab;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000277 static SymbolTableSection<ELFT> *DynSymTab;
278 static SymbolTableSection<ELFT> *SymTab;
279};
Rui Ueyamad888d102015-10-09 19:34:55 +0000280
281template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
282template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
283template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000284template <class ELFT> InterpSection<ELFT> *Out<ELFT>::Interp;
Rui Ueyamad888d102015-10-09 19:34:55 +0000285template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000286template <class ELFT> OutputSectionBase<ELFT> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000287template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000288template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
289template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000290template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::DynStrTab;
291template <class ELFT> StringTableSection<ELFT> *Out<ELFT>::StrTab;
Rui Ueyamad888d102015-10-09 19:34:55 +0000292template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
293template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000294}
295}
296#endif