This is the mail archive of the binutils@sources.redhat.com mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

{Patch] Computing section sizes


Hi Guys,

  Whilst working on something else I noticed how the computation of
  sh_size/sh_entsize occurs a lot in the ELF code in BFD, usually
  spanning a couple of lines.  Being the anal kind of guy that I am,
  this seemed to me to be the perfect opportunity to use a macro to
  simplify and ease code readability, and so here is the patch.

  It adds a new macro NUM_SHDR_ENTRIES to elf-bfd.h and then uses it
  in all the places that I could find.  I will be checking the patch
  in shortly.

Cheers
        Nick

2001-05-23  Nick Clifton  <nickc@cambridge.redhat.com>

	* elf-bfd.h (NUM_SHDR_ENTRIES): New macro: compute the number
	of entries in a structured section.
        * elf.c: Use new macro.
        * elf32-i386.c: Use new macro.
        * elf32-mips.c: Use new macro.
        * elf64-alpha.c: Use new macro.
        * elf64-sparc.c: Use new macro.
        * elfcode.h: Use new macro.
        * elflink.h: Use new macro.

? bfd/elflink.h.sav
? bfd/elf-bfd.h.sav
? bfd/elfxx-target.h.sav
Index: bfd/elf-bfd.h
===================================================================
RCS file: /cvs/src/src/bfd/elf-bfd.h,v
retrieving revision 1.33
diff -p -r1.33 elf-bfd.h
*** elf-bfd.h	2001/05/11 12:23:47	1.33
--- elf-bfd.h	2001/05/23 08:12:49
*************** Foundation, Inc., 59 Temple Place - Suit
*** 27,32 ****
--- 27,37 ----
  #include "elf/external.h"
  #include "bfdlink.h"
  
+ /* The number of entries in a section is its size divided by the size
+    of a single entry.  This is normally only applicaable to reloc and
+    symbol table sections.  */
+ #define NUM_SHDR_ENTRIES(shdr) ((shdr)->sh_size / (shdr)->sh_entsize)
+ 
  /* If size isn't specified as 64 or 32, NAME macro should fail.  */
  #ifndef NAME
  #if ARCH_SIZE==64
Index: bfd/elf.c
===================================================================
RCS file: /cvs/src/src/bfd/elf.c,v
retrieving revision 1.69
diff -p -r1.69 elf.c
*** elf.c	2001/05/11 12:23:47	1.69
--- elf.c	2001/05/23 08:12:55
*************** bfd_section_from_shdr (abfd, shindex)
*** 1449,1455 ****
  	  }
  	*hdr2 = *hdr;
  	elf_elfsections (abfd)[shindex] = hdr2;
! 	target_sect->reloc_count += hdr->sh_size / hdr->sh_entsize;
  	target_sect->flags |= SEC_RELOC;
  	target_sect->relocation = NULL;
  	target_sect->rel_filepos = hdr->sh_offset;
--- 1449,1455 ----
  	  }
  	*hdr2 = *hdr;
  	elf_elfsections (abfd)[shindex] = hdr2;
! 	target_sect->reloc_count += NUM_SHDR_ENTRIES (hdr);
  	target_sect->flags |= SEC_RELOC;
  	target_sect->relocation = NULL;
  	target_sect->rel_filepos = hdr->sh_offset;
