Automatic date update in version.in
[binutils-gdb.git] / bfd / ecoff.c
blobe8804bf2236fbd07c1cadc4dcafb72b327d0977e
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright (C) 1990-2024 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 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., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "sysdep.h"
24 #include "bfd.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "ecoff-bfd.h"
28 #include "aout/ar.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
33 we do want. */
34 #include "libaout.h"
35 #include "aout/aout64.h"
36 #undef N_ABS
37 #undef exec_hdr
38 #undef obj_sym_filepos
40 #include "coff/internal.h"
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
46 #include "libiberty.h"
48 #define streq(a, b) (strcmp ((a), (b)) == 0)
51 /* This stuff is somewhat copied from coffcode.h. */
52 static asection bfd_debug_section =
53 BFD_FAKE_SECTION (bfd_debug_section, NULL, "*DEBUG*", 0, 0);
55 /* Create an ECOFF object. */
57 bool
58 _bfd_ecoff_mkobject (bfd *abfd)
60 size_t amt = sizeof (ecoff_data_type);
62 abfd->tdata.ecoff_obj_data = (struct ecoff_tdata *) bfd_zalloc (abfd, amt);
63 if (abfd->tdata.ecoff_obj_data == NULL)
64 return false;
66 return true;
69 /* This is a hook called by coff_real_object_p to create any backend
70 specific information. */
72 void *
73 _bfd_ecoff_mkobject_hook (bfd *abfd, void * filehdr, void * aouthdr)
75 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
76 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
77 ecoff_data_type *ecoff;
79 if (! _bfd_ecoff_mkobject (abfd))
80 return NULL;
82 ecoff = ecoff_data (abfd);
83 ecoff->gp_size = 8;
84 ecoff->sym_filepos = internal_f->f_symptr;
86 if (internal_a != NULL)
88 int i;
90 ecoff->text_start = internal_a->text_start;
91 ecoff->text_end = internal_a->text_start + internal_a->tsize;
92 ecoff->gp = internal_a->gp_value;
93 ecoff->gprmask = internal_a->gprmask;
94 for (i = 0; i < 4; i++)
95 ecoff->cprmask[i] = internal_a->cprmask[i];
96 ecoff->fprmask = internal_a->fprmask;
97 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
98 abfd->flags |= D_PAGED;
99 else
100 abfd->flags &=~ D_PAGED;
103 /* It turns out that no special action is required by the MIPS or
104 Alpha ECOFF backends. They have different information in the
105 a.out header, but we just copy it all (e.g., gprmask, cprmask and
106 fprmask) and let the swapping routines ensure that only relevant
107 information is written out. */
109 return (void *) ecoff;
112 bool
113 _bfd_ecoff_bfd_free_cached_info (bfd *abfd)
115 struct ecoff_tdata *tdata;
117 if ((bfd_get_format (abfd) == bfd_object
118 || bfd_get_format (abfd) == bfd_core)
119 && (tdata = ecoff_data (abfd)) != NULL)
121 while (tdata->mips_refhi_list != NULL)
123 struct mips_hi *ref = tdata->mips_refhi_list;
124 tdata->mips_refhi_list = ref->next;
125 free (ref);
127 _bfd_ecoff_free_ecoff_debug_info (&tdata->debug_info);
129 return _bfd_generic_bfd_free_cached_info (abfd);
132 /* Initialize a new section. */
134 bool
135 _bfd_ecoff_new_section_hook (bfd *abfd, asection *section)
137 unsigned int i;
138 static struct
140 const char * name;
141 flagword flags;
143 section_flags [] =
145 { _TEXT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
146 { _INIT, SEC_ALLOC | SEC_CODE | SEC_LOAD },
147 { _FINI, SEC_ALLOC | SEC_CODE | SEC_LOAD },
148 { _DATA, SEC_ALLOC | SEC_DATA | SEC_LOAD },
149 { _SDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_SMALL_DATA },
150 { _RDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
151 { _LIT8, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
152 { _LIT4, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY | SEC_SMALL_DATA},
153 { _RCONST, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
154 { _PDATA, SEC_ALLOC | SEC_DATA | SEC_LOAD | SEC_READONLY},
155 { _BSS, SEC_ALLOC},
156 { _SBSS, SEC_ALLOC | SEC_SMALL_DATA},
157 /* An Irix 4 shared libary. */
158 { _LIB, SEC_COFF_SHARED_LIBRARY}
161 section->alignment_power = 4;
163 for (i = 0; i < ARRAY_SIZE (section_flags); i++)
164 if (streq (section->name, section_flags[i].name))
166 section->flags |= section_flags[i].flags;
167 break;
171 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
172 uncertain about .init on some systems and I don't know how shared
173 libraries work. */
175 return _bfd_generic_new_section_hook (abfd, section);
178 void
179 _bfd_ecoff_set_alignment_hook (bfd *abfd ATTRIBUTE_UNUSED,
180 asection *section ATTRIBUTE_UNUSED,
181 void *scnhdr ATTRIBUTE_UNUSED)
185 /* Determine the machine architecture and type. This is called from
186 the generic COFF routines. It is the inverse of ecoff_get_magic,
187 below. This could be an ECOFF backend routine, with one version
188 for each target, but there aren't all that many ECOFF targets. */
190 bool
191 _bfd_ecoff_set_arch_mach_hook (bfd *abfd, void * filehdr)
193 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
194 enum bfd_architecture arch;
195 unsigned long mach;
197 switch (internal_f->f_magic)
199 case MIPS_MAGIC_1:
200 case MIPS_MAGIC_LITTLE:
201 case MIPS_MAGIC_BIG:
202 arch = bfd_arch_mips;
203 mach = bfd_mach_mips3000;
204 break;
206 case MIPS_MAGIC_LITTLE2:
207 case MIPS_MAGIC_BIG2:
208 /* MIPS ISA level 2: the r6000. */
209 arch = bfd_arch_mips;
210 mach = bfd_mach_mips6000;
211 break;
213 case MIPS_MAGIC_LITTLE3:
214 case MIPS_MAGIC_BIG3:
215 /* MIPS ISA level 3: the r4000. */
216 arch = bfd_arch_mips;
217 mach = bfd_mach_mips4000;
218 break;
220 case ALPHA_MAGIC:
221 arch = bfd_arch_alpha;
222 mach = 0;
223 break;
225 default:
226 arch = bfd_arch_obscure;
227 mach = 0;
228 break;
231 return bfd_default_set_arch_mach (abfd, arch, mach);
234 bool
235 _bfd_ecoff_no_long_sections (bfd *abfd, int enable)
237 (void) abfd;
238 (void) enable;
239 return false;
242 /* Get the magic number to use based on the architecture and machine.
243 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
245 static int
246 ecoff_get_magic (bfd *abfd)
248 int big, little;
250 switch (bfd_get_arch (abfd))
252 case bfd_arch_mips:
253 switch (bfd_get_mach (abfd))
255 default:
256 case 0:
257 case bfd_mach_mips3000:
258 big = MIPS_MAGIC_BIG;
259 little = MIPS_MAGIC_LITTLE;
260 break;
262 case bfd_mach_mips6000:
263 big = MIPS_MAGIC_BIG2;
264 little = MIPS_MAGIC_LITTLE2;
265 break;
267 case bfd_mach_mips4000:
268 big = MIPS_MAGIC_BIG3;
269 little = MIPS_MAGIC_LITTLE3;
270 break;
273 return bfd_big_endian (abfd) ? big : little;
275 case bfd_arch_alpha:
276 return ALPHA_MAGIC;
278 default:
279 abort ();
280 return 0;
284 /* Get the section s_flags to use for a section. */
286 static long
287 ecoff_sec_to_styp_flags (const char *name, flagword flags)
289 unsigned int i;
290 static struct
292 const char * name;
293 long flags;
295 styp_flags [] =
297 { _TEXT, STYP_TEXT },
298 { _DATA, STYP_DATA },
299 { _SDATA, STYP_SDATA },
300 { _RDATA, STYP_RDATA },
301 { _LITA, STYP_LITA },
302 { _LIT8, STYP_LIT8 },
303 { _LIT4, STYP_LIT4 },
304 { _BSS, STYP_BSS },
305 { _SBSS, STYP_SBSS },
306 { _INIT, STYP_ECOFF_INIT },
307 { _FINI, STYP_ECOFF_FINI },
308 { _PDATA, STYP_PDATA },
309 { _XDATA, STYP_XDATA },
310 { _LIB, STYP_ECOFF_LIB },
311 { _GOT, STYP_GOT },
312 { _HASH, STYP_HASH },
313 { _DYNAMIC, STYP_DYNAMIC },
314 { _LIBLIST, STYP_LIBLIST },
315 { _RELDYN, STYP_RELDYN },
316 { _CONFLIC, STYP_CONFLIC },
317 { _DYNSTR, STYP_DYNSTR },
318 { _DYNSYM, STYP_DYNSYM },
319 { _RCONST, STYP_RCONST }
321 long styp = 0;
323 for (i = 0; i < ARRAY_SIZE (styp_flags); i++)
324 if (streq (name, styp_flags[i].name))
326 styp = styp_flags[i].flags;
327 break;
330 if (styp == 0)
332 if (streq (name, _COMMENT))
334 styp = STYP_COMMENT;
335 flags &=~ SEC_NEVER_LOAD;
337 else if (flags & SEC_CODE)
338 styp = STYP_TEXT;
339 else if (flags & SEC_DATA)
340 styp = STYP_DATA;
341 else if (flags & SEC_READONLY)
342 styp = STYP_RDATA;
343 else if (flags & SEC_LOAD)
344 styp = STYP_REG;
345 else
346 styp = STYP_BSS;
349 if (flags & SEC_NEVER_LOAD)
350 styp |= STYP_NOLOAD;
352 return styp;
355 /* Get the BFD flags to use for a section. */
357 bool
358 _bfd_ecoff_styp_to_sec_flags (bfd *abfd ATTRIBUTE_UNUSED,
359 void * hdr,
360 const char *name ATTRIBUTE_UNUSED,
361 asection *section ATTRIBUTE_UNUSED,
362 flagword * flags_ptr)
364 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
365 long styp_flags = internal_s->s_flags;
366 flagword sec_flags = 0;
368 if (styp_flags & STYP_NOLOAD)
369 sec_flags |= SEC_NEVER_LOAD;
371 /* For 386 COFF, at least, an unloadable text or data section is
372 actually a shared library section. */
373 if ((styp_flags & STYP_TEXT)
374 || (styp_flags & STYP_ECOFF_INIT)
375 || (styp_flags & STYP_ECOFF_FINI)
376 || (styp_flags & STYP_DYNAMIC)
377 || (styp_flags & STYP_LIBLIST)
378 || (styp_flags & STYP_RELDYN)
379 || styp_flags == STYP_CONFLIC
380 || (styp_flags & STYP_DYNSTR)
381 || (styp_flags & STYP_DYNSYM)
382 || (styp_flags & STYP_HASH))
384 if (sec_flags & SEC_NEVER_LOAD)
385 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
386 else
387 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
389 else if ((styp_flags & STYP_DATA)
390 || (styp_flags & STYP_RDATA)
391 || (styp_flags & STYP_SDATA)
392 || styp_flags == STYP_PDATA
393 || styp_flags == STYP_XDATA
394 || (styp_flags & STYP_GOT)
395 || styp_flags == STYP_RCONST)
397 if (sec_flags & SEC_NEVER_LOAD)
398 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
399 else
400 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
401 if ((styp_flags & STYP_RDATA)
402 || styp_flags == STYP_PDATA
403 || styp_flags == STYP_RCONST)
404 sec_flags |= SEC_READONLY;
405 if (styp_flags & STYP_SDATA)
406 sec_flags |= SEC_SMALL_DATA;
408 else if (styp_flags & STYP_SBSS)
409 sec_flags |= SEC_ALLOC | SEC_SMALL_DATA;
410 else if (styp_flags & STYP_BSS)
411 sec_flags |= SEC_ALLOC;
412 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
413 sec_flags |= SEC_NEVER_LOAD;
414 else if ((styp_flags & STYP_LITA)
415 || (styp_flags & STYP_LIT8)
416 || (styp_flags & STYP_LIT4))
417 sec_flags |= SEC_DATA |SEC_SMALL_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
418 else if (styp_flags & STYP_ECOFF_LIB)
419 sec_flags |= SEC_COFF_SHARED_LIBRARY;
420 else
421 sec_flags |= SEC_ALLOC | SEC_LOAD;
423 * flags_ptr = sec_flags;
424 return true;
427 /* Read in the symbolic header for an ECOFF object file. */
429 static bool
430 ecoff_slurp_symbolic_header (bfd *abfd)
432 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
433 bfd_size_type external_hdr_size;
434 void * raw = NULL;
435 HDRR *internal_symhdr;
437 /* See if we've already read it in. */
438 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
439 backend->debug_swap.sym_magic)
440 return true;
442 /* See whether there is a symbolic header. */
443 if (ecoff_data (abfd)->sym_filepos == 0)
445 abfd->symcount = 0;
446 return true;
449 /* At this point bfd_get_symcount (abfd) holds the number of symbols
450 as read from the file header, but on ECOFF this is always the
451 size of the symbolic information header. It would be cleaner to
452 handle this when we first read the file in coffgen.c. */
453 external_hdr_size = backend->debug_swap.external_hdr_size;
454 if (bfd_get_symcount (abfd) != external_hdr_size)
456 bfd_set_error (bfd_error_bad_value);
457 return false;
460 /* Read the symbolic information header. */
461 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) != 0)
462 goto error_return;
463 raw = _bfd_malloc_and_read (abfd, external_hdr_size, external_hdr_size);
464 if (raw == NULL)
465 goto error_return;
467 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
468 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
470 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
472 bfd_set_error (bfd_error_bad_value);
473 goto error_return;
476 #define FIX(start, count) \
477 if (internal_symhdr->start == 0) \
478 internal_symhdr->count = 0;
480 FIX (cbLineOffset, cbLine);
481 FIX (cbDnOffset, idnMax);
482 FIX (cbPdOffset, ipdMax);
483 FIX (cbSymOffset, isymMax);
484 FIX (cbOptOffset, ioptMax);
485 FIX (cbAuxOffset, iauxMax);
486 FIX (cbSsOffset, issMax);
487 FIX (cbSsExtOffset, issExtMax);
488 FIX (cbFdOffset, ifdMax);
489 FIX (cbRfdOffset, crfd);
490 FIX (cbExtOffset, iextMax);
491 #undef FIX
493 /* Now we can get the correct number of symbols. */
494 abfd->symcount = internal_symhdr->isymMax + internal_symhdr->iextMax;
496 free (raw);
497 return true;
498 error_return:
499 free (raw);
500 return false;
503 /* Read in and swap the important symbolic information for an ECOFF
504 object file. This is called by gdb via the read_debug_info entry
505 point in the backend structure. */
507 bool
508 _bfd_ecoff_slurp_symbolic_info (bfd *abfd,
509 asection *ignore ATTRIBUTE_UNUSED,
510 struct ecoff_debug_info *debug)
512 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
513 HDRR *internal_symhdr;
514 bfd_size_type raw_base;
515 bfd_size_type raw_size;
516 void * raw;
517 bfd_size_type external_fdr_size;
518 char *fraw_src;
519 char *fraw_end;
520 struct fdr *fdr_ptr;
521 bfd_size_type raw_end;
522 bfd_size_type cb_end;
523 file_ptr pos;
524 size_t amt;
526 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
528 /* Check whether we've already gotten it, and whether there's any to
529 get. */
530 if (debug->alloc_syments)
531 return true;
532 if (ecoff_data (abfd)->sym_filepos == 0)
534 abfd->symcount = 0;
535 return true;
538 if (! ecoff_slurp_symbolic_header (abfd))
539 return false;
541 internal_symhdr = &debug->symbolic_header;
543 /* Read all the symbolic information at once. */
544 raw_base = (ecoff_data (abfd)->sym_filepos
545 + backend->debug_swap.external_hdr_size);
547 /* Alpha ecoff makes the determination of raw_size difficult. It has
548 an undocumented debug data section between the symhdr and the first
549 documented section. And the ordering of the sections varies between
550 statically and dynamically linked executables.
551 If bfd supports SEEK_END someday, this code could be simplified. */
552 raw_end = raw_base;
554 #define UPDATE_RAW_END(start, count, size) \
555 do \
556 if (internal_symhdr->count != 0) \
558 if (internal_symhdr->start < raw_base) \
559 goto err; \
560 if (_bfd_mul_overflow ((unsigned long) internal_symhdr->count, \
561 (size), &amt)) \
562 goto err; \
563 cb_end = internal_symhdr->start + amt; \
564 if (cb_end < internal_symhdr->start) \
565 goto err; \
566 if (cb_end > raw_end) \
567 raw_end = cb_end; \
569 while (0)
571 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
572 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
573 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
574 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
575 /* eraxxon@alumni.rice.edu: ioptMax refers to the size of the
576 optimization symtab, not the number of entries. */
577 UPDATE_RAW_END (cbOptOffset, ioptMax, sizeof (char));
578 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
579 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
580 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
581 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
582 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
583 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
585 #undef UPDATE_RAW_END
587 raw_size = raw_end - raw_base;
588 if (raw_size == 0)
590 ecoff_data (abfd)->sym_filepos = 0;
591 return true;
593 pos = ecoff_data (abfd)->sym_filepos;
594 pos += backend->debug_swap.external_hdr_size;
595 if (bfd_seek (abfd, pos, SEEK_SET) != 0)
596 return false;
597 raw = _bfd_alloc_and_read (abfd, raw_size, raw_size);
598 if (raw == NULL)
599 return false;
601 debug->alloc_syments = true;
603 /* Get pointers for the numeric offsets in the HDRR structure. */
604 #define FIX(start, count, ptr, type) \
605 if (internal_symhdr->count == 0) \
606 debug->ptr = NULL; \
607 else \
608 debug->ptr = (type) ((char *) raw \
609 + (internal_symhdr->start - raw_base))
611 FIX (cbLineOffset, cbLine, line, unsigned char *);
612 FIX (cbDnOffset, idnMax, external_dnr, void *);
613 FIX (cbPdOffset, ipdMax, external_pdr, void *);
614 FIX (cbSymOffset, isymMax, external_sym, void *);
615 FIX (cbOptOffset, ioptMax, external_opt, void *);
616 FIX (cbAuxOffset, iauxMax, external_aux, union aux_ext *);
617 FIX (cbSsOffset, issMax, ss, char *);
618 FIX (cbSsExtOffset, issExtMax, ssext, char *);
619 FIX (cbFdOffset, ifdMax, external_fdr, void *);
620 FIX (cbRfdOffset, crfd, external_rfd, void *);
621 FIX (cbExtOffset, iextMax, external_ext, void *);
622 #undef FIX
624 /* Ensure string sections are zero terminated. */
625 if (debug->ss)
626 debug->ss[internal_symhdr->issMax - 1] = 0;
627 if (debug->ssext)
628 debug->ssext[internal_symhdr->issExtMax - 1] = 0;
630 /* I don't want to always swap all the data, because it will just
631 waste time and most programs will never look at it. The only
632 time the linker needs most of the debugging information swapped
633 is when linking big-endian and little-endian MIPS object files
634 together, which is not a common occurrence.
636 We need to look at the fdr to deal with a lot of information in
637 the symbols, so we swap them here. */
638 if (_bfd_mul_overflow ((unsigned long) internal_symhdr->ifdMax,
639 sizeof (struct fdr), &amt))
641 err:
642 bfd_set_error (bfd_error_file_too_big);
643 return false;
645 debug->fdr = (FDR *) bfd_alloc (abfd, amt);
646 if (debug->fdr == NULL)
647 return false;
648 external_fdr_size = backend->debug_swap.external_fdr_size;
649 fdr_ptr = debug->fdr;
650 fraw_src = (char *) debug->external_fdr;
651 /* PR 17512: file: 3372-1243-0.004. */
652 if (fraw_src == NULL && internal_symhdr->ifdMax > 0)
653 return false;
654 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
655 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
656 (*backend->debug_swap.swap_fdr_in) (abfd, (void *) fraw_src, fdr_ptr);
658 return true;
661 /* ECOFF symbol table routines. The ECOFF symbol table is described
662 in gcc/mips-tfile.c. */
664 /* ECOFF uses two common sections. One is the usual one, and the
665 other is for small objects. All the small objects are kept
666 together, and then referenced via the gp pointer, which yields
667 faster assembler code. This is what we use for the small common
668 section. */
669 static asection ecoff_scom_section;
670 static const asymbol ecoff_scom_symbol =
671 GLOBAL_SYM_INIT (SCOMMON, &ecoff_scom_section);
672 static asection ecoff_scom_section =
673 BFD_FAKE_SECTION (ecoff_scom_section, &ecoff_scom_symbol,
674 SCOMMON, 0, SEC_IS_COMMON | SEC_SMALL_DATA);
676 /* Create an empty symbol. */
678 asymbol *
679 _bfd_ecoff_make_empty_symbol (bfd *abfd)
681 ecoff_symbol_type *new_symbol;
682 size_t amt = sizeof (ecoff_symbol_type);
684 new_symbol = (ecoff_symbol_type *) bfd_zalloc (abfd, amt);
685 if (new_symbol == NULL)
686 return NULL;
687 new_symbol->symbol.section = NULL;
688 new_symbol->fdr = NULL;
689 new_symbol->local = false;
690 new_symbol->native = NULL;
691 new_symbol->symbol.the_bfd = abfd;
692 return &new_symbol->symbol;
695 /* Set the BFD flags and section for an ECOFF symbol. */
697 static bool
698 ecoff_set_symbol_info (bfd *abfd,
699 SYMR *ecoff_sym,
700 asymbol *asym,
701 int ext,
702 int weak)
704 asym->the_bfd = abfd;
705 asym->value = ecoff_sym->value;
706 asym->section = &bfd_debug_section;
707 asym->udata.i = 0;
709 /* Most symbol types are just for debugging. */
710 switch (ecoff_sym->st)
712 case stGlobal:
713 case stStatic:
714 case stLabel:
715 case stProc:
716 case stStaticProc:
717 break;
718 case stNil:
719 if (ECOFF_IS_STAB (ecoff_sym))
721 asym->flags = BSF_DEBUGGING;
722 return true;
724 break;
725 default:
726 asym->flags = BSF_DEBUGGING;
727 return true;
730 if (weak)
731 asym->flags = BSF_EXPORT | BSF_WEAK;
732 else if (ext)
733 asym->flags = BSF_EXPORT | BSF_GLOBAL;
734 else
736 asym->flags = BSF_LOCAL;
737 /* Normally, a local stProc symbol will have a corresponding
738 external symbol. We mark the local symbol as a debugging
739 symbol, in order to prevent nm from printing both out.
740 Similarly, we mark stLabel and stabs symbols as debugging
741 symbols. In both cases, we do want to set the value
742 correctly based on the symbol class. */
743 if (ecoff_sym->st == stProc
744 || ecoff_sym->st == stLabel
745 || ECOFF_IS_STAB (ecoff_sym))
746 asym->flags |= BSF_DEBUGGING;
749 if (ecoff_sym->st == stProc || ecoff_sym->st == stStaticProc)
750 asym->flags |= BSF_FUNCTION;
752 switch (ecoff_sym->sc)
754 case scNil:
755 /* Used for compiler generated labels. Leave them in the
756 debugging section, and mark them as local. If BSF_DEBUGGING
757 is set, then nm does not display them for some reason. If no
758 flags are set then the linker whines about them. */
759 asym->flags = BSF_LOCAL;
760 break;
761 case scText:
762 asym->section = bfd_make_section_old_way (abfd, _TEXT);
763 asym->value -= asym->section->vma;
764 break;
765 case scData:
766 asym->section = bfd_make_section_old_way (abfd, _DATA);
767 asym->value -= asym->section->vma;
768 break;
769 case scBss:
770 asym->section = bfd_make_section_old_way (abfd, _BSS);
771 asym->value -= asym->section->vma;
772 break;
773 case scRegister:
774 asym->flags = BSF_DEBUGGING;
775 break;
776 case scAbs:
777 asym->section = bfd_abs_section_ptr;
778 break;
779 case scUndefined:
780 asym->section = bfd_und_section_ptr;
781 asym->flags = 0;
782 asym->value = 0;
783 break;
784 case scCdbLocal:
785 case scBits:
786 case scCdbSystem:
787 case scRegImage:
788 case scInfo:
789 case scUserStruct:
790 asym->flags = BSF_DEBUGGING;
791 break;
792 case scSData:
793 asym->section = bfd_make_section_old_way (abfd, ".sdata");
794 asym->value -= asym->section->vma;
795 break;
796 case scSBss:
797 asym->section = bfd_make_section_old_way (abfd, ".sbss");
798 asym->value -= asym->section->vma;
799 break;
800 case scRData:
801 asym->section = bfd_make_section_old_way (abfd, ".rdata");
802 asym->value -= asym->section->vma;
803 break;
804 case scVar:
805 asym->flags = BSF_DEBUGGING;
806 break;
807 case scCommon:
808 if (asym->value > ecoff_data (abfd)->gp_size)
810 asym->section = bfd_com_section_ptr;
811 asym->flags = 0;
812 break;
814 /* Fall through. */
815 case scSCommon:
816 asym->section = &ecoff_scom_section;
817 asym->flags = 0;
818 break;
819 case scVarRegister:
820 case scVariant:
821 asym->flags = BSF_DEBUGGING;
822 break;
823 case scSUndefined:
824 asym->section = bfd_und_section_ptr;
825 asym->flags = 0;
826 asym->value = 0;
827 break;
828 case scInit:
829 asym->section = bfd_make_section_old_way (abfd, ".init");
830 asym->value -= asym->section->vma;
831 break;
832 case scBasedVar:
833 case scXData:
834 case scPData:
835 asym->flags = BSF_DEBUGGING;
836 break;
837 case scFini:
838 asym->section = bfd_make_section_old_way (abfd, ".fini");
839 asym->value -= asym->section->vma;
840 break;
841 case scRConst:
842 asym->section = bfd_make_section_old_way (abfd, ".rconst");
843 asym->value -= asym->section->vma;
844 break;
845 default:
846 break;
849 /* Look for special constructors symbols and make relocation entries
850 in a special construction section. These are produced by the
851 -fgnu-linker argument to g++. */
852 if (ECOFF_IS_STAB (ecoff_sym))
854 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
856 default:
857 break;
859 case N_SETA:
860 case N_SETT:
861 case N_SETD:
862 case N_SETB:
863 /* Mark the symbol as a constructor. */
864 asym->flags |= BSF_CONSTRUCTOR;
865 break;
868 return true;
871 /* Read an ECOFF symbol table. */
873 bool
874 _bfd_ecoff_slurp_symbol_table (bfd *abfd)
876 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
877 const bfd_size_type external_ext_size
878 = backend->debug_swap.external_ext_size;
879 const bfd_size_type external_sym_size
880 = backend->debug_swap.external_sym_size;
881 void (* const swap_ext_in) (bfd *, void *, EXTR *)
882 = backend->debug_swap.swap_ext_in;
883 void (* const swap_sym_in) (bfd *, void *, SYMR *)
884 = backend->debug_swap.swap_sym_in;
885 ecoff_symbol_type *internal;
886 ecoff_symbol_type *internal_ptr;
887 char *eraw_src;
888 char *eraw_end;
889 FDR *fdr_ptr;
890 FDR *fdr_end;
891 size_t amt;
893 /* If we've already read in the symbol table, do nothing. */
894 if (ecoff_data (abfd)->canonical_symbols != NULL)
895 return true;
897 /* Get the symbolic information. */
898 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
899 &ecoff_data (abfd)->debug_info))
900 return false;
901 if (bfd_get_symcount (abfd) == 0)
902 return true;
904 if (_bfd_mul_overflow (bfd_get_symcount (abfd),
905 sizeof (ecoff_symbol_type), &amt))
907 bfd_set_error (bfd_error_file_too_big);
908 return false;
910 internal = (ecoff_symbol_type *) bfd_alloc (abfd, amt);
911 if (internal == NULL)
912 return false;
914 internal_ptr = internal;
915 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
916 eraw_end = (eraw_src
917 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
918 * external_ext_size));
919 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
921 EXTR internal_esym;
923 (*swap_ext_in) (abfd, (void *) eraw_src, &internal_esym);
925 /* PR 17512: file: 3372-1000-0.004. */
926 HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
927 if (internal_esym.asym.iss >= symhdr->issExtMax
928 || internal_esym.asym.iss < 0)
930 bfd_set_error (bfd_error_bad_value);
931 return false;
934 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
935 + internal_esym.asym.iss);
937 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
938 &internal_ptr->symbol, 1,
939 internal_esym.weakext))
940 return false;
942 /* The alpha uses a negative ifd field for section symbols. */
943 /* PR 17512: file: 3372-1983-0.004. */
944 if (internal_esym.ifd >= symhdr->ifdMax
945 || internal_esym.ifd < 0)
946 internal_ptr->fdr = NULL;
947 else
948 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
949 + internal_esym.ifd);
950 internal_ptr->local = false;
951 internal_ptr->native = (void *) eraw_src;
954 /* The local symbols must be accessed via the fdr's, because the
955 string and aux indices are relative to the fdr information. */
956 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
957 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
958 for (; fdr_ptr < fdr_end; fdr_ptr++)
960 char *lraw_src;
961 char *lraw_end;
962 HDRR *symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
964 if (fdr_ptr->csym == 0)
965 continue;
966 if (fdr_ptr->isymBase < 0
967 || fdr_ptr->isymBase > symhdr->isymMax
968 || fdr_ptr->csym < 0
969 || fdr_ptr->csym > symhdr->isymMax - fdr_ptr->isymBase
970 || fdr_ptr->csym > ((long) bfd_get_symcount (abfd)
971 - (internal_ptr - internal))
972 || fdr_ptr->issBase < 0
973 || fdr_ptr->issBase > symhdr->issMax)
975 bfd_set_error (bfd_error_bad_value);
976 return false;
978 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
979 + fdr_ptr->isymBase * external_sym_size);
980 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
981 for (;
982 lraw_src < lraw_end;
983 lraw_src += external_sym_size, internal_ptr++)
985 SYMR internal_sym;
987 (*swap_sym_in) (abfd, (void *) lraw_src, &internal_sym);
989 if (internal_sym.iss >= symhdr->issMax - fdr_ptr->issBase
990 || internal_sym.iss < 0)
992 bfd_set_error (bfd_error_bad_value);
993 return false;
995 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
996 + fdr_ptr->issBase
997 + internal_sym.iss);
998 if (!ecoff_set_symbol_info (abfd, &internal_sym,
999 &internal_ptr->symbol, 0, 0))
1000 return false;
1001 internal_ptr->fdr = fdr_ptr;
1002 internal_ptr->local = true;
1003 internal_ptr->native = (void *) lraw_src;
1007 /* PR 17512: file: 3372-3080-0.004.
1008 A discrepancy between ecoff_data (abfd)->debug_info.symbolic_header.isymMax
1009 and ecoff_data (abfd)->debug_info.symbolic_header.ifdMax can mean that
1010 we have fewer symbols than we were expecting. Allow for this by updating
1011 the symbol count and warning the user. */
1012 if (internal_ptr - internal < (ptrdiff_t) bfd_get_symcount (abfd))
1014 abfd->symcount = internal_ptr - internal;
1015 _bfd_error_handler
1016 /* xgettext:c-format */
1017 (_("%pB: warning: isymMax (%ld) is greater than ifdMax (%ld)"),
1018 abfd, ecoff_data (abfd)->debug_info.symbolic_header.isymMax,
1019 ecoff_data (abfd)->debug_info.symbolic_header.ifdMax);
1022 ecoff_data (abfd)->canonical_symbols = internal;
1024 return true;
1027 /* Return the amount of space needed for the canonical symbols. */
1029 long
1030 _bfd_ecoff_get_symtab_upper_bound (bfd *abfd)
1032 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL,
1033 &ecoff_data (abfd)->debug_info))
1034 return -1;
1036 if (bfd_get_symcount (abfd) == 0)
1037 return 0;
1039 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1042 /* Get the canonical symbols. */
1044 long
1045 _bfd_ecoff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
1047 unsigned int counter = 0;
1048 ecoff_symbol_type *symbase;
1049 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1051 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1052 return -1;
1053 if (bfd_get_symcount (abfd) == 0)
1054 return 0;
1056 symbase = ecoff_data (abfd)->canonical_symbols;
1057 while (counter < bfd_get_symcount (abfd))
1059 *(location++) = symbase++;
1060 counter++;
1062 *location++ = NULL;
1063 return bfd_get_symcount (abfd);
1066 /* Turn ECOFF type information into a printable string.
1067 ecoff_emit_aggregate and ecoff_type_to_string are from
1068 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1070 /* Write aggregate information to a string. */
1072 static void
1073 ecoff_emit_aggregate (bfd *abfd,
1074 FDR *fdr,
1075 char *string,
1076 RNDXR *rndx,
1077 long isym,
1078 const char *which)
1080 const struct ecoff_debug_swap * const debug_swap =
1081 &ecoff_backend (abfd)->debug_swap;
1082 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1083 unsigned int ifd = rndx->rfd;
1084 unsigned int indx = rndx->index;
1085 const char *name;
1087 if (ifd == 0xfff)
1088 ifd = isym;
1090 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1091 struct return type of a procedure compiled without -g. */
1092 if (ifd == 0xffffffff
1093 || (rndx->rfd == 0xfff && indx == 0))
1094 name = "<undefined>";
1095 else if (indx == indexNil)
1096 name = "<no name>";
1097 else
1099 SYMR sym;
1101 if (debug_info->external_rfd == NULL)
1102 fdr = debug_info->fdr + ifd;
1103 else
1105 RFDT rfd;
1107 (*debug_swap->swap_rfd_in) (abfd,
1108 ((char *) debug_info->external_rfd
1109 + ((fdr->rfdBase + ifd)
1110 * debug_swap->external_rfd_size)),
1111 &rfd);
1112 fdr = debug_info->fdr + rfd;
1115 indx += fdr->isymBase;
1117 (*debug_swap->swap_sym_in) (abfd,
1118 ((char *) debug_info->external_sym
1119 + indx * debug_swap->external_sym_size),
1120 &sym);
1122 name = debug_info->ss + fdr->issBase + sym.iss;
1125 sprintf (string,
1126 "%s %s { ifd = %u, index = %lu }",
1127 which, name, ifd,
1128 ((unsigned long) indx
1129 + debug_info->symbolic_header.iextMax));
1132 /* Convert the type information to string format. */
1134 static char *
1135 ecoff_type_to_string (bfd *abfd, FDR *fdr, unsigned int indx, char *buff)
1137 union aux_ext *aux_ptr;
1138 int bigendian;
1139 AUXU u;
1140 struct qual
1142 unsigned int type;
1143 int low_bound;
1144 int high_bound;
1145 int stride;
1146 } qualifiers[7];
1147 unsigned int basic_type;
1148 int i;
1149 char buffer1[1024];
1150 char *p1 = buffer1;
1151 char *p2 = buff;
1152 RNDXR rndx;
1154 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1155 bigendian = fdr->fBigendian;
1157 for (i = 0; i < 7; i++)
1159 qualifiers[i].low_bound = 0;
1160 qualifiers[i].high_bound = 0;
1161 qualifiers[i].stride = 0;
1164 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1165 return "-1 (no type)";
1166 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1168 basic_type = u.ti.bt;
1169 qualifiers[0].type = u.ti.tq0;
1170 qualifiers[1].type = u.ti.tq1;
1171 qualifiers[2].type = u.ti.tq2;
1172 qualifiers[3].type = u.ti.tq3;
1173 qualifiers[4].type = u.ti.tq4;
1174 qualifiers[5].type = u.ti.tq5;
1175 qualifiers[6].type = tqNil;
1177 /* Go get the basic type. */
1178 switch (basic_type)
1180 case btNil: /* Undefined. */
1181 strcpy (p1, "nil");
1182 break;
1184 case btAdr: /* Address - integer same size as pointer. */
1185 strcpy (p1, "address");
1186 break;
1188 case btChar: /* Character. */
1189 strcpy (p1, "char");
1190 break;
1192 case btUChar: /* Unsigned character. */
1193 strcpy (p1, "unsigned char");
1194 break;
1196 case btShort: /* Short. */
1197 strcpy (p1, "short");
1198 break;
1200 case btUShort: /* Unsigned short. */
1201 strcpy (p1, "unsigned short");
1202 break;
1204 case btInt: /* Int. */
1205 strcpy (p1, "int");
1206 break;
1208 case btUInt: /* Unsigned int. */
1209 strcpy (p1, "unsigned int");
1210 break;
1212 case btLong: /* Long. */
1213 strcpy (p1, "long");
1214 break;
1216 case btULong: /* Unsigned long. */
1217 strcpy (p1, "unsigned long");
1218 break;
1220 case btFloat: /* Float (real). */
1221 strcpy (p1, "float");
1222 break;
1224 case btDouble: /* Double (real). */
1225 strcpy (p1, "double");
1226 break;
1228 /* Structures add 1-2 aux words:
1229 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1230 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1232 case btStruct: /* Structure (Record). */
1233 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1234 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1235 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1236 "struct");
1237 indx++; /* Skip aux words. */
1238 break;
1240 /* Unions add 1-2 aux words:
1241 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1242 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1244 case btUnion: /* Union. */
1245 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1246 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1247 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1248 "union");
1249 indx++; /* Skip aux words. */
1250 break;
1252 /* Enumerations add 1-2 aux words:
1253 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1254 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1256 case btEnum: /* Enumeration. */
1257 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1258 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1259 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1260 "enum");
1261 indx++; /* Skip aux words. */
1262 break;
1264 case btTypedef: /* Defined via a typedef, isymRef points. */
1265 strcpy (p1, "typedef");
1266 break;
1268 case btRange: /* Subrange of int. */
1269 strcpy (p1, "subrange");
1270 break;
1272 case btSet: /* Pascal sets. */
1273 strcpy (p1, "set");
1274 break;
1276 case btComplex: /* Fortran complex. */
1277 strcpy (p1, "complex");
1278 break;
1280 case btDComplex: /* Fortran double complex. */
1281 strcpy (p1, "double complex");
1282 break;
1284 case btIndirect: /* Forward or unnamed typedef. */
1285 strcpy (p1, "forward/unamed typedef");
1286 break;
1288 case btFixedDec: /* Fixed Decimal. */
1289 strcpy (p1, "fixed decimal");
1290 break;
1292 case btFloatDec: /* Float Decimal. */
1293 strcpy (p1, "float decimal");
1294 break;
1296 case btString: /* Varying Length Character String. */
1297 strcpy (p1, "string");
1298 break;
1300 case btBit: /* Aligned Bit String. */
1301 strcpy (p1, "bit");
1302 break;
1304 case btPicture: /* Picture. */
1305 strcpy (p1, "picture");
1306 break;
1308 case btVoid: /* Void. */
1309 strcpy (p1, "void");
1310 break;
1312 default:
1313 sprintf (p1, _("unknown basic type %d"), (int) basic_type);
1314 break;
1317 p1 += strlen (p1);
1319 /* If this is a bitfield, get the bitsize. */
1320 if (u.ti.fBitfield)
1322 int bitsize;
1324 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1325 sprintf (p1, " : %d", bitsize);
1328 /* Deal with any qualifiers. */
1329 if (qualifiers[0].type != tqNil)
1331 /* Snarf up any array bounds in the correct order. Arrays
1332 store 5 successive words in the aux. table:
1333 word 0 RNDXR to type of the bounds (ie, int)
1334 word 1 Current file descriptor index
1335 word 2 low bound
1336 word 3 high bound (or -1 if [])
1337 word 4 stride size in bits. */
1338 for (i = 0; i < 7; i++)
1340 if (qualifiers[i].type == tqArray)
1342 qualifiers[i].low_bound =
1343 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1344 qualifiers[i].high_bound =
1345 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1346 qualifiers[i].stride =
1347 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1348 indx += 5;
1352 /* Now print out the qualifiers. */
1353 for (i = 0; i < 6; i++)
1355 switch (qualifiers[i].type)
1357 case tqNil:
1358 case tqMax:
1359 break;
1361 case tqPtr:
1362 strcpy (p2, "ptr to ");
1363 p2 += sizeof ("ptr to ")-1;
1364 break;
1366 case tqVol:
1367 strcpy (p2, "volatile ");
1368 p2 += sizeof ("volatile ")-1;
1369 break;
1371 case tqFar:
1372 strcpy (p2, "far ");
1373 p2 += sizeof ("far ")-1;
1374 break;
1376 case tqProc:
1377 strcpy (p2, "func. ret. ");
1378 p2 += sizeof ("func. ret. ");
1379 break;
1381 case tqArray:
1383 int first_array = i;
1384 int j;
1386 /* Print array bounds reversed (ie, in the order the C
1387 programmer writes them). C is such a fun language.... */
1388 while (i < 5 && qualifiers[i+1].type == tqArray)
1389 i++;
1391 for (j = i; j >= first_array; j--)
1393 strcpy (p2, "array [");
1394 p2 += sizeof ("array [")-1;
1395 if (qualifiers[j].low_bound != 0)
1396 sprintf (p2,
1397 "%ld:%ld {%ld bits}",
1398 (long) qualifiers[j].low_bound,
1399 (long) qualifiers[j].high_bound,
1400 (long) qualifiers[j].stride);
1402 else if (qualifiers[j].high_bound != -1)
1403 sprintf (p2,
1404 "%ld {%ld bits}",
1405 (long) (qualifiers[j].high_bound + 1),
1406 (long) (qualifiers[j].stride));
1408 else
1409 sprintf (p2, " {%ld bits}", (long) qualifiers[j].stride);
1411 p2 += strlen (p2);
1412 strcpy (p2, "] of ");
1413 p2 += sizeof ("] of ")-1;
1416 break;
1421 strcpy (p2, buffer1);
1422 return buff;
1425 /* Return information about ECOFF symbol SYMBOL in RET. */
1427 void
1428 _bfd_ecoff_get_symbol_info (bfd *abfd ATTRIBUTE_UNUSED,
1429 asymbol *symbol,
1430 symbol_info *ret)
1432 bfd_symbol_info (symbol, ret);
1435 /* Return whether this is a local label. */
1437 bool
1438 _bfd_ecoff_bfd_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
1439 const char *name)
1441 return name[0] == '$';
1444 /* Print information about an ECOFF symbol. */
1446 void
1447 _bfd_ecoff_print_symbol (bfd *abfd,
1448 void * filep,
1449 asymbol *symbol,
1450 bfd_print_symbol_type how)
1452 const struct ecoff_debug_swap * const debug_swap
1453 = &ecoff_backend (abfd)->debug_swap;
1454 FILE *file = (FILE *)filep;
1455 const char *symname = (symbol->name != bfd_symbol_error_name
1456 ? symbol->name : _("<corrupt>"));
1458 switch (how)
1460 case bfd_print_symbol_name:
1461 fprintf (file, "%s", symname);
1462 break;
1463 case bfd_print_symbol_more:
1464 if (ecoffsymbol (symbol)->local)
1466 SYMR ecoff_sym;
1468 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1469 &ecoff_sym);
1470 fprintf (file, "ecoff local ");
1471 bfd_fprintf_vma (abfd, file, ecoff_sym.value);
1472 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1473 (unsigned) ecoff_sym.sc);
1475 else
1477 EXTR ecoff_ext;
1479 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1480 &ecoff_ext);
1481 fprintf (file, "ecoff extern ");
1482 bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1483 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1484 (unsigned) ecoff_ext.asym.sc);
1486 break;
1487 case bfd_print_symbol_all:
1488 /* Print out the symbols in a reasonable way. */
1490 char type;
1491 int pos;
1492 EXTR ecoff_ext;
1493 char jmptbl;
1494 char cobol_main;
1495 char weakext;
1497 if (ecoffsymbol (symbol)->local)
1499 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1500 &ecoff_ext.asym);
1501 type = 'l';
1502 pos = ((((char *) ecoffsymbol (symbol)->native
1503 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1504 / debug_swap->external_sym_size)
1505 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1506 jmptbl = ' ';
1507 cobol_main = ' ';
1508 weakext = ' ';
1510 else
1512 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1513 &ecoff_ext);
1514 type = 'e';
1515 pos = (((char *) ecoffsymbol (symbol)->native
1516 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1517 / debug_swap->external_ext_size);
1518 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1519 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1520 weakext = ecoff_ext.weakext ? 'w' : ' ';
1523 fprintf (file, "[%3d] %c ",
1524 pos, type);
1525 bfd_fprintf_vma (abfd, file, ecoff_ext.asym.value);
1526 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1527 (unsigned) ecoff_ext.asym.st,
1528 (unsigned) ecoff_ext.asym.sc,
1529 (unsigned) ecoff_ext.asym.index,
1530 jmptbl, cobol_main, weakext,
1531 symname);
1533 if (ecoffsymbol (symbol)->fdr != NULL
1534 && ecoff_ext.asym.index != indexNil)
1536 FDR *fdr;
1537 unsigned int indx;
1538 int bigendian;
1539 bfd_size_type sym_base;
1540 union aux_ext *aux_base;
1542 fdr = ecoffsymbol (symbol)->fdr;
1543 indx = ecoff_ext.asym.index;
1545 /* sym_base is used to map the fdr relative indices which
1546 appear in the file to the position number which we are
1547 using. */
1548 sym_base = fdr->isymBase;
1549 if (ecoffsymbol (symbol)->local)
1550 sym_base +=
1551 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1553 /* aux_base is the start of the aux entries for this file;
1554 asym.index is an offset from this. */
1555 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1556 + fdr->iauxBase);
1558 /* The aux entries are stored in host byte order; the
1559 order is indicated by a bit in the fdr. */
1560 bigendian = fdr->fBigendian;
1562 /* This switch is basically from gcc/mips-tdump.c. */
1563 switch (ecoff_ext.asym.st)
1565 case stNil:
1566 case stLabel:
1567 break;
1569 case stFile:
1570 case stBlock:
1571 fprintf (file, _("\n End+1 symbol: %ld"),
1572 (long) (indx + sym_base));
1573 break;
1575 case stEnd:
1576 if (ecoff_ext.asym.sc == scText
1577 || ecoff_ext.asym.sc == scInfo)
1578 fprintf (file, _("\n First symbol: %ld"),
1579 (long) (indx + sym_base));
1580 else
1581 fprintf (file, _("\n First symbol: %ld"),
1582 ((long)
1583 (AUX_GET_ISYM (bigendian,
1584 &aux_base[ecoff_ext.asym.index])
1585 + sym_base)));
1586 break;
1588 case stProc:
1589 case stStaticProc:
1590 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1592 else if (ecoffsymbol (symbol)->local)
1594 char buff[1024];
1595 /* xgettext:c-format */
1596 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1597 ((long)
1598 (AUX_GET_ISYM (bigendian,
1599 &aux_base[ecoff_ext.asym.index])
1600 + sym_base)),
1601 ecoff_type_to_string (abfd, fdr, indx + 1, buff));
1603 else
1604 fprintf (file, _("\n Local symbol: %ld"),
1605 ((long) indx
1606 + (long) sym_base
1607 + (ecoff_data (abfd)
1608 ->debug_info.symbolic_header.iextMax)));
1609 break;
1611 case stStruct:
1612 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1613 (long) (indx + sym_base));
1614 break;
1616 case stUnion:
1617 fprintf (file, _("\n union; End+1 symbol: %ld"),
1618 (long) (indx + sym_base));
1619 break;
1621 case stEnum:
1622 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1623 (long) (indx + sym_base));
1624 break;
1626 default:
1627 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1629 char buff[1024];
1630 fprintf (file, _("\n Type: %s"),
1631 ecoff_type_to_string (abfd, fdr, indx, buff));
1633 break;
1637 break;
1641 /* Read in the relocs for a section. */
1643 static bool
1644 ecoff_slurp_reloc_table (bfd *abfd,
1645 asection *section,
1646 asymbol **symbols)
1648 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1649 arelent *internal_relocs;
1650 bfd_size_type external_reloc_size;
1651 bfd_size_type amt;
1652 bfd_byte *external_relocs;
1653 arelent *rptr;
1654 unsigned int i;
1656 if (section->relocation != NULL
1657 || section->reloc_count == 0
1658 || (section->flags & SEC_CONSTRUCTOR) != 0)
1659 return true;
1661 if (! _bfd_ecoff_slurp_symbol_table (abfd))
1662 return false;
1664 external_reloc_size = backend->external_reloc_size;
1665 amt = external_reloc_size * section->reloc_count;
1666 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1667 return false;
1668 external_relocs = _bfd_malloc_and_read (abfd, amt, amt);
1669 if (external_relocs == NULL)
1670 return false;
1672 amt = section->reloc_count;
1673 amt *= sizeof (arelent);
1674 internal_relocs = (arelent *) bfd_alloc (abfd, amt);
1675 if (internal_relocs == NULL)
1677 free (external_relocs);
1678 return false;
1681 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1683 struct internal_reloc intern;
1685 (*backend->swap_reloc_in) (abfd,
1686 external_relocs + i * external_reloc_size,
1687 &intern);
1688 rptr->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
1689 rptr->addend = 0;
1691 if (intern.r_extern)
1693 /* r_symndx is an index into the external symbols. */
1694 if (symbols != NULL
1695 && intern.r_symndx >= 0
1696 && (intern.r_symndx
1697 < (ecoff_data (abfd)->debug_info.symbolic_header.iextMax)))
1698 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1700 else
1702 const char *sec_name;
1703 asection *sec;
1705 /* r_symndx is a section key. */
1706 switch (intern.r_symndx)
1708 case RELOC_SECTION_TEXT: sec_name = _TEXT; break;
1709 case RELOC_SECTION_RDATA: sec_name = _RDATA; break;
1710 case RELOC_SECTION_DATA: sec_name = _DATA; break;
1711 case RELOC_SECTION_SDATA: sec_name = _SDATA; break;
1712 case RELOC_SECTION_SBSS: sec_name = _SBSS; break;
1713 case RELOC_SECTION_BSS: sec_name = _BSS; break;
1714 case RELOC_SECTION_INIT: sec_name = _INIT; break;
1715 case RELOC_SECTION_LIT8: sec_name = _LIT8; break;
1716 case RELOC_SECTION_LIT4: sec_name = _LIT4; break;
1717 case RELOC_SECTION_XDATA: sec_name = _XDATA; break;
1718 case RELOC_SECTION_PDATA: sec_name = _PDATA; break;
1719 case RELOC_SECTION_FINI: sec_name = _FINI; break;
1720 case RELOC_SECTION_LITA: sec_name = _LITA; break;
1721 case RELOC_SECTION_RCONST: sec_name = _RCONST; break;
1722 default:
1723 sec_name = NULL;
1724 break;
1727 if (sec_name != NULL)
1729 sec = bfd_get_section_by_name (abfd, sec_name);
1730 if (sec != NULL)
1732 rptr->sym_ptr_ptr = &sec->symbol;
1733 rptr->addend = - bfd_section_vma (sec);
1738 rptr->address = intern.r_vaddr - bfd_section_vma (section);
1740 /* Let the backend select the howto field and do any other
1741 required processing. */
1742 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1745 free (external_relocs);
1747 section->relocation = internal_relocs;
1749 return true;
1752 /* Get a canonical list of relocs. */
1754 long
1755 _bfd_ecoff_canonicalize_reloc (bfd *abfd,
1756 asection *section,
1757 arelent **relptr,
1758 asymbol **symbols)
1760 unsigned int count;
1762 if (section->flags & SEC_CONSTRUCTOR)
1764 arelent_chain *chain;
1766 /* This section has relocs made up by us, not the file, so take
1767 them out of their chain and place them into the data area
1768 provided. */
1769 for (count = 0, chain = section->constructor_chain;
1770 count < section->reloc_count;
1771 count++, chain = chain->next)
1772 *relptr++ = &chain->relent;
1774 else
1776 arelent *tblptr;
1778 if (! ecoff_slurp_reloc_table (abfd, section, symbols))
1779 return -1;
1781 tblptr = section->relocation;
1783 for (count = 0; count < section->reloc_count; count++)
1784 *relptr++ = tblptr++;
1787 *relptr = NULL;
1789 return section->reloc_count;
1792 /* Provided a BFD, a section and an offset into the section, calculate
1793 and return the name of the source file and the line nearest to the
1794 wanted location. */
1796 bool
1797 _bfd_ecoff_find_nearest_line (bfd *abfd,
1798 asymbol **symbols ATTRIBUTE_UNUSED,
1799 asection *section,
1800 bfd_vma offset,
1801 const char **filename_ptr,
1802 const char **functionname_ptr,
1803 unsigned int *retline_ptr,
1804 unsigned int *discriminator_ptr)
1806 const struct ecoff_debug_swap * const debug_swap
1807 = &ecoff_backend (abfd)->debug_swap;
1808 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1809 struct ecoff_find_line *line_info;
1811 /* Make sure we have the FDR's. */
1812 if (! _bfd_ecoff_slurp_symbolic_info (abfd, NULL, debug_info)
1813 || bfd_get_symcount (abfd) == 0)
1814 return false;
1816 if (ecoff_data (abfd)->find_line_info == NULL)
1818 size_t amt = sizeof (struct ecoff_find_line);
1820 ecoff_data (abfd)->find_line_info =
1821 (struct ecoff_find_line *) bfd_zalloc (abfd, amt);
1822 if (ecoff_data (abfd)->find_line_info == NULL)
1823 return false;
1826 if (discriminator_ptr)
1827 *discriminator_ptr = 0;
1828 line_info = ecoff_data (abfd)->find_line_info;
1829 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1830 debug_swap, line_info, filename_ptr,
1831 functionname_ptr, retline_ptr);
1834 /* Copy private BFD data. This is called by objcopy and strip. We
1835 use it to copy the ECOFF debugging information from one BFD to the
1836 other. It would be theoretically possible to represent the ECOFF
1837 debugging information in the symbol table. However, it would be a
1838 lot of work, and there would be little gain (gas, gdb, and ld
1839 already access the ECOFF debugging information via the
1840 ecoff_debug_info structure, and that structure would have to be
1841 retained in order to support ECOFF debugging in MIPS ELF).
1843 The debugging information for the ECOFF external symbols comes from
1844 the symbol table, so this function only handles the other debugging
1845 information. */
1847 bool
1848 _bfd_ecoff_bfd_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
1850 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1851 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1852 int i;
1853 asymbol **sym_ptr_ptr;
1854 size_t c;
1855 bool local;
1857 /* We only want to copy information over if both BFD's use ECOFF
1858 format. */
1859 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1860 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1861 return true;
1863 /* Copy the GP value and the register masks. */
1864 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1865 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1866 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1867 for (i = 0; i < 3; i++)
1868 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1870 /* Copy the version stamp. */
1871 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1873 /* If there are no symbols, don't copy any debugging information. */
1874 c = bfd_get_symcount (obfd);
1875 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1876 if (c == 0 || sym_ptr_ptr == NULL)
1877 return true;
1879 /* See if there are any local symbols. */
1880 local = false;
1881 for (; c > 0; c--, sym_ptr_ptr++)
1883 if (ecoffsymbol (*sym_ptr_ptr)->local)
1885 local = true;
1886 break;
1890 if (local)
1892 /* There are some local symbols. We just bring over all the
1893 debugging information. FIXME: This is not quite the right
1894 thing to do. If the user has asked us to discard all
1895 debugging information, then we are probably going to wind up
1896 keeping it because there will probably be some local symbol
1897 which objcopy did not discard. We should actually break
1898 apart the debugging information and only keep that which
1899 applies to the symbols we want to keep. */
1900 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1901 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1902 oinfo->line = iinfo->line;
1904 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1905 oinfo->external_dnr = iinfo->external_dnr;
1907 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1908 oinfo->external_pdr = iinfo->external_pdr;
1910 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1911 oinfo->external_sym = iinfo->external_sym;
1913 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1914 oinfo->external_opt = iinfo->external_opt;
1916 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1917 oinfo->external_aux = iinfo->external_aux;
1919 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1920 oinfo->ss = iinfo->ss;
1922 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1923 oinfo->external_fdr = iinfo->external_fdr;
1925 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1926 oinfo->external_rfd = iinfo->external_rfd;
1928 /* Flag that oinfo entries should not be freed. */
1929 oinfo->alloc_syments = true;
1931 else
1933 /* We are discarding all the local symbol information. Look
1934 through the external symbols and remove all references to FDR
1935 or aux information. */
1936 c = bfd_get_symcount (obfd);
1937 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1938 for (; c > 0; c--, sym_ptr_ptr++)
1940 EXTR esym;
1942 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1943 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1944 esym.ifd = ifdNil;
1945 esym.asym.index = indexNil;
1946 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1947 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1951 return true;
1954 /* Set the architecture. The supported architecture is stored in the
1955 backend pointer. We always set the architecture anyhow, since many
1956 callers ignore the return value. */
1958 bool
1959 _bfd_ecoff_set_arch_mach (bfd *abfd,
1960 enum bfd_architecture arch,
1961 unsigned long machine)
1963 bfd_default_set_arch_mach (abfd, arch, machine);
1964 return arch == ecoff_backend (abfd)->arch;
1967 /* Get the size of the section headers. */
1970 _bfd_ecoff_sizeof_headers (bfd *abfd,
1971 struct bfd_link_info *info ATTRIBUTE_UNUSED)
1973 asection *current;
1974 int c;
1975 int ret;
1977 c = 0;
1978 for (current = abfd->sections;
1979 current != NULL;
1980 current = current->next)
1981 ++c;
1983 ret = (bfd_coff_filhsz (abfd)
1984 + bfd_coff_aoutsz (abfd)
1985 + c * bfd_coff_scnhsz (abfd));
1986 return (int) BFD_ALIGN (ret, 16);
1989 /* Get the contents of a section. */
1991 bool
1992 _bfd_ecoff_get_section_contents (bfd *abfd,
1993 asection *section,
1994 void * location,
1995 file_ptr offset,
1996 bfd_size_type count)
1998 return _bfd_generic_get_section_contents (abfd, section, location,
1999 offset, count);
2002 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2003 called via qsort. */
2005 static int
2006 ecoff_sort_hdrs (const void * arg1, const void * arg2)
2008 const asection *hdr1 = *(const asection **) arg1;
2009 const asection *hdr2 = *(const asection **) arg2;
2011 if ((hdr1->flags & SEC_ALLOC) != 0)
2013 if ((hdr2->flags & SEC_ALLOC) == 0)
2014 return -1;
2016 else
2018 if ((hdr2->flags & SEC_ALLOC) != 0)
2019 return 1;
2021 if (hdr1->vma < hdr2->vma)
2022 return -1;
2023 else if (hdr1->vma > hdr2->vma)
2024 return 1;
2025 else
2026 return 0;
2029 /* Calculate the file position for each section, and set
2030 reloc_filepos. */
2032 static bool
2033 ecoff_compute_section_file_positions (bfd *abfd)
2035 file_ptr sofar, file_sofar;
2036 asection **sorted_hdrs;
2037 asection *current;
2038 unsigned int i;
2039 file_ptr old_sofar;
2040 bool rdata_in_text;
2041 bool first_data, first_nonalloc;
2042 const bfd_vma round = ecoff_backend (abfd)->round;
2043 bfd_size_type amt;
2045 sofar = _bfd_ecoff_sizeof_headers (abfd, NULL);
2046 file_sofar = sofar;
2048 /* Sort the sections by VMA. */
2049 amt = abfd->section_count;
2050 amt *= sizeof (asection *);
2051 sorted_hdrs = (asection **) bfd_malloc (amt);
2052 if (sorted_hdrs == NULL)
2053 return false;
2054 for (current = abfd->sections, i = 0;
2055 current != NULL;
2056 current = current->next, i++)
2057 sorted_hdrs[i] = current;
2058 BFD_ASSERT (i == abfd->section_count);
2060 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2061 ecoff_sort_hdrs);
2063 /* Some versions of the OSF linker put the .rdata section in the
2064 text segment, and some do not. */
2065 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2066 if (rdata_in_text)
2068 for (i = 0; i < abfd->section_count; i++)
2070 current = sorted_hdrs[i];
2071 if (streq (current->name, _RDATA))
2072 break;
2073 if ((current->flags & SEC_CODE) == 0
2074 && ! streq (current->name, _PDATA)
2075 && ! streq (current->name, _RCONST))
2077 rdata_in_text = false;
2078 break;
2082 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2084 first_data = true;
2085 first_nonalloc = true;
2086 for (i = 0; i < abfd->section_count; i++)
2088 unsigned int alignment_power;
2090 current = sorted_hdrs[i];
2092 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2093 supposed to indicate the number of .pdata entries that are
2094 really in the section. Each entry is 8 bytes. We store this
2095 away in line_filepos before increasing the section size. */
2096 if (streq (current->name, _PDATA))
2097 current->line_filepos = current->size / 8;
2099 alignment_power = current->alignment_power;
2101 /* On Ultrix, the data sections in an executable file must be
2102 aligned to a page boundary within the file. This does not
2103 affect the section size, though. FIXME: Does this work for
2104 other platforms? It requires some modification for the
2105 Alpha, because .rdata on the Alpha goes with the text, not
2106 the data. */
2107 if ((abfd->flags & EXEC_P) != 0
2108 && (abfd->flags & D_PAGED) != 0
2109 && ! first_data
2110 && (current->flags & SEC_CODE) == 0
2111 && (! rdata_in_text
2112 || ! streq (current->name, _RDATA))
2113 && ! streq (current->name, _PDATA)
2114 && ! streq (current->name, _RCONST))
2116 sofar = (sofar + round - 1) &~ (round - 1);
2117 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2118 first_data = false;
2120 else if (streq (current->name, _LIB))
2122 /* On Irix 4, the location of contents of the .lib section
2123 from a shared library section is also rounded up to a
2124 page boundary. */
2126 sofar = (sofar + round - 1) &~ (round - 1);
2127 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2129 else if (first_nonalloc
2130 && (current->flags & SEC_ALLOC) == 0
2131 && (abfd->flags & D_PAGED) != 0)
2133 /* Skip up to the next page for an unallocated section, such
2134 as the .comment section on the Alpha. This leaves room
2135 for the .bss section. */
2136 first_nonalloc = false;
2137 sofar = (sofar + round - 1) &~ (round - 1);
2138 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2141 /* Align the sections in the file to the same boundary on
2142 which they are aligned in virtual memory. */
2143 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2144 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2145 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2147 if ((abfd->flags & D_PAGED) != 0
2148 && (current->flags & SEC_ALLOC) != 0)
2150 sofar += (current->vma - sofar) % round;
2151 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2152 file_sofar += (current->vma - file_sofar) % round;
2155 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2156 current->filepos = file_sofar;
2158 sofar += current->size;
2159 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2160 file_sofar += current->size;
2162 /* Make sure that this section is of the right size too. */
2163 old_sofar = sofar;
2164 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2165 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2166 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2167 current->size += sofar - old_sofar;
2170 free (sorted_hdrs);
2171 sorted_hdrs = NULL;
2173 ecoff_data (abfd)->reloc_filepos = file_sofar;
2175 return true;
2178 /* Determine the location of the relocs for all the sections in the
2179 output file, as well as the location of the symbolic debugging
2180 information. */
2182 static bfd_size_type
2183 ecoff_compute_reloc_file_positions (bfd *abfd)
2185 const bfd_size_type external_reloc_size =
2186 ecoff_backend (abfd)->external_reloc_size;
2187 file_ptr reloc_base;
2188 bfd_size_type reloc_size;
2189 asection *current;
2190 file_ptr sym_base;
2192 if (! abfd->output_has_begun)
2194 if (! ecoff_compute_section_file_positions (abfd))
2195 abort ();
2196 abfd->output_has_begun = true;
2199 reloc_base = ecoff_data (abfd)->reloc_filepos;
2201 reloc_size = 0;
2202 for (current = abfd->sections;
2203 current != NULL;
2204 current = current->next)
2206 if (current->reloc_count == 0)
2207 current->rel_filepos = 0;
2208 else
2210 bfd_size_type relsize;
2212 current->rel_filepos = reloc_base;
2213 relsize = current->reloc_count * external_reloc_size;
2214 reloc_size += relsize;
2215 reloc_base += relsize;
2219 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2221 /* At least on Ultrix, the symbol table of an executable file must
2222 be aligned to a page boundary. FIXME: Is this true on other
2223 platforms? */
2224 if ((abfd->flags & EXEC_P) != 0
2225 && (abfd->flags & D_PAGED) != 0)
2226 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2227 &~ (ecoff_backend (abfd)->round - 1));
2229 ecoff_data (abfd)->sym_filepos = sym_base;
2231 return reloc_size;
2234 /* Set the contents of a section. */
2236 bool
2237 _bfd_ecoff_set_section_contents (bfd *abfd,
2238 asection *section,
2239 const void * location,
2240 file_ptr offset,
2241 bfd_size_type count)
2243 file_ptr pos;
2245 /* This must be done first, because bfd_set_section_contents is
2246 going to set output_has_begun to TRUE. */
2247 if (! abfd->output_has_begun
2248 && ! ecoff_compute_section_file_positions (abfd))
2249 return false;
2251 /* Handle the .lib section specially so that Irix 4 shared libraries
2252 work out. See coff_set_section_contents in coffcode.h. */
2253 if (streq (section->name, _LIB))
2255 bfd_byte *rec, *recend;
2257 rec = (bfd_byte *) location;
2258 recend = rec + count;
2259 while (rec < recend)
2261 ++section->lma;
2262 rec += bfd_get_32 (abfd, rec) * 4;
2265 BFD_ASSERT (rec == recend);
2268 if (count == 0)
2269 return true;
2271 pos = section->filepos + offset;
2272 if (bfd_seek (abfd, pos, SEEK_SET) != 0
2273 || bfd_write (location, count, abfd) != count)
2274 return false;
2276 return true;
2279 /* Set the GP value for an ECOFF file. This is a hook used by the
2280 assembler. */
2282 bool
2283 bfd_ecoff_set_gp_value (bfd *abfd, bfd_vma gp_value)
2285 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2286 || bfd_get_format (abfd) != bfd_object)
2288 bfd_set_error (bfd_error_invalid_operation);
2289 return false;
2292 ecoff_data (abfd)->gp = gp_value;
2294 return true;
2297 /* Set the register masks for an ECOFF file. This is a hook used by
2298 the assembler. */
2300 bool
2301 bfd_ecoff_set_regmasks (bfd *abfd,
2302 unsigned long gprmask,
2303 unsigned long fprmask,
2304 unsigned long *cprmask)
2306 ecoff_data_type *tdata;
2308 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2309 || bfd_get_format (abfd) != bfd_object)
2311 bfd_set_error (bfd_error_invalid_operation);
2312 return false;
2315 tdata = ecoff_data (abfd);
2316 tdata->gprmask = gprmask;
2317 tdata->fprmask = fprmask;
2318 if (cprmask != NULL)
2320 int i;
2322 for (i = 0; i < 3; i++)
2323 tdata->cprmask[i] = cprmask[i];
2326 return true;
2329 /* Get ECOFF EXTR information for an external symbol. This function
2330 is passed to bfd_ecoff_debug_externals. */
2332 static bool
2333 ecoff_get_extr (asymbol *sym, EXTR *esym)
2335 ecoff_symbol_type *ecoff_sym_ptr;
2336 bfd *input_bfd;
2338 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2339 || ecoffsymbol (sym)->native == NULL)
2341 /* Don't include debugging, local, or section symbols. */
2342 if ((sym->flags & BSF_DEBUGGING) != 0
2343 || (sym->flags & BSF_LOCAL) != 0
2344 || (sym->flags & BSF_SECTION_SYM) != 0)
2345 return false;
2347 esym->jmptbl = 0;
2348 esym->cobol_main = 0;
2349 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2350 esym->reserved = 0;
2351 esym->ifd = ifdNil;
2352 /* FIXME: we can do better than this for st and sc. */
2353 esym->asym.st = stGlobal;
2354 esym->asym.sc = scAbs;
2355 esym->asym.reserved = 0;
2356 esym->asym.index = indexNil;
2357 return true;
2360 ecoff_sym_ptr = ecoffsymbol (sym);
2362 if (ecoff_sym_ptr->local)
2363 return false;
2365 input_bfd = bfd_asymbol_bfd (sym);
2366 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2367 (input_bfd, ecoff_sym_ptr->native, esym);
2369 /* If the symbol was defined by the linker, then esym will be
2370 undefined but sym will not be. Get a better class for such a
2371 symbol. */
2372 if ((esym->asym.sc == scUndefined
2373 || esym->asym.sc == scSUndefined)
2374 && ! bfd_is_und_section (bfd_asymbol_section (sym)))
2375 esym->asym.sc = scAbs;
2377 /* Adjust the FDR index for the symbol by that used for the input
2378 BFD. */
2379 if (esym->ifd != -1)
2381 struct ecoff_debug_info *input_debug;
2383 input_debug = &ecoff_data (input_bfd)->debug_info;
2384 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2385 if (input_debug->ifdmap != NULL)
2386 esym->ifd = input_debug->ifdmap[esym->ifd];
2389 return true;
2392 /* Set the external symbol index. This routine is passed to
2393 bfd_ecoff_debug_externals. */
2395 static void
2396 ecoff_set_index (asymbol *sym, bfd_size_type indx)
2398 ecoff_set_sym_index (sym, indx);
2401 /* Write out an ECOFF file. */
2403 bool
2404 _bfd_ecoff_write_object_contents (bfd *abfd)
2406 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2407 const bfd_vma round = backend->round;
2408 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2409 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2410 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2411 const bfd_size_type external_hdr_size
2412 = backend->debug_swap.external_hdr_size;
2413 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2414 void (* const adjust_reloc_out) (bfd *, const arelent *, struct internal_reloc *)
2415 = backend->adjust_reloc_out;
2416 void (* const swap_reloc_out) (bfd *, const struct internal_reloc *, void *)
2417 = backend->swap_reloc_out;
2418 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2419 HDRR * const symhdr = &debug->symbolic_header;
2420 asection *current;
2421 unsigned int count;
2422 bfd_size_type reloc_size;
2423 bfd_size_type text_size;
2424 bfd_vma text_start;
2425 bool set_text_start;
2426 bfd_size_type data_size;
2427 bfd_vma data_start;
2428 bool set_data_start;
2429 bfd_size_type bss_size;
2430 void * buff = NULL;
2431 void * reloc_buff = NULL;
2432 struct internal_filehdr internal_f;
2433 struct internal_aouthdr internal_a;
2434 int i;
2436 /* Determine where the sections and relocs will go in the output
2437 file. */
2438 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2440 count = 1;
2441 for (current = abfd->sections;
2442 current != NULL;
2443 current = current->next)
2445 current->target_index = count;
2446 ++count;
2449 if ((abfd->flags & D_PAGED) != 0)
2450 text_size = _bfd_ecoff_sizeof_headers (abfd, NULL);
2451 else
2452 text_size = 0;
2453 text_start = 0;
2454 set_text_start = false;
2455 data_size = 0;
2456 data_start = 0;
2457 set_data_start = false;
2458 bss_size = 0;
2460 /* Write section headers to the file. */
2462 /* Allocate buff big enough to hold a section header,
2463 file header, or a.out header. */
2465 bfd_size_type siz;
2467 siz = scnhsz;
2468 if (siz < filhsz)
2469 siz = filhsz;
2470 if (siz < aoutsz)
2471 siz = aoutsz;
2472 buff = bfd_malloc (siz);
2473 if (buff == NULL)
2474 goto error_return;
2477 internal_f.f_nscns = 0;
2478 if (bfd_seek (abfd, filhsz + aoutsz, SEEK_SET) != 0)
2479 goto error_return;
2481 for (current = abfd->sections;
2482 current != NULL;
2483 current = current->next)
2485 struct internal_scnhdr section;
2486 bfd_vma vma;
2488 ++internal_f.f_nscns;
2490 strncpy (section.s_name, current->name, sizeof section.s_name);
2492 /* This seems to be correct for Irix 4 shared libraries. */
2493 vma = bfd_section_vma (current);
2494 if (streq (current->name, _LIB))
2495 section.s_vaddr = 0;
2496 else
2497 section.s_vaddr = vma;
2499 section.s_paddr = current->lma;
2500 section.s_size = current->size;
2502 /* If this section is unloadable then the scnptr will be 0. */
2503 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2504 section.s_scnptr = 0;
2505 else
2506 section.s_scnptr = current->filepos;
2507 section.s_relptr = current->rel_filepos;
2509 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2510 object file produced by the assembler is supposed to point to
2511 information about how much room is required by objects of
2512 various different sizes. I think this only matters if we
2513 want the linker to compute the best size to use, or
2514 something. I don't know what happens if the information is
2515 not present. */
2516 if (! streq (current->name, _PDATA))
2517 section.s_lnnoptr = 0;
2518 else
2520 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2521 hold the number of entries in the section (each entry is
2522 8 bytes). We stored this in the line_filepos field in
2523 ecoff_compute_section_file_positions. */
2524 section.s_lnnoptr = current->line_filepos;
2527 section.s_nreloc = current->reloc_count;
2528 section.s_nlnno = 0;
2529 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2530 current->flags);
2532 if (bfd_coff_swap_scnhdr_out (abfd, (void *) &section, buff) == 0
2533 || bfd_write (buff, scnhsz, abfd) != scnhsz)
2534 goto error_return;
2536 if ((section.s_flags & STYP_TEXT) != 0
2537 || ((section.s_flags & STYP_RDATA) != 0
2538 && ecoff_data (abfd)->rdata_in_text)
2539 || section.s_flags == STYP_PDATA
2540 || (section.s_flags & STYP_DYNAMIC) != 0
2541 || (section.s_flags & STYP_LIBLIST) != 0
2542 || (section.s_flags & STYP_RELDYN) != 0
2543 || section.s_flags == STYP_CONFLIC
2544 || (section.s_flags & STYP_DYNSTR) != 0
2545 || (section.s_flags & STYP_DYNSYM) != 0
2546 || (section.s_flags & STYP_HASH) != 0
2547 || (section.s_flags & STYP_ECOFF_INIT) != 0
2548 || (section.s_flags & STYP_ECOFF_FINI) != 0
2549 || section.s_flags == STYP_RCONST)
2551 text_size += current->size;
2552 if (! set_text_start || text_start > vma)
2554 text_start = vma;
2555 set_text_start = true;
2558 else if ((section.s_flags & STYP_RDATA) != 0
2559 || (section.s_flags & STYP_DATA) != 0
2560 || (section.s_flags & STYP_LITA) != 0
2561 || (section.s_flags & STYP_LIT8) != 0
2562 || (section.s_flags & STYP_LIT4) != 0
2563 || (section.s_flags & STYP_SDATA) != 0
2564 || section.s_flags == STYP_XDATA
2565 || (section.s_flags & STYP_GOT) != 0)
2567 data_size += current->size;
2568 if (! set_data_start || data_start > vma)
2570 data_start = vma;
2571 set_data_start = true;
2574 else if ((section.s_flags & STYP_BSS) != 0
2575 || (section.s_flags & STYP_SBSS) != 0)
2576 bss_size += current->size;
2577 else if (section.s_flags == 0
2578 || (section.s_flags & STYP_ECOFF_LIB) != 0
2579 || section.s_flags == STYP_COMMENT)
2580 /* Do nothing. */ ;
2581 else
2582 abort ();
2585 /* Set up the file header. */
2586 internal_f.f_magic = ecoff_get_magic (abfd);
2588 /* We will NOT put a fucking timestamp in the header here. Every
2589 time you put it back, I will come in and take it out again. I'm
2590 sorry. This field does not belong here. We fill it with a 0 so
2591 it compares the same but is not a reasonable time. --
2592 gnu@cygnus.com. */
2593 internal_f.f_timdat = 0;
2595 if (bfd_get_symcount (abfd) != 0)
2597 /* The ECOFF f_nsyms field is not actually the number of
2598 symbols, it's the size of symbolic information header. */
2599 internal_f.f_nsyms = external_hdr_size;
2600 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2602 else
2604 internal_f.f_nsyms = 0;
2605 internal_f.f_symptr = 0;
2608 internal_f.f_opthdr = aoutsz;
2610 internal_f.f_flags = F_LNNO;
2611 if (reloc_size == 0)
2612 internal_f.f_flags |= F_RELFLG;
2613 if (bfd_get_symcount (abfd) == 0)
2614 internal_f.f_flags |= F_LSYMS;
2615 if (abfd->flags & EXEC_P)
2616 internal_f.f_flags |= F_EXEC;
2618 if (bfd_little_endian (abfd))
2619 internal_f.f_flags |= F_AR32WR;
2620 else
2621 internal_f.f_flags |= F_AR32W;
2623 /* Set up the ``optional'' header. */
2624 if ((abfd->flags & D_PAGED) != 0)
2625 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2626 else
2627 internal_a.magic = ECOFF_AOUT_OMAGIC;
2629 /* FIXME: Is this really correct? */
2630 internal_a.vstamp = symhdr->vstamp;
2632 /* At least on Ultrix, these have to be rounded to page boundaries.
2633 FIXME: Is this true on other platforms? */
2634 if ((abfd->flags & D_PAGED) != 0)
2636 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2637 internal_a.text_start = text_start &~ (round - 1);
2638 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2639 internal_a.data_start = data_start &~ (round - 1);
2641 else
2643 internal_a.tsize = text_size;
2644 internal_a.text_start = text_start;
2645 internal_a.dsize = data_size;
2646 internal_a.data_start = data_start;
2649 /* On Ultrix, the initial portions of the .sbss and .bss segments
2650 are at the end of the data section. The bsize field in the
2651 optional header records how many bss bytes are required beyond
2652 those in the data section. The value is not rounded to a page
2653 boundary. */
2654 if (bss_size < internal_a.dsize - data_size)
2655 bss_size = 0;
2656 else
2657 bss_size -= internal_a.dsize - data_size;
2658 internal_a.bsize = bss_size;
2659 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2661 internal_a.entry = bfd_get_start_address (abfd);
2663 internal_a.gp_value = ecoff_data (abfd)->gp;
2665 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2666 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2667 for (i = 0; i < 4; i++)
2668 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2670 /* Let the backend adjust the headers if necessary. */
2671 if (backend->adjust_headers)
2673 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2674 goto error_return;
2677 /* Write out the file header and the optional header. */
2678 if (bfd_seek (abfd, 0, SEEK_SET) != 0)
2679 goto error_return;
2681 bfd_coff_swap_filehdr_out (abfd, (void *) &internal_f, buff);
2682 if (bfd_write (buff, filhsz, abfd) != filhsz)
2683 goto error_return;
2685 bfd_coff_swap_aouthdr_out (abfd, (void *) &internal_a, buff);
2686 if (bfd_write (buff, aoutsz, abfd) != aoutsz)
2687 goto error_return;
2689 /* Build the external symbol information. This must be done before
2690 writing out the relocs so that we know the symbol indices. We
2691 don't do this if this BFD was created by the backend linker,
2692 since it will have already handled the symbols and relocs. */
2693 if (! ecoff_data (abfd)->linker)
2695 symhdr->iextMax = 0;
2696 symhdr->issExtMax = 0;
2697 debug->external_ext = debug->external_ext_end = NULL;
2698 debug->ssext = debug->ssext_end = NULL;
2699 if (! bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2700 (abfd->flags & EXEC_P) == 0,
2701 ecoff_get_extr, ecoff_set_index))
2702 goto error_return;
2704 /* Write out the relocs. */
2705 for (current = abfd->sections;
2706 current != NULL;
2707 current = current->next)
2709 arelent **reloc_ptr_ptr;
2710 arelent **reloc_end;
2711 char *out_ptr;
2712 bfd_size_type amt;
2714 if (current->reloc_count == 0)
2715 continue;
2717 amt = current->reloc_count * external_reloc_size;
2718 reloc_buff = bfd_zalloc (abfd, amt);
2719 if (reloc_buff == NULL)
2720 goto error_return;
2722 reloc_ptr_ptr = current->orelocation;
2723 reloc_end = reloc_ptr_ptr + current->reloc_count;
2724 out_ptr = (char *) reloc_buff;
2726 for (;
2727 reloc_ptr_ptr < reloc_end;
2728 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2730 arelent *reloc;
2731 asymbol *sym;
2732 struct internal_reloc in;
2734 memset ((void *) &in, 0, sizeof in);
2736 reloc = *reloc_ptr_ptr;
2737 sym = *reloc->sym_ptr_ptr;
2739 /* If the howto field has not been initialised then skip this reloc.
2740 This assumes that an error message has been issued elsewhere. */
2741 if (reloc->howto == NULL)
2742 continue;
2744 in.r_vaddr = reloc->address + bfd_section_vma (current);
2745 in.r_type = reloc->howto->type;
2747 if ((sym->flags & BSF_SECTION_SYM) == 0)
2749 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2750 in.r_extern = 1;
2752 else
2754 const char *name;
2755 unsigned int j;
2756 static struct
2758 const char * name;
2759 long r_symndx;
2761 section_symndx [] =
2763 { _TEXT, RELOC_SECTION_TEXT },
2764 { _RDATA, RELOC_SECTION_RDATA },
2765 { _DATA, RELOC_SECTION_DATA },
2766 { _SDATA, RELOC_SECTION_SDATA },
2767 { _SBSS, RELOC_SECTION_SBSS },
2768 { _BSS, RELOC_SECTION_BSS },
2769 { _INIT, RELOC_SECTION_INIT },
2770 { _LIT8, RELOC_SECTION_LIT8 },
2771 { _LIT4, RELOC_SECTION_LIT4 },
2772 { _XDATA, RELOC_SECTION_XDATA },
2773 { _PDATA, RELOC_SECTION_PDATA },
2774 { _FINI, RELOC_SECTION_FINI },
2775 { _LITA, RELOC_SECTION_LITA },
2776 { "*ABS*", RELOC_SECTION_ABS },
2777 { _RCONST, RELOC_SECTION_RCONST }
2780 name = bfd_section_name (bfd_asymbol_section (sym));
2782 for (j = 0; j < ARRAY_SIZE (section_symndx); j++)
2783 if (streq (name, section_symndx[j].name))
2785 in.r_symndx = section_symndx[j].r_symndx;
2786 break;
2789 if (j == ARRAY_SIZE (section_symndx))
2790 abort ();
2791 in.r_extern = 0;
2794 (*adjust_reloc_out) (abfd, reloc, &in);
2796 (*swap_reloc_out) (abfd, &in, (void *) out_ptr);
2799 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2800 goto error_return;
2801 amt = current->reloc_count * external_reloc_size;
2802 if (bfd_write (reloc_buff, amt, abfd) != amt)
2803 goto error_return;
2804 bfd_release (abfd, reloc_buff);
2805 reloc_buff = NULL;
2808 /* Write out the symbolic debugging information. */
2809 if (bfd_get_symcount (abfd) > 0)
2811 /* Write out the debugging information. */
2812 if (! bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2813 ecoff_data (abfd)->sym_filepos))
2814 goto error_return;
2818 /* The .bss section of a demand paged executable must receive an
2819 entire page. If there are symbols, the symbols will start on the
2820 next page. If there are no symbols, we must fill out the page by
2821 hand. */
2822 if (bfd_get_symcount (abfd) == 0
2823 && (abfd->flags & EXEC_P) != 0
2824 && (abfd->flags & D_PAGED) != 0)
2826 char c;
2828 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2829 goto error_return;
2830 if (bfd_read (&c, 1, abfd) == 0)
2831 c = 0;
2832 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos - 1, SEEK_SET) != 0)
2833 goto error_return;
2834 if (bfd_write (&c, 1, abfd) != 1)
2835 goto error_return;
2838 if (reloc_buff != NULL)
2839 bfd_release (abfd, reloc_buff);
2840 free (buff);
2841 return true;
2842 error_return:
2843 if (reloc_buff != NULL)
2844 bfd_release (abfd, reloc_buff);
2845 free (buff);
2846 return false;
2849 /* Archive handling. ECOFF uses what appears to be a unique type of
2850 archive header (armap). The byte ordering of the armap and the
2851 contents are encoded in the name of the armap itself. At least for
2852 now, we only support archives with the same byte ordering in the
2853 armap and the contents.
2855 The first four bytes in the armap are the number of symbol
2856 definitions. This is always a power of two.
2858 This is followed by the symbol definitions. Each symbol definition
2859 occupies 8 bytes. The first four bytes are the offset from the
2860 start of the armap strings to the null-terminated string naming
2861 this symbol. The second four bytes are the file offset to the
2862 archive member which defines this symbol. If the second four bytes
2863 are 0, then this is not actually a symbol definition, and it should
2864 be ignored.
2866 The symbols are hashed into the armap with a closed hashing scheme.
2867 See the functions below for the details of the algorithm.
2869 After the symbol definitions comes four bytes holding the size of
2870 the string table, followed by the string table itself. */
2872 /* The name of an archive headers looks like this:
2873 __________E[BL]E[BL]_ (with a trailing space).
2874 The trailing space is changed to an X if the archive is changed to
2875 indicate that the armap is out of date.
2877 The Alpha seems to use ________64E[BL]E[BL]_. */
2879 #define ARMAP_BIG_ENDIAN 'B'
2880 #define ARMAP_LITTLE_ENDIAN 'L'
2881 #define ARMAP_MARKER 'E'
2882 #define ARMAP_START_LENGTH 10
2883 #define ARMAP_HEADER_MARKER_INDEX 10
2884 #define ARMAP_HEADER_ENDIAN_INDEX 11
2885 #define ARMAP_OBJECT_MARKER_INDEX 12
2886 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2887 #define ARMAP_END_INDEX 14
2888 #define ARMAP_END "_ "
2890 /* This is a magic number used in the hashing algorithm. */
2891 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2893 /* This returns the hash value to use for a string. It also sets
2894 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2895 is the number of entries in the hash table, and HLOG is the log
2896 base 2 of SIZE. */
2898 static unsigned int
2899 ecoff_armap_hash (const char *s,
2900 unsigned int *rehash,
2901 unsigned int size,
2902 unsigned int hlog)
2904 unsigned int hash;
2906 if (hlog == 0)
2907 return 0;
2908 hash = *s++;
2909 while (*s != '\0')
2910 hash = ((hash >> 27) | (hash << 5)) + *s++;
2911 hash *= ARMAP_HASH_MAGIC;
2912 *rehash = (hash & (size - 1)) | 1;
2913 return hash >> (32 - hlog);
2916 /* Read in the armap. */
2918 bool
2919 _bfd_ecoff_slurp_armap (bfd *abfd)
2921 char nextname[17];
2922 unsigned int i;
2923 struct areltdata *mapdata;
2924 bfd_size_type parsed_size, stringsize;
2925 char *raw_armap;
2926 struct artdata *ardata;
2927 unsigned int count;
2928 char *raw_ptr;
2929 carsym *symdef_ptr;
2930 char *stringbase;
2931 bfd_size_type amt;
2933 /* Get the name of the first element. */
2934 i = bfd_read (nextname, 16, abfd);
2935 if (i == 0)
2936 return true;
2937 if (i != 16)
2938 return false;
2940 if (bfd_seek (abfd, -16, SEEK_CUR) != 0)
2941 return false;
2943 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2944 standard COFF armap. We could move the ECOFF armap stuff into
2945 bfd_slurp_armap, but that seems inappropriate since no other
2946 target uses this format. Instead, we check directly for a COFF
2947 armap. */
2948 if (startswith (nextname, "/ "))
2949 return bfd_slurp_armap (abfd);
2951 /* See if the first element is an armap. */
2952 if (strncmp (nextname, ecoff_backend (abfd)->armap_start, ARMAP_START_LENGTH) != 0
2953 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2954 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2955 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2956 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2957 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2958 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2959 || strncmp (nextname + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1) != 0)
2961 abfd->has_armap = false;
2962 return true;
2965 /* Make sure we have the right byte ordering. */
2966 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2967 ^ (bfd_header_big_endian (abfd)))
2968 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
2969 ^ (bfd_big_endian (abfd))))
2971 bfd_set_error (bfd_error_wrong_format);
2972 return false;
2975 /* Read in the armap. */
2976 ardata = bfd_ardata (abfd);
2977 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
2978 if (mapdata == NULL)
2979 return false;
2980 parsed_size = mapdata->parsed_size;
2981 free (mapdata);
2983 if (parsed_size + 1 < 9)
2985 bfd_set_error (bfd_error_malformed_archive);
2986 return false;
2989 raw_armap = (char *) _bfd_alloc_and_read (abfd, parsed_size + 1, parsed_size);
2990 if (raw_armap == NULL)
2991 return false;
2992 raw_armap[parsed_size] = 0;
2994 ardata->tdata = (void *) raw_armap;
2996 count = H_GET_32 (abfd, raw_armap);
2997 if ((parsed_size - 8) / 8 < count)
2998 goto error_malformed;
3000 ardata->symdef_count = 0;
3001 ardata->cache = NULL;
3003 /* This code used to overlay the symdefs over the raw archive data,
3004 but that doesn't work on a 64 bit host. */
3005 stringbase = raw_armap + count * 8 + 8;
3006 stringsize = parsed_size - (count * 8 + 8);
3008 #ifdef CHECK_ARMAP_HASH
3010 unsigned int hlog;
3012 /* Double check that I have the hashing algorithm right by making
3013 sure that every symbol can be looked up successfully. */
3014 hlog = 0;
3015 for (i = 1; i < count; i <<= 1)
3016 hlog++;
3017 BFD_ASSERT (i == count);
3019 raw_ptr = raw_armap + 4;
3020 for (i = 0; i < count; i++, raw_ptr += 8)
3022 unsigned int name_offset, file_offset;
3023 unsigned int hash, rehash, srch;
3025 name_offset = H_GET_32 (abfd, raw_ptr);
3026 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3027 if (file_offset == 0)
3028 continue;
3029 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3030 hlog);
3031 if (hash == i)
3032 continue;
3034 /* See if we can rehash to this location. */
3035 for (srch = (hash + rehash) & (count - 1);
3036 srch != hash && srch != i;
3037 srch = (srch + rehash) & (count - 1))
3038 BFD_ASSERT (H_GET_32 (abfd, (raw_armap + 8 + srch * 8)) != 0);
3039 BFD_ASSERT (srch == i);
3043 #endif /* CHECK_ARMAP_HASH */
3045 raw_ptr = raw_armap + 4;
3046 for (i = 0; i < count; i++, raw_ptr += 8)
3047 if (H_GET_32 (abfd, (raw_ptr + 4)) != 0)
3048 ++ardata->symdef_count;
3050 amt = ardata->symdef_count;
3051 amt *= sizeof (carsym);
3052 symdef_ptr = (carsym *) bfd_alloc (abfd, amt);
3053 if (!symdef_ptr)
3054 goto error_exit;
3056 ardata->symdefs = symdef_ptr;
3058 raw_ptr = raw_armap + 4;
3059 for (i = 0; i < count; i++, raw_ptr += 8)
3061 unsigned int name_offset, file_offset;
3063 file_offset = H_GET_32 (abfd, (raw_ptr + 4));
3064 if (file_offset == 0)
3065 continue;
3066 name_offset = H_GET_32 (abfd, raw_ptr);
3067 if (name_offset > stringsize)
3068 goto error_malformed;
3069 symdef_ptr->name = stringbase + name_offset;
3070 symdef_ptr->file_offset = file_offset;
3071 ++symdef_ptr;
3074 ardata->first_file_filepos = bfd_tell (abfd);
3075 /* Pad to an even boundary. */
3076 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3077 abfd->has_armap = true;
3078 return true;
3080 error_malformed:
3081 bfd_set_error (bfd_error_malformed_archive);
3082 error_exit:
3083 ardata->symdef_count = 0;
3084 ardata->symdefs = NULL;
3085 ardata->tdata = NULL;
3086 bfd_release (abfd, raw_armap);
3087 return false;
3090 /* Write out an armap. */
3092 bool
3093 _bfd_ecoff_write_armap (bfd *abfd,
3094 unsigned int elength,
3095 struct orl *map,
3096 unsigned int orl_count,
3097 int stridx)
3099 unsigned int hashsize, hashlog;
3100 bfd_size_type symdefsize;
3101 int padit;
3102 unsigned int stringsize;
3103 unsigned int mapsize;
3104 file_ptr firstreal;
3105 struct ar_hdr hdr;
3106 struct stat statbuf;
3107 unsigned int i;
3108 bfd_byte temp[4];
3109 bfd_byte *hashtable;
3110 bfd *current;
3111 bfd *last_elt;
3113 /* Ultrix appears to use as a hash table size the least power of two
3114 greater than twice the number of entries. */
3115 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3117 hashsize = 1 << hashlog;
3119 symdefsize = hashsize * 8;
3120 padit = stridx % 2;
3121 stringsize = stridx + padit;
3123 /* Include 8 bytes to store symdefsize and stringsize in output. */
3124 mapsize = symdefsize + stringsize + 8;
3126 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3128 memset ((void *) &hdr, 0, sizeof hdr);
3130 /* Work out the ECOFF armap name. */
3131 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3132 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3133 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3134 (bfd_header_big_endian (abfd)
3135 ? ARMAP_BIG_ENDIAN
3136 : ARMAP_LITTLE_ENDIAN);
3137 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3138 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3139 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3140 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3142 /* Write the timestamp of the archive header to be just a little bit
3143 later than the timestamp of the file, otherwise the linker will
3144 complain that the index is out of date. Actually, the Ultrix
3145 linker just checks the archive name; the GNU linker may check the
3146 date. */
3147 stat (bfd_get_filename (abfd), &statbuf);
3148 _bfd_ar_spacepad (hdr.ar_date, sizeof (hdr.ar_date), "%ld",
3149 (long) (statbuf.st_mtime + 60));
3151 /* The DECstation uses zeroes for the uid, gid and mode of the
3152 armap. */
3153 hdr.ar_uid[0] = '0';
3154 hdr.ar_gid[0] = '0';
3155 /* Building gcc ends up extracting the armap as a file - twice. */
3156 hdr.ar_mode[0] = '6';
3157 hdr.ar_mode[1] = '4';
3158 hdr.ar_mode[2] = '4';
3160 _bfd_ar_spacepad (hdr.ar_size, sizeof (hdr.ar_size), "%-10ld", mapsize);
3162 hdr.ar_fmag[0] = '`';
3163 hdr.ar_fmag[1] = '\012';
3165 /* Turn all null bytes in the header into spaces. */
3166 for (i = 0; i < sizeof (struct ar_hdr); i++)
3167 if (((char *) (&hdr))[i] == '\0')
3168 (((char *) (&hdr))[i]) = ' ';
3170 if (bfd_write (&hdr, sizeof (struct ar_hdr), abfd) != sizeof (struct ar_hdr))
3171 return false;
3173 H_PUT_32 (abfd, hashsize, temp);
3174 if (bfd_write (temp, 4, abfd) != 4)
3175 return false;
3177 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3178 if (!hashtable)
3179 return false;
3181 current = abfd->archive_head;
3182 last_elt = current;
3183 for (i = 0; i < orl_count; i++)
3185 unsigned int hash, rehash = 0;
3187 /* Advance firstreal to the file position of this archive
3188 element. */
3189 if (map[i].u.abfd != last_elt)
3193 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3194 firstreal += firstreal % 2;
3195 current = current->archive_next;
3197 while (current != map[i].u.abfd);
3200 last_elt = current;
3202 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3203 if (H_GET_32 (abfd, (hashtable + (hash * 8) + 4)) != 0)
3205 unsigned int srch;
3207 /* The desired slot is already taken. */
3208 for (srch = (hash + rehash) & (hashsize - 1);
3209 srch != hash;
3210 srch = (srch + rehash) & (hashsize - 1))
3211 if (H_GET_32 (abfd, (hashtable + (srch * 8) + 4)) == 0)
3212 break;
3214 BFD_ASSERT (srch != hash);
3216 hash = srch;
3219 H_PUT_32 (abfd, map[i].namidx, (hashtable + hash * 8));
3220 H_PUT_32 (abfd, firstreal, (hashtable + hash * 8 + 4));
3223 if (bfd_write (hashtable, symdefsize, abfd) != symdefsize)
3224 return false;
3226 bfd_release (abfd, hashtable);
3228 /* Now write the strings. */
3229 H_PUT_32 (abfd, stringsize, temp);
3230 if (bfd_write (temp, 4, abfd) != 4)
3231 return false;
3232 for (i = 0; i < orl_count; i++)
3234 bfd_size_type len;
3236 len = strlen (*map[i].name) + 1;
3237 if (bfd_write (*map[i].name, len, abfd) != len)
3238 return false;
3241 /* The spec sez this should be a newline. But in order to be
3242 bug-compatible for DECstation ar we use a null. */
3243 if (padit)
3245 if (bfd_write ("", 1, abfd) != 1)
3246 return false;
3249 return true;
3252 /* ECOFF linker code. */
3254 /* Routine to create an entry in an ECOFF link hash table. */
3256 static struct bfd_hash_entry *
3257 ecoff_link_hash_newfunc (struct bfd_hash_entry *entry,
3258 struct bfd_hash_table *table,
3259 const char *string)
3261 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3263 /* Allocate the structure if it has not already been allocated by a
3264 subclass. */
3265 if (ret == NULL)
3266 ret = ((struct ecoff_link_hash_entry *)
3267 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3268 if (ret == NULL)
3269 return NULL;
3271 /* Call the allocation method of the superclass. */
3272 ret = ((struct ecoff_link_hash_entry *)
3273 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3274 table, string));
3276 if (ret)
3278 /* Set local fields. */
3279 ret->indx = -1;
3280 ret->abfd = NULL;
3281 ret->written = 0;
3282 ret->small = 0;
3283 memset ((void *) &ret->esym, 0, sizeof ret->esym);
3286 return (struct bfd_hash_entry *) ret;
3289 /* Create an ECOFF link hash table. */
3291 struct bfd_link_hash_table *
3292 _bfd_ecoff_bfd_link_hash_table_create (bfd *abfd)
3294 struct ecoff_link_hash_table *ret;
3295 size_t amt = sizeof (struct ecoff_link_hash_table);
3297 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3298 if (ret == NULL)
3299 return NULL;
3300 if (!_bfd_link_hash_table_init (&ret->root, abfd,
3301 ecoff_link_hash_newfunc,
3302 sizeof (struct ecoff_link_hash_entry)))
3304 free (ret);
3305 return NULL;
3307 return &ret->root;
3310 /* Look up an entry in an ECOFF link hash table. */
3312 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3313 ((struct ecoff_link_hash_entry *) \
3314 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3316 /* Get the ECOFF link hash table from the info structure. This is
3317 just a cast. */
3319 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3321 /* Add the external symbols of an object file to the global linker
3322 hash table. The external symbols and strings we are passed are
3323 just allocated on the stack, and will be discarded. We must
3324 explicitly save any information we may need later on in the link.
3325 We do not want to read the external symbol information again. */
3327 static bool
3328 ecoff_link_add_externals (bfd *abfd,
3329 struct bfd_link_info *info,
3330 void * external_ext,
3331 char *ssext)
3333 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3334 void (* const swap_ext_in) (bfd *, void *, EXTR *)
3335 = backend->debug_swap.swap_ext_in;
3336 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3337 unsigned long ext_count;
3338 struct bfd_link_hash_entry **sym_hash;
3339 char *ext_ptr;
3340 char *ext_end;
3341 bfd_size_type amt;
3343 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3345 amt = ext_count;
3346 amt *= sizeof (struct bfd_link_hash_entry *);
3347 sym_hash = (struct bfd_link_hash_entry **) bfd_alloc (abfd, amt);
3348 if (!sym_hash)
3349 return false;
3350 ecoff_data (abfd)->sym_hashes = (struct ecoff_link_hash_entry **) sym_hash;
3352 ext_ptr = (char *) external_ext;
3353 ext_end = ext_ptr + ext_count * external_ext_size;
3354 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3356 EXTR esym;
3357 bool skip;
3358 bfd_vma value;
3359 asection *section;
3360 const char *name;
3361 struct ecoff_link_hash_entry *h;
3363 *sym_hash = NULL;
3365 (*swap_ext_in) (abfd, (void *) ext_ptr, &esym);
3367 /* Skip debugging symbols. */
3368 skip = false;
3369 switch (esym.asym.st)
3371 case stGlobal:
3372 case stStatic:
3373 case stLabel:
3374 case stProc:
3375 case stStaticProc:
3376 break;
3377 default:
3378 skip = true;
3379 break;
3382 if (skip)
3383 continue;
3385 /* Get the information for this symbol. */
3386 value = esym.asym.value;
3387 switch (esym.asym.sc)
3389 default:
3390 case scNil:
3391 case scRegister:
3392 case scCdbLocal:
3393 case scBits:
3394 case scCdbSystem:
3395 case scRegImage:
3396 case scInfo:
3397 case scUserStruct:
3398 case scVar:
3399 case scVarRegister:
3400 case scVariant:
3401 case scBasedVar:
3402 case scXData:
3403 case scPData:
3404 section = NULL;
3405 break;
3406 case scText:
3407 section = bfd_make_section_old_way (abfd, _TEXT);
3408 value -= section->vma;
3409 break;
3410 case scData:
3411 section = bfd_make_section_old_way (abfd, _DATA);
3412 value -= section->vma;
3413 break;
3414 case scBss:
3415 section = bfd_make_section_old_way (abfd, _BSS);
3416 value -= section->vma;
3417 break;
3418 case scAbs:
3419 section = bfd_abs_section_ptr;
3420 break;
3421 case scUndefined:
3422 section = bfd_und_section_ptr;
3423 break;
3424 case scSData:
3425 section = bfd_make_section_old_way (abfd, _SDATA);
3426 value -= section->vma;
3427 break;
3428 case scSBss:
3429 section = bfd_make_section_old_way (abfd, _SBSS);
3430 value -= section->vma;
3431 break;
3432 case scRData:
3433 section = bfd_make_section_old_way (abfd, _RDATA);
3434 value -= section->vma;
3435 break;
3436 case scCommon:
3437 if (value > ecoff_data (abfd)->gp_size)
3439 section = bfd_com_section_ptr;
3440 break;
3442 /* Fall through. */
3443 case scSCommon:
3444 section = &ecoff_scom_section;
3445 break;
3446 case scSUndefined:
3447 section = bfd_und_section_ptr;
3448 break;
3449 case scInit:
3450 section = bfd_make_section_old_way (abfd, _INIT);
3451 value -= section->vma;
3452 break;
3453 case scFini:
3454 section = bfd_make_section_old_way (abfd, _FINI);
3455 value -= section->vma;
3456 break;
3457 case scRConst:
3458 section = bfd_make_section_old_way (abfd, _RCONST);
3459 value -= section->vma;
3460 break;
3463 if (section == NULL)
3464 continue;
3466 name = ssext + esym.asym.iss;
3468 if (! (_bfd_generic_link_add_one_symbol
3469 (info, abfd, name,
3470 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3471 section, value, NULL, true, true, sym_hash)))
3472 return false;
3474 h = (struct ecoff_link_hash_entry *) *sym_hash;
3476 /* If we are building an ECOFF hash table, save the external
3477 symbol information. */
3478 if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
3480 if (h->abfd == NULL
3481 || (! bfd_is_und_section (section)
3482 && (! bfd_is_com_section (section)
3483 || (h->root.type != bfd_link_hash_defined
3484 && h->root.type != bfd_link_hash_defweak))))
3486 h->abfd = abfd;
3487 h->esym = esym;
3490 /* Remember whether this symbol was small undefined. */
3491 if (esym.asym.sc == scSUndefined)
3492 h->small = 1;
3494 /* If this symbol was ever small undefined, it needs to wind
3495 up in a GP relative section. We can't control the
3496 section of a defined symbol, but we can control the
3497 section of a common symbol. This case is actually needed
3498 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
3499 if (h->small
3500 && h->root.type == bfd_link_hash_common
3501 && streq (h->root.u.c.p->section->name, SCOMMON))
3503 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
3504 SCOMMON);
3505 h->root.u.c.p->section->flags = SEC_ALLOC;
3506 if (h->esym.asym.sc == scCommon)
3507 h->esym.asym.sc = scSCommon;
3512 return true;
3515 /* Add symbols from an ECOFF object file to the global linker hash
3516 table. */
3518 static bool
3519 ecoff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
3521 HDRR *symhdr;
3522 bfd_size_type external_ext_size;
3523 void * external_ext = NULL;
3524 bfd_size_type esize;
3525 char *ssext = NULL;
3526 bool result;
3528 if (! ecoff_slurp_symbolic_header (abfd))
3529 return false;
3531 /* If there are no symbols, we don't want it. */
3532 if (bfd_get_symcount (abfd) == 0)
3533 return true;
3535 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3537 /* Read in the external symbols and external strings. */
3538 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0)
3539 return false;
3540 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3541 esize = symhdr->iextMax * external_ext_size;
3542 external_ext = _bfd_malloc_and_read (abfd, esize, esize);
3543 if (external_ext == NULL && esize != 0)
3544 goto error_return;
3546 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0)
3547 goto error_return;
3548 ssext = (char *) _bfd_malloc_and_read (abfd, symhdr->issExtMax,
3549 symhdr->issExtMax);
3550 if (ssext == NULL && symhdr->issExtMax != 0)
3551 goto error_return;
3553 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3555 free (ssext);
3556 free (external_ext);
3557 return result;
3559 error_return:
3560 free (ssext);
3561 free (external_ext);
3562 return false;
3565 /* This is called if we used _bfd_generic_link_add_archive_symbols
3566 because we were not dealing with an ECOFF archive. */
3568 static bool
3569 ecoff_link_check_archive_element (bfd *abfd,
3570 struct bfd_link_info *info,
3571 struct bfd_link_hash_entry *h,
3572 const char *name,
3573 bool *pneeded)
3575 *pneeded = false;
3577 /* Unlike the generic linker, we do not pull in elements because
3578 of common symbols. */
3579 if (h->type != bfd_link_hash_undefined)
3580 return true;
3582 /* Include this element? */
3583 if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
3584 return true;
3585 *pneeded = true;
3587 return ecoff_link_add_object_symbols (abfd, info);
3590 /* Add the symbols from an archive file to the global hash table.
3591 This looks through the undefined symbols, looks each one up in the
3592 archive hash table, and adds any associated object file. We do not
3593 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3594 already have a hash table, so there is no reason to construct
3595 another one. */
3597 static bool
3598 ecoff_link_add_archive_symbols (bfd *abfd, struct bfd_link_info *info)
3600 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3601 const bfd_byte *raw_armap;
3602 struct bfd_link_hash_entry **pundef;
3603 unsigned int armap_count;
3604 unsigned int armap_log;
3605 unsigned int i;
3606 const bfd_byte *hashtable;
3607 const char *stringbase;
3609 if (! bfd_has_map (abfd))
3611 /* An empty archive is a special case. */
3612 if (bfd_openr_next_archived_file (abfd, NULL) == NULL)
3613 return true;
3614 bfd_set_error (bfd_error_no_armap);
3615 return false;
3618 /* If we don't have any raw data for this archive, as can happen on
3619 Irix 4.0.5F, we call the generic routine.
3620 FIXME: We should be more clever about this, since someday tdata
3621 may get to something for a generic archive. */
3622 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3623 if (raw_armap == NULL)
3624 return (_bfd_generic_link_add_archive_symbols
3625 (abfd, info, ecoff_link_check_archive_element));
3627 armap_count = H_GET_32 (abfd, raw_armap);
3629 armap_log = 0;
3630 for (i = 1; i < armap_count; i <<= 1)
3631 armap_log++;
3632 BFD_ASSERT (i == armap_count);
3634 hashtable = raw_armap + 4;
3635 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3637 /* Look through the list of undefined symbols. */
3638 pundef = &info->hash->undefs;
3639 while (*pundef != NULL)
3641 struct bfd_link_hash_entry *h;
3642 unsigned int hash, rehash = 0;
3643 unsigned int file_offset;
3644 const char *name;
3645 bfd *element;
3647 h = *pundef;
3649 /* When a symbol is defined, it is not necessarily removed from
3650 the list. */
3651 if (h->type != bfd_link_hash_undefined
3652 && h->type != bfd_link_hash_common)
3654 /* Remove this entry from the list, for general cleanliness
3655 and because we are going to look through the list again
3656 if we search any more libraries. We can't remove the
3657 entry if it is the tail, because that would lose any
3658 entries we add to the list later on. */
3659 if (*pundef != info->hash->undefs_tail)
3660 *pundef = (*pundef)->u.undef.next;
3661 else
3662 pundef = &(*pundef)->u.undef.next;
3663 continue;
3666 /* Native ECOFF linkers do not pull in archive elements merely
3667 to satisfy common definitions, so neither do we. We leave
3668 them on the list, though, in case we are linking against some
3669 other object format. */
3670 if (h->type != bfd_link_hash_undefined)
3672 pundef = &(*pundef)->u.undef.next;
3673 continue;
3676 /* Look for this symbol in the archive hash table. */
3677 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3678 armap_log);
3680 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3681 if (file_offset == 0)
3683 /* Nothing in this slot. */
3684 pundef = &(*pundef)->u.undef.next;
3685 continue;
3688 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3689 if (name[0] != h->root.string[0]
3690 || ! streq (name, h->root.string))
3692 unsigned int srch;
3693 bool found;
3695 /* That was the wrong symbol. Try rehashing. */
3696 found = false;
3697 for (srch = (hash + rehash) & (armap_count - 1);
3698 srch != hash;
3699 srch = (srch + rehash) & (armap_count - 1))
3701 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3702 if (file_offset == 0)
3703 break;
3704 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3705 if (name[0] == h->root.string[0]
3706 && streq (name, h->root.string))
3708 found = true;
3709 break;
3713 if (! found)
3715 pundef = &(*pundef)->u.undef.next;
3716 continue;
3719 hash = srch;
3722 element = (*backend->get_elt_at_filepos) (abfd,
3723 (file_ptr) file_offset,
3724 info);
3725 if (element == NULL)
3726 return false;
3728 if (! bfd_check_format (element, bfd_object))
3729 return false;
3731 /* Unlike the generic linker, we know that this element provides
3732 a definition for an undefined symbol and we know that we want
3733 to include it. We don't need to check anything. */
3734 if (!(*info->callbacks
3735 ->add_archive_element) (info, element, name, &element))
3736 return false;
3737 if (! ecoff_link_add_object_symbols (element, info))
3738 return false;
3740 pundef = &(*pundef)->u.undef.next;
3743 return true;
3746 /* Given an ECOFF BFD, add symbols to the global hash table as
3747 appropriate. */
3749 bool
3750 _bfd_ecoff_bfd_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
3752 switch (bfd_get_format (abfd))
3754 case bfd_object:
3755 return ecoff_link_add_object_symbols (abfd, info);
3756 case bfd_archive:
3757 return ecoff_link_add_archive_symbols (abfd, info);
3758 default:
3759 bfd_set_error (bfd_error_wrong_format);
3760 return false;
3765 /* ECOFF final link routines. */
3767 /* Structure used to pass information to ecoff_link_write_external. */
3769 struct extsym_info
3771 bfd *abfd;
3772 struct bfd_link_info *info;
3775 /* Accumulate the debugging information for an input BFD into the
3776 output BFD. This must read in the symbolic information of the
3777 input BFD. */
3779 static bool
3780 ecoff_final_link_debug_accumulate (bfd *output_bfd,
3781 bfd *input_bfd,
3782 struct bfd_link_info *info,
3783 void * handle)
3785 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
3786 const struct ecoff_debug_swap * const swap =
3787 &ecoff_backend (input_bfd)->debug_swap;
3788 HDRR *symhdr = &debug->symbolic_header;
3789 bool ret;
3791 #define READ(ptr, offset, count, size) \
3792 do \
3794 size_t amt; \
3795 debug->ptr = NULL; \
3796 if (symhdr->count == 0) \
3797 break; \
3798 if (_bfd_mul_overflow (size, symhdr->count, &amt)) \
3800 bfd_set_error (bfd_error_file_too_big); \
3801 ret = false; \
3802 goto return_something; \
3804 if (bfd_seek (input_bfd, symhdr->offset, SEEK_SET) != 0) \
3806 ret = false; \
3807 goto return_something; \
3809 debug->ptr = _bfd_malloc_and_read (input_bfd, amt + 1, amt); \
3810 if (debug->ptr == NULL) \
3812 ret = false; \
3813 goto return_something; \
3815 ((char *) debug->ptr)[amt] = 0; \
3816 } while (0)
3818 /* If alloc_syments is true, then the data was already by read by
3819 _bfd_ecoff_slurp_symbolic_info. */
3820 if (!debug->alloc_syments)
3822 READ (line, cbLineOffset, cbLine, sizeof (unsigned char));
3823 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size);
3824 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size);
3825 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size);
3826 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size);
3827 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext));
3828 READ (ss, cbSsOffset, issMax, sizeof (char));
3829 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size);
3830 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size);
3832 #undef READ
3834 /* We do not read the external strings or the external symbols. */
3836 ret = (bfd_ecoff_debug_accumulate
3837 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
3838 &ecoff_backend (output_bfd)->debug_swap,
3839 input_bfd, debug, swap, info));
3841 return_something:
3842 _bfd_ecoff_free_ecoff_debug_info (debug);
3843 return ret;
3846 /* Relocate and write an ECOFF section into an ECOFF output file. */
3848 static bool
3849 ecoff_indirect_link_order (bfd *output_bfd,
3850 struct bfd_link_info *info,
3851 asection *output_section,
3852 struct bfd_link_order *link_order)
3854 asection *input_section;
3855 bfd *input_bfd;
3856 bfd_byte *contents = NULL;
3857 bfd_size_type external_reloc_size;
3858 bfd_size_type external_relocs_size;
3859 void * external_relocs = NULL;
3861 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
3863 input_section = link_order->u.indirect.section;
3864 input_bfd = input_section->owner;
3865 if (input_section->size == 0)
3866 return true;
3868 BFD_ASSERT (input_section->output_section == output_section);
3869 BFD_ASSERT (input_section->output_offset == link_order->offset);
3870 BFD_ASSERT (input_section->size == link_order->size);
3872 /* Get the section contents. */
3873 if (!bfd_malloc_and_get_section (input_bfd, input_section, &contents))
3874 goto error_return;
3876 /* Get the relocs. If we are relaxing MIPS code, they will already
3877 have been read in. Otherwise, we read them in now. */
3878 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
3879 external_relocs_size = external_reloc_size * input_section->reloc_count;
3881 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0)
3882 goto error_return;
3883 external_relocs = _bfd_malloc_and_read (input_bfd, external_relocs_size,
3884 external_relocs_size);
3885 if (external_relocs == NULL && external_relocs_size != 0)
3886 goto error_return;
3888 /* Relocate the section contents. */
3889 if (! ((*ecoff_backend (input_bfd)->relocate_section)
3890 (output_bfd, info, input_bfd, input_section, contents,
3891 external_relocs)))
3892 goto error_return;
3894 /* Write out the relocated section. */
3895 if (! bfd_set_section_contents (output_bfd,
3896 output_section,
3897 contents,
3898 input_section->output_offset,
3899 input_section->size))
3900 goto error_return;
3902 /* If we are producing relocatable output, the relocs were
3903 modified, and we write them out now. We use the reloc_count
3904 field of output_section to keep track of the number of relocs we
3905 have output so far. */
3906 if (bfd_link_relocatable (info))
3908 file_ptr pos = (output_section->rel_filepos
3909 + output_section->reloc_count * external_reloc_size);
3910 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
3911 || (bfd_write (external_relocs, external_relocs_size, output_bfd)
3912 != external_relocs_size))
3913 goto error_return;
3914 output_section->reloc_count += input_section->reloc_count;
3917 free (contents);
3918 free (external_relocs);
3919 return true;
3921 error_return:
3922 free (contents);
3923 free (external_relocs);
3924 return false;
3927 /* Generate a reloc when linking an ECOFF file. This is a reloc
3928 requested by the linker, and does come from any input file. This
3929 is used to build constructor and destructor tables when linking
3930 with -Ur. */
3932 static bool
3933 ecoff_reloc_link_order (bfd *output_bfd,
3934 struct bfd_link_info *info,
3935 asection *output_section,
3936 struct bfd_link_order *link_order)
3938 enum bfd_link_order_type type;
3939 asection *section;
3940 bfd_vma addend;
3941 arelent rel;
3942 struct internal_reloc in;
3943 bfd_size_type external_reloc_size;
3944 bfd_byte *rbuf;
3945 bool ok;
3946 file_ptr pos;
3948 type = link_order->type;
3949 section = NULL;
3950 addend = link_order->u.reloc.p->addend;
3952 /* We set up an arelent to pass to the backend adjust_reloc_out
3953 routine. */
3954 rel.address = link_order->offset;
3956 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
3957 if (rel.howto == 0)
3959 bfd_set_error (bfd_error_bad_value);
3960 return false;
3963 if (type == bfd_section_reloc_link_order)
3965 section = link_order->u.reloc.p->u.section;
3966 rel.sym_ptr_ptr = &section->symbol;
3968 else
3970 struct bfd_link_hash_entry *h;
3972 /* Treat a reloc against a defined symbol as though it were
3973 actually against the section. */
3974 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
3975 link_order->u.reloc.p->u.name,
3976 false, false, false);
3977 if (h != NULL
3978 && (h->type == bfd_link_hash_defined
3979 || h->type == bfd_link_hash_defweak))
3981 type = bfd_section_reloc_link_order;
3982 section = h->u.def.section->output_section;
3983 /* It seems that we ought to add the symbol value to the
3984 addend here, but in practice it has already been added
3985 because it was passed to constructor_callback. */
3986 addend += section->vma + h->u.def.section->output_offset;
3988 else
3990 /* We can't set up a reloc against a symbol correctly,
3991 because we have no asymbol structure. Currently no
3992 adjust_reloc_out routine cares. */
3993 rel.sym_ptr_ptr = NULL;
3997 /* All ECOFF relocs are in-place. Put the addend into the object
3998 file. */
4000 BFD_ASSERT (rel.howto->partial_inplace);
4001 if (addend != 0)
4003 bfd_size_type size;
4004 bfd_reloc_status_type rstat;
4005 bfd_byte *buf;
4007 size = bfd_get_reloc_size (rel.howto);
4008 buf = (bfd_byte *) bfd_zmalloc (size);
4009 if (buf == NULL && size != 0)
4010 return false;
4011 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4012 (bfd_vma) addend, buf);
4013 switch (rstat)
4015 case bfd_reloc_ok:
4016 break;
4017 default:
4018 case bfd_reloc_outofrange:
4019 abort ();
4020 case bfd_reloc_overflow:
4021 (*info->callbacks->reloc_overflow)
4022 (info, NULL,
4023 (link_order->type == bfd_section_reloc_link_order
4024 ? bfd_section_name (section)
4025 : link_order->u.reloc.p->u.name),
4026 rel.howto->name, addend, NULL, NULL, (bfd_vma) 0);
4027 break;
4029 ok = bfd_set_section_contents (output_bfd, output_section, (void *) buf,
4030 (file_ptr) link_order->offset, size);
4031 free (buf);
4032 if (! ok)
4033 return false;
4036 rel.addend = 0;
4038 /* Move the information into an internal_reloc structure. */
4039 in.r_vaddr = rel.address + bfd_section_vma (output_section);
4040 in.r_type = rel.howto->type;
4042 if (type == bfd_symbol_reloc_link_order)
4044 struct ecoff_link_hash_entry *h;
4046 h = ((struct ecoff_link_hash_entry *)
4047 bfd_wrapped_link_hash_lookup (output_bfd, info,
4048 link_order->u.reloc.p->u.name,
4049 false, false, true));
4050 if (h != NULL
4051 && h->indx != -1)
4052 in.r_symndx = h->indx;
4053 else
4055 (*info->callbacks->unattached_reloc)
4056 (info, link_order->u.reloc.p->u.name, NULL, NULL, (bfd_vma) 0);
4057 in.r_symndx = 0;
4059 in.r_extern = 1;
4061 else
4063 const char *name;
4064 unsigned int i;
4065 static struct
4067 const char * name;
4068 long r_symndx;
4070 section_symndx [] =
4072 { _TEXT, RELOC_SECTION_TEXT },
4073 { _RDATA, RELOC_SECTION_RDATA },
4074 { _DATA, RELOC_SECTION_DATA },
4075 { _SDATA, RELOC_SECTION_SDATA },
4076 { _SBSS, RELOC_SECTION_SBSS },
4077 { _BSS, RELOC_SECTION_BSS },
4078 { _INIT, RELOC_SECTION_INIT },
4079 { _LIT8, RELOC_SECTION_LIT8 },
4080 { _LIT4, RELOC_SECTION_LIT4 },
4081 { _XDATA, RELOC_SECTION_XDATA },
4082 { _PDATA, RELOC_SECTION_PDATA },
4083 { _FINI, RELOC_SECTION_FINI },
4084 { _LITA, RELOC_SECTION_LITA },
4085 { "*ABS*", RELOC_SECTION_ABS },
4086 { _RCONST, RELOC_SECTION_RCONST }
4089 name = bfd_section_name (section);
4091 for (i = 0; i < ARRAY_SIZE (section_symndx); i++)
4092 if (streq (name, section_symndx[i].name))
4094 in.r_symndx = section_symndx[i].r_symndx;
4095 break;
4098 if (i == ARRAY_SIZE (section_symndx))
4099 abort ();
4101 in.r_extern = 0;
4104 /* Let the BFD backend adjust the reloc. */
4105 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4107 /* Get some memory and swap out the reloc. */
4108 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4109 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4110 if (rbuf == NULL)
4111 return false;
4113 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (void *) rbuf);
4115 pos = (output_section->rel_filepos
4116 + output_section->reloc_count * external_reloc_size);
4117 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4118 && (bfd_write (rbuf, external_reloc_size, output_bfd)
4119 == external_reloc_size));
4121 if (ok)
4122 ++output_section->reloc_count;
4124 free (rbuf);
4126 return ok;
4129 /* Put out information for an external symbol. These come only from
4130 the hash table. */
4132 static bool
4133 ecoff_link_write_external (struct bfd_hash_entry *bh, void * data)
4135 struct ecoff_link_hash_entry *h = (struct ecoff_link_hash_entry *) bh;
4136 struct extsym_info *einfo = (struct extsym_info *) data;
4137 bfd *output_bfd = einfo->abfd;
4138 bool strip;
4140 if (h->root.type == bfd_link_hash_warning)
4142 h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4143 if (h->root.type == bfd_link_hash_new)
4144 return true;
4147 /* We need to check if this symbol is being stripped. */
4148 if (h->root.type == bfd_link_hash_undefined
4149 || h->root.type == bfd_link_hash_undefweak)
4150 strip = false;
4151 else if (einfo->info->strip == strip_all
4152 || (einfo->info->strip == strip_some
4153 && bfd_hash_lookup (einfo->info->keep_hash,
4154 h->root.root.string,
4155 false, false) == NULL))
4156 strip = true;
4157 else
4158 strip = false;
4160 if (strip || h->written)
4161 return true;
4163 if (h->abfd == NULL)
4165 h->esym.jmptbl = 0;
4166 h->esym.cobol_main = 0;
4167 h->esym.weakext = 0;
4168 h->esym.reserved = 0;
4169 h->esym.ifd = ifdNil;
4170 h->esym.asym.value = 0;
4171 h->esym.asym.st = stGlobal;
4173 if (h->root.type != bfd_link_hash_defined
4174 && h->root.type != bfd_link_hash_defweak)
4175 h->esym.asym.sc = scAbs;
4176 else
4178 asection *output_section;
4179 const char *name;
4180 unsigned int i;
4181 static struct
4183 const char * name;
4184 int sc;
4186 section_storage_classes [] =
4188 { _TEXT, scText },
4189 { _DATA, scData },
4190 { _SDATA, scSData },
4191 { _RDATA, scRData },
4192 { _BSS, scBss },
4193 { _SBSS, scSBss },
4194 { _INIT, scInit },
4195 { _FINI, scFini },
4196 { _PDATA, scPData },
4197 { _XDATA, scXData },
4198 { _RCONST, scRConst }
4201 output_section = h->root.u.def.section->output_section;
4202 name = bfd_section_name (output_section);
4204 for (i = 0; i < ARRAY_SIZE (section_storage_classes); i++)
4205 if (streq (name, section_storage_classes[i].name))
4207 h->esym.asym.sc = section_storage_classes[i].sc;
4208 break;
4211 if (i == ARRAY_SIZE (section_storage_classes))
4212 h->esym.asym.sc = scAbs;
4215 h->esym.asym.reserved = 0;
4216 h->esym.asym.index = indexNil;
4218 else if (h->esym.ifd != -1)
4220 struct ecoff_debug_info *debug;
4222 /* Adjust the FDR index for the symbol by that used for the
4223 input BFD. */
4224 debug = &ecoff_data (h->abfd)->debug_info;
4225 BFD_ASSERT (h->esym.ifd >= 0
4226 && h->esym.ifd < debug->symbolic_header.ifdMax);
4227 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4230 switch (h->root.type)
4232 default:
4233 case bfd_link_hash_warning:
4234 case bfd_link_hash_new:
4235 abort ();
4236 case bfd_link_hash_undefined:
4237 case bfd_link_hash_undefweak:
4238 if (h->esym.asym.sc != scUndefined
4239 && h->esym.asym.sc != scSUndefined)
4240 h->esym.asym.sc = scUndefined;
4241 break;
4242 case bfd_link_hash_defined:
4243 case bfd_link_hash_defweak:
4244 if (h->esym.asym.sc == scUndefined
4245 || h->esym.asym.sc == scSUndefined)
4246 h->esym.asym.sc = scAbs;
4247 else if (h->esym.asym.sc == scCommon)
4248 h->esym.asym.sc = scBss;
4249 else if (h->esym.asym.sc == scSCommon)
4250 h->esym.asym.sc = scSBss;
4251 h->esym.asym.value = (h->root.u.def.value
4252 + h->root.u.def.section->output_section->vma
4253 + h->root.u.def.section->output_offset);
4254 break;
4255 case bfd_link_hash_common:
4256 if (h->esym.asym.sc != scCommon
4257 && h->esym.asym.sc != scSCommon)
4258 h->esym.asym.sc = scCommon;
4259 h->esym.asym.value = h->root.u.c.size;
4260 break;
4261 case bfd_link_hash_indirect:
4262 /* We ignore these symbols, since the indirected symbol is
4263 already in the hash table. */
4264 return true;
4267 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4268 symbol number. */
4269 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4270 h->written = 1;
4272 return (bfd_ecoff_debug_one_external
4273 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4274 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4275 &h->esym));
4278 /* ECOFF final link routine. This looks through all the input BFDs
4279 and gathers together all the debugging information, and then
4280 processes all the link order information. This may cause it to
4281 close and reopen some input BFDs; I'll see how bad this is. */
4283 bool
4284 _bfd_ecoff_bfd_final_link (bfd *abfd, struct bfd_link_info *info)
4286 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4287 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4288 HDRR *symhdr;
4289 void * handle;
4290 bfd *input_bfd;
4291 asection *o;
4292 struct bfd_link_order *p;
4293 struct extsym_info einfo;
4295 /* We accumulate the debugging information counts in the symbolic
4296 header. */
4297 symhdr = &debug->symbolic_header;
4298 symhdr->vstamp = 0;
4299 symhdr->ilineMax = 0;
4300 symhdr->cbLine = 0;
4301 symhdr->idnMax = 0;
4302 symhdr->ipdMax = 0;
4303 symhdr->isymMax = 0;
4304 symhdr->ioptMax = 0;
4305 symhdr->iauxMax = 0;
4306 symhdr->issMax = 0;
4307 symhdr->issExtMax = 0;
4308 symhdr->ifdMax = 0;
4309 symhdr->crfd = 0;
4310 symhdr->iextMax = 0;
4312 /* We accumulate the debugging information itself in the debug_info
4313 structure. */
4314 debug->line = NULL;
4315 debug->external_dnr = NULL;
4316 debug->external_pdr = NULL;
4317 debug->external_sym = NULL;
4318 debug->external_opt = NULL;
4319 debug->external_aux = NULL;
4320 debug->ss = NULL;
4321 debug->ssext = debug->ssext_end = NULL;
4322 debug->external_fdr = NULL;
4323 debug->external_rfd = NULL;
4324 debug->external_ext = debug->external_ext_end = NULL;
4326 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4327 if (handle == NULL)
4328 return false;
4330 /* Accumulate the debugging symbols from each input BFD. */
4331 for (input_bfd = info->input_bfds;
4332 input_bfd != NULL;
4333 input_bfd = input_bfd->link.next)
4335 bool ret;
4337 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4339 /* Arbitrarily set the symbolic header vstamp to the vstamp
4340 of the first object file in the link. */
4341 if (symhdr->vstamp == 0)
4342 symhdr->vstamp
4343 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4344 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4345 handle);
4347 else
4348 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4349 debug, &backend->debug_swap,
4350 input_bfd, info);
4351 if (! ret)
4352 return false;
4354 /* Combine the register masks. */
4355 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4356 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4357 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4358 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4359 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4360 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4363 /* Write out the external symbols. */
4364 einfo.abfd = abfd;
4365 einfo.info = info;
4366 bfd_hash_traverse (&info->hash->table, ecoff_link_write_external, &einfo);
4368 if (bfd_link_relocatable (info))
4370 /* We need to make a pass over the link_orders to count up the
4371 number of relocations we will need to output, so that we know
4372 how much space they will take up. */
4373 for (o = abfd->sections; o != NULL; o = o->next)
4375 o->reloc_count = 0;
4376 for (p = o->map_head.link_order;
4377 p != NULL;
4378 p = p->next)
4379 if (p->type == bfd_indirect_link_order)
4380 o->reloc_count += p->u.indirect.section->reloc_count;
4381 else if (p->type == bfd_section_reloc_link_order
4382 || p->type == bfd_symbol_reloc_link_order)
4383 ++o->reloc_count;
4387 /* Compute the reloc and symbol file positions. */
4388 ecoff_compute_reloc_file_positions (abfd);
4390 /* Write out the debugging information. */
4391 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4392 &backend->debug_swap, info,
4393 ecoff_data (abfd)->sym_filepos))
4394 return false;
4396 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4398 if (bfd_link_relocatable (info))
4400 /* Now reset the reloc_count field of the sections in the output
4401 BFD to 0, so that we can use them to keep track of how many
4402 relocs we have output thus far. */
4403 for (o = abfd->sections; o != NULL; o = o->next)
4404 o->reloc_count = 0;
4407 /* Get a value for the GP register. */
4408 if (ecoff_data (abfd)->gp == 0)
4410 struct bfd_link_hash_entry *h;
4412 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4413 if (h != NULL
4414 && h->type == bfd_link_hash_defined)
4415 ecoff_data (abfd)->gp = (h->u.def.value
4416 + h->u.def.section->output_section->vma
4417 + h->u.def.section->output_offset);
4418 else if (bfd_link_relocatable (info))
4420 bfd_vma lo;
4422 /* Make up a value. */
4423 lo = (bfd_vma) -1;
4424 for (o = abfd->sections; o != NULL; o = o->next)
4426 if (o->vma < lo
4427 && (streq (o->name, _SBSS)
4428 || streq (o->name, _SDATA)
4429 || streq (o->name, _LIT4)
4430 || streq (o->name, _LIT8)
4431 || streq (o->name, _LITA)))
4432 lo = o->vma;
4434 ecoff_data (abfd)->gp = lo + 0x8000;
4436 else
4438 /* If the relocate_section function needs to do a reloc
4439 involving the GP value, it should make a reloc_dangerous
4440 callback to warn that GP is not defined. */
4444 for (o = abfd->sections; o != NULL; o = o->next)
4446 for (p = o->map_head.link_order;
4447 p != NULL;
4448 p = p->next)
4450 if (p->type == bfd_indirect_link_order
4451 && (bfd_get_flavour (p->u.indirect.section->owner)
4452 == bfd_target_ecoff_flavour))
4454 if (! ecoff_indirect_link_order (abfd, info, o, p))
4455 return false;
4457 else if (p->type == bfd_section_reloc_link_order
4458 || p->type == bfd_symbol_reloc_link_order)
4460 if (! ecoff_reloc_link_order (abfd, info, o, p))
4461 return false;
4463 else
4465 if (! _bfd_default_link_order (abfd, info, o, p))
4466 return false;
4471 abfd->symcount = symhdr->iextMax + symhdr->isymMax;
4473 ecoff_data (abfd)->linker = true;
4475 return true;