Skip to content

Commit 9e81d8b

Browse files
committed
[MC] [COFF] Make sure that weak external symbols are undefined symbols
For comdats (e.g. caused by -ffunction-sections), Section is already set here; make sure it's null, for the weak external symbol to be undefined. This fixes PR46779. Differential Revision: https://reviews.llvm.org/D84507
1 parent 4d09ed9 commit 9e81d8b

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,7 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
375375
COFFSymbol *Local = nullptr;
376376
if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
377377
Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
378+
Sym->Section = nullptr;
378379

379380
COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
380381
if (!WeakDefault) {

llvm/test/MC/COFF/weak-comdat.s

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// RUN: llvm-mc -filetype=obj -triple x86_64-pc-win32 %s -o %t.o
2+
// RUN: llvm-readobj --symbols %t.o | FileCheck %s
3+
4+
// Test that the weak symbol is properly undefined, while originally being
5+
// the leader symbol for a comdat. (This can easily happen if building with
6+
// -ffunction-sections).
7+
8+
.section .text$func,"xr",one_only,func
9+
.weak func
10+
func:
11+
ret
12+
13+
// CHECK: Symbol {
14+
// CHECK: Name: func
15+
// CHECK-NEXT: Value: 0
16+
// CHECK-NEXT: Section: IMAGE_SYM_UNDEFINED (0)
17+
// CHECK-NEXT: BaseType: Null (0x0)
18+
// CHECK-NEXT: ComplexType: Null (0x0)
19+
// CHECK-NEXT: StorageClass: WeakExternal (0x69)
20+
// CHECK-NEXT: AuxSymbolCount: 1
21+
// CHECK-NEXT: AuxWeakExternal {
22+
// CHECK-NEXT: Linked: .weak.func.default (10)
23+
// CHECK-NEXT: Search: Alias (0x3)
24+
// CHECK-NEXT: }
25+
// CHECK-NEXT: }
26+
// CHECK-NEXT: Symbol {
27+
// CHECK-NEXT: Name: .weak.func.default
28+
// CHECK-NEXT: Value: 0
29+
// CHECK-NEXT: Section: .text$func (4)
30+
// CHECK-NEXT: BaseType: Null (0x0)
31+
// CHECK-NEXT: ComplexType: Null (0x0)
32+
// CHECK-NEXT: StorageClass: External (0x2)
33+
// CHECK-NEXT: AuxSymbolCount: 0
34+
// CHECK-NEXT: }

0 commit comments

Comments
 (0)