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