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) const {
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, BufEnd, &RI, Type, BaseAddr, SymVA);
78       continue;
79     }
80 
81     SymbolBody &Body = *File.getSymbolBody(SymIndex)->repl();
82     uintX_t SymVA = getSymVA<ELFT>(Body);
83     if (Target->relocNeedsPlt(Type, Body)) {
84       SymVA = Out<ELFT>::Plt->getEntryAddr(Body);
85       Type = Target->getPLTRefReloc(Type);
86     } else if (Target->relocNeedsGot(Type, Body)) {
87       SymVA = Out<ELFT>::Got->getEntryAddr(Body);
88       Type = Target->getGotRefReloc();
89     } else if (Target->relocPointsToGot(Type)) {
90       SymVA = Out<ELFT>::Got->getVA();
91       Type = Target->getPCRelReloc();
92     } else if (isa<SharedSymbol<ELFT>>(Body)) {
93       continue;
94     }
95     Target->relocateOne(Buf, BufEnd, &RI, Type, BaseAddr,
96                         SymVA + getAddend<ELFT>(RI));
97   }
98 }
99 
100 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) {
101   if (this->Header->sh_type == SHT_NOBITS)
102     return;
103   // Copy section contents from source object file to output file.
104   ArrayRef<uint8_t> Data = this->getSectionData();
105   memcpy(Buf + OutSecOff, Data.data(), Data.size());
106 
107   ELFFile<ELFT> &EObj = this->File->getObj();
108   uint8_t *Base = Buf + OutSecOff;
109   uintX_t BaseAddr = this->OutSec->getVA() + OutSecOff;
110   // Iterate over all relocation sections that apply to this section.
111   for (const Elf_Shdr *RelSec : RelocSections) {
112     if (RelSec->sh_type == SHT_RELA)
113       relocate(Base, Base + Data.size(), EObj.relas(RelSec), *this->File,
114                BaseAddr);
115     else
116       relocate(Base, Base + Data.size(), EObj.rels(RelSec), *this->File,
117                BaseAddr);
118   }
119 }
120 
121 template <class ELFT>
122 MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F,
123                                            const Elf_Shdr *Header)
124     : InputSectionBase<ELFT>(F, Header, Base::Merge) {}
125 
126 template <class ELFT>
127 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) {
128   return S->SectionKind == Base::Merge;
129 }
130 
131 // FIXME: Optimize this by keeping an offset for each element.
132 template <class ELFT>
133 typename MergeInputSection<ELFT>::uintX_t
134 MergeInputSection<ELFT>::getOffset(uintX_t Offset) const {
135   ArrayRef<uint8_t> Data = this->getSectionData();
136   uintX_t EntSize = this->Header->sh_entsize;
137   uintX_t Addend = Offset % EntSize;
138   Offset -= Addend;
139   if (Offset + EntSize > Data.size())
140     error("Entry is past the end of the section");
141   Data = Data.slice(Offset, EntSize);
142   return static_cast<MergeOutputSection<ELFT> *>(this->OutSec)
143              ->getOffset(Data) +
144          Addend;
145 }
146 
147 namespace lld {
148 namespace elf2 {
149 template class InputSectionBase<object::ELF32LE>;
150 template class InputSectionBase<object::ELF32BE>;
151 template class InputSectionBase<object::ELF64LE>;
152 template class InputSectionBase<object::ELF64BE>;
153 
154 template class InputSection<object::ELF32LE>;
155 template class InputSection<object::ELF32BE>;
156 template class InputSection<object::ELF64LE>;
157 template class InputSection<object::ELF64BE>;
158 
159 template class MergeInputSection<object::ELF32LE>;
160 template class MergeInputSection<object::ELF32BE>;
161 template class MergeInputSection<object::ELF64LE>;
162 template class MergeInputSection<object::ELF64BE>;
163 }
164 }
165