PR binutils/716
[deliverable/binutils-gdb.git] / bfd / som.c
index 6f053913c27aad2daad61d375b7a09fdc727b3bd..d44c474519c9f45fa6d4fe997242f376c52fd213 100644 (file)
--- a/bfd/som.c
+++ b/bfd/som.c
@@ -1,6 +1,6 @@
 /* bfd back-end for HP PA-RISC SOM objects.
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003
+   2000, 2001, 2002, 2003, 2004
    Free Software Foundation, Inc.
 
    Contributed by the Center for Software Science at the
@@ -192,8 +192,12 @@ static bfd_boolean som_bfd_copy_private_section_data
   PARAMS ((bfd *, asection *, bfd *, asection *));
 static bfd_boolean som_bfd_copy_private_bfd_data
   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
+  (bfd *, void *);
 static bfd_boolean som_bfd_is_local_label_name
   PARAMS ((bfd *, const char *));
 static bfd_boolean som_set_section_contents
@@ -209,7 +213,7 @@ static void som_get_symbol_info
   PARAMS ((bfd *, asymbol *, symbol_info *));
 static asection * bfd_section_from_som_symbol
   PARAMS ((bfd *, struct symbol_dictionary_record *));
-static int log2
+static int exact_log2
   PARAMS ((unsigned int));
 static bfd_reloc_status_type hppa_som_reloc
   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
@@ -1431,11 +1435,11 @@ som_reloc_call (abfd, p, subspace_reloc_sizep, bfd_reloc, sym_num, queue)
   return p;
 }
 
-/* Return the logarithm of X, base 2, considering X unsigned.
-   Abort -1 if X is not a power or two or is zero.  */
+/* Return the logarithm of X, base 2, considering X unsigned,
+   if X is a power of 2.  Otherwise, returns -1.  */
 
 static int
-log2 (x)
+exact_log2 (x)
      unsigned int x;
 {
   int log = 0;
@@ -1770,7 +1774,6 @@ som_object_setup (abfd, file_hdrp, aux_hdrp, current_offset)
      unsigned long current_offset;
 {
   asection *section;
-  int found;
 
   /* som_mkobject will set bfd_error if som_mkobject fails.  */
   if (! som_mkobject (abfd))
@@ -1808,6 +1811,9 @@ som_object_setup (abfd, file_hdrp, aux_hdrp, current_offset)
       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));
@@ -1822,32 +1828,40 @@ som_object_setup (abfd, file_hdrp, aux_hdrp, current_offset)
      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)
