Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tatsuya6502/cb584d073dbe792e2a1aa31ee49d3a25 to your computer and use it in GitHub Desktop.
Save tatsuya6502/cb584d073dbe792e2a1aa31ee49d3a25 to your computer and use it in GitHub Desktop.
Rust cross compile - Linux x86_64 -> Linux Sparc64 (gcc 5.4.0)
/home/tatsuya% docker run -it ubuntu:trusty
root@1ce8acf4991d:/# cd
root@1ce8acf4991d:~# uname -a
Linux 1ce8acf4991d 4.11.9-1-ARCH #1 SMP PREEMPT Wed Jul 5 18:23:08 CEST 2017 x86_64 x86_64 x86_64 GNU/Linux
root@1ce8acf4991d:~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.5 LTS"
root@1ce8acf4991d:~# apt-get update
...
root@1ce8acf4991d:~# apt-get install -y gcc-4.6 curl file make
root@1ce8acf4991d:~# mkdir -p $HOME/cross/sparc64
root@1ce8acf4991d:~# for v in 2.17 2.19.1 2.21.1; do \
curl -L -O http://ftpmirror.gnu.org/binutils/binutils-${v}.tar.bz2 && \
tar xf binutils-${v}.tar.bz2 && \
rm -rf build-binutils; mkdir build-binutils \
(cd build-binutils; \
CC=gcc-4.6 ../binutils-${v}/configure \
--prefix=$HOME/cross/sparc64/binutils-${v} \
--target=sparc64-unknown-linux-gnu \
--disable-werror \
--disable-multilib && \
make -j 4 && \
make install)
done
root@1ce8acf4991d:~# ls -l $HOME/cross/sparc64/
total 12
drwxr-xr-x 8 root root 4096 Jul 14 00:34 binutils-2.17
drwxr-xr-x 8 root root 4096 Jul 14 00:35 binutils-2.19.1
drwxr-xr-x 6 root root 4096 Jul 14 00:36 binutils-2.21.1
root@1ce8acf4991d:~# curl https://sh.rustup.rs -sSf | sh -s -- -y
...
stable installed - rustc 1.18.0 (03fc9d622 2017-06-06)
root@1ce8acf4991d:~# source $HOME/.cargo/env
root@1ce8acf4991d:~# rustup target add sparc64-unknown-linux-gnu
...
root@1ce8acf4991d:~# export USER=root
root@1ce8acf4991d:~# cargo new --bin hello
Created binary (application) `hello` project
root@1ce8acf4991d:~# cd hello
root@1ce8acf4991d:~/hello# rustc --emit obj --target=sparc64-unknown-linux-gnu src/main.rs
root@1ce8acf4991d:~/hello# file main.o
main.o: ELF 64-bit MSB relocatable, SPARC V9, total store ordering, version 1 (SYSV), not stripped
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.17/bin/sparc64-unknown-linux-gnu-ld --version
GNU ld version 2.17
Copyright 2005 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License. This program has absolutely no warranty.
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.17/bin/sparc64-unknown-linux-gnu-ld main.o
main.o: could not read symbols: Bad value
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.19.1/bin/sparc64-unknown-linux-gnu-ld --version
GNU ld (GNU Binutils) 2.19.1
Copyright 2007 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.19.1/bin/sparc64-unknown-linux-gnu-ld main.o
main.o: could not read symbols: Bad value
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.21.1/bin/sparc64-unknown-linux-gnu-ld --version
GNU ld (GNU Binutils) 2.21.1
Copyright 2011 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.21.1/bin/sparc64-unknown-linux-gnu-ld main.o
/root/cross/sparc64/binutils-2.21.1/bin/sparc64-unknown-linux-gnu-ld: warning: cannot find entry symbol _start; defaulting to 00000000001000e8
main.o: In function `main::main::h94cdd750d4114894':
main.cgu-0.rs:(.text._ZN4main4main17h94cdd750d4114894E+0x48): undefined reference to `std::io::stdio::_print::hfa7950538481df0b'
main.o: In function `main':
main.cgu-0.rs:(.text.main+0x34): undefined reference to `std::rt::lang_start::ha5350a26f8f175ab'
root@1ce8acf4991d:~/hello# rustc -O --emit obj --target=sparc64-unknown-linux-gnu src/main.rs
root@1ce8acf4991d:~/hello# $HOME/cross/sparc64/binutils-2.17/bin/sparc64-unknown-linux-gnu-ld main.o
/root/cross/sparc64/binutils-2.17/bin/sparc64-unknown-linux-gnu-ld: warning: cannot find entry symbol _start; defaulting to 00000000001000e8
main.o: In function `main::main::h94cdd750d4114894':
main.cgu-0.rs:(.text._ZN4main4main17h94cdd750d4114894E+0x44): undefined reference to `std::io::stdio::_print::hfa7950538481df0b'
main.o: In function `main':
main.cgu-0.rs:(.text.main+0x24): undefined reference to `std::rt::lang_start::ha5350a26f8f175ab'
$ docker run -it ubuntu:xenial
Unable to find image 'ubuntu:xenial' locally
xenial: Pulling from library/ubuntu
...
Digest: sha256:a0ee7647e24c8494f1cf6b94f1a3cd127f423268293c25d924fbe18fd82db5a4
Status: Downloaded newer image for ubuntu:xenial
root@3479ac349e68:/# cd
root@3479ac349e68:~# uname -a
Linux 3479ac349e68 4.7.3-coreos-r2 #1 SMP Thu Feb 2 02:26:10 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
root@3479ac349e68:~# cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.2 LTS"
root@3479ac349e68:~# apt update
...
10 packages can be upgraded. Run 'apt list --upgradable' to see them.
root@3479ac349e68:~# apt install -y gcc-5-sparc64-linux-gnu
...
root@3479ac349e68:~# apt install -y curl file
...
root@3479ac349e68:~# sparc64-linux-gnu-gcc-5 --version
sparc64-linux-gnu-gcc-5 (Ubuntu 5.4.0-6ubuntu1~16.04.1) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
root@3479ac349e68:~# sparc64-linux-gnu-ld --version
GNU ld (GNU Binutils for Ubuntu) 2.26.1
Copyright (C) 2015 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.
root@3479ac349e68:~# curl https://sh.rustup.rs -sSf | sh
...
stable installed - rustc 1.18.0 (03fc9d622 2017-06-06)
Rust is installed now. Great!
...
root@3479ac349e68:~# source $HOME/.cargo/env
root@3479ac349e68:~# rustup target add sparc64-unknown-linux-gnu
root@3479ac349e68:~# echo $USER
root@3479ac349e68:~# export USER=root
root@3479ac349e68:~# cargo new --bin hello
Created binary (application) `hello` project
root@3479ac349e68:~# cd hello
root@3479ac349e68:~/hello# RUSTFLAGS="-C linker=sparc64-linux-gnu-gcc-5" cargo build --target=sparc64-unknown-linux-gnu
Compiling hello v0.1.0 (file:///root/hello)
Finished dev [unoptimized + debuginfo] target(s) in 0.65 secs
root@3479ac349e68:~/hello# file target/sparc64-unknown-linux-gnu/debug/hello
target/sparc64-unknown-linux-gnu/debug/hello: ELF 64-bit MSB shared object, SPARC V9, total store ordering, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux.so.2, for GNU/Linux 3.2.0, BuildID[sha1]=1c94adfdaf50b6a6d0b6ee5c09443a1c8317feb2, not stripped
root@3479ac349e68:~/hello# rustc --emit obj --target=sparc64-unknown-linux-gnu -C linker=sparc64-linux-gnu-gcc-5 src/main.rs
root@3479ac349e68:~/hello# file main.o
main.o: ELF 64-bit MSB relocatable, SPARC V9, total store ordering, version 1 (SYSV), not stripped
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment