llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.6k stars 11.82k forks source link

lld-link does not detect duplicate symbols if optimizations are enabled #39647

Closed pavelkryukov closed 5 years ago

pavelkryukov commented 5 years ago
Bugzilla Link 40300
Resolution DUPLICATE
Resolved on Jan 14, 2019 15:02
Version unspecified
OS Windows NT
CC @rnk,@smithp35

Extended Description

Hi

Consider two following simple C sources defining 'int func()'

PS D:\Dev> cat test.c int func() { return 0; } PS D:\Dev> cat main.c int func() { return 1; }

int main() { return func(); }

lld-link.exe does not warn about duplicate symbols

PS D:\Dev> clang-cl /O1 /c test.c PS D:\Dev> clang-cl /O1 /c main.c PS D:\Dev> lld-link test.obj main.obj PS D:\Dev> .\test PS D:\Dev> lld-link.exe --version LLD 7.0.0

However, if we disable optimizations, warning appears:

PS D:\Dev> clang-cl /c main.c PS D:\Dev> clang-cl /c test.c PS D:\Dev> lld-link test.obj main.obj lld-link.exe: error: duplicate symbol: func in test.obj and in main.obj

MSCV linker emits a warning in both cases (sorry for the Russian output)

PS D:\Dev> clang-cl /O1 /c test.c PS D:\Dev> clang-cl /O1 /c main.c PS D:\Dev> link test.obj main.obj Microsoft (R) Incremental Linker Version 14.14.26430.0 Copyright (C) Microsoft Corporation. All rights reserved.

main.obj : error LNK2005: func уже определен в test.obj test.exe : fatal error LNK1169: обнаружен многократно определенный символ - один или более

GCC does the same:

PS D:\Dev> gcc test.c -O1 -c PS D:\Dev> gcc main.c -O1 -c PS D:\Dev> gcc main.c -O1 -c PS D:\Dev> ld test.o main.o main.o:main.c:(.text+0x0): multiple definition of func' test.o:test.c:(.text+0x0): first defined here main.o:main.c:(.text+0xd): undefined reference to__main'

pavelkryukov commented 5 years ago

Thanks. Indeed, the original bug has not appeared in search results...

rnk commented 5 years ago

Coincidentally, we ran into this last month. It's filed under a title that's more technical but less understandable to users. Hopefully it'll be fixed soon.

This bug has been marked as a duplicate of bug llvm/llvm-project#39441