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