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