blob: 47959806dbb0402173c239270c89113dc01c5ba0 [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;
28template <bool Is64Bits> class StringTableSection;
29template <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,
41 const typename llvm::object::ELFFile<ELFT>::Elf_Rel &Sym);
Rafael Espindolacc6ebb82015-10-14 18:42:16 +000042bool canBePreempted(const SymbolBody *Body, bool NeedsGot);
Rafael Espindola4f674ed2015-10-05 15:24:04 +000043template <class ELFT> bool includeInSymtab(const SymbolBody &B);
44
Rafael Espindola05a3dd22015-09-22 23:38:23 +000045bool includeInDynamicSymtab(const SymbolBody &B);
Rafael Espindolad1cf4212015-10-05 16:25:43 +000046
47template <class ELFT>
48bool shouldKeepInSymtab(
Rafael Espindola444576d2015-10-09 19:25:07 +000049 const ObjectFile<ELFT> &File, StringRef Name,
50 const typename llvm::object::ELFFile<ELFT>::Elf_Sym &Sym);
Davide Italiano85121bb2015-09-25 03:56:11 +000051
Rafael Espindola71675852015-09-22 00:16:19 +000052// This represents a section in an output file.
53// Different sub classes represent different types of sections. Some contain
54// input sections, others are created by the linker.
55// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000056// non-overlapping file offsets and VAs.
57template <bool Is64Bits> class OutputSectionBase {
58public:
59 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
60 typedef typename std::conditional<Is64Bits, llvm::ELF::Elf64_Shdr,
61 llvm::ELF::Elf32_Shdr>::type HeaderT;
62
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; }
67 template <llvm::object::endianness E>
68 void writeHeaderTo(typename llvm::object::ELFFile<
69 llvm::object::ELFType<E, Is64Bits>>::Elf_Shdr *SHdr);
70 StringRef getName() { return Name; }
71 void setNameOffset(uintX_t Offset) { Header.sh_name = Offset; }
72
Rui Ueyama2317d0d2015-10-15 20:55:22 +000073 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000074
75 // Returns the size of the section in the output file.
Rafael Espindola77572242015-10-02 19:37:55 +000076 uintX_t getSize() const { return Header.sh_size; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000077 void setSize(uintX_t Val) { Header.sh_size = Val; }
78 uintX_t getFlags() { return Header.sh_flags; }
79 uintX_t getFileOff() { return Header.sh_offset; }
80 uintX_t getAlign() {
81 // The ELF spec states that a value of 0 means the section has no alignment
82 // constraits.
83 return std::max<uintX_t>(Header.sh_addralign, 1);
84 }
85 uint32_t getType() { return Header.sh_type; }
86
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087 virtual void finalize() {}
88 virtual void writeTo(uint8_t *Buf) = 0;
89
90protected:
91 StringRef Name;
92 HeaderT Header;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000093 ~OutputSectionBase() = default;
94};
95
96template <class ELFT>
97class GotSection final : public OutputSectionBase<ELFT::Is64Bits> {
98 typedef OutputSectionBase<ELFT::Is64Bits> Base;
99 typedef typename Base::uintX_t uintX_t;
100
101public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000102 GotSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103 void finalize() override {
Rui Ueyama5f551ae2015-10-14 14:02:06 +0000104 this->Header.sh_size = Entries.size() * sizeof(uintX_t);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105 }
Rafael Espindolaa6627382015-10-06 23:56:53 +0000106 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107 void addEntry(SymbolBody *Sym);
108 bool empty() const { return Entries.empty(); }
109 uintX_t getEntryAddr(const SymbolBody &B) const;
110
111private:
112 std::vector<const SymbolBody *> Entries;
113};
114
115template <class ELFT>
116class PltSection final : public OutputSectionBase<ELFT::Is64Bits> {
117 typedef OutputSectionBase<ELFT::Is64Bits> Base;
118 typedef typename Base::uintX_t uintX_t;
119
120public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000121 PltSection();
Hal Finkel6c2a3b82015-10-08 21:51:31 +0000122 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000123 void writeTo(uint8_t *Buf) override;
124 void addEntry(SymbolBody *Sym);
125 bool empty() const { return Entries.empty(); }
126 uintX_t getEntryAddr(const SymbolBody &B) const;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000127
128private:
129 std::vector<const SymbolBody *> Entries;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130};
131
132template <class ELFT> struct DynamicReloc {
133 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
134 const InputSection<ELFT> &C;
135 const Elf_Rel &RI;
136};
137
138template <class ELFT>
139class SymbolTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
140public:
141 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
142 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
143 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym_Range Elf_Sym_Range;
144 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000145 SymbolTableSection(SymbolTable<ELFT> &Table,
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000146 StringTableSection<ELFT::Is64Bits> &StrTabSec);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000147
Rui Ueyama0db335f2015-10-07 16:58:54 +0000148 void finalize() override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000149 void writeTo(uint8_t *Buf) override;
Rui Ueyama0db335f2015-10-07 16:58:54 +0000150 void addSymbol(StringRef Name, bool isLocal = false);
Davide Italianoe44456b2015-09-23 01:50:53 +0000151 StringTableSection<ELFT::Is64Bits> &getStrTabSec() const { return StrTabSec; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000152 unsigned getNumSymbols() const { return NumVisible + 1; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000153
154private:
Rui Ueyama8ddfa812015-09-30 00:32:10 +0000155 void writeLocalSymbols(uint8_t *&Buf);
156 void writeGlobalSymbols(uint8_t *&Buf);
157
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000158 SymbolTable<ELFT> &Table;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000159 StringTableSection<ELFT::Is64Bits> &StrTabSec;
160 unsigned NumVisible = 0;
161 unsigned NumLocals = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000162};
163
164template <class ELFT>
165class RelocationSection final : public OutputSectionBase<ELFT::Is64Bits> {
166 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
167 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola3c83e2b2015-10-05 21:09:37 +0000168 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000169
170public:
Rui Ueyamac58656c2015-10-13 16:59:30 +0000171 RelocationSection(bool IsRela);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000172 void addReloc(const DynamicReloc<ELFT> &Reloc) { Relocs.push_back(Reloc); }
173 void finalize() override;
174 void writeTo(uint8_t *Buf) override;
175 bool hasRelocs() const { return !Relocs.empty(); }
176 bool isRela() const { return IsRela; }
177
178private:
179 std::vector<DynamicReloc<ELFT>> Relocs;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000180 const bool IsRela;
181};
182
183template <class ELFT>
184class OutputSection final : public OutputSectionBase<ELFT::Is64Bits> {
185public:
186 typedef typename OutputSectionBase<ELFT::Is64Bits>::uintX_t uintX_t;
187 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
188 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
189 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
190 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000191 OutputSection(StringRef Name, uint32_t sh_type, uintX_t sh_flags);
Rafael Espindola71675852015-09-22 00:16:19 +0000192 void addSection(InputSection<ELFT> *C);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000193 void writeTo(uint8_t *Buf) override;
194
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000195private:
Rafael Espindola71675852015-09-22 00:16:19 +0000196 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000197};
198
199template <bool Is64Bits>
200class InterpSection final : public OutputSectionBase<Is64Bits> {
201public:
202 InterpSection();
203
204 void writeTo(uint8_t *Buf);
205};
206
207template <bool Is64Bits>
208class StringTableSection final : public OutputSectionBase<Is64Bits> {
209public:
210 typedef typename OutputSectionBase<Is64Bits>::uintX_t uintX_t;
Rafael Espindola35c6af32015-09-25 17:19:10 +0000211 StringTableSection(bool Dynamic);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000212 void add(StringRef S) { StrTabBuilder.add(S); }
213 size_t getFileOff(StringRef S) const { return StrTabBuilder.getOffset(S); }
214 StringRef data() const { return StrTabBuilder.data(); }
215 void writeTo(uint8_t *Buf) override;
216
217 void finalize() override {
218 StrTabBuilder.finalize(llvm::StringTableBuilder::ELF);
219 this->Header.sh_size = StrTabBuilder.data().size();
220 }
221
222 bool isDynamic() const { return Dynamic; }
223
224private:
225 const bool Dynamic;
226 llvm::StringTableBuilder StrTabBuilder;
227};
228
229template <class ELFT>
230class HashTableSection final : public OutputSectionBase<ELFT::Is64Bits> {
231 typedef typename llvm::object::ELFFile<ELFT>::Elf_Word Elf_Word;
232
233public:
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000234 HashTableSection();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000235 void addSymbol(SymbolBody *S);
Rui Ueyama0db335f2015-10-07 16:58:54 +0000236 void finalize() override;
237 void writeTo(uint8_t *Buf) override;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000238
239private:
240 uint32_t hash(StringRef Name) {
241 uint32_t H = 0;
242 for (char C : Name) {
243 H = (H << 4) + C;
244 uint32_t G = H & 0xf0000000;
245 if (G)
246 H ^= G >> 24;
247 H &= ~G;
248 }
249 return H;
250 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000251 std::vector<uint32_t> Hashes;
252};
253
254template <class ELFT>
255class DynamicSection final : public OutputSectionBase<ELFT::Is64Bits> {
256 typedef OutputSectionBase<ELFT::Is64Bits> Base;
257 typedef typename Base::HeaderT HeaderT;
Rui Ueyama2dfd74f2015-09-30 21:57:53 +0000258 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
259 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
260 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Hal Finkeld26da922015-10-02 16:21:30 +0000261 typedef typename llvm::object::ELFFile<ELFT>::Elf_Dyn Elf_Dyn;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000262
263public:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000264 DynamicSection(SymbolTable<ELFT> &SymTab);
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000265 void finalize() override;
266 void writeTo(uint8_t *Buf) override;
267
Rafael Espindolaae81a7b2015-10-15 15:29:53 +0000268 OutputSectionBase<ELFT::Is64Bits> *PreInitArraySec = nullptr;
269 OutputSectionBase<ELFT::Is64Bits> *InitArraySec = nullptr;
270 OutputSectionBase<ELFT::Is64Bits> *FiniArraySec = nullptr;
Rafael Espindola77572242015-10-02 19:37:55 +0000271
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000272private:
Rui Ueyama3ce825e2015-10-09 21:07:25 +0000273 SymbolTable<ELFT> &SymTab;
Igor Kudrinb1f2b512015-10-05 10:29:46 +0000274 const ELFSymbolBody<ELFT> *InitSym = nullptr;
275 const ELFSymbolBody<ELFT> *FiniSym = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000276};
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000277
278// All output sections that are hadnled by the linker specially are
279// globally accessible. Writer initializes them, so don't use them
280// until Writer is initialized.
281template <class ELFT> struct Out {
282 static DynamicSection<ELFT> *Dynamic;
283 static GotSection<ELFT> *Got;
284 static HashTableSection<ELFT> *HashTab;
285 static InterpSection<ELFT::Is64Bits> *Interp;
286 static OutputSection<ELFT> *Bss;
Rafael Espindolaae81a7b2015-10-15 15:29:53 +0000287 static OutputSectionBase<ELFT::Is64Bits> *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000288 static uint8_t *OpdBuf;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000289 static PltSection<ELFT> *Plt;
290 static RelocationSection<ELFT> *RelaDyn;
291 static StringTableSection<ELFT::Is64Bits> *DynStrTab;
292 static StringTableSection<ELFT::Is64Bits> *StrTab;
293 static SymbolTableSection<ELFT> *DynSymTab;
294 static SymbolTableSection<ELFT> *SymTab;
295};
Rui Ueyamad888d102015-10-09 19:34:55 +0000296
297template <class ELFT> DynamicSection<ELFT> *Out<ELFT>::Dynamic;
298template <class ELFT> GotSection<ELFT> *Out<ELFT>::Got;
299template <class ELFT> HashTableSection<ELFT> *Out<ELFT>::HashTab;
300template <class ELFT> InterpSection<ELFT::Is64Bits> *Out<ELFT>::Interp;
301template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rafael Espindolaae81a7b2015-10-15 15:29:53 +0000302template <class ELFT> OutputSectionBase<ELFT::Is64Bits> *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000303template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyamad888d102015-10-09 19:34:55 +0000304template <class ELFT> PltSection<ELFT> *Out<ELFT>::Plt;
305template <class ELFT> RelocationSection<ELFT> *Out<ELFT>::RelaDyn;
306template <class ELFT> StringTableSection<ELFT::Is64Bits> *Out<ELFT>::DynStrTab;
307template <class ELFT> StringTableSection<ELFT::Is64Bits> *Out<ELFT>::StrTab;
308template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::DynSymTab;
309template <class ELFT> SymbolTableSection<ELFT> *Out<ELFT>::SymTab;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000310}
311}
312#endif