blob: 9064abbf31afa456e2c91d08a20cbb9c189805c7 [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"
Michael J. Spencer84487f12015-07-24 21:03:07 +000014#include "lld/Core/LLVM.h"
Rui Ueyamac00718f2016-02-23 03:34:37 +000015#include "llvm/ADT/TinyPtrVector.h"
Michael J. Spencer84487f12015-07-24 21:03:07 +000016#include "llvm/Object/ELF.h"
17
18namespace lld {
Rafael Espindolae0df00b2016-02-28 00:25:54 +000019namespace elf {
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
Rui Ueyama0b289522016-02-25 18:43:51 +000021template <class ELFT> class ICF;
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000022template <class ELFT> class DefinedRegular;
Michael J. Spencer84487f12015-07-24 21:03:07 +000023template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000024template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000025template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000026
Rafael Espindola71675852015-09-22 00:16:19 +000027// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000028template <class ELFT> class InputSectionBase {
29protected:
Rui Ueyama9328b2c2016-03-14 23:16:09 +000030 typedef typename ELFT::Rel Elf_Rel;
31 typedef typename ELFT::Rela Elf_Rela;
32 typedef typename ELFT::Shdr Elf_Shdr;
33 typedef typename ELFT::Sym Elf_Sym;
34 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000035 const Elf_Shdr *Header;
36
37 // The file this section is from.
38 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000039
40public:
Simon Atanasyan1d7df402015-12-20 10:57:34 +000041 enum Kind { Regular, EHFrame, Merge, MipsReginfo };
Rafael Espindolac159c962015-10-19 21:00:02 +000042 Kind SectionKind;
43
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000044 InputSectionBase() : Repl(this) {}
45
Rafael Espindolac159c962015-10-19 21:00:02 +000046 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
47 Kind SectionKind);
48 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama6a9ef852016-02-25 18:49:09 +000049 uint32_t Align;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000050
Rui Ueyamac4aaed92015-10-22 18:49:53 +000051 // Used for garbage collection.
Rui Ueyama6a9ef852016-02-25 18:49:09 +000052 bool Live;
Rui Ueyamac4aaed92015-10-22 18:49:53 +000053
Rui Ueyama0b289522016-02-25 18:43:51 +000054 // This pointer points to the "real" instance of this instance.
55 // Usually Repl == this. However, if ICF merges two sections,
56 // Repl pointer of one section points to another section. So,
57 // if you need to get a pointer to this instance, do not use
58 // this but instead this->Repl.
59 InputSectionBase<ELFT> *Repl;
60
Rafael Espindola71675852015-09-22 00:16:19 +000061 // Returns the size of this section (even if this is a common or BSS.)
Simon Atanasyan13f6da12016-03-31 21:26:23 +000062 size_t getSize() const;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000063
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000064 static InputSectionBase<ELFT> Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000065
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000066 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000067 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000068 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000069 uintX_t getOffset(const DefinedRegular<ELFT> &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000070
71 // Translate an offset in the input section to an offset in the output
72 // section.
73 uintX_t getOffset(uintX_t Offset);
74
Rafael Espindolac159c962015-10-19 21:00:02 +000075 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000076
77 // Returns a section that Rel is pointing to. Used by the garbage collector.
Rui Ueyamad7e4a282016-02-24 00:23:13 +000078 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const;
79 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const;
Rafael Espindola9a6e4632015-11-11 10:18:52 +000080
Rafael Espindola69082f02016-03-18 18:11:26 +000081 template <class RelTy>
82 void relocate(uint8_t *Buf, uint8_t *BufEnd,
83 llvm::iterator_range<const RelTy *> Rels);
Simon Atanasyan09b3e362015-12-01 21:24:45 +000084
85private:
Rui Ueyamafc467e72016-03-13 05:06:50 +000086 template <class RelTy>
Rafael Espindola163974d2016-03-29 23:05:59 +000087 int32_t findMipsPairedAddend(uint8_t *Buf, uint8_t *BufLoc, SymbolBody &Sym,
88 const RelTy *Rel, const RelTy *End);
Rafael Espindolac159c962015-10-19 21:00:02 +000089};
90
Rafael Espindolaccfe3cb2016-04-04 14:04:16 +000091template <class ELFT> InputSectionBase<ELFT> InputSectionBase<ELFT>::Discarded;
Rafael Espindolac159c962015-10-19 21:00:02 +000092
Rui Ueyama5a32c762016-01-06 03:16:23 +000093// Usually sections are copied to the output as atomic chunks of data,
94// but some special types of sections are split into small pieces of data
95// and each piece is copied to a different place in the output.
96// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000097template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +000098 typedef typename ELFT::Shdr Elf_Shdr;
99 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000100
101public:
102 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
103 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +0000104
105 // For each piece of data, we maintain the offsets in the input section and
106 // in the output section. The latter may be -1 if it is not assigned yet.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000107 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000108
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000109 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
110 getRangeAndSize(uintX_t Offset);
111};
112
Rafael Espindolac159c962015-10-19 21:00:02 +0000113// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000114template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000115 typedef typename ELFT::uint uintX_t;
116 typedef typename ELFT::Sym Elf_Sym;
117 typedef typename ELFT::Shdr Elf_Shdr;
Rafael Espindolac159c962015-10-19 21:00:02 +0000118
119public:
120 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
121 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000122 // Translate an offset in the input section to an offset in the output
123 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000124 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000125};
126
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000127// This corresponds to a .eh_frame section of an input file.
128template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
129public:
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000130 typedef typename ELFT::Shdr Elf_Shdr;
131 typedef typename ELFT::uint uintX_t;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000132 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
133 static bool classof(const InputSectionBase<ELFT> *S);
134
135 // Translate an offset in the input section to an offset in the output
136 // section.
137 uintX_t getOffset(uintX_t Offset);
138
139 // Relocation section that refer to this one.
140 const Elf_Shdr *RelocSection = nullptr;
141};
142
Rafael Espindolac159c962015-10-19 21:00:02 +0000143// This corresponds to a non SHF_MERGE section of an input file.
144template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
Rui Ueyama0b289522016-02-25 18:43:51 +0000145 friend ICF<ELFT>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000146 typedef InputSectionBase<ELFT> Base;
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000147 typedef typename ELFT::Shdr Elf_Shdr;
148 typedef typename ELFT::Rela Elf_Rela;
149 typedef typename ELFT::Rel Elf_Rel;
150 typedef typename ELFT::Sym Elf_Sym;
151 typedef typename ELFT::uint uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +0000152
153public:
154 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
155
156 // Write this section to a mmap'ed file, assuming Buf is pointing to
157 // beginning of the output section.
158 void writeTo(uint8_t *Buf);
159
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000160 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000161 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000162
Rui Ueyamaedffd912015-10-14 21:00:23 +0000163 // The offset from beginning of the output sections this section was assigned
164 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000165 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000166
Rafael Espindolac159c962015-10-19 21:00:02 +0000167 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000168
169 InputSectionBase<ELFT> *getRelocatedSection();
170
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000171 // Register thunk related to the symbol. When the section is written
172 // to a mmap'ed file, target is requested to write an actual thunk code.
173 // Now thunks is supported for MIPS target only.
174 void addThunk(SymbolBody &Body);
175
176 // The offset of synthetic thunk code from beginning of this section.
177 uint64_t getThunkOff() const;
178
179 // Size of chunk with thunks code.
180 uint64_t getThunksSize() const;
181
George Rimar58941ee2016-02-25 08:23:37 +0000182private:
Rui Ueyamafc467e72016-03-13 05:06:50 +0000183 template <class RelTy>
184 void copyRelocations(uint8_t *Buf, llvm::iterator_range<const RelTy *> Rels);
Rui Ueyama0b289522016-02-25 18:43:51 +0000185
186 // Called by ICF to merge two input sections.
187 void replace(InputSection<ELFT> *Other);
188
189 // Used by ICF.
190 uint64_t GroupId = 0;
Simon Atanasyan13f6da12016-03-31 21:26:23 +0000191
192 llvm::TinyPtrVector<const SymbolBody *> Thunks;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000193};
194
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000195// MIPS .reginfo section provides information on the registers used by the code
196// in the object file. Linker should collect this information and write a single
197// .reginfo section in the output file. The output section contains a union of
198// used registers masks taken from input .reginfo sections and final value
199// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
200// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
201template <class ELFT>
202class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Rui Ueyama9328b2c2016-03-14 23:16:09 +0000203 typedef typename ELFT::Shdr Elf_Shdr;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000204
205public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000206 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000207 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000208
209 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000210};
211
Rafael Espindolae0df00b2016-02-28 00:25:54 +0000212} // namespace elf
Michael J. Spencer84487f12015-07-24 21:03:07 +0000213} // namespace lld
214
215#endif