ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
35.12k stars 2.56k forks source link

undefined symbol: MapViewOfFile2 #22040

Open lawrenceamer opened 5 days ago

lawrenceamer commented 5 days ago

Zig Version

0.13.0

Steps to Reproduce and Observed Output

it seems MapViewOfFile2 is not declared or linked into Zig code base yet, currently i am declaring it externally :

pub extern "kernel32" fn MapViewOfFile2(
    FileMappingHandle: HANDLE,
    ProcessHandle: HANDLE,
    Offset: u64, 
    BaseAddress: ?*anyopaque, 
    ViewSize: usize, // SIZE_T
    AllocationType: u32, // ULONG
    PageProtection: u32, // ULONG
) callconv(WINAPI) ?*anyopaque; 

raising the following linking error :

error: warning(link): unexpected LLD stderr:
lld-link: warning: undefined symbol: MapViewOfFile2

any alternative solution? if we imported the C headeand then call it from it's name space should be a temporary solution?

Expected Output

error: warning(link): unexpected LLD stderr: lld-link: warning: undefined symbol: MapViewOfFile2

lawrenceamer commented 5 days ago

it seems it is not part of Mingw64 https://github.com/msys2-contrib/mingw-w64/blob/master/mingw-w64-headers/include/memoryapi.h

nektro commented 5 days ago

msdn https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffile2

lawrenceamer commented 5 days ago

@nektro I think you are referring to the following

pub extern "api-ms-win-core-memory-l1-1-5" fn UnmapViewOfFile2(
    Process: ?HANDLE,
    BaseAddress: ?*anyopaque,
    UnmapFlags: UNMAP_VIEW_OF_FILE_FLAGS,
) callconv(@import("std").os.windows.WINAPI) BOOL;

and thats limited to windows10.0.15063 check out here : https://github.com/marlersoft/zigwin32/blob/32d085ee67374ad2ec24fc012e6876ca27958fb7/win32/system/memory.zig#L1098

mesidex commented 1 day ago

As a workaround, does NtMapViewOfSection suit your use case?

https://github.com/ziglang/zig/blob/e163ae794b6d6598833c6a8248a818e1e49ce794/lib/std/os/windows/ntdll.zig#L144

lawrenceamer commented 1 day ago

@mesidex NtMapViewofSection is different than MapViewFile2, however i managed to make it work with following extern API inline call.

https://learn.microsoft.com/en-us/windows/win32/api/memoryapi/nf-memoryapi-mapviewoffilenuma2

pub extern "api-ms-win-core-memory-l1-1-5" fn MapViewOfFileNuma2(
    FileMappingHandle: HANDLE,
    ProcessHandle: HANDLE,
    Offset: u64, // ULONG64
    BaseAddress: ?*anyopaque, // PVOID (optional)
    ViewSize: usize, // SIZE_T
    AllocationType: u32, // ULONG
    PageProtection: u32, // ULONG
) callconv(windows.WINAPI) ?*anyopaque; // Returns PVOID