VS2022 Link - Prevent custom section from being removed?

2026-07-10

Stack Overflow: View Question

Tags: linker, visual-studio-2022, edk2

Score: 0 | Views: 49

The asker is building an EDK2/UEFI binary (almost certainly a shim-style bootloader) and needs to embed an SBAT (Secure Boot Advanced Targeting) revocation metadata blob into a dedicated PE section named .sbat. Shim's verifier reads this section by name at boot time, so the section must exist in the final image with that exact name — not just the data, the named container.

Their code declares the variable via #pragma section(".sbat", read) and __declspec(allocate(".sbat")), then uses /INCLUDE:sbat to keep the symbol alive against dead-strip. The symbol survives, but the .sbat section vanishes from the output PE.

Why this is subtle: the MSVC linker aggressively merges sections whose names share a common prefix and compatible attributes. Any section beginning with .rdata gets folded into .rdata; anything read-only-data-like with a short name may be merged into an existing section. The read attribute in #pragma section maps to characteristics (IMAGE_SCN_MEM_READ | IMAGE_SCN_CNT_INITIALIZED_DATA) that look identical to .rdata, and link.exe will happily coalesce them. The data survives; the name does not.

Direction toward a fix:

Gotchas: shim on Linux uses objcopy --set-section-flags to place SBAT with specific flags (alloc,readonly,data,contents). On MSVC/PE, the analog is section characteristics, and the shim verifier is strict about them. Even if the name is preserved, wrong characteristics can cause boot-time rejection. Also: volatile on the array doesn't prevent section merging — it only prevents compiler-level optimization of accesses.

The challenge: Keeping a symbol alive is a different problem from keeping its named section alive — MSVC's linker will silently fold identically-attributed sections regardless of /INCLUDE.

All newsletters