10b57cec5SDimitry Andric //===- InputFiles.cpp -----------------------------------------------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #include "InputFiles.h" 1081ad6265SDimitry Andric #include "Config.h" 1181ad6265SDimitry Andric #include "DWARF.h" 120b57cec5SDimitry Andric #include "Driver.h" 130b57cec5SDimitry Andric #include "InputSection.h" 140b57cec5SDimitry Andric #include "LinkerScript.h" 150b57cec5SDimitry Andric #include "SymbolTable.h" 160b57cec5SDimitry Andric #include "Symbols.h" 170b57cec5SDimitry Andric #include "SyntheticSections.h" 181fd87a68SDimitry Andric #include "Target.h" 1904eeddc0SDimitry Andric #include "lld/Common/CommonLinkerContext.h" 20480093f4SDimitry Andric #include "lld/Common/DWARF.h" 2181ad6265SDimitry Andric #include "llvm/ADT/CachedHashString.h" 220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 230b57cec5SDimitry Andric #include "llvm/LTO/LTO.h" 2481ad6265SDimitry Andric #include "llvm/Object/IRObjectFile.h" 250b57cec5SDimitry Andric #include "llvm/Support/ARMAttributeParser.h" 260b57cec5SDimitry Andric #include "llvm/Support/ARMBuildAttributes.h" 270b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 2881ad6265SDimitry Andric #include "llvm/Support/FileSystem.h" 290b57cec5SDimitry Andric #include "llvm/Support/Path.h" 30e8d8bef9SDimitry Andric #include "llvm/Support/RISCVAttributeParser.h" 310b57cec5SDimitry Andric #include "llvm/Support/TarWriter.h" 320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 33*5f757f3fSDimitry Andric #include <optional> 340b57cec5SDimitry Andric 350b57cec5SDimitry Andric using namespace llvm; 360b57cec5SDimitry Andric using namespace llvm::ELF; 370b57cec5SDimitry Andric using namespace llvm::object; 380b57cec5SDimitry Andric using namespace llvm::sys; 390b57cec5SDimitry Andric using namespace llvm::sys::fs; 400b57cec5SDimitry Andric using namespace llvm::support::endian; 415ffd83dbSDimitry Andric using namespace lld; 425ffd83dbSDimitry Andric using namespace lld::elf; 430b57cec5SDimitry Andric 44*5f757f3fSDimitry Andric // This function is explicity instantiated in ARM.cpp, don't do it here to avoid 45*5f757f3fSDimitry Andric // warnings with MSVC. 46*5f757f3fSDimitry Andric extern template void ObjFile<ELF32LE>::importCmseSymbols(); 47*5f757f3fSDimitry Andric extern template void ObjFile<ELF32BE>::importCmseSymbols(); 48*5f757f3fSDimitry Andric extern template void ObjFile<ELF64LE>::importCmseSymbols(); 49*5f757f3fSDimitry Andric extern template void ObjFile<ELF64BE>::importCmseSymbols(); 50*5f757f3fSDimitry Andric 515ffd83dbSDimitry Andric bool InputFile::isInGroup; 525ffd83dbSDimitry Andric uint32_t InputFile::nextGroupId; 535ffd83dbSDimitry Andric 545ffd83dbSDimitry Andric std::unique_ptr<TarWriter> elf::tar; 555ffd83dbSDimitry Andric 5685868e8aSDimitry Andric // Returns "<internal>", "foo.a(bar.o)" or "baz.o". 575ffd83dbSDimitry Andric std::string lld::toString(const InputFile *f) { 58bdd1243dSDimitry Andric static std::mutex mu; 5985868e8aSDimitry Andric if (!f) 6085868e8aSDimitry Andric return "<internal>"; 610b57cec5SDimitry Andric 62bdd1243dSDimitry Andric { 63bdd1243dSDimitry Andric std::lock_guard<std::mutex> lock(mu); 6485868e8aSDimitry Andric if (f->toStringCache.empty()) { 6585868e8aSDimitry Andric if (f->archiveName.empty()) 660eae32dcSDimitry Andric f->toStringCache = f->getName(); 6785868e8aSDimitry Andric else 680eae32dcSDimitry Andric (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache); 6985868e8aSDimitry Andric } 70bdd1243dSDimitry Andric } 710eae32dcSDimitry Andric return std::string(f->toStringCache); 7285868e8aSDimitry Andric } 7385868e8aSDimitry Andric 740b57cec5SDimitry Andric static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) { 750b57cec5SDimitry Andric unsigned char size; 760b57cec5SDimitry Andric unsigned char endian; 770b57cec5SDimitry Andric std::tie(size, endian) = getElfArchType(mb.getBuffer()); 780b57cec5SDimitry Andric 790b57cec5SDimitry Andric auto report = [&](StringRef msg) { 800b57cec5SDimitry Andric StringRef filename = mb.getBufferIdentifier(); 810b57cec5SDimitry Andric if (archiveName.empty()) 820b57cec5SDimitry Andric fatal(filename + ": " + msg); 830b57cec5SDimitry Andric else 840b57cec5SDimitry Andric fatal(archiveName + "(" + filename + "): " + msg); 850b57cec5SDimitry Andric }; 860b57cec5SDimitry Andric 8706c3fb27SDimitry Andric if (!mb.getBuffer().starts_with(ElfMagic)) 880b57cec5SDimitry Andric report("not an ELF file"); 890b57cec5SDimitry Andric if (endian != ELFDATA2LSB && endian != ELFDATA2MSB) 900b57cec5SDimitry Andric report("corrupted ELF file: invalid data encoding"); 910b57cec5SDimitry Andric if (size != ELFCLASS32 && size != ELFCLASS64) 920b57cec5SDimitry Andric report("corrupted ELF file: invalid file class"); 930b57cec5SDimitry Andric 940b57cec5SDimitry Andric size_t bufSize = mb.getBuffer().size(); 950b57cec5SDimitry Andric if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) || 960b57cec5SDimitry Andric (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr))) 970b57cec5SDimitry Andric report("corrupted ELF file: file is too short"); 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric if (size == ELFCLASS32) 1000b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 1010b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 1020b57cec5SDimitry Andric } 1030b57cec5SDimitry Andric 104bdd1243dSDimitry Andric // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD 105bdd1243dSDimitry Andric // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how 106bdd1243dSDimitry Andric // the input objects have been compiled. 107bdd1243dSDimitry Andric static void updateARMVFPArgs(const ARMAttributeParser &attributes, 108bdd1243dSDimitry Andric const InputFile *f) { 109bdd1243dSDimitry Andric std::optional<unsigned> attr = 110bdd1243dSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args); 111bdd1243dSDimitry Andric if (!attr) 112bdd1243dSDimitry Andric // If an ABI tag isn't present then it is implicitly given the value of 0 113bdd1243dSDimitry Andric // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files, 114bdd1243dSDimitry Andric // including some in glibc that don't use FP args (and should have value 3) 115bdd1243dSDimitry Andric // don't have the attribute so we do not consider an implicit value of 0 116bdd1243dSDimitry Andric // as a clash. 117bdd1243dSDimitry Andric return; 118bdd1243dSDimitry Andric 119bdd1243dSDimitry Andric unsigned vfpArgs = *attr; 120bdd1243dSDimitry Andric ARMVFPArgKind arg; 121bdd1243dSDimitry Andric switch (vfpArgs) { 122bdd1243dSDimitry Andric case ARMBuildAttrs::BaseAAPCS: 123bdd1243dSDimitry Andric arg = ARMVFPArgKind::Base; 124bdd1243dSDimitry Andric break; 125bdd1243dSDimitry Andric case ARMBuildAttrs::HardFPAAPCS: 126bdd1243dSDimitry Andric arg = ARMVFPArgKind::VFP; 127bdd1243dSDimitry Andric break; 128bdd1243dSDimitry Andric case ARMBuildAttrs::ToolChainFPPCS: 129bdd1243dSDimitry Andric // Tool chain specific convention that conforms to neither AAPCS variant. 130bdd1243dSDimitry Andric arg = ARMVFPArgKind::ToolChain; 131bdd1243dSDimitry Andric break; 132bdd1243dSDimitry Andric case ARMBuildAttrs::CompatibleFPAAPCS: 133bdd1243dSDimitry Andric // Object compatible with all conventions. 134bdd1243dSDimitry Andric return; 135bdd1243dSDimitry Andric default: 136bdd1243dSDimitry Andric error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs)); 137bdd1243dSDimitry Andric return; 138bdd1243dSDimitry Andric } 139bdd1243dSDimitry Andric // Follow ld.bfd and error if there is a mix of calling conventions. 140bdd1243dSDimitry Andric if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default) 141bdd1243dSDimitry Andric error(toString(f) + ": incompatible Tag_ABI_VFP_args"); 142bdd1243dSDimitry Andric else 143bdd1243dSDimitry Andric config->armVFPArgs = arg; 144bdd1243dSDimitry Andric } 145bdd1243dSDimitry Andric 146bdd1243dSDimitry Andric // The ARM support in lld makes some use of instructions that are not available 147bdd1243dSDimitry Andric // on all ARM architectures. Namely: 148bdd1243dSDimitry Andric // - Use of BLX instruction for interworking between ARM and Thumb state. 149bdd1243dSDimitry Andric // - Use of the extended Thumb branch encoding in relocation. 150bdd1243dSDimitry Andric // - Use of the MOVT/MOVW instructions in Thumb Thunks. 151bdd1243dSDimitry Andric // The ARM Attributes section contains information about the architecture chosen 152bdd1243dSDimitry Andric // at compile time. We follow the convention that if at least one input object 153bdd1243dSDimitry Andric // is compiled with an architecture that supports these features then lld is 154bdd1243dSDimitry Andric // permitted to use them. 155bdd1243dSDimitry Andric static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) { 156bdd1243dSDimitry Andric std::optional<unsigned> attr = 157bdd1243dSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 158bdd1243dSDimitry Andric if (!attr) 159bdd1243dSDimitry Andric return; 160bdd1243dSDimitry Andric auto arch = *attr; 161bdd1243dSDimitry Andric switch (arch) { 162bdd1243dSDimitry Andric case ARMBuildAttrs::Pre_v4: 163bdd1243dSDimitry Andric case ARMBuildAttrs::v4: 164bdd1243dSDimitry Andric case ARMBuildAttrs::v4T: 165bdd1243dSDimitry Andric // Architectures prior to v5 do not support BLX instruction 166bdd1243dSDimitry Andric break; 167bdd1243dSDimitry Andric case ARMBuildAttrs::v5T: 168bdd1243dSDimitry Andric case ARMBuildAttrs::v5TE: 169bdd1243dSDimitry Andric case ARMBuildAttrs::v5TEJ: 170bdd1243dSDimitry Andric case ARMBuildAttrs::v6: 171bdd1243dSDimitry Andric case ARMBuildAttrs::v6KZ: 172bdd1243dSDimitry Andric case ARMBuildAttrs::v6K: 173bdd1243dSDimitry Andric config->armHasBlx = true; 174bdd1243dSDimitry Andric // Architectures used in pre-Cortex processors do not support 175bdd1243dSDimitry Andric // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 176bdd1243dSDimitry Andric // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 177bdd1243dSDimitry Andric break; 178bdd1243dSDimitry Andric default: 179bdd1243dSDimitry Andric // All other Architectures have BLX and extended branch encoding 180bdd1243dSDimitry Andric config->armHasBlx = true; 181bdd1243dSDimitry Andric config->armJ1J2BranchEncoding = true; 182bdd1243dSDimitry Andric if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M) 183bdd1243dSDimitry Andric // All Architectures used in Cortex processors with the exception 184bdd1243dSDimitry Andric // of v6-M and v6S-M have the MOVT and MOVW instructions. 185bdd1243dSDimitry Andric config->armHasMovtMovw = true; 186bdd1243dSDimitry Andric break; 187bdd1243dSDimitry Andric } 18806c3fb27SDimitry Andric 18906c3fb27SDimitry Andric // Only ARMv8-M or later architectures have CMSE support. 19006c3fb27SDimitry Andric std::optional<unsigned> profile = 19106c3fb27SDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile); 19206c3fb27SDimitry Andric if (!profile) 19306c3fb27SDimitry Andric return; 19406c3fb27SDimitry Andric if (arch >= ARMBuildAttrs::CPUArch::v8_M_Base && 19506c3fb27SDimitry Andric profile == ARMBuildAttrs::MicroControllerProfile) 19606c3fb27SDimitry Andric config->armCMSESupport = true; 197bdd1243dSDimitry Andric } 198bdd1243dSDimitry Andric 1990b57cec5SDimitry Andric InputFile::InputFile(Kind k, MemoryBufferRef m) 2000b57cec5SDimitry Andric : mb(m), groupId(nextGroupId), fileKind(k) { 2010b57cec5SDimitry Andric // All files within the same --{start,end}-group get the same group ID. 2020b57cec5SDimitry Andric // Otherwise, a new file will get a new group ID. 2030b57cec5SDimitry Andric if (!isInGroup) 2040b57cec5SDimitry Andric ++nextGroupId; 2050b57cec5SDimitry Andric } 2060b57cec5SDimitry Andric 207bdd1243dSDimitry Andric std::optional<MemoryBufferRef> elf::readFile(StringRef path) { 208e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Load input files", path); 209e8d8bef9SDimitry Andric 2100b57cec5SDimitry Andric // The --chroot option changes our virtual root directory. 2110b57cec5SDimitry Andric // This is useful when you are dealing with files created by --reproduce. 21206c3fb27SDimitry Andric if (!config->chroot.empty() && path.starts_with("/")) 21304eeddc0SDimitry Andric path = saver().save(config->chroot + path); 2140b57cec5SDimitry Andric 21506c3fb27SDimitry Andric bool remapped = false; 21606c3fb27SDimitry Andric auto it = config->remapInputs.find(path); 21706c3fb27SDimitry Andric if (it != config->remapInputs.end()) { 21806c3fb27SDimitry Andric path = it->second; 21906c3fb27SDimitry Andric remapped = true; 22006c3fb27SDimitry Andric } else { 22106c3fb27SDimitry Andric for (const auto &[pat, toFile] : config->remapInputsWildcards) { 22206c3fb27SDimitry Andric if (pat.match(path)) { 22306c3fb27SDimitry Andric path = toFile; 22406c3fb27SDimitry Andric remapped = true; 22506c3fb27SDimitry Andric break; 22606c3fb27SDimitry Andric } 22706c3fb27SDimitry Andric } 22806c3fb27SDimitry Andric } 22906c3fb27SDimitry Andric if (remapped) { 23006c3fb27SDimitry Andric // Use /dev/null to indicate an input file that should be ignored. Change 23106c3fb27SDimitry Andric // the path to NUL on Windows. 23206c3fb27SDimitry Andric #ifdef _WIN32 23306c3fb27SDimitry Andric if (path == "/dev/null") 23406c3fb27SDimitry Andric path = "NUL"; 23506c3fb27SDimitry Andric #endif 23606c3fb27SDimitry Andric } 23706c3fb27SDimitry Andric 2380b57cec5SDimitry Andric log(path); 239e8d8bef9SDimitry Andric config->dependencyFiles.insert(llvm::CachedHashString(path)); 2400b57cec5SDimitry Andric 241fe6060f1SDimitry Andric auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, 242fe6060f1SDimitry Andric /*RequiresNullTerminator=*/false); 2430b57cec5SDimitry Andric if (auto ec = mbOrErr.getError()) { 2440b57cec5SDimitry Andric error("cannot open " + path + ": " + ec.message()); 245bdd1243dSDimitry Andric return std::nullopt; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 24804eeddc0SDimitry Andric MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef(); 249bdd1243dSDimitry Andric ctx.memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership 2500b57cec5SDimitry Andric 2510b57cec5SDimitry Andric if (tar) 2520b57cec5SDimitry Andric tar->append(relativeToRoot(path), mbref.getBuffer()); 2530b57cec5SDimitry Andric return mbref; 2540b57cec5SDimitry Andric } 2550b57cec5SDimitry Andric 2560b57cec5SDimitry Andric // All input object files must be for the same architecture 2570b57cec5SDimitry Andric // (e.g. it does not make sense to link x86 object files with 2580b57cec5SDimitry Andric // MIPS object files.) This function checks for that error. 2590b57cec5SDimitry Andric static bool isCompatible(InputFile *file) { 2600b57cec5SDimitry Andric if (!file->isElf() && !isa<BitcodeFile>(file)) 2610b57cec5SDimitry Andric return true; 2620b57cec5SDimitry Andric 2630b57cec5SDimitry Andric if (file->ekind == config->ekind && file->emachine == config->emachine) { 2640b57cec5SDimitry Andric if (config->emachine != EM_MIPS) 2650b57cec5SDimitry Andric return true; 2660b57cec5SDimitry Andric if (isMipsN32Abi(file) == config->mipsN32Abi) 2670b57cec5SDimitry Andric return true; 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2705ffd83dbSDimitry Andric StringRef target = 2715ffd83dbSDimitry Andric !config->bfdname.empty() ? config->bfdname : config->emulation; 2725ffd83dbSDimitry Andric if (!target.empty()) { 2735ffd83dbSDimitry Andric error(toString(file) + " is incompatible with " + target); 27485868e8aSDimitry Andric return false; 27585868e8aSDimitry Andric } 27685868e8aSDimitry Andric 277d56accc7SDimitry Andric InputFile *existing = nullptr; 278bdd1243dSDimitry Andric if (!ctx.objectFiles.empty()) 279bdd1243dSDimitry Andric existing = ctx.objectFiles[0]; 280bdd1243dSDimitry Andric else if (!ctx.sharedFiles.empty()) 281bdd1243dSDimitry Andric existing = ctx.sharedFiles[0]; 282bdd1243dSDimitry Andric else if (!ctx.bitcodeFiles.empty()) 283bdd1243dSDimitry Andric existing = ctx.bitcodeFiles[0]; 284d56accc7SDimitry Andric std::string with; 285d56accc7SDimitry Andric if (existing) 286d56accc7SDimitry Andric with = " with " + toString(existing); 287d56accc7SDimitry Andric error(toString(file) + " is incompatible" + with); 2880b57cec5SDimitry Andric return false; 2890b57cec5SDimitry Andric } 2900b57cec5SDimitry Andric 2910b57cec5SDimitry Andric template <class ELFT> static void doParseFile(InputFile *file) { 2920b57cec5SDimitry Andric if (!isCompatible(file)) 2930b57cec5SDimitry Andric return; 2940b57cec5SDimitry Andric 2950b57cec5SDimitry Andric // Lazy object file 2960eae32dcSDimitry Andric if (file->lazy) { 2970eae32dcSDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 298bdd1243dSDimitry Andric ctx.lazyBitcodeFiles.push_back(f); 2990eae32dcSDimitry Andric f->parseLazy(); 3000eae32dcSDimitry Andric } else { 3010eae32dcSDimitry Andric cast<ObjFile<ELFT>>(file)->parseLazy(); 3020eae32dcSDimitry Andric } 3030b57cec5SDimitry Andric return; 3040b57cec5SDimitry Andric } 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric if (config->trace) 3070b57cec5SDimitry Andric message(toString(file)); 3080b57cec5SDimitry Andric 309*5f757f3fSDimitry Andric if (file->kind() == InputFile::ObjKind) { 310bdd1243dSDimitry Andric ctx.objectFiles.push_back(cast<ELFFileBase>(file)); 3110b57cec5SDimitry Andric cast<ObjFile<ELFT>>(file)->parse(); 312*5f757f3fSDimitry Andric } else if (auto *f = dyn_cast<SharedFile>(file)) { 313*5f757f3fSDimitry Andric f->parse<ELFT>(); 314*5f757f3fSDimitry Andric } else if (auto *f = dyn_cast<BitcodeFile>(file)) { 315*5f757f3fSDimitry Andric ctx.bitcodeFiles.push_back(f); 316*5f757f3fSDimitry Andric f->parse(); 317*5f757f3fSDimitry Andric } else { 318*5f757f3fSDimitry Andric ctx.binaryFiles.push_back(cast<BinaryFile>(file)); 319*5f757f3fSDimitry Andric cast<BinaryFile>(file)->parse(); 320*5f757f3fSDimitry Andric } 3210b57cec5SDimitry Andric } 3220b57cec5SDimitry Andric 3230b57cec5SDimitry Andric // Add symbols in File to the symbol table. 3241fd87a68SDimitry Andric void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); } 3250b57cec5SDimitry Andric 326*5f757f3fSDimitry Andric // This function is explicity instantiated in ARM.cpp. Mark it extern here, 327*5f757f3fSDimitry Andric // to avoid warnings when building with MSVC. 328*5f757f3fSDimitry Andric extern template void ObjFile<ELF32LE>::importCmseSymbols(); 329*5f757f3fSDimitry Andric extern template void ObjFile<ELF32BE>::importCmseSymbols(); 330*5f757f3fSDimitry Andric extern template void ObjFile<ELF64LE>::importCmseSymbols(); 331*5f757f3fSDimitry Andric extern template void ObjFile<ELF64BE>::importCmseSymbols(); 332*5f757f3fSDimitry Andric 33306c3fb27SDimitry Andric template <class ELFT> static void doParseArmCMSEImportLib(InputFile *file) { 33406c3fb27SDimitry Andric cast<ObjFile<ELFT>>(file)->importCmseSymbols(); 33506c3fb27SDimitry Andric } 33606c3fb27SDimitry Andric 33706c3fb27SDimitry Andric void elf::parseArmCMSEImportLib(InputFile *file) { 33806c3fb27SDimitry Andric invokeELFT(doParseArmCMSEImportLib, file); 33906c3fb27SDimitry Andric } 34006c3fb27SDimitry Andric 3410b57cec5SDimitry Andric // Concatenates arguments to construct a string representing an error location. 3420b57cec5SDimitry Andric static std::string createFileLineMsg(StringRef path, unsigned line) { 3435ffd83dbSDimitry Andric std::string filename = std::string(path::filename(path)); 3440b57cec5SDimitry Andric std::string lineno = ":" + std::to_string(line); 3450b57cec5SDimitry Andric if (filename == path) 3460b57cec5SDimitry Andric return filename + lineno; 3470b57cec5SDimitry Andric return filename + lineno + " (" + path.str() + lineno + ")"; 3480b57cec5SDimitry Andric } 3490b57cec5SDimitry Andric 3500b57cec5SDimitry Andric template <class ELFT> 3510b57cec5SDimitry Andric static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym, 352*5f757f3fSDimitry Andric const InputSectionBase &sec, uint64_t offset) { 3530b57cec5SDimitry Andric // In DWARF, functions and variables are stored to different places. 3540b57cec5SDimitry Andric // First, look up a function for a given offset. 355bdd1243dSDimitry Andric if (std::optional<DILineInfo> info = file.getDILineInfo(&sec, offset)) 3560b57cec5SDimitry Andric return createFileLineMsg(info->FileName, info->Line); 3570b57cec5SDimitry Andric 3580b57cec5SDimitry Andric // If it failed, look up again as a variable. 359bdd1243dSDimitry Andric if (std::optional<std::pair<std::string, unsigned>> fileLine = 3600b57cec5SDimitry Andric file.getVariableLoc(sym.getName())) 3610b57cec5SDimitry Andric return createFileLineMsg(fileLine->first, fileLine->second); 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric // File.sourceFile contains STT_FILE symbol, and that is a last resort. 3645ffd83dbSDimitry Andric return std::string(file.sourceFile); 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 367*5f757f3fSDimitry Andric std::string InputFile::getSrcMsg(const Symbol &sym, const InputSectionBase &sec, 3680b57cec5SDimitry Andric uint64_t offset) { 3690b57cec5SDimitry Andric if (kind() != ObjKind) 3700b57cec5SDimitry Andric return ""; 371bdd1243dSDimitry Andric switch (ekind) { 3720b57cec5SDimitry Andric default: 3730b57cec5SDimitry Andric llvm_unreachable("Invalid kind"); 3740b57cec5SDimitry Andric case ELF32LEKind: 3750b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset); 3760b57cec5SDimitry Andric case ELF32BEKind: 3770b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset); 3780b57cec5SDimitry Andric case ELF64LEKind: 3790b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset); 3800b57cec5SDimitry Andric case ELF64BEKind: 3810b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset); 3820b57cec5SDimitry Andric } 3830b57cec5SDimitry Andric } 3840b57cec5SDimitry Andric 385e8d8bef9SDimitry Andric StringRef InputFile::getNameForScript() const { 386e8d8bef9SDimitry Andric if (archiveName.empty()) 387e8d8bef9SDimitry Andric return getName(); 388e8d8bef9SDimitry Andric 389e8d8bef9SDimitry Andric if (nameForScriptCache.empty()) 390e8d8bef9SDimitry Andric nameForScriptCache = (archiveName + Twine(':') + getName()).str(); 391e8d8bef9SDimitry Andric 392e8d8bef9SDimitry Andric return nameForScriptCache; 393e8d8bef9SDimitry Andric } 394e8d8bef9SDimitry Andric 395bdd1243dSDimitry Andric // An ELF object file may contain a `.deplibs` section. If it exists, the 396bdd1243dSDimitry Andric // section contains a list of library specifiers such as `m` for libm. This 397bdd1243dSDimitry Andric // function resolves a given name by finding the first matching library checking 398bdd1243dSDimitry Andric // the various ways that a library can be specified to LLD. This ELF extension 399bdd1243dSDimitry Andric // is a form of autolinking and is called `dependent libraries`. It is currently 400bdd1243dSDimitry Andric // unique to LLVM and lld. 401bdd1243dSDimitry Andric static void addDependentLibrary(StringRef specifier, const InputFile *f) { 402bdd1243dSDimitry Andric if (!config->dependentLibraries) 403bdd1243dSDimitry Andric return; 404bdd1243dSDimitry Andric if (std::optional<std::string> s = searchLibraryBaseName(specifier)) 405bdd1243dSDimitry Andric ctx.driver.addFile(saver().save(*s), /*withLOption=*/true); 406bdd1243dSDimitry Andric else if (std::optional<std::string> s = findFromSearchPaths(specifier)) 407bdd1243dSDimitry Andric ctx.driver.addFile(saver().save(*s), /*withLOption=*/true); 408bdd1243dSDimitry Andric else if (fs::exists(specifier)) 409bdd1243dSDimitry Andric ctx.driver.addFile(specifier, /*withLOption=*/false); 410bdd1243dSDimitry Andric else 411bdd1243dSDimitry Andric error(toString(f) + 412bdd1243dSDimitry Andric ": unable to find library from dependent library specifier: " + 413bdd1243dSDimitry Andric specifier); 414bdd1243dSDimitry Andric } 415bdd1243dSDimitry Andric 416bdd1243dSDimitry Andric // Record the membership of a section group so that in the garbage collection 417bdd1243dSDimitry Andric // pass, section group members are kept or discarded as a unit. 418bdd1243dSDimitry Andric template <class ELFT> 419bdd1243dSDimitry Andric static void handleSectionGroup(ArrayRef<InputSectionBase *> sections, 420bdd1243dSDimitry Andric ArrayRef<typename ELFT::Word> entries) { 421bdd1243dSDimitry Andric bool hasAlloc = false; 422bdd1243dSDimitry Andric for (uint32_t index : entries.slice(1)) { 423bdd1243dSDimitry Andric if (index >= sections.size()) 424bdd1243dSDimitry Andric return; 425bdd1243dSDimitry Andric if (InputSectionBase *s = sections[index]) 426bdd1243dSDimitry Andric if (s != &InputSection::discarded && s->flags & SHF_ALLOC) 427bdd1243dSDimitry Andric hasAlloc = true; 428bdd1243dSDimitry Andric } 429bdd1243dSDimitry Andric 430bdd1243dSDimitry Andric // If any member has the SHF_ALLOC flag, the whole group is subject to garbage 431bdd1243dSDimitry Andric // collection. See the comment in markLive(). This rule retains .debug_types 432bdd1243dSDimitry Andric // and .rela.debug_types. 433bdd1243dSDimitry Andric if (!hasAlloc) 434bdd1243dSDimitry Andric return; 435bdd1243dSDimitry Andric 436bdd1243dSDimitry Andric // Connect the members in a circular doubly-linked list via 437bdd1243dSDimitry Andric // nextInSectionGroup. 438bdd1243dSDimitry Andric InputSectionBase *head; 439bdd1243dSDimitry Andric InputSectionBase *prev = nullptr; 440bdd1243dSDimitry Andric for (uint32_t index : entries.slice(1)) { 441bdd1243dSDimitry Andric InputSectionBase *s = sections[index]; 442bdd1243dSDimitry Andric if (!s || s == &InputSection::discarded) 443bdd1243dSDimitry Andric continue; 444bdd1243dSDimitry Andric if (prev) 445bdd1243dSDimitry Andric prev->nextInSectionGroup = s; 446bdd1243dSDimitry Andric else 447bdd1243dSDimitry Andric head = s; 448bdd1243dSDimitry Andric prev = s; 449bdd1243dSDimitry Andric } 450bdd1243dSDimitry Andric if (prev) 451bdd1243dSDimitry Andric prev->nextInSectionGroup = head; 452bdd1243dSDimitry Andric } 453bdd1243dSDimitry Andric 4545ffd83dbSDimitry Andric template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() { 4555ffd83dbSDimitry Andric llvm::call_once(initDwarf, [this]() { 4565ffd83dbSDimitry Andric dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>( 4575ffd83dbSDimitry Andric std::make_unique<LLDDwarfObj<ELFT>>(this), "", 4585ffd83dbSDimitry Andric [&](Error err) { warn(getName() + ": " + toString(std::move(err))); }, 4595ffd83dbSDimitry Andric [&](Error warning) { 4605ffd83dbSDimitry Andric warn(getName() + ": " + toString(std::move(warning))); 4615ffd83dbSDimitry Andric })); 4625ffd83dbSDimitry Andric }); 4635ffd83dbSDimitry Andric 4645ffd83dbSDimitry Andric return dwarf.get(); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric 4670b57cec5SDimitry Andric // Returns the pair of file name and line number describing location of data 4680b57cec5SDimitry Andric // object (variable, array, etc) definition. 4690b57cec5SDimitry Andric template <class ELFT> 470bdd1243dSDimitry Andric std::optional<std::pair<std::string, unsigned>> 4710b57cec5SDimitry Andric ObjFile<ELFT>::getVariableLoc(StringRef name) { 4725ffd83dbSDimitry Andric return getDwarf()->getVariableLoc(name); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric // Returns source line information for a given offset 4760b57cec5SDimitry Andric // using DWARF debug info. 4770b57cec5SDimitry Andric template <class ELFT> 478*5f757f3fSDimitry Andric std::optional<DILineInfo> 479*5f757f3fSDimitry Andric ObjFile<ELFT>::getDILineInfo(const InputSectionBase *s, uint64_t offset) { 4800b57cec5SDimitry Andric // Detect SectionIndex for specified section. 4810b57cec5SDimitry Andric uint64_t sectionIndex = object::SectionedAddress::UndefSection; 4820b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = s->file->getSections(); 4830b57cec5SDimitry Andric for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) { 4840b57cec5SDimitry Andric if (s == sections[curIndex]) { 4850b57cec5SDimitry Andric sectionIndex = curIndex; 4860b57cec5SDimitry Andric break; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric } 4890b57cec5SDimitry Andric 4905ffd83dbSDimitry Andric return getDwarf()->getDILineInfo(offset, sectionIndex); 4910b57cec5SDimitry Andric } 4920b57cec5SDimitry Andric 493bdd1243dSDimitry Andric ELFFileBase::ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef mb) 494bdd1243dSDimitry Andric : InputFile(k, mb) { 495bdd1243dSDimitry Andric this->ekind = ekind; 4960b57cec5SDimitry Andric } 4970b57cec5SDimitry Andric 4980b57cec5SDimitry Andric template <typename Elf_Shdr> 4990b57cec5SDimitry Andric static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) { 5000b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) 5010b57cec5SDimitry Andric if (sec.sh_type == type) 5020b57cec5SDimitry Andric return &sec; 5030b57cec5SDimitry Andric return nullptr; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 506bdd1243dSDimitry Andric void ELFFileBase::init() { 507bdd1243dSDimitry Andric switch (ekind) { 508bdd1243dSDimitry Andric case ELF32LEKind: 509bdd1243dSDimitry Andric init<ELF32LE>(fileKind); 510bdd1243dSDimitry Andric break; 511bdd1243dSDimitry Andric case ELF32BEKind: 512bdd1243dSDimitry Andric init<ELF32BE>(fileKind); 513bdd1243dSDimitry Andric break; 514bdd1243dSDimitry Andric case ELF64LEKind: 515bdd1243dSDimitry Andric init<ELF64LE>(fileKind); 516bdd1243dSDimitry Andric break; 517bdd1243dSDimitry Andric case ELF64BEKind: 518bdd1243dSDimitry Andric init<ELF64BE>(fileKind); 519bdd1243dSDimitry Andric break; 520bdd1243dSDimitry Andric default: 521bdd1243dSDimitry Andric llvm_unreachable("getELFKind"); 522bdd1243dSDimitry Andric } 523bdd1243dSDimitry Andric } 524bdd1243dSDimitry Andric 525bdd1243dSDimitry Andric template <class ELFT> void ELFFileBase::init(InputFile::Kind k) { 5260b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 5270b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric // Initialize trivial attributes. 5300b57cec5SDimitry Andric const ELFFile<ELFT> &obj = getObj<ELFT>(); 531e8d8bef9SDimitry Andric emachine = obj.getHeader().e_machine; 532e8d8bef9SDimitry Andric osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI]; 533e8d8bef9SDimitry Andric abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION]; 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this); 5360eae32dcSDimitry Andric elfShdrs = sections.data(); 5370eae32dcSDimitry Andric numELFShdrs = sections.size(); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric // Find a symbol table. 5400b57cec5SDimitry Andric const Elf_Shdr *symtabSec = 541bdd1243dSDimitry Andric findSection(sections, k == SharedKind ? SHT_DYNSYM : SHT_SYMTAB); 5420b57cec5SDimitry Andric 5430b57cec5SDimitry Andric if (!symtabSec) 5440b57cec5SDimitry Andric return; 5450b57cec5SDimitry Andric 5460b57cec5SDimitry Andric // Initialize members corresponding to a symbol table. 5470b57cec5SDimitry Andric firstGlobal = symtabSec->sh_info; 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this); 5500b57cec5SDimitry Andric if (firstGlobal == 0 || firstGlobal > eSyms.size()) 5510b57cec5SDimitry Andric fatal(toString(this) + ": invalid sh_info in symbol table"); 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric elfSyms = reinterpret_cast<const void *>(eSyms.data()); 5540eae32dcSDimitry Andric numELFSyms = uint32_t(eSyms.size()); 5550b57cec5SDimitry Andric stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this); 5560b57cec5SDimitry Andric } 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric template <class ELFT> 5590b57cec5SDimitry Andric uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const { 5600b57cec5SDimitry Andric return CHECK( 561e8d8bef9SDimitry Andric this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable), 5620b57cec5SDimitry Andric this); 5630b57cec5SDimitry Andric } 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) { 5661fd87a68SDimitry Andric object::ELFFile<ELFT> obj = this->getObj(); 5670b57cec5SDimitry Andric // Read a section table. justSymbols is usually false. 568bdd1243dSDimitry Andric if (this->justSymbols) { 5690b57cec5SDimitry Andric initializeJustSymbols(); 570bdd1243dSDimitry Andric initializeSymbols(obj); 571bdd1243dSDimitry Andric return; 572bdd1243dSDimitry Andric } 573bdd1243dSDimitry Andric 574bdd1243dSDimitry Andric // Handle dependent libraries and selection of section groups as these are not 575bdd1243dSDimitry Andric // done in parallel. 576bdd1243dSDimitry Andric ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>(); 577bdd1243dSDimitry Andric StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this); 578bdd1243dSDimitry Andric uint64_t size = objSections.size(); 579bdd1243dSDimitry Andric sections.resize(size); 580bdd1243dSDimitry Andric for (size_t i = 0; i != size; ++i) { 581bdd1243dSDimitry Andric const Elf_Shdr &sec = objSections[i]; 582bdd1243dSDimitry Andric if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) { 583bdd1243dSDimitry Andric StringRef name = check(obj.getSectionName(sec, shstrtab)); 584bdd1243dSDimitry Andric ArrayRef<char> data = CHECK( 585bdd1243dSDimitry Andric this->getObj().template getSectionContentsAsArray<char>(sec), this); 586bdd1243dSDimitry Andric if (!data.empty() && data.back() != '\0') { 587bdd1243dSDimitry Andric error( 588bdd1243dSDimitry Andric toString(this) + 589bdd1243dSDimitry Andric ": corrupted dependent libraries section (unterminated string): " + 590bdd1243dSDimitry Andric name); 591bdd1243dSDimitry Andric } else { 592bdd1243dSDimitry Andric for (const char *d = data.begin(), *e = data.end(); d < e;) { 593bdd1243dSDimitry Andric StringRef s(d); 594bdd1243dSDimitry Andric addDependentLibrary(s, this); 595bdd1243dSDimitry Andric d += s.size() + 1; 596bdd1243dSDimitry Andric } 597bdd1243dSDimitry Andric } 598bdd1243dSDimitry Andric this->sections[i] = &InputSection::discarded; 599bdd1243dSDimitry Andric continue; 600bdd1243dSDimitry Andric } 601bdd1243dSDimitry Andric 602bdd1243dSDimitry Andric if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) { 603bdd1243dSDimitry Andric ARMAttributeParser attributes; 604bdd1243dSDimitry Andric ArrayRef<uint8_t> contents = 605bdd1243dSDimitry Andric check(this->getObj().getSectionContents(sec)); 606bdd1243dSDimitry Andric StringRef name = check(obj.getSectionName(sec, shstrtab)); 607bdd1243dSDimitry Andric this->sections[i] = &InputSection::discarded; 608*5f757f3fSDimitry Andric if (Error e = attributes.parse(contents, ekind == ELF32LEKind 609*5f757f3fSDimitry Andric ? llvm::endianness::little 610*5f757f3fSDimitry Andric : llvm::endianness::big)) { 611bdd1243dSDimitry Andric InputSection isec(*this, sec, name); 612bdd1243dSDimitry Andric warn(toString(&isec) + ": " + llvm::toString(std::move(e))); 613bdd1243dSDimitry Andric } else { 614bdd1243dSDimitry Andric updateSupportedARMFeatures(attributes); 615bdd1243dSDimitry Andric updateARMVFPArgs(attributes, this); 616bdd1243dSDimitry Andric 617bdd1243dSDimitry Andric // FIXME: Retain the first attribute section we see. The eglibc ARM 618bdd1243dSDimitry Andric // dynamic loaders require the presence of an attribute section for 619bdd1243dSDimitry Andric // dlopen to work. In a full implementation we would merge all attribute 620bdd1243dSDimitry Andric // sections. 621bdd1243dSDimitry Andric if (in.attributes == nullptr) { 622bdd1243dSDimitry Andric in.attributes = std::make_unique<InputSection>(*this, sec, name); 623bdd1243dSDimitry Andric this->sections[i] = in.attributes.get(); 624bdd1243dSDimitry Andric } 625bdd1243dSDimitry Andric } 626bdd1243dSDimitry Andric } 627bdd1243dSDimitry Andric 628*5f757f3fSDimitry Andric // Producing a static binary with MTE globals is not currently supported, 629*5f757f3fSDimitry Andric // remove all SHT_AARCH64_MEMTAG_GLOBALS_STATIC sections as they're unused 630*5f757f3fSDimitry Andric // medatada, and we don't want them to end up in the output file for static 631*5f757f3fSDimitry Andric // executables. 632*5f757f3fSDimitry Andric if (sec.sh_type == SHT_AARCH64_MEMTAG_GLOBALS_STATIC && 633*5f757f3fSDimitry Andric !canHaveMemtagGlobals()) { 634*5f757f3fSDimitry Andric this->sections[i] = &InputSection::discarded; 635*5f757f3fSDimitry Andric continue; 636*5f757f3fSDimitry Andric } 637*5f757f3fSDimitry Andric 638bdd1243dSDimitry Andric if (sec.sh_type != SHT_GROUP) 639bdd1243dSDimitry Andric continue; 640bdd1243dSDimitry Andric StringRef signature = getShtGroupSignature(objSections, sec); 641bdd1243dSDimitry Andric ArrayRef<Elf_Word> entries = 642bdd1243dSDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this); 643bdd1243dSDimitry Andric if (entries.empty()) 644bdd1243dSDimitry Andric fatal(toString(this) + ": empty SHT_GROUP"); 645bdd1243dSDimitry Andric 646bdd1243dSDimitry Andric Elf_Word flag = entries[0]; 647bdd1243dSDimitry Andric if (flag && flag != GRP_COMDAT) 648bdd1243dSDimitry Andric fatal(toString(this) + ": unsupported SHT_GROUP format"); 649bdd1243dSDimitry Andric 650bdd1243dSDimitry Andric bool keepGroup = 651bdd1243dSDimitry Andric (flag & GRP_COMDAT) == 0 || ignoreComdats || 652bdd1243dSDimitry Andric symtab.comdatGroups.try_emplace(CachedHashStringRef(signature), this) 653bdd1243dSDimitry Andric .second; 654bdd1243dSDimitry Andric if (keepGroup) { 655bdd1243dSDimitry Andric if (config->relocatable) 656bdd1243dSDimitry Andric this->sections[i] = createInputSection( 657bdd1243dSDimitry Andric i, sec, check(obj.getSectionName(sec, shstrtab))); 658bdd1243dSDimitry Andric continue; 659bdd1243dSDimitry Andric } 660bdd1243dSDimitry Andric 661bdd1243dSDimitry Andric // Otherwise, discard group members. 662bdd1243dSDimitry Andric for (uint32_t secIndex : entries.slice(1)) { 663bdd1243dSDimitry Andric if (secIndex >= size) 664bdd1243dSDimitry Andric fatal(toString(this) + 665bdd1243dSDimitry Andric ": invalid section index in group: " + Twine(secIndex)); 666bdd1243dSDimitry Andric this->sections[secIndex] = &InputSection::discarded; 667bdd1243dSDimitry Andric } 668bdd1243dSDimitry Andric } 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric // Read a symbol table. 6711fd87a68SDimitry Andric initializeSymbols(obj); 6720b57cec5SDimitry Andric } 6730b57cec5SDimitry Andric 6740b57cec5SDimitry Andric // Sections with SHT_GROUP and comdat bits define comdat section groups. 6750b57cec5SDimitry Andric // They are identified and deduplicated by group name. This function 6760b57cec5SDimitry Andric // returns a group name. 6770b57cec5SDimitry Andric template <class ELFT> 6780b57cec5SDimitry Andric StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections, 6790b57cec5SDimitry Andric const Elf_Shdr &sec) { 6800b57cec5SDimitry Andric typename ELFT::SymRange symbols = this->getELFSyms<ELFT>(); 6810b57cec5SDimitry Andric if (sec.sh_info >= symbols.size()) 6820b57cec5SDimitry Andric fatal(toString(this) + ": invalid symbol index"); 6830b57cec5SDimitry Andric const typename ELFT::Sym &sym = symbols[sec.sh_info]; 684349cc55cSDimitry Andric return CHECK(sym.getName(this->stringTable), this); 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 68785868e8aSDimitry Andric template <class ELFT> 68885868e8aSDimitry Andric bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) { 6890b57cec5SDimitry Andric // On a regular link we don't merge sections if -O0 (default is -O1). This 6900b57cec5SDimitry Andric // sometimes makes the linker significantly faster, although the output will 6910b57cec5SDimitry Andric // be bigger. 6920b57cec5SDimitry Andric // 6930b57cec5SDimitry Andric // Doing the same for -r would create a problem as it would combine sections 6940b57cec5SDimitry Andric // with different sh_entsize. One option would be to just copy every SHF_MERGE 6950b57cec5SDimitry Andric // section as is to the output. While this would produce a valid ELF file with 6960b57cec5SDimitry Andric // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 6970b57cec5SDimitry Andric // they see two .debug_str. We could have separate logic for combining 6980b57cec5SDimitry Andric // SHF_MERGE sections based both on their name and sh_entsize, but that seems 6990b57cec5SDimitry Andric // to be more trouble than it is worth. Instead, we just use the regular (-O1) 7000b57cec5SDimitry Andric // logic for -r. 7010b57cec5SDimitry Andric if (config->optimize == 0 && !config->relocatable) 7020b57cec5SDimitry Andric return false; 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric // A mergeable section with size 0 is useless because they don't have 7050b57cec5SDimitry Andric // any data to merge. A mergeable string section with size 0 can be 7060b57cec5SDimitry Andric // argued as invalid because it doesn't end with a null character. 7070b57cec5SDimitry Andric // We'll avoid a mess by handling them as if they were non-mergeable. 7080b57cec5SDimitry Andric if (sec.sh_size == 0) 7090b57cec5SDimitry Andric return false; 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric // Check for sh_entsize. The ELF spec is not clear about the zero 7120b57cec5SDimitry Andric // sh_entsize. It says that "the member [sh_entsize] contains 0 if 7130b57cec5SDimitry Andric // the section does not hold a table of fixed-size entries". We know 7140b57cec5SDimitry Andric // that Rust 1.13 produces a string mergeable section with a zero 7150b57cec5SDimitry Andric // sh_entsize. Here we just accept it rather than being picky about it. 7160b57cec5SDimitry Andric uint64_t entSize = sec.sh_entsize; 7170b57cec5SDimitry Andric if (entSize == 0) 7180b57cec5SDimitry Andric return false; 7190b57cec5SDimitry Andric if (sec.sh_size % entSize) 72085868e8aSDimitry Andric fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" + 72185868e8aSDimitry Andric Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" + 72285868e8aSDimitry Andric Twine(entSize) + ")"); 7230b57cec5SDimitry Andric 7245ffd83dbSDimitry Andric if (sec.sh_flags & SHF_WRITE) 72585868e8aSDimitry Andric fatal(toString(this) + ":(" + name + 72685868e8aSDimitry Andric "): writable SHF_MERGE section is not supported"); 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric return true; 7290b57cec5SDimitry Andric } 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric // This is for --just-symbols. 7320b57cec5SDimitry Andric // 7330b57cec5SDimitry Andric // --just-symbols is a very minor feature that allows you to link your 7340b57cec5SDimitry Andric // output against other existing program, so that if you load both your 7350b57cec5SDimitry Andric // program and the other program into memory, your output can refer the 7360b57cec5SDimitry Andric // other program's symbols. 7370b57cec5SDimitry Andric // 7380b57cec5SDimitry Andric // When the option is given, we link "just symbols". The section table is 7390b57cec5SDimitry Andric // initialized with null pointers. 7400b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 7410eae32dcSDimitry Andric sections.resize(numELFShdrs); 7420b57cec5SDimitry Andric } 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric template <class ELFT> 7451fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSections(bool ignoreComdats, 7461fd87a68SDimitry Andric const llvm::object::ELFFile<ELFT> &obj) { 7470eae32dcSDimitry Andric ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>(); 748349cc55cSDimitry Andric StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this); 7490b57cec5SDimitry Andric uint64_t size = objSections.size(); 750bdd1243dSDimitry Andric SmallVector<ArrayRef<Elf_Word>, 0> selectedGroups; 75104eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 7520b57cec5SDimitry Andric if (this->sections[i] == &InputSection::discarded) 7530b57cec5SDimitry Andric continue; 7540b57cec5SDimitry Andric const Elf_Shdr &sec = objSections[i]; 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric // SHF_EXCLUDE'ed sections are discarded by the linker. However, 7570b57cec5SDimitry Andric // if -r is given, we'll let the final link discard such sections. 7580b57cec5SDimitry Andric // This is compatible with GNU. 7590b57cec5SDimitry Andric if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) { 7600eae32dcSDimitry Andric if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE) 7610eae32dcSDimitry Andric cgProfileSectionIndex = i; 7620b57cec5SDimitry Andric if (sec.sh_type == SHT_LLVM_ADDRSIG) { 7630b57cec5SDimitry Andric // We ignore the address-significance table if we know that the object 7640b57cec5SDimitry Andric // file was created by objcopy or ld -r. This is because these tools 7650b57cec5SDimitry Andric // will reorder the symbols in the symbol table, invalidating the data 7660b57cec5SDimitry Andric // in the address-significance table, which refers to symbols by index. 7670b57cec5SDimitry Andric if (sec.sh_link != 0) 7680b57cec5SDimitry Andric this->addrsigSec = &sec; 7690b57cec5SDimitry Andric else if (config->icf == ICFLevel::Safe) 770fe6060f1SDimitry Andric warn(toString(this) + 771fe6060f1SDimitry Andric ": --icf=safe conservatively ignores " 772fe6060f1SDimitry Andric "SHT_LLVM_ADDRSIG [index " + 773fe6060f1SDimitry Andric Twine(i) + 774fe6060f1SDimitry Andric "] with sh_link=0 " 775fe6060f1SDimitry Andric "(likely created using objcopy or ld -r)"); 7760b57cec5SDimitry Andric } 7770b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 7780b57cec5SDimitry Andric continue; 7790b57cec5SDimitry Andric } 7800b57cec5SDimitry Andric 7810b57cec5SDimitry Andric switch (sec.sh_type) { 7820b57cec5SDimitry Andric case SHT_GROUP: { 783bdd1243dSDimitry Andric if (!config->relocatable) 784bdd1243dSDimitry Andric sections[i] = &InputSection::discarded; 785bdd1243dSDimitry Andric StringRef signature = 786bdd1243dSDimitry Andric cantFail(this->getELFSyms<ELFT>()[sec.sh_info].getName(stringTable)); 7870b57cec5SDimitry Andric ArrayRef<Elf_Word> entries = 788bdd1243dSDimitry Andric cantFail(obj.template getSectionContentsAsArray<Elf_Word>(sec)); 789bdd1243dSDimitry Andric if ((entries[0] & GRP_COMDAT) == 0 || ignoreComdats || 790bdd1243dSDimitry Andric symtab.comdatGroups.find(CachedHashStringRef(signature))->second == 791bdd1243dSDimitry Andric this) 792480093f4SDimitry Andric selectedGroups.push_back(entries); 7930b57cec5SDimitry Andric break; 7940b57cec5SDimitry Andric } 7950b57cec5SDimitry Andric case SHT_SYMTAB_SHNDX: 7960b57cec5SDimitry Andric shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this); 7970b57cec5SDimitry Andric break; 7980b57cec5SDimitry Andric case SHT_SYMTAB: 7990b57cec5SDimitry Andric case SHT_STRTAB: 8005ffd83dbSDimitry Andric case SHT_REL: 8015ffd83dbSDimitry Andric case SHT_RELA: 8020b57cec5SDimitry Andric case SHT_NULL: 8030b57cec5SDimitry Andric break; 80481ad6265SDimitry Andric case SHT_LLVM_SYMPART: 805bdd1243dSDimitry Andric ctx.hasSympart.store(true, std::memory_order_relaxed); 806bdd1243dSDimitry Andric [[fallthrough]]; 8070b57cec5SDimitry Andric default: 8081fd87a68SDimitry Andric this->sections[i] = 8091fd87a68SDimitry Andric createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab))); 8100b57cec5SDimitry Andric } 81185868e8aSDimitry Andric } 81285868e8aSDimitry Andric 8135ffd83dbSDimitry Andric // We have a second loop. It is used to: 8145ffd83dbSDimitry Andric // 1) handle SHF_LINK_ORDER sections. 8155ffd83dbSDimitry Andric // 2) create SHT_REL[A] sections. In some cases the section header index of a 8165ffd83dbSDimitry Andric // relocation section may be smaller than that of the relocated section. In 8175ffd83dbSDimitry Andric // such cases, the relocation section would attempt to reference a target 8185ffd83dbSDimitry Andric // section that has not yet been created. For simplicity, delay creation of 8195ffd83dbSDimitry Andric // relocation sections until now. 82004eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 82185868e8aSDimitry Andric if (this->sections[i] == &InputSection::discarded) 82285868e8aSDimitry Andric continue; 82385868e8aSDimitry Andric const Elf_Shdr &sec = objSections[i]; 8245ffd83dbSDimitry Andric 82504eeddc0SDimitry Andric if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) { 82604eeddc0SDimitry Andric // Find a relocation target section and associate this section with that. 82704eeddc0SDimitry Andric // Target may have been discarded if it is in a different section group 82804eeddc0SDimitry Andric // and the group is discarded, even though it's a violation of the spec. 82904eeddc0SDimitry Andric // We handle that situation gracefully by discarding dangling relocation 83004eeddc0SDimitry Andric // sections. 83104eeddc0SDimitry Andric const uint32_t info = sec.sh_info; 83204eeddc0SDimitry Andric InputSectionBase *s = getRelocTarget(i, sec, info); 83304eeddc0SDimitry Andric if (!s) 83404eeddc0SDimitry Andric continue; 83504eeddc0SDimitry Andric 83604eeddc0SDimitry Andric // ELF spec allows mergeable sections with relocations, but they are rare, 83704eeddc0SDimitry Andric // and it is in practice hard to merge such sections by contents, because 83804eeddc0SDimitry Andric // applying relocations at end of linking changes section contents. So, we 83904eeddc0SDimitry Andric // simply handle such sections as non-mergeable ones. Degrading like this 84004eeddc0SDimitry Andric // is acceptable because section merging is optional. 84104eeddc0SDimitry Andric if (auto *ms = dyn_cast<MergeInputSection>(s)) { 842bdd1243dSDimitry Andric s = makeThreadLocal<InputSection>( 843bdd1243dSDimitry Andric ms->file, ms->flags, ms->type, ms->addralign, 844bdd1243dSDimitry Andric ms->contentMaybeDecompress(), ms->name); 84504eeddc0SDimitry Andric sections[info] = s; 84604eeddc0SDimitry Andric } 84704eeddc0SDimitry Andric 84804eeddc0SDimitry Andric if (s->relSecIdx != 0) 84904eeddc0SDimitry Andric error( 85004eeddc0SDimitry Andric toString(s) + 85104eeddc0SDimitry Andric ": multiple relocation sections to one section are not supported"); 85204eeddc0SDimitry Andric s->relSecIdx = i; 85304eeddc0SDimitry Andric 85404eeddc0SDimitry Andric // Relocation sections are usually removed from the output, so return 85504eeddc0SDimitry Andric // `nullptr` for the normal case. However, if -r or --emit-relocs is 85604eeddc0SDimitry Andric // specified, we need to copy them to the output. (Some post link analysis 85704eeddc0SDimitry Andric // tools specify --emit-relocs to obtain the information.) 85804eeddc0SDimitry Andric if (config->copyRelocs) { 859bdd1243dSDimitry Andric auto *isec = makeThreadLocal<InputSection>( 86004eeddc0SDimitry Andric *this, sec, check(obj.getSectionName(sec, shstrtab))); 86104eeddc0SDimitry Andric // If the relocated section is discarded (due to /DISCARD/ or 86204eeddc0SDimitry Andric // --gc-sections), the relocation section should be discarded as well. 86304eeddc0SDimitry Andric s->dependentSections.push_back(isec); 86404eeddc0SDimitry Andric sections[i] = isec; 86504eeddc0SDimitry Andric } 86604eeddc0SDimitry Andric continue; 86704eeddc0SDimitry Andric } 8685ffd83dbSDimitry Andric 869e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have 870e8d8bef9SDimitry Andric // the flag. 87104eeddc0SDimitry Andric if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER)) 87285868e8aSDimitry Andric continue; 8730b57cec5SDimitry Andric 8740b57cec5SDimitry Andric InputSectionBase *linkSec = nullptr; 87504eeddc0SDimitry Andric if (sec.sh_link < size) 8760b57cec5SDimitry Andric linkSec = this->sections[sec.sh_link]; 8770b57cec5SDimitry Andric if (!linkSec) 87885868e8aSDimitry Andric fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link)); 8790b57cec5SDimitry Andric 880e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section is discarded if its linked-to section is 881e8d8bef9SDimitry Andric // discarded. 8820b57cec5SDimitry Andric InputSection *isec = cast<InputSection>(this->sections[i]); 8830b57cec5SDimitry Andric linkSec->dependentSections.push_back(isec); 8840b57cec5SDimitry Andric if (!isa<InputSection>(linkSec)) 8850b57cec5SDimitry Andric error("a section " + isec->name + 88685868e8aSDimitry Andric " with SHF_LINK_ORDER should not refer a non-regular section: " + 8870b57cec5SDimitry Andric toString(linkSec)); 8880b57cec5SDimitry Andric } 889480093f4SDimitry Andric 890480093f4SDimitry Andric for (ArrayRef<Elf_Word> entries : selectedGroups) 891480093f4SDimitry Andric handleSectionGroup<ELFT>(this->sections, entries); 8920b57cec5SDimitry Andric } 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric // If a source file is compiled with x86 hardware-assisted call flow control 8950b57cec5SDimitry Andric // enabled, the generated object file contains feature flags indicating that 8960b57cec5SDimitry Andric // fact. This function reads the feature flags and returns it. 8970b57cec5SDimitry Andric // 8980b57cec5SDimitry Andric // Essentially we want to read a single 32-bit value in this function, but this 8990b57cec5SDimitry Andric // function is rather complicated because the value is buried deep inside a 9000b57cec5SDimitry Andric // .note.gnu.property section. 9010b57cec5SDimitry Andric // 9020b57cec5SDimitry Andric // The section consists of one or more NOTE records. Each NOTE record consists 9030b57cec5SDimitry Andric // of zero or more type-length-value fields. We want to find a field of a 9040b57cec5SDimitry Andric // certain type. It seems a bit too much to just store a 32-bit value, perhaps 9050b57cec5SDimitry Andric // the ABI is unnecessarily complicated. 906e8d8bef9SDimitry Andric template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { 9070b57cec5SDimitry Andric using Elf_Nhdr = typename ELFT::Nhdr; 9080b57cec5SDimitry Andric using Elf_Note = typename ELFT::Note; 9090b57cec5SDimitry Andric 9100b57cec5SDimitry Andric uint32_t featuresSet = 0; 911bdd1243dSDimitry Andric ArrayRef<uint8_t> data = sec.content(); 912e8d8bef9SDimitry Andric auto reportFatal = [&](const uint8_t *place, const char *msg) { 913e8d8bef9SDimitry Andric fatal(toString(sec.file) + ":(" + sec.name + "+0x" + 914bdd1243dSDimitry Andric Twine::utohexstr(place - sec.content().data()) + "): " + msg); 915e8d8bef9SDimitry Andric }; 9160b57cec5SDimitry Andric while (!data.empty()) { 9170b57cec5SDimitry Andric // Read one NOTE record. 9180b57cec5SDimitry Andric auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data()); 91906c3fb27SDimitry Andric if (data.size() < sizeof(Elf_Nhdr) || 92006c3fb27SDimitry Andric data.size() < nhdr->getSize(sec.addralign)) 921e8d8bef9SDimitry Andric reportFatal(data.data(), "data is too short"); 9220b57cec5SDimitry Andric 9230b57cec5SDimitry Andric Elf_Note note(*nhdr); 9240b57cec5SDimitry Andric if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") { 92506c3fb27SDimitry Andric data = data.slice(nhdr->getSize(sec.addralign)); 9260b57cec5SDimitry Andric continue; 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 9300b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 9310b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 9320b57cec5SDimitry Andric 9330b57cec5SDimitry Andric // Read a body of a NOTE record, which consists of type-length-value fields. 93406c3fb27SDimitry Andric ArrayRef<uint8_t> desc = note.getDesc(sec.addralign); 9350b57cec5SDimitry Andric while (!desc.empty()) { 936e8d8bef9SDimitry Andric const uint8_t *place = desc.data(); 9370b57cec5SDimitry Andric if (desc.size() < 8) 938e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 939e8d8bef9SDimitry Andric uint32_t type = read32<ELFT::TargetEndianness>(desc.data()); 940e8d8bef9SDimitry Andric uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4); 941e8d8bef9SDimitry Andric desc = desc.slice(8); 942e8d8bef9SDimitry Andric if (desc.size() < size) 943e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 9440b57cec5SDimitry Andric 9450b57cec5SDimitry Andric if (type == featureAndType) { 9460b57cec5SDimitry Andric // We found a FEATURE_1_AND field. There may be more than one of these 947480093f4SDimitry Andric // in a .note.gnu.property section, for a relocatable object we 9480b57cec5SDimitry Andric // accumulate the bits set. 949e8d8bef9SDimitry Andric if (size < 4) 950e8d8bef9SDimitry Andric reportFatal(place, "FEATURE_1_AND entry is too short"); 951e8d8bef9SDimitry Andric featuresSet |= read32<ELFT::TargetEndianness>(desc.data()); 9520b57cec5SDimitry Andric } 9530b57cec5SDimitry Andric 954e8d8bef9SDimitry Andric // Padding is present in the note descriptor, if necessary. 955e8d8bef9SDimitry Andric desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size)); 9560b57cec5SDimitry Andric } 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric // Go to next NOTE record to look for more FEATURE_1_AND descriptions. 95906c3fb27SDimitry Andric data = data.slice(nhdr->getSize(sec.addralign)); 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric return featuresSet; 9630b57cec5SDimitry Andric } 9640b57cec5SDimitry Andric 9650b57cec5SDimitry Andric template <class ELFT> 96604eeddc0SDimitry Andric InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, 96704eeddc0SDimitry Andric const Elf_Shdr &sec, 96804eeddc0SDimitry Andric uint32_t info) { 969349cc55cSDimitry Andric if (info < this->sections.size()) { 970349cc55cSDimitry Andric InputSectionBase *target = this->sections[info]; 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric // Strictly speaking, a relocation section must be included in the 9730b57cec5SDimitry Andric // group of the section it relocates. However, LLVM 3.3 and earlier 9740b57cec5SDimitry Andric // would fail to do so, so we gracefully handle that case. 9750b57cec5SDimitry Andric if (target == &InputSection::discarded) 9760b57cec5SDimitry Andric return nullptr; 9770b57cec5SDimitry Andric 978349cc55cSDimitry Andric if (target != nullptr) 9790b57cec5SDimitry Andric return target; 9800b57cec5SDimitry Andric } 9810b57cec5SDimitry Andric 98204eeddc0SDimitry Andric error(toString(this) + Twine(": relocation section (index ") + Twine(idx) + 98304eeddc0SDimitry Andric ") has invalid sh_info (" + Twine(info) + ")"); 984349cc55cSDimitry Andric return nullptr; 985349cc55cSDimitry Andric } 986349cc55cSDimitry Andric 987bdd1243dSDimitry Andric // The function may be called concurrently for different input files. For 988bdd1243dSDimitry Andric // allocation, prefer makeThreadLocal which does not require holding a lock. 9890b57cec5SDimitry Andric template <class ELFT> 990349cc55cSDimitry Andric InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx, 991349cc55cSDimitry Andric const Elf_Shdr &sec, 9921fd87a68SDimitry Andric StringRef name) { 99306c3fb27SDimitry Andric if (name.starts_with(".n")) { 9940b57cec5SDimitry Andric // The GNU linker uses .note.GNU-stack section as a marker indicating 9950b57cec5SDimitry Andric // that the code in the object file does not expect that the stack is 9960b57cec5SDimitry Andric // executable (in terms of NX bit). If all input files have the marker, 9970b57cec5SDimitry Andric // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 9980b57cec5SDimitry Andric // make the stack non-executable. Most object files have this section as 9990b57cec5SDimitry Andric // of 2017. 10000b57cec5SDimitry Andric // 10010b57cec5SDimitry Andric // But making the stack non-executable is a norm today for security 10020b57cec5SDimitry Andric // reasons. Failure to do so may result in a serious security issue. 10030b57cec5SDimitry Andric // Therefore, we make LLD always add PT_GNU_STACK unless it is 10040b57cec5SDimitry Andric // explicitly told to do otherwise (by -z execstack). Because the stack 10050b57cec5SDimitry Andric // executable-ness is controlled solely by command line options, 10060b57cec5SDimitry Andric // .note.GNU-stack sections are simply ignored. 10070b57cec5SDimitry Andric if (name == ".note.GNU-stack") 10080b57cec5SDimitry Andric return &InputSection::discarded; 10090b57cec5SDimitry Andric 10100b57cec5SDimitry Andric // Object files that use processor features such as Intel Control-Flow 10110b57cec5SDimitry Andric // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a 10120b57cec5SDimitry Andric // .note.gnu.property section containing a bitfield of feature bits like the 10130b57cec5SDimitry Andric // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag. 10140b57cec5SDimitry Andric // 10150b57cec5SDimitry Andric // Since we merge bitmaps from multiple object files to create a new 10160b57cec5SDimitry Andric // .note.gnu.property containing a single AND'ed bitmap, we discard an input 10170b57cec5SDimitry Andric // file's .note.gnu.property section. 10180b57cec5SDimitry Andric if (name == ".note.gnu.property") { 1019e8d8bef9SDimitry Andric this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name)); 10200b57cec5SDimitry Andric return &InputSection::discarded; 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 10230b57cec5SDimitry Andric // Split stacks is a feature to support a discontiguous stack, 10240b57cec5SDimitry Andric // commonly used in the programming language Go. For the details, 10250b57cec5SDimitry Andric // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled 10260b57cec5SDimitry Andric // for split stack will include a .note.GNU-split-stack section. 10270b57cec5SDimitry Andric if (name == ".note.GNU-split-stack") { 10280b57cec5SDimitry Andric if (config->relocatable) { 10290eae32dcSDimitry Andric error( 10300eae32dcSDimitry Andric "cannot mix split-stack and non-split-stack in a relocatable link"); 10310b57cec5SDimitry Andric return &InputSection::discarded; 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric this->splitStack = true; 10340b57cec5SDimitry Andric return &InputSection::discarded; 10350b57cec5SDimitry Andric } 10360b57cec5SDimitry Andric 1037bdd1243dSDimitry Andric // An object file compiled for split stack, but where some of the 10380b57cec5SDimitry Andric // functions were compiled with the no_split_stack_attribute will 10390b57cec5SDimitry Andric // include a .note.GNU-no-split-stack section. 10400b57cec5SDimitry Andric if (name == ".note.GNU-no-split-stack") { 10410b57cec5SDimitry Andric this->someNoSplitStack = true; 10420b57cec5SDimitry Andric return &InputSection::discarded; 10430b57cec5SDimitry Andric } 10440b57cec5SDimitry Andric 10450eae32dcSDimitry Andric // Strip existing .note.gnu.build-id sections so that the output won't have 10460eae32dcSDimitry Andric // more than one build-id. This is not usually a problem because input 10470eae32dcSDimitry Andric // object files normally don't have .build-id sections, but you can create 10480eae32dcSDimitry Andric // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard 10490eae32dcSDimitry Andric // against it. 10500eae32dcSDimitry Andric if (name == ".note.gnu.build-id") 10510eae32dcSDimitry Andric return &InputSection::discarded; 10520eae32dcSDimitry Andric } 10530eae32dcSDimitry Andric 10540b57cec5SDimitry Andric // The linker merges EH (exception handling) frames and creates a 10550b57cec5SDimitry Andric // .eh_frame_hdr section for runtime. So we handle them with a special 10560b57cec5SDimitry Andric // class. For relocatable outputs, they are just passed through. 10570b57cec5SDimitry Andric if (name == ".eh_frame" && !config->relocatable) 1058bdd1243dSDimitry Andric return makeThreadLocal<EhInputSection>(*this, sec, name); 10590b57cec5SDimitry Andric 10600eae32dcSDimitry Andric if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name)) 1061bdd1243dSDimitry Andric return makeThreadLocal<MergeInputSection>(*this, sec, name); 1062bdd1243dSDimitry Andric return makeThreadLocal<InputSection>(*this, sec, name); 10630b57cec5SDimitry Andric } 10640b57cec5SDimitry Andric 106506c3fb27SDimitry Andric // Initialize symbols. symbols is a parallel array to the corresponding ELF 106606c3fb27SDimitry Andric // symbol table. 10671fd87a68SDimitry Andric template <class ELFT> 10681fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) { 10690eae32dcSDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 1070bdd1243dSDimitry Andric if (numSymbols == 0) { 1071bdd1243dSDimitry Andric numSymbols = eSyms.size(); 1072bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1073bdd1243dSDimitry Andric } 10740eae32dcSDimitry Andric 107581ad6265SDimitry Andric // Some entries have been filled by LazyObjFile. 107681ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) 107781ad6265SDimitry Andric if (!symbols[i]) 107881ad6265SDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 107981ad6265SDimitry Andric 108081ad6265SDimitry Andric // Perform symbol resolution on non-local symbols. 108181ad6265SDimitry Andric SmallVector<unsigned, 32> undefineds; 108281ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 108381ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 108481ad6265SDimitry Andric uint32_t secIdx = eSym.st_shndx; 108581ad6265SDimitry Andric if (secIdx == SHN_UNDEF) { 108681ad6265SDimitry Andric undefineds.push_back(i); 108781ad6265SDimitry Andric continue; 108881ad6265SDimitry Andric } 108981ad6265SDimitry Andric 109081ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 109181ad6265SDimitry Andric uint8_t stOther = eSym.st_other; 109281ad6265SDimitry Andric uint8_t type = eSym.getType(); 109381ad6265SDimitry Andric uint64_t value = eSym.st_value; 109481ad6265SDimitry Andric uint64_t size = eSym.st_size; 109581ad6265SDimitry Andric 109681ad6265SDimitry Andric Symbol *sym = symbols[i]; 109781ad6265SDimitry Andric sym->isUsedInRegularObj = true; 109881ad6265SDimitry Andric if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) { 109981ad6265SDimitry Andric if (value == 0 || value >= UINT32_MAX) 110081ad6265SDimitry Andric fatal(toString(this) + ": common symbol '" + sym->getName() + 110181ad6265SDimitry Andric "' has invalid alignment: " + Twine(value)); 110281ad6265SDimitry Andric hasCommonSyms = true; 110381ad6265SDimitry Andric sym->resolve( 110481ad6265SDimitry Andric CommonSymbol{this, StringRef(), binding, stOther, type, value, size}); 110581ad6265SDimitry Andric continue; 110681ad6265SDimitry Andric } 110781ad6265SDimitry Andric 110881ad6265SDimitry Andric // Handle global defined symbols. Defined::section will be set in postParse. 110981ad6265SDimitry Andric sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size, 111081ad6265SDimitry Andric nullptr}); 111181ad6265SDimitry Andric } 111281ad6265SDimitry Andric 111381ad6265SDimitry Andric // Undefined symbols (excluding those defined relative to non-prevailing 111481ad6265SDimitry Andric // sections) can trigger recursive extract. Process defined symbols first so 111581ad6265SDimitry Andric // that the relative order between a defined symbol and an undefined symbol 111681ad6265SDimitry Andric // does not change the symbol resolution behavior. In addition, a set of 111781ad6265SDimitry Andric // interconnected symbols will all be resolved to the same file, instead of 111881ad6265SDimitry Andric // being resolved to different files. 111981ad6265SDimitry Andric for (unsigned i : undefineds) { 112081ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 112181ad6265SDimitry Andric Symbol *sym = symbols[i]; 112281ad6265SDimitry Andric sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other, 112381ad6265SDimitry Andric eSym.getType()}); 112481ad6265SDimitry Andric sym->isUsedInRegularObj = true; 112581ad6265SDimitry Andric sym->referenced = true; 112681ad6265SDimitry Andric } 112781ad6265SDimitry Andric } 112881ad6265SDimitry Andric 1129bdd1243dSDimitry Andric template <class ELFT> 1130bdd1243dSDimitry Andric void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) { 1131bdd1243dSDimitry Andric if (!justSymbols) 1132bdd1243dSDimitry Andric initializeSections(ignoreComdats, getObj()); 1133bdd1243dSDimitry Andric 113481ad6265SDimitry Andric if (!firstGlobal) 113581ad6265SDimitry Andric return; 1136bdd1243dSDimitry Andric SymbolUnion *locals = makeThreadLocalN<SymbolUnion>(firstGlobal); 1137bdd1243dSDimitry Andric memset(locals, 0, sizeof(SymbolUnion) * firstGlobal); 113881ad6265SDimitry Andric 113981ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 11400eae32dcSDimitry Andric for (size_t i = 0, end = firstGlobal; i != end; ++i) { 11410b57cec5SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 11421fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 11431fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 11441fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 11451fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 11461fd87a68SDimitry Andric secIdx = 0; 11470eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 11480b57cec5SDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 11490eae32dcSDimitry Andric if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL)) 11505ffd83dbSDimitry Andric error(toString(this) + ": non-local symbol (" + Twine(i) + 11510eae32dcSDimitry Andric ") found at index < .symtab's sh_info (" + Twine(end) + ")"); 11525ffd83dbSDimitry Andric 11530eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 11545ffd83dbSDimitry Andric uint8_t type = eSym.getType(); 11555ffd83dbSDimitry Andric if (type == STT_FILE) 11560eae32dcSDimitry Andric sourceFile = CHECK(eSym.getName(stringTable), this); 11570eae32dcSDimitry Andric if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name)) 11585ffd83dbSDimitry Andric fatal(toString(this) + ": invalid symbol name offset"); 115904eeddc0SDimitry Andric StringRef name(stringTable.data() + eSym.st_name); 11605ffd83dbSDimitry Andric 11610eae32dcSDimitry Andric symbols[i] = reinterpret_cast<Symbol *>(locals + i); 11620eae32dcSDimitry Andric if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded) 11630eae32dcSDimitry Andric new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type, 11645ffd83dbSDimitry Andric /*discardedSecIdx=*/secIdx); 11655ffd83dbSDimitry Andric else 11660eae32dcSDimitry Andric new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type, 11670eae32dcSDimitry Andric eSym.st_value, eSym.st_size, sec); 1168bdd1243dSDimitry Andric symbols[i]->partition = 1; 116981ad6265SDimitry Andric symbols[i]->isUsedInRegularObj = true; 117081ad6265SDimitry Andric } 11715ffd83dbSDimitry Andric } 11725ffd83dbSDimitry Andric 117381ad6265SDimitry Andric // Called after all ObjFile::parse is called for all ObjFiles. This checks 117481ad6265SDimitry Andric // duplicate symbols and may do symbol property merge in the future. 117581ad6265SDimitry Andric template <class ELFT> void ObjFile<ELFT>::postParse() { 117681ad6265SDimitry Andric static std::mutex mu; 117781ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 11785ffd83dbSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 11795ffd83dbSDimitry Andric const Elf_Sym &eSym = eSyms[i]; 118081ad6265SDimitry Andric Symbol &sym = *symbols[i]; 11811fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 118281ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 118381ad6265SDimitry Andric if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK && 118481ad6265SDimitry Andric binding != STB_GNU_UNIQUE)) 118581ad6265SDimitry Andric errorOrWarn(toString(this) + ": symbol (" + Twine(i) + 118681ad6265SDimitry Andric ") has invalid binding: " + Twine((int)binding)); 118781ad6265SDimitry Andric 118881ad6265SDimitry Andric // st_value of STT_TLS represents the assigned offset, not the actual 118981ad6265SDimitry Andric // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can 119081ad6265SDimitry Andric // only be referenced by special TLS relocations. It is usually an error if 119181ad6265SDimitry Andric // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa. 119281ad6265SDimitry Andric if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS && 119381ad6265SDimitry Andric eSym.getType() != STT_NOTYPE) 119481ad6265SDimitry Andric errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " + 119581ad6265SDimitry Andric toString(sym.file) + "\n>>> in " + toString(this)); 119681ad6265SDimitry Andric 119781ad6265SDimitry Andric // Handle non-COMMON defined symbol below. !sym.file allows a symbol 119881ad6265SDimitry Andric // assignment to redefine a symbol without an error. 119981ad6265SDimitry Andric if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF || 120081ad6265SDimitry Andric secIdx == SHN_COMMON) 120181ad6265SDimitry Andric continue; 120281ad6265SDimitry Andric 12031fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 12041fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 12051fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 12061fd87a68SDimitry Andric secIdx = 0; 12070eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 12080eae32dcSDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 12090eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 12100b57cec5SDimitry Andric if (sec == &InputSection::discarded) { 121181ad6265SDimitry Andric if (sym.traced) { 121281ad6265SDimitry Andric printTraceSymbol(Undefined{this, sym.getName(), sym.binding, 121381ad6265SDimitry Andric sym.stOther, sym.type, secIdx}, 121481ad6265SDimitry Andric sym.getName()); 121581ad6265SDimitry Andric } 121681ad6265SDimitry Andric if (sym.file == this) { 121781ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1218bdd1243dSDimitry Andric ctx.nonPrevailingSyms.emplace_back(&sym, secIdx); 121981ad6265SDimitry Andric } 12200b57cec5SDimitry Andric continue; 12210b57cec5SDimitry Andric } 12220b57cec5SDimitry Andric 122381ad6265SDimitry Andric if (sym.file == this) { 122481ad6265SDimitry Andric cast<Defined>(sym).section = sec; 12250b57cec5SDimitry Andric continue; 12260b57cec5SDimitry Andric } 12270b57cec5SDimitry Andric 1228f3fd488fSDimitry Andric if (sym.binding == STB_WEAK || binding == STB_WEAK) 122981ad6265SDimitry Andric continue; 123081ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1231bdd1243dSDimitry Andric ctx.duplicates.push_back({&sym, this, sec, eSym.st_value}); 12320b57cec5SDimitry Andric } 12330b57cec5SDimitry Andric } 12340b57cec5SDimitry Andric 1235e8d8bef9SDimitry Andric // The handling of tentative definitions (COMMON symbols) in archives is murky. 1236fe6060f1SDimitry Andric // A tentative definition will be promoted to a global definition if there are 1237fe6060f1SDimitry Andric // no non-tentative definitions to dominate it. When we hold a tentative 1238fe6060f1SDimitry Andric // definition to a symbol and are inspecting archive members for inclusion 1239fe6060f1SDimitry Andric // there are 2 ways we can proceed: 1240e8d8bef9SDimitry Andric // 1241e8d8bef9SDimitry Andric // 1) Consider the tentative definition a 'real' definition (ie promotion from 1242e8d8bef9SDimitry Andric // tentative to real definition has already happened) and not inspect 1243e8d8bef9SDimitry Andric // archive members for Global/Weak definitions to replace the tentative 1244e8d8bef9SDimitry Andric // definition. An archive member would only be included if it satisfies some 1245e8d8bef9SDimitry Andric // other undefined symbol. This is the behavior Gold uses. 1246e8d8bef9SDimitry Andric // 1247e8d8bef9SDimitry Andric // 2) Consider the tentative definition as still undefined (ie the promotion to 1248fe6060f1SDimitry Andric // a real definition happens only after all symbol resolution is done). 1249fe6060f1SDimitry Andric // The linker searches archive members for STB_GLOBAL definitions to 1250e8d8bef9SDimitry Andric // replace the tentative definition with. This is the behavior used by 1251e8d8bef9SDimitry Andric // GNU ld. 1252e8d8bef9SDimitry Andric // 1253e8d8bef9SDimitry Andric // The second behavior is inherited from SysVR4, which based it on the FORTRAN 1254fe6060f1SDimitry Andric // COMMON BLOCK model. This behavior is needed for proper initialization in old 1255e8d8bef9SDimitry Andric // (pre F90) FORTRAN code that is packaged into an archive. 1256e8d8bef9SDimitry Andric // 1257fe6060f1SDimitry Andric // The following functions search archive members for definitions to replace 1258fe6060f1SDimitry Andric // tentative definitions (implementing behavior 2). 1259e8d8bef9SDimitry Andric static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName, 1260e8d8bef9SDimitry Andric StringRef archiveName) { 1261e8d8bef9SDimitry Andric IRSymtabFile symtabFile = check(readIRSymtab(mb)); 1262e8d8bef9SDimitry Andric for (const irsymtab::Reader::SymbolRef &sym : 1263e8d8bef9SDimitry Andric symtabFile.TheReader.symbols()) { 1264e8d8bef9SDimitry Andric if (sym.isGlobal() && sym.getName() == symName) 1265fe6060f1SDimitry Andric return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon(); 1266e8d8bef9SDimitry Andric } 1267e8d8bef9SDimitry Andric return false; 1268e8d8bef9SDimitry Andric } 1269e8d8bef9SDimitry Andric 1270e8d8bef9SDimitry Andric template <class ELFT> 1271bdd1243dSDimitry Andric static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName, 1272e8d8bef9SDimitry Andric StringRef archiveName) { 1273bdd1243dSDimitry Andric ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ekind, mb, archiveName); 1274bdd1243dSDimitry Andric obj->init(); 1275e8d8bef9SDimitry Andric StringRef stringtable = obj->getStringTable(); 1276e8d8bef9SDimitry Andric 1277e8d8bef9SDimitry Andric for (auto sym : obj->template getGlobalELFSyms<ELFT>()) { 1278e8d8bef9SDimitry Andric Expected<StringRef> name = sym.getName(stringtable); 1279e8d8bef9SDimitry Andric if (name && name.get() == symName) 1280fe6060f1SDimitry Andric return sym.isDefined() && sym.getBinding() == STB_GLOBAL && 1281fe6060f1SDimitry Andric !sym.isCommon(); 1282e8d8bef9SDimitry Andric } 1283e8d8bef9SDimitry Andric return false; 1284e8d8bef9SDimitry Andric } 1285e8d8bef9SDimitry Andric 1286e8d8bef9SDimitry Andric static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName, 1287e8d8bef9SDimitry Andric StringRef archiveName) { 1288e8d8bef9SDimitry Andric switch (getELFKind(mb, archiveName)) { 1289e8d8bef9SDimitry Andric case ELF32LEKind: 1290bdd1243dSDimitry Andric return isNonCommonDef<ELF32LE>(ELF32LEKind, mb, symName, archiveName); 1291e8d8bef9SDimitry Andric case ELF32BEKind: 1292bdd1243dSDimitry Andric return isNonCommonDef<ELF32BE>(ELF32BEKind, mb, symName, archiveName); 1293e8d8bef9SDimitry Andric case ELF64LEKind: 1294bdd1243dSDimitry Andric return isNonCommonDef<ELF64LE>(ELF64LEKind, mb, symName, archiveName); 1295e8d8bef9SDimitry Andric case ELF64BEKind: 1296bdd1243dSDimitry Andric return isNonCommonDef<ELF64BE>(ELF64BEKind, mb, symName, archiveName); 1297e8d8bef9SDimitry Andric default: 1298e8d8bef9SDimitry Andric llvm_unreachable("getELFKind"); 1299e8d8bef9SDimitry Andric } 1300e8d8bef9SDimitry Andric } 1301e8d8bef9SDimitry Andric 13020b57cec5SDimitry Andric unsigned SharedFile::vernauxNum; 13030b57cec5SDimitry Andric 1304bdd1243dSDimitry Andric SharedFile::SharedFile(MemoryBufferRef m, StringRef defaultSoName) 1305bdd1243dSDimitry Andric : ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName), 1306bdd1243dSDimitry Andric isNeeded(!config->asNeeded) {} 1307bdd1243dSDimitry Andric 13080b57cec5SDimitry Andric // Parse the version definitions in the object file if present, and return a 13090b57cec5SDimitry Andric // vector whose nth element contains a pointer to the Elf_Verdef for version 13100b57cec5SDimitry Andric // identifier n. Version identifiers that are not definitions map to nullptr. 13110b57cec5SDimitry Andric template <typename ELFT> 13120eae32dcSDimitry Andric static SmallVector<const void *, 0> 13130eae32dcSDimitry Andric parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) { 13140b57cec5SDimitry Andric if (!sec) 13150b57cec5SDimitry Andric return {}; 13160b57cec5SDimitry Andric 13170b57cec5SDimitry Andric // Build the Verdefs array by following the chain of Elf_Verdef objects 13180b57cec5SDimitry Andric // from the start of the .gnu.version_d section. 13190eae32dcSDimitry Andric SmallVector<const void *, 0> verdefs; 13200b57cec5SDimitry Andric const uint8_t *verdef = base + sec->sh_offset; 13210eae32dcSDimitry Andric for (unsigned i = 0, e = sec->sh_info; i != e; ++i) { 13220b57cec5SDimitry Andric auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef); 13230b57cec5SDimitry Andric verdef += curVerdef->vd_next; 13240b57cec5SDimitry Andric unsigned verdefIndex = curVerdef->vd_ndx; 13250eae32dcSDimitry Andric if (verdefIndex >= verdefs.size()) 13260b57cec5SDimitry Andric verdefs.resize(verdefIndex + 1); 13270b57cec5SDimitry Andric verdefs[verdefIndex] = curVerdef; 13280b57cec5SDimitry Andric } 13290b57cec5SDimitry Andric return verdefs; 13300b57cec5SDimitry Andric } 13310b57cec5SDimitry Andric 13325ffd83dbSDimitry Andric // Parse SHT_GNU_verneed to properly set the name of a versioned undefined 13335ffd83dbSDimitry Andric // symbol. We detect fatal issues which would cause vulnerabilities, but do not 13345ffd83dbSDimitry Andric // implement sophisticated error checking like in llvm-readobj because the value 13355ffd83dbSDimitry Andric // of such diagnostics is low. 13365ffd83dbSDimitry Andric template <typename ELFT> 13375ffd83dbSDimitry Andric std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj, 13385ffd83dbSDimitry Andric const typename ELFT::Shdr *sec) { 13395ffd83dbSDimitry Andric if (!sec) 13405ffd83dbSDimitry Andric return {}; 13415ffd83dbSDimitry Andric std::vector<uint32_t> verneeds; 1342e8d8bef9SDimitry Andric ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this); 13435ffd83dbSDimitry Andric const uint8_t *verneedBuf = data.begin(); 13445ffd83dbSDimitry Andric for (unsigned i = 0; i != sec->sh_info; ++i) { 13455ffd83dbSDimitry Andric if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) 13465ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Verneed"); 13475ffd83dbSDimitry Andric auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf); 13485ffd83dbSDimitry Andric const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux; 13495ffd83dbSDimitry Andric for (unsigned j = 0; j != vn->vn_cnt; ++j) { 13505ffd83dbSDimitry Andric if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) 13515ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Vernaux"); 13525ffd83dbSDimitry Andric auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf); 13535ffd83dbSDimitry Andric if (aux->vna_name >= this->stringTable.size()) 13545ffd83dbSDimitry Andric fatal(toString(this) + " has a Vernaux with an invalid vna_name"); 13555ffd83dbSDimitry Andric uint16_t version = aux->vna_other & VERSYM_VERSION; 13565ffd83dbSDimitry Andric if (version >= verneeds.size()) 13575ffd83dbSDimitry Andric verneeds.resize(version + 1); 13585ffd83dbSDimitry Andric verneeds[version] = aux->vna_name; 13595ffd83dbSDimitry Andric vernauxBuf += aux->vna_next; 13605ffd83dbSDimitry Andric } 13615ffd83dbSDimitry Andric verneedBuf += vn->vn_next; 13625ffd83dbSDimitry Andric } 13635ffd83dbSDimitry Andric return verneeds; 13645ffd83dbSDimitry Andric } 13655ffd83dbSDimitry Andric 13660b57cec5SDimitry Andric // We do not usually care about alignments of data in shared object 13670b57cec5SDimitry Andric // files because the loader takes care of it. However, if we promote a 13680b57cec5SDimitry Andric // DSO symbol to point to .bss due to copy relocation, we need to keep 13690b57cec5SDimitry Andric // the original alignment requirements. We infer it in this function. 13700b57cec5SDimitry Andric template <typename ELFT> 13710b57cec5SDimitry Andric static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections, 13720b57cec5SDimitry Andric const typename ELFT::Sym &sym) { 13730b57cec5SDimitry Andric uint64_t ret = UINT64_MAX; 13740b57cec5SDimitry Andric if (sym.st_value) 137506c3fb27SDimitry Andric ret = 1ULL << llvm::countr_zero((uint64_t)sym.st_value); 13760b57cec5SDimitry Andric if (0 < sym.st_shndx && sym.st_shndx < sections.size()) 13770b57cec5SDimitry Andric ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign); 13780b57cec5SDimitry Andric return (ret > UINT32_MAX) ? 0 : ret; 13790b57cec5SDimitry Andric } 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric // Fully parse the shared object file. 13820b57cec5SDimitry Andric // 13830b57cec5SDimitry Andric // This function parses symbol versions. If a DSO has version information, 13840b57cec5SDimitry Andric // the file has a ".gnu.version_d" section which contains symbol version 13850b57cec5SDimitry Andric // definitions. Each symbol is associated to one version through a table in 13860b57cec5SDimitry Andric // ".gnu.version" section. That table is a parallel array for the symbol 13870b57cec5SDimitry Andric // table, and each table entry contains an index in ".gnu.version_d". 13880b57cec5SDimitry Andric // 13890b57cec5SDimitry Andric // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for 13900b57cec5SDimitry Andric // VER_NDX_GLOBAL. There's no table entry for these special versions in 13910b57cec5SDimitry Andric // ".gnu.version_d". 13920b57cec5SDimitry Andric // 13930b57cec5SDimitry Andric // The file format for symbol versioning is perhaps a bit more complicated 13940b57cec5SDimitry Andric // than necessary, but you can easily understand the code if you wrap your 13950b57cec5SDimitry Andric // head around the data structure described above. 13960b57cec5SDimitry Andric template <class ELFT> void SharedFile::parse() { 13970b57cec5SDimitry Andric using Elf_Dyn = typename ELFT::Dyn; 13980b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 13990b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 14000b57cec5SDimitry Andric using Elf_Verdef = typename ELFT::Verdef; 14010b57cec5SDimitry Andric using Elf_Versym = typename ELFT::Versym; 14020b57cec5SDimitry Andric 14030b57cec5SDimitry Andric ArrayRef<Elf_Dyn> dynamicTags; 14040b57cec5SDimitry Andric const ELFFile<ELFT> obj = this->getObj<ELFT>(); 14050eae32dcSDimitry Andric ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>(); 14060b57cec5SDimitry Andric 14070b57cec5SDimitry Andric const Elf_Shdr *versymSec = nullptr; 14080b57cec5SDimitry Andric const Elf_Shdr *verdefSec = nullptr; 14095ffd83dbSDimitry Andric const Elf_Shdr *verneedSec = nullptr; 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. 14120b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) { 14130b57cec5SDimitry Andric switch (sec.sh_type) { 14140b57cec5SDimitry Andric default: 14150b57cec5SDimitry Andric continue; 14160b57cec5SDimitry Andric case SHT_DYNAMIC: 14170b57cec5SDimitry Andric dynamicTags = 1418e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this); 14190b57cec5SDimitry Andric break; 14200b57cec5SDimitry Andric case SHT_GNU_versym: 14210b57cec5SDimitry Andric versymSec = &sec; 14220b57cec5SDimitry Andric break; 14230b57cec5SDimitry Andric case SHT_GNU_verdef: 14240b57cec5SDimitry Andric verdefSec = &sec; 14250b57cec5SDimitry Andric break; 14265ffd83dbSDimitry Andric case SHT_GNU_verneed: 14275ffd83dbSDimitry Andric verneedSec = &sec; 14285ffd83dbSDimitry Andric break; 14290b57cec5SDimitry Andric } 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric if (versymSec && numELFSyms == 0) { 14330b57cec5SDimitry Andric error("SHT_GNU_versym should be associated with symbol table"); 14340b57cec5SDimitry Andric return; 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric 14370b57cec5SDimitry Andric // Search for a DT_SONAME tag to initialize this->soName. 14380b57cec5SDimitry Andric for (const Elf_Dyn &dyn : dynamicTags) { 14390b57cec5SDimitry Andric if (dyn.d_tag == DT_NEEDED) { 14400b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 14410b57cec5SDimitry Andric if (val >= this->stringTable.size()) 14420b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_NEEDED entry"); 14430b57cec5SDimitry Andric dtNeeded.push_back(this->stringTable.data() + val); 14440b57cec5SDimitry Andric } else if (dyn.d_tag == DT_SONAME) { 14450b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 14460b57cec5SDimitry Andric if (val >= this->stringTable.size()) 14470b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_SONAME entry"); 14480b57cec5SDimitry Andric soName = this->stringTable.data() + val; 14490b57cec5SDimitry Andric } 14500b57cec5SDimitry Andric } 14510b57cec5SDimitry Andric 14520b57cec5SDimitry Andric // DSOs are uniquified not by filename but by soname. 145304eeddc0SDimitry Andric DenseMap<CachedHashStringRef, SharedFile *>::iterator it; 14540b57cec5SDimitry Andric bool wasInserted; 145504eeddc0SDimitry Andric std::tie(it, wasInserted) = 1456bdd1243dSDimitry Andric symtab.soNames.try_emplace(CachedHashStringRef(soName), this); 14570b57cec5SDimitry Andric 14580b57cec5SDimitry Andric // If a DSO appears more than once on the command line with and without 14590b57cec5SDimitry Andric // --as-needed, --no-as-needed takes precedence over --as-needed because a 14600b57cec5SDimitry Andric // user can add an extra DSO with --no-as-needed to force it to be added to 14610b57cec5SDimitry Andric // the dependency list. 14620b57cec5SDimitry Andric it->second->isNeeded |= isNeeded; 14630b57cec5SDimitry Andric if (!wasInserted) 14640b57cec5SDimitry Andric return; 14650b57cec5SDimitry Andric 1466bdd1243dSDimitry Andric ctx.sharedFiles.push_back(this); 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andric verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec); 14695ffd83dbSDimitry Andric std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec); 14700b57cec5SDimitry Andric 14710b57cec5SDimitry Andric // Parse ".gnu.version" section which is a parallel array for the symbol 14720b57cec5SDimitry Andric // table. If a given file doesn't have a ".gnu.version" section, we use 14730b57cec5SDimitry Andric // VER_NDX_GLOBAL. 14740b57cec5SDimitry Andric size_t size = numELFSyms - firstGlobal; 14755ffd83dbSDimitry Andric std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL); 14760b57cec5SDimitry Andric if (versymSec) { 14770b57cec5SDimitry Andric ArrayRef<Elf_Versym> versym = 1478e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec), 14790b57cec5SDimitry Andric this) 14800b57cec5SDimitry Andric .slice(firstGlobal); 14810b57cec5SDimitry Andric for (size_t i = 0; i < size; ++i) 14820b57cec5SDimitry Andric versyms[i] = versym[i].vs_index; 14830b57cec5SDimitry Andric } 14840b57cec5SDimitry Andric 14850b57cec5SDimitry Andric // System libraries can have a lot of symbols with versions. Using a 14860b57cec5SDimitry Andric // fixed buffer for computing the versions name (foo@ver) can save a 14870b57cec5SDimitry Andric // lot of allocations. 14880b57cec5SDimitry Andric SmallString<0> versionedNameBuffer; 14890b57cec5SDimitry Andric 14900b57cec5SDimitry Andric // Add symbols to the symbol table. 14910b57cec5SDimitry Andric ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>(); 14920eae32dcSDimitry Andric for (size_t i = 0, e = syms.size(); i != e; ++i) { 14930b57cec5SDimitry Andric const Elf_Sym &sym = syms[i]; 14940b57cec5SDimitry Andric 14950b57cec5SDimitry Andric // ELF spec requires that all local symbols precede weak or global 14960b57cec5SDimitry Andric // symbols in each symbol table, and the index of first non-local symbol 14970b57cec5SDimitry Andric // is stored to sh_info. If a local symbol appears after some non-local 14980b57cec5SDimitry Andric // symbol, that's a violation of the spec. 14990eae32dcSDimitry Andric StringRef name = CHECK(sym.getName(stringTable), this); 15000b57cec5SDimitry Andric if (sym.getBinding() == STB_LOCAL) { 1501bdd1243dSDimitry Andric errorOrWarn(toString(this) + ": invalid local symbol '" + name + 1502bdd1243dSDimitry Andric "' in global part of symbol table"); 15030b57cec5SDimitry Andric continue; 15040b57cec5SDimitry Andric } 15050b57cec5SDimitry Andric 1506bdd1243dSDimitry Andric const uint16_t ver = versyms[i], idx = ver & ~VERSYM_HIDDEN; 15070b57cec5SDimitry Andric if (sym.isUndefined()) { 15085ffd83dbSDimitry Andric // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but 15095ffd83dbSDimitry Andric // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL. 1510bdd1243dSDimitry Andric if (ver != VER_NDX_LOCAL && ver != VER_NDX_GLOBAL) { 15115ffd83dbSDimitry Andric if (idx >= verneeds.size()) { 15125ffd83dbSDimitry Andric error("corrupt input file: version need index " + Twine(idx) + 15135ffd83dbSDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 15145ffd83dbSDimitry Andric toString(this)); 15155ffd83dbSDimitry Andric continue; 15165ffd83dbSDimitry Andric } 15170eae32dcSDimitry Andric StringRef verName = stringTable.data() + verneeds[idx]; 15185ffd83dbSDimitry Andric versionedNameBuffer.clear(); 151904eeddc0SDimitry Andric name = saver().save( 152004eeddc0SDimitry Andric (name + "@" + verName).toStringRef(versionedNameBuffer)); 15215ffd83dbSDimitry Andric } 15220eae32dcSDimitry Andric Symbol *s = symtab.addSymbol( 15230b57cec5SDimitry Andric Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()}); 15240b57cec5SDimitry Andric s->exportDynamic = true; 15250eae32dcSDimitry Andric if (s->isUndefined() && sym.getBinding() != STB_WEAK && 1526fe6060f1SDimitry Andric config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) 1527fe6060f1SDimitry Andric requiredSymbols.push_back(s); 15280b57cec5SDimitry Andric continue; 15290b57cec5SDimitry Andric } 15300b57cec5SDimitry Andric 1531bdd1243dSDimitry Andric if (ver == VER_NDX_LOCAL || 1532bdd1243dSDimitry Andric (ver != VER_NDX_GLOBAL && idx >= verdefs.size())) { 1533bdd1243dSDimitry Andric // In GNU ld < 2.31 (before 3be08ea4728b56d35e136af4e6fd3086ade17764), the 1534bdd1243dSDimitry Andric // MIPS port puts _gp_disp symbol into DSO files and incorrectly assigns 1535bdd1243dSDimitry Andric // VER_NDX_LOCAL. Workaround this bug. 1536bdd1243dSDimitry Andric if (config->emachine == EM_MIPS && name == "_gp_disp") 15370b57cec5SDimitry Andric continue; 15380b57cec5SDimitry Andric error("corrupt input file: version definition index " + Twine(idx) + 15390b57cec5SDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 15400b57cec5SDimitry Andric toString(this)); 15410b57cec5SDimitry Andric continue; 15420b57cec5SDimitry Andric } 15430b57cec5SDimitry Andric 1544bdd1243dSDimitry Andric uint32_t alignment = getAlignment<ELFT>(sections, sym); 1545bdd1243dSDimitry Andric if (ver == idx) { 1546bdd1243dSDimitry Andric auto *s = symtab.addSymbol( 1547bdd1243dSDimitry Andric SharedSymbol{*this, name, sym.getBinding(), sym.st_other, 1548bdd1243dSDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 1549bdd1243dSDimitry Andric if (s->file == this) 1550*5f757f3fSDimitry Andric s->versionId = ver; 1551bdd1243dSDimitry Andric } 1552bdd1243dSDimitry Andric 1553bdd1243dSDimitry Andric // Also add the symbol with the versioned name to handle undefined symbols 1554bdd1243dSDimitry Andric // with explicit versions. 1555bdd1243dSDimitry Andric if (ver == VER_NDX_GLOBAL) 1556bdd1243dSDimitry Andric continue; 1557bdd1243dSDimitry Andric 15580b57cec5SDimitry Andric StringRef verName = 15590eae32dcSDimitry Andric stringTable.data() + 15600b57cec5SDimitry Andric reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name; 15610b57cec5SDimitry Andric versionedNameBuffer.clear(); 15620b57cec5SDimitry Andric name = (name + "@" + verName).toStringRef(versionedNameBuffer); 156381ad6265SDimitry Andric auto *s = symtab.addSymbol( 156481ad6265SDimitry Andric SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other, 156581ad6265SDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 156681ad6265SDimitry Andric if (s->file == this) 1567*5f757f3fSDimitry Andric s->versionId = idx; 15680b57cec5SDimitry Andric } 15690b57cec5SDimitry Andric } 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric static ELFKind getBitcodeELFKind(const Triple &t) { 15720b57cec5SDimitry Andric if (t.isLittleEndian()) 15730b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 15740b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 15750b57cec5SDimitry Andric } 15760b57cec5SDimitry Andric 1577e8d8bef9SDimitry Andric static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) { 15780b57cec5SDimitry Andric switch (t.getArch()) { 15790b57cec5SDimitry Andric case Triple::aarch64: 1580fe6060f1SDimitry Andric case Triple::aarch64_be: 15810b57cec5SDimitry Andric return EM_AARCH64; 15820b57cec5SDimitry Andric case Triple::amdgcn: 15830b57cec5SDimitry Andric case Triple::r600: 15840b57cec5SDimitry Andric return EM_AMDGPU; 15850b57cec5SDimitry Andric case Triple::arm: 1586*5f757f3fSDimitry Andric case Triple::armeb: 15870b57cec5SDimitry Andric case Triple::thumb: 1588*5f757f3fSDimitry Andric case Triple::thumbeb: 15890b57cec5SDimitry Andric return EM_ARM; 15900b57cec5SDimitry Andric case Triple::avr: 15910b57cec5SDimitry Andric return EM_AVR; 1592349cc55cSDimitry Andric case Triple::hexagon: 1593349cc55cSDimitry Andric return EM_HEXAGON; 159406c3fb27SDimitry Andric case Triple::loongarch32: 159506c3fb27SDimitry Andric case Triple::loongarch64: 159606c3fb27SDimitry Andric return EM_LOONGARCH; 15970b57cec5SDimitry Andric case Triple::mips: 15980b57cec5SDimitry Andric case Triple::mipsel: 15990b57cec5SDimitry Andric case Triple::mips64: 16000b57cec5SDimitry Andric case Triple::mips64el: 16010b57cec5SDimitry Andric return EM_MIPS; 16020b57cec5SDimitry Andric case Triple::msp430: 16030b57cec5SDimitry Andric return EM_MSP430; 16040b57cec5SDimitry Andric case Triple::ppc: 1605e8d8bef9SDimitry Andric case Triple::ppcle: 16060b57cec5SDimitry Andric return EM_PPC; 16070b57cec5SDimitry Andric case Triple::ppc64: 16080b57cec5SDimitry Andric case Triple::ppc64le: 16090b57cec5SDimitry Andric return EM_PPC64; 16100b57cec5SDimitry Andric case Triple::riscv32: 16110b57cec5SDimitry Andric case Triple::riscv64: 16120b57cec5SDimitry Andric return EM_RISCV; 1613*5f757f3fSDimitry Andric case Triple::sparcv9: 1614*5f757f3fSDimitry Andric return EM_SPARCV9; 16150b57cec5SDimitry Andric case Triple::x86: 16160b57cec5SDimitry Andric return t.isOSIAMCU() ? EM_IAMCU : EM_386; 16170b57cec5SDimitry Andric case Triple::x86_64: 16180b57cec5SDimitry Andric return EM_X86_64; 16190b57cec5SDimitry Andric default: 16200b57cec5SDimitry Andric error(path + ": could not infer e_machine from bitcode target triple " + 16210b57cec5SDimitry Andric t.str()); 16220b57cec5SDimitry Andric return EM_NONE; 16230b57cec5SDimitry Andric } 16240b57cec5SDimitry Andric } 16250b57cec5SDimitry Andric 1626e8d8bef9SDimitry Andric static uint8_t getOsAbi(const Triple &t) { 1627e8d8bef9SDimitry Andric switch (t.getOS()) { 1628e8d8bef9SDimitry Andric case Triple::AMDHSA: 1629e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_HSA; 1630e8d8bef9SDimitry Andric case Triple::AMDPAL: 1631e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_PAL; 1632e8d8bef9SDimitry Andric case Triple::Mesa3D: 1633e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_MESA3D; 1634e8d8bef9SDimitry Andric default: 1635e8d8bef9SDimitry Andric return ELF::ELFOSABI_NONE; 1636e8d8bef9SDimitry Andric } 1637e8d8bef9SDimitry Andric } 1638e8d8bef9SDimitry Andric 16390b57cec5SDimitry Andric BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName, 16400eae32dcSDimitry Andric uint64_t offsetInArchive, bool lazy) 16410b57cec5SDimitry Andric : InputFile(BitcodeKind, mb) { 16420eae32dcSDimitry Andric this->archiveName = archiveName; 16430eae32dcSDimitry Andric this->lazy = lazy; 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric std::string path = mb.getBufferIdentifier().str(); 16460b57cec5SDimitry Andric if (config->thinLTOIndexOnly) 16470b57cec5SDimitry Andric path = replaceThinLTOSuffix(mb.getBufferIdentifier()); 16480b57cec5SDimitry Andric 16490b57cec5SDimitry Andric // ThinLTO assumes that all MemoryBufferRefs given to it have a unique 16500b57cec5SDimitry Andric // name. If two archives define two members with the same name, this 16510b57cec5SDimitry Andric // causes a collision which result in only one of the objects being taken 16520b57cec5SDimitry Andric // into consideration at LTO time (which very likely causes undefined 16530b57cec5SDimitry Andric // symbols later in the link stage). So we append file offset to make 16540b57cec5SDimitry Andric // filename unique. 165504eeddc0SDimitry Andric StringRef name = archiveName.empty() 165604eeddc0SDimitry Andric ? saver().save(path) 165704eeddc0SDimitry Andric : saver().save(archiveName + "(" + path::filename(path) + 165804eeddc0SDimitry Andric " at " + utostr(offsetInArchive) + ")"); 16590b57cec5SDimitry Andric MemoryBufferRef mbref(mb.getBuffer(), name); 16600b57cec5SDimitry Andric 16610b57cec5SDimitry Andric obj = CHECK(lto::InputFile::create(mbref), this); 16620b57cec5SDimitry Andric 16630b57cec5SDimitry Andric Triple t(obj->getTargetTriple()); 16640b57cec5SDimitry Andric ekind = getBitcodeELFKind(t); 16650b57cec5SDimitry Andric emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t); 1666e8d8bef9SDimitry Andric osabi = getOsAbi(t); 16670b57cec5SDimitry Andric } 16680b57cec5SDimitry Andric 16690b57cec5SDimitry Andric static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 16700b57cec5SDimitry Andric switch (gvVisibility) { 16710b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: 16720b57cec5SDimitry Andric return STV_DEFAULT; 16730b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: 16740b57cec5SDimitry Andric return STV_HIDDEN; 16750b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: 16760b57cec5SDimitry Andric return STV_PROTECTED; 16770b57cec5SDimitry Andric } 16780b57cec5SDimitry Andric llvm_unreachable("unknown visibility"); 16790b57cec5SDimitry Andric } 16800b57cec5SDimitry Andric 168104eeddc0SDimitry Andric static void 168204eeddc0SDimitry Andric createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats, 168304eeddc0SDimitry Andric const lto::InputFile::Symbol &objSym, BitcodeFile &f) { 16840b57cec5SDimitry Andric uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL; 16850b57cec5SDimitry Andric uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE; 16860b57cec5SDimitry Andric uint8_t visibility = mapVisibility(objSym.getVisibility()); 16870b57cec5SDimitry Andric 168881ad6265SDimitry Andric if (!sym) 1689bdd1243dSDimitry Andric sym = symtab.insert(saver().save(objSym.getName())); 169004eeddc0SDimitry Andric 16910b57cec5SDimitry Andric int c = objSym.getComdatIndex(); 16920b57cec5SDimitry Andric if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) { 169381ad6265SDimitry Andric Undefined newSym(&f, StringRef(), binding, visibility, type); 169404eeddc0SDimitry Andric sym->resolve(newSym); 169504eeddc0SDimitry Andric sym->referenced = true; 169604eeddc0SDimitry Andric return; 16970b57cec5SDimitry Andric } 16980b57cec5SDimitry Andric 169904eeddc0SDimitry Andric if (objSym.isCommon()) { 170081ad6265SDimitry Andric sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT, 170104eeddc0SDimitry Andric objSym.getCommonAlignment(), 170204eeddc0SDimitry Andric objSym.getCommonSize()}); 170304eeddc0SDimitry Andric } else { 170481ad6265SDimitry Andric Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr); 170581ad6265SDimitry Andric if (objSym.canBeOmittedFromSymbolTable()) 170685868e8aSDimitry Andric newSym.exportDynamic = false; 170704eeddc0SDimitry Andric sym->resolve(newSym); 170804eeddc0SDimitry Andric } 17090b57cec5SDimitry Andric } 17100b57cec5SDimitry Andric 1711bdd1243dSDimitry Andric void BitcodeFile::parse() { 1712fe6060f1SDimitry Andric for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) { 17130b57cec5SDimitry Andric keptComdats.push_back( 1714fe6060f1SDimitry Andric s.second == Comdat::NoDeduplicate || 1715bdd1243dSDimitry Andric symtab.comdatGroups.try_emplace(CachedHashStringRef(s.first), this) 1716fe6060f1SDimitry Andric .second); 1717fe6060f1SDimitry Andric } 17180b57cec5SDimitry Andric 1719bdd1243dSDimitry Andric if (numSymbols == 0) { 1720bdd1243dSDimitry Andric numSymbols = obj->symbols().size(); 1721bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1722bdd1243dSDimitry Andric } 172381ad6265SDimitry Andric // Process defined symbols first. See the comment in 172481ad6265SDimitry Andric // ObjFile<ELFT>::initializeSymbols. 1725bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1726bdd1243dSDimitry Andric if (!irSym.isUndefined()) 1727bdd1243dSDimitry Andric createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); 1728bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1729bdd1243dSDimitry Andric if (irSym.isUndefined()) 1730bdd1243dSDimitry Andric createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); 17310b57cec5SDimitry Andric 17320b57cec5SDimitry Andric for (auto l : obj->getDependentLibraries()) 17330b57cec5SDimitry Andric addDependentLibrary(l, this); 17340b57cec5SDimitry Andric } 17350b57cec5SDimitry Andric 17360eae32dcSDimitry Andric void BitcodeFile::parseLazy() { 1737bdd1243dSDimitry Andric numSymbols = obj->symbols().size(); 1738bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1739bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1740bdd1243dSDimitry Andric if (!irSym.isUndefined()) { 1741bdd1243dSDimitry Andric auto *sym = symtab.insert(saver().save(irSym.getName())); 174281ad6265SDimitry Andric sym->resolve(LazyObject{*this}); 1743bdd1243dSDimitry Andric symbols[i] = sym; 174481ad6265SDimitry Andric } 174581ad6265SDimitry Andric } 174681ad6265SDimitry Andric 174781ad6265SDimitry Andric void BitcodeFile::postParse() { 1748bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) { 1749bdd1243dSDimitry Andric const Symbol &sym = *symbols[i]; 1750bdd1243dSDimitry Andric if (sym.file == this || !sym.isDefined() || irSym.isUndefined() || 1751bdd1243dSDimitry Andric irSym.isCommon() || irSym.isWeak()) 175281ad6265SDimitry Andric continue; 1753bdd1243dSDimitry Andric int c = irSym.getComdatIndex(); 175481ad6265SDimitry Andric if (c != -1 && !keptComdats[c]) 175581ad6265SDimitry Andric continue; 175681ad6265SDimitry Andric reportDuplicate(sym, this, nullptr, 0); 175781ad6265SDimitry Andric } 17580eae32dcSDimitry Andric } 17590eae32dcSDimitry Andric 17600b57cec5SDimitry Andric void BinaryFile::parse() { 17610b57cec5SDimitry Andric ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer()); 17620b57cec5SDimitry Andric auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 17630b57cec5SDimitry Andric 8, data, ".data"); 17640b57cec5SDimitry Andric sections.push_back(section); 17650b57cec5SDimitry Andric 17660b57cec5SDimitry Andric // For each input file foo that is embedded to a result as a binary 17670b57cec5SDimitry Andric // blob, we define _binary_foo_{start,end,size} symbols, so that 17680b57cec5SDimitry Andric // user programs can access blobs by name. Non-alphanumeric 17690b57cec5SDimitry Andric // characters in a filename are replaced with underscore. 17700b57cec5SDimitry Andric std::string s = "_binary_" + mb.getBufferIdentifier().str(); 177106c3fb27SDimitry Andric for (char &c : s) 177206c3fb27SDimitry Andric if (!isAlnum(c)) 177306c3fb27SDimitry Andric c = '_'; 17740b57cec5SDimitry Andric 177504eeddc0SDimitry Andric llvm::StringSaver &saver = lld::saver(); 177604eeddc0SDimitry Andric 1777*5f757f3fSDimitry Andric symtab.addAndCheckDuplicate(Defined{this, saver.save(s + "_start"), 1778bdd1243dSDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 0, 0, 1779bdd1243dSDimitry Andric section}); 1780*5f757f3fSDimitry Andric symtab.addAndCheckDuplicate(Defined{this, saver.save(s + "_end"), STB_GLOBAL, 1781*5f757f3fSDimitry Andric STV_DEFAULT, STT_OBJECT, data.size(), 0, 1782*5f757f3fSDimitry Andric section}); 1783*5f757f3fSDimitry Andric symtab.addAndCheckDuplicate(Defined{this, saver.save(s + "_size"), STB_GLOBAL, 1784*5f757f3fSDimitry Andric STV_DEFAULT, STT_OBJECT, data.size(), 0, 1785*5f757f3fSDimitry Andric nullptr}); 17860b57cec5SDimitry Andric } 17870b57cec5SDimitry Andric 1788fcaf7f86SDimitry Andric ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName, 1789fcaf7f86SDimitry Andric bool lazy) { 1790fcaf7f86SDimitry Andric ELFFileBase *f; 17910b57cec5SDimitry Andric switch (getELFKind(mb, archiveName)) { 17920b57cec5SDimitry Andric case ELF32LEKind: 1793bdd1243dSDimitry Andric f = make<ObjFile<ELF32LE>>(ELF32LEKind, mb, archiveName); 1794fcaf7f86SDimitry Andric break; 17950b57cec5SDimitry Andric case ELF32BEKind: 1796bdd1243dSDimitry Andric f = make<ObjFile<ELF32BE>>(ELF32BEKind, mb, archiveName); 1797fcaf7f86SDimitry Andric break; 17980b57cec5SDimitry Andric case ELF64LEKind: 1799bdd1243dSDimitry Andric f = make<ObjFile<ELF64LE>>(ELF64LEKind, mb, archiveName); 1800fcaf7f86SDimitry Andric break; 18010b57cec5SDimitry Andric case ELF64BEKind: 1802bdd1243dSDimitry Andric f = make<ObjFile<ELF64BE>>(ELF64BEKind, mb, archiveName); 1803fcaf7f86SDimitry Andric break; 18040b57cec5SDimitry Andric default: 18050b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 18060b57cec5SDimitry Andric } 1807bdd1243dSDimitry Andric f->init(); 1808fcaf7f86SDimitry Andric f->lazy = lazy; 1809fcaf7f86SDimitry Andric return f; 18100b57cec5SDimitry Andric } 18110b57cec5SDimitry Andric 18120eae32dcSDimitry Andric template <class ELFT> void ObjFile<ELFT>::parseLazy() { 18130eae32dcSDimitry Andric const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>(); 1814bdd1243dSDimitry Andric numSymbols = eSyms.size(); 1815bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 18160b57cec5SDimitry Andric 18170eae32dcSDimitry Andric // resolve() may trigger this->extract() if an existing symbol is an undefined 18180eae32dcSDimitry Andric // symbol. If that happens, this function has served its purpose, and we can 18190eae32dcSDimitry Andric // exit from the loop early. 1820bdd1243dSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 1821bdd1243dSDimitry Andric if (eSyms[i].st_shndx == SHN_UNDEF) 1822bdd1243dSDimitry Andric continue; 1823bdd1243dSDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 1824bdd1243dSDimitry Andric symbols[i]->resolve(LazyObject{*this}); 18250eae32dcSDimitry Andric if (!lazy) 1826bdd1243dSDimitry Andric break; 18270b57cec5SDimitry Andric } 18280b57cec5SDimitry Andric } 18290b57cec5SDimitry Andric 1830*5f757f3fSDimitry Andric bool InputFile::shouldExtractForCommon(StringRef name) const { 1831fcaf7f86SDimitry Andric if (isa<BitcodeFile>(this)) 1832e8d8bef9SDimitry Andric return isBitcodeNonCommonDef(mb, name, archiveName); 1833e8d8bef9SDimitry Andric 1834e8d8bef9SDimitry Andric return isNonCommonDef(mb, name, archiveName); 1835e8d8bef9SDimitry Andric } 1836e8d8bef9SDimitry Andric 18375ffd83dbSDimitry Andric std::string elf::replaceThinLTOSuffix(StringRef path) { 1838bdd1243dSDimitry Andric auto [suffix, repl] = config->thinLTOObjectSuffixReplace; 18390b57cec5SDimitry Andric if (path.consume_back(suffix)) 18400b57cec5SDimitry Andric return (path + repl).str(); 18415ffd83dbSDimitry Andric return std::string(path); 18420b57cec5SDimitry Andric } 18430b57cec5SDimitry Andric 18445ffd83dbSDimitry Andric template class elf::ObjFile<ELF32LE>; 18455ffd83dbSDimitry Andric template class elf::ObjFile<ELF32BE>; 18465ffd83dbSDimitry Andric template class elf::ObjFile<ELF64LE>; 18475ffd83dbSDimitry Andric template class elf::ObjFile<ELF64BE>; 18480b57cec5SDimitry Andric 18490b57cec5SDimitry Andric template void SharedFile::parse<ELF32LE>(); 18500b57cec5SDimitry Andric template void SharedFile::parse<ELF32BE>(); 18510b57cec5SDimitry Andric template void SharedFile::parse<ELF64LE>(); 18520b57cec5SDimitry Andric template void SharedFile::parse<ELF64BE>(); 1853