blob: 57458588b6902a5f77b6823b577ba7bd8ca74b91 [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
Rui Ueyama80474a22017-02-28 19:29:55 +000030class DefinedRegular;
Rafael Espindola5c02b742017-03-06 21:17:18 +000031class SyntheticSection;
Rafael Espindola66b4e212017-02-23 22:06:28 +000032template <class ELFT> class EhFrameSection;
Rafael Espindola6119b862017-03-06 20:23:56 +000033class MergeSyntheticSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000034template <class ELFT> class ObjectFile;
Rafael Espindola24e6f362017-02-24 15:07:30 +000035class OutputSection;
Michael J. Spencer84487f12015-07-24 21:03:07 +000036
Rafael Espindola5616adf2017-03-08 22:36:28 +000037// This is the base class of all sections that lld handles. Some are sections in
38// input files, some are sections in the produced output file and some exist
39// just as a convenience for implementing special ways of combining some
40// sections.
41class SectionBase {
Eugene Leviant97403d12016-09-01 09:55:57 +000042public:
Rafael Espindola5616adf2017-03-08 22:36:28 +000043 enum Kind { Regular, EHFrame, Merge, Synthetic, Output };
Eugene Leviant97403d12016-09-01 09:55:57 +000044
Rafael Espindola16853bb2016-09-08 12:33:41 +000045 Kind kind() const { return (Kind)SectionKind; }
Rafael Espindolac404d502017-02-23 02:32:18 +000046
47 StringRef Name;
48
49 unsigned SectionKind : 3;
50
Rafael Espindola5616adf2017-03-08 22:36:28 +000051 // The next two bit fields are only used by InputSectionBase, but we
52 // put them here so the struct packs better.
53
Rafael Espindolac404d502017-02-23 02:32:18 +000054 // The garbage collector sets sections' Live bits.
55 // If GC is disabled, all sections are considered live by default.
56 unsigned Live : 1; // for garbage collection
57 unsigned Assigned : 1; // for linker script
58
59 uint32_t Alignment;
60
Rafael Espindola0e090522016-10-26 00:54:03 +000061 // These corresponds to the fields in Elf_Shdr.
Rafael Espindolab4c9b812017-02-23 02:28:28 +000062 uint64_t Flags;
Rafael Espindolab4c9b812017-02-23 02:28:28 +000063 uint64_t Entsize;
Rafael Espindola0e090522016-10-26 00:54:03 +000064 uint32_t Type;
65 uint32_t Link;
66 uint32_t Info;
67
Rafael Espindola5616adf2017-03-08 22:36:28 +000068 OutputSection *getOutputSection();
69 const OutputSection *getOutputSection() const {
70 return const_cast<SectionBase *>(this)->getOutputSection();
71 }
72
73 // Translate an offset in the input section to an offset in the output
74 // section.
75 uint64_t getOffset(uint64_t Offset) const;
76
77 uint64_t getOffset(const DefinedRegular &Sym) const;
78
79protected:
80 SectionBase(Kind SectionKind, StringRef Name, uint64_t Flags,
81 uint64_t Entsize, uint64_t Alignment, uint32_t Type,
82 uint32_t Info, uint32_t Link)
83 : Name(Name), SectionKind(SectionKind), Alignment(Alignment),
84 Flags(Flags), Entsize(Entsize), Type(Type), Link(Link), Info(Info) {
85 Live = false;
86 Assigned = false;
87 }
88};
89
90// This corresponds to a section of an input file.
91class InputSectionBase : public SectionBase {
92public:
93 static bool classof(const SectionBase *S);
94
95 // The file this section is from.
96 InputFile *File;
97
98 ArrayRef<uint8_t> Data;
Rafael Espindola35ae65e2017-03-08 15:57:17 +000099 uint64_t getOffsetInFile() const;
Rafael Espindolabdd2e3e2017-03-08 14:12:52 +0000100
Rafael Espindola2a80e112017-03-06 22:36:19 +0000101 static InputSectionBase Discarded;
102
Rafael Espindola042a3f22016-09-08 14:06:08 +0000103 InputSectionBase()
Rafael Espindola5616adf2017-03-08 22:36:28 +0000104 : SectionBase(Regular, "", /*Flags*/ 0, /*Entsize*/ 0, /*Alignment*/ 0,
105 /*Type*/ 0,
106 /*Info*/ 0, /*Link*/ 0),
107 Repl(this) {
108 Live = false;
109 Assigned = false;
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000110 NumRelocations = 0;
111 AreRelocsRela = false;
112 }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000113
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000114 template <class ELFT>
115 InputSectionBase(ObjectFile<ELFT> *File, const typename ELFT::Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +0000116 StringRef Name, Kind SectionKind);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000117
118 InputSectionBase(InputFile *File, uint64_t Flags, uint32_t Type,
119 uint64_t Entsize, uint32_t Link, uint32_t Info,
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000120 uint32_t Alignment, ArrayRef<uint8_t> Data, StringRef Name,
Rafael Espindola0e090522016-10-26 00:54:03 +0000121 Kind SectionKind);
Rafael Espindola24e6f362017-02-24 15:07:30 +0000122 OutputSection *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +0000123
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000124 // Relocations that refer to this section.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000125 const void *FirstRelocation = nullptr;
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000126 unsigned NumRelocations : 31;
127 unsigned AreRelocsRela : 1;
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000128 template <class ELFT> ArrayRef<typename ELFT::Rel> rels() const {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000129 assert(!AreRelocsRela);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000130 return llvm::makeArrayRef(
131 static_cast<const typename ELFT::Rel *>(FirstRelocation),
132 NumRelocations);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000133 }
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000134 template <class ELFT> ArrayRef<typename ELFT::Rela> relas() const {
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000135 assert(AreRelocsRela);
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000136 return llvm::makeArrayRef(
137 static_cast<const typename ELFT::Rela *>(FirstRelocation),
138 NumRelocations);
Rafael Espindola9f0c4bb2016-11-10 14:53:24 +0000139 }
140
Rui Ueyama0b289522016-02-25 18:43:51 +0000141 // This pointer points to the "real" instance of this instance.
142 // Usually Repl == this. However, if ICF merges two sections,
143 // Repl pointer of one section points to another section. So,
144 // if you need to get a pointer to this instance, do not use
145 // this but instead this->Repl.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000146 InputSectionBase *Repl;
Rui Ueyama0b289522016-02-25 18:43:51 +0000147
George Rimar647c1682017-02-17 19:34:05 +0000148 // InputSections that are dependent on us (reverse dependency for GC)
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000149 llvm::TinyPtrVector<InputSectionBase *> DependentSections;
George Rimar647c1682017-02-17 19:34:05 +0000150
Rafael Espindola1a541122016-11-08 14:47:16 +0000151 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola76b6bd32017-03-08 15:44:30 +0000152 size_t getSize() const;
Rafael Espindola1a541122016-11-08 14:47:16 +0000153
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000154 template <class ELFT> ObjectFile<ELFT> *getFile() const;
155
156 template <class ELFT> llvm::object::ELFFile<ELFT> getObj() const {
157 return getFile<ELFT>()->getObj();
158 }
159
George Rimar9353e2d2017-03-21 08:29:48 +0000160 InputSectionBase *getLinkOrderDep() const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000161
George Rimar76e562a2017-03-21 09:08:58 +0000162 void uncompress();
George Rimar602fbee2016-06-24 11:18:44 +0000163
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000164 // Returns a source location string. Used to construct an error message.
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000165 template <class ELFT> std::string getLocation(uint64_t Offset);
Rui Ueyamab8760202017-03-30 19:13:47 +0000166 template <class ELFT> std::string getSrcMsg(uint64_t Offset);
167 template <class ELFT> std::string getObjMsg(uint64_t Offset);
Rui Ueyamada06bfb2016-11-25 18:51:53 +0000168
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000169 template <class ELFT> void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rafael Espindolac404d502017-02-23 02:32:18 +0000170
171 std::vector<Relocation> Relocations;
172
173 template <typename T> llvm::ArrayRef<T> getDataAs() const {
174 size_t S = Data.size();
175 assert(S % sizeof(T) == 0);
176 return llvm::makeArrayRef<T>((const T *)Data.data(), S / sizeof(T));
177 }
Rafael Espindolac159c962015-10-19 21:00:02 +0000178};
179
Rui Ueyama3ea87272016-05-22 00:13:04 +0000180// SectionPiece represents a piece of splittable section contents.
Rafael Espindola113860b2016-10-20 10:55:58 +0000181// We allocate a lot of these and binary search on them. This means that they
182// have to be as compact as possible, which is why we don't store the size (can
183// be found by looking at the next one) and put the hash in a side table.
Rui Ueyama3ea87272016-05-22 00:13:04 +0000184struct SectionPiece {
Rafael Espindola113860b2016-10-20 10:55:58 +0000185 SectionPiece(size_t Off, bool Live = false)
186 : InputOff(Off), OutputOff(-1), Live(Live || !Config->GcSections) {}
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000187
Rui Ueyama3ea87272016-05-22 00:13:04 +0000188 size_t InputOff;
Rafael Espindola113860b2016-10-20 10:55:58 +0000189 ssize_t OutputOff : 8 * sizeof(ssize_t) - 1;
Hans Wennborg7314c482016-10-20 15:59:08 +0000190 size_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000191};
Rafael Espindola113860b2016-10-20 10:55:58 +0000192static_assert(sizeof(SectionPiece) == 2 * sizeof(size_t),
193 "SectionPiece is too big");
Rui Ueyama3ea87272016-05-22 00:13:04 +0000194
Rafael Espindolac159c962015-10-19 21:00:02 +0000195// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6119b862017-03-06 20:23:56 +0000196class MergeInputSection : public InputSectionBase {
Rafael Espindolac159c962015-10-19 21:00:02 +0000197public:
Rafael Espindola6119b862017-03-06 20:23:56 +0000198 template <class ELFT>
199 MergeInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +0000200 StringRef Name);
Rafael Espindola5616adf2017-03-08 22:36:28 +0000201 static bool classof(const SectionBase *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000202 void splitIntoPieces();
203
204 // Mark the piece at a given offset live. Used by GC.
Rafael Espindola6119b862017-03-06 20:23:56 +0000205 void markLiveAt(uint64_t Offset) {
Rafael Espindola1854a8e2016-10-26 12:36:56 +0000206 assert(this->Flags & llvm::ELF::SHF_ALLOC);
Rafael Espindola116d83f2016-10-19 23:13:40 +0000207 LiveOffsets.insert(Offset);
208 }
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000209
210 // Translate an offset in the input section to an offset
211 // in the output section.
Rafael Espindola6119b862017-03-06 20:23:56 +0000212 uint64_t getOffset(uint64_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000213
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000214 // Splittable sections are handled as a sequence of data
215 // rather than a single large blob of data.
216 std::vector<SectionPiece> Pieces;
Rui Ueyamac8e68842016-12-06 02:19:30 +0000217
218 // Returns I'th piece's data. This function is very hot when
219 // string merging is enabled, so we want to inline.
220 LLVM_ATTRIBUTE_ALWAYS_INLINE
221 llvm::CachedHashStringRef getData(size_t I) const {
222 size_t Begin = Pieces[I].InputOff;
223 size_t End;
224 if (Pieces.size() - 1 == I)
225 End = this->Data.size();
226 else
227 End = Pieces[I + 1].InputOff;
228
229 StringRef S = {(const char *)(this->Data.data() + Begin), End - Begin};
230 return {S, Hashes[I]};
231 }
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000232
233 // Returns the SectionPiece at a given input section offset.
Rafael Espindola6119b862017-03-06 20:23:56 +0000234 SectionPiece *getSectionPiece(uint64_t Offset);
235 const SectionPiece *getSectionPiece(uint64_t Offset) const;
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000236
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000237 // MergeInputSections are aggregated to a synthetic input sections,
238 // and then added to an OutputSection. This pointer points to a
239 // synthetic MergeSyntheticSection which this section belongs to.
Rafael Espindola6119b862017-03-06 20:23:56 +0000240 MergeSyntheticSection *MergeSec = nullptr;
Rafael Espindola9e9754b2017-02-03 13:06:18 +0000241
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000242private:
Rui Ueyamae8a077b2016-11-26 15:15:11 +0000243 void splitStrings(ArrayRef<uint8_t> A, size_t Size);
244 void splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000245
Rui Ueyama77f2a872016-11-18 05:05:43 +0000246 std::vector<uint32_t> Hashes;
247
Rafael Espindola6119b862017-03-06 20:23:56 +0000248 mutable llvm::DenseMap<uint64_t, uint64_t> OffsetMap;
Rui Ueyama77f2a872016-11-18 05:05:43 +0000249 mutable std::once_flag InitOffsetMap;
250
Rafael Espindola6119b862017-03-06 20:23:56 +0000251 llvm::DenseSet<uint64_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000252};
253
Rafael Espindola2deeb602016-07-21 20:18:30 +0000254struct EhSectionPiece : public SectionPiece {
Rafael Espindolac404d502017-02-23 02:32:18 +0000255 EhSectionPiece(size_t Off, InputSectionBase *ID, uint32_t Size,
Eugene Leviant531df4f2016-11-23 09:45:17 +0000256 unsigned FirstRelocation)
257 : SectionPiece(Off, false), ID(ID), Size(Size),
Rafael Espindola32aca872016-10-05 18:40:00 +0000258 FirstRelocation(FirstRelocation) {}
Rafael Espindolac404d502017-02-23 02:32:18 +0000259 InputSectionBase *ID;
Rafael Espindola113860b2016-10-20 10:55:58 +0000260 uint32_t Size;
261 uint32_t size() const { return Size; }
262
Eugene Leviant531df4f2016-11-23 09:45:17 +0000263 ArrayRef<uint8_t> data() { return {ID->Data.data() + this->InputOff, Size}; }
Rafael Espindola2deeb602016-07-21 20:18:30 +0000264 unsigned FirstRelocation;
265};
266
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000267// This corresponds to a .eh_frame section of an input file.
Rafael Espindola5c02b742017-03-06 21:17:18 +0000268class EhInputSection : public InputSectionBase {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000269public:
Rafael Espindola5c02b742017-03-06 21:17:18 +0000270 template <class ELFT>
271 EhInputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
272 StringRef Name);
Rafael Espindola5616adf2017-03-08 22:36:28 +0000273 static bool classof(const SectionBase *S);
Rafael Espindola5c02b742017-03-06 21:17:18 +0000274 template <class ELFT> void split();
275 template <class ELFT, class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000276
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000277 // Splittable sections are handled as a sequence of data
278 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000279 std::vector<EhSectionPiece> Pieces;
Rafael Espindola5c02b742017-03-06 21:17:18 +0000280 SyntheticSection *EHSec = nullptr;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000281};
282
Rafael Espindola798ad9a2017-02-24 13:06:59 +0000283// This is a section that is added directly to an output section
284// instead of needing special combination via a synthetic section. This
285// includes all input sections with the exceptions of SHF_MERGE and
286// .eh_frame. It also includes the synthetic sections themselves.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000287class InputSection : public InputSectionBase {
Rafael Espindolac159c962015-10-19 21:00:02 +0000288public:
Rafael Espindolafcd208f2017-03-08 19:35:29 +0000289 InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment,
Rafael Espindolac404d502017-02-23 02:32:18 +0000290 ArrayRef<uint8_t> Data, StringRef Name, Kind K = Regular);
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000291 template <class ELFT>
292 InputSection(ObjectFile<ELFT> *F, const typename ELFT::Shdr *Header,
293 StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000294
295 // Write this section to a mmap'ed file, assuming Buf is pointing to
296 // beginning of the output section.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000297 template <class ELFT> void writeTo(uint8_t *Buf);
Rafael Espindolac159c962015-10-19 21:00:02 +0000298
Rui Ueyamaedffd912015-10-14 21:00:23 +0000299 // The offset from beginning of the output sections this section was assigned
300 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000301 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000302
Rafael Espindola5616adf2017-03-08 22:36:28 +0000303 static bool classof(const SectionBase *S);
George Rimar58941ee2016-02-25 08:23:37 +0000304
George Rimar1ec03e42017-03-21 09:13:27 +0000305 InputSectionBase *getRelocatedSection();
George Rimar58941ee2016-02-25 08:23:37 +0000306
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000307 template <class ELFT, class RelTy>
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000308 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
309
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000310 // Used by ICF.
Rui Ueyamafcd3fa82016-12-05 18:11:35 +0000311 uint32_t Class[2] = {0, 0};
Rui Ueyama0b289522016-02-25 18:43:51 +0000312
313 // Called by ICF to merge two input sections.
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000314 void replace(InputSection *Other);
Rui Ueyama0b289522016-02-25 18:43:51 +0000315
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000316private:
Rafael Espindola774ea7d2017-02-23 16:49:07 +0000317 template <class ELFT, class RelTy>
Rui Ueyamabd1f0632016-11-20 02:39:59 +0000318 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000319};
320
Rui Ueyama536a2672017-02-27 02:32:08 +0000321// The list of all input sections.
322extern std::vector<InputSectionBase *> InputSections;
323
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000324} // namespace elf
Rui Ueyamace039262017-01-06 10:04:08 +0000325
Rafael Espindolab4c9b812017-02-23 02:28:28 +0000326std::string toString(const elf::InputSectionBase *);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000327} // namespace lld
328
329#endif