Skip to content

Conversation

squeek502
Copy link
Member

@squeek502 squeek502 commented Oct 1, 2025

Eliminates a dependency on LLVM with regards to the set of MinGW .def files shipped with Zig. This implementation is largely based on the LLVM implementation (specifically COFFModuleDefinition.cpp and COFFImportFile.cpp).

How this was tested

I developed/tested this in a separate repository here (the def.zig/implib.zig files are 1:1 between that repository and this PR)

I have a branch of zig here that adds a zig implib command that takes in a .def file and outputs two files: a .pp.def file that's the result of preprocessing with Aro (this is something that always happens during buildImportLib), and a .lib file that's the result of calling llvm_bindings.WriteImportLibrary after preprocessing.

I then have a gen.zig script here that walks Zig's lib/libc/mingw directory and spawns zig implib to create .pp.def/.lib pairs for every .def and .def.in file found.

Finally, I have a check.zig script here that walks a directory of these generated pairs and confirms that the new implementation generates a byte-for-byte identical .lib file for each .pp.def input.

The check.zig script passes cleanly for the entire lib/libc/mingw directory when targeting all supported windows-gnu targets: x86_64, x86, thumb, and aarch64 (see here for why these are specified).

(note that the byte-for-byte nature means that the implementation has inherited a few quirks from LLVM around size counts/padding; those quirks have been kept for now just to ensure there's no possible breakage, but that should be revisited when eliminating the LLD dependency for COFF)

How this fits into the bigger picture

There are effectively two parts to this implementation:

In theory, the second part could be made redundant once the self-hosted COFF linker is ready, since actually writing out the archive file format is not a necessary component of linking--all the required information exists in the .def file.

However, the archive file writing implementation is not (in principle) COFF-specific (although the current implementation is), so in the future:

  • the self-hosted COFF linker could just take the output of .def file parsing as an input and skip the COFF import library generation
  • the import library writing code could be repurposed (where possible) for zig ar: a drop-in llvm-ar replacement #9828

If #17807 is specifically about MinGW .def files, then this closes #17807

@squeek502
Copy link
Member Author

squeek502 commented Oct 1, 2025

Will likely wait until #25430 is merged and then rework the relevant parts of this PR accordingly (at the very least, the std.coff changes will conflict).

Previously, `setAlignment` would set the value to 1 fewer than it should, so if you were intending to set alignment to 8 bytes, it would actually set it to 4 bytes, etc.
Convenience function similar in nature to Symbol.sizeOf
Just makes this a bit nicer to work with since those fields only have 1 intended value.
For the supported COFF machine types of X64 (x86_64), I386 (x86), ARMNT (thumb), and ARM64 (aarch64), this new Zig implementation results in byte-for-byte identical .lib files when compared to the previous LLVM-backed implementation.
@andrewrk andrewrk added the release notes This PR should be mentioned in the release notes. label Oct 6, 2025
Copy link
Member

@andrewrk andrewrk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! One step closer to #16270

Comment on lines -341 to -347
pub const WriteImportLibrary = ZigLLVMWriteImportLibrary;
extern fn ZigLLVMWriteImportLibrary(
def_path: [*:0]const u8,
coff_machine: c_uint,
output_lib_path: [*:0]const u8,
kill_at: bool,
) bool;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🥳

@andrewrk andrewrk merged commit 6893e7f into ziglang:master Oct 6, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes This PR should be mentioned in the release notes.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ability to create import libs from def files without LLVM

2 participants