OSDN Git Service

* coffcode.h (coff_write_object_contents): Always initialise
[pf3gnuchains/pf3gnuchains4x.git] / bfd / coffcode.h
index aaf79c1..12d2ad5 100644 (file)
@@ -1,6 +1,6 @@
 /* Support for the generic parts of most COFF variants, for BFD.
    Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
-   2000, 2001, 2002, 2003, 2004, 2005
+   2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
    Free Software Foundation, Inc.
    Written by Cygnus Support.
 
@@ -8,7 +8,7 @@
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -18,7 +18,8 @@
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
 /* Most of this hacked by  Steve Chamberlain,
                        sac@cygnus.com.  */
@@ -170,7 +171,7 @@ SUBSUBSECTION
        At this time the symbol names are moved around. Coff stores
        all symbols less than nine characters long physically
        within the symbol table; longer strings are kept at the end of
-       the file in the string  table. This pass moves all strings
+       the file in the string table. This pass moves all strings
        into memory and replaces them with pointers to the strings.
 
        The symbol table is massaged once again, this time to create
@@ -427,20 +428,20 @@ sec_to_styp_flags (const char *sec_name, flagword sec_flags)
       styp_flags = STYP_LIT;
 #endif /* _LIT */
     }
-  else if (!strncmp (sec_name, DOT_DEBUG, sizeof (DOT_DEBUG) - 1))
+  else if (CONST_STRNEQ (sec_name, DOT_DEBUG))
     {
       /* Handle the XCOFF debug section and DWARF2 debug sections.  */
       if (!sec_name[6])
-        styp_flags = STYP_XCOFF_DEBUG;
+       styp_flags = STYP_XCOFF_DEBUG;
       else
-        styp_flags = STYP_DEBUG_INFO;
+       styp_flags = STYP_DEBUG_INFO;
     }
-  else if (!strncmp (sec_name, ".stab", 5))
+  else if (CONST_STRNEQ (sec_name, ".stab"))
     {
       styp_flags = STYP_DEBUG_INFO;
     }
 #ifdef COFF_LONG_SECTION_NAMES
-  else if (!strncmp (sec_name, GNU_LINKONCE_WI, sizeof (GNU_LINKONCE_WI) - 1))
+  else if (CONST_STRNEQ (sec_name, GNU_LINKONCE_WI))
     {
       styp_flags = STYP_DEBUG_INFO;
     }
@@ -529,8 +530,8 @@ sec_to_styp_flags (const char *sec_name, flagword sec_flags)
      but there are more IMAGE_SCN_* flags.  */
 
   /* FIXME: There is no gas syntax to specify the debug section flag.  */
-  if (strncmp (sec_name, DOT_DEBUG, sizeof (DOT_DEBUG) - 1) == 0
-      || strncmp (sec_name, GNU_LINKONCE_WI, sizeof (GNU_LINKONCE_WI) - 1) == 0)
+  if (CONST_STRNEQ (sec_name, DOT_DEBUG)
+      || CONST_STRNEQ (sec_name, GNU_LINKONCE_WI))
     sec_flags = SEC_DEBUGGING;
 
   /* skip LOAD */
@@ -674,14 +675,14 @@ styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
 #endif
        sec_flags |= SEC_ALLOC;
     }
