blob: dd8a8c61785a01ee633726a901dd6dd130c42ae6 [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 Espindola48225b42015-10-23 19:55:11 +000046InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
Rafael Espindolac159c962015-10-19 21:00:02 +000047 if (auto *S = dyn_cast<InputSection<ELFT>>(this))
48 return S->OutSecOff + Sym.st_value;
49 return cast<MergeInputSection<ELFT>>(this)->getOffset(Sym.st_value);
50}
51
Rui Ueyama12504642015-10-27 21:51:13 +000052// Returns a section that Rel relocation is pointing to.
53template <class ELFT>
54InputSectionBase<ELFT> *
55InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) {
56 // Global symbol
57 uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL);
58 if (SymbolBody *B = File->getSymbolBody(SymIndex))
59 if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl()))
60 return &D->Section;
61 // Local symbol
62 if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex))
63 if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym))
64 return Sec;
65 return nullptr;
66}
67
68template <class ELFT>
69InputSectionBase<ELFT> *
70InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) {
71 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel));
72}
73
Rafael Espindolac159c962015-10-19 21:00:02 +000074template <class ELFT>
Rafael Espindola53d5cea2015-09-21 17:47:00 +000075InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
Rafael Espindolac159c962015-10-19 21:00:02 +000076 : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
77
78template <class ELFT>
79bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
80 return S->SectionKind == Base::Regular;
81}
Michael J. Spencer84487f12015-07-24 21:03:07 +000082
Rafael Espindola4ea00212015-09-21 22:01:00 +000083template <class ELFT>
Rafael Espindola4ea00212015-09-21 22:01:00 +000084template <bool isRela>
85void InputSection<ELFT>::relocate(
Hal Finkel87bbd5f2015-10-12 21:19:18 +000086 uint8_t *Buf, uint8_t *BufEnd,
87 iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels,
Rui Ueyama15ef5e12015-10-07 19:18:16 +000088 const ObjectFile<ELFT> &File, uintX_t BaseAddr) {
Rafael Espindola4ea00212015-09-21 22:01:00 +000089 typedef Elf_Rel_Impl<ELFT, isRela> RelType;
Rafael Espindola4ea00212015-09-21 22:01:00 +000090 for (const RelType &RI : Rels) {
Rui Ueyama64558522015-10-16 22:51:43 +000091 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
92 uint32_t Type = RI.getType(Config->Mips64EL);
Rafael Espindola4ea00212015-09-21 22:01:00 +000093
94 // Handle relocations for local symbols -- they never get
95 // resolved so we don't allocate a SymbolBody.
96 const Elf_Shdr *SymTab = File.getSymbolTable();
97 if (SymIndex < SymTab->sh_info) {
Hal Finkel230c5c52015-10-16 22:37:32 +000098 uintX_t SymVA = getLocalRelTarget(File, RI);
Rui Ueyama96f0e0b2015-10-23 02:40:46 +000099 Target->relocateOne(Buf + RI.r_offset, BufEnd, Type,
100 BaseAddr + RI.r_offset, SymVA);
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000101 continue;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000102 }
103
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000104 SymbolBody &Body = *File.getSymbolBody(SymIndex)->repl();
105 uintX_t SymVA = getSymVA<ELFT>(Body);
106 if (Target->relocNeedsPlt(Type, Body)) {
107 SymVA = Out<ELFT>::Plt->getEntryAddr(Body);
Rafael Espindola227556e2015-10-14 16:15:46 +0000108 Type = Target->getPLTRefReloc(Type);
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000109 } else if (Target->relocNeedsGot(Type, Body)) {
110 SymVA = Out<ELFT>::Got->getEntryAddr(Body);
111 Type = Target->getGotRefReloc();
112 } else if (Target->relocPointsToGot(Type)) {
113 SymVA = Out<ELFT>::Got->getVA();
114 Type = Target->getPCRelReloc();
George Rimarbc590fe2015-10-28 16:48:58 +0000115 } else if (!Target->relocNeedsCopy(Type, Body) &&
116 isa<SharedSymbol<ELFT>>(Body)) {
Rui Ueyamabb3c3362015-10-12 20:28:23 +0000117 continue;
118 }
Rui Ueyama96f0e0b2015-10-23 02:40:46 +0000119 Target->relocateOne(Buf + RI.r_offset, BufEnd, Type, BaseAddr + RI.r_offset,
Rafael Espindola932efcf2015-10-19 20:24:44 +0000120 SymVA + getAddend<ELFT>(RI));
Rafael Espindola4ea00212015-09-21 22:01:00 +0000121 }
122}
123
Rui Ueyama15ef5e12015-10-07 19:18:16 +0000124template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
Rafael Espindolac159c962015-10-19 21:00:02 +0000125 if (this->Header->sh_type == SHT_NOBITS)
Michael J. Spencer84487f12015-07-24 21:03:07 +0000126 return;
127 // Copy section contents from source object file to output file.
Rafael Espindolac159c962015-10-19 21:00:02 +0000128 ArrayRef<uint8_t> Data = this->getSectionData();
Rui Ueyama55c3f892015-10-15 01:58:40 +0000129 memcpy(Buf + OutSecOff, Data.data(), Data.size());
Rafael Espindola4ea00212015-09-21 22:01:00 +0000130
Rafael Espindolac159c962015-10-19 21:00:02 +0000131 ELFFile<ELFT> &EObj = this->File->getObj();
Rui Ueyama55c3f892015-10-15 01:58:40 +0000132 uint8_t *Base = Buf + OutSecOff;
Rafael Espindolac159c962015-10-19 21:00:02 +0000133 uintX_t BaseAddr = this->OutSec->getVA() + OutSecOff;
Rafael Espindola4ea00212015-09-21 22:01:00 +0000134 // Iterate over all relocation sections that apply to this section.
135 for (const Elf_Shdr *RelSec : RelocSections) {
136 if (RelSec->sh_type == SHT_RELA)
Rafael Espindolac159c962015-10-19 21:00:02 +0000137 relocate(Base, Base + Data.size(), EObj.relas(RelSec), *this->File,
138 BaseAddr);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000139 else
Rafael Espindolac159c962015-10-19 21:00:02 +0000140 relocate(Base, Base + Data.size(), EObj.rels(RelSec), *this->File,
141 BaseAddr);
Rafael Espindola4ea00212015-09-21 22:01:00 +0000142 }
Michael J. Spencer84487f12015-07-24 21:03:07 +0000143}
144
Rafael Espindolac159c962015-10-19 21:00:02 +0000145template <class ELFT>
146MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
147 const Elf_Shdr *Header)
148 : InputSectionBase<ELFT>(F, Header, Base::Merge) {}
149
150template <class ELFT>
151bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
152 return S->SectionKind == Base::Merge;
153}
154
Rafael Espindolac159c962015-10-19 21:00:02 +0000155template <class ELFT>
156typename MergeInputSection<ELFT>::uintX_t
Rafael Espindola48225b42015-10-23 19:55:11 +0000157MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000158 ArrayRef<uint8_t> D = this->getSectionData();
Rui Ueyama7ba639b2015-10-25 16:25:04 +0000159 StringRef Data((const char *)D.data(), D.size());
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000160 uintX_t Size = Data.size();
161 if (Offset >= Size)
Rafael Espindolac159c962015-10-19 21:00:02 +0000162 error("Entry is past the end of the section");
Rafael Espindolaf82ed2a2015-10-24 22:51:01 +0000163
164 // Find the element this offset points to.
165 auto I = std::upper_bound(
166 this->Offsets.begin(), this->Offsets.end(), Offset,
167 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
168 return A < B.first;
169 });
170 size_t End = I == this->Offsets.end() ? Data.size() : I->first;
171 --I;
172 uintX_t Start = I->first;
173
174 // Compute the Addend and if the Base is cached, return.
175 uintX_t Addend = Offset - Start;
176 uintX_t &Base = I->second;
177 if (Base != uintX_t(-1))
178 return Base + Addend;
179
180 // Map the base to the offset in the output section and cashe it.
181 StringRef Entry = Data.substr(Start, End - Start);
182 Base =
183 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
184 return Base + Addend;
Rafael Espindola5d83ccd2015-08-13 19:18:30 +0000185}
186
Michael J. Spencer84487f12015-07-24 21:03:07 +0000187namespace lld {
188namespace elf2 {
Rafael Espindolac159c962015-10-19 21:00:02 +0000189template class InputSectionBase<object::ELF32LE>;
190template class InputSectionBase<object::ELF32BE>;
191template class InputSectionBase<object::ELF64LE>;
192template class InputSectionBase<object::ELF64BE>;
193
Rafael Espindola53d5cea2015-09-21 17:47:00 +0000194template class InputSection<object::ELF32LE>;
195template class InputSection<object::ELF32BE>;
196template class InputSection<object::ELF64LE>;
197template class InputSection<object::ELF64BE>;
Rafael Espindolac159c962015-10-19 21:00:02 +0000198
199template class MergeInputSection<object::ELF32LE>;
200template class MergeInputSection<object::ELF32BE>;
201template class MergeInputSection<object::ELF64LE>;
202template class MergeInputSection<object::ELF64BE>;
Michael J. Spencer84487f12015-07-24 21:03:07 +0000203}
204}