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