blob: 23e3a547ebb88dfe9c8b3d71d87c2ce4b03f5b4d [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"
20
21namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000022namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000023
Rafael Espindolae7553e42016-08-31 13:28:33 +000024class DefinedCommon;
Rafael Espindola38c67a22016-04-15 14:41:56 +000025class SymbolBody;
26
Rui Ueyama0b289522016-02-25 18:43:51 +000027template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000028template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000029template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000030template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000031template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000032
Eugene Leviant97403d12016-09-01 09:55:57 +000033// We need non-template input section class to store symbol layout
34// in linker script parser structures, where we do not have ELFT
35// template parameter. For each scripted output section symbol we
36// store pointer to preceding InputSectionData object or nullptr,
37// if symbol should be placed at the very beginning of the output
38// section
39class InputSectionData {
40public:
41 enum Kind { Regular, EHFrame, Merge, MipsReginfo, MipsOptions, MipsAbiFlags };
42
43 // The garbage collector sets sections' Live bits.
44 // If GC is disabled, all sections are considered live by default.
Rafael Espindola042a3f22016-09-08 14:06:08 +000045 InputSectionData(Kind SectionKind, StringRef Name, bool Compressed, bool Live)
46 : SectionKind(SectionKind), Live(Live), Compressed(Compressed),
47 Name(Name) {}
Eugene Leviant97403d12016-09-01 09:55:57 +000048
Rafael Espindola16853bb2016-09-08 12:33:41 +000049private:
50 unsigned SectionKind : 3;
Eugene Leviant97403d12016-09-01 09:55:57 +000051
Rafael Espindola16853bb2016-09-08 12:33:41 +000052public:
53 Kind kind() const { return (Kind)SectionKind; }
54
55 // Used for garbage collection.
56 unsigned Live : 1;
57
58 unsigned Compressed : 1;
59
60 uint32_t Alignment;
61
Rafael Espindola042a3f22016-09-08 14:06:08 +000062 StringRef Name;
63
Rafael Espindola54f16142016-09-12 13:06:10 +000064 // If a section is compressed, this has the uncompressed section data.
65 std::unique_ptr<char[]> UncompressedData;
66 size_t UncompressedDataSize = 0;
Rafael Espindola0a758502016-09-07 20:41:19 +000067
68 std::vector<Relocation> Relocations;
Eugene Leviant97403d12016-09-01 09:55:57 +000069};
70
Rafael Espindola71675852015-09-22 00:16:19 +000071// This corresponds to a section of an input file.
Eugene Leviant97403d12016-09-01 09:55:57 +000072template <class ELFT> class InputSectionBase : public InputSectionData {
Rafael Espindolac159c962015-10-19 21:00:02 +000073protected:
Rui Ueyama1d12ac12016-07-07 03:55:55 +000074 typedef typename ELFT::Chdr Elf_Chdr;
Rafael Espindola197d6a82016-04-22 16:39:59 +000075 typedef typename ELFT::Rel Elf_Rel;
76 typedef typename ELFT::Rela Elf_Rela;
Rui Ueyama9328b2c2016-03-14 23:16:09 +000077 typedef typename ELFT::Shdr Elf_Shdr;
78 typedef typename ELFT::Sym Elf_Sym;
79 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000080 const Elf_Shdr *Header;
81
82 // The file this section is from.
83 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000084
85public:
Rafael Espindola042a3f22016-09-08 14:06:08 +000086 InputSectionBase()
87 : InputSectionData(Regular, "", false, false), Repl(this) {}
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000088
Rafael Espindolac159c962015-10-19 21:00:02 +000089 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
Rafael Espindola042a3f22016-09-08 14:06:08 +000090 StringRef Name, Kind SectionKind);
Rafael Espindolac159c962015-10-19 21:00:02 +000091 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000092
Rui Ueyama0b289522016-02-25 18:43:51 +000093 // This pointer points to the "real" instance of this instance.
94 // Usually Repl == this. However, if ICF merges two sections,
95 // Repl pointer of one section points to another section. So,
96 // if you need to get a pointer to this instance, do not use
97 // this but instead this->Repl.
98 InputSectionBase<ELFT> *Repl;
99
Rafael Espindola71675852015-09-22 00:16:19 +0000100 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000101 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000102
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000103 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +0000104
Michael J. Spencer8039dae22015-07-29 00:30:10 +0000105 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +0000106 ObjectFile<ELFT> *getFile() const { return File; }
Rui Ueyama809d8e22016-06-23 04:33:42 +0000107 uintX_t getOffset(const DefinedRegular<ELFT> &Sym) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000108
109 // Translate an offset in the input section to an offset in the output
110 // section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000111 uintX_t getOffset(uintX_t Offset) const;
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +0000112
Rafael Espindolac159c962015-10-19 21:00:02 +0000113 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +0000114
George Rimar602fbee2016-06-24 11:18:44 +0000115 void uncompress();
116
Rafael Espindola22ef9562016-04-13 01:40:19 +0000117 void relocate(uint8_t *Buf, uint8_t *BufEnd);
Rafael Espindolac159c962015-10-19 21:00:02 +0000118};
119
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +0000120template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +0000121
Rui Ueyama3ea87272016-05-22 00:13:04 +0000122// SectionPiece represents a piece of splittable section contents.
123struct SectionPiece {
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000124 SectionPiece(size_t Off, ArrayRef<uint8_t> Data)
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000125 : InputOff(Off), Data((const uint8_t *)Data.data()), Size(Data.size()),
Rui Ueyamad8849272016-05-25 16:37:01 +0000126 Live(!Config->GcSections) {}
127
128 ArrayRef<uint8_t> data() { return {Data, Size}; }
129 size_t size() const { return Size; }
Rui Ueyama34dc99e2016-05-22 01:15:32 +0000130
Rui Ueyama3ea87272016-05-22 00:13:04 +0000131 size_t InputOff;
132 size_t OutputOff = -1;
Rui Ueyamad8849272016-05-25 16:37:01 +0000133
134private:
135 // We use bitfields because SplitInputSection is accessed by
136 // std::upper_bound very often.
137 // We want to save bits to make it cache friendly.
Davide Italianoe6c8fa42016-05-28 23:27:38 +0000138 const uint8_t *Data;
Rui Ueyamad8849272016-05-25 16:37:01 +0000139 uint32_t Size : 31;
140
141public:
142 uint32_t Live : 1;
Rui Ueyama3ea87272016-05-22 00:13:04 +0000143};
144
Rafael Espindolac159c962015-10-19 21:00:02 +0000145// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000146template <class ELFT> class MergeInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147 typedef typename ELFT::uint uintX_t;
148 typedef typename ELFT::Sym Elf_Sym;
149 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000150
151public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000152 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header,
153 StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000154 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000155 void splitIntoPieces();
156
157 // Mark the piece at a given offset live. Used by GC.
158 void markLiveAt(uintX_t Offset) { LiveOffsets.insert(Offset); }
159
160 // Translate an offset in the input section to an offset
161 // in the output section.
Rui Ueyama809d8e22016-06-23 04:33:42 +0000162 uintX_t getOffset(uintX_t Offset) const;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000163
Rui Ueyama406b4692016-05-27 14:39:13 +0000164 void finalizePieces();
165
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000166 // Splittable sections are handled as a sequence of data
167 // rather than a single large blob of data.
168 std::vector<SectionPiece> Pieces;
169
170 // Returns the SectionPiece at a given input section offset.
171 SectionPiece *getSectionPiece(uintX_t Offset);
172 const SectionPiece *getSectionPiece(uintX_t Offset) const;
173
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000174private:
Rui Ueyamad6bd1372016-08-03 04:39:42 +0000175 std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> A, size_t Size);
176 std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> A, size_t Size);
177
Rui Ueyama406b4692016-05-27 14:39:13 +0000178 llvm::DenseMap<uintX_t, uintX_t> OffsetMap;
Rui Ueyamab91bf1a2016-05-23 16:55:43 +0000179 llvm::DenseSet<uintX_t> LiveOffsets;
Rafael Espindolac159c962015-10-19 21:00:02 +0000180};
181
Rafael Espindola2deeb602016-07-21 20:18:30 +0000182struct EhSectionPiece : public SectionPiece {
183 EhSectionPiece(size_t Off, ArrayRef<uint8_t> Data, unsigned FirstRelocation)
184 : SectionPiece(Off, Data), FirstRelocation(FirstRelocation) {}
185 unsigned FirstRelocation;
186};
187
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000188// This corresponds to a .eh_frame section of an input file.
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000189template <class ELFT> class EhInputSection : public InputSectionBase<ELFT> {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000190public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000191 typedef typename ELFT::Shdr Elf_Shdr;
192 typedef typename ELFT::uint uintX_t;
Rafael Espindola042a3f22016-09-08 14:06:08 +0000193 EhInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000194 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama88abd9b2016-05-22 23:53:00 +0000195 void split();
Rafael Espindola2deeb602016-07-21 20:18:30 +0000196 template <class RelTy> void split(ArrayRef<RelTy> Rels);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000197
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000198 // Splittable sections are handled as a sequence of data
199 // rather than a single large blob of data.
Rafael Espindola2deeb602016-07-21 20:18:30 +0000200 std::vector<EhSectionPiece> Pieces;
Rafael Espindola6eae9f22016-07-21 13:32:37 +0000201
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000202 // Relocation section that refer to this one.
203 const Elf_Shdr *RelocSection = nullptr;
204};
205
Rafael Espindolac159c962015-10-19 21:00:02 +0000206// This corresponds to a non SHF_MERGE section of an input file.
207template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000208 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000209 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000210 typedef typename ELFT::Shdr Elf_Shdr;
211 typedef typename ELFT::Rela Elf_Rela;
212 typedef typename ELFT::Rel Elf_Rel;
213 typedef typename ELFT::Sym Elf_Sym;
214 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000215
216public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000217 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header, StringRef Name);
Rafael Espindolac159c962015-10-19 21:00:02 +0000218
219 // Write this section to a mmap'ed file, assuming Buf is pointing to
220 // beginning of the output section.
221 void writeTo(uint8_t *Buf);
222
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000223 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000224 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000225
Rui Ueyamaedffd912015-10-14 21:00:23 +0000226 // The offset from beginning of the output sections this section was assigned
227 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000228 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000229
Rafael Espindolac159c962015-10-19 21:00:02 +0000230 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000231
232 InputSectionBase<ELFT> *getRelocatedSection();
233
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000234 // Register thunk related to the symbol. When the section is written
235 // to a mmap'ed file, target is requested to write an actual thunk code.
Peter Smithfb05cd92016-07-08 16:10:27 +0000236 // Now thunks is supported for MIPS and ARM target only.
237 void addThunk(const Thunk<ELFT> *T);
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000238
239 // The offset of synthetic thunk code from beginning of this section.
240 uint64_t getThunkOff() const;
241
242 // Size of chunk with thunks code.
243 uint64_t getThunksSize() const;
244
Rui Ueyama2b6fb802016-04-28 18:42:04 +0000245 template <class RelTy>
246 void relocateNonAlloc(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
247
George Rimar58941ee2016-02-25 08:23:37 +0000248private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000249 template <class RelTy>
Rafael Espindola0f7ccc32016-04-05 14:47:28 +0000250 void copyRelocations(uint8_t *Buf, llvm::ArrayRef<RelTy> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000251
252 // Called by ICF to merge two input sections.
253 void replace(InputSection<ELFT> *Other);
254
255 // Used by ICF.
256 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000257
Peter Smithfb05cd92016-07-08 16:10:27 +0000258 llvm::TinyPtrVector<const Thunk<ELFT> *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000259};
260
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000261// MIPS .reginfo section provides information on the registers used by the code
262// in the object file. Linker should collect this information and write a single
263// .reginfo section in the output file. The output section contains a union of
264// used registers masks taken from input .reginfo sections and final value
265// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
266// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
267template <class ELFT>
268class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000269 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000270
271public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000272 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
273 StringRef Name);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000274 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000275
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000276 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
277};
278
279template <class ELFT>
280class MipsOptionsInputSection : public InputSectionBase<ELFT> {
281 typedef typename ELFT::Shdr Elf_Shdr;
282
283public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000284 MipsOptionsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
285 StringRef Name);
Simon Atanasyanadd74f32016-05-04 10:07:38 +0000286 static bool classof(const InputSectionBase<ELFT> *S);
287
288 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo = nullptr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000289};
290
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000291template <class ELFT>
292class MipsAbiFlagsInputSection : public InputSectionBase<ELFT> {
293 typedef typename ELFT::Shdr Elf_Shdr;
294
295public:
Rafael Espindola042a3f22016-09-08 14:06:08 +0000296 MipsAbiFlagsInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr,
297 StringRef Name);
Simon Atanasyan85c6b442016-08-12 06:28:49 +0000298 static bool classof(const InputSectionBase<ELFT> *S);
299
300 const llvm::object::Elf_Mips_ABIFlags<ELFT> *Flags = nullptr;
301};
302
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000303// Common symbols don't belong to any section. But it is easier for us
304// to handle them as if they belong to some input section. So we defined
305// this class. CommonInputSection is a virtual singleton class that
306// "contains" all common symbols.
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000307template <class ELFT> class CommonInputSection : public InputSection<ELFT> {
308 typedef typename ELFT::uint uintX_t;
309
310public:
Rafael Espindolae7553e42016-08-31 13:28:33 +0000311 CommonInputSection(std::vector<DefinedCommon *> Syms);
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000312
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000313 // The singleton instance of this class.
314 static CommonInputSection<ELFT> *X;
315
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000316private:
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000317 static typename ELFT::Shdr Hdr;
Eugene Leviant3e6b0272016-07-28 19:24:13 +0000318};
319
Rui Ueyamaad10c3d2016-07-28 21:05:04 +0000320template <class ELFT> CommonInputSection<ELFT> *CommonInputSection<ELFT>::X;
321template <class ELFT> typename ELFT::Shdr CommonInputSection<ELFT>::Hdr;
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