1#!/bin/sh
2# zig cc for $ZIG_TARGET, as `cargo.sh` sets it: the C compiler and linker. zig rejects
3# rustc's --target, --no-undefined-version and -O1, and strips the symbol table with the debug
4# info, so the debug info goes after linking.
5strip= fix= output= previous=
6for arg do
7 shift
8 [ "$previous" = -o ] && output=$arg
9 previous=$arg
10 case $arg in
11 --target=* | -Wl,--no-undefined-version | -Wl,-O1) ;;
12 -Wl,--strip-debug) strip=1 ;;
13 -Wl,--fix-cortex-a53-843419) fix=1 ;;
14 *) set -- "$@" "$arg" ;;
15 esac
16done
17if [ -n "$fix" ]; then
18 work=$(mktemp -d "${TMPDIR:-/tmp}/linux-link.XXXXXX") || exit
19 trap 'rm -rf "$work"' EXIT
20 work=$(cd "$work" && pwd) || exit
21 case $output in /*) ;; *) output=$PWD/$output ;; esac
22 # Zig rejects the erratum flag; LLD's response file preserves its resolved libc inputs.
23 LLD_REPRODUCE="$work/link.tar" \
24 zig cc -target "$ZIG_TARGET" "$@" -o "$work/unpatched" || exit
25 tar -xOf "$work/link.tar" link/response.txt > "$work/response.txt" || exit
26 (
27 unset LLD_REPRODUCE
28 cd / || exit
29 zig ld.lld "@$work/response.txt" --chroot / --fix-cortex-a53-843419 -o "$work/patched"
30 ) || exit
31 mv "$work/patched" "$output" || exit
32else
33 zig cc -target "$ZIG_TARGET" "$@" || exit
34fi
35[ -z "$strip" ] ||
36 "$(rustc --print sysroot)/lib/rustlib/$(rustc -vV | sed -n 's/^host: //p')/bin/rust-objcopy" \
37 --strip-debug "$output"