[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
This commit is contained in:
Jez Ng 2022-03-15 20:25:06 -04:00
parent c1d4c67718
commit 8ce3750ff6
3 changed files with 40 additions and 9 deletions

View File

@ -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);

View File

@ -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
}

View File

@ -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