<html>
<head>
<base href="https://bugs.llvm.org/">
</head>
<body><table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Bug ID</th>
<td><a class="bz_bug_link
bz_status_NEW "
title="NEW - DWARF breaks garbage collection in PE/COFF"
href="https://bugs.llvm.org/show_bug.cgi?id=45273">45273</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>DWARF breaks garbage collection in PE/COFF
</td>
</tr>
<tr>
<th>Product</th>
<td>lld
</td>
</tr>
<tr>
<th>Version</th>
<td>unspecified
</td>
</tr>
<tr>
<th>Hardware</th>
<td>All
</td>
</tr>
<tr>
<th>OS</th>
<td>All
</td>
</tr>
<tr>
<th>Status</th>
<td>NEW
</td>
</tr>
<tr>
<th>Severity</th>
<td>enhancement
</td>
</tr>
<tr>
<th>Priority</th>
<td>P
</td>
</tr>
<tr>
<th>Component</th>
<td>COFF
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>vit9696@avp.su
</td>
</tr>
<tr>
<th>CC</th>
<td>llvm-bugs@lists.llvm.org
</td>
</tr></table>
<p>
<div>
<pre>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:
<a href="https://github.com/llvm/llvm-project/blob/332fc712c604612fa9a28354ebd48826ea4dc830/lld/COFF/MarkLive.cpp#L31-L35">https://github.com/llvm/llvm-project/blob/332fc712c604612fa9a28354ebd48826ea4dc830/lld/COFF/MarkLive.cpp#L31-L35</a>
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<SectionChunk>(c)) {
if (!sc->live || sc->isDWARF())
continue;
worklist.push_back(sc);
}
}</pre>
</div>
</p>
<hr>
<span>You are receiving this mail because:</span>
<ul>
<li>You are on the CC list for the bug.</li>
</ul>
</body>
</html>