Checkpoint. Can now read relocs.
[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 "coff/mips.h"
27 #include "coff/internal.h"
28 #include "coff/sym.h"
29 #include "coff/symconst.h"
30 #include "coff/ecoff-ext.h"
31 #include "libcoff.h"
32
33 /* `Tdata' information kept for ECOFF files.  */
34
35 #define ecoff_data(abfd) ((abfd)->tdata.ecoff_obj_data)
36
37 typedef struct ecoff_tdata
38 {
39   /* The reloc file position, set by
40      ecoff_compute_section_file_positions.  */
41   file_ptr reloc_filepos;
42
43   /* The symbol table file position, set by ecoff_mkobject_hook.  */
44   file_ptr sym_filepos;
45
46   /* The cached gp value.  This is used when relocating.  */
47   bfd_vma gp;
48
49   /* The size of the unswapped ECOFF symbolic information.  */
50   bfd_size_type raw_size;
51
52   /* The unswapped ECOFF symbolic information.  */
53   PTR raw_syments;
54
55   /* The swapped ECOFF symbolic header.  */
56   HDRR symbolic_header;
57
58   /* Pointers to the unswapped symbolic information.  */
59   unsigned char *line;
60   struct dnr_ext *external_dnr;
61   struct pdr_ext *external_pdr;
62   struct sym_ext *external_sym;
63   struct opt_ext *external_opt;
64   union aux_ext *external_aux;
65   char *ss;
66   char *ssext;
67   struct fdr_ext *external_fdr;
68   struct rfd_ext *external_rfd;
69   struct ext_ext *external_ext;
70
71   /* The swapped fdr information.  */
72   FDR *fdr;
73
74   /* The canonical BFD symbols.  */
75   struct ecoff_symbol_struct *canonical_symbols;
76
77 } ecoff_data_type;
78
79 /* Each canonical asymbol really looks like this.  */
80
81 typedef struct ecoff_symbol_struct
82 {
83   /* The actual symbol which the rest of BFD works with */
84   asymbol symbol;
85
86   /* The fdr for this symbol.  */
87   FDR *fdr;
88
89   /* true if this is a local symbol rather than an external one.  */
90   boolean local;
91
92   /* A pointer to the unswapped hidden information for this symbol */
93   union
94     {
95       struct sym_ext *lnative;
96       struct ext_ext *enative;
97     }
98   native;
99 } ecoff_symbol_type;
100
101 /* We take the address of the first element of a asymbol to ensure that the
102    macro is only ever applied to an asymbol.  */
103 #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
104
105 /* MIPS ECOFF has COFF sections, but the debugging information is
106    stored in a completely different format.  This files uses the some
107    of the swapping routines from coffswap.h, and some of the generic
108    COFF routines in coffgen.c, but, unlike the real COFF targets, does
109    not use coffcode.h itself.  */
110
111 /* Get the generic COFF swapping routines, except for the reloc,
112    symbol, and lineno ones.  Give them ecoff names.  */
113 #define NO_COFF_RELOCS
114 #define NO_COFF_SYMBOLS
115 #define NO_COFF_LINENOS
116 #define coff_swap_filehdr_in ecoff_swap_filehdr_in
117 #define coff_swap_filehdr_out ecoff_swap_filehdr_out
118 #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
119 #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
120 #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
121 #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
122 #include "coffswap.h"
123 \f
124 /* This stuff is somewhat copied from coffcode.h.  */
125
126 static asection bfd_debug_section = { "*DEBUG*" };
127
128 /* See whether the magic number matches.  */
129
130 static boolean
131 DEFUN(ecoff_bad_format_hook, (abfd, filehdr),
132       bfd *abfd AND
133       PTR filehdr)
134 {
135   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
136
137   if (ECOFFBADMAG (*internal_f))
138     return false;
139
140   return true;
141 }
142
143 /* This is a hook needed by SCO COFF, but we have nothing to do.  */
144
145 static asection *
146 DEFUN (ecoff_make_section_hook, (abfd, name),
147        bfd *abfd AND
148        char *name)
149 {
150   return (asection *) NULL;
151 }
152
153 /* Initialize a new section.  */
154
155 static boolean
156 DEFUN (ecoff_new_section_hook, (abfd, section),
157        bfd *abfd AND
158        asection *section)
159 {
160   section->alignment_power = abfd->xvec->align_power_min;
161
162   if (strcmp (section->name, _TEXT) == 0)
163     section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
164   else if (strcmp (section->name, _DATA) == 0
165            || strcmp (section->name, _SDATA) == 0)
166     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
167   else if (strcmp (section->name, _RDATA) == 0
168            || strcmp (section->name, _LIT8) == 0
169            || strcmp (section->name, _LIT4) == 0)
170     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
171   else if (strcmp (section->name, _BSS) == 0
172            || strcmp (section->name, _SBSS) == 0)
173     section->flags |= SEC_ALLOC;
174
175   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
176      uncertain about .init on some systems and I don't know how shared
177      libraries work.  */
178
179   return true;
180 }
181
182 #define ecoff_set_alignment_hook \
183   ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
184
185 static boolean
186 DEFUN (ecoff_mkobject, (abfd),
187        bfd *abfd)
188 {
189   abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
190                                 bfd_zalloc (abfd, sizeof(ecoff_data_type)));
191   if (abfd->tdata.ecoff_obj_data == NULL)
192     {
193       bfd_error = no_memory;
194       return false;
195     }
196
197   return true;
198 }
199
200 /* Create the COFF backend specific information.  */
201
202 static          PTR
203 DEFUN(ecoff_mkobject_hook,(abfd, filehdr),
204       bfd            *abfd AND
205       PTR            filehdr)
206 {
207   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
208   ecoff_data_type *ecoff;
209
210   if (ecoff_mkobject (abfd) == false)
211     return NULL;
212
213   ecoff = ecoff_data (abfd);
214   ecoff->sym_filepos = internal_f->f_symptr;
215   return (PTR) ecoff;
216 }
217
218 /* Determine the machine architecture and type.  */
219 static boolean
220 DEFUN (ecoff_set_arch_mach_hook, (abfd, filehdr),
221        bfd *abfd AND
222        PTR filehdr)
223 {
224   long machine;
225   enum bfd_architecture arch;
226   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
227
228   machine = 0;
229   switch (internal_f->f_magic) {
230   case  MIPS_MAGIC_1:
231   case  MIPS_MAGIC_2:
232   case  MIPS_MAGIC_3:
233     arch = bfd_arch_mips;
234     machine = 0;
235     break;
236
237   default:                      /* Unreadable input file type */
238     arch = bfd_arch_obscure;
239     break;
240   }
241
242   bfd_default_set_arch_mach(abfd, arch, machine);
243   return true;
244 }
245
246 /* Get the section s_flags to use for a section.  */
247
248 static long
249 DEFUN (sec_to_styp_flags, (name, flags),
250        CONST char *name AND
251        flagword flags)
252 {
253   long styp;
254
255   styp = 0;
256
257   if (strcmp (name, _TEXT) == 0)
258     styp = STYP_TEXT;
259   else if (strcmp (name, _DATA) == 0)
260     styp == STYP_DATA;
261   else if (strcmp (name, _SDATA) == 0)
262     styp == STYP_SDATA;
263   else if (strcmp (name, _RDATA) == 0)
264     styp = STYP_RDATA;
265   else if (strcmp (name, _LIT8) == 0)
266     styp = STYP_LIT8;
267   else if (strcmp (name, _LIT4) == 0)
268     styp = STYP_LIT4;
269   else if (strcmp (name, _BSS) == 0)
270     styp = STYP_BSS;
271   else if (strcmp (name, _SBSS) == 0)
272     styp = STYP_SBSS;
273   else if (flags & SEC_CODE) 
274     styp = STYP_TEXT;
275   else if (flags & SEC_DATA) 
276     styp = STYP_DATA;
277   else if (flags & SEC_READONLY)
278     styp = STYP_RDATA;
279   else if (flags & SEC_LOAD)
280     styp = STYP_TEXT;
281   else
282     styp = STYP_BSS;
283
284   if (flags & SEC_NEVER_LOAD)
285     styp |= STYP_NOLOAD;
286
287   return styp;
288 }
289
290 /* Get the BFD flags to use for a section.  */
291
292 static flagword
293 DEFUN (styp_to_sec_flags, (abfd, hdr),
294        bfd *abfd AND
295        PTR hdr)
296 {
297   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
298   long styp_flags = internal_s->s_flags;
299   flagword sec_flags=0;
300
301   if (styp_flags & STYP_NOLOAD)
302     sec_flags |= SEC_NEVER_LOAD;
303
304   /* For 386 COFF, at least, an unloadable text or data section is
305      actually a shared library section.  */
306   if (styp_flags & STYP_TEXT)
307     {
308       if (sec_flags & SEC_NEVER_LOAD)
309         sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
310       else
311         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
312     }
313   else if ((styp_flags & STYP_DATA)
314            || (styp_flags & STYP_RDATA)
315            || (styp_flags & STYP_SDATA))
316     {
317       if (sec_flags & SEC_NEVER_LOAD)
318         sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
319       else
320         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
321       if (styp_flags & STYP_RDATA)
322         sec_flags |= SEC_READONLY;
323     }
324   else if ((styp_flags & STYP_BSS)
325            || (styp_flags & STYP_SBSS))
326     {
327       sec_flags |= SEC_ALLOC;
328     }
329   else if (styp_flags & STYP_INFO) 
330     {
331       sec_flags |= SEC_NEVER_LOAD;
332     }
333   else if ((styp_flags & STYP_LIT8)
334            || (styp_flags & STYP_LIT4))
335     {
336       sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
337     }
338   else
339     {
340       sec_flags |= SEC_ALLOC | SEC_LOAD;
341     }
342
343   return sec_flags;
344 }
345 \f
346 /* Read in and swap the important symbolic information for an ECOFF
347    object file.  */
348
349 static boolean
350 DEFUN (ecoff_slurp_symbolic_info, (abfd),
351        bfd *abfd)
352 {
353   struct hdr_ext external_symhdr;
354   HDRR *internal_symhdr;
355   bfd_size_type raw_base;
356   bfd_size_type raw_size;
357   PTR raw;
358   struct fdr_ext *fraw_src;
359   struct fdr_ext *fraw_end;
360   struct fdr *fdr_ptr;
361
362   /* Check whether we've already gotten it, and whether there's any to
363      get.  */
364   if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
365     return true;
366   if (ecoff_data (abfd)->sym_filepos == 0)
367     {
368       bfd_get_symcount (abfd) = 0;
369       return true;
370     }
371
372   /* At this point bfd_get_symcount (abfd) holds the number of symbols
373      as read from the file header, but on ECOFF this is always the
374      size of the symbolic information header.  It would be cleaner to
375      handle this when we first read the file in coffgen.c.  */
376   if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
377     {
378       bfd_error = bad_value;
379       return false;
380     }
381
382   /* Read the symbolic information header.  */
383   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
384       || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
385           != sizeof (external_symhdr)))
386     {
387       bfd_error = system_call_error;
388       return false;
389     }
390   internal_symhdr = &ecoff_data (abfd)->symbolic_header;
391   ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
392
393   if (internal_symhdr->magic != magicSym)
394     {
395       bfd_error = bad_value;
396       return false;
397     }
398
399   /* Now we can get the correct number of symbols.  */
400   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
401                              + internal_symhdr->iextMax);
402
403   /* Read all the symbolic information at once.  This expression
404      assumes that the external symbols are always the last item.  */
405   raw_base = ecoff_data (abfd)->sym_filepos + sizeof (external_symhdr);
406   raw_size = (internal_symhdr->cbExtOffset - raw_base
407               + internal_symhdr->iextMax * sizeof (struct ext_ext));
408   raw = (PTR) bfd_alloc (abfd, raw_size);
409   if (raw == NULL)
410     {
411       bfd_error = no_memory;
412       return false;
413     }
414   if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
415     {
416       bfd_error = system_call_error;
417       bfd_release (abfd, raw);
418       return false;
419     }
420
421   ecoff_data (abfd)->raw_size = raw_size;
422   ecoff_data (abfd)->raw_syments = raw;
423
424   /* Get pointers for the numeric offsets in the HDRR structure.  */
425 #define FIX(off1, off2, type) \
426   if (internal_symhdr->off1 == 0) \
427     ecoff_data (abfd)->off2 = (type *) NULL; \
428   else \
429     ecoff_data (abfd)->off2 = (type *) ((char *) raw \
430                                         + internal_symhdr->off1 \
431                                         - raw_base)
432   FIX (cbLineOffset, line, unsigned char);
433   FIX (cbDnOffset, external_dnr, struct dnr_ext);
434   FIX (cbPdOffset, external_pdr, struct pdr_ext);
435   FIX (cbSymOffset, external_sym, struct sym_ext);
436   FIX (cbOptOffset, external_opt, struct opt_ext);
437   FIX (cbAuxOffset, external_aux, union aux_ext);
438   FIX (cbSsOffset, ss, char);
439   FIX (cbSsExtOffset, ssext, char);
440   FIX (cbFdOffset, external_fdr, struct fdr_ext);
441   FIX (cbRfdOffset, external_rfd, struct rfd_ext);
442   FIX (cbExtOffset, external_ext, struct ext_ext);
443 #undef FIX
444
445   /* I don't want to always swap all the data, because it will just
446      waste time and most programs will never look at it.  The only
447      time the linker needs most of the debugging information swapped
448      is when linking big-endian and little-endian MIPS object files
449      together, which is not a common occurrence.
450
451      We need to look at the fdr to deal with a lot of information in
452      the symbols, so we swap them here.  */
453   ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
454                                                      (internal_symhdr->ifdMax *
455                                                       sizeof (struct fdr)));
456   if (ecoff_data (abfd)->fdr == NULL)
457     {
458       bfd_error = no_memory;
459       return false;
460     }
461   fdr_ptr = ecoff_data (abfd)->fdr;
462   fraw_src = ecoff_data (abfd)->external_fdr;
463   fraw_end = fraw_src + internal_symhdr->ifdMax;
464   for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
465     ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
466
467   return true;
468 }
469 \f
470 /* ECOFF symbol table routines.  The ECOFF symbol table is described
471    in gcc/mips-tfile.c.  */
472
473 /* Create an empty symbol.  */
474
475 static asymbol *
476 DEFUN (ecoff_make_empty_symbol, (abfd),
477        bfd *abfd)
478 {
479   ecoff_symbol_type *new;
480
481   new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
482   if (new == (ecoff_symbol_type *) NULL)
483     {
484       bfd_error = no_memory;
485       return (asymbol *) NULL;
486     }
487   new->symbol.section = (asection *) NULL;
488   new->fdr = (FDR *) NULL;
489   new->local = false;
490   new->native.lnative = (struct sym_ext *) NULL;
491   new->symbol.the_bfd = abfd;
492   return &new->symbol;
493 }
494
495 /* Set the BFD flags and section for an ECOFF symbol.  */
496
497 static void
498 DEFUN (ecoff_set_symbol_info, (abfd, ecoff_sym, asym, ext),
499        bfd *abfd AND
500        SYMR *ecoff_sym AND
501        asymbol *asym AND
502        int ext)
503 {
504   asym->the_bfd = abfd;
505   asym->value = ecoff_sym->value;
506   asym->section = &bfd_debug_section;
507   asym->udata = NULL;
508   if (ext)
509     asym->flags = BSF_EXPORT | BSF_GLOBAL;
510   else
511     asym->flags = BSF_LOCAL;
512   switch (ecoff_sym->sc)
513     {
514     case scNil:
515       asym->flags = 0;
516       break;
517     case scText:
518       asym->section = bfd_make_section_old_way (abfd, ".text");
519       asym->value -= asym->section->vma;
520       break;
521     case scData:
522       asym->section = bfd_make_section_old_way (abfd, ".data");
523       asym->value -= asym->section->vma;
524       break;
525     case scBss:
526       asym->section = &bfd_com_section;
527       asym->flags = 0;
528       break;
529     case scRegister:
530       asym->flags = BSF_DEBUGGING;
531       break;
532     case scAbs:
533       asym->section = &bfd_abs_section;
534       asym->flags = 0;
535       break;
536     case scUndefined:
537       asym->section = &bfd_und_section;
538       asym->flags = 0;
539       break;
540     case scCdbLocal:
541     case scBits:
542     case scCdbSystem:
543     case scRegImage:
544     case scInfo:
545     case scUserStruct:
546       asym->flags = BSF_DEBUGGING;
547       break;
548     case scSData:
549       asym->section = bfd_make_section_old_way (abfd, ".sdata");
550       asym->value -= asym->section->vma;
551       break;
552     case scSBss:
553       asym->section = &bfd_com_section;
554       asym->flags = 0;
555       break;
556     case scRData:
557       asym->section = bfd_make_section_old_way (abfd, ".rdata");
558       asym->value -= asym->section->vma;
559       break;
560     case scVar:
561       asym->flags = BSF_DEBUGGING;
562       break;
563     case scCommon:
564     case scSCommon:
565       asym->section = &bfd_com_section;
566       asym->flags = 0;
567       break;
568     case scVarRegister:
569     case scVariant:
570       asym->flags = BSF_DEBUGGING;
571       break;
572     case scSUndefined:
573       asym->section = &bfd_und_section;
574       asym->flags = 0;
575       break;
576     case scInit:
577       asym->section = bfd_make_section_old_way (abfd, ".init");
578       asym->value -= asym->section->vma;
579       break;
580     case scBasedVar:
581     case scXData:
582     case scPData:
583       asym->flags = BSF_DEBUGGING;
584       break;
585     case scFini:
586       asym->section = bfd_make_section_old_way (abfd, ".fini");
587       asym->value -= asym->section->vma;
588       break;
589     default:
590       asym->flags = 0;
591       break;
592     }
593 }
594
595 /* Read an ECOFF symbol table.  */
596
597 static boolean
598 DEFUN (ecoff_slurp_symbol_table, (abfd),
599        bfd *abfd)
600 {
601   bfd_size_type internal_size;
602   ecoff_symbol_type *internal;
603   ecoff_symbol_type *internal_ptr;
604   struct ext_ext *eraw_src;
605   struct ext_ext *eraw_end;
606   FDR *fdr_ptr;
607   FDR *fdr_end;
608
609   /* If we've already read in the symbol table, do nothing.  */
610   if (ecoff_data (abfd)->canonical_symbols != NULL)
611     return true;
612
613   /* Get the symbolic information.  */
614   if (ecoff_slurp_symbolic_info (abfd) == false)
615     return false;
616   if (bfd_get_symcount (abfd) == 0)
617     return true;
618
619   internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
620   internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
621   if (internal == NULL)
622     {
623       bfd_error = no_memory;
624       return false;
625     }
626
627   internal_ptr = internal;
628   eraw_src = ecoff_data (abfd)->external_ext;
629   eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
630   for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
631     {
632       EXTR internal_esym;
633
634       ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
635       internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
636                                    + internal_esym.asym.iss);
637       ecoff_set_symbol_info (abfd, &internal_esym.asym,
638                              &internal_ptr->symbol, 1);
639       internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
640       internal_ptr->local = false;
641       internal_ptr->native.enative = eraw_src;
642     }
643
644   /* The local symbols must be accessed via the fdr's, because the
645      string and aux indices are relative to the fdr information.  */
646   fdr_ptr = ecoff_data (abfd)->fdr;
647   fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
648   for (; fdr_ptr < fdr_end; fdr_ptr++)
649     {
650       struct sym_ext *lraw_src;
651       struct sym_ext *lraw_end;
652
653       lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
654       lraw_end = lraw_src + fdr_ptr->csym;
655       for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
656         {
657           SYMR internal_sym;
658
659           ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
660           internal_ptr->symbol.name = (ecoff_data (abfd)->ss
661                                        + fdr_ptr->issBase
662                                        + internal_sym.iss);
663           ecoff_set_symbol_info (abfd, &internal_sym,
664                                  &internal_ptr->symbol, 0);
665           internal_ptr->fdr = fdr_ptr;
666           internal_ptr->local = true;
667           internal_ptr->native.lnative = lraw_src;
668         }
669     }
670
671   ecoff_data (abfd)->canonical_symbols = internal;
672
673   return true;
674 }
675
676 static unsigned int
677 DEFUN (ecoff_get_symtab_upper_bound, (abfd),
678        bfd *abfd)
679 {
680   if (ecoff_slurp_symbolic_info (abfd) == false
681       || bfd_get_symcount (abfd) == 0)
682     return 0;
683
684   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
685 }
686
687 static unsigned int
688 DEFUN (ecoff_get_symtab, (abfd, alocation),
689        bfd *abfd AND
690        asymbol **alocation)
691 {
692   unsigned int counter = 0;
693   ecoff_symbol_type *symbase;
694   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
695
696   if (ecoff_slurp_symbol_table (abfd) == false
697       || bfd_get_symcount (abfd) == 0)
698     return 0;
699
700   symbase = ecoff_data (abfd)->canonical_symbols;
701   while (counter < bfd_get_symcount (abfd))
702     {
703       *(location++) = symbase++;
704       counter++;
705     }
706   *location++ = (ecoff_symbol_type *) NULL;
707   return bfd_get_symcount (abfd);
708 }
709
710 /* Turn ECOFF type information into a printable string.
711    emit_aggregate and type_to_string are from gcc/mips-tdump.c, with
712    swapping added and used_ptr removed.  */
713
714 /* Write aggregate information to a string.  */
715
716 static void
717 DEFUN (emit_aggregate, (abfd, string, rndx, isym, which),
718        bfd *abfd AND
719        char *string AND
720        RNDXR *rndx AND
721        long isym AND
722        CONST char *which)
723 {
724   int ifd = rndx->rfd;
725   int indx = rndx->index;
726   int sym_base, ss_base;
727   CONST char *name;
728   
729   if (ifd == 0xfff)
730     ifd = isym;
731
732   sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
733   ss_base  = ecoff_data (abfd)->fdr[ifd].issBase;
734   
735   if (indx == indexNil)
736     name = "/* no name */";
737   else
738     {
739       SYMR sym;
740
741       indx += sym_base;
742       ecoff_swap_sym_in (abfd,
743                          ecoff_data (abfd)->external_sym + indx,
744                          &sym);
745       name = ecoff_data (abfd)->ss + ss_base + sym.iss;
746     }
747
748   sprintf (string,
749            "%s %s { ifd = %d, index = %d }",
750            which, name, ifd,
751            indx + ecoff_data (abfd)->symbolic_header.iextMax);
752 }
753
754 /* Convert the type information to string format.  */
755
756 static char *
757 DEFUN (type_to_string, (abfd, aux_ptr, indx, bigendian),
758        bfd *abfd AND
759        union aux_ext *aux_ptr AND
760        int indx AND
761        int bigendian)
762 {
763   AUXU u;
764   struct qual {
765     unsigned int  type;
766     int  low_bound;
767     int  high_bound;
768     int  stride;
769   } qualifiers[7];
770
771   unsigned int basic_type;
772   int i;
773   static char buffer1[1024];
774   static char buffer2[1024];
775   char *p1 = buffer1;
776   char *p2 = buffer2;
777   RNDXR rndx;
778
779   for (i = 0; i < 7; i++)
780     {
781       qualifiers[i].low_bound = 0;
782       qualifiers[i].high_bound = 0;
783       qualifiers[i].stride = 0;
784     }
785
786   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
787     return "-1 (no type)";
788   ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
789
790   basic_type = u.ti.bt;
791   qualifiers[0].type = u.ti.tq0;
792   qualifiers[1].type = u.ti.tq1;
793   qualifiers[2].type = u.ti.tq2;
794   qualifiers[3].type = u.ti.tq3;
795   qualifiers[4].type = u.ti.tq4;
796   qualifiers[5].type = u.ti.tq5;
797   qualifiers[6].type = tqNil;
798
799   /*
800    * Go get the basic type.
801    */
802   switch (basic_type)
803     {
804     case btNil:                 /* undefined */
805       strcpy (p1, "nil");
806       break;
807
808     case btAdr:                 /* address - integer same size as pointer */
809       strcpy (p1, "address");
810       break;
811
812     case btChar:                /* character */
813       strcpy (p1, "char");
814       break;
815
816     case btUChar:               /* unsigned character */
817       strcpy (p1, "unsigned char");
818       break;
819
820     case btShort:               /* short */
821       strcpy (p1, "short");
822       break;
823
824     case btUShort:              /* unsigned short */
825       strcpy (p1, "unsigned short");
826       break;
827
828     case btInt:                 /* int */
829       strcpy (p1, "int");
830       break;
831
832     case btUInt:                /* unsigned int */
833       strcpy (p1, "unsigned int");
834       break;
835
836     case btLong:                /* long */
837       strcpy (p1, "long");
838       break;
839
840     case btULong:               /* unsigned long */
841       strcpy (p1, "unsigned long");
842       break;
843
844     case btFloat:               /* float (real) */
845       strcpy (p1, "float");
846       break;
847
848     case btDouble:              /* Double (real) */
849       strcpy (p1, "double");
850       break;
851
852       /* Structures add 1-2 aux words:
853          1st word is [ST_RFDESCAPE, offset] pointer to struct def;
854          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
855
856     case btStruct:              /* Structure (Record) */
857       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
858       emit_aggregate (abfd, p1, &rndx,
859                       AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
860                       "struct");
861       indx++;                   /* skip aux words */
862       break;
863
864       /* Unions add 1-2 aux words:
865          1st word is [ST_RFDESCAPE, offset] pointer to union def;
866          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
867
868     case btUnion:               /* Union */
869       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
870       emit_aggregate (abfd, p1, &rndx,
871                       AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
872                       "union");
873       indx++;                   /* skip aux words */
874       break;
875
876       /* Enumerations add 1-2 aux words:
877          1st word is [ST_RFDESCAPE, offset] pointer to enum def;
878          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
879
880     case btEnum:                /* Enumeration */
881       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
882       emit_aggregate (abfd, p1, &rndx,
883                       AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
884                       "enum");
885       indx++;                   /* skip aux words */
886       break;
887
888     case btTypedef:             /* defined via a typedef, isymRef points */
889       strcpy (p1, "typedef");
890       break;
891
892     case btRange:               /* subrange of int */
893       strcpy (p1, "subrange");
894       break;
895
896     case btSet:                 /* pascal sets */
897       strcpy (p1, "set");
898       break;
899
900     case btComplex:             /* fortran complex */
901       strcpy (p1, "complex");
902       break;
903
904     case btDComplex:            /* fortran double complex */
905       strcpy (p1, "double complex");
906       break;
907
908     case btIndirect:            /* forward or unnamed typedef */
909       strcpy (p1, "forward/unamed typedef");
910       break;
911
912     case btFixedDec:            /* Fixed Decimal */
913       strcpy (p1, "fixed decimal");
914       break;
915
916     case btFloatDec:            /* Float Decimal */
917       strcpy (p1, "float decimal");
918       break;
919
920     case btString:              /* Varying Length Character String */
921       strcpy (p1, "string");
922       break;
923
924     case btBit:                 /* Aligned Bit String */
925       strcpy (p1, "bit");
926       break;
927
928     case btPicture:             /* Picture */
929       strcpy (p1, "picture");
930       break;
931
932     case btVoid:                /* Void */
933       strcpy (p1, "void");
934       break;
935
936     default:
937       sprintf (p1, "Unknown basic type %d", (int) basic_type);
938       break;
939     }
940
941   p1 += strlen (buffer1);
942
943   /*
944    * If this is a bitfield, get the bitsize.
945    */
946   if (u.ti.fBitfield)
947     {
948       int bitsize;
949
950       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
951       sprintf (p1, " : %d", bitsize);
952       p1 += strlen (buffer1);
953     }
954
955
956   /*
957    * Deal with any qualifiers.
958    */
959   if (qualifiers[0].type != tqNil)
960     {
961       /*
962        * Snarf up any array bounds in the correct order.  Arrays
963        * store 5 successive words in the aux. table:
964        *        word 0  RNDXR to type of the bounds (ie, int)
965        *        word 1  Current file descriptor index
966        *        word 2  low bound
967        *        word 3  high bound (or -1 if [])
968        *        word 4  stride size in bits
969        */
970       for (i = 0; i < 7; i++)
971         {
972           if (qualifiers[i].type == tqArray)
973             {
974               qualifiers[i].low_bound =
975                 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
976               qualifiers[i].high_bound =
977                 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
978               qualifiers[i].stride =
979                 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
980               indx += 5;
981             }
982         }
983
984       /*
985        * Now print out the qualifiers.
986        */
987       for (i = 0; i < 6; i++)
988         {
989           switch (qualifiers[i].type)
990             {
991             case tqNil:
992             case tqMax:
993               break;
994
995             case tqPtr:
996               strcpy (p2, "ptr to ");
997               p2 += sizeof ("ptr to ")-1;
998               break;
999
1000             case tqVol:
1001               strcpy (p2, "volatile ");
1002               p2 += sizeof ("volatile ")-1;
1003               break;
1004
1005             case tqFar:
1006               strcpy (p2, "far ");
1007               p2 += sizeof ("far ")-1;
1008               break;
1009
1010             case tqProc:
1011               strcpy (p2, "func. ret. ");
1012               p2 += sizeof ("func. ret. ");
1013               break;
1014
1015             case tqArray:
1016               {
1017                 int first_array = i;
1018                 int j;
1019
1020                 /* Print array bounds reversed (ie, in the order the C
1021                    programmer writes them).  C is such a fun language.... */
1022
1023                 while (i < 5 && qualifiers[i+1].type == tqArray)
1024                   i++;
1025
1026                 for (j = i; j >= first_array; j--)
1027                   {
1028                     strcpy (p2, "array [");
1029                     p2 += sizeof ("array [")-1;
1030                     if (qualifiers[j].low_bound != 0)
1031                       sprintf (p2,
1032                                "%ld:%ld {%ld bits}",
1033                                (long) qualifiers[j].low_bound,
1034                                (long) qualifiers[j].high_bound,
1035                                (long) qualifiers[j].stride);
1036
1037                     else if (qualifiers[j].high_bound != -1)
1038                       sprintf (p2,
1039                                "%ld {%ld bits}",
1040                                (long) (qualifiers[j].high_bound + 1),
1041                                (long) (qualifiers[j].stride));
1042
1043                     else
1044                       sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1045
1046                     p2 += strlen (p2);
1047                     strcpy (p2, "] of ");
1048                     p2 += sizeof ("] of ")-1;
1049                   }
1050               }
1051               break;
1052             }
1053         }
1054     }
1055
1056   strcpy (p2, buffer1);
1057   return buffer2;
1058 }
1059
1060 /* Print information about an ECOFF symbol.  */
1061
1062 static void
1063 DEFUN (ecoff_print_symbol, (abfd, filep, symbol, how),
1064        bfd *abfd AND
1065        PTR filep AND
1066        asymbol *symbol AND
1067        bfd_print_symbol_type how)
1068 {
1069   FILE *file = (FILE *)filep;
1070
1071   switch (how)
1072     {
1073     case bfd_print_symbol_name:
1074       fprintf (file, "%s", symbol->name);
1075       break;
1076     case bfd_print_symbol_more:
1077       if (ecoffsymbol (symbol)->local)
1078         {
1079           SYMR ecoff_sym;
1080         
1081           ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1082                              &ecoff_sym);
1083           fprintf (file, "ecoff local %lx %x %x",
1084                    (unsigned long) ecoff_sym.value,
1085                    (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
1086         }
1087       else
1088         {
1089           EXTR ecoff_ext;
1090
1091           ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1092                              &ecoff_ext);
1093           fprintf (file, "ecoff extern %lx %x %x",
1094                    (unsigned long) ecoff_ext.asym.value,
1095                    (unsigned) ecoff_ext.asym.st,
1096                    (unsigned) ecoff_ext.asym.sc);
1097         }
1098       break;
1099     case bfd_print_symbol_nm:
1100       {
1101         CONST char *section_name = symbol->section->name;
1102
1103         bfd_print_symbol_vandf ((PTR) file, symbol);
1104         fprintf (file, " %-5s %s %s",
1105                  section_name,
1106                  ecoffsymbol (symbol)->local ? "l" : "e",
1107                  symbol->name);
1108       }
1109       break;
1110     case bfd_print_symbol_all:
1111       /* Print out the symbols in a reasonable way */
1112       {
1113         char type;
1114         int pos;
1115         EXTR ecoff_ext;
1116         char jmptbl;
1117         char cobol_main;
1118         char weakext;
1119
1120         if (ecoffsymbol (symbol)->local)
1121           {
1122             ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1123                                &ecoff_ext.asym);
1124             type = 'l';
1125             pos = (ecoffsymbol (symbol)->native.lnative
1126                    - ecoff_data (abfd)->external_sym
1127                    + ecoff_data (abfd)->symbolic_header.iextMax);
1128             jmptbl = ' ';
1129             cobol_main = ' ';
1130             weakext = ' ';
1131           }
1132         else
1133           {
1134             ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1135                                &ecoff_ext);
1136             type = 'e';
1137             pos = (ecoffsymbol (symbol)->native.enative
1138                    - ecoff_data (abfd)->external_ext);
1139             jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1140             cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1141             weakext = ecoff_ext.weakext ? 'w' : ' ';
1142           }
1143
1144         fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
1145                  pos, type, (unsigned long) ecoff_ext.asym.value,
1146                  (unsigned) ecoff_ext.asym.st,
1147                  (unsigned) ecoff_ext.asym.sc,
1148                  (unsigned) ecoff_ext.asym.index,
1149                  jmptbl, cobol_main, weakext,
1150                  symbol->name);
1151
1152         if (ecoffsymbol (symbol)->fdr != NULL
1153             && ecoff_ext.asym.index != indexNil)
1154           {
1155             unsigned indx;
1156             int bigendian;
1157             long sym_base;
1158             union aux_ext *aux_base;
1159
1160             indx = ecoff_ext.asym.index;
1161
1162             /* sym_base is used to map the fdr relative indices which
1163                appear in the file to the position number which we are
1164                using.  */
1165             sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1166             if (ecoffsymbol (symbol)->local)
1167               sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1168
1169             /* aux_base is the start of the aux entries for this file;
1170                asym.index is an offset from this.  */
1171             aux_base = (ecoff_data (abfd)->external_aux
1172                         + ecoffsymbol (symbol)->fdr->iauxBase);
1173
1174             /* The aux entries are stored in host byte order; the
1175                order is indicated by a bit in the fdr.  */
1176             bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1177
1178             /* This switch is basically from gcc/mips-tdump.c  */
1179             switch (ecoff_ext.asym.st)
1180               {
1181               case stNil:
1182               case stLabel:
1183                 break;
1184
1185               case stFile:
1186               case stBlock:
1187                 printf ("\n      End+1 symbol: %ld", indx + sym_base);
1188                 break;
1189
1190               case stEnd:
1191                 if (ecoff_ext.asym.sc == scText
1192                     || ecoff_ext.asym.sc == scInfo)
1193                   printf ("\n      First symbol: %ld", indx + sym_base);
1194                 else
1195                   printf ("\n      First symbol: %ld", 
1196                           (AUX_GET_ISYM (bigendian,
1197                                          &aux_base[ecoff_ext.asym.index])
1198                            + sym_base));
1199                 break;
1200
1201               case stProc:
1202               case stStaticProc:
1203                 if (MIPS_IS_STAB (&ecoff_ext.asym))
1204                   ;
1205                 else if (ecoffsymbol (symbol)->local)
1206                   printf ("\n      End+1 symbol: %-7ld   Type:  %s",
1207                           (AUX_GET_ISYM (bigendian,
1208                                          &aux_base[ecoff_ext.asym.index])
1209                            + sym_base),
1210                           type_to_string (abfd, aux_base, indx + 1,
1211                                           bigendian));
1212                 else
1213                   printf ("\n      Local symbol: %d",
1214                           (indx
1215                            + sym_base
1216                            + ecoff_data (abfd)->symbolic_header.iextMax));
1217                 break;
1218
1219               default:
1220                 if (!MIPS_IS_STAB (&ecoff_ext.asym))
1221                   printf ("\n      Type: %s",
1222                           type_to_string (abfd, aux_base, indx, bigendian));
1223                 break;
1224               }
1225           }
1226       }
1227       break;
1228     }
1229 }
1230 \f
1231 /* Reloc handling.  MIPS ECOFF relocs are packed into 8 bytes in
1232    external form.  They use a bit which indicates whether the symbol
1233    is external.  */
1234
1235 /* Swap a reloc in.  */
1236
1237 static void
1238 DEFUN (ecoff_swap_reloc_in, (abfd, ext, intern),
1239        bfd *abfd AND
1240        RELOC *ext AND
1241        struct internal_reloc *intern)
1242 {
1243   intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
1244   if (abfd->xvec->header_byteorder_big_p != false)
1245     {
1246       intern->r_symndx = ((ext->r_bits[0]
1247                            << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
1248                           | (ext->r_bits[1]
1249                              << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
1250                           | (ext->r_bits[2]
1251                              << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
1252       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
1253                         >> RELOC_BITS3_TYPE_SH_BIG);
1254       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
1255     }
1256   else
1257     {
1258       intern->r_symndx = ((ext->r_bits[0]
1259                            << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
1260                           | (ext->r_bits[1]
1261                              << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
1262                           | (ext->r_bits[2]
1263                              << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
1264       intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
1265                         >> RELOC_BITS3_TYPE_SH_LITTLE);
1266       intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
1267     }
1268 }
1269
1270 /* Swap a reloc out.  */
1271
1272 static unsigned int
1273 DEFUN (ecoff_swap_reloc_out, (abfd, src, dst),
1274        bfd *abfd AND
1275        PTR src AND
1276        PTR dst)
1277 {
1278   struct internal_reloc *intern = (struct internal_reloc *) src;
1279   RELOC *ext = (RELOC *) dst;
1280
1281   bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
1282   if (abfd->xvec->header_byteorder_big_p != false)
1283     {
1284       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
1285       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
1286       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
1287       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
1288                          & RELOC_BITS3_TYPE_BIG)
1289                         | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
1290     }
1291   else
1292     {
1293       ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
1294       ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
1295       ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
1296       ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
1297                          & RELOC_BITS3_TYPE_LITTLE)
1298                         | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
1299     }
1300
1301   return RELSZ;
1302 }
1303
1304 /* Do a REFHI relocation.  The next reloc must be the corresponding
1305    REFLO.  This has to be done in a function so that carry is handled
1306    correctly.  */
1307
1308 static bfd_reloc_status_type
1309 DEFUN (ecoff_refhi_reloc, (abfd,
1310                            reloc_entry,
1311                            symbol,
1312                            data,
1313                            input_section,
1314                            output_bfd),
1315        bfd *abfd AND
1316        arelent *reloc_entry AND
1317        asymbol *symbol AND
1318        PTR data AND
1319        asection *input_section AND
1320        bfd *output_bfd)
1321 {
1322   bfd_reloc_status_type ret;
1323   arelent *rello;
1324   bfd_vma relocation;
1325   asection *reloc_target_output_section;
1326   unsigned long val;
1327   unsigned long insn;
1328
1329   ret = bfd_reloc_ok;
1330   if (symbol->section == &bfd_und_section
1331       && output_bfd == (bfd *) NULL)
1332     ret = bfd_reloc_undefined;
1333
1334   rello = reloc_entry + 1;
1335   BFD_ASSERT (rello->howto->type == ECOFF_R_REFLO
1336               && *rello->sym_ptr_ptr == *reloc_entry->sym_ptr_ptr);
1337
1338   if (symbol->section == &bfd_com_section)
1339     relocation = 0;
1340   else
1341     relocation = symbol->value;
1342
1343   relocation += symbol->section->output_section->vma;
1344   relocation += symbol->section->output_offset;
1345
1346   if (reloc_entry->address > input_section->_cooked_size)
1347     return bfd_reloc_outofrange;
1348
1349   if (output_bfd != (bfd *) NULL) 
1350     reloc_entry->address += input_section->output_offset;
1351
1352   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1353   val = (((insn & 0xffff) << 16)
1354          + (bfd_get_32 (abfd, (bfd_byte *) data + rello->address) & 0xffff));
1355   insn = (insn &~ 0xffff) | (((val + symbol->value) >> 16) & 0xffff);
1356   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1357
1358   return ret;
1359 }
1360
1361 /* Do a GPREL relocation.  This is a 16 bit value which must become
1362    the offset from the gp register.  */
1363
1364 static bfd_reloc_status_type
1365 DEFUN (ecoff_gprel_reloc, (abfd,
1366                            reloc_entry,
1367                            symbol,
1368                            data,
1369                            input_section,
1370                            output_bfd),
1371        bfd *abfd AND
1372        arelent *reloc_entry AND
1373        asymbol *symbol AND
1374        PTR data AND
1375        asection *input_section AND
1376        bfd *output_bfd)
1377 {
1378   bfd_reloc_status_type ret;
1379   bfd_vma relocation;
1380   unsigned long val;
1381   unsigned long insn;
1382
1383   /* If this is a partial link, we don't need to do anything unusual
1384      here.  */
1385   if (output_bfd != (bfd *) NULL)
1386     return bfd_reloc_continue;
1387
1388   ret = bfd_reloc_ok;
1389   if (symbol->section == &bfd_und_section)
1390     ret = bfd_reloc_undefined;
1391
1392   /* Otherwise we have to figure out the gp value, so that we can
1393      adjust the symbol value correctly.  We look up the symbol _gp in
1394      the output BFD.  If we can't find it, we're stuck.  We cache it
1395      in the ECOFF target data.  */
1396   output_bfd = symbol->section->output_section->owner;
1397   if (ecoff_data (output_bfd)->gp == 0)
1398     {
1399       unsigned int count;
1400       asymbol **sym;
1401       unsigned int i;
1402
1403       count = bfd_get_symcount (output_bfd);
1404       sym = bfd_get_outsymbols (output_bfd);
1405
1406       /* We should do something more friendly here, but we don't have
1407          a good reloc status to return.  */
1408       if (sym == (asymbol **) NULL)
1409         abort ();
1410
1411       for (i = 0; i < count; i++, sym++)
1412         {
1413           register CONST char *name;
1414
1415           name = bfd_asymbol_name (*sym);
1416           if (*name == '_' && strcmp (name, "_gp") == 0)
1417             {
1418               ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1419               break;
1420             }
1421         }
1422
1423       /* We should do something more friendly here, but we don't have
1424          a good reloc status to return.  */
1425       if (i >= count)
1426         abort ();
1427     }
1428
1429   if (symbol->section == &bfd_com_section)
1430     relocation = 0;
1431   else
1432     relocation = symbol->value;
1433
1434   relocation += symbol->section->output_section->vma;
1435   relocation += symbol->section->output_offset;
1436
1437   if (reloc_entry->address > input_section->_cooked_size)
1438     return bfd_reloc_outofrange;
1439
1440   insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1441   val = insn & 0xffff;
1442   if ((val & 0x8000) != 0)
1443     val -= 0x10000;
1444   val -= ecoff_data (output_bfd)->gp;
1445   insn = (insn &~ 0xffff) | (val & 0xffff);
1446   bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1447
1448   return ret;
1449 }
1450
1451 /* How to process the various relocs types.  */
1452
1453 static reloc_howto_type ecoff_howto_table[] =
1454 {
1455   /* Reloc type 0 is ignored.  The reloc reading code ensures that
1456      this is a reference to the .abs section, which will cause
1457      bfd_perform_relocation to do nothing.  */
1458   HOWTO (ECOFF_R_IGNORE,        /* type */
1459          0,                     /* rightshift */
1460          0,                     /* size (0 = byte, 1 = short, 2 = long) */
1461          8,                     /* bitsize (obsolete) */
1462          false,                 /* pc_relative */
1463          0,                     /* bitpos */
1464          false,                 /* absolute (obsolete) */
1465          false,                 /* complain_on_overflow */
1466          0,                     /* special_function */
1467          "IGNORE",              /* name */
1468          false,                 /* partial_inplace */
1469          0,                     /* src_mask */
1470          0,                     /* dst_mask */
1471          false),                /* pcrel_offset */
1472
1473   /* A 16 bit reference to a symbol, normally from a data section.  */
1474   HOWTO (ECOFF_R_REFHALF,       /* type */
1475          0,                     /* rightshift */
1476          1,                     /* size (0 = byte, 1 = short, 2 = long) */
1477          16,                    /* bitsize (obsolete) */
1478          false,                 /* pc_relative */
1479          0,                     /* bitpos */
1480          false,                 /* absolute (obsolete) */
1481          true,                  /* complain_on_overflow */
1482          0,                     /* special_function */
1483          "REFHALF",             /* name */
1484          true,                  /* partial_inplace */
1485          0xffff,                /* src_mask */
1486          0xffff,                /* dst_mask */
1487          false),                /* pcrel_offset */
1488
1489   /* A 32 bit reference to a symbol, normally from a data section.  */
1490   HOWTO (ECOFF_R_REFWORD,       /* type */
1491          0,                     /* rightshift */
1492          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1493          32,                    /* bitsize (obsolete) */
1494          false,                 /* pc_relative */
1495          0,                     /* bitpos */
1496          false,                 /* absolute (obsolete) */
1497          true,                  /* complain_on_overflow */
1498          0,                     /* special_function */
1499          "REFWORD",             /* name */
1500          true,                  /* partial_inplace */
1501          0xffffffff,            /* src_mask */
1502          0xffffffff,            /* dst_mask */
1503          false),                /* pcrel_offset */
1504
1505   /* A 26 bit absolute jump address.  */
1506   HOWTO (ECOFF_R_JMPADDR,       /* type */
1507          2,                     /* rightshift */
1508          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1509          32,                    /* bitsize (obsolete) */
1510          false,                 /* pc_relative */
1511          0,                     /* bitpos */
1512          false,                 /* absolute (obsolete) */
1513          true,                  /* complain_on_overflow */
1514          0,                     /* special_function */
1515          "JMPADDR",             /* name */
1516          true,                  /* partial_inplace */
1517          0x3ffffff,             /* src_mask */
1518          0x3ffffff,             /* dst_mask */
1519          false),                /* pcrel_offset */
1520
1521   /* The high 16 bits of a symbol value.  Handled by the function
1522      ecoff_refhi_reloc.  */
1523   HOWTO (ECOFF_R_REFHI,         /* type */
1524          16,                    /* rightshift */
1525          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1526          32,                    /* bitsize (obsolete) */
1527          false,                 /* pc_relative */
1528          0,                     /* bitpos */
1529          false,                 /* absolute (obsolete) */
1530          true,                  /* complain_on_overflow */
1531          ecoff_refhi_reloc,     /* special_function */
1532          "REFHI",               /* name */
1533          true,                  /* partial_inplace */
1534          0xffff,                /* src_mask */
1535          0xffff,                /* dst_mask */
1536          false),                /* pcrel_offset */
1537
1538   /* The low 16 bits of a symbol value.  */
1539   HOWTO (ECOFF_R_REFLO,         /* type */
1540          0,                     /* rightshift */
1541          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1542          32,                    /* bitsize (obsolete) */
1543          false,                 /* pc_relative */
1544          0,                     /* bitpos */
1545          false,                 /* absolute (obsolete) */
1546          true,                  /* complain_on_overflow */
1547          0,                     /* special_function */
1548          "REFLO",               /* name */
1549          true,                  /* partial_inplace */
1550          0xffff,                /* src_mask */
1551          0xffff,                /* dst_mask */
1552          false),                /* pcrel_offset */
1553
1554   /* A reference to an offset from the gp register.  Handled by the
1555      function ecoff_gprel_reloc.  */
1556   HOWTO (ECOFF_R_GPREL,         /* type */
1557          0,                     /* rightshift */
1558          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1559          32,                    /* bitsize (obsolete) */
1560          false,                 /* pc_relative */
1561          0,                     /* bitpos */
1562          false,                 /* absolute (obsolete) */
1563          true,                  /* complain_on_overflow */
1564          ecoff_gprel_reloc,     /* special_function */
1565          "GPREL",               /* name */
1566          true,                  /* partial_inplace */
1567          0xffff,                /* src_mask */
1568          0xffff,                /* dst_mask */
1569          false),                /* pcrel_offset */
1570
1571   /* A reference to a literal using an offset from the gp register.
1572      Handled by the function ecoff_gprel_reloc.  */
1573   HOWTO (ECOFF_R_LITERAL,       /* type */
1574          0,                     /* rightshift */
1575          2,                     /* size (0 = byte, 1 = short, 2 = long) */
1576          32,                    /* bitsize (obsolete) */
1577          false,                 /* pc_relative */
1578          0,                     /* bitpos */
1579          false,                 /* absolute (obsolete) */
1580          true,                  /* complain_on_overflow */
1581          ecoff_gprel_reloc,     /* special_function */
1582          "LITERAL",             /* name */
1583          true,                  /* partial_inplace */
1584          0xffff,                /* src_mask */
1585          0xffff,                /* dst_mask */
1586          false)                 /* pcrel_offset */
1587 };
1588
1589 /* Read in the relocs for a section.  */
1590
1591 static boolean
1592 DEFUN (ecoff_slurp_reloc_table, (abfd, section, symbols),
1593        bfd *abfd AND
1594        asection *section AND
1595        asymbol **symbols)
1596 {
1597   RELOC *external_relocs;
1598   arelent *internal_relocs;
1599   arelent *rptr;
1600   unsigned int i;
1601
1602   if (section->relocation != (arelent *) NULL
1603       || section->reloc_count == 0
1604       || (section->flags & SEC_CONSTRUCTOR) != 0)
1605     return true;
1606
1607   if (ecoff_slurp_symbol_table (abfd) == false)
1608     return false;
1609   
1610   internal_relocs = (arelent *) bfd_alloc (abfd,
1611                                            (sizeof (arelent)
1612                                             * section->reloc_count));
1613   external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
1614   if (internal_relocs == (arelent *) NULL
1615       || external_relocs == (RELOC *) NULL)
1616     {
1617       bfd_error = no_memory;
1618       return false;
1619     }
1620   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1621     return false;
1622   if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
1623       != RELSZ * section->reloc_count)
1624     {
1625       bfd_error = system_call_error;
1626       return false;
1627     }
1628
1629   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1630     {
1631       struct internal_reloc intern;
1632
1633       ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
1634
1635       if (intern.r_type < 0 || intern.r_type > ECOFF_R_LITERAL)
1636         abort ();
1637
1638       if (intern.r_extern)
1639         {
1640           /* r_symndx is an index into the external symbols.  */
1641           BFD_ASSERT (intern.r_symndx >= 0
1642                       && (intern.r_symndx
1643                           < ecoff_data (abfd)->symbolic_header.iextMax));
1644           rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1645         }
1646       else
1647         {
1648           CONST char *sec_name;
1649           asection *sec;
1650
1651           /* r_symndx is a section key.  */
1652           switch (intern.r_symndx)
1653             {
1654             case RELOC_SECTION_TEXT:  sec_name = ".text";  break;
1655             case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1656             case RELOC_SECTION_DATA:  sec_name = ".data";  break;
1657             case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1658             case RELOC_SECTION_SBSS:  sec_name = ".sbss";  break;
1659             case RELOC_SECTION_BSS:   sec_name = ".bss";   break;
1660             case RELOC_SECTION_INIT:  sec_name = ".init";  break;
1661             case RELOC_SECTION_LIT8:  sec_name = ".lit8";  break;
1662             case RELOC_SECTION_LIT4:  sec_name = ".lit4";  break;
1663             default: abort ();
1664             }
1665
1666           sec = bfd_get_section_by_name (abfd, sec_name);
1667           if (sec == (asection *) NULL)
1668             abort ();
1669           rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1670         }
1671
1672       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1673       rptr->addend = 0;
1674       rptr->howto = &ecoff_howto_table[intern.r_type];
1675
1676       /* If the type is ECOFF_R_IGNORE, make sure this is a reference
1677          to the absolute section so that the reloc is ignored.  */
1678       if (intern.r_type == ECOFF_R_IGNORE)
1679         rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1680     }
1681
1682   bfd_release (abfd, external_relocs);
1683
1684   section->relocation = internal_relocs;
1685
1686   return true;
1687 }
1688
1689 /* Get a canonical list of relocs.  */
1690
1691 static unsigned int
1692 DEFUN (ecoff_canonicalize_reloc, (abfd, section, relptr, symbols),
1693        bfd *abfd AND
1694        asection *section AND
1695        arelent **relptr AND
1696        asymbol **symbols)
1697 {
1698   unsigned int count;
1699
1700   if (section->flags & SEC_CONSTRUCTOR) 
1701     {
1702       arelent_chain *chain;
1703
1704       /* This section has relocs made up by us, not the file, so take
1705          them out of their chain and place them into the data area
1706          provided.  */
1707       for (count = 0, chain = section->constructor_chain;
1708            count < section->reloc_count;
1709            count++, chain = chain->next)
1710         *relptr++ = &chain->relent;
1711     }
1712   else 
1713     { 
1714       arelent *tblptr;
1715
1716       if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1717         return 0;
1718
1719       tblptr = section->relocation;
1720       if (tblptr == (arelent *) NULL)
1721         return 0;
1722
1723       for (count = 0; count < section->reloc_count; count++)
1724         *relptr++ = tblptr++;
1725     }
1726
1727   *relptr = (arelent *) NULL;
1728
1729   return section->reloc_count;
1730 }
1731 \f
1732 /* Provided a BFD, a section and an offset into the section, calculate
1733    and return the name of the source file and the line nearest to the
1734    wanted location.  */
1735
1736 static boolean
1737 DEFUN (ecoff_find_nearest_line, (abfd,
1738                                  section,
1739                                  ignore_symbols,
1740                                  offset,
1741                                  filename_ptr,
1742                                  functionname_ptr,
1743                                  retline_ptr),
1744        bfd *abfd AND
1745        asection *section AND
1746        asymbol **ignore_symbols AND
1747        bfd_vma offset AND
1748        CONST char **filename_ptr AND
1749        CONST char **functionname_ptr AND
1750        unsigned int *retline_ptr)
1751 {
1752   FDR *fdr_ptr;
1753   FDR *fdr_start;
1754   FDR *fdr_end;
1755   FDR *fdr_hold;
1756   struct pdr_ext *pdr_ptr;
1757   struct pdr_ext *pdr_end;
1758   PDR pdr;
1759   unsigned char *line_ptr;
1760   unsigned char *line_end;
1761   int lineno;
1762   SYMR proc_sym;
1763
1764   /* If we're not in the .text section, we don't have any line
1765      numbers.  */
1766   if (strcmp (section->name, _TEXT) != 0)
1767     return false;
1768
1769   /* Make sure we have the FDR's.  */
1770   if (ecoff_slurp_symbolic_info (abfd) == false
1771       || bfd_get_symcount (abfd) == 0)
1772     return false;
1773
1774   /* Each file descriptor (FDR) has a memory address.  Here we track
1775      down which FDR we want.  The FDR's are stored in increasing
1776      memory order.  If speed is ever important, this can become a
1777      binary search.  We must ignore FDR's with no PDR entries; they
1778      will have the adr of the FDR before or after them.  */
1779   fdr_start = ecoff_data (abfd)->fdr;
1780   fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
1781   fdr_hold = (FDR *) NULL;
1782   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1783     {
1784       if (offset < fdr_ptr->adr)
1785         break;
1786       if (fdr_ptr->cpd > 0)
1787         fdr_hold = fdr_ptr;
1788     }
1789   if (fdr_hold == (FDR *) NULL)
1790     return false;
1791   fdr_ptr = fdr_hold;
1792
1793   /* Each FDR has a list of procedure descriptors (PDR).  PDR's also
1794      have an address, which is relative to the FDR address, and are
1795      also stored in increasing memory order.  */
1796   offset -= fdr_ptr->adr;
1797   pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
1798   pdr_end = pdr_ptr + fdr_ptr->cpd;
1799   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1800   if (offset < pdr.adr)
1801     return false;
1802   for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
1803     {
1804       ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1805       if (offset < pdr.adr)
1806         break;
1807     }
1808
1809   /* Now we can look for the actual line number.  The line numbers are
1810      stored in a very funky format, which I won't try to describe.
1811      Note that right here pdr_ptr and pdr hold the PDR *after* the one
1812      we want; we need this to compute line_end.  */
1813   line_end = ecoff_data (abfd)->line;
1814   if (pdr_ptr == pdr_end)
1815     line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
1816   else
1817     line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
1818
1819   /* Now change pdr and pdr_ptr to the one we want.  */
1820   pdr_ptr--;
1821   ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1822
1823   offset -= pdr.adr;
1824   lineno = pdr.lnLow;
1825   line_ptr = (ecoff_data (abfd)->line
1826               + fdr_ptr->cbLineOffset
1827               + pdr.cbLineOffset);
1828   while (line_ptr < line_end)
1829     {
1830       int delta;
1831       int count;
1832
1833       delta = *line_ptr >> 4;
1834       if (delta >= 0x8)
1835         delta -= 0x10;
1836       count = (*line_ptr & 0xf) + 1;
1837       ++line_ptr;
1838       if (delta == -8)
1839         {
1840           delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
1841           if (delta >= 0x8000)
1842             delta -= 0x10000;
1843           line_ptr += 2;
1844         }
1845       lineno += delta;
1846       if (offset < count * 4)
1847         break;
1848       offset -= count * 4;
1849     }
1850
1851   /* If offset is too large, this line is not interesting.  */
1852   if (offset > 100)
1853     return false;
1854
1855   *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
1856   ecoff_swap_sym_in (abfd,
1857                      (ecoff_data (abfd)->external_sym
1858                       + fdr_ptr->isymBase
1859                       + pdr.isym),
1860                      &proc_sym);
1861   *functionname_ptr = ecoff_data (abfd)->ss + proc_sym.iss;
1862   *retline_ptr = lineno;
1863   return true;
1864 }
1865 \f
1866 /* We can't use the generic linking routines for ECOFF, because we
1867    have to handle all the debugging information.  The generic link
1868    routine just works out the section contents and attaches a list of
1869    symbols.
1870
1871    We link by looping over all the seclets.  We make two passes.  On
1872    the first we set the actual section contents and determine the size
1873    of the debugging information.  On the second we accumulate the
1874    debugging information and write it out.
1875
1876    This currently always accumulates the debugging information, which
1877    is incorrect, because it ignores the -s and -S options of the
1878    linker.  The linker needs to be modified to give us that
1879    information in a more useful format (currently it just provides a
1880    list of symbols which should appear in the output file).  */
1881
1882 /* Clear the output_has_begun flag for all the input BFD's.  We use it
1883    to avoid linking in the debugging information for a BFD more than
1884    once.  */
1885
1886 static void
1887 DEFUN (ecoff_clear_output_flags, (abfd),
1888        bfd *abfd)
1889 {
1890   register asection *o;
1891   register bfd_seclet_type *p;
1892
1893   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1894     for (p = o->seclets_head;
1895          p != (bfd_seclet_type *) NULL;
1896          p = p->next)
1897       if (p->type == bfd_indirect_seclet)
1898         p->u.indirect.section->owner->output_has_begun = false;
1899 }
1900
1901 /* Handle an indirect seclet on the first pass.  Set the contents of
1902    the output section, and accumulate the debugging information if
1903    any.  */
1904
1905 static boolean
1906 DEFUN (ecoff_rel, (output_bfd, seclet, output_section, data, relocateable),
1907        bfd *output_bfd AND
1908        bfd_seclet_type *seclet AND
1909        asection *output_section AND
1910        PTR data AND
1911        boolean relocateable)
1912 {
1913   bfd *input_bfd;
1914   HDRR *output_symhdr;
1915   HDRR *input_symhdr;
1916
1917   if ((output_section->flags & SEC_HAS_CONTENTS)
1918       && !(output_section->flags & SEC_NEVER_LOAD)
1919       && (output_section->flags & SEC_LOAD)
1920       && seclet->size)
1921     {
1922       data = (PTR) bfd_get_relocated_section_contents (output_bfd,
1923                                                        seclet,
1924                                                        data,
1925                                                        relocateable);
1926       if (bfd_set_section_contents (output_bfd,
1927                                     output_section,
1928                                     data,
1929                                     seclet->offset,
1930                                     seclet->size)
1931           == false)
1932         {
1933           abort();
1934         }
1935     }
1936
1937   input_bfd = seclet->u.indirect.section->owner;
1938
1939   /* We want to figure out how much space will be required to
1940      incorporate all the debugging information from input_bfd.  We use
1941      the output_has_begun field to avoid adding it in more than once.
1942      The actual incorporation is done in the second pass, in
1943      ecoff_get_debug.  The code has to parallel that code in its
1944      manipulations of output_symhdr.  */
1945
1946   if (input_bfd->output_has_begun)
1947     return true;
1948   input_bfd->output_has_begun = true;
1949
1950   output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
1951
1952   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
1953     {
1954       asymbol **symbols;
1955       asymbol **sym_ptr;
1956       asymbol **sym_end;
1957
1958       /* We just accumulate symbols from a non-ECOFF BFD.  */
1959
1960       symbols = (asymbol **) bfd_alloc (output_bfd,
1961                                         get_symtab_upper_bound (input_bfd));
1962       if (symbols == (asymbol **) NULL)
1963         {
1964           bfd_error = no_memory;
1965           return false;
1966         }
1967       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
1968
1969       for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
1970         {
1971           size_t len;
1972
1973           len = strlen ((*sym_ptr)->name);
1974           if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
1975             {
1976               ++output_symhdr->isymMax;
1977               output_symhdr->issMax += len + 1;
1978             }
1979           else
1980             {
1981               ++output_symhdr->iextMax;
1982               output_symhdr->issExtMax += len + 1;
1983             }
1984         }
1985
1986       bfd_release (output_bfd, (PTR) symbols);
1987
1988       ++output_symhdr->ifdMax;
1989
1990       return true;
1991     }
1992
1993   /* We simply add in the information from another ECOFF BFD.  First
1994      we make sure we have the symbolic information.  */
1995   if (ecoff_slurp_symbolic_info (input_bfd) == false)
1996     return false;
1997   if (bfd_get_symcount (input_bfd) == 0)
1998     return true;
1999
2000   input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2001
2002   output_symhdr->ilineMax += input_symhdr->ilineMax;
2003   output_symhdr->cbLine += input_symhdr->cbLine;
2004   output_symhdr->idnMax += input_symhdr->idnMax;
2005   output_symhdr->ipdMax += input_symhdr->ipdMax;
2006   output_symhdr->isymMax += input_symhdr->isymMax;
2007   output_symhdr->ioptMax += input_symhdr->ioptMax;
2008   output_symhdr->iauxMax += input_symhdr->iauxMax;
2009   output_symhdr->issMax += input_symhdr->issMax;
2010   output_symhdr->issExtMax += input_symhdr->issExtMax;
2011   output_symhdr->ifdMax += input_symhdr->ifdMax;
2012   output_symhdr->iextMax += input_symhdr->iextMax;
2013
2014   /* The RFD's are special, since we create them if needed.  */
2015   if (input_symhdr->crfd > 0)
2016     output_symhdr->crfd += input_symhdr->crfd;
2017   else
2018     output_symhdr->crfd += input_symhdr->ifdMax;
2019
2020   return true;
2021 }
2022
2023 /* Handle an arbitrary seclet on the first pass.  */
2024
2025 static boolean
2026 DEFUN (ecoff_dump_seclet, (abfd, seclet, section, data, relocateable),
2027        bfd *abfd AND
2028        bfd_seclet_type *seclet AND
2029        asection *section AND
2030        PTR data AND
2031        boolean relocateable)
2032 {
2033   switch (seclet->type) 
2034     {
2035     case bfd_indirect_seclet:
2036       /* The contents of this section come from another one somewhere
2037          else.  */
2038       return ecoff_rel (abfd, seclet, section, data, relocateable);
2039
2040     case bfd_fill_seclet:
2041       /* Fill in the section with us.  */
2042       {
2043         char *d = (char *) bfd_alloc (abfd, seclet->size);
2044         unsigned int i;
2045         boolean ret;
2046
2047         for (i = 0; i < seclet->size; i+=2)
2048           d[i] = seclet->u.fill.value >> 8;
2049         for (i = 1; i < seclet->size; i+=2)
2050           d[i] = seclet->u.fill.value;
2051         ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2052                                         seclet->size);
2053         bfd_release (abfd, (PTR) d);
2054         return ret;
2055       }
2056
2057     default:
2058       abort();
2059     }
2060
2061   return true;
2062 }
2063
2064 /* Add a string to the debugging information we are accumulating for a
2065    file.  Return the offset from the fdr string base or from the
2066    external string base.  */
2067
2068 static long
2069 DEFUN (ecoff_add_string, (output_bfd, fdr, string, external),
2070        bfd *output_bfd AND
2071        FDR *fdr AND
2072        CONST char *string AND
2073        boolean external)
2074 {
2075   HDRR *symhdr;
2076   size_t len;
2077   long ret;
2078
2079   symhdr = &ecoff_data (output_bfd)->symbolic_header;
2080   len = strlen (string);
2081   if (external)
2082     {
2083       strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2084       ret = symhdr->issExtMax;
2085       symhdr->issExtMax += len + 1;
2086     }
2087   else
2088     {
2089       strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2090       ret = fdr->cbSs;
2091       symhdr->issMax += len + 1;
2092       fdr->cbSs += len + 1;
2093     }
2094   return ret;
2095 }
2096
2097 /* Accumulate the debugging information from an input section.  */
2098
2099 static boolean
2100 DEFUN (ecoff_get_debug, (output_bfd, seclet, section),
2101        bfd *output_bfd AND
2102        bfd_seclet_type *seclet AND
2103        asection *section)
2104 {
2105   bfd *input_bfd;
2106   HDRR *output_symhdr;
2107   HDRR *input_symhdr;
2108   ecoff_data_type *output_ecoff;
2109   ecoff_data_type *input_ecoff;
2110   FDR *fdr_ptr;
2111   FDR *fdr_end;
2112   struct fdr_ext *fdr_out;
2113   struct ext_ext *ext_ptr;
2114   struct ext_ext *ext_end;
2115   struct ext_ext *ext_out;
2116   long iss;
2117   long isym;
2118   long iline;
2119   long iopt;
2120   long ipdr;
2121   long iaux;
2122   long irfd;
2123   long cbline;
2124
2125   input_bfd = seclet->u.indirect.section->owner;
2126
2127   /* Don't get the information more than once. */
2128   if (input_bfd->output_has_begun)
2129     return true;
2130   input_bfd->output_has_begun = true;
2131
2132   output_ecoff = ecoff_data (output_bfd);
2133   output_symhdr = &output_ecoff->symbolic_header;
2134
2135   if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2136     {
2137       FDR fdr;
2138       asymbol **symbols;
2139       asymbol **sym_ptr;
2140       asymbol **sym_end;
2141
2142       /* This is not an ECOFF BFD.  Just gather the symbols.  */
2143
2144       memset (&fdr, 0, sizeof fdr);
2145
2146       fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2147       fdr.issBase = output_symhdr->issMax;
2148       fdr.cbSs = 0;
2149       fdr.rss = ecoff_add_string (output_bfd,
2150                                   &fdr,
2151                                   bfd_get_filename (input_bfd),
2152                                   false);
2153       fdr.isymBase = output_symhdr->isymMax;
2154
2155       /* Get the symbols from the input BFD.  */
2156       symbols = (asymbol **) bfd_alloc (output_bfd,
2157                                         get_symtab_upper_bound (input_bfd));
2158       if (symbols == (asymbol **) NULL)
2159         {
2160           bfd_error = no_memory;
2161           return false;
2162         }
2163       sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2164
2165       /* Handle the local symbols.  */
2166       fdr.csym = 0;
2167       for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2168         {
2169           SYMR internal_sym;
2170
2171           if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2172             continue;
2173           memset (&internal_sym, 0, sizeof internal_sym);
2174           internal_sym.iss = ecoff_add_string (output_bfd,
2175                                                &fdr,
2176                                                (*sym_ptr)->name,
2177                                                false);
2178           internal_sym.value = (*sym_ptr)->value;
2179           internal_sym.st = stNil;
2180           internal_sym.sc = scUndefined;
2181           internal_sym.index = indexNil;
2182           ecoff_swap_sym_out (output_bfd, &internal_sym,
2183                               (output_ecoff->external_sym
2184                                + output_symhdr->isymMax));
2185           ++fdr.csym;
2186           ++output_symhdr->isymMax;
2187         }
2188
2189       /* Handle the external symbols.  */
2190       for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2191         {
2192           EXTR internal_ext;
2193
2194           if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2195             continue;
2196           memset (&internal_ext, 0, sizeof internal_ext);
2197           internal_ext.ifd = output_symhdr->ifdMax;
2198           internal_ext.asym.iss = ecoff_add_string (output_bfd,
2199                                                     &fdr,
2200                                                     (*sym_ptr)->name,
2201                                                     true);
2202           internal_ext.asym.value = (*sym_ptr)->value;
2203           internal_ext.asym.st = stNil;
2204           internal_ext.asym.sc = scUndefined;
2205           internal_ext.asym.reserved = 0;
2206           internal_ext.asym.index = indexNil;
2207           ecoff_swap_ext_out (output_bfd, &internal_ext,
2208                               (output_ecoff->external_ext
2209                                + output_symhdr->iextMax));
2210           ++output_symhdr->iextMax;
2211         }
2212
2213       bfd_release (output_bfd, (PTR) symbols);
2214
2215       /* Leave everything else zeroed out.  This will cause the lang
2216          field to be langC.  The fBigendian field will indicate little
2217          endian format, but it doesn't matter because it only applies
2218          to aux fields and there are none.  */
2219
2220       ecoff_swap_fdr_out (output_bfd, &fdr,
2221                           (output_ecoff->external_fdr
2222                            + output_symhdr->ifdMax));
2223       ++output_symhdr->ifdMax;
2224       return true;
2225     }
2226
2227   /* This is an ECOFF BFD.  We want to grab the information from
2228      input_bfd and attach it to output_bfd.  */
2229   if (bfd_get_symcount (input_bfd) == 0)
2230     return true;
2231   input_ecoff = ecoff_data (input_bfd);
2232   input_symhdr = &input_ecoff->symbolic_header;
2233
2234   /* Because ECOFF uses relative pointers for most of the debugging
2235      information, much of it can simply be copied from input_bfd to
2236      output_bfd.  */
2237   memcpy (output_ecoff->line + output_symhdr->cbLineOffset,
2238           input_ecoff->line,
2239           input_symhdr->cbLine * sizeof (unsigned char));
2240   memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2241           input_ecoff->external_aux,
2242           input_symhdr->iauxMax * sizeof (union aux_ext));
2243   memcpy (output_ecoff->ss + output_symhdr->issMax,
2244           input_ecoff->ss,
2245           input_symhdr->issMax * sizeof (char));
2246   memcpy (output_ecoff->ssext + output_symhdr->issExtMax,
2247           input_ecoff->ssext,
2248           input_symhdr->issExtMax * sizeof (char));
2249
2250   /* Some of the information may need to be swapped.  */
2251   if (output_bfd->xvec->byteorder_big_p == input_bfd->xvec->byteorder_big_p)
2252     {
2253       /* The two BFD's have the same endianness, so memcpy will
2254          suffice.  */
2255       memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
2256               input_ecoff->external_dnr,
2257               input_symhdr->idnMax * sizeof (struct dnr_ext));
2258       memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
2259               input_ecoff->external_pdr,
2260               input_symhdr->ipdMax * sizeof (struct pdr_ext));
2261       memcpy (output_ecoff->external_sym + output_symhdr->isymMax,
2262               input_ecoff->external_sym,
2263               input_symhdr->isymMax * sizeof (struct sym_ext));
2264       memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
2265               input_ecoff->external_opt,
2266               input_symhdr->ioptMax * sizeof (struct opt_ext));
2267     }
2268   else
2269     {
2270       struct dnr_ext *dnr_in;
2271       struct dnr_ext *dnr_end;
2272       struct dnr_ext *dnr_out;
2273       struct pdr_ext *pdr_in;
2274       struct pdr_ext *pdr_end;
2275       struct pdr_ext *pdr_out;
2276       struct sym_ext *sym_in;
2277       struct sym_ext *sym_end;
2278       struct sym_ext *sym_out;
2279       struct opt_ext *opt_in;
2280       struct opt_ext *opt_end;
2281       struct opt_ext *opt_out;
2282
2283       /* The two BFD's have different endianness, so we must swap
2284          everything in and out.  This code would always work, but it
2285          would be very slow in the normal case.  */
2286       dnr_in = input_ecoff->external_dnr;
2287       dnr_end = dnr_in + input_symhdr->idnMax;
2288       dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
2289       for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
2290         {
2291           DNR dnr;
2292
2293           ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
2294           ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
2295         }
2296       pdr_in = input_ecoff->external_pdr;
2297       pdr_end = pdr_in + input_symhdr->ipdMax;
2298       pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
2299       for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
2300         {
2301           PDR pdr;
2302
2303           ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
2304           ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
2305         }
2306       sym_in = input_ecoff->external_sym;
2307       sym_end = sym_in + input_symhdr->isymMax;
2308       sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
2309       for (; sym_in < sym_end; sym_in++, sym_out++)
2310         {
2311           SYMR sym;
2312
2313           ecoff_swap_sym_in (input_bfd, sym_in, &sym);
2314           ecoff_swap_sym_out (output_bfd, &sym, sym_out);
2315         }
2316       opt_in = input_ecoff->external_opt;
2317       opt_end = opt_in + input_symhdr->ioptMax;
2318       opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
2319       for (; opt_in < opt_end; opt_in++, opt_out++)
2320         {
2321           OPTR opt;
2322
2323           ecoff_swap_opt_in (input_bfd, opt_in, &opt);
2324           ecoff_swap_opt_out (output_bfd, &opt, opt_out);
2325         }
2326     }
2327
2328   /* Step through the FDR's of input_bfd, adjust the offsets, and
2329      swap them out.  */
2330   iss = output_symhdr->issMax;
2331   isym = output_symhdr->isymMax;
2332   iline = output_symhdr->ilineMax;
2333   iopt = output_symhdr->ioptMax;
2334   ipdr = output_symhdr->ipdMax;
2335   iaux = output_symhdr->iauxMax;
2336   irfd = output_symhdr->crfd;
2337   cbline = output_symhdr->cbLineOffset;
2338
2339   fdr_ptr = input_ecoff->fdr;
2340   fdr_end = fdr_ptr + input_symhdr->ifdMax;
2341   fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
2342   for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
2343     {
2344       FDR fdr;
2345
2346       fdr = *fdr_ptr;
2347
2348       /* The memory address for this fdr is the address for the seclet
2349          plus the offset to this fdr within input_bfd.  */
2350       fdr.adr = (bfd_get_section_vma (output_bfd, section)
2351                  + seclet->offset
2352                  + (fdr_ptr->adr - input_ecoff->fdr->adr));
2353
2354       fdr.issBase = iss;
2355       iss += fdr.cbSs * sizeof (char);
2356       fdr.isymBase = isym;
2357       isym += fdr.csym * sizeof (struct sym_ext);
2358       fdr.ilineBase = iline;
2359       iline += fdr.cline;
2360       fdr.ioptBase = iopt;
2361       iopt += fdr.copt * sizeof (struct opt_ext);
2362       fdr.ipdFirst = ipdr;
2363       ipdr += fdr.cpd * sizeof (struct pdr_ext);
2364       fdr.iauxBase = iaux;
2365       iaux += fdr.caux * sizeof (union aux_ext);
2366       fdr.rfdBase = irfd;
2367       irfd += fdr.crfd * sizeof (struct rfd_ext);
2368       fdr.cbLineOffset = cbline;
2369       cbline += fdr.cbLine * sizeof (unsigned char);
2370
2371       ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
2372     }
2373
2374   if (input_symhdr->crfd > 0)
2375     {
2376       struct rfd_ext *rfd_in;
2377       struct rfd_ext *rfd_end;
2378       struct rfd_ext *rfd_out;
2379
2380       /* Swap and adjust the RFD's.  RFD's are only created by the
2381          linker, so this will only be necessary if one of the input
2382          files is the result of a partial link.  Presumably all
2383          necessary RFD's are present.  */
2384       rfd_in = input_ecoff->external_rfd;
2385       rfd_end = rfd_in + input_symhdr->crfd;
2386       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2387       for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
2388         {
2389           RFDT rfd;
2390
2391           ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
2392           rfd += output_symhdr->ifdMax;
2393           ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2394         }
2395       output_symhdr->crfd += input_symhdr->crfd;
2396     }
2397   else
2398     {
2399       struct rfd_ext *rfd_out;
2400       struct rfd_ext *rfd_end;
2401       RFDT rfd;
2402
2403       /* Create RFD's.  Some of the debugging information includes
2404          relative file indices.  These indices are taken as indices to
2405          the RFD table if there is one, or to the global table if
2406          there is not.  If we did not create RFD's, we would have to
2407          parse and adjust all the debugging information which contains
2408          file indices.  */
2409       rfd = output_symhdr->ifdMax;
2410       rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2411       rfd_end = rfd_out + input_symhdr->ifdMax;
2412       for (; rfd_out < rfd_end; rfd_out++, rfd++)
2413         ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2414       output_symhdr->crfd += input_symhdr->ifdMax;
2415     }
2416
2417   /* We have to swap the external symbols in and out because we have
2418      to adjust the file descriptor indices.  */
2419   ext_ptr = input_ecoff->external_ext;
2420   ext_end = ext_ptr + input_symhdr->iextMax;
2421   ext_out = output_ecoff->external_ext + output_symhdr->iextMax;
2422   for (; ext_ptr < ext_end; ext_ptr++, ext_out++)
2423     {
2424       EXTR ext;
2425
2426       ecoff_swap_ext_in (input_bfd, ext_ptr, &ext);
2427       ext.ifd += output_symhdr->ifdMax;
2428       ecoff_swap_ext_out (output_bfd, &ext, ext_out);
2429     }
2430
2431   /* Update the counts.  */
2432   output_symhdr->ilineMax += input_symhdr->ilineMax;
2433   output_symhdr->cbLine += input_symhdr->cbLine;
2434   output_symhdr->idnMax += input_symhdr->idnMax;
2435   output_symhdr->ipdMax += input_symhdr->ipdMax;
2436   output_symhdr->isymMax += input_symhdr->isymMax;
2437   output_symhdr->ioptMax += input_symhdr->ioptMax;
2438   output_symhdr->iauxMax += input_symhdr->iauxMax;
2439   output_symhdr->issMax += input_symhdr->issMax;
2440   output_symhdr->issExtMax += input_symhdr->issExtMax;
2441   output_symhdr->ifdMax += input_symhdr->ifdMax;
2442   output_symhdr->iextMax += input_symhdr->iextMax;
2443
2444   /* Double check that the counts we got by stepping through the FDR's
2445      match the counts we got from input_symhdr.  */
2446   BFD_ASSERT (output_symhdr->issMax == iss
2447               && output_symhdr->isymMax == isym
2448               && output_symhdr->ilineMax == iline
2449               && output_symhdr->ioptMax == iopt
2450               && output_symhdr->ipdMax == ipdr
2451               && output_symhdr->iauxMax == iaux
2452               && output_symhdr->cbLineOffset == cbline);
2453
2454   return true;
2455 }
2456
2457 /* This is the actual link routine.  It makes two passes over all the
2458    seclets.  */
2459
2460 static boolean
2461 DEFUN (ecoff_bfd_seclet_link, (abfd, data, relocateable),
2462        bfd *abfd AND
2463        PTR data AND
2464        boolean relocateable)
2465 {
2466   HDRR *symhdr;
2467   int ipass;
2468   register asection *o;
2469   register bfd_seclet_type *p;
2470   bfd_size_type size;
2471   char *raw;
2472
2473   /* We accumulate the debugging information counts in the symbolic
2474      header.  */
2475   symhdr = &ecoff_data (abfd)->symbolic_header;
2476   symhdr->ilineMax = 0;
2477   symhdr->cbLine = 0;
2478   symhdr->idnMax = 0;
2479   symhdr->ipdMax = 0;
2480   symhdr->isymMax = 0;
2481   symhdr->ioptMax = 0;
2482   symhdr->iauxMax = 0;
2483   symhdr->issMax = 0;
2484   symhdr->issExtMax = 0;
2485   symhdr->ifdMax = 0;
2486   symhdr->crfd = 0;
2487   symhdr->iextMax = 0;
2488
2489   /* We are only going to look at each input BFD once.  It is
2490      convenient to look at the code section first, so that the first
2491      time we look at a BFD we can set the text address (otherwise,
2492      when we get to the text section, we would have to be able to
2493      track down the file information associated with that BFD).  So we
2494      actually do each pass in two sub passes; first the code sections,
2495      then the non-code sections.  */
2496
2497   /* Do the first pass: set the output section contents and count the
2498      debugging information.  */
2499   ecoff_clear_output_flags (abfd);
2500   for (ipass = 0; ipass < 2; ipass++)
2501     {
2502       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2503         {
2504           /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
2505              so they are done on pass 0.  For other sections the
2506              expression is true, so they are done on pass 1.  */
2507           if (((o->flags & SEC_CODE) == 0) != ipass)
2508             continue;
2509
2510           for (p = o->seclets_head;
2511                p != (bfd_seclet_type *) NULL;
2512                p = p->next)
2513             {
2514               if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
2515                   == false)
2516                 return false;
2517             }
2518         }
2519     }
2520
2521   /* Now the counts in symhdr are the correct size for the debugging
2522      information.  We allocate the right amount of space, and reset
2523      the counts so that the second pass can use them as indices.  It
2524      would be possible to output the debugging information directly to
2525      the file in pass 2, rather than to build it in memory and then
2526      write it out.  Outputting to the file would require a lot of
2527      seeks and small writes, though, and I think this approach is
2528      faster.  */
2529   size = (symhdr->cbLine * sizeof (unsigned char)
2530           + symhdr->idnMax * sizeof (struct dnr_ext)
2531           + symhdr->ipdMax * sizeof (struct pdr_ext)
2532           + symhdr->isymMax * sizeof (struct sym_ext)
2533           + symhdr->ioptMax * sizeof (struct opt_ext)
2534           + symhdr->iauxMax * sizeof (union aux_ext)
2535           + symhdr->issMax * sizeof (char)
2536           + symhdr->issExtMax * sizeof (char)
2537           + symhdr->ifdMax * sizeof (struct fdr_ext)
2538           + symhdr->crfd * sizeof (struct rfd_ext)
2539           + symhdr->iextMax * sizeof (struct ext_ext));
2540   raw = (char *) bfd_alloc (abfd, size);
2541   if (raw == (char *) NULL)
2542     {
2543       bfd_error = no_memory;
2544       return false;
2545     }
2546   ecoff_data (abfd)->raw_size = size;
2547   ecoff_data (abfd)->raw_syments = (PTR) raw;
2548
2549   /* Initialize the raw pointers.  */
2550 #define SET(field, count, type) \
2551   ecoff_data (abfd)->field = (type *) raw; \
2552   raw += symhdr->count * sizeof (type)
2553
2554   SET (line, cbLine, unsigned char);
2555   SET (external_dnr, idnMax, struct dnr_ext);
2556   SET (external_pdr, ipdMax, struct pdr_ext);
2557   SET (external_sym, isymMax, struct sym_ext);
2558   SET (external_opt, ioptMax, struct opt_ext);
2559   SET (external_aux, iauxMax, union aux_ext);
2560   SET (ss, issMax, char);
2561   SET (ssext, issExtMax, char);
2562   SET (external_fdr, ifdMax, struct fdr_ext);
2563   SET (external_rfd, crfd, struct rfd_ext);
2564   SET (external_ext, iextMax, struct ext_ext);
2565 #undef SET
2566
2567   /* Reset the counts so the second pass can use them to know how far
2568      it has gotten.  */
2569   symhdr->ilineMax = 0;
2570   symhdr->cbLine = 0;
2571   symhdr->idnMax = 0;
2572   symhdr->ipdMax = 0;
2573   symhdr->isymMax = 0;
2574   symhdr->ioptMax = 0;
2575   symhdr->iauxMax = 0;
2576   symhdr->issMax = 0;
2577   symhdr->issExtMax = 0;
2578   symhdr->ifdMax = 0;
2579   symhdr->crfd = 0;
2580   symhdr->iextMax = 0;
2581
2582   /* Do the second pass: accumulate the debugging information.  */
2583   ecoff_clear_output_flags (abfd);
2584   for (ipass = 0; ipass < 2; ipass++)
2585     {
2586       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2587         {
2588           if (((o->flags & SEC_CODE) == 0) != ipass)
2589             continue;
2590           for (p = o->seclets_head;
2591                p != (bfd_seclet_type *) NULL;
2592                p = p->next)
2593             {
2594               if (p->type == bfd_indirect_seclet)
2595                 {
2596                   if (ecoff_get_debug (abfd, p, o) == false)
2597                     return false;
2598                 }
2599             }
2600         }
2601     }
2602
2603   return true;
2604 }
2605 \f
2606 /* Set the architecture.  The only architecture we support here is
2607    mips.  We set the architecture anyhow, since many callers ignore
2608    the return value.  */
2609
2610 static boolean
2611 DEFUN (ecoff_set_arch_mach, (abfd, arch, machine),
2612        bfd *abfd AND
2613        enum bfd_architecture arch AND
2614        unsigned long machine)
2615 {
2616   bfd_default_set_arch_mach (abfd, arch, machine);
2617   return arch == bfd_arch_mips;
2618 }
2619
2620 /* Calculate the file position for each section, and set
2621    reloc_filepos.  */
2622
2623 static void
2624 DEFUN (ecoff_compute_section_file_positions, (abfd),
2625        bfd *abfd)
2626 {
2627   asection *current;
2628   asection *previous;
2629   file_ptr sofar;
2630   file_ptr old_sofar;
2631
2632   sofar = FILHSZ;
2633
2634   if (bfd_get_start_address (abfd)) 
2635     {
2636       /* A start address may have been added to the original file. In
2637          this case it will need an optional header to record it.  */
2638       abfd->flags |= EXEC_P;
2639     }
2640
2641   if (abfd->flags & EXEC_P)
2642     sofar += AOUTSZ;
2643
2644   sofar += abfd->section_count * SCNHSZ;
2645
2646   previous = (asection *) NULL;
2647   for (current = abfd->sections;
2648        current != (asection *) NULL;
2649        current = current->next)
2650     {
2651       /* Only deal with sections which have contents */
2652       if (! (current->flags & SEC_HAS_CONTENTS))
2653         continue;
2654
2655       /* Align the sections in the file to the same boundary on
2656          which they are aligned in virtual memory.  */
2657       old_sofar = sofar;
2658       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2659       if (previous != (asection *) NULL)
2660         previous->_raw_size += sofar - old_sofar;
2661
2662       current->filepos = sofar;
2663
2664       sofar += current->_raw_size;
2665
2666       /* make sure that this section is of the right size too */
2667       old_sofar = sofar;
2668       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2669       current->_raw_size += sofar - old_sofar;
2670       previous = current;
2671     }
2672
2673   ecoff_data (abfd)->reloc_filepos = sofar;
2674 }
2675
2676 /* Set the contents of a section.  */
2677
2678 static boolean
2679 DEFUN (ecoff_set_section_contents, (abfd, section, location, offset, count),
2680        bfd *abfd AND
2681        asection *section AND
2682        PTR location AND
2683        file_ptr offset AND
2684        bfd_size_type count)
2685 {
2686   if (abfd->output_has_begun == false)
2687     ecoff_compute_section_file_positions (abfd);
2688
2689   bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
2690
2691   if (count != 0)
2692     return (bfd_write (location, 1, count, abfd) == count) ? true : false;
2693
2694   return true;
2695 }
2696
2697 /* Write out an ECOFF file.  */
2698
2699 static boolean
2700 DEFUN (ecoff_write_object_contents, (abfd),
2701        bfd *abfd)
2702 {
2703   asection *current;
2704   unsigned int count;
2705   file_ptr scn_base;
2706   file_ptr reloc_base;
2707   file_ptr sym_base;
2708   unsigned long reloc_size;
2709   unsigned long text_size;
2710   unsigned long text_start;
2711   unsigned long data_size;
2712   unsigned long data_start;
2713   unsigned long bss_size;
2714   struct internal_filehdr internal_f;
2715   struct internal_aouthdr internal_a;
2716
2717   bfd_error = system_call_error;
2718
2719   if(abfd->output_has_begun == false)
2720     ecoff_compute_section_file_positions(abfd);
2721
2722   if (abfd->sections != (asection *) NULL)
2723     scn_base = abfd->sections->filepos;
2724   else
2725     scn_base = 0;
2726   reloc_base = ecoff_data (abfd)->reloc_filepos;
2727
2728   count = 1;
2729   reloc_size = 0;
2730   for (current = abfd->sections;
2731        current != (asection *)NULL; 
2732        current = current->next) 
2733     {
2734       current->target_index = count;
2735       ++count;
2736       if (current->reloc_count != 0)
2737         {
2738           bfd_size_type relsize;
2739
2740           current->rel_filepos = reloc_base;
2741           relsize = current->reloc_count * RELSZ;
2742           reloc_size += relsize;
2743           reloc_base += relsize;
2744         }
2745       else
2746         current->rel_filepos = 0;
2747     }
2748
2749   sym_base = reloc_base + reloc_size;
2750   ecoff_data (abfd)->sym_filepos = sym_base;
2751
2752   text_size = 0;
2753   text_start = 0;
2754   data_size = 0;
2755   data_start = 0;
2756   bss_size = 0;
2757
2758   /* Write section headers to the file.  */
2759
2760   internal_f.f_nscns = 0;
2761   if (bfd_seek (abfd,
2762                 (file_ptr) ((abfd->flags & EXEC_P) ?
2763                             (FILHSZ + AOUTSZ) : FILHSZ),
2764                 SEEK_SET) != 0)
2765     return false;
2766   for (current = abfd->sections;
2767        current != (asection *) NULL;
2768        current = current->next)
2769     {
2770       struct internal_scnhdr section;
2771       bfd_vma vma;
2772
2773       ++internal_f.f_nscns;
2774
2775       strncpy (section.s_name, current->name, sizeof section.s_name);
2776
2777       /* FIXME: is this correct for shared libraries?  I think it is
2778          but I have no platform to check.  Ian Lance Taylor.  */
2779       vma = bfd_get_section_vma (abfd, current);
2780       if (strcmp (current->name, _LIB) == 0)
2781         section.s_vaddr = 0;
2782       else
2783         section.s_vaddr = vma;
2784
2785       section.s_paddr = vma;
2786       section.s_size = bfd_get_section_size_before_reloc (current);
2787
2788       /* If this section has no size or is unloadable then the scnptr
2789          will be 0 too.  */
2790       if (current->_raw_size == 0
2791           || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2792         section.s_scnptr = 0;
2793       else
2794         section.s_scnptr = current->filepos;
2795       section.s_relptr = current->rel_filepos;
2796
2797       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2798          object file produced by the assembler is supposed to point to
2799          information about how much room is required by objects of
2800          various different sizes.  I think this only matters if we
2801          want the linker to compute the best size to use, or
2802          something.  I don't know what happens if the information is
2803          not present.  */
2804       section.s_lnnoptr = 0;
2805
2806       section.s_nreloc = current->reloc_count;
2807       section.s_nlnno = 0;
2808       section.s_flags = sec_to_styp_flags (current->name, current->flags);
2809
2810       {
2811         SCNHDR buff;
2812
2813         ecoff_swap_scnhdr_out (abfd, (PTR) &section, (PTR) &buff);
2814         if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
2815           return false;
2816       }
2817
2818       /* FIXME: These numbers don't add up to what the MIPS tools
2819          produce, although I don't think it matters.  */
2820       if ((section.s_flags & STYP_TEXT) != 0
2821           || (section.s_flags & STYP_RDATA) != 0
2822           || (section.s_flags & STYP_LIT8) != 0
2823           || (section.s_flags & STYP_LIT4) != 0)
2824         {
2825           text_size += bfd_get_section_size_before_reloc (current);
2826           if (text_start == 0 || text_start > vma)
2827             text_start = vma;
2828         }
2829       else if ((section.s_flags & STYP_DATA) != 0
2830                || (section.s_flags & STYP_SDATA) != 0)
2831         {
2832           data_size += bfd_get_section_size_before_reloc (current);
2833           if (data_start == 0 || data_start > vma)
2834             data_start = vma;
2835         }
2836       else if ((section.s_flags & STYP_BSS) != 0
2837                || (section.s_flags & STYP_SBSS) != 0)
2838         bss_size += bfd_get_section_size_before_reloc (current);
2839     }   
2840
2841   /* Set up the file header.  */
2842
2843   internal_f.f_magic = MIPS_MAGIC_2;
2844
2845   /*
2846     We will NOT put a fucking timestamp in the header here. Every time you
2847     put it back, I will come in and take it out again.  I'm sorry.  This
2848     field does not belong here.  We fill it with a 0 so it compares the
2849     same but is not a reasonable time. -- gnu@cygnus.com
2850     */
2851   internal_f.f_timdat = 0;
2852
2853   internal_f.f_nsyms =  bfd_get_symcount(abfd);
2854   if (internal_f.f_nsyms != 0)
2855     internal_f.f_symptr = sym_base;
2856   else
2857     internal_f.f_symptr = 0;
2858
2859   if (abfd->flags & EXEC_P)
2860     internal_f.f_opthdr = AOUTSZ;
2861   else
2862     internal_f.f_opthdr = 0;
2863
2864   internal_f.f_flags = 0;
2865   if (reloc_size == 0)
2866     internal_f.f_flags |= F_RELFLG;
2867   if (bfd_get_symcount (abfd) == 0)
2868     internal_f.f_flags |= F_LSYMS;
2869   if (abfd->flags & EXEC_P)
2870     internal_f.f_flags |= F_EXEC;
2871
2872   if (! abfd->xvec->byteorder_big_p)
2873     internal_f.f_flags |= F_AR32WR;
2874   else
2875     internal_f.f_flags |= F_AR32W;
2876
2877   /* Set up the optional header.  */
2878
2879   if ((abfd->flags & EXEC_P) != 0)
2880     {
2881       internal_a.magic = ZMAGIC;
2882
2883       /* FIXME: What should this be?  */
2884       internal_a.vstamp = 0;
2885
2886       internal_a.tsize = text_size;
2887       internal_a.text_start = text_start;
2888       internal_a.dsize = data_size;
2889       internal_a.data_start = data_start;
2890       internal_a.bsize = bss_size;
2891
2892       internal_a.entry = bfd_get_start_address (abfd);
2893
2894       /* FIXME: The MIPS optional header is larger than this....  */
2895     }
2896
2897   /* Write out the file header and the optional header.  */
2898
2899   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2900     return false;
2901
2902   {
2903     FILHDR buff;
2904     ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
2905     if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
2906       return false;
2907   }
2908
2909   if ((abfd->flags & EXEC_P) != 0)
2910     {
2911       AOUTHDR buff;
2912
2913       ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
2914       if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
2915         return false;
2916     }
2917
2918   /* Write out the relocs.  */
2919
2920   /* Write out the symbolic debugging information.  */
2921   if (bfd_get_symcount (abfd) > 0)
2922     {
2923       struct hdr_ext buff;
2924
2925       ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
2926       if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
2927         return false;
2928       if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
2929                      ecoff_data (abfd)->raw_size, abfd)
2930           != ecoff_data (abfd)->raw_size)
2931         return false;
2932     }
2933
2934   return true;
2935 }
2936 \f
2937 static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
2938   (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
2939   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2940   (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2941   (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
2942   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2943   (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2944   ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
2945   ecoff_swap_scnhdr_out,
2946   FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
2947   ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
2948   ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
2949   styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
2950   ecoff_slurp_symbol_table
2951 };
2952
2953 /* get_lineno could be written for ECOFF, but it would currently only
2954    be useful for linking ECOFF and COFF files together, which doesn't
2955    seem too likely.  */
2956 #define ecoff_get_lineno \
2957   ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
2958
2959 #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
2960 #define ecoff_core_file_failing_signal  _bfd_dummy_core_file_failing_signal
2961 #define ecoff_core_file_matches_executable_p    _bfd_dummy_core_file_matches_executable_p
2962 #define ecoff_slurp_armap               bfd_slurp_coff_armap
2963 #define ecoff_slurp_extended_name_table _bfd_slurp_extended_name_table
2964 #define ecoff_write_armap               coff_write_armap
2965 #define ecoff_truncate_arname           bfd_dont_truncate_arname
2966 #define ecoff_openr_next_archived_file  bfd_generic_openr_next_archived_file
2967 #define ecoff_generic_stat_arch_elt     bfd_generic_stat_arch_elt
2968 #define ecoff_get_section_contents      bfd_generic_get_section_contents
2969 #define ecoff_get_reloc_upper_bound     coff_get_reloc_upper_bound
2970 #define ecoff_close_and_cleanup         bfd_generic_close_and_cleanup
2971 #define ecoff_sizeof_headers            coff_sizeof_headers
2972 #define ecoff_bfd_debug_info_start      bfd_void
2973 #define ecoff_bfd_debug_info_end        bfd_void
2974 #define ecoff_bfd_debug_info_accumulate \
2975   ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
2976 #define ecoff_bfd_get_relocated_section_contents  bfd_generic_get_relocated_section_contents
2977 #define ecoff_bfd_relax_section         bfd_generic_relax_section
2978
2979 bfd_target ecoff_little_vec =
2980 {
2981   "ecoff-littlemips",           /* name */
2982   bfd_target_ecoff_flavour,
2983   false,                        /* data byte order is little */
2984   false,                        /* header byte order is little */
2985
2986   (HAS_RELOC | EXEC_P |         /* object flags */
2987    HAS_LINENO | HAS_DEBUG |
2988    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
2989
2990   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
2991                                                             flags */
2992   0,                            /* leading underscore */
2993   '/',                          /* ar_pad_char */
2994   15,                           /* ar_max_namelen */
2995   3,                            /* minimum alignment power */
2996   _do_getl64, _do_putl64,       _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
2997   _do_getl64, _do_putl64,       _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
2998
2999   {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3000    bfd_generic_archive_p, _bfd_dummy_target},
3001   {bfd_false, ecoff_mkobject, bfd_false, /* bfd_set_format */
3002    bfd_false},
3003   {bfd_false, ecoff_write_object_contents, bfd_false, bfd_false},
3004   JUMP_TABLE (ecoff),
3005   0, 0,
3006   (PTR) &bfd_ecoff_std_swap_table
3007 };
3008
3009 bfd_target ecoff_big_vec =
3010 {
3011   "ecoff-bigmips",              /* name */
3012   bfd_target_ecoff_flavour,
3013   true,                         /* data byte order is big */
3014   true,                         /* header byte order is big */
3015
3016   (HAS_RELOC | EXEC_P |         /* object flags */
3017    HAS_LINENO | HAS_DEBUG |
3018    HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
3019
3020   (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
3021   0,                            /* leading underscore */
3022   ' ',                          /* ar_pad_char */
3023   16,                           /* ar_max_namelen */
3024   3,                            /* minimum alignment power */
3025   _do_getb64, _do_putb64,       _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3026   _do_getb64, _do_putb64,       _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3027  {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3028    bfd_generic_archive_p, _bfd_dummy_target},
3029  {bfd_false, ecoff_mkobject, bfd_false, /* bfd_set_format */
3030    bfd_false},
3031  {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
3032    bfd_false, bfd_false},
3033   JUMP_TABLE(ecoff),
3034   0, 0,
3035   (PTR) &bfd_ecoff_std_swap_table
3036   /* Note that there is another bfd_target just above this one.  If
3037      you are adding initializers here, you should be adding them there
3038      as well.  */
3039 };