blob: edb028ff790a848953a4c38b6bcf903e63579773 [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"
Simon Atanasyan41325112016-06-19 21:39:37 +000014#include "Relocations.h"
Davide Italiano85121bb2015-09-25 03:56:11 +000015
Rui Ueyamaa0752a52016-03-13 20:28:29 +000016#include "lld/Core/LLVM.h"
17#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;
Rafael Espindola2deeb602016-07-21 20:18:30 +000024struct EhSectionPiece;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000025template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000026template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000027template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000028template <class ELFT> class MergeInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000029template <class ELFT> class OutputSection;
30template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000031template <class ELFT> class SharedFile;
32template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000033template <class ELFT> class DefinedRegular;
34
Rafael Espindola71675852015-09-22 00:16:19 +000035// This represents a section in an output file.
36// Different sub classes represent different types of sections. Some contain
37// input sections, others are created by the linker.
38// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000039// non-overlapping file offsets and VAs.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000040class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000041public:
Eugene Leviant9d278b62016-08-10 18:10:41 +000042 enum Kind {
43 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000044 EHFrame,
Eugene Leviant9d278b62016-08-10 18:10:41 +000045 Merge,
Eugene Leviant9d278b62016-08-10 18:10:41 +000046 Regular,
Eugene Leviant9d278b62016-08-10 18:10:41 +000047 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000048
Rafael Espindolae08e78d2016-11-09 23:23:45 +000049 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
50 void setLMAOffset(uint64_t LMAOff) { LMAOffset = LMAOff; }
51 uint64_t getLMA() const { return Addr + LMAOffset; }
52 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000053 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000054
Rafael Espindolae08e78d2016-11-09 23:23:45 +000055 virtual void addSection(InputSectionData *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000056 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000057 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000058 return B->getKind() == Base;
59 }
Rui Ueyama40845e62015-12-26 05:51:07 +000060
Rui Ueyama2317d0d2015-10-15 20:55:22 +000061 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000062
Rafael Espindola0b113672016-07-27 14:10:56 +000063 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000064
Rafael Espindolae08e78d2016-11-09 23:23:45 +000065 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000066 if (Alignment > Addralign)
67 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000068 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000069
Rui Ueyama47091902016-03-30 19:41:51 +000070 // If true, this section will be page aligned on disk.
71 // Typically the first section of each PT_LOAD segment has this flag.
72 bool PageAlign = false;
73
Eugene Leviant3d9abec2016-09-29 09:20:33 +000074 // Pointer to the first section in PT_LOAD segment, which this section
75 // also resides in. This field is used to correctly compute file offset
76 // of a section. When two sections share the same load segment, difference
77 // between their file offsets should be equal to difference between their
78 // virtual addresses. To compute some section offset we use the following
79 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000080 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000081
Rafael Espindola5805c4f2015-09-21 21:38:08 +000082 virtual void finalize() {}
Rui Ueyama809d8e22016-06-23 04:33:42 +000083 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000084 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000085 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000086
Rafael Espindola5805c4f2015-09-21 21:38:08 +000087 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +000088
89 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000090 uint64_t Size = 0;
91 uint64_t Entsize = 0;
92 uint64_t Addralign = 0;
93 uint64_t Offset = 0;
94 uint64_t Flags = 0;
95 uint64_t LMAOffset = 0;
96 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000097 uint32_t ShName = 0;
98 uint32_t Type = 0;
99 uint32_t Info = 0;
100 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000101};
102
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000103template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000104
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000105public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000106 typedef typename ELFT::Shdr Elf_Shdr;
107 typedef typename ELFT::Sym Elf_Sym;
108 typedef typename ELFT::Rel Elf_Rel;
109 typedef typename ELFT::Rela Elf_Rela;
110 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000111 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000112 void addSection(InputSectionData *C) override;
George Rimar1a33c0f2016-11-10 09:05:20 +0000113 void sort(std::function<unsigned(InputSection<ELFT> *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000114 void sortInitFini();
115 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000116 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000117 void finalize() override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000118 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000119 Kind getKind() const override { return Regular; }
120 static bool classof(const OutputSectionBase *B) {
121 return B->getKind() == Regular;
122 }
Rafael Espindola71675852015-09-22 00:16:19 +0000123 std::vector<InputSection<ELFT> *> Sections;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000124};
125
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000126template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000127class MergeOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000128 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000129
130public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000131 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
132 uintX_t Alignment);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000133 void addSection(InputSectionData *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000134 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000135 void finalize() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000136 bool shouldTailMerge() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000137 Kind getKind() const override { return Merge; }
138 static bool classof(const OutputSectionBase *B) {
139 return B->getKind() == Merge;
140 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000141
142private:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000143 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000144 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000145};
146
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000147struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000148 EhSectionPiece *Piece = nullptr;
149 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000150};
151
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000152// Output section for .eh_frame.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000153template <class ELFT> class EhOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000154 typedef typename ELFT::uint uintX_t;
155 typedef typename ELFT::Shdr Elf_Shdr;
156 typedef typename ELFT::Rel Elf_Rel;
157 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000158
159public:
160 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000161 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000162 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000163 bool empty() const { return Sections.empty(); }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000164
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000165 void addSection(InputSectionData *S) override;
166 Kind getKind() const override { return EHFrame; }
167 static bool classof(const OutputSectionBase *B) {
168 return B->getKind() == EHFrame;
169 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000170
Rui Ueyamade9777a2016-05-23 16:30:41 +0000171 size_t NumFdes = 0;
172
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000173private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000174 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000175 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000176
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000177 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000178 CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000179
180 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000181 bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000182
Rui Ueyamae75e9332016-05-23 03:00:33 +0000183 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
184
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000185 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000186 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000187
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000188 // CIE records are uniquified by their contents and personality functions.
189 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000190};
191
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000192// All output sections that are hadnled by the linker specially are
193// globally accessible. Writer initializes them, so don't use them
194// until Writer is initialized.
195template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000196 typedef typename ELFT::uint uintX_t;
197 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000198
199 static uint8_t First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000200 static EhOutputSection<ELFT> *EhFrame;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000201 static OutputSection<ELFT> *Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000202 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000203 static uint8_t *OpdBuf;
Rafael Espindolaea7a1e902015-11-06 22:14:44 +0000204 static Elf_Phdr *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000205 static OutputSectionBase *DebugInfo;
206 static OutputSectionBase *ElfHeader;
207 static OutputSectionBase *ProgramHeaders;
208 static OutputSectionBase *PreinitArray;
209 static OutputSectionBase *InitArray;
210 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211};
Rui Ueyamad888d102015-10-09 19:34:55 +0000212
George Rimar6892afa2016-07-12 09:49:43 +0000213template <bool Is64Bits> struct SectionKey {
214 typedef typename std::conditional<Is64Bits, uint64_t, uint32_t>::type uintX_t;
215 StringRef Name;
216 uint32_t Type;
217 uintX_t Flags;
218 uintX_t Alignment;
219};
220
221// This class knows how to create an output section for a given
222// input section. Output section type is determined by various
223// factors, including input section's sh_flags, sh_type and
224// linker scripts.
225template <class ELFT> class OutputSectionFactory {
226 typedef typename ELFT::Shdr Elf_Shdr;
227 typedef typename ELFT::uint uintX_t;
228 typedef typename elf::SectionKey<ELFT::Is64Bits> Key;
229
230public:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000231 std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
232 StringRef OutsecName);
233 std::pair<OutputSectionBase *, bool>
Rafael Espindola10897f12016-09-13 14:23:14 +0000234 create(const SectionKey<ELFT::Is64Bits> &Key, InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000235
George Rimar6892afa2016-07-12 09:49:43 +0000236private:
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000237 llvm::SmallDenseMap<Key, OutputSectionBase *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000238};
239
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000240template <class ELFT> uint64_t getHeaderSize() {
241 if (Config->OFormatBinary)
242 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000243 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000244}
245
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000246template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000247template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000248template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000249template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000250template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000251template <class ELFT> typename ELFT::Phdr *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000252template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
253template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
254template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
255template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
256template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
257template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000258} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000259} // namespace lld
260
George Rimar6892afa2016-07-12 09:49:43 +0000261namespace llvm {
262template <bool Is64Bits> struct DenseMapInfo<lld::elf::SectionKey<Is64Bits>> {
263 typedef typename lld::elf::SectionKey<Is64Bits> Key;
264
265 static Key getEmptyKey();
266 static Key getTombstoneKey();
267 static unsigned getHashValue(const Key &Val);
268 static bool isEqual(const Key &LHS, const Key &RHS);
269};
270}
271
272#endif