aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJez Ng <jezng@fb.com>2022-03-15 20:25:06 -0400
committerJez Ng <jezng@fb.com>2022-03-15 20:25:06 -0400
commit8ce3750ff62d68e7428a828530b9b6a8cc93e813 (patch)
treeafb442f04c96329d7b5609209ef286ccb8f71dab
parentc1d4c67718db88be48f451b91f7bddab8fff2424 (diff)
downloadllvm-8ce3750ff62d68e7428a828530b9b6a8cc93e813.zip
llvm-8ce3750ff62d68e7428a828530b9b6a8cc93e813.tar.gz
llvm-8ce3750ff62d68e7428a828530b9b6a8cc93e813.tar.bz2
[lld-macho] Set FinalDefinitionInLinkageUnit on most LTO externs
Since Mach-O has a two-level namespace (unlike ELF), we can usually set this property to true. (I believe this setting is only available in the new LTO backend, so I can't really use ld64 / libLTO's behavior as a reference here... I'm just doing what I think is correct.) See {D119294} for the work done to calculate the `interposable` used in this diff. Reviewed By: MaskRay Differential Revision: https://reviews.llvm.org/D119506
-rw-r--r--lld/MachO/LTO.cpp8
-rw-r--r--lld/test/MachO/lto-final-definition.ll27
-rw-r--r--lld/test/MachO/lto-internalize.ll14
3 files changed, 40 insertions, 9 deletions
diff --git a/lld/MachO/LTO.cpp b/lld/MachO/LTO.cpp
index c2863c7..3962de8 100644
--- a/lld/MachO/LTO.cpp
+++ b/lld/MachO/LTO.cpp
@@ -79,11 +79,15 @@ void BitcodeCompiler::add(BitcodeFile &f) {
// be removed.
r.Prevailing = !objSym.isUndefined() && sym->getFile() == &f;
- if (const auto *defined = dyn_cast<Defined>(sym))
+ if (const auto *defined = dyn_cast<Defined>(sym)) {
r.ExportDynamic =
defined->isExternal() && !defined->privateExtern && exportDynamic;
- else if (const auto *common = dyn_cast<CommonSymbol>(sym))
+ r.FinalDefinitionInLinkageUnit =
+ !defined->isExternalWeakDef() && !defined->interposable;
+ } else if (const auto *common = dyn_cast<CommonSymbol>(sym)) {
r.ExportDynamic = !common->privateExtern && exportDynamic;
+ r.FinalDefinitionInLinkageUnit = true;
+ }
r.VisibleToRegularObj =
sym->isUsedInRegularObj || (r.Prevailing && r.ExportDynamic);
diff --git a/lld/test/MachO/lto-final-definition.ll b/lld/test/MachO/lto-final-definition.ll
new file mode 100644
index 0000000..e0feb43
--- /dev/null
+++ b/lld/test/MachO/lto-final-definition.ll
@@ -0,0 +1,27 @@
+; REQUIRES: x86
+; RUN: rm -rf %t; mkdir %t
+; RUN: llvm-as %s -o %t/test.o
+; RUN: %lld -lSystem -dylib %t/test.o -o %t/test -save-temps
+; RUN: llvm-dis %t/test.0.2.internalize.bc -o - | FileCheck %s
+; RUN: %lld -lSystem -dylib %t/test.o -o %t/flat-namespace.dylib -save-temps \
+; RUN: -flat_namespace
+; RUN: llvm-dis %t/flat-namespace.dylib.0.2.internalize.bc -o - | FileCheck %s \
+; RUN: --check-prefix=NO-DSO-LOCAL
+
+;; f() is never dso_local since it is a weak external.
+; CHECK: define weak_odr void @f()
+; CHECK: define dso_local void @main()
+
+; NO-DSO-LOCAL: define weak_odr void @f()
+; NO-DSO-LOCAL: define void @main()
+
+target triple = "x86_64-apple-macosx10.15.0"
+target datalayout = "e-m:o-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+
+define weak_odr void @f() {
+ ret void
+}
+
+define void @main() {
+ ret void
+}
diff --git a/lld/test/MachO/lto-internalize.ll b/lld/test/MachO/lto-internalize.ll
index 755484c..75962ae 100644
--- a/lld/test/MachO/lto-internalize.ll
+++ b/lld/test/MachO/lto-internalize.ll
@@ -17,7 +17,7 @@
;; Check that main is not internalized. This covers the case of bitcode symbols
;; referenced by undefined symbols that don't belong to any InputFile.
-; CHECK: define void @main()
+; CHECK: define dso_local void @main()
;; Check that the foo and bar functions are correctly internalized.
; CHECK: define internal void @bar()
@@ -25,7 +25,7 @@
;; Check that a bitcode symbol that is referenced by a regular object file isn't
;; internalized.
-; CHECK: define void @used_in_regular_obj()
+; CHECK: define dso_local void @used_in_regular_obj()
;; Check that a bitcode symbol that is defined in another bitcode file gets
;; internalized.
@@ -51,13 +51,13 @@
;; Note that only foo() gets internalized here; everything else that isn't
;; hidden must be exported.
-; DYN: @comm = common global
+; DYN: @comm = common dso_local global
; DYN: @comm_hide = internal global
-; DYN: define void @main()
-; DYN: define void @bar()
+; DYN: define dso_local void @main()
+; DYN: define dso_local void @bar()
; DYN: define internal void @foo()
-; DYN: define void @used_in_regular_obj()
-; DYN: define void @baz()
+; DYN: define dso_local void @used_in_regular_obj()
+; DYN: define dso_local void @baz()
; DYN-SYMS-DAG: (__TEXT,__text) external _bar
; DYN-SYMS-DAG: (__TEXT,__text) external _baz