2097 lines · cpp
1//===- InputFiles.cpp -----------------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "InputFiles.h"10#include "Config.h"11#include "DWARF.h"12#include "Driver.h"13#include "InputSection.h"14#include "LinkerScript.h"15#include "SymbolTable.h"16#include "Symbols.h"17#include "SyntheticSections.h"18#include "Target.h"19#include "lld/Common/DWARF.h"20#include "llvm/ADT/CachedHashString.h"21#include "llvm/ADT/STLExtras.h"22#include "llvm/LTO/LTO.h"23#include "llvm/Object/Archive.h"24#include "llvm/Object/IRObjectFile.h"25#include "llvm/Support/AArch64AttributeParser.h"26#include "llvm/Support/ARMAttributeParser.h"27#include "llvm/Support/ARMBuildAttributes.h"28#include "llvm/Support/Endian.h"29#include "llvm/Support/FileSystem.h"30#include "llvm/Support/Path.h"31#include "llvm/Support/TimeProfiler.h"32#include "llvm/Support/raw_ostream.h"33#include <optional>34 35using namespace llvm;36using namespace llvm::ELF;37using namespace llvm::object;38using namespace llvm::sys;39using namespace llvm::sys::fs;40using namespace llvm::support::endian;41using namespace lld;42using namespace lld::elf;43 44// This function is explicitly instantiated in ARM.cpp, don't do it here to45// avoid warnings with MSVC.46extern template void ObjFile<ELF32LE>::importCmseSymbols();47extern template void ObjFile<ELF32BE>::importCmseSymbols();48extern template void ObjFile<ELF64LE>::importCmseSymbols();49extern template void ObjFile<ELF64BE>::importCmseSymbols();50 51// Returns "<internal>", "foo.a(bar.o)" or "baz.o".52std::string elf::toStr(Ctx &ctx, const InputFile *f) {53 static std::mutex mu;54 if (!f)55 return "<internal>";56 57 {58 std::lock_guard<std::mutex> lock(mu);59 if (f->toStringCache.empty()) {60 if (f->archiveName.empty())61 f->toStringCache = f->getName();62 else63 (f->archiveName + "(" + f->getName() + ")").toVector(f->toStringCache);64 }65 }66 return std::string(f->toStringCache);67}68 69const ELFSyncStream &elf::operator<<(const ELFSyncStream &s,70 const InputFile *f) {71 return s << toStr(s.ctx, f);72}73 74static ELFKind getELFKind(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName) {75 unsigned char size;76 unsigned char endian;77 std::tie(size, endian) = getElfArchType(mb.getBuffer());78 79 auto report = [&](StringRef msg) {80 StringRef filename = mb.getBufferIdentifier();81 if (archiveName.empty())82 Fatal(ctx) << filename << ": " << msg;83 else84 Fatal(ctx) << archiveName << "(" << filename << "): " << msg;85 };86 87 if (!mb.getBuffer().starts_with(ElfMagic))88 report("not an ELF file");89 if (endian != ELFDATA2LSB && endian != ELFDATA2MSB)90 report("corrupted ELF file: invalid data encoding");91 if (size != ELFCLASS32 && size != ELFCLASS64)92 report("corrupted ELF file: invalid file class");93 94 size_t bufSize = mb.getBuffer().size();95 if ((size == ELFCLASS32 && bufSize < sizeof(Elf32_Ehdr)) ||96 (size == ELFCLASS64 && bufSize < sizeof(Elf64_Ehdr)))97 report("corrupted ELF file: file is too short");98 99 if (size == ELFCLASS32)100 return (endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;101 return (endian == ELFDATA2LSB) ? ELF64LEKind : ELF64BEKind;102}103 104// For ARM only, to set the EF_ARM_ABI_FLOAT_SOFT or EF_ARM_ABI_FLOAT_HARD105// flag in the ELF Header we need to look at Tag_ABI_VFP_args to find out how106// the input objects have been compiled.107static void updateARMVFPArgs(Ctx &ctx, const ARMAttributeParser &attributes,108 const InputFile *f) {109 std::optional<unsigned> attr =110 attributes.getAttributeValue(ARMBuildAttrs::ABI_VFP_args);111 if (!attr)112 // If an ABI tag isn't present then it is implicitly given the value of 0113 // which maps to ARMBuildAttrs::BaseAAPCS. However many assembler files,114 // including some in glibc that don't use FP args (and should have value 3)115 // don't have the attribute so we do not consider an implicit value of 0116 // as a clash.117 return;118 119 unsigned vfpArgs = *attr;120 ARMVFPArgKind arg;121 switch (vfpArgs) {122 case ARMBuildAttrs::BaseAAPCS:123 arg = ARMVFPArgKind::Base;124 break;125 case ARMBuildAttrs::HardFPAAPCS:126 arg = ARMVFPArgKind::VFP;127 break;128 case ARMBuildAttrs::ToolChainFPPCS:129 // Tool chain specific convention that conforms to neither AAPCS variant.130 arg = ARMVFPArgKind::ToolChain;131 break;132 case ARMBuildAttrs::CompatibleFPAAPCS:133 // Object compatible with all conventions.134 return;135 default:136 ErrAlways(ctx) << f << ": unknown Tag_ABI_VFP_args value: " << vfpArgs;137 return;138 }139 // Follow ld.bfd and error if there is a mix of calling conventions.140 if (ctx.arg.armVFPArgs != arg && ctx.arg.armVFPArgs != ARMVFPArgKind::Default)141 ErrAlways(ctx) << f << ": incompatible Tag_ABI_VFP_args";142 else143 ctx.arg.armVFPArgs = arg;144}145 146// The ARM support in lld makes some use of instructions that are not available147// on all ARM architectures. Namely:148// - Use of BLX instruction for interworking between ARM and Thumb state.149// - Use of the extended Thumb branch encoding in relocation.150// - Use of the MOVT/MOVW instructions in Thumb Thunks.151// The ARM Attributes section contains information about the architecture chosen152// at compile time. We follow the convention that if at least one input object153// is compiled with an architecture that supports these features then lld is154// permitted to use them.155static void updateSupportedARMFeatures(Ctx &ctx,156 const ARMAttributeParser &attributes) {157 std::optional<unsigned> attr =158 attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);159 if (!attr)160 return;161 auto arch = *attr;162 switch (arch) {163 case ARMBuildAttrs::Pre_v4:164 case ARMBuildAttrs::v4:165 case ARMBuildAttrs::v4T:166 // Architectures prior to v5 do not support BLX instruction167 break;168 case ARMBuildAttrs::v5T:169 case ARMBuildAttrs::v5TE:170 case ARMBuildAttrs::v5TEJ:171 case ARMBuildAttrs::v6:172 case ARMBuildAttrs::v6KZ:173 case ARMBuildAttrs::v6K:174 ctx.arg.armHasBlx = true;175 // Architectures used in pre-Cortex processors do not support176 // The J1 = 1 J2 = 1 Thumb branch range extension, with the exception177 // of Architecture v6T2 (arm1156t2-s and arm1156t2f-s) that do.178 break;179 default:180 // All other Architectures have BLX and extended branch encoding181 ctx.arg.armHasBlx = true;182 ctx.arg.armJ1J2BranchEncoding = true;183 if (arch != ARMBuildAttrs::v6_M && arch != ARMBuildAttrs::v6S_M)184 // All Architectures used in Cortex processors with the exception185 // of v6-M and v6S-M have the MOVT and MOVW instructions.186 ctx.arg.armHasMovtMovw = true;187 break;188 }189 190 // Only ARMv8-M or later architectures have CMSE support.191 std::optional<unsigned> profile =192 attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);193 if (!profile)194 return;195 if (arch >= ARMBuildAttrs::CPUArch::v8_M_Base &&196 profile == ARMBuildAttrs::MicroControllerProfile)197 ctx.arg.armCMSESupport = true;198 199 // The thumb PLT entries require Thumb2 which can be used on multiple archs.200 // For now, let's limit it to ones where ARM isn't available and we know have201 // Thumb2.202 std::optional<unsigned> armISA =203 attributes.getAttributeValue(ARMBuildAttrs::ARM_ISA_use);204 std::optional<unsigned> thumb =205 attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);206 ctx.arg.armHasArmISA |= armISA && *armISA >= ARMBuildAttrs::Allowed;207 ctx.arg.armHasThumb2ISA |= thumb && *thumb >= ARMBuildAttrs::AllowThumb32;208}209 210InputFile::InputFile(Ctx &ctx, Kind k, MemoryBufferRef m)211 : ctx(ctx), mb(m), groupId(ctx.driver.nextGroupId), fileKind(k) {212 // All files within the same --{start,end}-group get the same group ID.213 // Otherwise, a new file will get a new group ID.214 if (!ctx.driver.isInGroup)215 ++ctx.driver.nextGroupId;216}217 218InputFile::~InputFile() {}219 220std::optional<MemoryBufferRef> elf::readFile(Ctx &ctx, StringRef path) {221 llvm::TimeTraceScope timeScope("Load input files", path);222 223 // The --chroot option changes our virtual root directory.224 // This is useful when you are dealing with files created by --reproduce.225 if (!ctx.arg.chroot.empty() && path.starts_with("/"))226 path = ctx.saver.save(ctx.arg.chroot + path);227 228 bool remapped = false;229 auto it = ctx.arg.remapInputs.find(path);230 if (it != ctx.arg.remapInputs.end()) {231 path = it->second;232 remapped = true;233 } else {234 for (const auto &[pat, toFile] : ctx.arg.remapInputsWildcards) {235 if (pat.match(path)) {236 path = toFile;237 remapped = true;238 break;239 }240 }241 }242 if (remapped) {243 // Use /dev/null to indicate an input file that should be ignored. Change244 // the path to NUL on Windows.245#ifdef _WIN32246 if (path == "/dev/null")247 path = "NUL";248#endif249 }250 251 Log(ctx) << path;252 ctx.arg.dependencyFiles.insert(llvm::CachedHashString(path));253 254 auto mbOrErr = MemoryBuffer::getFile(path, /*IsText=*/false,255 /*RequiresNullTerminator=*/false);256 if (auto ec = mbOrErr.getError()) {257 ErrAlways(ctx) << "cannot open " << path << ": " << ec.message();258 return std::nullopt;259 }260 261 MemoryBufferRef mbref = (*mbOrErr)->getMemBufferRef();262 ctx.memoryBuffers.push_back(std::move(*mbOrErr)); // take MB ownership263 264 if (ctx.tar)265 ctx.tar->append(relativeToRoot(path), mbref.getBuffer());266 return mbref;267}268 269// All input object files must be for the same architecture270// (e.g. it does not make sense to link x86 object files with271// MIPS object files.) This function checks for that error.272static bool isCompatible(Ctx &ctx, InputFile *file) {273 if (!file->isElf() && !isa<BitcodeFile>(file))274 return true;275 276 if (file->ekind == ctx.arg.ekind && file->emachine == ctx.arg.emachine) {277 if (ctx.arg.emachine != EM_MIPS)278 return true;279 if (isMipsN32Abi(ctx, *file) == ctx.arg.mipsN32Abi)280 return true;281 }282 283 StringRef target =284 !ctx.arg.bfdname.empty() ? ctx.arg.bfdname : ctx.arg.emulation;285 if (!target.empty()) {286 Err(ctx) << file << " is incompatible with " << target;287 return false;288 }289 290 InputFile *existing = nullptr;291 if (!ctx.objectFiles.empty())292 existing = ctx.objectFiles[0];293 else if (!ctx.sharedFiles.empty())294 existing = ctx.sharedFiles[0];295 else if (!ctx.bitcodeFiles.empty())296 existing = ctx.bitcodeFiles[0];297 auto diag = Err(ctx);298 diag << file << " is incompatible";299 if (existing)300 diag << " with " << existing;301 return false;302}303 304template <class ELFT> static void doParseFile(Ctx &ctx, InputFile *file) {305 if (!isCompatible(ctx, file))306 return;307 308 // Lazy object file309 if (file->lazy) {310 if (auto *f = dyn_cast<BitcodeFile>(file)) {311 ctx.lazyBitcodeFiles.push_back(f);312 f->parseLazy();313 } else {314 cast<ObjFile<ELFT>>(file)->parseLazy();315 }316 return;317 }318 319 if (ctx.arg.trace)320 Msg(ctx) << file;321 322 if (file->kind() == InputFile::ObjKind) {323 ctx.objectFiles.push_back(cast<ELFFileBase>(file));324 cast<ObjFile<ELFT>>(file)->parse();325 } else if (auto *f = dyn_cast<SharedFile>(file)) {326 f->parse<ELFT>();327 } else if (auto *f = dyn_cast<BitcodeFile>(file)) {328 ctx.bitcodeFiles.push_back(f);329 f->parse();330 } else {331 ctx.binaryFiles.push_back(cast<BinaryFile>(file));332 cast<BinaryFile>(file)->parse();333 }334}335 336// Add symbols in File to the symbol table.337void elf::parseFile(Ctx &ctx, InputFile *file) {338 invokeELFT(doParseFile, ctx, file);339}340 341// This function is explicitly instantiated in ARM.cpp. Mark it extern here,342// to avoid warnings when building with MSVC.343extern template void ObjFile<ELF32LE>::importCmseSymbols();344extern template void ObjFile<ELF32BE>::importCmseSymbols();345extern template void ObjFile<ELF64LE>::importCmseSymbols();346extern template void ObjFile<ELF64BE>::importCmseSymbols();347 348template <class ELFT>349static void350doParseFiles(Ctx &ctx,351 const SmallVector<std::unique_ptr<InputFile>, 0> &files) {352 // Add all files to the symbol table. This will add almost all symbols that we353 // need to the symbol table. This process might add files to the link due to354 // addDependentLibrary.355 for (size_t i = 0; i < files.size(); ++i) {356 llvm::TimeTraceScope timeScope("Parse input files", files[i]->getName());357 doParseFile<ELFT>(ctx, files[i].get());358 }359 if (ctx.driver.armCmseImpLib)360 cast<ObjFile<ELFT>>(*ctx.driver.armCmseImpLib).importCmseSymbols();361}362 363void elf::parseFiles(Ctx &ctx,364 const SmallVector<std::unique_ptr<InputFile>, 0> &files) {365 llvm::TimeTraceScope timeScope("Parse input files");366 invokeELFT(doParseFiles, ctx, files);367}368 369// Concatenates arguments to construct a string representing an error location.370StringRef InputFile::getNameForScript() const {371 if (archiveName.empty())372 return getName();373 374 if (nameForScriptCache.empty())375 nameForScriptCache = (archiveName + Twine(':') + getName()).str();376 377 return nameForScriptCache;378}379 380// An ELF object file may contain a `.deplibs` section. If it exists, the381// section contains a list of library specifiers such as `m` for libm. This382// function resolves a given name by finding the first matching library checking383// the various ways that a library can be specified to LLD. This ELF extension384// is a form of autolinking and is called `dependent libraries`. It is currently385// unique to LLVM and lld.386static void addDependentLibrary(Ctx &ctx, StringRef specifier,387 const InputFile *f) {388 if (!ctx.arg.dependentLibraries)389 return;390 if (std::optional<std::string> s = searchLibraryBaseName(ctx, specifier))391 ctx.driver.addFile(ctx.saver.save(*s), /*withLOption=*/true);392 else if (std::optional<std::string> s = findFromSearchPaths(ctx, specifier))393 ctx.driver.addFile(ctx.saver.save(*s), /*withLOption=*/true);394 else if (fs::exists(specifier))395 ctx.driver.addFile(specifier, /*withLOption=*/false);396 else397 ErrAlways(ctx)398 << f << ": unable to find library from dependent library specifier: "399 << specifier;400}401 402// Record the membership of a section group so that in the garbage collection403// pass, section group members are kept or discarded as a unit.404template <class ELFT>405static void handleSectionGroup(ArrayRef<InputSectionBase *> sections,406 ArrayRef<typename ELFT::Word> entries) {407 bool hasAlloc = false;408 for (uint32_t index : entries.slice(1)) {409 if (index >= sections.size())410 return;411 if (InputSectionBase *s = sections[index])412 if (s != &InputSection::discarded && s->flags & SHF_ALLOC)413 hasAlloc = true;414 }415 416 // If any member has the SHF_ALLOC flag, the whole group is subject to garbage417 // collection. See the comment in markLive(). This rule retains .debug_types418 // and .rela.debug_types.419 if (!hasAlloc)420 return;421 422 // Connect the members in a circular doubly-linked list via423 // nextInSectionGroup.424 InputSectionBase *head;425 InputSectionBase *prev = nullptr;426 for (uint32_t index : entries.slice(1)) {427 InputSectionBase *s = sections[index];428 if (!s || s == &InputSection::discarded)429 continue;430 if (prev)431 prev->nextInSectionGroup = s;432 else433 head = s;434 prev = s;435 }436 if (prev)437 prev->nextInSectionGroup = head;438}439 440template <class ELFT> void ObjFile<ELFT>::initDwarf() {441 dwarf = std::make_unique<DWARFCache>(std::make_unique<DWARFContext>(442 std::make_unique<LLDDwarfObj<ELFT>>(this), "",443 [&](Error err) { Warn(ctx) << getName() + ": " << std::move(err); },444 [&](Error warning) {445 Warn(ctx) << getName() << ": " << std::move(warning);446 }));447}448 449DWARFCache *ELFFileBase::getDwarf() {450 assert(fileKind == ObjKind);451 llvm::call_once(initDwarf, [this]() {452 switch (ekind) {453 default:454 llvm_unreachable("");455 case ELF32LEKind:456 return cast<ObjFile<ELF32LE>>(this)->initDwarf();457 case ELF32BEKind:458 return cast<ObjFile<ELF32BE>>(this)->initDwarf();459 case ELF64LEKind:460 return cast<ObjFile<ELF64LE>>(this)->initDwarf();461 case ELF64BEKind:462 return cast<ObjFile<ELF64BE>>(this)->initDwarf();463 }464 });465 return dwarf.get();466}467 468ELFFileBase::ELFFileBase(Ctx &ctx, Kind k, ELFKind ekind, MemoryBufferRef mb)469 : InputFile(ctx, k, mb) {470 this->ekind = ekind;471}472 473ELFFileBase::~ELFFileBase() {}474 475template <typename Elf_Shdr>476static const Elf_Shdr *findSection(ArrayRef<Elf_Shdr> sections, uint32_t type) {477 for (const Elf_Shdr &sec : sections)478 if (sec.sh_type == type)479 return &sec;480 return nullptr;481}482 483void ELFFileBase::init() {484 switch (ekind) {485 case ELF32LEKind:486 init<ELF32LE>(fileKind);487 break;488 case ELF32BEKind:489 init<ELF32BE>(fileKind);490 break;491 case ELF64LEKind:492 init<ELF64LE>(fileKind);493 break;494 case ELF64BEKind:495 init<ELF64BE>(fileKind);496 break;497 default:498 llvm_unreachable("getELFKind");499 }500}501 502template <class ELFT> void ELFFileBase::init(InputFile::Kind k) {503 using Elf_Shdr = typename ELFT::Shdr;504 using Elf_Sym = typename ELFT::Sym;505 506 // Initialize trivial attributes.507 const ELFFile<ELFT> &obj = getObj<ELFT>();508 emachine = obj.getHeader().e_machine;509 osabi = obj.getHeader().e_ident[llvm::ELF::EI_OSABI];510 abiVersion = obj.getHeader().e_ident[llvm::ELF::EI_ABIVERSION];511 512 ArrayRef<Elf_Shdr> sections = CHECK2(obj.sections(), this);513 elfShdrs = sections.data();514 numELFShdrs = sections.size();515 516 // Find a symbol table.517 const Elf_Shdr *symtabSec =518 findSection(sections, k == SharedKind ? SHT_DYNSYM : SHT_SYMTAB);519 520 if (!symtabSec)521 return;522 523 // Initialize members corresponding to a symbol table.524 firstGlobal = symtabSec->sh_info;525 526 ArrayRef<Elf_Sym> eSyms = CHECK2(obj.symbols(symtabSec), this);527 if (firstGlobal == 0 || firstGlobal > eSyms.size())528 Fatal(ctx) << this << ": invalid sh_info in symbol table";529 530 elfSyms = reinterpret_cast<const void *>(eSyms.data());531 numSymbols = eSyms.size();532 stringTable = CHECK2(obj.getStringTableForSymtab(*symtabSec, sections), this);533}534 535template <class ELFT>536uint32_t ObjFile<ELFT>::getSectionIndex(const Elf_Sym &sym) const {537 return CHECK2(538 this->getObj().getSectionIndex(sym, getELFSyms<ELFT>(), shndxTable),539 this);540}541 542template <class ELFT>543static void544handleAArch64BAAndGnuProperties(ObjFile<ELFT> *file, Ctx &ctx,545 const AArch64BuildAttrSubsections &baInfo) {546 if (file->aarch64PauthAbiCoreInfo) {547 // Check for data mismatch.548 if (file->aarch64PauthAbiCoreInfo) {549 if (baInfo.Pauth.TagPlatform != file->aarch64PauthAbiCoreInfo->platform ||550 baInfo.Pauth.TagSchema != file->aarch64PauthAbiCoreInfo->version)551 Err(ctx) << file552 << " GNU properties and build attributes have conflicting "553 "AArch64 PAuth data";554 }555 if (baInfo.AndFeatures != file->andFeatures)556 Err(ctx) << file557 << " GNU properties and build attributes have conflicting "558 "AArch64 PAuth data";559 } else {560 // When BuildAttributes are missing, PauthABI value defaults to (TagPlatform561 // = 0, TagSchema = 0). GNU properties do not write PAuthAbiCoreInfo if GNU562 // property is not present. To match this behaviour, we only write563 // PAuthAbiCoreInfo when there is at least one non-zero value. The564 // specification reserves TagPlatform = 0, TagSchema = 1 values to match the565 // 'Invalid' GNU property section with platform = 0, version = 0.566 if (baInfo.Pauth.TagPlatform || baInfo.Pauth.TagSchema) {567 if (baInfo.Pauth.TagPlatform == 0 && baInfo.Pauth.TagSchema == 1)568 file->aarch64PauthAbiCoreInfo = {0, 0};569 else570 file->aarch64PauthAbiCoreInfo = {baInfo.Pauth.TagPlatform,571 baInfo.Pauth.TagSchema};572 }573 file->andFeatures = baInfo.AndFeatures;574 }575}576 577template <class ELFT> void ObjFile<ELFT>::parse(bool ignoreComdats) {578 object::ELFFile<ELFT> obj = this->getObj();579 // Read a section table. justSymbols is usually false.580 if (this->justSymbols) {581 initializeJustSymbols();582 initializeSymbols(obj);583 return;584 }585 586 // Handle dependent libraries and selection of section groups as these are not587 // done in parallel.588 ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();589 StringRef shstrtab = CHECK2(obj.getSectionStringTable(objSections), this);590 uint64_t size = objSections.size();591 sections.resize(size);592 for (size_t i = 0; i != size; ++i) {593 const Elf_Shdr &sec = objSections[i];594 595 if (LLVM_LIKELY(sec.sh_type == SHT_PROGBITS))596 continue;597 if (LLVM_LIKELY(sec.sh_type == SHT_GROUP)) {598 StringRef signature = getShtGroupSignature(objSections, sec);599 ArrayRef<Elf_Word> entries =600 CHECK2(obj.template getSectionContentsAsArray<Elf_Word>(sec), this);601 if (entries.empty())602 Fatal(ctx) << this << ": empty SHT_GROUP";603 604 Elf_Word flag = entries[0];605 if (flag && flag != GRP_COMDAT)606 Fatal(ctx) << this << ": unsupported SHT_GROUP format";607 608 bool keepGroup = !flag || ignoreComdats ||609 ctx.symtab->comdatGroups610 .try_emplace(CachedHashStringRef(signature), this)611 .second;612 if (keepGroup) {613 if (!ctx.arg.resolveGroups)614 sections[i] = createInputSection(615 i, sec, check(obj.getSectionName(sec, shstrtab)));616 } else {617 // Otherwise, discard group members.618 for (uint32_t secIndex : entries.slice(1)) {619 if (secIndex >= size)620 Fatal(ctx) << this621 << ": invalid section index in group: " << secIndex;622 sections[secIndex] = &InputSection::discarded;623 }624 }625 continue;626 }627 628 if (sec.sh_type == SHT_LLVM_DEPENDENT_LIBRARIES && !ctx.arg.relocatable) {629 StringRef name = check(obj.getSectionName(sec, shstrtab));630 ArrayRef<char> data = CHECK2(631 this->getObj().template getSectionContentsAsArray<char>(sec), this);632 if (!data.empty() && data.back() != '\0') {633 Err(ctx)634 << this635 << ": corrupted dependent libraries section (unterminated string): "636 << name;637 } else {638 for (const char *d = data.begin(), *e = data.end(); d < e;) {639 StringRef s(d);640 addDependentLibrary(ctx, s, this);641 d += s.size() + 1;642 }643 }644 sections[i] = &InputSection::discarded;645 continue;646 }647 648 switch (ctx.arg.emachine) {649 case EM_ARM:650 if (sec.sh_type == SHT_ARM_ATTRIBUTES) {651 ARMAttributeParser attributes;652 ArrayRef<uint8_t> contents =653 check(this->getObj().getSectionContents(sec));654 StringRef name = check(obj.getSectionName(sec, shstrtab));655 sections[i] = &InputSection::discarded;656 if (Error e = attributes.parse(contents, ekind == ELF32LEKind657 ? llvm::endianness::little658 : llvm::endianness::big)) {659 InputSection isec(*this, sec, name);660 Warn(ctx) << &isec << ": " << std::move(e);661 } else {662 updateSupportedARMFeatures(ctx, attributes);663 updateARMVFPArgs(ctx, attributes, this);664 665 // FIXME: Retain the first attribute section we see. The eglibc ARM666 // dynamic loaders require the presence of an attribute section for667 // dlopen to work. In a full implementation we would merge all668 // attribute sections.669 if (ctx.in.attributes == nullptr) {670 ctx.in.attributes =671 std::make_unique<InputSection>(*this, sec, name);672 sections[i] = ctx.in.attributes.get();673 }674 }675 }676 break;677 case EM_AARCH64:678 // Producing a static binary with MTE globals is not currently supported,679 // remove all SHT_AARCH64_MEMTAG_GLOBALS_STATIC sections as they're unused680 // medatada, and we don't want them to end up in the output file for681 // static executables.682 if (sec.sh_type == SHT_AARCH64_MEMTAG_GLOBALS_STATIC &&683 !canHaveMemtagGlobals(ctx))684 sections[i] = &InputSection::discarded;685 break;686 }687 }688 689 // Read a symbol table.690 initializeSymbols(obj);691}692 693// Sections with SHT_GROUP and comdat bits define comdat section groups.694// They are identified and deduplicated by group name. This function695// returns a group name.696template <class ELFT>697StringRef ObjFile<ELFT>::getShtGroupSignature(ArrayRef<Elf_Shdr> sections,698 const Elf_Shdr &sec) {699 typename ELFT::SymRange symbols = this->getELFSyms<ELFT>();700 if (sec.sh_info >= symbols.size())701 Fatal(ctx) << this << ": invalid symbol index";702 const typename ELFT::Sym &sym = symbols[sec.sh_info];703 return CHECK2(sym.getName(this->stringTable), this);704}705 706template <class ELFT>707bool ObjFile<ELFT>::shouldMerge(const Elf_Shdr &sec, StringRef name) {708 // On a regular link we don't merge sections if -O0 (default is -O1). This709 // sometimes makes the linker significantly faster, although the output will710 // be bigger.711 //712 // Doing the same for -r would create a problem as it would combine sections713 // with different sh_entsize. One option would be to just copy every SHF_MERGE714 // section as is to the output. While this would produce a valid ELF file with715 // usable SHF_MERGE sections, tools like (llvm-)?dwarfdump get confused when716 // they see two .debug_str. We could have separate logic for combining717 // SHF_MERGE sections based both on their name and sh_entsize, but that seems718 // to be more trouble than it is worth. Instead, we just use the regular (-O1)719 // logic for -r.720 if (ctx.arg.optimize == 0 && !ctx.arg.relocatable)721 return false;722 723 // A mergeable section with size 0 is useless because they don't have724 // any data to merge. A mergeable string section with size 0 can be725 // argued as invalid because it doesn't end with a null character.726 // We'll avoid a mess by handling them as if they were non-mergeable.727 if (sec.sh_size == 0)728 return false;729 730 // Check for sh_entsize. The ELF spec is not clear about the zero731 // sh_entsize. It says that "the member [sh_entsize] contains 0 if732 // the section does not hold a table of fixed-size entries". We know733 // that Rust 1.13 produces a string mergeable section with a zero734 // sh_entsize. Here we just accept it rather than being picky about it.735 uint64_t entSize = sec.sh_entsize;736 if (entSize == 0)737 return false;738 if (sec.sh_size % entSize)739 ErrAlways(ctx) << this << ":(" << name << "): SHF_MERGE section size ("740 << uint64_t(sec.sh_size)741 << ") must be a multiple of sh_entsize (" << entSize << ")";742 if (sec.sh_flags & SHF_WRITE)743 Err(ctx) << this << ":(" << name744 << "): writable SHF_MERGE section is not supported";745 746 return true;747}748 749// This is for --just-symbols.750//751// --just-symbols is a very minor feature that allows you to link your752// output against other existing program, so that if you load both your753// program and the other program into memory, your output can refer the754// other program's symbols.755//756// When the option is given, we link "just symbols". The section table is757// initialized with null pointers.758template <class ELFT> void ObjFile<ELFT>::initializeJustSymbols() {759 sections.resize(numELFShdrs);760}761 762static bool isKnownSpecificSectionType(uint32_t t, uint32_t flags) {763 if (SHT_LOUSER <= t && t <= SHT_HIUSER && !(flags & SHF_ALLOC))764 return true;765 if (SHT_LOOS <= t && t <= SHT_HIOS && !(flags & SHF_OS_NONCONFORMING))766 return true;767 // Allow all processor-specific types. This is different from GNU ld.768 return SHT_LOPROC <= t && t <= SHT_HIPROC;769}770 771template <class ELFT>772void ObjFile<ELFT>::initializeSections(bool ignoreComdats,773 const llvm::object::ELFFile<ELFT> &obj) {774 ArrayRef<Elf_Shdr> objSections = getELFShdrs<ELFT>();775 StringRef shstrtab = CHECK2(obj.getSectionStringTable(objSections), this);776 uint64_t size = objSections.size();777 SmallVector<ArrayRef<Elf_Word>, 0> selectedGroups;778 AArch64BuildAttrSubsections aarch64BAsubSections;779 bool hasAArch64BuildAttributes = false;780 for (size_t i = 0; i != size; ++i) {781 if (this->sections[i] == &InputSection::discarded)782 continue;783 const Elf_Shdr &sec = objSections[i];784 const uint32_t type = sec.sh_type;785 786 // SHF_EXCLUDE'ed sections are discarded by the linker. However,787 // if -r is given, we'll let the final link discard such sections.788 // This is compatible with GNU.789 if ((sec.sh_flags & SHF_EXCLUDE) && !ctx.arg.relocatable) {790 if (type == SHT_LLVM_CALL_GRAPH_PROFILE)791 cgProfileSectionIndex = i;792 if (type == SHT_LLVM_ADDRSIG) {793 // We ignore the address-significance table if we know that the object794 // file was created by objcopy or ld -r. This is because these tools795 // will reorder the symbols in the symbol table, invalidating the data796 // in the address-significance table, which refers to symbols by index.797 if (sec.sh_link != 0)798 this->addrsigSec = &sec;799 else if (ctx.arg.icf == ICFLevel::Safe)800 Warn(ctx) << this801 << ": --icf=safe conservatively ignores "802 "SHT_LLVM_ADDRSIG [index "803 << i804 << "] with sh_link=0 "805 "(likely created using objcopy or ld -r)";806 }807 this->sections[i] = &InputSection::discarded;808 continue;809 }810 811 // Processor-specific types that do not use the following switch statement.812 //813 // Extract Build Attributes section contents into aarch64BAsubSections.814 // Input objects may contain both build Build Attributes and GNU815 // properties. We delay processing Build Attributes until we have finished816 // reading all sections so that we can check that these are consistent.817 if (type == SHT_AARCH64_ATTRIBUTES && ctx.arg.emachine == EM_AARCH64) {818 ArrayRef<uint8_t> contents = check(obj.getSectionContents(sec));819 AArch64AttributeParser attributes;820 if (Error e = attributes.parse(contents, ELFT::Endianness)) {821 StringRef name = check(obj.getSectionName(sec, shstrtab));822 InputSection isec(*this, sec, name);823 Warn(ctx) << &isec << ": " << std::move(e);824 } else {825 aarch64BAsubSections = extractBuildAttributesSubsections(attributes);826 hasAArch64BuildAttributes = true;827 }828 this->sections[i] = &InputSection::discarded;829 continue;830 }831 switch (type) {832 case SHT_GROUP: {833 if (!ctx.arg.relocatable)834 sections[i] = &InputSection::discarded;835 StringRef signature =836 cantFail(this->getELFSyms<ELFT>()[sec.sh_info].getName(stringTable));837 ArrayRef<Elf_Word> entries =838 cantFail(obj.template getSectionContentsAsArray<Elf_Word>(sec));839 if ((entries[0] & GRP_COMDAT) == 0 || ignoreComdats ||840 ctx.symtab->comdatGroups.find(CachedHashStringRef(signature))841 ->second == this)842 selectedGroups.push_back(entries);843 break;844 }845 case SHT_SYMTAB_SHNDX:846 shndxTable = CHECK2(obj.getSHNDXTable(sec, objSections), this);847 break;848 case SHT_SYMTAB:849 case SHT_STRTAB:850 case SHT_REL:851 case SHT_RELA:852 case SHT_CREL:853 case SHT_NULL:854 break;855 case SHT_PROGBITS:856 case SHT_NOTE:857 case SHT_NOBITS:858 case SHT_INIT_ARRAY:859 case SHT_FINI_ARRAY:860 case SHT_PREINIT_ARRAY:861 this->sections[i] =862 createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));863 break;864 case SHT_LLVM_LTO:865 // Discard .llvm.lto in a relocatable link that does not use the bitcode.866 // The concatenated output does not properly reflect the linking867 // semantics. In addition, since we do not use the bitcode wrapper format,868 // the concatenated raw bitcode would be invalid.869 if (ctx.arg.relocatable && !ctx.arg.fatLTOObjects) {870 sections[i] = &InputSection::discarded;871 break;872 }873 [[fallthrough]];874 default:875 this->sections[i] =876 createInputSection(i, sec, check(obj.getSectionName(sec, shstrtab)));877 if (type == SHT_LLVM_SYMPART)878 ctx.hasSympart.store(true, std::memory_order_relaxed);879 else if (ctx.arg.rejectMismatch &&880 !isKnownSpecificSectionType(type, sec.sh_flags))881 Err(ctx) << this->sections[i] << ": unknown section type 0x"882 << Twine::utohexstr(type);883 break;884 }885 }886 887 // We have a second loop. It is used to:888 // 1) handle SHF_LINK_ORDER sections.889 // 2) create relocation sections. In some cases the section header index of a890 // relocation section may be smaller than that of the relocated section. In891 // such cases, the relocation section would attempt to reference a target892 // section that has not yet been created. For simplicity, delay creation of893 // relocation sections until now.894 for (size_t i = 0; i != size; ++i) {895 if (this->sections[i] == &InputSection::discarded)896 continue;897 const Elf_Shdr &sec = objSections[i];898 899 if (isStaticRelSecType(sec.sh_type)) {900 // Find a relocation target section and associate this section with that.901 // Target may have been discarded if it is in a different section group902 // and the group is discarded, even though it's a violation of the spec.903 // We handle that situation gracefully by discarding dangling relocation904 // sections.905 const uint32_t info = sec.sh_info;906 InputSectionBase *s = getRelocTarget(i, info);907 if (!s)908 continue;909 910 // ELF spec allows mergeable sections with relocations, but they are rare,911 // and it is in practice hard to merge such sections by contents, because912 // applying relocations at end of linking changes section contents. So, we913 // simply handle such sections as non-mergeable ones. Degrading like this914 // is acceptable because section merging is optional.915 if (auto *ms = dyn_cast<MergeInputSection>(s)) {916 s = makeThreadLocal<InputSection>(ms->file, ms->name, ms->type,917 ms->flags, ms->addralign, ms->entsize,918 ms->contentMaybeDecompress());919 sections[info] = s;920 }921 922 if (s->relSecIdx != 0)923 ErrAlways(ctx) << s924 << ": multiple relocation sections to one section are "925 "not supported";926 s->relSecIdx = i;927 928 // Relocation sections are usually removed from the output, so return929 // `nullptr` for the normal case. However, if -r or --emit-relocs is930 // specified, we need to copy them to the output. (Some post link analysis931 // tools specify --emit-relocs to obtain the information.)932 if (ctx.arg.copyRelocs) {933 auto *isec = makeThreadLocal<InputSection>(934 *this, sec, check(obj.getSectionName(sec, shstrtab)));935 // If the relocated section is discarded (due to /DISCARD/ or936 // --gc-sections), the relocation section should be discarded as well.937 s->dependentSections.push_back(isec);938 sections[i] = isec;939 }940 continue;941 }942 943 // A SHF_LINK_ORDER section with sh_link=0 is handled as if it did not have944 // the flag.945 if (!sec.sh_link || !(sec.sh_flags & SHF_LINK_ORDER))946 continue;947 948 InputSectionBase *linkSec = nullptr;949 if (sec.sh_link < size)950 linkSec = this->sections[sec.sh_link];951 if (!linkSec) {952 ErrAlways(ctx) << this953 << ": invalid sh_link index: " << uint32_t(sec.sh_link);954 continue;955 }956 957 // A SHF_LINK_ORDER section is discarded if its linked-to section is958 // discarded.959 InputSection *isec = cast<InputSection>(this->sections[i]);960 linkSec->dependentSections.push_back(isec);961 if (!isa<InputSection>(linkSec))962 ErrAlways(ctx)963 << "a section " << isec->name964 << " with SHF_LINK_ORDER should not refer a non-regular section: "965 << linkSec;966 }967 968 // Handle AArch64 Build Attributes and GNU properties:969 // - Err on mismatched values.970 // - Store missing values as GNU properties.971 if (hasAArch64BuildAttributes)972 handleAArch64BAAndGnuProperties<ELFT>(this, ctx, aarch64BAsubSections);973 974 for (ArrayRef<Elf_Word> entries : selectedGroups)975 handleSectionGroup<ELFT>(this->sections, entries);976}977 978template <typename ELFT>979static void parseGnuPropertyNote(Ctx &ctx, ELFFileBase &f,980 uint32_t featureAndType,981 ArrayRef<uint8_t> &desc, const uint8_t *base,982 ArrayRef<uint8_t> *data = nullptr) {983 auto err = [&](const uint8_t *place) -> ELFSyncStream {984 auto diag = Err(ctx);985 diag << &f << ":(" << ".note.gnu.property+0x"986 << Twine::utohexstr(place - base) << "): ";987 return diag;988 };989 990 while (!desc.empty()) {991 const uint8_t *place = desc.data();992 if (desc.size() < 8)993 return void(err(place) << "program property is too short");994 uint32_t type = read32<ELFT::Endianness>(desc.data());995 uint32_t size = read32<ELFT::Endianness>(desc.data() + 4);996 desc = desc.slice(8);997 if (desc.size() < size)998 return void(err(place) << "program property is too short");999 1000 if (type == featureAndType) {1001 // We found a FEATURE_1_AND field. There may be more than one of these1002 // in a .note.gnu.property section, for a relocatable object we1003 // accumulate the bits set.1004 if (size < 4)1005 return void(err(place) << "FEATURE_1_AND entry is too short");1006 f.andFeatures |= read32<ELFT::Endianness>(desc.data());1007 } else if (ctx.arg.emachine == EM_AARCH64 &&1008 type == GNU_PROPERTY_AARCH64_FEATURE_PAUTH) {1009 ArrayRef<uint8_t> contents = data ? *data : desc;1010 if (f.aarch64PauthAbiCoreInfo) {1011 return void(1012 err(contents.data())1013 << "multiple GNU_PROPERTY_AARCH64_FEATURE_PAUTH entries are "1014 "not supported");1015 } else if (size != 16) {1016 return void(err(contents.data())1017 << "GNU_PROPERTY_AARCH64_FEATURE_PAUTH entry "1018 "is invalid: expected 16 bytes, but got "1019 << size);1020 }1021 f.aarch64PauthAbiCoreInfo = {1022 support::endian::read64<ELFT::Endianness>(&desc[0]),1023 support::endian::read64<ELFT::Endianness>(&desc[8])};1024 }1025 1026 // Padding is present in the note descriptor, if necessary.1027 desc = desc.slice(alignTo<(ELFT::Is64Bits ? 8 : 4)>(size));1028 }1029}1030// Read the following info from the .note.gnu.property section and write it to1031// the corresponding fields in `ObjFile`:1032// - Feature flags (32 bits) representing x86, AArch64 or RISC-V features for1033// hardware-assisted call flow control;1034// - AArch64 PAuth ABI core info (16 bytes).1035template <class ELFT>1036static void readGnuProperty(Ctx &ctx, const InputSection &sec,1037 ObjFile<ELFT> &f) {1038 using Elf_Nhdr = typename ELFT::Nhdr;1039 using Elf_Note = typename ELFT::Note;1040 1041 uint32_t featureAndType;1042 switch (ctx.arg.emachine) {1043 case EM_386:1044 case EM_X86_64:1045 featureAndType = GNU_PROPERTY_X86_FEATURE_1_AND;1046 break;1047 case EM_AARCH64:1048 featureAndType = GNU_PROPERTY_AARCH64_FEATURE_1_AND;1049 break;1050 case EM_RISCV:1051 featureAndType = GNU_PROPERTY_RISCV_FEATURE_1_AND;1052 break;1053 default:1054 return;1055 }1056 1057 ArrayRef<uint8_t> data = sec.content();1058 auto err = [&](const uint8_t *place) -> ELFSyncStream {1059 auto diag = Err(ctx);1060 diag << sec.file << ":(" << sec.name << "+0x"1061 << Twine::utohexstr(place - sec.content().data()) << "): ";1062 return diag;1063 };1064 while (!data.empty()) {1065 // Read one NOTE record.1066 auto *nhdr = reinterpret_cast<const Elf_Nhdr *>(data.data());1067 if (data.size() < sizeof(Elf_Nhdr) ||1068 data.size() < nhdr->getSize(sec.addralign))1069 return void(err(data.data()) << "data is too short");1070 1071 Elf_Note note(*nhdr);1072 if (nhdr->n_type != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU") {1073 data = data.slice(nhdr->getSize(sec.addralign));1074 continue;1075 }1076 1077 // Read a body of a NOTE record, which consists of type-length-value fields.1078 ArrayRef<uint8_t> desc = note.getDesc(sec.addralign);1079 const uint8_t *base = sec.content().data();1080 parseGnuPropertyNote<ELFT>(ctx, f, featureAndType, desc, base, &data);1081 1082 // Go to next NOTE record to look for more FEATURE_1_AND descriptions.1083 data = data.slice(nhdr->getSize(sec.addralign));1084 }1085}1086 1087template <class ELFT>1088InputSectionBase *ObjFile<ELFT>::getRelocTarget(uint32_t idx, uint32_t info) {1089 if (info < this->sections.size()) {1090 InputSectionBase *target = this->sections[info];1091 1092 // Strictly speaking, a relocation section must be included in the1093 // group of the section it relocates. However, LLVM 3.3 and earlier1094 // would fail to do so, so we gracefully handle that case.1095 if (target == &InputSection::discarded)1096 return nullptr;1097 1098 if (target != nullptr)1099 return target;1100 }1101 1102 Err(ctx) << this << ": relocation section (index " << idx1103 << ") has invalid sh_info (" << info << ')';1104 return nullptr;1105}1106 1107// The function may be called concurrently for different input files. For1108// allocation, prefer makeThreadLocal which does not require holding a lock.1109template <class ELFT>1110InputSectionBase *ObjFile<ELFT>::createInputSection(uint32_t idx,1111 const Elf_Shdr &sec,1112 StringRef name) {1113 if (name.starts_with(".n")) {1114 // The GNU linker uses .note.GNU-stack section as a marker indicating1115 // that the code in the object file does not expect that the stack is1116 // executable (in terms of NX bit). If all input files have the marker,1117 // the GNU linker adds a PT_GNU_STACK segment to tells the loader to1118 // make the stack non-executable. Most object files have this section as1119 // of 2017.1120 //1121 // But making the stack non-executable is a norm today for security1122 // reasons. Failure to do so may result in a serious security issue.1123 // Therefore, we make LLD always add PT_GNU_STACK unless it is1124 // explicitly told to do otherwise (by -z execstack). Because the stack1125 // executable-ness is controlled solely by command line options,1126 // .note.GNU-stack sections are, with one exception, ignored. Report1127 // an error if we encounter an executable .note.GNU-stack to force the1128 // user to explicitly request an executable stack.1129 if (name == ".note.GNU-stack") {1130 if ((sec.sh_flags & SHF_EXECINSTR) && !ctx.arg.relocatable &&1131 ctx.arg.zGnustack != GnuStackKind::Exec) {1132 Err(ctx) << this1133 << ": requires an executable stack, but -z execstack is not "1134 "specified";1135 }1136 return &InputSection::discarded;1137 }1138 1139 // Object files that use processor features such as Intel Control-Flow1140 // Enforcement (CET), AArch64 Branch Target Identification BTI or RISC-V1141 // Zicfilp/Zicfiss extensions, use a .note.gnu.property section containing1142 // a bitfield of feature bits like the GNU_PROPERTY_X86_FEATURE_1_IBT flag.1143 //1144 // Since we merge bitmaps from multiple object files to create a new1145 // .note.gnu.property containing a single AND'ed bitmap, we discard an input1146 // file's .note.gnu.property section.1147 if (name == ".note.gnu.property") {1148 readGnuProperty<ELFT>(ctx, InputSection(*this, sec, name), *this);1149 return &InputSection::discarded;1150 }1151 1152 // Split stacks is a feature to support a discontiguous stack,1153 // commonly used in the programming language Go. For the details,1154 // see https://gcc.gnu.org/wiki/SplitStacks. An object file compiled1155 // for split stack will include a .note.GNU-split-stack section.1156 if (name == ".note.GNU-split-stack") {1157 if (ctx.arg.relocatable) {1158 ErrAlways(ctx) << "cannot mix split-stack and non-split-stack in a "1159 "relocatable link";1160 return &InputSection::discarded;1161 }1162 this->splitStack = true;1163 return &InputSection::discarded;1164 }1165 1166 // An object file compiled for split stack, but where some of the1167 // functions were compiled with the no_split_stack_attribute will1168 // include a .note.GNU-no-split-stack section.1169 if (name == ".note.GNU-no-split-stack") {1170 this->someNoSplitStack = true;1171 return &InputSection::discarded;1172 }1173 1174 // Strip existing .note.gnu.build-id sections so that the output won't have1175 // more than one build-id. This is not usually a problem because input1176 // object files normally don't have .build-id sections, but you can create1177 // such files by "ld.{bfd,gold,lld} -r --build-id", and we want to guard1178 // against it.1179 if (name == ".note.gnu.build-id")1180 return &InputSection::discarded;1181 }1182 1183 // The linker merges EH (exception handling) frames and creates a1184 // .eh_frame_hdr section for runtime. So we handle them with a special1185 // class. For relocatable outputs, they are just passed through.1186 if (name == ".eh_frame" && !ctx.arg.relocatable)1187 return makeThreadLocal<EhInputSection>(*this, sec, name);1188 1189 if ((sec.sh_flags & SHF_MERGE) && shouldMerge(sec, name))1190 return makeThreadLocal<MergeInputSection>(*this, sec, name);1191 return makeThreadLocal<InputSection>(*this, sec, name);1192}1193 1194// Initialize symbols. symbols is a parallel array to the corresponding ELF1195// symbol table.1196template <class ELFT>1197void ObjFile<ELFT>::initializeSymbols(const object::ELFFile<ELFT> &obj) {1198 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();1199 if (!symbols)1200 symbols = std::make_unique<Symbol *[]>(numSymbols);1201 1202 // Some entries have been filled by LazyObjFile.1203 auto *symtab = ctx.symtab.get();1204 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i)1205 if (!symbols[i])1206 symbols[i] = symtab->insert(CHECK2(eSyms[i].getName(stringTable), this));1207 1208 // Perform symbol resolution on non-local symbols.1209 SmallVector<unsigned, 32> undefineds;1210 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {1211 const Elf_Sym &eSym = eSyms[i];1212 uint32_t secIdx = eSym.st_shndx;1213 if (secIdx == SHN_UNDEF) {1214 undefineds.push_back(i);1215 continue;1216 }1217 1218 uint8_t binding = eSym.getBinding();1219 uint8_t stOther = eSym.st_other;1220 uint8_t type = eSym.getType();1221 uint64_t value = eSym.st_value;1222 uint64_t size = eSym.st_size;1223 1224 Symbol *sym = symbols[i];1225 sym->isUsedInRegularObj = true;1226 if (LLVM_UNLIKELY(eSym.st_shndx == SHN_COMMON)) {1227 if (value == 0 || value >= UINT32_MAX)1228 Err(ctx) << this << ": common symbol '" << sym->getName()1229 << "' has invalid alignment: " << value;1230 hasCommonSyms = true;1231 sym->resolve(ctx, CommonSymbol{ctx, this, StringRef(), binding, stOther,1232 type, value, size});1233 continue;1234 }1235 1236 // Handle global defined symbols. Defined::section will be set in postParse.1237 sym->resolve(ctx, Defined{ctx, this, StringRef(), binding, stOther, type,1238 value, size, nullptr});1239 }1240 1241 // Undefined symbols (excluding those defined relative to non-prevailing1242 // sections) can trigger recursive extract. Process defined symbols first so1243 // that the relative order between a defined symbol and an undefined symbol1244 // does not change the symbol resolution behavior. In addition, a set of1245 // interconnected symbols will all be resolved to the same file, instead of1246 // being resolved to different files.1247 for (unsigned i : undefineds) {1248 const Elf_Sym &eSym = eSyms[i];1249 Symbol *sym = symbols[i];1250 sym->resolve(ctx, Undefined{this, StringRef(), eSym.getBinding(),1251 eSym.st_other, eSym.getType()});1252 sym->isUsedInRegularObj = true;1253 sym->referenced = true;1254 }1255}1256 1257template <class ELFT>1258void ObjFile<ELFT>::initSectionsAndLocalSyms(bool ignoreComdats) {1259 if (!justSymbols)1260 initializeSections(ignoreComdats, getObj());1261 1262 if (!firstGlobal)1263 return;1264 SymbolUnion *locals = makeThreadLocalN<SymbolUnion>(firstGlobal);1265 memset(locals, 0, sizeof(SymbolUnion) * firstGlobal);1266 1267 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();1268 for (size_t i = 0, end = firstGlobal; i != end; ++i) {1269 const Elf_Sym &eSym = eSyms[i];1270 uint32_t secIdx = eSym.st_shndx;1271 if (LLVM_UNLIKELY(secIdx == SHN_XINDEX))1272 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));1273 else if (secIdx >= SHN_LORESERVE)1274 secIdx = 0;1275 if (LLVM_UNLIKELY(secIdx >= sections.size())) {1276 Err(ctx) << this << ": invalid section index: " << secIdx;1277 secIdx = 0;1278 }1279 if (LLVM_UNLIKELY(eSym.getBinding() != STB_LOCAL))1280 ErrAlways(ctx) << this << ": non-local symbol (" << i1281 << ") found at index < .symtab's sh_info (" << end << ")";1282 1283 InputSectionBase *sec = sections[secIdx];1284 uint8_t type = eSym.getType();1285 if (type == STT_FILE)1286 sourceFile = CHECK2(eSym.getName(stringTable), this);1287 unsigned stName = eSym.st_name;1288 if (LLVM_UNLIKELY(stringTable.size() <= stName)) {1289 Err(ctx) << this << ": invalid symbol name offset";1290 stName = 0;1291 }1292 StringRef name(stringTable.data() + stName);1293 1294 symbols[i] = reinterpret_cast<Symbol *>(locals + i);1295 if (eSym.st_shndx == SHN_UNDEF || sec == &InputSection::discarded)1296 new (symbols[i]) Undefined(this, name, STB_LOCAL, eSym.st_other, type,1297 /*discardedSecIdx=*/secIdx);1298 else1299 new (symbols[i]) Defined(ctx, this, name, STB_LOCAL, eSym.st_other, type,1300 eSym.st_value, eSym.st_size, sec);1301 symbols[i]->partition = 1;1302 symbols[i]->isUsedInRegularObj = true;1303 }1304}1305 1306// Called after all ObjFile::parse is called for all ObjFiles. This checks1307// duplicate symbols and may do symbol property merge in the future.1308template <class ELFT> void ObjFile<ELFT>::postParse() {1309 static std::mutex mu;1310 ArrayRef<Elf_Sym> eSyms = this->getELFSyms<ELFT>();1311 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {1312 const Elf_Sym &eSym = eSyms[i];1313 Symbol &sym = *symbols[i];1314 uint32_t secIdx = eSym.st_shndx;1315 uint8_t binding = eSym.getBinding();1316 if (LLVM_UNLIKELY(binding != STB_GLOBAL && binding != STB_WEAK &&1317 binding != STB_GNU_UNIQUE))1318 Err(ctx) << this << ": symbol (" << i1319 << ") has invalid binding: " << (int)binding;1320 1321 // st_value of STT_TLS represents the assigned offset, not the actual1322 // address which is used by STT_FUNC and STT_OBJECT. STT_TLS symbols can1323 // only be referenced by special TLS relocations. It is usually an error if1324 // a STT_TLS symbol is replaced by a non-STT_TLS symbol, vice versa.1325 if (LLVM_UNLIKELY(sym.isTls()) && eSym.getType() != STT_TLS &&1326 eSym.getType() != STT_NOTYPE)1327 Err(ctx) << "TLS attribute mismatch: " << &sym << "\n>>> in " << sym.file1328 << "\n>>> in " << this;1329 1330 // Handle non-COMMON defined symbol below. !sym.file allows a symbol1331 // assignment to redefine a symbol without an error.1332 if (!sym.isDefined() || secIdx == SHN_UNDEF)1333 continue;1334 if (LLVM_UNLIKELY(secIdx >= SHN_LORESERVE)) {1335 if (secIdx == SHN_COMMON)1336 continue;1337 if (secIdx == SHN_XINDEX)1338 secIdx = check(getExtendedSymbolTableIndex<ELFT>(eSym, i, shndxTable));1339 else1340 secIdx = 0;1341 }1342 1343 if (LLVM_UNLIKELY(secIdx >= sections.size())) {1344 Err(ctx) << this << ": invalid section index: " << secIdx;1345 continue;1346 }1347 InputSectionBase *sec = sections[secIdx];1348 if (sec == &InputSection::discarded) {1349 if (sym.traced) {1350 printTraceSymbol(Undefined{this, sym.getName(), sym.binding,1351 sym.stOther, sym.type, secIdx},1352 sym.getName());1353 }1354 if (sym.file == this) {1355 std::lock_guard<std::mutex> lock(mu);1356 ctx.nonPrevailingSyms.emplace_back(&sym, secIdx);1357 }1358 continue;1359 }1360 1361 if (sym.file == this) {1362 cast<Defined>(sym).section = sec;1363 continue;1364 }1365 1366 if (sym.binding == STB_WEAK || binding == STB_WEAK)1367 continue;1368 std::lock_guard<std::mutex> lock(mu);1369 ctx.duplicates.push_back({&sym, this, sec, eSym.st_value});1370 }1371}1372 1373// The handling of tentative definitions (COMMON symbols) in archives is murky.1374// A tentative definition will be promoted to a global definition if there are1375// no non-tentative definitions to dominate it. When we hold a tentative1376// definition to a symbol and are inspecting archive members for inclusion1377// there are 2 ways we can proceed:1378//1379// 1) Consider the tentative definition a 'real' definition (ie promotion from1380// tentative to real definition has already happened) and not inspect1381// archive members for Global/Weak definitions to replace the tentative1382// definition. An archive member would only be included if it satisfies some1383// other undefined symbol. This is the behavior Gold uses.1384//1385// 2) Consider the tentative definition as still undefined (ie the promotion to1386// a real definition happens only after all symbol resolution is done).1387// The linker searches archive members for STB_GLOBAL definitions to1388// replace the tentative definition with. This is the behavior used by1389// GNU ld.1390//1391// The second behavior is inherited from SysVR4, which based it on the FORTRAN1392// COMMON BLOCK model. This behavior is needed for proper initialization in old1393// (pre F90) FORTRAN code that is packaged into an archive.1394//1395// The following functions search archive members for definitions to replace1396// tentative definitions (implementing behavior 2).1397static bool isBitcodeNonCommonDef(MemoryBufferRef mb, StringRef symName,1398 StringRef archiveName) {1399 IRSymtabFile symtabFile = check(readIRSymtab(mb));1400 for (const irsymtab::Reader::SymbolRef &sym :1401 symtabFile.TheReader.symbols()) {1402 if (sym.isGlobal() && sym.getName() == symName)1403 return !sym.isUndefined() && !sym.isWeak() && !sym.isCommon();1404 }1405 return false;1406}1407 1408template <class ELFT>1409static bool isNonCommonDef(Ctx &ctx, ELFKind ekind, MemoryBufferRef mb,1410 StringRef symName, StringRef archiveName) {1411 ObjFile<ELFT> *obj = make<ObjFile<ELFT>>(ctx, ekind, mb, archiveName);1412 obj->init();1413 StringRef stringtable = obj->getStringTable();1414 1415 for (auto sym : obj->template getGlobalELFSyms<ELFT>()) {1416 Expected<StringRef> name = sym.getName(stringtable);1417 if (name && name.get() == symName)1418 return sym.isDefined() && sym.getBinding() == STB_GLOBAL &&1419 !sym.isCommon();1420 }1421 return false;1422}1423 1424static bool isNonCommonDef(Ctx &ctx, MemoryBufferRef mb, StringRef symName,1425 StringRef archiveName) {1426 switch (getELFKind(ctx, mb, archiveName)) {1427 case ELF32LEKind:1428 return isNonCommonDef<ELF32LE>(ctx, ELF32LEKind, mb, symName, archiveName);1429 case ELF32BEKind:1430 return isNonCommonDef<ELF32BE>(ctx, ELF32BEKind, mb, symName, archiveName);1431 case ELF64LEKind:1432 return isNonCommonDef<ELF64LE>(ctx, ELF64LEKind, mb, symName, archiveName);1433 case ELF64BEKind:1434 return isNonCommonDef<ELF64BE>(ctx, ELF64BEKind, mb, symName, archiveName);1435 default:1436 llvm_unreachable("getELFKind");1437 }1438}1439 1440SharedFile::SharedFile(Ctx &ctx, MemoryBufferRef m, StringRef defaultSoName)1441 : ELFFileBase(ctx, SharedKind, getELFKind(ctx, m, ""), m),1442 soName(defaultSoName), isNeeded(!ctx.arg.asNeeded) {}1443 1444// Parse the version definitions in the object file if present, and return a1445// vector whose nth element contains a pointer to the Elf_Verdef for version1446// identifier n. Version identifiers that are not definitions map to nullptr.1447template <typename ELFT>1448static SmallVector<const void *, 0>1449parseVerdefs(const uint8_t *base, const typename ELFT::Shdr *sec) {1450 if (!sec)1451 return {};1452 1453 // Build the Verdefs array by following the chain of Elf_Verdef objects1454 // from the start of the .gnu.version_d section.1455 SmallVector<const void *, 0> verdefs;1456 const uint8_t *verdef = base + sec->sh_offset;1457 for (unsigned i = 0, e = sec->sh_info; i != e; ++i) {1458 auto *curVerdef = reinterpret_cast<const typename ELFT::Verdef *>(verdef);1459 verdef += curVerdef->vd_next;1460 unsigned verdefIndex = curVerdef->vd_ndx;1461 if (verdefIndex >= verdefs.size())1462 verdefs.resize(verdefIndex + 1);1463 verdefs[verdefIndex] = curVerdef;1464 }1465 return verdefs;1466}1467 1468// Parse SHT_GNU_verneed to properly set the name of a versioned undefined1469// symbol. We detect fatal issues which would cause vulnerabilities, but do not1470// implement sophisticated error checking like in llvm-readobj because the value1471// of such diagnostics is low.1472template <typename ELFT>1473std::vector<uint32_t> SharedFile::parseVerneed(const ELFFile<ELFT> &obj,1474 const typename ELFT::Shdr *sec) {1475 if (!sec)1476 return {};1477 std::vector<uint32_t> verneeds;1478 ArrayRef<uint8_t> data = CHECK2(obj.getSectionContents(*sec), this);1479 const uint8_t *verneedBuf = data.begin();1480 for (unsigned i = 0; i != sec->sh_info; ++i) {1481 if (verneedBuf + sizeof(typename ELFT::Verneed) > data.end()) {1482 Err(ctx) << this << " has an invalid Verneed";1483 break;1484 }1485 auto *vn = reinterpret_cast<const typename ELFT::Verneed *>(verneedBuf);1486 const uint8_t *vernauxBuf = verneedBuf + vn->vn_aux;1487 for (unsigned j = 0; j != vn->vn_cnt; ++j) {1488 if (vernauxBuf + sizeof(typename ELFT::Vernaux) > data.end()) {1489 Err(ctx) << this << " has an invalid Vernaux";1490 break;1491 }1492 auto *aux = reinterpret_cast<const typename ELFT::Vernaux *>(vernauxBuf);1493 if (aux->vna_name >= this->stringTable.size()) {1494 Err(ctx) << this << " has a Vernaux with an invalid vna_name";1495 break;1496 }1497 uint16_t version = aux->vna_other & VERSYM_VERSION;1498 if (version >= verneeds.size())1499 verneeds.resize(version + 1);1500 verneeds[version] = aux->vna_name;1501 vernauxBuf += aux->vna_next;1502 }1503 verneedBuf += vn->vn_next;1504 }1505 return verneeds;1506}1507 1508// Parse PT_GNU_PROPERTY segments in DSO. The process is similar to1509// readGnuProperty, but we don't have the InputSection information.1510template <typename ELFT>1511void SharedFile::parseGnuAndFeatures(const ELFFile<ELFT> &obj) {1512 if (ctx.arg.emachine != EM_AARCH64)1513 return;1514 const uint8_t *base = obj.base();1515 auto phdrs = CHECK2(obj.program_headers(), this);1516 for (auto phdr : phdrs) {1517 if (phdr.p_type != PT_GNU_PROPERTY)1518 continue;1519 typename ELFT::Note note(1520 *reinterpret_cast<const typename ELFT::Nhdr *>(base + phdr.p_offset));1521 if (note.getType() != NT_GNU_PROPERTY_TYPE_0 || note.getName() != "GNU")1522 continue;1523 1524 ArrayRef<uint8_t> desc = note.getDesc(phdr.p_align);1525 parseGnuPropertyNote<ELFT>(ctx, *this, GNU_PROPERTY_AARCH64_FEATURE_1_AND,1526 desc, base);1527 }1528}1529 1530// We do not usually care about alignments of data in shared object1531// files because the loader takes care of it. However, if we promote a1532// DSO symbol to point to .bss due to copy relocation, we need to keep1533// the original alignment requirements. We infer it in this function.1534template <typename ELFT>1535static uint64_t getAlignment(ArrayRef<typename ELFT::Shdr> sections,1536 const typename ELFT::Sym &sym) {1537 uint64_t ret = UINT64_MAX;1538 if (sym.st_value)1539 ret = 1ULL << llvm::countr_zero((uint64_t)sym.st_value);1540 if (0 < sym.st_shndx && sym.st_shndx < sections.size())1541 ret = std::min<uint64_t>(ret, sections[sym.st_shndx].sh_addralign);1542 return (ret > UINT32_MAX) ? 0 : ret;1543}1544 1545// Fully parse the shared object file.1546//1547// This function parses symbol versions. If a DSO has version information,1548// the file has a ".gnu.version_d" section which contains symbol version1549// definitions. Each symbol is associated to one version through a table in1550// ".gnu.version" section. That table is a parallel array for the symbol1551// table, and each table entry contains an index in ".gnu.version_d".1552//1553// The special index 0 is reserved for VERF_NDX_LOCAL and 1 is for1554// VER_NDX_GLOBAL. There's no table entry for these special versions in1555// ".gnu.version_d".1556//1557// The file format for symbol versioning is perhaps a bit more complicated1558// than necessary, but you can easily understand the code if you wrap your1559// head around the data structure described above.1560template <class ELFT> void SharedFile::parse() {1561 using Elf_Dyn = typename ELFT::Dyn;1562 using Elf_Shdr = typename ELFT::Shdr;1563 using Elf_Sym = typename ELFT::Sym;1564 using Elf_Verdef = typename ELFT::Verdef;1565 using Elf_Versym = typename ELFT::Versym;1566 1567 ArrayRef<Elf_Dyn> dynamicTags;1568 const ELFFile<ELFT> obj = this->getObj<ELFT>();1569 ArrayRef<Elf_Shdr> sections = getELFShdrs<ELFT>();1570 1571 const Elf_Shdr *versymSec = nullptr;1572 const Elf_Shdr *verdefSec = nullptr;1573 const Elf_Shdr *verneedSec = nullptr;1574 symbols = std::make_unique<Symbol *[]>(numSymbols);1575 1576 // Search for .dynsym, .dynamic, .symtab, .gnu.version and .gnu.version_d.1577 for (const Elf_Shdr &sec : sections) {1578 switch (sec.sh_type) {1579 default:1580 continue;1581 case SHT_DYNAMIC:1582 dynamicTags =1583 CHECK2(obj.template getSectionContentsAsArray<Elf_Dyn>(sec), this);1584 break;1585 case SHT_GNU_versym:1586 versymSec = &sec;1587 break;1588 case SHT_GNU_verdef:1589 verdefSec = &sec;1590 break;1591 case SHT_GNU_verneed:1592 verneedSec = &sec;1593 break;1594 }1595 }1596 1597 if (versymSec && numSymbols == 0) {1598 ErrAlways(ctx) << "SHT_GNU_versym should be associated with symbol table";1599 return;1600 }1601 1602 // Search for a DT_SONAME tag to initialize this->soName.1603 for (const Elf_Dyn &dyn : dynamicTags) {1604 if (dyn.d_tag == DT_NEEDED) {1605 uint64_t val = dyn.getVal();1606 if (val >= this->stringTable.size()) {1607 Err(ctx) << this << ": invalid DT_NEEDED entry";1608 return;1609 }1610 dtNeeded.push_back(this->stringTable.data() + val);1611 } else if (dyn.d_tag == DT_SONAME) {1612 uint64_t val = dyn.getVal();1613 if (val >= this->stringTable.size()) {1614 Err(ctx) << this << ": invalid DT_SONAME entry";1615 return;1616 }1617 soName = this->stringTable.data() + val;1618 }1619 }1620 1621 // DSOs are uniquified not by filename but by soname.1622 StringSaver &ss = ctx.saver;1623 DenseMap<CachedHashStringRef, SharedFile *>::iterator it;1624 bool wasInserted;1625 std::tie(it, wasInserted) =1626 ctx.symtab->soNames.try_emplace(CachedHashStringRef(soName), this);1627 1628 // If a DSO appears more than once on the command line with and without1629 // --as-needed, --no-as-needed takes precedence over --as-needed because a1630 // user can add an extra DSO with --no-as-needed to force it to be added to1631 // the dependency list.1632 it->second->isNeeded |= isNeeded;1633 if (!wasInserted)1634 return;1635 1636 ctx.sharedFiles.push_back(this);1637 1638 verdefs = parseVerdefs<ELFT>(obj.base(), verdefSec);1639 std::vector<uint32_t> verneeds = parseVerneed<ELFT>(obj, verneedSec);1640 parseGnuAndFeatures<ELFT>(obj);1641 1642 // Parse ".gnu.version" section which is a parallel array for the symbol1643 // table. If a given file doesn't have a ".gnu.version" section, we use1644 // VER_NDX_GLOBAL.1645 size_t size = numSymbols - firstGlobal;1646 std::vector<uint16_t> versyms(size, VER_NDX_GLOBAL);1647 if (versymSec) {1648 ArrayRef<Elf_Versym> versym =1649 CHECK2(obj.template getSectionContentsAsArray<Elf_Versym>(*versymSec),1650 this)1651 .slice(firstGlobal);1652 for (size_t i = 0; i < size; ++i)1653 versyms[i] = versym[i].vs_index;1654 }1655 1656 // System libraries can have a lot of symbols with versions. Using a1657 // fixed buffer for computing the versions name (foo@ver) can save a1658 // lot of allocations.1659 SmallString<0> versionedNameBuffer;1660 1661 // Add symbols to the symbol table.1662 ArrayRef<Elf_Sym> syms = this->getGlobalELFSyms<ELFT>();1663 for (size_t i = 0, e = syms.size(); i != e; ++i) {1664 const Elf_Sym &sym = syms[i];1665 1666 // ELF spec requires that all local symbols precede weak or global1667 // symbols in each symbol table, and the index of first non-local symbol1668 // is stored to sh_info. If a local symbol appears after some non-local1669 // symbol, that's a violation of the spec.1670 StringRef name = CHECK2(sym.getName(stringTable), this);1671 if (sym.getBinding() == STB_LOCAL) {1672 Err(ctx) << this << ": invalid local symbol '" << name1673 << "' in global part of symbol table";1674 continue;1675 }1676 1677 const uint16_t ver = versyms[i], idx = ver & ~VERSYM_HIDDEN;1678 if (sym.isUndefined()) {1679 // Index 0 (VER_NDX_LOCAL) is used for unversioned undefined symbols.1680 // GNU ld versions between 2.35 and 2.45 also generate VER_NDX_GLOBAL1681 // for this case (https://sourceware.org/PR33577).1682 if (ver != VER_NDX_LOCAL && ver != VER_NDX_GLOBAL) {1683 if (idx >= verneeds.size()) {1684 ErrAlways(ctx) << "corrupt input file: version need index " << idx1685 << " for symbol " << name1686 << " is out of bounds\n>>> defined in " << this;1687 continue;1688 }1689 StringRef verName = stringTable.data() + verneeds[idx];1690 versionedNameBuffer.clear();1691 name = ss.save((name + "@" + verName).toStringRef(versionedNameBuffer));1692 }1693 Symbol *s = ctx.symtab->addSymbol(1694 Undefined{this, name, sym.getBinding(), sym.st_other, sym.getType()});1695 s->isExported = true;1696 if (sym.getBinding() != STB_WEAK &&1697 ctx.arg.unresolvedSymbolsInShlib != UnresolvedPolicy::Ignore)1698 requiredSymbols.push_back(s);1699 continue;1700 }1701 1702 if (ver == VER_NDX_LOCAL ||1703 (ver != VER_NDX_GLOBAL && idx >= verdefs.size())) {1704 // In GNU ld < 2.31 (before 3be08ea4728b56d35e136af4e6fd3086ade17764), the1705 // MIPS port puts _gp_disp symbol into DSO files and incorrectly assigns1706 // VER_NDX_LOCAL. Workaround this bug.1707 if (ctx.arg.emachine == EM_MIPS && name == "_gp_disp")1708 continue;1709 ErrAlways(ctx) << "corrupt input file: version definition index " << idx1710 << " for symbol " << name1711 << " is out of bounds\n>>> defined in " << this;1712 continue;1713 }1714 1715 uint32_t alignment = getAlignment<ELFT>(sections, sym);1716 if (ver == idx) {1717 auto *s = ctx.symtab->addSymbol(1718 SharedSymbol{*this, name, sym.getBinding(), sym.st_other,1719 sym.getType(), sym.st_value, sym.st_size, alignment});1720 s->dsoDefined = true;1721 if (s->file == this)1722 s->versionId = ver;1723 }1724 1725 // Also add the symbol with the versioned name to handle undefined symbols1726 // with explicit versions.1727 if (ver == VER_NDX_GLOBAL)1728 continue;1729 1730 StringRef verName =1731 stringTable.data() +1732 reinterpret_cast<const Elf_Verdef *>(verdefs[idx])->getAux()->vda_name;1733 versionedNameBuffer.clear();1734 name = (name + "@" + verName).toStringRef(versionedNameBuffer);1735 auto *s = ctx.symtab->addSymbol(1736 SharedSymbol{*this, ss.save(name), sym.getBinding(), sym.st_other,1737 sym.getType(), sym.st_value, sym.st_size, alignment});1738 s->dsoDefined = true;1739 if (s->file == this)1740 s->versionId = idx;1741 }1742}1743 1744static ELFKind getBitcodeELFKind(const Triple &t) {1745 if (t.isLittleEndian())1746 return t.isArch64Bit() ? ELF64LEKind : ELF32LEKind;1747 return t.isArch64Bit() ? ELF64BEKind : ELF32BEKind;1748}1749 1750static uint16_t getBitcodeMachineKind(Ctx &ctx, StringRef path,1751 const Triple &t) {1752 switch (t.getArch()) {1753 case Triple::aarch64:1754 case Triple::aarch64_be:1755 return EM_AARCH64;1756 case Triple::amdgcn:1757 case Triple::r600:1758 return EM_AMDGPU;1759 case Triple::arm:1760 case Triple::armeb:1761 case Triple::thumb:1762 case Triple::thumbeb:1763 return EM_ARM;1764 case Triple::avr:1765 return EM_AVR;1766 case Triple::hexagon:1767 return EM_HEXAGON;1768 case Triple::loongarch32:1769 case Triple::loongarch64:1770 return EM_LOONGARCH;1771 case Triple::mips:1772 case Triple::mipsel:1773 case Triple::mips64:1774 case Triple::mips64el:1775 return EM_MIPS;1776 case Triple::msp430:1777 return EM_MSP430;1778 case Triple::ppc:1779 case Triple::ppcle:1780 return EM_PPC;1781 case Triple::ppc64:1782 case Triple::ppc64le:1783 return EM_PPC64;1784 case Triple::riscv32:1785 case Triple::riscv64:1786 return EM_RISCV;1787 case Triple::sparcv9:1788 return EM_SPARCV9;1789 case Triple::systemz:1790 return EM_S390;1791 case Triple::x86:1792 return t.isOSIAMCU() ? EM_IAMCU : EM_386;1793 case Triple::x86_64:1794 return EM_X86_64;1795 default:1796 ErrAlways(ctx) << path1797 << ": could not infer e_machine from bitcode target triple "1798 << t.str();1799 return EM_NONE;1800 }1801}1802 1803static uint8_t getOsAbi(const Triple &t) {1804 switch (t.getOS()) {1805 case Triple::AMDHSA:1806 return ELF::ELFOSABI_AMDGPU_HSA;1807 case Triple::AMDPAL:1808 return ELF::ELFOSABI_AMDGPU_PAL;1809 case Triple::Mesa3D:1810 return ELF::ELFOSABI_AMDGPU_MESA3D;1811 default:1812 return ELF::ELFOSABI_NONE;1813 }1814}1815 1816// For DTLTO, bitcode member names must be valid paths to files on disk.1817// For thin archives, resolve `memberPath` relative to the archive's location.1818// Returns true if adjusted; false otherwise. Non-thin archives are unsupported.1819static bool dtltoAdjustMemberPathIfThinArchive(Ctx &ctx, StringRef archivePath,1820 std::string &memberPath) {1821 assert(!archivePath.empty());1822 1823 if (ctx.arg.dtltoDistributor.empty())1824 return false;1825 1826 // Read the archive header to determine if it's a thin archive.1827 auto bufferOrErr =1828 MemoryBuffer::getFileSlice(archivePath, sizeof(ThinArchiveMagic) - 1, 0);1829 if (std::error_code ec = bufferOrErr.getError()) {1830 ErrAlways(ctx) << "cannot open " << archivePath << ": " << ec.message();1831 return false;1832 }1833 1834 if (!bufferOrErr->get()->getBuffer().starts_with(ThinArchiveMagic))1835 return false;1836 1837 SmallString<128> resolvedPath;1838 if (path::is_relative(memberPath)) {1839 resolvedPath = path::parent_path(archivePath);1840 path::append(resolvedPath, memberPath);1841 } else1842 resolvedPath = memberPath;1843 1844 path::remove_dots(resolvedPath, /*remove_dot_dot=*/true);1845 memberPath = resolvedPath.str();1846 return true;1847}1848 1849BitcodeFile::BitcodeFile(Ctx &ctx, MemoryBufferRef mb, StringRef archiveName,1850 uint64_t offsetInArchive, bool lazy)1851 : InputFile(ctx, BitcodeKind, mb) {1852 this->archiveName = archiveName;1853 this->lazy = lazy;1854 1855 std::string path = mb.getBufferIdentifier().str();1856 if (ctx.arg.thinLTOIndexOnly)1857 path = replaceThinLTOSuffix(ctx, mb.getBufferIdentifier());1858 1859 StringSaver &ss = ctx.saver;1860 StringRef name;1861 if (archiveName.empty() ||1862 dtltoAdjustMemberPathIfThinArchive(ctx, archiveName, path)) {1863 name = ss.save(path);1864 } else {1865 // ThinLTO assumes that all MemoryBufferRefs given to it have a unique1866 // name. If two archives define two members with the same name, this1867 // causes a collision which result in only one of the objects being taken1868 // into consideration at LTO time (which very likely causes undefined1869 // symbols later in the link stage). So we append file offset to make1870 // filename unique.1871 name = ss.save(archiveName + "(" + path::filename(path) + " at " +1872 utostr(offsetInArchive) + ")");1873 }1874 1875 MemoryBufferRef mbref(mb.getBuffer(), name);1876 1877 obj = CHECK2(lto::InputFile::create(mbref), this);1878 1879 Triple t(obj->getTargetTriple());1880 ekind = getBitcodeELFKind(t);1881 emachine = getBitcodeMachineKind(ctx, mb.getBufferIdentifier(), t);1882 osabi = getOsAbi(t);1883}1884 1885static uint8_t mapVisibility(GlobalValue::VisibilityTypes gvVisibility) {1886 switch (gvVisibility) {1887 case GlobalValue::DefaultVisibility:1888 return STV_DEFAULT;1889 case GlobalValue::HiddenVisibility:1890 return STV_HIDDEN;1891 case GlobalValue::ProtectedVisibility:1892 return STV_PROTECTED;1893 }1894 llvm_unreachable("unknown visibility");1895}1896 1897static void createBitcodeSymbol(Ctx &ctx, Symbol *&sym,1898 const lto::InputFile::Symbol &objSym,1899 BitcodeFile &f) {1900 uint8_t binding = objSym.isWeak() ? STB_WEAK : STB_GLOBAL;1901 uint8_t type = objSym.isTLS() ? STT_TLS : STT_NOTYPE;1902 uint8_t visibility = mapVisibility(objSym.getVisibility());1903 1904 if (!sym) {1905 // Symbols can be duplicated in bitcode files because of '#include' and1906 // linkonce_odr. Use uniqueSaver to save symbol names for de-duplication.1907 // Update objSym.Name to reference (via StringRef) the string saver's copy;1908 // this way LTO can reference the same string saver's copy rather than1909 // keeping copies of its own.1910 objSym.Name = ctx.uniqueSaver.save(objSym.getName());1911 sym = ctx.symtab->insert(objSym.getName());1912 }1913 1914 if (objSym.isUndefined()) {1915 Undefined newSym(&f, StringRef(), binding, visibility, type);1916 sym->resolve(ctx, newSym);1917 sym->referenced = true;1918 return;1919 }1920 1921 if (objSym.isCommon()) {1922 sym->resolve(ctx, CommonSymbol{ctx, &f, StringRef(), binding, visibility,1923 STT_OBJECT, objSym.getCommonAlignment(),1924 objSym.getCommonSize()});1925 } else {1926 Defined newSym(ctx, &f, StringRef(), binding, visibility, type, 0, 0,1927 nullptr);1928 // The definition can be omitted if all bitcode definitions satisfy1929 // `canBeOmittedFromSymbolTable()` and isUsedInRegularObj is false.1930 // The latter condition is tested in parseVersionAndComputeIsPreemptible.1931 sym->ltoCanOmit = objSym.canBeOmittedFromSymbolTable() &&1932 (!sym->isDefined() || sym->ltoCanOmit);1933 sym->resolve(ctx, newSym);1934 }1935}1936 1937void BitcodeFile::parse() {1938 for (std::pair<StringRef, Comdat::SelectionKind> s : obj->getComdatTable()) {1939 keptComdats.push_back(1940 s.second == Comdat::NoDeduplicate ||1941 ctx.symtab->comdatGroups.try_emplace(CachedHashStringRef(s.first), this)1942 .second);1943 }1944 1945 if (numSymbols == 0) {1946 numSymbols = obj->symbols().size();1947 symbols = std::make_unique<Symbol *[]>(numSymbols);1948 }1949 // Process defined symbols first. See the comment in1950 // ObjFile<ELFT>::initializeSymbols.1951 for (auto [i, irSym] : llvm::enumerate(obj->symbols()))1952 if (!irSym.isUndefined())1953 createBitcodeSymbol(ctx, symbols[i], irSym, *this);1954 for (auto [i, irSym] : llvm::enumerate(obj->symbols()))1955 if (irSym.isUndefined())1956 createBitcodeSymbol(ctx, symbols[i], irSym, *this);1957 1958 for (auto l : obj->getDependentLibraries())1959 addDependentLibrary(ctx, l, this);1960}1961 1962void BitcodeFile::parseLazy() {1963 numSymbols = obj->symbols().size();1964 symbols = std::make_unique<Symbol *[]>(numSymbols);1965 for (auto [i, irSym] : llvm::enumerate(obj->symbols())) {1966 // Symbols can be duplicated in bitcode files because of '#include' and1967 // linkonce_odr. Use uniqueSaver to save symbol names for de-duplication.1968 // Update objSym.Name to reference (via StringRef) the string saver's copy;1969 // this way LTO can reference the same string saver's copy rather than1970 // keeping copies of its own.1971 irSym.Name = ctx.uniqueSaver.save(irSym.getName());1972 if (!irSym.isUndefined()) {1973 auto *sym = ctx.symtab->insert(irSym.getName());1974 sym->resolve(ctx, LazySymbol{*this});1975 symbols[i] = sym;1976 }1977 }1978}1979 1980void BitcodeFile::postParse() {1981 for (auto [i, irSym] : llvm::enumerate(obj->symbols())) {1982 const Symbol &sym = *symbols[i];1983 if (sym.file == this || !sym.isDefined() || irSym.isUndefined() ||1984 irSym.isCommon() || irSym.isWeak())1985 continue;1986 int c = irSym.getComdatIndex();1987 if (c != -1 && !keptComdats[c])1988 continue;1989 reportDuplicate(ctx, sym, this, nullptr, 0);1990 }1991}1992 1993void BinaryFile::parse() {1994 ArrayRef<uint8_t> data = arrayRefFromStringRef(mb.getBuffer());1995 auto *section =1996 make<InputSection>(this, ".data", SHT_PROGBITS, SHF_ALLOC | SHF_WRITE,1997 /*addralign=*/8, /*entsize=*/0, data);1998 sections.push_back(section);1999 2000 // For each input file foo that is embedded to a result as a binary2001 // blob, we define _binary_foo_{start,end,size} symbols, so that2002 // user programs can access blobs by name. Non-alphanumeric2003 // characters in a filename are replaced with underscore.2004 std::string s = "_binary_" + mb.getBufferIdentifier().str();2005 for (char &c : s)2006 if (!isAlnum(c))2007 c = '_';2008 2009 llvm::StringSaver &ss = ctx.saver;2010 ctx.symtab->addAndCheckDuplicate(2011 ctx, Defined{ctx, this, ss.save(s + "_start"), STB_GLOBAL, STV_DEFAULT,2012 STT_OBJECT, 0, 0, section});2013 ctx.symtab->addAndCheckDuplicate(2014 ctx, Defined{ctx, this, ss.save(s + "_end"), STB_GLOBAL, STV_DEFAULT,2015 STT_OBJECT, data.size(), 0, section});2016 ctx.symtab->addAndCheckDuplicate(2017 ctx, Defined{ctx, this, ss.save(s + "_size"), STB_GLOBAL, STV_DEFAULT,2018 STT_OBJECT, data.size(), 0, nullptr});2019}2020 2021InputFile *elf::createInternalFile(Ctx &ctx, StringRef name) {2022 auto *file =2023 make<InputFile>(ctx, InputFile::InternalKind, MemoryBufferRef("", name));2024 // References from an internal file do not lead to --warn-backrefs2025 // diagnostics.2026 file->groupId = 0;2027 return file;2028}2029 2030std::unique_ptr<ELFFileBase> elf::createObjFile(Ctx &ctx, MemoryBufferRef mb,2031 StringRef archiveName,2032 bool lazy) {2033 std::unique_ptr<ELFFileBase> f;2034 switch (getELFKind(ctx, mb, archiveName)) {2035 case ELF32LEKind:2036 f = std::make_unique<ObjFile<ELF32LE>>(ctx, ELF32LEKind, mb, archiveName);2037 break;2038 case ELF32BEKind:2039 f = std::make_unique<ObjFile<ELF32BE>>(ctx, ELF32BEKind, mb, archiveName);2040 break;2041 case ELF64LEKind:2042 f = std::make_unique<ObjFile<ELF64LE>>(ctx, ELF64LEKind, mb, archiveName);2043 break;2044 case ELF64BEKind:2045 f = std::make_unique<ObjFile<ELF64BE>>(ctx, ELF64BEKind, mb, archiveName);2046 break;2047 default:2048 llvm_unreachable("getELFKind");2049 }2050 f->init();2051 f->lazy = lazy;2052 return f;2053}2054 2055template <class ELFT> void ObjFile<ELFT>::parseLazy() {2056 const ArrayRef<typename ELFT::Sym> eSyms = this->getELFSyms<ELFT>();2057 numSymbols = eSyms.size();2058 symbols = std::make_unique<Symbol *[]>(numSymbols);2059 2060 // resolve() may trigger this->extract() if an existing symbol is an undefined2061 // symbol. If that happens, this function has served its purpose, and we can2062 // exit from the loop early.2063 auto *symtab = ctx.symtab.get();2064 for (size_t i = firstGlobal, end = eSyms.size(); i != end; ++i) {2065 if (eSyms[i].st_shndx == SHN_UNDEF)2066 continue;2067 symbols[i] = symtab->insert(CHECK2(eSyms[i].getName(stringTable), this));2068 symbols[i]->resolve(ctx, LazySymbol{*this});2069 if (!lazy)2070 break;2071 }2072}2073 2074bool InputFile::shouldExtractForCommon(StringRef name) const {2075 if (isa<BitcodeFile>(this))2076 return isBitcodeNonCommonDef(mb, name, archiveName);2077 2078 return isNonCommonDef(ctx, mb, name, archiveName);2079}2080 2081std::string elf::replaceThinLTOSuffix(Ctx &ctx, StringRef path) {2082 auto [suffix, repl] = ctx.arg.thinLTOObjectSuffixReplace;2083 if (path.consume_back(suffix))2084 return (path + repl).str();2085 return std::string(path);2086}2087 2088template class elf::ObjFile<ELF32LE>;2089template class elf::ObjFile<ELF32BE>;2090template class elf::ObjFile<ELF64LE>;2091template class elf::ObjFile<ELF64BE>;2092 2093template void SharedFile::parse<ELF32LE>();2094template void SharedFile::parse<ELF32BE>();2095template void SharedFile::parse<ELF64LE>();2096template void SharedFile::parse<ELF64BE>();2097