blob: fe1ec19cfe026de9dabcfbd380b167e46a69b70b [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 {
19namespace elf2 {
20
Michael J. Spencer84487f12015-07-24 21:03:07 +000021template <class ELFT> class ObjectFile;
Rafael Espindola832b93f2015-08-24 20:06:32 +000022template <class ELFT> class OutputSection;
Rui Ueyamac7cc6ec2015-10-15 22:27:29 +000023template <class ELFT> class OutputSectionBase;
Michael J. Spencer84487f12015-07-24 21:03:07 +000024
Rafael Espindola71675852015-09-22 00:16:19 +000025// This corresponds to a section of an input file.
Rafael Espindolac159c962015-10-19 21:00:02 +000026template <class ELFT> class InputSectionBase {
27protected:
Rui Ueyama12504642015-10-27 21:51:13 +000028 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
29 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
Rafael Espindola7d4038dc2015-09-15 12:43:09 +000030 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
Rafael Espindola4ea00212015-09-21 22:01:00 +000031 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
Rafael Espindolac5c82912015-08-24 19:28:31 +000032 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
Rafael Espindolac159c962015-10-19 21:00:02 +000033 const Elf_Shdr *Header;
34
35 // The file this section is from.
36 ObjectFile<ELFT> *File;
Michael J. Spencer84487f12015-07-24 21:03:07 +000037
38public:
Simon Atanasyan1d7df402015-12-20 10:57:34 +000039 enum Kind { Regular, EHFrame, Merge, MipsReginfo };
Rafael Espindolac159c962015-10-19 21:00:02 +000040 Kind SectionKind;
41
42 InputSectionBase(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
43 Kind SectionKind);
44 OutputSectionBase<ELFT> *OutSec = nullptr;
Rui Ueyama5ac58912016-02-24 00:38:18 +000045 uint32_t Align = 1;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000046
Rui Ueyamac4aaed92015-10-22 18:49:53 +000047 // Used for garbage collection.
Rui Ueyamac4aaed92015-10-22 18:49:53 +000048 bool Live = false;
49
Rafael Espindola71675852015-09-22 00:16:19 +000050 // Returns the size of this section (even if this is a common or BSS.)
Rafael Espindola83b0dc62015-08-13 22:21:37 +000051 size_t getSize() const { return Header->sh_size; }
52
Rui Ueyama733153d2016-02-24 18:33:35 +000053 static InputSectionBase<ELFT> *Discarded;
Rafael Espindola83b0dc62015-08-13 22:21:37 +000054
Rafael Espindola5d83ccd2015-08-13 19:18:30 +000055 StringRef getSectionName() const;
Michael J. Spencer8039dae22015-07-29 00:30:10 +000056 const Elf_Shdr *getSectionHdr() const { return Header; }
Rafael Espindolae1901cc2015-09-24 15:11:50 +000057 ObjectFile<ELFT> *getFile() const { return File; }
Rafael Espindola48225b42015-10-23 19:55:11 +000058 uintX_t getOffset(const Elf_Sym &Sym);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000059
60 // Translate an offset in the input section to an offset in the output
61 // section.
62 uintX_t getOffset(uintX_t Offset);
63
Rafael Espindolac159c962015-10-19 21:00:02 +000064 ArrayRef<uint8_t> getSectionData() const;
Rui Ueyama12504642015-10-27 21:51:13 +000065
66 // Returns a section that Rel is pointing to. Used by the garbage collector.
Rui Ueyamad7e4a282016-02-24 00:23:13 +000067 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rel &Rel) const;
68 InputSectionBase<ELFT> *getRelocTarget(const Elf_Rela &Rel) const;
Rafael Espindola9a6e4632015-11-11 10:18:52 +000069
70 template <bool isRela>
Simon Atanasyan09b3e362015-12-01 21:24:45 +000071 using RelIteratorRange =
72 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, isRela> *>;
73
74 template <bool isRela>
75 void relocate(uint8_t *Buf, uint8_t *BufEnd, RelIteratorRange<isRela> Rels);
76
77private:
78 template <bool isRela>
Simon Atanasyandddbeb72015-12-13 06:49:08 +000079 uint8_t *findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex, uint32_t Type,
Simon Atanasyan09b3e362015-12-01 21:24:45 +000080 RelIteratorRange<isRela> Rels);
Rafael Espindolac159c962015-10-19 21:00:02 +000081};
82
83template <class ELFT>
Rui Ueyama733153d2016-02-24 18:33:35 +000084InputSectionBase<ELFT> *
85 InputSectionBase<ELFT>::Discarded = (InputSectionBase<ELFT> *)-1ULL;
Rafael Espindolac159c962015-10-19 21:00:02 +000086
Rui Ueyama5a32c762016-01-06 03:16:23 +000087// Usually sections are copied to the output as atomic chunks of data,
88// but some special types of sections are split into small pieces of data
89// and each piece is copied to a different place in the output.
90// This class represents such special sections.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000091template <class ELFT> class SplitInputSection : public InputSectionBase<ELFT> {
92 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
93 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
94
95public:
96 SplitInputSection(ObjectFile<ELFT> *File, const Elf_Shdr *Header,
97 typename InputSectionBase<ELFT>::Kind SectionKind);
Rui Ueyama5a32c762016-01-06 03:16:23 +000098
99 // For each piece of data, we maintain the offsets in the input section and
100 // in the output section. The latter may be -1 if it is not assigned yet.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000101 std::vector<std::pair<uintX_t, uintX_t>> Offsets;
Rui Ueyama5a32c762016-01-06 03:16:23 +0000102
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000103 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t>
104 getRangeAndSize(uintX_t Offset);
105};
106
Rafael Espindolac159c962015-10-19 21:00:02 +0000107// This corresponds to a SHF_MERGE section of an input file.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000108template <class ELFT> class MergeInputSection : public SplitInputSection<ELFT> {
Rafael Espindolac159c962015-10-19 21:00:02 +0000109 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
110 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
111 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
112
113public:
114 MergeInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
115 static bool classof(const InputSectionBase<ELFT> *S);
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000116 // Translate an offset in the input section to an offset in the output
117 // section.
Rafael Espindola48225b42015-10-23 19:55:11 +0000118 uintX_t getOffset(uintX_t Offset);
Rafael Espindolac159c962015-10-19 21:00:02 +0000119};
120
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000121// This corresponds to a .eh_frame section of an input file.
122template <class ELFT> class EHInputSection : public SplitInputSection<ELFT> {
123public:
124 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
125 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
126 EHInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
127 static bool classof(const InputSectionBase<ELFT> *S);
128
129 // Translate an offset in the input section to an offset in the output
130 // section.
131 uintX_t getOffset(uintX_t Offset);
132
133 // Relocation section that refer to this one.
134 const Elf_Shdr *RelocSection = nullptr;
135};
136
Rafael Espindolac159c962015-10-19 21:00:02 +0000137// This corresponds to a non SHF_MERGE section of an input file.
138template <class ELFT> class InputSection : public InputSectionBase<ELFT> {
139 typedef InputSectionBase<ELFT> Base;
140 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
141 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rela Elf_Rela;
142 typedef typename llvm::object::ELFFile<ELFT>::Elf_Rel Elf_Rel;
143 typedef typename llvm::object::ELFFile<ELFT>::Elf_Sym Elf_Sym;
144 typedef typename llvm::object::ELFFile<ELFT>::uintX_t uintX_t;
145
146public:
147 InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header);
148
149 // Write this section to a mmap'ed file, assuming Buf is pointing to
150 // beginning of the output section.
151 void writeTo(uint8_t *Buf);
152
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000153 // Relocation sections that refer to this one.
Rui Ueyamac00718f2016-02-23 03:34:37 +0000154 llvm::TinyPtrVector<const Elf_Shdr *> RelocSections;
Michael J. Spencer67bc8d62015-08-27 23:15:56 +0000155
Rui Ueyamaedffd912015-10-14 21:00:23 +0000156 // The offset from beginning of the output sections this section was assigned
157 // to. The writer sets a value.
Rui Ueyama55c3f892015-10-15 01:58:40 +0000158 uint64_t OutSecOff = 0;
Rui Ueyamaedffd912015-10-14 21:00:23 +0000159
Rafael Espindolac159c962015-10-19 21:00:02 +0000160 static bool classof(const InputSectionBase<ELFT> *S);
George Rimar58941ee2016-02-25 08:23:37 +0000161
162 InputSectionBase<ELFT> *getRelocatedSection();
163
164private:
165 template <bool isRela>
166 using RelIteratorRange =
167 llvm::iterator_range<const llvm::object::Elf_Rel_Impl<ELFT, isRela> *>;
168
169 template <bool isRela>
170 void copyRelocations(uint8_t *Buf, RelIteratorRange<isRela> Rels);
Michael J. Spencer84487f12015-07-24 21:03:07 +0000171};
172
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000173// MIPS .reginfo section provides information on the registers used by the code
174// in the object file. Linker should collect this information and write a single
175// .reginfo section in the output file. The output section contains a union of
176// used registers masks taken from input .reginfo sections and final value
177// of the `_gp` symbol. For details: Chapter 4 / "Register Information" at
178// ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
179template <class ELFT>
180class MipsReginfoInputSection : public InputSectionBase<ELFT> {
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000181 typedef typename llvm::object::ELFFile<ELFT>::Elf_Shdr Elf_Shdr;
182
183public:
Rui Ueyama70eed362016-01-06 22:42:43 +0000184 MipsReginfoInputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Hdr);
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000185 static bool classof(const InputSectionBase<ELFT> *S);
Rui Ueyama70eed362016-01-06 22:42:43 +0000186
187 const llvm::object::Elf_Mips_RegInfo<ELFT> *Reginfo;
Simon Atanasyan1d7df402015-12-20 10:57:34 +0000188};
189
Michael J. Spencer84487f12015-07-24 21:03:07 +0000190} // namespace elf2
191} // namespace lld
192
193#endif