7

I'm writing a Rust program targeted for an STM32F407 processor using zinc. I'd like to be able to produce a linker map file. I've found that I can put the following in my main.rs and this gives me the desired result:

#![feature(link_args)]
#[link_args = "-Wl,-Map=blink_stm32f4.map"]
extern {}

However, the documentation for link_args suggests not to use this method.

What other methods exist to get the linker to produce a map file?

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Dave Hylands
  • 811
  • 10
  • 9

2 Answers2

7

link-args is possible to pass to rustc via rustc -C link-args="-Wl,-Map=blink_stm32f4.map" test.rs.

And there is option of cargo rustflags in build section. See cargo config. It works like this:

$ cargo new --bin testbin
$ cd testbin
$ cat .cargo/config 
[build]
rustflags = ["-Clink-args=-Wl,-Map=/tmp/blink_f7.map"]
$ cargo build

Also there is linker option in cargo config. I don't try to pass via this option gcc plus flags, only gcc, but you can write gcc wrapper script like:

$ cat my-linker.sh
#!/bin/sh

arm-...-gcc -Wl,-Map=blink_stm32f4.map $@
fghj
  • 8,898
  • 4
  • 28
  • 56
  • 1
    I tried to add: [build] rustflags = [ "-Wl,-Map=blink_f7.map" ] to my Cargo.toml file and I get the following warning warning: unused manifest key: build.rustflags I'm using nightly-2016-05-24 to build. It seems that many of the tips I've found by google searching only work on older versions of rust/cargo. – Dave Hylands Sep 04 '16 at 17:33
  • 1
    I got the my-linker.sh script to work - thanks. That should work under linux and OSX. I'm building something much bigger than a single file, so I'm not sure how calling rustc directly will help. I need to include zinc and its dependencies at link time. 1 of 3 is still progress though. – Dave Hylands Sep 04 '16 at 17:50
  • 1
    @DaveHylands I updated my answer with description how to use `cargo` config, by the way linker also you can set in `.cargo/config` and you can set it to different value for different architictures. – fghj Sep 04 '16 at 18:29
  • 1
    Sweet. I got your example to work. Using rustflags = ["-C", "linkargs=...] also works. using rustflags = [ "-C linkargs=...] (i.e. space between -C and linkargs) does not. I think I've been confusing Cargo.toml and .cargo/config which is why some of my attempts didn't work. – Dave Hylands Sep 04 '16 at 19:20
0

I tried to add something like this in .cargo/config:

[build]
rustflags = ["-Clink-args=-Wl,-Map=/tmp/app.map"]

But I got error:

rust-lld: error: unknown argument '-Wl,-Map=/tmp/app.map'

According to this blog, it worked after I changed it to:

[build]
rustflags = ["-Clink-args=-Map=/tmp/app.map"]
fengqi
  • 367
  • 2
  • 10
  • 1
    Arguments like `-Wl,-Map` are for GCC or compatible C compilers, which are sometimes used as linkers. The arguments after each `-Wl` argument are passed to the linker by the C compiler. If `rustc` is using a linker directly, which is the default on most targets, you don't need to `-Wl` since you're not talking to a C compiler intermediary. – Mastax Jun 11 '21 at 15:11
  • Thanks Mastax for the information. I'm aware that `-Wl,option` is a gcc comand line option to pass options to the linker. I'm not sure in which situation will `rustc` choose C compiler or `rust-lld` as the linker. Maybe the default is changed after a certain version. Or just because I'm using `#![no_std]`. – fengqi Jun 15 '21 at 07:41