From fe15a8732c309bc1defbff3c2a0f63aeff5c6e4a Mon Sep 17 00:00:00 2001 From: Jacek Caban Date: Wed, 4 Dec 2024 16:54:35 +0100 Subject: [PATCH] [LLD][COFF] Store machine type in SymbolTable (NFC) This change prepares for hybrid ARM64X support, which requires two SymbolTable instances: one for native symbols and one for EC symbols. In such cases, config.machine will remain ARM64X, while the SymbolTable instances will store ARM64 and ARM64EC machine types. --- lld/COFF/DLL.cpp | 2 +- lld/COFF/Driver.cpp | 1 + lld/COFF/InputFiles.cpp | 8 ++++---- lld/COFF/SymbolTable.cpp | 21 +++++++++------------ lld/COFF/SymbolTable.h | 3 +++ 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/lld/COFF/DLL.cpp b/lld/COFF/DLL.cpp index 3d6ed5a9ddeae..875ada9d60539 100644 --- a/lld/COFF/DLL.cpp +++ b/lld/COFF/DLL.cpp @@ -167,7 +167,7 @@ class AuxImportChunk : public NonSectionChunk { void getBaserels(std::vector *res) override { if (file->impchkThunk) - res->emplace_back(rva, file->symtab.ctx.config.machine); + res->emplace_back(rva, file->symtab.machine); } private: diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 5a3db54846318..0705f1c1be999 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -596,6 +596,7 @@ void LinkerDriver::setMachine(MachineTypes machine) { assert(machine != IMAGE_FILE_MACHINE_UNKNOWN); ctx.config.machine = machine; + ctx.symtab.machine = machine; addWinSysRootLibSearchPaths(); } diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index 42c1a9aa90a0f..9ff9346ed598b 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -551,7 +551,7 @@ Symbol *ObjFile::createUndefined(COFFSymbolRef sym, bool overrideLazy) { // Add an anti-dependency alias for undefined AMD64 symbols on the ARM64EC // target. - if (isArm64EC(symtab.ctx.config.machine) && getMachineType() == AMD64) { + if (symtab.isEC() && getMachineType() == AMD64) { auto u = dyn_cast(s); if (u && !u->weakAlias) { if (std::optional mangledName = @@ -1035,7 +1035,7 @@ ObjFile::getVariableLocation(StringRef var) { if (!dwarf) return std::nullopt; } - if (symtab.ctx.config.machine == I386) + if (symtab.machine == I386) var.consume_front("_"); std::optional> ret = dwarf->getVariableLoc(var); @@ -1139,7 +1139,7 @@ void ImportFile::parse() { bool isCode = hdr->getType() == llvm::COFF::IMPORT_CODE; - if (symtab.ctx.config.machine != ARM64EC) { + if (!symtab.isEC()) { impSym = symtab.addImportData(impName, this, location); } else { // In addition to the regular IAT, ARM64EC also contains an auxiliary IAT, @@ -1175,7 +1175,7 @@ void ImportFile::parse() { // address pointed by the __imp_ symbol. (This allows you to call // DLL functions just like regular non-DLL functions.) if (isCode) { - if (symtab.ctx.config.machine != ARM64EC) { + if (!symtab.isEC()) { thunkSym = symtab.addImportThunk(name, impSym, makeImportThunk()); } else { thunkSym = symtab.addImportThunk( diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp index d6cf10756e296..515239db029f7 100644 --- a/lld/COFF/SymbolTable.cpp +++ b/lld/COFF/SymbolTable.cpp @@ -300,7 +300,7 @@ void SymbolTable::loadMinGWSymbols() { StringRef name = undef->getName(); - if (ctx.config.machine == I386 && ctx.config.stdcallFixup) { + if (machine == I386 && ctx.config.stdcallFixup) { // Check if we can resolve an undefined decorated symbol by finding // the intended target as an undecorated symbol (only with a leading // underscore). @@ -524,7 +524,7 @@ bool SymbolTable::resolveRemainingUndefines() { StringRef impName = name.substr(strlen("__imp_")); Symbol *imp = findLocalSym(impName); - if (!imp && isArm64EC(ctx.config.machine)) { + if (!imp && isEC()) { // Try to use the mangled symbol on ARM64EC. std::optional mangledName = getArm64ECMangledFunctionName(impName); @@ -582,7 +582,7 @@ std::pair SymbolTable::insert(StringRef name) { sym->canInline = true; inserted = true; - if (isArm64EC(ctx.config.machine) && name.starts_with("EXP+")) + if (isEC() && name.starts_with("EXP+")) expSymbols.push_back(sym); } return {sym, inserted}; @@ -700,8 +700,7 @@ bool checkLazyECPair(SymbolTable *symtab, StringRef name, InputFile *f) { void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) { StringRef name = sym.getName(); - if (isArm64EC(ctx.config.machine) && - !checkLazyECPair(this, name, f)) + if (isEC() && !checkLazyECPair(this, name, f)) return; auto [s, wasInserted] = insert(name); if (wasInserted) { @@ -709,8 +708,7 @@ void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) { return; } auto *u = dyn_cast(s); - if (!u || (u->weakAlias && !u->isECAlias(ctx.config.machine)) || - s->pendingArchiveLoad) + if (!u || (u->weakAlias && !u->isECAlias(machine)) || s->pendingArchiveLoad) return; s->pendingArchiveLoad = true; f->addMember(sym); @@ -718,7 +716,7 @@ void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) { void SymbolTable::addLazyObject(InputFile *f, StringRef n) { assert(f->lazy); - if (isArm64EC(ctx.config.machine) && !checkLazyECPair(this, n, f)) + if (isEC() && !checkLazyECPair(this, n, f)) return; auto [s, wasInserted] = insert(n, f); if (wasInserted) { @@ -726,8 +724,7 @@ void SymbolTable::addLazyObject(InputFile *f, StringRef n) { return; } auto *u = dyn_cast(s); - if (!u || (u->weakAlias && !u->isECAlias(ctx.config.machine)) || - s->pendingArchiveLoad) + if (!u || (u->weakAlias && !u->isECAlias(machine)) || s->pendingArchiveLoad) return; s->pendingArchiveLoad = true; f->lazy = false; @@ -939,7 +936,7 @@ Symbol *SymbolTable::find(StringRef name) const { } Symbol *SymbolTable::findUnderscore(StringRef name) const { - if (ctx.config.machine == I386) + if (machine == I386) return find(("_" + name).str()); return find(name); } @@ -986,7 +983,7 @@ Symbol *SymbolTable::findMangle(StringRef name) { }; // For non-x86, just look for C++ functions. - if (ctx.config.machine != I386) + if (machine != I386) return findByPrefix("?" + name + "@@Y"); if (!name.starts_with("_")) diff --git a/lld/COFF/SymbolTable.h b/lld/COFF/SymbolTable.h index 92e94671ce813..40dbb99ae9cdd 100644 --- a/lld/COFF/SymbolTable.h +++ b/lld/COFF/SymbolTable.h @@ -120,6 +120,9 @@ class SymbolTable { uint32_t newSectionOffset = 0); COFFLinkerContext &ctx; + llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN; + + bool isEC() const { return machine == ARM64EC; } // A list of chunks which to be added to .rdata. std::vector localImportChunks;