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(uintX_t Offset) { 47 switch (SectionKind) { 48 case Regular: 49 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; 50 case EHFrame: 51 return cast<EHInputSection<ELFT>>(this)->getOffset(Offset); 52 case Merge: 53 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); 54 } 55 llvm_unreachable("Invalid section kind"); 56 } 57 58 template <class ELFT> 59 typename ELFFile<ELFT>::uintX_t 60 InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) { 61 return getOffset(Sym.st_value); 62 } 63 64 // Returns a section that Rel relocation is pointing to. 65 template <class ELFT> 66 InputSectionBase<ELFT> * 67 InputSectionBase<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 80 template <class ELFT> 81 InputSectionBase<ELFT> * 82 InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) { 83 return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel)); 84 } 85 86 template <class ELFT> 87 InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) 88 : InputSectionBase<ELFT>(F, Header, Base::Regular) {} 89 90 template <class ELFT> 91 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 92 return S->SectionKind == Base::Regular; 93 } 94 95 template <class ELFT> 96 template <bool isRela> 97 uint8_t * 98 InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t Type, 99 RelIteratorRange<isRela> Rels) { 100 // Some MIPS relocations use addend calculated from addend of the relocation 101 // itself and addend of paired relocation. ABI requires to compute such 102 // combined addend in case of REL relocation record format only. 103 // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 104 if (isRela || Config->EMachine != EM_MIPS) 105 return nullptr; 106 if (Type == R_MIPS_HI16) 107 Type = R_MIPS_LO16; 108 else if (Type == R_MIPS_PCHI16) 109 Type = R_MIPS_PCLO16; 110 else if (Type == R_MICROMIPS_HI16) 111 Type = R_MICROMIPS_LO16; 112 else 113 return nullptr; 114 for (const auto &RI : Rels) { 115 if (RI.getType(Config->Mips64EL) != Type) 116 continue; 117 uintX_t Offset = getOffset(RI.r_offset); 118 if (Offset == (uintX_t)-1) 119 return nullptr; 120 return Buf + Offset; 121 } 122 return nullptr; 123 } 124 125 template <class ELFT> 126 template <bool isRela> 127 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd, 128 RelIteratorRange<isRela> Rels) { 129 typedef Elf_Rel_Impl<ELFT, isRela> RelType; 130 size_t Num = Rels.end() - Rels.begin(); 131 for (size_t I = 0; I < Num; ++I) { 132 const RelType &RI = *(Rels.begin() + I); 133 uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); 134 uint32_t Type = RI.getType(Config->Mips64EL); 135 uintX_t Offset = getOffset(RI.r_offset); 136 if (Offset == (uintX_t)-1) 137 continue; 138 139 uint8_t *BufLoc = Buf + Offset; 140 uintX_t AddrLoc = OutSec->getVA() + Offset; 141 auto NextRelocs = llvm::make_range(&RI, Rels.end()); 142 143 if (Target->isTlsLocalDynamicReloc(Type) && 144 !Target->isTlsOptimized(Type, nullptr)) { 145 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, 146 Out<ELFT>::Got->getLocalTlsIndexVA() + 147 getAddend<ELFT>(RI)); 148 continue; 149 } 150 151 // Handle relocations for local symbols -- they never get 152 // resolved so we don't allocate a SymbolBody. 153 const Elf_Shdr *SymTab = File->getSymbolTable(); 154 if (SymIndex < SymTab->sh_info) { 155 uintX_t SymVA = getLocalRelTarget(*File, RI); 156 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 157 findMipsPairedReloc(Buf, Type, NextRelocs)); 158 continue; 159 } 160 161 SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); 162 163 if (Target->isTlsGlobalDynamicReloc(Type) && 164 !Target->isTlsOptimized(Type, &Body)) { 165 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, 166 Out<ELFT>::Got->getGlobalDynAddr(Body) + 167 getAddend<ELFT>(RI)); 168 continue; 169 } 170 171 if (Target->isTlsOptimized(Type, &Body)) { 172 uintX_t SymVA = Target->relocNeedsGot(Type, Body) 173 ? Out<ELFT>::Got->getEntryAddr(Body) 174 : getSymVA<ELFT>(Body); 175 // By optimizing TLS relocations, it is sometimes needed to skip 176 // relocations that immediately follow TLS relocations. This function 177 // knows how many slots we need to skip. 178 I += Target->relocateTlsOptimize(BufLoc, BufEnd, Type, AddrLoc, SymVA, 179 Body); 180 continue; 181 } 182 183 uintX_t SymVA = getSymVA<ELFT>(Body); 184 if (Target->relocNeedsPlt(Type, Body)) { 185 SymVA = Out<ELFT>::Plt->getEntryAddr(Body); 186 Type = Target->getPltRefReloc(Type); 187 } else if (Target->relocNeedsGot(Type, Body)) { 188 SymVA = Out<ELFT>::Got->getEntryAddr(Body); 189 if (Body.isTLS()) 190 Type = Target->getTlsGotReloc(); 191 } else if (!Target->relocNeedsCopy(Type, Body) && 192 isa<SharedSymbol<ELFT>>(Body)) { 193 continue; 194 } else if (Target->isTlsDynReloc(Type)) { 195 continue; 196 } 197 Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, 198 SymVA + getAddend<ELFT>(RI), 199 findMipsPairedReloc(Buf, Type, NextRelocs)); 200 } 201 } 202 203 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { 204 if (this->Header->sh_type == SHT_NOBITS) 205 return; 206 // Copy section contents from source object file to output file. 207 ArrayRef<uint8_t> Data = this->getSectionData(); 208 memcpy(Buf + OutSecOff, Data.data(), Data.size()); 209 210 ELFFile<ELFT> &EObj = this->File->getObj(); 211 uint8_t *BufEnd = Buf + OutSecOff + Data.size(); 212 // Iterate over all relocation sections that apply to this section. 213 for (const Elf_Shdr *RelSec : this->RelocSections) { 214 if (RelSec->sh_type == SHT_RELA) 215 this->relocate(Buf, BufEnd, EObj.relas(RelSec)); 216 else 217 this->relocate(Buf, BufEnd, EObj.rels(RelSec)); 218 } 219 } 220 221 template <class ELFT> 222 SplitInputSection<ELFT>::SplitInputSection( 223 ObjectFile<ELFT> *File, const Elf_Shdr *Header, 224 typename InputSectionBase<ELFT>::Kind SectionKind) 225 : InputSectionBase<ELFT>(File, Header, SectionKind) {} 226 227 template <class ELFT> 228 EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F, 229 const Elf_Shdr *Header) 230 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) {} 231 232 template <class ELFT> 233 bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 234 return S->SectionKind == InputSectionBase<ELFT>::EHFrame; 235 } 236 237 template <class ELFT> 238 typename EHInputSection<ELFT>::uintX_t 239 EHInputSection<ELFT>::getOffset(uintX_t Offset) { 240 std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first; 241 uintX_t Base = I->second; 242 if (Base == uintX_t(-1)) 243 return -1; // Not in the output 244 245 uintX_t Addend = Offset - I->first; 246 return Base + Addend; 247 } 248 249 template <class ELFT> 250 MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F, 251 const Elf_Shdr *Header) 252 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {} 253 254 template <class ELFT> 255 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 256 return S->SectionKind == InputSectionBase<ELFT>::Merge; 257 } 258 259 template <class ELFT> 260 std::pair<std::pair<typename ELFFile<ELFT>::uintX_t, 261 typename ELFFile<ELFT>::uintX_t> *, 262 typename ELFFile<ELFT>::uintX_t> 263 SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) { 264 ArrayRef<uint8_t> D = this->getSectionData(); 265 StringRef Data((const char *)D.data(), D.size()); 266 uintX_t Size = Data.size(); 267 if (Offset >= Size) 268 error("Entry is past the end of the section"); 269 270 // Find the element this offset points to. 271 auto I = std::upper_bound( 272 Offsets.begin(), Offsets.end(), Offset, 273 [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) { 274 return A < B.first; 275 }); 276 uintX_t End = I == Offsets.end() ? Data.size() : I->first; 277 --I; 278 return std::make_pair(&*I, End); 279 } 280 281 template <class ELFT> 282 typename MergeInputSection<ELFT>::uintX_t 283 MergeInputSection<ELFT>::getOffset(uintX_t Offset) { 284 std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T = 285 this->getRangeAndSize(Offset); 286 std::pair<uintX_t, uintX_t> *I = T.first; 287 uintX_t End = T.second; 288 uintX_t Start = I->first; 289 290 // Compute the Addend and if the Base is cached, return. 291 uintX_t Addend = Offset - Start; 292 uintX_t &Base = I->second; 293 if (Base != uintX_t(-1)) 294 return Base + Addend; 295 296 // Map the base to the offset in the output section and cache it. 297 ArrayRef<uint8_t> D = this->getSectionData(); 298 StringRef Data((const char *)D.data(), D.size()); 299 StringRef Entry = Data.substr(Start, End - Start); 300 Base = 301 static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry); 302 return Base + Addend; 303 } 304 305 namespace lld { 306 namespace elf2 { 307 template class InputSectionBase<object::ELF32LE>; 308 template class InputSectionBase<object::ELF32BE>; 309 template class InputSectionBase<object::ELF64LE>; 310 template class InputSectionBase<object::ELF64BE>; 311 312 template class InputSection<object::ELF32LE>; 313 template class InputSection<object::ELF32BE>; 314 template class InputSection<object::ELF64LE>; 315 template class InputSection<object::ELF64BE>; 316 317 template class EHInputSection<object::ELF32LE>; 318 template class EHInputSection<object::ELF32BE>; 319 template class EHInputSection<object::ELF64LE>; 320 template class EHInputSection<object::ELF64BE>; 321 322 template class MergeInputSection<object::ELF32LE>; 323 template class MergeInputSection<object::ELF32BE>; 324 template class MergeInputSection<object::ELF64LE>; 325 template class MergeInputSection<object::ELF64BE>; 326 } 327 } 328