Files
DroidDeck/tools/nativeguest/build-gosys.sh
T

83 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Builds the code the native userland adds to static Go programs (gosys/gosys.c) into the C header
# the hook includes (gosys/gosys_blob.h, not kept in the repository; build.sh makes it each time).
#
# The code is AArch64 whatever the machine this runs on: it is compiled freestanding with an
# aarch64 cross compiler (gcc-aarch64-linux-gnu: the one that builds the hook; or clang with lld).
#
# usage: tools/nativeguest/build-gosys.sh [output header]
set -euo pipefail
here=$(cd "$(dirname "$0")" && pwd)
src=$here/gosys
out=${1:-$src/gosys_blob.h}
if command -v aarch64-linux-gnu-gcc >/dev/null 2>&1; then
CC=(aarch64-linux-gnu-gcc)
LD=(aarch64-linux-gnu-ld)
OBJCOPY=(aarch64-linux-gnu-objcopy)
NM=(aarch64-linux-gnu-nm)
OBJDUMP=(aarch64-linux-gnu-objdump)
elif command -v clang >/dev/null 2>&1 && command -v ld.lld >/dev/null 2>&1; then
CC=(clang --target=aarch64-linux-gnu)
LD=(ld.lld)
OBJCOPY=(llvm-objcopy)
NM=(llvm-nm)
OBJDUMP=(llvm-objdump)
else
echo "build-gosys.sh: needs aarch64-linux-gnu-gcc (apt install gcc-aarch64-linux-gnu) or clang with lld" >&2
exit 1
fi
tmp=$(mktemp -d)
trap 'rm -rf "$tmp"' EXIT
"${CC[@]}" -std=gnu11 -O2 -Wall -Wextra -Werror \
-ffreestanding -fno-builtin -nostdlib -fPIE -fvisibility=hidden -fno-stack-protector \
-fno-asynchronous-unwind-tables -fno-unwind-tables -fno-tree-loop-distribute-patterns \
-mcmodel=tiny -mgeneral-regs-only -mno-outline-atomics -ffixed-x18 -ffixed-x28 \
-c "$src/gosys.c" -o "$tmp/gosys.o"
"${LD[@]}" -T "$src/gosys.ld" -nostdlib -o "$tmp/gosys.elf" "$tmp/gosys.o"
"${OBJCOPY[@]}" -O binary "$tmp/gosys.elf" "$tmp/gosys.bin"
# It must stand on its own: nothing undefined, no writable data, and nothing that depends on the
# address it is loaded at (adrp assumes the page offset of the link address; adr does not)
if [ -n "$("${NM[@]}" -u "$tmp/gosys.elf")" ]; then
echo "build-gosys.sh: gosys.c needs symbols it does not define:" >&2
"${NM[@]}" -u "$tmp/gosys.elf" >&2
exit 1
fi
if "${OBJDUMP[@]}" -d "$tmp/gosys.elf" | grep -q -w adrp; then
echo "build-gosys.sh: gosys.c uses adrp, which is not position-independent at any address" >&2
exit 1
fi
sections=$("${OBJDUMP[@]}" -h "$tmp/gosys.elf" | awk '$1 ~ /^[0-9]+$/ {print $2}' | tr '\n' ' ')
for s in $sections; do
case $s in
.hdr|.thunks|.text|.rodata|.trailer|.symtab|.strtab|.shstrtab) ;;
*) echo "build-gosys.sh: unexpected section $s in gosys.elf (sections: $sections)" >&2; exit 1 ;;
esac
done
size=$(stat -c %s "$tmp/gosys.bin")
entry=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "cxgo_entry" {print $1}')
thunks=$("${NM[@]}" "$tmp/gosys.elf" | awk '$3 == "g_thunks" {print $1}')
if [ -z "$entry" ] || [ -z "$thunks" ]; then
echo "build-gosys.sh: cxgo_entry or g_thunks not found" >&2
exit 1
fi
[ "$(head -c 8 "$tmp/gosys.bin")" = "CXGOSYS1" ] || { echo "build-gosys.sh: the header is not first" >&2; exit 1; }
[ "$(tail -c 64 "$tmp/gosys.bin" | head -c 8)" = "CXGOEND1" ] || { echo "build-gosys.sh: the trailer is not last" >&2; exit 1; }
{
echo "// Generated by tools/nativeguest/build-gosys.sh from gosys/gosys.c. Do not edit."
echo "#define GOSYS_BLOB_SIZE $size"
echo "#define GOSYS_ENTRY_OFF 0x$entry"
echo "#define GOSYS_THUNKS_OFF 0x$thunks"
echo "static const unsigned char gosys_blob[GOSYS_BLOB_SIZE] = {"
od -An -v -tx1 "$tmp/gosys.bin" | sed -e 's/^ *//' -e 's/ /, 0x/g' -e 's/^/0x/' -e 's/$/,/'
echo "};"
} > "$out.tmp"
mv "$out.tmp" "$out"
echo "build-gosys.sh: $out ($size bytes, entry 0x$entry)"