From 783a58dbc77a5af99d979bcdbf64a97e43ca9be8 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Tue, 7 Sep 2021 02:14:32 +0300 Subject: [PATCH] Toolchain: Build aarch64-gdb for cross-debugging on x86 --- Meta/debug-kernel.sh | 29 +++++++++++++----- Toolchain/BuildIt.sh | 60 +++++++++++++++++++++++++++++++++++++ Toolchain/Patches/gdb.patch | 40 +++++++++++++++++++++++++ 3 files changed, 122 insertions(+), 7 deletions(-) create mode 100644 Toolchain/Patches/gdb.patch diff --git a/Meta/debug-kernel.sh b/Meta/debug-kernel.sh index 47d4bdc25e5..235841944de 100755 --- a/Meta/debug-kernel.sh +++ b/Meta/debug-kernel.sh @@ -1,8 +1,18 @@ #!/bin/sh +SCRIPT_DIR="$(dirname "${0}")" + # Set this environment variable to override the default debugger. # -[ -z "$SERENITY_KERNEL_DEBUGGER" ] && SERENITY_KERNEL_DEBUGGER="gdb" +if [ -z "$SERENITY_KERNEL_DEBUGGER" ]; then + if [ "$SERENITY_ARCH" = "aarch64" ]; then + # Prepend the toolchain aarch64 bin directory so we pick up GDB from there + PATH="$SCRIPT_DIR/../Toolchain/Local/aarch64/bin:$PATH" + SERENITY_KERNEL_DEBUGGER="aarch64-pc-serenity-gdb" + else + SERENITY_KERNEL_DEBUGGER="gdb" + fi +fi # The QEMU -s option (enabled by default in ./run) sets up a debugger # remote on localhost:1234. So point our debugger there, and inform @@ -12,10 +22,14 @@ if [ "$SERENITY_ARCH" = "x86_64" ]; then gdb_arch=i386:x86-64 prekernel_image=Prekernel64 kernel_base=0x2000200000 -else +elif [ "$SERENITY_ARCH" = "i686" ]; then gdb_arch=i386:intel - prekernel_image=Prekernel + prekernel_image=Prekernel32 kernel_base=0xc0200000 +elif [ "$SERENITY_ARCH" = "aarch64" ]; then + gdb_arch=aarch64:armv8-r + prekernel_image=Prekernel + kernel_base=0xc0000000 # FIXME fi # FIXME: This doesn't work when running QEMU inside the WSL2 VM @@ -25,16 +39,17 @@ else gdb_host=localhost fi + exec $SERENITY_KERNEL_DEBUGGER \ - -ex "file $(dirname "$0")/../Build/${SERENITY_ARCH:-i686}/Kernel/Prekernel/$prekernel_image" \ + -ex "file $SCRIPT_DIR/../Build/${SERENITY_ARCH:-i686}/Kernel/Prekernel/$prekernel_image" \ -ex "set confirm off" \ - -ex "directory $(dirname "$0")/../Build/${SERENITY_ARCH:-i686}/" \ - -ex "add-symbol-file $(dirname "$0")/../Build/${SERENITY_ARCH:-i686}/Kernel/Kernel -o $kernel_base" \ + -ex "directory $SCRIPT_DIR/../Build/${SERENITY_ARCH:-i686}/" \ + -ex "add-symbol-file $SCRIPT_DIR/../Build/${SERENITY_ARCH:-i686}/Kernel/Kernel -o $kernel_base" \ -ex "set confirm on" \ -ex "set arch $gdb_arch" \ -ex "set print frame-arguments none" \ -ex "target remote ${gdb_host}:1234" \ - -ex "source $(dirname "$0")/serenity_gdb.py" \ + -ex "source $SCRIPT_DIR/serenity_gdb.py" \ -ex "layout asm" \ -ex "fs next" \ "$@" diff --git a/Toolchain/BuildIt.sh b/Toolchain/BuildIt.sh index 4f411c0b36c..eb62622e317 100755 --- a/Toolchain/BuildIt.sh +++ b/Toolchain/BuildIt.sh @@ -78,6 +78,12 @@ BINUTILS_NAME="binutils-$BINUTILS_VERSION" BINUTILS_PKG="${BINUTILS_NAME}.tar.gz" BINUTILS_BASE_URL="http://ftp.gnu.org/gnu/binutils" +GDB_VERSION="10.2" +GDB_MD5SUM="7aeb896762924ae9a2ec59525088bada" +GDB_NAME="gdb-$GDB_VERSION" +GDB_PKG="${GDB_NAME}.tar.gz" +GDB_BASE_URL="http://ftp.gnu.org/gnu/gdb" + # Note: If you bump the gcc version, you also have to update the matching # GCC_VERSION variable in the project's root CMakeLists.txt GCC_VERSION="11.2.0" @@ -174,6 +180,21 @@ popd # === DOWNLOAD AND PATCH === pushd "$DIR/Tarballs" + # Build aarch64-gdb for cross-debugging support on x86 systems + if [ "$ARCH" = "aarch64" ]; then + md5="" + if [ -e "$GDB_PKG" ]; then + md5="$($MD5SUM $GDB_PKG | cut -f1 -d' ')" + echo "bu md5='$md5'" + fi + if [ "$md5" != ${GDB_MD5SUM} ] ; then + rm -f $GDB_PKG + curl -LO "$GDB_BASE_URL/$GDB_PKG" + else + echo "Skipped downloading gdb" + fi + fi + md5="" if [ -e "$BINUTILS_PKG" ]; then md5="$($MD5SUM $BINUTILS_PKG | cut -f1 -d' ')" @@ -198,6 +219,27 @@ pushd "$DIR/Tarballs" echo "Skipped downloading gcc" fi + if [ "$ARCH" = "aarch64" ]; then + if [ -d ${GDB_NAME} ]; then + rm -rf "${GDB_NAME}" + rm -rf "$DIR/Build/$ARCH/$GDB_NAME" + fi + echo "Extracting GDB..." + tar -xzf ${GDB_PKG} + + pushd ${GDB_NAME} + if [ "$git_patch" = "1" ]; then + git init > /dev/null + git add . > /dev/null + git commit -am "BASE" > /dev/null + git apply "$DIR"/Patches/gdb.patch > /dev/null + else + patch -p1 < "$DIR"/Patches/gdb.patch > /dev/null + fi + $MD5SUM "$DIR"/Patches/gdb.patch > .patch.applied + popd + fi + if [ -d ${BINUTILS_NAME} ]; then rm -rf "${BINUTILS_NAME}" rm -rf "$DIR/Build/$ARCH/$BINUTILS_NAME" @@ -259,6 +301,24 @@ mkdir -p "$DIR/Build/$ARCH" pushd "$DIR/Build/$ARCH" unset PKG_CONFIG_LIBDIR # Just in case + if [ "$ARCH" = "aarch64" ]; then + rm -rf gdb + mkdir -p gdb + + pushd gdb + echo "XXX configure gdb" + buildstep "gdb/configure" "$DIR"/Tarballs/$GDB_NAME/configure --prefix="$PREFIX" \ + --target="$TARGET" \ + --with-sysroot="$SYSROOT" \ + --enable-shared \ + --disable-nls \ + ${TRY_USE_LOCAL_TOOLCHAIN:+"--quiet"} || exit 1 + echo "XXX build gdb" + buildstep "gdb/build" "$MAKE" -j "$MAKEJOBS" || exit 1 + buildstep "gdb/install" "$MAKE" install || exit 1 + popd + fi + rm -rf binutils mkdir -p binutils diff --git a/Toolchain/Patches/gdb.patch b/Toolchain/Patches/gdb.patch new file mode 100644 index 00000000000..9b6e1a51b87 --- /dev/null +++ b/Toolchain/Patches/gdb.patch @@ -0,0 +1,40 @@ +diff -ur gdb-10.2-orig/bfd/config.bfd gdb-10.2/bfd/config.bfd +--- gdb-10.2-orig/bfd/config.bfd 2021-04-25 07:06:26.000000000 +0300 ++++ gdb-10.2/bfd/config.bfd 2021-09-07 01:14:58.781960654 +0300 +@@ -224,7 +224,25 @@ + + case "${targ}" in + # START OF targmatch.h ++ i[3-7]86-*-serenity*) ++ targ_defvec=i386_elf32_vec ++ targ_selvecs= ++ ;; ++ arm-*-serenity*) ++ targ_defvec=arm_elf32_le_vec ++ targ_selvecs= ++ ;; + #ifdef BFD64 ++ x86_64-*-serenity*) ++ targ_defvec=x86_64_elf64_vec ++ targ_selvecs=i386_elf32_vec ++ want64=true ++ ;; ++ aarch64-*-serenity*) ++ targ_defvec=aarch64_elf64_le_vec ++ targ_selvecs= ++ want64=true ++ ;; + aarch64-*-darwin*) + targ_defvec=aarch64_mach_o_vec + targ_selvecs="arm_mach_o_vec mach_o_le_vec mach_o_be_vec mach_o_fat_vec" +diff -ur gdb-10.2-orig/config.sub gdb-10.2/config.sub +--- gdb-10.2-orig/config.sub 2021-04-25 07:06:26.000000000 +0300 ++++ gdb-10.2/config.sub 2021-09-07 01:20:15.360899801 +0300 +@@ -1339,6 +1339,7 @@ + # Each alternative MUST end in a * to match a version number. + # sysv* is not here because it comes later, after sysvr4. + gnu* | bsd* | mach* | minix* | genix* | ultrix* | irix* \ ++ | serenity* \ + | *vms* | esix* | aix* | cnk* | sunos | sunos[34]*\ + | hpux* | unos* | osf* | luna* | dgux* | auroraux* | solaris* \ + | sym* | kopensolaris* | plan9* \