Home | History | Annotate | Download | only in bfd
      1 /* Generic ECOFF (Extended-COFF) routines.
      2    Copyright (C) 1990-2016 Free Software Foundation, Inc.
      3    Original version by Per Bothner.
      4    Full support added by Ian Lance Taylor, ian (at) cygnus.com.
      5 
      6    This file is part of BFD, the Binary File Descriptor library.
      7 
      8    This program is free software; you can redistribute it and/or modify
      9    it under the terms of the GNU General Public License as published by
     10    the Free Software Foundation; either version 3 of the License, or
     11    (at your option) any later version.
     12 
     13    This program is distributed in the hope that it will be useful,
     14    but WITHOUT ANY WARRANTY; without even the implied warranty of
     15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16    GNU General Public License for more details.
     17 
     18    You should have received a copy of the GNU General Public License
     19    along with this program; if not, write to the Free Software
     20    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
     21    MA 02110-1301, USA.  */
     22 
     23 #include "sysdep.h"
     24 #include "bfd.h"
     25 #include "bfdlink.h"
     26 #include "libbfd.h"
     27 #include "aout/ar.h"
     28 #include "aout/stab_gnu.h"
     29 
     30 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
     31    some other stuff which we don't want and which conflicts with stuff
     32    we do want.  */
     33 #include "libaout.h"
     34 #include "aout/aout64.h"
     35 #undef N_ABS
     36 #undef exec_hdr
     37 #undef obj_sym_filepos
     38 
     39 #include "coff/internal.h"
     40 #include "coff/sym.h"
     41 #include "coff/symconst.h"
     42 #include "coff/ecoff.h"
     43 #include "libcoff.h"
     44 #include "libecoff.h"
     45 #include "libiberty.h"
     46 
     47 #define streq(a, b)	(strcmp ((a), (b)) == 0)
     48 #define strneq(a, b, n)	(strncmp ((a), (b), (n)) == 0)
     49 
     50 
     51 /* This stuff is somewhat copied from coffcode.h.  */
     53 static asection bfd_debug_section =
     54 {
     55   /* name,      id,  index, next, prev, flags, user_set_vma,       */
     56      "*DEBUG*", 0,   0,     NULL, NULL, 0,     0,
     57   /* linker_mark, linker_has_input, gc_mark, compress_status,      */
     58      0,           0,                1,       0,
     59   /* segment_mark, sec_info_type, use_rela_p,                      */
     60      0,            0,             0,
     61   /* sec_flg0, sec_flg1, sec_flg2, sec_flg3, sec_flg4, sec_flg5,   */
     62      0,        0,        0,        0,        0,        0,
     63   /* vma, lma, size, rawsize, compressed_size, relax, relax_count, */
     64      0,   0,   0,    0,       0,               0,     0,
     65   /* output_offset, output_section, alignment_power,               */
     66      0,             NULL,           0,
     67   /* relocation, orelocation, reloc_count, filepos, rel_filepos,   */
     68      NULL,       NULL,        0,           0,       0,
     69   /* line_filepos, userdata, contents, lineno, lineno_count,       */
     70      0,            NULL,     NULL,     NULL,   0,
     71   /* entsize, kept_section, moving_line_filepos,                   */
     72      0,       NULL,         0,
     73   /* target_index, used_by_bfd, constructor_chain, owner,          */
     74      0,            NULL,        NULL,              NULL,
     75   /* symbol,                                                       */
     76      NULL,
     77   /* symbol_ptr_ptr,                                               */
     78      NULL,
     79   /* map_head, map_tail                                            */
     80      { NULL }, { NULL }
     81 };
     82 
     83 /* Create an ECOFF object.  */
     84 
     85 bfd_boolean
     86 _bfd_ecoff_mkobject (bfd *abfd)
     87 {
     88   bfd_size_type amt = sizeof (ecoff_data_type);
     89 
     90   abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
     91   if (abfd->tdata.ecoff_obj_data == NULL)
     92     return FALSE;
     93 
     94   return TRUE;
     95 }
     96 
     97 /* This is a hook called by coff_real_object_p to create any backend
     98    specific information.  */
     99 
    100 void *
    101 _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
    102 {
    103   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
    104   struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
    105   ecoff_data_type *ecoff;
    106 
    107   if (! _bfd_ecoff_mkobject (abfd))
    108     return NULL;
    109 
    110   ecoff = ecoff_data (abfd);
    111   ecoff->gp_size = 8;
    112   ecoff->sym_filepos = internal_f->f_symptr;
    113 
    114   if (internal_a != NULL)
    115     {
    116       int i;
    117 
    118       ecoff->text_start = internal_a->text_start;
    119       ecoff->text_end = internal_a->text_start + internal_a->tsize;
    120       ecoff->gp = internal_a->gp_value;
    121       ecoff->gprmask = internal_a->gprmask;
    122       for (i = 0; i < 4; i++)
    123 	ecoff->cprmask[i] = internal_a->cprmask[i];
    124       ecoff->fprmask = internal_a->fprmask;
    125       if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
    126 	abfd->flags |= D_PAGED;
    127       else
    128 	abfd->flags &=~ D_PAGED;
    129     }
    130 
    131   /* It turns out that no special action is required by the MIPS or
    132      Alpha ECOFF backends.  They have different information in the
    133      a.out header, but we just copy it all (e.g., gprmask, cprmask and
    134      fprmask) and let the swapping routines ensure that only relevant
    135      information is written out.  */
    136 
    137   return (void *) ecoff;
    138 }
    139 
    140 /* Initialize a new section.  */
    141 
    142 bfd_boolean
    143 _bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
    144 {
    145   unsigned int i;
    146   static struct
    147   {
    148     const char * name;
    149     flagword flags;
    150   }
    151   section_flags [] =
    152   {
    153     { _TEXT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
    154     { _INIT,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
    155     { _FINI,   SEC_ALLOC | SEC_CODE | SEC_LOAD },
    156     { _DATA,   SEC_ALLOC | SEC_DATA | SEC_LOAD },
    157     { _SDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD },
    158     { _RDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
    159     { _LIT8,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
    160     { _LIT4,   SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
    161     { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
    162     { _PDATA,  SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
    163     { _BSS,    SEC_ALLOC},
    164     { _SBSS,   SEC_ALLOC},
    165     /* An Irix 4 shared libary.  */
    166     { _LIB,    SEC_COFF_SHARED_LIBRARY}
    167   };
    168 
    169   section->alignment_power = 4;
    170 
    171   for (i = 0; i < ARRAY_SIZE (section_flags); i++)
    172     if (streq (section->name, section_flags[i].name))
    173       {
    174 	section->flags |= section_flags[i].flags;
    175 	break;
    176       }
    177 
    178 
    179   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
    180      uncertain about .init on some systems and I don't know how shared
    181      libraries work.  */
    182 
    183   return _bfd_generic_new_section_hook (abfd, section);
    184 }
    185 
    186 /* Determine the machine architecture and type.  This is called from
    187    the generic COFF routines.  It is the inverse of ecoff_get_magic,
    188    below.  This could be an ECOFF backend routine, with one version
    189    for each target, but there aren't all that many ECOFF targets.  */
    190 
    191 bfd_boolean
    192 _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
    193 {
    194   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
    195   enum bfd_architecture arch;
    196   unsigned long mach;
    197 
    198   switch (internal_f->f_magic)
    199     {
    200     case MIPS_MAGIC_1:
    201     case MIPS_MAGIC_LITTLE:
    202     case MIPS_MAGIC_BIG:
    203       arch = bfd_arch_mips;
    204       mach = bfd_mach_mips3000;
    205       break;
    206 
    207     case MIPS_MAGIC_LITTLE2:
    208     case MIPS_MAGIC_BIG2:
    209       /* MIPS ISA level 2: the r6000.  */
    210       arch = bfd_arch_mips;
    211       mach = bfd_mach_mips6000;
    212       break;
    213 
    214     case MIPS_MAGIC_LITTLE3:
    215     case MIPS_MAGIC_BIG3:
    216       /* MIPS ISA level 3: the r4000.  */
    217       arch = bfd_arch_mips;
    218       mach = bfd_mach_mips4000;
    219       break;
    220 
    221     case ALPHA_MAGIC:
    222       arch = bfd_arch_alpha;
    223       mach = 0;
    224       break;
    225 
    226     default:
    227       arch = bfd_arch_obscure;
    228       mach = 0;
    229       break;
    230     }
    231 
    232   return bfd_default_set_arch_mach (abfd, arch, mach);
    233 }
    234 
    235 bfd_boolean
    236 _bfd_ecoff_no_long_sections (bfd *abfd, int enable)
    237 {
    238   (void) abfd;
    239   (void) enable;
    240   return FALSE;
    241 }
    242 
    243 /* Get the magic number to use based on the architecture and machine.
    244    This is the inverse of _bfd_ecoff_set_arch_mach_hook, above.  */
    245 
    246 static int
    247 ecoff_get_magic (bfd *abfd)
    248 {
    249   int big, little;
    250 
    251   switch (bfd_get_arch (abfd))
    252     {
    253     case bfd_arch_mips:
    254       switch (bfd_get_mach (abfd))
    255 	{
    256 	default:
    257 	case 0:
    258 	case bfd_mach_mips3000:
    259 	  big = MIPS_MAGIC_BIG;
    260 	  little = MIPS_MAGIC_LITTLE;
    261 	  break;
    262 
    263 	case bfd_mach_mips6000:
    264 	  big = MIPS_MAGIC_BIG2;
    265 	  little = MIPS_MAGIC_LITTLE2;
    266 	  break;
    267 
    268 	case bfd_mach_mips4000:
    269 	  big = MIPS_MAGIC_BIG3;
    270 	  little = MIPS_MAGIC_LITTLE3;
    271 	  break;
    272 	}
    273 
    274       return bfd_big_endian (abfd) ? big : little;
    275 
    276     case bfd_arch_alpha:
    277       return ALPHA_MAGIC;
    278 
    279     default:
    280       abort ();
    281       return 0;
    282     }
    283 }
    284 
    285 /* Get the section s_flags to use for a section.  */
    286 
    287 static long
    288 ecoff_sec_to_styp_flags (const char *name, flagword flags)
    289 {
    290   unsigned int i;
    291   static struct
    292   {
    293     const char * name;
    294     long flags;
    295   }
    296   styp_flags [] =
    297   {
    298     { _TEXT,    STYP_TEXT       },
    299     { _DATA,    STYP_DATA       },
    300     { _SDATA,   STYP_SDATA      },
    301     { _RDATA,   STYP_RDATA      },
    302     { _LITA,    STYP_LITA       },
    303     { _LIT8,    STYP_LIT8       },
    304     { _LIT4,    STYP_LIT4       },
    305     { _BSS,     STYP_BSS        },
    306     { _SBSS,    STYP_SBSS       },
    307     { _INIT,    STYP_ECOFF_INIT },
    308     { _FINI,    STYP_ECOFF_FINI },
    309     { _PDATA,   STYP_PDATA      },
    310     { _XDATA,   STYP_XDATA      },
    311     { _LIB,     STYP_ECOFF_LIB  },
    312     { _GOT,     STYP_GOT        },
    313     { _HASH,    STYP_HASH       },
    314     { _DYNAMIC, STYP_DYNAMIC    },
    315     { _LIBLIST, STYP_LIBLIST    },
    316     { _RELDYN,  STYP_RELDYN     },
    317     { _CONFLIC, STYP_CONFLIC    },
    318     { _DYNSTR,  STYP_DYNSTR     },
    319     { _DYNSYM,  STYP_DYNSYM     },
    320     { _RCONST,  STYP_RCONST     }
    321   };
    322   long styp = 0;
    323 
    324   for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
    325     if (streq (name, styp_flags[i].name))
    326       {
    327 	styp = styp_flags[i].flags;
    328 	break;
    329       }
    330 
    331   if (styp == 0)
    332     {
    333       if (streq (name, _COMMENT))
    334 	{
    335 	  styp = STYP_COMMENT;
    336 	  flags &=~ SEC_NEVER_LOAD;
    337 	}
    338       else if (flags & SEC_CODE)
    339 	styp = STYP_TEXT;
    340       else if (flags & SEC_DATA)
    341 	styp = STYP_DATA;
    342       else if (flags & SEC_READONLY)
    343 	styp = STYP_RDATA;
    344       else if (flags & SEC_LOAD)
    345 	styp = STYP_REG;
    346       else
    347 	styp = STYP_BSS;
    348     }
    349 
    350   if (flags & SEC_NEVER_LOAD)
    351     styp |= STYP_NOLOAD;
    352 
    353   return styp;
    354 }
    355 
    356 /* Get the BFD flags to use for a section.  */
    357 
    358 bfd_boolean
    359 _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
    360 			      void * hdr,
    361 			      const char *name ATTRIBUTE_UNUSED,
    362 			      asection *section ATTRIBUTE_UNUSED,
    363 			      flagword * flags_ptr)
    364 {
    365   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
    366   long styp_flags = internal_s->s_flags;
    367   flagword sec_flags = 0;
    368 
    369   if (styp_flags & STYP_NOLOAD)
    370     sec_flags |= SEC_NEVER_LOAD;
    371 
    372   /* For 386 COFF, at least, an unloadable text or data section is
    373      actually a shared library section.  */
    374   if ((styp_flags & STYP_TEXT)
    375       || (styp_flags & STYP_ECOFF_INIT)
    376       || (styp_flags & STYP_ECOFF_FINI)
    377       || (styp_flags & STYP_DYNAMIC)
    378       || (styp_flags & STYP_LIBLIST)
    379       || (styp_flags & STYP_RELDYN)
    380       || styp_flags == STYP_CONFLIC
    381       || (styp_flags & STYP_DYNSTR)
    382       || (styp_flags & STYP_DYNSYM)
    383       || (styp_flags & STYP_HASH))
    384     {
    385       if (sec_flags & SEC_NEVER_LOAD)
    386 	sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
    387       else
    388 	sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
    389     }
    390   else if ((styp_flags & STYP_DATA)
    391 	   || (styp_flags & STYP_RDATA)
    392 	   || (styp_flags & STYP_SDATA)
    393 	   || styp_flags == STYP_PDATA
    394 	   || styp_flags == STYP_XDATA
    395 	   || (styp_flags & STYP_GOT)
    396 	   || styp_flags == STYP_RCONST)
    397     {
    398       if (sec_flags & SEC_NEVER_LOAD)
    399 	sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
    400       else
    401 	sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
    402       if ((styp_flags & STYP_RDATA)
    403 	  || styp_flags == STYP_PDATA
    404 	  || styp_flags == STYP_RCONST)
    405 	sec_flags |= SEC_READONLY;
    406     }
    407   else if ((styp_flags & STYP_BSS)
    408 	   || (styp_flags & STYP_SBSS))
    409     sec_flags |= SEC_ALLOC;
    410   else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
    411     sec_flags |= SEC_NEVER_LOAD;
    412   else if ((styp_flags & STYP_LITA)
    413 	   || (styp_flags & STYP_LIT8)
    414 	   || (styp_flags & STYP_LIT4))
    415     sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
    416   else if (styp_flags & STYP_ECOFF_LIB)
    417     sec_flags |= SEC_COFF_SHARED_LIBRARY;
    418   else
    419     sec_flags |= SEC_ALLOC | SEC_LOAD;
    420 
    421   * flags_ptr = sec_flags;
    422   return TRUE;
    423 }
    424 
    425 /* Read in the symbolic header for an ECOFF object file.  */
    427 
    428 static bfd_boolean
    429 ecoff_slurp_symbolic_header (bfd *abfd)
    430 {
    431   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
    432   bfd_size_type external_hdr_size;
    433   void * raw = NULL;
    434   HDRR *internal_symhdr;
    435 
    436   /* See if we've already read it in.  */
    437   if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
    438       backend->debug_swap.sym_magic)
    439     return TRUE;
    440 
    441   /* See whether there is a symbolic header.  */
    442   if (ecoff_data (abfd)->sym_filepos == 0)
    443     {
    444       bfd_get_symcount (abfd) = 0;
    445       return TRUE;
    446     }
    447 
    448   /* At this point bfd_get_symcount (abfd) holds the number of symbols
    449      as read from the file header, but on ECOFF this is always the
    450      size of the symbolic information header.  It would be cleaner to
    451      handle this when we first read the file in coffgen.c.  */
    452   external_hdr_size = backend->debug_swap.external_hdr_size;
    453   if (bfd_get_symcount (abfd) != external_hdr_size)
    454     {
    455       bfd_set_error (bfd_error_bad_value);
    456       return FALSE;
    457     }
    458 
    459   /* Read the symbolic information header.  */
    460   raw = bfd_malloc (external_hdr_size);
    461   if (raw == NULL)
    462     goto error_return;
    463 
    464   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
    465       || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
    466     goto error_return;
    467   internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
    468   (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
    469 
    470   if (internal_symhdr->magic != backend->debug_swap.sym_magic)
    471     {
    472       bfd_set_error (bfd_error_bad_value);
    473       goto error_return;
    474     }
    475 
    476   /* Now we can get the correct number of symbols.  */
    477   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
    478 			     + internal_symhdr->iextMax);
    479 
    480   if (raw != NULL)
    481     free (raw);
    482   return TRUE;
    483  error_return:
    484   if (raw != NULL)
    485     free (raw);
    486   return FALSE;
    487 }
    488 
    489 /* Read in and swap the important symbolic information for an ECOFF
    490    object file.  This is called by gdb via the read_debug_info entry
    491    point in the backend structure.  */
    492 
    493 bfd_boolean
    494 _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
    495 				asection *ignore ATTRIBUTE_UNUSED,
    496 				struct ecoff_debug_info *debug)
    497 {
    498   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
    499   HDRR *internal_symhdr;
    500   bfd_size_type raw_base;
    501   bfd_size_type raw_size;
    502   void * raw;
    503   bfd_size_type external_fdr_size;
    504   char *fraw_src;
    505   char *fraw_end;
    506   struct fdr *fdr_ptr;
    507   bfd_size_type raw_end;
    508   bfd_size_type cb_end;
    509   file_ptr pos;
    510 
    511   BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
    512 
    513   /* Check whether we've already gotten it, and whether there's any to
    514      get.  */
    515   if (ecoff_data (abfd)->raw_syments != NULL)
    516     return TRUE;
    517   if (ecoff_data (abfd)->sym_filepos == 0)
    518     {
    519       bfd_get_symcount (abfd) = 0;
    520       return TRUE;
    521     }
    522 
    523   if (! ecoff_slurp_symbolic_header (abfd))
    524     return FALSE;
    525 
    526   internal_symhdr = &debug->symbolic_header;
    527 
    528   /* Read all the symbolic information at once.  */
    529   raw_base = (ecoff_data (abfd)->sym_filepos
    530 	      + backend->debug_swap.external_hdr_size);
    531 
    532   /* Alpha ecoff makes the determination of raw_size difficult. It has
    533      an undocumented debug data section between the symhdr and the first
    534      documented section. And the ordering of the sections varies between
    535      statically and dynamically linked executables.
    536      If bfd supports SEEK_END someday, this code could be simplified.  */
    537   raw_end = 0;
    538 
    539 #define UPDATE_RAW_END(start, count, size) \
    540   cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
    541   if (cb_end > raw_end) \
    542     raw_end = cb_end
    543 
    544   UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
    545   UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
    546   UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
    547   UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
    548   /* eraxxon (at) alumni.rice.edu: ioptMax refers to the size of the
    549      optimization symtab, not the number of entries.  */
    550   UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
    551   UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
    552   UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
    553   UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
    554   UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
    555   UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
    556   UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
    557 
    558 #undef UPDATE_RAW_END
    559 
    560   raw_size = raw_end - raw_base;
    561   if (raw_size == 0)
    562     {
    563       ecoff_data (abfd)->sym_filepos = 0;
    564       return TRUE;
    565     }
    566   raw = bfd_alloc (abfd, raw_size);
    567   if (raw == NULL)
    568     return FALSE;
    569 
    570   pos = ecoff_data (abfd)->sym_filepos;
    571   pos += backend->debug_swap.external_hdr_size;
    572   if (bfd_seek (abfd, pos, SEEK_SET) != 0
    573       || bfd_bread (raw, raw_size, abfd) != raw_size)
    574     {
    575       bfd_release (abfd, raw);
    576       return FALSE;
    577     }
    578 
    579   ecoff_data (abfd)->raw_syments = raw;
    580 
    581   /* Get pointers for the numeric offsets in the HDRR structure.  */
    582 #define FIX(off1, off2, type)				\
    583   if (internal_symhdr->off1 == 0)			\
    584     debug->off2 = NULL;					\
    585   else							\
    586     debug->off2 = (type) ((char *) raw			\
    587 			  + (internal_symhdr->off1	\
    588 			     - raw_base))
    589 
    590   FIX (cbLineOffset, line, unsigned char *);
    591   FIX (cbDnOffset, external_dnr, void *);
    592   FIX (cbPdOffset, external_pdr, void *);
    593   FIX (cbSymOffset, external_sym, void *);
    594   FIX (cbOptOffset, external_opt, void *);
    595   FIX (cbAuxOffset, external_aux, union aux_ext *);
    596   FIX (cbSsOffset, ss, char *);
    597   FIX (cbSsExtOffset, ssext, char *);
    598   FIX (cbFdOffset, external_fdr, void *);
    599   FIX (cbRfdOffset, external_rfd, void *);
    600   FIX (cbExtOffset, external_ext, void *);
    601 #undef FIX
    602 
    603   /* I don't want to always swap all the data, because it will just
    604      waste time and most programs will never look at it.  The only
    605      time the linker needs most of the debugging information swapped
    606      is when linking big-endian and little-endian MIPS object files
    607      together, which is not a common occurrence.
    608 
    609      We need to look at the fdr to deal with a lot of information in
    610      the symbols, so we swap them here.  */
    611   debug->fdr = (FDR *) bfd_alloc2 (abfd, internal_symhdr->ifdMax,
    612 				   sizeof (struct fdr));
    613   if (debug->fdr == NULL)
    614     return FALSE;
    615   external_fdr_size = backend->debug_swap.external_fdr_size;
    616   fdr_ptr = debug->fdr;
    617   fraw_src = (char *) debug->external_fdr;
    618   /* PR 17512: file: 3372-1243-0.004.  */
    619   if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
    620     return FALSE;
    621   fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
    622   for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
    623     (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
    624 
    625   return TRUE;
    626 }
    627 
    628 /* ECOFF symbol table routines.  The ECOFF symbol table is described
    630    in gcc/mips-tfile.c.  */
    631 
    632 /* ECOFF uses two common sections.  One is the usual one, and the
    633    other is for small objects.  All the small objects are kept
    634    together, and then referenced via the gp pointer, which yields
    635    faster assembler code.  This is what we use for the small common
    636    section.  */
    637 static asection ecoff_scom_section;
    638 static asymbol ecoff_scom_symbol;
    639 static asymbol *ecoff_scom_symbol_ptr;
    640 
    641 /* Create an empty symbol.  */
    642 
    643 asymbol *
    644 _bfd_ecoff_make_empty_symbol (bfd *abfd)
    645 {
    646   ecoff_symbol_type *new_symbol;
    647   bfd_size_type amt = sizeof (ecoff_symbol_type);
    648 
    649   new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
    650   if (new_symbol == NULL)
    651     return NULL;
    652   new_symbol->symbol.section = NULL;
    653   new_symbol->fdr = NULL;
    654   new_symbol->local = FALSE;
    655   new_symbol->native = NULL;
    656   new_symbol->symbol.the_bfd = abfd;
    657   return &new_symbol->symbol;
    658 }
    659 
    660 /* Set the BFD flags and section for an ECOFF symbol.  */
    661 
    662 static bfd_boolean
    663 ecoff_set_symbol_info (bfd *abfd,
    664 		       SYMR *ecoff_sym,
    665 		       asymbol *asym,
    666 		       int ext,
    667 		       int weak)
    668 {
    669   asym->the_bfd = abfd;
    670   asym->value = ecoff_sym->value;
    671   asym->section = &bfd_debug_section;
    672   asym->udata.i = 0;
    673 
    674   /* Most symbol types are just for debugging.  */
    675   switch (ecoff_sym->st)
    676     {
    677     case stGlobal:
    678     case stStatic:
    679     case stLabel:
    680     case stProc:
    681     case stStaticProc:
    682       break;
    683     case stNil:
    684       if (ECOFF_IS_STAB (ecoff_sym))
    685 	{
    686 	  asym->flags = BSF_DEBUGGING;
    687 	  return TRUE;
    688 	}
    689       break;
    690     default:
    691       asym->flags = BSF_DEBUGGING;
    692       return TRUE;
    693     }
    694 
    695   if (weak)
    696     asym->flags = BSF_EXPORT | BSF_WEAK;
    697   else if (ext)
    698     asym->flags = BSF_EXPORT | BSF_GLOBAL;
    699   else
    700     {
    701       asym->flags = BSF_LOCAL;
    702       /* Normally, a local stProc symbol will have a corresponding
    703          external symbol.  We mark the local symbol as a debugging
    704          symbol, in order to prevent nm from printing both out.
    705          Similarly, we mark stLabel and stabs symbols as debugging
    706          symbols.  In both cases, we do want to set the value
    707          correctly based on the symbol class.  */
    708       if (ecoff_sym->st == stProc
    709 	  || ecoff_sym->st == stLabel
    710 	  || ECOFF_IS_STAB (ecoff_sym))
    711 	asym->flags |= BSF_DEBUGGING;
    712     }
    713 
    714   if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
    715     asym->flags |= BSF_FUNCTION;
    716 
    717   switch (ecoff_sym->sc)
    718     {
    719     case scNil:
    720       /* Used for compiler generated labels.  Leave them in the
    721 	 debugging section, and mark them as local.  If BSF_DEBUGGING
    722 	 is set, then nm does not display them for some reason.  If no
    723 	 flags are set then the linker whines about them.  */
    724       asym->flags = BSF_LOCAL;
    725       break;
    726     case scText:
    727       asym->section = bfd_make_section_old_way (abfd, _TEXT);
    728       asym->value -= asym->section->vma;
    729       break;
    730     case scData:
    731       asym->section = bfd_make_section_old_way (abfd, _DATA);
    732       asym->value -= asym->section->vma;
    733       break;
    734     case scBss:
    735       asym->section = bfd_make_section_old_way (abfd, _BSS);
    736       asym->value -= asym->section->vma;
    737       break;
    738     case scRegister:
    739       asym->flags = BSF_DEBUGGING;
    740       break;
    741     case scAbs:
    742       asym->section = bfd_abs_section_ptr;
    743       break;
    744     case scUndefined:
    745       asym->section = bfd_und_section_ptr;
    746       asym->flags = 0;
    747       asym->value = 0;
    748       break;
    749     case scCdbLocal:
    750     case scBits:
    751     case scCdbSystem:
    752     case scRegImage:
    753     case scInfo:
    754     case scUserStruct:
    755       asym->flags = BSF_DEBUGGING;
    756       break;
    757     case scSData:
    758       asym->section = bfd_make_section_old_way (abfd, ".sdata");
    759       asym->value -= asym->section->vma;
    760       break;
    761     case scSBss:
    762       asym->section = bfd_make_section_old_way (abfd, ".sbss");
    763       asym->value -= asym->section->vma;
    764       break;
    765     case scRData:
    766       asym->section = bfd_make_section_old_way (abfd, ".rdata");
    767       asym->value -= asym->section->vma;
    768       break;
    769     case scVar:
    770       asym->flags = BSF_DEBUGGING;
    771       break;
    772     case scCommon:
    773       if (asym->value > ecoff_data (abfd)->gp_size)
    774 	{
    775 	  asym->section = bfd_com_section_ptr;
    776 	  asym->flags = 0;
    777 	  break;
    778 	}
    779       /* Fall through.  */
    780     case scSCommon:
    781       if (ecoff_scom_section.name == NULL)
    782 	{
    783 	  /* Initialize the small common section.  */
    784 	  ecoff_scom_section.name = SCOMMON;
    785 	  ecoff_scom_section.flags = SEC_IS_COMMON;
    786 	  ecoff_scom_section.output_section = &ecoff_scom_section;
    787 	  ecoff_scom_section.symbol = &ecoff_scom_symbol;
    788 	  ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
    789 	  ecoff_scom_symbol.name = SCOMMON;
    790 	  ecoff_scom_symbol.flags = BSF_SECTION_SYM;
    791 	  ecoff_scom_symbol.section = &ecoff_scom_section;
    792 	  ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
    793 	}
    794       asym->section = &ecoff_scom_section;
    795       asym->flags = 0;
    796       break;
    797     case scVarRegister:
    798     case scVariant:
    799       asym->flags = BSF_DEBUGGING;
    800       break;
    801     case scSUndefined:
    802       asym->section = bfd_und_section_ptr;
    803       asym->flags = 0;
    804       asym->value = 0;
    805       break;
    806     case scInit:
    807       asym->section = bfd_make_section_old_way (abfd, ".init");
    808       asym->value -= asym->section->vma;
    809       break;
    810     case scBasedVar:
    811     case scXData:
    812     case scPData:
    813       asym->flags = BSF_DEBUGGING;
    814       break;
    815     case scFini:
    816       asym->section = bfd_make_section_old_way (abfd, ".fini");
    817       asym->value -= asym->section->vma;
    818       break;
    819     case scRConst:
    820       asym->section = bfd_make_section_old_way (abfd, ".rconst");
    821       asym->value -= asym->section->vma;
    822       break;
    823     default:
    824       break;
    825     }
    826 
    827   /* Look for special constructors symbols and make relocation entries
    828      in a special construction section.  These are produced by the
    829      -fgnu-linker argument to g++.  */
    830   if (ECOFF_IS_STAB (ecoff_sym))
    831     {
    832       switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
    833 	{
    834 	default:
    835 	  break;
    836 
    837 	case N_SETA:
    838 	case N_SETT:
    839 	case N_SETD:
    840 	case N_SETB:
    841 	  /* Mark the symbol as a constructor.  */
    842 	  asym->flags |= BSF_CONSTRUCTOR;
    843 	  break;
    844 	}
    845     }
    846   return TRUE;
    847 }
    848 
    849 /* Read an ECOFF symbol table.  */
    850 
    851 bfd_boolean
    852 _bfd_ecoff_slurp_symbol_table (bfd *abfd)
    853 {
    854   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
    855   const bfd_size_type external_ext_size
    856     = backend->debug_swap.external_ext_size;
    857   const bfd_size_type external_sym_size
    858     = backend->debug_swap.external_sym_size;
    859   void (* const swap_ext_in) (bfd *, void *, EXTR *)
    860     = backend->debug_swap.swap_ext_in;
    861   void (* const swap_sym_in) (bfd *, void *, SYMR *)
    862     = backend->debug_swap.swap_sym_in;
    863   ecoff_symbol_type *internal;
    864   ecoff_symbol_type *internal_ptr;
    865   char *eraw_src;
    866   char *eraw_end;
    867   FDR *fdr_ptr;
    868   FDR *fdr_end;
    869 
    870   /* If we've already read in the symbol table, do nothing.  */
    871   if (ecoff_data (abfd)->canonical_symbols != NULL)
    872     return TRUE;
    873 
    874   /* Get the symbolic information.  */
    875   if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
    876 					&ecoff_data (abfd)->debug_info))
    877     return FALSE;
    878   if (bfd_get_symcount (abfd) == 0)
    879     return TRUE;
    880 
    881   internal = (ecoff_symbol_type *) bfd_alloc2 (abfd, bfd_get_symcount (abfd),
    882 					       sizeof (ecoff_symbol_type));
    883   if (internal == NULL)
    884     return FALSE;
    885 
    886   internal_ptr = internal;
    887   eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
    888   eraw_end = (eraw_src
    889 	      + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
    890 		 * external_ext_size));
    891   for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
    892     {
    893       EXTR internal_esym;
    894 
    895       (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
    896 
    897       /* PR 17512: file: 3372-1000-0.004.  */
    898       if (internal_esym.asym.iss >= ecoff_data (abfd)->debug_info.symbolic_header.issExtMax
    899 	  || internal_esym.asym.iss < 0)
    900 	return FALSE;
    901 
    902       internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
    903 				   + internal_esym.asym.iss);
    904 
    905       if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
    906 				  &internal_ptr->symbol, 1,
    907 				  internal_esym.weakext))
    908 	return FALSE;
    909 
    910       /* The alpha uses a negative ifd field for section symbols.  */
    911       if (internal_esym.ifd >= 0)
    912 	{
    913 	  /* PR 17512: file: 3372-1983-0.004.  */
    914 	  if (internal_esym.ifd >= ecoff_data (abfd)->debug_info.symbolic_header.ifdMax)
    915 	    internal_ptr->fdr = NULL;
    916 	  else
    917 	    internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
    918 				 + internal_esym.ifd);
    919 	}
    920       else
    921 	internal_ptr->fdr = NULL;
    922       internal_ptr->local = FALSE;
    923       internal_ptr->native = (void *) eraw_src;
    924     }
    925 
    926   /* The local symbols must be accessed via the fdr's, because the
    927      string and aux indices are relative to the fdr information.  */
    928   fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
    929   fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
    930   for (; fdr_ptr < fdr_end; fdr_ptr++)
    931     {
    932       char *lraw_src;
    933       char *lraw_end;
    934 
    935       lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
    936 		  + fdr_ptr->isymBase * external_sym_size);
    937       lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
    938       for (;
    939 	   lraw_src < lraw_end;
    940 	   lraw_src += external_sym_size, internal_ptr++)
    941 	{
    942 	  SYMR internal_sym;
    943 
    944 	  (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
    945 	  internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
    946 				       + fdr_ptr->issBase
    947 				       + internal_sym.iss);
    948 	  if (!ecoff_set_symbol_info (abfd, &internal_sym,
    949 				      &internal_ptr->symbol, 0, 0))
    950 	    return FALSE;
    951 	  internal_ptr->fdr = fdr_ptr;
    952 	  internal_ptr->local = TRUE;
    953 	  internal_ptr->native = (void *) lraw_src;
    954 	}
    955     }
    956 
    957   /* PR 17512: file: 3372-3080-0.004.
    958      A discrepancy between ecoff_data (abfd)->debug_info.symbolic_header.isymMax
    959      and ecoff_data (abfd)->debug_info.symbolic_header.ifdMax can mean that
    960      we have fewer symbols than we were expecting.  Allow for this by updating
    961      the symbol count and warning the user.  */
    962   if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
    963     {
    964       bfd_get_symcount (abfd) = internal_ptr - internal;
    965       (*_bfd_error_handler)
    966 	(_("%B: warning: isymMax (%ld) is greater than ifdMax (%d)\n"),
    967 	 abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
    968 	 ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
    969     }
    970 
    971   ecoff_data (abfd)->canonical_symbols = internal;
    972 
    973   return TRUE;
    974 }
    975 
    976 /* Return the amount of space needed for the canonical symbols.  */
    977 
    978 long
    979 _bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
    980 {
    981   if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
    982 					&ecoff_data (abfd)->debug_info))
    983     return -1;
    984 
    985   if (bfd_get_symcount (abfd) == 0)
    986     return 0;
    987 
    988   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
    989 }
    990 
    991 /* Get the canonical symbols.  */
    992 
    993 long
    994 _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
    995 {
    996   unsigned int counter = 0;
    997   ecoff_symbol_type *symbase;
    998   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
    999 
   1000   if (! _bfd_ecoff_slurp_symbol_table (abfd))
   1001     return -1;
   1002   if (bfd_get_symcount (abfd) == 0)
   1003     return 0;
   1004 
   1005   symbase = ecoff_data (abfd)->canonical_symbols;
   1006   while (counter < bfd_get_symcount (abfd))
   1007     {
   1008       *(location++) = symbase++;
   1009       counter++;
   1010     }
   1011   *location++ = NULL;
   1012   return bfd_get_symcount (abfd);
   1013 }
   1014 
   1015 /* Turn ECOFF type information into a printable string.
   1016    ecoff_emit_aggregate and ecoff_type_to_string are from
   1017    gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
   1018 
   1019 /* Write aggregate information to a string.  */
   1020 
   1021 static void
   1022 ecoff_emit_aggregate (bfd *abfd,
   1023 		      FDR *fdr,
   1024 		      char *string,
   1025 		      RNDXR *rndx,
   1026 		      long isym,
   1027 		      const char *which)
   1028 {
   1029   const struct ecoff_debug_swap * const debug_swap =
   1030     &ecoff_backend (abfd)->debug_swap;
   1031   struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
   1032   unsigned int ifd = rndx->rfd;
   1033   unsigned int indx = rndx->index;
   1034   const char *name;
   1035 
   1036   if (ifd == 0xfff)
   1037     ifd = isym;
   1038 
   1039   /* An ifd of -1 is an opaque type.  An escaped index of 0 is a
   1040      struct return type of a procedure compiled without -g.  */
   1041   if (ifd == 0xffffffff
   1042       || (rndx->rfd == 0xfff && indx == 0))
   1043     name = "<undefined>";
   1044   else if (indx == indexNil)
   1045     name = "<no name>";
   1046   else
   1047     {
   1048       SYMR sym;
   1049 
   1050       if (debug_info->external_rfd == NULL)
   1051 	fdr = debug_info->fdr + ifd;
   1052       else
   1053 	{
   1054 	  RFDT rfd;
   1055 
   1056 	  (*debug_swap->swap_rfd_in) (abfd,
   1057 				      ((char *) debug_info->external_rfd
   1058 				       + ((fdr->rfdBase + ifd)
   1059 					  * debug_swap->external_rfd_size)),
   1060 				      &rfd);
   1061 	  fdr = debug_info->fdr + rfd;
   1062 	}
   1063 
   1064       indx += fdr->isymBase;
   1065 
   1066       (*debug_swap->swap_sym_in) (abfd,
   1067 				  ((char *) debug_info->external_sym
   1068 				   + indx * debug_swap->external_sym_size),
   1069 				  &sym);
   1070 
   1071       name = debug_info->ss + fdr->issBase + sym.iss;
   1072     }
   1073 
   1074   sprintf (string,
   1075 	   "%s %s { ifd = %u, index = %lu }",
   1076 	   which, name, ifd,
   1077 	   ((unsigned long) indx
   1078 	    + debug_info->symbolic_header.iextMax));
   1079 }
   1080 
   1081 /* Convert the type information to string format.  */
   1082 
   1083 static char *
   1084 ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx)
   1085 {
   1086   union aux_ext *aux_ptr;
   1087   int bigendian;
   1088   AUXU u;
   1089   struct qual
   1090   {
   1091     unsigned int  type;
   1092     int  low_bound;
   1093     int  high_bound;
   1094     int  stride;
   1095   } qualifiers[7];
   1096   unsigned int basic_type;
   1097   int i;
   1098   char buffer1[1024];
   1099   static char buffer2[1024];
   1100   char *p1 = buffer1;
   1101   char *p2 = buffer2;
   1102   RNDXR rndx;
   1103 
   1104   aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
   1105   bigendian = fdr->fBigendian;
   1106 
   1107   for (i = 0; i < 7; i++)
   1108     {
   1109       qualifiers[i].low_bound = 0;
   1110       qualifiers[i].high_bound = 0;
   1111       qualifiers[i].stride = 0;
   1112     }
   1113 
   1114   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
   1115     return "-1 (no type)";
   1116   _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
   1117 
   1118   basic_type = u.ti.bt;
   1119   qualifiers[0].type = u.ti.tq0;
   1120   qualifiers[1].type = u.ti.tq1;
   1121   qualifiers[2].type = u.ti.tq2;
   1122   qualifiers[3].type = u.ti.tq3;
   1123   qualifiers[4].type = u.ti.tq4;
   1124   qualifiers[5].type = u.ti.tq5;
   1125   qualifiers[6].type = tqNil;
   1126 
   1127   /* Go get the basic type.  */
   1128   switch (basic_type)
   1129     {
   1130     case btNil:			/* Undefined.  */
   1131       strcpy (p1, "nil");
   1132       break;
   1133 
   1134     case btAdr:			/* Address - integer same size as pointer.  */
   1135       strcpy (p1, "address");
   1136       break;
   1137 
   1138     case btChar:		/* Character.  */
   1139       strcpy (p1, "char");
   1140       break;
   1141 
   1142     case btUChar:		/* Unsigned character.  */
   1143       strcpy (p1, "unsigned char");
   1144       break;
   1145 
   1146     case btShort:		/* Short.  */
   1147       strcpy (p1, "short");
   1148       break;
   1149 
   1150     case btUShort:		/* Unsigned short.  */
   1151       strcpy (p1, "unsigned short");
   1152       break;
   1153 
   1154     case btInt:			/* Int.  */
   1155       strcpy (p1, "int");
   1156       break;
   1157 
   1158     case btUInt:		/* Unsigned int.  */
   1159       strcpy (p1, "unsigned int");
   1160       break;
   1161 
   1162     case btLong:		/* Long.  */
   1163       strcpy (p1, "long");
   1164       break;
   1165 
   1166     case btULong:		/* Unsigned long.  */
   1167       strcpy (p1, "unsigned long");
   1168       break;
   1169 
   1170     case btFloat:		/* Float (real).  */
   1171       strcpy (p1, "float");
   1172       break;
   1173 
   1174     case btDouble:		/* Double (real).  */
   1175       strcpy (p1, "double");
   1176       break;
   1177 
   1178       /* Structures add 1-2 aux words:
   1179 	 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
   1180 	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
   1181 
   1182     case btStruct:		/* Structure (Record).  */
   1183       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
   1184       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
   1185 			    (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
   1186 			    "struct");
   1187       indx++;			/* Skip aux words.  */
   1188       break;
   1189 
   1190       /* Unions add 1-2 aux words:
   1191 	 1st word is [ST_RFDESCAPE, offset] pointer to union def;
   1192 	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
   1193 
   1194     case btUnion:		/* Union.  */
   1195       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
   1196       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
   1197 			    (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
   1198 			    "union");
   1199       indx++;			/* Skip aux words.  */
   1200       break;
   1201 
   1202       /* Enumerations add 1-2 aux words:
   1203 	 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
   1204 	 2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
   1205 
   1206     case btEnum:		/* Enumeration.  */
   1207       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
   1208       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
   1209 			    (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
   1210 			    "enum");
   1211       indx++;			/* Skip aux words.  */
   1212       break;
   1213 
   1214     case btTypedef:		/* Defined via a typedef, isymRef points.  */
   1215       strcpy (p1, "typedef");
   1216       break;
   1217 
   1218     case btRange:		/* Subrange of int.  */
   1219       strcpy (p1, "subrange");
   1220       break;
   1221 
   1222     case btSet:			/* Pascal sets.  */
   1223       strcpy (p1, "set");
   1224       break;
   1225 
   1226     case btComplex:		/* Fortran complex.  */
   1227       strcpy (p1, "complex");
   1228       break;
   1229 
   1230     case btDComplex:		/* Fortran double complex.  */
   1231       strcpy (p1, "double complex");
   1232       break;
   1233 
   1234     case btIndirect:		/* Forward or unnamed typedef.  */
   1235       strcpy (p1, "forward/unamed typedef");
   1236       break;
   1237 
   1238     case btFixedDec:		/* Fixed Decimal.  */
   1239       strcpy (p1, "fixed decimal");
   1240       break;
   1241 
   1242     case btFloatDec:		/* Float Decimal.  */
   1243       strcpy (p1, "float decimal");
   1244       break;
   1245 
   1246     case btString:		/* Varying Length Character String.  */
   1247       strcpy (p1, "string");
   1248       break;
   1249 
   1250     case btBit:			/* Aligned Bit String.  */
   1251       strcpy (p1, "bit");
   1252       break;
   1253 
   1254     case btPicture:		/* Picture.  */
   1255       strcpy (p1, "picture");
   1256       break;
   1257 
   1258     case btVoid:		/* Void.  */
   1259       strcpy (p1, "void");
   1260       break;
   1261 
   1262     default:
   1263       sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
   1264       break;
   1265     }
   1266 
   1267   p1 += strlen (buffer1);
   1268 
   1269   /* If this is a bitfield, get the bitsize.  */
   1270   if (u.ti.fBitfield)
   1271     {
   1272       int bitsize;
   1273 
   1274       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
   1275       sprintf (p1, " : %d", bitsize);
   1276       p1 += strlen (buffer1);
   1277     }
   1278 
   1279   /* Deal with any qualifiers.  */
   1280   if (qualifiers[0].type != tqNil)
   1281     {
   1282       /* Snarf up any array bounds in the correct order.  Arrays
   1283          store 5 successive words in the aux. table:
   1284         	word 0	RNDXR to type of the bounds (ie, int)
   1285         	word 1	Current file descriptor index
   1286         	word 2	low bound
   1287         	word 3	high bound (or -1 if [])
   1288         	word 4	stride size in bits.  */
   1289       for (i = 0; i < 7; i++)
   1290 	{
   1291 	  if (qualifiers[i].type == tqArray)
   1292 	    {
   1293 	      qualifiers[i].low_bound =
   1294 		AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
   1295 	      qualifiers[i].high_bound =
   1296 		AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
   1297 	      qualifiers[i].stride =
   1298 		AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
   1299 	      indx += 5;
   1300 	    }
   1301 	}
   1302 
   1303       /* Now print out the qualifiers.  */
   1304       for (i = 0; i < 6; i++)
   1305 	{
   1306 	  switch (qualifiers[i].type)
   1307 	    {
   1308 	    case tqNil:
   1309 	    case tqMax:
   1310 	      break;
   1311 
   1312 	    case tqPtr:
   1313 	      strcpy (p2, "ptr to ");
   1314 	      p2 += sizeof ("ptr to ")-1;
   1315 	      break;
   1316 
   1317 	    case tqVol:
   1318 	      strcpy (p2, "volatile ");
   1319 	      p2 += sizeof ("volatile ")-1;
   1320 	      break;
   1321 
   1322 	    case tqFar:
   1323 	      strcpy (p2, "far ");
   1324 	      p2 += sizeof ("far ")-1;
   1325 	      break;
   1326 
   1327 	    case tqProc:
   1328 	      strcpy (p2, "func. ret. ");
   1329 	      p2 += sizeof ("func. ret. ");
   1330 	      break;
   1331 
   1332 	    case tqArray:
   1333 	      {
   1334 		int first_array = i;
   1335 		int j;
   1336 
   1337 		/* Print array bounds reversed (ie, in the order the C
   1338 		   programmer writes them).  C is such a fun language....  */
   1339 		while (i < 5 && qualifiers[i+1].type == tqArray)
   1340 		  i++;
   1341 
   1342 		for (j = i; j >= first_array; j--)
   1343 		  {
   1344 		    strcpy (p2, "array [");
   1345 		    p2 += sizeof ("array [")-1;
   1346 		    if (qualifiers[j].low_bound != 0)
   1347 		      sprintf (p2,
   1348 			       "%ld:%ld {%ld bits}",
   1349 			       (long) qualifiers[j].low_bound,
   1350 			       (long) qualifiers[j].high_bound,
   1351 			       (long) qualifiers[j].stride);
   1352 
   1353 		    else if (qualifiers[j].high_bound != -1)
   1354 		      sprintf (p2,
   1355 			       "%ld {%ld bits}",
   1356 			       (long) (qualifiers[j].high_bound + 1),
   1357 			       (long) (qualifiers[j].stride));
   1358 
   1359 		    else
   1360 		      sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
   1361 
   1362 		    p2 += strlen (p2);
   1363 		    strcpy (p2, "] of ");
   1364 		    p2 += sizeof ("] of ")-1;
   1365 		  }
   1366 	      }
   1367 	      break;
   1368 	    }
   1369 	}
   1370     }
   1371 
   1372   strcpy (p2, buffer1);
   1373   return buffer2;
   1374 }
   1375 
   1376 /* Return information about ECOFF symbol SYMBOL in RET.  */
   1377 
   1378 void
   1379 _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
   1380 			    asymbol *symbol,
   1381 			    symbol_info *ret)
   1382 {
   1383   bfd_symbol_info (symbol, ret);
   1384 }
   1385 
   1386 /* Return whether this is a local label.  */
   1387 
   1388 bfd_boolean
   1389 _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
   1390 				    const char *name)
   1391 {
   1392   return name[0] == '$';
   1393 }
   1394 
   1395 /* Print information about an ECOFF symbol.  */
   1396 
   1397 void
   1398 _bfd_ecoff_print_symbol (bfd *abfd,
   1399 			 void * filep,
   1400 			 asymbol *symbol,
   1401 			 bfd_print_symbol_type how)
   1402 {
   1403   const struct ecoff_debug_swap * const debug_swap
   1404     = &ecoff_backend (abfd)->debug_swap;
   1405   FILE *file = (FILE *)filep;
   1406 
   1407   switch (how)
   1408     {
   1409     case bfd_print_symbol_name:
   1410       fprintf (file, "%s", symbol->name);
   1411       break;
   1412     case bfd_print_symbol_more:
   1413       if (ecoffsymbol (symbol)->local)
   1414 	{
   1415 	  SYMR ecoff_sym;
   1416 
   1417 	  (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
   1418 				      &ecoff_sym);
   1419 	  fprintf (file, "ecoff local ");
   1420 	  fprintf_vma (file, (bfd_vma) ecoff_sym.value);
   1421 	  fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
   1422 		   (unsigned) ecoff_sym.sc);
   1423 	}
   1424       else
   1425 	{
   1426 	  EXTR ecoff_ext;
   1427 
   1428 	  (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
   1429 				      &ecoff_ext);
   1430 	  fprintf (file, "ecoff extern ");
   1431 	  fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
   1432 	  fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
   1433 		   (unsigned) ecoff_ext.asym.sc);
   1434 	}
   1435       break;
   1436     case bfd_print_symbol_all:
   1437       /* Print out the symbols in a reasonable way.  */
   1438       {
   1439 	char type;
   1440 	int pos;
   1441 	EXTR ecoff_ext;
   1442 	char jmptbl;
   1443 	char cobol_main;
   1444 	char weakext;
   1445 
   1446 	if (ecoffsymbol (symbol)->local)
   1447 	  {
   1448 	    (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
   1449 					&ecoff_ext.asym);
   1450 	    type = 'l';
   1451 	    pos = ((((char *) ecoffsymbol (symbol)->native
   1452 		     - (char *) ecoff_data (abfd)->debug_info.external_sym)
   1453 		    / debug_swap->external_sym_size)
   1454 		   + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
   1455 	    jmptbl = ' ';
   1456 	    cobol_main = ' ';
   1457 	    weakext = ' ';
   1458 	  }
   1459 	else
   1460 	  {
   1461 	    (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
   1462 					&ecoff_ext);
   1463 	    type = 'e';
   1464 	    pos = (((char *) ecoffsymbol (symbol)->native
   1465 		    - (char *) ecoff_data (abfd)->debug_info.external_ext)
   1466 		   / debug_swap->external_ext_size);
   1467 	    jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
   1468 	    cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
   1469 	    weakext = ecoff_ext.weakext ? 'w' : ' ';
   1470 	  }
   1471 
   1472 	fprintf (file, "[%3d] %c ",
   1473 		 pos, type);
   1474 	fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
   1475 	fprintf (file, " st %x sc %x indx %x %c%c%c %s",
   1476 		 (unsigned) ecoff_ext.asym.st,
   1477 		 (unsigned) ecoff_ext.asym.sc,
   1478 		 (unsigned) ecoff_ext.asym.index,
   1479 		 jmptbl, cobol_main, weakext,
   1480 		 symbol->name);
   1481 
   1482 	if (ecoffsymbol (symbol)->fdr != NULL
   1483 	    && ecoff_ext.asym.index != indexNil)
   1484 	  {
   1485 	    FDR *fdr;
   1486 	    unsigned int indx;
   1487 	    int bigendian;
   1488 	    bfd_size_type sym_base;
   1489 	    union aux_ext *aux_base;
   1490 
   1491 	    fdr = ecoffsymbol (symbol)->fdr;
   1492 	    indx = ecoff_ext.asym.index;
   1493 
   1494 	    /* sym_base is used to map the fdr relative indices which
   1495 	       appear in the file to the position number which we are
   1496 	       using.  */
   1497 	    sym_base = fdr->isymBase;
   1498 	    if (ecoffsymbol (symbol)->local)
   1499 	      sym_base +=
   1500 		ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
   1501 
   1502 	    /* aux_base is the start of the aux entries for this file;
   1503 	       asym.index is an offset from this.  */
   1504 	    aux_base = (ecoff_data (abfd)->debug_info.external_aux
   1505 			+ fdr->iauxBase);
   1506 
   1507 	    /* The aux entries are stored in host byte order; the
   1508 	       order is indicated by a bit in the fdr.  */
   1509 	    bigendian = fdr->fBigendian;
   1510 
   1511 	    /* This switch is basically from gcc/mips-tdump.c.  */
   1512 	    switch (ecoff_ext.asym.st)
   1513 	      {
   1514 	      case stNil:
   1515 	      case stLabel:
   1516 		break;
   1517 
   1518 	      case stFile:
   1519 	      case stBlock:
   1520 		fprintf (file, _("\n      End+1 symbol: %ld"),
   1521 			 (long) (indx + sym_base));
   1522 		break;
   1523 
   1524 	      case stEnd:
   1525 		if (ecoff_ext.asym.sc == scText
   1526 		    || ecoff_ext.asym.sc == scInfo)
   1527 		  fprintf (file, _("\n      First symbol: %ld"),
   1528 			   (long) (indx + sym_base));
   1529 		else
   1530 		  fprintf (file, _("\n      First symbol: %ld"),
   1531 			   ((long)
   1532 			    (AUX_GET_ISYM (bigendian,
   1533 					   &aux_base[ecoff_ext.asym.index])
   1534 			     + sym_base)));
   1535 		break;
   1536 
   1537 	      case stProc:
   1538 	      case stStaticProc:
   1539 		if (ECOFF_IS_STAB (&ecoff_ext.asym))
   1540 		  ;
   1541 		else if (ecoffsymbol (symbol)->local)
   1542 		  fprintf (file, _("\n      End+1 symbol: %-7ld   Type:  %s"),
   1543 			   ((long)
   1544 			    (AUX_GET_ISYM (bigendian,
   1545 					   &aux_base[ecoff_ext.asym.index])
   1546 			     + sym_base)),
   1547 			   ecoff_type_to_string (abfd, fdr, indx + 1));
   1548 		else
   1549 		  fprintf (file, _("\n      Local symbol: %ld"),
   1550 			   ((long) indx
   1551 			    + (long) sym_base
   1552 			    + (ecoff_data (abfd)
   1553 			       ->debug_info.symbolic_header.iextMax)));
   1554 		break;
   1555 
   1556 	      case stStruct:
   1557 		fprintf (file, _("\n      struct; End+1 symbol: %ld"),
   1558 			 (long) (indx + sym_base));
   1559 		break;
   1560 
   1561 	      case stUnion:
   1562 		fprintf (file, _("\n      union; End+1 symbol: %ld"),
   1563 			 (long) (indx + sym_base));
   1564 		break;
   1565 
   1566 	      case stEnum:
   1567 		fprintf (file, _("\n      enum; End+1 symbol: %ld"),
   1568 			 (long) (indx + sym_base));
   1569 		break;
   1570 
   1571 	      default:
   1572 		if (! ECOFF_IS_STAB (&ecoff_ext.asym))
   1573 		  fprintf (file, _("\n      Type: %s"),
   1574 			   ecoff_type_to_string (abfd, fdr, indx));
   1575 		break;
   1576 	      }
   1577 	  }
   1578       }
   1579       break;
   1580     }
   1581 }
   1582 
   1583 /* Read in the relocs for a section.  */
   1585 
   1586 static bfd_boolean
   1587 ecoff_slurp_reloc_table (bfd *abfd,
   1588 			 asection *section,
   1589 			 asymbol **symbols)
   1590 {
   1591   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
   1592   arelent *internal_relocs;
   1593   bfd_size_type external_reloc_size;
   1594   bfd_size_type amt;
   1595   char *external_relocs;
   1596   arelent *rptr;
   1597   unsigned int i;
   1598 
   1599   if (section->relocation != NULL
   1600       || section->reloc_count == 0
   1601       || (section->flags & SEC_CONSTRUCTOR) != 0)
   1602     return TRUE;
   1603 
   1604   if (! _bfd_ecoff_slurp_symbol_table (abfd))
   1605     return FALSE;
   1606 
   1607   amt = section->reloc_count;
   1608   amt *= sizeof (arelent);
   1609   internal_relocs = (arelent *) bfd_alloc (abfd, amt);
   1610 
   1611   external_reloc_size = backend->external_reloc_size;
   1612   amt = external_reloc_size * section->reloc_count;
   1613   external_relocs = (char *) bfd_alloc (abfd, amt);
   1614   if (internal_relocs == NULL || external_relocs == NULL)
   1615     return FALSE;
   1616   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
   1617     return FALSE;
   1618   if (bfd_bread (external_relocs, amt, abfd) != amt)
   1619     return FALSE;
   1620 
   1621   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
   1622     {
   1623       struct internal_reloc intern;
   1624 
   1625       (*backend->swap_reloc_in) (abfd,
   1626 				 external_relocs + i * external_reloc_size,
   1627 				 &intern);
   1628 
   1629       if (intern.r_extern)
   1630 	{
   1631 	  /* r_symndx is an index into the external symbols.  */
   1632 	  BFD_ASSERT (intern.r_symndx >= 0
   1633 		      && (intern.r_symndx
   1634 			  < (ecoff_data (abfd)
   1635 			     ->debug_info.symbolic_header.iextMax)));
   1636 	  rptr->sym_ptr_ptr = symbols + intern.r_symndx;
   1637 	  rptr->addend = 0;
   1638 	}
   1639       else if (intern.r_symndx == RELOC_SECTION_NONE
   1640 	       || intern.r_symndx == RELOC_SECTION_ABS)
   1641 	{
   1642 	  rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
   1643 	  rptr->addend = 0;
   1644 	}
   1645       else
   1646 	{
   1647 	  const char *sec_name;
   1648 	  asection *sec;
   1649 
   1650 	  /* r_symndx is a section key.  */
   1651 	  switch (intern.r_symndx)
   1652 	    {
   1653 	    case RELOC_SECTION_TEXT:  sec_name = _TEXT;  break;
   1654 	    case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
   1655 	    case RELOC_SECTION_DATA:  sec_name = _DATA;  break;
   1656 	    case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
   1657 	    case RELOC_SECTION_SBSS:  sec_name = _SBSS;  break;
   1658 	    case RELOC_SECTION_BSS:   sec_name = _BSS;   break;
   1659 	    case RELOC_SECTION_INIT:  sec_name = _INIT;  break;
   1660 	    case RELOC_SECTION_LIT8:  sec_name = _LIT8;  break;
   1661 	    case RELOC_SECTION_LIT4:  sec_name = _LIT4;  break;
   1662 	    case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
   1663 	    case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
   1664 	    case RELOC_SECTION_FINI:  sec_name = _FINI;  break;
   1665 	    case RELOC_SECTION_LITA:  sec_name = _LITA;  break;
   1666 	    case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
   1667 	    default: abort ();
   1668 	    }
   1669 
   1670 	  sec = bfd_get_section_by_name (abfd, sec_name);
   1671 	  if (sec == NULL)
   1672 	    abort ();
   1673 	  rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
   1674 
   1675 	  rptr->addend = - bfd_get_section_vma (abfd, sec);
   1676 	}
   1677 
   1678       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
   1679 
   1680       /* Let the backend select the howto field and do any other
   1681 	 required processing.  */
   1682       (*backend->adjust_reloc_in) (abfd, &intern, rptr);
   1683     }
   1684 
   1685   bfd_release (abfd, external_relocs);
   1686 
   1687   section->relocation = internal_relocs;
   1688 
   1689   return TRUE;
   1690 }
   1691 
   1692 /* Get a canonical list of relocs.  */
   1693 
   1694 long
   1695 _bfd_ecoff_canonicalize_reloc (bfd *abfd,
   1696 			       asection *section,
   1697 			       arelent **relptr,
   1698 			       asymbol **symbols)
   1699 {
   1700   unsigned int count;
   1701 
   1702   if (section->flags & SEC_CONSTRUCTOR)
   1703     {
   1704       arelent_chain *chain;
   1705 
   1706       /* This section has relocs made up by us, not the file, so take
   1707 	 them out of their chain and place them into the data area
   1708 	 provided.  */
   1709       for (count = 0, chain = section->constructor_chain;
   1710 	   count < section->reloc_count;
   1711 	   count++, chain = chain->next)
   1712 	*relptr++ = &chain->relent;
   1713     }
   1714   else
   1715     {
   1716       arelent *tblptr;
   1717 
   1718       if (! ecoff_slurp_reloc_table (abfd, section, symbols))
   1719 	return -1;
   1720 
   1721       tblptr = section->relocation;
   1722 
   1723       for (count = 0; count < section->reloc_count; count++)
   1724 	*relptr++ = tblptr++;
   1725     }
   1726 
   1727   *relptr = NULL;
   1728 
   1729   return section->reloc_count;
   1730 }
   1731 
   1732 /* Provided a BFD, a section and an offset into the section, calculate
   1734    and return the name of the source file and the line nearest to the
   1735    wanted location.  */
   1736 
   1737 bfd_boolean
   1738 _bfd_ecoff_find_nearest_line (bfd *abfd,
   1739 			      asymbol **symbols ATTRIBUTE_UNUSED,
   1740 			      asection *section,
   1741 			      bfd_vma offset,
   1742 			      const char **filename_ptr,
   1743 			      const char **functionname_ptr,
   1744 			      unsigned int *retline_ptr,
   1745 			      unsigned int *discriminator_ptr)
   1746 {
   1747   const struct ecoff_debug_swap * const debug_swap
   1748     = &ecoff_backend (abfd)->debug_swap;
   1749   struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
   1750   struct ecoff_find_line *line_info;
   1751 
   1752   /* Make sure we have the FDR's.  */
   1753   if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
   1754       || bfd_get_symcount (abfd) == 0)
   1755     return FALSE;
   1756 
   1757   if (ecoff_data (abfd)->find_line_info == NULL)
   1758     {
   1759       bfd_size_type amt = sizeof (struct ecoff_find_line);
   1760 
   1761       ecoff_data (abfd)->find_line_info =
   1762           (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
   1763       if (ecoff_data (abfd)->find_line_info == NULL)
   1764 	return FALSE;
   1765     }
   1766 
   1767   if (discriminator_ptr)
   1768     *discriminator_ptr = 0;
   1769   line_info = ecoff_data (abfd)->find_line_info;
   1770   return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
   1771 				 debug_swap, line_info, filename_ptr,
   1772 				 functionname_ptr, retline_ptr);
   1773 }
   1774 
   1775 /* Copy private BFD data.  This is called by objcopy and strip.  We
   1777    use it to copy the ECOFF debugging information from one BFD to the
   1778    other.  It would be theoretically possible to represent the ECOFF
   1779    debugging information in the symbol table.  However, it would be a
   1780    lot of work, and there would be little gain (gas, gdb, and ld
   1781    already access the ECOFF debugging information via the
   1782    ecoff_debug_info structure, and that structure would have to be
   1783    retained in order to support ECOFF debugging in MIPS ELF).
   1784 
   1785    The debugging information for the ECOFF external symbols comes from
   1786    the symbol table, so this function only handles the other debugging
   1787    information.  */
   1788 
   1789 bfd_boolean
   1790 _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
   1791 {
   1792   struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
   1793   struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
   1794   int i;
   1795   asymbol **sym_ptr_ptr;
   1796   size_t c;
   1797   bfd_boolean local;
   1798 
   1799   /* We only want to copy information over if both BFD's use ECOFF
   1800      format.  */
   1801   if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
   1802       || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
   1803     return TRUE;
   1804 
   1805   /* Copy the GP value and the register masks.  */
   1806   ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
   1807   ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
   1808   ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
   1809   for (i = 0; i < 3; i++)
   1810     ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
   1811 
   1812   /* Copy the version stamp.  */
   1813   oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
   1814 
   1815   /* If there are no symbols, don't copy any debugging information.  */
   1816   c = bfd_get_symcount (obfd);
   1817   sym_ptr_ptr = bfd_get_outsymbols (obfd);
   1818   if (c == 0 || sym_ptr_ptr == NULL)
   1819     return TRUE;
   1820 
   1821   /* See if there are any local symbols.  */
   1822   local = FALSE;
   1823   for (; c > 0; c--, sym_ptr_ptr++)
   1824     {
   1825       if (ecoffsymbol (*sym_ptr_ptr)->local)
   1826 	{
   1827 	  local = TRUE;
   1828 	  break;
   1829 	}
   1830     }
   1831 
   1832   if (local)
   1833     {
   1834       /* There are some local symbols.  We just bring over all the
   1835 	 debugging information.  FIXME: This is not quite the right
   1836 	 thing to do.  If the user has asked us to discard all
   1837 	 debugging information, then we are probably going to wind up
   1838 	 keeping it because there will probably be some local symbol
   1839 	 which objcopy did not discard.  We should actually break
   1840 	 apart the debugging information and only keep that which
   1841 	 applies to the symbols we want to keep.  */
   1842       oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
   1843       oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
   1844       oinfo->line = iinfo->line;
   1845 
   1846       oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
   1847       oinfo->external_dnr = iinfo->external_dnr;
   1848 
   1849       oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
   1850       oinfo->external_pdr = iinfo->external_pdr;
   1851 
   1852       oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
   1853       oinfo->external_sym = iinfo->external_sym;
   1854 
   1855       oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
   1856       oinfo->external_opt = iinfo->external_opt;
   1857 
   1858       oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
   1859       oinfo->external_aux = iinfo->external_aux;
   1860 
   1861       oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
   1862       oinfo->ss = iinfo->ss;
   1863 
   1864       oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
   1865       oinfo->external_fdr = iinfo->external_fdr;
   1866 
   1867       oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
   1868       oinfo->external_rfd = iinfo->external_rfd;
   1869     }
   1870   else
   1871     {
   1872       /* We are discarding all the local symbol information.  Look
   1873 	 through the external symbols and remove all references to FDR
   1874 	 or aux information.  */
   1875       c = bfd_get_symcount (obfd);
   1876       sym_ptr_ptr = bfd_get_outsymbols (obfd);
   1877       for (; c > 0; c--, sym_ptr_ptr++)
   1878 	{
   1879 	  EXTR esym;
   1880 
   1881 	  (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
   1882 	    (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
   1883 	  esym.ifd = ifdNil;
   1884 	  esym.asym.index = indexNil;
   1885 	  (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
   1886 	    (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
   1887 	}
   1888     }
   1889 
   1890   return TRUE;
   1891 }
   1892 
   1893 /* Set the architecture.  The supported architecture is stored in the
   1895    backend pointer.  We always set the architecture anyhow, since many
   1896    callers ignore the return value.  */
   1897 
   1898 bfd_boolean
   1899 _bfd_ecoff_set_arch_mach (bfd *abfd,
   1900 			  enum bfd_architecture arch,
   1901 			  unsigned long machine)
   1902 {
   1903   bfd_default_set_arch_mach (abfd, arch, machine);
   1904   return arch == ecoff_backend (abfd)->arch;
   1905 }
   1906 
   1907 /* Get the size of the section headers.  */
   1908 
   1909 int
   1910 _bfd_ecoff_sizeof_headers (bfd *abfd,
   1911 			   struct bfd_link_info *info ATTRIBUTE_UNUSED)
   1912 {
   1913   asection *current;
   1914   int c;
   1915   int ret;
   1916 
   1917   c = 0;
   1918   for (current = abfd->sections;
   1919        current != NULL;
   1920        current = current->next)
   1921     ++c;
   1922 
   1923   ret = (bfd_coff_filhsz (abfd)
   1924 	 + bfd_coff_aoutsz (abfd)
   1925 	 + c * bfd_coff_scnhsz (abfd));
   1926   return (int) BFD_ALIGN (ret, 16);
   1927 }
   1928 
   1929 /* Get the contents of a section.  */
   1930 
   1931 bfd_boolean
   1932 _bfd_ecoff_get_section_contents (bfd *abfd,
   1933 				 asection *section,
   1934 				 void * location,
   1935 				 file_ptr offset,
   1936 				 bfd_size_type count)
   1937 {
   1938   return _bfd_generic_get_section_contents (abfd, section, location,
   1939 					    offset, count);
   1940 }
   1941 
   1942 /* Sort sections by VMA, but put SEC_ALLOC sections first.  This is
   1943    called via qsort.  */
   1944 
   1945 static int
   1946 ecoff_sort_hdrs (const void * arg1, const void * arg2)
   1947 {
   1948   const asection *hdr1 = *(const asection **) arg1;
   1949   const asection *hdr2 = *(const asection **) arg2;
   1950 
   1951   if ((hdr1->flags & SEC_ALLOC) != 0)
   1952     {
   1953       if ((hdr2->flags & SEC_ALLOC) == 0)
   1954 	return -1;
   1955     }
   1956   else
   1957     {
   1958       if ((hdr2->flags & SEC_ALLOC) != 0)
   1959 	return 1;
   1960     }
   1961   if (hdr1->vma < hdr2->vma)
   1962     return -1;
   1963   else if (hdr1->vma > hdr2->vma)
   1964     return 1;
   1965   else
   1966     return 0;
   1967 }
   1968 
   1969 /* Calculate the file position for each section, and set
   1970    reloc_filepos.  */
   1971 
   1972 static bfd_boolean
   1973 ecoff_compute_section_file_positions (bfd *abfd)
   1974 {
   1975   file_ptr sofar, file_sofar;
   1976   asection **sorted_hdrs;
   1977   asection *current;
   1978   unsigned int i;
   1979   file_ptr old_sofar;
   1980   bfd_boolean rdata_in_text;
   1981   bfd_boolean first_data, first_nonalloc;
   1982   const bfd_vma round = ecoff_backend (abfd)->round;
   1983   bfd_size_type amt;
   1984 
   1985   sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
   1986   file_sofar = sofar;
   1987 
   1988   /* Sort the sections by VMA.  */
   1989   amt = abfd->section_count;
   1990   amt *= sizeof (asection *);
   1991   sorted_hdrs = (asection **) bfd_malloc (amt);
   1992   if (sorted_hdrs == NULL)
   1993     return FALSE;
   1994   for (current = abfd->sections, i = 0;
   1995        current != NULL;
   1996        current = current->next, i++)
   1997     sorted_hdrs[i] = current;
   1998   BFD_ASSERT (i == abfd->section_count);
   1999 
   2000   qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
   2001 	 ecoff_sort_hdrs);
   2002 
   2003   /* Some versions of the OSF linker put the .rdata section in the
   2004      text segment, and some do not.  */
   2005   rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
   2006   if (rdata_in_text)
   2007     {
   2008       for (i = 0; i < abfd->section_count; i++)
   2009 	{
   2010 	  current = sorted_hdrs[i];
   2011 	  if (streq (current->name, _RDATA))
   2012 	    break;
   2013 	  if ((current->flags & SEC_CODE) == 0
   2014 	      && ! streq (current->name, _PDATA)
   2015 	      && ! streq (current->name, _RCONST))
   2016 	    {
   2017 	      rdata_in_text = FALSE;
   2018 	      break;
   2019 	    }
   2020 	}
   2021     }
   2022   ecoff_data (abfd)->rdata_in_text = rdata_in_text;
   2023 
   2024   first_data = TRUE;
   2025   first_nonalloc = TRUE;
   2026   for (i = 0; i < abfd->section_count; i++)
   2027     {
   2028       unsigned int alignment_power;
   2029 
   2030       current = sorted_hdrs[i];
   2031 
   2032       /* For the Alpha ECOFF .pdata section the lnnoptr field is
   2033 	 supposed to indicate the number of .pdata entries that are
   2034 	 really in the section.  Each entry is 8 bytes.  We store this
   2035 	 away in line_filepos before increasing the section size.  */
   2036       if (streq (current->name, _PDATA))
   2037 	current->line_filepos = current->size / 8;
   2038 
   2039       alignment_power = current->alignment_power;
   2040 
   2041       /* On Ultrix, the data sections in an executable file must be
   2042 	 aligned to a page boundary within the file.  This does not
   2043 	 affect the section size, though.  FIXME: Does this work for
   2044 	 other platforms?  It requires some modification for the
   2045 	 Alpha, because .rdata on the Alpha goes with the text, not
   2046 	 the data.  */
   2047       if ((abfd->flags & EXEC_P) != 0
   2048 	  && (abfd->flags & D_PAGED) != 0
   2049 	  && ! first_data
   2050 	  && (current->flags & SEC_CODE) == 0
   2051 	  && (! rdata_in_text
   2052 	      || ! streq (current->name, _RDATA))
   2053 	  && ! streq (current->name, _PDATA)
   2054 	  && ! streq (current->name, _RCONST))
   2055 	{
   2056 	  sofar = (sofar + round - 1) &~ (round - 1);
   2057 	  file_sofar = (file_sofar + round - 1) &~ (round - 1);
   2058 	  first_data = FALSE;
   2059 	}
   2060       else if (streq (current->name, _LIB))
   2061 	{
   2062 	  /* On Irix 4, the location of contents of the .lib section
   2063 	     from a shared library section is also rounded up to a
   2064 	     page boundary.  */
   2065 
   2066 	  sofar = (sofar + round - 1) &~ (round - 1);
   2067 	  file_sofar = (file_sofar + round - 1) &~ (round - 1);
   2068 	}
   2069       else if (first_nonalloc
   2070 	       && (current->flags & SEC_ALLOC) == 0
   2071 	       && (abfd->flags & D_PAGED) != 0)
   2072 	{
   2073 	  /* Skip up to the next page for an unallocated section, such
   2074              as the .comment section on the Alpha.  This leaves room
   2075              for the .bss section.  */
   2076 	  first_nonalloc = FALSE;
   2077 	  sofar = (sofar + round - 1) &~ (round - 1);
   2078 	  file_sofar = (file_sofar + round - 1) &~ (round - 1);
   2079 	}
   2080 
   2081       /* Align the sections in the file to the same boundary on
   2082 	 which they are aligned in virtual memory.  */
   2083       sofar = BFD_ALIGN (sofar, 1 << alignment_power);
   2084       if ((current->flags & SEC_HAS_CONTENTS) != 0)
   2085 	file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
   2086 
   2087       if ((abfd->flags & D_PAGED) != 0
   2088 	  && (current->flags & SEC_ALLOC) != 0)
   2089 	{
   2090 	  sofar += (current->vma - sofar) % round;
   2091 	  if ((current->flags & SEC_HAS_CONTENTS) != 0)
   2092 	    file_sofar += (current->vma - file_sofar) % round;
   2093 	}
   2094 
   2095       if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
   2096 	current->filepos = file_sofar;
   2097 
   2098       sofar += current->size;
   2099       if ((current->flags & SEC_HAS_CONTENTS) != 0)
   2100 	file_sofar += current->size;
   2101 
   2102       /* Make sure that this section is of the right size too.  */
   2103       old_sofar = sofar;
   2104       sofar = BFD_ALIGN (sofar, 1 << alignment_power);
   2105       if ((current->flags & SEC_HAS_CONTENTS) != 0)
   2106 	file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
   2107       current->size += sofar - old_sofar;
   2108     }
   2109 
   2110   free (sorted_hdrs);
   2111   sorted_hdrs = NULL;
   2112 
   2113   ecoff_data (abfd)->reloc_filepos = file_sofar;
   2114 
   2115   return TRUE;
   2116 }
   2117 
   2118 /* Determine the location of the relocs for all the sections in the
   2119    output file, as well as the location of the symbolic debugging
   2120    information.  */
   2121 
   2122 static bfd_size_type
   2123 ecoff_compute_reloc_file_positions (bfd *abfd)
   2124 {
   2125   const bfd_size_type external_reloc_size =
   2126     ecoff_backend (abfd)->external_reloc_size;
   2127   file_ptr reloc_base;
   2128   bfd_size_type reloc_size;
   2129   asection *current;
   2130   file_ptr sym_base;
   2131 
   2132   if (! abfd->output_has_begun)
   2133     {
   2134       if (! ecoff_compute_section_file_positions (abfd))
   2135 	abort ();
   2136       abfd->output_has_begun = TRUE;
   2137     }
   2138 
   2139   reloc_base = ecoff_data (abfd)->reloc_filepos;
   2140 
   2141   reloc_size = 0;
   2142   for (current = abfd->sections;
   2143        current != NULL;
   2144        current = current->next)
   2145     {
   2146       if (current->reloc_count == 0)
   2147 	current->rel_filepos = 0;
   2148       else
   2149 	{
   2150 	  bfd_size_type relsize;
   2151 
   2152 	  current->rel_filepos = reloc_base;
   2153 	  relsize = current->reloc_count * external_reloc_size;
   2154 	  reloc_size += relsize;
   2155 	  reloc_base += relsize;
   2156 	}
   2157     }
   2158 
   2159   sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
   2160 
   2161   /* At least on Ultrix, the symbol table of an executable file must
   2162      be aligned to a page boundary.  FIXME: Is this true on other
   2163      platforms?  */
   2164   if ((abfd->flags & EXEC_P) != 0
   2165       && (abfd->flags & D_PAGED) != 0)
   2166     sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
   2167 		&~ (ecoff_backend (abfd)->round - 1));
   2168 
   2169   ecoff_data (abfd)->sym_filepos = sym_base;
   2170 
   2171   return reloc_size;
   2172 }
   2173 
   2174 /* Set the contents of a section.  */
   2175 
   2176 bfd_boolean
   2177 _bfd_ecoff_set_section_contents (bfd *abfd,
   2178 				 asection *section,
   2179 				 const void * location,
   2180 				 file_ptr offset,
   2181 				 bfd_size_type count)
   2182 {
   2183   file_ptr pos;
   2184 
   2185   /* This must be done first, because bfd_set_section_contents is
   2186      going to set output_has_begun to TRUE.  */
   2187   if (! abfd->output_has_begun
   2188       && ! ecoff_compute_section_file_positions (abfd))
   2189     return FALSE;
   2190 
   2191   /* Handle the .lib section specially so that Irix 4 shared libraries
   2192      work out.  See coff_set_section_contents in coffcode.h.  */
   2193   if (streq (section->name, _LIB))
   2194     {
   2195       bfd_byte *rec, *recend;
   2196 
   2197       rec = (bfd_byte *) location;
   2198       recend = rec + count;
   2199       while (rec < recend)
   2200 	{
   2201 	  ++section->lma;
   2202 	  rec += bfd_get_32 (abfd, rec) * 4;
   2203 	}
   2204 
   2205       BFD_ASSERT (rec == recend);
   2206     }
   2207 
   2208   if (count == 0)
   2209     return TRUE;
   2210 
   2211   pos = section->filepos + offset;
   2212   if (bfd_seek (abfd, pos, SEEK_SET) != 0
   2213       || bfd_bwrite (location, count, abfd) != count)
   2214     return FALSE;
   2215 
   2216   return TRUE;
   2217 }
   2218 
   2219 /* Get the GP value for an ECOFF file.  This is a hook used by
   2220    nlmconv.  */
   2221 
   2222 bfd_vma
   2223 bfd_ecoff_get_gp_value (bfd *abfd)
   2224 {
   2225   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
   2226       || bfd_get_format (abfd) != bfd_object)
   2227     {
   2228       bfd_set_error (bfd_error_invalid_operation);
   2229       return 0;
   2230     }
   2231 
   2232   return ecoff_data (abfd)->gp;
   2233 }
   2234 
   2235 /* Set the GP value for an ECOFF file.  This is a hook used by the
   2236    assembler.  */
   2237 
   2238 bfd_boolean
   2239 bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
   2240 {
   2241   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
   2242       || bfd_get_format (abfd) != bfd_object)
   2243     {
   2244       bfd_set_error (bfd_error_invalid_operation);
   2245       return FALSE;
   2246     }
   2247 
   2248   ecoff_data (abfd)->gp = gp_value;
   2249 
   2250   return TRUE;
   2251 }
   2252 
   2253 /* Set the register masks for an ECOFF file.  This is a hook used by
   2254    the assembler.  */
   2255 
   2256 bfd_boolean
   2257 bfd_ecoff_set_regmasks (bfd *abfd,
   2258 			unsigned long gprmask,
   2259 			unsigned long fprmask,
   2260 			unsigned long *cprmask)
   2261 {
   2262   ecoff_data_type *tdata;
   2263 
   2264   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
   2265       || bfd_get_format (abfd) != bfd_object)
   2266     {
   2267       bfd_set_error (bfd_error_invalid_operation);
   2268       return FALSE;
   2269     }
   2270 
   2271   tdata = ecoff_data (abfd);
   2272   tdata->gprmask = gprmask;
   2273   tdata->fprmask = fprmask;
   2274   if (cprmask != NULL)
   2275     {
   2276       int i;
   2277 
   2278       for (i = 0; i < 3; i++)
   2279 	tdata->cprmask[i] = cprmask[i];
   2280     }
   2281 
   2282   return TRUE;
   2283 }
   2284 
   2285 /* Get ECOFF EXTR information for an external symbol.  This function
   2286    is passed to bfd_ecoff_debug_externals.  */
   2287 
   2288 static bfd_boolean
   2289 ecoff_get_extr (asymbol *sym, EXTR *esym)
   2290 {
   2291   ecoff_symbol_type *ecoff_sym_ptr;
   2292   bfd *input_bfd;
   2293 
   2294   if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
   2295       || ecoffsymbol (sym)->native == NULL)
   2296     {
   2297       /* Don't include debugging, local, or section symbols.  */
   2298       if ((sym->flags & BSF_DEBUGGING) != 0
   2299 	  || (sym->flags & BSF_LOCAL) != 0
   2300 	  || (sym->flags & BSF_SECTION_SYM) != 0)
   2301 	return FALSE;
   2302 
   2303       esym->jmptbl = 0;
   2304       esym->cobol_main = 0;
   2305       esym->weakext = (sym->flags & BSF_WEAK) != 0;
   2306       esym->reserved = 0;
   2307       esym->ifd = ifdNil;
   2308       /* FIXME: we can do better than this for st and sc.  */
   2309       esym->asym.st = stGlobal;
   2310       esym->asym.sc = scAbs;
   2311       esym->asym.reserved = 0;
   2312       esym->asym.index = indexNil;
   2313       return TRUE;
   2314     }
   2315 
   2316   ecoff_sym_ptr = ecoffsymbol (sym);
   2317 
   2318   if (ecoff_sym_ptr->local)
   2319     return FALSE;
   2320 
   2321   input_bfd = bfd_asymbol_bfd (sym);
   2322   (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
   2323     (input_bfd, ecoff_sym_ptr->native, esym);
   2324 
   2325   /* If the symbol was defined by the linker, then esym will be
   2326      undefined but sym will not be.  Get a better class for such a
   2327      symbol.  */
   2328   if ((esym->asym.sc == scUndefined
   2329        || esym->asym.sc == scSUndefined)
   2330       && ! bfd_is_und_section (bfd_get_section (sym)))
   2331     esym->asym.sc = scAbs;
   2332 
   2333   /* Adjust the FDR index for the symbol by that used for the input
   2334      BFD.  */
   2335   if (esym->ifd != -1)
   2336     {
   2337       struct ecoff_debug_info *input_debug;
   2338 
   2339       input_debug = &ecoff_data (input_bfd)->debug_info;
   2340       BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
   2341       if (input_debug->ifdmap != NULL)
   2342 	esym->ifd = input_debug->ifdmap[esym->ifd];
   2343     }
   2344 
   2345   return TRUE;
   2346 }
   2347 
   2348 /* Set the external symbol index.  This routine is passed to
   2349    bfd_ecoff_debug_externals.  */
   2350 
   2351 static void
   2352 ecoff_set_index (asymbol *sym, bfd_size_type indx)
   2353 {
   2354   ecoff_set_sym_index (sym, indx);
   2355 }
   2356 
   2357 /* Write out an ECOFF file.  */
   2358 
   2359 bfd_boolean
   2360 _bfd_ecoff_write_object_contents (bfd *abfd)
   2361 {
   2362   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
   2363   const bfd_vma round = backend->round;
   2364   const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
   2365   const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
   2366   const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
   2367   const bfd_size_type external_hdr_size
   2368     = backend->debug_swap.external_hdr_size;
   2369   const bfd_size_type external_reloc_size = backend->external_reloc_size;
   2370   void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
   2371     = backend->adjust_reloc_out;
   2372   void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
   2373     = backend->swap_reloc_out;
   2374   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
   2375   HDRR * const symhdr = &debug->symbolic_header;
   2376   asection *current;
   2377   unsigned int count;
   2378   bfd_size_type reloc_size;
   2379   bfd_size_type text_size;
   2380   bfd_vma text_start;
   2381   bfd_boolean set_text_start;
   2382   bfd_size_type data_size;
   2383   bfd_vma data_start;
   2384   bfd_boolean set_data_start;
   2385   bfd_size_type bss_size;
   2386   void * buff = NULL;
   2387   void * reloc_buff = NULL;
   2388   struct internal_filehdr internal_f;
   2389   struct internal_aouthdr internal_a;
   2390   int i;
   2391 
   2392   /* Determine where the sections and relocs will go in the output
   2393      file.  */
   2394   reloc_size = ecoff_compute_reloc_file_positions (abfd);
   2395 
   2396   count = 1;
   2397   for (current = abfd->sections;
   2398        current != NULL;
   2399        current = current->next)
   2400     {
   2401       current->target_index = count;
   2402       ++count;
   2403     }
   2404 
   2405   if ((abfd->flags & D_PAGED) != 0)
   2406     text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
   2407   else
   2408     text_size = 0;
   2409   text_start = 0;
   2410   set_text_start = FALSE;
   2411   data_size = 0;
   2412   data_start = 0;
   2413   set_data_start = FALSE;
   2414   bss_size = 0;
   2415 
   2416   /* Write section headers to the file.  */
   2417 
   2418   /* Allocate buff big enough to hold a section header,
   2419      file header, or a.out header.  */
   2420   {
   2421     bfd_size_type siz;
   2422 
   2423     siz = scnhsz;
   2424     if (siz < filhsz)
   2425       siz = filhsz;
   2426     if (siz < aoutsz)
   2427       siz = aoutsz;
   2428     buff = bfd_malloc (siz);
   2429     if (buff == NULL)
   2430       goto error_return;
   2431   }
   2432 
   2433   internal_f.f_nscns = 0;
   2434   if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
   2435     goto error_return;
   2436 
   2437   for (current = abfd->sections;
   2438        current != NULL;
   2439        current = current->next)
   2440     {
   2441       struct internal_scnhdr section;
   2442       bfd_vma vma;
   2443 
   2444       ++internal_f.f_nscns;
   2445 
   2446       strncpy (section.s_name, current->name, sizeof section.s_name);
   2447 
   2448       /* This seems to be correct for Irix 4 shared libraries.  */
   2449       vma = bfd_get_section_vma (abfd, current);
   2450       if (streq (current->name, _LIB))
   2451 	section.s_vaddr = 0;
   2452       else
   2453 	section.s_vaddr = vma;
   2454 
   2455       section.s_paddr = current->lma;
   2456       section.s_size = current->size;
   2457 
   2458       /* If this section is unloadable then the scnptr will be 0.  */
   2459       if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
   2460 	section.s_scnptr = 0;
   2461       else
   2462 	section.s_scnptr = current->filepos;
   2463       section.s_relptr = current->rel_filepos;
   2464 
   2465       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
   2466 	 object file produced by the assembler is supposed to point to
   2467 	 information about how much room is required by objects of
   2468 	 various different sizes.  I think this only matters if we
   2469 	 want the linker to compute the best size to use, or
   2470 	 something.  I don't know what happens if the information is
   2471 	 not present.  */
   2472       if (! streq (current->name, _PDATA))
   2473 	section.s_lnnoptr = 0;
   2474       else
   2475 	{
   2476 	  /* The Alpha ECOFF .pdata section uses the lnnoptr field to
   2477 	     hold the number of entries in the section (each entry is
   2478 	     8 bytes).  We stored this in the line_filepos field in
   2479 	     ecoff_compute_section_file_positions.  */
   2480 	  section.s_lnnoptr = current->line_filepos;
   2481 	}
   2482 
   2483       section.s_nreloc = current->reloc_count;
   2484       section.s_nlnno = 0;
   2485       section.s_flags = ecoff_sec_to_styp_flags (current->name,
   2486 						 current->flags);
   2487 
   2488       if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
   2489 	  || bfd_bwrite (buff, scnhsz, abfd) != scnhsz)
   2490 	goto error_return;
   2491 
   2492       if ((section.s_flags & STYP_TEXT) != 0
   2493 	  || ((section.s_flags & STYP_RDATA) != 0
   2494 	      && ecoff_data (abfd)->rdata_in_text)
   2495 	  || section.s_flags == STYP_PDATA
   2496 	  || (section.s_flags & STYP_DYNAMIC) != 0
   2497 	  || (section.s_flags & STYP_LIBLIST) != 0
   2498 	  || (section.s_flags & STYP_RELDYN) != 0
   2499 	  || section.s_flags == STYP_CONFLIC
   2500 	  || (section.s_flags & STYP_DYNSTR) != 0
   2501 	  || (section.s_flags & STYP_DYNSYM) != 0
   2502 	  || (section.s_flags & STYP_HASH) != 0
   2503 	  || (section.s_flags & STYP_ECOFF_INIT) != 0
   2504 	  || (section.s_flags & STYP_ECOFF_FINI) != 0
   2505 	  || section.s_flags == STYP_RCONST)
   2506 	{
   2507 	  text_size += current->size;
   2508 	  if (! set_text_start || text_start > vma)
   2509 	    {
   2510 	      text_start = vma;
   2511 	      set_text_start = TRUE;
   2512 	    }
   2513 	}
   2514       else if ((section.s_flags & STYP_RDATA) != 0
   2515 	       || (section.s_flags & STYP_DATA) != 0
   2516 	       || (section.s_flags & STYP_LITA) != 0
   2517 	       || (section.s_flags & STYP_LIT8) != 0
   2518 	       || (section.s_flags & STYP_LIT4) != 0
   2519 	       || (section.s_flags & STYP_SDATA) != 0
   2520 	       || section.s_flags == STYP_XDATA
   2521 	       || (section.s_flags & STYP_GOT) != 0)
   2522 	{
   2523 	  data_size += current->size;
   2524 	  if (! set_data_start || data_start > vma)
   2525 	    {
   2526 	      data_start = vma;
   2527 	      set_data_start = TRUE;
   2528 	    }
   2529 	}
   2530       else if ((section.s_flags & STYP_BSS) != 0
   2531 	       || (section.s_flags & STYP_SBSS) != 0)
   2532 	bss_size += current->size;
   2533       else if (section.s_flags == 0
   2534 	       || (section.s_flags & STYP_ECOFF_LIB) != 0
   2535 	       || section.s_flags == STYP_COMMENT)
   2536 	/* Do nothing.  */ ;
   2537       else
   2538 	abort ();
   2539     }
   2540 
   2541   /* Set up the file header.  */
   2542   internal_f.f_magic = ecoff_get_magic (abfd);
   2543 
   2544   /* We will NOT put a fucking timestamp in the header here. Every
   2545      time you put it back, I will come in and take it out again.  I'm
   2546      sorry.  This field does not belong here.  We fill it with a 0 so
   2547      it compares the same but is not a reasonable time. --
   2548      gnu (at) cygnus.com.  */
   2549   internal_f.f_timdat = 0;
   2550 
   2551   if (bfd_get_symcount (abfd) != 0)
   2552     {
   2553       /* The ECOFF f_nsyms field is not actually the number of
   2554 	 symbols, it's the size of symbolic information header.  */
   2555       internal_f.f_nsyms = external_hdr_size;
   2556       internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
   2557     }
   2558   else
   2559     {
   2560       internal_f.f_nsyms = 0;
   2561       internal_f.f_symptr = 0;
   2562     }
   2563 
   2564   internal_f.f_opthdr = aoutsz;
   2565 
   2566   internal_f.f_flags = F_LNNO;
   2567   if (reloc_size == 0)
   2568     internal_f.f_flags |= F_RELFLG;
   2569   if (bfd_get_symcount (abfd) == 0)
   2570     internal_f.f_flags |= F_LSYMS;
   2571   if (abfd->flags & EXEC_P)
   2572     internal_f.f_flags |= F_EXEC;
   2573 
   2574   if (bfd_little_endian (abfd))
   2575     internal_f.f_flags |= F_AR32WR;
   2576   else
   2577     internal_f.f_flags |= F_AR32W;
   2578 
   2579   /* Set up the ``optional'' header.  */
   2580   if ((abfd->flags & D_PAGED) != 0)
   2581     internal_a.magic = ECOFF_AOUT_ZMAGIC;
   2582   else
   2583     internal_a.magic = ECOFF_AOUT_OMAGIC;
   2584 
   2585   /* FIXME: Is this really correct?  */
   2586   internal_a.vstamp = symhdr->vstamp;
   2587 
   2588   /* At least on Ultrix, these have to be rounded to page boundaries.
   2589      FIXME: Is this true on other platforms?  */
   2590   if ((abfd->flags & D_PAGED) != 0)
   2591     {
   2592       internal_a.tsize = (text_size + round - 1) &~ (round - 1);
   2593       internal_a.text_start = text_start &~ (round - 1);
   2594       internal_a.dsize = (data_size + round - 1) &~ (round - 1);
   2595       internal_a.data_start = data_start &~ (round - 1);
   2596     }
   2597   else
   2598     {
   2599       internal_a.tsize = text_size;
   2600       internal_a.text_start = text_start;
   2601       internal_a.dsize = data_size;
   2602       internal_a.data_start = data_start;
   2603     }
   2604 
   2605   /* On Ultrix, the initial portions of the .sbss and .bss segments
   2606      are at the end of the data section.  The bsize field in the
   2607      optional header records how many bss bytes are required beyond
   2608      those in the data section.  The value is not rounded to a page
   2609      boundary.  */
   2610   if (bss_size < internal_a.dsize - data_size)
   2611     bss_size = 0;
   2612   else
   2613     bss_size -= internal_a.dsize - data_size;
   2614   internal_a.bsize = bss_size;
   2615   internal_a.bss_start = internal_a.data_start + internal_a.dsize;
   2616 
   2617   internal_a.entry = bfd_get_start_address (abfd);
   2618 
   2619   internal_a.gp_value = ecoff_data (abfd)->gp;
   2620 
   2621   internal_a.gprmask = ecoff_data (abfd)->gprmask;
   2622   internal_a.fprmask = ecoff_data (abfd)->fprmask;
   2623   for (i = 0; i < 4; i++)
   2624     internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
   2625 
   2626   /* Let the backend adjust the headers if necessary.  */
   2627   if (backend->adjust_headers)
   2628     {
   2629       if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
   2630 	goto error_return;
   2631     }
   2632 
   2633   /* Write out the file header and the optional header.  */
   2634   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
   2635     goto error_return;
   2636 
   2637   bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
   2638   if (bfd_bwrite (buff, filhsz, abfd) != filhsz)
   2639     goto error_return;
   2640 
   2641   bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
   2642   if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz)
   2643     goto error_return;
   2644 
   2645   /* Build the external symbol information.  This must be done before
   2646      writing out the relocs so that we know the symbol indices.  We
   2647      don't do this if this BFD was created by the backend linker,
   2648      since it will have already handled the symbols and relocs.  */
   2649   if (! ecoff_data (abfd)->linker)
   2650     {
   2651       symhdr->iextMax = 0;
   2652       symhdr->issExtMax = 0;
   2653       debug->external_ext = debug->external_ext_end = NULL;
   2654       debug->ssext = debug->ssext_end = NULL;
   2655       if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
   2656 				       (abfd->flags & EXEC_P) == 0,
   2657 				       ecoff_get_extr, ecoff_set_index))
   2658 	goto error_return;
   2659 
   2660       /* Write out the relocs.  */
   2661       for (current = abfd->sections;
   2662 	   current != NULL;
   2663 	   current = current->next)
   2664 	{
   2665 	  arelent **reloc_ptr_ptr;
   2666 	  arelent **reloc_end;
   2667 	  char *out_ptr;
   2668 	  bfd_size_type amt;
   2669 
   2670 	  if (current->reloc_count == 0)
   2671 	    continue;
   2672 
   2673 	  amt = current->reloc_count * external_reloc_size;
   2674 	  reloc_buff = bfd_alloc (abfd, amt);
   2675 	  if (reloc_buff == NULL)
   2676 	    goto error_return;
   2677 
   2678 	  reloc_ptr_ptr = current->orelocation;
   2679 	  reloc_end = reloc_ptr_ptr + current->reloc_count;
   2680 	  out_ptr = (char *) reloc_buff;
   2681 
   2682 	  for (;
   2683 	       reloc_ptr_ptr < reloc_end;
   2684 	       reloc_ptr_ptr++, out_ptr += external_reloc_size)
   2685 	    {
   2686 	      arelent *reloc;
   2687 	      asymbol *sym;
   2688 	      struct internal_reloc in;
   2689 
   2690 	      memset ((void *) &in, 0, sizeof in);
   2691 
   2692 	      reloc = *reloc_ptr_ptr;
   2693 	      sym = *reloc->sym_ptr_ptr;
   2694 
   2695 	      /* If the howto field has not been initialised then skip this reloc.
   2696 		 This assumes that an error message has been issued elsewhere.  */
   2697 	      if (reloc->howto == NULL)
   2698 		continue;
   2699 
   2700 	      in.r_vaddr = (reloc->address
   2701 			    + bfd_get_section_vma (abfd, current));
   2702 	      in.r_type = reloc->howto->type;
   2703 
   2704 	      if ((sym->flags & BSF_SECTION_SYM) == 0)
   2705 		{
   2706 		  in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
   2707 		  in.r_extern = 1;
   2708 		}
   2709 	      else
   2710 		{
   2711 		  const char *name;
   2712 		  unsigned int j;
   2713 		  static struct
   2714 		  {
   2715 		    const char * name;
   2716 		    long r_symndx;
   2717 		  }
   2718 		  section_symndx [] =
   2719 		  {
   2720 		    { _TEXT,   RELOC_SECTION_TEXT   },
   2721 		    { _RDATA,  RELOC_SECTION_RDATA  },
   2722 		    { _DATA,   RELOC_SECTION_DATA   },
   2723 		    { _SDATA,  RELOC_SECTION_SDATA  },
   2724 		    { _SBSS,   RELOC_SECTION_SBSS   },
   2725 		    { _BSS,    RELOC_SECTION_BSS    },
   2726 		    { _INIT,   RELOC_SECTION_INIT   },
   2727 		    { _LIT8,   RELOC_SECTION_LIT8   },
   2728 		    { _LIT4,   RELOC_SECTION_LIT4   },
   2729 		    { _XDATA,  RELOC_SECTION_XDATA  },
   2730 		    { _PDATA,  RELOC_SECTION_PDATA  },
   2731 		    { _FINI,   RELOC_SECTION_FINI   },
   2732 		    { _LITA,   RELOC_SECTION_LITA   },
   2733 		    { "*ABS*", RELOC_SECTION_ABS    },
   2734 		    { _RCONST, RELOC_SECTION_RCONST }
   2735 		  };
   2736 
   2737 		  name = bfd_get_section_name (abfd, bfd_get_section (sym));
   2738 
   2739 		  for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
   2740 		    if (streq (name, section_symndx[j].name))
   2741 		      {
   2742 			in.r_symndx = section_symndx[j].r_symndx;
   2743 			break;
   2744 		      }
   2745 
   2746 		  if (j == ARRAY_SIZE (section_symndx))
   2747 		    abort ();
   2748 		  in.r_extern = 0;
   2749 		}
   2750 
   2751 	      (*adjust_reloc_out) (abfd, reloc, &in);
   2752 
   2753 	      (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
   2754 	    }
   2755 
   2756 	  if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
   2757 	    goto error_return;
   2758 	  amt = current->reloc_count * external_reloc_size;
   2759 	  if (bfd_bwrite (reloc_buff, amt, abfd) != amt)
   2760 	    goto error_return;
   2761 	  bfd_release (abfd, reloc_buff);
   2762 	  reloc_buff = NULL;
   2763 	}
   2764 
   2765       /* Write out the symbolic debugging information.  */
   2766       if (bfd_get_symcount (abfd) > 0)
   2767 	{
   2768 	  /* Write out the debugging information.  */
   2769 	  if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
   2770 				       ecoff_data (abfd)->sym_filepos))
   2771 	    goto error_return;
   2772 	}
   2773     }
   2774 
   2775   /* The .bss section of a demand paged executable must receive an
   2776      entire page.  If there are symbols, the symbols will start on the
   2777      next page.  If there are no symbols, we must fill out the page by
   2778      hand.  */
   2779   if (bfd_get_symcount (abfd) == 0
   2780       && (abfd->flags & EXEC_P) != 0
   2781       && (abfd->flags & D_PAGED) != 0)
   2782     {
   2783       char c;
   2784 
   2785       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
   2786 		    SEEK_SET) != 0)
   2787 	goto error_return;
   2788       if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0)
   2789 	c = 0;
   2790       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
   2791 		    SEEK_SET) != 0)
   2792 	goto error_return;
   2793       if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1)
   2794 	goto error_return;
   2795     }
   2796 
   2797   if (reloc_buff != NULL)
   2798     bfd_release (abfd, reloc_buff);
   2799   if (buff != NULL)
   2800     free (buff);
   2801   return TRUE;
   2802  error_return:
   2803   if (reloc_buff != NULL)
   2804     bfd_release (abfd, reloc_buff);
   2805   if (buff != NULL)
   2806     free (buff);
   2807   return FALSE;
   2808 }
   2809 
   2810 /* Archive handling.  ECOFF uses what appears to be a unique type of
   2812    archive header (armap).  The byte ordering of the armap and the
   2813    contents are encoded in the name of the armap itself.  At least for
   2814    now, we only support archives with the same byte ordering in the
   2815    armap and the contents.
   2816 
   2817    The first four bytes in the armap are the number of symbol
   2818    definitions.  This is always a power of two.
   2819 
   2820    This is followed by the symbol definitions.  Each symbol definition
   2821    occupies 8 bytes.  The first four bytes are the offset from the
   2822    start of the armap strings to the null-terminated string naming
   2823    this symbol.  The second four bytes are the file offset to the
   2824    archive member which defines this symbol.  If the second four bytes
   2825    are 0, then this is not actually a symbol definition, and it should
   2826    be ignored.
   2827 
   2828    The symbols are hashed into the armap with a closed hashing scheme.
   2829    See the functions below for the details of the algorithm.
   2830 
   2831    After the symbol definitions comes four bytes holding the size of
   2832    the string table, followed by the string table itself.  */
   2833 
   2834 /* The name of an archive headers looks like this:
   2835    __________E[BL]E[BL]_ (with a trailing space).
   2836    The trailing space is changed to an X if the archive is changed to
   2837    indicate that the armap is out of date.
   2838 
   2839    The Alpha seems to use ________64E[BL]E[BL]_.  */
   2840 
   2841 #define ARMAP_BIG_ENDIAN 		'B'
   2842 #define ARMAP_LITTLE_ENDIAN 		'L'
   2843 #define ARMAP_MARKER 			'E'
   2844 #define ARMAP_START_LENGTH 		10
   2845 #define ARMAP_HEADER_MARKER_INDEX	10
   2846 #define ARMAP_HEADER_ENDIAN_INDEX 	11
   2847 #define ARMAP_OBJECT_MARKER_INDEX 	12
   2848 #define ARMAP_OBJECT_ENDIAN_INDEX 	13
   2849 #define ARMAP_END_INDEX 		14
   2850 #define ARMAP_END 			"_ "
   2851 
   2852 /* This is a magic number used in the hashing algorithm.  */
   2853 #define ARMAP_HASH_MAGIC 		0x9dd68ab5
   2854 
   2855 /* This returns the hash value to use for a string.  It also sets
   2856    *REHASH to the rehash adjustment if the first slot is taken.  SIZE
   2857    is the number of entries in the hash table, and HLOG is the log
   2858    base 2 of SIZE.  */
   2859 
   2860 static unsigned int
   2861 ecoff_armap_hash (const char *s,
   2862 		  unsigned int *rehash,
   2863 		  unsigned int size,
   2864 		  unsigned int hlog)
   2865 {
   2866   unsigned int hash;
   2867 
   2868   if (hlog == 0)
   2869     return 0;
   2870   hash = *s++;
   2871   while (*s != '\0')
   2872     hash = ((hash >> 27) | (hash << 5)) + *s++;
   2873   hash *= ARMAP_HASH_MAGIC;
   2874   *rehash = (hash & (size - 1)) | 1;
   2875   return hash >> (32 - hlog);
   2876 }
   2877 
   2878 /* Read in the armap.  */
   2879 
   2880 bfd_boolean
   2881 _bfd_ecoff_slurp_armap (bfd *abfd)
   2882 {
   2883   char nextname[17];
   2884   unsigned int i;
   2885   struct areltdata *mapdata;
   2886   bfd_size_type parsed_size;
   2887   char *raw_armap;
   2888   struct artdata *ardata;
   2889   unsigned int count;
   2890   char *raw_ptr;
   2891   carsym *symdef_ptr;
   2892   char *stringbase;
   2893   bfd_size_type amt;
   2894 
   2895   /* Get the name of the first element.  */
   2896   i = bfd_bread ((void *) nextname, (bfd_size_type) 16, abfd);
   2897   if (i == 0)
   2898       return TRUE;
   2899   if (i != 16)
   2900       return FALSE;
   2901 
   2902   if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
   2903     return FALSE;
   2904 
   2905   /* Irix 4.0.5F apparently can use either an ECOFF armap or a
   2906      standard COFF armap.  We could move the ECOFF armap stuff into
   2907      bfd_slurp_armap, but that seems inappropriate since no other
   2908      target uses this format.  Instead, we check directly for a COFF
   2909      armap.  */
   2910   if (CONST_STRNEQ (nextname, "/               "))
   2911     return bfd_slurp_armap (abfd);
   2912 
   2913   /* See if the first element is an armap.  */
   2914   if (! strneq (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH)
   2915       || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
   2916       || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
   2917 	  && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
   2918       || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
   2919       || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
   2920 	  && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
   2921       || ! strneq (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1))
   2922     {
   2923       bfd_has_map (abfd) = FALSE;
   2924       return TRUE;
   2925     }
   2926 
   2927   /* Make sure we have the right byte ordering.  */
   2928   if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
   2929        ^ (bfd_header_big_endian (abfd)))
   2930       || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
   2931 	  ^ (bfd_big_endian (abfd))))
   2932     {
   2933       bfd_set_error (bfd_error_wrong_format);
   2934       return FALSE;
   2935     }
   2936 
   2937   /* Read in the armap.  */
   2938   ardata = bfd_ardata (abfd);
   2939   mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
   2940   if (mapdata == NULL)
   2941     return FALSE;
   2942   parsed_size = mapdata->parsed_size;
   2943   free (mapdata);
   2944 
   2945   raw_armap = (char *) bfd_alloc (abfd, parsed_size);
   2946   if (raw_armap == NULL)
   2947     return FALSE;
   2948 
   2949   if (bfd_bread ((void *) raw_armap, parsed_size, abfd) != parsed_size)
   2950     {
   2951       if (bfd_get_error () != bfd_error_system_call)
   2952 	bfd_set_error (bfd_error_malformed_archive);
   2953       bfd_release (abfd, (void *) raw_armap);
   2954       return FALSE;
   2955     }
   2956 
   2957   ardata->tdata = (void *) raw_armap;
   2958 
   2959   count = H_GET_32 (abfd, raw_armap);
   2960 
   2961   ardata->symdef_count = 0;
   2962   ardata->cache = NULL;
   2963 
   2964   /* This code used to overlay the symdefs over the raw archive data,
   2965      but that doesn't work on a 64 bit host.  */
   2966   stringbase = raw_armap + count * 8 + 8;
   2967 
   2968 #ifdef CHECK_ARMAP_HASH
   2969   {
   2970     unsigned int hlog;
   2971 
   2972     /* Double check that I have the hashing algorithm right by making
   2973        sure that every symbol can be looked up successfully.  */
   2974     hlog = 0;
   2975     for (i = 1; i < count; i <<= 1)
   2976       hlog++;
   2977     BFD_ASSERT (i == count);
   2978 
   2979     raw_ptr = raw_armap + 4;
   2980     for (i = 0; i < count; i++, raw_ptr += 8)
   2981       {
   2982 	unsigned int name_offset, file_offset;
   2983 	unsigned int hash, rehash, srch;
   2984 
   2985 	name_offset = H_GET_32 (abfd, raw_ptr);
   2986 	file_offset = H_GET_32 (abfd, (raw_ptr + 4));
   2987 	if (file_offset == 0)
   2988 	  continue;
   2989 	hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
   2990 				 hlog);
   2991 	if (hash == i)
   2992 	  continue;
   2993 
   2994 	/* See if we can rehash to this location.  */
   2995 	for (srch = (hash + rehash) & (count - 1);
   2996 	     srch != hash && srch != i;
   2997 	     srch = (srch + rehash) & (count - 1))
   2998 	  BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
   2999 	BFD_ASSERT (srch == i);
   3000       }
   3001   }
   3002 
   3003 #endif /* CHECK_ARMAP_HASH */
   3004 
   3005   raw_ptr = raw_armap + 4;
   3006   for (i = 0; i < count; i++, raw_ptr += 8)
   3007     if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
   3008       ++ardata->symdef_count;
   3009 
   3010   amt = ardata->symdef_count;
   3011   amt *= sizeof (carsym);
   3012   symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
   3013   if (!symdef_ptr)
   3014     return FALSE;
   3015 
   3016   ardata->symdefs = symdef_ptr;
   3017 
   3018   raw_ptr = raw_armap + 4;
   3019   for (i = 0; i < count; i++, raw_ptr += 8)
   3020     {
   3021       unsigned int name_offset, file_offset;
   3022 
   3023       file_offset = H_GET_32 (abfd, (raw_ptr + 4));
   3024       if (file_offset == 0)
   3025 	continue;
   3026       name_offset = H_GET_32 (abfd, raw_ptr);
   3027       symdef_ptr->name = stringbase + name_offset;
   3028       symdef_ptr->file_offset = file_offset;
   3029       ++symdef_ptr;
   3030     }
   3031 
   3032   ardata->first_file_filepos = bfd_tell (abfd);
   3033   /* Pad to an even boundary.  */
   3034   ardata->first_file_filepos += ardata->first_file_filepos % 2;
   3035 
   3036   bfd_has_map (abfd) = TRUE;
   3037 
   3038   return TRUE;
   3039 }
   3040 
   3041 /* Write out an armap.  */
   3042 
   3043 bfd_boolean
   3044 _bfd_ecoff_write_armap (bfd *abfd,
   3045 			unsigned int elength,
   3046 			struct orl *map,
   3047 			unsigned int orl_count,
   3048 			int stridx)
   3049 {
   3050   unsigned int hashsize, hashlog;
   3051   bfd_size_type symdefsize;
   3052   int padit;
   3053   unsigned int stringsize;
   3054   unsigned int mapsize;
   3055   file_ptr firstreal;
   3056   struct ar_hdr hdr;
   3057   struct stat statbuf;
   3058   unsigned int i;
   3059   bfd_byte temp[4];
   3060   bfd_byte *hashtable;
   3061   bfd *current;
   3062   bfd *last_elt;
   3063 
   3064   /* Ultrix appears to use as a hash table size the least power of two
   3065      greater than twice the number of entries.  */
   3066   for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
   3067     ;
   3068   hashsize = 1 << hashlog;
   3069 
   3070   symdefsize = hashsize * 8;
   3071   padit = stridx % 2;
   3072   stringsize = stridx + padit;
   3073 
   3074   /* Include 8 bytes to store symdefsize and stringsize in output.  */
   3075   mapsize = symdefsize + stringsize + 8;
   3076 
   3077   firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
   3078 
   3079   memset ((void *) &hdr, 0, sizeof hdr);
   3080 
   3081   /* Work out the ECOFF armap name.  */
   3082   strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
   3083   hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
   3084   hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
   3085     (bfd_header_big_endian (abfd)
   3086      ? ARMAP_BIG_ENDIAN
   3087      : ARMAP_LITTLE_ENDIAN);
   3088   hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
   3089   hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
   3090     bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
   3091   memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
   3092 
   3093   /* Write the timestamp of the archive header to be just a little bit
   3094      later than the timestamp of the file, otherwise the linker will
   3095      complain that the index is out of date.  Actually, the Ultrix
   3096      linker just checks the archive name; the GNU linker may check the
   3097      date.  */
   3098   stat (abfd->filename, &statbuf);
   3099   _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
   3100 		    (long) (statbuf.st_mtime + 60));
   3101 
   3102   /* The DECstation uses zeroes for the uid, gid and mode of the
   3103      armap.  */
   3104   hdr.ar_uid[0] = '0';
   3105   hdr.ar_gid[0] = '0';
   3106   /* Building gcc ends up extracting the armap as a file - twice.  */
   3107   hdr.ar_mode[0] = '6';
   3108   hdr.ar_mode[1] = '4';
   3109   hdr.ar_mode[2] = '4';
   3110 
   3111   _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
   3112 
   3113   hdr.ar_fmag[0] = '`';
   3114   hdr.ar_fmag[1] = '\012';
   3115 
   3116   /* Turn all null bytes in the header into spaces.  */
   3117   for (i = 0; i < sizeof (struct ar_hdr); i++)
   3118    if (((char *) (&hdr))[i] == '\0')
   3119      (((char *) (&hdr))[i]) = ' ';
   3120 
   3121   if (bfd_bwrite ((void *) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd)
   3122       != sizeof (struct ar_hdr))
   3123     return FALSE;
   3124 
   3125   H_PUT_32 (abfd, hashsize, temp);
   3126   if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
   3127     return FALSE;
   3128 
   3129   hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
   3130   if (!hashtable)
   3131     return FALSE;
   3132 
   3133   current = abfd->archive_head;
   3134   last_elt = current;
   3135   for (i = 0; i < orl_count; i++)
   3136     {
   3137       unsigned int hash, rehash = 0;
   3138 
   3139       /* Advance firstreal to the file position of this archive
   3140 	 element.  */
   3141       if (map[i].u.abfd != last_elt)
   3142 	{
   3143 	  do
   3144 	    {
   3145 	      firstreal += arelt_size (current) + sizeof (struct ar_hdr);
   3146 	      firstreal += firstreal % 2;
   3147 	      current = current->archive_next;
   3148 	    }
   3149 	  while (current != map[i].u.abfd);
   3150 	}
   3151 
   3152       last_elt = current;
   3153 
   3154       hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
   3155       if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
   3156 	{
   3157 	  unsigned int srch;
   3158 
   3159 	  /* The desired slot is already taken.  */
   3160 	  for (srch = (hash + rehash) & (hashsize - 1);
   3161 	       srch != hash;
   3162 	       srch = (srch + rehash) & (hashsize - 1))
   3163 	    if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
   3164 	      break;
   3165 
   3166 	  BFD_ASSERT (srch != hash);
   3167 
   3168 	  hash = srch;
   3169 	}
   3170 
   3171       H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
   3172       H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
   3173     }
   3174 
   3175   if (bfd_bwrite ((void *) hashtable, symdefsize, abfd) != symdefsize)
   3176     return FALSE;
   3177 
   3178   bfd_release (abfd, hashtable);
   3179 
   3180   /* Now write the strings.  */
   3181   H_PUT_32 (abfd, stringsize, temp);
   3182   if (bfd_bwrite ((void *) temp, (bfd_size_type) 4, abfd) != 4)
   3183     return FALSE;
   3184   for (i = 0; i < orl_count; i++)
   3185     {
   3186       bfd_size_type len;
   3187 
   3188       len = strlen (*map[i].name) + 1;
   3189       if (bfd_bwrite ((void *) (*map[i].name), len, abfd) != len)
   3190 	return FALSE;
   3191     }
   3192 
   3193   /* The spec sez this should be a newline.  But in order to be
   3194      bug-compatible for DECstation ar we use a null.  */
   3195   if (padit)
   3196     {
   3197       if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1)
   3198 	return FALSE;
   3199     }
   3200 
   3201   return TRUE;
   3202 }
   3203 
   3204 /* ECOFF linker code.  */
   3206 
   3207 /* Routine to create an entry in an ECOFF link hash table.  */
   3208 
   3209 static struct bfd_hash_entry *
   3210 ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
   3211 			 struct bfd_hash_table *table,
   3212 			 const char *string)
   3213 {
   3214   struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
   3215 
   3216   /* Allocate the structure if it has not already been allocated by a
   3217      subclass.  */
   3218   if (ret == NULL)
   3219     ret = ((struct ecoff_link_hash_entry *)
   3220 	   bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
   3221   if (ret == NULL)
   3222     return NULL;
   3223 
   3224   /* Call the allocation method of the superclass.  */
   3225   ret = ((struct ecoff_link_hash_entry *)
   3226 	 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
   3227 				 table, string));
   3228 
   3229   if (ret)
   3230     {
   3231       /* Set local fields.  */
   3232       ret->indx = -1;
   3233       ret->abfd = NULL;
   3234       ret->written = 0;
   3235       ret->small = 0;
   3236     }
   3237   memset ((void *) &ret->esym, 0, sizeof ret->esym);
   3238 
   3239   return (struct bfd_hash_entry *) ret;
   3240 }
   3241 
   3242 /* Create an ECOFF link hash table.  */
   3243 
   3244 struct bfd_link_hash_table *
   3245 _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
   3246 {
   3247   struct ecoff_link_hash_table *ret;
   3248   bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
   3249 
   3250   ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
   3251   if (ret == NULL)
   3252     return NULL;
   3253   if (!_bfd_link_hash_table_init (&ret->root, abfd,
   3254 				  ecoff_link_hash_newfunc,
   3255 				  sizeof (struct ecoff_link_hash_entry)))
   3256     {
   3257       free (ret);
   3258       return NULL;
   3259     }
   3260   return &ret->root;
   3261 }
   3262 
   3263 /* Look up an entry in an ECOFF link hash table.  */
   3264 
   3265 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
   3266   ((struct ecoff_link_hash_entry *) \
   3267    bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
   3268 
   3269 /* Get the ECOFF link hash table from the info structure.  This is
   3270    just a cast.  */
   3271 
   3272 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
   3273 
   3274 /* Add the external symbols of an object file to the global linker
   3275    hash table.  The external symbols and strings we are passed are
   3276    just allocated on the stack, and will be discarded.  We must
   3277    explicitly save any information we may need later on in the link.
   3278    We do not want to read the external symbol information again.  */
   3279 
   3280 static bfd_boolean
   3281 ecoff_link_add_externals (bfd *abfd,
   3282 			  struct bfd_link_info *info,
   3283 			  void * external_ext,
   3284 			  char *ssext)
   3285 {
   3286   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
   3287   void (* const swap_ext_in) (bfd *, void *, EXTR *)
   3288     = backend->debug_swap.swap_ext_in;
   3289   bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
   3290   unsigned long ext_count;
   3291   struct bfd_link_hash_entry **sym_hash;
   3292   char *ext_ptr;
   3293   char *ext_end;
   3294   bfd_size_type amt;
   3295 
   3296   ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
   3297 
   3298   amt = ext_count;
   3299   amt *= sizeof (struct bfd_link_hash_entry *);
   3300   sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
   3301   if (!sym_hash)
   3302     return FALSE;
   3303   ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
   3304 
   3305   ext_ptr = (char *) external_ext;
   3306   ext_end = ext_ptr + ext_count * external_ext_size;
   3307   for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
   3308     {
   3309       EXTR esym;
   3310       bfd_boolean skip;
   3311       bfd_vma value;
   3312       asection *section;
   3313       const char *name;
   3314       struct ecoff_link_hash_entry *h;
   3315 
   3316       *sym_hash = NULL;
   3317 
   3318       (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
   3319 
   3320       /* Skip debugging symbols.  */
   3321       skip = FALSE;
   3322       switch (esym.asym.st)
   3323 	{
   3324 	case stGlobal:
   3325 	case stStatic:
   3326 	case stLabel:
   3327 	case stProc:
   3328 	case stStaticProc:
   3329 	  break;
   3330 	default:
   3331 	  skip = TRUE;
   3332 	  break;
   3333 	}
   3334 
   3335       if (skip)
   3336 	continue;
   3337 
   3338       /* Get the information for this symbol.  */
   3339       value = esym.asym.value;
   3340       switch (esym.asym.sc)
   3341 	{
   3342 	default:
   3343 	case scNil:
   3344 	case scRegister:
   3345 	case scCdbLocal:
   3346 	case scBits:
   3347 	case scCdbSystem:
   3348 	case scRegImage:
   3349 	case scInfo:
   3350 	case scUserStruct:
   3351 	case scVar:
   3352 	case scVarRegister:
   3353 	case scVariant:
   3354 	case scBasedVar:
   3355 	case scXData:
   3356 	case scPData:
   3357 	  section = NULL;
   3358 	  break;
   3359 	case scText:
   3360 	  section = bfd_make_section_old_way (abfd, _TEXT);
   3361 	  value -= section->vma;
   3362 	  break;
   3363 	case scData:
   3364 	  section = bfd_make_section_old_way (abfd, _DATA);
   3365 	  value -= section->vma;
   3366 	  break;
   3367 	case scBss:
   3368 	  section = bfd_make_section_old_way (abfd, _BSS);
   3369 	  value -= section->vma;
   3370 	  break;
   3371 	case scAbs:
   3372 	  section = bfd_abs_section_ptr;
   3373 	  break;
   3374 	case scUndefined:
   3375 	  section = bfd_und_section_ptr;
   3376 	  break;
   3377 	case scSData:
   3378 	  section = bfd_make_section_old_way (abfd, _SDATA);
   3379 	  value -= section->vma;
   3380 	  break;
   3381 	case scSBss:
   3382 	  section = bfd_make_section_old_way (abfd, _SBSS);
   3383 	  value -= section->vma;
   3384 	  break;
   3385 	case scRData:
   3386 	  section = bfd_make_section_old_way (abfd, _RDATA);
   3387 	  value -= section->vma;
   3388 	  break;
   3389 	case scCommon:
   3390 	  if (value > ecoff_data (abfd)->gp_size)
   3391 	    {
   3392 	      section = bfd_com_section_ptr;
   3393 	      break;
   3394 	    }
   3395 	  /* Fall through.  */
   3396 	case scSCommon:
   3397 	  if (ecoff_scom_section.name == NULL)
   3398 	    {
   3399 	      /* Initialize the small common section.  */
   3400 	      ecoff_scom_section.name = SCOMMON;
   3401 	      ecoff_scom_section.flags = SEC_IS_COMMON;
   3402 	      ecoff_scom_section.output_section = &ecoff_scom_section;
   3403 	      ecoff_scom_section.symbol = &ecoff_scom_symbol;
   3404 	      ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
   3405 	      ecoff_scom_symbol.name = SCOMMON;
   3406 	      ecoff_scom_symbol.flags = BSF_SECTION_SYM;
   3407 	      ecoff_scom_symbol.section = &ecoff_scom_section;
   3408 	      ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
   3409 	    }
   3410 	  section = &ecoff_scom_section;
   3411 	  break;
   3412 	case scSUndefined:
   3413 	  section = bfd_und_section_ptr;
   3414 	  break;
   3415 	case scInit:
   3416 	  section = bfd_make_section_old_way (abfd, _INIT);
   3417 	  value -= section->vma;
   3418 	  break;
   3419 	case scFini:
   3420 	  section = bfd_make_section_old_way (abfd, _FINI);
   3421 	  value -= section->vma;
   3422 	  break;
   3423 	case scRConst:
   3424 	  section = bfd_make_section_old_way (abfd, _RCONST);
   3425 	  value -= section->vma;
   3426 	  break;
   3427 	}
   3428 
   3429       if (section == NULL)
   3430 	continue;
   3431 
   3432       name = ssext + esym.asym.iss;
   3433 
   3434       if (! (_bfd_generic_link_add_one_symbol
   3435 	     (info, abfd, name,
   3436 	      (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
   3437 	      section, value, NULL, TRUE, TRUE, sym_hash)))
   3438 	return FALSE;
   3439 
   3440       h = (struct ecoff_link_hash_entry *) *sym_hash;
   3441 
   3442       /* If we are building an ECOFF hash table, save the external
   3443 	 symbol information.  */
   3444       if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
   3445 	{
   3446 	  if (h->abfd == NULL
   3447 	      || (! bfd_is_und_section (section)
   3448 		  && (! bfd_is_com_section (section)
   3449 		      || (h->root.type != bfd_link_hash_defined
   3450 			  && h->root.type != bfd_link_hash_defweak))))
   3451 	    {
   3452 	      h->abfd = abfd;
   3453 	      h->esym = esym;
   3454 	    }
   3455 
   3456 	  /* Remember whether this symbol was small undefined.  */
   3457 	  if (esym.asym.sc == scSUndefined)
   3458 	    h->small = 1;
   3459 
   3460 	  /* If this symbol was ever small undefined, it needs to wind
   3461 	     up in a GP relative section.  We can't control the
   3462 	     section of a defined symbol, but we can control the
   3463 	     section of a common symbol.  This case is actually needed
   3464 	     on Ultrix 4.2 to handle the symbol cred in -lckrb.  */
   3465 	  if (h->small
   3466 	      && h->root.type == bfd_link_hash_common
   3467 	      && streq (h->root.u.c.p->section->name, SCOMMON))
   3468 	    {
   3469 	      h->root.u.c.p->section = bfd_make_section_old_way (abfd,
   3470 								 SCOMMON);
   3471 	      h->root.u.c.p->section->flags = SEC_ALLOC;
   3472 	      if (h->esym.asym.sc == scCommon)
   3473 		h->esym.asym.sc = scSCommon;
   3474 	    }
   3475 	}
   3476     }
   3477 
   3478   return TRUE;
   3479 }
   3480 
   3481 /* Add symbols from an ECOFF object file to the global linker hash
   3482    table.  */
   3483 
   3484 static bfd_boolean
   3485 ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
   3486 {
   3487   HDRR *symhdr;
   3488   bfd_size_type external_ext_size;
   3489   void * external_ext = NULL;
   3490   bfd_size_type esize;
   3491   char *ssext = NULL;
   3492   bfd_boolean result;
   3493 
   3494   if (! ecoff_slurp_symbolic_header (abfd))
   3495     return FALSE;
   3496 
   3497   /* If there are no symbols, we don't want it.  */
   3498   if (bfd_get_symcount (abfd) == 0)
   3499     return TRUE;
   3500 
   3501   symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
   3502 
   3503   /* Read in the external symbols and external strings.  */
   3504   external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
   3505   esize = symhdr->iextMax * external_ext_size;
   3506   external_ext = bfd_malloc (esize);
   3507   if (external_ext == NULL && esize != 0)
   3508     goto error_return;
   3509 
   3510   if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
   3511       || bfd_bread (external_ext, esize, abfd) != esize)
   3512     goto error_return;
   3513 
   3514   ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
   3515   if (ssext == NULL && symhdr->issExtMax != 0)
   3516     goto error_return;
   3517 
   3518   if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
   3519       || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
   3520 	  != (bfd_size_type) symhdr->issExtMax))
   3521     goto error_return;
   3522 
   3523   result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
   3524 
   3525   if (ssext != NULL)
   3526     free (ssext);
   3527   if (external_ext != NULL)
   3528     free (external_ext);
   3529   return result;
   3530 
   3531  error_return:
   3532   if (ssext != NULL)
   3533     free (ssext);
   3534   if (external_ext != NULL)
   3535     free (external_ext);
   3536   return FALSE;
   3537 }
   3538 
   3539 /* This is called if we used _bfd_generic_link_add_archive_symbols
   3540    because we were not dealing with an ECOFF archive.  */
   3541 
   3542 static bfd_boolean
   3543 ecoff_link_check_archive_element (bfd *abfd,
   3544 				  struct bfd_link_info *info,
   3545 				  struct bfd_link_hash_entry *h,
   3546 				  const char *name,
   3547 				  bfd_boolean *pneeded)
   3548 {
   3549   *pneeded = FALSE;
   3550 
   3551   /* Unlike the generic linker, we do not pull in elements because
   3552      of common symbols.  */
   3553   if (h->type != bfd_link_hash_undefined)
   3554     return TRUE;
   3555 
   3556   /* Include this element?  */
   3557   if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
   3558     return TRUE;
   3559   *pneeded = TRUE;
   3560 
   3561   return ecoff_link_add_object_symbols (abfd, info);
   3562 }
   3563 
   3564 /* Add the symbols from an archive file to the global hash table.
   3565    This looks through the undefined symbols, looks each one up in the
   3566    archive hash table, and adds any associated object file.  We do not
   3567    use _bfd_generic_link_add_archive_symbols because ECOFF archives
   3568    already have a hash table, so there is no reason to construct
   3569    another one.  */
   3570 
   3571 static bfd_boolean
   3572 ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
   3573 {
   3574   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
   3575   const bfd_byte *raw_armap;
   3576   struct bfd_link_hash_entry **pundef;
   3577   unsigned int armap_count;
   3578   unsigned int armap_log;
   3579   unsigned int i;
   3580   const bfd_byte *hashtable;
   3581   const char *stringbase;
   3582 
   3583   if (! bfd_has_map (abfd))
   3584     {
   3585       /* An empty archive is a special case.  */
   3586       if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
   3587 	return TRUE;
   3588       bfd_set_error (bfd_error_no_armap);
   3589       return FALSE;
   3590     }
   3591 
   3592   /* If we don't have any raw data for this archive, as can happen on
   3593      Irix 4.0.5F, we call the generic routine.
   3594      FIXME: We should be more clever about this, since someday tdata
   3595      may get to something for a generic archive.  */
   3596   raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
   3597   if (raw_armap == NULL)
   3598     return (_bfd_generic_link_add_archive_symbols
   3599 	    (abfd, info, ecoff_link_check_archive_element));
   3600 
   3601   armap_count = H_GET_32 (abfd, raw_armap);
   3602 
   3603   armap_log = 0;
   3604   for (i = 1; i < armap_count; i <<= 1)
   3605     armap_log++;
   3606   BFD_ASSERT (i == armap_count);
   3607 
   3608   hashtable = raw_armap + 4;
   3609   stringbase = (const char *) raw_armap + armap_count * 8 + 8;
   3610 
   3611   /* Look through the list of undefined symbols.  */
   3612   pundef = &info->hash->undefs;
   3613   while (*pundef != NULL)
   3614     {
   3615       struct bfd_link_hash_entry *h;
   3616       unsigned int hash, rehash = 0;
   3617       unsigned int file_offset;
   3618       const char *name;
   3619       bfd *element;
   3620 
   3621       h = *pundef;
   3622 
   3623       /* When a symbol is defined, it is not necessarily removed from
   3624 	 the list.  */
   3625       if (h->type != bfd_link_hash_undefined
   3626 	  && h->type != bfd_link_hash_common)
   3627 	{
   3628 	  /* Remove this entry from the list, for general cleanliness
   3629 	     and because we are going to look through the list again
   3630 	     if we search any more libraries.  We can't remove the
   3631 	     entry if it is the tail, because that would lose any
   3632 	     entries we add to the list later on.  */
   3633 	  if (*pundef != info->hash->undefs_tail)
   3634 	    *pundef = (*pundef)->u.undef.next;
   3635 	  else
   3636 	    pundef = &(*pundef)->u.undef.next;
   3637 	  continue;
   3638 	}
   3639 
   3640       /* Native ECOFF linkers do not pull in archive elements merely
   3641 	 to satisfy common definitions, so neither do we.  We leave
   3642 	 them on the list, though, in case we are linking against some
   3643 	 other object format.  */
   3644       if (h->type != bfd_link_hash_undefined)
   3645 	{
   3646 	  pundef = &(*pundef)->u.undef.next;
   3647 	  continue;
   3648 	}
   3649 
   3650       /* Look for this symbol in the archive hash table.  */
   3651       hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
   3652 			       armap_log);
   3653 
   3654       file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
   3655       if (file_offset == 0)
   3656 	{
   3657 	  /* Nothing in this slot.  */
   3658 	  pundef = &(*pundef)->u.undef.next;
   3659 	  continue;
   3660 	}
   3661 
   3662       name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
   3663       if (name[0] != h->root.string[0]
   3664 	  || ! streq (name, h->root.string))
   3665 	{
   3666 	  unsigned int srch;
   3667 	  bfd_boolean found;
   3668 
   3669 	  /* That was the wrong symbol.  Try rehashing.  */
   3670 	  found = FALSE;
   3671 	  for (srch = (hash + rehash) & (armap_count - 1);
   3672 	       srch != hash;
   3673 	       srch = (srch + rehash) & (armap_count - 1))
   3674 	    {
   3675 	      file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
   3676 	      if (file_offset == 0)
   3677 		break;
   3678 	      name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
   3679 	      if (name[0] == h->root.string[0]
   3680 		  && streq (name, h->root.string))
   3681 		{
   3682 		  found = TRUE;
   3683 		  break;
   3684 		}
   3685 	    }
   3686 
   3687 	  if (! found)
   3688 	    {
   3689 	      pundef = &(*pundef)->u.undef.next;
   3690 	      continue;
   3691 	    }
   3692 
   3693 	  hash = srch;
   3694 	}
   3695 
   3696       element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
   3697       if (element == NULL)
   3698 	return FALSE;
   3699 
   3700       if (! bfd_check_format (element, bfd_object))
   3701 	return FALSE;
   3702 
   3703       /* Unlike the generic linker, we know that this element provides
   3704 	 a definition for an undefined symbol and we know that we want
   3705 	 to include it.  We don't need to check anything.  */
   3706       if (!(*info->callbacks
   3707 	    ->add_archive_element) (info, element, name, &element))
   3708 	return FALSE;
   3709       if (! ecoff_link_add_object_symbols (element, info))
   3710 	return FALSE;
   3711 
   3712       pundef = &(*pundef)->u.undef.next;
   3713     }
   3714 
   3715   return TRUE;
   3716 }
   3717 
   3718 /* Given an ECOFF BFD, add symbols to the global hash table as
   3719    appropriate.  */
   3720 
   3721 bfd_boolean
   3722 _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
   3723 {
   3724   switch (bfd_get_format (abfd))
   3725     {
   3726     case bfd_object:
   3727       return ecoff_link_add_object_symbols (abfd, info);
   3728     case bfd_archive:
   3729       return ecoff_link_add_archive_symbols (abfd, info);
   3730     default:
   3731       bfd_set_error (bfd_error_wrong_format);
   3732       return FALSE;
   3733     }
   3734 }
   3735 
   3736 
   3737 /* ECOFF final link routines.  */
   3739 
   3740 /* Structure used to pass information to ecoff_link_write_external.  */
   3741 
   3742 struct extsym_info
   3743 {
   3744   bfd *abfd;
   3745   struct bfd_link_info *info;
   3746 };
   3747 
   3748 /* Accumulate the debugging information for an input BFD into the
   3749    output BFD.  This must read in the symbolic information of the
   3750    input BFD.  */
   3751 
   3752 static bfd_boolean
   3753 ecoff_final_link_debug_accumulate (bfd *output_bfd,
   3754 				   bfd *input_bfd,
   3755 				   struct bfd_link_info *info,
   3756 				   void * handle)
   3757 {
   3758   struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
   3759   const struct ecoff_debug_swap * const swap =
   3760     &ecoff_backend (input_bfd)->debug_swap;
   3761   HDRR *symhdr = &debug->symbolic_header;
   3762   bfd_boolean ret;
   3763 
   3764 #define READ(ptr, offset, count, size, type)				 \
   3765   if (symhdr->count == 0)						 \
   3766     debug->ptr = NULL;							 \
   3767   else									 \
   3768     {									 \
   3769       bfd_size_type amt = (bfd_size_type) size * symhdr->count;		 \
   3770       debug->ptr = (type) bfd_malloc (amt);                              \
   3771       if (debug->ptr == NULL)						 \
   3772 	{								 \
   3773           ret = FALSE;							 \
   3774           goto return_something;					 \
   3775 	}								 \
   3776       if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
   3777 	  || bfd_bread (debug->ptr, amt, input_bfd) != amt)		 \
   3778 	{								 \
   3779           ret = FALSE;							 \
   3780           goto return_something;					 \
   3781 	}								 \
   3782     }
   3783 
   3784   /* If raw_syments is not NULL, then the data was already by read by
   3785      _bfd_ecoff_slurp_symbolic_info.  */
   3786   if (ecoff_data (input_bfd)->raw_syments == NULL)
   3787     {
   3788       READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
   3789 	    unsigned char *);
   3790       READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, void *);
   3791       READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, void *);
   3792       READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, void *);
   3793       READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, void *);
   3794       READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
   3795 	    union aux_ext *);
   3796       READ (ss, cbSsOffset, issMax, sizeof (char), char *);
   3797       READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, void *);
   3798       READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, void *);
   3799     }
   3800 #undef READ
   3801 
   3802   /* We do not read the external strings or the external symbols.  */
   3803 
   3804   ret = (bfd_ecoff_debug_accumulate
   3805 	 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
   3806 	  &ecoff_backend (output_bfd)->debug_swap,
   3807 	  input_bfd, debug, swap, info));
   3808 
   3809  return_something:
   3810   if (ecoff_data (input_bfd)->raw_syments == NULL)
   3811     {
   3812       if (debug->line != NULL)
   3813 	free (debug->line);
   3814       if (debug->external_dnr != NULL)
   3815 	free (debug->external_dnr);
   3816       if (debug->external_pdr != NULL)
   3817 	free (debug->external_pdr);
   3818       if (debug->external_sym != NULL)
   3819 	free (debug->external_sym);
   3820       if (debug->external_opt != NULL)
   3821 	free (debug->external_opt);
   3822       if (debug->external_aux != NULL)
   3823 	free (debug->external_aux);
   3824       if (debug->ss != NULL)
   3825 	free (debug->ss);
   3826       if (debug->external_fdr != NULL)
   3827 	free (debug->external_fdr);
   3828       if (debug->external_rfd != NULL)
   3829 	free (debug->external_rfd);
   3830 
   3831       /* Make sure we don't accidentally follow one of these pointers
   3832 	 into freed memory.  */
   3833       debug->line = NULL;
   3834       debug->external_dnr = NULL;
   3835       debug->external_pdr = NULL;
   3836       debug->external_sym = NULL;
   3837       debug->external_opt = NULL;
   3838       debug->external_aux = NULL;
   3839       debug->ss = NULL;
   3840       debug->external_fdr = NULL;
   3841       debug->external_rfd = NULL;
   3842     }
   3843 
   3844   return ret;
   3845 }
   3846 
   3847 /* Relocate and write an ECOFF section into an ECOFF output file.  */
   3848 
   3849 static bfd_boolean
   3850 ecoff_indirect_link_order (bfd *output_bfd,
   3851 			   struct bfd_link_info *info,
   3852 			   asection *output_section,
   3853 			   struct bfd_link_order *link_order)
   3854 {
   3855   asection *input_section;
   3856   bfd *input_bfd;
   3857   bfd_byte *contents = NULL;
   3858   bfd_size_type external_reloc_size;
   3859   bfd_size_type external_relocs_size;
   3860   void * external_relocs = NULL;
   3861 
   3862   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
   3863 
   3864   input_section = link_order->u.indirect.section;
   3865   input_bfd = input_section->owner;
   3866   if (input_section->size == 0)
   3867     return TRUE;
   3868 
   3869   BFD_ASSERT (input_section->output_section == output_section);
   3870   BFD_ASSERT (input_section->output_offset == link_order->offset);
   3871   BFD_ASSERT (input_section->size == link_order->size);
   3872 
   3873   /* Get the section contents.  */
   3874   if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
   3875     goto error_return;
   3876 
   3877   /* Get the relocs.  If we are relaxing MIPS code, they will already
   3878      have been read in.  Otherwise, we read them in now.  */
   3879   external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
   3880   external_relocs_size = external_reloc_size * input_section->reloc_count;
   3881 
   3882   external_relocs = bfd_malloc (external_relocs_size);
   3883   if (external_relocs == NULL && external_relocs_size != 0)
   3884     goto error_return;
   3885 
   3886   if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
   3887       || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
   3888 	  != external_relocs_size))
   3889     goto error_return;
   3890 
   3891   /* Relocate the section contents.  */
   3892   if (! ((*ecoff_backend (input_bfd)->relocate_section)
   3893 	 (output_bfd, info, input_bfd, input_section, contents,
   3894 	  external_relocs)))
   3895     goto error_return;
   3896 
   3897   /* Write out the relocated section.  */
   3898   if (! bfd_set_section_contents (output_bfd,
   3899 				  output_section,
   3900 				  contents,
   3901 				  input_section->output_offset,
   3902 				  input_section->size))
   3903     goto error_return;
   3904 
   3905   /* If we are producing relocatable output, the relocs were
   3906      modified, and we write them out now.  We use the reloc_count
   3907      field of output_section to keep track of the number of relocs we
   3908      have output so far.  */
   3909   if (bfd_link_relocatable (info))
   3910     {
   3911       file_ptr pos = (output_section->rel_filepos
   3912 		      + output_section->reloc_count * external_reloc_size);
   3913       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
   3914 	  || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
   3915 	      != external_relocs_size))
   3916 	goto error_return;
   3917       output_section->reloc_count += input_section->reloc_count;
   3918     }
   3919 
   3920   if (contents != NULL)
   3921     free (contents);
   3922   if (external_relocs != NULL)
   3923     free (external_relocs);
   3924   return TRUE;
   3925 
   3926  error_return:
   3927   if (contents != NULL)
   3928     free (contents);
   3929   if (external_relocs != NULL)
   3930     free (external_relocs);
   3931   return FALSE;
   3932 }
   3933 
   3934 /* Generate a reloc when linking an ECOFF file.  This is a reloc
   3935    requested by the linker, and does come from any input file.  This
   3936    is used to build constructor and destructor tables when linking
   3937    with -Ur.  */
   3938 
   3939 static bfd_boolean
   3940 ecoff_reloc_link_order (bfd *output_bfd,
   3941 			struct bfd_link_info *info,
   3942 			asection *output_section,
   3943 			struct bfd_link_order *link_order)
   3944 {
   3945   enum bfd_link_order_type type;
   3946   asection *section;
   3947   bfd_vma addend;
   3948   arelent rel;
   3949   struct internal_reloc in;
   3950   bfd_size_type external_reloc_size;
   3951   bfd_byte *rbuf;
   3952   bfd_boolean ok;
   3953   file_ptr pos;
   3954 
   3955   type = link_order->type;
   3956   section = NULL;
   3957   addend = link_order->u.reloc.p->addend;
   3958 
   3959   /* We set up an arelent to pass to the backend adjust_reloc_out
   3960      routine.  */
   3961   rel.address = link_order->offset;
   3962 
   3963   rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
   3964   if (rel.howto == 0)
   3965     {
   3966       bfd_set_error (bfd_error_bad_value);
   3967       return FALSE;
   3968     }
   3969 
   3970   if (type == bfd_section_reloc_link_order)
   3971     {
   3972       section = link_order->u.reloc.p->u.section;
   3973       rel.sym_ptr_ptr = section->symbol_ptr_ptr;
   3974     }
   3975   else
   3976     {
   3977       struct bfd_link_hash_entry *h;
   3978 
   3979       /* Treat a reloc against a defined symbol as though it were
   3980          actually against the section.  */
   3981       h = bfd_wrapped_link_hash_lookup (output_bfd, info,
   3982 					link_order->u.reloc.p->u.name,
   3983 					FALSE, FALSE, FALSE);
   3984       if (h != NULL
   3985 	  && (h->type == bfd_link_hash_defined
   3986 	      || h->type == bfd_link_hash_defweak))
   3987 	{
   3988 	  type = bfd_section_reloc_link_order;
   3989 	  section = h->u.def.section->output_section;
   3990 	  /* It seems that we ought to add the symbol value to the
   3991              addend here, but in practice it has already been added
   3992              because it was passed to constructor_callback.  */
   3993 	  addend += section->vma + h->u.def.section->output_offset;
   3994 	}
   3995       else
   3996 	{
   3997 	  /* We can't set up a reloc against a symbol correctly,
   3998 	     because we have no asymbol structure.  Currently no
   3999 	     adjust_reloc_out routine cares.  */
   4000 	  rel.sym_ptr_ptr = NULL;
   4001 	}
   4002     }
   4003 
   4004   /* All ECOFF relocs are in-place.  Put the addend into the object
   4005      file.  */
   4006 
   4007   BFD_ASSERT (rel.howto->partial_inplace);
   4008   if (addend != 0)
   4009     {
   4010       bfd_size_type size;
   4011       bfd_reloc_status_type rstat;
   4012       bfd_byte *buf;
   4013 
   4014       size = bfd_get_reloc_size (rel.howto);
   4015       buf = (bfd_byte *) bfd_zmalloc (size);
   4016       if (buf == NULL && size != 0)
   4017 	return FALSE;
   4018       rstat = _bfd_relocate_contents (rel.howto, output_bfd,
   4019 				      (bfd_vma) addend, buf);
   4020       switch (rstat)
   4021 	{
   4022 	case bfd_reloc_ok:
   4023 	  break;
   4024 	default:
   4025 	case bfd_reloc_outofrange:
   4026 	  abort ();
   4027 	case bfd_reloc_overflow:
   4028 	  (*info->callbacks->reloc_overflow)
   4029 	    (info, NULL,
   4030 	     (link_order->type == bfd_section_reloc_link_order
   4031 	      ? bfd_section_name (output_bfd, section)
   4032 	      : link_order->u.reloc.p->u.name),
   4033 	     rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
   4034 	  break;
   4035 	}
   4036       ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
   4037 				     (file_ptr) link_order->offset, size);
   4038       free (buf);
   4039       if (! ok)
   4040 	return FALSE;
   4041     }
   4042 
   4043   rel.addend = 0;
   4044 
   4045   /* Move the information into an internal_reloc structure.  */
   4046   in.r_vaddr = (rel.address
   4047 		+ bfd_get_section_vma (output_bfd, output_section));
   4048   in.r_type = rel.howto->type;
   4049 
   4050   if (type == bfd_symbol_reloc_link_order)
   4051     {
   4052       struct ecoff_link_hash_entry *h;
   4053 
   4054       h = ((struct ecoff_link_hash_entry *)
   4055 	   bfd_wrapped_link_hash_lookup (output_bfd, info,
   4056 					 link_order->u.reloc.p->u.name,
   4057 					 FALSE, FALSE, TRUE));
   4058       if (h != NULL
   4059 	  && h->indx != -1)
   4060 	in.r_symndx = h->indx;
   4061       else
   4062 	{
   4063 	  (*info->callbacks->unattached_reloc)
   4064 	    (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
   4065 	  in.r_symndx = 0;
   4066 	}
   4067       in.r_extern = 1;
   4068     }
   4069   else
   4070     {
   4071       const char *name;
   4072       unsigned int i;
   4073       static struct
   4074       {
   4075 	const char * name;
   4076 	long r_symndx;
   4077       }
   4078       section_symndx [] =
   4079       {
   4080 	{ _TEXT,   RELOC_SECTION_TEXT   },
   4081 	{ _RDATA,  RELOC_SECTION_RDATA  },
   4082 	{ _DATA,   RELOC_SECTION_DATA   },
   4083 	{ _SDATA,  RELOC_SECTION_SDATA  },
   4084 	{ _SBSS,   RELOC_SECTION_SBSS   },
   4085 	{ _BSS,    RELOC_SECTION_BSS    },
   4086 	{ _INIT,   RELOC_SECTION_INIT   },
   4087 	{ _LIT8,   RELOC_SECTION_LIT8   },
   4088 	{ _LIT4,   RELOC_SECTION_LIT4   },
   4089 	{ _XDATA,  RELOC_SECTION_XDATA  },
   4090 	{ _PDATA,  RELOC_SECTION_PDATA  },
   4091 	{ _FINI,   RELOC_SECTION_FINI   },
   4092 	{ _LITA,   RELOC_SECTION_LITA   },
   4093 	{ "*ABS*", RELOC_SECTION_ABS    },
   4094 	{ _RCONST, RELOC_SECTION_RCONST }
   4095       };
   4096 
   4097       name = bfd_get_section_name (output_bfd, section);
   4098 
   4099       for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
   4100 	if (streq (name, section_symndx[i].name))
   4101 	  {
   4102 	    in.r_symndx = section_symndx[i].r_symndx;
   4103 	    break;
   4104 	  }
   4105 
   4106       if (i == ARRAY_SIZE (section_symndx))
   4107 	abort ();
   4108 
   4109       in.r_extern = 0;
   4110     }
   4111 
   4112   /* Let the BFD backend adjust the reloc.  */
   4113   (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
   4114 
   4115   /* Get some memory and swap out the reloc.  */
   4116   external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
   4117   rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
   4118   if (rbuf == NULL)
   4119     return FALSE;
   4120 
   4121   (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
   4122 
   4123   pos = (output_section->rel_filepos
   4124 	 + output_section->reloc_count * external_reloc_size);
   4125   ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
   4126 	&& (bfd_bwrite ((void *) rbuf, external_reloc_size, output_bfd)
   4127 	    == external_reloc_size));
   4128 
   4129   if (ok)
   4130     ++output_section->reloc_count;
   4131 
   4132   free (rbuf);
   4133 
   4134   return ok;
   4135 }
   4136 
   4137 /* Put out information for an external symbol.  These come only from
   4138    the hash table.  */
   4139 
   4140 static bfd_boolean
   4141 ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
   4142 {
   4143   struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
   4144   struct extsym_info *einfo = (struct extsym_info *) data;
   4145   bfd *output_bfd = einfo->abfd;
   4146   bfd_boolean strip;
   4147 
   4148   if (h->root.type == bfd_link_hash_warning)
   4149     {
   4150       h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
   4151       if (h->root.type == bfd_link_hash_new)
   4152 	return TRUE;
   4153     }
   4154 
   4155   /* We need to check if this symbol is being stripped.  */
   4156   if (h->root.type == bfd_link_hash_undefined
   4157       || h->root.type == bfd_link_hash_undefweak)
   4158     strip = FALSE;
   4159   else if (einfo->info->strip == strip_all
   4160 	   || (einfo->info->strip == strip_some
   4161 	       && bfd_hash_lookup (einfo->info->keep_hash,
   4162 				   h->root.root.string,
   4163 				   FALSE, FALSE) == NULL))
   4164     strip = TRUE;
   4165   else
   4166     strip = FALSE;
   4167 
   4168   if (strip || h->written)
   4169     return TRUE;
   4170 
   4171   if (h->abfd == NULL)
   4172     {
   4173       h->esym.jmptbl = 0;
   4174       h->esym.cobol_main = 0;
   4175       h->esym.weakext = 0;
   4176       h->esym.reserved = 0;
   4177       h->esym.ifd = ifdNil;
   4178       h->esym.asym.value = 0;
   4179       h->esym.asym.st = stGlobal;
   4180 
   4181       if (h->root.type != bfd_link_hash_defined
   4182 	  && h->root.type != bfd_link_hash_defweak)
   4183 	h->esym.asym.sc = scAbs;
   4184       else
   4185 	{
   4186 	  asection *output_section;
   4187 	  const char *name;
   4188 	  unsigned int i;
   4189 	  static struct
   4190 	  {
   4191 	    const char * name;
   4192 	    int sc;
   4193 	  }
   4194 	  section_storage_classes [] =
   4195 	  {
   4196 	    { _TEXT,   scText   },
   4197 	    { _DATA,   scData   },
   4198 	    { _SDATA,  scSData  },
   4199 	    { _RDATA,  scRData  },
   4200 	    { _BSS,    scBss    },
   4201 	    { _SBSS,   scSBss   },
   4202 	    { _INIT,   scInit   },
   4203 	    { _FINI,   scFini   },
   4204 	    { _PDATA,  scPData  },
   4205 	    { _XDATA,  scXData  },
   4206 	    { _RCONST, scRConst }
   4207 	  };
   4208 
   4209 	  output_section = h->root.u.def.section->output_section;
   4210 	  name = bfd_section_name (output_section->owner, output_section);
   4211 
   4212 	  for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
   4213 	    if (streq (name, section_storage_classes[i].name))
   4214 	      {
   4215 		h->esym.asym.sc = section_storage_classes[i].sc;
   4216 		break;
   4217 	      }
   4218 
   4219 	  if (i == ARRAY_SIZE (section_storage_classes))
   4220 	    h->esym.asym.sc = scAbs;
   4221 	}
   4222 
   4223       h->esym.asym.reserved = 0;
   4224       h->esym.asym.index = indexNil;
   4225     }
   4226   else if (h->esym.ifd != -1)
   4227     {
   4228       struct ecoff_debug_info *debug;
   4229 
   4230       /* Adjust the FDR index for the symbol by that used for the
   4231 	 input BFD.  */
   4232       debug = &ecoff_data (h->abfd)->debug_info;
   4233       BFD_ASSERT (h->esym.ifd >= 0
   4234 		  && h->esym.ifd < debug->symbolic_header.ifdMax);
   4235       h->esym.ifd = debug->ifdmap[h->esym.ifd];
   4236     }
   4237 
   4238   switch (h->root.type)
   4239     {
   4240     default:
   4241     case bfd_link_hash_warning:
   4242     case bfd_link_hash_new:
   4243       abort ();
   4244     case bfd_link_hash_undefined:
   4245     case bfd_link_hash_undefweak:
   4246       if (h->esym.asym.sc != scUndefined
   4247 	  && h->esym.asym.sc != scSUndefined)
   4248 	h->esym.asym.sc = scUndefined;
   4249       break;
   4250     case bfd_link_hash_defined:
   4251     case bfd_link_hash_defweak:
   4252       if (h->esym.asym.sc == scUndefined
   4253 	  || h->esym.asym.sc == scSUndefined)
   4254 	h->esym.asym.sc = scAbs;
   4255       else if (h->esym.asym.sc == scCommon)
   4256 	h->esym.asym.sc = scBss;
   4257       else if (h->esym.asym.sc == scSCommon)
   4258 	h->esym.asym.sc = scSBss;
   4259       h->esym.asym.value = (h->root.u.def.value
   4260 			    + h->root.u.def.section->output_section->vma
   4261 			    + h->root.u.def.section->output_offset);
   4262       break;
   4263     case bfd_link_hash_common:
   4264       if (h->esym.asym.sc != scCommon
   4265 	  && h->esym.asym.sc != scSCommon)
   4266 	h->esym.asym.sc = scCommon;
   4267       h->esym.asym.value = h->root.u.c.size;
   4268       break;
   4269     case bfd_link_hash_indirect:
   4270       /* We ignore these symbols, since the indirected symbol is
   4271 	 already in the hash table.  */
   4272       return TRUE;
   4273     }
   4274 
   4275   /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
   4276      symbol number.  */
   4277   h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
   4278   h->written = 1;
   4279 
   4280   return (bfd_ecoff_debug_one_external
   4281 	  (output_bfd, &ecoff_data (output_bfd)->debug_info,
   4282 	   &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
   4283 	   &h->esym));
   4284 }
   4285 
   4286 /* ECOFF final link routine.  This looks through all the input BFDs
   4287    and gathers together all the debugging information, and then
   4288    processes all the link order information.  This may cause it to
   4289    close and reopen some input BFDs; I'll see how bad this is.  */
   4290 
   4291 bfd_boolean
   4292 _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
   4293 {
   4294   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
   4295   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
   4296   HDRR *symhdr;
   4297   void * handle;
   4298   bfd *input_bfd;
   4299   asection *o;
   4300   struct bfd_link_order *p;
   4301   struct extsym_info einfo;
   4302 
   4303   /* We accumulate the debugging information counts in the symbolic
   4304      header.  */
   4305   symhdr = &debug->symbolic_header;
   4306   symhdr->vstamp = 0;
   4307   symhdr->ilineMax = 0;
   4308   symhdr->cbLine = 0;
   4309   symhdr->idnMax = 0;
   4310   symhdr->ipdMax = 0;
   4311   symhdr->isymMax = 0;
   4312   symhdr->ioptMax = 0;
   4313   symhdr->iauxMax = 0;
   4314   symhdr->issMax = 0;
   4315   symhdr->issExtMax = 0;
   4316   symhdr->ifdMax = 0;
   4317   symhdr->crfd = 0;
   4318   symhdr->iextMax = 0;
   4319 
   4320   /* We accumulate the debugging information itself in the debug_info
   4321      structure.  */
   4322   debug->line = NULL;
   4323   debug->external_dnr = NULL;
   4324   debug->external_pdr = NULL;
   4325   debug->external_sym = NULL;
   4326   debug->external_opt = NULL;
   4327   debug->external_aux = NULL;
   4328   debug->ss = NULL;
   4329   debug->ssext = debug->ssext_end = NULL;
   4330   debug->external_fdr = NULL;
   4331   debug->external_rfd = NULL;
   4332   debug->external_ext = debug->external_ext_end = NULL;
   4333 
   4334   handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
   4335   if (handle == NULL)
   4336     return FALSE;
   4337 
   4338   /* Accumulate the debugging symbols from each input BFD.  */
   4339   for (input_bfd = info->input_bfds;
   4340        input_bfd != NULL;
   4341        input_bfd = input_bfd->link.next)
   4342     {
   4343       bfd_boolean ret;
   4344 
   4345       if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
   4346 	{
   4347 	  /* Arbitrarily set the symbolic header vstamp to the vstamp
   4348 	     of the first object file in the link.  */
   4349 	  if (symhdr->vstamp == 0)
   4350 	    symhdr->vstamp
   4351 	      = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
   4352 	  ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
   4353 						   handle);
   4354 	}
   4355       else
   4356 	ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
   4357 						debug, &backend->debug_swap,
   4358 						input_bfd, info);
   4359       if (! ret)
   4360 	return FALSE;
   4361 
   4362       /* Combine the register masks.  */
   4363       ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
   4364       ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
   4365       ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
   4366       ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
   4367       ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
   4368       ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
   4369     }
   4370 
   4371   /* Write out the external symbols.  */
   4372   einfo.abfd = abfd;
   4373   einfo.info = info;
   4374   bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
   4375 
   4376   if (bfd_link_relocatable (info))
   4377     {
   4378       /* We need to make a pass over the link_orders to count up the
   4379 	 number of relocations we will need to output, so that we know
   4380 	 how much space they will take up.  */
   4381       for (o = abfd->sections; o != NULL; o = o->next)
   4382 	{
   4383 	  o->reloc_count = 0;
   4384 	  for (p = o->map_head.link_order;
   4385 	       p != NULL;
   4386 	       p = p->next)
   4387 	    if (p->type == bfd_indirect_link_order)
   4388 	      o->reloc_count += p->u.indirect.section->reloc_count;
   4389 	    else if (p->type == bfd_section_reloc_link_order
   4390 		     || p->type == bfd_symbol_reloc_link_order)
   4391 	      ++o->reloc_count;
   4392 	}
   4393     }
   4394 
   4395   /* Compute the reloc and symbol file positions.  */
   4396   ecoff_compute_reloc_file_positions (abfd);
   4397 
   4398   /* Write out the debugging information.  */
   4399   if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
   4400 					   &backend->debug_swap, info,
   4401 					   ecoff_data (abfd)->sym_filepos))
   4402     return FALSE;
   4403 
   4404   bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
   4405 
   4406   if (bfd_link_relocatable (info))
   4407     {
   4408       /* Now reset the reloc_count field of the sections in the output
   4409 	 BFD to 0, so that we can use them to keep track of how many
   4410 	 relocs we have output thus far.  */
   4411       for (o = abfd->sections; o != NULL; o = o->next)
   4412 	o->reloc_count = 0;
   4413     }
   4414 
   4415   /* Get a value for the GP register.  */
   4416   if (ecoff_data (abfd)->gp == 0)
   4417     {
   4418       struct bfd_link_hash_entry *h;
   4419 
   4420       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
   4421       if (h != NULL
   4422 	  && h->type == bfd_link_hash_defined)
   4423 	ecoff_data (abfd)->gp = (h->u.def.value
   4424 				 + h->u.def.section->output_section->vma
   4425 				 + h->u.def.section->output_offset);
   4426       else if (bfd_link_relocatable (info))
   4427 	{
   4428 	  bfd_vma lo;
   4429 
   4430 	  /* Make up a value.  */
   4431 	  lo = (bfd_vma) -1;
   4432 	  for (o = abfd->sections; o != NULL; o = o->next)
   4433 	    {
   4434 	      if (o->vma < lo
   4435 		  && (streq (o->name, _SBSS)
   4436 		      || streq (o->name, _SDATA)
   4437 		      || streq (o->name, _LIT4)
   4438 		      || streq (o->name, _LIT8)
   4439 		      || streq (o->name, _LITA)))
   4440 		lo = o->vma;
   4441 	    }
   4442 	  ecoff_data (abfd)->gp = lo + 0x8000;
   4443 	}
   4444       else
   4445 	{
   4446 	  /* If the relocate_section function needs to do a reloc
   4447 	     involving the GP value, it should make a reloc_dangerous
   4448 	     callback to warn that GP is not defined.  */
   4449 	}
   4450     }
   4451 
   4452   for (o = abfd->sections; o != NULL; o = o->next)
   4453     {
   4454       for (p = o->map_head.link_order;
   4455 	   p != NULL;
   4456 	   p = p->next)
   4457 	{
   4458 	  if (p->type == bfd_indirect_link_order
   4459 	      && (bfd_get_flavour (p->u.indirect.section->owner)
   4460 		  == bfd_target_ecoff_flavour))
   4461 	    {
   4462 	      if (! ecoff_indirect_link_order (abfd, info, o, p))
   4463 		return FALSE;
   4464 	    }
   4465 	  else if (p->type == bfd_section_reloc_link_order
   4466 		   || p->type == bfd_symbol_reloc_link_order)
   4467 	    {
   4468 	      if (! ecoff_reloc_link_order (abfd, info, o, p))
   4469 		return FALSE;
   4470 	    }
   4471 	  else
   4472 	    {
   4473 	      if (! _bfd_default_link_order (abfd, info, o, p))
   4474 		return FALSE;
   4475 	    }
   4476 	}
   4477     }
   4478 
   4479   bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
   4480 
   4481   ecoff_data (abfd)->linker = TRUE;
   4482 
   4483   return TRUE;
   4484 }
   4485