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