home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / gdb-4.9 / bfd / coff-mips.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-12  |  123.0 KB  |  4,321 lines

  1. /* BFD back-end for MIPS Extended-Coff files.
  2.    Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
  3.    Original version by Per Bothner.
  4.    Full support added by Ian Lance Taylor, ian@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 2 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., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. #include "bfd.h"
  23. #include "sysdep.h"
  24. #include "libbfd.h"
  25. #include "seclet.h"
  26. #include "aout/ar.h"
  27. #include "aout/ranlib.h"
  28. #include "coff/mips.h"
  29. #include "coff/internal.h"
  30. #include "coff/sym.h"
  31. #include "coff/symconst.h"
  32. #include "coff/ecoff-ext.h"
  33. #include "libcoff.h"
  34. #include "libecoff.h"
  35.  
  36. /* Each canonical asymbol really looks like this.  */
  37.  
  38. typedef struct ecoff_symbol_struct
  39. {
  40.   /* The actual symbol which the rest of BFD works with */
  41.   asymbol symbol;
  42.  
  43.   /* The fdr for this symbol.  */
  44.   FDR *fdr;
  45.  
  46.   /* true if this is a local symbol rather than an external one.  */
  47.   boolean local;
  48.  
  49.   /* A pointer to the unswapped hidden information for this symbol */
  50.   union
  51.     {
  52.       struct sym_ext *lnative;
  53.       struct ext_ext *enative;
  54.     }
  55.   native;
  56. } ecoff_symbol_type;
  57.  
  58. /* We take the address of the first element of a asymbol to ensure that the
  59.    macro is only ever applied to an asymbol.  */
  60. #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
  61.  
  62. /* The page boundary used to align sections in the executable file.  */
  63. #define ROUND_SIZE 0x1000
  64.  
  65. /* The linker needs a section to hold small common variables while
  66.    linking.  There is no convenient way to create it when the linker
  67.    needs it, so we always create one for each BFD.  We then avoid
  68.    writing it out.  */
  69. #define SCOMMON ".scommon"
  70.  
  71. /* MIPS ECOFF has COFF sections, but the debugging information is
  72.    stored in a completely different format.  This files uses the some
  73.    of the swapping routines from coffswap.h, and some of the generic
  74.    COFF routines in coffgen.c, but, unlike the real COFF targets, does
  75.    not use coffcode.h itself.  */
  76.  
  77. /* Prototypes for static functions.  */
  78.  
  79. static boolean ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
  80. static asection *ecoff_make_section_hook PARAMS ((bfd *abfd, char *name));
  81. static boolean ecoff_new_section_hook PARAMS ((bfd *abfd, asection *section));
  82. static boolean ecoff_mkobject PARAMS ((bfd *abfd));
  83. static PTR ecoff_mkobject_hook PARAMS ((bfd *abfd, PTR filehdr, PTR aouthdr));
  84. static boolean ecoff_set_arch_mach_hook PARAMS ((bfd *abfd, PTR filehdr));
  85. static long ecoff_sec_to_styp_flags PARAMS ((CONST char *name,
  86.                          flagword flags));
  87. static flagword ecoff_styp_to_sec_flags PARAMS ((bfd *abfd, PTR hdr));
  88. static asymbol *ecoff_make_empty_symbol PARAMS ((bfd *abfd));
  89. static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
  90.                        asymbol *asym, int ext));
  91. static boolean ecoff_slurp_symbol_table PARAMS ((bfd *abfd));
  92. static unsigned int ecoff_get_symtab_upper_bound PARAMS ((bfd *abfd));
  93. static unsigned int ecoff_get_symtab PARAMS ((bfd *abfd,
  94.                           asymbol **alocation));
  95. static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
  96.                       RNDXR *rndx, long isym,
  97.                       CONST char *which));
  98. static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
  99.                        int indx, int bigendian));
  100. static void ecoff_print_symbol PARAMS ((bfd *abfd, PTR filep,
  101.                     asymbol *symbol,
  102.                     bfd_print_symbol_type how));
  103. static void ecoff_swap_reloc_in PARAMS ((bfd *abfd, RELOC *ext,
  104.                      struct internal_reloc *intern));
  105. static unsigned int ecoff_swap_reloc_out PARAMS ((bfd *abfd, PTR src,
  106.                           PTR dst));
  107. static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
  108.                               arelent *reloc,
  109.                               asymbol *symbol,
  110.                               PTR data,
  111.                               asection *section,
  112.                               bfd *output_bfd));
  113. static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
  114.                             arelent *reloc,
  115.                             asymbol *symbol,
  116.                             PTR data,
  117.                             asection *section,
  118.                             bfd *output_bfd));
  119. static bfd_reloc_status_type ecoff_reflo_reloc PARAMS ((bfd *abfd,
  120.                             arelent *reloc,
  121.                             asymbol *symbol,
  122.                             PTR data,
  123.                             asection *section,
  124.                             bfd *output_bfd));
  125. static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
  126.                             arelent *reloc,
  127.                             asymbol *symbol,
  128.                             PTR data,
  129.                             asection *section,
  130.                             bfd *output_bfd));
  131. static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
  132.                         asymbol **symbols));
  133. static unsigned int ecoff_canonicalize_reloc PARAMS ((bfd *abfd,
  134.                               asection *section,
  135.                               arelent **relptr,
  136.                               asymbol **symbols));
  137. static CONST struct reloc_howto_struct *ecoff_bfd_reloc_type_lookup
  138.   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
  139. static boolean ecoff_find_nearest_line PARAMS ((bfd *abfd,
  140.                         asection *section,
  141.                         asymbol **symbols,
  142.                         bfd_vma offset,
  143.                         CONST char **filename_ptr,
  144.                         CONST char **fnname_ptr,
  145.                         unsigned int *retline_ptr));
  146. static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
  147. static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
  148.                   asection *output_section, PTR data,
  149.                   boolean relocateable));
  150. static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
  151.                       asection *section, PTR data,
  152.                       boolean relocateable));
  153. static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
  154.                       CONST char *string, boolean external));
  155. static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
  156.                     bfd_seclet_type *seclet,
  157.                     asection *section,
  158.                     boolean relocateable));
  159. static boolean ecoff_bfd_seclet_link PARAMS ((bfd *abfd, PTR data,
  160.                           boolean relocateable));
  161. static boolean ecoff_set_arch_mach PARAMS ((bfd *abfd,
  162.                         enum bfd_architecture arch,
  163.                         unsigned long machine));
  164. static int ecoff_sizeof_headers PARAMS ((bfd *abfd, boolean reloc));
  165. static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
  166. static boolean ecoff_set_section_contents PARAMS ((bfd *abfd,
  167.                            asection *section,
  168.                            PTR location,
  169.                            file_ptr offset,
  170.                            bfd_size_type count));
  171. static boolean ecoff_write_object_contents PARAMS ((bfd *abfd));
  172. static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
  173.                           unsigned int *rehash,
  174.                           unsigned int size,
  175.                           unsigned int hlog));
  176. static boolean ecoff_slurp_armap PARAMS ((bfd *abfd));
  177. static boolean ecoff_write_armap PARAMS ((bfd *abfd, unsigned int elength,
  178.                       struct orl *map,
  179.                       unsigned int orl_count,
  180.                       int stridx));
  181. static bfd_target *ecoff_archive_p PARAMS ((bfd *abfd));
  182.  
  183. /* Get the generic COFF swapping routines, except for the reloc,
  184.    symbol, and lineno ones.  Give them ecoff names.  */
  185. #define MIPSECOFF
  186. #define NO_COFF_RELOCS
  187. #define NO_COFF_SYMBOLS
  188. #define NO_COFF_LINENOS
  189. #define coff_swap_filehdr_in ecoff_swap_filehdr_in
  190. #define coff_swap_filehdr_out ecoff_swap_filehdr_out
  191. #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
  192. #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
  193. #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
  194. #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
  195. #include "coffswap.h"
  196.  
  197. /* This stuff is somewhat copied from coffcode.h.  */
  198.  
  199. static asection bfd_debug_section = { "*DEBUG*" };
  200.  
  201. /* See whether the magic number matches.  */
  202.  
  203. static boolean
  204. ecoff_bad_format_hook (abfd, filehdr)
  205.      bfd *abfd;
  206.      PTR filehdr;
  207. {
  208.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  209.  
  210.   if (ECOFFBADMAG (*internal_f))
  211.     return false;
  212.  
  213.   return true;
  214. }
  215.  
  216. /* This is a hook needed by SCO COFF, but we have nothing to do.  */
  217.  
  218. static asection *
  219. ecoff_make_section_hook (abfd, name)
  220.      bfd *abfd;
  221.      char *name;
  222. {
  223.   return (asection *) NULL;
  224. }
  225.  
  226. /* Initialize a new section.  */
  227.  
  228. static boolean
  229. ecoff_new_section_hook (abfd, section)
  230.      bfd *abfd;
  231.      asection *section;
  232. {
  233.   section->alignment_power = abfd->xvec->align_power_min;
  234.  
  235.   if (strcmp (section->name, _TEXT) == 0)
  236.     section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  237.   else if (strcmp (section->name, _DATA) == 0
  238.        || strcmp (section->name, _SDATA) == 0)
  239.     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  240.   else if (strcmp (section->name, _RDATA) == 0
  241.        || strcmp (section->name, _LIT8) == 0
  242.        || strcmp (section->name, _LIT4) == 0)
  243.     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  244.   else if (strcmp (section->name, _BSS) == 0
  245.        || strcmp (section->name, _SBSS) == 0)
  246.     section->flags |= SEC_ALLOC;
  247.  
  248.   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
  249.      uncertain about .init on some systems and I don't know how shared
  250.      libraries work.  */
  251.  
  252.   return true;
  253. }
  254.  
  255. /* Set the alignment of a section; we have nothing to do.  */
  256.  
  257. #define ecoff_set_alignment_hook \
  258.   ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
  259.  
  260. /* Create an ECOFF object.  */
  261.  
  262. static boolean
  263. ecoff_mkobject (abfd)
  264.      bfd *abfd;
  265. {
  266.   abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
  267.                 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
  268.   if (abfd->tdata.ecoff_obj_data == NULL)
  269.     {
  270.       bfd_error = no_memory;
  271.       return false;
  272.     }
  273.  
  274.   /* Always create a .scommon section for every BFD.  This is a hack so
  275.      that the linker has something to attach scSCommon symbols to.  */
  276.   bfd_make_section (abfd, SCOMMON);
  277.  
  278.   return true;
  279. }
  280.  
  281. /* Create the ECOFF backend specific information.  */
  282.  
  283. static PTR
  284. ecoff_mkobject_hook (abfd, filehdr, aouthdr)
  285.      bfd *abfd;
  286.      PTR filehdr;
  287.      PTR aouthdr;
  288. {
  289.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  290.   struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
  291.   ecoff_data_type *ecoff;
  292.  
  293.   if (ecoff_mkobject (abfd) == false)
  294.     return NULL;
  295.  
  296.   ecoff = ecoff_data (abfd);
  297.   ecoff->gp_size = 8;
  298.   ecoff->sym_filepos = internal_f->f_symptr;
  299.  
  300.   if (internal_a != (struct internal_aouthdr *) NULL)
  301.     {
  302.       int i;
  303.  
  304.       ecoff->text_start = internal_a->text_start;
  305.       ecoff->text_end = internal_a->text_start + internal_a->tsize;
  306.       ecoff->gp = internal_a->gp_value;
  307.       ecoff->gprmask = internal_a->gprmask;
  308.       for (i = 0; i < 4; i++)
  309.     ecoff->cprmask[i] = internal_a->cprmask[i];
  310.     }
  311.  
  312.   return (PTR) ecoff;
  313. }
  314.  
  315. /* Determine the machine architecture and type.  */
  316.  
  317. static boolean
  318. ecoff_set_arch_mach_hook (abfd, filehdr)
  319.      bfd *abfd;
  320.      PTR filehdr;
  321. {
  322.   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
  323.   enum bfd_architecture arch;
  324.  
  325.   switch (internal_f->f_magic)
  326.     {
  327.     case MIPS_MAGIC_1:
  328.     case MIPS_MAGIC_LITTLE:
  329.     case MIPS_MAGIC_BIG:
  330.       arch = bfd_arch_mips;
  331.       break;
  332.  
  333.     default:
  334.       arch = bfd_arch_obscure;
  335.       break;
  336.     }
  337.  
  338.   bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
  339.  
  340.   return true;
  341. }
  342.  
  343. /* Get the section s_flags to use for a section.  */
  344.  
  345. static long
  346. ecoff_sec_to_styp_flags (name, flags)
  347.      CONST char *name;
  348.      flagword flags;
  349. {
  350.   long styp;
  351.  
  352.   styp = 0;
  353.  
  354.   if (strcmp (name, _TEXT) == 0)
  355.     styp = STYP_TEXT;
  356.   else if (strcmp (name, _DATA) == 0)
  357.     styp = STYP_DATA;
  358.   else if (strcmp (name, _SDATA) == 0)
  359.     styp = STYP_SDATA;
  360.   else if (strcmp (name, _RDATA) == 0)
  361.     styp = STYP_RDATA;
  362.   else if (strcmp (name, _LIT8) == 0)
  363.     styp = STYP_LIT8;
  364.   else if (strcmp (name, _LIT4) == 0)
  365.     styp = STYP_LIT4;
  366.   else if (strcmp (name, _BSS) == 0)
  367.     styp = STYP_BSS;
  368.   else if (strcmp (name, _SBSS) == 0)
  369.     styp = STYP_SBSS;
  370.   else if (flags & SEC_CODE) 
  371.     styp = STYP_TEXT;
  372.   else if (flags & SEC_DATA) 
  373.     styp = STYP_DATA;
  374.   else if (flags & SEC_READONLY)
  375.     styp = STYP_RDATA;
  376.   else if (flags & SEC_LOAD)
  377.     styp = STYP_TEXT;
  378.   else
  379.     styp = STYP_BSS;
  380.  
  381.   if (flags & SEC_NEVER_LOAD)
  382.     styp |= STYP_NOLOAD;
  383.  
  384.   return styp;
  385. }
  386.  
  387. /* Get the BFD flags to use for a section.  */
  388.  
  389. static flagword
  390. ecoff_styp_to_sec_flags (abfd, hdr)
  391.      bfd *abfd;
  392.      PTR hdr;
  393. {
  394.   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
  395.   long styp_flags = internal_s->s_flags;
  396.   flagword sec_flags=0;
  397.  
  398.   if (styp_flags & STYP_NOLOAD)
  399.     sec_flags |= SEC_NEVER_LOAD;
  400.  
  401.   /* For 386 COFF, at least, an unloadable text or data section is
  402.      actually a shared library section.  */
  403.   if (styp_flags & STYP_TEXT)
  404.     {
  405.       if (sec_flags & SEC_NEVER_LOAD)
  406.     sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
  407.       else
  408.     sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
  409.     }
  410.   else if ((styp_flags & STYP_DATA)
  411.        || (styp_flags & STYP_RDATA)
  412.        || (styp_flags & STYP_SDATA))
  413.     {
  414.       if (sec_flags & SEC_NEVER_LOAD)
  415.     sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
  416.       else
  417.     sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
  418.       if (styp_flags & STYP_RDATA)
  419.     sec_flags |= SEC_READONLY;
  420.     }
  421.   else if ((styp_flags & STYP_BSS)
  422.        || (styp_flags & STYP_SBSS))
  423.     {
  424.       sec_flags |= SEC_ALLOC;
  425.     }
  426.   else if (styp_flags & STYP_INFO) 
  427.     {
  428.       sec_flags |= SEC_NEVER_LOAD;
  429.     }
  430.   else if ((styp_flags & STYP_LIT8)
  431.        || (styp_flags & STYP_LIT4))
  432.     {
  433.       sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
  434.     }
  435.   else
  436.     {
  437.       sec_flags |= SEC_ALLOC | SEC_LOAD;
  438.     }
  439.  
  440.   return sec_flags;
  441. }
  442.  
  443. /* Read in and swap the important symbolic information for an ECOFF
  444.    object file.  FIXME: This is called by gdb.  If there is ever
  445.    another ECOFF target, it should be moved into some sort of target
  446.    specific structure.  */
  447.  
  448. boolean
  449. ecoff_slurp_symbolic_info (abfd)
  450.      bfd *abfd;
  451. {
  452.   struct hdr_ext external_symhdr;
  453.   HDRR *internal_symhdr;
  454.   bfd_size_type raw_base;
  455.   bfd_size_type raw_size;
  456.   PTR raw;
  457.   struct fdr_ext *fraw_src;
  458.   struct fdr_ext *fraw_end;
  459.   struct fdr *fdr_ptr;
  460.  
  461.   /* Check whether we've already gotten it, and whether there's any to
  462.      get.  */
  463.   if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
  464.     return true;
  465.   if (ecoff_data (abfd)->sym_filepos == 0)
  466.     {
  467.       bfd_get_symcount (abfd) = 0;
  468.       return true;
  469.     }
  470.  
  471.   /* At this point bfd_get_symcount (abfd) holds the number of symbols
  472.      as read from the file header, but on ECOFF this is always the
  473.      size of the symbolic information header.  It would be cleaner to
  474.      handle this when we first read the file in coffgen.c.  */
  475.   if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
  476.     {
  477.       bfd_error = bad_value;
  478.       return false;
  479.     }
  480.  
  481.   /* Read the symbolic information header.  */
  482.   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
  483.       || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
  484.       != sizeof (external_symhdr)))
  485.     {
  486.       bfd_error = system_call_error;
  487.       return false;
  488.     }
  489.   internal_symhdr = &ecoff_data (abfd)->symbolic_header;
  490.   ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
  491.  
  492.   if (internal_symhdr->magic != magicSym)
  493.     {
  494.       bfd_error = bad_value;
  495.       return false;
  496.     }
  497.  
  498.   /* Now we can get the correct number of symbols.  */
  499.   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
  500.                  + internal_symhdr->iextMax);
  501.  
  502.   /* Read all the symbolic information at once.  */
  503.   raw_base = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
  504.  
  505.   if (internal_symhdr->cbExtOffset != 0)
  506.     raw_size = (internal_symhdr->cbExtOffset
  507.         - raw_base
  508.         + internal_symhdr->iextMax * sizeof (struct ext_ext));
  509.   else
  510.     {
  511.       long cbline, issmax, issextmax;
  512.  
  513.       cbline = (internal_symhdr->cbLine + 3) &~ 3;
  514.       issmax = (internal_symhdr->issMax + 3) &~ 3;
  515.       issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
  516.       raw_size = (cbline * sizeof (unsigned char)
  517.           + internal_symhdr->idnMax * sizeof (struct dnr_ext)
  518.           + internal_symhdr->ipdMax * sizeof (struct pdr_ext)
  519.           + internal_symhdr->isymMax * sizeof (struct sym_ext)
  520.           + internal_symhdr->ioptMax * sizeof (struct opt_ext)
  521.           + internal_symhdr->iauxMax * sizeof (union aux_ext)
  522.           + issmax * sizeof (char)
  523.           + issextmax * sizeof (char)
  524.           + internal_symhdr->ifdMax * sizeof (struct fdr_ext)
  525.           + internal_symhdr->crfd * sizeof (struct rfd_ext)
  526.           + internal_symhdr->iextMax * sizeof (struct ext_ext));
  527.     }
  528.  
  529.   if (raw_size == 0)
  530.     {
  531.       ecoff_data (abfd)->sym_filepos = 0;
  532.       return true;
  533.     }
  534.   raw = (PTR) bfd_alloc (abfd, raw_size);
  535.   if (raw == NULL)
  536.     {
  537.       bfd_error = no_memory;
  538.       return false;
  539.     }
  540.   if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
  541.     {
  542.       bfd_error = system_call_error;
  543.       bfd_release (abfd, raw);
  544.       return false;
  545.     }
  546.  
  547.   ecoff_data (abfd)->raw_size = raw_size;
  548.   ecoff_data (abfd)->raw_syments = raw;
  549.  
  550.   /* Get pointers for the numeric offsets in the HDRR structure.  */
  551. #define FIX(off1, off2, type) \
  552.   if (internal_symhdr->off1 == 0) \
  553.     ecoff_data (abfd)->off2 = (type *) NULL; \
  554.   else \
  555.     ecoff_data (abfd)->off2 = (type *) ((char *) raw \
  556.                     + internal_symhdr->off1 \
  557.                     - raw_base)
  558.   FIX (cbLineOffset, line, unsigned char);
  559.   FIX (cbDnOffset, external_dnr, struct dnr_ext);
  560.   FIX (cbPdOffset, external_pdr, struct pdr_ext);
  561.   FIX (cbSymOffset, external_sym, struct sym_ext);
  562.   FIX (cbOptOffset, external_opt, struct opt_ext);
  563.   FIX (cbAuxOffset, external_aux, union aux_ext);
  564.   FIX (cbSsOffset, ss, char);
  565.   FIX (cbSsExtOffset, ssext, char);
  566.   FIX (cbFdOffset, external_fdr, struct fdr_ext);
  567.   FIX (cbRfdOffset, external_rfd, struct rfd_ext);
  568.   FIX (cbExtOffset, external_ext, struct ext_ext);
  569. #undef FIX
  570.  
  571.   /* I don't want to always swap all the data, because it will just
  572.      waste time and most programs will never look at it.  The only
  573.      time the linker needs most of the debugging information swapped
  574.      is when linking big-endian and little-endian MIPS object files
  575.      together, which is not a common occurrence.
  576.  
  577.      We need to look at the fdr to deal with a lot of information in
  578.      the symbols, so we swap them here.  */
  579.   ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
  580.                              (internal_symhdr->ifdMax *
  581.                               sizeof (struct fdr)));
  582.   if (ecoff_data (abfd)->fdr == NULL)
  583.     {
  584.       bfd_error = no_memory;
  585.       return false;
  586.     }
  587.   fdr_ptr = ecoff_data (abfd)->fdr;
  588.   fraw_src = ecoff_data (abfd)->external_fdr;
  589.   fraw_end = fraw_src + internal_symhdr->ifdMax;
  590.   for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
  591.     ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
  592.  
  593.   return true;
  594. }
  595.  
  596. /* ECOFF symbol table routines.  The ECOFF symbol table is described
  597.    in gcc/mips-tfile.c.  */
  598.  
  599. /* ECOFF uses two common sections.  One is the usual one, and the
  600.    other is for small objects.  All the small objects are kept
  601.    together, and then referenced via the gp pointer, which yields
  602.    faster assembler code.  This is what we use for the small common
  603.    section.  */
  604. static asection ecoff_scom_section;
  605. static asymbol ecoff_scom_symbol;
  606. static asymbol *ecoff_scom_symbol_ptr;
  607.  
  608. /* Create an empty symbol.  */
  609.  
  610. static asymbol *
  611. ecoff_make_empty_symbol (abfd)
  612.      bfd *abfd;
  613. {
  614.   ecoff_symbol_type *new;
  615.  
  616.   new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
  617.   if (new == (ecoff_symbol_type *) NULL)
  618.     {
  619.       bfd_error = no_memory;
  620.       return (asymbol *) NULL;
  621.     }
  622.   memset (new, 0, sizeof *new);
  623.   new->symbol.section = (asection *) NULL;
  624.   new->fdr = (FDR *) NULL;
  625.   new->local = false;
  626.   new->native.lnative = (struct sym_ext *) NULL;
  627.   new->symbol.the_bfd = abfd;
  628.   return &new->symbol;
  629. }
  630.  
  631. /* Set the BFD flags and section for an ECOFF symbol.  */
  632.  
  633. static void
  634. ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext)
  635.      bfd *abfd;
  636.      SYMR *ecoff_sym;
  637.      asymbol *asym;
  638.      int ext;
  639. {
  640.   asym->the_bfd = abfd;
  641.   asym->value = ecoff_sym->value;
  642.   asym->section = &bfd_debug_section;
  643.   asym->udata = NULL;
  644.  
  645.   /* Most symbol types are just for debugging.  */
  646.   switch (ecoff_sym->st)
  647.     {
  648.     case stGlobal:
  649.     case stStatic:
  650.     case stLabel:
  651.     case stProc:
  652.     case stStaticProc:
  653.       break;
  654.     case stNil:
  655.       if (MIPS_IS_STAB (ecoff_sym))
  656.     {
  657.       asym->flags = BSF_DEBUGGING;
  658.       return;
  659.     }
  660.       break;
  661.     default:
  662.       asym->flags = BSF_DEBUGGING;
  663.       return;
  664.     }
  665.  
  666.   if (ext)
  667.     asym->flags = BSF_EXPORT | BSF_GLOBAL;
  668.   else
  669.     asym->flags = BSF_LOCAL;
  670.   switch (ecoff_sym->sc)
  671.     {
  672.     case scNil:
  673.       /* Used for compiler generated labels.  Leave them in the
  674.      debugging section, and mark them as local.  If BSF_DEBUGGING
  675.      is set, then nm does not display them for some reason.  If no
  676.      flags are set then the linker whines about them.  */
  677.       asym->flags = BSF_LOCAL;
  678.       break;
  679.     case scText:
  680.       asym->section = bfd_make_section_old_way (abfd, ".text");
  681.       asym->value -= asym->section->vma;
  682.       break;
  683.     case scData:
  684.       asym->section = bfd_make_section_old_way (abfd, ".data");
  685.       asym->value -= asym->section->vma;
  686.       break;
  687.     case scBss:
  688.       if (ext)
  689.     {
  690.       asym->section = &bfd_com_section;
  691.       asym->flags = 0;
  692.     }
  693.       else
  694.     {
  695.       asym->section = bfd_make_section_old_way (abfd, ".bss");
  696.       asym->value -= asym->section->vma;
  697.     }
  698.       break;
  699.     case scRegister:
  700.       asym->flags = BSF_DEBUGGING;
  701.       break;
  702.     case scAbs:
  703.       asym->section = &bfd_abs_section;
  704.       break;
  705.     case scUndefined:
  706.       asym->section = &bfd_und_section;
  707.       asym->flags = 0;
  708.       asym->value = 0;
  709.       break;
  710.     case scCdbLocal:
  711.     case scBits:
  712.     case scCdbSystem:
  713.     case scRegImage:
  714.     case scInfo:
  715.     case scUserStruct:
  716.       asym->flags = BSF_DEBUGGING;
  717.       break;
  718.     case scSData:
  719.       asym->section = bfd_make_section_old_way (abfd, ".sdata");
  720.       asym->value -= asym->section->vma;
  721.       break;
  722.     case scSBss:
  723.       asym->section = bfd_make_section_old_way (abfd, ".sbss");
  724.       if (! ext)
  725.     asym->value -= asym->section->vma;
  726.       break;
  727.     case scRData:
  728.       asym->section = bfd_make_section_old_way (abfd, ".rdata");
  729.       asym->value -= asym->section->vma;
  730.       break;
  731.     case scVar:
  732.       asym->flags = BSF_DEBUGGING;
  733.       break;
  734.     case scCommon:
  735.       if (asym->value > ecoff_data (abfd)->gp_size)
  736.     {
  737.       asym->section = &bfd_com_section;
  738.       asym->flags = 0;
  739.       break;
  740.     }
  741.       /* Fall through.  */
  742.     case scSCommon:
  743.       if (ecoff_scom_section.name == NULL)
  744.     {
  745.       /* Initialize the small common section.  */
  746.       ecoff_scom_section.name = SCOMMON;
  747.       ecoff_scom_section.flags = SEC_IS_COMMON;
  748.       ecoff_scom_section.output_section = &ecoff_scom_section;
  749.       ecoff_scom_section.symbol = &ecoff_scom_symbol;
  750.       ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
  751.       ecoff_scom_symbol.name = SCOMMON;
  752.       ecoff_scom_symbol.flags = BSF_SECTION_SYM;
  753.       ecoff_scom_symbol.section = &ecoff_scom_section;
  754.       ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
  755.     }
  756.       asym->section = &ecoff_scom_section;
  757.       asym->flags = 0;
  758.       break;
  759.     case scVarRegister:
  760.     case scVariant:
  761.       asym->flags = BSF_DEBUGGING;
  762.       break;
  763.     case scSUndefined:
  764.       asym->section = &bfd_und_section;
  765.       asym->flags = 0;
  766.       asym->value = 0;
  767.       break;
  768.     case scInit:
  769.       asym->section = bfd_make_section_old_way (abfd, ".init");
  770.       asym->value -= asym->section->vma;
  771.       break;
  772.     case scBasedVar:
  773.     case scXData:
  774.     case scPData:
  775.       asym->flags = BSF_DEBUGGING;
  776.       break;
  777.     case scFini:
  778.       asym->section = bfd_make_section_old_way (abfd, ".fini");
  779.       asym->value -= asym->section->vma;
  780.       break;
  781.     default:
  782.       break;
  783.     }
  784. }
  785.  
  786. /* Read an ECOFF symbol table.  */
  787.  
  788. static boolean
  789. ecoff_slurp_symbol_table (abfd)
  790.      bfd *abfd;
  791. {
  792.   bfd_size_type internal_size;
  793.   ecoff_symbol_type *internal;
  794.   ecoff_symbol_type *internal_ptr;
  795.   struct ext_ext *eraw_src;
  796.   struct ext_ext *eraw_end;
  797.   FDR *fdr_ptr;
  798.   FDR *fdr_end;
  799.  
  800.   /* If we've already read in the symbol table, do nothing.  */
  801.   if (ecoff_data (abfd)->canonical_symbols != NULL)
  802.     return true;
  803.  
  804.   /* Get the symbolic information.  */
  805.   if (ecoff_slurp_symbolic_info (abfd) == false)
  806.     return false;
  807.   if (bfd_get_symcount (abfd) == 0)
  808.     return true;
  809.  
  810.   internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
  811.   internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
  812.   if (internal == NULL)
  813.     {
  814.       bfd_error = no_memory;
  815.       return false;
  816.     }
  817.  
  818.   internal_ptr = internal;
  819.   eraw_src = ecoff_data (abfd)->external_ext;
  820.   eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
  821.   for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
  822.     {
  823.       EXTR internal_esym;
  824.  
  825.       ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
  826.       internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
  827.                    + internal_esym.asym.iss);
  828.       ecoff_set_symbol_info (abfd, &internal_esym.asym,
  829.                  &internal_ptr->symbol, 1);
  830.       internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
  831.       internal_ptr->local = false;
  832.       internal_ptr->native.enative = eraw_src;
  833.     }
  834.  
  835.   /* The local symbols must be accessed via the fdr's, because the
  836.      string and aux indices are relative to the fdr information.  */
  837.   fdr_ptr = ecoff_data (abfd)->fdr;
  838.   fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
  839.   for (; fdr_ptr < fdr_end; fdr_ptr++)
  840.     {
  841.       struct sym_ext *lraw_src;
  842.       struct sym_ext *lraw_end;
  843.  
  844.       lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
  845.       lraw_end = lraw_src + fdr_ptr->csym;
  846.       for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
  847.     {
  848.       SYMR internal_sym;
  849.  
  850.       ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
  851.       internal_ptr->symbol.name = (ecoff_data (abfd)->ss
  852.                        + fdr_ptr->issBase
  853.                        + internal_sym.iss);
  854.       ecoff_set_symbol_info (abfd, &internal_sym,
  855.                  &internal_ptr->symbol, 0);
  856.       internal_ptr->fdr = fdr_ptr;
  857.       internal_ptr->local = true;
  858.       internal_ptr->native.lnative = lraw_src;
  859.     }
  860.     }
  861.  
  862.   ecoff_data (abfd)->canonical_symbols = internal;
  863.  
  864.   return true;
  865. }
  866.  
  867. static unsigned int
  868. ecoff_get_symtab_upper_bound (abfd)
  869.      bfd *abfd;
  870. {
  871.   if (ecoff_slurp_symbolic_info (abfd) == false
  872.       || bfd_get_symcount (abfd) == 0)
  873.     return 0;
  874.  
  875.   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
  876. }
  877.  
  878. static unsigned int
  879. ecoff_get_symtab (abfd, alocation)
  880.      bfd *abfd;
  881.      asymbol **alocation;
  882. {
  883.   unsigned int counter = 0;
  884.   ecoff_symbol_type *symbase;
  885.   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
  886.  
  887.   if (ecoff_slurp_symbol_table (abfd) == false
  888.       || bfd_get_symcount (abfd) == 0)
  889.     return 0;
  890.  
  891.   symbase = ecoff_data (abfd)->canonical_symbols;
  892.   while (counter < bfd_get_symcount (abfd))
  893.     {
  894.       *(location++) = symbase++;
  895.       counter++;
  896.     }
  897.   *location++ = (ecoff_symbol_type *) NULL;
  898.   return bfd_get_symcount (abfd);
  899. }
  900.  
  901. /* Turn ECOFF type information into a printable string.
  902.    ecoff_emit_aggregate and ecoff_type_to_string are from
  903.    gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
  904.  
  905. /* Write aggregate information to a string.  */
  906.  
  907. static void
  908. ecoff_emit_aggregate (abfd, string, rndx, isym, which)
  909.      bfd *abfd;
  910.      char *string;
  911.      RNDXR *rndx;
  912.      long isym;
  913.      CONST char *which;
  914. {
  915.   int ifd = rndx->rfd;
  916.   int indx = rndx->index;
  917.   int sym_base, ss_base;
  918.   CONST char *name;
  919.   
  920.   if (ifd == 0xfff)
  921.     ifd = isym;
  922.  
  923.   sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
  924.   ss_base  = ecoff_data (abfd)->fdr[ifd].issBase;
  925.   
  926.   if (indx == indexNil)
  927.     name = "/* no name */";
  928.   else
  929.     {
  930.       SYMR sym;
  931.  
  932.       indx += sym_base;
  933.       ecoff_swap_sym_in (abfd,
  934.              ecoff_data (abfd)->external_sym + indx,
  935.              &sym);
  936.       name = ecoff_data (abfd)->ss + ss_base + sym.iss;
  937.     }
  938.  
  939.   sprintf (string,
  940.        "%s %s { ifd = %d, index = %d }",
  941.        which, name, ifd,
  942.        indx + ecoff_data (abfd)->symbolic_header.iextMax);
  943. }
  944.  
  945. /* Convert the type information to string format.  */
  946.  
  947. static char *
  948. ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
  949.      bfd *abfd;
  950.      union aux_ext *aux_ptr;
  951.      int indx;
  952.      int bigendian;
  953. {
  954.   AUXU u;
  955.   struct qual {
  956.     unsigned int  type;
  957.     int  low_bound;
  958.     int  high_bound;
  959.     int  stride;
  960.   } qualifiers[7];
  961.  
  962.   unsigned int basic_type;
  963.   int i;
  964.   static char buffer1[1024];
  965.   static char buffer2[1024];
  966.   char *p1 = buffer1;
  967.   char *p2 = buffer2;
  968.   RNDXR rndx;
  969.  
  970.   for (i = 0; i < 7; i++)
  971.     {
  972.       qualifiers[i].low_bound = 0;
  973.       qualifiers[i].high_bound = 0;
  974.       qualifiers[i].stride = 0;
  975.     }
  976.  
  977.   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
  978.     return "-1 (no type)";
  979.   ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
  980.  
  981.   basic_type = u.ti.bt;
  982.   qualifiers[0].type = u.ti.tq0;
  983.   qualifiers[1].type = u.ti.tq1;
  984.   qualifiers[2].type = u.ti.tq2;
  985.   qualifiers[3].type = u.ti.tq3;
  986.   qualifiers[4].type = u.ti.tq4;
  987.   qualifiers[5].type = u.ti.tq5;
  988.   qualifiers[6].type = tqNil;
  989.  
  990.   /*
  991.    * Go get the basic type.
  992.    */
  993.   switch (basic_type)
  994.     {
  995.     case btNil:            /* undefined */
  996.       strcpy (p1, "nil");
  997.       break;
  998.  
  999.     case btAdr:            /* address - integer same size as pointer */
  1000.       strcpy (p1, "address");
  1001.       break;
  1002.  
  1003.     case btChar:        /* character */
  1004.       strcpy (p1, "char");
  1005.       break;
  1006.  
  1007.     case btUChar:        /* unsigned character */
  1008.       strcpy (p1, "unsigned char");
  1009.       break;
  1010.  
  1011.     case btShort:        /* short */
  1012.       strcpy (p1, "short");
  1013.       break;
  1014.  
  1015.     case btUShort:        /* unsigned short */
  1016.       strcpy (p1, "unsigned short");
  1017.       break;
  1018.  
  1019.     case btInt:            /* int */
  1020.       strcpy (p1, "int");
  1021.       break;
  1022.  
  1023.     case btUInt:        /* unsigned int */
  1024.       strcpy (p1, "unsigned int");
  1025.       break;
  1026.  
  1027.     case btLong:        /* long */
  1028.       strcpy (p1, "long");
  1029.       break;
  1030.  
  1031.     case btULong:        /* unsigned long */
  1032.       strcpy (p1, "unsigned long");
  1033.       break;
  1034.  
  1035.     case btFloat:        /* float (real) */
  1036.       strcpy (p1, "float");
  1037.       break;
  1038.  
  1039.     case btDouble:        /* Double (real) */
  1040.       strcpy (p1, "double");
  1041.       break;
  1042.  
  1043.       /* Structures add 1-2 aux words:
  1044.      1st word is [ST_RFDESCAPE, offset] pointer to struct def;
  1045.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1046.  
  1047.     case btStruct:        /* Structure (Record) */
  1048.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1049.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1050.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1051.                 "struct");
  1052.       indx++;            /* skip aux words */
  1053.       break;
  1054.  
  1055.       /* Unions add 1-2 aux words:
  1056.      1st word is [ST_RFDESCAPE, offset] pointer to union def;
  1057.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1058.  
  1059.     case btUnion:        /* Union */
  1060.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1061.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1062.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1063.                 "union");
  1064.       indx++;            /* skip aux words */
  1065.       break;
  1066.  
  1067.       /* Enumerations add 1-2 aux words:
  1068.      1st word is [ST_RFDESCAPE, offset] pointer to enum def;
  1069.      2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
  1070.  
  1071.     case btEnum:        /* Enumeration */
  1072.       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
  1073.       ecoff_emit_aggregate (abfd, p1, &rndx,
  1074.                 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
  1075.                 "enum");
  1076.       indx++;            /* skip aux words */
  1077.       break;
  1078.  
  1079.     case btTypedef:        /* defined via a typedef, isymRef points */
  1080.       strcpy (p1, "typedef");
  1081.       break;
  1082.  
  1083.     case btRange:        /* subrange of int */
  1084.       strcpy (p1, "subrange");
  1085.       break;
  1086.  
  1087.     case btSet:            /* pascal sets */
  1088.       strcpy (p1, "set");
  1089.       break;
  1090.  
  1091.     case btComplex:        /* fortran complex */
  1092.       strcpy (p1, "complex");
  1093.       break;
  1094.  
  1095.     case btDComplex:        /* fortran double complex */
  1096.       strcpy (p1, "double complex");
  1097.       break;
  1098.  
  1099.     case btIndirect:        /* forward or unnamed typedef */
  1100.       strcpy (p1, "forward/unamed typedef");
  1101.       break;
  1102.  
  1103.     case btFixedDec:        /* Fixed Decimal */
  1104.       strcpy (p1, "fixed decimal");
  1105.       break;
  1106.  
  1107.     case btFloatDec:        /* Float Decimal */
  1108.       strcpy (p1, "float decimal");
  1109.       break;
  1110.  
  1111.     case btString:        /* Varying Length Character String */
  1112.       strcpy (p1, "string");
  1113.       break;
  1114.  
  1115.     case btBit:            /* Aligned Bit String */
  1116.       strcpy (p1, "bit");
  1117.       break;
  1118.  
  1119.     case btPicture:        /* Picture */
  1120.       strcpy (p1, "picture");
  1121.       break;
  1122.  
  1123.     case btVoid:        /* Void */
  1124.       strcpy (p1, "void");
  1125.       break;
  1126.  
  1127.     default:
  1128.       sprintf (p1, "Unknown basic type %d", (int) basic_type);
  1129.       break;
  1130.     }
  1131.  
  1132.   p1 += strlen (buffer1);
  1133.  
  1134.   /*
  1135.    * If this is a bitfield, get the bitsize.
  1136.    */
  1137.   if (u.ti.fBitfield)
  1138.     {
  1139.       int bitsize;
  1140.  
  1141.       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
  1142.       sprintf (p1, " : %d", bitsize);
  1143.       p1 += strlen (buffer1);
  1144.     }
  1145.  
  1146.  
  1147.   /*
  1148.    * Deal with any qualifiers.
  1149.    */
  1150.   if (qualifiers[0].type != tqNil)
  1151.     {
  1152.       /*
  1153.        * Snarf up any array bounds in the correct order.  Arrays
  1154.        * store 5 successive words in the aux. table:
  1155.        *    word 0    RNDXR to type of the bounds (ie, int)
  1156.        *    word 1    Current file descriptor index
  1157.        *    word 2    low bound
  1158.        *    word 3    high bound (or -1 if [])
  1159.        *    word 4    stride size in bits
  1160.        */
  1161.       for (i = 0; i < 7; i++)
  1162.     {
  1163.       if (qualifiers[i].type == tqArray)
  1164.         {
  1165.           qualifiers[i].low_bound =
  1166.         AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
  1167.           qualifiers[i].high_bound =
  1168.         AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
  1169.           qualifiers[i].stride =
  1170.         AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
  1171.           indx += 5;
  1172.         }
  1173.     }
  1174.  
  1175.       /*
  1176.        * Now print out the qualifiers.
  1177.        */
  1178.       for (i = 0; i < 6; i++)
  1179.     {
  1180.       switch (qualifiers[i].type)
  1181.         {
  1182.         case tqNil:
  1183.         case tqMax:
  1184.           break;
  1185.  
  1186.         case tqPtr:
  1187.           strcpy (p2, "ptr to ");
  1188.           p2 += sizeof ("ptr to ")-1;
  1189.           break;
  1190.  
  1191.         case tqVol:
  1192.           strcpy (p2, "volatile ");
  1193.           p2 += sizeof ("volatile ")-1;
  1194.           break;
  1195.  
  1196.         case tqFar:
  1197.           strcpy (p2, "far ");
  1198.           p2 += sizeof ("far ")-1;
  1199.           break;
  1200.  
  1201.         case tqProc:
  1202.           strcpy (p2, "func. ret. ");
  1203.           p2 += sizeof ("func. ret. ");
  1204.           break;
  1205.  
  1206.         case tqArray:
  1207.           {
  1208.         int first_array = i;
  1209.         int j;
  1210.  
  1211.         /* Print array bounds reversed (ie, in the order the C
  1212.            programmer writes them).  C is such a fun language.... */
  1213.  
  1214.         while (i < 5 && qualifiers[i+1].type == tqArray)
  1215.           i++;
  1216.  
  1217.         for (j = i; j >= first_array; j--)
  1218.           {
  1219.             strcpy (p2, "array [");
  1220.             p2 += sizeof ("array [")-1;
  1221.             if (qualifiers[j].low_bound != 0)
  1222.               sprintf (p2,
  1223.                    "%ld:%ld {%ld bits}",
  1224.                    (long) qualifiers[j].low_bound,
  1225.                    (long) qualifiers[j].high_bound,
  1226.                    (long) qualifiers[j].stride);
  1227.  
  1228.             else if (qualifiers[j].high_bound != -1)
  1229.               sprintf (p2,
  1230.                    "%ld {%ld bits}",
  1231.                    (long) (qualifiers[j].high_bound + 1),
  1232.                    (long) (qualifiers[j].stride));
  1233.  
  1234.             else
  1235.               sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
  1236.  
  1237.             p2 += strlen (p2);
  1238.             strcpy (p2, "] of ");
  1239.             p2 += sizeof ("] of ")-1;
  1240.           }
  1241.           }
  1242.           break;
  1243.         }
  1244.     }
  1245.     }
  1246.  
  1247.   strcpy (p2, buffer1);
  1248.   return buffer2;
  1249. }
  1250.  
  1251. /* Print information about an ECOFF symbol.  */
  1252.  
  1253. static void
  1254. ecoff_print_symbol (abfd, filep, symbol, how)
  1255.      bfd *abfd;
  1256.      PTR filep;
  1257.      asymbol *symbol;
  1258.      bfd_print_symbol_type how;
  1259. {
  1260.   FILE *file = (FILE *)filep;
  1261.  
  1262.   switch (how)
  1263.     {
  1264.     case bfd_print_symbol_name:
  1265.       fprintf (file, "%s", symbol->name);
  1266.       break;
  1267.     case bfd_print_symbol_more:
  1268.       if (ecoffsymbol (symbol)->local)
  1269.     {
  1270.       SYMR ecoff_sym;
  1271.     
  1272.       ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
  1273.                  &ecoff_sym);
  1274.       fprintf (file, "ecoff local %lx %x %x",
  1275.            (unsigned long) ecoff_sym.value,
  1276.            (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
  1277.     }
  1278.       else
  1279.     {
  1280.       EXTR ecoff_ext;
  1281.  
  1282.       ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
  1283.                  &ecoff_ext);
  1284.       fprintf (file, "ecoff extern %lx %x %x",
  1285.            (unsigned long) ecoff_ext.asym.value,
  1286.            (unsigned) ecoff_ext.asym.st,
  1287.            (unsigned) ecoff_ext.asym.sc);
  1288.     }
  1289.       break;
  1290.     case bfd_print_symbol_nm:
  1291.       {
  1292.     CONST char *section_name = symbol->section->name;
  1293.  
  1294.     bfd_print_symbol_vandf ((PTR) file, symbol);
  1295.     fprintf (file, " %-5s %s %s",
  1296.          section_name,
  1297.          ecoffsymbol (symbol)->local ? "l" : "e",
  1298.          symbol->name);
  1299.       }
  1300.       break;
  1301.     case bfd_print_symbol_all:
  1302.       /* Print out the symbols in a reasonable way */
  1303.       {
  1304.     char type;
  1305.     int pos;
  1306.     EXTR ecoff_ext;
  1307.     char jmptbl;
  1308.     char cobol_main;
  1309.     char weakext;
  1310.  
  1311.     if (ecoffsymbol (symbol)->local)
  1312.       {
  1313.         ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
  1314.                    &ecoff_ext.asym);
  1315.         type = 'l';
  1316.         pos = (ecoffsymbol (symbol)->native.lnative
  1317.            - ecoff_data (abfd)->external_sym
  1318.            + ecoff_data (abfd)->symbolic_header.iextMax);
  1319.         jmptbl = ' ';
  1320.         cobol_main = ' ';
  1321.         weakext = ' ';
  1322.       }
  1323.     else
  1324.       {
  1325.         ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
  1326.                    &ecoff_ext);
  1327.         type = 'e';
  1328.         pos = (ecoffsymbol (symbol)->native.enative
  1329.            - ecoff_data (abfd)->external_ext);
  1330.         jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
  1331.         cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
  1332.         weakext = ecoff_ext.weakext ? 'w' : ' ';
  1333.       }
  1334.  
  1335.     fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
  1336.          pos, type, (unsigned long) ecoff_ext.asym.value,
  1337.          (unsigned) ecoff_ext.asym.st,
  1338.          (unsigned) ecoff_ext.asym.sc,
  1339.          (unsigned) ecoff_ext.asym.index,
  1340.          jmptbl, cobol_main, weakext,
  1341.          symbol->name);
  1342.  
  1343.     if (ecoffsymbol (symbol)->fdr != NULL
  1344.         && ecoff_ext.asym.index != indexNil)
  1345.       {
  1346.         unsigned indx;
  1347.         int bigendian;
  1348.         long sym_base;
  1349.         union aux_ext *aux_base;
  1350.  
  1351.         indx = ecoff_ext.asym.index;
  1352.  
  1353.         /* sym_base is used to map the fdr relative indices which
  1354.            appear in the file to the position number which we are
  1355.            using.  */
  1356.         sym_base = ecoffsymbol (symbol)->fdr->isymBase;
  1357.         if (ecoffsymbol (symbol)->local)
  1358.           sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
  1359.  
  1360.         /* aux_base is the start of the aux entries for this file;
  1361.            asym.index is an offset from this.  */
  1362.         aux_base = (ecoff_data (abfd)->external_aux
  1363.             + ecoffsymbol (symbol)->fdr->iauxBase);
  1364.  
  1365.         /* The aux entries are stored in host byte order; the
  1366.            order is indicated by a bit in the fdr.  */
  1367.         bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
  1368.  
  1369.         /* This switch is basically from gcc/mips-tdump.c  */
  1370.         switch (ecoff_ext.asym.st)
  1371.           {
  1372.           case stNil:
  1373.           case stLabel:
  1374.         break;
  1375.  
  1376.           case stFile:
  1377.           case stBlock:
  1378.         printf ("\n      End+1 symbol: %ld", indx + sym_base);
  1379.         break;
  1380.  
  1381.           case stEnd:
  1382.         if (ecoff_ext.asym.sc == scText
  1383.             || ecoff_ext.asym.sc == scInfo)
  1384.           printf ("\n      First symbol: %ld", indx + sym_base);
  1385.         else
  1386.           printf ("\n      First symbol: %ld", 
  1387.               (AUX_GET_ISYM (bigendian,
  1388.                      &aux_base[ecoff_ext.asym.index])
  1389.                + sym_base));
  1390.         break;
  1391.  
  1392.           case stProc:
  1393.           case stStaticProc:
  1394.         if (MIPS_IS_STAB (&ecoff_ext.asym))
  1395.           ;
  1396.         else if (ecoffsymbol (symbol)->local)
  1397.           printf ("\n      End+1 symbol: %-7ld   Type:  %s",
  1398.               (AUX_GET_ISYM (bigendian,
  1399.                      &aux_base[ecoff_ext.asym.index])
  1400.                + sym_base),
  1401.               ecoff_type_to_string (abfd, aux_base, indx + 1,
  1402.                         bigendian));
  1403.         else
  1404.           printf ("\n      Local symbol: %d",
  1405.               (indx
  1406.                + sym_base
  1407.                + ecoff_data (abfd)->symbolic_header.iextMax));
  1408.         break;
  1409.  
  1410.           default:
  1411.         if (!MIPS_IS_STAB (&ecoff_ext.asym))
  1412.           printf ("\n      Type: %s",
  1413.               ecoff_type_to_string (abfd, aux_base, indx,
  1414.                         bigendian));
  1415.         break;
  1416.           }
  1417.       }
  1418.       }
  1419.       break;
  1420.     }
  1421. }
  1422.  
  1423. /* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in
  1424.    external form.  They use a bit which indicates whether the symbol
  1425.    is external.  */
  1426.  
  1427. /* Swap a reloc in.  */
  1428.  
  1429. static void
  1430. ecoff_swap_reloc_in (abfd, ext, intern)
  1431.      bfd *abfd;
  1432.      RELOC *ext;
  1433.      struct internal_reloc *intern;
  1434. {
  1435.   intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
  1436.   if (abfd->xvec->header_byteorder_big_p != false)
  1437.     {
  1438.       intern->r_symndx = (((int) ext->r_bits[0]
  1439.                << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
  1440.               | ((int) ext->r_bits[1]
  1441.                  << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
  1442.               | ((int) ext->r_bits[2]
  1443.                  << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
  1444.       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
  1445.             >> RELOC_BITS3_TYPE_SH_BIG);
  1446.       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
  1447.     }
  1448.   else
  1449.     {
  1450.       intern->r_symndx = (((int) ext->r_bits[0]
  1451.                << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
  1452.               | ((int) ext->r_bits[1]
  1453.                  << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
  1454.               | ((int) ext->r_bits[2]
  1455.                  << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
  1456.       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
  1457.             >> RELOC_BITS3_TYPE_SH_LITTLE);
  1458.       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
  1459.     }
  1460. }
  1461.  
  1462. /* Swap a reloc out.  */
  1463.  
  1464. static unsigned int
  1465. ecoff_swap_reloc_out (abfd, src, dst)
  1466.      bfd *abfd;
  1467.      PTR src;
  1468.      PTR dst;
  1469. {
  1470.   struct internal_reloc *intern = (struct internal_reloc *) src;
  1471.   RELOC *ext = (RELOC *) dst;
  1472.  
  1473.   bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
  1474.   if (abfd->xvec->header_byteorder_big_p != false)
  1475.     {
  1476.       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
  1477.       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
  1478.       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
  1479.       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
  1480.              & RELOC_BITS3_TYPE_BIG)
  1481.             | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
  1482.     }
  1483.   else
  1484.     {
  1485.       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
  1486.       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
  1487.       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
  1488.       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
  1489.              & RELOC_BITS3_TYPE_LITTLE)
  1490.             | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
  1491.     }
  1492.  
  1493.   return RELSZ;
  1494. }
  1495.  
  1496. /* ECOFF relocs are either against external symbols, or against
  1497.    sections.  If we are producing relocateable output, and the reloc
  1498.    is against an external symbol, and nothing has given us any
  1499.    additional addend, the resulting reloc will also be against the
  1500.    same symbol.  In such a case, we don't want to change anything
  1501.    about the way the reloc is handled, since it will all be done at
  1502.    final link time.  Rather than put special case code into
  1503.    bfd_perform_relocation, all the reloc types use this howto
  1504.    function.  It just short circuits the reloc if producing
  1505.    relocateable output against an external symbol.  */
  1506.  
  1507. static bfd_reloc_status_type
  1508. ecoff_generic_reloc (abfd,
  1509.              reloc_entry,
  1510.              symbol,
  1511.              data,
  1512.              input_section,
  1513.              output_bfd)
  1514.      bfd *abfd;
  1515.      arelent *reloc_entry;
  1516.      asymbol *symbol;
  1517.      PTR data;
  1518.      asection *input_section;
  1519.      bfd *output_bfd;
  1520. {
  1521.   if (output_bfd != (bfd *) NULL
  1522.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1523.       && reloc_entry->addend == 0)
  1524.     {
  1525.       reloc_entry->address += input_section->output_offset;
  1526.       return bfd_reloc_ok;
  1527.     }
  1528.  
  1529.   return bfd_reloc_continue;
  1530. }
  1531.  
  1532. /* Do a REFHI relocation.  This has to be done in combination with a
  1533.    REFLO reloc, because there is a carry from the REFLO to the REFHI.
  1534.    Here we just save the information we need; we do the actual
  1535.    relocation when we see the REFLO.  ECOFF requires that the REFLO
  1536.    immediately follow the REFHI, so this ought to work.  */
  1537.  
  1538. static bfd_byte *ecoff_refhi_addr;
  1539. static bfd_vma ecoff_refhi_addend;
  1540.  
  1541. static bfd_reloc_status_type
  1542. ecoff_refhi_reloc (abfd,
  1543.            reloc_entry,
  1544.            symbol,
  1545.            data,
  1546.            input_section,
  1547.            output_bfd)
  1548.      bfd *abfd;
  1549.      arelent *reloc_entry;
  1550.      asymbol *symbol;
  1551.      PTR data;
  1552.      asection *input_section;
  1553.      bfd *output_bfd;
  1554. {
  1555.   bfd_reloc_status_type ret;
  1556.   bfd_vma relocation;
  1557.  
  1558.   /* If we're relocating, and this an external symbol, we don't want
  1559.      to change anything.  */
  1560.   if (output_bfd != (bfd *) NULL
  1561.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1562.       && reloc_entry->addend == 0)
  1563.     {
  1564.       reloc_entry->address += input_section->output_offset;
  1565.       return bfd_reloc_ok;
  1566.     }
  1567.  
  1568.   ret = bfd_reloc_ok;
  1569.   if (symbol->section == &bfd_und_section
  1570.       && output_bfd == (bfd *) NULL)
  1571.     ret = bfd_reloc_undefined;
  1572.  
  1573.   if (bfd_is_com_section (symbol->section))
  1574.     relocation = 0;
  1575.   else
  1576.     relocation = symbol->value;
  1577.  
  1578.   relocation += symbol->section->output_section->vma;
  1579.   relocation += symbol->section->output_offset;
  1580.   relocation += reloc_entry->addend;
  1581.  
  1582.   if (reloc_entry->address > input_section->_cooked_size)
  1583.     return bfd_reloc_outofrange;
  1584.  
  1585.   /* Save the information, and let REFLO do the actual relocation.  */
  1586.   ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
  1587.   ecoff_refhi_addend = relocation;
  1588.  
  1589.   if (output_bfd != (bfd *) NULL)
  1590.     reloc_entry->address += input_section->output_offset;
  1591.  
  1592.   return ret;
  1593. }
  1594.  
  1595. /* Do a REFLO relocation.  This is a straightforward 16 bit inplace
  1596.    relocation; this function exists in order to do the REFHI
  1597.    relocation described above.  */
  1598.  
  1599. static bfd_reloc_status_type
  1600. ecoff_reflo_reloc (abfd,
  1601.            reloc_entry,
  1602.            symbol,
  1603.            data,
  1604.            input_section,
  1605.            output_bfd)
  1606.      bfd *abfd;
  1607.      arelent *reloc_entry;
  1608.      asymbol *symbol;
  1609.      PTR data;
  1610.      asection *input_section;
  1611.      bfd *output_bfd;
  1612. {
  1613.   if (ecoff_refhi_addr != (bfd_byte *) NULL)
  1614.     {
  1615.       unsigned long insn;
  1616.       unsigned long val;
  1617.       unsigned long vallo;
  1618.  
  1619.       /* Do the REFHI relocation.  Note that we actually don't need to
  1620.      know anything about the REFLO itself, except where to find
  1621.      the low 16 bits of the addend needed by the REFHI.  */
  1622.       insn = bfd_get_32 (abfd, ecoff_refhi_addr);
  1623.       vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
  1624.            & 0xffff);
  1625.       val = ((insn & 0xffff) << 16) + vallo;
  1626.       val += ecoff_refhi_addend;
  1627.  
  1628.       /* The low order 16 bits are always treated as a signed value.
  1629.      Therefore, a negative value in the low order bits requires an
  1630.      adjustment in the high order bits.  We need to make this
  1631.      adjustment in two ways: once for the bits we took from the
  1632.      data, and once for the bits we are putting back in to the
  1633.      data.  */
  1634.       if ((vallo & 0x8000) != 0)
  1635.     val -= 0x10000;
  1636.       if ((val & 0x8000) != 0)
  1637.     val += 0x10000;
  1638.  
  1639.       insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
  1640.       bfd_put_32 (abfd, insn, ecoff_refhi_addr);
  1641.  
  1642.       ecoff_refhi_addr = (bfd_byte *) NULL;
  1643.     }
  1644.  
  1645.   /* Now do the REFLO reloc in the usual way.  */
  1646.   return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
  1647.                   input_section, output_bfd);
  1648. }
  1649.  
  1650. /* Do a GPREL relocation.  This is a 16 bit value which must become
  1651.    the offset from the gp register.  */
  1652.  
  1653. static bfd_reloc_status_type
  1654. ecoff_gprel_reloc (abfd,
  1655.            reloc_entry,
  1656.            symbol,
  1657.            data,
  1658.            input_section,
  1659.            output_bfd)
  1660.      bfd *abfd;
  1661.      arelent *reloc_entry;
  1662.      asymbol *symbol;
  1663.      PTR data;
  1664.      asection *input_section;
  1665.      bfd *output_bfd;
  1666. {
  1667.   boolean relocateable;
  1668.   bfd_vma relocation;
  1669.   unsigned long val;
  1670.   unsigned long insn;
  1671.  
  1672.   /* If we're relocating, and this is an external symbol with no
  1673.      addend, we don't want to change anything.  We will only have an
  1674.      addend if this is a newly created reloc, not read from an ECOFF
  1675.      file.  */
  1676.   if (output_bfd != (bfd *) NULL
  1677.       && (symbol->flags & BSF_SECTION_SYM) == 0
  1678.       && reloc_entry->addend == 0)
  1679.     {
  1680.       reloc_entry->address += input_section->output_offset;
  1681.       return bfd_reloc_ok;
  1682.     }
  1683.  
  1684.   if (output_bfd != (bfd *) NULL)
  1685.     relocateable = true;
  1686.   else
  1687.     {
  1688.       relocateable = false;
  1689.       output_bfd = symbol->section->output_section->owner;
  1690.     }
  1691.  
  1692.   if (symbol->section == &bfd_und_section
  1693.       && relocateable == false)
  1694.     return bfd_reloc_undefined;
  1695.  
  1696.   /* We have to figure out the gp value, so that we can adjust the
  1697.      symbol value correctly.  We look up the symbol _gp in the output
  1698.      BFD.  If we can't find it, we're stuck.  We cache it in the ECOFF
  1699.      target data.  We don't need to adjust the symbol value for an
  1700.      external symbol if we are producing relocateable output.  */
  1701.   if (ecoff_data (output_bfd)->gp == 0
  1702.       && (relocateable == false
  1703.       || (symbol->flags & BSF_SECTION_SYM) != 0))
  1704.     {
  1705.       if (relocateable != false)
  1706.     {
  1707.       /* Make up a value.  */
  1708.       ecoff_data (output_bfd)->gp =
  1709.         symbol->section->output_section->vma + 0x4000;
  1710.     }
  1711.       else
  1712.     {
  1713.       unsigned int count;
  1714.       asymbol **sym;
  1715.       unsigned int i;
  1716.  
  1717.       count = bfd_get_symcount (output_bfd);
  1718.       sym = bfd_get_outsymbols (output_bfd);
  1719.  
  1720.       /* We should do something more friendly here, but we don't
  1721.          have a good reloc status to return.  */
  1722.       if (sym == (asymbol **) NULL)
  1723.         abort ();
  1724.  
  1725.       for (i = 0; i < count; i++, sym++)
  1726.         {
  1727.           register CONST char *name;
  1728.  
  1729.           name = bfd_asymbol_name (*sym);
  1730.           if (*name == '_' && strcmp (name, "_gp") == 0)
  1731.         {
  1732.           ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
  1733.           break;
  1734.         }
  1735.         }
  1736.  
  1737.       /* We should do something more friendly here, but we don't have
  1738.          a good reloc status to return.  */
  1739.       if (i >= count)
  1740.         abort ();
  1741.     }
  1742.     }
  1743.  
  1744.   if (bfd_is_com_section (symbol->section))
  1745.     relocation = 0;
  1746.   else
  1747.     relocation = symbol->value;
  1748.  
  1749.   relocation += symbol->section->output_section->vma;
  1750.   relocation += symbol->section->output_offset;
  1751.  
  1752.   if (reloc_entry->address > input_section->_cooked_size)
  1753.     return bfd_reloc_outofrange;
  1754.  
  1755.   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
  1756.  
  1757.   /* Set val to the offset into the section or symbol.  */
  1758.   val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
  1759.   if (val & 0x8000)
  1760.     val -= 0x10000;
  1761.  
  1762.   /* Adjust val for the final section location and GP value.  If we
  1763.      are producing relocateable output, we don't want to do this for
  1764.      an external symbol.  */
  1765.   if (relocateable == false
  1766.       || (symbol->flags & BSF_SECTION_SYM) != 0)
  1767.     val += relocation - ecoff_data (output_bfd)->gp;
  1768.  
  1769.   insn = (insn &~ 0xffff) | (val & 0xffff);
  1770.   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
  1771.  
  1772.   if (relocateable != false)
  1773.     reloc_entry->address += input_section->output_offset;
  1774.  
  1775.   /* Make sure it fit in 16 bits.  */
  1776.   if (val >= 0x8000 && val < 0xffff8000)
  1777.     return bfd_reloc_outofrange;
  1778.  
  1779.   return bfd_reloc_ok;
  1780. }
  1781.  
  1782. /* How to process the various relocs types.  */
  1783.  
  1784. static reloc_howto_type ecoff_howto_table[] =
  1785. {
  1786.   /* Reloc type 0 is ignored.  The reloc reading code ensures that
  1787.      this is a reference to the .abs section, which will cause
  1788.      bfd_perform_relocation to do nothing.  */
  1789.   HOWTO (ECOFF_R_IGNORE,    /* type */
  1790.      0,            /* rightshift */
  1791.      0,            /* size (0 = byte, 1 = short, 2 = long) */
  1792.      8,            /* bitsize (obsolete) */
  1793.      false,            /* pc_relative */
  1794.      0,            /* bitpos */
  1795.      false,            /* absolute (obsolete) */
  1796.      false,            /* complain_on_overflow */
  1797.      0,            /* special_function */
  1798.      "IGNORE",        /* name */
  1799.      false,            /* partial_inplace */
  1800.      0,            /* src_mask */
  1801.      0,            /* dst_mask */
  1802.      false),        /* pcrel_offset */
  1803.  
  1804.   /* A 16 bit reference to a symbol, normally from a data section.  */
  1805.   HOWTO (ECOFF_R_REFHALF,    /* type */
  1806.      0,            /* rightshift */
  1807.      1,            /* size (0 = byte, 1 = short, 2 = long) */
  1808.      16,            /* bitsize (obsolete) */
  1809.      false,            /* pc_relative */
  1810.      0,            /* bitpos */
  1811.      false,            /* absolute (obsolete) */
  1812.      true,            /* complain_on_overflow */
  1813.      ecoff_generic_reloc,    /* special_function */
  1814.      "REFHALF",        /* name */
  1815.      true,            /* partial_inplace */
  1816.      0xffff,        /* src_mask */
  1817.      0xffff,        /* dst_mask */
  1818.      false),        /* pcrel_offset */
  1819.  
  1820.   /* A 32 bit reference to a symbol, normally from a data section.  */
  1821.   HOWTO (ECOFF_R_REFWORD,    /* type */
  1822.      0,            /* rightshift */
  1823.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1824.      32,            /* bitsize (obsolete) */
  1825.      false,            /* pc_relative */
  1826.      0,            /* bitpos */
  1827.      false,            /* absolute (obsolete) */
  1828.      true,            /* complain_on_overflow */
  1829.      ecoff_generic_reloc,    /* special_function */
  1830.      "REFWORD",        /* name */
  1831.      true,            /* partial_inplace */
  1832.      0xffffffff,        /* src_mask */
  1833.      0xffffffff,        /* dst_mask */
  1834.      false),        /* pcrel_offset */
  1835.  
  1836.   /* A 26 bit absolute jump address.  */
  1837.   HOWTO (ECOFF_R_JMPADDR,    /* type */
  1838.      2,            /* rightshift */
  1839.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1840.      32,            /* bitsize (obsolete) */
  1841.      false,            /* pc_relative */
  1842.      0,            /* bitpos */
  1843.      false,            /* absolute (obsolete) */
  1844.      true,            /* complain_on_overflow */
  1845.      ecoff_generic_reloc,    /* special_function */
  1846.      "JMPADDR",        /* name */
  1847.      true,            /* partial_inplace */
  1848.      0x3ffffff,        /* src_mask */
  1849.      0x3ffffff,        /* dst_mask */
  1850.      false),        /* pcrel_offset */
  1851.  
  1852.   /* The high 16 bits of a symbol value.  Handled by the function
  1853.      ecoff_refhi_reloc.  */
  1854.   HOWTO (ECOFF_R_REFHI,        /* type */
  1855.      16,            /* rightshift */
  1856.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1857.      32,            /* bitsize (obsolete) */
  1858.      false,            /* pc_relative */
  1859.      0,            /* bitpos */
  1860.      false,            /* absolute (obsolete) */
  1861.      true,            /* complain_on_overflow */
  1862.      ecoff_refhi_reloc,    /* special_function */
  1863.      "REFHI",        /* name */
  1864.      true,            /* partial_inplace */
  1865.      0xffff,        /* src_mask */
  1866.      0xffff,        /* dst_mask */
  1867.      false),        /* pcrel_offset */
  1868.  
  1869.   /* The low 16 bits of a symbol value.  */
  1870.   HOWTO (ECOFF_R_REFLO,        /* type */
  1871.      0,            /* rightshift */
  1872.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1873.      32,            /* bitsize (obsolete) */
  1874.      false,            /* pc_relative */
  1875.      0,            /* bitpos */
  1876.      false,            /* absolute (obsolete) */
  1877.      true,            /* complain_on_overflow */
  1878.      ecoff_reflo_reloc,    /* special_function */
  1879.      "REFLO",        /* name */
  1880.      true,            /* partial_inplace */
  1881.      0xffff,        /* src_mask */
  1882.      0xffff,        /* dst_mask */
  1883.      false),        /* pcrel_offset */
  1884.  
  1885.   /* A reference to an offset from the gp register.  Handled by the
  1886.      function ecoff_gprel_reloc.  */
  1887.   HOWTO (ECOFF_R_GPREL,        /* type */
  1888.      0,            /* rightshift */
  1889.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1890.      32,            /* bitsize (obsolete) */
  1891.      false,            /* pc_relative */
  1892.      0,            /* bitpos */
  1893.      false,            /* absolute (obsolete) */
  1894.      true,            /* complain_on_overflow */
  1895.      ecoff_gprel_reloc,    /* special_function */
  1896.      "GPREL",        /* name */
  1897.      true,            /* partial_inplace */
  1898.      0xffff,        /* src_mask */
  1899.      0xffff,        /* dst_mask */
  1900.      false),        /* pcrel_offset */
  1901.  
  1902.   /* A reference to a literal using an offset from the gp register.
  1903.      Handled by the function ecoff_gprel_reloc.  */
  1904.   HOWTO (ECOFF_R_LITERAL,    /* type */
  1905.      0,            /* rightshift */
  1906.      2,            /* size (0 = byte, 1 = short, 2 = long) */
  1907.      32,            /* bitsize (obsolete) */
  1908.      false,            /* pc_relative */
  1909.      0,            /* bitpos */
  1910.      false,            /* absolute (obsolete) */
  1911.      true,            /* complain_on_overflow */
  1912.      ecoff_gprel_reloc,    /* special_function */
  1913.      "LITERAL",        /* name */
  1914.      true,            /* partial_inplace */
  1915.      0xffff,        /* src_mask */
  1916.      0xffff,        /* dst_mask */
  1917.      false)            /* pcrel_offset */
  1918. };
  1919.  
  1920. #define ECOFF_HOWTO_COUNT \
  1921.   (sizeof ecoff_howto_table / sizeof ecoff_howto_table[0])
  1922.  
  1923. /* Read in the relocs for a section.  */
  1924.  
  1925. static boolean
  1926. ecoff_slurp_reloc_table (abfd, section, symbols)
  1927.      bfd *abfd;
  1928.      asection *section;
  1929.      asymbol **symbols;
  1930. {
  1931.   RELOC *external_relocs;
  1932.   arelent *internal_relocs;
  1933.   arelent *rptr;
  1934.   unsigned int i;
  1935.  
  1936.   if (section->relocation != (arelent *) NULL
  1937.       || section->reloc_count == 0
  1938.       || (section->flags & SEC_CONSTRUCTOR) != 0)
  1939.     return true;
  1940.  
  1941.   if (ecoff_slurp_symbol_table (abfd) == false)
  1942.     return false;
  1943.   
  1944.   internal_relocs = (arelent *) bfd_alloc (abfd,
  1945.                        (sizeof (arelent)
  1946.                         * section->reloc_count));
  1947.   external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
  1948.   if (internal_relocs == (arelent *) NULL
  1949.       || external_relocs == (RELOC *) NULL)
  1950.     {
  1951.       bfd_error = no_memory;
  1952.       return false;
  1953.     }
  1954.   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
  1955.     return false;
  1956.   if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
  1957.       != RELSZ * section->reloc_count)
  1958.     {
  1959.       bfd_error = system_call_error;
  1960.       return false;
  1961.     }
  1962.  
  1963.   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
  1964.     {
  1965.       struct internal_reloc intern;
  1966.  
  1967.       ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
  1968.  
  1969.       if (intern.r_type > ECOFF_R_LITERAL)
  1970.     abort ();
  1971.  
  1972.       if (intern.r_extern)
  1973.     {
  1974.       /* r_symndx is an index into the external symbols.  */
  1975.       BFD_ASSERT (intern.r_symndx >= 0
  1976.               && (intern.r_symndx
  1977.               < ecoff_data (abfd)->symbolic_header.iextMax));
  1978.       rptr->sym_ptr_ptr = symbols + intern.r_symndx;
  1979.       rptr->addend = 0;
  1980.     }
  1981.       else
  1982.     {
  1983.       CONST char *sec_name;
  1984.       asection *sec;
  1985.  
  1986.       /* r_symndx is a section key.  */
  1987.       switch (intern.r_symndx)
  1988.         {
  1989.         case RELOC_SECTION_TEXT:  sec_name = ".text";  break;
  1990.         case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
  1991.         case RELOC_SECTION_DATA:  sec_name = ".data";  break;
  1992.         case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
  1993.         case RELOC_SECTION_SBSS:  sec_name = ".sbss";  break;
  1994.         case RELOC_SECTION_BSS:   sec_name = ".bss";   break;
  1995.         case RELOC_SECTION_INIT:  sec_name = ".init";  break;
  1996.         case RELOC_SECTION_LIT8:  sec_name = ".lit8";  break;
  1997.         case RELOC_SECTION_LIT4:  sec_name = ".lit4";  break;
  1998.         default: abort ();
  1999.         }
  2000.  
  2001.       sec = bfd_get_section_by_name (abfd, sec_name);
  2002.       if (sec == (asection *) NULL)
  2003.         abort ();
  2004.       rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
  2005.  
  2006.       rptr->addend = - bfd_get_section_vma (abfd, sec);
  2007.       if (intern.r_type == ECOFF_R_GPREL
  2008.           || intern.r_type == ECOFF_R_LITERAL)
  2009.         rptr->addend += ecoff_data (abfd)->gp;
  2010.     }
  2011.  
  2012.       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
  2013.       rptr->howto = &ecoff_howto_table[intern.r_type];
  2014.  
  2015.       /* If the type is ECOFF_R_IGNORE, make sure this is a reference
  2016.      to the absolute section so that the reloc is ignored.  */
  2017.       if (intern.r_type == ECOFF_R_IGNORE)
  2018.     rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
  2019.     }
  2020.  
  2021.   bfd_release (abfd, external_relocs);
  2022.  
  2023.   section->relocation = internal_relocs;
  2024.  
  2025.   return true;
  2026. }
  2027.  
  2028. /* Get a canonical list of relocs.  */
  2029.  
  2030. static unsigned int
  2031. ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
  2032.      bfd *abfd;
  2033.      asection *section;
  2034.      arelent **relptr;
  2035.      asymbol **symbols;
  2036. {
  2037.   unsigned int count;
  2038.  
  2039.   if (section->flags & SEC_CONSTRUCTOR) 
  2040.     {
  2041.       arelent_chain *chain;
  2042.  
  2043.       /* This section has relocs made up by us, not the file, so take
  2044.      them out of their chain and place them into the data area
  2045.      provided.  */
  2046.       for (count = 0, chain = section->constructor_chain;
  2047.        count < section->reloc_count;
  2048.        count++, chain = chain->next)
  2049.     *relptr++ = &chain->relent;
  2050.     }
  2051.   else
  2052.     { 
  2053.       arelent *tblptr;
  2054.  
  2055.       if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
  2056.     return 0;
  2057.  
  2058.       tblptr = section->relocation;
  2059.       if (tblptr == (arelent *) NULL)
  2060.     return 0;
  2061.  
  2062.       for (count = 0; count < section->reloc_count; count++)
  2063.     *relptr++ = tblptr++;
  2064.     }
  2065.  
  2066.   *relptr = (arelent *) NULL;
  2067.  
  2068.   return section->reloc_count;
  2069. }
  2070.  
  2071. /* Get the howto structure for a generic reloc type.  */
  2072.  
  2073. static CONST struct reloc_howto_struct *
  2074. ecoff_bfd_reloc_type_lookup (abfd, code)
  2075.      bfd *abfd;
  2076.      bfd_reloc_code_real_type code;
  2077. {
  2078.   int ecoff_type;
  2079.  
  2080.   switch (code)
  2081.     {
  2082.     case BFD_RELOC_16:
  2083.       ecoff_type = ECOFF_R_REFHALF;
  2084.       break;
  2085.     case BFD_RELOC_32:
  2086.       ecoff_type = ECOFF_R_REFWORD;
  2087.       break;
  2088.     case BFD_RELOC_MIPS_JMP:
  2089.       ecoff_type = ECOFF_R_JMPADDR;
  2090.       break;
  2091.     case BFD_RELOC_HI16_S:
  2092.       ecoff_type = ECOFF_R_REFHI;
  2093.       break;
  2094.     case BFD_RELOC_LO16:
  2095.       ecoff_type = ECOFF_R_REFLO;
  2096.       break;
  2097.     case BFD_RELOC_MIPS_GPREL:
  2098.       ecoff_type = ECOFF_R_GPREL;
  2099.       break;
  2100.     default:
  2101.       return (CONST struct reloc_howto_struct *) NULL;
  2102.     }
  2103.  
  2104.   return &ecoff_howto_table[ecoff_type];
  2105. }
  2106.  
  2107. /* Provided a BFD, a section and an offset into the section, calculate
  2108.    and return the name of the source file and the line nearest to the
  2109.    wanted location.  */
  2110.  
  2111. static boolean
  2112. ecoff_find_nearest_line (abfd,
  2113.              section,
  2114.              ignore_symbols,
  2115.              offset,
  2116.              filename_ptr,
  2117.              functionname_ptr,
  2118.              retline_ptr)
  2119.      bfd *abfd;
  2120.      asection *section;
  2121.      asymbol **ignore_symbols;
  2122.      bfd_vma offset;
  2123.      CONST char **filename_ptr;
  2124.      CONST char **functionname_ptr;
  2125.      unsigned int *retline_ptr;
  2126. {
  2127.   FDR *fdr_ptr;
  2128.   FDR *fdr_start;
  2129.   FDR *fdr_end;
  2130.   FDR *fdr_hold;
  2131.   struct pdr_ext *pdr_ptr;
  2132.   struct pdr_ext *pdr_end;
  2133.   PDR pdr;
  2134.   unsigned char *line_ptr;
  2135.   unsigned char *line_end;
  2136.   int lineno;
  2137.  
  2138.   /* If we're not in the .text section, we don't have any line
  2139.      numbers.  */
  2140.   if (strcmp (section->name, _TEXT) != 0
  2141.       || offset < ecoff_data (abfd)->text_start
  2142.       || offset >= ecoff_data (abfd)->text_end)
  2143.     return false;
  2144.  
  2145.   /* Make sure we have the FDR's.  */
  2146.   if (ecoff_slurp_symbolic_info (abfd) == false
  2147.       || bfd_get_symcount (abfd) == 0)
  2148.     return false;
  2149.  
  2150.   /* Each file descriptor (FDR) has a memory address.  Here we track
  2151.      down which FDR we want.  The FDR's are stored in increasing
  2152.      memory order.  If speed is ever important, this can become a
  2153.      binary search.  We must ignore FDR's with no PDR entries; they
  2154.      will have the adr of the FDR before or after them.  */
  2155.   fdr_start = ecoff_data (abfd)->fdr;
  2156.   fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
  2157.   fdr_hold = (FDR *) NULL;
  2158.   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
  2159.     {
  2160.       if (fdr_ptr->cpd == 0)
  2161.     continue;
  2162.       if (offset < fdr_ptr->adr)
  2163.     break;
  2164.       fdr_hold = fdr_ptr;
  2165.     }
  2166.   if (fdr_hold == (FDR *) NULL)
  2167.     return false;
  2168.   fdr_ptr = fdr_hold;
  2169.  
  2170.   /* Each FDR has a list of procedure descriptors (PDR).  PDR's also
  2171.      have an address, which is relative to the FDR address, and are
  2172.      also stored in increasing memory order.  */
  2173.   offset -= fdr_ptr->adr;
  2174.   pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
  2175.   pdr_end = pdr_ptr + fdr_ptr->cpd;
  2176.   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2177.  
  2178.   /* The address of the first PDR is an offset which applies to the
  2179.      addresses of all the PDR's.  */
  2180.   offset += pdr.adr;
  2181.  
  2182.   for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
  2183.     {
  2184.       ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2185.       if (offset < pdr.adr)
  2186.     break;
  2187.     }
  2188.  
  2189.   /* Now we can look for the actual line number.  The line numbers are
  2190.      stored in a very funky format, which I won't try to describe.
  2191.      Note that right here pdr_ptr and pdr hold the PDR *after* the one
  2192.      we want; we need this to compute line_end.  */
  2193.   line_end = ecoff_data (abfd)->line;
  2194.   if (pdr_ptr == pdr_end)
  2195.     line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
  2196.   else
  2197.     line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
  2198.  
  2199.   /* Now change pdr and pdr_ptr to the one we want.  */
  2200.   pdr_ptr--;
  2201.   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
  2202.  
  2203.   offset -= pdr.adr;
  2204.   lineno = pdr.lnLow;
  2205.   line_ptr = (ecoff_data (abfd)->line
  2206.           + fdr_ptr->cbLineOffset
  2207.           + pdr.cbLineOffset);
  2208.   while (line_ptr < line_end)
  2209.     {
  2210.       int delta;
  2211.       int count;
  2212.  
  2213.       delta = *line_ptr >> 4;
  2214.       if (delta >= 0x8)
  2215.     delta -= 0x10;
  2216.       count = (*line_ptr & 0xf) + 1;
  2217.       ++line_ptr;
  2218.       if (delta == -8)
  2219.     {
  2220.       delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
  2221.       if (delta >= 0x8000)
  2222.         delta -= 0x10000;
  2223.       line_ptr += 2;
  2224.     }
  2225.       lineno += delta;
  2226.       if (offset < count * 4)
  2227.     break;
  2228.       offset -= count * 4;
  2229.     }
  2230.  
  2231.   /* If fdr_ptr->rss is -1, then this file does not have full symbols,
  2232.      at least according to gdb/mipsread.c.  */
  2233.   if (fdr_ptr->rss == -1)
  2234.     {
  2235.       *filename_ptr = NULL;
  2236.       if (pdr.isym == -1)
  2237.     *functionname_ptr = NULL;
  2238.       else
  2239.     {
  2240.       EXTR proc_ext;
  2241.  
  2242.       ecoff_swap_ext_in (abfd,
  2243.                  (ecoff_data (abfd)->external_ext
  2244.                   + pdr.isym),
  2245.                  &proc_ext);
  2246.       *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
  2247.     }
  2248.     }
  2249.   else
  2250.     {
  2251.       SYMR proc_sym;
  2252.  
  2253.       *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
  2254.       ecoff_swap_sym_in (abfd,
  2255.              (ecoff_data (abfd)->external_sym
  2256.               + fdr_ptr->isymBase
  2257.               + pdr.isym),
  2258.              &proc_sym);
  2259.       *functionname_ptr = (ecoff_data (abfd)->ss
  2260.                + fdr_ptr->issBase
  2261.                + proc_sym.iss);
  2262.     }
  2263.   *retline_ptr = lineno;
  2264.   return true;
  2265. }
  2266.  
  2267. /* We can't use the generic linking routines for ECOFF, because we
  2268.    have to handle all the debugging information.  The generic link
  2269.    routine just works out the section contents and attaches a list of
  2270.    symbols.
  2271.  
  2272.    We link by looping over all the seclets.  We make two passes.  On
  2273.    the first we set the actual section contents and determine the size
  2274.    of the debugging information.  On the second we accumulate the
  2275.    debugging information and write it out.
  2276.  
  2277.    This currently always accumulates the debugging information, which
  2278.    is incorrect, because it ignores the -s and -S options of the
  2279.    linker.  The linker needs to be modified to give us that
  2280.    information in a more useful format (currently it just provides a
  2281.    list of symbols which should appear in the output file).  */
  2282.  
  2283. /* Clear the output_has_begun flag for all the input BFD's.  We use it
  2284.    to avoid linking in the debugging information for a BFD more than
  2285.    once.  */
  2286.  
  2287. static void
  2288. ecoff_clear_output_flags (abfd)
  2289.      bfd *abfd;
  2290. {
  2291.   register asection *o;
  2292.   register bfd_seclet_type *p;
  2293.  
  2294.   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2295.     for (p = o->seclets_head;
  2296.      p != (bfd_seclet_type *) NULL;
  2297.      p = p->next)
  2298.       if (p->type == bfd_indirect_seclet)
  2299.     p->u.indirect.section->owner->output_has_begun = false;
  2300. }
  2301.  
  2302. /* Handle an indirect seclet on the first pass.  Set the contents of
  2303.    the output section, and accumulate the debugging information if
  2304.    any.  */
  2305.  
  2306. static boolean
  2307. ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
  2308.      bfd *output_bfd;
  2309.      bfd_seclet_type *seclet;
  2310.      asection *output_section;
  2311.      PTR data;
  2312.      boolean relocateable;
  2313. {
  2314.   bfd *input_bfd;
  2315.   HDRR *output_symhdr;
  2316.   HDRR *input_symhdr;
  2317.  
  2318.   if ((output_section->flags & SEC_HAS_CONTENTS)
  2319.       && !(output_section->flags & SEC_NEVER_LOAD)
  2320.       && (output_section->flags & SEC_LOAD)
  2321.       && seclet->size)
  2322.     {
  2323.       data = (PTR) bfd_get_relocated_section_contents (output_bfd,
  2324.                                seclet,
  2325.                                data,
  2326.                                relocateable);
  2327.       if (bfd_set_section_contents (output_bfd,
  2328.                     output_section,
  2329.                     data,
  2330.                     seclet->offset,
  2331.                     seclet->size)
  2332.       == false)
  2333.     {
  2334.       abort();
  2335.     }
  2336.     }
  2337.  
  2338.   input_bfd = seclet->u.indirect.section->owner;
  2339.  
  2340.   /* We want to figure out how much space will be required to
  2341.      incorporate all the debugging information from input_bfd.  We use
  2342.      the output_has_begun field to avoid adding it in more than once.
  2343.      The actual incorporation is done in the second pass, in
  2344.      ecoff_get_debug.  The code has to parallel that code in its
  2345.      manipulations of output_symhdr.  */
  2346.  
  2347.   if (input_bfd->output_has_begun)
  2348.     return true;
  2349.   input_bfd->output_has_begun = true;
  2350.  
  2351.   output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
  2352.  
  2353.   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
  2354.     {
  2355.       asymbol **symbols;
  2356.       asymbol **sym_ptr;
  2357.       asymbol **sym_end;
  2358.  
  2359.       /* We just accumulate local symbols from a non-ECOFF BFD.  The
  2360.      external symbols are handled separately.  */
  2361.  
  2362.       symbols = (asymbol **) bfd_alloc (output_bfd,
  2363.                     get_symtab_upper_bound (input_bfd));
  2364.       if (symbols == (asymbol **) NULL)
  2365.     {
  2366.       bfd_error = no_memory;
  2367.       return false;
  2368.     }
  2369.       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
  2370.  
  2371.       for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
  2372.     {
  2373.       size_t len;
  2374.  
  2375.       len = strlen ((*sym_ptr)->name);
  2376.       if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
  2377.         {
  2378.           ++output_symhdr->isymMax;
  2379.           output_symhdr->issMax += len + 1;
  2380.         }
  2381.     }
  2382.  
  2383.       bfd_release (output_bfd, (PTR) symbols);
  2384.  
  2385.       ++output_symhdr->ifdMax;
  2386.  
  2387.       return true;
  2388.     }
  2389.  
  2390.   /* We simply add in the information from another ECOFF BFD.  First
  2391.      we make sure we have the symbolic information.  */
  2392.   if (ecoff_slurp_symbol_table (input_bfd) == false)
  2393.     return false;
  2394.   if (bfd_get_symcount (input_bfd) == 0)
  2395.     return true;
  2396.  
  2397.   input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
  2398.  
  2399.   /* Figure out how much information we are going to be putting in.
  2400.      The external symbols are handled separately.  */
  2401.   output_symhdr->ilineMax += input_symhdr->ilineMax;
  2402.   output_symhdr->cbLine += input_symhdr->cbLine;
  2403.   output_symhdr->idnMax += input_symhdr->idnMax;
  2404.   output_symhdr->ipdMax += input_symhdr->ipdMax;
  2405.   output_symhdr->isymMax += input_symhdr->isymMax;
  2406.   output_symhdr->ioptMax += input_symhdr->ioptMax;
  2407.   output_symhdr->iauxMax += input_symhdr->iauxMax;
  2408.   output_symhdr->issMax += input_symhdr->issMax;
  2409.   output_symhdr->ifdMax += input_symhdr->ifdMax;
  2410.  
  2411.   /* The RFD's are special, since we create them if needed.  */
  2412.   if (input_symhdr->crfd > 0)
  2413.     output_symhdr->crfd += input_symhdr->crfd;
  2414.   else
  2415.     output_symhdr->crfd += input_symhdr->ifdMax;
  2416.  
  2417.   return true;
  2418. }
  2419.  
  2420. /* Handle an arbitrary seclet on the first pass.  */
  2421.  
  2422. static boolean
  2423. ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
  2424.      bfd *abfd;
  2425.      bfd_seclet_type *seclet;
  2426.      asection *section;
  2427.      PTR data;
  2428.      boolean relocateable;
  2429. {
  2430.   switch (seclet->type) 
  2431.     {
  2432.     case bfd_indirect_seclet:
  2433.       /* The contents of this section come from another one somewhere
  2434.      else.  */
  2435.       return ecoff_rel (abfd, seclet, section, data, relocateable);
  2436.  
  2437.     case bfd_fill_seclet:
  2438.       /* Fill in the section with fill.value.  This is used to pad out
  2439.      sections, but we must avoid padding the .bss section.  */
  2440.       if ((section->flags & SEC_HAS_CONTENTS) == 0)
  2441.     {
  2442.       if (seclet->u.fill.value != 0)
  2443.         abort ();
  2444.     }
  2445.       else
  2446.     {
  2447.       char *d = (char *) bfd_alloc (abfd, seclet->size);
  2448.       unsigned int i;
  2449.       boolean ret;
  2450.  
  2451.       for (i = 0; i < seclet->size; i+=2)
  2452.         d[i] = seclet->u.fill.value >> 8;
  2453.       for (i = 1; i < seclet->size; i+=2)
  2454.         d[i] = seclet->u.fill.value;
  2455.       ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
  2456.                       seclet->size);
  2457.       bfd_release (abfd, (PTR) d);
  2458.       return ret;
  2459.     }
  2460.       break;
  2461.  
  2462.     default:
  2463.       abort();
  2464.     }
  2465.  
  2466.   return true;
  2467. }
  2468.  
  2469. /* Add a string to the debugging information we are accumulating for a
  2470.    file.  Return the offset from the fdr string base or from the
  2471.    external string base.  */
  2472.  
  2473. static long
  2474. ecoff_add_string (output_bfd, fdr, string, external)
  2475.      bfd *output_bfd;
  2476.      FDR *fdr;
  2477.      CONST char *string;
  2478.      boolean external;
  2479. {
  2480.   HDRR *symhdr;
  2481.   size_t len;
  2482.   long ret;
  2483.  
  2484.   symhdr = &ecoff_data (output_bfd)->symbolic_header;
  2485.   len = strlen (string);
  2486.   if (external)
  2487.     {
  2488.       strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
  2489.       ret = symhdr->issExtMax;
  2490.       symhdr->issExtMax += len + 1;
  2491.     }
  2492.   else
  2493.     {
  2494.       strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
  2495.       ret = fdr->cbSs;
  2496.       symhdr->issMax += len + 1;
  2497.       fdr->cbSs += len + 1;
  2498.     }
  2499.   return ret;
  2500. }
  2501.  
  2502. /* Accumulate the debugging information from an input section.  */
  2503.  
  2504. static boolean
  2505. ecoff_get_debug (output_bfd, seclet, section, relocateable)
  2506.      bfd *output_bfd;
  2507.      bfd_seclet_type *seclet;
  2508.      asection *section;
  2509.      boolean relocateable;
  2510. {
  2511.   bfd *input_bfd;
  2512.   HDRR *output_symhdr;
  2513.   HDRR *input_symhdr;
  2514.   ecoff_data_type *output_ecoff;
  2515.   ecoff_data_type *input_ecoff;
  2516.   unsigned int count;
  2517.   struct sym_ext *sym_out;
  2518.   ecoff_symbol_type *esym_ptr;
  2519.   ecoff_symbol_type *esym_end;
  2520.   unsigned long pdr_off;
  2521.   FDR *fdr_ptr;
  2522.   FDR *fdr_end;
  2523.   struct fdr_ext *fdr_out;
  2524.  
  2525.   input_bfd = seclet->u.indirect.section->owner;
  2526.  
  2527.   /* Don't get the information more than once. */
  2528.   if (input_bfd->output_has_begun)
  2529.     return true;
  2530.   input_bfd->output_has_begun = true;
  2531.  
  2532.   output_ecoff = ecoff_data (output_bfd);
  2533.   output_symhdr = &output_ecoff->symbolic_header;
  2534.  
  2535.   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
  2536.     {
  2537.       FDR fdr;
  2538.       asymbol **symbols;
  2539.       asymbol **sym_ptr;
  2540.       asymbol **sym_end;
  2541.  
  2542.       /* This is not an ECOFF BFD.  Just gather the symbols.  */
  2543.  
  2544.       memset (&fdr, 0, sizeof fdr);
  2545.  
  2546.       fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
  2547.       fdr.issBase = output_symhdr->issMax;
  2548.       fdr.cbSs = 0;
  2549.       fdr.rss = ecoff_add_string (output_bfd,
  2550.                   &fdr,
  2551.                   bfd_get_filename (input_bfd),
  2552.                   false);
  2553.       fdr.isymBase = output_symhdr->isymMax;
  2554.  
  2555.       /* Get the local symbols from the input BFD.  */
  2556.       symbols = (asymbol **) bfd_alloc (output_bfd,
  2557.                     get_symtab_upper_bound (input_bfd));
  2558.       if (symbols == (asymbol **) NULL)
  2559.     {
  2560.       bfd_error = no_memory;
  2561.       return false;
  2562.     }
  2563.       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
  2564.  
  2565.       /* Handle the local symbols.  Any external symbols are handled
  2566.      separately.  */
  2567.       fdr.csym = 0;
  2568.       for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
  2569.     {
  2570.       SYMR internal_sym;
  2571.  
  2572.       if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
  2573.         continue;
  2574.       memset (&internal_sym, 0, sizeof internal_sym);
  2575.       internal_sym.iss = ecoff_add_string (output_bfd,
  2576.                            &fdr,
  2577.                            (*sym_ptr)->name,
  2578.                            false);
  2579.  
  2580.       if (bfd_is_com_section ((*sym_ptr)->section)
  2581.           || (*sym_ptr)->section == &bfd_und_section)
  2582.         internal_sym.value = (*sym_ptr)->value;
  2583.       else
  2584.         internal_sym.value = ((*sym_ptr)->value
  2585.                   + (*sym_ptr)->section->output_offset
  2586.                   + (*sym_ptr)->section->output_section->vma);
  2587.       internal_sym.st = stNil;
  2588.       internal_sym.sc = scUndefined;
  2589.       internal_sym.index = indexNil;
  2590.       ecoff_swap_sym_out (output_bfd, &internal_sym,
  2591.                   (output_ecoff->external_sym
  2592.                    + output_symhdr->isymMax));
  2593.       ++fdr.csym;
  2594.       ++output_symhdr->isymMax;
  2595.     }
  2596.  
  2597.       bfd_release (output_bfd, (PTR) symbols);
  2598.  
  2599.       /* Leave everything else in the FDR zeroed out.  This will cause
  2600.      the lang field to be langC.  The fBigendian field will
  2601.      indicate little endian format, but it doesn't matter because
  2602.      it only applies to aux fields and there are none.  */
  2603.  
  2604.       ecoff_swap_fdr_out (output_bfd, &fdr,
  2605.               (output_ecoff->external_fdr
  2606.                + output_symhdr->ifdMax));
  2607.       ++output_symhdr->ifdMax;
  2608.       return true;
  2609.     }
  2610.  
  2611.   /* This is an ECOFF BFD.  We want to grab the information from
  2612.      input_bfd and attach it to output_bfd.  */
  2613.   count = bfd_get_symcount (input_bfd);
  2614.   if (count == 0)
  2615.     return true;
  2616.   input_ecoff = ecoff_data (input_bfd);
  2617.   input_symhdr = &input_ecoff->symbolic_header;
  2618.  
  2619.   /* I think that it is more efficient to simply copy the debugging
  2620.      information from the input BFD to the output BFD.  Because ECOFF
  2621.      uses relative pointers for most of the debugging information,
  2622.      only a little of it has to be changed at all.  */
  2623.  
  2624.   /* Swap in the local symbols, adjust their values, and swap them out
  2625.      again.  The external symbols are handled separately.  */
  2626.   sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
  2627.  
  2628.   esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
  2629.   esym_end = esym_ptr + count;
  2630.   for (; esym_ptr < esym_end; esym_ptr++)
  2631.     {
  2632.       if (esym_ptr->local)
  2633.     {
  2634.       SYMR sym;
  2635.  
  2636.       ecoff_swap_sym_in (input_bfd, esym_ptr->native.lnative, &sym);
  2637.  
  2638.       /* If we're producing an executable, move common symbols
  2639.          into bss.  */
  2640.       if (relocateable == false)
  2641.         {
  2642.           if (sym.sc == scCommon)
  2643.         sym.sc = scBss;
  2644.           else if (sym.sc == scSCommon)
  2645.         sym.sc = scSBss;
  2646.         }
  2647.  
  2648.       if (! bfd_is_com_section (esym_ptr->symbol.section)
  2649.           && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
  2650.           && esym_ptr->symbol.section != &bfd_und_section)
  2651.         sym.value = (esym_ptr->symbol.value
  2652.              + esym_ptr->symbol.section->output_offset
  2653.              + esym_ptr->symbol.section->output_section->vma);
  2654.       ecoff_swap_sym_out (output_bfd, &sym, sym_out);
  2655.       ++sym_out;
  2656.     }
  2657.     }
  2658.  
  2659.   /* That should have accounted for all the local symbols in
  2660.      input_bfd.  */
  2661.   BFD_ASSERT ((sym_out - output_ecoff->external_sym) - output_symhdr->isymMax
  2662.           == input_symhdr->isymMax);
  2663.  
  2664.   /* Copy the information that does not need swapping.  */
  2665.   memcpy (output_ecoff->line + output_symhdr->cbLine,
  2666.       input_ecoff->line,
  2667.       input_symhdr->cbLine * sizeof (unsigned char));
  2668.   memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
  2669.       input_ecoff->external_aux,
  2670.       input_symhdr->iauxMax * sizeof (union aux_ext));
  2671.   memcpy (output_ecoff->ss + output_symhdr->issMax,
  2672.       input_ecoff->ss,
  2673.       input_symhdr->issMax * sizeof (char));
  2674.  
  2675.   /* Some of the information may need to be swapped.  */
  2676.   if (output_bfd->xvec->header_byteorder_big_p
  2677.       == input_bfd->xvec->header_byteorder_big_p)
  2678.     {
  2679.       /* The two BFD's have the same endianness, so memcpy will
  2680.      suffice.  */
  2681.       memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
  2682.           input_ecoff->external_dnr,
  2683.           input_symhdr->idnMax * sizeof (struct dnr_ext));
  2684.       memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
  2685.           input_ecoff->external_pdr,
  2686.           input_symhdr->ipdMax * sizeof (struct pdr_ext));
  2687.       if (input_symhdr->ipdMax == 0)
  2688.     pdr_off = 0;
  2689.       else
  2690.     {
  2691.       PDR pdr;
  2692.  
  2693.       ecoff_swap_pdr_in (input_bfd, input_ecoff->external_pdr, &pdr);
  2694.       pdr_off = pdr.adr;
  2695.     }
  2696.       memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
  2697.           input_ecoff->external_opt,
  2698.           input_symhdr->ioptMax * sizeof (struct opt_ext));
  2699.     }
  2700.   else
  2701.     {
  2702.       struct dnr_ext *dnr_in;
  2703.       struct dnr_ext *dnr_end;
  2704.       struct dnr_ext *dnr_out;
  2705.       struct pdr_ext *pdr_in;
  2706.       struct pdr_ext *pdr_end;
  2707.       struct pdr_ext *pdr_out;
  2708.       int first_pdr;
  2709.       struct opt_ext *opt_in;
  2710.       struct opt_ext *opt_end;
  2711.       struct opt_ext *opt_out;
  2712.  
  2713.       /* The two BFD's have different endianness, so we must swap
  2714.      everything in and out.  This code would always work, but it
  2715.      would be slow in the normal case.  */
  2716.       dnr_in = input_ecoff->external_dnr;
  2717.       dnr_end = dnr_in + input_symhdr->idnMax;
  2718.       dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
  2719.       for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
  2720.     {
  2721.       DNR dnr;
  2722.  
  2723.       ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
  2724.       ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
  2725.     }
  2726.       pdr_in = input_ecoff->external_pdr;
  2727.       pdr_end = pdr_in + input_symhdr->ipdMax;
  2728.       pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
  2729.       first_pdr = 1;
  2730.       pdr_off = 0;
  2731.       for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
  2732.     {
  2733.       PDR pdr;
  2734.  
  2735.       ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
  2736.       ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
  2737.       if (first_pdr)
  2738.         {
  2739.           pdr_off = pdr.adr;
  2740.           first_pdr = 0;
  2741.         }
  2742.     }
  2743.       opt_in = input_ecoff->external_opt;
  2744.       opt_end = opt_in + input_symhdr->ioptMax;
  2745.       opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
  2746.       for (; opt_in < opt_end; opt_in++, opt_out++)
  2747.     {
  2748.       OPTR opt;
  2749.  
  2750.       ecoff_swap_opt_in (input_bfd, opt_in, &opt);
  2751.       ecoff_swap_opt_out (output_bfd, &opt, opt_out);
  2752.     }
  2753.     }
  2754.  
  2755.   /* Set ifdbase so that the external symbols know how to adjust their
  2756.      ifd values.  */
  2757.   input_ecoff->ifdbase = output_symhdr->ifdMax;
  2758.  
  2759.   fdr_ptr = input_ecoff->fdr;
  2760.   fdr_end = fdr_ptr + input_symhdr->ifdMax;
  2761.   fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
  2762.   for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
  2763.     {
  2764.       FDR fdr;
  2765.  
  2766.       fdr = *fdr_ptr;
  2767.  
  2768.       /* The memory address for this fdr is the address for the seclet
  2769.      plus the offset to this fdr within input_bfd.  For some
  2770.      reason the offset of the first procedure pointer is also
  2771.      added in.  */
  2772.       fdr.adr = (bfd_get_section_vma (output_bfd, section)
  2773.          + seclet->offset
  2774.          + (fdr_ptr->adr - input_ecoff->fdr->adr)
  2775.          + pdr_off);
  2776.  
  2777.       fdr.issBase += output_symhdr->issMax;
  2778.       fdr.isymBase += output_symhdr->isymMax;
  2779.       fdr.ilineBase += output_symhdr->ilineMax;
  2780.       fdr.ioptBase += output_symhdr->ioptMax;
  2781.       fdr.ipdFirst += output_symhdr->ipdMax;
  2782.       fdr.iauxBase += output_symhdr->iauxMax;
  2783.       fdr.rfdBase += output_symhdr->crfd;
  2784.  
  2785.       /* If there are no RFD's, we are going to add some.  We don't
  2786.      want to adjust irfd for this, so that all the FDR's can share
  2787.      the RFD's.  */
  2788.       if (input_symhdr->crfd == 0)
  2789.     fdr.crfd = input_symhdr->ifdMax;
  2790.  
  2791.       if (fdr.cbLine != 0)
  2792.     fdr.cbLineOffset += output_symhdr->cbLine;
  2793.  
  2794.       ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
  2795.     }
  2796.  
  2797.   if (input_symhdr->crfd > 0)
  2798.     {
  2799.       struct rfd_ext *rfd_in;
  2800.       struct rfd_ext *rfd_end;
  2801.       struct rfd_ext *rfd_out;
  2802.  
  2803.       /* Swap and adjust the RFD's.  RFD's are only created by the
  2804.      linker, so this will only be necessary if one of the input
  2805.      files is the result of a partial link.  Presumably all
  2806.      necessary RFD's are present.  */
  2807.       rfd_in = input_ecoff->external_rfd;
  2808.       rfd_end = rfd_in + input_symhdr->crfd;
  2809.       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
  2810.       for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
  2811.     {
  2812.       RFDT rfd;
  2813.  
  2814.       ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
  2815.       rfd += output_symhdr->ifdMax;
  2816.       ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
  2817.     }
  2818.       output_symhdr->crfd += input_symhdr->crfd;
  2819.     }
  2820.   else
  2821.     {
  2822.       struct rfd_ext *rfd_out;
  2823.       struct rfd_ext *rfd_end;
  2824.       RFDT rfd;
  2825.  
  2826.       /* Create RFD's.  Some of the debugging information includes
  2827.      relative file indices.  These indices are taken as indices to
  2828.      the RFD table if there is one, or to the global table if
  2829.      there is not.  If we did not create RFD's, we would have to
  2830.      parse and adjust all the debugging information which contains
  2831.      file indices.  */
  2832.       rfd = output_symhdr->ifdMax;
  2833.       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
  2834.       rfd_end = rfd_out + input_symhdr->ifdMax;
  2835.       for (; rfd_out < rfd_end; rfd_out++, rfd++)
  2836.     ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
  2837.       output_symhdr->crfd += input_symhdr->ifdMax;
  2838.     }
  2839.  
  2840.   /* Combine the register masks.  */
  2841.   {
  2842.     int i;
  2843.  
  2844.     output_ecoff->gprmask |= input_ecoff->gprmask;
  2845.     for (i = 0; i < 4; i++)
  2846.       output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
  2847.   }
  2848.  
  2849.   /* Update the counts.  */
  2850.   output_symhdr->ilineMax += input_symhdr->ilineMax;
  2851.   output_symhdr->cbLine += input_symhdr->cbLine;
  2852.   output_symhdr->idnMax += input_symhdr->idnMax;
  2853.   output_symhdr->ipdMax += input_symhdr->ipdMax;
  2854.   output_symhdr->isymMax += input_symhdr->isymMax;
  2855.   output_symhdr->ioptMax += input_symhdr->ioptMax;
  2856.   output_symhdr->iauxMax += input_symhdr->iauxMax;
  2857.   output_symhdr->issMax += input_symhdr->issMax;
  2858.   output_symhdr->ifdMax += input_symhdr->ifdMax;
  2859.  
  2860.   return true;
  2861. }
  2862.  
  2863. /* This is the actual link routine.  It makes two passes over all the
  2864.    seclets.  */
  2865.  
  2866. static boolean
  2867. ecoff_bfd_seclet_link (abfd, data, relocateable)
  2868.      bfd *abfd;
  2869.      PTR data;
  2870.      boolean relocateable;
  2871. {
  2872.   HDRR *symhdr;
  2873.   int ipass;
  2874.   register asection *o;
  2875.   register bfd_seclet_type *p;
  2876.   asymbol **sym_ptr_ptr;
  2877.   bfd_size_type size;
  2878.   char *raw;
  2879.  
  2880.   /* We accumulate the debugging information counts in the symbolic
  2881.      header.  */
  2882.   symhdr = &ecoff_data (abfd)->symbolic_header;
  2883.   symhdr->magic = magicSym;
  2884.   /* FIXME: What should the version stamp be?  */
  2885.   symhdr->vstamp = 0;
  2886.   symhdr->ilineMax = 0;
  2887.   symhdr->cbLine = 0;
  2888.   symhdr->idnMax = 0;
  2889.   symhdr->ipdMax = 0;
  2890.   symhdr->isymMax = 0;
  2891.   symhdr->ioptMax = 0;
  2892.   symhdr->iauxMax = 0;
  2893.   symhdr->issMax = 0;
  2894.   symhdr->issExtMax = 0;
  2895.   symhdr->ifdMax = 0;
  2896.   symhdr->crfd = 0;
  2897.   symhdr->iextMax = 0;
  2898.  
  2899.   /* We need to copy over the debugging symbols from each input BFD.
  2900.      When we do this copying, we have to adjust the text address in
  2901.      the FDR structures, so we have to know the text address used for
  2902.      the input BFD.  Since we only want to copy the symbols once per
  2903.      input BFD, but we are going to look at each input BFD multiple
  2904.      times (once for each section it provides), we arrange to always
  2905.      look at the text section first.  That means that when we copy the
  2906.      debugging information, we always know the text address.  So we
  2907.      actually do each pass in two sub passes; first the text sections,
  2908.      then the non-text sections.  We use the output_has_begun flag to
  2909.      determine whether we have copied over the debugging information
  2910.      yet.  */
  2911.  
  2912.   /* Do the first pass: set the output section contents and count the
  2913.      debugging information.  */
  2914.   ecoff_clear_output_flags (abfd);
  2915.   for (ipass = 0; ipass < 2; ipass++)
  2916.     {
  2917.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  2918.     {
  2919.       /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
  2920.          so they are done on pass 0.  For other sections the
  2921.          expression is true, so they are done on pass 1.  */
  2922.       if (((o->flags & SEC_CODE) == 0) != ipass)
  2923.         continue;
  2924.  
  2925.       for (p = o->seclets_head;
  2926.            p != (bfd_seclet_type *) NULL;
  2927.            p = p->next)
  2928.         {
  2929.           if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
  2930.           == false)
  2931.         return false;
  2932.         }
  2933.     }
  2934.     }
  2935.  
  2936.   /* We handle the external symbols differently.  We use the ones
  2937.      attached to the output_bfd.  The linker will have already
  2938.      determined which symbols are to be attached.  Here we just
  2939.      determine how much space we will need for them.  */
  2940.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  2941.   if (sym_ptr_ptr != NULL)
  2942.     {
  2943.       asymbol **sym_end;
  2944.  
  2945.       sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
  2946.       for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
  2947.     {
  2948.       if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
  2949.           && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
  2950.         {
  2951.           ++symhdr->iextMax;
  2952.           symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
  2953.         }
  2954.     }
  2955.     }
  2956.  
  2957.   /* Adjust the counts so that structures are longword aligned.  */
  2958.   symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
  2959.   symhdr->issMax = (symhdr->issMax + 3) &~ 3;
  2960.   symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
  2961.  
  2962.   /* Now the counts in symhdr are the correct size for the debugging
  2963.      information.  We allocate the right amount of space, and reset
  2964.      the counts so that the second pass can use them as indices.  It
  2965.      would be possible to output the debugging information directly to
  2966.      the file in pass 2, rather than to build it in memory and then
  2967.      write it out.  Outputting to the file would require a lot of
  2968.      seeks and small writes, though, and I think this approach is
  2969.      faster.  */
  2970.   size = (symhdr->cbLine * sizeof (unsigned char)
  2971.       + symhdr->idnMax * sizeof (struct dnr_ext)
  2972.       + symhdr->ipdMax * sizeof (struct pdr_ext)
  2973.       + symhdr->isymMax * sizeof (struct sym_ext)
  2974.       + symhdr->ioptMax * sizeof (struct opt_ext)
  2975.       + symhdr->iauxMax * sizeof (union aux_ext)
  2976.       + symhdr->issMax * sizeof (char)
  2977.       + symhdr->issExtMax * sizeof (char)
  2978.       + symhdr->ifdMax * sizeof (struct fdr_ext)
  2979.       + symhdr->crfd * sizeof (struct rfd_ext)
  2980.       + symhdr->iextMax * sizeof (struct ext_ext));
  2981.   raw = (char *) bfd_alloc (abfd, size);
  2982.   if (raw == (char *) NULL)
  2983.     {
  2984.       bfd_error = no_memory;
  2985.       return false;
  2986.     }
  2987.   ecoff_data (abfd)->raw_size = size;
  2988.   ecoff_data (abfd)->raw_syments = (PTR) raw;
  2989.  
  2990.   /* Initialize the raw pointers.  */
  2991. #define SET(field, count, type) \
  2992.   ecoff_data (abfd)->field = (type *) raw; \
  2993.   raw += symhdr->count * sizeof (type)
  2994.  
  2995.   SET (line, cbLine, unsigned char);
  2996.   SET (external_dnr, idnMax, struct dnr_ext);
  2997.   SET (external_pdr, ipdMax, struct pdr_ext);
  2998.   SET (external_sym, isymMax, struct sym_ext);
  2999.   SET (external_opt, ioptMax, struct opt_ext);
  3000.   SET (external_aux, iauxMax, union aux_ext);
  3001.   SET (ss, issMax, char);
  3002.   SET (ssext, issExtMax, char);
  3003.   SET (external_fdr, ifdMax, struct fdr_ext);
  3004.   SET (external_rfd, crfd, struct rfd_ext);
  3005.   SET (external_ext, iextMax, struct ext_ext);
  3006. #undef SET
  3007.  
  3008.   /* Reset the counts so the second pass can use them to know how far
  3009.      it has gotten.  */
  3010.   symhdr->ilineMax = 0;
  3011.   symhdr->cbLine = 0;
  3012.   symhdr->idnMax = 0;
  3013.   symhdr->ipdMax = 0;
  3014.   symhdr->isymMax = 0;
  3015.   symhdr->ioptMax = 0;
  3016.   symhdr->iauxMax = 0;
  3017.   symhdr->issMax = 0;
  3018.   symhdr->issExtMax = 0;
  3019.   symhdr->ifdMax = 0;
  3020.   symhdr->crfd = 0;
  3021.   symhdr->iextMax = 0;
  3022.  
  3023.   /* Do the second pass: accumulate the debugging information.  */
  3024.   ecoff_clear_output_flags (abfd);
  3025.   for (ipass = 0; ipass < 2; ipass++)
  3026.     {
  3027.       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
  3028.     {
  3029.       if (((o->flags & SEC_CODE) == 0) != ipass)
  3030.         continue;
  3031.       for (p = o->seclets_head;
  3032.            p != (bfd_seclet_type *) NULL;
  3033.            p = p->next)
  3034.         {
  3035.           if (p->type == bfd_indirect_seclet)
  3036.         {
  3037.           if (ecoff_get_debug (abfd, p, o, relocateable) == false)
  3038.             return false;
  3039.         }
  3040.         }
  3041.     }
  3042.     }
  3043.  
  3044.   /* Put in the external symbols.  */
  3045.   sym_ptr_ptr = bfd_get_outsymbols (abfd);
  3046.   if (sym_ptr_ptr != NULL)
  3047.     {
  3048.       char *ssext;
  3049.       struct ext_ext *external_ext;
  3050.  
  3051.       ssext = ecoff_data (abfd)->ssext;
  3052.       external_ext = ecoff_data (abfd)->external_ext;
  3053.       for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
  3054.     {
  3055.       asymbol *sym_ptr;
  3056.       EXTR esym;
  3057.  
  3058.       sym_ptr = *sym_ptr_ptr;
  3059.  
  3060.       if ((sym_ptr->flags & BSF_DEBUGGING) != 0
  3061.           || (sym_ptr->flags & BSF_LOCAL) != 0)
  3062.         continue;
  3063.  
  3064.       /* The enative pointer can be NULL for a symbol created by
  3065.          the linker via ecoff_make_empty_symbol.  */
  3066.       if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
  3067.           || (((ecoff_symbol_type *) sym_ptr)->native.enative
  3068.           == (struct ext_ext *) NULL))
  3069.         {
  3070.           esym.jmptbl = 0;
  3071.           esym.cobol_main = 0;
  3072.           esym.weakext = 0;
  3073.           esym.reserved = 0;
  3074.           esym.ifd = ifdNil;
  3075.           /* FIXME: we can do better than this for st and sc.  */
  3076.           esym.asym.st = stGlobal;
  3077.           esym.asym.sc = scAbs;
  3078.           esym.asym.reserved = 0;
  3079.           esym.asym.index = indexNil;
  3080.         }
  3081.       else
  3082.         {
  3083.           ecoff_symbol_type *ecoff_sym_ptr;
  3084.  
  3085.           ecoff_sym_ptr = (ecoff_symbol_type *) sym_ptr;
  3086.           if (ecoff_sym_ptr->local)
  3087.         abort ();
  3088.           ecoff_swap_ext_in (abfd, ecoff_sym_ptr->native.enative, &esym);
  3089.  
  3090.           /* If we're producing an executable, move common symbols
  3091.          into bss.  */
  3092.           if (relocateable == false)
  3093.         {
  3094.           if (esym.asym.sc == scCommon)
  3095.             esym.asym.sc = scBss;
  3096.           else if (esym.asym.sc == scSCommon)
  3097.             esym.asym.sc = scSBss;
  3098.         }
  3099.  
  3100.           /* Adjust the FDR index for the symbol by that used for
  3101.          the input BFD.  */
  3102.           esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
  3103.         }
  3104.  
  3105.       esym.asym.iss = symhdr->issExtMax;
  3106.  
  3107.       if (bfd_is_com_section (sym_ptr->section)
  3108.           || sym_ptr->section == &bfd_und_section)
  3109.         esym.asym.value = sym_ptr->value;
  3110.       else
  3111.         esym.asym.value = (sym_ptr->value
  3112.                    + sym_ptr->section->output_offset
  3113.                    + sym_ptr->section->output_section->vma);
  3114.  
  3115.       ecoff_swap_ext_out (abfd, &esym, external_ext + symhdr->iextMax);
  3116.  
  3117.       ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
  3118.  
  3119.       ++symhdr->iextMax;
  3120.  
  3121.       strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
  3122.       symhdr->issExtMax += strlen (sym_ptr->name) + 1;
  3123.     }
  3124.     }
  3125.  
  3126.   /* Adjust the counts so that structures are longword aligned.  */
  3127.   symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
  3128.   symhdr->issMax = (symhdr->issMax + 3) &~ 3;
  3129.   symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
  3130.  
  3131.   return true;
  3132. }
  3133.  
  3134. /* Set the architecture.  The only architecture we support here is
  3135.    mips.  We set the architecture anyhow, since many callers ignore
  3136.    the return value.  */
  3137.  
  3138. static boolean
  3139. ecoff_set_arch_mach (abfd, arch, machine)
  3140.      bfd *abfd;
  3141.      enum bfd_architecture arch;
  3142.      unsigned long machine;
  3143. {
  3144.   bfd_default_set_arch_mach (abfd, arch, machine);
  3145.   return arch == bfd_arch_mips;
  3146. }
  3147.  
  3148. /* Get the size of the section headers.  We do not output the .scommon
  3149.    section which we created in ecoff_mkobject.  */
  3150.  
  3151. static int
  3152. ecoff_sizeof_headers (abfd, reloc)
  3153.      bfd *abfd;
  3154.      boolean reloc;
  3155. {
  3156.   return FILHSZ + AOUTSZ + (abfd->section_count - 1) * SCNHSZ;
  3157. }
  3158.  
  3159. /* Calculate the file position for each section, and set
  3160.    reloc_filepos.  */
  3161.  
  3162. static void
  3163. ecoff_compute_section_file_positions (abfd)
  3164.      bfd *abfd;
  3165. {
  3166.   asection *current;
  3167.   file_ptr sofar;
  3168.   file_ptr old_sofar;
  3169.   boolean first_data;
  3170.  
  3171.   if (bfd_get_start_address (abfd)) 
  3172.     abfd->flags |= EXEC_P;
  3173.  
  3174.   sofar = ecoff_sizeof_headers (abfd, false);
  3175.  
  3176.   first_data = true;
  3177.   for (current = abfd->sections;
  3178.        current != (asection *) NULL;
  3179.        current = current->next)
  3180.     {
  3181.       /* Only deal with sections which have contents */
  3182.       if (! (current->flags & SEC_HAS_CONTENTS)
  3183.       || strcmp (current->name, SCOMMON) == 0)
  3184.     continue;
  3185.  
  3186.       /* On Ultrix, the data sections in an executable file must be
  3187.      aligned to a page boundary within the file.  This does not
  3188.      affect the section size, though.  FIXME: Does this work for
  3189.      other platforms?  */
  3190.       if ((abfd->flags & EXEC_P) != 0
  3191.       && first_data != false
  3192.       && (current->flags & SEC_CODE) == 0)
  3193.     {
  3194.       sofar = (sofar + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3195.       first_data = false;
  3196.     }
  3197.  
  3198.       /* Align the sections in the file to the same boundary on
  3199.      which they are aligned in virtual memory.  */
  3200.       old_sofar = sofar;
  3201.       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3202.  
  3203.       current->filepos = sofar;
  3204.  
  3205.       sofar += current->_raw_size;
  3206.  
  3207.       /* make sure that this section is of the right size too */
  3208.       old_sofar = sofar;
  3209.       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
  3210.       current->_raw_size += sofar - old_sofar;
  3211.     }
  3212.  
  3213.   ecoff_data (abfd)->reloc_filepos = sofar;
  3214. }
  3215.  
  3216. /* Set the contents of a section.  */
  3217.  
  3218. static boolean
  3219. ecoff_set_section_contents (abfd, section, location, offset, count)
  3220.      bfd *abfd;
  3221.      asection *section;
  3222.      PTR location;
  3223.      file_ptr offset;
  3224.      bfd_size_type count;
  3225. {
  3226.   if (abfd->output_has_begun == false)
  3227.     ecoff_compute_section_file_positions (abfd);
  3228.  
  3229.   bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
  3230.  
  3231.   if (count != 0)
  3232.     return (bfd_write (location, 1, count, abfd) == count) ? true : false;
  3233.  
  3234.   return true;
  3235. }
  3236.  
  3237. /* Write out an ECOFF file.  */
  3238.  
  3239. static boolean
  3240. ecoff_write_object_contents (abfd)
  3241.      bfd *abfd;
  3242. {
  3243.   asection *current;
  3244.   unsigned int count;
  3245.   file_ptr scn_base;
  3246.   file_ptr reloc_base;
  3247.   file_ptr sym_base;
  3248.   unsigned long reloc_size;
  3249.   unsigned long text_size;
  3250.   unsigned long text_start;
  3251.   unsigned long data_size;
  3252.   unsigned long data_start;
  3253.   unsigned long bss_size;
  3254.   struct internal_filehdr internal_f;
  3255.   struct internal_aouthdr internal_a;
  3256.   int i;
  3257.  
  3258.   bfd_error = system_call_error;
  3259.  
  3260.   if(abfd->output_has_begun == false)
  3261.     ecoff_compute_section_file_positions(abfd);
  3262.  
  3263.   if (abfd->sections != (asection *) NULL)
  3264.     scn_base = abfd->sections->filepos;
  3265.   else
  3266.     scn_base = 0;
  3267.   reloc_base = ecoff_data (abfd)->reloc_filepos;
  3268.  
  3269.   count = 1;
  3270.   reloc_size = 0;
  3271.   for (current = abfd->sections;
  3272.        current != (asection *)NULL; 
  3273.        current = current->next) 
  3274.     {
  3275.       if (strcmp (current->name, SCOMMON) == 0)
  3276.     continue;
  3277.       current->target_index = count;
  3278.       ++count;
  3279.       if (current->reloc_count != 0)
  3280.     {
  3281.       bfd_size_type relsize;
  3282.  
  3283.       current->rel_filepos = reloc_base;
  3284.       relsize = current->reloc_count * RELSZ;
  3285.       reloc_size += relsize;
  3286.       reloc_base += relsize;
  3287.     }
  3288.       else
  3289.     current->rel_filepos = 0;
  3290.     }
  3291.  
  3292.   sym_base = reloc_base + reloc_size;
  3293.  
  3294.   /* At least on Ultrix, the symbol table of an executable file must
  3295.      be aligned to a page boundary.  FIXME: Is this true on other
  3296.      platforms?  */
  3297.   if ((abfd->flags & EXEC_P) != 0)
  3298.     sym_base = (sym_base + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3299.  
  3300.   ecoff_data (abfd)->sym_filepos = sym_base;
  3301.  
  3302.   text_size = ecoff_sizeof_headers (abfd, false);
  3303.   text_start = 0;
  3304.   data_size = 0;
  3305.   data_start = 0;
  3306.   bss_size = 0;
  3307.  
  3308.   /* Write section headers to the file.  */
  3309.  
  3310.   internal_f.f_nscns = 0;
  3311.   if (bfd_seek (abfd, (file_ptr) (FILHSZ + AOUTSZ), SEEK_SET) != 0)
  3312.     return false;
  3313.   for (current = abfd->sections;
  3314.        current != (asection *) NULL;
  3315.        current = current->next)
  3316.     {
  3317.       struct internal_scnhdr section;
  3318.       bfd_vma vma;
  3319.  
  3320.       if (strcmp (current->name, SCOMMON) == 0)
  3321.     {
  3322.       BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
  3323.               && current->reloc_count == 0);
  3324.       continue;
  3325.     }
  3326.  
  3327.       ++internal_f.f_nscns;
  3328.  
  3329.       strncpy (section.s_name, current->name, sizeof section.s_name);
  3330.  
  3331.       /* FIXME: is this correct for shared libraries?  I think it is
  3332.      but I have no platform to check.  Ian Lance Taylor.  */
  3333.       vma = bfd_get_section_vma (abfd, current);
  3334.       if (strcmp (current->name, _LIB) == 0)
  3335.     section.s_vaddr = 0;
  3336.       else
  3337.     section.s_vaddr = vma;
  3338.  
  3339.       section.s_paddr = vma;
  3340.       section.s_size = bfd_get_section_size_before_reloc (current);
  3341.  
  3342.       /* If this section has no size or is unloadable then the scnptr
  3343.      will be 0 too.  */
  3344.       if (current->_raw_size == 0
  3345.       || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
  3346.     section.s_scnptr = 0;
  3347.       else
  3348.     section.s_scnptr = current->filepos;
  3349.       section.s_relptr = current->rel_filepos;
  3350.  
  3351.       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
  3352.      object file produced by the assembler is supposed to point to
  3353.      information about how much room is required by objects of
  3354.      various different sizes.  I think this only matters if we
  3355.      want the linker to compute the best size to use, or
  3356.      something.  I don't know what happens if the information is
  3357.      not present.  */
  3358.       section.s_lnnoptr = 0;
  3359.  
  3360.       section.s_nreloc = current->reloc_count;
  3361.       section.s_nlnno = 0;
  3362.       section.s_flags = ecoff_sec_to_styp_flags (current->name,
  3363.                          current->flags);
  3364.  
  3365.       {
  3366.     SCNHDR buff;
  3367.  
  3368.     ecoff_swap_scnhdr_out (abfd, (PTR) §ion, (PTR) &buff);
  3369.     if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
  3370.       return false;
  3371.       }
  3372.  
  3373.       if ((section.s_flags & STYP_TEXT) != 0)
  3374.     {
  3375.       text_size += bfd_get_section_size_before_reloc (current);
  3376.       if (text_start == 0 || text_start > vma)
  3377.         text_start = vma;
  3378.     }
  3379.       else if ((section.s_flags & STYP_RDATA) != 0
  3380.            || (section.s_flags & STYP_DATA) != 0
  3381.            || (section.s_flags & STYP_LIT8) != 0
  3382.            || (section.s_flags & STYP_LIT4) != 0
  3383.            || (section.s_flags & STYP_SDATA) != 0)
  3384.     {
  3385.       data_size += bfd_get_section_size_before_reloc (current);
  3386.       if (data_start == 0 || data_start > vma)
  3387.         data_start = vma;
  3388.     }
  3389.       else if ((section.s_flags & STYP_BSS) != 0
  3390.            || (section.s_flags & STYP_SBSS) != 0)
  3391.     bss_size += bfd_get_section_size_before_reloc (current);
  3392.     }    
  3393.  
  3394.   /* Set up the file header.  */
  3395.  
  3396.   if (abfd->xvec->header_byteorder_big_p != false)
  3397.     internal_f.f_magic = MIPS_MAGIC_BIG;
  3398.   else
  3399.     internal_f.f_magic = MIPS_MAGIC_LITTLE;
  3400.  
  3401.   /*
  3402.     We will NOT put a fucking timestamp in the header here. Every time you
  3403.     put it back, I will come in and take it out again.  I'm sorry.  This
  3404.     field does not belong here.  We fill it with a 0 so it compares the
  3405.     same but is not a reasonable time. -- gnu@cygnus.com
  3406.     */
  3407.   internal_f.f_timdat = 0;
  3408.  
  3409.   if (bfd_get_symcount (abfd) != 0)
  3410.     {
  3411.       /* The ECOFF f_nsyms field is not actually the number of
  3412.      symbols, it's the size of symbolic information header.  */
  3413.       internal_f.f_nsyms = sizeof (struct hdr_ext);
  3414.       internal_f.f_symptr = sym_base;
  3415.     }
  3416.   else
  3417.     {
  3418.       internal_f.f_nsyms = 0;
  3419.       internal_f.f_symptr = 0;
  3420.     }
  3421.  
  3422.   internal_f.f_opthdr = AOUTSZ;
  3423.  
  3424.   internal_f.f_flags = F_LNNO;
  3425.   if (reloc_size == 0)
  3426.     internal_f.f_flags |= F_RELFLG;
  3427.   if (bfd_get_symcount (abfd) == 0)
  3428.     internal_f.f_flags |= F_LSYMS;
  3429.   if (abfd->flags & EXEC_P)
  3430.     internal_f.f_flags |= F_EXEC;
  3431.  
  3432.   if (! abfd->xvec->byteorder_big_p)
  3433.     internal_f.f_flags |= F_AR32WR;
  3434.   else
  3435.     internal_f.f_flags |= F_AR32W;
  3436.  
  3437.   /* Set up the ``optional'' header.  */
  3438.   internal_a.magic = ZMAGIC;
  3439.  
  3440.   /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
  3441.      linker happy.  But, is it right?  */
  3442.   internal_a.vstamp = 0x20a;
  3443.  
  3444.   /* At least on Ultrix, these have to be rounded to page boundaries.
  3445.      FIXME: Is this true on other platforms?  */
  3446.   internal_a.tsize = (text_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3447.   internal_a.text_start = text_start &~ (ROUND_SIZE - 1);
  3448.   internal_a.dsize = (data_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
  3449.   internal_a.data_start = data_start &~ (ROUND_SIZE - 1);
  3450.  
  3451.   /* On Ultrix, the initial portions of the .sbss and .bss segments
  3452.      are at the end of the data section.  The bsize field in the
  3453.      optional header records how many bss bytes are required beyond
  3454.      those in the data section.  The value is not rounded to a page
  3455.      boundary.  */
  3456.   if (bss_size < internal_a.dsize - data_size)
  3457.     bss_size = 0;
  3458.   else
  3459.     bss_size -= internal_a.dsize - data_size;
  3460.   internal_a.bsize = bss_size;
  3461.   internal_a.bss_start = internal_a.data_start + internal_a.dsize;
  3462.  
  3463.   internal_a.entry = bfd_get_start_address (abfd);
  3464.  
  3465.   internal_a.gp_value = ecoff_data (abfd)->gp;
  3466.  
  3467.   internal_a.gprmask = ecoff_data (abfd)->gprmask;
  3468.   for (i = 0; i < 4; i++)
  3469.     internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
  3470.  
  3471.   /* Write out the file header and the optional header.  */
  3472.  
  3473.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  3474.     return false;
  3475.  
  3476.   {
  3477.     FILHDR buff;
  3478.     ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
  3479.     if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
  3480.       return false;
  3481.   }
  3482.  
  3483.   {
  3484.     AOUTHDR buff;
  3485.  
  3486.     ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
  3487.     if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
  3488.       return false;
  3489.   }
  3490.  
  3491.   /* Write out the relocs.  */
  3492.   for (current = abfd->sections;
  3493.        current != (asection *) NULL;
  3494.        current = current->next)
  3495.     {
  3496.       RELOC *buff;
  3497.       arelent **reloc_ptr_ptr;
  3498.       arelent **reloc_end;
  3499.       RELOC *out_ptr;
  3500.  
  3501.       if (current->reloc_count == 0)
  3502.     continue;
  3503.  
  3504.       buff = (RELOC *) bfd_alloc (abfd, current->reloc_count * RELSZ);
  3505.       if (buff == (RELOC *) NULL)
  3506.     {
  3507.       bfd_error = no_memory;
  3508.       return false;
  3509.     }
  3510.  
  3511.       reloc_ptr_ptr = current->orelocation;
  3512.       reloc_end = reloc_ptr_ptr + current->reloc_count;
  3513.       out_ptr = buff;
  3514.       for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr++)
  3515.     {
  3516.       arelent *reloc;
  3517.       asymbol *sym;
  3518.       struct internal_reloc in;
  3519.       
  3520.       memset (&in, 0, sizeof in);
  3521.  
  3522.       reloc = *reloc_ptr_ptr;
  3523.       sym = *reloc->sym_ptr_ptr;
  3524.  
  3525.       /* This must be an ECOFF reloc.  */
  3526.       BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
  3527.               && reloc->howto >= ecoff_howto_table
  3528.               && (reloc->howto
  3529.               < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
  3530.  
  3531.       in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
  3532.       in.r_type = reloc->howto->type;
  3533.  
  3534.       /* If this is a REFHI reloc, the next one must be a REFLO
  3535.          reloc for the same symbol.  */
  3536.       BFD_ASSERT (in.r_type != ECOFF_R_REFHI
  3537.               || (reloc_ptr_ptr < reloc_end
  3538.               && (reloc_ptr_ptr[1]->howto
  3539.                   != (reloc_howto_type *) NULL)
  3540.               && (reloc_ptr_ptr[1]->howto->type
  3541.                   == ECOFF_R_REFLO)
  3542.               && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
  3543.  
  3544.       if ((sym->flags & BSF_SECTION_SYM) == 0)
  3545.         {
  3546.           in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
  3547.           in.r_extern = 1;
  3548.         }
  3549.       else
  3550.         {
  3551.           CONST char *name;
  3552.  
  3553.           name = bfd_get_section_name (abfd, bfd_get_section (sym));
  3554.           if (strcmp (name, ".text") == 0)
  3555.         in.r_symndx = RELOC_SECTION_TEXT;
  3556.           else if (strcmp (name, ".rdata") == 0)
  3557.         in.r_symndx = RELOC_SECTION_RDATA;
  3558.           else if (strcmp (name, ".data") == 0)
  3559.         in.r_symndx = RELOC_SECTION_DATA;
  3560.           else if (strcmp (name, ".sdata") == 0)
  3561.         in.r_symndx = RELOC_SECTION_SDATA;
  3562.           else if (strcmp (name, ".sbss") == 0)
  3563.         in.r_symndx = RELOC_SECTION_SBSS;
  3564.           else if (strcmp (name, ".bss") == 0)
  3565.         in.r_symndx = RELOC_SECTION_BSS;
  3566.           else if (strcmp (name, ".init") == 0)
  3567.         in.r_symndx = RELOC_SECTION_INIT;
  3568.           else if (strcmp (name, ".lit8") == 0)
  3569.         in.r_symndx = RELOC_SECTION_LIT8;
  3570.           else if (strcmp (name, ".lit4") == 0)
  3571.         in.r_symndx = RELOC_SECTION_LIT4;
  3572.           else
  3573.         abort ();
  3574.           in.r_extern = 0;
  3575.         }
  3576.  
  3577.       ecoff_swap_reloc_out (abfd, (PTR) &in, (PTR) out_ptr);
  3578.     }
  3579.  
  3580.       if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
  3581.     return false;
  3582.       if (bfd_write ((PTR) buff, RELSZ, current->reloc_count, abfd)
  3583.       != RELSZ * current->reloc_count)
  3584.     return false;
  3585.       bfd_release (abfd, (PTR) buff);
  3586.     }
  3587.  
  3588.   /* Write out the symbolic debugging information.  */
  3589.   if (bfd_get_symcount (abfd) > 0)
  3590.     {
  3591.       HDRR *symhdr;
  3592.       unsigned long sym_offset;
  3593.       struct hdr_ext buff;
  3594.  
  3595.       /* Set up the offsets in the symbolic header.  */
  3596.       symhdr = &ecoff_data (abfd)->symbolic_header;
  3597.       sym_offset = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
  3598.  
  3599. #define SET(offset, size, ptr) \
  3600.   if (symhdr->size == 0) \
  3601.     symhdr->offset = 0; \
  3602.   else \
  3603.     symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
  3604.                - (char *) ecoff_data (abfd)->raw_syments) \
  3605.               + sym_offset);
  3606.  
  3607.       SET (cbLineOffset, cbLine, line);
  3608.       SET (cbDnOffset, idnMax, external_dnr);
  3609.       SET (cbPdOffset, ipdMax, external_pdr);
  3610.       SET (cbSymOffset, isymMax, external_sym);
  3611.       SET (cbOptOffset, ioptMax, external_opt);
  3612.       SET (cbAuxOffset, iauxMax, external_aux);
  3613.       SET (cbSsOffset, issMax, ss);
  3614.       SET (cbSsExtOffset, issExtMax, ssext);
  3615.       SET (cbFdOffset, ifdMax, external_fdr);
  3616.       SET (cbRfdOffset, crfd, external_rfd);
  3617.       SET (cbExtOffset, iextMax, external_ext);
  3618. #undef SET
  3619.  
  3620.       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
  3621.             SEEK_SET) != 0)
  3622.     return false;
  3623.       ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
  3624.       if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
  3625.     return false;
  3626.       if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
  3627.              ecoff_data (abfd)->raw_size, abfd)
  3628.       != ecoff_data (abfd)->raw_size)
  3629.     return false;
  3630.     }
  3631.  
  3632.   return true;
  3633. }
  3634.  
  3635. /* Archive handling.  ECOFF uses what appears to be a unique type of
  3636.    archive header (which I call an armap).  The byte ordering of the
  3637.    armap and the contents are encoded in the name of the armap itself.
  3638.    At least for now, we only support archives with the same byte
  3639.    ordering in the armap and the contents.
  3640.  
  3641.    The first four bytes in the armap are the number of symbol
  3642.    definitions.  This is always a power of two.
  3643.  
  3644.    This is followed by the symbol definitions.  Each symbol definition
  3645.    occupies 8 bytes.  The first four bytes are the offset from the
  3646.    start of the armap strings to the null-terminated string naming
  3647.    this symbol.  The second four bytes are the file offset to the
  3648.    archive member which defines this symbol.  If the second four bytes
  3649.    are 0, then this is not actually a symbol definition, and it should
  3650.    be ignored.
  3651.  
  3652.    The symbols are hashed into the armap with a closed hashing scheme.
  3653.    See the functions below for the details of the algorithm.
  3654.  
  3655.    We could use the hash table when looking up symbols in a library.
  3656.    This would require a new BFD target entry point to replace the
  3657.    bfd_get_next_mapent function used by the linker.
  3658.  
  3659.    After the symbol definitions comes four bytes holding the size of
  3660.    the string table, followed by the string table itself.  */
  3661.  
  3662. /* The name of an archive headers looks like this:
  3663.    __________E[BL]E[BL]_ (with a trailing space).
  3664.    The trailing space is changed to an X if the archive is changed to
  3665.    indicate that the armap is out of date.  */
  3666.  
  3667. #define ARMAP_BIG_ENDIAN 'B'
  3668. #define ARMAP_LITTLE_ENDIAN 'L'
  3669. #define ARMAP_MARKER 'E'
  3670. #define ARMAP_START "__________"
  3671. #define ARMAP_HEADER_MARKER_INDEX 10
  3672. #define ARMAP_HEADER_ENDIAN_INDEX 11
  3673. #define ARMAP_OBJECT_MARKER_INDEX 12
  3674. #define ARMAP_OBJECT_ENDIAN_INDEX 13
  3675. #define ARMAP_END_INDEX 14
  3676. #define ARMAP_END "_ "
  3677.  
  3678. /* This is a magic number used in the hashing algorithm.  */
  3679. #define ARMAP_HASH_MAGIC 0x9dd68ab5
  3680.  
  3681. /* This returns the hash value to use for a string.  It also sets
  3682.    *REHASH to the rehash adjustment if the first slot is taken.  SIZE
  3683.    is the number of entries in the hash table, and HLOG is the log
  3684.    base 2 of SIZE.  */
  3685.  
  3686. static unsigned int
  3687. ecoff_armap_hash (s, rehash, size, hlog)
  3688.      CONST char *s;
  3689.      unsigned int *rehash;
  3690.      unsigned int size;
  3691.      unsigned int hlog;
  3692. {
  3693.   unsigned int hash;
  3694.  
  3695.   hash = *s++;
  3696.   while (*s != '\0')
  3697.     hash = ((hash >> 27) | (hash << 5)) + *s++;
  3698.   hash *= ARMAP_HASH_MAGIC;
  3699.   *rehash = (hash & (size - 1)) | 1;
  3700.   return hash >> (32 - hlog);
  3701. }
  3702.  
  3703. /* Read in the armap.  */
  3704.  
  3705. static boolean
  3706. ecoff_slurp_armap (abfd)
  3707.      bfd *abfd;
  3708. {
  3709.   char nextname[17];
  3710.   unsigned int i;
  3711.   struct areltdata *mapdata;
  3712.   bfd_size_type parsed_size;
  3713.   char *raw_armap;
  3714.   struct artdata *ardata;
  3715.   unsigned int count;
  3716.   char *raw_ptr;
  3717.   struct symdef *symdef_ptr;
  3718.   char *stringbase;
  3719.   
  3720.   /* Get the name of the first element.  */
  3721.   i = bfd_read ((PTR) nextname, 1, 16, abfd);
  3722.   if (i == 0)
  3723.       return true;
  3724.   if (i != 16)
  3725.       return false;
  3726.  
  3727.   bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
  3728.  
  3729.   /* See if the first element is an armap.  */
  3730.   if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
  3731.       || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
  3732.       || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  3733.       && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  3734.       || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
  3735.       || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
  3736.       && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
  3737.       || strncmp (nextname + ARMAP_END_INDEX,
  3738.           ARMAP_END, sizeof ARMAP_END - 1) != 0)
  3739.     {
  3740.       bfd_has_map (abfd) = false;
  3741.       return true;
  3742.     }
  3743.  
  3744.   /* Make sure we have the right byte ordering.  */
  3745.   if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  3746.        ^ (abfd->xvec->header_byteorder_big_p != false))
  3747.       || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
  3748.       ^ (abfd->xvec->byteorder_big_p != false)))
  3749.     {
  3750.       bfd_error = wrong_format;
  3751.       return false;
  3752.     }
  3753.  
  3754.   /* Read in the armap.  */
  3755.   ardata = bfd_ardata (abfd);
  3756.   mapdata = snarf_ar_hdr (abfd);
  3757.   if (mapdata == (struct areltdata *) NULL)
  3758.     return false;
  3759.   parsed_size = mapdata->parsed_size;
  3760.   bfd_release (abfd, (PTR) mapdata);
  3761.     
  3762.   raw_armap = (char *) bfd_alloc (abfd, parsed_size);
  3763.   if (raw_armap == (char *) NULL)
  3764.     {
  3765.       bfd_error = no_memory;
  3766.       return false;
  3767.     }
  3768.     
  3769.   if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
  3770.     {
  3771.       bfd_error = malformed_archive;
  3772.       bfd_release (abfd, (PTR) raw_armap);
  3773.       return false;
  3774.     }
  3775.     
  3776.   count = bfd_h_get_32 (abfd, (PTR) raw_armap);
  3777.  
  3778.   ardata->symdef_count = 0;
  3779.   ardata->cache = (struct ar_cache *) NULL;
  3780.  
  3781.   /* Hack: overlay the symdefs on top of the raw archive data.  This
  3782.      is the way do_slurp_bsd_armap works.  */
  3783.   raw_ptr = raw_armap + LONG_SIZE;
  3784.   symdef_ptr = (struct symdef *) raw_ptr;
  3785.   ardata->symdefs = (carsym *) symdef_ptr;
  3786.   stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
  3787.  
  3788. #ifdef CHECK_ARMAP_HASH
  3789.   {
  3790.     unsigned int hlog;
  3791.  
  3792.     /* Double check that I have the hashing algorithm right by making
  3793.        sure that every symbol can be looked up successfully.  */
  3794.     hlog = 0;
  3795.     for (i = 1; i < count; i <<= 1)
  3796.       hlog++;
  3797.     BFD_ASSERT (i == count);
  3798.  
  3799.     for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
  3800.       {
  3801.     unsigned int name_offset, file_offset;
  3802.     unsigned int hash, rehash, srch;
  3803.       
  3804.     name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
  3805.     file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
  3806.     if (file_offset == 0)
  3807.       continue;
  3808.     hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
  3809.                  hlog);
  3810.     if (hash == i)
  3811.       continue;
  3812.  
  3813.     /* See if we can rehash to this location.  */
  3814.     for (srch = (hash + rehash) & (count - 1);
  3815.          srch != hash && srch != i;
  3816.          srch = (srch + rehash) & (count - 1))
  3817.       BFD_ASSERT (bfd_h_get_32 (abfd,
  3818.                     (PTR) (raw_armap
  3819.                        + LONG_SIZE
  3820.                        + (srch * 2 * LONG_SIZE)
  3821.                        + LONG_SIZE))
  3822.               != 0);
  3823.     BFD_ASSERT (srch == i);
  3824.       }
  3825.   }
  3826.  
  3827.   raw_ptr = raw_armap + LONG_SIZE;
  3828. #endif /* CHECK_ARMAP_HASH */
  3829.  
  3830.   for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
  3831.     {
  3832.       unsigned int name_offset, file_offset;
  3833.  
  3834.       name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
  3835.       file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
  3836.       if (file_offset == 0)
  3837.     continue;
  3838.       symdef_ptr->s.name = stringbase + name_offset;
  3839.       symdef_ptr->file_offset = file_offset;
  3840.       ++symdef_ptr;
  3841.       ++ardata->symdef_count;
  3842.     }
  3843.  
  3844.   ardata->first_file_filepos = bfd_tell (abfd);
  3845.   /* Pad to an even boundary.  */
  3846.   ardata->first_file_filepos += ardata->first_file_filepos % 2;
  3847.  
  3848.   bfd_has_map (abfd) = true;
  3849.  
  3850.   return true;
  3851. }
  3852.  
  3853. /* Write out an armap.  */
  3854.  
  3855. static boolean
  3856. ecoff_write_armap (abfd, elength, map, orl_count, stridx)
  3857.      bfd *abfd;
  3858.      unsigned int elength;
  3859.      struct orl *map;
  3860.      unsigned int orl_count;
  3861.      int stridx;
  3862. {
  3863.   unsigned int hashsize, hashlog;
  3864.   unsigned int symdefsize;
  3865.   int padit;
  3866.   unsigned int stringsize;
  3867.   unsigned int mapsize;
  3868.   file_ptr firstreal;
  3869.   struct ar_hdr hdr;
  3870.   struct stat statbuf;
  3871.   unsigned int i;
  3872.   bfd_byte temp[LONG_SIZE];
  3873.   bfd_byte *hashtable;
  3874.   bfd *current;
  3875.   bfd *last_elt;
  3876.  
  3877.   /* Ultrix appears to use as a hash table size the least power of two
  3878.      greater than twice the number of entries.  */
  3879.   for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
  3880.     ;
  3881.   hashsize = 1 << hashlog;
  3882.  
  3883.   symdefsize = hashsize * 2 * LONG_SIZE;
  3884.   padit = stridx % 2;
  3885.   stringsize = stridx + padit;
  3886.  
  3887.   /* Include 8 bytes to store symdefsize and stringsize in output. */
  3888.   mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
  3889.  
  3890.   firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
  3891.  
  3892.   memset ((PTR) &hdr, 0, sizeof hdr);
  3893.  
  3894.   /* Work out the ECOFF armap name.  */
  3895.   strcpy (hdr.ar_name, ARMAP_START);
  3896.   hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
  3897.   hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
  3898.     (abfd->xvec->header_byteorder_big_p
  3899.      ? ARMAP_BIG_ENDIAN
  3900.      : ARMAP_LITTLE_ENDIAN);
  3901.   hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
  3902.   hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
  3903.     abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
  3904.   memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
  3905.  
  3906.   /* Write the timestamp of the archive header to be just a little bit
  3907.      later than the timestamp of the file, otherwise the linker will
  3908.      complain that the index is out of date.  Actually, the Ultrix
  3909.      linker just checks the archive name; the GNU linker may check the
  3910.      date.  */
  3911.   stat (abfd->filename, &statbuf);
  3912.   sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
  3913.  
  3914.   /* The DECstation uses zeroes for the uid, gid and mode of the
  3915.      armap.  */
  3916.   hdr.ar_uid[0] = '0';
  3917.   hdr.ar_gid[0] = '0';
  3918.   hdr.ar_mode[0] = '0';
  3919.  
  3920.   sprintf (hdr.ar_size, "%-10d", (int) mapsize);
  3921.  
  3922.   hdr.ar_fmag[0] = '`';
  3923.   hdr.ar_fmag[1] = '\n';
  3924.  
  3925.   /* Turn all null bytes in the header into spaces.  */
  3926.   for (i = 0; i < sizeof (struct ar_hdr); i++)
  3927.    if (((char *)(&hdr))[i] == '\0')
  3928.      (((char *)(&hdr))[i]) = ' ';
  3929.  
  3930.   if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
  3931.       != sizeof (struct ar_hdr))
  3932.     return false;
  3933.  
  3934.   bfd_h_put_32 (abfd, hashsize, temp);
  3935.   if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
  3936.     return false;
  3937.   
  3938.   hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
  3939.  
  3940.   current = abfd->archive_head;
  3941.   last_elt = current;
  3942.   for (i = 0; i < orl_count; i++)
  3943.     {
  3944.       unsigned int hash, rehash;
  3945.  
  3946.       /* Advance firstreal to the file position of this archive
  3947.      element.  */
  3948.       if (((bfd *) map[i].pos) != last_elt)
  3949.     {
  3950.       do
  3951.         {
  3952.           firstreal += arelt_size (current) + sizeof (struct ar_hdr);
  3953.           firstreal += firstreal % 2;
  3954.           current = current->next;
  3955.         }
  3956.       while (current != (bfd *) map[i].pos);
  3957.     }
  3958.  
  3959.       last_elt = current;
  3960.  
  3961.       hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
  3962.       if (bfd_h_get_32 (abfd, (PTR) (hashtable
  3963.                      + (hash * 2 * LONG_SIZE)
  3964.                      + LONG_SIZE))
  3965.       != 0)
  3966.     {
  3967.       unsigned int srch;
  3968.  
  3969.       /* The desired slot is already taken.  */
  3970.       for (srch = (hash + rehash) & (hashsize - 1);
  3971.            srch != hash;
  3972.            srch = (srch + rehash) & (hashsize - 1))
  3973.         if (bfd_h_get_32 (abfd, (PTR) (hashtable
  3974.                        + (srch * 2 * LONG_SIZE)
  3975.                        + LONG_SIZE))
  3976.         == 0)
  3977.           break;
  3978.  
  3979.       BFD_ASSERT (srch != hash);
  3980.  
  3981.       hash = srch;
  3982.     }
  3983.     
  3984.       bfd_h_put_32 (abfd, map[i].namidx,
  3985.             (PTR) (hashtable + hash * 2 * LONG_SIZE));
  3986.       bfd_h_put_32 (abfd, firstreal,
  3987.             (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
  3988.     }
  3989.  
  3990.   if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
  3991.     return false;
  3992.  
  3993.   bfd_release (abfd, hashtable);
  3994.  
  3995.   /* Now write the strings.  */
  3996.   bfd_h_put_32 (abfd, stringsize, temp);
  3997.   if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
  3998.     return false;
  3999.   for (i = 0; i < orl_count; i++)
  4000.     {
  4001.       bfd_size_type len;
  4002.  
  4003.       len = strlen (*map[i].name) + 1;
  4004.       if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
  4005.     return false;
  4006.     }
  4007.  
  4008.   /* The spec sez this should be a newline.  But in order to be
  4009.      bug-compatible for DECstation ar we use a null.  */
  4010.   if (padit)
  4011.     {
  4012.       if (bfd_write ("\0", 1, 1, abfd) != 1)
  4013.     return false;
  4014.     }
  4015.  
  4016.   return true;
  4017. }
  4018.  
  4019. /* We just use the generic extended name support.  This is a GNU
  4020.    extension.  */
  4021. #define ecoff_slurp_extended_name_table    _bfd_slurp_extended_name_table
  4022.  
  4023. /* See whether this BFD is an archive.  If it is, read in the armap
  4024.    and the extended name table.  */
  4025.  
  4026. static bfd_target *
  4027. ecoff_archive_p (abfd)
  4028.      bfd *abfd;
  4029. {
  4030.   char armag[SARMAG + 1];
  4031.  
  4032.   if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
  4033.       || strncmp (armag, ARMAG, SARMAG) != 0)
  4034.     {
  4035.       bfd_error = wrong_format;
  4036.       return (bfd_target *) NULL;
  4037.     }
  4038.  
  4039.   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
  4040.      involves a cast, we can't do it as the left operand of
  4041.      assignment.  */
  4042.   abfd->tdata.aout_ar_data =
  4043.     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
  4044.  
  4045.   if (bfd_ardata (abfd) == (struct artdata *) NULL)
  4046.     {
  4047.       bfd_error = no_memory;
  4048.       return (bfd_target *) NULL;
  4049.     }
  4050.  
  4051.   bfd_ardata (abfd)->first_file_filepos = SARMAG;
  4052.   
  4053.   if (ecoff_slurp_armap (abfd) == false
  4054.       || ecoff_slurp_extended_name_table (abfd) == false)
  4055.     {
  4056.       bfd_release (abfd, bfd_ardata (abfd));
  4057.       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
  4058.       return (bfd_target *) NULL;
  4059.     }
  4060.   
  4061.   return abfd->xvec;
  4062. }
  4063.  
  4064. #ifdef HOST_IRIX4
  4065.  
  4066. #include <core.out.h>
  4067.  
  4068. struct sgi_core_struct 
  4069. {
  4070.   int sig;
  4071.   char cmd[CORE_NAMESIZE];
  4072. };
  4073.  
  4074. #define core_hdr(bfd) ((bfd)->tdata.sgi_core_data)
  4075. #define core_signal(bfd) (core_hdr(bfd)->sig)
  4076. #define core_command(bfd) (core_hdr(bfd)->cmd)
  4077.  
  4078. static asection *
  4079. make_bfd_asection (abfd, name, flags, _raw_size, vma, filepos)
  4080.      bfd *abfd;
  4081.      CONST char *name;
  4082.      flagword flags;
  4083.      bfd_size_type _raw_size;
  4084.      bfd_vma vma;
  4085.      file_ptr filepos;
  4086. {
  4087.   asection *asect;
  4088.  
  4089.   asect = bfd_make_section (abfd, name);
  4090.   if (!asect)
  4091.     return NULL;
  4092.  
  4093.   asect->flags = flags;
  4094.   asect->_raw_size = _raw_size;
  4095.   asect->vma = vma;
  4096.   asect->filepos = filepos;
  4097.   asect->alignment_power = 2;;
  4098.  
  4099.   return asect;
  4100. }
  4101.  
  4102. static bfd_target *
  4103. ecoff_core_file_p (abfd)
  4104.      bfd *abfd;
  4105. {
  4106.   int val;
  4107.   int i;
  4108.   char *secname;
  4109.   struct coreout coreout;
  4110.   struct idesc *idg, *idf, *ids;
  4111.  
  4112.   val = bfd_read ((PTR)&coreout, 1, sizeof coreout, abfd);
  4113.   if (val != sizeof coreout)
  4114.     return 0;
  4115.  
  4116.   if (coreout.c_magic != CORE_MAGIC
  4117.       || coreout.c_version != CORE_VERSION1)
  4118.     return 0;
  4119.  
  4120.   core_hdr (abfd) = (struct sgi_core_struct *) bfd_zalloc (abfd, sizeof (struct sgi_core_struct));
  4121.   if (!core_hdr (abfd))
  4122.     return NULL;
  4123.  
  4124.   strncpy (core_command (abfd), coreout.c_name, CORE_NAMESIZE);
  4125.   core_signal (abfd) = coreout.c_sigcause;
  4126.  
  4127.   bfd_seek (abfd, coreout.c_vmapoffset, SEEK_SET);
  4128.  
  4129.   for (i = 0; i < coreout.c_nvmap; i++)
  4130.     {
  4131.       struct vmap vmap;
  4132.  
  4133.       val = bfd_read ((PTR)&vmap, 1, sizeof vmap, abfd);
  4134.       if (val != sizeof vmap)
  4135.     break;
  4136.  
  4137.       switch (vmap.v_type)
  4138.     {
  4139.     case VDATA:
  4140.       secname = ".data";
  4141.       break;
  4142.     case VSTACK:
  4143.       secname = ".stack";
  4144.       break;
  4145.     default:
  4146.       continue;
  4147.     }
  4148.  
  4149.       if (!make_bfd_asection (abfd, secname,
  4150.                   SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS,
  4151.                   vmap.v_len,
  4152.                   vmap.v_vaddr,
  4153.                   vmap.v_offset,
  4154.                   2))
  4155.     return NULL;
  4156.     }
  4157.  
  4158.   /* Make sure that the regs are contiguous within the core file. */
  4159.  
  4160.   idg = &coreout.c_idesc[I_GPREGS];
  4161.   idf = &coreout.c_idesc[I_FPREGS];
  4162.   ids = &coreout.c_idesc[I_SPECREGS];
  4163.  
  4164.   if (idg->i_offset + idg->i_len != idf->i_offset
  4165.       || idf->i_offset + idf->i_len != ids->i_offset)
  4166.     return 0;            /* Can't deal with non-contig regs */
  4167.  
  4168.   bfd_seek (abfd, idg->i_offset, SEEK_SET);
  4169.  
  4170.   make_bfd_asection (abfd, ".reg",
  4171.              SEC_ALLOC+SEC_HAS_CONTENTS,
  4172.              idg->i_len + idf->i_len + ids->i_len,
  4173.              0,
  4174.              idg->i_offset);
  4175.  
  4176.   /* OK, we believe you.  You're a core file (sure, sure).  */
  4177.  
  4178.   return abfd->xvec;
  4179. }
  4180.  
  4181. static char *
  4182. ecoff_core_file_failing_command (abfd)
  4183.      bfd *abfd;
  4184. {
  4185.   return core_command (abfd);
  4186. }
  4187.  
  4188. static int
  4189. ecoff_core_file_failing_signal (abfd)
  4190.      bfd *abfd;
  4191. {
  4192.   return core_signal (abfd);
  4193. }
  4194.  
  4195. static boolean
  4196. ecoff_core_file_matches_executable_p (core_bfd, exec_bfd)
  4197.      bfd *core_bfd, *exec_bfd;
  4198. {
  4199.   return true;            /* XXX - FIXME */
  4200. }
  4201. #else /* not def HOST_IRIX4 */
  4202. #define ecoff_core_file_p _bfd_dummy_target
  4203. #define ecoff_core_file_failing_command    _bfd_dummy_core_file_failing_command
  4204. #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
  4205. #define ecoff_core_file_matches_executable_p \
  4206.   _bfd_dummy_core_file_matches_executable_p
  4207. #endif
  4208.  
  4209. /* This is the COFF backend structure.  The backend_data field of the
  4210.    bfd_target structure is set to this.  The section reading code in
  4211.    coffgen.c uses this structure.  */
  4212.  
  4213. static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
  4214.   (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
  4215.   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
  4216.   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
  4217.   (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
  4218.   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
  4219.   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
  4220.   ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
  4221.   ecoff_swap_scnhdr_out,
  4222.   FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
  4223.   ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
  4224.   ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
  4225.   ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
  4226.   ecoff_slurp_symbol_table
  4227. };
  4228.  
  4229. /* get_lineno could be written for ECOFF, but it would currently only
  4230.    be useful for linking ECOFF and COFF files together, which doesn't
  4231.    seem too likely.  */
  4232. #define ecoff_get_lineno \
  4233.   ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
  4234.  
  4235. /* These bfd_target functions are defined in other files.  */
  4236.  
  4237. #define ecoff_truncate_arname        bfd_dont_truncate_arname
  4238. #define ecoff_openr_next_archived_file    bfd_generic_openr_next_archived_file
  4239. #define ecoff_generic_stat_arch_elt    bfd_generic_stat_arch_elt
  4240. #define    ecoff_get_section_contents    bfd_generic_get_section_contents
  4241. #define ecoff_get_reloc_upper_bound    coff_get_reloc_upper_bound
  4242. #define    ecoff_close_and_cleanup        bfd_generic_close_and_cleanup
  4243. #define ecoff_bfd_debug_info_start    bfd_void
  4244. #define ecoff_bfd_debug_info_end    bfd_void
  4245. #define ecoff_bfd_debug_info_accumulate    \
  4246.   ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
  4247. #define ecoff_bfd_get_relocated_section_contents \
  4248.   bfd_generic_get_relocated_section_contents
  4249. #define ecoff_bfd_relax_section        bfd_generic_relax_section
  4250. #define ecoff_bfd_make_debug_symbol \
  4251.   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
  4252.  
  4253. bfd_target ecoff_little_vec =
  4254. {
  4255.   "ecoff-littlemips",        /* name */
  4256.   bfd_target_ecoff_flavour,
  4257.   false,            /* data byte order is little */
  4258.   false,            /* header byte order is little */
  4259.  
  4260.   (HAS_RELOC | EXEC_P |        /* object flags */
  4261.    HAS_LINENO | HAS_DEBUG |
  4262.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  4263.  
  4264.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
  4265.                                 flags */
  4266.   0,                /* leading underscore */
  4267.   '/',                /* ar_pad_char */
  4268.   15,                /* ar_max_namelen */
  4269.   3,                /* minimum alignment power */
  4270.   _do_getl64, _do_getl_signed_64, _do_putl64,
  4271.      _do_getl32, _do_getl_signed_32, _do_putl32,
  4272.      _do_getl16, _do_getl_signed_16, _do_putl16, /* data */
  4273.   _do_getl64, _do_getl_signed_64, _do_putl64,
  4274.      _do_getl32, _do_getl_signed_32, _do_putl32,
  4275.      _do_getl16, _do_getl_signed_16, _do_putl16, /* hdrs */
  4276.  
  4277.   {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  4278.      ecoff_archive_p, _bfd_dummy_target},
  4279.   {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  4280.      bfd_false},
  4281.   {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
  4282.      _bfd_write_archive_contents, bfd_false},
  4283.   JUMP_TABLE (ecoff),
  4284.   (PTR) &bfd_ecoff_std_swap_table
  4285. };
  4286.  
  4287. bfd_target ecoff_big_vec =
  4288. {
  4289.   "ecoff-bigmips",        /* name */
  4290.   bfd_target_ecoff_flavour,
  4291.   true,                /* data byte order is big */
  4292.   true,                /* header byte order is big */
  4293.  
  4294.   (HAS_RELOC | EXEC_P |        /* object flags */
  4295.    HAS_LINENO | HAS_DEBUG |
  4296.    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
  4297.  
  4298.   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
  4299.   0,                /* leading underscore */
  4300.   ' ',                /* ar_pad_char */
  4301.   16,                /* ar_max_namelen */
  4302.   3,                /* minimum alignment power */
  4303.   _do_getb64, _do_getb_signed_64, _do_putb64,
  4304.      _do_getb32, _do_getb_signed_32, _do_putb32,
  4305.      _do_getb16, _do_getb_signed_16, _do_putb16,
  4306.   _do_getb64, _do_getb_signed_64, _do_putb64,
  4307.      _do_getb32, _do_getb_signed_32, _do_putb32,
  4308.      _do_getb16, _do_getb_signed_16, _do_putb16,
  4309.  {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
  4310.     ecoff_archive_p, ecoff_core_file_p},
  4311.  {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  4312.     bfd_false},
  4313.  {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
  4314.     _bfd_write_archive_contents, bfd_false},
  4315.   JUMP_TABLE(ecoff),
  4316.   (PTR) &bfd_ecoff_std_swap_table
  4317.   /* Note that there is another bfd_target just above this one.  If
  4318.      you are adding initializers here, you should be adding them there
  4319.      as well.  */
  4320. };
  4321.