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