blob: 36713225840c951ecc78e26586fa7860c28be42b [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
Rafael Espindola17cb7c02016-12-19 17:01:01 +000023struct PhdrEntry;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000024class SymbolBody;
Rafael Espindola2deeb602016-07-21 20:18:30 +000025struct EhSectionPiece;
Rui Ueyama0b9a9032016-05-24 04:19:20 +000026template <class ELFT> class EhInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000027template <class ELFT> class InputSection;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000028template <class ELFT> class InputSectionBase;
Rafael Espindolac159c962015-10-19 21:00:02 +000029template <class ELFT> class MergeInputSection;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000030template <class ELFT> class OutputSection;
31template <class ELFT> class ObjectFile;
Peter Collingbourne21a12fc2016-04-27 20:22:31 +000032template <class ELFT> class SharedFile;
33template <class ELFT> class SharedSymbol;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000034template <class ELFT> class DefinedRegular;
35
Rafael Espindola71675852015-09-22 00:16:19 +000036// This represents a section in an output file.
37// Different sub classes represent different types of sections. Some contain
38// input sections, others are created by the linker.
39// The writer creates multiple OutputSections and assign them unique,
Rafael Espindola5805c4f2015-09-21 21:38:08 +000040// non-overlapping file offsets and VAs.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000041class OutputSectionBase {
Rafael Espindola5805c4f2015-09-21 21:38:08 +000042public:
Eugene Leviant9d278b62016-08-10 18:10:41 +000043 enum Kind {
44 Base,
Eugene Leviant9d278b62016-08-10 18:10:41 +000045 EHFrame,
Eugene Leviant9d278b62016-08-10 18:10:41 +000046 Merge,
Eugene Leviant9d278b62016-08-10 18:10:41 +000047 Regular,
Eugene Leviant9d278b62016-08-10 18:10:41 +000048 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000049
Rafael Espindolae08e78d2016-11-09 23:23:45 +000050 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
51 void setLMAOffset(uint64_t LMAOff) { LMAOffset = LMAOff; }
52 uint64_t getLMA() const { return Addr + LMAOffset; }
53 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola63732f52016-11-04 13:20:45 +000054 StringRef getName() const { return Name; }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000055
Rafael Espindolae08e78d2016-11-09 23:23:45 +000056 virtual void addSection(InputSectionData *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000057 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000058 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000059 return B->getKind() == Base;
60 }
Rui Ueyama40845e62015-12-26 05:51:07 +000061
Rui Ueyama2317d0d2015-10-15 20:55:22 +000062 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000063
Rafael Espindola0b113672016-07-27 14:10:56 +000064 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000065
Rafael Espindolae08e78d2016-11-09 23:23:45 +000066 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000067 if (Alignment > Addralign)
68 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000069 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000070
Rui Ueyama47091902016-03-30 19:41:51 +000071 // If true, this section will be page aligned on disk.
72 // Typically the first section of each PT_LOAD segment has this flag.
73 bool PageAlign = false;
74
Eugene Leviant3d9abec2016-09-29 09:20:33 +000075 // Pointer to the first section in PT_LOAD segment, which this section
76 // also resides in. This field is used to correctly compute file offset
77 // of a section. When two sections share the same load segment, difference
78 // between their file offsets should be equal to difference between their
79 // virtual addresses. To compute some section offset we use the following
80 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000081 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000082
Rafael Espindola5805c4f2015-09-21 21:38:08 +000083 virtual void finalize() {}
Rafael Espindola1ebfc592017-01-13 21:05:46 +000084 virtual void forEachInputSection(std::function<void(InputSectionData *)> F) {}
Rui Ueyama809d8e22016-06-23 04:33:42 +000085 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000086 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000087 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000088
Rafael Espindola5805c4f2015-09-21 21:38:08 +000089 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +000090
91 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000092 uint64_t Size = 0;
93 uint64_t Entsize = 0;
94 uint64_t Addralign = 0;
95 uint64_t Offset = 0;
96 uint64_t Flags = 0;
97 uint64_t LMAOffset = 0;
98 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000099 uint32_t ShName = 0;
100 uint32_t Type = 0;
101 uint32_t Info = 0;
102 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103};
104
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000105template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000106
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000107public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000108 typedef typename ELFT::Shdr Elf_Shdr;
109 typedef typename ELFT::Sym Elf_Sym;
110 typedef typename ELFT::Rel Elf_Rel;
111 typedef typename ELFT::Rela Elf_Rela;
112 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000113 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000114 void addSection(InputSectionData *C) override;
Rui Ueyama31270312016-12-20 01:51:08 +0000115 void sort(std::function<int(InputSection<ELFT> *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000116 void sortInitFini();
117 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000118 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000119 void finalize() override;
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000120 void forEachInputSection(std::function<void(InputSectionData *)> F) override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000121 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000122 Kind getKind() const override { return Regular; }
123 static bool classof(const OutputSectionBase *B) {
124 return B->getKind() == Regular;
125 }
Rafael Espindola71675852015-09-22 00:16:19 +0000126 std::vector<InputSection<ELFT> *> Sections;
Eugene Leviant84569e62016-11-29 08:05:44 +0000127
128 // Location in the output buffer.
129 uint8_t *Loc = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000130};
131
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +0000132template <class ELFT>
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000133class MergeOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000134 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000135
136public:
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000137 MergeOutputSection(StringRef Name, uint32_t Type, uintX_t Flags,
138 uintX_t Alignment);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000139 void addSection(InputSectionData *S) override;
Rafael Espindolac159c962015-10-19 21:00:02 +0000140 void writeTo(uint8_t *Buf) override;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000141 void finalize() override;
Peter Collingbournee29e1422016-05-05 04:10:12 +0000142 bool shouldTailMerge() const;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000143 Kind getKind() const override { return Merge; }
144 static bool classof(const OutputSectionBase *B) {
145 return B->getKind() == Merge;
146 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000147
148private:
Rui Ueyama1880bbe2016-11-26 15:09:58 +0000149 void finalizeTailMerge();
150 void finalizeNoTailMerge();
151
Rafael Espindola7efa5be2016-02-19 14:17:40 +0000152 llvm::StringTableBuilder Builder;
Rui Ueyama406b4692016-05-27 14:39:13 +0000153 std::vector<MergeInputSection<ELFT> *> Sections;
Rafael Espindolac159c962015-10-19 21:00:02 +0000154};
155
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000156struct CieRecord {
Rafael Espindola2deeb602016-07-21 20:18:30 +0000157 EhSectionPiece *Piece = nullptr;
158 std::vector<EhSectionPiece *> FdePieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000159};
160
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000161// Output section for .eh_frame.
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000162template <class ELFT> class EhOutputSection final : public OutputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000163 typedef typename ELFT::uint uintX_t;
164 typedef typename ELFT::Shdr Elf_Shdr;
165 typedef typename ELFT::Rel Elf_Rel;
166 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyamaf86cb902016-05-23 15:12:41 +0000167
168public:
169 EhOutputSection();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000170 void writeTo(uint8_t *Buf) override;
Rafael Espindola56004c52016-04-07 14:22:09 +0000171 void finalize() override;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000172 bool empty() const { return Sections.empty(); }
Rafael Espindola1ebfc592017-01-13 21:05:46 +0000173 void forEachInputSection(std::function<void(InputSectionData *)> F) override;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000174
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000175 void addSection(InputSectionData *S) override;
176 Kind getKind() const override { return EHFrame; }
177 static bool classof(const OutputSectionBase *B) {
178 return B->getKind() == EHFrame;
179 }
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000180
Rui Ueyamade9777a2016-05-23 16:30:41 +0000181 size_t NumFdes = 0;
182
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000183private:
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000184 template <class RelTy>
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000185 void addSectionAux(EhInputSection<ELFT> *S, llvm::ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000186
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000187 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000188 CieRecord *addCie(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000189
190 template <class RelTy>
Eugene Leviantc8c1b7b2016-11-25 08:27:15 +0000191 bool isFdeLive(EhSectionPiece &Piece, ArrayRef<RelTy> Rels);
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000192
Rui Ueyamae75e9332016-05-23 03:00:33 +0000193 uintX_t getFdePc(uint8_t *Buf, size_t Off, uint8_t Enc);
194
Rui Ueyama0b9a9032016-05-24 04:19:20 +0000195 std::vector<EhInputSection<ELFT> *> Sections;
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000196 std::vector<CieRecord *> Cies;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000197
Rui Ueyamaf8b285c2016-05-22 23:16:14 +0000198 // CIE records are uniquified by their contents and personality functions.
199 llvm::DenseMap<std::pair<ArrayRef<uint8_t>, SymbolBody *>, CieRecord> CieMap;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000200};
201
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000202// All output sections that are hadnled by the linker specially are
203// globally accessible. Writer initializes them, so don't use them
204// until Writer is initialized.
205template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000206 typedef typename ELFT::uint uintX_t;
207 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000208
209 static uint8_t First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000210 static EhOutputSection<ELFT> *EhFrame;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000211 static OutputSection<ELFT> *Bss;
Peter Collingbournefeb66292017-01-10 01:21:50 +0000212 static OutputSection<ELFT> *BssRelRo;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000213 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000214 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000215 static PhdrEntry *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000216 static OutputSectionBase *DebugInfo;
217 static OutputSectionBase *ElfHeader;
218 static OutputSectionBase *ProgramHeaders;
219 static OutputSectionBase *PreinitArray;
220 static OutputSectionBase *InitArray;
221 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000222};
Rui Ueyamad888d102015-10-09 19:34:55 +0000223
Rafael Espindola72447082017-01-05 14:35:41 +0000224struct SectionKey {
George Rimar6892afa2016-07-12 09:49:43 +0000225 StringRef Name;
Rafael Espindola72447082017-01-05 14:35:41 +0000226 uint64_t Flags;
227 uint64_t Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000228};
229
230// This class knows how to create an output section for a given
231// input section. Output section type is determined by various
232// factors, including input section's sh_flags, sh_type and
233// linker scripts.
234template <class ELFT> class OutputSectionFactory {
235 typedef typename ELFT::Shdr Elf_Shdr;
236 typedef typename ELFT::uint uintX_t;
George Rimar6892afa2016-07-12 09:49:43 +0000237
238public:
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000239 OutputSectionFactory();
240 ~OutputSectionFactory();
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000241 std::pair<OutputSectionBase *, bool> create(InputSectionBase<ELFT> *C,
242 StringRef OutsecName);
Rafael Espindola72447082017-01-05 14:35:41 +0000243 std::pair<OutputSectionBase *, bool> create(const SectionKey &Key,
244 InputSectionBase<ELFT> *C);
George Rimar6892afa2016-07-12 09:49:43 +0000245
George Rimar6892afa2016-07-12 09:49:43 +0000246private:
Rafael Espindola72447082017-01-05 14:35:41 +0000247 llvm::SmallDenseMap<SectionKey, OutputSectionBase *> Map;
George Rimar6892afa2016-07-12 09:49:43 +0000248};
249
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000250template <class ELFT> uint64_t getHeaderSize() {
251 if (Config->OFormatBinary)
252 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000253 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000254}
255
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000256template <class ELFT> uint8_t Out<ELFT>::First;
Rui Ueyama3b31e672016-05-23 16:24:16 +0000257template <class ELFT> EhOutputSection<ELFT> *Out<ELFT>::EhFrame;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000258template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Peter Collingbournefeb66292017-01-10 01:21:50 +0000259template <class ELFT> OutputSection<ELFT> *Out<ELFT>::BssRelRo;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000260template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000261template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000262template <class ELFT> PhdrEntry *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000263template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
264template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
265template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
266template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
267template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
268template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000269} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000270} // namespace lld
271
George Rimar6892afa2016-07-12 09:49:43 +0000272
273#endif