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