# Linker options in Cargo.toml

**URL:** <https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406>\
**Category:** Uncategorized\
**Created:** [November 16, 2020, 7:52pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406 "2020-11-16T19:52:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [November 16, 2020, 7:52pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/1 "2020-11-16T19:52:54Z")

</div>

I deal with custom linker scripts a lot (more than I'd like, really...). Of the "weird embedded things", it simultaneously feels like one of the most common and most painful ones I have to deal with. Having to fuss around with RUSTFLAGS, especially when you're in a workspace with lots of different linker scripts, is a great way to kill code caching (though `cargo rustc` can sometimes be used to work around this). This also often means you need to wrap Cargo in another build system to set up the correct flags.

What I want is something morally equivalent to this:

```rust
[[bin]]
name = "my_kernel"
linker_script = "link.ld"

```

which would cause Cargo to pass `-Clinker-args -T,link.ld` to rustc. Of course, this assumes a GNU-flavored linker, and I honestly have no background in linkers on other platforms (I am clueless about how MSVC handles this kind of thing); I would sort of assume this is a "best effort based on the linker flavor" thing. I could imagine having separate keys for each flavor, or something like `linker_script = { gnu = "link.ld" }` or similar.

(I think it would be amazing if a `link.ld` was detected much like `main.rs` and `build.rs` are, but that feels much more like a "nice-to-have".)

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [November 16, 2020, 10:01pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/2 "2020-11-16T22:01:01Z")

</div>

> [@mcy](#):
>
> What I want is something morally equivalent to this:
> 
> ```rust
> [[bin]]
> name = "my_kernel"
> linker_script = "link.ld"
> 
> ```
> 
> which would cause Cargo to pass `-Clinker-args -T,link.ld` to rustc.

That sounds great to me.

For future compatibility, I'd suggest `ld-script`, and then other linkers can use `somethingelse-script`.

Otherwise, that seems entirely reasonable, and I'd happily support a Cargo patch to that effect. I've added it to the agenda for this week's Cargo team meeting.

> [@mcy](#):
>
> Of course, this assumes a GNU-flavored linker, and I honestly have no background in linkers on other platforms (I am clueless about how MSVC handles this kind of thing);

To the best of my knowledge, the MSVC linker handles some very limited cases like base address and entry point, and there's a separate mechanism for definition files to handle symbols; beyond that, from what I've seen, people who need that level of control either write standalone post-processing scripts for object files or they use an ld-compatible linker script with clang or gcc.

> [@mcy](#):
>
> (I think it would be amazing if a `link.ld` was detected much like `main.rs` and `build.rs` are, but that feels much more like a "nice-to-have".)

I'd love to see that as well. I'd suggest including that from the start, to set a convention for what the script should be called.

In workspaces, `link.ld` should live within each crate like `build.rs` does. (If people want to share a linker script across crates in a workspace, which might make sense for some kinds of loadable modules or plugins, this could use the workspace deduplication mechanism.)

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [November 16, 2020, 10:28pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/3 "2020-11-16T22:28:54Z")

</div>

I think it would be good to have a more general `link.rs` script that can print out arguments which are in some way sent to the linker. I'm not against directly supporting the linker specific scripts either but, speaking from the Windows side of things, a more general approach would be useful because it's very common for people to want to change Rust's defaults. Currently doing so is very awkward.

---

<div class="post-metadata">

**Author:** ![ratmice](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ratmice/32/4917_2.png) [@ratmice](https://internals.rust-lang.org/u/ratmice)\
**Post date:** [November 16, 2020, 11:43pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/4 "2020-11-16T23:43:19Z")

</div>

I find it a odd in that it seems like a big departure as very little precedent for embedding paths to files, in Cargo.toml, the closest thing I know of is the `path="dir"`, e.g. in a `[[bin]]`, you specify a name `foo` rather than a `foo.rs`.

I guess the first thing that comes to mind is to drop the `.ld` so that various linkers can look up the path to the script with a different extension?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 17, 2020, 12:03am UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/5 "2020-11-17T00:03:26Z")

</div>

There is also a `build-script="build.rs"` key (forget the exact key right now) which takes a path to a Rust file.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [November 17, 2020, 12:30am UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/6 "2020-11-17T00:30:33Z")

</div>

> [@chrisd](#):
>
> I think it would be good to have a more general `link.rs` script that can print out arguments which are in some way sent to the linker.

That doesn't make it unnecessary to have a linker script, and it would make it more complex to handle the common case.

We certainly should allow `build.rs` scripts to supply arguments to the linker, and specifically to supply a linker script filename (which would allow generating the linker script). But I still think we should enable the common case by having a declarative `Cargo.toml` option, and a default filename.

> [@ratmice](#):
>
> I guess the first thing that comes to mind is to drop the `.ld` so that various linkers can look up the path to the script with a different extension?

I don't think we should assume that some other platform's linker-script mechanism works similarly enough that it suffices to use a file with the same name and a different extension. I would rather see `ld-script = "link.ld"` and `otherlink-script = "link.ols"`, rather than making assumptions about naming.

> [@CAD97](#):
>
> There is also a `build-script="build.rs"` key (forget the exact key right now) which takes a path to a Rust file.

[https://doc.rust-lang.org/cargo/reference/manifest.html#the-build-field](https://doc.rust-lang.org/cargo/reference/manifest.html#the-build-field)`build = "build.rs"`, yes. That'd be the model to work from: a default name and location, the ability to override, and a path relative to the manifest.

---

<div class="post-metadata">

**Author:** ![chrisd](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/chrisd/32/7232_2.png) [@chrisd](https://internals.rust-lang.org/u/chrisd)\
**Post date:** [November 17, 2020, 12:56am UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/7 "2020-11-17T00:56:08Z")

</div>

> [@josh](#):
>
> That doesn't make it unnecessary to have a linker script, and it would make it more complex to handle the common case.
> 
> We certainly should allow `build.rs` scripts to supply arguments to the linker, and specifically to supply a linker script filename (which would allow generating the linker script). But I still think we should enable the common case by having a declarative `Cargo.toml` option, and a default filename.

Oh definitely. But I mean if it's specifically for `ld` scripts then that's inherently saying the common case is GNU compatible linkers. For sure, that's probably totally true but I think that it should be made clear that's what's being said.

My bias here is that I'd like a more ergonomic solution for Windows than having to specify linker arguments on the command line when I want to target e.g. a particular binary. Using Cargo is honestly a bit of a pain point here. I end up either having to wrap `cargo rustc` or call `rustc` directly.

I guess what I'm saying is it would be great if there was a generic "linker script" file that rust tooling understood. But I'm not at all suggesting this should be a blocker on supporting GNU linker scripts.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [November 17, 2020, 7:59am UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/8 "2020-11-17T07:59:55Z")

</div>

I would like to see a better way to handle linker arguments as well. I don't think that should be a separate script, though. I think that should be in build.rs.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [November 17, 2020, 4:05pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/9 "2020-11-17T16:05:26Z")

</div>

@josh Do you think composing a Cargo RFC for this is worthwhile?

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [November 17, 2020, 11:21pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/10 "2020-11-17T23:21:53Z")

</div>

Let's see how the discussion goes at tomorrow's Cargo meeting. It may need an RFC, or it may just need a PR.

---

<div class="post-metadata">

**Author:** ![josh](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/josh/32/5934_2.png) [@josh](https://internals.rust-lang.org/u/josh)\
**Post date:** [November 18, 2020, 9:53pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/11 "2020-11-18T21:53:20Z")

</div>

We discussed this in the Cargo meeting this week.

Our consensus was that we definitely want to support linker scripts, and also support other linker options.

We already have support in progress for doing this via build scripts: [https://github.com/rust-lang/cargo/pull/8441](https://github.com/rust-lang/cargo/pull/8441) . That got stalled slightly, but we're reviving it, and @alexcrichton plans to r+ that PR this week. Once that goes in, we'll have support in nightly for emitting linker arguments for anything a crate can build. That would include linker scripts (whether universal or target-specific), as well as options like `--nmagic` or `-s`.

We felt that that support should fully address this issue, without additionally supporting a subset of cases (those that don't need target-specific scripts or generated scripts) via static information in `Cargo.toml`.

We'd appreciate help testing [https://github.com/rust-lang/cargo/pull/8441](https://github.com/rust-lang/cargo/pull/8441) , confirming that it addresses the issue here, providing experience reports, and helping us bring it to stable Rust and stable Cargo.

---

<div class="post-metadata">

**Author:** ![mcy](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mcy/32/6512_2.png) [@mcy](https://internals.rust-lang.org/u/mcy)\
**Post date:** [November 19, 2020, 4:22pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/12 "2020-11-19T16:22:01Z")

</div>

That should help! Unfortunately I don't have any projects that _directly_ benefit, due to me needing to pass a custom --target JSON file as well... but I'll try it out when I get a chance.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [February 17, 2021, 4:22pm UTC](https://internals.rust-lang.org/t/linker-options-in-cargo-toml/13406/13 "2021-02-17T16:22:03Z")

</div>

Closed on February 17, 2021, 4:22pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
