blob: 28c14b0ec79ca57e213f1c56ced567479c5b9543 [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 Ueyamab91bf1a2016-05-23 16:55:43 +000017#include "llvm/ADT/DenseSet.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000018#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000019#include "llvm/Object/ELF.h"
Rui Ueyama77f2a872016-11-18 05:05:43 +000020#include <mutex>
Michael J. Spencer84487f12015-07-24 21:03:07 +000021
22namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000023namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
Rafael Espindolae7553e42016-08-31 13:28:33 +000025class DefinedCommon;
Rafael Espindola38c67a22016-04-15 14:41:56 +000026class SymbolBody;
Rafael Espindola32aca872016-10-05 18:40:00 +000027struct SectionPiece;
Rafael Espindola38c67a22016-04-15 14:41:56 +000028
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000029template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000030template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000031template <class ELFT> class OutputSection;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000032class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000033
Eugene Leviant97403d12016-09-01 09:55:57 +000034// We need non-template input section class to store symbol layout
35// in linker script parser structures, where we do not have ELFT
36// template parameter. For each scripted output section symbol we
37// store pointer to preceding InputSectionData object or nullptr,
38// if symbol should be placed at the very beginning of the output
39// section
40class InputSectionData {
41public:
Eugene Leviant41ca3272016-11-10 09:48:29 +000042 enum Kind { Regular, EHFrame, Merge, Synthetic, };
Eugene Leviant97403d12016-09-01 09:55:57 +000043
44 // The garbage collector sets sections' Live bits.
45 // If GC is disabled, all sections are considered live by default.
Rafael Espindolac7e1e032016-09-12 13:13:53 +000046 InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
47 bool Compressed, bool Live)
Rui Ueyamaf94efdd2016-11-20 23:15:52 +000048 : SectionKind(SectionKind), Live(Live), Assigned(false),
49 Compressed(Compressed), Name(Name), Data(Data) {}
Eugene Leviant97403d12016-09-01 09:55:57 +000050
Rafael Espindola16853bb2016-09-08 12:33:41 +000051private:
52 unsigned SectionKind : 3;
Eugene Leviant97403d12016-09-01 09:55:57 +000053
Rafael Espindola16853bb2016-09-08 12:33:41 +000054public:
55 Kind kind() const { return (Kind)SectionKind; }
56
Rui Ueyamaf94efdd2016-11-20 23:15:52 +000057 unsigned Live : 1; // for garbage collection
58 unsigned Assigned : 1; // for linker script
59 unsigned Compressed : 1; // true if section data is compressed
Rafael Espindola16853bb2016-09-08 12:33:41 +000060 uint32_t Alignment;
Rafael Espindola042a3f22016-09-08 14:06:08 +000061 StringRef Name;
Rafael Espindolac7e1e032016-09-12 13:13:53 +000062 ArrayRef<uint8_t> Data;
63
Rafael Espindola0e090522016-10-26 00:54:03 +000064 template <typename T> llvm::ArrayRef<T> getDataAs() const {
65 size_t S = Data.size();
66 assert(S % sizeof(T) == 0);
67 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
68 }
69
Rafael Espindola0a758502016-09-07 20:41:19 +000070 std::vector<Relocation> Relocations;
Eugene Leviant97403d12016-09-01 09:55:57 +000071};
72
Rafael Espindola71675852015-09-22 00:16:19 +000073// This corresponds to a section of an input file.
Eugene Leviant97403d12016-09-01 09:55:57 +000074template <class ELFT> class InputSectionBase : public InputSectionData {
Rafael Espindolac159c962015-10-19 21:00:02 +000075protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000076 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000077 typedef typename ELFT::Rel Elf_Rel;
78 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000079 typedef typename ELFT::Shdr Elf_Shdr;
80 typedef typename ELFT::Sym Elf_Sym;
81 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000082
83 // The file this section is from.
84 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000085
Rafael Espindola1854a8e2016-10-26 12:36:56 +000086public:
Rafael Espindola0e090522016-10-26 00:54:03 +000087 // These corresponds to the fields in Elf_Shdr.
88 uintX_t Flags;
Eugene Leviantc4681202016-11-01 09:17:50 +000089 uintX_t Offset = 0;
Rafael Espindola0e090522016-10-26 00:54:03 +000090 uintX_t Entsize;
91 uint32_t Type;
92 uint32_t Link;
93 uint32_t Info;
94
Rafael Espindola042a3f22016-09-08 14:06:08 +000095 InputSectionBase()
Rafael Espindolac7e1e032016-09-12 13:13:53 +000096 : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false, false),
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000097 Repl(this) {
98 NumRelocations = 0;
99 AreRelocsRela = false;
100 }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000101
Rafael Espindolac159c962015-10-19 21:00:02 +0000102 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +0000103 StringRef Name, Kind SectionKind);
Rafael Espindola0e090522016-10-26 00:54:03 +0000104 InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type,
105 uintX_t Entsize, uint32_t Link, uint32_t Info,
106 uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
107 Kind SectionKind);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000108 OutputSectionBase *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000109
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000110 // Relocations that refer to this section.
111 const Elf_Rel *FirstRelocation = nullptr;
112 unsigned NumRelocations : 31;
113 unsigned AreRelocsRela : 1;
114 ArrayRef<Elf_Rel> rels() const {
115 assert(!AreRelocsRela);
116 return llvm::makeArrayRef(FirstRelocation, NumRelocations);
117 }
118 ArrayRef<Elf_Rela> relas() const {
119 assert(AreRelocsRela);
120 return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation),
121 NumRelocations);
122 }
123
Rui Ueyama0b289522016-02-25 18:43:51 +0000124 // This pointer points to the "real" instance of this instance.
125 // Usually Repl == this. However, if ICF merges two sections,
126 // Repl pointer of one section points to another section. So,
127 // if you need to get a pointer to this instance, do not use
128 // this but instead this->Repl.
129 InputSectionBase<ELFT> *Repl;
130
Rafael Espindola1a541122016-11-08 14:47:16 +0000131 // Returns the size of this section (even if this is a common or BSS.)
132 size_t getSize() const;
133
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000134 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindola77dbe9a2016-11-09 14:39:20 +0000135 llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000136 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Peter Smith0a259f32016-10-10 09:39:26 +0000137 InputSectionBase *getLinkOrderDep() const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000138 // Translate an offset in the input section to an offset in the output
139 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000140 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000141
George Rimar602fbee2016-06-24 11:18:44 +0000142 void uncompress();
143
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000144 // Returns a source location string. Used to construct an error message.
145 std::string getLocation(uintX_t Offset);
146
Rafael Espindola22ef9562016-04-13 01:40:19 +0000147 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama05384082016-10-12 22:36:31 +0000148
149private:
150 std::pair<ArrayRef<uint8_t>, uint64_t>
151 getElfCompressedData(ArrayRef<uint8_t> Data);
152
153 std::pair<ArrayRef<uint8_t>, uint64_t>
154 getRawCompressedData(ArrayRef<uint8_t> Data);
Rafael Espindolac159c962015-10-19 21:00:02 +0000155};
156
Rui Ueyama3ea87272016-05-22 00:13:04 +0000157// SectionPiece represents a piece of splittable section contents.
Rafael Espindola113860b2016-10-20 10:55:58 +0000158// We allocate a lot of these and binary search on them. This means that they
159// have to be as compact as possible, which is why we don't store the size (can
160// be found by looking at the next one) and put the hash in a side table.
Rui Ueyama3ea87272016-05-22 00:13:04 +0000161struct SectionPiece {
Rafael Espindola113860b2016-10-20 10:55:58 +0000162 SectionPiece(size_t Off, bool Live = false)
163 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000164
Rui Ueyama3ea87272016-05-22 00:13:04 +0000165 size_t InputOff;
Rafael Espindola113860b2016-10-20 10:55:58 +0000166 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
Hans Wennborg7314c482016-10-20 15:59:08 +0000167 size_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000168};
Rafael Espindola113860b2016-10-20 10:55:58 +0000169static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
170 "SectionPiece is too big");
Rui Ueyama3ea87272016-05-22 00:13:04 +0000171
Rafael Espindolac159c962015-10-19 21:00:02 +0000172// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000173template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000174 typedef typename ELFT::uint uintX_t;
175 typedef typename ELFT::Sym Elf_Sym;
176 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000177
178public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000179 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
180 StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000181 static bool classof(const InputSectionData *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000182 void splitIntoPieces();
183
184 // Mark the piece at a given offset live. Used by GC.
Rafael Espindola116d83f2016-10-19 23:13:40 +0000185 void markLiveAt(uintX_t Offset) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000186 assert(this->Flags & llvm::ELF::SHF_ALLOC);
Rafael Espindola116d83f2016-10-19 23:13:40 +0000187 LiveOffsets.insert(Offset);
188 }
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000189
190 // Translate an offset in the input section to an offset
191 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000192 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000193
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000194 // Splittable sections are handled as a sequence of data
195 // rather than a single large blob of data.
196 std::vector<SectionPiece> Pieces;
Rui Ueyama77f2a872016-11-18 05:05:43 +0000197 llvm::CachedHashStringRef getData(size_t Idx) const;
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000198
199 // Returns the SectionPiece at a given input section offset.
200 SectionPiece *getSectionPiece(uintX_t Offset);
201 const SectionPiece *getSectionPiece(uintX_t Offset) const;
202
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000203private:
Rui Ueyamae8a077b2016-11-26 15:15:11 +0000204 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
205 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000206
Rui Ueyama77f2a872016-11-18 05:05:43 +0000207 std::vector<uint32_t> Hashes;
208
209 mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
210 mutable std::once_flag InitOffsetMap;
211
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000212 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000213};
214
Rafael Espindola2deeb602016-07-21 20:18:30 +0000215struct EhSectionPiece : public SectionPiece {
Eugene Leviant531df4f2016-11-23 09:45:17 +0000216 EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size,
217 unsigned FirstRelocation)
218 : SectionPiece(Off, false), ID(ID), Size(Size),
Rafael Espindola32aca872016-10-05 18:40:00 +0000219 FirstRelocation(FirstRelocation) {}
Eugene Leviant531df4f2016-11-23 09:45:17 +0000220 InputSectionData *ID;
Rafael Espindola113860b2016-10-20 10:55:58 +0000221 uint32_t Size;
222 uint32_t size() const { return Size; }
223
Eugene Leviant531df4f2016-11-23 09:45:17 +0000224 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
Rafael Espindola2deeb602016-07-21 20:18:30 +0000225 unsigned FirstRelocation;
226};
227
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000228// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000229template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000230public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000231 typedef typename ELFT::Shdr Elf_Shdr;
232 typedef typename ELFT::uint uintX_t;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000233 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000234 static bool classof(const InputSectionData *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000235 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000236 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000237
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000238 // Splittable sections are handled as a sequence of data
239 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000240 std::vector<EhSectionPiece> Pieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000241};
242
Rafael Espindolac159c962015-10-19 21:00:02 +0000243// This corresponds to a non SHF_MERGE section of an input file.
244template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
245 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000246 typedef typename ELFT::Shdr Elf_Shdr;
247 typedef typename ELFT::Rela Elf_Rela;
248 typedef typename ELFT::Rel Elf_Rel;
249 typedef typename ELFT::Sym Elf_Sym;
250 typedef typename ELFT::uint uintX_t;
Eugene Leviant41ca3272016-11-10 09:48:29 +0000251 typedef InputSectionData::Kind Kind;
Rafael Espindolac159c962015-10-19 21:00:02 +0000252
253public:
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000254 InputSection();
Rafael Espindola0e090522016-10-26 00:54:03 +0000255 InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
Eugene Leviant41ca3272016-11-10 09:48:29 +0000256 ArrayRef<uint8_t> Data, StringRef Name,
257 Kind K = InputSectionData::Regular);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000258 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000259
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000260 static InputSection<ELFT> Discarded;
261
Rafael Espindolac159c962015-10-19 21:00:02 +0000262 // Write this section to a mmap'ed file, assuming Buf is pointing to
263 // beginning of the output section.
Rafael Espindola1a541122016-11-08 14:47:16 +0000264 void writeTo(uint8_t *Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000265
Rui Ueyamaedffd912015-10-14 21:00:23 +0000266 // The offset from beginning of the output sections this section was assigned
267 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000268 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000269
Peter Smith07606052016-10-10 10:10:27 +0000270 // InputSection that is dependent on us (reverse dependency for GC)
271 InputSectionBase<ELFT> *DependentSection = nullptr;
272
Rafael Espindola99558ef2016-10-26 18:44:57 +0000273 static bool classof(const InputSectionData *S);
George Rimar58941ee2016-02-25 08:23:37 +0000274
275 InputSectionBase<ELFT> *getRelocatedSection();
276
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000277 // Register thunk related to the symbol. When the section is written
278 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000279 // Now thunks is supported for MIPS and ARM target only.
280 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000281
282 // The offset of synthetic thunk code from beginning of this section.
283 uint64_t getThunkOff() const;
284
285 // Size of chunk with thunks code.
286 uint64_t getThunksSize() const;
287
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000288 template <class RelTy>
289 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
290
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000291 // Used by ICF.
292 uint64_t GroupId = 0;
Rui Ueyama0b289522016-02-25 18:43:51 +0000293
294 // Called by ICF to merge two input sections.
295 void replace(InputSection<ELFT> *Other);
296
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000297private:
298 template <class RelTy>
299 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000300
Peter Smithfb05cd92016-07-08 16:10:27 +0000301 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000302};
303
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000304template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
305
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000306template <class ELFT> std::string toString(const InputSectionBase<ELFT> *);
307
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000308} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000309} // namespace lld
310
311#endif