[PATCH] D104058: ThinLTO: Fix inline assembly references to static functions with CFI

Sami Tolvanen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 10 13:44:41 PDT 2021


samitolvanen created this revision.
Herald added subscribers: ormris, steven_wu, hiraditya, inglorion.
samitolvanen requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

Create an internal alias with the original name for static functions
that are renamed in promoteInternals to avoid breaking inline
assembly references to them.

Link: https://github.com/ClangBuiltLinux/linux/issues/1354


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104058

Files:
  clang/test/CodeGen/thinlto-cfi-icall-static-inline-asm.c
  llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp


Index: llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
===================================================================
--- llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
+++ llvm/lib/Transforms/IPO/ThinLTOBitcodeWriter.cpp
@@ -69,6 +69,15 @@
       ImportGV->setName(NewName);
       ImportGV->setVisibility(GlobalValue::HiddenVisibility);
     }
+
+    if (Function *F = dyn_cast<Function>(&ExportGV)) {
+      // Create a local alias with the original name to avoid breaking
+      // references from inline assembly.
+      GlobalAlias *A =
+          GlobalAlias::create(F->getValueType(), F->getAddressSpace(),
+                              GlobalValue::InternalLinkage, Name, F, &ExportM);
+      appendToCompilerUsed(ExportM, A);
+    }
   }
 
   if (!RenamedComdats.empty())
Index: clang/test/CodeGen/thinlto-cfi-icall-static-inline-asm.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/thinlto-cfi-icall-static-inline-asm.c
@@ -0,0 +1,20 @@
+// REQUIRES: x86-registered-target
+
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm-bc -flto=thin -fsanitize=cfi-icall -fsplit-lto-unit -o - %s | llvm-dis - | FileCheck %s
+
+// CHECK: @a = internal alias {{.*}}@a.[[HASH:[0-9a-f]+]]
+static void a(void);
+
+// CHECK-LABEL: define{{.*}} @b(){{.*}} !type
+void b(void) {
+  // CHECK: store{{.*}} @a.[[HASH]],{{.*}} %f
+  void (*f)(void) = a;
+  // CHECK: call void ()* asm sideeffect "leaq a(%rip)
+  asm volatile("leaq a(%%rip), %0\n\t"
+               : "=r"(f));
+  f();
+}
+
+// CHECK: define{{.*}} @a.[[HASH]](){{.*}} !type
+static void a(void) {
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104058.351253.patch
Type: text/x-patch
Size: 1637 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210610/f95e8591/attachment-0001.bin>


More information about the cfe-commits mailing list