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