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