Skip to content

Commit 7ca86ee

Browse files
committed
[COFF] Make the autogenerated .weak.<name>.default symbols static
If we have references to the same extern_weak in multiple objects, all of them would generate external symbols with the same name. Make them static to avoid duplicate definitions; nothing should need to refer to this symbol outside of the current object. GCC/binutils seems to handle the same by not using a fixed string for the ".default" suffix, but instead using the name of some other defined external symbol from the same object (which is supposed to be unique among objects unless there's other duplicate definitions). Differential Revision: https://reviews.llvm.org/D71711
1 parent e7853a5 commit 7ca86ee

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

llvm/lib/MC/WinCOFFObjectWriter.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,10 @@ COFFSymbol *WinCOFFObjectWriter::getLinkedSymbol(const MCSymbol &Symbol) {
352352

353353
/// This function takes a symbol data object from the assembler
354354
/// and creates the associated COFF symbol staging object.
355-
void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
355+
void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSymGeneric,
356356
MCAssembler &Assembler,
357357
const MCAsmLayout &Layout) {
358+
const auto &MCSym = cast<MCSymbolCOFF>(MCSymGeneric);
358359
COFFSymbol *Sym = GetOrCreateCOFFSymbol(&MCSym);
359360
const MCSymbol *Base = Layout.getBaseSymbol(MCSym);
360361
COFFSection *Sec = nullptr;
@@ -365,7 +366,7 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
365366
}
366367

367368
COFFSymbol *Local = nullptr;
368-
if (cast<MCSymbolCOFF>(MCSym).isWeakExternal()) {
369+
if (MCSym.isWeakExternal()) {
369370
Sym->Data.StorageClass = COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL;
370371

371372
COFFSymbol *WeakDefault = getLinkedSymbol(MCSym);
@@ -376,6 +377,9 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
376377
WeakDefault->Data.SectionNumber = COFF::IMAGE_SYM_ABSOLUTE;
377378
else
378379
WeakDefault->Section = Sec;
380+
// Make the default symbol static, in order to not conflict with
381+
// similar default symbols for the same weak in other objects.
382+
WeakDefault->Data.StorageClass = COFF::IMAGE_SYM_CLASS_STATIC;
379383
Local = WeakDefault;
380384
}
381385

@@ -394,14 +398,8 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
394398
else
395399
Sym->Section = Sec;
396400
Local = Sym;
397-
}
398401

399-
if (Local) {
400-
Local->Data.Value = getSymbolValue(MCSym, Layout);
401-
402-
const MCSymbolCOFF &SymbolCOFF = cast<MCSymbolCOFF>(MCSym);
403-
Local->Data.Type = SymbolCOFF.getType();
404-
Local->Data.StorageClass = SymbolCOFF.getClass();
402+
Local->Data.StorageClass = MCSym.getClass();
405403

406404
// If no storage class was specified in the streamer, define it here.
407405
if (Local->Data.StorageClass == COFF::IMAGE_SYM_CLASS_NULL) {
@@ -413,6 +411,12 @@ void WinCOFFObjectWriter::DefineSymbol(const MCSymbol &MCSym,
413411
}
414412
}
415413

414+
if (Local) {
415+
Local->Data.Value = getSymbolValue(MCSym, Layout);
416+
417+
Local->Data.Type = MCSym.getType();
418+
}
419+
416420
Sym->MC = &MCSym;
417421
}
418422

llvm/test/MC/COFF/weak-alias-local.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,6 @@ a=b
3838
// CHECK-NEXT: Section: .data (2)
3939
// CHECK-NEXT: BaseType: Null (0x0)
4040
// CHECK-NEXT: ComplexType: Null (0x0)
41-
// CHECK-NEXT: StorageClass: External (0x2)
41+
// CHECK-NEXT: StorageClass: Static (0x3)
4242
// CHECK-NEXT: AuxSymbolCount: 0
4343
// CHECK-NEXT: }

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ b:
2828
// CHECK-NEXT: Section: .data (2)
2929
// CHECK-NEXT: BaseType: Null (0x0)
3030
// CHECK-NEXT: ComplexType: Null (0x0)
31-
// CHECK-NEXT: StorageClass: External (0x2)
31+
// CHECK-NEXT: StorageClass: Static (0x3)
3232
// CHECK-NEXT: AuxSymbolCount: 0
3333
// CHECK-NEXT: }

llvm/test/MC/COFF/weak.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ LBB0_2: # %return
6464
// CHECK-NEXT: Section: IMAGE_SYM_ABSOLUTE (-1)
6565
// CHECK-NEXT: BaseType: Null
6666
// CHECK-NEXT: ComplexType: Null
67-
// CHECK-NEXT: StorageClass: External
67+
// CHECK-NEXT: StorageClass: Static
6868
// CHECK-NEXT: AuxSymbolCount: 0
6969
// CHECK-NEXT: }
7070

@@ -88,6 +88,6 @@ LBB0_2: # %return
8888
// CHECK-NEXT: Section: .text
8989
// CHECK-NEXT: BaseType: Null
9090
// CHECK-NEXT: ComplexType: Null
91-
// CHECK-NEXT: StorageClass: External
91+
// CHECK-NEXT: StorageClass: Static
9292
// CHECK-NEXT: AuxSymbolCount: 0
9393
// CHECK-NEXT: }

0 commit comments

Comments
 (0)