Skip to content

Commit c6598f6

Browse files
authored
[LLD][COFF] Add support for autoimports on ARM64X (#129282)
1 parent f858ac7 commit c6598f6

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
@@ -2288,49 +2288,53 @@ void Writer::createECChunks() {
22882288
// uses for fixing them up, and provide the synthetic symbols that the
22892289
// runtime uses for finding the table.
22902290
void Writer::createRuntimePseudoRelocs() {
2291-
std::vector<RuntimePseudoReloc> rels;
2291+
ctx.forEachSymtab([&](SymbolTable &symtab) {
2292+
std::vector<RuntimePseudoReloc> rels;
22922293

2293-
for (Chunk *c : ctx.driver.getChunks()) {
2294-
auto *sc = dyn_cast<SectionChunk>(c);
2295-
if (!sc || !sc->live)
2296-
continue;
2297-
// Don't create pseudo relocations for sections that won't be
2298-
// mapped at runtime.
2299-
if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2300-
continue;
2301-
sc->getRuntimePseudoRelocs(rels);
2302-
}
2294+
for (Chunk *c : ctx.driver.getChunks()) {
2295+
auto *sc = dyn_cast<SectionChunk>(c);
2296+
if (!sc || !sc->live || &sc->file->symtab != &symtab)
2297+
continue;
2298+
// Don't create pseudo relocations for sections that won't be
2299+
// mapped at runtime.
2300+
if (sc->header->Characteristics & IMAGE_SCN_MEM_DISCARDABLE)
2301+
continue;
2302+
sc->getRuntimePseudoRelocs(rels);
2303+
}
23032304

2304-
if (!ctx.config.pseudoRelocs) {
2305-
// Not writing any pseudo relocs; if some were needed, error out and
2306-
// indicate what required them.
2307-
for (const RuntimePseudoReloc &rpr : rels)
2308-
Err(ctx) << "automatic dllimport of " << rpr.sym->getName() << " in "
2309-
<< toString(rpr.target->file) << " requires pseudo relocations";
2310-
return;
2311-
}
2305+
if (!ctx.config.pseudoRelocs) {
2306+
// Not writing any pseudo relocs; if some were needed, error out and
2307+
// indicate what required them.
2308+
for (const RuntimePseudoReloc &rpr : rels)
2309+
Err(ctx) << "automatic dllimport of " << rpr.sym->getName() << " in "
2310+
<< toString(rpr.target->file)
2311+
<< " requires pseudo relocations";
2312+
return;
2313+
}
23122314

2313-
if (!rels.empty()) {
2314-
Log(ctx) << "Writing " << rels.size() << " runtime pseudo relocations";
2315-
const char *symbolName = "_pei386_runtime_relocator";
2316-
Symbol *relocator = ctx.symtab.findUnderscore(symbolName);
2317-
if (!relocator)
2318-
Err(ctx)
2319-
<< "output image has runtime pseudo relocations, but the function "
2320-
<< symbolName
2321-
<< " is missing; it is needed for fixing the relocations at runtime";
2322-
}
2315+
if (!rels.empty()) {
2316+
Log(ctx) << "Writing " << Twine(rels.size())
2317+
<< " runtime pseudo relocations";
2318+
const char *symbolName = "_pei386_runtime_relocator";
2319+
Symbol *relocator = symtab.findUnderscore(symbolName);
2320+
if (!relocator)
2321+
Err(ctx)
2322+
<< "output image has runtime pseudo relocations, but the function "
2323+
<< symbolName
2324+
<< " is missing; it is needed for fixing the relocations at "
2325+
"runtime";
2326+
}
23232327

2324-
PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
2325-
rdataSec->addChunk(table);
2326-
EmptyChunk *endOfList = make<EmptyChunk>();
2327-
rdataSec->addChunk(endOfList);
2328+
PseudoRelocTableChunk *table = make<PseudoRelocTableChunk>(rels);
2329+
rdataSec->addChunk(table);
2330+
EmptyChunk *endOfList = make<EmptyChunk>();
2331+
rdataSec->addChunk(endOfList);
23282332

2329-
Symbol *headSym = ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
2330-
Symbol *endSym =
2331-
ctx.symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
2332-
replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
2333-
replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
2333+
Symbol *headSym = symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST__");
2334+
Symbol *endSym = symtab.findUnderscore("__RUNTIME_PSEUDO_RELOC_LIST_END__");
2335+
replaceSymbol<DefinedSynthetic>(headSym, headSym->getName(), table);
2336+
replaceSymbol<DefinedSynthetic>(endSym, endSym->getName(), endOfList);
2337+
});
23342338
}
23352339

23362340
// 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)