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