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