fb4960afa3640d7cafbe2f19869cb008858595d6
[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 void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
49                                            asymbol *asym, int ext,
50                                            asymbol **indirect_ptr_ptr));
51 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
52                                           RNDXR *rndx, long isym,
53                                           CONST char *which));
54 static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
55                                            unsigned int indx, int bigendian));
56 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
57                                                 asymbol **symbols));
58 static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
59 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
60 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
61 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
62                                               unsigned int *rehash,
63                                               unsigned int size,
64                                               unsigned int hlog));
65 \f
66 /* This stuff is somewhat copied from coffcode.h.  */
67
68 static asection bfd_debug_section = { "*DEBUG*" };
69
70 /* Create an ECOFF object.  */
71
72 boolean
73 ecoff_mkobject (abfd)
74      bfd *abfd;
75 {
76   abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
77                                 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
78   if (abfd->tdata.ecoff_obj_data == NULL)
79     {
80       bfd_error = no_memory;
81       return false;
82     }
83
84   return true;
85 }
86
87 /* This is a hook called by coff_real_object_p to create any backend
88    specific information.  */
89
90 PTR
91 ecoff_mkobject_hook (abfd, filehdr, aouthdr)
92      bfd *abfd;
93      PTR filehdr;
94      PTR aouthdr;
95 {
96   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
97   struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
98   ecoff_data_type *ecoff;
99   asection *regsec;
100
101   if (ecoff_mkobject (abfd) == false)
102     return NULL;
103
104   ecoff = ecoff_data (abfd);
105   ecoff->gp_size = 8;
106   ecoff->sym_filepos = internal_f->f_symptr;
107
108   /* Create the .reginfo section to give programs outside BFD a way to
109      see the information stored in the a.out header.  See the comment
110      in coff/ecoff.h.  */
111   regsec = bfd_make_section (abfd, REGINFO);
112   if (regsec == NULL)
113     return NULL;
114   /* Tell the linker to leave this section completely alone.  */
115   regsec->flags = SEC_SHARED_LIBRARY;
116
117   if (internal_a != (struct internal_aouthdr *) NULL)
118     {
119       int i;
120
121       ecoff->text_start = internal_a->text_start;
122       ecoff->text_end = internal_a->text_start + internal_a->tsize;
123       ecoff->gp = internal_a->gp_value;
124       ecoff->gprmask = internal_a->gprmask;
125       for (i = 0; i < 4; i++)
126         ecoff->cprmask[i] = internal_a->cprmask[i];
127       ecoff->fprmask = internal_a->fprmask;
128       if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
129         abfd->flags |= D_PAGED;
130     }
131
132   /* It turns out that no special action is required by the MIPS or
133      Alpha ECOFF backends.  They have different information in the
134      a.out header, but we just copy it all (e.g., gprmask, cprmask and
135      fprmask) and let the swapping routines ensure that only relevant
136      information is written out.  */
137
138   return (PTR) ecoff;
139 }
140
141 /* This is a hook needed by SCO COFF, but we have nothing to do.  */
142
143 /*ARGSUSED*/
144 asection *
145 ecoff_make_section_hook (abfd, name)
146      bfd *abfd;
147      char *name;
148 {
149   return (asection *) NULL;
150 }
151
152 /* Initialize a new section.  */
153
154 boolean
155 ecoff_new_section_hook (abfd, section)
156      bfd *abfd;
157      asection *section;
158 {
159   section->alignment_power = abfd->xvec->align_power_min;
160
161   if (strcmp (section->name, _TEXT) == 0)
162     section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
163   else if (strcmp (section->name, _DATA) == 0
164            || strcmp (section->name, _SDATA) == 0)
165     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
166   else if (strcmp (section->name, _RDATA) == 0
167            || strcmp (section->name, _LIT8) == 0
168            || strcmp (section->name, _LIT4) == 0)
169     section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
170   else if (strcmp (section->name, _BSS) == 0
171            || strcmp (section->name, _SBSS) == 0)
172     section->flags |= SEC_ALLOC;
173   else if (strcmp (section->name, REGINFO) == 0)
174     {
175       section->flags |= SEC_HAS_CONTENTS | SEC_NEVER_LOAD;
176       section->_raw_size = sizeof (struct ecoff_reginfo);
177     }
178
179   /* Probably any other section name is SEC_NEVER_LOAD, but I'm
180      uncertain about .init on some systems and I don't know how shared
181      libraries work.  */
182
183   return true;
184 }
185
186 /* Determine the machine architecture and type.  This is called from
187    the generic COFF routines.  It is the inverse of ecoff_get_magic,
188    below.  This could be an ECOFF backend routine, with one version
189    for each target, but there aren't all that many ECOFF targets.  */
190
191 boolean
192 ecoff_set_arch_mach_hook (abfd, filehdr)
193      bfd *abfd;
194      PTR filehdr;
195 {
196   struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
197   enum bfd_architecture arch;
198   unsigned long mach;
199
200   switch (internal_f->f_magic)
201     {
202     case MIPS_MAGIC_1:
203     case MIPS_MAGIC_LITTLE:
204     case MIPS_MAGIC_BIG:
205       arch = bfd_arch_mips;
206       mach = 3000;
207       break;
208
209     case MIPS_MAGIC_LITTLE2:
210     case MIPS_MAGIC_BIG2:
211       /* MIPS ISA level 2: the r6000 */
212       arch = bfd_arch_mips;
213       mach = 6000;
214       break;
215
216     case MIPS_MAGIC_LITTLE3:
217     case MIPS_MAGIC_BIG3:
218       /* MIPS ISA level 3: the r4000 */
219       arch = bfd_arch_mips;
220       mach = 4000;
221       break;
222
223     case ALPHA_MAGIC:
224       arch = bfd_arch_alpha;
225       mach = 0;
226       break;
227
228     default:
229       arch = bfd_arch_obscure;
230       mach = 0;
231       break;
232     }
233
234   return bfd_default_set_arch_mach (abfd, arch, mach);
235 }
236
237 /* Get the magic number to use based on the architecture and machine.
238    This is the inverse of ecoff_set_arch_mach_hook, above.  */
239
240 static int
241 ecoff_get_magic (abfd)
242      bfd *abfd;
243 {
244   int big, little;
245
246   switch (bfd_get_arch (abfd))
247     {
248     case bfd_arch_mips:
249       switch (bfd_get_mach (abfd))
250         {
251         default:
252         case 0:
253         case 3000:
254           big = MIPS_MAGIC_BIG;
255           little = MIPS_MAGIC_LITTLE;
256           break;
257
258         case 6000:
259           big = MIPS_MAGIC_BIG2;
260           little = MIPS_MAGIC_LITTLE2;
261           break;
262
263         case 4000:
264           big = MIPS_MAGIC_BIG3;
265           little = MIPS_MAGIC_LITTLE3;
266           break;
267         }
268
269       return abfd->xvec->byteorder_big_p ? big : little;
270
271     case bfd_arch_alpha:
272       return ALPHA_MAGIC;
273
274     default:
275       abort ();
276       return 0;
277     }
278 }
279
280 /* Get the section s_flags to use for a section.  */
281
282 long
283 ecoff_sec_to_styp_flags (name, flags)
284      CONST char *name;
285      flagword flags;
286 {
287   long styp;
288
289   styp = 0;
290
291   if (strcmp (name, _TEXT) == 0)
292     styp = STYP_TEXT;
293   else if (strcmp (name, _DATA) == 0)
294     styp = STYP_DATA;
295   else if (strcmp (name, _SDATA) == 0)
296     styp = STYP_SDATA;
297   else if (strcmp (name, _RDATA) == 0)
298     styp = STYP_RDATA;
299   else if (strcmp (name, _LITA) == 0)
300     styp = STYP_LITA;
301   else if (strcmp (name, _LIT8) == 0)
302     styp = STYP_LIT8;
303   else if (strcmp (name, _LIT4) == 0)
304     styp = STYP_LIT4;
305   else if (strcmp (name, _BSS) == 0)
306     styp = STYP_BSS;
307   else if (strcmp (name, _SBSS) == 0)
308     styp = STYP_SBSS;
309   else if (strcmp (name, _INIT) == 0)
310     styp = STYP_ECOFF_INIT;
311   else if (strcmp (name, _FINI) == 0)
312     styp = STYP_ECOFF_FINI;
313   else if (flags & SEC_CODE) 
314     styp = STYP_TEXT;
315   else if (flags & SEC_DATA) 
316     styp = STYP_DATA;
317   else if (flags & SEC_READONLY)
318     styp = STYP_RDATA;
319   else if (flags & SEC_LOAD)
320     styp = STYP_REG;
321   else
322     styp = STYP_BSS;
323
324   if (flags & SEC_NEVER_LOAD)
325     styp |= STYP_NOLOAD;
326
327   return styp;
328 }
329
330 /* Get the BFD flags to use for a section.  */
331
332 /*ARGSUSED*/
333 flagword
334 ecoff_styp_to_sec_flags (abfd, hdr)
335      bfd *abfd;
336      PTR hdr;
337 {
338   struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
339   long styp_flags = internal_s->s_flags;
340   flagword sec_flags=0;
341
342   if (styp_flags & STYP_NOLOAD)
343     sec_flags |= SEC_NEVER_LOAD;
344
345   /* For 386 COFF, at least, an unloadable text or data section is
346      actually a shared library section.  */
347   if ((styp_flags & STYP_TEXT)
348       || (styp_flags & STYP_ECOFF_INIT)
349       || (styp_flags & STYP_ECOFF_FINI))
350     {
351       if (sec_flags & SEC_NEVER_LOAD)
352         sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
353       else
354         sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
355     }
356   else if ((styp_flags & STYP_DATA)
357            || (styp_flags & STYP_RDATA)
358            || (styp_flags & STYP_SDATA))
359     {
360       if (sec_flags & SEC_NEVER_LOAD)
361         sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
362       else
363         sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
364       if (styp_flags & STYP_RDATA)
365         sec_flags |= SEC_READONLY;
366     }
367   else if ((styp_flags & STYP_BSS)
368            || (styp_flags & STYP_SBSS))
369     {
370       sec_flags |= SEC_ALLOC;
371     }
372   else if (styp_flags & STYP_INFO) 
373     {
374       sec_flags |= SEC_NEVER_LOAD;
375     }
376   else if ((styp_flags & STYP_LITA)
377            || (styp_flags & STYP_LIT8)
378            || (styp_flags & STYP_LIT4))
379     {
380       sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
381     }
382   else
383     {
384       sec_flags |= SEC_ALLOC | SEC_LOAD;
385     }
386
387   return sec_flags;
388 }
389 \f
390 /* Routines to swap auxiliary information in and out.  I am assuming
391    that the auxiliary information format is always going to be target
392    independent.  */
393
394 /* Swap in a type information record.
395    BIGEND says whether AUX symbols are big-endian or little-endian; this
396    info comes from the file header record (fh-fBigendian).  */
397
398 void
399 ecoff_swap_tir_in (bigend, ext_copy, intern)
400      int bigend;
401      struct tir_ext *ext_copy;
402      TIR *intern;
403 {
404   struct tir_ext ext[1];
405
406   *ext = *ext_copy;             /* Make it reasonable to do in-place.  */
407   
408   /* now the fun stuff... */
409   if (bigend) {
410     intern->fBitfield   = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_BIG);
411     intern->continued   = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_BIG);
412     intern->bt          = (ext->t_bits1[0] & TIR_BITS1_BT_BIG)
413                         >>                  TIR_BITS1_BT_SH_BIG;
414     intern->tq4         = (ext->t_tq45[0] & TIR_BITS_TQ4_BIG)
415                         >>                  TIR_BITS_TQ4_SH_BIG;
416     intern->tq5         = (ext->t_tq45[0] & TIR_BITS_TQ5_BIG)
417                         >>                  TIR_BITS_TQ5_SH_BIG;
418     intern->tq0         = (ext->t_tq01[0] & TIR_BITS_TQ0_BIG)
419                         >>                  TIR_BITS_TQ0_SH_BIG;
420     intern->tq1         = (ext->t_tq01[0] & TIR_BITS_TQ1_BIG)
421                         >>                  TIR_BITS_TQ1_SH_BIG;
422     intern->tq2         = (ext->t_tq23[0] & TIR_BITS_TQ2_BIG)
423                         >>                  TIR_BITS_TQ2_SH_BIG;
424     intern->tq3         = (ext->t_tq23[0] & TIR_BITS_TQ3_BIG)
425                         >>                  TIR_BITS_TQ3_SH_BIG;
426   } else {
427     intern->fBitfield   = 0 != (ext->t_bits1[0] & TIR_BITS1_FBITFIELD_LITTLE);
428     intern->continued   = 0 != (ext->t_bits1[0] & TIR_BITS1_CONTINUED_LITTLE);
429     intern->bt          = (ext->t_bits1[0] & TIR_BITS1_BT_LITTLE)
430                         >>                  TIR_BITS1_BT_SH_LITTLE;
431     intern->tq4         = (ext->t_tq45[0] & TIR_BITS_TQ4_LITTLE)
432                         >>                  TIR_BITS_TQ4_SH_LITTLE;
433     intern->tq5         = (ext->t_tq45[0] & TIR_BITS_TQ5_LITTLE)
434                         >>                  TIR_BITS_TQ5_SH_LITTLE;
435     intern->tq0         = (ext->t_tq01[0] & TIR_BITS_TQ0_LITTLE)
436                         >>                  TIR_BITS_TQ0_SH_LITTLE;
437     intern->tq1         = (ext->t_tq01[0] & TIR_BITS_TQ1_LITTLE)
438                         >>                  TIR_BITS_TQ1_SH_LITTLE;
439     intern->tq2         = (ext->t_tq23[0] & TIR_BITS_TQ2_LITTLE)
440                         >>                  TIR_BITS_TQ2_SH_LITTLE;
441     intern->tq3         = (ext->t_tq23[0] & TIR_BITS_TQ3_LITTLE)
442                         >>                  TIR_BITS_TQ3_SH_LITTLE;
443   }
444
445 #ifdef TEST
446   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
447     abort();
448 #endif
449 }
450
451 /* Swap out a type information record.
452    BIGEND says whether AUX symbols are big-endian or little-endian; this
453    info comes from the file header record (fh-fBigendian).  */
454
455 void
456 ecoff_swap_tir_out (bigend, intern_copy, ext)
457      int bigend;
458      TIR *intern_copy;
459      struct tir_ext *ext;
460 {
461   TIR intern[1];
462
463   *intern = *intern_copy;       /* Make it reasonable to do in-place.  */
464   
465   /* now the fun stuff... */
466   if (bigend) {
467     ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_BIG : 0)
468                        | (intern->continued ? TIR_BITS1_CONTINUED_BIG : 0)
469                        | ((intern->bt << TIR_BITS1_BT_SH_BIG)
470                           & TIR_BITS1_BT_BIG));
471     ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_BIG)
472                        & TIR_BITS_TQ4_BIG)
473                       | ((intern->tq5 << TIR_BITS_TQ5_SH_BIG)
474                          & TIR_BITS_TQ5_BIG));
475     ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_BIG)
476                        & TIR_BITS_TQ0_BIG)
477                       | ((intern->tq1 << TIR_BITS_TQ1_SH_BIG)
478                          & TIR_BITS_TQ1_BIG));
479     ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_BIG)
480                        & TIR_BITS_TQ2_BIG)
481                       | ((intern->tq3 << TIR_BITS_TQ3_SH_BIG)
482                          & TIR_BITS_TQ3_BIG));
483   } else {
484     ext->t_bits1[0] = ((intern->fBitfield ? TIR_BITS1_FBITFIELD_LITTLE : 0)
485                        | (intern->continued ? TIR_BITS1_CONTINUED_LITTLE : 0)
486                        | ((intern->bt << TIR_BITS1_BT_SH_LITTLE)
487                           & TIR_BITS1_BT_LITTLE));
488     ext->t_tq45[0] = (((intern->tq4 << TIR_BITS_TQ4_SH_LITTLE)
489                        & TIR_BITS_TQ4_LITTLE)
490                       | ((intern->tq5 << TIR_BITS_TQ5_SH_LITTLE)
491                          & TIR_BITS_TQ5_LITTLE));
492     ext->t_tq01[0] = (((intern->tq0 << TIR_BITS_TQ0_SH_LITTLE)
493                        & TIR_BITS_TQ0_LITTLE)
494                       | ((intern->tq1 << TIR_BITS_TQ1_SH_LITTLE)
495                          & TIR_BITS_TQ1_LITTLE));
496     ext->t_tq23[0] = (((intern->tq2 << TIR_BITS_TQ2_SH_LITTLE)
497                        & TIR_BITS_TQ2_LITTLE)
498                       | ((intern->tq3 << TIR_BITS_TQ3_SH_LITTLE)
499                          & TIR_BITS_TQ3_LITTLE));
500   }
501
502 #ifdef TEST
503   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
504     abort();
505 #endif
506 }
507
508 /* Swap in a relative symbol record.  BIGEND says whether it is in
509    big-endian or little-endian format.*/
510
511 void
512 ecoff_swap_rndx_in (bigend, ext_copy, intern)
513      int bigend;
514      struct rndx_ext *ext_copy;
515      RNDXR *intern;
516 {
517   struct rndx_ext ext[1];
518
519   *ext = *ext_copy;             /* Make it reasonable to do in-place.  */
520   
521   /* now the fun stuff... */
522   if (bigend) {
523     intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_BIG)
524                   | ((ext->r_bits[1] & RNDX_BITS1_RFD_BIG)
525                                     >> RNDX_BITS1_RFD_SH_BIG);
526     intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_BIG)
527                                     << RNDX_BITS1_INDEX_SH_LEFT_BIG)
528                   | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_BIG)
529                   | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_BIG);
530   } else {
531     intern->rfd   = (ext->r_bits[0] << RNDX_BITS0_RFD_SH_LEFT_LITTLE)
532                   | ((ext->r_bits[1] & RNDX_BITS1_RFD_LITTLE)
533                                     << RNDX_BITS1_RFD_SH_LEFT_LITTLE);
534     intern->index = ((ext->r_bits[1] & RNDX_BITS1_INDEX_LITTLE)
535                                     >> RNDX_BITS1_INDEX_SH_LITTLE)
536                   | (ext->r_bits[2] << RNDX_BITS2_INDEX_SH_LEFT_LITTLE)
537                   | (ext->r_bits[3] << RNDX_BITS3_INDEX_SH_LEFT_LITTLE);
538   }
539
540 #ifdef TEST
541   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
542     abort();
543 #endif
544 }
545
546 /* Swap out a relative symbol record.  BIGEND says whether it is in
547    big-endian or little-endian format.*/
548
549 void
550 ecoff_swap_rndx_out (bigend, intern_copy, ext)
551      int bigend;
552      RNDXR *intern_copy;
553      struct rndx_ext *ext;
554 {
555   RNDXR intern[1];
556
557   *intern = *intern_copy;       /* Make it reasonable to do in-place.  */
558   
559   /* now the fun stuff... */
560   if (bigend) {
561     ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_BIG;
562     ext->r_bits[1] = (((intern->rfd << RNDX_BITS1_RFD_SH_BIG)
563                        & RNDX_BITS1_RFD_BIG)
564                       | ((intern->index >> RNDX_BITS1_INDEX_SH_LEFT_BIG)
565                          & RNDX_BITS1_INDEX_BIG));
566     ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_BIG;
567     ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_BIG;
568   } else {
569     ext->r_bits[0] = intern->rfd >> RNDX_BITS0_RFD_SH_LEFT_LITTLE;
570     ext->r_bits[1] = (((intern->rfd >> RNDX_BITS1_RFD_SH_LEFT_LITTLE)
571                        & RNDX_BITS1_RFD_LITTLE)
572                       | ((intern->index << RNDX_BITS1_INDEX_SH_LITTLE)
573                          & RNDX_BITS1_INDEX_LITTLE));
574     ext->r_bits[2] = intern->index >> RNDX_BITS2_INDEX_SH_LEFT_LITTLE;
575     ext->r_bits[3] = intern->index >> RNDX_BITS3_INDEX_SH_LEFT_LITTLE;
576   }
577
578 #ifdef TEST
579   if (memcmp ((char *)ext, (char *)intern, sizeof (*intern)) != 0)
580     abort();
581 #endif
582 }
583 \f
584 /* Read in and swap the important symbolic information for an ECOFF
585    object file.  This is called by gdb.  */
586
587 boolean
588 ecoff_slurp_symbolic_info (abfd)
589      bfd *abfd;
590 {
591   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
592   bfd_size_type external_hdr_size;
593   HDRR *internal_symhdr;
594   bfd_size_type raw_base;
595   bfd_size_type raw_size;
596   PTR raw;
597   bfd_size_type external_fdr_size;
598   char *fraw_src;
599   char *fraw_end;
600   struct fdr *fdr_ptr;
601   bfd_size_type raw_end;
602   bfd_size_type cb_end;
603
604   /* Check whether we've already gotten it, and whether there's any to
605      get.  */
606   if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
607     return true;
608   if (ecoff_data (abfd)->sym_filepos == 0)
609     {
610       bfd_get_symcount (abfd) = 0;
611       return true;
612     }
613
614   /* At this point bfd_get_symcount (abfd) holds the number of symbols
615      as read from the file header, but on ECOFF this is always the
616      size of the symbolic information header.  It would be cleaner to
617      handle this when we first read the file in coffgen.c.  */
618   external_hdr_size = backend->debug_swap.external_hdr_size;
619   if (bfd_get_symcount (abfd) != external_hdr_size)
620     {
621       bfd_error = bad_value;
622       return false;
623     }
624
625   /* Read the symbolic information header.  */
626   raw = (PTR) alloca (external_hdr_size);
627   if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
628       || (bfd_read (raw, external_hdr_size, 1, abfd)
629           != external_hdr_size))
630     {
631       bfd_error = system_call_error;
632       return false;
633     }
634   internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
635   (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
636
637   if (internal_symhdr->magic != backend->debug_swap.sym_magic)
638     {
639       bfd_error = bad_value;
640       return false;
641     }
642
643   /* Now we can get the correct number of symbols.  */
644   bfd_get_symcount (abfd) = (internal_symhdr->isymMax
645                              + internal_symhdr->iextMax);
646
647   /* Read all the symbolic information at once.  */
648   raw_base = ecoff_data (abfd)->sym_filepos + external_hdr_size;
649
650   /* Alpha ecoff makes the determination of raw_size difficult. It has
651      an undocumented debug data section between the symhdr and the first
652      documented section. And the ordering of the sections varies between
653      statically and dynamically linked executables.
654      If bfd supports SEEK_END someday, this code could be simplified.  */
655
656   raw_end = 0;
657
658 #define UPDATE_RAW_END(start, count, size) \
659   cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
660   if (cb_end > raw_end) \
661     raw_end = cb_end
662
663   UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
664   UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
665   UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
666   UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
667   UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
668   UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
669   UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
670   UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
671   UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
672   UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
673   UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
674
675 #undef UPDATE_RAW_END
676
677   raw_size = raw_end - raw_base;
678   if (raw_size == 0)
679     {
680       ecoff_data (abfd)->sym_filepos = 0;
681       return true;
682     }
683   raw = (PTR) bfd_alloc (abfd, raw_size);
684   if (raw == NULL)
685     {
686       bfd_error = no_memory;
687       return false;
688     }
689   if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
690     {
691       bfd_error = system_call_error;
692       bfd_release (abfd, raw);
693       return false;
694     }
695
696   ecoff_data (abfd)->raw_syments = raw;
697
698   /* Get pointers for the numeric offsets in the HDRR structure.  */
699 #define FIX(off1, off2, type) \
700   if (internal_symhdr->off1 == 0) \
701     ecoff_data (abfd)->debug_info.off2 = (type) NULL; \
702   else \
703     ecoff_data (abfd)->debug_info.off2 = (type) ((char *) raw \
704                                                  + internal_symhdr->off1 \
705                                                  - raw_base)
706   FIX (cbLineOffset, line, unsigned char *);
707   FIX (cbDnOffset, external_dnr, PTR);
708   FIX (cbPdOffset, external_pdr, PTR);
709   FIX (cbSymOffset, external_sym, PTR);
710   FIX (cbOptOffset, external_opt, PTR);
711   FIX (cbAuxOffset, external_aux, union aux_ext *);
712   FIX (cbSsOffset, ss, char *);
713   FIX (cbSsExtOffset, ssext, char *);
714   FIX (cbFdOffset, external_fdr, PTR);
715   FIX (cbRfdOffset, external_rfd, PTR);
716   FIX (cbExtOffset, external_ext, PTR);
717 #undef FIX
718
719   /* I don't want to always swap all the data, because it will just
720      waste time and most programs will never look at it.  The only
721      time the linker needs most of the debugging information swapped
722      is when linking big-endian and little-endian MIPS object files
723      together, which is not a common occurrence.
724
725      We need to look at the fdr to deal with a lot of information in
726      the symbols, so we swap them here.  */
727   ecoff_data (abfd)->debug_info.fdr =
728     (struct fdr *) bfd_alloc (abfd,
729                               (internal_symhdr->ifdMax *
730                                sizeof (struct fdr)));
731   if (ecoff_data (abfd)->debug_info.fdr == NULL)
732     {
733       bfd_error = no_memory;
734       return false;
735     }
736   external_fdr_size = backend->debug_swap.external_fdr_size;
737   fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
738   fraw_src = (char *) ecoff_data (abfd)->debug_info.external_fdr;
739   fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
740   for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
741     (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
742
743   return true;
744 }
745 \f
746 /* ECOFF symbol table routines.  The ECOFF symbol table is described
747    in gcc/mips-tfile.c.  */
748
749 /* ECOFF uses two common sections.  One is the usual one, and the
750    other is for small objects.  All the small objects are kept
751    together, and then referenced via the gp pointer, which yields
752    faster assembler code.  This is what we use for the small common
753    section.  */
754 static asection ecoff_scom_section;
755 static asymbol ecoff_scom_symbol;
756 static asymbol *ecoff_scom_symbol_ptr;
757
758 /* Create an empty symbol.  */
759
760 asymbol *
761 ecoff_make_empty_symbol (abfd)
762      bfd *abfd;
763 {
764   ecoff_symbol_type *new;
765
766   new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
767   if (new == (ecoff_symbol_type *) NULL)
768     {
769       bfd_error = no_memory;
770       return (asymbol *) NULL;
771     }
772   memset (new, 0, sizeof *new);
773   new->symbol.section = (asection *) NULL;
774   new->fdr = (FDR *) NULL;
775   new->local = false;
776   new->native = NULL;
777   new->symbol.the_bfd = abfd;
778   return &new->symbol;
779 }
780
781 /* Set the BFD flags and section for an ECOFF symbol.  */
782
783 static void
784 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
785      bfd *abfd;
786      SYMR *ecoff_sym;
787      asymbol *asym;
788      int ext;
789      asymbol **indirect_ptr_ptr;
790 {
791   asym->the_bfd = abfd;
792   asym->value = ecoff_sym->value;
793   asym->section = &bfd_debug_section;
794   asym->udata = NULL;
795
796   /* An indirect symbol requires two consecutive stabs symbols.  */
797   if (*indirect_ptr_ptr != (asymbol *) NULL)
798     {
799       BFD_ASSERT (ECOFF_IS_STAB (ecoff_sym));
800
801       /* @@ Stuffing pointers into integers is a no-no.
802          We can usually get away with it if the integer is
803          large enough though.  */
804       if (sizeof (asym) > sizeof (bfd_vma))
805         abort ();
806       (*indirect_ptr_ptr)->value = (bfd_vma) asym;
807
808       asym->flags = BSF_DEBUGGING;
809       asym->section = &bfd_und_section;
810       *indirect_ptr_ptr = NULL;
811       return;
812     }
813
814   if (ECOFF_IS_STAB (ecoff_sym)
815       && (ECOFF_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
816     {
817       asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
818       asym->section = &bfd_ind_section;
819       /* Pass this symbol on to the next call to this function.  */
820       *indirect_ptr_ptr = asym;
821       return;
822     }
823
824   /* Most symbol types are just for debugging.  */
825   switch (ecoff_sym->st)
826     {
827     case stGlobal:
828     case stStatic:
829     case stLabel:
830     case stProc:
831     case stStaticProc:
832       break;
833     case stNil:
834       if (ECOFF_IS_STAB (ecoff_sym))
835         {
836           asym->flags = BSF_DEBUGGING;
837           return;
838         }
839       break;
840     default:
841       asym->flags = BSF_DEBUGGING;
842       return;
843     }
844
845   if (ext)
846     asym->flags = BSF_EXPORT | BSF_GLOBAL;
847   else
848     asym->flags = BSF_LOCAL;
849   switch (ecoff_sym->sc)
850     {
851     case scNil:
852       /* Used for compiler generated labels.  Leave them in the
853          debugging section, and mark them as local.  If BSF_DEBUGGING
854          is set, then nm does not display them for some reason.  If no
855          flags are set then the linker whines about them.  */
856       asym->flags = BSF_LOCAL;
857       break;
858     case scText:
859       asym->section = bfd_make_section_old_way (abfd, ".text");
860       asym->value -= asym->section->vma;
861       break;
862     case scData:
863       asym->section = bfd_make_section_old_way (abfd, ".data");
864       asym->value -= asym->section->vma;
865       break;
866     case scBss:
867       asym->section = bfd_make_section_old_way (abfd, ".bss");
868       asym->value -= asym->section->vma;
869       break;
870     case scRegister:
871       asym->flags = BSF_DEBUGGING;
872       break;
873     case scAbs:
874       asym->section = &bfd_abs_section;
875       break;
876     case scUndefined:
877       asym->section = &bfd_und_section;
878       asym->flags = 0;
879       asym->value = 0;
880       break;
881     case scCdbLocal:
882     case scBits:
883     case scCdbSystem:
884     case scRegImage:
885     case scInfo:
886     case scUserStruct:
887       asym->flags = BSF_DEBUGGING;
888       break;
889     case scSData:
890       asym->section = bfd_make_section_old_way (abfd, ".sdata");
891       asym->value -= asym->section->vma;
892       break;
893     case scSBss:
894       asym->section = bfd_make_section_old_way (abfd, ".sbss");
895       asym->value -= asym->section->vma;
896       break;
897     case scRData:
898       asym->section = bfd_make_section_old_way (abfd, ".rdata");
899       asym->value -= asym->section->vma;
900       break;
901     case scVar:
902       asym->flags = BSF_DEBUGGING;
903       break;
904     case scCommon:
905       if (asym->value > ecoff_data (abfd)->gp_size)
906         {
907           asym->section = &bfd_com_section;
908           asym->flags = 0;
909           break;
910         }
911       /* Fall through.  */
912     case scSCommon:
913       if (ecoff_scom_section.name == NULL)
914         {
915           /* Initialize the small common section.  */
916           ecoff_scom_section.name = SCOMMON;
917           ecoff_scom_section.flags = SEC_IS_COMMON;
918           ecoff_scom_section.output_section = &ecoff_scom_section;
919           ecoff_scom_section.symbol = &ecoff_scom_symbol;
920           ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
921           ecoff_scom_symbol.name = SCOMMON;
922           ecoff_scom_symbol.flags = BSF_SECTION_SYM;
923           ecoff_scom_symbol.section = &ecoff_scom_section;
924           ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
925         }
926       asym->section = &ecoff_scom_section;
927       asym->flags = 0;
928       break;
929     case scVarRegister:
930     case scVariant:
931       asym->flags = BSF_DEBUGGING;
932       break;
933     case scSUndefined:
934       asym->section = &bfd_und_section;
935       asym->flags = 0;
936       asym->value = 0;
937       break;
938     case scInit:
939       asym->section = bfd_make_section_old_way (abfd, ".init");
940       asym->value -= asym->section->vma;
941       break;
942     case scBasedVar:
943     case scXData:
944     case scPData:
945       asym->flags = BSF_DEBUGGING;
946       break;
947     case scFini:
948       asym->section = bfd_make_section_old_way (abfd, ".fini");
949       asym->value -= asym->section->vma;
950       break;
951     default:
952       break;
953     }
954
955   /* Look for special constructors symbols and make relocation entries
956      in a special construction section.  These are produced by the
957      -fgnu-linker argument to g++.  */
958   if (ECOFF_IS_STAB (ecoff_sym))
959     {
960       switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
961         {
962         default:
963           break;
964
965         case N_SETA:
966         case N_SETT:
967         case N_SETD:
968         case N_SETB:
969           {
970             const char *name;
971             asection *section;
972             arelent_chain *reloc_chain;
973             unsigned int bitsize;
974
975             /* Get a section with the same name as the symbol (usually
976                __CTOR_LIST__ or __DTOR_LIST__).  FIXME: gcc uses the
977                name ___CTOR_LIST (three underscores).  We need
978                __CTOR_LIST (two underscores), since ECOFF doesn't use
979                a leading underscore.  This should be handled by gcc,
980                but instead we do it here.  Actually, this should all
981                be done differently anyhow.  */
982             name = bfd_asymbol_name (asym);
983             if (name[0] == '_' && name[1] == '_' && name[2] == '_')
984               {
985                 ++name;
986                 asym->name = name;
987               }
988             section = bfd_get_section_by_name (abfd, name);
989             if (section == (asection *) NULL)
990               {
991                 char *copy;
992
993                 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
994                 strcpy (copy, name);
995                 section = bfd_make_section (abfd, copy);
996               }
997
998             /* Build a reloc pointing to this constructor.  */
999             reloc_chain =
1000               (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
1001             reloc_chain->relent.sym_ptr_ptr =
1002               bfd_get_section (asym)->symbol_ptr_ptr;
1003             reloc_chain->relent.address = section->_raw_size;
1004             reloc_chain->relent.addend = asym->value;
1005             reloc_chain->relent.howto =
1006               ecoff_backend (abfd)->constructor_reloc;
1007
1008             /* Set up the constructor section to hold the reloc.  */
1009             section->flags = SEC_CONSTRUCTOR;
1010             ++section->reloc_count;
1011
1012             /* Constructor sections must be rounded to a boundary
1013                based on the bitsize.  These are not real sections--
1014                they are handled specially by the linker--so the ECOFF
1015                16 byte alignment restriction does not apply.  */
1016             bitsize = ecoff_backend (abfd)->constructor_bitsize;
1017             section->alignment_power = 1;
1018             while ((1 << section->alignment_power) < bitsize / 8)
1019               ++section->alignment_power;
1020
1021             reloc_chain->next = section->constructor_chain;
1022             section->constructor_chain = reloc_chain;
1023             section->_raw_size += bitsize / 8;
1024
1025             /* Mark the symbol as a constructor.  */
1026             asym->flags |= BSF_CONSTRUCTOR;
1027           }
1028           break;
1029         }
1030     }
1031 }
1032
1033 /* Read an ECOFF symbol table.  */
1034
1035 boolean
1036 ecoff_slurp_symbol_table (abfd)
1037      bfd *abfd;
1038 {
1039   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1040   const bfd_size_type external_ext_size
1041     = backend->debug_swap.external_ext_size;
1042   const bfd_size_type external_sym_size
1043     = backend->debug_swap.external_sym_size;
1044   void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
1045     = backend->debug_swap.swap_ext_in;
1046   void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
1047     = backend->debug_swap.swap_sym_in;
1048   bfd_size_type internal_size;
1049   ecoff_symbol_type *internal;
1050   ecoff_symbol_type *internal_ptr;
1051   asymbol *indirect_ptr;
1052   char *eraw_src;
1053   char *eraw_end;
1054   FDR *fdr_ptr;
1055   FDR *fdr_end;
1056
1057   /* If we've already read in the symbol table, do nothing.  */
1058   if (ecoff_data (abfd)->canonical_symbols != NULL)
1059     return true;
1060
1061   /* Get the symbolic information.  */
1062   if (ecoff_slurp_symbolic_info (abfd) == false)
1063     return false;
1064   if (bfd_get_symcount (abfd) == 0)
1065     return true;
1066
1067   internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1068   internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1069   if (internal == NULL)
1070     {
1071       bfd_error = no_memory;
1072       return false;
1073     }
1074
1075   internal_ptr = internal;
1076   indirect_ptr = NULL;
1077   eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
1078   eraw_end = (eraw_src
1079               + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
1080                  * external_ext_size));
1081   for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
1082     {
1083       EXTR internal_esym;
1084
1085       (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
1086       internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
1087                                    + internal_esym.asym.iss);
1088       ecoff_set_symbol_info (abfd, &internal_esym.asym,
1089                              &internal_ptr->symbol, 1, &indirect_ptr);
1090       /* The alpha uses a negative ifd field for section symbols.  */
1091       if (internal_esym.ifd >= 0)
1092         internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
1093                              + internal_esym.ifd);
1094       else
1095         internal_ptr->fdr = NULL;
1096       internal_ptr->local = false;
1097       internal_ptr->native = (PTR) eraw_src;
1098     }
1099   BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1100
1101   /* The local symbols must be accessed via the fdr's, because the
1102      string and aux indices are relative to the fdr information.  */
1103   fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
1104   fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1105   for (; fdr_ptr < fdr_end; fdr_ptr++)
1106     {
1107       char *lraw_src;
1108       char *lraw_end;
1109
1110       lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
1111                   + fdr_ptr->isymBase * external_sym_size);
1112       lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
1113       for (;
1114            lraw_src < lraw_end;
1115            lraw_src += external_sym_size, internal_ptr++)
1116         {
1117           SYMR internal_sym;
1118
1119           (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
1120           internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
1121                                        + fdr_ptr->issBase
1122                                        + internal_sym.iss);
1123           ecoff_set_symbol_info (abfd, &internal_sym,
1124                                  &internal_ptr->symbol, 0, &indirect_ptr);
1125           internal_ptr->fdr = fdr_ptr;
1126           internal_ptr->local = true;
1127           internal_ptr->native = (PTR) lraw_src;
1128         }
1129     }
1130   BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1131
1132   ecoff_data (abfd)->canonical_symbols = internal;
1133
1134   return true;
1135 }
1136
1137 /* Return the amount of space needed for the canonical symbols.  */
1138
1139 unsigned int
1140 ecoff_get_symtab_upper_bound (abfd)
1141      bfd *abfd;
1142 {
1143   if (ecoff_slurp_symbolic_info (abfd) == false
1144       || bfd_get_symcount (abfd) == 0)
1145     return 0;
1146
1147   return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1148 }
1149
1150 /* Get the canonical symbols.  */
1151
1152 unsigned int
1153 ecoff_get_symtab (abfd, alocation)
1154      bfd *abfd;
1155      asymbol **alocation;
1156 {
1157   unsigned int counter = 0;
1158   ecoff_symbol_type *symbase;
1159   ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1160
1161   if (ecoff_slurp_symbol_table (abfd) == false
1162       || bfd_get_symcount (abfd) == 0)
1163     return 0;
1164
1165   symbase = ecoff_data (abfd)->canonical_symbols;
1166   while (counter < bfd_get_symcount (abfd))
1167     {
1168       *(location++) = symbase++;
1169       counter++;
1170     }
1171   *location++ = (ecoff_symbol_type *) NULL;
1172   return bfd_get_symcount (abfd);
1173 }
1174
1175 /* Turn ECOFF type information into a printable string.
1176    ecoff_emit_aggregate and ecoff_type_to_string are from
1177    gcc/mips-tdump.c, with swapping added and used_ptr removed.  */
1178
1179 /* Write aggregate information to a string.  */
1180
1181 static void
1182 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1183      bfd *abfd;
1184      char *string;
1185      RNDXR *rndx;
1186      long isym;
1187      CONST char *which;
1188 {
1189   int ifd = rndx->rfd;
1190   int indx = rndx->index;
1191   int sym_base, ss_base;
1192   CONST char *name;
1193   
1194   if (ifd == 0xfff)
1195     ifd = isym;
1196
1197   sym_base = ecoff_data (abfd)->debug_info.fdr[ifd].isymBase;
1198   ss_base  = ecoff_data (abfd)->debug_info.fdr[ifd].issBase;
1199   
1200   if (indx == indexNil)
1201     name = "/* no name */";
1202   else
1203     {
1204       const struct ecoff_debug_swap * const debug_swap
1205         = &ecoff_backend (abfd)->debug_swap;
1206       SYMR sym;
1207
1208       indx += sym_base;
1209       (*debug_swap->swap_sym_in)
1210         (abfd,
1211          ((char *) ecoff_data (abfd)->debug_info.external_sym
1212           + indx * debug_swap->external_sym_size),
1213          &sym);
1214       name = ecoff_data (abfd)->debug_info.ss + ss_base + sym.iss;
1215     }
1216
1217   sprintf (string,
1218            "%s %s { ifd = %d, index = %ld }",
1219            which, name, ifd,
1220            ((long) indx
1221             + ecoff_data (abfd)->debug_info.symbolic_header.iextMax));
1222 }
1223
1224 /* Convert the type information to string format.  */
1225
1226 static char *
1227 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1228      bfd *abfd;
1229      union aux_ext *aux_ptr;
1230      unsigned int indx;
1231      int bigendian;
1232 {
1233   AUXU u;
1234   struct qual {
1235     unsigned int  type;
1236     int  low_bound;
1237     int  high_bound;
1238     int  stride;
1239   } qualifiers[7];
1240
1241   unsigned int basic_type;
1242   int i;
1243   static char buffer1[1024];
1244   static char buffer2[1024];
1245   char *p1 = buffer1;
1246   char *p2 = buffer2;
1247   RNDXR rndx;
1248
1249   for (i = 0; i < 7; i++)
1250     {
1251       qualifiers[i].low_bound = 0;
1252       qualifiers[i].high_bound = 0;
1253       qualifiers[i].stride = 0;
1254     }
1255
1256   if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1257     return "-1 (no type)";
1258   ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1259
1260   basic_type = u.ti.bt;
1261   qualifiers[0].type = u.ti.tq0;
1262   qualifiers[1].type = u.ti.tq1;
1263   qualifiers[2].type = u.ti.tq2;
1264   qualifiers[3].type = u.ti.tq3;
1265   qualifiers[4].type = u.ti.tq4;
1266   qualifiers[5].type = u.ti.tq5;
1267   qualifiers[6].type = tqNil;
1268
1269   /*
1270    * Go get the basic type.
1271    */
1272   switch (basic_type)
1273     {
1274     case btNil:                 /* undefined */
1275       strcpy (p1, "nil");
1276       break;
1277
1278     case btAdr:                 /* address - integer same size as pointer */
1279       strcpy (p1, "address");
1280       break;
1281
1282     case btChar:                /* character */
1283       strcpy (p1, "char");
1284       break;
1285
1286     case btUChar:               /* unsigned character */
1287       strcpy (p1, "unsigned char");
1288       break;
1289
1290     case btShort:               /* short */
1291       strcpy (p1, "short");
1292       break;
1293
1294     case btUShort:              /* unsigned short */
1295       strcpy (p1, "unsigned short");
1296       break;
1297
1298     case btInt:                 /* int */
1299       strcpy (p1, "int");
1300       break;
1301
1302     case btUInt:                /* unsigned int */
1303       strcpy (p1, "unsigned int");
1304       break;
1305
1306     case btLong:                /* long */
1307       strcpy (p1, "long");
1308       break;
1309
1310     case btULong:               /* unsigned long */
1311       strcpy (p1, "unsigned long");
1312       break;
1313
1314     case btFloat:               /* float (real) */
1315       strcpy (p1, "float");
1316       break;
1317
1318     case btDouble:              /* Double (real) */
1319       strcpy (p1, "double");
1320       break;
1321
1322       /* Structures add 1-2 aux words:
1323          1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1324          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1325
1326     case btStruct:              /* Structure (Record) */
1327       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1328       ecoff_emit_aggregate (abfd, p1, &rndx,
1329                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1330                             "struct");
1331       indx++;                   /* skip aux words */
1332       break;
1333
1334       /* Unions add 1-2 aux words:
1335          1st word is [ST_RFDESCAPE, offset] pointer to union def;
1336          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1337
1338     case btUnion:               /* Union */
1339       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1340       ecoff_emit_aggregate (abfd, p1, &rndx,
1341                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1342                             "union");
1343       indx++;                   /* skip aux words */
1344       break;
1345
1346       /* Enumerations add 1-2 aux words:
1347          1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1348          2nd word is file index if 1st word rfd is ST_RFDESCAPE.  */
1349
1350     case btEnum:                /* Enumeration */
1351       ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1352       ecoff_emit_aggregate (abfd, p1, &rndx,
1353                             (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1354                             "enum");
1355       indx++;                   /* skip aux words */
1356       break;
1357
1358     case btTypedef:             /* defined via a typedef, isymRef points */
1359       strcpy (p1, "typedef");
1360       break;
1361
1362     case btRange:               /* subrange of int */
1363       strcpy (p1, "subrange");
1364       break;
1365
1366     case btSet:                 /* pascal sets */
1367       strcpy (p1, "set");
1368       break;
1369
1370     case btComplex:             /* fortran complex */
1371       strcpy (p1, "complex");
1372       break;
1373
1374     case btDComplex:            /* fortran double complex */
1375       strcpy (p1, "double complex");
1376       break;
1377
1378     case btIndirect:            /* forward or unnamed typedef */
1379       strcpy (p1, "forward/unamed typedef");
1380       break;
1381
1382     case btFixedDec:            /* Fixed Decimal */
1383       strcpy (p1, "fixed decimal");
1384       break;
1385
1386     case btFloatDec:            /* Float Decimal */
1387       strcpy (p1, "float decimal");
1388       break;
1389
1390     case btString:              /* Varying Length Character String */
1391       strcpy (p1, "string");
1392       break;
1393
1394     case btBit:                 /* Aligned Bit String */
1395       strcpy (p1, "bit");
1396       break;
1397
1398     case btPicture:             /* Picture */
1399       strcpy (p1, "picture");
1400       break;
1401
1402     case btVoid:                /* Void */
1403       strcpy (p1, "void");
1404       break;
1405
1406     default:
1407       sprintf (p1, "Unknown basic type %d", (int) basic_type);
1408       break;
1409     }
1410
1411   p1 += strlen (buffer1);
1412
1413   /*
1414    * If this is a bitfield, get the bitsize.
1415    */
1416   if (u.ti.fBitfield)
1417     {
1418       int bitsize;
1419
1420       bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1421       sprintf (p1, " : %d", bitsize);
1422       p1 += strlen (buffer1);
1423     }
1424
1425
1426   /*
1427    * Deal with any qualifiers.
1428    */
1429   if (qualifiers[0].type != tqNil)
1430     {
1431       /*
1432        * Snarf up any array bounds in the correct order.  Arrays
1433        * store 5 successive words in the aux. table:
1434        *        word 0  RNDXR to type of the bounds (ie, int)
1435        *        word 1  Current file descriptor index
1436        *        word 2  low bound
1437        *        word 3  high bound (or -1 if [])
1438        *        word 4  stride size in bits
1439        */
1440       for (i = 0; i < 7; i++)
1441         {
1442           if (qualifiers[i].type == tqArray)
1443             {
1444               qualifiers[i].low_bound =
1445                 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1446               qualifiers[i].high_bound =
1447                 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1448               qualifiers[i].stride =
1449                 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1450               indx += 5;
1451             }
1452         }
1453
1454       /*
1455        * Now print out the qualifiers.
1456        */
1457       for (i = 0; i < 6; i++)
1458         {
1459           switch (qualifiers[i].type)
1460             {
1461             case tqNil:
1462             case tqMax:
1463               break;
1464
1465             case tqPtr:
1466               strcpy (p2, "ptr to ");
1467               p2 += sizeof ("ptr to ")-1;
1468               break;
1469
1470             case tqVol:
1471               strcpy (p2, "volatile ");
1472               p2 += sizeof ("volatile ")-1;
1473               break;
1474
1475             case tqFar:
1476               strcpy (p2, "far ");
1477               p2 += sizeof ("far ")-1;
1478               break;
1479
1480             case tqProc:
1481               strcpy (p2, "func. ret. ");
1482               p2 += sizeof ("func. ret. ");
1483               break;
1484
1485             case tqArray:
1486               {
1487                 int first_array = i;
1488                 int j;
1489
1490                 /* Print array bounds reversed (ie, in the order the C
1491                    programmer writes them).  C is such a fun language.... */
1492
1493                 while (i < 5 && qualifiers[i+1].type == tqArray)
1494                   i++;
1495
1496                 for (j = i; j >= first_array; j--)
1497                   {
1498                     strcpy (p2, "array [");
1499                     p2 += sizeof ("array [")-1;
1500                     if (qualifiers[j].low_bound != 0)
1501                       sprintf (p2,
1502                                "%ld:%ld {%ld bits}",
1503                                (long) qualifiers[j].low_bound,
1504                                (long) qualifiers[j].high_bound,
1505                                (long) qualifiers[j].stride);
1506
1507                     else if (qualifiers[j].high_bound != -1)
1508                       sprintf (p2,
1509                                "%ld {%ld bits}",
1510                                (long) (qualifiers[j].high_bound + 1),
1511                                (long) (qualifiers[j].stride));
1512
1513                     else
1514                       sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1515
1516                     p2 += strlen (p2);
1517                     strcpy (p2, "] of ");
1518                     p2 += sizeof ("] of ")-1;
1519                   }
1520               }
1521               break;
1522             }
1523         }
1524     }
1525
1526   strcpy (p2, buffer1);
1527   return buffer2;
1528 }
1529
1530 /* Return information about ECOFF symbol SYMBOL in RET.  */
1531
1532 /*ARGSUSED*/
1533 void
1534 ecoff_get_symbol_info (abfd, symbol, ret)
1535      bfd *abfd;                 /* Ignored.  */
1536      asymbol *symbol;
1537      symbol_info *ret;
1538 {
1539   bfd_symbol_info (symbol, ret);
1540 }
1541
1542 /* Print information about an ECOFF symbol.  */
1543
1544 void
1545 ecoff_print_symbol (abfd, filep, symbol, how)
1546      bfd *abfd;
1547      PTR filep;
1548      asymbol *symbol;
1549      bfd_print_symbol_type how;
1550 {
1551   const struct ecoff_debug_swap * const debug_swap
1552     = &ecoff_backend (abfd)->debug_swap;
1553   FILE *file = (FILE *)filep;
1554
1555   switch (how)
1556     {
1557     case bfd_print_symbol_name:
1558       fprintf (file, "%s", symbol->name);
1559       break;
1560     case bfd_print_symbol_more:
1561       if (ecoffsymbol (symbol)->local)
1562         {
1563           SYMR ecoff_sym;
1564         
1565           (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1566                                       &ecoff_sym);
1567           fprintf (file, "ecoff local ");
1568           fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1569           fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1570                    (unsigned) ecoff_sym.sc);
1571         }
1572       else
1573         {
1574           EXTR ecoff_ext;
1575
1576           (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1577                                       &ecoff_ext);
1578           fprintf (file, "ecoff extern ");
1579           fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1580           fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1581                    (unsigned) ecoff_ext.asym.sc);
1582         }
1583       break;
1584     case bfd_print_symbol_all:
1585       /* Print out the symbols in a reasonable way */
1586       {
1587         char type;
1588         int pos;
1589         EXTR ecoff_ext;
1590         char jmptbl;
1591         char cobol_main;
1592         char weakext;
1593
1594         if (ecoffsymbol (symbol)->local)
1595           {
1596             (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1597                                         &ecoff_ext.asym);
1598             type = 'l';
1599             pos = ((((char *) ecoffsymbol (symbol)->native
1600                      - (char *) ecoff_data (abfd)->debug_info.external_sym)
1601                     / debug_swap->external_sym_size)
1602                    + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1603             jmptbl = ' ';
1604             cobol_main = ' ';
1605             weakext = ' ';
1606           }
1607         else
1608           {
1609             (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1610                                         &ecoff_ext);
1611             type = 'e';
1612             pos = (((char *) ecoffsymbol (symbol)->native
1613                     - (char *) ecoff_data (abfd)->debug_info.external_ext)
1614                    / debug_swap->external_ext_size);
1615             jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1616             cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1617             weakext = ecoff_ext.weakext ? 'w' : ' ';
1618           }
1619
1620         fprintf (file, "[%3d] %c ",
1621                  pos, type);
1622         fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1623         fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1624                  (unsigned) ecoff_ext.asym.st,
1625                  (unsigned) ecoff_ext.asym.sc,
1626                  (unsigned) ecoff_ext.asym.index,
1627                  jmptbl, cobol_main, weakext,
1628                  symbol->name);
1629
1630         if (ecoffsymbol (symbol)->fdr != NULL
1631             && ecoff_ext.asym.index != indexNil)
1632           {
1633             unsigned int indx;
1634             int bigendian;
1635             bfd_size_type sym_base;
1636             union aux_ext *aux_base;
1637
1638             indx = ecoff_ext.asym.index;
1639
1640             /* sym_base is used to map the fdr relative indices which
1641                appear in the file to the position number which we are
1642                using.  */
1643             sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1644             if (ecoffsymbol (symbol)->local)
1645               sym_base +=
1646                 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1647
1648             /* aux_base is the start of the aux entries for this file;
1649                asym.index is an offset from this.  */
1650             aux_base = (ecoff_data (abfd)->debug_info.external_aux
1651                         + ecoffsymbol (symbol)->fdr->iauxBase);
1652
1653             /* The aux entries are stored in host byte order; the
1654                order is indicated by a bit in the fdr.  */
1655             bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1656
1657             /* This switch is basically from gcc/mips-tdump.c  */
1658             switch (ecoff_ext.asym.st)
1659               {
1660               case stNil:
1661               case stLabel:
1662                 break;
1663
1664               case stFile:
1665               case stBlock:
1666                 fprintf (file, "\n      End+1 symbol: %ld",
1667                          (long) (indx + sym_base));
1668                 break;
1669
1670               case stEnd:
1671                 if (ecoff_ext.asym.sc == scText
1672                     || ecoff_ext.asym.sc == scInfo)
1673                   fprintf (file, "\n      First symbol: %ld",
1674                            (long) (indx + sym_base));
1675                 else
1676                   fprintf (file, "\n      First symbol: %ld", 
1677                            (long) (AUX_GET_ISYM (bigendian,
1678                                                  &aux_base[ecoff_ext.asym.index])
1679                                    + sym_base));
1680                 break;
1681
1682               case stProc:
1683               case stStaticProc:
1684                 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1685                   ;
1686                 else if (ecoffsymbol (symbol)->local)
1687                   fprintf (file, "\n      End+1 symbol: %-7ld   Type:  %s",
1688                            (long) (AUX_GET_ISYM (bigendian,
1689                                                  &aux_base[ecoff_ext.asym.index])
1690                                    + sym_base),
1691                            ecoff_type_to_string (abfd, aux_base, indx + 1,
1692                                                  bigendian));
1693                 else
1694                   fprintf (file, "\n      Local symbol: %ld",
1695                            ((long) indx
1696                             + (long) sym_base
1697                             + (ecoff_data (abfd)
1698                                ->debug_info.symbolic_header.iextMax)));
1699                 break;
1700
1701               default:
1702                 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1703                   fprintf (file, "\n      Type: %s",
1704                            ecoff_type_to_string (abfd, aux_base, indx,
1705                                                  bigendian));
1706                 break;
1707               }
1708           }
1709       }
1710       break;
1711     }
1712 }
1713 \f
1714 /* Read in the relocs for a section.  */
1715
1716 static boolean
1717 ecoff_slurp_reloc_table (abfd, section, symbols)
1718      bfd *abfd;
1719      asection *section;
1720      asymbol **symbols;
1721 {
1722   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1723   arelent *internal_relocs;
1724   bfd_size_type external_reloc_size;
1725   bfd_size_type external_relocs_size;
1726   char *external_relocs;
1727   arelent *rptr;
1728   unsigned int i;
1729
1730   if (section->relocation != (arelent *) NULL
1731       || section->reloc_count == 0
1732       || (section->flags & SEC_CONSTRUCTOR) != 0)
1733     return true;
1734
1735   if (ecoff_slurp_symbol_table (abfd) == false)
1736     return false;
1737   
1738   internal_relocs = (arelent *) bfd_alloc (abfd,
1739                                            (sizeof (arelent)
1740                                             * section->reloc_count));
1741   external_reloc_size = backend->external_reloc_size;
1742   external_relocs_size = external_reloc_size * section->reloc_count;
1743   external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1744   if (internal_relocs == (arelent *) NULL
1745       || external_relocs == (char *) NULL)
1746     {
1747       bfd_error = no_memory;
1748       return false;
1749     }
1750   if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1751     return false;
1752   if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1753       != external_relocs_size)
1754     {
1755       bfd_error = system_call_error;
1756       return false;
1757     }
1758
1759   for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1760     {
1761       struct internal_reloc intern;
1762
1763       (*backend->swap_reloc_in) (abfd,
1764                                  external_relocs + i * external_reloc_size,
1765                                  &intern);
1766
1767       if (intern.r_extern)
1768         {
1769           /* r_symndx is an index into the external symbols.  */
1770           BFD_ASSERT (intern.r_symndx >= 0
1771                       && (intern.r_symndx
1772                           < (ecoff_data (abfd)
1773                              ->debug_info.symbolic_header.iextMax)));
1774           rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1775           rptr->addend = 0;
1776         }
1777       else if (intern.r_symndx == RELOC_SECTION_NONE
1778                || intern.r_symndx == RELOC_SECTION_ABS)
1779         {
1780           rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1781           rptr->addend = 0;
1782         }
1783       else
1784         {
1785           CONST char *sec_name;
1786           asection *sec;
1787
1788           /* r_symndx is a section key.  */
1789           switch (intern.r_symndx)
1790             {
1791             case RELOC_SECTION_TEXT:  sec_name = ".text";  break;
1792             case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1793             case RELOC_SECTION_DATA:  sec_name = ".data";  break;
1794             case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1795             case RELOC_SECTION_SBSS:  sec_name = ".sbss";  break;
1796             case RELOC_SECTION_BSS:   sec_name = ".bss";   break;
1797             case RELOC_SECTION_INIT:  sec_name = ".init";  break;
1798             case RELOC_SECTION_LIT8:  sec_name = ".lit8";  break;
1799             case RELOC_SECTION_LIT4:  sec_name = ".lit4";  break;
1800             case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1801             case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1802             case RELOC_SECTION_FINI:  sec_name = ".fini"; break;
1803             case RELOC_SECTION_LITA:  sec_name = ".lita";  break;
1804             default: abort ();
1805             }
1806
1807           sec = bfd_get_section_by_name (abfd, sec_name);
1808           if (sec == (asection *) NULL)
1809             abort ();
1810           rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1811
1812           rptr->addend = - bfd_get_section_vma (abfd, sec);
1813         }
1814
1815       rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1816
1817       /* Let the backend select the howto field and do any other
1818          required processing.  */
1819       (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1820     }
1821
1822   bfd_release (abfd, external_relocs);
1823
1824   section->relocation = internal_relocs;
1825
1826   return true;
1827 }
1828
1829 /* Get a canonical list of relocs.  */
1830
1831 unsigned int
1832 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1833      bfd *abfd;
1834      asection *section;
1835      arelent **relptr;
1836      asymbol **symbols;
1837 {
1838   unsigned int count;
1839
1840   if (section->flags & SEC_CONSTRUCTOR) 
1841     {
1842       arelent_chain *chain;
1843
1844       /* This section has relocs made up by us, not the file, so take
1845          them out of their chain and place them into the data area
1846          provided.  */
1847       for (count = 0, chain = section->constructor_chain;
1848            count < section->reloc_count;
1849            count++, chain = chain->next)
1850         *relptr++ = &chain->relent;
1851     }
1852   else
1853     { 
1854       arelent *tblptr;
1855
1856       if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1857         return 0;
1858
1859       tblptr = section->relocation;
1860       if (tblptr == (arelent *) NULL)
1861         return 0;
1862
1863       for (count = 0; count < section->reloc_count; count++)
1864         *relptr++ = tblptr++;
1865     }
1866
1867   *relptr = (arelent *) NULL;
1868
1869   return section->reloc_count;
1870 }
1871 \f
1872 /* Provided a BFD, a section and an offset into the section, calculate
1873    and return the name of the source file and the line nearest to the
1874    wanted location.  */
1875
1876 /*ARGSUSED*/
1877 boolean
1878 ecoff_find_nearest_line (abfd,
1879                          section,
1880                          ignore_symbols,
1881                          offset,
1882                          filename_ptr,
1883                          functionname_ptr,
1884                          retline_ptr)
1885      bfd *abfd;
1886      asection *section;
1887      asymbol **ignore_symbols;
1888      bfd_vma offset;
1889      CONST char **filename_ptr;
1890      CONST char **functionname_ptr;
1891      unsigned int *retline_ptr;
1892 {
1893   const struct ecoff_debug_swap * const debug_swap
1894     = &ecoff_backend (abfd)->debug_swap;
1895   FDR *fdr_ptr;
1896   FDR *fdr_start;
1897   FDR *fdr_end;
1898   FDR *fdr_hold;
1899   bfd_size_type external_pdr_size;
1900   char *pdr_ptr;
1901   char *pdr_end;
1902   PDR pdr;
1903   unsigned char *line_ptr;
1904   unsigned char *line_end;
1905   int lineno;
1906
1907   /* If we're not in the .text section, we don't have any line
1908      numbers.  */
1909   if (strcmp (section->name, _TEXT) != 0
1910       || offset < ecoff_data (abfd)->text_start
1911       || offset >= ecoff_data (abfd)->text_end)
1912     return false;
1913
1914   /* Make sure we have the FDR's.  */
1915   if (ecoff_slurp_symbolic_info (abfd) == false
1916       || bfd_get_symcount (abfd) == 0)
1917     return false;
1918
1919   /* Each file descriptor (FDR) has a memory address.  Here we track
1920      down which FDR we want.  The FDR's are stored in increasing
1921      memory order.  If speed is ever important, this can become a
1922      binary search.  We must ignore FDR's with no PDR entries; they
1923      will have the adr of the FDR before or after them.  */
1924   fdr_start = ecoff_data (abfd)->debug_info.fdr;
1925   fdr_end = fdr_start + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
1926   fdr_hold = (FDR *) NULL;
1927   for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1928     {
1929       if (fdr_ptr->cpd == 0)
1930         continue;
1931       if (offset < fdr_ptr->adr)
1932         break;
1933       fdr_hold = fdr_ptr;
1934     }
1935   if (fdr_hold == (FDR *) NULL)
1936     return false;
1937   fdr_ptr = fdr_hold;
1938
1939   /* Each FDR has a list of procedure descriptors (PDR).  PDR's also
1940      have an address, which is relative to the FDR address, and are
1941      also stored in increasing memory order.  */
1942   offset -= fdr_ptr->adr;
1943   external_pdr_size = debug_swap->external_pdr_size;
1944   pdr_ptr = ((char *) ecoff_data (abfd)->debug_info.external_pdr
1945              + fdr_ptr->ipdFirst * external_pdr_size);
1946   pdr_end = pdr_ptr + fdr_ptr->cpd * external_pdr_size;
1947   (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1948
1949   /* The address of the first PDR is an offset which applies to the
1950      addresses of all the PDR's.  */
1951   offset += pdr.adr;
1952
1953   for (pdr_ptr += external_pdr_size;
1954        pdr_ptr < pdr_end;
1955        pdr_ptr += external_pdr_size)
1956     {
1957       (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1958       if (offset < pdr.adr)
1959         break;
1960     }
1961
1962   /* Now we can look for the actual line number.  The line numbers are
1963      stored in a very funky format, which I won't try to describe.
1964      Note that right here pdr_ptr and pdr hold the PDR *after* the one
1965      we want; we need this to compute line_end.  */
1966   line_end = ecoff_data (abfd)->debug_info.line;
1967   if (pdr_ptr == pdr_end)
1968     line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
1969   else
1970     line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
1971
1972   /* Now change pdr and pdr_ptr to the one we want.  */
1973   pdr_ptr -= external_pdr_size;
1974   (*debug_swap->swap_pdr_in) (abfd, (PTR) pdr_ptr, &pdr);
1975
1976   offset -= pdr.adr;
1977   lineno = pdr.lnLow;
1978   line_ptr = (ecoff_data (abfd)->debug_info.line
1979               + fdr_ptr->cbLineOffset
1980               + pdr.cbLineOffset);
1981   while (line_ptr < line_end)
1982     {
1983       int delta;
1984       int count;
1985
1986       delta = *line_ptr >> 4;
1987       if (delta >= 0x8)
1988         delta -= 0x10;
1989       count = (*line_ptr & 0xf) + 1;
1990       ++line_ptr;
1991       if (delta == -8)
1992         {
1993           delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
1994           if (delta >= 0x8000)
1995             delta -= 0x10000;
1996           line_ptr += 2;
1997         }
1998       lineno += delta;
1999       if (offset < count * 4)
2000         break;
2001       offset -= count * 4;
2002     }
2003
2004   /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2005      at least according to gdb/mipsread.c.  */
2006   if (fdr_ptr->rss == -1)
2007     {
2008       *filename_ptr = NULL;
2009       if (pdr.isym == -1)
2010         *functionname_ptr = NULL;
2011       else
2012         {
2013           EXTR proc_ext;
2014
2015           (*debug_swap->swap_ext_in)
2016             (abfd,
2017              ((char *) ecoff_data (abfd)->debug_info.external_ext
2018               + pdr.isym * debug_swap->external_ext_size),
2019              &proc_ext);
2020           *functionname_ptr = (ecoff_data (abfd)->debug_info.ssext
2021                                + proc_ext.asym.iss);
2022         }
2023     }
2024   else
2025     {
2026       SYMR proc_sym;
2027
2028       *filename_ptr = (ecoff_data (abfd)->debug_info.ss
2029                        + fdr_ptr->issBase
2030                        + fdr_ptr->rss);
2031       (*debug_swap->swap_sym_in)
2032         (abfd,
2033          ((char *) ecoff_data (abfd)->debug_info.external_sym
2034           + (fdr_ptr->isymBase + pdr.isym) * debug_swap->external_sym_size),
2035          &proc_sym);
2036       *functionname_ptr = (ecoff_data (abfd)->debug_info.ss
2037                            + fdr_ptr->issBase
2038                            + proc_sym.iss);
2039     }
2040   if (lineno == ilineNil)
2041     lineno = 0;
2042   *retline_ptr = lineno;
2043   return true;
2044 }
2045 \f
2046 /* We can't use the generic linking routines for ECOFF, because we
2047    have to handle all the debugging information.  The generic link
2048    routine just works out the section contents and attaches a list of
2049    symbols.  We find each input BFD by looping over all the link_order
2050    information.  We accumulate the debugging information for each
2051    input BFD.  */
2052
2053 /* Get ECOFF EXTR information for an external symbol.  This function
2054    is passed to bfd_ecoff_debug_externals.  */
2055
2056 static boolean
2057 ecoff_get_extr (sym, esym)
2058      asymbol *sym;
2059      EXTR *esym;
2060 {
2061   ecoff_symbol_type *ecoff_sym_ptr;
2062   bfd *input_bfd;
2063
2064   /* Don't include debugging or local symbols.  */
2065   if ((sym->flags & BSF_DEBUGGING) != 0
2066       || (sym->flags & BSF_LOCAL) != 0)
2067     return false;
2068
2069   if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2070       || ecoffsymbol (sym)->native == NULL)
2071     {
2072       esym->jmptbl = 0;
2073       esym->cobol_main = 0;
2074       esym->weakext = 0;
2075       esym->reserved = 0;
2076       esym->ifd = ifdNil;
2077       /* FIXME: we can do better than this for st and sc.  */
2078       esym->asym.st = stGlobal;
2079       esym->asym.sc = scAbs;
2080       esym->asym.reserved = 0;
2081       esym->asym.index = indexNil;
2082       return true;
2083     }
2084
2085   ecoff_sym_ptr = ecoffsymbol (sym);
2086
2087   if (ecoff_sym_ptr->local)
2088     abort ();
2089
2090   input_bfd = bfd_asymbol_bfd (sym);
2091   (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2092     (input_bfd, ecoff_sym_ptr->native, esym);
2093
2094   /* If the symbol was defined by the linker, then esym will be
2095      undefined but sym will not be.  Get a better class for such a
2096      symbol.  */
2097   if ((esym->asym.sc == scUndefined
2098        || esym->asym.sc == scSUndefined)
2099       && bfd_get_section (sym) != &bfd_und_section)
2100     esym->asym.sc = scAbs;
2101
2102   /* Adjust the FDR index for the symbol by that used for the input
2103      BFD.  */
2104   esym->ifd += ecoff_data (input_bfd)->debug_info.ifdbase;
2105
2106   return true;
2107 }
2108
2109 /* Set the external symbol index.  This routine is passed to
2110    bfd_ecoff_debug_externals.  */
2111
2112 static void
2113 ecoff_set_index (sym, indx)
2114      asymbol *sym;
2115      bfd_size_type indx;
2116 {
2117   ecoff_set_sym_index (sym, indx);
2118 }
2119
2120 /* This is the actual link routine.  It builds the debugging
2121    information, and then lets the generic linking routine complete the
2122    link.  */
2123
2124 boolean
2125 ecoff_bfd_final_link (abfd, info)
2126      bfd *abfd;
2127      struct bfd_link_info *info;
2128 {
2129   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2130   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2131   HDRR *symhdr;
2132   register bfd *input_bfd;
2133   asection *o;
2134
2135   /* We accumulate the debugging information counts in the symbolic
2136      header.  */
2137   symhdr = &debug->symbolic_header;
2138   symhdr->magic = backend->debug_swap.sym_magic;
2139   /* FIXME: What should the version stamp be?  */
2140   symhdr->vstamp = 0;
2141   symhdr->ilineMax = 0;
2142   symhdr->cbLine = 0;
2143   symhdr->idnMax = 0;
2144   symhdr->ipdMax = 0;
2145   symhdr->isymMax = 0;
2146   symhdr->ioptMax = 0;
2147   symhdr->iauxMax = 0;
2148   symhdr->issMax = 0;
2149   symhdr->ifdMax = 0;
2150   symhdr->crfd = 0;
2151
2152   /* We accumulate the debugging information itself in the debug_info
2153      structure.  */
2154   debug->line = debug->line_end = NULL;
2155   debug->external_dnr = debug->external_dnr_end = NULL;
2156   debug->external_pdr = debug->external_pdr_end = NULL;
2157   debug->external_sym = debug->external_sym_end = NULL;
2158   debug->external_opt = debug->external_opt_end = NULL;
2159   debug->external_aux = debug->external_aux_end = NULL;
2160   debug->ss = debug->ss_end = NULL;
2161   debug->external_fdr = debug->external_fdr_end = NULL;
2162   debug->external_rfd = debug->external_rfd_end = NULL;
2163
2164   /* We accumulate the debugging symbols from each input BFD.  */
2165   for (input_bfd = info->input_bfds;
2166        input_bfd != (bfd *) NULL;
2167        input_bfd = input_bfd->link_next)
2168     {
2169       boolean ret;
2170
2171       if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
2172         ret = (bfd_ecoff_debug_accumulate
2173                (abfd, debug, &backend->debug_swap,
2174                 input_bfd, &ecoff_data (input_bfd)->debug_info,
2175                 &ecoff_backend (input_bfd)->debug_swap, info->relocateable));
2176       else
2177         ret = bfd_ecoff_debug_link_other (abfd,
2178                                           debug,
2179                                           &backend->debug_swap,
2180                                           input_bfd);
2181
2182       if (! ret)
2183         return false;
2184
2185       /* Combine the register masks.  */
2186       ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
2187       ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
2188       ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
2189       ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
2190       ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
2191       ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
2192     }
2193
2194   /* Don't let the generic routine link the .reginfo sections.  */
2195   for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2196     {
2197       if (strcmp (o->name, REGINFO) == 0)
2198         {
2199           o->link_order_head = (struct bfd_link_order *) NULL;
2200           break;
2201         }
2202     }
2203
2204   /* Let the generic link routine handle writing out the section
2205      contents.  */
2206   return _bfd_generic_final_link (abfd, info);
2207 }
2208 \f
2209 /* Set the architecture.  The supported architecture is stored in the
2210    backend pointer.  We always set the architecture anyhow, since many
2211    callers ignore the return value.  */
2212
2213 boolean
2214 ecoff_set_arch_mach (abfd, arch, machine)
2215      bfd *abfd;
2216      enum bfd_architecture arch;
2217      unsigned long machine;
2218 {
2219   bfd_default_set_arch_mach (abfd, arch, machine);
2220   return arch == ecoff_backend (abfd)->arch;
2221 }
2222
2223 /* Get the size of the section headers.  We do not output the .reginfo
2224    section.  */
2225
2226 /*ARGSUSED*/
2227 int
2228 ecoff_sizeof_headers (abfd, reloc)
2229      bfd *abfd;
2230      boolean reloc;
2231 {
2232   asection *current;
2233   int c;
2234
2235   c = 0;
2236   for (current = abfd->sections;
2237        current != (asection *)NULL; 
2238        current = current->next) 
2239     if (strcmp (current->name, REGINFO) != 0)
2240       ++c;
2241
2242   return (bfd_coff_filhsz (abfd)
2243           + bfd_coff_aoutsz (abfd)
2244           + c * bfd_coff_scnhsz (abfd));
2245 }
2246
2247 /* Get the contents of a section.  This is where we handle reading the
2248    .reginfo section, which implicitly holds the contents of an
2249    ecoff_reginfo structure.  */
2250
2251 boolean
2252 ecoff_get_section_contents (abfd, section, location, offset, count)
2253      bfd *abfd;
2254      asection *section;
2255      PTR location;
2256      file_ptr offset;
2257      bfd_size_type count;
2258 {
2259   ecoff_data_type *tdata = ecoff_data (abfd);
2260   struct ecoff_reginfo s;
2261   int i;
2262
2263   if (strcmp (section->name, REGINFO) != 0)
2264     return bfd_generic_get_section_contents (abfd, section, location,
2265                                              offset, count);
2266
2267   s.gp_value = tdata->gp;
2268   s.gprmask = tdata->gprmask;
2269   for (i = 0; i < 4; i++)
2270     s.cprmask[i] = tdata->cprmask[i];
2271   s.fprmask = tdata->fprmask;
2272
2273   /* bfd_get_section_contents has already checked that the offset and
2274      size is reasonable.  We don't have to worry about swapping or any
2275      such thing; the .reginfo section is defined such that the
2276      contents are an ecoff_reginfo structure as seen on the host.  */
2277   memcpy (location, ((char *) &s) + offset, (size_t) count);
2278   return true;
2279 }
2280
2281 /* Calculate the file position for each section, and set
2282    reloc_filepos.  */
2283
2284 static void
2285 ecoff_compute_section_file_positions (abfd)
2286      bfd *abfd;
2287 {
2288   asection *current;
2289   file_ptr sofar;
2290   file_ptr old_sofar;
2291   boolean first_data;
2292
2293   sofar = ecoff_sizeof_headers (abfd, false);
2294
2295   first_data = true;
2296   for (current = abfd->sections;
2297        current != (asection *) NULL;
2298        current = current->next)
2299     {
2300       /* Only deal with sections which have contents */
2301       if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) == 0
2302           || strcmp (current->name, REGINFO) == 0)
2303         continue;
2304
2305       /* On Ultrix, the data sections in an executable file must be
2306          aligned to a page boundary within the file.  This does not
2307          affect the section size, though.  FIXME: Does this work for
2308          other platforms?  It requires some modification for the
2309          Alpha, because .rdata on the Alpha goes with the text, not
2310          the data.  */
2311       if ((abfd->flags & EXEC_P) != 0
2312           && (abfd->flags & D_PAGED) != 0
2313           && first_data != false
2314           && (current->flags & SEC_CODE) == 0
2315           && (! ecoff_backend (abfd)->rdata_in_text
2316               || strcmp (current->name, _RDATA) != 0)
2317           && strcmp (current->name, _PDATA) != 0)
2318         {
2319           const bfd_vma round = ecoff_backend (abfd)->round;
2320
2321           sofar = (sofar + round - 1) &~ (round - 1);
2322           first_data = false;
2323         }
2324
2325       /* Align the sections in the file to the same boundary on
2326          which they are aligned in virtual memory.  */
2327       old_sofar = sofar;
2328       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2329
2330       current->filepos = sofar;
2331
2332       sofar += current->_raw_size;
2333
2334       /* make sure that this section is of the right size too */
2335       old_sofar = sofar;
2336       sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2337       current->_raw_size += sofar - old_sofar;
2338     }
2339
2340   ecoff_data (abfd)->reloc_filepos = sofar;
2341 }
2342
2343 /* Set the contents of a section.  This is where we handle setting the
2344    contents of the .reginfo section, which implicitly holds a
2345    ecoff_reginfo structure.  */
2346
2347 boolean
2348 ecoff_set_section_contents (abfd, section, location, offset, count)
2349      bfd *abfd;
2350      asection *section;
2351      PTR location;
2352      file_ptr offset;
2353      bfd_size_type count;
2354 {
2355   if (abfd->output_has_begun == false)
2356     ecoff_compute_section_file_positions (abfd);
2357
2358   if (strcmp (section->name, REGINFO) == 0)
2359     {
2360       ecoff_data_type *tdata = ecoff_data (abfd);
2361       struct ecoff_reginfo s;
2362       int i;
2363
2364       /* If the caller is only changing part of the structure, we must
2365          retrieve the current information before the memcpy.  */
2366       if (offset != 0 || count != sizeof (struct ecoff_reginfo))
2367         {
2368           s.gp_value = tdata->gp;
2369           s.gprmask = tdata->gprmask;
2370           for (i = 0; i < 4; i++)
2371             s.cprmask[i] = tdata->cprmask[i];
2372           s.fprmask = tdata->fprmask;
2373         }
2374
2375       /* bfd_set_section_contents has already checked that the offset
2376          and size is reasonable.  We don't have to worry about
2377          swapping or any such thing; the .reginfo section is defined
2378          such that the contents are an ecoff_reginfo structure as seen
2379          on the host.  */
2380       memcpy (((char *) &s) + offset, location, (size_t) count);
2381
2382       tdata->gp = s.gp_value;
2383       tdata->gprmask = s.gprmask;
2384       for (i = 0; i < 4; i++)
2385         tdata->cprmask[i] = s.cprmask[i];
2386       tdata->fprmask = s.fprmask;
2387
2388       return true;
2389
2390     }
2391
2392   bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
2393
2394   if (count != 0)
2395     return (bfd_write (location, 1, count, abfd) == count) ? true : false;
2396
2397   return true;
2398 }
2399
2400 /* Write out an ECOFF file.  */
2401
2402 boolean
2403 ecoff_write_object_contents (abfd)
2404      bfd *abfd;
2405 {
2406   const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2407   const bfd_vma round = backend->round;
2408   const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2409   const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2410   const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2411   const bfd_size_type external_hdr_size
2412     = backend->debug_swap.external_hdr_size;
2413   const bfd_size_type external_reloc_size = backend->external_reloc_size;
2414   void (* const adjust_reloc_out) PARAMS ((bfd *,
2415                                            const arelent *,
2416                                            struct internal_reloc *))
2417     = backend->adjust_reloc_out;
2418   void (* const swap_reloc_out) PARAMS ((bfd *,
2419                                          const struct internal_reloc *,
2420                                          PTR))
2421     = backend->swap_reloc_out;
2422   struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2423   HDRR * const symhdr = &debug->symbolic_header;
2424   asection *current;
2425   unsigned int count;
2426   file_ptr reloc_base;
2427   file_ptr sym_base;
2428   unsigned long reloc_size;
2429   unsigned long text_size;
2430   unsigned long text_start;
2431   unsigned long data_size;
2432   unsigned long data_start;
2433   unsigned long bss_size;
2434   PTR buff;
2435   struct internal_filehdr internal_f;
2436   struct internal_aouthdr internal_a;
2437   int i;
2438
2439   bfd_error = system_call_error;
2440
2441   if(abfd->output_has_begun == false)
2442     ecoff_compute_section_file_positions(abfd);
2443
2444   reloc_base = ecoff_data (abfd)->reloc_filepos;
2445
2446   count = 1;
2447   reloc_size = 0;
2448   for (current = abfd->sections;
2449        current != (asection *)NULL; 
2450        current = current->next) 
2451     {
2452       if (strcmp (current->name, REGINFO) == 0)
2453         continue;
2454       current->target_index = count;
2455       ++count;
2456       if (current->reloc_count != 0)
2457         {
2458           bfd_size_type relsize;
2459
2460           current->rel_filepos = reloc_base;
2461           relsize = current->reloc_count * external_reloc_size;
2462           reloc_size += relsize;
2463           reloc_base += relsize;
2464         }
2465       else
2466         current->rel_filepos = 0;
2467     }
2468
2469   sym_base = reloc_base + reloc_size;
2470
2471   /* At least on Ultrix, the symbol table of an executable file must
2472      be aligned to a page boundary.  FIXME: Is this true on other
2473      platforms?  */
2474   if ((abfd->flags & EXEC_P) != 0
2475       && (abfd->flags & D_PAGED) != 0)
2476     sym_base = (sym_base + round - 1) &~ (round - 1);
2477
2478   ecoff_data (abfd)->sym_filepos = sym_base;
2479
2480   if ((abfd->flags & D_PAGED) != 0)
2481     text_size = ecoff_sizeof_headers (abfd, false);
2482   else
2483     text_size = 0;
2484   text_start = 0;
2485   data_size = 0;
2486   data_start = 0;
2487   bss_size = 0;
2488
2489   /* Write section headers to the file.  */
2490
2491   buff = (PTR) alloca (scnhsz);
2492   internal_f.f_nscns = 0;
2493   if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2494     return false;
2495   for (current = abfd->sections;
2496        current != (asection *) NULL;
2497        current = current->next)
2498     {
2499       struct internal_scnhdr section;
2500       bfd_vma vma;
2501
2502       if (strcmp (current->name, REGINFO) == 0)
2503         {
2504           BFD_ASSERT (current->reloc_count == 0);
2505           continue;
2506         }
2507
2508       ++internal_f.f_nscns;
2509
2510       strncpy (section.s_name, current->name, sizeof section.s_name);
2511
2512       /* FIXME: is this correct for shared libraries?  I think it is
2513          but I have no platform to check.  Ian Lance Taylor.  */
2514       vma = bfd_get_section_vma (abfd, current);
2515       if (strcmp (current->name, _LIB) == 0)
2516         section.s_vaddr = 0;
2517       else
2518         section.s_vaddr = vma;
2519
2520       section.s_paddr = vma;
2521       section.s_size = bfd_get_section_size_before_reloc (current);
2522
2523       /* If this section is unloadable then the scnptr will be 0.  */
2524       if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2525         section.s_scnptr = 0;
2526       else
2527         section.s_scnptr = current->filepos;
2528       section.s_relptr = current->rel_filepos;
2529
2530       /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2531          object file produced by the assembler is supposed to point to
2532          information about how much room is required by objects of
2533          various different sizes.  I think this only matters if we
2534          want the linker to compute the best size to use, or
2535          something.  I don't know what happens if the information is
2536          not present.  */
2537       section.s_lnnoptr = 0;
2538
2539       section.s_nreloc = current->reloc_count;
2540       section.s_nlnno = 0;
2541       section.s_flags = ecoff_sec_to_styp_flags (current->name,
2542                                                  current->flags);
2543
2544       bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff);
2545       if (bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2546         return false;
2547
2548       if ((section.s_flags & STYP_TEXT) != 0
2549           || ((section.s_flags & STYP_RDATA) != 0
2550               && backend->rdata_in_text)
2551           || strcmp (current->name, _PDATA) == 0)
2552         {
2553           text_size += bfd_get_section_size_before_reloc (current);
2554           if (text_start == 0 || text_start > vma)
2555             text_start = vma;
2556         }
2557       else if ((section.s_flags & STYP_RDATA) != 0
2558                || (section.s_flags & STYP_DATA) != 0
2559                || (section.s_flags & STYP_LITA) != 0
2560                || (section.s_flags & STYP_LIT8) != 0
2561                || (section.s_flags & STYP_LIT4) != 0
2562                || (section.s_flags & STYP_SDATA) != 0)
2563         {
2564           data_size += bfd_get_section_size_before_reloc (current);
2565           if (data_start == 0 || data_start > vma)
2566             data_start = vma;
2567         }
2568       else if ((section.s_flags & STYP_BSS) != 0
2569                || (section.s_flags & STYP_SBSS) != 0)
2570         bss_size += bfd_get_section_size_before_reloc (current);
2571     }   
2572
2573   /* Set up the file header.  */
2574
2575   internal_f.f_magic = ecoff_get_magic (abfd);
2576
2577   /* We will NOT put a fucking timestamp in the header here. Every
2578      time you put it back, I will come in and take it out again.  I'm
2579      sorry.  This field does not belong here.  We fill it with a 0 so
2580      it compares the same but is not a reasonable time. --
2581      gnu@cygnus.com.  */
2582   internal_f.f_timdat = 0;
2583
2584   if (bfd_get_symcount (abfd) != 0)
2585     {
2586       /* The ECOFF f_nsyms field is not actually the number of
2587          symbols, it's the size of symbolic information header.  */
2588       internal_f.f_nsyms = external_hdr_size;
2589       internal_f.f_symptr = sym_base;
2590     }
2591   else
2592     {
2593       internal_f.f_nsyms = 0;
2594       internal_f.f_symptr = 0;
2595     }
2596
2597   internal_f.f_opthdr = aoutsz;
2598
2599   internal_f.f_flags = F_LNNO;
2600   if (reloc_size == 0)
2601     internal_f.f_flags |= F_RELFLG;
2602   if (bfd_get_symcount (abfd) == 0)
2603     internal_f.f_flags |= F_LSYMS;
2604   if (abfd->flags & EXEC_P)
2605     internal_f.f_flags |= F_EXEC;
2606
2607   if (! abfd->xvec->byteorder_big_p)
2608     internal_f.f_flags |= F_AR32WR;
2609   else
2610     internal_f.f_flags |= F_AR32W;
2611
2612   /* Set up the ``optional'' header.  */
2613   if ((abfd->flags & D_PAGED) != 0)
2614     internal_a.magic = ECOFF_AOUT_ZMAGIC;
2615   else
2616     internal_a.magic = ECOFF_AOUT_OMAGIC;
2617
2618   /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
2619      linker happy.  But, is it right?  */
2620   internal_a.vstamp = 0x20a;
2621
2622   /* At least on Ultrix, these have to be rounded to page boundaries.
2623      FIXME: Is this true on other platforms?  */
2624   if ((abfd->flags & D_PAGED) != 0)
2625     {
2626       internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2627       internal_a.text_start = text_start &~ (round - 1);
2628       internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2629       internal_a.data_start = data_start &~ (round - 1);
2630     }
2631   else
2632     {
2633       internal_a.tsize = text_size;
2634       internal_a.text_start = text_start;
2635       internal_a.dsize = data_size;
2636       internal_a.data_start = data_start;
2637     }
2638
2639   /* On Ultrix, the initial portions of the .sbss and .bss segments
2640      are at the end of the data section.  The bsize field in the
2641      optional header records how many bss bytes are required beyond
2642      those in the data section.  The value is not rounded to a page
2643      boundary.  */
2644   if (bss_size < internal_a.dsize - data_size)
2645     bss_size = 0;
2646   else
2647     bss_size -= internal_a.dsize - data_size;
2648   internal_a.bsize = bss_size;
2649   internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2650
2651   internal_a.entry = bfd_get_start_address (abfd);
2652
2653   internal_a.gp_value = ecoff_data (abfd)->gp;
2654
2655   internal_a.gprmask = ecoff_data (abfd)->gprmask;
2656   internal_a.fprmask = ecoff_data (abfd)->fprmask;
2657   for (i = 0; i < 4; i++)
2658     internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2659
2660   /* Write out the file header and the optional header.  */
2661
2662   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2663     return false;
2664
2665   buff = (PTR) alloca (filhsz);
2666   bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2667   if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2668     return false;
2669
2670   buff = (PTR) alloca (aoutsz);
2671   bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2672   if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2673     return false;
2674
2675   /* Build the external symbol information.  This must be done before
2676      writing out the relocs so that we know the symbol indices.  */
2677   symhdr->iextMax = 0;
2678   symhdr->issExtMax = 0;
2679   debug->external_ext = debug->external_ext_end = NULL;
2680   debug->ssext = debug->ssext_end = NULL;
2681   if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2682                                  (((abfd->flags & EXEC_P) == 0)
2683                                   ? true : false),
2684                                  ecoff_get_extr, ecoff_set_index)
2685       == false)
2686     return false;
2687
2688   /* Write out the relocs.  */
2689   for (current = abfd->sections;
2690        current != (asection *) NULL;
2691        current = current->next)
2692     {
2693       arelent **reloc_ptr_ptr;
2694       arelent **reloc_end;
2695       char *out_ptr;
2696
2697       if (current->reloc_count == 0)
2698         continue;
2699
2700       buff = bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2701       if (buff == NULL)
2702         {
2703           bfd_error = no_memory;
2704           return false;
2705         }
2706
2707       reloc_ptr_ptr = current->orelocation;
2708       reloc_end = reloc_ptr_ptr + current->reloc_count;
2709       out_ptr = (char *) buff;
2710       for (;
2711            reloc_ptr_ptr < reloc_end;
2712            reloc_ptr_ptr++, out_ptr += external_reloc_size)
2713         {
2714           arelent *reloc;
2715           asymbol *sym;
2716           struct internal_reloc in;
2717           
2718           memset (&in, 0, sizeof in);
2719
2720           reloc = *reloc_ptr_ptr;
2721           sym = *reloc->sym_ptr_ptr;
2722
2723           in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
2724           in.r_type = reloc->howto->type;
2725
2726           if ((sym->flags & BSF_SECTION_SYM) == 0)
2727             {
2728               in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2729               in.r_extern = 1;
2730             }
2731           else
2732             {
2733               CONST char *name;
2734
2735               name = bfd_get_section_name (abfd, bfd_get_section (sym));
2736               if (strcmp (name, ".text") == 0)
2737                 in.r_symndx = RELOC_SECTION_TEXT;
2738               else if (strcmp (name, ".rdata") == 0)
2739                 in.r_symndx = RELOC_SECTION_RDATA;
2740               else if (strcmp (name, ".data") == 0)
2741                 in.r_symndx = RELOC_SECTION_DATA;
2742               else if (strcmp (name, ".sdata") == 0)
2743                 in.r_symndx = RELOC_SECTION_SDATA;
2744               else if (strcmp (name, ".sbss") == 0)
2745                 in.r_symndx = RELOC_SECTION_SBSS;
2746               else if (strcmp (name, ".bss") == 0)
2747                 in.r_symndx = RELOC_SECTION_BSS;
2748               else if (strcmp (name, ".init") == 0)
2749                 in.r_symndx = RELOC_SECTION_INIT;
2750               else if (strcmp (name, ".lit8") == 0)
2751                 in.r_symndx = RELOC_SECTION_LIT8;
2752               else if (strcmp (name, ".lit4") == 0)
2753                 in.r_symndx = RELOC_SECTION_LIT4;
2754               else if (strcmp (name, ".xdata") == 0)
2755                 in.r_symndx = RELOC_SECTION_XDATA;
2756               else if (strcmp (name, ".pdata") == 0)
2757                 in.r_symndx = RELOC_SECTION_PDATA;
2758               else if (strcmp (name, ".fini") == 0)
2759                 in.r_symndx = RELOC_SECTION_FINI;
2760               else if (strcmp (name, ".lita") == 0)
2761                 in.r_symndx = RELOC_SECTION_LITA;
2762               else if (strcmp (name, "*ABS*") == 0)
2763                 in.r_symndx = RELOC_SECTION_ABS;
2764               else
2765                 abort ();
2766               in.r_extern = 0;
2767             }
2768
2769           (*adjust_reloc_out) (abfd, reloc, &in);
2770
2771           (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2772         }
2773
2774       if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2775         return false;
2776       if (bfd_write (buff, external_reloc_size, current->reloc_count, abfd)
2777           != external_reloc_size * current->reloc_count)
2778         return false;
2779       bfd_release (abfd, buff);
2780     }
2781
2782   /* Write out the symbolic debugging information.  */
2783   if (bfd_get_symcount (abfd) > 0)
2784     {
2785       /* Write out the debugging information.  */
2786       if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2787                                  ecoff_data (abfd)->sym_filepos)
2788           == false)
2789         return false;
2790     }
2791   else if ((abfd->flags & EXEC_P) != 0
2792            && (abfd->flags & D_PAGED) != 0)
2793     {
2794       char c;
2795
2796       /* A demand paged executable must occupy an even number of
2797          pages.  */
2798       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2799                     SEEK_SET) != 0)
2800         return false;
2801       if (bfd_read (&c, 1, 1, abfd) == 0)
2802         c = 0;
2803       if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2804                     SEEK_SET) != 0)
2805         return false;
2806       if (bfd_write (&c, 1, 1, abfd) != 1)
2807         return false;      
2808     }
2809
2810   return true;
2811 }
2812 \f
2813 /* Archive handling.  ECOFF uses what appears to be a unique type of
2814    archive header (armap).  The byte ordering of the armap and the
2815    contents are encoded in the name of the armap itself.  At least for
2816    now, we only support archives with the same byte ordering in the
2817    armap and the contents.
2818
2819    The first four bytes in the armap are the number of symbol
2820    definitions.  This is always a power of two.
2821
2822    This is followed by the symbol definitions.  Each symbol definition
2823    occupies 8 bytes.  The first four bytes are the offset from the
2824    start of the armap strings to the null-terminated string naming
2825    this symbol.  The second four bytes are the file offset to the
2826    archive member which defines this symbol.  If the second four bytes
2827    are 0, then this is not actually a symbol definition, and it should
2828    be ignored.
2829
2830    The symbols are hashed into the armap with a closed hashing scheme.
2831    See the functions below for the details of the algorithm.
2832
2833    After the symbol definitions comes four bytes holding the size of
2834    the string table, followed by the string table itself.  */
2835
2836 /* The name of an archive headers looks like this:
2837    __________E[BL]E[BL]_ (with a trailing space).
2838    The trailing space is changed to an X if the archive is changed to
2839    indicate that the armap is out of date.
2840
2841    The Alpha seems to use ________64E[BL]E[BL]_.  */
2842
2843 #define ARMAP_BIG_ENDIAN 'B'
2844 #define ARMAP_LITTLE_ENDIAN 'L'
2845 #define ARMAP_MARKER 'E'
2846 #define ARMAP_START_LENGTH 10
2847 #define ARMAP_HEADER_MARKER_INDEX 10
2848 #define ARMAP_HEADER_ENDIAN_INDEX 11
2849 #define ARMAP_OBJECT_MARKER_INDEX 12
2850 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2851 #define ARMAP_END_INDEX 14
2852 #define ARMAP_END "_ "
2853
2854 /* This is a magic number used in the hashing algorithm.  */
2855 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2856
2857 /* This returns the hash value to use for a string.  It also sets
2858    *REHASH to the rehash adjustment if the first slot is taken.  SIZE
2859    is the number of entries in the hash table, and HLOG is the log
2860    base 2 of SIZE.  */
2861
2862 static unsigned int
2863 ecoff_armap_hash (s, rehash, size, hlog)
2864      CONST char *s;
2865      unsigned int *rehash;
2866      unsigned int size;
2867      unsigned int hlog;
2868 {
2869   unsigned int hash;
2870
2871   hash = *s++;
2872   while (*s != '\0')
2873     hash = ((hash >> 27) | (hash << 5)) + *s++;
2874   hash *= ARMAP_HASH_MAGIC;
2875   *rehash = (hash & (size - 1)) | 1;
2876   return hash >> (32 - hlog);
2877 }
2878
2879 /* Read in the armap.  */
2880
2881 boolean
2882 ecoff_slurp_armap (abfd)
2883      bfd *abfd;
2884 {
2885   char nextname[17];
2886   unsigned int i;
2887   struct areltdata *mapdata;
2888   bfd_size_type parsed_size;
2889   char *raw_armap;
2890   struct artdata *ardata;
2891   unsigned int count;
2892   char *raw_ptr;
2893   struct symdef *symdef_ptr;
2894   char *stringbase;
2895   
2896   /* Get the name of the first element.  */
2897   i = bfd_read ((PTR) nextname, 1, 16, abfd);
2898   if (i == 0)
2899       return true;
2900   if (i != 16)
2901       return false;
2902
2903   bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
2904
2905   /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2906      standard COFF armap.  We could move the ECOFF armap stuff into
2907      bfd_slurp_armap, but that seems inappropriate since no other
2908      target uses this format.  Instead, we check directly for a COFF
2909      armap.  */
2910   if (strncmp (nextname, "/               ", 16) == 0)
2911     return bfd_slurp_armap (abfd);
2912
2913   /* See if the first element is an armap.  */
2914   if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2915                ARMAP_START_LENGTH) != 0
2916       || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2917       || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2918           && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2919       || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2920       || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2921           && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2922       || strncmp (nextname + ARMAP_END_INDEX,
2923                   ARMAP_END, sizeof ARMAP_END - 1) != 0)
2924     {
2925       bfd_has_map (abfd) = false;
2926       return true;
2927     }
2928
2929   /* Make sure we have the right byte ordering.  */
2930   if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2931        ^ (abfd->xvec->header_byteorder_big_p != false))
2932       || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2933           ^ (abfd->xvec->byteorder_big_p != false)))
2934     {
2935       bfd_error = wrong_format;
2936       return false;
2937     }
2938
2939   /* Read in the armap.  */
2940   ardata = bfd_ardata (abfd);
2941   mapdata = _bfd_snarf_ar_hdr (abfd);
2942   if (mapdata == (struct areltdata *) NULL)
2943     return false;
2944   parsed_size = mapdata->parsed_size;
2945   bfd_release (abfd, (PTR) mapdata);
2946     
2947   raw_armap = (char *) bfd_alloc (abfd, parsed_size);
2948   if (raw_armap == (char *) NULL)
2949     {
2950       bfd_error = no_memory;
2951       return false;
2952     }
2953     
2954   if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
2955     {
2956       bfd_error = malformed_archive;
2957       bfd_release (abfd, (PTR) raw_armap);
2958       return false;
2959     }
2960     
2961   ardata->tdata = (PTR) raw_armap;
2962
2963   count = bfd_h_get_32 (abfd, (PTR) raw_armap);
2964
2965   ardata->symdef_count = 0;
2966   ardata->cache = (struct ar_cache *) NULL;
2967
2968   /* This code used to overlay the symdefs over the raw archive data,
2969      but that doesn't work on a 64 bit host.  */
2970
2971   stringbase = raw_armap + count * 8 + 8;
2972
2973 #ifdef CHECK_ARMAP_HASH
2974   {
2975     unsigned int hlog;
2976
2977     /* Double check that I have the hashing algorithm right by making
2978        sure that every symbol can be looked up successfully.  */
2979     hlog = 0;
2980     for (i = 1; i < count; i <<= 1)
2981       hlog++;
2982     BFD_ASSERT (i == count);
2983
2984     raw_ptr = raw_armap + 4;
2985     for (i = 0; i < count; i++, raw_ptr += 8)
2986       {
2987         unsigned int name_offset, file_offset;
2988         unsigned int hash, rehash, srch;
2989       
2990         name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
2991         file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
2992         if (file_offset == 0)
2993           continue;
2994         hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
2995                                  hlog);
2996         if (hash == i)
2997           continue;
2998
2999         /* See if we can rehash to this location.  */
3000         for (srch = (hash + rehash) & (count - 1);
3001              srch != hash && srch != i;
3002              srch = (srch + rehash) & (count - 1))
3003           BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3004                       != 0);
3005         BFD_ASSERT (srch == i);
3006       }
3007   }
3008
3009 #endif /* CHECK_ARMAP_HASH */
3010
3011   raw_ptr = raw_armap + 4;
3012   for (i = 0; i < count; i++, raw_ptr += 8)
3013     if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3014       ++ardata->symdef_count;
3015
3016   symdef_ptr = ((struct symdef *)
3017                 bfd_alloc (abfd,
3018                            ardata->symdef_count * sizeof (struct symdef)));
3019   ardata->symdefs = (carsym *) symdef_ptr;
3020
3021   raw_ptr = raw_armap + 4;
3022   for (i = 0; i < count; i++, raw_ptr += 8)
3023     {
3024       unsigned int name_offset, file_offset;
3025
3026       file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3027       if (file_offset == 0)
3028         continue;
3029       name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3030       symdef_ptr->s.name = stringbase + name_offset;
3031       symdef_ptr->file_offset = file_offset;
3032       ++symdef_ptr;
3033     }
3034
3035   ardata->first_file_filepos = bfd_tell (abfd);
3036   /* Pad to an even boundary.  */
3037   ardata->first_file_filepos += ardata->first_file_filepos % 2;
3038
3039   bfd_has_map (abfd) = true;
3040
3041   return true;
3042 }
3043
3044 /* Write out an armap.  */
3045
3046 boolean
3047 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3048      bfd *abfd;
3049      unsigned int elength;
3050      struct orl *map;
3051      unsigned int orl_count;
3052      int stridx;
3053 {
3054   unsigned int hashsize, hashlog;
3055   unsigned int symdefsize;
3056   int padit;
3057   unsigned int stringsize;
3058   unsigned int mapsize;
3059   file_ptr firstreal;
3060   struct ar_hdr hdr;
3061   struct stat statbuf;
3062   unsigned int i;
3063   bfd_byte temp[4];
3064   bfd_byte *hashtable;
3065   bfd *current;
3066   bfd *last_elt;
3067
3068   /* Ultrix appears to use as a hash table size the least power of two
3069      greater than twice the number of entries.  */
3070   for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3071     ;
3072   hashsize = 1 << hashlog;
3073
3074   symdefsize = hashsize * 8;
3075   padit = stridx % 2;
3076   stringsize = stridx + padit;
3077
3078   /* Include 8 bytes to store symdefsize and stringsize in output. */
3079   mapsize = symdefsize + stringsize + 8;
3080
3081   firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3082
3083   memset ((PTR) &hdr, 0, sizeof hdr);
3084
3085   /* Work out the ECOFF armap name.  */
3086   strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3087   hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3088   hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3089     (abfd->xvec->header_byteorder_big_p
3090      ? ARMAP_BIG_ENDIAN
3091      : ARMAP_LITTLE_ENDIAN);
3092   hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3093   hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3094     abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3095   memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3096
3097   /* Write the timestamp of the archive header to be just a little bit
3098      later than the timestamp of the file, otherwise the linker will
3099      complain that the index is out of date.  Actually, the Ultrix
3100      linker just checks the archive name; the GNU linker may check the
3101      date.  */
3102   stat (abfd->filename, &statbuf);
3103   sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3104
3105   /* The DECstation uses zeroes for the uid, gid and mode of the
3106      armap.  */
3107   hdr.ar_uid[0] = '0';
3108   hdr.ar_gid[0] = '0';
3109   hdr.ar_mode[0] = '0';
3110
3111   sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3112
3113   hdr.ar_fmag[0] = '`';
3114   hdr.ar_fmag[1] = '\n';
3115
3116   /* Turn all null bytes in the header into spaces.  */
3117   for (i = 0; i < sizeof (struct ar_hdr); i++)
3118    if (((char *)(&hdr))[i] == '\0')
3119      (((char *)(&hdr))[i]) = ' ';
3120
3121   if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3122       != sizeof (struct ar_hdr))
3123     return false;
3124
3125   bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3126   if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3127     return false;
3128   
3129   hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3130
3131   current = abfd->archive_head;
3132   last_elt = current;
3133   for (i = 0; i < orl_count; i++)
3134     {
3135       unsigned int hash, rehash;
3136
3137       /* Advance firstreal to the file position of this archive
3138          element.  */
3139       if (((bfd *) map[i].pos) != last_elt)
3140         {
3141           do
3142             {
3143               firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3144               firstreal += firstreal % 2;
3145               current = current->next;
3146             }
3147           while (current != (bfd *) map[i].pos);
3148         }
3149
3150       last_elt = current;
3151
3152       hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3153       if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3154         {
3155           unsigned int srch;
3156
3157           /* The desired slot is already taken.  */
3158           for (srch = (hash + rehash) & (hashsize - 1);
3159                srch != hash;
3160                srch = (srch + rehash) & (hashsize - 1))
3161             if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3162               break;
3163
3164           BFD_ASSERT (srch != hash);
3165
3166           hash = srch;
3167         }
3168         
3169       bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3170                     (PTR) (hashtable + hash * 8));
3171       bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3172                     (PTR) (hashtable + hash * 8 + 4));
3173     }
3174
3175   if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3176     return false;
3177
3178   bfd_release (abfd, hashtable);
3179
3180   /* Now write the strings.  */
3181   bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3182   if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3183     return false;
3184   for (i = 0; i < orl_count; i++)
3185     {
3186       bfd_size_type len;
3187
3188       len = strlen (*map[i].name) + 1;
3189       if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3190         return false;
3191     }
3192
3193   /* The spec sez this should be a newline.  But in order to be
3194      bug-compatible for DECstation ar we use a null.  */
3195   if (padit)
3196     {
3197       if (bfd_write ("", 1, 1, abfd) != 1)
3198         return false;
3199     }
3200
3201   return true;
3202 }
3203
3204 /* See whether this BFD is an archive.  If it is, read in the armap
3205    and the extended name table.  */
3206
3207 bfd_target *
3208 ecoff_archive_p (abfd)
3209      bfd *abfd;
3210 {
3211   char armag[SARMAG + 1];
3212
3213   if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3214       || strncmp (armag, ARMAG, SARMAG) != 0)
3215     {
3216       bfd_error = wrong_format;
3217       return (bfd_target *) NULL;
3218     }
3219
3220   /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3221      involves a cast, we can't do it as the left operand of
3222      assignment.  */
3223   abfd->tdata.aout_ar_data =
3224     (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3225
3226   if (bfd_ardata (abfd) == (struct artdata *) NULL)
3227     {
3228       bfd_error = no_memory;
3229       return (bfd_target *) NULL;
3230     }
3231
3232   bfd_ardata (abfd)->first_file_filepos = SARMAG;
3233   bfd_ardata (abfd)->cache = NULL;
3234   bfd_ardata (abfd)->archive_head = NULL;
3235   bfd_ardata (abfd)->symdefs = NULL;
3236   bfd_ardata (abfd)->extended_names = NULL;
3237   bfd_ardata (abfd)->tdata = NULL;
3238   
3239   if (ecoff_slurp_armap (abfd) == false
3240       || ecoff_slurp_extended_name_table (abfd) == false)
3241     {
3242       bfd_release (abfd, bfd_ardata (abfd));
3243       abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3244       return (bfd_target *) NULL;
3245     }
3246   
3247   return abfd->xvec;
3248 }