Skip to content

Zig is failing to build for m68k-freestanding-eabi #19510

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
TeamPuzel opened this issue Apr 2, 2024 · 9 comments
Open

Zig is failing to build for m68k-freestanding-eabi #19510

TeamPuzel opened this issue Apr 2, 2024 · 9 comments
Labels
arch-m68k Motorola 68000 series backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Milestone

Comments

@TeamPuzel
Copy link

Zig Version

0.12.0-dev.3498+aff71c613

Steps to Reproduce and Observed Behavior

I built Zig and LLVM with the m68k target enabled, however Zig fails to build or even emit assembly for m68k-freestanding-eabi.

error: error(llvm): LLVM failed to parse 'm68k-unknown-unknown-eabi': Unable to find target for this triple (no targets are registered)
error: InvalidLlvmTriple

That's still confusing as the built clang can definitely emit assembly for the target zig is passing to llvm.

The command I used:

zig build-obj -fno-emit-bin -femit-asm=add.s -target m68k-freestanding-eabi ./src/main.zig

Expected Behavior

LLVM has support for this and the llvm/clang I built with m68k support when building Zig does accept this target, so I would expect Zig to also accept it.

@TeamPuzel TeamPuzel added the bug Observed behavior contradicts documented or intended behavior label Apr 2, 2024
@nektro
Copy link
Contributor

nektro commented Apr 2, 2024

did you build zig with -Dllvm-has-m68k ?

@TeamPuzel
Copy link
Author

Okay, I now tried doing that, this happened:

15:08 zig > /Users/teampuzel/Downloads/zig-macos-aarch64-0.12.0-dev.3518+d2be725e4/zig build -p stage3 --search-prefix "/Users/teampuzel/local/llvm17-release" --search-prefix "$(brew --prefix zstd)" --zig-lib-dir lib -Dstatic-llvm -Dllvm-has-m68k -Doptimize=ReleaseSafe
install
└─ install zig
   └─ zig build-exe zig ReleaseSafe native 5 errors
error: undefined symbol: _LLVMInitializeM68kAsmParser
    note: referenced by /Users/teampuzel/local/llvm17-release/lib/liblldELF.a(Driver.cpp.o):__ZN4llvm23InitializeAllAsmParsersEv
    note: referenced by /Users/teampuzel/OpenSource/zig/zig-cache/o/ad0e2c9095df7909c490603aa6dfaf75/zig.o:_codegen.llvm.initializeLLVMTarget
error: undefined symbol: _LLVMInitializeM68kTarget
    note: referenced by /Users/teampuzel/local/llvm17-release/lib/liblldELF.a(Driver.cpp.o):__ZN4llvm20InitializeAllTargetsEv
    note: referenced by /Users/teampuzel/OpenSource/zig/zig-cache/o/ad0e2c9095df7909c490603aa6dfaf75/zig.o:_codegen.llvm.initializeLLVMTarget
error: undefined symbol: _LLVMInitializeM68kTargetInfo
    note: referenced by /Users/teampuzel/local/llvm17-release/lib/liblldELF.a(Driver.cpp.o):__ZN4llvm24InitializeAllTargetInfosEv
    note: referenced by /Users/teampuzel/OpenSource/zig/zig-cache/o/ad0e2c9095df7909c490603aa6dfaf75/zig.o:_codegen.llvm.initializeLLVMTarget
error: undefined symbol: _LLVMInitializeM68kTargetMC
    note: referenced by /Users/teampuzel/local/llvm17-release/lib/liblldELF.a(Driver.cpp.o):__ZN4llvm22InitializeAllTargetMCsEv
    note: referenced by /Users/teampuzel/OpenSource/zig/zig-cache/o/ad0e2c9095df7909c490603aa6dfaf75/zig.o:_codegen.llvm.initializeLLVMTarget
error: undefined symbol: _LLVMInitializeM68kAsmPrinter
    note: referenced by /Users/teampuzel/local/llvm17-release/lib/liblldELF.a(Driver.cpp.o):__ZN4llvm24InitializeAllAsmPrintersEv
    note: referenced by /Users/teampuzel/OpenSource/zig/zig-cache/o/ad0e2c9095df7909c490603aa6dfaf75/zig.o:_codegen.llvm.initializeLLVMTarget
