1 //===- InputSection.cpp ---------------------------------------------------===//
2 //
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 
10 #include "InputSection.h"
11 #include "Config.h"
12 #include "Error.h"
13 #include "InputFiles.h"
14 #include "OutputSections.h"
15 #include "Target.h"
16 
17 using namespace llvm;
18 using namespace llvm::ELF;
19 using namespace llvm::object;
20 
21 using namespace lld;
22 using namespace lld::elf2;
23 
24 template <class ELFT>
25 InputSectionBase<ELFT>::InputSectionBase(ObjectFile<ELFT> *File,
26                                          const Elf_Shdr *Header,
27                                          Kind SectionKind)
28     : Header(Header), File(File), SectionKind(SectionKind) {}
29 
30 template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const {
31   ErrorOr<StringRef> Name = File->getObj().getSectionName(this->Header);
32   error(Name);
33   return *Name;
34 }
35 
36 template <class ELFT>
37 ArrayRef<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 
44 template <class ELFT>
45 typename ELFFile<ELFT>::uintX_t
46 InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) {
47   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 
52 template <class ELFT>
53 InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header)
54     : InputSectionBase<ELFT>(F, Header, Base::Regular) {}
55 
56 template <class ELFT>
57 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
58   return S->SectionKind == Base::Regular;
59 }
60 
61 template <class ELFT>
62 template <bool isRela>
63 void InputSection<ELFT>::relocate(
64     uint8_t *Buf, uint8_t *BufEnd,
65     iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels,
66     const ObjectFile<ELFT> &File, uintX_t BaseAddr) {
67   typedef Elf_Rel_Impl<ELFT, isRela> RelType;
68   for (const RelType &RI : Rels) {
69     uint32_t SymIndex = RI.getSymbol(Config->Mips64EL);
70     uint32_t Type = RI.getType(Config->Mips64EL);
71 
72     // Handle relocations for local symbols -- they never get
73     // resolved so we don't allocate a SymbolBody.
74     const Elf_Shdr *SymTab = File.getSymbolTable();
75     if (SymIndex < SymTab->sh_info) {
76       uintX_t SymVA = getLocalRelTarget(File, RI);
77       Target->relocateOne(Buf + RI.r_offset, BufEnd, Type,
78                           BaseAddr + RI.r_offset, SymVA);
79       continue;
80     }
81 
82     SymbolBody &Body = *File.getSymbolBody(SymIndex)->repl();
83     uintX_t SymVA = getSymVA<ELFT>(Body);
84     if (Target->relocNeedsPlt(Type, Body)) {
85       SymVA = Out<ELFT>::Plt->getEntryAddr(Body);
86       Type = Target->getPLTRefReloc(Type);
87     } else if (Target->relocNeedsGot(Type, Body)) {
88       SymVA = Out<ELFT>::Got->getEntryAddr(Body);
89       Type = Target->getGotRefReloc();
90     } else if (Target->relocPointsToGot(Type)) {
91       SymVA = Out<ELFT>::Got->getVA();
92       Type = Target->getPCRelReloc();
93     } else if (isa<SharedSymbol<ELFT>>(Body)) {
94       continue;
95     }
96     Target->relocateOne(Buf + RI.r_offset, BufEnd, Type, BaseAddr + RI.r_offset,
97                         SymVA + getAddend<ELFT>(RI));
98   }
99 }
100 
101 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
102   if (this->Header->sh_type == SHT_NOBITS)
103     return;
104   // Copy section contents from source object file to output file.
105   ArrayRef<uint8_t> Data = this->getSectionData();
106   memcpy(Buf + OutSecOff, Data.data(), Data.size());
107 
108   ELFFile<ELFT> &EObj = this->File->getObj();
109   uint8_t *Base = Buf + OutSecOff;
110   uintX_t BaseAddr = this->OutSec->getVA() + OutSecOff;
111   // Iterate over all relocation sections that apply to this section.
112   for (const Elf_Shdr *RelSec : RelocSections) {
113     if (RelSec->sh_type == SHT_RELA)
114       relocate(Base, Base + Data.size(), EObj.relas(RelSec), *this->File,
115                BaseAddr);
116     else
117       relocate(Base, Base + Data.size(), EObj.rels(RelSec), *this->File,
118                BaseAddr);
119   }
120 }
121 
122 template <class ELFT>
123 MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
124                                            const Elf_Shdr *Header)
125     : InputSectionBase<ELFT>(F, Header, Base::Merge) {}
126 
127 template <class ELFT>
128 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
129   return S->SectionKind == Base::Merge;
130 }
131 
132 template <class ELFT>
133 typename MergeInputSection<ELFT>::uintX_t
134 MergeInputSection<ELFT>::getOffset(uintX_t Offset) {
135   ArrayRef<uint8_t> D = this->getSectionData();
136   StringRef Data((const char *)D.data(), D.size());
137   uintX_t Size = Data.size();
138   if (Offset >= Size)
139     error("Entry is past the end of the section");
140 
141   // Find the element this offset points to.
142   auto I = std::upper_bound(
143       this->Offsets.begin(), this->Offsets.end(), Offset,
144       [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) {
145         return A < B.first;
146       });
147   size_t End = I == this->Offsets.end() ? Data.size() : I->first;
148   --I;
149   uintX_t Start = I->first;
150 
151   // Compute the Addend and if the Base is cached, return.
152   uintX_t Addend = Offset - Start;
153   uintX_t &Base = I->second;
154   if (Base != uintX_t(-1))
155     return Base + Addend;
156 
157   // Map the base to the offset in the output section and cashe it.
158   StringRef Entry = Data.substr(Start, End - Start);
159   Base =
160       static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry);
161   return Base + Addend;
162 }
163 
164 namespace lld {
165 namespace elf2 {
166 template class InputSectionBase<object::ELF32LE>;
167 template class InputSectionBase<object::ELF32BE>;
168 template class InputSectionBase<object::ELF64LE>;
169 template class InputSectionBase<object::ELF64BE>;
170 
171 template class InputSection<object::ELF32LE>;
172 template class InputSection<object::ELF32BE>;
173 template class InputSection<object::ELF64LE>;
174 template class InputSection<object::ELF64BE>;
175 
176 template class MergeInputSection<object::ELF32LE>;
177 template class MergeInputSection<object::ELF32BE>;
178 template class MergeInputSection<object::ELF64LE>;
179 template class MergeInputSection<object::ELF64BE>;
180 }
181 }
182