<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 - Clang generates .debug_frame/.eh_frame with exceptions disabled for PE/COFF"
   href="https://bugs.llvm.org/show_bug.cgi?id=45324">45324</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang generates .debug_frame/.eh_frame with exceptions disabled for PE/COFF
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </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>Driver
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>vit9696@avp.su
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>For any file (e.g. test.c as below) compiled into PE/COFF PDB debug information
and exceptions disabled (via -fno-exceptions) for i686, clang generates
.eh_frame / .debug_frame sections.

I believe this is undesired, as exceptions are explicitly disabled. My reasons
are:
- exceptions are not supported in UEFI in any case;
- we use PDB debug information, so DWARF exception info will not be handled
anyway;
- it is critical to have smaller file size in the firmware.

test.c:

void entry() {
}

$ clang -g -fno-builtin -ffunction-sections -fdata-sections -fno-common
-fno-stack-protector -funsigned-char -mno-implicit-float -nostdlib -nostdlibinc
-m32 -Oz -march=i586 -target i686-unknown-windows-gnu -gcodeview
-fno-exceptions -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:X86 /DLL /ENTRY:entry
/SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /BASE:0 /DEBUG:GHASH /SAFESEH:NO /lldmap
test.obj

The issue happens, because Clang Driver defines exception model regardless of
whether exceptions are disabled or not[1], and then AsmPrinter uncodintionally
uses the exception model to create a DwarfCFIException instance[2]. Then it is
emitted by emitFunctionHeader, which calls DwarfCFIException::beginFunction,
and eventually reaches MCAsmStreamer::EmitCFISections.

There is also a LLVM nuance here. In LLVM MCAsmInfo has Target-specific
defaults, which are set to DWARF[3] for 32-bit PE/COFF, and LLVMTargetMachine
will not override exception model to disabled[4]. This is likely because
exceptions are turned off for C code, however, on a non-freestanding
environment we probably still want to generate .eh_frame/.debug_info to
smoothly mix C and C++ code.

In my opinion, to properly resolve this issue we need to explicitly handle
-fno-exceptions in clang equally to other -fXXX-exceptions flags, forward it to
LLVM, and then handle it in LLVM as well. This is slightly unintuitive, because
of three states: as -fexceptions (some exception model), -fno-exceptions (none
exception model), and no argument (some exception model), but probably the
cleanest route.

[1]
<a href="https://github.com/llvm/llvm-project/blob/080d046c91d26bd3b0afba817cf5c2f99d1288ff/clang/lib/Driver/ToolChains/Clang.cpp#L5623-L5651">https://github.com/llvm/llvm-project/blob/080d046c91d26bd3b0afba817cf5c2f99d1288ff/clang/lib/Driver/ToolChains/Clang.cpp#L5623-L5651</a>
[2]
<a href="https://github.com/llvm/llvm-project/blob/fdfe411e7c9a844e7465bbe443fb6d1d9dbc4539/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L353-L378">https://github.com/llvm/llvm-project/blob/fdfe411e7c9a844e7465bbe443fb6d1d9dbc4539/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp#L353-L378</a>
[3]
<a href="https://github.com/llvm/llvm-project/blob/ee2c0f76d74cc03e54389926c9e626987e1408c3/llvm/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp#L162-L170">https://github.com/llvm/llvm-project/blob/ee2c0f76d74cc03e54389926c9e626987e1408c3/llvm/lib/Target/X86/MCTargetDesc/X86MCAsmInfo.cpp#L162-L170</a>
[4]
<a href="https://github.com/llvm/llvm-project/blob/b61a4aaca5f8a1e71eeaee74d93cdd6fccbef065/llvm/lib/CodeGen/LLVMTargetMachine.cpp#L70-L71">https://github.com/llvm/llvm-project/blob/b61a4aaca5f8a1e71eeaee74d93cdd6fccbef065/llvm/lib/CodeGen/LLVMTargetMachine.cpp#L70-L71</a></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>