blob: 9e5b09070cf000e429e674bb3ec5d6150ace838f [file] [log] [blame]
Rafael Espindola9d06ab62015-09-22 00:01:39 +00001//===- InputSection.h -------------------------------------------*- C++ -*-===//
Michael J. Spencer84487f12015-07-24 21:03:07 +00002//
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
Rafael Espindola9d06ab62015-09-22 00:01:39 +000010#ifndef LLD_ELF_INPUT_SECTION_H
11#define LLD_ELF_INPUT_SECTION_H
Michael J. Spencer84487f12015-07-24 21:03:07 +000012
Rui Ueyamac4aaed92015-10-22 18:49:53 +000013#include "Config.h"
Rui Ueyama0fcdc732016-05-24 20:24:43 +000014#include "Relocations.h"
Peter Smithfb05cd92016-07-08 16:10:27 +000015#include "Thunks.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "lld/Core/LLVM.h"
Rui Ueyama8f687f72016-12-19 03:14:16 +000017#include "llvm/ADT/CachedHashString.h"
Rui Ueyamab91bf1a2016-05-23 16:55:43 +000018#include "llvm/ADT/DenseSet.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000019#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000020#include "llvm/Object/ELF.h"
Rui Ueyama77f2a872016-11-18 05:05:43 +000021#include <mutex>
Michael J. Spencer84487f12015-07-24 21:03:07 +000022
23namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000024namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000025
Rafael Espindolae7553e42016-08-31 13:28:33 +000026class DefinedCommon;
Rafael Espindola38c67a22016-04-15 14:41:56 +000027class SymbolBody;
Rafael Espindola32aca872016-10-05 18:40:00 +000028struct SectionPiece;
Rafael Espindola38c67a22016-04-15 14:41:56 +000029
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000030template <class ELFT> class DefinedRegular;
Rafael Espindola9e9754b2017-02-03 13:06:18 +000031template <class ELFT> class MergeSyntheticSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000033template <class ELFT> class OutputSection;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000034class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000035
Rafael Espindolac404d502017-02-23 02:32:18 +000036// This corresponds to a section of an input file.
37class InputSectionBase {
Eugene Leviant97403d12016-09-01 09:55:57 +000038public:
Eugene Leviant41ca3272016-11-10 09:48:29 +000039 enum Kind { Regular, EHFrame, Merge, Synthetic, };
Eugene Leviant97403d12016-09-01 09:55:57 +000040
Rafael Espindola16853bb2016-09-08 12:33:41 +000041 Kind kind() const { return (Kind)SectionKind; }
Rafael Espindolab4c9b812017-02-23 02:28:28 +000042 // The file this section is from.
43 InputFile *File;
44
Rafael Espindolac404d502017-02-23 02:32:18 +000045 ArrayRef<uint8_t> Data;
46
47 StringRef Name;
48
49 unsigned SectionKind : 3;
50
51 // The garbage collector sets sections' Live bits.
52 // If GC is disabled, all sections are considered live by default.
53 unsigned Live : 1; // for garbage collection
54 unsigned Assigned : 1; // for linker script
55
56 uint32_t Alignment;
57
Rafael Espindola0e090522016-10-26 00:54:03 +000058 // These corresponds to the fields in Elf_Shdr.
Rafael Espindolab4c9b812017-02-23 02:28:28 +000059 uint64_t Flags;
60 uint64_t Offset = 0;
61 uint64_t Entsize;
Rafael Espindola0e090522016-10-26 00:54:03 +000062 uint32_t Type;
63 uint32_t Link;
64 uint32_t Info;
65
Rafael Espindola042a3f22016-09-08 14:06:08 +000066 InputSectionBase()
Rafael Espindolac404d502017-02-23 02:32:18 +000067 : SectionKind(Regular), Live(false), Assigned(false), Repl(this) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000068 NumRelocations = 0;
69 AreRelocsRela = false;
70 }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000071
Rafael Espindolab4c9b812017-02-23 02:28:28 +000072 template <class ELFT>
73 InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +000074 StringRef Name, Kind SectionKind);
Rafael Espindolab4c9b812017-02-23 02:28:28 +000075
76 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type,
77 uint64_t Entsize, uint32_t Link, uint32_t Info,
78 uint64_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
Rafael Espindola0e090522016-10-26 00:54:03 +000079 Kind SectionKind);
Rafael Espindolae08e78d2016-11-09 23:23:45 +000080 OutputSectionBase *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000081
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000082 // Relocations that refer to this section.
Rafael Espindolab4c9b812017-02-23 02:28:28 +000083 const void *FirstRelocation = nullptr;
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000084 unsigned NumRelocations : 31;
85 unsigned AreRelocsRela : 1;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000086 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000087 assert(!AreRelocsRela);
Rafael Espindolab4c9b812017-02-23 02:28:28 +000088 return llvm::makeArrayRef(
89 static_cast<const typename ELFT::Rel *>(FirstRelocation),
90 NumRelocations);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000091 }
Rafael Espindolab4c9b812017-02-23 02:28:28 +000092 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000093 assert(AreRelocsRela);
Rafael Espindolab4c9b812017-02-23 02:28:28 +000094 return llvm::makeArrayRef(
95 static_cast<const typename ELFT::Rela *>(FirstRelocation),
96 NumRelocations);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000097 }
98
Rui Ueyama0b289522016-02-25 18:43:51 +000099 // This pointer points to the "real" instance of this instance.
100 // Usually Repl == this. However, if ICF merges two sections,
101 // Repl pointer of one section points to another section. So,
102 // if you need to get a pointer to this instance, do not use
103 // this but instead this->Repl.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000104 InputSectionBase *Repl;
Rui Ueyama0b289522016-02-25 18:43:51 +0000105
George Rimar647c1682017-02-17 19:34:05 +0000106 // InputSections that are dependent on us (reverse dependency for GC)
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000107 llvm::TinyPtrVector<InputSectionBase *> DependentSections;
George Rimar647c1682017-02-17 19:34:05 +0000108
Rafael Espindola1a541122016-11-08 14:47:16 +0000109 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000110 template <class ELFT> size_t getSize() const;
Rafael Espindola1a541122016-11-08 14:47:16 +0000111
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000112 template <class ELFT> OutputSectionBase *getOutputSection() const;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000113
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000114 template <class ELFT> ObjectFile<ELFT> *getFile() const;
115
116 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const {
117 return getFile<ELFT>()->getObj();
118 }
119
120 template <class ELFT>
121 uint64_t getOffset(const DefinedRegular<ELFT> &Sym) const;
122
123 template <class ELFT> InputSectionBase *getLinkOrderDep() const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000124 // Translate an offset in the input section to an offset in the output
125 // section.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000126 template <class ELFT> uint64_t getOffset(uint64_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000127
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000128 template <class ELFT> void uncompress();
George Rimar602fbee2016-06-24 11:18:44 +0000129
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000130 // Returns a source location string. Used to construct an error message.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000131 template <class ELFT> std::string getLocation(uint64_t Offset);
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000132
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000133 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rafael Espindolac404d502017-02-23 02:32:18 +0000134
135 std::vector<Relocation> Relocations;
136
137 template <typename T> llvm::ArrayRef<T> getDataAs() const {
138 size_t S = Data.size();
139 assert(S % sizeof(T) == 0);
140 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
141 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000142};
143
Rui Ueyama3ea87272016-05-22 00:13:04 +0000144// SectionPiece represents a piece of splittable section contents.
Rafael Espindola113860b2016-10-20 10:55:58 +0000145// We allocate a lot of these and binary search on them. This means that they
146// have to be as compact as possible, which is why we don't store the size (can
147// be found by looking at the next one) and put the hash in a side table.
Rui Ueyama3ea87272016-05-22 00:13:04 +0000148struct SectionPiece {
Rafael Espindola113860b2016-10-20 10:55:58 +0000149 SectionPiece(size_t Off, bool Live = false)
150 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000151
Rui Ueyama3ea87272016-05-22 00:13:04 +0000152 size_t InputOff;
Rafael Espindola113860b2016-10-20 10:55:58 +0000153 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
Hans Wennborg7314c482016-10-20 15:59:08 +0000154 size_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000155};
Rafael Espindola113860b2016-10-20 10:55:58 +0000156static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
157 "SectionPiece is too big");
Rui Ueyama3ea87272016-05-22 00:13:04 +0000158
Rafael Espindolac159c962015-10-19 21:00:02 +0000159// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000160template <class ELFT> class MergeInputSection : public InputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000161 typedef typename ELFT::uint uintX_t;
162 typedef typename ELFT::Sym Elf_Sym;
163 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000164
165public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000166 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
167 StringRef Name);
Rafael Espindolac404d502017-02-23 02:32:18 +0000168 static bool classof(const InputSectionBase *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000169 void splitIntoPieces();
170
171 // Mark the piece at a given offset live. Used by GC.
Rafael Espindola116d83f2016-10-19 23:13:40 +0000172 void markLiveAt(uintX_t Offset) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000173 assert(this->Flags & llvm::ELF::SHF_ALLOC);
Rafael Espindola116d83f2016-10-19 23:13:40 +0000174 LiveOffsets.insert(Offset);
175 }
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000176
177 // Translate an offset in the input section to an offset
178 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000179 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000180
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000181 // Splittable sections are handled as a sequence of data
182 // rather than a single large blob of data.
183 std::vector<SectionPiece> Pieces;
Rui Ueyamac8e68842016-12-06 02:19:30 +0000184
185 // Returns I'th piece's data. This function is very hot when
186 // string merging is enabled, so we want to inline.
187 LLVM_ATTRIBUTE_ALWAYS_INLINE
188 llvm::CachedHashStringRef getData(size_t I) const {
189 size_t Begin = Pieces[I].InputOff;
190 size_t End;
191 if (Pieces.size() - 1 == I)
192 End = this->Data.size();
193 else
194 End = Pieces[I + 1].InputOff;
195
196 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
197 return {S, Hashes[I]};
198 }
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000199
200 // Returns the SectionPiece at a given input section offset.
201 SectionPiece *getSectionPiece(uintX_t Offset);
202 const SectionPiece *getSectionPiece(uintX_t Offset) const;
203
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000204 // MergeInputSections are aggregated to a synthetic input sections,
205 // and then added to an OutputSection. This pointer points to a
206 // synthetic MergeSyntheticSection which this section belongs to.
207 MergeSyntheticSection<ELFT> *MergeSec = nullptr;
208
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000209private:
Rui Ueyamae8a077b2016-11-26 15:15:11 +0000210 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
211 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000212
Rui Ueyama77f2a872016-11-18 05:05:43 +0000213 std::vector<uint32_t> Hashes;
214
215 mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
216 mutable std::once_flag InitOffsetMap;
217
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000218 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000219};
220
Rafael Espindola2deeb602016-07-21 20:18:30 +0000221struct EhSectionPiece : public SectionPiece {
Rafael Espindolac404d502017-02-23 02:32:18 +0000222 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
Eugene Leviant531df4f2016-11-23 09:45:17 +0000223 unsigned FirstRelocation)
224 : SectionPiece(Off, false), ID(ID), Size(Size),
Rafael Espindola32aca872016-10-05 18:40:00 +0000225 FirstRelocation(FirstRelocation) {}
Rafael Espindolac404d502017-02-23 02:32:18 +0000226 InputSectionBase *ID;
Rafael Espindola113860b2016-10-20 10:55:58 +0000227 uint32_t Size;
228 uint32_t size() const { return Size; }
229
Eugene Leviant531df4f2016-11-23 09:45:17 +0000230 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
Rafael Espindola2deeb602016-07-21 20:18:30 +0000231 unsigned FirstRelocation;
232};
233
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000234// This corresponds to a .eh_frame section of an input file.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000235template <class ELFT> class EhInputSection : public InputSectionBase {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000236public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000237 typedef typename ELFT::Shdr Elf_Shdr;
238 typedef typename ELFT::uint uintX_t;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000239 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac404d502017-02-23 02:32:18 +0000240 static bool classof(const InputSectionBase *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000241 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000242 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000243
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000244 // Splittable sections are handled as a sequence of data
245 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000246 std::vector<EhSectionPiece> Pieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000247};
248
Rafael Espindolac159c962015-10-19 21:00:02 +0000249// This corresponds to a non SHF_MERGE section of an input file.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000250template <class ELFT> class InputSection : public InputSectionBase {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000251 typedef typename ELFT::Shdr Elf_Shdr;
252 typedef typename ELFT::Rela Elf_Rela;
253 typedef typename ELFT::Rel Elf_Rel;
254 typedef typename ELFT::Sym Elf_Sym;
255 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000256
257public:
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000258 InputSection();
Rafael Espindola0e090522016-10-26 00:54:03 +0000259 InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
Rafael Espindolac404d502017-02-23 02:32:18 +0000260 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000261 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000262
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000263 static InputSection<ELFT> Discarded;
264
Rafael Espindolac159c962015-10-19 21:00:02 +0000265 // Write this section to a mmap'ed file, assuming Buf is pointing to
266 // beginning of the output section.
Rafael Espindola1a541122016-11-08 14:47:16 +0000267 void writeTo(uint8_t *Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000268
Rui Ueyamaedffd912015-10-14 21:00:23 +0000269 // The offset from beginning of the output sections this section was assigned
270 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000271 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000272
Rafael Espindolac404d502017-02-23 02:32:18 +0000273 static bool classof(const InputSectionBase *S);
George Rimar58941ee2016-02-25 08:23:37 +0000274
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000275 InputSectionBase *getRelocatedSection();
George Rimar58941ee2016-02-25 08:23:37 +0000276
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000277 template <class RelTy>
278 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
279
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000280 // Used by ICF.
Rui Ueyamafcd3fa82016-12-05 18:11:35 +0000281 uint32_t Class[2] = {0, 0};
Rui Ueyama0b289522016-02-25 18:43:51 +0000282
283 // Called by ICF to merge two input sections.
284 void replace(InputSection<ELFT> *Other);
285
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000286private:
287 template <class RelTy>
288 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000289};
290
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000291template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000292} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000293
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000294std::string toString(const elf::InputSectionBase *);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000295} // namespace lld
296
297#endif