Skip to content

[LLD][COFF] Store machine type in SymbolTable (NFC) #119298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lld/COFF/DLL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class AuxImportChunk : public NonSectionChunk {

void getBaserels(std::vector<Baserel> *res) override {
if (file->impchkThunk)
res->emplace_back(rva, file->symtab.ctx.config.machine);
res->emplace_back(rva, file->symtab.machine);
}

private:
Expand Down
1 change: 1 addition & 0 deletions lld/COFF/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ void LinkerDriver::setMachine(MachineTypes machine) {
assert(machine != IMAGE_FILE_MACHINE_UNKNOWN);

ctx.config.machine = machine;
ctx.symtab.machine = machine;
addWinSysRootLibSearchPaths();
}

Expand Down
8 changes: 4 additions & 4 deletions lld/COFF/InputFiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Undefined>(s);
if (u && !u->weakAlias) {
if (std::optional<std::string> mangledName =
Expand Down Expand Up @@ -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<std::pair<std::string, unsigned>> ret =
dwarf->getVariableLoc(var);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
21 changes: 9 additions & 12 deletions lld/COFF/SymbolTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -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<std::string> mangledName =
getArm64ECMangledFunctionName(impName);
Expand Down Expand Up @@ -582,7 +582,7 @@ std::pair<Symbol *, bool> 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};
Expand Down Expand Up @@ -700,34 +700,31 @@ 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<LazyArchive>(this, name, f))
if (isEC() && !checkLazyECPair<LazyArchive>(this, name, f))
return;
auto [s, wasInserted] = insert(name);
if (wasInserted) {
replaceSymbol<LazyArchive>(s, f, sym);
return;
}
auto *u = dyn_cast<Undefined>(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);
}

void SymbolTable::addLazyObject(InputFile *f, StringRef n) {
assert(f->lazy);
if (isArm64EC(ctx.config.machine) && !checkLazyECPair<LazyObject>(this, n, f))
if (isEC() && !checkLazyECPair<LazyObject>(this, n, f))
return;
auto [s, wasInserted] = insert(n, f);
if (wasInserted) {
replaceSymbol<LazyObject>(s, f, n);
return;
}
auto *u = dyn_cast<Undefined>(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;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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("_"))
Expand Down
3 changes: 3 additions & 0 deletions lld/COFF/SymbolTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chunk *> localImportChunks;
Expand Down
Loading