* h8300-dis.c: Fix formatting.
[binutils-gdb.git] / bfd / elf.c
index 6a4006846022948e92547accb19285f9bbc240d4..c582e3ba6ad98c4e7f4213a6c1c1e23bddde0430 100644 (file)
--- a/bfd/elf.c
+++ b/bfd/elf.c
@@ -1,5 +1,5 @@
 /* ELF executable support for BFD.
-   Copyright 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
+   Copyright 1993, 94, 95, 96, 97, 98, 99, 2000 Free Software Foundation, Inc.
 
 This file is part of BFD, the Binary File Descriptor library.
 
@@ -31,6 +31,10 @@ SECTION
        haven't bothered yet.
  */
 
+#ifdef __sparcv9
+#define _SYSCALL32     /* For Sparc64-cross-32 */
+#endif
+
 #include "bfd.h"
 #include "sysdep.h"
 #include "bfdlink.h"
@@ -977,6 +981,7 @@ _bfd_elf_link_hash_table_init (table, abfd, newfunc)
   table->needed = NULL;
   table->hgot = NULL;
   table->stab_info = NULL;
+  table->dynlocal = NULL;
   return _bfd_link_hash_table_init (&table->root, abfd, newfunc);
 }
 
@@ -1354,8 +1359,9 @@ bfd_section_from_shdr (abfd, shindex)
        target_sect->rel_filepos = hdr->sh_offset;
        /* In the section to which the relocations apply, mark whether
           its relocations are of the REL or RELA variety.  */
-       elf_section_data (target_sect)->use_rela_p 
-         = (hdr->sh_type == SHT_RELA);
+       if (hdr->sh_size != 0)
+         elf_section_data (target_sect)->use_rela_p
+           = (hdr->sh_type == SHT_RELA);
        abfd->flags |= HAS_RELOC;
        return true;
       }
@@ -1752,6 +1758,22 @@ elf_fake_sections (abfd, asect, failedptrarg)
     *failedptr = true;
 }
 
+/* Get elf arch size (32 / 64).
+   Returns -1 if not elf.  */
+
+int
+bfd_elf_get_arch_size (abfd)
+     bfd *abfd;
+{
+  if (abfd->xvec->flavour != bfd_target_elf_flavour)
+    {
+      bfd_set_error (bfd_error_wrong_format);
+      return -1;
+    }
+
+  return (get_elf_backend_data (abfd))->s->arch_size;
+}
+
 /* Assign all ELF section numbers.  The dummy first section is handled here
    too.  The link/info pointers for the standard section types are filled
    in here too, while we're at it.  */
@@ -1764,7 +1786,6 @@ assign_section_numbers (abfd)
   asection *sec;
   unsigned int section_number;
   Elf_Internal_Shdr **i_shdrp;
-  struct elf_backend_data *bed = get_elf_backend_data (abfd);
 
   section_number = 1;
 
@@ -1899,7 +1920,7 @@ assign_section_numbers (abfd)
 
                  /* This is a .stab section.  */
                  elf_section_data (s)->this_hdr.sh_entsize =
-                   4 + 2 * (bed->s->arch_size / 8);
+                   4 + 2 * bfd_elf_get_arch_size (abfd) / 8;
                }
            }
          break;
