Skip to content

lld-link /OPT:REF drops a locally defined function reached only through an __imp_ slot (upstream LLVM) #1002

Duplicate

Description

@Sunrisepeak

lld-link, with /OPT:REF, discards a function that is defined in the link and reached only through an __imp_ slot. LLD creates that slot when a declaration says __declspec(dllimport) and the definition is local. The slot then holds the image base, and the first call through it is an access violation. MSVC's link.exe keeps the function.

Reproduction

// a.c
__declspec(dllimport) int foo(void);
int main(void) { return foo(); }
// b.c
int foo(void) { return 42; }
clang --target=x86_64-pc-windows-msvc -O2 -ffunction-sections -c a.c b.c
lld-link /entry:main /subsystem:console /nodefaultlib /opt:ref a.o b.o /out:t.exe

lld prints locally defined symbol imported: foo (defined in b.o) [LNK4217]. The output has an 8-byte .text (only jmp *slot), and its .rdata slot is 0x140000000, the image base. With /opt:noref, or without -ffunction-sections, the slot points at foo.

Linker /OPT:REF /OPT:NOREF
MSVC link.exe (VS 2022, windows-2022 runner, cl /O2 /Gy) exit 42 --
lld-link 20.1.8 (runner) exit 0xC0000005 exit 42
lld-link 20.1.7, 22.1.8, 23.1.3 (inspected) slot = image base slot = foo

Where mcpp meets it

mcpp compiles test units with one section per function and links tests with section GC (2026.11.9.1). Release links collect sections too. A static library whose header declares dllimport by default crashes at its first call when a consumer does not define the library's static-API macro. The first such crash was mcpp pack --compress xz (mwrap.xz 5.8.3.1). The mcpp-index archive members crashed the same way on Windows, from mcpp 2026.11.9.1 on: 2026.11.8.1 passes, and 2026.11.9.1 fails with exit 133.

Workaround

A static library states its static-API macro for every TU that includes its headers. In the mcpp-index descriptors this is a staticlib feature that default implies, because a feature's defines reach consumers: compat.xz (LZMA_API_STATIC), compat.libarchive (LIBARCHIVE_STATIC), and the packages that already did so (compat.curl, compat.c-ares). The correct use of these headers requires the macro whichever linker is used. With it, the lld defect is not reached.

The upstream report on llvm/llvm-project is still to be filed.

Activity

  1. added
    upstream-bug
    on Nov 22, 2026
  2. speak-agent commented on Nov 26, 2026

    @speak-agent
    Member

    Moved to the upstream defects register, #1027: UP-09.

    The entry states what is measured, what mcpp does about it and when it is retired. This report stays as the full record and reproduction; a new measurement or a change of workaround goes to the entry's comment.

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

    upstream-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions