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