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" 10*81ad6265SDimitry Andric #include "Config.h" 11*81ad6265SDimitry 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" 21*81ad6265SDimitry Andric #include "llvm/ADT/CachedHashString.h" 220b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 230b57cec5SDimitry Andric #include "llvm/LTO/LTO.h" 24*81ad6265SDimitry 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" 28*81ad6265SDimitry 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) { 5085868e8aSDimitry Andric if (!f) 5185868e8aSDimitry Andric return "<internal>"; 520b57cec5SDimitry Andric 5385868e8aSDimitry Andric if (f->toStringCache.empty()) { 5485868e8aSDimitry Andric if (f->archiveName.empty()) 550eae32dcSDimitry Andric f->toStringCache = f->getName(); 5685868e8aSDimitry Andric else 570eae32dcSDimitry Andric (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache); 5885868e8aSDimitry Andric } 590eae32dcSDimitry Andric return std::string(f->toStringCache); 6085868e8aSDimitry Andric } 6185868e8aSDimitry Andric 620b57cec5SDimitry Andric static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) { 630b57cec5SDimitry Andric unsigned char size; 640b57cec5SDimitry Andric unsigned char endian; 650b57cec5SDimitry Andric std::tie(size, endian) = getElfArchType(mb.getBuffer()); 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric auto report = [&](StringRef msg) { 680b57cec5SDimitry Andric StringRef filename = mb.getBufferIdentifier(); 690b57cec5SDimitry Andric if (archiveName.empty()) 700b57cec5SDimitry Andric fatal(filename + ": " + msg); 710b57cec5SDimitry Andric else 720b57cec5SDimitry Andric fatal(archiveName + "(" + filename + "): " + msg); 730b57cec5SDimitry Andric }; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric if (!mb.getBuffer().startswith(ElfMagic)) 760b57cec5SDimitry Andric report("not an ELF file"); 770b57cec5SDimitry Andric if (endian != ELFDATA2LSB && endian != ELFDATA2MSB) 780b57cec5SDimitry Andric report("corrupted ELF file: invalid data encoding"); 790b57cec5SDimitry Andric if (size != ELFCLASS32 && size != ELFCLASS64) 800b57cec5SDimitry Andric report("corrupted ELF file: invalid file class"); 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric size_t bufSize = mb.getBuffer().size(); 830b57cec5SDimitry Andric if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) || 840b57cec5SDimitry Andric (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr))) 850b57cec5SDimitry Andric report("corrupted ELF file: file is too short"); 860b57cec5SDimitry Andric 870b57cec5SDimitry Andric if (size == ELFCLASS32) 880b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 890b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 900b57cec5SDimitry Andric } 910b57cec5SDimitry Andric 920b57cec5SDimitry Andric InputFile::InputFile(Kind k, MemoryBufferRef m) 930b57cec5SDimitry Andric : mb(m), groupId(nextGroupId), fileKind(k) { 940b57cec5SDimitry Andric // All files within the same --{start,end}-group get the same group ID. 950b57cec5SDimitry Andric // Otherwise, a new file will get a new group ID. 960b57cec5SDimitry Andric if (!isInGroup) 970b57cec5SDimitry Andric ++nextGroupId; 980b57cec5SDimitry Andric } 990b57cec5SDimitry Andric 1005ffd83dbSDimitry Andric Optional<MemoryBufferRef> elf::readFile(StringRef path) { 101e8d8bef9SDimitry Andric llvm::TimeTraceScope timeScope("Load input files", path); 102e8d8bef9SDimitry Andric 1030b57cec5SDimitry Andric // The --chroot option changes our virtual root directory. 1040b57cec5SDimitry Andric // This is useful when you are dealing with files created by --reproduce. 1050b57cec5SDimitry Andric if (!config->chroot.empty() && path.startswith("/")) 10604eeddc0SDimitry Andric path = saver().save(config->chroot + path); 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric log(path); 109e8d8bef9SDimitry Andric config->dependencyFiles.insert(llvm::CachedHashString(path)); 1100b57cec5SDimitry Andric 111fe6060f1SDimitry Andric auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false, 112fe6060f1SDimitry Andric /*RequiresNullTerminator=*/false); 1130b57cec5SDimitry Andric if (auto ec = mbOrErr.getError()) { 1140b57cec5SDimitry Andric error("cannot open " + path + ": " + ec.message()); 1150b57cec5SDimitry Andric return None; 1160b57cec5SDimitry Andric } 1170b57cec5SDimitry Andric 11804eeddc0SDimitry Andric MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef(); 119*81ad6265SDimitry Andric ctx->memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership 1200b57cec5SDimitry Andric 1210b57cec5SDimitry Andric if (tar) 1220b57cec5SDimitry Andric tar->append(relativeToRoot(path), mbref.getBuffer()); 1230b57cec5SDimitry Andric return mbref; 1240b57cec5SDimitry Andric } 1250b57cec5SDimitry Andric 1260b57cec5SDimitry Andric // All input object files must be for the same architecture 1270b57cec5SDimitry Andric // (e.g. it does not make sense to link x86 object files with 1280b57cec5SDimitry Andric // MIPS object files.) This function checks for that error. 1290b57cec5SDimitry Andric static bool isCompatible(InputFile *file) { 1300b57cec5SDimitry Andric if (!file->isElf() && !isa<BitcodeFile>(file)) 1310b57cec5SDimitry Andric return true; 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric if (file->ekind == config->ekind && file->emachine == config->emachine) { 1340b57cec5SDimitry Andric if (config->emachine != EM_MIPS) 1350b57cec5SDimitry Andric return true; 1360b57cec5SDimitry Andric if (isMipsN32Abi(file) == config->mipsN32Abi) 1370b57cec5SDimitry Andric return true; 1380b57cec5SDimitry Andric } 1390b57cec5SDimitry Andric 1405ffd83dbSDimitry Andric StringRef target = 1415ffd83dbSDimitry Andric !config->bfdname.empty() ? config->bfdname : config->emulation; 1425ffd83dbSDimitry Andric if (!target.empty()) { 1435ffd83dbSDimitry Andric error(toString(file) + " is incompatible with " + target); 14485868e8aSDimitry Andric return false; 14585868e8aSDimitry Andric } 14685868e8aSDimitry Andric 147d56accc7SDimitry Andric InputFile *existing = nullptr; 148*81ad6265SDimitry Andric if (!ctx->objectFiles.empty()) 149*81ad6265SDimitry Andric existing = ctx->objectFiles[0]; 150*81ad6265SDimitry Andric else if (!ctx->sharedFiles.empty()) 151*81ad6265SDimitry Andric existing = ctx->sharedFiles[0]; 152*81ad6265SDimitry Andric else if (!ctx->bitcodeFiles.empty()) 153*81ad6265SDimitry Andric existing = ctx->bitcodeFiles[0]; 154d56accc7SDimitry Andric std::string with; 155d56accc7SDimitry Andric if (existing) 156d56accc7SDimitry Andric with = " with " + toString(existing); 157d56accc7SDimitry Andric error(toString(file) + " is incompatible" + with); 1580b57cec5SDimitry Andric return false; 1590b57cec5SDimitry Andric } 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric template <class ELFT> static void doParseFile(InputFile *file) { 1620b57cec5SDimitry Andric if (!isCompatible(file)) 1630b57cec5SDimitry Andric return; 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric // Binary file 1660b57cec5SDimitry Andric if (auto *f = dyn_cast<BinaryFile>(file)) { 167*81ad6265SDimitry Andric ctx->binaryFiles.push_back(f); 1680b57cec5SDimitry Andric f->parse(); 1690b57cec5SDimitry Andric return; 1700b57cec5SDimitry Andric } 1710b57cec5SDimitry Andric 1720b57cec5SDimitry Andric // Lazy object file 1730eae32dcSDimitry Andric if (file->lazy) { 1740eae32dcSDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 175*81ad6265SDimitry Andric ctx->lazyBitcodeFiles.push_back(f); 1760eae32dcSDimitry Andric f->parseLazy(); 1770eae32dcSDimitry Andric } else { 1780eae32dcSDimitry Andric cast<ObjFile<ELFT>>(file)->parseLazy(); 1790eae32dcSDimitry Andric } 1800b57cec5SDimitry Andric return; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric if (config->trace) 1840b57cec5SDimitry Andric message(toString(file)); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // .so file 1870b57cec5SDimitry Andric if (auto *f = dyn_cast<SharedFile>(file)) { 1880b57cec5SDimitry Andric f->parse<ELFT>(); 1890b57cec5SDimitry Andric return; 1900b57cec5SDimitry Andric } 1910b57cec5SDimitry Andric 1920b57cec5SDimitry Andric // LLVM bitcode file 1930b57cec5SDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 194*81ad6265SDimitry Andric ctx->bitcodeFiles.push_back(f); 1950b57cec5SDimitry Andric f->parse<ELFT>(); 1960b57cec5SDimitry Andric return; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // Regular object file 200*81ad6265SDimitry Andric ctx->objectFiles.push_back(cast<ELFFileBase>(file)); 2010b57cec5SDimitry Andric cast<ObjFile<ELFT>>(file)->parse(); 2020b57cec5SDimitry Andric } 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric // Add symbols in File to the symbol table. 2051fd87a68SDimitry Andric void elf::parseFile(InputFile *file) { invokeELFT(doParseFile, file); } 2060b57cec5SDimitry Andric 2070b57cec5SDimitry Andric // Concatenates arguments to construct a string representing an error location. 2080b57cec5SDimitry Andric static std::string createFileLineMsg(StringRef path, unsigned line) { 2095ffd83dbSDimitry Andric std::string filename = std::string(path::filename(path)); 2100b57cec5SDimitry Andric std::string lineno = ":" + std::to_string(line); 2110b57cec5SDimitry Andric if (filename == path) 2120b57cec5SDimitry Andric return filename + lineno; 2130b57cec5SDimitry Andric return filename + lineno + " (" + path.str() + lineno + ")"; 2140b57cec5SDimitry Andric } 2150b57cec5SDimitry Andric 2160b57cec5SDimitry Andric template <class ELFT> 2170b57cec5SDimitry Andric static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym, 2180b57cec5SDimitry Andric InputSectionBase &sec, uint64_t offset) { 2190b57cec5SDimitry Andric // In DWARF, functions and variables are stored to different places. 2200b57cec5SDimitry Andric // First, look up a function for a given offset. 2210b57cec5SDimitry Andric if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset)) 2220b57cec5SDimitry Andric return createFileLineMsg(info->FileName, info->Line); 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric // If it failed, look up again as a variable. 2250b57cec5SDimitry Andric if (Optional<std::pair<std::string, unsigned>> fileLine = 2260b57cec5SDimitry Andric file.getVariableLoc(sym.getName())) 2270b57cec5SDimitry Andric return createFileLineMsg(fileLine->first, fileLine->second); 2280b57cec5SDimitry Andric 2290b57cec5SDimitry Andric // File.sourceFile contains STT_FILE symbol, and that is a last resort. 2305ffd83dbSDimitry Andric return std::string(file.sourceFile); 2310b57cec5SDimitry Andric } 2320b57cec5SDimitry Andric 2330b57cec5SDimitry Andric std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec, 2340b57cec5SDimitry Andric uint64_t offset) { 2350b57cec5SDimitry Andric if (kind() != ObjKind) 2360b57cec5SDimitry Andric return ""; 2370b57cec5SDimitry Andric switch (config->ekind) { 2380b57cec5SDimitry Andric default: 2390b57cec5SDimitry Andric llvm_unreachable("Invalid kind"); 2400b57cec5SDimitry Andric case ELF32LEKind: 2410b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset); 2420b57cec5SDimitry Andric case ELF32BEKind: 2430b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset); 2440b57cec5SDimitry Andric case ELF64LEKind: 2450b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset); 2460b57cec5SDimitry Andric case ELF64BEKind: 2470b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset); 2480b57cec5SDimitry Andric } 2490b57cec5SDimitry Andric } 2500b57cec5SDimitry Andric 251e8d8bef9SDimitry Andric StringRef InputFile::getNameForScript() const { 252e8d8bef9SDimitry Andric if (archiveName.empty()) 253e8d8bef9SDimitry Andric return getName(); 254e8d8bef9SDimitry Andric 255e8d8bef9SDimitry Andric if (nameForScriptCache.empty()) 256e8d8bef9SDimitry Andric nameForScriptCache = (archiveName + Twine(':') + getName()).str(); 257e8d8bef9SDimitry Andric 258e8d8bef9SDimitry Andric return nameForScriptCache; 259e8d8bef9SDimitry Andric } 260e8d8bef9SDimitry Andric 2615ffd83dbSDimitry Andric template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() { 2625ffd83dbSDimitry Andric llvm::call_once(initDwarf, [this]() { 2635ffd83dbSDimitry Andric dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>( 2645ffd83dbSDimitry Andric std::make_unique<LLDDwarfObj<ELFT>>(this), "", 2655ffd83dbSDimitry Andric [&](Error err) { warn(getName() + ": " + toString(std::move(err))); }, 2665ffd83dbSDimitry Andric [&](Error warning) { 2675ffd83dbSDimitry Andric warn(getName() + ": " + toString(std::move(warning))); 2685ffd83dbSDimitry Andric })); 2695ffd83dbSDimitry Andric }); 2705ffd83dbSDimitry Andric 2715ffd83dbSDimitry Andric return dwarf.get(); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric // Returns the pair of file name and line number describing location of data 2750b57cec5SDimitry Andric // object (variable, array, etc) definition. 2760b57cec5SDimitry Andric template <class ELFT> 2770b57cec5SDimitry Andric Optional<std::pair<std::string, unsigned>> 2780b57cec5SDimitry Andric ObjFile<ELFT>::getVariableLoc(StringRef name) { 2795ffd83dbSDimitry Andric return getDwarf()->getVariableLoc(name); 2800b57cec5SDimitry Andric } 2810b57cec5SDimitry Andric 2820b57cec5SDimitry Andric // Returns source line information for a given offset 2830b57cec5SDimitry Andric // using DWARF debug info. 2840b57cec5SDimitry Andric template <class ELFT> 2850b57cec5SDimitry Andric Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s, 2860b57cec5SDimitry Andric uint64_t offset) { 2870b57cec5SDimitry Andric // Detect SectionIndex for specified section. 2880b57cec5SDimitry Andric uint64_t sectionIndex = object::SectionedAddress::UndefSection; 2890b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = s->file->getSections(); 2900b57cec5SDimitry Andric for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) { 2910b57cec5SDimitry Andric if (s == sections[curIndex]) { 2920b57cec5SDimitry Andric sectionIndex = curIndex; 2930b57cec5SDimitry Andric break; 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric } 2960b57cec5SDimitry Andric 2975ffd83dbSDimitry Andric return getDwarf()->getDILineInfo(offset, sectionIndex); 2980b57cec5SDimitry Andric } 2990b57cec5SDimitry Andric 3000b57cec5SDimitry Andric ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) { 3010b57cec5SDimitry Andric ekind = getELFKind(mb, ""); 3020b57cec5SDimitry Andric 3030b57cec5SDimitry Andric switch (ekind) { 3040b57cec5SDimitry Andric case ELF32LEKind: 3050b57cec5SDimitry Andric init<ELF32LE>(); 3060b57cec5SDimitry Andric break; 3070b57cec5SDimitry Andric case ELF32BEKind: 3080b57cec5SDimitry Andric init<ELF32BE>(); 3090b57cec5SDimitry Andric break; 3100b57cec5SDimitry Andric case ELF64LEKind: 3110b57cec5SDimitry Andric init<ELF64LE>(); 3120b57cec5SDimitry Andric break; 3130b57cec5SDimitry Andric case ELF64BEKind: 3140b57cec5SDimitry Andric init<ELF64BE>(); 3150b57cec5SDimitry Andric break; 3160b57cec5SDimitry Andric default: 3170b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric } 3200b57cec5SDimitry Andric 3210b57cec5SDimitry Andric template <typename Elf_Shdr> 3220b57cec5SDimitry Andric static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) { 3230b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) 3240b57cec5SDimitry Andric if (sec.sh_type == type) 3250b57cec5SDimitry Andric return &sec; 3260b57cec5SDimitry Andric return nullptr; 3270b57cec5SDimitry Andric } 3280b57cec5SDimitry Andric 3290b57cec5SDimitry Andric template <class ELFT> void ELFFileBase::init() { 3300b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 3310b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 3320b57cec5SDimitry Andric 3330b57cec5SDimitry Andric // Initialize trivial attributes. 3340b57cec5SDimitry Andric const ELFFile<ELFT> &obj = getObj<ELFT>(); 335e8d8bef9SDimitry Andric emachine = obj.getHeader().e_machine; 336e8d8bef9SDimitry Andric osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI]; 337e8d8bef9SDimitry Andric abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION]; 3380b57cec5SDimitry Andric 3390b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this); 3400eae32dcSDimitry Andric elfShdrs = sections.data(); 3410eae32dcSDimitry Andric numELFShdrs = sections.size(); 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric // Find a symbol table. 3440b57cec5SDimitry Andric bool isDSO = 3450b57cec5SDimitry Andric (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object); 3460b57cec5SDimitry Andric const Elf_Shdr *symtabSec = 3470b57cec5SDimitry Andric findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB); 3480b57cec5SDimitry Andric 3490b57cec5SDimitry Andric if (!symtabSec) 3500b57cec5SDimitry Andric return; 3510b57cec5SDimitry Andric 3520b57cec5SDimitry Andric // Initialize members corresponding to a symbol table. 3530b57cec5SDimitry Andric firstGlobal = symtabSec->sh_info; 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this); 3560b57cec5SDimitry Andric if (firstGlobal == 0 || firstGlobal > eSyms.size()) 3570b57cec5SDimitry Andric fatal(toString(this) + ": invalid sh_info in symbol table"); 3580b57cec5SDimitry Andric 3590b57cec5SDimitry Andric elfSyms = reinterpret_cast<const void *>(eSyms.data()); 3600eae32dcSDimitry Andric numELFSyms = uint32_t(eSyms.size()); 3610b57cec5SDimitry Andric stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this); 3620b57cec5SDimitry Andric } 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric template <class ELFT> 3650b57cec5SDimitry Andric uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const { 3660b57cec5SDimitry Andric return CHECK( 367e8d8bef9SDimitry Andric this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable), 3680b57cec5SDimitry Andric this); 3690b57cec5SDimitry Andric } 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) { 3721fd87a68SDimitry Andric object::ELFFile<ELFT> obj = this->getObj(); 3730b57cec5SDimitry Andric // Read a section table. justSymbols is usually false. 3740b57cec5SDimitry Andric if (this->justSymbols) 3750b57cec5SDimitry Andric initializeJustSymbols(); 3760b57cec5SDimitry Andric else 3771fd87a68SDimitry Andric initializeSections(ignoreComdats, obj); 3780b57cec5SDimitry Andric 3790b57cec5SDimitry Andric // Read a symbol table. 3801fd87a68SDimitry Andric initializeSymbols(obj); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric // Sections with SHT_GROUP and comdat bits define comdat section groups. 3840b57cec5SDimitry Andric // They are identified and deduplicated by group name. This function 3850b57cec5SDimitry Andric // returns a group name. 3860b57cec5SDimitry Andric template <class ELFT> 3870b57cec5SDimitry Andric StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections, 3880b57cec5SDimitry Andric const Elf_Shdr &sec) { 3890b57cec5SDimitry Andric typename ELFT::SymRange symbols = this->getELFSyms<ELFT>(); 3900b57cec5SDimitry Andric if (sec.sh_info >= symbols.size()) 3910b57cec5SDimitry Andric fatal(toString(this) + ": invalid symbol index"); 3920b57cec5SDimitry Andric const typename ELFT::Sym &sym = symbols[sec.sh_info]; 393349cc55cSDimitry Andric return CHECK(sym.getName(this->stringTable), this); 3940b57cec5SDimitry Andric } 3950b57cec5SDimitry Andric 39685868e8aSDimitry Andric template <class ELFT> 39785868e8aSDimitry Andric bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) { 3980b57cec5SDimitry Andric // On a regular link we don't merge sections if -O0 (default is -O1). This 3990b57cec5SDimitry Andric // sometimes makes the linker significantly faster, although the output will 4000b57cec5SDimitry Andric // be bigger. 4010b57cec5SDimitry Andric // 4020b57cec5SDimitry Andric // Doing the same for -r would create a problem as it would combine sections 4030b57cec5SDimitry Andric // with different sh_entsize. One option would be to just copy every SHF_MERGE 4040b57cec5SDimitry Andric // section as is to the output. While this would produce a valid ELF file with 4050b57cec5SDimitry Andric // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 4060b57cec5SDimitry Andric // they see two .debug_str. We could have separate logic for combining 4070b57cec5SDimitry Andric // SHF_MERGE sections based both on their name and sh_entsize, but that seems 4080b57cec5SDimitry Andric // to be more trouble than it is worth. Instead, we just use the regular (-O1) 4090b57cec5SDimitry Andric // logic for -r. 4100b57cec5SDimitry Andric if (config->optimize == 0 && !config->relocatable) 4110b57cec5SDimitry Andric return false; 4120b57cec5SDimitry Andric 4130b57cec5SDimitry Andric // A mergeable section with size 0 is useless because they don't have 4140b57cec5SDimitry Andric // any data to merge. A mergeable string section with size 0 can be 4150b57cec5SDimitry Andric // argued as invalid because it doesn't end with a null character. 4160b57cec5SDimitry Andric // We'll avoid a mess by handling them as if they were non-mergeable. 4170b57cec5SDimitry Andric if (sec.sh_size == 0) 4180b57cec5SDimitry Andric return false; 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric // Check for sh_entsize. The ELF spec is not clear about the zero 4210b57cec5SDimitry Andric // sh_entsize. It says that "the member [sh_entsize] contains 0 if 4220b57cec5SDimitry Andric // the section does not hold a table of fixed-size entries". We know 4230b57cec5SDimitry Andric // that Rust 1.13 produces a string mergeable section with a zero 4240b57cec5SDimitry Andric // sh_entsize. Here we just accept it rather than being picky about it. 4250b57cec5SDimitry Andric uint64_t entSize = sec.sh_entsize; 4260b57cec5SDimitry Andric if (entSize == 0) 4270b57cec5SDimitry Andric return false; 4280b57cec5SDimitry Andric if (sec.sh_size % entSize) 42985868e8aSDimitry Andric fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" + 43085868e8aSDimitry Andric Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" + 43185868e8aSDimitry Andric Twine(entSize) + ")"); 4320b57cec5SDimitry Andric 4335ffd83dbSDimitry Andric if (sec.sh_flags & SHF_WRITE) 43485868e8aSDimitry Andric fatal(toString(this) + ":(" + name + 43585868e8aSDimitry Andric "): writable SHF_MERGE section is not supported"); 4360b57cec5SDimitry Andric 4370b57cec5SDimitry Andric return true; 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric 4400b57cec5SDimitry Andric // This is for --just-symbols. 4410b57cec5SDimitry Andric // 4420b57cec5SDimitry Andric // --just-symbols is a very minor feature that allows you to link your 4430b57cec5SDimitry Andric // output against other existing program, so that if you load both your 4440b57cec5SDimitry Andric // program and the other program into memory, your output can refer the 4450b57cec5SDimitry Andric // other program's symbols. 4460b57cec5SDimitry Andric // 4470b57cec5SDimitry Andric // When the option is given, we link "just symbols". The section table is 4480b57cec5SDimitry Andric // initialized with null pointers. 4490b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 4500eae32dcSDimitry Andric sections.resize(numELFShdrs); 4510b57cec5SDimitry Andric } 4520b57cec5SDimitry Andric 4530b57cec5SDimitry Andric // An ELF object file may contain a `.deplibs` section. If it exists, the 4540b57cec5SDimitry Andric // section contains a list of library specifiers such as `m` for libm. This 4550b57cec5SDimitry Andric // function resolves a given name by finding the first matching library checking 4560b57cec5SDimitry Andric // the various ways that a library can be specified to LLD. This ELF extension 4570b57cec5SDimitry Andric // is a form of autolinking and is called `dependent libraries`. It is currently 4580b57cec5SDimitry Andric // unique to LLVM and lld. 4590b57cec5SDimitry Andric static void addDependentLibrary(StringRef specifier, const InputFile *f) { 4600b57cec5SDimitry Andric if (!config->dependentLibraries) 4610b57cec5SDimitry Andric return; 4621fd87a68SDimitry Andric if (Optional<std::string> s = searchLibraryBaseName(specifier)) 4631fd87a68SDimitry Andric driver->addFile(*s, /*withLOption=*/true); 4640b57cec5SDimitry Andric else if (Optional<std::string> s = findFromSearchPaths(specifier)) 4650b57cec5SDimitry Andric driver->addFile(*s, /*withLOption=*/true); 4661fd87a68SDimitry Andric else if (fs::exists(specifier)) 4671fd87a68SDimitry Andric driver->addFile(specifier, /*withLOption=*/false); 4680b57cec5SDimitry Andric else 4690b57cec5SDimitry Andric error(toString(f) + 4700b57cec5SDimitry Andric ": unable to find library from dependent library specifier: " + 4710b57cec5SDimitry Andric specifier); 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric 474480093f4SDimitry Andric // Record the membership of a section group so that in the garbage collection 475480093f4SDimitry Andric // pass, section group members are kept or discarded as a unit. 476480093f4SDimitry Andric template <class ELFT> 477480093f4SDimitry Andric static void handleSectionGroup(ArrayRef<InputSectionBase *> sections, 478480093f4SDimitry Andric ArrayRef<typename ELFT::Word> entries) { 479480093f4SDimitry Andric bool hasAlloc = false; 480480093f4SDimitry Andric for (uint32_t index : entries.slice(1)) { 481480093f4SDimitry Andric if (index >= sections.size()) 482480093f4SDimitry Andric return; 483480093f4SDimitry Andric if (InputSectionBase *s = sections[index]) 484480093f4SDimitry Andric if (s != &InputSection::discarded && s->flags & SHF_ALLOC) 485480093f4SDimitry Andric hasAlloc = true; 486480093f4SDimitry Andric } 487480093f4SDimitry Andric 488480093f4SDimitry Andric // If any member has the SHF_ALLOC flag, the whole group is subject to garbage 489480093f4SDimitry Andric // collection. See the comment in markLive(). This rule retains .debug_types 490480093f4SDimitry Andric // and .rela.debug_types. 491480093f4SDimitry Andric if (!hasAlloc) 492480093f4SDimitry Andric return; 493480093f4SDimitry Andric 494480093f4SDimitry Andric // Connect the members in a circular doubly-linked list via 495480093f4SDimitry Andric // nextInSectionGroup. 496480093f4SDimitry Andric InputSectionBase *head; 497480093f4SDimitry Andric InputSectionBase *prev = nullptr; 498480093f4SDimitry Andric for (uint32_t index : entries.slice(1)) { 499480093f4SDimitry Andric InputSectionBase *s = sections[index]; 500480093f4SDimitry Andric if (!s || s == &InputSection::discarded) 501480093f4SDimitry Andric continue; 502480093f4SDimitry Andric if (prev) 503480093f4SDimitry Andric prev->nextInSectionGroup = s; 504480093f4SDimitry Andric else 505480093f4SDimitry Andric head = s; 506480093f4SDimitry Andric prev = s; 507480093f4SDimitry Andric } 508480093f4SDimitry Andric if (prev) 509480093f4SDimitry Andric prev->nextInSectionGroup = head; 510480093f4SDimitry Andric } 511480093f4SDimitry Andric 5120b57cec5SDimitry Andric template <class ELFT> 5131fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSections(bool ignoreComdats, 5141fd87a68SDimitry Andric const llvm::object::ELFFile<ELFT> &obj) { 5150eae32dcSDimitry Andric ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>(); 516349cc55cSDimitry Andric StringRef shstrtab = CHECK(obj.getSectionStringTable(objSections), this); 5170b57cec5SDimitry Andric uint64_t size = objSections.size(); 5180b57cec5SDimitry Andric this->sections.resize(size); 5190b57cec5SDimitry Andric 520480093f4SDimitry Andric std::vector<ArrayRef<Elf_Word>> selectedGroups; 521480093f4SDimitry Andric 52204eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 5230b57cec5SDimitry Andric if (this->sections[i] == &InputSection::discarded) 5240b57cec5SDimitry Andric continue; 5250b57cec5SDimitry Andric const Elf_Shdr &sec = objSections[i]; 5260b57cec5SDimitry Andric 5270b57cec5SDimitry Andric // SHF_EXCLUDE'ed sections are discarded by the linker. However, 5280b57cec5SDimitry Andric // if -r is given, we'll let the final link discard such sections. 5290b57cec5SDimitry Andric // This is compatible with GNU. 5300b57cec5SDimitry Andric if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) { 5310eae32dcSDimitry Andric if (sec.sh_type == SHT_LLVM_CALL_GRAPH_PROFILE) 5320eae32dcSDimitry Andric cgProfileSectionIndex = i; 5330b57cec5SDimitry Andric if (sec.sh_type == SHT_LLVM_ADDRSIG) { 5340b57cec5SDimitry Andric // We ignore the address-significance table if we know that the object 5350b57cec5SDimitry Andric // file was created by objcopy or ld -r. This is because these tools 5360b57cec5SDimitry Andric // will reorder the symbols in the symbol table, invalidating the data 5370b57cec5SDimitry Andric // in the address-significance table, which refers to symbols by index. 5380b57cec5SDimitry Andric if (sec.sh_link != 0) 5390b57cec5SDimitry Andric this->addrsigSec = &sec; 5400b57cec5SDimitry Andric else if (config->icf == ICFLevel::Safe) 541fe6060f1SDimitry Andric warn(toString(this) + 542fe6060f1SDimitry Andric ": --icf=safe conservatively ignores " 543fe6060f1SDimitry Andric "SHT_LLVM_ADDRSIG [index " + 544fe6060f1SDimitry Andric Twine(i) + 545fe6060f1SDimitry Andric "] with sh_link=0 " 546fe6060f1SDimitry Andric "(likely created using objcopy or ld -r)"); 5470b57cec5SDimitry Andric } 5480b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 5490b57cec5SDimitry Andric continue; 5500b57cec5SDimitry Andric } 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric switch (sec.sh_type) { 5530b57cec5SDimitry Andric case SHT_GROUP: { 5540b57cec5SDimitry Andric // De-duplicate section groups by their signatures. 5550b57cec5SDimitry Andric StringRef signature = getShtGroupSignature(objSections, sec); 5560b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 5570b57cec5SDimitry Andric 5580b57cec5SDimitry Andric ArrayRef<Elf_Word> entries = 559e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Word>(sec), this); 5600b57cec5SDimitry Andric if (entries.empty()) 5610b57cec5SDimitry Andric fatal(toString(this) + ": empty SHT_GROUP"); 5620b57cec5SDimitry Andric 563fe6060f1SDimitry Andric Elf_Word flag = entries[0]; 564fe6060f1SDimitry Andric if (flag && flag != GRP_COMDAT) 5650b57cec5SDimitry Andric fatal(toString(this) + ": unsupported SHT_GROUP format"); 5660b57cec5SDimitry Andric 567fe6060f1SDimitry Andric bool keepGroup = 568fe6060f1SDimitry Andric (flag & GRP_COMDAT) == 0 || ignoreComdats || 5690b57cec5SDimitry Andric symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this) 5700b57cec5SDimitry Andric .second; 571fe6060f1SDimitry Andric if (keepGroup) { 5720b57cec5SDimitry Andric if (config->relocatable) 5731fd87a68SDimitry Andric this->sections[i] = createInputSection( 5741fd87a68SDimitry Andric i, sec, check(obj.getSectionName(sec, shstrtab))); 575480093f4SDimitry Andric selectedGroups.push_back(entries); 5760b57cec5SDimitry Andric continue; 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric 5790b57cec5SDimitry Andric // Otherwise, discard group members. 5800b57cec5SDimitry Andric for (uint32_t secIndex : entries.slice(1)) { 5810b57cec5SDimitry Andric if (secIndex >= size) 5820b57cec5SDimitry Andric fatal(toString(this) + 5830b57cec5SDimitry Andric ": invalid section index in group: " + Twine(secIndex)); 5840b57cec5SDimitry Andric this->sections[secIndex] = &InputSection::discarded; 5850b57cec5SDimitry Andric } 5860b57cec5SDimitry Andric break; 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric case SHT_SYMTAB_SHNDX: 5890b57cec5SDimitry Andric shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this); 5900b57cec5SDimitry Andric break; 5910b57cec5SDimitry Andric case SHT_SYMTAB: 5920b57cec5SDimitry Andric case SHT_STRTAB: 5935ffd83dbSDimitry Andric case SHT_REL: 5945ffd83dbSDimitry Andric case SHT_RELA: 5950b57cec5SDimitry Andric case SHT_NULL: 5960b57cec5SDimitry Andric break; 597*81ad6265SDimitry Andric case SHT_LLVM_SYMPART: 598*81ad6265SDimitry Andric ctx->hasSympart.store(true, std::memory_order_relaxed); 599*81ad6265SDimitry Andric LLVM_FALLTHROUGH; 6000b57cec5SDimitry Andric default: 6011fd87a68SDimitry Andric this->sections[i] = 6021fd87a68SDimitry Andric createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab))); 6030b57cec5SDimitry Andric } 60485868e8aSDimitry Andric } 60585868e8aSDimitry Andric 6065ffd83dbSDimitry Andric // We have a second loop. It is used to: 6075ffd83dbSDimitry Andric // 1) handle SHF_LINK_ORDER sections. 6085ffd83dbSDimitry Andric // 2) create SHT_REL[A] sections. In some cases the section header index of a 6095ffd83dbSDimitry Andric // relocation section may be smaller than that of the relocated section. In 6105ffd83dbSDimitry Andric // such cases, the relocation section would attempt to reference a target 6115ffd83dbSDimitry Andric // section that has not yet been created. For simplicity, delay creation of 6125ffd83dbSDimitry Andric // relocation sections until now. 61304eeddc0SDimitry Andric for (size_t i = 0; i != size; ++i) { 61485868e8aSDimitry Andric if (this->sections[i] == &InputSection::discarded) 61585868e8aSDimitry Andric continue; 61685868e8aSDimitry Andric const Elf_Shdr &sec = objSections[i]; 6175ffd83dbSDimitry Andric 61804eeddc0SDimitry Andric if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) { 61904eeddc0SDimitry Andric // Find a relocation target section and associate this section with that. 62004eeddc0SDimitry Andric // Target may have been discarded if it is in a different section group 62104eeddc0SDimitry Andric // and the group is discarded, even though it's a violation of the spec. 62204eeddc0SDimitry Andric // We handle that situation gracefully by discarding dangling relocation 62304eeddc0SDimitry Andric // sections. 62404eeddc0SDimitry Andric const uint32_t info = sec.sh_info; 62504eeddc0SDimitry Andric InputSectionBase *s = getRelocTarget(i, sec, info); 62604eeddc0SDimitry Andric if (!s) 62704eeddc0SDimitry Andric continue; 62804eeddc0SDimitry Andric 62904eeddc0SDimitry Andric // ELF spec allows mergeable sections with relocations, but they are rare, 63004eeddc0SDimitry Andric // and it is in practice hard to merge such sections by contents, because 63104eeddc0SDimitry Andric // applying relocations at end of linking changes section contents. So, we 63204eeddc0SDimitry Andric // simply handle such sections as non-mergeable ones. Degrading like this 63304eeddc0SDimitry Andric // is acceptable because section merging is optional. 63404eeddc0SDimitry Andric if (auto *ms = dyn_cast<MergeInputSection>(s)) { 63504eeddc0SDimitry Andric s = make<InputSection>(ms->file, ms->flags, ms->type, ms->alignment, 63604eeddc0SDimitry Andric ms->data(), ms->name); 63704eeddc0SDimitry Andric sections[info] = s; 63804eeddc0SDimitry Andric } 63904eeddc0SDimitry Andric 64004eeddc0SDimitry Andric if (s->relSecIdx != 0) 64104eeddc0SDimitry Andric error( 64204eeddc0SDimitry Andric toString(s) + 64304eeddc0SDimitry Andric ": multiple relocation sections to one section are not supported"); 64404eeddc0SDimitry Andric s->relSecIdx = i; 64504eeddc0SDimitry Andric 64604eeddc0SDimitry Andric // Relocation sections are usually removed from the output, so return 64704eeddc0SDimitry Andric // `nullptr` for the normal case. However, if -r or --emit-relocs is 64804eeddc0SDimitry Andric // specified, we need to copy them to the output. (Some post link analysis 64904eeddc0SDimitry Andric // tools specify --emit-relocs to obtain the information.) 65004eeddc0SDimitry Andric if (config->copyRelocs) { 65104eeddc0SDimitry Andric auto *isec = make<InputSection>( 65204eeddc0SDimitry Andric *this, sec, check(obj.getSectionName(sec, shstrtab))); 65304eeddc0SDimitry Andric // If the relocated section is discarded (due to /DISCARD/ or 65404eeddc0SDimitry Andric // --gc-sections), the relocation section should be discarded as well. 65504eeddc0SDimitry Andric s->dependentSections.push_back(isec); 65604eeddc0SDimitry Andric sections[i] = isec; 65704eeddc0SDimitry Andric } 65804eeddc0SDimitry Andric continue; 65904eeddc0SDimitry Andric } 6605ffd83dbSDimitry Andric 661e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have 662e8d8bef9SDimitry Andric // the flag. 66304eeddc0SDimitry Andric if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER)) 66485868e8aSDimitry Andric continue; 6650b57cec5SDimitry Andric 6660b57cec5SDimitry Andric InputSectionBase *linkSec = nullptr; 66704eeddc0SDimitry Andric if (sec.sh_link < size) 6680b57cec5SDimitry Andric linkSec = this->sections[sec.sh_link]; 6690b57cec5SDimitry Andric if (!linkSec) 67085868e8aSDimitry Andric fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link)); 6710b57cec5SDimitry Andric 672e8d8bef9SDimitry Andric // A SHF_LINK_ORDER section is discarded if its linked-to section is 673e8d8bef9SDimitry Andric // discarded. 6740b57cec5SDimitry Andric InputSection *isec = cast<InputSection>(this->sections[i]); 6750b57cec5SDimitry Andric linkSec->dependentSections.push_back(isec); 6760b57cec5SDimitry Andric if (!isa<InputSection>(linkSec)) 6770b57cec5SDimitry Andric error("a section " + isec->name + 67885868e8aSDimitry Andric " with SHF_LINK_ORDER should not refer a non-regular section: " + 6790b57cec5SDimitry Andric toString(linkSec)); 6800b57cec5SDimitry Andric } 681480093f4SDimitry Andric 682480093f4SDimitry Andric for (ArrayRef<Elf_Word> entries : selectedGroups) 683480093f4SDimitry Andric handleSectionGroup<ELFT>(this->sections, entries); 6840b57cec5SDimitry Andric } 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD 6870b57cec5SDimitry Andric // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how 6880b57cec5SDimitry Andric // the input objects have been compiled. 6890b57cec5SDimitry Andric static void updateARMVFPArgs(const ARMAttributeParser &attributes, 6900b57cec5SDimitry Andric const InputFile *f) { 6915ffd83dbSDimitry Andric Optional<unsigned> attr = 6925ffd83dbSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args); 693*81ad6265SDimitry Andric if (!attr) 6940b57cec5SDimitry Andric // If an ABI tag isn't present then it is implicitly given the value of 0 6950b57cec5SDimitry Andric // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files, 6960b57cec5SDimitry Andric // including some in glibc that don't use FP args (and should have value 3) 6970b57cec5SDimitry Andric // don't have the attribute so we do not consider an implicit value of 0 6980b57cec5SDimitry Andric // as a clash. 6990b57cec5SDimitry Andric return; 7000b57cec5SDimitry Andric 701*81ad6265SDimitry Andric unsigned vfpArgs = *attr; 7020b57cec5SDimitry Andric ARMVFPArgKind arg; 7030b57cec5SDimitry Andric switch (vfpArgs) { 7040b57cec5SDimitry Andric case ARMBuildAttrs::BaseAAPCS: 7050b57cec5SDimitry Andric arg = ARMVFPArgKind::Base; 7060b57cec5SDimitry Andric break; 7070b57cec5SDimitry Andric case ARMBuildAttrs::HardFPAAPCS: 7080b57cec5SDimitry Andric arg = ARMVFPArgKind::VFP; 7090b57cec5SDimitry Andric break; 7100b57cec5SDimitry Andric case ARMBuildAttrs::ToolChainFPPCS: 7110b57cec5SDimitry Andric // Tool chain specific convention that conforms to neither AAPCS variant. 7120b57cec5SDimitry Andric arg = ARMVFPArgKind::ToolChain; 7130b57cec5SDimitry Andric break; 7140b57cec5SDimitry Andric case ARMBuildAttrs::CompatibleFPAAPCS: 7150b57cec5SDimitry Andric // Object compatible with all conventions. 7160b57cec5SDimitry Andric return; 7170b57cec5SDimitry Andric default: 7180b57cec5SDimitry Andric error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs)); 7190b57cec5SDimitry Andric return; 7200b57cec5SDimitry Andric } 7210b57cec5SDimitry Andric // Follow ld.bfd and error if there is a mix of calling conventions. 7220b57cec5SDimitry Andric if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default) 7230b57cec5SDimitry Andric error(toString(f) + ": incompatible Tag_ABI_VFP_args"); 7240b57cec5SDimitry Andric else 7250b57cec5SDimitry Andric config->armVFPArgs = arg; 7260b57cec5SDimitry Andric } 7270b57cec5SDimitry Andric 7280b57cec5SDimitry Andric // The ARM support in lld makes some use of instructions that are not available 7290b57cec5SDimitry Andric // on all ARM architectures. Namely: 7300b57cec5SDimitry Andric // - Use of BLX instruction for interworking between ARM and Thumb state. 7310b57cec5SDimitry Andric // - Use of the extended Thumb branch encoding in relocation. 7320b57cec5SDimitry Andric // - Use of the MOVT/MOVW instructions in Thumb Thunks. 7330b57cec5SDimitry Andric // The ARM Attributes section contains information about the architecture chosen 7340b57cec5SDimitry Andric // at compile time. We follow the convention that if at least one input object 7350b57cec5SDimitry Andric // is compiled with an architecture that supports these features then lld is 7360b57cec5SDimitry Andric // permitted to use them. 7370b57cec5SDimitry Andric static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) { 7385ffd83dbSDimitry Andric Optional<unsigned> attr = 7395ffd83dbSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 740*81ad6265SDimitry Andric if (!attr) 7410b57cec5SDimitry Andric return; 7425ffd83dbSDimitry Andric auto arch = attr.getValue(); 7430b57cec5SDimitry Andric switch (arch) { 7440b57cec5SDimitry Andric case ARMBuildAttrs::Pre_v4: 7450b57cec5SDimitry Andric case ARMBuildAttrs::v4: 7460b57cec5SDimitry Andric case ARMBuildAttrs::v4T: 7470b57cec5SDimitry Andric // Architectures prior to v5 do not support BLX instruction 7480b57cec5SDimitry Andric break; 7490b57cec5SDimitry Andric case ARMBuildAttrs::v5T: 7500b57cec5SDimitry Andric case ARMBuildAttrs::v5TE: 7510b57cec5SDimitry Andric case ARMBuildAttrs::v5TEJ: 7520b57cec5SDimitry Andric case ARMBuildAttrs::v6: 7530b57cec5SDimitry Andric case ARMBuildAttrs::v6KZ: 7540b57cec5SDimitry Andric case ARMBuildAttrs::v6K: 7550b57cec5SDimitry Andric config->armHasBlx = true; 7560b57cec5SDimitry Andric // Architectures used in pre-Cortex processors do not support 7570b57cec5SDimitry Andric // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 7580b57cec5SDimitry Andric // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 7590b57cec5SDimitry Andric break; 7600b57cec5SDimitry Andric default: 7610b57cec5SDimitry Andric // All other Architectures have BLX and extended branch encoding 7620b57cec5SDimitry Andric config->armHasBlx = true; 7630b57cec5SDimitry Andric config->armJ1J2BranchEncoding = true; 7640b57cec5SDimitry Andric if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M) 7650b57cec5SDimitry Andric // All Architectures used in Cortex processors with the exception 7660b57cec5SDimitry Andric // of v6-M and v6S-M have the MOVT and MOVW instructions. 7670b57cec5SDimitry Andric config->armHasMovtMovw = true; 7680b57cec5SDimitry Andric break; 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric } 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric // If a source file is compiled with x86 hardware-assisted call flow control 7730b57cec5SDimitry Andric // enabled, the generated object file contains feature flags indicating that 7740b57cec5SDimitry Andric // fact. This function reads the feature flags and returns it. 7750b57cec5SDimitry Andric // 7760b57cec5SDimitry Andric // Essentially we want to read a single 32-bit value in this function, but this 7770b57cec5SDimitry Andric // function is rather complicated because the value is buried deep inside a 7780b57cec5SDimitry Andric // .note.gnu.property section. 7790b57cec5SDimitry Andric // 7800b57cec5SDimitry Andric // The section consists of one or more NOTE records. Each NOTE record consists 7810b57cec5SDimitry Andric // of zero or more type-length-value fields. We want to find a field of a 7820b57cec5SDimitry Andric // certain type. It seems a bit too much to just store a 32-bit value, perhaps 7830b57cec5SDimitry Andric // the ABI is unnecessarily complicated. 784e8d8bef9SDimitry Andric template <class ELFT> static uint32_t readAndFeatures(const InputSection &sec) { 7850b57cec5SDimitry Andric using Elf_Nhdr = typename ELFT::Nhdr; 7860b57cec5SDimitry Andric using Elf_Note = typename ELFT::Note; 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric uint32_t featuresSet = 0; 789*81ad6265SDimitry Andric ArrayRef<uint8_t> data = sec.rawData; 790e8d8bef9SDimitry Andric auto reportFatal = [&](const uint8_t *place, const char *msg) { 791e8d8bef9SDimitry Andric fatal(toString(sec.file) + ":(" + sec.name + "+0x" + 792*81ad6265SDimitry Andric Twine::utohexstr(place - sec.rawData.data()) + "): " + msg); 793e8d8bef9SDimitry Andric }; 7940b57cec5SDimitry Andric while (!data.empty()) { 7950b57cec5SDimitry Andric // Read one NOTE record. 7960b57cec5SDimitry Andric auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data()); 797e8d8bef9SDimitry Andric if (data.size() < sizeof(Elf_Nhdr) || data.size() < nhdr->getSize()) 798e8d8bef9SDimitry Andric reportFatal(data.data(), "data is too short"); 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric Elf_Note note(*nhdr); 8010b57cec5SDimitry Andric if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") { 8020b57cec5SDimitry Andric data = data.slice(nhdr->getSize()); 8030b57cec5SDimitry Andric continue; 8040b57cec5SDimitry Andric } 8050b57cec5SDimitry Andric 8060b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 8070b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 8080b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric // Read a body of a NOTE record, which consists of type-length-value fields. 8110b57cec5SDimitry Andric ArrayRef<uint8_t> desc = note.getDesc(); 8120b57cec5SDimitry Andric while (!desc.empty()) { 813e8d8bef9SDimitry Andric const uint8_t *place = desc.data(); 8140b57cec5SDimitry Andric if (desc.size() < 8) 815e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 816e8d8bef9SDimitry Andric uint32_t type = read32<ELFT::TargetEndianness>(desc.data()); 817e8d8bef9SDimitry Andric uint32_t size = read32<ELFT::TargetEndianness>(desc.data() + 4); 818e8d8bef9SDimitry Andric desc = desc.slice(8); 819e8d8bef9SDimitry Andric if (desc.size() < size) 820e8d8bef9SDimitry Andric reportFatal(place, "program property is too short"); 8210b57cec5SDimitry Andric 8220b57cec5SDimitry Andric if (type == featureAndType) { 8230b57cec5SDimitry Andric // We found a FEATURE_1_AND field. There may be more than one of these 824480093f4SDimitry Andric // in a .note.gnu.property section, for a relocatable object we 8250b57cec5SDimitry Andric // accumulate the bits set. 826e8d8bef9SDimitry Andric if (size < 4) 827e8d8bef9SDimitry Andric reportFatal(place, "FEATURE_1_AND entry is too short"); 828e8d8bef9SDimitry Andric featuresSet |= read32<ELFT::TargetEndianness>(desc.data()); 8290b57cec5SDimitry Andric } 8300b57cec5SDimitry Andric 831e8d8bef9SDimitry Andric // Padding is present in the note descriptor, if necessary. 832e8d8bef9SDimitry Andric desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size)); 8330b57cec5SDimitry Andric } 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric // Go to next NOTE record to look for more FEATURE_1_AND descriptions. 8360b57cec5SDimitry Andric data = data.slice(nhdr->getSize()); 8370b57cec5SDimitry Andric } 8380b57cec5SDimitry Andric 8390b57cec5SDimitry Andric return featuresSet; 8400b57cec5SDimitry Andric } 8410b57cec5SDimitry Andric 8420b57cec5SDimitry Andric template <class ELFT> 84304eeddc0SDimitry Andric InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, 84404eeddc0SDimitry Andric const Elf_Shdr &sec, 84504eeddc0SDimitry Andric uint32_t info) { 846349cc55cSDimitry Andric if (info < this->sections.size()) { 847349cc55cSDimitry Andric InputSectionBase *target = this->sections[info]; 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric // Strictly speaking, a relocation section must be included in the 8500b57cec5SDimitry Andric // group of the section it relocates. However, LLVM 3.3 and earlier 8510b57cec5SDimitry Andric // would fail to do so, so we gracefully handle that case. 8520b57cec5SDimitry Andric if (target == &InputSection::discarded) 8530b57cec5SDimitry Andric return nullptr; 8540b57cec5SDimitry Andric 855349cc55cSDimitry Andric if (target != nullptr) 8560b57cec5SDimitry Andric return target; 8570b57cec5SDimitry Andric } 8580b57cec5SDimitry Andric 85904eeddc0SDimitry Andric error(toString(this) + Twine(": relocation section (index ") + Twine(idx) + 86004eeddc0SDimitry Andric ") has invalid sh_info (" + Twine(info) + ")"); 861349cc55cSDimitry Andric return nullptr; 862349cc55cSDimitry Andric } 863349cc55cSDimitry Andric 8640b57cec5SDimitry Andric template <class ELFT> 865349cc55cSDimitry Andric InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx, 866349cc55cSDimitry Andric const Elf_Shdr &sec, 8671fd87a68SDimitry Andric StringRef name) { 8681fd87a68SDimitry Andric if (sec.sh_type == SHT_ARM_ATTRIBUTES && config->emachine == EM_ARM) { 8690b57cec5SDimitry Andric ARMAttributeParser attributes; 870e8d8bef9SDimitry Andric ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec)); 8715ffd83dbSDimitry Andric if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind 8725ffd83dbSDimitry Andric ? support::little 8735ffd83dbSDimitry Andric : support::big)) { 8745ffd83dbSDimitry Andric auto *isec = make<InputSection>(*this, sec, name); 8755ffd83dbSDimitry Andric warn(toString(isec) + ": " + llvm::toString(std::move(e))); 876e8d8bef9SDimitry Andric } else { 8770b57cec5SDimitry Andric updateSupportedARMFeatures(attributes); 8780b57cec5SDimitry Andric updateARMVFPArgs(attributes, this); 8790b57cec5SDimitry Andric 8800b57cec5SDimitry Andric // FIXME: Retain the first attribute section we see. The eglibc ARM 8810b57cec5SDimitry Andric // dynamic loaders require the presence of an attribute section for dlopen 882e8d8bef9SDimitry Andric // to work. In a full implementation we would merge all attribute 883e8d8bef9SDimitry Andric // sections. 884e8d8bef9SDimitry Andric if (in.attributes == nullptr) { 88504eeddc0SDimitry Andric in.attributes = std::make_unique<InputSection>(*this, sec, name); 88604eeddc0SDimitry Andric return in.attributes.get(); 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric return &InputSection::discarded; 8890b57cec5SDimitry Andric } 890e8d8bef9SDimitry Andric } 891e8d8bef9SDimitry Andric 8921fd87a68SDimitry Andric if (sec.sh_type == SHT_RISCV_ATTRIBUTES && config->emachine == EM_RISCV) { 893e8d8bef9SDimitry Andric RISCVAttributeParser attributes; 894e8d8bef9SDimitry Andric ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(sec)); 895e8d8bef9SDimitry Andric if (Error e = attributes.parse(contents, support::little)) { 896e8d8bef9SDimitry Andric auto *isec = make<InputSection>(*this, sec, name); 897e8d8bef9SDimitry Andric warn(toString(isec) + ": " + llvm::toString(std::move(e))); 898e8d8bef9SDimitry Andric } else { 899e8d8bef9SDimitry Andric // FIXME: Validate arch tag contains C if and only if EF_RISCV_RVC is 900e8d8bef9SDimitry Andric // present. 901e8d8bef9SDimitry Andric 902e8d8bef9SDimitry Andric // FIXME: Retain the first attribute section we see. Tools such as 903e8d8bef9SDimitry Andric // llvm-objdump make use of the attribute section to determine which 904e8d8bef9SDimitry Andric // standard extensions to enable. In a full implementation we would merge 905e8d8bef9SDimitry Andric // all attribute sections. 906e8d8bef9SDimitry Andric if (in.attributes == nullptr) { 90704eeddc0SDimitry Andric in.attributes = std::make_unique<InputSection>(*this, sec, name); 90804eeddc0SDimitry Andric return in.attributes.get(); 909e8d8bef9SDimitry Andric } 910e8d8bef9SDimitry Andric return &InputSection::discarded; 911e8d8bef9SDimitry Andric } 912e8d8bef9SDimitry Andric } 913e8d8bef9SDimitry Andric 91404eeddc0SDimitry Andric if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !config->relocatable) { 9150b57cec5SDimitry Andric ArrayRef<char> data = 916e8d8bef9SDimitry Andric CHECK(this->getObj().template getSectionContentsAsArray<char>(sec), this); 9170b57cec5SDimitry Andric if (!data.empty() && data.back() != '\0') { 9180b57cec5SDimitry Andric error(toString(this) + 9190b57cec5SDimitry Andric ": corrupted dependent libraries section (unterminated string): " + 9200b57cec5SDimitry Andric name); 9210b57cec5SDimitry Andric return &InputSection::discarded; 9220b57cec5SDimitry Andric } 9230b57cec5SDimitry Andric for (const char *d = data.begin(), *e = data.end(); d < e;) { 9240b57cec5SDimitry Andric StringRef s(d); 9250b57cec5SDimitry Andric addDependentLibrary(s, this); 9260b57cec5SDimitry Andric d += s.size() + 1; 9270b57cec5SDimitry Andric } 9280b57cec5SDimitry Andric return &InputSection::discarded; 9290b57cec5SDimitry Andric } 9300b57cec5SDimitry Andric 9310eae32dcSDimitry Andric if (name.startswith(".n")) { 9320b57cec5SDimitry Andric // The GNU linker uses .note.GNU-stack section as a marker indicating 9330b57cec5SDimitry Andric // that the code in the object file does not expect that the stack is 9340b57cec5SDimitry Andric // executable (in terms of NX bit). If all input files have the marker, 9350b57cec5SDimitry Andric // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 9360b57cec5SDimitry Andric // make the stack non-executable. Most object files have this section as 9370b57cec5SDimitry Andric // of 2017. 9380b57cec5SDimitry Andric // 9390b57cec5SDimitry Andric // But making the stack non-executable is a norm today for security 9400b57cec5SDimitry Andric // reasons. Failure to do so may result in a serious security issue. 9410b57cec5SDimitry Andric // Therefore, we make LLD always add PT_GNU_STACK unless it is 9420b57cec5SDimitry Andric // explicitly told to do otherwise (by -z execstack). Because the stack 9430b57cec5SDimitry Andric // executable-ness is controlled solely by command line options, 9440b57cec5SDimitry Andric // .note.GNU-stack sections are simply ignored. 9450b57cec5SDimitry Andric if (name == ".note.GNU-stack") 9460b57cec5SDimitry Andric return &InputSection::discarded; 9470b57cec5SDimitry Andric 9480b57cec5SDimitry Andric // Object files that use processor features such as Intel Control-Flow 9490b57cec5SDimitry Andric // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a 9500b57cec5SDimitry Andric // .note.gnu.property section containing a bitfield of feature bits like the 9510b57cec5SDimitry Andric // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag. 9520b57cec5SDimitry Andric // 9530b57cec5SDimitry Andric // Since we merge bitmaps from multiple object files to create a new 9540b57cec5SDimitry Andric // .note.gnu.property containing a single AND'ed bitmap, we discard an input 9550b57cec5SDimitry Andric // file's .note.gnu.property section. 9560b57cec5SDimitry Andric if (name == ".note.gnu.property") { 957e8d8bef9SDimitry Andric this->andFeatures = readAndFeatures<ELFT>(InputSection(*this, sec, name)); 9580b57cec5SDimitry Andric return &InputSection::discarded; 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric // Split stacks is a feature to support a discontiguous stack, 9620b57cec5SDimitry Andric // commonly used in the programming language Go. For the details, 9630b57cec5SDimitry Andric // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled 9640b57cec5SDimitry Andric // for split stack will include a .note.GNU-split-stack section. 9650b57cec5SDimitry Andric if (name == ".note.GNU-split-stack") { 9660b57cec5SDimitry Andric if (config->relocatable) { 9670eae32dcSDimitry Andric error( 9680eae32dcSDimitry Andric "cannot mix split-stack and non-split-stack in a relocatable link"); 9690b57cec5SDimitry Andric return &InputSection::discarded; 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric this->splitStack = true; 9720b57cec5SDimitry Andric return &InputSection::discarded; 9730b57cec5SDimitry Andric } 9740b57cec5SDimitry Andric 9750b57cec5SDimitry Andric // An object file cmpiled for split stack, but where some of the 9760b57cec5SDimitry Andric // functions were compiled with the no_split_stack_attribute will 9770b57cec5SDimitry Andric // include a .note.GNU-no-split-stack section. 9780b57cec5SDimitry Andric if (name == ".note.GNU-no-split-stack") { 9790b57cec5SDimitry Andric this->someNoSplitStack = true; 9800b57cec5SDimitry Andric return &InputSection::discarded; 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric 9830eae32dcSDimitry Andric // Strip existing .note.gnu.build-id sections so that the output won't have 9840eae32dcSDimitry Andric // more than one build-id. This is not usually a problem because input 9850eae32dcSDimitry Andric // object files normally don't have .build-id sections, but you can create 9860eae32dcSDimitry Andric // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard 9870eae32dcSDimitry Andric // against it. 9880eae32dcSDimitry Andric if (name == ".note.gnu.build-id") 9890eae32dcSDimitry Andric return &InputSection::discarded; 9900eae32dcSDimitry Andric } 9910eae32dcSDimitry Andric 9920b57cec5SDimitry Andric // The linker merges EH (exception handling) frames and creates a 9930b57cec5SDimitry Andric // .eh_frame_hdr section for runtime. So we handle them with a special 9940b57cec5SDimitry Andric // class. For relocatable outputs, they are just passed through. 9950b57cec5SDimitry Andric if (name == ".eh_frame" && !config->relocatable) 9960b57cec5SDimitry Andric return make<EhInputSection>(*this, sec, name); 9970b57cec5SDimitry Andric 9980eae32dcSDimitry Andric if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name)) 9990b57cec5SDimitry Andric return make<MergeInputSection>(*this, sec, name); 10000b57cec5SDimitry Andric return make<InputSection>(*this, sec, name); 10010b57cec5SDimitry Andric } 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // Initialize this->Symbols. this->Symbols is a parallel array as 10040b57cec5SDimitry Andric // its corresponding ELF symbol table. 10051fd87a68SDimitry Andric template <class ELFT> 10061fd87a68SDimitry Andric void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) { 10070eae32dcSDimitry Andric SymbolTable &symtab = *elf::symtab; 10080b57cec5SDimitry Andric 10090eae32dcSDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 10100eae32dcSDimitry Andric symbols.resize(eSyms.size()); 10110eae32dcSDimitry Andric 1012*81ad6265SDimitry Andric // Some entries have been filled by LazyObjFile. 1013*81ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) 1014*81ad6265SDimitry Andric if (!symbols[i]) 1015*81ad6265SDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 1016*81ad6265SDimitry Andric 1017*81ad6265SDimitry Andric // Perform symbol resolution on non-local symbols. 1018*81ad6265SDimitry Andric SmallVector<unsigned, 32> undefineds; 1019*81ad6265SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 1020*81ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 1021*81ad6265SDimitry Andric uint32_t secIdx = eSym.st_shndx; 1022*81ad6265SDimitry Andric if (secIdx == SHN_UNDEF) { 1023*81ad6265SDimitry Andric undefineds.push_back(i); 1024*81ad6265SDimitry Andric continue; 1025*81ad6265SDimitry Andric } 1026*81ad6265SDimitry Andric 1027*81ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 1028*81ad6265SDimitry Andric uint8_t stOther = eSym.st_other; 1029*81ad6265SDimitry Andric uint8_t type = eSym.getType(); 1030*81ad6265SDimitry Andric uint64_t value = eSym.st_value; 1031*81ad6265SDimitry Andric uint64_t size = eSym.st_size; 1032*81ad6265SDimitry Andric 1033*81ad6265SDimitry Andric Symbol *sym = symbols[i]; 1034*81ad6265SDimitry Andric sym->isUsedInRegularObj = true; 1035*81ad6265SDimitry Andric if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) { 1036*81ad6265SDimitry Andric if (value == 0 || value >= UINT32_MAX) 1037*81ad6265SDimitry Andric fatal(toString(this) + ": common symbol '" + sym->getName() + 1038*81ad6265SDimitry Andric "' has invalid alignment: " + Twine(value)); 1039*81ad6265SDimitry Andric hasCommonSyms = true; 1040*81ad6265SDimitry Andric sym->resolve( 1041*81ad6265SDimitry Andric CommonSymbol{this, StringRef(), binding, stOther, type, value, size}); 1042*81ad6265SDimitry Andric continue; 1043*81ad6265SDimitry Andric } 1044*81ad6265SDimitry Andric 1045*81ad6265SDimitry Andric // Handle global defined symbols. Defined::section will be set in postParse. 1046*81ad6265SDimitry Andric sym->resolve(Defined{this, StringRef(), binding, stOther, type, value, size, 1047*81ad6265SDimitry Andric nullptr}); 1048*81ad6265SDimitry Andric } 1049*81ad6265SDimitry Andric 1050*81ad6265SDimitry Andric // Undefined symbols (excluding those defined relative to non-prevailing 1051*81ad6265SDimitry Andric // sections) can trigger recursive extract. Process defined symbols first so 1052*81ad6265SDimitry Andric // that the relative order between a defined symbol and an undefined symbol 1053*81ad6265SDimitry Andric // does not change the symbol resolution behavior. In addition, a set of 1054*81ad6265SDimitry Andric // interconnected symbols will all be resolved to the same file, instead of 1055*81ad6265SDimitry Andric // being resolved to different files. 1056*81ad6265SDimitry Andric for (unsigned i : undefineds) { 1057*81ad6265SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 1058*81ad6265SDimitry Andric Symbol *sym = symbols[i]; 1059*81ad6265SDimitry Andric sym->resolve(Undefined{this, StringRef(), eSym.getBinding(), eSym.st_other, 1060*81ad6265SDimitry Andric eSym.getType()}); 1061*81ad6265SDimitry Andric sym->isUsedInRegularObj = true; 1062*81ad6265SDimitry Andric sym->referenced = true; 1063*81ad6265SDimitry Andric } 1064*81ad6265SDimitry Andric } 1065*81ad6265SDimitry Andric 1066*81ad6265SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeLocalSymbols() { 1067*81ad6265SDimitry Andric if (!firstGlobal) 1068*81ad6265SDimitry Andric return; 1069*81ad6265SDimitry Andric localSymStorage = std::make_unique<SymbolUnion[]>(firstGlobal); 1070*81ad6265SDimitry Andric SymbolUnion *locals = localSymStorage.get(); 1071*81ad6265SDimitry Andric 1072*81ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 10730eae32dcSDimitry Andric for (size_t i = 0, end = firstGlobal; i != end; ++i) { 10740b57cec5SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 10751fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 10761fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 10771fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 10781fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 10791fd87a68SDimitry Andric secIdx = 0; 10800eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 10810b57cec5SDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 10820eae32dcSDimitry Andric if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL)) 10835ffd83dbSDimitry Andric error(toString(this) + ": non-local symbol (" + Twine(i) + 10840eae32dcSDimitry Andric ") found at index < .symtab's sh_info (" + Twine(end) + ")"); 10855ffd83dbSDimitry Andric 10860eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 10875ffd83dbSDimitry Andric uint8_t type = eSym.getType(); 10885ffd83dbSDimitry Andric if (type == STT_FILE) 10890eae32dcSDimitry Andric sourceFile = CHECK(eSym.getName(stringTable), this); 10900eae32dcSDimitry Andric if (LLVM_UNLIKELY(stringTable.size() <= eSym.st_name)) 10915ffd83dbSDimitry Andric fatal(toString(this) + ": invalid symbol name offset"); 109204eeddc0SDimitry Andric StringRef name(stringTable.data() + eSym.st_name); 10935ffd83dbSDimitry Andric 10940eae32dcSDimitry Andric symbols[i] = reinterpret_cast<Symbol *>(locals + i); 10950eae32dcSDimitry Andric if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded) 10960eae32dcSDimitry Andric new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type, 10975ffd83dbSDimitry Andric /*discardedSecIdx=*/secIdx); 10985ffd83dbSDimitry Andric else 10990eae32dcSDimitry Andric new (symbols[i]) Defined(this, name, STB_LOCAL, eSym.st_other, type, 11000eae32dcSDimitry Andric eSym.st_value, eSym.st_size, sec); 1101*81ad6265SDimitry Andric symbols[i]->isUsedInRegularObj = true; 1102*81ad6265SDimitry Andric } 11035ffd83dbSDimitry Andric } 11045ffd83dbSDimitry Andric 1105*81ad6265SDimitry Andric // Called after all ObjFile::parse is called for all ObjFiles. This checks 1106*81ad6265SDimitry Andric // duplicate symbols and may do symbol property merge in the future. 1107*81ad6265SDimitry Andric template <class ELFT> void ObjFile<ELFT>::postParse() { 1108*81ad6265SDimitry Andric static std::mutex mu; 1109*81ad6265SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 11105ffd83dbSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 11115ffd83dbSDimitry Andric const Elf_Sym &eSym = eSyms[i]; 1112*81ad6265SDimitry Andric Symbol &sym = *symbols[i]; 11131fd87a68SDimitry Andric uint32_t secIdx = eSym.st_shndx; 1114*81ad6265SDimitry Andric uint8_t binding = eSym.getBinding(); 1115*81ad6265SDimitry Andric if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK && 1116*81ad6265SDimitry Andric binding != STB_GNU_UNIQUE)) 1117*81ad6265SDimitry Andric errorOrWarn(toString(this) + ": symbol (" + Twine(i) + 1118*81ad6265SDimitry Andric ") has invalid binding: " + Twine((int)binding)); 1119*81ad6265SDimitry Andric 1120*81ad6265SDimitry Andric // st_value of STT_TLS represents the assigned offset, not the actual 1121*81ad6265SDimitry Andric // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can 1122*81ad6265SDimitry Andric // only be referenced by special TLS relocations. It is usually an error if 1123*81ad6265SDimitry Andric // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa. 1124*81ad6265SDimitry Andric if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS && 1125*81ad6265SDimitry Andric eSym.getType() != STT_NOTYPE) 1126*81ad6265SDimitry Andric errorOrWarn("TLS attribute mismatch: " + toString(sym) + "\n>>> in " + 1127*81ad6265SDimitry Andric toString(sym.file) + "\n>>> in " + toString(this)); 1128*81ad6265SDimitry Andric 1129*81ad6265SDimitry Andric // Handle non-COMMON defined symbol below. !sym.file allows a symbol 1130*81ad6265SDimitry Andric // assignment to redefine a symbol without an error. 1131*81ad6265SDimitry Andric if (!sym.file || !sym.isDefined() || secIdx == SHN_UNDEF || 1132*81ad6265SDimitry Andric secIdx == SHN_COMMON) 1133*81ad6265SDimitry Andric continue; 1134*81ad6265SDimitry Andric 11351fd87a68SDimitry Andric if (LLVM_UNLIKELY(secIdx == SHN_XINDEX)) 11361fd87a68SDimitry Andric secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable)); 11371fd87a68SDimitry Andric else if (secIdx >= SHN_LORESERVE) 11381fd87a68SDimitry Andric secIdx = 0; 11390eae32dcSDimitry Andric if (LLVM_UNLIKELY(secIdx >= sections.size())) 11400eae32dcSDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 11410eae32dcSDimitry Andric InputSectionBase *sec = sections[secIdx]; 11420b57cec5SDimitry Andric if (sec == &InputSection::discarded) { 1143*81ad6265SDimitry Andric if (sym.traced) { 1144*81ad6265SDimitry Andric printTraceSymbol(Undefined{this, sym.getName(), sym.binding, 1145*81ad6265SDimitry Andric sym.stOther, sym.type, secIdx}, 1146*81ad6265SDimitry Andric sym.getName()); 1147*81ad6265SDimitry Andric } 1148*81ad6265SDimitry Andric if (sym.file == this) { 1149*81ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1150*81ad6265SDimitry Andric ctx->nonPrevailingSyms.emplace_back(&sym, secIdx); 1151*81ad6265SDimitry Andric } 11520b57cec5SDimitry Andric continue; 11530b57cec5SDimitry Andric } 11540b57cec5SDimitry Andric 1155*81ad6265SDimitry Andric if (sym.file == this) { 1156*81ad6265SDimitry Andric cast<Defined>(sym).section = sec; 11570b57cec5SDimitry Andric continue; 11580b57cec5SDimitry Andric } 11590b57cec5SDimitry Andric 1160*81ad6265SDimitry Andric if (binding == STB_WEAK) 1161*81ad6265SDimitry Andric continue; 1162*81ad6265SDimitry Andric std::lock_guard<std::mutex> lock(mu); 1163*81ad6265SDimitry Andric ctx->duplicates.push_back({&sym, this, sec, eSym.st_value}); 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric } 11660b57cec5SDimitry Andric 1167e8d8bef9SDimitry Andric // The handling of tentative definitions (COMMON symbols) in archives is murky. 1168fe6060f1SDimitry Andric // A tentative definition will be promoted to a global definition if there are 1169fe6060f1SDimitry Andric // no non-tentative definitions to dominate it. When we hold a tentative 1170fe6060f1SDimitry Andric // definition to a symbol and are inspecting archive members for inclusion 1171fe6060f1SDimitry Andric // there are 2 ways we can proceed: 1172e8d8bef9SDimitry Andric // 1173e8d8bef9SDimitry Andric // 1) Consider the tentative definition a 'real' definition (ie promotion from 1174e8d8bef9SDimitry Andric // tentative to real definition has already happened) and not inspect 1175e8d8bef9SDimitry Andric // archive members for Global/Weak definitions to replace the tentative 1176e8d8bef9SDimitry Andric // definition. An archive member would only be included if it satisfies some 1177e8d8bef9SDimitry Andric // other undefined symbol. This is the behavior Gold uses. 1178e8d8bef9SDimitry Andric // 1179e8d8bef9SDimitry Andric // 2) Consider the tentative definition as still undefined (ie the promotion to 1180fe6060f1SDimitry Andric // a real definition happens only after all symbol resolution is done). 1181fe6060f1SDimitry Andric // The linker searches archive members for STB_GLOBAL definitions to 1182e8d8bef9SDimitry Andric // replace the tentative definition with. This is the behavior used by 1183e8d8bef9SDimitry Andric // GNU ld. 1184e8d8bef9SDimitry Andric // 1185e8d8bef9SDimitry Andric // The second behavior is inherited from SysVR4, which based it on the FORTRAN 1186fe6060f1SDimitry Andric // COMMON BLOCK model. This behavior is needed for proper initialization in old 1187e8d8bef9SDimitry Andric // (pre F90) FORTRAN code that is packaged into an archive. 1188e8d8bef9SDimitry Andric // 1189fe6060f1SDimitry Andric // The following functions search archive members for definitions to replace 1190fe6060f1SDimitry Andric // tentative definitions (implementing behavior 2). 1191e8d8bef9SDimitry Andric static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName, 1192e8d8bef9SDimitry Andric StringRef archiveName) { 1193e8d8bef9SDimitry Andric IRSymtabFile symtabFile = check(readIRSymtab(mb)); 1194e8d8bef9SDimitry Andric for (const irsymtab::Reader::SymbolRef &sym : 1195e8d8bef9SDimitry Andric symtabFile.TheReader.symbols()) { 1196e8d8bef9SDimitry Andric if (sym.isGlobal() && sym.getName() == symName) 1197fe6060f1SDimitry Andric return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon(); 1198e8d8bef9SDimitry Andric } 1199e8d8bef9SDimitry Andric return false; 1200e8d8bef9SDimitry Andric } 1201e8d8bef9SDimitry Andric 1202e8d8bef9SDimitry Andric template <class ELFT> 1203e8d8bef9SDimitry Andric static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName, 1204e8d8bef9SDimitry Andric StringRef archiveName) { 1205e8d8bef9SDimitry Andric ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(mb, archiveName); 1206e8d8bef9SDimitry Andric StringRef stringtable = obj->getStringTable(); 1207e8d8bef9SDimitry Andric 1208e8d8bef9SDimitry Andric for (auto sym : obj->template getGlobalELFSyms<ELFT>()) { 1209e8d8bef9SDimitry Andric Expected<StringRef> name = sym.getName(stringtable); 1210e8d8bef9SDimitry Andric if (name && name.get() == symName) 1211fe6060f1SDimitry Andric return sym.isDefined() && sym.getBinding() == STB_GLOBAL && 1212fe6060f1SDimitry Andric !sym.isCommon(); 1213e8d8bef9SDimitry Andric } 1214e8d8bef9SDimitry Andric return false; 1215e8d8bef9SDimitry Andric } 1216e8d8bef9SDimitry Andric 1217e8d8bef9SDimitry Andric static bool isNonCommonDef(MemoryBufferRef mb, StringRef symName, 1218e8d8bef9SDimitry Andric StringRef archiveName) { 1219e8d8bef9SDimitry Andric switch (getELFKind(mb, archiveName)) { 1220e8d8bef9SDimitry Andric case ELF32LEKind: 1221e8d8bef9SDimitry Andric return isNonCommonDef<ELF32LE>(mb, symName, archiveName); 1222e8d8bef9SDimitry Andric case ELF32BEKind: 1223e8d8bef9SDimitry Andric return isNonCommonDef<ELF32BE>(mb, symName, archiveName); 1224e8d8bef9SDimitry Andric case ELF64LEKind: 1225e8d8bef9SDimitry Andric return isNonCommonDef<ELF64LE>(mb, symName, archiveName); 1226e8d8bef9SDimitry Andric case ELF64BEKind: 1227e8d8bef9SDimitry Andric return isNonCommonDef<ELF64BE>(mb, symName, archiveName); 1228e8d8bef9SDimitry Andric default: 1229e8d8bef9SDimitry Andric llvm_unreachable("getELFKind"); 1230e8d8bef9SDimitry Andric } 1231e8d8bef9SDimitry Andric } 1232e8d8bef9SDimitry Andric 12330b57cec5SDimitry Andric unsigned SharedFile::vernauxNum; 12340b57cec5SDimitry Andric 12350b57cec5SDimitry Andric // Parse the version definitions in the object file if present, and return a 12360b57cec5SDimitry Andric // vector whose nth element contains a pointer to the Elf_Verdef for version 12370b57cec5SDimitry Andric // identifier n. Version identifiers that are not definitions map to nullptr. 12380b57cec5SDimitry Andric template <typename ELFT> 12390eae32dcSDimitry Andric static SmallVector<const void *, 0> 12400eae32dcSDimitry Andric parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) { 12410b57cec5SDimitry Andric if (!sec) 12420b57cec5SDimitry Andric return {}; 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric // Build the Verdefs array by following the chain of Elf_Verdef objects 12450b57cec5SDimitry Andric // from the start of the .gnu.version_d section. 12460eae32dcSDimitry Andric SmallVector<const void *, 0> verdefs; 12470b57cec5SDimitry Andric const uint8_t *verdef = base + sec->sh_offset; 12480eae32dcSDimitry Andric for (unsigned i = 0, e = sec->sh_info; i != e; ++i) { 12490b57cec5SDimitry Andric auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef); 12500b57cec5SDimitry Andric verdef += curVerdef->vd_next; 12510b57cec5SDimitry Andric unsigned verdefIndex = curVerdef->vd_ndx; 12520eae32dcSDimitry Andric if (verdefIndex >= verdefs.size()) 12530b57cec5SDimitry Andric verdefs.resize(verdefIndex + 1); 12540b57cec5SDimitry Andric verdefs[verdefIndex] = curVerdef; 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric return verdefs; 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric 12595ffd83dbSDimitry Andric // Parse SHT_GNU_verneed to properly set the name of a versioned undefined 12605ffd83dbSDimitry Andric // symbol. We detect fatal issues which would cause vulnerabilities, but do not 12615ffd83dbSDimitry Andric // implement sophisticated error checking like in llvm-readobj because the value 12625ffd83dbSDimitry Andric // of such diagnostics is low. 12635ffd83dbSDimitry Andric template <typename ELFT> 12645ffd83dbSDimitry Andric std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj, 12655ffd83dbSDimitry Andric const typename ELFT::Shdr *sec) { 12665ffd83dbSDimitry Andric if (!sec) 12675ffd83dbSDimitry Andric return {}; 12685ffd83dbSDimitry Andric std::vector<uint32_t> verneeds; 1269e8d8bef9SDimitry Andric ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(*sec), this); 12705ffd83dbSDimitry Andric const uint8_t *verneedBuf = data.begin(); 12715ffd83dbSDimitry Andric for (unsigned i = 0; i != sec->sh_info; ++i) { 12725ffd83dbSDimitry Andric if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) 12735ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Verneed"); 12745ffd83dbSDimitry Andric auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf); 12755ffd83dbSDimitry Andric const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux; 12765ffd83dbSDimitry Andric for (unsigned j = 0; j != vn->vn_cnt; ++j) { 12775ffd83dbSDimitry Andric if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) 12785ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Vernaux"); 12795ffd83dbSDimitry Andric auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf); 12805ffd83dbSDimitry Andric if (aux->vna_name >= this->stringTable.size()) 12815ffd83dbSDimitry Andric fatal(toString(this) + " has a Vernaux with an invalid vna_name"); 12825ffd83dbSDimitry Andric uint16_t version = aux->vna_other & VERSYM_VERSION; 12835ffd83dbSDimitry Andric if (version >= verneeds.size()) 12845ffd83dbSDimitry Andric verneeds.resize(version + 1); 12855ffd83dbSDimitry Andric verneeds[version] = aux->vna_name; 12865ffd83dbSDimitry Andric vernauxBuf += aux->vna_next; 12875ffd83dbSDimitry Andric } 12885ffd83dbSDimitry Andric verneedBuf += vn->vn_next; 12895ffd83dbSDimitry Andric } 12905ffd83dbSDimitry Andric return verneeds; 12915ffd83dbSDimitry Andric } 12925ffd83dbSDimitry Andric 12930b57cec5SDimitry Andric // We do not usually care about alignments of data in shared object 12940b57cec5SDimitry Andric // files because the loader takes care of it. However, if we promote a 12950b57cec5SDimitry Andric // DSO symbol to point to .bss due to copy relocation, we need to keep 12960b57cec5SDimitry Andric // the original alignment requirements. We infer it in this function. 12970b57cec5SDimitry Andric template <typename ELFT> 12980b57cec5SDimitry Andric static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections, 12990b57cec5SDimitry Andric const typename ELFT::Sym &sym) { 13000b57cec5SDimitry Andric uint64_t ret = UINT64_MAX; 13010b57cec5SDimitry Andric if (sym.st_value) 13020b57cec5SDimitry Andric ret = 1ULL << countTrailingZeros((uint64_t)sym.st_value); 13030b57cec5SDimitry Andric if (0 < sym.st_shndx && sym.st_shndx < sections.size()) 13040b57cec5SDimitry Andric ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign); 13050b57cec5SDimitry Andric return (ret > UINT32_MAX) ? 0 : ret; 13060b57cec5SDimitry Andric } 13070b57cec5SDimitry Andric 13080b57cec5SDimitry Andric // Fully parse the shared object file. 13090b57cec5SDimitry Andric // 13100b57cec5SDimitry Andric // This function parses symbol versions. If a DSO has version information, 13110b57cec5SDimitry Andric // the file has a ".gnu.version_d" section which contains symbol version 13120b57cec5SDimitry Andric // definitions. Each symbol is associated to one version through a table in 13130b57cec5SDimitry Andric // ".gnu.version" section. That table is a parallel array for the symbol 13140b57cec5SDimitry Andric // table, and each table entry contains an index in ".gnu.version_d". 13150b57cec5SDimitry Andric // 13160b57cec5SDimitry Andric // The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for 13170b57cec5SDimitry Andric // VER_NDX_GLOBAL. There's no table entry for these special versions in 13180b57cec5SDimitry Andric // ".gnu.version_d". 13190b57cec5SDimitry Andric // 13200b57cec5SDimitry Andric // The file format for symbol versioning is perhaps a bit more complicated 13210b57cec5SDimitry Andric // than necessary, but you can easily understand the code if you wrap your 13220b57cec5SDimitry Andric // head around the data structure described above. 13230b57cec5SDimitry Andric template <class ELFT> void SharedFile::parse() { 13240b57cec5SDimitry Andric using Elf_Dyn = typename ELFT::Dyn; 13250b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 13260b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 13270b57cec5SDimitry Andric using Elf_Verdef = typename ELFT::Verdef; 13280b57cec5SDimitry Andric using Elf_Versym = typename ELFT::Versym; 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric ArrayRef<Elf_Dyn> dynamicTags; 13310b57cec5SDimitry Andric const ELFFile<ELFT> obj = this->getObj<ELFT>(); 13320eae32dcSDimitry Andric ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>(); 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric const Elf_Shdr *versymSec = nullptr; 13350b57cec5SDimitry Andric const Elf_Shdr *verdefSec = nullptr; 13365ffd83dbSDimitry Andric const Elf_Shdr *verneedSec = nullptr; 13370b57cec5SDimitry Andric 13380b57cec5SDimitry Andric // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d. 13390b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) { 13400b57cec5SDimitry Andric switch (sec.sh_type) { 13410b57cec5SDimitry Andric default: 13420b57cec5SDimitry Andric continue; 13430b57cec5SDimitry Andric case SHT_DYNAMIC: 13440b57cec5SDimitry Andric dynamicTags = 1345e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this); 13460b57cec5SDimitry Andric break; 13470b57cec5SDimitry Andric case SHT_GNU_versym: 13480b57cec5SDimitry Andric versymSec = &sec; 13490b57cec5SDimitry Andric break; 13500b57cec5SDimitry Andric case SHT_GNU_verdef: 13510b57cec5SDimitry Andric verdefSec = &sec; 13520b57cec5SDimitry Andric break; 13535ffd83dbSDimitry Andric case SHT_GNU_verneed: 13545ffd83dbSDimitry Andric verneedSec = &sec; 13555ffd83dbSDimitry Andric break; 13560b57cec5SDimitry Andric } 13570b57cec5SDimitry Andric } 13580b57cec5SDimitry Andric 13590b57cec5SDimitry Andric if (versymSec && numELFSyms == 0) { 13600b57cec5SDimitry Andric error("SHT_GNU_versym should be associated with symbol table"); 13610b57cec5SDimitry Andric return; 13620b57cec5SDimitry Andric } 13630b57cec5SDimitry Andric 13640b57cec5SDimitry Andric // Search for a DT_SONAME tag to initialize this->soName. 13650b57cec5SDimitry Andric for (const Elf_Dyn &dyn : dynamicTags) { 13660b57cec5SDimitry Andric if (dyn.d_tag == DT_NEEDED) { 13670b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 13680b57cec5SDimitry Andric if (val >= this->stringTable.size()) 13690b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_NEEDED entry"); 13700b57cec5SDimitry Andric dtNeeded.push_back(this->stringTable.data() + val); 13710b57cec5SDimitry Andric } else if (dyn.d_tag == DT_SONAME) { 13720b57cec5SDimitry Andric uint64_t val = dyn.getVal(); 13730b57cec5SDimitry Andric if (val >= this->stringTable.size()) 13740b57cec5SDimitry Andric fatal(toString(this) + ": invalid DT_SONAME entry"); 13750b57cec5SDimitry Andric soName = this->stringTable.data() + val; 13760b57cec5SDimitry Andric } 13770b57cec5SDimitry Andric } 13780b57cec5SDimitry Andric 13790b57cec5SDimitry Andric // DSOs are uniquified not by filename but by soname. 138004eeddc0SDimitry Andric DenseMap<CachedHashStringRef, SharedFile *>::iterator it; 13810b57cec5SDimitry Andric bool wasInserted; 138204eeddc0SDimitry Andric std::tie(it, wasInserted) = 138304eeddc0SDimitry Andric symtab->soNames.try_emplace(CachedHashStringRef(soName), this); 13840b57cec5SDimitry Andric 13850b57cec5SDimitry Andric // If a DSO appears more than once on the command line with and without 13860b57cec5SDimitry Andric // --as-needed, --no-as-needed takes precedence over --as-needed because a 13870b57cec5SDimitry Andric // user can add an extra DSO with --no-as-needed to force it to be added to 13880b57cec5SDimitry Andric // the dependency list. 13890b57cec5SDimitry Andric it->second->isNeeded |= isNeeded; 13900b57cec5SDimitry Andric if (!wasInserted) 13910b57cec5SDimitry Andric return; 13920b57cec5SDimitry Andric 1393*81ad6265SDimitry Andric ctx->sharedFiles.push_back(this); 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec); 13965ffd83dbSDimitry Andric std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec); 13970b57cec5SDimitry Andric 13980b57cec5SDimitry Andric // Parse ".gnu.version" section which is a parallel array for the symbol 13990b57cec5SDimitry Andric // table. If a given file doesn't have a ".gnu.version" section, we use 14000b57cec5SDimitry Andric // VER_NDX_GLOBAL. 14010b57cec5SDimitry Andric size_t size = numELFSyms - firstGlobal; 14025ffd83dbSDimitry Andric std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL); 14030b57cec5SDimitry Andric if (versymSec) { 14040b57cec5SDimitry Andric ArrayRef<Elf_Versym> versym = 1405e8d8bef9SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec), 14060b57cec5SDimitry Andric this) 14070b57cec5SDimitry Andric .slice(firstGlobal); 14080b57cec5SDimitry Andric for (size_t i = 0; i < size; ++i) 14090b57cec5SDimitry Andric versyms[i] = versym[i].vs_index; 14100b57cec5SDimitry Andric } 14110b57cec5SDimitry Andric 14120b57cec5SDimitry Andric // System libraries can have a lot of symbols with versions. Using a 14130b57cec5SDimitry Andric // fixed buffer for computing the versions name (foo@ver) can save a 14140b57cec5SDimitry Andric // lot of allocations. 14150b57cec5SDimitry Andric SmallString<0> versionedNameBuffer; 14160b57cec5SDimitry Andric 14170b57cec5SDimitry Andric // Add symbols to the symbol table. 14180eae32dcSDimitry Andric SymbolTable &symtab = *elf::symtab; 14190b57cec5SDimitry Andric ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>(); 14200eae32dcSDimitry Andric for (size_t i = 0, e = syms.size(); i != e; ++i) { 14210b57cec5SDimitry Andric const Elf_Sym &sym = syms[i]; 14220b57cec5SDimitry Andric 14230b57cec5SDimitry Andric // ELF spec requires that all local symbols precede weak or global 14240b57cec5SDimitry Andric // symbols in each symbol table, and the index of first non-local symbol 14250b57cec5SDimitry Andric // is stored to sh_info. If a local symbol appears after some non-local 14260b57cec5SDimitry Andric // symbol, that's a violation of the spec. 14270eae32dcSDimitry Andric StringRef name = CHECK(sym.getName(stringTable), this); 14280b57cec5SDimitry Andric if (sym.getBinding() == STB_LOCAL) { 14290b57cec5SDimitry Andric warn("found local symbol '" + name + 14300b57cec5SDimitry Andric "' in global part of symbol table in file " + toString(this)); 14310b57cec5SDimitry Andric continue; 14320b57cec5SDimitry Andric } 14330b57cec5SDimitry Andric 14345ffd83dbSDimitry Andric uint16_t idx = versyms[i] & ~VERSYM_HIDDEN; 14350b57cec5SDimitry Andric if (sym.isUndefined()) { 14365ffd83dbSDimitry Andric // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but 14375ffd83dbSDimitry Andric // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL. 14385ffd83dbSDimitry Andric if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) { 14395ffd83dbSDimitry Andric if (idx >= verneeds.size()) { 14405ffd83dbSDimitry Andric error("corrupt input file: version need index " + Twine(idx) + 14415ffd83dbSDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 14425ffd83dbSDimitry Andric toString(this)); 14435ffd83dbSDimitry Andric continue; 14445ffd83dbSDimitry Andric } 14450eae32dcSDimitry Andric StringRef verName = stringTable.data() + verneeds[idx]; 14465ffd83dbSDimitry Andric versionedNameBuffer.clear(); 144704eeddc0SDimitry Andric name = saver().save( 144804eeddc0SDimitry Andric (name + "@" + verName).toStringRef(versionedNameBuffer)); 14495ffd83dbSDimitry Andric } 14500eae32dcSDimitry Andric Symbol *s = symtab.addSymbol( 14510b57cec5SDimitry Andric Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()}); 14520b57cec5SDimitry Andric s->exportDynamic = true; 14530eae32dcSDimitry Andric if (s->isUndefined() && sym.getBinding() != STB_WEAK && 1454fe6060f1SDimitry Andric config->unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore) 1455fe6060f1SDimitry Andric requiredSymbols.push_back(s); 14560b57cec5SDimitry Andric continue; 14570b57cec5SDimitry Andric } 14580b57cec5SDimitry Andric 14590b57cec5SDimitry Andric // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly 14600b57cec5SDimitry Andric // assigns VER_NDX_LOCAL to this section global symbol. Here is a 14610b57cec5SDimitry Andric // workaround for this bug. 14620b57cec5SDimitry Andric if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL && 14630b57cec5SDimitry Andric name == "_gp_disp") 14640b57cec5SDimitry Andric continue; 14650b57cec5SDimitry Andric 14660b57cec5SDimitry Andric uint32_t alignment = getAlignment<ELFT>(sections, sym); 14670b57cec5SDimitry Andric if (!(versyms[i] & VERSYM_HIDDEN)) { 1468*81ad6265SDimitry Andric auto *s = symtab.addSymbol( 1469*81ad6265SDimitry Andric SharedSymbol{*this, name, sym.getBinding(), sym.st_other, 1470*81ad6265SDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 1471*81ad6265SDimitry Andric if (s->file == this) 1472*81ad6265SDimitry Andric s->verdefIndex = idx; 14730b57cec5SDimitry Andric } 14740b57cec5SDimitry Andric 14750b57cec5SDimitry Andric // Also add the symbol with the versioned name to handle undefined symbols 14760b57cec5SDimitry Andric // with explicit versions. 14770b57cec5SDimitry Andric if (idx == VER_NDX_GLOBAL) 14780b57cec5SDimitry Andric continue; 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) { 14810b57cec5SDimitry Andric error("corrupt input file: version definition index " + Twine(idx) + 14820b57cec5SDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 14830b57cec5SDimitry Andric toString(this)); 14840b57cec5SDimitry Andric continue; 14850b57cec5SDimitry Andric } 14860b57cec5SDimitry Andric 14870b57cec5SDimitry Andric StringRef verName = 14880eae32dcSDimitry Andric stringTable.data() + 14890b57cec5SDimitry Andric reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name; 14900b57cec5SDimitry Andric versionedNameBuffer.clear(); 14910b57cec5SDimitry Andric name = (name + "@" + verName).toStringRef(versionedNameBuffer); 1492*81ad6265SDimitry Andric auto *s = symtab.addSymbol( 1493*81ad6265SDimitry Andric SharedSymbol{*this, saver().save(name), sym.getBinding(), sym.st_other, 1494*81ad6265SDimitry Andric sym.getType(), sym.st_value, sym.st_size, alignment}); 1495*81ad6265SDimitry Andric if (s->file == this) 1496*81ad6265SDimitry Andric s->verdefIndex = idx; 14970b57cec5SDimitry Andric } 14980b57cec5SDimitry Andric } 14990b57cec5SDimitry Andric 15000b57cec5SDimitry Andric static ELFKind getBitcodeELFKind(const Triple &t) { 15010b57cec5SDimitry Andric if (t.isLittleEndian()) 15020b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 15030b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 15040b57cec5SDimitry Andric } 15050b57cec5SDimitry Andric 1506e8d8bef9SDimitry Andric static uint16_t getBitcodeMachineKind(StringRef path, const Triple &t) { 15070b57cec5SDimitry Andric switch (t.getArch()) { 15080b57cec5SDimitry Andric case Triple::aarch64: 1509fe6060f1SDimitry Andric case Triple::aarch64_be: 15100b57cec5SDimitry Andric return EM_AARCH64; 15110b57cec5SDimitry Andric case Triple::amdgcn: 15120b57cec5SDimitry Andric case Triple::r600: 15130b57cec5SDimitry Andric return EM_AMDGPU; 15140b57cec5SDimitry Andric case Triple::arm: 15150b57cec5SDimitry Andric case Triple::thumb: 15160b57cec5SDimitry Andric return EM_ARM; 15170b57cec5SDimitry Andric case Triple::avr: 15180b57cec5SDimitry Andric return EM_AVR; 1519349cc55cSDimitry Andric case Triple::hexagon: 1520349cc55cSDimitry Andric return EM_HEXAGON; 15210b57cec5SDimitry Andric case Triple::mips: 15220b57cec5SDimitry Andric case Triple::mipsel: 15230b57cec5SDimitry Andric case Triple::mips64: 15240b57cec5SDimitry Andric case Triple::mips64el: 15250b57cec5SDimitry Andric return EM_MIPS; 15260b57cec5SDimitry Andric case Triple::msp430: 15270b57cec5SDimitry Andric return EM_MSP430; 15280b57cec5SDimitry Andric case Triple::ppc: 1529e8d8bef9SDimitry Andric case Triple::ppcle: 15300b57cec5SDimitry Andric return EM_PPC; 15310b57cec5SDimitry Andric case Triple::ppc64: 15320b57cec5SDimitry Andric case Triple::ppc64le: 15330b57cec5SDimitry Andric return EM_PPC64; 15340b57cec5SDimitry Andric case Triple::riscv32: 15350b57cec5SDimitry Andric case Triple::riscv64: 15360b57cec5SDimitry Andric return EM_RISCV; 15370b57cec5SDimitry Andric case Triple::x86: 15380b57cec5SDimitry Andric return t.isOSIAMCU() ? EM_IAMCU : EM_386; 15390b57cec5SDimitry Andric case Triple::x86_64: 15400b57cec5SDimitry Andric return EM_X86_64; 15410b57cec5SDimitry Andric default: 15420b57cec5SDimitry Andric error(path + ": could not infer e_machine from bitcode target triple " + 15430b57cec5SDimitry Andric t.str()); 15440b57cec5SDimitry Andric return EM_NONE; 15450b57cec5SDimitry Andric } 15460b57cec5SDimitry Andric } 15470b57cec5SDimitry Andric 1548e8d8bef9SDimitry Andric static uint8_t getOsAbi(const Triple &t) { 1549e8d8bef9SDimitry Andric switch (t.getOS()) { 1550e8d8bef9SDimitry Andric case Triple::AMDHSA: 1551e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_HSA; 1552e8d8bef9SDimitry Andric case Triple::AMDPAL: 1553e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_PAL; 1554e8d8bef9SDimitry Andric case Triple::Mesa3D: 1555e8d8bef9SDimitry Andric return ELF::ELFOSABI_AMDGPU_MESA3D; 1556e8d8bef9SDimitry Andric default: 1557e8d8bef9SDimitry Andric return ELF::ELFOSABI_NONE; 1558e8d8bef9SDimitry Andric } 1559e8d8bef9SDimitry Andric } 1560e8d8bef9SDimitry Andric 15610b57cec5SDimitry Andric BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName, 15620eae32dcSDimitry Andric uint64_t offsetInArchive, bool lazy) 15630b57cec5SDimitry Andric : InputFile(BitcodeKind, mb) { 15640eae32dcSDimitry Andric this->archiveName = archiveName; 15650eae32dcSDimitry Andric this->lazy = lazy; 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric std::string path = mb.getBufferIdentifier().str(); 15680b57cec5SDimitry Andric if (config->thinLTOIndexOnly) 15690b57cec5SDimitry Andric path = replaceThinLTOSuffix(mb.getBufferIdentifier()); 15700b57cec5SDimitry Andric 15710b57cec5SDimitry Andric // ThinLTO assumes that all MemoryBufferRefs given to it have a unique 15720b57cec5SDimitry Andric // name. If two archives define two members with the same name, this 15730b57cec5SDimitry Andric // causes a collision which result in only one of the objects being taken 15740b57cec5SDimitry Andric // into consideration at LTO time (which very likely causes undefined 15750b57cec5SDimitry Andric // symbols later in the link stage). So we append file offset to make 15760b57cec5SDimitry Andric // filename unique. 157704eeddc0SDimitry Andric StringRef name = archiveName.empty() 157804eeddc0SDimitry Andric ? saver().save(path) 157904eeddc0SDimitry Andric : saver().save(archiveName + "(" + path::filename(path) + 158004eeddc0SDimitry Andric " at " + utostr(offsetInArchive) + ")"); 15810b57cec5SDimitry Andric MemoryBufferRef mbref(mb.getBuffer(), name); 15820b57cec5SDimitry Andric 15830b57cec5SDimitry Andric obj = CHECK(lto::InputFile::create(mbref), this); 15840b57cec5SDimitry Andric 15850b57cec5SDimitry Andric Triple t(obj->getTargetTriple()); 15860b57cec5SDimitry Andric ekind = getBitcodeELFKind(t); 15870b57cec5SDimitry Andric emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t); 1588e8d8bef9SDimitry Andric osabi = getOsAbi(t); 15890b57cec5SDimitry Andric } 15900b57cec5SDimitry Andric 15910b57cec5SDimitry Andric static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 15920b57cec5SDimitry Andric switch (gvVisibility) { 15930b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: 15940b57cec5SDimitry Andric return STV_DEFAULT; 15950b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: 15960b57cec5SDimitry Andric return STV_HIDDEN; 15970b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: 15980b57cec5SDimitry Andric return STV_PROTECTED; 15990b57cec5SDimitry Andric } 16000b57cec5SDimitry Andric llvm_unreachable("unknown visibility"); 16010b57cec5SDimitry Andric } 16020b57cec5SDimitry Andric 16030b57cec5SDimitry Andric template <class ELFT> 160404eeddc0SDimitry Andric static void 160504eeddc0SDimitry Andric createBitcodeSymbol(Symbol *&sym, const std::vector<bool> &keptComdats, 160604eeddc0SDimitry Andric const lto::InputFile::Symbol &objSym, BitcodeFile &f) { 16070b57cec5SDimitry Andric uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL; 16080b57cec5SDimitry Andric uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE; 16090b57cec5SDimitry Andric uint8_t visibility = mapVisibility(objSym.getVisibility()); 16100b57cec5SDimitry Andric 1611*81ad6265SDimitry Andric if (!sym) 1612*81ad6265SDimitry Andric sym = symtab->insert(saver().save(objSym.getName())); 161304eeddc0SDimitry Andric 16140b57cec5SDimitry Andric int c = objSym.getComdatIndex(); 16150b57cec5SDimitry Andric if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) { 1616*81ad6265SDimitry Andric Undefined newSym(&f, StringRef(), binding, visibility, type); 161704eeddc0SDimitry Andric sym->resolve(newSym); 161804eeddc0SDimitry Andric sym->referenced = true; 161904eeddc0SDimitry Andric return; 16200b57cec5SDimitry Andric } 16210b57cec5SDimitry Andric 162204eeddc0SDimitry Andric if (objSym.isCommon()) { 1623*81ad6265SDimitry Andric sym->resolve(CommonSymbol{&f, StringRef(), binding, visibility, STT_OBJECT, 162404eeddc0SDimitry Andric objSym.getCommonAlignment(), 162504eeddc0SDimitry Andric objSym.getCommonSize()}); 162604eeddc0SDimitry Andric } else { 1627*81ad6265SDimitry Andric Defined newSym(&f, StringRef(), binding, visibility, type, 0, 0, nullptr); 1628*81ad6265SDimitry Andric if (objSym.canBeOmittedFromSymbolTable()) 162985868e8aSDimitry Andric newSym.exportDynamic = false; 163004eeddc0SDimitry Andric sym->resolve(newSym); 163104eeddc0SDimitry Andric } 16320b57cec5SDimitry Andric } 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric template <class ELFT> void BitcodeFile::parse() { 1635fe6060f1SDimitry Andric for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) { 16360b57cec5SDimitry Andric keptComdats.push_back( 1637fe6060f1SDimitry Andric s.second == Comdat::NoDeduplicate || 1638fe6060f1SDimitry Andric symtab->comdatGroups.try_emplace(CachedHashStringRef(s.first), this) 1639fe6060f1SDimitry Andric .second); 1640fe6060f1SDimitry Andric } 16410b57cec5SDimitry Andric 164204eeddc0SDimitry Andric symbols.resize(obj->symbols().size()); 1643*81ad6265SDimitry Andric // Process defined symbols first. See the comment in 1644*81ad6265SDimitry Andric // ObjFile<ELFT>::initializeSymbols. 1645*81ad6265SDimitry Andric for (auto it : llvm::enumerate(obj->symbols())) 1646*81ad6265SDimitry Andric if (!it.value().isUndefined()) { 1647*81ad6265SDimitry Andric Symbol *&sym = symbols[it.index()]; 1648*81ad6265SDimitry Andric createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this); 1649*81ad6265SDimitry Andric } 1650*81ad6265SDimitry Andric for (auto it : llvm::enumerate(obj->symbols())) 1651*81ad6265SDimitry Andric if (it.value().isUndefined()) { 165204eeddc0SDimitry Andric Symbol *&sym = symbols[it.index()]; 165304eeddc0SDimitry Andric createBitcodeSymbol<ELFT>(sym, keptComdats, it.value(), *this); 165404eeddc0SDimitry Andric } 16550b57cec5SDimitry Andric 16560b57cec5SDimitry Andric for (auto l : obj->getDependentLibraries()) 16570b57cec5SDimitry Andric addDependentLibrary(l, this); 16580b57cec5SDimitry Andric } 16590b57cec5SDimitry Andric 16600eae32dcSDimitry Andric void BitcodeFile::parseLazy() { 16610eae32dcSDimitry Andric SymbolTable &symtab = *elf::symtab; 166204eeddc0SDimitry Andric symbols.resize(obj->symbols().size()); 166304eeddc0SDimitry Andric for (auto it : llvm::enumerate(obj->symbols())) 1664*81ad6265SDimitry Andric if (!it.value().isUndefined()) { 1665*81ad6265SDimitry Andric auto *sym = symtab.insert(saver().save(it.value().getName())); 1666*81ad6265SDimitry Andric sym->resolve(LazyObject{*this}); 1667*81ad6265SDimitry Andric symbols[it.index()] = sym; 1668*81ad6265SDimitry Andric } 1669*81ad6265SDimitry Andric } 1670*81ad6265SDimitry Andric 1671*81ad6265SDimitry Andric void BitcodeFile::postParse() { 1672*81ad6265SDimitry Andric for (auto it : llvm::enumerate(obj->symbols())) { 1673*81ad6265SDimitry Andric const Symbol &sym = *symbols[it.index()]; 1674*81ad6265SDimitry Andric const auto &objSym = it.value(); 1675*81ad6265SDimitry Andric if (sym.file == this || !sym.isDefined() || objSym.isUndefined() || 1676*81ad6265SDimitry Andric objSym.isCommon() || objSym.isWeak()) 1677*81ad6265SDimitry Andric continue; 1678*81ad6265SDimitry Andric int c = objSym.getComdatIndex(); 1679*81ad6265SDimitry Andric if (c != -1 && !keptComdats[c]) 1680*81ad6265SDimitry Andric continue; 1681*81ad6265SDimitry Andric reportDuplicate(sym, this, nullptr, 0); 1682*81ad6265SDimitry Andric } 16830eae32dcSDimitry Andric } 16840eae32dcSDimitry Andric 16850b57cec5SDimitry Andric void BinaryFile::parse() { 16860b57cec5SDimitry Andric ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer()); 16870b57cec5SDimitry Andric auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 16880b57cec5SDimitry Andric 8, data, ".data"); 16890b57cec5SDimitry Andric sections.push_back(section); 16900b57cec5SDimitry Andric 16910b57cec5SDimitry Andric // For each input file foo that is embedded to a result as a binary 16920b57cec5SDimitry Andric // blob, we define _binary_foo_{start,end,size} symbols, so that 16930b57cec5SDimitry Andric // user programs can access blobs by name. Non-alphanumeric 16940b57cec5SDimitry Andric // characters in a filename are replaced with underscore. 16950b57cec5SDimitry Andric std::string s = "_binary_" + mb.getBufferIdentifier().str(); 16960b57cec5SDimitry Andric for (size_t i = 0; i < s.size(); ++i) 16970b57cec5SDimitry Andric if (!isAlnum(s[i])) 16980b57cec5SDimitry Andric s[i] = '_'; 16990b57cec5SDimitry Andric 170004eeddc0SDimitry Andric llvm::StringSaver &saver = lld::saver(); 170104eeddc0SDimitry Andric 1702*81ad6265SDimitry Andric symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_start"), 1703*81ad6265SDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 0, 1704*81ad6265SDimitry Andric 0, section}); 1705*81ad6265SDimitry Andric symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_end"), 1706*81ad6265SDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 1707*81ad6265SDimitry Andric data.size(), 0, section}); 1708*81ad6265SDimitry Andric symtab->addAndCheckDuplicate(Defined{nullptr, saver.save(s + "_size"), 1709*81ad6265SDimitry Andric STB_GLOBAL, STV_DEFAULT, STT_OBJECT, 1710*81ad6265SDimitry Andric data.size(), 0, nullptr}); 17110b57cec5SDimitry Andric } 17120b57cec5SDimitry Andric 17135ffd83dbSDimitry Andric InputFile *elf::createObjectFile(MemoryBufferRef mb, StringRef archiveName, 17140b57cec5SDimitry Andric uint64_t offsetInArchive) { 17150b57cec5SDimitry Andric if (isBitcode(mb)) 17160eae32dcSDimitry Andric return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/false); 17170b57cec5SDimitry Andric 17180b57cec5SDimitry Andric switch (getELFKind(mb, archiveName)) { 17190b57cec5SDimitry Andric case ELF32LEKind: 17200b57cec5SDimitry Andric return make<ObjFile<ELF32LE>>(mb, archiveName); 17210b57cec5SDimitry Andric case ELF32BEKind: 17220b57cec5SDimitry Andric return make<ObjFile<ELF32BE>>(mb, archiveName); 17230b57cec5SDimitry Andric case ELF64LEKind: 17240b57cec5SDimitry Andric return make<ObjFile<ELF64LE>>(mb, archiveName); 17250b57cec5SDimitry Andric case ELF64BEKind: 17260b57cec5SDimitry Andric return make<ObjFile<ELF64BE>>(mb, archiveName); 17270b57cec5SDimitry Andric default: 17280b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 17290b57cec5SDimitry Andric } 17300b57cec5SDimitry Andric } 17310b57cec5SDimitry Andric 17320eae32dcSDimitry Andric InputFile *elf::createLazyFile(MemoryBufferRef mb, StringRef archiveName, 17330eae32dcSDimitry Andric uint64_t offsetInArchive) { 17340eae32dcSDimitry Andric if (isBitcode(mb)) 17350eae32dcSDimitry Andric return make<BitcodeFile>(mb, archiveName, offsetInArchive, /*lazy=*/true); 17360b57cec5SDimitry Andric 17370eae32dcSDimitry Andric auto *file = 17380eae32dcSDimitry Andric cast<ELFFileBase>(createObjectFile(mb, archiveName, offsetInArchive)); 17390eae32dcSDimitry Andric file->lazy = true; 17400eae32dcSDimitry Andric return file; 17410b57cec5SDimitry Andric } 17420b57cec5SDimitry Andric 17430eae32dcSDimitry Andric template <class ELFT> void ObjFile<ELFT>::parseLazy() { 17440eae32dcSDimitry Andric const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>(); 17450eae32dcSDimitry Andric SymbolTable &symtab = *elf::symtab; 17460b57cec5SDimitry Andric 17470eae32dcSDimitry Andric symbols.resize(eSyms.size()); 17480b57cec5SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) 17490b57cec5SDimitry Andric if (eSyms[i].st_shndx != SHN_UNDEF) 17500eae32dcSDimitry Andric symbols[i] = symtab.insert(CHECK(eSyms[i].getName(stringTable), this)); 17510b57cec5SDimitry Andric 17520b57cec5SDimitry Andric // Replace existing symbols with LazyObject symbols. 17530b57cec5SDimitry Andric // 17540eae32dcSDimitry Andric // resolve() may trigger this->extract() if an existing symbol is an undefined 17550eae32dcSDimitry Andric // symbol. If that happens, this function has served its purpose, and we can 17560eae32dcSDimitry Andric // exit from the loop early. 17570eae32dcSDimitry Andric for (Symbol *sym : makeArrayRef(symbols).slice(firstGlobal)) 17580eae32dcSDimitry Andric if (sym) { 1759*81ad6265SDimitry Andric sym->resolve(LazyObject{*this}); 17600eae32dcSDimitry Andric if (!lazy) 17610b57cec5SDimitry Andric return; 17620b57cec5SDimitry Andric } 17630b57cec5SDimitry Andric } 17640b57cec5SDimitry Andric 17650eae32dcSDimitry Andric bool InputFile::shouldExtractForCommon(StringRef name) { 1766e8d8bef9SDimitry Andric if (isBitcode(mb)) 1767e8d8bef9SDimitry Andric return isBitcodeNonCommonDef(mb, name, archiveName); 1768e8d8bef9SDimitry Andric 1769e8d8bef9SDimitry Andric return isNonCommonDef(mb, name, archiveName); 1770e8d8bef9SDimitry Andric } 1771e8d8bef9SDimitry Andric 17725ffd83dbSDimitry Andric std::string elf::replaceThinLTOSuffix(StringRef path) { 17730b57cec5SDimitry Andric StringRef suffix = config->thinLTOObjectSuffixReplace.first; 17740b57cec5SDimitry Andric StringRef repl = config->thinLTOObjectSuffixReplace.second; 17750b57cec5SDimitry Andric 17760b57cec5SDimitry Andric if (path.consume_back(suffix)) 17770b57cec5SDimitry Andric return (path + repl).str(); 17785ffd83dbSDimitry Andric return std::string(path); 17790b57cec5SDimitry Andric } 17800b57cec5SDimitry Andric 17810b57cec5SDimitry Andric template void BitcodeFile::parse<ELF32LE>(); 17820b57cec5SDimitry Andric template void BitcodeFile::parse<ELF32BE>(); 17830b57cec5SDimitry Andric template void BitcodeFile::parse<ELF64LE>(); 17840b57cec5SDimitry Andric template void BitcodeFile::parse<ELF64BE>(); 17850b57cec5SDimitry Andric 17865ffd83dbSDimitry Andric template class elf::ObjFile<ELF32LE>; 17875ffd83dbSDimitry Andric template class elf::ObjFile<ELF32BE>; 17885ffd83dbSDimitry Andric template class elf::ObjFile<ELF64LE>; 17895ffd83dbSDimitry Andric template class elf::ObjFile<ELF64BE>; 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric template void SharedFile::parse<ELF32LE>(); 17920b57cec5SDimitry Andric template void SharedFile::parse<ELF32BE>(); 17930b57cec5SDimitry Andric template void SharedFile::parse<ELF64LE>(); 17940b57cec5SDimitry Andric template void SharedFile::parse<ELF64BE>(); 1795