Skip to content

Commit c605431

Browse files
authored
[lld] Sort data chunks before code chunks on ARM64EC. (#70722)
1 parent 24228ae commit c605431

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

lld/COFF/Chunks.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class Chunk {
116116
bool isHotPatchable() const;
117117

118118
MachineTypes getMachine() const;
119-
chpe_range_type getArm64ECRangeType() const;
119+
std::optional<chpe_range_type> getArm64ECRangeType() const;
120120

121121
protected:
122122
Chunk(Kind k = OtherKind) : chunkKind(k), hasData(true), p2Align(0) {}
@@ -437,7 +437,11 @@ inline MachineTypes Chunk::getMachine() const {
437437
return static_cast<const NonSectionChunk *>(this)->getMachine();
438438
}
439439

440-
inline chpe_range_type Chunk::getArm64ECRangeType() const {
440+
inline std::optional<chpe_range_type> Chunk::getArm64ECRangeType() const {
441+
// Data sections don't need codemap entries.
442+
if (!(getOutputCharacteristics() & llvm::COFF::IMAGE_SCN_MEM_EXECUTE))
443+
return std::nullopt;
444+
441445
switch (getMachine()) {
442446
case AMD64:
443447
return chpe_range_type::Amd64;

lld/COFF/Writer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,9 @@ void Writer::sortECChunks() {
13891389
for (OutputSection *sec : ctx.outputSections) {
13901390
if (sec->isCodeSection())
13911391
llvm::stable_sort(sec->chunks, [=](const Chunk *a, const Chunk *b) {
1392-
return a->getArm64ECRangeType() < b->getArm64ECRangeType();
1392+
std::optional<chpe_range_type> aType = a->getArm64ECRangeType(),
1393+
bType = b->getArm64ECRangeType();
1394+
return !aType || (bType && *aType < *bType);
13931395
});
13941396
}
13951397
}

lld/test/COFF/arm64ec-codemap.test

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,40 @@ DISASMM-NEXT: ...
110110
DISASMM-NEXT: 180002ffe: 00 00 addb %al, (%rax)
111111
DISASMM-NEXT: 180003000: b8 06 00 00 00 movl $0x6, %eax
112112

113+
Merging data sections into code sections causes data to be separated from the code when sorting chunks.
114+
115+
RUN: lld-link -out:testdm.dll -machine:arm64ec arm64ec-func-sym.obj x86_64-func-sym.obj codemap.obj \
116+
RUN: data-sec.obj loadconfig-arm64ec.obj -dll -noentry -merge:.testdata=.text -merge:.rdata=test
117+
118+
RUN: llvm-readobj --coff-load-config testdm.dll | FileCheck -check-prefix=CODEMAPDM %s
119+
CODEMAPDM: CodeMap [
120+
CODEMAPDM-NEXT: 0x2000 - 0x2008 ARM64EC
121+
CODEMAPDM-NEXT: 0x3000 - 0x3006 X64
122+
CODEMAPDM-NEXT: 0x5200 - 0x5208 ARM64EC
123+
CODEMAPDM-NEXT: 0x6000 - 0x6006 X64
124+
CODEMAPDM-NEXT: ]
125+
126+
RUN: llvm-objdump -d testdm.dll | FileCheck -check-prefix=DISASMDM %s
127+
DISASMDM: Disassembly of section .text:
128+
DISASMDM-EMPTY:
129+
DISASMDM-NEXT: 0000000180001000 <.text>:
130+
DISASMDM-NEXT: 180001000: 00000001 udf #0x1
131+
DISASMDM-NEXT: ...
132+
DISASMDM-NEXT: 180002000: 52800040 mov w0, #0x2
133+
DISASMDM-NEXT: 180002004: d65f03c0 ret
134+
DISASMDM-NEXT: ...
135+
DISASMDM-NEXT: 180003000: b8 03 00 00 00 movl $0x3, %eax
136+
DISASMDM-NEXT: 180003005: c3 retq
137+
DISASMDM-EMPTY:
138+
DISASMDM-NEXT: Disassembly of section test:
139+
DISASMDM-EMPTY:
140+
DISASMDM-NEXT: 0000000180005000 <test>:
141+
DISASMDM: 180005200: 528000a0 mov w0, #0x5
142+
DISASMDM-NEXT: 180005204: d65f03c0 ret
143+
DISASMDM-NEXT: ...
144+
DISASMDM-NEXT: 180006000: b8 06 00 00 00 movl $0x6, %eax
145+
DISASMDM-NEXT: 180006005: c3 retq
146+
113147
#--- arm64-func-sym.s
114148
.text
115149
.globl arm64_func_sym
@@ -148,6 +182,10 @@ x86_64_func_sym2:
148182
movl $6, %eax
149183
retq
150184

185+
#--- data-sec.s
186+
.section .testdata, "rd"
187+
.xword 1
188+
151189
#--- codemap.s
152190
.section .rdata,"dr"
153191
.globl code_map

0 commit comments

Comments
 (0)