[PATCH v4 07/14] BFD: Add BFD pointer member to `struct carsym'

Maciej W. Rozycki macro@orcam.me.uk
Tue Jan 13 01:44:50 GMT 2026


From: Maciej W. Rozycki <macro@redhat.com>

Replace the file offset with a union of one and a BFD pointer in `struct 
carsym', as a preparation to handle symbol maps built on the fly rather 
than fetched from a file.  No functional change.

NB there's a note on `struct carsym' being a type pun on `struct symdef'
as in include/aout/ranlib.h (or `struct ranlib'; no idea where it comes
from).  It is possibly not true anymore, even more so with this code
update in place, depending on the underlying type of `file_ptr'.  This
is not a problem however, because we always process `struct carsym' data
by hand and only use it internally rather than reading/writing from/to a
file.  Therefore remove the note, as no longer applicable.
---
This has been previously approved, but relies on 06/14.

No change from v3 (08/18),
<https://inbox.sourceware.org/binutils/alpine.DEB.2.21.2511192128420.57987@angie.orcam.me.uk/>.

No change from v2 (07/16), 
<https://inbox.sourceware.org/binutils/alpine.DEB.2.21.2511060337360.25436@angie.orcam.me.uk/>.

Changes from v1 (4/8), 
<https://inbox.sourceware.org/binutils/alpine.DEB.2.21.2509261803560.63399@angie.orcam.me.uk/>:

- Update to use `ufile_ptr' rather than `file_ptr' for the file offset.

- Reword description per feedback received to make it clear the type pun 
  referred may not have been true even without this change applied.

- Regenerate for coff-alpha.c reformatting.
---
 bfd/archive.c       |   17 ++++++++++++-----
 bfd/archive64.c     |    2 +-
 bfd/bfd-in2.h       |   11 +++++++++--
 bfd/coff-alpha.c    |    2 +-
 bfd/coff-rs6000.c   |    4 ++--
 bfd/coff64-rs6000.c |    2 +-
 bfd/ecoff.c         |    2 +-
 bfd/elflink.c       |   10 +++++-----
 bfd/linker.c        |    8 ++++----
 bfd/som.c           |    4 ++--
 bfd/vms-lib.c       |    8 ++++----
 11 files changed, 42 insertions(+), 28 deletions(-)

binutils-bfd-carsym-bfd.diff
Index: binutils-gdb/bfd/archive.c
===================================================================
--- binutils-gdb.orig/bfd/archive.c
+++ binutils-gdb/bfd/archive.c
@@ -149,12 +149,19 @@ extern int errno;
 
 /*
 EXTERNAL
+.{* Holds a file position or bfd* depending on context.  *}
+.typedef union ufile_ptr_or_bfd
+.{
+.  ufile_ptr file_offset;
+.  bfd *abfd;
+.}
+.ufile_ptr_or_bfd;
+.
 .{* A canonical archive symbol.  *}
-.{* This is a type pun with struct symdef/struct ranlib on purpose!  *}
 .typedef struct carsym
 .{
 .  const char *name;
-.  ufile_ptr file_offset;	{* Look here to find the file.  *}
+.  ufile_ptr_or_bfd u;	{* bfd* or file position.  *}
 .}
 .carsym;
 .
@@ -804,7 +811,7 @@ _bfd_generic_get_elt_at_index (bfd *abfd
   carsym *entry;
 
   entry = bfd_ardata (abfd)->symdefs + sym_index;
-  return _bfd_get_elt_at_filepos (abfd, entry->file_offset, NULL);
+  return _bfd_get_elt_at_filepos (abfd, entry->u.file_offset, NULL);
 }
 
 bfd *
@@ -1038,7 +1045,7 @@ do_slurp_bsd_armap (bfd *abfd)
 	  goto release_armap;
 	}
       set->name = stringbase + nameoff;
-      set->file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
+      set->u.file_offset = H_GET_32 (abfd, rbase + BSD_SYMDEF_OFFSET_SIZE);
     }
 
   ardata->first_file_filepos = bfd_tell (abfd);
@@ -1139,7 +1146,7 @@ do_slurp_coff_armap (bfd *abfd)
   for (i = 0; i < nsymz; i++)
     {
       rawptr = raw_armap + i;
-      carsyms->file_offset = swap ((bfd_byte *) rawptr);
+      carsyms->u.file_offset = swap ((bfd_byte *) rawptr);
       carsyms->name = stringbase;
       stringbase += strlen (stringbase);
       if (stringbase != stringend)
Index: binutils-gdb/bfd/archive64.c
===================================================================
--- binutils-gdb.orig/bfd/archive64.c
+++ binutils-gdb/bfd/archive64.c
@@ -129,7 +129,7 @@ _bfd_archive_64_bit_slurp_armap (bfd *ab
   *stringend = 0;
   for (i = 0; i < nsymz; i++)
     {
-      carsyms->file_offset = bfd_getb64 (raw_armap + i * 8);
+      carsyms->u.file_offset = bfd_getb64 (raw_armap + i * 8);
       carsyms->name = stringbase;
       stringbase += strlen (stringbase);
       if (stringbase != stringend)
Index: binutils-gdb/bfd/bfd-in2.h
===================================================================
--- binutils-gdb.orig/bfd/bfd-in2.h
+++ binutils-gdb/bfd/bfd-in2.h
@@ -1273,12 +1273,19 @@ void bfd_symbol_info (asymbol *symbol, s
 		 (ibfd, isymbol, obfd, osymbol))
 
 /* Extracted from archive.c.  */