-  else if (strncmp (name, DOT_DEBUG, sizeof (DOT_DEBUG) - 1) == 0
+  else if (CONST_STRNEQ (name, DOT_DEBUG)
 #ifdef _COMMENT
           || strcmp (name, _COMMENT) == 0
 #endif
 #ifdef COFF_LONG_SECTION_NAMES
-          || strncmp (name, GNU_LINKONCE_WI, sizeof (GNU_LINKONCE_WI) - 1) == 0
+          || CONST_STRNEQ (name, GNU_LINKONCE_WI)
 #endif
-          || strncmp (name, ".stab", 5) == 0)
+          || CONST_STRNEQ (name, ".stab"))
     {
 #ifdef COFF_PAGE_SIZE
       sec_flags |= SEC_DEBUGGING;
@@ -715,7 +716,7 @@ styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
      The symbols will be defined as weak, so that multiple definitions
      are permitted.  The GNU linker extension is to actually discard
      all but one of the sections.  */
-  if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
+  if (CONST_STRNEQ (name, ".gnu.linkonce"))
     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
 #endif
 
@@ -1071,7 +1072,7 @@ styp_to_sec_flags (bfd *abfd,
          /* The MS PE spec sets the DISCARDABLE flag on .reloc sections
             but we do not want them to be labelled as debug section, since
             then strip would remove them.  */
-         if (strncmp (name, ".reloc", sizeof ".reloc" - 1) != 0)
+         if (! CONST_STRNEQ (name, ".reloc"))
            sec_flags |= SEC_DEBUGGING;
          break;
        case IMAGE_SCN_MEM_SHARED:
@@ -1126,7 +1127,7 @@ styp_to_sec_flags (bfd *abfd,
      The symbols will be defined as weak, so that multiple definitions
      are permitted.  The GNU linker extension is to actually discard
      all but one of the sections.  */
-  if (strncmp (name, ".gnu.linkonce", sizeof ".gnu.linkonce" - 1) == 0)
+  if (CONST_STRNEQ (name, ".gnu.linkonce"))
     sec_flags |= SEC_LINK_ONCE | SEC_LINK_DUPLICATES_DISCARD;
 #endif
 
@@ -1291,6 +1292,9 @@ Special entry points for gdb to swap in coff symbol table parts:
 .  bfd_boolean (*_bfd_coff_final_link_postscript)
 .    (bfd *, struct coff_final_link_info *);
 .
+.  bfd_boolean (*_bfd_coff_print_pdata)
+.    (bfd *, void *);
+.
 .} bfd_coff_backend_data;
 .
 .#define coff_backend_info(abfd) \
@@ -1423,6 +1427,11 @@ Special entry points for gdb to swap in coff symbol table parts:
 .#define bfd_coff_final_link_postscript(a,p) \
 .  ((coff_backend_info (a)->_bfd_coff_final_link_postscript) (a, p))
 .
+.#define bfd_coff_have_print_pdata(a) \
+.  (coff_backend_info (a)->_bfd_coff_print_pdata)
+.#define bfd_coff_print_pdata(a,p) \
+.  ((coff_backend_info (a)->_bfd_coff_print_pdata) (a, p))
+.
 */
 
 /* See whether the magic number matches.  */
@@ -1560,6 +1569,10 @@ coff_new_section_hook (bfd * abfd, asection * section)
     section->alignment_power = bfd_xcoff_data_align_power (abfd);
 #endif
 
+  /* Set up the section symbol.  */
+  if (!_bfd_generic_new_section_hook (abfd, section))
+    return FALSE;
+
   /* Allocate aux records for section symbols, to store size and
      related info.
 
@@ -1623,19 +1636,6 @@ coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
 #else /* ! COFF_ALIGN_IN_SECTION_HEADER */
 #ifdef COFF_WITH_PE
 
-/* A couple of macros to help setting the alignment power field.  */
-#define ALIGN_SET(field, x, y) \
-  if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x)\
-    {\
-      section->alignment_power = y;\
-    }
-
-#define ELIFALIGN_SET(field, x, y) \
-  else if (((field) & IMAGE_SCN_ALIGN_64BYTES) == x) \
-    {\
-      section->alignment_power = y;\
-    }
-
 static void
 coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
                         asection * section,
@@ -1643,14 +1643,31 @@ coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
 {
   struct internal_scnhdr *hdr = (struct internal_scnhdr *) scnhdr;
   bfd_size_type amt;
+  unsigned int alignment_power_const
+    = hdr->s_flags & IMAGE_SCN_ALIGN_POWER_BIT_MASK;
 
-  ALIGN_SET     (hdr->s_flags, IMAGE_SCN_ALIGN_64BYTES, 6)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_32BYTES, 5)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_16BYTES, 4)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_8BYTES,  3)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_4BYTES,  2)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_2BYTES,  1)
-  ELIFALIGN_SET (hdr->s_flags, IMAGE_SCN_ALIGN_1BYTES,  0)
+  switch (alignment_power_const)
+    {
+    case IMAGE_SCN_ALIGN_8192BYTES:
+    case IMAGE_SCN_ALIGN_4096BYTES:
+    case IMAGE_SCN_ALIGN_2048BYTES:
+    case IMAGE_SCN_ALIGN_1024BYTES:
+    case IMAGE_SCN_ALIGN_512BYTES:
+    case IMAGE_SCN_ALIGN_256BYTES:
+    case IMAGE_SCN_ALIGN_128BYTES:
+    case IMAGE_SCN_ALIGN_64BYTES:
+    case IMAGE_SCN_ALIGN_32BYTES:
+    case IMAGE_SCN_ALIGN_16BYTES:
+    case IMAGE_SCN_ALIGN_8BYTES:
+    case IMAGE_SCN_ALIGN_4BYTES:
+    case IMAGE_SCN_ALIGN_2BYTES:
+    case IMAGE_SCN_ALIGN_1BYTES:
+      section->alignment_power
+       = IMAGE_SCN_ALIGN_POWER_NUM (alignment_power_const);
+      break;
+    default:
+      break;
+    }
 
   /* In a PE image file, the s_paddr field holds the virtual size of a
      section, while the s_size field holds the raw size.  We also keep
@@ -1685,7 +1702,7 @@ coff_set_alignment_hook (bfd * abfd ATTRIBUTE_UNUSED,
       struct internal_reloc n;
       file_ptr oldpos = bfd_tell (abfd);
       bfd_size_type relsz = bfd_coff_relsz (abfd);
-      
+
       bfd_seek (abfd, (file_ptr) hdr->s_relptr, 0);
       if (bfd_bread (& dst, relsz, abfd) != relsz)
        return;
@@ -1878,9 +1895,15 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
 #ifdef I386MAGIC
     case I386MAGIC:
     case I386PTXMAGIC:
-    case I386AIXMAGIC:         /* Danbury PS/2 AIX C Compiler */
-    case LYNXCOFFMAGIC:        /* shadows the m68k Lynx number below, sigh */
+    case I386AIXMAGIC:         /* Danbury PS/2 AIX C Compiler.  */
+    case LYNXCOFFMAGIC:                /* Shadows the m68k Lynx number below, sigh.  */
+      arch = bfd_arch_i386;
+      break;
+#endif
+#ifdef AMD64MAGIC
+    case AMD64MAGIC:
       arch = bfd_arch_i386;
+      machine = bfd_mach_x86_64;
       break;
 #endif
 #ifdef IA64MAGIC
@@ -1935,15 +1958,15 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
     case MAXQ20MAGIC:
       arch = bfd_arch_maxq;
       switch (internal_f->f_flags & F_MACHMASK)
