Skip to content

lld-link.exe does not provide unwind information on Windows x64 for tailMergeX64 code when using /delayload #59639

Closed

Description

@yjugl

Hello,

As described below, lld-link.exe adds no unwind information for the tailMergeX64 code when using /delayload, even though this code includes a call instruction. As stated in Microsoft documentation:

Table-based exception handling requires a table entry for all functions that allocate stack space or call another function (for example, nonleaf functions). [...] Failure to do so will result in unreliable exception handling and debugging of processes.

This missing unwind information currently has the following real-world impact in the Firefox web browser for Windows x64, when an exception is raised with a return address pointing to tailMergeX64 stored on the stack:

  • this can prevent the crash reporter from being reached, because Microsoft's internal code in ntdll that unwinds structured exception handlers may stop at tailMergeX64, resulting in no crash dump being created (see bug 1801322 comment 1);
  • when we somehow succeed to get a crash dump, our crash analyzer generates garbage once it reaches tailMergeX64, resulting in hard-to-triage crashes (see bug 1740094).

Now I will provide more details with a simple example that reproduces the issue. When using the latest LLVM Windows x64 public release:

>"C:\Program Files\LLVM\bin\clang.exe" --version
clang version 15.0.6
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\LLVM\bin

>"C:\Program Files\LLVM\bin\lld-link.exe" --version
LLD 15.0.6

To compile the following program into an object file:

#include <windows.h>

int main(void) {
  MessageBoxW(NULL, L"Hello", L"Hello", MB_OK);
  return 0;
}

Then link it with lld-link.exe using /delayload:

> "C:\Program Files\LLVM\bin\clang.exe" -c main.cpp
> "C:\Program Files\LLVM\bin\lld-link.exe" main.o /defaultlib:libcmt.lib /defaultlib:user32.lib /defaultlib:delayimp.lib /delayload:user32.dll

The tailMergeX64 code is added at the end of the .text section to handle delay-loading, but there is not associated unwind information for this function in the .pdata (RUNTIME_FUNCTION) and .xdata (UNWIND_INFO) sections.

This can be verified with a script that I provide here:

> python show_missing_unwind_info.py main.exe
Loading 'main.exe', please wait...

Section .text: unwind information covers 94.00% of 51712 bytes.

Looking for call instructions in the parts not covered by unwind information...

0x14000d8af:    e8903dffff      call    0x140001644
0x14000d8b4:    660f6f0424      movdqa  xmm0, xmmword ptr [rsp]
0x14000d8b9:    660f6f4c2410    movdqa  xmm1, xmmword ptr [rsp + 0x10]
0x14000d8bf:    660f6f542420    movdqa  xmm2, xmmword ptr [rsp + 0x20]

Found 1 call instructions without unwind information in section .text.
Note that some of those may be false positives.

Or with a debug session, here in WinDbg:

# This is the entry in the import address table for MessageBoxW
0:000> dq main+179f0 L1
00007ff6`afde79f0  00007ff6`afddd878
# This is the delay-load jumper for MessageBoxW
0:000> u main+d878 L0x18
main+0xd878:
00007ff6`afddd878 488d0571a10000  lea     rax,[main+0x179f0 (00007ff6`afde79f0)]
00007ff6`afddd87f e900000000      jmp     main+0xd884 (00007ff6`afddd884)
# This is the tailMergeX64 function for delay-loaded user32.dll
00007ff6`afddd884 51              push    rcx
00007ff6`afddd885 52              push    rdx
00007ff6`afddd886 4150            push    r8
00007ff6`afddd888 4151            push    r9
00007ff6`afddd88a 4883ec48        sub     rsp,48h
00007ff6`afddd88e 660f7f0424      movdqa  xmmword ptr [rsp],xmm0
00007ff6`afddd893 660f7f4c2410    movdqa  xmmword ptr [rsp+10h],xmm1
00007ff6`afddd899 660f7f542420    movdqa  xmmword ptr [rsp+20h],xmm2
00007ff6`afddd89f 660f7f5c2430    movdqa  xmmword ptr [rsp+30h],xmm3
00007ff6`afddd8a5 488bd0          mov     rdx,rax
00007ff6`afddd8a8 488d0d817c0000  lea     rcx,[main+0x15530 (00007ff6`afde5530)]
# This calls __delayLoadHelper2
00007ff6`afddd8af e8903dffff      call    main+0x1644 (00007ff6`afdd1644)
00007ff6`afddd8b4 660f6f0424      movdqa  xmm0,xmmword ptr [rsp]
00007ff6`afddd8b9 660f6f4c2410    movdqa  xmm1,xmmword ptr [rsp+10h]
00007ff6`afddd8bf 660f6f542420    movdqa  xmm2,xmmword ptr [rsp+20h]
00007ff6`afddd8c5 660f6f5c2430    movdqa  xmm3,xmmword ptr [rsp+30h]
00007ff6`afddd8cb 4883c448        add     rsp,48h
00007ff6`afddd8cf 4159            pop     r9
00007ff6`afddd8d1 4158            pop     r8
00007ff6`afddd8d3 5a              pop     rdx
00007ff6`afddd8d4 59              pop     rcx
00007ff6`afddd8d5 ffe0            jmp     rax
# Displaying full contents of the .pdata section (which is sorted)
0:000> dt ntdll!_IMAGE_RUNTIME_FUNCTION_ENTRY[0x128] main+0x19000
[...]
   +0x000 BeginAddress     : 0xd81b
   +0x004 EndAddress       : 0xd834
   +0x008 UnwindInfoAddress : 0x15ff8
   +0x008 UnwindData       : 0x15ff8

   +0x000 BeginAddress     : 0xd834
   +0x004 EndAddress       : 0xd84c
   +0x008 UnwindInfoAddress : 0x15ff8
   +0x008 UnwindData       : 0x15ff8

   +0x000 BeginAddress     : 0xd84c
   +0x004 EndAddress       : 0xd878
   +0x008 UnwindInfoAddress : 0x15ff8
   +0x008 UnwindData       : 0x15ff8

# There should be an entry here for the tailMergeX64 function (BeginAddress: 0xd884),
# but the unwind information ends here
   +0x000 BeginAddress     : 0
   +0x004 EndAddress       : 0
   +0x008 UnwindInfoAddress : 0
   +0x008 UnwindData       : 0

The expected result is the one obtained when linking with MSVC's link.exe instead:

> link.exe .\main.o libcmt.lib user32.lib delayimp.lib /DELAYLOAD:user32.dll
Microsoft (R) Incremental Linker Version 14.33.31630.0
Copyright (C) Microsoft Corporation.  All rights reserved.

Or using clang-cl.exe to do compile and link at the same time, although I'm not sure what this does under the hood:

"C:\Program Files\LLVM\bin\clang-cl.exe" main.cpp user32.lib delayimp.lib /link/DELAYLOAD:user32.dll

In both cases, unwind information is present for the tailMergeX64 code; note that this code also differs slightly from the one added by lld-link.exe.

This can be seen with the script:

> python show_missing_unwind_info.py main.exe
Loading 'main.exe', please wait...

Section .text: unwind information covers 94.23% of 51712 bytes.

Looking for call instructions in the parts not covered by unwind information...

Found no call instructions without unwind information in section .text.

Or, in a debug session:

# This is the entry in the import address table for MessageBoxW
0:000> dq main+0x18a10
00007ff7`2fcd8a10  00007ff7`2fcc1030
# This is the delay-load jumper for MessageBoxW
0:000> u main+0x1030 L0x1C
main+0x1030:
00007ff7`2fcc1030 488d05d9790100  lea     rax,[main+0x18a10 (00007ff7`2fcd8a10)]
00007ff7`2fcc1037 e900000000      jmp     main+0x103c (00007ff7`2fcc103c)
# This is the tailMergeX64 function for delay-loaded user32.dll
00007ff7`2fcc103c 48894c2408      mov     qword ptr [rsp+8],rcx
00007ff7`2fcc1041 4889542410      mov     qword ptr [rsp+10h],rdx
00007ff7`2fcc1046 4c89442418      mov     qword ptr [rsp+18h],r8
00007ff7`2fcc104b 4c894c2420      mov     qword ptr [rsp+20h],r9
00007ff7`2fcc1050 4883ec68        sub     rsp,68h
00007ff7`2fcc1054 660f7f442420    movdqa  xmmword ptr [rsp+20h],xmm0
00007ff7`2fcc105a 660f7f4c2430    movdqa  xmmword ptr [rsp+30h],xmm1
00007ff7`2fcc1060 660f7f542440    movdqa  xmmword ptr [rsp+40h],xmm2
00007ff7`2fcc1066 660f7f5c2450    movdqa  xmmword ptr [rsp+50h],xmm3
00007ff7`2fcc106c 488bd0          mov     rdx,rax
00007ff7`2fcc106f 488d0df2570100  lea     rcx,[main+0x16868 (00007ff7`2fcd6868)]
# This calls __delayLoadHelper2
00007ff7`2fcc1076 e8e1030000      call    main+0x145c (00007ff7`2fcc145c)
00007ff7`2fcc107b 660f6f442420    movdqa  xmm0,xmmword ptr [rsp+20h]
00007ff7`2fcc1081 660f6f4c2430    movdqa  xmm1,xmmword ptr [rsp+30h]
00007ff7`2fcc1087 660f6f542440    movdqa  xmm2,xmmword ptr [rsp+40h]
00007ff7`2fcc108d 660f6f5c2450    movdqa  xmm3,xmmword ptr [rsp+50h]
00007ff7`2fcc1093 488b4c2470      mov     rcx,qword ptr [rsp+70h]
00007ff7`2fcc1098 488b542478      mov     rdx,qword ptr [rsp+78h]
00007ff7`2fcc109d 4c8b842480000000 mov     r8,qword ptr [rsp+80h]
00007ff7`2fcc10a5 4c8b8c2488000000 mov     r9,qword ptr [rsp+88h]
00007ff7`2fcc10ad 4883c468        add     rsp,68h
00007ff7`2fcc10b1 eb00            jmp     main+0x10b3 (00007ff7`2fcc10b3)
00007ff7`2fcc10b3 ffe0            jmp     rax
00007ff7`2fcc10b5 cc              int     3
00007ff7`2fcc10b6 cc              int     3
00007ff7`2fcc10b7 cc              int     3
# Displaying contents of the .pdata section (which is sorted)
0:000> dt ntdll!_IMAGE_RUNTIME_FUNCTION_ENTRY[4] main+0x1a000
[4]    +0x000 BeginAddress     : 0x1000
   +0x004 EndAddress       : 0x102a
   +0x008 UnwindInfoAddress : 0x15cd0
   +0x008 UnwindData       : 0x15cd0

-> +0x000 BeginAddress     : 0x103c
   +0x004 EndAddress       : 0x10b3
   +0x008 UnwindInfoAddress : 0x15cd8
   +0x008 UnwindData       : 0x15cd8   <-

   +0x000 BeginAddress     : 0x10b8
   +0x004 EndAddress       : 0x1162
   +0x008 UnwindInfoAddress : 0x15d18
   +0x008 UnwindData       : 0x15d18

   +0x000 BeginAddress     : 0x1164
   +0x004 EndAddress       : 0x1202
   +0x008 UnwindInfoAddress : 0x15ce0
   +0x008 UnwindData       : 0x15ce0

   [...]
# This is the unwind information for the tailMergeX64 function,
# stored in the .xdata section
0:000> db main+0x15cd8 L8
00007ff7`2fcd5cd8  01 18 01 00 18 c2 00 00

Activity

  1. yjugl commented on Jan 13, 2023

    @yjugl
    Author

    The following unwind information can be used for the current version of tailMergeX64:

    uint8_t unwindInfo[] = {
      0x01, // Version=1, Flags=UNW_FLAG_NHANDLER
      0x0a, // Size of prolog
      0x05, // Count of unwind codes
      0x00, // No frame register
      0x0a, 0x82, // Offset 0xa: UWOP_ALLOC_SMALL(0x48)
      0x06, 0x02, // Offset 6: UWOP_ALLOC_SMALL(8)
      0x04, 0x02, // Offset 4: UWOP_ALLOC_SMALL(8)
      0x02, 0x02, // Offset 2: UWOP_ALLOC_SMALL(8)
      0x01, 0x02, // Offset 1: UWOP_ALLOC_SMALL(8)
      0x00, 0x00 // Padding to align on 32-bits
    };

    It was originally generated for the following MASM procedure:

    .DATA
    
    delayImport DW 0
    
    .CODE
    
    delayLoadHelper PROC
      ret
    delayLoadHelper ENDP
    
    tailMergeX64 PROC FRAME
       ; "For pushes of volatile registers, use an .ALLOCSTACK 8"
       ; https://learn.microsoft.com/en-us/cpp/build/exception-handling-x64#raw-pseudo-operations
       ; rcx, rdx, r8, r9 are all volatile
       ; https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions#register-volatility-and-preservation
       push    rcx
       .ALLOCSTACK 8
       push    rdx
       .ALLOCSTACK 8
       push    r8
       .ALLOCSTACK 8
       push    r9
       .ALLOCSTACK 8
       sub     rsp, 48h
       .ALLOCSTACK 048h
       .ENDPROLOG
    
       ; xmm0-xmm3 are volatile, their saving does not impact unwind information
       ; https://learn.microsoft.com/en-us/cpp/build/x64-software-conventions#register-volatility-and-preservation
       movdqa  xmmword ptr [rsp], xmm0
       movdqa  xmmword ptr [rsp+10h], xmm1
       movdqa  xmmword ptr [rsp+20h], xmm2
       movdqa  xmmword ptr [rsp+30h], xmm3
    
       mov     rdx, rax
       lea     rcx, [delayImport]
       call    delayLoadHelper
    
       movdqa  xmm0, xmmword ptr [rsp]
       movdqa  xmm1, xmmword ptr [rsp+10h]
       movdqa  xmm2, xmmword ptr [rsp+20h]
       movdqa  xmm3, xmmword ptr [rsp+30h]
    
       ; This is an officially recognized epilogue:
       ; "It must consist of either an add RSP,constant or lea RSP,constant[FPReg],
       ;  followed by a series of zero or more 8-byte register pops and a return or
       ;  a jmp."
       ; https://learn.microsoft.com/en-us/cpp/build/prolog-and-epilog#epilog-code
       add     rsp, 48h
       pop     r9
       pop     r8
       pop     rdx
       pop     rcx
       jmp     rax
    tailMergeX64 ENDP
    
    END

    The bytes generated for the MASM procedure were the same as the ones hardcoded for tailMergeX64 in lld/COFF/DLL.cpp (except for the placeholder zeros).

  2. yjugl commented on Jan 13, 2023

    @yjugl
    Author

    The patch looks good to me!

    First, the script is happy:

    >python show_missing_unwind_info.py ..\DelayLoad\DelayLoad\main.exe
    Loading '..\DelayLoad\DelayLoad\main.exe', please wait...
    
    Section .text: unwind information covers 94.16% of 51712 bytes.
    
    Looking for call instructions in the parts not covered by unwind information...
    
    Found no call instructions without unwind information in section .text.
    

    Then, I created (with Visual Studio) a DLL that triggers an exception at load:

    #include <windows.h>
    #include <cstdio>
    
    __declspec(dllexport) void DelayLoadedFunc()
    {
        printf("Hello world\n");
    }
    
    BOOL APIENTRY DllMain(HMODULE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
    {
        switch (ul_reason_for_call)
        {
        case DLL_PROCESS_ATTACH:
            DebugBreak();
            break;
    
        case DLL_THREAD_ATTACH:
        case DLL_THREAD_DETACH:
        case DLL_PROCESS_DETACH:
            break;
        }
        return TRUE;
    }

    Then compiled a simple program that delay-loads the DLL by calling into the function:

    void DelayLoadedFunc();
    
    int main(void) {
        DelayLoadedFunc();
        return 0;
    }
    > "C:\Program Files\LLVM\bin\clang.exe" -c main.cpp
    > "C:\repos\llvm-project\build\Release\bin\lld-link.exe" main.o /defaultlib:libcmt.lib /defaultlib:delayimp.lib DelayLoadedDll.lib /delayload:DelayLoadedDll.dll
    

    When launching the program in a debugger, the stack is correctly fully accessible when the exception is triggered:

    KERNELBASE!wil::details::DebugBreak+0x2:
    00007ffa`650388e2 cc              int     3
    0:000> k
     # Child-SP          RetAddr               Call Site
    00 0000002d`f2fdf1a8 00007ff9`9966108f     KERNELBASE!wil::details::DebugBreak+0x2
    01 0000002d`f2fdf1b0 00007ff9`9966134b     DelayLoadedDll!DllMain+0xf [C:\repos\DelayLoad\DelayLoadedDll\dllmain.cpp @ 26] 
    02 0000002d`f2fdf1e0 00007ffa`67b38ddf     DelayLoadedDll!dllmain_dispatch+0x8f [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp @ 281] 
    03 0000002d`f2fdf240 00007ffa`67b7d6bd     ntdll!LdrpCallInitRoutine+0x6b
    04 0000002d`f2fdf2b0 00007ffa`67b7d46e     ntdll!LdrpInitializeNode+0x1c9
    05 0000002d`f2fdf400 00007ffa`67b3dd6d     ntdll!LdrpInitializeGraphRecurse+0x42
    06 0000002d`f2fdf440 00007ffa`67b39080     ntdll!LdrpPrepareModuleForExecution+0xc5
    07 0000002d`f2fdf480 00007ffa`67b2932c     ntdll!LdrpLoadDllInternal+0x20c
    08 0000002d`f2fdf520 00007ffa`67b3a99a     ntdll!LdrpLoadDll+0xb0
    09 0000002d`f2fdf6e0 00007ffa`64f5bbb2     ntdll!LdrLoadDll+0xfa
    0a 0000002d`f2fdf7d0 00007ffa`64f57c41     KERNELBASE!LoadLibraryExW+0x172
    0b 0000002d`f2fdf840 00007ff7`05ef177c     KERNELBASE!LoadLibraryExA+0x31
    0c 0000002d`f2fdf880 00007ff7`05efd8b2     main+0x177c
    0d 0000002d`f2fdf920 00007ff7`05ef1011     main+0xd8b2
    0e 0000002d`f2fdf990 00007ff7`05ef1208     main+0x1011
    0f 0000002d`f2fdf9c0 00007ffa`679226bd     main+0x1208
    10 0000002d`f2fdfa00 00007ffa`67b6dfb8     KERNEL32!BaseThreadInitThunk+0x1d
    11 0000002d`f2fdfa30 00000000`00000000     ntdll!RtlUserThreadStart+0x28
    

    Whereas linking with the public version of lld-link.exe yields a partial stack, because unwinding stops at tailMergeX64:

    KERNELBASE!wil::details::DebugBreak+0x2:
    00007ffa`650388e2 cc              int     3
    0:000> k
     # Child-SP          RetAddr               Call Site
    00 00000082`837ef338 00007ff9`9966108f     KERNELBASE!wil::details::DebugBreak+0x2
    01 00000082`837ef340 00007ff9`9966134b     DelayLoadedDll!DllMain+0xf [C:\repos\DelayLoad\DelayLoadedDll\dllmain.cpp @ 26] 
    02 00000082`837ef370 00007ffa`67b38ddf     DelayLoadedDll!dllmain_dispatch+0x8f [D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\dll_dllmain.cpp @ 281] 
    03 00000082`837ef3d0 00007ffa`67b7d6bd     ntdll!LdrpCallInitRoutine+0x6b
    04 00000082`837ef440 00007ffa`67b7d46e     ntdll!LdrpInitializeNode+0x1c9
    05 00000082`837ef590 00007ffa`67b3dd6d     ntdll!LdrpInitializeGraphRecurse+0x42
    06 00000082`837ef5d0 00007ffa`67b39080     ntdll!LdrpPrepareModuleForExecution+0xc5
    07 00000082`837ef610 00007ffa`67b2932c     ntdll!LdrpLoadDllInternal+0x20c
    08 00000082`837ef6b0 00007ffa`67b3a99a     ntdll!LdrpLoadDll+0xb0
    09 00000082`837ef870 00007ffa`64f5bbb2     ntdll!LdrLoadDll+0xfa
    0a 00000082`837ef960 00007ffa`64f57c41     KERNELBASE!LoadLibraryExW+0x172
    0b 00000082`837ef9d0 00007ff7`5a05177c     KERNELBASE!LoadLibraryExA+0x31
    0c 00000082`837efa10 00007ff7`5a05d8b2     main+0x177c
    0d 00000082`837efab0 00000000`00000000     main+0xd8b2
    0:000> u main+0xd8b2
    main+0xd8b2:
    00007ff7`5a05d8b2 660f6f0424      movdqa  xmm0,xmmword ptr [rsp]
    00007ff7`5a05d8b7 660f6f4c2410    movdqa  xmm1,xmmword ptr [rsp+10h]
    00007ff7`5a05d8bd 660f6f542420    movdqa  xmm2,xmmword ptr [rsp+20h]
    00007ff7`5a05d8c3 660f6f5c2430    movdqa  xmm3,xmmword ptr [rsp+30h]
    00007ff7`5a05d8c9 4883c448        add     rsp,48h
    00007ff7`5a05d8cd 4159            pop     r9
    00007ff7`5a05d8cf 4158            pop     r8
    00007ff7`5a05d8d1 5a              pop     rdx
    
  3. llvmbot commented on Jan 13, 2023

    @llvmbot
    Member

    @llvm/issue-subscribers-lld-coff

  4. added a commit that references this issue on Jan 16, 2023
    c512eda
  5. added a commit that references this issue on Oct 7, 2026
    d08b537
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions