public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] PIE support for OpenBSD
@ 2011-12-18  1:14 Mark Kettenis
  2011-12-21 17:16 ` Jan Kratochvil
  0 siblings, 1 reply; 10+ messages in thread
From: Mark Kettenis @ 2011-12-18  1:14 UTC (permalink / raw)
  To: gdb-patches

Just a matter of getting the right info out of the auxv vector, and I
added support to the OpenBSD kernel for that a while ago.  Needs its
own function to parse the values since default_auxv_parse is
Linux-specific (and therefore not standards compliant on LP64
platforms).  It can be argued that the parsing routine should really
live in the architecture vector.  But 32x64-bit cross-debugging
muddies the waters here.

For now this is OpenBSD-specific, but FreeBSD and NetBSD might
implement the PIOD_READ_AUXV request at some point too.

ok?


2011-12-17  Mark Kettenis  <kettenis@gnu.org>
 
	* inf-ptrace.c [PT_IO && PIOD_READ_AUXV]
	(inf_ptrace_xfer_partial): Implement TARGET_OBJECT_AUXV.
	(inf_ptrace_auxv_parse): New function.
	(inf_ptrace_target): Initialize to_auxv_parse field.

Index: inf-ptrace.c
===================================================================
RCS file: /cvs/src/src/gdb/inf-ptrace.c,v
retrieving revision 1.75
diff -u -p -r1.75 inf-ptrace.c
--- inf-ptrace.c	22 Sep 2011 10:22:28 -0000	1.75
+++ inf-ptrace.c	17 Dec 2011 20:58:40 -0000
@@ -582,6 +582,23 @@ inf_ptrace_xfer_partial (struct target_o
       return -1;
 
     case TARGET_OBJECT_AUXV:
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+      {
+	struct ptrace_io_desc piod;
+
+	if (writebuf)
+		return -1;
+	piod.piod_op = PIOD_READ_AUXV;
+	piod.piod_addr = readbuf;
+	piod.piod_offs = (void *) (long) offset;
+	piod.piod_len = len;
+
+	errno = 0;
+	if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
+	  /* Return the actual number of bytes read or written.  */
+	  return piod.piod_len;
+      }
+#endif
       return -1;
 
     case TARGET_OBJECT_WCOOKIE:
@@ -619,6 +636,41 @@ inf_ptrace_pid_to_str (struct target_ops
   return normal_pid_to_str (ptid);
 }
 
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+
+/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
+   Return 0 if *READPTR is already at the end of the buffer.
+   Return -1 if there is insufficient buffer for a whole entry.
+   Return 1 if an entry was read into *TYPEP and *VALP.  */
+
+static int
+inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
+		       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
+{
+  struct type *int_type = builtin_type (target_gdbarch)->builtin_int;
+  struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
+  const int sizeof_auxv_type = TYPE_LENGTH (int_type);
+  const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
+  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
+  gdb_byte *ptr = *readptr;
+
+  if (endptr == ptr)
+    return 0;
+
+  if (endptr - ptr < 2 * sizeof_auxv_val)
+    return -1;
+
+  *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
+  ptr += sizeof_auxv_val;	/* Alignment.  */
+  *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
+  ptr += sizeof_auxv_val;
+
+  *readptr = ptr;
+  return 1;
+}
+
+#endif
+
 /* Create a prototype ptrace target.  The client can override it with
    local methods.  */
 
@@ -644,6 +696,9 @@ inf_ptrace_target (void)
   t->to_pid_to_str = inf_ptrace_pid_to_str;
   t->to_stop = inf_ptrace_stop;
   t->to_xfer_partial = inf_ptrace_xfer_partial;
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+  t->to_auxv_parse = inf_ptrace_auxv_parse;
+#endif
 
   return t;
 }

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-18  1:14 [PATCH] PIE support for OpenBSD Mark Kettenis
@ 2011-12-21 17:16 ` Jan Kratochvil
  2011-12-21 21:27   ` Mark Kettenis
  0 siblings, 1 reply; 10+ messages in thread
From: Jan Kratochvil @ 2011-12-21 17:16 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On Sat, 17 Dec 2011 22:08:29 +0100, Mark Kettenis wrote:
> For now this is OpenBSD-specific, but FreeBSD and NetBSD might
> implement the PIOD_READ_AUXV request at some point too.
[...]
> 	* inf-ptrace.c [PT_IO && PIOD_READ_AUXV]

So why didn't you put it into *bsd*-nat.c files?


Thanks,
Jan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-21 17:16 ` Jan Kratochvil
@ 2011-12-21 21:27   ` Mark Kettenis
  2011-12-21 22:01     ` Jan Kratochvil
  2011-12-22  2:34     ` Stan Shebs
  0 siblings, 2 replies; 10+ messages in thread
From: Mark Kettenis @ 2011-12-21 21:27 UTC (permalink / raw)
  To: jan.kratochvil; +Cc: gdb-patches

> Date: Wed, 21 Dec 2011 18:13:15 +0100
> From: Jan Kratochvil <jan.kratochvil@redhat.com>
> 
> On Sat, 17 Dec 2011 22:08:29 +0100, Mark Kettenis wrote:
> > For now this is OpenBSD-specific, but FreeBSD and NetBSD might
> > implement the PIOD_READ_AUXV request at some point too.
> [...]
> > 	* inf-ptrace.c [PT_IO && PIOD_READ_AUXV]
> 
> So why didn't you put it into *bsd*-nat.c files?

There is already PT_IO support code in inf-ptrace.c.  It makes sense
to keep it all together.  I guess I could move all that code into a
seperate bsd-nat.c file, but that's quite a big undertaking.  And
inf-ptrace.c *BSD really is the primary user of inf-ptrace.c anyway.
The various Linux targets only need it to support ancient versions of
the Linux kernels; linux-nat.c ovverrides everything except for
to_fetch_registers and to_store_registers.  And those are overridden
by most, if not all, *-linux-nat.c files.

Cheers,

Mark

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-21 21:27   ` Mark Kettenis
@ 2011-12-21 22:01     ` Jan Kratochvil
  2011-12-22  2:34     ` Stan Shebs
  1 sibling, 0 replies; 10+ messages in thread
From: Jan Kratochvil @ 2011-12-21 22:01 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: gdb-patches

On Wed, 21 Dec 2011 22:26:02 +0100, Mark Kettenis wrote:
> And
> inf-ptrace.c *BSD really is the primary user of inf-ptrace.c anyway.
> The various Linux targets only need it to support ancient versions of
> the Linux kernels; linux-nat.c ovverrides everything except for
> to_fetch_registers and to_store_registers.

linux-nat.c is still heavily dependent on inf-ptrace.c through all its
linux_ops->to_* calls therein.


Regards,
Jan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-21 21:27   ` Mark Kettenis
  2011-12-21 22:01     ` Jan Kratochvil
@ 2011-12-22  2:34     ` Stan Shebs
  2011-12-22 10:25       ` Mark Kettenis
  1 sibling, 1 reply; 10+ messages in thread
From: Stan Shebs @ 2011-12-22  2:34 UTC (permalink / raw)
  To: gdb-patches

On 12/21/11 1:26 PM, Mark Kettenis wrote:
>> Date: Wed, 21 Dec 2011 18:13:15 +0100
>> From: Jan Kratochvil<jan.kratochvil@redhat.com>
>>
>> On Sat, 17 Dec 2011 22:08:29 +0100, Mark Kettenis wrote:
>>> For now this is OpenBSD-specific, but FreeBSD and NetBSD might
>>> implement the PIOD_READ_AUXV request at some point too.
>> [...]
>>> 	* inf-ptrace.c [PT_IO&&  PIOD_READ_AUXV]
>> So why didn't you put it into *bsd*-nat.c files?
> There is already PT_IO support code in inf-ptrace.c.  It makes sense
> to keep it all together.  I guess I could move all that code into a
> seperate bsd-nat.c file, but that's quite a big undertaking.  And
> inf-ptrace.c *BSD really is the primary user of inf-ptrace.c anyway.
> The various Linux targets only need it to support ancient versions of
> the Linux kernels; linux-nat.c ovverrides everything except for
> to_fetch_registers and to_store_registers.  And those are overridden
> by most, if not all, *-linux-nat.c files.
>

I wonder if inf-ptrace.c could be retired altogether.  It was always 
based on a weak assumption, that Unix variants would tend to have the 
same syntax and semantics for the various ptrace commands, and I suspect 
that more of its code is unreachable than is obvious from inspection, 
what with configs overriding or on the verge of being retired themselves.

