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