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