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