Skip to content

[lld] Mark target section as code section when merging code sections into a data section. #72030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lld/COFF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,14 @@ void OutputSection::merge(OutputSection *other) {
contribSections.insert(contribSections.end(), other->contribSections.begin(),
other->contribSections.end());
other->contribSections.clear();

// MS link.exe compatibility: when merging a code section into a data section,
// mark the target section as a code section.
if (other->header.Characteristics & IMAGE_SCN_CNT_CODE) {
header.Characteristics |= IMAGE_SCN_CNT_CODE;
header.Characteristics &=
~(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA);
}
}

// Write the section header to a given buffer.
Expand Down
51 changes: 46 additions & 5 deletions lld/test/COFF/code-merge.s
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
# CHECK-NEXT: PointerToLineNumbers: 0x0
# CHECK-NEXT: RelocationCount: 0
# CHECK-NEXT: LineNumberCount: 0
# CHECK-NEXT: Characteristics [ (0xC0000080)
# CHECK-NEXT: IMAGE_SCN_CNT_UNINITIALIZED_DATA (0x80)
# CHECK-NEXT: Characteristics [ (0xC0000020)
# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
# CHECK-NEXT: IMAGE_SCN_MEM_WRITE (0x80000000)
# CHECK-NEXT: ]
Expand All @@ -35,8 +35,8 @@
# CHECK-NEXT: PointerToLineNumbers: 0x0
# CHECK-NEXT: RelocationCount: 0
# CHECK-NEXT: LineNumberCount: 0
# CHECK-NEXT: Characteristics [ (0x40000040)
# CHECK-NEXT: IMAGE_SCN_CNT_INITIALIZED_DATA (0x40)
# CHECK-NEXT: Characteristics [ (0x40000020)
# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
# CHECK-NEXT: ]
# CHECK-NEXT: }
Expand Down Expand Up @@ -77,7 +77,48 @@
# CHECK-NEXT: ]

# RUN: llvm-objdump -d %t.dll | FileCheck -check-prefix=DISASM %s
# DISASM: Disassembly of section .testx3:
# DISASM: Disassembly of section .testbss:
# DISASM-EMPTY:
# DISASM-NEXT: 0000000180001000 <.testbss>:
# DISASM-NEXT: 180001000: 00 00 addb %al, (%rax)
# DISASM-NEXT: 180001002: 00 00 addb %al, (%rax)
# DISASM-NEXT: 180001004: cc int3
# DISASM-NEXT: 180001005: cc int3
# DISASM-NEXT: 180001006: cc int3
# DISASM-NEXT: 180001007: cc int3
# DISASM-NEXT: 180001008: cc int3
# DISASM-NEXT: 180001009: cc int3
# DISASM-NEXT: 18000100a: cc int3
# DISASM-NEXT: 18000100b: cc int3
# DISASM-NEXT: 18000100c: cc int3
# DISASM-NEXT: 18000100d: cc int3
# DISASM-NEXT: 18000100e: cc int3
# DISASM-NEXT: 18000100f: cc int3
# DISASM-NEXT: 180001010: 48 c7 c0 02 00 00 00 movq $0x2, %rax
# DISASM-NEXT: 180001017: c3 retq
# DISASM-EMPTY:
# DISASM-NEXT: Disassembly of section .testd:
# DISASM-EMPTY:
# DISASM-NEXT: 0000000180002000 <.testd>:
# DISASM-NEXT: 180002000: 01 00 addl %eax, (%rax)
# DISASM-NEXT: 180002002: cc int3
# DISASM-NEXT: 180002003: cc int3
# DISASM-NEXT: 180002004: cc int3
# DISASM-NEXT: 180002005: cc int3
# DISASM-NEXT: 180002006: cc int3
# DISASM-NEXT: 180002007: cc int3
# DISASM-NEXT: 180002008: cc int3
# DISASM-NEXT: 180002009: cc int3
# DISASM-NEXT: 18000200a: cc int3
# DISASM-NEXT: 18000200b: cc int3
# DISASM-NEXT: 18000200c: cc int3
# DISASM-NEXT: 18000200d: cc int3
# DISASM-NEXT: 18000200e: cc int3
# DISASM-NEXT: 18000200f: cc int3
# DISASM-NEXT: 180002010: 48 c7 c0 01 00 00 00 movq $0x1, %rax
# DISASM-NEXT: 180002017: c3 retq
# DISASM-EMPTY:
# DISASM-NEXT: Disassembly of section .testx3:
# DISASM-EMPTY:
# DISASM-NEXT: 0000000180003000 <.testx3>:
# DISASM-NEXT: 180003000: 48 c7 c0 03 00 00 00 movq $0x3, %rax
Expand Down