Self-hosted x86: UEFI application is not supported?

In Zig 0.15.1, I am trying to build my toy OS using the x86-64 native backend.
It builds and runs without issues using the LLVM backend, but when using the native backend, the generated image cannot be loaded by UEFI.

I am not very familiar with PE, but for example, the subsystem field in the PE header is set to WINDOWS_CUI instead of EFI_APPLICATION, which causes UEFI (OVMF) to treat it as unsupported. Looking at the relevant code in Zig, it seems that the subsystem is always fixed to .WINDOWS_CUI:

    const subsystem: coff_util.Subsystem = .WINDOWS_CUI;

Does the native backend not support building UEFI applications? Has anyone been able to build and run a UEFI application using the native backend?

FYI, here’s my build.zig:

    const surtr = b.addExecutable(.{
        .name = "BOOTX64.EFI",
        .root_module = b.createModule(.{
            .root_source_file = b.path("surtr/main.zig"),
            .target = b.resolveTargetQuery(.{
                .cpu_arch = .x86_64,
                .os_tag = .uefi,
            }),
            .optimize = optimize,
        }),
        .linkage = .static,
        .use_llvm = false,
    });
    surtr.entry = .{ .symbol_name = "main" };
    surtr.subsystem = .EfiApplication;
    b.installArtifact(surtr);
2 Likes

Not ready yet. See this and linked/related ones: x64 self-hosted backend generates EFI binaries with incorrect PE subsystem · Issue #25108 · ziglang/zig

2 Likes

Yes, I filed the issue after I got no reply here. The release note says the self-hosted backend is not ready on NetBSD, OpenBSD, and Windows. But I somehow didn’t notice UEFI (almost Windows) is not ready :frowning:
Thank you.