1 //===- InputFiles.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 "InputFiles.h" 10 #include "Driver.h" 11 #include "InputSection.h" 12 #include "LinkerScript.h" 13 #include "SymbolTable.h" 14 #include "Symbols.h" 15 #include "SyntheticSections.h" 16 #include "lld/Common/ErrorHandler.h" 17 #include "lld/Common/Memory.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/CodeGen/Analysis.h" 20 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 21 #include "llvm/IR/LLVMContext.h" 22 #include "llvm/IR/Module.h" 23 #include "llvm/LTO/LTO.h" 24 #include "llvm/MC/StringTableBuilder.h" 25 #include "llvm/Object/ELFObjectFile.h" 26 #include "llvm/Support/ARMAttributeParser.h" 27 #include "llvm/Support/ARMBuildAttributes.h" 28 #include "llvm/Support/Endian.h" 29 #include "llvm/Support/Path.h" 30 #include "llvm/Support/TarWriter.h" 31 #include "llvm/Support/raw_ostream.h" 32 33 using namespace llvm; 34 using namespace llvm::ELF; 35 using namespace llvm::object; 36 using namespace llvm::sys; 37 using namespace llvm::sys::fs; 38 using namespace llvm::support::endian; 39 40 using namespace lld; 41 using namespace lld::elf; 42 43 bool InputFile::IsInGroup; 44 uint32_t InputFile::NextGroupId; 45 std::vector<BinaryFile *> elf::BinaryFiles; 46 std::vector<BitcodeFile *> elf::BitcodeFiles; 47 std::vector<LazyObjFile *> elf::LazyObjFiles; 48 std::vector<InputFile *> elf::ObjectFiles; 49 std::vector<SharedFile *> elf::SharedFiles; 50 51 std::unique_ptr<TarWriter> elf::Tar; 52 53 static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) { 54 unsigned char Size; 55 unsigned char Endian; 56 std::tie(Size, Endian) = getElfArchType(MB.getBuffer()); 57 58 auto Fatal = [&](StringRef Msg) { 59 StringRef Filename = MB.getBufferIdentifier(); 60 if (ArchiveName.empty()) 61 fatal(Filename + ": " + Msg); 62 else 63 fatal(ArchiveName + "(" + Filename + "): " + Msg); 64 }; 65 66 if (!MB.getBuffer().startswith(ElfMagic)) 67 Fatal("not an ELF file"); 68 if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB) 69 Fatal("corrupted ELF file: invalid data encoding"); 70 if (Size != ELFCLASS32 && Size != ELFCLASS64) 71 Fatal("corrupted ELF file: invalid file class"); 72 73 size_t BufSize = MB.getBuffer().size(); 74 if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) || 75 (Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr))) 76 Fatal("corrupted ELF file: file is too short"); 77 78 if (Size == ELFCLASS32) 79 return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 80 return (Endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 81 } 82 83 InputFile::InputFile(Kind K, MemoryBufferRef M) 84 : MB(M), GroupId(NextGroupId), FileKind(K) { 85 // All files within the same --{start,end}-group get the same group ID. 86 // Otherwise, a new file will get a new group ID. 87 if (!IsInGroup) 88 ++NextGroupId; 89 } 90 91 Optional<MemoryBufferRef> elf::readFile(StringRef Path) { 92 // The --chroot option changes our virtual root directory. 93 // This is useful when you are dealing with files created by --reproduce. 94 if (!Config->Chroot.empty() && Path.startswith("/")) 95 Path = Saver.save(Config->Chroot + Path); 96 97 log(Path); 98 99 auto MBOrErr = MemoryBuffer::getFile(Path, -1, false); 100 if (auto EC = MBOrErr.getError()) { 101 error("cannot open " + Path + ": " + EC.message()); 102 return None; 103 } 104 105 std::unique_ptr<MemoryBuffer> &MB = *MBOrErr; 106 MemoryBufferRef MBRef = MB->getMemBufferRef(); 107 make<std::unique_ptr<MemoryBuffer>>(std::move(MB)); // take MB ownership 108 109 if (Tar) 110 Tar->append(relativeToRoot(Path), MBRef.getBuffer()); 111 return MBRef; 112 } 113 114 // All input object files must be for the same architecture 115 // (e.g. it does not make sense to link x86 object files with 116 // MIPS object files.) This function checks for that error. 117 static bool isCompatible(InputFile *File) { 118 if (!File->isElf() && !isa<BitcodeFile>(File)) 119 return true; 120 121 if (File->EKind == Config->EKind && File->EMachine == Config->EMachine) { 122 if (Config->EMachine != EM_MIPS) 123 return true; 124 if (isMipsN32Abi(File) == Config->MipsN32Abi) 125 return true; 126 } 127 128 if (!Config->Emulation.empty()) { 129 error(toString(File) + " is incompatible with " + Config->Emulation); 130 } else { 131 InputFile *Existing; 132 if (!ObjectFiles.empty()) 133 Existing = ObjectFiles[0]; 134 else if (!SharedFiles.empty()) 135 Existing = SharedFiles[0]; 136 else 137 Existing = BitcodeFiles[0]; 138 139 error(toString(File) + " is incompatible with " + toString(Existing)); 140 } 141 142 return false; 143 } 144 145 template <class ELFT> static void doParseFile(InputFile *File) { 146 if (!isCompatible(File)) 147 return; 148 149 // Binary file 150 if (auto *F = dyn_cast<BinaryFile>(File)) { 151 BinaryFiles.push_back(F); 152 F->parse(); 153 return; 154 } 155 156 // .a file 157 if (auto *F = dyn_cast<ArchiveFile>(File)) { 158 F->parse(); 159 return; 160 } 161 162 // Lazy object file 163 if (auto *F = dyn_cast<LazyObjFile>(File)) { 164 LazyObjFiles.push_back(F); 165 F->parse<ELFT>(); 166 return; 167 } 168 169 if (Config->Trace) 170 message(toString(File)); 171 172 // .so file 173 if (auto *F = dyn_cast<SharedFile>(File)) { 174 F->parse<ELFT>(); 175 return; 176 } 177 178 // LLVM bitcode file 179 if (auto *F = dyn_cast<BitcodeFile>(File)) { 180 BitcodeFiles.push_back(F); 181 F->parse<ELFT>(); 182 return; 183 } 184 185 // Regular object file 186 ObjectFiles.push_back(File); 187 cast<ObjFile<ELFT>>(File)->parse(); 188 } 189 190 // Add symbols in File to the symbol table. 191 void elf::parseFile(InputFile *File) { 192 switch (Config->EKind) { 193 case ELF32LEKind: 194 doParseFile<ELF32LE>(File); 195 return; 196 case ELF32BEKind: 197 doParseFile<ELF32BE>(File); 198 return; 199 case ELF64LEKind: 200 doParseFile<ELF64LE>(File); 201 return; 202 case ELF64BEKind: 203 doParseFile<ELF64BE>(File); 204 return; 205 default: 206 llvm_unreachable("unknown ELFT"); 207 } 208 } 209 210 // Concatenates arguments to construct a string representing an error location. 211 static std::string createFileLineMsg(StringRef Path, unsigned Line) { 212 std::string Filename = path::filename(Path); 213 std::string Lineno = ":" + std::to_string(Line); 214 if (Filename == Path) 215 return Filename + Lineno; 216 return Filename + Lineno + " (" + Path.str() + Lineno + ")"; 217 } 218 219 template <class ELFT> 220 static std::string getSrcMsgAux(ObjFile<ELFT> &File, const Symbol &Sym, 221 InputSectionBase &Sec, uint64_t Offset) { 222 // In DWARF, functions and variables are stored to different places. 223 // First, lookup a function for a given offset. 224 if (Optional<DILineInfo> Info = File.getDILineInfo(&Sec, Offset)) 225 return createFileLineMsg(Info->FileName, Info->Line); 226 227 // If it failed, lookup again as a variable. 228 if (Optional<std::pair<std::string, unsigned>> FileLine = 229 File.getVariableLoc(Sym.getName())) 230 return createFileLineMsg(FileLine->first, FileLine->second); 231 232 // File.SourceFile contains STT_FILE symbol, and that is a last resort. 233 return File.SourceFile; 234 } 235 236 std::string InputFile::getSrcMsg(const Symbol &Sym, InputSectionBase &Sec, 237 uint64_t Offset) { 238 if (kind() != ObjKind) 239 return ""; 240 switch (Config->EKind) { 241 default: 242 llvm_unreachable("Invalid kind"); 243 case ELF32LEKind: 244 return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), Sym, Sec, Offset); 245 case ELF32BEKind: 246 return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), Sym, Sec, Offset); 247 case ELF64LEKind: 248 return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), Sym, Sec, Offset); 249 case ELF64BEKind: 250 return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), Sym, Sec, Offset); 251 } 252 } 253 254 template <class ELFT> void ObjFile<ELFT>::initializeDwarf() { 255 Dwarf = llvm::make_unique<DWARFContext>(make_unique<LLDDwarfObj<ELFT>>(this)); 256 for (std::unique_ptr<DWARFUnit> &CU : Dwarf->compile_units()) { 257 auto Report = [](Error Err) { 258 handleAllErrors(std::move(Err), 259 [](ErrorInfoBase &Info) { warn(Info.message()); }); 260 }; 261 Expected<const DWARFDebugLine::LineTable *> ExpectedLT = 262 Dwarf->getLineTableForUnit(CU.get(), Report); 263 const DWARFDebugLine::LineTable *LT = nullptr; 264 if (ExpectedLT) 265 LT = *ExpectedLT; 266 else 267 Report(ExpectedLT.takeError()); 268 if (!LT) 269 continue; 270 LineTables.push_back(LT); 271 272 // Loop over variable records and insert them to VariableLoc. 273 for (const auto &Entry : CU->dies()) { 274 DWARFDie Die(CU.get(), &Entry); 275 // Skip all tags that are not variables. 276 if (Die.getTag() != dwarf::DW_TAG_variable) 277 continue; 278 279 // Skip if a local variable because we don't need them for generating 280 // error messages. In general, only non-local symbols can fail to be 281 // linked. 282 if (!dwarf::toUnsigned(Die.find(dwarf::DW_AT_external), 0)) 283 continue; 284 285 // Get the source filename index for the variable. 286 unsigned File = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_file), 0); 287 if (!LT->hasFileAtIndex(File)) 288 continue; 289 290 // Get the line number on which the variable is declared. 291 unsigned Line = dwarf::toUnsigned(Die.find(dwarf::DW_AT_decl_line), 0); 292 293 // Here we want to take the variable name to add it into VariableLoc. 294 // Variable can have regular and linkage name associated. At first, we try 295 // to get linkage name as it can be different, for example when we have 296 // two variables in different namespaces of the same object. Use common 297 // name otherwise, but handle the case when it also absent in case if the 298 // input object file lacks some debug info. 299 StringRef Name = 300 dwarf::toString(Die.find(dwarf::DW_AT_linkage_name), 301 dwarf::toString(Die.find(dwarf::DW_AT_name), "")); 302 if (!Name.empty()) 303 VariableLoc.insert({Name, {LT, File, Line}}); 304 } 305 } 306 } 307 308 // Returns the pair of file name and line number describing location of data 309 // object (variable, array, etc) definition. 310 template <class ELFT> 311 Optional<std::pair<std::string, unsigned>> 312 ObjFile<ELFT>::getVariableLoc(StringRef Name) { 313 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); }); 314 315 // Return if we have no debug information about data object. 316 auto It = VariableLoc.find(Name); 317 if (It == VariableLoc.end()) 318 return None; 319 320 // Take file name string from line table. 321 std::string FileName; 322 if (!It->second.LT->getFileNameByIndex( 323 It->second.File, nullptr, 324 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, FileName)) 325 return None; 326 327 return std::make_pair(FileName, It->second.Line); 328 } 329 330 // Returns source line information for a given offset 331 // using DWARF debug info. 332 template <class ELFT> 333 Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *S, 334 uint64_t Offset) { 335 llvm::call_once(InitDwarfLine, [this]() { initializeDwarf(); }); 336 337 // Detect SectionIndex for specified section. 338 uint64_t SectionIndex = object::SectionedAddress::UndefSection; 339 ArrayRef<InputSectionBase *> Sections = S->File->getSections(); 340 for (uint64_t CurIndex = 0; CurIndex < Sections.size(); ++CurIndex) { 341 if (S == Sections[CurIndex]) { 342 SectionIndex = CurIndex; 343 break; 344 } 345 } 346 347 // Use fake address calcuated by adding section file offset and offset in 348 // section. See comments for ObjectInfo class. 349 DILineInfo Info; 350 for (const llvm::DWARFDebugLine::LineTable *LT : LineTables) { 351 if (LT->getFileLineInfoForAddress( 352 {S->getOffsetInFile() + Offset, SectionIndex}, nullptr, 353 DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, Info)) 354 return Info; 355 } 356 return None; 357 } 358 359 // Returns "<internal>", "foo.a(bar.o)" or "baz.o". 360 std::string lld::toString(const InputFile *F) { 361 if (!F) 362 return "<internal>"; 363 364 if (F->ToStringCache.empty()) { 365 if (F->ArchiveName.empty()) 366 F->ToStringCache = F->getName(); 367 else 368 F->ToStringCache = (F->ArchiveName + "(" + F->getName() + ")").str(); 369 } 370 return F->ToStringCache; 371 } 372 373 ELFFileBase::ELFFileBase(Kind K, MemoryBufferRef MB) : InputFile(K, MB) { 374 EKind = getELFKind(MB, ""); 375 376 switch (EKind) { 377 case ELF32LEKind: 378 init<ELF32LE>(); 379 break; 380 case ELF32BEKind: 381 init<ELF32BE>(); 382 break; 383 case ELF64LEKind: 384 init<ELF64LE>(); 385 break; 386 case ELF64BEKind: 387 init<ELF64BE>(); 388 break; 389 default: 390 llvm_unreachable("getELFKind"); 391 } 392 } 393 394 template <typename Elf_Shdr> 395 static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> Sections, uint32_t Type) { 396 for (const Elf_Shdr &Sec : Sections) 397 if (Sec.sh_type == Type) 398 return &Sec; 399 return nullptr; 400 } 401 402 template <class ELFT> void ELFFileBase::init() { 403 using Elf_Shdr = typename ELFT::Shdr; 404 using Elf_Sym = typename ELFT::Sym; 405 406 // Initialize trivial attributes. 407 const ELFFile<ELFT> &Obj = getObj<ELFT>(); 408 EMachine = Obj.getHeader()->e_machine; 409 OSABI = Obj.getHeader()->e_ident[llvm::ELF::EI_OSABI]; 410 ABIVersion = Obj.getHeader()->e_ident[llvm::ELF::EI_ABIVERSION]; 411 412 ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this); 413 414 // Find a symbol table. 415 bool IsDSO = 416 (identify_magic(MB.getBuffer()) == file_magic::elf_shared_object); 417 const Elf_Shdr *SymtabSec = 418 findSection(Sections, IsDSO ? SHT_DYNSYM : SHT_SYMTAB); 419 420 if (!SymtabSec) 421 return; 422 423 // Initialize members corresponding to a symbol table. 424 FirstGlobal = SymtabSec->sh_info; 425 426 ArrayRef<Elf_Sym> ESyms = CHECK(Obj.symbols(SymtabSec), this); 427 if (FirstGlobal == 0 || FirstGlobal > ESyms.size()) 428 fatal(toString(this) + ": invalid sh_info in symbol table"); 429 430 ELFSyms = reinterpret_cast<const void *>(ESyms.data()); 431 NumELFSyms = ESyms.size(); 432 StringTable = CHECK(Obj.getStringTableForSymtab(*SymtabSec, Sections), this); 433 } 434 435 template <class ELFT> 436 uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &Sym) const { 437 return CHECK( 438 this->getObj().getSectionIndex(&Sym, getELFSyms<ELFT>(), ShndxTable), 439 this); 440 } 441 442 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() { 443 if (this->Symbols.empty()) 444 return {}; 445 return makeArrayRef(this->Symbols).slice(1, this->FirstGlobal - 1); 446 } 447 448 template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() { 449 return makeArrayRef(this->Symbols).slice(this->FirstGlobal); 450 } 451 452 template <class ELFT> void ObjFile<ELFT>::parse(bool IgnoreComdats) { 453 // Read a section table. JustSymbols is usually false. 454 if (this->JustSymbols) 455 initializeJustSymbols(); 456 else 457 initializeSections(IgnoreComdats); 458 459 // Read a symbol table. 460 initializeSymbols(); 461 } 462 463 // Sections with SHT_GROUP and comdat bits define comdat section groups. 464 // They are identified and deduplicated by group name. This function 465 // returns a group name. 466 template <class ELFT> 467 StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> Sections, 468 const Elf_Shdr &Sec) { 469 const Elf_Sym *Sym = 470 CHECK(object::getSymbol<ELFT>(this->getELFSyms<ELFT>(), Sec.sh_info), this); 471 StringRef Signature = CHECK(Sym->getName(this->StringTable), this); 472 473 // As a special case, if a symbol is a section symbol and has no name, 474 // we use a section name as a signature. 475 // 476 // Such SHT_GROUP sections are invalid from the perspective of the ELF 477 // standard, but GNU gold 1.14 (the newest version as of July 2017) or 478 // older produce such sections as outputs for the -r option, so we need 479 // a bug-compatibility. 480 if (Signature.empty() && Sym->getType() == STT_SECTION) 481 return getSectionName(Sec); 482 return Signature; 483 } 484 485 template <class ELFT> bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &Sec) { 486 // On a regular link we don't merge sections if -O0 (default is -O1). This 487 // sometimes makes the linker significantly faster, although the output will 488 // be bigger. 489 // 490 // Doing the same for -r would create a problem as it would combine sections 491 // with different sh_entsize. One option would be to just copy every SHF_MERGE 492 // section as is to the output. While this would produce a valid ELF file with 493 // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 494 // they see two .debug_str. We could have separate logic for combining 495 // SHF_MERGE sections based both on their name and sh_entsize, but that seems 496 // to be more trouble than it is worth. Instead, we just use the regular (-O1) 497 // logic for -r. 498 if (Config->Optimize == 0 && !Config->Relocatable) 499 return false; 500 501 // A mergeable section with size 0 is useless because they don't have 502 // any data to merge. A mergeable string section with size 0 can be 503 // argued as invalid because it doesn't end with a null character. 504 // We'll avoid a mess by handling them as if they were non-mergeable. 505 if (Sec.sh_size == 0) 506 return false; 507 508 // Check for sh_entsize. The ELF spec is not clear about the zero 509 // sh_entsize. It says that "the member [sh_entsize] contains 0 if 510 // the section does not hold a table of fixed-size entries". We know 511 // that Rust 1.13 produces a string mergeable section with a zero 512 // sh_entsize. Here we just accept it rather than being picky about it. 513 uint64_t EntSize = Sec.sh_entsize; 514 if (EntSize == 0) 515 return false; 516 if (Sec.sh_size % EntSize) 517 fatal(toString(this) + 518 ": SHF_MERGE section size must be a multiple of sh_entsize"); 519 520 uint64_t Flags = Sec.sh_flags; 521 if (!(Flags & SHF_MERGE)) 522 return false; 523 if (Flags & SHF_WRITE) 524 fatal(toString(this) + ": writable SHF_MERGE section is not supported"); 525 526 return true; 527 } 528 529 // This is for --just-symbols. 530 // 531 // --just-symbols is a very minor feature that allows you to link your 532 // output against other existing program, so that if you load both your 533 // program and the other program into memory, your output can refer the 534 // other program's symbols. 535 // 536 // When the option is given, we link "just symbols". The section table is 537 // initialized with null pointers. 538 template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 539 ArrayRef<Elf_Shdr> Sections = CHECK(this->getObj().sections(), this); 540 this->Sections.resize(Sections.size()); 541 } 542 543 // An ELF object file may contain a `.deplibs` section. If it exists, the 544 // section contains a list of library specifiers such as `m` for libm. This 545 // function resolves a given name by finding the first matching library checking 546 // the various ways that a library can be specified to LLD. This ELF extension 547 // is a form of autolinking and is called `dependent libraries`. It is currently 548 // unique to LLVM and lld. 549 static void addDependentLibrary(StringRef Specifier, const InputFile *F) { 550 if (!Config->DependentLibraries) 551 return; 552 if (fs::exists(Specifier)) 553 Driver->addFile(Specifier, /*WithLOption=*/false); 554 else if (Optional<std::string> S = findFromSearchPaths(Specifier)) 555 Driver->addFile(*S, /*WithLOption=*/true); 556 else if (Optional<std::string> S = searchLibraryBaseName(Specifier)) 557 Driver->addFile(*S, /*WithLOption=*/true); 558 else 559 error(toString(F) + 560 ": unable to find library from dependent library specifier: " + 561 Specifier); 562 } 563 564 template <class ELFT> 565 void ObjFile<ELFT>::initializeSections(bool IgnoreComdats) { 566 const ELFFile<ELFT> &Obj = this->getObj(); 567 568 ArrayRef<Elf_Shdr> ObjSections = CHECK(Obj.sections(), this); 569 uint64_t Size = ObjSections.size(); 570 this->Sections.resize(Size); 571 this->SectionStringTable = 572 CHECK(Obj.getSectionStringTable(ObjSections), this); 573 574 for (size_t I = 0, E = ObjSections.size(); I < E; I++) { 575 if (this->Sections[I] == &InputSection::Discarded) 576 continue; 577 const Elf_Shdr &Sec = ObjSections[I]; 578 579 if (Sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) 580 CGProfile = 581 check(Obj.template getSectionContentsAsArray<Elf_CGProfile>(&Sec)); 582 583 // SHF_EXCLUDE'ed sections are discarded by the linker. However, 584 // if -r is given, we'll let the final link discard such sections. 585 // This is compatible with GNU. 586 if ((Sec.sh_flags & SHF_EXCLUDE) && !Config->Relocatable) { 587 if (Sec.sh_type == SHT_LLVM_ADDRSIG) { 588 // We ignore the address-significance table if we know that the object 589 // file was created by objcopy or ld -r. This is because these tools 590 // will reorder the symbols in the symbol table, invalidating the data 591 // in the address-significance table, which refers to symbols by index. 592 if (Sec.sh_link != 0) 593 this->AddrsigSec = &Sec; 594 else if (Config->ICF == ICFLevel::Safe) 595 warn(toString(this) + ": --icf=safe is incompatible with object " 596 "files created using objcopy or ld -r"); 597 } 598 this->Sections[I] = &InputSection::Discarded; 599 continue; 600 } 601 602 switch (Sec.sh_type) { 603 case SHT_GROUP: { 604 // De-duplicate section groups by their signatures. 605 StringRef Signature = getShtGroupSignature(ObjSections, Sec); 606 this->Sections[I] = &InputSection::Discarded; 607 608 609 ArrayRef<Elf_Word> Entries = 610 CHECK(Obj.template getSectionContentsAsArray<Elf_Word>(&Sec), this); 611 if (Entries.empty()) 612 fatal(toString(this) + ": empty SHT_GROUP"); 613 614 // The first word of a SHT_GROUP section contains flags. Currently, 615 // the standard defines only "GRP_COMDAT" flag for the COMDAT group. 616 // An group with the empty flag doesn't define anything; such sections 617 // are just skipped. 618 if (Entries[0] == 0) 619 continue; 620 621 if (Entries[0] != GRP_COMDAT) 622 fatal(toString(this) + ": unsupported SHT_GROUP format"); 623 624 bool IsNew = 625 IgnoreComdats || 626 Symtab->ComdatGroups.try_emplace(CachedHashStringRef(Signature), this) 627 .second; 628 if (IsNew) { 629 if (Config->Relocatable) 630 this->Sections[I] = createInputSection(Sec); 631 continue; 632 } 633 634 // Otherwise, discard group members. 635 for (uint32_t SecIndex : Entries.slice(1)) { 636 if (SecIndex >= Size) 637 fatal(toString(this) + 638 ": invalid section index in group: " + Twine(SecIndex)); 639 this->Sections[SecIndex] = &InputSection::Discarded; 640 } 641 break; 642 } 643 case SHT_SYMTAB_SHNDX: 644 ShndxTable = CHECK(Obj.getSHNDXTable(Sec, ObjSections), this); 645 break; 646 case SHT_SYMTAB: 647 case SHT_STRTAB: 648 case SHT_NULL: 649 break; 650 default: 651 this->Sections[I] = createInputSection(Sec); 652 } 653 654 // .ARM.exidx sections have a reverse dependency on the InputSection they 655 // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. 656 if (Sec.sh_flags & SHF_LINK_ORDER) { 657 InputSectionBase *LinkSec = nullptr; 658 if (Sec.sh_link < this->Sections.size()) 659 LinkSec = this->Sections[Sec.sh_link]; 660 if (!LinkSec) 661 fatal(toString(this) + 662 ": invalid sh_link index: " + Twine(Sec.sh_link)); 663 664 InputSection *IS = cast<InputSection>(this->Sections[I]); 665 LinkSec->DependentSections.push_back(IS); 666 if (!isa<InputSection>(LinkSec)) 667 error("a section " + IS->Name + 668 " with SHF_LINK_ORDER should not refer a non-regular " 669 "section: " + 670 toString(LinkSec)); 671 } 672 } 673 } 674 675 // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD 676 // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how 677 // the input objects have been compiled. 678 static void updateARMVFPArgs(const ARMAttributeParser &Attributes, 679 const InputFile *F) { 680 if (!Attributes.hasAttribute(ARMBuildAttrs::ABI_VFP_args)) 681 // If an ABI tag isn't present then it is implicitly given the value of 0 682 // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files, 683 // including some in glibc that don't use FP args (and should have value 3) 684 // don't have the attribute so we do not consider an implicit value of 0 685 // as a clash. 686 return; 687 688 unsigned VFPArgs = Attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args); 689 ARMVFPArgKind Arg; 690 switch (VFPArgs) { 691 case ARMBuildAttrs::BaseAAPCS: 692 Arg = ARMVFPArgKind::Base; 693 break; 694 case ARMBuildAttrs::HardFPAAPCS: 695 Arg = ARMVFPArgKind::VFP; 696 break; 697 case ARMBuildAttrs::ToolChainFPPCS: 698 // Tool chain specific convention that conforms to neither AAPCS variant. 699 Arg = ARMVFPArgKind::ToolChain; 700 break; 701 case ARMBuildAttrs::CompatibleFPAAPCS: 702 // Object compatible with all conventions. 703 return; 704 default: 705 error(toString(F) + ": unknown Tag_ABI_VFP_args value: " + Twine(VFPArgs)); 706 return; 707 } 708 // Follow ld.bfd and error if there is a mix of calling conventions. 709 if (Config->ARMVFPArgs != Arg && Config->ARMVFPArgs != ARMVFPArgKind::Default) 710 error(toString(F) + ": incompatible Tag_ABI_VFP_args"); 711 else 712 Config->ARMVFPArgs = Arg; 713 } 714 715 // The ARM support in lld makes some use of instructions that are not available 716 // on all ARM architectures. Namely: 717 // - Use of BLX instruction for interworking between ARM and Thumb state. 718 // - Use of the extended Thumb branch encoding in relocation. 719 // - Use of the MOVT/MOVW instructions in Thumb Thunks. 720 // The ARM Attributes section contains information about the architecture chosen 721 // at compile time. We follow the convention that if at least one input object 722 // is compiled with an architecture that supports these features then lld is 723 // permitted to use them. 724 static void updateSupportedARMFeatures(const ARMAttributeParser &Attributes) { 725 if (!Attributes.hasAttribute(ARMBuildAttrs::CPU_arch)) 726 return; 727 auto Arch = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 728 switch (Arch) { 729 case ARMBuildAttrs::Pre_v4: 730 case ARMBuildAttrs::v4: 731 case ARMBuildAttrs::v4T: 732 // Architectures prior to v5 do not support BLX instruction 733 break; 734 case ARMBuildAttrs::v5T: 735 case ARMBuildAttrs::v5TE: 736 case ARMBuildAttrs::v5TEJ: 737 case ARMBuildAttrs::v6: 738 case ARMBuildAttrs::v6KZ: 739 case ARMBuildAttrs::v6K: 740 Config->ARMHasBlx = true; 741 // Architectures used in pre-Cortex processors do not support 742 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 743 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 744 break; 745 default: 746 // All other Architectures have BLX and extended branch encoding 747 Config->ARMHasBlx = true; 748 Config->ARMJ1J2BranchEncoding = true; 749 if (Arch != ARMBuildAttrs::v6_M && Arch != ARMBuildAttrs::v6S_M) 750 // All Architectures used in Cortex processors with the exception 751 // of v6-M and v6S-M have the MOVT and MOVW instructions. 752 Config->ARMHasMovtMovw = true; 753 break; 754 } 755 } 756 757 // If a source file is compiled with x86 hardware-assisted call flow control 758 // enabled, the generated object file contains feature flags indicating that 759 // fact. This function reads the feature flags and returns it. 760 // 761 // Essentially we want to read a single 32-bit value in this function, but this 762 // function is rather complicated because the value is buried deep inside a 763 // .note.gnu.property section. 764 // 765 // The section consists of one or more NOTE records. Each NOTE record consists 766 // of zero or more type-length-value fields. We want to find a field of a 767 // certain type. It seems a bit too much to just store a 32-bit value, perhaps 768 // the ABI is unnecessarily complicated. 769 template <class ELFT> 770 static uint32_t readAndFeatures(ObjFile<ELFT> *Obj, ArrayRef<uint8_t> Data) { 771 using Elf_Nhdr = typename ELFT::Nhdr; 772 using Elf_Note = typename ELFT::Note; 773 774 uint32_t FeaturesSet = 0; 775 while (!Data.empty()) { 776 // Read one NOTE record. 777 if (Data.size() < sizeof(Elf_Nhdr)) 778 fatal(toString(Obj) + ": .note.gnu.property: section too short"); 779 780 auto *Nhdr = reinterpret_cast<const Elf_Nhdr *>(Data.data()); 781 if (Data.size() < Nhdr->getSize()) 782 fatal(toString(Obj) + ": .note.gnu.property: section too short"); 783 784 Elf_Note Note(*Nhdr); 785 if (Nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || Note.getName() != "GNU") { 786 Data = Data.slice(Nhdr->getSize()); 787 continue; 788 } 789 790 uint32_t FeatureAndType = Config->EMachine == EM_AARCH64 791 ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 792 : GNU_PROPERTY_X86_FEATURE_1_AND; 793 794 // Read a body of a NOTE record, which consists of type-length-value fields. 795 ArrayRef<uint8_t> Desc = Note.getDesc(); 796 while (!Desc.empty()) { 797 if (Desc.size() < 8) 798 fatal(toString(Obj) + ": .note.gnu.property: section too short"); 799 800 uint32_t Type = read32le(Desc.data()); 801 uint32_t Size = read32le(Desc.data() + 4); 802 803 if (Type == FeatureAndType) { 804 // We found a FEATURE_1_AND field. There may be more than one of these 805 // in a .note.gnu.propery section, for a relocatable object we 806 // accumulate the bits set. 807 FeaturesSet |= read32le(Desc.data() + 8); 808 } 809 810 // On 64-bit, a payload may be followed by a 4-byte padding to make its 811 // size a multiple of 8. 812 if (ELFT::Is64Bits) 813 Size = alignTo(Size, 8); 814 815 Desc = Desc.slice(Size + 8); // +8 for Type and Size 816 } 817 818 // Go to next NOTE record to look for more FEATURE_1_AND descriptions. 819 Data = Data.slice(Nhdr->getSize()); 820 } 821 822 return FeaturesSet; 823 } 824 825 template <class ELFT> 826 InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &Sec) { 827 uint32_t Idx = Sec.sh_info; 828 if (Idx >= this->Sections.size()) 829 fatal(toString(this) + ": invalid relocated section index: " + Twine(Idx)); 830 InputSectionBase *Target = this->Sections[Idx]; 831 832 // Strictly speaking, a relocation section must be included in the 833 // group of the section it relocates. However, LLVM 3.3 and earlier 834 // would fail to do so, so we gracefully handle that case. 835 if (Target == &InputSection::Discarded) 836 return nullptr; 837 838 if (!Target) 839 fatal(toString(this) + ": unsupported relocation reference"); 840 return Target; 841 } 842 843 // Create a regular InputSection class that has the same contents 844 // as a given section. 845 static InputSection *toRegularSection(MergeInputSection *Sec) { 846 return make<InputSection>(Sec->File, Sec->Flags, Sec->Type, Sec->Alignment, 847 Sec->data(), Sec->Name); 848 } 849 850 template <class ELFT> 851 InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &Sec) { 852 StringRef Name = getSectionName(Sec); 853 854 switch (Sec.sh_type) { 855 case SHT_ARM_ATTRIBUTES: { 856 if (Config->EMachine != EM_ARM) 857 break; 858 ARMAttributeParser Attributes; 859 ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec)); 860 Attributes.Parse(Contents, /*isLittle*/ Config->EKind == ELF32LEKind); 861 updateSupportedARMFeatures(Attributes); 862 updateARMVFPArgs(Attributes, this); 863 864 // FIXME: Retain the first attribute section we see. The eglibc ARM 865 // dynamic loaders require the presence of an attribute section for dlopen 866 // to work. In a full implementation we would merge all attribute sections. 867 if (In.ARMAttributes == nullptr) { 868 In.ARMAttributes = make<InputSection>(*this, Sec, Name); 869 return In.ARMAttributes; 870 } 871 return &InputSection::Discarded; 872 } 873 case SHT_LLVM_DEPENDENT_LIBRARIES: { 874 if (Config->Relocatable) 875 break; 876 ArrayRef<char> Data = 877 CHECK(this->getObj().template getSectionContentsAsArray<char>(&Sec), this); 878 if (!Data.empty() && Data.back() != '\0') { 879 error(toString(this) + 880 ": corrupted dependent libraries section (unterminated string): " + 881 Name); 882 return &InputSection::Discarded; 883 } 884 for (const char *D = Data.begin(), *E = Data.end(); D < E;) { 885 StringRef S(D); 886 addDependentLibrary(S, this); 887 D += S.size() + 1; 888 } 889 return &InputSection::Discarded; 890 } 891 case SHT_RELA: 892 case SHT_REL: { 893 // Find a relocation target section and associate this section with that. 894 // Target may have been discarded if it is in a different section group 895 // and the group is discarded, even though it's a violation of the 896 // spec. We handle that situation gracefully by discarding dangling 897 // relocation sections. 898 InputSectionBase *Target = getRelocTarget(Sec); 899 if (!Target) 900 return nullptr; 901 902 // This section contains relocation information. 903 // If -r is given, we do not interpret or apply relocation 904 // but just copy relocation sections to output. 905 if (Config->Relocatable) { 906 InputSection *RelocSec = make<InputSection>(*this, Sec, Name); 907 // We want to add a dependency to target, similar like we do for 908 // -emit-relocs below. This is useful for the case when linker script 909 // contains the "/DISCARD/". It is perhaps uncommon to use a script with 910 // -r, but we faced it in the Linux kernel and have to handle such case 911 // and not to crash. 912 Target->DependentSections.push_back(RelocSec); 913 return RelocSec; 914 } 915 916 if (Target->FirstRelocation) 917 fatal(toString(this) + 918 ": multiple relocation sections to one section are not supported"); 919 920 // ELF spec allows mergeable sections with relocations, but they are 921 // rare, and it is in practice hard to merge such sections by contents, 922 // because applying relocations at end of linking changes section 923 // contents. So, we simply handle such sections as non-mergeable ones. 924 // Degrading like this is acceptable because section merging is optional. 925 if (auto *MS = dyn_cast<MergeInputSection>(Target)) { 926 Target = toRegularSection(MS); 927 this->Sections[Sec.sh_info] = Target; 928 } 929 930 if (Sec.sh_type == SHT_RELA) { 931 ArrayRef<Elf_Rela> Rels = CHECK(getObj().relas(&Sec), this); 932 Target->FirstRelocation = Rels.begin(); 933 Target->NumRelocations = Rels.size(); 934 Target->AreRelocsRela = true; 935 } else { 936 ArrayRef<Elf_Rel> Rels = CHECK(getObj().rels(&Sec), this); 937 Target->FirstRelocation = Rels.begin(); 938 Target->NumRelocations = Rels.size(); 939 Target->AreRelocsRela = false; 940 } 941 assert(isUInt<31>(Target->NumRelocations)); 942 943 // Relocation sections processed by the linker are usually removed 944 // from the output, so returning `nullptr` for the normal case. 945 // However, if -emit-relocs is given, we need to leave them in the output. 946 // (Some post link analysis tools need this information.) 947 if (Config->EmitRelocs) { 948 InputSection *RelocSec = make<InputSection>(*this, Sec, Name); 949 // We will not emit relocation section if target was discarded. 950 Target->DependentSections.push_back(RelocSec); 951 return RelocSec; 952 } 953 return nullptr; 954 } 955 } 956 957 // The GNU linker uses .note.GNU-stack section as a marker indicating 958 // that the code in the object file does not expect that the stack is 959 // executable (in terms of NX bit). If all input files have the marker, 960 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 961 // make the stack non-executable. Most object files have this section as 962 // of 2017. 963 // 964 // But making the stack non-executable is a norm today for security 965 // reasons. Failure to do so may result in a serious security issue. 966 // Therefore, we make LLD always add PT_GNU_STACK unless it is 967 // explicitly told to do otherwise (by -z execstack). Because the stack 968 // executable-ness is controlled solely by command line options, 969 // .note.GNU-stack sections are simply ignored. 970 if (Name == ".note.GNU-stack") 971 return &InputSection::Discarded; 972 973 // Object files that use processor features such as Intel Control-Flow 974 // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a 975 // .note.gnu.property section containing a bitfield of feature bits like the 976 // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag. 977 // 978 // Since we merge bitmaps from multiple object files to create a new 979 // .note.gnu.property containing a single AND'ed bitmap, we discard an input 980 // file's .note.gnu.property section. 981 if (Name == ".note.gnu.property") { 982 ArrayRef<uint8_t> Contents = check(this->getObj().getSectionContents(&Sec)); 983 this->AndFeatures = readAndFeatures(this, Contents); 984 return &InputSection::Discarded; 985 } 986 987 // Split stacks is a feature to support a discontiguous stack, 988 // commonly used in the programming language Go. For the details, 989 // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled 990 // for split stack will include a .note.GNU-split-stack section. 991 if (Name == ".note.GNU-split-stack") { 992 if (Config->Relocatable) { 993 error("cannot mix split-stack and non-split-stack in a relocatable link"); 994 return &InputSection::Discarded; 995 } 996 this->SplitStack = true; 997 return &InputSection::Discarded; 998 } 999 1000 // An object file cmpiled for split stack, but where some of the 1001 // functions were compiled with the no_split_stack_attribute will 1002 // include a .note.GNU-no-split-stack section. 1003 if (Name == ".note.GNU-no-split-stack") { 1004 this->SomeNoSplitStack = true; 1005 return &InputSection::Discarded; 1006 } 1007 1008 // The linkonce feature is a sort of proto-comdat. Some glibc i386 object 1009 // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce 1010 // sections. Drop those sections to avoid duplicate symbol errors. 1011 // FIXME: This is glibc PR20543, we should remove this hack once that has been 1012 // fixed for a while. 1013 if (Name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" || 1014 Name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx") 1015 return &InputSection::Discarded; 1016 1017 // If we are creating a new .build-id section, strip existing .build-id 1018 // sections so that the output won't have more than one .build-id. 1019 // This is not usually a problem because input object files normally don't 1020 // have .build-id sections, but you can create such files by 1021 // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it. 1022 if (Name == ".note.gnu.build-id" && Config->BuildId != BuildIdKind::None) 1023 return &InputSection::Discarded; 1024 1025 // The linker merges EH (exception handling) frames and creates a 1026 // .eh_frame_hdr section for runtime. So we handle them with a special 1027 // class. For relocatable outputs, they are just passed through. 1028 if (Name == ".eh_frame" && !Config->Relocatable) 1029 return make<EhInputSection>(*this, Sec, Name); 1030 1031 if (shouldMerge(Sec)) 1032 return make<MergeInputSection>(*this, Sec, Name); 1033 return make<InputSection>(*this, Sec, Name); 1034 } 1035 1036 template <class ELFT> 1037 StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &Sec) { 1038 return CHECK(getObj().getSectionName(&Sec, SectionStringTable), this); 1039 } 1040 1041 // Initialize this->Symbols. this->Symbols is a parallel array as 1042 // its corresponding ELF symbol table. 1043 template <class ELFT> void ObjFile<ELFT>::initializeSymbols() { 1044 ArrayRef<Elf_Sym> ESyms = this->getELFSyms<ELFT>(); 1045 this->Symbols.resize(ESyms.size()); 1046 1047 // Our symbol table may have already been partially initialized 1048 // because of LazyObjFile. 1049 for (size_t I = 0, End = ESyms.size(); I != End; ++I) 1050 if (!this->Symbols[I] && ESyms[I].getBinding() != STB_LOCAL) 1051 this->Symbols[I] = 1052 Symtab->insert(CHECK(ESyms[I].getName(this->StringTable), this)); 1053 1054 // Fill this->Symbols. A symbol is either local or global. 1055 for (size_t I = 0, End = ESyms.size(); I != End; ++I) { 1056 const Elf_Sym &ESym = ESyms[I]; 1057 1058 // Read symbol attributes. 1059 uint32_t SecIdx = getSectionIndex(ESym); 1060 if (SecIdx >= this->Sections.size()) 1061 fatal(toString(this) + ": invalid section index: " + Twine(SecIdx)); 1062 1063 InputSectionBase *Sec = this->Sections[SecIdx]; 1064 uint8_t Binding = ESym.getBinding(); 1065 uint8_t StOther = ESym.st_other; 1066 uint8_t Type = ESym.getType(); 1067 uint64_t Value = ESym.st_value; 1068 uint64_t Size = ESym.st_size; 1069 StringRefZ Name = this->StringTable.data() + ESym.st_name; 1070 1071 // Handle local symbols. Local symbols are not added to the symbol 1072 // table because they are not visible from other object files. We 1073 // allocate symbol instances and add their pointers to Symbols. 1074 if (Binding == STB_LOCAL) { 1075 if (ESym.getType() == STT_FILE) 1076 SourceFile = CHECK(ESym.getName(this->StringTable), this); 1077 1078 if (this->StringTable.size() <= ESym.st_name) 1079 fatal(toString(this) + ": invalid symbol name offset"); 1080 1081 if (ESym.st_shndx == SHN_UNDEF) 1082 this->Symbols[I] = make<Undefined>(this, Name, Binding, StOther, Type); 1083 else if (Sec == &InputSection::Discarded) 1084 this->Symbols[I] = make<Undefined>(this, Name, Binding, StOther, Type, 1085 /*DiscardedSecIdx=*/SecIdx); 1086 else 1087 this->Symbols[I] = 1088 make<Defined>(this, Name, Binding, StOther, Type, Value, Size, Sec); 1089 continue; 1090 } 1091 1092 // Handle global undefined symbols. 1093 if (ESym.st_shndx == SHN_UNDEF) { 1094 this->Symbols[I]->resolve(Undefined{this, Name, Binding, StOther, Type}); 1095 continue; 1096 } 1097 1098 // Handle global common symbols. 1099 if (ESym.st_shndx == SHN_COMMON) { 1100 if (Value == 0 || Value >= UINT32_MAX) 1101 fatal(toString(this) + ": common symbol '" + StringRef(Name.Data) + 1102 "' has invalid alignment: " + Twine(Value)); 1103 this->Symbols[I]->resolve( 1104 CommonSymbol{this, Name, Binding, StOther, Type, Value, Size}); 1105 continue; 1106 } 1107 1108 // If a defined symbol is in a discarded section, handle it as if it 1109 // were an undefined symbol. Such symbol doesn't comply with the 1110 // standard, but in practice, a .eh_frame often directly refer 1111 // COMDAT member sections, and if a comdat group is discarded, some 1112 // defined symbol in a .eh_frame becomes dangling symbols. 1113 if (Sec == &InputSection::Discarded) { 1114 this->Symbols[I]->resolve( 1115 Undefined{this, Name, Binding, StOther, Type, SecIdx}); 1116 continue; 1117 } 1118 1119 // Handle global defined symbols. 1120 if (Binding == STB_GLOBAL || Binding == STB_WEAK || 1121 Binding == STB_GNU_UNIQUE) { 1122 this->Symbols[I]->resolve( 1123 Defined{this, Name, Binding, StOther, Type, Value, Size, Sec}); 1124 continue; 1125 } 1126 1127 fatal(toString(this) + ": unexpected binding: " + Twine((int)Binding)); 1128 } 1129 } 1130 1131 ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&File) 1132 : InputFile(ArchiveKind, File->getMemoryBufferRef()), 1133 File(std::move(File)) {} 1134 1135 void ArchiveFile::parse() { 1136 for (const Archive::Symbol &Sym : File->symbols()) 1137 Symtab->addSymbol(LazyArchive{*this, Sym}); 1138 } 1139 1140 // Returns a buffer pointing to a member file containing a given symbol. 1141 void ArchiveFile::fetch(const Archive::Symbol &Sym) { 1142 Archive::Child C = 1143 CHECK(Sym.getMember(), toString(this) + 1144 ": could not get the member for symbol " + 1145 Sym.getName()); 1146 1147 if (!Seen.insert(C.getChildOffset()).second) 1148 return; 1149 1150 MemoryBufferRef MB = 1151 CHECK(C.getMemoryBufferRef(), 1152 toString(this) + 1153 ": could not get the buffer for the member defining symbol " + 1154 Sym.getName()); 1155 1156 if (Tar && C.getParent()->isThin()) 1157 Tar->append(relativeToRoot(CHECK(C.getFullName(), this)), MB.getBuffer()); 1158 1159 InputFile *File = createObjectFile( 1160 MB, getName(), C.getParent()->isThin() ? 0 : C.getChildOffset()); 1161 File->GroupId = GroupId; 1162 parseFile(File); 1163 } 1164 1165 unsigned SharedFile::VernauxNum; 1166 1167 // Parse the version definitions in the object file if present, and return a 1168 // vector whose nth element contains a pointer to the Elf_Verdef for version 1169 // identifier n. Version identifiers that are not definitions map to nullptr. 1170 template <typename ELFT> 1171 static std::vector<const void *> parseVerdefs(const uint8_t *Base, 1172 const typename ELFT::Shdr *Sec) { 1173 if (!Sec) 1174 return {}; 1175 1176 // We cannot determine the largest verdef identifier without inspecting 1177 // every Elf_Verdef, but both bfd and gold assign verdef identifiers 1178 // sequentially starting from 1, so we predict that the largest identifier 1179 // will be VerdefCount. 1180 unsigned VerdefCount = Sec->sh_info; 1181 std::vector<const void *> Verdefs(VerdefCount + 1); 1182 1183 // Build the Verdefs array by following the chain of Elf_Verdef objects 1184 // from the start of the .gnu.version_d section. 1185 const uint8_t *Verdef = Base + Sec->sh_offset; 1186 for (unsigned I = 0; I != VerdefCount; ++I) { 1187 auto *CurVerdef = reinterpret_cast<const typename ELFT::Verdef *>(Verdef); 1188 Verdef += CurVerdef->vd_next; 1189 unsigned VerdefIndex = CurVerdef->vd_ndx; 1190 Verdefs.resize(VerdefIndex + 1); 1191 Verdefs[VerdefIndex] = CurVerdef; 1192 } 1193 return Verdefs; 1194 } 1195 1196 // We do not usually care about alignments of data in shared object 1197 // files because the loader takes care of it. However, if we promote a 1198 // DSO symbol to point to .bss due to copy relocation, we need to keep 1199 // the original alignment requirements. We infer it in this function. 1200 template <typename ELFT> 1201 static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> Sections, 1202 const typename ELFT::Sym &Sym) { 1203 uint64_t Ret = UINT64_MAX; 1204 if (Sym.st_value) 1205 Ret = 1ULL << countTrailingZeros((uint64_t)Sym.st_value); 1206 if (0 < Sym.st_shndx && Sym.st_shndx < Sections.size()) 1207 Ret = std::min<uint64_t>(Ret, Sections[Sym.st_shndx].sh_addralign); 1208 return (Ret > UINT32_MAX) ? 0 : Ret; 1209 } 1210 1211 // Fully parse the shared object file. 1212 // 1213 // This function parses symbol versions. If a DSO has version information, 1214 // the file has a ".gnu.version_d" section which contains symbol version 1215 // definitions. Each symbol is associated to one version through a table in 1216 // ".gnu.version" section. That table is a parallel array for the symbol 1217 // table, and each table entry contains an index in ".gnu.version_d". 1218 // 1219 // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for 1220 // VER_NDX_GLOBAL. There's no table entry for these special versions in 1221 // ".gnu.version_d". 1222 // 1223 // The file format for symbol versioning is perhaps a bit more complicated 1224 // than necessary, but you can easily understand the code if you wrap your 1225 // head around the data structure described above. 1226 template <class ELFT> void SharedFile::parse() { 1227 using Elf_Dyn = typename ELFT::Dyn; 1228 using Elf_Shdr = typename ELFT::Shdr; 1229 using Elf_Sym = typename ELFT::Sym; 1230 using Elf_Verdef = typename ELFT::Verdef; 1231 using Elf_Versym = typename ELFT::Versym; 1232 1233 ArrayRef<Elf_Dyn> DynamicTags; 1234 const ELFFile<ELFT> Obj = this->getObj<ELFT>(); 1235 ArrayRef<Elf_Shdr> Sections = CHECK(Obj.sections(), this); 1236 1237 const Elf_Shdr *VersymSec = nullptr; 1238 const Elf_Shdr *VerdefSec = nullptr; 1239 1240 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. 1241 for (const Elf_Shdr &Sec : Sections) { 1242 switch (Sec.sh_type) { 1243 default: 1244 continue; 1245 case SHT_DYNAMIC: 1246 DynamicTags = 1247 CHECK(Obj.template getSectionContentsAsArray<Elf_Dyn>(&Sec), this); 1248 break; 1249 case SHT_GNU_versym: 1250 VersymSec = &Sec; 1251 break; 1252 case SHT_GNU_verdef: 1253 VerdefSec = &Sec; 1254 break; 1255 } 1256 } 1257 1258 if (VersymSec && NumELFSyms == 0) { 1259 error("SHT_GNU_versym should be associated with symbol table"); 1260 return; 1261 } 1262 1263 // Search for a DT_SONAME tag to initialize this->SoName. 1264 for (const Elf_Dyn &Dyn : DynamicTags) { 1265 if (Dyn.d_tag == DT_NEEDED) { 1266 uint64_t Val = Dyn.getVal(); 1267 if (Val >= this->StringTable.size()) 1268 fatal(toString(this) + ": invalid DT_NEEDED entry"); 1269 DtNeeded.push_back(this->StringTable.data() + Val); 1270 } else if (Dyn.d_tag == DT_SONAME) { 1271 uint64_t Val = Dyn.getVal(); 1272 if (Val >= this->StringTable.size()) 1273 fatal(toString(this) + ": invalid DT_SONAME entry"); 1274 SoName = this->StringTable.data() + Val; 1275 } 1276 } 1277 1278 // DSOs are uniquified not by filename but by soname. 1279 DenseMap<StringRef, SharedFile *>::iterator It; 1280 bool WasInserted; 1281 std::tie(It, WasInserted) = Symtab->SoNames.try_emplace(SoName, this); 1282 1283 // If a DSO appears more than once on the command line with and without 1284 // --as-needed, --no-as-needed takes precedence over --as-needed because a 1285 // user can add an extra DSO with --no-as-needed to force it to be added to 1286 // the dependency list. 1287 It->second->IsNeeded |= IsNeeded; 1288 if (!WasInserted) 1289 return; 1290 1291 SharedFiles.push_back(this); 1292 1293 Verdefs = parseVerdefs<ELFT>(Obj.base(), VerdefSec); 1294 1295 // Parse ".gnu.version" section which is a parallel array for the symbol 1296 // table. If a given file doesn't have a ".gnu.version" section, we use 1297 // VER_NDX_GLOBAL. 1298 size_t Size = NumELFSyms - FirstGlobal; 1299 std::vector<uint32_t> Versyms(Size, VER_NDX_GLOBAL); 1300 if (VersymSec) { 1301 ArrayRef<Elf_Versym> Versym = 1302 CHECK(Obj.template getSectionContentsAsArray<Elf_Versym>(VersymSec), 1303 this) 1304 .slice(FirstGlobal); 1305 for (size_t I = 0; I < Size; ++I) 1306 Versyms[I] = Versym[I].vs_index; 1307 } 1308 1309 // System libraries can have a lot of symbols with versions. Using a 1310 // fixed buffer for computing the versions name (foo@ver) can save a 1311 // lot of allocations. 1312 SmallString<0> VersionedNameBuffer; 1313 1314 // Add symbols to the symbol table. 1315 ArrayRef<Elf_Sym> Syms = this->getGlobalELFSyms<ELFT>(); 1316 for (size_t I = 0; I < Syms.size(); ++I) { 1317 const Elf_Sym &Sym = Syms[I]; 1318 1319 // ELF spec requires that all local symbols precede weak or global 1320 // symbols in each symbol table, and the index of first non-local symbol 1321 // is stored to sh_info. If a local symbol appears after some non-local 1322 // symbol, that's a violation of the spec. 1323 StringRef Name = CHECK(Sym.getName(this->StringTable), this); 1324 if (Sym.getBinding() == STB_LOCAL) { 1325 warn("found local symbol '" + Name + 1326 "' in global part of symbol table in file " + toString(this)); 1327 continue; 1328 } 1329 1330 if (Sym.isUndefined()) { 1331 Symbol *S = Symtab->addSymbol( 1332 Undefined{this, Name, Sym.getBinding(), Sym.st_other, Sym.getType()}); 1333 S->ExportDynamic = true; 1334 continue; 1335 } 1336 1337 // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly 1338 // assigns VER_NDX_LOCAL to this section global symbol. Here is a 1339 // workaround for this bug. 1340 uint32_t Idx = Versyms[I] & ~VERSYM_HIDDEN; 1341 if (Config->EMachine == EM_MIPS && Idx == VER_NDX_LOCAL && 1342 Name == "_gp_disp") 1343 continue; 1344 1345 uint32_t Alignment = getAlignment<ELFT>(Sections, Sym); 1346 if (!(Versyms[I] & VERSYM_HIDDEN)) { 1347 Symtab->addSymbol(SharedSymbol{*this, Name, Sym.getBinding(), 1348 Sym.st_other, Sym.getType(), Sym.st_value, 1349 Sym.st_size, Alignment, Idx}); 1350 } 1351 1352 // Also add the symbol with the versioned name to handle undefined symbols 1353 // with explicit versions. 1354 if (Idx == VER_NDX_GLOBAL) 1355 continue; 1356 1357 if (Idx >= Verdefs.size() || Idx == VER_NDX_LOCAL) { 1358 error("corrupt input file: version definition index " + Twine(Idx) + 1359 " for symbol " + Name + " is out of bounds\n>>> defined in " + 1360 toString(this)); 1361 continue; 1362 } 1363 1364 StringRef VerName = 1365 this->StringTable.data() + 1366 reinterpret_cast<const Elf_Verdef *>(Verdefs[Idx])->getAux()->vda_name; 1367 VersionedNameBuffer.clear(); 1368 Name = (Name + "@" + VerName).toStringRef(VersionedNameBuffer); 1369 Symtab->addSymbol(SharedSymbol{*this, Saver.save(Name), Sym.getBinding(), 1370 Sym.st_other, Sym.getType(), Sym.st_value, 1371 Sym.st_size, Alignment, Idx}); 1372 } 1373 } 1374 1375 static ELFKind getBitcodeELFKind(const Triple &T) { 1376 if (T.isLittleEndian()) 1377 return T.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 1378 return T.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 1379 } 1380 1381 static uint8_t getBitcodeMachineKind(StringRef Path, const Triple &T) { 1382 switch (T.getArch()) { 1383 case Triple::aarch64: 1384 return EM_AARCH64; 1385 case Triple::amdgcn: 1386 case Triple::r600: 1387 return EM_AMDGPU; 1388 case Triple::arm: 1389 case Triple::thumb: 1390 return EM_ARM; 1391 case Triple::avr: 1392 return EM_AVR; 1393 case Triple::mips: 1394 case Triple::mipsel: 1395 case Triple::mips64: 1396 case Triple::mips64el: 1397 return EM_MIPS; 1398 case Triple::msp430: 1399 return EM_MSP430; 1400 case Triple::ppc: 1401 return EM_PPC; 1402 case Triple::ppc64: 1403 case Triple::ppc64le: 1404 return EM_PPC64; 1405 case Triple::x86: 1406 return T.isOSIAMCU() ? EM_IAMCU : EM_386; 1407 case Triple::x86_64: 1408 return EM_X86_64; 1409 default: 1410 error(Path + ": could not infer e_machine from bitcode target triple " + 1411 T.str()); 1412 return EM_NONE; 1413 } 1414 } 1415 1416 BitcodeFile::BitcodeFile(MemoryBufferRef MB, StringRef ArchiveName, 1417 uint64_t OffsetInArchive) 1418 : InputFile(BitcodeKind, MB) { 1419 this->ArchiveName = ArchiveName; 1420 1421 std::string Path = MB.getBufferIdentifier().str(); 1422 if (Config->ThinLTOIndexOnly) 1423 Path = replaceThinLTOSuffix(MB.getBufferIdentifier()); 1424 1425 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique 1426 // name. If two archives define two members with the same name, this 1427 // causes a collision which result in only one of the objects being taken 1428 // into consideration at LTO time (which very likely causes undefined 1429 // symbols later in the link stage). So we append file offset to make 1430 // filename unique. 1431 StringRef Name = ArchiveName.empty() 1432 ? Saver.save(Path) 1433 : Saver.save(ArchiveName + "(" + Path + " at " + 1434 utostr(OffsetInArchive) + ")"); 1435 MemoryBufferRef MBRef(MB.getBuffer(), Name); 1436 1437 Obj = CHECK(lto::InputFile::create(MBRef), this); 1438 1439 Triple T(Obj->getTargetTriple()); 1440 EKind = getBitcodeELFKind(T); 1441 EMachine = getBitcodeMachineKind(MB.getBufferIdentifier(), T); 1442 } 1443 1444 static uint8_t mapVisibility(GlobalValue::VisibilityTypes GvVisibility) { 1445 switch (GvVisibility) { 1446 case GlobalValue::DefaultVisibility: 1447 return STV_DEFAULT; 1448 case GlobalValue::HiddenVisibility: 1449 return STV_HIDDEN; 1450 case GlobalValue::ProtectedVisibility: 1451 return STV_PROTECTED; 1452 } 1453 llvm_unreachable("unknown visibility"); 1454 } 1455 1456 template <class ELFT> 1457 static Symbol *createBitcodeSymbol(const std::vector<bool> &KeptComdats, 1458 const lto::InputFile::Symbol &ObjSym, 1459 BitcodeFile &F) { 1460 StringRef Name = Saver.save(ObjSym.getName()); 1461 uint8_t Binding = ObjSym.isWeak() ? STB_WEAK : STB_GLOBAL; 1462 uint8_t Type = ObjSym.isTLS() ? STT_TLS : STT_NOTYPE; 1463 uint8_t Visibility = mapVisibility(ObjSym.getVisibility()); 1464 bool CanOmitFromDynSym = ObjSym.canBeOmittedFromSymbolTable(); 1465 1466 int C = ObjSym.getComdatIndex(); 1467 if (ObjSym.isUndefined() || (C != -1 && !KeptComdats[C])) { 1468 Undefined New(&F, Name, Binding, Visibility, Type); 1469 if (CanOmitFromDynSym) 1470 New.ExportDynamic = false; 1471 return Symtab->addSymbol(New); 1472 } 1473 1474 if (ObjSym.isCommon()) 1475 return Symtab->addSymbol( 1476 CommonSymbol{&F, Name, Binding, Visibility, STT_OBJECT, 1477 ObjSym.getCommonAlignment(), ObjSym.getCommonSize()}); 1478 1479 Defined New(&F, Name, Binding, Visibility, Type, 0, 0, nullptr); 1480 if (CanOmitFromDynSym) 1481 New.ExportDynamic = false; 1482 return Symtab->addSymbol(New); 1483 } 1484 1485 template <class ELFT> void BitcodeFile::parse() { 1486 std::vector<bool> KeptComdats; 1487 for (StringRef S : Obj->getComdatTable()) 1488 KeptComdats.push_back( 1489 Symtab->ComdatGroups.try_emplace(CachedHashStringRef(S), this).second); 1490 1491 for (const lto::InputFile::Symbol &ObjSym : Obj->symbols()) 1492 Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this)); 1493 1494 for (auto L : Obj->getDependentLibraries()) 1495 addDependentLibrary(L, this); 1496 } 1497 1498 void BinaryFile::parse() { 1499 ArrayRef<uint8_t> Data = arrayRefFromStringRef(MB.getBuffer()); 1500 auto *Section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 1501 8, Data, ".data"); 1502 Sections.push_back(Section); 1503 1504 // For each input file foo that is embedded to a result as a binary 1505 // blob, we define _binary_foo_{start,end,size} symbols, so that 1506 // user programs can access blobs by name. Non-alphanumeric 1507 // characters in a filename are replaced with underscore. 1508 std::string S = "_binary_" + MB.getBufferIdentifier().str(); 1509 for (size_t I = 0; I < S.size(); ++I) 1510 if (!isAlnum(S[I])) 1511 S[I] = '_'; 1512 1513 Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_start"), STB_GLOBAL, 1514 STV_DEFAULT, STT_OBJECT, 0, 0, Section}); 1515 Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_end"), STB_GLOBAL, 1516 STV_DEFAULT, STT_OBJECT, Data.size(), 0, Section}); 1517 Symtab->addSymbol(Defined{nullptr, Saver.save(S + "_size"), STB_GLOBAL, 1518 STV_DEFAULT, STT_OBJECT, Data.size(), 0, nullptr}); 1519 } 1520 1521 InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName, 1522 uint64_t OffsetInArchive) { 1523 if (isBitcode(MB)) 1524 return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive); 1525 1526 switch (getELFKind(MB, ArchiveName)) { 1527 case ELF32LEKind: 1528 return make<ObjFile<ELF32LE>>(MB, ArchiveName); 1529 case ELF32BEKind: 1530 return make<ObjFile<ELF32BE>>(MB, ArchiveName); 1531 case ELF64LEKind: 1532 return make<ObjFile<ELF64LE>>(MB, ArchiveName); 1533 case ELF64BEKind: 1534 return make<ObjFile<ELF64BE>>(MB, ArchiveName); 1535 default: 1536 llvm_unreachable("getELFKind"); 1537 } 1538 } 1539 1540 void LazyObjFile::fetch() { 1541 if (MB.getBuffer().empty()) 1542 return; 1543 1544 InputFile *File = createObjectFile(MB, ArchiveName, OffsetInArchive); 1545 File->GroupId = GroupId; 1546 1547 MB = {}; 1548 1549 // Copy symbol vector so that the new InputFile doesn't have to 1550 // insert the same defined symbols to the symbol table again. 1551 File->Symbols = std::move(Symbols); 1552 1553 parseFile(File); 1554 } 1555 1556 template <class ELFT> void LazyObjFile::parse() { 1557 using Elf_Sym = typename ELFT::Sym; 1558 1559 // A lazy object file wraps either a bitcode file or an ELF file. 1560 if (isBitcode(this->MB)) { 1561 std::unique_ptr<lto::InputFile> Obj = 1562 CHECK(lto::InputFile::create(this->MB), this); 1563 for (const lto::InputFile::Symbol &Sym : Obj->symbols()) { 1564 if (Sym.isUndefined()) 1565 continue; 1566 Symtab->addSymbol(LazyObject{*this, Saver.save(Sym.getName())}); 1567 } 1568 return; 1569 } 1570 1571 if (getELFKind(this->MB, ArchiveName) != Config->EKind) { 1572 error("incompatible file: " + this->MB.getBufferIdentifier()); 1573 return; 1574 } 1575 1576 // Find a symbol table. 1577 ELFFile<ELFT> Obj = check(ELFFile<ELFT>::create(MB.getBuffer())); 1578 ArrayRef<typename ELFT::Shdr> Sections = CHECK(Obj.sections(), this); 1579 1580 for (const typename ELFT::Shdr &Sec : Sections) { 1581 if (Sec.sh_type != SHT_SYMTAB) 1582 continue; 1583 1584 // A symbol table is found. 1585 ArrayRef<Elf_Sym> ESyms = CHECK(Obj.symbols(&Sec), this); 1586 uint32_t FirstGlobal = Sec.sh_info; 1587 StringRef Strtab = CHECK(Obj.getStringTableForSymtab(Sec, Sections), this); 1588 this->Symbols.resize(ESyms.size()); 1589 1590 // Get existing symbols or insert placeholder symbols. 1591 for (size_t I = FirstGlobal, End = ESyms.size(); I != End; ++I) 1592 if (ESyms[I].st_shndx != SHN_UNDEF) 1593 this->Symbols[I] = Symtab->insert(CHECK(ESyms[I].getName(Strtab), this)); 1594 1595 // Replace existing symbols with LazyObject symbols. 1596 // 1597 // resolve() may trigger this->fetch() if an existing symbol is an 1598 // undefined symbol. If that happens, this LazyObjFile has served 1599 // its purpose, and we can exit from the loop early. 1600 for (Symbol *Sym : this->Symbols) { 1601 if (!Sym) 1602 continue; 1603 Sym->resolve(LazyObject{*this, Sym->getName()}); 1604 1605 // MemoryBuffer is emptied if this file is instantiated as ObjFile. 1606 if (MB.getBuffer().empty()) 1607 return; 1608 } 1609 return; 1610 } 1611 } 1612 1613 std::string elf::replaceThinLTOSuffix(StringRef Path) { 1614 StringRef Suffix = Config->ThinLTOObjectSuffixReplace.first; 1615 StringRef Repl = Config->ThinLTOObjectSuffixReplace.second; 1616 1617 if (Path.consume_back(Suffix)) 1618 return (Path + Repl).str(); 1619 return Path; 1620 } 1621 1622 template void BitcodeFile::parse<ELF32LE>(); 1623 template void BitcodeFile::parse<ELF32BE>(); 1624 template void BitcodeFile::parse<ELF64LE>(); 1625 template void BitcodeFile::parse<ELF64BE>(); 1626 1627 template void LazyObjFile::parse<ELF32LE>(); 1628 template void LazyObjFile::parse<ELF32BE>(); 1629 template void LazyObjFile::parse<ELF64LE>(); 1630 template void LazyObjFile::parse<ELF64BE>(); 1631 1632 template class elf::ObjFile<ELF32LE>; 1633 template class elf::ObjFile<ELF32BE>; 1634 template class elf::ObjFile<ELF64LE>; 1635 template class elf::ObjFile<ELF64BE>; 1636 1637 template void SharedFile::parse<ELF32LE>(); 1638 template void SharedFile::parse<ELF32BE>(); 1639 template void SharedFile::parse<ELF64LE>(); 1640 template void SharedFile::parse<ELF64BE>(); 1641