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 "EhFrame.h" 13 #include "Error.h" 14 #include "InputFiles.h" 15 #include "OutputSections.h" 16 #include "Target.h" 17 18 #include "llvm/Support/Compression.h" 19 #include "llvm/Support/Endian.h" 20 21 using namespace llvm; 22 using namespace llvm::ELF; 23 using namespace llvm::object; 24 using namespace llvm::support::endian; 25 26 using namespace lld; 27 using namespace lld::elf; 28 29 template <class ELFT> 30 InputSectionBase<ELFT>::InputSectionBase(elf::ObjectFile<ELFT> *File, 31 const Elf_Shdr *Header, 32 Kind SectionKind) 33 : Header(Header), File(File), SectionKind(SectionKind), Repl(this), 34 Compressed(Header->sh_flags & SHF_COMPRESSED) { 35 // The garbage collector sets sections' Live bits. 36 // If GC is disabled, all sections are considered live by default. 37 Live = !Config->GcSections; 38 39 // The ELF spec states that a value of 0 means the section has 40 // no alignment constraits. 41 Alignment = std::max<uintX_t>(Header->sh_addralign, 1); 42 } 43 44 template <class ELFT> size_t InputSectionBase<ELFT>::getSize() const { 45 if (auto *D = dyn_cast<InputSection<ELFT>>(this)) 46 if (D->getThunksSize() > 0) 47 return D->getThunkOff() + D->getThunksSize(); 48 return Header->sh_size; 49 } 50 51 template <class ELFT> StringRef InputSectionBase<ELFT>::getSectionName() const { 52 return check(File->getObj().getSectionName(this->Header)); 53 } 54 55 template <class ELFT> 56 ArrayRef<uint8_t> InputSectionBase<ELFT>::getSectionData() const { 57 if (Compressed) 58 return ArrayRef<uint8_t>((const uint8_t *)Uncompressed.data(), 59 Uncompressed.size()); 60 return check(this->File->getObj().getSectionContents(this->Header)); 61 } 62 63 template <class ELFT> 64 typename ELFT::uint InputSectionBase<ELFT>::getOffset(uintX_t Offset) const { 65 switch (SectionKind) { 66 case Regular: 67 return cast<InputSection<ELFT>>(this)->OutSecOff + Offset; 68 case EHFrame: 69 return cast<EhInputSection<ELFT>>(this)->getOffset(Offset); 70 case Merge: 71 return cast<MergeInputSection<ELFT>>(this)->getOffset(Offset); 72 case MipsReginfo: 73 case MipsOptions: 74 // MIPS .reginfo and .MIPS.options sections are consumed by the linker, 75 // and the linker produces a single output section. It is possible that 76 // input files contain section symbol points to the corresponding input 77 // section. Redirect it to the produced output section. 78 if (Offset != 0) 79 fatal("Unsupported reference to the middle of '" + getSectionName() + 80 "' section"); 81 return this->OutSec->getVA(); 82 } 83 llvm_unreachable("invalid section kind"); 84 } 85 86 template <class ELFT> void InputSectionBase<ELFT>::uncompress() { 87 typedef typename std::conditional<ELFT::Is64Bits, Elf64_Chdr, 88 Elf32_Chdr>::type Elf_Chdr; 89 const endianness E = ELFT::TargetEndianness; 90 91 if (!zlib::isAvailable()) 92 fatal("build lld with zlib to enable compressed sections support"); 93 94 ArrayRef<uint8_t> Data = 95 check(this->File->getObj().getSectionContents(this->Header)); 96 if (read32<E>(Data.data()) != ELFCOMPRESS_ZLIB) 97 fatal("unsupported elf compression type"); 98 99 size_t UncompressedSize = 100 reinterpret_cast<const Elf_Chdr *>(Data.data())->ch_size; 101 size_t HdrSize = sizeof(Elf_Chdr); 102 StringRef Buf((const char *)Data.data() + HdrSize, Data.size() - HdrSize); 103 if (zlib::uncompress(Buf, Uncompressed, UncompressedSize) != zlib::StatusOK) 104 fatal("error uncompressing section"); 105 } 106 107 template <class ELFT> 108 typename ELFT::uint 109 InputSectionBase<ELFT>::getOffset(const DefinedRegular<ELFT> &Sym) const { 110 return getOffset(Sym.Value); 111 } 112 113 template <class ELFT> 114 InputSection<ELFT>::InputSection(elf::ObjectFile<ELFT> *F, 115 const Elf_Shdr *Header) 116 : InputSectionBase<ELFT>(F, Header, Base::Regular) {} 117 118 template <class ELFT> 119 bool InputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 120 return S->SectionKind == Base::Regular; 121 } 122 123 template <class ELFT> 124 InputSectionBase<ELFT> *InputSection<ELFT>::getRelocatedSection() { 125 assert(this->Header->sh_type == SHT_RELA || this->Header->sh_type == SHT_REL); 126 ArrayRef<InputSectionBase<ELFT> *> Sections = this->File->getSections(); 127 return Sections[this->Header->sh_info]; 128 } 129 130 template <class ELFT> void InputSection<ELFT>::addThunk(SymbolBody &Body) { 131 Body.ThunkIndex = Thunks.size(); 132 Thunks.push_back(&Body); 133 } 134 135 template <class ELFT> uint64_t InputSection<ELFT>::getThunkOff() const { 136 return this->Header->sh_size; 137 } 138 139 template <class ELFT> uint64_t InputSection<ELFT>::getThunksSize() const { 140 return Thunks.size() * Target->ThunkSize; 141 } 142 143 // This is used for -r. We can't use memcpy to copy relocations because we need 144 // to update symbol table offset and section index for each relocation. So we 145 // copy relocations one by one. 146 template <class ELFT> 147 template <class RelTy> 148 void InputSection<ELFT>::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) { 149 InputSectionBase<ELFT> *RelocatedSection = getRelocatedSection(); 150 151 for (const RelTy &Rel : Rels) { 152 uint32_t Type = Rel.getType(Config->Mips64EL); 153 SymbolBody &Body = this->File->getRelocTargetSym(Rel); 154 155 RelTy *P = reinterpret_cast<RelTy *>(Buf); 156 Buf += sizeof(RelTy); 157 158 P->r_offset = RelocatedSection->getOffset(Rel.r_offset); 159 P->setSymbolAndType(Body.DynsymIndex, Type, Config->Mips64EL); 160 } 161 } 162 163 // Page(Expr) is the page address of the expression Expr, defined 164 // as (Expr & ~0xFFF). (This applies even if the machine page size 165 // supported by the platform has a different value.) 166 static uint64_t getAArch64Page(uint64_t Expr) { 167 return Expr & (~static_cast<uint64_t>(0xFFF)); 168 } 169 170 template <class ELFT> 171 static typename ELFT::uint 172 getSymVA(uint32_t Type, typename ELFT::uint A, typename ELFT::uint P, 173 const SymbolBody &Body, uint8_t *BufLoc, 174 const elf::ObjectFile<ELFT> &File, RelExpr Expr) { 175 typedef typename ELFT::uint uintX_t; 176 177 switch (Expr) { 178 case R_HINT: 179 llvm_unreachable("cannot relocate hint relocs"); 180 case R_TLSLD: 181 return Out<ELFT>::Got->getTlsIndexOff() + A - 182 Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t); 183 case R_TLSLD_PC: 184 return Out<ELFT>::Got->getTlsIndexVA() + A - P; 185 case R_THUNK: 186 return Body.getThunkVA<ELFT>(); 187 case R_PPC_TOC: 188 return getPPC64TocBase() + A; 189 case R_TLSGD: 190 return Out<ELFT>::Got->getGlobalDynOffset(Body) + A - 191 Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t); 192 case R_TLSGD_PC: 193 return Out<ELFT>::Got->getGlobalDynAddr(Body) + A - P; 194 case R_TLSDESC: 195 return Out<ELFT>::Got->getGlobalDynAddr(Body) + A; 196 case R_TLSDESC_PAGE: 197 return getAArch64Page(Out<ELFT>::Got->getGlobalDynAddr(Body) + A) - 198 getAArch64Page(P); 199 case R_PLT: 200 return Body.getPltVA<ELFT>() + A; 201 case R_PLT_PC: 202 case R_PPC_PLT_OPD: 203 return Body.getPltVA<ELFT>() + A - P; 204 case R_SIZE: 205 return Body.getSize<ELFT>() + A; 206 case R_GOTREL: 207 return Body.getVA<ELFT>(A) - Out<ELFT>::Got->getVA(); 208 case R_RELAX_TLS_GD_TO_IE_END: 209 case R_GOT_FROM_END: 210 return Body.getGotOffset<ELFT>() + A - 211 Out<ELFT>::Got->getNumEntries() * sizeof(uintX_t); 212 case R_RELAX_TLS_GD_TO_IE_ABS: 213 case R_GOT: 214 return Body.getGotVA<ELFT>() + A; 215 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 216 case R_GOT_PAGE_PC: 217 return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P); 218 case R_RELAX_TLS_GD_TO_IE: 219 case R_GOT_PC: 220 return Body.getGotVA<ELFT>() + A - P; 221 case R_GOTONLY_PC: 222 return Out<ELFT>::Got->getVA() + A - P; 223 case R_RELAX_TLS_LD_TO_LE: 224 case R_RELAX_TLS_IE_TO_LE: 225 case R_RELAX_TLS_GD_TO_LE: 226 case R_TLS: 227 if (Target->TcbSize) 228 return Body.getVA<ELFT>(A) + 229 alignTo(Target->TcbSize, Out<ELFT>::TlsPhdr->p_align); 230 return Body.getVA<ELFT>(A) - Out<ELFT>::TlsPhdr->p_memsz; 231 case R_RELAX_TLS_GD_TO_LE_NEG: 232 case R_NEG_TLS: 233 return Out<ELF32LE>::TlsPhdr->p_memsz - Body.getVA<ELFT>(A); 234 case R_ABS: 235 case R_RELAX_GOT_PC_NOPIC: 236 return Body.getVA<ELFT>(A); 237 case R_GOT_OFF: 238 return Body.getGotOffset<ELFT>() + A; 239 case R_MIPS_GOT_LOCAL_PAGE: 240 // If relocation against MIPS local symbol requires GOT entry, this entry 241 // should be initialized by 'page address'. This address is high 16-bits 242 // of sum the symbol's value and the addend. 243 return Out<ELFT>::Got->getMipsLocalPageOffset(Body.getVA<ELFT>(A)); 244 case R_MIPS_GOT_OFF: 245 // In case of MIPS if a GOT relocation has non-zero addend this addend 246 // should be applied to the GOT entry content not to the GOT entry offset. 247 // That is why we use separate expression type. 248 return Out<ELFT>::Got->getMipsGotOffset(Body, A); 249 case R_MIPS_TLSGD: 250 return Out<ELFT>::Got->getGlobalDynOffset(Body) + 251 Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset; 252 case R_MIPS_TLSLD: 253 return Out<ELFT>::Got->getTlsIndexOff() + 254 Out<ELFT>::Got->getMipsTlsOffset() - MipsGPOffset; 255 case R_PPC_OPD: { 256 uint64_t SymVA = Body.getVA<ELFT>(A); 257 // If we have an undefined weak symbol, we might get here with a symbol 258 // address of zero. That could overflow, but the code must be unreachable, 259 // so don't bother doing anything at all. 260 if (!SymVA) 261 return 0; 262 if (Out<ELF64BE>::Opd) { 263 // If this is a local call, and we currently have the address of a 264 // function-descriptor, get the underlying code address instead. 265 uint64_t OpdStart = Out<ELF64BE>::Opd->getVA(); 266 uint64_t OpdEnd = OpdStart + Out<ELF64BE>::Opd->getSize(); 267 bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd; 268 if (InOpd) 269 SymVA = read64be(&Out<ELF64BE>::OpdBuf[SymVA - OpdStart]); 270 } 271 return SymVA - P; 272 } 273 case R_PC: 274 case R_RELAX_GOT_PC: 275 return Body.getVA<ELFT>(A) - P; 276 case R_PLT_PAGE_PC: 277 case R_PAGE_PC: 278 return getAArch64Page(Body.getVA<ELFT>(A)) - getAArch64Page(P); 279 } 280 llvm_unreachable("Invalid expression"); 281 } 282 283 // This function applies relocations to sections without SHF_ALLOC bit. 284 // Such sections are never mapped to memory at runtime. Debug sections are 285 // an example. Relocations in non-alloc sections are much easier to 286 // handle than in allocated sections because it will never need complex 287 // treatement such as GOT or PLT (because at runtime no one refers them). 288 // So, we handle relocations for non-alloc sections directly in this 289 // function as a performance optimization. 290 template <class ELFT> 291 template <class RelTy> 292 void InputSection<ELFT>::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) { 293 const unsigned Bits = sizeof(uintX_t) * 8; 294 for (const RelTy &Rel : Rels) { 295 uint32_t Type = Rel.getType(Config->Mips64EL); 296 uintX_t Offset = this->getOffset(Rel.r_offset); 297 uint8_t *BufLoc = Buf + Offset; 298 uintX_t Addend = getAddend<ELFT>(Rel); 299 if (!RelTy::IsRela) 300 Addend += Target->getImplicitAddend(BufLoc, Type); 301 302 SymbolBody &Sym = this->File->getRelocTargetSym(Rel); 303 if (Target->getRelExpr(Type, Sym) != R_ABS) { 304 error(this->getSectionName() + " has non-ABS reloc"); 305 return; 306 } 307 308 uintX_t AddrLoc = this->OutSec->getVA() + Offset; 309 uint64_t SymVA = SignExtend64<Bits>(getSymVA<ELFT>( 310 Type, Addend, AddrLoc, Sym, BufLoc, *this->File, R_ABS)); 311 Target->relocateOne(BufLoc, Type, SymVA); 312 } 313 } 314 315 template <class ELFT> 316 void InputSectionBase<ELFT>::relocate(uint8_t *Buf, uint8_t *BufEnd) { 317 // scanReloc function in Writer.cpp constructs Relocations 318 // vector only for SHF_ALLOC'ed sections. For other sections, 319 // we handle relocations directly here. 320 auto *IS = dyn_cast<InputSection<ELFT>>(this); 321 if (IS && !(IS->Header->sh_flags & SHF_ALLOC)) { 322 for (const Elf_Shdr *RelSec : IS->RelocSections) { 323 if (RelSec->sh_type == SHT_RELA) 324 IS->relocateNonAlloc(Buf, IS->File->getObj().relas(RelSec)); 325 else 326 IS->relocateNonAlloc(Buf, IS->File->getObj().rels(RelSec)); 327 } 328 return; 329 } 330 331 const unsigned Bits = sizeof(uintX_t) * 8; 332 for (const Relocation<ELFT> &Rel : Relocations) { 333 uintX_t Offset = Rel.InputSec->getOffset(Rel.Offset); 334 uint8_t *BufLoc = Buf + Offset; 335 uint32_t Type = Rel.Type; 336 uintX_t A = Rel.Addend; 337 338 uintX_t AddrLoc = OutSec->getVA() + Offset; 339 RelExpr Expr = Rel.Expr; 340 uint64_t SymVA = SignExtend64<Bits>( 341 getSymVA<ELFT>(Type, A, AddrLoc, *Rel.Sym, BufLoc, *File, Expr)); 342 343 switch (Expr) { 344 case R_RELAX_GOT_PC: 345 case R_RELAX_GOT_PC_NOPIC: 346 Target->relaxGot(BufLoc, SymVA); 347 break; 348 case R_RELAX_TLS_IE_TO_LE: 349 Target->relaxTlsIeToLe(BufLoc, Type, SymVA); 350 break; 351 case R_RELAX_TLS_LD_TO_LE: 352 Target->relaxTlsLdToLe(BufLoc, Type, SymVA); 353 break; 354 case R_RELAX_TLS_GD_TO_LE: 355 case R_RELAX_TLS_GD_TO_LE_NEG: 356 Target->relaxTlsGdToLe(BufLoc, Type, SymVA); 357 break; 358 case R_RELAX_TLS_GD_TO_IE: 359 case R_RELAX_TLS_GD_TO_IE_ABS: 360 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 361 case R_RELAX_TLS_GD_TO_IE_END: 362 Target->relaxTlsGdToIe(BufLoc, Type, SymVA); 363 break; 364 case R_PPC_PLT_OPD: 365 // Patch a nop (0x60000000) to a ld. 366 if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000) 367 write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1) 368 // fallthrough 369 default: 370 Target->relocateOne(BufLoc, Type, SymVA); 371 break; 372 } 373 } 374 } 375 376 template <class ELFT> void InputSection<ELFT>::writeTo(uint8_t *Buf) { 377 if (this->Header->sh_type == SHT_NOBITS) 378 return; 379 ELFFile<ELFT> &EObj = this->File->getObj(); 380 381 // If -r is given, then an InputSection may be a relocation section. 382 if (this->Header->sh_type == SHT_RELA) { 383 copyRelocations(Buf + OutSecOff, EObj.relas(this->Header)); 384 return; 385 } 386 if (this->Header->sh_type == SHT_REL) { 387 copyRelocations(Buf + OutSecOff, EObj.rels(this->Header)); 388 return; 389 } 390 391 // Copy section contents from source object file to output file. 392 ArrayRef<uint8_t> Data = this->getSectionData(); 393 memcpy(Buf + OutSecOff, Data.data(), Data.size()); 394 395 // Iterate over all relocation sections that apply to this section. 396 uint8_t *BufEnd = Buf + OutSecOff + Data.size(); 397 this->relocate(Buf, BufEnd); 398 399 // The section might have a data/code generated by the linker and need 400 // to be written after the section. Usually these are thunks - small piece 401 // of code used to jump between "incompatible" functions like PIC and non-PIC 402 // or if the jump target too far and its address does not fit to the short 403 // jump istruction. 404 if (!Thunks.empty()) { 405 Buf += OutSecOff + getThunkOff(); 406 for (const SymbolBody *S : Thunks) { 407 Target->writeThunk(Buf, S->getVA<ELFT>()); 408 Buf += Target->ThunkSize; 409 } 410 } 411 } 412 413 template <class ELFT> 414 void InputSection<ELFT>::replace(InputSection<ELFT> *Other) { 415 this->Alignment = std::max(this->Alignment, Other->Alignment); 416 Other->Repl = this->Repl; 417 Other->Live = false; 418 } 419 420 template <class ELFT> 421 SplitInputSection<ELFT>::SplitInputSection( 422 elf::ObjectFile<ELFT> *File, const Elf_Shdr *Header, 423 typename InputSectionBase<ELFT>::Kind SectionKind) 424 : InputSectionBase<ELFT>(File, Header, SectionKind) {} 425 426 template <class ELFT> 427 EhInputSection<ELFT>::EhInputSection(elf::ObjectFile<ELFT> *F, 428 const Elf_Shdr *Header) 429 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::EHFrame) { 430 // Mark .eh_frame sections as live by default because there are 431 // usually no relocations that point to .eh_frames. Otherwise, 432 // the garbage collector would drop all .eh_frame sections. 433 this->Live = true; 434 } 435 436 template <class ELFT> 437 bool EhInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 438 return S->SectionKind == InputSectionBase<ELFT>::EHFrame; 439 } 440 441 // .eh_frame is a sequence of CIE or FDE records. 442 // This function splits an input section into records and returns them. 443 template <class ELFT> 444 void EhInputSection<ELFT>::split() { 445 ArrayRef<uint8_t> Data = this->getSectionData(); 446 for (size_t Off = 0, End = Data.size(); Off != End;) { 447 size_t Size = readEhRecordSize<ELFT>(Data.slice(Off)); 448 this->Pieces.emplace_back(Off, Data.slice(Off, Size)); 449 // The empty record is the end marker. 450 if (Size == 4) 451 break; 452 Off += Size; 453 } 454 } 455 456 template <class ELFT> 457 typename ELFT::uint EhInputSection<ELFT>::getOffset(uintX_t Offset) const { 458 // The file crtbeginT.o has relocations pointing to the start of an empty 459 // .eh_frame that is known to be the first in the link. It does that to 460 // identify the start of the output .eh_frame. Handle this special case. 461 if (this->getSectionHdr()->sh_size == 0) 462 return Offset; 463 const SectionPiece *Piece = this->getSectionPiece(Offset); 464 if (Piece->OutputOff == size_t(-1)) 465 return -1; // Not in the output 466 467 uintX_t Addend = Offset - Piece->InputOff; 468 return Piece->OutputOff + Addend; 469 } 470 471 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) { 472 // Optimize the common case. 473 StringRef S((const char *)A.data(), A.size()); 474 if (EntSize == 1) 475 return S.find(0); 476 477 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { 478 const char *B = S.begin() + I; 479 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) 480 return I; 481 } 482 return StringRef::npos; 483 } 484 485 // Split SHF_STRINGS section. Such section is a sequence of 486 // null-terminated strings. 487 static std::vector<SectionPiece> splitStrings(ArrayRef<uint8_t> Data, 488 size_t EntSize) { 489 std::vector<SectionPiece> V; 490 size_t Off = 0; 491 while (!Data.empty()) { 492 size_t End = findNull(Data, EntSize); 493 if (End == StringRef::npos) 494 fatal("string is not null terminated"); 495 size_t Size = End + EntSize; 496 V.emplace_back(Off, Data.slice(0, Size)); 497 Data = Data.slice(Size); 498 Off += Size; 499 } 500 return V; 501 } 502 503 // Split non-SHF_STRINGS section. Such section is a sequence of 504 // fixed size records. 505 static std::vector<SectionPiece> splitNonStrings(ArrayRef<uint8_t> Data, 506 size_t EntSize) { 507 std::vector<SectionPiece> V; 508 size_t Size = Data.size(); 509 assert((Size % EntSize) == 0); 510 for (unsigned I = 0, N = Size; I != N; I += EntSize) 511 V.emplace_back(I, Data.slice(I, EntSize)); 512 return V; 513 } 514 515 template <class ELFT> 516 MergeInputSection<ELFT>::MergeInputSection(elf::ObjectFile<ELFT> *F, 517 const Elf_Shdr *Header) 518 : SplitInputSection<ELFT>(F, Header, InputSectionBase<ELFT>::Merge) {} 519 520 template <class ELFT> void MergeInputSection<ELFT>::splitIntoPieces() { 521 ArrayRef<uint8_t> Data = this->getSectionData(); 522 uintX_t EntSize = this->Header->sh_entsize; 523 if (this->Header->sh_flags & SHF_STRINGS) 524 this->Pieces = splitStrings(Data, EntSize); 525 else 526 this->Pieces = splitNonStrings(Data, EntSize); 527 528 if (Config->GcSections) 529 for (uintX_t Off : LiveOffsets) 530 this->getSectionPiece(Off)->Live = true; 531 } 532 533 template <class ELFT> 534 bool MergeInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 535 return S->SectionKind == InputSectionBase<ELFT>::Merge; 536 } 537 538 // Do binary search to get a section piece at a given input offset. 539 template <class ELFT> 540 SectionPiece *SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) { 541 auto *This = static_cast<const SplitInputSection<ELFT> *>(this); 542 return const_cast<SectionPiece *>(This->getSectionPiece(Offset)); 543 } 544 545 template <class ELFT> 546 const SectionPiece * 547 SplitInputSection<ELFT>::getSectionPiece(uintX_t Offset) const { 548 ArrayRef<uint8_t> D = this->getSectionData(); 549 StringRef Data((const char *)D.data(), D.size()); 550 uintX_t Size = Data.size(); 551 if (Offset >= Size) 552 fatal("entry is past the end of the section"); 553 554 // Find the element this offset points to. 555 auto I = std::upper_bound( 556 Pieces.begin(), Pieces.end(), Offset, 557 [](const uintX_t &A, const SectionPiece &B) { return A < B.InputOff; }); 558 --I; 559 return &*I; 560 } 561 562 // Returns the offset in an output section for a given input offset. 563 // Because contents of a mergeable section is not contiguous in output, 564 // it is not just an addition to a base output offset. 565 template <class ELFT> 566 typename ELFT::uint MergeInputSection<ELFT>::getOffset(uintX_t Offset) const { 567 auto It = OffsetMap.find(Offset); 568 if (It != OffsetMap.end()) 569 return It->second; 570 571 // If Offset is not at beginning of a section piece, it is not in the map. 572 // In that case we need to search from the original section piece vector. 573 const SectionPiece &Piece = *this->getSectionPiece(Offset); 574 assert(Piece.Live); 575 uintX_t Addend = Offset - Piece.InputOff; 576 return Piece.OutputOff + Addend; 577 } 578 579 // Create a map from input offsets to output offsets for all section pieces. 580 // It is called after finalize(). 581 template <class ELFT> void MergeInputSection<ELFT>::finalizePieces() { 582 OffsetMap.grow(this->Pieces.size()); 583 for (SectionPiece &Piece : this->Pieces) { 584 if (!Piece.Live) 585 continue; 586 if (Piece.OutputOff == size_t(-1)) { 587 // Offsets of tail-merged strings are computed lazily. 588 auto *OutSec = static_cast<MergeOutputSection<ELFT> *>(this->OutSec); 589 ArrayRef<uint8_t> D = Piece.data(); 590 StringRef S((const char *)D.data(), D.size()); 591 Piece.OutputOff = OutSec->getOffset(S); 592 } 593 OffsetMap[Piece.InputOff] = Piece.OutputOff; 594 } 595 } 596 597 template <class ELFT> 598 MipsReginfoInputSection<ELFT>::MipsReginfoInputSection(elf::ObjectFile<ELFT> *F, 599 const Elf_Shdr *Hdr) 600 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsReginfo) { 601 // Initialize this->Reginfo. 602 ArrayRef<uint8_t> D = this->getSectionData(); 603 if (D.size() != sizeof(Elf_Mips_RegInfo<ELFT>)) { 604 error("invalid size of .reginfo section"); 605 return; 606 } 607 Reginfo = reinterpret_cast<const Elf_Mips_RegInfo<ELFT> *>(D.data()); 608 } 609 610 template <class ELFT> 611 bool MipsReginfoInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 612 return S->SectionKind == InputSectionBase<ELFT>::MipsReginfo; 613 } 614 615 template <class ELFT> 616 MipsOptionsInputSection<ELFT>::MipsOptionsInputSection(elf::ObjectFile<ELFT> *F, 617 const Elf_Shdr *Hdr) 618 : InputSectionBase<ELFT>(F, Hdr, InputSectionBase<ELFT>::MipsOptions) { 619 // Find ODK_REGINFO option in the section's content. 620 ArrayRef<uint8_t> D = this->getSectionData(); 621 while (!D.empty()) { 622 if (D.size() < sizeof(Elf_Mips_Options<ELFT>)) { 623 error("invalid size of .MIPS.options section"); 624 break; 625 } 626 auto *O = reinterpret_cast<const Elf_Mips_Options<ELFT> *>(D.data()); 627 if (O->kind == ODK_REGINFO) { 628 Reginfo = &O->getRegInfo(); 629 break; 630 } 631 D = D.slice(O->size); 632 } 633 } 634 635 template <class ELFT> 636 bool MipsOptionsInputSection<ELFT>::classof(const InputSectionBase<ELFT> *S) { 637 return S->SectionKind == InputSectionBase<ELFT>::MipsOptions; 638 } 639 640 template class elf::InputSectionBase<ELF32LE>; 641 template class elf::InputSectionBase<ELF32BE>; 642 template class elf::InputSectionBase<ELF64LE>; 643 template class elf::InputSectionBase<ELF64BE>; 644 645 template class elf::InputSection<ELF32LE>; 646 template class elf::InputSection<ELF32BE>; 647 template class elf::InputSection<ELF64LE>; 648 template class elf::InputSection<ELF64BE>; 649 650 template class elf::SplitInputSection<ELF32LE>; 651 template class elf::SplitInputSection<ELF32BE>; 652 template class elf::SplitInputSection<ELF64LE>; 653 template class elf::SplitInputSection<ELF64BE>; 654 655 template class elf::EhInputSection<ELF32LE>; 656 template class elf::EhInputSection<ELF32BE>; 657 template class elf::EhInputSection<ELF64LE>; 658 template class elf::EhInputSection<ELF64BE>; 659 660 template class elf::MergeInputSection<ELF32LE>; 661 template class elf::MergeInputSection<ELF32BE>; 662 template class elf::MergeInputSection<ELF64LE>; 663 template class elf::MergeInputSection<ELF64BE>; 664 665 template class elf::MipsReginfoInputSection<ELF32LE>; 666 template class elf::MipsReginfoInputSection<ELF32BE>; 667 template class elf::MipsReginfoInputSection<ELF64LE>; 668 template class elf::MipsReginfoInputSection<ELF64BE>; 669 670 template class elf::MipsOptionsInputSection<ELF32LE>; 671 template class elf::MipsOptionsInputSection<ELF32BE>; 672 template class elf::MipsOptionsInputSection<ELF64LE>; 673 template class elf::MipsOptionsInputSection<ELF64BE>; 674