@@ -2901,18 +2922,21 @@ assign_file_positions_for_segments (abfd)
 
          if (p->p_type == PT_NOTE && bfd_get_format (abfd) == bfd_core)
            {
-             if (i == 0)       /* the actual "note" segment */
-               {               /* this one actually contains everything. */
+             /* The actual "note" segment has i == 0.
+                This is the one that actually contains everything.  */
+             if (i == 0)
+               {
                  sec->filepos = off;
                  p->p_filesz = sec->_raw_size;
                  off += sec->_raw_size;
                  voff = off;
                }
-             else      /* fake sections -- don't need to be written */
+             else
                {
+                 /* Fake sections -- don't need to be written.  */
                  sec->filepos = 0;
                  sec->_raw_size = 0;
-                 flags = sec->flags = 0;       /* no contents */
+                 flags = sec->flags = 0;
                }
              p->p_memsz = 0;
              p->p_align = 1;
@@ -3205,7 +3229,7 @@ prep_headers (abfd)
     bfd_big_endian (abfd) ? ELFDATA2MSB : ELFDATA2LSB;
   i_ehdrp->e_ident[EI_VERSION] = bed->s->ev_current;
 
-  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_SYSV;
+  i_ehdrp->e_ident[EI_OSABI] = ELFOSABI_NONE;
   i_ehdrp->e_ident[EI_ABIVERSION] = 0;
 
   for (count = EI_PAD; count < EI_NIDENT; count++)
@@ -3226,14 +3250,20 @@ prep_headers (abfd)
       i_ehdrp->e_machine = EM_NONE;
       break;
     case bfd_arch_sparc:
-      if (bed->s->arch_size == 64)
+      if (bfd_elf_get_arch_size (abfd) == 64)
        i_ehdrp->e_machine = EM_SPARCV9;
       else
        i_ehdrp->e_machine = EM_SPARC;
       break;
+    case bfd_arch_i370:
+      i_ehdrp->e_machine = EM_S370;
+      break;
     case bfd_arch_i386:
       i_ehdrp->e_machine = EM_386;
       break;
+    case bfd_arch_ia64:
+      i_ehdrp->e_machine = EM_IA_64;
+      break;
     case bfd_arch_m68k:
       i_ehdrp->e_machine = EM_68K;
       break;
@@ -3273,6 +3303,9 @@ prep_headers (abfd)
     case bfd_arch_mcore:
       i_ehdrp->e_machine = EM_MCORE;
       break;
+    case bfd_arch_avr:
+      i_ehdrp->e_machine = EM_AVR;
+      break;
     case bfd_arch_v850:
       switch (bfd_get_mach (abfd))
        {
@@ -3720,7 +3753,7 @@ copy_private_bfd_data (ibfd, obfd)
         more to do.  */
 
       isec = 0;
-      matching_lma = false;
+      matching_lma = 0;
       suggested_lma = 0;
 
       for (j = 0, s = ibfd->sections; s != NULL; s = s->next)
@@ -3786,21 +3819,32 @@ copy_private_bfd_data (ibfd, obfd)
          free (sections);
          continue;
        }
-      else if (matching_lma != 0)
-       {
-         /* At least one section fits inside the current segment.
-            Keep it, but modify its physical address to match the
-            LMA of the first section that fitted.  */
-
-         m->p_paddr = matching_lma;
-       }
       else
        {
-         /* None of the sections fitted inside the current segment.
-            Change the current segment's physical address to match
-            the LMA of the first section.  */
+         if (matching_lma != 0)
+           {
+             /* At least one section fits inside the current segment.
+                Keep it, but modify its physical address to match the
+                LMA of the first section that fitted.  */
 
-         m->p_paddr = suggested_lma;
+             m->p_paddr = matching_lma;
+           }
+         else
+           {
+             /* None of the sections fitted inside the current segment.
+                Change the current segment's physical address to match
+                the LMA of the first section.  */
+
+             m->p_paddr = suggested_lma;
+           }
+
+         /* Offset the segment physical address from the lma to allow
+            for space taken up by elf headers.  */
+         if (m->includes_filehdr)
+           m->p_paddr -= iehdr->e_ehsize;
+
+         if (m->includes_phdrs)
+           m->p_paddr -= iehdr->e_phnum * iehdr->e_phentsize;
        }
 
       /* Step Three: Loop over the sections again, this time assigning
@@ -3833,7 +3877,12 @@ copy_private_bfd_data (ibfd, obfd)
                    {
                      /* If the first section in a segment does not start at
                         the beginning of the segment, then something is wrong.  */
-                     if (os->lma != m->p_paddr)
+                     if (os->lma != (m->p_paddr
+                                     + (m->includes_filehdr
+                                        ? iehdr->e_ehsize : 0)
+                                     + (m->includes_phdrs
+                                        ? iehdr->e_phnum * iehdr->e_phentsize
+                                        : 0)))
                        abort ();
                    }
                  else
@@ -4221,7 +4270,8 @@ swap_out_syms (abfd, sttp, relocatable_p)
          type = STT_NOTYPE;
 
         /* Processor-specific types */
-        if (bed->elf_backend_get_symbol_type)
+        if (type_ptr != NULL
+           && bed->elf_backend_get_symbol_type)
           type = (*bed->elf_backend_get_symbol_type) (&type_ptr->internal_elf_sym, type);
 
        if (flags & BSF_SECTION_SYM)
@@ -4463,18 +4513,13 @@ _bfd_elf_slurp_version_tables (abfd)
       Elf_Internal_Shdr *hdr;
       Elf_External_Verdef *everdef;
       Elf_Internal_Verdef *iverdef;
+      Elf_Internal_Verdef *iverdefarr;
+      Elf_Internal_Verdef iverdefmem;
       unsigned int i;
+      unsigned int maxidx;
 
       hdr = &elf_tdata (abfd)->dynverdef_hdr;
 
-      elf_tdata (abfd)->verdef =
-       ((Elf_Internal_Verdef *)
-        bfd_zalloc (abfd, hdr->sh_info * sizeof (Elf_Internal_Verdef)));
-      if (elf_tdata (abfd)->verdef == NULL)
-       goto error_return;
-
-      elf_tdata (abfd)->cverdefs = hdr->sh_info;
-
       contents = (bfd_byte *) bfd_malloc (hdr->sh_size);
       if (contents == NULL)
        goto error_return;
@@ -4482,15 +4527,42 @@ _bfd_elf_slurp_version_tables (abfd)
          || bfd_read ((PTR) contents, 1, hdr->sh_size, abfd) != hdr->sh_size)
        goto error_return;
 
