blob: d2f0c3b117560795f189fc4597f0a7d3183a77b4 [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 Espindola774ea7d2017-02-23 16:49:07 +000027class InputSection;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000028class 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 Regular,
Eugene Leviant9d278b62016-08-10 18:10:41 +000046 };
Rafael Espindola5805c4f2015-09-21 21:38:08 +000047
Rafael Espindolae08e78d2016-11-09 23:23:45 +000048 OutputSectionBase(StringRef Name, uint32_t Type, uint64_t Flags);
Rafael Espindolae08e78d2016-11-09 23:23:45 +000049 uint64_t getLMA() const { return Addr + LMAOffset; }
50 template <typename ELFT> void writeHeaderTo(typename ELFT::Shdr *SHdr);
Rafael Espindola5805c4f2015-09-21 21:38:08 +000051
Rafael Espindolac404d502017-02-23 02:32:18 +000052 virtual void addSection(InputSectionBase *C) {}
Eugene Leviant9d278b62016-08-10 18:10:41 +000053 virtual Kind getKind() const { return Base; }
Rafael Espindolae08e78d2016-11-09 23:23:45 +000054 static bool classof(const OutputSectionBase *B) {
Eugene Leviant9d278b62016-08-10 18:10:41 +000055 return B->getKind() == Base;
56 }
Rui Ueyama40845e62015-12-26 05:51:07 +000057
Rui Ueyama2317d0d2015-10-15 20:55:22 +000058 unsigned SectionIndex;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000059
Rafael Espindola0b113672016-07-27 14:10:56 +000060 uint32_t getPhdrFlags() const;
Rui Ueyama3b04d832016-07-14 05:46:24 +000061
Rafael Espindolae08e78d2016-11-09 23:23:45 +000062 void updateAlignment(uint64_t Alignment) {
Rafael Espindola04a2e342016-11-09 01:42:41 +000063 if (Alignment > Addralign)
64 Addralign = Alignment;
Rafael Espindola115f0f32015-11-03 14:13:40 +000065 }
Rafael Espindola5805c4f2015-09-21 21:38:08 +000066
Rui Ueyama47091902016-03-30 19:41:51 +000067 // If true, this section will be page aligned on disk.
68 // Typically the first section of each PT_LOAD segment has this flag.
69 bool PageAlign = false;
70
Eugene Leviant3d9abec2016-09-29 09:20:33 +000071 // Pointer to the first section in PT_LOAD segment, which this section
72 // also resides in. This field is used to correctly compute file offset
73 // of a section. When two sections share the same load segment, difference
74 // between their file offsets should be equal to difference between their
75 // virtual addresses. To compute some section offset we use the following
76 // formula: Off = Off_first + VA - VA_first.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000077 OutputSectionBase *FirstInPtLoad = nullptr;
Eugene Leviant3d9abec2016-09-29 09:20:33 +000078
Rafael Espindola5805c4f2015-09-21 21:38:08 +000079 virtual void finalize() {}
Rafael Espindolac404d502017-02-23 02:32:18 +000080 virtual void forEachInputSection(std::function<void(InputSectionBase *)> F) {}
Rui Ueyama809d8e22016-06-23 04:33:42 +000081 virtual void assignOffsets() {}
Rafael Espindola4fc60442016-02-10 22:43:13 +000082 virtual void writeTo(uint8_t *Buf) {}
Rui Ueyamad4ea7dd2015-12-26 07:01:26 +000083 virtual ~OutputSectionBase() = default;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000084
Rafael Espindola5805c4f2015-09-21 21:38:08 +000085 StringRef Name;
Rafael Espindola04a2e342016-11-09 01:42:41 +000086
87 // The following fields correspond to Elf_Shdr members.
Rafael Espindolae08e78d2016-11-09 23:23:45 +000088 uint64_t Size = 0;
89 uint64_t Entsize = 0;
90 uint64_t Addralign = 0;
91 uint64_t Offset = 0;
92 uint64_t Flags = 0;
93 uint64_t LMAOffset = 0;
94 uint64_t Addr = 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +000095 uint32_t ShName = 0;
96 uint32_t Type = 0;
97 uint32_t Info = 0;
98 uint32_t Link = 0;
Rafael Espindola5805c4f2015-09-21 21:38:08 +000099};
100
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000101template <class ELFT> class OutputSection final : public OutputSectionBase {
Eugene Leviant9d278b62016-08-10 18:10:41 +0000102
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000103public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000104 typedef typename ELFT::Shdr Elf_Shdr;
105 typedef typename ELFT::Sym Elf_Sym;
106 typedef typename ELFT::Rel Elf_Rel;
107 typedef typename ELFT::Rela Elf_Rela;
108 typedef typename ELFT::uint uintX_t;
George Rimar9bec24a2016-02-18 14:20:08 +0000109 OutputSection(StringRef Name, uint32_t Type, uintX_t Flags);
Rafael Espindolac404d502017-02-23 02:32:18 +0000110 void addSection(InputSectionBase *C) override;
111 void sort(std::function<int(InputSectionBase *S)> Order);
Rui Ueyama5af83682016-02-11 23:41:38 +0000112 void sortInitFini();
113 void sortCtorsDtors();
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000114 void writeTo(uint8_t *Buf) override;
George Rimar58941ee2016-02-25 08:23:37 +0000115 void finalize() override;
Rafael Espindolac404d502017-02-23 02:32:18 +0000116 void forEachInputSection(std::function<void(InputSectionBase *)> F) override;
Rui Ueyama809d8e22016-06-23 04:33:42 +0000117 void assignOffsets() override;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000118 Kind getKind() const override { return Regular; }
119 static bool classof(const OutputSectionBase *B) {
120 return B->getKind() == Regular;
121 }
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000122 std::vector<InputSection *> Sections;
Eugene Leviant84569e62016-11-29 08:05:44 +0000123
124 // Location in the output buffer.
125 uint8_t *Loc = nullptr;
Rafael Espindola5805c4f2015-09-21 21:38:08 +0000126};
127
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000128// All output sections that are hadnled by the linker specially are
129// globally accessible. Writer initializes them, so don't use them
130// until Writer is initialized.
131template <class ELFT> struct Out {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000132 typedef typename ELFT::uint uintX_t;
133 typedef typename ELFT::Phdr Elf_Phdr;
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000134
135 static uint8_t First;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000136 static OutputSection<ELFT> *Bss;
Peter Collingbournefeb66292017-01-10 01:21:50 +0000137 static OutputSection<ELFT> *BssRelRo;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000138 static OutputSectionBase *Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000139 static uint8_t *OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000140 static PhdrEntry *TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000141 static OutputSectionBase *DebugInfo;
142 static OutputSectionBase *ElfHeader;
143 static OutputSectionBase *ProgramHeaders;
144 static OutputSectionBase *PreinitArray;
145 static OutputSectionBase *InitArray;
146 static OutputSectionBase *FiniArray;
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000147};
Rui Ueyamad888d102015-10-09 19:34:55 +0000148
Rafael Espindola72447082017-01-05 14:35:41 +0000149struct SectionKey {
George Rimar6892afa2016-07-12 09:49:43 +0000150 StringRef Name;
Rafael Espindola72447082017-01-05 14:35:41 +0000151 uint64_t Flags;
152 uint64_t Alignment;
George Rimar6892afa2016-07-12 09:49:43 +0000153};
Rafael Espindola63866282017-02-16 19:23:15 +0000154}
155}
156namespace llvm {
157template <> struct DenseMapInfo<lld::elf::SectionKey> {
158 static lld::elf::SectionKey getEmptyKey();
159 static lld::elf::SectionKey getTombstoneKey();
160 static unsigned getHashValue(const lld::elf::SectionKey &Val);
161 static bool isEqual(const lld::elf::SectionKey &LHS,
162 const lld::elf::SectionKey &RHS);
163};
164}
165namespace lld {
166namespace elf {
George Rimar6892afa2016-07-12 09:49:43 +0000167
168// This class knows how to create an output section for a given
169// input section. Output section type is determined by various
170// factors, including input section's sh_flags, sh_type and
171// linker scripts.
172template <class ELFT> class OutputSectionFactory {
173 typedef typename ELFT::Shdr Elf_Shdr;
174 typedef typename ELFT::uint uintX_t;
George Rimar6892afa2016-07-12 09:49:43 +0000175
176public:
Rafael Espindola82902742017-02-16 17:32:26 +0000177 OutputSectionFactory(std::vector<OutputSectionBase *> &OutputSections);
Rafael Espindolabd3ab092017-01-05 14:52:46 +0000178 ~OutputSectionFactory();
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000179 void addInputSec(InputSectionBase *IS, StringRef OutsecName);
Rafael Espindola82902742017-02-16 17:32:26 +0000180
George Rimar6892afa2016-07-12 09:49:43 +0000181private:
Rafael Espindola72447082017-01-05 14:35:41 +0000182 llvm::SmallDenseMap<SectionKey, OutputSectionBase *> Map;
Rafael Espindola82902742017-02-16 17:32:26 +0000183 std::vector<OutputSectionBase *> &OutputSections;
George Rimar6892afa2016-07-12 09:49:43 +0000184};
185
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000186template <class ELFT> uint64_t getHeaderSize() {
187 if (Config->OFormatBinary)
188 return 0;
Rafael Espindola04a2e342016-11-09 01:42:41 +0000189 return Out<ELFT>::ElfHeader->Size + Out<ELFT>::ProgramHeaders->Size;
Rafael Espindola0d4b6d52016-09-22 16:47:21 +0000190}
191
Rui Ueyamacfadbd92016-11-01 23:12:51 +0000192template <class ELFT> uint8_t Out<ELFT>::First;
Rafael Espindolad7a267b2015-11-03 22:01:20 +0000193template <class ELFT> OutputSection<ELFT> *Out<ELFT>::Bss;
Peter Collingbournefeb66292017-01-10 01:21:50 +0000194template <class ELFT> OutputSection<ELFT> *Out<ELFT>::BssRelRo;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000195template <class ELFT> OutputSectionBase *Out<ELFT>::Opd;
Hal Finkeldaedc122015-10-12 23:16:53 +0000196template <class ELFT> uint8_t *Out<ELFT>::OpdBuf;
Rafael Espindola17cb7c02016-12-19 17:01:01 +0000197template <class ELFT> PhdrEntry *Out<ELFT>::TlsPhdr;
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000198template <class ELFT> OutputSectionBase *Out<ELFT>::DebugInfo;
199template <class ELFT> OutputSectionBase *Out<ELFT>::ElfHeader;
200template <class ELFT> OutputSectionBase *Out<ELFT>::ProgramHeaders;
201template <class ELFT> OutputSectionBase *Out<ELFT>::PreinitArray;
202template <class ELFT> OutputSectionBase *Out<ELFT>::InitArray;
203template <class ELFT> OutputSectionBase *Out<ELFT>::FiniArray;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000204} // namespace elf
Eugene Zelenko6e43b492015-11-04 02:11:57 +0000205} // namespace lld
206
George Rimar6892afa2016-07-12 09:49:43 +0000207
208#endif