Skip to content

Commit ec4d5a6

Browse files
authored
[LLD][COFF] Preserve original symbol name when resolving weak aliases. (#105897)
Instead of replacing it with target's name.
1 parent 2ef3dcf commit ec4d5a6

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

lld/COFF/SymbolTable.cpp

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -494,20 +494,8 @@ void SymbolTable::resolveRemainingUndefines() {
494494
StringRef name = undef->getName();
495495

496496
// A weak alias may have been resolved, so check for that.
497-
if (Defined *d = undef->getWeakAlias()) {
498-
// We want to replace Sym with D. However, we can't just blindly
499-
// copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
500-
// internal symbol, and internal symbols are stored as "unparented"
501-
// Symbols. For that reason we need to check which type of symbol we
502-
// are dealing with and copy the correct number of bytes.
503-
if (isa<DefinedRegular>(d))
504-
memcpy(sym, d, sizeof(DefinedRegular));
505-
else if (isa<DefinedAbsolute>(d))
506-
memcpy(sym, d, sizeof(DefinedAbsolute));
507-
else
508-
memcpy(sym, d, sizeof(SymbolUnion));
497+
if (undef->resolveWeakAlias())
509498
continue;
510-
}
511499

512500
// If we can resolve a symbol by removing __imp_ prefix, do that.
513501
// This odd rule is for compatibility with MSVC linker.

lld/COFF/Symbols.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,29 @@ Defined *Undefined::getWeakAlias() {
136136
return nullptr;
137137
}
138138

139+
bool Undefined::resolveWeakAlias() {
140+
Defined *d = getWeakAlias();
141+
if (!d)
142+
return false;
143+
144+
// We want to replace Sym with D. However, we can't just blindly
145+
// copy sizeof(SymbolUnion) bytes from D to Sym because D may be an
146+
// internal symbol, and internal symbols are stored as "unparented"
147+
// Symbols. For that reason we need to check which type of symbol we
148+
// are dealing with and copy the correct number of bytes.
149+
StringRef name = getName();
150+
if (isa<DefinedRegular>(d))
151+
memcpy(this, d, sizeof(DefinedRegular));
152+
else if (isa<DefinedAbsolute>(d))
153+
memcpy(this, d, sizeof(DefinedAbsolute));
154+
else
155+
memcpy(this, d, sizeof(SymbolUnion));
156+
157+
nameData = name.data();
158+
nameSize = name.size();
159+
return true;
160+
}
161+
139162
MemoryBufferRef LazyArchive::getMemberBuffer() {
140163
Archive::Child c =
141164
CHECK(sym.getMember(), "could not get the member for symbol " +

lld/COFF/Symbols.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ class Undefined : public Symbol {
341341
// symbol by searching the chain of fallback symbols. Returns the symbol if
342342
// successful, otherwise returns null.
343343
Defined *getWeakAlias();
344+
345+
// If this symbol is external weak, replace this object with aliased symbol.
346+
bool resolveWeakAlias();
344347
};
345348

346349
// Windows-specific classes.

lld/test/COFF/symtab.test

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@
8686
# CHECK-NEXT: StorageClass: External (0x2)
8787
# CHECK-NEXT: AuxSymbolCount: 0
8888
# CHECK-NEXT: }
89+
# CHECK-NEXT: Symbol {
90+
# CHECK-NEXT: Name: weak_main
91+
# CHECK-NEXT: Value: 0
92+
# CHECK-NEXT: Section: .text (1)
93+
# CHECK-NEXT: BaseType: Null (0x0)
94+
# CHECK-NEXT: ComplexType: Null (0x0)
95+
# CHECK-NEXT: StorageClass: External (0x2)
96+
# CHECK-NEXT: AuxSymbolCount: 0
97+
# CHECK-NEXT: }
8998
# CHECK-NEXT: ]
9099

91100
# NO: Symbols [
@@ -237,4 +246,13 @@ symbols:
237246
SimpleType: IMAGE_SYM_TYPE_NULL
238247
ComplexType: IMAGE_SYM_DTYPE_NULL
239248
StorageClass: IMAGE_SYM_CLASS_LABEL
249+
- Name: weak_main
250+
Value: 0
251+
SectionNumber: 0
252+
SimpleType: IMAGE_SYM_TYPE_NULL
253+
ComplexType: IMAGE_SYM_DTYPE_NULL
254+
StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
255+
WeakExternal:
256+
TagIndex: 10
257+
Characteristics: IMAGE_WEAK_EXTERN_SEARCH_ALIAS
240258
...

0 commit comments

Comments
 (0)