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