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