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