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