public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note
@ 2022-03-24 20:56 John Baldwin
  2022-03-24 20:56 ` [PATCH 1/5] elfcore_grok_freebsd_note: Remove checks of note->namesz John Baldwin
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

This core dump note contains the value of the %fsbase and %gsbase
registers (or on i386 the base address of the respective segments).
This is used to resolve the address of TLS variables in core dumps.

John Baldwin (5):
  elfcore_grok_freebsd_note: Remove checks of note->namesz.
  Recognize FreeBSD core dump note for x86 segment base registers.
  Use pseudosections for NT_FREEBSD_X86_SEGBASES core dump notes.
  FreeBSD/x86: Read segment base registers from NT_X86_SEGBASES.
  Use I386_GSBASE_REGNUM in i386fbsd_get_thread_local_address.

 bfd/ChangeLog         | 13 +++++++++++++
 bfd/elf-bfd.h         |  2 ++
 bfd/elf.c             | 24 ++++++++++++++++--------
 binutils/ChangeLog    |  5 +++++
 binutils/readelf.c    |  2 ++
 gdb/amd64-fbsd-tdep.c | 18 ++++++++++++++++++
 gdb/i386-fbsd-tdep.c  | 25 ++++++++++++++++++++-----
 include/ChangeLog     |  4 ++++
 include/elf/common.h  |  1 +
 9 files changed, 81 insertions(+), 13 deletions(-)

-- 
2.34.1


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

* [PATCH 1/5] elfcore_grok_freebsd_note: Remove checks of note->namesz.
  2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
@ 2022-03-24 20:56 ` John Baldwin
  2022-03-24 20:56 ` [PATCH 2/5] Recognize FreeBSD core dump note for x86 segment base registers John Baldwin
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

This function is only called if the note name is "FreeBSD", so
checking the name size is unnecessary.

bfd/ChangeLog:

	* elf.c (elfcore_grok_freebsd_note): Remove checks for namesz.
---
 bfd/ChangeLog |  4 ++++
 bfd/elf.c     | 10 ++--------
 2 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index 86d42b2d639..d1a31efe59c 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,7 @@
+2022-03-23  John Baldwin  <jhb@FreeBSD.org>
+
+	* elf.c (elfcore_grok_freebsd_note): Remove checks for namesz.
+
 2022-03-22  Steiner H Gunderson  <steinar+sourceware@gunderson.no>
 
 	* dwarf2.c (_bfd_dwarf2_find_nearest_line): if a function name is
diff --git a/bfd/elf.c b/bfd/elf.c
index 82b53be99f9..a99149e50b3 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -11010,10 +11010,7 @@ elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
       return elfcore_grok_freebsd_psinfo (abfd, note);
 
     case NT_FREEBSD_THRMISC:
-      if (note->namesz == 8)
-	return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
-      else
-	return true;
+      return elfcore_make_note_pseudosection (abfd, ".thrmisc", note);
 
     case NT_FREEBSD_PROCSTAT_PROC:
       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.proc",
@@ -11031,10 +11028,7 @@ elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
       return elfcore_make_auxv_note_section (abfd, note, 4);
 
     case NT_X86_XSTATE:
-      if (note->namesz == 8)
-	return elfcore_grok_xstatereg (abfd, note);
-      else
-	return true;
+      return elfcore_grok_xstatereg (abfd, note);
 
     case NT_FREEBSD_PTLWPINFO:
       return elfcore_make_note_pseudosection (abfd, ".note.freebsdcore.lwpinfo",
-- 
2.34.1


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

* [PATCH 2/5] Recognize FreeBSD core dump note for x86 segment base registers.
  2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
  2022-03-24 20:56 ` [PATCH 1/5] elfcore_grok_freebsd_note: Remove checks of note->namesz John Baldwin
@ 2022-03-24 20:56 ` John Baldwin
  2022-03-24 20:56 ` [PATCH 3/5] Use pseudosections for NT_FREEBSD_X86_SEGBASES core dump notes John Baldwin
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

This core dump note contains the value of the base address of the %fs
and %gs segments for both i386 and amd64 core dumps.  It is primarily
useful in resolving the address of TLS variables in core dumps.

