1 //===- InputSection.cpp ---------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "InputSection.h" 10 #include "Config.h" 11 #include "InputFiles.h" 12 #include "OutputSections.h" 13 #include "Relocations.h" 14 #include "SymbolTable.h" 15 #include "Symbols.h" 16 #include "SyntheticSections.h" 17 #include "Target.h" 18 #include "lld/Common/CommonLinkerContext.h" 19 #include "llvm/Support/Compiler.h" 20 #include "llvm/Support/Compression.h" 21 #include "llvm/Support/Endian.h" 22 #include "llvm/Support/xxhash.h" 23 #include <algorithm> 24 #include <mutex> 25 #include <vector> 26 27 using namespace llvm; 28 using namespace llvm::ELF; 29 using namespace llvm::object; 30 using namespace llvm::support; 31 using namespace llvm::support::endian; 32 using namespace llvm::sys; 33 using namespace lld; 34 using namespace lld::elf; 35 36 DenseSet<std::pair<const Symbol *, uint64_t>> elf::ppc64noTocRelax; 37 38 // Returns a string to construct an error message. 39 std::string lld::toString(const InputSectionBase *sec) { 40 return (toString(sec->file) + ":(" + sec->name + ")").str(); 41 } 42 43 template <class ELFT> 44 static ArrayRef<uint8_t> getSectionContents(ObjFile<ELFT> &file, 45 const typename ELFT::Shdr &hdr) { 46 if (hdr.sh_type == SHT_NOBITS) 47 return ArrayRef<uint8_t>(nullptr, hdr.sh_size); 48 return check(file.getObj().getSectionContents(hdr)); 49 } 50 51 InputSectionBase::InputSectionBase(InputFile *file, uint64_t flags, 52 uint32_t type, uint64_t entsize, 53 uint32_t link, uint32_t info, 54 uint32_t addralign, ArrayRef<uint8_t> data, 55 StringRef name, Kind sectionKind) 56 : SectionBase(sectionKind, name, flags, entsize, addralign, type, info, 57 link), 58 file(file), content_(data.data()), size(data.size()) { 59 // In order to reduce memory allocation, we assume that mergeable 60 // sections are smaller than 4 GiB, which is not an unreasonable 61 // assumption as of 2017. 62 if (sectionKind == SectionBase::Merge && content().size() > UINT32_MAX) 63 error(toString(this) + ": section too large"); 64 65 // The ELF spec states that a value of 0 means the section has 66 // no alignment constraints. 67 uint32_t v = std::max<uint32_t>(addralign, 1); 68 if (!isPowerOf2_64(v)) 69 fatal(toString(this) + ": sh_addralign is not a power of 2"); 70 this->addralign = v; 71 72 // If SHF_COMPRESSED is set, parse the header. The legacy .zdebug format is no 73 // longer supported. 74 if (flags & SHF_COMPRESSED) 75 invokeELFT(parseCompressedHeader,); 76 } 77 78 // Drop SHF_GROUP bit unless we are producing a re-linkable object file. 79 // SHF_GROUP is a marker that a section belongs to some comdat group. 80 // That flag doesn't make sense in an executable. 81 static uint64_t getFlags(uint64_t flags) { 82 flags &= ~(uint64_t)SHF_INFO_LINK; 83 if (!config->relocatable) 84 flags &= ~(uint64_t)SHF_GROUP; 85 return flags; 86 } 87 88 template <class ELFT> 89 InputSectionBase::InputSectionBase(ObjFile<ELFT> &file, 90 const typename ELFT::Shdr &hdr, 91 StringRef name, Kind sectionKind) 92 : InputSectionBase(&file, getFlags(hdr.sh_flags), hdr.sh_type, 93 hdr.sh_entsize, hdr.sh_link, hdr.sh_info, 94 hdr.sh_addralign, getSectionContents(file, hdr), name, 95 sectionKind) { 96 // We reject object files having insanely large alignments even though 97 // they are allowed by the spec. I think 4GB is a reasonable limitation. 98 // We might want to relax this in the future. 99 if (hdr.sh_addralign > UINT32_MAX) 100 fatal(toString(&file) + ": section sh_addralign is too large"); 101 } 102 103 size_t InputSectionBase::getSize() const { 104 if (auto *s = dyn_cast<SyntheticSection>(this)) 105 return s->getSize(); 106 return size - bytesDropped; 107 } 108 109 template <class ELFT> 110 static void decompressAux(const InputSectionBase &sec, uint8_t *out, 111 size_t size) { 112 auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(sec.content_); 113 auto compressed = ArrayRef<uint8_t>(sec.content_, sec.compressedSize) 114 .slice(sizeof(typename ELFT::Chdr)); 115 if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB 116 ? compression::zlib::decompress(compressed, out, size) 117 : compression::zstd::decompress(compressed, out, size)) 118 fatal(toString(&sec) + 119 ": decompress failed: " + llvm::toString(std::move(e))); 120 } 121 122 void InputSectionBase::decompress() const { 123 uint8_t *uncompressedBuf; 124 { 125 static std::mutex mu; 126 std::lock_guard<std::mutex> lock(mu); 127 uncompressedBuf = bAlloc().Allocate<uint8_t>(size); 128 } 129 130 invokeELFT(decompressAux, *this, uncompressedBuf, size); 131 content_ = uncompressedBuf; 132 compressed = false; 133 } 134 135 template <class ELFT> RelsOrRelas<ELFT> InputSectionBase::relsOrRelas() const { 136 if (relSecIdx == 0) 137 return {}; 138 RelsOrRelas<ELFT> ret; 139 typename ELFT::Shdr shdr = 140 cast<ELFFileBase>(file)->getELFShdrs<ELFT>()[relSecIdx]; 141 if (shdr.sh_type == SHT_REL) { 142 ret.rels = ArrayRef(reinterpret_cast<const typename ELFT::Rel *>( 143 file->mb.getBufferStart() + shdr.sh_offset), 144 shdr.sh_size / sizeof(typename ELFT::Rel)); 145 } else { 146 assert(shdr.sh_type == SHT_RELA); 147 ret.relas = ArrayRef(reinterpret_cast<const typename ELFT::Rela *>( 148 file->mb.getBufferStart() + shdr.sh_offset), 149 shdr.sh_size / sizeof(typename ELFT::Rela)); 150 } 151 return ret; 152 } 153 154 uint64_t SectionBase::getOffset(uint64_t offset) const { 155 switch (kind()) { 156 case Output: { 157 auto *os = cast<OutputSection>(this); 158 // For output sections we treat offset -1 as the end of the section. 159 return offset == uint64_t(-1) ? os->size : offset; 160 } 161 case Regular: 162 case Synthetic: 163 return cast<InputSection>(this)->outSecOff + offset; 164 case EHFrame: { 165 // Two code paths may reach here. First, clang_rt.crtbegin.o and GCC 166 // crtbeginT.o may reference the start of an empty .eh_frame to identify the 167 // start of the output .eh_frame. Just return offset. 168 // 169 // Second, InputSection::copyRelocations on .eh_frame. Some pieces may be 170 // discarded due to GC/ICF. We should compute the output section offset. 171 const EhInputSection *es = cast<EhInputSection>(this); 172 if (!es->content().empty()) 173 if (InputSection *isec = es->getParent()) 174 return isec->outSecOff + es->getParentOffset(offset); 175 return offset; 176 } 177 case Merge: 178 const MergeInputSection *ms = cast<MergeInputSection>(this); 179 if (InputSection *isec = ms->getParent()) 180 return isec->outSecOff + ms->getParentOffset(offset); 181 return ms->getParentOffset(offset); 182 } 183 llvm_unreachable("invalid section kind"); 184 } 185 186 uint64_t SectionBase::getVA(uint64_t offset) const { 187 const OutputSection *out = getOutputSection(); 188 return (out ? out->addr : 0) + getOffset(offset); 189 } 190 191 OutputSection *SectionBase::getOutputSection() { 192 InputSection *sec; 193 if (auto *isec = dyn_cast<InputSection>(this)) 194 sec = isec; 195 else if (auto *ms = dyn_cast<MergeInputSection>(this)) 196 sec = ms->getParent(); 197 else if (auto *eh = dyn_cast<EhInputSection>(this)) 198 sec = eh->getParent(); 199 else 200 return cast<OutputSection>(this); 201 return sec ? sec->getParent() : nullptr; 202 } 203 204 // When a section is compressed, `rawData` consists with a header followed 205 // by zlib-compressed data. This function parses a header to initialize 206 // `uncompressedSize` member and remove the header from `rawData`. 207 template <typename ELFT> void InputSectionBase::parseCompressedHeader() { 208 flags &= ~(uint64_t)SHF_COMPRESSED; 209 210 // New-style header 211 if (content().size() < sizeof(typename ELFT::Chdr)) { 212 error(toString(this) + ": corrupted compressed section"); 213 return; 214 } 215 216 auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content().data()); 217 if (hdr->ch_type == ELFCOMPRESS_ZLIB) { 218 if (!compression::zlib::isAvailable()) 219 error(toString(this) + " is compressed with ELFCOMPRESS_ZLIB, but lld is " 220 "not built with zlib support"); 221 } else if (hdr->ch_type == ELFCOMPRESS_ZSTD) { 222 if (!compression::zstd::isAvailable()) 223 error(toString(this) + " is compressed with ELFCOMPRESS_ZSTD, but lld is " 224 "not built with zstd support"); 225 } else { 226 error(toString(this) + ": unsupported compression type (" + 227 Twine(hdr->ch_type) + ")"); 228 return; 229 } 230 231 compressed = true; 232 compressedSize = size; 233 size = hdr->ch_size; 234 addralign = std::max<uint32_t>(hdr->ch_addralign, 1); 235 } 236 237 InputSection *InputSectionBase::getLinkOrderDep() const { 238 assert(flags & SHF_LINK_ORDER); 239 if (!link) 240 return nullptr; 241 return cast<InputSection>(file->getSections()[link]); 242 } 243 244 // Find a function symbol that encloses a given location. 245 Defined *InputSectionBase::getEnclosingFunction(uint64_t offset) { 246 for (Symbol *b : file->getSymbols()) 247 if (Defined *d = dyn_cast<Defined>(b)) 248 if (d->section == this && d->type == STT_FUNC && d->value <= offset && 249 offset < d->value + d->size) 250 return d; 251 return nullptr; 252 } 253 254 // Returns an object file location string. Used to construct an error message. 255 std::string InputSectionBase::getLocation(uint64_t offset) { 256 std::string secAndOffset = 257 (name + "+0x" + Twine::utohexstr(offset) + ")").str(); 258 259 // We don't have file for synthetic sections. 260 if (file == nullptr) 261 return (config->outputFile + ":(" + secAndOffset).str(); 262 263 std::string filename = toString(file); 264 if (Defined *d = getEnclosingFunction(offset)) 265 return filename + ":(function " + toString(*d) + ": " + secAndOffset; 266 267 return filename + ":(" + secAndOffset; 268 } 269 270 // This function is intended to be used for constructing an error message. 271 // The returned message looks like this: 272 // 273 // foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42) 274 // 275 // Returns an empty string if there's no way to get line info. 276 std::string InputSectionBase::getSrcMsg(const Symbol &sym, uint64_t offset) { 277 return file->getSrcMsg(sym, *this, offset); 278 } 279 280 // Returns a filename string along with an optional section name. This 281 // function is intended to be used for constructing an error 282 // message. The returned message looks like this: 283 // 284 // path/to/foo.o:(function bar) 285 // 286 // or 287 // 288 // path/to/foo.o:(function bar) in archive path/to/bar.a 289 std::string InputSectionBase::getObjMsg(uint64_t off) { 290 std::string filename = std::string(file->getName()); 291 292 std::string archive; 293 if (!file->archiveName.empty()) 294 archive = (" in archive " + file->archiveName).str(); 295 296 // Find a symbol that encloses a given location. getObjMsg may be called 297 // before ObjFile::initSectionsAndLocalSyms where local symbols are 298 // initialized. 299 for (Symbol *b : file->getSymbols()) 300 if (auto *d = dyn_cast_or_null<Defined>(b)) 301 if (d->section == this && d->value <= off && off < d->value + d->size) 302 return filename + ":(" + toString(*d) + ")" + archive; 303 304 // If there's no symbol, print out the offset in the section. 305 return (filename + ":(" + name + "+0x" + utohexstr(off) + ")" + archive) 306 .str(); 307 } 308 309 InputSection InputSection::discarded(nullptr, 0, 0, 0, ArrayRef<uint8_t>(), ""); 310 311 InputSection::InputSection(InputFile *f, uint64_t flags, uint32_t type, 312 uint32_t addralign, ArrayRef<uint8_t> data, 313 StringRef name, Kind k) 314 : InputSectionBase(f, flags, type, 315 /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, addralign, data, 316 name, k) {} 317 318 template <class ELFT> 319 InputSection::InputSection(ObjFile<ELFT> &f, const typename ELFT::Shdr &header, 320 StringRef name) 321 : InputSectionBase(f, header, name, InputSectionBase::Regular) {} 322 323 // Copy SHT_GROUP section contents. Used only for the -r option. 324 template <class ELFT> void InputSection::copyShtGroup(uint8_t *buf) { 325 // ELFT::Word is the 32-bit integral type in the target endianness. 326 using u32 = typename ELFT::Word; 327 ArrayRef<u32> from = getDataAs<u32>(); 328 auto *to = reinterpret_cast<u32 *>(buf); 329 330 // The first entry is not a section number but a flag. 331 *to++ = from[0]; 332 333 // Adjust section numbers because section numbers in an input object files are 334 // different in the output. We also need to handle combined or discarded 335 // members. 336 ArrayRef<InputSectionBase *> sections = file->getSections(); 337 DenseSet<uint32_t> seen; 338 for (uint32_t idx : from.slice(1)) { 339 OutputSection *osec = sections[idx]->getOutputSection(); 340 if (osec && seen.insert(osec->sectionIndex).second) 341 *to++ = osec->sectionIndex; 342 } 343 } 344 345 InputSectionBase *InputSection::getRelocatedSection() const { 346 if (!file || (type != SHT_RELA && type != SHT_REL)) 347 return nullptr; 348 ArrayRef<InputSectionBase *> sections = file->getSections(); 349 return sections[info]; 350 } 351 352 // This is used for -r and --emit-relocs. We can't use memcpy to copy 353 // relocations because we need to update symbol table offset and section index 354 // for each relocation. So we copy relocations one by one. 355 template <class ELFT, class RelTy> 356 void InputSection::copyRelocations(uint8_t *buf, ArrayRef<RelTy> rels) { 357 const TargetInfo &target = *elf::target; 358 InputSectionBase *sec = getRelocatedSection(); 359 (void)sec->contentMaybeDecompress(); // uncompress if needed 360 361 for (const RelTy &rel : rels) { 362 RelType type = rel.getType(config->isMips64EL); 363 const ObjFile<ELFT> *file = getFile<ELFT>(); 364 Symbol &sym = file->getRelocTargetSym(rel); 365 366 auto *p = reinterpret_cast<typename ELFT::Rela *>(buf); 367 buf += sizeof(RelTy); 368 369 if (RelTy::IsRela) 370 p->r_addend = getAddend<ELFT>(rel); 371 372 // Output section VA is zero for -r, so r_offset is an offset within the 373 // section, but for --emit-relocs it is a virtual address. 374 p->r_offset = sec->getVA(rel.r_offset); 375 p->setSymbolAndType(in.symTab->getSymbolIndex(&sym), type, 376 config->isMips64EL); 377 378 if (sym.type == STT_SECTION) { 379 // We combine multiple section symbols into only one per 380 // section. This means we have to update the addend. That is 381 // trivial for Elf_Rela, but for Elf_Rel we have to write to the 382 // section data. We do that by adding to the Relocation vector. 383 384 // .eh_frame is horribly special and can reference discarded sections. To 385 // avoid having to parse and recreate .eh_frame, we just replace any 386 // relocation in it pointing to discarded sections with R_*_NONE, which 387 // hopefully creates a frame that is ignored at runtime. Also, don't warn 388 // on .gcc_except_table and debug sections. 389 // 390 // See the comment in maybeReportUndefined for PPC32 .got2 and PPC64 .toc 391 auto *d = dyn_cast<Defined>(&sym); 392 if (!d) { 393 if (!isDebugSection(*sec) && sec->name != ".eh_frame" && 394 sec->name != ".gcc_except_table" && sec->name != ".got2" && 395 sec->name != ".toc") { 396 uint32_t secIdx = cast<Undefined>(sym).discardedSecIdx; 397 Elf_Shdr_Impl<ELFT> sec = file->template getELFShdrs<ELFT>()[secIdx]; 398 warn("relocation refers to a discarded section: " + 399 CHECK(file->getObj().getSectionName(sec), file) + 400 "\n>>> referenced by " + getObjMsg(p->r_offset)); 401 } 402 p->setSymbolAndType(0, 0, false); 403 continue; 404 } 405 SectionBase *section = d->section; 406 if (!section->isLive()) { 407 p->setSymbolAndType(0, 0, false); 408 continue; 409 } 410 411 int64_t addend = getAddend<ELFT>(rel); 412 const uint8_t *bufLoc = sec->content().begin() + rel.r_offset; 413 if (!RelTy::IsRela) 414 addend = target.getImplicitAddend(bufLoc, type); 415 416 if (config->emachine == EM_MIPS && 417 target.getRelExpr(type, sym, bufLoc) == R_MIPS_GOTREL) { 418 // Some MIPS relocations depend on "gp" value. By default, 419 // this value has 0x7ff0 offset from a .got section. But 420 // relocatable files produced by a compiler or a linker 421 // might redefine this default value and we must use it 422 // for a calculation of the relocation result. When we 423 // generate EXE or DSO it's trivial. Generating a relocatable 424 // output is more difficult case because the linker does 425 // not calculate relocations in this mode and loses 426 // individual "gp" values used by each input object file. 427 // As a workaround we add the "gp" value to the relocation 428 // addend and save it back to the file. 429 addend += sec->getFile<ELFT>()->mipsGp0; 430 } 431 432 if (RelTy::IsRela) 433 p->r_addend = sym.getVA(addend) - section->getOutputSection()->addr; 434 else if (config->relocatable && type != target.noneRel) 435 sec->addReloc({R_ABS, type, rel.r_offset, addend, &sym}); 436 } else if (config->emachine == EM_PPC && type == R_PPC_PLTREL24 && 437 p->r_addend >= 0x8000 && sec->file->ppc32Got2) { 438 // Similar to R_MIPS_GPREL{16,32}. If the addend of R_PPC_PLTREL24 439 // indicates that r30 is relative to the input section .got2 440 // (r_addend>=0x8000), after linking, r30 should be relative to the output 441 // section .got2 . To compensate for the shift, adjust r_addend by 442 // ppc32Got->outSecOff. 443 p->r_addend += sec->file->ppc32Got2->outSecOff; 444 } 445 } 446 } 447 448 // The ARM and AArch64 ABI handle pc-relative relocations to undefined weak 449 // references specially. The general rule is that the value of the symbol in 450 // this context is the address of the place P. A further special case is that 451 // branch relocations to an undefined weak reference resolve to the next 452 // instruction. 453 static uint32_t getARMUndefinedRelativeWeakVA(RelType type, uint32_t a, 454 uint32_t p) { 455 switch (type) { 456 // Unresolved branch relocations to weak references resolve to next 457 // instruction, this will be either 2 or 4 bytes on from P. 458 case R_ARM_THM_JUMP8: 459 case R_ARM_THM_JUMP11: 460 return p + 2 + a; 461 case R_ARM_CALL: 462 case R_ARM_JUMP24: 463 case R_ARM_PC24: 464 case R_ARM_PLT32: 465 case R_ARM_PREL31: 466 case R_ARM_THM_JUMP19: 467 case R_ARM_THM_JUMP24: 468 return p + 4 + a; 469 case R_ARM_THM_CALL: 470 // We don't want an interworking BLX to ARM 471 return p + 5 + a; 472 // Unresolved non branch pc-relative relocations 473 // R_ARM_TARGET2 which can be resolved relatively is not present as it never 474 // targets a weak-reference. 475 case R_ARM_MOVW_PREL_NC: 476 case R_ARM_MOVT_PREL: 477 case R_ARM_REL32: 478 case R_ARM_THM_ALU_PREL_11_0: 479 case R_ARM_THM_MOVW_PREL_NC: 480 case R_ARM_THM_MOVT_PREL: 481 case R_ARM_THM_PC12: 482 return p + a; 483 // p + a is unrepresentable as negative immediates can't be encoded. 484 case R_ARM_THM_PC8: 485 return p; 486 } 487 llvm_unreachable("ARM pc-relative relocation expected\n"); 488 } 489 490 // The comment above getARMUndefinedRelativeWeakVA applies to this function. 491 static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t type, uint64_t p) { 492 switch (type) { 493 // Unresolved branch relocations to weak references resolve to next 494 // instruction, this is 4 bytes on from P. 495 case R_AARCH64_CALL26: 496 case R_AARCH64_CONDBR19: 497 case R_AARCH64_JUMP26: 498 case R_AARCH64_TSTBR14: 499 return p + 4; 500 // Unresolved non branch pc-relative relocations 501 case R_AARCH64_PREL16: 502 case R_AARCH64_PREL32: 503 case R_AARCH64_PREL64: 504 case R_AARCH64_ADR_PREL_LO21: 505 case R_AARCH64_LD_PREL_LO19: 506 case R_AARCH64_PLT32: 507 return p; 508 } 509 llvm_unreachable("AArch64 pc-relative relocation expected\n"); 510 } 511 512 static uint64_t getRISCVUndefinedRelativeWeakVA(uint64_t type, uint64_t p) { 513 switch (type) { 514 case R_RISCV_BRANCH: 515 case R_RISCV_JAL: 516 case R_RISCV_CALL: 517 case R_RISCV_CALL_PLT: 518 case R_RISCV_RVC_BRANCH: 519 case R_RISCV_RVC_JUMP: 520 case R_RISCV_PLT32: 521 return p; 522 default: 523 return 0; 524 } 525 } 526 527 // ARM SBREL relocations are of the form S + A - B where B is the static base 528 // The ARM ABI defines base to be "addressing origin of the output segment 529 // defining the symbol S". We defined the "addressing origin"/static base to be 530 // the base of the PT_LOAD segment containing the Sym. 531 // The procedure call standard only defines a Read Write Position Independent 532 // RWPI variant so in practice we should expect the static base to be the base 533 // of the RW segment. 534 static uint64_t getARMStaticBase(const Symbol &sym) { 535 OutputSection *os = sym.getOutputSection(); 536 if (!os || !os->ptLoad || !os->ptLoad->firstSec) 537 fatal("SBREL relocation to " + sym.getName() + " without static base"); 538 return os->ptLoad->firstSec->addr; 539 } 540 541 // For R_RISCV_PC_INDIRECT (R_RISCV_PCREL_LO12_{I,S}), the symbol actually 542 // points the corresponding R_RISCV_PCREL_HI20 relocation, and the target VA 543 // is calculated using PCREL_HI20's symbol. 544 // 545 // This function returns the R_RISCV_PCREL_HI20 relocation from 546 // R_RISCV_PCREL_LO12's symbol and addend. 547 static Relocation *getRISCVPCRelHi20(const Symbol *sym, uint64_t addend) { 548 const Defined *d = cast<Defined>(sym); 549 if (!d->section) { 550 errorOrWarn("R_RISCV_PCREL_LO12 relocation points to an absolute symbol: " + 551 sym->getName()); 552 return nullptr; 553 } 554 InputSection *isec = cast<InputSection>(d->section); 555 556 if (addend != 0) 557 warn("non-zero addend in R_RISCV_PCREL_LO12 relocation to " + 558 isec->getObjMsg(d->value) + " is ignored"); 559 560 // Relocations are sorted by offset, so we can use std::equal_range to do 561 // binary search. 562 Relocation r; 563 r.offset = d->value; 564 auto range = 565 std::equal_range(isec->relocs().begin(), isec->relocs().end(), r, 566 [](const Relocation &lhs, const Relocation &rhs) { 567 return lhs.offset < rhs.offset; 568 }); 569 570 for (auto it = range.first; it != range.second; ++it) 571 if (it->type == R_RISCV_PCREL_HI20 || it->type == R_RISCV_GOT_HI20 || 572 it->type == R_RISCV_TLS_GD_HI20 || it->type == R_RISCV_TLS_GOT_HI20) 573 return &*it; 574 575 errorOrWarn("R_RISCV_PCREL_LO12 relocation points to " + 576 isec->getObjMsg(d->value) + 577 " without an associated R_RISCV_PCREL_HI20 relocation"); 578 return nullptr; 579 } 580 581 // A TLS symbol's virtual address is relative to the TLS segment. Add a 582 // target-specific adjustment to produce a thread-pointer-relative offset. 583 static int64_t getTlsTpOffset(const Symbol &s) { 584 // On targets that support TLSDESC, _TLS_MODULE_BASE_@tpoff = 0. 585 if (&s == ElfSym::tlsModuleBase) 586 return 0; 587 588 // There are 2 TLS layouts. Among targets we support, x86 uses TLS Variant 2 589 // while most others use Variant 1. At run time TP will be aligned to p_align. 590 591 // Variant 1. TP will be followed by an optional gap (which is the size of 2 592 // pointers on ARM/AArch64, 0 on other targets), followed by alignment 593 // padding, then the static TLS blocks. The alignment padding is added so that 594 // (TP + gap + padding) is congruent to p_vaddr modulo p_align. 595 // 596 // Variant 2. Static TLS blocks, followed by alignment padding are placed 597 // before TP. The alignment padding is added so that (TP - padding - 598 // p_memsz) is congruent to p_vaddr modulo p_align. 599 PhdrEntry *tls = Out::tlsPhdr; 600 switch (config->emachine) { 601 // Variant 1. 602 case EM_ARM: 603 case EM_AARCH64: 604 return s.getVA(0) + config->wordsize * 2 + 605 ((tls->p_vaddr - config->wordsize * 2) & (tls->p_align - 1)); 606 case EM_MIPS: 607 case EM_PPC: 608 case EM_PPC64: 609 // Adjusted Variant 1. TP is placed with a displacement of 0x7000, which is 610 // to allow a signed 16-bit offset to reach 0x1000 of TCB/thread-library 611 // data and 0xf000 of the program's TLS segment. 612 return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)) - 0x7000; 613 case EM_LOONGARCH: 614 case EM_RISCV: 615 return s.getVA(0) + (tls->p_vaddr & (tls->p_align - 1)); 616 617 // Variant 2. 618 case EM_HEXAGON: 619 case EM_SPARCV9: 620 case EM_386: 621 case EM_X86_64: 622 return s.getVA(0) - tls->p_memsz - 623 ((-tls->p_vaddr - tls->p_memsz) & (tls->p_align - 1)); 624 default: 625 llvm_unreachable("unhandled Config->EMachine"); 626 } 627 } 628 629 uint64_t InputSectionBase::getRelocTargetVA(const InputFile *file, RelType type, 630 int64_t a, uint64_t p, 631 const Symbol &sym, RelExpr expr) { 632 switch (expr) { 633 case R_ABS: 634 case R_DTPREL: 635 case R_RELAX_TLS_LD_TO_LE_ABS: 636 case R_RELAX_GOT_PC_NOPIC: 637 case R_RISCV_ADD: 638 return sym.getVA(a); 639 case R_ADDEND: 640 return a; 641 case R_RELAX_HINT: 642 return 0; 643 case R_ARM_SBREL: 644 return sym.getVA(a) - getARMStaticBase(sym); 645 case R_GOT: 646 case R_RELAX_TLS_GD_TO_IE_ABS: 647 return sym.getGotVA() + a; 648 case R_LOONGARCH_GOT: 649 // The LoongArch TLS GD relocs reuse the R_LARCH_GOT_PC_LO12 reloc type 650 // for their page offsets. The arithmetics are different in the TLS case 651 // so we have to duplicate some logic here. 652 if (sym.hasFlag(NEEDS_TLSGD) && type != R_LARCH_TLS_IE_PC_LO12) 653 // Like R_LOONGARCH_TLSGD_PAGE_PC but taking the absolute value. 654 return in.got->getGlobalDynAddr(sym) + a; 655 return getRelocTargetVA(file, type, a, p, sym, R_GOT); 656 case R_GOTONLY_PC: 657 return in.got->getVA() + a - p; 658 case R_GOTPLTONLY_PC: 659 return in.gotPlt->getVA() + a - p; 660 case R_GOTREL: 661 case R_PPC64_RELAX_TOC: 662 return sym.getVA(a) - in.got->getVA(); 663 case R_GOTPLTREL: 664 return sym.getVA(a) - in.gotPlt->getVA(); 665 case R_GOTPLT: 666 case R_RELAX_TLS_GD_TO_IE_GOTPLT: 667 return sym.getGotVA() + a - in.gotPlt->getVA(); 668 case R_TLSLD_GOT_OFF: 669 case R_GOT_OFF: 670 case R_RELAX_TLS_GD_TO_IE_GOT_OFF: 671 return sym.getGotOffset() + a; 672 case R_AARCH64_GOT_PAGE_PC: 673 case R_AARCH64_RELAX_TLS_GD_TO_IE_PAGE_PC: 674 return getAArch64Page(sym.getGotVA() + a) - getAArch64Page(p); 675 case R_AARCH64_GOT_PAGE: 676 return sym.getGotVA() + a - getAArch64Page(in.got->getVA()); 677 case R_GOT_PC: 678 case R_RELAX_TLS_GD_TO_IE: 679 return sym.getGotVA() + a - p; 680 case R_LOONGARCH_GOT_PAGE_PC: 681 if (sym.hasFlag(NEEDS_TLSGD)) 682 return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p); 683 return getLoongArchPageDelta(sym.getGotVA() + a, p); 684 case R_MIPS_GOTREL: 685 return sym.getVA(a) - in.mipsGot->getGp(file); 686 case R_MIPS_GOT_GP: 687 return in.mipsGot->getGp(file) + a; 688 case R_MIPS_GOT_GP_PC: { 689 // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target 690 // is _gp_disp symbol. In that case we should use the following 691 // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at 692 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 693 // microMIPS variants of these relocations use slightly different 694 // expressions: AHL + GP - P + 3 for %lo() and AHL + GP - P - 1 for %hi() 695 // to correctly handle less-significant bit of the microMIPS symbol. 696 uint64_t v = in.mipsGot->getGp(file) + a - p; 697 if (type == R_MIPS_LO16 || type == R_MICROMIPS_LO16) 698 v += 4; 699 if (type == R_MICROMIPS_LO16 || type == R_MICROMIPS_HI16) 700 v -= 1; 701 return v; 702 } 703 case R_MIPS_GOT_LOCAL_PAGE: 704 // If relocation against MIPS local symbol requires GOT entry, this entry 705 // should be initialized by 'page address'. This address is high 16-bits 706 // of sum the symbol's value and the addend. 707 return in.mipsGot->getVA() + in.mipsGot->getPageEntryOffset(file, sym, a) - 708 in.mipsGot->getGp(file); 709 case R_MIPS_GOT_OFF: 710 case R_MIPS_GOT_OFF32: 711 // In case of MIPS if a GOT relocation has non-zero addend this addend 712 // should be applied to the GOT entry content not to the GOT entry offset. 713 // That is why we use separate expression type. 714 return in.mipsGot->getVA() + in.mipsGot->getSymEntryOffset(file, sym, a) - 715 in.mipsGot->getGp(file); 716 case R_MIPS_TLSGD: 717 return in.mipsGot->getVA() + in.mipsGot->getGlobalDynOffset(file, sym) - 718 in.mipsGot->getGp(file); 719 case R_MIPS_TLSLD: 720 return in.mipsGot->getVA() + in.mipsGot->getTlsIndexOffset(file) - 721 in.mipsGot->getGp(file); 722 case R_AARCH64_PAGE_PC: { 723 uint64_t val = sym.isUndefWeak() ? p + a : sym.getVA(a); 724 return getAArch64Page(val) - getAArch64Page(p); 725 } 726 case R_RISCV_PC_INDIRECT: { 727 if (const Relocation *hiRel = getRISCVPCRelHi20(&sym, a)) 728 return getRelocTargetVA(file, hiRel->type, hiRel->addend, sym.getVA(), 729 *hiRel->sym, hiRel->expr); 730 return 0; 731 } 732 case R_LOONGARCH_PAGE_PC: 733 return getLoongArchPageDelta(sym.getVA(a), p); 734 case R_PC: 735 case R_ARM_PCA: { 736 uint64_t dest; 737 if (expr == R_ARM_PCA) 738 // Some PC relative ARM (Thumb) relocations align down the place. 739 p = p & 0xfffffffc; 740 if (sym.isUndefined()) { 741 // On ARM and AArch64 a branch to an undefined weak resolves to the next 742 // instruction, otherwise the place. On RISC-V, resolve an undefined weak 743 // to the same instruction to cause an infinite loop (making the user 744 // aware of the issue) while ensuring no overflow. 745 // Note: if the symbol is hidden, its binding has been converted to local, 746 // so we just check isUndefined() here. 747 if (config->emachine == EM_ARM) 748 dest = getARMUndefinedRelativeWeakVA(type, a, p); 749 else if (config->emachine == EM_AARCH64) 750 dest = getAArch64UndefinedRelativeWeakVA(type, p) + a; 751 else if (config->emachine == EM_PPC) 752 dest = p; 753 else if (config->emachine == EM_RISCV) 754 dest = getRISCVUndefinedRelativeWeakVA(type, p) + a; 755 else 756 dest = sym.getVA(a); 757 } else { 758 dest = sym.getVA(a); 759 } 760 return dest - p; 761 } 762 case R_PLT: 763 return sym.getPltVA() + a; 764 case R_PLT_PC: 765 case R_PPC64_CALL_PLT: 766 return sym.getPltVA() + a - p; 767 case R_LOONGARCH_PLT_PAGE_PC: 768 return getLoongArchPageDelta(sym.getPltVA() + a, p); 769 case R_PLT_GOTPLT: 770 return sym.getPltVA() + a - in.gotPlt->getVA(); 771 case R_PPC32_PLTREL: 772 // R_PPC_PLTREL24 uses the addend (usually 0 or 0x8000) to indicate r30 773 // stores _GLOBAL_OFFSET_TABLE_ or .got2+0x8000. The addend is ignored for 774 // target VA computation. 775 return sym.getPltVA() - p; 776 case R_PPC64_CALL: { 777 uint64_t symVA = sym.getVA(a); 778 // If we have an undefined weak symbol, we might get here with a symbol 779 // address of zero. That could overflow, but the code must be unreachable, 780 // so don't bother doing anything at all. 781 if (!symVA) 782 return 0; 783 784 // PPC64 V2 ABI describes two entry points to a function. The global entry 785 // point is used for calls where the caller and callee (may) have different 786 // TOC base pointers and r2 needs to be modified to hold the TOC base for 787 // the callee. For local calls the caller and callee share the same 788 // TOC base and so the TOC pointer initialization code should be skipped by 789 // branching to the local entry point. 790 return symVA - p + getPPC64GlobalEntryToLocalEntryOffset(sym.stOther); 791 } 792 case R_PPC64_TOCBASE: 793 return getPPC64TocBase() + a; 794 case R_RELAX_GOT_PC: 795 case R_PPC64_RELAX_GOT_PC: 796 return sym.getVA(a) - p; 797 case R_RELAX_TLS_GD_TO_LE: 798 case R_RELAX_TLS_IE_TO_LE: 799 case R_RELAX_TLS_LD_TO_LE: 800 case R_TPREL: 801 // It is not very clear what to return if the symbol is undefined. With 802 // --noinhibit-exec, even a non-weak undefined reference may reach here. 803 // Just return A, which matches R_ABS, and the behavior of some dynamic 804 // loaders. 805 if (sym.isUndefined()) 806 return a; 807 return getTlsTpOffset(sym) + a; 808 case R_RELAX_TLS_GD_TO_LE_NEG: 809 case R_TPREL_NEG: 810 if (sym.isUndefined()) 811 return a; 812 return -getTlsTpOffset(sym) + a; 813 case R_SIZE: 814 return sym.getSize() + a; 815 case R_TLSDESC: 816 return in.got->getTlsDescAddr(sym) + a; 817 case R_TLSDESC_PC: 818 return in.got->getTlsDescAddr(sym) + a - p; 819 case R_TLSDESC_GOTPLT: 820 return in.got->getTlsDescAddr(sym) + a - in.gotPlt->getVA(); 821 case R_AARCH64_TLSDESC_PAGE: 822 return getAArch64Page(in.got->getTlsDescAddr(sym) + a) - getAArch64Page(p); 823 case R_TLSGD_GOT: 824 return in.got->getGlobalDynOffset(sym) + a; 825 case R_TLSGD_GOTPLT: 826 return in.got->getGlobalDynAddr(sym) + a - in.gotPlt->getVA(); 827 case R_TLSGD_PC: 828 return in.got->getGlobalDynAddr(sym) + a - p; 829 case R_LOONGARCH_TLSGD_PAGE_PC: 830 return getLoongArchPageDelta(in.got->getGlobalDynAddr(sym) + a, p); 831 case R_TLSLD_GOTPLT: 832 return in.got->getVA() + in.got->getTlsIndexOff() + a - in.gotPlt->getVA(); 833 case R_TLSLD_GOT: 834 return in.got->getTlsIndexOff() + a; 835 case R_TLSLD_PC: 836 return in.got->getTlsIndexVA() + a - p; 837 default: 838 llvm_unreachable("invalid expression"); 839 } 840 } 841 842 // This function applies relocations to sections without SHF_ALLOC bit. 843 // Such sections are never mapped to memory at runtime. Debug sections are 844 // an example. Relocations in non-alloc sections are much easier to 845 // handle than in allocated sections because it will never need complex 846 // treatment such as GOT or PLT (because at runtime no one refers them). 847 // So, we handle relocations for non-alloc sections directly in this 848 // function as a performance optimization. 849 template <class ELFT, class RelTy> 850 void InputSection::relocateNonAlloc(uint8_t *buf, ArrayRef<RelTy> rels) { 851 const unsigned bits = sizeof(typename ELFT::uint) * 8; 852 const TargetInfo &target = *elf::target; 853 const bool isDebug = isDebugSection(*this); 854 const bool isDebugLocOrRanges = 855 isDebug && (name == ".debug_loc" || name == ".debug_ranges"); 856 const bool isDebugLine = isDebug && name == ".debug_line"; 857 std::optional<uint64_t> tombstone; 858 for (const auto &patAndValue : llvm::reverse(config->deadRelocInNonAlloc)) 859 if (patAndValue.first.match(this->name)) { 860 tombstone = patAndValue.second; 861 break; 862 } 863 864 for (const RelTy &rel : rels) { 865 RelType type = rel.getType(config->isMips64EL); 866 867 // GCC 8.0 or earlier have a bug that they emit R_386_GOTPC relocations 868 // against _GLOBAL_OFFSET_TABLE_ for .debug_info. The bug has been fixed 869 // in 2017 (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=82630), but we 870 // need to keep this bug-compatible code for a while. 871 if (config->emachine == EM_386 && type == R_386_GOTPC) 872 continue; 873 874 uint64_t offset = rel.r_offset; 875 uint8_t *bufLoc = buf + offset; 876 int64_t addend = getAddend<ELFT>(rel); 877 if (!RelTy::IsRela) 878 addend += target.getImplicitAddend(bufLoc, type); 879 880 Symbol &sym = getFile<ELFT>()->getRelocTargetSym(rel); 881 RelExpr expr = target.getRelExpr(type, sym, bufLoc); 882 if (expr == R_NONE) 883 continue; 884 885 if (tombstone || 886 (isDebug && (type == target.symbolicRel || expr == R_DTPREL))) { 887 // Resolve relocations in .debug_* referencing (discarded symbols or ICF 888 // folded section symbols) to a tombstone value. Resolving to addend is 889 // unsatisfactory because the result address range may collide with a 890 // valid range of low address, or leave multiple CUs claiming ownership of 891 // the same range of code, which may confuse consumers. 892 // 893 // To address the problems, we use -1 as a tombstone value for most 894 // .debug_* sections. We have to ignore the addend because we don't want 895 // to resolve an address attribute (which may have a non-zero addend) to 896 // -1+addend (wrap around to a low address). 897 // 898 // R_DTPREL type relocations represent an offset into the dynamic thread 899 // vector. The computed value is st_value plus a non-negative offset. 900 // Negative values are invalid, so -1 can be used as the tombstone value. 901 // 902 // If the referenced symbol is discarded (made Undefined), or the 903 // section defining the referenced symbol is garbage collected, 904 // sym.getOutputSection() is nullptr. `ds->folded` catches the ICF folded 905 // case. However, resolving a relocation in .debug_line to -1 would stop 906 // debugger users from setting breakpoints on the folded-in function, so 907 // exclude .debug_line. 908 // 909 // For pre-DWARF-v5 .debug_loc and .debug_ranges, -1 is a reserved value 910 // (base address selection entry), use 1 (which is used by GNU ld for 911 // .debug_ranges). 912 // 913 // TODO To reduce disruption, we use 0 instead of -1 as the tombstone 914 // value. Enable -1 in a future release. 915 auto *ds = dyn_cast<Defined>(&sym); 916 if (!sym.getOutputSection() || (ds && ds->folded && !isDebugLine)) { 917 // If -z dead-reloc-in-nonalloc= is specified, respect it. 918 const uint64_t value = tombstone ? SignExtend64<bits>(*tombstone) 919 : (isDebugLocOrRanges ? 1 : 0); 920 target.relocateNoSym(bufLoc, type, value); 921 continue; 922 } 923 } 924 925 // For a relocatable link, only tombstone values are applied. 926 if (config->relocatable) 927 continue; 928 929 if (expr == R_SIZE) { 930 target.relocateNoSym(bufLoc, type, 931 SignExtend64<bits>(sym.getSize() + addend)); 932 continue; 933 } 934 935 // R_ABS/R_DTPREL and some other relocations can be used from non-SHF_ALLOC 936 // sections. 937 if (expr == R_ABS || expr == R_DTPREL || expr == R_GOTPLTREL || 938 expr == R_RISCV_ADD) { 939 target.relocateNoSym(bufLoc, type, SignExtend64<bits>(sym.getVA(addend))); 940 continue; 941 } 942 943 std::string msg = getLocation(offset) + ": has non-ABS relocation " + 944 toString(type) + " against symbol '" + toString(sym) + 945 "'"; 946 if (expr != R_PC && expr != R_ARM_PCA) { 947 error(msg); 948 return; 949 } 950 951 // If the control reaches here, we found a PC-relative relocation in a 952 // non-ALLOC section. Since non-ALLOC section is not loaded into memory 953 // at runtime, the notion of PC-relative doesn't make sense here. So, 954 // this is a usage error. However, GNU linkers historically accept such 955 // relocations without any errors and relocate them as if they were at 956 // address 0. For bug-compatibility, we accept them with warnings. We 957 // know Steel Bank Common Lisp as of 2018 have this bug. 958 warn(msg); 959 target.relocateNoSym( 960 bufLoc, type, 961 SignExtend64<bits>(sym.getVA(addend - offset - outSecOff))); 962 } 963 } 964 965 // This is used when '-r' is given. 966 // For REL targets, InputSection::copyRelocations() may store artificial 967 // relocations aimed to update addends. They are handled in relocateAlloc() 968 // for allocatable sections, and this function does the same for 969 // non-allocatable sections, such as sections with debug information. 970 static void relocateNonAllocForRelocatable(InputSection *sec, uint8_t *buf) { 971 const unsigned bits = config->is64 ? 64 : 32; 972 973 for (const Relocation &rel : sec->relocs()) { 974 // InputSection::copyRelocations() adds only R_ABS relocations. 975 assert(rel.expr == R_ABS); 976 uint8_t *bufLoc = buf + rel.offset; 977 uint64_t targetVA = SignExtend64(rel.sym->getVA(rel.addend), bits); 978 target->relocate(bufLoc, rel, targetVA); 979 } 980 } 981 982 template <class ELFT> 983 void InputSectionBase::relocate(uint8_t *buf, uint8_t *bufEnd) { 984 if ((flags & SHF_EXECINSTR) && LLVM_UNLIKELY(getFile<ELFT>()->splitStack)) 985 adjustSplitStackFunctionPrologues<ELFT>(buf, bufEnd); 986 987 if (flags & SHF_ALLOC) { 988 target->relocateAlloc(*this, buf); 989 return; 990 } 991 992 auto *sec = cast<InputSection>(this); 993 if (config->relocatable) 994 relocateNonAllocForRelocatable(sec, buf); 995 // For a relocatable link, also call relocateNonAlloc() to rewrite applicable 996 // locations with tombstone values. 997 const RelsOrRelas<ELFT> rels = sec->template relsOrRelas<ELFT>(); 998 if (rels.areRelocsRel()) 999 sec->relocateNonAlloc<ELFT>(buf, rels.rels); 1000 else 1001 sec->relocateNonAlloc<ELFT>(buf, rels.relas); 1002 } 1003 1004 // For each function-defining prologue, find any calls to __morestack, 1005 // and replace them with calls to __morestack_non_split. 1006 static void switchMorestackCallsToMorestackNonSplit( 1007 DenseSet<Defined *> &prologues, 1008 SmallVector<Relocation *, 0> &morestackCalls) { 1009 1010 // If the target adjusted a function's prologue, all calls to 1011 // __morestack inside that function should be switched to 1012 // __morestack_non_split. 1013 Symbol *moreStackNonSplit = symtab.find("__morestack_non_split"); 1014 if (!moreStackNonSplit) { 1015 error("mixing split-stack objects requires a definition of " 1016 "__morestack_non_split"); 1017 return; 1018 } 1019 1020 // Sort both collections to compare addresses efficiently. 1021 llvm::sort(morestackCalls, [](const Relocation *l, const Relocation *r) { 1022 return l->offset < r->offset; 1023 }); 1024 std::vector<Defined *> functions(prologues.begin(), prologues.end()); 1025 llvm::sort(functions, [](const Defined *l, const Defined *r) { 1026 return l->value < r->value; 1027 }); 1028 1029 auto it = morestackCalls.begin(); 1030 for (Defined *f : functions) { 1031 // Find the first call to __morestack within the function. 1032 while (it != morestackCalls.end() && (*it)->offset < f->value) 1033 ++it; 1034 // Adjust all calls inside the function. 1035 while (it != morestackCalls.end() && (*it)->offset < f->value + f->size) { 1036 (*it)->sym = moreStackNonSplit; 1037 ++it; 1038 } 1039 } 1040 } 1041 1042 static bool enclosingPrologueAttempted(uint64_t offset, 1043 const DenseSet<Defined *> &prologues) { 1044 for (Defined *f : prologues) 1045 if (f->value <= offset && offset < f->value + f->size) 1046 return true; 1047 return false; 1048 } 1049 1050 // If a function compiled for split stack calls a function not 1051 // compiled for split stack, then the caller needs its prologue 1052 // adjusted to ensure that the called function will have enough stack 1053 // available. Find those functions, and adjust their prologues. 1054 template <class ELFT> 1055 void InputSectionBase::adjustSplitStackFunctionPrologues(uint8_t *buf, 1056 uint8_t *end) { 1057 DenseSet<Defined *> prologues; 1058 SmallVector<Relocation *, 0> morestackCalls; 1059 1060 for (Relocation &rel : relocs()) { 1061 // Ignore calls into the split-stack api. 1062 if (rel.sym->getName().starts_with("__morestack")) { 1063 if (rel.sym->getName().equals("__morestack")) 1064 morestackCalls.push_back(&rel); 1065 continue; 1066 } 1067 1068 // A relocation to non-function isn't relevant. Sometimes 1069 // __morestack is not marked as a function, so this check comes 1070 // after the name check. 1071 if (rel.sym->type != STT_FUNC) 1072 continue; 1073 1074 // If the callee's-file was compiled with split stack, nothing to do. In 1075 // this context, a "Defined" symbol is one "defined by the binary currently 1076 // being produced". So an "undefined" symbol might be provided by a shared 1077 // library. It is not possible to tell how such symbols were compiled, so be 1078 // conservative. 1079 if (Defined *d = dyn_cast<Defined>(rel.sym)) 1080 if (InputSection *isec = cast_or_null<InputSection>(d->section)) 1081 if (!isec || !isec->getFile<ELFT>() || isec->getFile<ELFT>()->splitStack) 1082 continue; 1083 1084 if (enclosingPrologueAttempted(rel.offset, prologues)) 1085 continue; 1086 1087 if (Defined *f = getEnclosingFunction(rel.offset)) { 1088 prologues.insert(f); 1089 if (target->adjustPrologueForCrossSplitStack(buf + f->value, end, 1090 f->stOther)) 1091 continue; 1092 if (!getFile<ELFT>()->someNoSplitStack) 1093 error(lld::toString(this) + ": " + f->getName() + 1094 " (with -fsplit-stack) calls " + rel.sym->getName() + 1095 " (without -fsplit-stack), but couldn't adjust its prologue"); 1096 } 1097 } 1098 1099 if (target->needsMoreStackNonSplit) 1100 switchMorestackCallsToMorestackNonSplit(prologues, morestackCalls); 1101 } 1102 1103 template <class ELFT> void InputSection::writeTo(uint8_t *buf) { 1104 if (LLVM_UNLIKELY(type == SHT_NOBITS)) 1105 return; 1106 // If -r or --emit-relocs is given, then an InputSection 1107 // may be a relocation section. 1108 if (LLVM_UNLIKELY(type == SHT_RELA)) { 1109 copyRelocations<ELFT>(buf, getDataAs<typename ELFT::Rela>()); 1110 return; 1111 } 1112 if (LLVM_UNLIKELY(type == SHT_REL)) { 1113 copyRelocations<ELFT>(buf, getDataAs<typename ELFT::Rel>()); 1114 return; 1115 } 1116 1117 // If -r is given, we may have a SHT_GROUP section. 1118 if (LLVM_UNLIKELY(type == SHT_GROUP)) { 1119 copyShtGroup<ELFT>(buf); 1120 return; 1121 } 1122 1123 // If this is a compressed section, uncompress section contents directly 1124 // to the buffer. 1125 if (compressed) { 1126 auto *hdr = reinterpret_cast<const typename ELFT::Chdr *>(content_); 1127 auto compressed = ArrayRef<uint8_t>(content_, compressedSize) 1128 .slice(sizeof(typename ELFT::Chdr)); 1129 size_t size = this->size; 1130 if (Error e = hdr->ch_type == ELFCOMPRESS_ZLIB 1131 ? compression::zlib::decompress(compressed, buf, size) 1132 : compression::zstd::decompress(compressed, buf, size)) 1133 fatal(toString(this) + 1134 ": decompress failed: " + llvm::toString(std::move(e))); 1135 uint8_t *bufEnd = buf + size; 1136 relocate<ELFT>(buf, bufEnd); 1137 return; 1138 } 1139 1140 // Copy section contents from source object file to output file 1141 // and then apply relocations. 1142 memcpy(buf, content().data(), content().size()); 1143 relocate<ELFT>(buf, buf + content().size()); 1144 } 1145 1146 void InputSection::replace(InputSection *other) { 1147 addralign = std::max(addralign, other->addralign); 1148 1149 // When a section is replaced with another section that was allocated to 1150 // another partition, the replacement section (and its associated sections) 1151 // need to be placed in the main partition so that both partitions will be 1152 // able to access it. 1153 if (partition != other->partition) { 1154 partition = 1; 1155 for (InputSection *isec : dependentSections) 1156 isec->partition = 1; 1157 } 1158 1159 other->repl = repl; 1160 other->markDead(); 1161 } 1162 1163 template <class ELFT> 1164 EhInputSection::EhInputSection(ObjFile<ELFT> &f, 1165 const typename ELFT::Shdr &header, 1166 StringRef name) 1167 : InputSectionBase(f, header, name, InputSectionBase::EHFrame) {} 1168 1169 SyntheticSection *EhInputSection::getParent() const { 1170 return cast_or_null<SyntheticSection>(parent); 1171 } 1172 1173 // .eh_frame is a sequence of CIE or FDE records. 1174 // This function splits an input section into records and returns them. 1175 template <class ELFT> void EhInputSection::split() { 1176 const RelsOrRelas<ELFT> rels = relsOrRelas<ELFT>(); 1177 // getReloc expects the relocations to be sorted by r_offset. See the comment 1178 // in scanRelocs. 1179 if (rels.areRelocsRel()) { 1180 SmallVector<typename ELFT::Rel, 0> storage; 1181 split<ELFT>(sortRels(rels.rels, storage)); 1182 } else { 1183 SmallVector<typename ELFT::Rela, 0> storage; 1184 split<ELFT>(sortRels(rels.relas, storage)); 1185 } 1186 } 1187 1188 template <class ELFT, class RelTy> 1189 void EhInputSection::split(ArrayRef<RelTy> rels) { 1190 ArrayRef<uint8_t> d = content(); 1191 const char *msg = nullptr; 1192 unsigned relI = 0; 1193 while (!d.empty()) { 1194 if (d.size() < 4) { 1195 msg = "CIE/FDE too small"; 1196 break; 1197 } 1198 uint64_t size = endian::read32<ELFT::TargetEndianness>(d.data()); 1199 if (size == 0) // ZERO terminator 1200 break; 1201 uint32_t id = endian::read32<ELFT::TargetEndianness>(d.data() + 4); 1202 size += 4; 1203 if (LLVM_UNLIKELY(size > d.size())) { 1204 // If it is 0xFFFFFFFF, the next 8 bytes contain the size instead, 1205 // but we do not support that format yet. 1206 msg = size == UINT32_MAX + uint64_t(4) 1207 ? "CIE/FDE too large" 1208 : "CIE/FDE ends past the end of the section"; 1209 break; 1210 } 1211 1212 // Find the first relocation that points to [off,off+size). Relocations 1213 // have been sorted by r_offset. 1214 const uint64_t off = d.data() - content().data(); 1215 while (relI != rels.size() && rels[relI].r_offset < off) 1216 ++relI; 1217 unsigned firstRel = -1; 1218 if (relI != rels.size() && rels[relI].r_offset < off + size) 1219 firstRel = relI; 1220 (id == 0 ? cies : fdes).emplace_back(off, this, size, firstRel); 1221 d = d.slice(size); 1222 } 1223 if (msg) 1224 errorOrWarn("corrupted .eh_frame: " + Twine(msg) + "\n>>> defined in " + 1225 getObjMsg(d.data() - content().data())); 1226 } 1227 1228 // Return the offset in an output section for a given input offset. 1229 uint64_t EhInputSection::getParentOffset(uint64_t offset) const { 1230 auto it = partition_point( 1231 fdes, [=](EhSectionPiece p) { return p.inputOff <= offset; }); 1232 if (it == fdes.begin() || it[-1].inputOff + it[-1].size <= offset) { 1233 it = partition_point( 1234 cies, [=](EhSectionPiece p) { return p.inputOff <= offset; }); 1235 if (it == cies.begin()) // invalid piece 1236 return offset; 1237 } 1238 if (it[-1].outputOff == -1) // invalid piece 1239 return offset - it[-1].inputOff; 1240 return it[-1].outputOff + (offset - it[-1].inputOff); 1241 } 1242 1243 static size_t findNull(StringRef s, size_t entSize) { 1244 for (unsigned i = 0, n = s.size(); i != n; i += entSize) { 1245 const char *b = s.begin() + i; 1246 if (std::all_of(b, b + entSize, [](char c) { return c == 0; })) 1247 return i; 1248 } 1249 llvm_unreachable(""); 1250 } 1251 1252 // Split SHF_STRINGS section. Such section is a sequence of 1253 // null-terminated strings. 1254 void MergeInputSection::splitStrings(StringRef s, size_t entSize) { 1255 const bool live = !(flags & SHF_ALLOC) || !config->gcSections; 1256 const char *p = s.data(), *end = s.data() + s.size(); 1257 if (!std::all_of(end - entSize, end, [](char c) { return c == 0; })) 1258 fatal(toString(this) + ": string is not null terminated"); 1259 if (entSize == 1) { 1260 // Optimize the common case. 1261 do { 1262 size_t size = strlen(p); 1263 pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live); 1264 p += size + 1; 1265 } while (p != end); 1266 } else { 1267 do { 1268 size_t size = findNull(StringRef(p, end - p), entSize); 1269 pieces.emplace_back(p - s.begin(), xxh3_64bits(StringRef(p, size)), live); 1270 p += size + entSize; 1271 } while (p != end); 1272 } 1273 } 1274 1275 // Split non-SHF_STRINGS section. Such section is a sequence of 1276 // fixed size records. 1277 void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> data, 1278 size_t entSize) { 1279 size_t size = data.size(); 1280 assert((size % entSize) == 0); 1281 const bool live = !(flags & SHF_ALLOC) || !config->gcSections; 1282 1283 pieces.resize_for_overwrite(size / entSize); 1284 for (size_t i = 0, j = 0; i != size; i += entSize, j++) 1285 pieces[j] = {i, (uint32_t)xxh3_64bits(data.slice(i, entSize)), live}; 1286 } 1287 1288 template <class ELFT> 1289 MergeInputSection::MergeInputSection(ObjFile<ELFT> &f, 1290 const typename ELFT::Shdr &header, 1291 StringRef name) 1292 : InputSectionBase(f, header, name, InputSectionBase::Merge) {} 1293 1294 MergeInputSection::MergeInputSection(uint64_t flags, uint32_t type, 1295 uint64_t entsize, ArrayRef<uint8_t> data, 1296 StringRef name) 1297 : InputSectionBase(nullptr, flags, type, entsize, /*Link*/ 0, /*Info*/ 0, 1298 /*Alignment*/ entsize, data, name, SectionBase::Merge) {} 1299 1300 // This function is called after we obtain a complete list of input sections 1301 // that need to be linked. This is responsible to split section contents 1302 // into small chunks for further processing. 1303 // 1304 // Note that this function is called from parallelForEach. This must be 1305 // thread-safe (i.e. no memory allocation from the pools). 1306 void MergeInputSection::splitIntoPieces() { 1307 assert(pieces.empty()); 1308 1309 if (flags & SHF_STRINGS) 1310 splitStrings(toStringRef(contentMaybeDecompress()), entsize); 1311 else 1312 splitNonStrings(contentMaybeDecompress(), entsize); 1313 } 1314 1315 SectionPiece &MergeInputSection::getSectionPiece(uint64_t offset) { 1316 if (content().size() <= offset) 1317 fatal(toString(this) + ": offset is outside the section"); 1318 return partition_point( 1319 pieces, [=](SectionPiece p) { return p.inputOff <= offset; })[-1]; 1320 } 1321 1322 // Return the offset in an output section for a given input offset. 1323 uint64_t MergeInputSection::getParentOffset(uint64_t offset) const { 1324 const SectionPiece &piece = getSectionPiece(offset); 1325 return piece.outputOff + (offset - piece.inputOff); 1326 } 1327 1328 template InputSection::InputSection(ObjFile<ELF32LE> &, const ELF32LE::Shdr &, 1329 StringRef); 1330 template InputSection::InputSection(ObjFile<ELF32BE> &, const ELF32BE::Shdr &, 1331 StringRef); 1332 template InputSection::InputSection(ObjFile<ELF64LE> &, const ELF64LE::Shdr &, 1333 StringRef); 1334 template InputSection::InputSection(ObjFile<ELF64BE> &, const ELF64BE::Shdr &, 1335 StringRef); 1336 1337 template void InputSection::writeTo<ELF32LE>(uint8_t *); 1338 template void InputSection::writeTo<ELF32BE>(uint8_t *); 1339 template void InputSection::writeTo<ELF64LE>(uint8_t *); 1340 template void InputSection::writeTo<ELF64BE>(uint8_t *); 1341 1342 template RelsOrRelas<ELF32LE> InputSectionBase::relsOrRelas<ELF32LE>() const; 1343 template RelsOrRelas<ELF32BE> InputSectionBase::relsOrRelas<ELF32BE>() const; 1344 template RelsOrRelas<ELF64LE> InputSectionBase::relsOrRelas<ELF64LE>() const; 1345 template RelsOrRelas<ELF64BE> InputSectionBase::relsOrRelas<ELF64BE>() const; 1346 1347 template MergeInputSection::MergeInputSection(ObjFile<ELF32LE> &, 1348 const ELF32LE::Shdr &, StringRef); 1349 template MergeInputSection::MergeInputSection(ObjFile<ELF32BE> &, 1350 const ELF32BE::Shdr &, StringRef); 1351 template MergeInputSection::MergeInputSection(ObjFile<ELF64LE> &, 1352 const ELF64LE::Shdr &, StringRef); 1353 template MergeInputSection::MergeInputSection(ObjFile<ELF64BE> &, 1354 const ELF64BE::Shdr &, StringRef); 1355 1356 template EhInputSection::EhInputSection(ObjFile<ELF32LE> &, 1357 const ELF32LE::Shdr &, StringRef); 1358 template EhInputSection::EhInputSection(ObjFile<ELF32BE> &, 1359 const ELF32BE::Shdr &, StringRef); 1360 template EhInputSection::EhInputSection(ObjFile<ELF64LE> &, 1361 const ELF64LE::Shdr &, StringRef); 1362 template EhInputSection::EhInputSection(ObjFile<ELF64BE> &, 1363 const ELF64BE::Shdr &, StringRef); 1364 1365 template void EhInputSection::split<ELF32LE>(); 1366 template void EhInputSection::split<ELF32BE>(); 1367 template void EhInputSection::split<ELF64LE>(); 1368 template void EhInputSection::split<ELF64BE>(); 1369