1 /* BFD back-end for MIPS Extended-Coff files.
2 Copyright 1990, 1991, 1992 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support 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"
28 #include "coff/mips.h"
29 #include "coff/internal.h"
31 #include "coff/symconst.h"
32 #include "coff/ecoff-ext.h"
35 /* `Tdata' information kept for ECOFF files. */
37 #define ecoff_data(abfd) ((abfd)->tdata.ecoff_obj_data)
39 typedef struct ecoff_tdata
41 /* The reloc file position, set by
42 ecoff_compute_section_file_positions. */
43 file_ptr reloc_filepos;
45 /* The symbol table file position, set by ecoff_mkobject_hook. */
48 /* The cached gp value. This is used when relocating. */
51 /* The register masks. When linking, all the masks found in the
52 input files are combined into the masks of the output file. */
53 unsigned long gprmask;
54 unsigned long cprmask[4];
56 /* The size of the unswapped ECOFF symbolic information. */
57 bfd_size_type raw_size;
59 /* The unswapped ECOFF symbolic information. */
62 /* The swapped ECOFF symbolic header. */
65 /* Pointers to the unswapped symbolic information. */
67 struct dnr_ext *external_dnr;
68 struct pdr_ext *external_pdr;
69 struct sym_ext *external_sym;
70 struct opt_ext *external_opt;
71 union aux_ext *external_aux;
74 struct fdr_ext *external_fdr;
75 struct rfd_ext *external_rfd;
76 struct ext_ext *external_ext;
78 /* The swapped FDR information. */
81 /* The FDR index. This is set for an input BFD to a link so that
82 the external symbols can set their FDR index correctly. */
85 /* The canonical BFD symbols. */
86 struct ecoff_symbol_struct *canonical_symbols;
90 /* Each canonical asymbol really looks like this. */
92 typedef struct ecoff_symbol_struct
94 /* The actual symbol which the rest of BFD works with */
97 /* The fdr for this symbol. */
100 /* true if this is a local symbol rather than an external one. */
103 /* A pointer to the unswapped hidden information for this symbol */
106 struct sym_ext *lnative;
107 struct ext_ext *enative;
112 /* We take the address of the first element of a asymbol to ensure that the
113 macro is only ever applied to an asymbol. */
114 #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
116 /* This is a hack borrowed from coffcode.h; we need to save the index
117 of a symbol when we write it out so that can set the symbol index
118 correctly when we write out the relocs. */
119 #define ecoff_get_sym_index(symbol) ((unsigned long) (symbol)->udata)
120 #define ecoff_set_sym_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
122 /* The page boundary used to align sections in the executable file. */
123 #define PAGE_SIZE 0x2000
125 /* MIPS ECOFF has COFF sections, but the debugging information is
126 stored in a completely different format. This files uses the some
127 of the swapping routines from coffswap.h, and some of the generic
128 COFF routines in coffgen.c, but, unlike the real COFF targets, does
129 not use coffcode.h itself. */
131 /* Get the generic COFF swapping routines, except for the reloc,
132 symbol, and lineno ones. Give them ecoff names. */
134 #define NO_COFF_RELOCS
135 #define NO_COFF_SYMBOLS
136 #define NO_COFF_LINENOS
137 #define coff_swap_filehdr_in ecoff_swap_filehdr_in
138 #define coff_swap_filehdr_out ecoff_swap_filehdr_out
139 #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
140 #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
141 #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
142 #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
143 #include "coffswap.h"
145 /* This stuff is somewhat copied from coffcode.h. */
147 static asection bfd_debug_section = { "*DEBUG*" };
149 /* See whether the magic number matches. */
152 DEFUN(ecoff_bad_format_hook, (abfd, filehdr),
156 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
158 if (ECOFFBADMAG (*internal_f))
164 /* This is a hook needed by SCO COFF, but we have nothing to do. */
167 DEFUN (ecoff_make_section_hook, (abfd, name),
171 return (asection *) NULL;
174 /* Initialize a new section. */
177 DEFUN (ecoff_new_section_hook, (abfd, section),
181 section->alignment_power = abfd->xvec->align_power_min;
183 if (strcmp (section->name, _TEXT) == 0)
184 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
185 else if (strcmp (section->name, _DATA) == 0
186 || strcmp (section->name, _SDATA) == 0)
187 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
188 else if (strcmp (section->name, _RDATA) == 0
189 || strcmp (section->name, _LIT8) == 0
190 || strcmp (section->name, _LIT4) == 0)
191 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
192 else if (strcmp (section->name, _BSS) == 0
193 || strcmp (section->name, _SBSS) == 0)
194 section->flags |= SEC_ALLOC;
196 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
197 uncertain about .init on some systems and I don't know how shared
203 #define ecoff_set_alignment_hook \
204 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
207 DEFUN (ecoff_mkobject, (abfd),
210 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
211 bfd_zalloc (abfd, sizeof(ecoff_data_type)));
212 if (abfd->tdata.ecoff_obj_data == NULL)
214 bfd_error = no_memory;
221 /* Create the COFF backend specific information. */
224 ecoff_mkobject_hook (abfd, filehdr, aouthdr)
229 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
230 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
231 ecoff_data_type *ecoff;
233 if (ecoff_mkobject (abfd) == false)
236 ecoff = ecoff_data (abfd);
237 ecoff->sym_filepos = internal_f->f_symptr;
239 if (internal_a != (struct internal_aouthdr *) NULL)
243 ecoff->gp = internal_a->gp_value;
244 ecoff->gprmask = internal_a->gprmask;
245 for (i = 0; i < 3; i++)
246 ecoff->cprmask[i] = internal_a->cprmask[i];
252 /* Determine the machine architecture and type. */
254 DEFUN (ecoff_set_arch_mach_hook, (abfd, filehdr),
259 enum bfd_architecture arch;
260 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
263 switch (internal_f->f_magic) {
267 arch = bfd_arch_mips;
271 default: /* Unreadable input file type */
272 arch = bfd_arch_obscure;
276 bfd_default_set_arch_mach(abfd, arch, machine);
280 /* Get the section s_flags to use for a section. */
283 DEFUN (sec_to_styp_flags, (name, flags),
291 if (strcmp (name, _TEXT) == 0)
293 else if (strcmp (name, _DATA) == 0)
295 else if (strcmp (name, _SDATA) == 0)
297 else if (strcmp (name, _RDATA) == 0)
299 else if (strcmp (name, _LIT8) == 0)
301 else if (strcmp (name, _LIT4) == 0)
303 else if (strcmp (name, _BSS) == 0)
305 else if (strcmp (name, _SBSS) == 0)
307 else if (flags & SEC_CODE)
309 else if (flags & SEC_DATA)
311 else if (flags & SEC_READONLY)
313 else if (flags & SEC_LOAD)
318 if (flags & SEC_NEVER_LOAD)
324 /* Get the BFD flags to use for a section. */
327 DEFUN (styp_to_sec_flags, (abfd, hdr),
331 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
332 long styp_flags = internal_s->s_flags;
333 flagword sec_flags=0;
335 if (styp_flags & STYP_NOLOAD)
336 sec_flags |= SEC_NEVER_LOAD;
338 /* For 386 COFF, at least, an unloadable text or data section is
339 actually a shared library section. */
340 if (styp_flags & STYP_TEXT)
342 if (sec_flags & SEC_NEVER_LOAD)
343 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
345 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
347 else if ((styp_flags & STYP_DATA)
348 || (styp_flags & STYP_RDATA)
349 || (styp_flags & STYP_SDATA))
351 if (sec_flags & SEC_NEVER_LOAD)
352 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
354 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
355 if (styp_flags & STYP_RDATA)
356 sec_flags |= SEC_READONLY;
358 else if ((styp_flags & STYP_BSS)
359 || (styp_flags & STYP_SBSS))
361 sec_flags |= SEC_ALLOC;
363 else if (styp_flags & STYP_INFO)
365 sec_flags |= SEC_NEVER_LOAD;
367 else if ((styp_flags & STYP_LIT8)
368 || (styp_flags & STYP_LIT4))
370 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
374 sec_flags |= SEC_ALLOC | SEC_LOAD;
380 /* Read in and swap the important symbolic information for an ECOFF
384 DEFUN (ecoff_slurp_symbolic_info, (abfd),
387 struct hdr_ext external_symhdr;
388 HDRR *internal_symhdr;
389 bfd_size_type raw_base;
390 bfd_size_type raw_size;
392 struct fdr_ext *fraw_src;
393 struct fdr_ext *fraw_end;
396 /* Check whether we've already gotten it, and whether there's any to
398 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
400 if (ecoff_data (abfd)->sym_filepos == 0)
402 bfd_get_symcount (abfd) = 0;
406 /* At this point bfd_get_symcount (abfd) holds the number of symbols
407 as read from the file header, but on ECOFF this is always the
408 size of the symbolic information header. It would be cleaner to
409 handle this when we first read the file in coffgen.c. */
410 if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
412 bfd_error = bad_value;
416 /* Read the symbolic information header. */
417 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
418 || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
419 != sizeof (external_symhdr)))
421 bfd_error = system_call_error;
424 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
425 ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
427 if (internal_symhdr->magic != magicSym)
429 bfd_error = bad_value;
433 /* Now we can get the correct number of symbols. */
434 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
435 + internal_symhdr->iextMax);
437 /* Read all the symbolic information at once. */
438 raw_size = (internal_symhdr->cbLine * sizeof (unsigned char)
439 + internal_symhdr->idnMax * sizeof (struct dnr_ext)
440 + internal_symhdr->ipdMax * sizeof (struct pdr_ext)
441 + internal_symhdr->isymMax * sizeof (struct sym_ext)
442 + internal_symhdr->ioptMax * sizeof (struct opt_ext)
443 + internal_symhdr->iauxMax * sizeof (union aux_ext)
444 + internal_symhdr->issMax * sizeof (char)
445 + internal_symhdr->issExtMax * sizeof (char)
446 + internal_symhdr->ifdMax * sizeof (struct fdr_ext)
447 + internal_symhdr->crfd * sizeof (struct rfd_ext)
448 + internal_symhdr->iextMax * sizeof (struct ext_ext));
451 ecoff_data (abfd)->sym_filepos = 0;
454 raw = (PTR) bfd_alloc (abfd, raw_size);
457 bfd_error = no_memory;
460 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
462 bfd_error = system_call_error;
463 bfd_release (abfd, raw);
467 ecoff_data (abfd)->raw_size = raw_size;
468 ecoff_data (abfd)->raw_syments = raw;
470 /* Get pointers for the numeric offsets in the HDRR structure. */
471 raw_base = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
473 #define FIX(off1, off2, type) \
474 if (internal_symhdr->off1 == 0) \
475 ecoff_data (abfd)->off2 = (type *) NULL; \
477 ecoff_data (abfd)->off2 = (type *) ((char *) raw \
478 + internal_symhdr->off1 \
480 FIX (cbLineOffset, line, unsigned char);
481 FIX (cbDnOffset, external_dnr, struct dnr_ext);
482 FIX (cbPdOffset, external_pdr, struct pdr_ext);
483 FIX (cbSymOffset, external_sym, struct sym_ext);
484 FIX (cbOptOffset, external_opt, struct opt_ext);
485 FIX (cbAuxOffset, external_aux, union aux_ext);
486 FIX (cbSsOffset, ss, char);
487 FIX (cbSsExtOffset, ssext, char);
488 FIX (cbFdOffset, external_fdr, struct fdr_ext);
489 FIX (cbRfdOffset, external_rfd, struct rfd_ext);
490 FIX (cbExtOffset, external_ext, struct ext_ext);
493 /* I don't want to always swap all the data, because it will just
494 waste time and most programs will never look at it. The only
495 time the linker needs most of the debugging information swapped
496 is when linking big-endian and little-endian MIPS object files
497 together, which is not a common occurrence.
499 We need to look at the fdr to deal with a lot of information in
500 the symbols, so we swap them here. */
501 ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
502 (internal_symhdr->ifdMax *
503 sizeof (struct fdr)));
504 if (ecoff_data (abfd)->fdr == NULL)
506 bfd_error = no_memory;
509 fdr_ptr = ecoff_data (abfd)->fdr;
510 fraw_src = ecoff_data (abfd)->external_fdr;
511 fraw_end = fraw_src + internal_symhdr->ifdMax;
512 for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
513 ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
518 /* ECOFF symbol table routines. The ECOFF symbol table is described
519 in gcc/mips-tfile.c. */
521 /* Create an empty symbol. */
524 DEFUN (ecoff_make_empty_symbol, (abfd),
527 ecoff_symbol_type *new;
529 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
530 if (new == (ecoff_symbol_type *) NULL)
532 bfd_error = no_memory;
533 return (asymbol *) NULL;
535 new->symbol.section = (asection *) NULL;
536 new->fdr = (FDR *) NULL;
538 new->native.lnative = (struct sym_ext *) NULL;
539 new->symbol.the_bfd = abfd;
543 /* Set the BFD flags and section for an ECOFF symbol. */
546 DEFUN (ecoff_set_symbol_info, (abfd, ecoff_sym, asym, ext),
552 asym->the_bfd = abfd;
553 asym->value = ecoff_sym->value;
554 asym->section = &bfd_debug_section;
557 /* Most symbol types are just for debugging. */
558 switch (ecoff_sym->st)
568 asym->flags = BSF_DEBUGGING;
573 asym->flags = BSF_EXPORT | BSF_GLOBAL;
575 asym->flags = BSF_LOCAL;
576 switch (ecoff_sym->sc)
579 asym->flags = BSF_DEBUGGING;
582 asym->section = bfd_make_section_old_way (abfd, ".text");
583 asym->value -= asym->section->vma;
586 asym->section = bfd_make_section_old_way (abfd, ".data");
587 asym->value -= asym->section->vma;
591 asym->section = &bfd_com_section;
594 asym->section = bfd_make_section_old_way (abfd, ".bss");
595 asym->value -= asym->section->vma;
599 asym->flags = BSF_DEBUGGING;
602 asym->section = &bfd_abs_section;
605 asym->section = &bfd_und_section;
613 asym->flags = BSF_DEBUGGING;
616 asym->section = bfd_make_section_old_way (abfd, ".sdata");
617 asym->value -= asym->section->vma;
620 /* FIXME: putting global .sbss symbols in the common section is
621 wrong, because it means they may not be accessible via the gp
622 register. But the linker checks the section a symbol is in
623 rather than checking the flags. */
625 asym->section = &bfd_com_section;
628 asym->section = bfd_make_section_old_way (abfd, ".sbss");
629 asym->value -= asym->section->vma;
633 asym->section = bfd_make_section_old_way (abfd, ".rdata");
634 asym->value -= asym->section->vma;
637 asym->flags = BSF_DEBUGGING;
641 asym->section = &bfd_com_section;
645 asym->flags = BSF_DEBUGGING;
648 asym->section = &bfd_und_section;
651 asym->section = bfd_make_section_old_way (abfd, ".init");
652 asym->value -= asym->section->vma;
657 asym->flags = BSF_DEBUGGING;
660 asym->section = bfd_make_section_old_way (abfd, ".fini");
661 asym->value -= asym->section->vma;
668 /* Read an ECOFF symbol table. */
671 DEFUN (ecoff_slurp_symbol_table, (abfd),
674 bfd_size_type internal_size;
675 ecoff_symbol_type *internal;
676 ecoff_symbol_type *internal_ptr;
677 struct ext_ext *eraw_src;
678 struct ext_ext *eraw_end;
682 /* If we've already read in the symbol table, do nothing. */
683 if (ecoff_data (abfd)->canonical_symbols != NULL)
686 /* Get the symbolic information. */
687 if (ecoff_slurp_symbolic_info (abfd) == false)
689 if (bfd_get_symcount (abfd) == 0)
692 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
693 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
694 if (internal == NULL)
696 bfd_error = no_memory;
700 internal_ptr = internal;
701 eraw_src = ecoff_data (abfd)->external_ext;
702 eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
703 for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
707 ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
708 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
709 + internal_esym.asym.iss);
710 ecoff_set_symbol_info (abfd, &internal_esym.asym,
711 &internal_ptr->symbol, 1);
712 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
713 internal_ptr->local = false;
714 internal_ptr->native.enative = eraw_src;
717 /* The local symbols must be accessed via the fdr's, because the
718 string and aux indices are relative to the fdr information. */
719 fdr_ptr = ecoff_data (abfd)->fdr;
720 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
721 for (; fdr_ptr < fdr_end; fdr_ptr++)
723 struct sym_ext *lraw_src;
724 struct sym_ext *lraw_end;
726 lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
727 lraw_end = lraw_src + fdr_ptr->csym;
728 for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
732 ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
733 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
736 ecoff_set_symbol_info (abfd, &internal_sym,
737 &internal_ptr->symbol, 0);
738 internal_ptr->fdr = fdr_ptr;
739 internal_ptr->local = true;
740 internal_ptr->native.lnative = lraw_src;
744 ecoff_data (abfd)->canonical_symbols = internal;
750 DEFUN (ecoff_get_symtab_upper_bound, (abfd),
753 if (ecoff_slurp_symbolic_info (abfd) == false
754 || bfd_get_symcount (abfd) == 0)
757 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
761 DEFUN (ecoff_get_symtab, (abfd, alocation),
765 unsigned int counter = 0;
766 ecoff_symbol_type *symbase;
767 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
769 if (ecoff_slurp_symbol_table (abfd) == false
770 || bfd_get_symcount (abfd) == 0)
773 symbase = ecoff_data (abfd)->canonical_symbols;
774 while (counter < bfd_get_symcount (abfd))
776 *(location++) = symbase++;
779 *location++ = (ecoff_symbol_type *) NULL;
780 return bfd_get_symcount (abfd);
783 /* Turn ECOFF type information into a printable string.
784 emit_aggregate and type_to_string are from gcc/mips-tdump.c, with
785 swapping added and used_ptr removed. */
787 /* Write aggregate information to a string. */
790 DEFUN (emit_aggregate, (abfd, string, rndx, isym, which),
798 int indx = rndx->index;
799 int sym_base, ss_base;
805 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
806 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
808 if (indx == indexNil)
809 name = "/* no name */";
815 ecoff_swap_sym_in (abfd,
816 ecoff_data (abfd)->external_sym + indx,
818 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
822 "%s %s { ifd = %d, index = %d }",
824 indx + ecoff_data (abfd)->symbolic_header.iextMax);
827 /* Convert the type information to string format. */
830 DEFUN (type_to_string, (abfd, aux_ptr, indx, bigendian),
832 union aux_ext *aux_ptr AND
844 unsigned int basic_type;
846 static char buffer1[1024];
847 static char buffer2[1024];
852 for (i = 0; i < 7; i++)
854 qualifiers[i].low_bound = 0;
855 qualifiers[i].high_bound = 0;
856 qualifiers[i].stride = 0;
859 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
860 return "-1 (no type)";
861 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
863 basic_type = u.ti.bt;
864 qualifiers[0].type = u.ti.tq0;
865 qualifiers[1].type = u.ti.tq1;
866 qualifiers[2].type = u.ti.tq2;
867 qualifiers[3].type = u.ti.tq3;
868 qualifiers[4].type = u.ti.tq4;
869 qualifiers[5].type = u.ti.tq5;
870 qualifiers[6].type = tqNil;
873 * Go get the basic type.
877 case btNil: /* undefined */
881 case btAdr: /* address - integer same size as pointer */
882 strcpy (p1, "address");
885 case btChar: /* character */
889 case btUChar: /* unsigned character */
890 strcpy (p1, "unsigned char");
893 case btShort: /* short */
894 strcpy (p1, "short");
897 case btUShort: /* unsigned short */
898 strcpy (p1, "unsigned short");
901 case btInt: /* int */
905 case btUInt: /* unsigned int */
906 strcpy (p1, "unsigned int");
909 case btLong: /* long */
913 case btULong: /* unsigned long */
914 strcpy (p1, "unsigned long");
917 case btFloat: /* float (real) */
918 strcpy (p1, "float");
921 case btDouble: /* Double (real) */
922 strcpy (p1, "double");
925 /* Structures add 1-2 aux words:
926 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
927 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
929 case btStruct: /* Structure (Record) */
930 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
931 emit_aggregate (abfd, p1, &rndx,
932 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
934 indx++; /* skip aux words */
937 /* Unions add 1-2 aux words:
938 1st word is [ST_RFDESCAPE, offset] pointer to union def;
939 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
941 case btUnion: /* Union */
942 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
943 emit_aggregate (abfd, p1, &rndx,
944 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
946 indx++; /* skip aux words */
949 /* Enumerations add 1-2 aux words:
950 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
951 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
953 case btEnum: /* Enumeration */
954 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
955 emit_aggregate (abfd, p1, &rndx,
956 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
958 indx++; /* skip aux words */
961 case btTypedef: /* defined via a typedef, isymRef points */
962 strcpy (p1, "typedef");
965 case btRange: /* subrange of int */
966 strcpy (p1, "subrange");
969 case btSet: /* pascal sets */
973 case btComplex: /* fortran complex */
974 strcpy (p1, "complex");
977 case btDComplex: /* fortran double complex */
978 strcpy (p1, "double complex");
981 case btIndirect: /* forward or unnamed typedef */
982 strcpy (p1, "forward/unamed typedef");
985 case btFixedDec: /* Fixed Decimal */
986 strcpy (p1, "fixed decimal");
989 case btFloatDec: /* Float Decimal */
990 strcpy (p1, "float decimal");
993 case btString: /* Varying Length Character String */
994 strcpy (p1, "string");
997 case btBit: /* Aligned Bit String */
1001 case btPicture: /* Picture */
1002 strcpy (p1, "picture");
1005 case btVoid: /* Void */
1006 strcpy (p1, "void");
1010 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1014 p1 += strlen (buffer1);
1017 * If this is a bitfield, get the bitsize.
1023 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1024 sprintf (p1, " : %d", bitsize);
1025 p1 += strlen (buffer1);
1030 * Deal with any qualifiers.
1032 if (qualifiers[0].type != tqNil)
1035 * Snarf up any array bounds in the correct order. Arrays
1036 * store 5 successive words in the aux. table:
1037 * word 0 RNDXR to type of the bounds (ie, int)
1038 * word 1 Current file descriptor index
1040 * word 3 high bound (or -1 if [])
1041 * word 4 stride size in bits
1043 for (i = 0; i < 7; i++)
1045 if (qualifiers[i].type == tqArray)
1047 qualifiers[i].low_bound =
1048 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1049 qualifiers[i].high_bound =
1050 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1051 qualifiers[i].stride =
1052 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1058 * Now print out the qualifiers.
1060 for (i = 0; i < 6; i++)
1062 switch (qualifiers[i].type)
1069 strcpy (p2, "ptr to ");
1070 p2 += sizeof ("ptr to ")-1;
1074 strcpy (p2, "volatile ");
1075 p2 += sizeof ("volatile ")-1;
1079 strcpy (p2, "far ");
1080 p2 += sizeof ("far ")-1;
1084 strcpy (p2, "func. ret. ");
1085 p2 += sizeof ("func. ret. ");
1090 int first_array = i;
1093 /* Print array bounds reversed (ie, in the order the C
1094 programmer writes them). C is such a fun language.... */
1096 while (i < 5 && qualifiers[i+1].type == tqArray)
1099 for (j = i; j >= first_array; j--)
1101 strcpy (p2, "array [");
1102 p2 += sizeof ("array [")-1;
1103 if (qualifiers[j].low_bound != 0)
1105 "%ld:%ld {%ld bits}",
1106 (long) qualifiers[j].low_bound,
1107 (long) qualifiers[j].high_bound,
1108 (long) qualifiers[j].stride);
1110 else if (qualifiers[j].high_bound != -1)
1113 (long) (qualifiers[j].high_bound + 1),
1114 (long) (qualifiers[j].stride));
1117 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1120 strcpy (p2, "] of ");
1121 p2 += sizeof ("] of ")-1;
1129 strcpy (p2, buffer1);
1133 /* Print information about an ECOFF symbol. */
1136 DEFUN (ecoff_print_symbol, (abfd, filep, symbol, how),
1140 bfd_print_symbol_type how)
1142 FILE *file = (FILE *)filep;
1146 case bfd_print_symbol_name:
1147 fprintf (file, "%s", symbol->name);
1149 case bfd_print_symbol_more:
1150 if (ecoffsymbol (symbol)->local)
1154 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1156 fprintf (file, "ecoff local %lx %x %x",
1157 (unsigned long) ecoff_sym.value,
1158 (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
1164 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1166 fprintf (file, "ecoff extern %lx %x %x",
1167 (unsigned long) ecoff_ext.asym.value,
1168 (unsigned) ecoff_ext.asym.st,
1169 (unsigned) ecoff_ext.asym.sc);
1172 case bfd_print_symbol_nm:
1174 CONST char *section_name = symbol->section->name;
1176 bfd_print_symbol_vandf ((PTR) file, symbol);
1177 fprintf (file, " %-5s %s %s",
1179 ecoffsymbol (symbol)->local ? "l" : "e",
1183 case bfd_print_symbol_all:
1184 /* Print out the symbols in a reasonable way */
1193 if (ecoffsymbol (symbol)->local)
1195 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1198 pos = (ecoffsymbol (symbol)->native.lnative
1199 - ecoff_data (abfd)->external_sym
1200 + ecoff_data (abfd)->symbolic_header.iextMax);
1207 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1210 pos = (ecoffsymbol (symbol)->native.enative
1211 - ecoff_data (abfd)->external_ext);
1212 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1213 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1214 weakext = ecoff_ext.weakext ? 'w' : ' ';
1217 fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
1218 pos, type, (unsigned long) ecoff_ext.asym.value,
1219 (unsigned) ecoff_ext.asym.st,
1220 (unsigned) ecoff_ext.asym.sc,
1221 (unsigned) ecoff_ext.asym.index,
1222 jmptbl, cobol_main, weakext,
1225 if (ecoffsymbol (symbol)->fdr != NULL
1226 && ecoff_ext.asym.index != indexNil)
1231 union aux_ext *aux_base;
1233 indx = ecoff_ext.asym.index;
1235 /* sym_base is used to map the fdr relative indices which
1236 appear in the file to the position number which we are
1238 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1239 if (ecoffsymbol (symbol)->local)
1240 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1242 /* aux_base is the start of the aux entries for this file;
1243 asym.index is an offset from this. */
1244 aux_base = (ecoff_data (abfd)->external_aux
1245 + ecoffsymbol (symbol)->fdr->iauxBase);
1247 /* The aux entries are stored in host byte order; the
1248 order is indicated by a bit in the fdr. */
1249 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1251 /* This switch is basically from gcc/mips-tdump.c */
1252 switch (ecoff_ext.asym.st)
1260 printf ("\n End+1 symbol: %ld", indx + sym_base);
1264 if (ecoff_ext.asym.sc == scText
1265 || ecoff_ext.asym.sc == scInfo)
1266 printf ("\n First symbol: %ld", indx + sym_base);
1268 printf ("\n First symbol: %ld",
1269 (AUX_GET_ISYM (bigendian,
1270 &aux_base[ecoff_ext.asym.index])
1276 if (MIPS_IS_STAB (&ecoff_ext.asym))
1278 else if (ecoffsymbol (symbol)->local)
1279 printf ("\n End+1 symbol: %-7ld Type: %s",
1280 (AUX_GET_ISYM (bigendian,
1281 &aux_base[ecoff_ext.asym.index])
1283 type_to_string (abfd, aux_base, indx + 1,
1286 printf ("\n Local symbol: %d",
1289 + ecoff_data (abfd)->symbolic_header.iextMax));
1293 if (!MIPS_IS_STAB (&ecoff_ext.asym))
1294 printf ("\n Type: %s",
1295 type_to_string (abfd, aux_base, indx, bigendian));
1304 /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
1305 external form. They use a bit which indicates whether the symbol
1308 /* Swap a reloc in. */
1311 DEFUN (ecoff_swap_reloc_in, (abfd, ext, intern),
1314 struct internal_reloc *intern)
1316 intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
1317 if (abfd->xvec->header_byteorder_big_p != false)
1319 intern->r_symndx = (((int) ext->r_bits[0]
1320 << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
1321 | ((int) ext->r_bits[1]
1322 << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
1323 | ((int) ext->r_bits[2]
1324 << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
1325 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
1326 >> RELOC_BITS3_TYPE_SH_BIG);
1327 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
1331 intern->r_symndx = (((int) ext->r_bits[0]
1332 << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
1333 | ((int) ext->r_bits[1]
1334 << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
1335 | ((int) ext->r_bits[2]
1336 << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
1337 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
1338 >> RELOC_BITS3_TYPE_SH_LITTLE);
1339 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
1343 /* Swap a reloc out. */
1346 DEFUN (ecoff_swap_reloc_out, (abfd, src, dst),
1351 struct internal_reloc *intern = (struct internal_reloc *) src;
1352 RELOC *ext = (RELOC *) dst;
1354 bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
1355 if (abfd->xvec->header_byteorder_big_p != false)
1357 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
1358 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
1359 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
1360 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
1361 & RELOC_BITS3_TYPE_BIG)
1362 | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
1366 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
1367 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
1368 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
1369 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
1370 & RELOC_BITS3_TYPE_LITTLE)
1371 | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
1377 /* Do a REFHI relocation. The next reloc must be the corresponding
1378 REFLO. This has to be done in a function so that carry is handled
1381 static bfd_reloc_status_type
1382 DEFUN (ecoff_refhi_reloc, (abfd,
1389 arelent *reloc_entry AND
1392 asection *input_section AND
1395 bfd_reloc_status_type ret;
1400 unsigned long vallo;
1403 if (symbol->section == &bfd_und_section
1404 && output_bfd == (bfd *) NULL)
1405 ret = bfd_reloc_undefined;
1407 rello = reloc_entry + 1;
1408 BFD_ASSERT (rello->howto->type == ECOFF_R_REFLO
1409 && *rello->sym_ptr_ptr == *reloc_entry->sym_ptr_ptr);
1411 if (symbol->section == &bfd_com_section)
1414 relocation = symbol->value;
1416 if (output_bfd == (bfd *) NULL)
1418 relocation += symbol->section->output_section->vma;
1419 relocation += symbol->section->output_offset;
1422 relocation += reloc_entry->addend;
1424 if (reloc_entry->address > input_section->_cooked_size)
1425 return bfd_reloc_outofrange;
1427 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1428 vallo = bfd_get_32 (abfd, (bfd_byte *) data + rello->address) & 0xffff;
1429 val = ((insn & 0xffff) << 16) + vallo;
1432 /* The low order 16 bits are always treated as a signed value.
1433 Therefore, a negative value in the low order bits requires an
1434 adjustment in the high order bits. We need to make this
1435 adjustment in two ways: once for the bits we took from the data,
1436 and once for the bits we are putting back in to the data. */
1437 if ((vallo & 0x8000) != 0)
1439 if ((val & 0x8000) != 0)
1442 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
1443 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1445 if (output_bfd != (bfd *) NULL)
1446 reloc_entry->address += input_section->output_offset;
1451 /* Do a GPREL relocation. This is a 16 bit value which must become
1452 the offset from the gp register. */
1454 static bfd_reloc_status_type
1455 DEFUN (ecoff_gprel_reloc, (abfd,
1462 arelent *reloc_entry AND
1465 asection *input_section AND
1468 boolean relocateable;
1473 if (output_bfd != (bfd *) NULL)
1474 relocateable = true;
1477 relocateable = false;
1478 output_bfd = symbol->section->output_section->owner;
1481 if (symbol->section == &bfd_und_section
1482 && relocateable == false)
1483 return bfd_reloc_undefined;
1485 /* We have to figure out the gp value, so that we can adjust the
1486 symbol value correctly. We look up the symbol _gp in the output
1487 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1489 if (ecoff_data (output_bfd)->gp == 0)
1493 /* Make up a value. */
1494 ecoff_data (output_bfd)->gp =
1495 symbol->section->output_section->vma + 0x4000;
1503 count = bfd_get_symcount (output_bfd);
1504 sym = bfd_get_outsymbols (output_bfd);
1506 /* We should do something more friendly here, but we don't
1507 have a good reloc status to return. */
1508 if (sym == (asymbol **) NULL)
1511 for (i = 0; i < count; i++, sym++)
1513 register CONST char *name;
1515 name = bfd_asymbol_name (*sym);
1516 if (*name == '_' && strcmp (name, "_gp") == 0)
1518 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1523 /* We should do something more friendly here, but we don't have
1524 a good reloc status to return. */
1530 if (symbol->section == &bfd_com_section)
1533 relocation = symbol->value;
1535 relocation += symbol->section->output_section->vma;
1536 relocation += symbol->section->output_offset;
1538 if (reloc_entry->address > input_section->_cooked_size)
1539 return bfd_reloc_outofrange;
1541 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1543 /* Set val to the offset into the section (if we are relocating
1544 against an external symbol, insn & 0xffff will be zero and so
1545 will reloc_entry->addend). */
1546 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1550 /* Adjust val for the final section location and GP value. */
1551 val += (relocation - ecoff_data (output_bfd)->gp);
1553 insn = (insn &~ 0xffff) | (val & 0xffff);
1554 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1557 reloc_entry->address += input_section->output_offset;
1559 /* Make sure it fit in 16 bits. */
1560 if (val >= 0x8000 && val < 0xffff8000)
1561 return bfd_reloc_outofrange;
1563 return bfd_reloc_ok;
1566 /* How to process the various relocs types. */
1568 static reloc_howto_type ecoff_howto_table[] =
1570 /* Reloc type 0 is ignored. The reloc reading code ensures that
1571 this is a reference to the .abs section, which will cause
1572 bfd_perform_relocation to do nothing. */
1573 HOWTO (ECOFF_R_IGNORE, /* type */
1575 0, /* size (0 = byte, 1 = short, 2 = long) */
1576 8, /* bitsize (obsolete) */
1577 false, /* pc_relative */
1579 false, /* absolute (obsolete) */
1580 false, /* complain_on_overflow */
1581 0, /* special_function */
1582 "IGNORE", /* name */
1583 false, /* partial_inplace */
1586 false), /* pcrel_offset */
1588 /* A 16 bit reference to a symbol, normally from a data section. */
1589 HOWTO (ECOFF_R_REFHALF, /* type */
1591 1, /* size (0 = byte, 1 = short, 2 = long) */
1592 16, /* bitsize (obsolete) */
1593 false, /* pc_relative */
1595 false, /* absolute (obsolete) */
1596 true, /* complain_on_overflow */
1597 0, /* special_function */
1598 "REFHALF", /* name */
1599 true, /* partial_inplace */
1600 0xffff, /* src_mask */
1601 0xffff, /* dst_mask */
1602 false), /* pcrel_offset */
1604 /* A 32 bit reference to a symbol, normally from a data section. */
1605 HOWTO (ECOFF_R_REFWORD, /* type */
1607 2, /* size (0 = byte, 1 = short, 2 = long) */
1608 32, /* bitsize (obsolete) */
1609 false, /* pc_relative */
1611 false, /* absolute (obsolete) */
1612 true, /* complain_on_overflow */
1613 0, /* special_function */
1614 "REFWORD", /* name */
1615 true, /* partial_inplace */
1616 0xffffffff, /* src_mask */
1617 0xffffffff, /* dst_mask */
1618 false), /* pcrel_offset */
1620 /* A 26 bit absolute jump address. */
1621 HOWTO (ECOFF_R_JMPADDR, /* type */
1623 2, /* size (0 = byte, 1 = short, 2 = long) */
1624 32, /* bitsize (obsolete) */
1625 false, /* pc_relative */
1627 false, /* absolute (obsolete) */
1628 true, /* complain_on_overflow */
1629 0, /* special_function */
1630 "JMPADDR", /* name */
1631 true, /* partial_inplace */
1632 0x3ffffff, /* src_mask */
1633 0x3ffffff, /* dst_mask */
1634 false), /* pcrel_offset */
1636 /* The high 16 bits of a symbol value. Handled by the function
1637 ecoff_refhi_reloc. */
1638 HOWTO (ECOFF_R_REFHI, /* type */
1639 16, /* rightshift */
1640 2, /* size (0 = byte, 1 = short, 2 = long) */
1641 32, /* bitsize (obsolete) */
1642 false, /* pc_relative */
1644 false, /* absolute (obsolete) */
1645 true, /* complain_on_overflow */
1646 ecoff_refhi_reloc, /* special_function */
1648 true, /* partial_inplace */
1649 0xffff, /* src_mask */
1650 0xffff, /* dst_mask */
1651 false), /* pcrel_offset */
1653 /* The low 16 bits of a symbol value. */
1654 HOWTO (ECOFF_R_REFLO, /* type */
1656 2, /* size (0 = byte, 1 = short, 2 = long) */
1657 32, /* bitsize (obsolete) */
1658 false, /* pc_relative */
1660 false, /* absolute (obsolete) */
1661 true, /* complain_on_overflow */
1662 0, /* special_function */
1664 true, /* partial_inplace */
1665 0xffff, /* src_mask */
1666 0xffff, /* dst_mask */
1667 false), /* pcrel_offset */
1669 /* A reference to an offset from the gp register. Handled by the
1670 function ecoff_gprel_reloc. */
1671 HOWTO (ECOFF_R_GPREL, /* type */
1673 2, /* size (0 = byte, 1 = short, 2 = long) */
1674 32, /* bitsize (obsolete) */
1675 false, /* pc_relative */
1677 false, /* absolute (obsolete) */
1678 true, /* complain_on_overflow */
1679 ecoff_gprel_reloc, /* special_function */
1681 true, /* partial_inplace */
1682 0xffff, /* src_mask */
1683 0xffff, /* dst_mask */
1684 false), /* pcrel_offset */
1686 /* A reference to a literal using an offset from the gp register.
1687 Handled by the function ecoff_gprel_reloc. */
1688 HOWTO (ECOFF_R_LITERAL, /* type */
1690 2, /* size (0 = byte, 1 = short, 2 = long) */
1691 32, /* bitsize (obsolete) */
1692 false, /* pc_relative */
1694 false, /* absolute (obsolete) */
1695 true, /* complain_on_overflow */
1696 ecoff_gprel_reloc, /* special_function */
1697 "LITERAL", /* name */
1698 true, /* partial_inplace */
1699 0xffff, /* src_mask */
1700 0xffff, /* dst_mask */
1701 false) /* pcrel_offset */
1704 /* Read in the relocs for a section. */
1707 DEFUN (ecoff_slurp_reloc_table, (abfd, section, symbols),
1709 asection *section AND
1712 RELOC *external_relocs;
1713 arelent *internal_relocs;
1717 if (section->relocation != (arelent *) NULL
1718 || section->reloc_count == 0
1719 || (section->flags & SEC_CONSTRUCTOR) != 0)
1722 if (ecoff_slurp_symbol_table (abfd) == false)
1725 internal_relocs = (arelent *) bfd_alloc (abfd,
1727 * section->reloc_count));
1728 external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
1729 if (internal_relocs == (arelent *) NULL
1730 || external_relocs == (RELOC *) NULL)
1732 bfd_error = no_memory;
1735 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1737 if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
1738 != RELSZ * section->reloc_count)
1740 bfd_error = system_call_error;
1744 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1746 struct internal_reloc intern;
1748 ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
1750 if (intern.r_type > ECOFF_R_LITERAL)
1753 if (intern.r_extern)
1755 /* r_symndx is an index into the external symbols. */
1756 BFD_ASSERT (intern.r_symndx >= 0
1758 < ecoff_data (abfd)->symbolic_header.iextMax));
1759 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1764 CONST char *sec_name;
1767 /* r_symndx is a section key. */
1768 switch (intern.r_symndx)
1770 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1771 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1772 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1773 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1774 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1775 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1776 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1777 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1778 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1782 sec = bfd_get_section_by_name (abfd, sec_name);
1783 if (sec == (asection *) NULL)
1785 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1787 rptr->addend = - bfd_get_section_vma (abfd, sec);
1788 if (intern.r_type == ECOFF_R_GPREL
1789 || intern.r_type == ECOFF_R_LITERAL)
1790 rptr->addend += ecoff_data (abfd)->gp;
1793 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1794 rptr->howto = &ecoff_howto_table[intern.r_type];
1796 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
1797 to the absolute section so that the reloc is ignored. */
1798 if (intern.r_type == ECOFF_R_IGNORE)
1799 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1802 bfd_release (abfd, external_relocs);
1804 section->relocation = internal_relocs;
1809 /* Get a canonical list of relocs. */
1812 DEFUN (ecoff_canonicalize_reloc, (abfd, section, relptr, symbols),
1814 asection *section AND
1815 arelent **relptr AND
1820 if (section->flags & SEC_CONSTRUCTOR)
1822 arelent_chain *chain;
1824 /* This section has relocs made up by us, not the file, so take
1825 them out of their chain and place them into the data area
1827 for (count = 0, chain = section->constructor_chain;
1828 count < section->reloc_count;
1829 count++, chain = chain->next)
1830 *relptr++ = &chain->relent;
1836 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1839 tblptr = section->relocation;
1840 if (tblptr == (arelent *) NULL)
1843 for (count = 0; count < section->reloc_count; count++)
1844 *relptr++ = tblptr++;
1847 *relptr = (arelent *) NULL;
1849 return section->reloc_count;
1852 /* Provided a BFD, a section and an offset into the section, calculate
1853 and return the name of the source file and the line nearest to the
1857 DEFUN (ecoff_find_nearest_line, (abfd,
1865 asection *section AND
1866 asymbol **ignore_symbols AND
1868 CONST char **filename_ptr AND
1869 CONST char **functionname_ptr AND
1870 unsigned int *retline_ptr)
1876 struct pdr_ext *pdr_ptr;
1877 struct pdr_ext *pdr_end;
1879 unsigned char *line_ptr;
1880 unsigned char *line_end;
1884 /* If we're not in the .text section, we don't have any line
1886 if (strcmp (section->name, _TEXT) != 0)
1889 /* Make sure we have the FDR's. */
1890 if (ecoff_slurp_symbolic_info (abfd) == false
1891 || bfd_get_symcount (abfd) == 0)
1894 /* Each file descriptor (FDR) has a memory address. Here we track
1895 down which FDR we want. The FDR's are stored in increasing
1896 memory order. If speed is ever important, this can become a
1897 binary search. We must ignore FDR's with no PDR entries; they
1898 will have the adr of the FDR before or after them. */
1899 fdr_start = ecoff_data (abfd)->fdr;
1900 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
1901 fdr_hold = (FDR *) NULL;
1902 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1904 if (offset < fdr_ptr->adr)
1906 if (fdr_ptr->cpd > 0)
1909 if (fdr_hold == (FDR *) NULL)
1913 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
1914 have an address, which is relative to the FDR address, and are
1915 also stored in increasing memory order. */
1916 offset -= fdr_ptr->adr;
1917 pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
1918 pdr_end = pdr_ptr + fdr_ptr->cpd;
1919 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1920 if (offset < pdr.adr)
1922 for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
1924 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1925 if (offset < pdr.adr)
1929 /* Now we can look for the actual line number. The line numbers are
1930 stored in a very funky format, which I won't try to describe.
1931 Note that right here pdr_ptr and pdr hold the PDR *after* the one
1932 we want; we need this to compute line_end. */
1933 line_end = ecoff_data (abfd)->line;
1934 if (pdr_ptr == pdr_end)
1935 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
1937 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
1939 /* Now change pdr and pdr_ptr to the one we want. */
1941 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1945 line_ptr = (ecoff_data (abfd)->line
1946 + fdr_ptr->cbLineOffset
1947 + pdr.cbLineOffset);
1948 while (line_ptr < line_end)
1953 delta = *line_ptr >> 4;
1956 count = (*line_ptr & 0xf) + 1;
1960 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
1961 if (delta >= 0x8000)
1966 if (offset < count * 4)
1968 offset -= count * 4;
1971 /* If offset is too large, this line is not interesting. */
1975 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
1976 ecoff_swap_sym_in (abfd,
1977 (ecoff_data (abfd)->external_sym
1981 *functionname_ptr = ecoff_data (abfd)->ss + proc_sym.iss;
1982 *retline_ptr = lineno;
1986 /* We can't use the generic linking routines for ECOFF, because we
1987 have to handle all the debugging information. The generic link
1988 routine just works out the section contents and attaches a list of
1991 We link by looping over all the seclets. We make two passes. On
1992 the first we set the actual section contents and determine the size
1993 of the debugging information. On the second we accumulate the
1994 debugging information and write it out.
1996 This currently always accumulates the debugging information, which
1997 is incorrect, because it ignores the -s and -S options of the
1998 linker. The linker needs to be modified to give us that
1999 information in a more useful format (currently it just provides a
2000 list of symbols which should appear in the output file). */
2002 /* Clear the output_has_begun flag for all the input BFD's. We use it
2003 to avoid linking in the debugging information for a BFD more than
2007 DEFUN (ecoff_clear_output_flags, (abfd),
2010 register asection *o;
2011 register bfd_seclet_type *p;
2013 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2014 for (p = o->seclets_head;
2015 p != (bfd_seclet_type *) NULL;
2017 if (p->type == bfd_indirect_seclet)
2018 p->u.indirect.section->owner->output_has_begun = false;
2021 /* Handle an indirect seclet on the first pass. Set the contents of
2022 the output section, and accumulate the debugging information if
2026 DEFUN (ecoff_rel, (output_bfd, seclet, output_section, data, relocateable),
2028 bfd_seclet_type *seclet AND
2029 asection *output_section AND
2031 boolean relocateable)
2034 HDRR *output_symhdr;
2036 ecoff_symbol_type *sym_ptr;
2037 ecoff_symbol_type *sym_end;
2039 if ((output_section->flags & SEC_HAS_CONTENTS)
2040 && !(output_section->flags & SEC_NEVER_LOAD)
2041 && (output_section->flags & SEC_LOAD)
2044 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2048 if (bfd_set_section_contents (output_bfd,
2059 input_bfd = seclet->u.indirect.section->owner;
2061 /* We want to figure out how much space will be required to
2062 incorporate all the debugging information from input_bfd. We use
2063 the output_has_begun field to avoid adding it in more than once.
2064 The actual incorporation is done in the second pass, in
2065 ecoff_get_debug. The code has to parallel that code in its
2066 manipulations of output_symhdr. */
2068 if (input_bfd->output_has_begun)
2070 input_bfd->output_has_begun = true;
2072 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2074 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2080 /* We just accumulate local symbols from a non-ECOFF BFD. The
2081 external symbols are handled separately. */
2083 symbols = (asymbol **) bfd_alloc (output_bfd,
2084 get_symtab_upper_bound (input_bfd));
2085 if (symbols == (asymbol **) NULL)
2087 bfd_error = no_memory;
2090 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2092 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2096 len = strlen ((*sym_ptr)->name);
2097 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2099 ++output_symhdr->isymMax;
2100 output_symhdr->issMax += len + 1;
2104 bfd_release (output_bfd, (PTR) symbols);
2106 ++output_symhdr->ifdMax;
2111 /* We simply add in the information from another ECOFF BFD. First
2112 we make sure we have the symbolic information. */
2113 if (ecoff_slurp_symbol_table (input_bfd) == false)
2115 if (bfd_get_symcount (input_bfd) == 0)
2118 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2120 /* Figure out how much information we are going to be putting in.
2121 The external symbols are handled separately. */
2122 output_symhdr->ilineMax += input_symhdr->ilineMax;
2123 output_symhdr->cbLine += input_symhdr->cbLine;
2124 output_symhdr->idnMax += input_symhdr->idnMax;
2125 output_symhdr->ipdMax += input_symhdr->ipdMax;
2126 output_symhdr->isymMax += input_symhdr->isymMax;
2127 output_symhdr->ioptMax += input_symhdr->ioptMax;
2128 output_symhdr->iauxMax += input_symhdr->iauxMax;
2129 output_symhdr->issMax += input_symhdr->issMax;
2130 output_symhdr->ifdMax += input_symhdr->ifdMax;
2132 /* The RFD's are special, since we create them if needed. */
2133 if (input_symhdr->crfd > 0)
2134 output_symhdr->crfd += input_symhdr->crfd;
2136 output_symhdr->crfd += input_symhdr->ifdMax;
2141 /* Handle an arbitrary seclet on the first pass. */
2144 DEFUN (ecoff_dump_seclet, (abfd, seclet, section, data, relocateable),
2146 bfd_seclet_type *seclet AND
2147 asection *section AND
2149 boolean relocateable)
2151 switch (seclet->type)
2153 case bfd_indirect_seclet:
2154 /* The contents of this section come from another one somewhere
2156 return ecoff_rel (abfd, seclet, section, data, relocateable);
2158 case bfd_fill_seclet:
2159 /* Fill in the section with fill.value. This is used to pad out
2160 sections, but we must avoid padding the .bss section. */
2161 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2163 if (seclet->u.fill.value != 0)
2168 char *d = (char *) bfd_alloc (abfd, seclet->size);
2172 for (i = 0; i < seclet->size; i+=2)
2173 d[i] = seclet->u.fill.value >> 8;
2174 for (i = 1; i < seclet->size; i+=2)
2175 d[i] = seclet->u.fill.value;
2176 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2178 bfd_release (abfd, (PTR) d);
2190 /* Add a string to the debugging information we are accumulating for a
2191 file. Return the offset from the fdr string base or from the
2192 external string base. */
2195 DEFUN (ecoff_add_string, (output_bfd, fdr, string, external),
2198 CONST char *string AND
2205 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2206 len = strlen (string);
2209 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2210 ret = symhdr->issExtMax;
2211 symhdr->issExtMax += len + 1;
2215 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2217 symhdr->issMax += len + 1;
2218 fdr->cbSs += len + 1;
2223 /* Accumulate the debugging information from an input section. */
2226 DEFUN (ecoff_get_debug, (output_bfd, seclet, section),
2228 bfd_seclet_type *seclet AND
2232 HDRR *output_symhdr;
2234 ecoff_data_type *output_ecoff;
2235 ecoff_data_type *input_ecoff;
2237 struct sym_ext *sym_out;
2238 struct ext_ext *ext_out;
2239 ecoff_symbol_type *esym_ptr;
2240 ecoff_symbol_type *esym_end;
2243 struct fdr_ext *fdr_out;
2253 input_bfd = seclet->u.indirect.section->owner;
2255 /* Don't get the information more than once. */
2256 if (input_bfd->output_has_begun)
2258 input_bfd->output_has_begun = true;
2260 output_ecoff = ecoff_data (output_bfd);
2261 output_symhdr = &output_ecoff->symbolic_header;
2263 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2270 /* This is not an ECOFF BFD. Just gather the symbols. */
2272 memset (&fdr, 0, sizeof fdr);
2274 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2275 fdr.issBase = output_symhdr->issMax;
2277 fdr.rss = ecoff_add_string (output_bfd,
2279 bfd_get_filename (input_bfd),
2281 fdr.isymBase = output_symhdr->isymMax;
2283 /* Get the local symbols from the input BFD. */
2284 symbols = (asymbol **) bfd_alloc (output_bfd,
2285 get_symtab_upper_bound (input_bfd));
2286 if (symbols == (asymbol **) NULL)
2288 bfd_error = no_memory;
2291 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2293 /* Handle the local symbols. Any external symbols are handled
2296 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2300 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2302 memset (&internal_sym, 0, sizeof internal_sym);
2303 internal_sym.iss = ecoff_add_string (output_bfd,
2307 internal_sym.value = (*sym_ptr)->value;
2308 internal_sym.st = stNil;
2309 internal_sym.sc = scUndefined;
2310 internal_sym.index = indexNil;
2311 ecoff_swap_sym_out (output_bfd, &internal_sym,
2312 (output_ecoff->external_sym
2313 + output_symhdr->isymMax));
2315 ++output_symhdr->isymMax;
2318 bfd_release (output_bfd, (PTR) symbols);
2320 /* Leave everything else in the FDR zeroed out. This will cause
2321 the lang field to be langC. The fBigendian field will
2322 indicate little endian format, but it doesn't matter because
2323 it only applies to aux fields and there are none. */
2325 ecoff_swap_fdr_out (output_bfd, &fdr,
2326 (output_ecoff->external_fdr
2327 + output_symhdr->ifdMax));
2328 ++output_symhdr->ifdMax;
2332 /* This is an ECOFF BFD. We want to grab the information from
2333 input_bfd and attach it to output_bfd. */
2334 count = bfd_get_symcount (input_bfd);
2337 input_ecoff = ecoff_data (input_bfd);
2338 input_symhdr = &input_ecoff->symbolic_header;
2340 /* I think that it is more efficient to simply copy the debugging
2341 information from the input BFD to the output BFD. Because ECOFF
2342 uses relative pointers for most of the debugging information,
2343 only a little of it has to be changed at all. */
2345 /* Swap in the local symbols, adjust their values, and swap them out
2346 again. The external symbols are handled separately. */
2347 sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
2349 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2350 esym_end = esym_ptr + count;
2351 for (; esym_ptr < esym_end; esym_ptr++)
2353 if (esym_ptr->local)
2357 ecoff_swap_sym_in (input_bfd, esym_ptr->native.lnative, &sym);
2358 if (esym_ptr->symbol.section != &bfd_com_section
2359 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2360 && esym_ptr->symbol.section != &bfd_und_section)
2361 sym.value = (esym_ptr->symbol.value
2362 + esym_ptr->symbol.section->output_offset
2363 + esym_ptr->symbol.section->output_section->vma);
2364 ecoff_swap_sym_out (output_bfd, &sym, sym_out);
2369 /* That should have accounted for all the local symbols in
2371 BFD_ASSERT ((sym_out - output_ecoff->external_sym) - output_symhdr->isymMax
2372 == input_symhdr->isymMax);
2374 /* Copy the information that does not need swapping. */
2375 memcpy (output_ecoff->line + output_symhdr->cbLine,
2377 input_symhdr->cbLine * sizeof (unsigned char));
2378 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2379 input_ecoff->external_aux,
2380 input_symhdr->iauxMax * sizeof (union aux_ext));
2381 memcpy (output_ecoff->ss + output_symhdr->issMax,
2383 input_symhdr->issMax * sizeof (char));
2385 /* Some of the information may need to be swapped. */
2386 if (output_bfd->xvec->header_byteorder_big_p
2387 == input_bfd->xvec->header_byteorder_big_p)
2389 /* The two BFD's have the same endianness, so memcpy will
2391 memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
2392 input_ecoff->external_dnr,
2393 input_symhdr->idnMax * sizeof (struct dnr_ext));
2394 memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
2395 input_ecoff->external_pdr,
2396 input_symhdr->ipdMax * sizeof (struct pdr_ext));
2397 memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
2398 input_ecoff->external_opt,
2399 input_symhdr->ioptMax * sizeof (struct opt_ext));
2403 struct dnr_ext *dnr_in;
2404 struct dnr_ext *dnr_end;
2405 struct dnr_ext *dnr_out;
2406 struct pdr_ext *pdr_in;
2407 struct pdr_ext *pdr_end;
2408 struct pdr_ext *pdr_out;
2409 struct opt_ext *opt_in;
2410 struct opt_ext *opt_end;
2411 struct opt_ext *opt_out;
2413 /* The two BFD's have different endianness, so we must swap
2414 everything in and out. This code would always work, but it
2415 would be slow in the normal case. */
2416 dnr_in = input_ecoff->external_dnr;
2417 dnr_end = dnr_in + input_symhdr->idnMax;
2418 dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
2419 for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
2423 ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
2424 ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
2426 pdr_in = input_ecoff->external_pdr;
2427 pdr_end = pdr_in + input_symhdr->ipdMax;
2428 pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
2429 for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
2433 ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
2434 ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
2436 opt_in = input_ecoff->external_opt;
2437 opt_end = opt_in + input_symhdr->ioptMax;
2438 opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
2439 for (; opt_in < opt_end; opt_in++, opt_out++)
2443 ecoff_swap_opt_in (input_bfd, opt_in, &opt);
2444 ecoff_swap_opt_out (output_bfd, &opt, opt_out);
2448 /* Set ifdbase so that the external symbols know how to adjust their
2450 input_ecoff->ifdbase = output_symhdr->ifdMax;
2452 /* Step through the FDR's of input_bfd, adjust the offsets, and
2454 iss = output_symhdr->issMax;
2455 isym = output_symhdr->isymMax;
2456 iline = output_symhdr->ilineMax;
2457 iopt = output_symhdr->ioptMax;
2458 ipdr = output_symhdr->ipdMax;
2459 iaux = output_symhdr->iauxMax;
2460 irfd = output_symhdr->crfd;
2461 cbline = output_symhdr->cbLine;
2463 fdr_ptr = input_ecoff->fdr;
2464 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2465 fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
2466 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
2472 /* The memory address for this fdr is the address for the seclet
2473 plus the offset to this fdr within input_bfd. */
2474 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2476 + (fdr_ptr->adr - input_ecoff->fdr->adr));
2480 fdr.isymBase = isym;
2482 fdr.ilineBase = iline;
2484 fdr.ioptBase = iopt;
2486 fdr.ipdFirst = ipdr;
2488 fdr.iauxBase = iaux;
2493 /* If there are no RFD's, we are going to add some. We don't
2494 want to adjust irfd for this, so that all the FDR's can share
2496 if (input_symhdr->crfd == 0)
2497 fdr.crfd = input_symhdr->ifdMax;
2499 if (fdr.cbLine != 0)
2501 fdr.cbLineOffset = cbline;
2502 cbline += fdr.cbLine;
2505 ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
2508 if (input_symhdr->crfd > 0)
2510 struct rfd_ext *rfd_in;
2511 struct rfd_ext *rfd_end;
2512 struct rfd_ext *rfd_out;
2514 /* Swap and adjust the RFD's. RFD's are only created by the
2515 linker, so this will only be necessary if one of the input
2516 files is the result of a partial link. Presumably all
2517 necessary RFD's are present. */
2518 rfd_in = input_ecoff->external_rfd;
2519 rfd_end = rfd_in + input_symhdr->crfd;
2520 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2521 for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
2525 ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
2526 rfd += output_symhdr->ifdMax;
2527 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2529 output_symhdr->crfd += input_symhdr->crfd;
2533 struct rfd_ext *rfd_out;
2534 struct rfd_ext *rfd_end;
2537 /* Create RFD's. Some of the debugging information includes
2538 relative file indices. These indices are taken as indices to
2539 the RFD table if there is one, or to the global table if
2540 there is not. If we did not create RFD's, we would have to
2541 parse and adjust all the debugging information which contains
2543 rfd = output_symhdr->ifdMax;
2544 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2545 rfd_end = rfd_out + input_symhdr->ifdMax;
2546 for (; rfd_out < rfd_end; rfd_out++, rfd++)
2547 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2548 output_symhdr->crfd += input_symhdr->ifdMax;
2551 /* Combine the register masks. */
2555 output_ecoff->gprmask |= input_ecoff->gprmask;
2556 for (i = 0; i < 3; i++)
2557 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
2560 /* Update the counts. */
2561 output_symhdr->ilineMax += input_symhdr->ilineMax;
2562 output_symhdr->cbLine += input_symhdr->cbLine;
2563 output_symhdr->idnMax += input_symhdr->idnMax;
2564 output_symhdr->ipdMax += input_symhdr->ipdMax;
2565 output_symhdr->isymMax += input_symhdr->isymMax;
2566 output_symhdr->ioptMax += input_symhdr->ioptMax;
2567 output_symhdr->iauxMax += input_symhdr->iauxMax;
2568 output_symhdr->issMax += input_symhdr->issMax;
2569 output_symhdr->ifdMax += input_symhdr->ifdMax;
2571 /* Double check that the counts we got by stepping through the FDR's
2572 match the counts we got from input_symhdr. We don't check iss or
2573 cbline because they are rounded. */
2574 BFD_ASSERT (output_symhdr->isymMax == isym
2575 && output_symhdr->ilineMax == iline
2576 && output_symhdr->ioptMax == iopt
2577 && output_symhdr->ipdMax == ipdr
2578 && output_symhdr->iauxMax == iaux);
2583 /* This is the actual link routine. It makes two passes over all the
2587 DEFUN (ecoff_bfd_seclet_link, (abfd, data, relocateable),
2590 boolean relocateable)
2594 register asection *o;
2595 register bfd_seclet_type *p;
2596 asymbol **sym_ptr_ptr;
2600 /* We accumulate the debugging information counts in the symbolic
2602 symhdr = &ecoff_data (abfd)->symbolic_header;
2603 symhdr->magic = magicSym;
2604 /* FIXME: What should the version stamp be? */
2606 symhdr->ilineMax = 0;
2610 symhdr->isymMax = 0;
2611 symhdr->ioptMax = 0;
2612 symhdr->iauxMax = 0;
2614 symhdr->issExtMax = 0;
2617 symhdr->iextMax = 0;
2619 /* We need to copy over the debugging symbols from each input BFD.
2620 When we do this copying, we have to adjust the text address in
2621 the FDR structures, so we have to know the text address used for
2622 the input BFD. Since we only want to copy the symbols once per
2623 input BFD, but we are going to look at each input BFD multiple
2624 times (once for each section it provides), we arrange to always
2625 look at the text section first. That means that when we copy the
2626 debugging information, we always know the text address. So we
2627 actually do each pass in two sub passes; first the text sections,
2628 then the non-text sections. We use the output_has_begun flag to
2629 determine whether we have copied over the debugging information
2632 /* Do the first pass: set the output section contents and count the
2633 debugging information. */
2634 ecoff_clear_output_flags (abfd);
2635 for (ipass = 0; ipass < 2; ipass++)
2637 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2639 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
2640 so they are done on pass 0. For other sections the
2641 expression is true, so they are done on pass 1. */
2642 if (((o->flags & SEC_CODE) == 0) != ipass)
2645 for (p = o->seclets_head;
2646 p != (bfd_seclet_type *) NULL;
2649 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
2656 /* We handle the external symbols differently. We use the ones
2657 attached to the output_bfd. The linker will have already
2658 determined which symbols are to be attached. Here we just
2659 determine how much space we will need for them. */
2660 sym_ptr_ptr = bfd_get_outsymbols (abfd);
2661 if (sym_ptr_ptr != NULL)
2665 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
2666 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
2668 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
2669 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
2672 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
2677 /* Adjust the counts so that structures are longword aligned. */
2678 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
2679 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
2680 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
2682 /* Now the counts in symhdr are the correct size for the debugging
2683 information. We allocate the right amount of space, and reset
2684 the counts so that the second pass can use them as indices. It
2685 would be possible to output the debugging information directly to
2686 the file in pass 2, rather than to build it in memory and then
2687 write it out. Outputting to the file would require a lot of
2688 seeks and small writes, though, and I think this approach is
2690 size = (symhdr->cbLine * sizeof (unsigned char)
2691 + symhdr->idnMax * sizeof (struct dnr_ext)
2692 + symhdr->ipdMax * sizeof (struct pdr_ext)
2693 + symhdr->isymMax * sizeof (struct sym_ext)
2694 + symhdr->ioptMax * sizeof (struct opt_ext)
2695 + symhdr->iauxMax * sizeof (union aux_ext)
2696 + symhdr->issMax * sizeof (char)
2697 + symhdr->issExtMax * sizeof (char)
2698 + symhdr->ifdMax * sizeof (struct fdr_ext)
2699 + symhdr->crfd * sizeof (struct rfd_ext)
2700 + symhdr->iextMax * sizeof (struct ext_ext));
2701 raw = (char *) bfd_alloc (abfd, size);
2702 if (raw == (char *) NULL)
2704 bfd_error = no_memory;
2707 ecoff_data (abfd)->raw_size = size;
2708 ecoff_data (abfd)->raw_syments = (PTR) raw;
2710 /* Initialize the raw pointers. */
2711 #define SET(field, count, type) \
2712 ecoff_data (abfd)->field = (type *) raw; \
2713 raw += symhdr->count * sizeof (type)
2715 SET (line, cbLine, unsigned char);
2716 SET (external_dnr, idnMax, struct dnr_ext);
2717 SET (external_pdr, ipdMax, struct pdr_ext);
2718 SET (external_sym, isymMax, struct sym_ext);
2719 SET (external_opt, ioptMax, struct opt_ext);
2720 SET (external_aux, iauxMax, union aux_ext);
2721 SET (ss, issMax, char);
2722 SET (ssext, issExtMax, char);
2723 SET (external_fdr, ifdMax, struct fdr_ext);
2724 SET (external_rfd, crfd, struct rfd_ext);
2725 SET (external_ext, iextMax, struct ext_ext);
2728 /* Reset the counts so the second pass can use them to know how far
2730 symhdr->ilineMax = 0;
2734 symhdr->isymMax = 0;
2735 symhdr->ioptMax = 0;
2736 symhdr->iauxMax = 0;
2738 symhdr->issExtMax = 0;
2741 symhdr->iextMax = 0;
2743 /* Do the second pass: accumulate the debugging information. */
2744 ecoff_clear_output_flags (abfd);
2745 for (ipass = 0; ipass < 2; ipass++)
2747 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2749 if (((o->flags & SEC_CODE) == 0) != ipass)
2751 for (p = o->seclets_head;
2752 p != (bfd_seclet_type *) NULL;
2755 if (p->type == bfd_indirect_seclet)
2757 if (ecoff_get_debug (abfd, p, o) == false)
2764 /* Put in the external symbols. */
2765 sym_ptr_ptr = bfd_get_outsymbols (abfd);
2766 if (sym_ptr_ptr != NULL)
2769 struct ext_ext *external_ext;
2771 ssext = ecoff_data (abfd)->ssext;
2772 external_ext = ecoff_data (abfd)->external_ext;
2773 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
2778 sym_ptr = *sym_ptr_ptr;
2780 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
2781 || (sym_ptr->flags & BSF_LOCAL) != 0)
2784 /* The enative pointer can be NULL for a symbol created by
2785 the linker via ecoff_make_empty_symbol. */
2786 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
2787 || (((ecoff_symbol_type *) sym_ptr)->native.enative
2788 == (struct ext_ext *) NULL))
2791 esym.cobol_main = 0;
2795 /* FIXME: we can do better than this for st and sc. */
2796 esym.asym.st = stGlobal;
2797 esym.asym.sc = scAbs;
2798 esym.asym.reserved = 0;
2799 esym.asym.index = indexNil;
2803 ecoff_symbol_type *ecoff_sym_ptr;
2805 ecoff_sym_ptr = (ecoff_symbol_type *) sym_ptr;
2806 if (ecoff_sym_ptr->local)
2808 ecoff_swap_ext_in (abfd, ecoff_sym_ptr->native.enative, &esym);
2810 /* Adjust the FDR index for the symbol by that used for
2812 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
2815 esym.asym.iss = symhdr->issExtMax;
2817 if (sym_ptr->section == &bfd_com_section
2818 || sym_ptr->section == &bfd_und_section)
2819 esym.asym.value = sym_ptr->value;
2821 esym.asym.value = (sym_ptr->value
2822 + sym_ptr->section->output_offset
2823 + sym_ptr->section->output_section->vma);
2825 ecoff_swap_ext_out (abfd, &esym, external_ext + symhdr->iextMax);
2827 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
2831 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
2832 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
2836 /* Adjust the counts so that structures are longword aligned. */
2837 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
2838 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
2839 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
2844 /* Set the architecture. The only architecture we support here is
2845 mips. We set the architecture anyhow, since many callers ignore
2846 the return value. */
2849 DEFUN (ecoff_set_arch_mach, (abfd, arch, machine),
2851 enum bfd_architecture arch AND
2852 unsigned long machine)
2854 bfd_default_set_arch_mach (abfd, arch, machine);
2855 return arch == bfd_arch_mips;
2858 /* Calculate the file position for each section, and set
2862 DEFUN (ecoff_compute_section_file_positions, (abfd),
2872 if (bfd_get_start_address (abfd))
2873 abfd->flags |= EXEC_P;
2875 /* Unlike normal COFF, ECOFF always use the ``optional'' header. */
2878 sofar += abfd->section_count * SCNHSZ;
2881 for (current = abfd->sections;
2882 current != (asection *) NULL;
2883 current = current->next)
2885 /* Only deal with sections which have contents */
2886 if (! (current->flags & SEC_HAS_CONTENTS))
2889 /* On Ultrix, the data sections in an executable file must be
2890 aligned to a page boundary within the file. This does not
2891 affect the section size, though. FIXME: Does this work for
2893 if ((abfd->flags & EXEC_P) != 0
2894 && first_data != false
2895 && (current->flags & SEC_CODE) == 0)
2897 sofar = (sofar + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
2901 /* Align the sections in the file to the same boundary on
2902 which they are aligned in virtual memory. */
2904 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2906 current->filepos = sofar;
2908 sofar += current->_raw_size;
2910 /* make sure that this section is of the right size too */
2912 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2913 current->_raw_size += sofar - old_sofar;
2916 ecoff_data (abfd)->reloc_filepos = sofar;
2919 /* Set the contents of a section. */
2922 DEFUN (ecoff_set_section_contents, (abfd, section, location, offset, count),
2924 asection *section AND
2927 bfd_size_type count)
2929 if (abfd->output_has_begun == false)
2930 ecoff_compute_section_file_positions (abfd);
2932 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
2935 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
2940 /* Write out an ECOFF file. */
2943 DEFUN (ecoff_write_object_contents, (abfd),
2949 file_ptr reloc_base;
2951 unsigned long reloc_size;
2952 unsigned long text_size;
2953 unsigned long text_start;
2954 unsigned long data_size;
2955 unsigned long data_start;
2956 unsigned long bss_size;
2957 struct internal_filehdr internal_f;
2958 struct internal_aouthdr internal_a;
2961 bfd_error = system_call_error;
2963 if(abfd->output_has_begun == false)
2964 ecoff_compute_section_file_positions(abfd);
2966 if (abfd->sections != (asection *) NULL)
2967 scn_base = abfd->sections->filepos;
2970 reloc_base = ecoff_data (abfd)->reloc_filepos;
2974 for (current = abfd->sections;
2975 current != (asection *)NULL;
2976 current = current->next)
2978 current->target_index = count;
2980 if (current->reloc_count != 0)
2982 bfd_size_type relsize;
2984 current->rel_filepos = reloc_base;
2985 relsize = current->reloc_count * RELSZ;
2986 reloc_size += relsize;
2987 reloc_base += relsize;
2990 current->rel_filepos = 0;
2993 sym_base = reloc_base + reloc_size;
2995 /* At least on Ultrix, the symbol table of an executable file must
2996 be aligned to a page boundary. FIXME: Is this true on other
2998 if ((abfd->flags & EXEC_P) != 0)
2999 sym_base = (sym_base + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3001 ecoff_data (abfd)->sym_filepos = sym_base;
3009 /* Write section headers to the file. */
3011 internal_f.f_nscns = 0;
3012 if (bfd_seek (abfd, (file_ptr) (FILHSZ + AOUTSZ), SEEK_SET) != 0)
3014 for (current = abfd->sections;
3015 current != (asection *) NULL;
3016 current = current->next)
3018 struct internal_scnhdr section;
3021 ++internal_f.f_nscns;
3023 strncpy (section.s_name, current->name, sizeof section.s_name);
3025 /* FIXME: is this correct for shared libraries? I think it is
3026 but I have no platform to check. Ian Lance Taylor. */
3027 vma = bfd_get_section_vma (abfd, current);
3028 if (strcmp (current->name, _LIB) == 0)
3029 section.s_vaddr = 0;
3031 section.s_vaddr = vma;
3033 section.s_paddr = vma;
3034 section.s_size = bfd_get_section_size_before_reloc (current);
3036 /* If this section has no size or is unloadable then the scnptr
3038 if (current->_raw_size == 0
3039 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3040 section.s_scnptr = 0;
3042 section.s_scnptr = current->filepos;
3043 section.s_relptr = current->rel_filepos;
3045 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3046 object file produced by the assembler is supposed to point to
3047 information about how much room is required by objects of
3048 various different sizes. I think this only matters if we
3049 want the linker to compute the best size to use, or
3050 something. I don't know what happens if the information is
3052 section.s_lnnoptr = 0;
3054 section.s_nreloc = current->reloc_count;
3055 section.s_nlnno = 0;
3056 section.s_flags = sec_to_styp_flags (current->name, current->flags);
3061 ecoff_swap_scnhdr_out (abfd, (PTR) §ion, (PTR) &buff);
3062 if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
3066 if ((section.s_flags & STYP_TEXT) != 0)
3068 text_size += bfd_get_section_size_before_reloc (current);
3069 if (text_start == 0 || text_start > vma)
3072 else if ((section.s_flags & STYP_RDATA) != 0
3073 || (section.s_flags & STYP_DATA) != 0
3074 || (section.s_flags & STYP_LIT8) != 0
3075 || (section.s_flags & STYP_LIT4) != 0
3076 || (section.s_flags & STYP_SDATA) != 0)
3078 data_size += bfd_get_section_size_before_reloc (current);
3079 if (data_start == 0 || data_start > vma)
3082 else if ((section.s_flags & STYP_BSS) != 0
3083 || (section.s_flags & STYP_SBSS) != 0)
3084 bss_size += bfd_get_section_size_before_reloc (current);
3087 /* Set up the file header. */
3089 internal_f.f_magic = MIPS_MAGIC_2;
3092 We will NOT put a fucking timestamp in the header here. Every time you
3093 put it back, I will come in and take it out again. I'm sorry. This
3094 field does not belong here. We fill it with a 0 so it compares the
3095 same but is not a reasonable time. -- gnu@cygnus.com
3097 internal_f.f_timdat = 0;
3099 if (bfd_get_symcount (abfd) != 0)
3101 /* The ECOFF f_nsyms field is not actually the number of
3102 symbols, it's the size of symbolic information header. */
3103 internal_f.f_nsyms = sizeof (struct hdr_ext);
3104 internal_f.f_symptr = sym_base;
3108 internal_f.f_nsyms = 0;
3109 internal_f.f_symptr = 0;
3112 internal_f.f_opthdr = AOUTSZ;
3114 internal_f.f_flags = F_LNNO;
3115 if (reloc_size == 0)
3116 internal_f.f_flags |= F_RELFLG;
3117 if (bfd_get_symcount (abfd) == 0)
3118 internal_f.f_flags |= F_LSYMS;
3119 if (abfd->flags & EXEC_P)
3120 internal_f.f_flags |= F_EXEC;
3122 if (! abfd->xvec->byteorder_big_p)
3123 internal_f.f_flags |= F_AR32WR;
3125 internal_f.f_flags |= F_AR32W;
3127 /* Set up the ``optional'' header. */
3128 internal_a.magic = ZMAGIC;
3130 /* FIXME: What should this be? */
3131 internal_a.vstamp = 0;
3133 /* At least on Ultrix, these have to be rounded to page boundaries.
3134 FIXME: Is this true on other platforms? */
3135 internal_a.tsize = (text_size + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3136 internal_a.text_start = text_start &~ (PAGE_SIZE - 1);
3137 internal_a.dsize = (data_size + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3138 internal_a.data_start = data_start &~ (PAGE_SIZE - 1);
3140 /* On Ultrix, the initial portions of the .sbss and .bss segments
3141 are at the end of the data section. The bsize field in the
3142 optional header records how many bss bytes are required beyond
3143 those in the data section. The value is not rounded to a page
3145 if (bss_size < internal_a.dsize - data_size)
3148 bss_size -= internal_a.dsize - data_size;
3149 internal_a.bsize = bss_size;
3150 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
3152 internal_a.entry = bfd_get_start_address (abfd);
3154 internal_a.gp_value = ecoff_data (abfd)->gp;
3156 internal_a.gprmask = ecoff_data (abfd)->gprmask;
3157 for (i = 0; i < 3; i++)
3158 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
3160 /* Write out the file header and the optional header. */
3162 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3167 ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
3168 if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
3175 ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
3176 if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
3180 /* Write out the relocs. */
3181 for (current = abfd->sections;
3182 current != (asection *) NULL;
3183 current = current->next)
3186 arelent **reloc_ptr_ptr;
3187 arelent **reloc_end;
3190 if (current->reloc_count == 0)
3193 buff = (RELOC *) bfd_alloc (abfd, current->reloc_count * RELSZ);
3194 if (buff == (RELOC *) NULL)
3196 bfd_error = no_memory;
3200 reloc_ptr_ptr = current->orelocation;
3201 reloc_end = reloc_ptr_ptr + current->reloc_count;
3203 for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr++)
3207 struct internal_reloc in;
3209 memset (&in, 0, sizeof in);
3211 reloc = *reloc_ptr_ptr;
3212 sym = *reloc->sym_ptr_ptr;
3214 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3215 in.r_type = reloc->howto->type;
3216 if ((sym->flags & BSF_SECTION_SYM) == 0)
3218 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3225 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3226 if (strcmp (name, ".text") == 0)
3227 in.r_symndx = RELOC_SECTION_TEXT;
3228 else if (strcmp (name, ".rdata") == 0)
3229 in.r_symndx = RELOC_SECTION_RDATA;
3230 else if (strcmp (name, ".data") == 0)
3231 in.r_symndx = RELOC_SECTION_DATA;
3232 else if (strcmp (name, ".sdata") == 0)
3233 in.r_symndx = RELOC_SECTION_SDATA;
3234 else if (strcmp (name, ".sbss") == 0)
3235 in.r_symndx = RELOC_SECTION_SBSS;
3236 else if (strcmp (name, ".bss") == 0)
3237 in.r_symndx = RELOC_SECTION_BSS;
3238 else if (strcmp (name, ".init") == 0)
3239 in.r_symndx = RELOC_SECTION_INIT;
3240 else if (strcmp (name, ".lit8") == 0)
3241 in.r_symndx = RELOC_SECTION_LIT8;
3242 else if (strcmp (name, ".lit4") == 0)
3243 in.r_symndx = RELOC_SECTION_LIT4;
3249 ecoff_swap_reloc_out (abfd, (PTR) &in, (PTR) out_ptr);
3252 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
3254 if (bfd_write ((PTR) buff, RELSZ, current->reloc_count, abfd)
3255 != RELSZ * current->reloc_count)
3257 bfd_release (abfd, (PTR) buff);
3260 /* Write out the symbolic debugging information. */
3261 if (bfd_get_symcount (abfd) > 0)
3264 unsigned long sym_offset;
3265 struct hdr_ext buff;
3267 /* Set up the offsets in the symbolic header. */
3268 symhdr = &ecoff_data (abfd)->symbolic_header;
3269 sym_offset = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
3271 #define SET(offset, size, ptr) \
3272 if (symhdr->size == 0) \
3273 symhdr->offset = 0; \
3275 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3276 - (char *) ecoff_data (abfd)->raw_syments) \
3279 SET (cbLineOffset, cbLine, line);
3280 SET (cbDnOffset, idnMax, external_dnr);
3281 SET (cbPdOffset, ipdMax, external_pdr);
3282 SET (cbSymOffset, isymMax, external_sym);
3283 SET (cbOptOffset, ioptMax, external_opt);
3284 SET (cbAuxOffset, iauxMax, external_aux);
3285 SET (cbSsOffset, issMax, ss);
3286 SET (cbSsExtOffset, issExtMax, ssext);
3287 SET (cbFdOffset, ifdMax, external_fdr);
3288 SET (cbRfdOffset, crfd, external_rfd);
3289 SET (cbExtOffset, iextMax, external_ext);
3292 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3295 ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
3296 if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
3298 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3299 ecoff_data (abfd)->raw_size, abfd)
3300 != ecoff_data (abfd)->raw_size)
3307 /* Archive handling. ECOFF uses what appears to be a unique type of
3308 archive header (which I call an armap). The byte ordering of the
3309 armap and the contents are encoded in the name of the armap itself.
3310 At least for now, we only support archives with the same byte
3311 ordering in the armap and the contents.
3313 The first four bytes in the armap are the number of symbol
3314 definitions. This always seems to be a power of two, presumably
3315 because ranlib uses a hash table of some sort. I don't know what
3316 the hashing scheme is at the moment.
3318 This is followed by the symbol definitions. Each symbol definition
3319 occupies 8 bytes. The first four bytes are the offset from the
3320 start of the armap strings to the null-terminated string naming
3321 this symbol. The second four bytes are the file offset to the
3322 archive member which defines this symbol. If the second four bytes
3323 are 0, then this is not actually a symbol definition, and it should
3326 After the symbol definitions comes four bytes holding the size of
3327 the string table, followed by the string table itself. */
3329 /* The name of an archive headers looks like this:
3330 __________E[BL]E[BL]_ (with a trailing space). */
3332 #define ARMAP_BIG_ENDIAN 'B'
3333 #define ARMAP_LITTLE_ENDIAN 'L'
3334 #define ARMAP_MARKER 'E'
3335 #define ARMAP_START "__________"
3336 #define ARMAP_HEADER_MARKER_INDEX 10
3337 #define ARMAP_HEADER_ENDIAN_INDEX 11
3338 #define ARMAP_OBJECT_MARKER_INDEX 12
3339 #define ARMAP_OBJECT_ENDIAN_INDEX 13
3340 #define ARMAP_END_INDEX 14
3341 #define ARMAP_END "_ "
3343 /* Read in the armap. */
3346 DEFUN (ecoff_slurp_armap, (abfd),
3351 struct areltdata *mapdata;
3352 bfd_size_type parsed_size;
3354 struct artdata *ardata;
3357 struct symdef *symdef_ptr;
3360 /* Get the name of the first element. */
3361 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3367 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3369 /* See if the first element is an armap. */
3370 if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
3371 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3372 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3373 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3374 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3375 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3376 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3377 || strncmp (nextname + ARMAP_END_INDEX,
3378 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3380 bfd_has_map (abfd) = false;
3384 /* Make sure we have the right byte ordering. */
3385 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3386 ^ (abfd->xvec->header_byteorder_big_p != false))
3387 || ((nextname[ARMAP_OBJECT_MARKER_INDEX] == ARMAP_BIG_ENDIAN)
3388 ^ (abfd->xvec->byteorder_big_p != false)))
3390 bfd_error = wrong_format;
3394 /* Read in the armap. */
3395 ardata = bfd_ardata (abfd);
3396 mapdata = snarf_ar_hdr (abfd);
3397 if (mapdata == (struct areltdata *) NULL)
3399 parsed_size = mapdata->parsed_size;
3400 bfd_release (abfd, (PTR) mapdata);
3402 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3403 if (raw_armap == (char *) NULL)
3405 bfd_error = no_memory;
3409 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3411 bfd_error = malformed_archive;
3412 bfd_release (abfd, (PTR) raw_armap);
3416 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3418 ardata->symdef_count = 0;
3419 ardata->cache = (struct ar_cache *) NULL;
3421 /* Hack: overlay the symdefs on top of the raw archive data. This
3422 is the way do_slurp_bsd_armap works. */
3423 raw_ptr = raw_armap + LONG_SIZE;
3424 symdef_ptr = (struct symdef *) raw_ptr;
3425 ardata->symdefs = (carsym *) symdef_ptr;
3426 stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
3428 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3430 unsigned long name_offset, file_offset;
3432 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3433 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3434 if (file_offset == 0)
3436 symdef_ptr->s.name = stringbase + name_offset;
3437 symdef_ptr->file_offset = file_offset;
3439 ++ardata->symdef_count;
3442 ardata->first_file_filepos = bfd_tell (abfd);
3443 /* Pad to an even boundary. */
3444 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3446 bfd_has_map (abfd) = true;
3451 /* Write out an armap. */
3454 DEFUN (ecoff_write_armap, (abfd, elength, map, orl_count, stridx),
3456 unsigned int elength AND
3458 unsigned int orl_count AND
3461 unsigned int symdefsize;
3463 unsigned int stringsize;
3464 unsigned int mapsize;
3467 struct stat statbuf;
3473 symdefsize = orl_count * 8;
3475 stringsize = stridx + padit;
3477 /* Include 8 bytes to store symdefsize and stringsize in output. */
3478 mapsize = 4 + symdefsize + stringsize + 4;
3480 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3482 memset ((PTR) &hdr, 0, sizeof hdr);
3484 /* Work out the ECOFF armap name. */
3485 strcpy (hdr.ar_name, ARMAP_START);
3486 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3487 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3488 (abfd->xvec->header_byteorder_big_p
3490 : ARMAP_LITTLE_ENDIAN);
3491 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3492 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3493 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3494 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3496 /* Write the timestamp of the archive header to be just a little bit
3497 later than the timestamp of the file, otherwise the linker will
3498 complain that the index is out of date. */
3499 if (stat (abfd->filename, &statbuf) < 0)
3500 statbuf.st_mtime = time ((PTR) NULL);
3501 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3503 /* The DECstation uses zeroes for the uid, gid and mode of the
3505 hdr.ar_uid[0] = '0';
3506 hdr.ar_gid[0] = '0';
3507 hdr.ar_mode[0] = '0';
3509 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3511 hdr.ar_fmag[0] = '`';
3512 hdr.ar_fmag[1] = '\n';
3514 /* Turn all null bytes in the header into spaces. */
3515 for (i = 0; i < sizeof (struct ar_hdr); i++)
3516 if (((char *)(&hdr))[i] == '\0')
3517 (((char *)(&hdr))[i]) = ' ';
3519 bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd);
3521 bfd_h_put_32 (abfd, symdefsize, temp);
3522 bfd_write (temp, 1, LONG_SIZE, abfd);
3524 current = abfd->archive_head;
3526 for (i = 0; i < orl_count; i++)
3530 /* Advance firstreal to the file position of this archive
3532 if (((bfd *) map[i].pos) != last_elt)
3536 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3537 firstreal += firstreal % 2;
3538 current = current->next;
3540 while (current != (bfd *) map[i].pos);
3545 bfd_h_put_32 (abfd, map[i].namidx, buff);
3546 bfd_h_put_32 (abfd, firstreal, buff + LONG_SIZE);
3547 bfd_write (buff, 1, 2 * LONG_SIZE, abfd);
3550 /* Now write the strings. */
3551 bfd_h_put_32 (abfd, stringsize, temp);
3552 bfd_write (temp, 1, LONG_SIZE, abfd);
3553 for (i = 0; i < orl_count; i++)
3554 bfd_write ((PTR) (*map[i].name), 1, strlen (*map[i].name) + 1, abfd);
3556 /* The spec sez this should be a newline. But in order to be
3557 bug-compatible for DECstation ar we use a null. */
3559 bfd_write ("\0", 1, 1, abfd);
3564 /* We just use the generic extended name support. This is a GNU
3566 #define ecoff_slurp_extended_name_table _bfd_slurp_extended_name_table
3568 /* See whether this BFD is an archive. If it is, read in the armap
3569 and the extended name table. */
3572 DEFUN (ecoff_archive_p, (abfd),
3575 char armag[SARMAG + 1];
3577 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3578 || strncmp (armag, ARMAG, SARMAG) != 0)
3580 bfd_error = wrong_format;
3581 return (bfd_target *) NULL;
3584 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3585 involves a cast, we can't do it as the left operand of
3587 abfd->tdata.aout_ar_data =
3588 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3590 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3592 bfd_error = no_memory;
3593 return (bfd_target *) NULL;
3596 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3598 if (ecoff_slurp_armap (abfd) == false
3599 || ecoff_slurp_extended_name_table (abfd) == false)
3601 bfd_release (abfd, bfd_ardata (abfd));
3602 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3603 return (bfd_target *) NULL;
3609 static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
3610 (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
3611 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
3612 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
3613 (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
3614 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
3615 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
3616 ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
3617 ecoff_swap_scnhdr_out,
3618 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
3619 ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
3620 ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
3621 styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
3622 ecoff_slurp_symbol_table
3625 /* get_lineno could be written for ECOFF, but it would currently only
3626 be useful for linking ECOFF and COFF files together, which doesn't
3628 #define ecoff_get_lineno \
3629 ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
3631 #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
3632 #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
3633 #define ecoff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
3634 #define ecoff_truncate_arname bfd_dont_truncate_arname
3635 #define ecoff_openr_next_archived_file bfd_generic_openr_next_archived_file
3636 #define ecoff_generic_stat_arch_elt bfd_generic_stat_arch_elt
3637 #define ecoff_get_section_contents bfd_generic_get_section_contents
3638 #define ecoff_get_reloc_upper_bound coff_get_reloc_upper_bound
3639 #define ecoff_close_and_cleanup bfd_generic_close_and_cleanup
3640 #define ecoff_sizeof_headers coff_sizeof_headers
3641 #define ecoff_bfd_debug_info_start bfd_void
3642 #define ecoff_bfd_debug_info_end bfd_void
3643 #define ecoff_bfd_debug_info_accumulate \
3644 ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
3645 #define ecoff_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
3646 #define ecoff_bfd_relax_section bfd_generic_relax_section
3648 bfd_target ecoff_little_vec =
3650 "ecoff-littlemips", /* name */
3651 bfd_target_ecoff_flavour,
3652 false, /* data byte order is little */
3653 false, /* header byte order is little */
3655 (HAS_RELOC | EXEC_P | /* object flags */
3656 HAS_LINENO | HAS_DEBUG |
3657 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
3659 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
3661 0, /* leading underscore */
3662 '/', /* ar_pad_char */
3663 15, /* ar_max_namelen */
3664 3, /* minimum alignment power */
3665 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
3666 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
3668 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3669 ecoff_archive_p, _bfd_dummy_target},
3670 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3672 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
3673 _bfd_write_archive_contents, bfd_false},
3676 (PTR) &bfd_ecoff_std_swap_table
3679 bfd_target ecoff_big_vec =
3681 "ecoff-bigmips", /* name */
3682 bfd_target_ecoff_flavour,
3683 true, /* data byte order is big */
3684 true, /* header byte order is big */
3686 (HAS_RELOC | EXEC_P | /* object flags */
3687 HAS_LINENO | HAS_DEBUG |
3688 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
3690 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
3691 0, /* leading underscore */
3692 ' ', /* ar_pad_char */
3693 16, /* ar_max_namelen */
3694 3, /* minimum alignment power */
3695 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3696 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3697 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3698 ecoff_archive_p, _bfd_dummy_target},
3699 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
3701 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
3702 _bfd_write_archive_contents, bfd_false},
3705 (PTR) &bfd_ecoff_std_swap_table
3706 /* Note that there is another bfd_target just above this one. If
3707 you are adding initializers here, you should be adding them there