binutils/ChangeLog:

	* readelf.c (get_freebsd_elfcore_note_type): Handle
	NT_FREEBSD_X86_SEGBASES.

include/ChangeLog:

	* elf/common.h (NT_FREEBSD_X86_SEGBASES): Define.
---
 binutils/ChangeLog   | 5 +++++
 binutils/readelf.c   | 2 ++
 include/ChangeLog    | 4 ++++
 include/elf/common.h | 1 +
 4 files changed, 12 insertions(+)

diff --git a/binutils/ChangeLog b/binutils/ChangeLog
index f84323953d6..b9df76e9e56 100644
--- a/binutils/ChangeLog
+++ b/binutils/ChangeLog
@@ -1,3 +1,8 @@
+2022-03-23  John Baldwin  <jhb@FreeBSD.org>
+
+	* readelf.c (get_freebsd_elfcore_note_type): Handle
+	NT_FREEBSD_X86_SEGBASES.
+
 2022-03-16  Fangrui Song  <maskray@google.com>
 
 	PR binutils/28926
diff --git a/binutils/readelf.c b/binutils/readelf.c
index a2dbaa4722e..84c8cfd45fc 100644
--- a/binutils/readelf.c
+++ b/binutils/readelf.c
@@ -20448,6 +20448,8 @@ get_freebsd_elfcore_note_type (Filedata * filedata, unsigned e_type)
       return _("NT_PROCSTAT_AUXV (auxv data)");
     case NT_FREEBSD_PTLWPINFO:
       return _("NT_PTLWPINFO (ptrace_lwpinfo structure)");
+    case NT_FREEBSD_X86_SEGBASES:
+      return _("NT_X86_SEGBASES (x86 segment base registers)");
     }
   return get_note_type (filedata, e_type);
 }
diff --git a/include/ChangeLog b/include/ChangeLog
index 82194629c97..05d95e623b6 100644
--- a/include/ChangeLog
+++ b/include/ChangeLog
@@ -1,3 +1,7 @@
+2022-03-23  John Baldwin  <jhb@FreeBSD.org>
+
+	* elf/common.h (NT_FREEBSD_X86_SEGBASES): Define.
+
 2022-03-16  Simon Marchi  <simon.marchi@efficios.com>
 
 	* elf/amdgpu.h: Add relocation values.
diff --git a/include/elf/common.h b/include/elf/common.h
index 70d63e3299c..ad62a7d8523 100644
--- a/include/elf/common.h
+++ b/include/elf/common.h
@@ -738,6 +738,7 @@
 #define	NT_FREEBSD_PROCSTAT_PSSTRINGS	15	/* Procstat ps_strings data. */
 #define	NT_FREEBSD_PROCSTAT_AUXV	16	/* Procstat auxv data. */
 #define	NT_FREEBSD_PTLWPINFO	17	/* Thread ptrace miscellaneous info. */
+#define	NT_FREEBSD_X86_SEGBASES	0x200	/* x86 segment base registers */
 
 /* Note segments for core files on NetBSD systems.  Note name
    must start with "NetBSD-CORE".  */
-- 
2.34.1


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

* [PATCH 3/5] Use pseudosections for NT_FREEBSD_X86_SEGBASES core dump notes.
  2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
  2022-03-24 20:56 ` [PATCH 1/5] elfcore_grok_freebsd_note: Remove checks of note->namesz John Baldwin
  2022-03-24 20:56 ` [PATCH 2/5] Recognize FreeBSD core dump note for x86 segment base registers John Baldwin
@ 2022-03-24 20:56 ` John Baldwin
  2022-03-24 20:56 ` [PATCH 4/5] FreeBSD/x86: Read segment base registers from NT_X86_SEGBASES John Baldwin
  2022-03-24 20:56 ` [PATCH 5/5] Use I386_GSBASE_REGNUM in i386fbsd_get_thread_local_address John Baldwin
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

This includes adding pseudosections when reading a core dump as well
as support for writing out a core dump note from a pseudosection.

bfd/ChangeLog:

	* elf-bfd.h (elfcore_write_x86_segbases): New.
	* elf.c (elfcore_grok_freebsd_note): Add pseudosections for
	NT_FREEBSD_X86_SEGBASES register notes.
	(elfcore_write_x86_segbases): New.
	(elfcore_write_register_note): Write NT_FREEBSD_X86_SEGBASES
	register notes.
---
 bfd/ChangeLog |  9 +++++++++
 bfd/elf-bfd.h |  2 ++
 bfd/elf.c     | 14 ++++++++++++++
 3 files changed, 25 insertions(+)

diff --git a/bfd/ChangeLog b/bfd/ChangeLog
index d1a31efe59c..9085d3a7664 100644
--- a/bfd/ChangeLog
+++ b/bfd/ChangeLog
@@ -1,3 +1,12 @@
+2022-03-23  John Baldwin  <jhb@FreeBSD.org>
+
+	* elf-bfd.h (elfcore_write_x86_segbases): New.
+	* elf.c (elfcore_grok_freebsd_note): Add pseudosections for
+	NT_FREEBSD_X86_SEGBASES register notes.
+	(elfcore_write_x86_segbases): New.
+	(elfcore_write_register_note): Write NT_FREEBSD_X86_SEGBASES
+	register notes.
+
 2022-03-23  John Baldwin  <jhb@FreeBSD.org>
 
 	* elf.c (elfcore_grok_freebsd_note): Remove checks for namesz.
diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index 5c3985f6e57..c7c0a793b15 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2786,6 +2786,8 @@ extern char *elfcore_write_prxfpreg
   (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_xstatereg
   (bfd *, char *, int *, const void *, int);
+extern char *elfcore_write_x86_segbases
+  (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_ppc_vmx
   (bfd *, char *, int *, const void *, int);
 extern char *elfcore_write_ppc_vsx
diff --git a/bfd/elf.c b/bfd/elf.c
index a99149e50b3..37c53cfdf32 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -11027,6 +11027,9 @@ elfcore_grok_freebsd_note (bfd *abfd, Elf_Internal_Note *note)
     case NT_FREEBSD_PROCSTAT_AUXV:
       return elfcore_make_auxv_note_section (abfd, note, 4);
 
+    case NT_FREEBSD_X86_SEGBASES:
+      return elfcore_make_note_pseudosection (abfd, ".reg-x86-segbases", note);
+
     case NT_X86_XSTATE:
       return elfcore_grok_xstatereg (abfd, note);
 
@@ -11904,6 +11907,15 @@ elfcore_write_xstatereg (bfd *abfd, char *buf, int *bufsiz,
 			     note_name, NT_X86_XSTATE, xfpregs, size);
 }
 
+char *
+elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
+			    const void *regs, int size)
+{
+  char *note_name = "FreeBSD";
+  return elfcore_write_note (abfd, buf, bufsiz,
+			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
+}
+
 char *
 elfcore_write_ppc_vmx (bfd *abfd,
 		       char *buf,
@@ -12441,6 +12453,8 @@ elfcore_write_register_note (bfd *abfd,
     return elfcore_write_prxfpreg (abfd, buf, bufsiz, data, size);
   if (strcmp (section, ".reg-xstate") == 0)
     return elfcore_write_xstatereg (abfd, buf, bufsiz, data, size);
+  if (strcmp (section, ".reg-x86-segbases") == 0)
+    return elfcore_write_x86_segbases (abfd, buf, bufsiz, data, size);
   if (strcmp (section, ".reg-ppc-vmx") == 0)
     return elfcore_write_ppc_vmx (abfd, buf, bufsiz, data, size);
   if (strcmp (section, ".reg-ppc-vsx") == 0)
-- 
2.34.1


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

* [PATCH 4/5] FreeBSD/x86: Read segment base registers from NT_X86_SEGBASES.
  2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
                   ` (2 preceding siblings ...)
  2022-03-24 20:56 ` [PATCH 3/5] Use pseudosections for NT_FREEBSD_X86_SEGBASES core dump notes John Baldwin
@ 2022-03-24 20:56 ` John Baldwin
  2022-03-24 20:56 ` [PATCH 5/5] Use I386_GSBASE_REGNUM in i386fbsd_get_thread_local_address John Baldwin
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

FreeBSD kernels recently grew a new register core dump note containing
the base addresses of the %fs and %gs segments (corresponding to the
%fsbase and %gsbase registers).  Parse this note to permit inspecting
TLS variables in core dumps.  Native processes already supported TLS
via older ptrace() operations.
---
 gdb/amd64-fbsd-tdep.c | 18 ++++++++++++++++++
 gdb/i386-fbsd-tdep.c  | 18 ++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/gdb/amd64-fbsd-tdep.c b/gdb/amd64-fbsd-tdep.c
index da5c297902d..55764beaad2 100644
--- a/gdb/amd64-fbsd-tdep.c
+++ b/gdb/amd64-fbsd-tdep.c
@@ -37,6 +37,9 @@
    16-bit segment registers.  */
 #define AMD64_FBSD_SIZEOF_GREGSET	(22 * 8)
 
+/* The segment base register set consists of 2 64-bit registers.  */
+#define AMD64_FBSD_SIZEOF_SEGBASES_REGSET	(2 * 8)
+
 /* Register maps.  */
 
 static const struct regcache_map_entry amd64_fbsd_gregmap[] =
@@ -70,6 +73,13 @@ static const struct regcache_map_entry amd64_fbsd_gregmap[] =
   { 0 }
 };
 
+static const struct regcache_map_entry amd64_fbsd_segbases_regmap[] =
+{
+  { 1, AMD64_FSBASE_REGNUM, 0 },
+  { 1, AMD64_GSBASE_REGNUM, 0 },
+  { 0 }
+};
+
 /* This layout including fsbase and gsbase was adopted in FreeBSD
    8.0.  */
 
@@ -120,6 +130,11 @@ const struct regset amd64_fbsd_gregset =
   amd64_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
 };
 
+const struct regset amd64_fbsd_segbases_regset =
+{
+  amd64_fbsd_segbases_regmap, regcache_supply_regset, regcache_collect_regset
+};
+
 /* Support for signal handlers.  */
 
 /* In a signal frame, rsp points to a 'struct sigframe' which is
@@ -253,6 +268,9 @@ amd64fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
       &amd64_fbsd_gregset, NULL, cb_data);
   cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &amd64_fpregset,
       NULL, cb_data);
+  cb (".reg-x86-segbases", AMD64_FBSD_SIZEOF_SEGBASES_REGSET,
+      AMD64_FBSD_SIZEOF_SEGBASES_REGSET, &amd64_fbsd_segbases_regset,
+      "segment bases", cb_data);
   cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0), X86_XSTATE_SIZE (tdep->xcr0),
       &amd64fbsd_xstateregset, "XSAVE extended state", cb_data);
 }
diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index 16ffd576323..fad091f8472 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -35,6 +35,9 @@
 /* The general-purpose regset consists of 19 32-bit slots.  */
 #define I386_FBSD_SIZEOF_GREGSET	(19 * 4)
 
+/* The segment base register set consists of 2 32-bit registers.  */
+#define I386_FBSD_SIZEOF_SEGBASES_REGSET	(2 * 4)
+
 /* Register maps.  */
 
 static const struct regcache_map_entry i386_fbsd_gregmap[] =
@@ -61,6 +64,13 @@ static const struct regcache_map_entry i386_fbsd_gregmap[] =
   { 0 }
 };
 
+static const struct regcache_map_entry i386_fbsd_segbases_regmap[] =
+{
+  { 1, I386_FSBASE_REGNUM, 0 },
+  { 1, I386_GSBASE_REGNUM, 0 },
+  { 0 }
+};
+
 /* This layout including fsbase and gsbase was adopted in FreeBSD
    8.0.  */
 
@@ -103,6 +113,11 @@ const struct regset i386_fbsd_gregset =
   i386_fbsd_gregmap, regcache_supply_regset, regcache_collect_regset
 };
 
+const struct regset i386_fbsd_segbases_regset =
+{
+  i386_fbsd_segbases_regmap, regcache_supply_regset, regcache_collect_regset
+};
+
 /* Support for signal handlers.  */
 
 /* In a signal frame, esp points to a 'struct sigframe' which is
@@ -316,6 +331,9 @@ i386fbsd_iterate_over_regset_sections (struct gdbarch *gdbarch,
       &i386_fbsd_gregset, NULL, cb_data);
   cb (".reg2", tdep->sizeof_fpregset, tdep->sizeof_fpregset, &i386_fpregset,
       NULL, cb_data);
+  cb (".reg-x86-segbases", I386_FBSD_SIZEOF_SEGBASES_REGSET,
+      I386_FBSD_SIZEOF_SEGBASES_REGSET, &i386_fbsd_segbases_regset,
+      "segment bases", cb_data);
 
   if (tdep->xcr0 & X86_XSTATE_AVX)
     cb (".reg-xstate", X86_XSTATE_SIZE (tdep->xcr0),
-- 
2.34.1


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

* [PATCH 5/5] Use I386_GSBASE_REGNUM in i386fbsd_get_thread_local_address.
  2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
                   ` (3 preceding siblings ...)
  2022-03-24 20:56 ` [PATCH 4/5] FreeBSD/x86: Read segment base registers from NT_X86_SEGBASES John Baldwin
@ 2022-03-24 20:56 ` John Baldwin
  4 siblings, 0 replies; 6+ messages in thread
