ziglang / zig

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

Allow COFF output for freestanding target #3182

Open jayschwa opened 4 years ago

jayschwa commented 4 years ago

I am experimenting with linking Zig libraries into DOS executables using the Open Watcom toolchain. The Watcom linker seems to require COFF because it does not understand ELF. When building the Zig library, I cannot figure out how to target freestanding and get COFF output. For now, I am using i386-uefi-none as my target.

andrewrk commented 4 years ago

I think we can add this as a DOS OS. That should be a pretty straightforward modification. Then you could use -target i386-dos as your target.

Tetralux commented 4 years ago

Another option here is something like:

// main.zig

pub const debug_info_type = .COFF; // or .ELF, .Auto

pub fn main() void {}
ikskuh commented 4 years ago

If it's possible, add support for allowing arbitrary overrides of the output format for the user. The linker could output PE, COFF, ECOFF, ELF, flat binaries, ihex, ....

This would allow something like x86_64-windows-elf or aarch64-linux-coff.

This is especially interesting for freestanding targets if you write plugins/modules for custom/embedded systems.

daurnimator commented 4 years ago

Same as #2826 (but add COFF?)

jayschwa commented 3 years ago

My original use-case for this issue has been obsoleted. I am now using a linker script to produce a MZ executable that subsequently loads and runs an ELF executable. That said, making output format configurable is probably still desirable.

Ronsor commented 3 years ago

After some investigation, I've found LLVM will never output COFF objects unless the target triple is *-windows-msvc.

This seems to leave three options: (1) change LLVM's behavior, (2) add a separate freestanding-coff OS target that tells LLVM to target *-windows-msvc (in a similar way to the UEFI target), or (3) add a special case for *-freestanding-msvc that does the same as (2).