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 "Error.h" 12 #include "InputFiles.h" 13 #include "OutputSections.h" 14 #include "Target.h" 15 16 using namespace llvm; 17 using namespace llvm::ELF; 18 using namespace llvm::object; 19 20 using namespace lld; 21 using namespace lld::elf2; 22 23 template <class ELFT> 24 InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) 25 : File(F), Header(Header) {} 26 27 template <class ELFT> 28 template <bool isRela> 29 void InputSection<ELFT>::relocate( 30 uint8_t *Buf, iterator_range<const Elf_Rel_Impl<ELFT, isRela> *> Rels, 31 const ObjectFile<ELFT> &File, uintX_t BaseAddr, 32 const OutputSection<ELFT> &BssSec, const PltSection<ELFT> &PltSec, 33 const GotSection<ELFT> &GotSec) { 34 typedef Elf_Rel_Impl<ELFT, isRela> RelType; 35 bool IsMips64EL = File.getObj()->isMips64EL(); 36 for (const RelType &RI : Rels) { 37 uint32_t SymIndex = RI.getSymbol(IsMips64EL); 38 uint32_t Type = RI.getType(IsMips64EL); 39 uintX_t SymVA; 40 41 // Handle relocations for local symbols -- they never get 42 // resolved so we don't allocate a SymbolBody. 43 const Elf_Shdr *SymTab = File.getSymbolTable(); 44 if (SymIndex < SymTab->sh_info) { 45 const Elf_Sym *Sym = File.getObj()->getRelocationSymbol(&RI, SymTab); 46 if (!Sym) 47 continue; 48 SymVA = getLocalSymVA(Sym, File); 49 } else { 50 const SymbolBody *Body = File.getSymbolBody(SymIndex); 51 if (!Body) 52 continue; 53 uint32_t OrigType = Type; 54 switch (Body->kind()) { 55 case SymbolBody::DefinedRegularKind: 56 SymVA = getSymVA<ELFT>(cast<DefinedRegular<ELFT>>(Body)); 57 break; 58 case SymbolBody::DefinedAbsoluteKind: 59 SymVA = cast<DefinedAbsolute<ELFT>>(Body)->Sym.st_value; 60 break; 61 case SymbolBody::DefinedCommonKind: { 62 auto *DC = cast<DefinedCommon<ELFT>>(Body); 63 SymVA = BssSec.getVA() + DC->OffsetInBSS; 64 break; 65 } 66 case SymbolBody::SharedKind: 67 if (Target->relocNeedsPlt(Type)) 68 Type = Target->getPCRelReloc(); 69 else if (Target->relocNeedsGot(Type)) 70 Type = Target->getPCRelReloc(); 71 else 72 continue; 73 break; 74 case SymbolBody::UndefinedKind: 75 assert(Body->isWeak() && "Undefined symbol reached writer"); 76 SymVA = 0; 77 break; 78 case SymbolBody::LazyKind: 79 llvm_unreachable("Lazy symbol reached writer"); 80 } 81 82 if (Target->relocNeedsPlt(OrigType)) 83 SymVA = PltSec.getEntryAddr(*Body); 84 else if (Target->relocNeedsGot(OrigType)) 85 SymVA = GotSec.getEntryAddr(*Body); 86 } 87 88 Target->relocateOne(Buf, reinterpret_cast<const void *>(&RI), Type, 89 BaseAddr, SymVA); 90 } 91 } 92 93 template <class ELFT> 94 void InputSection<ELFT>::writeTo(uint8_t *Buf, 95 const OutputSection<ELFT> &BssSec, 96 const PltSection<ELFT> &PltSec, 97 const GotSection<ELFT> &GotSec) { 98 if (Header->sh_type == SHT_NOBITS) 99 return; 100 // Copy section contents from source object file to output file. 101 ArrayRef<uint8_t> Data = *File->getObj()->getSectionContents(Header); 102 memcpy(Buf + OutputSectionOff, Data.data(), Data.size()); 103 104 const ObjectFile<ELFT> *File = getFile(); 105 ELFFile<ELFT> *EObj = File->getObj(); 106 uint8_t *Base = Buf + getOutputSectionOff(); 107 uintX_t BaseAddr = Out->getVA() + getOutputSectionOff(); 108 // Iterate over all relocation sections that apply to this section. 109 for (const Elf_Shdr *RelSec : RelocSections) { 110 if (RelSec->sh_type == SHT_RELA) 111 relocate(Base, EObj->relas(RelSec), *File, BaseAddr, BssSec, PltSec, 112 GotSec); 113 else 114 relocate(Base, EObj->rels(RelSec), *File, BaseAddr, BssSec, PltSec, 115 GotSec); 116 } 117 } 118 119 template <class ELFT> StringRef InputSection<ELFT>::getSectionName() const { 120 ErrorOr<StringRef> Name = File->getObj()->getSectionName(Header); 121 error(Name); 122 return *Name; 123 } 124 125 namespace lld { 126 namespace elf2 { 127 template class InputSection<object::ELF32LE>; 128 template class InputSection<object::ELF32BE>; 129 template class InputSection<object::ELF64LE>; 130 template class InputSection<object::ELF64BE>; 131 } 132 } 133