[PATCH] bfd: support for NT_386_TLS notes

Andrew Burgess aburgess@redhat.com
Thu Aug 14 15:12:13 GMT 2025


Nick Clifton <nickc@redhat.com> writes:

> Hi Andrew,
>
>> The only part of this patch which I think needs consideration is the
>> name I selected for the pseudo section to hold the note contents when
>> a core file is loaded.  I chose '.reg-i386-tls'.  The '.reg' prefix is
>> the standard used by most other pseudo sections, and the '-i386-tls'
>> suffix seemed to match the note name, though I added the 'i' to
>> 'i386', instead of just using '.reg-386-tls'.  I thought 'i386' seemed
>> clearer.
>
> The name is fine, as it is the patch itself - please go ahead an
> apply.

Thanks for the review.

>
> I do have a couple of suggestions for code improvement however, which
> you might like to consider before committing:
>
>> +static bool
>> +elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
>> +{
>> +  return elfcore_make_note_pseudosection (abfd, ".reg-i386-tls", note);
>> +}
>
> The ".reg-i386-tls" string is used several times.  It might be better
> to replace this with a #define I386_TLS_SECTION_NAME (or some such) and
> use that for consistency and ease of updating, should the section name
> change.

I used PSEUDO_SECTION_NAME_I386_TLS.  If the other pseudo sections are
also updated to use #defines then the names can all start with a common
PSEUDO_SECTION_NAME_ prefix then.

>
>
>> +	  && strcmp (note->namedata, "LINUX") == 0)
>
> A similar comment could be made about the "LINUX" string, although
> if you do make such a change there will be a lot more places that need
> updating.

I didn't change this one, as you say, this is really a wider clean up,
so I left that for another day.

>
>
>> +char *
>> +elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
>> +			    const void *regs, int size)
>> +{
>> +  char *note_name = "LINUX";
>> +  return elfcore_write_note (abfd, buf, bufsiz,
>> +			     note_name, NT_386_TLS, regs, size);
>> +}
>
> Do you really need to define the note_name local here ?  You could
> just use the "LINUX" string in the call to elfcore_write_note().

No, this was just copy&paste.  I moved the string into the function call
line.

>
>
>> @@ -13287,6 +13309,8 @@ elfcore_write_register_note (bfd *abfd,
>>       return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
>>     if (strcmp (section, ".reg-loongarch-lasx") == 0)
>>       return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
>> +  if (strcmp (section, ".reg-i386-tls") == 0)
>> +    return elfcore_write_i386_tls (abfd, buf, bufsiz, data, size);
>>     return NULL;
>>   }
>
> I have always felt that 'strcmp() == 0' is a very confusing way to check
> for string equality.  I prefer defining a macro "streq()" and using that.
> This is just a personal preference however and again making this change
> would mean updating a lot more code in the elf.c file.

I left this one too.  As you point out, this is really a wider clean up.

The patch I pushed is below.

Thanks,
Andrew

--

commit ea6ec00ff4520895735e4913cb90c933c7296f04
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Fri Jul 25 19:51:58 2025 +0100

    bfd: support for NT_386_TLS notes
    
    In a later commit I'd like to add support to GDB for including the
    NT_386_TLS note in the core files that GDB creates (using 'gcore'
    command).
    
    To achieve this we need some standard boilerplate code added to bfd.
    
    The only part of this patch which I think needs consideration is the
    name I selected for the pseudo section to hold the note contents when
    a core file is loaded.  I chose '.reg-i386-tls'.  The '.reg' prefix is
    the standard used by most other pseudo sections, and the '-i386-tls'
    suffix seemed to match the note name, though I added the 'i' to
    'i386', instead of just using '.reg-386-tls'.  I thought 'i386' seemed
    clearer.
    
    There's no test included here, but when I merge the NT_386_TLS
    creation to GDB it will depend on this and act as a test.  I plan to
    post that work to the GDB list once this patch is merged.

diff --git a/bfd/elf-bfd.h b/bfd/elf-bfd.h
index f24342cb029..accdd6d41a8 100644
--- a/bfd/elf-bfd.h
+++ b/bfd/elf-bfd.h
@@ -2927,6 +2927,8 @@ 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_i386_tls
+  (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 1b2e331eaa8..4f6bcde2365 100644
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -49,6 +49,10 @@ SECTION
 #include CORE_HEADER
 #endif
 
+/* Name of a pseudo-section which represents NT_386_TLS notes within a core
+   file.  */
+#define PSEUDO_SECTION_NAME_I386_TLS ".reg-i386-tls"
+
 static int elf_sort_sections (const void *, const void *);
 static bool assign_file_positions_except_relocs (bfd *, struct bfd_link_info *);
 static bool swap_out_syms (bfd *, struct elf_strtab_hash **, int,
@@ -10673,6 +10677,13 @@ elfcore_grok_gdb_tdesc (bfd *abfd, Elf_Internal_Note *note)
   return elfcore_make_note_pseudosection (abfd, ".gdb-tdesc", note);
 }
 
+static bool
+elfcore_grok_i386_tls (bfd *abfd, Elf_Internal_Note *note)
+{
+  return elfcore_make_note_pseudosection (abfd, PSEUDO_SECTION_NAME_I386_TLS,
+					  note);
+}
+
 static bool
 elfcore_grok_loongarch_cpucfg (bfd *abfd, Elf_Internal_Note *note)
 {
@@ -11336,6 +11347,13 @@ elfcore_grok_note (bfd *abfd, Elf_Internal_Note *note)
       else
 	return true;
 
+    case NT_386_TLS:
+      if (note->namesz == 6
+	  && strcmp (note->namedata, "LINUX") == 0)
+	return elfcore_grok_i386_tls (abfd, note);
+      else
+	return true;
+
     case NT_ARM_HW_BREAK:
       if (note->namesz == 6
 	  && strcmp (note->namedata, "LINUX") == 0)
@@ -12593,6 +12611,14 @@ elfcore_write_x86_segbases (bfd *abfd, char *buf, int *bufsiz,
 			     note_name, NT_FREEBSD_X86_SEGBASES, regs, size);
 }
 
+char *
+elfcore_write_i386_tls (bfd *abfd, char *buf, int *bufsiz,
+			    const void *regs, int size)
+{
+  return elfcore_write_note (abfd, buf, bufsiz, "LINUX", NT_386_TLS,
+			     regs, size);
+}
+
 char *
 elfcore_write_ppc_vmx (bfd *abfd,
 		       char *buf,
@@ -13287,6 +13313,8 @@ elfcore_write_register_note (bfd *abfd,
     return elfcore_write_loongarch_lsx (abfd, buf, bufsiz, data, size);
   if (strcmp (section, ".reg-loongarch-lasx") == 0)
     return elfcore_write_loongarch_lasx (abfd, buf, bufsiz, data, size);
+  if (strcmp (section, PSEUDO_SECTION_NAME_I386_TLS) == 0)
+    return elfcore_write_i386_tls (abfd, buf, bufsiz, data, size);
   return NULL;
 }
 



More information about the Binutils mailing list