-       { 
-        case F_MAXQ10:
-          machine = bfd_mach_maxq10;
-          break;
-        case F_MAXQ20:
-          machine = bfd_mach_maxq20;
-          break;
-        default:
-          return FALSE;
+       {
+       case F_MAXQ10:
+         machine = bfd_mach_maxq10;
+         break;
+       case F_MAXQ20:
+         machine = bfd_mach_maxq20;
+         break;
+       default:
+         return FALSE;
        }
       break;
 #endif
@@ -2208,20 +2231,20 @@ coff_set_arch_mach_hook (bfd *abfd, void * filehdr)
     case TICOFF1MAGIC:
     case TICOFF2MAGIC:
       switch (internal_f->f_target_id)
-        {
+       {
 #ifdef TI_TARGET_ID
-        case TI_TARGET_ID:
-          arch = TICOFF_TARGET_ARCH;
+       case TI_TARGET_ID:
+         arch = TICOFF_TARGET_ARCH;
          machine = TICOFF_TARGET_MACHINE_GET (internal_f->f_flags);
-          break;
-#endif
-        default:
-          arch = bfd_arch_obscure;
-          (*_bfd_error_handler)
-            (_("Unrecognized TI COFF target id '0x%x'"),
-             internal_f->f_target_id);
-          break;
-        }
+         break;
+#endif
+       default:
+         arch = bfd_arch_obscure;
+         (*_bfd_error_handler)
+           (_("Unrecognized TI COFF target id '0x%x'"),
+            internal_f->f_target_id);
+         break;
+       }
       break;
 #endif
 
@@ -2295,7 +2318,7 @@ coff_pointerize_aux_hook (bfd *abfd ATTRIBUTE_UNUSED,
        }
 
       /* Return TRUE to indicate that the caller should not do any
-         further work on this auxent.  */
+        further work on this auxent.  */
       return TRUE;
     }
 
@@ -2518,7 +2541,7 @@ coff_write_relocs (bfd * abfd, int first_undef)
            if (q->sym_ptr_ptr)
              {
 #ifdef SECTION_RELATIVE_ABSOLUTE_SYMBOL_P
-                if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
+               if (SECTION_RELATIVE_ABSOLUTE_SYMBOL_P (q, s))
 #else
                if ((*q->sym_ptr_ptr)->section == bfd_abs_section_ptr
                    && ((*q->sym_ptr_ptr)->flags & BSF_SECTION_SYM) != 0)
@@ -2528,11 +2551,15 @@ coff_write_relocs (bfd * abfd, int first_undef)
                else
                  {
                    n.r_symndx = get_index ((*(q->sym_ptr_ptr)));
-                   /* Take notice if the symbol reloc points to a symbol
-                      we don't have in our symbol table.  What should we
-                      do for this??  */
+                   /* Check to see if the symbol reloc points to a symbol
+                      we don't have in our symbol table.  */
                    if (n.r_symndx > obj_conv_table_size (abfd))
-                     abort ();
+                     {
+                       bfd_set_error (bfd_error_bad_value);
+                       _bfd_error_handler (_("%B: reloc against a non-existant symbol index: %ld"),
+                                           abfd, n.r_symndx);
+                       return FALSE;
+                     }
                  }
              }
 
@@ -2621,7 +2648,7 @@ coff_set_flags (bfd * abfd,
          case bfd_mach_i960_ka_sa: flags = F_I960KA;   break;
          case bfd_mach_i960_jx:    flags = F_I960JX;   break;
          case bfd_mach_i960_hx:    flags = F_I960HX;   break;
-         default:                  return FALSE;
+         default:                  return FALSE;
          }
        *flagsp = flags;
        return TRUE;
