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