[llvm] bd19876 - [COFF] Aliases resolve directly to defined external targets
Eric Astor via llvm-commits
llvm-commits at lists.llvm.org
Mon Sep 28 13:13:13 PDT 2020
Author: Eric Astor
Date: 2020-09-28T16:12:45-04:00
New Revision: bd19876dc60c69f50a7110740e97c6878e56be60
URL: https://github.com/llvm/llvm-project/commit/bd19876dc60c69f50a7110740e97c6878e56be60
DIFF: https://github.com/llvm/llvm-project/commit/bd19876dc60c69f50a7110740e97c6878e56be60.diff
LOG: [COFF] Aliases resolve directly to defined external targets
Avoid introducing unnecessary indirection for weak-external references.
We only need to introduce ".weak.<SYMBOL>.default" when referencing a
symbol that is defined, but not external.
Reviewed By: mstorsjo
Differential Revision: https://reviews.llvm.org/D88305
Added:
llvm/test/MC/COFF/weak-alias-labels.s
Modified:
llvm/lib/MC/WinCOFFObjectWriter.cpp
llvm/test/MC/COFF/weak.s
Removed:
################################################################################
diff --git a/llvm/lib/MC/WinCOFFObjectWriter.cpp b/llvm/lib/MC/WinCOFFObjectWriter.cpp
index 8e7bf1eb0169..901d2c06e716 100644
--- a/llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ b/llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -353,9 +353,10 @@ COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
return nullptr;
const MCSymbol &Aliasee = SymRef->getSymbol();
- if (!Aliasee.isUndefined())
+ if (Aliasee.isUndefined() || Aliasee.isExternal())
+ return GetOrCreateCOFFSymbol(&Aliasee);
+ else
return nullptr;
- return GetOrCreateCOFFSymbol(&Aliasee);
}
/// This function takes a symbol data object from the assembler
diff --git a/llvm/test/MC/COFF/weak-alias-labels.s b/llvm/test/MC/COFF/weak-alias-labels.s
new file mode 100644
index 000000000000..f53389d332fe
--- /dev/null
+++ b/llvm/test/MC/COFF/weak-alias-labels.s
@@ -0,0 +1,122 @@
+// RUN: llvm-mc -filetype=obj -triple i686-pc-win32 %s | llvm-readobj --symbols - | FileCheck %s
+// RUN: llvm-mc -filetype=obj -triple x86_64-pc-win32 %s | llvm-readobj --symbols - | FileCheck %s
+
+// When making weak references to labels/procedures, we reference them directly
+// if they have global symbols; otherwise, we need to create a global symbol for
+// the reference to resolve to.
+
+ .text
+
+ .globl proc1
+proc1:
+ ret
+
+proc2:
+ ret
+// CHECK: Symbol {
+// CHECK: Name: proc2
+// CHECK-NEXT: Value: [[PROC2_VALUE:[0-9]+]]
+// CHECK-NEXT: Section: [[PROC2_SECTION:.*]]
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: Static
+// CHECK-NEXT: AuxSymbolCount: 0
+// CHECK-NEXT: }
+
+ .weak t1
+t1 = proc1
+
+// CHECK: Symbol {
+// CHECK: Name: t1
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: proc1
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+ .weak t2
+t2 = proc2
+
+// CHECK: Symbol {
+// CHECK: Name: t2
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: .weak.t2.default
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+// CHECK: Symbol {
+// CHECK: Name: .weak.t2.default
+// CHECK-NEXT: Value: [[PROC2_VALUE]]
+// CHECK-NEXT: Section: [[PROC2_SECTION]]
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: External
+// CHECK-NEXT: AuxSymbolCount: 0
+// CHECK-NEXT: }
+
+ .weak t3
+t3 = foo
+
+// CHECK: Symbol {
+// CHECK: Name: t3
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: foo
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+ .weak t4
+t4 = bar
+
+ .globl bar
+bar:
+ ret
+
+// CHECK: Symbol {
+// CHECK: Name: t4
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: bar
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+
+ .weak t5
+t5 = t2
+
+// CHECK: Symbol {
+// CHECK: Name: t5
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: t2
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
diff --git a/llvm/test/MC/COFF/weak.s b/llvm/test/MC/COFF/weak.s
index 5a0c3a32192f..e700aabc2425 100644
--- a/llvm/test/MC/COFF/weak.s
+++ b/llvm/test/MC/COFF/weak.s
@@ -32,6 +32,11 @@ LBB0_2: # %return
.weak _test_weak_alias
_test_weak_alias=_main
+ .weak weakfunc
+ .globl weakfunc
+weakfunc:
+ ret
+
// CHECK: Symbols [
// CHECK: Symbol {
@@ -47,7 +52,7 @@ LBB0_2: # %return
// CHECK: Symbol {
// CHECK: Name: _test_weak
// CHECK-NEXT: Value: 0
-// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED (0)
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
// CHECK-NEXT: BaseType: Null
// CHECK-NEXT: ComplexType: Null
// CHECK-NEXT: StorageClass: WeakExternal
@@ -61,7 +66,7 @@ LBB0_2: # %return
// CHECK: Symbol {
// CHECK: Name: .weak._test_weak.default._main
// CHECK-NEXT: Value: 0
-// CHECK-NEXT: Section: IMAGE_SYM_ABSOLUTE (-1)
+// CHECK-NEXT: Section: IMAGE_SYM_ABSOLUTE
// CHECK-NEXT: BaseType: Null
// CHECK-NEXT: ComplexType: Null
// CHECK-NEXT: StorageClass: External
@@ -71,23 +76,37 @@ LBB0_2: # %return
// CHECK: Symbol {
// CHECK: Name: _test_weak_alias
// CHECK-NEXT: Value: 0
-// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED (0)
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
// CHECK-NEXT: BaseType: Null
// CHECK-NEXT: ComplexType: Null
// CHECK-NEXT: StorageClass: WeakExternal
// CHECK-NEXT: AuxSymbolCount: 1
// CHECK-NEXT: AuxWeakExternal {
-// CHECK-NEXT: Linked: .weak._test_weak_alias.default
-// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: Linked: _main
+// CHECK-NEXT: Search: Alias
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK: Symbol {
-// CHECK: Name: .weak._test_weak_alias.default._main
-// CHECK-NEXT: Value: 0
-// CHECK-NEXT: Section: .text
-// CHECK-NEXT: BaseType: Null
-// CHECK-NEXT: ComplexType: Null
-// CHECK-NEXT: StorageClass: External
+// CHECK: Name: weakfunc
+// CHECK-NEXT: Value: 0
+// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: WeakExternal
+// CHECK-NEXT: AuxSymbolCount: 1
+// CHECK-NEXT: AuxWeakExternal {
+// CHECK-NEXT: Linked: .weak.weakfunc.default
+// CHECK-NEXT: Search: Alias
+// CHECK-NEXT: }
+// CHECK-NEXT: }
+// CHECK: Symbol {
+// CHECK: Name: .weak.weakfunc.default
+// CHECK-NOT: Value: 0
+// CHECK-NOT: Symbol {
+// CHECK: Section: .text
+// CHECK-NEXT: BaseType: Null
+// CHECK-NEXT: ComplexType: Null
+// CHECK-NEXT: StorageClass: External
// CHECK-NEXT: AuxSymbolCount: 0
// CHECK-NEXT: }
More information about the llvm-commits
mailing list