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. */
26 #include "coff/mips.h"
27 #include "coff/internal.h"
29 #include "coff/symconst.h"
30 #include "coff/ecoff-ext.h"
33 /* `Tdata' information kept for ECOFF files. */
35 #define ecoff_data(abfd) ((abfd)->tdata.ecoff_obj_data)
37 typedef struct ecoff_tdata
39 /* The reloc file position, set by
40 ecoff_compute_section_file_positions. */
41 file_ptr reloc_filepos;
43 /* The symbol table file position, set by ecoff_mkobject_hook. */
46 /* The cached gp value. This is used when relocating. */
49 /* The size of the unswapped ECOFF symbolic information. */
50 bfd_size_type raw_size;
52 /* The unswapped ECOFF symbolic information. */
55 /* The swapped ECOFF symbolic header. */
58 /* Pointers to the unswapped symbolic information. */
60 struct dnr_ext *external_dnr;
61 struct pdr_ext *external_pdr;
62 struct sym_ext *external_sym;
63 struct opt_ext *external_opt;
64 union aux_ext *external_aux;
67 struct fdr_ext *external_fdr;
68 struct rfd_ext *external_rfd;
69 struct ext_ext *external_ext;
71 /* The swapped fdr information. */
74 /* The canonical BFD symbols. */
75 struct ecoff_symbol_struct *canonical_symbols;
79 /* Each canonical asymbol really looks like this. */
81 typedef struct ecoff_symbol_struct
83 /* The actual symbol which the rest of BFD works with */
86 /* The fdr for this symbol. */
89 /* true if this is a local symbol rather than an external one. */
92 /* A pointer to the unswapped hidden information for this symbol */
95 struct sym_ext *lnative;
96 struct ext_ext *enative;
101 /* We take the address of the first element of a asymbol to ensure that the
102 macro is only ever applied to an asymbol. */
103 #define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
105 /* MIPS ECOFF has COFF sections, but the debugging information is
106 stored in a completely different format. This files uses the some
107 of the swapping routines from coffswap.h, and some of the generic
108 COFF routines in coffgen.c, but, unlike the real COFF targets, does
109 not use coffcode.h itself. */
111 /* Get the generic COFF swapping routines, except for the reloc,
112 symbol, and lineno ones. Give them ecoff names. */
113 #define NO_COFF_RELOCS
114 #define NO_COFF_SYMBOLS
115 #define NO_COFF_LINENOS
116 #define coff_swap_filehdr_in ecoff_swap_filehdr_in
117 #define coff_swap_filehdr_out ecoff_swap_filehdr_out
118 #define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
119 #define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
120 #define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
121 #define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
122 #include "coffswap.h"
124 /* This stuff is somewhat copied from coffcode.h. */
126 static asection bfd_debug_section = { "*DEBUG*" };
128 /* See whether the magic number matches. */
131 DEFUN(ecoff_bad_format_hook, (abfd, filehdr),
135 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
137 if (ECOFFBADMAG (*internal_f))
143 /* This is a hook needed by SCO COFF, but we have nothing to do. */
146 DEFUN (ecoff_make_section_hook, (abfd, name),
150 return (asection *) NULL;
153 /* Initialize a new section. */
156 DEFUN (ecoff_new_section_hook, (abfd, section),
160 section->alignment_power = abfd->xvec->align_power_min;
162 if (strcmp (section->name, _TEXT) == 0)
163 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
164 else if (strcmp (section->name, _DATA) == 0
165 || strcmp (section->name, _SDATA) == 0)
166 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
167 else if (strcmp (section->name, _RDATA) == 0
168 || strcmp (section->name, _LIT8) == 0
169 || strcmp (section->name, _LIT4) == 0)
170 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
171 else if (strcmp (section->name, _BSS) == 0
172 || strcmp (section->name, _SBSS) == 0)
173 section->flags |= SEC_ALLOC;
175 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
176 uncertain about .init on some systems and I don't know how shared
182 #define ecoff_set_alignment_hook \
183 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
186 DEFUN (ecoff_mkobject, (abfd),
189 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
190 bfd_zalloc (abfd, sizeof(ecoff_data_type)));
191 if (abfd->tdata.ecoff_obj_data == NULL)
193 bfd_error = no_memory;
200 /* Create the COFF backend specific information. */
203 DEFUN(ecoff_mkobject_hook,(abfd, filehdr),
207 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
208 ecoff_data_type *ecoff;
210 if (ecoff_mkobject (abfd) == false)
213 ecoff = ecoff_data (abfd);
214 ecoff->sym_filepos = internal_f->f_symptr;
218 /* Determine the machine architecture and type. */
220 DEFUN (ecoff_set_arch_mach_hook, (abfd, filehdr),
225 enum bfd_architecture arch;
226 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
229 switch (internal_f->f_magic) {
233 arch = bfd_arch_mips;
237 default: /* Unreadable input file type */
238 arch = bfd_arch_obscure;
242 bfd_default_set_arch_mach(abfd, arch, machine);
246 /* Get the section s_flags to use for a section. */
249 DEFUN (sec_to_styp_flags, (name, flags),
257 if (strcmp (name, _TEXT) == 0)
259 else if (strcmp (name, _DATA) == 0)
261 else if (strcmp (name, _SDATA) == 0)
263 else if (strcmp (name, _RDATA) == 0)
265 else if (strcmp (name, _LIT8) == 0)
267 else if (strcmp (name, _LIT4) == 0)
269 else if (strcmp (name, _BSS) == 0)
271 else if (strcmp (name, _SBSS) == 0)
273 else if (flags & SEC_CODE)
275 else if (flags & SEC_DATA)
277 else if (flags & SEC_READONLY)
279 else if (flags & SEC_LOAD)
284 if (flags & SEC_NEVER_LOAD)
290 /* Get the BFD flags to use for a section. */
293 DEFUN (styp_to_sec_flags, (abfd, hdr),
297 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
298 long styp_flags = internal_s->s_flags;
299 flagword sec_flags=0;
301 if (styp_flags & STYP_NOLOAD)
302 sec_flags |= SEC_NEVER_LOAD;
304 /* For 386 COFF, at least, an unloadable text or data section is
305 actually a shared library section. */
306 if (styp_flags & STYP_TEXT)
308 if (sec_flags & SEC_NEVER_LOAD)
309 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
311 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
313 else if ((styp_flags & STYP_DATA)
314 || (styp_flags & STYP_RDATA)
315 || (styp_flags & STYP_SDATA))
317 if (sec_flags & SEC_NEVER_LOAD)
318 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
320 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
321 if (styp_flags & STYP_RDATA)
322 sec_flags |= SEC_READONLY;
324 else if ((styp_flags & STYP_BSS)
325 || (styp_flags & STYP_SBSS))
327 sec_flags |= SEC_ALLOC;
329 else if (styp_flags & STYP_INFO)
331 sec_flags |= SEC_NEVER_LOAD;
333 else if ((styp_flags & STYP_LIT8)
334 || (styp_flags & STYP_LIT4))
336 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
340 sec_flags |= SEC_ALLOC | SEC_LOAD;
346 /* Read in and swap the important symbolic information for an ECOFF
350 DEFUN (ecoff_slurp_symbolic_info, (abfd),
353 struct hdr_ext external_symhdr;
354 HDRR *internal_symhdr;
355 bfd_size_type raw_base;
356 bfd_size_type raw_size;
358 struct fdr_ext *fraw_src;
359 struct fdr_ext *fraw_end;
362 /* Check whether we've already gotten it, and whether there's any to
364 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
366 if (ecoff_data (abfd)->sym_filepos == 0)
368 bfd_get_symcount (abfd) = 0;
372 /* At this point bfd_get_symcount (abfd) holds the number of symbols
373 as read from the file header, but on ECOFF this is always the
374 size of the symbolic information header. It would be cleaner to
375 handle this when we first read the file in coffgen.c. */
376 if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
378 bfd_error = bad_value;
382 /* Read the symbolic information header. */
383 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
384 || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
385 != sizeof (external_symhdr)))
387 bfd_error = system_call_error;
390 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
391 ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
393 if (internal_symhdr->magic != magicSym)
395 bfd_error = bad_value;
399 /* Now we can get the correct number of symbols. */
400 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
401 + internal_symhdr->iextMax);
403 /* Read all the symbolic information at once. This expression
404 assumes that the external symbols are always the last item. */
405 raw_base = ecoff_data (abfd)->sym_filepos + sizeof (external_symhdr);
406 raw_size = (internal_symhdr->cbExtOffset - raw_base
407 + internal_symhdr->iextMax * sizeof (struct ext_ext));
408 raw = (PTR) bfd_alloc (abfd, raw_size);
411 bfd_error = no_memory;
414 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
416 bfd_error = system_call_error;
417 bfd_release (abfd, raw);
421 ecoff_data (abfd)->raw_size = raw_size;
422 ecoff_data (abfd)->raw_syments = raw;
424 /* Get pointers for the numeric offsets in the HDRR structure. */
425 #define FIX(off1, off2, type) \
426 if (internal_symhdr->off1 == 0) \
427 ecoff_data (abfd)->off2 = (type *) NULL; \
429 ecoff_data (abfd)->off2 = (type *) ((char *) raw \
430 + internal_symhdr->off1 \
432 FIX (cbLineOffset, line, unsigned char);
433 FIX (cbDnOffset, external_dnr, struct dnr_ext);
434 FIX (cbPdOffset, external_pdr, struct pdr_ext);
435 FIX (cbSymOffset, external_sym, struct sym_ext);
436 FIX (cbOptOffset, external_opt, struct opt_ext);
437 FIX (cbAuxOffset, external_aux, union aux_ext);
438 FIX (cbSsOffset, ss, char);
439 FIX (cbSsExtOffset, ssext, char);
440 FIX (cbFdOffset, external_fdr, struct fdr_ext);
441 FIX (cbRfdOffset, external_rfd, struct rfd_ext);
442 FIX (cbExtOffset, external_ext, struct ext_ext);
445 /* I don't want to always swap all the data, because it will just
446 waste time and most programs will never look at it. The only
447 time the linker needs most of the debugging information swapped
448 is when linking big-endian and little-endian MIPS object files
449 together, which is not a common occurrence.
451 We need to look at the fdr to deal with a lot of information in
452 the symbols, so we swap them here. */
453 ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
454 (internal_symhdr->ifdMax *
455 sizeof (struct fdr)));
456 if (ecoff_data (abfd)->fdr == NULL)
458 bfd_error = no_memory;
461 fdr_ptr = ecoff_data (abfd)->fdr;
462 fraw_src = ecoff_data (abfd)->external_fdr;
463 fraw_end = fraw_src + internal_symhdr->ifdMax;
464 for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
465 ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
470 /* ECOFF symbol table routines. The ECOFF symbol table is described
471 in gcc/mips-tfile.c. */
473 /* Create an empty symbol. */
476 DEFUN (ecoff_make_empty_symbol, (abfd),
479 ecoff_symbol_type *new;
481 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
482 if (new == (ecoff_symbol_type *) NULL)
484 bfd_error = no_memory;
485 return (asymbol *) NULL;
487 new->symbol.section = (asection *) NULL;
488 new->fdr = (FDR *) NULL;
490 new->native.lnative = (struct sym_ext *) NULL;
491 new->symbol.the_bfd = abfd;
495 /* Set the BFD flags and section for an ECOFF symbol. */
498 DEFUN (ecoff_set_symbol_info, (abfd, ecoff_sym, asym, ext),
504 asym->the_bfd = abfd;
505 asym->value = ecoff_sym->value;
506 asym->section = &bfd_debug_section;
509 asym->flags = BSF_EXPORT | BSF_GLOBAL;
511 asym->flags = BSF_LOCAL;
512 switch (ecoff_sym->sc)
518 asym->section = bfd_make_section_old_way (abfd, ".text");
519 asym->value -= asym->section->vma;
522 asym->section = bfd_make_section_old_way (abfd, ".data");
523 asym->value -= asym->section->vma;
526 asym->section = &bfd_com_section;
530 asym->flags = BSF_DEBUGGING;
533 asym->section = &bfd_abs_section;
537 asym->section = &bfd_und_section;
546 asym->flags = BSF_DEBUGGING;
549 asym->section = bfd_make_section_old_way (abfd, ".sdata");
550 asym->value -= asym->section->vma;
553 asym->section = &bfd_com_section;
557 asym->section = bfd_make_section_old_way (abfd, ".rdata");
558 asym->value -= asym->section->vma;
561 asym->flags = BSF_DEBUGGING;
565 asym->section = &bfd_com_section;
570 asym->flags = BSF_DEBUGGING;
573 asym->section = &bfd_und_section;
577 asym->section = bfd_make_section_old_way (abfd, ".init");
578 asym->value -= asym->section->vma;
583 asym->flags = BSF_DEBUGGING;
586 asym->section = bfd_make_section_old_way (abfd, ".fini");
587 asym->value -= asym->section->vma;
595 /* Read an ECOFF symbol table. */
598 DEFUN (ecoff_slurp_symbol_table, (abfd),
601 bfd_size_type internal_size;
602 ecoff_symbol_type *internal;
603 ecoff_symbol_type *internal_ptr;
604 struct ext_ext *eraw_src;
605 struct ext_ext *eraw_end;
609 /* If we've already read in the symbol table, do nothing. */
610 if (ecoff_data (abfd)->canonical_symbols != NULL)
613 /* Get the symbolic information. */
614 if (ecoff_slurp_symbolic_info (abfd) == false)
616 if (bfd_get_symcount (abfd) == 0)
619 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
620 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
621 if (internal == NULL)
623 bfd_error = no_memory;
627 internal_ptr = internal;
628 eraw_src = ecoff_data (abfd)->external_ext;
629 eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
630 for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
634 ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
635 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
636 + internal_esym.asym.iss);
637 ecoff_set_symbol_info (abfd, &internal_esym.asym,
638 &internal_ptr->symbol, 1);
639 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
640 internal_ptr->local = false;
641 internal_ptr->native.enative = eraw_src;
644 /* The local symbols must be accessed via the fdr's, because the
645 string and aux indices are relative to the fdr information. */
646 fdr_ptr = ecoff_data (abfd)->fdr;
647 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
648 for (; fdr_ptr < fdr_end; fdr_ptr++)
650 struct sym_ext *lraw_src;
651 struct sym_ext *lraw_end;
653 lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
654 lraw_end = lraw_src + fdr_ptr->csym;
655 for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
659 ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
660 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
663 ecoff_set_symbol_info (abfd, &internal_sym,
664 &internal_ptr->symbol, 0);
665 internal_ptr->fdr = fdr_ptr;
666 internal_ptr->local = true;
667 internal_ptr->native.lnative = lraw_src;
671 ecoff_data (abfd)->canonical_symbols = internal;
677 DEFUN (ecoff_get_symtab_upper_bound, (abfd),
680 if (ecoff_slurp_symbolic_info (abfd) == false
681 || bfd_get_symcount (abfd) == 0)
684 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
688 DEFUN (ecoff_get_symtab, (abfd, alocation),
692 unsigned int counter = 0;
693 ecoff_symbol_type *symbase;
694 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
696 if (ecoff_slurp_symbol_table (abfd) == false
697 || bfd_get_symcount (abfd) == 0)
700 symbase = ecoff_data (abfd)->canonical_symbols;
701 while (counter < bfd_get_symcount (abfd))
703 *(location++) = symbase++;
706 *location++ = (ecoff_symbol_type *) NULL;
707 return bfd_get_symcount (abfd);
710 /* Turn ECOFF type information into a printable string.
711 emit_aggregate and type_to_string are from gcc/mips-tdump.c, with
712 swapping added and used_ptr removed. */
714 /* Write aggregate information to a string. */
717 DEFUN (emit_aggregate, (abfd, string, rndx, isym, which),
725 int indx = rndx->index;
726 int sym_base, ss_base;
732 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
733 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
735 if (indx == indexNil)
736 name = "/* no name */";
742 ecoff_swap_sym_in (abfd,
743 ecoff_data (abfd)->external_sym + indx,
745 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
749 "%s %s { ifd = %d, index = %d }",
751 indx + ecoff_data (abfd)->symbolic_header.iextMax);
754 /* Convert the type information to string format. */
757 DEFUN (type_to_string, (abfd, aux_ptr, indx, bigendian),
759 union aux_ext *aux_ptr AND
771 unsigned int basic_type;
773 static char buffer1[1024];
774 static char buffer2[1024];
779 for (i = 0; i < 7; i++)
781 qualifiers[i].low_bound = 0;
782 qualifiers[i].high_bound = 0;
783 qualifiers[i].stride = 0;
786 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
787 return "-1 (no type)";
788 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
790 basic_type = u.ti.bt;
791 qualifiers[0].type = u.ti.tq0;
792 qualifiers[1].type = u.ti.tq1;
793 qualifiers[2].type = u.ti.tq2;
794 qualifiers[3].type = u.ti.tq3;
795 qualifiers[4].type = u.ti.tq4;
796 qualifiers[5].type = u.ti.tq5;
797 qualifiers[6].type = tqNil;
800 * Go get the basic type.
804 case btNil: /* undefined */
808 case btAdr: /* address - integer same size as pointer */
809 strcpy (p1, "address");
812 case btChar: /* character */
816 case btUChar: /* unsigned character */
817 strcpy (p1, "unsigned char");
820 case btShort: /* short */
821 strcpy (p1, "short");
824 case btUShort: /* unsigned short */
825 strcpy (p1, "unsigned short");
828 case btInt: /* int */
832 case btUInt: /* unsigned int */
833 strcpy (p1, "unsigned int");
836 case btLong: /* long */
840 case btULong: /* unsigned long */
841 strcpy (p1, "unsigned long");
844 case btFloat: /* float (real) */
845 strcpy (p1, "float");
848 case btDouble: /* Double (real) */
849 strcpy (p1, "double");
852 /* Structures add 1-2 aux words:
853 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
854 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
856 case btStruct: /* Structure (Record) */
857 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
858 emit_aggregate (abfd, p1, &rndx,
859 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
861 indx++; /* skip aux words */
864 /* Unions add 1-2 aux words:
865 1st word is [ST_RFDESCAPE, offset] pointer to union def;
866 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
868 case btUnion: /* Union */
869 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
870 emit_aggregate (abfd, p1, &rndx,
871 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
873 indx++; /* skip aux words */
876 /* Enumerations add 1-2 aux words:
877 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
878 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
880 case btEnum: /* Enumeration */
881 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
882 emit_aggregate (abfd, p1, &rndx,
883 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
885 indx++; /* skip aux words */
888 case btTypedef: /* defined via a typedef, isymRef points */
889 strcpy (p1, "typedef");
892 case btRange: /* subrange of int */
893 strcpy (p1, "subrange");
896 case btSet: /* pascal sets */
900 case btComplex: /* fortran complex */
901 strcpy (p1, "complex");
904 case btDComplex: /* fortran double complex */
905 strcpy (p1, "double complex");
908 case btIndirect: /* forward or unnamed typedef */
909 strcpy (p1, "forward/unamed typedef");
912 case btFixedDec: /* Fixed Decimal */
913 strcpy (p1, "fixed decimal");
916 case btFloatDec: /* Float Decimal */
917 strcpy (p1, "float decimal");
920 case btString: /* Varying Length Character String */
921 strcpy (p1, "string");
924 case btBit: /* Aligned Bit String */
928 case btPicture: /* Picture */
929 strcpy (p1, "picture");
932 case btVoid: /* Void */
937 sprintf (p1, "Unknown basic type %d", (int) basic_type);
941 p1 += strlen (buffer1);
944 * If this is a bitfield, get the bitsize.
950 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
951 sprintf (p1, " : %d", bitsize);
952 p1 += strlen (buffer1);
957 * Deal with any qualifiers.
959 if (qualifiers[0].type != tqNil)
962 * Snarf up any array bounds in the correct order. Arrays
963 * store 5 successive words in the aux. table:
964 * word 0 RNDXR to type of the bounds (ie, int)
965 * word 1 Current file descriptor index
967 * word 3 high bound (or -1 if [])
968 * word 4 stride size in bits
970 for (i = 0; i < 7; i++)
972 if (qualifiers[i].type == tqArray)
974 qualifiers[i].low_bound =
975 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
976 qualifiers[i].high_bound =
977 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
978 qualifiers[i].stride =
979 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
985 * Now print out the qualifiers.
987 for (i = 0; i < 6; i++)
989 switch (qualifiers[i].type)
996 strcpy (p2, "ptr to ");
997 p2 += sizeof ("ptr to ")-1;
1001 strcpy (p2, "volatile ");
1002 p2 += sizeof ("volatile ")-1;
1006 strcpy (p2, "far ");
1007 p2 += sizeof ("far ")-1;
1011 strcpy (p2, "func. ret. ");
1012 p2 += sizeof ("func. ret. ");
1017 int first_array = i;
1020 /* Print array bounds reversed (ie, in the order the C
1021 programmer writes them). C is such a fun language.... */
1023 while (i < 5 && qualifiers[i+1].type == tqArray)
1026 for (j = i; j >= first_array; j--)
1028 strcpy (p2, "array [");
1029 p2 += sizeof ("array [")-1;
1030 if (qualifiers[j].low_bound != 0)
1032 "%ld:%ld {%ld bits}",
1033 (long) qualifiers[j].low_bound,
1034 (long) qualifiers[j].high_bound,
1035 (long) qualifiers[j].stride);
1037 else if (qualifiers[j].high_bound != -1)
1040 (long) (qualifiers[j].high_bound + 1),
1041 (long) (qualifiers[j].stride));
1044 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1047 strcpy (p2, "] of ");
1048 p2 += sizeof ("] of ")-1;
1056 strcpy (p2, buffer1);
1060 /* Print information about an ECOFF symbol. */
1063 DEFUN (ecoff_print_symbol, (abfd, filep, symbol, how),
1067 bfd_print_symbol_type how)
1069 FILE *file = (FILE *)filep;
1073 case bfd_print_symbol_name:
1074 fprintf (file, "%s", symbol->name);
1076 case bfd_print_symbol_more:
1077 if (ecoffsymbol (symbol)->local)
1081 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1083 fprintf (file, "ecoff local %lx %x %x",
1084 (unsigned long) ecoff_sym.value,
1085 (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
1091 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1093 fprintf (file, "ecoff extern %lx %x %x",
1094 (unsigned long) ecoff_ext.asym.value,
1095 (unsigned) ecoff_ext.asym.st,
1096 (unsigned) ecoff_ext.asym.sc);
1099 case bfd_print_symbol_nm:
1101 CONST char *section_name = symbol->section->name;
1103 bfd_print_symbol_vandf ((PTR) file, symbol);
1104 fprintf (file, " %-5s %s %s",
1106 ecoffsymbol (symbol)->local ? "l" : "e",
1110 case bfd_print_symbol_all:
1111 /* Print out the symbols in a reasonable way */
1120 if (ecoffsymbol (symbol)->local)
1122 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1125 pos = (ecoffsymbol (symbol)->native.lnative
1126 - ecoff_data (abfd)->external_sym
1127 + ecoff_data (abfd)->symbolic_header.iextMax);
1134 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1137 pos = (ecoffsymbol (symbol)->native.enative
1138 - ecoff_data (abfd)->external_ext);
1139 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1140 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1141 weakext = ecoff_ext.weakext ? 'w' : ' ';
1144 fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
1145 pos, type, (unsigned long) ecoff_ext.asym.value,
1146 (unsigned) ecoff_ext.asym.st,
1147 (unsigned) ecoff_ext.asym.sc,
1148 (unsigned) ecoff_ext.asym.index,
1149 jmptbl, cobol_main, weakext,
1152 if (ecoffsymbol (symbol)->fdr != NULL
1153 && ecoff_ext.asym.index != indexNil)
1158 union aux_ext *aux_base;
1160 indx = ecoff_ext.asym.index;
1162 /* sym_base is used to map the fdr relative indices which
1163 appear in the file to the position number which we are
1165 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1166 if (ecoffsymbol (symbol)->local)
1167 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1169 /* aux_base is the start of the aux entries for this file;
1170 asym.index is an offset from this. */
1171 aux_base = (ecoff_data (abfd)->external_aux
1172 + ecoffsymbol (symbol)->fdr->iauxBase);
1174 /* The aux entries are stored in host byte order; the
1175 order is indicated by a bit in the fdr. */
1176 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1178 /* This switch is basically from gcc/mips-tdump.c */
1179 switch (ecoff_ext.asym.st)
1187 printf ("\n End+1 symbol: %ld", indx + sym_base);
1191 if (ecoff_ext.asym.sc == scText
1192 || ecoff_ext.asym.sc == scInfo)
1193 printf ("\n First symbol: %ld", indx + sym_base);
1195 printf ("\n First symbol: %ld",
1196 (AUX_GET_ISYM (bigendian,
1197 &aux_base[ecoff_ext.asym.index])
1203 if (MIPS_IS_STAB (&ecoff_ext.asym))
1205 else if (ecoffsymbol (symbol)->local)
1206 printf ("\n End+1 symbol: %-7ld Type: %s",
1207 (AUX_GET_ISYM (bigendian,
1208 &aux_base[ecoff_ext.asym.index])
1210 type_to_string (abfd, aux_base, indx + 1,
1213 printf ("\n Local symbol: %d",
1216 + ecoff_data (abfd)->symbolic_header.iextMax));
1220 if (!MIPS_IS_STAB (&ecoff_ext.asym))
1221 printf ("\n Type: %s",
1222 type_to_string (abfd, aux_base, indx, bigendian));
1231 /* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
1232 external form. They use a bit which indicates whether the symbol
1235 /* Swap a reloc in. */
1238 DEFUN (ecoff_swap_reloc_in, (abfd, ext, intern),
1241 struct internal_reloc *intern)
1243 intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
1244 if (abfd->xvec->header_byteorder_big_p != false)
1246 intern->r_symndx = ((ext->r_bits[0]
1247 << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
1249 << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
1251 << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
1252 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
1253 >> RELOC_BITS3_TYPE_SH_BIG);
1254 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
1258 intern->r_symndx = ((ext->r_bits[0]
1259 << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
1261 << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
1263 << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
1264 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
1265 >> RELOC_BITS3_TYPE_SH_LITTLE);
1266 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
1270 /* Swap a reloc out. */
1273 DEFUN (ecoff_swap_reloc_out, (abfd, src, dst),
1278 struct internal_reloc *intern = (struct internal_reloc *) src;
1279 RELOC *ext = (RELOC *) dst;
1281 bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
1282 if (abfd->xvec->header_byteorder_big_p != false)
1284 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
1285 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
1286 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
1287 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
1288 & RELOC_BITS3_TYPE_BIG)
1289 | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
1293 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
1294 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
1295 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
1296 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
1297 & RELOC_BITS3_TYPE_LITTLE)
1298 | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
1304 /* Do a REFHI relocation. The next reloc must be the corresponding
1305 REFLO. This has to be done in a function so that carry is handled
1308 static bfd_reloc_status_type
1309 DEFUN (ecoff_refhi_reloc, (abfd,
1316 arelent *reloc_entry AND
1319 asection *input_section AND
1322 bfd_reloc_status_type ret;
1325 asection *reloc_target_output_section;
1330 if (symbol->section == &bfd_und_section
1331 && output_bfd == (bfd *) NULL)
1332 ret = bfd_reloc_undefined;
1334 rello = reloc_entry + 1;
1335 BFD_ASSERT (rello->howto->type == ECOFF_R_REFLO
1336 && *rello->sym_ptr_ptr == *reloc_entry->sym_ptr_ptr);
1338 if (symbol->section == &bfd_com_section)
1341 relocation = symbol->value;
1343 relocation += symbol->section->output_section->vma;
1344 relocation += symbol->section->output_offset;
1346 if (reloc_entry->address > input_section->_cooked_size)
1347 return bfd_reloc_outofrange;
1349 if (output_bfd != (bfd *) NULL)
1350 reloc_entry->address += input_section->output_offset;
1352 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1353 val = (((insn & 0xffff) << 16)
1354 + (bfd_get_32 (abfd, (bfd_byte *) data + rello->address) & 0xffff));
1355 insn = (insn &~ 0xffff) | (((val + symbol->value) >> 16) & 0xffff);
1356 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1361 /* Do a GPREL relocation. This is a 16 bit value which must become
1362 the offset from the gp register. */
1364 static bfd_reloc_status_type
1365 DEFUN (ecoff_gprel_reloc, (abfd,
1372 arelent *reloc_entry AND
1375 asection *input_section AND
1378 bfd_reloc_status_type ret;
1383 /* If this is a partial link, we don't need to do anything unusual
1385 if (output_bfd != (bfd *) NULL)
1386 return bfd_reloc_continue;
1389 if (symbol->section == &bfd_und_section)
1390 ret = bfd_reloc_undefined;
1392 /* Otherwise we have to figure out the gp value, so that we can
1393 adjust the symbol value correctly. We look up the symbol _gp in
1394 the output BFD. If we can't find it, we're stuck. We cache it
1395 in the ECOFF target data. */
1396 output_bfd = symbol->section->output_section->owner;
1397 if (ecoff_data (output_bfd)->gp == 0)
1403 count = bfd_get_symcount (output_bfd);
1404 sym = bfd_get_outsymbols (output_bfd);
1406 /* We should do something more friendly here, but we don't have
1407 a good reloc status to return. */
1408 if (sym == (asymbol **) NULL)
1411 for (i = 0; i < count; i++, sym++)
1413 register CONST char *name;
1415 name = bfd_asymbol_name (*sym);
1416 if (*name == '_' && strcmp (name, "_gp") == 0)
1418 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1423 /* We should do something more friendly here, but we don't have
1424 a good reloc status to return. */
1429 if (symbol->section == &bfd_com_section)
1432 relocation = symbol->value;
1434 relocation += symbol->section->output_section->vma;
1435 relocation += symbol->section->output_offset;
1437 if (reloc_entry->address > input_section->_cooked_size)
1438 return bfd_reloc_outofrange;
1440 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
1441 val = insn & 0xffff;
1442 if ((val & 0x8000) != 0)
1444 val -= ecoff_data (output_bfd)->gp;
1445 insn = (insn &~ 0xffff) | (val & 0xffff);
1446 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1451 /* How to process the various relocs types. */
1453 static reloc_howto_type ecoff_howto_table[] =
1455 /* Reloc type 0 is ignored. The reloc reading code ensures that
1456 this is a reference to the .abs section, which will cause
1457 bfd_perform_relocation to do nothing. */
1458 HOWTO (ECOFF_R_IGNORE, /* type */
1460 0, /* size (0 = byte, 1 = short, 2 = long) */
1461 8, /* bitsize (obsolete) */
1462 false, /* pc_relative */
1464 false, /* absolute (obsolete) */
1465 false, /* complain_on_overflow */
1466 0, /* special_function */
1467 "IGNORE", /* name */
1468 false, /* partial_inplace */
1471 false), /* pcrel_offset */
1473 /* A 16 bit reference to a symbol, normally from a data section. */
1474 HOWTO (ECOFF_R_REFHALF, /* type */
1476 1, /* size (0 = byte, 1 = short, 2 = long) */
1477 16, /* bitsize (obsolete) */
1478 false, /* pc_relative */
1480 false, /* absolute (obsolete) */
1481 true, /* complain_on_overflow */
1482 0, /* special_function */
1483 "REFHALF", /* name */
1484 true, /* partial_inplace */
1485 0xffff, /* src_mask */
1486 0xffff, /* dst_mask */
1487 false), /* pcrel_offset */
1489 /* A 32 bit reference to a symbol, normally from a data section. */
1490 HOWTO (ECOFF_R_REFWORD, /* type */
1492 2, /* size (0 = byte, 1 = short, 2 = long) */
1493 32, /* bitsize (obsolete) */
1494 false, /* pc_relative */
1496 false, /* absolute (obsolete) */
1497 true, /* complain_on_overflow */
1498 0, /* special_function */
1499 "REFWORD", /* name */
1500 true, /* partial_inplace */
1501 0xffffffff, /* src_mask */
1502 0xffffffff, /* dst_mask */
1503 false), /* pcrel_offset */
1505 /* A 26 bit absolute jump address. */
1506 HOWTO (ECOFF_R_JMPADDR, /* type */
1508 2, /* size (0 = byte, 1 = short, 2 = long) */
1509 32, /* bitsize (obsolete) */
1510 false, /* pc_relative */
1512 false, /* absolute (obsolete) */
1513 true, /* complain_on_overflow */
1514 0, /* special_function */
1515 "JMPADDR", /* name */
1516 true, /* partial_inplace */
1517 0x3ffffff, /* src_mask */
1518 0x3ffffff, /* dst_mask */
1519 false), /* pcrel_offset */
1521 /* The high 16 bits of a symbol value. Handled by the function
1522 ecoff_refhi_reloc. */
1523 HOWTO (ECOFF_R_REFHI, /* type */
1524 16, /* rightshift */
1525 2, /* size (0 = byte, 1 = short, 2 = long) */
1526 32, /* bitsize (obsolete) */
1527 false, /* pc_relative */
1529 false, /* absolute (obsolete) */
1530 true, /* complain_on_overflow */
1531 ecoff_refhi_reloc, /* special_function */
1533 true, /* partial_inplace */
1534 0xffff, /* src_mask */
1535 0xffff, /* dst_mask */
1536 false), /* pcrel_offset */
1538 /* The low 16 bits of a symbol value. */
1539 HOWTO (ECOFF_R_REFLO, /* type */
1541 2, /* size (0 = byte, 1 = short, 2 = long) */
1542 32, /* bitsize (obsolete) */
1543 false, /* pc_relative */
1545 false, /* absolute (obsolete) */
1546 true, /* complain_on_overflow */
1547 0, /* special_function */
1549 true, /* partial_inplace */
1550 0xffff, /* src_mask */
1551 0xffff, /* dst_mask */
1552 false), /* pcrel_offset */
1554 /* A reference to an offset from the gp register. Handled by the
1555 function ecoff_gprel_reloc. */
1556 HOWTO (ECOFF_R_GPREL, /* type */
1558 2, /* size (0 = byte, 1 = short, 2 = long) */
1559 32, /* bitsize (obsolete) */
1560 false, /* pc_relative */
1562 false, /* absolute (obsolete) */
1563 true, /* complain_on_overflow */
1564 ecoff_gprel_reloc, /* special_function */
1566 true, /* partial_inplace */
1567 0xffff, /* src_mask */
1568 0xffff, /* dst_mask */
1569 false), /* pcrel_offset */
1571 /* A reference to a literal using an offset from the gp register.
1572 Handled by the function ecoff_gprel_reloc. */
1573 HOWTO (ECOFF_R_LITERAL, /* type */
1575 2, /* size (0 = byte, 1 = short, 2 = long) */
1576 32, /* bitsize (obsolete) */
1577 false, /* pc_relative */
1579 false, /* absolute (obsolete) */
1580 true, /* complain_on_overflow */
1581 ecoff_gprel_reloc, /* special_function */
1582 "LITERAL", /* name */
1583 true, /* partial_inplace */
1584 0xffff, /* src_mask */
1585 0xffff, /* dst_mask */
1586 false) /* pcrel_offset */
1589 /* Read in the relocs for a section. */
1592 DEFUN (ecoff_slurp_reloc_table, (abfd, section, symbols),
1594 asection *section AND
1597 RELOC *external_relocs;
1598 arelent *internal_relocs;
1602 if (section->relocation != (arelent *) NULL
1603 || section->reloc_count == 0
1604 || (section->flags & SEC_CONSTRUCTOR) != 0)
1607 if (ecoff_slurp_symbol_table (abfd) == false)
1610 internal_relocs = (arelent *) bfd_alloc (abfd,
1612 * section->reloc_count));
1613 external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
1614 if (internal_relocs == (arelent *) NULL
1615 || external_relocs == (RELOC *) NULL)
1617 bfd_error = no_memory;
1620 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1622 if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
1623 != RELSZ * section->reloc_count)
1625 bfd_error = system_call_error;
1629 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1631 struct internal_reloc intern;
1633 ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
1635 if (intern.r_type < 0 || intern.r_type > ECOFF_R_LITERAL)
1638 if (intern.r_extern)
1640 /* r_symndx is an index into the external symbols. */
1641 BFD_ASSERT (intern.r_symndx >= 0
1643 < ecoff_data (abfd)->symbolic_header.iextMax));
1644 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1648 CONST char *sec_name;
1651 /* r_symndx is a section key. */
1652 switch (intern.r_symndx)
1654 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1655 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1656 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1657 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1658 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1659 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1660 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1661 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1662 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1666 sec = bfd_get_section_by_name (abfd, sec_name);
1667 if (sec == (asection *) NULL)
1669 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1672 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1674 rptr->howto = &ecoff_howto_table[intern.r_type];
1676 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
1677 to the absolute section so that the reloc is ignored. */
1678 if (intern.r_type == ECOFF_R_IGNORE)
1679 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
1682 bfd_release (abfd, external_relocs);
1684 section->relocation = internal_relocs;
1689 /* Get a canonical list of relocs. */
1692 DEFUN (ecoff_canonicalize_reloc, (abfd, section, relptr, symbols),
1694 asection *section AND
1695 arelent **relptr AND
1700 if (section->flags & SEC_CONSTRUCTOR)
1702 arelent_chain *chain;
1704 /* This section has relocs made up by us, not the file, so take
1705 them out of their chain and place them into the data area
1707 for (count = 0, chain = section->constructor_chain;
1708 count < section->reloc_count;
1709 count++, chain = chain->next)
1710 *relptr++ = &chain->relent;
1716 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1719 tblptr = section->relocation;
1720 if (tblptr == (arelent *) NULL)
1723 for (count = 0; count < section->reloc_count; count++)
1724 *relptr++ = tblptr++;
1727 *relptr = (arelent *) NULL;
1729 return section->reloc_count;
1732 /* Provided a BFD, a section and an offset into the section, calculate
1733 and return the name of the source file and the line nearest to the
1737 DEFUN (ecoff_find_nearest_line, (abfd,
1745 asection *section AND
1746 asymbol **ignore_symbols AND
1748 CONST char **filename_ptr AND
1749 CONST char **functionname_ptr AND
1750 unsigned int *retline_ptr)
1756 struct pdr_ext *pdr_ptr;
1757 struct pdr_ext *pdr_end;
1759 unsigned char *line_ptr;
1760 unsigned char *line_end;
1764 /* If we're not in the .text section, we don't have any line
1766 if (strcmp (section->name, _TEXT) != 0)
1769 /* Make sure we have the FDR's. */
1770 if (ecoff_slurp_symbolic_info (abfd) == false
1771 || bfd_get_symcount (abfd) == 0)
1774 /* Each file descriptor (FDR) has a memory address. Here we track
1775 down which FDR we want. The FDR's are stored in increasing
1776 memory order. If speed is ever important, this can become a
1777 binary search. We must ignore FDR's with no PDR entries; they
1778 will have the adr of the FDR before or after them. */
1779 fdr_start = ecoff_data (abfd)->fdr;
1780 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
1781 fdr_hold = (FDR *) NULL;
1782 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
1784 if (offset < fdr_ptr->adr)
1786 if (fdr_ptr->cpd > 0)
1789 if (fdr_hold == (FDR *) NULL)
1793 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
1794 have an address, which is relative to the FDR address, and are
1795 also stored in increasing memory order. */
1796 offset -= fdr_ptr->adr;
1797 pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
1798 pdr_end = pdr_ptr + fdr_ptr->cpd;
1799 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1800 if (offset < pdr.adr)
1802 for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
1804 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1805 if (offset < pdr.adr)
1809 /* Now we can look for the actual line number. The line numbers are
1810 stored in a very funky format, which I won't try to describe.
1811 Note that right here pdr_ptr and pdr hold the PDR *after* the one
1812 we want; we need this to compute line_end. */
1813 line_end = ecoff_data (abfd)->line;
1814 if (pdr_ptr == pdr_end)
1815 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
1817 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
1819 /* Now change pdr and pdr_ptr to the one we want. */
1821 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
1825 line_ptr = (ecoff_data (abfd)->line
1826 + fdr_ptr->cbLineOffset
1827 + pdr.cbLineOffset);
1828 while (line_ptr < line_end)
1833 delta = *line_ptr >> 4;
1836 count = (*line_ptr & 0xf) + 1;
1840 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
1841 if (delta >= 0x8000)
1846 if (offset < count * 4)
1848 offset -= count * 4;
1851 /* If offset is too large, this line is not interesting. */
1855 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
1856 ecoff_swap_sym_in (abfd,
1857 (ecoff_data (abfd)->external_sym
1861 *functionname_ptr = ecoff_data (abfd)->ss + proc_sym.iss;
1862 *retline_ptr = lineno;
1866 /* We can't use the generic linking routines for ECOFF, because we
1867 have to handle all the debugging information. The generic link
1868 routine just works out the section contents and attaches a list of
1871 We link by looping over all the seclets. We make two passes. On
1872 the first we set the actual section contents and determine the size
1873 of the debugging information. On the second we accumulate the
1874 debugging information and write it out.
1876 This currently always accumulates the debugging information, which
1877 is incorrect, because it ignores the -s and -S options of the
1878 linker. The linker needs to be modified to give us that
1879 information in a more useful format (currently it just provides a
1880 list of symbols which should appear in the output file). */
1882 /* Clear the output_has_begun flag for all the input BFD's. We use it
1883 to avoid linking in the debugging information for a BFD more than
1887 DEFUN (ecoff_clear_output_flags, (abfd),
1890 register asection *o;
1891 register bfd_seclet_type *p;
1893 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
1894 for (p = o->seclets_head;
1895 p != (bfd_seclet_type *) NULL;
1897 if (p->type == bfd_indirect_seclet)
1898 p->u.indirect.section->owner->output_has_begun = false;
1901 /* Handle an indirect seclet on the first pass. Set the contents of
1902 the output section, and accumulate the debugging information if
1906 DEFUN (ecoff_rel, (output_bfd, seclet, output_section, data, relocateable),
1908 bfd_seclet_type *seclet AND
1909 asection *output_section AND
1911 boolean relocateable)
1914 HDRR *output_symhdr;
1917 if ((output_section->flags & SEC_HAS_CONTENTS)
1918 && !(output_section->flags & SEC_NEVER_LOAD)
1919 && (output_section->flags & SEC_LOAD)
1922 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
1926 if (bfd_set_section_contents (output_bfd,
1937 input_bfd = seclet->u.indirect.section->owner;
1939 /* We want to figure out how much space will be required to
1940 incorporate all the debugging information from input_bfd. We use
1941 the output_has_begun field to avoid adding it in more than once.
1942 The actual incorporation is done in the second pass, in
1943 ecoff_get_debug. The code has to parallel that code in its
1944 manipulations of output_symhdr. */
1946 if (input_bfd->output_has_begun)
1948 input_bfd->output_has_begun = true;
1950 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
1952 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
1958 /* We just accumulate symbols from a non-ECOFF BFD. */
1960 symbols = (asymbol **) bfd_alloc (output_bfd,
1961 get_symtab_upper_bound (input_bfd));
1962 if (symbols == (asymbol **) NULL)
1964 bfd_error = no_memory;
1967 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
1969 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
1973 len = strlen ((*sym_ptr)->name);
1974 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
1976 ++output_symhdr->isymMax;
1977 output_symhdr->issMax += len + 1;
1981 ++output_symhdr->iextMax;
1982 output_symhdr->issExtMax += len + 1;
1986 bfd_release (output_bfd, (PTR) symbols);
1988 ++output_symhdr->ifdMax;
1993 /* We simply add in the information from another ECOFF BFD. First
1994 we make sure we have the symbolic information. */
1995 if (ecoff_slurp_symbolic_info (input_bfd) == false)
1997 if (bfd_get_symcount (input_bfd) == 0)
2000 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2002 output_symhdr->ilineMax += input_symhdr->ilineMax;
2003 output_symhdr->cbLine += input_symhdr->cbLine;
2004 output_symhdr->idnMax += input_symhdr->idnMax;
2005 output_symhdr->ipdMax += input_symhdr->ipdMax;
2006 output_symhdr->isymMax += input_symhdr->isymMax;
2007 output_symhdr->ioptMax += input_symhdr->ioptMax;
2008 output_symhdr->iauxMax += input_symhdr->iauxMax;
2009 output_symhdr->issMax += input_symhdr->issMax;
2010 output_symhdr->issExtMax += input_symhdr->issExtMax;
2011 output_symhdr->ifdMax += input_symhdr->ifdMax;
2012 output_symhdr->iextMax += input_symhdr->iextMax;
2014 /* The RFD's are special, since we create them if needed. */
2015 if (input_symhdr->crfd > 0)
2016 output_symhdr->crfd += input_symhdr->crfd;
2018 output_symhdr->crfd += input_symhdr->ifdMax;
2023 /* Handle an arbitrary seclet on the first pass. */
2026 DEFUN (ecoff_dump_seclet, (abfd, seclet, section, data, relocateable),
2028 bfd_seclet_type *seclet AND
2029 asection *section AND
2031 boolean relocateable)
2033 switch (seclet->type)
2035 case bfd_indirect_seclet:
2036 /* The contents of this section come from another one somewhere
2038 return ecoff_rel (abfd, seclet, section, data, relocateable);
2040 case bfd_fill_seclet:
2041 /* Fill in the section with us. */
2043 char *d = (char *) bfd_alloc (abfd, seclet->size);
2047 for (i = 0; i < seclet->size; i+=2)
2048 d[i] = seclet->u.fill.value >> 8;
2049 for (i = 1; i < seclet->size; i+=2)
2050 d[i] = seclet->u.fill.value;
2051 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2053 bfd_release (abfd, (PTR) d);
2064 /* Add a string to the debugging information we are accumulating for a
2065 file. Return the offset from the fdr string base or from the
2066 external string base. */
2069 DEFUN (ecoff_add_string, (output_bfd, fdr, string, external),
2072 CONST char *string AND
2079 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2080 len = strlen (string);
2083 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2084 ret = symhdr->issExtMax;
2085 symhdr->issExtMax += len + 1;
2089 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2091 symhdr->issMax += len + 1;
2092 fdr->cbSs += len + 1;
2097 /* Accumulate the debugging information from an input section. */
2100 DEFUN (ecoff_get_debug, (output_bfd, seclet, section),
2102 bfd_seclet_type *seclet AND
2106 HDRR *output_symhdr;
2108 ecoff_data_type *output_ecoff;
2109 ecoff_data_type *input_ecoff;
2112 struct fdr_ext *fdr_out;
2113 struct ext_ext *ext_ptr;
2114 struct ext_ext *ext_end;
2115 struct ext_ext *ext_out;
2125 input_bfd = seclet->u.indirect.section->owner;
2127 /* Don't get the information more than once. */
2128 if (input_bfd->output_has_begun)
2130 input_bfd->output_has_begun = true;
2132 output_ecoff = ecoff_data (output_bfd);
2133 output_symhdr = &output_ecoff->symbolic_header;
2135 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2142 /* This is not an ECOFF BFD. Just gather the symbols. */
2144 memset (&fdr, 0, sizeof fdr);
2146 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2147 fdr.issBase = output_symhdr->issMax;
2149 fdr.rss = ecoff_add_string (output_bfd,
2151 bfd_get_filename (input_bfd),
2153 fdr.isymBase = output_symhdr->isymMax;
2155 /* Get the symbols from the input BFD. */
2156 symbols = (asymbol **) bfd_alloc (output_bfd,
2157 get_symtab_upper_bound (input_bfd));
2158 if (symbols == (asymbol **) NULL)
2160 bfd_error = no_memory;
2163 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2165 /* Handle the local symbols. */
2167 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2171 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2173 memset (&internal_sym, 0, sizeof internal_sym);
2174 internal_sym.iss = ecoff_add_string (output_bfd,
2178 internal_sym.value = (*sym_ptr)->value;
2179 internal_sym.st = stNil;
2180 internal_sym.sc = scUndefined;
2181 internal_sym.index = indexNil;
2182 ecoff_swap_sym_out (output_bfd, &internal_sym,
2183 (output_ecoff->external_sym
2184 + output_symhdr->isymMax));
2186 ++output_symhdr->isymMax;
2189 /* Handle the external symbols. */
2190 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2194 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2196 memset (&internal_ext, 0, sizeof internal_ext);
2197 internal_ext.ifd = output_symhdr->ifdMax;
2198 internal_ext.asym.iss = ecoff_add_string (output_bfd,
2202 internal_ext.asym.value = (*sym_ptr)->value;
2203 internal_ext.asym.st = stNil;
2204 internal_ext.asym.sc = scUndefined;
2205 internal_ext.asym.reserved = 0;
2206 internal_ext.asym.index = indexNil;
2207 ecoff_swap_ext_out (output_bfd, &internal_ext,
2208 (output_ecoff->external_ext
2209 + output_symhdr->iextMax));
2210 ++output_symhdr->iextMax;
2213 bfd_release (output_bfd, (PTR) symbols);
2215 /* Leave everything else zeroed out. This will cause the lang
2216 field to be langC. The fBigendian field will indicate little
2217 endian format, but it doesn't matter because it only applies
2218 to aux fields and there are none. */
2220 ecoff_swap_fdr_out (output_bfd, &fdr,
2221 (output_ecoff->external_fdr
2222 + output_symhdr->ifdMax));
2223 ++output_symhdr->ifdMax;
2227 /* This is an ECOFF BFD. We want to grab the information from
2228 input_bfd and attach it to output_bfd. */
2229 if (bfd_get_symcount (input_bfd) == 0)
2231 input_ecoff = ecoff_data (input_bfd);
2232 input_symhdr = &input_ecoff->symbolic_header;
2234 /* Because ECOFF uses relative pointers for most of the debugging
2235 information, much of it can simply be copied from input_bfd to
2237 memcpy (output_ecoff->line + output_symhdr->cbLineOffset,
2239 input_symhdr->cbLine * sizeof (unsigned char));
2240 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2241 input_ecoff->external_aux,
2242 input_symhdr->iauxMax * sizeof (union aux_ext));
2243 memcpy (output_ecoff->ss + output_symhdr->issMax,
2245 input_symhdr->issMax * sizeof (char));
2246 memcpy (output_ecoff->ssext + output_symhdr->issExtMax,
2248 input_symhdr->issExtMax * sizeof (char));
2250 /* Some of the information may need to be swapped. */
2251 if (output_bfd->xvec->byteorder_big_p == input_bfd->xvec->byteorder_big_p)
2253 /* The two BFD's have the same endianness, so memcpy will
2255 memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
2256 input_ecoff->external_dnr,
2257 input_symhdr->idnMax * sizeof (struct dnr_ext));
2258 memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
2259 input_ecoff->external_pdr,
2260 input_symhdr->ipdMax * sizeof (struct pdr_ext));
2261 memcpy (output_ecoff->external_sym + output_symhdr->isymMax,
2262 input_ecoff->external_sym,
2263 input_symhdr->isymMax * sizeof (struct sym_ext));
2264 memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
2265 input_ecoff->external_opt,
2266 input_symhdr->ioptMax * sizeof (struct opt_ext));
2270 struct dnr_ext *dnr_in;
2271 struct dnr_ext *dnr_end;
2272 struct dnr_ext *dnr_out;
2273 struct pdr_ext *pdr_in;
2274 struct pdr_ext *pdr_end;
2275 struct pdr_ext *pdr_out;
2276 struct sym_ext *sym_in;
2277 struct sym_ext *sym_end;
2278 struct sym_ext *sym_out;
2279 struct opt_ext *opt_in;
2280 struct opt_ext *opt_end;
2281 struct opt_ext *opt_out;
2283 /* The two BFD's have different endianness, so we must swap
2284 everything in and out. This code would always work, but it
2285 would be very slow in the normal case. */
2286 dnr_in = input_ecoff->external_dnr;
2287 dnr_end = dnr_in + input_symhdr->idnMax;
2288 dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
2289 for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
2293 ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
2294 ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
2296 pdr_in = input_ecoff->external_pdr;
2297 pdr_end = pdr_in + input_symhdr->ipdMax;
2298 pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
2299 for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
2303 ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
2304 ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
2306 sym_in = input_ecoff->external_sym;
2307 sym_end = sym_in + input_symhdr->isymMax;
2308 sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
2309 for (; sym_in < sym_end; sym_in++, sym_out++)
2313 ecoff_swap_sym_in (input_bfd, sym_in, &sym);
2314 ecoff_swap_sym_out (output_bfd, &sym, sym_out);
2316 opt_in = input_ecoff->external_opt;
2317 opt_end = opt_in + input_symhdr->ioptMax;
2318 opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
2319 for (; opt_in < opt_end; opt_in++, opt_out++)
2323 ecoff_swap_opt_in (input_bfd, opt_in, &opt);
2324 ecoff_swap_opt_out (output_bfd, &opt, opt_out);
2328 /* Step through the FDR's of input_bfd, adjust the offsets, and
2330 iss = output_symhdr->issMax;
2331 isym = output_symhdr->isymMax;
2332 iline = output_symhdr->ilineMax;
2333 iopt = output_symhdr->ioptMax;
2334 ipdr = output_symhdr->ipdMax;
2335 iaux = output_symhdr->iauxMax;
2336 irfd = output_symhdr->crfd;
2337 cbline = output_symhdr->cbLineOffset;
2339 fdr_ptr = input_ecoff->fdr;
2340 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2341 fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
2342 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
2348 /* The memory address for this fdr is the address for the seclet
2349 plus the offset to this fdr within input_bfd. */
2350 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2352 + (fdr_ptr->adr - input_ecoff->fdr->adr));
2355 iss += fdr.cbSs * sizeof (char);
2356 fdr.isymBase = isym;
2357 isym += fdr.csym * sizeof (struct sym_ext);
2358 fdr.ilineBase = iline;
2360 fdr.ioptBase = iopt;
2361 iopt += fdr.copt * sizeof (struct opt_ext);
2362 fdr.ipdFirst = ipdr;
2363 ipdr += fdr.cpd * sizeof (struct pdr_ext);
2364 fdr.iauxBase = iaux;
2365 iaux += fdr.caux * sizeof (union aux_ext);
2367 irfd += fdr.crfd * sizeof (struct rfd_ext);
2368 fdr.cbLineOffset = cbline;
2369 cbline += fdr.cbLine * sizeof (unsigned char);
2371 ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
2374 if (input_symhdr->crfd > 0)
2376 struct rfd_ext *rfd_in;
2377 struct rfd_ext *rfd_end;
2378 struct rfd_ext *rfd_out;
2380 /* Swap and adjust the RFD's. RFD's are only created by the
2381 linker, so this will only be necessary if one of the input
2382 files is the result of a partial link. Presumably all
2383 necessary RFD's are present. */
2384 rfd_in = input_ecoff->external_rfd;
2385 rfd_end = rfd_in + input_symhdr->crfd;
2386 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2387 for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
2391 ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
2392 rfd += output_symhdr->ifdMax;
2393 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2395 output_symhdr->crfd += input_symhdr->crfd;
2399 struct rfd_ext *rfd_out;
2400 struct rfd_ext *rfd_end;
2403 /* Create RFD's. Some of the debugging information includes
2404 relative file indices. These indices are taken as indices to
2405 the RFD table if there is one, or to the global table if
2406 there is not. If we did not create RFD's, we would have to
2407 parse and adjust all the debugging information which contains
2409 rfd = output_symhdr->ifdMax;
2410 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2411 rfd_end = rfd_out + input_symhdr->ifdMax;
2412 for (; rfd_out < rfd_end; rfd_out++, rfd++)
2413 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2414 output_symhdr->crfd += input_symhdr->ifdMax;
2417 /* We have to swap the external symbols in and out because we have
2418 to adjust the file descriptor indices. */
2419 ext_ptr = input_ecoff->external_ext;
2420 ext_end = ext_ptr + input_symhdr->iextMax;
2421 ext_out = output_ecoff->external_ext + output_symhdr->iextMax;
2422 for (; ext_ptr < ext_end; ext_ptr++, ext_out++)
2426 ecoff_swap_ext_in (input_bfd, ext_ptr, &ext);
2427 ext.ifd += output_symhdr->ifdMax;
2428 ecoff_swap_ext_out (output_bfd, &ext, ext_out);
2431 /* Update the counts. */
2432 output_symhdr->ilineMax += input_symhdr->ilineMax;
2433 output_symhdr->cbLine += input_symhdr->cbLine;
2434 output_symhdr->idnMax += input_symhdr->idnMax;
2435 output_symhdr->ipdMax += input_symhdr->ipdMax;
2436 output_symhdr->isymMax += input_symhdr->isymMax;
2437 output_symhdr->ioptMax += input_symhdr->ioptMax;
2438 output_symhdr->iauxMax += input_symhdr->iauxMax;
2439 output_symhdr->issMax += input_symhdr->issMax;
2440 output_symhdr->issExtMax += input_symhdr->issExtMax;
2441 output_symhdr->ifdMax += input_symhdr->ifdMax;
2442 output_symhdr->iextMax += input_symhdr->iextMax;
2444 /* Double check that the counts we got by stepping through the FDR's
2445 match the counts we got from input_symhdr. */
2446 BFD_ASSERT (output_symhdr->issMax == iss
2447 && output_symhdr->isymMax == isym
2448 && output_symhdr->ilineMax == iline
2449 && output_symhdr->ioptMax == iopt
2450 && output_symhdr->ipdMax == ipdr
2451 && output_symhdr->iauxMax == iaux
2452 && output_symhdr->cbLineOffset == cbline);
2457 /* This is the actual link routine. It makes two passes over all the
2461 DEFUN (ecoff_bfd_seclet_link, (abfd, data, relocateable),
2464 boolean relocateable)
2468 register asection *o;
2469 register bfd_seclet_type *p;
2473 /* We accumulate the debugging information counts in the symbolic
2475 symhdr = &ecoff_data (abfd)->symbolic_header;
2476 symhdr->ilineMax = 0;
2480 symhdr->isymMax = 0;
2481 symhdr->ioptMax = 0;
2482 symhdr->iauxMax = 0;
2484 symhdr->issExtMax = 0;
2487 symhdr->iextMax = 0;
2489 /* We are only going to look at each input BFD once. It is
2490 convenient to look at the code section first, so that the first
2491 time we look at a BFD we can set the text address (otherwise,
2492 when we get to the text section, we would have to be able to
2493 track down the file information associated with that BFD). So we
2494 actually do each pass in two sub passes; first the code sections,
2495 then the non-code sections. */
2497 /* Do the first pass: set the output section contents and count the
2498 debugging information. */
2499 ecoff_clear_output_flags (abfd);
2500 for (ipass = 0; ipass < 2; ipass++)
2502 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2504 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
2505 so they are done on pass 0. For other sections the
2506 expression is true, so they are done on pass 1. */
2507 if (((o->flags & SEC_CODE) == 0) != ipass)
2510 for (p = o->seclets_head;
2511 p != (bfd_seclet_type *) NULL;
2514 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
2521 /* Now the counts in symhdr are the correct size for the debugging
2522 information. We allocate the right amount of space, and reset
2523 the counts so that the second pass can use them as indices. It
2524 would be possible to output the debugging information directly to
2525 the file in pass 2, rather than to build it in memory and then
2526 write it out. Outputting to the file would require a lot of
2527 seeks and small writes, though, and I think this approach is
2529 size = (symhdr->cbLine * sizeof (unsigned char)
2530 + symhdr->idnMax * sizeof (struct dnr_ext)
2531 + symhdr->ipdMax * sizeof (struct pdr_ext)
2532 + symhdr->isymMax * sizeof (struct sym_ext)
2533 + symhdr->ioptMax * sizeof (struct opt_ext)
2534 + symhdr->iauxMax * sizeof (union aux_ext)
2535 + symhdr->issMax * sizeof (char)
2536 + symhdr->issExtMax * sizeof (char)
2537 + symhdr->ifdMax * sizeof (struct fdr_ext)
2538 + symhdr->crfd * sizeof (struct rfd_ext)
2539 + symhdr->iextMax * sizeof (struct ext_ext));
2540 raw = (char *) bfd_alloc (abfd, size);
2541 if (raw == (char *) NULL)
2543 bfd_error = no_memory;
2546 ecoff_data (abfd)->raw_size = size;
2547 ecoff_data (abfd)->raw_syments = (PTR) raw;
2549 /* Initialize the raw pointers. */
2550 #define SET(field, count, type) \
2551 ecoff_data (abfd)->field = (type *) raw; \
2552 raw += symhdr->count * sizeof (type)
2554 SET (line, cbLine, unsigned char);
2555 SET (external_dnr, idnMax, struct dnr_ext);
2556 SET (external_pdr, ipdMax, struct pdr_ext);
2557 SET (external_sym, isymMax, struct sym_ext);
2558 SET (external_opt, ioptMax, struct opt_ext);
2559 SET (external_aux, iauxMax, union aux_ext);
2560 SET (ss, issMax, char);
2561 SET (ssext, issExtMax, char);
2562 SET (external_fdr, ifdMax, struct fdr_ext);
2563 SET (external_rfd, crfd, struct rfd_ext);
2564 SET (external_ext, iextMax, struct ext_ext);
2567 /* Reset the counts so the second pass can use them to know how far
2569 symhdr->ilineMax = 0;
2573 symhdr->isymMax = 0;
2574 symhdr->ioptMax = 0;
2575 symhdr->iauxMax = 0;
2577 symhdr->issExtMax = 0;
2580 symhdr->iextMax = 0;
2582 /* Do the second pass: accumulate the debugging information. */
2583 ecoff_clear_output_flags (abfd);
2584 for (ipass = 0; ipass < 2; ipass++)
2586 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2588 if (((o->flags & SEC_CODE) == 0) != ipass)
2590 for (p = o->seclets_head;
2591 p != (bfd_seclet_type *) NULL;
2594 if (p->type == bfd_indirect_seclet)
2596 if (ecoff_get_debug (abfd, p, o) == false)
2606 /* Set the architecture. The only architecture we support here is
2607 mips. We set the architecture anyhow, since many callers ignore
2608 the return value. */
2611 DEFUN (ecoff_set_arch_mach, (abfd, arch, machine),
2613 enum bfd_architecture arch AND
2614 unsigned long machine)
2616 bfd_default_set_arch_mach (abfd, arch, machine);
2617 return arch == bfd_arch_mips;
2620 /* Calculate the file position for each section, and set
2624 DEFUN (ecoff_compute_section_file_positions, (abfd),
2634 if (bfd_get_start_address (abfd))
2636 /* A start address may have been added to the original file. In
2637 this case it will need an optional header to record it. */
2638 abfd->flags |= EXEC_P;
2641 if (abfd->flags & EXEC_P)
2644 sofar += abfd->section_count * SCNHSZ;
2646 previous = (asection *) NULL;
2647 for (current = abfd->sections;
2648 current != (asection *) NULL;
2649 current = current->next)
2651 /* Only deal with sections which have contents */
2652 if (! (current->flags & SEC_HAS_CONTENTS))
2655 /* Align the sections in the file to the same boundary on
2656 which they are aligned in virtual memory. */
2658 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2659 if (previous != (asection *) NULL)
2660 previous->_raw_size += sofar - old_sofar;
2662 current->filepos = sofar;
2664 sofar += current->_raw_size;
2666 /* make sure that this section is of the right size too */
2668 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
2669 current->_raw_size += sofar - old_sofar;
2673 ecoff_data (abfd)->reloc_filepos = sofar;
2676 /* Set the contents of a section. */
2679 DEFUN (ecoff_set_section_contents, (abfd, section, location, offset, count),
2681 asection *section AND
2684 bfd_size_type count)
2686 if (abfd->output_has_begun == false)
2687 ecoff_compute_section_file_positions (abfd);
2689 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
2692 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
2697 /* Write out an ECOFF file. */
2700 DEFUN (ecoff_write_object_contents, (abfd),
2706 file_ptr reloc_base;
2708 unsigned long reloc_size;
2709 unsigned long text_size;
2710 unsigned long text_start;
2711 unsigned long data_size;
2712 unsigned long data_start;
2713 unsigned long bss_size;
2714 struct internal_filehdr internal_f;
2715 struct internal_aouthdr internal_a;
2717 bfd_error = system_call_error;
2719 if(abfd->output_has_begun == false)
2720 ecoff_compute_section_file_positions(abfd);
2722 if (abfd->sections != (asection *) NULL)
2723 scn_base = abfd->sections->filepos;
2726 reloc_base = ecoff_data (abfd)->reloc_filepos;
2730 for (current = abfd->sections;
2731 current != (asection *)NULL;
2732 current = current->next)
2734 current->target_index = count;
2736 if (current->reloc_count != 0)
2738 bfd_size_type relsize;
2740 current->rel_filepos = reloc_base;
2741 relsize = current->reloc_count * RELSZ;
2742 reloc_size += relsize;
2743 reloc_base += relsize;
2746 current->rel_filepos = 0;
2749 sym_base = reloc_base + reloc_size;
2750 ecoff_data (abfd)->sym_filepos = sym_base;
2758 /* Write section headers to the file. */
2760 internal_f.f_nscns = 0;
2762 (file_ptr) ((abfd->flags & EXEC_P) ?
2763 (FILHSZ + AOUTSZ) : FILHSZ),
2766 for (current = abfd->sections;
2767 current != (asection *) NULL;
2768 current = current->next)
2770 struct internal_scnhdr section;
2773 ++internal_f.f_nscns;
2775 strncpy (section.s_name, current->name, sizeof section.s_name);
2777 /* FIXME: is this correct for shared libraries? I think it is
2778 but I have no platform to check. Ian Lance Taylor. */
2779 vma = bfd_get_section_vma (abfd, current);
2780 if (strcmp (current->name, _LIB) == 0)
2781 section.s_vaddr = 0;
2783 section.s_vaddr = vma;
2785 section.s_paddr = vma;
2786 section.s_size = bfd_get_section_size_before_reloc (current);
2788 /* If this section has no size or is unloadable then the scnptr
2790 if (current->_raw_size == 0
2791 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2792 section.s_scnptr = 0;
2794 section.s_scnptr = current->filepos;
2795 section.s_relptr = current->rel_filepos;
2797 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2798 object file produced by the assembler is supposed to point to
2799 information about how much room is required by objects of
2800 various different sizes. I think this only matters if we
2801 want the linker to compute the best size to use, or
2802 something. I don't know what happens if the information is
2804 section.s_lnnoptr = 0;
2806 section.s_nreloc = current->reloc_count;
2807 section.s_nlnno = 0;
2808 section.s_flags = sec_to_styp_flags (current->name, current->flags);
2813 ecoff_swap_scnhdr_out (abfd, (PTR) §ion, (PTR) &buff);
2814 if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
2818 /* FIXME: These numbers don't add up to what the MIPS tools
2819 produce, although I don't think it matters. */
2820 if ((section.s_flags & STYP_TEXT) != 0
2821 || (section.s_flags & STYP_RDATA) != 0
2822 || (section.s_flags & STYP_LIT8) != 0
2823 || (section.s_flags & STYP_LIT4) != 0)
2825 text_size += bfd_get_section_size_before_reloc (current);
2826 if (text_start == 0 || text_start > vma)
2829 else if ((section.s_flags & STYP_DATA) != 0
2830 || (section.s_flags & STYP_SDATA) != 0)
2832 data_size += bfd_get_section_size_before_reloc (current);
2833 if (data_start == 0 || data_start > vma)
2836 else if ((section.s_flags & STYP_BSS) != 0
2837 || (section.s_flags & STYP_SBSS) != 0)
2838 bss_size += bfd_get_section_size_before_reloc (current);
2841 /* Set up the file header. */
2843 internal_f.f_magic = MIPS_MAGIC_2;
2846 We will NOT put a fucking timestamp in the header here. Every time you
2847 put it back, I will come in and take it out again. I'm sorry. This
2848 field does not belong here. We fill it with a 0 so it compares the
2849 same but is not a reasonable time. -- gnu@cygnus.com
2851 internal_f.f_timdat = 0;
2853 internal_f.f_nsyms = bfd_get_symcount(abfd);
2854 if (internal_f.f_nsyms != 0)
2855 internal_f.f_symptr = sym_base;
2857 internal_f.f_symptr = 0;
2859 if (abfd->flags & EXEC_P)
2860 internal_f.f_opthdr = AOUTSZ;
2862 internal_f.f_opthdr = 0;
2864 internal_f.f_flags = 0;
2865 if (reloc_size == 0)
2866 internal_f.f_flags |= F_RELFLG;
2867 if (bfd_get_symcount (abfd) == 0)
2868 internal_f.f_flags |= F_LSYMS;
2869 if (abfd->flags & EXEC_P)
2870 internal_f.f_flags |= F_EXEC;
2872 if (! abfd->xvec->byteorder_big_p)
2873 internal_f.f_flags |= F_AR32WR;
2875 internal_f.f_flags |= F_AR32W;
2877 /* Set up the optional header. */
2879 if ((abfd->flags & EXEC_P) != 0)
2881 internal_a.magic = ZMAGIC;
2883 /* FIXME: What should this be? */
2884 internal_a.vstamp = 0;
2886 internal_a.tsize = text_size;
2887 internal_a.text_start = text_start;
2888 internal_a.dsize = data_size;
2889 internal_a.data_start = data_start;
2890 internal_a.bsize = bss_size;
2892 internal_a.entry = bfd_get_start_address (abfd);
2894 /* FIXME: The MIPS optional header is larger than this.... */
2897 /* Write out the file header and the optional header. */
2899 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2904 ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
2905 if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
2909 if ((abfd->flags & EXEC_P) != 0)
2913 ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
2914 if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
2918 /* Write out the relocs. */
2920 /* Write out the symbolic debugging information. */
2921 if (bfd_get_symcount (abfd) > 0)
2923 struct hdr_ext buff;
2925 ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
2926 if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
2928 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
2929 ecoff_data (abfd)->raw_size, abfd)
2930 != ecoff_data (abfd)->raw_size)
2937 static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
2938 (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
2939 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
2940 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
2941 (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
2942 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
2943 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
2944 ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
2945 ecoff_swap_scnhdr_out,
2946 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
2947 ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
2948 ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
2949 styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
2950 ecoff_slurp_symbol_table
2953 /* get_lineno could be written for ECOFF, but it would currently only
2954 be useful for linking ECOFF and COFF files together, which doesn't
2956 #define ecoff_get_lineno \
2957 ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
2959 #define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
2960 #define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
2961 #define ecoff_core_file_matches_executable_p _bfd_dummy_core_file_matches_executable_p
2962 #define ecoff_slurp_armap bfd_slurp_coff_armap
2963 #define ecoff_slurp_extended_name_table _bfd_slurp_extended_name_table
2964 #define ecoff_write_armap coff_write_armap
2965 #define ecoff_truncate_arname bfd_dont_truncate_arname
2966 #define ecoff_openr_next_archived_file bfd_generic_openr_next_archived_file
2967 #define ecoff_generic_stat_arch_elt bfd_generic_stat_arch_elt
2968 #define ecoff_get_section_contents bfd_generic_get_section_contents
2969 #define ecoff_get_reloc_upper_bound coff_get_reloc_upper_bound
2970 #define ecoff_close_and_cleanup bfd_generic_close_and_cleanup
2971 #define ecoff_sizeof_headers coff_sizeof_headers
2972 #define ecoff_bfd_debug_info_start bfd_void
2973 #define ecoff_bfd_debug_info_end bfd_void
2974 #define ecoff_bfd_debug_info_accumulate \
2975 ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
2976 #define ecoff_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
2977 #define ecoff_bfd_relax_section bfd_generic_relax_section
2979 bfd_target ecoff_little_vec =
2981 "ecoff-littlemips", /* name */
2982 bfd_target_ecoff_flavour,
2983 false, /* data byte order is little */
2984 false, /* header byte order is little */
2986 (HAS_RELOC | EXEC_P | /* object flags */
2987 HAS_LINENO | HAS_DEBUG |
2988 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
2990 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
2992 0, /* leading underscore */
2993 '/', /* ar_pad_char */
2994 15, /* ar_max_namelen */
2995 3, /* minimum alignment power */
2996 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
2997 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
2999 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3000 bfd_generic_archive_p, _bfd_dummy_target},
3001 {bfd_false, ecoff_mkobject, bfd_false, /* bfd_set_format */
3003 {bfd_false, ecoff_write_object_contents, bfd_false, bfd_false},
3006 (PTR) &bfd_ecoff_std_swap_table
3009 bfd_target ecoff_big_vec =
3011 "ecoff-bigmips", /* name */
3012 bfd_target_ecoff_flavour,
3013 true, /* data byte order is big */
3014 true, /* header byte order is big */
3016 (HAS_RELOC | EXEC_P | /* object flags */
3017 HAS_LINENO | HAS_DEBUG |
3018 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
3020 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
3021 0, /* leading underscore */
3022 ' ', /* ar_pad_char */
3023 16, /* ar_max_namelen */
3024 3, /* minimum alignment power */
3025 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3026 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
3027 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
3028 bfd_generic_archive_p, _bfd_dummy_target},
3029 {bfd_false, ecoff_mkobject, bfd_false, /* bfd_set_format */
3031 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
3032 bfd_false, bfd_false},
3035 (PTR) &bfd_ecoff_std_swap_table
3036 /* Note that there is another bfd_target just above this one. If
3037 you are adding initializers here, you should be adding them there