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