| 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 | 551dfd8 | 2015-09-25 19:24:57 +0000 | [diff] [blame] | 11 | #include "Config.h" | 
| Rafael Espindola | 192e1fa | 2015-08-06 15:08:23 +0000 | [diff] [blame] | 12 | #include "Error.h" | 
| Michael J. Spencer | 67bc8d6 | 2015-08-27 23:15:56 +0000 | [diff] [blame] | 13 | #include "InputFiles.h" | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 14 | #include "OutputSections.h" | 
| Rafael Espindola | 01205f7 | 2015-09-22 18:19:46 +0000 | [diff] [blame] | 15 | #include "Target.h" | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 16 |  | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 17 | using namespace llvm; | 
|  | 18 | using namespace llvm::ELF; | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 19 | using namespace llvm::object; | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 20 |  | 
|  | 21 | using namespace lld; | 
|  | 22 | using namespace lld::elf2; | 
|  | 23 |  | 
|  | 24 | template <class ELFT> | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 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); | 
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 32 | fatal(Name); | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 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); | 
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 40 | fatal(Ret); | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 41 | return *Ret; | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | template <class ELFT> | 
|  | 45 | typename ELFFile<ELFT>::uintX_t | 
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 46 | InputSectionBase<ELFT>::getOffset(uintX_t Offset) { | 
|  | 47 | switch (SectionKind) { | 
|  | 48 | case Regular: | 
|  | 49 | return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 50 | case EHFrame: | 
|  | 51 | return cast<EHInputSection<ELFT>>(this)->getOffset(Offset); | 
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 52 | case Merge: | 
|  | 53 | return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); | 
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 54 | case MipsReginfo: | 
| Rui Ueyama | 58a636a | 2016-01-06 22:01:25 +0000 | [diff] [blame] | 55 | // MIPS .reginfo sections are consumed by the linker, | 
|  | 56 | // so it should never be copied to output. | 
|  | 57 | llvm_unreachable("MIPS .reginfo reached writeTo()."); | 
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 58 | } | 
| Denis Protivensky | 1b1b34e | 2015-11-12 09:11:20 +0000 | [diff] [blame] | 59 | llvm_unreachable("Invalid section kind"); | 
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
|  | 62 | template <class ELFT> | 
|  | 63 | typename ELFFile<ELFT>::uintX_t | 
| Rafael Espindola | 48225b4 | 2015-10-23 19:55:11 +0000 | [diff] [blame] | 64 | InputSectionBase<ELFT>::getOffset(const Elf_Sym &Sym) { | 
| Rafael Espindola | db9bf4d | 2015-11-11 16:50:37 +0000 | [diff] [blame] | 65 | return getOffset(Sym.st_value); | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 66 | } | 
|  | 67 |  | 
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 68 | // Returns a section that Rel relocation is pointing to. | 
|  | 69 | template <class ELFT> | 
|  | 70 | InputSectionBase<ELFT> * | 
|  | 71 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rel &Rel) { | 
|  | 72 | // Global symbol | 
|  | 73 | uint32_t SymIndex = Rel.getSymbol(Config->Mips64EL); | 
|  | 74 | if (SymbolBody *B = File->getSymbolBody(SymIndex)) | 
|  | 75 | if (auto *D = dyn_cast<DefinedRegular<ELFT>>(B->repl())) | 
| Rafael Espindola | 02ce26a | 2015-12-24 14:22:24 +0000 | [diff] [blame] | 76 | return D->Section; | 
| Rui Ueyama | 1250464 | 2015-10-27 21:51:13 +0000 | [diff] [blame] | 77 | // Local symbol | 
|  | 78 | if (const Elf_Sym *Sym = File->getLocalSymbol(SymIndex)) | 
|  | 79 | if (InputSectionBase<ELFT> *Sec = File->getSection(*Sym)) | 
|  | 80 | return Sec; | 
|  | 81 | return nullptr; | 
|  | 82 | } | 
|  | 83 |  | 
|  | 84 | template <class ELFT> | 
|  | 85 | InputSectionBase<ELFT> * | 
|  | 86 | InputSectionBase<ELFT>::getRelocTarget(const Elf_Rela &Rel) { | 
|  | 87 | return getRelocTarget(reinterpret_cast<const Elf_Rel &>(Rel)); | 
|  | 88 | } | 
|  | 89 |  | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 90 | template <class ELFT> | 
| Rafael Espindola | 53d5cea | 2015-09-21 17:47:00 +0000 | [diff] [blame] | 91 | InputSection<ELFT>::InputSection(ObjectFile<ELFT> *F, const Elf_Shdr *Header) | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 92 | : InputSectionBase<ELFT>(F, Header, Base::Regular) {} | 
|  | 93 |  | 
|  | 94 | template <class ELFT> | 
|  | 95 | bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { | 
|  | 96 | return S->SectionKind == Base::Regular; | 
|  | 97 | } | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 98 |  | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 99 | template <class ELFT> | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 100 | template <bool isRela> | 
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 101 | uint8_t * | 
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 102 | InputSectionBase<ELFT>::findMipsPairedReloc(uint8_t *Buf, uint32_t SymIndex, | 
|  | 103 | uint32_t Type, | 
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 104 | RelIteratorRange<isRela> Rels) { | 
|  | 105 | // Some MIPS relocations use addend calculated from addend of the relocation | 
|  | 106 | // itself and addend of paired relocation. ABI requires to compute such | 
|  | 107 | // combined addend in case of REL relocation record format only. | 
|  | 108 | // See p. 4-17 at ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf | 
|  | 109 | if (isRela || Config->EMachine != EM_MIPS) | 
|  | 110 | return nullptr; | 
|  | 111 | if (Type == R_MIPS_HI16) | 
|  | 112 | Type = R_MIPS_LO16; | 
|  | 113 | else if (Type == R_MIPS_PCHI16) | 
|  | 114 | Type = R_MIPS_PCLO16; | 
|  | 115 | else if (Type == R_MICROMIPS_HI16) | 
|  | 116 | Type = R_MICROMIPS_LO16; | 
|  | 117 | else | 
|  | 118 | return nullptr; | 
|  | 119 | for (const auto &RI : Rels) { | 
|  | 120 | if (RI.getType(Config->Mips64EL) != Type) | 
|  | 121 | continue; | 
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 122 | if (RI.getSymbol(Config->Mips64EL) != SymIndex) | 
|  | 123 | continue; | 
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 124 | uintX_t Offset = getOffset(RI.r_offset); | 
|  | 125 | if (Offset == (uintX_t)-1) | 
|  | 126 | return nullptr; | 
|  | 127 | return Buf + Offset; | 
|  | 128 | } | 
|  | 129 | return nullptr; | 
|  | 130 | } | 
|  | 131 |  | 
|  | 132 | template <class ELFT> | 
|  | 133 | template <bool isRela> | 
|  | 134 | void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd, | 
|  | 135 | RelIteratorRange<isRela> Rels) { | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 136 | typedef Elf_Rel_Impl<ELFT, isRela> RelType; | 
| George Rimar | 6713cf8 | 2015-11-25 21:46:05 +0000 | [diff] [blame] | 137 | size_t Num = Rels.end() - Rels.begin(); | 
|  | 138 | for (size_t I = 0; I < Num; ++I) { | 
|  | 139 | const RelType &RI = *(Rels.begin() + I); | 
| Rui Ueyama | 6455852 | 2015-10-16 22:51:43 +0000 | [diff] [blame] | 140 | uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); | 
|  | 141 | uint32_t Type = RI.getType(Config->Mips64EL); | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 142 | uintX_t Offset = getOffset(RI.r_offset); | 
|  | 143 | if (Offset == (uintX_t)-1) | 
|  | 144 | continue; | 
|  | 145 |  | 
|  | 146 | uint8_t *BufLoc = Buf + Offset; | 
|  | 147 | uintX_t AddrLoc = OutSec->getVA() + Offset; | 
| Simon Atanasyan | 09b3e36 | 2015-12-01 21:24:45 +0000 | [diff] [blame] | 148 | auto NextRelocs = llvm::make_range(&RI, Rels.end()); | 
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 149 |  | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 150 | if (Target->isTlsLocalDynamicRel(Type) && | 
| Rui Ueyama | baf1651 | 2016-01-29 00:20:12 +0000 | [diff] [blame] | 151 | !Target->canRelaxTls(Type, nullptr)) { | 
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 152 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, | 
| Rui Ueyama | 0e53c7d | 2016-02-05 00:10:02 +0000 | [diff] [blame] | 153 | Out<ELFT>::Got->getTlsIndexVA() + | 
| Michael J. Spencer | 1e22561 | 2015-11-11 01:00:24 +0000 | [diff] [blame] | 154 | getAddend<ELFT>(RI)); | 
|  | 155 | continue; | 
|  | 156 | } | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 157 |  | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 158 | const Elf_Shdr *SymTab = File->getSymbolTable(); | 
|  | 159 | SymbolBody *Body = nullptr; | 
|  | 160 | if (SymIndex >= SymTab->sh_info) | 
|  | 161 | Body = File->getSymbolBody(SymIndex)->repl(); | 
|  | 162 |  | 
| Rui Ueyama | baf1651 | 2016-01-29 00:20:12 +0000 | [diff] [blame] | 163 | if (Target->canRelaxTls(Type, Body)) { | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 164 | uintX_t SymVA; | 
|  | 165 | if (!Body) | 
|  | 166 | SymVA = getLocalRelTarget(*File, RI, 0); | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 167 | else if (Target->needsGot(Type, *Body)) | 
| Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 168 | SymVA = Body->getGotVA<ELFT>(); | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 169 | else | 
| Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 170 | SymVA = Body->getVA<ELFT>(); | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 171 | // By optimizing TLS relocations, it is sometimes needed to skip | 
|  | 172 | // relocations that immediately follow TLS relocations. This function | 
|  | 173 | // knows how many slots we need to skip. | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 174 | I += Target->relaxTls(BufLoc, BufEnd, Type, AddrLoc, SymVA, Body); | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 175 | continue; | 
|  | 176 | } | 
|  | 177 |  | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 178 | // Handle relocations for local symbols -- they never get | 
|  | 179 | // resolved so we don't allocate a SymbolBody. | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 180 | uintX_t A = getAddend<ELFT>(RI); | 
|  | 181 | if (!Body) { | 
|  | 182 | uintX_t SymVA = getLocalRelTarget(*File, RI, A); | 
| Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 183 | if (Config->EMachine == EM_MIPS) { | 
|  | 184 | if (Type == R_MIPS_GPREL16 || Type == R_MIPS_GPREL32) | 
|  | 185 | // We need to adjust SymVA value in case of R_MIPS_GPREL16/32 | 
|  | 186 | // relocations because they use the following expression to calculate | 
|  | 187 | // the relocation's result for local symbol: S + A + GP0 - G. | 
|  | 188 | SymVA += File->getMipsGp0(); | 
|  | 189 | else if (Type == R_MIPS_GOT16) | 
|  | 190 | // R_MIPS_GOT16 relocation against local symbol requires index of | 
|  | 191 | // a local GOT entry which contains page address corresponds | 
|  | 192 | // to the symbol address. | 
|  | 193 | SymVA = Out<ELFT>::Got->getMipsLocalPageAddr(SymVA); | 
|  | 194 | } | 
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 195 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0, | 
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 196 | findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); | 
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 197 | continue; | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 198 | } | 
|  | 199 |  | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 200 | if (Target->isTlsGlobalDynamicRel(Type) && | 
| Rui Ueyama | baf1651 | 2016-01-29 00:20:12 +0000 | [diff] [blame] | 201 | !Target->canRelaxTls(Type, Body)) { | 
| Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 202 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 203 | Out<ELFT>::Got->getGlobalDynAddr(*Body) + | 
| Michael J. Spencer | 627ae70 | 2015-11-13 00:28:34 +0000 | [diff] [blame] | 204 | getAddend<ELFT>(RI)); | 
|  | 205 | continue; | 
|  | 206 | } | 
|  | 207 |  | 
| Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 208 | uintX_t SymVA = Body->getVA<ELFT>(); | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 209 | if (Target->needsPlt(Type, *Body)) { | 
| Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 210 | SymVA = Body->getPltVA<ELFT>(); | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 211 | } else if (Target->needsGot(Type, *Body)) { | 
| Simon Atanasyan | 4b03451 | 2016-02-04 11:51:45 +0000 | [diff] [blame] | 212 | if (Config->EMachine == EM_MIPS && !canBePreempted(Body, true)) | 
| Simon Atanasyan | 56ab5f0 | 2016-01-21 05:33:23 +0000 | [diff] [blame] | 213 | // Under some conditions relocations against non-local symbols require | 
|  | 214 | // entries in the local part of MIPS GOT. In that case we need an entry | 
|  | 215 | // initialized by full address of the symbol. | 
|  | 216 | SymVA = Out<ELFT>::Got->getMipsLocalFullAddr(*Body); | 
|  | 217 | else | 
| Rui Ueyama | b5a6970 | 2016-02-01 21:00:35 +0000 | [diff] [blame] | 218 | SymVA = Body->getGotVA<ELFT>(); | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 219 | if (Body->isTls()) | 
| Rui Ueyama | 724d625 | 2016-01-29 01:49:32 +0000 | [diff] [blame] | 220 | Type = Target->getTlsGotRel(Type); | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 221 | } else if (!Target->needsCopyRel(Type, *Body) && | 
|  | 222 | isa<SharedSymbol<ELFT>>(*Body)) { | 
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 223 | continue; | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 224 | } else if (Target->isTlsDynRel(Type, *Body)) { | 
| Rui Ueyama | 3a7c2f6 | 2016-01-08 00:13:23 +0000 | [diff] [blame] | 225 | continue; | 
| Rui Ueyama | c516ae1 | 2016-01-29 02:33:45 +0000 | [diff] [blame] | 226 | } else if (Target->isSizeRel(Type) && canBePreempted(Body, false)) { | 
| Rui Ueyama | 3a7c2f6 | 2016-01-08 00:13:23 +0000 | [diff] [blame] | 227 | // A SIZE relocation is supposed to set a symbol size, but if a symbol | 
|  | 228 | // can be preempted, the size at runtime may be different than link time. | 
|  | 229 | // If that's the case, we leave the field alone rather than filling it | 
|  | 230 | // with a possibly incorrect value. | 
| George Rimar | d23970f | 2015-11-25 20:41:53 +0000 | [diff] [blame] | 231 | continue; | 
| Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 232 | } else if (Config->EMachine == EM_MIPS) { | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 233 | if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp) | 
| Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 234 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc; | 
| George Rimar | 0b8ed1d | 2015-12-21 10:37:33 +0000 | [diff] [blame] | 235 | else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp) | 
| Simon Atanasyan | 09dae7c | 2015-12-16 14:45:09 +0000 | [diff] [blame] | 236 | SymVA = getMipsGpAddr<ELFT>() - AddrLoc + 4; | 
| Simon Atanasyan | 597df21 | 2016-02-04 12:09:49 +0000 | [diff] [blame] | 237 | else if (Body == Config->MipsLocalGp) | 
|  | 238 | SymVA = getMipsGpAddr<ELFT>(); | 
| Rui Ueyama | bb3c336 | 2015-10-12 20:28:23 +0000 | [diff] [blame] | 239 | } | 
| Rui Ueyama | 512c61d | 2016-02-03 00:12:24 +0000 | [diff] [blame] | 240 | uintX_t Size = Body->getSize<ELFT>(); | 
| George Rimar | 4865148 | 2015-12-11 08:59:37 +0000 | [diff] [blame] | 241 | Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A, | 
| Simon Atanasyan | dddbeb7 | 2015-12-13 06:49:08 +0000 | [diff] [blame] | 242 | findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 243 | } | 
|  | 244 | } | 
|  | 245 |  | 
| Rui Ueyama | 15ef5e1 | 2015-10-07 19:18:16 +0000 | [diff] [blame] | 246 | template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 247 | if (this->Header->sh_type == SHT_NOBITS) | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 248 | return; | 
|  | 249 | // Copy section contents from source object file to output file. | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 250 | ArrayRef<uint8_t> Data = this->getSectionData(); | 
| Rui Ueyama | 55c3f89 | 2015-10-15 01:58:40 +0000 | [diff] [blame] | 251 | memcpy(Buf + OutSecOff, Data.data(), Data.size()); | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 252 |  | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 253 | ELFFile<ELFT> &EObj = this->File->getObj(); | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 254 | uint8_t *BufEnd = Buf + OutSecOff + Data.size(); | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 255 | // Iterate over all relocation sections that apply to this section. | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 256 | for (const Elf_Shdr *RelSec : this->RelocSections) { | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 257 | if (RelSec->sh_type == SHT_RELA) | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 258 | this->relocate(Buf, BufEnd, EObj.relas(RelSec)); | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 259 | else | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 260 | this->relocate(Buf, BufEnd, EObj.rels(RelSec)); | 
| Rafael Espindola | 4ea0021 | 2015-09-21 22:01:00 +0000 | [diff] [blame] | 261 | } | 
| Michael J. Spencer | 84487f1 | 2015-07-24 21:03:07 +0000 | [diff] [blame] | 262 | } | 
|  | 263 |  | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 264 | template <class ELFT> | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 265 | SplitInputSection<ELFT>::SplitInputSection( | 
|  | 266 | ObjectFile<ELFT> *File, const Elf_Shdr *Header, | 
|  | 267 | typename InputSectionBase<ELFT>::Kind SectionKind) | 
|  | 268 | : InputSectionBase<ELFT>(File, Header, SectionKind) {} | 
|  | 269 |  | 
|  | 270 | template <class ELFT> | 
|  | 271 | EHInputSection<ELFT>::EHInputSection(ObjectFile<ELFT> *F, | 
|  | 272 | const Elf_Shdr *Header) | 
| Rui Ueyama | da73532 | 2015-12-24 10:08:54 +0000 | [diff] [blame] | 273 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) { | 
|  | 274 | // Mark .eh_frame sections as live by default because there are | 
|  | 275 | // usually no relocations that point to .eh_frames. Otherwise, | 
| George Rimar | e9e1d32 | 2016-02-18 15:17:01 +0000 | [diff] [blame^] | 276 | // the garbage collector would drop all .eh_frame sections. | 
| Rui Ueyama | da73532 | 2015-12-24 10:08:54 +0000 | [diff] [blame] | 277 | this->Live = true; | 
|  | 278 | } | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 279 |  | 
|  | 280 | template <class ELFT> | 
|  | 281 | bool EHInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { | 
|  | 282 | return S->SectionKind == InputSectionBase<ELFT>::EHFrame; | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | template <class ELFT> | 
|  | 286 | typename EHInputSection<ELFT>::uintX_t | 
|  | 287 | EHInputSection<ELFT>::getOffset(uintX_t Offset) { | 
| George Rimar | 6ab275c | 2015-12-25 09:51:42 +0000 | [diff] [blame] | 288 | // The file crtbeginT.o has relocations pointing to the start of an empty | 
|  | 289 | // .eh_frame that is known to be the first in the link. It does that to | 
|  | 290 | // identify the start of the output .eh_frame. Handle this special case. | 
|  | 291 | if (this->getSectionHdr()->sh_size == 0) | 
|  | 292 | return Offset; | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 293 | std::pair<uintX_t, uintX_t> *I = this->getRangeAndSize(Offset).first; | 
|  | 294 | uintX_t Base = I->second; | 
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 295 | if (Base == uintX_t(-1)) | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 296 | return -1; // Not in the output | 
|  | 297 |  | 
|  | 298 | uintX_t Addend = Offset - I->first; | 
|  | 299 | return Base + Addend; | 
|  | 300 | } | 
|  | 301 |  | 
|  | 302 | template <class ELFT> | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 303 | MergeInputSection<ELFT>::MergeInputSection(ObjectFile<ELFT> *F, | 
|  | 304 | const Elf_Shdr *Header) | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 305 | : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {} | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 306 |  | 
|  | 307 | template <class ELFT> | 
|  | 308 | bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 309 | return S->SectionKind == InputSectionBase<ELFT>::Merge; | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 312 | template <class ELFT> | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 313 | std::pair<std::pair<typename ELFFile<ELFT>::uintX_t, | 
|  | 314 | typename ELFFile<ELFT>::uintX_t> *, | 
|  | 315 | typename ELFFile<ELFT>::uintX_t> | 
|  | 316 | SplitInputSection<ELFT>::getRangeAndSize(uintX_t Offset) { | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 317 | ArrayRef<uint8_t> D = this->getSectionData(); | 
| Rui Ueyama | 7ba639b | 2015-10-25 16:25:04 +0000 | [diff] [blame] | 318 | StringRef Data((const char *)D.data(), D.size()); | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 319 | uintX_t Size = Data.size(); | 
|  | 320 | if (Offset >= Size) | 
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 321 | fatal("Entry is past the end of the section"); | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | // Find the element this offset points to. | 
|  | 324 | auto I = std::upper_bound( | 
| Rafael Espindola | d04c12a | 2015-11-11 15:20:45 +0000 | [diff] [blame] | 325 | Offsets.begin(), Offsets.end(), Offset, | 
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 326 | [](const uintX_t &A, const std::pair<uintX_t, uintX_t> &B) { | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 327 | return A < B.first; | 
|  | 328 | }); | 
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 329 | uintX_t End = I == Offsets.end() ? Data.size() : I->first; | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 330 | --I; | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 331 | return std::make_pair(&*I, End); | 
|  | 332 | } | 
|  | 333 |  | 
|  | 334 | template <class ELFT> | 
|  | 335 | typename MergeInputSection<ELFT>::uintX_t | 
|  | 336 | MergeInputSection<ELFT>::getOffset(uintX_t Offset) { | 
| George Rimar | 4b40ebc | 2015-11-13 13:44:59 +0000 | [diff] [blame] | 337 | std::pair<std::pair<uintX_t, uintX_t> *, uintX_t> T = | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 338 | this->getRangeAndSize(Offset); | 
|  | 339 | std::pair<uintX_t, uintX_t> *I = T.first; | 
|  | 340 | uintX_t End = T.second; | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 341 | uintX_t Start = I->first; | 
|  | 342 |  | 
|  | 343 | // Compute the Addend and if the Base is cached, return. | 
|  | 344 | uintX_t Addend = Offset - Start; | 
| Rafael Espindola | 3299499 | 2015-11-11 15:40:37 +0000 | [diff] [blame] | 345 | uintX_t &Base = I->second; | 
| Rafael Espindola | 1fe2d1e | 2015-11-11 15:55:00 +0000 | [diff] [blame] | 346 | if (Base != uintX_t(-1)) | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 347 | return Base + Addend; | 
|  | 348 |  | 
| Hal Finkel | f950595 | 2015-11-25 23:54:53 +0000 | [diff] [blame] | 349 | // Map the base to the offset in the output section and cache it. | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 350 | ArrayRef<uint8_t> D = this->getSectionData(); | 
|  | 351 | StringRef Data((const char *)D.data(), D.size()); | 
| Rafael Espindola | f82ed2a | 2015-10-24 22:51:01 +0000 | [diff] [blame] | 352 | StringRef Entry = Data.substr(Start, End - Start); | 
|  | 353 | Base = | 
|  | 354 | static_cast<MergeOutputSection<ELFT> *>(this->OutSec)->getOffset(Entry); | 
|  | 355 | return Base + Addend; | 
| Rafael Espindola | 5d83ccd | 2015-08-13 19:18:30 +0000 | [diff] [blame] | 356 | } | 
|  | 357 |  | 
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 358 | template <class ELFT> | 
|  | 359 | MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(ObjectFile<ELFT> *F, | 
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 360 | const Elf_Shdr *Hdr) | 
|  | 361 | : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) { | 
|  | 362 | // Initialize this->Reginfo. | 
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 363 | ArrayRef<uint8_t> D = this->getSectionData(); | 
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 364 | if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) | 
| Rui Ueyama | 64cfffd | 2016-01-28 18:40:06 +0000 | [diff] [blame] | 365 | fatal("Invalid size of .reginfo section"); | 
| Rui Ueyama | 70eed36 | 2016-01-06 22:42:43 +0000 | [diff] [blame] | 366 | Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data()); | 
| Simon Atanasyan | 57830b6 | 2015-12-25 13:02:13 +0000 | [diff] [blame] | 367 | } | 
|  | 368 |  | 
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 369 | template <class ELFT> | 
|  | 370 | bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { | 
|  | 371 | return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo; | 
|  | 372 | } | 
|  | 373 |  | 
| Rafael Espindola | 25472ee | 2016-01-22 21:49:07 +0000 | [diff] [blame] | 374 | template class elf2::InputSectionBase<ELF32LE>; | 
|  | 375 | template class elf2::InputSectionBase<ELF32BE>; | 
|  | 376 | template class elf2::InputSectionBase<ELF64LE>; | 
|  | 377 | template class elf2::InputSectionBase<ELF64BE>; | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 378 |  | 
| Rafael Espindola | 25472ee | 2016-01-22 21:49:07 +0000 | [diff] [blame] | 379 | template class elf2::InputSection<ELF32LE>; | 
|  | 380 | template class elf2::InputSection<ELF32BE>; | 
|  | 381 | template class elf2::InputSection<ELF64LE>; | 
|  | 382 | template class elf2::InputSection<ELF64BE>; | 
| Rafael Espindola | c159c96 | 2015-10-19 21:00:02 +0000 | [diff] [blame] | 383 |  | 
| Rafael Espindola | 25472ee | 2016-01-22 21:49:07 +0000 | [diff] [blame] | 384 | template class elf2::EHInputSection<ELF32LE>; | 
|  | 385 | template class elf2::EHInputSection<ELF32BE>; | 
|  | 386 | template class elf2::EHInputSection<ELF64LE>; | 
|  | 387 | template class elf2::EHInputSection<ELF64BE>; | 
| Rafael Espindola | 0c6a4f1 | 2015-11-11 19:54:14 +0000 | [diff] [blame] | 388 |  | 
| Rafael Espindola | 25472ee | 2016-01-22 21:49:07 +0000 | [diff] [blame] | 389 | template class elf2::MergeInputSection<ELF32LE>; | 
|  | 390 | template class elf2::MergeInputSection<ELF32BE>; | 
|  | 391 | template class elf2::MergeInputSection<ELF64LE>; | 
|  | 392 | template class elf2::MergeInputSection<ELF64BE>; | 
| Simon Atanasyan | 1d7df40 | 2015-12-20 10:57:34 +0000 | [diff] [blame] | 393 |  | 
| Rafael Espindola | 25472ee | 2016-01-22 21:49:07 +0000 | [diff] [blame] | 394 | template class elf2::MipsReginfoInputSection<ELF32LE>; | 
|  | 395 | template class elf2::MipsReginfoInputSection<ELF32BE>; | 
|  | 396 | template class elf2::MipsReginfoInputSection<ELF64LE>; | 
|  | 397 | template class elf2::MipsReginfoInputSection<ELF64BE>; |