home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / coff-mip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  126.3 KB  |  4,429 lines

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