Stan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-22  2:34     ` Stan Shebs
@ 2011-12-22 10:25       ` Mark Kettenis
  2011-12-22 19:40         ` Stan Shebs
  0 siblings, 1 reply; 10+ messages in thread
From: Mark Kettenis @ 2011-12-22 10:25 UTC (permalink / raw)
  To: stanshebs; +Cc: gdb-patches

> Date: Wed, 21 Dec 2011 15:39:04 -0800
> From: Stan Shebs <stanshebs@earthlink.net>
> 
> On 12/21/11 1:26 PM, Mark Kettenis wrote:
> >> Date: Wed, 21 Dec 2011 18:13:15 +0100
> >> From: Jan Kratochvil<jan.kratochvil@redhat.com>
> >>
> >> On Sat, 17 Dec 2011 22:08:29 +0100, Mark Kettenis wrote:
> >>> For now this is OpenBSD-specific, but FreeBSD and NetBSD might
> >>> implement the PIOD_READ_AUXV request at some point too.
> >> [...]
> >>> 	* inf-ptrace.c [PT_IO&&  PIOD_READ_AUXV]
> >> So why didn't you put it into *bsd*-nat.c files?
> > There is already PT_IO support code in inf-ptrace.c.  It makes sense
> > to keep it all together.  I guess I could move all that code into a
> > seperate bsd-nat.c file, but that's quite a big undertaking.  And
> > inf-ptrace.c *BSD really is the primary user of inf-ptrace.c anyway.
> > The various Linux targets only need it to support ancient versions of
> > the Linux kernels; linux-nat.c ovverrides everything except for
> > to_fetch_registers and to_store_registers.  And those are overridden
> > by most, if not all, *-linux-nat.c files.
> >
> 
> I wonder if inf-ptrace.c could be retired altogether.  It was always 
> based on a weak assumption, that Unix variants would tend to have the 
> same syntax and semantics for the various ptrace commands, and I suspect 
> that more of its code is unreachable than is obvious from inspection, 
> what with configs overriding or on the verge of being retired themselves.

Almost all of the code is used on OpenBSD and the other BSDs.  It is
certainly true that systems have diverged.  This is especially true
for Linux where we have a lot of support code for dealing with threads
that sits on top of the code in inf-ptrace.c.  It still uses a fair
chunk of the code through linux_ops->to_create_inferior, to_attach,
to_detach and to_stop, to_resume and to_mourn_inferior, as pointed out
by Jan.  But I currently do see inf-ptrace.c primarily as BSD support
code and further decoupling the Linux code might make some sense.  But
you'd need to duplicate a fair amount of code to fully decouple
to_create_inferior for example.

Back to my origional diff.  Any remaining objections to committing it as is?

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-22 10:25       ` Mark Kettenis
@ 2011-12-22 19:40         ` Stan Shebs
  2011-12-27 22:03           ` Mark Kettenis
  0 siblings, 1 reply; 10+ messages in thread
From: Stan Shebs @ 2011-12-22 19:40 UTC (permalink / raw)
  To: gdb-patches

On 12/22/11 2:20 AM, Mark Kettenis wrote:
>> Date: Wed, 21 Dec 2011 15:39:04 -0800
>> From: Stan Shebs<stanshebs@earthlink.net>
>>
>> On 12/21/11 1:26 PM, Mark Kettenis wrote:
>>>> Date: Wed, 21 Dec 2011 18:13:15 +0100
>>>> From: Jan Kratochvil<jan.kratochvil@redhat.com>
>>>>
>>>> On Sat, 17 Dec 2011 22:08:29 +0100, Mark Kettenis wrote:
>>>>> For now this is OpenBSD-specific, but FreeBSD and NetBSD might
>>>>> implement the PIOD_READ_AUXV request at some point too.
>>>> [...]
>>>>> 	* inf-ptrace.c [PT_IO&&   PIOD_READ_AUXV]
>>>> So why didn't you put it into *bsd*-nat.c files?
>>> There is already PT_IO support code in inf-ptrace.c.  It makes sense
>>> to keep it all together.  I guess I could move all that code into a
>>> seperate bsd-nat.c file, but that's quite a big undertaking.  And
>>> inf-ptrace.c *BSD really is the primary user of inf-ptrace.c anyway.
>>> The various Linux targets only need it to support ancient versions of
>>> the Linux kernels; linux-nat.c ovverrides everything except for
>>> to_fetch_registers and to_store_registers.  And those are overridden
>>> by most, if not all, *-linux-nat.c files.
>>>
>> I wonder if inf-ptrace.c could be retired altogether.  It was always
>> based on a weak assumption, that Unix variants would tend to have the
>> same syntax and semantics for the various ptrace commands, and I suspect
>> that more of its code is unreachable than is obvious from inspection,
>> what with configs overriding or on the verge of being retired themselves.
> Almost all of the code is used on OpenBSD and the other BSDs.  It is
> certainly true that systems have diverged.  This is especially true
> for Linux where we have a lot of support code for dealing with threads
> that sits on top of the code in inf-ptrace.c.  It still uses a fair
> chunk of the code through linux_ops->to_create_inferior, to_attach,
> to_detach and to_stop, to_resume and to_mourn_inferior, as pointed out
> by Jan.  But I currently do see inf-ptrace.c primarily as BSD support
> code and further decoupling the Linux code might make some sense.  But
> you'd need to duplicate a fair amount of code to fully decouple
> to_create_inferior for example.
>
> Back to my origional diff.  Any remaining objections to committing it as is?
>

