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 "Memory.h" 17 #include "OutputSections.h" 18 #include "Relocations.h" 19 #include "SyntheticSections.h" 20 #include "Target.h" 21 #include "Thunks.h" 22 #include "llvm/Object/Decompressor.h" 23 #include "llvm/Support/Compression.h" 24 #include "llvm/Support/Endian.h" 25 #include "llvm/Support/Path.h" 26 #include <mutex> 27 28 using namespace llvm; 29 using namespace llvm::ELF; 30 using namespace llvm::object; 31 using namespace llvm::support; 32 using namespace llvm::support::endian; 33 using namespace llvm::sys; 34 35 using namespace lld; 36 using namespace lld::elf; 37 38 std::vector<InputSectionBase *> elf::InputSections; 39 40 // Returns a string to construct an error message. 41 std::string lld::toString(const InputSectionBase *Sec) { 42 // File can be absent if section is synthetic. 43 std::string FileName = Sec->File ? Sec->File->getName() : "<internal>"; 44 return (FileName + ":(" + Sec->Name + ")").str(); 45 } 46 47 template <class ELFT> 48 static ArrayRef<uint8_t> getSectionContents(elf::ObjectFile<ELFT> *File, 49 const typename ELFT::Shdr *Hdr) { 50 if (!File || Hdr->sh_type == SHT_NOBITS) 51 return makeArrayRef<uint8_t>(nullptr, Hdr->sh_size); 52 return check(File->getObj().getSectionContents(Hdr)); 53 } 54 55 InputSectionBase::InputSectionBase(InputFile *File, uint64_t Flags, 56 uint32_t Type, uint64_t Entsize, 57 uint32_t Link, uint32_t Info, 58 uint32_t Alignment, ArrayRef<uint8_t> Data, 59 StringRef Name, Kind SectionKind) 60 : SectionBase(SectionKind, Name, Flags, Entsize, Alignment, Type, Info, 61 Link), 62 File(File), Data(Data), Repl(this) { 63 Live = !Config->GcSections || !(Flags & SHF_ALLOC); 64 Assigned = false; 65 NumRelocations = 0; 66 AreRelocsRela = false; 67 68 // The ELF spec states that a value of 0 means the section has 69 // no alignment constraits. 70 uint32_t V = std::max<uint64_t>(Alignment, 1); 71 if (!isPowerOf2_64(V)) 72 fatal(toString(File) + ": section sh_addralign is not a power of 2"); 73 this->Alignment = V; 74 } 75 76 // GNU assembler 2.24 and LLVM 4.0.0's MC (the newest release as of 77 // March 2017) fail to infer section types for sections starting with 78 // ".init_array." or ".fini_array.". They set SHT_PROGBITS instead of 79 // SHF_INIT_ARRAY. As a result, the following assembler directive 80 // creates ".init_array.100" with SHT_PROGBITS, for example. 81 // 82 // .section .init_array.100, "aw" 83 // 84 // This function forces SHT_{INIT,FINI}_ARRAY so that we can handle 85 // incorrect inputs as if they were correct from the beginning. 86 static uint64_t getType(uint64_t Type, StringRef Name) { 87 if (Type == SHT_PROGBITS && Name.startswith(".init_array.")) 88 return SHT_INIT_ARRAY; 89 if (Type == SHT_PROGBITS && Name.startswith(".fini_array.")) 90 return SHT_FINI_ARRAY; 91 return Type; 92 } 93 94 template <class ELFT> 95 InputSectionBase::InputSectionBase(elf::ObjectFile<ELFT> *File, 96 const typename ELFT::Shdr *Hdr, 97 StringRef Name, Kind SectionKind) 98 : InputSectionBase(File, Hdr->sh_flags & ~SHF_INFO_LINK, 99 getType(Hdr->sh_type, Name), Hdr->sh_entsize, 100 Hdr->sh_link, Hdr->sh_info, Hdr->sh_addralign, 101 getSectionContents(File, Hdr), Name, SectionKind) { 102 // We reject object files having insanely large alignments even though 103 // they are allowed by the spec. I think 4GB is a reasonable limitation. 104 // We might want to relax this in the future. 105 if (Hdr->sh_addralign > UINT32_MAX) 106 fatal(toString(File) + ": section sh_addralign is too large"); 107 } 108 109 size_t InputSectionBase::getSize() const { 110 if (auto *S = dyn_cast<SyntheticSection>(this)) 111 return S->getSize(); 112 113 return Data.size(); 114 } 115 116 uint64_t InputSectionBase::getOffsetInFile() const { 117 const uint8_t *FileStart = (const uint8_t *)File->MB.getBufferStart(); 118 const uint8_t *SecStart = Data.begin(); 119 return SecStart - FileStart; 120 } 121 122 uint64_t SectionBase::getOffset(uint64_t Offset) const { 123 switch (kind()) { 124 case Output: { 125 auto *OS = cast<OutputSection>(this); 126 // For output sections we treat offset -1 as the end of the section. 127 return Offset == uint64_t(-1) ? OS->Size : Offset; 128 } 129 case Regular: 130 return cast<InputSection>(this)->OutSecOff + Offset; 131 case Synthetic: { 132 auto *IS = cast<InputSection>(this); 133 // For synthetic sections we treat offset -1 as the end of the section. 134 return IS->OutSecOff + (Offset == uint64_t(-1) ? IS->getSize() : Offset); 135 } 136 case EHFrame: 137 // The file crtbeginT.o has relocations pointing to the start of an empty 138 // .eh_frame that is known to be the first in the link. It does that to 139 // identify the start of the output .eh_frame. 140 return Offset; 141 case Merge: 142 const MergeInputSection *MS = cast<MergeInputSection>(this); 143 if (MS->MergeSec) 144 return MS->MergeSec->OutSecOff + MS->getOffset(Offset); 145 return MS->getOffset(Offset); 146 } 147 llvm_unreachable("invalid section kind"); 148 } 149 150 OutputSection *SectionBase::getOutputSection() { 151 if (auto *IS = dyn_cast<InputSection>(this)) 152 return IS->OutSec; 153 if (auto *MS = dyn_cast<MergeInputSection>(this)) 154 return MS->MergeSec ? MS->MergeSec->OutSec : nullptr; 155 if (auto *EH = dyn_cast<EhInputSection>(this)) 156 return EH->EHSec->OutSec; 157 return cast<OutputSection>(this); 158 } 159 160 // Uncompress section contents. Note that this function is called 161 // from parallel_for_each, so it must be thread-safe. 162 void InputSectionBase::uncompress() { 163 Decompressor Dec = check(Decompressor::create(Name, toStringRef(Data), 164 Config->IsLE, Config->Is64)); 165 166 size_t Size = Dec.getDecompressedSize(); 167 char *OutputBuf; 168 { 169 static std::mutex Mu; 170 std::lock_guard<std::mutex> Lock(Mu); 171 OutputBuf = BAlloc.Allocate<char>(Size); 172 } 173 174 if (Error E = Dec.decompress({OutputBuf, Size})) 175 fatal(toString(this) + 176 ": decompress failed: " + llvm::toString(std::move(E))); 177 Data = ArrayRef<uint8_t>((uint8_t *)OutputBuf, Size); 178 } 179 180 uint64_t SectionBase::getOffset(const DefinedRegular &Sym) const { 181 return getOffset(Sym.Value); 182 } 183 184 InputSectionBase *InputSectionBase::getLinkOrderDep() const { 185 if ((Flags & SHF_LINK_ORDER) && Link != 0) 186 return File->getSections()[Link]; 187 return nullptr; 188 } 189 190 // Returns a source location string. Used to construct an error message. 191 template <class ELFT> 192 std::string InputSectionBase::getLocation(uint64_t Offset) { 193 // We don't have file for synthetic sections. 194 if (getFile<ELFT>() == nullptr) 195 return (Config->OutputFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")") 196 .str(); 197 198 // First check if we can get desired values from debugging information. 199 std::string LineInfo = getFile<ELFT>()->getLineInfo(this, Offset); 200 if (!LineInfo.empty()) 201 return LineInfo; 202 203 // File->SourceFile contains STT_FILE symbol that contains a 204 // source file name. If it's missing, we use an object file name. 205 std::string SrcFile = getFile<ELFT>()->SourceFile; 206 if (SrcFile.empty()) 207 SrcFile = toString(File); 208 209 // Find a function symbol that encloses a given location. 210 for (SymbolBody *B : getFile<ELFT>()->getSymbols()) 211 if (auto *D = dyn_cast<DefinedRegular>(B)) 212 if (D->Section == this && D->Type == STT_FUNC) 213 if (D->Value <= Offset && Offset < D->Value + D->Size) 214 return SrcFile + ":(function " + toString(*D) + ")"; 215 216 // If there's no symbol, print out the offset in the section. 217 return (SrcFile + ":(" + Name + "+0x" + utohexstr(Offset) + ")").str(); 218 } 219 220 // Returns a source location string. This function is intended to be 221 // used for constructing an error message. The returned message looks 222 // like this: 223 // 224 // foo.c:42 (/home/alice/possibly/very/long/path/foo.c:42) 225 // 226 // Returns an empty string if there's no way to get line info. 227 template <class ELFT> std::string InputSectionBase::getSrcMsg(uint64_t Offset) { 228 // Synthetic sections don't have input files. 229 elf::ObjectFile<ELFT> *File = getFile<ELFT>(); 230 if (!File) 231 return ""; 232 233 Optional<DILineInfo> Info = File->getDILineInfo(this, Offset); 234 235 // File->SourceFile contains STT_FILE symbol, and that is a last resort. 236 if (!Info) 237 return File->SourceFile; 238 239 std::string Path = Info->FileName; 240 std::string Filename = path::filename(Path); 241 std::string Lineno = ":" + std::to_string(Info->Line); 242 if (Filename == Path) 243 return Filename + Lineno; 244 return Filename + Lineno + " (" + Path + Lineno + ")"; 245 } 246 247 // Returns a filename string along with an optional section name. This 248 // function is intended to be used for constructing an error 249 // message. The returned message looks like this: 250 // 251 // path/to/foo.o:(function bar) 252 // 253 // or 254 // 255 // path/to/foo.o:(function bar) in archive path/to/bar.a 256 template <class ELFT> std::string InputSectionBase::getObjMsg(uint64_t Off) { 257 // Synthetic sections don't have input files. 258 elf::ObjectFile<ELFT> *File = getFile<ELFT>(); 259 std::string Filename = File ? File->getName() : "(internal)"; 260 261 std::string Archive; 262 if (!File->ArchiveName.empty()) 263 Archive = (" in archive " + File->ArchiveName).str(); 264 265 // Find a symbol that encloses a given location. 266 for (SymbolBody *B : getFile<ELFT>()->getSymbols()) 267 if (auto *D = dyn_cast<DefinedRegular>(B)) 268 if (D->Section == this && D->Value <= Off && Off < D->Value + D->Size) 269 return Filename + ":(" + toString(*D) + ")" + Archive; 270 271 // If there's no symbol, print out the offset in the section. 272 return (Filename + ":(" + Name + "+0x" + utohexstr(Off) + ")" + Archive) 273 .str(); 274 } 275 276 InputSectionBase InputSectionBase::Discarded; 277 278 InputSection::InputSection(uint64_t Flags, uint32_t Type, uint32_t Alignment, 279 ArrayRef<uint8_t> Data, StringRef Name, Kind K) 280 : InputSectionBase(nullptr, Flags, Type, 281 /*Entsize*/ 0, /*Link*/ 0, /*Info*/ 0, Alignment, Data, 282 Name, K) {} 283 284 template <class ELFT> 285 InputSection::InputSection(elf::ObjectFile<ELFT> *F, 286 const typename ELFT::Shdr *Header, StringRef Name) 287 : InputSectionBase(F, Header, Name, InputSectionBase::Regular) {} 288 289 bool InputSection::classof(const SectionBase *S) { 290 return S->kind() == SectionBase::Regular || 291 S->kind() == SectionBase::Synthetic; 292 } 293 294 bool InputSectionBase::classof(const SectionBase *S) { 295 return S->kind() != Output; 296 } 297 298 InputSectionBase *InputSection::getRelocatedSection() { 299 assert(this->Type == SHT_RELA || this->Type == SHT_REL); 300 ArrayRef<InputSectionBase *> Sections = this->File->getSections(); 301 return Sections[this->Info]; 302 } 303 304 // This is used for -r and --emit-relocs. We can't use memcpy to copy 305 // relocations because we need to update symbol table offset and section index 306 // for each relocation. So we copy relocations one by one. 307 template <class ELFT, class RelTy> 308 void InputSection::copyRelocations(uint8_t *Buf, ArrayRef<RelTy> Rels) { 309 InputSectionBase *RelocatedSection = getRelocatedSection(); 310 311 // Loop is slow and have complexity O(N*M), where N - amount of 312 // relocations and M - amount of symbols in symbol table. 313 // That happens because getSymbolIndex(...) call below performs 314 // simple linear search. 315 for (const RelTy &Rel : Rels) { 316 uint32_t Type = Rel.getType(Config->IsMips64EL); 317 SymbolBody &Body = this->getFile<ELFT>()->getRelocTargetSym(Rel); 318 319 auto *P = reinterpret_cast<typename ELFT::Rela *>(Buf); 320 Buf += sizeof(RelTy); 321 322 if (Config->IsRela) 323 P->r_addend = getAddend<ELFT>(Rel); 324 325 // Output section VA is zero for -r, so r_offset is an offset within the 326 // section, but for --emit-relocs it is an virtual address. 327 P->r_offset = RelocatedSection->OutSec->Addr + 328 RelocatedSection->getOffset(Rel.r_offset); 329 P->setSymbolAndType(In<ELFT>::SymTab->getSymbolIndex(&Body), Type, 330 Config->IsMips64EL); 331 332 if (Body.Type == STT_SECTION) { 333 // We combine multiple section symbols into only one per 334 // section. This means we have to update the addend. That is 335 // trivial for Elf_Rela, but for Elf_Rel we have to write to the 336 // section data. We do that by adding to the Relocation vector. 337 338 // .eh_frame is horribly special and can reference discarded sections. To 339 // avoid having to parse and recreate .eh_frame, we just replace any 340 // relocation in it pointing to discarded sections with R_*_NONE, which 341 // hopefully creates a frame that is ignored at runtime. 342 SectionBase *Section = cast<DefinedRegular>(Body).Section; 343 if (Section == &InputSection::Discarded) { 344 P->setSymbolAndType(0, 0, false); 345 continue; 346 } 347 348 if (Config->IsRela) { 349 P->r_addend += Body.getVA() - Section->getOutputSection()->Addr; 350 } else if (Config->Relocatable) { 351 const uint8_t *BufLoc = RelocatedSection->Data.begin() + Rel.r_offset; 352 RelocatedSection->Relocations.push_back( 353 {R_ABS, Type, Rel.r_offset, Target->getImplicitAddend(BufLoc, Type), 354 &Body}); 355 } 356 } 357 358 } 359 } 360 361 static uint32_t getARMUndefinedRelativeWeakVA(uint32_t Type, uint32_t A, 362 uint32_t P) { 363 switch (Type) { 364 case R_ARM_THM_JUMP11: 365 return P + 2; 366 case R_ARM_CALL: 367 case R_ARM_JUMP24: 368 case R_ARM_PC24: 369 case R_ARM_PLT32: 370 case R_ARM_PREL31: 371 case R_ARM_THM_JUMP19: 372 case R_ARM_THM_JUMP24: 373 return P + 4; 374 case R_ARM_THM_CALL: 375 // We don't want an interworking BLX to ARM 376 return P + 5; 377 default: 378 return A; 379 } 380 } 381 382 static uint64_t getAArch64UndefinedRelativeWeakVA(uint64_t Type, uint64_t A, 383 uint64_t P) { 384 switch (Type) { 385 case R_AARCH64_CALL26: 386 case R_AARCH64_CONDBR19: 387 case R_AARCH64_JUMP26: 388 case R_AARCH64_TSTBR14: 389 return P + 4; 390 default: 391 return A; 392 } 393 } 394 395 template <class ELFT> 396 static typename ELFT::uint 397 getRelocTargetVA(uint32_t Type, int64_t A, typename ELFT::uint P, 398 const SymbolBody &Body, RelExpr Expr) { 399 switch (Expr) { 400 case R_ABS: 401 case R_RELAX_GOT_PC_NOPIC: 402 return Body.getVA(A); 403 case R_GOT: 404 case R_RELAX_TLS_GD_TO_IE_ABS: 405 return Body.getGotVA<ELFT>() + A; 406 case R_GOTONLY_PC: 407 return In<ELFT>::Got->getVA() + A - P; 408 case R_GOTONLY_PC_FROM_END: 409 return In<ELFT>::Got->getVA() + A - P + In<ELFT>::Got->getSize(); 410 case R_GOTREL: 411 return Body.getVA(A) - In<ELFT>::Got->getVA(); 412 case R_GOTREL_FROM_END: 413 return Body.getVA(A) - In<ELFT>::Got->getVA() - In<ELFT>::Got->getSize(); 414 case R_GOT_FROM_END: 415 case R_RELAX_TLS_GD_TO_IE_END: 416 return Body.getGotOffset() + A - In<ELFT>::Got->getSize(); 417 case R_GOT_OFF: 418 return Body.getGotOffset() + A; 419 case R_GOT_PAGE_PC: 420 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 421 return getAArch64Page(Body.getGotVA<ELFT>() + A) - getAArch64Page(P); 422 case R_GOT_PC: 423 case R_RELAX_TLS_GD_TO_IE: 424 return Body.getGotVA<ELFT>() + A - P; 425 case R_HINT: 426 case R_NONE: 427 case R_TLSDESC_CALL: 428 llvm_unreachable("cannot relocate hint relocs"); 429 case R_MIPS_GOTREL: 430 return Body.getVA(A) - In<ELFT>::MipsGot->getGp(); 431 case R_MIPS_GOT_GP: 432 return In<ELFT>::MipsGot->getGp() + A; 433 case R_MIPS_GOT_GP_PC: { 434 // R_MIPS_LO16 expression has R_MIPS_GOT_GP_PC type iif the target 435 // is _gp_disp symbol. In that case we should use the following 436 // formula for calculation "AHL + GP - P + 4". For details see p. 4-19 at 437 // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf 438 uint64_t V = In<ELFT>::MipsGot->getGp() + A - P; 439 if (Type == R_MIPS_LO16) 440 V += 4; 441 return V; 442 } 443 case R_MIPS_GOT_LOCAL_PAGE: 444 // If relocation against MIPS local symbol requires GOT entry, this entry 445 // should be initialized by 'page address'. This address is high 16-bits 446 // of sum the symbol's value and the addend. 447 return In<ELFT>::MipsGot->getVA() + 448 In<ELFT>::MipsGot->getPageEntryOffset(Body, A) - 449 In<ELFT>::MipsGot->getGp(); 450 case R_MIPS_GOT_OFF: 451 case R_MIPS_GOT_OFF32: 452 // In case of MIPS if a GOT relocation has non-zero addend this addend 453 // should be applied to the GOT entry content not to the GOT entry offset. 454 // That is why we use separate expression type. 455 return In<ELFT>::MipsGot->getVA() + 456 In<ELFT>::MipsGot->getBodyEntryOffset(Body, A) - 457 In<ELFT>::MipsGot->getGp(); 458 case R_MIPS_TLSGD: 459 return In<ELFT>::MipsGot->getVA() + In<ELFT>::MipsGot->getTlsOffset() + 460 In<ELFT>::MipsGot->getGlobalDynOffset(Body) - 461 In<ELFT>::MipsGot->getGp(); 462 case R_MIPS_TLSLD: 463 return In<ELFT>::MipsGot->getVA() + In<ELFT>::MipsGot->getTlsOffset() + 464 In<ELFT>::MipsGot->getTlsIndexOff() - In<ELFT>::MipsGot->getGp(); 465 case R_PAGE_PC: 466 case R_PLT_PAGE_PC: 467 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) 468 return getAArch64Page(A); 469 return getAArch64Page(Body.getVA(A)) - getAArch64Page(P); 470 case R_PC: 471 if (Body.isUndefined() && !Body.isLocal() && Body.symbol()->isWeak()) { 472 // On ARM and AArch64 a branch to an undefined weak resolves to the 473 // next instruction, otherwise the place. 474 if (Config->EMachine == EM_ARM) 475 return getARMUndefinedRelativeWeakVA(Type, A, P); 476 if (Config->EMachine == EM_AARCH64) 477 return getAArch64UndefinedRelativeWeakVA(Type, A, P); 478 } 479 return Body.getVA(A) - P; 480 case R_PLT: 481 return Body.getPltVA() + A; 482 case R_PLT_PC: 483 case R_PPC_PLT_OPD: 484 return Body.getPltVA() + A - P; 485 case R_PPC_OPD: { 486 uint64_t SymVA = Body.getVA(A); 487 // If we have an undefined weak symbol, we might get here with a symbol 488 // address of zero. That could overflow, but the code must be unreachable, 489 // so don't bother doing anything at all. 490 if (!SymVA) 491 return 0; 492 if (Out::Opd) { 493 // If this is a local call, and we currently have the address of a 494 // function-descriptor, get the underlying code address instead. 495 uint64_t OpdStart = Out::Opd->Addr; 496 uint64_t OpdEnd = OpdStart + Out::Opd->Size; 497 bool InOpd = OpdStart <= SymVA && SymVA < OpdEnd; 498 if (InOpd) 499 SymVA = read64be(&Out::OpdBuf[SymVA - OpdStart]); 500 } 501 return SymVA - P; 502 } 503 case R_PPC_TOC: 504 return getPPC64TocBase() + A; 505 case R_RELAX_GOT_PC: 506 return Body.getVA(A) - P; 507 case R_RELAX_TLS_GD_TO_LE: 508 case R_RELAX_TLS_IE_TO_LE: 509 case R_RELAX_TLS_LD_TO_LE: 510 case R_TLS: 511 // A weak undefined TLS symbol resolves to the base of the TLS 512 // block, i.e. gets a value of zero. If we pass --gc-sections to 513 // lld and .tbss is not referenced, it gets reclaimed and we don't 514 // create a TLS program header. Therefore, we resolve this 515 // statically to zero. 516 if (Body.isTls() && (Body.isLazy() || Body.isUndefined()) && 517 Body.symbol()->isWeak()) 518 return 0; 519 if (Target->TcbSize) 520 return Body.getVA(A) + alignTo(Target->TcbSize, Out::TlsPhdr->p_align); 521 return Body.getVA(A) - Out::TlsPhdr->p_memsz; 522 case R_RELAX_TLS_GD_TO_LE_NEG: 523 case R_NEG_TLS: 524 return Out::TlsPhdr->p_memsz - Body.getVA(A); 525 case R_SIZE: 526 return Body.getSize<ELFT>() + A; 527 case R_TLSDESC: 528 return In<ELFT>::Got->getGlobalDynAddr(Body) + A; 529 case R_TLSDESC_PAGE: 530 return getAArch64Page(In<ELFT>::Got->getGlobalDynAddr(Body) + A) - 531 getAArch64Page(P); 532 case R_TLSGD: 533 return In<ELFT>::Got->getGlobalDynOffset(Body) + A - 534 In<ELFT>::Got->getSize(); 535 case R_TLSGD_PC: 536 return In<ELFT>::Got->getGlobalDynAddr(Body) + A - P; 537 case R_TLSLD: 538 return In<ELFT>::Got->getTlsIndexOff() + A - In<ELFT>::Got->getSize(); 539 case R_TLSLD_PC: 540 return In<ELFT>::Got->getTlsIndexVA() + A - P; 541 } 542 llvm_unreachable("Invalid expression"); 543 } 544 545 // This function applies relocations to sections without SHF_ALLOC bit. 546 // Such sections are never mapped to memory at runtime. Debug sections are 547 // an example. Relocations in non-alloc sections are much easier to 548 // handle than in allocated sections because it will never need complex 549 // treatement such as GOT or PLT (because at runtime no one refers them). 550 // So, we handle relocations for non-alloc sections directly in this 551 // function as a performance optimization. 552 template <class ELFT, class RelTy> 553 void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef<RelTy> Rels) { 554 for (const RelTy &Rel : Rels) { 555 uint32_t Type = Rel.getType(Config->IsMips64EL); 556 uint64_t Offset = getOffset(Rel.r_offset); 557 uint8_t *BufLoc = Buf + Offset; 558 int64_t Addend = getAddend<ELFT>(Rel); 559 if (!RelTy::IsRela) 560 Addend += Target->getImplicitAddend(BufLoc, Type); 561 562 SymbolBody &Sym = this->getFile<ELFT>()->getRelocTargetSym(Rel); 563 RelExpr Expr = Target->getRelExpr(Type, Sym, BufLoc); 564 if (Expr == R_NONE) 565 continue; 566 if (Expr != R_ABS) { 567 error(this->getLocation<ELFT>(Offset) + ": has non-ABS reloc"); 568 return; 569 } 570 571 uint64_t AddrLoc = this->OutSec->Addr + Offset; 572 uint64_t SymVA = 0; 573 if (!Sym.isTls() || Out::TlsPhdr) 574 SymVA = SignExtend64<sizeof(typename ELFT::uint) * 8>( 575 getRelocTargetVA<ELFT>(Type, Addend, AddrLoc, Sym, R_ABS)); 576 Target->relocateOne(BufLoc, Type, SymVA); 577 } 578 } 579 580 template <class ELFT> elf::ObjectFile<ELFT> *InputSectionBase::getFile() const { 581 return cast_or_null<elf::ObjectFile<ELFT>>(File); 582 } 583 584 template <class ELFT> 585 void InputSectionBase::relocate(uint8_t *Buf, uint8_t *BufEnd) { 586 // scanReloc function in Writer.cpp constructs Relocations 587 // vector only for SHF_ALLOC'ed sections. For other sections, 588 // we handle relocations directly here. 589 auto *IS = dyn_cast<InputSection>(this); 590 if (IS && !(IS->Flags & SHF_ALLOC)) { 591 if (IS->AreRelocsRela) 592 IS->relocateNonAlloc<ELFT>(Buf, IS->template relas<ELFT>()); 593 else 594 IS->relocateNonAlloc<ELFT>(Buf, IS->template rels<ELFT>()); 595 return; 596 } 597 598 const unsigned Bits = sizeof(typename ELFT::uint) * 8; 599 for (const Relocation &Rel : Relocations) { 600 uint64_t Offset = getOffset(Rel.Offset); 601 uint8_t *BufLoc = Buf + Offset; 602 uint32_t Type = Rel.Type; 603 604 uint64_t AddrLoc = getOutputSection()->Addr + Offset; 605 RelExpr Expr = Rel.Expr; 606 uint64_t TargetVA = SignExtend64<Bits>( 607 getRelocTargetVA<ELFT>(Type, Rel.Addend, AddrLoc, *Rel.Sym, Expr)); 608 609 switch (Expr) { 610 case R_RELAX_GOT_PC: 611 case R_RELAX_GOT_PC_NOPIC: 612 Target->relaxGot(BufLoc, TargetVA); 613 break; 614 case R_RELAX_TLS_IE_TO_LE: 615 Target->relaxTlsIeToLe(BufLoc, Type, TargetVA); 616 break; 617 case R_RELAX_TLS_LD_TO_LE: 618 Target->relaxTlsLdToLe(BufLoc, Type, TargetVA); 619 break; 620 case R_RELAX_TLS_GD_TO_LE: 621 case R_RELAX_TLS_GD_TO_LE_NEG: 622 Target->relaxTlsGdToLe(BufLoc, Type, TargetVA); 623 break; 624 case R_RELAX_TLS_GD_TO_IE: 625 case R_RELAX_TLS_GD_TO_IE_ABS: 626 case R_RELAX_TLS_GD_TO_IE_PAGE_PC: 627 case R_RELAX_TLS_GD_TO_IE_END: 628 Target->relaxTlsGdToIe(BufLoc, Type, TargetVA); 629 break; 630 case R_PPC_PLT_OPD: 631 // Patch a nop (0x60000000) to a ld. 632 if (BufLoc + 8 <= BufEnd && read32be(BufLoc + 4) == 0x60000000) 633 write32be(BufLoc + 4, 0xe8410028); // ld %r2, 40(%r1) 634 // fallthrough 635 default: 636 Target->relocateOne(BufLoc, Type, TargetVA); 637 break; 638 } 639 } 640 } 641 642 template <class ELFT> void InputSection::writeTo(uint8_t *Buf) { 643 if (this->Type == SHT_NOBITS) 644 return; 645 646 if (auto *S = dyn_cast<SyntheticSection>(this)) { 647 S->writeTo(Buf + OutSecOff); 648 return; 649 } 650 651 // If -r or --emit-relocs is given, then an InputSection 652 // may be a relocation section. 653 if (this->Type == SHT_RELA) { 654 copyRelocations<ELFT>(Buf + OutSecOff, 655 this->template getDataAs<typename ELFT::Rela>()); 656 return; 657 } 658 if (this->Type == SHT_REL) { 659 copyRelocations<ELFT>(Buf + OutSecOff, 660 this->template getDataAs<typename ELFT::Rel>()); 661 return; 662 } 663 664 // Copy section contents from source object file to output file 665 // and then apply relocations. 666 memcpy(Buf + OutSecOff, Data.data(), Data.size()); 667 uint8_t *BufEnd = Buf + OutSecOff + Data.size(); 668 this->relocate<ELFT>(Buf, BufEnd); 669 } 670 671 void InputSection::replace(InputSection *Other) { 672 this->Alignment = std::max(this->Alignment, Other->Alignment); 673 Other->Repl = this->Repl; 674 Other->Live = false; 675 } 676 677 template <class ELFT> 678 EhInputSection::EhInputSection(elf::ObjectFile<ELFT> *F, 679 const typename ELFT::Shdr *Header, 680 StringRef Name) 681 : InputSectionBase(F, Header, Name, InputSectionBase::EHFrame) { 682 // Mark .eh_frame sections as live by default because there are 683 // usually no relocations that point to .eh_frames. Otherwise, 684 // the garbage collector would drop all .eh_frame sections. 685 this->Live = true; 686 } 687 688 bool EhInputSection::classof(const SectionBase *S) { 689 return S->kind() == InputSectionBase::EHFrame; 690 } 691 692 // Returns the index of the first relocation that points to a region between 693 // Begin and Begin+Size. 694 template <class IntTy, class RelTy> 695 static unsigned getReloc(IntTy Begin, IntTy Size, const ArrayRef<RelTy> &Rels, 696 unsigned &RelocI) { 697 // Start search from RelocI for fast access. That works because the 698 // relocations are sorted in .eh_frame. 699 for (unsigned N = Rels.size(); RelocI < N; ++RelocI) { 700 const RelTy &Rel = Rels[RelocI]; 701 if (Rel.r_offset < Begin) 702 continue; 703 704 if (Rel.r_offset < Begin + Size) 705 return RelocI; 706 return -1; 707 } 708 return -1; 709 } 710 711 // .eh_frame is a sequence of CIE or FDE records. 712 // This function splits an input section into records and returns them. 713 template <class ELFT> void EhInputSection::split() { 714 // Early exit if already split. 715 if (!this->Pieces.empty()) 716 return; 717 718 if (this->NumRelocations) { 719 if (this->AreRelocsRela) 720 split<ELFT>(this->relas<ELFT>()); 721 else 722 split<ELFT>(this->rels<ELFT>()); 723 return; 724 } 725 split<ELFT>(makeArrayRef<typename ELFT::Rela>(nullptr, nullptr)); 726 } 727 728 template <class ELFT, class RelTy> 729 void EhInputSection::split(ArrayRef<RelTy> Rels) { 730 ArrayRef<uint8_t> Data = this->Data; 731 unsigned RelI = 0; 732 for (size_t Off = 0, End = Data.size(); Off != End;) { 733 size_t Size = readEhRecordSize<ELFT>(this, Off); 734 this->Pieces.emplace_back(Off, this, Size, getReloc(Off, Size, Rels, RelI)); 735 // The empty record is the end marker. 736 if (Size == 4) 737 break; 738 Off += Size; 739 } 740 } 741 742 static size_t findNull(ArrayRef<uint8_t> A, size_t EntSize) { 743 // Optimize the common case. 744 StringRef S((const char *)A.data(), A.size()); 745 if (EntSize == 1) 746 return S.find(0); 747 748 for (unsigned I = 0, N = S.size(); I != N; I += EntSize) { 749 const char *B = S.begin() + I; 750 if (std::all_of(B, B + EntSize, [](char C) { return C == 0; })) 751 return I; 752 } 753 return StringRef::npos; 754 } 755 756 // Split SHF_STRINGS section. Such section is a sequence of 757 // null-terminated strings. 758 void MergeInputSection::splitStrings(ArrayRef<uint8_t> Data, size_t EntSize) { 759 size_t Off = 0; 760 bool IsAlloc = this->Flags & SHF_ALLOC; 761 while (!Data.empty()) { 762 size_t End = findNull(Data, EntSize); 763 if (End == StringRef::npos) 764 fatal(toString(this) + ": string is not null terminated"); 765 size_t Size = End + EntSize; 766 Pieces.emplace_back(Off, !IsAlloc); 767 Hashes.push_back(hash_value(toStringRef(Data.slice(0, Size)))); 768 Data = Data.slice(Size); 769 Off += Size; 770 } 771 } 772 773 // Split non-SHF_STRINGS section. Such section is a sequence of 774 // fixed size records. 775 void MergeInputSection::splitNonStrings(ArrayRef<uint8_t> Data, 776 size_t EntSize) { 777 size_t Size = Data.size(); 778 assert((Size % EntSize) == 0); 779 bool IsAlloc = this->Flags & SHF_ALLOC; 780 for (unsigned I = 0, N = Size; I != N; I += EntSize) { 781 Hashes.push_back(hash_value(toStringRef(Data.slice(I, EntSize)))); 782 Pieces.emplace_back(I, !IsAlloc); 783 } 784 } 785 786 template <class ELFT> 787 MergeInputSection::MergeInputSection(elf::ObjectFile<ELFT> *F, 788 const typename ELFT::Shdr *Header, 789 StringRef Name) 790 : InputSectionBase(F, Header, Name, InputSectionBase::Merge) {} 791 792 // This function is called after we obtain a complete list of input sections 793 // that need to be linked. This is responsible to split section contents 794 // into small chunks for further processing. 795 // 796 // Note that this function is called from parallel_for_each. This must be 797 // thread-safe (i.e. no memory allocation from the pools). 798 void MergeInputSection::splitIntoPieces() { 799 ArrayRef<uint8_t> Data = this->Data; 800 uint64_t EntSize = this->Entsize; 801 if (this->Flags & SHF_STRINGS) 802 splitStrings(Data, EntSize); 803 else 804 splitNonStrings(Data, EntSize); 805 806 if (Config->GcSections && (this->Flags & SHF_ALLOC)) 807 for (uint64_t Off : LiveOffsets) 808 this->getSectionPiece(Off)->Live = true; 809 } 810 811 bool MergeInputSection::classof(const SectionBase *S) { 812 return S->kind() == InputSectionBase::Merge; 813 } 814 815 // Do binary search to get a section piece at a given input offset. 816 SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) { 817 auto *This = static_cast<const MergeInputSection *>(this); 818 return const_cast<SectionPiece *>(This->getSectionPiece(Offset)); 819 } 820 821 template <class It, class T, class Compare> 822 static It fastUpperBound(It First, It Last, const T &Value, Compare Comp) { 823 size_t Size = std::distance(First, Last); 824 assert(Size != 0); 825 while (Size != 1) { 826 size_t H = Size / 2; 827 const It MI = First + H; 828 Size -= H; 829 First = Comp(Value, *MI) ? First : First + H; 830 } 831 return Comp(Value, *First) ? First : First + 1; 832 } 833 834 const SectionPiece *MergeInputSection::getSectionPiece(uint64_t Offset) const { 835 uint64_t Size = this->Data.size(); 836 if (Offset >= Size) 837 fatal(toString(this) + ": entry is past the end of the section"); 838 839 // Find the element this offset points to. 840 auto I = fastUpperBound( 841 Pieces.begin(), Pieces.end(), Offset, 842 [](const uint64_t &A, const SectionPiece &B) { return A < B.InputOff; }); 843 --I; 844 return &*I; 845 } 846 847 // Returns the offset in an output section for a given input offset. 848 // Because contents of a mergeable section is not contiguous in output, 849 // it is not just an addition to a base output offset. 850 uint64_t MergeInputSection::getOffset(uint64_t Offset) const { 851 // Initialize OffsetMap lazily. 852 std::call_once(InitOffsetMap, [&] { 853 OffsetMap.reserve(Pieces.size()); 854 for (const SectionPiece &Piece : Pieces) 855 OffsetMap[Piece.InputOff] = Piece.OutputOff; 856 }); 857 858 // Find a string starting at a given offset. 859 auto It = OffsetMap.find(Offset); 860 if (It != OffsetMap.end()) 861 return It->second; 862 863 if (!this->Live) 864 return 0; 865 866 // If Offset is not at beginning of a section piece, it is not in the map. 867 // In that case we need to search from the original section piece vector. 868 const SectionPiece &Piece = *this->getSectionPiece(Offset); 869 if (!Piece.Live) 870 return 0; 871 872 uint64_t Addend = Offset - Piece.InputOff; 873 return Piece.OutputOff + Addend; 874 } 875 876 template InputSection::InputSection(elf::ObjectFile<ELF32LE> *, 877 const ELF32LE::Shdr *, StringRef); 878 template InputSection::InputSection(elf::ObjectFile<ELF32BE> *, 879 const ELF32BE::Shdr *, StringRef); 880 template InputSection::InputSection(elf::ObjectFile<ELF64LE> *, 881 const ELF64LE::Shdr *, StringRef); 882 template InputSection::InputSection(elf::ObjectFile<ELF64BE> *, 883 const ELF64BE::Shdr *, StringRef); 884 885 template std::string InputSectionBase::getLocation<ELF32LE>(uint64_t); 886 template std::string InputSectionBase::getLocation<ELF32BE>(uint64_t); 887 template std::string InputSectionBase::getLocation<ELF64LE>(uint64_t); 888 template std::string InputSectionBase::getLocation<ELF64BE>(uint64_t); 889 890 template std::string InputSectionBase::getSrcMsg<ELF32LE>(uint64_t); 891 template std::string InputSectionBase::getSrcMsg<ELF32BE>(uint64_t); 892 template std::string InputSectionBase::getSrcMsg<ELF64LE>(uint64_t); 893 template std::string InputSectionBase::getSrcMsg<ELF64BE>(uint64_t); 894 895 template std::string InputSectionBase::getObjMsg<ELF32LE>(uint64_t); 896 template std::string InputSectionBase::getObjMsg<ELF32BE>(uint64_t); 897 template std::string InputSectionBase::getObjMsg<ELF64LE>(uint64_t); 898 template std::string InputSectionBase::getObjMsg<ELF64BE>(uint64_t); 899 900 template void InputSection::writeTo<ELF32LE>(uint8_t *); 901 template void InputSection::writeTo<ELF32BE>(uint8_t *); 902 template void InputSection::writeTo<ELF64LE>(uint8_t *); 903 template void InputSection::writeTo<ELF64BE>(uint8_t *); 904 905 template elf::ObjectFile<ELF32LE> *InputSectionBase::getFile<ELF32LE>() const; 906 template elf::ObjectFile<ELF32BE> *InputSectionBase::getFile<ELF32BE>() const; 907 template elf::ObjectFile<ELF64LE> *InputSectionBase::getFile<ELF64LE>() const; 908 template elf::ObjectFile<ELF64BE> *InputSectionBase::getFile<ELF64BE>() const; 909 910 template MergeInputSection::MergeInputSection(elf::ObjectFile<ELF32LE> *, 911 const ELF32LE::Shdr *, StringRef); 912 template MergeInputSection::MergeInputSection(elf::ObjectFile<ELF32BE> *, 913 const ELF32BE::Shdr *, StringRef); 914 template MergeInputSection::MergeInputSection(elf::ObjectFile<ELF64LE> *, 915 const ELF64LE::Shdr *, StringRef); 916 template MergeInputSection::MergeInputSection(elf::ObjectFile<ELF64BE> *, 917 const ELF64BE::Shdr *, StringRef); 918 919 template EhInputSection::EhInputSection(elf::ObjectFile<ELF32LE> *, 920 const ELF32LE::Shdr *, StringRef); 921 template EhInputSection::EhInputSection(elf::ObjectFile<ELF32BE> *, 922 const ELF32BE::Shdr *, StringRef); 923 template EhInputSection::EhInputSection(elf::ObjectFile<ELF64LE> *, 924 const ELF64LE::Shdr *, StringRef); 925 template EhInputSection::EhInputSection(elf::ObjectFile<ELF64BE> *, 926 const ELF64BE::Shdr *, StringRef); 927 928 template void EhInputSection::split<ELF32LE>(); 929 template void EhInputSection::split<ELF32BE>(); 930 template void EhInputSection::split<ELF64LE>(); 931 template void EhInputSection::split<ELF64BE>(); 932