bazelbuild / bazelbuild/rules_rust

`alwayslink`ing a rust_library in a cc_binary

Open
#3,722 2 comments 0 reactions 1 assignee Claimed by @krasimirgg View on GitHub
Dominant language
Starlark
Stars
843
Forks
651
Avg merge
2d 18h
Merged PRs (30d)
15

Description

# Issue
When building a rust_library that contains unreferenced symbols that is a link dependency of a `cc_binary`

e.g. lowlevel CPU startup code that get's located into a defined address via the section.

To ensure that the symbols get linked the `alwayslink` attribut of the library gets set to `True`

Code Example

```Starlark
load("@rules_rust//rust:defs.bzl", "rust_library")

rust_library(
name = "startup_code",
srcs = glob(["rust/src/**/*.rs"]),
deps = ["@crates//:vcell"],
alwayslink = True,
)

cc_binary(
name = "main",
deps = [
":startup_code",
],
)
```

```
# rust/src/startup.rs
#[no_mangle]
#[link_section = ".cpu_entrypoint"]
pub fn _start() -> ! {
// Startup code
}

#[link_section = ".cpu_entrypoint"]
#[no_mangle]
pub static __ENTRY_POINT: fn() -> ! = _start;
```

# The error

```
ld.lld failed: error executing CppLink command (from target //:main)
ld.lld: error: bazel-out/platform-dbg/bin/libstartup_code-2667693308.a(lib.rmeta): not an ELF file
```
I assume that the archiver puts the rmeta file into the archive as well.

There are also warnings `.a: archive member 'lib.rmeta' is neither ET_REL nor LLVM bitcode` that hint at this

However with alwayslink we enforce to include the complete archive and the linker failes to include the rmeta file.

I tried my way through the code base. I saw the tests for `alwayslink` but only in combination with rust_binary.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.