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