[lld] [lld] Mark target section as code section when merging code sections into a data section. (PR #72030)
Jacek Caban via llvm-commits
llvm-commits at lists.llvm.org
Sun Nov 12 12:37:55 PST 2023
https://github.com/cjacek updated https://github.com/llvm/llvm-project/pull/72030
>From 889c45baf1b00df6286f0dfe4bdb577cd94ed42c Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Sat, 11 Nov 2023 15:57:48 +0100
Subject: [PATCH 1/3] [lld] Mark target section as code section when merging
code sections into a data section.
---
lld/COFF/Writer.cpp | 8 ++++++
lld/test/COFF/code-merge.s | 56 ++++++++++++++++++++++++++++++++++++++
2 files changed, 64 insertions(+)
create mode 100644 lld/test/COFF/code-merge.s
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 960328d686852a3..8c0499b9ad7a14c 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -346,6 +346,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.
diff --git a/lld/test/COFF/code-merge.s b/lld/test/COFF/code-merge.s
new file mode 100644
index 000000000000000..5728fbc93ff9b9d
--- /dev/null
+++ b/lld/test/COFF/code-merge.s
@@ -0,0 +1,56 @@
+# REQUIRES: x86
+
+# Test that merging code section into a data section changes its characteristics.
+
+# RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
+# RUN: lld-link -machine:amd64 -dll -noentry -out:%t.dll %t.obj -merge:.testx=.testd -merge:.testx2=.testbss
+# RUN: llvm-readobj --sections %t.dll | FileCheck %s
+
+# CHECK: Sections [
+# CHECK-NEXT: Section {
+# CHECK-NEXT: Number: 1
+# CHECK-NEXT: Name: .testbss (2E 74 65 73 74 62 73 73)
+# CHECK-NEXT: VirtualSize: 0xA
+# CHECK-NEXT: VirtualAddress: 0x1000
+# CHECK-NEXT: RawDataSize: 512
+# CHECK-NEXT: PointerToRawData: 0x400
+# CHECK-NEXT: PointerToRelocations: 0x0
+# CHECK-NEXT: PointerToLineNumbers: 0x0
+# CHECK-NEXT: RelocationCount: 0
+# CHECK-NEXT: LineNumberCount: 0
+# CHECK-NEXT: Characteristics [ (0x40000020)
+# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
+# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
+# CHECK-NEXT: ]
+# CHECK-NEXT: }
+# CHECK-NEXT: Section {
+# CHECK-NEXT: Number: 2
+# CHECK-NEXT: Name: .testd (2E 74 65 73 74 64 00 00)
+# CHECK-NEXT: VirtualSize: 0xA
+# CHECK-NEXT: VirtualAddress: 0x2000
+# CHECK-NEXT: RawDataSize: 512
+# CHECK-NEXT: PointerToRawData: 0x600
+# CHECK-NEXT: PointerToRelocations: 0x0
+# CHECK-NEXT: PointerToLineNumbers: 0x0
+# CHECK-NEXT: RelocationCount: 0
+# CHECK-NEXT: LineNumberCount: 0
+# CHECK-NEXT: Characteristics [ (0x40000020)
+# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
+# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
+# CHECK-NEXT: ]
+# CHECK-NEXT: }
+# CHECK-NEXT: ]
+
+ .section .testx, "xr"
+ movq $1, %rax
+ retq
+
+ .section .testx2, "xr"
+ movq $2, %rax
+ retq
+
+ .section .testd, "dr"
+ .word 1
+
+ .section .testbss, "br"
+ .word 0
>From 1270b910bae60d220afcccc21d4ba108ab2e4374 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Sat, 11 Nov 2023 22:24:40 +0100
Subject: [PATCH 2/3] Fix uninitialized data handlign
---
lld/COFF/Writer.cpp | 2 +-
lld/test/COFF/code-merge.s | 9 +++++----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 8c0499b9ad7a14c..e55d64f82d130ea 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -352,7 +352,7 @@ void OutputSection::merge(OutputSection *other) {
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;
+ ~(IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_CNT_UNINITIALIZED_DATA);
}
}
diff --git a/lld/test/COFF/code-merge.s b/lld/test/COFF/code-merge.s
index 5728fbc93ff9b9d..ed50a870f052b8f 100644
--- a/lld/test/COFF/code-merge.s
+++ b/lld/test/COFF/code-merge.s
@@ -10,7 +10,7 @@
# CHECK-NEXT: Section {
# CHECK-NEXT: Number: 1
# CHECK-NEXT: Name: .testbss (2E 74 65 73 74 62 73 73)
-# CHECK-NEXT: VirtualSize: 0xA
+# CHECK-NEXT: VirtualSize: 0xC
# CHECK-NEXT: VirtualAddress: 0x1000
# CHECK-NEXT: RawDataSize: 512
# CHECK-NEXT: PointerToRawData: 0x400
@@ -18,9 +18,10 @@
# CHECK-NEXT: PointerToLineNumbers: 0x0
# CHECK-NEXT: RelocationCount: 0
# CHECK-NEXT: LineNumberCount: 0
-# CHECK-NEXT: Characteristics [ (0x40000020)
+# 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: ]
# CHECK-NEXT: }
# CHECK-NEXT: Section {
@@ -52,5 +53,5 @@
.section .testd, "dr"
.word 1
- .section .testbss, "br"
- .word 0
+ .section .testbss, "b"
+ .skip 4
>From 3fcd4246323d4ad11bded65a03f0915b371356b3 Mon Sep 17 00:00:00 2001
From: Jacek Caban <jacek at codeweavers.com>
Date: Sun, 12 Nov 2023 13:35:11 +0100
Subject: [PATCH 3/3] Add data to code merge tests
---
lld/test/COFF/code-merge.s | 50 +++++++++++++++++++++++++++++++++++++-
1 file changed, 49 insertions(+), 1 deletion(-)
diff --git a/lld/test/COFF/code-merge.s b/lld/test/COFF/code-merge.s
index ed50a870f052b8f..2c6d6dc354c0f4b 100644
--- a/lld/test/COFF/code-merge.s
+++ b/lld/test/COFF/code-merge.s
@@ -3,7 +3,7 @@
# Test that merging code section into a data section changes its characteristics.
# RUN: llvm-mc -triple x86_64-windows-msvc %s -filetype=obj -o %t.obj
-# RUN: lld-link -machine:amd64 -dll -noentry -out:%t.dll %t.obj -merge:.testx=.testd -merge:.testx2=.testbss
+# RUN: lld-link -machine:amd64 -dll -noentry -out:%t.dll %t.obj -merge:.testx=.testd -merge:.testx2=.testbss -merge:.testd2=.testx3 -merge:.testbss2=.testx4
# RUN: llvm-readobj --sections %t.dll | FileCheck %s
# CHECK: Sections [
@@ -40,6 +40,40 @@
# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
# CHECK-NEXT: ]
# CHECK-NEXT: }
+# CHECK-NEXT: Section {
+# CHECK-NEXT: Number: 3
+# CHECK-NEXT: Name: .testx3 (2E 74 65 73 74 78 33 00)
+# CHECK-NEXT: VirtualSize: 0xA
+# CHECK-NEXT: VirtualAddress: 0x3000
+# CHECK-NEXT: RawDataSize: 512
+# CHECK-NEXT: PointerToRawData: 0x800
+# CHECK-NEXT: PointerToRelocations: 0x0
+# CHECK-NEXT: PointerToLineNumbers: 0x0
+# CHECK-NEXT: RelocationCount: 0
+# CHECK-NEXT: LineNumberCount: 0
+# CHECK-NEXT: Characteristics [ (0x60000020)
+# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
+# CHECK-NEXT: IMAGE_SCN_MEM_EXECUTE (0x20000000)
+# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
+# CHECK-NEXT: ]
+# CHECK-NEXT: }
+# CHECK-NEXT: Section {
+# CHECK-NEXT: Number: 4
+# CHECK-NEXT: Name: .testx4 (2E 74 65 73 74 78 34 00)
+# CHECK-NEXT: VirtualSize: 0xC
+# CHECK-NEXT: VirtualAddress: 0x4000
+# CHECK-NEXT: RawDataSize: 512
+# CHECK-NEXT: PointerToRawData: 0xA00
+# CHECK-NEXT: PointerToRelocations: 0x0
+# CHECK-NEXT: PointerToLineNumbers: 0x0
+# CHECK-NEXT: RelocationCount: 0
+# CHECK-NEXT: LineNumberCount: 0
+# CHECK-NEXT: Characteristics [ (0x60000020)
+# CHECK-NEXT: IMAGE_SCN_CNT_CODE (0x20)
+# CHECK-NEXT: IMAGE_SCN_MEM_EXECUTE (0x20000000)
+# CHECK-NEXT: IMAGE_SCN_MEM_READ (0x40000000)
+# CHECK-NEXT: ]
+# CHECK-NEXT: }
# CHECK-NEXT: ]
.section .testx, "xr"
@@ -55,3 +89,17 @@
.section .testbss, "b"
.skip 4
+
+ .section .testx3, "xr"
+ movq $3, %rax
+ retq
+
+ .section .testx4, "xr"
+ movq $4, %rax
+ retq
+
+ .section .testd2, "dr"
+ .word 2
+
+ .section .testbss2, "b"
+ .skip 4
More information about the llvm-commits
mailing list