error: the following command failed with 5 compilation errors:
/Users/teampuzel/Downloads/zig-macos-aarch64-0.12.0-dev.3518+d2be725e4/zig build-exe --stack 33554432 -cflags -std=c++17 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D_GNU_SOURCE -fvisibility-inlines-hidden -fno-exceptions -fno-rtti -Werror=type-limits -Wno-missing-braces -Wno-comment -DNDEBUG=1 -- /Users/teampuzel/OpenSource/zig/src/zig_llvm.cpp /Users/teampuzel/OpenSource/zig/src/zig_clang.cpp /Users/teampuzel/OpenSource/zig/src/zig_llvm-ar.cpp /Users/teampuzel/OpenSource/zig/src/zig_clang_driver.cpp /Users/teampuzel/OpenSource/zig/src/zig_clang_cc1_main.cpp /Users/teampuzel/OpenSource/zig/src/zig_clang_cc1as_main.cpp -lclangFrontendTool -lclangCodeGen -lclangFrontend -lclangDriver -lclangSerialization -lclangSema -lclangStaticAnalyzerFrontend -lclangStaticAnalyzerCheckers -lclangStaticAnalyzerCore -lclangAnalysis -lclangASTMatchers -lclangAST -lclangParse -lclangBasic -lclangEdit -lclangLex -lclangARCMigrate -lclangRewriteFrontend -lclangRewrite -lclangCrossTU -lclangIndex -lclangToolingCore -lclangExtractAPI -lclangSupport -llldMinGW -llldELF -llldCOFF -llldWasm -llldMachO -llldCommon -lLLVMWindowsManifest -lLLVMXRay -lLLVMLibDriver -lLLVMDlltoolDriver -lLLVMCoverage -lLLVMLineEditor -lLLVMXCoreDisassembler -lLLVMXCoreCodeGen -lLLVMXCoreDesc -lLLVMXCoreInfo -lLLVMX86TargetMCA -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86CodeGen -lLLVMX86Desc -lLLVMX86Info -lLLVMWebAssemblyDisassembler -lLLVMWebAssemblyAsmParser -lLLVMWebAssemblyCodeGen -lLLVMWebAssemblyUtils -lLLVMWebAssemblyDesc -lLLVMWebAssemblyInfo -lLLVMVEDisassembler -lLLVMVEAsmParser -lLLVMVECodeGen -lLLVMVEDesc -lLLVMVEInfo -lLLVMSystemZDisassembler -lLLVMSystemZAsmParser -lLLVMSystemZCodeGen -lLLVMSystemZDesc -lLLVMSystemZInfo -lLLVMSparcDisassembler -lLLVMSparcAsmParser -lLLVMSparcCodeGen -lLLVMSparcDesc -lLLVMSparcInfo -lLLVMRISCVTargetMCA -lLLVMRISCVDisassembler -lLLVMRISCVAsmParser -lLLVMRISCVCodeGen -lLLVMRISCVDesc -lLLVMRISCVInfo -lLLVMPowerPCDisassembler -lLLVMPowerPCAsmParser -lLLVMPowerPCCodeGen -lLLVMPowerPCDesc -lLLVMPowerPCInfo -lLLVMNVPTXCodeGen -lLLVMNVPTXDesc -lLLVMNVPTXInfo -lLLVMMSP430Disassembler -lLLVMMSP430AsmParser -lLLVMMSP430CodeGen -lLLVMMSP430Desc -lLLVMMSP430Info -lLLVMMipsDisassembler -lLLVMMipsAsmParser -lLLVMMipsCodeGen -lLLVMMipsDesc -lLLVMMipsInfo -lLLVMLoongArchDisassembler -lLLVMLoongArchAsmParser -lLLVMLoongArchCodeGen -lLLVMLoongArchDesc -lLLVMLoongArchInfo -lLLVMLanaiDisassembler -lLLVMLanaiCodeGen -lLLVMLanaiAsmParser -lLLVMLanaiDesc -lLLVMLanaiInfo -lLLVMHexagonDisassembler -lLLVMHexagonCodeGen -lLLVMHexagonAsmParser -lLLVMHexagonDesc -lLLVMHexagonInfo -lLLVMBPFDisassembler -lLLVMBPFAsmParser -lLLVMBPFCodeGen -lLLVMBPFDesc -lLLVMBPFInfo -lLLVMAVRDisassembler -lLLVMAVRAsmParser -lLLVMAVRCodeGen -lLLVMAVRDesc -lLLVMAVRInfo -lLLVMARMDisassembler -lLLVMARMAsmParser -lLLVMARMCodeGen -lLLVMARMDesc -lLLVMARMUtils -lLLVMARMInfo -lLLVMAMDGPUTargetMCA -lLLVMAMDGPUDisassembler -lLLVMAMDGPUAsmParser -lLLVMAMDGPUCodeGen -lLLVMAMDGPUDesc -lLLVMAMDGPUUtils -lLLVMAMDGPUInfo -lLLVMAArch64Disassembler -lLLVMAArch64AsmParser -lLLVMAArch64CodeGen -lLLVMAArch64Desc -lLLVMAArch64Utils -lLLVMAArch64Info -lLLVMOrcJIT -lLLVMWindowsDriver -lLLVMMCJIT -lLLVMJITLink -lLLVMInterpreter -lLLVMExecutionEngine -lLLVMRuntimeDyld -lLLVMOrcTargetProcess -lLLVMOrcShared -lLLVMDWP -lLLVMDebugInfoLogicalView -lLLVMDebugInfoGSYM -lLLVMOption -lLLVMObjectYAML -lLLVMObjCopy -lLLVMMCA -lLLVMMCDisassembler -lLLVMLTO -lLLVMPasses -lLLVMCFGuard -lLLVMCoroutines -lLLVMipo -lLLVMVectorize -lLLVMLinker -lLLVMInstrumentation -lLLVMFrontendOpenMP -lLLVMFrontendOpenACC -lLLVMFrontendHLSL -lLLVMExtensions -lLLVMDWARFLinkerParallel -lLLVMDWARFLinker -lLLVMGlobalISel -lLLVMMIRParser -lLLVMAsmPrinter -lLLVMSelectionDAG -lLLVMCodeGen -lLLVMTarget -lLLVMObjCARCOpts -lLLVMCodeGenTypes -lLLVMIRPrinter -lLLVMInterfaceStub -lLLVMFileCheck -lLLVMFuzzMutate -lLLVMScalarOpts -lLLVMInstCombine -lLLVMAggressiveInstCombine -lLLVMTransformUtils -lLLVMBitWriter -lLLVMAnalysis -lLLVMProfileData -lLLVMSymbolize -lLLVMDebugInfoBTF -lLLVMDebugInfoPDB -lLLVMDebugInfoMSF -lLLVMDebugInfoDWARF -lLLVMObject -lLLVMTextAPI -lLLVMMCParser -lLLVMIRReader -lLLVMAsmParser -lLLVMMC -lLLVMDebugInfoCodeView -lLLVMBitReader -lLLVMFuzzerCLI -lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader -lLLVMBinaryFormat -lLLVMTargetParser -lLLVMSupport -lLLVMDemangle -lz -lzstd -fno-sanitize-thread -OReleaseSafe --dep aro --dep aro_translate_c --dep build_options -Mroot=/Users/teampuzel/OpenSource/zig/src/main.zig -Maro=/Users/teampuzel/OpenSource/zig/lib/compiler/aro/aro.zig --dep aro -Maro_translate_c=/Users/teampuzel/OpenSource/zig/lib/compiler/aro_translate_c.zig -Mbuild_options=/Users/teampuzel/OpenSource/zig/zig-cache/c/2dc9068ab484fd5a686bcf3cc1a8f8f5/options.zig -lc++ -lc --cache-dir /Users/teampuzel/OpenSource/zig/zig-cache --global-cache-dir /Users/teampuzel/.cache/zig --name zig -L /Users/teampuzel/local/llvm17-release/lib -I /Users/teampuzel/local/llvm17-release/include -L /opt/homebrew/opt/zstd/lib -I /opt/homebrew/opt/zstd/include --zig-lib-dir /Users/teampuzel/OpenSource/zig/lib --listen=- 
Build Summary: 5/8 steps succeeded; 1 failed (disable with --summary none)
install transitive failure
└─ install zig transitive failure
   └─ zig build-exe zig ReleaseSafe native 5 errors