+      /* We know the number of entries in the section but not the maximum
+        index.  Therefore we have to run through all entries and find
+        the maximum.  */
+      everdef = (Elf_External_Verdef *) contents;
+      maxidx = 0;
+      for (i = 0; i < hdr->sh_info; ++i)
+       {
+         _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
+
+         if ((iverdefmem.vd_ndx & VERSYM_VERSION) > maxidx)
+           maxidx = iverdefmem.vd_ndx & VERSYM_VERSION;
+
+         everdef = ((Elf_External_Verdef *)
+                    ((bfd_byte *) everdef + iverdefmem.vd_next));
+       }
+
+      elf_tdata (abfd)->verdef =
+       ((Elf_Internal_Verdef *)
+        bfd_zalloc (abfd, maxidx * sizeof (Elf_Internal_Verdef)));
+      if (elf_tdata (abfd)->verdef == NULL)
+       goto error_return;
+
+      elf_tdata (abfd)->cverdefs = maxidx;
+
       everdef = (Elf_External_Verdef *) contents;
-      iverdef = elf_tdata (abfd)->verdef;
-      for (i = 0; i < hdr->sh_info; i++, iverdef++)
+      iverdefarr = elf_tdata (abfd)->verdef;
+      for (i = 0; i < hdr->sh_info; i++)
        {
          Elf_External_Verdaux *everdaux;
          Elf_Internal_Verdaux *iverdaux;
          unsigned int j;
 
-         _bfd_elf_swap_verdef_in (abfd, everdef, iverdef);
+         _bfd_elf_swap_verdef_in (abfd, everdef, &iverdefmem);
+
+         iverdef = &iverdefarr[(iverdefmem.vd_ndx & VERSYM_VERSION) - 1];
+         memcpy (iverdef, &iverdefmem, sizeof (Elf_Internal_Verdef));
 
          iverdef->vd_bfd = abfd;
 
@@ -5041,7 +5113,7 @@ elfcore_maybe_make_sect (abfd, name, sect)
 
 
 /* prstatus_t exists on:
-     solaris 2.[567]
+     solaris 2.5+
      linux 2.[01] + glibc
      unixware 4.2
 */
@@ -5052,28 +5124,60 @@ elfcore_grok_prstatus (abfd, note)
      bfd* abfd;
      Elf_Internal_Note* note;
 {
-  prstatus_t prstat;
   char buf[100];
   char* name;
   asection* sect;
+  int raw_size;
 
-  if (note->descsz != sizeof (prstat))
-    return true;
+  if (note->descsz == sizeof (prstatus_t))
+    {
+      prstatus_t prstat;
+
+      raw_size = sizeof (prstat.pr_reg);
+      memcpy (&prstat, note->descdata, sizeof (prstat));
+
+      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
+      elf_tdata (abfd)->core_pid = prstat.pr_pid;
+
+      /* pr_who exists on:
+        solaris 2.5+
+        unixware 4.2
+        pr_who doesn't exist on:
+        linux 2.[01]
+        */
+#if defined (HAVE_PRSTATUS_T_PR_WHO)
+      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
+#endif
+    }
+#if defined (__sparcv9)
+  else if (note->descsz == sizeof (prstatus32_t))
+    {
+      /* 64-bit host, 32-bit corefile */
+      prstatus32_t prstat;
 
-  memcpy (&prstat, note->descdata, sizeof (prstat));
+      raw_size = sizeof (prstat.pr_reg);
+      memcpy (&prstat, note->descdata, sizeof (prstat));
 
-  elf_tdata (abfd)->core_signal = prstat.pr_cursig;
-  elf_tdata (abfd)->core_pid = prstat.pr_pid;
+      elf_tdata (abfd)->core_signal = prstat.pr_cursig;
+      elf_tdata (abfd)->core_pid = prstat.pr_pid;
 
-  /* pr_who exists on:
-       solaris 2.[567]
-       unixware 4.2
-     pr_who doesn't exist on:
-       linux 2.[01]
-  */
+      /* pr_who exists on:
+        solaris 2.5+
+        unixware 4.2
+        pr_who doesn't exist on:
+        linux 2.[01]
+        */
 #if defined (HAVE_PRSTATUS_T_PR_WHO)
-  elf_tdata (abfd)->core_lwpid = prstat.pr_who;
+      elf_tdata (abfd)->core_lwpid = prstat.pr_who;
 #endif
+    }
+#endif /* __sparcv9 */
+  else
+    {
+      /* Fail - we don't know how to handle any other
+        note size (ie. data object type).  */
+      return true;
+    }
 
   /* Make a ".reg/999" section. */
 
@@ -5086,8 +5190,20 @@ elfcore_grok_prstatus (abfd, note)
   sect = bfd_make_section (abfd, name);
   if (sect == NULL)
     return false;
-  sect->_raw_size = sizeof (prstat.pr_reg);
-  sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
+
+  if (note->descsz == sizeof (prstatus_t))
+    {
+      sect->_raw_size = raw_size;
+      sect->filepos = note->descpos + offsetof (prstatus_t, pr_reg);
+    }
+#if defined (__sparcv9)
+  else if (note->descsz == sizeof (prstatus32_t))
+    {
+      sect->_raw_size = raw_size;
+      sect->filepos = note->descpos + offsetof (prstatus32_t, pr_reg);
+    }
+#endif
+
   sect->flags = SEC_HAS_CONTENTS;
   sect->alignment_power = 2;
 
@@ -5165,11 +5281,17 @@ elfcore_grok_prxfpreg (abfd, note)
 
 
 #if defined (HAVE_PRPSINFO_T)
-# define elfcore_psinfo_t prpsinfo_t
+typedef prpsinfo_t   elfcore_psinfo_t;
+#if defined (__sparcv9)        /* Sparc64 cross Sparc32 */
+typedef prpsinfo32_t elfcore_psinfo32_t;
+#endif
 #endif
 
 #if defined (HAVE_PSINFO_T)
-# define elfcore_psinfo_t psinfo_t
+typedef psinfo_t   elfcore_psinfo_t;
+#if defined (__sparcv9)        /* Sparc64 cross Sparc32 */
+typedef psinfo32_t elfcore_psinfo32_t;
+#endif
 #endif
 
 
@@ -5209,18 +5331,40 @@ elfcore_grok_psinfo (abfd, note)
      bfd* abfd;
      Elf_Internal_Note* note;
 {
-  elfcore_psinfo_t psinfo;
+  if (note->descsz == sizeof (elfcore_psinfo_t))
+    {
+      elfcore_psinfo_t psinfo;
 
-  if (note->descsz != sizeof (elfcore_psinfo_t))
-    return true;
+      memcpy (&psinfo, note->descdata, note->descsz);
 
-  memcpy (&psinfo, note->descdata, note->descsz);
+      elf_tdata (abfd)->core_program
+       = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
 
-  elf_tdata (abfd)->core_program
-    = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
+      elf_tdata (abfd)->core_command
+       = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
+    }
+#if defined (__sparcv9)
+  else if (note->descsz == sizeof (elfcore_psinfo32_t))
+    {
+      /* 64-bit host, 32-bit corefile */
+      elfcore_psinfo32_t psinfo;
+
+      memcpy (&psinfo, note->descdata, note->descsz);
 
-  elf_tdata (abfd)->core_command
-    = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
+      elf_tdata (abfd)->core_program
+       = elfcore_strndup (abfd, psinfo.pr_fname, sizeof (psinfo.pr_fname));
+
+      elf_tdata (abfd)->core_command
+       = elfcore_strndup (abfd, psinfo.pr_psargs, sizeof (psinfo.pr_psargs));
+    }
+#endif
+
+  else
+    {
+      /* Fail - we don't know how to handle any other
+        note size (ie. data object type).  */
+      return true;
+    }
 
   /* Note that for some reason, a spurious space is tacked
      onto the end of the args in some (at least one anyway)
@@ -5245,15 +5389,25 @@ elfcore_grok_pstatus (abfd, note)
      bfd* abfd;
      Elf_Internal_Note* note;
 {
-  pstatus_t pstat;
+  if (note->descsz == sizeof (pstatus_t))
+    {
+      pstatus_t pstat;
 
-  if (note->descsz != sizeof (pstat))
-    return true;
+      memcpy (&pstat, note->descdata, sizeof (pstat));
 
-  memcpy (&pstat, note->descdata, sizeof (pstat));
+      elf_tdata (abfd)->core_pid = pstat.pr_pid;
+    }
+#if defined (__sparcv9)
+  else if (note->descsz == sizeof (pstatus32_t))
+    {
+      /* 64-bit host, 32-bit corefile */
+      pstatus32_t pstat;
 
-  elf_tdata (abfd)->core_pid = pstat.pr_pid;
+      memcpy (&pstat, note->descdata, sizeof (pstat));
 
+      elf_tdata (abfd)->core_pid = pstat.pr_pid;
+    }
+#endif
   /* Could grab some more details from the "representative"
      lwpstatus_t in pstat.pr_lwp, but we'll catch it all in an
      NT_LWPSTATUS note, presumably. */