-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Description
Bugzilla Link | 45273 |
Resolution | FIXED |
Resolved on | Apr 13, 2020 13:55 |
Version | unspecified |
OS | All |
Blocks | #44654 |
CC | @dwblaikie,@MaskRay,@mstorsjo,@rnk,@rui314,@smeenai,@tstellar |
Fixed by commit(s) | c579a5b edbe962 |
Extended Description
Generating DWARF debug information breaks dead code removal in PE/COFF images. Consider test.c file with the following content:
void unused() {}
void entry() {}
After compiling and linking it with LLD I expect unused function to be stripped from the resulting binary, but for some reason it does not happen. These are the commands I execute:
$ clang -g -fno-builtin -ffunction-sections -fdata-sections -fno-common -fno-stack-protector -mno-implicit-float -mms-bitfields -mno-stack-arg-probe -nostdlib -nostdlibinc -m64 -mno-red-zone -mcmodel=small -O0 -target x86_64-unknown-windows-gnu -gdwarf -funwind-tables -c -o test.obj test.c
$ lld-link /OUT:test.dll /NOLOGO /NODEFAULTLIB /IGNORE:4001 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /Machine:X64 /DLL /ENTRY:entry /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /DEBUG:DWARF /lldmap test.obj
$ llvm-readobj -t test.dll
File: test.dll
Format: COFF-x86-64
Arch: x86_64
AddressSize: 64bit
Symbols [
Symbol {
Name: .text
Value: 0
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .text$unused
Value: 16
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: unused
Value: 16
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Function (0x2)
StorageClass: External (0x2)
AuxSymbolCount: 0
}
Symbol {
Name: .text$entry
Value: 0
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: entry
Value: 0
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Function (0x2)
StorageClass: External (0x2)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_str
Value: 0
Section: .debug_str (8)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_abbrev
Value: 0
Section: .debug_abbrev (3)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_info
Value: 0
Section: .debug_info (4)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_ranges
Value: 0
Section: .debug_ranges (7)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_macinfo
Value: 0
Section: .debug_macinfo (6)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
Symbol {
Name: .debug_line
Value: 0
Section: .debug_line (5)
BaseType: Null (0x0)
ComplexType: Null (0x0)
StorageClass: Static (0x3)
AuxSymbolCount: 0
}
]
Not even (well, expectedly) it changes after stripping:
$ llvm-objcopy --strip-unneeded test.dll
$ llvm-readobj -t test.dll
File: test.dll
Format: COFF-x86-64
Arch: x86_64
AddressSize: 64bit
Symbols [
Symbol {
Name: unused
Value: 16
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Function (0x2)
StorageClass: External (0x2)
AuxSymbolCount: 0
}
Symbol {
Name: entry
Value: 0
Section: .text (1)
BaseType: Null (0x0)
ComplexType: Function (0x2)
StorageClass: External (0x2)
AuxSymbolCount: 0
}
]
From what I can tell the issue is in MarkLive implementation of LLD, which assumes DWARF symbols are live, and then links everything in the file:
llvm-project/lld/COFF/MarkLive.cpp
Lines 31 to 35 in 332fc71
// COMDAT section chunks are dead by default. Add non-COMDAT chunks. | |
for (Chunk *c : chunks) | |
if (auto *sc = dyn_cast<SectionChunk>(c)) | |
if (sc->live) | |
worklist.push_back(sc); |
These .debug_xxx symbols have 0x42100040 section characteristics (IMAGE_SCN_MEM_READ|IMAGE_SCN_MEM_DISCARDABLE|IMAGE_SCN_ALIGN_1BYTES|IMAGE_SCN_CNT_INITIALIZED_DATA), and can probably be skipped by IMAGE_SCN_MEM_DISCARDABLE flag, but I do not know if it is safe. For now I changed the code locally to simply skip symbols with DWARF section names in them.
for (Chunk *c : chunks) {
if (auto *sc = dyn_cast(c)) {
if (!sc->live || sc->isDWARF())
continue;
worklist.push_back(sc);
}
}