error: the following build command failed with exit code 1:
/Users/teampuzel/OpenSource/zig/zig-cache/o/1694726d7c52352d1f6a630ff604cc48/build /Users/teampuzel/Downloads/zig-macos-aarch64-0.12.0-dev.3518+d2be725e4/zig /Users/teampuzel/OpenSource/zig /Users/teampuzel/OpenSource/zig/zig-cache /Users/teampuzel/.cache/zig --seed 0xd0f7c164 -Zd69050eb6a06d25f -p stage3 --search-prefix /Users/teampuzel/local/llvm17-release --search-prefix /opt/homebrew/opt/zstd --zig-lib-dir lib -Dstatic-llvm -Dllvm-has-m68k -Doptimize=ReleaseSafe

@Vexu
Copy link
Member

Vexu commented Apr 2, 2024

Your LLVM needs to also be built with -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k".

@TeamPuzel
Copy link
Author

Yeah, I did build llvm with the experimental m68k target, like I said the clang I built does recognize the target.

@kassane
Copy link
Contributor

kassane commented May 7, 2024

build.zig bootstrap no add experimental targets libraries.

Similar issue: #5467 (comment)

@kassane
Copy link
Contributor

kassane commented May 7, 2024

My zig-fork with

    "LLVMM68kCodeGen",
    "LLVMM68kDesc",
    "LLVMM68kDisassembler",
    "LLVMM68kAsmParser",
    "LLVMM68kInfo",