+     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)
     {
-      bfd_vma entry;
+      int found = 0;
 
-      if ((section->flags & SEC_CODE) == 0)
-       continue;
-      entry = aux_hdrp->exec_entry;
-      if (entry >= section->vma
-         && entry < section->vma + section->_cooked_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;
+      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;
@@ -1911,6 +1925,7 @@ setup_sections (abfd, file_hdr, current_offset)
       struct som_subspace_dictionary_record subspace, save_subspace;
       unsigned int subspace_index;
       asection *space_asect;
+      bfd_size_type space_size = 0;
       char *newname;
 
       /* Read the space dictionary element.  */
@@ -1970,7 +1985,7 @@ setup_sections (abfd, file_hdr, current_offset)
         record.  */
       space_asect->vma = subspace.subspace_start;
       space_asect->filepos = subspace.file_loc_init_value + current_offset;
-      space_asect->alignment_power = log2 (subspace.alignment);
+      space_asect->alignment_power = exact_log2 (subspace.alignment);
       if (space_asect->alignment_power == (unsigned) -1)
        goto error_return;
 
@@ -2096,32 +2111,40 @@ setup_sections (abfd, file_hdr, current_offset)
            save_subspace = subspace;
 
          subspace_asect->vma = subspace.subspace_start;
-         subspace_asect->_cooked_size = subspace.subspace_length;
-         subspace_asect->_raw_size = subspace.subspace_length;
+         subspace_asect->size = subspace.subspace_length;
          subspace_asect->filepos = (subspace.file_loc_init_value
                                     + current_offset);
-         subspace_asect->alignment_power = log2 (subspace.alignment);
+         subspace_asect->alignment_power = exact_log2 (subspace.alignment);
          if (subspace_asect->alignment_power == (unsigned) -1)
            goto error_return;
+
+         /* Keep track of the accumulated sizes of the sections.  */
+         space_size += subspace.subspace_length;
        }
 
       /* This can happen for a .o which defines symbols in otherwise
         empty subspaces.  */
       if (!save_subspace.file_loc_init_value)
-       {
-         space_asect->_cooked_size = 0;
-         space_asect->_raw_size = 0;
-       }
+       space_asect->size = 0;
       else
        {
-         /* Setup the sizes for the space section based upon the info in the
-            last subspace of the space.  */
-         space_asect->_cooked_size = (save_subspace.subspace_start
-                                      - space_asect->vma
-                                      + save_subspace.subspace_length);
-         space_asect->_raw_size = (save_subspace.file_loc_init_value
-                                   - space_asect->filepos
-                                   + save_subspace.initialization_length);
+         if (file_hdr->a_magic != RELOC_MAGIC)
+           {
+             /* Setup the size for the space section based upon the info
+                in the last subspace of the space.  */
+             space_asect->size = (save_subspace.subspace_start
+                                  - space_asect->vma
+                                  + save_subspace.subspace_length);
+           }
+         else
+           {
+             /* The subspace_start field is not initialised in relocatable
+                only objects, so it cannot be used for length calculations.
+                Instead we use the space_size value which we have been
+                accumulating.  This isn't an accurate estimate since it
+                ignores alignment and ordering issues.  */
+             space_asect->size = space_size;
+           }
        }
     }
   /* Now that we've read in all the subspace records, we need to assign
@@ -2172,7 +2195,7 @@ som_object_p (abfd)
      bfd *abfd;
 {
   struct header file_hdr;
-  struct som_exec_auxhdr aux_hdr;
+  struct som_exec_auxhdr *aux_hdr_ptr = NULL;
   unsigned long current_offset = 0;
   struct lst_header lst_header;
   struct som_entry som_entry;
@@ -2284,11 +2307,14 @@ som_object_p (abfd)
   /* 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)
     {
+      aux_hdr_ptr = bfd_zalloc (abfd, 
+                               (bfd_size_type) sizeof (*aux_hdr_ptr));
+      if (aux_hdr_ptr == NULL)
+       return NULL;
       amt = AUX_HDR_SIZE;
-      if (bfd_bread ((PTR) &aux_hdr, amt, abfd) != amt)
+      if (bfd_bread ((PTR) aux_hdr_ptr, amt, abfd) != amt)
        {
          if (bfd_get_error () != bfd_error_system_call)
            bfd_set_error (bfd_error_wrong_format);
@@ -2304,7 +2330,7 @@ som_object_p (abfd)
     }
 
   /* This appears to be a valid SOM object.  Do some initialization.  */
-  return som_object_setup (abfd, &file_hdr, &aux_hdr, current_offset);
+  return som_object_setup (abfd, &file_hdr, aux_hdr_ptr, current_offset);
 }
 
 /* Create a SOM object.  */
@@ -2427,9 +2453,9 @@ som_prep_headers (abfd)
          som_section_data (section)->subspace_dict->subspace_start =
            section->vma;
          som_section_data (section)->subspace_dict->subspace_length =
-           bfd_section_size (abfd, section);
+           section->size;
          som_section_data (section)->subspace_dict->initialization_length =
-           bfd_section_size (abfd, section);
+           section->size;
          som_section_data (section)->subspace_dict->alignment =
            1 << section->alignment_power;
 
@@ -3067,8 +3093,7 @@ som_write_fixups (abfd, current_offset, total_reloc_sizep)
 
          /* Last BFD relocation for a subspace has been processed.
             Map the rest of the subspace with R_NO_RELOCATION fixups.  */
-         p = som_reloc_skip (abfd, (bfd_section_size (abfd, subsection)
-                                    - reloc_offset),
+         p = som_reloc_skip (abfd, subsection->size - reloc_offset,
                              p, &subspace_reloc_size, reloc_queue);
 
          /* Scribble out the relocations.  */
@@ -3609,22 +3634,22 @@ som_begin_writing (abfd)
              /* Update the size of the code & data.  */
              if (abfd->flags & (EXEC_P | DYNAMIC)
                  && subsection->flags & SEC_CODE)
-               exec_header->exec_tsize += subsection->_cooked_size;
+               exec_header->exec_tsize += subsection->size;
              else if (abfd->flags & (EXEC_P | DYNAMIC)
                       && subsection->flags & SEC_DATA)
-               exec_header->exec_dsize += subsection->_cooked_size;
+               exec_header->exec_dsize += subsection->size;
              som_section_data (subsection)->subspace_dict->file_loc_init_value
                = current_offset;
              subsection->filepos = current_offset;
-             current_offset += bfd_section_size (abfd, subsection);
-             subspace_offset += bfd_section_size (abfd, subsection);
+             current_offset += subsection->size;
+             subspace_offset += subsection->size;
            }
          /* Looks like uninitialized data.  */
          else
            {
              /* Update the size of the bss section.  */
              if (abfd->flags & (EXEC_P | DYNAMIC))
-               exec_header->exec_bsize += subsection->_cooked_size;
+               exec_header->exec_bsize += subsection->size;
 
              som_section_data (subsection)->subspace_dict->file_loc_init_value
                = 0;
@@ -3674,7 +3699,7 @@ som_begin_writing (abfd)
              som_section_data (subsection)->subspace_dict->file_loc_init_value
                = current_offset;
              subsection->filepos = current_offset;
-             current_offset += bfd_section_size (abfd, subsection);
+             current_offset += subsection->size;
            }
          /* Looks like uninitialized data.  */
          else