From: John Baldwin @ 2022-03-24 20:56 UTC (permalink / raw)
  To: binutils, gdb-patches

32-bit x86 arches always the I386_*BASE_REGNUM values.  Only code that
needs to support both 64-bit and 32-bit arches needs to use
tdep->fsbase_regnum to compute a segment base register number.
---
 gdb/i386-fbsd-tdep.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/gdb/i386-fbsd-tdep.c b/gdb/i386-fbsd-tdep.c
index fad091f8472..d50f35707ee 100644
--- a/gdb/i386-fbsd-tdep.c
+++ b/gdb/i386-fbsd-tdep.c
@@ -350,16 +350,13 @@ i386fbsd_get_thread_local_address (struct gdbarch *gdbarch, ptid_t ptid,
   i386_gdbarch_tdep *tdep = (i386_gdbarch_tdep *) gdbarch_tdep (gdbarch);
   struct regcache *regcache;
 
-  if (tdep->fsbase_regnum == -1)
-    error (_("Unable to fetch %%gsbase"));
-
   regcache = get_thread_arch_regcache (current_inferior ()->process_target (),
 				       ptid, gdbarch);
 
-  target_fetch_registers (regcache, tdep->fsbase_regnum + 1);
+  target_fetch_registers (regcache, I386_GSBASE_REGNUM);
 
   ULONGEST gsbase;
-  if (regcache->cooked_read (tdep->fsbase_regnum + 1, &gsbase) != REG_VALID)
+  if (regcache->cooked_read (I386_GSBASE_REGNUM, &gsbase) != REG_VALID)
     error (_("Unable to fetch %%gsbase"));
 
   CORE_ADDR dtv_addr = gsbase + gdbarch_ptr_bit (gdbarch) / 8;
-- 
2.34.1


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

end of thread, other threads:[~2022-03-24 20:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 20:56 [PATCH 0/5] Handle FreeBSD's NT_X86_SEGBASES core dump note John Baldwin
2022-03-24 20:56 ` [PATCH 1/5] elfcore_grok_freebsd_note: Remove checks of note->namesz John Baldwin
2022-03-24 20:56 ` [PATCH 2/5] Recognize FreeBSD core dump note for x86 segment base registers John Baldwin
2022-03-24 20:56 ` [PATCH 3/5] Use pseudosections for NT_FREEBSD_X86_SEGBASES core dump notes John Baldwin
2022-03-24 20:56 ` [PATCH 4/5] FreeBSD/x86: Read segment base registers from NT_X86_SEGBASES John Baldwin
2022-03-24 20:56 ` [PATCH 5/5] Use I386_GSBASE_REGNUM in i386fbsd_get_thread_local_address John Baldwin

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).