Index: bfd/elf32-i386.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-i386.c,v
retrieving revision 1.25
diff -p -r1.25 elf32-i386.c
*** elf32-i386.c	2001/04/26 19:09:20	1.25
--- elf32-i386.c	2001/05/23 08:12:57
*************** elf_i386_check_relocs (abfd, info, sec, 
*** 501,507 ****
  
        r_symndx = ELF32_R_SYM (rel->r_info);
  
!       if (r_symndx >= symtab_hdr->sh_size / symtab_hdr->sh_entsize)
  	{
  	  if (abfd->my_archive)
  	    (*_bfd_error_handler) (_("%s(%s): bad symbol index: %d"),
--- 501,507 ----
  
        r_symndx = ELF32_R_SYM (rel->r_info);
  
!       if (r_symndx >= NUM_SHDR_ENTRIES (symtab_hdr))
  	{
  	  if (abfd->my_archive)
  	    (*_bfd_error_handler) (_("%s(%s): bad symbol index: %d"),
Index: bfd/elf32-mips.c
===================================================================
RCS file: /cvs/src/src/bfd/elf32-mips.c,v
retrieving revision 1.95
diff -p -r1.95 elf32-mips.c
*** elf32-mips.c	2001/05/08 17:14:35	1.95
--- elf32-mips.c	2001/05/23 08:13:06
*************** _bfd_mips_elf_fake_sections (abfd, hdr, 
*** 3071,3077 ****
  
  boolean
  _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
!      bfd *abfd;
       Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
       asection *sec;
       int *retval;
--- 3071,3077 ----
  
  boolean
  _bfd_mips_elf_section_from_bfd_section (abfd, hdr, sec, retval)
!      bfd *abfd ATTRIBUTE_UNUSED;
       Elf_Internal_Shdr *hdr ATTRIBUTE_UNUSED;
       asection *sec;
       int *retval;
*************** _bfd_mips_elf_relocate_section (output_b
*** 6822,6829 ****
  	     REL_HDR is read before its REL_HDR2.  */
  	  rel_hdr = &elf_section_data (input_section)->rel_hdr;
  	  if ((size_t) (rel - relocs)
! 	      >= (rel_hdr->sh_size / rel_hdr->sh_entsize
! 		  * bed->s->int_rels_per_ext_rel))
  	    rel_hdr = elf_section_data (input_section)->rel_hdr2;
  	  if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
  	    {
--- 6822,6828 ----
  	     REL_HDR is read before its REL_HDR2.  */
  	  rel_hdr = &elf_section_data (input_section)->rel_hdr;
  	  if ((size_t) (rel - relocs)
! 	      >= (NUM_SHDR_ENTRIES (rel_hdr) * bed->s->int_rels_per_ext_rel))
  	    rel_hdr = elf_section_data (input_section)->rel_hdr2;
  	  if (rel_hdr->sh_entsize == MIPS_ELF_REL_SIZE (input_bfd))
  	    {
*************** _bfd_mips_elf_check_relocs (abfd, info, 
*** 7592,7598 ****
  	      asection **n;
  
  	      if (elf_bad_symtab (abfd))
! 		symcount = symtab_hdr->sh_size / symtab_hdr->sh_entsize;
  	      else
  		symcount = symtab_hdr->sh_info;
  	      n = (asection **) bfd_zalloc (abfd,
--- 7591,7597 ----
  	      asection **n;
  
  	      if (elf_bad_symtab (abfd))
! 		symcount = NUM_SHDR_ENTRIES (symtab_hdr);
  	      else
  		symcount = symtab_hdr->sh_info;
  	      n = (asection **) bfd_zalloc (abfd,
*************** _bfd_mips_elf_check_relocs (abfd, info, 
*** 7707,7713 ****
  
        if (r_symndx < extsymoff)
  	h = NULL;
!       else if (r_symndx >= extsymoff + (symtab_hdr->sh_size / symtab_hdr->sh_entsize))
  	{
  	  (*_bfd_error_handler)
  	    (_("Malformed reloc detected for section %s"), name);
--- 7706,7712 ----
  
        if (r_symndx < extsymoff)
  	h = NULL;
!       else if (r_symndx >= extsymoff + NUM_SHDR_ENTRIES (symtab_hdr))
  	{
  	  (*_bfd_error_handler)
  	    (_("Malformed reloc detected for section %s"), name);
Index: bfd/elf64-alpha.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-alpha.c,v
retrieving revision 1.25
diff -p -r1.25 elf64-alpha.c
*** elf64-alpha.c	2001/05/04 23:22:31	1.25
--- elf64-alpha.c	2001/05/23 08:13:11
*************** elf64_alpha_can_merge_gots (a, b)
*** 2831,2837 ****
        Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
        int i, n;
  
!       n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
        for (i = 0; i < n; ++i)
  	{
  	  struct alpha_elf_got_entry *ae, *be;
--- 2831,2837 ----
        Elf_Internal_Shdr *symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
        int i, n;
  
!       n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
        for (i = 0; i < n; ++i)
  	{
  	  struct alpha_elf_got_entry *ae, *be;
*************** elf64_alpha_merge_gots (a, b)
*** 2903,2909 ****
        hashes = alpha_elf_sym_hashes (bsub);
        symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
  
!       n = symtab_hdr->sh_size / symtab_hdr->sh_entsize - symtab_hdr->sh_info;
        for (i = 0; i < n; ++i)
          {
  	  struct alpha_elf_got_entry *ae, *be, **pbe, **start;
--- 2903,2909 ----
        hashes = alpha_elf_sym_hashes (bsub);
        symtab_hdr = &elf_tdata (bsub)->symtab_hdr;
  
!       n = NUM_SHDR_ENTRIES (symtab_hdr) - symtab_hdr->sh_info;
        for (i = 0; i < n; ++i)
          {
  	  struct alpha_elf_got_entry *ae, *be, **pbe, **start;
Index: bfd/elf64-sparc.c
===================================================================
RCS file: /cvs/src/src/bfd/elf64-sparc.c,v
retrieving revision 1.26
diff -p -r1.26 elf64-sparc.c
*** elf64-sparc.c	2001/03/08 21:04:01	1.26
--- elf64-sparc.c	2001/05/23 08:13:14
*************** sparc64_elf_slurp_reloc_table (abfd, ase
*** 396,402 ****
  	return true;
  
        rel_hdr = &d->this_hdr;
!       asect->reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
        rel_hdr2 = NULL;
      }
  
--- 396,402 ----
  	return true;
  
        rel_hdr = &d->this_hdr;
!       asect->reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
        rel_hdr2 = NULL;
      }
  
*************** sparc64_elf_check_relocs (abfd, info, se
*** 996,1003 ****
    srelgot = NULL;
    sreloc = NULL;
  
!   rel_end = relocs + elf_section_data (sec)->rel_hdr.sh_size
! 		     / elf_section_data (sec)->rel_hdr.sh_entsize;
    for (rel = relocs; rel < rel_end; rel++)
      {
        unsigned long r_symndx;
--- 996,1002 ----
    srelgot = NULL;
    sreloc = NULL;
  
!   rel_end = relocs + NUM_SHDR_ENTRIES (& elf_section_data (sec)->rel_hdr);
    for (rel = relocs; rel < rel_end; rel++)
      {
        unsigned long r_symndx;
*************** sparc64_elf_relocate_section (output_bfd
*** 1903,1910 ****
    sgot = splt = sreloc = NULL;
  
    rel = relocs;
!   relend = relocs + elf_section_data (input_section)->rel_hdr.sh_size
! 		    / elf_section_data (input_section)->rel_hdr.sh_entsize;
    for (; rel < relend; rel++)
      {
        int r_type;
--- 1902,1908 ----
    sgot = splt = sreloc = NULL;
  
    rel = relocs;
!   relend = relocs + NUM_SHDR_ENTIRES (& elf_section_data (input_section)->rel_hdr);
    for (; rel < relend; rel++)
      {
        int r_type;
Index: bfd/elfcode.h
===================================================================
RCS file: /cvs/src/src/bfd/elfcode.h,v
retrieving revision 1.18
diff -p -r1.18 elfcode.h
*** elfcode.h	2001/05/03 06:18:09	1.18
--- elfcode.h	2001/05/23 08:13:15
*************** elf_slurp_reloc_table (abfd, asect, symb
*** 1373,1383 ****
  	return true;
  
        rel_hdr = &d->rel_hdr;
!       reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
        rel_hdr2 = d->rel_hdr2;
!       reloc_count2 = (rel_hdr2
! 		      ? (rel_hdr2->sh_size / rel_hdr2->sh_entsize)
! 		      : 0);
  
        BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
        BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
--- 1373,1381 ----
  	return true;
  
        rel_hdr = &d->rel_hdr;
!       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
        rel_hdr2 = d->rel_hdr2;
!       reloc_count2 = (rel_hdr2 ? NUM_SHDR_ENTRIES (rel_hdr2) : 0);
  
        BFD_ASSERT (asect->reloc_count == reloc_count + reloc_count2);
        BFD_ASSERT (asect->rel_filepos == rel_hdr->sh_offset
*************** elf_slurp_reloc_table (abfd, asect, symb
*** 1394,1400 ****
  	return true;
  
        rel_hdr = &d->this_hdr;
!       reloc_count = rel_hdr->sh_size / rel_hdr->sh_entsize;
        rel_hdr2 = NULL;
        reloc_count2 = 0;
      }
--- 1392,1398 ----
  	return true;
  
        rel_hdr = &d->this_hdr;
!       reloc_count = NUM_SHDR_ENTRIES (rel_hdr);
        rel_hdr2 = NULL;
        reloc_count2 = 0;
      }
Index: bfd/elflink.h
===================================================================
RCS file: /cvs/src/src/bfd/elflink.h,v
retrieving revision 1.91
diff -p -r1.91 elflink.h
*** elflink.h	2001/05/18 16:06:43	1.91
--- elflink.h	2001/05/23 08:13:22
*************** elf_link_read_relocs_from_section (abfd,
*** 2465,2471 ****
        Elf_Internal_Rel *irel;
  
        erel = (Elf_External_Rel *) external_relocs;
!       erelend = erel + shdr->sh_size / shdr->sh_entsize;
        irela = internal_relocs;
        irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
  			       * sizeof (Elf_Internal_Rel)));
--- 2465,2471 ----
        Elf_Internal_Rel *irel;
  
        erel = (Elf_External_Rel *) external_relocs;
!       erelend = erel + NUM_SHDR_ENTRIES (shdr);
        irela = internal_relocs;
        irel = bfd_alloc (abfd, (bed->s->int_rels_per_ext_rel
  			       * sizeof (Elf_Internal_Rel)));
*************** elf_link_read_relocs_from_section (abfd,
*** 2495,2501 ****
        BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
  
        erela = (Elf_External_Rela *) external_relocs;
!       erelaend = erela + shdr->sh_size / shdr->sh_entsize;
        irela = internal_relocs;
        for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
  	{
--- 2495,2501 ----
        BFD_ASSERT (shdr->sh_entsize == sizeof (Elf_External_Rela));
  
        erela = (Elf_External_Rela *) external_relocs;
!       erelaend = erela + NUM_SHDR_ENTRIES (shdr);
        irela = internal_relocs;
        for (; erela < erelaend; erela++, irela += bed->s->int_rels_per_ext_rel)
  	{
*************** NAME(_bfd_elf,link_read_relocs) (abfd, o
*** 2575,2581 ****
        (abfd,
         elf_section_data (o)->rel_hdr2,
         ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
!        internal_relocs + (rel_hdr->sh_size / rel_hdr->sh_entsize
  			  * bed->s->int_rels_per_ext_rel)))
      goto error_return;
  
--- 2575,2581 ----
        (abfd,
         elf_section_data (o)->rel_hdr2,
         ((bfd_byte *) external_relocs) + rel_hdr->sh_size,
!        internal_relocs + (NUM_SHDR_ENTRIES (rel_hdr)
  			  * bed->s->int_rels_per_ext_rel)))
      goto error_return;
  
*************** elf_bfd_final_link (abfd, info)
*** 4413,4423 ****
  		  rel_count2 = &esdo->rel_count;
  		}
  
! 	      *rel_count += (esdi->rel_hdr.sh_size
! 			     / esdi->rel_hdr.sh_entsize);
  	      if (esdi->rel_hdr2)
! 		*rel_count2 += (esdi->rel_hdr2->sh_size
! 				/ esdi->rel_hdr2->sh_entsize);
  	    }
  	}
  
--- 4413,4421 ----
  		  rel_count2 = &esdo->rel_count;
  		}
  
! 	      *rel_count += NUM_SHDR_ENTRIES (& esdi->rel_hdr);
  	      if (esdi->rel_hdr2)
! 		*rel_count2 += NUM_SHDR_ENTRIES (esdi->rel_hdr2);
  	    }
  	}
  
*************** elf_link_output_relocs (output_bfd, inpu
*** 5442,5448 ****
  
    bed = get_elf_backend_data (output_bfd);
    irela = internal_relocs;
!   irelaend = irela + input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
    if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
      {
        Elf_External_Rel *erel;
--- 5440,5446 ----
  
    bed = get_elf_backend_data (output_bfd);
    irela = internal_relocs;
!   irelaend = irela + NUM_SHDR_ENTRIES (input_rel_hdr);
    if (input_rel_hdr->sh_entsize == sizeof (Elf_External_Rel))
      {
        Elf_External_Rel *erel;
*************** elf_link_output_relocs (output_bfd, inpu
*** 5477,5483 ****
  
    /* Bump the counter, so that we know where to add the next set of
       relocations.  */
!   *rel_countp += input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
  }
  
  /* Link an input file into the linker output file.  This function
--- 5475,5481 ----
  
    /* Bump the counter, so that we know where to add the next set of
       relocations.  */
!   *rel_countp += NUM_SHDR_ENTRIES (input_rel_hdr);
  }
  
  /* Link an input file into the linker output file.  This function
*************** elf_link_input_bfd (finfo, input_bfd)
*** 5939,5946 ****
  	      elf_link_output_relocs (output_bfd, o,
  				      input_rel_hdr,
  				      internal_relocs);
! 	      internal_relocs
! 		+= input_rel_hdr->sh_size / input_rel_hdr->sh_entsize;
  	      input_rel_hdr = elf_section_data (o)->rel_hdr2;
  	      if (input_rel_hdr)
  		elf_link_output_relocs (output_bfd, o,
--- 5937,5943 ----
  	      elf_link_output_relocs (output_bfd, o,
  				      input_rel_hdr,
  				      internal_relocs);
! 	      internal_relocs += NUM_SHDR_ENTRIES (input_rel_hdr);
  	      input_rel_hdr = elf_section_data (o)->rel_hdr2;
  	      if (input_rel_hdr)
  		elf_link_output_relocs (output_bfd, o,

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]