blob: afb373decc66cf7536d14f98e768e604bb971767 [file] [log] [blame]
Rafael Espindola9d06ab62015-09-22 00:01:39 +00001//===- InputSection.cpp ---------------------------------------------------===//
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#include "InputSection.h"
Rafael Espindola551dfd82015-09-25 19:24:57 +000011#include "Config.h"
Rafael Espindola192e1fa2015-08-06 15:08:23 +000012#include "Error.h"
Michael J. Spencer67bc8d62015-08-27 23:15:56 +000013#include "InputFiles.h"
Rafael Espindola4ea00212015-09-21 22:01:00 +000014#include "OutputSections.h"
Rafael Espindola01205f72015-09-22 18:19:46 +000015#include "Target.h"
Rafael Espindola4ea00212015-09-21 22:01:00 +000016
Michael J. Spencer84487f12015-07-24 21:03:07 +000017using namespace llvm;
18using namespace llvm::ELF;
Rafael Espindola4ea00212015-09-21 22:01:00 +000019using namespace llvm::object;
Michael J. Spencer84487f12015-07-24 21:03:07 +000020
21using namespace lld;
22using namespace lld::elf2;
23
24template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +000025InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
26 const Elf_Shdr *Header,
27 Kind SectionKind)
28 : Header(Header), File(File), SectionKind(SectionKind) {}
29
30template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
31 ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
32 error(Name);
33 return *Name;
34}
35
36template <class ELFT>
37ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const {
38 ErrorOr<ArrayRef<uint8_t>> Ret =
39 this->File->getObj().getSectionContents(this->Header);
40 error(Ret);
41 return *Ret;
42}
43
44template <class ELFT>
45typename ELFFile<ELFT>::uintX_t
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000046InputSectionBase<ELFT>::getOffset(uintX_t Offset) {
47 switch (SectionKind) {
48 case Regular:
49 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000050 case EHFrame:
51 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset);
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000052 case Merge:
53 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset);
54 }
Denis Protivensky1b1b34e2015-11-12 09:11:20 +000055 llvm_unreachable("Invalid section kind");
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000056}
57
58template <class ELFT>
59typename ELFFile<ELFT>::uintX_t
Rafael Espindola48225b42015-10-23 19:55:11 +000060InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindoladb9bf4d2015-11-11 16:50:37 +000061 return getOffset(Sym.st_value);
Rafael Espindolac159c962015-10-19 21:00:02 +000062}
63
Rui Ueyama12504642015-10-27 21:51:13 +000064// Returns a section that Rel relocation is pointing to.
65template <class ELFT>
66InputSectionBase<ELFT> *
67InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) {
68 // Global symbol
69 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
70 if (SymbolBody *B = File->getSymbolBody(SymIndex))
71 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
72 return &D->Section;
73 // Local symbol
74 if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
75 if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
76 return Sec;
77 return nullptr;
78}
79
80template <class ELFT>
81InputSectionBase<ELFT> *
82InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) {
83 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
84}
85
Rafael Espindolac159c962015-10-19 21:00:02 +000086template <class ELFT>
Rafael Espindola53d5cea2015-09-21 17:47:00 +000087InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +000088 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
89
90template <class ELFT>
91bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
92 return S->SectionKind == Base::Regular;
93}
Michael J. Spencer84487f12015-07-24 21:03:07 +000094
Rafael Espindola4ea00212015-09-21 22:01:00 +000095template <class ELFT>
Rafael Espindola4ea00212015-09-21 22:01:00 +000096template <bool isRela>
Rafael Espindola9a6e4632015-11-11 10:18:52 +000097void InputSectionBase<ELFT>::relocate(
Hal Finkel87bbd5f2015-10-12 21:19:18 +000098 uint8_t *Buf, uint8_t *BufEnd,
Rafael Espindola0c6a4f12015-11-11 19:54:14 +000099 iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000100 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000101 for (const RelType &RI : Rels) {
Rui Ueyama64558522015-10-16 22:51:43 +0000102 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
103 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000104 uintX_t Offset = getOffset(RI.r_offset);
105 if (Offset == (uintX_t)-1)
106 continue;
107
108 uint8_t *BufLoc = Buf + Offset;
109 uintX_t AddrLoc = OutSec->getVA() + Offset;
Michael J. Spencer1e225612015-11-11 01:00:24 +0000110
111 if (Type == Target->getTlsLocalDynamicReloc()) {
112 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
113 Out<ELFT>::Got->getVA() +
114 Out<ELFT>::LocalModuleTlsIndexOffset +
115 getAddend<ELFT>(RI));
116 continue;
117 }
Rafael Espindola4ea00212015-09-21 22:01:00 +0000118
119 // Handle relocations for local symbols -- they never get
120 // resolved so we don't allocate a SymbolBody.
Rafael Espindola8e37f792015-11-11 10:23:32 +0000121 const Elf_Shdr *SymTab = File->getSymbolTable();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000122 if (SymIndex < SymTab->sh_info) {
Rafael Espindola8e37f792015-11-11 10:23:32 +0000123 uintX_t SymVA = getLocalRelTarget(*File, RI);
Michael J. Spencer1e225612015-11-11 01:00:24 +0000124 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA);
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000125 continue;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000126 }
127
Rafael Espindola8e37f792015-11-11 10:23:32 +0000128 SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl();
Michael J. Spencer627ae702015-11-13 00:28:34 +0000129
130 if (Type == Target->getTlsGlobalDynamicReloc()) {
131 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
132 Out<ELFT>::Got->getEntryAddr(Body) +
133 getAddend<ELFT>(RI));
134 continue;
135 }
136
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000137 uintX_t SymVA = getSymVA<ELFT>(Body);
138 if (Target->relocNeedsPlt(Type, Body)) {
139 SymVA = Out<ELFT>::Plt->getEntryAddr(Body);
Rafael Espindola227556e2015-10-14 16:15:46 +0000140 Type = Target->getPLTRefReloc(Type);
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000141 } else if (Target->relocNeedsGot(Type, Body)) {
142 SymVA = Out<ELFT>::Got->getEntryAddr(Body);
143 Type = Target->getGotRefReloc();
144 } else if (Target->relocPointsToGot(Type)) {
145 SymVA = Out<ELFT>::Got->getVA();
146 Type = Target->getPCRelReloc();
George Rimarbc590fe2015-10-28 16:48:58 +0000147 } else if (!Target->relocNeedsCopy(Type, Body) &&
148 isa<SharedSymbol<ELFT>>(Body)) {
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000149 continue;
150 }
Michael J. Spencer1e225612015-11-11 01:00:24 +0000151 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000152 SymVA + getAddend<ELFT>(RI));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000153 }
154}
155
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000156template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000157 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000158 return;
159 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000160 ArrayRef<uint8_t> Data = this->getSectionData();
Rui Ueyama55c3f892015-10-15 01:58:40 +0000161 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000162
Rafael Espindolac159c962015-10-19 21:00:02 +0000163 ELFFile<ELFT> &EObj = this->File->getObj();
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000164 uint8_t *BufEnd = Buf + OutSecOff + Data.size();
Rafael Espindola4ea00212015-09-21 22:01:00 +0000165 // Iterate over all relocation sections that apply to this section.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000166 for (const Elf_Shdr *RelSec : this->RelocSections) {
Rafael Espindola4ea00212015-09-21 22:01:00 +0000167 if (RelSec->sh_type == SHT_RELA)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000168 this->relocate(Buf, BufEnd, EObj.relas(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000169 else
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000170 this->relocate(Buf, BufEnd, EObj.rels(RelSec));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000171 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000172}
173
Rafael Espindolac159c962015-10-19 21:00:02 +0000174template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000175SplitInputSection<ELFT>::SplitInputSection(
176 ObjectFile<ELFT> *File, const Elf_Shdr *Header,
177 typename InputSectionBase<ELFT>::Kind SectionKind)
178 : InputSectionBase<ELFT>(File, Header, SectionKind) {}
179
180template <class ELFT>
181EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F,
182 const Elf_Shdr *Header)
183 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {}
184
185template <class ELFT>
186bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
187 return S->SectionKind == InputSectionBase<ELFT>::EHFrame;
188}
189
190template <class ELFT>
191typename EHInputSection<ELFT>::uintX_t
192EHInputSection<ELFT>::getOffset(uintX_t Offset) {
193 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first;
194 uintX_t Base = I->second;
195 if (Base == size_t(-1))
196 return -1; // Not in the output
197
198 uintX_t Addend = Offset - I->first;
199 return Base + Addend;
200}
201
202template <class ELFT>
Rafael Espindolac159c962015-10-19 21:00:02 +0000203MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
204 const Elf_Shdr *Header)
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000205 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {}
Rafael Espindolac159c962015-10-19 21:00:02 +0000206
207template <class ELFT>
208bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000209 return S->SectionKind == InputSectionBase<ELFT>::Merge;
Rafael Espindolac159c962015-10-19 21:00:02 +0000210}
211
Rafael Espindolac159c962015-10-19 21:00:02 +0000212template <class ELFT>
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000213std::pair<std::pair<typename ELFFile<ELFT>::uintX_t,
214 typename ELFFile<ELFT>::uintX_t> *,
215 typename ELFFile<ELFT>::uintX_t>
216SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000217 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000218 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000219 uintX_t Size = Data.size();
220 if (Offset >= Size)
Rafael Espindolac159c962015-10-19 21:00:02 +0000221 error("Entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000222
223 // Find the element this offset points to.
224 auto I = std::upper_bound(
Rafael Espindolad04c12a2015-11-11 15:20:45 +0000225 Offsets.begin(), Offsets.end(), Offset,
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000226 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000227 return A < B.first;
228 });
Rafael Espindola32994992015-11-11 15:40:37 +0000229 uintX_t End = I == Offsets.end() ? Data.size() : I->first;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000230 --I;
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000231 return std::make_pair(&*I, End);
232}
233
234template <class ELFT>
235typename MergeInputSection<ELFT>::uintX_t
236MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
237 std::pair<std::pair<uintX_t, uintX_t> *, size_t> T =
238 this->getRangeAndSize(Offset);
239 std::pair<uintX_t, uintX_t> *I = T.first;
240 uintX_t End = T.second;
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000241 uintX_t Start = I->first;
242
243 // Compute the Addend and if the Base is cached, return.
244 uintX_t Addend = Offset - Start;
Rafael Espindola32994992015-11-11 15:40:37 +0000245 uintX_t &Base = I->second;
Rafael Espindola1fe2d1e2015-11-11 15:55:00 +0000246 if (Base != uintX_t(-1))
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000247 return Base + Addend;
248
249 // Map the base to the offset in the output section and cashe it.
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000250 ArrayRef<uint8_t> D = this->getSectionData();
251 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000252 StringRef Entry = Data.substr(Start, End - Start);
253 Base =
254 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
255 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000256}
257
Michael J. Spencer84487f12015-07-24 21:03:07 +0000258namespace lld {
259namespace elf2 {
Rafael Espindolac159c962015-10-19 21:00:02 +0000260template class InputSectionBase<object::ELF32LE>;
261template class InputSectionBase<object::ELF32BE>;
262template class InputSectionBase<object::ELF64LE>;
263template class InputSectionBase<object::ELF64BE>;
264
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000265template class InputSection<object::ELF32LE>;
266template class InputSection<object::ELF32BE>;
267template class InputSection<object::ELF64LE>;
268template class InputSection<object::ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000269
Rafael Espindola0c6a4f12015-11-11 19:54:14 +0000270template class EHInputSection<object::ELF32LE>;
271template class EHInputSection<object::ELF32BE>;
272template class EHInputSection<object::ELF64LE>;
273template class EHInputSection<object::ELF64BE>;
274
Rafael Espindolac159c962015-10-19 21:00:02 +0000275template class MergeInputSection<object::ELF32LE>;
276template class MergeInputSection<object::ELF32BE>;
277template class MergeInputSection<object::ELF64LE>;
278template class MergeInputSection<object::ELF64BE>;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000279}
280}