Skip to content

Commit c425db2

Browse files
authored
[lld] Mark target section as code section when merging code sections into a data section. (#72030)
1 parent a3d76b3 commit c425db2

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lld/COFF/Writer.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,14 @@ void OutputSection::merge(OutputSection *other) {
347347
contribSections.insert(contribSections.end(), other->contribSections.begin(),
348348
other->contribSections.end());
349349
other->contribSections.clear();
350+
351+
// MS link.exe compatibility: when merging a code section into a data section,
352+
// mark the target section as a code section.
353+
if (other->header.Characteristics & IMAGE_SCN_CNT_CODE) {
354+
header.Characteristics |= IMAGE_SCN_CNT_CODE;
355+
header.Characteristics &=
356+
~(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA);
357+
}
350358
}
351359

352360
// Write the section header to a given buffer.

lld/test/COFF/code-merge.s

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
# CHECK-NEXT: PointerToLineNumbers: 0x0
1919
# CHECK-NEXT: RelocationCount: 0
2020
# CHECK-NEXT: LineNumberCount: 0
21-
# CHECK-NEXT: Characteristics [ (0xC0000080)
22-
# CHECK-NEXT: IMAGE_SCN_CNT_UNINITIALIZED_DATA (0x80)
21+
# CHECK-NEXT: Characteristics [ (0xC0000020)
22+
# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
2323
# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
2424
# CHECK-NEXT: IMAGE_SCN_MEM_WRITE (0x80000000)
2525
# CHECK-NEXT: ]
@@ -35,8 +35,8 @@
3535
# CHECK-NEXT: PointerToLineNumbers: 0x0
3636
# CHECK-NEXT: RelocationCount: 0
3737
# CHECK-NEXT: LineNumberCount: 0
38-
# CHECK-NEXT: Characteristics [ (0x40000040)
39-
# CHECK-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
38+
# CHECK-NEXT: Characteristics [ (0x40000020)
39+
# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
4040
# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
4141
# CHECK-NEXT: ]
4242
# CHECK-NEXT: }
@@ -77,7 +77,48 @@
7777
# CHECK-NEXT: ]
7878

7979
# RUN: llvm-objdump -d %t.dll | FileCheck -check-prefix=DISASM %s
80-
# DISASM: Disassembly of section .testx3:
80+
# DISASM: Disassembly of section .testbss:
81+
# DISASM-EMPTY:
82+
# DISASM-NEXT: 0000000180001000 <.testbss>:
83+
# DISASM-NEXT: 180001000: 00 00 addb %al, (%rax)
84+
# DISASM-NEXT: 180001002: 00 00 addb %al, (%rax)
85+
# DISASM-NEXT: 180001004: cc int3
86+
# DISASM-NEXT: 180001005: cc int3
87+
# DISASM-NEXT: 180001006: cc int3
88+
# DISASM-NEXT: 180001007: cc int3
89+
# DISASM-NEXT: 180001008: cc int3
90+
# DISASM-NEXT: 180001009: cc int3
91+
# DISASM-NEXT: 18000100a: cc int3
92+
# DISASM-NEXT: 18000100b: cc int3
93+
# DISASM-NEXT: 18000100c: cc int3
94+
# DISASM-NEXT: 18000100d: cc int3
95+
# DISASM-NEXT: 18000100e: cc int3
96+
# DISASM-NEXT: 18000100f: cc int3
97+
# DISASM-NEXT: 180001010: 48 c7 c0 02 00 00 00 movq $0x2, %rax
98+
# DISASM-NEXT: 180001017: c3 retq
99+
# DISASM-EMPTY:
100+
# DISASM-NEXT: Disassembly of section .testd:
101+
# DISASM-EMPTY:
102+
# DISASM-NEXT: 0000000180002000 <.testd>:
103+
# DISASM-NEXT: 180002000: 01 00 addl %eax, (%rax)
104+
# DISASM-NEXT: 180002002: cc int3
105+
# DISASM-NEXT: 180002003: cc int3
106+
# DISASM-NEXT: 180002004: cc int3
107+
# DISASM-NEXT: 180002005: cc int3
108+
# DISASM-NEXT: 180002006: cc int3
109+
# DISASM-NEXT: 180002007: cc int3
110+
# DISASM-NEXT: 180002008: cc int3
111+
# DISASM-NEXT: 180002009: cc int3
112+
# DISASM-NEXT: 18000200a: cc int3
113+
# DISASM-NEXT: 18000200b: cc int3
114+
# DISASM-NEXT: 18000200c: cc int3
115+
# DISASM-NEXT: 18000200d: cc int3
116+
# DISASM-NEXT: 18000200e: cc int3
117+
# DISASM-NEXT: 18000200f: cc int3
118+
# DISASM-NEXT: 180002010: 48 c7 c0 01 00 00 00 movq $0x1, %rax
119+
# DISASM-NEXT: 180002017: c3 retq
120+
# DISASM-EMPTY:
121+
# DISASM-NEXT: Disassembly of section .testx3:
81122
# DISASM-EMPTY:
82123
# DISASM-NEXT: 0000000180003000 <.testx3>:
83124
# DISASM-NEXT: 180003000: 48 c7 c0 03 00 00 00 movq $0x3, %rax

0 commit comments

Comments
 (0)