Skip to content

Commit d3c4857

Browse files
authored
[LLD][COFF] Store machine type in SymbolTable (NFC) (#119298)
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.
1 parent 0a9810d commit d3c4857

File tree

5 files changed

+18
-17
lines changed

5 files changed

+18
-17
lines changed

lld/COFF/DLL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ class AuxImportChunk : public NonSectionChunk {
167167

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

173173
private:

lld/COFF/Driver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ void LinkerDriver::setMachine(MachineTypes machine) {
596596
assert(machine != IMAGE_FILE_MACHINE_UNKNOWN);
597597

598598
ctx.config.machine = machine;
599+
ctx.symtab.machine = machine;
599600
addWinSysRootLibSearchPaths();
600601
}
601602

lld/COFF/InputFiles.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ Symbol *ObjFile::createUndefined(COFFSymbolRef sym, bool overrideLazy) {
551551

552552
// Add an anti-dependency alias for undefined AMD64 symbols on the ARM64EC
553553
// target.
554-
if (isArm64EC(symtab.ctx.config.machine) && getMachineType() == AMD64) {
554+
if (symtab.isEC() && getMachineType() == AMD64) {
555555
auto u = dyn_cast<Undefined>(s);
556556
if (u && !u->weakAlias) {
557557
if (std::optional<std::string> mangledName =
@@ -1035,7 +1035,7 @@ ObjFile::getVariableLocation(StringRef var) {
10351035
if (!dwarf)
10361036
return std::nullopt;
10371037
}
1038-
if (symtab.ctx.config.machine == I386)
1038+
if (symtab.machine == I386)
10391039
var.consume_front("_");
10401040
std::optional<std::pair<std::string, unsigned>> ret =
10411041
dwarf->getVariableLoc(var);
@@ -1139,7 +1139,7 @@ void ImportFile::parse() {
11391139

11401140
bool isCode = hdr->getType() == llvm::COFF::IMPORT_CODE;
11411141

1142-
if (symtab.ctx.config.machine != ARM64EC) {
1142+
if (!symtab.isEC()) {
11431143
impSym = symtab.addImportData(impName, this, location);
11441144
} else {
11451145
// In addition to the regular IAT, ARM64EC also contains an auxiliary IAT,
@@ -1175,7 +1175,7 @@ void ImportFile::parse() {
11751175
// address pointed by the __imp_ symbol. (This allows you to call
11761176
// DLL functions just like regular non-DLL functions.)
11771177
if (isCode) {
1178-
if (symtab.ctx.config.machine != ARM64EC) {
1178+
if (!symtab.isEC()) {
11791179
thunkSym = symtab.addImportThunk(name, impSym, makeImportThunk());
11801180
} else {
11811181
thunkSym = symtab.addImportThunk(

lld/COFF/SymbolTable.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ void SymbolTable::loadMinGWSymbols() {
300300

301301
StringRef name = undef->getName();
302302

303-
if (ctx.config.machine == I386 && ctx.config.stdcallFixup) {
303+
if (machine == I386 && ctx.config.stdcallFixup) {
304304
// Check if we can resolve an undefined decorated symbol by finding
305305
// the intended target as an undecorated symbol (only with a leading
306306
// underscore).
@@ -524,7 +524,7 @@ bool SymbolTable::resolveRemainingUndefines() {
524524

525525
StringRef impName = name.substr(strlen("__imp_"));
526526
Symbol *imp = findLocalSym(impName);
527-
if (!imp && isArm64EC(ctx.config.machine)) {
527+
if (!imp && isEC()) {
528528
// Try to use the mangled symbol on ARM64EC.
529529
std::optional<std::string> mangledName =
530530
getArm64ECMangledFunctionName(impName);
@@ -582,7 +582,7 @@ std::pair<Symbol *, bool> SymbolTable::insert(StringRef name) {
582582
sym->canInline = true;
583583
inserted = true;
584584

585-
if (isArm64EC(ctx.config.machine) && name.starts_with("EXP+"))
585+
if (isEC() && name.starts_with("EXP+"))
586586
expSymbols.push_back(sym);
587587
}
588588
return {sym, inserted};
@@ -700,34 +700,31 @@ bool checkLazyECPair(SymbolTable *symtab, StringRef name, InputFile *f) {
700700

701701
void SymbolTable::addLazyArchive(ArchiveFile *f, const Archive::Symbol &sym) {
702702
StringRef name = sym.getName();
703-
if (isArm64EC(ctx.config.machine) &&
704-
!checkLazyECPair<LazyArchive>(this, name, f))
703+
if (isEC() && !checkLazyECPair<LazyArchive>(this, name, f))
705704
return;
706705
auto [s, wasInserted] = insert(name);
707706
if (wasInserted) {
708707
replaceSymbol<LazyArchive>(s, f, sym);
709708
return;
710709
}
711710
auto *u = dyn_cast<Undefined>(s);
712-
if (!u || (u->weakAlias && !u->isECAlias(ctx.config.machine)) ||
713-
s->pendingArchiveLoad)
711+
if (!u || (u->weakAlias && !u->isECAlias(machine)) || s->pendingArchiveLoad)
714712
return;
715713
s->pendingArchiveLoad = true;
716714
f->addMember(sym);
717715
}
718716

719717
void SymbolTable::addLazyObject(InputFile *f, StringRef n) {
720718
assert(f->lazy);
721-
if (isArm64EC(ctx.config.machine) && !checkLazyECPair<LazyObject>(this, n, f))
719+
if (isEC() && !checkLazyECPair<LazyObject>(this, n, f))
722720
return;
723721
auto [s, wasInserted] = insert(n, f);
724722
if (wasInserted) {
725723
replaceSymbol<LazyObject>(s, f, n);
726724
return;
727725
}
728726
auto *u = dyn_cast<Undefined>(s);
729-
if (!u || (u->weakAlias && !u->isECAlias(ctx.config.machine)) ||
730-
s->pendingArchiveLoad)
727+
if (!u || (u->weakAlias && !u->isECAlias(machine)) || s->pendingArchiveLoad)
731728
return;
732729
s->pendingArchiveLoad = true;
733730
f->lazy = false;
@@ -939,7 +936,7 @@ Symbol *SymbolTable::find(StringRef name) const {
939936
}
940937

941938
Symbol *SymbolTable::findUnderscore(StringRef name) const {
942-
if (ctx.config.machine == I386)
939+
if (machine == I386)
943940
return find(("_" + name).str());
944941
return find(name);
945942
}
@@ -986,7 +983,7 @@ Symbol *SymbolTable::findMangle(StringRef name) {
986983
};
987984

988985
// For non-x86, just look for C++ functions.
989-
if (ctx.config.machine != I386)
986+
if (machine != I386)
990987
return findByPrefix("?" + name + "@@Y");
991988

992989
if (!name.starts_with("_"))

lld/COFF/SymbolTable.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ class SymbolTable {
120120
uint32_t newSectionOffset = 0);
121121

122122
COFFLinkerContext &ctx;
123+
llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN;
124+
125+
bool isEC() const { return machine == ARM64EC; }
123126

124127
// A list of chunks which to be added to .rdata.
125128
std::vector<Chunk *> localImportChunks;

0 commit comments

Comments
 (0)