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