[PATCH] D135710: [lto] Do not try to internalize symbols with escaped name

serge via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 11 13:35:18 PDT 2022


serge-sans-paille created this revision.
serge-sans-paille added reviewers: int3, glandium, steven_wu, mehdi_amini.
Herald added subscribers: ormris, hiraditya, inglorion.
Herald added projects: lld-macho, All.
Herald added a reviewer: lld-macho.
serge-sans-paille requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Because of LLVM mangling escape sequence (through '\01' prefix), it is possible
for a single symbols two have two different IR representations.

For instance, consider @symbol and @"\01_symbol". On OSX, because of the system
mangling rules, these two IR names point are converted in the same final symbol
upon linkage.

LTO doesn't model this behavior, which may result in symbols being incorrectly
internalized (if all reference use the escaping sequence while the definition
doesn't).

The proper approach is probably to use the mangled name to compute GUID to
avoid the dual representation, but we can also avoid discarding symbols that are
bound to two different IR names. This is an approximation, but it's less
intrusive on the codebase.

Fix #57864


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D135710

Files:
  lld/test/MachO/hidden-escaped-symbols-alt.ll
  lld/test/MachO/hidden-escaped-symbols.ll
  llvm/lib/LTO/LTO.cpp


Index: llvm/lib/LTO/LTO.cpp
===================================================================
--- llvm/lib/LTO/LTO.cpp
+++ llvm/lib/LTO/LTO.cpp
@@ -567,6 +567,15 @@
       GlobalRes.IRName = std::string(Sym.getIRName());
     }
 
+    // In rare occasion, the symbol used to initialize GlobalRes has a different
+    // IRName from the inspected Symbol. In that case, we have the same actual
+    // Symbol that can get two different GUID, leading to some invalid
+    // dismissal. Workaround this by marking the GlobalRes external.
+    if (GlobalRes.IRName != Sym.getIRName()) {
+      GlobalRes.Partition = GlobalResolution::External;
+      GlobalRes.VisibleOutsideSummary = true;
+    }
+
     // Set the partition to external if we know it is re-defined by the linker
     // with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
     // regular object, is referenced from llvm.compiler.used/llvm.used, or was
Index: lld/test/MachO/hidden-escaped-symbols.ll
===================================================================
--- /dev/null
+++ lld/test/MachO/hidden-escaped-symbols.ll
@@ -0,0 +1,22 @@
+; REQUIRES: x86
+; RUN: split-file %s %t
+; RUN: opt -module-summary %t/hide-me.ll -o %t/hide-me.o
+; RUN: opt -module-summary %t/ref.ll -o %t/ref.o
+; RUN: %lld %t/hide-me.o %t/ref.o -o %t/out
+
+;--- hide-me.ll
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.7.0"
+
+ at hide_me = hidden local_unnamed_addr global i8 8, align 1
+
+;--- ref.ll
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.7.0"
+
+@"\01_hide_me" = external local_unnamed_addr global i8
+
+define i8 @main() {
+  %1 = load i8, ptr @"\01_hide_me", align 1
+  ret i8 %1
+}
Index: lld/test/MachO/hidden-escaped-symbols-alt.ll
===================================================================
--- /dev/null
+++ lld/test/MachO/hidden-escaped-symbols-alt.ll
@@ -0,0 +1,22 @@
+; REQUIRES: x86
+; RUN: split-file %s %t
+; RUN: opt -module-summary %t/hide-me.ll -o %t/hide-me.o
+; RUN: opt -module-summary %t/ref.ll -o %t/ref.o
+; RUN: %lld %t/hide-me.o %t/ref.o -o %t/out
+
+;--- hide-me.ll
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.7.0"
+
+@"\01_hide_me" = hidden local_unnamed_addr global i8 8, align 1
+
+;--- ref.ll
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.7.0"
+
+ at hide_me = external local_unnamed_addr global i8
+
+define i8 @main() {
+  %1 = load i8, ptr @hide_me, align 1
+  ret i8 %1
+}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D135710.466902.patch
Type: text/x-patch
Size: 2742 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20221011/1f07bf50/attachment.bin>


More information about the llvm-commits mailing list