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