[lld] [LLD] [COFF] Recognize Itanium vtables for ICF (PR #70196)

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 25 04:33:33 PDT 2023


https://github.com/mstorsjo created https://github.com/llvm/llvm-project/pull/70196

The testcases are plain copies of the existing ICF vtable testcase, with symbol names renamed to match the Itanium vtable name pattern.

>From dfda917cd569cb0df12e443591a9e14742e42e3e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin at martin.st>
Date: Wed, 25 Oct 2023 14:26:55 +0300
Subject: [PATCH] [LLD] [COFF] Recognize Itanium vtables for ICF

The testcases are plain copies of the existing ICF vtable testcase,
with symbol names renamed to match the Itanium vtable name pattern.
---
 lld/COFF/ICF.cpp                         |  5 ++++-
 lld/test/COFF/icf-vtables-itanium-i386.s | 27 +++++++++++++++++++++++
 lld/test/COFF/icf-vtables-itanium.s      | 28 ++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 lld/test/COFF/icf-vtables-itanium-i386.s
 create mode 100644 lld/test/COFF/icf-vtables-itanium.s

diff --git a/lld/COFF/ICF.cpp b/lld/COFF/ICF.cpp
index 0f43da0dbc101af..013ffcfb3d5d1a4 100644
--- a/lld/COFF/ICF.cpp
+++ b/lld/COFF/ICF.cpp
@@ -94,7 +94,10 @@ bool ICF::isEligible(SectionChunk *c) {
     return true;
 
   // So are vtables.
-  if (c->sym && c->sym->getName().starts_with("??_7"))
+  const char *itaniumVtablePrefix =
+      ctx.config.machine == I386 ? "__ZTV" : "_ZTV";
+  if (c->sym && (c->sym->getName().starts_with("??_7") ||
+                 c->sym->getName().starts_with(itaniumVtablePrefix)))
     return true;
 
   // Anything else not in an address-significance table is eligible.
diff --git a/lld/test/COFF/icf-vtables-itanium-i386.s b/lld/test/COFF/icf-vtables-itanium-i386.s
new file mode 100644
index 000000000000000..77b3755178b8b27
--- /dev/null
+++ b/lld/test/COFF/icf-vtables-itanium-i386.s
@@ -0,0 +1,27 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple=i386-windows-gnu -filetype=obj -o %t.obj %s
+# RUN: lld-link %t.obj /out:%t.exe /entry:main /subsystem:console /safeseh:no
+# RUN: llvm-objdump -s %t.exe | FileCheck %s
+
+# CHECK: Contents of section .text:
+.globl _main
+_main:
+# CHECK-NEXT: 401000 00204000 01204000 01204000
+.long __ZTS
+.long __ZTV
+.long __ZTVa
+
+.section .rdata,"dr",discard,__ZTS
+.globl __ZTS
+__ZTS:
+.byte 42
+
+.section .rdata,"dr",discard,__ZTV
+.globl __ZTV
+__ZTV:
+.byte 42
+
+.section .rdata,"dr",discard,__ZTVa
+.globl __ZTVa
+__ZTVa:
+.byte 42
diff --git a/lld/test/COFF/icf-vtables-itanium.s b/lld/test/COFF/icf-vtables-itanium.s
new file mode 100644
index 000000000000000..94f2b611dc86b1c
--- /dev/null
+++ b/lld/test/COFF/icf-vtables-itanium.s
@@ -0,0 +1,28 @@
+# REQUIRES: x86
+# RUN: llvm-mc -triple=x86_64-windows-gnu -filetype=obj -o %t.obj %s
+# RUN: lld-link %t.obj /out:%t.exe /entry:main /subsystem:console
+# RUN: llvm-objdump -s %t.exe | FileCheck %s
+
+# CHECK: Contents of section .text:
+.globl main
+main:
+# CHECK-NEXT: 140001000 00200040 01000000 01200040 01000000
+.8byte _ZTS
+.8byte _ZTV
+# CHECK-NEXT: 140001010 01200040 01000000
+.8byte _ZTVa
+
+.section .rdata,"dr",discard,_ZTS
+.globl _ZTS
+_ZTS:
+.byte 42
+
+.section .rdata,"dr",discard,_ZTV
+.globl _ZTV
+_ZTV:
+.byte 42
+
+.section .rdata,"dr",discard,_ZTVa
+.globl _ZTVa
+_ZTVa:
+.byte 42



More information about the llvm-commits mailing list