[lld] a598803 - [lld] Fill .text section gaps with INT3 only on x86 targets.

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 23 04:44:00 PDT 2023


Author: Jacek Caban
Date: 2023-03-23T13:43:21+02:00
New Revision: a5988034a44d039f95db3067e4ad0dfeeca155c3

URL: https://github.com/llvm/llvm-project/commit/a5988034a44d039f95db3067e4ad0dfeeca155c3
DIFF: https://github.com/llvm/llvm-project/commit/a5988034a44d039f95db3067e4ad0dfeeca155c3.diff

LOG: [lld] Fill .text section gaps with INT3 only on x86 targets.

It doesn't make sense on ARM and using default 0 fill is compatible
with MSVC.

(It's more noticeable ARM64EC targets, where additional padding mixed
with alignment is used for entry thunk association, so there are more
gaps).

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D145962

Added: 
    lld/test/COFF/gaps-fill.test

Modified: 
    lld/COFF/Writer.cpp
    lld/test/COFF/arm-thumb-thunks-multipass.s
    lld/test/COFF/arm64-import2.test

Removed: 
    


################################################################################
diff  --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index 0909b14d8190..603703e65290 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -1953,7 +1953,8 @@ void Writer::writeSections() {
     // Fill gaps between functions in .text with INT3 instructions
     // instead of leaving as NUL bytes (which can be interpreted as
     // ADD instructions).
-    if (sec->header.Characteristics & IMAGE_SCN_CNT_CODE)
+    if ((sec->header.Characteristics & IMAGE_SCN_CNT_CODE) &&
+        (ctx.config.machine == AMD64 || ctx.config.machine == I386))
       memset(secBuf, 0xCC, sec->getRawSize());
     parallelForEach(sec->chunks, [&](Chunk *c) {
       c->writeTo(secBuf + c->getRVA() - sec->getRVA());

diff  --git a/lld/test/COFF/arm-thumb-thunks-multipass.s b/lld/test/COFF/arm-thumb-thunks-multipass.s
index 71ce53d99b31..c10b22963187 100644
--- a/lld/test/COFF/arm-thumb-thunks-multipass.s
+++ b/lld/test/COFF/arm-thumb-thunks-multipass.s
@@ -67,4 +67,4 @@ far_func\i:
 // FUNC01-THUNKS: 40500a:       f2c0 0c10       movt    r12, #16
 // FUNC01-THUNKS: 40500e:       44e7            add     pc, r12
 // The instruction below is padding from the .balign
-// FUNC01-THUNKS: 405010:       cccc            ldm     r4!, {r2, r3, r6, r7}
+// FUNC01-THUNKS: 405010:       0000            movs    r0, r0

diff  --git a/lld/test/COFF/arm64-import2.test b/lld/test/COFF/arm64-import2.test
index 9b95f1a29b83..342671211db8 100644
--- a/lld/test/COFF/arm64-import2.test
+++ b/lld/test/COFF/arm64-import2.test
@@ -18,7 +18,7 @@
 # AFTER:  140001000:      94000004        bl      0x140001010
 # AFTER:  140001004:      94000006        bl      0x14000101c
 # AFTER:  140001008:      d65f03c0        ret
-# AFTER:  14000100c:      ccccccff        <unknown>
+# AFTER:  14000100c:      000000ff
 # AFTER:  140001010:      b0000010        adrp    x16, 0x140002000
 # AFTER:  140001014:      f9403210        ldr     x16, [x16, #96]
 # AFTER:  140001018:      d61f0200        br      x16

diff  --git a/lld/test/COFF/gaps-fill.test b/lld/test/COFF/gaps-fill.test
new file mode 100644
index 000000000000..17cd9cbc86ab
--- /dev/null
+++ b/lld/test/COFF/gaps-fill.test
@@ -0,0 +1,78 @@
+# REQUIRES: aarch64
+# RUN: split-file %s %t.dir
+
+# RUN: llvm-mc -filetype=obj -triple=aarch64-windows %t.dir/arm64-dllmain.s -o %t.dir/arm64-dllmain.obj
+# RUN: llvm-mc -filetype=obj -triple=aarch64-windows %t.dir/arm64-p4sym.s -o %t.dir/arm64-p4sym.obj
+# RUN: lld-link -dll -machine:arm64 %t.dir/arm64-dllmain.obj %t.dir/arm64-p4sym.obj -out:%t.dll
+
+# RUN: llvm-objdump -dz %t.dll | FileCheck -check-prefix=CHECK-ARM64 %s
+# CHECK-ARM64: 180001000: 52800020     mov     w0, #0x1
+# CHECK-ARM64: 180001004: d65f03c0     ret
+# CHECK-ARM64: 180001008: 00000000
+# CHECK-ARM64: 18000100c: 00000000
+# CHECK-ARM64: 180001010: 52800040     mov     w0, #0x2
+# CHECK-ARM64: 180001014: d65f03c0     ret
+
+#--- arm64-dllmain.s
+        .def _DllMainCRTStartup;
+        .scl 2;
+        .type 32;
+        .endef
+        .globl _DllMainCRTStartup
+        .p2align 2
+_DllMainCRTStartup:
+        mov w0, #1
+        ret
+
+#--- arm64-p4sym.s
+        .def p4sym;
+        .scl 2;
+        .type 32;
+        .endef
+        .globl p4sym
+        .p2align 4
+p4sym:
+        mov w0, #2
+        ret
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-windows %t.dir/x86_64-dllmain.s -o %t.dir/x86_64-dllmain.obj
+# RUN: llvm-mc -filetype=obj -triple=x86_64-windows %t.dir/x86_64-p4sym.s -o %t.dir/x86_64-p4sym.obj
+# RUN: lld-link -dll -machine:amd64 %t.dir/x86_64-dllmain.obj %t.dir/x86_64-p4sym.obj -out:%t.dll
+
+# RUN: llvm-objdump -dz %t.dll | FileCheck -check-prefix=CHECK-X64 %s
+# CHECK-X64: 180001000: b8 01 00 00 00 movl $0x1, %eax
+# CHECK-X64: 180001005: c3 retq
+# CHECK-X64: 180001006: cc int3
+# CHECK-X64: 180001007: cc int3
+# CHECK-X64: 180001008: cc int3
+# CHECK-X64: 180001009: cc int3
+# CHECK-X64: 18000100a: cc int3
+# CHECK-X64: 18000100b: cc int3
+# CHECK-X64: 18000100c: cc int3
+# CHECK-X64: 18000100d: cc int3
+# CHECK-X64: 18000100e: cc int3
+# CHECK-X64: 18000100f: cc int3
+# CHECK-X64: 180001010: b8 02 00 00 00 movl $0x2, %eax
+# CHECK-X64: 180001015: c3 retq
+
+#--- x86_64-dllmain.s
+        .def _DllMainCRTStartup;
+        .scl 2;
+        .type 32;
+        .endef
+        .globl _DllMainCRTStartup
+        .p2align 4, 0x90
+_DllMainCRTStartup:
+        movl $1, %eax
+        retq
+
+#--- x86_64-p4sym.s
+        .def p4sym;
+        .scl 2;
+        .type 32;
+        .endef
+        .globl p4sym
+        .p2align 4, 0x90
+p4sym:
+        movl $2, %eax
+        retq


        


More information about the llvm-commits mailing list