blob: fc7a7fb60973f39a6527074cd91d616482a0c25d [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,
Rui Ueyamac207a892016-12-20 05:47:55 +000048 bool Live)
49 : SectionKind(SectionKind), Live(Live), Assigned(false), Name(Name),
50 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
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()
Rui Ueyamac207a892016-12-20 05:47:55 +000096 : InputSectionData(Regular, "", ArrayRef<uint8_t>(), false), Repl(this) {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +000097 NumRelocations = 0;
98 AreRelocsRela = false;
99 }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000100
Rafael Espindolac159c962015-10-19 21:00:02 +0000101 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +0000102 StringRef Name, Kind SectionKind);
Rafael Espindola0e090522016-10-26 00:54:03 +0000103 InputSectionBase(ObjectFile<ELFT> *File, uintX_t Flags, uint32_t Type,
104 uintX_t Entsize, uint32_t Link, uint32_t Info,
105 uintX_t Addralign, ArrayRef<uint8_t> Data, StringRef Name,
106 Kind SectionKind);
Rafael Espindolae08e78d2016-11-09 23:23:45 +0000107 OutputSectionBase *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000108
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000109 // Relocations that refer to this section.
110 const Elf_Rel *FirstRelocation = nullptr;
111 unsigned NumRelocations : 31;
112 unsigned AreRelocsRela : 1;
113 ArrayRef<Elf_Rel> rels() const {
114 assert(!AreRelocsRela);
115 return llvm::makeArrayRef(FirstRelocation, NumRelocations);
116 }
117 ArrayRef<Elf_Rela> relas() const {
118 assert(AreRelocsRela);
119 return llvm::makeArrayRef(static_cast<const Elf_Rela *>(FirstRelocation),
120 NumRelocations);
121 }
122
Rui Ueyama0b289522016-02-25 18:43:51 +0000123 // This pointer points to the "real" instance of this instance.
124 // Usually Repl == this. However, if ICF merges two sections,
125 // Repl pointer of one section points to another section. So,
126 // if you need to get a pointer to this instance, do not use
127 // this but instead this->Repl.
128 InputSectionBase<ELFT> *Repl;
129
Rafael Espindola1a541122016-11-08 14:47:16 +0000130 // Returns the size of this section (even if this is a common or BSS.)
131 size_t getSize() const;
132
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000133 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindola77dbe9a2016-11-09 14:39:20 +0000134 llvm::object::ELFFile<ELFT> getObj() const { return File->getObj(); }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000135 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Peter Smith0a259f32016-10-10 09:39:26 +0000136 InputSectionBase *getLinkOrderDep() const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000137 // Translate an offset in the input section to an offset in the output
138 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000139 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000140
Rui Ueyamac207a892016-12-20 05:47:55 +0000141 // ELF supports ZLIB-compressed section.
142 // Returns true if the section is compressed.
143 bool isCompressed() const;
George Rimar602fbee2016-06-24 11:18:44 +0000144 void uncompress();
145
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000146 // Returns a source location string. Used to construct an error message.
147 std::string getLocation(uintX_t Offset);
148
Rafael Espindola22ef9562016-04-13 01:40:19 +0000149 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rui Ueyama05384082016-10-12 22:36:31 +0000150
151private:
152 std::pair<ArrayRef<uint8_t>, uint64_t>
153 getElfCompressedData(ArrayRef<uint8_t> Data);
154
155 std::pair<ArrayRef<uint8_t>, uint64_t>
156 getRawCompressedData(ArrayRef<uint8_t> Data);
Rafael Espindolac159c962015-10-19 21:00:02 +0000157};
158
Rui Ueyama3ea87272016-05-22 00:13:04 +0000159// SectionPiece represents a piece of splittable section contents.
Rafael Espindola113860b2016-10-20 10:55:58 +0000160// We allocate a lot of these and binary search on them. This means that they
161// have to be as compact as possible, which is why we don't store the size (can
162// be found by looking at the next one) and put the hash in a side table.
Rui Ueyama3ea87272016-05-22 00:13:04 +0000163struct SectionPiece {
Rafael Espindola113860b2016-10-20 10:55:58 +0000164 SectionPiece(size_t Off, bool Live = false)
165 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000166
Rui Ueyama3ea87272016-05-22 00:13:04 +0000167 size_t InputOff;
Rafael Espindola113860b2016-10-20 10:55:58 +0000168 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
Hans Wennborg7314c482016-10-20 15:59:08 +0000169 size_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000170};
Rafael Espindola113860b2016-10-20 10:55:58 +0000171static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
172 "SectionPiece is too big");
Rui Ueyama3ea87272016-05-22 00:13:04 +0000173
Rafael Espindolac159c962015-10-19 21:00:02 +0000174// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000175template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000176 typedef typename ELFT::uint uintX_t;
177 typedef typename ELFT::Sym Elf_Sym;
178 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000179
180public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000181 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
182 StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000183 static bool classof(const InputSectionData *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000184 void splitIntoPieces();
185
186 // Mark the piece at a given offset live. Used by GC.
Rafael Espindola116d83f2016-10-19 23:13:40 +0000187 void markLiveAt(uintX_t Offset) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000188 assert(this->Flags & llvm::ELF::SHF_ALLOC);
Rafael Espindola116d83f2016-10-19 23:13:40 +0000189 LiveOffsets.insert(Offset);
190 }
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000191
192 // Translate an offset in the input section to an offset
193 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000194 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000195
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000196 // Splittable sections are handled as a sequence of data
197 // rather than a single large blob of data.
198 std::vector<SectionPiece> Pieces;
Rui Ueyamac8e68842016-12-06 02:19:30 +0000199
200 // Returns I'th piece's data. This function is very hot when
201 // string merging is enabled, so we want to inline.
202 LLVM_ATTRIBUTE_ALWAYS_INLINE
203 llvm::CachedHashStringRef getData(size_t I) const {
204 size_t Begin = Pieces[I].InputOff;
205 size_t End;
206 if (Pieces.size() - 1 == I)
207 End = this->Data.size();
208 else
209 End = Pieces[I + 1].InputOff;
210
211 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
212 return {S, Hashes[I]};
213 }
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000214
215 // Returns the SectionPiece at a given input section offset.
216 SectionPiece *getSectionPiece(uintX_t Offset);
217 const SectionPiece *getSectionPiece(uintX_t Offset) const;
218
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000219private:
Rui Ueyamae8a077b2016-11-26 15:15:11 +0000220 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
221 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000222
Rui Ueyama77f2a872016-11-18 05:05:43 +0000223 std::vector<uint32_t> Hashes;
224
225 mutable llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
226 mutable std::once_flag InitOffsetMap;
227
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000228 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000229};
230
Rafael Espindola2deeb602016-07-21 20:18:30 +0000231struct EhSectionPiece : public SectionPiece {
Eugene Leviant531df4f2016-11-23 09:45:17 +0000232 EhSectionPiece(size_t Off, InputSectionData *ID, uint32_t Size,
233 unsigned FirstRelocation)
234 : SectionPiece(Off, false), ID(ID), Size(Size),
Rafael Espindola32aca872016-10-05 18:40:00 +0000235 FirstRelocation(FirstRelocation) {}
Eugene Leviant531df4f2016-11-23 09:45:17 +0000236 InputSectionData *ID;
Rafael Espindola113860b2016-10-20 10:55:58 +0000237 uint32_t Size;
238 uint32_t size() const { return Size; }
239
Eugene Leviant531df4f2016-11-23 09:45:17 +0000240 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
Rafael Espindola2deeb602016-07-21 20:18:30 +0000241 unsigned FirstRelocation;
242};
243
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000244// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000245template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000246public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000247 typedef typename ELFT::Shdr Elf_Shdr;
248 typedef typename ELFT::uint uintX_t;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000249 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindola99558ef2016-10-26 18:44:57 +0000250 static bool classof(const InputSectionData *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000251 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000252 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000253
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000254 // Splittable sections are handled as a sequence of data
255 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000256 std::vector<EhSectionPiece> Pieces;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000257};
258
Rafael Espindolac159c962015-10-19 21:00:02 +0000259// This corresponds to a non SHF_MERGE section of an input file.
260template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
261 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000262 typedef typename ELFT::Shdr Elf_Shdr;
263 typedef typename ELFT::Rela Elf_Rela;
264 typedef typename ELFT::Rel Elf_Rel;
265 typedef typename ELFT::Sym Elf_Sym;
266 typedef typename ELFT::uint uintX_t;
Eugene Leviant41ca3272016-11-10 09:48:29 +0000267 typedef InputSectionData::Kind Kind;
Rafael Espindolac159c962015-10-19 21:00:02 +0000268
269public:
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000270 InputSection();
Rafael Espindola0e090522016-10-26 00:54:03 +0000271 InputSection(uintX_t Flags, uint32_t Type, uintX_t Addralign,
Eugene Leviant41ca3272016-11-10 09:48:29 +0000272 ArrayRef<uint8_t> Data, StringRef Name,
273 Kind K = InputSectionData::Regular);
Rafael Espindola042a3f22016-09-08 14:06:08 +0000274 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000275
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000276 static InputSection<ELFT> Discarded;
277
Rafael Espindolac159c962015-10-19 21:00:02 +0000278 // Write this section to a mmap'ed file, assuming Buf is pointing to
279 // beginning of the output section.
Rafael Espindola1a541122016-11-08 14:47:16 +0000280 void writeTo(uint8_t *Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000281
Rui Ueyamaedffd912015-10-14 21:00:23 +0000282 // The offset from beginning of the output sections this section was assigned
283 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000284 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000285
Peter Smith07606052016-10-10 10:10:27 +0000286 // InputSection that is dependent on us (reverse dependency for GC)
287 InputSectionBase<ELFT> *DependentSection = nullptr;
288
Rafael Espindola99558ef2016-10-26 18:44:57 +0000289 static bool classof(const InputSectionData *S);
George Rimar58941ee2016-02-25 08:23:37 +0000290
291 InputSectionBase<ELFT> *getRelocatedSection();
292
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000293 // Register thunk related to the symbol. When the section is written
294 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000295 // Now thunks is supported for MIPS and ARM target only.
296 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000297
298 // The offset of synthetic thunk code from beginning of this section.
299 uint64_t getThunkOff() const;
300
301 // Size of chunk with thunks code.
302 uint64_t getThunksSize() const;
303
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000304 template <class RelTy>
305 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
306
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000307 // Used by ICF.
Rui Ueyamafcd3fa82016-12-05 18:11:35 +0000308 uint32_t Class[2] = {0, 0};
Rui Ueyama0b289522016-02-25 18:43:51 +0000309
310 // Called by ICF to merge two input sections.
311 void replace(InputSection<ELFT> *Other);
312
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000313private:
314 template <class RelTy>
315 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000316
Peter Smithfb05cd92016-07-08 16:10:27 +0000317 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000318};
319
Rafael Espindola6ff570a2016-11-09 16:55:07 +0000320template <class ELFT> InputSection<ELFT> InputSection<ELFT>::Discarded;
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000321} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000322
323template <class ELFT> std::string toString(const elf::InputSectionBase<ELFT> *);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000324} // namespace lld
325
326#endif