Skip to content

Commit 28212df

Browse files
committed
[COFF] Fix error handling on duplicates for import library symbols
Normally one wouldn't run into that case, but it is possible with a little creative ordering of special libraries. Differential Revision: https://reviews.llvm.org/D53388 llvm-svn: 344776
1 parent d243ac6 commit 28212df

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,10 @@ void ImportFile::parse() {
501501
ExternalName = ExtName;
502502

503503
ImpSym = Symtab->addImportData(ImpName, this);
504+
// If this was a duplicate, we logged an error but may continue;
505+
// in this case, ImpSym is nullptr.
506+
if (!ImpSym)
507+
return;
504508

505509
if (Hdr->getType() == llvm::COFF::IMPORT_CONST)
506510
static_cast<void>(Symtab->addImportData(Name, this));

lld/test/COFF/duplicate-imp-func.s

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# REQUIRES: x86
2+
3+
# RUN: echo -e ".globl libfunc\n.text\nlibfunc:\nret" > %t.lib.s
4+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.lib.s -filetype=obj -o %t.lib.o
5+
# RUN: lld-link -lldmingw -dll -out:%t.lib.dll -entry:libfunc %t.lib.o -implib:%t.lib.dll.a
6+
7+
# RUN: echo -e ".globl helper1\n.text\nhelper1:\ncall libfunc\nret" > %t.helper1.s
8+
# RUN: echo -e ".globl helper2\n.text\nhelper2:\nret\n.globl libfunc\n.globl __imp_libfunc\nlibfunc:\nret\n.data\n__imp_libfunc:\n.quad libfunc" > %t.helper2.s
9+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.helper1.s -filetype=obj -o %t.helper1.o
10+
# RUN: llvm-mc -triple=x86_64-windows-gnu %t.helper2.s -filetype=obj -o %t.helper2.o
11+
12+
# RUN: llvm-ar rcs %t.helper.a %t.helper1.o %t.helper2.o
13+
14+
# RUN: llvm-mc -triple=x86_64-windows-gnu %s -filetype=obj -o %t.main.o
15+
16+
# Simulate a setup, where two libraries provide the same import function;
17+
# %t.lib.dll.a is a pure import library which provides "libfunc".
18+
# %t.helper.a is a static library which contains "helper1" and "helper2".
19+
#
20+
# helper1 contains an undefined reference to libfunc. helper2 contains a
21+
# fake local implementation of libfunc, together with the __imp_libfunc
22+
# stub.
23+
#
24+
# %t.lib.dll.a is listed before %t.helper.a on the command line. After
25+
# including helper1, the library member that first declared the Lazy libfunc
26+
# (%t.lib.dll.a) gets enqueued to be loaded. Before that gets done, helper2
27+
# gets loaded, which also turns out to provide a definition of libfunc.
28+
# Once the import library member from %t.lib.dll.a gets loaded, libfunc
29+
# and __imp_libfunc already are defined.
30+
31+
# Just check that this fails cleanly (doesn't crash).
32+
# RUN: not lld-link -lldmingw -out:%t.main.exe -entry:main %t.main.o %t.lib.dll.a %t.helper.a
33+
34+
# Test with %t.helper.a on the command line; in this case we won't try to
35+
# include libfunc from %t.lib.dll.a and everything works fine.
36+
# RUN: lld-link -lldmingw -out:%t.main.exe -entry:main %t.main.o %t.helper.a %t.lib.dll.a
37+
38+
.globl main
39+
.text
40+
main:
41+
call helper1
42+
call helper2
43+
ret

0 commit comments

Comments
 (0)