* "objcopy -O binary" warning tweak, suggested by dmoseley
[binutils-gdb.git] / bfd / ecoff.c
blobdcedb7b9f71d8f79715a6797b882aabafdad209d
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Original version by Per Bothner.
4 Full support added by Ian Lance Taylor, ian@cygnus.com.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "bfdlink.h"
25 #include "libbfd.h"
26 #include "aout/ar.h"
27 #include "aout/ranlib.h"
28 #include "aout/stab_gnu.h"
30 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
31 some other stuff which we don't want and which conflicts with stuff
32 we do want. */
33 #include "libaout.h"
34 #include "aout/aout64.h"
35 #undef N_ABS
36 #undef exec_hdr
37 #undef obj_sym_filepos
39 #include "coff/internal.h"
40 #include "coff/sym.h"
41 #include "coff/symconst.h"
42 #include "coff/ecoff.h"
43 #include "libcoff.h"
44 #include "libecoff.h"
46 /* Prototypes for static functions. */
48 static int ecoff_get_magic PARAMS ((bfd *abfd));
49 static long ecoff_sec_to_styp_flags PARAMS ((const char *name,
50 flagword flags));
51 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
52 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
53 asymbol *asym, int ext, int weak));
54 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, FDR *fdr,
55 char *string,
56 RNDXR *rndx, long isym,
57 const char *which));
58 static char *ecoff_type_to_string PARAMS ((bfd *abfd, FDR *fdr,
59 unsigned int indx));
60 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
61 asymbol **symbols));
62 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
63 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
64 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
65 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
66 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
67 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
68 unsigned int *rehash,
69 unsigned int size,
70 unsigned int hlog));
72 /* This stuff is somewhat copied from coffcode.h. */
74 static asection bfd_debug_section = { "*DEBUG*" };
76 /* Create an ECOFF object. */
78 boolean
79 _bfd_ecoff_mkobject (abfd)
80 bfd *abfd;
82 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
83 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
84 if (abfd->tdata.ecoff_obj_data == NULL)
85 return false;
87 return true;
90 /* This is a hook called by coff_real_object_p to create any backend
91 specific information. */
93 PTR
94 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
95 bfd *abfd;
96 PTR filehdr;
97 PTR aouthdr;
99 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
100 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
101 ecoff_data_type *ecoff;
103 if (_bfd_ecoff_mkobject (abfd) == false)
104 return NULL;
106 ecoff = ecoff_data (abfd);
107 ecoff->gp_size = 8;
108 ecoff->sym_filepos = internal_f->f_symptr;
110 if (internal_a != (struct internal_aouthdr *) NULL)
112 int i;
114 ecoff->text_start = internal_a->text_start;
115 ecoff->text_end = internal_a->text_start + internal_a->tsize;
116 ecoff->gp = internal_a->gp_value;
117 ecoff->gprmask = internal_a->gprmask;
118 for (i = 0; i < 4; i++)
119 ecoff->cprmask[i] = internal_a->cprmask[i];
120 ecoff->fprmask = internal_a->fprmask;
121 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
122 abfd->flags |= D_PAGED;
123 else
124 abfd->flags &=~ D_PAGED;
127 /* It turns out that no special action is required by the MIPS or
128 Alpha ECOFF backends. They have different information in the
129 a.out header, but we just copy it all (e.g., gprmask, cprmask and
130 fprmask) and let the swapping routines ensure that only relevant
131 information is written out. */
133 return (PTR) ecoff;
136 /* Initialize a new section. */
138 boolean
139 _bfd_ecoff_new_section_hook (abfd, section)
140 bfd *abfd;
141 asection *section;
143 section->alignment_power = 4;
145 if (strcmp (section->name, _TEXT) == 0
146 || strcmp (section->name, _INIT) == 0
147 || strcmp (section->name, _FINI) == 0)
148 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
149 else if (strcmp (section->name, _DATA) == 0
150 || strcmp (section->name, _SDATA) == 0)
151 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
152 else if (strcmp (section->name, _RDATA) == 0
153 || strcmp (section->name, _LIT8) == 0
154 || strcmp (section->name, _LIT4) == 0
155 || strcmp (section->name, _RCONST) == 0
156 || strcmp (section->name, _PDATA) == 0)
157 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
158 else if (strcmp (section->name, _BSS) == 0
159 || strcmp (section->name, _SBSS) == 0)
160 section->flags |= SEC_ALLOC;
161 else if (strcmp (section->name, _LIB) == 0)
163 /* An Irix 4 shared libary. */
164 section->flags |= SEC_COFF_SHARED_LIBRARY;
167 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
168 uncertain about .init on some systems and I don't know how shared
169 libraries work. */
171 return true;
174 /* Determine the machine architecture and type. This is called from
175 the generic COFF routines. It is the inverse of ecoff_get_magic,
176 below. This could be an ECOFF backend routine, with one version
177 for each target, but there aren't all that many ECOFF targets. */
179 boolean
180 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
181 bfd *abfd;
182 PTR filehdr;
184 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
185 enum bfd_architecture arch;
186 unsigned long mach;
188 switch (internal_f->f_magic)
190 case MIPS_MAGIC_1:
191 case MIPS_MAGIC_LITTLE:
192 case MIPS_MAGIC_BIG:
193 arch = bfd_arch_mips;
194 mach = 3000;
195 break;
197 case MIPS_MAGIC_LITTLE2:
198 case MIPS_MAGIC_BIG2:
199 /* MIPS ISA level 2: the r6000 */
200 arch = bfd_arch_mips;
201 mach = 6000;
202 break;
204 case MIPS_MAGIC_LITTLE3:
205 case MIPS_MAGIC_BIG3:
206 /* MIPS ISA level 3: the r4000 */
207 arch = bfd_arch_mips;
208 mach = 4000;
209 break;
211 case ALPHA_MAGIC:
212 case ALPHA_MAGIC_BSD:
213 arch = bfd_arch_alpha;
214 mach = 0;
215 break;
217 default:
218 arch = bfd_arch_obscure;
219 mach = 0;
220 break;
223 return bfd_default_set_arch_mach (abfd, arch, mach);
226 /* Get the magic number to use based on the architecture and machine.
227 This is the inverse of _bfd_ecoff_set_arch_mach_hook, above. */
229 static int
230 ecoff_get_magic (abfd)
231 bfd *abfd;
233 extern const bfd_target bsd_ecoffalpha_little_vec;
234 int big, little;
236 switch (bfd_get_arch (abfd))
238 case bfd_arch_mips:
239 switch (bfd_get_mach (abfd))
241 default:
242 case 0:
243 case 3000:
244 big = MIPS_MAGIC_BIG;
245 little = MIPS_MAGIC_LITTLE;
246 break;
248 case 6000:
249 big = MIPS_MAGIC_BIG2;
250 little = MIPS_MAGIC_LITTLE2;
251 break;
253 case 4000:
254 big = MIPS_MAGIC_BIG3;
255 little = MIPS_MAGIC_LITTLE3;
256 break;
259 return bfd_big_endian (abfd) ? big : little;
261 case bfd_arch_alpha:
262 return (abfd->xvec == &bsd_ecoffalpha_little_vec
263 ? ALPHA_MAGIC_BSD : ALPHA_MAGIC);
265 default:
266 abort ();
267 return 0;
271 /* Get the section s_flags to use for a section. */
273 static long
274 ecoff_sec_to_styp_flags (name, flags)
275 const char *name;
276 flagword flags;
278 long styp;
280 styp = 0;
282 if (strcmp (name, _TEXT) == 0)
283 styp = STYP_TEXT;
284 else if (strcmp (name, _DATA) == 0)
285 styp = STYP_DATA;
286 else if (strcmp (name, _SDATA) == 0)
287 styp = STYP_SDATA;
288 else if (strcmp (name, _RDATA) == 0)
289 styp = STYP_RDATA;
290 else if (strcmp (name, _LITA) == 0)
291 styp = STYP_LITA;
292 else if (strcmp (name, _LIT8) == 0)
293 styp = STYP_LIT8;
294 else if (strcmp (name, _LIT4) == 0)
295 styp = STYP_LIT4;
296 else if (strcmp (name, _BSS) == 0)
297 styp = STYP_BSS;
298 else if (strcmp (name, _SBSS) == 0)
299 styp = STYP_SBSS;
300 else if (strcmp (name, _INIT) == 0)
301 styp = STYP_ECOFF_INIT;
302 else if (strcmp (name, _FINI) == 0)
303 styp = STYP_ECOFF_FINI;
304 else if (strcmp (name, _PDATA) == 0)
305 styp = STYP_PDATA;
306 else if (strcmp (name, _XDATA) == 0)
307 styp = STYP_XDATA;
308 else if (strcmp (name, _LIB) == 0)
309 styp = STYP_ECOFF_LIB;
310 else if (strcmp (name, _GOT) == 0)
311 styp = STYP_GOT;
312 else if (strcmp (name, _HASH) == 0)
313 styp = STYP_HASH;
314 else if (strcmp (name, _DYNAMIC) == 0)
315 styp = STYP_DYNAMIC;
316 else if (strcmp (name, _LIBLIST) == 0)
317 styp = STYP_LIBLIST;
318 else if (strcmp (name, _RELDYN) == 0)
319 styp = STYP_RELDYN;
320 else if (strcmp (name, _CONFLIC) == 0)
321 styp = STYP_CONFLIC;
322 else if (strcmp (name, _DYNSTR) == 0)
323 styp = STYP_DYNSTR;
324 else if (strcmp (name, _DYNSYM) == 0)
325 styp = STYP_DYNSYM;
326 else if (strcmp (name, _COMMENT) == 0)
328 styp = STYP_COMMENT;
329 flags &=~ SEC_NEVER_LOAD;
331 else if (strcmp (name, _RCONST) == 0)
332 styp = STYP_RCONST;
333 else if (flags & SEC_CODE)
334 styp = STYP_TEXT;
335 else if (flags & SEC_DATA)
336 styp = STYP_DATA;
337 else if (flags & SEC_READONLY)
338 styp = STYP_RDATA;
339 else if (flags & SEC_LOAD)
340 styp = STYP_REG;
341 else
342 styp = STYP_BSS;
344 if (flags & SEC_NEVER_LOAD)
345 styp |= STYP_NOLOAD;
347 return styp;
350 /* Get the BFD flags to use for a section. */
352 /*ARGSUSED*/
353 flagword
354 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name)
355 bfd *abfd;
356 PTR hdr;
357 const char *name;
359 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
360 long styp_flags = internal_s->s_flags;
361 flagword sec_flags=0;
363 if (styp_flags & STYP_NOLOAD)
364 sec_flags |= SEC_NEVER_LOAD;
366 /* For 386 COFF, at least, an unloadable text or data section is
367 actually a shared library section. */
368 if ((styp_flags & STYP_TEXT)
369 || (styp_flags & STYP_ECOFF_INIT)
370 || (styp_flags & STYP_ECOFF_FINI)
371 || (styp_flags & STYP_DYNAMIC)
372 || (styp_flags & STYP_LIBLIST)
373 || (styp_flags & STYP_RELDYN)
374 || styp_flags == STYP_CONFLIC
375 || (styp_flags & STYP_DYNSTR)
376 || (styp_flags & STYP_DYNSYM)
377 || (styp_flags & STYP_HASH))
379 if (sec_flags & SEC_NEVER_LOAD)
380 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
381 else
382 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
384 else if ((styp_flags & STYP_DATA)
385 || (styp_flags & STYP_RDATA)
386 || (styp_flags & STYP_SDATA)
387 || styp_flags == STYP_PDATA
388 || styp_flags == STYP_XDATA
389 || (styp_flags & STYP_GOT)
390 || styp_flags == STYP_RCONST)
392 if (sec_flags & SEC_NEVER_LOAD)
393 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
394 else
395 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
396 if ((styp_flags & STYP_RDATA)
397 || styp_flags == STYP_PDATA
398 || styp_flags == STYP_RCONST)
399 sec_flags |= SEC_READONLY;
401 else if ((styp_flags & STYP_BSS)
402 || (styp_flags & STYP_SBSS))
404 sec_flags |= SEC_ALLOC;
406 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
408 sec_flags |= SEC_NEVER_LOAD;
410 else if ((styp_flags & STYP_LITA)
411 || (styp_flags & STYP_LIT8)
412 || (styp_flags & STYP_LIT4))
414 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
416 else if (styp_flags & STYP_ECOFF_LIB)
418 sec_flags |= SEC_COFF_SHARED_LIBRARY;
420 else
422 sec_flags |= SEC_ALLOC | SEC_LOAD;
425 return sec_flags;
428 /* Read in the symbolic header for an ECOFF object file. */
430 static boolean
431 ecoff_slurp_symbolic_header (abfd)
432 bfd *abfd;
434 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
435 bfd_size_type external_hdr_size;
436 PTR raw = NULL;
437 HDRR *internal_symhdr;
439 /* See if we've already read it in. */
440 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
441 backend->debug_swap.sym_magic)
442 return true;
444 /* See whether there is a symbolic header. */
445 if (ecoff_data (abfd)->sym_filepos == 0)
447 bfd_get_symcount (abfd) = 0;
448 return true;
451 /* At this point bfd_get_symcount (abfd) holds the number of symbols
452 as read from the file header, but on ECOFF this is always the
453 size of the symbolic information header. It would be cleaner to
454 handle this when we first read the file in coffgen.c. */
455 external_hdr_size = backend->debug_swap.external_hdr_size;
456 if (bfd_get_symcount (abfd) != external_hdr_size)
458 bfd_set_error (bfd_error_bad_value);
459 return false;
462 /* Read the symbolic information header. */
463 raw = (PTR) bfd_malloc ((size_t) external_hdr_size);
464 if (raw == NULL)
465 goto error_return;
467 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
468 || (bfd_read (raw, external_hdr_size, 1, abfd)
469 != external_hdr_size))
470 goto error_return;
471 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
472 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
474 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
476 bfd_set_error (bfd_error_bad_value);
477 goto error_return;
480 /* Now we can get the correct number of symbols. */
481 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
482 + internal_symhdr->iextMax);
484 if (raw != NULL)
485 free (raw);
486 return true;
487 error_return:
488 if (raw != NULL)
489 free (raw);
490 return false;
493 /* Read in and swap the important symbolic information for an ECOFF
494 object file. This is called by gdb via the read_debug_info entry
495 point in the backend structure. */
497 /*ARGSUSED*/
498 boolean
499 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
500 bfd *abfd;
501 asection *ignore;
502 struct ecoff_debug_info *debug;
504 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
505 HDRR *internal_symhdr;
506 bfd_size_type raw_base;
507 bfd_size_type raw_size;
508 PTR raw;
509 bfd_size_type external_fdr_size;
510 char *fraw_src;
511 char *fraw_end;
512 struct fdr *fdr_ptr;
513 bfd_size_type raw_end;
514 bfd_size_type cb_end;
516 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
518 /* Check whether we've already gotten it, and whether there's any to
519 get. */
520 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
521 return true;
522 if (ecoff_data (abfd)->sym_filepos == 0)
524 bfd_get_symcount (abfd) = 0;
525 return true;
528 if (! ecoff_slurp_symbolic_header (abfd))
529 return false;
531 internal_symhdr = &debug->symbolic_header;
533 /* Read all the symbolic information at once. */
534 raw_base = (ecoff_data (abfd)->sym_filepos
535 + backend->debug_swap.external_hdr_size);
537 /* Alpha ecoff makes the determination of raw_size difficult. It has
538 an undocumented debug data section between the symhdr and the first
539 documented section. And the ordering of the sections varies between
540 statically and dynamically linked executables.
541 If bfd supports SEEK_END someday, this code could be simplified. */
543 raw_end = 0;
545 #define UPDATE_RAW_END(start, count, size) \
546 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
547 if (cb_end > raw_end) \
548 raw_end = cb_end
550 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
551 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
552 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
553 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
554 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
555 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
556 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
557 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
558 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
559 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
560 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
562 #undef UPDATE_RAW_END
564 raw_size = raw_end - raw_base;
565 if (raw_size == 0)
567 ecoff_data (abfd)->sym_filepos = 0;
568 return true;
570 raw = (PTR) bfd_alloc (abfd, raw_size);
571 if (raw == NULL)
572 return false;
573 if (bfd_seek (abfd,
574 (ecoff_data (abfd)->sym_filepos
575 + backend->debug_swap.external_hdr_size),
576 SEEK_SET) != 0
577 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
579 bfd_release (abfd, raw);
580 return false;
583 ecoff_data (abfd)->raw_syments = raw;
585 /* Get pointers for the numeric offsets in the HDRR structure. */
586 #define FIX(off1, off2, type) \
587 if (internal_symhdr->off1 == 0) \
588 debug->off2 = (type) NULL; \
589 else \
590 debug->off2 = (type) ((char *) raw \
591 + (internal_symhdr->off1 \
592 - raw_base))
593 FIX (cbLineOffset, line, unsigned char *);
594 FIX (cbDnOffset, external_dnr, PTR);
595 FIX (cbPdOffset, external_pdr, PTR);
596 FIX (cbSymOffset, external_sym, PTR);
597 FIX (cbOptOffset, external_opt, PTR);
598 FIX (cbAuxOffset, external_aux, union aux_ext *);
599 FIX (cbSsOffset, ss, char *);
600 FIX (cbSsExtOffset, ssext, char *);
601 FIX (cbFdOffset, external_fdr, PTR);
602 FIX (cbRfdOffset, external_rfd, PTR);
603 FIX (cbExtOffset, external_ext, PTR);
604 #undef FIX
606 /* I don't want to always swap all the data, because it will just
607 waste time and most programs will never look at it. The only
608 time the linker needs most of the debugging information swapped
609 is when linking big-endian and little-endian MIPS object files
610 together, which is not a common occurrence.
612 We need to look at the fdr to deal with a lot of information in
613 the symbols, so we swap them here. */
614 debug->fdr = (struct fdr *) bfd_alloc (abfd,
615 (internal_symhdr->ifdMax *
616 sizeof (struct fdr)));
617 if (debug->fdr == NULL)
618 return false;
619 external_fdr_size = backend->debug_swap.external_fdr_size;
620 fdr_ptr = debug->fdr;
621 fraw_src = (char *) debug->external_fdr;
622 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
623 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
624 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
626 return true;
629 /* ECOFF symbol table routines. The ECOFF symbol table is described
630 in gcc/mips-tfile.c. */
632 /* ECOFF uses two common sections. One is the usual one, and the
633 other is for small objects. All the small objects are kept
634 together, and then referenced via the gp pointer, which yields
635 faster assembler code. This is what we use for the small common
636 section. */
637 static asection ecoff_scom_section;
638 static asymbol ecoff_scom_symbol;
639 static asymbol *ecoff_scom_symbol_ptr;
641 /* Create an empty symbol. */
643 asymbol *
644 _bfd_ecoff_make_empty_symbol (abfd)
645 bfd *abfd;
647 ecoff_symbol_type *new;
649 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
650 if (new == (ecoff_symbol_type *) NULL)
651 return (asymbol *) NULL;
652 memset ((PTR) new, 0, sizeof *new);
653 new->symbol.section = (asection *) NULL;
654 new->fdr = (FDR *) NULL;
655 new->local = false;
656 new->native = NULL;
657 new->symbol.the_bfd = abfd;
658 return &new->symbol;
661 /* Set the BFD flags and section for an ECOFF symbol. */
663 static boolean
664 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
665 bfd *abfd;
666 SYMR *ecoff_sym;
667 asymbol *asym;
668 int ext;
669 int weak;
671 asym->the_bfd = abfd;
672 asym->value = ecoff_sym->value;
673 asym->section = &bfd_debug_section;
674 asym->udata.i = 0;
676 /* Most symbol types are just for debugging. */
677 switch (ecoff_sym->st)
679 case stGlobal:
680 case stStatic:
681 case stLabel:
682 case stProc:
683 case stStaticProc:
684 break;
685 case stNil:
686 if (ECOFF_IS_STAB (ecoff_sym))
688 asym->flags = BSF_DEBUGGING;
689 return true;
691 break;
692 default:
693 asym->flags = BSF_DEBUGGING;
694 return true;
697 if (weak)
698 asym->flags = BSF_EXPORT | BSF_WEAK;
699 else if (ext)
700 asym->flags = BSF_EXPORT | BSF_GLOBAL;
701 else
703 asym->flags = BSF_LOCAL;
704 /* Normally, a local stProc symbol will have a corresponding
705 external symbol. We mark the local symbol as a debugging
706 symbol, in order to prevent nm from printing both out.
707 Similarly, we mark stLabel and stabs symbols as debugging
708 symbols. In both cases, we do want to set the value
709 correctly based on the symbol class. */
710 if (ecoff_sym->st == stProc
711 || ecoff_sym->st == stLabel
712 || ECOFF_IS_STAB (ecoff_sym))
713 asym->flags |= BSF_DEBUGGING;
715 switch (ecoff_sym->sc)
717 case scNil:
718 /* Used for compiler generated labels. Leave them in the
719 debugging section, and mark them as local. If BSF_DEBUGGING
720 is set, then nm does not display them for some reason. If no
721 flags are set then the linker whines about them. */
722 asym->flags = BSF_LOCAL;
723 break;
724 case scText:
725 asym->section = bfd_make_section_old_way (abfd, ".text");
726 asym->value -= asym->section->vma;
727 break;
728 case scData:
729 asym->section = bfd_make_section_old_way (abfd, ".data");
730 asym->value -= asym->section->vma;
731 break;
732 case scBss:
733 asym->section = bfd_make_section_old_way (abfd, ".bss");
734 asym->value -= asym->section->vma;
735 break;
736 case scRegister:
737 asym->flags = BSF_DEBUGGING;
738 break;
739 case scAbs:
740 asym->section = bfd_abs_section_ptr;
741 break;
742 case scUndefined:
743 asym->section = bfd_und_section_ptr;
744 asym->flags = 0;
745 asym->value = 0;
746 break;
747 case scCdbLocal:
748 case scBits:
749 case scCdbSystem:
750 case scRegImage:
751 case scInfo:
752 case scUserStruct:
753 asym->flags = BSF_DEBUGGING;
754 break;
755 case scSData:
756 asym->section = bfd_make_section_old_way (abfd, ".sdata");
757 asym->value -= asym->section->vma;
758 break;
759 case scSBss:
760 asym->section = bfd_make_section_old_way (abfd, ".sbss");
761 asym->value -= asym->section->vma;
762 break;
763 case scRData:
764 asym->section = bfd_make_section_old_way (abfd, ".rdata");
765 asym->value -= asym->section->vma;
766 break;
767 case scVar:
768 asym->flags = BSF_DEBUGGING;
769 break;
770 case scCommon:
771 if (asym->value > ecoff_data (abfd)->gp_size)
773 asym->section = bfd_com_section_ptr;
774 asym->flags = 0;
775 break;
777 /* Fall through. */
778 case scSCommon:
779 if (ecoff_scom_section.name == NULL)
781 /* Initialize the small common section. */
782 ecoff_scom_section.name = SCOMMON;
783 ecoff_scom_section.flags = SEC_IS_COMMON;
784 ecoff_scom_section.output_section = &ecoff_scom_section;
785 ecoff_scom_section.symbol = &ecoff_scom_symbol;
786 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
787 ecoff_scom_symbol.name = SCOMMON;
788 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
789 ecoff_scom_symbol.section = &ecoff_scom_section;
790 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
792 asym->section = &ecoff_scom_section;
793 asym->flags = 0;
794 break;
795 case scVarRegister:
796 case scVariant:
797 asym->flags = BSF_DEBUGGING;
798 break;
799 case scSUndefined:
800 asym->section = bfd_und_section_ptr;
801 asym->flags = 0;
802 asym->value = 0;
803 break;
804 case scInit:
805 asym->section = bfd_make_section_old_way (abfd, ".init");
806 asym->value -= asym->section->vma;
807 break;
808 case scBasedVar:
809 case scXData:
810 case scPData:
811 asym->flags = BSF_DEBUGGING;
812 break;
813 case scFini:
814 asym->section = bfd_make_section_old_way (abfd, ".fini");
815 asym->value -= asym->section->vma;
816 break;
817 case scRConst:
818 asym->section = bfd_make_section_old_way (abfd, ".rconst");
819 asym->value -= asym->section->vma;
820 break;
821 default:
822 break;
825 /* Look for special constructors symbols and make relocation entries
826 in a special construction section. These are produced by the
827 -fgnu-linker argument to g++. */
828 if (ECOFF_IS_STAB (ecoff_sym))
830 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
832 default:
833 break;
835 case N_SETA:
836 case N_SETT:
837 case N_SETD:
838 case N_SETB:
840 /* This code is no longer needed. It used to be used to
841 make the linker handle set symbols, but they are now
842 handled in the add_symbols routine instead. */
843 #if 0
844 const char *name;
845 asection *section;
846 arelent_chain *reloc_chain;
847 unsigned int bitsize;
849 /* Get a section with the same name as the symbol (usually
850 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
851 name ___CTOR_LIST (three underscores). We need
852 __CTOR_LIST (two underscores), since ECOFF doesn't use
853 a leading underscore. This should be handled by gcc,
854 but instead we do it here. Actually, this should all
855 be done differently anyhow. */
856 name = bfd_asymbol_name (asym);
857 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
859 ++name;
860 asym->name = name;
862 section = bfd_get_section_by_name (abfd, name);
863 if (section == (asection *) NULL)
865 char *copy;
867 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
868 if (!copy)
869 return false;
870 strcpy (copy, name);
871 section = bfd_make_section (abfd, copy);
874 /* Build a reloc pointing to this constructor. */
875 reloc_chain =
876 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
877 if (!reloc_chain)
878 return false;
879 reloc_chain->relent.sym_ptr_ptr =
880 bfd_get_section (asym)->symbol_ptr_ptr;
881 reloc_chain->relent.address = section->_raw_size;
882 reloc_chain->relent.addend = asym->value;
883 reloc_chain->relent.howto =
884 ecoff_backend (abfd)->constructor_reloc;
886 /* Set up the constructor section to hold the reloc. */
887 section->flags = SEC_CONSTRUCTOR;
888 ++section->reloc_count;
890 /* Constructor sections must be rounded to a boundary
891 based on the bitsize. These are not real sections--
892 they are handled specially by the linker--so the ECOFF
893 16 byte alignment restriction does not apply. */
894 bitsize = ecoff_backend (abfd)->constructor_bitsize;
895 section->alignment_power = 1;
896 while ((1 << section->alignment_power) < bitsize / 8)
897 ++section->alignment_power;
899 reloc_chain->next = section->constructor_chain;
900 section->constructor_chain = reloc_chain;
901 section->_raw_size += bitsize / 8;
903 #endif /* 0 */
905 /* Mark the symbol as a constructor. */
906 asym->flags |= BSF_CONSTRUCTOR;
908 break;
911 return true;
914 /* Read an ECOFF symbol table. */
916 boolean
917 _bfd_ecoff_slurp_symbol_table (abfd)
918 bfd *abfd;
920 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
921 const bfd_size_type external_ext_size
922 = backend->debug_swap.external_ext_size;
923 const bfd_size_type external_sym_size
924 = backend->debug_swap.external_sym_size;
925 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
926 = backend->debug_swap.swap_ext_in;
927 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
928 = backend->debug_swap.swap_sym_in;
929 bfd_size_type internal_size;
930 ecoff_symbol_type *internal;
931 ecoff_symbol_type *internal_ptr;
932 char *eraw_src;
933 char *eraw_end;
934 FDR *fdr_ptr;
935 FDR *fdr_end;
937 /* If we've already read in the symbol table, do nothing. */
938 if (ecoff_data (abfd)->canonical_symbols != NULL)
939 return true;
941 /* Get the symbolic information. */
942 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
943 &ecoff_data (abfd)->debug_info))
944 return false;
945 if (bfd_get_symcount (abfd) == 0)
946 return true;
948 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
949 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
950 if (internal == NULL)
951 return false;
953 internal_ptr = internal;
954 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
955 eraw_end = (eraw_src
956 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
957 * external_ext_size));
958 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
960 EXTR internal_esym;
962 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
963 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
964 + internal_esym.asym.iss);
965 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
966 &internal_ptr->symbol, 1,
967 internal_esym.weakext))
968 return false;
969 /* The alpha uses a negative ifd field for section symbols. */
970 if (internal_esym.ifd >= 0)
971 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
972 + internal_esym.ifd);
973 else
974 internal_ptr->fdr = NULL;
975 internal_ptr->local = false;
976 internal_ptr->native = (PTR) eraw_src;
979 /* The local symbols must be accessed via the fdr's, because the
980 string and aux indices are relative to the fdr information. */
981 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
982 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
983 for (; fdr_ptr < fdr_end; fdr_ptr++)
985 char *lraw_src;
986 char *lraw_end;
988 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
989 + fdr_ptr->isymBase * external_sym_size);
990 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
991 for (;
992 lraw_src < lraw_end;
993 lraw_src += external_sym_size, internal_ptr++)
995 SYMR internal_sym;
997 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
998 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
999 + fdr_ptr->issBase
1000 + internal_sym.iss);
1001 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1002 &internal_ptr->symbol, 0, 0))
1003 return false;
1004 internal_ptr->fdr = fdr_ptr;
1005 internal_ptr->local = true;
1006 internal_ptr->native = (PTR) lraw_src;
1010 ecoff_data (abfd)->canonical_symbols = internal;
1012 return true;
1015 /* Return the amount of space needed for the canonical symbols. */
1017 long
1018 _bfd_ecoff_get_symtab_upper_bound (abfd)
1019 bfd *abfd;
1021 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1022 &ecoff_data (abfd)->debug_info))
1023 return -1;
1025 if (bfd_get_symcount (abfd) == 0)
1026 return 0;
1028 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1031 /* Get the canonical symbols. */
1033 long
1034 _bfd_ecoff_get_symtab (abfd, alocation)
1035 bfd *abfd;
1036 asymbol **alocation;
1038 unsigned int counter = 0;
1039 ecoff_symbol_type *symbase;
1040 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1042 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1043 return -1;
1044 if (bfd_get_symcount (abfd) == 0)
1045 return 0;
1047 symbase = ecoff_data (abfd)->canonical_symbols;
1048 while (counter < bfd_get_symcount (abfd))
1050 *(location++) = symbase++;
1051 counter++;
1053 *location++ = (ecoff_symbol_type *) NULL;
1054 return bfd_get_symcount (abfd);
1057 /* Turn ECOFF type information into a printable string.
1058 ecoff_emit_aggregate and ecoff_type_to_string are from
1059 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1061 /* Write aggregate information to a string. */
1063 static void
1064 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1065 bfd *abfd;
1066 FDR *fdr;
1067 char *string;
1068 RNDXR *rndx;
1069 long isym;
1070 const char *which;
1072 const struct ecoff_debug_swap * const debug_swap =
1073 &ecoff_backend (abfd)->debug_swap;
1074 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1075 unsigned int ifd = rndx->rfd;
1076 unsigned int indx = rndx->index;
1077 const char *name;
1079 if (ifd == 0xfff)
1080 ifd = isym;
1082 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1083 struct return type of a procedure compiled without -g. */
1084 if (ifd == 0xffffffff
1085 || (rndx->rfd == 0xfff && indx == 0))
1086 name = "<undefined>";
1087 else if (indx == indexNil)
1088 name = "<no name>";
1089 else
1091 SYMR sym;
1093 if (debug_info->external_rfd == NULL)
1094 fdr = debug_info->fdr + ifd;
1095 else
1097 RFDT rfd;
1099 (*debug_swap->swap_rfd_in) (abfd,
1100 ((char *) debug_info->external_rfd
1101 + ((fdr->rfdBase + ifd)
1102 * debug_swap->external_rfd_size)),
1103 &rfd);
1104 fdr = debug_info->fdr + rfd;
1107 indx += fdr->isymBase;
1109 (*debug_swap->swap_sym_in) (abfd,
1110 ((char *) debug_info->external_sym
1111 + indx * debug_swap->external_sym_size),
1112 &sym);
1114 name = debug_info->ss + fdr->issBase + sym.iss;
1117 sprintf (string,
1118 "%s %s { ifd = %u, index = %lu }",
1119 which, name, ifd,
1120 ((long) indx
1121 + debug_info->symbolic_header.iextMax));
1124 /* Convert the type information to string format. */
1126 static char *
1127 ecoff_type_to_string (abfd, fdr, indx)
1128 bfd *abfd;
1129 FDR *fdr;
1130 unsigned int indx;
1132 union aux_ext *aux_ptr;
1133 int bigendian;
1134 AUXU u;
1135 struct qual {
1136 unsigned int type;
1137 int low_bound;
1138 int high_bound;
1139 int stride;
1140 } qualifiers[7];
1141 unsigned int basic_type;
1142 int i;
1143 char buffer1[1024];
1144 static char buffer2[1024];
1145 char *p1 = buffer1;
1146 char *p2 = buffer2;
1147 RNDXR rndx;
1149 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1150 bigendian = fdr->fBigendian;
1152 for (i = 0; i < 7; i++)
1154 qualifiers[i].low_bound = 0;
1155 qualifiers[i].high_bound = 0;
1156 qualifiers[i].stride = 0;
1159 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1160 return "-1 (no type)";
1161 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1163 basic_type = u.ti.bt;
1164 qualifiers[0].type = u.ti.tq0;
1165 qualifiers[1].type = u.ti.tq1;
1166 qualifiers[2].type = u.ti.tq2;
1167 qualifiers[3].type = u.ti.tq3;
1168 qualifiers[4].type = u.ti.tq4;
1169 qualifiers[5].type = u.ti.tq5;
1170 qualifiers[6].type = tqNil;
1173 * Go get the basic type.
1175 switch (basic_type)
1177 case btNil: /* undefined */
1178 strcpy (p1, "nil");
1179 break;
1181 case btAdr: /* address - integer same size as pointer */
1182 strcpy (p1, "address");
1183 break;
1185 case btChar: /* character */
1186 strcpy (p1, "char");
1187 break;
1189 case btUChar: /* unsigned character */
1190 strcpy (p1, "unsigned char");
1191 break;
1193 case btShort: /* short */
1194 strcpy (p1, "short");
1195 break;
1197 case btUShort: /* unsigned short */
1198 strcpy (p1, "unsigned short");
1199 break;
1201 case btInt: /* int */
1202 strcpy (p1, "int");
1203 break;
1205 case btUInt: /* unsigned int */
1206 strcpy (p1, "unsigned int");
1207 break;
1209 case btLong: /* long */
1210 strcpy (p1, "long");
1211 break;
1213 case btULong: /* unsigned long */
1214 strcpy (p1, "unsigned long");
1215 break;
1217 case btFloat: /* float (real) */
1218 strcpy (p1, "float");
1219 break;
1221 case btDouble: /* Double (real) */
1222 strcpy (p1, "double");
1223 break;
1225 /* Structures add 1-2 aux words:
1226 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1227 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1229 case btStruct: /* Structure (Record) */
1230 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1231 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1232 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1233 "struct");
1234 indx++; /* skip aux words */
1235 break;
1237 /* Unions add 1-2 aux words:
1238 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1239 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1241 case btUnion: /* Union */
1242 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1243 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1244 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1245 "union");
1246 indx++; /* skip aux words */
1247 break;
1249 /* Enumerations add 1-2 aux words:
1250 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1251 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1253 case btEnum: /* Enumeration */
1254 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1255 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1256 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1257 "enum");
1258 indx++; /* skip aux words */
1259 break;
1261 case btTypedef: /* defined via a typedef, isymRef points */
1262 strcpy (p1, "typedef");
1263 break;
1265 case btRange: /* subrange of int */
1266 strcpy (p1, "subrange");
1267 break;
1269 case btSet: /* pascal sets */
1270 strcpy (p1, "set");
1271 break;
1273 case btComplex: /* fortran complex */
1274 strcpy (p1, "complex");
1275 break;
1277 case btDComplex: /* fortran double complex */
1278 strcpy (p1, "double complex");
1279 break;
1281 case btIndirect: /* forward or unnamed typedef */
1282 strcpy (p1, "forward/unamed typedef");
1283 break;
1285 case btFixedDec: /* Fixed Decimal */
1286 strcpy (p1, "fixed decimal");
1287 break;
1289 case btFloatDec: /* Float Decimal */
1290 strcpy (p1, "float decimal");
1291 break;
1293 case btString: /* Varying Length Character String */
1294 strcpy (p1, "string");
1295 break;
1297 case btBit: /* Aligned Bit String */
1298 strcpy (p1, "bit");
1299 break;
1301 case btPicture: /* Picture */
1302 strcpy (p1, "picture");
1303 break;
1305 case btVoid: /* Void */
1306 strcpy (p1, "void");
1307 break;
1309 default:
1310 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1311 break;
1314 p1 += strlen (buffer1);
1317 * If this is a bitfield, get the bitsize.
1319 if (u.ti.fBitfield)
1321 int bitsize;
1323 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1324 sprintf (p1, " : %d", bitsize);
1325 p1 += strlen (buffer1);
1330 * Deal with any qualifiers.
1332 if (qualifiers[0].type != tqNil)
1335 * Snarf up any array bounds in the correct order. Arrays
1336 * store 5 successive words in the aux. table:
1337 * word 0 RNDXR to type of the bounds (ie, int)
1338 * word 1 Current file descriptor index
1339 * word 2 low bound
1340 * word 3 high bound (or -1 if [])
1341 * word 4 stride size in bits
1343 for (i = 0; i < 7; i++)
1345 if (qualifiers[i].type == tqArray)
1347 qualifiers[i].low_bound =
1348 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1349 qualifiers[i].high_bound =
1350 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1351 qualifiers[i].stride =
1352 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1353 indx += 5;
1358 * Now print out the qualifiers.
1360 for (i = 0; i < 6; i++)
1362 switch (qualifiers[i].type)
1364 case tqNil:
1365 case tqMax:
1366 break;
1368 case tqPtr:
1369 strcpy (p2, "ptr to ");
1370 p2 += sizeof ("ptr to ")-1;
1371 break;
1373 case tqVol:
1374 strcpy (p2, "volatile ");
1375 p2 += sizeof ("volatile ")-1;
1376 break;
1378 case tqFar:
1379 strcpy (p2, "far ");
1380 p2 += sizeof ("far ")-1;
1381 break;
1383 case tqProc:
1384 strcpy (p2, "func. ret. ");
1385 p2 += sizeof ("func. ret. ");
1386 break;
1388 case tqArray:
1390 int first_array = i;
1391 int j;
1393 /* Print array bounds reversed (ie, in the order the C
1394 programmer writes them). C is such a fun language.... */
1396 while (i < 5 && qualifiers[i+1].type == tqArray)
1397 i++;
1399 for (j = i; j >= first_array; j--)
1401 strcpy (p2, "array [");
1402 p2 += sizeof ("array [")-1;
1403 if (qualifiers[j].low_bound != 0)
1404 sprintf (p2,
1405 "%ld:%ld {%ld bits}",
1406 (long) qualifiers[j].low_bound,
1407 (long) qualifiers[j].high_bound,
1408 (long) qualifiers[j].stride);
1410 else if (qualifiers[j].high_bound != -1)
1411 sprintf (p2,
1412 "%ld {%ld bits}",
1413 (long) (qualifiers[j].high_bound + 1),
1414 (long) (qualifiers[j].stride));
1416 else
1417 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1419 p2 += strlen (p2);
1420 strcpy (p2, "] of ");
1421 p2 += sizeof ("] of ")-1;
1424 break;
1429 strcpy (p2, buffer1);
1430 return buffer2;
1433 /* Return information about ECOFF symbol SYMBOL in RET. */
1435 /*ARGSUSED*/
1436 void
1437 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1438 bfd *abfd; /* Ignored. */
1439 asymbol *symbol;
1440 symbol_info *ret;
1442 bfd_symbol_info (symbol, ret);
1445 /* Return whether this is a local label. */
1447 /*ARGSUSED*/
1448 boolean
1449 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1450 bfd *abfd;
1451 const char *name;
1453 return name[0] == '$';
1456 /* Print information about an ECOFF symbol. */
1458 void
1459 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1460 bfd *abfd;
1461 PTR filep;
1462 asymbol *symbol;
1463 bfd_print_symbol_type how;
1465 const struct ecoff_debug_swap * const debug_swap
1466 = &ecoff_backend (abfd)->debug_swap;
1467 FILE *file = (FILE *)filep;
1469 switch (how)
1471 case bfd_print_symbol_name:
1472 fprintf (file, "%s", symbol->name);
1473 break;
1474 case bfd_print_symbol_more:
1475 if (ecoffsymbol (symbol)->local)
1477 SYMR ecoff_sym;
1479 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1480 &ecoff_sym);
1481 fprintf (file, "ecoff local ");
1482 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1483 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1484 (unsigned) ecoff_sym.sc);
1486 else
1488 EXTR ecoff_ext;
1490 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1491 &ecoff_ext);
1492 fprintf (file, "ecoff extern ");
1493 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1494 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1495 (unsigned) ecoff_ext.asym.sc);
1497 break;
1498 case bfd_print_symbol_all:
1499 /* Print out the symbols in a reasonable way */
1501 char type;
1502 int pos;
1503 EXTR ecoff_ext;
1504 char jmptbl;
1505 char cobol_main;
1506 char weakext;
1508 if (ecoffsymbol (symbol)->local)
1510 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1511 &ecoff_ext.asym);
1512 type = 'l';
1513 pos = ((((char *) ecoffsymbol (symbol)->native
1514 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1515 / debug_swap->external_sym_size)
1516 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1517 jmptbl = ' ';
1518 cobol_main = ' ';
1519 weakext = ' ';
1521 else
1523 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1524 &ecoff_ext);
1525 type = 'e';
1526 pos = (((char *) ecoffsymbol (symbol)->native
1527 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1528 / debug_swap->external_ext_size);
1529 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1530 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1531 weakext = ecoff_ext.weakext ? 'w' : ' ';
1534 fprintf (file, "[%3d] %c ",
1535 pos, type);
1536 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1537 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1538 (unsigned) ecoff_ext.asym.st,
1539 (unsigned) ecoff_ext.asym.sc,
1540 (unsigned) ecoff_ext.asym.index,
1541 jmptbl, cobol_main, weakext,
1542 symbol->name);
1544 if (ecoffsymbol (symbol)->fdr != NULL
1545 && ecoff_ext.asym.index != indexNil)
1547 FDR *fdr;
1548 unsigned int indx;
1549 int bigendian;
1550 bfd_size_type sym_base;
1551 union aux_ext *aux_base;
1553 fdr = ecoffsymbol (symbol)->fdr;
1554 indx = ecoff_ext.asym.index;
1556 /* sym_base is used to map the fdr relative indices which
1557 appear in the file to the position number which we are
1558 using. */
1559 sym_base = fdr->isymBase;
1560 if (ecoffsymbol (symbol)->local)
1561 sym_base +=
1562 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1564 /* aux_base is the start of the aux entries for this file;
1565 asym.index is an offset from this. */
1566 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1567 + fdr->iauxBase);
1569 /* The aux entries are stored in host byte order; the
1570 order is indicated by a bit in the fdr. */
1571 bigendian = fdr->fBigendian;
1573 /* This switch is basically from gcc/mips-tdump.c */
1574 switch (ecoff_ext.asym.st)
1576 case stNil:
1577 case stLabel:
1578 break;
1580 case stFile:
1581 case stBlock:
1582 fprintf (file, "\n End+1 symbol: %ld",
1583 (long) (indx + sym_base));
1584 break;
1586 case stEnd:
1587 if (ecoff_ext.asym.sc == scText
1588 || ecoff_ext.asym.sc == scInfo)
1589 fprintf (file, "\n First symbol: %ld",
1590 (long) (indx + sym_base));
1591 else
1592 fprintf (file, "\n First symbol: %ld",
1593 ((long)
1594 (AUX_GET_ISYM (bigendian,
1595 &aux_base[ecoff_ext.asym.index])
1596 + sym_base)));
1597 break;
1599 case stProc:
1600 case stStaticProc:
1601 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1603 else if (ecoffsymbol (symbol)->local)
1604 fprintf (file, "\n End+1 symbol: %-7ld Type: %s",
1605 ((long)
1606 (AUX_GET_ISYM (bigendian,
1607 &aux_base[ecoff_ext.asym.index])
1608 + sym_base)),
1609 ecoff_type_to_string (abfd, fdr, indx + 1));
1610 else
1611 fprintf (file, "\n Local symbol: %ld",
1612 ((long) indx
1613 + (long) sym_base
1614 + (ecoff_data (abfd)
1615 ->debug_info.symbolic_header.iextMax)));
1616 break;
1618 case stStruct:
1619 fprintf (file, "\n struct; End+1 symbol: %ld",
1620 (long) (indx + sym_base));
1621 break;
1623 case stUnion:
1624 fprintf (file, "\n union; End+1 symbol: %ld",
1625 (long) (indx + sym_base));
1626 break;
1628 case stEnum:
1629 fprintf (file, "\n enum; End+1 symbol: %ld",
1630 (long) (indx + sym_base));
1631 break;
1633 default:
1634 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1635 fprintf (file, "\n Type: %s",
1636 ecoff_type_to_string (abfd, fdr, indx));
1637 break;
1641 break;
1645 /* Read in the relocs for a section. */
1647 static boolean
1648 ecoff_slurp_reloc_table (abfd, section, symbols)
1649 bfd *abfd;
1650 asection *section;
1651 asymbol **symbols;
1653 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1654 arelent *internal_relocs;
1655 bfd_size_type external_reloc_size;
1656 bfd_size_type external_relocs_size;
1657 char *external_relocs;
1658 arelent *rptr;
1659 unsigned int i;
1661 if (section->relocation != (arelent *) NULL
1662 || section->reloc_count == 0
1663 || (section->flags & SEC_CONSTRUCTOR) != 0)
1664 return true;
1666 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1667 return false;
1669 internal_relocs = (arelent *) bfd_alloc (abfd,
1670 (sizeof (arelent)
1671 * section->reloc_count));
1672 external_reloc_size = backend->external_reloc_size;
1673 external_relocs_size = external_reloc_size * section->reloc_count;
1674 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1675 if (internal_relocs == (arelent *) NULL
1676 || external_relocs == (char *) NULL)
1677 return false;
1678 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1679 return false;
1680 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1681 != external_relocs_size)
1682 return false;
1684 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1686 struct internal_reloc intern;
1688 (*backend->swap_reloc_in) (abfd,
1689 external_relocs + i * external_reloc_size,
1690 &intern);
1692 if (intern.r_extern)
1694 /* r_symndx is an index into the external symbols. */
1695 BFD_ASSERT (intern.r_symndx >= 0
1696 && (intern.r_symndx
1697 < (ecoff_data (abfd)
1698 ->debug_info.symbolic_header.iextMax)));
1699 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1700 rptr->addend = 0;
1702 else if (intern.r_symndx == RELOC_SECTION_NONE
1703 || intern.r_symndx == RELOC_SECTION_ABS)
1705 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1706 rptr->addend = 0;
1708 else
1710 CONST char *sec_name;
1711 asection *sec;
1713 /* r_symndx is a section key. */
1714 switch (intern.r_symndx)
1716 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1717 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1718 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1719 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1720 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1721 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1722 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1723 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1724 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1725 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1726 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1727 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1728 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1729 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1730 default: abort ();
1733 sec = bfd_get_section_by_name (abfd, sec_name);
1734 if (sec == (asection *) NULL)
1735 abort ();
1736 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1738 rptr->addend = - bfd_get_section_vma (abfd, sec);
1741 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1743 /* Let the backend select the howto field and do any other
1744 required processing. */
1745 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1748 bfd_release (abfd, external_relocs);
1750 section->relocation = internal_relocs;
1752 return true;
1755 /* Get a canonical list of relocs. */
1757 long
1758 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1759 bfd *abfd;
1760 asection *section;
1761 arelent **relptr;
1762 asymbol **symbols;
1764 unsigned int count;
1766 if (section->flags & SEC_CONSTRUCTOR)
1768 arelent_chain *chain;
1770 /* This section has relocs made up by us, not the file, so take
1771 them out of their chain and place them into the data area
1772 provided. */
1773 for (count = 0, chain = section->constructor_chain;
1774 count < section->reloc_count;
1775 count++, chain = chain->next)
1776 *relptr++ = &chain->relent;
1778 else
1780 arelent *tblptr;
1782 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1783 return -1;
1785 tblptr = section->relocation;
1787 for (count = 0; count < section->reloc_count; count++)
1788 *relptr++ = tblptr++;
1791 *relptr = (arelent *) NULL;
1793 return section->reloc_count;
1796 /* Provided a BFD, a section and an offset into the section, calculate
1797 and return the name of the source file and the line nearest to the
1798 wanted location. */
1800 /*ARGSUSED*/
1801 boolean
1802 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1803 filename_ptr, functionname_ptr, retline_ptr)
1804 bfd *abfd;
1805 asection *section;
1806 asymbol **ignore_symbols;
1807 bfd_vma offset;
1808 CONST char **filename_ptr;
1809 CONST char **functionname_ptr;
1810 unsigned int *retline_ptr;
1812 const struct ecoff_debug_swap * const debug_swap
1813 = &ecoff_backend (abfd)->debug_swap;
1814 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1815 struct ecoff_find_line *line_info;
1817 /* Make sure we have the FDR's. */
1818 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1819 || bfd_get_symcount (abfd) == 0)
1820 return false;
1822 if (ecoff_data (abfd)->find_line_info == NULL)
1824 ecoff_data (abfd)->find_line_info =
1825 ((struct ecoff_find_line *)
1826 bfd_zalloc (abfd, sizeof (struct ecoff_find_line)));
1827 if (ecoff_data (abfd)->find_line_info == NULL)
1828 return false;
1830 line_info = ecoff_data (abfd)->find_line_info;
1832 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1833 debug_swap, line_info, filename_ptr,
1834 functionname_ptr, retline_ptr);
1837 /* Copy private BFD data. This is called by objcopy and strip. We
1838 use it to copy the ECOFF debugging information from one BFD to the
1839 other. It would be theoretically possible to represent the ECOFF
1840 debugging information in the symbol table. However, it would be a
1841 lot of work, and there would be little gain (gas, gdb, and ld
1842 already access the ECOFF debugging information via the
1843 ecoff_debug_info structure, and that structure would have to be
1844 retained in order to support ECOFF debugging in MIPS ELF).
1846 The debugging information for the ECOFF external symbols comes from
1847 the symbol table, so this function only handles the other debugging
1848 information. */
1850 boolean
1851 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1852 bfd *ibfd;
1853 bfd *obfd;
1855 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1856 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1857 register int i;
1858 asymbol **sym_ptr_ptr;
1859 size_t c;
1860 boolean local;
1862 /* We only want to copy information over if both BFD's use ECOFF
1863 format. */
1864 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1865 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1866 return true;
1868 /* Copy the GP value and the register masks. */
1869 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1870 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1871 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1872 for (i = 0; i < 3; i++)
1873 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1875 /* Copy the version stamp. */
1876 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1878 /* If there are no symbols, don't copy any debugging information. */
1879 c = bfd_get_symcount (obfd);
1880 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1881 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1882 return true;
1884 /* See if there are any local symbols. */
1885 local = false;
1886 for (; c > 0; c--, sym_ptr_ptr++)
1888 if (ecoffsymbol (*sym_ptr_ptr)->local)
1890 local = true;
1891 break;
1895 if (local)
1897 /* There are some local symbols. We just bring over all the
1898 debugging information. FIXME: This is not quite the right
1899 thing to do. If the user has asked us to discard all
1900 debugging information, then we are probably going to wind up
1901 keeping it because there will probably be some local symbol
1902 which objcopy did not discard. We should actually break
1903 apart the debugging information and only keep that which
1904 applies to the symbols we want to keep. */
1905 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1906 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1907 oinfo->line = iinfo->line;
1909 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1910 oinfo->external_dnr = iinfo->external_dnr;
1912 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1913 oinfo->external_pdr = iinfo->external_pdr;
1915 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1916 oinfo->external_sym = iinfo->external_sym;
1918 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1919 oinfo->external_opt = iinfo->external_opt;
1921 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1922 oinfo->external_aux = iinfo->external_aux;
1924 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1925 oinfo->ss = iinfo->ss;
1927 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1928 oinfo->external_fdr = iinfo->external_fdr;
1930 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1931 oinfo->external_rfd = iinfo->external_rfd;
1933 else
1935 /* We are discarding all the local symbol information. Look
1936 through the external symbols and remove all references to FDR
1937 or aux information. */
1938 c = bfd_get_symcount (obfd);
1939 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1940 for (; c > 0; c--, sym_ptr_ptr++)
1942 EXTR esym;
1944 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1945 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1946 esym.ifd = ifdNil;
1947 esym.asym.index = indexNil;
1948 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1949 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1953 return true;
1956 /* Set the architecture. The supported architecture is stored in the
1957 backend pointer. We always set the architecture anyhow, since many
1958 callers ignore the return value. */
1960 boolean
1961 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1962 bfd *abfd;
1963 enum bfd_architecture arch;
1964 unsigned long machine;
1966 bfd_default_set_arch_mach (abfd, arch, machine);
1967 return arch == ecoff_backend (abfd)->arch;
1970 /* Get the size of the section headers. */
1972 /*ARGSUSED*/
1974 _bfd_ecoff_sizeof_headers (abfd, reloc)
1975 bfd *abfd;
1976 boolean reloc;
1978 asection *current;
1979 int c;
1980 int ret;
1982 c = 0;
1983 for (current = abfd->sections;
1984 current != (asection *)NULL;
1985 current = current->next)
1986 ++c;
1988 ret = (bfd_coff_filhsz (abfd)
1989 + bfd_coff_aoutsz (abfd)
1990 + c * bfd_coff_scnhsz (abfd));
1991 return BFD_ALIGN (ret, 16);
1994 /* Get the contents of a section. */
1996 boolean
1997 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
1998 bfd *abfd;
1999 asection *section;
2000 PTR location;
2001 file_ptr offset;
2002 bfd_size_type count;
2004 return _bfd_generic_get_section_contents (abfd, section, location,
2005 offset, count);
2008 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2009 called via qsort. */
2011 static int
2012 ecoff_sort_hdrs (arg1, arg2)
2013 const PTR arg1;
2014 const PTR arg2;
2016 const asection *hdr1 = *(const asection **) arg1;
2017 const asection *hdr2 = *(const asection **) arg2;
2019 if ((hdr1->flags & SEC_ALLOC) != 0)
2021 if ((hdr2->flags & SEC_ALLOC) == 0)
2022 return -1;
2024 else
2026 if ((hdr2->flags & SEC_ALLOC) != 0)
2027 return 1;
2029 if (hdr1->vma < hdr2->vma)
2030 return -1;
2031 else if (hdr1->vma > hdr2->vma)
2032 return 1;
2033 else
2034 return 0;
2037 /* Calculate the file position for each section, and set
2038 reloc_filepos. */
2040 static boolean
2041 ecoff_compute_section_file_positions (abfd)
2042 bfd *abfd;
2044 file_ptr sofar, file_sofar;
2045 asection **sorted_hdrs;
2046 asection *current;
2047 unsigned int i;
2048 file_ptr old_sofar;
2049 boolean rdata_in_text;
2050 boolean first_data, first_nonalloc;
2051 const bfd_vma round = ecoff_backend (abfd)->round;
2053 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2054 file_sofar = sofar;
2056 /* Sort the sections by VMA. */
2057 sorted_hdrs = (asection **) bfd_malloc (abfd->section_count
2058 * sizeof (asection *));
2059 if (sorted_hdrs == NULL)
2060 return false;
2061 for (current = abfd->sections, i = 0;
2062 current != NULL;
2063 current = current->next, i++)
2064 sorted_hdrs[i] = current;
2065 BFD_ASSERT (i == abfd->section_count);
2067 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2068 ecoff_sort_hdrs);
2070 /* Some versions of the OSF linker put the .rdata section in the
2071 text segment, and some do not. */
2072 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2073 if (rdata_in_text)
2075 for (i = 0; i < abfd->section_count; i++)
2077 current = sorted_hdrs[i];
2078 if (strcmp (current->name, _RDATA) == 0)
2079 break;
2080 if ((current->flags & SEC_CODE) == 0
2081 && strcmp (current->name, _PDATA) != 0
2082 && strcmp (current->name, _RCONST) != 0)
2084 rdata_in_text = false;
2085 break;
2089 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2091 first_data = true;
2092 first_nonalloc = true;
2093 for (i = 0; i < abfd->section_count; i++)
2095 unsigned int alignment_power;
2097 current = sorted_hdrs[i];
2099 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2100 supposed to indicate the number of .pdata entries that are
2101 really in the section. Each entry is 8 bytes. We store this
2102 away in line_filepos before increasing the section size. */
2103 if (strcmp (current->name, _PDATA) == 0)
2104 current->line_filepos = current->_raw_size / 8;
2106 alignment_power = current->alignment_power;
2108 /* On Ultrix, the data sections in an executable file must be
2109 aligned to a page boundary within the file. This does not
2110 affect the section size, though. FIXME: Does this work for
2111 other platforms? It requires some modification for the
2112 Alpha, because .rdata on the Alpha goes with the text, not
2113 the data. */
2114 if ((abfd->flags & EXEC_P) != 0
2115 && (abfd->flags & D_PAGED) != 0
2116 && ! first_data
2117 && (current->flags & SEC_CODE) == 0
2118 && (! rdata_in_text
2119 || strcmp (current->name, _RDATA) != 0)
2120 && strcmp (current->name, _PDATA) != 0
2121 && strcmp (current->name, _RCONST) != 0)
2123 sofar = (sofar + round - 1) &~ (round - 1);
2124 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2125 first_data = false;
2127 else if (strcmp (current->name, _LIB) == 0)
2129 /* On Irix 4, the location of contents of the .lib section
2130 from a shared library section is also rounded up to a
2131 page boundary. */
2133 sofar = (sofar + round - 1) &~ (round - 1);
2134 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2136 else if (first_nonalloc
2137 && (current->flags & SEC_ALLOC) == 0
2138 && (abfd->flags & D_PAGED) != 0)
2140 /* Skip up to the next page for an unallocated section, such
2141 as the .comment section on the Alpha. This leaves room
2142 for the .bss section. */
2143 first_nonalloc = false;
2144 sofar = (sofar + round - 1) &~ (round - 1);
2145 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2148 /* Align the sections in the file to the same boundary on
2149 which they are aligned in virtual memory. */
2150 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2151 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2152 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2154 if ((abfd->flags & D_PAGED) != 0
2155 && (current->flags & SEC_ALLOC) != 0)
2157 sofar += (current->vma - sofar) % round;
2158 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2159 file_sofar += (current->vma - file_sofar) % round;
2162 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2163 current->filepos = file_sofar;
2165 sofar += current->_raw_size;
2166 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2167 file_sofar += current->_raw_size;
2169 /* make sure that this section is of the right size too */
2170 old_sofar = sofar;
2171 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2172 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2173 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2174 current->_raw_size += sofar - old_sofar;
2177 free (sorted_hdrs);
2178 sorted_hdrs = NULL;
2180 ecoff_data (abfd)->reloc_filepos = file_sofar;
2182 return true;
2185 /* Determine the location of the relocs for all the sections in the
2186 output file, as well as the location of the symbolic debugging
2187 information. */
2189 static bfd_size_type
2190 ecoff_compute_reloc_file_positions (abfd)
2191 bfd *abfd;
2193 const bfd_size_type external_reloc_size =
2194 ecoff_backend (abfd)->external_reloc_size;
2195 file_ptr reloc_base;
2196 bfd_size_type reloc_size;
2197 asection *current;
2198 file_ptr sym_base;
2200 if (! abfd->output_has_begun)
2202 if (! ecoff_compute_section_file_positions (abfd))
2203 abort ();
2204 abfd->output_has_begun = true;
2207 reloc_base = ecoff_data (abfd)->reloc_filepos;
2209 reloc_size = 0;
2210 for (current = abfd->sections;
2211 current != (asection *)NULL;
2212 current = current->next)
2214 if (current->reloc_count == 0)
2215 current->rel_filepos = 0;
2216 else
2218 bfd_size_type relsize;
2220 current->rel_filepos = reloc_base;
2221 relsize = current->reloc_count * external_reloc_size;
2222 reloc_size += relsize;
2223 reloc_base += relsize;
2227 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2229 /* At least on Ultrix, the symbol table of an executable file must
2230 be aligned to a page boundary. FIXME: Is this true on other
2231 platforms? */
2232 if ((abfd->flags & EXEC_P) != 0
2233 && (abfd->flags & D_PAGED) != 0)
2234 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2235 &~ (ecoff_backend (abfd)->round - 1));
2237 ecoff_data (abfd)->sym_filepos = sym_base;
2239 return reloc_size;
2242 /* Set the contents of a section. */
2244 boolean
2245 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2246 bfd *abfd;
2247 asection *section;
2248 PTR location;
2249 file_ptr offset;
2250 bfd_size_type count;
2252 /* This must be done first, because bfd_set_section_contents is
2253 going to set output_has_begun to true. */
2254 if (abfd->output_has_begun == false)
2256 if (! ecoff_compute_section_file_positions (abfd))
2257 return false;
2260 /* Handle the .lib section specially so that Irix 4 shared libraries
2261 work out. See coff_set_section_contents in coffcode.h. */
2262 if (strcmp (section->name, _LIB) == 0)
2264 bfd_byte *rec, *recend;
2266 rec = (bfd_byte *) location;
2267 recend = rec + count;
2268 while (rec < recend)
2270 ++section->lma;
2271 rec += bfd_get_32 (abfd, rec) * 4;
2274 BFD_ASSERT (rec == recend);
2277 if (count == 0)
2278 return true;
2280 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2281 || bfd_write (location, 1, count, abfd) != count)
2282 return false;
2284 return true;
2287 /* Get the GP value for an ECOFF file. This is a hook used by
2288 nlmconv. */
2290 bfd_vma
2291 bfd_ecoff_get_gp_value (abfd)
2292 bfd *abfd;
2294 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2295 || bfd_get_format (abfd) != bfd_object)
2297 bfd_set_error (bfd_error_invalid_operation);
2298 return 0;
2301 return ecoff_data (abfd)->gp;
2304 /* Set the GP value for an ECOFF file. This is a hook used by the
2305 assembler. */
2307 boolean
2308 bfd_ecoff_set_gp_value (abfd, gp_value)
2309 bfd *abfd;
2310 bfd_vma gp_value;
2312 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2313 || bfd_get_format (abfd) != bfd_object)
2315 bfd_set_error (bfd_error_invalid_operation);
2316 return false;
2319 ecoff_data (abfd)->gp = gp_value;
2321 return true;
2324 /* Set the register masks for an ECOFF file. This is a hook used by
2325 the assembler. */
2327 boolean
2328 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2329 bfd *abfd;
2330 unsigned long gprmask;
2331 unsigned long fprmask;
2332 unsigned long *cprmask;
2334 ecoff_data_type *tdata;
2336 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2337 || bfd_get_format (abfd) != bfd_object)
2339 bfd_set_error (bfd_error_invalid_operation);
2340 return false;
2343 tdata = ecoff_data (abfd);
2344 tdata->gprmask = gprmask;
2345 tdata->fprmask = fprmask;
2346 if (cprmask != (unsigned long *) NULL)
2348 register int i;
2350 for (i = 0; i < 3; i++)
2351 tdata->cprmask[i] = cprmask[i];
2354 return true;
2357 /* Get ECOFF EXTR information for an external symbol. This function
2358 is passed to bfd_ecoff_debug_externals. */
2360 static boolean
2361 ecoff_get_extr (sym, esym)
2362 asymbol *sym;
2363 EXTR *esym;
2365 ecoff_symbol_type *ecoff_sym_ptr;
2366 bfd *input_bfd;
2368 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2369 || ecoffsymbol (sym)->native == NULL)
2371 /* Don't include debugging, local, or section symbols. */
2372 if ((sym->flags & BSF_DEBUGGING) != 0
2373 || (sym->flags & BSF_LOCAL) != 0
2374 || (sym->flags & BSF_SECTION_SYM) != 0)
2375 return false;
2377 esym->jmptbl = 0;
2378 esym->cobol_main = 0;
2379 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2380 esym->reserved = 0;
2381 esym->ifd = ifdNil;
2382 /* FIXME: we can do better than this for st and sc. */
2383 esym->asym.st = stGlobal;
2384 esym->asym.sc = scAbs;
2385 esym->asym.reserved = 0;
2386 esym->asym.index = indexNil;
2387 return true;
2390 ecoff_sym_ptr = ecoffsymbol (sym);
2392 if (ecoff_sym_ptr->local)
2393 return false;
2395 input_bfd = bfd_asymbol_bfd (sym);
2396 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2397 (input_bfd, ecoff_sym_ptr->native, esym);
2399 /* If the symbol was defined by the linker, then esym will be
2400 undefined but sym will not be. Get a better class for such a
2401 symbol. */
2402 if ((esym->asym.sc == scUndefined
2403 || esym->asym.sc == scSUndefined)
2404 && ! bfd_is_und_section (bfd_get_section (sym)))
2405 esym->asym.sc = scAbs;
2407 /* Adjust the FDR index for the symbol by that used for the input
2408 BFD. */
2409 if (esym->ifd != -1)
2411 struct ecoff_debug_info *input_debug;
2413 input_debug = &ecoff_data (input_bfd)->debug_info;
2414 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2415 if (input_debug->ifdmap != (RFDT *) NULL)
2416 esym->ifd = input_debug->ifdmap[esym->ifd];
2419 return true;
2422 /* Set the external symbol index. This routine is passed to
2423 bfd_ecoff_debug_externals. */
2425 static void
2426 ecoff_set_index (sym, indx)
2427 asymbol *sym;
2428 bfd_size_type indx;
2430 ecoff_set_sym_index (sym, indx);
2433 /* Write out an ECOFF file. */
2435 boolean
2436 _bfd_ecoff_write_object_contents (abfd)
2437 bfd *abfd;
2439 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2440 const bfd_vma round = backend->round;
2441 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2442 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2443 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2444 const bfd_size_type external_hdr_size
2445 = backend->debug_swap.external_hdr_size;
2446 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2447 void (* const adjust_reloc_out) PARAMS ((bfd *,
2448 const arelent *,
2449 struct internal_reloc *))
2450 = backend->adjust_reloc_out;
2451 void (* const swap_reloc_out) PARAMS ((bfd *,
2452 const struct internal_reloc *,
2453 PTR))
2454 = backend->swap_reloc_out;
2455 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2456 HDRR * const symhdr = &debug->symbolic_header;
2457 asection *current;
2458 unsigned int count;
2459 bfd_size_type reloc_size;
2460 bfd_size_type text_size;
2461 bfd_vma text_start;
2462 boolean set_text_start;
2463 bfd_size_type data_size;
2464 bfd_vma data_start;
2465 boolean set_data_start;
2466 bfd_size_type bss_size;
2467 PTR buff = NULL;
2468 PTR reloc_buff = NULL;
2469 struct internal_filehdr internal_f;
2470 struct internal_aouthdr internal_a;
2471 int i;
2473 /* Determine where the sections and relocs will go in the output
2474 file. */
2475 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2477 count = 1;
2478 for (current = abfd->sections;
2479 current != (asection *)NULL;
2480 current = current->next)
2482 current->target_index = count;
2483 ++count;
2486 if ((abfd->flags & D_PAGED) != 0)
2487 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2488 else
2489 text_size = 0;
2490 text_start = 0;
2491 set_text_start = false;
2492 data_size = 0;
2493 data_start = 0;
2494 set_data_start = false;
2495 bss_size = 0;
2497 /* Write section headers to the file. */
2499 /* Allocate buff big enough to hold a section header,
2500 file header, or a.out header. */
2502 bfd_size_type siz;
2503 siz = scnhsz;
2504 if (siz < filhsz)
2505 siz = filhsz;
2506 if (siz < aoutsz)
2507 siz = aoutsz;
2508 buff = (PTR) bfd_malloc ((size_t) siz);
2509 if (buff == NULL)
2510 goto error_return;
2513 internal_f.f_nscns = 0;
2514 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2515 goto error_return;
2516 for (current = abfd->sections;
2517 current != (asection *) NULL;
2518 current = current->next)
2520 struct internal_scnhdr section;
2521 bfd_vma vma;
2523 ++internal_f.f_nscns;
2525 strncpy (section.s_name, current->name, sizeof section.s_name);
2527 /* This seems to be correct for Irix 4 shared libraries. */
2528 vma = bfd_get_section_vma (abfd, current);
2529 if (strcmp (current->name, _LIB) == 0)
2530 section.s_vaddr = 0;
2531 else
2532 section.s_vaddr = vma;
2534 section.s_paddr = current->lma;
2535 section.s_size = bfd_get_section_size_before_reloc (current);
2537 /* If this section is unloadable then the scnptr will be 0. */
2538 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2539 section.s_scnptr = 0;
2540 else
2541 section.s_scnptr = current->filepos;
2542 section.s_relptr = current->rel_filepos;
2544 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2545 object file produced by the assembler is supposed to point to
2546 information about how much room is required by objects of
2547 various different sizes. I think this only matters if we
2548 want the linker to compute the best size to use, or
2549 something. I don't know what happens if the information is
2550 not present. */
2551 if (strcmp (current->name, _PDATA) != 0)
2552 section.s_lnnoptr = 0;
2553 else
2555 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2556 hold the number of entries in the section (each entry is
2557 8 bytes). We stored this in the line_filepos field in
2558 ecoff_compute_section_file_positions. */
2559 section.s_lnnoptr = current->line_filepos;
2562 section.s_nreloc = current->reloc_count;
2563 section.s_nlnno = 0;
2564 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2565 current->flags);
2567 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff) == 0
2568 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2569 goto error_return;
2571 if ((section.s_flags & STYP_TEXT) != 0
2572 || ((section.s_flags & STYP_RDATA) != 0
2573 && ecoff_data (abfd)->rdata_in_text)
2574 || section.s_flags == STYP_PDATA
2575 || (section.s_flags & STYP_DYNAMIC) != 0
2576 || (section.s_flags & STYP_LIBLIST) != 0
2577 || (section.s_flags & STYP_RELDYN) != 0
2578 || section.s_flags == STYP_CONFLIC
2579 || (section.s_flags & STYP_DYNSTR) != 0
2580 || (section.s_flags & STYP_DYNSYM) != 0
2581 || (section.s_flags & STYP_HASH) != 0
2582 || (section.s_flags & STYP_ECOFF_INIT) != 0
2583 || (section.s_flags & STYP_ECOFF_FINI) != 0
2584 || section.s_flags == STYP_RCONST)
2586 text_size += bfd_get_section_size_before_reloc (current);
2587 if (! set_text_start || text_start > vma)
2589 text_start = vma;
2590 set_text_start = true;
2593 else if ((section.s_flags & STYP_RDATA) != 0
2594 || (section.s_flags & STYP_DATA) != 0
2595 || (section.s_flags & STYP_LITA) != 0
2596 || (section.s_flags & STYP_LIT8) != 0
2597 || (section.s_flags & STYP_LIT4) != 0
2598 || (section.s_flags & STYP_SDATA) != 0
2599 || section.s_flags == STYP_XDATA
2600 || (section.s_flags & STYP_GOT) != 0)
2602 data_size += bfd_get_section_size_before_reloc (current);
2603 if (! set_data_start || data_start > vma)
2605 data_start = vma;
2606 set_data_start = true;
2609 else if ((section.s_flags & STYP_BSS) != 0
2610 || (section.s_flags & STYP_SBSS) != 0)
2611 bss_size += bfd_get_section_size_before_reloc (current);
2612 else if (section.s_flags == 0
2613 || (section.s_flags & STYP_ECOFF_LIB) != 0
2614 || section.s_flags == STYP_COMMENT)
2615 /* Do nothing */ ;
2616 else
2617 abort ();
2620 /* Set up the file header. */
2622 internal_f.f_magic = ecoff_get_magic (abfd);
2624 /* We will NOT put a fucking timestamp in the header here. Every
2625 time you put it back, I will come in and take it out again. I'm
2626 sorry. This field does not belong here. We fill it with a 0 so
2627 it compares the same but is not a reasonable time. --
2628 gnu@cygnus.com. */
2629 internal_f.f_timdat = 0;
2631 if (bfd_get_symcount (abfd) != 0)
2633 /* The ECOFF f_nsyms field is not actually the number of
2634 symbols, it's the size of symbolic information header. */
2635 internal_f.f_nsyms = external_hdr_size;
2636 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2638 else
2640 internal_f.f_nsyms = 0;
2641 internal_f.f_symptr = 0;
2644 internal_f.f_opthdr = aoutsz;
2646 internal_f.f_flags = F_LNNO;
2647 if (reloc_size == 0)
2648 internal_f.f_flags |= F_RELFLG;
2649 if (bfd_get_symcount (abfd) == 0)
2650 internal_f.f_flags |= F_LSYMS;
2651 if (abfd->flags & EXEC_P)
2652 internal_f.f_flags |= F_EXEC;
2654 if (bfd_little_endian (abfd))
2655 internal_f.f_flags |= F_AR32WR;
2656 else
2657 internal_f.f_flags |= F_AR32W;
2659 /* Set up the ``optional'' header. */
2660 if ((abfd->flags & D_PAGED) != 0)
2661 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2662 else
2663 internal_a.magic = ECOFF_AOUT_OMAGIC;
2665 /* FIXME: Is this really correct? */
2666 internal_a.vstamp = symhdr->vstamp;
2668 /* At least on Ultrix, these have to be rounded to page boundaries.
2669 FIXME: Is this true on other platforms? */
2670 if ((abfd->flags & D_PAGED) != 0)
2672 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2673 internal_a.text_start = text_start &~ (round - 1);
2674 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2675 internal_a.data_start = data_start &~ (round - 1);
2677 else
2679 internal_a.tsize = text_size;
2680 internal_a.text_start = text_start;
2681 internal_a.dsize = data_size;
2682 internal_a.data_start = data_start;
2685 /* On Ultrix, the initial portions of the .sbss and .bss segments
2686 are at the end of the data section. The bsize field in the
2687 optional header records how many bss bytes are required beyond
2688 those in the data section. The value is not rounded to a page
2689 boundary. */
2690 if (bss_size < internal_a.dsize - data_size)
2691 bss_size = 0;
2692 else
2693 bss_size -= internal_a.dsize - data_size;
2694 internal_a.bsize = bss_size;
2695 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2697 internal_a.entry = bfd_get_start_address (abfd);
2699 internal_a.gp_value = ecoff_data (abfd)->gp;
2701 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2702 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2703 for (i = 0; i < 4; i++)
2704 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2706 /* Let the backend adjust the headers if necessary. */
2707 if (backend->adjust_headers)
2709 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2710 goto error_return;
2713 /* Write out the file header and the optional header. */
2715 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2716 goto error_return;
2718 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2719 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2720 goto error_return;
2722 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2723 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2724 goto error_return;
2726 /* Build the external symbol information. This must be done before
2727 writing out the relocs so that we know the symbol indices. We
2728 don't do this if this BFD was created by the backend linker,
2729 since it will have already handled the symbols and relocs. */
2730 if (! ecoff_data (abfd)->linker)
2732 symhdr->iextMax = 0;
2733 symhdr->issExtMax = 0;
2734 debug->external_ext = debug->external_ext_end = NULL;
2735 debug->ssext = debug->ssext_end = NULL;
2736 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2737 (((abfd->flags & EXEC_P) == 0)
2738 ? true : false),
2739 ecoff_get_extr, ecoff_set_index)
2740 == false)
2741 goto error_return;
2743 /* Write out the relocs. */
2744 for (current = abfd->sections;
2745 current != (asection *) NULL;
2746 current = current->next)
2748 arelent **reloc_ptr_ptr;
2749 arelent **reloc_end;
2750 char *out_ptr;
2752 if (current->reloc_count == 0)
2753 continue;
2755 reloc_buff =
2756 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2757 if (reloc_buff == NULL)
2758 goto error_return;
2760 reloc_ptr_ptr = current->orelocation;
2761 reloc_end = reloc_ptr_ptr + current->reloc_count;
2762 out_ptr = (char *) reloc_buff;
2763 for (;
2764 reloc_ptr_ptr < reloc_end;
2765 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2767 arelent *reloc;
2768 asymbol *sym;
2769 struct internal_reloc in;
2771 memset ((PTR) &in, 0, sizeof in);
2773 reloc = *reloc_ptr_ptr;
2774 sym = *reloc->sym_ptr_ptr;
2776 in.r_vaddr = (reloc->address
2777 + bfd_get_section_vma (abfd, current));
2778 in.r_type = reloc->howto->type;
2780 if ((sym->flags & BSF_SECTION_SYM) == 0)
2782 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2783 in.r_extern = 1;
2785 else
2787 CONST char *name;
2789 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2790 if (strcmp (name, ".text") == 0)
2791 in.r_symndx = RELOC_SECTION_TEXT;
2792 else if (strcmp (name, ".rdata") == 0)
2793 in.r_symndx = RELOC_SECTION_RDATA;
2794 else if (strcmp (name, ".data") == 0)
2795 in.r_symndx = RELOC_SECTION_DATA;
2796 else if (strcmp (name, ".sdata") == 0)
2797 in.r_symndx = RELOC_SECTION_SDATA;
2798 else if (strcmp (name, ".sbss") == 0)
2799 in.r_symndx = RELOC_SECTION_SBSS;
2800 else if (strcmp (name, ".bss") == 0)
2801 in.r_symndx = RELOC_SECTION_BSS;
2802 else if (strcmp (name, ".init") == 0)
2803 in.r_symndx = RELOC_SECTION_INIT;
2804 else if (strcmp (name, ".lit8") == 0)
2805 in.r_symndx = RELOC_SECTION_LIT8;
2806 else if (strcmp (name, ".lit4") == 0)
2807 in.r_symndx = RELOC_SECTION_LIT4;
2808 else if (strcmp (name, ".xdata") == 0)
2809 in.r_symndx = RELOC_SECTION_XDATA;
2810 else if (strcmp (name, ".pdata") == 0)
2811 in.r_symndx = RELOC_SECTION_PDATA;
2812 else if (strcmp (name, ".fini") == 0)
2813 in.r_symndx = RELOC_SECTION_FINI;
2814 else if (strcmp (name, ".lita") == 0)
2815 in.r_symndx = RELOC_SECTION_LITA;
2816 else if (strcmp (name, "*ABS*") == 0)
2817 in.r_symndx = RELOC_SECTION_ABS;
2818 else if (strcmp (name, ".rconst") == 0)
2819 in.r_symndx = RELOC_SECTION_RCONST;
2820 else
2821 abort ();
2822 in.r_extern = 0;
2825 (*adjust_reloc_out) (abfd, reloc, &in);
2827 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2830 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2831 goto error_return;
2832 if (bfd_write (reloc_buff,
2833 external_reloc_size, current->reloc_count, abfd)
2834 != external_reloc_size * current->reloc_count)
2835 goto error_return;
2836 bfd_release (abfd, reloc_buff);
2837 reloc_buff = NULL;
2840 /* Write out the symbolic debugging information. */
2841 if (bfd_get_symcount (abfd) > 0)
2843 /* Write out the debugging information. */
2844 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2845 ecoff_data (abfd)->sym_filepos)
2846 == false)
2847 goto error_return;
2851 /* The .bss section of a demand paged executable must receive an
2852 entire page. If there are symbols, the symbols will start on the
2853 next page. If there are no symbols, we must fill out the page by
2854 hand. */
2855 if (bfd_get_symcount (abfd) == 0
2856 && (abfd->flags & EXEC_P) != 0
2857 && (abfd->flags & D_PAGED) != 0)
2859 char c;
2861 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2862 SEEK_SET) != 0)
2863 goto error_return;
2864 if (bfd_read (&c, 1, 1, abfd) == 0)
2865 c = 0;
2866 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2867 SEEK_SET) != 0)
2868 goto error_return;
2869 if (bfd_write (&c, 1, 1, abfd) != 1)
2870 goto error_return;
2873 if (reloc_buff != NULL)
2874 bfd_release (abfd, reloc_buff);
2875 if (buff != NULL)
2876 free (buff);
2877 return true;
2878 error_return:
2879 if (reloc_buff != NULL)
2880 bfd_release (abfd, reloc_buff);
2881 if (buff != NULL)
2882 free (buff);
2883 return false;
2886 /* Archive handling. ECOFF uses what appears to be a unique type of
2887 archive header (armap). The byte ordering of the armap and the
2888 contents are encoded in the name of the armap itself. At least for
2889 now, we only support archives with the same byte ordering in the
2890 armap and the contents.
2892 The first four bytes in the armap are the number of symbol
2893 definitions. This is always a power of two.
2895 This is followed by the symbol definitions. Each symbol definition
2896 occupies 8 bytes. The first four bytes are the offset from the
2897 start of the armap strings to the null-terminated string naming
2898 this symbol. The second four bytes are the file offset to the
2899 archive member which defines this symbol. If the second four bytes
2900 are 0, then this is not actually a symbol definition, and it should
2901 be ignored.
2903 The symbols are hashed into the armap with a closed hashing scheme.
2904 See the functions below for the details of the algorithm.
2906 After the symbol definitions comes four bytes holding the size of
2907 the string table, followed by the string table itself. */
2909 /* The name of an archive headers looks like this:
2910 __________E[BL]E[BL]_ (with a trailing space).
2911 The trailing space is changed to an X if the archive is changed to
2912 indicate that the armap is out of date.
2914 The Alpha seems to use ________64E[BL]E[BL]_. */
2916 #define ARMAP_BIG_ENDIAN 'B'
2917 #define ARMAP_LITTLE_ENDIAN 'L'
2918 #define ARMAP_MARKER 'E'
2919 #define ARMAP_START_LENGTH 10
2920 #define ARMAP_HEADER_MARKER_INDEX 10
2921 #define ARMAP_HEADER_ENDIAN_INDEX 11
2922 #define ARMAP_OBJECT_MARKER_INDEX 12
2923 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2924 #define ARMAP_END_INDEX 14
2925 #define ARMAP_END "_ "
2927 /* This is a magic number used in the hashing algorithm. */
2928 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2930 /* This returns the hash value to use for a string. It also sets
2931 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2932 is the number of entries in the hash table, and HLOG is the log
2933 base 2 of SIZE. */
2935 static unsigned int
2936 ecoff_armap_hash (s, rehash, size, hlog)
2937 CONST char *s;
2938 unsigned int *rehash;
2939 unsigned int size;
2940 unsigned int hlog;
2942 unsigned int hash;
2944 if (hlog == 0)
2945 return 0;
2946 hash = *s++;
2947 while (*s != '\0')
2948 hash = ((hash >> 27) | (hash << 5)) + *s++;
2949 hash *= ARMAP_HASH_MAGIC;
2950 *rehash = (hash & (size - 1)) | 1;
2951 return hash >> (32 - hlog);
2954 /* Read in the armap. */
2956 boolean
2957 _bfd_ecoff_slurp_armap (abfd)
2958 bfd *abfd;
2960 char nextname[17];
2961 unsigned int i;
2962 struct areltdata *mapdata;
2963 bfd_size_type parsed_size;
2964 char *raw_armap;
2965 struct artdata *ardata;
2966 unsigned int count;
2967 char *raw_ptr;
2968 struct symdef *symdef_ptr;
2969 char *stringbase;
2971 /* Get the name of the first element. */
2972 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2973 if (i == 0)
2974 return true;
2975 if (i != 16)
2976 return false;
2978 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2979 return false;
2981 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2982 standard COFF armap. We could move the ECOFF armap stuff into
2983 bfd_slurp_armap, but that seems inappropriate since no other
2984 target uses this format. Instead, we check directly for a COFF
2985 armap. */
2986 if (strncmp (nextname, "/ ", 16) == 0)
2987 return bfd_slurp_armap (abfd);
2989 /* See if the first element is an armap. */
2990 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2991 ARMAP_START_LENGTH) != 0
2992 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2993 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2994 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2995 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2996 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2997 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2998 || strncmp (nextname + ARMAP_END_INDEX,
2999 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3001 bfd_has_map (abfd) = false;
3002 return true;
3005 /* Make sure we have the right byte ordering. */
3006 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3007 ^ (bfd_header_big_endian (abfd)))
3008 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3009 ^ (bfd_big_endian (abfd))))
3011 bfd_set_error (bfd_error_wrong_format);
3012 return false;
3015 /* Read in the armap. */
3016 ardata = bfd_ardata (abfd);
3017 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
3018 if (mapdata == (struct areltdata *) NULL)
3019 return false;
3020 parsed_size = mapdata->parsed_size;
3021 bfd_release (abfd, (PTR) mapdata);
3023 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3024 if (raw_armap == (char *) NULL)
3025 return false;
3027 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3029 if (bfd_get_error () != bfd_error_system_call)
3030 bfd_set_error (bfd_error_malformed_archive);
3031 bfd_release (abfd, (PTR) raw_armap);
3032 return false;
3035 ardata->tdata = (PTR) raw_armap;
3037 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3039 ardata->symdef_count = 0;
3040 ardata->cache = (struct ar_cache *) NULL;
3042 /* This code used to overlay the symdefs over the raw archive data,
3043 but that doesn't work on a 64 bit host. */
3045 stringbase = raw_armap + count * 8 + 8;
3047 #ifdef CHECK_ARMAP_HASH
3049 unsigned int hlog;
3051 /* Double check that I have the hashing algorithm right by making
3052 sure that every symbol can be looked up successfully. */
3053 hlog = 0;
3054 for (i = 1; i < count; i <<= 1)
3055 hlog++;
3056 BFD_ASSERT (i == count);
3058 raw_ptr = raw_armap + 4;
3059 for (i = 0; i < count; i++, raw_ptr += 8)
3061 unsigned int name_offset, file_offset;
3062 unsigned int hash, rehash, srch;
3064 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3065 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3066 if (file_offset == 0)
3067 continue;
3068 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3069 hlog);
3070 if (hash == i)
3071 continue;
3073 /* See if we can rehash to this location. */
3074 for (srch = (hash + rehash) & (count - 1);
3075 srch != hash && srch != i;
3076 srch = (srch + rehash) & (count - 1))
3077 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3078 != 0);
3079 BFD_ASSERT (srch == i);
3083 #endif /* CHECK_ARMAP_HASH */
3085 raw_ptr = raw_armap + 4;
3086 for (i = 0; i < count; i++, raw_ptr += 8)
3087 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3088 ++ardata->symdef_count;
3090 symdef_ptr = ((struct symdef *)
3091 bfd_alloc (abfd,
3092 ardata->symdef_count * sizeof (struct symdef)));
3093 if (!symdef_ptr)
3094 return false;
3096 ardata->symdefs = (carsym *) symdef_ptr;
3098 raw_ptr = raw_armap + 4;
3099 for (i = 0; i < count; i++, raw_ptr += 8)
3101 unsigned int name_offset, file_offset;
3103 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3104 if (file_offset == 0)
3105 continue;
3106 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3107 symdef_ptr->s.name = stringbase + name_offset;
3108 symdef_ptr->file_offset = file_offset;
3109 ++symdef_ptr;
3112 ardata->first_file_filepos = bfd_tell (abfd);
3113 /* Pad to an even boundary. */
3114 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3116 bfd_has_map (abfd) = true;
3118 return true;
3121 /* Write out an armap. */
3123 boolean
3124 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3125 bfd *abfd;
3126 unsigned int elength;
3127 struct orl *map;
3128 unsigned int orl_count;
3129 int stridx;
3131 unsigned int hashsize, hashlog;
3132 unsigned int symdefsize;
3133 int padit;
3134 unsigned int stringsize;
3135 unsigned int mapsize;
3136 file_ptr firstreal;
3137 struct ar_hdr hdr;
3138 struct stat statbuf;
3139 unsigned int i;
3140 bfd_byte temp[4];
3141 bfd_byte *hashtable;
3142 bfd *current;
3143 bfd *last_elt;
3145 /* Ultrix appears to use as a hash table size the least power of two
3146 greater than twice the number of entries. */
3147 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3149 hashsize = 1 << hashlog;
3151 symdefsize = hashsize * 8;
3152 padit = stridx % 2;
3153 stringsize = stridx + padit;
3155 /* Include 8 bytes to store symdefsize and stringsize in output. */
3156 mapsize = symdefsize + stringsize + 8;
3158 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3160 memset ((PTR) &hdr, 0, sizeof hdr);
3162 /* Work out the ECOFF armap name. */
3163 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3164 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3165 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3166 (bfd_header_big_endian (abfd)
3167 ? ARMAP_BIG_ENDIAN
3168 : ARMAP_LITTLE_ENDIAN);
3169 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3170 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3171 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3172 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3174 /* Write the timestamp of the archive header to be just a little bit
3175 later than the timestamp of the file, otherwise the linker will
3176 complain that the index is out of date. Actually, the Ultrix
3177 linker just checks the archive name; the GNU linker may check the
3178 date. */
3179 stat (abfd->filename, &statbuf);
3180 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3182 /* The DECstation uses zeroes for the uid, gid and mode of the
3183 armap. */
3184 hdr.ar_uid[0] = '0';
3185 hdr.ar_gid[0] = '0';
3186 hdr.ar_mode[0] = '0';
3188 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3190 hdr.ar_fmag[0] = '`';
3191 hdr.ar_fmag[1] = '\012';
3193 /* Turn all null bytes in the header into spaces. */
3194 for (i = 0; i < sizeof (struct ar_hdr); i++)
3195 if (((char *)(&hdr))[i] == '\0')
3196 (((char *)(&hdr))[i]) = ' ';
3198 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3199 != sizeof (struct ar_hdr))
3200 return false;
3202 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3203 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3204 return false;
3206 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3207 if (!hashtable)
3208 return false;
3210 current = abfd->archive_head;
3211 last_elt = current;
3212 for (i = 0; i < orl_count; i++)
3214 unsigned int hash, rehash;
3216 /* Advance firstreal to the file position of this archive
3217 element. */
3218 if (((bfd *) map[i].pos) != last_elt)
3222 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3223 firstreal += firstreal % 2;
3224 current = current->next;
3226 while (current != (bfd *) map[i].pos);
3229 last_elt = current;
3231 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3232 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3234 unsigned int srch;
3236 /* The desired slot is already taken. */
3237 for (srch = (hash + rehash) & (hashsize - 1);
3238 srch != hash;
3239 srch = (srch + rehash) & (hashsize - 1))
3240 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3241 break;
3243 BFD_ASSERT (srch != hash);
3245 hash = srch;
3248 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3249 (PTR) (hashtable + hash * 8));
3250 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3251 (PTR) (hashtable + hash * 8 + 4));
3254 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3255 return false;
3257 bfd_release (abfd, hashtable);
3259 /* Now write the strings. */
3260 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3261 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3262 return false;
3263 for (i = 0; i < orl_count; i++)
3265 bfd_size_type len;
3267 len = strlen (*map[i].name) + 1;
3268 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3269 return false;
3272 /* The spec sez this should be a newline. But in order to be
3273 bug-compatible for DECstation ar we use a null. */
3274 if (padit)
3276 if (bfd_write ("", 1, 1, abfd) != 1)
3277 return false;
3280 return true;
3283 /* See whether this BFD is an archive. If it is, read in the armap
3284 and the extended name table. */
3286 const bfd_target *
3287 _bfd_ecoff_archive_p (abfd)
3288 bfd *abfd;
3290 struct artdata *tdata_hold;
3291 char armag[SARMAG + 1];
3293 tdata_hold = abfd->tdata.aout_ar_data;
3295 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
3297 if (bfd_get_error () != bfd_error_system_call)
3298 bfd_set_error (bfd_error_wrong_format);
3299 return (const bfd_target *) NULL;
3302 if (strncmp (armag, ARMAG, SARMAG) != 0)
3304 bfd_set_error (bfd_error_wrong_format);
3305 return NULL;
3308 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3309 involves a cast, we can't do it as the left operand of
3310 assignment. */
3311 abfd->tdata.aout_ar_data =
3312 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3314 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3316 abfd->tdata.aout_ar_data = tdata_hold;
3317 return (const bfd_target *) NULL;
3320 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3321 bfd_ardata (abfd)->cache = NULL;
3322 bfd_ardata (abfd)->archive_head = NULL;
3323 bfd_ardata (abfd)->symdefs = NULL;
3324 bfd_ardata (abfd)->extended_names = NULL;
3325 bfd_ardata (abfd)->tdata = NULL;
3327 if (_bfd_ecoff_slurp_armap (abfd) == false
3328 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3330 bfd_release (abfd, bfd_ardata (abfd));
3331 abfd->tdata.aout_ar_data = tdata_hold;
3332 return (const bfd_target *) NULL;
3335 if (bfd_has_map (abfd))
3337 bfd *first;
3339 /* This archive has a map, so we may presume that the contents
3340 are object files. Make sure that if the first file in the
3341 archive can be recognized as an object file, it is for this
3342 target. If not, assume that this is the wrong format. If
3343 the first file is not an object file, somebody is doing
3344 something weird, and we permit it so that ar -t will work. */
3346 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3347 if (first != NULL)
3349 boolean fail;
3351 first->target_defaulted = false;
3352 fail = false;
3353 if (bfd_check_format (first, bfd_object)
3354 && first->xvec != abfd->xvec)
3356 (void) bfd_close (first);
3357 bfd_release (abfd, bfd_ardata (abfd));
3358 abfd->tdata.aout_ar_data = tdata_hold;
3359 bfd_set_error (bfd_error_wrong_format);
3360 return NULL;
3363 /* We ought to close first here, but we can't, because we
3364 have no way to remove it from the archive cache. FIXME. */
3368 return abfd->xvec;
3371 /* ECOFF linker code. */
3373 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3374 PARAMS ((struct bfd_hash_entry *entry,
3375 struct bfd_hash_table *table,
3376 const char *string));
3377 static boolean ecoff_link_add_archive_symbols
3378 PARAMS ((bfd *, struct bfd_link_info *));
3379 static boolean ecoff_link_check_archive_element
3380 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3381 static boolean ecoff_link_add_object_symbols
3382 PARAMS ((bfd *, struct bfd_link_info *));
3383 static boolean ecoff_link_add_externals
3384 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3386 /* Routine to create an entry in an ECOFF link hash table. */
3388 static struct bfd_hash_entry *
3389 ecoff_link_hash_newfunc (entry, table, string)
3390 struct bfd_hash_entry *entry;
3391 struct bfd_hash_table *table;
3392 const char *string;
3394 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3396 /* Allocate the structure if it has not already been allocated by a
3397 subclass. */
3398 if (ret == (struct ecoff_link_hash_entry *) NULL)
3399 ret = ((struct ecoff_link_hash_entry *)
3400 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3401 if (ret == (struct ecoff_link_hash_entry *) NULL)
3402 return NULL;
3404 /* Call the allocation method of the superclass. */
3405 ret = ((struct ecoff_link_hash_entry *)
3406 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3407 table, string));
3409 if (ret)
3411 /* Set local fields. */
3412 ret->indx = -1;
3413 ret->abfd = NULL;
3414 ret->written = 0;
3415 ret->small = 0;
3417 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3419 return (struct bfd_hash_entry *) ret;
3422 /* Create an ECOFF link hash table. */
3424 struct bfd_link_hash_table *
3425 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3426 bfd *abfd;
3428 struct ecoff_link_hash_table *ret;
3430 ret = ((struct ecoff_link_hash_table *)
3431 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3432 if (ret == NULL)
3433 return NULL;
3434 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3435 ecoff_link_hash_newfunc))
3437 free (ret);
3438 return (struct bfd_link_hash_table *) NULL;
3440 return &ret->root;
3443 /* Look up an entry in an ECOFF link hash table. */
3445 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3446 ((struct ecoff_link_hash_entry *) \
3447 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3449 /* Traverse an ECOFF link hash table. */
3451 #define ecoff_link_hash_traverse(table, func, info) \
3452 (bfd_link_hash_traverse \
3453 (&(table)->root, \
3454 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3455 (info)))
3457 /* Get the ECOFF link hash table from the info structure. This is
3458 just a cast. */
3460 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3462 /* Given an ECOFF BFD, add symbols to the global hash table as
3463 appropriate. */
3465 boolean
3466 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3467 bfd *abfd;
3468 struct bfd_link_info *info;
3470 switch (bfd_get_format (abfd))
3472 case bfd_object:
3473 return ecoff_link_add_object_symbols (abfd, info);
3474 case bfd_archive:
3475 return ecoff_link_add_archive_symbols (abfd, info);
3476 default:
3477 bfd_set_error (bfd_error_wrong_format);
3478 return false;
3482 /* Add the symbols from an archive file to the global hash table.
3483 This looks through the undefined symbols, looks each one up in the
3484 archive hash table, and adds any associated object file. We do not
3485 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3486 already have a hash table, so there is no reason to construct
3487 another one. */
3489 static boolean
3490 ecoff_link_add_archive_symbols (abfd, info)
3491 bfd *abfd;
3492 struct bfd_link_info *info;
3494 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3495 const bfd_byte *raw_armap;
3496 struct bfd_link_hash_entry **pundef;
3497 unsigned int armap_count;
3498 unsigned int armap_log;
3499 unsigned int i;
3500 const bfd_byte *hashtable;
3501 const char *stringbase;
3503 if (! bfd_has_map (abfd))
3505 /* An empty archive is a special case. */
3506 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3507 return true;
3508 bfd_set_error (bfd_error_no_armap);
3509 return false;
3512 /* If we don't have any raw data for this archive, as can happen on
3513 Irix 4.0.5F, we call the generic routine.
3514 FIXME: We should be more clever about this, since someday tdata
3515 may get to something for a generic archive. */
3516 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3517 if (raw_armap == (bfd_byte *) NULL)
3518 return (_bfd_generic_link_add_archive_symbols
3519 (abfd, info, ecoff_link_check_archive_element));
3521 armap_count = bfd_h_get_32 (abfd, raw_armap);
3523 armap_log = 0;
3524 for (i = 1; i < armap_count; i <<= 1)
3525 armap_log++;
3526 BFD_ASSERT (i == armap_count);
3528 hashtable = raw_armap + 4;
3529 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3531 /* Look through the list of undefined symbols. */
3532 pundef = &info->hash->undefs;
3533 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3535 struct bfd_link_hash_entry *h;
3536 unsigned int hash, rehash;
3537 unsigned int file_offset;
3538 const char *name;
3539 bfd *element;
3541 h = *pundef;
3543 /* When a symbol is defined, it is not necessarily removed from
3544 the list. */
3545 if (h->type != bfd_link_hash_undefined
3546 && h->type != bfd_link_hash_common)
3548 /* Remove this entry from the list, for general cleanliness
3549 and because we are going to look through the list again
3550 if we search any more libraries. We can't remove the
3551 entry if it is the tail, because that would lose any
3552 entries we add to the list later on. */
3553 if (*pundef != info->hash->undefs_tail)
3554 *pundef = (*pundef)->next;
3555 else
3556 pundef = &(*pundef)->next;
3557 continue;
3560 /* Native ECOFF linkers do not pull in archive elements merely
3561 to satisfy common definitions, so neither do we. We leave
3562 them on the list, though, in case we are linking against some
3563 other object format. */
3564 if (h->type != bfd_link_hash_undefined)
3566 pundef = &(*pundef)->next;
3567 continue;
3570 /* Look for this symbol in the archive hash table. */
3571 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3572 armap_log);
3574 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3575 if (file_offset == 0)
3577 /* Nothing in this slot. */
3578 pundef = &(*pundef)->next;
3579 continue;
3582 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3583 if (name[0] != h->root.string[0]
3584 || strcmp (name, h->root.string) != 0)
3586 unsigned int srch;
3587 boolean found;
3589 /* That was the wrong symbol. Try rehashing. */
3590 found = false;
3591 for (srch = (hash + rehash) & (armap_count - 1);
3592 srch != hash;
3593 srch = (srch + rehash) & (armap_count - 1))
3595 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3596 if (file_offset == 0)
3597 break;
3598 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3599 if (name[0] == h->root.string[0]
3600 && strcmp (name, h->root.string) == 0)
3602 found = true;
3603 break;
3607 if (! found)
3609 pundef = &(*pundef)->next;
3610 continue;
3613 hash = srch;
3616 element = (*backend->get_elt_at_filepos) (abfd, file_offset);
3617 if (element == (bfd *) NULL)
3618 return false;
3620 if (! bfd_check_format (element, bfd_object))
3621 return false;
3623 /* Unlike the generic linker, we know that this element provides
3624 a definition for an undefined symbol and we know that we want
3625 to include it. We don't need to check anything. */
3626 if (! (*info->callbacks->add_archive_element) (info, element, name))
3627 return false;
3628 if (! ecoff_link_add_object_symbols (element, info))
3629 return false;
3631 pundef = &(*pundef)->next;
3634 return true;
3637 /* This is called if we used _bfd_generic_link_add_archive_symbols
3638 because we were not dealing with an ECOFF archive. */
3640 static boolean
3641 ecoff_link_check_archive_element (abfd, info, pneeded)
3642 bfd *abfd;
3643 struct bfd_link_info *info;
3644 boolean *pneeded;
3646 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3647 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3648 = backend->debug_swap.swap_ext_in;
3649 HDRR *symhdr;
3650 bfd_size_type external_ext_size;
3651 PTR external_ext = NULL;
3652 size_t esize;
3653 char *ssext = NULL;
3654 char *ext_ptr;
3655 char *ext_end;
3657 *pneeded = false;
3659 if (! ecoff_slurp_symbolic_header (abfd))
3660 goto error_return;
3662 /* If there are no symbols, we don't want it. */
3663 if (bfd_get_symcount (abfd) == 0)
3664 goto successful_return;
3666 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3668 /* Read in the external symbols and external strings. */
3669 external_ext_size = backend->debug_swap.external_ext_size;
3670 esize = symhdr->iextMax * external_ext_size;
3671 external_ext = (PTR) bfd_malloc (esize);
3672 if (external_ext == NULL && esize != 0)
3673 goto error_return;
3675 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3676 || bfd_read (external_ext, 1, esize, abfd) != esize)
3677 goto error_return;
3679 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3680 if (ssext == NULL && symhdr->issExtMax != 0)
3681 goto error_return;
3683 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3684 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3685 (bfd_size_type) symhdr->issExtMax))
3686 goto error_return;
3688 /* Look through the external symbols to see if they define some
3689 symbol that is currently undefined. */
3690 ext_ptr = (char *) external_ext;
3691 ext_end = ext_ptr + esize;
3692 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3694 EXTR esym;
3695 boolean def;
3696 const char *name;
3697 struct bfd_link_hash_entry *h;
3699 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3701 /* See if this symbol defines something. */
3702 if (esym.asym.st != stGlobal
3703 && esym.asym.st != stLabel
3704 && esym.asym.st != stProc)
3705 continue;
3707 switch (esym.asym.sc)
3709 case scText:
3710 case scData:
3711 case scBss:
3712 case scAbs:
3713 case scSData:
3714 case scSBss:
3715 case scRData:
3716 case scCommon:
3717 case scSCommon:
3718 case scInit:
3719 case scFini:
3720 case scRConst:
3721 def = true;
3722 break;
3723 default:
3724 def = false;
3725 break;
3728 if (! def)
3729 continue;
3731 name = ssext + esym.asym.iss;
3732 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3734 /* Unlike the generic linker, we do not pull in elements because
3735 of common symbols. */
3736 if (h == (struct bfd_link_hash_entry *) NULL
3737 || h->type != bfd_link_hash_undefined)
3738 continue;
3740 /* Include this element. */
3741 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3742 goto error_return;
3743 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3744 goto error_return;
3746 *pneeded = true;
3747 goto successful_return;
3750 successful_return:
3751 if (external_ext != NULL)
3752 free (external_ext);
3753 if (ssext != NULL)
3754 free (ssext);
3755 return true;
3756 error_return:
3757 if (external_ext != NULL)
3758 free (external_ext);
3759 if (ssext != NULL)
3760 free (ssext);
3761 return false;
3764 /* Add symbols from an ECOFF object file to the global linker hash
3765 table. */
3767 static boolean
3768 ecoff_link_add_object_symbols (abfd, info)
3769 bfd *abfd;
3770 struct bfd_link_info *info;
3772 HDRR *symhdr;
3773 bfd_size_type external_ext_size;
3774 PTR external_ext = NULL;
3775 size_t esize;
3776 char *ssext = NULL;
3777 boolean result;
3779 if (! ecoff_slurp_symbolic_header (abfd))
3780 return false;
3782 /* If there are no symbols, we don't want it. */
3783 if (bfd_get_symcount (abfd) == 0)
3784 return true;
3786 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3788 /* Read in the external symbols and external strings. */
3789 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3790 esize = symhdr->iextMax * external_ext_size;
3791 external_ext = (PTR) bfd_malloc (esize);
3792 if (external_ext == NULL && esize != 0)
3793 goto error_return;
3795 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3796 || bfd_read (external_ext, 1, esize, abfd) != esize)
3797 goto error_return;
3799 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3800 if (ssext == NULL && symhdr->issExtMax != 0)
3801 goto error_return;
3803 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3804 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3805 != (bfd_size_type) symhdr->issExtMax))
3806 goto error_return;
3808 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3810 if (ssext != NULL)
3811 free (ssext);
3812 if (external_ext != NULL)
3813 free (external_ext);
3814 return result;
3816 error_return:
3817 if (ssext != NULL)
3818 free (ssext);
3819 if (external_ext != NULL)
3820 free (external_ext);
3821 return false;
3824 /* Add the external symbols of an object file to the global linker
3825 hash table. The external symbols and strings we are passed are
3826 just allocated on the stack, and will be discarded. We must
3827 explicitly save any information we may need later on in the link.
3828 We do not want to read the external symbol information again. */
3830 static boolean
3831 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3832 bfd *abfd;
3833 struct bfd_link_info *info;
3834 PTR external_ext;
3835 char *ssext;
3837 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3838 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3839 = backend->debug_swap.swap_ext_in;
3840 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3841 unsigned long ext_count;
3842 struct ecoff_link_hash_entry **sym_hash;
3843 char *ext_ptr;
3844 char *ext_end;
3846 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3848 sym_hash = ((struct ecoff_link_hash_entry **)
3849 bfd_alloc (abfd,
3850 ext_count * sizeof (struct bfd_link_hash_entry *)));
3851 if (!sym_hash)
3852 return false;
3853 ecoff_data (abfd)->sym_hashes = sym_hash;
3855 ext_ptr = (char *) external_ext;
3856 ext_end = ext_ptr + ext_count * external_ext_size;
3857 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3859 EXTR esym;
3860 boolean skip;
3861 bfd_vma value;
3862 asection *section;
3863 const char *name;
3864 struct ecoff_link_hash_entry *h;
3866 *sym_hash = NULL;
3868 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3870 /* Skip debugging symbols. */
3871 skip = false;
3872 switch (esym.asym.st)
3874 case stGlobal:
3875 case stStatic:
3876 case stLabel:
3877 case stProc:
3878 case stStaticProc:
3879 break;
3880 default:
3881 skip = true;
3882 break;
3885 if (skip)
3886 continue;
3888 /* Get the information for this symbol. */
3889 value = esym.asym.value;
3890 switch (esym.asym.sc)
3892 default:
3893 case scNil:
3894 case scRegister:
3895 case scCdbLocal:
3896 case scBits:
3897 case scCdbSystem:
3898 case scRegImage:
3899 case scInfo:
3900 case scUserStruct:
3901 case scVar:
3902 case scVarRegister:
3903 case scVariant:
3904 case scBasedVar:
3905 case scXData:
3906 case scPData:
3907 section = NULL;
3908 break;
3909 case scText:
3910 section = bfd_make_section_old_way (abfd, ".text");
3911 value -= section->vma;
3912 break;
3913 case scData:
3914 section = bfd_make_section_old_way (abfd, ".data");
3915 value -= section->vma;
3916 break;
3917 case scBss:
3918 section = bfd_make_section_old_way (abfd, ".bss");
3919 value -= section->vma;
3920 break;
3921 case scAbs:
3922 section = bfd_abs_section_ptr;
3923 break;
3924 case scUndefined:
3925 section = bfd_und_section_ptr;
3926 break;
3927 case scSData:
3928 section = bfd_make_section_old_way (abfd, ".sdata");
3929 value -= section->vma;
3930 break;
3931 case scSBss:
3932 section = bfd_make_section_old_way (abfd, ".sbss");
3933 value -= section->vma;
3934 break;
3935 case scRData:
3936 section = bfd_make_section_old_way (abfd, ".rdata");
3937 value -= section->vma;
3938 break;
3939 case scCommon:
3940 if (value > ecoff_data (abfd)->gp_size)
3942 section = bfd_com_section_ptr;
3943 break;
3945 /* Fall through. */
3946 case scSCommon:
3947 if (ecoff_scom_section.name == NULL)
3949 /* Initialize the small common section. */
3950 ecoff_scom_section.name = SCOMMON;
3951 ecoff_scom_section.flags = SEC_IS_COMMON;
3952 ecoff_scom_section.output_section = &ecoff_scom_section;
3953 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3954 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3955 ecoff_scom_symbol.name = SCOMMON;
3956 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3957 ecoff_scom_symbol.section = &ecoff_scom_section;
3958 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3960 section = &ecoff_scom_section;
3961 break;
3962 case scSUndefined:
3963 section = bfd_und_section_ptr;
3964 break;
3965 case scInit:
3966 section = bfd_make_section_old_way (abfd, ".init");
3967 value -= section->vma;
3968 break;
3969 case scFini:
3970 section = bfd_make_section_old_way (abfd, ".fini");
3971 value -= section->vma;
3972 break;
3973 case scRConst:
3974 section = bfd_make_section_old_way (abfd, ".rconst");
3975 value -= section->vma;
3976 break;
3979 if (section == (asection *) NULL)
3980 continue;
3982 name = ssext + esym.asym.iss;
3984 h = NULL;
3985 if (! (_bfd_generic_link_add_one_symbol
3986 (info, abfd, name,
3987 esym.weakext ? BSF_WEAK : BSF_GLOBAL,
3988 section, value, (const char *) NULL, true, true,
3989 (struct bfd_link_hash_entry **) &h)))
3990 return false;
3992 *sym_hash = h;
3994 /* If we are building an ECOFF hash table, save the external
3995 symbol information. */
3996 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3998 if (h->abfd == (bfd *) NULL
3999 || (! bfd_is_und_section (section)
4000 && (! bfd_is_com_section (section)
4001 || (h->root.type != bfd_link_hash_defined
4002 && h->root.type != bfd_link_hash_defweak))))
4004 h->abfd = abfd;
4005 h->esym = esym;
4008 /* Remember whether this symbol was small undefined. */
4009 if (esym.asym.sc == scSUndefined)
4010 h->small = 1;
4012 /* If this symbol was ever small undefined, it needs to wind
4013 up in a GP relative section. We can't control the
4014 section of a defined symbol, but we can control the
4015 section of a common symbol. This case is actually needed
4016 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4017 if (h->small
4018 && h->root.type == bfd_link_hash_common
4019 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4021 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4022 SCOMMON);
4023 h->root.u.c.p->section->flags = SEC_ALLOC;
4024 if (h->esym.asym.sc == scCommon)
4025 h->esym.asym.sc = scSCommon;
4030 return true;
4033 /* ECOFF final link routines. */
4035 static boolean ecoff_final_link_debug_accumulate
4036 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4037 PTR handle));
4038 static boolean ecoff_link_write_external
4039 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4040 static boolean ecoff_indirect_link_order
4041 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4042 struct bfd_link_order *));
4043 static boolean ecoff_reloc_link_order
4044 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4045 struct bfd_link_order *));
4047 /* Structure used to pass information to ecoff_link_write_external. */
4049 struct extsym_info
4051 bfd *abfd;
4052 struct bfd_link_info *info;
4055 /* ECOFF final link routine. This looks through all the input BFDs
4056 and gathers together all the debugging information, and then
4057 processes all the link order information. This may cause it to
4058 close and reopen some input BFDs; I'll see how bad this is. */
4060 boolean
4061 _bfd_ecoff_bfd_final_link (abfd, info)
4062 bfd *abfd;
4063 struct bfd_link_info *info;
4065 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4066 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4067 HDRR *symhdr;
4068 PTR handle;
4069 register bfd *input_bfd;
4070 asection *o;
4071 struct bfd_link_order *p;
4072 struct extsym_info einfo;
4074 /* We accumulate the debugging information counts in the symbolic
4075 header. */
4076 symhdr = &debug->symbolic_header;
4077 symhdr->vstamp = 0;
4078 symhdr->ilineMax = 0;
4079 symhdr->cbLine = 0;
4080 symhdr->idnMax = 0;
4081 symhdr->ipdMax = 0;
4082 symhdr->isymMax = 0;
4083 symhdr->ioptMax = 0;
4084 symhdr->iauxMax = 0;
4085 symhdr->issMax = 0;
4086 symhdr->issExtMax = 0;
4087 symhdr->ifdMax = 0;
4088 symhdr->crfd = 0;
4089 symhdr->iextMax = 0;
4091 /* We accumulate the debugging information itself in the debug_info
4092 structure. */
4093 debug->line = NULL;
4094 debug->external_dnr = NULL;
4095 debug->external_pdr = NULL;
4096 debug->external_sym = NULL;
4097 debug->external_opt = NULL;
4098 debug->external_aux = NULL;
4099 debug->ss = NULL;
4100 debug->ssext = debug->ssext_end = NULL;
4101 debug->external_fdr = NULL;
4102 debug->external_rfd = NULL;
4103 debug->external_ext = debug->external_ext_end = NULL;
4105 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4106 if (handle == (PTR) NULL)
4107 return false;
4109 /* Accumulate the debugging symbols from each input BFD. */
4110 for (input_bfd = info->input_bfds;
4111 input_bfd != (bfd *) NULL;
4112 input_bfd = input_bfd->link_next)
4114 boolean ret;
4116 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4118 /* Abitrarily set the symbolic header vstamp to the vstamp
4119 of the first object file in the link. */
4120 if (symhdr->vstamp == 0)
4121 symhdr->vstamp
4122 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4123 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4124 handle);
4126 else
4127 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4128 debug, &backend->debug_swap,
4129 input_bfd, info);
4130 if (! ret)
4131 return false;
4133 /* Combine the register masks. */
4134 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4135 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4136 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4137 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4138 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4139 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4142 /* Write out the external symbols. */
4143 einfo.abfd = abfd;
4144 einfo.info = info;
4145 ecoff_link_hash_traverse (ecoff_hash_table (info),
4146 ecoff_link_write_external,
4147 (PTR) &einfo);
4149 if (info->relocateable)
4151 /* We need to make a pass over the link_orders to count up the
4152 number of relocations we will need to output, so that we know
4153 how much space they will take up. */
4154 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4156 o->reloc_count = 0;
4157 for (p = o->link_order_head;
4158 p != (struct bfd_link_order *) NULL;
4159 p = p->next)
4160 if (p->type == bfd_indirect_link_order)
4161 o->reloc_count += p->u.indirect.section->reloc_count;
4162 else if (p->type == bfd_section_reloc_link_order
4163 || p->type == bfd_symbol_reloc_link_order)
4164 ++o->reloc_count;
4168 /* Compute the reloc and symbol file positions. */
4169 ecoff_compute_reloc_file_positions (abfd);
4171 /* Write out the debugging information. */
4172 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4173 &backend->debug_swap, info,
4174 ecoff_data (abfd)->sym_filepos))
4175 return false;
4177 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4179 if (info->relocateable)
4181 /* Now reset the reloc_count field of the sections in the output
4182 BFD to 0, so that we can use them to keep track of how many
4183 relocs we have output thus far. */
4184 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4185 o->reloc_count = 0;
4188 /* Get a value for the GP register. */
4189 if (ecoff_data (abfd)->gp == 0)
4191 struct bfd_link_hash_entry *h;
4193 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4194 if (h != (struct bfd_link_hash_entry *) NULL
4195 && h->type == bfd_link_hash_defined)
4196 ecoff_data (abfd)->gp = (h->u.def.value
4197 + h->u.def.section->output_section->vma
4198 + h->u.def.section->output_offset);
4199 else if (info->relocateable)
4201 bfd_vma lo;
4203 /* Make up a value. */
4204 lo = (bfd_vma) -1;
4205 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4207 if (o->vma < lo
4208 && (strcmp (o->name, _SBSS) == 0
4209 || strcmp (o->name, _SDATA) == 0
4210 || strcmp (o->name, _LIT4) == 0
4211 || strcmp (o->name, _LIT8) == 0
4212 || strcmp (o->name, _LITA) == 0))
4213 lo = o->vma;
4215 ecoff_data (abfd)->gp = lo + 0x8000;
4217 else
4219 /* If the relocate_section function needs to do a reloc
4220 involving the GP value, it should make a reloc_dangerous
4221 callback to warn that GP is not defined. */
4225 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4227 for (p = o->link_order_head;
4228 p != (struct bfd_link_order *) NULL;
4229 p = p->next)
4231 if (p->type == bfd_indirect_link_order
4232 && (bfd_get_flavour (p->u.indirect.section->owner)
4233 == bfd_target_ecoff_flavour))
4235 if (! ecoff_indirect_link_order (abfd, info, o, p))
4236 return false;
4238 else if (p->type == bfd_section_reloc_link_order
4239 || p->type == bfd_symbol_reloc_link_order)
4241 if (! ecoff_reloc_link_order (abfd, info, o, p))
4242 return false;
4244 else
4246 if (! _bfd_default_link_order (abfd, info, o, p))
4247 return false;
4252 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4254 ecoff_data (abfd)->linker = true;
4256 return true;
4259 /* Accumulate the debugging information for an input BFD into the
4260 output BFD. This must read in the symbolic information of the
4261 input BFD. */
4263 static boolean
4264 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4265 bfd *output_bfd;
4266 bfd *input_bfd;
4267 struct bfd_link_info *info;
4268 PTR handle;
4270 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4271 const struct ecoff_debug_swap * const swap =
4272 &ecoff_backend (input_bfd)->debug_swap;
4273 HDRR *symhdr = &debug->symbolic_header;
4274 boolean ret;
4276 #define READ(ptr, offset, count, size, type) \
4277 if (symhdr->count == 0) \
4278 debug->ptr = NULL; \
4279 else \
4281 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
4282 if (debug->ptr == NULL) \
4284 ret = false; \
4285 goto return_something; \
4287 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4288 != 0) \
4289 || (bfd_read (debug->ptr, size, symhdr->count, \
4290 input_bfd) != size * symhdr->count)) \
4292 ret = false; \
4293 goto return_something; \
4297 /* If raw_syments is not NULL, then the data was already by read by
4298 _bfd_ecoff_slurp_symbolic_info. */
4299 if (ecoff_data (input_bfd)->raw_syments == NULL)
4301 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4302 unsigned char *);
4303 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4304 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4305 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4306 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4307 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4308 union aux_ext *);
4309 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4310 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4311 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4313 #undef READ
4315 /* We do not read the external strings or the external symbols. */
4317 ret = (bfd_ecoff_debug_accumulate
4318 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4319 &ecoff_backend (output_bfd)->debug_swap,
4320 input_bfd, debug, swap, info));
4322 return_something:
4323 if (ecoff_data (input_bfd)->raw_syments == NULL)
4325 if (debug->line != NULL)
4326 free (debug->line);
4327 if (debug->external_dnr != NULL)
4328 free (debug->external_dnr);
4329 if (debug->external_pdr != NULL)
4330 free (debug->external_pdr);
4331 if (debug->external_sym != NULL)
4332 free (debug->external_sym);
4333 if (debug->external_opt != NULL)
4334 free (debug->external_opt);
4335 if (debug->external_aux != NULL)
4336 free (debug->external_aux);
4337 if (debug->ss != NULL)
4338 free (debug->ss);
4339 if (debug->external_fdr != NULL)
4340 free (debug->external_fdr);
4341 if (debug->external_rfd != NULL)
4342 free (debug->external_rfd);
4344 /* Make sure we don't accidentally follow one of these pointers
4345 into freed memory. */
4346 debug->line = NULL;
4347 debug->external_dnr = NULL;
4348 debug->external_pdr = NULL;
4349 debug->external_sym = NULL;
4350 debug->external_opt = NULL;
4351 debug->external_aux = NULL;
4352 debug->ss = NULL;
4353 debug->external_fdr = NULL;
4354 debug->external_rfd = NULL;
4357 return ret;
4360 /* Put out information for an external symbol. These come only from
4361 the hash table. */
4363 static boolean
4364 ecoff_link_write_external (h, data)
4365 struct ecoff_link_hash_entry *h;
4366 PTR data;
4368 struct extsym_info *einfo = (struct extsym_info *) data;
4369 bfd *output_bfd = einfo->abfd;
4370 boolean strip;
4372 /* We need to check if this symbol is being stripped. */
4373 if (h->root.type == bfd_link_hash_undefined
4374 || h->root.type == bfd_link_hash_undefweak)
4375 strip = false;
4376 else if (einfo->info->strip == strip_all
4377 || (einfo->info->strip == strip_some
4378 && bfd_hash_lookup (einfo->info->keep_hash,
4379 h->root.root.string,
4380 false, false) == NULL))
4381 strip = true;
4382 else
4383 strip = false;
4385 if (strip || h->written)
4386 return true;
4388 if (h->abfd == (bfd *) NULL)
4390 h->esym.jmptbl = 0;
4391 h->esym.cobol_main = 0;
4392 h->esym.weakext = 0;
4393 h->esym.reserved = 0;
4394 h->esym.ifd = ifdNil;
4395 h->esym.asym.value = 0;
4396 h->esym.asym.st = stGlobal;
4398 if (h->root.type != bfd_link_hash_defined
4399 && h->root.type != bfd_link_hash_defweak)
4400 h->esym.asym.sc = scAbs;
4401 else
4403 asection *output_section;
4404 const char *name;
4406 output_section = h->root.u.def.section->output_section;
4407 name = bfd_section_name (output_section->owner, output_section);
4409 if (strcmp (name, _TEXT) == 0)
4410 h->esym.asym.sc = scText;
4411 else if (strcmp (name, _DATA) == 0)
4412 h->esym.asym.sc = scData;
4413 else if (strcmp (name, _SDATA) == 0)
4414 h->esym.asym.sc = scSData;
4415 else if (strcmp (name, _RDATA) == 0)
4416 h->esym.asym.sc = scRData;
4417 else if (strcmp (name, _BSS) == 0)
4418 h->esym.asym.sc = scBss;
4419 else if (strcmp (name, _SBSS) == 0)
4420 h->esym.asym.sc = scSBss;
4421 else if (strcmp (name, _INIT) == 0)
4422 h->esym.asym.sc = scInit;
4423 else if (strcmp (name, _FINI) == 0)
4424 h->esym.asym.sc = scFini;
4425 else if (strcmp (name, _PDATA) == 0)
4426 h->esym.asym.sc = scPData;
4427 else if (strcmp (name, _XDATA) == 0)
4428 h->esym.asym.sc = scXData;
4429 else if (strcmp (name, _RCONST) == 0)
4430 h->esym.asym.sc = scRConst;
4431 else
4432 h->esym.asym.sc = scAbs;
4435 h->esym.asym.reserved = 0;
4436 h->esym.asym.index = indexNil;
4438 else if (h->esym.ifd != -1)
4440 struct ecoff_debug_info *debug;
4442 /* Adjust the FDR index for the symbol by that used for the
4443 input BFD. */
4444 debug = &ecoff_data (h->abfd)->debug_info;
4445 BFD_ASSERT (h->esym.ifd >= 0
4446 && h->esym.ifd < debug->symbolic_header.ifdMax);
4447 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4450 switch (h->root.type)
4452 default:
4453 case bfd_link_hash_new:
4454 abort ();
4455 case bfd_link_hash_undefined:
4456 case bfd_link_hash_undefweak:
4457 if (h->esym.asym.sc != scUndefined
4458 && h->esym.asym.sc != scSUndefined)
4459 h->esym.asym.sc = scUndefined;
4460 break;
4461 case bfd_link_hash_defined:
4462 case bfd_link_hash_defweak:
4463 if (h->esym.asym.sc == scUndefined
4464 || h->esym.asym.sc == scSUndefined)
4465 h->esym.asym.sc = scAbs;
4466 else if (h->esym.asym.sc == scCommon)
4467 h->esym.asym.sc = scBss;
4468 else if (h->esym.asym.sc == scSCommon)
4469 h->esym.asym.sc = scSBss;
4470 h->esym.asym.value = (h->root.u.def.value
4471 + h->root.u.def.section->output_section->vma
4472 + h->root.u.def.section->output_offset);
4473 break;
4474 case bfd_link_hash_common:
4475 if (h->esym.asym.sc != scCommon
4476 && h->esym.asym.sc != scSCommon)
4477 h->esym.asym.sc = scCommon;
4478 h->esym.asym.value = h->root.u.c.size;
4479 break;
4480 case bfd_link_hash_indirect:
4481 case bfd_link_hash_warning:
4482 /* FIXME: Ignore these for now. The circumstances under which
4483 they should be written out are not clear to me. */
4484 return true;
4487 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4488 symbol number. */
4489 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4490 h->written = 1;
4492 return (bfd_ecoff_debug_one_external
4493 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4494 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4495 &h->esym));
4498 /* Relocate and write an ECOFF section into an ECOFF output file. */
4500 static boolean
4501 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4502 bfd *output_bfd;
4503 struct bfd_link_info *info;
4504 asection *output_section;
4505 struct bfd_link_order *link_order;
4507 asection *input_section;
4508 bfd *input_bfd;
4509 struct ecoff_section_tdata *section_tdata;
4510 bfd_size_type raw_size;
4511 bfd_size_type cooked_size;
4512 bfd_byte *contents = NULL;
4513 bfd_size_type external_reloc_size;
4514 bfd_size_type external_relocs_size;
4515 PTR external_relocs = NULL;
4517 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4519 if (link_order->size == 0)
4520 return true;
4522 input_section = link_order->u.indirect.section;
4523 input_bfd = input_section->owner;
4524 section_tdata = ecoff_section_data (input_bfd, input_section);
4526 raw_size = input_section->_raw_size;
4527 cooked_size = input_section->_cooked_size;
4528 if (cooked_size == 0)
4529 cooked_size = raw_size;
4531 BFD_ASSERT (input_section->output_section == output_section);
4532 BFD_ASSERT (input_section->output_offset == link_order->offset);
4533 BFD_ASSERT (cooked_size == link_order->size);
4535 /* Get the section contents. We allocate memory for the larger of
4536 the size before relocating and the size after relocating. */
4537 contents = (bfd_byte *) bfd_malloc (raw_size >= cooked_size
4538 ? (size_t) raw_size
4539 : (size_t) cooked_size);
4540 if (contents == NULL && raw_size != 0)
4541 goto error_return;
4543 /* If we are relaxing, the contents may have already been read into
4544 memory, in which case we copy them into our new buffer. We don't
4545 simply reuse the old buffer in case cooked_size > raw_size. */
4546 if (section_tdata != (struct ecoff_section_tdata *) NULL
4547 && section_tdata->contents != (bfd_byte *) NULL)
4548 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4549 else
4551 if (! bfd_get_section_contents (input_bfd, input_section,
4552 (PTR) contents,
4553 (file_ptr) 0, raw_size))
4554 goto error_return;
4557 /* Get the relocs. If we are relaxing MIPS code, they will already
4558 have been read in. Otherwise, we read them in now. */
4559 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4560 external_relocs_size = external_reloc_size * input_section->reloc_count;
4562 if (section_tdata != (struct ecoff_section_tdata *) NULL
4563 && section_tdata->external_relocs != NULL)
4564 external_relocs = section_tdata->external_relocs;
4565 else
4567 external_relocs = (PTR) bfd_malloc ((size_t) external_relocs_size);
4568 if (external_relocs == NULL && external_relocs_size != 0)
4569 goto error_return;
4571 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4572 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4573 != external_relocs_size))
4574 goto error_return;
4577 /* Relocate the section contents. */
4578 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4579 (output_bfd, info, input_bfd, input_section, contents,
4580 external_relocs)))
4581 goto error_return;
4583 /* Write out the relocated section. */
4584 if (! bfd_set_section_contents (output_bfd,
4585 output_section,
4586 (PTR) contents,
4587 input_section->output_offset,
4588 cooked_size))
4589 goto error_return;
4591 /* If we are producing relocateable output, the relocs were
4592 modified, and we write them out now. We use the reloc_count
4593 field of output_section to keep track of the number of relocs we
4594 have output so far. */
4595 if (info->relocateable)
4597 if (bfd_seek (output_bfd,
4598 (output_section->rel_filepos +
4599 output_section->reloc_count * external_reloc_size),
4600 SEEK_SET) != 0
4601 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4602 != external_relocs_size))
4603 goto error_return;
4604 output_section->reloc_count += input_section->reloc_count;
4607 if (contents != NULL)
4608 free (contents);
4609 if (external_relocs != NULL && section_tdata == NULL)
4610 free (external_relocs);
4611 return true;
4613 error_return:
4614 if (contents != NULL)
4615 free (contents);
4616 if (external_relocs != NULL && section_tdata == NULL)
4617 free (external_relocs);
4618 return false;
4621 /* Generate a reloc when linking an ECOFF file. This is a reloc
4622 requested by the linker, and does come from any input file. This
4623 is used to build constructor and destructor tables when linking
4624 with -Ur. */
4626 static boolean
4627 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4628 bfd *output_bfd;
4629 struct bfd_link_info *info;
4630 asection *output_section;
4631 struct bfd_link_order *link_order;
4633 enum bfd_link_order_type type;
4634 asection *section;
4635 bfd_vma addend;
4636 arelent rel;
4637 struct internal_reloc in;
4638 bfd_size_type external_reloc_size;
4639 bfd_byte *rbuf;
4640 boolean ok;
4642 type = link_order->type;
4643 section = NULL;
4644 addend = link_order->u.reloc.p->addend;
4646 /* We set up an arelent to pass to the backend adjust_reloc_out
4647 routine. */
4648 rel.address = link_order->offset;
4650 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4651 if (rel.howto == 0)
4653 bfd_set_error (bfd_error_bad_value);
4654 return false;
4657 if (type == bfd_section_reloc_link_order)
4659 section = link_order->u.reloc.p->u.section;
4660 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4662 else
4664 struct bfd_link_hash_entry *h;
4666 /* Treat a reloc against a defined symbol as though it were
4667 actually against the section. */
4668 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4669 link_order->u.reloc.p->u.name,
4670 false, false, false);
4671 if (h != NULL
4672 && (h->type == bfd_link_hash_defined
4673 || h->type == bfd_link_hash_defweak))
4675 type = bfd_section_reloc_link_order;
4676 section = h->u.def.section->output_section;
4677 /* It seems that we ought to add the symbol value to the
4678 addend here, but in practice it has already been added
4679 because it was passed to constructor_callback. */
4680 addend += section->vma + h->u.def.section->output_offset;
4682 else
4684 /* We can't set up a reloc against a symbol correctly,
4685 because we have no asymbol structure. Currently no
4686 adjust_reloc_out routine cares. */
4687 rel.sym_ptr_ptr = (asymbol **) NULL;
4691 /* All ECOFF relocs are in-place. Put the addend into the object
4692 file. */
4694 BFD_ASSERT (rel.howto->partial_inplace);
4695 if (addend != 0)
4697 bfd_size_type size;
4698 bfd_reloc_status_type rstat;
4699 bfd_byte *buf;
4700 boolean ok;
4702 size = bfd_get_reloc_size (rel.howto);
4703 buf = (bfd_byte *) bfd_zmalloc (size);
4704 if (buf == (bfd_byte *) NULL)
4705 return false;
4706 rstat = _bfd_relocate_contents (rel.howto, output_bfd, addend, buf);
4707 switch (rstat)
4709 case bfd_reloc_ok:
4710 break;
4711 default:
4712 case bfd_reloc_outofrange:
4713 abort ();
4714 case bfd_reloc_overflow:
4715 if (! ((*info->callbacks->reloc_overflow)
4716 (info,
4717 (link_order->type == bfd_section_reloc_link_order
4718 ? bfd_section_name (output_bfd, section)
4719 : link_order->u.reloc.p->u.name),
4720 rel.howto->name, addend, (bfd *) NULL,
4721 (asection *) NULL, (bfd_vma) 0)))
4723 free (buf);
4724 return false;
4726 break;
4728 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4729 (file_ptr) link_order->offset, size);
4730 free (buf);
4731 if (! ok)
4732 return false;
4735 rel.addend = 0;
4737 /* Move the information into a internal_reloc structure. */
4738 in.r_vaddr = (rel.address
4739 + bfd_get_section_vma (output_bfd, output_section));
4740 in.r_type = rel.howto->type;
4742 if (type == bfd_symbol_reloc_link_order)
4744 struct ecoff_link_hash_entry *h;
4746 h = ((struct ecoff_link_hash_entry *)
4747 bfd_wrapped_link_hash_lookup (output_bfd, info,
4748 link_order->u.reloc.p->u.name,
4749 false, false, true));
4750 if (h != (struct ecoff_link_hash_entry *) NULL
4751 && h->indx != -1)
4752 in.r_symndx = h->indx;
4753 else
4755 if (! ((*info->callbacks->unattached_reloc)
4756 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4757 (asection *) NULL, (bfd_vma) 0)))
4758 return false;
4759 in.r_symndx = 0;
4761 in.r_extern = 1;
4763 else
4765 CONST char *name;
4767 name = bfd_get_section_name (output_bfd, section);
4768 if (strcmp (name, ".text") == 0)
4769 in.r_symndx = RELOC_SECTION_TEXT;
4770 else if (strcmp (name, ".rdata") == 0)
4771 in.r_symndx = RELOC_SECTION_RDATA;
4772 else if (strcmp (name, ".data") == 0)
4773 in.r_symndx = RELOC_SECTION_DATA;
4774 else if (strcmp (name, ".sdata") == 0)
4775 in.r_symndx = RELOC_SECTION_SDATA;
4776 else if (strcmp (name, ".sbss") == 0)
4777 in.r_symndx = RELOC_SECTION_SBSS;
4778 else if (strcmp (name, ".bss") == 0)
4779 in.r_symndx = RELOC_SECTION_BSS;
4780 else if (strcmp (name, ".init") == 0)
4781 in.r_symndx = RELOC_SECTION_INIT;
4782 else if (strcmp (name, ".lit8") == 0)
4783 in.r_symndx = RELOC_SECTION_LIT8;
4784 else if (strcmp (name, ".lit4") == 0)
4785 in.r_symndx = RELOC_SECTION_LIT4;
4786 else if (strcmp (name, ".xdata") == 0)
4787 in.r_symndx = RELOC_SECTION_XDATA;
4788 else if (strcmp (name, ".pdata") == 0)
4789 in.r_symndx = RELOC_SECTION_PDATA;
4790 else if (strcmp (name, ".fini") == 0)
4791 in.r_symndx = RELOC_SECTION_FINI;
4792 else if (strcmp (name, ".lita") == 0)
4793 in.r_symndx = RELOC_SECTION_LITA;
4794 else if (strcmp (name, "*ABS*") == 0)
4795 in.r_symndx = RELOC_SECTION_ABS;
4796 else if (strcmp (name, ".rconst") == 0)
4797 in.r_symndx = RELOC_SECTION_RCONST;
4798 else
4799 abort ();
4800 in.r_extern = 0;
4803 /* Let the BFD backend adjust the reloc. */
4804 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4806 /* Get some memory and swap out the reloc. */
4807 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4808 rbuf = (bfd_byte *) bfd_malloc ((size_t) external_reloc_size);
4809 if (rbuf == (bfd_byte *) NULL)
4810 return false;
4812 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4814 ok = (bfd_seek (output_bfd,
4815 (output_section->rel_filepos +
4816 output_section->reloc_count * external_reloc_size),
4817 SEEK_SET) == 0
4818 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4819 == external_reloc_size));
4821 if (ok)
4822 ++output_section->reloc_count;
4824 free (rbuf);
4826 return ok;