$ cat start.c 
void _start(){}

$ zig cc -target m68k-freestanding-none start.c -c -v
clang version 18.0.0git
Target: m68k-unknown-unknown-unknown
Thread model: posix
InstalledDir: /usr/bin
 (in-process)
 "/home/kassane/zig-bootstrap/out/zig-mos-x86_64-linux-musl-baseline/zig" -cc1 -triple m68k-unknown-unknown-unknown -emit-obj -mrelax-all -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name start.c -mrelocation-model static -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -ffreestanding -debug-info-kind=constructor -dwarf-version=4 -debugger-tuning=gdb -gdwarf32 -fdebug-compilation-dir=/home/kassane/zig-bootstrap -v -fcoverage-compilation-dir=/home/kassane/zig-bootstrap -nostdsysteminc -nobuiltininc -resource-dir /home/kassane/zig-bootstrap/out/lib/clang/18 -dependency-file /home/kassane/.cache/zig/tmp/124d4dd3231e3be0-start.o.d -MT /home/kassane/.cache/zig/tmp/124d4dd3231e3be0-start.o -sys-header-deps -MV -isystem out/zig-mos-x86_64-linux-musl-baseline/lib/include -D _DEBUG -O0 -ferror-limit 19 -fsanitize=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound -fsanitize-trap=alignment,array-bounds,bool,builtin,enum,float-cast-overflow,integer-divide-by-zero,nonnull-attribute,null,pointer-overflow,return,returns-nonnull-attribute,shift-base,shift-exponent,signed-integer-overflow,unreachable,vla-bound -fno-sanitize-memory-param-retval -fno-sanitize-address-use-odr-indicator -fgnuc-version=4.2.1 -fcolor-diagnostics -fno-spell-checking -target-cpu generic -target-feature +isa-68000 -target-feature -isa-68010 -target-feature -isa-68020 -target-feature -isa-68030 -target-feature -isa-68040 -target-feature -isa-68060 -target-feature -isa-68881 -target-feature -isa-68882 -target-feature -reserve-a0 -target-feature -reserve-a1 -target-feature -reserve-a2 -target-feature -reserve-a3 -target-feature -reserve-a4 -target-feature -reserve-a5 -target-feature -reserve-a6 -target-feature -reserve-d0 -target-feature -reserve-d1 -target-feature -reserve-d2 -target-feature -reserve-d3 -target-feature -reserve-d4 -target-feature -reserve-d5 -target-feature -reserve-d6 -target-feature -reserve-d7 -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/kassane/.cache/zig/tmp/124d4dd3231e3be0-start.o -x c start.c
clang -cc1 version 18.0.0git based upon LLVM 18.0.0git default target x86_64-linux-musl
#include "..." search starts here:
#include <...> search starts here:
 out/zig-mos-x86_64-linux-musl-baseline/lib/include
End of search list.
LLD Link... ld.lld -r --error-limit=0 --image-base=65536 --eh-frame-hdr -znow -static -o start.o /home/kassane/.cache/zig/o/abd428bca8cfaa8d5f581a275cccb138/start.o --allow-shlib-undefined
Aborted (core dumped)

$ ./gcc-m68k/bin/m68k-elf-objdump -x $HOME/.cache/zig/o/abd428bca8cfaa8d5f581a275cccb138/start.o | head -n 5

