]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: add gdbarch_stack_grows_down function
authorAndrew Burgess <aburgess@redhat.com>
Sun, 5 May 2024 10:00:04 +0000 (11:00 +0100)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 10 May 2024 13:21:21 +0000 (14:21 +0100)
In another patch I'm working on I needed to ask: does the stack grow
down, or grow up?

Looking around I found in infcall.c some code where we needed to ask
the same question, what we do there is ask:

  gdbarch_inner_than (gdbarch, 1, 2)

which should do the job.  However, I don't particularly like copying
this, it feels like we're asking something slightly different that
just happens to align with the question we're actually asking.

I propose adding a new function `gdbarch_stack_grows_down`.  This is
not going to be a gdbarch method that can be overridden, instead, this
will just call the gdbarch_inner_than function.  We already have some
gdbarch methods like this, checkout arch-utils.c for examples.

I think it's now clearer what we're actually doing.

A new self-test ensures that all architectures have a stack that
either grows down, or grows up.

There should be no user visible changes after this commit.

Approved-By: Tom Tromey <tom@tromey.com>
gdb/gdbarch-selftests.c
gdb/gdbarch.h
gdb/infcall.c

index 0dc0c5006544aae4729564785ca35fc661820ed5..db99fe08141234109deae07953f2c05d6457b025 100644 (file)
@@ -164,6 +164,21 @@ register_name_test (struct gdbarch *gdbarch)
     }
 }
 
+/* Test gdbarch_stack_grows_down.  Stacks must either grow down or up.  */
+
+static void
+check_stack_growth (struct gdbarch *gdbarch)
+{
+  /* We don't call gdbarch_stack_grows_down here, instead we're testing the
+     implementation by calling gdbarch_inner_than.  GDB assumes that stacks
+     either grow down or up (see uses of gdbarch_stack_grows_down), so exactly
+     one of these needs to be true.  */
+  bool stack_grows_down = gdbarch_inner_than (gdbarch, 1, 2) != 0;
+  bool stack_grows_up = gdbarch_inner_than (gdbarch, 2, 1) != 0;
+
+  SELF_CHECK (stack_grows_up != stack_grows_down);
+}
+
 } // namespace selftests
 
 void _initialize_gdbarch_selftests ();
@@ -175,4 +190,7 @@ _initialize_gdbarch_selftests ()
 
   selftests::register_test_foreach_arch ("register_name",
                                         selftests::register_name_test);
+
+  selftests::register_test_foreach_arch ("stack_growth",
+                                        selftests::check_stack_growth);
 }
index 77d3406779ffaad94ec97bab7478a51769d14881..d4c6795a12b11d10b7c7dadf85d2271593b28a62 100644 (file)
@@ -370,4 +370,12 @@ gdbarch_num_cooked_regs (gdbarch *arch)
   return gdbarch_num_regs (arch) + gdbarch_num_pseudo_regs (arch);
 }
 
+/* Return true if stacks for ARCH grow down, otherwise return true.  */
+
+static inline bool
+gdbarch_stack_grows_down (gdbarch *arch)
+{
+  return gdbarch_inner_than (arch, 1, 2) != 0;
+}
+
 #endif
index 23d5652dd2130771aa50bfa268f1ee47b3eac15f..edac9a7417916219de3f05e25a91dbd2212f4476 100644 (file)
@@ -947,7 +947,7 @@ reserve_stack_space (const type *values_type, CORE_ADDR &sp)
   struct gdbarch *gdbarch = get_frame_arch (frame);
   CORE_ADDR addr = 0;
 
-  if (gdbarch_inner_than (gdbarch, 1, 2))
+  if (gdbarch_stack_grows_down (gdbarch))
     {
       /* Stack grows downward.  Align STRUCT_ADDR and SP after
         making space.  */
@@ -1128,7 +1128,7 @@ call_function_by_hand_dummy (struct value *function,
           address.  AMD64 called that region the "red zone".  Skip at
           least the "red zone" size before allocating any space on
           the stack.  */
-       if (gdbarch_inner_than (gdbarch, 1, 2))
+       if (gdbarch_stack_grows_down (gdbarch))
          sp -= gdbarch_frame_red_zone_size (gdbarch);
        else
          sp += gdbarch_frame_red_zone_size (gdbarch);
@@ -1156,11 +1156,9 @@ call_function_by_hand_dummy (struct value *function,
           to pay :-).  */
        if (sp == old_sp)
          {
-           if (gdbarch_inner_than (gdbarch, 1, 2))
-             /* Stack grows down.  */
+           if (gdbarch_stack_grows_down (gdbarch))
              sp = gdbarch_frame_align (gdbarch, old_sp - 1);
            else
-             /* Stack grows up.  */
              sp = gdbarch_frame_align (gdbarch, old_sp + 1);
          }
        /* SP may have underflown address zero here from OLD_SP.  Memory access
@@ -1193,7 +1191,7 @@ call_function_by_hand_dummy (struct value *function,
          {
            CORE_ADDR lastval_addr = lastval->address ();
 
-           if (gdbarch_inner_than (gdbarch, 1, 2))
+           if (gdbarch_stack_grows_down (gdbarch))
              {
                gdb_assert (sp >= lastval_addr);
                sp = lastval_addr;