public inbox for binutils@sourceware.org
 help / color / mirror / Atom feed
* PATCH: Print out bits of SOM headers
@ 2004-11-08 22:17 Mark Mitchell
  2004-11-09  9:06 ` Nick Clifton
  0 siblings, 1 reply; 3+ messages in thread
From: Mark Mitchell @ 2004-11-08 22:17 UTC (permalink / raw)
  To: binutils


In investigating a problem on PA HP-UX, I added some code to som.c to
print out some bits from the SOM headers.  In particular, we can now
get accurate information about the "Exec Auxililary Header".  I've
tested this code lightly.

I'm not terribly attached to this patch, but, having done it, I'm
posting it.  OK to commit?

--
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

2004-11-08  Mark Mitchell  <mark@codesourcery.com>

	* som.c (som_bfd_print_private_bfd_data): New function.
	(som_object_setup): Save the auxiliary header.  Don't assume that
	zero is an invalid entry point for a shared library.
	(som_object_p): Allocate the auxiliary header on the heap.

Index: som.c
===================================================================
RCS file: /cvs/src/src/bfd/som.c,v
retrieving revision 1.48
diff -c -5 -p -r1.48 som.c
*** som.c	9 Oct 2004 02:51:31 -0000	1.48
--- som.c	8 Nov 2004 22:14:05 -0000
*************** static bfd_boolean som_bfd_copy_private_
*** 194,203 ****
--- 194,205 ----
    PARAMS ((bfd *, bfd *));
  #define som_bfd_copy_private_header_data \
    _bfd_generic_bfd_copy_private_header_data
  #define som_bfd_merge_private_bfd_data _bfd_generic_bfd_merge_private_bfd_data
  #define som_bfd_set_private_flags _bfd_generic_bfd_set_private_flags
+ static bfd_boolean som_bfd_print_private_bfd_data
+   PARAMS ((bfd *, void *));
  static bfd_boolean som_bfd_is_local_label_name
    PARAMS ((bfd *, const char *));
  static bfd_boolean som_set_section_contents
    PARAMS ((bfd *, sec_ptr, const PTR, file_ptr, bfd_size_type));
  static bfd_boolean som_get_section_contents
*************** som_object_setup (abfd, file_hdrp, aux_h
*** 1770,1780 ****
       struct header *file_hdrp;
       struct som_exec_auxhdr *aux_hdrp;
       unsigned long current_offset;
  {
    asection *section;
-   int found;
  
    /* som_mkobject will set bfd_error if som_mkobject fails.  */
    if (! som_mkobject (abfd))
      return 0;
  
--- 1772,1781 ----
*************** som_object_setup (abfd, file_hdrp, aux_h
*** 1808,1817 ****
--- 1809,1821 ----
  
      default:
        break;
      }
  
+   /* Save the auxiliary header.  */
+   obj_som_exec_hdr (abfd) = aux_hdrp;
+ 
    /* Allocate space to hold the saved exec header information.  */
    obj_som_exec_data (abfd) = (struct som_exec_data *)
      bfd_zalloc (abfd, (bfd_size_type) sizeof (struct som_exec_data));
    if (obj_som_exec_data (abfd) == NULL)
      return NULL;
*************** som_object_setup (abfd, file_hdrp, aux_h
*** 1822,1857 ****
       apparently the latest HPUX linker is using NEW_VERSION_ID now.
  
       It's about time, OSF has used the new id since at least 1992;
       HPUX didn't start till nearly 1995!.
  
!      The new approach examines the entry field.  If it's zero or not 4
!      byte aligned then it's not a proper code address and we guess it's
!      really the executable flags.  */
!   found = 0;
!   for (section = abfd->sections; section; section = section->next)
      {
!       bfd_vma entry;
  
!       if ((section->flags & SEC_CODE) == 0)
! 	continue;
!       entry = aux_hdrp->exec_entry;
!       if (entry >= section->vma
! 	  && entry < section->vma + section->size)
! 	found = 1;
!     }
!   if (aux_hdrp->exec_entry == 0
!       || (aux_hdrp->exec_entry & 0x3) != 0
!       || ! found)
!     {
!       bfd_get_start_address (abfd) = aux_hdrp->exec_flags;
!       obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_entry;
!     }
!   else
!     {
!       bfd_get_start_address (abfd) = aux_hdrp->exec_entry + current_offset;
!       obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_flags;
      }
  
    obj_som_exec_data (abfd)->version_id = file_hdrp->version_id;
  
    bfd_default_set_arch_mach (abfd, bfd_arch_hppa, pa10);
--- 1826,1869 ----
       apparently the latest HPUX linker is using NEW_VERSION_ID now.
  
       It's about time, OSF has used the new id since at least 1992;
       HPUX didn't start till nearly 1995!.
  
!      The new approach examines the entry field for an executable.  If
!      it is not 4-byte aligned then it's not a proper code address and
!      we guess it's really the executable flags.  For a main program,
!      we also consider zero to be indicative of a buggy linker, since
!      that is not a valid entry point.  The entry point for a shared
!      library, however, can be zero so we do not consider that to be
!      indicative of a buggy linker.  */
!   if (aux_hdrp)
      {
!       int found = 0;
  
!       for (section = abfd->sections; section; section = section->next)
! 	{
! 	  bfd_vma entry;
! 
! 	  if ((section->flags & SEC_CODE) == 0)
! 	    continue;
! 	  entry = aux_hdrp->exec_entry + aux_hdrp->exec_tmem;
! 	  if (entry >= section->vma
! 	      && entry < section->vma + section->size)
! 	    found = 1;
! 	}
!       if ((aux_hdrp->exec_entry == 0 && !(abfd->flags & DYNAMIC))
! 	  || (aux_hdrp->exec_entry & 0x3) != 0
! 	  || ! found)
! 	{
! 	  bfd_get_start_address (abfd) = aux_hdrp->exec_flags;
! 	  obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_entry;
! 	}
!       else
! 	{
! 	  bfd_get_start_address (abfd) = aux_hdrp->exec_entry + current_offset;
! 	  obj_som_exec_data (abfd)->exec_flags = aux_hdrp->exec_flags;
! 	}
      }
  
    obj_som_exec_data (abfd)->version_id = file_hdrp->version_id;
  
    bfd_default_set_arch_mach (abfd, bfd_arch_hppa, pa10);
*************** setup_sections (abfd, file_hdr, current_
*** 2181,2191 ****
  static const bfd_target *
  som_object_p (abfd)
       bfd *abfd;
  {
    struct header file_hdr;
!   struct som_exec_auxhdr aux_hdr;
    unsigned long current_offset = 0;
    struct lst_header lst_header;
    struct som_entry som_entry;
    bfd_size_type amt;
  #define ENTRY_SIZE sizeof (struct som_entry)
--- 2193,2203 ----
  static const bfd_target *
  som_object_p (abfd)
       bfd *abfd;
  {
    struct header file_hdr;
!   struct som_exec_auxhdr *aux_hdr = NULL;
    unsigned long current_offset = 0;
    struct lst_header lst_header;
    struct som_entry som_entry;
    bfd_size_type amt;
  #define ENTRY_SIZE sizeof (struct som_entry)
*************** som_object_p (abfd)
*** 2293,2307 ****
      }
  
    /* If the aux_header_size field in the file header is zero, then this
       object is an incomplete executable (a .o file).  Do not try to read
       a non-existant auxiliary header.  */
-   memset (&aux_hdr, 0, sizeof (struct som_exec_auxhdr));
    if (file_hdr.aux_header_size != 0)
      {
        amt = AUX_HDR_SIZE;
!       if (bfd_bread ((PTR) &aux_hdr, amt, abfd) != amt)
  	{
  	  if (bfd_get_error () != bfd_error_system_call)
  	    bfd_set_error (bfd_error_wrong_format);
  	  return 0;
  	}
--- 2305,2323 ----
      }
  
    /* If the aux_header_size field in the file header is zero, then this
       object is an incomplete executable (a .o file).  Do not try to read
       a non-existant auxiliary header.  */
    if (file_hdr.aux_header_size != 0)
      {
+       aux_hdr = ((struct som_exec_auxhdr *)
+ 		 bfd_zalloc (abfd, 
+ 			     (bfd_size_type) sizeof (struct som_exec_auxhdr)));
+       if (aux_hdr == NULL)
+ 	return NULL;
        amt = AUX_HDR_SIZE;
!       if (bfd_bread ((PTR) aux_hdr, amt, abfd) != amt)
  	{
  	  if (bfd_get_error () != bfd_error_system_call)
  	    bfd_set_error (bfd_error_wrong_format);
  	  return 0;
  	}
*************** som_object_p (abfd)
*** 2313,2323 ****
        bfd_set_error (bfd_error_bad_value);
        return 0;
      }
  
    /* This appears to be a valid SOM object.  Do some initialization.  */
!   return som_object_setup (abfd, &file_hdr, &aux_hdr, current_offset);
  }
  
  /* Create a SOM object.  */
  
  static bfd_boolean
--- 2329,2339 ----
        bfd_set_error (bfd_error_bad_value);
        return 0;
      }
  
    /* This appears to be a valid SOM object.  Do some initialization.  */
!   return som_object_setup (abfd, &file_hdr, aux_hdr, current_offset);
  }
  
  /* Create a SOM object.  */
  
  static bfd_boolean
*************** som_bfd_copy_private_bfd_data (ibfd, obf
*** 5240,5249 ****
--- 5256,5310 ----
  	  sizeof (struct som_exec_data));
  
    return TRUE;
  }
  
+ /* Display the SOM header.  */
+ 
+ static bfd_boolean
+ som_bfd_print_private_bfd_data (abfd, farg)
+      bfd *abfd;
+      void *farg;
+ {
+   struct som_exec_auxhdr *exec_header;
+   struct aux_id* auxhdr;
+   FILE *f;
+ 
+   f = (FILE *) farg;
+ 
+   exec_header = obj_som_exec_hdr (abfd);
+   if (exec_header)
+     {
+       fprintf (f, _("\nExec Auxiliary Header\n"));
+       fprintf (f, "  flags              ");
+       auxhdr = &exec_header->som_auxhdr;
+       if (auxhdr->mandatory)
+ 	fprintf (f, "mandatory ");
+       if (auxhdr->copy)
+ 	fprintf (f, "copy ");
+       if (auxhdr->append)
+ 	fprintf (f, "append ");
+       if (auxhdr->ignore)
+ 	fprintf (f, "ignore ");
+       fprintf (f, "\n");
+       fprintf (f, "  type               %#x\n", auxhdr->type);
+       fprintf (f, "  length             %#x\n", auxhdr->length);
+       fprintf (f, "  text size          %#x\n", exec_header->exec_tsize);
+       fprintf (f, "  text memory offset %#x\n", exec_header->exec_tmem);
+       fprintf (f, "  text file offset   %#x\n", exec_header->exec_tfile);
+       fprintf (f, "  data size          %#x\n", exec_header->exec_dsize);
+       fprintf (f, "  data memory offset %#x\n", exec_header->exec_dmem);
+       fprintf (f, "  data file offset   %#x\n", exec_header->exec_dfile);
+       fprintf (f, "  bss size           %#x\n", exec_header->exec_bsize);
+       fprintf (f, "  entry point        %#x\n", exec_header->exec_entry);
+       fprintf (f, "  loader flags       %#x\n", exec_header->exec_flags);
+       fprintf (f, "  bss initializer    %#x\n", exec_header->exec_bfill);
+     }
+ 
+   return TRUE;
+ }
+ 
  /* Set backend info for sections which can not be described
     in the BFD data structures.  */
  
  bfd_boolean
  bfd_som_set_section_attributes (section, defined, private, sort_key, spnum)
*************** som_bfd_link_split_section (abfd, sec)
*** 6398,6408 ****
  #define som_truncate_arname		bfd_bsd_truncate_arname
  #define som_slurp_extended_name_table	_bfd_slurp_extended_name_table
  #define som_construct_extended_name_table \
    _bfd_archive_coff_construct_extended_name_table
  #define som_update_armap_timestamp	bfd_true
- #define som_bfd_print_private_bfd_data  _bfd_generic_bfd_print_private_bfd_data
  
  #define som_bfd_is_target_special_symbol \
    ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  #define som_get_lineno			_bfd_nosymbols_get_lineno
  #define som_bfd_make_debug_symbol	_bfd_nosymbols_bfd_make_debug_symbol
--- 6459,6468 ----

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

* Re: PATCH: Print out bits of SOM headers
  2004-11-08 22:17 PATCH: Print out bits of SOM headers Mark Mitchell
@ 2004-11-09  9:06 ` Nick Clifton
  2004-11-09 17:43   ` Mark Mitchell
  0 siblings, 1 reply; 3+ messages in thread
From: Nick Clifton @ 2004-11-09  9:06 UTC (permalink / raw)
  To: mark; +Cc: binutils

Hi Mark,

> 2004-11-08  Mark Mitchell  <mark@codesourcery.com>
> 
> 	* som.c (som_bfd_print_private_bfd_data): New function.
> 	(som_object_setup): Save the auxiliary header.  Don't assume that
> 	zero is an invalid entry point for a shared library.
> 	(som_object_p): Allocate the auxiliary header on the heap.

Could you make a couple of tidy-ups to the patch first please ?

> + static bfd_boolean som_bfd_print_private_bfd_data
> +   PARAMS ((bfd *, void *));

We are using ISO C90 in binutils now, and even though this file has not 
yet been converted, I think that it is better that any new code added 
should follow this standard.  Hence the PARAMS macro is no longer needed.

> !   struct som_exec_auxhdr aux_hdr;
> --- 2193,2203 ----
> !   struct som_exec_auxhdr *aux_hdr = NULL;

My gut feeling here is that if you are changing the type of a variable 
you ought to change its name as well.  Hence I would recommend changing 
aux_hdr to aux_hdr_ptr.

> +       aux_hdr = ((struct som_exec_auxhdr *)
> + 		 bfd_zalloc (abfd, 
> + 			     (bfd_size_type) sizeof (struct som_exec_auxhdr)));

bfd_zalloc returns a (void *) so there is no need for the cast to 
(struct som_exec_auxhdr *).  Also, and this is just a personal 
preference, I think that it is safer to take the size of the pointer 
being allocated rather than the type that you know the pointer to be. 
That way if the pointer's type is ever changed you do not have to alter 
the code inside the alloc.  ie, I would suggest that the above statement 
could be coded as:

    aux_hdr_ptr = bfd_zalloc (abfd, (bfd_size_type) sizeof (* aux_hdr_ptr));

> + /* Display the SOM header.  */
> + 
> + static bfd_boolean
> + som_bfd_print_private_bfd_data (abfd, farg)
> +      bfd *abfd;
> +      void *farg;

ISO C90 again - please include the function's parameter types inside the 
parentheses.

> +       auxhdr = &exec_header->som_auxhdr;
> +       if (auxhdr->mandatory)
> + 	fprintf (f, "mandatory ");

For paranoia purposes I would suggest that you check for a NULL auxhdr 
before using it, in case the som_exec_auxhdr structure is corrupt.

With these changes made, please consider the patch approved.

Cheers
   Nick

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

* Re: PATCH: Print out bits of SOM headers
  2004-11-09  9:06 ` Nick Clifton
@ 2004-11-09 17:43   ` Mark Mitchell
  0 siblings, 0 replies; 3+ messages in thread
From: Mark Mitchell @ 2004-11-09 17:43 UTC (permalink / raw)
  To: Nick Clifton; +Cc: binutils

Nick Clifton wrote:
> Hi Mark,
> 
>> 2004-11-08  Mark Mitchell  <mark@codesourcery.com>
>>
>>     * som.c (som_bfd_print_private_bfd_data): New function.
>>     (som_object_setup): Save the auxiliary header.  Don't assume that
>>     zero is an invalid entry point for a shared library.
>>     (som_object_p): Allocate the auxiliary header on the heap.
> 
> 
> Could you make a couple of tidy-ups to the patch first please ?

I've made the changes you requested, with one exception.

>> +       auxhdr = &exec_header->som_auxhdr;
>> +       if (auxhdr->mandatory)
>> +     fprintf (f, "mandatory ");
> 
> 
> For paranoia purposes I would suggest that you check for a NULL auxhdr 
> before using it, in case the som_exec_auxhdr structure is corrupt.

I did not make this change.  Note the "&" above; auxhdr is a subobject 
of (*exec_header).  It cannot be NULL.

Patch committed; thanks.

-- 
Mark Mitchell
CodeSourcery, LLC
mark@codesourcery.com

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

end of thread, other threads:[~2004-11-09 17:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-11-08 22:17 PATCH: Print out bits of SOM headers Mark Mitchell
2004-11-09  9:06 ` Nick Clifton
2004-11-09 17:43   ` Mark Mitchell

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