AArch64: Detect exit from execve syscall
[deliverable/binutils-gdb.git] / gdb / aarch64-linux-tdep.c
index e55b1a726ff184499f2f17de352939a6daf2928d..39e607658f45494382598d220ab315b8b74c191e 100644 (file)
@@ -1,6 +1,6 @@
 /* Target-dependent code for GNU/Linux AArch64.
 
-   Copyright (C) 2009-2018 Free Software Foundation, Inc.
+   Copyright (C) 2009-2019 Free Software Foundation, Inc.
    Contributed by ARM Ltd.
 
    This file is part of GDB.
 #define AARCH64_SIGCONTEXT_XO_OFFSET            8
 #define AARCH64_SIGCONTEXT_RESERVED_OFFSET      288
 
+#define AARCH64_SIGCONTEXT_RESERVED_SIZE       4096
+
 /* Unique identifiers that may be used for aarch64_ctx.magic.  */
 #define AARCH64_EXTRA_MAGIC                    0x45585401
 #define AARCH64_FPSIMD_MAGIC                   0x46508001
@@ -197,9 +199,11 @@ aarch64_linux_sigframe_init (const struct tramp_frame *self,
   CORE_ADDR sigcontext_addr = (sp + AARCH64_RT_SIGFRAME_UCONTEXT_OFFSET
                               + AARCH64_UCONTEXT_SIGCONTEXT_OFFSET );
   CORE_ADDR section = sigcontext_addr + AARCH64_SIGCONTEXT_RESERVED_OFFSET;
+  CORE_ADDR section_end = section + AARCH64_SIGCONTEXT_RESERVED_SIZE;
   CORE_ADDR fpsimd = 0;
   CORE_ADDR sve_regs = 0;
   uint32_t size, magic;
+  bool extra_found = false;
   int num_regs = gdbarch_num_regs (gdbarch);
 
   /* Read in the integer registers.  */
@@ -218,8 +222,9 @@ aarch64_linux_sigframe_init (const struct tramp_frame *self,
                           sigcontext_addr + AARCH64_SIGCONTEXT_XO_OFFSET
                             + 32 * AARCH64_SIGCONTEXT_REG_SIZE);
 
-  /* Find the FP and SVE sections.  */
-  while ((magic = read_aarch64_ctx (section, byte_order, &size)) != 0)
+  /* Search for the FP and SVE sections, stopping at null.  */
+  while ((magic = read_aarch64_ctx (section, byte_order, &size)) != 0
+        && size != 0)
     {
       switch (magic)
        {
@@ -247,8 +252,8 @@ aarch64_linux_sigframe_init (const struct tramp_frame *self,
            vq = sve_vq_from_vl (extract_unsigned_integer (buf, 2, byte_order));
 
            if (vq != tdep->vq)
-             error (_("Invalid vector length in signal frame %d vs %ld."), vq,
-                    tdep->vq);
+             error (_("Invalid vector length in signal frame %d vs %s."), vq,
+                    pulongest (tdep->vq));
 
            if (size >= AARCH64_SVE_CONTEXT_SIZE (vq))
              sve_regs = section + AARCH64_SVE_CONTEXT_REGS_OFFSET;
@@ -273,12 +278,20 @@ aarch64_linux_sigframe_init (const struct tramp_frame *self,
              }
 
            section = extract_unsigned_integer (buf, 8, byte_order);
+           extra_found = true;
            break;
          }
 
        default:
+         section += size;
          break;
        }
+
+      /* Prevent searching past the end of the reserved section.  The extra
+        section does not have a hard coded limit - we have to rely on it ending
+        with nulls.  */
+      if (!extra_found && section > section_end)
+       break;
     }
 
   if (sve_regs != 0)
@@ -744,28 +757,6 @@ aarch64_stap_parse_special_token (struct gdbarch *gdbarch,
   return 1;
 }
 
-/* Implement the "get_syscall_number" gdbarch method.  */
-
-static LONGEST
-aarch64_linux_get_syscall_number (struct gdbarch *gdbarch,
-                                 thread_info *thread)
-{
-  struct regcache *regs = get_thread_regcache (thread);
-  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
-
-  /* The content of register x8.  */
-  gdb_byte buf[X_REGISTER_SIZE];
-  /* The result.  */
-  LONGEST ret;
-
-  /* Getting the system call number from the register x8.  */
-  regs->cooked_read (AARCH64_DWARF_X0 + 8, buf);
-
-  ret = extract_signed_integer (buf, X_REGISTER_SIZE, byte_order);
-
-  return ret;
-}
-
 /* AArch64 process record-replay constructs: syscall, signal etc.  */
 
 struct linux_record_tdep aarch64_linux_record_tdep;
@@ -1321,6 +1312,40 @@ aarch64_canonicalize_syscall (enum aarch64_syscall syscall_number)
   }
 }
 
+/* Retrieve the syscall number at a ptrace syscall-stop, either on syscall entry
+   or exit.  Return -1 upon error.  */
+
+static LONGEST
+aarch64_linux_get_syscall_number (struct gdbarch *gdbarch, thread_info *thread)
+{
+  struct regcache *regs = get_thread_regcache (thread);
+  LONGEST ret;
+
+  /* Get the system call number from register x8.  */
+  regs->cooked_read (AARCH64_X0_REGNUM + 8, &ret);
+
+  /* On exit from a successful execve, we will be in a new process and all the
+     registers will be cleared - x0 to x30 will be 0, except for a 1 in x7.
+     This function will only ever get called when stopped at the entry or exit
+     of a syscall, so by checking for 0 in x0 (arg0/retval), x1 (arg1), x8
+     (syscall), x29 (FP) and x30 (LR) we can infer:
+     1) Either inferior is at exit from sucessful execve.
+     2) Or inferior is at entry to a call to io_setup with invalid arguments and
+       a corrupted FP and LR.
+     It should be safe enough to assume case 1.  */
+  if (ret == 0)
+    {
+      LONGEST x1 = -1, fp = -1, lr = -1;
+      regs->cooked_read (AARCH64_X0_REGNUM + 1, &x1);
+      regs->cooked_read (AARCH64_FP_REGNUM, &fp);
+      regs->cooked_read (AARCH64_LR_REGNUM, &lr);
+      if (x1 == 0 && fp ==0 && lr == 0)
+       return aarch64_sys_execve;
+    }
+
+  return ret;
+}
+
 /* Record all registers but PC register for process-record.  */
 
 static int
This page took 0.026454 seconds and 4 git commands to generate.