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