Relocations in generic ELF (EM: 40)

Bradley Noyes published on
2 min, 292 words

Today I added caching to my gitlab CI since i keep on running low on minutes. This was annoyly challening for a few reasons.

  • Gitlab only caches data in the build directory, so if you want to cache $HOME/.cargo you can't. So you must redefine $CARGO_HOME.
  • There's a bug in cargo where it uses the wrong linker that seemd to pop-up when redefining $CARGO_HOME and cross compiling.

The error I was recieving was the following for each compiled object.

Relocations in generic ELF (EM: 40)
Relocations in generic ELF (EM: 40)
Relocations in generic ELF (EM: 40)
Relocations in generic ELF (EM: 40)
Relocations in generic ELF (EM: 40)
...
error adding symbols: File in wrong format

My project builds both x86 and armv7 binaries. My native builds always succeeded, but the cross compile always failed. This lead me to believe (incorrectly) that this was a problem with using x86 cache to build an arm binary. Spoiler alert -- it wasn't an archetecture problem as cargo already does a good job keep those file seperate.

The problem is related to the cargo bugs #4133 and rust bug #28924. To solve this problem either set the environment variable export CARGO_TARGET_ARMV7_UNKNOWN_LINUX_GNUEABIHF_LINKER=/usr/bin/arm-linux-gnueabihf-gcc or insert

[target.armv7-unknown-linux-gnueabihf]
linker = "arm-linux-gnueabihf-gcc"

into ~/.cargo/config or $CARGO_HOME/config

(note: your target-triple may vary)

Add to .gitlab-ci.yml🔗

Lastly, after I commited the new cargo-config file, i just had to add the following to my gitlab-ci.yml file to enable caching.

cache:
  paths:
    - target/
    - cargo/

variables:
  CARGO_HOME: $CI_PROJECT_DIR/cargo

And all was well.