Looks fine to me.  It would be helpful to mention somewhere in the 
vicinity of the #if's that the code is BSD-specific or BSDish, so as to 
forestall people hunting around in other OS headers wondering if those 
macros are defined or not.

Stan

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-22 19:40         ` Stan Shebs
@ 2011-12-27 22:03           ` Mark Kettenis
  2012-01-02  3:58             ` Yao Qi
  0 siblings, 1 reply; 10+ messages in thread
From: Mark Kettenis @ 2011-12-27 22:03 UTC (permalink / raw)
  To: stanshebs; +Cc: gdb-patches

> Date: Thu, 22 Dec 2011 11:25:16 -0800
> From: Stan Shebs <stanshebs@earthlink.net>
> 
> Looks fine to me.  It would be helpful to mention somewhere in the 
> vicinity of the #if's that the code is BSD-specific or BSDish, so as to 
> forestall people hunting around in other OS headers wondering if those 
> macros are defined or not.

Fair enough.  The diff below is what I committed.


2011-12-27  Mark Kettenis  <kettenis@gnu.org>

	* inf-ptrace.c [PT_IO && PIOD_READ_AUXV]
	(inf_ptrace_xfer_partial): Implement TARGET_OBJECT_AUXV.
	(inf_ptrace_auxv_parse): New function.
	(inf_ptrace_target): Initialize to_auxv_parse field.

Index: inf-ptrace.c
===================================================================
RCS file: /cvs/src/src/gdb/inf-ptrace.c,v
retrieving revision 1.75
diff -u -p -r1.75 inf-ptrace.c
--- inf-ptrace.c	22 Sep 2011 10:22:28 -0000	1.75
+++ inf-ptrace.c	27 Dec 2011 21:35:05 -0000
@@ -582,6 +582,26 @@ inf_ptrace_xfer_partial (struct target_o
       return -1;
 
     case TARGET_OBJECT_AUXV:
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+      /* OpenBSD 4.5 has a new PIOD_READ_AUXV operation for the PT_IO
+	 request that allows us to read the auxilliary vector.  Other
+	 BSD's may follow if they feel the need to support PIE.  */
+      {
+	struct ptrace_io_desc piod;
+
+	if (writebuf)
+		return -1;
+	piod.piod_op = PIOD_READ_AUXV;
+	piod.piod_addr = readbuf;
+	piod.piod_offs = (void *) (long) offset;
+	piod.piod_len = len;
+
+	errno = 0;
+	if (ptrace (PT_IO, pid, (caddr_t)&piod, 0) == 0)
+	  /* Return the actual number of bytes read or written.  */
+	  return piod.piod_len;
+      }
+#endif
       return -1;
 
     case TARGET_OBJECT_WCOOKIE:
@@ -619,6 +639,41 @@ inf_ptrace_pid_to_str (struct target_ops
   return normal_pid_to_str (ptid);
 }
 
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+
+/* Read one auxv entry from *READPTR, not reading locations >= ENDPTR.
+   Return 0 if *READPTR is already at the end of the buffer.
+   Return -1 if there is insufficient buffer for a whole entry.
+   Return 1 if an entry was read into *TYPEP and *VALP.  */
+
+static int
+inf_ptrace_auxv_parse (struct target_ops *ops, gdb_byte **readptr,
+		       gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
+{
+  struct type *int_type = builtin_type (target_gdbarch)->builtin_int;
+  struct type *ptr_type = builtin_type (target_gdbarch)->builtin_data_ptr;
+  const int sizeof_auxv_type = TYPE_LENGTH (int_type);
+  const int sizeof_auxv_val = TYPE_LENGTH (ptr_type);
+  enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch);
+  gdb_byte *ptr = *readptr;
+
+  if (endptr == ptr)
+    return 0;
+
+  if (endptr - ptr < 2 * sizeof_auxv_val)
+    return -1;
+
+  *typep = extract_unsigned_integer (ptr, sizeof_auxv_type, byte_order);
+  ptr += sizeof_auxv_val;	/* Alignment.  */
+  *valp = extract_unsigned_integer (ptr, sizeof_auxv_val, byte_order);
+  ptr += sizeof_auxv_val;
+
+  *readptr = ptr;
+  return 1;
+}
+
+#endif
+
 /* Create a prototype ptrace target.  The client can override it with
    local methods.  */
 
@@ -644,6 +699,9 @@ inf_ptrace_target (void)
   t->to_pid_to_str = inf_ptrace_pid_to_str;
   t->to_stop = inf_ptrace_stop;
   t->to_xfer_partial = inf_ptrace_xfer_partial;
+#if defined (PT_IO) && defined (PIOD_READ_AUXV)
+  t->to_auxv_parse = inf_ptrace_auxv_parse;
+#endif
 
   return t;
 }

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2011-12-27 22:03           ` Mark Kettenis
@ 2012-01-02  3:58             ` Yao Qi
  2012-01-02  9:05               ` Mark Kettenis
  0 siblings, 1 reply; 10+ messages in thread
