0001-GDB-aarch64-linux-Fix-build-failure-on-musl-systems.patch 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. From 1ccc3f6a2e28fa1f3357826374cba165b3ba3ff7 Mon Sep 17 00:00:00 2001
  2. From: Thiago Jung Bauermann <thiago.bauermann@linaro.org>
  3. Date: Wed, 11 Feb 2026 04:54:28 +0000
  4. Subject: [PATCH] GDB: aarch64-linux: Fix build failure on musl systems
  5. (cherry picked from commit 02090062127d59978ccc312dabf63c6ea838cd85)
  6. When building against musl (e.g. on Alpine Linux), the following error
  7. happens:
  8. CXX linux-aarch64-low.o
  9. In file included from /home/bauermann/src/binutils-gdb/gdbserver/linux-aarch64-low.cc:42:
  10. /home/bauermann/src/binutils-gdb/gdbserver/../gdb/arch/aarch64-gcs-linux.h:35:8: error: redefinition of 'struct user_gcs'
  11. 35 | struct user_gcs
  12. | ^~~~~~~~
  13. In file included from /home/bauermann/src/binutils-gdb/gdbserver/linux-aarch64-low.cc:35:
  14. /usr/include/asm/ptrace.h:329:8: note: previous definition of 'struct user_gcs'
  15. 329 | struct user_gcs {
  16. | ^~~~~~~~
  17. make[2]: *** [Makefile:565: linux-aarch64-low.o] Error 1
  18. aarch64-linux-tdep.c fails to build in the same way. This happens because
  19. aarch64-gcs-linux.h uses GCS_MAGIC to see whether the system headers
  20. have GCS-related definitions. The problem is that GCS_MAGIC is defined in
  21. <asm/sigcontext.h> while struct gcs_user is defined in <asm/ptrace.h>.
  22. It's fine on glibc systems because in the set of system headers that
  23. linux-aarch64-low.cc and aarch64-linux-tdep.c include, <asm/sigcontext.h>
  24. ends up being included implicitly as well. This doesn't happen when using
  25. musl's headers though.
  26. There isn't a macro in <asm/ptrace.h> whose presence is correlated with
  27. the presence of the struct user_gcs definition, so a configure check is
  28. needed to detect it and conditionally define the struct.
  29. Also, this change requires aarch64-linux-tdep.c to stop using
  30. struct user_gcs because target-dependent code can't include <asm/ptrace.h>
  31. and thus even if HAVE_STRUCT_USER_GCS is set, the file won't have the
  32. struct definition available. To fix this problem, also backport the
  33. definition of AARCH64_LINUX_SIZEOF_GCS_REGSET and use it there.
  34. Note that there's another build issue with musl, described in
  35. PR gdb/33747 affecting compilation of gdb/ser-unix.c. In order to be
  36. able to test this patch, I applied the patch in comment 11 there.
  37. Tested with a native build on an Alpine Linux aarch64 system, and also
  38. verified that all gdb.arch/aarch64-gcs*.exp tests pass on it.
  39. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=33926
  40. Co-authored-by: Chris Packham <judge.packham@gmail.com>
  41. Approved-By: Andrew Burgess <aburgess@redhat.com>
  42. Signed-off-by: Waldemar Brodkorb <wbx@openadk.org>
  43. ---
  44. gdb/aarch64-linux-tdep.c | 5 +++--
  45. gdb/arch/aarch64-gcs-linux.h | 8 +++++---
  46. gdbsupport/config.in | 3 +++
  47. gdbsupport/configure | 36 ++++++++++++++++++++++++++++++++++++
  48. gdbsupport/configure.ac | 19 +++++++++++++++++++
  49. 5 files changed, 66 insertions(+), 5 deletions(-)
  50. diff --git a/gdb/aarch64-linux-tdep.c b/gdb/aarch64-linux-tdep.c
  51. index 76bde85188b..6c402e7ecdd 100644
  52. --- a/gdb/aarch64-linux-tdep.c
  53. +++ b/gdb/aarch64-linux-tdep.c
  54. @@ -1684,8 +1684,9 @@ aarch64_linux_iterate_over_regset_sections (struct gdbarch *gdbarch,
  55. gcs_regmap, regcache_supply_regset, regcache_collect_regset
  56. };
  57. - cb (".reg-aarch-gcs", sizeof (user_gcs), sizeof (user_gcs),
  58. - &aarch64_linux_gcs_regset, "GCS registers", cb_data);
  59. + cb (".reg-aarch-gcs", AARCH64_LINUX_SIZEOF_GCS_REGSET,
  60. + AARCH64_LINUX_SIZEOF_GCS_REGSET, &aarch64_linux_gcs_regset,
  61. + "GCS registers", cb_data);
  62. }
  63. }
  64. diff --git a/gdb/arch/aarch64-gcs-linux.h b/gdb/arch/aarch64-gcs-linux.h
  65. index 018ca37a522..632823a8120 100644
  66. --- a/gdb/arch/aarch64-gcs-linux.h
  67. +++ b/gdb/arch/aarch64-gcs-linux.h
  68. @@ -27,8 +27,7 @@
  69. #define HWCAP_GCS (1ULL << 32)
  70. #endif
  71. -/* Make sure we only define these if the kernel header doesn't. */
  72. -#ifndef GCS_MAGIC
  73. +#ifndef HAVE_STRUCT_USER_GCS
  74. /* GCS state (NT_ARM_GCS). */
  75. @@ -39,6 +38,9 @@ struct user_gcs
  76. uint64_t gcspr_el0;
  77. };
  78. -#endif /* GCS_MAGIC */
  79. +#endif /* HAVE_STRUCT_USER_GCS */
  80. +
  81. +/* The GCS regset consists of 3 64-bit registers. */
  82. +#define AARCH64_LINUX_SIZEOF_GCS_REGSET (3 * 8)
  83. #endif /* GDB_ARCH_AARCH64_GCS_LINUX_H */
  84. diff --git a/gdbsupport/config.in b/gdbsupport/config.in
  85. index 0beacf22c05..2957ee0f030 100644
  86. --- a/gdbsupport/config.in
  87. +++ b/gdbsupport/config.in
  88. @@ -271,6 +271,9 @@
  89. /* Define to 1 if `st_blocks' is a member of `struct stat'. */
  90. #undef HAVE_STRUCT_STAT_ST_BLOCKS
  91. +/* Define to 1 if your system has struct user_gcs. */
  92. +#undef HAVE_STRUCT_USER_GCS
  93. +
  94. /* Define to 1 if you have the <sys/param.h> header file. */
  95. #undef HAVE_SYS_PARAM_H
  96. diff --git a/gdbsupport/configure b/gdbsupport/configure
  97. index 133ddfa7f6c..66135791aa5 100755
  98. --- a/gdbsupport/configure
  99. +++ b/gdbsupport/configure
  100. @@ -14307,6 +14307,42 @@ ac_compiler_gnu=$ac_cv_c_compiler_gnu
  101. +# Check for `struct user_gcs`
  102. +{ $as_echo "$as_me:${as_lineno-$LINENO}: checking for struct user_gcs" >&5
  103. +$as_echo_n "checking for struct user_gcs... " >&6; }
  104. +if ${gdb_cv_struct_user_gcs+:} false; then :
  105. + $as_echo_n "(cached) " >&6
  106. +else
  107. + cat confdefs.h - <<_ACEOF >conftest.$ac_ext
  108. +/* end confdefs.h. */
  109. +#include <sys/ptrace.h>
  110. + #include <asm/ptrace.h>
  111. +int
  112. +main ()
  113. +{
  114. +struct user_gcs u;
  115. +
  116. + ;
  117. + return 0;
  118. +}
  119. +_ACEOF
  120. +if ac_fn_c_try_compile "$LINENO"; then :
  121. + gdb_cv_struct_user_gcs=yes
  122. +else
  123. + gdb_cv_struct_user_gcs=no
  124. +
  125. +fi
  126. +rm -f core conftest.err conftest.$ac_objext conftest.$ac_ext
  127. +
  128. +fi
  129. +{ $as_echo "$as_me:${as_lineno-$LINENO}: result: $gdb_cv_struct_user_gcs" >&5
  130. +$as_echo "$gdb_cv_struct_user_gcs" >&6; }
  131. +if test "$gdb_cv_struct_user_gcs" = yes; then
  132. +
  133. +$as_echo "#define HAVE_STRUCT_USER_GCS 1" >>confdefs.h
  134. +
  135. +fi
  136. +
  137. # Set the 'development' global.
  138. . $srcdir/../bfd/development.sh
  139. diff --git a/gdbsupport/configure.ac b/gdbsupport/configure.ac
  140. index b7ccfabd6c6..d3b4c05daeb 100644
  141. --- a/gdbsupport/configure.ac
  142. +++ b/gdbsupport/configure.ac
  143. @@ -68,6 +68,25 @@ GDB_AC_PTRACE
  144. AM_GDB_COMPILER_TYPE
  145. AM_GDB_WARNINGS
  146. +# Check for `struct user_gcs`
  147. +AC_CACHE_CHECK(
  148. + [for struct user_gcs],
  149. + [gdb_cv_struct_user_gcs],
  150. + [AC_COMPILE_IFELSE(
  151. + [AC_LANG_PROGRAM(
  152. + [#include <sys/ptrace.h>
  153. + #include <asm/ptrace.h>],
  154. + [struct user_gcs u;]
  155. + )],
  156. + [gdb_cv_struct_user_gcs=yes],
  157. + [gdb_cv_struct_user_gcs=no]
  158. + )]
  159. +)
  160. +if test "$gdb_cv_struct_user_gcs" = yes; then
  161. + AC_DEFINE(HAVE_STRUCT_USER_GCS, 1,
  162. + [Define to 1 if your system has struct user_gcs.])
  163. +fi
  164. +
  165. # Set the 'development' global.
  166. . $srcdir/../bfd/development.sh
  167. --
  168. 2.47.3