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" 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric using namespace llvm::ELF; 360b57cec5SDimitry Andric using namespace llvm::object; 370b57cec5SDimitry Andric using namespace llvm::sys; 380b57cec5SDimitry Andric using namespace llvm::sys::fs; 390b57cec5SDimitry Andric using namespace llvm::support::endian; 405ffd83dbSDimitry Andric using namespace lld; 415ffd83dbSDimitry Andric using namespace lld::elf; 420b57cec5SDimitry Andric 435ffd83dbSDimitry Andric bool InputFile::isInGroup; 445ffd83dbSDimitry Andric uint32_t InputFile::nextGroupId; 455ffd83dbSDimitry Andric 465ffd83dbSDimitry Andric std::unique_ptr<TarWriter> elf::tar; 475ffd83dbSDimitry Andric 4885868e8aSDimitry Andric // Returns "<internal>", "foo.a(bar.o)" or "baz.o". 495ffd83dbSDimitry Andric std::string lld::toString(const InputFile *f) { 50bdd1243dSDimitry Andric static std::mutex mu; 5185868e8aSDimitry Andric if (!f) 5285868e8aSDimitry Andric return "<internal>"; 530b57cec5SDimitry Andric 54bdd1243dSDimitry Andric { 55bdd1243dSDimitry Andric std::lock_guard<std::mutex> lock(mu); 5685868e8aSDimitry Andric if (f->toStringCache.empty()) { 5785868e8aSDimitry Andric if (f->archiveName.empty()) 580eae32dcSDimitry Andric f->toStringCache = f->getName(); 5985868e8aSDimitry Andric else 600eae32dcSDimitry Andric (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache); 6185868e8aSDimitry Andric } 62bdd1243dSDimitry Andric } 630eae32dcSDimitry Andric return std::string(f->toStringCache); 6485868e8aSDimitry Andric } 6585868e8aSDimitry Andric 660b57cec5SDimitry Andric static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) { 670b57cec5SDimitry Andric unsigned char size; 680b57cec5SDimitry Andric unsigned char endian; 690b57cec5SDimitry Andric std::tie(size, endian) = getElfArchType(mb.getBuffer()); 700b57cec5SDimitry Andric 710b57cec5SDimitry Andric auto report = [&](StringRef msg) { 720b57cec5SDimitry Andric StringRef filename = mb.getBufferIdentifier(); 730b57cec5SDimitry Andric if (archiveName.empty()) 740b57cec5SDimitry Andric fatal(filename + ": " + msg); 750b57cec5SDimitry Andric else 760b57cec5SDimitry Andric fatal(archiveName + "(" + filename + "): " + msg); 770b57cec5SDimitry Andric }; 780b57cec5SDimitry Andric 79*06c3fb27SDimitry Andric if (!mb.getBuffer().starts_with(ElfMagic)) 800b57cec5SDimitry Andric report("not an ELF file"); 810b57cec5SDimitry Andric if (endian != ELFDATA2LSB && endian != ELFDATA2MSB) 820b57cec5SDimitry Andric report("corrupted ELF file: invalid data encoding"); 830b57cec5SDimitry Andric if (size != ELFCLASS32 && size != ELFCLASS64) 840b57cec5SDimitry Andric report("corrupted ELF file: invalid file class"); 850b57cec5SDimitry Andric 860b57cec5SDimitry Andric size_t bufSize = mb.getBuffer().size(); 870b57cec5SDimitry Andric if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) || 880b57cec5SDimitry Andric (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr))) 890b57cec5SDimitry Andric report("corrupted ELF file: file is too short"); 900b57cec5SDimitry Andric 910b57cec5SDimitry Andric if (size == ELFCLASS32) 920b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 930b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 940b57cec5SDimitry Andric } 950b57cec5SDimitry Andric 96bdd1243dSDimitry Andric // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD 97bdd1243dSDimitry Andric // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how 98bdd1243dSDimitry Andric // the input objects have been compiled. 99bdd1243dSDimitry Andric static void updateARMVFPArgs(const ARMAttributeParser &attributes, 100bdd1243dSDimitry Andric const InputFile *f) { 101bdd1243dSDimitry Andric std::optional<unsigned> attr = 102bdd1243dSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args); 103bdd1243dSDimitry Andric if (!attr) 104bdd1243dSDimitry Andric // If an ABI tag isn't present then it is implicitly given the value of 0 105bdd1243dSDimitry Andric // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files, 106bdd1243dSDimitry Andric // including some in glibc that don't use FP args (and should have value 3) 107bdd1243dSDimitry Andric // don't have the attribute so we do not consider an implicit value of 0 108bdd1243dSDimitry Andric // as a clash. 109bdd1243dSDimitry Andric return; 110bdd1243dSDimitry Andric 111bdd1243dSDimitry Andric unsigned vfpArgs = *attr; 112bdd1243dSDimitry Andric ARMVFPArgKind arg; 113bdd1243dSDimitry Andric switch (vfpArgs) { 114bdd1243dSDimitry Andric case ARMBuildAttrs::BaseAAPCS: 115bdd1243dSDimitry Andric arg = ARMVFPArgKind::Base; 116bdd1243dSDimitry Andric break; 117bdd1243dSDimitry Andric case ARMBuildAttrs::HardFPAAPCS: 118bdd1243dSDimitry Andric arg = ARMVFPArgKind::VFP; 119bdd1243dSDimitry Andric break; 120bdd1243dSDimitry Andric case ARMBuildAttrs::ToolChainFPPCS: 121bdd1243dSDimitry Andric // Tool chain specific convention that conforms to neither AAPCS variant. 122bdd1243dSDimitry Andric arg = ARMVFPArgKind::ToolChain; 123bdd1243dSDimitry Andric break; 124bdd1243dSDimitry Andric case ARMBuildAttrs::CompatibleFPAAPCS: 125bdd1243dSDimitry Andric // Object compatible with all conventions. 126bdd1243dSDimitry Andric return; 127bdd1243dSDimitry Andric default: 128bdd1243dSDimitry Andric error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs)); 129bdd1243dSDimitry Andric return; 130bdd1243dSDimitry Andric } 131bdd1243dSDimitry Andric // Follow ld.bfd and error if there is a mix of calling conventions. 132bdd1243dSDimitry Andric if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default) 133bdd1243dSDimitry Andric error(toString(f) + ": incompatible Tag_ABI_VFP_args"); 134bdd1243dSDimitry Andric else 135bdd1243dSDimitry Andric config->armVFPArgs = arg; 136bdd1243dSDimitry Andric } 137bdd1243dSDimitry Andric 138bdd1243dSDimitry Andric // The ARM support in lld makes some use of instructions that are not available 139bdd1243dSDimitry Andric // on all ARM architectures. Namely: 140bdd1243dSDimitry Andric // - Use of BLX instruction for interworking between ARM and Thumb state. 141bdd1243dSDimitry Andric // - Use of the extended Thumb branch encoding in relocation. 142bdd1243dSDimitry Andric // - Use of the MOVT/MOVW instructions in Thumb Thunks. 143bdd1243dSDimitry Andric // The ARM Attributes section contains information about the architecture chosen 144bdd1243dSDimitry Andric // at compile time. We follow the convention that if at least one input object 145bdd1243dSDimitry Andric // is compiled with an architecture that supports these features then lld is 146bdd1243dSDimitry Andric // permitted to use them. 147bdd1243dSDimitry Andric static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) { 148bdd1243dSDimitry Andric std::optional<unsigned> attr = 149bdd1243dSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 150bdd1243dSDimitry Andric if (!attr) 151bdd1243dSDimitry Andric return; 152bdd1243dSDimitry Andric auto arch = *attr; 153bdd1243dSDimitry Andric switch (arch) { 154bdd1243dSDimitry Andric case ARMBuildAttrs::Pre_v4: 155bdd1243dSDimitry Andric case ARMBuildAttrs::v4: 156bdd1243dSDimitry Andric case ARMBuildAttrs::v4T: 157bdd1243dSDimitry Andric // Architectures prior to v5 do not support BLX instruction 158bdd1243dSDimitry Andric break; 159bdd1243dSDimitry Andric case ARMBuildAttrs::v5T: 160bdd1243dSDimitry Andric case ARMBuildAttrs::v5TE: 161bdd1243dSDimitry Andric case ARMBuildAttrs::v5TEJ: 162bdd1243dSDimitry Andric case ARMBuildAttrs::v6: 163bdd1243dSDimitry Andric case ARMBuildAttrs::v6KZ: 164bdd1243dSDimitry Andric case ARMBuildAttrs::v6K: 165bdd1243dSDimitry Andric config->armHasBlx = true; 166bdd1243dSDimitry Andric // Architectures used in pre-Cortex processors do not support 167bdd1243dSDimitry Andric // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 168bdd1243dSDimitry Andric // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 169bdd1243dSDimitry Andric break; 170bdd1243dSDimitry Andric default: 171bdd1243dSDimitry Andric // All other Architectures have BLX and extended branch encoding 172bdd1243dSDimitry Andric config->armHasBlx = true; 173bdd1243dSDimitry Andric config->armJ1J2BranchEncoding = true; 174bdd1243dSDimitry Andric if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M) 175bdd1243dSDimitry Andric // All Architectures used in Cortex processors with the exception 176bdd1243dSDimitry Andric // of v6-M and v6S-M have the MOVT and MOVW instructions. 177bdd1243dSDimitry Andric config->armHasMovtMovw = true; 178bdd1243dSDimitry Andric break; 179bdd1243dSDimitry Andric } 180*06c3fb27SDimitry Andric 181*06c3fb27SDimitry Andric // Only ARMv8-M or later architectures have CMSE support. 182*06c3fb27SDimitry Andric std::optional<unsigned> profile = 183*06c3fb27SDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile); 184*06c3fb27SDimitry Andric if (!profile) 185*06c3fb27SDimitry Andric return; 186*06c3fb27SDimitry Andric if (arch >= ARMBuildAttrs::CPUArch::v8_M_Base && 187*06c3fb27SDimitry Andric profile == ARMBuildAttrs::MicroControllerProfile) 188*06c3fb27SDimitry Andric config->armCMSESupport = true; 189bdd1243dSDimitry Andric } 190bdd1243dSDimitry Andric 1910b57cec5SDimitry Andric InputFile::InputFile(Kind k, MemoryBufferRef m) 1920b57cec5SDimitry Andric : mb(m), groupId(nextGroupId), fileKind(k) { 1930b57cec5SDimitry Andric // All files within the same --{start,end}-group get the same group ID. 1940b57cec5SDimitry Andric // Otherwise, a new file will get a new group ID. 1950b57cec5SDimitry Andric if (!isInGroup) 1960b57cec5SDimitry Andric ++nextGroupId; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 199bdd1243dSDimitry Andric std::optional<MemoryBufferRef> elf::readFile(StringRef path) { 200e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Load input files", path); 201e8d8bef9SDimitry Andric 2020b57cec5SDimitry Andric // The --chroot option changes our virtual root directory. 2030b57cec5SDimitry Andric // This is useful when you are dealing with files created by --reproduce. 204*06c3fb27SDimitry Andric if (!config->chroot.empty() && path.starts_with("/")) 20504eeddc0SDimitry Andric path = saver().save(config->chroot + path); 2060b57cec5SDimitry Andric 207*06c3fb27SDimitry Andric bool remapped = false; 208*06c3fb27SDimitry Andric auto it = config->remapInputs.find(path); 209*06c3fb27SDimitry Andric if (it != config->remapInputs.end()) { 210*06c3fb27SDimitry Andric path = it->second; 211*06c3fb27SDimitry Andric remapped = true; 212*06c3fb27SDimitry Andric } else { 213*06c3fb27SDimitry Andric for (const auto &[pat, toFile] : config->remapInputsWildcards) { 214*06c3fb27SDimitry Andric if (pat.match(path)) { 215*06c3fb27SDimitry Andric path = toFile; 216*06c3fb27SDimitry Andric remapped = true; 217*06c3fb27SDimitry Andric break; 218*06c3fb27SDimitry Andric } 219*06c3fb27SDimitry Andric } 220*06c3fb27SDimitry Andric } 221*06c3fb27SDimitry Andric if (remapped) { 222*06c3fb27SDimitry Andric // Use /dev/null to indicate an input file that should be ignored. Change 223*06c3fb27SDimitry Andric // the path to NUL on Windows. 224*06c3fb27SDimitry Andric #ifdef _WIN32 225*06c3fb27SDimitry Andric if (path == "/dev/null") 226*06c3fb27SDimitry Andric path = "NUL"; 227*06c3fb27SDimitry Andric #endif 228*06c3fb27SDimitry Andric } 229*06c3fb27SDimitry Andric 2300b57cec5SDimitry Andric log(path); 231e8d8bef9SDimitry Andric config->dependencyFiles.insert(llvm::CachedHashString(path)); 2320b57cec5SDimitry Andric 233fe6060f1SDimitry Andric auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, 234fe6060f1SDimitry Andric /*RequiresNullTerminator=*/false); 2350b57cec5SDimitry Andric if (auto ec = mbOrErr.getError()) { 2360b57cec5SDimitry Andric error("cannot open " + path + ": " + ec.message()); 237bdd1243dSDimitry Andric return std::nullopt; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 24004eeddc0SDimitry Andric MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef(); 241bdd1243dSDimitry Andric ctx.memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership 2420b57cec5SDimitry Andric 2430b57cec5SDimitry Andric if (tar) 2440b57cec5SDimitry Andric tar->append(relativeToRoot(path), mbref.getBuffer()); 2450b57cec5SDimitry Andric return mbref; 2460b57cec5SDimitry Andric } 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // All input object files must be for the same architecture 2490b57cec5SDimitry Andric // (e.g. it does not make sense to link x86 object files with 2500b57cec5SDimitry Andric // MIPS object files.) This function checks for that error. 2510b57cec5SDimitry Andric static bool isCompatible(InputFile *file) { 2520b57cec5SDimitry Andric if (!file->isElf() && !isa<BitcodeFile>(file)) 2530b57cec5SDimitry Andric return true; 2540b57cec5SDimitry Andric 2550b57cec5SDimitry Andric if (file->ekind == config->ekind && file->emachine == config->emachine) { 2560b57cec5SDimitry Andric if (config->emachine != EM_MIPS) 2570b57cec5SDimitry Andric return true; 2580b57cec5SDimitry Andric if (isMipsN32Abi(file) == config->mipsN32Abi) 2590b57cec5SDimitry Andric return true; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2625ffd83dbSDimitry Andric StringRef target = 2635ffd83dbSDimitry Andric !config->bfdname.empty() ? config->bfdname : config->emulation; 2645ffd83dbSDimitry Andric if (!target.empty()) { 2655ffd83dbSDimitry Andric error(toString(file) + " is incompatible with " + target); 26685868e8aSDimitry Andric return false; 26785868e8aSDimitry Andric } 26885868e8aSDimitry Andric 269d56accc7SDimitry Andric InputFile *existing = nullptr; 270bdd1243dSDimitry Andric if (!ctx.objectFiles.empty()) 271bdd1243dSDimitry Andric existing = ctx.objectFiles[0]; 272bdd1243dSDimitry Andric else if (!ctx.sharedFiles.empty()) 273bdd1243dSDimitry Andric existing = ctx.sharedFiles[0]; 274bdd1243dSDimitry Andric else if (!ctx.bitcodeFiles.empty()) 275bdd1243dSDimitry Andric existing = ctx.bitcodeFiles[0]; 276d56accc7SDimitry Andric std::string with; 277d56accc7SDimitry Andric if (existing) 278d56accc7SDimitry Andric with = " with " + toString(existing); 279d56accc7SDimitry Andric error(toString(file) + " is incompatible" + with); 2800b57cec5SDimitry Andric return false; 2810b57cec5SDimitry Andric } 2820b57cec5SDimitry Andric 2830b57cec5SDimitry Andric template <class ELFT> static void doParseFile(InputFile *file) { 2840b57cec5SDimitry Andric if (!isCompatible(file)) 2850b57cec5SDimitry Andric return; 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Binary file 2880b57cec5SDimitry Andric if (auto *f = dyn_cast<BinaryFile>(file)) { 289bdd1243dSDimitry Andric ctx.binaryFiles.push_back(f); 2900b57cec5SDimitry Andric f->parse(); 2910b57cec5SDimitry Andric return; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 2940b57cec5SDimitry Andric // Lazy object file 2950eae32dcSDimitry Andric if (file->lazy) { 2960eae32dcSDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 297bdd1243dSDimitry Andric ctx.lazyBitcodeFiles.push_back(f); 2980eae32dcSDimitry Andric f->parseLazy(); 2990eae32dcSDimitry Andric } else { 3000eae32dcSDimitry Andric cast<ObjFile<ELFT>>(file)->parseLazy(); 3010eae32dcSDimitry Andric } 3020b57cec5SDimitry Andric return; 3030b57cec5SDimitry Andric } 3040b57cec5SDimitry Andric 3050b57cec5SDimitry Andric if (config->trace) 3060b57cec5SDimitry Andric message(toString(file)); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric // .so file 3090b57cec5SDimitry Andric if (auto *f = dyn_cast<SharedFile>(file)) { 3100b57cec5SDimitry Andric f->parse<ELFT>(); 3110b57cec5SDimitry Andric return; 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric // LLVM bitcode file 3150b57cec5SDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 316bdd1243dSDimitry Andric ctx.bitcodeFiles.push_back(f); 317bdd1243dSDimitry Andric f->parse(); 3180b57cec5SDimitry Andric return; 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric // Regular object file 322bdd1243dSDimitry Andric ctx.objectFiles.push_back(cast<ELFFileBase>(file)); 3230b57cec5SDimitry Andric cast<ObjFile<ELFT>>(file)->parse(); 3240b57cec5SDimitry Andric } 3250b57cec5SDimitry Andric 3260b57cec5SDimitry Andric // Add symbols in File to the symbol table. 3271fd87a68SDimitry Andric void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); } 3280b57cec5SDimitry Andric 329*06c3fb27SDimitry Andric template <class ELFT> static void doParseArmCMSEImportLib(InputFile *file) { 330*06c3fb27SDimitry Andric cast<ObjFile<ELFT>>(file)->importCmseSymbols(); 331*06c3fb27SDimitry Andric } 332*06c3fb27SDimitry Andric 333*06c3fb27SDimitry Andric void elf::parseArmCMSEImportLib(InputFile *file) { 334*06c3fb27SDimitry Andric invokeELFT(doParseArmCMSEImportLib, file); 335*06c3fb27SDimitry Andric } 336*06c3fb27SDimitry Andric 3370b57cec5SDimitry Andric // Concatenates arguments to construct a string representing an error location. 3380b57cec5SDimitry Andric static std::string createFileLineMsg(StringRef path, unsigned line) { 3395ffd83dbSDimitry Andric std::string filename = std::string(path::filename(path)); 3400b57cec5SDimitry Andric std::string lineno = ":" + std::to_string(line); 3410b57cec5SDimitry Andric if (filename == path) 3420b57cec5SDimitry Andric return filename + lineno; 3430b57cec5SDimitry Andric return filename + lineno + " (" + path.str() + lineno + ")"; 3440b57cec5SDimitry Andric } 3450b57cec5SDimitry Andric 3460b57cec5SDimitry Andric template <class ELFT> 3470b57cec5SDimitry Andric static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym, 3480b57cec5SDimitry Andric InputSectionBase &sec, uint64_t offset) { 3490b57cec5SDimitry Andric // In DWARF, functions and variables are stored to different places. 3500b57cec5SDimitry Andric // First, look up a function for a given offset. 351bdd1243dSDimitry Andric if (std::optional<DILineInfo> info = file.getDILineInfo(&sec, offset)) 3520b57cec5SDimitry Andric return createFileLineMsg(info->FileName, info->Line); 3530b57cec5SDimitry Andric 3540b57cec5SDimitry Andric // If it failed, look up again as a variable. 355bdd1243dSDimitry Andric if (std::optional<std::pair<std::string, unsigned>> fileLine = 3560b57cec5SDimitry Andric file.getVariableLoc(sym.getName())) 3570b57cec5SDimitry Andric return createFileLineMsg(fileLine->first, fileLine->second); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric // File.sourceFile contains STT_FILE symbol, and that is a last resort. 3605ffd83dbSDimitry Andric return std::string(file.sourceFile); 3610b57cec5SDimitry Andric } 3620b57cec5SDimitry Andric 3630b57cec5SDimitry Andric std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec, 3640b57cec5SDimitry Andric uint64_t offset) { 3650b57cec5SDimitry Andric if (kind() != ObjKind) 3660b57cec5SDimitry Andric return ""; 367bdd1243dSDimitry Andric switch (ekind) { 3680b57cec5SDimitry Andric default: 3690b57cec5SDimitry Andric llvm_unreachable("Invalid kind"); 3700b57cec5SDimitry Andric case ELF32LEKind: 3710b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset); 3720b57cec5SDimitry Andric case ELF32BEKind: 3730b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset); 3740b57cec5SDimitry Andric case ELF64LEKind: 3750b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset); 3760b57cec5SDimitry Andric case ELF64BEKind: 3770b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset); 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric } 3800b57cec5SDimitry Andric 381e8d8bef9SDimitry Andric StringRef InputFile::getNameForScript() const { 382e8d8bef9SDimitry Andric if (archiveName.empty()) 383e8d8bef9SDimitry Andric return getName(); 384e8d8bef9SDimitry Andric 385e8d8bef9SDimitry Andric if (nameForScriptCache.empty()) 386e8d8bef9SDimitry Andric nameForScriptCache = (archiveName + Twine(':') + getName()).str(); 387e8d8bef9SDimitry Andric 388e8d8bef9SDimitry Andric return nameForScriptCache; 389e8d8bef9SDimitry Andric } 390e8d8bef9SDimitry Andric 391bdd1243dSDimitry Andric // An ELF object file may contain a `.deplibs` section. If it exists, the 392bdd1243dSDimitry Andric // section contains a list of library specifiers such as `m` for libm. This 393bdd1243dSDimitry Andric // function resolves a given name by finding the first matching library checking 394bdd1243dSDimitry Andric // the various ways that a library can be specified to LLD. This ELF extension 395bdd1243dSDimitry Andric // is a form of autolinking and is called `dependent libraries`. It is currently 396bdd1243dSDimitry Andric // unique to LLVM and lld. 397bdd1243dSDimitry Andric static void addDependentLibrary(StringRef specifier, const InputFile *f) { 398bdd1243dSDimitry Andric if (!config->dependentLibraries) 399bdd1243dSDimitry Andric return; 400bdd1243dSDimitry Andric if (std::optional<std::string> s = searchLibraryBaseName(specifier)) 401bdd1243dSDimitry Andric ctx.driver.addFile(saver().save(*s), /*withLOption=*/true); 402bdd1243dSDimitry Andric else if (std::optional<std::string> s = findFromSearchPaths(specifier)) 403bdd1243dSDimitry Andric ctx.driver.addFile(saver().save(*s), /*withLOption=*/true); 404bdd1243dSDimitry Andric else if (fs::exists(specifier)) 405bdd1243dSDimitry Andric ctx.driver.addFile(specifier, /*withLOption=*/false); 406bdd1243dSDimitry Andric else 407bdd1243dSDimitry Andric error(toString(f) + 408bdd1243dSDimitry Andric ": unable to find library from dependent library specifier: " + 409bdd1243dSDimitry Andric specifier); 410bdd1243dSDimitry Andric } 411bdd1243dSDimitry Andric 412bdd1243dSDimitry Andric // Record the membership of a section group so that in the garbage collection 413bdd1243dSDimitry Andric // pass, section group members are kept or discarded as a unit. 414bdd1243dSDimitry Andric template <class ELFT> 415bdd1243dSDimitry Andric static void handleSectionGroup(ArrayRef<InputSectionBase *> sections, 416bdd1243dSDimitry Andric ArrayRef<typename ELFT::Word> entries) { 417bdd1243dSDimitry Andric bool hasAlloc = false; 418bdd1243dSDimitry Andric for (uint32_t index : entries.slice(1)) { 419bdd1243dSDimitry Andric if (index >= sections.size()) 420bdd1243dSDimitry Andric return; 421bdd1243dSDimitry Andric if (InputSectionBase *s = sections[index]) 422bdd1243dSDimitry Andric if (s != &InputSection::discarded && s->flags & SHF_ALLOC) 423bdd1243dSDimitry Andric hasAlloc = true; 424bdd1243dSDimitry Andric } 425bdd1243dSDimitry Andric 426bdd1243dSDimitry Andric // If any member has the SHF_ALLOC flag, the whole group is subject to garbage 427bdd1243dSDimitry Andric // collection. See the comment in markLive(). This rule retains .debug_types 428bdd1243dSDimitry Andric // and .rela.debug_types. 429bdd1243dSDimitry Andric if (!hasAlloc) 430bdd1243dSDimitry Andric return; 431bdd1243dSDimitry Andric 432bdd1243dSDimitry Andric // Connect the members in a circular doubly-linked list via 433bdd1243dSDimitry Andric // nextInSectionGroup. 434bdd1243dSDimitry Andric InputSectionBase *head; 435bdd1243dSDimitry Andric InputSectionBase *prev = nullptr; 436bdd1243dSDimitry Andric for (uint32_t index : entries.slice(1)) { 437bdd1243dSDimitry Andric InputSectionBase *s = sections[index]; 438bdd1243dSDimitry Andric if (!s || s == &InputSection::discarded) 439bdd1243dSDimitry Andric continue; 440bdd1243dSDimitry Andric if (prev) 441bdd1243dSDimitry Andric prev->nextInSectionGroup = s; 442bdd1243dSDimitry Andric else 443bdd1243dSDimitry Andric head = s; 444bdd1243dSDimitry Andric prev = s; 445bdd1243dSDimitry Andric } 446bdd1243dSDimitry Andric if (prev) 447bdd1243dSDimitry Andric prev->nextInSectionGroup = head; 448bdd1243dSDimitry Andric } 449bdd1243dSDimitry Andric 4505ffd83dbSDimitry Andric template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() { 4515ffd83dbSDimitry Andric llvm::call_once(initDwarf, [this]() { 4525ffd83dbSDimitry Andric dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>( 4535ffd83dbSDimitry Andric std::make_unique<LLDDwarfObj<ELFT>>(this), "", 4545ffd83dbSDimitry Andric [&](Error err) { warn(getName() + ": " + toString(std::move(err))); }, 4555ffd83dbSDimitry Andric [&](Error warning) { 4565ffd83dbSDimitry Andric warn(getName() + ": " + toString(std::move(warning))); 4575ffd83dbSDimitry Andric })); 4585ffd83dbSDimitry Andric }); 4595ffd83dbSDimitry Andric 4605ffd83dbSDimitry Andric return dwarf.get(); 4610b57cec5SDimitry Andric } 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric // Returns the pair of file name and line number describing location of data 4640b57cec5SDimitry Andric // object (variable, array, etc) definition. 4650b57cec5SDimitry Andric template <class ELFT> 466bdd1243dSDimitry Andric std::optional<std::pair<std::string, unsigned>> 4670b57cec5SDimitry Andric ObjFile<ELFT>::getVariableLoc(StringRef name) { 4685ffd83dbSDimitry Andric return getDwarf()->getVariableLoc(name); 4690b57cec5SDimitry Andric } 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric // Returns source line information for a given offset 4720b57cec5SDimitry Andric // using DWARF debug info. 4730b57cec5SDimitry Andric template <class ELFT> 474bdd1243dSDimitry Andric std::optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s, 4750b57cec5SDimitry Andric uint64_t offset) { 4760b57cec5SDimitry Andric // Detect SectionIndex for specified section. 4770b57cec5SDimitry Andric uint64_t sectionIndex = object::SectionedAddress::UndefSection; 4780b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = s->file->getSections(); 4790b57cec5SDimitry Andric for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) { 4800b57cec5SDimitry Andric if (s == sections[curIndex]) { 4810b57cec5SDimitry Andric sectionIndex = curIndex; 4820b57cec5SDimitry Andric break; 4830b57cec5SDimitry Andric } 4840b57cec5SDimitry Andric } 4850b57cec5SDimitry Andric 4865ffd83dbSDimitry Andric return getDwarf()->getDILineInfo(offset, sectionIndex); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 489bdd1243dSDimitry Andric ELFFileBase::ELFFileBase(Kind k, ELFKind ekind, MemoryBufferRef mb) 490bdd1243dSDimitry Andric : InputFile(k, mb) { 491bdd1243dSDimitry Andric this->ekind = ekind; 4920b57cec5SDimitry Andric } 4930b57cec5SDimitry Andric 4940b57cec5SDimitry Andric template <typename Elf_Shdr> 4950b57cec5SDimitry Andric static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) { 4960b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) 4970b57cec5SDimitry Andric if (sec.sh_type == type) 4980b57cec5SDimitry Andric return &sec; 4990b57cec5SDimitry Andric return nullptr; 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 502bdd1243dSDimitry Andric void ELFFileBase::init() { 503bdd1243dSDimitry Andric switch (ekind) { 504bdd1243dSDimitry Andric case ELF32LEKind: 505bdd1243dSDimitry Andric init<ELF32LE>(fileKind); 506bdd1243dSDimitry Andric break; 507bdd1243dSDimitry Andric case ELF32BEKind: 508bdd1243dSDimitry Andric init<ELF32BE>(fileKind); 509bdd1243dSDimitry Andric break; 510bdd1243dSDimitry Andric case ELF64LEKind: 511bdd1243dSDimitry Andric init<ELF64LE>(fileKind); 512bdd1243dSDimitry Andric break; 513bdd1243dSDimitry Andric case ELF64BEKind: 514bdd1243dSDimitry Andric init<ELF64BE>(fileKind); 515bdd1243dSDimitry Andric break; 516bdd1243dSDimitry Andric default: 517bdd1243dSDimitry Andric llvm_unreachable("getELFKind"); 518bdd1243dSDimitry Andric } 519bdd1243dSDimitry Andric } 520bdd1243dSDimitry Andric 521bdd1243dSDimitry Andric template <class ELFT> void ELFFileBase::init(InputFile::Kind k) { 5220b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 5230b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric // Initialize trivial attributes. 5260b57cec5SDimitry Andric const ELFFile<ELFT> &obj = getObj<ELFT>(); 527e8d8bef9SDimitry Andric emachine = obj.getHeader().e_machine; 528e8d8bef9SDimitry Andric osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI]; 529e8d8bef9SDimitry Andric abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION]; 5300b57cec5SDimitry Andric 5310b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this); 5320eae32dcSDimitry Andric elfShdrs = sections.data(); 5330eae32dcSDimitry Andric numELFShdrs = sections.size(); 5340b57cec5SDimitry Andric 5350b57cec5SDimitry Andric // Find a symbol table. 5360b57cec5SDimitry Andric const Elf_Shdr *symtabSec = 537bdd1243dSDimitry Andric findSection(sections, k == SharedKind ? SHT_DYNSYM : SHT_SYMTAB); 5380b57cec5SDimitry Andric 5390b57cec5SDimitry Andric if (!symtabSec) 5400b57cec5SDimitry Andric return; 5410b57cec5SDimitry Andric 5420b57cec5SDimitry Andric // Initialize members corresponding to a symbol table. 5430b57cec5SDimitry Andric firstGlobal = symtabSec->sh_info; 5440b57cec5SDimitry Andric 5450b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this); 5460b57cec5SDimitry Andric if (firstGlobal == 0 || firstGlobal > eSyms.size()) 5470b57cec5SDimitry Andric fatal(toString(this) + ": invalid sh_info in symbol table"); 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric elfSyms = reinterpret_cast<const void *>(eSyms.data()); 5500eae32dcSDimitry Andric numELFSyms = uint32_t(eSyms.size()); 5510b57cec5SDimitry Andric stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this); 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric template <class ELFT> 5550b57cec5SDimitry Andric uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const { 5560b57cec5SDimitry Andric return CHECK( 557e8d8bef9SDimitry Andric this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable), 5580b57cec5SDimitry Andric this); 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) { 5621fd87a68SDimitry Andric object::ELFFile<ELFT> obj = this->getObj(); 5630b57cec5SDimitry Andric // Read a section table. justSymbols is usually false. 564bdd1243dSDimitry Andric if (this->justSymbols) { 5650b57cec5SDimitry Andric initializeJustSymbols(); 566bdd1243dSDimitry Andric initializeSymbols(obj); 567bdd1243dSDimitry Andric return; 568bdd1243dSDimitry Andric } 569bdd1243dSDimitry Andric 570bdd1243dSDimitry Andric // Handle dependent libraries and selection of section groups as these are not 571bdd1243dSDimitry Andric // done in parallel. 572bdd1243dSDimitry Andric ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>(); 573bdd1243dSDimitry Andric StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this); 574bdd1243dSDimitry Andric uint64_t size = objSections.size(); 575bdd1243dSDimitry Andric sections.resize(size); 576bdd1243dSDimitry Andric for (size_t i = 0; i != size; ++i) { 577bdd1243dSDimitry Andric const Elf_Shdr &sec = objSections[i]; 578bdd1243dSDimitry Andric if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) { 579bdd1243dSDimitry Andric StringRef name = check(obj.getSectionName(sec, shstrtab)); 580bdd1243dSDimitry Andric ArrayRef<char> data = CHECK( 581bdd1243dSDimitry Andric this->getObj().template getSectionContentsAsArray<char>(sec), this); 582bdd1243dSDimitry Andric if (!data.empty() && data.back() != '\0') { 583bdd1243dSDimitry Andric error( 584bdd1243dSDimitry Andric toString(this) + 585bdd1243dSDimitry Andric ": corrupted dependent libraries section (unterminated string): " + 586bdd1243dSDimitry Andric name); 587bdd1243dSDimitry Andric } else { 588bdd1243dSDimitry Andric for (const char *d = data.begin(), *e = data.end(); d < e;) { 589bdd1243dSDimitry Andric StringRef s(d); 590bdd1243dSDimitry Andric addDependentLibrary(s, this); 591bdd1243dSDimitry Andric d += s.size() + 1; 592bdd1243dSDimitry Andric } 593bdd1243dSDimitry Andric } 594bdd1243dSDimitry Andric this->sections[i] = &InputSection::discarded; 595bdd1243dSDimitry Andric continue; 596bdd1243dSDimitry Andric } 597bdd1243dSDimitry Andric 598bdd1243dSDimitry Andric if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) { 599bdd1243dSDimitry Andric ARMAttributeParser attributes; 600bdd1243dSDimitry Andric ArrayRef<uint8_t> contents = 601bdd1243dSDimitry Andric check(this->getObj().getSectionContents(sec)); 602bdd1243dSDimitry Andric StringRef name = check(obj.getSectionName(sec, shstrtab)); 603bdd1243dSDimitry Andric this->sections[i] = &InputSection::discarded; 604bdd1243dSDimitry Andric if (Error e = 605bdd1243dSDimitry Andric attributes.parse(contents, ekind == ELF32LEKind ? support::little 606bdd1243dSDimitry Andric : support::big)) { 607bdd1243dSDimitry Andric InputSection isec(*this, sec, name); 608bdd1243dSDimitry Andric warn(toString(&isec) + ": " + llvm::toString(std::move(e))); 609bdd1243dSDimitry Andric } else { 610bdd1243dSDimitry Andric updateSupportedARMFeatures(attributes); 611bdd1243dSDimitry Andric updateARMVFPArgs(attributes, this); 612bdd1243dSDimitry Andric 613bdd1243dSDimitry Andric // FIXME: Retain the first attribute section we see. The eglibc ARM 614bdd1243dSDimitry Andric // dynamic loaders require the presence of an attribute section for 615bdd1243dSDimitry Andric // dlopen to work. In a full implementation we would merge all attribute 616bdd1243dSDimitry Andric // sections. 617bdd1243dSDimitry Andric if (in.attributes == nullptr) { 618bdd1243dSDimitry Andric in.attributes = std::make_unique<InputSection>(*this, sec, name); 619bdd1243dSDimitry Andric this->sections[i] = in.attributes.get(); 620bdd1243dSDimitry Andric } 621bdd1243dSDimitry Andric } 622bdd1243dSDimitry Andric } 623bdd1243dSDimitry Andric 624bdd1243dSDimitry Andric if (sec.sh_type != SHT_GROUP) 625bdd1243dSDimitry Andric continue; 626bdd1243dSDimitry Andric StringRef signature = getShtGroupSignature(objSections, sec); 627bdd1243dSDimitry Andric ArrayRef<Elf_Word> entries = 628bdd1243dSDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this); 629bdd1243dSDimitry Andric if (entries.empty()) 630bdd1243dSDimitry Andric fatal(toString(this) + ": empty SHT_GROUP"); 631bdd1243dSDimitry Andric 632bdd1243dSDimitry Andric Elf_Word flag = entries[0]; 633bdd1243dSDimitry Andric if (flag && flag != GRP_COMDAT) 634bdd1243dSDimitry Andric fatal(toString(this) + ": unsupported SHT_GROUP format"); 635bdd1243dSDimitry Andric 636bdd1243dSDimitry Andric bool keepGroup = 637bdd1243dSDimitry Andric (flag & GRP_COMDAT) == 0 || ignoreComdats || 638bdd1243dSDimitry Andric symtab.comdatGroups.try_emplace(CachedHashStringRef(signature), this) 639bdd1243dSDimitry Andric .second; 640bdd1243dSDimitry Andric if (keepGroup) { 641bdd1243dSDimitry Andric if (config->relocatable) 642bdd1243dSDimitry Andric this->sections[i] = createInputSection( 643bdd1243dSDimitry Andric i, sec, check(obj.getSectionName(sec, shstrtab))); 644bdd1243dSDimitry Andric continue; 645bdd1243dSDimitry Andric } 646bdd1243dSDimitry Andric 647bdd1243dSDimitry Andric // Otherwise, discard group members. 648bdd1243dSDimitry Andric for (uint32_t secIndex : entries.slice(1)) { 649bdd1243dSDimitry Andric if (secIndex >= size) 650bdd1243dSDimitry Andric fatal(toString(this) + 651bdd1243dSDimitry Andric ": invalid section index in group: " + Twine(secIndex)); 652bdd1243dSDimitry Andric this->sections[secIndex] = &InputSection::discarded; 653bdd1243dSDimitry Andric } 654bdd1243dSDimitry Andric } 6550b57cec5SDimitry Andric 6560b57cec5SDimitry Andric // Read a symbol table. 6571fd87a68SDimitry Andric initializeSymbols(obj); 6580b57cec5SDimitry Andric } 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric // Sections with SHT_GROUP and comdat bits define comdat section groups. 6610b57cec5SDimitry Andric // They are identified and deduplicated by group name. This function 6620b57cec5SDimitry Andric // returns a group name. 6630b57cec5SDimitry Andric template <class ELFT> 6640b57cec5SDimitry Andric StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections, 6650b57cec5SDimitry Andric const Elf_Shdr &sec) { 6660b57cec5SDimitry Andric typename ELFT::SymRange symbols = this->getELFSyms<ELFT>(); 6670b57cec5SDimitry Andric if (sec.sh_info >= symbols.size()) 6680b57cec5SDimitry Andric fatal(toString(this) + ": invalid symbol index"); 6690b57cec5SDimitry Andric const typename ELFT::Sym &sym = symbols[sec.sh_info]; 670349cc55cSDimitry Andric return CHECK(sym.getName(this->stringTable), this); 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric 67385868e8aSDimitry Andric template <class ELFT> 67485868e8aSDimitry Andric bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) { 6750b57cec5SDimitry Andric // On a regular link we don't merge sections if -O0 (default is -O1). This 6760b57cec5SDimitry Andric // sometimes makes the linker significantly faster, although the output will 6770b57cec5SDimitry Andric // be bigger. 6780b57cec5SDimitry Andric // 6790b57cec5SDimitry Andric // Doing the same for -r would create a problem as it would combine sections 6800b57cec5SDimitry Andric // with different sh_entsize. One option would be to just copy every SHF_MERGE 6810b57cec5SDimitry Andric // section as is to the output. While this would produce a valid ELF file with 6820b57cec5SDimitry Andric // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 6830b57cec5SDimitry Andric // they see two .debug_str. We could have separate logic for combining 6840b57cec5SDimitry Andric // SHF_MERGE sections based both on their name and sh_entsize, but that seems 6850b57cec5SDimitry Andric // to be more trouble than it is worth. Instead, we just use the regular (-O1) 6860b57cec5SDimitry Andric // logic for -r. 6870b57cec5SDimitry Andric if (config->optimize == 0 && !config->relocatable) 6880b57cec5SDimitry Andric return false; 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric // A mergeable section with size 0 is useless because they don't have 6910b57cec5SDimitry Andric // any data to merge. A mergeable string section with size 0 can be 6920b57cec5SDimitry Andric // argued as invalid because it doesn't end with a null character. 6930b57cec5SDimitry Andric // We'll avoid a mess by handling them as if they were non-mergeable. 6940b57cec5SDimitry Andric if (sec.sh_size == 0) 6950b57cec5SDimitry Andric return false; 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric // Check for sh_entsize. The ELF spec is not clear about the zero 6980b57cec5SDimitry Andric // sh_entsize. It says that "the member [sh_entsize] contains 0 if 6990b57cec5SDimitry Andric // the section does not hold a table of fixed-size entries". We know 7000b57cec5SDimitry Andric // that Rust 1.13 produces a string mergeable section with a zero 7010b57cec5SDimitry Andric // sh_entsize. Here we just accept it rather than being picky about it. 7020b57cec5SDimitry Andric uint64_t entSize = sec.sh_entsize; 7030b57cec5SDimitry Andric if (entSize == 0) 7040b57cec5SDimitry Andric return false; 7050b57cec5SDimitry Andric if (sec.sh_size % entSize) 70685868e8aSDimitry Andric fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" + 70785868e8aSDimitry Andric Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" + 70885868e8aSDimitry Andric Twine(entSize) + ")"); 7090b57cec5SDimitry Andric 7105ffd83dbSDimitry Andric if (sec.sh_flags & SHF_WRITE) 71185868e8aSDimitry Andric fatal(toString(this) + ":(" + name + 71285868e8aSDimitry Andric "): writable SHF_MERGE section is not supported"); 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric return true; 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // This is for --just-symbols. 7180b57cec5SDimitry Andric // 7190b57cec5SDimitry Andric // --just-symbols is a very minor feature that allows you to link your 7200b57cec5SDimitry Andric // output against other existing program, so that if you load both your 7210b57cec5SDimitry Andric // program and the other program into memory, your output can refer the 7220b57cec5SDimitry Andric // other program's symbols. 7230b57cec5SDimitry Andric // 7240b57cec5SDimitry Andric // When the option is given, we link "just symbols". The section table is 7250b57cec5SDimitry Andric // initialized with null pointers. 7260b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 7270eae32dcSDimitry Andric sections.resize(numELFShdrs); 7280b57cec5SDimitry Andric } 7290b57cec5SDimitry Andric 7300b57cec5SDimitry Andric template <class ELFT> 7311fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSections(bool ignoreComdats, 7321fd87a68SDimitry Andric const llvm::object::ELFFile<ELFT> &obj) { 7330eae32dcSDimitry Andric ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>(); 734349cc55cSDimitry Andric StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this); 7350b57cec5SDimitry Andric uint64_t size = objSections.size(); 736bdd1243dSDimitry Andric SmallVector<ArrayRef<Elf_Word>, 0> selectedGroups; 73704eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 7380b57cec5SDimitry Andric if (this->sections[i] == &InputSection::discarded) 7390b57cec5SDimitry Andric continue; 7400b57cec5SDimitry Andric const Elf_Shdr &sec = objSections[i]; 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric // SHF_EXCLUDE'ed sections are discarded by the linker. However, 7430b57cec5SDimitry Andric // if -r is given, we'll let the final link discard such sections. 7440b57cec5SDimitry Andric // This is compatible with GNU. 7450b57cec5SDimitry Andric if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) { 7460eae32dcSDimitry Andric if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE) 7470eae32dcSDimitry Andric cgProfileSectionIndex = i; 7480b57cec5SDimitry Andric if (sec.sh_type == SHT_LLVM_ADDRSIG) { 7490b57cec5SDimitry Andric // We ignore the address-significance table if we know that the object 7500b57cec5SDimitry Andric // file was created by objcopy or ld -r. This is because these tools 7510b57cec5SDimitry Andric // will reorder the symbols in the symbol table, invalidating the data 7520b57cec5SDimitry Andric // in the address-significance table, which refers to symbols by index. 7530b57cec5SDimitry Andric if (sec.sh_link != 0) 7540b57cec5SDimitry Andric this->addrsigSec = &sec; 7550b57cec5SDimitry Andric else if (config->icf == ICFLevel::Safe) 756fe6060f1SDimitry Andric warn(toString(this) + 757fe6060f1SDimitry Andric ": --icf=safe conservatively ignores " 758fe6060f1SDimitry Andric "SHT_LLVM_ADDRSIG [index " + 759fe6060f1SDimitry Andric Twine(i) + 760fe6060f1SDimitry Andric "] with sh_link=0 " 761fe6060f1SDimitry Andric "(likely created using objcopy or ld -r)"); 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 7640b57cec5SDimitry Andric continue; 7650b57cec5SDimitry Andric } 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric switch (sec.sh_type) { 7680b57cec5SDimitry Andric case SHT_GROUP: { 769bdd1243dSDimitry Andric if (!config->relocatable) 770bdd1243dSDimitry Andric sections[i] = &InputSection::discarded; 771bdd1243dSDimitry Andric StringRef signature = 772bdd1243dSDimitry Andric cantFail(this->getELFSyms<ELFT>()[sec.sh_info].getName(stringTable)); 7730b57cec5SDimitry Andric ArrayRef<Elf_Word> entries = 774bdd1243dSDimitry Andric cantFail(obj.template getSectionContentsAsArray<Elf_Word>(sec)); 775bdd1243dSDimitry Andric if ((entries[0] & GRP_COMDAT) == 0 || ignoreComdats || 776bdd1243dSDimitry Andric symtab.comdatGroups.find(CachedHashStringRef(signature))->second == 777bdd1243dSDimitry Andric this) 778480093f4SDimitry Andric selectedGroups.push_back(entries); 7790b57cec5SDimitry Andric break; 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric case SHT_SYMTAB_SHNDX: 7820b57cec5SDimitry Andric shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this); 7830b57cec5SDimitry Andric break; 7840b57cec5SDimitry Andric case SHT_SYMTAB: 7850b57cec5SDimitry Andric case SHT_STRTAB: 7865ffd83dbSDimitry Andric case SHT_REL: 7875ffd83dbSDimitry Andric case SHT_RELA: 7880b57cec5SDimitry Andric case SHT_NULL: 7890b57cec5SDimitry Andric break; 79081ad6265SDimitry Andric case SHT_LLVM_SYMPART: 791bdd1243dSDimitry Andric ctx.hasSympart.store(true, std::memory_order_relaxed); 792bdd1243dSDimitry Andric [[fallthrough]]; 7930b57cec5SDimitry Andric default: 7941fd87a68SDimitry Andric this->sections[i] = 7951fd87a68SDimitry Andric createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab))); 7960b57cec5SDimitry Andric } 79785868e8aSDimitry Andric } 79885868e8aSDimitry Andric 7995ffd83dbSDimitry Andric // We have a second loop. It is used to: 8005ffd83dbSDimitry Andric // 1) handle SHF_LINK_ORDER sections. 8015ffd83dbSDimitry Andric // 2) create SHT_REL[A] sections. In some cases the section header index of a 8025ffd83dbSDimitry Andric // relocation section may be smaller than that of the relocated section. In 8035ffd83dbSDimitry Andric // such cases, the relocation section would attempt to reference a target 8045ffd83dbSDimitry Andric // section that has not yet been created. For simplicity, delay creation of 8055ffd83dbSDimitry Andric // relocation sections until now. 80604eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 80785868e8aSDimitry Andric if (this->sections[i] == &InputSection::discarded) 80885868e8aSDimitry Andric continue; 80985868e8aSDimitry Andric const Elf_Shdr &sec = objSections[i]; 8105ffd83dbSDimitry Andric 81104eeddc0SDimitry Andric if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) { 81204eeddc0SDimitry Andric // Find a relocation target section and associate this section with that. 81304eeddc0SDimitry Andric // Target may have been discarded if it is in a different section group 81404eeddc0SDimitry Andric // and the group is discarded, even though it's a violation of the spec. 81504eeddc0SDimitry Andric // We handle that situation gracefully by discarding dangling relocation 81604eeddc0SDimitry Andric // sections. 81704eeddc0SDimitry Andric const uint32_t info = sec.sh_info; 81804eeddc0SDimitry Andric InputSectionBase *s = getRelocTarget(i, sec, info); 81904eeddc0SDimitry Andric if (!s) 82004eeddc0SDimitry Andric continue; 82104eeddc0SDimitry Andric 82204eeddc0SDimitry Andric // ELF spec allows mergeable sections with relocations, but they are rare, 82304eeddc0SDimitry Andric // and it is in practice hard to merge such sections by contents, because 82404eeddc0SDimitry Andric // applying relocations at end of linking changes section contents. So, we 82504eeddc0SDimitry Andric // simply handle such sections as non-mergeable ones. Degrading like this 82604eeddc0SDimitry Andric // is acceptable because section merging is optional. 82704eeddc0SDimitry Andric if (auto *ms = dyn_cast<MergeInputSection>(s)) { 828bdd1243dSDimitry Andric s = makeThreadLocal<InputSection>( 829bdd1243dSDimitry Andric ms->file, ms->flags, ms->type, ms->addralign, 830bdd1243dSDimitry Andric ms->contentMaybeDecompress(), ms->name); 83104eeddc0SDimitry Andric sections[info] = s; 83204eeddc0SDimitry Andric } 83304eeddc0SDimitry Andric 83404eeddc0SDimitry Andric if (s->relSecIdx != 0) 83504eeddc0SDimitry Andric error( 83604eeddc0SDimitry Andric toString(s) + 83704eeddc0SDimitry Andric ": multiple relocation sections to one section are not supported"); 83804eeddc0SDimitry Andric s->relSecIdx = i; 83904eeddc0SDimitry Andric 84004eeddc0SDimitry Andric // Relocation sections are usually removed from the output, so return 84104eeddc0SDimitry Andric // `nullptr` for the normal case. However, if -r or --emit-relocs is 84204eeddc0SDimitry Andric // specified, we need to copy them to the output. (Some post link analysis 84304eeddc0SDimitry Andric // tools specify --emit-relocs to obtain the information.) 84404eeddc0SDimitry Andric if (config->copyRelocs) { 845bdd1243dSDimitry Andric auto *isec = makeThreadLocal<InputSection>( 84604eeddc0SDimitry Andric *this, sec, check(obj.getSectionName(sec, shstrtab))); 84704eeddc0SDimitry Andric // If the relocated section is discarded (due to /DISCARD/ or 84804eeddc0SDimitry Andric // --gc-sections), the relocation section should be discarded as well. 84904eeddc0SDimitry Andric s->dependentSections.push_back(isec); 85004eeddc0SDimitry Andric sections[i] = isec; 85104eeddc0SDimitry Andric } 85204eeddc0SDimitry Andric continue; 85304eeddc0SDimitry Andric } 8545ffd83dbSDimitry Andric 855e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have 856e8d8bef9SDimitry Andric // the flag. 85704eeddc0SDimitry Andric if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER)) 85885868e8aSDimitry Andric continue; 8590b57cec5SDimitry Andric 8600b57cec5SDimitry Andric InputSectionBase *linkSec = nullptr; 86104eeddc0SDimitry Andric if (sec.sh_link < size) 8620b57cec5SDimitry Andric linkSec = this->sections[sec.sh_link]; 8630b57cec5SDimitry Andric if (!linkSec) 86485868e8aSDimitry Andric fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link)); 8650b57cec5SDimitry Andric 866e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section is discarded if its linked-to section is 867e8d8bef9SDimitry Andric // discarded. 8680b57cec5SDimitry Andric InputSection *isec = cast<InputSection>(this->sections[i]); 8690b57cec5SDimitry Andric linkSec->dependentSections.push_back(isec); 8700b57cec5SDimitry Andric if (!isa<InputSection>(linkSec)) 8710b57cec5SDimitry Andric error("a section " + isec->name + 87285868e8aSDimitry Andric " with SHF_LINK_ORDER should not refer a non-regular section: " + 8730b57cec5SDimitry Andric toString(linkSec)); 8740b57cec5SDimitry Andric } 875480093f4SDimitry Andric 876480093f4SDimitry Andric for (ArrayRef<Elf_Word> entries : selectedGroups) 877480093f4SDimitry Andric handleSectionGroup<ELFT>(this->sections, entries); 8780b57cec5SDimitry Andric } 8790b57cec5SDimitry Andric 8800b57cec5SDimitry Andric // If a source file is compiled with x86 hardware-assisted call flow control 8810b57cec5SDimitry Andric // enabled, the generated object file contains feature flags indicating that 8820b57cec5SDimitry Andric // fact. This function reads the feature flags and returns it. 8830b57cec5SDimitry Andric // 8840b57cec5SDimitry Andric // Essentially we want to read a single 32-bit value in this function, but this 8850b57cec5SDimitry Andric // function is rather complicated because the value is buried deep inside a 8860b57cec5SDimitry Andric // .note.gnu.property section. 8870b57cec5SDimitry Andric // 8880b57cec5SDimitry Andric // The section consists of one or more NOTE records. Each NOTE record consists 8890b57cec5SDimitry Andric // of zero or more type-length-value fields. We want to find a field of a 8900b57cec5SDimitry Andric // certain type. It seems a bit too much to just store a 32-bit value, perhaps 8910b57cec5SDimitry Andric // the ABI is unnecessarily complicated. 892e8d8bef9SDimitry Andric template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { 8930b57cec5SDimitry Andric using Elf_Nhdr = typename ELFT::Nhdr; 8940b57cec5SDimitry Andric using Elf_Note = typename ELFT::Note; 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric uint32_t featuresSet = 0; 897bdd1243dSDimitry Andric ArrayRef<uint8_t> data = sec.content(); 898e8d8bef9SDimitry Andric auto reportFatal = [&](const uint8_t *place, const char *msg) { 899e8d8bef9SDimitry Andric fatal(toString(sec.file) + ":(" + sec.name + "+0x" + 900bdd1243dSDimitry Andric Twine::utohexstr(place - sec.content().data()) + "): " + msg); 901e8d8bef9SDimitry Andric }; 9020b57cec5SDimitry Andric while (!data.empty()) { 9030b57cec5SDimitry Andric // Read one NOTE record. 9040b57cec5SDimitry Andric auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data()); 905*06c3fb27SDimitry Andric if (data.size() < sizeof(Elf_Nhdr) || 906*06c3fb27SDimitry Andric data.size() < nhdr->getSize(sec.addralign)) 907e8d8bef9SDimitry Andric reportFatal(data.data(), "data is too short"); 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric Elf_Note note(*nhdr); 9100b57cec5SDimitry Andric if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") { 911*06c3fb27SDimitry Andric data = data.slice(nhdr->getSize(sec.addralign)); 9120b57cec5SDimitry Andric continue; 9130b57cec5SDimitry Andric } 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 9160b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 9170b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric // Read a body of a NOTE record, which consists of type-length-value fields. 920*06c3fb27SDimitry Andric ArrayRef<uint8_t> desc = note.getDesc(sec.addralign); 9210b57cec5SDimitry Andric while (!desc.empty()) { 922e8d8bef9SDimitry Andric const uint8_t *place = desc.data(); 9230b57cec5SDimitry Andric if (desc.size() < 8) 924e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 925e8d8bef9SDimitry Andric uint32_t type = read32<ELFT::TargetEndianness>(desc.data()); 926e8d8bef9SDimitry Andric uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4); 927e8d8bef9SDimitry Andric desc = desc.slice(8); 928e8d8bef9SDimitry Andric if (desc.size() < size) 929e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 9300b57cec5SDimitry Andric 9310b57cec5SDimitry Andric if (type == featureAndType) { 9320b57cec5SDimitry Andric // We found a FEATURE_1_AND field. There may be more than one of these 933480093f4SDimitry Andric // in a .note.gnu.property section, for a relocatable object we 9340b57cec5SDimitry Andric // accumulate the bits set. 935e8d8bef9SDimitry Andric if (size < 4) 936e8d8bef9SDimitry Andric reportFatal(place, "FEATURE_1_AND entry is too short"); 937e8d8bef9SDimitry Andric featuresSet |= read32<ELFT::TargetEndianness>(desc.data()); 9380b57cec5SDimitry Andric } 9390b57cec5SDimitry Andric 940e8d8bef9SDimitry Andric // Padding is present in the note descriptor, if necessary. 941e8d8bef9SDimitry Andric desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size)); 9420b57cec5SDimitry Andric } 9430b57cec5SDimitry Andric 9440b57cec5SDimitry Andric // Go to next NOTE record to look for more FEATURE_1_AND descriptions. 945*06c3fb27SDimitry Andric data = data.slice(nhdr->getSize(sec.addralign)); 9460b57cec5SDimitry Andric } 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric return featuresSet; 9490b57cec5SDimitry Andric } 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric template <class ELFT> 95204eeddc0SDimitry Andric InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, 95304eeddc0SDimitry Andric const Elf_Shdr &sec, 95404eeddc0SDimitry Andric uint32_t info) { 955349cc55cSDimitry Andric if (info < this->sections.size()) { 956349cc55cSDimitry Andric InputSectionBase *target = this->sections[info]; 9570b57cec5SDimitry Andric 9580b57cec5SDimitry Andric // Strictly speaking, a relocation section must be included in the 9590b57cec5SDimitry Andric // group of the section it relocates. However, LLVM 3.3 and earlier 9600b57cec5SDimitry Andric // would fail to do so, so we gracefully handle that case. 9610b57cec5SDimitry Andric if (target == &InputSection::discarded) 9620b57cec5SDimitry Andric return nullptr; 9630b57cec5SDimitry Andric 964349cc55cSDimitry Andric if (target != nullptr) 9650b57cec5SDimitry Andric return target; 9660b57cec5SDimitry Andric } 9670b57cec5SDimitry Andric 96804eeddc0SDimitry Andric error(toString(this) + Twine(": relocation section (index ") + Twine(idx) + 96904eeddc0SDimitry Andric ") has invalid sh_info (" + Twine(info) + ")"); 970349cc55cSDimitry Andric return nullptr; 971349cc55cSDimitry Andric } 972349cc55cSDimitry Andric 973bdd1243dSDimitry Andric // The function may be called concurrently for different input files. For 974bdd1243dSDimitry Andric // allocation, prefer makeThreadLocal which does not require holding a lock. 9750b57cec5SDimitry Andric template <class ELFT> 976349cc55cSDimitry Andric InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx, 977349cc55cSDimitry Andric const Elf_Shdr &sec, 9781fd87a68SDimitry Andric StringRef name) { 979*06c3fb27SDimitry Andric if (name.starts_with(".n")) { 9800b57cec5SDimitry Andric // The GNU linker uses .note.GNU-stack section as a marker indicating 9810b57cec5SDimitry Andric // that the code in the object file does not expect that the stack is 9820b57cec5SDimitry Andric // executable (in terms of NX bit). If all input files have the marker, 9830b57cec5SDimitry Andric // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 9840b57cec5SDimitry Andric // make the stack non-executable. Most object files have this section as 9850b57cec5SDimitry Andric // of 2017. 9860b57cec5SDimitry Andric // 9870b57cec5SDimitry Andric // But making the stack non-executable is a norm today for security 9880b57cec5SDimitry Andric // reasons. Failure to do so may result in a serious security issue. 9890b57cec5SDimitry Andric // Therefore, we make LLD always add PT_GNU_STACK unless it is 9900b57cec5SDimitry Andric // explicitly told to do otherwise (by -z execstack). Because the stack 9910b57cec5SDimitry Andric // executable-ness is controlled solely by command line options, 9920b57cec5SDimitry Andric // .note.GNU-stack sections are simply ignored. 9930b57cec5SDimitry Andric if (name == ".note.GNU-stack") 9940b57cec5SDimitry Andric return &InputSection::discarded; 9950b57cec5SDimitry Andric 9960b57cec5SDimitry Andric // Object files that use processor features such as Intel Control-Flow 9970b57cec5SDimitry Andric // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a 9980b57cec5SDimitry Andric // .note.gnu.property section containing a bitfield of feature bits like the 9990b57cec5SDimitry Andric // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag. 10000b57cec5SDimitry Andric // 10010b57cec5SDimitry Andric // Since we merge bitmaps from multiple object files to create a new 10020b57cec5SDimitry Andric // .note.gnu.property containing a single AND'ed bitmap, we discard an input 10030b57cec5SDimitry Andric // file's .note.gnu.property section. 10040b57cec5SDimitry Andric if (name == ".note.gnu.property") { 1005e8d8bef9SDimitry Andric this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name)); 10060b57cec5SDimitry Andric return &InputSection::discarded; 10070b57cec5SDimitry Andric } 10080b57cec5SDimitry Andric 10090b57cec5SDimitry Andric // Split stacks is a feature to support a discontiguous stack, 10100b57cec5SDimitry Andric // commonly used in the programming language Go. For the details, 10110b57cec5SDimitry Andric // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled 10120b57cec5SDimitry Andric // for split stack will include a .note.GNU-split-stack section. 10130b57cec5SDimitry Andric if (name == ".note.GNU-split-stack") { 10140b57cec5SDimitry Andric if (config->relocatable) { 10150eae32dcSDimitry Andric error( 10160eae32dcSDimitry Andric "cannot mix split-stack and non-split-stack in a relocatable link"); 10170b57cec5SDimitry Andric return &InputSection::discarded; 10180b57cec5SDimitry Andric } 10190b57cec5SDimitry Andric this->splitStack = true; 10200b57cec5SDimitry Andric return &InputSection::discarded; 10210b57cec5SDimitry Andric } 10220b57cec5SDimitry Andric 1023bdd1243dSDimitry Andric // An object file compiled for split stack, but where some of the 10240b57cec5SDimitry Andric // functions were compiled with the no_split_stack_attribute will 10250b57cec5SDimitry Andric // include a .note.GNU-no-split-stack section. 10260b57cec5SDimitry Andric if (name == ".note.GNU-no-split-stack") { 10270b57cec5SDimitry Andric this->someNoSplitStack = true; 10280b57cec5SDimitry Andric return &InputSection::discarded; 10290b57cec5SDimitry Andric } 10300b57cec5SDimitry Andric 10310eae32dcSDimitry Andric // Strip existing .note.gnu.build-id sections so that the output won't have 10320eae32dcSDimitry Andric // more than one build-id. This is not usually a problem because input 10330eae32dcSDimitry Andric // object files normally don't have .build-id sections, but you can create 10340eae32dcSDimitry Andric // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard 10350eae32dcSDimitry Andric // against it. 10360eae32dcSDimitry Andric if (name == ".note.gnu.build-id") 10370eae32dcSDimitry Andric return &InputSection::discarded; 10380eae32dcSDimitry Andric } 10390eae32dcSDimitry Andric 10400b57cec5SDimitry Andric // The linker merges EH (exception handling) frames and creates a 10410b57cec5SDimitry Andric // .eh_frame_hdr section for runtime. So we handle them with a special 10420b57cec5SDimitry Andric // class. For relocatable outputs, they are just passed through. 10430b57cec5SDimitry Andric if (name == ".eh_frame" && !config->relocatable) 1044bdd1243dSDimitry Andric return makeThreadLocal<EhInputSection>(*this, sec, name); 10450b57cec5SDimitry Andric 10460eae32dcSDimitry Andric if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name)) 1047bdd1243dSDimitry Andric return makeThreadLocal<MergeInputSection>(*this, sec, name); 1048bdd1243dSDimitry Andric return makeThreadLocal<InputSection>(*this, sec, name); 10490b57cec5SDimitry Andric } 10500b57cec5SDimitry Andric 1051*06c3fb27SDimitry Andric // Initialize symbols. symbols is a parallel array to the corresponding ELF 1052*06c3fb27SDimitry Andric // symbol table. 10531fd87a68SDimitry Andric template <class ELFT> 10541fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) { 10550eae32dcSDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 1056bdd1243dSDimitry Andric if (numSymbols == 0) { 1057bdd1243dSDimitry Andric numSymbols = eSyms.size(); 1058bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1059bdd1243dSDimitry Andric } 10600eae32dcSDimitry Andric 106181ad6265SDimitry Andric // Some entries have been filled by LazyObjFile. 106281ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) 106381ad6265SDimitry Andric if (!symbols[i]) 106481ad6265SDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 106581ad6265SDimitry Andric 106681ad6265SDimitry Andric // Perform symbol resolution on non-local symbols. 106781ad6265SDimitry Andric SmallVector<unsigned, 32> undefineds; 106881ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 106981ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 107081ad6265SDimitry Andric uint32_t secIdx = eSym.st_shndx; 107181ad6265SDimitry Andric if (secIdx == SHN_UNDEF) { 107281ad6265SDimitry Andric undefineds.push_back(i); 107381ad6265SDimitry Andric continue; 107481ad6265SDimitry Andric } 107581ad6265SDimitry Andric 107681ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 107781ad6265SDimitry Andric uint8_t stOther = eSym.st_other; 107881ad6265SDimitry Andric uint8_t type = eSym.getType(); 107981ad6265SDimitry Andric uint64_t value = eSym.st_value; 108081ad6265SDimitry Andric uint64_t size = eSym.st_size; 108181ad6265SDimitry Andric 108281ad6265SDimitry Andric Symbol *sym = symbols[i]; 108381ad6265SDimitry Andric sym->isUsedInRegularObj = true; 108481ad6265SDimitry Andric if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) { 108581ad6265SDimitry Andric if (value == 0 || value >= UINT32_MAX) 108681ad6265SDimitry Andric fatal(toString(this) + ": common symbol '" + sym->getName() + 108781ad6265SDimitry Andric "' has invalid alignment: " + Twine(value)); 108881ad6265SDimitry Andric hasCommonSyms = true; 108981ad6265SDimitry Andric sym->resolve( 109081ad6265SDimitry Andric CommonSymbol{this, StringRef(), binding, stOther, type, value, size}); 109181ad6265SDimitry Andric continue; 109281ad6265SDimitry Andric } 109381ad6265SDimitry Andric 109481ad6265SDimitry Andric // Handle global defined symbols. Defined::section will be set in postParse. 109581ad6265SDimitry Andric sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size, 109681ad6265SDimitry Andric nullptr}); 109781ad6265SDimitry Andric } 109881ad6265SDimitry Andric 109981ad6265SDimitry Andric // Undefined symbols (excluding those defined relative to non-prevailing 110081ad6265SDimitry Andric // sections) can trigger recursive extract. Process defined symbols first so 110181ad6265SDimitry Andric // that the relative order between a defined symbol and an undefined symbol 110281ad6265SDimitry Andric // does not change the symbol resolution behavior. In addition, a set of 110381ad6265SDimitry Andric // interconnected symbols will all be resolved to the same file, instead of 110481ad6265SDimitry Andric // being resolved to different files. 110581ad6265SDimitry Andric for (unsigned i : undefineds) { 110681ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 110781ad6265SDimitry Andric Symbol *sym = symbols[i]; 110881ad6265SDimitry Andric sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other, 110981ad6265SDimitry Andric eSym.getType()}); 111081ad6265SDimitry Andric sym->isUsedInRegularObj = true; 111181ad6265SDimitry Andric sym->referenced = true; 111281ad6265SDimitry Andric } 111381ad6265SDimitry Andric } 111481ad6265SDimitry Andric 1115bdd1243dSDimitry Andric template <class ELFT> 1116bdd1243dSDimitry Andric void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) { 1117bdd1243dSDimitry Andric if (!justSymbols) 1118bdd1243dSDimitry Andric initializeSections(ignoreComdats, getObj()); 1119bdd1243dSDimitry Andric 112081ad6265SDimitry Andric if (!firstGlobal) 112181ad6265SDimitry Andric return; 1122bdd1243dSDimitry Andric SymbolUnion *locals = makeThreadLocalN<SymbolUnion>(firstGlobal); 1123bdd1243dSDimitry Andric memset(locals, 0, sizeof(SymbolUnion) * firstGlobal); 112481ad6265SDimitry Andric 112581ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 11260eae32dcSDimitry Andric for (size_t i = 0, end = firstGlobal; i != end; ++i) { 11270b57cec5SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 11281fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 11291fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 11301fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 11311fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 11321fd87a68SDimitry Andric secIdx = 0; 11330eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 11340b57cec5SDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 11350eae32dcSDimitry Andric if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL)) 11365ffd83dbSDimitry Andric error(toString(this) + ": non-local symbol (" + Twine(i) + 11370eae32dcSDimitry Andric ") found at index < .symtab's sh_info (" + Twine(end) + ")"); 11385ffd83dbSDimitry Andric 11390eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 11405ffd83dbSDimitry Andric uint8_t type = eSym.getType(); 11415ffd83dbSDimitry Andric if (type == STT_FILE) 11420eae32dcSDimitry Andric sourceFile = CHECK(eSym.getName(stringTable), this); 11430eae32dcSDimitry Andric if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name)) 11445ffd83dbSDimitry Andric fatal(toString(this) + ": invalid symbol name offset"); 114504eeddc0SDimitry Andric StringRef name(stringTable.data() + eSym.st_name); 11465ffd83dbSDimitry Andric 11470eae32dcSDimitry Andric symbols[i] = reinterpret_cast<Symbol *>(locals + i); 11480eae32dcSDimitry Andric if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded) 11490eae32dcSDimitry Andric new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type, 11505ffd83dbSDimitry Andric /*discardedSecIdx=*/secIdx); 11515ffd83dbSDimitry Andric else 11520eae32dcSDimitry Andric new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type, 11530eae32dcSDimitry Andric eSym.st_value, eSym.st_size, sec); 1154bdd1243dSDimitry Andric symbols[i]->partition = 1; 115581ad6265SDimitry Andric symbols[i]->isUsedInRegularObj = true; 115681ad6265SDimitry Andric } 11575ffd83dbSDimitry Andric } 11585ffd83dbSDimitry Andric 115981ad6265SDimitry Andric // Called after all ObjFile::parse is called for all ObjFiles. This checks 116081ad6265SDimitry Andric // duplicate symbols and may do symbol property merge in the future. 116181ad6265SDimitry Andric template <class ELFT> void ObjFile<ELFT>::postParse() { 116281ad6265SDimitry Andric static std::mutex mu; 116381ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 11645ffd83dbSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 11655ffd83dbSDimitry Andric const Elf_Sym &eSym = eSyms[i]; 116681ad6265SDimitry Andric Symbol &sym = *symbols[i]; 11671fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 116881ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 116981ad6265SDimitry Andric if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK && 117081ad6265SDimitry Andric binding != STB_GNU_UNIQUE)) 117181ad6265SDimitry Andric errorOrWarn(toString(this) + ": symbol (" + Twine(i) + 117281ad6265SDimitry Andric ") has invalid binding: " + Twine((int)binding)); 117381ad6265SDimitry Andric 117481ad6265SDimitry Andric // st_value of STT_TLS represents the assigned offset, not the actual 117581ad6265SDimitry Andric // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can 117681ad6265SDimitry Andric // only be referenced by special TLS relocations. It is usually an error if 117781ad6265SDimitry Andric // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa. 117881ad6265SDimitry Andric if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS && 117981ad6265SDimitry Andric eSym.getType() != STT_NOTYPE) 118081ad6265SDimitry Andric errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " + 118181ad6265SDimitry Andric toString(sym.file) + "\n>>> in " + toString(this)); 118281ad6265SDimitry Andric 118381ad6265SDimitry Andric // Handle non-COMMON defined symbol below. !sym.file allows a symbol 118481ad6265SDimitry Andric // assignment to redefine a symbol without an error. 118581ad6265SDimitry Andric if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF || 118681ad6265SDimitry Andric secIdx == SHN_COMMON) 118781ad6265SDimitry Andric continue; 118881ad6265SDimitry Andric 11891fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 11901fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 11911fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 11921fd87a68SDimitry Andric secIdx = 0; 11930eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 11940eae32dcSDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 11950eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 11960b57cec5SDimitry Andric if (sec == &InputSection::discarded) { 119781ad6265SDimitry Andric if (sym.traced) { 119881ad6265SDimitry Andric printTraceSymbol(Undefined{this, sym.getName(), sym.binding, 119981ad6265SDimitry Andric sym.stOther, sym.type, secIdx}, 120081ad6265SDimitry Andric sym.getName()); 120181ad6265SDimitry Andric } 120281ad6265SDimitry Andric if (sym.file == this) { 120381ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1204bdd1243dSDimitry Andric ctx.nonPrevailingSyms.emplace_back(&sym, secIdx); 120581ad6265SDimitry Andric } 12060b57cec5SDimitry Andric continue; 12070b57cec5SDimitry Andric } 12080b57cec5SDimitry Andric 120981ad6265SDimitry Andric if (sym.file == this) { 121081ad6265SDimitry Andric cast<Defined>(sym).section = sec; 12110b57cec5SDimitry Andric continue; 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric 1214f3fd488fSDimitry Andric if (sym.binding == STB_WEAK || binding == STB_WEAK) 121581ad6265SDimitry Andric continue; 121681ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1217bdd1243dSDimitry Andric ctx.duplicates.push_back({&sym, this, sec, eSym.st_value}); 12180b57cec5SDimitry Andric } 12190b57cec5SDimitry Andric } 12200b57cec5SDimitry Andric 1221e8d8bef9SDimitry Andric // The handling of tentative definitions (COMMON symbols) in archives is murky. 1222fe6060f1SDimitry Andric // A tentative definition will be promoted to a global definition if there are 1223fe6060f1SDimitry Andric // no non-tentative definitions to dominate it. When we hold a tentative 1224fe6060f1SDimitry Andric // definition to a symbol and are inspecting archive members for inclusion 1225fe6060f1SDimitry Andric // there are 2 ways we can proceed: 1226e8d8bef9SDimitry Andric // 1227e8d8bef9SDimitry Andric // 1) Consider the tentative definition a 'real' definition (ie promotion from 1228e8d8bef9SDimitry Andric // tentative to real definition has already happened) and not inspect 1229e8d8bef9SDimitry Andric // archive members for Global/Weak definitions to replace the tentative 1230e8d8bef9SDimitry Andric // definition. An archive member would only be included if it satisfies some 1231e8d8bef9SDimitry Andric // other undefined symbol. This is the behavior Gold uses. 1232e8d8bef9SDimitry Andric // 1233e8d8bef9SDimitry Andric // 2) Consider the tentative definition as still undefined (ie the promotion to 1234fe6060f1SDimitry Andric // a real definition happens only after all symbol resolution is done). 1235fe6060f1SDimitry Andric // The linker searches archive members for STB_GLOBAL definitions to 1236e8d8bef9SDimitry Andric // replace the tentative definition with. This is the behavior used by 1237e8d8bef9SDimitry Andric // GNU ld. 1238e8d8bef9SDimitry Andric // 1239e8d8bef9SDimitry Andric // The second behavior is inherited from SysVR4, which based it on the FORTRAN 1240fe6060f1SDimitry Andric // COMMON BLOCK model. This behavior is needed for proper initialization in old 1241e8d8bef9SDimitry Andric // (pre F90) FORTRAN code that is packaged into an archive. 1242e8d8bef9SDimitry Andric // 1243fe6060f1SDimitry Andric // The following functions search archive members for definitions to replace 1244fe6060f1SDimitry Andric // tentative definitions (implementing behavior 2). 1245e8d8bef9SDimitry Andric static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName, 1246e8d8bef9SDimitry Andric StringRef archiveName) { 1247e8d8bef9SDimitry Andric IRSymtabFile symtabFile = check(readIRSymtab(mb)); 1248e8d8bef9SDimitry Andric for (const irsymtab::Reader::SymbolRef &sym : 1249e8d8bef9SDimitry Andric symtabFile.TheReader.symbols()) { 1250e8d8bef9SDimitry Andric if (sym.isGlobal() && sym.getName() == symName) 1251fe6060f1SDimitry Andric return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon(); 1252e8d8bef9SDimitry Andric } 1253e8d8bef9SDimitry Andric return false; 1254e8d8bef9SDimitry Andric } 1255e8d8bef9SDimitry Andric 1256e8d8bef9SDimitry Andric template <class ELFT> 1257bdd1243dSDimitry Andric static bool isNonCommonDef(ELFKind ekind, MemoryBufferRef mb, StringRef symName, 1258e8d8bef9SDimitry Andric StringRef archiveName) { 1259bdd1243dSDimitry Andric ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ekind, mb, archiveName); 1260bdd1243dSDimitry Andric obj->init(); 1261e8d8bef9SDimitry Andric StringRef stringtable = obj->getStringTable(); 1262e8d8bef9SDimitry Andric 1263e8d8bef9SDimitry Andric for (auto sym : obj->template getGlobalELFSyms<ELFT>()) { 1264e8d8bef9SDimitry Andric Expected<StringRef> name = sym.getName(stringtable); 1265e8d8bef9SDimitry Andric if (name && name.get() == symName) 1266fe6060f1SDimitry Andric return sym.isDefined() && sym.getBinding() == STB_GLOBAL && 1267fe6060f1SDimitry Andric !sym.isCommon(); 1268e8d8bef9SDimitry Andric } 1269e8d8bef9SDimitry Andric return false; 1270e8d8bef9SDimitry Andric } 1271e8d8bef9SDimitry Andric 1272e8d8bef9SDimitry Andric static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName, 1273e8d8bef9SDimitry Andric StringRef archiveName) { 1274e8d8bef9SDimitry Andric switch (getELFKind(mb, archiveName)) { 1275e8d8bef9SDimitry Andric case ELF32LEKind: 1276bdd1243dSDimitry Andric return isNonCommonDef<ELF32LE>(ELF32LEKind, mb, symName, archiveName); 1277e8d8bef9SDimitry Andric case ELF32BEKind: 1278bdd1243dSDimitry Andric return isNonCommonDef<ELF32BE>(ELF32BEKind, mb, symName, archiveName); 1279e8d8bef9SDimitry Andric case ELF64LEKind: 1280bdd1243dSDimitry Andric return isNonCommonDef<ELF64LE>(ELF64LEKind, mb, symName, archiveName); 1281e8d8bef9SDimitry Andric case ELF64BEKind: 1282bdd1243dSDimitry Andric return isNonCommonDef<ELF64BE>(ELF64BEKind, mb, symName, archiveName); 1283e8d8bef9SDimitry Andric default: 1284e8d8bef9SDimitry Andric llvm_unreachable("getELFKind"); 1285e8d8bef9SDimitry Andric } 1286e8d8bef9SDimitry Andric } 1287e8d8bef9SDimitry Andric 12880b57cec5SDimitry Andric unsigned SharedFile::vernauxNum; 12890b57cec5SDimitry Andric 1290bdd1243dSDimitry Andric SharedFile::SharedFile(MemoryBufferRef m, StringRef defaultSoName) 1291bdd1243dSDimitry Andric : ELFFileBase(SharedKind, getELFKind(m, ""), m), soName(defaultSoName), 1292bdd1243dSDimitry Andric isNeeded(!config->asNeeded) {} 1293bdd1243dSDimitry Andric 12940b57cec5SDimitry Andric // Parse the version definitions in the object file if present, and return a 12950b57cec5SDimitry Andric // vector whose nth element contains a pointer to the Elf_Verdef for version 12960b57cec5SDimitry Andric // identifier n. Version identifiers that are not definitions map to nullptr. 12970b57cec5SDimitry Andric template <typename ELFT> 12980eae32dcSDimitry Andric static SmallVector<const void *, 0> 12990eae32dcSDimitry Andric parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) { 13000b57cec5SDimitry Andric if (!sec) 13010b57cec5SDimitry Andric return {}; 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric // Build the Verdefs array by following the chain of Elf_Verdef objects 13040b57cec5SDimitry Andric // from the start of the .gnu.version_d section. 13050eae32dcSDimitry Andric SmallVector<const void *, 0> verdefs; 13060b57cec5SDimitry Andric const uint8_t *verdef = base + sec->sh_offset; 13070eae32dcSDimitry Andric for (unsigned i = 0, e = sec->sh_info; i != e; ++i) { 13080b57cec5SDimitry Andric auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef); 13090b57cec5SDimitry Andric verdef += curVerdef->vd_next; 13100b57cec5SDimitry Andric unsigned verdefIndex = curVerdef->vd_ndx; 13110eae32dcSDimitry Andric if (verdefIndex >= verdefs.size()) 13120b57cec5SDimitry Andric verdefs.resize(verdefIndex + 1); 13130b57cec5SDimitry Andric verdefs[verdefIndex] = curVerdef; 13140b57cec5SDimitry Andric } 13150b57cec5SDimitry Andric return verdefs; 13160b57cec5SDimitry Andric } 13170b57cec5SDimitry Andric 13185ffd83dbSDimitry Andric // Parse SHT_GNU_verneed to properly set the name of a versioned undefined 13195ffd83dbSDimitry Andric // symbol. We detect fatal issues which would cause vulnerabilities, but do not 13205ffd83dbSDimitry Andric // implement sophisticated error checking like in llvm-readobj because the value 13215ffd83dbSDimitry Andric // of such diagnostics is low. 13225ffd83dbSDimitry Andric template <typename ELFT> 13235ffd83dbSDimitry Andric std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj, 13245ffd83dbSDimitry Andric const typename ELFT::Shdr *sec) { 13255ffd83dbSDimitry Andric if (!sec) 13265ffd83dbSDimitry Andric return {}; 13275ffd83dbSDimitry Andric std::vector<uint32_t> verneeds; 1328e8d8bef9SDimitry Andric ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this); 13295ffd83dbSDimitry Andric const uint8_t *verneedBuf = data.begin(); 13305ffd83dbSDimitry Andric for (unsigned i = 0; i != sec->sh_info; ++i) { 13315ffd83dbSDimitry Andric if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) 13325ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Verneed"); 13335ffd83dbSDimitry Andric auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf); 13345ffd83dbSDimitry Andric const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux; 13355ffd83dbSDimitry Andric for (unsigned j = 0; j != vn->vn_cnt; ++j) { 13365ffd83dbSDimitry Andric if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) 13375ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Vernaux"); 13385ffd83dbSDimitry Andric auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf); 13395ffd83dbSDimitry Andric if (aux->vna_name >= this->stringTable.size()) 13405ffd83dbSDimitry Andric fatal(toString(this) + " has a Vernaux with an invalid vna_name"); 13415ffd83dbSDimitry Andric uint16_t version = aux->vna_other & VERSYM_VERSION; 13425ffd83dbSDimitry Andric if (version >= verneeds.size()) 13435ffd83dbSDimitry Andric verneeds.resize(version + 1); 13445ffd83dbSDimitry Andric verneeds[version] = aux->vna_name; 13455ffd83dbSDimitry Andric vernauxBuf += aux->vna_next; 13465ffd83dbSDimitry Andric } 13475ffd83dbSDimitry Andric verneedBuf += vn->vn_next; 13485ffd83dbSDimitry Andric } 13495ffd83dbSDimitry Andric return verneeds; 13505ffd83dbSDimitry Andric } 13515ffd83dbSDimitry Andric 13520b57cec5SDimitry Andric // We do not usually care about alignments of data in shared object 13530b57cec5SDimitry Andric // files because the loader takes care of it. However, if we promote a 13540b57cec5SDimitry Andric // DSO symbol to point to .bss due to copy relocation, we need to keep 13550b57cec5SDimitry Andric // the original alignment requirements. We infer it in this function. 13560b57cec5SDimitry Andric template <typename ELFT> 13570b57cec5SDimitry Andric static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections, 13580b57cec5SDimitry Andric const typename ELFT::Sym &sym) { 13590b57cec5SDimitry Andric uint64_t ret = UINT64_MAX; 13600b57cec5SDimitry Andric if (sym.st_value) 1361*06c3fb27SDimitry Andric ret = 1ULL << llvm::countr_zero((uint64_t)sym.st_value); 13620b57cec5SDimitry Andric if (0 < sym.st_shndx && sym.st_shndx < sections.size()) 13630b57cec5SDimitry Andric ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign); 13640b57cec5SDimitry Andric return (ret > UINT32_MAX) ? 0 : ret; 13650b57cec5SDimitry Andric } 13660b57cec5SDimitry Andric 13670b57cec5SDimitry Andric // Fully parse the shared object file. 13680b57cec5SDimitry Andric // 13690b57cec5SDimitry Andric // This function parses symbol versions. If a DSO has version information, 13700b57cec5SDimitry Andric // the file has a ".gnu.version_d" section which contains symbol version 13710b57cec5SDimitry Andric // definitions. Each symbol is associated to one version through a table in 13720b57cec5SDimitry Andric // ".gnu.version" section. That table is a parallel array for the symbol 13730b57cec5SDimitry Andric // table, and each table entry contains an index in ".gnu.version_d". 13740b57cec5SDimitry Andric // 13750b57cec5SDimitry Andric // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for 13760b57cec5SDimitry Andric // VER_NDX_GLOBAL. There's no table entry for these special versions in 13770b57cec5SDimitry Andric // ".gnu.version_d". 13780b57cec5SDimitry Andric // 13790b57cec5SDimitry Andric // The file format for symbol versioning is perhaps a bit more complicated 13800b57cec5SDimitry Andric // than necessary, but you can easily understand the code if you wrap your 13810b57cec5SDimitry Andric // head around the data structure described above. 13820b57cec5SDimitry Andric template <class ELFT> void SharedFile::parse() { 13830b57cec5SDimitry Andric using Elf_Dyn = typename ELFT::Dyn; 13840b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 13850b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 13860b57cec5SDimitry Andric using Elf_Verdef = typename ELFT::Verdef; 13870b57cec5SDimitry Andric using Elf_Versym = typename ELFT::Versym; 13880b57cec5SDimitry Andric 13890b57cec5SDimitry Andric ArrayRef<Elf_Dyn> dynamicTags; 13900b57cec5SDimitry Andric const ELFFile<ELFT> obj = this->getObj<ELFT>(); 13910eae32dcSDimitry Andric ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>(); 13920b57cec5SDimitry Andric 13930b57cec5SDimitry Andric const Elf_Shdr *versymSec = nullptr; 13940b57cec5SDimitry Andric const Elf_Shdr *verdefSec = nullptr; 13955ffd83dbSDimitry Andric const Elf_Shdr *verneedSec = nullptr; 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. 13980b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) { 13990b57cec5SDimitry Andric switch (sec.sh_type) { 14000b57cec5SDimitry Andric default: 14010b57cec5SDimitry Andric continue; 14020b57cec5SDimitry Andric case SHT_DYNAMIC: 14030b57cec5SDimitry Andric dynamicTags = 1404e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this); 14050b57cec5SDimitry Andric break; 14060b57cec5SDimitry Andric case SHT_GNU_versym: 14070b57cec5SDimitry Andric versymSec = &sec; 14080b57cec5SDimitry Andric break; 14090b57cec5SDimitry Andric case SHT_GNU_verdef: 14100b57cec5SDimitry Andric verdefSec = &sec; 14110b57cec5SDimitry Andric break; 14125ffd83dbSDimitry Andric case SHT_GNU_verneed: 14135ffd83dbSDimitry Andric verneedSec = &sec; 14145ffd83dbSDimitry Andric break; 14150b57cec5SDimitry Andric } 14160b57cec5SDimitry Andric } 14170b57cec5SDimitry Andric 14180b57cec5SDimitry Andric if (versymSec && numELFSyms == 0) { 14190b57cec5SDimitry Andric error("SHT_GNU_versym should be associated with symbol table"); 14200b57cec5SDimitry Andric return; 14210b57cec5SDimitry Andric } 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric // Search for a DT_SONAME tag to initialize this->soName. 14240b57cec5SDimitry Andric for (const Elf_Dyn &dyn : dynamicTags) { 14250b57cec5SDimitry Andric if (dyn.d_tag == DT_NEEDED) { 14260b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 14270b57cec5SDimitry Andric if (val >= this->stringTable.size()) 14280b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_NEEDED entry"); 14290b57cec5SDimitry Andric dtNeeded.push_back(this->stringTable.data() + val); 14300b57cec5SDimitry Andric } else if (dyn.d_tag == DT_SONAME) { 14310b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 14320b57cec5SDimitry Andric if (val >= this->stringTable.size()) 14330b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_SONAME entry"); 14340b57cec5SDimitry Andric soName = this->stringTable.data() + val; 14350b57cec5SDimitry Andric } 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric // DSOs are uniquified not by filename but by soname. 143904eeddc0SDimitry Andric DenseMap<CachedHashStringRef, SharedFile *>::iterator it; 14400b57cec5SDimitry Andric bool wasInserted; 144104eeddc0SDimitry Andric std::tie(it, wasInserted) = 1442bdd1243dSDimitry Andric symtab.soNames.try_emplace(CachedHashStringRef(soName), this); 14430b57cec5SDimitry Andric 14440b57cec5SDimitry Andric // If a DSO appears more than once on the command line with and without 14450b57cec5SDimitry Andric // --as-needed, --no-as-needed takes precedence over --as-needed because a 14460b57cec5SDimitry Andric // user can add an extra DSO with --no-as-needed to force it to be added to 14470b57cec5SDimitry Andric // the dependency list. 14480b57cec5SDimitry Andric it->second->isNeeded |= isNeeded; 14490b57cec5SDimitry Andric if (!wasInserted) 14500b57cec5SDimitry Andric return; 14510b57cec5SDimitry Andric 1452bdd1243dSDimitry Andric ctx.sharedFiles.push_back(this); 14530b57cec5SDimitry Andric 14540b57cec5SDimitry Andric verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec); 14555ffd83dbSDimitry Andric std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec); 14560b57cec5SDimitry Andric 14570b57cec5SDimitry Andric // Parse ".gnu.version" section which is a parallel array for the symbol 14580b57cec5SDimitry Andric // table. If a given file doesn't have a ".gnu.version" section, we use 14590b57cec5SDimitry Andric // VER_NDX_GLOBAL. 14600b57cec5SDimitry Andric size_t size = numELFSyms - firstGlobal; 14615ffd83dbSDimitry Andric std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL); 14620b57cec5SDimitry Andric if (versymSec) { 14630b57cec5SDimitry Andric ArrayRef<Elf_Versym> versym = 1464e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec), 14650b57cec5SDimitry Andric this) 14660b57cec5SDimitry Andric .slice(firstGlobal); 14670b57cec5SDimitry Andric for (size_t i = 0; i < size; ++i) 14680b57cec5SDimitry Andric versyms[i] = versym[i].vs_index; 14690b57cec5SDimitry Andric } 14700b57cec5SDimitry Andric 14710b57cec5SDimitry Andric // System libraries can have a lot of symbols with versions. Using a 14720b57cec5SDimitry Andric // fixed buffer for computing the versions name (foo@ver) can save a 14730b57cec5SDimitry Andric // lot of allocations. 14740b57cec5SDimitry Andric SmallString<0> versionedNameBuffer; 14750b57cec5SDimitry Andric 14760b57cec5SDimitry Andric // Add symbols to the symbol table. 14770b57cec5SDimitry Andric ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>(); 14780eae32dcSDimitry Andric for (size_t i = 0, e = syms.size(); i != e; ++i) { 14790b57cec5SDimitry Andric const Elf_Sym &sym = syms[i]; 14800b57cec5SDimitry Andric 14810b57cec5SDimitry Andric // ELF spec requires that all local symbols precede weak or global 14820b57cec5SDimitry Andric // symbols in each symbol table, and the index of first non-local symbol 14830b57cec5SDimitry Andric // is stored to sh_info. If a local symbol appears after some non-local 14840b57cec5SDimitry Andric // symbol, that's a violation of the spec. 14850eae32dcSDimitry Andric StringRef name = CHECK(sym.getName(stringTable), this); 14860b57cec5SDimitry Andric if (sym.getBinding() == STB_LOCAL) { 1487bdd1243dSDimitry Andric errorOrWarn(toString(this) + ": invalid local symbol '" + name + 1488bdd1243dSDimitry Andric "' in global part of symbol table"); 14890b57cec5SDimitry Andric continue; 14900b57cec5SDimitry Andric } 14910b57cec5SDimitry Andric 1492bdd1243dSDimitry Andric const uint16_t ver = versyms[i], idx = ver & ~VERSYM_HIDDEN; 14930b57cec5SDimitry Andric if (sym.isUndefined()) { 14945ffd83dbSDimitry Andric // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but 14955ffd83dbSDimitry Andric // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL. 1496bdd1243dSDimitry Andric if (ver != VER_NDX_LOCAL && ver != VER_NDX_GLOBAL) { 14975ffd83dbSDimitry Andric if (idx >= verneeds.size()) { 14985ffd83dbSDimitry Andric error("corrupt input file: version need index " + Twine(idx) + 14995ffd83dbSDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 15005ffd83dbSDimitry Andric toString(this)); 15015ffd83dbSDimitry Andric continue; 15025ffd83dbSDimitry Andric } 15030eae32dcSDimitry Andric StringRef verName = stringTable.data() + verneeds[idx]; 15045ffd83dbSDimitry Andric versionedNameBuffer.clear(); 150504eeddc0SDimitry Andric name = saver().save( 150604eeddc0SDimitry Andric (name + "@" + verName).toStringRef(versionedNameBuffer)); 15075ffd83dbSDimitry Andric } 15080eae32dcSDimitry Andric Symbol *s = symtab.addSymbol( 15090b57cec5SDimitry Andric Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()}); 15100b57cec5SDimitry Andric s->exportDynamic = true; 15110eae32dcSDimitry Andric if (s->isUndefined() && sym.getBinding() != STB_WEAK && 1512fe6060f1SDimitry Andric config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) 1513fe6060f1SDimitry Andric requiredSymbols.push_back(s); 15140b57cec5SDimitry Andric continue; 15150b57cec5SDimitry Andric } 15160b57cec5SDimitry Andric 1517bdd1243dSDimitry Andric if (ver == VER_NDX_LOCAL || 1518bdd1243dSDimitry Andric (ver != VER_NDX_GLOBAL && idx >= verdefs.size())) { 1519bdd1243dSDimitry Andric // In GNU ld < 2.31 (before 3be08ea4728b56d35e136af4e6fd3086ade17764), the 1520bdd1243dSDimitry Andric // MIPS port puts _gp_disp symbol into DSO files and incorrectly assigns 1521bdd1243dSDimitry Andric // VER_NDX_LOCAL. Workaround this bug. 1522bdd1243dSDimitry Andric if (config->emachine == EM_MIPS && name == "_gp_disp") 15230b57cec5SDimitry Andric continue; 15240b57cec5SDimitry Andric error("corrupt input file: version definition index " + Twine(idx) + 15250b57cec5SDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 15260b57cec5SDimitry Andric toString(this)); 15270b57cec5SDimitry Andric continue; 15280b57cec5SDimitry Andric } 15290b57cec5SDimitry Andric 1530bdd1243dSDimitry Andric uint32_t alignment = getAlignment<ELFT>(sections, sym); 1531bdd1243dSDimitry Andric if (ver == idx) { 1532bdd1243dSDimitry Andric auto *s = symtab.addSymbol( 1533bdd1243dSDimitry Andric SharedSymbol{*this, name, sym.getBinding(), sym.st_other, 1534bdd1243dSDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 1535bdd1243dSDimitry Andric if (s->file == this) 1536bdd1243dSDimitry Andric s->verdefIndex = ver; 1537bdd1243dSDimitry Andric } 1538bdd1243dSDimitry Andric 1539bdd1243dSDimitry Andric // Also add the symbol with the versioned name to handle undefined symbols 1540bdd1243dSDimitry Andric // with explicit versions. 1541bdd1243dSDimitry Andric if (ver == VER_NDX_GLOBAL) 1542bdd1243dSDimitry Andric continue; 1543bdd1243dSDimitry Andric 15440b57cec5SDimitry Andric StringRef verName = 15450eae32dcSDimitry Andric stringTable.data() + 15460b57cec5SDimitry Andric reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name; 15470b57cec5SDimitry Andric versionedNameBuffer.clear(); 15480b57cec5SDimitry Andric name = (name + "@" + verName).toStringRef(versionedNameBuffer); 154981ad6265SDimitry Andric auto *s = symtab.addSymbol( 155081ad6265SDimitry Andric SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other, 155181ad6265SDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 155281ad6265SDimitry Andric if (s->file == this) 155381ad6265SDimitry Andric s->verdefIndex = idx; 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric } 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric static ELFKind getBitcodeELFKind(const Triple &t) { 15580b57cec5SDimitry Andric if (t.isLittleEndian()) 15590b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 15600b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 15610b57cec5SDimitry Andric } 15620b57cec5SDimitry Andric 1563e8d8bef9SDimitry Andric static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) { 15640b57cec5SDimitry Andric switch (t.getArch()) { 15650b57cec5SDimitry Andric case Triple::aarch64: 1566fe6060f1SDimitry Andric case Triple::aarch64_be: 15670b57cec5SDimitry Andric return EM_AARCH64; 15680b57cec5SDimitry Andric case Triple::amdgcn: 15690b57cec5SDimitry Andric case Triple::r600: 15700b57cec5SDimitry Andric return EM_AMDGPU; 15710b57cec5SDimitry Andric case Triple::arm: 15720b57cec5SDimitry Andric case Triple::thumb: 15730b57cec5SDimitry Andric return EM_ARM; 15740b57cec5SDimitry Andric case Triple::avr: 15750b57cec5SDimitry Andric return EM_AVR; 1576349cc55cSDimitry Andric case Triple::hexagon: 1577349cc55cSDimitry Andric return EM_HEXAGON; 1578*06c3fb27SDimitry Andric case Triple::loongarch32: 1579*06c3fb27SDimitry Andric case Triple::loongarch64: 1580*06c3fb27SDimitry Andric return EM_LOONGARCH; 15810b57cec5SDimitry Andric case Triple::mips: 15820b57cec5SDimitry Andric case Triple::mipsel: 15830b57cec5SDimitry Andric case Triple::mips64: 15840b57cec5SDimitry Andric case Triple::mips64el: 15850b57cec5SDimitry Andric return EM_MIPS; 15860b57cec5SDimitry Andric case Triple::msp430: 15870b57cec5SDimitry Andric return EM_MSP430; 15880b57cec5SDimitry Andric case Triple::ppc: 1589e8d8bef9SDimitry Andric case Triple::ppcle: 15900b57cec5SDimitry Andric return EM_PPC; 15910b57cec5SDimitry Andric case Triple::ppc64: 15920b57cec5SDimitry Andric case Triple::ppc64le: 15930b57cec5SDimitry Andric return EM_PPC64; 15940b57cec5SDimitry Andric case Triple::riscv32: 15950b57cec5SDimitry Andric case Triple::riscv64: 15960b57cec5SDimitry Andric return EM_RISCV; 15970b57cec5SDimitry Andric case Triple::x86: 15980b57cec5SDimitry Andric return t.isOSIAMCU() ? EM_IAMCU : EM_386; 15990b57cec5SDimitry Andric case Triple::x86_64: 16000b57cec5SDimitry Andric return EM_X86_64; 16010b57cec5SDimitry Andric default: 16020b57cec5SDimitry Andric error(path + ": could not infer e_machine from bitcode target triple " + 16030b57cec5SDimitry Andric t.str()); 16040b57cec5SDimitry Andric return EM_NONE; 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric } 16070b57cec5SDimitry Andric 1608e8d8bef9SDimitry Andric static uint8_t getOsAbi(const Triple &t) { 1609e8d8bef9SDimitry Andric switch (t.getOS()) { 1610e8d8bef9SDimitry Andric case Triple::AMDHSA: 1611e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_HSA; 1612e8d8bef9SDimitry Andric case Triple::AMDPAL: 1613e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_PAL; 1614e8d8bef9SDimitry Andric case Triple::Mesa3D: 1615e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_MESA3D; 1616e8d8bef9SDimitry Andric default: 1617e8d8bef9SDimitry Andric return ELF::ELFOSABI_NONE; 1618e8d8bef9SDimitry Andric } 1619e8d8bef9SDimitry Andric } 1620e8d8bef9SDimitry Andric 16210b57cec5SDimitry Andric BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName, 16220eae32dcSDimitry Andric uint64_t offsetInArchive, bool lazy) 16230b57cec5SDimitry Andric : InputFile(BitcodeKind, mb) { 16240eae32dcSDimitry Andric this->archiveName = archiveName; 16250eae32dcSDimitry Andric this->lazy = lazy; 16260b57cec5SDimitry Andric 16270b57cec5SDimitry Andric std::string path = mb.getBufferIdentifier().str(); 16280b57cec5SDimitry Andric if (config->thinLTOIndexOnly) 16290b57cec5SDimitry Andric path = replaceThinLTOSuffix(mb.getBufferIdentifier()); 16300b57cec5SDimitry Andric 16310b57cec5SDimitry Andric // ThinLTO assumes that all MemoryBufferRefs given to it have a unique 16320b57cec5SDimitry Andric // name. If two archives define two members with the same name, this 16330b57cec5SDimitry Andric // causes a collision which result in only one of the objects being taken 16340b57cec5SDimitry Andric // into consideration at LTO time (which very likely causes undefined 16350b57cec5SDimitry Andric // symbols later in the link stage). So we append file offset to make 16360b57cec5SDimitry Andric // filename unique. 163704eeddc0SDimitry Andric StringRef name = archiveName.empty() 163804eeddc0SDimitry Andric ? saver().save(path) 163904eeddc0SDimitry Andric : saver().save(archiveName + "(" + path::filename(path) + 164004eeddc0SDimitry Andric " at " + utostr(offsetInArchive) + ")"); 16410b57cec5SDimitry Andric MemoryBufferRef mbref(mb.getBuffer(), name); 16420b57cec5SDimitry Andric 16430b57cec5SDimitry Andric obj = CHECK(lto::InputFile::create(mbref), this); 16440b57cec5SDimitry Andric 16450b57cec5SDimitry Andric Triple t(obj->getTargetTriple()); 16460b57cec5SDimitry Andric ekind = getBitcodeELFKind(t); 16470b57cec5SDimitry Andric emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t); 1648e8d8bef9SDimitry Andric osabi = getOsAbi(t); 16490b57cec5SDimitry Andric } 16500b57cec5SDimitry Andric 16510b57cec5SDimitry Andric static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 16520b57cec5SDimitry Andric switch (gvVisibility) { 16530b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: 16540b57cec5SDimitry Andric return STV_DEFAULT; 16550b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: 16560b57cec5SDimitry Andric return STV_HIDDEN; 16570b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: 16580b57cec5SDimitry Andric return STV_PROTECTED; 16590b57cec5SDimitry Andric } 16600b57cec5SDimitry Andric llvm_unreachable("unknown visibility"); 16610b57cec5SDimitry Andric } 16620b57cec5SDimitry Andric 166304eeddc0SDimitry Andric static void 166404eeddc0SDimitry Andric createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats, 166504eeddc0SDimitry Andric const lto::InputFile::Symbol &objSym, BitcodeFile &f) { 16660b57cec5SDimitry Andric uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL; 16670b57cec5SDimitry Andric uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE; 16680b57cec5SDimitry Andric uint8_t visibility = mapVisibility(objSym.getVisibility()); 16690b57cec5SDimitry Andric 167081ad6265SDimitry Andric if (!sym) 1671bdd1243dSDimitry Andric sym = symtab.insert(saver().save(objSym.getName())); 167204eeddc0SDimitry Andric 16730b57cec5SDimitry Andric int c = objSym.getComdatIndex(); 16740b57cec5SDimitry Andric if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) { 167581ad6265SDimitry Andric Undefined newSym(&f, StringRef(), binding, visibility, type); 167604eeddc0SDimitry Andric sym->resolve(newSym); 167704eeddc0SDimitry Andric sym->referenced = true; 167804eeddc0SDimitry Andric return; 16790b57cec5SDimitry Andric } 16800b57cec5SDimitry Andric 168104eeddc0SDimitry Andric if (objSym.isCommon()) { 168281ad6265SDimitry Andric sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT, 168304eeddc0SDimitry Andric objSym.getCommonAlignment(), 168404eeddc0SDimitry Andric objSym.getCommonSize()}); 168504eeddc0SDimitry Andric } else { 168681ad6265SDimitry Andric Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr); 168781ad6265SDimitry Andric if (objSym.canBeOmittedFromSymbolTable()) 168885868e8aSDimitry Andric newSym.exportDynamic = false; 168904eeddc0SDimitry Andric sym->resolve(newSym); 169004eeddc0SDimitry Andric } 16910b57cec5SDimitry Andric } 16920b57cec5SDimitry Andric 1693bdd1243dSDimitry Andric void BitcodeFile::parse() { 1694fe6060f1SDimitry Andric for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) { 16950b57cec5SDimitry Andric keptComdats.push_back( 1696fe6060f1SDimitry Andric s.second == Comdat::NoDeduplicate || 1697bdd1243dSDimitry Andric symtab.comdatGroups.try_emplace(CachedHashStringRef(s.first), this) 1698fe6060f1SDimitry Andric .second); 1699fe6060f1SDimitry Andric } 17000b57cec5SDimitry Andric 1701bdd1243dSDimitry Andric if (numSymbols == 0) { 1702bdd1243dSDimitry Andric numSymbols = obj->symbols().size(); 1703bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1704bdd1243dSDimitry Andric } 170581ad6265SDimitry Andric // Process defined symbols first. See the comment in 170681ad6265SDimitry Andric // ObjFile<ELFT>::initializeSymbols. 1707bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1708bdd1243dSDimitry Andric if (!irSym.isUndefined()) 1709bdd1243dSDimitry Andric createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); 1710bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1711bdd1243dSDimitry Andric if (irSym.isUndefined()) 1712bdd1243dSDimitry Andric createBitcodeSymbol(symbols[i], keptComdats, irSym, *this); 17130b57cec5SDimitry Andric 17140b57cec5SDimitry Andric for (auto l : obj->getDependentLibraries()) 17150b57cec5SDimitry Andric addDependentLibrary(l, this); 17160b57cec5SDimitry Andric } 17170b57cec5SDimitry Andric 17180eae32dcSDimitry Andric void BitcodeFile::parseLazy() { 1719bdd1243dSDimitry Andric numSymbols = obj->symbols().size(); 1720bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 1721bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) 1722bdd1243dSDimitry Andric if (!irSym.isUndefined()) { 1723bdd1243dSDimitry Andric auto *sym = symtab.insert(saver().save(irSym.getName())); 172481ad6265SDimitry Andric sym->resolve(LazyObject{*this}); 1725bdd1243dSDimitry Andric symbols[i] = sym; 172681ad6265SDimitry Andric } 172781ad6265SDimitry Andric } 172881ad6265SDimitry Andric 172981ad6265SDimitry Andric void BitcodeFile::postParse() { 1730bdd1243dSDimitry Andric for (auto [i, irSym] : llvm::enumerate(obj->symbols())) { 1731bdd1243dSDimitry Andric const Symbol &sym = *symbols[i]; 1732bdd1243dSDimitry Andric if (sym.file == this || !sym.isDefined() || irSym.isUndefined() || 1733bdd1243dSDimitry Andric irSym.isCommon() || irSym.isWeak()) 173481ad6265SDimitry Andric continue; 1735bdd1243dSDimitry Andric int c = irSym.getComdatIndex(); 173681ad6265SDimitry Andric if (c != -1 && !keptComdats[c]) 173781ad6265SDimitry Andric continue; 173881ad6265SDimitry Andric reportDuplicate(sym, this, nullptr, 0); 173981ad6265SDimitry Andric } 17400eae32dcSDimitry Andric } 17410eae32dcSDimitry Andric 17420b57cec5SDimitry Andric void BinaryFile::parse() { 17430b57cec5SDimitry Andric ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer()); 17440b57cec5SDimitry Andric auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 17450b57cec5SDimitry Andric 8, data, ".data"); 17460b57cec5SDimitry Andric sections.push_back(section); 17470b57cec5SDimitry Andric 17480b57cec5SDimitry Andric // For each input file foo that is embedded to a result as a binary 17490b57cec5SDimitry Andric // blob, we define _binary_foo_{start,end,size} symbols, so that 17500b57cec5SDimitry Andric // user programs can access blobs by name. Non-alphanumeric 17510b57cec5SDimitry Andric // characters in a filename are replaced with underscore. 17520b57cec5SDimitry Andric std::string s = "_binary_" + mb.getBufferIdentifier().str(); 1753*06c3fb27SDimitry Andric for (char &c : s) 1754*06c3fb27SDimitry Andric if (!isAlnum(c)) 1755*06c3fb27SDimitry Andric c = '_'; 17560b57cec5SDimitry Andric 175704eeddc0SDimitry Andric llvm::StringSaver &saver = lld::saver(); 175804eeddc0SDimitry Andric 1759bdd1243dSDimitry Andric symtab.addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_start"), 1760bdd1243dSDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 0, 0, 1761bdd1243dSDimitry Andric section}); 1762bdd1243dSDimitry Andric symtab.addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_end"), 176381ad6265SDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 176481ad6265SDimitry Andric data.size(), 0, section}); 1765bdd1243dSDimitry Andric symtab.addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_size"), 176681ad6265SDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 176781ad6265SDimitry Andric data.size(), 0, nullptr}); 17680b57cec5SDimitry Andric } 17690b57cec5SDimitry Andric 1770fcaf7f86SDimitry Andric ELFFileBase *elf::createObjFile(MemoryBufferRef mb, StringRef archiveName, 1771fcaf7f86SDimitry Andric bool lazy) { 1772fcaf7f86SDimitry Andric ELFFileBase *f; 17730b57cec5SDimitry Andric switch (getELFKind(mb, archiveName)) { 17740b57cec5SDimitry Andric case ELF32LEKind: 1775bdd1243dSDimitry Andric f = make<ObjFile<ELF32LE>>(ELF32LEKind, mb, archiveName); 1776fcaf7f86SDimitry Andric break; 17770b57cec5SDimitry Andric case ELF32BEKind: 1778bdd1243dSDimitry Andric f = make<ObjFile<ELF32BE>>(ELF32BEKind, mb, archiveName); 1779fcaf7f86SDimitry Andric break; 17800b57cec5SDimitry Andric case ELF64LEKind: 1781bdd1243dSDimitry Andric f = make<ObjFile<ELF64LE>>(ELF64LEKind, mb, archiveName); 1782fcaf7f86SDimitry Andric break; 17830b57cec5SDimitry Andric case ELF64BEKind: 1784bdd1243dSDimitry Andric f = make<ObjFile<ELF64BE>>(ELF64BEKind, mb, archiveName); 1785fcaf7f86SDimitry Andric break; 17860b57cec5SDimitry Andric default: 17870b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 17880b57cec5SDimitry Andric } 1789bdd1243dSDimitry Andric f->init(); 1790fcaf7f86SDimitry Andric f->lazy = lazy; 1791fcaf7f86SDimitry Andric return f; 17920b57cec5SDimitry Andric } 17930b57cec5SDimitry Andric 17940eae32dcSDimitry Andric template <class ELFT> void ObjFile<ELFT>::parseLazy() { 17950eae32dcSDimitry Andric const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>(); 1796bdd1243dSDimitry Andric numSymbols = eSyms.size(); 1797bdd1243dSDimitry Andric symbols = std::make_unique<Symbol *[]>(numSymbols); 17980b57cec5SDimitry Andric 17990eae32dcSDimitry Andric // resolve() may trigger this->extract() if an existing symbol is an undefined 18000eae32dcSDimitry Andric // symbol. If that happens, this function has served its purpose, and we can 18010eae32dcSDimitry Andric // exit from the loop early. 1802bdd1243dSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 1803bdd1243dSDimitry Andric if (eSyms[i].st_shndx == SHN_UNDEF) 1804bdd1243dSDimitry Andric continue; 1805bdd1243dSDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 1806bdd1243dSDimitry Andric symbols[i]->resolve(LazyObject{*this}); 18070eae32dcSDimitry Andric if (!lazy) 1808bdd1243dSDimitry Andric break; 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric } 18110b57cec5SDimitry Andric 18120eae32dcSDimitry Andric bool InputFile::shouldExtractForCommon(StringRef name) { 1813fcaf7f86SDimitry Andric if (isa<BitcodeFile>(this)) 1814e8d8bef9SDimitry Andric return isBitcodeNonCommonDef(mb, name, archiveName); 1815e8d8bef9SDimitry Andric 1816e8d8bef9SDimitry Andric return isNonCommonDef(mb, name, archiveName); 1817e8d8bef9SDimitry Andric } 1818e8d8bef9SDimitry Andric 18195ffd83dbSDimitry Andric std::string elf::replaceThinLTOSuffix(StringRef path) { 1820bdd1243dSDimitry Andric auto [suffix, repl] = config->thinLTOObjectSuffixReplace; 18210b57cec5SDimitry Andric if (path.consume_back(suffix)) 18220b57cec5SDimitry Andric return (path + repl).str(); 18235ffd83dbSDimitry Andric return std::string(path); 18240b57cec5SDimitry Andric } 18250b57cec5SDimitry Andric 18265ffd83dbSDimitry Andric template class elf::ObjFile<ELF32LE>; 18275ffd83dbSDimitry Andric template class elf::ObjFile<ELF32BE>; 18285ffd83dbSDimitry Andric template class elf::ObjFile<ELF64LE>; 18295ffd83dbSDimitry Andric template class elf::ObjFile<ELF64BE>; 18300b57cec5SDimitry Andric 18310b57cec5SDimitry Andric template void SharedFile::parse<ELF32LE>(); 18320b57cec5SDimitry Andric template void SharedFile::parse<ELF32BE>(); 18330b57cec5SDimitry Andric template void SharedFile::parse<ELF64LE>(); 18340b57cec5SDimitry Andric template void SharedFile::parse<ELF64BE>(); 1835