From: Yao Qi @ 2012-01-02  3:58 UTC (permalink / raw)
  To: Mark Kettenis; +Cc: stanshebs, gdb-patches

On 12/28/2011 05:38 AM, Mark Kettenis wrote:
> +	if (writebuf)
> +		return -1;

The indentation looks wrong.  Fixed it as obvious.

-- 
Yao (齐尧)

2012-01-02  Yao Qi  <yao@codesourcery.com>

	* inf-ptrace.c (inf_ptrace_xfer_partial): Reindent.

Index: inf-ptrace.c
===================================================================
RCS file: /cvs/src/src/gdb/inf-ptrace.c,v
retrieving revision 1.76
diff -u -r1.76 inf-ptrace.c
--- inf-ptrace.c	27 Dec 2011 21:36:40 -0000	1.76
+++ inf-ptrace.c	2 Jan 2012 03:55:43 -0000
@@ -590,7 +590,7 @@
 	struct ptrace_io_desc piod;

 	if (writebuf)
-		return -1;
+	  return -1;
 	piod.piod_op = PIOD_READ_AUXV;
 	piod.piod_addr = readbuf;
 	piod.piod_offs = (void *) (long) offset;

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH] PIE support for OpenBSD
  2012-01-02  3:58             ` Yao Qi
@ 2012-01-02  9:05               ` Mark Kettenis
  0 siblings, 0 replies; 10+ messages in thread
From: Mark Kettenis @ 2012-01-02  9:05 UTC (permalink / raw)
  To: yao; +Cc: gdb-patches

> Date: Mon, 2 Jan 2012 11:58:34 +0800
> From: Yao Qi <yao@codesourcery.com>
> 
> On 12/28/2011 05:38 AM, Mark Kettenis wrote:
> > +	if (writebuf)
> > +		return -1;
> 
> The indentation looks wrong.  Fixed it as obvious.

Oops, sorry 'bout that.  Thanks for fixing this.

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2012-01-02  9:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-18  1:14 [PATCH] PIE support for OpenBSD Mark Kettenis
2011-12-21 17:16 ` Jan Kratochvil
2011-12-21 21:27   ` Mark Kettenis
2011-12-21 22:01     ` Jan Kratochvil
2011-12-22  2:34     ` Stan Shebs
2011-12-22 10:25       ` Mark Kettenis
2011-12-22 19:40         ` Stan Shebs
2011-12-27 22:03           ` Mark Kettenis
2012-01-02  3:58             ` Yao Qi
2012-01-02  9:05               ` Mark Kettenis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).