@@ -3682,7 +3707,7 @@ som_begin_writing (abfd)
              som_section_data (subsection)->subspace_dict->file_loc_init_value
                = 0;
              som_section_data (subsection)->subspace_dict->
-               initialization_length = bfd_section_size (abfd, subsection);
+               initialization_length = subsection->size;
            }
        }
       /* Goto the next section.  */
@@ -4371,7 +4396,7 @@ bfd_section_from_som_symbol (abfd, symbol)
       for (section = abfd->sections; section; section = section->next)
        {
          if (value >= section->vma
-             && value <= section->vma + section->_cooked_size
+             && value <= section->vma + section->size
              && som_is_subspace (section))
            return section;
        }
@@ -4518,11 +4543,6 @@ som_slurp_symbol_table (abfd)
          sym->symbol.value -= sym->symbol.section->vma;
          break;
 
-#if 0
-       /* SS_GLOBAL and SS_LOCAL are two names for the same thing.
-          Sound dumb?  It is.  */
-       case SS_GLOBAL:
-#endif
        case SS_LOCAL:
          sym->symbol.flags |= BSF_LOCAL;
          sym->symbol.section = bfd_section_from_som_symbol (abfd, bufp);
@@ -4961,16 +4981,16 @@ som_set_reloc_info (fixup, end, internal_relocs, section, symbols, just_count)
                      /* Got to read the damn contents first.  We don't
                         bother saving the contents (yet).  Add it one
                         day if the need arises.  */
-                     section->contents = bfd_malloc (section->_raw_size);
-                     if (section->contents == NULL)
-                       return (unsigned) -1;
-
+                     bfd_byte *contents;
+                     if (!bfd_malloc_and_get_section (section->owner, section,
+                                                      &contents))
+                       {
+                         if (contents != NULL)
+                           free (contents);
+                         return (unsigned) -1;
+                       }
+                     section->contents = contents;
                      deallocate_contents = 1;
-                     bfd_get_section_contents (section->owner,
-                                               section,
-                                               section->contents,
-                                               (bfd_vma) 0,
-                                               section->_raw_size);
                    }
                  else if (rptr->addend == 0)
                    rptr->addend = bfd_get_32 (section->owner,
@@ -5232,6 +5252,49 @@ som_bfd_copy_private_bfd_data (ibfd, obfd)
   return TRUE;
 }
 
+/* Display the SOM header.  */
+
+static bfd_boolean
+som_bfd_print_private_bfd_data (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.  */
 
@@ -5405,7 +5468,7 @@ som_get_section_contents (abfd, section, location, offset, count)
 {
   if (count == 0 || ((section->flags & SEC_HAS_CONTENTS) == 0))
     return TRUE;
-  if ((bfd_size_type) (offset+count) > section->_raw_size
+  if ((bfd_size_type) (offset+count) > section->size
       || bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
       || bfd_bread (location, count, abfd) != count)
     return FALSE; /* On error.  */
@@ -6376,7 +6439,7 @@ som_bfd_link_split_section (abfd, sec)
      bfd *abfd ATTRIBUTE_UNUSED;
      asection *sec;
 {
-  return (som_is_subspace (sec) && sec->_raw_size > 240000);
+  return (som_is_subspace (sec) && sec->size > 240000);
 }
 
 #define        som_close_and_cleanup           som_bfd_free_cached_info
@@ -6390,8 +6453,9 @@ som_bfd_link_split_section (abfd, sec)
 #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
 #define som_read_minisymbols           _bfd_generic_read_minisymbols
@@ -6410,7 +6474,10 @@ som_bfd_link_split_section (abfd, sec)
 
 #define som_bfd_gc_sections            bfd_generic_gc_sections
 #define som_bfd_merge_sections         bfd_generic_merge_sections
+#define som_bfd_is_group_section       bfd_generic_is_group_section
 #define som_bfd_discard_group          bfd_generic_discard_group
+#define som_section_already_linked \
+  _bfd_generic_section_already_linked
 
 const bfd_target som_vec = {
   "som",                       /* name */
This page took 0.028818 seconds and 4 git commands to generate.