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