+/* Holds a file position or bfd* depending on context.  */
+typedef union ufile_ptr_or_bfd
+{
+  ufile_ptr file_offset;
+  bfd *abfd;
+}
+ufile_ptr_or_bfd;
+
 /* A canonical archive symbol.  */
-/* This is a type pun with struct symdef/struct ranlib on purpose!  */
 typedef struct carsym
 {
   const char *name;
-  ufile_ptr file_offset;       /* Look here to find the file.  */
+  ufile_ptr_or_bfd u;  /* bfd* or file position.  */
 }
 carsym;
 
Index: binutils-gdb/bfd/coff-alpha.c
===================================================================
--- binutils-gdb.orig/bfd/coff-alpha.c
+++ binutils-gdb/bfd/coff-alpha.c
@@ -2240,7 +2240,7 @@ alpha_ecoff_get_elt_at_index (bfd *abfd,
   carsym *entry;
 
   entry = bfd_ardata (abfd)->symdefs + sym_index;
-  return alpha_ecoff_get_elt_at_filepos (abfd, entry->file_offset, NULL);
+  return alpha_ecoff_get_elt_at_filepos (abfd, entry->u.file_offset, NULL);
 }
 
 static void
Index: binutils-gdb/bfd/coff-rs6000.c
===================================================================
--- binutils-gdb.orig/bfd/coff-rs6000.c
+++ binutils-gdb/bfd/coff-rs6000.c
@@ -1411,7 +1411,7 @@ _bfd_xcoff_slurp_armap (bfd *abfd)
       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 4;
 	   i < c;
 	   ++i, ++arsym, p += 4)
-	arsym->file_offset = H_GET_32 (abfd, p);
+	arsym->u.file_offset = H_GET_32 (abfd, p);
     }
   else
     {
@@ -1472,7 +1472,7 @@ _bfd_xcoff_slurp_armap (bfd *abfd)
       for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
 	   i < c;
 	   ++i, ++arsym, p += 8)
-	arsym->file_offset = H_GET_64 (abfd, p);
+	arsym->u.file_offset = H_GET_64 (abfd, p);
     }
 
   /* After the file offsets come null terminated symbol names.  */
Index: binutils-gdb/bfd/coff64-rs6000.c
===================================================================
--- binutils-gdb.orig/bfd/coff64-rs6000.c
+++ binutils-gdb/bfd/coff64-rs6000.c
@@ -1872,7 +1872,7 @@ xcoff64_slurp_armap (bfd *abfd)
   for (i = 0, arsym = bfd_ardata (abfd)->symdefs, p = contents + 8;
        i < c;
        ++i, ++arsym, p += 8)
-    arsym->file_offset = H_GET_64 (abfd, p);
+    arsym->u.file_offset = H_GET_64 (abfd, p);
 
   /* After the file offsets come null terminated symbol names.  */
   cend = contents + sz;
Index: binutils-gdb/bfd/ecoff.c
===================================================================
--- binutils-gdb.orig/bfd/ecoff.c
+++ binutils-gdb/bfd/ecoff.c
@@ -3065,7 +3065,7 @@ _bfd_ecoff_slurp_armap (bfd *abfd)
       if (name_offset > stringsize)
 	goto error_malformed;
       symdef_ptr->name = stringbase + name_offset;
-      symdef_ptr->file_offset = file_offset;
+      symdef_ptr->u.file_offset = file_offset;
       ++symdef_ptr;
     }
 
