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