Any dependency with a build-script segfaults at compile time when running no_std and not linking libc

This calls back to this question Linker error 'undefined reference to memcpy' when not linking libc - #4 by MarcusGrass which gives a bit of background.

I was trying to build a no_std no libc binary and it complained about missing symbols, memcpy specifically in that case. The suggestion was to link compiler-builtins, which probably would have worked. However, I couldn't because the build script immediately segfaulted. I settled for just implementing a #[no_mangle] memcpy and went on my merry way.

Yesterday the problem resurfaced when trying to do this in a bigger project and it turns out the problem seems to be that any dependency will fail to compile if they have a build-script because trying to run any build-script causes a segfault.

Again a minimal(ish) repro can be found here GitHub - MarcusGrass/nostd-alloc-repr if adding the dependency compiler_builtins = "0.1.82" (or any other dependency with a build-script)
I get:

error: failed to run custom build command for `compiler_builtins v0.1.82`

Caused by:
  process didn't exit successfully: `/home/gramar/code/rust/no_std/target/lto/build/compiler_builtins-78c1eb17a6c061f5/build-script-build` (signal: 11, SIGSEGV: invalid memory reference)

The build-scripts are pretty vanilla, I'm guessing that the culprit is in the .cargo/config.toml

[target.'cfg(all())']
rustflags = [
    "-C", "link-arg=-fuse-ld=lld",
    "-C", "link-arg=-nostartfiles", # <----
]

But can I get that to not apply to build-scripts or work around this in some way?

Grateful for any help, I'm a bit out of my element here.

You can pass --target <your_host_target> to cargo build, which should cause RUSTFLAGS to not be passed to build scripts (See this cargo issue).

3 Likes

... That was so easy, huge thanks for the hel!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.