@@ -2639,25 +2666,25 @@ coff_set_flags (bfd * abfd,
     case TICOFF_TARGET_ARCH:
       /* If there's no indication of which version we want, use the default.  */
       if (!abfd->xvec )
-        *magicp = TICOFF_DEFAULT_MAGIC;
+       *magicp = TICOFF_DEFAULT_MAGIC;
       else
-        {
-          /* We may want to output in a different COFF version.  */
-          switch (abfd->xvec->name[4])
-            {
-            case '0':
-              *magicp = TICOFF0MAGIC;
-              break;
-            case '1':
-              *magicp = TICOFF1MAGIC;
-              break;
-            case '2':
-              *magicp = TICOFF2MAGIC;
-              break;
-            default:
-              return FALSE;
-            }
-        }
+       {
+         /* We may want to output in a different COFF version.  */
+         switch (abfd->xvec->name[4])
+           {
+           case '0':
+             *magicp = TICOFF0MAGIC;
+             break;
+           case '1':
+             *magicp = TICOFF1MAGIC;
+             break;
+           case '2':
+             *magicp = TICOFF2MAGIC;
+             break;
+           default:
+             return FALSE;
+           }
+       }
       TICOFF_TARGET_MACHINE_SET (flagsp, bfd_get_mach (abfd));
       return TRUE;
 #endif
@@ -2713,13 +2740,18 @@ coff_set_flags (bfd * abfd,
       return TRUE;
 #endif
 
-#ifdef I386MAGIC
+#if defined(I386MAGIC) || defined(AMD64MAGIC)
     case bfd_arch_i386:
+#if defined(I386MAGIC)
       *magicp = I386MAGIC;
-#ifdef LYNXOS
+#endif
+#if defined LYNXOS
       /* Just overwrite the usual value if we're doing Lynx.  */
       *magicp = LYNXCOFFMAGIC;
 #endif
+#if defined AMD64MAGIC
+      *magicp = AMD64MAGIC;
+#endif
       return TRUE;
 #endif
 
@@ -2841,9 +2873,9 @@ coff_set_flags (bfd * abfd,
 #ifdef OR32_MAGIC_BIG
     case bfd_arch_or32:
       if (bfd_big_endian (abfd))
-        * magicp = OR32_MAGIC_BIG;
+       * magicp = OR32_MAGIC_BIG;
       else
-        * magicp = OR32_MAGIC_LITTLE;
+       * magicp = OR32_MAGIC_LITTLE;
       return TRUE;
 #endif
 
@@ -2975,7 +3007,7 @@ coff_compute_section_file_positions (bfd * abfd)
       /* If no file alignment has been set, default to one.
         This repairs 'ld -r' for arm-wince-pe target.  */
       if (page_size == 0)
-        page_size = 1;
+       page_size = 1;
     }
   else
     page_size = PE_DEF_FILE_ALIGNMENT;
@@ -3210,8 +3242,8 @@ coff_compute_section_file_positions (bfd * abfd)
 
 #ifdef COFF_IMAGE_WITH_PE
       /* For PE we need to make sure we pad out to the aligned
-         size, in case the caller only writes out data to the
-         unaligned size.  */
+        size, in case the caller only writes out data to the
+        unaligned size.  */
       if (pei_section_data (abfd, current)->virt_size < current->size)
        align_adjust = TRUE;
 #endif
@@ -3469,7 +3501,7 @@ coff_write_object_contents (bfd * abfd)
 
 #ifdef COFF_LONG_SECTION_NAMES
       /* Handle long section names as in PE.  This must be compatible
-         with the code in coff_write_symbols and _bfd_coff_final_link.  */
+        with the code in coff_write_symbols and _bfd_coff_final_link.  */
       {
        size_t len;
 
@@ -3496,6 +3528,8 @@ coff_write_object_contents (bfd * abfd)
       section.s_size =  current->size;
 #ifdef coff_get_section_load_page
       section.s_page = coff_get_section_load_page (current);
+#else
+      section.s_page = 0;
 #endif
 
 #ifdef COFF_WITH_PE
@@ -3587,8 +3621,8 @@ coff_write_object_contents (bfd * abfd)
 
 #ifdef COFF_WITH_PE
       /* PE stores COMDAT section information in the symbol table.  If
-         this section is supposed to have some COMDAT info, track down
-         the symbol in the symbol table and modify it.  */
+        this section is supposed to have some COMDAT info, track down
+        the symbol in the symbol table and modify it.  */
       if ((current->flags & SEC_LINK_ONCE) != 0)
        {
          unsigned int i, count;
@@ -3604,7 +3638,7 @@ coff_write_object_contents (bfd * abfd)
                continue;
 
              /* Remember the location of the first symbol in this
-                 section.  */
+                section.  */
              if (psymsec == NULL)
                psymsec = psym;
 
@@ -3659,12 +3693,12 @@ coff_write_object_contents (bfd * abfd)
                }
 
              /* The COMDAT symbol must be the first symbol from this
-                 section in the symbol table.  In order to make this
-                 work, we move the COMDAT symbol before the first
-                 symbol we found in the search above.  It's OK to
-                 rearrange the symbol table at this point, because
-                 coff_renumber_symbols is going to rearrange it
-                 further and fix up all the aux entries.  */
+                section in the symbol table.  In order to make this
+                work, we move the COMDAT symbol before the first
+                symbol we found in the search above.  It's OK to
+                rearrange the symbol table at this point, because
+                coff_renumber_symbols is going to rearrange it
+                further and fix up all the aux entries.  */
              if (psym != psymsec)
                {
                  asymbol *hold;
@@ -3751,6 +3785,7 @@ coff_write_object_contents (bfd * abfd)
     internal_f.f_flags |= IMAGE_FILE_LARGE_ADDRESS_AWARE;
 #endif
 
+#ifndef COFF_WITH_pex64
 #ifdef COFF_WITH_PE
   internal_f.f_flags |= IMAGE_FILE_32BIT_MACHINE;
 #else
@@ -3759,6 +3794,7 @@ coff_write_object_contents (bfd * abfd)
   else
     internal_f.f_flags |= F_AR32W;
 #endif
+#endif
 
 #ifdef TI_TARGET_ID
   /* Target id is used in TI COFF v1 and later; COFF0 won't use this field,
@@ -3852,16 +3888,18 @@ coff_write_object_contents (bfd * abfd)
 
 #if defined(I386)
 #define __A_MAGIC_SET__
-#if defined(LYNXOS)
+#if defined LYNXOS
     internal_a.magic = LYNXCOFFMAGIC;
-#else  /* LYNXOS */
+#elif defined AMD64
+    internal_a.magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC;
+#else
     internal_a.magic = ZMAGIC;
-#endif /* LYNXOS */
+#endif
 #endif /* I386 */
 
 #if defined(IA64)
 #define __A_MAGIC_SET__
-    internal_a.magic = ZMAGIC;
+    internal_a.magic = PE32PMAGIC;
 #endif /* IA64 */
 
 #if defined(SPARC)
@@ -3897,7 +3935,7 @@ coff_write_object_contents (bfd * abfd)
 #define __A_MAGIC_SET__
       internal_a.magic = MAXQ20MAGIC;
 #endif
+
 #ifndef __A_MAGIC_SET__
 #include "Your aouthdr magic number is not being set!"
 #else
@@ -3929,7 +3967,7 @@ coff_write_object_contents (bfd * abfd)
   else if (long_section_names && ! obj_coff_strings_written (abfd))
     {
       /* If we have long section names we have to write out the string
-         table even if there are no symbols.  */
+        table even if there are no symbols.  */
       if (! coff_write_symbols (abfd))
        return FALSE;
     }
@@ -3941,7 +3979,7 @@ coff_write_object_contents (bfd * abfd)
       bfd_byte b;
 
       /* PowerPC PE appears to require that all executable files be
-         rounded up to the page size.  */
+        rounded up to the page size.  */
       b = 0;
       if (bfd_seek (abfd,
                    (file_ptr) BFD_ALIGN (sym_base, COFF_PAGE_SIZE) - 1,
@@ -3960,7 +3998,7 @@ coff_write_object_contents (bfd * abfd)
       internal_f.f_symptr = sym_base;
 #ifdef RS6000COFF_C
       /* AIX appears to require that F_RELFLG not be set if there are
-         local symbols but no relocations.  */
+        local symbols but no relocations.  */
       internal_f.f_flags &=~ F_RELFLG;
 #endif
     }
@@ -4229,84 +4267,160 @@ SUBSUBSECTION
        How does this work ?
 */
 
+static int
+coff_sort_func_alent (const void * arg1, const void * arg2)
+{
+  const alent *al1 = *(const alent **) arg1;
+  const alent *al2 = *(const alent **) arg2;
+  const coff_symbol_type *s1 = (const coff_symbol_type *) (al1->u.sym);
+  const coff_symbol_type *s2 = (const coff_symbol_type *) (al2->u.sym);
+
+  if (s1->symbol.value < s2->symbol.value)
+    return -1;
+  else if (s1->symbol.value > s2->symbol.value)
+    return 1;
+
+  return 0;
+}
+
 static bfd_boolean
 coff_slurp_line_table (bfd *abfd, asection *asect)
 {
   LINENO *native_lineno;
   alent *lineno_cache;
   bfd_size_type amt;
+  unsigned int counter;
+  alent *cache_ptr;
+  bfd_vma prev_offset = 0;
+  int ordered = 1;
+  unsigned int nbr_func;
+  LINENO *src;
 
   BFD_ASSERT (asect->lineno == NULL);
 
+  amt = ((bfd_size_type) asect->lineno_count + 1) * sizeof (alent);
+  lineno_cache = bfd_alloc (abfd, amt);
+  if (lineno_cache == NULL)
+    return FALSE;
+
   amt = (bfd_size_type) bfd_coff_linesz (abfd) * asect->lineno_count;
   native_lineno = (LINENO *) buy_and_read (abfd, asect->line_filepos, amt);
   if (native_lineno == NULL)
     {
       (*_bfd_error_handler)
-        (_("%B: warning: line number table read failed"), abfd);
+       (_("%B: warning: line number table read failed"), abfd);
+      bfd_release (abfd, lineno_cache);
       return FALSE;
     }
-  amt = ((bfd_size_type) asect->lineno_count + 1) * sizeof (alent);
-  lineno_cache = bfd_alloc (abfd, amt);
-  if (lineno_cache == NULL)
-    return FALSE;
-  else
+
+  cache_ptr = lineno_cache;
+  asect->lineno = lineno_cache;
+  src = native_lineno;
+  nbr_func = 0;
+
+  for (counter = 0; counter < asect->lineno_count; counter++)
     {
-      unsigned int counter = 0;
-      alent *cache_ptr = lineno_cache;
-      LINENO *src = native_lineno;
+      struct internal_lineno dst;
+
+      bfd_coff_swap_lineno_in (abfd, src, &dst);
+      cache_ptr->line_number = dst.l_lnno;
 
-      while (counter < asect->lineno_count)
+      if (cache_ptr->line_number == 0)
        {
-         struct internal_lineno dst;
+         bfd_boolean warned;
+         bfd_signed_vma symndx;
+         coff_symbol_type *sym;
+
+         nbr_func++;
+         warned = FALSE;
+         symndx = dst.l_addr.l_symndx;
+         if (symndx < 0
+             || (bfd_vma) symndx >= obj_raw_syment_count (abfd))
+           {
+             (*_bfd_error_handler)
+               (_("%B: warning: illegal symbol index %ld in line numbers"),
+                abfd, dst.l_addr.l_symndx);
+             symndx = 0;
+             warned = TRUE;
+           }
+
+         /* FIXME: We should not be casting between ints and
+            pointers like this.  */
+         sym = ((coff_symbol_type *)
+                ((symndx + obj_raw_syments (abfd))
+                 ->u.syment._n._n_n._n_zeroes));
+         cache_ptr->u.sym = (asymbol *) sym;
+         if (sym->lineno != NULL && ! warned)
+           (*_bfd_error_handler)
+             (_("%B: warning: duplicate line number information for `%s'"),
+              abfd, bfd_asymbol_name (&sym->symbol));
+
+         sym->lineno = cache_ptr;
+         if (sym->symbol.value < prev_offset)
+           ordered = 0;
+         prev_offset = sym->symbol.value;
+       }
+      else
+       cache_ptr->u.offset = dst.l_addr.l_paddr
+         - bfd_section_vma (abfd, asect);
 
-         bfd_coff_swap_lineno_in (abfd, src, &dst);
-         cache_ptr->line_number = dst.l_lnno;
+      cache_ptr++;
+      src++;
+    }
+  cache_ptr->line_number = 0;
+  bfd_release (abfd, native_lineno);
 
-         if (cache_ptr->line_number == 0)
+  /* On some systems (eg AIX5.3) the lineno table may not be sorted.  */
+  if (!ordered)
+    {
+      /* Sort the table.  */
+      alent **func_table;
+      alent *n_lineno_cache;
+
+      /* Create a table of functions.  */
+      func_table = bfd_alloc (abfd, nbr_func * sizeof (alent *));
+      if (func_table != NULL)
+       {
+         alent **p = func_table;
+         unsigned int i;
+
+         for (i = 0; i < counter; i++)
+           if (lineno_cache[i].line_number == 0)
+             *p++ = &lineno_cache[i];
+
+         /* Sort by functions.  */
+         qsort (func_table, nbr_func, sizeof (alent *), coff_sort_func_alent);
+
+         /* Create the new sorted table.  */
+         amt = ((bfd_size_type) asect->lineno_count + 1) * sizeof (alent);
+         n_lineno_cache = bfd_alloc (abfd, amt);
+         if (n_lineno_cache != NULL)
            {
-             bfd_boolean warned;
-             bfd_signed_vma symndx;
-             coff_symbol_type *sym;
-
-             warned = FALSE;
-             symndx = dst.l_addr.l_symndx;
-             if (symndx < 0
-                 || (bfd_vma) symndx >= obj_raw_syment_count (abfd))
-               {
-                 (*_bfd_error_handler)
-                   (_("%B: warning: illegal symbol index %ld in line numbers"),
-                    abfd, dst.l_addr.l_symndx);
-                 symndx = 0;
-                 warned = TRUE;
-               }
-             /* FIXME: We should not be casting between ints and
-                 pointers like this.  */
-             sym = ((coff_symbol_type *)
-                    ((symndx + obj_raw_syments (abfd))
-                     ->u.syment._n._n_n._n_zeroes));
-             cache_ptr->u.sym = (asymbol *) sym;
-             if (sym->lineno != NULL && ! warned)
+             alent *n_cache_ptr = n_lineno_cache;
+
+             for (i = 0; i < nbr_func; i++)
                {
-                 (*_bfd_error_handler)
-                   (_("%B: warning: duplicate line number information for `%s'"),
-                    abfd, bfd_asymbol_name (&sym->symbol));
+                 coff_symbol_type *sym;
+                 alent *old_ptr = func_table[i];
+
+                 /* Copy the function entry and update it.  */
+                 *n_cache_ptr = *old_ptr;
+                 sym = (coff_symbol_type *)n_cache_ptr->u.sym;
+                 sym->lineno = n_cache_ptr;
+                 n_cache_ptr++;
+                 old_ptr++;
+
+                 /* Copy the line number entries.  */
+                 while (old_ptr->line_number != 0)
+                   *n_cache_ptr++ = *old_ptr++;
                }
-             sym->lineno = cache_ptr;
+             n_cache_ptr->line_number = 0;
+             memcpy (lineno_cache, n_lineno_cache, amt);
            }
-         else
-           cache_ptr->u.offset = dst.l_addr.l_paddr
-             - bfd_section_vma (abfd, asect);
-
-         cache_ptr++;
-         src++;
-         counter++;
+         bfd_release (abfd, func_table);
        }
-      cache_ptr->line_number = 0;
-
     }
-  asect->lineno = lineno_cache;
-  /* FIXME, free native_lineno here, or use alloca or something.  */
+
   return TRUE;
 }
 
@@ -4357,7 +4471,7 @@ coff_slurp_symbol_table (bfd * abfd)
 
          dst->symbol.name = (char *) (src->u.syment._n._n_n._n_offset);
          /* We use the native name field to point to the cached field.  */
-         src->u.syment._n._n_n._n_zeroes = (long) dst;
+         src->u.syment._n._n_n._n_zeroes = (bfd_hostptr_t) dst;
          dst->symbol.section = coff_section_from_bfd_index (abfd,
                                                     src->u.syment.n_scnum);
          dst->symbol.flags = 0;
@@ -4373,8 +4487,8 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_EXT:
            case C_WEAKEXT:
 #if defined ARM
-            case C_THUMBEXT:
-            case C_THUMBEXTFUNC:
+           case C_THUMBEXT:
+           case C_THUMBEXTFUNC:
 #endif
 #ifdef RS6000COFF_C
            case C_HIDEXT:
@@ -4383,8 +4497,8 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_SYSTEM:      /* System Wide variable.  */
 #endif
 #ifdef COFF_WITH_PE
-            /* In PE, 0x68 (104) denotes a section symbol.  */
-            case C_SECTION:
+           /* In PE, 0x68 (104) denotes a section symbol.  */
+           case C_SECTION:
            /* In PE, 0x69 (105) denotes a weak external symbol.  */
            case C_NT_WEAK:
 #endif
@@ -4394,7 +4508,7 @@ coff_slurp_symbol_table (bfd * abfd)
                  dst->symbol.flags = BSF_EXPORT | BSF_GLOBAL;
 #if defined COFF_WITH_PE
                  /* PE sets the symbol to a value relative to the
-                     start of the section.  */
+                    start of the section.  */
                  dst->symbol.value = src->u.syment.n_value;
 #else
                  dst->symbol.value = (src->u.syment.n_value
@@ -4425,7 +4539,7 @@ coff_slurp_symbol_table (bfd * abfd)
                  dst->symbol.flags = BSF_LOCAL;
 #if defined COFF_WITH_PE
                  /* PE sets the symbol to a value relative to the
-                     start of the section.  */
+                    start of the section.  */
                  dst->symbol.value = src->u.syment.n_value;
 #else
                  dst->symbol.value = (src->u.syment.n_value
@@ -4460,9 +4574,9 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_LEAFSTAT:     /* Static leaf procedure.  */
 #endif
 #if defined ARM
-            case C_THUMBSTAT:    /* Thumb static.  */
-            case C_THUMBLABEL:   /* Thumb label.  */
-            case C_THUMBSTATFUNC:/* Thumb static function.  */
+           case C_THUMBSTAT:    /* Thumb static.  */
+           case C_THUMBLABEL:   /* Thumb label.  */
+           case C_THUMBSTATFUNC:/* Thumb static function.  */
 #endif
            case C_LABEL:        /* Label.  */
              if (src->u.syment.n_scnum == N_DEBUG)
@@ -4476,7 +4590,7 @@ coff_slurp_symbol_table (bfd * abfd)
                {
 #if defined COFF_WITH_PE
                  /* PE sets the symbol to a value relative to the
-                     start of the section.  */
+                    start of the section.  */
                  dst->symbol.value = src->u.syment.n_value;
 #else
                  dst->symbol.value = (src->u.syment.n_value
@@ -4491,7 +4605,7 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_EOS:         /* End of structure.  */
            case C_REGPARM:     /* Register parameter.  */
            case C_REG:         /* register variable.  */
-              /* C_AUTOARG conflicts with TI COFF C_UEXT.  */
+             /* C_AUTOARG conflicts with TI COFF C_UEXT.  */
 #if !defined (TIC80COFF) && !defined (TICOFF)
 #ifdef C_AUTOARG
            case C_AUTOARG:     /* 960-specific storage class.  */
@@ -4535,9 +4649,9 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_BINCL:       /* Beginning of include file.  */
            case C_EINCL:       /* Ending of include file.  */
              /* The value is actually a pointer into the line numbers
-                 of the file.  We locate the line number entry, and
-                 set the section to the section which contains it, and
-                 the value to the index in that section.  */
+                of the file.  We locate the line number entry, and
+                set the section to the section which contains it, and
+                the value to the index in that section.  */
              {
                asection *sec;
 
@@ -4584,7 +4698,7 @@ coff_slurp_symbol_table (bfd * abfd)
              if (strcmp (dst->symbol.name, ".bf") != 0)
                {
                  /* PE uses funny values for .ef and .lf; don't
-                     relocate them.  */
+                    relocate them.  */
                  dst->symbol.flags = BSF_DEBUGGING;
                }
              else
@@ -4599,13 +4713,13 @@ coff_slurp_symbol_table (bfd * abfd)
              break;
 
            case C_STATLAB:     /* Static load time label.  */
-              dst->symbol.value = src->u.syment.n_value;
-              dst->symbol.flags = BSF_GLOBAL;
-              break;
+             dst->symbol.value = src->u.syment.n_value;
+             dst->symbol.flags = BSF_GLOBAL;
+             break;
 
            case C_NULL:
              /* PE DLLs sometimes have zeroed out symbols for some
-                 reason.  Just ignore them without a warning.  */
+                reason.  Just ignore them without a warning.  */
              if (src->u.syment.n_type == 0
                  && src->u.syment.n_value == 0
                  && src->u.syment.n_scnum == 0)
@@ -4615,8 +4729,8 @@ coff_slurp_symbol_table (bfd * abfd)
            case C_ULABEL:      /* Undefined label.  */
            case C_USTATIC:     /* Undefined static.  */
 #ifndef COFF_WITH_PE
-            /* C_LINE in regular coff is 0x68.  NT has taken over this storage
-               class to represent a section symbol.  */
+           /* C_LINE in regular coff is 0x68.  NT has taken over this storage
+              class to represent a section symbol.  */
            case C_LINE:        /* line # reformatted as symbol table entry.  */
              /* NT uses 0x67 for a weak symbol, not C_ALIAS.  */
            case C_ALIAS:       /* Duplicate tag.  */
@@ -4719,7 +4833,7 @@ coff_classify_symbol (bfd *abfd,
 
 #ifdef STRICT_PE_FORMAT
       /* This is correct for Microsoft generated objects, but it
-         breaks gas generated objects.  */
+        breaks gas generated objects.  */
       if (syment->n_value == 0)
        {
          asection *sec;
@@ -4740,8 +4854,8 @@ coff_classify_symbol (bfd *abfd,
   if (syment->n_sclass == C_SECTION)
     {
       /* In some cases in a DLL generated by the Microsoft linker, the
-         n_value field will contain garbage.  FIXME: This should
-         probably be handled by the swapping function instead.  */
+        n_value field will contain garbage.  FIXME: This should
+        probably be handled by the swapping function instead.  */
       syment->n_value = 0;
       if (syment->n_scnum == 0)
        return COFF_SYMBOL_UNDEFINED;
@@ -4790,23 +4904,23 @@ SUBSUBSECTION
 */
 
 #ifndef CALC_ADDEND
-#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)                \
-  {                                                             \
-    coff_symbol_type *coffsym = NULL;                          \
-                                                                \
-    if (ptr && bfd_asymbol_bfd (ptr) != abfd)                   \
-      coffsym = (obj_symbols (abfd)                             \
-                 + (cache_ptr->sym_ptr_ptr - symbols));         \
-    else if (ptr)                                               \
-      coffsym = coff_symbol_from (abfd, ptr);                   \
-    if (coffsym != NULL                                                \
-        && coffsym->native->u.syment.n_scnum == 0)              \
-      cache_ptr->addend = 0;                                    \
-    else if (ptr && bfd_asymbol_bfd (ptr) == abfd               \
-             && ptr->section != NULL)                          \
-      cache_ptr->addend = - (ptr->section->vma + ptr->value);   \
-    else                                                        \
-      cache_ptr->addend = 0;                                    \
+#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr)               \
+  {                                                            \
+    coff_symbol_type *coffsym = NULL;                          \
+                                                               \
+    if (ptr && bfd_asymbol_bfd (ptr) != abfd)                  \
+      coffsym = (obj_symbols (abfd)                            \
+                + (cache_ptr->sym_ptr_ptr - symbols));         \
+    else if (ptr)                                              \
+      coffsym = coff_symbol_from (abfd, ptr);                  \
+    if (coffsym != NULL                                                \
+       && coffsym->native->u.syment.n_scnum == 0)              \
+      cache_ptr->addend = 0;                                   \
+    else if (ptr && bfd_asymbol_bfd (ptr) == abfd              \
+            && ptr->section != NULL)                           \
+      cache_ptr->addend = - (ptr->section->vma + ptr->value);  \
+    else                                                       \
+      cache_ptr->addend = 0;                                   \
   }
 #endif
 
@@ -4847,6 +4961,7 @@ coff_slurp_reloc_table (bfd * abfd, sec_ptr asect, asymbol ** symbols)
       cache_ptr = reloc_cache + idx;
       src = native_relocs + idx;
 
+      dst.r_offset = 0;
       coff_swap_reloc_in (abfd, src, &dst);
 
 #ifdef RELOC_PROCESSING
@@ -4927,6 +5042,7 @@ coff_rtype_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
 {
   arelent genrel;
 
+  genrel.howto = NULL;
   RTYPE2HOWTO (&genrel, rel);
   return genrel.howto;
 }
@@ -5158,7 +5274,8 @@ static const bfd_coff_backend_data bfd_coff_std_swap_table ATTRIBUTE_UNUSED =
   coff_classify_symbol, coff_compute_section_file_positions,
   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   coff_adjust_symndx, coff_link_add_one_symbol,
-  coff_link_output_has_begun, coff_final_link_postscript
+  coff_link_output_has_begun, coff_final_link_postscript,
+  bfd_pe_print_pdata
 };
 
 #ifdef TICOFF
@@ -5201,7 +5318,8 @@ static const bfd_coff_backend_data ticoff0_swap_table =
   coff_classify_symbol, coff_compute_section_file_positions,
   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   coff_adjust_symndx, coff_link_add_one_symbol,
-  coff_link_output_has_begun, coff_final_link_postscript
+  coff_link_output_has_begun, coff_final_link_postscript,
+  bfd_pe_print_pdata
 };
 #endif
 
@@ -5245,20 +5363,21 @@ static const bfd_coff_backend_data ticoff1_swap_table =
   coff_classify_symbol, coff_compute_section_file_positions,
   coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
   coff_adjust_symndx, coff_link_add_one_symbol,
-  coff_link_output_has_begun, coff_final_link_postscript
+  coff_link_output_has_begun, coff_final_link_postscript,
+  bfd_pe_print_pdata   /* huh */
 };
 #endif
 
 #ifndef coff_close_and_cleanup
-#define        coff_close_and_cleanup              _bfd_generic_close_and_cleanup
+#define coff_close_and_cleanup             _bfd_generic_close_and_cleanup
 #endif
 
 #ifndef coff_bfd_free_cached_info
-#define coff_bfd_free_cached_info           _bfd_generic_bfd_free_cached_info
+#define coff_bfd_free_cached_info          _bfd_generic_bfd_free_cached_info
 #endif
 
 #ifndef coff_get_section_contents
-#define        coff_get_section_contents           _bfd_generic_get_section_contents
+#define coff_get_section_contents          _bfd_generic_get_section_contents
 #endif
 
 #ifndef coff_bfd_copy_private_symbol_data
@@ -5282,7 +5401,7 @@ static const bfd_coff_backend_data ticoff1_swap_table =
 #endif
 
 #ifndef coff_bfd_set_private_flags
-#define coff_bfd_set_private_flags          _bfd_generic_bfd_set_private_flags
+#define coff_bfd_set_private_flags         _bfd_generic_bfd_set_private_flags
 #endif
 
 #ifndef coff_bfd_print_private_bfd_data
@@ -5310,6 +5429,9 @@ static const bfd_coff_backend_data ticoff1_swap_table =
 #ifndef coff_bfd_reloc_type_lookup
 #define coff_bfd_reloc_type_lookup         _bfd_norelocs_bfd_reloc_type_lookup
 #endif
+#ifndef coff_bfd_reloc_name_lookup
+#define coff_bfd_reloc_name_lookup    _bfd_norelocs_bfd_reloc_name_lookup
+#endif
 
 #ifndef coff_bfd_get_relocated_section_contents
 #define coff_bfd_get_relocated_section_contents \
@@ -5356,12 +5478,12 @@ const bfd_target VAR =                                                  \
   UNDER,                       /* Leading symbol underscore.  */       \
   '/',                         /* AR_pad_char.  */                     \
   15,                          /* AR_max_namelen.  */                  \
-                                                                       \
+                                                                       \
   /* Data conversion functions.  */                                    \
   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                          \
   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                          \
   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                          \
-                                                                       \
+                                                                       \
   /* Header conversion functions.  */                                  \
   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                          \
   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                          \
@@ -5385,9 +5507,9 @@ const bfd_target VAR =                                                    \
   BFD_JUMP_TABLE_WRITE (coff),                                         \
   BFD_JUMP_TABLE_LINK (coff),                                          \
   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                             \
-                                                                       \
+                                                                       \
   ALTERNATIVE,                                                         \
-                                                                       \
+                                                                       \
   SWAP_TABLE                                                           \
 };
 
@@ -5406,12 +5528,12 @@ const bfd_target VAR =                                                  \
   UNDER,                       /* Leading symbol underscore.  */       \
   '/',                         /* AR_pad_char.  */                     \
   15,                          /* AR_max_namelen.  */                  \
-                                                                       \
+                                                                       \
   /* Data conversion functions.  */                                    \
   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                          \
   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                          \
   bfd_getb16, bfd_getb_signed_16, bfd_putb16,                          \
-                                                                       \
+                                                                       \
   /* Header conversion functions.  */                                  \
   bfd_getb64, bfd_getb_signed_64, bfd_putb64,                          \
   bfd_getb32, bfd_getb_signed_32, bfd_putb32,                          \
@@ -5435,9 +5557,9 @@ const bfd_target VAR =                                                    \
   BFD_JUMP_TABLE_WRITE (coff),                                         \
   BFD_JUMP_TABLE_LINK (coff),                                          \
   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                             \
-                                                                       \
+                                                                       \
   ALTERNATIVE,                                                         \
-                                                                       \
+                                                                       \
   SWAP_TABLE                                                           \
 };
 
@@ -5485,6 +5607,6 @@ const bfd_target VAR =                                                    \
   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),                             \
                                                                        \
   ALTERNATIVE,                                                         \
-                                                                       \
+                                                                       \
   SWAP_TABLE                                                           \
 };