Linker Script INCLUDE Errors

Hello!
I’ve recently been trying to compile the raspberry pi pico sdk using zig instead of cmake, but I’ve run into an error where they use a linker script that includes another linker script via the normal “INCLUDE” syntax.
Specifically the script is memmap_default.ld, and I’m trying to auto-generate the “pico_flash_region.ld” script.
This is a little snippet of my build.zig:

const ldscript = try this.ldscript_gen_step(b);
firmobj.step.dependOn(&ldscript.step);
firmobj.root_module.addLibraryPath(ldscript.getDirectory());
firmobj.setVerboseLink(true);
// And set it
firmobj.setLinkerScript(try this.repo_root.join(b.allocator, try std.fmt.allocPrint(b.allocator, "src/rp2_common/pico_crt0/{s}/memmap_default.ld", .{@tagName(platform_by_board(this.board))})));

Where this.ldscript_gen_step(b) just returns a Step.WriteFile.

The output of the verbose link is:

ld.lld -r --error-limit=0 -mllvm -target-abi=aapcs -mllvm -float-abi=soft --build-id=none --image-base=65536 -T /Users/phitch/Purdue/microprocessor362/ta/pico-sdk/src/rp2_common/pico_crt0/rp2350/memmap_default.ld --eh-frame-hdr -znow -m armelf -Bstatic -o [output + all the input ofiles...]

So I think the Library is just not getting passed to the linker, however the “zig build-obj” command that gets printed includes the “-L .zig-cache/o/[big-hash]” flag, and I’ve verified that directory contains the correct “pico_flash_region.ld” file.

Any guidance would be appreciated! I saw some stuff in std’s Compile.zig around line 1700 about ommitting flags but that’s the only lead I have rn and it doesn’t look like it pertains to the linker, just zig build-obj.

I’m on macos running 0.15.1 (tried 0.15.2 too) if that helps.

You need to check the complete ld.lld command and see if it actually includes any -L options. I could be wrong but from quickly skimming the compiler code base I believe all library link inputs (-L, -l, etc.) are processed by Zig itself in advance before the linker is invoked, so even though -L is present in the original zig build-obj command it’s not propagated to the internal zig ld.lld invocation.

If so, this is a flaw in the Zig compiler and you should probably open an issue.

As a workaround, since the contents of pico_flash_region.template.ld is literally just the line FLASH(rx) : ORIGIN = 0x10000000, LENGTH = ${PICO_FLASH_SIZE_BYTES_STRING}, maybe you could preprocess memmap_default.ld in advance from your build.zig?

Just double checked ld.lld invocation and the part I omitted in the post really is just a bunch of .o files so I’ll open an issue.

And yeah since it is so small that’s probably what I’ll do.

Thank you!

Edit 10/28:
Issue created: GitHub · Where software is built