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