Skip to content

Commit 52a7116

Browse files
authored
[LLD][COFF] Add support for CHPE code ranges metadata. (#105741)
This is part of CHPE metadata containing a sorted list of x86_64 export thunks RVAs and sizes.
1 parent a0fac6f commit 52a7116

File tree

7 files changed

+51
-5
lines changed

7 files changed

+51
-5
lines changed

lld/COFF/Chunks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,22 @@ void ECExportThunkChunk::writeTo(uint8_t *buf) const {
10781078
write32le(buf + 10, target->getRVA() - rva - 14);
10791079
}
10801080

1081+
size_t CHPECodeRangesChunk::getSize() const {
1082+
return exportThunks.size() * sizeof(chpe_code_range_entry);
1083+
}
1084+
1085+
void CHPECodeRangesChunk::writeTo(uint8_t *buf) const {
1086+
auto ranges = reinterpret_cast<chpe_code_range_entry *>(buf);
1087+
1088+
for (uint32_t i = 0; i < exportThunks.size(); i++) {
1089+
Chunk *thunk = exportThunks[i].first;
1090+
uint32_t start = thunk->getRVA();
1091+
ranges[i].StartRva = start;
1092+
ranges[i].EndRva = start + thunk->getSize();
1093+
ranges[i].EntryPoint = start;
1094+
}
1095+
}
1096+
10811097
size_t CHPERedirectionChunk::getSize() const {
10821098
return exportThunks.size() * sizeof(chpe_redirection_entry);
10831099
}

lld/COFF/Chunks.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,17 @@ class ECCodeMapChunk : public NonSectionChunk {
749749
std::vector<ECCodeMapEntry> &map;
750750
};
751751

752+
class CHPECodeRangesChunk : public NonSectionChunk {
753+
public:
754+
CHPECodeRangesChunk(std::vector<std::pair<Chunk *, Defined *>> &exportThunks)
755+
: exportThunks(exportThunks) {}
756+
size_t getSize() const override;
757+
void writeTo(uint8_t *buf) const override;
758+
759+
private:
760+
std::vector<std::pair<Chunk *, Defined *>> &exportThunks;
761+
};
762+
752763
class CHPERedirectionChunk : public NonSectionChunk {
753764
public:
754765
CHPERedirectionChunk(std::vector<std::pair<Chunk *, Defined *>> &exportThunks)

lld/COFF/Driver.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,6 +2444,8 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
24442444
ctx.symtab.addAbsolute("__arm64x_redirection_metadata_count", 0);
24452445
ctx.symtab.addAbsolute("__hybrid_code_map", 0);
24462446
ctx.symtab.addAbsolute("__hybrid_code_map_count", 0);
2447+
ctx.symtab.addAbsolute("__x64_code_ranges_to_entry_points", 0);
2448+
ctx.symtab.addAbsolute("__x64_code_ranges_to_entry_points_count", 0);
24472449
}
24482450

24492451
if (config->pseudoRelocs) {

lld/COFF/Writer.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,12 @@ void Writer::createECChunks() {
20712071
replaceSymbol<DefinedSynthetic>(codeMapSym, codeMapSym->getName(),
20722072
codeMapChunk);
20732073

2074+
CHPECodeRangesChunk *ranges = make<CHPECodeRangesChunk>(exportThunks);
2075+
rdataSec->addChunk(ranges);
2076+
Symbol *rangesSym =
2077+
ctx.symtab.findUnderscore("__x64_code_ranges_to_entry_points");
2078+
replaceSymbol<DefinedSynthetic>(rangesSym, rangesSym->getName(), ranges);
2079+
20742080
CHPERedirectionChunk *entryPoints = make<CHPERedirectionChunk>(exportThunks);
20752081
a64xrmSec->addChunk(entryPoints);
20762082
Symbol *entryPointsSym =
@@ -2186,6 +2192,10 @@ void Writer::setECSymbols() {
21862192
pdata.first->getRVA());
21872193
}
21882194

2195+
Symbol *rangesCountSym =
2196+
ctx.symtab.findUnderscore("__x64_code_ranges_to_entry_points_count");
2197+
cast<DefinedAbsolute>(rangesCountSym)->setVA(exportThunks.size());
2198+
21892199
Symbol *entryPointCountSym =
21902200
ctx.symtab.findUnderscore("__arm64x_redirection_metadata_count");
21912201
cast<DefinedAbsolute>(entryPointCountSym)->setVA(exportThunks.size());

lld/test/COFF/Inputs/loadconfig-arm64ec.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ __chpe_metadata:
6666
.word 1
6767
.rva __hybrid_code_map
6868
.word __hybrid_code_map_count
69-
.word 0 // __x64_code_ranges_to_entry_points
69+
.rva __x64_code_ranges_to_entry_points
7070
.rva __arm64x_redirection_metadata
7171
.rva __os_arm64x_dispatch_call_no_redirect
7272
.rva __os_arm64x_dispatch_ret
@@ -75,7 +75,7 @@ __chpe_metadata:
7575
.rva __os_arm64x_check_icall_cfg
7676
.word 0 // __arm64x_native_entrypoint
7777
.word 0 // __hybrid_auxiliary_iat
78-
.word 0 // __x64_code_ranges_to_entry_points_count
78+
.word __x64_code_ranges_to_entry_points_count
7979
.word __arm64x_redirection_metadata_count
8080
.rva __os_arm64x_get_x64_information
8181
.rva __os_arm64x_set_x64_information

lld/test/COFF/arm64ec-export-thunks.test

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ EXP-CHPE: CodeMap [
5858
EXP-CHPE-NEXT: 0x1000 - 0x100C ARM64EC
5959
EXP-CHPE-NEXT: 0x2000 - 0x3020 X64
6060
EXP-CHPE-NEXT: ]
61-
EXP-CHPE-NEXT: CodeRangesToEntryPoints: 0
61+
EXP-CHPE-NEXT: CodeRangesToEntryPoints [
62+
EXP-CHPE-NEXT: 0x3000 - 0x3010 -> 0x3000
63+
EXP-CHPE-NEXT: 0x3010 - 0x3020 -> 0x3010
64+
EXP-CHPE-NEXT: ]
6265
EXP-CHPE-NEXT: RedirectionMetadata [
6366
EXP-CHPE-NEXT: 0x3000 -> 0x1000
6467
EXP-CHPE-NEXT: 0x3010 -> 0x1000
@@ -121,7 +124,9 @@ ENTRY-CHPE: CodeMap [
121124
ENTRY-CHPE-NEXT: 0x1000 - 0x100C ARM64EC
122125
ENTRY-CHPE-NEXT: 0x2000 - 0x2010 X64
123126
ENTRY-CHPE-NEXT: ]
124-
ENTRY-CHPE-NEXT: CodeRangesToEntryPoints: 0
127+
ENTRY-CHPE-NEXT: CodeRangesToEntryPoints [
128+
ENTRY-CHPE-NEXT: 0x2000 - 0x2010 -> 0x2000
129+
ENTRY-CHPE-NEXT: ]
125130
ENTRY-CHPE-NEXT: RedirectionMetadata [
126131
ENTRY-CHPE-NEXT: 0x2000 -> 0x1000
127132
ENTRY-CHPE-NEXT: ]

lld/test/COFF/arm64ec-patchable-thunks.test

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ PATCH-CHPE: CodeMap [
3434
PATCH-CHPE-NEXT: 0x1000 - 0x1008 ARM64EC
3535
PATCH-CHPE-NEXT: 0x2000 - 0x2010 X64
3636
PATCH-CHPE-NEXT: ]
37-
PATCH-CHPE-NEXT: CodeRangesToEntryPoints: 0
37+
PATCH-CHPE-NEXT: CodeRangesToEntryPoints [
38+
PATCH-CHPE-NEXT: 0x2000 - 0x2010 -> 0x2000
39+
PATCH-CHPE-NEXT: ]
3840
PATCH-CHPE-NEXT: RedirectionMetadata [
3941
PATCH-CHPE-NEXT: 0x2000 -> 0x1000
4042
PATCH-CHPE-NEXT: ]

0 commit comments

Comments
 (0)