[llvm-bugs] [Bug 45273] New: DWARF breaks garbage collection in PE/COFF

via llvm-bugs llvm-bugs at lists.llvm.org
Sun Mar 22 00:41:30 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45273

            Bug ID: 45273
           Summary: DWARF breaks garbage collection in PE/COFF
           Product: lld
           Version: unspecified
          Hardware: All
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: COFF
          Assignee: unassignedbugs at nondot.org
          Reporter: vit9696 at avp.su
                CC: llvm-bugs at lists.llvm.org

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:

https://github.com/llvm/llvm-project/blob/332fc712c604612fa9a28354ebd48826ea4dc830/lld/COFF/MarkLive.cpp#L31-L35

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);
    }
  }

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200322/721d37e4/attachment.html>


More information about the llvm-bugs mailing list