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" 100b57cec5SDimitry Andric #include "Driver.h" 110b57cec5SDimitry Andric #include "InputSection.h" 120b57cec5SDimitry Andric #include "LinkerScript.h" 130b57cec5SDimitry Andric #include "SymbolTable.h" 140b57cec5SDimitry Andric #include "Symbols.h" 150b57cec5SDimitry Andric #include "SyntheticSections.h" 16480093f4SDimitry Andric #include "lld/Common/DWARF.h" 170b57cec5SDimitry Andric #include "lld/Common/ErrorHandler.h" 180b57cec5SDimitry Andric #include "lld/Common/Memory.h" 190b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/Analysis.h" 210b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h" 220b57cec5SDimitry Andric #include "llvm/IR/Module.h" 230b57cec5SDimitry Andric #include "llvm/LTO/LTO.h" 240b57cec5SDimitry Andric #include "llvm/MC/StringTableBuilder.h" 250b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h" 260b57cec5SDimitry Andric #include "llvm/Support/ARMAttributeParser.h" 270b57cec5SDimitry Andric #include "llvm/Support/ARMBuildAttributes.h" 280b57cec5SDimitry Andric #include "llvm/Support/Endian.h" 290b57cec5SDimitry Andric #include "llvm/Support/Path.h" 300b57cec5SDimitry Andric #include "llvm/Support/TarWriter.h" 310b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric using namespace llvm; 340b57cec5SDimitry Andric using namespace llvm::ELF; 350b57cec5SDimitry Andric using namespace llvm::object; 360b57cec5SDimitry Andric using namespace llvm::sys; 370b57cec5SDimitry Andric using namespace llvm::sys::fs; 380b57cec5SDimitry Andric using namespace llvm::support::endian; 39*5ffd83dbSDimitry Andric using namespace lld; 40*5ffd83dbSDimitry Andric using namespace lld::elf; 410b57cec5SDimitry Andric 42*5ffd83dbSDimitry Andric bool InputFile::isInGroup; 43*5ffd83dbSDimitry Andric uint32_t InputFile::nextGroupId; 44*5ffd83dbSDimitry Andric 45*5ffd83dbSDimitry Andric std::vector<ArchiveFile *> elf::archiveFiles; 46*5ffd83dbSDimitry Andric std::vector<BinaryFile *> elf::binaryFiles; 47*5ffd83dbSDimitry Andric std::vector<BitcodeFile *> elf::bitcodeFiles; 48*5ffd83dbSDimitry Andric std::vector<LazyObjFile *> elf::lazyObjFiles; 49*5ffd83dbSDimitry Andric std::vector<InputFile *> elf::objectFiles; 50*5ffd83dbSDimitry Andric std::vector<SharedFile *> elf::sharedFiles; 51*5ffd83dbSDimitry Andric 52*5ffd83dbSDimitry Andric std::unique_ptr<TarWriter> elf::tar; 53*5ffd83dbSDimitry Andric 5485868e8aSDimitry Andric // Returns "<internal>", "foo.a(bar.o)" or "baz.o". 55*5ffd83dbSDimitry Andric std::string lld::toString(const InputFile *f) { 5685868e8aSDimitry Andric if (!f) 5785868e8aSDimitry Andric return "<internal>"; 580b57cec5SDimitry Andric 5985868e8aSDimitry Andric if (f->toStringCache.empty()) { 6085868e8aSDimitry Andric if (f->archiveName.empty()) 61*5ffd83dbSDimitry Andric f->toStringCache = std::string(f->getName()); 6285868e8aSDimitry Andric else 6385868e8aSDimitry Andric f->toStringCache = (f->archiveName + "(" + f->getName() + ")").str(); 6485868e8aSDimitry Andric } 6585868e8aSDimitry Andric return f->toStringCache; 6685868e8aSDimitry Andric } 6785868e8aSDimitry Andric 680b57cec5SDimitry Andric static ELFKind getELFKind(MemoryBufferRef mb, StringRef archiveName) { 690b57cec5SDimitry Andric unsigned char size; 700b57cec5SDimitry Andric unsigned char endian; 710b57cec5SDimitry Andric std::tie(size, endian) = getElfArchType(mb.getBuffer()); 720b57cec5SDimitry Andric 730b57cec5SDimitry Andric auto report = [&](StringRef msg) { 740b57cec5SDimitry Andric StringRef filename = mb.getBufferIdentifier(); 750b57cec5SDimitry Andric if (archiveName.empty()) 760b57cec5SDimitry Andric fatal(filename + ": " + msg); 770b57cec5SDimitry Andric else 780b57cec5SDimitry Andric fatal(archiveName + "(" + filename + "): " + msg); 790b57cec5SDimitry Andric }; 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric if (!mb.getBuffer().startswith(ElfMagic)) 820b57cec5SDimitry Andric report("not an ELF file"); 830b57cec5SDimitry Andric if (endian != ELFDATA2LSB && endian != ELFDATA2MSB) 840b57cec5SDimitry Andric report("corrupted ELF file: invalid data encoding"); 850b57cec5SDimitry Andric if (size != ELFCLASS32 && size != ELFCLASS64) 860b57cec5SDimitry Andric report("corrupted ELF file: invalid file class"); 870b57cec5SDimitry Andric 880b57cec5SDimitry Andric size_t bufSize = mb.getBuffer().size(); 890b57cec5SDimitry Andric if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) || 900b57cec5SDimitry Andric (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr))) 910b57cec5SDimitry Andric report("corrupted ELF file: file is too short"); 920b57cec5SDimitry Andric 930b57cec5SDimitry Andric if (size == ELFCLASS32) 940b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind; 950b57cec5SDimitry Andric return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind; 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric InputFile::InputFile(Kind k, MemoryBufferRef m) 990b57cec5SDimitry Andric : mb(m), groupId(nextGroupId), fileKind(k) { 1000b57cec5SDimitry Andric // All files within the same --{start,end}-group get the same group ID. 1010b57cec5SDimitry Andric // Otherwise, a new file will get a new group ID. 1020b57cec5SDimitry Andric if (!isInGroup) 1030b57cec5SDimitry Andric ++nextGroupId; 1040b57cec5SDimitry Andric } 1050b57cec5SDimitry Andric 106*5ffd83dbSDimitry Andric Optional<MemoryBufferRef> elf::readFile(StringRef path) { 1070b57cec5SDimitry Andric // The --chroot option changes our virtual root directory. 1080b57cec5SDimitry Andric // This is useful when you are dealing with files created by --reproduce. 1090b57cec5SDimitry Andric if (!config->chroot.empty() && path.startswith("/")) 1100b57cec5SDimitry Andric path = saver.save(config->chroot + path); 1110b57cec5SDimitry Andric 1120b57cec5SDimitry Andric log(path); 1130b57cec5SDimitry Andric 1140b57cec5SDimitry Andric auto mbOrErr = MemoryBuffer::getFile(path, -1, false); 1150b57cec5SDimitry Andric if (auto ec = mbOrErr.getError()) { 1160b57cec5SDimitry Andric error("cannot open " + path + ": " + ec.message()); 1170b57cec5SDimitry Andric return None; 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric std::unique_ptr<MemoryBuffer> &mb = *mbOrErr; 1210b57cec5SDimitry Andric MemoryBufferRef mbref = mb->getMemBufferRef(); 1220b57cec5SDimitry Andric make<std::unique_ptr<MemoryBuffer>>(std::move(mb)); // take MB ownership 1230b57cec5SDimitry Andric 1240b57cec5SDimitry Andric if (tar) 1250b57cec5SDimitry Andric tar->append(relativeToRoot(path), mbref.getBuffer()); 1260b57cec5SDimitry Andric return mbref; 1270b57cec5SDimitry Andric } 1280b57cec5SDimitry Andric 1290b57cec5SDimitry Andric // All input object files must be for the same architecture 1300b57cec5SDimitry Andric // (e.g. it does not make sense to link x86 object files with 1310b57cec5SDimitry Andric // MIPS object files.) This function checks for that error. 1320b57cec5SDimitry Andric static bool isCompatible(InputFile *file) { 1330b57cec5SDimitry Andric if (!file->isElf() && !isa<BitcodeFile>(file)) 1340b57cec5SDimitry Andric return true; 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric if (file->ekind == config->ekind && file->emachine == config->emachine) { 1370b57cec5SDimitry Andric if (config->emachine != EM_MIPS) 1380b57cec5SDimitry Andric return true; 1390b57cec5SDimitry Andric if (isMipsN32Abi(file) == config->mipsN32Abi) 1400b57cec5SDimitry Andric return true; 1410b57cec5SDimitry Andric } 1420b57cec5SDimitry Andric 143*5ffd83dbSDimitry Andric StringRef target = 144*5ffd83dbSDimitry Andric !config->bfdname.empty() ? config->bfdname : config->emulation; 145*5ffd83dbSDimitry Andric if (!target.empty()) { 146*5ffd83dbSDimitry Andric error(toString(file) + " is incompatible with " + target); 14785868e8aSDimitry Andric return false; 14885868e8aSDimitry Andric } 14985868e8aSDimitry Andric 1500b57cec5SDimitry Andric InputFile *existing; 1510b57cec5SDimitry Andric if (!objectFiles.empty()) 1520b57cec5SDimitry Andric existing = objectFiles[0]; 1530b57cec5SDimitry Andric else if (!sharedFiles.empty()) 1540b57cec5SDimitry Andric existing = sharedFiles[0]; 155*5ffd83dbSDimitry Andric else if (!bitcodeFiles.empty()) 1560b57cec5SDimitry Andric existing = bitcodeFiles[0]; 157*5ffd83dbSDimitry Andric else 158*5ffd83dbSDimitry Andric llvm_unreachable("Must have -m, OUTPUT_FORMAT or existing input file to " 159*5ffd83dbSDimitry Andric "determine target emulation"); 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric error(toString(file) + " is incompatible with " + toString(existing)); 1620b57cec5SDimitry Andric return false; 1630b57cec5SDimitry Andric } 1640b57cec5SDimitry Andric 1650b57cec5SDimitry Andric template <class ELFT> static void doParseFile(InputFile *file) { 1660b57cec5SDimitry Andric if (!isCompatible(file)) 1670b57cec5SDimitry Andric return; 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric // Binary file 1700b57cec5SDimitry Andric if (auto *f = dyn_cast<BinaryFile>(file)) { 1710b57cec5SDimitry Andric binaryFiles.push_back(f); 1720b57cec5SDimitry Andric f->parse(); 1730b57cec5SDimitry Andric return; 1740b57cec5SDimitry Andric } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric // .a file 1770b57cec5SDimitry Andric if (auto *f = dyn_cast<ArchiveFile>(file)) { 178*5ffd83dbSDimitry Andric archiveFiles.push_back(f); 1790b57cec5SDimitry Andric f->parse(); 1800b57cec5SDimitry Andric return; 1810b57cec5SDimitry Andric } 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric // Lazy object file 1840b57cec5SDimitry Andric if (auto *f = dyn_cast<LazyObjFile>(file)) { 1850b57cec5SDimitry Andric lazyObjFiles.push_back(f); 1860b57cec5SDimitry Andric f->parse<ELFT>(); 1870b57cec5SDimitry Andric return; 1880b57cec5SDimitry Andric } 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric if (config->trace) 1910b57cec5SDimitry Andric message(toString(file)); 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric // .so file 1940b57cec5SDimitry Andric if (auto *f = dyn_cast<SharedFile>(file)) { 1950b57cec5SDimitry Andric f->parse<ELFT>(); 1960b57cec5SDimitry Andric return; 1970b57cec5SDimitry Andric } 1980b57cec5SDimitry Andric 1990b57cec5SDimitry Andric // LLVM bitcode file 2000b57cec5SDimitry Andric if (auto *f = dyn_cast<BitcodeFile>(file)) { 2010b57cec5SDimitry Andric bitcodeFiles.push_back(f); 2020b57cec5SDimitry Andric f->parse<ELFT>(); 2030b57cec5SDimitry Andric return; 2040b57cec5SDimitry Andric } 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Regular object file 2070b57cec5SDimitry Andric objectFiles.push_back(file); 2080b57cec5SDimitry Andric cast<ObjFile<ELFT>>(file)->parse(); 2090b57cec5SDimitry Andric } 2100b57cec5SDimitry Andric 2110b57cec5SDimitry Andric // Add symbols in File to the symbol table. 212*5ffd83dbSDimitry Andric void elf::parseFile(InputFile *file) { 2130b57cec5SDimitry Andric switch (config->ekind) { 2140b57cec5SDimitry Andric case ELF32LEKind: 2150b57cec5SDimitry Andric doParseFile<ELF32LE>(file); 2160b57cec5SDimitry Andric return; 2170b57cec5SDimitry Andric case ELF32BEKind: 2180b57cec5SDimitry Andric doParseFile<ELF32BE>(file); 2190b57cec5SDimitry Andric return; 2200b57cec5SDimitry Andric case ELF64LEKind: 2210b57cec5SDimitry Andric doParseFile<ELF64LE>(file); 2220b57cec5SDimitry Andric return; 2230b57cec5SDimitry Andric case ELF64BEKind: 2240b57cec5SDimitry Andric doParseFile<ELF64BE>(file); 2250b57cec5SDimitry Andric return; 2260b57cec5SDimitry Andric default: 2270b57cec5SDimitry Andric llvm_unreachable("unknown ELFT"); 2280b57cec5SDimitry Andric } 2290b57cec5SDimitry Andric } 2300b57cec5SDimitry Andric 2310b57cec5SDimitry Andric // Concatenates arguments to construct a string representing an error location. 2320b57cec5SDimitry Andric static std::string createFileLineMsg(StringRef path, unsigned line) { 233*5ffd83dbSDimitry Andric std::string filename = std::string(path::filename(path)); 2340b57cec5SDimitry Andric std::string lineno = ":" + std::to_string(line); 2350b57cec5SDimitry Andric if (filename == path) 2360b57cec5SDimitry Andric return filename + lineno; 2370b57cec5SDimitry Andric return filename + lineno + " (" + path.str() + lineno + ")"; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric template <class ELFT> 2410b57cec5SDimitry Andric static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym, 2420b57cec5SDimitry Andric InputSectionBase &sec, uint64_t offset) { 2430b57cec5SDimitry Andric // In DWARF, functions and variables are stored to different places. 2440b57cec5SDimitry Andric // First, lookup a function for a given offset. 2450b57cec5SDimitry Andric if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset)) 2460b57cec5SDimitry Andric return createFileLineMsg(info->FileName, info->Line); 2470b57cec5SDimitry Andric 2480b57cec5SDimitry Andric // If it failed, lookup again as a variable. 2490b57cec5SDimitry Andric if (Optional<std::pair<std::string, unsigned>> fileLine = 2500b57cec5SDimitry Andric file.getVariableLoc(sym.getName())) 2510b57cec5SDimitry Andric return createFileLineMsg(fileLine->first, fileLine->second); 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // File.sourceFile contains STT_FILE symbol, and that is a last resort. 254*5ffd83dbSDimitry Andric return std::string(file.sourceFile); 2550b57cec5SDimitry Andric } 2560b57cec5SDimitry Andric 2570b57cec5SDimitry Andric std::string InputFile::getSrcMsg(const Symbol &sym, InputSectionBase &sec, 2580b57cec5SDimitry Andric uint64_t offset) { 2590b57cec5SDimitry Andric if (kind() != ObjKind) 2600b57cec5SDimitry Andric return ""; 2610b57cec5SDimitry Andric switch (config->ekind) { 2620b57cec5SDimitry Andric default: 2630b57cec5SDimitry Andric llvm_unreachable("Invalid kind"); 2640b57cec5SDimitry Andric case ELF32LEKind: 2650b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32LE>>(*this), sym, sec, offset); 2660b57cec5SDimitry Andric case ELF32BEKind: 2670b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF32BE>>(*this), sym, sec, offset); 2680b57cec5SDimitry Andric case ELF64LEKind: 2690b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64LE>>(*this), sym, sec, offset); 2700b57cec5SDimitry Andric case ELF64BEKind: 2710b57cec5SDimitry Andric return getSrcMsgAux(cast<ObjFile<ELF64BE>>(*this), sym, sec, offset); 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 275*5ffd83dbSDimitry Andric template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() { 276*5ffd83dbSDimitry Andric llvm::call_once(initDwarf, [this]() { 277*5ffd83dbSDimitry Andric dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>( 278*5ffd83dbSDimitry Andric std::make_unique<LLDDwarfObj<ELFT>>(this), "", 279*5ffd83dbSDimitry Andric [&](Error err) { warn(getName() + ": " + toString(std::move(err))); }, 280*5ffd83dbSDimitry Andric [&](Error warning) { 281*5ffd83dbSDimitry Andric warn(getName() + ": " + toString(std::move(warning))); 282*5ffd83dbSDimitry Andric })); 283*5ffd83dbSDimitry Andric }); 284*5ffd83dbSDimitry Andric 285*5ffd83dbSDimitry Andric return dwarf.get(); 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // Returns the pair of file name and line number describing location of data 2890b57cec5SDimitry Andric // object (variable, array, etc) definition. 2900b57cec5SDimitry Andric template <class ELFT> 2910b57cec5SDimitry Andric Optional<std::pair<std::string, unsigned>> 2920b57cec5SDimitry Andric ObjFile<ELFT>::getVariableLoc(StringRef name) { 293*5ffd83dbSDimitry Andric return getDwarf()->getVariableLoc(name); 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // Returns source line information for a given offset 2970b57cec5SDimitry Andric // using DWARF debug info. 2980b57cec5SDimitry Andric template <class ELFT> 2990b57cec5SDimitry Andric Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s, 3000b57cec5SDimitry Andric uint64_t offset) { 3010b57cec5SDimitry Andric // Detect SectionIndex for specified section. 3020b57cec5SDimitry Andric uint64_t sectionIndex = object::SectionedAddress::UndefSection; 3030b57cec5SDimitry Andric ArrayRef<InputSectionBase *> sections = s->file->getSections(); 3040b57cec5SDimitry Andric for (uint64_t curIndex = 0; curIndex < sections.size(); ++curIndex) { 3050b57cec5SDimitry Andric if (s == sections[curIndex]) { 3060b57cec5SDimitry Andric sectionIndex = curIndex; 3070b57cec5SDimitry Andric break; 3080b57cec5SDimitry Andric } 3090b57cec5SDimitry Andric } 3100b57cec5SDimitry Andric 311*5ffd83dbSDimitry Andric return getDwarf()->getDILineInfo(offset, sectionIndex); 3120b57cec5SDimitry Andric } 3130b57cec5SDimitry Andric 3140b57cec5SDimitry Andric ELFFileBase::ELFFileBase(Kind k, MemoryBufferRef mb) : InputFile(k, mb) { 3150b57cec5SDimitry Andric ekind = getELFKind(mb, ""); 3160b57cec5SDimitry Andric 3170b57cec5SDimitry Andric switch (ekind) { 3180b57cec5SDimitry Andric case ELF32LEKind: 3190b57cec5SDimitry Andric init<ELF32LE>(); 3200b57cec5SDimitry Andric break; 3210b57cec5SDimitry Andric case ELF32BEKind: 3220b57cec5SDimitry Andric init<ELF32BE>(); 3230b57cec5SDimitry Andric break; 3240b57cec5SDimitry Andric case ELF64LEKind: 3250b57cec5SDimitry Andric init<ELF64LE>(); 3260b57cec5SDimitry Andric break; 3270b57cec5SDimitry Andric case ELF64BEKind: 3280b57cec5SDimitry Andric init<ELF64BE>(); 3290b57cec5SDimitry Andric break; 3300b57cec5SDimitry Andric default: 3310b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 3320b57cec5SDimitry Andric } 3330b57cec5SDimitry Andric } 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric template <typename Elf_Shdr> 3360b57cec5SDimitry Andric static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) { 3370b57cec5SDimitry Andric for (const Elf_Shdr &sec : sections) 3380b57cec5SDimitry Andric if (sec.sh_type == type) 3390b57cec5SDimitry Andric return &sec; 3400b57cec5SDimitry Andric return nullptr; 3410b57cec5SDimitry Andric } 3420b57cec5SDimitry Andric 3430b57cec5SDimitry Andric template <class ELFT> void ELFFileBase::init() { 3440b57cec5SDimitry Andric using Elf_Shdr = typename ELFT::Shdr; 3450b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric // Initialize trivial attributes. 3480b57cec5SDimitry Andric const ELFFile<ELFT> &obj = getObj<ELFT>(); 3490b57cec5SDimitry Andric emachine = obj.getHeader()->e_machine; 3500b57cec5SDimitry Andric osabi = obj.getHeader()->e_ident[llvm::ELF::EI_OSABI]; 3510b57cec5SDimitry Andric abiVersion = obj.getHeader()->e_ident[llvm::ELF::EI_ABIVERSION]; 3520b57cec5SDimitry Andric 3530b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this); 3540b57cec5SDimitry Andric 3550b57cec5SDimitry Andric // Find a symbol table. 3560b57cec5SDimitry Andric bool isDSO = 3570b57cec5SDimitry Andric (identify_magic(mb.getBuffer()) == file_magic::elf_shared_object); 3580b57cec5SDimitry Andric const Elf_Shdr *symtabSec = 3590b57cec5SDimitry Andric findSection(sections, isDSO ? SHT_DYNSYM : SHT_SYMTAB); 3600b57cec5SDimitry Andric 3610b57cec5SDimitry Andric if (!symtabSec) 3620b57cec5SDimitry Andric return; 3630b57cec5SDimitry Andric 3640b57cec5SDimitry Andric // Initialize members corresponding to a symbol table. 3650b57cec5SDimitry Andric firstGlobal = symtabSec->sh_info; 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(symtabSec), this); 3680b57cec5SDimitry Andric if (firstGlobal == 0 || firstGlobal > eSyms.size()) 3690b57cec5SDimitry Andric fatal(toString(this) + ": invalid sh_info in symbol table"); 3700b57cec5SDimitry Andric 3710b57cec5SDimitry Andric elfSyms = reinterpret_cast<const void *>(eSyms.data()); 3720b57cec5SDimitry Andric numELFSyms = eSyms.size(); 3730b57cec5SDimitry Andric stringTable = CHECK(obj.getStringTableForSymtab(*symtabSec, sections), this); 3740b57cec5SDimitry Andric } 3750b57cec5SDimitry Andric 3760b57cec5SDimitry Andric template <class ELFT> 3770b57cec5SDimitry Andric uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const { 3780b57cec5SDimitry Andric return CHECK( 3790b57cec5SDimitry Andric this->getObj().getSectionIndex(&sym, getELFSyms<ELFT>(), shndxTable), 3800b57cec5SDimitry Andric this); 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getLocalSymbols() { 3840b57cec5SDimitry Andric if (this->symbols.empty()) 3850b57cec5SDimitry Andric return {}; 3860b57cec5SDimitry Andric return makeArrayRef(this->symbols).slice(1, this->firstGlobal - 1); 3870b57cec5SDimitry Andric } 3880b57cec5SDimitry Andric 3890b57cec5SDimitry Andric template <class ELFT> ArrayRef<Symbol *> ObjFile<ELFT>::getGlobalSymbols() { 3900b57cec5SDimitry Andric return makeArrayRef(this->symbols).slice(this->firstGlobal); 3910b57cec5SDimitry Andric } 3920b57cec5SDimitry Andric 3930b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) { 3940b57cec5SDimitry Andric // Read a section table. justSymbols is usually false. 3950b57cec5SDimitry Andric if (this->justSymbols) 3960b57cec5SDimitry Andric initializeJustSymbols(); 3970b57cec5SDimitry Andric else 3980b57cec5SDimitry Andric initializeSections(ignoreComdats); 3990b57cec5SDimitry Andric 4000b57cec5SDimitry Andric // Read a symbol table. 4010b57cec5SDimitry Andric initializeSymbols(); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric // Sections with SHT_GROUP and comdat bits define comdat section groups. 4050b57cec5SDimitry Andric // They are identified and deduplicated by group name. This function 4060b57cec5SDimitry Andric // returns a group name. 4070b57cec5SDimitry Andric template <class ELFT> 4080b57cec5SDimitry Andric StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections, 4090b57cec5SDimitry Andric const Elf_Shdr &sec) { 4100b57cec5SDimitry Andric typename ELFT::SymRange symbols = this->getELFSyms<ELFT>(); 4110b57cec5SDimitry Andric if (sec.sh_info >= symbols.size()) 4120b57cec5SDimitry Andric fatal(toString(this) + ": invalid symbol index"); 4130b57cec5SDimitry Andric const typename ELFT::Sym &sym = symbols[sec.sh_info]; 4140b57cec5SDimitry Andric StringRef signature = CHECK(sym.getName(this->stringTable), this); 4150b57cec5SDimitry Andric 4160b57cec5SDimitry Andric // As a special case, if a symbol is a section symbol and has no name, 4170b57cec5SDimitry Andric // we use a section name as a signature. 4180b57cec5SDimitry Andric // 4190b57cec5SDimitry Andric // Such SHT_GROUP sections are invalid from the perspective of the ELF 4200b57cec5SDimitry Andric // standard, but GNU gold 1.14 (the newest version as of July 2017) or 4210b57cec5SDimitry Andric // older produce such sections as outputs for the -r option, so we need 4220b57cec5SDimitry Andric // a bug-compatibility. 4230b57cec5SDimitry Andric if (signature.empty() && sym.getType() == STT_SECTION) 4240b57cec5SDimitry Andric return getSectionName(sec); 4250b57cec5SDimitry Andric return signature; 4260b57cec5SDimitry Andric } 4270b57cec5SDimitry Andric 42885868e8aSDimitry Andric template <class ELFT> 42985868e8aSDimitry Andric bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) { 430*5ffd83dbSDimitry Andric if (!(sec.sh_flags & SHF_MERGE)) 431*5ffd83dbSDimitry Andric return false; 432*5ffd83dbSDimitry Andric 4330b57cec5SDimitry Andric // On a regular link we don't merge sections if -O0 (default is -O1). This 4340b57cec5SDimitry Andric // sometimes makes the linker significantly faster, although the output will 4350b57cec5SDimitry Andric // be bigger. 4360b57cec5SDimitry Andric // 4370b57cec5SDimitry Andric // Doing the same for -r would create a problem as it would combine sections 4380b57cec5SDimitry Andric // with different sh_entsize. One option would be to just copy every SHF_MERGE 4390b57cec5SDimitry Andric // section as is to the output. While this would produce a valid ELF file with 4400b57cec5SDimitry Andric // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when 4410b57cec5SDimitry Andric // they see two .debug_str. We could have separate logic for combining 4420b57cec5SDimitry Andric // SHF_MERGE sections based both on their name and sh_entsize, but that seems 4430b57cec5SDimitry Andric // to be more trouble than it is worth. Instead, we just use the regular (-O1) 4440b57cec5SDimitry Andric // logic for -r. 4450b57cec5SDimitry Andric if (config->optimize == 0 && !config->relocatable) 4460b57cec5SDimitry Andric return false; 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric // A mergeable section with size 0 is useless because they don't have 4490b57cec5SDimitry Andric // any data to merge. A mergeable string section with size 0 can be 4500b57cec5SDimitry Andric // argued as invalid because it doesn't end with a null character. 4510b57cec5SDimitry Andric // We'll avoid a mess by handling them as if they were non-mergeable. 4520b57cec5SDimitry Andric if (sec.sh_size == 0) 4530b57cec5SDimitry Andric return false; 4540b57cec5SDimitry Andric 4550b57cec5SDimitry Andric // Check for sh_entsize. The ELF spec is not clear about the zero 4560b57cec5SDimitry Andric // sh_entsize. It says that "the member [sh_entsize] contains 0 if 4570b57cec5SDimitry Andric // the section does not hold a table of fixed-size entries". We know 4580b57cec5SDimitry Andric // that Rust 1.13 produces a string mergeable section with a zero 4590b57cec5SDimitry Andric // sh_entsize. Here we just accept it rather than being picky about it. 4600b57cec5SDimitry Andric uint64_t entSize = sec.sh_entsize; 4610b57cec5SDimitry Andric if (entSize == 0) 4620b57cec5SDimitry Andric return false; 4630b57cec5SDimitry Andric if (sec.sh_size % entSize) 46485868e8aSDimitry Andric fatal(toString(this) + ":(" + name + "): SHF_MERGE section size (" + 46585868e8aSDimitry Andric Twine(sec.sh_size) + ") must be a multiple of sh_entsize (" + 46685868e8aSDimitry Andric Twine(entSize) + ")"); 4670b57cec5SDimitry Andric 468*5ffd83dbSDimitry Andric if (sec.sh_flags & SHF_WRITE) 46985868e8aSDimitry Andric fatal(toString(this) + ":(" + name + 47085868e8aSDimitry Andric "): writable SHF_MERGE section is not supported"); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric return true; 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric 4750b57cec5SDimitry Andric // This is for --just-symbols. 4760b57cec5SDimitry Andric // 4770b57cec5SDimitry Andric // --just-symbols is a very minor feature that allows you to link your 4780b57cec5SDimitry Andric // output against other existing program, so that if you load both your 4790b57cec5SDimitry Andric // program and the other program into memory, your output can refer the 4800b57cec5SDimitry Andric // other program's symbols. 4810b57cec5SDimitry Andric // 4820b57cec5SDimitry Andric // When the option is given, we link "just symbols". The section table is 4830b57cec5SDimitry Andric // initialized with null pointers. 4840b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() { 4850b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(this->getObj().sections(), this); 4860b57cec5SDimitry Andric this->sections.resize(sections.size()); 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // An ELF object file may contain a `.deplibs` section. If it exists, the 4900b57cec5SDimitry Andric // section contains a list of library specifiers such as `m` for libm. This 4910b57cec5SDimitry Andric // function resolves a given name by finding the first matching library checking 4920b57cec5SDimitry Andric // the various ways that a library can be specified to LLD. This ELF extension 4930b57cec5SDimitry Andric // is a form of autolinking and is called `dependent libraries`. It is currently 4940b57cec5SDimitry Andric // unique to LLVM and lld. 4950b57cec5SDimitry Andric static void addDependentLibrary(StringRef specifier, const InputFile *f) { 4960b57cec5SDimitry Andric if (!config->dependentLibraries) 4970b57cec5SDimitry Andric return; 4980b57cec5SDimitry Andric if (fs::exists(specifier)) 4990b57cec5SDimitry Andric driver->addFile(specifier, /*withLOption=*/false); 5000b57cec5SDimitry Andric else if (Optional<std::string> s = findFromSearchPaths(specifier)) 5010b57cec5SDimitry Andric driver->addFile(*s, /*withLOption=*/true); 5020b57cec5SDimitry Andric else if (Optional<std::string> s = searchLibraryBaseName(specifier)) 5030b57cec5SDimitry Andric driver->addFile(*s, /*withLOption=*/true); 5040b57cec5SDimitry Andric else 5050b57cec5SDimitry Andric error(toString(f) + 5060b57cec5SDimitry Andric ": unable to find library from dependent library specifier: " + 5070b57cec5SDimitry Andric specifier); 5080b57cec5SDimitry Andric } 5090b57cec5SDimitry Andric 510480093f4SDimitry Andric // Record the membership of a section group so that in the garbage collection 511480093f4SDimitry Andric // pass, section group members are kept or discarded as a unit. 512480093f4SDimitry Andric template <class ELFT> 513480093f4SDimitry Andric static void handleSectionGroup(ArrayRef<InputSectionBase *> sections, 514480093f4SDimitry Andric ArrayRef<typename ELFT::Word> entries) { 515480093f4SDimitry Andric bool hasAlloc = false; 516480093f4SDimitry Andric for (uint32_t index : entries.slice(1)) { 517480093f4SDimitry Andric if (index >= sections.size()) 518480093f4SDimitry Andric return; 519480093f4SDimitry Andric if (InputSectionBase *s = sections[index]) 520480093f4SDimitry Andric if (s != &InputSection::discarded && s->flags & SHF_ALLOC) 521480093f4SDimitry Andric hasAlloc = true; 522480093f4SDimitry Andric } 523480093f4SDimitry Andric 524480093f4SDimitry Andric // If any member has the SHF_ALLOC flag, the whole group is subject to garbage 525480093f4SDimitry Andric // collection. See the comment in markLive(). This rule retains .debug_types 526480093f4SDimitry Andric // and .rela.debug_types. 527480093f4SDimitry Andric if (!hasAlloc) 528480093f4SDimitry Andric return; 529480093f4SDimitry Andric 530480093f4SDimitry Andric // Connect the members in a circular doubly-linked list via 531480093f4SDimitry Andric // nextInSectionGroup. 532480093f4SDimitry Andric InputSectionBase *head; 533480093f4SDimitry Andric InputSectionBase *prev = nullptr; 534480093f4SDimitry Andric for (uint32_t index : entries.slice(1)) { 535480093f4SDimitry Andric InputSectionBase *s = sections[index]; 536480093f4SDimitry Andric if (!s || s == &InputSection::discarded) 537480093f4SDimitry Andric continue; 538480093f4SDimitry Andric if (prev) 539480093f4SDimitry Andric prev->nextInSectionGroup = s; 540480093f4SDimitry Andric else 541480093f4SDimitry Andric head = s; 542480093f4SDimitry Andric prev = s; 543480093f4SDimitry Andric } 544480093f4SDimitry Andric if (prev) 545480093f4SDimitry Andric prev->nextInSectionGroup = head; 546480093f4SDimitry Andric } 547480093f4SDimitry Andric 5480b57cec5SDimitry Andric template <class ELFT> 5490b57cec5SDimitry Andric void ObjFile<ELFT>::initializeSections(bool ignoreComdats) { 5500b57cec5SDimitry Andric const ELFFile<ELFT> &obj = this->getObj(); 5510b57cec5SDimitry Andric 5520b57cec5SDimitry Andric ArrayRef<Elf_Shdr> objSections = CHECK(obj.sections(), this); 5530b57cec5SDimitry Andric uint64_t size = objSections.size(); 5540b57cec5SDimitry Andric this->sections.resize(size); 5550b57cec5SDimitry Andric this->sectionStringTable = 5560b57cec5SDimitry Andric CHECK(obj.getSectionStringTable(objSections), this); 5570b57cec5SDimitry Andric 558480093f4SDimitry Andric std::vector<ArrayRef<Elf_Word>> selectedGroups; 559480093f4SDimitry Andric 56085868e8aSDimitry Andric for (size_t i = 0, e = objSections.size(); i < e; ++i) { 5610b57cec5SDimitry Andric if (this->sections[i] == &InputSection::discarded) 5620b57cec5SDimitry Andric continue; 5630b57cec5SDimitry Andric const Elf_Shdr &sec = objSections[i]; 5640b57cec5SDimitry Andric 5650b57cec5SDimitry Andric if (sec.sh_type == ELF::SHT_LLVM_CALL_GRAPH_PROFILE) 5660b57cec5SDimitry Andric cgProfile = 5670b57cec5SDimitry Andric check(obj.template getSectionContentsAsArray<Elf_CGProfile>(&sec)); 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric // SHF_EXCLUDE'ed sections are discarded by the linker. However, 5700b57cec5SDimitry Andric // if -r is given, we'll let the final link discard such sections. 5710b57cec5SDimitry Andric // This is compatible with GNU. 5720b57cec5SDimitry Andric if ((sec.sh_flags & SHF_EXCLUDE) && !config->relocatable) { 5730b57cec5SDimitry Andric if (sec.sh_type == SHT_LLVM_ADDRSIG) { 5740b57cec5SDimitry Andric // We ignore the address-significance table if we know that the object 5750b57cec5SDimitry Andric // file was created by objcopy or ld -r. This is because these tools 5760b57cec5SDimitry Andric // will reorder the symbols in the symbol table, invalidating the data 5770b57cec5SDimitry Andric // in the address-significance table, which refers to symbols by index. 5780b57cec5SDimitry Andric if (sec.sh_link != 0) 5790b57cec5SDimitry Andric this->addrsigSec = &sec; 5800b57cec5SDimitry Andric else if (config->icf == ICFLevel::Safe) 5810b57cec5SDimitry Andric warn(toString(this) + ": --icf=safe is incompatible with object " 5820b57cec5SDimitry Andric "files created using objcopy or ld -r"); 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 5850b57cec5SDimitry Andric continue; 5860b57cec5SDimitry Andric } 5870b57cec5SDimitry Andric 5880b57cec5SDimitry Andric switch (sec.sh_type) { 5890b57cec5SDimitry Andric case SHT_GROUP: { 5900b57cec5SDimitry Andric // De-duplicate section groups by their signatures. 5910b57cec5SDimitry Andric StringRef signature = getShtGroupSignature(objSections, sec); 5920b57cec5SDimitry Andric this->sections[i] = &InputSection::discarded; 5930b57cec5SDimitry Andric 5940b57cec5SDimitry Andric 5950b57cec5SDimitry Andric ArrayRef<Elf_Word> entries = 5960b57cec5SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Word>(&sec), this); 5970b57cec5SDimitry Andric if (entries.empty()) 5980b57cec5SDimitry Andric fatal(toString(this) + ": empty SHT_GROUP"); 5990b57cec5SDimitry Andric 6000b57cec5SDimitry Andric // The first word of a SHT_GROUP section contains flags. Currently, 6010b57cec5SDimitry Andric // the standard defines only "GRP_COMDAT" flag for the COMDAT group. 6020b57cec5SDimitry Andric // An group with the empty flag doesn't define anything; such sections 6030b57cec5SDimitry Andric // are just skipped. 6040b57cec5SDimitry Andric if (entries[0] == 0) 6050b57cec5SDimitry Andric continue; 6060b57cec5SDimitry Andric 6070b57cec5SDimitry Andric if (entries[0] != GRP_COMDAT) 6080b57cec5SDimitry Andric fatal(toString(this) + ": unsupported SHT_GROUP format"); 6090b57cec5SDimitry Andric 6100b57cec5SDimitry Andric bool isNew = 6110b57cec5SDimitry Andric ignoreComdats || 6120b57cec5SDimitry Andric symtab->comdatGroups.try_emplace(CachedHashStringRef(signature), this) 6130b57cec5SDimitry Andric .second; 6140b57cec5SDimitry Andric if (isNew) { 6150b57cec5SDimitry Andric if (config->relocatable) 6160b57cec5SDimitry Andric this->sections[i] = createInputSection(sec); 617480093f4SDimitry Andric selectedGroups.push_back(entries); 6180b57cec5SDimitry Andric continue; 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric // Otherwise, discard group members. 6220b57cec5SDimitry Andric for (uint32_t secIndex : entries.slice(1)) { 6230b57cec5SDimitry Andric if (secIndex >= size) 6240b57cec5SDimitry Andric fatal(toString(this) + 6250b57cec5SDimitry Andric ": invalid section index in group: " + Twine(secIndex)); 6260b57cec5SDimitry Andric this->sections[secIndex] = &InputSection::discarded; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric break; 6290b57cec5SDimitry Andric } 6300b57cec5SDimitry Andric case SHT_SYMTAB_SHNDX: 6310b57cec5SDimitry Andric shndxTable = CHECK(obj.getSHNDXTable(sec, objSections), this); 6320b57cec5SDimitry Andric break; 6330b57cec5SDimitry Andric case SHT_SYMTAB: 6340b57cec5SDimitry Andric case SHT_STRTAB: 635*5ffd83dbSDimitry Andric case SHT_REL: 636*5ffd83dbSDimitry Andric case SHT_RELA: 6370b57cec5SDimitry Andric case SHT_NULL: 6380b57cec5SDimitry Andric break; 6390b57cec5SDimitry Andric default: 6400b57cec5SDimitry Andric this->sections[i] = createInputSection(sec); 6410b57cec5SDimitry Andric } 64285868e8aSDimitry Andric } 64385868e8aSDimitry Andric 644*5ffd83dbSDimitry Andric // We have a second loop. It is used to: 645*5ffd83dbSDimitry Andric // 1) handle SHF_LINK_ORDER sections. 646*5ffd83dbSDimitry Andric // 2) create SHT_REL[A] sections. In some cases the section header index of a 647*5ffd83dbSDimitry Andric // relocation section may be smaller than that of the relocated section. In 648*5ffd83dbSDimitry Andric // such cases, the relocation section would attempt to reference a target 649*5ffd83dbSDimitry Andric // section that has not yet been created. For simplicity, delay creation of 650*5ffd83dbSDimitry Andric // relocation sections until now. 65185868e8aSDimitry Andric for (size_t i = 0, e = objSections.size(); i < e; ++i) { 65285868e8aSDimitry Andric if (this->sections[i] == &InputSection::discarded) 65385868e8aSDimitry Andric continue; 65485868e8aSDimitry Andric const Elf_Shdr &sec = objSections[i]; 655*5ffd83dbSDimitry Andric 656*5ffd83dbSDimitry Andric if (sec.sh_type == SHT_REL || sec.sh_type == SHT_RELA) 657*5ffd83dbSDimitry Andric this->sections[i] = createInputSection(sec); 658*5ffd83dbSDimitry Andric 65985868e8aSDimitry Andric if (!(sec.sh_flags & SHF_LINK_ORDER)) 66085868e8aSDimitry Andric continue; 6610b57cec5SDimitry Andric 6620b57cec5SDimitry Andric // .ARM.exidx sections have a reverse dependency on the InputSection they 6630b57cec5SDimitry Andric // have a SHF_LINK_ORDER dependency, this is identified by the sh_link. 6640b57cec5SDimitry Andric InputSectionBase *linkSec = nullptr; 6650b57cec5SDimitry Andric if (sec.sh_link < this->sections.size()) 6660b57cec5SDimitry Andric linkSec = this->sections[sec.sh_link]; 6670b57cec5SDimitry Andric if (!linkSec) 66885868e8aSDimitry Andric fatal(toString(this) + ": invalid sh_link index: " + Twine(sec.sh_link)); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric InputSection *isec = cast<InputSection>(this->sections[i]); 6710b57cec5SDimitry Andric linkSec->dependentSections.push_back(isec); 6720b57cec5SDimitry Andric if (!isa<InputSection>(linkSec)) 6730b57cec5SDimitry Andric error("a section " + isec->name + 67485868e8aSDimitry Andric " with SHF_LINK_ORDER should not refer a non-regular section: " + 6750b57cec5SDimitry Andric toString(linkSec)); 6760b57cec5SDimitry Andric } 677480093f4SDimitry Andric 678480093f4SDimitry Andric for (ArrayRef<Elf_Word> entries : selectedGroups) 679480093f4SDimitry Andric handleSectionGroup<ELFT>(this->sections, entries); 6800b57cec5SDimitry Andric } 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric // For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD 6830b57cec5SDimitry Andric // flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how 6840b57cec5SDimitry Andric // the input objects have been compiled. 6850b57cec5SDimitry Andric static void updateARMVFPArgs(const ARMAttributeParser &attributes, 6860b57cec5SDimitry Andric const InputFile *f) { 687*5ffd83dbSDimitry Andric Optional<unsigned> attr = 688*5ffd83dbSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args); 689*5ffd83dbSDimitry Andric if (!attr.hasValue()) 6900b57cec5SDimitry Andric // If an ABI tag isn't present then it is implicitly given the value of 0 6910b57cec5SDimitry Andric // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files, 6920b57cec5SDimitry Andric // including some in glibc that don't use FP args (and should have value 3) 6930b57cec5SDimitry Andric // don't have the attribute so we do not consider an implicit value of 0 6940b57cec5SDimitry Andric // as a clash. 6950b57cec5SDimitry Andric return; 6960b57cec5SDimitry Andric 697*5ffd83dbSDimitry Andric unsigned vfpArgs = attr.getValue(); 6980b57cec5SDimitry Andric ARMVFPArgKind arg; 6990b57cec5SDimitry Andric switch (vfpArgs) { 7000b57cec5SDimitry Andric case ARMBuildAttrs::BaseAAPCS: 7010b57cec5SDimitry Andric arg = ARMVFPArgKind::Base; 7020b57cec5SDimitry Andric break; 7030b57cec5SDimitry Andric case ARMBuildAttrs::HardFPAAPCS: 7040b57cec5SDimitry Andric arg = ARMVFPArgKind::VFP; 7050b57cec5SDimitry Andric break; 7060b57cec5SDimitry Andric case ARMBuildAttrs::ToolChainFPPCS: 7070b57cec5SDimitry Andric // Tool chain specific convention that conforms to neither AAPCS variant. 7080b57cec5SDimitry Andric arg = ARMVFPArgKind::ToolChain; 7090b57cec5SDimitry Andric break; 7100b57cec5SDimitry Andric case ARMBuildAttrs::CompatibleFPAAPCS: 7110b57cec5SDimitry Andric // Object compatible with all conventions. 7120b57cec5SDimitry Andric return; 7130b57cec5SDimitry Andric default: 7140b57cec5SDimitry Andric error(toString(f) + ": unknown Tag_ABI_VFP_args value: " + Twine(vfpArgs)); 7150b57cec5SDimitry Andric return; 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric // Follow ld.bfd and error if there is a mix of calling conventions. 7180b57cec5SDimitry Andric if (config->armVFPArgs != arg && config->armVFPArgs != ARMVFPArgKind::Default) 7190b57cec5SDimitry Andric error(toString(f) + ": incompatible Tag_ABI_VFP_args"); 7200b57cec5SDimitry Andric else 7210b57cec5SDimitry Andric config->armVFPArgs = arg; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric // The ARM support in lld makes some use of instructions that are not available 7250b57cec5SDimitry Andric // on all ARM architectures. Namely: 7260b57cec5SDimitry Andric // - Use of BLX instruction for interworking between ARM and Thumb state. 7270b57cec5SDimitry Andric // - Use of the extended Thumb branch encoding in relocation. 7280b57cec5SDimitry Andric // - Use of the MOVT/MOVW instructions in Thumb Thunks. 7290b57cec5SDimitry Andric // The ARM Attributes section contains information about the architecture chosen 7300b57cec5SDimitry Andric // at compile time. We follow the convention that if at least one input object 7310b57cec5SDimitry Andric // is compiled with an architecture that supports these features then lld is 7320b57cec5SDimitry Andric // permitted to use them. 7330b57cec5SDimitry Andric static void updateSupportedARMFeatures(const ARMAttributeParser &attributes) { 734*5ffd83dbSDimitry Andric Optional<unsigned> attr = 735*5ffd83dbSDimitry Andric attributes.getAttributeValue(ARMBuildAttrs::CPU_arch); 736*5ffd83dbSDimitry Andric if (!attr.hasValue()) 7370b57cec5SDimitry Andric return; 738*5ffd83dbSDimitry Andric auto arch = attr.getValue(); 7390b57cec5SDimitry Andric switch (arch) { 7400b57cec5SDimitry Andric case ARMBuildAttrs::Pre_v4: 7410b57cec5SDimitry Andric case ARMBuildAttrs::v4: 7420b57cec5SDimitry Andric case ARMBuildAttrs::v4T: 7430b57cec5SDimitry Andric // Architectures prior to v5 do not support BLX instruction 7440b57cec5SDimitry Andric break; 7450b57cec5SDimitry Andric case ARMBuildAttrs::v5T: 7460b57cec5SDimitry Andric case ARMBuildAttrs::v5TE: 7470b57cec5SDimitry Andric case ARMBuildAttrs::v5TEJ: 7480b57cec5SDimitry Andric case ARMBuildAttrs::v6: 7490b57cec5SDimitry Andric case ARMBuildAttrs::v6KZ: 7500b57cec5SDimitry Andric case ARMBuildAttrs::v6K: 7510b57cec5SDimitry Andric config->armHasBlx = true; 7520b57cec5SDimitry Andric // Architectures used in pre-Cortex processors do not support 7530b57cec5SDimitry Andric // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception 7540b57cec5SDimitry Andric // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do. 7550b57cec5SDimitry Andric break; 7560b57cec5SDimitry Andric default: 7570b57cec5SDimitry Andric // All other Architectures have BLX and extended branch encoding 7580b57cec5SDimitry Andric config->armHasBlx = true; 7590b57cec5SDimitry Andric config->armJ1J2BranchEncoding = true; 7600b57cec5SDimitry Andric if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M) 7610b57cec5SDimitry Andric // All Architectures used in Cortex processors with the exception 7620b57cec5SDimitry Andric // of v6-M and v6S-M have the MOVT and MOVW instructions. 7630b57cec5SDimitry Andric config->armHasMovtMovw = true; 7640b57cec5SDimitry Andric break; 7650b57cec5SDimitry Andric } 7660b57cec5SDimitry Andric } 7670b57cec5SDimitry Andric 7680b57cec5SDimitry Andric // If a source file is compiled with x86 hardware-assisted call flow control 7690b57cec5SDimitry Andric // enabled, the generated object file contains feature flags indicating that 7700b57cec5SDimitry Andric // fact. This function reads the feature flags and returns it. 7710b57cec5SDimitry Andric // 7720b57cec5SDimitry Andric // Essentially we want to read a single 32-bit value in this function, but this 7730b57cec5SDimitry Andric // function is rather complicated because the value is buried deep inside a 7740b57cec5SDimitry Andric // .note.gnu.property section. 7750b57cec5SDimitry Andric // 7760b57cec5SDimitry Andric // The section consists of one or more NOTE records. Each NOTE record consists 7770b57cec5SDimitry Andric // of zero or more type-length-value fields. We want to find a field of a 7780b57cec5SDimitry Andric // certain type. It seems a bit too much to just store a 32-bit value, perhaps 7790b57cec5SDimitry Andric // the ABI is unnecessarily complicated. 7800b57cec5SDimitry Andric template <class ELFT> 7810b57cec5SDimitry Andric static uint32_t readAndFeatures(ObjFile<ELFT> *obj, ArrayRef<uint8_t> data) { 7820b57cec5SDimitry Andric using Elf_Nhdr = typename ELFT::Nhdr; 7830b57cec5SDimitry Andric using Elf_Note = typename ELFT::Note; 7840b57cec5SDimitry Andric 7850b57cec5SDimitry Andric uint32_t featuresSet = 0; 7860b57cec5SDimitry Andric while (!data.empty()) { 7870b57cec5SDimitry Andric // Read one NOTE record. 7880b57cec5SDimitry Andric if (data.size() < sizeof(Elf_Nhdr)) 7890b57cec5SDimitry Andric fatal(toString(obj) + ": .note.gnu.property: section too short"); 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data()); 7920b57cec5SDimitry Andric if (data.size() < nhdr->getSize()) 7930b57cec5SDimitry Andric fatal(toString(obj) + ": .note.gnu.property: section too short"); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric Elf_Note note(*nhdr); 7960b57cec5SDimitry Andric if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") { 7970b57cec5SDimitry Andric data = data.slice(nhdr->getSize()); 7980b57cec5SDimitry Andric continue; 7990b57cec5SDimitry Andric } 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric uint32_t featureAndType = config->emachine == EM_AARCH64 8020b57cec5SDimitry Andric ? GNU_PROPERTY_AARCH64_FEATURE_1_AND 8030b57cec5SDimitry Andric : GNU_PROPERTY_X86_FEATURE_1_AND; 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric // Read a body of a NOTE record, which consists of type-length-value fields. 8060b57cec5SDimitry Andric ArrayRef<uint8_t> desc = note.getDesc(); 8070b57cec5SDimitry Andric while (!desc.empty()) { 8080b57cec5SDimitry Andric if (desc.size() < 8) 8090b57cec5SDimitry Andric fatal(toString(obj) + ": .note.gnu.property: section too short"); 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric uint32_t type = read32le(desc.data()); 8120b57cec5SDimitry Andric uint32_t size = read32le(desc.data() + 4); 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric if (type == featureAndType) { 8150b57cec5SDimitry Andric // We found a FEATURE_1_AND field. There may be more than one of these 816480093f4SDimitry Andric // in a .note.gnu.property section, for a relocatable object we 8170b57cec5SDimitry Andric // accumulate the bits set. 8180b57cec5SDimitry Andric featuresSet |= read32le(desc.data() + 8); 8190b57cec5SDimitry Andric } 8200b57cec5SDimitry Andric 8210b57cec5SDimitry Andric // On 64-bit, a payload may be followed by a 4-byte padding to make its 8220b57cec5SDimitry Andric // size a multiple of 8. 8230b57cec5SDimitry Andric if (ELFT::Is64Bits) 8240b57cec5SDimitry Andric size = alignTo(size, 8); 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric desc = desc.slice(size + 8); // +8 for Type and Size 8270b57cec5SDimitry Andric } 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric // Go to next NOTE record to look for more FEATURE_1_AND descriptions. 8300b57cec5SDimitry Andric data = data.slice(nhdr->getSize()); 8310b57cec5SDimitry Andric } 8320b57cec5SDimitry Andric 8330b57cec5SDimitry Andric return featuresSet; 8340b57cec5SDimitry Andric } 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric template <class ELFT> 8370b57cec5SDimitry Andric InputSectionBase *ObjFile<ELFT>::getRelocTarget(const Elf_Shdr &sec) { 8380b57cec5SDimitry Andric uint32_t idx = sec.sh_info; 8390b57cec5SDimitry Andric if (idx >= this->sections.size()) 8400b57cec5SDimitry Andric fatal(toString(this) + ": invalid relocated section index: " + Twine(idx)); 8410b57cec5SDimitry Andric InputSectionBase *target = this->sections[idx]; 8420b57cec5SDimitry Andric 8430b57cec5SDimitry Andric // Strictly speaking, a relocation section must be included in the 8440b57cec5SDimitry Andric // group of the section it relocates. However, LLVM 3.3 and earlier 8450b57cec5SDimitry Andric // would fail to do so, so we gracefully handle that case. 8460b57cec5SDimitry Andric if (target == &InputSection::discarded) 8470b57cec5SDimitry Andric return nullptr; 8480b57cec5SDimitry Andric 8490b57cec5SDimitry Andric if (!target) 8500b57cec5SDimitry Andric fatal(toString(this) + ": unsupported relocation reference"); 8510b57cec5SDimitry Andric return target; 8520b57cec5SDimitry Andric } 8530b57cec5SDimitry Andric 8540b57cec5SDimitry Andric // Create a regular InputSection class that has the same contents 8550b57cec5SDimitry Andric // as a given section. 8560b57cec5SDimitry Andric static InputSection *toRegularSection(MergeInputSection *sec) { 8570b57cec5SDimitry Andric return make<InputSection>(sec->file, sec->flags, sec->type, sec->alignment, 8580b57cec5SDimitry Andric sec->data(), sec->name); 8590b57cec5SDimitry Andric } 8600b57cec5SDimitry Andric 8610b57cec5SDimitry Andric template <class ELFT> 8620b57cec5SDimitry Andric InputSectionBase *ObjFile<ELFT>::createInputSection(const Elf_Shdr &sec) { 8630b57cec5SDimitry Andric StringRef name = getSectionName(sec); 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric switch (sec.sh_type) { 8660b57cec5SDimitry Andric case SHT_ARM_ATTRIBUTES: { 8670b57cec5SDimitry Andric if (config->emachine != EM_ARM) 8680b57cec5SDimitry Andric break; 8690b57cec5SDimitry Andric ARMAttributeParser attributes; 8700b57cec5SDimitry Andric ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(&sec)); 871*5ffd83dbSDimitry Andric if (Error e = attributes.parse(contents, config->ekind == ELF32LEKind 872*5ffd83dbSDimitry Andric ? support::little 873*5ffd83dbSDimitry Andric : support::big)) { 874*5ffd83dbSDimitry Andric auto *isec = make<InputSection>(*this, sec, name); 875*5ffd83dbSDimitry Andric warn(toString(isec) + ": " + llvm::toString(std::move(e))); 876*5ffd83dbSDimitry Andric break; 877*5ffd83dbSDimitry Andric } 8780b57cec5SDimitry Andric updateSupportedARMFeatures(attributes); 8790b57cec5SDimitry Andric updateARMVFPArgs(attributes, this); 8800b57cec5SDimitry Andric 8810b57cec5SDimitry Andric // FIXME: Retain the first attribute section we see. The eglibc ARM 8820b57cec5SDimitry Andric // dynamic loaders require the presence of an attribute section for dlopen 8830b57cec5SDimitry Andric // to work. In a full implementation we would merge all attribute sections. 8840b57cec5SDimitry Andric if (in.armAttributes == nullptr) { 8850b57cec5SDimitry Andric in.armAttributes = make<InputSection>(*this, sec, name); 8860b57cec5SDimitry Andric return in.armAttributes; 8870b57cec5SDimitry Andric } 8880b57cec5SDimitry Andric return &InputSection::discarded; 8890b57cec5SDimitry Andric } 8900b57cec5SDimitry Andric case SHT_LLVM_DEPENDENT_LIBRARIES: { 8910b57cec5SDimitry Andric if (config->relocatable) 8920b57cec5SDimitry Andric break; 8930b57cec5SDimitry Andric ArrayRef<char> data = 8940b57cec5SDimitry Andric CHECK(this->getObj().template getSectionContentsAsArray<char>(&sec), this); 8950b57cec5SDimitry Andric if (!data.empty() && data.back() != '\0') { 8960b57cec5SDimitry Andric error(toString(this) + 8970b57cec5SDimitry Andric ": corrupted dependent libraries section (unterminated string): " + 8980b57cec5SDimitry Andric name); 8990b57cec5SDimitry Andric return &InputSection::discarded; 9000b57cec5SDimitry Andric } 9010b57cec5SDimitry Andric for (const char *d = data.begin(), *e = data.end(); d < e;) { 9020b57cec5SDimitry Andric StringRef s(d); 9030b57cec5SDimitry Andric addDependentLibrary(s, this); 9040b57cec5SDimitry Andric d += s.size() + 1; 9050b57cec5SDimitry Andric } 9060b57cec5SDimitry Andric return &InputSection::discarded; 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric case SHT_RELA: 9090b57cec5SDimitry Andric case SHT_REL: { 9100b57cec5SDimitry Andric // Find a relocation target section and associate this section with that. 9110b57cec5SDimitry Andric // Target may have been discarded if it is in a different section group 9120b57cec5SDimitry Andric // and the group is discarded, even though it's a violation of the 9130b57cec5SDimitry Andric // spec. We handle that situation gracefully by discarding dangling 9140b57cec5SDimitry Andric // relocation sections. 9150b57cec5SDimitry Andric InputSectionBase *target = getRelocTarget(sec); 9160b57cec5SDimitry Andric if (!target) 9170b57cec5SDimitry Andric return nullptr; 9180b57cec5SDimitry Andric 919480093f4SDimitry Andric // ELF spec allows mergeable sections with relocations, but they are 920480093f4SDimitry Andric // rare, and it is in practice hard to merge such sections by contents, 921480093f4SDimitry Andric // because applying relocations at end of linking changes section 922480093f4SDimitry Andric // contents. So, we simply handle such sections as non-mergeable ones. 923480093f4SDimitry Andric // Degrading like this is acceptable because section merging is optional. 924480093f4SDimitry Andric if (auto *ms = dyn_cast<MergeInputSection>(target)) { 925480093f4SDimitry Andric target = toRegularSection(ms); 926480093f4SDimitry Andric this->sections[sec.sh_info] = target; 927480093f4SDimitry Andric } 928480093f4SDimitry Andric 9290b57cec5SDimitry Andric // This section contains relocation information. 9300b57cec5SDimitry Andric // If -r is given, we do not interpret or apply relocation 9310b57cec5SDimitry Andric // but just copy relocation sections to output. 9320b57cec5SDimitry Andric if (config->relocatable) { 9330b57cec5SDimitry Andric InputSection *relocSec = make<InputSection>(*this, sec, name); 9340b57cec5SDimitry Andric // We want to add a dependency to target, similar like we do for 9350b57cec5SDimitry Andric // -emit-relocs below. This is useful for the case when linker script 9360b57cec5SDimitry Andric // contains the "/DISCARD/". It is perhaps uncommon to use a script with 9370b57cec5SDimitry Andric // -r, but we faced it in the Linux kernel and have to handle such case 9380b57cec5SDimitry Andric // and not to crash. 9390b57cec5SDimitry Andric target->dependentSections.push_back(relocSec); 9400b57cec5SDimitry Andric return relocSec; 9410b57cec5SDimitry Andric } 9420b57cec5SDimitry Andric 9430b57cec5SDimitry Andric if (target->firstRelocation) 9440b57cec5SDimitry Andric fatal(toString(this) + 9450b57cec5SDimitry Andric ": multiple relocation sections to one section are not supported"); 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric if (sec.sh_type == SHT_RELA) { 9480b57cec5SDimitry Andric ArrayRef<Elf_Rela> rels = CHECK(getObj().relas(&sec), this); 9490b57cec5SDimitry Andric target->firstRelocation = rels.begin(); 9500b57cec5SDimitry Andric target->numRelocations = rels.size(); 9510b57cec5SDimitry Andric target->areRelocsRela = true; 9520b57cec5SDimitry Andric } else { 9530b57cec5SDimitry Andric ArrayRef<Elf_Rel> rels = CHECK(getObj().rels(&sec), this); 9540b57cec5SDimitry Andric target->firstRelocation = rels.begin(); 9550b57cec5SDimitry Andric target->numRelocations = rels.size(); 9560b57cec5SDimitry Andric target->areRelocsRela = false; 9570b57cec5SDimitry Andric } 9580b57cec5SDimitry Andric assert(isUInt<31>(target->numRelocations)); 9590b57cec5SDimitry Andric 9600b57cec5SDimitry Andric // Relocation sections processed by the linker are usually removed 9610b57cec5SDimitry Andric // from the output, so returning `nullptr` for the normal case. 9620b57cec5SDimitry Andric // However, if -emit-relocs is given, we need to leave them in the output. 9630b57cec5SDimitry Andric // (Some post link analysis tools need this information.) 9640b57cec5SDimitry Andric if (config->emitRelocs) { 9650b57cec5SDimitry Andric InputSection *relocSec = make<InputSection>(*this, sec, name); 9660b57cec5SDimitry Andric // We will not emit relocation section if target was discarded. 9670b57cec5SDimitry Andric target->dependentSections.push_back(relocSec); 9680b57cec5SDimitry Andric return relocSec; 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric return nullptr; 9710b57cec5SDimitry Andric } 9720b57cec5SDimitry Andric } 9730b57cec5SDimitry Andric 9740b57cec5SDimitry Andric // The GNU linker uses .note.GNU-stack section as a marker indicating 9750b57cec5SDimitry Andric // that the code in the object file does not expect that the stack is 9760b57cec5SDimitry Andric // executable (in terms of NX bit). If all input files have the marker, 9770b57cec5SDimitry Andric // the GNU linker adds a PT_GNU_STACK segment to tells the loader to 9780b57cec5SDimitry Andric // make the stack non-executable. Most object files have this section as 9790b57cec5SDimitry Andric // of 2017. 9800b57cec5SDimitry Andric // 9810b57cec5SDimitry Andric // But making the stack non-executable is a norm today for security 9820b57cec5SDimitry Andric // reasons. Failure to do so may result in a serious security issue. 9830b57cec5SDimitry Andric // Therefore, we make LLD always add PT_GNU_STACK unless it is 9840b57cec5SDimitry Andric // explicitly told to do otherwise (by -z execstack). Because the stack 9850b57cec5SDimitry Andric // executable-ness is controlled solely by command line options, 9860b57cec5SDimitry Andric // .note.GNU-stack sections are simply ignored. 9870b57cec5SDimitry Andric if (name == ".note.GNU-stack") 9880b57cec5SDimitry Andric return &InputSection::discarded; 9890b57cec5SDimitry Andric 9900b57cec5SDimitry Andric // Object files that use processor features such as Intel Control-Flow 9910b57cec5SDimitry Andric // Enforcement (CET) or AArch64 Branch Target Identification BTI, use a 9920b57cec5SDimitry Andric // .note.gnu.property section containing a bitfield of feature bits like the 9930b57cec5SDimitry Andric // GNU_PROPERTY_X86_FEATURE_1_IBT flag. Read a bitmap containing the flag. 9940b57cec5SDimitry Andric // 9950b57cec5SDimitry Andric // Since we merge bitmaps from multiple object files to create a new 9960b57cec5SDimitry Andric // .note.gnu.property containing a single AND'ed bitmap, we discard an input 9970b57cec5SDimitry Andric // file's .note.gnu.property section. 9980b57cec5SDimitry Andric if (name == ".note.gnu.property") { 9990b57cec5SDimitry Andric ArrayRef<uint8_t> contents = check(this->getObj().getSectionContents(&sec)); 10000b57cec5SDimitry Andric this->andFeatures = readAndFeatures(this, contents); 10010b57cec5SDimitry Andric return &InputSection::discarded; 10020b57cec5SDimitry Andric } 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric // Split stacks is a feature to support a discontiguous stack, 10050b57cec5SDimitry Andric // commonly used in the programming language Go. For the details, 10060b57cec5SDimitry Andric // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled 10070b57cec5SDimitry Andric // for split stack will include a .note.GNU-split-stack section. 10080b57cec5SDimitry Andric if (name == ".note.GNU-split-stack") { 10090b57cec5SDimitry Andric if (config->relocatable) { 10100b57cec5SDimitry Andric error("cannot mix split-stack and non-split-stack in a relocatable link"); 10110b57cec5SDimitry Andric return &InputSection::discarded; 10120b57cec5SDimitry Andric } 10130b57cec5SDimitry Andric this->splitStack = true; 10140b57cec5SDimitry Andric return &InputSection::discarded; 10150b57cec5SDimitry Andric } 10160b57cec5SDimitry Andric 10170b57cec5SDimitry Andric // An object file cmpiled for split stack, but where some of the 10180b57cec5SDimitry Andric // functions were compiled with the no_split_stack_attribute will 10190b57cec5SDimitry Andric // include a .note.GNU-no-split-stack section. 10200b57cec5SDimitry Andric if (name == ".note.GNU-no-split-stack") { 10210b57cec5SDimitry Andric this->someNoSplitStack = true; 10220b57cec5SDimitry Andric return &InputSection::discarded; 10230b57cec5SDimitry Andric } 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric // The linkonce feature is a sort of proto-comdat. Some glibc i386 object 10260b57cec5SDimitry Andric // files contain definitions of symbol "__x86.get_pc_thunk.bx" in linkonce 10270b57cec5SDimitry Andric // sections. Drop those sections to avoid duplicate symbol errors. 10280b57cec5SDimitry Andric // FIXME: This is glibc PR20543, we should remove this hack once that has been 10290b57cec5SDimitry Andric // fixed for a while. 10300b57cec5SDimitry Andric if (name == ".gnu.linkonce.t.__x86.get_pc_thunk.bx" || 10310b57cec5SDimitry Andric name == ".gnu.linkonce.t.__i686.get_pc_thunk.bx") 10320b57cec5SDimitry Andric return &InputSection::discarded; 10330b57cec5SDimitry Andric 10340b57cec5SDimitry Andric // If we are creating a new .build-id section, strip existing .build-id 10350b57cec5SDimitry Andric // sections so that the output won't have more than one .build-id. 10360b57cec5SDimitry Andric // This is not usually a problem because input object files normally don't 10370b57cec5SDimitry Andric // have .build-id sections, but you can create such files by 10380b57cec5SDimitry Andric // "ld.{bfd,gold,lld} -r --build-id", and we want to guard against it. 10390b57cec5SDimitry Andric if (name == ".note.gnu.build-id" && config->buildId != BuildIdKind::None) 10400b57cec5SDimitry Andric return &InputSection::discarded; 10410b57cec5SDimitry Andric 10420b57cec5SDimitry Andric // The linker merges EH (exception handling) frames and creates a 10430b57cec5SDimitry Andric // .eh_frame_hdr section for runtime. So we handle them with a special 10440b57cec5SDimitry Andric // class. For relocatable outputs, they are just passed through. 10450b57cec5SDimitry Andric if (name == ".eh_frame" && !config->relocatable) 10460b57cec5SDimitry Andric return make<EhInputSection>(*this, sec, name); 10470b57cec5SDimitry Andric 104885868e8aSDimitry Andric if (shouldMerge(sec, name)) 10490b57cec5SDimitry Andric return make<MergeInputSection>(*this, sec, name); 10500b57cec5SDimitry Andric return make<InputSection>(*this, sec, name); 10510b57cec5SDimitry Andric } 10520b57cec5SDimitry Andric 10530b57cec5SDimitry Andric template <class ELFT> 10540b57cec5SDimitry Andric StringRef ObjFile<ELFT>::getSectionName(const Elf_Shdr &sec) { 10550b57cec5SDimitry Andric return CHECK(getObj().getSectionName(&sec, sectionStringTable), this); 10560b57cec5SDimitry Andric } 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric // Initialize this->Symbols. this->Symbols is a parallel array as 10590b57cec5SDimitry Andric // its corresponding ELF symbol table. 10600b57cec5SDimitry Andric template <class ELFT> void ObjFile<ELFT>::initializeSymbols() { 10610b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>(); 10620b57cec5SDimitry Andric this->symbols.resize(eSyms.size()); 10630b57cec5SDimitry Andric 1064*5ffd83dbSDimitry Andric // Fill in InputFile::symbols. Some entries have been initialized 10650b57cec5SDimitry Andric // because of LazyObjFile. 10660b57cec5SDimitry Andric for (size_t i = 0, end = eSyms.size(); i != end; ++i) { 1067*5ffd83dbSDimitry Andric if (this->symbols[i]) 1068*5ffd83dbSDimitry Andric continue; 10690b57cec5SDimitry Andric const Elf_Sym &eSym = eSyms[i]; 10700b57cec5SDimitry Andric uint32_t secIdx = getSectionIndex(eSym); 10710b57cec5SDimitry Andric if (secIdx >= this->sections.size()) 10720b57cec5SDimitry Andric fatal(toString(this) + ": invalid section index: " + Twine(secIdx)); 1073*5ffd83dbSDimitry Andric if (eSym.getBinding() != STB_LOCAL) { 1074*5ffd83dbSDimitry Andric if (i < firstGlobal) 1075*5ffd83dbSDimitry Andric error(toString(this) + ": non-local symbol (" + Twine(i) + 1076*5ffd83dbSDimitry Andric ") found at index < .symtab's sh_info (" + Twine(firstGlobal) + 1077*5ffd83dbSDimitry Andric ")"); 1078*5ffd83dbSDimitry Andric this->symbols[i] = 1079*5ffd83dbSDimitry Andric symtab->insert(CHECK(eSyms[i].getName(this->stringTable), this)); 1080*5ffd83dbSDimitry Andric continue; 1081*5ffd83dbSDimitry Andric } 1082*5ffd83dbSDimitry Andric 1083*5ffd83dbSDimitry Andric // Handle local symbols. Local symbols are not added to the symbol 1084*5ffd83dbSDimitry Andric // table because they are not visible from other object files. We 1085*5ffd83dbSDimitry Andric // allocate symbol instances and add their pointers to symbols. 1086*5ffd83dbSDimitry Andric if (i >= firstGlobal) 1087*5ffd83dbSDimitry Andric errorOrWarn(toString(this) + ": STB_LOCAL symbol (" + Twine(i) + 1088*5ffd83dbSDimitry Andric ") found at index >= .symtab's sh_info (" + 1089*5ffd83dbSDimitry Andric Twine(firstGlobal) + ")"); 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric InputSectionBase *sec = this->sections[secIdx]; 1092*5ffd83dbSDimitry Andric uint8_t type = eSym.getType(); 1093*5ffd83dbSDimitry Andric if (type == STT_FILE) 1094*5ffd83dbSDimitry Andric sourceFile = CHECK(eSym.getName(this->stringTable), this); 1095*5ffd83dbSDimitry Andric if (this->stringTable.size() <= eSym.st_name) 1096*5ffd83dbSDimitry Andric fatal(toString(this) + ": invalid symbol name offset"); 1097*5ffd83dbSDimitry Andric StringRefZ name = this->stringTable.data() + eSym.st_name; 1098*5ffd83dbSDimitry Andric 1099*5ffd83dbSDimitry Andric if (eSym.st_shndx == SHN_UNDEF) 1100*5ffd83dbSDimitry Andric this->symbols[i] = 1101*5ffd83dbSDimitry Andric make<Undefined>(this, name, STB_LOCAL, eSym.st_other, type); 1102*5ffd83dbSDimitry Andric else if (sec == &InputSection::discarded) 1103*5ffd83dbSDimitry Andric this->symbols[i] = 1104*5ffd83dbSDimitry Andric make<Undefined>(this, name, STB_LOCAL, eSym.st_other, type, 1105*5ffd83dbSDimitry Andric /*discardedSecIdx=*/secIdx); 1106*5ffd83dbSDimitry Andric else 1107*5ffd83dbSDimitry Andric this->symbols[i] = make<Defined>(this, name, STB_LOCAL, eSym.st_other, 1108*5ffd83dbSDimitry Andric type, eSym.st_value, eSym.st_size, sec); 1109*5ffd83dbSDimitry Andric } 1110*5ffd83dbSDimitry Andric 1111*5ffd83dbSDimitry Andric // Symbol resolution of non-local symbols. 1112*5ffd83dbSDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) { 1113*5ffd83dbSDimitry Andric const Elf_Sym &eSym = eSyms[i]; 11140b57cec5SDimitry Andric uint8_t binding = eSym.getBinding(); 1115*5ffd83dbSDimitry Andric if (binding == STB_LOCAL) 1116*5ffd83dbSDimitry Andric continue; // Errored above. 1117*5ffd83dbSDimitry Andric 1118*5ffd83dbSDimitry Andric uint32_t secIdx = getSectionIndex(eSym); 1119*5ffd83dbSDimitry Andric InputSectionBase *sec = this->sections[secIdx]; 11200b57cec5SDimitry Andric uint8_t stOther = eSym.st_other; 11210b57cec5SDimitry Andric uint8_t type = eSym.getType(); 11220b57cec5SDimitry Andric uint64_t value = eSym.st_value; 11230b57cec5SDimitry Andric uint64_t size = eSym.st_size; 11240b57cec5SDimitry Andric StringRefZ name = this->stringTable.data() + eSym.st_name; 11250b57cec5SDimitry Andric 11260b57cec5SDimitry Andric // Handle global undefined symbols. 11270b57cec5SDimitry Andric if (eSym.st_shndx == SHN_UNDEF) { 11280b57cec5SDimitry Andric this->symbols[i]->resolve(Undefined{this, name, binding, stOther, type}); 112985868e8aSDimitry Andric this->symbols[i]->referenced = true; 11300b57cec5SDimitry Andric continue; 11310b57cec5SDimitry Andric } 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric // Handle global common symbols. 11340b57cec5SDimitry Andric if (eSym.st_shndx == SHN_COMMON) { 11350b57cec5SDimitry Andric if (value == 0 || value >= UINT32_MAX) 11360b57cec5SDimitry Andric fatal(toString(this) + ": common symbol '" + StringRef(name.data) + 11370b57cec5SDimitry Andric "' has invalid alignment: " + Twine(value)); 11380b57cec5SDimitry Andric this->symbols[i]->resolve( 11390b57cec5SDimitry Andric CommonSymbol{this, name, binding, stOther, type, value, size}); 11400b57cec5SDimitry Andric continue; 11410b57cec5SDimitry Andric } 11420b57cec5SDimitry Andric 11430b57cec5SDimitry Andric // If a defined symbol is in a discarded section, handle it as if it 11440b57cec5SDimitry Andric // were an undefined symbol. Such symbol doesn't comply with the 11450b57cec5SDimitry Andric // standard, but in practice, a .eh_frame often directly refer 11460b57cec5SDimitry Andric // COMDAT member sections, and if a comdat group is discarded, some 11470b57cec5SDimitry Andric // defined symbol in a .eh_frame becomes dangling symbols. 11480b57cec5SDimitry Andric if (sec == &InputSection::discarded) { 1149*5ffd83dbSDimitry Andric Undefined und{this, name, binding, stOther, type, secIdx}; 1150*5ffd83dbSDimitry Andric Symbol *sym = this->symbols[i]; 1151*5ffd83dbSDimitry Andric // !ArchiveFile::parsed or LazyObjFile::fetched means that the file 1152*5ffd83dbSDimitry Andric // containing this object has not finished processing, i.e. this symbol is 1153*5ffd83dbSDimitry Andric // a result of a lazy symbol fetch. We should demote the lazy symbol to an 1154*5ffd83dbSDimitry Andric // Undefined so that any relocations outside of the group to it will 1155*5ffd83dbSDimitry Andric // trigger a discarded section error. 1156*5ffd83dbSDimitry Andric if ((sym->symbolKind == Symbol::LazyArchiveKind && 1157*5ffd83dbSDimitry Andric !cast<ArchiveFile>(sym->file)->parsed) || 1158*5ffd83dbSDimitry Andric (sym->symbolKind == Symbol::LazyObjectKind && 1159*5ffd83dbSDimitry Andric cast<LazyObjFile>(sym->file)->fetched)) 1160*5ffd83dbSDimitry Andric sym->replace(und); 1161*5ffd83dbSDimitry Andric else 1162*5ffd83dbSDimitry Andric sym->resolve(und); 11630b57cec5SDimitry Andric continue; 11640b57cec5SDimitry Andric } 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric // Handle global defined symbols. 11670b57cec5SDimitry Andric if (binding == STB_GLOBAL || binding == STB_WEAK || 11680b57cec5SDimitry Andric binding == STB_GNU_UNIQUE) { 11690b57cec5SDimitry Andric this->symbols[i]->resolve( 11700b57cec5SDimitry Andric Defined{this, name, binding, stOther, type, value, size, sec}); 11710b57cec5SDimitry Andric continue; 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric fatal(toString(this) + ": unexpected binding: " + Twine((int)binding)); 11750b57cec5SDimitry Andric } 11760b57cec5SDimitry Andric } 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric ArchiveFile::ArchiveFile(std::unique_ptr<Archive> &&file) 11790b57cec5SDimitry Andric : InputFile(ArchiveKind, file->getMemoryBufferRef()), 11800b57cec5SDimitry Andric file(std::move(file)) {} 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric void ArchiveFile::parse() { 11830b57cec5SDimitry Andric for (const Archive::Symbol &sym : file->symbols()) 11840b57cec5SDimitry Andric symtab->addSymbol(LazyArchive{*this, sym}); 1185*5ffd83dbSDimitry Andric 1186*5ffd83dbSDimitry Andric // Inform a future invocation of ObjFile<ELFT>::initializeSymbols() that this 1187*5ffd83dbSDimitry Andric // archive has been processed. 1188*5ffd83dbSDimitry Andric parsed = true; 11890b57cec5SDimitry Andric } 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric // Returns a buffer pointing to a member file containing a given symbol. 11920b57cec5SDimitry Andric void ArchiveFile::fetch(const Archive::Symbol &sym) { 11930b57cec5SDimitry Andric Archive::Child c = 11940b57cec5SDimitry Andric CHECK(sym.getMember(), toString(this) + 11950b57cec5SDimitry Andric ": could not get the member for symbol " + 11960b57cec5SDimitry Andric toELFString(sym)); 11970b57cec5SDimitry Andric 11980b57cec5SDimitry Andric if (!seen.insert(c.getChildOffset()).second) 11990b57cec5SDimitry Andric return; 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric MemoryBufferRef mb = 12020b57cec5SDimitry Andric CHECK(c.getMemoryBufferRef(), 12030b57cec5SDimitry Andric toString(this) + 12040b57cec5SDimitry Andric ": could not get the buffer for the member defining symbol " + 12050b57cec5SDimitry Andric toELFString(sym)); 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric if (tar && c.getParent()->isThin()) 12080b57cec5SDimitry Andric tar->append(relativeToRoot(CHECK(c.getFullName(), this)), mb.getBuffer()); 12090b57cec5SDimitry Andric 1210*5ffd83dbSDimitry Andric InputFile *file = createObjectFile(mb, getName(), c.getChildOffset()); 12110b57cec5SDimitry Andric file->groupId = groupId; 12120b57cec5SDimitry Andric parseFile(file); 12130b57cec5SDimitry Andric } 12140b57cec5SDimitry Andric 1215*5ffd83dbSDimitry Andric size_t ArchiveFile::getMemberCount() const { 1216*5ffd83dbSDimitry Andric size_t count = 0; 1217*5ffd83dbSDimitry Andric Error err = Error::success(); 1218*5ffd83dbSDimitry Andric for (const Archive::Child &c : file->children(err)) { 1219*5ffd83dbSDimitry Andric (void)c; 1220*5ffd83dbSDimitry Andric ++count; 1221*5ffd83dbSDimitry Andric } 1222*5ffd83dbSDimitry Andric // This function is used by --print-archive-stats=, where an error does not 1223*5ffd83dbSDimitry Andric // really matter. 1224*5ffd83dbSDimitry Andric consumeError(std::move(err)); 1225*5ffd83dbSDimitry Andric return count; 1226*5ffd83dbSDimitry Andric } 1227*5ffd83dbSDimitry Andric 12280b57cec5SDimitry Andric unsigned SharedFile::vernauxNum; 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric // Parse the version definitions in the object file if present, and return a 12310b57cec5SDimitry Andric // vector whose nth element contains a pointer to the Elf_Verdef for version 12320b57cec5SDimitry Andric // identifier n. Version identifiers that are not definitions map to nullptr. 12330b57cec5SDimitry Andric template <typename ELFT> 12340b57cec5SDimitry Andric static std::vector<const void *> parseVerdefs(const uint8_t *base, 12350b57cec5SDimitry Andric const typename ELFT::Shdr *sec) { 12360b57cec5SDimitry Andric if (!sec) 12370b57cec5SDimitry Andric return {}; 12380b57cec5SDimitry Andric 12390b57cec5SDimitry Andric // We cannot determine the largest verdef identifier without inspecting 12400b57cec5SDimitry Andric // every Elf_Verdef, but both bfd and gold assign verdef identifiers 12410b57cec5SDimitry Andric // sequentially starting from 1, so we predict that the largest identifier 12420b57cec5SDimitry Andric // will be verdefCount. 12430b57cec5SDimitry Andric unsigned verdefCount = sec->sh_info; 12440b57cec5SDimitry Andric std::vector<const void *> verdefs(verdefCount + 1); 12450b57cec5SDimitry Andric 12460b57cec5SDimitry Andric // Build the Verdefs array by following the chain of Elf_Verdef objects 12470b57cec5SDimitry Andric // from the start of the .gnu.version_d section. 12480b57cec5SDimitry Andric const uint8_t *verdef = base + sec->sh_offset; 12490b57cec5SDimitry Andric for (unsigned i = 0; i != verdefCount; ++i) { 12500b57cec5SDimitry Andric auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef); 12510b57cec5SDimitry Andric verdef += curVerdef->vd_next; 12520b57cec5SDimitry Andric unsigned verdefIndex = curVerdef->vd_ndx; 12530b57cec5SDimitry Andric verdefs.resize(verdefIndex + 1); 12540b57cec5SDimitry Andric verdefs[verdefIndex] = curVerdef; 12550b57cec5SDimitry Andric } 12560b57cec5SDimitry Andric return verdefs; 12570b57cec5SDimitry Andric } 12580b57cec5SDimitry Andric 1259*5ffd83dbSDimitry Andric // Parse SHT_GNU_verneed to properly set the name of a versioned undefined 1260*5ffd83dbSDimitry Andric // symbol. We detect fatal issues which would cause vulnerabilities, but do not 1261*5ffd83dbSDimitry Andric // implement sophisticated error checking like in llvm-readobj because the value 1262*5ffd83dbSDimitry Andric // of such diagnostics is low. 1263*5ffd83dbSDimitry Andric template <typename ELFT> 1264*5ffd83dbSDimitry Andric std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj, 1265*5ffd83dbSDimitry Andric const typename ELFT::Shdr *sec) { 1266*5ffd83dbSDimitry Andric if (!sec) 1267*5ffd83dbSDimitry Andric return {}; 1268*5ffd83dbSDimitry Andric std::vector<uint32_t> verneeds; 1269*5ffd83dbSDimitry Andric ArrayRef<uint8_t> data = CHECK(obj.getSectionContents(sec), this); 1270*5ffd83dbSDimitry Andric const uint8_t *verneedBuf = data.begin(); 1271*5ffd83dbSDimitry Andric for (unsigned i = 0; i != sec->sh_info; ++i) { 1272*5ffd83dbSDimitry Andric if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) 1273*5ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Verneed"); 1274*5ffd83dbSDimitry Andric auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf); 1275*5ffd83dbSDimitry Andric const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux; 1276*5ffd83dbSDimitry Andric for (unsigned j = 0; j != vn->vn_cnt; ++j) { 1277*5ffd83dbSDimitry Andric if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) 1278*5ffd83dbSDimitry Andric fatal(toString(this) + " has an invalid Vernaux"); 1279*5ffd83dbSDimitry Andric auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf); 1280*5ffd83dbSDimitry Andric if (aux->vna_name >= this->stringTable.size()) 1281*5ffd83dbSDimitry Andric fatal(toString(this) + " has a Vernaux with an invalid vna_name"); 1282*5ffd83dbSDimitry Andric uint16_t version = aux->vna_other & VERSYM_VERSION; 1283*5ffd83dbSDimitry Andric if (version >= verneeds.size()) 1284*5ffd83dbSDimitry Andric verneeds.resize(version + 1); 1285*5ffd83dbSDimitry Andric verneeds[version] = aux->vna_name; 1286*5ffd83dbSDimitry Andric vernauxBuf += aux->vna_next; 1287*5ffd83dbSDimitry Andric } 1288*5ffd83dbSDimitry Andric verneedBuf += vn->vn_next; 1289*5ffd83dbSDimitry Andric } 1290*5ffd83dbSDimitry Andric return verneeds; 1291*5ffd83dbSDimitry Andric } 1292*5ffd83dbSDimitry 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>(); 13320b57cec5SDimitry Andric ArrayRef<Elf_Shdr> sections = CHECK(obj.sections(), this); 13330b57cec5SDimitry Andric 13340b57cec5SDimitry Andric const Elf_Shdr *versymSec = nullptr; 13350b57cec5SDimitry Andric const Elf_Shdr *verdefSec = nullptr; 1336*5ffd83dbSDimitry 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 = 13450b57cec5SDimitry 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; 1353*5ffd83dbSDimitry Andric case SHT_GNU_verneed: 1354*5ffd83dbSDimitry Andric verneedSec = &sec; 1355*5ffd83dbSDimitry 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. 13800b57cec5SDimitry Andric DenseMap<StringRef, SharedFile *>::iterator it; 13810b57cec5SDimitry Andric bool wasInserted; 13820b57cec5SDimitry Andric std::tie(it, wasInserted) = symtab->soNames.try_emplace(soName, this); 13830b57cec5SDimitry Andric 13840b57cec5SDimitry Andric // If a DSO appears more than once on the command line with and without 13850b57cec5SDimitry Andric // --as-needed, --no-as-needed takes precedence over --as-needed because a 13860b57cec5SDimitry Andric // user can add an extra DSO with --no-as-needed to force it to be added to 13870b57cec5SDimitry Andric // the dependency list. 13880b57cec5SDimitry Andric it->second->isNeeded |= isNeeded; 13890b57cec5SDimitry Andric if (!wasInserted) 13900b57cec5SDimitry Andric return; 13910b57cec5SDimitry Andric 13920b57cec5SDimitry Andric sharedFiles.push_back(this); 13930b57cec5SDimitry Andric 13940b57cec5SDimitry Andric verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec); 1395*5ffd83dbSDimitry Andric std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec); 13960b57cec5SDimitry Andric 13970b57cec5SDimitry Andric // Parse ".gnu.version" section which is a parallel array for the symbol 13980b57cec5SDimitry Andric // table. If a given file doesn't have a ".gnu.version" section, we use 13990b57cec5SDimitry Andric // VER_NDX_GLOBAL. 14000b57cec5SDimitry Andric size_t size = numELFSyms - firstGlobal; 1401*5ffd83dbSDimitry Andric std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL); 14020b57cec5SDimitry Andric if (versymSec) { 14030b57cec5SDimitry Andric ArrayRef<Elf_Versym> versym = 14040b57cec5SDimitry Andric CHECK(obj.template getSectionContentsAsArray<Elf_Versym>(versymSec), 14050b57cec5SDimitry Andric this) 14060b57cec5SDimitry Andric .slice(firstGlobal); 14070b57cec5SDimitry Andric for (size_t i = 0; i < size; ++i) 14080b57cec5SDimitry Andric versyms[i] = versym[i].vs_index; 14090b57cec5SDimitry Andric } 14100b57cec5SDimitry Andric 14110b57cec5SDimitry Andric // System libraries can have a lot of symbols with versions. Using a 14120b57cec5SDimitry Andric // fixed buffer for computing the versions name (foo@ver) can save a 14130b57cec5SDimitry Andric // lot of allocations. 14140b57cec5SDimitry Andric SmallString<0> versionedNameBuffer; 14150b57cec5SDimitry Andric 14160b57cec5SDimitry Andric // Add symbols to the symbol table. 14170b57cec5SDimitry Andric ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>(); 14180b57cec5SDimitry Andric for (size_t i = 0; i < syms.size(); ++i) { 14190b57cec5SDimitry Andric const Elf_Sym &sym = syms[i]; 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric // ELF spec requires that all local symbols precede weak or global 14220b57cec5SDimitry Andric // symbols in each symbol table, and the index of first non-local symbol 14230b57cec5SDimitry Andric // is stored to sh_info. If a local symbol appears after some non-local 14240b57cec5SDimitry Andric // symbol, that's a violation of the spec. 14250b57cec5SDimitry Andric StringRef name = CHECK(sym.getName(this->stringTable), this); 14260b57cec5SDimitry Andric if (sym.getBinding() == STB_LOCAL) { 14270b57cec5SDimitry Andric warn("found local symbol '" + name + 14280b57cec5SDimitry Andric "' in global part of symbol table in file " + toString(this)); 14290b57cec5SDimitry Andric continue; 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 1432*5ffd83dbSDimitry Andric uint16_t idx = versyms[i] & ~VERSYM_HIDDEN; 14330b57cec5SDimitry Andric if (sym.isUndefined()) { 1434*5ffd83dbSDimitry Andric // For unversioned undefined symbols, VER_NDX_GLOBAL makes more sense but 1435*5ffd83dbSDimitry Andric // as of binutils 2.34, GNU ld produces VER_NDX_LOCAL. 1436*5ffd83dbSDimitry Andric if (idx != VER_NDX_LOCAL && idx != VER_NDX_GLOBAL) { 1437*5ffd83dbSDimitry Andric if (idx >= verneeds.size()) { 1438*5ffd83dbSDimitry Andric error("corrupt input file: version need index " + Twine(idx) + 1439*5ffd83dbSDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 1440*5ffd83dbSDimitry Andric toString(this)); 1441*5ffd83dbSDimitry Andric continue; 1442*5ffd83dbSDimitry Andric } 1443*5ffd83dbSDimitry Andric StringRef verName = this->stringTable.data() + verneeds[idx]; 1444*5ffd83dbSDimitry Andric versionedNameBuffer.clear(); 1445*5ffd83dbSDimitry Andric name = 1446*5ffd83dbSDimitry Andric saver.save((name + "@" + verName).toStringRef(versionedNameBuffer)); 1447*5ffd83dbSDimitry Andric } 14480b57cec5SDimitry Andric Symbol *s = symtab->addSymbol( 14490b57cec5SDimitry Andric Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()}); 14500b57cec5SDimitry Andric s->exportDynamic = true; 14510b57cec5SDimitry Andric continue; 14520b57cec5SDimitry Andric } 14530b57cec5SDimitry Andric 14540b57cec5SDimitry Andric // MIPS BFD linker puts _gp_disp symbol into DSO files and incorrectly 14550b57cec5SDimitry Andric // assigns VER_NDX_LOCAL to this section global symbol. Here is a 14560b57cec5SDimitry Andric // workaround for this bug. 14570b57cec5SDimitry Andric if (config->emachine == EM_MIPS && idx == VER_NDX_LOCAL && 14580b57cec5SDimitry Andric name == "_gp_disp") 14590b57cec5SDimitry Andric continue; 14600b57cec5SDimitry Andric 14610b57cec5SDimitry Andric uint32_t alignment = getAlignment<ELFT>(sections, sym); 14620b57cec5SDimitry Andric if (!(versyms[i] & VERSYM_HIDDEN)) { 14630b57cec5SDimitry Andric symtab->addSymbol(SharedSymbol{*this, name, sym.getBinding(), 14640b57cec5SDimitry Andric sym.st_other, sym.getType(), sym.st_value, 14650b57cec5SDimitry Andric sym.st_size, alignment, idx}); 14660b57cec5SDimitry Andric } 14670b57cec5SDimitry Andric 14680b57cec5SDimitry Andric // Also add the symbol with the versioned name to handle undefined symbols 14690b57cec5SDimitry Andric // with explicit versions. 14700b57cec5SDimitry Andric if (idx == VER_NDX_GLOBAL) 14710b57cec5SDimitry Andric continue; 14720b57cec5SDimitry Andric 14730b57cec5SDimitry Andric if (idx >= verdefs.size() || idx == VER_NDX_LOCAL) { 14740b57cec5SDimitry Andric error("corrupt input file: version definition index " + Twine(idx) + 14750b57cec5SDimitry Andric " for symbol " + name + " is out of bounds\n>>> defined in " + 14760b57cec5SDimitry Andric toString(this)); 14770b57cec5SDimitry Andric continue; 14780b57cec5SDimitry Andric } 14790b57cec5SDimitry Andric 14800b57cec5SDimitry Andric StringRef verName = 14810b57cec5SDimitry Andric this->stringTable.data() + 14820b57cec5SDimitry Andric reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name; 14830b57cec5SDimitry Andric versionedNameBuffer.clear(); 14840b57cec5SDimitry Andric name = (name + "@" + verName).toStringRef(versionedNameBuffer); 14850b57cec5SDimitry Andric symtab->addSymbol(SharedSymbol{*this, saver.save(name), sym.getBinding(), 14860b57cec5SDimitry Andric sym.st_other, sym.getType(), sym.st_value, 14870b57cec5SDimitry Andric sym.st_size, alignment, idx}); 14880b57cec5SDimitry Andric } 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric 14910b57cec5SDimitry Andric static ELFKind getBitcodeELFKind(const Triple &t) { 14920b57cec5SDimitry Andric if (t.isLittleEndian()) 14930b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind; 14940b57cec5SDimitry Andric return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind; 14950b57cec5SDimitry Andric } 14960b57cec5SDimitry Andric 14970b57cec5SDimitry Andric static uint8_t getBitcodeMachineKind(StringRef path, const Triple &t) { 14980b57cec5SDimitry Andric switch (t.getArch()) { 14990b57cec5SDimitry Andric case Triple::aarch64: 15000b57cec5SDimitry Andric return EM_AARCH64; 15010b57cec5SDimitry Andric case Triple::amdgcn: 15020b57cec5SDimitry Andric case Triple::r600: 15030b57cec5SDimitry Andric return EM_AMDGPU; 15040b57cec5SDimitry Andric case Triple::arm: 15050b57cec5SDimitry Andric case Triple::thumb: 15060b57cec5SDimitry Andric return EM_ARM; 15070b57cec5SDimitry Andric case Triple::avr: 15080b57cec5SDimitry Andric return EM_AVR; 15090b57cec5SDimitry Andric case Triple::mips: 15100b57cec5SDimitry Andric case Triple::mipsel: 15110b57cec5SDimitry Andric case Triple::mips64: 15120b57cec5SDimitry Andric case Triple::mips64el: 15130b57cec5SDimitry Andric return EM_MIPS; 15140b57cec5SDimitry Andric case Triple::msp430: 15150b57cec5SDimitry Andric return EM_MSP430; 15160b57cec5SDimitry Andric case Triple::ppc: 15170b57cec5SDimitry Andric return EM_PPC; 15180b57cec5SDimitry Andric case Triple::ppc64: 15190b57cec5SDimitry Andric case Triple::ppc64le: 15200b57cec5SDimitry Andric return EM_PPC64; 15210b57cec5SDimitry Andric case Triple::riscv32: 15220b57cec5SDimitry Andric case Triple::riscv64: 15230b57cec5SDimitry Andric return EM_RISCV; 15240b57cec5SDimitry Andric case Triple::x86: 15250b57cec5SDimitry Andric return t.isOSIAMCU() ? EM_IAMCU : EM_386; 15260b57cec5SDimitry Andric case Triple::x86_64: 15270b57cec5SDimitry Andric return EM_X86_64; 15280b57cec5SDimitry Andric default: 15290b57cec5SDimitry Andric error(path + ": could not infer e_machine from bitcode target triple " + 15300b57cec5SDimitry Andric t.str()); 15310b57cec5SDimitry Andric return EM_NONE; 15320b57cec5SDimitry Andric } 15330b57cec5SDimitry Andric } 15340b57cec5SDimitry Andric 15350b57cec5SDimitry Andric BitcodeFile::BitcodeFile(MemoryBufferRef mb, StringRef archiveName, 15360b57cec5SDimitry Andric uint64_t offsetInArchive) 15370b57cec5SDimitry Andric : InputFile(BitcodeKind, mb) { 1538*5ffd83dbSDimitry Andric this->archiveName = std::string(archiveName); 15390b57cec5SDimitry Andric 15400b57cec5SDimitry Andric std::string path = mb.getBufferIdentifier().str(); 15410b57cec5SDimitry Andric if (config->thinLTOIndexOnly) 15420b57cec5SDimitry Andric path = replaceThinLTOSuffix(mb.getBufferIdentifier()); 15430b57cec5SDimitry Andric 15440b57cec5SDimitry Andric // ThinLTO assumes that all MemoryBufferRefs given to it have a unique 15450b57cec5SDimitry Andric // name. If two archives define two members with the same name, this 15460b57cec5SDimitry Andric // causes a collision which result in only one of the objects being taken 15470b57cec5SDimitry Andric // into consideration at LTO time (which very likely causes undefined 15480b57cec5SDimitry Andric // symbols later in the link stage). So we append file offset to make 15490b57cec5SDimitry Andric // filename unique. 1550*5ffd83dbSDimitry Andric StringRef name = 1551*5ffd83dbSDimitry Andric archiveName.empty() 15520b57cec5SDimitry Andric ? saver.save(path) 1553*5ffd83dbSDimitry Andric : saver.save(archiveName + "(" + path::filename(path) + " at " + 15540b57cec5SDimitry Andric utostr(offsetInArchive) + ")"); 15550b57cec5SDimitry Andric MemoryBufferRef mbref(mb.getBuffer(), name); 15560b57cec5SDimitry Andric 15570b57cec5SDimitry Andric obj = CHECK(lto::InputFile::create(mbref), this); 15580b57cec5SDimitry Andric 15590b57cec5SDimitry Andric Triple t(obj->getTargetTriple()); 15600b57cec5SDimitry Andric ekind = getBitcodeELFKind(t); 15610b57cec5SDimitry Andric emachine = getBitcodeMachineKind(mb.getBufferIdentifier(), t); 15620b57cec5SDimitry Andric } 15630b57cec5SDimitry Andric 15640b57cec5SDimitry Andric static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) { 15650b57cec5SDimitry Andric switch (gvVisibility) { 15660b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: 15670b57cec5SDimitry Andric return STV_DEFAULT; 15680b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: 15690b57cec5SDimitry Andric return STV_HIDDEN; 15700b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: 15710b57cec5SDimitry Andric return STV_PROTECTED; 15720b57cec5SDimitry Andric } 15730b57cec5SDimitry Andric llvm_unreachable("unknown visibility"); 15740b57cec5SDimitry Andric } 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric template <class ELFT> 15770b57cec5SDimitry Andric static Symbol *createBitcodeSymbol(const std::vector<bool> &keptComdats, 15780b57cec5SDimitry Andric const lto::InputFile::Symbol &objSym, 15790b57cec5SDimitry Andric BitcodeFile &f) { 15800b57cec5SDimitry Andric StringRef name = saver.save(objSym.getName()); 15810b57cec5SDimitry Andric uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL; 15820b57cec5SDimitry Andric uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE; 15830b57cec5SDimitry Andric uint8_t visibility = mapVisibility(objSym.getVisibility()); 15840b57cec5SDimitry Andric bool canOmitFromDynSym = objSym.canBeOmittedFromSymbolTable(); 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric int c = objSym.getComdatIndex(); 15870b57cec5SDimitry Andric if (objSym.isUndefined() || (c != -1 && !keptComdats[c])) { 158885868e8aSDimitry Andric Undefined newSym(&f, name, binding, visibility, type); 15890b57cec5SDimitry Andric if (canOmitFromDynSym) 159085868e8aSDimitry Andric newSym.exportDynamic = false; 159185868e8aSDimitry Andric Symbol *ret = symtab->addSymbol(newSym); 159285868e8aSDimitry Andric ret->referenced = true; 159385868e8aSDimitry Andric return ret; 15940b57cec5SDimitry Andric } 15950b57cec5SDimitry Andric 15960b57cec5SDimitry Andric if (objSym.isCommon()) 15970b57cec5SDimitry Andric return symtab->addSymbol( 15980b57cec5SDimitry Andric CommonSymbol{&f, name, binding, visibility, STT_OBJECT, 15990b57cec5SDimitry Andric objSym.getCommonAlignment(), objSym.getCommonSize()}); 16000b57cec5SDimitry Andric 160185868e8aSDimitry Andric Defined newSym(&f, name, binding, visibility, type, 0, 0, nullptr); 16020b57cec5SDimitry Andric if (canOmitFromDynSym) 160385868e8aSDimitry Andric newSym.exportDynamic = false; 160485868e8aSDimitry Andric return symtab->addSymbol(newSym); 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric 16070b57cec5SDimitry Andric template <class ELFT> void BitcodeFile::parse() { 16080b57cec5SDimitry Andric std::vector<bool> keptComdats; 16090b57cec5SDimitry Andric for (StringRef s : obj->getComdatTable()) 16100b57cec5SDimitry Andric keptComdats.push_back( 16110b57cec5SDimitry Andric symtab->comdatGroups.try_emplace(CachedHashStringRef(s), this).second); 16120b57cec5SDimitry Andric 16130b57cec5SDimitry Andric for (const lto::InputFile::Symbol &objSym : obj->symbols()) 16140b57cec5SDimitry Andric symbols.push_back(createBitcodeSymbol<ELFT>(keptComdats, objSym, *this)); 16150b57cec5SDimitry Andric 16160b57cec5SDimitry Andric for (auto l : obj->getDependentLibraries()) 16170b57cec5SDimitry Andric addDependentLibrary(l, this); 16180b57cec5SDimitry Andric } 16190b57cec5SDimitry Andric 16200b57cec5SDimitry Andric void BinaryFile::parse() { 16210b57cec5SDimitry Andric ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer()); 16220b57cec5SDimitry Andric auto *section = make<InputSection>(this, SHF_ALLOC | SHF_WRITE, SHT_PROGBITS, 16230b57cec5SDimitry Andric 8, data, ".data"); 16240b57cec5SDimitry Andric sections.push_back(section); 16250b57cec5SDimitry Andric 16260b57cec5SDimitry Andric // For each input file foo that is embedded to a result as a binary 16270b57cec5SDimitry Andric // blob, we define _binary_foo_{start,end,size} symbols, so that 16280b57cec5SDimitry Andric // user programs can access blobs by name. Non-alphanumeric 16290b57cec5SDimitry Andric // characters in a filename are replaced with underscore. 16300b57cec5SDimitry Andric std::string s = "_binary_" + mb.getBufferIdentifier().str(); 16310b57cec5SDimitry Andric for (size_t i = 0; i < s.size(); ++i) 16320b57cec5SDimitry Andric if (!isAlnum(s[i])) 16330b57cec5SDimitry Andric s[i] = '_'; 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric symtab->addSymbol(Defined{nullptr, saver.save(s + "_start"), STB_GLOBAL, 16360b57cec5SDimitry Andric STV_DEFAULT, STT_OBJECT, 0, 0, section}); 16370b57cec5SDimitry Andric symtab->addSymbol(Defined{nullptr, saver.save(s + "_end"), STB_GLOBAL, 16380b57cec5SDimitry Andric STV_DEFAULT, STT_OBJECT, data.size(), 0, section}); 16390b57cec5SDimitry Andric symtab->addSymbol(Defined{nullptr, saver.save(s + "_size"), STB_GLOBAL, 16400b57cec5SDimitry Andric STV_DEFAULT, STT_OBJECT, data.size(), 0, nullptr}); 16410b57cec5SDimitry Andric } 16420b57cec5SDimitry Andric 1643*5ffd83dbSDimitry Andric InputFile *elf::createObjectFile(MemoryBufferRef mb, StringRef archiveName, 16440b57cec5SDimitry Andric uint64_t offsetInArchive) { 16450b57cec5SDimitry Andric if (isBitcode(mb)) 16460b57cec5SDimitry Andric return make<BitcodeFile>(mb, archiveName, offsetInArchive); 16470b57cec5SDimitry Andric 16480b57cec5SDimitry Andric switch (getELFKind(mb, archiveName)) { 16490b57cec5SDimitry Andric case ELF32LEKind: 16500b57cec5SDimitry Andric return make<ObjFile<ELF32LE>>(mb, archiveName); 16510b57cec5SDimitry Andric case ELF32BEKind: 16520b57cec5SDimitry Andric return make<ObjFile<ELF32BE>>(mb, archiveName); 16530b57cec5SDimitry Andric case ELF64LEKind: 16540b57cec5SDimitry Andric return make<ObjFile<ELF64LE>>(mb, archiveName); 16550b57cec5SDimitry Andric case ELF64BEKind: 16560b57cec5SDimitry Andric return make<ObjFile<ELF64BE>>(mb, archiveName); 16570b57cec5SDimitry Andric default: 16580b57cec5SDimitry Andric llvm_unreachable("getELFKind"); 16590b57cec5SDimitry Andric } 16600b57cec5SDimitry Andric } 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric void LazyObjFile::fetch() { 1663*5ffd83dbSDimitry Andric if (fetched) 16640b57cec5SDimitry Andric return; 1665*5ffd83dbSDimitry Andric fetched = true; 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric InputFile *file = createObjectFile(mb, archiveName, offsetInArchive); 16680b57cec5SDimitry Andric file->groupId = groupId; 16690b57cec5SDimitry Andric 16700b57cec5SDimitry Andric // Copy symbol vector so that the new InputFile doesn't have to 16710b57cec5SDimitry Andric // insert the same defined symbols to the symbol table again. 16720b57cec5SDimitry Andric file->symbols = std::move(symbols); 16730b57cec5SDimitry Andric 16740b57cec5SDimitry Andric parseFile(file); 16750b57cec5SDimitry Andric } 16760b57cec5SDimitry Andric 16770b57cec5SDimitry Andric template <class ELFT> void LazyObjFile::parse() { 16780b57cec5SDimitry Andric using Elf_Sym = typename ELFT::Sym; 16790b57cec5SDimitry Andric 16800b57cec5SDimitry Andric // A lazy object file wraps either a bitcode file or an ELF file. 16810b57cec5SDimitry Andric if (isBitcode(this->mb)) { 16820b57cec5SDimitry Andric std::unique_ptr<lto::InputFile> obj = 16830b57cec5SDimitry Andric CHECK(lto::InputFile::create(this->mb), this); 16840b57cec5SDimitry Andric for (const lto::InputFile::Symbol &sym : obj->symbols()) { 16850b57cec5SDimitry Andric if (sym.isUndefined()) 16860b57cec5SDimitry Andric continue; 16870b57cec5SDimitry Andric symtab->addSymbol(LazyObject{*this, saver.save(sym.getName())}); 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric return; 16900b57cec5SDimitry Andric } 16910b57cec5SDimitry Andric 16920b57cec5SDimitry Andric if (getELFKind(this->mb, archiveName) != config->ekind) { 16930b57cec5SDimitry Andric error("incompatible file: " + this->mb.getBufferIdentifier()); 16940b57cec5SDimitry Andric return; 16950b57cec5SDimitry Andric } 16960b57cec5SDimitry Andric 16970b57cec5SDimitry Andric // Find a symbol table. 16980b57cec5SDimitry Andric ELFFile<ELFT> obj = check(ELFFile<ELFT>::create(mb.getBuffer())); 16990b57cec5SDimitry Andric ArrayRef<typename ELFT::Shdr> sections = CHECK(obj.sections(), this); 17000b57cec5SDimitry Andric 17010b57cec5SDimitry Andric for (const typename ELFT::Shdr &sec : sections) { 17020b57cec5SDimitry Andric if (sec.sh_type != SHT_SYMTAB) 17030b57cec5SDimitry Andric continue; 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andric // A symbol table is found. 17060b57cec5SDimitry Andric ArrayRef<Elf_Sym> eSyms = CHECK(obj.symbols(&sec), this); 17070b57cec5SDimitry Andric uint32_t firstGlobal = sec.sh_info; 17080b57cec5SDimitry Andric StringRef strtab = CHECK(obj.getStringTableForSymtab(sec, sections), this); 17090b57cec5SDimitry Andric this->symbols.resize(eSyms.size()); 17100b57cec5SDimitry Andric 17110b57cec5SDimitry Andric // Get existing symbols or insert placeholder symbols. 17120b57cec5SDimitry Andric for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) 17130b57cec5SDimitry Andric if (eSyms[i].st_shndx != SHN_UNDEF) 17140b57cec5SDimitry Andric this->symbols[i] = symtab->insert(CHECK(eSyms[i].getName(strtab), this)); 17150b57cec5SDimitry Andric 17160b57cec5SDimitry Andric // Replace existing symbols with LazyObject symbols. 17170b57cec5SDimitry Andric // 17180b57cec5SDimitry Andric // resolve() may trigger this->fetch() if an existing symbol is an 17190b57cec5SDimitry Andric // undefined symbol. If that happens, this LazyObjFile has served 17200b57cec5SDimitry Andric // its purpose, and we can exit from the loop early. 17210b57cec5SDimitry Andric for (Symbol *sym : this->symbols) { 17220b57cec5SDimitry Andric if (!sym) 17230b57cec5SDimitry Andric continue; 17240b57cec5SDimitry Andric sym->resolve(LazyObject{*this, sym->getName()}); 17250b57cec5SDimitry Andric 1726*5ffd83dbSDimitry Andric // If fetched, stop iterating because this->symbols has been transferred 1727*5ffd83dbSDimitry Andric // to the instantiated ObjFile. 1728*5ffd83dbSDimitry Andric if (fetched) 17290b57cec5SDimitry Andric return; 17300b57cec5SDimitry Andric } 17310b57cec5SDimitry Andric return; 17320b57cec5SDimitry Andric } 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric 1735*5ffd83dbSDimitry Andric std::string elf::replaceThinLTOSuffix(StringRef path) { 17360b57cec5SDimitry Andric StringRef suffix = config->thinLTOObjectSuffixReplace.first; 17370b57cec5SDimitry Andric StringRef repl = config->thinLTOObjectSuffixReplace.second; 17380b57cec5SDimitry Andric 17390b57cec5SDimitry Andric if (path.consume_back(suffix)) 17400b57cec5SDimitry Andric return (path + repl).str(); 1741*5ffd83dbSDimitry Andric return std::string(path); 17420b57cec5SDimitry Andric } 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric template void BitcodeFile::parse<ELF32LE>(); 17450b57cec5SDimitry Andric template void BitcodeFile::parse<ELF32BE>(); 17460b57cec5SDimitry Andric template void BitcodeFile::parse<ELF64LE>(); 17470b57cec5SDimitry Andric template void BitcodeFile::parse<ELF64BE>(); 17480b57cec5SDimitry Andric 17490b57cec5SDimitry Andric template void LazyObjFile::parse<ELF32LE>(); 17500b57cec5SDimitry Andric template void LazyObjFile::parse<ELF32BE>(); 17510b57cec5SDimitry Andric template void LazyObjFile::parse<ELF64LE>(); 17520b57cec5SDimitry Andric template void LazyObjFile::parse<ELF64BE>(); 17530b57cec5SDimitry Andric 1754*5ffd83dbSDimitry Andric template class elf::ObjFile<ELF32LE>; 1755*5ffd83dbSDimitry Andric template class elf::ObjFile<ELF32BE>; 1756*5ffd83dbSDimitry Andric template class elf::ObjFile<ELF64LE>; 1757*5ffd83dbSDimitry Andric template class elf::ObjFile<ELF64BE>; 17580b57cec5SDimitry Andric 17590b57cec5SDimitry Andric template void SharedFile::parse<ELF32LE>(); 17600b57cec5SDimitry Andric template void SharedFile::parse<ELF32BE>(); 17610b57cec5SDimitry Andric template void SharedFile::parse<ELF64LE>(); 17620b57cec5SDimitry Andric template void SharedFile::parse<ELF64BE>(); 1763