1 /* BFD back-end for MIPS Extended-Coff files.
2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 #include "aout/ranlib.h"
29 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
30 some other stuff which we don't want and which conflicts with stuff
33 #include "aout/aout64.h"
38 #undef obj_sym_filepos
40 #include "coff/mips.h"
41 #include "coff/internal.h"
43 #include "coff/symconst.h"
44 #include "coff/ecoff-ext.h"
48 /* Each canonical asymbol really looks like this. */
50 typedef struct ecoff_symbol_struct
52 /* The actual symbol which the rest of BFD works with */
55 /* The fdr for this symbol. */
58 /* true if this is a local symbol rather than an external one. */
61 /* A pointer to the unswapped hidden information for this symbol */
64 struct sym_ext *lnative;
65 struct ext_ext *enative;
70 /* We take the address of the first element of a asymbol to ensure that the
71 macro is only ever applied to an asymbol. */
72 #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
74 /* The page boundary used to align sections in the executable file. */
75 #define ROUND_SIZE 0x1000
77 /* The linker needs a section to hold small common variables while
78 linking. There is no convenient way to create it when the linker
79 needs it, so we always create one for each BFD. We then avoid
81 #define SCOMMON ".scommon"
83 /* MIPS ECOFF has COFF sections, but the debugging information is
84 stored in a completely different format. This files uses the some
85 of the swapping routines from coffswap.h, and some of the generic
86 COFF routines in coffgen.c, but, unlike the real COFF targets, does
87 not use coffcode.h itself. */
89 /* Prototypes for static functions. */
91 static boolean ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
92 static asection *ecoff_make_section_hook PARAMS ((bfd *abfd, char *name));
93 static boolean ecoff_new_section_hook PARAMS ((bfd *abfd, asection *section));
94 static boolean ecoff_mkobject PARAMS ((bfd *abfd));
95 static PTR ecoff_mkobject_hook PARAMS ((bfd *abfd, PTR filehdr, PTR aouthdr));
96 static boolean ecoff_set_arch_mach_hook PARAMS ((bfd *abfd, PTR filehdr));
97 static long ecoff_sec_to_styp_flags PARAMS ((CONST char *name,
99 static flagword ecoff_styp_to_sec_flags PARAMS ((bfd *abfd, PTR hdr));
100 static asymbol *ecoff_make_empty_symbol PARAMS ((bfd *abfd));
101 static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
102 asymbol *asym, int ext,
103 asymbol **indirect_ptr_ptr));
104 static boolean ecoff_slurp_symbol_table PARAMS ((bfd *abfd));
105 static unsigned int ecoff_get_symtab_upper_bound PARAMS ((bfd *abfd));
106 static unsigned int ecoff_get_symtab PARAMS ((bfd *abfd,
107 asymbol **alocation));
108 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
109 RNDXR *rndx, long isym,
111 static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
112 int indx, int bigendian));
113 static void ecoff_print_symbol PARAMS ((bfd *abfd, PTR filep,
115 bfd_print_symbol_type how));
116 static void ecoff_get_symbol_info PARAMS ((bfd *abfd,
119 static void ecoff_swap_reloc_in PARAMS ((bfd *abfd, RELOC *ext,
120 struct internal_reloc *intern));
121 static unsigned int ecoff_swap_reloc_out PARAMS ((bfd *abfd, PTR src,
123 static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
129 static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
135 static bfd_reloc_status_type ecoff_reflo_reloc PARAMS ((bfd *abfd,
141 static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
147 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
149 static unsigned int ecoff_canonicalize_reloc PARAMS ((bfd *abfd,
153 static CONST struct reloc_howto_struct *ecoff_bfd_reloc_type_lookup
154 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
155 static boolean ecoff_find_nearest_line PARAMS ((bfd *abfd,
159 CONST char **filename_ptr,
160 CONST char **fnname_ptr,
161 unsigned int *retline_ptr));
162 static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
163 static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
164 asection *output_section, PTR data,
165 boolean relocateable));
166 static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
167 asection *section, PTR data,
168 boolean relocateable));
169 static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
170 CONST char *string, boolean external));
171 static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
172 bfd_seclet_type *seclet,
174 boolean relocateable));
175 static boolean ecoff_bfd_seclet_link PARAMS ((bfd *abfd, PTR data,
176 boolean relocateable));
177 static boolean ecoff_set_arch_mach PARAMS ((bfd *abfd,
178 enum bfd_architecture arch,
179 unsigned long machine));
180 static int ecoff_sizeof_headers PARAMS ((bfd *abfd, boolean reloc));
181 static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
182 static boolean ecoff_set_section_contents PARAMS ((bfd *abfd,
186 bfd_size_type count));
187 static boolean ecoff_write_object_contents PARAMS ((bfd *abfd));
188 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
189 unsigned int *rehash,
192 static boolean ecoff_slurp_armap PARAMS ((bfd *abfd));
193 static boolean ecoff_write_armap PARAMS ((bfd *abfd, unsigned int elength,
195 unsigned int orl_count,
197 static bfd_target *ecoff_archive_p PARAMS ((bfd *abfd));
199 /* Get the generic COFF swapping routines, except for the reloc,
200 symbol, and lineno ones. Give them ecoff names. */
202 #define NO_COFF_RELOCS
203 #define NO_COFF_SYMBOLS
204 #define NO_COFF_LINENOS
205 #define coff_swap_filehdr_in ecoff_swap_filehdr_in
206 #define coff_swap_filehdr_out ecoff_swap_filehdr_out
207 #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
208 #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
209 #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
210 #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
211 #include "coffswap.h"
213 /* How to process the various relocs types. */
215 static reloc_howto_type ecoff_howto_table[] =
217 /* Reloc type 0 is ignored. The reloc reading code ensures that
218 this is a reference to the .abs section, which will cause
219 bfd_perform_relocation to do nothing. */
220 HOWTO (ECOFF_R_IGNORE, /* type */
222 0, /* size (0 = byte, 1 = short, 2 = long) */
223 8, /* bitsize (obsolete) */
224 false, /* pc_relative */
226 false, /* absolute (obsolete) */
227 false, /* complain_on_overflow */
228 0, /* special_function */
230 false, /* partial_inplace */
233 false), /* pcrel_offset */
235 /* A 16 bit reference to a symbol, normally from a data section. */
236 HOWTO (ECOFF_R_REFHALF, /* type */
238 1, /* size (0 = byte, 1 = short, 2 = long) */
239 16, /* bitsize (obsolete) */
240 false, /* pc_relative */
242 false, /* absolute (obsolete) */
243 true, /* complain_on_overflow */
244 ecoff_generic_reloc, /* special_function */
245 "REFHALF", /* name */
246 true, /* partial_inplace */
247 0xffff, /* src_mask */
248 0xffff, /* dst_mask */
249 false), /* pcrel_offset */
251 /* A 32 bit reference to a symbol, normally from a data section. */
252 HOWTO (ECOFF_R_REFWORD, /* type */
254 2, /* size (0 = byte, 1 = short, 2 = long) */
255 32, /* bitsize (obsolete) */
256 false, /* pc_relative */
258 false, /* absolute (obsolete) */
259 true, /* complain_on_overflow */
260 ecoff_generic_reloc, /* special_function */
261 "REFWORD", /* name */
262 true, /* partial_inplace */
263 0xffffffff, /* src_mask */
264 0xffffffff, /* dst_mask */
265 false), /* pcrel_offset */
267 /* A 26 bit absolute jump address. */
268 HOWTO (ECOFF_R_JMPADDR, /* type */
270 2, /* size (0 = byte, 1 = short, 2 = long) */
271 32, /* bitsize (obsolete) */
272 false, /* pc_relative */
274 false, /* absolute (obsolete) */
275 true, /* complain_on_overflow */
276 ecoff_generic_reloc, /* special_function */
277 "JMPADDR", /* name */
278 true, /* partial_inplace */
279 0x3ffffff, /* src_mask */
280 0x3ffffff, /* dst_mask */
281 false), /* pcrel_offset */
283 /* The high 16 bits of a symbol value. Handled by the function
284 ecoff_refhi_reloc. */
285 HOWTO (ECOFF_R_REFHI, /* type */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
288 32, /* bitsize (obsolete) */
289 false, /* pc_relative */
291 false, /* absolute (obsolete) */
292 true, /* complain_on_overflow */
293 ecoff_refhi_reloc, /* special_function */
295 true, /* partial_inplace */
296 0xffff, /* src_mask */
297 0xffff, /* dst_mask */
298 false), /* pcrel_offset */
300 /* The low 16 bits of a symbol value. */
301 HOWTO (ECOFF_R_REFLO, /* type */
303 2, /* size (0 = byte, 1 = short, 2 = long) */
304 32, /* bitsize (obsolete) */
305 false, /* pc_relative */
307 false, /* absolute (obsolete) */
308 true, /* complain_on_overflow */
309 ecoff_reflo_reloc, /* special_function */
311 true, /* partial_inplace */
312 0xffff, /* src_mask */
313 0xffff, /* dst_mask */
314 false), /* pcrel_offset */
316 /* A reference to an offset from the gp register. Handled by the
317 function ecoff_gprel_reloc. */
318 HOWTO (ECOFF_R_GPREL, /* type */
320 2, /* size (0 = byte, 1 = short, 2 = long) */
321 32, /* bitsize (obsolete) */
322 false, /* pc_relative */
324 false, /* absolute (obsolete) */
325 true, /* complain_on_overflow */
326 ecoff_gprel_reloc, /* special_function */
328 true, /* partial_inplace */
329 0xffff, /* src_mask */
330 0xffff, /* dst_mask */
331 false), /* pcrel_offset */
333 /* A reference to a literal using an offset from the gp register.
334 Handled by the function ecoff_gprel_reloc. */
335 HOWTO (ECOFF_R_LITERAL, /* type */
337 2, /* size (0 = byte, 1 = short, 2 = long) */
338 32, /* bitsize (obsolete) */
339 false, /* pc_relative */
341 false, /* absolute (obsolete) */
342 true, /* complain_on_overflow */
343 ecoff_gprel_reloc, /* special_function */
344 "LITERAL", /* name */
345 true, /* partial_inplace */
346 0xffff, /* src_mask */
347 0xffff, /* dst_mask */
348 false) /* pcrel_offset */
351 #define ECOFF_HOWTO_COUNT \
352 (sizeof ecoff_howto_table / sizeof ecoff_howto_table[0])
354 /* This stuff is somewhat copied from coffcode.h. */
356 static asection bfd_debug_section = { "*DEBUG*" };
358 /* See whether the magic number matches. */
361 ecoff_bad_format_hook (abfd, filehdr)
365 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
367 if (ECOFFBADMAG (*internal_f))
373 /* This is a hook needed by SCO COFF, but we have nothing to do. */
376 ecoff_make_section_hook (abfd, name)
380 return (asection *) NULL;
383 /* Initialize a new section. */
386 ecoff_new_section_hook (abfd, section)
390 section->alignment_power = abfd->xvec->align_power_min;
392 if (strcmp (section->name, _TEXT) == 0)
393 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
394 else if (strcmp (section->name, _DATA) == 0
395 || strcmp (section->name, _SDATA) == 0)
396 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
397 else if (strcmp (section->name, _RDATA) == 0
398 || strcmp (section->name, _LIT8) == 0
399 || strcmp (section->name, _LIT4) == 0)
400 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
401 else if (strcmp (section->name, _BSS) == 0
402 || strcmp (section->name, _SBSS) == 0)
403 section->flags |= SEC_ALLOC;
405 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
406 uncertain about .init on some systems and I don't know how shared
412 /* Set the alignment of a section; we have nothing to do. */
414 #define ecoff_set_alignment_hook \
415 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
417 /* Create an ECOFF object. */
420 ecoff_mkobject (abfd)
423 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
424 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
425 if (abfd->tdata.ecoff_obj_data == NULL)
427 bfd_error = no_memory;
431 /* Always create a .scommon section for every BFD. This is a hack so
432 that the linker has something to attach scSCommon symbols to. */
433 bfd_make_section (abfd, SCOMMON);
438 /* Create the ECOFF backend specific information. */
441 ecoff_mkobject_hook (abfd, filehdr, aouthdr)
446 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
447 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
448 ecoff_data_type *ecoff;
450 if (ecoff_mkobject (abfd) == false)
453 ecoff = ecoff_data (abfd);
455 ecoff->sym_filepos = internal_f->f_symptr;
457 if (internal_a != (struct internal_aouthdr *) NULL)
461 ecoff->text_start = internal_a->text_start;
462 ecoff->text_end = internal_a->text_start + internal_a->tsize;
463 ecoff->gp = internal_a->gp_value;
464 ecoff->gprmask = internal_a->gprmask;
465 for (i = 0; i < 4; i++)
466 ecoff->cprmask[i] = internal_a->cprmask[i];
467 if (internal_a->magic == ZMAGIC)
468 abfd->flags |= D_PAGED;
474 /* Determine the machine architecture and type. */
477 ecoff_set_arch_mach_hook (abfd, filehdr)
481 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
482 enum bfd_architecture arch;
484 switch (internal_f->f_magic)
487 case MIPS_MAGIC_LITTLE:
489 arch = bfd_arch_mips;
493 arch = bfd_arch_obscure;
497 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
502 /* Get the section s_flags to use for a section. */
505 ecoff_sec_to_styp_flags (name, flags)
513 if (strcmp (name, _TEXT) == 0)
515 else if (strcmp (name, _DATA) == 0)
517 else if (strcmp (name, _SDATA) == 0)
519 else if (strcmp (name, _RDATA) == 0)
521 else if (strcmp (name, _LIT8) == 0)
523 else if (strcmp (name, _LIT4) == 0)
525 else if (strcmp (name, _BSS) == 0)
527 else if (strcmp (name, _SBSS) == 0)
529 else if (flags & SEC_CODE)
531 else if (flags & SEC_DATA)
533 else if (flags & SEC_READONLY)
535 else if (flags & SEC_LOAD)
540 if (flags & SEC_NEVER_LOAD)
546 /* Get the BFD flags to use for a section. */
549 ecoff_styp_to_sec_flags (abfd, hdr)
553 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
554 long styp_flags = internal_s->s_flags;
555 flagword sec_flags=0;
557 if (styp_flags & STYP_NOLOAD)
558 sec_flags |= SEC_NEVER_LOAD;
560 /* For 386 COFF, at least, an unloadable text or data section is
561 actually a shared library section. */
562 if (styp_flags & STYP_TEXT)
564 if (sec_flags & SEC_NEVER_LOAD)
565 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
567 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
569 else if ((styp_flags & STYP_DATA)
570 || (styp_flags & STYP_RDATA)
571 || (styp_flags & STYP_SDATA))
573 if (sec_flags & SEC_NEVER_LOAD)
574 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
576 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
577 if (styp_flags & STYP_RDATA)
578 sec_flags |= SEC_READONLY;
580 else if ((styp_flags & STYP_BSS)
581 || (styp_flags & STYP_SBSS))
583 sec_flags |= SEC_ALLOC;
585 else if (styp_flags & STYP_INFO)
587 sec_flags |= SEC_NEVER_LOAD;
589 else if ((styp_flags & STYP_LIT8)
590 || (styp_flags & STYP_LIT4))
592 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
596 sec_flags |= SEC_ALLOC | SEC_LOAD;
602 /* Read in and swap the important symbolic information for an ECOFF
603 object file. FIXME: This is called by gdb. If there is ever
604 another ECOFF target, it should be moved into some sort of target
605 specific structure. */
608 ecoff_slurp_symbolic_info (abfd)
611 struct hdr_ext external_symhdr;
612 HDRR *internal_symhdr;
613 bfd_size_type raw_base;
614 bfd_size_type raw_size;
616 struct fdr_ext *fraw_src;
617 struct fdr_ext *fraw_end;
620 /* Check whether we've already gotten it, and whether there's any to
622 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
624 if (ecoff_data (abfd)->sym_filepos == 0)
626 bfd_get_symcount (abfd) = 0;
630 /* At this point bfd_get_symcount (abfd) holds the number of symbols
631 as read from the file header, but on ECOFF this is always the
632 size of the symbolic information header. It would be cleaner to
633 handle this when we first read the file in coffgen.c. */
634 if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
636 bfd_error = bad_value;
640 /* Read the symbolic information header. */
641 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
642 || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
643 != sizeof (external_symhdr)))
645 bfd_error = system_call_error;
648 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
649 ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
651 if (internal_symhdr->magic != magicSym)
653 bfd_error = bad_value;
657 /* Now we can get the correct number of symbols. */
658 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
659 + internal_symhdr->iextMax);
661 /* Read all the symbolic information at once. */
662 raw_base = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
664 if (internal_symhdr->cbExtOffset != 0)
665 raw_size = (internal_symhdr->cbExtOffset
667 + internal_symhdr->iextMax * sizeof (struct ext_ext));
670 long cbline, issmax, issextmax;
672 cbline = (internal_symhdr->cbLine + 3) &~ 3;
673 issmax = (internal_symhdr->issMax + 3) &~ 3;
674 issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
675 raw_size = (cbline * sizeof (unsigned char)
676 + internal_symhdr->idnMax * sizeof (struct dnr_ext)
677 + internal_symhdr->ipdMax * sizeof (struct pdr_ext)
678 + internal_symhdr->isymMax * sizeof (struct sym_ext)
679 + internal_symhdr->ioptMax * sizeof (struct opt_ext)
680 + internal_symhdr->iauxMax * sizeof (union aux_ext)
681 + issmax * sizeof (char)
682 + issextmax * sizeof (char)
683 + internal_symhdr->ifdMax * sizeof (struct fdr_ext)
684 + internal_symhdr->crfd * sizeof (struct rfd_ext)
685 + internal_symhdr->iextMax * sizeof (struct ext_ext));
690 ecoff_data (abfd)->sym_filepos = 0;
693 raw = (PTR) bfd_alloc (abfd, raw_size);
696 bfd_error = no_memory;
699 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
701 bfd_error = system_call_error;
702 bfd_release (abfd, raw);
706 ecoff_data (abfd)->raw_size = raw_size;
707 ecoff_data (abfd)->raw_syments = raw;
709 /* Get pointers for the numeric offsets in the HDRR structure. */
710 #define FIX(off1, off2, type) \
711 if (internal_symhdr->off1 == 0) \
712 ecoff_data (abfd)->off2 = (type *) NULL; \
714 ecoff_data (abfd)->off2 = (type *) ((char *) raw \
715 + internal_symhdr->off1 \
717 FIX (cbLineOffset, line, unsigned char);
718 FIX (cbDnOffset, external_dnr, struct dnr_ext);
719 FIX (cbPdOffset, external_pdr, struct pdr_ext);
720 FIX (cbSymOffset, external_sym, struct sym_ext);
721 FIX (cbOptOffset, external_opt, struct opt_ext);
722 FIX (cbAuxOffset, external_aux, union aux_ext);
723 FIX (cbSsOffset, ss, char);
724 FIX (cbSsExtOffset, ssext, char);
725 FIX (cbFdOffset, external_fdr, struct fdr_ext);
726 FIX (cbRfdOffset, external_rfd, struct rfd_ext);
727 FIX (cbExtOffset, external_ext, struct ext_ext);
730 /* I don't want to always swap all the data, because it will just
731 waste time and most programs will never look at it. The only
732 time the linker needs most of the debugging information swapped
733 is when linking big-endian and little-endian MIPS object files
734 together, which is not a common occurrence.
736 We need to look at the fdr to deal with a lot of information in
737 the symbols, so we swap them here. */
738 ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
739 (internal_symhdr->ifdMax *
740 sizeof (struct fdr)));
741 if (ecoff_data (abfd)->fdr == NULL)
743 bfd_error = no_memory;
746 fdr_ptr = ecoff_data (abfd)->fdr;
747 fraw_src = ecoff_data (abfd)->external_fdr;
748 fraw_end = fraw_src + internal_symhdr->ifdMax;
749 for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
750 ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
755 /* ECOFF symbol table routines. The ECOFF symbol table is described
756 in gcc/mips-tfile.c. */
758 /* ECOFF uses two common sections. One is the usual one, and the
759 other is for small objects. All the small objects are kept
760 together, and then referenced via the gp pointer, which yields
761 faster assembler code. This is what we use for the small common
763 static asection ecoff_scom_section;
764 static asymbol ecoff_scom_symbol;
765 static asymbol *ecoff_scom_symbol_ptr;
767 /* Create an empty symbol. */
770 ecoff_make_empty_symbol (abfd)
773 ecoff_symbol_type *new;
775 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
776 if (new == (ecoff_symbol_type *) NULL)
778 bfd_error = no_memory;
779 return (asymbol *) NULL;
781 memset (new, 0, sizeof *new);
782 new->symbol.section = (asection *) NULL;
783 new->fdr = (FDR *) NULL;
785 new->native.lnative = (struct sym_ext *) NULL;
786 new->symbol.the_bfd = abfd;
790 /* Set the BFD flags and section for an ECOFF symbol. */
793 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, indirect_ptr_ptr)
798 asymbol **indirect_ptr_ptr;
800 asym->the_bfd = abfd;
801 asym->value = ecoff_sym->value;
802 asym->section = &bfd_debug_section;
805 /* An indirect symbol requires two consecutive stabs symbols. */
806 if (*indirect_ptr_ptr != (asymbol *) NULL)
808 BFD_ASSERT (MIPS_IS_STAB (ecoff_sym));
809 (*indirect_ptr_ptr)->value = (bfd_vma) asym;
810 asym->flags = BSF_DEBUGGING;
811 asym->section = &bfd_und_section;
812 *indirect_ptr_ptr = NULL;
816 if (MIPS_IS_STAB (ecoff_sym)
817 && (MIPS_UNMARK_STAB (ecoff_sym->index) | N_EXT) == (N_INDR | N_EXT))
819 asym->flags = BSF_DEBUGGING | BSF_INDIRECT;
820 asym->section = &bfd_ind_section;
821 /* Pass this symbol on to the next call to this function. */
822 *indirect_ptr_ptr = asym;
826 /* Most symbol types are just for debugging. */
827 switch (ecoff_sym->st)
836 if (MIPS_IS_STAB (ecoff_sym))
838 asym->flags = BSF_DEBUGGING;
843 asym->flags = BSF_DEBUGGING;
848 asym->flags = BSF_EXPORT | BSF_GLOBAL;
850 asym->flags = BSF_LOCAL;
851 switch (ecoff_sym->sc)
854 /* Used for compiler generated labels. Leave them in the
855 debugging section, and mark them as local. If BSF_DEBUGGING
856 is set, then nm does not display them for some reason. If no
857 flags are set then the linker whines about them. */
858 asym->flags = BSF_LOCAL;
861 asym->section = bfd_make_section_old_way (abfd, ".text");
862 asym->value -= asym->section->vma;
865 asym->section = bfd_make_section_old_way (abfd, ".data");
866 asym->value -= asym->section->vma;
871 asym->section = &bfd_com_section;
876 asym->section = bfd_make_section_old_way (abfd, ".bss");
877 asym->value -= asym->section->vma;
881 asym->flags = BSF_DEBUGGING;
884 asym->section = &bfd_abs_section;
887 asym->section = &bfd_und_section;
897 asym->flags = BSF_DEBUGGING;
900 asym->section = bfd_make_section_old_way (abfd, ".sdata");
901 asym->value -= asym->section->vma;
904 asym->section = bfd_make_section_old_way (abfd, ".sbss");
906 asym->value -= asym->section->vma;
909 asym->section = bfd_make_section_old_way (abfd, ".rdata");
910 asym->value -= asym->section->vma;
913 asym->flags = BSF_DEBUGGING;
916 if (asym->value > ecoff_data (abfd)->gp_size)
918 asym->section = &bfd_com_section;
924 if (ecoff_scom_section.name == NULL)
926 /* Initialize the small common section. */
927 ecoff_scom_section.name = SCOMMON;
928 ecoff_scom_section.flags = SEC_IS_COMMON;
929 ecoff_scom_section.output_section = &ecoff_scom_section;
930 ecoff_scom_section.symbol = &ecoff_scom_symbol;
931 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
932 ecoff_scom_symbol.name = SCOMMON;
933 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
934 ecoff_scom_symbol.section = &ecoff_scom_section;
935 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
937 asym->section = &ecoff_scom_section;
942 asym->flags = BSF_DEBUGGING;
945 asym->section = &bfd_und_section;
950 asym->section = bfd_make_section_old_way (abfd, ".init");
951 asym->value -= asym->section->vma;
956 asym->flags = BSF_DEBUGGING;
959 asym->section = bfd_make_section_old_way (abfd, ".fini");
960 asym->value -= asym->section->vma;
966 /* Look for special constructors symbols and make relocation entries
967 in a special construction section. These are produced by the
968 -fgnu-linker argument to g++. */
969 if (MIPS_IS_STAB (ecoff_sym))
971 switch (MIPS_UNMARK_STAB (ecoff_sym->index))
983 arelent_chain *reloc_chain;
985 /* Get a section with the same name as the symbol (usually
986 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
987 name ___CTOR_LIST (three underscores). We need
988 __CTOR_LIST (two underscores), since ECOFF doesn't use
989 a leading underscore. This should be handled by gcc,
990 but instead we do it here. Actually, this should all
991 be done differently anyhow. */
992 name = bfd_asymbol_name (asym);
993 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
998 section = bfd_get_section_by_name (abfd, name);
999 if (section == (asection *) NULL)
1003 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
1004 strcpy (copy, name);
1005 section = bfd_make_section (abfd, copy);
1008 /* Build a reloc pointing to this constructor. */
1009 reloc_chain = (arelent_chain *) bfd_alloc (abfd,
1010 sizeof (arelent_chain));
1011 reloc_chain->relent.sym_ptr_ptr =
1012 bfd_get_section (asym)->symbol_ptr_ptr;
1013 reloc_chain->relent.address = section->_raw_size;
1014 reloc_chain->relent.addend = asym->value;
1016 /* FIXME: Assumes 32 bit __CTOR_LIST__ entries. */
1017 reloc_chain->relent.howto = ecoff_howto_table + ECOFF_R_REFWORD;
1019 /* Set up the constructor section to hold the reloc. */
1020 section->flags = SEC_CONSTRUCTOR;
1021 ++section->reloc_count;
1022 section->alignment_power = 4;
1023 reloc_chain->next = section->constructor_chain;
1024 section->constructor_chain = reloc_chain;
1026 /* FIXME: Assumes 32 bit __CTOR_LIST__ entries. */
1027 section->_raw_size += 4;
1029 /* Mark the symbol as a constructor. */
1030 asym->flags |= BSF_CONSTRUCTOR;
1037 /* Read an ECOFF symbol table. */
1040 ecoff_slurp_symbol_table (abfd)
1043 bfd_size_type internal_size;
1044 ecoff_symbol_type *internal;
1045 ecoff_symbol_type *internal_ptr;
1046 asymbol *indirect_ptr;
1047 struct ext_ext *eraw_src;
1048 struct ext_ext *eraw_end;
1052 /* If we've already read in the symbol table, do nothing. */
1053 if (ecoff_data (abfd)->canonical_symbols != NULL)
1056 /* Get the symbolic information. */
1057 if (ecoff_slurp_symbolic_info (abfd) == false)
1059 if (bfd_get_symcount (abfd) == 0)
1062 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
1063 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
1064 if (internal == NULL)
1066 bfd_error = no_memory;
1070 internal_ptr = internal;
1071 indirect_ptr = NULL;
1072 eraw_src = ecoff_data (abfd)->external_ext;
1073 eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
1074 for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
1078 ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
1079 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
1080 + internal_esym.asym.iss);
1081 ecoff_set_symbol_info (abfd, &internal_esym.asym,
1082 &internal_ptr->symbol, 1, &indirect_ptr);
1083 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
1084 internal_ptr->local = false;
1085 internal_ptr->native.enative = eraw_src;
1087 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1089 /* The local symbols must be accessed via the fdr's, because the
1090 string and aux indices are relative to the fdr information. */
1091 fdr_ptr = ecoff_data (abfd)->fdr;
1092 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
1093 for (; fdr_ptr < fdr_end; fdr_ptr++)
1095 struct sym_ext *lraw_src;
1096 struct sym_ext *lraw_end;
1098 lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
1099 lraw_end = lraw_src + fdr_ptr->csym;
1100 for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
1104 ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
1105 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
1107 + internal_sym.iss);
1108 ecoff_set_symbol_info (abfd, &internal_sym,
1109 &internal_ptr->symbol, 0, &indirect_ptr);
1110 internal_ptr->fdr = fdr_ptr;
1111 internal_ptr->local = true;
1112 internal_ptr->native.lnative = lraw_src;
1115 BFD_ASSERT (indirect_ptr == (asymbol *) NULL);
1117 ecoff_data (abfd)->canonical_symbols = internal;
1123 ecoff_get_symtab_upper_bound (abfd)
1126 if (ecoff_slurp_symbolic_info (abfd) == false
1127 || bfd_get_symcount (abfd) == 0)
1130 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1134 ecoff_get_symtab (abfd, alocation)
1136 asymbol **alocation;
1138 unsigned int counter = 0;
1139 ecoff_symbol_type *symbase;
1140 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1142 if (ecoff_slurp_symbol_table (abfd) == false
1143 || bfd_get_symcount (abfd) == 0)
1146 symbase = ecoff_data (abfd)->canonical_symbols;
1147 while (counter < bfd_get_symcount (abfd))
1149 *(location++) = symbase++;
1152 *location++ = (ecoff_symbol_type *) NULL;
1153 return bfd_get_symcount (abfd);
1156 /* Turn ECOFF type information into a printable string.
1157 ecoff_emit_aggregate and ecoff_type_to_string are from
1158 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1160 /* Write aggregate information to a string. */
1163 ecoff_emit_aggregate (abfd, string, rndx, isym, which)
1170 int ifd = rndx->rfd;
1171 int indx = rndx->index;
1172 int sym_base, ss_base;
1178 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
1179 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
1181 if (indx == indexNil)
1182 name = "/* no name */";
1188 ecoff_swap_sym_in (abfd,
1189 ecoff_data (abfd)->external_sym + indx,
1191 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
1195 "%s %s { ifd = %d, index = %d }",
1197 indx + ecoff_data (abfd)->symbolic_header.iextMax);
1200 /* Convert the type information to string format. */
1203 ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1205 union aux_ext *aux_ptr;
1217 unsigned int basic_type;
1219 static char buffer1[1024];
1220 static char buffer2[1024];
1225 for (i = 0; i < 7; i++)
1227 qualifiers[i].low_bound = 0;
1228 qualifiers[i].high_bound = 0;
1229 qualifiers[i].stride = 0;
1232 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1233 return "-1 (no type)";
1234 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1236 basic_type = u.ti.bt;
1237 qualifiers[0].type = u.ti.tq0;
1238 qualifiers[1].type = u.ti.tq1;
1239 qualifiers[2].type = u.ti.tq2;
1240 qualifiers[3].type = u.ti.tq3;
1241 qualifiers[4].type = u.ti.tq4;
1242 qualifiers[5].type = u.ti.tq5;
1243 qualifiers[6].type = tqNil;
1246 * Go get the basic type.
1250 case btNil: /* undefined */
1254 case btAdr: /* address - integer same size as pointer */
1255 strcpy (p1, "address");
1258 case btChar: /* character */
1259 strcpy (p1, "char");
1262 case btUChar: /* unsigned character */
1263 strcpy (p1, "unsigned char");
1266 case btShort: /* short */
1267 strcpy (p1, "short");
1270 case btUShort: /* unsigned short */
1271 strcpy (p1, "unsigned short");
1274 case btInt: /* int */
1278 case btUInt: /* unsigned int */
1279 strcpy (p1, "unsigned int");
1282 case btLong: /* long */
1283 strcpy (p1, "long");
1286 case btULong: /* unsigned long */
1287 strcpy (p1, "unsigned long");
1290 case btFloat: /* float (real) */
1291 strcpy (p1, "float");
1294 case btDouble: /* Double (real) */
1295 strcpy (p1, "double");
1298 /* Structures add 1-2 aux words:
1299 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1300 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1302 case btStruct: /* Structure (Record) */
1303 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1304 ecoff_emit_aggregate (abfd, p1, &rndx,
1305 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1307 indx++; /* skip aux words */
1310 /* Unions add 1-2 aux words:
1311 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1312 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1314 case btUnion: /* Union */
1315 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1316 ecoff_emit_aggregate (abfd, p1, &rndx,
1317 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1319 indx++; /* skip aux words */
1322 /* Enumerations add 1-2 aux words:
1323 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1324 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1326 case btEnum: /* Enumeration */
1327 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1328 ecoff_emit_aggregate (abfd, p1, &rndx,
1329 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1331 indx++; /* skip aux words */
1334 case btTypedef: /* defined via a typedef, isymRef points */
1335 strcpy (p1, "typedef");
1338 case btRange: /* subrange of int */
1339 strcpy (p1, "subrange");
1342 case btSet: /* pascal sets */
1346 case btComplex: /* fortran complex */
1347 strcpy (p1, "complex");
1350 case btDComplex: /* fortran double complex */
1351 strcpy (p1, "double complex");
1354 case btIndirect: /* forward or unnamed typedef */
1355 strcpy (p1, "forward/unamed typedef");
1358 case btFixedDec: /* Fixed Decimal */
1359 strcpy (p1, "fixed decimal");
1362 case btFloatDec: /* Float Decimal */
1363 strcpy (p1, "float decimal");
1366 case btString: /* Varying Length Character String */
1367 strcpy (p1, "string");
1370 case btBit: /* Aligned Bit String */
1374 case btPicture: /* Picture */
1375 strcpy (p1, "picture");
1378 case btVoid: /* Void */
1379 strcpy (p1, "void");
1383 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1387 p1 += strlen (buffer1);
1390 * If this is a bitfield, get the bitsize.
1396 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1397 sprintf (p1, " : %d", bitsize);
1398 p1 += strlen (buffer1);
1403 * Deal with any qualifiers.
1405 if (qualifiers[0].type != tqNil)
1408 * Snarf up any array bounds in the correct order. Arrays
1409 * store 5 successive words in the aux. table:
1410 * word 0 RNDXR to type of the bounds (ie, int)
1411 * word 1 Current file descriptor index
1413 * word 3 high bound (or -1 if [])
1414 * word 4 stride size in bits
1416 for (i = 0; i < 7; i++)
1418 if (qualifiers[i].type == tqArray)
1420 qualifiers[i].low_bound =
1421 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1422 qualifiers[i].high_bound =
1423 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1424 qualifiers[i].stride =
1425 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1431 * Now print out the qualifiers.
1433 for (i = 0; i < 6; i++)
1435 switch (qualifiers[i].type)
1442 strcpy (p2, "ptr to ");
1443 p2 += sizeof ("ptr to ")-1;
1447 strcpy (p2, "volatile ");
1448 p2 += sizeof ("volatile ")-1;
1452 strcpy (p2, "far ");
1453 p2 += sizeof ("far ")-1;
1457 strcpy (p2, "func. ret. ");
1458 p2 += sizeof ("func. ret. ");
1463 int first_array = i;
1466 /* Print array bounds reversed (ie, in the order the C
1467 programmer writes them). C is such a fun language.... */
1469 while (i < 5 && qualifiers[i+1].type == tqArray)
1472 for (j = i; j >= first_array; j--)
1474 strcpy (p2, "array [");
1475 p2 += sizeof ("array [")-1;
1476 if (qualifiers[j].low_bound != 0)
1478 "%ld:%ld {%ld bits}",
1479 (long) qualifiers[j].low_bound,
1480 (long) qualifiers[j].high_bound,
1481 (long) qualifiers[j].stride);
1483 else if (qualifiers[j].high_bound != -1)
1486 (long) (qualifiers[j].high_bound + 1),
1487 (long) (qualifiers[j].stride));
1490 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1493 strcpy (p2, "] of ");
1494 p2 += sizeof ("] of ")-1;
1502 strcpy (p2, buffer1);
1506 /* Return information about ECOFF symbol SYMBOL in RET. */
1509 ecoff_get_symbol_info (abfd, symbol, ret)
1510 bfd *abfd; /* Ignored. */
1514 bfd_symbol_info (symbol, ret);
1517 /* Print information about an ECOFF symbol. */
1520 ecoff_print_symbol (abfd, filep, symbol, how)
1524 bfd_print_symbol_type how;
1526 FILE *file = (FILE *)filep;
1530 case bfd_print_symbol_name:
1531 fprintf (file, "%s", symbol->name);
1533 case bfd_print_symbol_more:
1534 if (ecoffsymbol (symbol)->local)
1538 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1540 fprintf (file, "ecoff local %lx %x %x",
1541 (unsigned long) ecoff_sym.value,
1542 (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
1548 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1550 fprintf (file, "ecoff extern %lx %x %x",
1551 (unsigned long) ecoff_ext.asym.value,
1552 (unsigned) ecoff_ext.asym.st,
1553 (unsigned) ecoff_ext.asym.sc);
1556 case bfd_print_symbol_all:
1557 /* Print out the symbols in a reasonable way */
1566 if (ecoffsymbol (symbol)->local)
1568 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1571 pos = (ecoffsymbol (symbol)->native.lnative
1572 - ecoff_data (abfd)->external_sym
1573 + ecoff_data (abfd)->symbolic_header.iextMax);
1580 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1583 pos = (ecoffsymbol (symbol)->native.enative
1584 - ecoff_data (abfd)->external_ext);
1585 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1586 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1587 weakext = ecoff_ext.weakext ? 'w' : ' ';
1590 fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
1591 pos, type, (unsigned long) ecoff_ext.asym.value,
1592 (unsigned) ecoff_ext.asym.st,
1593 (unsigned) ecoff_ext.asym.sc,
1594 (unsigned) ecoff_ext.asym.index,
1595 jmptbl, cobol_main, weakext,
1598 if (ecoffsymbol (symbol)->fdr != NULL
1599 && ecoff_ext.asym.index != indexNil)
1604 union aux_ext *aux_base;
1606 indx = ecoff_ext.asym.index;
1608 /* sym_base is used to map the fdr relative indices which
1609 appear in the file to the position number which we are
1611 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1612 if (ecoffsymbol (symbol)->local)
1613 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1615 /* aux_base is the start of the aux entries for this file;
1616 asym.index is an offset from this. */
1617 aux_base = (ecoff_data (abfd)->external_aux
1618 + ecoffsymbol (symbol)->fdr->iauxBase);
1620 /* The aux entries are stored in host byte order; the
1621 order is indicated by a bit in the fdr. */
1622 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1624 /* This switch is basically from gcc/mips-tdump.c */
1625 switch (ecoff_ext.asym.st)
1633 printf ("\n End+1 symbol: %ld", indx + sym_base);
1637 if (ecoff_ext.asym.sc == scText
1638 || ecoff_ext.asym.sc == scInfo)
1639 printf ("\n First symbol: %ld", indx + sym_base);
1641 printf ("\n First symbol: %ld",
1642 (long) (AUX_GET_ISYM (bigendian,
1643 &aux_base[ecoff_ext.asym.index])
1649 if (MIPS_IS_STAB (&ecoff_ext.asym))
1651 else if (ecoffsymbol (symbol)->local)
1652 printf ("\n End+1 symbol: %-7ld Type: %s",
1653 (long) (AUX_GET_ISYM (bigendian,
1654 &aux_base[ecoff_ext.asym.index])
1656 ecoff_type_to_string (abfd, aux_base, indx + 1,
1659 printf ("\n Local symbol: %d",
1662 + ecoff_data (abfd)->symbolic_header.iextMax));
1666 if (!MIPS_IS_STAB (&ecoff_ext.asym))
1667 printf ("\n Type: %s",
1668 ecoff_type_to_string (abfd, aux_base, indx,
1678 /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
1679 external form. They use a bit which indicates whether the symbol
1682 /* Swap a reloc in. */
1685 ecoff_swap_reloc_in (abfd, ext, intern)
1688 struct internal_reloc *intern;
1690 intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
1691 if (abfd->xvec->header_byteorder_big_p != false)
1693 intern->r_symndx = (((int) ext->r_bits[0]
1694 << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
1695 | ((int) ext->r_bits[1]
1696 << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
1697 | ((int) ext->r_bits[2]
1698 << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
1699 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
1700 >> RELOC_BITS3_TYPE_SH_BIG);
1701 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
1705 intern->r_symndx = (((int) ext->r_bits[0]
1706 << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
1707 | ((int) ext->r_bits[1]
1708 << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
1709 | ((int) ext->r_bits[2]
1710 << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
1711 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
1712 >> RELOC_BITS3_TYPE_SH_LITTLE);
1713 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
1717 /* Swap a reloc out. */
1720 ecoff_swap_reloc_out (abfd, src, dst)
1725 struct internal_reloc *intern = (struct internal_reloc *) src;
1726 RELOC *ext = (RELOC *) dst;
1728 bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
1729 if (abfd->xvec->header_byteorder_big_p != false)
1731 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
1732 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
1733 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
1734 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
1735 & RELOC_BITS3_TYPE_BIG)
1736 | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
1740 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
1741 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
1742 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
1743 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
1744 & RELOC_BITS3_TYPE_LITTLE)
1745 | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
1751 /* ECOFF relocs are either against external symbols, or against
1752 sections. If we are producing relocateable output, and the reloc
1753 is against an external symbol, and nothing has given us any
1754 additional addend, the resulting reloc will also be against the
1755 same symbol. In such a case, we don't want to change anything
1756 about the way the reloc is handled, since it will all be done at
1757 final link time. Rather than put special case code into
1758 bfd_perform_relocation, all the reloc types use this howto
1759 function. It just short circuits the reloc if producing
1760 relocateable output against an external symbol. */
1762 static bfd_reloc_status_type
1763 ecoff_generic_reloc (abfd,
1770 arelent *reloc_entry;
1773 asection *input_section;
1776 if (output_bfd != (bfd *) NULL
1777 && (symbol->flags & BSF_SECTION_SYM) == 0
1778 && reloc_entry->addend == 0)
1780 reloc_entry->address += input_section->output_offset;
1781 return bfd_reloc_ok;
1784 return bfd_reloc_continue;
1787 /* Do a REFHI relocation. This has to be done in combination with a
1788 REFLO reloc, because there is a carry from the REFLO to the REFHI.
1789 Here we just save the information we need; we do the actual
1790 relocation when we see the REFLO. ECOFF requires that the REFLO
1791 immediately follow the REFHI, so this ought to work. */
1793 static bfd_byte *ecoff_refhi_addr;
1794 static bfd_vma ecoff_refhi_addend;
1796 static bfd_reloc_status_type
1797 ecoff_refhi_reloc (abfd,
1804 arelent *reloc_entry;
1807 asection *input_section;
1810 bfd_reloc_status_type ret;
1813 /* If we're relocating, and this an external symbol, we don't want
1814 to change anything. */
1815 if (output_bfd != (bfd *) NULL
1816 && (symbol->flags & BSF_SECTION_SYM) == 0
1817 && reloc_entry->addend == 0)
1819 reloc_entry->address += input_section->output_offset;
1820 return bfd_reloc_ok;
1824 if (symbol->section == &bfd_und_section
1825 && output_bfd == (bfd *) NULL)
1826 ret = bfd_reloc_undefined;
1828 if (bfd_is_com_section (symbol->section))
1831 relocation = symbol->value;
1833 relocation += symbol->section->output_section->vma;
1834 relocation += symbol->section->output_offset;
1835 relocation += reloc_entry->addend;
1837 if (reloc_entry->address > input_section->_cooked_size)
1838 return bfd_reloc_outofrange;
1840 /* Save the information, and let REFLO do the actual relocation. */
1841 ecoff_refhi_addr = (bfd_byte *) data + reloc_entry->address;
1842 ecoff_refhi_addend = relocation;
1844 if (output_bfd != (bfd *) NULL)
1845 reloc_entry->address += input_section->output_offset;
1850 /* Do a REFLO relocation. This is a straightforward 16 bit inplace
1851 relocation; this function exists in order to do the REFHI
1852 relocation described above. */
1854 static bfd_reloc_status_type
1855 ecoff_reflo_reloc (abfd,
1862 arelent *reloc_entry;
1865 asection *input_section;
1868 if (ecoff_refhi_addr != (bfd_byte *) NULL)
1872 unsigned long vallo;
1874 /* Do the REFHI relocation. Note that we actually don't need to
1875 know anything about the REFLO itself, except where to find
1876 the low 16 bits of the addend needed by the REFHI. */
1877 insn = bfd_get_32 (abfd, ecoff_refhi_addr);
1878 vallo = (bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address)
1880 val = ((insn & 0xffff) << 16) + vallo;
1881 val += ecoff_refhi_addend;
1883 /* The low order 16 bits are always treated as a signed value.
1884 Therefore, a negative value in the low order bits requires an
1885 adjustment in the high order bits. We need to make this
1886 adjustment in two ways: once for the bits we took from the
1887 data, and once for the bits we are putting back in to the
1889 if ((vallo & 0x8000) != 0)
1891 if ((val & 0x8000) != 0)
1894 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1895 bfd_put_32 (abfd, insn, ecoff_refhi_addr);
1897 ecoff_refhi_addr = (bfd_byte *) NULL;
1900 /* Now do the REFLO reloc in the usual way. */
1901 return ecoff_generic_reloc (abfd, reloc_entry, symbol, data,
1902 input_section, output_bfd);
1905 /* Do a GPREL relocation. This is a 16 bit value which must become
1906 the offset from the gp register. */
1908 static bfd_reloc_status_type
1909 ecoff_gprel_reloc (abfd,
1916 arelent *reloc_entry;
1919 asection *input_section;
1922 boolean relocateable;
1927 /* If we're relocating, and this is an external symbol with no
1928 addend, we don't want to change anything. We will only have an
1929 addend if this is a newly created reloc, not read from an ECOFF
1931 if (output_bfd != (bfd *) NULL
1932 && (symbol->flags & BSF_SECTION_SYM) == 0
1933 && reloc_entry->addend == 0)
1935 reloc_entry->address += input_section->output_offset;
1936 return bfd_reloc_ok;
1939 if (output_bfd != (bfd *) NULL)
1940 relocateable = true;
1943 relocateable = false;
1944 output_bfd = symbol->section->output_section->owner;
1947 if (symbol->section == &bfd_und_section
1948 && relocateable == false)
1949 return bfd_reloc_undefined;
1951 /* We have to figure out the gp value, so that we can adjust the
1952 symbol value correctly. We look up the symbol _gp in the output
1953 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1954 target data. We don't need to adjust the symbol value for an
1955 external symbol if we are producing relocateable output. */
1956 if (ecoff_data (output_bfd)->gp == 0
1957 && (relocateable == false
1958 || (symbol->flags & BSF_SECTION_SYM) != 0))
1960 if (relocateable != false)
1962 /* Make up a value. */
1963 ecoff_data (output_bfd)->gp =
1964 symbol->section->output_section->vma + 0x4000;
1972 count = bfd_get_symcount (output_bfd);
1973 sym = bfd_get_outsymbols (output_bfd);
1975 /* We should do something more friendly here, but we don't
1976 have a good reloc status to return. */
1977 if (sym == (asymbol **) NULL)
1980 for (i = 0; i < count; i++, sym++)
1982 register CONST char *name;
1984 name = bfd_asymbol_name (*sym);
1985 if (*name == '_' && strcmp (name, "_gp") == 0)
1987 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1992 /* We should do something more friendly here, but we don't have
1993 a good reloc status to return. */
1999 if (bfd_is_com_section (symbol->section))
2002 relocation = symbol->value;
2004 relocation += symbol->section->output_section->vma;
2005 relocation += symbol->section->output_offset;
2007 if (reloc_entry->address > input_section->_cooked_size)
2008 return bfd_reloc_outofrange;
2010 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
2012 /* Set val to the offset into the section or symbol. */
2013 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
2017 /* Adjust val for the final section location and GP value. If we
2018 are producing relocateable output, we don't want to do this for
2019 an external symbol. */
2020 if (relocateable == false
2021 || (symbol->flags & BSF_SECTION_SYM) != 0)
2022 val += relocation - ecoff_data (output_bfd)->gp;
2024 insn = (insn &~ 0xffff) | (val & 0xffff);
2025 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
2027 if (relocateable != false)
2028 reloc_entry->address += input_section->output_offset;
2030 /* Make sure it fit in 16 bits. */
2031 if (val >= 0x8000 && val < 0xffff8000)
2032 return bfd_reloc_outofrange;
2034 return bfd_reloc_ok;
2037 /* Read in the relocs for a section. */
2040 ecoff_slurp_reloc_table (abfd, section, symbols)
2045 RELOC *external_relocs;
2046 arelent *internal_relocs;
2050 if (section->relocation != (arelent *) NULL
2051 || section->reloc_count == 0
2052 || (section->flags & SEC_CONSTRUCTOR) != 0)
2055 if (ecoff_slurp_symbol_table (abfd) == false)
2058 internal_relocs = (arelent *) bfd_alloc (abfd,
2060 * section->reloc_count));
2061 external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
2062 if (internal_relocs == (arelent *) NULL
2063 || external_relocs == (RELOC *) NULL)
2065 bfd_error = no_memory;
2068 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
2070 if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
2071 != RELSZ * section->reloc_count)
2073 bfd_error = system_call_error;
2077 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
2079 struct internal_reloc intern;
2081 ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
2083 if (intern.r_type > ECOFF_R_LITERAL)
2086 if (intern.r_extern)
2088 /* r_symndx is an index into the external symbols. */
2089 BFD_ASSERT (intern.r_symndx >= 0
2091 < ecoff_data (abfd)->symbolic_header.iextMax));
2092 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
2097 CONST char *sec_name;
2100 /* r_symndx is a section key. */
2101 switch (intern.r_symndx)
2103 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
2104 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
2105 case RELOC_SECTION_DATA: sec_name = ".data"; break;
2106 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
2107 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
2108 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
2109 case RELOC_SECTION_INIT: sec_name = ".init"; break;
2110 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
2111 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
2115 sec = bfd_get_section_by_name (abfd, sec_name);
2116 if (sec == (asection *) NULL)
2118 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
2120 rptr->addend = - bfd_get_section_vma (abfd, sec);
2121 if (intern.r_type == ECOFF_R_GPREL
2122 || intern.r_type == ECOFF_R_LITERAL)
2123 rptr->addend += ecoff_data (abfd)->gp;
2126 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
2127 rptr->howto = &ecoff_howto_table[intern.r_type];
2129 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
2130 to the absolute section so that the reloc is ignored. */
2131 if (intern.r_type == ECOFF_R_IGNORE)
2132 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2135 bfd_release (abfd, external_relocs);
2137 section->relocation = internal_relocs;
2142 /* Get a canonical list of relocs. */
2145 ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
2153 if (section->flags & SEC_CONSTRUCTOR)
2155 arelent_chain *chain;
2157 /* This section has relocs made up by us, not the file, so take
2158 them out of their chain and place them into the data area
2160 for (count = 0, chain = section->constructor_chain;
2161 count < section->reloc_count;
2162 count++, chain = chain->next)
2163 *relptr++ = &chain->relent;
2169 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
2172 tblptr = section->relocation;
2173 if (tblptr == (arelent *) NULL)
2176 for (count = 0; count < section->reloc_count; count++)
2177 *relptr++ = tblptr++;
2180 *relptr = (arelent *) NULL;
2182 return section->reloc_count;
2185 /* Get the howto structure for a generic reloc type. */
2187 static CONST struct reloc_howto_struct *
2188 ecoff_bfd_reloc_type_lookup (abfd, code)
2190 bfd_reloc_code_real_type code;
2197 ecoff_type = ECOFF_R_REFHALF;
2200 ecoff_type = ECOFF_R_REFWORD;
2202 case BFD_RELOC_MIPS_JMP:
2203 ecoff_type = ECOFF_R_JMPADDR;
2205 case BFD_RELOC_HI16_S:
2206 ecoff_type = ECOFF_R_REFHI;
2208 case BFD_RELOC_LO16:
2209 ecoff_type = ECOFF_R_REFLO;
2211 case BFD_RELOC_MIPS_GPREL:
2212 ecoff_type = ECOFF_R_GPREL;
2215 return (CONST struct reloc_howto_struct *) NULL;
2218 return &ecoff_howto_table[ecoff_type];
2221 /* Provided a BFD, a section and an offset into the section, calculate
2222 and return the name of the source file and the line nearest to the
2226 ecoff_find_nearest_line (abfd,
2235 asymbol **ignore_symbols;
2237 CONST char **filename_ptr;
2238 CONST char **functionname_ptr;
2239 unsigned int *retline_ptr;
2245 struct pdr_ext *pdr_ptr;
2246 struct pdr_ext *pdr_end;
2248 unsigned char *line_ptr;
2249 unsigned char *line_end;
2252 /* If we're not in the .text section, we don't have any line
2254 if (strcmp (section->name, _TEXT) != 0
2255 || offset < ecoff_data (abfd)->text_start
2256 || offset >= ecoff_data (abfd)->text_end)
2259 /* Make sure we have the FDR's. */
2260 if (ecoff_slurp_symbolic_info (abfd) == false
2261 || bfd_get_symcount (abfd) == 0)
2264 /* Each file descriptor (FDR) has a memory address. Here we track
2265 down which FDR we want. The FDR's are stored in increasing
2266 memory order. If speed is ever important, this can become a
2267 binary search. We must ignore FDR's with no PDR entries; they
2268 will have the adr of the FDR before or after them. */
2269 fdr_start = ecoff_data (abfd)->fdr;
2270 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
2271 fdr_hold = (FDR *) NULL;
2272 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2274 if (fdr_ptr->cpd == 0)
2276 if (offset < fdr_ptr->adr)
2280 if (fdr_hold == (FDR *) NULL)
2284 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2285 have an address, which is relative to the FDR address, and are
2286 also stored in increasing memory order. */
2287 offset -= fdr_ptr->adr;
2288 pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
2289 pdr_end = pdr_ptr + fdr_ptr->cpd;
2290 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2292 /* The address of the first PDR is an offset which applies to the
2293 addresses of all the PDR's. */
2296 for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
2298 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2299 if (offset < pdr.adr)
2303 /* Now we can look for the actual line number. The line numbers are
2304 stored in a very funky format, which I won't try to describe.
2305 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2306 we want; we need this to compute line_end. */
2307 line_end = ecoff_data (abfd)->line;
2308 if (pdr_ptr == pdr_end)
2309 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2311 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2313 /* Now change pdr and pdr_ptr to the one we want. */
2315 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2319 line_ptr = (ecoff_data (abfd)->line
2320 + fdr_ptr->cbLineOffset
2321 + pdr.cbLineOffset);
2322 while (line_ptr < line_end)
2327 delta = *line_ptr >> 4;
2330 count = (*line_ptr & 0xf) + 1;
2334 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2335 if (delta >= 0x8000)
2340 if (offset < count * 4)
2342 offset -= count * 4;
2345 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2346 at least according to gdb/mipsread.c. */
2347 if (fdr_ptr->rss == -1)
2349 *filename_ptr = NULL;
2351 *functionname_ptr = NULL;
2356 ecoff_swap_ext_in (abfd,
2357 (ecoff_data (abfd)->external_ext
2360 *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
2367 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
2368 ecoff_swap_sym_in (abfd,
2369 (ecoff_data (abfd)->external_sym
2373 *functionname_ptr = (ecoff_data (abfd)->ss
2377 *retline_ptr = lineno;
2381 /* We can't use the generic linking routines for ECOFF, because we
2382 have to handle all the debugging information. The generic link
2383 routine just works out the section contents and attaches a list of
2386 We link by looping over all the seclets. We make two passes. On
2387 the first we set the actual section contents and determine the size
2388 of the debugging information. On the second we accumulate the
2389 debugging information and write it out.
2391 This currently always accumulates the debugging information, which
2392 is incorrect, because it ignores the -s and -S options of the
2393 linker. The linker needs to be modified to give us that
2394 information in a more useful format (currently it just provides a
2395 list of symbols which should appear in the output file). */
2397 /* Clear the output_has_begun flag for all the input BFD's. We use it
2398 to avoid linking in the debugging information for a BFD more than
2402 ecoff_clear_output_flags (abfd)
2405 register asection *o;
2406 register bfd_seclet_type *p;
2408 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2409 for (p = o->seclets_head;
2410 p != (bfd_seclet_type *) NULL;
2412 if (p->type == bfd_indirect_seclet)
2413 p->u.indirect.section->owner->output_has_begun = false;
2416 /* Handle an indirect seclet on the first pass. Set the contents of
2417 the output section, and accumulate the debugging information if
2421 ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
2423 bfd_seclet_type *seclet;
2424 asection *output_section;
2426 boolean relocateable;
2429 HDRR *output_symhdr;
2432 if ((output_section->flags & SEC_HAS_CONTENTS)
2433 && !(output_section->flags & SEC_NEVER_LOAD)
2434 && (output_section->flags & SEC_LOAD)
2437 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2441 if (bfd_set_section_contents (output_bfd,
2452 input_bfd = seclet->u.indirect.section->owner;
2454 /* We want to figure out how much space will be required to
2455 incorporate all the debugging information from input_bfd. We use
2456 the output_has_begun field to avoid adding it in more than once.
2457 The actual incorporation is done in the second pass, in
2458 ecoff_get_debug. The code has to parallel that code in its
2459 manipulations of output_symhdr. */
2461 if (input_bfd->output_has_begun)
2463 input_bfd->output_has_begun = true;
2465 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2467 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2473 /* We just accumulate local symbols from a non-ECOFF BFD. The
2474 external symbols are handled separately. */
2476 symbols = (asymbol **) bfd_alloc (output_bfd,
2477 get_symtab_upper_bound (input_bfd));
2478 if (symbols == (asymbol **) NULL)
2480 bfd_error = no_memory;
2483 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2485 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2489 len = strlen ((*sym_ptr)->name);
2490 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2492 ++output_symhdr->isymMax;
2493 output_symhdr->issMax += len + 1;
2497 bfd_release (output_bfd, (PTR) symbols);
2499 ++output_symhdr->ifdMax;
2504 /* We simply add in the information from another ECOFF BFD. First
2505 we make sure we have the symbolic information. */
2506 if (ecoff_slurp_symbol_table (input_bfd) == false)
2508 if (bfd_get_symcount (input_bfd) == 0)
2511 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2513 /* Figure out how much information we are going to be putting in.
2514 The external symbols are handled separately. */
2515 output_symhdr->ilineMax += input_symhdr->ilineMax;
2516 output_symhdr->cbLine += input_symhdr->cbLine;
2517 output_symhdr->idnMax += input_symhdr->idnMax;
2518 output_symhdr->ipdMax += input_symhdr->ipdMax;
2519 output_symhdr->isymMax += input_symhdr->isymMax;
2520 output_symhdr->ioptMax += input_symhdr->ioptMax;
2521 output_symhdr->iauxMax += input_symhdr->iauxMax;
2522 output_symhdr->issMax += input_symhdr->issMax;
2523 output_symhdr->ifdMax += input_symhdr->ifdMax;
2525 /* The RFD's are special, since we create them if needed. */
2526 if (input_symhdr->crfd > 0)
2527 output_symhdr->crfd += input_symhdr->crfd;
2529 output_symhdr->crfd += input_symhdr->ifdMax;
2534 /* Handle an arbitrary seclet on the first pass. */
2537 ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
2539 bfd_seclet_type *seclet;
2542 boolean relocateable;
2544 switch (seclet->type)
2546 case bfd_indirect_seclet:
2547 /* The contents of this section come from another one somewhere
2549 return ecoff_rel (abfd, seclet, section, data, relocateable);
2551 case bfd_fill_seclet:
2552 /* Fill in the section with fill.value. This is used to pad out
2553 sections, but we must avoid padding the .bss section. */
2554 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2556 if (seclet->u.fill.value != 0)
2561 char *d = (char *) bfd_alloc (abfd, seclet->size);
2565 for (i = 0; i < seclet->size; i+=2)
2566 d[i] = seclet->u.fill.value >> 8;
2567 for (i = 1; i < seclet->size; i+=2)
2568 d[i] = seclet->u.fill.value;
2569 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2571 bfd_release (abfd, (PTR) d);
2583 /* Add a string to the debugging information we are accumulating for a
2584 file. Return the offset from the fdr string base or from the
2585 external string base. */
2588 ecoff_add_string (output_bfd, fdr, string, external)
2598 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2599 len = strlen (string);
2602 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2603 ret = symhdr->issExtMax;
2604 symhdr->issExtMax += len + 1;
2608 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2610 symhdr->issMax += len + 1;
2611 fdr->cbSs += len + 1;
2616 /* Accumulate the debugging information from an input section. */
2619 ecoff_get_debug (output_bfd, seclet, section, relocateable)
2621 bfd_seclet_type *seclet;
2623 boolean relocateable;
2626 HDRR *output_symhdr;
2628 ecoff_data_type *output_ecoff;
2629 ecoff_data_type *input_ecoff;
2631 struct sym_ext *sym_out;
2632 ecoff_symbol_type *esym_ptr;
2633 ecoff_symbol_type *esym_end;
2636 struct fdr_ext *fdr_out;
2638 input_bfd = seclet->u.indirect.section->owner;
2640 /* Don't get the information more than once. */
2641 if (input_bfd->output_has_begun)
2643 input_bfd->output_has_begun = true;
2645 output_ecoff = ecoff_data (output_bfd);
2646 output_symhdr = &output_ecoff->symbolic_header;
2648 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2655 /* This is not an ECOFF BFD. Just gather the symbols. */
2657 memset (&fdr, 0, sizeof fdr);
2659 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2660 fdr.issBase = output_symhdr->issMax;
2662 fdr.rss = ecoff_add_string (output_bfd,
2664 bfd_get_filename (input_bfd),
2666 fdr.isymBase = output_symhdr->isymMax;
2668 /* Get the local symbols from the input BFD. */
2669 symbols = (asymbol **) bfd_alloc (output_bfd,
2670 get_symtab_upper_bound (input_bfd));
2671 if (symbols == (asymbol **) NULL)
2673 bfd_error = no_memory;
2676 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2678 /* Handle the local symbols. Any external symbols are handled
2681 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2685 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2687 memset (&internal_sym, 0, sizeof internal_sym);
2688 internal_sym.iss = ecoff_add_string (output_bfd,
2693 if (bfd_is_com_section ((*sym_ptr)->section)
2694 || (*sym_ptr)->section == &bfd_und_section)
2695 internal_sym.value = (*sym_ptr)->value;
2697 internal_sym.value = ((*sym_ptr)->value
2698 + (*sym_ptr)->section->output_offset
2699 + (*sym_ptr)->section->output_section->vma);
2700 internal_sym.st = stNil;
2701 internal_sym.sc = scUndefined;
2702 internal_sym.index = indexNil;
2703 ecoff_swap_sym_out (output_bfd, &internal_sym,
2704 (output_ecoff->external_sym
2705 + output_symhdr->isymMax));
2707 ++output_symhdr->isymMax;
2710 bfd_release (output_bfd, (PTR) symbols);
2712 /* Leave everything else in the FDR zeroed out. This will cause
2713 the lang field to be langC. The fBigendian field will
2714 indicate little endian format, but it doesn't matter because
2715 it only applies to aux fields and there are none. */
2717 ecoff_swap_fdr_out (output_bfd, &fdr,
2718 (output_ecoff->external_fdr
2719 + output_symhdr->ifdMax));
2720 ++output_symhdr->ifdMax;
2724 /* This is an ECOFF BFD. We want to grab the information from
2725 input_bfd and attach it to output_bfd. */
2726 count = bfd_get_symcount (input_bfd);
2729 input_ecoff = ecoff_data (input_bfd);
2730 input_symhdr = &input_ecoff->symbolic_header;
2732 /* I think that it is more efficient to simply copy the debugging
2733 information from the input BFD to the output BFD. Because ECOFF
2734 uses relative pointers for most of the debugging information,
2735 only a little of it has to be changed at all. */
2737 /* Swap in the local symbols, adjust their values, and swap them out
2738 again. The external symbols are handled separately. */
2739 sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
2741 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2742 esym_end = esym_ptr + count;
2743 for (; esym_ptr < esym_end; esym_ptr++)
2745 if (esym_ptr->local)
2749 ecoff_swap_sym_in (input_bfd, esym_ptr->native.lnative, &sym);
2751 /* If we're producing an executable, move common symbols
2753 if (relocateable == false)
2755 if (sym.sc == scCommon)
2757 else if (sym.sc == scSCommon)
2761 if (! bfd_is_com_section (esym_ptr->symbol.section)
2762 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2763 && esym_ptr->symbol.section != &bfd_und_section)
2764 sym.value = (esym_ptr->symbol.value
2765 + esym_ptr->symbol.section->output_offset
2766 + esym_ptr->symbol.section->output_section->vma);
2767 ecoff_swap_sym_out (output_bfd, &sym, sym_out);
2772 /* That should have accounted for all the local symbols in
2774 BFD_ASSERT ((sym_out - output_ecoff->external_sym) - output_symhdr->isymMax
2775 == input_symhdr->isymMax);
2777 /* Copy the information that does not need swapping. */
2778 memcpy (output_ecoff->line + output_symhdr->cbLine,
2780 input_symhdr->cbLine * sizeof (unsigned char));
2781 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2782 input_ecoff->external_aux,
2783 input_symhdr->iauxMax * sizeof (union aux_ext));
2784 memcpy (output_ecoff->ss + output_symhdr->issMax,
2786 input_symhdr->issMax * sizeof (char));
2788 /* Some of the information may need to be swapped. */
2789 if (output_bfd->xvec->header_byteorder_big_p
2790 == input_bfd->xvec->header_byteorder_big_p)
2792 /* The two BFD's have the same endianness, so memcpy will
2794 memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
2795 input_ecoff->external_dnr,
2796 input_symhdr->idnMax * sizeof (struct dnr_ext));
2797 memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
2798 input_ecoff->external_pdr,
2799 input_symhdr->ipdMax * sizeof (struct pdr_ext));
2800 memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
2801 input_ecoff->external_opt,
2802 input_symhdr->ioptMax * sizeof (struct opt_ext));
2806 struct dnr_ext *dnr_in;
2807 struct dnr_ext *dnr_end;
2808 struct dnr_ext *dnr_out;
2809 struct pdr_ext *pdr_in;
2810 struct pdr_ext *pdr_end;
2811 struct pdr_ext *pdr_out;
2812 struct opt_ext *opt_in;
2813 struct opt_ext *opt_end;
2814 struct opt_ext *opt_out;
2816 /* The two BFD's have different endianness, so we must swap
2817 everything in and out. This code would always work, but it
2818 would be slow in the normal case. */
2819 dnr_in = input_ecoff->external_dnr;
2820 dnr_end = dnr_in + input_symhdr->idnMax;
2821 dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
2822 for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
2826 ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
2827 ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
2829 pdr_in = input_ecoff->external_pdr;
2830 pdr_end = pdr_in + input_symhdr->ipdMax;
2831 pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
2832 for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
2836 ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
2837 ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
2839 opt_in = input_ecoff->external_opt;
2840 opt_end = opt_in + input_symhdr->ioptMax;
2841 opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
2842 for (; opt_in < opt_end; opt_in++, opt_out++)
2846 ecoff_swap_opt_in (input_bfd, opt_in, &opt);
2847 ecoff_swap_opt_out (output_bfd, &opt, opt_out);
2851 /* Set ifdbase so that the external symbols know how to adjust their
2853 input_ecoff->ifdbase = output_symhdr->ifdMax;
2855 fdr_ptr = input_ecoff->fdr;
2856 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2857 fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
2858 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
2861 unsigned long pdr_off;
2865 /* The memory address for this fdr is the address for the seclet
2866 plus the offset to this fdr within input_bfd. For some
2867 reason the offset of the first procedure pointer is also
2875 ecoff_swap_pdr_in (input_bfd,
2876 input_ecoff->external_pdr + fdr.ipdFirst,
2880 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2882 + (fdr_ptr->adr - input_ecoff->fdr->adr)
2885 fdr.issBase += output_symhdr->issMax;
2886 fdr.isymBase += output_symhdr->isymMax;
2887 fdr.ilineBase += output_symhdr->ilineMax;
2888 fdr.ioptBase += output_symhdr->ioptMax;
2889 fdr.ipdFirst += output_symhdr->ipdMax;
2890 fdr.iauxBase += output_symhdr->iauxMax;
2891 fdr.rfdBase += output_symhdr->crfd;
2893 /* If there are no RFD's, we are going to add some. We don't
2894 want to adjust irfd for this, so that all the FDR's can share
2896 if (input_symhdr->crfd == 0)
2897 fdr.crfd = input_symhdr->ifdMax;
2899 if (fdr.cbLine != 0)
2900 fdr.cbLineOffset += output_symhdr->cbLine;
2902 ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
2905 if (input_symhdr->crfd > 0)
2907 struct rfd_ext *rfd_in;
2908 struct rfd_ext *rfd_end;
2909 struct rfd_ext *rfd_out;
2911 /* Swap and adjust the RFD's. RFD's are only created by the
2912 linker, so this will only be necessary if one of the input
2913 files is the result of a partial link. Presumably all
2914 necessary RFD's are present. */
2915 rfd_in = input_ecoff->external_rfd;
2916 rfd_end = rfd_in + input_symhdr->crfd;
2917 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2918 for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
2922 ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
2923 rfd += output_symhdr->ifdMax;
2924 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2926 output_symhdr->crfd += input_symhdr->crfd;
2930 struct rfd_ext *rfd_out;
2931 struct rfd_ext *rfd_end;
2934 /* Create RFD's. Some of the debugging information includes
2935 relative file indices. These indices are taken as indices to
2936 the RFD table if there is one, or to the global table if
2937 there is not. If we did not create RFD's, we would have to
2938 parse and adjust all the debugging information which contains
2940 rfd = output_symhdr->ifdMax;
2941 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2942 rfd_end = rfd_out + input_symhdr->ifdMax;
2943 for (; rfd_out < rfd_end; rfd_out++, rfd++)
2944 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2945 output_symhdr->crfd += input_symhdr->ifdMax;
2948 /* Combine the register masks. */
2952 output_ecoff->gprmask |= input_ecoff->gprmask;
2953 for (i = 0; i < 4; i++)
2954 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
2957 /* Update the counts. */
2958 output_symhdr->ilineMax += input_symhdr->ilineMax;
2959 output_symhdr->cbLine += input_symhdr->cbLine;
2960 output_symhdr->idnMax += input_symhdr->idnMax;
2961 output_symhdr->ipdMax += input_symhdr->ipdMax;
2962 output_symhdr->isymMax += input_symhdr->isymMax;
2963 output_symhdr->ioptMax += input_symhdr->ioptMax;
2964 output_symhdr->iauxMax += input_symhdr->iauxMax;
2965 output_symhdr->issMax += input_symhdr->issMax;
2966 output_symhdr->ifdMax += input_symhdr->ifdMax;
2971 /* This is the actual link routine. It makes two passes over all the
2975 ecoff_bfd_seclet_link (abfd, data, relocateable)
2978 boolean relocateable;
2982 register asection *o;
2983 register bfd_seclet_type *p;
2984 asymbol **sym_ptr_ptr;
2988 /* We accumulate the debugging information counts in the symbolic
2990 symhdr = &ecoff_data (abfd)->symbolic_header;
2991 symhdr->magic = magicSym;
2992 /* FIXME: What should the version stamp be? */
2994 symhdr->ilineMax = 0;
2998 symhdr->isymMax = 0;
2999 symhdr->ioptMax = 0;
3000 symhdr->iauxMax = 0;
3002 symhdr->issExtMax = 0;
3005 symhdr->iextMax = 0;
3007 /* We need to copy over the debugging symbols from each input BFD.
3008 When we do this copying, we have to adjust the text address in
3009 the FDR structures, so we have to know the text address used for
3010 the input BFD. Since we only want to copy the symbols once per
3011 input BFD, but we are going to look at each input BFD multiple
3012 times (once for each section it provides), we arrange to always
3013 look at the text section first. That means that when we copy the
3014 debugging information, we always know the text address. So we
3015 actually do each pass in two sub passes; first the text sections,
3016 then the non-text sections. We use the output_has_begun flag to
3017 determine whether we have copied over the debugging information
3020 /* Do the first pass: set the output section contents and count the
3021 debugging information. */
3022 ecoff_clear_output_flags (abfd);
3023 for (ipass = 0; ipass < 2; ipass++)
3025 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3027 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
3028 so they are done on pass 0. For other sections the
3029 expression is true, so they are done on pass 1. */
3030 if (((o->flags & SEC_CODE) == 0) != ipass)
3033 for (p = o->seclets_head;
3034 p != (bfd_seclet_type *) NULL;
3037 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
3044 /* We handle the external symbols differently. We use the ones
3045 attached to the output_bfd. The linker will have already
3046 determined which symbols are to be attached. Here we just
3047 determine how much space we will need for them. */
3048 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3049 if (sym_ptr_ptr != NULL)
3053 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
3054 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
3056 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
3057 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
3060 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
3065 /* Adjust the counts so that structures are longword aligned. */
3066 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
3067 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
3068 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
3070 /* Now the counts in symhdr are the correct size for the debugging
3071 information. We allocate the right amount of space, and reset
3072 the counts so that the second pass can use them as indices. It
3073 would be possible to output the debugging information directly to
3074 the file in pass 2, rather than to build it in memory and then
3075 write it out. Outputting to the file would require a lot of
3076 seeks and small writes, though, and I think this approach is
3078 size = (symhdr->cbLine * sizeof (unsigned char)
3079 + symhdr->idnMax * sizeof (struct dnr_ext)
3080 + symhdr->ipdMax * sizeof (struct pdr_ext)
3081 + symhdr->isymMax * sizeof (struct sym_ext)
3082 + symhdr->ioptMax * sizeof (struct opt_ext)
3083 + symhdr->iauxMax * sizeof (union aux_ext)
3084 + symhdr->issMax * sizeof (char)
3085 + symhdr->issExtMax * sizeof (char)
3086 + symhdr->ifdMax * sizeof (struct fdr_ext)
3087 + symhdr->crfd * sizeof (struct rfd_ext)
3088 + symhdr->iextMax * sizeof (struct ext_ext));
3089 raw = (char *) bfd_alloc (abfd, size);
3090 if (raw == (char *) NULL)
3092 bfd_error = no_memory;
3095 ecoff_data (abfd)->raw_size = size;
3096 ecoff_data (abfd)->raw_syments = (PTR) raw;
3098 /* Initialize the raw pointers. */
3099 #define SET(field, count, type) \
3100 ecoff_data (abfd)->field = (type *) raw; \
3101 raw += symhdr->count * sizeof (type)
3103 SET (line, cbLine, unsigned char);
3104 SET (external_dnr, idnMax, struct dnr_ext);
3105 SET (external_pdr, ipdMax, struct pdr_ext);
3106 SET (external_sym, isymMax, struct sym_ext);
3107 SET (external_opt, ioptMax, struct opt_ext);
3108 SET (external_aux, iauxMax, union aux_ext);
3109 SET (ss, issMax, char);
3110 SET (ssext, issExtMax, char);
3111 SET (external_fdr, ifdMax, struct fdr_ext);
3112 SET (external_rfd, crfd, struct rfd_ext);
3113 SET (external_ext, iextMax, struct ext_ext);
3116 /* Reset the counts so the second pass can use them to know how far
3118 symhdr->ilineMax = 0;
3122 symhdr->isymMax = 0;
3123 symhdr->ioptMax = 0;
3124 symhdr->iauxMax = 0;
3126 symhdr->issExtMax = 0;
3129 symhdr->iextMax = 0;
3131 /* Do the second pass: accumulate the debugging information. */
3132 ecoff_clear_output_flags (abfd);
3133 for (ipass = 0; ipass < 2; ipass++)
3135 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
3137 if (((o->flags & SEC_CODE) == 0) != ipass)
3139 for (p = o->seclets_head;
3140 p != (bfd_seclet_type *) NULL;
3143 if (p->type == bfd_indirect_seclet)
3145 if (ecoff_get_debug (abfd, p, o, relocateable) == false)
3152 /* Put in the external symbols. */
3153 sym_ptr_ptr = bfd_get_outsymbols (abfd);
3154 if (sym_ptr_ptr != NULL)
3157 struct ext_ext *external_ext;
3159 ssext = ecoff_data (abfd)->ssext;
3160 external_ext = ecoff_data (abfd)->external_ext;
3161 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
3166 sym_ptr = *sym_ptr_ptr;
3168 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
3169 || (sym_ptr->flags & BSF_LOCAL) != 0)
3172 /* The enative pointer can be NULL for a symbol created by
3173 the linker via ecoff_make_empty_symbol. */
3174 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
3175 || (((ecoff_symbol_type *) sym_ptr)->native.enative
3176 == (struct ext_ext *) NULL))
3179 esym.cobol_main = 0;
3183 /* FIXME: we can do better than this for st and sc. */
3184 esym.asym.st = stGlobal;
3185 esym.asym.sc = scAbs;
3186 esym.asym.reserved = 0;
3187 esym.asym.index = indexNil;
3191 ecoff_symbol_type *ecoff_sym_ptr;
3193 ecoff_sym_ptr = (ecoff_symbol_type *) sym_ptr;
3194 if (ecoff_sym_ptr->local)
3196 ecoff_swap_ext_in (abfd, ecoff_sym_ptr->native.enative, &esym);
3198 /* If we're producing an executable, move common symbols
3200 if (relocateable == false)
3202 if (esym.asym.sc == scCommon)
3203 esym.asym.sc = scBss;
3204 else if (esym.asym.sc == scSCommon)
3205 esym.asym.sc = scSBss;
3208 /* Adjust the FDR index for the symbol by that used for
3210 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
3213 esym.asym.iss = symhdr->issExtMax;
3215 if (bfd_is_com_section (sym_ptr->section)
3216 || sym_ptr->section == &bfd_und_section)
3217 esym.asym.value = sym_ptr->value;
3219 esym.asym.value = (sym_ptr->value
3220 + sym_ptr->section->output_offset
3221 + sym_ptr->section->output_section->vma);
3223 ecoff_swap_ext_out (abfd, &esym, external_ext + symhdr->iextMax);
3225 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
3229 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
3230 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
3234 /* Adjust the counts so that structures are longword aligned. */
3235 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
3236 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
3237 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
3242 /* Set the architecture. The only architecture we support here is
3243 mips. We set the architecture anyhow, since many callers ignore
3244 the return value. */
3247 ecoff_set_arch_mach (abfd, arch, machine)
3249 enum bfd_architecture arch;
3250 unsigned long machine;
3252 bfd_default_set_arch_mach (abfd, arch, machine);
3253 return arch == bfd_arch_mips;
3256 /* Get the size of the section headers. We do not output the .scommon
3257 section which we created in ecoff_mkobject. */
3260 ecoff_sizeof_headers (abfd, reloc)
3264 return FILHSZ + AOUTSZ + (abfd->section_count - 1) * SCNHSZ;
3267 /* Calculate the file position for each section, and set
3271 ecoff_compute_section_file_positions (abfd)
3279 if (bfd_get_start_address (abfd))
3280 abfd->flags |= EXEC_P;
3282 sofar = ecoff_sizeof_headers (abfd, false);
3285 for (current = abfd->sections;
3286 current != (asection *) NULL;
3287 current = current->next)
3289 /* Only deal with sections which have contents */
3290 if (! (current->flags & SEC_HAS_CONTENTS)
3291 || strcmp (current->name, SCOMMON) == 0)
3294 /* On Ultrix, the data sections in an executable file must be
3295 aligned to a page boundary within the file. This does not
3296 affect the section size, though. FIXME: Does this work for
3298 if ((abfd->flags & EXEC_P) != 0
3299 && (abfd->flags & D_PAGED) != 0
3300 && first_data != false
3301 && (current->flags & SEC_CODE) == 0)
3303 sofar = (sofar + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
3307 /* Align the sections in the file to the same boundary on
3308 which they are aligned in virtual memory. */
3310 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3312 current->filepos = sofar;
3314 sofar += current->_raw_size;
3316 /* make sure that this section is of the right size too */
3318 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3319 current->_raw_size += sofar - old_sofar;
3322 ecoff_data (abfd)->reloc_filepos = sofar;
3325 /* Set the contents of a section. */
3328 ecoff_set_section_contents (abfd, section, location, offset, count)
3333 bfd_size_type count;
3335 if (abfd->output_has_begun == false)
3336 ecoff_compute_section_file_positions (abfd);
3338 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
3341 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
3346 /* Write out an ECOFF file. */
3349 ecoff_write_object_contents (abfd)
3355 file_ptr reloc_base;
3357 unsigned long reloc_size;
3358 unsigned long text_size;
3359 unsigned long text_start;
3360 unsigned long data_size;
3361 unsigned long data_start;
3362 unsigned long bss_size;
3363 struct internal_filehdr internal_f;
3364 struct internal_aouthdr internal_a;
3367 bfd_error = system_call_error;
3369 if(abfd->output_has_begun == false)
3370 ecoff_compute_section_file_positions(abfd);
3372 if (abfd->sections != (asection *) NULL)
3373 scn_base = abfd->sections->filepos;
3376 reloc_base = ecoff_data (abfd)->reloc_filepos;
3380 for (current = abfd->sections;
3381 current != (asection *)NULL;
3382 current = current->next)
3384 if (strcmp (current->name, SCOMMON) == 0)
3386 current->target_index = count;
3388 if (current->reloc_count != 0)
3390 bfd_size_type relsize;
3392 current->rel_filepos = reloc_base;
3393 relsize = current->reloc_count * RELSZ;
3394 reloc_size += relsize;
3395 reloc_base += relsize;
3398 current->rel_filepos = 0;
3401 sym_base = reloc_base + reloc_size;
3403 /* At least on Ultrix, the symbol table of an executable file must
3404 be aligned to a page boundary. FIXME: Is this true on other
3406 if ((abfd->flags & EXEC_P) != 0
3407 && (abfd->flags & D_PAGED) != 0)
3408 sym_base = (sym_base + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
3410 ecoff_data (abfd)->sym_filepos = sym_base;
3412 if ((abfd->flags & D_PAGED) != 0)
3413 text_size = ecoff_sizeof_headers (abfd, false);
3421 /* Write section headers to the file. */
3423 internal_f.f_nscns = 0;
3424 if (bfd_seek (abfd, (file_ptr) (FILHSZ + AOUTSZ), SEEK_SET) != 0)
3426 for (current = abfd->sections;
3427 current != (asection *) NULL;
3428 current = current->next)
3430 struct internal_scnhdr section;
3433 if (strcmp (current->name, SCOMMON) == 0)
3435 BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
3436 && current->reloc_count == 0);
3440 ++internal_f.f_nscns;
3442 strncpy (section.s_name, current->name, sizeof section.s_name);
3444 /* FIXME: is this correct for shared libraries? I think it is
3445 but I have no platform to check. Ian Lance Taylor. */
3446 vma = bfd_get_section_vma (abfd, current);
3447 if (strcmp (current->name, _LIB) == 0)
3448 section.s_vaddr = 0;
3450 section.s_vaddr = vma;
3452 section.s_paddr = vma;
3453 section.s_size = bfd_get_section_size_before_reloc (current);
3455 /* If this section has no size or is unloadable then the scnptr
3457 if (current->_raw_size == 0
3458 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3459 section.s_scnptr = 0;
3461 section.s_scnptr = current->filepos;
3462 section.s_relptr = current->rel_filepos;
3464 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3465 object file produced by the assembler is supposed to point to
3466 information about how much room is required by objects of
3467 various different sizes. I think this only matters if we
3468 want the linker to compute the best size to use, or
3469 something. I don't know what happens if the information is
3471 section.s_lnnoptr = 0;
3473 section.s_nreloc = current->reloc_count;
3474 section.s_nlnno = 0;
3475 section.s_flags = ecoff_sec_to_styp_flags (current->name,
3481 ecoff_swap_scnhdr_out (abfd, (PTR) §ion, (PTR) &buff);
3482 if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
3486 if ((section.s_flags & STYP_TEXT) != 0)
3488 text_size += bfd_get_section_size_before_reloc (current);
3489 if (text_start == 0 || text_start > vma)
3492 else if ((section.s_flags & STYP_RDATA) != 0
3493 || (section.s_flags & STYP_DATA) != 0
3494 || (section.s_flags & STYP_LIT8) != 0
3495 || (section.s_flags & STYP_LIT4) != 0
3496 || (section.s_flags & STYP_SDATA) != 0)
3498 data_size += bfd_get_section_size_before_reloc (current);
3499 if (data_start == 0 || data_start > vma)
3502 else if ((section.s_flags & STYP_BSS) != 0
3503 || (section.s_flags & STYP_SBSS) != 0)
3504 bss_size += bfd_get_section_size_before_reloc (current);
3507 /* Set up the file header. */
3509 if (abfd->xvec->header_byteorder_big_p != false)
3510 internal_f.f_magic = MIPS_MAGIC_BIG;
3512 internal_f.f_magic = MIPS_MAGIC_LITTLE;
3515 We will NOT put a fucking timestamp in the header here. Every time you
3516 put it back, I will come in and take it out again. I'm sorry. This
3517 field does not belong here. We fill it with a 0 so it compares the
3518 same but is not a reasonable time. -- gnu@cygnus.com
3520 internal_f.f_timdat = 0;
3522 if (bfd_get_symcount (abfd) != 0)
3524 /* The ECOFF f_nsyms field is not actually the number of
3525 symbols, it's the size of symbolic information header. */
3526 internal_f.f_nsyms = sizeof (struct hdr_ext);
3527 internal_f.f_symptr = sym_base;
3531 internal_f.f_nsyms = 0;
3532 internal_f.f_symptr = 0;
3535 internal_f.f_opthdr = AOUTSZ;
3537 internal_f.f_flags = F_LNNO;
3538 if (reloc_size == 0)
3539 internal_f.f_flags |= F_RELFLG;
3540 if (bfd_get_symcount (abfd) == 0)
3541 internal_f.f_flags |= F_LSYMS;
3542 if (abfd->flags & EXEC_P)
3543 internal_f.f_flags |= F_EXEC;
3545 if (! abfd->xvec->byteorder_big_p)
3546 internal_f.f_flags |= F_AR32WR;
3548 internal_f.f_flags |= F_AR32W;
3550 /* Set up the ``optional'' header. */
3551 if ((abfd->flags & D_PAGED) != 0)
3552 internal_a.magic = ZMAGIC;
3554 internal_a.magic = OMAGIC;
3556 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
3557 linker happy. But, is it right? */
3558 internal_a.vstamp = 0x20a;
3560 /* At least on Ultrix, these have to be rounded to page boundaries.
3561 FIXME: Is this true on other platforms? */
3562 if ((abfd->flags & D_PAGED) != 0)
3564 internal_a.tsize = (text_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
3565 internal_a.text_start = text_start &~ (ROUND_SIZE - 1);
3566 internal_a.dsize = (data_size + ROUND_SIZE - 1) &~ (ROUND_SIZE - 1);
3567 internal_a.data_start = data_start &~ (ROUND_SIZE - 1);
3571 internal_a.tsize = text_size;
3572 internal_a.text_start = text_start;
3573 internal_a.dsize = data_size;
3574 internal_a.data_start = data_start;
3577 /* On Ultrix, the initial portions of the .sbss and .bss segments
3578 are at the end of the data section. The bsize field in the
3579 optional header records how many bss bytes are required beyond
3580 those in the data section. The value is not rounded to a page
3582 if (bss_size < internal_a.dsize - data_size)
3585 bss_size -= internal_a.dsize - data_size;
3586 internal_a.bsize = bss_size;
3587 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
3589 internal_a.entry = bfd_get_start_address (abfd);
3591 internal_a.gp_value = ecoff_data (abfd)->gp;
3593 internal_a.gprmask = ecoff_data (abfd)->gprmask;
3594 for (i = 0; i < 4; i++)
3595 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
3597 /* Write out the file header and the optional header. */
3599 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3604 ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
3605 if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
3612 ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
3613 if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
3617 /* Write out the relocs. */
3618 for (current = abfd->sections;
3619 current != (asection *) NULL;
3620 current = current->next)
3623 arelent **reloc_ptr_ptr;
3624 arelent **reloc_end;
3627 if (current->reloc_count == 0)
3630 buff = (RELOC *) bfd_alloc (abfd, current->reloc_count * RELSZ);
3631 if (buff == (RELOC *) NULL)
3633 bfd_error = no_memory;
3637 reloc_ptr_ptr = current->orelocation;
3638 reloc_end = reloc_ptr_ptr + current->reloc_count;
3640 for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr++)
3644 struct internal_reloc in;
3646 memset (&in, 0, sizeof in);
3648 reloc = *reloc_ptr_ptr;
3649 sym = *reloc->sym_ptr_ptr;
3651 /* This must be an ECOFF reloc. */
3652 BFD_ASSERT (reloc->howto != (reloc_howto_type *) NULL
3653 && reloc->howto >= ecoff_howto_table
3655 < (ecoff_howto_table + ECOFF_HOWTO_COUNT)));
3657 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3658 in.r_type = reloc->howto->type;
3660 /* If this is a REFHI reloc, the next one must be a REFLO
3661 reloc for the same symbol. */
3662 BFD_ASSERT (in.r_type != ECOFF_R_REFHI
3663 || (reloc_ptr_ptr < reloc_end
3664 && (reloc_ptr_ptr[1]->howto
3665 != (reloc_howto_type *) NULL)
3666 && (reloc_ptr_ptr[1]->howto->type
3668 && (sym == *reloc_ptr_ptr[1]->sym_ptr_ptr)));
3670 if ((sym->flags & BSF_SECTION_SYM) == 0)
3672 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3679 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3680 if (strcmp (name, ".text") == 0)
3681 in.r_symndx = RELOC_SECTION_TEXT;
3682 else if (strcmp (name, ".rdata") == 0)
3683 in.r_symndx = RELOC_SECTION_RDATA;
3684 else if (strcmp (name, ".data") == 0)
3685 in.r_symndx = RELOC_SECTION_DATA;
3686 else if (strcmp (name, ".sdata") == 0)
3687 in.r_symndx = RELOC_SECTION_SDATA;
3688 else if (strcmp (name, ".sbss") == 0)
3689 in.r_symndx = RELOC_SECTION_SBSS;
3690 else if (strcmp (name, ".bss") == 0)
3691 in.r_symndx = RELOC_SECTION_BSS;
3692 else if (strcmp (name, ".init") == 0)
3693 in.r_symndx = RELOC_SECTION_INIT;
3694 else if (strcmp (name, ".lit8") == 0)
3695 in.r_symndx = RELOC_SECTION_LIT8;
3696 else if (strcmp (name, ".lit4") == 0)
3697 in.r_symndx = RELOC_SECTION_LIT4;
3703 ecoff_swap_reloc_out (abfd, (PTR) &in, (PTR) out_ptr);
3706 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
3708 if (bfd_write ((PTR) buff, RELSZ, current->reloc_count, abfd)
3709 != RELSZ * current->reloc_count)
3711 bfd_release (abfd, (PTR) buff);
3714 /* Write out the symbolic debugging information. */
3715 if (bfd_get_symcount (abfd) > 0)
3718 unsigned long sym_offset;
3719 struct hdr_ext buff;
3721 /* Set up the offsets in the symbolic header. */
3722 symhdr = &ecoff_data (abfd)->symbolic_header;
3723 sym_offset = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
3725 #define SET(offset, size, ptr) \
3726 if (symhdr->size == 0) \
3727 symhdr->offset = 0; \
3729 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3730 - (char *) ecoff_data (abfd)->raw_syments) \
3733 SET (cbLineOffset, cbLine, line);
3734 SET (cbDnOffset, idnMax, external_dnr);
3735 SET (cbPdOffset, ipdMax, external_pdr);
3736 SET (cbSymOffset, isymMax, external_sym);
3737 SET (cbOptOffset, ioptMax, external_opt);
3738 SET (cbAuxOffset, iauxMax, external_aux);
3739 SET (cbSsOffset, issMax, ss);
3740 SET (cbSsExtOffset, issExtMax, ssext);
3741 SET (cbFdOffset, ifdMax, external_fdr);
3742 SET (cbRfdOffset, crfd, external_rfd);
3743 SET (cbExtOffset, iextMax, external_ext);
3746 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3749 ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
3750 if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
3752 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3753 ecoff_data (abfd)->raw_size, abfd)
3754 != ecoff_data (abfd)->raw_size)
3757 else if ((abfd->flags & EXEC_P) != 0
3758 && (abfd->flags & D_PAGED) != 0)
3762 /* A demand paged executable must occupy an even number of
3764 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3767 if (bfd_read (&c, 1, 1, abfd) == 0)
3769 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
3772 if (bfd_write (&c, 1, 1, abfd) != 1)
3779 /* Archive handling. ECOFF uses what appears to be a unique type of
3780 archive header (which I call an armap). The byte ordering of the
3781 armap and the contents are encoded in the name of the armap itself.
3782 At least for now, we only support archives with the same byte
3783 ordering in the armap and the contents.
3785 The first four bytes in the armap are the number of symbol
3786 definitions. This is always a power of two.
3788 This is followed by the symbol definitions. Each symbol definition
3789 occupies 8 bytes. The first four bytes are the offset from the
3790 start of the armap strings to the null-terminated string naming
3791 this symbol. The second four bytes are the file offset to the
3792 archive member which defines this symbol. If the second four bytes
3793 are 0, then this is not actually a symbol definition, and it should
3796 The symbols are hashed into the armap with a closed hashing scheme.
3797 See the functions below for the details of the algorithm.
3799 We could use the hash table when looking up symbols in a library.
3800 This would require a new BFD target entry point to replace the
3801 bfd_get_next_mapent function used by the linker.
3803 After the symbol definitions comes four bytes holding the size of
3804 the string table, followed by the string table itself. */
3806 /* The name of an archive headers looks like this:
3807 __________E[BL]E[BL]_ (with a trailing space).
3808 The trailing space is changed to an X if the archive is changed to
3809 indicate that the armap is out of date. */
3811 #define ARMAP_BIG_ENDIAN 'B'
3812 #define ARMAP_LITTLE_ENDIAN 'L'
3813 #define ARMAP_MARKER 'E'
3814 #define ARMAP_START "__________"
3815 #define ARMAP_HEADER_MARKER_INDEX 10
3816 #define ARMAP_HEADER_ENDIAN_INDEX 11
3817 #define ARMAP_OBJECT_MARKER_INDEX 12
3818 #define ARMAP_OBJECT_ENDIAN_INDEX 13
3819 #define ARMAP_END_INDEX 14
3820 #define ARMAP_END "_ "
3822 /* This is a magic number used in the hashing algorithm. */
3823 #define ARMAP_HASH_MAGIC 0x9dd68ab5
3825 /* This returns the hash value to use for a string. It also sets
3826 *REHASH to the rehash adjustment if the first slot is taken. SIZE
3827 is the number of entries in the hash table, and HLOG is the log
3831 ecoff_armap_hash (s, rehash, size, hlog)
3833 unsigned int *rehash;
3841 hash = ((hash >> 27) | (hash << 5)) + *s++;
3842 hash *= ARMAP_HASH_MAGIC;
3843 *rehash = (hash & (size - 1)) | 1;
3844 return hash >> (32 - hlog);
3847 /* Read in the armap. */
3850 ecoff_slurp_armap (abfd)
3855 struct areltdata *mapdata;
3856 bfd_size_type parsed_size;
3858 struct artdata *ardata;
3861 struct symdef *symdef_ptr;
3864 /* Get the name of the first element. */
3865 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3871 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3873 /* See if the first element is an armap. */
3874 if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
3875 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3876 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3877 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3878 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3879 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3880 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3881 || strncmp (nextname + ARMAP_END_INDEX,
3882 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3884 bfd_has_map (abfd) = false;
3888 /* Make sure we have the right byte ordering. */
3889 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3890 ^ (abfd->xvec->header_byteorder_big_p != false))
3891 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3892 ^ (abfd->xvec->byteorder_big_p != false)))
3894 bfd_error = wrong_format;
3898 /* Read in the armap. */
3899 ardata = bfd_ardata (abfd);
3900 mapdata = snarf_ar_hdr (abfd);
3901 if (mapdata == (struct areltdata *) NULL)
3903 parsed_size = mapdata->parsed_size;
3904 bfd_release (abfd, (PTR) mapdata);
3906 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3907 if (raw_armap == (char *) NULL)
3909 bfd_error = no_memory;
3913 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3915 bfd_error = malformed_archive;
3916 bfd_release (abfd, (PTR) raw_armap);
3920 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3922 ardata->symdef_count = 0;
3923 ardata->cache = (struct ar_cache *) NULL;
3925 /* Hack: overlay the symdefs on top of the raw archive data. This
3926 is the way do_slurp_bsd_armap works. */
3927 raw_ptr = raw_armap + LONG_SIZE;
3928 symdef_ptr = (struct symdef *) raw_ptr;
3929 ardata->symdefs = (carsym *) symdef_ptr;
3930 stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
3932 #ifdef CHECK_ARMAP_HASH
3936 /* Double check that I have the hashing algorithm right by making
3937 sure that every symbol can be looked up successfully. */
3939 for (i = 1; i < count; i <<= 1)
3941 BFD_ASSERT (i == count);
3943 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3945 unsigned int name_offset, file_offset;
3946 unsigned int hash, rehash, srch;
3948 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3949 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3950 if (file_offset == 0)
3952 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3957 /* See if we can rehash to this location. */
3958 for (srch = (hash + rehash) & (count - 1);
3959 srch != hash && srch != i;
3960 srch = (srch + rehash) & (count - 1))
3961 BFD_ASSERT (bfd_h_get_32 (abfd,
3964 + (srch * 2 * LONG_SIZE)
3967 BFD_ASSERT (srch == i);
3971 raw_ptr = raw_armap + LONG_SIZE;
3972 #endif /* CHECK_ARMAP_HASH */
3974 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3976 unsigned int name_offset, file_offset;
3978 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3979 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3980 if (file_offset == 0)
3982 symdef_ptr->s.name = stringbase + name_offset;
3983 symdef_ptr->file_offset = file_offset;
3985 ++ardata->symdef_count;
3988 ardata->first_file_filepos = bfd_tell (abfd);
3989 /* Pad to an even boundary. */
3990 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3992 bfd_has_map (abfd) = true;
3997 /* Write out an armap. */
4000 ecoff_write_armap (abfd, elength, map, orl_count, stridx)
4002 unsigned int elength;
4004 unsigned int orl_count;
4007 unsigned int hashsize, hashlog;
4008 unsigned int symdefsize;
4010 unsigned int stringsize;
4011 unsigned int mapsize;
4014 struct stat statbuf;
4016 bfd_byte temp[LONG_SIZE];
4017 bfd_byte *hashtable;
4021 /* Ultrix appears to use as a hash table size the least power of two
4022 greater than twice the number of entries. */
4023 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
4025 hashsize = 1 << hashlog;
4027 symdefsize = hashsize * 2 * LONG_SIZE;
4029 stringsize = stridx + padit;
4031 /* Include 8 bytes to store symdefsize and stringsize in output. */
4032 mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
4034 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
4036 memset ((PTR) &hdr, 0, sizeof hdr);
4038 /* Work out the ECOFF armap name. */
4039 strcpy (hdr.ar_name, ARMAP_START);
4040 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
4041 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
4042 (abfd->xvec->header_byteorder_big_p
4044 : ARMAP_LITTLE_ENDIAN);
4045 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
4046 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
4047 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
4048 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
4050 /* Write the timestamp of the archive header to be just a little bit
4051 later than the timestamp of the file, otherwise the linker will
4052 complain that the index is out of date. Actually, the Ultrix
4053 linker just checks the archive name; the GNU linker may check the
4055 stat (abfd->filename, &statbuf);
4056 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
4058 /* The DECstation uses zeroes for the uid, gid and mode of the
4060 hdr.ar_uid[0] = '0';
4061 hdr.ar_gid[0] = '0';
4062 hdr.ar_mode[0] = '0';
4064 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
4066 hdr.ar_fmag[0] = '`';
4067 hdr.ar_fmag[1] = '\n';
4069 /* Turn all null bytes in the header into spaces. */
4070 for (i = 0; i < sizeof (struct ar_hdr); i++)
4071 if (((char *)(&hdr))[i] == '\0')
4072 (((char *)(&hdr))[i]) = ' ';
4074 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
4075 != sizeof (struct ar_hdr))
4078 bfd_h_put_32 (abfd, hashsize, temp);
4079 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4082 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
4084 current = abfd->archive_head;
4086 for (i = 0; i < orl_count; i++)
4088 unsigned int hash, rehash;
4090 /* Advance firstreal to the file position of this archive
4092 if (((bfd *) map[i].pos) != last_elt)
4096 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
4097 firstreal += firstreal % 2;
4098 current = current->next;
4100 while (current != (bfd *) map[i].pos);
4105 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
4106 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4107 + (hash * 2 * LONG_SIZE)
4113 /* The desired slot is already taken. */
4114 for (srch = (hash + rehash) & (hashsize - 1);
4116 srch = (srch + rehash) & (hashsize - 1))
4117 if (bfd_h_get_32 (abfd, (PTR) (hashtable
4118 + (srch * 2 * LONG_SIZE)
4123 BFD_ASSERT (srch != hash);
4128 bfd_h_put_32 (abfd, map[i].namidx,
4129 (PTR) (hashtable + hash * 2 * LONG_SIZE));
4130 bfd_h_put_32 (abfd, firstreal,
4131 (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
4134 if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
4137 bfd_release (abfd, hashtable);
4139 /* Now write the strings. */
4140 bfd_h_put_32 (abfd, stringsize, temp);
4141 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
4143 for (i = 0; i < orl_count; i++)
4147 len = strlen (*map[i].name) + 1;
4148 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
4152 /* The spec sez this should be a newline. But in order to be
4153 bug-compatible for DECstation ar we use a null. */
4156 if (bfd_write ("\0", 1, 1, abfd) != 1)
4163 /* We just use the generic extended name support. This is a GNU
4165 #define ecoff_slurp_extended_name_table _bfd_slurp_extended_name_table
4167 /* See whether this BFD is an archive. If it is, read in the armap
4168 and the extended name table. */
4171 ecoff_archive_p (abfd)
4174 char armag[SARMAG + 1];
4176 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
4177 || strncmp (armag, ARMAG, SARMAG) != 0)
4179 bfd_error = wrong_format;
4180 return (bfd_target *) NULL;
4183 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
4184 involves a cast, we can't do it as the left operand of
4186 abfd->tdata.aout_ar_data =
4187 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
4189 if (bfd_ardata (abfd) == (struct artdata *) NULL)
4191 bfd_error = no_memory;
4192 return (bfd_target *) NULL;
4195 bfd_ardata (abfd)->first_file_filepos = SARMAG;
4197 if (ecoff_slurp_armap (abfd) == false
4198 || ecoff_slurp_extended_name_table (abfd) == false)
4200 bfd_release (abfd, bfd_ardata (abfd));
4201 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
4202 return (bfd_target *) NULL;
4210 #include <core.out.h>
4212 struct sgi_core_struct
4215 char cmd[CORE_NAMESIZE];
4218 #define core_hdr(bfd) ((bfd)->tdata.sgi_core_data)
4219 #define core_signal(bfd) (core_hdr(bfd)->sig)
4220 #define core_command(bfd) (core_hdr(bfd)->cmd)
4223 make_bfd_asection (abfd, name, flags, _raw_size, vma, filepos)
4227 bfd_size_type _raw_size;
4233 asect = bfd_make_section (abfd, name);
4237 asect->flags = flags;
4238 asect->_raw_size = _raw_size;
4240 asect->filepos = filepos;
4241 asect->alignment_power = 4;
4247 ecoff_core_file_p (abfd)
4253 struct coreout coreout;
4254 struct idesc *idg, *idf, *ids;
4256 val = bfd_read ((PTR)&coreout, 1, sizeof coreout, abfd);
4257 if (val != sizeof coreout)
4260 if (coreout.c_magic != CORE_MAGIC
4261 || coreout.c_version != CORE_VERSION1)
4264 core_hdr (abfd) = (struct sgi_core_struct *) bfd_zalloc (abfd, sizeof (struct sgi_core_struct));
4265 if (!core_hdr (abfd))
4268 strncpy (core_command (abfd), coreout.c_name, CORE_NAMESIZE);
4269 core_signal (abfd) = coreout.c_sigcause;
4271 bfd_seek (abfd, coreout.c_vmapoffset, SEEK_SET);
4273 for (i = 0; i < coreout.c_nvmap; i++)
4277 val = bfd_read ((PTR)&vmap, 1, sizeof vmap, abfd);
4278 if (val != sizeof vmap)
4281 switch (vmap.v_type)
4293 if (!make_bfd_asection (abfd, secname,
4294 SEC_ALLOC+SEC_LOAD+SEC_HAS_CONTENTS,
4302 /* Make sure that the regs are contiguous within the core file. */
4304 idg = &coreout.c_idesc[I_GPREGS];
4305 idf = &coreout.c_idesc[I_FPREGS];
4306 ids = &coreout.c_idesc[I_SPECREGS];
4308 if (idg->i_offset + idg->i_len != idf->i_offset
4309 || idf->i_offset + idf->i_len != ids->i_offset)
4310 return 0; /* Can't deal with non-contig regs */
4312 bfd_seek (abfd, idg->i_offset, SEEK_SET);
4314 make_bfd_asection (abfd, ".reg",
4315 SEC_ALLOC+SEC_HAS_CONTENTS,
4316 idg->i_len + idf->i_len + ids->i_len,
4320 /* OK, we believe you. You're a core file (sure, sure). */
4326 ecoff_core_file_failing_command (abfd)
4329 return core_command (abfd);
4333 ecoff_core_file_failing_signal (abfd)
4336 return core_signal (abfd);
4340 ecoff_core_file_matches_executable_p (core_bfd, exec_bfd)
4341 bfd *core_bfd, *exec_bfd;
4343 return true; /* XXX - FIXME */
4345 #else /* not def HOST_IRIX4 */
4346 #define ecoff_core_file_p _bfd_dummy_target
4347 #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
4348 #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
4349 #define ecoff_core_file_matches_executable_p \
4350 _bfd_dummy_core_file_matches_executable_p
4353 /* This is the COFF backend structure. The backend_data field of the
4354 bfd_target structure is set to this. The section reading code in
4355 coffgen.c uses this structure. */
4357 static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
4358 (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
4359 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
4360 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
4361 (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
4362 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
4363 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
4364 ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
4365 ecoff_swap_scnhdr_out,
4366 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
4367 ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
4368 ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
4369 ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
4370 ecoff_slurp_symbol_table
4373 /* get_lineno could be written for ECOFF, but it would currently only
4374 be useful for linking ECOFF and COFF files together, which doesn't
4376 #define ecoff_get_lineno \
4377 ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
4379 /* These bfd_target functions are defined in other files. */
4381 #define ecoff_truncate_arname bfd_dont_truncate_arname
4382 #define ecoff_openr_next_archived_file bfd_generic_openr_next_archived_file
4383 #define ecoff_generic_stat_arch_elt bfd_generic_stat_arch_elt
4384 #define ecoff_get_section_contents bfd_generic_get_section_contents
4385 #define ecoff_get_reloc_upper_bound coff_get_reloc_upper_bound
4386 #define ecoff_close_and_cleanup bfd_generic_close_and_cleanup
4387 #define ecoff_bfd_debug_info_start bfd_void
4388 #define ecoff_bfd_debug_info_end bfd_void
4389 #define ecoff_bfd_debug_info_accumulate \
4390 ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
4391 #define ecoff_bfd_get_relocated_section_contents \
4392 bfd_generic_get_relocated_section_contents
4393 #define ecoff_bfd_relax_section bfd_generic_relax_section
4394 #define ecoff_bfd_make_debug_symbol \
4395 ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
4397 bfd_target ecoff_little_vec =
4399 "ecoff-littlemips", /* name */
4400 bfd_target_ecoff_flavour,
4401 false, /* data byte order is little */
4402 false, /* header byte order is little */
4404 (HAS_RELOC | EXEC_P | /* object flags */
4405 HAS_LINENO | HAS_DEBUG |
4406 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
4408 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
4410 0, /* leading underscore */
4411 ' ', /* ar_pad_char */
4412 15, /* ar_max_namelen */
4413 4, /* minimum alignment power */
4414 _do_getl64, _do_getl_signed_64, _do_putl64,
4415 _do_getl32, _do_getl_signed_32, _do_putl32,
4416 _do_getl16, _do_getl_signed_16, _do_putl16, /* data */
4417 _do_getl64, _do_getl_signed_64, _do_putl64,
4418 _do_getl32, _do_getl_signed_32, _do_putl32,
4419 _do_getl16, _do_getl_signed_16, _do_putl16, /* hdrs */
4421 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
4422 ecoff_archive_p, _bfd_dummy_target},
4423 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
4425 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
4426 _bfd_write_archive_contents, bfd_false},
4428 (PTR) &bfd_ecoff_std_swap_table
4431 bfd_target ecoff_big_vec =
4433 "ecoff-bigmips", /* name */
4434 bfd_target_ecoff_flavour,
4435 true, /* data byte order is big */
4436 true, /* header byte order is big */
4438 (HAS_RELOC | EXEC_P | /* object flags */
4439 HAS_LINENO | HAS_DEBUG |
4440 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
4442 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
4443 0, /* leading underscore */
4444 ' ', /* ar_pad_char */
4445 15, /* ar_max_namelen */
4446 4, /* minimum alignment power */
4447 _do_getb64, _do_getb_signed_64, _do_putb64,
4448 _do_getb32, _do_getb_signed_32, _do_putb32,
4449 _do_getb16, _do_getb_signed_16, _do_putb16,
4450 _do_getb64, _do_getb_signed_64, _do_putb64,
4451 _do_getb32, _do_getb_signed_32, _do_putb32,
4452 _do_getb16, _do_getb_signed_16, _do_putb16,
4453 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
4454 ecoff_archive_p, ecoff_core_file_p},
4455 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
4457 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
4458 _bfd_write_archive_contents, bfd_false},
4460 (PTR) &bfd_ecoff_std_swap_table
4461 /* Note that there is another bfd_target just above this one. If
4462 you are adding initializers here, you should be adding them there