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