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