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