Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 1 | //===- InputSection.cpp ---------------------------------------------------===// |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 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 | |
Rafael Espindola | 9d06ab6 | 2015-09-22 00:01:39 +0000 | [diff] [blame] | 10 | #include "InputSection.h" |
Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 11 | #include "Error.h" |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 12 | #include "InputFiles.h" |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 13 | #include "OutputSections.h" |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 14 | #include "Target.h" |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 15 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 16 | using namespace llvm; |
| 17 | using namespace llvm::ELF; |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 18 | using namespace llvm::object; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace lld; |
| 21 | using namespace lld::elf2; |
| 22 | |
| 23 | template <class ELFT> |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 24 | InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 25 | : File(F), Header(Header) {} |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 26 | |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 27 | template <class ELFT> |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 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, |
Michael J. Spencer | 2812aa8 | 2015-09-23 16:57:31 +0000 | [diff] [blame] | 32 | const OutputSection<ELFT> &BssSec, const PltSection<ELFT> &PltSec, |
| 33 | const GotSection<ELFT> &GotSec) { |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 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; |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 53 | uint32_t OrigType = Type; |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 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); |
Michael J. Spencer | 2812aa8 | 2015-09-23 16:57:31 +0000 | [diff] [blame] | 63 | SymVA = BssSec.getVA() + DC->OffsetInBSS; |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 64 | break; |
| 65 | } |
| 66 | case SymbolBody::SharedKind: |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 67 | if (Target->relocNeedsPlt(Type)) |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 68 | Type = Target->getPCRelReloc(); |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 69 | else if (Target->relocNeedsGot(Type)) |
Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 70 | Type = Target->getPCRelReloc(); |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 71 | else |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 72 | continue; |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 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 | } |
Rafael Espindola | cdfecff | 2015-09-23 20:08:25 +0000 | [diff] [blame] | 81 | |
| 82 | if (Target->relocNeedsPlt(OrigType)) |
| 83 | SymVA = PltSec.getEntryAddr(*Body); |
| 84 | else if (Target->relocNeedsGot(OrigType)) |
| 85 | SymVA = GotSec.getEntryAddr(*Body); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Rafael Espindola | c401088 | 2015-09-22 20:54:08 +0000 | [diff] [blame] | 88 | Target->relocateOne(Buf, reinterpret_cast<const void *>(&RI), Type, |
| 89 | BaseAddr, SymVA); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
| 93 | template <class ELFT> |
Michael J. Spencer | 2812aa8 | 2015-09-23 16:57:31 +0000 | [diff] [blame] | 94 | void InputSection<ELFT>::writeTo(uint8_t *Buf, |
| 95 | const OutputSection<ELFT> &BssSec, |
| 96 | const PltSection<ELFT> &PltSec, |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 97 | const GotSection<ELFT> &GotSec) { |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 98 | if (Header->sh_type == SHT_NOBITS) |
| 99 | return; |
| 100 | // Copy section contents from source object file to output file. |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 101 | ArrayRef<uint8_t> Data = *File->getObj()->getSectionContents(Header); |
Rafael Espindola | 674b5d5 | 2015-08-13 15:54:36 +0000 | [diff] [blame] | 102 | memcpy(Buf + OutputSectionOff, Data.data(), Data.size()); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 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) |
Michael J. Spencer | 2812aa8 | 2015-09-23 16:57:31 +0000 | [diff] [blame] | 111 | relocate(Base, EObj->relas(RelSec), *File, BaseAddr, BssSec, PltSec, |
| 112 | GotSec); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 113 | else |
Michael J. Spencer | 2812aa8 | 2015-09-23 16:57:31 +0000 | [diff] [blame] | 114 | relocate(Base, EObj->rels(RelSec), *File, BaseAddr, BssSec, PltSec, |
| 115 | GotSec); |
Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 116 | } |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 119 | template <class ELFT> StringRef InputSection<ELFT>::getSectionName() const { |
Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 120 | ErrorOr<StringRef> Name = File->getObj()->getSectionName(Header); |
Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 121 | error(Name); |
| 122 | return *Name; |
| 123 | } |
| 124 | |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 125 | namespace lld { |
| 126 | namespace elf2 { |
Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 127 | template class InputSection<object::ELF32LE>; |
| 128 | template class InputSection<object::ELF32BE>; |
| 129 | template class InputSection<object::ELF64LE>; |
| 130 | template class InputSection<object::ELF64BE>; |
Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 131 | } |
| 132 | } |