a826415a82659a75cddfcb5224f5b56e9ed7fab7
[platform/upstream/binutils.git] / bfd / ecoff.c
1 /* Generic ECOFF (Extended-COFF) routines.
2    Copyright 1990, 1991, 1993, 1994, 1995, 1996, 1998, 1999, 2000, 2001,
3    2002, 2003, 2004 Free Software Foundation, Inc.
4    Original version by Per Bothner.
5    Full support added by Ian Lance Taylor, ian@cygnus.com.
6
7    This file is part of BFD, the Binary File Descriptor library.
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 2 of the License, or
12    (at your option) any later version.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18
19    You should have received a copy of the GNU General Public License
20    along with this program; if not, write to the Free Software
21    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "aout/ar.h"
28 #include "aout/ranlib.h"
29 #include "aout/stab_gnu.h"
30
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32    some other stuff which we don't want and which conflicts with stuff
33    we do want.  */
34 #include "libaout.h"
35 #include "aout/aout64.h"
36 #undef N_ABS
37 #undef exec_hdr
38 #undef obj_sym_filepos
39
40 #include "coff/internal.h"
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
46 \f
47 /* Prototypes for static functions.  */
48
49 static int ecoff_get_magic
50   PARAMS ((bfd *));
51 static long ecoff_sec_to_styp_flags
52   PARAMS ((const char *, flagword));
53 static bfd_boolean ecoff_slurp_symbolic_header
54   PARAMS ((bfd *));
55 static bfd_boolean ecoff_set_symbol_info
56   PARAMS ((bfd *, SYMR *, asymbol *, int, int));
57 static void ecoff_emit_aggregate
58   PARAMS ((bfd *, FDR *, char *, RNDXR *, long, const char *));
59 static char *ecoff_type_to_string
60   PARAMS ((bfd *, FDR *, unsigned int));
61 static bfd_boolean ecoff_slurp_reloc_table
62   PARAMS ((bfd *, asection *, asymbol **));
63 static int ecoff_sort_hdrs
64   PARAMS ((const PTR, const PTR));
65 static bfd_boolean ecoff_compute_section_file_positions
66   PARAMS ((bfd *));
67 static bfd_size_type ecoff_compute_reloc_file_positions
68   PARAMS ((bfd *));
69 static bfd_boolean ecoff_get_extr
70   PARAMS ((asymbol *, EXTR *));
71 static void ecoff_set_index
72   PARAMS ((asymbol *, bfd_size_type));
73 static unsigned int ecoff_armap_hash
74   PARAMS ((const char *, unsigned int *, unsigned int, unsigned int));
75 \f
76 /* This stuff is somewhat copied from coffcode.h.  */
77 static asection bfd_debug_section =
78 {
79   /* name,      id,  index, next, flags, user_set_vma,             */
80      "*DEBUG*", 0,   0,     NULL, 0,     0,
81   /* linker_mark, linker_has_input, gc_mark, segment_mark,         */
82      0,           0,                0,       0,
83   /* sec_info_type, use_rela_p, has_tls_reloc, has_gp_reloc,       */
84      0,             0,          0,             0,
85   /* need_finalize_relax, reloc_done,                              */
86      0,                   0,
87   /* vma, lma, size, rawsize,                                      */
88      0,   0,   0,    0,
89   /* output_offset, output_section, alignment_power,               */
90      0,             NULL,           0,
91   /* relocation, orelocation, reloc_count, filepos, rel_filepos,   */
92      NULL,       NULL,        0,           0,       0,
93   /* line_filepos, userdata, contents, lineno, lineno_count,       */
94      0,            NULL,     NULL,     NULL,   0,
95   /* entsize, kept_section, moving_line_filepos,                   */
96      0,       NULL,         0,
97   /* target_index, used_by_bfd, constructor_chain, owner,          */
98      0,            NULL,        NULL,              NULL,
99   /* symbol,                                                       */
100      (struct bfd_symbol *) NULL,
101   /* symbol_ptr_ptr,                                               */
102      (struct bfd_symbol **) NULL,
103   /* link_order_head, link_order_tail                              */
104      NULL,            NULL
105 };
106
107 /* Create an ECOFF object.  */
108
109 bfd_boolean
110 _bfd_ecoff_mkobject (abfd)
111      bfd *abfd;
112 {
113   bfd_size_type amt = sizeof (ecoff_data_type);
114
115   abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
116   if (abfd->tdata.ecoff_obj_data == NULL)
117     return FALSE;
118
119   return TRUE;
120 }
121
122 /* This is a hook called by coff_real_object_p to create any backend
123    specific information.  */
124
125 PTR
126 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
127      bfd *abfd;
128      PTR filehdr;
129      PTR aouthdr;
130 {
131   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
132   struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
133   ecoff_data_type *ecoff;
134
135   if (! _bfd_ecoff_mkobject (abfd))
136     return NULL;
137
138   ecoff = ecoff_data (abfd);
139   ecoff->gp_size = 8;
140   ecoff->sym_filepos = internal_f->f_symptr;
141
142   if (internal_a != (struct internal_aouthdr *) NULL)
143     {
144       int i;
145
146       ecoff->text_start = internal_a->text_start;
147       ecoff->text_end = internal_a->text_start + internal_a->tsize;
148       ecoff->gp = internal_a->gp_value;
149       ecoff->gprmask = internal_a->gprmask;
150       for (i = 0; i < 4; i++)
151         ecoff->cprmask[i] = internal_a->cprmask[i];
152       ecoff->fprmask = internal_a->fprmask;
153       if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
154         abfd->flags |= D_PAGED;
155       else
156         abfd->flags &=~ D_PAGED;
157     }
158
159   /* It turns out that no special action is required by the MIPS or
160      Alpha ECOFF backends.  They have different information in the
161      a.out header, but we just copy it all (e.g., gprmask, cprmask and
162      fprmask) and let the swapping routines ensure that only relevant
163      information is written out.  */
164
165   return (PTR) ecoff;
166 }
167
168 /* Initialize a new section.  */
169
170 bfd_boolean
171 _bfd_ecoff_new_section_hook (abfd, section)
172      bfd *abfd ATTRIBUTE_UNUSED;
173      asection *section;
174 {
175   section->alignment_power = 4;
176
177   if (strcmp (section->name, _TEXT) == 0
178       || strcmp (section->name, _INIT) == 0
179       || strcmp (section->name, _FINI) == 0)
180     section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
181   else if (strcmp (section->name, _DATA) == 0
182            || strcmp (section->name, _SDATA) == 0)
183     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
184   else if (strcmp (section->name, _RDATA) == 0
185            || strcmp (section->name, _LIT8) == 0
186            || strcmp (section->name, _LIT4) == 0
187            || strcmp (section->name, _RCONST) == 0
188            || strcmp (section->name, _PDATA) == 0)
189     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
190   else if (strcmp (section->name, _BSS) == 0
191            || strcmp (section->name, _SBSS) == 0)
192     section->flags |= SEC_ALLOC;
193   else if (strcmp (section->name, _LIB) == 0)
194     /* An Irix 4 shared libary.  */
195     section->flags |= SEC_COFF_SHARED_LIBRARY;
196
197   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
198      uncertain about .init on some systems and I don't know how shared
199      libraries work.  */
200
201   return TRUE;
202 }
203
204 /* Determine the machine architecture and type.  This is called from
205    the generic COFF routines.  It is the inverse of ecoff_get_magic,
206    below.  This could be an ECOFF backend routine, with one version
207    for each target, but there aren't all that many ECOFF targets.  */
208
209 bfd_boolean
210 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
211      bfd *abfd;
212      PTR filehdr;
213 {
214   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
215   enum bfd_architecture arch;
216   unsigned long mach;
217
218   switch (internal_f->f_magic)
219     {
220     case MIPS_MAGIC_1:
221     case MIPS_MAGIC_LITTLE:
222     case MIPS_MAGIC_BIG:
223       arch = bfd_arch_mips;
224       mach = bfd_mach_mips3000;
225       break;
226
227     case MIPS_MAGIC_LITTLE2:
228     case MIPS_MAGIC_BIG2:
229       /* MIPS ISA level 2: the r6000.  */
230       arch = bfd_arch_mips;
231       mach = bfd_mach_mips6000;
232       break;
233
234     case MIPS_MAGIC_LITTLE3:
235     case MIPS_MAGIC_BIG3:
236       /* MIPS ISA level 3: the r4000.  */
237       arch = bfd_arch_mips;
238       mach = bfd_mach_mips4000;
239       break;
240
241     case ALPHA_MAGIC:
242       arch = bfd_arch_alpha;
243       mach = 0;
244       break;
245
246     default:
247       arch = bfd_arch_obscure;
248       mach = 0;
249       break;
250     }
251
252   return bfd_default_set_arch_mach (abfd, arch, mach);
253 }
254
255 /* Get the magic number to use based on the architecture and machine.
256    This is the inverse of _bfd_ecoff_set_arch_mach_hook, above.  */
257
258 static int
259 ecoff_get_magic (abfd)
260      bfd *abfd;
261 {
262   int big, little;
263
264   switch (bfd_get_arch (abfd))
265     {
266     case bfd_arch_mips:
267       switch (bfd_get_mach (abfd))
268         {
269         default:
270         case 0:
271         case bfd_mach_mips3000:
272           big = MIPS_MAGIC_BIG;
273           little = MIPS_MAGIC_LITTLE;
274           break;
275
276         case bfd_mach_mips6000:
277           big = MIPS_MAGIC_BIG2;
278           little = MIPS_MAGIC_LITTLE2;
279           break;
280
281         case bfd_mach_mips4000:
282           big = MIPS_MAGIC_BIG3;
283           little = MIPS_MAGIC_LITTLE3;
284           break;
285         }
286
287       return bfd_big_endian (abfd) ? big : little;
288
289     case bfd_arch_alpha:
290       return ALPHA_MAGIC;
291
292     default:
293       abort ();
294       return 0;
295     }
296 }
297
298 /* Get the section s_flags to use for a section.  */
299
300 static long
301 ecoff_sec_to_styp_flags (name, flags)
302      const char *name;
303      flagword flags;
304 {
305   long styp;
306
307   styp = 0;
308
309   if (strcmp (name, _TEXT) == 0)
310     styp = STYP_TEXT;
311   else if (strcmp (name, _DATA) == 0)
312     styp = STYP_DATA;
313   else if (strcmp (name, _SDATA) == 0)
314     styp = STYP_SDATA;
315   else if (strcmp (name, _RDATA) == 0)
316     styp = STYP_RDATA;
317   else if (strcmp (name, _LITA) == 0)
318     styp = STYP_LITA;
319   else if (strcmp (name, _LIT8) == 0)
320     styp = STYP_LIT8;
321   else if (strcmp (name, _LIT4) == 0)
322     styp = STYP_LIT4;
323   else if (strcmp (name, _BSS) == 0)
324     styp = STYP_BSS;
325   else if (strcmp (name, _SBSS) == 0)
326     styp = STYP_SBSS;
327   else if (strcmp (name, _INIT) == 0)
328     styp = STYP_ECOFF_INIT;
329   else if (strcmp (name, _FINI) == 0)
330     styp = STYP_ECOFF_FINI;
331   else if (strcmp (name, _PDATA) == 0)
332     styp = STYP_PDATA;
333   else if (strcmp (name, _XDATA) == 0)
334     styp = STYP_XDATA;
335   else if (strcmp (name, _LIB) == 0)
336     styp = STYP_ECOFF_LIB;
337   else if (strcmp (name, _GOT) == 0)
338     styp = STYP_GOT;
339   else if (strcmp (name, _HASH) == 0)
340     styp = STYP_HASH;
341   else if (strcmp (name, _DYNAMIC) == 0)
342     styp = STYP_DYNAMIC;
343   else if (strcmp (name, _LIBLIST) == 0)
344     styp = STYP_LIBLIST;
345   else if (strcmp (name, _RELDYN) == 0)
346     styp = STYP_RELDYN;
347   else if (strcmp (name, _CONFLIC) == 0)
348     styp = STYP_CONFLIC;
349   else if (strcmp (name, _DYNSTR) == 0)
350     styp = STYP_DYNSTR;
351   else if (strcmp (name, _DYNSYM) == 0)
352     styp = STYP_DYNSYM;
353   else if (strcmp (name, _COMMENT) == 0)
354     {
355       styp = STYP_COMMENT;
356       flags &=~ SEC_NEVER_LOAD;
357     }
358   else if (strcmp (name, _RCONST) == 0)
359     styp = STYP_RCONST;
360   else if (flags & SEC_CODE)
361     styp = STYP_TEXT;
362   else if (flags & SEC_DATA)
363     styp = STYP_DATA;
364   else if (flags & SEC_READONLY)
365     styp = STYP_RDATA;
366   else if (flags & SEC_LOAD)
367     styp = STYP_REG;
368   else
369     styp = STYP_BSS;
370
371   if (flags & SEC_NEVER_LOAD)
372     styp |= STYP_NOLOAD;
373
374   return styp;
375 }
376
377 /* Get the BFD flags to use for a section.  */
378
379 bfd_boolean
380 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name, section, flags_ptr)
381      bfd *abfd ATTRIBUTE_UNUSED;
382      PTR hdr;
383      const char *name ATTRIBUTE_UNUSED;
384      asection *section ATTRIBUTE_UNUSED;
385      flagword * flags_ptr;
386 {
387   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
388   long styp_flags = internal_s->s_flags;
389   flagword sec_flags = 0;
390
391   if (styp_flags & STYP_NOLOAD)
392     sec_flags |= SEC_NEVER_LOAD;
393
394   /* For 386 COFF, at least, an unloadable text or data section is
395      actually a shared library section.  */
396   if ((styp_flags & STYP_TEXT)
397       || (styp_flags & STYP_ECOFF_INIT)
398       || (styp_flags & STYP_ECOFF_FINI)
399       || (styp_flags & STYP_DYNAMIC)
400       || (styp_flags & STYP_LIBLIST)
401       || (styp_flags & STYP_RELDYN)
402       || styp_flags == STYP_CONFLIC
403       || (styp_flags & STYP_DYNSTR)
404       || (styp_flags & STYP_DYNSYM)
405       || (styp_flags & STYP_HASH))
406     {
407       if (sec_flags & SEC_NEVER_LOAD)
408         sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
409       else
410         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
411     }
412   else if ((styp_flags & STYP_DATA)
413            || (styp_flags & STYP_RDATA)
414            || (styp_flags & STYP_SDATA)
415            || styp_flags == STYP_PDATA
416            || styp_flags == STYP_XDATA
417            || (styp_flags & STYP_GOT)
418            || styp_flags == STYP_RCONST)
419     {
420       if (sec_flags & SEC_NEVER_LOAD)
421         sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
422       else
423         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
424       if ((styp_flags & STYP_RDATA)
425           || styp_flags == STYP_PDATA
426           || styp_flags == STYP_RCONST)
427         sec_flags |= SEC_READONLY;
428     }
429   else if ((styp_flags & STYP_BSS)
430            || (styp_flags & STYP_SBSS))
431     sec_flags |= SEC_ALLOC;
432   else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
433     sec_flags |= SEC_NEVER_LOAD;
434   else if ((styp_flags & STYP_LITA)
435            || (styp_flags & STYP_LIT8)
436            || (styp_flags & STYP_LIT4))
437     sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
438   else if (styp_flags & STYP_ECOFF_LIB)
439     sec_flags |= SEC_COFF_SHARED_LIBRARY;
440   else
441     sec_flags |= SEC_ALLOC | SEC_LOAD;
442
443   * flags_ptr = sec_flags;
444   return TRUE;
445 }
446 \f
447 /* Read in the symbolic header for an ECOFF object file.  */
448
449 static bfd_boolean
450 ecoff_slurp_symbolic_header (abfd)
451      bfd *abfd;
452 {
453   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
454   bfd_size_type external_hdr_size;
455   PTR raw = NULL;
456   HDRR *internal_symhdr;
457
458   /* See if we've already read it in.  */
459   if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
460       backend->debug_swap.sym_magic)
461     return TRUE;
462
463   /* See whether there is a symbolic header.  */
464   if (ecoff_data (abfd)->sym_filepos == 0)
465     {
466       bfd_get_symcount (abfd) = 0;
467       return TRUE;
468     }
469
470   /* At this point bfd_get_symcount (abfd) holds the number of symbols
471      as read from the file header, but on ECOFF this is always the
472      size of the symbolic information header.  It would be cleaner to
473      handle this when we first read the file in coffgen.c.  */
474   external_hdr_size = backend->debug_swap.external_hdr_size;
475   if (bfd_get_symcount (abfd) != external_hdr_size)
476     {
477       bfd_set_error (bfd_error_bad_value);
478       return FALSE;
479     }
480
481   /* Read the symbolic information header.  */
482   raw = (PTR) bfd_malloc (external_hdr_size);
483   if (raw == NULL)
484     goto error_return;
485
486   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0
487       || bfd_bread (raw, external_hdr_size, abfd) != external_hdr_size)
488     goto error_return;
489   internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
490   (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
491
492   if (internal_symhdr->magic != backend->debug_swap.sym_magic)
493     {
494       bfd_set_error (bfd_error_bad_value);
495       goto error_return;
496     }
497
498   /* Now we can get the correct number of symbols.  */
499   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
500                              + internal_symhdr->iextMax);
501
502   if (raw != NULL)
503     free (raw);
504   return TRUE;
505  error_return:
506   if (raw != NULL)
507     free (raw);
508   return FALSE;
509 }
510
511 /* Read in and swap the important symbolic information for an ECOFF
512    object file.  This is called by gdb via the read_debug_info entry
513    point in the backend structure.  */
514
515 bfd_boolean
516 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
517      bfd *abfd;
518      asection *ignore ATTRIBUTE_UNUSED;
519      struct ecoff_debug_info *debug;
520 {
521   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
522   HDRR *internal_symhdr;
523   bfd_size_type raw_base;
524   bfd_size_type raw_size;
525   PTR raw;
526   bfd_size_type external_fdr_size;
527   char *fraw_src;
528   char *fraw_end;
529   struct fdr *fdr_ptr;
530   bfd_size_type raw_end;
531   bfd_size_type cb_end;
532   bfd_size_type amt;
533   file_ptr pos;
534
535   BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
536
537   /* Check whether we've already gotten it, and whether there's any to
538      get.  */
539   if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
540     return TRUE;
541   if (ecoff_data (abfd)->sym_filepos == 0)
542     {
543       bfd_get_symcount (abfd) = 0;
544       return TRUE;
545     }
546
547   if (! ecoff_slurp_symbolic_header (abfd))
548     return FALSE;
549
550   internal_symhdr = &debug->symbolic_header;
551
552   /* Read all the symbolic information at once.  */
553   raw_base = (ecoff_data (abfd)->sym_filepos
554               + backend->debug_swap.external_hdr_size);
555
556   /* Alpha ecoff makes the determination of raw_size difficult. It has
557      an undocumented debug data section between the symhdr and the first
558      documented section. And the ordering of the sections varies between
559      statically and dynamically linked executables.
560      If bfd supports SEEK_END someday, this code could be simplified.  */
561   raw_end = 0;
562
563 #define UPDATE_RAW_END(start, count, size) \
564   cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
565   if (cb_end > raw_end) \
566     raw_end = cb_end
567
568   UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
569   UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
570   UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
571   UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
572   /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
573      optimization symtab, not the number of entries */
574   UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
575   UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
576   UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
577   UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
578   UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
579   UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
580   UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
581
582 #undef UPDATE_RAW_END
583
584   raw_size = raw_end - raw_base;
585   if (raw_size == 0)
586     {
587       ecoff_data (abfd)->sym_filepos = 0;
588       return TRUE;
589     }
590   raw = (PTR) bfd_alloc (abfd, raw_size);
591   if (raw == NULL)
592     return FALSE;
593
594   pos = ecoff_data (abfd)->sym_filepos;
595   pos += backend->debug_swap.external_hdr_size;
596   if (bfd_seek (abfd, pos, SEEK_SET) != 0
597       || bfd_bread (raw, raw_size, abfd) != raw_size)
598     {
599       bfd_release (abfd, raw);
600       return FALSE;
601     }
602
603   ecoff_data (abfd)->raw_syments = raw;
604
605   /* Get pointers for the numeric offsets in the HDRR structure.  */
606 #define FIX(off1, off2, type) \
607   if (internal_symhdr->off1 == 0) \
608     debug->off2 = (type) NULL; \
609   else \
610     debug->off2 = (type) ((char *) raw \
611                           + (internal_symhdr->off1 \
612                              - raw_base))
613
614   FIX (cbLineOffset, line, unsigned char *);
615   FIX (cbDnOffset, external_dnr, PTR);
616   FIX (cbPdOffset, external_pdr, PTR);
617   FIX (cbSymOffset, external_sym, PTR);
618   FIX (cbOptOffset, external_opt, PTR);
619   FIX (cbAuxOffset, external_aux, union aux_ext *);
620   FIX (cbSsOffset, ss, char *);
621   FIX (cbSsExtOffset, ssext, char *);
622   FIX (cbFdOffset, external_fdr, PTR);
623   FIX (cbRfdOffset, external_rfd, PTR);
624   FIX (cbExtOffset, external_ext, PTR);
625 #undef FIX
626
627   /* I don't want to always swap all the data, because it will just
628      waste time and most programs will never look at it.  The only
629      time the linker needs most of the debugging information swapped
630      is when linking big-endian and little-endian MIPS object files
631      together, which is not a common occurrence.
632
633      We need to look at the fdr to deal with a lot of information in
634      the symbols, so we swap them here.  */
635   amt = internal_symhdr->ifdMax;
636   amt *= sizeof (struct fdr);
637   debug->fdr = (struct fdr *) bfd_alloc (abfd, amt);
638   if (debug->fdr == NULL)
639     return FALSE;
640   external_fdr_size = backend->debug_swap.external_fdr_size;
641   fdr_ptr = debug->fdr;
642   fraw_src = (char *) debug->external_fdr;
643   fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
644   for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
645     (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
646
647   return TRUE;
648 }
649 \f
650 /* ECOFF symbol table routines.  The ECOFF symbol table is described
651    in gcc/mips-tfile.c.  */
652
653 /* ECOFF uses two common sections.  One is the usual one, and the
654    other is for small objects.  All the small objects are kept
655    together, and then referenced via the gp pointer, which yields
656    faster assembler code.  This is what we use for the small common
657    section.  */
658 static asection ecoff_scom_section;
659 static asymbol ecoff_scom_symbol;
660 static asymbol *ecoff_scom_symbol_ptr;
661
662 /* Create an empty symbol.  */
663
664 asymbol *
665 _bfd_ecoff_make_empty_symbol (abfd)
666      bfd *abfd;
667 {
668   ecoff_symbol_type *new;
669   bfd_size_type amt = sizeof (ecoff_symbol_type);
670
671   new = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
672   if (new == (ecoff_symbol_type *) NULL)
673     return (asymbol *) NULL;
674   new->symbol.section = (asection *) NULL;
675   new->fdr = (FDR *) NULL;
676   new->local = FALSE;
677   new->native = NULL;
678   new->symbol.the_bfd = abfd;
679   return &new->symbol;
680 }
681
682 /* Set the BFD flags and section for an ECOFF symbol.  */
683
684 static bfd_boolean
685 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
686      bfd *abfd;
687      SYMR *ecoff_sym;
688      asymbol *asym;
689      int ext;
690      int weak;
691 {
692   asym->the_bfd = abfd;
693   asym->value = ecoff_sym->value;
694   asym->section = &bfd_debug_section;
695   asym->udata.i = 0;
696
697   /* Most symbol types are just for debugging.  */
698   switch (ecoff_sym->st)
699     {
700     case stGlobal:
701     case stStatic:
702     case stLabel:
703     case stProc:
704     case stStaticProc:
705       break;
706     case stNil:
707       if (ECOFF_IS_STAB (ecoff_sym))
708         {
709           asym->flags = BSF_DEBUGGING;
710           return TRUE;
711         }
712       break;
713     default:
714       asym->flags = BSF_DEBUGGING;
715       return TRUE;
716     }
717
718   if (weak)
719     asym->flags = BSF_EXPORT | BSF_WEAK;
720   else if (ext)
721     asym->flags = BSF_EXPORT | BSF_GLOBAL;
722   else
723     {
724       asym->flags = BSF_LOCAL;
725       /* Normally, a local stProc symbol will have a corresponding
726          external symbol.  We mark the local symbol as a debugging
727          symbol, in order to prevent nm from printing both out.
728          Similarly, we mark stLabel and stabs symbols as debugging
729          symbols.  In both cases, we do want to set the value
730          correctly based on the symbol class.  */
731       if (ecoff_sym->st == stProc
732           || ecoff_sym->st == stLabel
733           || ECOFF_IS_STAB (ecoff_sym))
734         asym->flags |= BSF_DEBUGGING;
735     }
736
737   if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
738     asym->flags |= BSF_FUNCTION;
739
740   switch (ecoff_sym->sc)
741     {
742     case scNil:
743       /* Used for compiler generated labels.  Leave them in the
744          debugging section, and mark them as local.  If BSF_DEBUGGING
745          is set, then nm does not display them for some reason.  If no
746          flags are set then the linker whines about them.  */
747       asym->flags = BSF_LOCAL;
748       break;
749     case scText:
750       asym->section = bfd_make_section_old_way (abfd, ".text");
751       asym->value -= asym->section->vma;
752       break;
753     case scData:
754       asym->section = bfd_make_section_old_way (abfd, ".data");
755       asym->value -= asym->section->vma;
756       break;
757     case scBss:
758       asym->section = bfd_make_section_old_way (abfd, ".bss");
759       asym->value -= asym->section->vma;
760       break;
761     case scRegister:
762       asym->flags = BSF_DEBUGGING;
763       break;
764     case scAbs:
765       asym->section = bfd_abs_section_ptr;
766       break;
767     case scUndefined:
768       asym->section = bfd_und_section_ptr;
769       asym->flags = 0;
770       asym->value = 0;
771       break;
772     case scCdbLocal:
773     case scBits:
774     case scCdbSystem:
775     case scRegImage:
776     case scInfo:
777     case scUserStruct:
778       asym->flags = BSF_DEBUGGING;
779       break;
780     case scSData:
781       asym->section = bfd_make_section_old_way (abfd, ".sdata");
782       asym->value -= asym->section->vma;
783       break;
784     case scSBss:
785       asym->section = bfd_make_section_old_way (abfd, ".sbss");
786       asym->value -= asym->section->vma;
787       break;
788     case scRData:
789       asym->section = bfd_make_section_old_way (abfd, ".rdata");
790       asym->value -= asym->section->vma;
791       break;
792     case scVar:
793       asym->flags = BSF_DEBUGGING;
794       break;
795     case scCommon:
796       if (asym->value > ecoff_data (abfd)->gp_size)
797         {
798           asym->section = bfd_com_section_ptr;
799           asym->flags = 0;
800           break;
801         }
802       /* Fall through.  */
803     case scSCommon:
804       if (ecoff_scom_section.name == NULL)
805         {
806           /* Initialize the small common section.  */
807           ecoff_scom_section.name = SCOMMON;
808           ecoff_scom_section.flags = SEC_IS_COMMON;
809           ecoff_scom_section.output_section = &ecoff_scom_section;
810           ecoff_scom_section.symbol = &ecoff_scom_symbol;
811           ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
812           ecoff_scom_symbol.name = SCOMMON;
813           ecoff_scom_symbol.flags = BSF_SECTION_SYM;
814           ecoff_scom_symbol.section = &ecoff_scom_section;
815           ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
816         }
817       asym->section = &ecoff_scom_section;
818       asym->flags = 0;
819       break;
820     case scVarRegister:
821     case scVariant:
822       asym->flags = BSF_DEBUGGING;
823       break;
824     case scSUndefined:
825       asym->section = bfd_und_section_ptr;
826       asym->flags = 0;
827       asym->value = 0;
828       break;
829     case scInit:
830       asym->section = bfd_make_section_old_way (abfd, ".init");
831       asym->value -= asym->section->vma;
832       break;
833     case scBasedVar:
834     case scXData:
835     case scPData:
836       asym->flags = BSF_DEBUGGING;
837       break;
838     case scFini:
839       asym->section = bfd_make_section_old_way (abfd, ".fini");
840       asym->value -= asym->section->vma;
841       break;
842     case scRConst:
843       asym->section = bfd_make_section_old_way (abfd, ".rconst");
844       asym->value -= asym->section->vma;
845       break;
846     default:
847       break;
848     }
849
850   /* Look for special constructors symbols and make relocation entries
851      in a special construction section.  These are produced by the
852      -fgnu-linker argument to g++.  */
853   if (ECOFF_IS_STAB (ecoff_sym))
854     {
855       switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
856         {
857         default:
858           break;
859
860         case N_SETA:
861         case N_SETT:
862         case N_SETD:
863         case N_SETB:
864           {
865             /* Mark the symbol as a constructor.  */
866             asym->flags |= BSF_CONSTRUCTOR;
867           }
868           break;
869         }
870     }
871   return TRUE;
872 }
873
874 /* Read an ECOFF symbol table.  */
875
876 bfd_boolean
877 _bfd_ecoff_slurp_symbol_table (abfd)
878      bfd *abfd;
879 {
880   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
881   const bfd_size_type external_ext_size
882     = backend->debug_swap.external_ext_size;
883   const bfd_size_type external_sym_size
884     = backend->debug_swap.external_sym_size;
885   void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
886     = backend->debug_swap.swap_ext_in;
887   void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
888     = backend->debug_swap.swap_sym_in;
889   bfd_size_type internal_size;
890   ecoff_symbol_type *internal;
891   ecoff_symbol_type *internal_ptr;
892   char *eraw_src;
893   char *eraw_end;
894   FDR *fdr_ptr;
895   FDR *fdr_end;
896
897   /* If we've already read in the symbol table, do nothing.  */
898   if (ecoff_data (abfd)->canonical_symbols != NULL)
899     return TRUE;
900
901   /* Get the symbolic information.  */
902   if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
903                                         &ecoff_data (abfd)->debug_info))
904     return FALSE;
905   if (bfd_get_symcount (abfd) == 0)
906     return TRUE;
907
908   internal_size = bfd_get_symcount (abfd);
909   internal_size *= sizeof (ecoff_symbol_type);
910   internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
911   if (internal == NULL)
912     return FALSE;
913
914   internal_ptr = internal;
915   eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
916   eraw_end = (eraw_src
917               + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
918                  * external_ext_size));
919   for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
920     {
921       EXTR internal_esym;
922
923       (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
924       internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
925                                    + internal_esym.asym.iss);
926       if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
927                                   &internal_ptr->symbol, 1,
928                                   internal_esym.weakext))
929         return FALSE;
930       /* The alpha uses a negative ifd field for section symbols.  */
931       if (internal_esym.ifd >= 0)
932         internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
933                              + internal_esym.ifd);
934       else
935         internal_ptr->fdr = NULL;
936       internal_ptr->local = FALSE;
937       internal_ptr->native = (PTR) eraw_src;
938     }
939
940   /* The local symbols must be accessed via the fdr's, because the
941      string and aux indices are relative to the fdr information.  */
942   fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
943   fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
944   for (; fdr_ptr < fdr_end; fdr_ptr++)
945     {
946       char *lraw_src;
947       char *lraw_end;
948
949       lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
950                   + fdr_ptr->isymBase * external_sym_size);
951       lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
952       for (;
953            lraw_src < lraw_end;
954            lraw_src += external_sym_size, internal_ptr++)
955         {
956           SYMR internal_sym;
957
958           (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
959           internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
960                                        + fdr_ptr->issBase
961                                        + internal_sym.iss);
962           if (!ecoff_set_symbol_info (abfd, &internal_sym,
963                                       &internal_ptr->symbol, 0, 0))
964             return FALSE;
965           internal_ptr->fdr = fdr_ptr;
966           internal_ptr->local = TRUE;
967           internal_ptr->native = (PTR) lraw_src;
968         }
969     }
970
971   ecoff_data (abfd)->canonical_symbols = internal;
972
973   return TRUE;
974 }
975
976 /* Return the amount of space needed for the canonical symbols.  */
977
978 long
979 _bfd_ecoff_get_symtab_upper_bound (abfd)
980      bfd *abfd;
981 {
982   if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
983                                         &ecoff_data (abfd)->debug_info))
984     return -1;
985
986   if (bfd_get_symcount (abfd) == 0)
987     return 0;
988
989   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
990 }
991
992 /* Get the canonical symbols.  */
993
994 long
995 _bfd_ecoff_canonicalize_symtab (abfd, alocation)
996      bfd *abfd;
997      asymbol **alocation;
998 {
999   unsigned int counter = 0;
1000   ecoff_symbol_type *symbase;
1001   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1002
1003   if (! _bfd_ecoff_slurp_symbol_table (abfd))
1004     return -1;
1005   if (bfd_get_symcount (abfd) == 0)
1006     return 0;
1007
1008   symbase = ecoff_data (abfd)->canonical_symbols;
1009   while (counter < bfd_get_symcount (abfd))
1010     {
1011       *(location++) = symbase++;
1012       counter++;
1013     }
1014   *location++ = (ecoff_symbol_type *) NULL;
1015   return bfd_get_symcount (abfd);
1016 }
1017
1018 /* Turn ECOFF type information into a printable string.
1019    ecoff_emit_aggregate and ecoff_type_to_string are from
1020    gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
1021
1022 /* Write aggregate information to a string.  */
1023
1024 static void
1025 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1026      bfd *abfd;
1027      FDR *fdr;
1028      char *string;
1029      RNDXR *rndx;
1030      long isym;
1031      const char *which;
1032 {
1033   const struct ecoff_debug_swap * const debug_swap =
1034     &ecoff_backend (abfd)->debug_swap;
1035   struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1036   unsigned int ifd = rndx->rfd;
1037   unsigned int indx = rndx->index;
1038   const char *name;
1039
1040   if (ifd == 0xfff)
1041     ifd = isym;
1042
1043   /* An ifd of -1 is an opaque type.  An escaped index of 0 is a
1044      struct return type of a procedure compiled without -g.  */
1045   if (ifd == 0xffffffff
1046       || (rndx->rfd == 0xfff && indx == 0))
1047     name = "<undefined>";
1048   else if (indx == indexNil)
1049     name = "<no name>";
1050   else
1051     {
1052       SYMR sym;
1053
1054       if (debug_info->external_rfd == NULL)
1055         fdr = debug_info->fdr + ifd;
1056       else
1057         {
1058           RFDT rfd;
1059
1060           (*debug_swap->swap_rfd_in) (abfd,
1061                                       ((char *) debug_info->external_rfd
1062                                        + ((fdr->rfdBase + ifd)
1063                                           * debug_swap->external_rfd_size)),
1064                                       &rfd);
1065           fdr = debug_info->fdr + rfd;
1066         }
1067
1068       indx += fdr->isymBase;
1069
1070       (*debug_swap->swap_sym_in) (abfd,
1071                                   ((char *) debug_info->external_sym
1072                                    + indx * debug_swap->external_sym_size),
1073                                   &sym);
1074
1075       name = debug_info->ss + fdr->issBase + sym.iss;
1076     }
1077
1078   sprintf (string,
1079            "%s %s { ifd = %u, index = %lu }",
1080            which, name, ifd,
1081            ((long) indx
1082             + debug_info->symbolic_header.iextMax));
1083 }
1084
1085 /* Convert the type information to string format.  */
1086
1087 static char *
1088 ecoff_type_to_string (abfd, fdr, indx)
1089      bfd *abfd;
1090      FDR *fdr;
1091      unsigned int indx;
1092 {
1093   union aux_ext *aux_ptr;
1094   int bigendian;
1095   AUXU u;
1096   struct qual {
1097     unsigned int  type;
1098     int  low_bound;
1099     int  high_bound;
1100     int  stride;
1101   } qualifiers[7];
1102   unsigned int basic_type;
1103   int i;
1104   char buffer1[1024];
1105   static char buffer2[1024];
1106   char *p1 = buffer1;
1107   char *p2 = buffer2;
1108   RNDXR rndx;
1109
1110   aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1111   bigendian = fdr->fBigendian;
1112
1113   for (i = 0; i < 7; i++)
1114     {
1115       qualifiers[i].low_bound = 0;
1116       qualifiers[i].high_bound = 0;
1117       qualifiers[i].stride = 0;
1118     }
1119
1120   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1121     return "-1 (no type)";
1122   _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1123
1124   basic_type = u.ti.bt;
1125   qualifiers[0].type = u.ti.tq0;
1126   qualifiers[1].type = u.ti.tq1;
1127   qualifiers[2].type = u.ti.tq2;
1128   qualifiers[3].type = u.ti.tq3;
1129   qualifiers[4].type = u.ti.tq4;
1130   qualifiers[5].type = u.ti.tq5;
1131   qualifiers[6].type = tqNil;
1132
1133   /* Go get the basic type.  */
1134   switch (basic_type)
1135     {
1136     case btNil:                 /* Undefined.  */
1137       strcpy (p1, "nil");
1138       break;
1139
1140     case btAdr:                 /* Address - integer same size as pointer.  */
1141       strcpy (p1, "address");
1142       break;
1143
1144     case btChar:                /* Character.  */
1145       strcpy (p1, "char");
1146       break;
1147
1148     case btUChar:               /* Unsigned character.  */
1149       strcpy (p1, "unsigned char");
1150       break;
1151
1152     case btShort:               /* Short.  */
1153       strcpy (p1, "short");
1154       break;
1155
1156     case btUShort:              /* Unsigned short.  */
1157       strcpy (p1, "unsigned short");
1158       break;
1159
1160     case btInt:                 /* Int.  */
1161       strcpy (p1, "int");
1162       break;
1163
1164     case btUInt:                /* Unsigned int.  */
1165       strcpy (p1, "unsigned int");
1166       break;
1167
1168     case btLong:                /* Long.  */
1169       strcpy (p1, "long");
1170       break;
1171
1172     case btULong:               /* Unsigned long.  */
1173       strcpy (p1, "unsigned long");
1174       break;
1175
1176     case btFloat:               /* Float (real).  */
1177       strcpy (p1, "float");
1178       break;
1179
1180     case btDouble:              /* Double (real).  */
1181       strcpy (p1, "double");
1182       break;
1183
1184       /* Structures add 1-2 aux words:
1185          1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1186          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1187
1188     case btStruct:              /* Structure (Record).  */
1189       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1190       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1191                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1192                             "struct");
1193       indx++;                   /* Skip aux words.  */
1194       break;
1195
1196       /* Unions add 1-2 aux words:
1197          1st word is [ST_RFDESCAPE, offset] pointer to union def;
1198          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1199
1200     case btUnion:               /* Union.  */
1201       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1202       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1203                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1204                             "union");
1205       indx++;                   /* Skip aux words.  */
1206       break;
1207
1208       /* Enumerations add 1-2 aux words:
1209          1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1210          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1211
1212     case btEnum:                /* Enumeration.  */
1213       _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1214       ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1215                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1216                             "enum");
1217       indx++;                   /* Skip aux words.  */
1218       break;
1219
1220     case btTypedef:             /* Defined via a typedef, isymRef points.  */
1221       strcpy (p1, "typedef");
1222       break;
1223
1224     case btRange:               /* Subrange of int.  */
1225       strcpy (p1, "subrange");
1226       break;
1227
1228     case btSet:                 /* Pascal sets.  */
1229       strcpy (p1, "set");
1230       break;
1231
1232     case btComplex:             /* Fortran complex.  */
1233       strcpy (p1, "complex");
1234       break;
1235
1236     case btDComplex:            /* Fortran double complex.  */
1237       strcpy (p1, "double complex");
1238       break;
1239
1240     case btIndirect:            /* Forward or unnamed typedef.  */
1241       strcpy (p1, "forward/unamed typedef");
1242       break;
1243
1244     case btFixedDec:            /* Fixed Decimal.  */
1245       strcpy (p1, "fixed decimal");
1246       break;
1247
1248     case btFloatDec:            /* Float Decimal.  */
1249       strcpy (p1, "float decimal");
1250       break;
1251
1252     case btString:              /* Varying Length Character String.  */
1253       strcpy (p1, "string");
1254       break;
1255
1256     case btBit:                 /* Aligned Bit String.  */
1257       strcpy (p1, "bit");
1258       break;
1259
1260     case btPicture:             /* Picture.  */
1261       strcpy (p1, "picture");
1262       break;
1263
1264     case btVoid:                /* Void.  */
1265       strcpy (p1, "void");
1266       break;
1267
1268     default:
1269       sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1270       break;
1271     }
1272
1273   p1 += strlen (buffer1);
1274
1275   /* If this is a bitfield, get the bitsize.  */
1276   if (u.ti.fBitfield)
1277     {
1278       int bitsize;
1279
1280       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1281       sprintf (p1, " : %d", bitsize);
1282       p1 += strlen (buffer1);
1283     }
1284
1285   /* Deal with any qualifiers.  */
1286   if (qualifiers[0].type != tqNil)
1287     {
1288       /* Snarf up any array bounds in the correct order.  Arrays
1289          store 5 successive words in the aux. table:
1290                 word 0  RNDXR to type of the bounds (ie, int)
1291                 word 1  Current file descriptor index
1292                 word 2  low bound
1293                 word 3  high bound (or -1 if [])
1294                 word 4  stride size in bits.  */
1295       for (i = 0; i < 7; i++)
1296         {
1297           if (qualifiers[i].type == tqArray)
1298             {
1299               qualifiers[i].low_bound =
1300                 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1301               qualifiers[i].high_bound =
1302                 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1303               qualifiers[i].stride =
1304                 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1305               indx += 5;
1306             }
1307         }
1308
1309       /* Now print out the qualifiers.  */
1310       for (i = 0; i < 6; i++)
1311         {
1312           switch (qualifiers[i].type)
1313             {
1314             case tqNil:
1315             case tqMax:
1316               break;
1317
1318             case tqPtr:
1319               strcpy (p2, "ptr to ");
1320               p2 += sizeof ("ptr to ")-1;
1321               break;
1322
1323             case tqVol:
1324               strcpy (p2, "volatile ");
1325               p2 += sizeof ("volatile ")-1;
1326               break;
1327
1328             case tqFar:
1329               strcpy (p2, "far ");
1330               p2 += sizeof ("far ")-1;
1331               break;
1332
1333             case tqProc:
1334               strcpy (p2, "func. ret. ");
1335               p2 += sizeof ("func. ret. ");
1336               break;
1337
1338             case tqArray:
1339               {
1340                 int first_array = i;
1341                 int j;
1342
1343                 /* Print array bounds reversed (ie, in the order the C
1344                    programmer writes them).  C is such a fun language....  */
1345                 while (i < 5 && qualifiers[i+1].type == tqArray)
1346                   i++;
1347
1348                 for (j = i; j >= first_array; j--)
1349                   {
1350                     strcpy (p2, "array [");
1351                     p2 += sizeof ("array [")-1;
1352                     if (qualifiers[j].low_bound != 0)
1353                       sprintf (p2,
1354                                "%ld:%ld {%ld bits}",
1355                                (long) qualifiers[j].low_bound,
1356                                (long) qualifiers[j].high_bound,
1357                                (long) qualifiers[j].stride);
1358
1359                     else if (qualifiers[j].high_bound != -1)
1360                       sprintf (p2,
1361                                "%ld {%ld bits}",
1362                                (long) (qualifiers[j].high_bound + 1),
1363                                (long) (qualifiers[j].stride));
1364
1365                     else
1366                       sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1367
1368                     p2 += strlen (p2);
1369                     strcpy (p2, "] of ");
1370                     p2 += sizeof ("] of ")-1;
1371                   }
1372               }
1373               break;
1374             }
1375         }
1376     }
1377
1378   strcpy (p2, buffer1);
1379   return buffer2;
1380 }
1381
1382 /* Return information about ECOFF symbol SYMBOL in RET.  */
1383
1384 void
1385 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1386      bfd *abfd ATTRIBUTE_UNUSED;
1387      asymbol *symbol;
1388      symbol_info *ret;
1389 {
1390   bfd_symbol_info (symbol, ret);
1391 }
1392
1393 /* Return whether this is a local label.  */
1394
1395 bfd_boolean
1396 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1397      bfd *abfd ATTRIBUTE_UNUSED;
1398      const char *name;
1399 {
1400   return name[0] == '$';
1401 }
1402
1403 /* Print information about an ECOFF symbol.  */
1404
1405 void
1406 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1407      bfd *abfd;
1408      PTR filep;
1409      asymbol *symbol;
1410      bfd_print_symbol_type how;
1411 {
1412   const struct ecoff_debug_swap * const debug_swap
1413     = &ecoff_backend (abfd)->debug_swap;
1414   FILE *file = (FILE *)filep;
1415
1416   switch (how)
1417     {
1418     case bfd_print_symbol_name:
1419       fprintf (file, "%s", symbol->name);
1420       break;
1421     case bfd_print_symbol_more:
1422       if (ecoffsymbol (symbol)->local)
1423         {
1424           SYMR ecoff_sym;
1425
1426           (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1427                                       &ecoff_sym);
1428           fprintf (file, "ecoff local ");
1429           fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1430           fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1431                    (unsigned) ecoff_sym.sc);
1432         }
1433       else
1434         {
1435           EXTR ecoff_ext;
1436
1437           (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1438                                       &ecoff_ext);
1439           fprintf (file, "ecoff extern ");
1440           fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1441           fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1442                    (unsigned) ecoff_ext.asym.sc);
1443         }
1444       break;
1445     case bfd_print_symbol_all:
1446       /* Print out the symbols in a reasonable way.  */
1447       {
1448         char type;
1449         int pos;
1450         EXTR ecoff_ext;
1451         char jmptbl;
1452         char cobol_main;
1453         char weakext;
1454
1455         if (ecoffsymbol (symbol)->local)
1456           {
1457             (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1458                                         &ecoff_ext.asym);
1459             type = 'l';
1460             pos = ((((char *) ecoffsymbol (symbol)->native
1461                      - (char *) ecoff_data (abfd)->debug_info.external_sym)
1462                     / debug_swap->external_sym_size)
1463                    + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1464             jmptbl = ' ';
1465             cobol_main = ' ';
1466             weakext = ' ';
1467           }
1468         else
1469           {
1470             (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1471                                         &ecoff_ext);
1472             type = 'e';
1473             pos = (((char *) ecoffsymbol (symbol)->native
1474                     - (char *) ecoff_data (abfd)->debug_info.external_ext)
1475                    / debug_swap->external_ext_size);
1476             jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1477             cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1478             weakext = ecoff_ext.weakext ? 'w' : ' ';
1479           }
1480
1481         fprintf (file, "[%3d] %c ",
1482                  pos, type);
1483         fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1484         fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1485                  (unsigned) ecoff_ext.asym.st,
1486                  (unsigned) ecoff_ext.asym.sc,
1487                  (unsigned) ecoff_ext.asym.index,
1488                  jmptbl, cobol_main, weakext,
1489                  symbol->name);
1490
1491         if (ecoffsymbol (symbol)->fdr != NULL
1492             && ecoff_ext.asym.index != indexNil)
1493           {
1494             FDR *fdr;
1495             unsigned int indx;
1496             int bigendian;
1497             bfd_size_type sym_base;
1498             union aux_ext *aux_base;
1499
1500             fdr = ecoffsymbol (symbol)->fdr;
1501             indx = ecoff_ext.asym.index;
1502
1503             /* sym_base is used to map the fdr relative indices which
1504                appear in the file to the position number which we are
1505                using.  */
1506             sym_base = fdr->isymBase;
1507             if (ecoffsymbol (symbol)->local)
1508               sym_base +=
1509                 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1510
1511             /* aux_base is the start of the aux entries for this file;
1512                asym.index is an offset from this.  */
1513             aux_base = (ecoff_data (abfd)->debug_info.external_aux
1514                         + fdr->iauxBase);
1515
1516             /* The aux entries are stored in host byte order; the
1517                order is indicated by a bit in the fdr.  */
1518             bigendian = fdr->fBigendian;
1519
1520             /* This switch is basically from gcc/mips-tdump.c.  */
1521             switch (ecoff_ext.asym.st)
1522               {
1523               case stNil:
1524               case stLabel:
1525                 break;
1526
1527               case stFile:
1528               case stBlock:
1529                 fprintf (file, _("\n      End+1 symbol: %ld"),
1530                          (long) (indx + sym_base));
1531                 break;
1532
1533               case stEnd:
1534                 if (ecoff_ext.asym.sc == scText
1535                     || ecoff_ext.asym.sc == scInfo)
1536                   fprintf (file, _("\n      First symbol: %ld"),
1537                            (long) (indx + sym_base));
1538                 else
1539                   fprintf (file, _("\n      First symbol: %ld"),
1540                            ((long)
1541                             (AUX_GET_ISYM (bigendian,
1542                                            &aux_base[ecoff_ext.asym.index])
1543                              + sym_base)));
1544                 break;
1545
1546               case stProc:
1547               case stStaticProc:
1548                 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1549                   ;
1550                 else if (ecoffsymbol (symbol)->local)
1551                   fprintf (file, _("\n      End+1 symbol: %-7ld   Type:  %s"),
1552                            ((long)
1553                             (AUX_GET_ISYM (bigendian,
1554                                            &aux_base[ecoff_ext.asym.index])
1555                              + sym_base)),
1556                            ecoff_type_to_string (abfd, fdr, indx + 1));
1557                 else
1558                   fprintf (file, _("\n      Local symbol: %ld"),
1559                            ((long) indx
1560                             + (long) sym_base
1561                             + (ecoff_data (abfd)
1562                                ->debug_info.symbolic_header.iextMax)));
1563                 break;
1564
1565               case stStruct:
1566                 fprintf (file, _("\n      struct; End+1 symbol: %ld"),
1567                          (long) (indx + sym_base));
1568                 break;
1569
1570               case stUnion:
1571                 fprintf (file, _("\n      union; End+1 symbol: %ld"),
1572                          (long) (indx + sym_base));
1573                 break;
1574
1575               case stEnum:
1576                 fprintf (file, _("\n      enum; End+1 symbol: %ld"),
1577                          (long) (indx + sym_base));
1578                 break;
1579
1580               default:
1581                 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1582                   fprintf (file, _("\n      Type: %s"),
1583                            ecoff_type_to_string (abfd, fdr, indx));
1584                 break;
1585               }
1586           }
1587       }
1588       break;
1589     }
1590 }
1591 \f
1592 /* Read in the relocs for a section.  */
1593
1594 static bfd_boolean
1595 ecoff_slurp_reloc_table (abfd, section, symbols)
1596      bfd *abfd;
1597      asection *section;
1598      asymbol **symbols;
1599 {
1600   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1601   arelent *internal_relocs;
1602   bfd_size_type external_reloc_size;
1603   bfd_size_type amt;
1604   char *external_relocs;
1605   arelent *rptr;
1606   unsigned int i;
1607
1608   if (section->relocation != (arelent *) NULL
1609       || section->reloc_count == 0
1610       || (section->flags & SEC_CONSTRUCTOR) != 0)
1611     return TRUE;
1612
1613   if (! _bfd_ecoff_slurp_symbol_table (abfd))
1614     return FALSE;
1615
1616   amt = section->reloc_count;
1617   amt *= sizeof (arelent);
1618   internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1619
1620   external_reloc_size = backend->external_reloc_size;
1621   amt = external_reloc_size * section->reloc_count;
1622   external_relocs = (char *) bfd_alloc (abfd, amt);
1623   if (internal_relocs == (arelent *) NULL
1624       || external_relocs == (char *) NULL)
1625     return FALSE;
1626   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1627     return FALSE;
1628   if (bfd_bread (external_relocs, amt, abfd) != amt)
1629     return FALSE;
1630
1631   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1632     {
1633       struct internal_reloc intern;
1634
1635       (*backend->swap_reloc_in) (abfd,
1636                                  external_relocs + i * external_reloc_size,
1637                                  &intern);
1638
1639       if (intern.r_extern)
1640         {
1641           /* r_symndx is an index into the external symbols.  */
1642           BFD_ASSERT (intern.r_symndx >= 0
1643                       && (intern.r_symndx
1644                           < (ecoff_data (abfd)
1645                              ->debug_info.symbolic_header.iextMax)));
1646           rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1647           rptr->addend = 0;
1648         }
1649       else if (intern.r_symndx == RELOC_SECTION_NONE
1650                || intern.r_symndx == RELOC_SECTION_ABS)
1651         {
1652           rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1653           rptr->addend = 0;
1654         }
1655       else
1656         {
1657           const char *sec_name;
1658           asection *sec;
1659
1660           /* r_symndx is a section key.  */
1661           switch (intern.r_symndx)
1662             {
1663             case RELOC_SECTION_TEXT:  sec_name = ".text";  break;
1664             case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1665             case RELOC_SECTION_DATA:  sec_name = ".data";  break;
1666             case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1667             case RELOC_SECTION_SBSS:  sec_name = ".sbss";  break;
1668             case RELOC_SECTION_BSS:   sec_name = ".bss";   break;
1669             case RELOC_SECTION_INIT:  sec_name = ".init";  break;
1670             case RELOC_SECTION_LIT8:  sec_name = ".lit8";  break;
1671             case RELOC_SECTION_LIT4:  sec_name = ".lit4";  break;
1672             case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1673             case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1674             case RELOC_SECTION_FINI:  sec_name = ".fini"; break;
1675             case RELOC_SECTION_LITA:  sec_name = ".lita";  break;
1676             case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1677             default: abort ();
1678             }
1679
1680           sec = bfd_get_section_by_name (abfd, sec_name);
1681           if (sec == (asection *) NULL)
1682             abort ();
1683           rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1684
1685           rptr->addend = - bfd_get_section_vma (abfd, sec);
1686         }
1687
1688       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1689
1690       /* Let the backend select the howto field and do any other
1691          required processing.  */
1692       (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1693     }
1694
1695   bfd_release (abfd, external_relocs);
1696
1697   section->relocation = internal_relocs;
1698
1699   return TRUE;
1700 }
1701
1702 /* Get a canonical list of relocs.  */
1703
1704 long
1705 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1706      bfd *abfd;
1707      asection *section;
1708      arelent **relptr;
1709      asymbol **symbols;
1710 {
1711   unsigned int count;
1712
1713   if (section->flags & SEC_CONSTRUCTOR)
1714     {
1715       arelent_chain *chain;
1716
1717       /* This section has relocs made up by us, not the file, so take
1718          them out of their chain and place them into the data area
1719          provided.  */
1720       for (count = 0, chain = section->constructor_chain;
1721            count < section->reloc_count;
1722            count++, chain = chain->next)
1723         *relptr++ = &chain->relent;
1724     }
1725   else
1726     {
1727       arelent *tblptr;
1728
1729       if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1730         return -1;
1731
1732       tblptr = section->relocation;
1733
1734       for (count = 0; count < section->reloc_count; count++)
1735         *relptr++ = tblptr++;
1736     }
1737
1738   *relptr = (arelent *) NULL;
1739
1740   return section->reloc_count;
1741 }
1742 \f
1743 /* Provided a BFD, a section and an offset into the section, calculate
1744    and return the name of the source file and the line nearest to the
1745    wanted location.  */
1746
1747 bfd_boolean
1748 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1749                               filename_ptr, functionname_ptr, retline_ptr)
1750      bfd *abfd;
1751      asection *section;
1752      asymbol **ignore_symbols ATTRIBUTE_UNUSED;
1753      bfd_vma offset;
1754      const char **filename_ptr;
1755      const char **functionname_ptr;
1756      unsigned int *retline_ptr;
1757 {
1758   const struct ecoff_debug_swap * const debug_swap
1759     = &ecoff_backend (abfd)->debug_swap;
1760   struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1761   struct ecoff_find_line *line_info;
1762
1763   /* Make sure we have the FDR's.  */
1764   if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1765       || bfd_get_symcount (abfd) == 0)
1766     return FALSE;
1767
1768   if (ecoff_data (abfd)->find_line_info == NULL)
1769     {
1770       bfd_size_type amt = sizeof (struct ecoff_find_line);
1771       ecoff_data (abfd)->find_line_info
1772         = (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1773       if (ecoff_data (abfd)->find_line_info == NULL)
1774         return FALSE;
1775     }
1776   line_info = ecoff_data (abfd)->find_line_info;
1777
1778   return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1779                                  debug_swap, line_info, filename_ptr,
1780                                  functionname_ptr, retline_ptr);
1781 }
1782 \f
1783 /* Copy private BFD data.  This is called by objcopy and strip.  We
1784    use it to copy the ECOFF debugging information from one BFD to the
1785    other.  It would be theoretically possible to represent the ECOFF
1786    debugging information in the symbol table.  However, it would be a
1787    lot of work, and there would be little gain (gas, gdb, and ld
1788    already access the ECOFF debugging information via the
1789    ecoff_debug_info structure, and that structure would have to be
1790    retained in order to support ECOFF debugging in MIPS ELF).
1791
1792    The debugging information for the ECOFF external symbols comes from
1793    the symbol table, so this function only handles the other debugging
1794    information.  */
1795
1796 bfd_boolean
1797 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1798      bfd *ibfd;
1799      bfd *obfd;
1800 {
1801   struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1802   struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1803   register int i;
1804   asymbol **sym_ptr_ptr;
1805   size_t c;
1806   bfd_boolean local;
1807
1808   /* We only want to copy information over if both BFD's use ECOFF
1809      format.  */
1810   if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1811       || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1812     return TRUE;
1813
1814   /* Copy the GP value and the register masks.  */
1815   ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1816   ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1817   ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1818   for (i = 0; i < 3; i++)
1819     ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1820
1821   /* Copy the version stamp.  */
1822   oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1823
1824   /* If there are no symbols, don't copy any debugging information.  */
1825   c = bfd_get_symcount (obfd);
1826   sym_ptr_ptr = bfd_get_outsymbols (obfd);
1827   if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1828     return TRUE;
1829
1830   /* See if there are any local symbols.  */
1831   local = FALSE;
1832   for (; c > 0; c--, sym_ptr_ptr++)
1833     {
1834       if (ecoffsymbol (*sym_ptr_ptr)->local)
1835         {
1836           local = TRUE;
1837           break;
1838         }
1839     }
1840
1841   if (local)
1842     {
1843       /* There are some local symbols.  We just bring over all the
1844          debugging information.  FIXME: This is not quite the right
1845          thing to do.  If the user has asked us to discard all
1846          debugging information, then we are probably going to wind up
1847          keeping it because there will probably be some local symbol
1848          which objcopy did not discard.  We should actually break
1849          apart the debugging information and only keep that which
1850          applies to the symbols we want to keep.  */
1851       oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1852       oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1853       oinfo->line = iinfo->line;
1854
1855       oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1856       oinfo->external_dnr = iinfo->external_dnr;
1857
1858       oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1859       oinfo->external_pdr = iinfo->external_pdr;
1860
1861       oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1862       oinfo->external_sym = iinfo->external_sym;
1863
1864       oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1865       oinfo->external_opt = iinfo->external_opt;
1866
1867       oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1868       oinfo->external_aux = iinfo->external_aux;
1869
1870       oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1871       oinfo->ss = iinfo->ss;
1872
1873       oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1874       oinfo->external_fdr = iinfo->external_fdr;
1875
1876       oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1877       oinfo->external_rfd = iinfo->external_rfd;
1878     }
1879   else
1880     {
1881       /* We are discarding all the local symbol information.  Look
1882          through the external symbols and remove all references to FDR
1883          or aux information.  */
1884       c = bfd_get_symcount (obfd);
1885       sym_ptr_ptr = bfd_get_outsymbols (obfd);
1886       for (; c > 0; c--, sym_ptr_ptr++)
1887         {
1888           EXTR esym;
1889
1890           (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1891             (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1892           esym.ifd = ifdNil;
1893           esym.asym.index = indexNil;
1894           (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1895             (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1896         }
1897     }
1898
1899   return TRUE;
1900 }
1901 \f
1902 /* Set the architecture.  The supported architecture is stored in the
1903    backend pointer.  We always set the architecture anyhow, since many
1904    callers ignore the return value.  */
1905
1906 bfd_boolean
1907 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1908      bfd *abfd;
1909      enum bfd_architecture arch;
1910      unsigned long machine;
1911 {
1912   bfd_default_set_arch_mach (abfd, arch, machine);
1913   return arch == ecoff_backend (abfd)->arch;
1914 }
1915
1916 /* Get the size of the section headers.  */
1917
1918 int
1919 _bfd_ecoff_sizeof_headers (abfd, reloc)
1920      bfd *abfd;
1921      bfd_boolean reloc ATTRIBUTE_UNUSED;
1922 {
1923   asection *current;
1924   int c;
1925   int ret;
1926
1927   c = 0;
1928   for (current = abfd->sections;
1929        current != (asection *)NULL;
1930        current = current->next)
1931     ++c;
1932
1933   ret = (bfd_coff_filhsz (abfd)
1934          + bfd_coff_aoutsz (abfd)
1935          + c * bfd_coff_scnhsz (abfd));
1936   return BFD_ALIGN (ret, 16);
1937 }
1938
1939 /* Get the contents of a section.  */
1940
1941 bfd_boolean
1942 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
1943      bfd *abfd;
1944      asection *section;
1945      PTR location;
1946      file_ptr offset;
1947      bfd_size_type count;
1948 {
1949   return _bfd_generic_get_section_contents (abfd, section, location,
1950                                             offset, count);
1951 }
1952
1953 /* Sort sections by VMA, but put SEC_ALLOC sections first.  This is
1954    called via qsort.  */
1955
1956 static int
1957 ecoff_sort_hdrs (arg1, arg2)
1958      const PTR arg1;
1959      const PTR arg2;
1960 {
1961   const asection *hdr1 = *(const asection **) arg1;
1962   const asection *hdr2 = *(const asection **) arg2;
1963
1964   if ((hdr1->flags & SEC_ALLOC) != 0)
1965     {
1966       if ((hdr2->flags & SEC_ALLOC) == 0)
1967         return -1;
1968     }
1969   else
1970     {
1971       if ((hdr2->flags & SEC_ALLOC) != 0)
1972         return 1;
1973     }
1974   if (hdr1->vma < hdr2->vma)
1975     return -1;
1976   else if (hdr1->vma > hdr2->vma)
1977     return 1;
1978   else
1979     return 0;
1980 }
1981
1982 /* Calculate the file position for each section, and set
1983    reloc_filepos.  */
1984
1985 static bfd_boolean
1986 ecoff_compute_section_file_positions (abfd)
1987      bfd *abfd;
1988 {
1989   file_ptr sofar, file_sofar;
1990   asection **sorted_hdrs;
1991   asection *current;
1992   unsigned int i;
1993   file_ptr old_sofar;
1994   bfd_boolean rdata_in_text;
1995   bfd_boolean first_data, first_nonalloc;
1996   const bfd_vma round = ecoff_backend (abfd)->round;
1997   bfd_size_type amt;
1998
1999   sofar = _bfd_ecoff_sizeof_headers (abfd, FALSE);
2000   file_sofar = sofar;
2001
2002   /* Sort the sections by VMA.  */
2003   amt = abfd->section_count;
2004   amt *= sizeof (asection *);
2005   sorted_hdrs = (asection **) bfd_malloc (amt);
2006   if (sorted_hdrs == NULL)
2007     return FALSE;
2008   for (current = abfd->sections, i = 0;
2009        current != NULL;
2010        current = current->next, i++)
2011     sorted_hdrs[i] = current;
2012   BFD_ASSERT (i == abfd->section_count);
2013
2014   qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2015          ecoff_sort_hdrs);
2016
2017   /* Some versions of the OSF linker put the .rdata section in the
2018      text segment, and some do not.  */
2019   rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2020   if (rdata_in_text)
2021     {
2022       for (i = 0; i < abfd->section_count; i++)
2023         {
2024           current = sorted_hdrs[i];
2025           if (strcmp (current->name, _RDATA) == 0)
2026             break;
2027           if ((current->flags & SEC_CODE) == 0
2028               && strcmp (current->name, _PDATA) != 0
2029               && strcmp (current->name, _RCONST) != 0)
2030             {
2031               rdata_in_text = FALSE;
2032               break;
2033             }
2034         }
2035     }
2036   ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2037
2038   first_data = TRUE;
2039   first_nonalloc = TRUE;
2040   for (i = 0; i < abfd->section_count; i++)
2041     {
2042       unsigned int alignment_power;
2043
2044       current = sorted_hdrs[i];
2045
2046       /* For the Alpha ECOFF .pdata section the lnnoptr field is
2047          supposed to indicate the number of .pdata entries that are
2048          really in the section.  Each entry is 8 bytes.  We store this
2049          away in line_filepos before increasing the section size.  */
2050       if (strcmp (current->name, _PDATA) == 0)
2051         current->line_filepos = current->size / 8;
2052
2053       alignment_power = current->alignment_power;
2054
2055       /* On Ultrix, the data sections in an executable file must be
2056          aligned to a page boundary within the file.  This does not
2057          affect the section size, though.  FIXME: Does this work for
2058          other platforms?  It requires some modification for the
2059          Alpha, because .rdata on the Alpha goes with the text, not
2060          the data.  */
2061       if ((abfd->flags & EXEC_P) != 0
2062           && (abfd->flags & D_PAGED) != 0
2063           && ! first_data
2064           && (current->flags & SEC_CODE) == 0
2065           && (! rdata_in_text
2066               || strcmp (current->name, _RDATA) != 0)
2067           && strcmp (current->name, _PDATA) != 0
2068           && strcmp (current->name, _RCONST) != 0)
2069         {
2070           sofar = (sofar + round - 1) &~ (round - 1);
2071           file_sofar = (file_sofar + round - 1) &~ (round - 1);
2072           first_data = FALSE;
2073         }
2074       else if (strcmp (current->name, _LIB) == 0)
2075         {
2076           /* On Irix 4, the location of contents of the .lib section
2077              from a shared library section is also rounded up to a
2078              page boundary.  */
2079
2080           sofar = (sofar + round - 1) &~ (round - 1);
2081           file_sofar = (file_sofar + round - 1) &~ (round - 1);
2082         }
2083       else if (first_nonalloc
2084                && (current->flags & SEC_ALLOC) == 0
2085                && (abfd->flags & D_PAGED) != 0)
2086         {
2087           /* Skip up to the next page for an unallocated section, such
2088              as the .comment section on the Alpha.  This leaves room
2089              for the .bss section.  */
2090           first_nonalloc = FALSE;
2091           sofar = (sofar + round - 1) &~ (round - 1);
2092           file_sofar = (file_sofar + round - 1) &~ (round - 1);
2093         }
2094
2095       /* Align the sections in the file to the same boundary on
2096          which they are aligned in virtual memory.  */
2097       sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2098       if ((current->flags & SEC_HAS_CONTENTS) != 0)
2099         file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2100
2101       if ((abfd->flags & D_PAGED) != 0
2102           && (current->flags & SEC_ALLOC) != 0)
2103         {
2104           sofar += (current->vma - sofar) % round;
2105           if ((current->flags & SEC_HAS_CONTENTS) != 0)
2106             file_sofar += (current->vma - file_sofar) % round;
2107         }
2108
2109       if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2110         current->filepos = file_sofar;
2111
2112       sofar += current->size;
2113       if ((current->flags & SEC_HAS_CONTENTS) != 0)
2114         file_sofar += current->size;
2115
2116       /* Make sure that this section is of the right size too.  */
2117       old_sofar = sofar;
2118       sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2119       if ((current->flags & SEC_HAS_CONTENTS) != 0)
2120         file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2121       current->size += sofar - old_sofar;
2122     }
2123
2124   free (sorted_hdrs);
2125   sorted_hdrs = NULL;
2126
2127   ecoff_data (abfd)->reloc_filepos = file_sofar;
2128
2129   return TRUE;
2130 }
2131
2132 /* Determine the location of the relocs for all the sections in the
2133    output file, as well as the location of the symbolic debugging
2134    information.  */
2135
2136 static bfd_size_type
2137 ecoff_compute_reloc_file_positions (abfd)
2138      bfd *abfd;
2139 {
2140   const bfd_size_type external_reloc_size =
2141     ecoff_backend (abfd)->external_reloc_size;
2142   file_ptr reloc_base;
2143   bfd_size_type reloc_size;
2144   asection *current;
2145   file_ptr sym_base;
2146
2147   if (! abfd->output_has_begun)
2148     {
2149       if (! ecoff_compute_section_file_positions (abfd))
2150         abort ();
2151       abfd->output_has_begun = TRUE;
2152     }
2153
2154   reloc_base = ecoff_data (abfd)->reloc_filepos;
2155
2156   reloc_size = 0;
2157   for (current = abfd->sections;
2158        current != (asection *)NULL;
2159        current = current->next)
2160     {
2161       if (current->reloc_count == 0)
2162         current->rel_filepos = 0;
2163       else
2164         {
2165           bfd_size_type relsize;
2166
2167           current->rel_filepos = reloc_base;
2168           relsize = current->reloc_count * external_reloc_size;
2169           reloc_size += relsize;
2170           reloc_base += relsize;
2171         }
2172     }
2173
2174   sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2175
2176   /* At least on Ultrix, the symbol table of an executable file must
2177      be aligned to a page boundary.  FIXME: Is this true on other
2178      platforms?  */
2179   if ((abfd->flags & EXEC_P) != 0
2180       && (abfd->flags & D_PAGED) != 0)
2181     sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2182                 &~ (ecoff_backend (abfd)->round - 1));
2183
2184   ecoff_data (abfd)->sym_filepos = sym_base;
2185
2186   return reloc_size;
2187 }
2188
2189 /* Set the contents of a section.  */
2190
2191 bfd_boolean
2192 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2193      bfd *abfd;
2194      asection *section;
2195      const PTR location;
2196      file_ptr offset;
2197      bfd_size_type count;
2198 {
2199   file_ptr pos;
2200
2201   /* This must be done first, because bfd_set_section_contents is
2202      going to set output_has_begun to TRUE.  */
2203   if (! abfd->output_has_begun)
2204     {
2205       if (! ecoff_compute_section_file_positions (abfd))
2206         return FALSE;
2207     }
2208
2209   /* Handle the .lib section specially so that Irix 4 shared libraries
2210      work out.  See coff_set_section_contents in coffcode.h.  */
2211   if (strcmp (section->name, _LIB) == 0)
2212     {
2213       bfd_byte *rec, *recend;
2214
2215       rec = (bfd_byte *) location;
2216       recend = rec + count;
2217       while (rec < recend)
2218         {
2219           ++section->lma;
2220           rec += bfd_get_32 (abfd, rec) * 4;
2221         }
2222
2223       BFD_ASSERT (rec == recend);
2224     }
2225
2226   if (count == 0)
2227     return TRUE;
2228
2229   pos = section->filepos + offset;
2230   if (bfd_seek (abfd, pos, SEEK_SET) != 0
2231       || bfd_bwrite (location, count, abfd) != count)
2232     return FALSE;
2233
2234   return TRUE;
2235 }
2236
2237 /* Get the GP value for an ECOFF file.  This is a hook used by
2238    nlmconv.  */
2239
2240 bfd_vma
2241 bfd_ecoff_get_gp_value (abfd)
2242      bfd *abfd;
2243 {
2244   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2245       || bfd_get_format (abfd) != bfd_object)
2246     {
2247       bfd_set_error (bfd_error_invalid_operation);
2248       return 0;
2249     }
2250
2251   return ecoff_data (abfd)->gp;
2252 }
2253
2254 /* Set the GP value for an ECOFF file.  This is a hook used by the
2255    assembler.  */
2256
2257 bfd_boolean
2258 bfd_ecoff_set_gp_value (abfd, gp_value)
2259      bfd *abfd;
2260      bfd_vma gp_value;
2261 {
2262   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2263       || bfd_get_format (abfd) != bfd_object)
2264     {
2265       bfd_set_error (bfd_error_invalid_operation);
2266       return FALSE;
2267     }
2268
2269   ecoff_data (abfd)->gp = gp_value;
2270
2271   return TRUE;
2272 }
2273
2274 /* Set the register masks for an ECOFF file.  This is a hook used by
2275    the assembler.  */
2276
2277 bfd_boolean
2278 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2279      bfd *abfd;
2280      unsigned long gprmask;
2281      unsigned long fprmask;
2282      unsigned long *cprmask;
2283 {
2284   ecoff_data_type *tdata;
2285
2286   if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2287       || bfd_get_format (abfd) != bfd_object)
2288     {
2289       bfd_set_error (bfd_error_invalid_operation);
2290       return FALSE;
2291     }
2292
2293   tdata = ecoff_data (abfd);
2294   tdata->gprmask = gprmask;
2295   tdata->fprmask = fprmask;
2296   if (cprmask != (unsigned long *) NULL)
2297     {
2298       int i;
2299
2300       for (i = 0; i < 3; i++)
2301         tdata->cprmask[i] = cprmask[i];
2302     }
2303
2304   return TRUE;
2305 }
2306
2307 /* Get ECOFF EXTR information for an external symbol.  This function
2308    is passed to bfd_ecoff_debug_externals.  */
2309
2310 static bfd_boolean
2311 ecoff_get_extr (sym, esym)
2312      asymbol *sym;
2313      EXTR *esym;
2314 {
2315   ecoff_symbol_type *ecoff_sym_ptr;
2316   bfd *input_bfd;
2317
2318   if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2319       || ecoffsymbol (sym)->native == NULL)
2320     {
2321       /* Don't include debugging, local, or section symbols.  */
2322       if ((sym->flags & BSF_DEBUGGING) != 0
2323           || (sym->flags & BSF_LOCAL) != 0
2324           || (sym->flags & BSF_SECTION_SYM) != 0)
2325         return FALSE;
2326
2327       esym->jmptbl = 0;
2328       esym->cobol_main = 0;
2329       esym->weakext = (sym->flags & BSF_WEAK) != 0;
2330       esym->reserved = 0;
2331       esym->ifd = ifdNil;
2332       /* FIXME: we can do better than this for st and sc.  */
2333       esym->asym.st = stGlobal;
2334       esym->asym.sc = scAbs;
2335       esym->asym.reserved = 0;
2336       esym->asym.index = indexNil;
2337       return TRUE;
2338     }
2339
2340   ecoff_sym_ptr = ecoffsymbol (sym);
2341
2342   if (ecoff_sym_ptr->local)
2343     return FALSE;
2344
2345   input_bfd = bfd_asymbol_bfd (sym);
2346   (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2347     (input_bfd, ecoff_sym_ptr->native, esym);
2348
2349   /* If the symbol was defined by the linker, then esym will be
2350      undefined but sym will not be.  Get a better class for such a
2351      symbol.  */
2352   if ((esym->asym.sc == scUndefined
2353        || esym->asym.sc == scSUndefined)
2354       && ! bfd_is_und_section (bfd_get_section (sym)))
2355     esym->asym.sc = scAbs;
2356
2357   /* Adjust the FDR index for the symbol by that used for the input
2358      BFD.  */
2359   if (esym->ifd != -1)
2360     {
2361       struct ecoff_debug_info *input_debug;
2362
2363       input_debug = &ecoff_data (input_bfd)->debug_info;
2364       BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2365       if (input_debug->ifdmap != (RFDT *) NULL)
2366         esym->ifd = input_debug->ifdmap[esym->ifd];
2367     }
2368
2369   return TRUE;
2370 }
2371
2372 /* Set the external symbol index.  This routine is passed to
2373    bfd_ecoff_debug_externals.  */
2374
2375 static void
2376 ecoff_set_index (sym, indx)
2377      asymbol *sym;
2378      bfd_size_type indx;
2379 {
2380   ecoff_set_sym_index (sym, indx);
2381 }
2382
2383 /* Write out an ECOFF file.  */
2384
2385 bfd_boolean
2386 _bfd_ecoff_write_object_contents (abfd)
2387      bfd *abfd;
2388 {
2389   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2390   const bfd_vma round = backend->round;
2391   const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2392   const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2393   const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2394   const bfd_size_type external_hdr_size
2395     = backend->debug_swap.external_hdr_size;
2396   const bfd_size_type external_reloc_size = backend->external_reloc_size;
2397   void (* const adjust_reloc_out)
2398   PARAMS ((bfd *, const arelent *, struct internal_reloc *))
2399     = backend->adjust_reloc_out;
2400   void (* const swap_reloc_out)
2401   PARAMS ((bfd *, const struct internal_reloc *, PTR))
2402     = backend->swap_reloc_out;
2403   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2404   HDRR * const symhdr = &debug->symbolic_header;
2405   asection *current;
2406   unsigned int count;
2407   bfd_size_type reloc_size;
2408   bfd_size_type text_size;
2409   bfd_vma text_start;
2410   bfd_boolean set_text_start;
2411   bfd_size_type data_size;
2412   bfd_vma data_start;
2413   bfd_boolean set_data_start;
2414   bfd_size_type bss_size;
2415   PTR buff = NULL;
2416   PTR reloc_buff = NULL;
2417   struct internal_filehdr internal_f;
2418   struct internal_aouthdr internal_a;
2419   int i;
2420
2421   /* Determine where the sections and relocs will go in the output
2422      file.  */
2423   reloc_size = ecoff_compute_reloc_file_positions (abfd);
2424
2425   count = 1;
2426   for (current = abfd->sections;
2427        current != (asection *)NULL;
2428        current = current->next)
2429     {
2430       current->target_index = count;
2431       ++count;
2432     }
2433
2434   if ((abfd->flags & D_PAGED) != 0)
2435     text_size = _bfd_ecoff_sizeof_headers (abfd, FALSE);
2436   else
2437     text_size = 0;
2438   text_start = 0;
2439   set_text_start = FALSE;
2440   data_size = 0;
2441   data_start = 0;
2442   set_data_start = FALSE;
2443   bss_size = 0;
2444
2445   /* Write section headers to the file.  */
2446
2447   /* Allocate buff big enough to hold a section header,
2448      file header, or a.out header.  */
2449   {
2450     bfd_size_type siz;
2451     siz = scnhsz;
2452     if (siz < filhsz)
2453       siz = filhsz;
2454     if (siz < aoutsz)
2455       siz = aoutsz;
2456     buff = (PTR) bfd_malloc (siz);
2457     if (buff == NULL)
2458       goto error_return;
2459   }
2460
2461   internal_f.f_nscns = 0;
2462   if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2463     goto error_return;
2464   for (current = abfd->sections;
2465        current != (asection *) NULL;
2466        current = current->next)
2467     {
2468       struct internal_scnhdr section;
2469       bfd_vma vma;
2470
2471       ++internal_f.f_nscns;
2472
2473       strncpy (section.s_name, current->name, sizeof section.s_name);
2474
2475       /* This seems to be correct for Irix 4 shared libraries.  */
2476       vma = bfd_get_section_vma (abfd, current);
2477       if (strcmp (current->name, _LIB) == 0)
2478         section.s_vaddr = 0;
2479       else
2480         section.s_vaddr = vma;
2481
2482       section.s_paddr = current->lma;
2483       section.s_size = current->size;
2484
2485       /* If this section is unloadable then the scnptr will be 0.  */
2486       if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2487         section.s_scnptr = 0;
2488       else
2489         section.s_scnptr = current->filepos;
2490       section.s_relptr = current->rel_filepos;
2491
2492       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2493          object file produced by the assembler is supposed to point to
2494          information about how much room is required by objects of
2495          various different sizes.  I think this only matters if we
2496          want the linker to compute the best size to use, or
2497          something.  I don't know what happens if the information is
2498          not present.  */
2499       if (strcmp (current->name, _PDATA) != 0)
2500         section.s_lnnoptr = 0;
2501       else
2502         {
2503           /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2504              hold the number of entries in the section (each entry is
2505              8 bytes).  We stored this in the line_filepos field in
2506              ecoff_compute_section_file_positions.  */
2507           section.s_lnnoptr = current->line_filepos;
2508         }
2509
2510       section.s_nreloc = current->reloc_count;
2511       section.s_nlnno = 0;
2512       section.s_flags = ecoff_sec_to_styp_flags (current->name,
2513                                                  current->flags);
2514
2515       if (bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff) == 0
2516           || bfd_bwrite (buff, scnhsz, abfd) != scnhsz)
2517         goto error_return;
2518
2519       if ((section.s_flags & STYP_TEXT) != 0
2520           || ((section.s_flags & STYP_RDATA) != 0
2521               && ecoff_data (abfd)->rdata_in_text)
2522           || section.s_flags == STYP_PDATA
2523           || (section.s_flags & STYP_DYNAMIC) != 0
2524           || (section.s_flags & STYP_LIBLIST) != 0
2525           || (section.s_flags & STYP_RELDYN) != 0
2526           || section.s_flags == STYP_CONFLIC
2527           || (section.s_flags & STYP_DYNSTR) != 0
2528           || (section.s_flags & STYP_DYNSYM) != 0
2529           || (section.s_flags & STYP_HASH) != 0
2530           || (section.s_flags & STYP_ECOFF_INIT) != 0
2531           || (section.s_flags & STYP_ECOFF_FINI) != 0
2532           || section.s_flags == STYP_RCONST)
2533         {
2534           text_size += current->size;
2535           if (! set_text_start || text_start > vma)
2536             {
2537               text_start = vma;
2538               set_text_start = TRUE;
2539             }
2540         }
2541       else if ((section.s_flags & STYP_RDATA) != 0
2542                || (section.s_flags & STYP_DATA) != 0
2543                || (section.s_flags & STYP_LITA) != 0
2544                || (section.s_flags & STYP_LIT8) != 0
2545                || (section.s_flags & STYP_LIT4) != 0
2546                || (section.s_flags & STYP_SDATA) != 0
2547                || section.s_flags == STYP_XDATA
2548                || (section.s_flags & STYP_GOT) != 0)
2549         {
2550           data_size += current->size;
2551           if (! set_data_start || data_start > vma)
2552             {
2553               data_start = vma;
2554               set_data_start = TRUE;
2555             }
2556         }
2557       else if ((section.s_flags & STYP_BSS) != 0
2558                || (section.s_flags & STYP_SBSS) != 0)
2559         bss_size += current->size;
2560       else if (section.s_flags == 0
2561                || (section.s_flags & STYP_ECOFF_LIB) != 0
2562                || section.s_flags == STYP_COMMENT)
2563         /* Do nothing.  */ ;
2564       else
2565         abort ();
2566     }
2567
2568   /* Set up the file header.  */
2569   internal_f.f_magic = ecoff_get_magic (abfd);
2570
2571   /* We will NOT put a fucking timestamp in the header here. Every
2572      time you put it back, I will come in and take it out again.  I'm
2573      sorry.  This field does not belong here.  We fill it with a 0 so
2574      it compares the same but is not a reasonable time. --
2575      gnu@cygnus.com.  */
2576   internal_f.f_timdat = 0;
2577
2578   if (bfd_get_symcount (abfd) != 0)
2579     {
2580       /* The ECOFF f_nsyms field is not actually the number of
2581          symbols, it's the size of symbolic information header.  */
2582       internal_f.f_nsyms = external_hdr_size;
2583       internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2584     }
2585   else
2586     {
2587       internal_f.f_nsyms = 0;
2588       internal_f.f_symptr = 0;
2589     }
2590
2591   internal_f.f_opthdr = aoutsz;
2592
2593   internal_f.f_flags = F_LNNO;
2594   if (reloc_size == 0)
2595     internal_f.f_flags |= F_RELFLG;
2596   if (bfd_get_symcount (abfd) == 0)
2597     internal_f.f_flags |= F_LSYMS;
2598   if (abfd->flags & EXEC_P)
2599     internal_f.f_flags |= F_EXEC;
2600
2601   if (bfd_little_endian (abfd))
2602     internal_f.f_flags |= F_AR32WR;
2603   else
2604     internal_f.f_flags |= F_AR32W;
2605
2606   /* Set up the ``optional'' header.  */
2607   if ((abfd->flags & D_PAGED) != 0)
2608     internal_a.magic = ECOFF_AOUT_ZMAGIC;
2609   else
2610     internal_a.magic = ECOFF_AOUT_OMAGIC;
2611
2612   /* FIXME: Is this really correct?  */
2613   internal_a.vstamp = symhdr->vstamp;
2614
2615   /* At least on Ultrix, these have to be rounded to page boundaries.
2616      FIXME: Is this true on other platforms?  */
2617   if ((abfd->flags & D_PAGED) != 0)
2618     {
2619       internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2620       internal_a.text_start = text_start &~ (round - 1);
2621       internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2622       internal_a.data_start = data_start &~ (round - 1);
2623     }
2624   else
2625     {
2626       internal_a.tsize = text_size;
2627       internal_a.text_start = text_start;
2628       internal_a.dsize = data_size;
2629       internal_a.data_start = data_start;
2630     }
2631
2632   /* On Ultrix, the initial portions of the .sbss and .bss segments
2633      are at the end of the data section.  The bsize field in the
2634      optional header records how many bss bytes are required beyond
2635      those in the data section.  The value is not rounded to a page
2636      boundary.  */
2637   if (bss_size < internal_a.dsize - data_size)
2638     bss_size = 0;
2639   else
2640     bss_size -= internal_a.dsize - data_size;
2641   internal_a.bsize = bss_size;
2642   internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2643
2644   internal_a.entry = bfd_get_start_address (abfd);
2645
2646   internal_a.gp_value = ecoff_data (abfd)->gp;
2647
2648   internal_a.gprmask = ecoff_data (abfd)->gprmask;
2649   internal_a.fprmask = ecoff_data (abfd)->fprmask;
2650   for (i = 0; i < 4; i++)
2651     internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2652
2653   /* Let the backend adjust the headers if necessary.  */
2654   if (backend->adjust_headers)
2655     {
2656       if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2657         goto error_return;
2658     }
2659
2660   /* Write out the file header and the optional header.  */
2661   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2662     goto error_return;
2663
2664   bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2665   if (bfd_bwrite (buff, filhsz, abfd) != filhsz)
2666     goto error_return;
2667
2668   bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2669   if (bfd_bwrite (buff, aoutsz, abfd) != aoutsz)
2670     goto error_return;
2671
2672   /* Build the external symbol information.  This must be done before
2673      writing out the relocs so that we know the symbol indices.  We
2674      don't do this if this BFD was created by the backend linker,
2675      since it will have already handled the symbols and relocs.  */
2676   if (! ecoff_data (abfd)->linker)
2677     {
2678       symhdr->iextMax = 0;
2679       symhdr->issExtMax = 0;
2680       debug->external_ext = debug->external_ext_end = NULL;
2681       debug->ssext = debug->ssext_end = NULL;
2682       if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2683                                        (abfd->flags & EXEC_P) == 0,
2684                                        ecoff_get_extr, ecoff_set_index))
2685         goto error_return;
2686
2687       /* Write out the relocs.  */
2688       for (current = abfd->sections;
2689            current != (asection *) NULL;
2690            current = current->next)
2691         {
2692           arelent **reloc_ptr_ptr;
2693           arelent **reloc_end;
2694           char *out_ptr;
2695           bfd_size_type amt;
2696
2697           if (current->reloc_count == 0)
2698             continue;
2699
2700           amt = current->reloc_count * external_reloc_size;
2701           reloc_buff = bfd_alloc (abfd, amt);
2702           if (reloc_buff == NULL)
2703             goto error_return;
2704
2705           reloc_ptr_ptr = current->orelocation;
2706           reloc_end = reloc_ptr_ptr + current->reloc_count;
2707           out_ptr = (char *) reloc_buff;
2708           for (;
2709                reloc_ptr_ptr < reloc_end;
2710                reloc_ptr_ptr++, out_ptr += external_reloc_size)
2711             {
2712               arelent *reloc;
2713               asymbol *sym;
2714               struct internal_reloc in;
2715
2716               memset ((PTR) &in, 0, sizeof in);
2717
2718               reloc = *reloc_ptr_ptr;
2719               sym = *reloc->sym_ptr_ptr;
2720
2721               in.r_vaddr = (reloc->address
2722                             + bfd_get_section_vma (abfd, current));
2723               in.r_type = reloc->howto->type;
2724
2725               if ((sym->flags & BSF_SECTION_SYM) == 0)
2726                 {
2727                   in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2728                   in.r_extern = 1;
2729                 }
2730               else
2731                 {
2732                   const char *name;
2733
2734                   name = bfd_get_section_name (abfd, bfd_get_section (sym));
2735                   if (strcmp (name, ".text") == 0)
2736                     in.r_symndx = RELOC_SECTION_TEXT;
2737                   else if (strcmp (name, ".rdata") == 0)
2738                     in.r_symndx = RELOC_SECTION_RDATA;
2739                   else if (strcmp (name, ".data") == 0)
2740                     in.r_symndx = RELOC_SECTION_DATA;
2741                   else if (strcmp (name, ".sdata") == 0)
2742                     in.r_symndx = RELOC_SECTION_SDATA;
2743                   else if (strcmp (name, ".sbss") == 0)
2744                     in.r_symndx = RELOC_SECTION_SBSS;
2745                   else if (strcmp (name, ".bss") == 0)
2746                     in.r_symndx = RELOC_SECTION_BSS;
2747                   else if (strcmp (name, ".init") == 0)
2748                     in.r_symndx = RELOC_SECTION_INIT;
2749                   else if (strcmp (name, ".lit8") == 0)
2750                     in.r_symndx = RELOC_SECTION_LIT8;
2751                   else if (strcmp (name, ".lit4") == 0)
2752                     in.r_symndx = RELOC_SECTION_LIT4;
2753                   else if (strcmp (name, ".xdata") == 0)
2754                     in.r_symndx = RELOC_SECTION_XDATA;
2755                   else if (strcmp (name, ".pdata") == 0)
2756                     in.r_symndx = RELOC_SECTION_PDATA;
2757                   else if (strcmp (name, ".fini") == 0)
2758                     in.r_symndx = RELOC_SECTION_FINI;
2759                   else if (strcmp (name, ".lita") == 0)
2760                     in.r_symndx = RELOC_SECTION_LITA;
2761                   else if (strcmp (name, "*ABS*") == 0)
2762                     in.r_symndx = RELOC_SECTION_ABS;
2763                   else if (strcmp (name, ".rconst") == 0)
2764                     in.r_symndx = RELOC_SECTION_RCONST;
2765                   else
2766                     abort ();
2767                   in.r_extern = 0;
2768                 }
2769
2770               (*adjust_reloc_out) (abfd, reloc, &in);
2771
2772               (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2773             }
2774
2775           if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2776             goto error_return;
2777           amt = current->reloc_count * external_reloc_size;
2778           if (bfd_bwrite (reloc_buff, amt, abfd) != amt)
2779             goto error_return;
2780           bfd_release (abfd, reloc_buff);
2781           reloc_buff = NULL;
2782         }
2783
2784       /* Write out the symbolic debugging information.  */
2785       if (bfd_get_symcount (abfd) > 0)
2786         {
2787           /* Write out the debugging information.  */
2788           if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2789                                        ecoff_data (abfd)->sym_filepos))
2790             goto error_return;
2791         }
2792     }
2793
2794   /* The .bss section of a demand paged executable must receive an
2795      entire page.  If there are symbols, the symbols will start on the
2796      next page.  If there are no symbols, we must fill out the page by
2797      hand.  */
2798   if (bfd_get_symcount (abfd) == 0
2799       && (abfd->flags & EXEC_P) != 0
2800       && (abfd->flags & D_PAGED) != 0)
2801     {
2802       char c;
2803
2804       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2805                     SEEK_SET) != 0)
2806         goto error_return;
2807       if (bfd_bread (&c, (bfd_size_type) 1, abfd) == 0)
2808         c = 0;
2809       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2810                     SEEK_SET) != 0)
2811         goto error_return;
2812       if (bfd_bwrite (&c, (bfd_size_type) 1, abfd) != 1)
2813         goto error_return;
2814     }
2815
2816   if (reloc_buff != NULL)
2817     bfd_release (abfd, reloc_buff);
2818   if (buff != NULL)
2819     free (buff);
2820   return TRUE;
2821  error_return:
2822   if (reloc_buff != NULL)
2823     bfd_release (abfd, reloc_buff);
2824   if (buff != NULL)
2825     free (buff);
2826   return FALSE;
2827 }
2828 \f
2829 /* Archive handling.  ECOFF uses what appears to be a unique type of
2830    archive header (armap).  The byte ordering of the armap and the
2831    contents are encoded in the name of the armap itself.  At least for
2832    now, we only support archives with the same byte ordering in the
2833    armap and the contents.
2834
2835    The first four bytes in the armap are the number of symbol
2836    definitions.  This is always a power of two.
2837
2838    This is followed by the symbol definitions.  Each symbol definition
2839    occupies 8 bytes.  The first four bytes are the offset from the
2840    start of the armap strings to the null-terminated string naming
2841    this symbol.  The second four bytes are the file offset to the
2842    archive member which defines this symbol.  If the second four bytes
2843    are 0, then this is not actually a symbol definition, and it should
2844    be ignored.
2845
2846    The symbols are hashed into the armap with a closed hashing scheme.
2847    See the functions below for the details of the algorithm.
2848
2849    After the symbol definitions comes four bytes holding the size of
2850    the string table, followed by the string table itself.  */
2851
2852 /* The name of an archive headers looks like this:
2853    __________E[BL]E[BL]_ (with a trailing space).
2854    The trailing space is changed to an X if the archive is changed to
2855    indicate that the armap is out of date.
2856
2857    The Alpha seems to use ________64E[BL]E[BL]_.  */
2858
2859 #define ARMAP_BIG_ENDIAN 'B'
2860 #define ARMAP_LITTLE_ENDIAN 'L'
2861 #define ARMAP_MARKER 'E'
2862 #define ARMAP_START_LENGTH 10
2863 #define ARMAP_HEADER_MARKER_INDEX 10
2864 #define ARMAP_HEADER_ENDIAN_INDEX 11
2865 #define ARMAP_OBJECT_MARKER_INDEX 12
2866 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2867 #define ARMAP_END_INDEX 14
2868 #define ARMAP_END "_ "
2869
2870 /* This is a magic number used in the hashing algorithm.  */
2871 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2872
2873 /* This returns the hash value to use for a string.  It also sets
2874    *REHASH to the rehash adjustment if the first slot is taken.  SIZE
2875    is the number of entries in the hash table, and HLOG is the log
2876    base 2 of SIZE.  */
2877
2878 static unsigned int
2879 ecoff_armap_hash (s, rehash, size, hlog)
2880      const char *s;
2881      unsigned int *rehash;
2882      unsigned int size;
2883      unsigned int hlog;
2884 {
2885   unsigned int hash;
2886
2887   if (hlog == 0)
2888     return 0;
2889   hash = *s++;
2890   while (*s != '\0')
2891     hash = ((hash >> 27) | (hash << 5)) + *s++;
2892   hash *= ARMAP_HASH_MAGIC;
2893   *rehash = (hash & (size - 1)) | 1;
2894   return hash >> (32 - hlog);
2895 }
2896
2897 /* Read in the armap.  */
2898
2899 bfd_boolean
2900 _bfd_ecoff_slurp_armap (abfd)
2901      bfd *abfd;
2902 {
2903   char nextname[17];
2904   unsigned int i;
2905   struct areltdata *mapdata;
2906   bfd_size_type parsed_size;
2907   char *raw_armap;
2908   struct artdata *ardata;
2909   unsigned int count;
2910   char *raw_ptr;
2911   struct symdef *symdef_ptr;
2912   char *stringbase;
2913   bfd_size_type amt;
2914
2915   /* Get the name of the first element.  */
2916   i = bfd_bread ((PTR) nextname, (bfd_size_type) 16, abfd);
2917   if (i == 0)
2918       return TRUE;
2919   if (i != 16)
2920       return FALSE;
2921
2922   if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2923     return FALSE;
2924
2925   /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2926      standard COFF armap.  We could move the ECOFF armap stuff into
2927      bfd_slurp_armap, but that seems inappropriate since no other
2928      target uses this format.  Instead, we check directly for a COFF
2929      armap.  */
2930   if (strncmp (nextname, "/               ", 16) == 0)
2931     return bfd_slurp_armap (abfd);
2932
2933   /* See if the first element is an armap.  */
2934   if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2935                ARMAP_START_LENGTH) != 0
2936       || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2937       || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2938           && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2939       || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2940       || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2941           && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2942       || strncmp (nextname + ARMAP_END_INDEX,
2943                   ARMAP_END, sizeof ARMAP_END - 1) != 0)
2944     {
2945       bfd_has_map (abfd) = FALSE;
2946       return TRUE;
2947     }
2948
2949   /* Make sure we have the right byte ordering.  */
2950   if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2951        ^ (bfd_header_big_endian (abfd)))
2952       || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2953           ^ (bfd_big_endian (abfd))))
2954     {
2955       bfd_set_error (bfd_error_wrong_format);
2956       return FALSE;
2957     }
2958
2959   /* Read in the armap.  */
2960   ardata = bfd_ardata (abfd);
2961   mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2962   if (mapdata == (struct areltdata *) NULL)
2963     return FALSE;
2964   parsed_size = mapdata->parsed_size;
2965   bfd_release (abfd, (PTR) mapdata);
2966
2967   raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2968   if (raw_armap == (char *) NULL)
2969     return FALSE;
2970
2971   if (bfd_bread ((PTR) raw_armap, parsed_size, abfd) != parsed_size)
2972     {
2973       if (bfd_get_error () != bfd_error_system_call)
2974         bfd_set_error (bfd_error_malformed_archive);
2975       bfd_release (abfd, (PTR) raw_armap);
2976       return FALSE;
2977     }
2978
2979   ardata->tdata = (PTR) raw_armap;
2980
2981   count = H_GET_32 (abfd, raw_armap);
2982
2983   ardata->symdef_count = 0;
2984   ardata->cache = (struct ar_cache *) NULL;
2985
2986   /* This code used to overlay the symdefs over the raw archive data,
2987      but that doesn't work on a 64 bit host.  */
2988   stringbase = raw_armap + count * 8 + 8;
2989
2990 #ifdef CHECK_ARMAP_HASH
2991   {
2992     unsigned int hlog;
2993
2994     /* Double check that I have the hashing algorithm right by making
2995        sure that every symbol can be looked up successfully.  */
2996     hlog = 0;
2997     for (i = 1; i < count; i <<= 1)
2998       hlog++;
2999     BFD_ASSERT (i == count);
3000
3001     raw_ptr = raw_armap + 4;
3002     for (i = 0; i < count; i++, raw_ptr += 8)
3003       {
3004         unsigned int name_offset, file_offset;
3005         unsigned int hash, rehash, srch;
3006
3007         name_offset = H_GET_32 (abfd, raw_ptr);
3008         file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3009         if (file_offset == 0)
3010           continue;
3011         hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3012                                  hlog);
3013         if (hash == i)
3014           continue;
3015
3016         /* See if we can rehash to this location.  */
3017         for (srch = (hash + rehash) & (count - 1);
3018              srch != hash && srch != i;
3019              srch = (srch + rehash) & (count - 1))
3020           BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3021         BFD_ASSERT (srch == i);
3022       }
3023   }
3024
3025 #endif /* CHECK_ARMAP_HASH */
3026
3027   raw_ptr = raw_armap + 4;
3028   for (i = 0; i < count; i++, raw_ptr += 8)
3029     if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3030       ++ardata->symdef_count;
3031
3032   amt = ardata->symdef_count;
3033   amt *= sizeof (struct symdef);
3034   symdef_ptr = (struct symdef *) bfd_alloc (abfd, amt);
3035   if (!symdef_ptr)
3036     return FALSE;
3037
3038   ardata->symdefs = (carsym *) symdef_ptr;
3039
3040   raw_ptr = raw_armap + 4;
3041   for (i = 0; i < count; i++, raw_ptr += 8)
3042     {
3043       unsigned int name_offset, file_offset;
3044
3045       file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3046       if (file_offset == 0)
3047         continue;
3048       name_offset = H_GET_32 (abfd, raw_ptr);
3049       symdef_ptr->s.name = stringbase + name_offset;
3050       symdef_ptr->file_offset = file_offset;
3051       ++symdef_ptr;
3052     }
3053
3054   ardata->first_file_filepos = bfd_tell (abfd);
3055   /* Pad to an even boundary.  */
3056   ardata->first_file_filepos += ardata->first_file_filepos % 2;
3057
3058   bfd_has_map (abfd) = TRUE;
3059
3060   return TRUE;
3061 }
3062
3063 /* Write out an armap.  */
3064
3065 bfd_boolean
3066 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3067      bfd *abfd;
3068      unsigned int elength;
3069      struct orl *map;
3070      unsigned int orl_count;
3071      int stridx;
3072 {
3073   unsigned int hashsize, hashlog;
3074   bfd_size_type symdefsize;
3075   int padit;
3076   unsigned int stringsize;
3077   unsigned int mapsize;
3078   file_ptr firstreal;
3079   struct ar_hdr hdr;
3080   struct stat statbuf;
3081   unsigned int i;
3082   bfd_byte temp[4];
3083   bfd_byte *hashtable;
3084   bfd *current;
3085   bfd *last_elt;
3086
3087   /* Ultrix appears to use as a hash table size the least power of two
3088      greater than twice the number of entries.  */
3089   for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3090     ;
3091   hashsize = 1 << hashlog;
3092
3093   symdefsize = hashsize * 8;
3094   padit = stridx % 2;
3095   stringsize = stridx + padit;
3096
3097   /* Include 8 bytes to store symdefsize and stringsize in output.  */
3098   mapsize = symdefsize + stringsize + 8;
3099
3100   firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3101
3102   memset ((PTR) &hdr, 0, sizeof hdr);
3103
3104   /* Work out the ECOFF armap name.  */
3105   strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3106   hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3107   hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3108     (bfd_header_big_endian (abfd)
3109      ? ARMAP_BIG_ENDIAN
3110      : ARMAP_LITTLE_ENDIAN);
3111   hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3112   hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3113     bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3114   memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3115
3116   /* Write the timestamp of the archive header to be just a little bit
3117      later than the timestamp of the file, otherwise the linker will
3118      complain that the index is out of date.  Actually, the Ultrix
3119      linker just checks the archive name; the GNU linker may check the
3120      date.  */
3121   stat (abfd->filename, &statbuf);
3122   sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3123
3124   /* The DECstation uses zeroes for the uid, gid and mode of the
3125      armap.  */
3126   hdr.ar_uid[0] = '0';
3127   hdr.ar_gid[0] = '0';
3128   /* Building gcc ends up extracting the armap as a file - twice.  */
3129   hdr.ar_mode[0] = '6';
3130   hdr.ar_mode[1] = '4';
3131   hdr.ar_mode[2] = '4';
3132
3133   sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3134
3135   hdr.ar_fmag[0] = '`';
3136   hdr.ar_fmag[1] = '\012';
3137
3138   /* Turn all null bytes in the header into spaces.  */
3139   for (i = 0; i < sizeof (struct ar_hdr); i++)
3140    if (((char *) (&hdr))[i] == '\0')
3141      (((char *) (&hdr))[i]) = ' ';
3142
3143   if (bfd_bwrite ((PTR) &hdr, (bfd_size_type) sizeof (struct ar_hdr), abfd)
3144       != sizeof (struct ar_hdr))
3145     return FALSE;
3146
3147   H_PUT_32 (abfd, hashsize, temp);
3148   if (bfd_bwrite ((PTR) temp, (bfd_size_type) 4, abfd) != 4)
3149     return FALSE;
3150
3151   hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3152   if (!hashtable)
3153     return FALSE;
3154
3155   current = abfd->archive_head;
3156   last_elt = current;
3157   for (i = 0; i < orl_count; i++)
3158     {
3159       unsigned int hash, rehash;
3160
3161       /* Advance firstreal to the file position of this archive
3162          element.  */
3163       if (map[i].u.abfd != last_elt)
3164         {
3165           do
3166             {
3167               firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3168               firstreal += firstreal % 2;
3169               current = current->next;
3170             }
3171           while (current != map[i].u.abfd);
3172         }
3173
3174       last_elt = current;
3175
3176       hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3177       if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3178         {
3179           unsigned int srch;
3180
3181           /* The desired slot is already taken.  */
3182           for (srch = (hash + rehash) & (hashsize - 1);
3183                srch != hash;
3184                srch = (srch + rehash) & (hashsize - 1))
3185             if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3186               break;
3187
3188           BFD_ASSERT (srch != hash);
3189
3190           hash = srch;
3191         }
3192
3193       H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3194       H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3195     }
3196
3197   if (bfd_bwrite ((PTR) hashtable, symdefsize, abfd) != symdefsize)
3198     return FALSE;
3199
3200   bfd_release (abfd, hashtable);
3201
3202   /* Now write the strings.  */
3203   H_PUT_32 (abfd, stringsize, temp);
3204   if (bfd_bwrite ((PTR) temp, (bfd_size_type) 4, abfd) != 4)
3205     return FALSE;
3206   for (i = 0; i < orl_count; i++)
3207     {
3208       bfd_size_type len;
3209
3210       len = strlen (*map[i].name) + 1;
3211       if (bfd_bwrite ((PTR) (*map[i].name), len, abfd) != len)
3212         return FALSE;
3213     }
3214
3215   /* The spec sez this should be a newline.  But in order to be
3216      bug-compatible for DECstation ar we use a null.  */
3217   if (padit)
3218     {
3219       if (bfd_bwrite ("", (bfd_size_type) 1, abfd) != 1)
3220         return FALSE;
3221     }
3222
3223   return TRUE;
3224 }
3225
3226 /* See whether this BFD is an archive.  If it is, read in the armap
3227    and the extended name table.  */
3228
3229 const bfd_target *
3230 _bfd_ecoff_archive_p (abfd)
3231      bfd *abfd;
3232 {
3233   struct artdata *tdata_hold;
3234   char armag[SARMAG + 1];
3235   bfd_size_type amt;
3236
3237   if (bfd_bread ((PTR) armag, (bfd_size_type) SARMAG, abfd) != SARMAG)
3238     {
3239       if (bfd_get_error () != bfd_error_system_call)
3240         bfd_set_error (bfd_error_wrong_format);
3241       return (const bfd_target *) NULL;
3242     }
3243
3244   if (strncmp (armag, ARMAG, SARMAG) != 0)
3245     {
3246       bfd_set_error (bfd_error_wrong_format);
3247       return NULL;
3248     }
3249
3250   tdata_hold = bfd_ardata (abfd);
3251
3252   amt = sizeof (struct artdata);
3253   bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
3254   if (bfd_ardata (abfd) == (struct artdata *) NULL)
3255     {
3256       bfd_ardata (abfd) = tdata_hold;
3257       return (const bfd_target *) NULL;
3258     }
3259
3260   bfd_ardata (abfd)->first_file_filepos = SARMAG;
3261   bfd_ardata (abfd)->cache = NULL;
3262   bfd_ardata (abfd)->archive_head = NULL;
3263   bfd_ardata (abfd)->symdefs = NULL;
3264   bfd_ardata (abfd)->extended_names = NULL;
3265   bfd_ardata (abfd)->tdata = NULL;
3266
3267   if (! _bfd_ecoff_slurp_armap (abfd)
3268       || ! _bfd_ecoff_slurp_extended_name_table (abfd))
3269     {
3270       bfd_release (abfd, bfd_ardata (abfd));
3271       bfd_ardata (abfd) = tdata_hold;
3272       return (const bfd_target *) NULL;
3273     }
3274
3275   if (bfd_has_map (abfd))
3276     {
3277       bfd *first;
3278
3279       /* This archive has a map, so we may presume that the contents
3280          are object files.  Make sure that if the first file in the
3281          archive can be recognized as an object file, it is for this
3282          target.  If not, assume that this is the wrong format.  If
3283          the first file is not an object file, somebody is doing
3284          something weird, and we permit it so that ar -t will work.  */
3285
3286       first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3287       if (first != NULL)
3288         {
3289           first->target_defaulted = FALSE;
3290           if (bfd_check_format (first, bfd_object)
3291               && first->xvec != abfd->xvec)
3292             {
3293               /* We ought to close `first' here, but we can't, because
3294                  we have no way to remove it from the archive cache.
3295                  It's almost impossible to figure out when we can
3296                  release bfd_ardata.  FIXME.  */
3297               bfd_set_error (bfd_error_wrong_object_format);
3298               bfd_ardata (abfd) = tdata_hold;
3299               return NULL;
3300             }
3301           /* And we ought to close `first' here too.  */
3302         }
3303     }
3304
3305   return abfd->xvec;
3306 }
3307 \f
3308 /* ECOFF linker code.  */
3309
3310 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3311   PARAMS ((struct bfd_hash_entry *entry,
3312            struct bfd_hash_table *table,
3313            const char *string));
3314 static bfd_boolean ecoff_link_add_archive_symbols
3315   PARAMS ((bfd *, struct bfd_link_info *));
3316 static bfd_boolean ecoff_link_check_archive_element
3317   PARAMS ((bfd *, struct bfd_link_info *, bfd_boolean *pneeded));
3318 static bfd_boolean ecoff_link_add_object_symbols
3319   PARAMS ((bfd *, struct bfd_link_info *));
3320 static bfd_boolean ecoff_link_add_externals
3321   PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3322
3323 /* Routine to create an entry in an ECOFF link hash table.  */
3324
3325 static struct bfd_hash_entry *
3326 ecoff_link_hash_newfunc (entry, table, string)
3327      struct bfd_hash_entry *entry;
3328      struct bfd_hash_table *table;
3329      const char *string;
3330 {
3331   struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3332
3333   /* Allocate the structure if it has not already been allocated by a
3334      subclass.  */
3335   if (ret == (struct ecoff_link_hash_entry *) NULL)
3336     ret = ((struct ecoff_link_hash_entry *)
3337            bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3338   if (ret == (struct ecoff_link_hash_entry *) NULL)
3339     return NULL;
3340
3341   /* Call the allocation method of the superclass.  */
3342   ret = ((struct ecoff_link_hash_entry *)
3343          _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3344                                  table, string));
3345
3346   if (ret)
3347     {
3348       /* Set local fields.  */
3349       ret->indx = -1;
3350       ret->abfd = NULL;
3351       ret->written = 0;
3352       ret->small = 0;
3353     }
3354   memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3355
3356   return (struct bfd_hash_entry *) ret;
3357 }
3358
3359 /* Create an ECOFF link hash table.  */
3360
3361 struct bfd_link_hash_table *
3362 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3363      bfd *abfd;
3364 {
3365   struct ecoff_link_hash_table *ret;
3366   bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
3367
3368   ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3369   if (ret == NULL)
3370     return NULL;
3371   if (! _bfd_link_hash_table_init (&ret->root, abfd,
3372                                    ecoff_link_hash_newfunc))
3373     {
3374       free (ret);
3375       return (struct bfd_link_hash_table *) NULL;
3376     }
3377   return &ret->root;
3378 }
3379
3380 /* Look up an entry in an ECOFF link hash table.  */
3381
3382 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3383   ((struct ecoff_link_hash_entry *) \
3384    bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3385
3386 /* Traverse an ECOFF link hash table.  */
3387
3388 #define ecoff_link_hash_traverse(table, func, info)                     \
3389   (bfd_link_hash_traverse                                               \
3390    (&(table)->root,                                                     \
3391     (bfd_boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3392     (info)))
3393
3394 /* Get the ECOFF link hash table from the info structure.  This is
3395    just a cast.  */
3396
3397 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3398
3399 /* Given an ECOFF BFD, add symbols to the global hash table as
3400    appropriate.  */
3401
3402 bfd_boolean
3403 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3404      bfd *abfd;
3405      struct bfd_link_info *info;
3406 {
3407   switch (bfd_get_format (abfd))
3408     {
3409     case bfd_object:
3410       return ecoff_link_add_object_symbols (abfd, info);
3411     case bfd_archive:
3412       return ecoff_link_add_archive_symbols (abfd, info);
3413     default:
3414       bfd_set_error (bfd_error_wrong_format);
3415       return FALSE;
3416     }
3417 }
3418
3419 /* Add the symbols from an archive file to the global hash table.
3420    This looks through the undefined symbols, looks each one up in the
3421    archive hash table, and adds any associated object file.  We do not
3422    use _bfd_generic_link_add_archive_symbols because ECOFF archives
3423    already have a hash table, so there is no reason to construct
3424    another one.  */
3425
3426 static bfd_boolean
3427 ecoff_link_add_archive_symbols (abfd, info)
3428      bfd *abfd;
3429      struct bfd_link_info *info;
3430 {
3431   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3432   const bfd_byte *raw_armap;
3433   struct bfd_link_hash_entry **pundef;
3434   unsigned int armap_count;
3435   unsigned int armap_log;
3436   unsigned int i;
3437   const bfd_byte *hashtable;
3438   const char *stringbase;
3439
3440   if (! bfd_has_map (abfd))
3441     {
3442       /* An empty archive is a special case.  */
3443       if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3444         return TRUE;
3445       bfd_set_error (bfd_error_no_armap);
3446       return FALSE;
3447     }
3448
3449   /* If we don't have any raw data for this archive, as can happen on
3450      Irix 4.0.5F, we call the generic routine.
3451      FIXME: We should be more clever about this, since someday tdata
3452      may get to something for a generic archive.  */
3453   raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3454   if (raw_armap == (bfd_byte *) NULL)
3455     return (_bfd_generic_link_add_archive_symbols
3456             (abfd, info, ecoff_link_check_archive_element));
3457
3458   armap_count = H_GET_32 (abfd, raw_armap);
3459
3460   armap_log = 0;
3461   for (i = 1; i < armap_count; i <<= 1)
3462     armap_log++;
3463   BFD_ASSERT (i == armap_count);
3464
3465   hashtable = raw_armap + 4;
3466   stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3467
3468   /* Look through the list of undefined symbols.  */
3469   pundef = &info->hash->undefs;
3470   while (*pundef != (struct bfd_link_hash_entry *) NULL)
3471     {
3472       struct bfd_link_hash_entry *h;
3473       unsigned int hash, rehash;
3474       unsigned int file_offset;
3475       const char *name;
3476       bfd *element;
3477
3478       h = *pundef;
3479
3480       /* When a symbol is defined, it is not necessarily removed from
3481          the list.  */
3482       if (h->type != bfd_link_hash_undefined
3483           && h->type != bfd_link_hash_common)
3484         {
3485           /* Remove this entry from the list, for general cleanliness
3486              and because we are going to look through the list again
3487              if we search any more libraries.  We can't remove the
3488              entry if it is the tail, because that would lose any
3489              entries we add to the list later on.  */
3490           if (*pundef != info->hash->undefs_tail)
3491             *pundef = (*pundef)->u.undef.next;
3492           else
3493             pundef = &(*pundef)->u.undef.next;
3494           continue;
3495         }
3496
3497       /* Native ECOFF linkers do not pull in archive elements merely
3498          to satisfy common definitions, so neither do we.  We leave
3499          them on the list, though, in case we are linking against some
3500          other object format.  */
3501       if (h->type != bfd_link_hash_undefined)
3502         {
3503           pundef = &(*pundef)->u.undef.next;
3504           continue;
3505         }
3506
3507       /* Look for this symbol in the archive hash table.  */
3508       hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3509                                armap_log);
3510
3511       file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3512       if (file_offset == 0)
3513         {
3514           /* Nothing in this slot.  */
3515           pundef = &(*pundef)->u.undef.next;
3516           continue;
3517         }
3518
3519       name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3520       if (name[0] != h->root.string[0]
3521           || strcmp (name, h->root.string) != 0)
3522         {
3523           unsigned int srch;
3524           bfd_boolean found;
3525
3526           /* That was the wrong symbol.  Try rehashing.  */
3527           found = FALSE;
3528           for (srch = (hash + rehash) & (armap_count - 1);
3529                srch != hash;
3530                srch = (srch + rehash) & (armap_count - 1))
3531             {
3532               file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3533               if (file_offset == 0)
3534                 break;
3535               name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3536               if (name[0] == h->root.string[0]
3537                   && strcmp (name, h->root.string) == 0)
3538                 {
3539                   found = TRUE;
3540                   break;
3541                 }
3542             }
3543
3544           if (! found)
3545             {
3546               pundef = &(*pundef)->u.undef.next;
3547               continue;
3548             }
3549
3550           hash = srch;
3551         }
3552
3553       element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
3554       if (element == (bfd *) NULL)
3555         return FALSE;
3556
3557       if (! bfd_check_format (element, bfd_object))
3558         return FALSE;
3559
3560       /* Unlike the generic linker, we know that this element provides
3561          a definition for an undefined symbol and we know that we want
3562          to include it.  We don't need to check anything.  */
3563       if (! (*info->callbacks->add_archive_element) (info, element, name))
3564         return FALSE;
3565       if (! ecoff_link_add_object_symbols (element, info))
3566         return FALSE;
3567
3568       pundef = &(*pundef)->u.undef.next;
3569     }
3570
3571   return TRUE;
3572 }
3573
3574 /* This is called if we used _bfd_generic_link_add_archive_symbols
3575    because we were not dealing with an ECOFF archive.  */
3576
3577 static bfd_boolean
3578 ecoff_link_check_archive_element (abfd, info, pneeded)
3579      bfd *abfd;
3580      struct bfd_link_info *info;
3581      bfd_boolean *pneeded;
3582 {
3583   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3584   void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3585     = backend->debug_swap.swap_ext_in;
3586   HDRR *symhdr;
3587   bfd_size_type external_ext_size;
3588   PTR external_ext = NULL;
3589   bfd_size_type esize;
3590   char *ssext = NULL;
3591   char *ext_ptr;
3592   char *ext_end;
3593
3594   *pneeded = FALSE;
3595
3596   if (! ecoff_slurp_symbolic_header (abfd))
3597     goto error_return;
3598
3599   /* If there are no symbols, we don't want it.  */
3600   if (bfd_get_symcount (abfd) == 0)
3601     goto successful_return;
3602
3603   symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3604
3605   /* Read in the external symbols and external strings.  */
3606   external_ext_size = backend->debug_swap.external_ext_size;
3607   esize = symhdr->iextMax * external_ext_size;
3608   external_ext = (PTR) bfd_malloc (esize);
3609   if (external_ext == NULL && esize != 0)
3610     goto error_return;
3611
3612   if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3613       || bfd_bread (external_ext, esize, abfd) != esize)
3614     goto error_return;
3615
3616   ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3617   if (ssext == NULL && symhdr->issExtMax != 0)
3618     goto error_return;
3619
3620   if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3621       || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3622           != (bfd_size_type) symhdr->issExtMax))
3623     goto error_return;
3624
3625   /* Look through the external symbols to see if they define some
3626      symbol that is currently undefined.  */
3627   ext_ptr = (char *) external_ext;
3628   ext_end = ext_ptr + esize;
3629   for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3630     {
3631       EXTR esym;
3632       bfd_boolean def;
3633       const char *name;
3634       struct bfd_link_hash_entry *h;
3635
3636       (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3637
3638       /* See if this symbol defines something.  */
3639       if (esym.asym.st != stGlobal
3640           && esym.asym.st != stLabel
3641           && esym.asym.st != stProc)
3642         continue;
3643
3644       switch (esym.asym.sc)
3645         {
3646         case scText:
3647         case scData:
3648         case scBss:
3649         case scAbs:
3650         case scSData:
3651         case scSBss:
3652         case scRData:
3653         case scCommon:
3654         case scSCommon:
3655         case scInit:
3656         case scFini:
3657         case scRConst:
3658           def = TRUE;
3659           break;
3660         default:
3661           def = FALSE;
3662           break;
3663         }
3664
3665       if (! def)
3666         continue;
3667
3668       name = ssext + esym.asym.iss;
3669       h = bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, TRUE);
3670
3671       /* Unlike the generic linker, we do not pull in elements because
3672          of common symbols.  */
3673       if (h == (struct bfd_link_hash_entry *) NULL
3674           || h->type != bfd_link_hash_undefined)
3675         continue;
3676
3677       /* Include this element.  */
3678       if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3679         goto error_return;
3680       if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3681         goto error_return;
3682
3683       *pneeded = TRUE;
3684       goto successful_return;
3685     }
3686
3687  successful_return:
3688   if (external_ext != NULL)
3689     free (external_ext);
3690   if (ssext != NULL)
3691     free (ssext);
3692   return TRUE;
3693  error_return:
3694   if (external_ext != NULL)
3695     free (external_ext);
3696   if (ssext != NULL)
3697     free (ssext);
3698   return FALSE;
3699 }
3700
3701 /* Add symbols from an ECOFF object file to the global linker hash
3702    table.  */
3703
3704 static bfd_boolean
3705 ecoff_link_add_object_symbols (abfd, info)
3706      bfd *abfd;
3707      struct bfd_link_info *info;
3708 {
3709   HDRR *symhdr;
3710   bfd_size_type external_ext_size;
3711   PTR external_ext = NULL;
3712   bfd_size_type esize;
3713   char *ssext = NULL;
3714   bfd_boolean result;
3715
3716   if (! ecoff_slurp_symbolic_header (abfd))
3717     return FALSE;
3718
3719   /* If there are no symbols, we don't want it.  */
3720   if (bfd_get_symcount (abfd) == 0)
3721     return TRUE;
3722
3723   symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3724
3725   /* Read in the external symbols and external strings.  */
3726   external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3727   esize = symhdr->iextMax * external_ext_size;
3728   external_ext = (PTR) bfd_malloc (esize);
3729   if (external_ext == NULL && esize != 0)
3730     goto error_return;
3731
3732   if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3733       || bfd_bread (external_ext, esize, abfd) != esize)
3734     goto error_return;
3735
3736   ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3737   if (ssext == NULL && symhdr->issExtMax != 0)
3738     goto error_return;
3739
3740   if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3741       || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3742           != (bfd_size_type) symhdr->issExtMax))
3743     goto error_return;
3744
3745   result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3746
3747   if (ssext != NULL)
3748     free (ssext);
3749   if (external_ext != NULL)
3750     free (external_ext);
3751   return result;
3752
3753  error_return:
3754   if (ssext != NULL)
3755     free (ssext);
3756   if (external_ext != NULL)
3757     free (external_ext);
3758   return FALSE;
3759 }
3760
3761 /* Add the external symbols of an object file to the global linker
3762    hash table.  The external symbols and strings we are passed are
3763    just allocated on the stack, and will be discarded.  We must
3764    explicitly save any information we may need later on in the link.
3765    We do not want to read the external symbol information again.  */
3766
3767 static bfd_boolean
3768 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3769      bfd *abfd;
3770      struct bfd_link_info *info;
3771      PTR external_ext;
3772      char *ssext;
3773 {
3774   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3775   void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3776     = backend->debug_swap.swap_ext_in;
3777   bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3778   unsigned long ext_count;
3779   struct bfd_link_hash_entry **sym_hash;
3780   char *ext_ptr;
3781   char *ext_end;
3782   bfd_size_type amt;
3783
3784   ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3785
3786   amt = ext_count;
3787   amt *= sizeof (struct bfd_link_hash_entry *);
3788   sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3789   if (!sym_hash)
3790     return FALSE;
3791   ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3792
3793   ext_ptr = (char *) external_ext;
3794   ext_end = ext_ptr + ext_count * external_ext_size;
3795   for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3796     {
3797       EXTR esym;
3798       bfd_boolean skip;
3799       bfd_vma value;
3800       asection *section;
3801       const char *name;
3802       struct ecoff_link_hash_entry *h;
3803
3804       *sym_hash = NULL;
3805
3806       (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3807
3808       /* Skip debugging symbols.  */
3809       skip = FALSE;
3810       switch (esym.asym.st)
3811         {
3812         case stGlobal:
3813         case stStatic:
3814         case stLabel:
3815         case stProc:
3816         case stStaticProc:
3817           break;
3818         default:
3819           skip = TRUE;
3820           break;
3821         }
3822
3823       if (skip)
3824         continue;
3825
3826       /* Get the information for this symbol.  */
3827       value = esym.asym.value;
3828       switch (esym.asym.sc)
3829         {
3830         default:
3831         case scNil:
3832         case scRegister:
3833         case scCdbLocal:
3834         case scBits:
3835         case scCdbSystem:
3836         case scRegImage:
3837         case scInfo:
3838         case scUserStruct:
3839         case scVar:
3840         case scVarRegister:
3841         case scVariant:
3842         case scBasedVar:
3843         case scXData:
3844         case scPData:
3845           section = NULL;
3846           break;
3847         case scText:
3848           section = bfd_make_section_old_way (abfd, ".text");
3849           value -= section->vma;
3850           break;
3851         case scData:
3852           section = bfd_make_section_old_way (abfd, ".data");
3853           value -= section->vma;
3854           break;
3855         case scBss:
3856           section = bfd_make_section_old_way (abfd, ".bss");
3857           value -= section->vma;
3858           break;
3859         case scAbs:
3860           section = bfd_abs_section_ptr;
3861           break;
3862         case scUndefined:
3863           section = bfd_und_section_ptr;
3864           break;
3865         case scSData:
3866           section = bfd_make_section_old_way (abfd, ".sdata");
3867           value -= section->vma;
3868           break;
3869         case scSBss:
3870           section = bfd_make_section_old_way (abfd, ".sbss");
3871           value -= section->vma;
3872           break;
3873         case scRData:
3874           section = bfd_make_section_old_way (abfd, ".rdata");
3875           value -= section->vma;
3876           break;
3877         case scCommon:
3878           if (value > ecoff_data (abfd)->gp_size)
3879             {
3880               section = bfd_com_section_ptr;
3881               break;
3882             }
3883           /* Fall through.  */
3884         case scSCommon:
3885           if (ecoff_scom_section.name == NULL)
3886             {
3887               /* Initialize the small common section.  */
3888               ecoff_scom_section.name = SCOMMON;
3889               ecoff_scom_section.flags = SEC_IS_COMMON;
3890               ecoff_scom_section.output_section = &ecoff_scom_section;
3891               ecoff_scom_section.symbol = &ecoff_scom_symbol;
3892               ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3893               ecoff_scom_symbol.name = SCOMMON;
3894               ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3895               ecoff_scom_symbol.section = &ecoff_scom_section;
3896               ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3897             }
3898           section = &ecoff_scom_section;
3899           break;
3900         case scSUndefined:
3901           section = bfd_und_section_ptr;
3902           break;
3903         case scInit:
3904           section = bfd_make_section_old_way (abfd, ".init");
3905           value -= section->vma;
3906           break;
3907         case scFini:
3908           section = bfd_make_section_old_way (abfd, ".fini");
3909           value -= section->vma;
3910           break;
3911         case scRConst:
3912           section = bfd_make_section_old_way (abfd, ".rconst");
3913           value -= section->vma;
3914           break;
3915         }
3916
3917       if (section == (asection *) NULL)
3918         continue;
3919
3920       name = ssext + esym.asym.iss;
3921
3922       if (! (_bfd_generic_link_add_one_symbol
3923              (info, abfd, name,
3924               (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3925               section, value, (const char *) NULL, TRUE, TRUE, sym_hash)))
3926         return FALSE;
3927
3928       h = (struct ecoff_link_hash_entry *) *sym_hash;
3929
3930       /* If we are building an ECOFF hash table, save the external
3931          symbol information.  */
3932       if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3933         {
3934           if (h->abfd == (bfd *) NULL
3935               || (! bfd_is_und_section (section)
3936                   && (! bfd_is_com_section (section)
3937                       || (h->root.type != bfd_link_hash_defined
3938                           && h->root.type != bfd_link_hash_defweak))))
3939             {
3940               h->abfd = abfd;
3941               h->esym = esym;
3942             }
3943
3944           /* Remember whether this symbol was small undefined.  */
3945           if (esym.asym.sc == scSUndefined)
3946             h->small = 1;
3947
3948           /* If this symbol was ever small undefined, it needs to wind
3949              up in a GP relative section.  We can't control the
3950              section of a defined symbol, but we can control the
3951              section of a common symbol.  This case is actually needed
3952              on Ultrix 4.2 to handle the symbol cred in -lckrb.  */
3953           if (h->small
3954               && h->root.type == bfd_link_hash_common
3955               && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
3956             {
3957               h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3958                                                                  SCOMMON);
3959               h->root.u.c.p->section->flags = SEC_ALLOC;
3960               if (h->esym.asym.sc == scCommon)
3961                 h->esym.asym.sc = scSCommon;
3962             }
3963         }
3964     }
3965
3966   return TRUE;
3967 }
3968 \f
3969 /* ECOFF final link routines.  */
3970
3971 static bfd_boolean ecoff_final_link_debug_accumulate
3972   PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
3973            PTR handle));
3974 static bfd_boolean ecoff_link_write_external
3975   PARAMS ((struct ecoff_link_hash_entry *, PTR));
3976 static bfd_boolean ecoff_indirect_link_order
3977   PARAMS ((bfd *, struct bfd_link_info *, asection *,
3978            struct bfd_link_order *));
3979 static bfd_boolean ecoff_reloc_link_order
3980   PARAMS ((bfd *, struct bfd_link_info *, asection *,
3981            struct bfd_link_order *));
3982
3983 /* Structure used to pass information to ecoff_link_write_external.  */
3984
3985 struct extsym_info
3986 {
3987   bfd *abfd;
3988   struct bfd_link_info *info;
3989 };
3990
3991 /* ECOFF final link routine.  This looks through all the input BFDs
3992    and gathers together all the debugging information, and then
3993    processes all the link order information.  This may cause it to
3994    close and reopen some input BFDs; I'll see how bad this is.  */
3995
3996 bfd_boolean
3997 _bfd_ecoff_bfd_final_link (abfd, info)
3998      bfd *abfd;
3999      struct bfd_link_info *info;
4000 {
4001   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4002   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4003   HDRR *symhdr;
4004   PTR handle;
4005   register bfd *input_bfd;
4006   asection *o;
4007   struct bfd_link_order *p;
4008   struct extsym_info einfo;
4009
4010   /* We accumulate the debugging information counts in the symbolic
4011      header.  */
4012   symhdr = &debug->symbolic_header;
4013   symhdr->vstamp = 0;
4014   symhdr->ilineMax = 0;
4015   symhdr->cbLine = 0;
4016   symhdr->idnMax = 0;
4017   symhdr->ipdMax = 0;
4018   symhdr->isymMax = 0;
4019   symhdr->ioptMax = 0;
4020   symhdr->iauxMax = 0;
4021   symhdr->issMax = 0;
4022   symhdr->issExtMax = 0;
4023   symhdr->ifdMax = 0;
4024   symhdr->crfd = 0;
4025   symhdr->iextMax = 0;
4026
4027   /* We accumulate the debugging information itself in the debug_info
4028      structure.  */
4029   debug->line = NULL;
4030   debug->external_dnr = NULL;
4031   debug->external_pdr = NULL;
4032   debug->external_sym = NULL;
4033   debug->external_opt = NULL;
4034   debug->external_aux = NULL;
4035   debug->ss = NULL;
4036   debug->ssext = debug->ssext_end = NULL;
4037   debug->external_fdr = NULL;
4038   debug->external_rfd = NULL;
4039   debug->external_ext = debug->external_ext_end = NULL;
4040
4041   handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4042   if (handle == (PTR) NULL)
4043     return FALSE;
4044
4045   /* Accumulate the debugging symbols from each input BFD.  */
4046   for (input_bfd = info->input_bfds;
4047        input_bfd != (bfd *) NULL;
4048        input_bfd = input_bfd->link_next)
4049     {
4050       bfd_boolean ret;
4051
4052       if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4053         {
4054           /* Arbitrarily set the symbolic header vstamp to the vstamp
4055              of the first object file in the link.  */
4056           if (symhdr->vstamp == 0)
4057             symhdr->vstamp
4058               = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4059           ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4060                                                    handle);
4061         }
4062       else
4063         ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4064                                                 debug, &backend->debug_swap,
4065                                                 input_bfd, info);
4066       if (! ret)
4067         return FALSE;
4068
4069       /* Combine the register masks.  */
4070       ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4071       ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4072       ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4073       ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4074       ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4075       ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4076     }
4077
4078   /* Write out the external symbols.  */
4079   einfo.abfd = abfd;
4080   einfo.info = info;
4081   ecoff_link_hash_traverse (ecoff_hash_table (info),
4082                             ecoff_link_write_external,
4083                             (PTR) &einfo);
4084
4085   if (info->relocatable)
4086     {
4087       /* We need to make a pass over the link_orders to count up the
4088          number of relocations we will need to output, so that we know
4089          how much space they will take up.  */
4090       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4091         {
4092           o->reloc_count = 0;
4093           for (p = o->link_order_head;
4094                p != (struct bfd_link_order *) NULL;
4095                p = p->next)
4096             if (p->type == bfd_indirect_link_order)
4097               o->reloc_count += p->u.indirect.section->reloc_count;
4098             else if (p->type == bfd_section_reloc_link_order
4099                      || p->type == bfd_symbol_reloc_link_order)
4100               ++o->reloc_count;
4101         }
4102     }
4103
4104   /* Compute the reloc and symbol file positions.  */
4105   ecoff_compute_reloc_file_positions (abfd);
4106
4107   /* Write out the debugging information.  */
4108   if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4109                                            &backend->debug_swap, info,
4110                                            ecoff_data (abfd)->sym_filepos))
4111     return FALSE;
4112
4113   bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4114
4115   if (info->relocatable)
4116     {
4117       /* Now reset the reloc_count field of the sections in the output
4118          BFD to 0, so that we can use them to keep track of how many
4119          relocs we have output thus far.  */
4120       for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4121         o->reloc_count = 0;
4122     }
4123
4124   /* Get a value for the GP register.  */
4125   if (ecoff_data (abfd)->gp == 0)
4126     {
4127       struct bfd_link_hash_entry *h;
4128
4129       h = bfd_link_hash_lookup (info->hash, "_gp", FALSE, FALSE, TRUE);
4130       if (h != (struct bfd_link_hash_entry *) NULL
4131           && h->type == bfd_link_hash_defined)
4132         ecoff_data (abfd)->gp = (h->u.def.value
4133                                  + h->u.def.section->output_section->vma
4134                                  + h->u.def.section->output_offset);
4135       else if (info->relocatable)
4136         {
4137           bfd_vma lo;
4138
4139           /* Make up a value.  */
4140           lo = (bfd_vma) -1;
4141           for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4142             {
4143               if (o->vma < lo
4144                   && (strcmp (o->name, _SBSS) == 0
4145                       || strcmp (o->name, _SDATA) == 0
4146                       || strcmp (o->name, _LIT4) == 0
4147                       || strcmp (o->name, _LIT8) == 0
4148                       || strcmp (o->name, _LITA) == 0))
4149                 lo = o->vma;
4150             }
4151           ecoff_data (abfd)->gp = lo + 0x8000;
4152         }
4153       else
4154         {
4155           /* If the relocate_section function needs to do a reloc
4156              involving the GP value, it should make a reloc_dangerous
4157              callback to warn that GP is not defined.  */
4158         }
4159     }
4160
4161   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4162     {
4163       for (p = o->link_order_head;
4164            p != (struct bfd_link_order *) NULL;
4165            p = p->next)
4166         {
4167           if (p->type == bfd_indirect_link_order
4168               && (bfd_get_flavour (p->u.indirect.section->owner)
4169                   == bfd_target_ecoff_flavour))
4170             {
4171               if (! ecoff_indirect_link_order (abfd, info, o, p))
4172                 return FALSE;
4173             }
4174           else if (p->type == bfd_section_reloc_link_order
4175                    || p->type == bfd_symbol_reloc_link_order)
4176             {
4177               if (! ecoff_reloc_link_order (abfd, info, o, p))
4178                 return FALSE;
4179             }
4180           else
4181             {
4182               if (! _bfd_default_link_order (abfd, info, o, p))
4183                 return FALSE;
4184             }
4185         }
4186     }
4187
4188   bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4189
4190   ecoff_data (abfd)->linker = TRUE;
4191
4192   return TRUE;
4193 }
4194
4195 /* Accumulate the debugging information for an input BFD into the
4196    output BFD.  This must read in the symbolic information of the
4197    input BFD.  */
4198
4199 static bfd_boolean
4200 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4201      bfd *output_bfd;
4202      bfd *input_bfd;
4203      struct bfd_link_info *info;
4204      PTR handle;
4205 {
4206   struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4207   const struct ecoff_debug_swap * const swap =
4208     &ecoff_backend (input_bfd)->debug_swap;
4209   HDRR *symhdr = &debug->symbolic_header;
4210   bfd_boolean ret;
4211
4212 #define READ(ptr, offset, count, size, type)                             \
4213   if (symhdr->count == 0)                                                \
4214     debug->ptr = NULL;                                                   \
4215   else                                                                   \
4216     {                                                                    \
4217       bfd_size_type amt = (bfd_size_type) size * symhdr->count;          \
4218       debug->ptr = (type) bfd_malloc (amt);                              \
4219       if (debug->ptr == NULL)                                            \
4220         {                                                                \
4221           ret = FALSE;                                                   \
4222           goto return_something;                                         \
4223         }                                                                \
4224       if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
4225           || bfd_bread (debug->ptr, amt, input_bfd) != amt)              \
4226         {                                                                \
4227           ret = FALSE;                                                   \
4228           goto return_something;                                         \
4229         }                                                                \
4230     }
4231
4232   /* If raw_syments is not NULL, then the data was already by read by
4233      _bfd_ecoff_slurp_symbolic_info.  */
4234   if (ecoff_data (input_bfd)->raw_syments == NULL)
4235     {
4236       READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4237             unsigned char *);
4238       READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4239       READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4240       READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4241       READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4242       READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4243             union aux_ext *);
4244       READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4245       READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4246       READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4247     }
4248 #undef READ
4249
4250   /* We do not read the external strings or the external symbols.  */
4251
4252   ret = (bfd_ecoff_debug_accumulate
4253          (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4254           &ecoff_backend (output_bfd)->debug_swap,
4255           input_bfd, debug, swap, info));
4256
4257  return_something:
4258   if (ecoff_data (input_bfd)->raw_syments == NULL)
4259     {
4260       if (debug->line != NULL)
4261         free (debug->line);
4262       if (debug->external_dnr != NULL)
4263         free (debug->external_dnr);
4264       if (debug->external_pdr != NULL)
4265         free (debug->external_pdr);
4266       if (debug->external_sym != NULL)
4267         free (debug->external_sym);
4268       if (debug->external_opt != NULL)
4269         free (debug->external_opt);
4270       if (debug->external_aux != NULL)
4271         free (debug->external_aux);
4272       if (debug->ss != NULL)
4273         free (debug->ss);
4274       if (debug->external_fdr != NULL)
4275         free (debug->external_fdr);
4276       if (debug->external_rfd != NULL)
4277         free (debug->external_rfd);
4278
4279       /* Make sure we don't accidentally follow one of these pointers
4280          into freed memory.  */
4281       debug->line = NULL;
4282       debug->external_dnr = NULL;
4283       debug->external_pdr = NULL;
4284       debug->external_sym = NULL;
4285       debug->external_opt = NULL;
4286       debug->external_aux = NULL;
4287       debug->ss = NULL;
4288       debug->external_fdr = NULL;
4289       debug->external_rfd = NULL;
4290     }
4291
4292   return ret;
4293 }
4294
4295 /* Put out information for an external symbol.  These come only from
4296    the hash table.  */
4297
4298 static bfd_boolean
4299 ecoff_link_write_external (h, data)
4300      struct ecoff_link_hash_entry *h;
4301      PTR data;
4302 {
4303   struct extsym_info *einfo = (struct extsym_info *) data;
4304   bfd *output_bfd = einfo->abfd;
4305   bfd_boolean strip;
4306
4307   if (h->root.type == bfd_link_hash_warning)
4308     {
4309       h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4310       if (h->root.type == bfd_link_hash_new)
4311         return TRUE;
4312     }
4313
4314   /* We need to check if this symbol is being stripped.  */
4315   if (h->root.type == bfd_link_hash_undefined
4316       || h->root.type == bfd_link_hash_undefweak)
4317     strip = FALSE;
4318   else if (einfo->info->strip == strip_all
4319            || (einfo->info->strip == strip_some
4320                && bfd_hash_lookup (einfo->info->keep_hash,
4321                                    h->root.root.string,
4322                                    FALSE, FALSE) == NULL))
4323     strip = TRUE;
4324   else
4325     strip = FALSE;
4326
4327   if (strip || h->written)
4328     return TRUE;
4329
4330   if (h->abfd == (bfd *) NULL)
4331     {
4332       h->esym.jmptbl = 0;
4333       h->esym.cobol_main = 0;
4334       h->esym.weakext = 0;
4335       h->esym.reserved = 0;
4336       h->esym.ifd = ifdNil;
4337       h->esym.asym.value = 0;
4338       h->esym.asym.st = stGlobal;
4339
4340       if (h->root.type != bfd_link_hash_defined
4341           && h->root.type != bfd_link_hash_defweak)
4342         h->esym.asym.sc = scAbs;
4343       else
4344         {
4345           asection *output_section;
4346           const char *name;
4347
4348           output_section = h->root.u.def.section->output_section;
4349           name = bfd_section_name (output_section->owner, output_section);
4350
4351           if (strcmp (name, _TEXT) == 0)
4352             h->esym.asym.sc = scText;
4353           else if (strcmp (name, _DATA) == 0)
4354             h->esym.asym.sc = scData;
4355           else if (strcmp (name, _SDATA) == 0)
4356             h->esym.asym.sc = scSData;
4357           else if (strcmp (name, _RDATA) == 0)
4358             h->esym.asym.sc = scRData;
4359           else if (strcmp (name, _BSS) == 0)
4360             h->esym.asym.sc = scBss;
4361           else if (strcmp (name, _SBSS) == 0)
4362             h->esym.asym.sc = scSBss;
4363           else if (strcmp (name, _INIT) == 0)
4364             h->esym.asym.sc = scInit;
4365           else if (strcmp (name, _FINI) == 0)
4366             h->esym.asym.sc = scFini;
4367           else if (strcmp (name, _PDATA) == 0)
4368             h->esym.asym.sc = scPData;
4369           else if (strcmp (name, _XDATA) == 0)
4370             h->esym.asym.sc = scXData;
4371           else if (strcmp (name, _RCONST) == 0)
4372             h->esym.asym.sc = scRConst;
4373           else
4374             h->esym.asym.sc = scAbs;
4375         }
4376
4377       h->esym.asym.reserved = 0;
4378       h->esym.asym.index = indexNil;
4379     }
4380   else if (h->esym.ifd != -1)
4381     {
4382       struct ecoff_debug_info *debug;
4383
4384       /* Adjust the FDR index for the symbol by that used for the
4385          input BFD.  */
4386       debug = &ecoff_data (h->abfd)->debug_info;
4387       BFD_ASSERT (h->esym.ifd >= 0
4388                   && h->esym.ifd < debug->symbolic_header.ifdMax);
4389       h->esym.ifd = debug->ifdmap[h->esym.ifd];
4390     }
4391
4392   switch (h->root.type)
4393     {
4394     default:
4395     case bfd_link_hash_warning:
4396     case bfd_link_hash_new:
4397       abort ();
4398     case bfd_link_hash_undefined:
4399     case bfd_link_hash_undefweak:
4400       if (h->esym.asym.sc != scUndefined
4401           && h->esym.asym.sc != scSUndefined)
4402         h->esym.asym.sc = scUndefined;
4403       break;
4404     case bfd_link_hash_defined:
4405     case bfd_link_hash_defweak:
4406       if (h->esym.asym.sc == scUndefined
4407           || h->esym.asym.sc == scSUndefined)
4408         h->esym.asym.sc = scAbs;
4409       else if (h->esym.asym.sc == scCommon)
4410         h->esym.asym.sc = scBss;
4411       else if (h->esym.asym.sc == scSCommon)
4412         h->esym.asym.sc = scSBss;
4413       h->esym.asym.value = (h->root.u.def.value
4414                             + h->root.u.def.section->output_section->vma
4415                             + h->root.u.def.section->output_offset);
4416       break;
4417     case bfd_link_hash_common:
4418       if (h->esym.asym.sc != scCommon
4419           && h->esym.asym.sc != scSCommon)
4420         h->esym.asym.sc = scCommon;
4421       h->esym.asym.value = h->root.u.c.size;
4422       break;
4423     case bfd_link_hash_indirect:
4424       /* We ignore these symbols, since the indirected symbol is
4425          already in the hash table.  */
4426       return TRUE;
4427     }
4428
4429   /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4430      symbol number.  */
4431   h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4432   h->written = 1;
4433
4434   return (bfd_ecoff_debug_one_external
4435           (output_bfd, &ecoff_data (output_bfd)->debug_info,
4436            &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4437            &h->esym));
4438 }
4439
4440 /* Relocate and write an ECOFF section into an ECOFF output file.  */
4441
4442 static bfd_boolean
4443 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4444      bfd *output_bfd;
4445      struct bfd_link_info *info;
4446      asection *output_section;
4447      struct bfd_link_order *link_order;
4448 {
4449   asection *input_section;
4450   bfd *input_bfd;
4451   bfd_byte *contents = NULL;
4452   bfd_size_type external_reloc_size;
4453   bfd_size_type external_relocs_size;
4454   PTR external_relocs = NULL;
4455
4456   BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4457
4458   if (link_order->size == 0)
4459     return TRUE;
4460
4461   input_section = link_order->u.indirect.section;
4462   input_bfd = input_section->owner;
4463
4464   BFD_ASSERT (input_section->output_section == output_section);
4465   BFD_ASSERT (input_section->output_offset == link_order->offset);
4466   BFD_ASSERT (input_section->size == link_order->size);
4467
4468   /* Get the section contents.  */
4469   if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
4470     goto error_return;
4471
4472   /* Get the relocs.  If we are relaxing MIPS code, they will already
4473      have been read in.  Otherwise, we read them in now.  */
4474   external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4475   external_relocs_size = external_reloc_size * input_section->reloc_count;
4476
4477   external_relocs = (PTR) bfd_malloc (external_relocs_size);
4478   if (external_relocs == NULL && external_relocs_size != 0)
4479     goto error_return;
4480
4481   if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4482       || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
4483           != external_relocs_size))
4484     goto error_return;
4485
4486   /* Relocate the section contents.  */
4487   if (! ((*ecoff_backend (input_bfd)->relocate_section)
4488          (output_bfd, info, input_bfd, input_section, contents,
4489           external_relocs)))
4490     goto error_return;
4491
4492   /* Write out the relocated section.  */
4493   if (! bfd_set_section_contents (output_bfd,
4494                                   output_section,
4495                                   contents,
4496                                   input_section->output_offset,
4497                                   input_section->size))
4498     goto error_return;
4499
4500   /* If we are producing relocatable output, the relocs were
4501      modified, and we write them out now.  We use the reloc_count
4502      field of output_section to keep track of the number of relocs we
4503      have output so far.  */
4504   if (info->relocatable)
4505     {
4506       file_ptr pos = (output_section->rel_filepos
4507                       + output_section->reloc_count * external_reloc_size);
4508       if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4509           || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
4510               != external_relocs_size))
4511         goto error_return;
4512       output_section->reloc_count += input_section->reloc_count;
4513     }
4514
4515   if (contents != NULL)
4516     free (contents);
4517   if (external_relocs != NULL)
4518     free (external_relocs);
4519   return TRUE;
4520
4521  error_return:
4522   if (contents != NULL)
4523     free (contents);
4524   if (external_relocs != NULL)
4525     free (external_relocs);
4526   return FALSE;
4527 }
4528
4529 /* Generate a reloc when linking an ECOFF file.  This is a reloc
4530    requested by the linker, and does come from any input file.  This
4531    is used to build constructor and destructor tables when linking
4532    with -Ur.  */
4533
4534 static bfd_boolean
4535 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4536      bfd *output_bfd;
4537      struct bfd_link_info *info;
4538      asection *output_section;
4539      struct bfd_link_order *link_order;
4540 {
4541   enum bfd_link_order_type type;
4542   asection *section;
4543   bfd_vma addend;
4544   arelent rel;
4545   struct internal_reloc in;
4546   bfd_size_type external_reloc_size;
4547   bfd_byte *rbuf;
4548   bfd_boolean ok;
4549   file_ptr pos;
4550
4551   type = link_order->type;
4552   section = NULL;
4553   addend = link_order->u.reloc.p->addend;
4554
4555   /* We set up an arelent to pass to the backend adjust_reloc_out
4556      routine.  */
4557   rel.address = link_order->offset;
4558
4559   rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4560   if (rel.howto == 0)
4561     {
4562       bfd_set_error (bfd_error_bad_value);
4563       return FALSE;
4564     }
4565
4566   if (type == bfd_section_reloc_link_order)
4567     {
4568       section = link_order->u.reloc.p->u.section;
4569       rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4570     }
4571   else
4572     {
4573       struct bfd_link_hash_entry *h;
4574
4575       /* Treat a reloc against a defined symbol as though it were
4576          actually against the section.  */
4577       h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4578                                         link_order->u.reloc.p->u.name,
4579                                         FALSE, FALSE, FALSE);
4580       if (h != NULL
4581           && (h->type == bfd_link_hash_defined
4582               || h->type == bfd_link_hash_defweak))
4583         {
4584           type = bfd_section_reloc_link_order;
4585           section = h->u.def.section->output_section;
4586           /* It seems that we ought to add the symbol value to the
4587              addend here, but in practice it has already been added
4588              because it was passed to constructor_callback.  */
4589           addend += section->vma + h->u.def.section->output_offset;
4590         }
4591       else
4592         {
4593           /* We can't set up a reloc against a symbol correctly,
4594              because we have no asymbol structure.  Currently no
4595              adjust_reloc_out routine cares.  */
4596           rel.sym_ptr_ptr = (asymbol **) NULL;
4597         }
4598     }
4599
4600   /* All ECOFF relocs are in-place.  Put the addend into the object
4601      file.  */
4602
4603   BFD_ASSERT (rel.howto->partial_inplace);
4604   if (addend != 0)
4605     {
4606       bfd_size_type size;
4607       bfd_reloc_status_type rstat;
4608       bfd_byte *buf;
4609
4610       size = bfd_get_reloc_size (rel.howto);
4611       buf = (bfd_byte *) bfd_zmalloc (size);
4612       if (buf == (bfd_byte *) NULL)
4613         return FALSE;
4614       rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4615                                       (bfd_vma) addend, buf);
4616       switch (rstat)
4617         {
4618         case bfd_reloc_ok:
4619           break;
4620         default:
4621         case bfd_reloc_outofrange:
4622           abort ();
4623         case bfd_reloc_overflow:
4624           if (! ((*info->callbacks->reloc_overflow)
4625                  (info, NULL,
4626                   (link_order->type == bfd_section_reloc_link_order
4627                    ? bfd_section_name (output_bfd, section)
4628                    : link_order->u.reloc.p->u.name),
4629                   rel.howto->name, addend, (bfd *) NULL,
4630                   (asection *) NULL, (bfd_vma) 0)))
4631             {
4632               free (buf);
4633               return FALSE;
4634             }
4635           break;
4636         }
4637       ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4638                                      (file_ptr) link_order->offset, size);
4639       free (buf);
4640       if (! ok)
4641         return FALSE;
4642     }
4643
4644   rel.addend = 0;
4645
4646   /* Move the information into an internal_reloc structure.  */
4647   in.r_vaddr = (rel.address
4648                 + bfd_get_section_vma (output_bfd, output_section));
4649   in.r_type = rel.howto->type;
4650
4651   if (type == bfd_symbol_reloc_link_order)
4652     {
4653       struct ecoff_link_hash_entry *h;
4654
4655       h = ((struct ecoff_link_hash_entry *)
4656            bfd_wrapped_link_hash_lookup (output_bfd, info,
4657                                          link_order->u.reloc.p->u.name,
4658                                          FALSE, FALSE, TRUE));
4659       if (h != (struct ecoff_link_hash_entry *) NULL
4660           && h->indx != -1)
4661         in.r_symndx = h->indx;
4662       else
4663         {
4664           if (! ((*info->callbacks->unattached_reloc)
4665                  (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4666                   (asection *) NULL, (bfd_vma) 0)))
4667             return FALSE;
4668           in.r_symndx = 0;
4669         }
4670       in.r_extern = 1;
4671     }
4672   else
4673     {
4674       const char *name;
4675
4676       name = bfd_get_section_name (output_bfd, section);
4677       if (strcmp (name, ".text") == 0)
4678         in.r_symndx = RELOC_SECTION_TEXT;
4679       else if (strcmp (name, ".rdata") == 0)
4680         in.r_symndx = RELOC_SECTION_RDATA;
4681       else if (strcmp (name, ".data") == 0)
4682         in.r_symndx = RELOC_SECTION_DATA;
4683       else if (strcmp (name, ".sdata") == 0)
4684         in.r_symndx = RELOC_SECTION_SDATA;
4685       else if (strcmp (name, ".sbss") == 0)
4686         in.r_symndx = RELOC_SECTION_SBSS;
4687       else if (strcmp (name, ".bss") == 0)
4688         in.r_symndx = RELOC_SECTION_BSS;
4689       else if (strcmp (name, ".init") == 0)
4690         in.r_symndx = RELOC_SECTION_INIT;
4691       else if (strcmp (name, ".lit8") == 0)
4692         in.r_symndx = RELOC_SECTION_LIT8;
4693       else if (strcmp (name, ".lit4") == 0)
4694         in.r_symndx = RELOC_SECTION_LIT4;
4695       else if (strcmp (name, ".xdata") == 0)
4696         in.r_symndx = RELOC_SECTION_XDATA;
4697       else if (strcmp (name, ".pdata") == 0)
4698         in.r_symndx = RELOC_SECTION_PDATA;
4699       else if (strcmp (name, ".fini") == 0)
4700         in.r_symndx = RELOC_SECTION_FINI;
4701       else if (strcmp (name, ".lita") == 0)
4702         in.r_symndx = RELOC_SECTION_LITA;
4703       else if (strcmp (name, "*ABS*") == 0)
4704         in.r_symndx = RELOC_SECTION_ABS;
4705       else if (strcmp (name, ".rconst") == 0)
4706         in.r_symndx = RELOC_SECTION_RCONST;
4707       else
4708         abort ();
4709       in.r_extern = 0;
4710     }
4711
4712   /* Let the BFD backend adjust the reloc.  */
4713   (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4714
4715   /* Get some memory and swap out the reloc.  */
4716   external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4717   rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4718   if (rbuf == (bfd_byte *) NULL)
4719     return FALSE;
4720
4721   (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4722
4723   pos = (output_section->rel_filepos
4724          + output_section->reloc_count * external_reloc_size);
4725   ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4726         && (bfd_bwrite ((PTR) rbuf, external_reloc_size, output_bfd)
4727             == external_reloc_size));
4728
4729   if (ok)
4730     ++output_section->reloc_count;
4731
4732   free (rbuf);
4733
4734   return ok;
4735 }