blob: af22254ad055e8477e38dc6340c8119e3db482ff [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;
Michael J. Spencer84487f12015-07-24 21:03:07 +000031template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000032template <class ELFT> class OutputSection;
Rafael Espindolae08e78d2016-11-09 23:23:45 +000033class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034
Eugene Leviant97403d12016-09-01 09:55:57 +000035// We need non-template input section class to store symbol layout
36// in linker script parser structures, where we do not have ELFT
37// template parameter. For each scripted output section symbol we
38// store pointer to preceding InputSectionData object or nullptr,
39// if symbol should be placed at the very beginning of the output
40// section
41class InputSectionData {
42public:
Eugene Leviant41ca3272016-11-10 09:48:29 +000043 enum Kind { Regular, EHFrame, Merge, Synthetic, };
Eugene Leviant97403d12016-09-01 09:55:57 +000044
45 // The garbage collector sets sections' Live bits.
46 // If GC is disabled, all sections are considered live by default.
Rafael Espindolac7e1e032016-09-12 13:13:53 +000047 InputSectionData(Kind SectionKind, StringRef Name, ArrayRef<uint8_t> Data,
48 bool Compressed, bool Live)
Rui Ueyamaf94efdd2016-11-20 23:15:52 +000049 : SectionKind(SectionKind), Live(Live), Assigned(false),
50 Compressed(Compressed), Name(Name), Data(Data) {}
Eugene Leviant97403d12016-09-01 09:55:57 +000051
Rafael Espindola16853bb2016-09-08 12:33:41 +000052private:
53 unsigned SectionKind : 3;
Eugene Leviant97403d12016-09-01 09:55:57 +000054
Rafael Espindola16853bb2016-09-08 12:33:41 +000055public:
56 Kind kind() const { return (Kind)SectionKind; }
57
Rui Ueyamaf94efdd2016-11-20 23:15:52 +000058 unsigned Live : 1; // for garbage collection
59 unsigned Assigned : 1; // for linker script
60 unsigned Compressed : 1; // true if section data is compressed
Rafael Espindola16853bb2016-09-08 12:33:41 +000061 uint32_t Alignment;
Rafael Espindola042a3f22016-09-08 14:06:08 +000062 StringRef Name;
Rafael Espindolac7e1e032016-09-12 13:13:53 +000063 ArrayRef<uint8_t> Data;
64
Rafael Espindola0e090522016-10-26 00:54:03 +000065 template <typename T> llvm::ArrayRef<T> getDataAs() const {
66 size_t S = Data.size();
67 assert(S % sizeof(T) == 0);
68 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
69 }
70
Rafael Espindola0a758502016-09-07 20:41:19 +000071 std::vector<Relocation> Relocations;
Eugene Leviant97403d12016-09-01 09:55:57 +000072};
73
Rafael Espindola71675852015-09-22 00:16:19 +000074// This corresponds to a section of an input file.
Eugene Leviant97403d12016-09-01 09:55:57 +000075template <class ELFT> class InputSectionBase : public InputSectionData {
Rafael Espindolac159c962015-10-19 21:00:02 +000076protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000077 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000078 typedef typename ELFT::Rel Elf_Rel;
79 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000080 typedef typename ELFT::Shdr Elf_Shdr;
81 typedef typename ELFT::Sym Elf_Sym;
82 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000083
84 // The file this section is from.
85 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000086
Rafael Espindola1854a8e2016-10-26 12:36:56 +000087public:
Rafael Espindola0e090522016-10-26 00:54:03 +000088 // These corresponds to the fields in Elf_Shdr.
89 uintX_t Flags;
Eugene Leviantc4681202016-11-01 09:17:50 +000090 uintX_t Offset = 0;
Rafael Espindola0e090522016-10-26 00:54:03 +000091 uintX_t Entsize;
92 uint32_t Type;
93 uint32_t Link;
94 uint32_t Info;
95
Rafael Espindola042a3f22016-09-08 14:06:08 +000096 InputSectionBase()
Rafael Espindolac7e1e032016-09-12 13:13:53 +000097 : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false, false),
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000098 Repl(this) {
99 NumRelocations = 0;
100 AreRelocsRela = false;
101 }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000102
Rafael Espindolac159c962015-10-19 21:00:02 +0000103 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +0000104 StringRef Name, Kind SectionKind);
Rafael Espindola0e090522016-10-26 00:54:03 +0000105 InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type,
106 uintX_t Entsize, uint32_t Link, uint32_t Info,
107 uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
108 Kind SectionKind);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000109 OutputSectionBase *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000110
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000111 // Relocations that refer to this section.
112 const Elf_Rel *FirstRelocation = nullptr;
113 unsigned NumRelocations : 31;
114 unsigned AreRelocsRela : 1;
115 ArrayRef<Elf_Rel> rels() const {
116 assert(!AreRelocsRela);
117 return llvm::makeArrayRef(FirstRelocation, NumRelocations);
118 }
119 ArrayRef<Elf_Rela> relas() const {
120 assert(AreRelocsRela);
121 return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation),
122 NumRelocations);
123 }
124
Rui Ueyama0b289522016-02-25 18:43:51 +0000125 // This pointer points to the "real" instance of this instance.
126 // Usually Repl == this. However, if ICF merges two sections,
127 // Repl pointer of one section points to another section. So,
128 // if you need to get a pointer to this instance, do not use
129 // this but instead this->Repl.
130 InputSectionBase<ELFT> *Repl;
131
Rafael Espindola1a541122016-11-08 14:47:16 +0000132 // Returns the size of this section (even if this is a common or BSS.)
133 size_t getSize() const;
134
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000135 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindola77dbe9a2016-11-09 14:39:20 +0000136 llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000137 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Peter Smith0a259f32016-10-10 09:39:26 +0000138 InputSectionBase *getLinkOrderDep() const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000139 // Translate an offset in the input section to an offset in the output
140 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000141 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000142
George Rimar602fbee2016-06-24 11:18:44 +0000143 void uncompress();
144
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000145 // Returns a source location string. Used to construct an error message.
146 std::string getLocation(uintX_t Offset);
147
Rafael Espindola22ef9562016-04-13 01:40:19 +0000148 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama05384082016-10-12 22:36:31 +0000149
150private:
151 std::pair<ArrayRef<uint8_t>, uint64_t>
152 getElfCompressedData(ArrayRef<uint8_t> Data);
153
154 std::pair<ArrayRef<uint8_t>, uint64_t>
155 getRawCompressedData(ArrayRef<uint8_t> Data);
Rafael Espindolac159c962015-10-19 21:00:02 +0000156};
157
Rui Ueyama3ea87272016-05-22 00:13:04 +0000158// SectionPiece represents a piece of splittable section contents.
Rafael Espindola113860b2016-10-20 10:55:58 +0000159// We allocate a lot of these and binary search on them. This means that they
160// have to be as compact as possible, which is why we don't store the size (can
161// be found by looking at the next one) and put the hash in a side table.
Rui Ueyama3ea87272016-05-22 00:13:04 +0000162struct SectionPiece {
Rafael Espindola113860b2016-10-20 10:55:58 +0000163 SectionPiece(size_t Off, bool Live = false)
164 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000165
Rui Ueyama3ea87272016-05-22 00:13:04 +0000166 size_t InputOff;
Rafael Espindola113860b2016-10-20 10:55:58 +0000167 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
Hans Wennborg7314c482016-10-20 15:59:08 +0000168 size_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000169};
Rafael Espindola113860b2016-10-20 10:55:58 +0000170static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
171 "SectionPiece is too big");
Rui Ueyama3ea87272016-05-22 00:13:04 +0000172
Rafael Espindolac159c962015-10-19 21:00:02 +0000173// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000174template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000175 typedef typename ELFT::uint uintX_t;
176 typedef typename ELFT::Sym Elf_Sym;
177 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000178
179public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000180 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
181 StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000182 static bool classof(const InputSectionData *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000183 void splitIntoPieces();
184
185 // Mark the piece at a given offset live. Used by GC.
Rafael Espindola116d83f2016-10-19 23:13:40 +0000186 void markLiveAt(uintX_t Offset) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000187 assert(this->Flags & llvm::ELF::SHF_ALLOC);
Rafael Espindola116d83f2016-10-19 23:13:40 +0000188 LiveOffsets.insert(Offset);
189 }
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000190
191 // Translate an offset in the input section to an offset
192 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000193 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000194
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000195 // Splittable sections are handled as a sequence of data
196 // rather than a single large blob of data.
197 std::vector<SectionPiece> Pieces;
Rui Ueyamac8e68842016-12-06 02:19:30 +0000198
199 // Returns I'th piece's data. This function is very hot when
200 // string merging is enabled, so we want to inline.
201 LLVM_ATTRIBUTE_ALWAYS_INLINE
202 llvm::CachedHashStringRef getData(size_t I) const {
203 size_t Begin = Pieces[I].InputOff;
204 size_t End;
205 if (Pieces.size() - 1 == I)
206 End = this->Data.size();
207 else
208 End = Pieces[I + 1].InputOff;
209
210 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
211 return {S, Hashes[I]};
212 }
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000213
214 // Returns the SectionPiece at a given input section offset.
215 SectionPiece *getSectionPiece(uintX_t Offset);
216 const SectionPiece *getSectionPiece(uintX_t Offset) const;
217
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000218private:
Rui Ueyamae8a077b2016-11-26 15:15:11 +0000219 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
220 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000221
Rui Ueyama77f2a872016-11-18 05:05:43 +0000222 std::vector<uint32_t> Hashes;
223
224 mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
225 mutable std::once_flag InitOffsetMap;
226
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000227 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000228};
229
Rafael Espindola2deeb602016-07-21 20:18:30 +0000230struct EhSectionPiece : public SectionPiece {
Eugene Leviant531df4f2016-11-23 09:45:17 +0000231 EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size,
232 unsigned FirstRelocation)
233 : SectionPiece(Off, false), ID(ID), Size(Size),
Rafael Espindola32aca872016-10-05 18:40:00 +0000234 FirstRelocation(FirstRelocation) {}
Eugene Leviant531df4f2016-11-23 09:45:17 +0000235 InputSectionData *ID;
Rafael Espindola113860b2016-10-20 10:55:58 +0000236 uint32_t Size;
237 uint32_t size() const { return Size; }
238
Eugene Leviant531df4f2016-11-23 09:45:17 +0000239 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
Rafael Espindola2deeb602016-07-21 20:18:30 +0000240 unsigned FirstRelocation;
241};
242
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000243// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000244template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000245public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000246 typedef typename ELFT::Shdr Elf_Shdr;
247 typedef typename ELFT::uint uintX_t;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000248 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000249 static bool classof(const InputSectionData *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000250 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000251 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000252
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000253 // Splittable sections are handled as a sequence of data
254 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000255 std::vector<EhSectionPiece> Pieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000256};
257
Rafael Espindolac159c962015-10-19 21:00:02 +0000258// This corresponds to a non SHF_MERGE section of an input file.
259template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
260 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000261 typedef typename ELFT::Shdr Elf_Shdr;
262 typedef typename ELFT::Rela Elf_Rela;
263 typedef typename ELFT::Rel Elf_Rel;
264 typedef typename ELFT::Sym Elf_Sym;
265 typedef typename ELFT::uint uintX_t;
Eugene Leviant41ca3272016-11-10 09:48:29 +0000266 typedef InputSectionData::Kind Kind;
Rafael Espindolac159c962015-10-19 21:00:02 +0000267
268public:
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000269 InputSection();
Rafael Espindola0e090522016-10-26 00:54:03 +0000270 InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
Eugene Leviant41ca3272016-11-10 09:48:29 +0000271 ArrayRef<uint8_t> Data, StringRef Name,
272 Kind K = InputSectionData::Regular);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000273 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000274
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000275 static InputSection<ELFT> Discarded;
276
Rafael Espindolac159c962015-10-19 21:00:02 +0000277 // Write this section to a mmap'ed file, assuming Buf is pointing to
278 // beginning of the output section.
Rafael Espindola1a541122016-11-08 14:47:16 +0000279 void writeTo(uint8_t *Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000280
Rui Ueyamaedffd912015-10-14 21:00:23 +0000281 // The offset from beginning of the output sections this section was assigned
282 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000283 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000284
Peter Smith07606052016-10-10 10:10:27 +0000285 // InputSection that is dependent on us (reverse dependency for GC)
286 InputSectionBase<ELFT> *DependentSection = nullptr;
287
Rafael Espindola99558ef2016-10-26 18:44:57 +0000288 static bool classof(const InputSectionData *S);
George Rimar58941ee2016-02-25 08:23:37 +0000289
290 InputSectionBase<ELFT> *getRelocatedSection();
291
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000292 // Register thunk related to the symbol. When the section is written
293 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000294 // Now thunks is supported for MIPS and ARM target only.
295 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000296
297 // The offset of synthetic thunk code from beginning of this section.
298 uint64_t getThunkOff() const;
299
300 // Size of chunk with thunks code.
301 uint64_t getThunksSize() const;
302
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000303 template <class RelTy>
304 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
305
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000306 // Used by ICF.
Rui Ueyamafcd3fa82016-12-05 18:11:35 +0000307 uint32_t Class[2] = {0, 0};
Rui Ueyama0b289522016-02-25 18:43:51 +0000308
309 // Called by ICF to merge two input sections.
310 void replace(InputSection<ELFT> *Other);
311
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000312private:
313 template <class RelTy>
314 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000315
Peter Smithfb05cd92016-07-08 16:10:27 +0000316 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000317};
318
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000319template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
320
Rui Ueyama3fc0f7e2016-11-23 18:07:33 +0000321template <class ELFT> std::string toString(const InputSectionBase<ELFT> *);
322
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000323} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000324} // namespace lld
325
326#endif