Index: binutils-gdb/bfd/elflink.c
===================================================================
--- binutils-gdb.orig/bfd/elflink.c
+++ binutils-gdb/bfd/elflink.c
@@ -3707,7 +3707,7 @@ elf_link_is_defined_archive_symbol (bfd
   Elf_Internal_Sym *isymend;
   bool result;
 
-  abfd = _bfd_get_elt_at_filepos (abfd, symdef->file_offset, NULL);
+  abfd = _bfd_get_elt_at_filepos (abfd, symdef->u.file_offset, NULL);
   if (abfd == NULL)
     return false;
 
@@ -6304,7 +6304,7 @@ elf_link_add_archive_symbols (bfd *abfd,
 
 	  if (included[i])
 	    continue;
-	  if (symdef->file_offset == last)
+	  if (symdef->u.file_offset == last)
 	    {
 	      included[i] = true;
 	      continue;
@@ -6389,7 +6389,7 @@ elf_link_add_archive_symbols (bfd *abfd,
 	    }
 
 	  /* We need to include this archive member.  */
-	  element = _bfd_get_elt_at_filepos (abfd, symdef->file_offset,
+	  element = _bfd_get_elt_at_filepos (abfd, symdef->u.file_offset,
 					     info);
 	  if (element == NULL)
 	    goto error_return;
@@ -6425,11 +6425,11 @@ elf_link_add_archive_symbols (bfd *abfd,
 		break;
 	      --mark;
 	    }
-	  while (symdefs[mark].file_offset == symdef->file_offset);
+	  while (symdefs[mark].u.file_offset == symdef->u.file_offset);
 
 	  /* We mark subsequent symbols from this object file as we go
 	     on through the loop.  */
-	  last = symdef->file_offset;
+	  last = symdef->u.file_offset;
 	}
     }
   while (loop);
Index: binutils-gdb/bfd/linker.c
===================================================================
--- binutils-gdb.orig/bfd/linker.c
+++ binutils-gdb/bfd/linker.c
@@ -974,7 +974,7 @@ _bfd_generic_link_add_archive_symbols
 
 	  if (included[indx])
 	    continue;
-	  if (needed && arsym->file_offset == last_ar_offset)
+	  if (needed && arsym->u.file_offset == last_ar_offset)
 	    {
 	      included[indx] = 1;
 	      continue;
@@ -1003,9 +1003,9 @@ _bfd_generic_link_add_archive_symbols
 	      continue;
 	    }
 
-	  if (last_ar_offset != arsym->file_offset)
+	  if (last_ar_offset != arsym->u.file_offset)
 	    {
-	      last_ar_offset = arsym->file_offset;
+	      last_ar_offset = arsym->u.file_offset;
 	      element = _bfd_get_elt_at_filepos (abfd, last_ar_offset,
 						 info);
 	      if (element == NULL
@@ -1034,7 +1034,7 @@ _bfd_generic_link_add_archive_symbols
 		    break;
 		  --mark;
 		}
-	      while (arsyms[mark].file_offset == last_ar_offset);
+	      while (arsyms[mark].u.file_offset == last_ar_offset);
 
 	      if (undefs_tail != info->hash->undefs_tail)
 		loop = true;
Index: binutils-gdb/bfd/som.c
===================================================================
--- binutils-gdb.orig/bfd/som.c
+++ binutils-gdb/bfd/som.c
@@ -6042,7 +6042,7 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
 	  bfd_set_error (bfd_error_bad_value);
 	  goto error_return;
 	}
-      set->file_offset
+      set->u.file_offset
 	= bfd_getb32 (som_dict[ndx].location) - sizeof (struct ar_hdr);
 
       /* Go to the next symbol.  */
@@ -6093,7 +6093,7 @@ som_bfd_fill_in_ar_symbols (bfd *abfd,
 	      bfd_set_error (bfd_error_bad_value);
 	      goto error_return;
 	    }
-	  set->file_offset
+	  set->u.file_offset
 	    = bfd_getb32 (som_dict[ndx].location) - sizeof (struct ar_hdr);
 
 	  /* Go on to the next symbol.  */
Index: binutils-gdb/bfd/vms-lib.c
===================================================================
--- binutils-gdb.orig/bfd/vms-lib.c
+++ binutils-gdb/bfd/vms-lib.c
@@ -170,7 +170,7 @@ vms_add_index (struct carsym_mem *cs, ch
       cs->idx = n;
       cs->realloced = true;
     }
-  cs->idx[cs->nbr].file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
+  cs->idx[cs->nbr].u.file_offset = (idx_vbn - 1) * VMS_BLOCK_SIZE + idx_off;
   cs->idx[cs->nbr].name = name;
   cs->nbr++;
   return true;
@@ -1385,7 +1385,7 @@ _bfd_vms_lib_get_module (bfd *abfd, unsi
     return tdata->cache[modidx];
 
   /* Build it.  */
-  file_off = tdata->modules[modidx].file_offset;
+  file_off = tdata->modules[modidx].u.file_offset;
   if (tdata->type != LBR__C_TYP_IOBJ)
     {
       res = _bfd_create_empty_archive_element_shell (abfd);
@@ -1485,12 +1485,12 @@ _bfd_vms_lib_get_elt_at_index (bfd *abfd
   /* Check symidx.  */
   if (symidx > tdata->artdata.symdef_count)
     return NULL;
-  file_off = tdata->artdata.symdefs[symidx].file_offset;
+  file_off = tdata->artdata.symdefs[symidx].u.file_offset;
 
   /* Linear-scan.  */
   for (modidx = 0; modidx < tdata->nbr_modules; modidx++)
     {
-      if (tdata->modules[modidx].file_offset == file_off)
+      if (tdata->modules[modidx].u.file_offset == file_off)
 	break;
     }
   if (modidx >= tdata->nbr_modules)


More information about the Binutils mailing list