Skip to content

Commit a6ffc9c

Browse files
committedJul 20, 2018
[COFF] Adjust how we flag weak externals
This fixes PR36096. Originally based on a patch by Martell Malone. Differential Revision: https://reviews.llvm.org/D44357 llvm-svn: 337613
1 parent d432d82 commit a6ffc9c

File tree

6 files changed

+71
-11
lines changed

6 files changed

+71
-11
lines changed
 

‎llvm/include/llvm/Object/COFF.h

+8
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ struct coff_symbol_generic {
276276
};
277277

278278
struct coff_aux_section_definition;
279+
struct coff_aux_weak_external;
279280

280281
class COFFSymbolRef {
281282
public:
@@ -360,6 +361,13 @@ class COFFSymbolRef {
360361
return getAux<coff_aux_section_definition>();
361362
}
362363

364+
const coff_aux_weak_external *getWeakExternal() const {
365+
if (!getNumberOfAuxSymbols() ||
366+
getStorageClass() != COFF::IMAGE_SYM_CLASS_WEAK_EXTERNAL)
367+
return nullptr;
368+
return getAux<coff_aux_weak_external>();
369+
}
370+
363371
bool isAbsolute() const {
364372
return getSectionNumber() == -1;
365373
}

‎llvm/lib/Object/ArchiveWriter.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ static bool isArchiveSymbol(const object::BasicSymbolRef &S) {
294294
return false;
295295
if (!(Symflags & object::SymbolRef::SF_Global))
296296
return false;
297-
if (Symflags & object::SymbolRef::SF_Undefined &&
298-
!(Symflags & object::SymbolRef::SF_Indirect))
297+
if (Symflags & object::SymbolRef::SF_Undefined)
299298
return false;
300299
return true;
301300
}

‎llvm/lib/Object/COFFObjectFile.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
217217
if (Symb.isExternal() || Symb.isWeakExternal())
218218
Result |= SymbolRef::SF_Global;
219219

220-
if (Symb.isWeakExternal()) {
220+
if (const coff_aux_weak_external *AWE = Symb.getWeakExternal()) {
221221
Result |= SymbolRef::SF_Weak;
222-
// We use indirect to allow the archiver to write weak externs
223-
Result |= SymbolRef::SF_Indirect;
222+
if (AWE->Characteristics != COFF::IMAGE_WEAK_EXTERN_SEARCH_ALIAS)
223+
Result |= SymbolRef::SF_Undefined;
224224
}
225225

226226
if (Symb.getSectionNumber() == COFF::IMAGE_SYM_ABSOLUTE)
@@ -235,7 +235,7 @@ uint32_t COFFObjectFile::getSymbolFlags(DataRefImpl Ref) const {
235235
if (Symb.isCommon())
236236
Result |= SymbolRef::SF_Common;
237237

238-
if (Symb.isAnyUndefined())
238+
if (Symb.isUndefined())
239239
Result |= SymbolRef::SF_Undefined;
240240

241241
return Result;
+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# RUN: yaml2obj %s -o %t.obj
2+
#
3+
# RUN: rm -f %t.ar
4+
# RUN: llvm-ar crs %t.a %t.obj
5+
# RUN: llvm-nm -print-armap %t.a | FileCheck %s
6+
7+
# CHECK: Archive map
8+
# CHECK-NEXT: WeakSearchAlias in coff-weak.yaml.tmp.obj
9+
# CHECK-EMPTY:
10+
11+
--- !COFF
12+
header:
13+
Machine: IMAGE_FILE_MACHINE_UNKNOWN
14+
Characteristics: [ ]
15+
sections:
16+
- Name: .drectve
17+
Characteristics: [ IMAGE_SCN_LNK_INFO, IMAGE_SCN_LNK_REMOVE ]
18+
SectionData: ''
19+
symbols:
20+
- Name: NormalUndefined
21+
Value: 0
22+
SectionNumber: 0
23+
SimpleType: IMAGE_SYM_TYPE_NULL
24+
ComplexType: IMAGE_SYM_DTYPE_NULL
25+
StorageClass: IMAGE_SYM_CLASS_EXTERNAL
26+
- Name: WeakSearchAlias
27+
Value: 0
28+
SectionNumber: 0
29+
SimpleType: IMAGE_SYM_TYPE_NULL
30+
ComplexType: IMAGE_SYM_DTYPE_NULL
31+
StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
32+
WeakExternal:
33+
TagIndex: 0
34+
Characteristics: IMAGE_WEAK_EXTERN_SEARCH_ALIAS
35+
- Name: WeakSearchLibrary
36+
Value: 0
37+
SectionNumber: 0
38+
SimpleType: IMAGE_SYM_TYPE_NULL
39+
ComplexType: IMAGE_SYM_DTYPE_NULL
40+
StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
41+
WeakExternal:
42+
TagIndex: 0
43+
Characteristics: IMAGE_WEAK_EXTERN_SEARCH_LIBRARY
44+
- Name: WeakSearchNolibrary
45+
Value: 0
46+
SectionNumber: 0
47+
SimpleType: IMAGE_SYM_TYPE_NULL
48+
ComplexType: IMAGE_SYM_DTYPE_NULL
49+
StorageClass: IMAGE_SYM_CLASS_WEAK_EXTERNAL
50+
WeakExternal:
51+
TagIndex: 0
52+
Characteristics: IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY
53+
...

‎llvm/test/tools/llvm-dlltool/coff-decorated.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ StdcallAlias@4==StdcallFunction@4
2222
; CHECK: Name type: name
2323
; CHECK: Symbol: __imp_??_7exception@@6B@
2424
; CHECK: Symbol: ??_7exception@@6B@
25-
; CHECK-NM: w _StdcallAlias@4
25+
; CHECK-NM: W _StdcallAlias@4
2626
; CHECK-NM: U _StdcallFunction@4

‎llvm/test/tools/llvm-dlltool/coff-weak-exports.def

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ ImpLibName2 = Implementation2 == AltTestFunction2
1515
ImpLibName3 = kernel32.Sleep
1616

1717
; CHECK: U AltTestFunction
18-
; CHECK-NEXT: w TestFunction
18+
; CHECK-NEXT: W TestFunction
1919
; CHECK: U __imp_AltTestFunction
20-
; CHECK-NEXT: w __imp_TestFunction
20+
; CHECK-NEXT: W __imp_TestFunction
2121
; CHECK: T ImpLibName
2222
; CHECK-NEXT: T __imp_ImpLibName
2323
; CHECK: U AltTestFunction2
24-
; CHECK-NEXT: w ImpLibName2
24+
; CHECK-NEXT: W ImpLibName2
2525
; CHECK: U __imp_AltTestFunction2
26-
; CHECK-NEXT: w __imp_ImpLibName2
26+
; CHECK-NEXT: W __imp_ImpLibName2
2727
; CHECK: T ImpLibName3
2828
; CHECK-NEXT: T __imp_ImpLibName3

0 commit comments

Comments
 (0)
Please sign in to comment.