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