home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / gnu / gdb-4.12.tar.gz / gdb-4.12.tar / gdb-4.12 / bfd / ecoff.c < prev    next >
C/C++ Source or Header  |  1994-02-03  |  124KB  |  4,274 lines

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