From: Mark Kettenis Date: Wed, 4 Feb 2004 21:49:58 +0000 (+0000) Subject: * target.h (target_object): Add TARGET_OBJECT_WCOOKIE. X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=baf92889ebd77dceec279733db08e537eefbff49;p=binutils-gdb.git * target.h (target_object): Add TARGET_OBJECT_WCOOKIE. * inftarg.c: Update copyright year. (child_xfer_partial): Add support for TARGET_OBJECT_WCOOKIE. * sparc-nat.c: Include "target.h" and "gdb_assert.h". (sparc_xfer_wcookie): New function. * sparc-tdep.c (sparc_fetch_wcookie): New function. * Makefile.in (sparc-nat.o): Update dependencies. * config/sparc/nm-nbsd.h: Include "target.h". (NATIVE_XFER_WCOOKIE): New define. (sparc_xfer_wcookie): New prototype. --- diff --git a/gdb/Makefile.in b/gdb/Makefile.in index e5fbe7e4201..0a2d210ec81 100644 --- a/gdb/Makefile.in +++ b/gdb/Makefile.in @@ -2383,8 +2383,9 @@ sparc-linux-tdep.o: sparc-linux-tdep.c $(defs_h) $(floatformat_h) $(frame_h) \ $(frame_unwind_h) $(gdbarch_h) $(gdbcore_h) $(osabi_h) $(regcache_h) \ $(solib_svr4_h) $(symtab_h) $(trad_frame_h) $(gdb_assert_h) \ $(gdb_string_h) $(sparc_tdep_h) -sparc-nat.o: sparc-nat.c $(defs_h) $(inferior_h) $(regcache_h) \ - $(gdb_string_h) $(gdb_wait_h) $(sparc_tdep_h) $(sparc_nat_h) +sparc-nat.o: sparc-nat.c $(defs_h) $(inferior_h) $(regcache_h) $(target_h) \ + $(gdb_assert_h) $(gdb_string_h) $(gdb_wait_h) $(sparc_tdep_h) \ + $(sparc_nat_h) sparcnbsd-nat.o: sparcnbsd-nat.c $(defs_h) $(sparc_tdep_h) $(sparc_nat_h) sparcnbsd-tdep.o: sparcnbsd-tdep.c $(defs_h) $(floatformat_h) $(frame_h) \ $(frame_unwind_h) $(gdbcore_h) $(osabi_h) $(regcache_h) $(regset_h) \ diff --git a/gdb/config/sparc/nm-nbsd.h b/gdb/config/sparc/nm-nbsd.h index 089a5acf1dc..5e052341399 100644 --- a/gdb/config/sparc/nm-nbsd.h +++ b/gdb/config/sparc/nm-nbsd.h @@ -25,5 +25,17 @@ /* Get generic NetBSD native definitions. */ #include "config/nm-nbsd.h" + + +/* Support for StackGhost cookies. */ + +#include "target.h" + +#define NATIVE_XFER_WCOOKIE sparc_xfer_wcookie +extern LONGEST sparc_xfer_wcookie (struct target_ops *ops, + enum target_object object, + const char *annex, + void *readbuf, const void *writebuf, + ULONGEST offset, LONGEST len); #endif /* nm-nbsd.h */ diff --git a/gdb/inftarg.c b/gdb/inftarg.c index 67706d24897..a6f40ec12c2 100644 --- a/gdb/inftarg.c +++ b/gdb/inftarg.c @@ -1,7 +1,7 @@ /* Target-vector operations for controlling Unix child processes, for GDB. Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1998, 1999, - 2000, 2002, 2003 Free Software Foundation, Inc. + 2000, 2002, 2003, 2004 Free Software Foundation, Inc. Contributed by Cygnus Support. @@ -585,6 +585,13 @@ child_xfer_partial (struct target_ops *ops, enum target_object object, return NATIVE_XFER_AUXV (ops, object, annex, readbuf, writebuf, offset, len); + case TARGET_OBJECT_WCOOKIE: +#ifndef NATIVE_XFER_WCOOKIE +#define NATIVE_XFER_WCOOKIE(OPS,OBJECT,ANNEX,WRITEBUF,READBUF,OFFSET,LEN) (-1) +#endif + return NATIVE_XFER_WCOOKIE (ops, object, annex, readbuf, writebuf, + offset, len); + default: return -1; } diff --git a/gdb/sparc-nat.c b/gdb/sparc-nat.c index 3ab21f0e160..477fc975b74 100644 --- a/gdb/sparc-nat.c +++ b/gdb/sparc-nat.c @@ -22,7 +22,9 @@ #include "defs.h" #include "inferior.h" #include "regcache.h" +#include "target.h" +#include "gdb_assert.h" #include #include "gdb_string.h" #include @@ -246,8 +248,40 @@ store_inferior_registers (int regnum) return; } } + +/* Fetch StackGhost Per-Process XOR cookie. */ + +LONGEST +sparc_xfer_wcookie (struct target_ops *ops, enum target_object object, + const char *annex, void *readbuf, const void *writebuf, + ULONGEST offset, LONGEST len) +{ + unsigned long wcookie = 0; + char *buf = (char *)&wcookie; + + gdb_assert (object == TARGET_OBJECT_WCOOKIE); + gdb_assert (readbuf && writebuf == NULL); + + if (offset >= sizeof (unsigned long)) + return -1; +#ifdef PT_WCOOKIE + /* If PT_WCOOKIE is defined (by ), assume we're + running on an OpenBSD release that uses StackGhost (3.1 or + later). As of release 3.4, OpenBSD doesn't use a randomized + cookie yet. */ + wcookie = 0x3; +#endif /* PT_WCOOKIE */ + + if (len > sizeof (unsigned long) - offset) + len = sizeof (unsigned long) - offset; + + memcpy (readbuf, buf + offset, len); + return len; +} + + /* Provide a prototype to silence -Wmissing-prototypes. */ void _initialize_sparc_nat (void); diff --git a/gdb/sparc-tdep.c b/gdb/sparc-tdep.c index 1719109ac28..bbe56652096 100644 --- a/gdb/sparc-tdep.c +++ b/gdb/sparc-tdep.c @@ -141,13 +141,21 @@ sparc_fetch_instruction (CORE_ADDR pc) ULONGEST sparc_fetch_wcookie (void) { - /* FIXME: kettenis/20040131: We should fetch the cookie from the - target. For now, return zero, which is right for targets without - StackGhost. */ - return 0; -} + struct target_ops *ops = ¤t_target; + char buf[8]; + int len; + + len = target_read_partial (ops, TARGET_OBJECT_WCOOKIE, NULL, buf, 0, 8); + if (len == -1) + return 0; + + /* We should have either an 32-bit or an 64-bit cookie. */ + gdb_assert (len == 4 || len == 8); + return extract_unsigned_integer (buf, len); +} + /* Return the contents if register REGNUM as an address. */ static CORE_ADDR diff --git a/gdb/target.h b/gdb/target.h index 97feb7d3644..2d8ce372fc5 100644 --- a/gdb/target.h +++ b/gdb/target.h @@ -228,6 +228,8 @@ enum target_object TARGET_OBJECT_UNWIND_TABLE, /* Transfer auxilliary vector. */ TARGET_OBJECT_AUXV, + /* StackGhost cookie. See "sparc-tdep.c". */ + TARGET_OBJECT_WCOOKIE /* Possible future objects: TARGET_OBJECT_FILE, TARGET_OBJECT_PROC, ... */ };