Skip to content

Instantly share code, notes, and snippets.

@m0sys
Last active October 5, 2024 11:46
Show Gist options
  • Save m0sys/711d0ec5e52102c6ba44451caf38bd38 to your computer and use it in GitHub Desktop.
Save m0sys/711d0ec5e52102c6ba44451caf38bd38 to your computer and use it in GitHub Desktop.
Apple M1 ARM GDB Build from Source with Python Support
#!/usr/bin/env sh
# NOTE: First, have to ignore the following warnings to make it build on M1 Chip.
#
# -Wno-constant-logical-operand -Wno-format-nonliteral -Wno-self-assign
#
# To do so the above line should be appended to consts CFLAGS and CXXFLAGS
# under the 'Programs producing files for the HOST machine' section of
# the generated Makefile in dir 'build-gdb'.
# With the new updated flags clang should be able build gdb successfully with
# no more errors. Rerun "make" and then "make install" while in the "gdb-build"
# dir.
#
# Second, be sure to update 'with-python' and 'with-libgmp' flags below to appropriate
# location on personal system so gdb knows where to find them.
#
# Third, copy line 24 and 25 to terminal rc file i.e. .zshrc|.bashrc|etc ... so gdb is
# on terminal PATH.
#
# Fourth, enjoy debugging on the M1 with GDB.
export TARGET=arm-none-eabi
export PREFIX=$HOME/.local
export PATH=$PATH:$PREFIX/bin
export VERSION=12.1
export GDB=gdb-$VERSION
rm -rf $GDB
# Get archives
wget http://ftp.gnu.org/gnu/gdb/$GDB.tar.gz
# Extract archives
tar xzvf $GDB.tar.gz
mkdir build-gdb
cd build-gdb
../$GDB/configure --target=$TARGET --prefix=$PREFIX --enable-interwork --enable-multilib --with-python=/opt/homebrew/bin/python3 --with-libgmp-prefix=/opt/homebrew/Cellar/gmp/6.2.1_1
make
make install
@JayFoxRox
Copy link

I think this still needs some work:

  1. When rerunning, it will delete the build folder, but it will still redownload the GDB sources (but rename it to avoid overwriting the old one).
  2. build-gdb folder also isn't removed; this might be intentional so users can modify the flags?
  3. Flags can be modified like this (configure respects environment variables):
     export CFLAGS="$CFLAGS -Wno-constant-logical-operand -Wno-format-nonliteral -Wno-self-assign"
     export CXXFLAGS="$CXXFLAGS -Wno-constant-logical-operand -Wno-format-nonliteral -Wno-self-assign"
  4. Building also requires the brew texinfo package for makeinfo (probably for generating some docs?). I'd suggest to turn off the feature which requires this, or list which dependencies this script requires (the source also suggests python and libgmp).
  5. Even with all of these things done, I have an arm-none-eabi-gdb but it's not able to load mach-o binaries, so even trivial stuff like gcc-12 main.c -g -o main && gdb main (with CC from either homebrew gcc OR xcode) will result in "[...]/main": not in executable format: file format not recognized (file: main: Mach-O 64-bit executable arm64).

I think it's very annoying that I can't easily have GDB on M1.
I've automated some tasks with Python scripts using the GDB Python API and CLI, so I can't just easily migrate to lldb.

@simonjwright
Copy link

TARGET=arm-none-eabi would be reasonable if you were building a cross-debugger for a microprocessor control unit with an ARM chip.
TARGET=aarch64-apple-darwin would be more appropriate.

@JayFoxRox
Copy link

TARGET=aarch64-apple-darwin would be more appropriate

Leads to:

*** This configuration is not supported in the following subdirectories:
     gdbserver gdb sim
    (Any other directories should still work fine.)

(which is to be expected, because gdb wasn't ported yet)

@DongShuaike
Copy link

Sir, thank you very much for the gist.
Have you ever tried compile corresponding gdbserver 12.1 as well?

@lmBored
Copy link

lmBored commented Mar 8, 2023

I tried installing gdb 12.1 manually on a macbook air m2 and it still not working, so not sure if this script works on m2

@illyoung
Copy link

@JayFoxRox Have you solved the issue with
"[...]/main": not in executable format: file format not recognized (file: main: Mach-O 64-bit executable arm64).
I'm on the same stage and cannot solve the issue.
If you have any progress, please share the solution. Currently my OS version is 13.1

Thanks in advance.
Young

@JayFoxRox
Copy link

No, it's not a user-error, but just missing support for it in gdb.

The latest information appears to be in https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96168 - it links to a fork which has support for macOS on aarch64, but I didn't try it yet.

@m0sys
Copy link
Author

m0sys commented Mar 22, 2023

Hey guys, I apologize for late response! This script that I posted only works for arm-none-eabi. I haven't personally tried to compile gdb for aarch64 since its not supported. Essentially, it's an abi issue. Check out gcc-darwin-arm64 for further support (haven't tried it yet). Thanks @JayFoxRox for the link!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment