Skip to content

Commit 2d5bfa6

Browse files
cjacekSquallATF
authored andcommitted
[LLD][COFF] Add support for autoimports on ARM64X (llvm#129282)
1 parent 058d6e9 commit 2d5bfa6

File tree

3 files changed

+118
-39
lines changed

3 files changed

+118
-39
lines changed

lld/COFF/Driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2600,7 +2600,7 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
26002600
// If it ends up pulling in more object files from static libraries,
26012601
// (and maybe doing more stdcall fixups along the way), this would need
26022602
// to loop these two calls.
2603-
ctx.symtab.loadMinGWSymbols();
2603+
ctx.forEachSymtab([](SymbolTable &symtab) { symtab.loadMinGWSymbols(); });
26042604
run();
26052605
}
26062606

lld/COFF/Writer.cpp

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,49 +2275,53 @@ void Writer::createECChunks() {
22752275
// uses for fixing them up, and provide the synthetic symbols that the
22762276
// runtime uses for finding the table.
22772277
void Writer::createRuntimePseudoRelocs() {
2278-
std::vector<RuntimePseudoReloc> rels;
2278+
ctx.forEachSymtab([&](SymbolTable &symtab) {
2279+
std::vector<RuntimePseudoReloc> rels;
22792280

2280-
for (Chunk *c : ctx.driver.getChunks()) {
2281-
auto *sc = dyn_cast<SectionChunk>(c);
2282-
if (!sc || !sc->live)
2283-
continue;
2284-
// Don't create pseudo relocations for sections that won't be
2285-
// mapped at runtime.
2286-
if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2287-
continue;
2288-
sc->getRuntimePseudoRelocs(rels);
2289-
}
2281+
for (Chunk *c : ctx.driver.getChunks()) {
2282+
auto *sc = dyn_cast<SectionChunk>(c);
2283+
if (!sc || !sc->live || &sc->file->symtab != &symtab)
2284+
continue;
2285+
// Don't create pseudo relocations for sections that won't be
2286+
// mapped at runtime.
2287+
if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2288+
continue;
2289+
sc->getRuntimePseudoRelocs(rels);
2290+
}
22902291

2291-
if (!ctx.config.pseudoRelocs) {
2292-
// Not writing any pseudo relocs; if some were needed, error out and
2293-
// indicate what required them.
2294-
for (const RuntimePseudoReloc &rpr : rels)
2295-
Err(ctx) << "automatic dllimport of " << rpr.sym->getName() << " in "
2296-
<< toString(rpr.target->file) << " requires pseudo relocations";
2297-
return;
2298-
}
2292+
if (!ctx.config.pseudoRelocs) {
2293+
// Not writing any pseudo relocs; if some were needed, error out and
2294+
// indicate what required them.
2295+
for (const RuntimePseudoReloc &rpr : rels)
2296+
Err(ctx) << "automatic dllimport of " << rpr.sym->getName() << " in "
2297+
<< toString(rpr.target->file)
2298+
<< " requires pseudo relocations";
2299+
return;
2300+
}
22992301

2300-
if (!rels.empty()) {
2301-
Log(ctx) << "Writing " << rels.size() << " runtime pseudo relocations";
2302-
const char *symbolName = "_pei386_runtime_relocator";
2303-
Symbol *relocator = ctx.symtab.findUnderscore(symbolName);
2304-
if (!relocator)
2305-
Err(ctx)
2306-
<< "output image has runtime pseudo relocations, but the function "
2307-
<< symbolName
2308-
<< " is missing; it is needed for fixing the relocations at runtime";
2309-
}
2302+
if (!rels.empty()) {
2303+
Log(ctx) << "Writing " << Twine(rels.size())
2304+
<< " runtime pseudo relocations";
2305+
const char *symbolName = "_pei386_runtime_relocator";
2306+
Symbol *relocator = symtab.findUnderscore(symbolName);
2307+
if (!relocator)
2308+
Err(ctx)
2309+
<< "output image has runtime pseudo relocations, but the function "
2310+
<< symbolName
2311+
<< " is missing; it is needed for fixing the relocations at "
2312+
"runtime";
2313+
}
23102314

2311-
PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
2312-
rdataSec->addChunk(table);
2313-
EmptyChunk *endOfList = make<EmptyChunk>();
2314-
rdataSec->addChunk(endOfList);
2315+
PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
2316+
rdataSec->addChunk(table);
2317+
EmptyChunk *endOfList = make<EmptyChunk>();
2318+
rdataSec->addChunk(endOfList);
23152319

2316-
Symbol *headSym = ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
2317-
Symbol *endSym =
2318-
ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
2319-
replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
2320-
replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
2320+
Symbol *headSym = symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
2321+
Symbol *endSym = symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
2322+
replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
2323+
replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
2324+
});
23212325
}
23222326

23232327
// MinGW specific.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# REQUIRES: aarch64
2+
RUN: split-file %s %t.dir && cd %t.dir
3+
4+
RUN: llvm-lib -machine:arm64x -out:libtest.a -defArm64Native:test.def -def:test.def
5+
RUN: llvm-mc -triple=arm64ec-windows-gnu arm64ec.s -filetype=obj -o arm64ec.obj
6+
RUN: llvm-mc -triple=aarch64-windows-gnu aarch64.s -filetype=obj -o aarch64.obj
7+
RUN: llvm-mc -filetype=obj -triple=arm64ec-windows %S/Inputs/loadconfig-arm64ec.s -o loadconfig-arm64ec.obj
8+
RUN: llvm-mc -filetype=obj -triple=aarch64-windows %S/Inputs/loadconfig-arm64.s -o loadconfig-arm64.obj
9+
10+
RUN: lld-link -machine:arm64x -out:out.dll -dll -noentry arm64ec.obj aarch64.obj libtest.a loadconfig-arm64.obj loadconfig-arm64ec.obj -lldmingw
11+
12+
RUN: llvm-readobj --coff-imports out.dll | FileCheck -check-prefix=IMPORTS %s
13+
RUN: llvm-objdump -s out.dll | FileCheck --check-prefix=CONTENTS %s
14+
15+
IMPORTS: Import {
16+
IMPORTS-NEXT: Name: test.dll
17+
IMPORTS-NEXT: ImportLookupTableRVA: 0x5{{.*}}
18+
IMPORTS-NEXT: ImportAddressTableRVA: 0x4000
19+
IMPORTS-NEXT: Symbol: variable (0)
20+
IMPORTS-NEXT: }
21+
IMPORTS-NEXT: HybridObject {
22+
IMPORTS: Import {
23+
IMPORTS-NEXT: Name: test.dll
24+
IMPORTS-NEXT: ImportLookupTableRVA: 0x5{{.*}}
25+
IMPORTS-NEXT: ImportAddressTableRVA: 0x4000
26+
IMPORTS-NEXT: Symbol: variable (0)
27+
IMPORTS-NEXT: }
28+
IMPORTS-NEXT: }
29+
30+
Native ARM64 runtime pseudo relocation list header at 0x5164, consisting of 0x0, 0x0, 0x1.
31+
The runtime pseudo relocation is from an aarch64.obj object file, with import from 0x4000,
32+
applied at 0x9018 with a size of 64 bits. ARM64EC runtime pseudo relocation list header at
33+
0x517c, consisting of 0x0, 0x0, 0x1. The runtime pseudo relocation is from arm64ec.obj
34+
object file, with import from 0x4000, applied at 0x9000 with a size of 64 bits.
35+
36+
CONTENTS: Contents of section .rdata:
37+
CONTENTS: 180005160 00300000 00000000 00000000 01000000
38+
CONTENTS: 180005170 00400000 18900000 40000000 00000000
39+
CONTENTS: 180005180 00000000 01000000 00400000 00900000
40+
CONTENTS: 180005190 40000000
41+
42+
CONTENTS: Contents of section .test:
43+
CONTENTS-NEXT: 180009000 00400080 01000000 7c510080 01000000
44+
CONTENTS-NEXT: 180009010 94510080 01000000 00400080 01000000
45+
CONTENTS-NEXT: 180009020 64510080 01000000 7c510080 01000000
46+
47+
#--- arm64ec.s
48+
.text
49+
.global "#_pei386_runtime_relocator"
50+
"#_pei386_runtime_relocator":
51+
ret
52+
53+
.weak_anti_dep _pei386_runtime_relocator
54+
.set _pei386_runtime_relocator,"#_pei386_runtime_relocator"
55+
56+
.section .test,"dr"
57+
.quad variable
58+
.quad __RUNTIME_PSEUDO_RELOC_LIST__
59+
.quad __RUNTIME_PSEUDO_RELOC_LIST_END__
60+
61+
#--- aarch64.s
62+
.text
63+
.global _pei386_runtime_relocator
64+
_pei386_runtime_relocator:
65+
ret
66+
67+
.section .test,"dr"
68+
.quad variable
69+
.quad __RUNTIME_PSEUDO_RELOC_LIST__
70+
.quad __RUNTIME_PSEUDO_RELOC_LIST_END__
71+
72+
#--- test.def
73+
LIBRARY test.dll
74+
EXPORTS
75+
variable DATA

0 commit comments

Comments
 (0)