[llvm] [BOLT] Don't create function entry points for unnamed symbols (PR #68977)

Job Noorman via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 16 00:15:59 PDT 2023


https://github.com/mtvec updated https://github.com/llvm/llvm-project/pull/68977

>From 3d346a4138d2edfddef726d9bd933eb15b70d5bd Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Fri, 13 Oct 2023 13:20:56 +0200
Subject: [PATCH 1/3] [BOLT] Don't create function entry points for unnamed
 symbols

Unnamed symbols are used, for example, for debug info related
relocations on RISC-V.
---
 bolt/lib/Rewrite/RewriteInstance.cpp   |  6 ++++++
 bolt/test/RISCV/unnamed-sym-no-entry.c | 16 ++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 bolt/test/RISCV/unnamed-sym-no-entry.c

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index ddcc21878abb818..3f3cd4ed5ffc6b6 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1582,6 +1582,12 @@ void RewriteInstance::adjustFunctionBoundaries() {
       if (!Function.isSymbolValidInScope(Symbol, SymbolSize))
         break;
 
+      // Ignore unnamed symbols. Used, for example, by debugging info on RISC-V.
+      if (cantFail(Symbol.getName()).empty()) {
+        ++NextSymRefI;
+        continue;
+      }
+
       // Skip basic block labels. This happens on RISC-V with linker relaxation
       // enabled because every branch needs a relocation and corresponding
       // symbol. We don't want to add such symbols as entry points.
diff --git a/bolt/test/RISCV/unnamed-sym-no-entry.c b/bolt/test/RISCV/unnamed-sym-no-entry.c
new file mode 100644
index 000000000000000..57ba292fe6e5a8b
--- /dev/null
+++ b/bolt/test/RISCV/unnamed-sym-no-entry.c
@@ -0,0 +1,16 @@
+/// Verify that unnamed symbols are not added as function entry points. Such
+/// symbols are used by relocations in debugging sections.
+
+// RUN: %clang %cflags -g -Wl,-q -o %t %s
+
+/// Verify that the binary indeed contains an unnamed symbol at _start
+// RUN: llvm-readelf -s %t | FileCheck %s --check-prefix=CHECK-ELF
+// CHECK-ELF-DAG: [[#%x,START:]] {{.*}} FUNC GLOBAL DEFAULT [[#%d,SECTION:]] _start{{$}}
+// CHECK-ELF-DAG: [[#%x,START]] {{.*}} NOTYPE LOCAL DEFAULT [[#SECTION]] {{$}}
+
+/// Verify that BOLT did not create an extra entry point for the unnamed symbol
+// RUN: llvm-bolt -o %t.bolt %t --print-cfg | FileCheck %s
+// CHECK: Binary Function "_start" after building cfg {
+// CHECK:  IsMultiEntry: 0
+
+void _start() {}

>From bb834aabe652caf0580be1a84f05ba3a3eee6551 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Fri, 13 Oct 2023 13:48:29 +0200
Subject: [PATCH 2/3] Disable clang-format on the test

---
 bolt/test/RISCV/unnamed-sym-no-entry.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/bolt/test/RISCV/unnamed-sym-no-entry.c b/bolt/test/RISCV/unnamed-sym-no-entry.c
index 57ba292fe6e5a8b..605bbc00aeec463 100644
--- a/bolt/test/RISCV/unnamed-sym-no-entry.c
+++ b/bolt/test/RISCV/unnamed-sym-no-entry.c
@@ -1,6 +1,8 @@
 /// Verify that unnamed symbols are not added as function entry points. Such
 /// symbols are used by relocations in debugging sections.
 
+// clang-format off
+
 // RUN: %clang %cflags -g -Wl,-q -o %t %s
 
 /// Verify that the binary indeed contains an unnamed symbol at _start

>From bf69acd8710bc4c3a1458851ea24c61964bef8d0 Mon Sep 17 00:00:00 2001
From: Job Noorman <jnoorman at igalia.com>
Date: Mon, 16 Oct 2023 09:15:28 +0200
Subject: [PATCH 3/3] Make check RISC-V specific

---
 bolt/lib/Rewrite/RewriteInstance.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp
index 3f3cd4ed5ffc6b6..b3de3b96b3ab8e6 100644
--- a/bolt/lib/Rewrite/RewriteInstance.cpp
+++ b/bolt/lib/Rewrite/RewriteInstance.cpp
@@ -1583,7 +1583,7 @@ void RewriteInstance::adjustFunctionBoundaries() {
         break;
 
       // Ignore unnamed symbols. Used, for example, by debugging info on RISC-V.
-      if (cantFail(Symbol.getName()).empty()) {
+      if (BC->isRISCV() && cantFail(Symbol.getName()).empty()) {
         ++NextSymRefI;
         continue;
       }



More information about the llvm-commits mailing list