/home/kassane/.cache/zig/o/abd428bca8cfaa8d5f581a275cccb138/start.o:     file format elf32-m68k
/home/kassane/.cache/zig/o/abd428bca8cfaa8d5f581a275cccb138/start.o
architecture: m68k, flags 0x00000011:
HAS_RELOC, HAS_SYMS

$ cat start.zig 
export fn _start() callconv(.C) void {
}

$ zig cc -target m68k-freestanding-none start.zig -c -v    
LLVM Emit Object... error(codegen): LLVM failed to parse 'm68k-unknown-unknown-unknown': Unable to find target for this triple (no targets are registered)
thread 114923 panic: Invalid LLVM triple
Unwind error at address `exe:0xa396134` (error.InvalidDebugInfo), trace may be incomplete

Aborted (core dumped)

Reference

@andrewrk andrewrk added this to the unplanned milestone Aug 15, 2024
@alexrp
Copy link
Member

alexrp commented Sep 5, 2024

Note that m68k support in LLVM and Clang is still highly experimental and missing lots of essential features. Realistically, don't expect support for this target to be usable for some years still.

@alexrp alexrp added arch-m68k Motorola 68000 series upstream An issue with a third party project that Zig uses. backend-llvm The LLVM backend outputs an LLVM IR Module. zig cc Zig as a drop-in C compiler feature and removed zig cc Zig as a drop-in C compiler feature labels Oct 3, 2024
@dansalvato
Copy link

dansalvato commented Mar 13, 2025

As of d091178, Zig can successfully build to m68k-freestanding. Tested on macOS.

Here is the procedure I used:

  1. Build LLVM as described here with the extra flag added: -DLLVM_EXPERIMENTAL_TARGETS_TO_BUILD="M68k"
  2. Build Zig via cmake as described here with the extra flag added: -DZIG_EXTRA_BUILD_ARGS="-Dllvm-has-m68k"

We now have a stage3 zig binary with m68k support. This build of Zig can also build itself:

$ build/stage3/bin/zig build --search-prefix "$(realpath ~/local/llvm19-assert)" --zig-lib-dir "$(realpath build/stage3/lib/zig)" -Denable-llvm -Dllvm-has-m68k

Testing

I'm using this test.zig file for the tests below:

pub export fn main() u8 {
    return 0;
}

Building an ELF object doesn't currently seem possible, because LLD doesn't support m68k.

$ zig build-obj test.zig -target m68k-freestanding
error: ld.lld: unsupported e_machine value: 4

Using -fno-lld yields similar results, this time with a Zig error:

$ zig build-obj test.zig -target m68k-freestanding -fno-lld
thread 11960094 panic: invalid enum value
zig/src/link/Elf/Object.zig:113:13: 0x10e5e4073 in parseCommon (zig)
            @tagName(self.header.?.e_machine),
            ^
zig/src/link/Elf.zig:1162:27: 0x10e5ed103 in parseObject (zig)
    try object.parseCommon(gpa, diags, obj.path, handle, target);
                          ^
zig/src/link/Elf.zig:1130:21: 0x10e1dafc7 in parseObjectReportingFailure (zig)
    self.parseObject(obj) catch |err| switch (err) {

...

On the other hand, building to ASM works and provides results similar to clang.

$ zig build-obj test.zig -target m68k-freestanding -fno-emit-bin -femit-asm="test.s" -fstrip -O ReleaseFast -fno-unwind-tables -fomit-frame-pointer

Output:

	.text
	.file	"test"
	.globl	main
	.p2align	1
	.type	main,@function
main:
	moveq	#0, %d0
	rts
.Lfunc_end0:
	.size	main, .Lfunc_end0-main

	.section	".note.GNU-stack","",@progbits

Outputting to C and building with clang also works:

$ zig build-obj test.zig -target m68k-freestanding -fstrip -O ReleaseFast -ofmt=c
$ ~/local/llvm19-assert/bin/clang -S --target=m68k -Ibuild/stage3/lib/zig -o test-clang.s test.c

@alexrp
Copy link
Member

alexrp commented Apr 26, 2025

After #23663 is merged, you'll be able to zig build-obj directly to an object file, bypassing the linker, making -fno-emit-bin -femit-asm or -ofmt=c unnecessary for basic experimentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-m68k Motorola 68000 series backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

7 participants