Add support for storing local symbols in a small structure to save
[binutils.git] / bfd / ecoff.c
blob32a2309e941db78ed19cc2b5e1da40aa098a85a7
1 /* Generic ECOFF (Extended-COFF) routines.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4 Original version by Per Bothner.
5 Full support added by Ian Lance Taylor, ian@cygnus.com.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "bfdlink.h"
26 #include "libbfd.h"
27 #include "aout/ar.h"
28 #include "aout/ranlib.h"
29 #include "aout/stab_gnu.h"
31 /* FIXME: We need the definitions of N_SET[ADTB], but aout64.h defines
32 some other stuff which we don't want and which conflicts with stuff
33 we do want. */
34 #include "libaout.h"
35 #include "aout/aout64.h"
36 #undef N_ABS
37 #undef exec_hdr
38 #undef obj_sym_filepos
40 #include "coff/internal.h"
41 #include "coff/sym.h"
42 #include "coff/symconst.h"
43 #include "coff/ecoff.h"
44 #include "libcoff.h"
45 #include "libecoff.h"
47 /* Prototypes for static functions. */
49 static int ecoff_get_magic PARAMS ((bfd *abfd));
50 static long ecoff_sec_to_styp_flags PARAMS ((const char *name,
51 flagword flags));
52 static boolean ecoff_slurp_symbolic_header PARAMS ((bfd *abfd));
53 static boolean ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
54 asymbol *asym, int ext, int weak));
55 static void ecoff_emit_aggregate PARAMS ((bfd *abfd, FDR *fdr,
56 char *string,
57 RNDXR *rndx, long isym,
58 const char *which));
59 static char *ecoff_type_to_string PARAMS ((bfd *abfd, FDR *fdr,
60 unsigned int indx));
61 static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
62 asymbol **symbols));
63 static int ecoff_sort_hdrs PARAMS ((const PTR, const PTR));
64 static boolean ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
65 static bfd_size_type ecoff_compute_reloc_file_positions PARAMS ((bfd *abfd));
66 static boolean ecoff_get_extr PARAMS ((asymbol *, EXTR *));
67 static void ecoff_set_index PARAMS ((asymbol *, bfd_size_type));
68 static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
69 unsigned int *rehash,
70 unsigned int size,
71 unsigned int hlog));
73 /* This stuff is somewhat copied from coffcode.h. */
75 static asection bfd_debug_section = { "*DEBUG*" };
77 /* Create an ECOFF object. */
79 boolean
80 _bfd_ecoff_mkobject (abfd)
81 bfd *abfd;
83 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
84 bfd_zalloc (abfd, sizeof (ecoff_data_type)));
85 if (abfd->tdata.ecoff_obj_data == NULL)
86 return false;
88 return true;
91 /* This is a hook called by coff_real_object_p to create any backend
92 specific information. */
94 PTR
95 _bfd_ecoff_mkobject_hook (abfd, filehdr, aouthdr)
96 bfd *abfd;
97 PTR filehdr;
98 PTR aouthdr;
100 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
101 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
102 ecoff_data_type *ecoff;
104 if (_bfd_ecoff_mkobject (abfd) == false)
105 return NULL;
107 ecoff = ecoff_data (abfd);
108 ecoff->gp_size = 8;
109 ecoff->sym_filepos = internal_f->f_symptr;
111 if (internal_a != (struct internal_aouthdr *) NULL)
113 int i;
115 ecoff->text_start = internal_a->text_start;
116 ecoff->text_end = internal_a->text_start + internal_a->tsize;
117 ecoff->gp = internal_a->gp_value;
118 ecoff->gprmask = internal_a->gprmask;
119 for (i = 0; i < 4; i++)
120 ecoff->cprmask[i] = internal_a->cprmask[i];
121 ecoff->fprmask = internal_a->fprmask;
122 if (internal_a->magic == ECOFF_AOUT_ZMAGIC)
123 abfd->flags |= D_PAGED;
124 else
125 abfd->flags &=~ D_PAGED;
128 /* It turns out that no special action is required by the MIPS or
129 Alpha ECOFF backends. They have different information in the
130 a.out header, but we just copy it all (e.g., gprmask, cprmask and
131 fprmask) and let the swapping routines ensure that only relevant
132 information is written out. */
134 return (PTR) ecoff;
137 /* Initialize a new section. */
139 boolean
140 _bfd_ecoff_new_section_hook (abfd, section)
141 bfd *abfd;
142 asection *section;
144 section->alignment_power = 4;
146 if (strcmp (section->name, _TEXT) == 0
147 || strcmp (section->name, _INIT) == 0
148 || strcmp (section->name, _FINI) == 0)
149 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
150 else if (strcmp (section->name, _DATA) == 0
151 || strcmp (section->name, _SDATA) == 0)
152 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
153 else if (strcmp (section->name, _RDATA) == 0
154 || strcmp (section->name, _LIT8) == 0
155 || strcmp (section->name, _LIT4) == 0
156 || strcmp (section->name, _RCONST) == 0
157 || strcmp (section->name, _PDATA) == 0)
158 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
159 else if (strcmp (section->name, _BSS) == 0
160 || strcmp (section->name, _SBSS) == 0)
161 section->flags |= SEC_ALLOC;
162 else if (strcmp (section->name, _LIB) == 0)
164 /* An Irix 4 shared libary. */
165 section->flags |= SEC_COFF_SHARED_LIBRARY;
168 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
169 uncertain about .init on some systems and I don't know how shared
170 libraries work. */
172 return true;
175 /* Determine the machine architecture and type. This is called from
176 the generic COFF routines. It is the inverse of ecoff_get_magic,
177 below. This could be an ECOFF backend routine, with one version
178 for each target, but there aren't all that many ECOFF targets. */
180 boolean
181 _bfd_ecoff_set_arch_mach_hook (abfd, filehdr)
182 bfd *abfd;
183 PTR filehdr;
185 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
186 enum bfd_architecture arch;
187 unsigned long mach;
189 switch (internal_f->f_magic)
191 case MIPS_MAGIC_1:
192 case MIPS_MAGIC_LITTLE:
193 case MIPS_MAGIC_BIG:
194 arch = bfd_arch_mips;
195 mach = 3000;
196 break;
198 case MIPS_MAGIC_LITTLE2:
199 case MIPS_MAGIC_BIG2:
200 /* MIPS ISA level 2: the r6000 */
201 arch = bfd_arch_mips;
202 mach = 6000;
203 break;
205 case MIPS_MAGIC_LITTLE3:
206 case MIPS_MAGIC_BIG3:
207 /* MIPS ISA level 3: the r4000 */
208 arch = bfd_arch_mips;
209 mach = 4000;
210 break;
212 case ALPHA_MAGIC:
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 int big, little;
235 switch (bfd_get_arch (abfd))
237 case bfd_arch_mips:
238 switch (bfd_get_mach (abfd))
240 default:
241 case 0:
242 case 3000:
243 big = MIPS_MAGIC_BIG;
244 little = MIPS_MAGIC_LITTLE;
245 break;
247 case 6000:
248 big = MIPS_MAGIC_BIG2;
249 little = MIPS_MAGIC_LITTLE2;
250 break;
252 case 4000:
253 big = MIPS_MAGIC_BIG3;
254 little = MIPS_MAGIC_LITTLE3;
255 break;
258 return bfd_big_endian (abfd) ? big : little;
260 case bfd_arch_alpha:
261 return ALPHA_MAGIC;
263 default:
264 abort ();
265 return 0;
269 /* Get the section s_flags to use for a section. */
271 static long
272 ecoff_sec_to_styp_flags (name, flags)
273 const char *name;
274 flagword flags;
276 long styp;
278 styp = 0;
280 if (strcmp (name, _TEXT) == 0)
281 styp = STYP_TEXT;
282 else if (strcmp (name, _DATA) == 0)
283 styp = STYP_DATA;
284 else if (strcmp (name, _SDATA) == 0)
285 styp = STYP_SDATA;
286 else if (strcmp (name, _RDATA) == 0)
287 styp = STYP_RDATA;
288 else if (strcmp (name, _LITA) == 0)
289 styp = STYP_LITA;
290 else if (strcmp (name, _LIT8) == 0)
291 styp = STYP_LIT8;
292 else if (strcmp (name, _LIT4) == 0)
293 styp = STYP_LIT4;
294 else if (strcmp (name, _BSS) == 0)
295 styp = STYP_BSS;
296 else if (strcmp (name, _SBSS) == 0)
297 styp = STYP_SBSS;
298 else if (strcmp (name, _INIT) == 0)
299 styp = STYP_ECOFF_INIT;
300 else if (strcmp (name, _FINI) == 0)
301 styp = STYP_ECOFF_FINI;
302 else if (strcmp (name, _PDATA) == 0)
303 styp = STYP_PDATA;
304 else if (strcmp (name, _XDATA) == 0)
305 styp = STYP_XDATA;
306 else if (strcmp (name, _LIB) == 0)
307 styp = STYP_ECOFF_LIB;
308 else if (strcmp (name, _GOT) == 0)
309 styp = STYP_GOT;
310 else if (strcmp (name, _HASH) == 0)
311 styp = STYP_HASH;
312 else if (strcmp (name, _DYNAMIC) == 0)
313 styp = STYP_DYNAMIC;
314 else if (strcmp (name, _LIBLIST) == 0)
315 styp = STYP_LIBLIST;
316 else if (strcmp (name, _RELDYN) == 0)
317 styp = STYP_RELDYN;
318 else if (strcmp (name, _CONFLIC) == 0)
319 styp = STYP_CONFLIC;
320 else if (strcmp (name, _DYNSTR) == 0)
321 styp = STYP_DYNSTR;
322 else if (strcmp (name, _DYNSYM) == 0)
323 styp = STYP_DYNSYM;
324 else if (strcmp (name, _COMMENT) == 0)
326 styp = STYP_COMMENT;
327 flags &=~ SEC_NEVER_LOAD;
329 else if (strcmp (name, _RCONST) == 0)
330 styp = STYP_RCONST;
331 else if (flags & SEC_CODE)
332 styp = STYP_TEXT;
333 else if (flags & SEC_DATA)
334 styp = STYP_DATA;
335 else if (flags & SEC_READONLY)
336 styp = STYP_RDATA;
337 else if (flags & SEC_LOAD)
338 styp = STYP_REG;
339 else
340 styp = STYP_BSS;
342 if (flags & SEC_NEVER_LOAD)
343 styp |= STYP_NOLOAD;
345 return styp;
348 /* Get the BFD flags to use for a section. */
350 /*ARGSUSED*/
351 flagword
352 _bfd_ecoff_styp_to_sec_flags (abfd, hdr, name)
353 bfd *abfd;
354 PTR hdr;
355 const char *name;
357 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
358 long styp_flags = internal_s->s_flags;
359 flagword sec_flags=0;
361 if (styp_flags & STYP_NOLOAD)
362 sec_flags |= SEC_NEVER_LOAD;
364 /* For 386 COFF, at least, an unloadable text or data section is
365 actually a shared library section. */
366 if ((styp_flags & STYP_TEXT)
367 || (styp_flags & STYP_ECOFF_INIT)
368 || (styp_flags & STYP_ECOFF_FINI)
369 || (styp_flags & STYP_DYNAMIC)
370 || (styp_flags & STYP_LIBLIST)
371 || (styp_flags & STYP_RELDYN)
372 || styp_flags == STYP_CONFLIC
373 || (styp_flags & STYP_DYNSTR)
374 || (styp_flags & STYP_DYNSYM)
375 || (styp_flags & STYP_HASH))
377 if (sec_flags & SEC_NEVER_LOAD)
378 sec_flags |= SEC_CODE | SEC_COFF_SHARED_LIBRARY;
379 else
380 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
382 else if ((styp_flags & STYP_DATA)
383 || (styp_flags & STYP_RDATA)
384 || (styp_flags & STYP_SDATA)
385 || styp_flags == STYP_PDATA
386 || styp_flags == STYP_XDATA
387 || (styp_flags & STYP_GOT)
388 || styp_flags == STYP_RCONST)
390 if (sec_flags & SEC_NEVER_LOAD)
391 sec_flags |= SEC_DATA | SEC_COFF_SHARED_LIBRARY;
392 else
393 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
394 if ((styp_flags & STYP_RDATA)
395 || styp_flags == STYP_PDATA
396 || styp_flags == STYP_RCONST)
397 sec_flags |= SEC_READONLY;
399 else if ((styp_flags & STYP_BSS)
400 || (styp_flags & STYP_SBSS))
402 sec_flags |= SEC_ALLOC;
404 else if ((styp_flags & STYP_INFO) || styp_flags == STYP_COMMENT)
406 sec_flags |= SEC_NEVER_LOAD;
408 else if ((styp_flags & STYP_LITA)
409 || (styp_flags & STYP_LIT8)
410 || (styp_flags & STYP_LIT4))
412 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
414 else if (styp_flags & STYP_ECOFF_LIB)
416 sec_flags |= SEC_COFF_SHARED_LIBRARY;
418 else
420 sec_flags |= SEC_ALLOC | SEC_LOAD;
423 return sec_flags;
426 /* Read in the symbolic header for an ECOFF object file. */
428 static boolean
429 ecoff_slurp_symbolic_header (abfd)
430 bfd *abfd;
432 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
433 bfd_size_type external_hdr_size;
434 PTR raw = NULL;
435 HDRR *internal_symhdr;
437 /* See if we've already read it in. */
438 if (ecoff_data (abfd)->debug_info.symbolic_header.magic ==
439 backend->debug_swap.sym_magic)
440 return true;
442 /* See whether there is a symbolic header. */
443 if (ecoff_data (abfd)->sym_filepos == 0)
445 bfd_get_symcount (abfd) = 0;
446 return true;
449 /* At this point bfd_get_symcount (abfd) holds the number of symbols
450 as read from the file header, but on ECOFF this is always the
451 size of the symbolic information header. It would be cleaner to
452 handle this when we first read the file in coffgen.c. */
453 external_hdr_size = backend->debug_swap.external_hdr_size;
454 if (bfd_get_symcount (abfd) != external_hdr_size)
456 bfd_set_error (bfd_error_bad_value);
457 return false;
460 /* Read the symbolic information header. */
461 raw = (PTR) bfd_malloc ((size_t) external_hdr_size);
462 if (raw == NULL)
463 goto error_return;
465 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
466 || (bfd_read (raw, external_hdr_size, 1, abfd)
467 != external_hdr_size))
468 goto error_return;
469 internal_symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
470 (*backend->debug_swap.swap_hdr_in) (abfd, raw, internal_symhdr);
472 if (internal_symhdr->magic != backend->debug_swap.sym_magic)
474 bfd_set_error (bfd_error_bad_value);
475 goto error_return;
478 /* Now we can get the correct number of symbols. */
479 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
480 + internal_symhdr->iextMax);
482 if (raw != NULL)
483 free (raw);
484 return true;
485 error_return:
486 if (raw != NULL)
487 free (raw);
488 return false;
491 /* Read in and swap the important symbolic information for an ECOFF
492 object file. This is called by gdb via the read_debug_info entry
493 point in the backend structure. */
495 /*ARGSUSED*/
496 boolean
497 _bfd_ecoff_slurp_symbolic_info (abfd, ignore, debug)
498 bfd *abfd;
499 asection *ignore;
500 struct ecoff_debug_info *debug;
502 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
503 HDRR *internal_symhdr;
504 bfd_size_type raw_base;
505 bfd_size_type raw_size;
506 PTR raw;
507 bfd_size_type external_fdr_size;
508 char *fraw_src;
509 char *fraw_end;
510 struct fdr *fdr_ptr;
511 bfd_size_type raw_end;
512 bfd_size_type cb_end;
514 BFD_ASSERT (debug == &ecoff_data (abfd)->debug_info);
516 /* Check whether we've already gotten it, and whether there's any to
517 get. */
518 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
519 return true;
520 if (ecoff_data (abfd)->sym_filepos == 0)
522 bfd_get_symcount (abfd) = 0;
523 return true;
526 if (! ecoff_slurp_symbolic_header (abfd))
527 return false;
529 internal_symhdr = &debug->symbolic_header;
531 /* Read all the symbolic information at once. */
532 raw_base = (ecoff_data (abfd)->sym_filepos
533 + backend->debug_swap.external_hdr_size);
535 /* Alpha ecoff makes the determination of raw_size difficult. It has
536 an undocumented debug data section between the symhdr and the first
537 documented section. And the ordering of the sections varies between
538 statically and dynamically linked executables.
539 If bfd supports SEEK_END someday, this code could be simplified. */
541 raw_end = 0;
543 #define UPDATE_RAW_END(start, count, size) \
544 cb_end = internal_symhdr->start + internal_symhdr->count * (size); \
545 if (cb_end > raw_end) \
546 raw_end = cb_end
548 UPDATE_RAW_END (cbLineOffset, cbLine, sizeof (unsigned char));
549 UPDATE_RAW_END (cbDnOffset, idnMax, backend->debug_swap.external_dnr_size);
550 UPDATE_RAW_END (cbPdOffset, ipdMax, backend->debug_swap.external_pdr_size);
551 UPDATE_RAW_END (cbSymOffset, isymMax, backend->debug_swap.external_sym_size);
552 UPDATE_RAW_END (cbOptOffset, ioptMax, backend->debug_swap.external_opt_size);
553 UPDATE_RAW_END (cbAuxOffset, iauxMax, sizeof (union aux_ext));
554 UPDATE_RAW_END (cbSsOffset, issMax, sizeof (char));
555 UPDATE_RAW_END (cbSsExtOffset, issExtMax, sizeof (char));
556 UPDATE_RAW_END (cbFdOffset, ifdMax, backend->debug_swap.external_fdr_size);
557 UPDATE_RAW_END (cbRfdOffset, crfd, backend->debug_swap.external_rfd_size);
558 UPDATE_RAW_END (cbExtOffset, iextMax, backend->debug_swap.external_ext_size);
560 #undef UPDATE_RAW_END
562 raw_size = raw_end - raw_base;
563 if (raw_size == 0)
565 ecoff_data (abfd)->sym_filepos = 0;
566 return true;
568 raw = (PTR) bfd_alloc (abfd, raw_size);
569 if (raw == NULL)
570 return false;
571 if (bfd_seek (abfd,
572 (ecoff_data (abfd)->sym_filepos
573 + backend->debug_swap.external_hdr_size),
574 SEEK_SET) != 0
575 || bfd_read (raw, raw_size, 1, abfd) != raw_size)
577 bfd_release (abfd, raw);
578 return false;
581 ecoff_data (abfd)->raw_syments = raw;
583 /* Get pointers for the numeric offsets in the HDRR structure. */
584 #define FIX(off1, off2, type) \
585 if (internal_symhdr->off1 == 0) \
586 debug->off2 = (type) NULL; \
587 else \
588 debug->off2 = (type) ((char *) raw \
589 + (internal_symhdr->off1 \
590 - raw_base))
591 FIX (cbLineOffset, line, unsigned char *);
592 FIX (cbDnOffset, external_dnr, PTR);
593 FIX (cbPdOffset, external_pdr, PTR);
594 FIX (cbSymOffset, external_sym, PTR);
595 FIX (cbOptOffset, external_opt, PTR);
596 FIX (cbAuxOffset, external_aux, union aux_ext *);
597 FIX (cbSsOffset, ss, char *);
598 FIX (cbSsExtOffset, ssext, char *);
599 FIX (cbFdOffset, external_fdr, PTR);
600 FIX (cbRfdOffset, external_rfd, PTR);
601 FIX (cbExtOffset, external_ext, PTR);
602 #undef FIX
604 /* I don't want to always swap all the data, because it will just
605 waste time and most programs will never look at it. The only
606 time the linker needs most of the debugging information swapped
607 is when linking big-endian and little-endian MIPS object files
608 together, which is not a common occurrence.
610 We need to look at the fdr to deal with a lot of information in
611 the symbols, so we swap them here. */
612 debug->fdr = (struct fdr *) bfd_alloc (abfd,
613 (internal_symhdr->ifdMax *
614 sizeof (struct fdr)));
615 if (debug->fdr == NULL)
616 return false;
617 external_fdr_size = backend->debug_swap.external_fdr_size;
618 fdr_ptr = debug->fdr;
619 fraw_src = (char *) debug->external_fdr;
620 fraw_end = fraw_src + internal_symhdr->ifdMax * external_fdr_size;
621 for (; fraw_src < fraw_end; fraw_src += external_fdr_size, fdr_ptr++)
622 (*backend->debug_swap.swap_fdr_in) (abfd, (PTR) fraw_src, fdr_ptr);
624 return true;
627 /* ECOFF symbol table routines. The ECOFF symbol table is described
628 in gcc/mips-tfile.c. */
630 /* ECOFF uses two common sections. One is the usual one, and the
631 other is for small objects. All the small objects are kept
632 together, and then referenced via the gp pointer, which yields
633 faster assembler code. This is what we use for the small common
634 section. */
635 static asection ecoff_scom_section;
636 static asymbol ecoff_scom_symbol;
637 static asymbol *ecoff_scom_symbol_ptr;
639 /* Create an empty symbol. */
641 asymbol *
642 _bfd_ecoff_make_empty_symbol (abfd)
643 bfd *abfd;
645 ecoff_symbol_type *new;
647 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
648 if (new == (ecoff_symbol_type *) NULL)
649 return (asymbol *) NULL;
650 memset ((PTR) new, 0, sizeof *new);
651 new->symbol.section = (asection *) NULL;
652 new->fdr = (FDR *) NULL;
653 new->local = false;
654 new->native = NULL;
655 new->symbol.the_bfd = abfd;
656 return &new->symbol;
659 /* Set the BFD flags and section for an ECOFF symbol. */
661 static boolean
662 ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext, weak)
663 bfd *abfd;
664 SYMR *ecoff_sym;
665 asymbol *asym;
666 int ext;
667 int weak;
669 asym->the_bfd = abfd;
670 asym->value = ecoff_sym->value;
671 asym->section = &bfd_debug_section;
672 asym->udata.i = 0;
674 /* Most symbol types are just for debugging. */
675 switch (ecoff_sym->st)
677 case stGlobal:
678 case stStatic:
679 case stLabel:
680 case stProc:
681 case stStaticProc:
682 break;
683 case stNil:
684 if (ECOFF_IS_STAB (ecoff_sym))
686 asym->flags = BSF_DEBUGGING;
687 return true;
689 break;
690 default:
691 asym->flags = BSF_DEBUGGING;
692 return true;
695 if (weak)
696 asym->flags = BSF_EXPORT | BSF_WEAK;
697 else if (ext)
698 asym->flags = BSF_EXPORT | BSF_GLOBAL;
699 else
701 asym->flags = BSF_LOCAL;
702 /* Normally, a local stProc symbol will have a corresponding
703 external symbol. We mark the local symbol as a debugging
704 symbol, in order to prevent nm from printing both out.
705 Similarly, we mark stLabel and stabs symbols as debugging
706 symbols. In both cases, we do want to set the value
707 correctly based on the symbol class. */
708 if (ecoff_sym->st == stProc
709 || ecoff_sym->st == stLabel
710 || ECOFF_IS_STAB (ecoff_sym))
711 asym->flags |= BSF_DEBUGGING;
713 switch (ecoff_sym->sc)
715 case scNil:
716 /* Used for compiler generated labels. Leave them in the
717 debugging section, and mark them as local. If BSF_DEBUGGING
718 is set, then nm does not display them for some reason. If no
719 flags are set then the linker whines about them. */
720 asym->flags = BSF_LOCAL;
721 break;
722 case scText:
723 asym->section = bfd_make_section_old_way (abfd, ".text");
724 asym->value -= asym->section->vma;
725 break;
726 case scData:
727 asym->section = bfd_make_section_old_way (abfd, ".data");
728 asym->value -= asym->section->vma;
729 break;
730 case scBss:
731 asym->section = bfd_make_section_old_way (abfd, ".bss");
732 asym->value -= asym->section->vma;
733 break;
734 case scRegister:
735 asym->flags = BSF_DEBUGGING;
736 break;
737 case scAbs:
738 asym->section = bfd_abs_section_ptr;
739 break;
740 case scUndefined:
741 asym->section = bfd_und_section_ptr;
742 asym->flags = 0;
743 asym->value = 0;
744 break;
745 case scCdbLocal:
746 case scBits:
747 case scCdbSystem:
748 case scRegImage:
749 case scInfo:
750 case scUserStruct:
751 asym->flags = BSF_DEBUGGING;
752 break;
753 case scSData:
754 asym->section = bfd_make_section_old_way (abfd, ".sdata");
755 asym->value -= asym->section->vma;
756 break;
757 case scSBss:
758 asym->section = bfd_make_section_old_way (abfd, ".sbss");
759 asym->value -= asym->section->vma;
760 break;
761 case scRData:
762 asym->section = bfd_make_section_old_way (abfd, ".rdata");
763 asym->value -= asym->section->vma;
764 break;
765 case scVar:
766 asym->flags = BSF_DEBUGGING;
767 break;
768 case scCommon:
769 if (asym->value > ecoff_data (abfd)->gp_size)
771 asym->section = bfd_com_section_ptr;
772 asym->flags = 0;
773 break;
775 /* Fall through. */
776 case scSCommon:
777 if (ecoff_scom_section.name == NULL)
779 /* Initialize the small common section. */
780 ecoff_scom_section.name = SCOMMON;
781 ecoff_scom_section.flags = SEC_IS_COMMON;
782 ecoff_scom_section.output_section = &ecoff_scom_section;
783 ecoff_scom_section.symbol = &ecoff_scom_symbol;
784 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
785 ecoff_scom_symbol.name = SCOMMON;
786 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
787 ecoff_scom_symbol.section = &ecoff_scom_section;
788 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
790 asym->section = &ecoff_scom_section;
791 asym->flags = 0;
792 break;
793 case scVarRegister:
794 case scVariant:
795 asym->flags = BSF_DEBUGGING;
796 break;
797 case scSUndefined:
798 asym->section = bfd_und_section_ptr;
799 asym->flags = 0;
800 asym->value = 0;
801 break;
802 case scInit:
803 asym->section = bfd_make_section_old_way (abfd, ".init");
804 asym->value -= asym->section->vma;
805 break;
806 case scBasedVar:
807 case scXData:
808 case scPData:
809 asym->flags = BSF_DEBUGGING;
810 break;
811 case scFini:
812 asym->section = bfd_make_section_old_way (abfd, ".fini");
813 asym->value -= asym->section->vma;
814 break;
815 case scRConst:
816 asym->section = bfd_make_section_old_way (abfd, ".rconst");
817 asym->value -= asym->section->vma;
818 break;
819 default:
820 break;
823 /* Look for special constructors symbols and make relocation entries
824 in a special construction section. These are produced by the
825 -fgnu-linker argument to g++. */
826 if (ECOFF_IS_STAB (ecoff_sym))
828 switch (ECOFF_UNMARK_STAB (ecoff_sym->index))
830 default:
831 break;
833 case N_SETA:
834 case N_SETT:
835 case N_SETD:
836 case N_SETB:
838 /* This code is no longer needed. It used to be used to
839 make the linker handle set symbols, but they are now
840 handled in the add_symbols routine instead. */
841 #if 0
842 const char *name;
843 asection *section;
844 arelent_chain *reloc_chain;
845 unsigned int bitsize;
847 /* Get a section with the same name as the symbol (usually
848 __CTOR_LIST__ or __DTOR_LIST__). FIXME: gcc uses the
849 name ___CTOR_LIST (three underscores). We need
850 __CTOR_LIST (two underscores), since ECOFF doesn't use
851 a leading underscore. This should be handled by gcc,
852 but instead we do it here. Actually, this should all
853 be done differently anyhow. */
854 name = bfd_asymbol_name (asym);
855 if (name[0] == '_' && name[1] == '_' && name[2] == '_')
857 ++name;
858 asym->name = name;
860 section = bfd_get_section_by_name (abfd, name);
861 if (section == (asection *) NULL)
863 char *copy;
865 copy = (char *) bfd_alloc (abfd, strlen (name) + 1);
866 if (!copy)
867 return false;
868 strcpy (copy, name);
869 section = bfd_make_section (abfd, copy);
872 /* Build a reloc pointing to this constructor. */
873 reloc_chain =
874 (arelent_chain *) bfd_alloc (abfd, sizeof (arelent_chain));
875 if (!reloc_chain)
876 return false;
877 reloc_chain->relent.sym_ptr_ptr =
878 bfd_get_section (asym)->symbol_ptr_ptr;
879 reloc_chain->relent.address = section->_raw_size;
880 reloc_chain->relent.addend = asym->value;
881 reloc_chain->relent.howto =
882 ecoff_backend (abfd)->constructor_reloc;
884 /* Set up the constructor section to hold the reloc. */
885 section->flags = SEC_CONSTRUCTOR;
886 ++section->reloc_count;
888 /* Constructor sections must be rounded to a boundary
889 based on the bitsize. These are not real sections--
890 they are handled specially by the linker--so the ECOFF
891 16 byte alignment restriction does not apply. */
892 bitsize = ecoff_backend (abfd)->constructor_bitsize;
893 section->alignment_power = 1;
894 while ((1 << section->alignment_power) < bitsize / 8)
895 ++section->alignment_power;
897 reloc_chain->next = section->constructor_chain;
898 section->constructor_chain = reloc_chain;
899 section->_raw_size += bitsize / 8;
901 #endif /* 0 */
903 /* Mark the symbol as a constructor. */
904 asym->flags |= BSF_CONSTRUCTOR;
906 break;
909 return true;
912 /* Read an ECOFF symbol table. */
914 boolean
915 _bfd_ecoff_slurp_symbol_table (abfd)
916 bfd *abfd;
918 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
919 const bfd_size_type external_ext_size
920 = backend->debug_swap.external_ext_size;
921 const bfd_size_type external_sym_size
922 = backend->debug_swap.external_sym_size;
923 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
924 = backend->debug_swap.swap_ext_in;
925 void (* const swap_sym_in) PARAMS ((bfd *, PTR, SYMR *))
926 = backend->debug_swap.swap_sym_in;
927 bfd_size_type internal_size;
928 ecoff_symbol_type *internal;
929 ecoff_symbol_type *internal_ptr;
930 char *eraw_src;
931 char *eraw_end;
932 FDR *fdr_ptr;
933 FDR *fdr_end;
935 /* If we've already read in the symbol table, do nothing. */
936 if (ecoff_data (abfd)->canonical_symbols != NULL)
937 return true;
939 /* Get the symbolic information. */
940 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
941 &ecoff_data (abfd)->debug_info))
942 return false;
943 if (bfd_get_symcount (abfd) == 0)
944 return true;
946 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
947 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
948 if (internal == NULL)
949 return false;
951 internal_ptr = internal;
952 eraw_src = (char *) ecoff_data (abfd)->debug_info.external_ext;
953 eraw_end = (eraw_src
954 + (ecoff_data (abfd)->debug_info.symbolic_header.iextMax
955 * external_ext_size));
956 for (; eraw_src < eraw_end; eraw_src += external_ext_size, internal_ptr++)
958 EXTR internal_esym;
960 (*swap_ext_in) (abfd, (PTR) eraw_src, &internal_esym);
961 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ssext
962 + internal_esym.asym.iss);
963 if (!ecoff_set_symbol_info (abfd, &internal_esym.asym,
964 &internal_ptr->symbol, 1,
965 internal_esym.weakext))
966 return false;
967 /* The alpha uses a negative ifd field for section symbols. */
968 if (internal_esym.ifd >= 0)
969 internal_ptr->fdr = (ecoff_data (abfd)->debug_info.fdr
970 + internal_esym.ifd);
971 else
972 internal_ptr->fdr = NULL;
973 internal_ptr->local = false;
974 internal_ptr->native = (PTR) eraw_src;
977 /* The local symbols must be accessed via the fdr's, because the
978 string and aux indices are relative to the fdr information. */
979 fdr_ptr = ecoff_data (abfd)->debug_info.fdr;
980 fdr_end = fdr_ptr + ecoff_data (abfd)->debug_info.symbolic_header.ifdMax;
981 for (; fdr_ptr < fdr_end; fdr_ptr++)
983 char *lraw_src;
984 char *lraw_end;
986 lraw_src = ((char *) ecoff_data (abfd)->debug_info.external_sym
987 + fdr_ptr->isymBase * external_sym_size);
988 lraw_end = lraw_src + fdr_ptr->csym * external_sym_size;
989 for (;
990 lraw_src < lraw_end;
991 lraw_src += external_sym_size, internal_ptr++)
993 SYMR internal_sym;
995 (*swap_sym_in) (abfd, (PTR) lraw_src, &internal_sym);
996 internal_ptr->symbol.name = (ecoff_data (abfd)->debug_info.ss
997 + fdr_ptr->issBase
998 + internal_sym.iss);
999 if (!ecoff_set_symbol_info (abfd, &internal_sym,
1000 &internal_ptr->symbol, 0, 0))
1001 return false;
1002 internal_ptr->fdr = fdr_ptr;
1003 internal_ptr->local = true;
1004 internal_ptr->native = (PTR) lraw_src;
1008 ecoff_data (abfd)->canonical_symbols = internal;
1010 return true;
1013 /* Return the amount of space needed for the canonical symbols. */
1015 long
1016 _bfd_ecoff_get_symtab_upper_bound (abfd)
1017 bfd *abfd;
1019 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL,
1020 &ecoff_data (abfd)->debug_info))
1021 return -1;
1023 if (bfd_get_symcount (abfd) == 0)
1024 return 0;
1026 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
1029 /* Get the canonical symbols. */
1031 long
1032 _bfd_ecoff_get_symtab (abfd, alocation)
1033 bfd *abfd;
1034 asymbol **alocation;
1036 unsigned int counter = 0;
1037 ecoff_symbol_type *symbase;
1038 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
1040 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1041 return -1;
1042 if (bfd_get_symcount (abfd) == 0)
1043 return 0;
1045 symbase = ecoff_data (abfd)->canonical_symbols;
1046 while (counter < bfd_get_symcount (abfd))
1048 *(location++) = symbase++;
1049 counter++;
1051 *location++ = (ecoff_symbol_type *) NULL;
1052 return bfd_get_symcount (abfd);
1055 /* Turn ECOFF type information into a printable string.
1056 ecoff_emit_aggregate and ecoff_type_to_string are from
1057 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
1059 /* Write aggregate information to a string. */
1061 static void
1062 ecoff_emit_aggregate (abfd, fdr, string, rndx, isym, which)
1063 bfd *abfd;
1064 FDR *fdr;
1065 char *string;
1066 RNDXR *rndx;
1067 long isym;
1068 const char *which;
1070 const struct ecoff_debug_swap * const debug_swap =
1071 &ecoff_backend (abfd)->debug_swap;
1072 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1073 unsigned int ifd = rndx->rfd;
1074 unsigned int indx = rndx->index;
1075 const char *name;
1077 if (ifd == 0xfff)
1078 ifd = isym;
1080 /* An ifd of -1 is an opaque type. An escaped index of 0 is a
1081 struct return type of a procedure compiled without -g. */
1082 if (ifd == 0xffffffff
1083 || (rndx->rfd == 0xfff && indx == 0))
1084 name = "<undefined>";
1085 else if (indx == indexNil)
1086 name = "<no name>";
1087 else
1089 SYMR sym;
1091 if (debug_info->external_rfd == NULL)
1092 fdr = debug_info->fdr + ifd;
1093 else
1095 RFDT rfd;
1097 (*debug_swap->swap_rfd_in) (abfd,
1098 ((char *) debug_info->external_rfd
1099 + ((fdr->rfdBase + ifd)
1100 * debug_swap->external_rfd_size)),
1101 &rfd);
1102 fdr = debug_info->fdr + rfd;
1105 indx += fdr->isymBase;
1107 (*debug_swap->swap_sym_in) (abfd,
1108 ((char *) debug_info->external_sym
1109 + indx * debug_swap->external_sym_size),
1110 &sym);
1112 name = debug_info->ss + fdr->issBase + sym.iss;
1115 sprintf (string,
1116 "%s %s { ifd = %u, index = %lu }",
1117 which, name, ifd,
1118 ((long) indx
1119 + debug_info->symbolic_header.iextMax));
1122 /* Convert the type information to string format. */
1124 static char *
1125 ecoff_type_to_string (abfd, fdr, indx)
1126 bfd *abfd;
1127 FDR *fdr;
1128 unsigned int indx;
1130 union aux_ext *aux_ptr;
1131 int bigendian;
1132 AUXU u;
1133 struct qual {
1134 unsigned int type;
1135 int low_bound;
1136 int high_bound;
1137 int stride;
1138 } qualifiers[7];
1139 unsigned int basic_type;
1140 int i;
1141 char buffer1[1024];
1142 static char buffer2[1024];
1143 char *p1 = buffer1;
1144 char *p2 = buffer2;
1145 RNDXR rndx;
1147 aux_ptr = ecoff_data (abfd)->debug_info.external_aux + fdr->iauxBase;
1148 bigendian = fdr->fBigendian;
1150 for (i = 0; i < 7; i++)
1152 qualifiers[i].low_bound = 0;
1153 qualifiers[i].high_bound = 0;
1154 qualifiers[i].stride = 0;
1157 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == (bfd_vma) -1)
1158 return "-1 (no type)";
1159 _bfd_ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1161 basic_type = u.ti.bt;
1162 qualifiers[0].type = u.ti.tq0;
1163 qualifiers[1].type = u.ti.tq1;
1164 qualifiers[2].type = u.ti.tq2;
1165 qualifiers[3].type = u.ti.tq3;
1166 qualifiers[4].type = u.ti.tq4;
1167 qualifiers[5].type = u.ti.tq5;
1168 qualifiers[6].type = tqNil;
1171 * Go get the basic type.
1173 switch (basic_type)
1175 case btNil: /* undefined */
1176 strcpy (p1, "nil");
1177 break;
1179 case btAdr: /* address - integer same size as pointer */
1180 strcpy (p1, "address");
1181 break;
1183 case btChar: /* character */
1184 strcpy (p1, "char");
1185 break;
1187 case btUChar: /* unsigned character */
1188 strcpy (p1, "unsigned char");
1189 break;
1191 case btShort: /* short */
1192 strcpy (p1, "short");
1193 break;
1195 case btUShort: /* unsigned short */
1196 strcpy (p1, "unsigned short");
1197 break;
1199 case btInt: /* int */
1200 strcpy (p1, "int");
1201 break;
1203 case btUInt: /* unsigned int */
1204 strcpy (p1, "unsigned int");
1205 break;
1207 case btLong: /* long */
1208 strcpy (p1, "long");
1209 break;
1211 case btULong: /* unsigned long */
1212 strcpy (p1, "unsigned long");
1213 break;
1215 case btFloat: /* float (real) */
1216 strcpy (p1, "float");
1217 break;
1219 case btDouble: /* Double (real) */
1220 strcpy (p1, "double");
1221 break;
1223 /* Structures add 1-2 aux words:
1224 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1225 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1227 case btStruct: /* Structure (Record) */
1228 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1229 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1230 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1231 "struct");
1232 indx++; /* skip aux words */
1233 break;
1235 /* Unions add 1-2 aux words:
1236 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1237 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1239 case btUnion: /* Union */
1240 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1241 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1242 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1243 "union");
1244 indx++; /* skip aux words */
1245 break;
1247 /* Enumerations add 1-2 aux words:
1248 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1249 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1251 case btEnum: /* Enumeration */
1252 _bfd_ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
1253 ecoff_emit_aggregate (abfd, fdr, p1, &rndx,
1254 (long) AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1255 "enum");
1256 indx++; /* skip aux words */
1257 break;
1259 case btTypedef: /* defined via a typedef, isymRef points */
1260 strcpy (p1, "typedef");
1261 break;
1263 case btRange: /* subrange of int */
1264 strcpy (p1, "subrange");
1265 break;
1267 case btSet: /* pascal sets */
1268 strcpy (p1, "set");
1269 break;
1271 case btComplex: /* fortran complex */
1272 strcpy (p1, "complex");
1273 break;
1275 case btDComplex: /* fortran double complex */
1276 strcpy (p1, "double complex");
1277 break;
1279 case btIndirect: /* forward or unnamed typedef */
1280 strcpy (p1, "forward/unamed typedef");
1281 break;
1283 case btFixedDec: /* Fixed Decimal */
1284 strcpy (p1, "fixed decimal");
1285 break;
1287 case btFloatDec: /* Float Decimal */
1288 strcpy (p1, "float decimal");
1289 break;
1291 case btString: /* Varying Length Character String */
1292 strcpy (p1, "string");
1293 break;
1295 case btBit: /* Aligned Bit String */
1296 strcpy (p1, "bit");
1297 break;
1299 case btPicture: /* Picture */
1300 strcpy (p1, "picture");
1301 break;
1303 case btVoid: /* Void */
1304 strcpy (p1, "void");
1305 break;
1307 default:
1308 sprintf (p1, _("Unknown basic type %d"), (int) basic_type);
1309 break;
1312 p1 += strlen (buffer1);
1315 * If this is a bitfield, get the bitsize.
1317 if (u.ti.fBitfield)
1319 int bitsize;
1321 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1322 sprintf (p1, " : %d", bitsize);
1323 p1 += strlen (buffer1);
1328 * Deal with any qualifiers.
1330 if (qualifiers[0].type != tqNil)
1333 * Snarf up any array bounds in the correct order. Arrays
1334 * store 5 successive words in the aux. table:
1335 * word 0 RNDXR to type of the bounds (ie, int)
1336 * word 1 Current file descriptor index
1337 * word 2 low bound
1338 * word 3 high bound (or -1 if [])
1339 * word 4 stride size in bits
1341 for (i = 0; i < 7; i++)
1343 if (qualifiers[i].type == tqArray)
1345 qualifiers[i].low_bound =
1346 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1347 qualifiers[i].high_bound =
1348 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1349 qualifiers[i].stride =
1350 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1351 indx += 5;
1356 * Now print out the qualifiers.
1358 for (i = 0; i < 6; i++)
1360 switch (qualifiers[i].type)
1362 case tqNil:
1363 case tqMax:
1364 break;
1366 case tqPtr:
1367 strcpy (p2, "ptr to ");
1368 p2 += sizeof ("ptr to ")-1;
1369 break;
1371 case tqVol:
1372 strcpy (p2, "volatile ");
1373 p2 += sizeof ("volatile ")-1;
1374 break;
1376 case tqFar:
1377 strcpy (p2, "far ");
1378 p2 += sizeof ("far ")-1;
1379 break;
1381 case tqProc:
1382 strcpy (p2, "func. ret. ");
1383 p2 += sizeof ("func. ret. ");
1384 break;
1386 case tqArray:
1388 int first_array = i;
1389 int j;
1391 /* Print array bounds reversed (ie, in the order the C
1392 programmer writes them). C is such a fun language.... */
1394 while (i < 5 && qualifiers[i+1].type == tqArray)
1395 i++;
1397 for (j = i; j >= first_array; j--)
1399 strcpy (p2, "array [");
1400 p2 += sizeof ("array [")-1;
1401 if (qualifiers[j].low_bound != 0)
1402 sprintf (p2,
1403 "%ld:%ld {%ld bits}",
1404 (long) qualifiers[j].low_bound,
1405 (long) qualifiers[j].high_bound,
1406 (long) qualifiers[j].stride);
1408 else if (qualifiers[j].high_bound != -1)
1409 sprintf (p2,
1410 "%ld {%ld bits}",
1411 (long) (qualifiers[j].high_bound + 1),
1412 (long) (qualifiers[j].stride));
1414 else
1415 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1417 p2 += strlen (p2);
1418 strcpy (p2, "] of ");
1419 p2 += sizeof ("] of ")-1;
1422 break;
1427 strcpy (p2, buffer1);
1428 return buffer2;
1431 /* Return information about ECOFF symbol SYMBOL in RET. */
1433 /*ARGSUSED*/
1434 void
1435 _bfd_ecoff_get_symbol_info (abfd, symbol, ret)
1436 bfd *abfd; /* Ignored. */
1437 asymbol *symbol;
1438 symbol_info *ret;
1440 bfd_symbol_info (symbol, ret);
1443 /* Return whether this is a local label. */
1445 /*ARGSUSED*/
1446 boolean
1447 _bfd_ecoff_bfd_is_local_label_name (abfd, name)
1448 bfd *abfd;
1449 const char *name;
1451 return name[0] == '$';
1454 /* Print information about an ECOFF symbol. */
1456 void
1457 _bfd_ecoff_print_symbol (abfd, filep, symbol, how)
1458 bfd *abfd;
1459 PTR filep;
1460 asymbol *symbol;
1461 bfd_print_symbol_type how;
1463 const struct ecoff_debug_swap * const debug_swap
1464 = &ecoff_backend (abfd)->debug_swap;
1465 FILE *file = (FILE *)filep;
1467 switch (how)
1469 case bfd_print_symbol_name:
1470 fprintf (file, "%s", symbol->name);
1471 break;
1472 case bfd_print_symbol_more:
1473 if (ecoffsymbol (symbol)->local)
1475 SYMR ecoff_sym;
1477 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1478 &ecoff_sym);
1479 fprintf (file, "ecoff local ");
1480 fprintf_vma (file, (bfd_vma) ecoff_sym.value);
1481 fprintf (file, " %x %x", (unsigned) ecoff_sym.st,
1482 (unsigned) ecoff_sym.sc);
1484 else
1486 EXTR ecoff_ext;
1488 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1489 &ecoff_ext);
1490 fprintf (file, "ecoff extern ");
1491 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1492 fprintf (file, " %x %x", (unsigned) ecoff_ext.asym.st,
1493 (unsigned) ecoff_ext.asym.sc);
1495 break;
1496 case bfd_print_symbol_all:
1497 /* Print out the symbols in a reasonable way */
1499 char type;
1500 int pos;
1501 EXTR ecoff_ext;
1502 char jmptbl;
1503 char cobol_main;
1504 char weakext;
1506 if (ecoffsymbol (symbol)->local)
1508 (*debug_swap->swap_sym_in) (abfd, ecoffsymbol (symbol)->native,
1509 &ecoff_ext.asym);
1510 type = 'l';
1511 pos = ((((char *) ecoffsymbol (symbol)->native
1512 - (char *) ecoff_data (abfd)->debug_info.external_sym)
1513 / debug_swap->external_sym_size)
1514 + ecoff_data (abfd)->debug_info.symbolic_header.iextMax);
1515 jmptbl = ' ';
1516 cobol_main = ' ';
1517 weakext = ' ';
1519 else
1521 (*debug_swap->swap_ext_in) (abfd, ecoffsymbol (symbol)->native,
1522 &ecoff_ext);
1523 type = 'e';
1524 pos = (((char *) ecoffsymbol (symbol)->native
1525 - (char *) ecoff_data (abfd)->debug_info.external_ext)
1526 / debug_swap->external_ext_size);
1527 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1528 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1529 weakext = ecoff_ext.weakext ? 'w' : ' ';
1532 fprintf (file, "[%3d] %c ",
1533 pos, type);
1534 fprintf_vma (file, (bfd_vma) ecoff_ext.asym.value);
1535 fprintf (file, " st %x sc %x indx %x %c%c%c %s",
1536 (unsigned) ecoff_ext.asym.st,
1537 (unsigned) ecoff_ext.asym.sc,
1538 (unsigned) ecoff_ext.asym.index,
1539 jmptbl, cobol_main, weakext,
1540 symbol->name);
1542 if (ecoffsymbol (symbol)->fdr != NULL
1543 && ecoff_ext.asym.index != indexNil)
1545 FDR *fdr;
1546 unsigned int indx;
1547 int bigendian;
1548 bfd_size_type sym_base;
1549 union aux_ext *aux_base;
1551 fdr = ecoffsymbol (symbol)->fdr;
1552 indx = ecoff_ext.asym.index;
1554 /* sym_base is used to map the fdr relative indices which
1555 appear in the file to the position number which we are
1556 using. */
1557 sym_base = fdr->isymBase;
1558 if (ecoffsymbol (symbol)->local)
1559 sym_base +=
1560 ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
1562 /* aux_base is the start of the aux entries for this file;
1563 asym.index is an offset from this. */
1564 aux_base = (ecoff_data (abfd)->debug_info.external_aux
1565 + fdr->iauxBase);
1567 /* The aux entries are stored in host byte order; the
1568 order is indicated by a bit in the fdr. */
1569 bigendian = fdr->fBigendian;
1571 /* This switch is basically from gcc/mips-tdump.c */
1572 switch (ecoff_ext.asym.st)
1574 case stNil:
1575 case stLabel:
1576 break;
1578 case stFile:
1579 case stBlock:
1580 fprintf (file, _("\n End+1 symbol: %ld"),
1581 (long) (indx + sym_base));
1582 break;
1584 case stEnd:
1585 if (ecoff_ext.asym.sc == scText
1586 || ecoff_ext.asym.sc == scInfo)
1587 fprintf (file, _("\n First symbol: %ld"),
1588 (long) (indx + sym_base));
1589 else
1590 fprintf (file, _("\n First symbol: %ld"),
1591 ((long)
1592 (AUX_GET_ISYM (bigendian,
1593 &aux_base[ecoff_ext.asym.index])
1594 + sym_base)));
1595 break;
1597 case stProc:
1598 case stStaticProc:
1599 if (ECOFF_IS_STAB (&ecoff_ext.asym))
1601 else if (ecoffsymbol (symbol)->local)
1602 fprintf (file, _("\n End+1 symbol: %-7ld Type: %s"),
1603 ((long)
1604 (AUX_GET_ISYM (bigendian,
1605 &aux_base[ecoff_ext.asym.index])
1606 + sym_base)),
1607 ecoff_type_to_string (abfd, fdr, indx + 1));
1608 else
1609 fprintf (file, _("\n Local symbol: %ld"),
1610 ((long) indx
1611 + (long) sym_base
1612 + (ecoff_data (abfd)
1613 ->debug_info.symbolic_header.iextMax)));
1614 break;
1616 case stStruct:
1617 fprintf (file, _("\n struct; End+1 symbol: %ld"),
1618 (long) (indx + sym_base));
1619 break;
1621 case stUnion:
1622 fprintf (file, _("\n union; End+1 symbol: %ld"),
1623 (long) (indx + sym_base));
1624 break;
1626 case stEnum:
1627 fprintf (file, _("\n enum; End+1 symbol: %ld"),
1628 (long) (indx + sym_base));
1629 break;
1631 default:
1632 if (! ECOFF_IS_STAB (&ecoff_ext.asym))
1633 fprintf (file, _("\n Type: %s"),
1634 ecoff_type_to_string (abfd, fdr, indx));
1635 break;
1639 break;
1643 /* Read in the relocs for a section. */
1645 static boolean
1646 ecoff_slurp_reloc_table (abfd, section, symbols)
1647 bfd *abfd;
1648 asection *section;
1649 asymbol **symbols;
1651 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
1652 arelent *internal_relocs;
1653 bfd_size_type external_reloc_size;
1654 bfd_size_type external_relocs_size;
1655 char *external_relocs;
1656 arelent *rptr;
1657 unsigned int i;
1659 if (section->relocation != (arelent *) NULL
1660 || section->reloc_count == 0
1661 || (section->flags & SEC_CONSTRUCTOR) != 0)
1662 return true;
1664 if (_bfd_ecoff_slurp_symbol_table (abfd) == false)
1665 return false;
1667 internal_relocs = (arelent *) bfd_alloc (abfd,
1668 (sizeof (arelent)
1669 * section->reloc_count));
1670 external_reloc_size = backend->external_reloc_size;
1671 external_relocs_size = external_reloc_size * section->reloc_count;
1672 external_relocs = (char *) bfd_alloc (abfd, external_relocs_size);
1673 if (internal_relocs == (arelent *) NULL
1674 || external_relocs == (char *) NULL)
1675 return false;
1676 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1677 return false;
1678 if (bfd_read (external_relocs, 1, external_relocs_size, abfd)
1679 != external_relocs_size)
1680 return false;
1682 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1684 struct internal_reloc intern;
1686 (*backend->swap_reloc_in) (abfd,
1687 external_relocs + i * external_reloc_size,
1688 &intern);
1690 if (intern.r_extern)
1692 /* r_symndx is an index into the external symbols. */
1693 BFD_ASSERT (intern.r_symndx >= 0
1694 && (intern.r_symndx
1695 < (ecoff_data (abfd)
1696 ->debug_info.symbolic_header.iextMax)));
1697 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
1698 rptr->addend = 0;
1700 else if (intern.r_symndx == RELOC_SECTION_NONE
1701 || intern.r_symndx == RELOC_SECTION_ABS)
1703 rptr->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
1704 rptr->addend = 0;
1706 else
1708 CONST char *sec_name;
1709 asection *sec;
1711 /* r_symndx is a section key. */
1712 switch (intern.r_symndx)
1714 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1715 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1716 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1717 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1718 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1719 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1720 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1721 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1722 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1723 case RELOC_SECTION_XDATA: sec_name = ".xdata"; break;
1724 case RELOC_SECTION_PDATA: sec_name = ".pdata"; break;
1725 case RELOC_SECTION_FINI: sec_name = ".fini"; break;
1726 case RELOC_SECTION_LITA: sec_name = ".lita"; break;
1727 case RELOC_SECTION_RCONST: sec_name = ".rconst"; break;
1728 default: abort ();
1731 sec = bfd_get_section_by_name (abfd, sec_name);
1732 if (sec == (asection *) NULL)
1733 abort ();
1734 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
1736 rptr->addend = - bfd_get_section_vma (abfd, sec);
1739 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
1741 /* Let the backend select the howto field and do any other
1742 required processing. */
1743 (*backend->adjust_reloc_in) (abfd, &intern, rptr);
1746 bfd_release (abfd, external_relocs);
1748 section->relocation = internal_relocs;
1750 return true;
1753 /* Get a canonical list of relocs. */
1755 long
1756 _bfd_ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
1757 bfd *abfd;
1758 asection *section;
1759 arelent **relptr;
1760 asymbol **symbols;
1762 unsigned int count;
1764 if (section->flags & SEC_CONSTRUCTOR)
1766 arelent_chain *chain;
1768 /* This section has relocs made up by us, not the file, so take
1769 them out of their chain and place them into the data area
1770 provided. */
1771 for (count = 0, chain = section->constructor_chain;
1772 count < section->reloc_count;
1773 count++, chain = chain->next)
1774 *relptr++ = &chain->relent;
1776 else
1778 arelent *tblptr;
1780 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
1781 return -1;
1783 tblptr = section->relocation;
1785 for (count = 0; count < section->reloc_count; count++)
1786 *relptr++ = tblptr++;
1789 *relptr = (arelent *) NULL;
1791 return section->reloc_count;
1794 /* Provided a BFD, a section and an offset into the section, calculate
1795 and return the name of the source file and the line nearest to the
1796 wanted location. */
1798 /*ARGSUSED*/
1799 boolean
1800 _bfd_ecoff_find_nearest_line (abfd, section, ignore_symbols, offset,
1801 filename_ptr, functionname_ptr, retline_ptr)
1802 bfd *abfd;
1803 asection *section;
1804 asymbol **ignore_symbols;
1805 bfd_vma offset;
1806 CONST char **filename_ptr;
1807 CONST char **functionname_ptr;
1808 unsigned int *retline_ptr;
1810 const struct ecoff_debug_swap * const debug_swap
1811 = &ecoff_backend (abfd)->debug_swap;
1812 struct ecoff_debug_info * const debug_info = &ecoff_data (abfd)->debug_info;
1813 struct ecoff_find_line *line_info;
1815 /* Make sure we have the FDR's. */
1816 if (! _bfd_ecoff_slurp_symbolic_info (abfd, (asection *) NULL, debug_info)
1817 || bfd_get_symcount (abfd) == 0)
1818 return false;
1820 if (ecoff_data (abfd)->find_line_info == NULL)
1822 ecoff_data (abfd)->find_line_info =
1823 ((struct ecoff_find_line *)
1824 bfd_zalloc (abfd, sizeof (struct ecoff_find_line)));
1825 if (ecoff_data (abfd)->find_line_info == NULL)
1826 return false;
1828 line_info = ecoff_data (abfd)->find_line_info;
1830 return _bfd_ecoff_locate_line (abfd, section, offset, debug_info,
1831 debug_swap, line_info, filename_ptr,
1832 functionname_ptr, retline_ptr);
1835 /* Copy private BFD data. This is called by objcopy and strip. We
1836 use it to copy the ECOFF debugging information from one BFD to the
1837 other. It would be theoretically possible to represent the ECOFF
1838 debugging information in the symbol table. However, it would be a
1839 lot of work, and there would be little gain (gas, gdb, and ld
1840 already access the ECOFF debugging information via the
1841 ecoff_debug_info structure, and that structure would have to be
1842 retained in order to support ECOFF debugging in MIPS ELF).
1844 The debugging information for the ECOFF external symbols comes from
1845 the symbol table, so this function only handles the other debugging
1846 information. */
1848 boolean
1849 _bfd_ecoff_bfd_copy_private_bfd_data (ibfd, obfd)
1850 bfd *ibfd;
1851 bfd *obfd;
1853 struct ecoff_debug_info *iinfo = &ecoff_data (ibfd)->debug_info;
1854 struct ecoff_debug_info *oinfo = &ecoff_data (obfd)->debug_info;
1855 register int i;
1856 asymbol **sym_ptr_ptr;
1857 size_t c;
1858 boolean local;
1860 /* We only want to copy information over if both BFD's use ECOFF
1861 format. */
1862 if (bfd_get_flavour (ibfd) != bfd_target_ecoff_flavour
1863 || bfd_get_flavour (obfd) != bfd_target_ecoff_flavour)
1864 return true;
1866 /* Copy the GP value and the register masks. */
1867 ecoff_data (obfd)->gp = ecoff_data (ibfd)->gp;
1868 ecoff_data (obfd)->gprmask = ecoff_data (ibfd)->gprmask;
1869 ecoff_data (obfd)->fprmask = ecoff_data (ibfd)->fprmask;
1870 for (i = 0; i < 3; i++)
1871 ecoff_data (obfd)->cprmask[i] = ecoff_data (ibfd)->cprmask[i];
1873 /* Copy the version stamp. */
1874 oinfo->symbolic_header.vstamp = iinfo->symbolic_header.vstamp;
1876 /* If there are no symbols, don't copy any debugging information. */
1877 c = bfd_get_symcount (obfd);
1878 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1879 if (c == 0 || sym_ptr_ptr == (asymbol **) NULL)
1880 return true;
1882 /* See if there are any local symbols. */
1883 local = false;
1884 for (; c > 0; c--, sym_ptr_ptr++)
1886 if (ecoffsymbol (*sym_ptr_ptr)->local)
1888 local = true;
1889 break;
1893 if (local)
1895 /* There are some local symbols. We just bring over all the
1896 debugging information. FIXME: This is not quite the right
1897 thing to do. If the user has asked us to discard all
1898 debugging information, then we are probably going to wind up
1899 keeping it because there will probably be some local symbol
1900 which objcopy did not discard. We should actually break
1901 apart the debugging information and only keep that which
1902 applies to the symbols we want to keep. */
1903 oinfo->symbolic_header.ilineMax = iinfo->symbolic_header.ilineMax;
1904 oinfo->symbolic_header.cbLine = iinfo->symbolic_header.cbLine;
1905 oinfo->line = iinfo->line;
1907 oinfo->symbolic_header.idnMax = iinfo->symbolic_header.idnMax;
1908 oinfo->external_dnr = iinfo->external_dnr;
1910 oinfo->symbolic_header.ipdMax = iinfo->symbolic_header.ipdMax;
1911 oinfo->external_pdr = iinfo->external_pdr;
1913 oinfo->symbolic_header.isymMax = iinfo->symbolic_header.isymMax;
1914 oinfo->external_sym = iinfo->external_sym;
1916 oinfo->symbolic_header.ioptMax = iinfo->symbolic_header.ioptMax;
1917 oinfo->external_opt = iinfo->external_opt;
1919 oinfo->symbolic_header.iauxMax = iinfo->symbolic_header.iauxMax;
1920 oinfo->external_aux = iinfo->external_aux;
1922 oinfo->symbolic_header.issMax = iinfo->symbolic_header.issMax;
1923 oinfo->ss = iinfo->ss;
1925 oinfo->symbolic_header.ifdMax = iinfo->symbolic_header.ifdMax;
1926 oinfo->external_fdr = iinfo->external_fdr;
1928 oinfo->symbolic_header.crfd = iinfo->symbolic_header.crfd;
1929 oinfo->external_rfd = iinfo->external_rfd;
1931 else
1933 /* We are discarding all the local symbol information. Look
1934 through the external symbols and remove all references to FDR
1935 or aux information. */
1936 c = bfd_get_symcount (obfd);
1937 sym_ptr_ptr = bfd_get_outsymbols (obfd);
1938 for (; c > 0; c--, sym_ptr_ptr++)
1940 EXTR esym;
1942 (*(ecoff_backend (obfd)->debug_swap.swap_ext_in))
1943 (obfd, ecoffsymbol (*sym_ptr_ptr)->native, &esym);
1944 esym.ifd = ifdNil;
1945 esym.asym.index = indexNil;
1946 (*(ecoff_backend (obfd)->debug_swap.swap_ext_out))
1947 (obfd, &esym, ecoffsymbol (*sym_ptr_ptr)->native);
1951 return true;
1954 /* Set the architecture. The supported architecture is stored in the
1955 backend pointer. We always set the architecture anyhow, since many
1956 callers ignore the return value. */
1958 boolean
1959 _bfd_ecoff_set_arch_mach (abfd, arch, machine)
1960 bfd *abfd;
1961 enum bfd_architecture arch;
1962 unsigned long machine;
1964 bfd_default_set_arch_mach (abfd, arch, machine);
1965 return arch == ecoff_backend (abfd)->arch;
1968 /* Get the size of the section headers. */
1970 /*ARGSUSED*/
1972 _bfd_ecoff_sizeof_headers (abfd, reloc)
1973 bfd *abfd;
1974 boolean reloc;
1976 asection *current;
1977 int c;
1978 int ret;
1980 c = 0;
1981 for (current = abfd->sections;
1982 current != (asection *)NULL;
1983 current = current->next)
1984 ++c;
1986 ret = (bfd_coff_filhsz (abfd)
1987 + bfd_coff_aoutsz (abfd)
1988 + c * bfd_coff_scnhsz (abfd));
1989 return BFD_ALIGN (ret, 16);
1992 /* Get the contents of a section. */
1994 boolean
1995 _bfd_ecoff_get_section_contents (abfd, section, location, offset, count)
1996 bfd *abfd;
1997 asection *section;
1998 PTR location;
1999 file_ptr offset;
2000 bfd_size_type count;
2002 return _bfd_generic_get_section_contents (abfd, section, location,
2003 offset, count);
2006 /* Sort sections by VMA, but put SEC_ALLOC sections first. This is
2007 called via qsort. */
2009 static int
2010 ecoff_sort_hdrs (arg1, arg2)
2011 const PTR arg1;
2012 const PTR arg2;
2014 const asection *hdr1 = *(const asection **) arg1;
2015 const asection *hdr2 = *(const asection **) arg2;
2017 if ((hdr1->flags & SEC_ALLOC) != 0)
2019 if ((hdr2->flags & SEC_ALLOC) == 0)
2020 return -1;
2022 else
2024 if ((hdr2->flags & SEC_ALLOC) != 0)
2025 return 1;
2027 if (hdr1->vma < hdr2->vma)
2028 return -1;
2029 else if (hdr1->vma > hdr2->vma)
2030 return 1;
2031 else
2032 return 0;
2035 /* Calculate the file position for each section, and set
2036 reloc_filepos. */
2038 static boolean
2039 ecoff_compute_section_file_positions (abfd)
2040 bfd *abfd;
2042 file_ptr sofar, file_sofar;
2043 asection **sorted_hdrs;
2044 asection *current;
2045 unsigned int i;
2046 file_ptr old_sofar;
2047 boolean rdata_in_text;
2048 boolean first_data, first_nonalloc;
2049 const bfd_vma round = ecoff_backend (abfd)->round;
2051 sofar = _bfd_ecoff_sizeof_headers (abfd, false);
2052 file_sofar = sofar;
2054 /* Sort the sections by VMA. */
2055 sorted_hdrs = (asection **) bfd_malloc (abfd->section_count
2056 * sizeof (asection *));
2057 if (sorted_hdrs == NULL)
2058 return false;
2059 for (current = abfd->sections, i = 0;
2060 current != NULL;
2061 current = current->next, i++)
2062 sorted_hdrs[i] = current;
2063 BFD_ASSERT (i == abfd->section_count);
2065 qsort (sorted_hdrs, abfd->section_count, sizeof (asection *),
2066 ecoff_sort_hdrs);
2068 /* Some versions of the OSF linker put the .rdata section in the
2069 text segment, and some do not. */
2070 rdata_in_text = ecoff_backend (abfd)->rdata_in_text;
2071 if (rdata_in_text)
2073 for (i = 0; i < abfd->section_count; i++)
2075 current = sorted_hdrs[i];
2076 if (strcmp (current->name, _RDATA) == 0)
2077 break;
2078 if ((current->flags & SEC_CODE) == 0
2079 && strcmp (current->name, _PDATA) != 0
2080 && strcmp (current->name, _RCONST) != 0)
2082 rdata_in_text = false;
2083 break;
2087 ecoff_data (abfd)->rdata_in_text = rdata_in_text;
2089 first_data = true;
2090 first_nonalloc = true;
2091 for (i = 0; i < abfd->section_count; i++)
2093 unsigned int alignment_power;
2095 current = sorted_hdrs[i];
2097 /* For the Alpha ECOFF .pdata section the lnnoptr field is
2098 supposed to indicate the number of .pdata entries that are
2099 really in the section. Each entry is 8 bytes. We store this
2100 away in line_filepos before increasing the section size. */
2101 if (strcmp (current->name, _PDATA) == 0)
2102 current->line_filepos = current->_raw_size / 8;
2104 alignment_power = current->alignment_power;
2106 /* On Ultrix, the data sections in an executable file must be
2107 aligned to a page boundary within the file. This does not
2108 affect the section size, though. FIXME: Does this work for
2109 other platforms? It requires some modification for the
2110 Alpha, because .rdata on the Alpha goes with the text, not
2111 the data. */
2112 if ((abfd->flags & EXEC_P) != 0
2113 && (abfd->flags & D_PAGED) != 0
2114 && ! first_data
2115 && (current->flags & SEC_CODE) == 0
2116 && (! rdata_in_text
2117 || strcmp (current->name, _RDATA) != 0)
2118 && strcmp (current->name, _PDATA) != 0
2119 && strcmp (current->name, _RCONST) != 0)
2121 sofar = (sofar + round - 1) &~ (round - 1);
2122 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2123 first_data = false;
2125 else if (strcmp (current->name, _LIB) == 0)
2127 /* On Irix 4, the location of contents of the .lib section
2128 from a shared library section is also rounded up to a
2129 page boundary. */
2131 sofar = (sofar + round - 1) &~ (round - 1);
2132 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2134 else if (first_nonalloc
2135 && (current->flags & SEC_ALLOC) == 0
2136 && (abfd->flags & D_PAGED) != 0)
2138 /* Skip up to the next page for an unallocated section, such
2139 as the .comment section on the Alpha. This leaves room
2140 for the .bss section. */
2141 first_nonalloc = false;
2142 sofar = (sofar + round - 1) &~ (round - 1);
2143 file_sofar = (file_sofar + round - 1) &~ (round - 1);
2146 /* Align the sections in the file to the same boundary on
2147 which they are aligned in virtual memory. */
2148 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2149 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2150 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2152 if ((abfd->flags & D_PAGED) != 0
2153 && (current->flags & SEC_ALLOC) != 0)
2155 sofar += (current->vma - sofar) % round;
2156 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2157 file_sofar += (current->vma - file_sofar) % round;
2160 if ((current->flags & (SEC_HAS_CONTENTS | SEC_LOAD)) != 0)
2161 current->filepos = file_sofar;
2163 sofar += current->_raw_size;
2164 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2165 file_sofar += current->_raw_size;
2167 /* make sure that this section is of the right size too */
2168 old_sofar = sofar;
2169 sofar = BFD_ALIGN (sofar, 1 << alignment_power);
2170 if ((current->flags & SEC_HAS_CONTENTS) != 0)
2171 file_sofar = BFD_ALIGN (file_sofar, 1 << alignment_power);
2172 current->_raw_size += sofar - old_sofar;
2175 free (sorted_hdrs);
2176 sorted_hdrs = NULL;
2178 ecoff_data (abfd)->reloc_filepos = file_sofar;
2180 return true;
2183 /* Determine the location of the relocs for all the sections in the
2184 output file, as well as the location of the symbolic debugging
2185 information. */
2187 static bfd_size_type
2188 ecoff_compute_reloc_file_positions (abfd)
2189 bfd *abfd;
2191 const bfd_size_type external_reloc_size =
2192 ecoff_backend (abfd)->external_reloc_size;
2193 file_ptr reloc_base;
2194 bfd_size_type reloc_size;
2195 asection *current;
2196 file_ptr sym_base;
2198 if (! abfd->output_has_begun)
2200 if (! ecoff_compute_section_file_positions (abfd))
2201 abort ();
2202 abfd->output_has_begun = true;
2205 reloc_base = ecoff_data (abfd)->reloc_filepos;
2207 reloc_size = 0;
2208 for (current = abfd->sections;
2209 current != (asection *)NULL;
2210 current = current->next)
2212 if (current->reloc_count == 0)
2213 current->rel_filepos = 0;
2214 else
2216 bfd_size_type relsize;
2218 current->rel_filepos = reloc_base;
2219 relsize = current->reloc_count * external_reloc_size;
2220 reloc_size += relsize;
2221 reloc_base += relsize;
2225 sym_base = ecoff_data (abfd)->reloc_filepos + reloc_size;
2227 /* At least on Ultrix, the symbol table of an executable file must
2228 be aligned to a page boundary. FIXME: Is this true on other
2229 platforms? */
2230 if ((abfd->flags & EXEC_P) != 0
2231 && (abfd->flags & D_PAGED) != 0)
2232 sym_base = ((sym_base + ecoff_backend (abfd)->round - 1)
2233 &~ (ecoff_backend (abfd)->round - 1));
2235 ecoff_data (abfd)->sym_filepos = sym_base;
2237 return reloc_size;
2240 /* Set the contents of a section. */
2242 boolean
2243 _bfd_ecoff_set_section_contents (abfd, section, location, offset, count)
2244 bfd *abfd;
2245 asection *section;
2246 PTR location;
2247 file_ptr offset;
2248 bfd_size_type count;
2250 /* This must be done first, because bfd_set_section_contents is
2251 going to set output_has_begun to true. */
2252 if (abfd->output_has_begun == false)
2254 if (! ecoff_compute_section_file_positions (abfd))
2255 return false;
2258 /* Handle the .lib section specially so that Irix 4 shared libraries
2259 work out. See coff_set_section_contents in coffcode.h. */
2260 if (strcmp (section->name, _LIB) == 0)
2262 bfd_byte *rec, *recend;
2264 rec = (bfd_byte *) location;
2265 recend = rec + count;
2266 while (rec < recend)
2268 ++section->lma;
2269 rec += bfd_get_32 (abfd, rec) * 4;
2272 BFD_ASSERT (rec == recend);
2275 if (count == 0)
2276 return true;
2278 if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) != 0
2279 || bfd_write (location, 1, count, abfd) != count)
2280 return false;
2282 return true;
2285 /* Get the GP value for an ECOFF file. This is a hook used by
2286 nlmconv. */
2288 bfd_vma
2289 bfd_ecoff_get_gp_value (abfd)
2290 bfd *abfd;
2292 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2293 || bfd_get_format (abfd) != bfd_object)
2295 bfd_set_error (bfd_error_invalid_operation);
2296 return 0;
2299 return ecoff_data (abfd)->gp;
2302 /* Set the GP value for an ECOFF file. This is a hook used by the
2303 assembler. */
2305 boolean
2306 bfd_ecoff_set_gp_value (abfd, gp_value)
2307 bfd *abfd;
2308 bfd_vma gp_value;
2310 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2311 || bfd_get_format (abfd) != bfd_object)
2313 bfd_set_error (bfd_error_invalid_operation);
2314 return false;
2317 ecoff_data (abfd)->gp = gp_value;
2319 return true;
2322 /* Set the register masks for an ECOFF file. This is a hook used by
2323 the assembler. */
2325 boolean
2326 bfd_ecoff_set_regmasks (abfd, gprmask, fprmask, cprmask)
2327 bfd *abfd;
2328 unsigned long gprmask;
2329 unsigned long fprmask;
2330 unsigned long *cprmask;
2332 ecoff_data_type *tdata;
2334 if (bfd_get_flavour (abfd) != bfd_target_ecoff_flavour
2335 || bfd_get_format (abfd) != bfd_object)
2337 bfd_set_error (bfd_error_invalid_operation);
2338 return false;
2341 tdata = ecoff_data (abfd);
2342 tdata->gprmask = gprmask;
2343 tdata->fprmask = fprmask;
2344 if (cprmask != (unsigned long *) NULL)
2346 register int i;
2348 for (i = 0; i < 3; i++)
2349 tdata->cprmask[i] = cprmask[i];
2352 return true;
2355 /* Get ECOFF EXTR information for an external symbol. This function
2356 is passed to bfd_ecoff_debug_externals. */
2358 static boolean
2359 ecoff_get_extr (sym, esym)
2360 asymbol *sym;
2361 EXTR *esym;
2363 ecoff_symbol_type *ecoff_sym_ptr;
2364 bfd *input_bfd;
2366 if (bfd_asymbol_flavour (sym) != bfd_target_ecoff_flavour
2367 || ecoffsymbol (sym)->native == NULL)
2369 /* Don't include debugging, local, or section symbols. */
2370 if ((sym->flags & BSF_DEBUGGING) != 0
2371 || (sym->flags & BSF_LOCAL) != 0
2372 || (sym->flags & BSF_SECTION_SYM) != 0)
2373 return false;
2375 esym->jmptbl = 0;
2376 esym->cobol_main = 0;
2377 esym->weakext = (sym->flags & BSF_WEAK) != 0;
2378 esym->reserved = 0;
2379 esym->ifd = ifdNil;
2380 /* FIXME: we can do better than this for st and sc. */
2381 esym->asym.st = stGlobal;
2382 esym->asym.sc = scAbs;
2383 esym->asym.reserved = 0;
2384 esym->asym.index = indexNil;
2385 return true;
2388 ecoff_sym_ptr = ecoffsymbol (sym);
2390 if (ecoff_sym_ptr->local)
2391 return false;
2393 input_bfd = bfd_asymbol_bfd (sym);
2394 (*(ecoff_backend (input_bfd)->debug_swap.swap_ext_in))
2395 (input_bfd, ecoff_sym_ptr->native, esym);
2397 /* If the symbol was defined by the linker, then esym will be
2398 undefined but sym will not be. Get a better class for such a
2399 symbol. */
2400 if ((esym->asym.sc == scUndefined
2401 || esym->asym.sc == scSUndefined)
2402 && ! bfd_is_und_section (bfd_get_section (sym)))
2403 esym->asym.sc = scAbs;
2405 /* Adjust the FDR index for the symbol by that used for the input
2406 BFD. */
2407 if (esym->ifd != -1)
2409 struct ecoff_debug_info *input_debug;
2411 input_debug = &ecoff_data (input_bfd)->debug_info;
2412 BFD_ASSERT (esym->ifd < input_debug->symbolic_header.ifdMax);
2413 if (input_debug->ifdmap != (RFDT *) NULL)
2414 esym->ifd = input_debug->ifdmap[esym->ifd];
2417 return true;
2420 /* Set the external symbol index. This routine is passed to
2421 bfd_ecoff_debug_externals. */
2423 static void
2424 ecoff_set_index (sym, indx)
2425 asymbol *sym;
2426 bfd_size_type indx;
2428 ecoff_set_sym_index (sym, indx);
2431 /* Write out an ECOFF file. */
2433 boolean
2434 _bfd_ecoff_write_object_contents (abfd)
2435 bfd *abfd;
2437 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
2438 const bfd_vma round = backend->round;
2439 const bfd_size_type filhsz = bfd_coff_filhsz (abfd);
2440 const bfd_size_type aoutsz = bfd_coff_aoutsz (abfd);
2441 const bfd_size_type scnhsz = bfd_coff_scnhsz (abfd);
2442 const bfd_size_type external_hdr_size
2443 = backend->debug_swap.external_hdr_size;
2444 const bfd_size_type external_reloc_size = backend->external_reloc_size;
2445 void (* const adjust_reloc_out) PARAMS ((bfd *,
2446 const arelent *,
2447 struct internal_reloc *))
2448 = backend->adjust_reloc_out;
2449 void (* const swap_reloc_out) PARAMS ((bfd *,
2450 const struct internal_reloc *,
2451 PTR))
2452 = backend->swap_reloc_out;
2453 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
2454 HDRR * const symhdr = &debug->symbolic_header;
2455 asection *current;
2456 unsigned int count;
2457 bfd_size_type reloc_size;
2458 bfd_size_type text_size;
2459 bfd_vma text_start;
2460 boolean set_text_start;
2461 bfd_size_type data_size;
2462 bfd_vma data_start;
2463 boolean set_data_start;
2464 bfd_size_type bss_size;
2465 PTR buff = NULL;
2466 PTR reloc_buff = NULL;
2467 struct internal_filehdr internal_f;
2468 struct internal_aouthdr internal_a;
2469 int i;
2471 /* Determine where the sections and relocs will go in the output
2472 file. */
2473 reloc_size = ecoff_compute_reloc_file_positions (abfd);
2475 count = 1;
2476 for (current = abfd->sections;
2477 current != (asection *)NULL;
2478 current = current->next)
2480 current->target_index = count;
2481 ++count;
2484 if ((abfd->flags & D_PAGED) != 0)
2485 text_size = _bfd_ecoff_sizeof_headers (abfd, false);
2486 else
2487 text_size = 0;
2488 text_start = 0;
2489 set_text_start = false;
2490 data_size = 0;
2491 data_start = 0;
2492 set_data_start = false;
2493 bss_size = 0;
2495 /* Write section headers to the file. */
2497 /* Allocate buff big enough to hold a section header,
2498 file header, or a.out header. */
2500 bfd_size_type siz;
2501 siz = scnhsz;
2502 if (siz < filhsz)
2503 siz = filhsz;
2504 if (siz < aoutsz)
2505 siz = aoutsz;
2506 buff = (PTR) bfd_malloc ((size_t) siz);
2507 if (buff == NULL)
2508 goto error_return;
2511 internal_f.f_nscns = 0;
2512 if (bfd_seek (abfd, (file_ptr) (filhsz + aoutsz), SEEK_SET) != 0)
2513 goto error_return;
2514 for (current = abfd->sections;
2515 current != (asection *) NULL;
2516 current = current->next)
2518 struct internal_scnhdr section;
2519 bfd_vma vma;
2521 ++internal_f.f_nscns;
2523 strncpy (section.s_name, current->name, sizeof section.s_name);
2525 /* This seems to be correct for Irix 4 shared libraries. */
2526 vma = bfd_get_section_vma (abfd, current);
2527 if (strcmp (current->name, _LIB) == 0)
2528 section.s_vaddr = 0;
2529 else
2530 section.s_vaddr = vma;
2532 section.s_paddr = current->lma;
2533 section.s_size = bfd_get_section_size_before_reloc (current);
2535 /* If this section is unloadable then the scnptr will be 0. */
2536 if ((current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
2537 section.s_scnptr = 0;
2538 else
2539 section.s_scnptr = current->filepos;
2540 section.s_relptr = current->rel_filepos;
2542 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
2543 object file produced by the assembler is supposed to point to
2544 information about how much room is required by objects of
2545 various different sizes. I think this only matters if we
2546 want the linker to compute the best size to use, or
2547 something. I don't know what happens if the information is
2548 not present. */
2549 if (strcmp (current->name, _PDATA) != 0)
2550 section.s_lnnoptr = 0;
2551 else
2553 /* The Alpha ECOFF .pdata section uses the lnnoptr field to
2554 hold the number of entries in the section (each entry is
2555 8 bytes). We stored this in the line_filepos field in
2556 ecoff_compute_section_file_positions. */
2557 section.s_lnnoptr = current->line_filepos;
2560 section.s_nreloc = current->reloc_count;
2561 section.s_nlnno = 0;
2562 section.s_flags = ecoff_sec_to_styp_flags (current->name,
2563 current->flags);
2565 if (bfd_coff_swap_scnhdr_out (abfd, (PTR) &section, buff) == 0
2566 || bfd_write (buff, 1, scnhsz, abfd) != scnhsz)
2567 goto error_return;
2569 if ((section.s_flags & STYP_TEXT) != 0
2570 || ((section.s_flags & STYP_RDATA) != 0
2571 && ecoff_data (abfd)->rdata_in_text)
2572 || section.s_flags == STYP_PDATA
2573 || (section.s_flags & STYP_DYNAMIC) != 0
2574 || (section.s_flags & STYP_LIBLIST) != 0
2575 || (section.s_flags & STYP_RELDYN) != 0
2576 || section.s_flags == STYP_CONFLIC
2577 || (section.s_flags & STYP_DYNSTR) != 0
2578 || (section.s_flags & STYP_DYNSYM) != 0
2579 || (section.s_flags & STYP_HASH) != 0
2580 || (section.s_flags & STYP_ECOFF_INIT) != 0
2581 || (section.s_flags & STYP_ECOFF_FINI) != 0
2582 || section.s_flags == STYP_RCONST)
2584 text_size += bfd_get_section_size_before_reloc (current);
2585 if (! set_text_start || text_start > vma)
2587 text_start = vma;
2588 set_text_start = true;
2591 else if ((section.s_flags & STYP_RDATA) != 0
2592 || (section.s_flags & STYP_DATA) != 0
2593 || (section.s_flags & STYP_LITA) != 0
2594 || (section.s_flags & STYP_LIT8) != 0
2595 || (section.s_flags & STYP_LIT4) != 0
2596 || (section.s_flags & STYP_SDATA) != 0
2597 || section.s_flags == STYP_XDATA
2598 || (section.s_flags & STYP_GOT) != 0)
2600 data_size += bfd_get_section_size_before_reloc (current);
2601 if (! set_data_start || data_start > vma)
2603 data_start = vma;
2604 set_data_start = true;
2607 else if ((section.s_flags & STYP_BSS) != 0
2608 || (section.s_flags & STYP_SBSS) != 0)
2609 bss_size += bfd_get_section_size_before_reloc (current);
2610 else if (section.s_flags == 0
2611 || (section.s_flags & STYP_ECOFF_LIB) != 0
2612 || section.s_flags == STYP_COMMENT)
2613 /* Do nothing */ ;
2614 else
2615 abort ();
2618 /* Set up the file header. */
2620 internal_f.f_magic = ecoff_get_magic (abfd);
2622 /* We will NOT put a fucking timestamp in the header here. Every
2623 time you put it back, I will come in and take it out again. I'm
2624 sorry. This field does not belong here. We fill it with a 0 so
2625 it compares the same but is not a reasonable time. --
2626 gnu@cygnus.com. */
2627 internal_f.f_timdat = 0;
2629 if (bfd_get_symcount (abfd) != 0)
2631 /* The ECOFF f_nsyms field is not actually the number of
2632 symbols, it's the size of symbolic information header. */
2633 internal_f.f_nsyms = external_hdr_size;
2634 internal_f.f_symptr = ecoff_data (abfd)->sym_filepos;
2636 else
2638 internal_f.f_nsyms = 0;
2639 internal_f.f_symptr = 0;
2642 internal_f.f_opthdr = aoutsz;
2644 internal_f.f_flags = F_LNNO;
2645 if (reloc_size == 0)
2646 internal_f.f_flags |= F_RELFLG;
2647 if (bfd_get_symcount (abfd) == 0)
2648 internal_f.f_flags |= F_LSYMS;
2649 if (abfd->flags & EXEC_P)
2650 internal_f.f_flags |= F_EXEC;
2652 if (bfd_little_endian (abfd))
2653 internal_f.f_flags |= F_AR32WR;
2654 else
2655 internal_f.f_flags |= F_AR32W;
2657 /* Set up the ``optional'' header. */
2658 if ((abfd->flags & D_PAGED) != 0)
2659 internal_a.magic = ECOFF_AOUT_ZMAGIC;
2660 else
2661 internal_a.magic = ECOFF_AOUT_OMAGIC;
2663 /* FIXME: Is this really correct? */
2664 internal_a.vstamp = symhdr->vstamp;
2666 /* At least on Ultrix, these have to be rounded to page boundaries.
2667 FIXME: Is this true on other platforms? */
2668 if ((abfd->flags & D_PAGED) != 0)
2670 internal_a.tsize = (text_size + round - 1) &~ (round - 1);
2671 internal_a.text_start = text_start &~ (round - 1);
2672 internal_a.dsize = (data_size + round - 1) &~ (round - 1);
2673 internal_a.data_start = data_start &~ (round - 1);
2675 else
2677 internal_a.tsize = text_size;
2678 internal_a.text_start = text_start;
2679 internal_a.dsize = data_size;
2680 internal_a.data_start = data_start;
2683 /* On Ultrix, the initial portions of the .sbss and .bss segments
2684 are at the end of the data section. The bsize field in the
2685 optional header records how many bss bytes are required beyond
2686 those in the data section. The value is not rounded to a page
2687 boundary. */
2688 if (bss_size < internal_a.dsize - data_size)
2689 bss_size = 0;
2690 else
2691 bss_size -= internal_a.dsize - data_size;
2692 internal_a.bsize = bss_size;
2693 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
2695 internal_a.entry = bfd_get_start_address (abfd);
2697 internal_a.gp_value = ecoff_data (abfd)->gp;
2699 internal_a.gprmask = ecoff_data (abfd)->gprmask;
2700 internal_a.fprmask = ecoff_data (abfd)->fprmask;
2701 for (i = 0; i < 4; i++)
2702 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
2704 /* Let the backend adjust the headers if necessary. */
2705 if (backend->adjust_headers)
2707 if (! (*backend->adjust_headers) (abfd, &internal_f, &internal_a))
2708 goto error_return;
2711 /* Write out the file header and the optional header. */
2713 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
2714 goto error_return;
2716 bfd_coff_swap_filehdr_out (abfd, (PTR) &internal_f, buff);
2717 if (bfd_write (buff, 1, filhsz, abfd) != filhsz)
2718 goto error_return;
2720 bfd_coff_swap_aouthdr_out (abfd, (PTR) &internal_a, buff);
2721 if (bfd_write (buff, 1, aoutsz, abfd) != aoutsz)
2722 goto error_return;
2724 /* Build the external symbol information. This must be done before
2725 writing out the relocs so that we know the symbol indices. We
2726 don't do this if this BFD was created by the backend linker,
2727 since it will have already handled the symbols and relocs. */
2728 if (! ecoff_data (abfd)->linker)
2730 symhdr->iextMax = 0;
2731 symhdr->issExtMax = 0;
2732 debug->external_ext = debug->external_ext_end = NULL;
2733 debug->ssext = debug->ssext_end = NULL;
2734 if (bfd_ecoff_debug_externals (abfd, debug, &backend->debug_swap,
2735 (((abfd->flags & EXEC_P) == 0)
2736 ? true : false),
2737 ecoff_get_extr, ecoff_set_index)
2738 == false)
2739 goto error_return;
2741 /* Write out the relocs. */
2742 for (current = abfd->sections;
2743 current != (asection *) NULL;
2744 current = current->next)
2746 arelent **reloc_ptr_ptr;
2747 arelent **reloc_end;
2748 char *out_ptr;
2750 if (current->reloc_count == 0)
2751 continue;
2753 reloc_buff =
2754 bfd_alloc (abfd, current->reloc_count * external_reloc_size);
2755 if (reloc_buff == NULL)
2756 goto error_return;
2758 reloc_ptr_ptr = current->orelocation;
2759 reloc_end = reloc_ptr_ptr + current->reloc_count;
2760 out_ptr = (char *) reloc_buff;
2761 for (;
2762 reloc_ptr_ptr < reloc_end;
2763 reloc_ptr_ptr++, out_ptr += external_reloc_size)
2765 arelent *reloc;
2766 asymbol *sym;
2767 struct internal_reloc in;
2769 memset ((PTR) &in, 0, sizeof in);
2771 reloc = *reloc_ptr_ptr;
2772 sym = *reloc->sym_ptr_ptr;
2774 in.r_vaddr = (reloc->address
2775 + bfd_get_section_vma (abfd, current));
2776 in.r_type = reloc->howto->type;
2778 if ((sym->flags & BSF_SECTION_SYM) == 0)
2780 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
2781 in.r_extern = 1;
2783 else
2785 CONST char *name;
2787 name = bfd_get_section_name (abfd, bfd_get_section (sym));
2788 if (strcmp (name, ".text") == 0)
2789 in.r_symndx = RELOC_SECTION_TEXT;
2790 else if (strcmp (name, ".rdata") == 0)
2791 in.r_symndx = RELOC_SECTION_RDATA;
2792 else if (strcmp (name, ".data") == 0)
2793 in.r_symndx = RELOC_SECTION_DATA;
2794 else if (strcmp (name, ".sdata") == 0)
2795 in.r_symndx = RELOC_SECTION_SDATA;
2796 else if (strcmp (name, ".sbss") == 0)
2797 in.r_symndx = RELOC_SECTION_SBSS;
2798 else if (strcmp (name, ".bss") == 0)
2799 in.r_symndx = RELOC_SECTION_BSS;
2800 else if (strcmp (name, ".init") == 0)
2801 in.r_symndx = RELOC_SECTION_INIT;
2802 else if (strcmp (name, ".lit8") == 0)
2803 in.r_symndx = RELOC_SECTION_LIT8;
2804 else if (strcmp (name, ".lit4") == 0)
2805 in.r_symndx = RELOC_SECTION_LIT4;
2806 else if (strcmp (name, ".xdata") == 0)
2807 in.r_symndx = RELOC_SECTION_XDATA;
2808 else if (strcmp (name, ".pdata") == 0)
2809 in.r_symndx = RELOC_SECTION_PDATA;
2810 else if (strcmp (name, ".fini") == 0)
2811 in.r_symndx = RELOC_SECTION_FINI;
2812 else if (strcmp (name, ".lita") == 0)
2813 in.r_symndx = RELOC_SECTION_LITA;
2814 else if (strcmp (name, "*ABS*") == 0)
2815 in.r_symndx = RELOC_SECTION_ABS;
2816 else if (strcmp (name, ".rconst") == 0)
2817 in.r_symndx = RELOC_SECTION_RCONST;
2818 else
2819 abort ();
2820 in.r_extern = 0;
2823 (*adjust_reloc_out) (abfd, reloc, &in);
2825 (*swap_reloc_out) (abfd, &in, (PTR) out_ptr);
2828 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
2829 goto error_return;
2830 if (bfd_write (reloc_buff,
2831 external_reloc_size, current->reloc_count, abfd)
2832 != external_reloc_size * current->reloc_count)
2833 goto error_return;
2834 bfd_release (abfd, reloc_buff);
2835 reloc_buff = NULL;
2838 /* Write out the symbolic debugging information. */
2839 if (bfd_get_symcount (abfd) > 0)
2841 /* Write out the debugging information. */
2842 if (bfd_ecoff_write_debug (abfd, debug, &backend->debug_swap,
2843 ecoff_data (abfd)->sym_filepos)
2844 == false)
2845 goto error_return;
2849 /* The .bss section of a demand paged executable must receive an
2850 entire page. If there are symbols, the symbols will start on the
2851 next page. If there are no symbols, we must fill out the page by
2852 hand. */
2853 if (bfd_get_symcount (abfd) == 0
2854 && (abfd->flags & EXEC_P) != 0
2855 && (abfd->flags & D_PAGED) != 0)
2857 char c;
2859 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2860 SEEK_SET) != 0)
2861 goto error_return;
2862 if (bfd_read (&c, 1, 1, abfd) == 0)
2863 c = 0;
2864 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos - 1,
2865 SEEK_SET) != 0)
2866 goto error_return;
2867 if (bfd_write (&c, 1, 1, abfd) != 1)
2868 goto error_return;
2871 if (reloc_buff != NULL)
2872 bfd_release (abfd, reloc_buff);
2873 if (buff != NULL)
2874 free (buff);
2875 return true;
2876 error_return:
2877 if (reloc_buff != NULL)
2878 bfd_release (abfd, reloc_buff);
2879 if (buff != NULL)
2880 free (buff);
2881 return false;
2884 /* Archive handling. ECOFF uses what appears to be a unique type of
2885 archive header (armap). The byte ordering of the armap and the
2886 contents are encoded in the name of the armap itself. At least for
2887 now, we only support archives with the same byte ordering in the
2888 armap and the contents.
2890 The first four bytes in the armap are the number of symbol
2891 definitions. This is always a power of two.
2893 This is followed by the symbol definitions. Each symbol definition
2894 occupies 8 bytes. The first four bytes are the offset from the
2895 start of the armap strings to the null-terminated string naming
2896 this symbol. The second four bytes are the file offset to the
2897 archive member which defines this symbol. If the second four bytes
2898 are 0, then this is not actually a symbol definition, and it should
2899 be ignored.
2901 The symbols are hashed into the armap with a closed hashing scheme.
2902 See the functions below for the details of the algorithm.
2904 After the symbol definitions comes four bytes holding the size of
2905 the string table, followed by the string table itself. */
2907 /* The name of an archive headers looks like this:
2908 __________E[BL]E[BL]_ (with a trailing space).
2909 The trailing space is changed to an X if the archive is changed to
2910 indicate that the armap is out of date.
2912 The Alpha seems to use ________64E[BL]E[BL]_. */
2914 #define ARMAP_BIG_ENDIAN 'B'
2915 #define ARMAP_LITTLE_ENDIAN 'L'
2916 #define ARMAP_MARKER 'E'
2917 #define ARMAP_START_LENGTH 10
2918 #define ARMAP_HEADER_MARKER_INDEX 10
2919 #define ARMAP_HEADER_ENDIAN_INDEX 11
2920 #define ARMAP_OBJECT_MARKER_INDEX 12
2921 #define ARMAP_OBJECT_ENDIAN_INDEX 13
2922 #define ARMAP_END_INDEX 14
2923 #define ARMAP_END "_ "
2925 /* This is a magic number used in the hashing algorithm. */
2926 #define ARMAP_HASH_MAGIC 0x9dd68ab5
2928 /* This returns the hash value to use for a string. It also sets
2929 *REHASH to the rehash adjustment if the first slot is taken. SIZE
2930 is the number of entries in the hash table, and HLOG is the log
2931 base 2 of SIZE. */
2933 static unsigned int
2934 ecoff_armap_hash (s, rehash, size, hlog)
2935 CONST char *s;
2936 unsigned int *rehash;
2937 unsigned int size;
2938 unsigned int hlog;
2940 unsigned int hash;
2942 if (hlog == 0)
2943 return 0;
2944 hash = *s++;
2945 while (*s != '\0')
2946 hash = ((hash >> 27) | (hash << 5)) + *s++;
2947 hash *= ARMAP_HASH_MAGIC;
2948 *rehash = (hash & (size - 1)) | 1;
2949 return hash >> (32 - hlog);
2952 /* Read in the armap. */
2954 boolean
2955 _bfd_ecoff_slurp_armap (abfd)
2956 bfd *abfd;
2958 char nextname[17];
2959 unsigned int i;
2960 struct areltdata *mapdata;
2961 bfd_size_type parsed_size;
2962 char *raw_armap;
2963 struct artdata *ardata;
2964 unsigned int count;
2965 char *raw_ptr;
2966 struct symdef *symdef_ptr;
2967 char *stringbase;
2969 /* Get the name of the first element. */
2970 i = bfd_read ((PTR) nextname, 1, 16, abfd);
2971 if (i == 0)
2972 return true;
2973 if (i != 16)
2974 return false;
2976 if (bfd_seek (abfd, (file_ptr) -16, SEEK_CUR) != 0)
2977 return false;
2979 /* Irix 4.0.5F apparently can use either an ECOFF armap or a
2980 standard COFF armap. We could move the ECOFF armap stuff into
2981 bfd_slurp_armap, but that seems inappropriate since no other
2982 target uses this format. Instead, we check directly for a COFF
2983 armap. */
2984 if (strncmp (nextname, "/ ", 16) == 0)
2985 return bfd_slurp_armap (abfd);
2987 /* See if the first element is an armap. */
2988 if (strncmp (nextname, ecoff_backend (abfd)->armap_start,
2989 ARMAP_START_LENGTH) != 0
2990 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
2991 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2992 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2993 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
2994 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
2995 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
2996 || strncmp (nextname + ARMAP_END_INDEX,
2997 ARMAP_END, sizeof ARMAP_END - 1) != 0)
2999 bfd_has_map (abfd) = false;
3000 return true;
3003 /* Make sure we have the right byte ordering. */
3004 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3005 ^ (bfd_header_big_endian (abfd)))
3006 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3007 ^ (bfd_big_endian (abfd))))
3009 bfd_set_error (bfd_error_wrong_format);
3010 return false;
3013 /* Read in the armap. */
3014 ardata = bfd_ardata (abfd);
3015 mapdata = (struct areltdata *) _bfd_read_ar_hdr (abfd);
3016 if (mapdata == (struct areltdata *) NULL)
3017 return false;
3018 parsed_size = mapdata->parsed_size;
3019 bfd_release (abfd, (PTR) mapdata);
3021 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3022 if (raw_armap == (char *) NULL)
3023 return false;
3025 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3027 if (bfd_get_error () != bfd_error_system_call)
3028 bfd_set_error (bfd_error_malformed_archive);
3029 bfd_release (abfd, (PTR) raw_armap);
3030 return false;
3033 ardata->tdata = (PTR) raw_armap;
3035 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3037 ardata->symdef_count = 0;
3038 ardata->cache = (struct ar_cache *) NULL;
3040 /* This code used to overlay the symdefs over the raw archive data,
3041 but that doesn't work on a 64 bit host. */
3043 stringbase = raw_armap + count * 8 + 8;
3045 #ifdef CHECK_ARMAP_HASH
3047 unsigned int hlog;
3049 /* Double check that I have the hashing algorithm right by making
3050 sure that every symbol can be looked up successfully. */
3051 hlog = 0;
3052 for (i = 1; i < count; i <<= 1)
3053 hlog++;
3054 BFD_ASSERT (i == count);
3056 raw_ptr = raw_armap + 4;
3057 for (i = 0; i < count; i++, raw_ptr += 8)
3059 unsigned int name_offset, file_offset;
3060 unsigned int hash, rehash, srch;
3062 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3063 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3064 if (file_offset == 0)
3065 continue;
3066 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3067 hlog);
3068 if (hash == i)
3069 continue;
3071 /* See if we can rehash to this location. */
3072 for (srch = (hash + rehash) & (count - 1);
3073 srch != hash && srch != i;
3074 srch = (srch + rehash) & (count - 1))
3075 BFD_ASSERT (bfd_h_get_32 (abfd, (PTR) (raw_armap + 8 + srch * 8))
3076 != 0);
3077 BFD_ASSERT (srch == i);
3081 #endif /* CHECK_ARMAP_HASH */
3083 raw_ptr = raw_armap + 4;
3084 for (i = 0; i < count; i++, raw_ptr += 8)
3085 if (bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4)) != 0)
3086 ++ardata->symdef_count;
3088 symdef_ptr = ((struct symdef *)
3089 bfd_alloc (abfd,
3090 ardata->symdef_count * sizeof (struct symdef)));
3091 if (!symdef_ptr)
3092 return false;
3094 ardata->symdefs = (carsym *) symdef_ptr;
3096 raw_ptr = raw_armap + 4;
3097 for (i = 0; i < count; i++, raw_ptr += 8)
3099 unsigned int name_offset, file_offset;
3101 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + 4));
3102 if (file_offset == 0)
3103 continue;
3104 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3105 symdef_ptr->s.name = stringbase + name_offset;
3106 symdef_ptr->file_offset = file_offset;
3107 ++symdef_ptr;
3110 ardata->first_file_filepos = bfd_tell (abfd);
3111 /* Pad to an even boundary. */
3112 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3114 bfd_has_map (abfd) = true;
3116 return true;
3119 /* Write out an armap. */
3121 boolean
3122 _bfd_ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3123 bfd *abfd;
3124 unsigned int elength;
3125 struct orl *map;
3126 unsigned int orl_count;
3127 int stridx;
3129 unsigned int hashsize, hashlog;
3130 unsigned int symdefsize;
3131 int padit;
3132 unsigned int stringsize;
3133 unsigned int mapsize;
3134 file_ptr firstreal;
3135 struct ar_hdr hdr;
3136 struct stat statbuf;
3137 unsigned int i;
3138 bfd_byte temp[4];
3139 bfd_byte *hashtable;
3140 bfd *current;
3141 bfd *last_elt;
3143 /* Ultrix appears to use as a hash table size the least power of two
3144 greater than twice the number of entries. */
3145 for (hashlog = 0; ((unsigned int) 1 << hashlog) <= 2 * orl_count; hashlog++)
3147 hashsize = 1 << hashlog;
3149 symdefsize = hashsize * 8;
3150 padit = stridx % 2;
3151 stringsize = stridx + padit;
3153 /* Include 8 bytes to store symdefsize and stringsize in output. */
3154 mapsize = symdefsize + stringsize + 8;
3156 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3158 memset ((PTR) &hdr, 0, sizeof hdr);
3160 /* Work out the ECOFF armap name. */
3161 strcpy (hdr.ar_name, ecoff_backend (abfd)->armap_start);
3162 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3163 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3164 (bfd_header_big_endian (abfd)
3165 ? ARMAP_BIG_ENDIAN
3166 : ARMAP_LITTLE_ENDIAN);
3167 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3168 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3169 bfd_big_endian (abfd) ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3170 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3172 /* Write the timestamp of the archive header to be just a little bit
3173 later than the timestamp of the file, otherwise the linker will
3174 complain that the index is out of date. Actually, the Ultrix
3175 linker just checks the archive name; the GNU linker may check the
3176 date. */
3177 stat (abfd->filename, &statbuf);
3178 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3180 /* The DECstation uses zeroes for the uid, gid and mode of the
3181 armap. */
3182 hdr.ar_uid[0] = '0';
3183 hdr.ar_gid[0] = '0';
3184 #if 0
3185 hdr.ar_mode[0] = '0';
3186 #else
3187 /* Building gcc ends up extracting the armap as a file - twice. */
3188 hdr.ar_mode[0] = '6';
3189 hdr.ar_mode[1] = '4';
3190 hdr.ar_mode[2] = '4';
3191 #endif
3193 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3195 hdr.ar_fmag[0] = '`';
3196 hdr.ar_fmag[1] = '\012';
3198 /* Turn all null bytes in the header into spaces. */
3199 for (i = 0; i < sizeof (struct ar_hdr); i++)
3200 if (((char *)(&hdr))[i] == '\0')
3201 (((char *)(&hdr))[i]) = ' ';
3203 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3204 != sizeof (struct ar_hdr))
3205 return false;
3207 bfd_h_put_32 (abfd, (bfd_vma) hashsize, temp);
3208 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3209 return false;
3211 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3212 if (!hashtable)
3213 return false;
3215 current = abfd->archive_head;
3216 last_elt = current;
3217 for (i = 0; i < orl_count; i++)
3219 unsigned int hash, rehash;
3221 /* Advance firstreal to the file position of this archive
3222 element. */
3223 if (((bfd *) map[i].pos) != last_elt)
3227 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3228 firstreal += firstreal % 2;
3229 current = current->next;
3231 while (current != (bfd *) map[i].pos);
3234 last_elt = current;
3236 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3237 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (hash * 8) + 4)) != 0)
3239 unsigned int srch;
3241 /* The desired slot is already taken. */
3242 for (srch = (hash + rehash) & (hashsize - 1);
3243 srch != hash;
3244 srch = (srch + rehash) & (hashsize - 1))
3245 if (bfd_h_get_32 (abfd, (PTR) (hashtable + (srch * 8) + 4)) == 0)
3246 break;
3248 BFD_ASSERT (srch != hash);
3250 hash = srch;
3253 bfd_h_put_32 (abfd, (bfd_vma) map[i].namidx,
3254 (PTR) (hashtable + hash * 8));
3255 bfd_h_put_32 (abfd, (bfd_vma) firstreal,
3256 (PTR) (hashtable + hash * 8 + 4));
3259 if (bfd_write ((PTR) hashtable, 1, symdefsize, abfd) != symdefsize)
3260 return false;
3262 bfd_release (abfd, hashtable);
3264 /* Now write the strings. */
3265 bfd_h_put_32 (abfd, (bfd_vma) stringsize, temp);
3266 if (bfd_write ((PTR) temp, 1, 4, abfd) != 4)
3267 return false;
3268 for (i = 0; i < orl_count; i++)
3270 bfd_size_type len;
3272 len = strlen (*map[i].name) + 1;
3273 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3274 return false;
3277 /* The spec sez this should be a newline. But in order to be
3278 bug-compatible for DECstation ar we use a null. */
3279 if (padit)
3281 if (bfd_write ("", 1, 1, abfd) != 1)
3282 return false;
3285 return true;
3288 /* See whether this BFD is an archive. If it is, read in the armap
3289 and the extended name table. */
3291 const bfd_target *
3292 _bfd_ecoff_archive_p (abfd)
3293 bfd *abfd;
3295 struct artdata *tdata_hold;
3296 char armag[SARMAG + 1];
3298 tdata_hold = abfd->tdata.aout_ar_data;
3300 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG)
3302 if (bfd_get_error () != bfd_error_system_call)
3303 bfd_set_error (bfd_error_wrong_format);
3304 return (const bfd_target *) NULL;
3307 if (strncmp (armag, ARMAG, SARMAG) != 0)
3309 bfd_set_error (bfd_error_wrong_format);
3310 return NULL;
3313 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3314 involves a cast, we can't do it as the left operand of
3315 assignment. */
3316 abfd->tdata.aout_ar_data =
3317 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3319 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3321 abfd->tdata.aout_ar_data = tdata_hold;
3322 return (const bfd_target *) NULL;
3325 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3326 bfd_ardata (abfd)->cache = NULL;
3327 bfd_ardata (abfd)->archive_head = NULL;
3328 bfd_ardata (abfd)->symdefs = NULL;
3329 bfd_ardata (abfd)->extended_names = NULL;
3330 bfd_ardata (abfd)->tdata = NULL;
3332 if (_bfd_ecoff_slurp_armap (abfd) == false
3333 || _bfd_ecoff_slurp_extended_name_table (abfd) == false)
3335 bfd_release (abfd, bfd_ardata (abfd));
3336 abfd->tdata.aout_ar_data = tdata_hold;
3337 return (const bfd_target *) NULL;
3340 if (bfd_has_map (abfd))
3342 bfd *first;
3344 /* This archive has a map, so we may presume that the contents
3345 are object files. Make sure that if the first file in the
3346 archive can be recognized as an object file, it is for this
3347 target. If not, assume that this is the wrong format. If
3348 the first file is not an object file, somebody is doing
3349 something weird, and we permit it so that ar -t will work. */
3351 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3352 if (first != NULL)
3354 boolean fail;
3356 first->target_defaulted = false;
3357 fail = false;
3358 if (bfd_check_format (first, bfd_object)
3359 && first->xvec != abfd->xvec)
3361 (void) bfd_close (first);
3362 bfd_release (abfd, bfd_ardata (abfd));
3363 abfd->tdata.aout_ar_data = tdata_hold;
3364 bfd_set_error (bfd_error_wrong_format);
3365 return NULL;
3368 /* We ought to close first here, but we can't, because we
3369 have no way to remove it from the archive cache. FIXME. */
3373 return abfd->xvec;
3376 /* ECOFF linker code. */
3378 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3379 PARAMS ((struct bfd_hash_entry *entry,
3380 struct bfd_hash_table *table,
3381 const char *string));
3382 static boolean ecoff_link_add_archive_symbols
3383 PARAMS ((bfd *, struct bfd_link_info *));
3384 static boolean ecoff_link_check_archive_element
3385 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3386 static boolean ecoff_link_add_object_symbols
3387 PARAMS ((bfd *, struct bfd_link_info *));
3388 static boolean ecoff_link_add_externals
3389 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3391 /* Routine to create an entry in an ECOFF link hash table. */
3393 static struct bfd_hash_entry *
3394 ecoff_link_hash_newfunc (entry, table, string)
3395 struct bfd_hash_entry *entry;
3396 struct bfd_hash_table *table;
3397 const char *string;
3399 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3401 /* Allocate the structure if it has not already been allocated by a
3402 subclass. */
3403 if (ret == (struct ecoff_link_hash_entry *) NULL)
3404 ret = ((struct ecoff_link_hash_entry *)
3405 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3406 if (ret == (struct ecoff_link_hash_entry *) NULL)
3407 return NULL;
3409 /* Call the allocation method of the superclass. */
3410 ret = ((struct ecoff_link_hash_entry *)
3411 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3412 table, string));
3414 if (ret)
3416 /* Set local fields. */
3417 ret->indx = -1;
3418 ret->abfd = NULL;
3419 ret->written = 0;
3420 ret->small = 0;
3422 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3424 return (struct bfd_hash_entry *) ret;
3427 /* Create an ECOFF link hash table. */
3429 struct bfd_link_hash_table *
3430 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3431 bfd *abfd;
3433 struct ecoff_link_hash_table *ret;
3435 ret = ((struct ecoff_link_hash_table *)
3436 bfd_alloc (abfd, sizeof (struct ecoff_link_hash_table)));
3437 if (ret == NULL)
3438 return NULL;
3439 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3440 ecoff_link_hash_newfunc))
3442 free (ret);
3443 return (struct bfd_link_hash_table *) NULL;
3445 return &ret->root;
3448 /* Look up an entry in an ECOFF link hash table. */
3450 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3451 ((struct ecoff_link_hash_entry *) \
3452 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3454 /* Traverse an ECOFF link hash table. */
3456 #define ecoff_link_hash_traverse(table, func, info) \
3457 (bfd_link_hash_traverse \
3458 (&(table)->root, \
3459 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3460 (info)))
3462 /* Get the ECOFF link hash table from the info structure. This is
3463 just a cast. */
3465 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3467 /* Given an ECOFF BFD, add symbols to the global hash table as
3468 appropriate. */
3470 boolean
3471 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3472 bfd *abfd;
3473 struct bfd_link_info *info;
3475 switch (bfd_get_format (abfd))
3477 case bfd_object:
3478 return ecoff_link_add_object_symbols (abfd, info);
3479 case bfd_archive:
3480 return ecoff_link_add_archive_symbols (abfd, info);
3481 default:
3482 bfd_set_error (bfd_error_wrong_format);
3483 return false;
3487 /* Add the symbols from an archive file to the global hash table.
3488 This looks through the undefined symbols, looks each one up in the
3489 archive hash table, and adds any associated object file. We do not
3490 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3491 already have a hash table, so there is no reason to construct
3492 another one. */
3494 static boolean
3495 ecoff_link_add_archive_symbols (abfd, info)
3496 bfd *abfd;
3497 struct bfd_link_info *info;
3499 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3500 const bfd_byte *raw_armap;
3501 struct bfd_link_hash_entry **pundef;
3502 unsigned int armap_count;
3503 unsigned int armap_log;
3504 unsigned int i;
3505 const bfd_byte *hashtable;
3506 const char *stringbase;
3508 if (! bfd_has_map (abfd))
3510 /* An empty archive is a special case. */
3511 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3512 return true;
3513 bfd_set_error (bfd_error_no_armap);
3514 return false;
3517 /* If we don't have any raw data for this archive, as can happen on
3518 Irix 4.0.5F, we call the generic routine.
3519 FIXME: We should be more clever about this, since someday tdata
3520 may get to something for a generic archive. */
3521 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3522 if (raw_armap == (bfd_byte *) NULL)
3523 return (_bfd_generic_link_add_archive_symbols
3524 (abfd, info, ecoff_link_check_archive_element));
3526 armap_count = bfd_h_get_32 (abfd, raw_armap);
3528 armap_log = 0;
3529 for (i = 1; i < armap_count; i <<= 1)
3530 armap_log++;
3531 BFD_ASSERT (i == armap_count);
3533 hashtable = raw_armap + 4;
3534 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3536 /* Look through the list of undefined symbols. */
3537 pundef = &info->hash->undefs;
3538 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3540 struct bfd_link_hash_entry *h;
3541 unsigned int hash, rehash;
3542 unsigned int file_offset;
3543 const char *name;
3544 bfd *element;
3546 h = *pundef;
3548 /* When a symbol is defined, it is not necessarily removed from
3549 the list. */
3550 if (h->type != bfd_link_hash_undefined
3551 && h->type != bfd_link_hash_common)
3553 /* Remove this entry from the list, for general cleanliness
3554 and because we are going to look through the list again
3555 if we search any more libraries. We can't remove the
3556 entry if it is the tail, because that would lose any
3557 entries we add to the list later on. */
3558 if (*pundef != info->hash->undefs_tail)
3559 *pundef = (*pundef)->next;
3560 else
3561 pundef = &(*pundef)->next;
3562 continue;
3565 /* Native ECOFF linkers do not pull in archive elements merely
3566 to satisfy common definitions, so neither do we. We leave
3567 them on the list, though, in case we are linking against some
3568 other object format. */
3569 if (h->type != bfd_link_hash_undefined)
3571 pundef = &(*pundef)->next;
3572 continue;
3575 /* Look for this symbol in the archive hash table. */
3576 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3577 armap_log);
3579 file_offset = bfd_h_get_32 (abfd, hashtable + (hash * 8) + 4);
3580 if (file_offset == 0)
3582 /* Nothing in this slot. */
3583 pundef = &(*pundef)->next;
3584 continue;
3587 name = stringbase + bfd_h_get_32 (abfd, hashtable + (hash * 8));
3588 if (name[0] != h->root.string[0]
3589 || strcmp (name, h->root.string) != 0)
3591 unsigned int srch;
3592 boolean found;
3594 /* That was the wrong symbol. Try rehashing. */
3595 found = false;
3596 for (srch = (hash + rehash) & (armap_count - 1);
3597 srch != hash;
3598 srch = (srch + rehash) & (armap_count - 1))
3600 file_offset = bfd_h_get_32 (abfd, hashtable + (srch * 8) + 4);
3601 if (file_offset == 0)
3602 break;
3603 name = stringbase + bfd_h_get_32 (abfd, hashtable + (srch * 8));
3604 if (name[0] == h->root.string[0]
3605 && strcmp (name, h->root.string) == 0)
3607 found = true;
3608 break;
3612 if (! found)
3614 pundef = &(*pundef)->next;
3615 continue;
3618 hash = srch;
3621 element = (*backend->get_elt_at_filepos) (abfd, file_offset);
3622 if (element == (bfd *) NULL)
3623 return false;
3625 if (! bfd_check_format (element, bfd_object))
3626 return false;
3628 /* Unlike the generic linker, we know that this element provides
3629 a definition for an undefined symbol and we know that we want
3630 to include it. We don't need to check anything. */
3631 if (! (*info->callbacks->add_archive_element) (info, element, name))
3632 return false;
3633 if (! ecoff_link_add_object_symbols (element, info))
3634 return false;
3636 pundef = &(*pundef)->next;
3639 return true;
3642 /* This is called if we used _bfd_generic_link_add_archive_symbols
3643 because we were not dealing with an ECOFF archive. */
3645 static boolean
3646 ecoff_link_check_archive_element (abfd, info, pneeded)
3647 bfd *abfd;
3648 struct bfd_link_info *info;
3649 boolean *pneeded;
3651 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3652 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3653 = backend->debug_swap.swap_ext_in;
3654 HDRR *symhdr;
3655 bfd_size_type external_ext_size;
3656 PTR external_ext = NULL;
3657 size_t esize;
3658 char *ssext = NULL;
3659 char *ext_ptr;
3660 char *ext_end;
3662 *pneeded = false;
3664 if (! ecoff_slurp_symbolic_header (abfd))
3665 goto error_return;
3667 /* If there are no symbols, we don't want it. */
3668 if (bfd_get_symcount (abfd) == 0)
3669 goto successful_return;
3671 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3673 /* Read in the external symbols and external strings. */
3674 external_ext_size = backend->debug_swap.external_ext_size;
3675 esize = symhdr->iextMax * external_ext_size;
3676 external_ext = (PTR) bfd_malloc (esize);
3677 if (external_ext == NULL && esize != 0)
3678 goto error_return;
3680 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3681 || bfd_read (external_ext, 1, esize, abfd) != esize)
3682 goto error_return;
3684 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3685 if (ssext == NULL && symhdr->issExtMax != 0)
3686 goto error_return;
3688 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3689 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd) !=
3690 (bfd_size_type) symhdr->issExtMax))
3691 goto error_return;
3693 /* Look through the external symbols to see if they define some
3694 symbol that is currently undefined. */
3695 ext_ptr = (char *) external_ext;
3696 ext_end = ext_ptr + esize;
3697 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3699 EXTR esym;
3700 boolean def;
3701 const char *name;
3702 struct bfd_link_hash_entry *h;
3704 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3706 /* See if this symbol defines something. */
3707 if (esym.asym.st != stGlobal
3708 && esym.asym.st != stLabel
3709 && esym.asym.st != stProc)
3710 continue;
3712 switch (esym.asym.sc)
3714 case scText:
3715 case scData:
3716 case scBss:
3717 case scAbs:
3718 case scSData:
3719 case scSBss:
3720 case scRData:
3721 case scCommon:
3722 case scSCommon:
3723 case scInit:
3724 case scFini:
3725 case scRConst:
3726 def = true;
3727 break;
3728 default:
3729 def = false;
3730 break;
3733 if (! def)
3734 continue;
3736 name = ssext + esym.asym.iss;
3737 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3739 /* Unlike the generic linker, we do not pull in elements because
3740 of common symbols. */
3741 if (h == (struct bfd_link_hash_entry *) NULL
3742 || h->type != bfd_link_hash_undefined)
3743 continue;
3745 /* Include this element. */
3746 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3747 goto error_return;
3748 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3749 goto error_return;
3751 *pneeded = true;
3752 goto successful_return;
3755 successful_return:
3756 if (external_ext != NULL)
3757 free (external_ext);
3758 if (ssext != NULL)
3759 free (ssext);
3760 return true;
3761 error_return:
3762 if (external_ext != NULL)
3763 free (external_ext);
3764 if (ssext != NULL)
3765 free (ssext);
3766 return false;
3769 /* Add symbols from an ECOFF object file to the global linker hash
3770 table. */
3772 static boolean
3773 ecoff_link_add_object_symbols (abfd, info)
3774 bfd *abfd;
3775 struct bfd_link_info *info;
3777 HDRR *symhdr;
3778 bfd_size_type external_ext_size;
3779 PTR external_ext = NULL;
3780 size_t esize;
3781 char *ssext = NULL;
3782 boolean result;
3784 if (! ecoff_slurp_symbolic_header (abfd))
3785 return false;
3787 /* If there are no symbols, we don't want it. */
3788 if (bfd_get_symcount (abfd) == 0)
3789 return true;
3791 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3793 /* Read in the external symbols and external strings. */
3794 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3795 esize = symhdr->iextMax * external_ext_size;
3796 external_ext = (PTR) bfd_malloc (esize);
3797 if (external_ext == NULL && esize != 0)
3798 goto error_return;
3800 if (bfd_seek (abfd, symhdr->cbExtOffset, SEEK_SET) != 0
3801 || bfd_read (external_ext, 1, esize, abfd) != esize)
3802 goto error_return;
3804 ssext = (char *) bfd_malloc (symhdr->issExtMax);
3805 if (ssext == NULL && symhdr->issExtMax != 0)
3806 goto error_return;
3808 if (bfd_seek (abfd, symhdr->cbSsExtOffset, SEEK_SET) != 0
3809 || (bfd_read (ssext, 1, symhdr->issExtMax, abfd)
3810 != (bfd_size_type) symhdr->issExtMax))
3811 goto error_return;
3813 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3815 if (ssext != NULL)
3816 free (ssext);
3817 if (external_ext != NULL)
3818 free (external_ext);
3819 return result;
3821 error_return:
3822 if (ssext != NULL)
3823 free (ssext);
3824 if (external_ext != NULL)
3825 free (external_ext);
3826 return false;
3829 /* Add the external symbols of an object file to the global linker
3830 hash table. The external symbols and strings we are passed are
3831 just allocated on the stack, and will be discarded. We must
3832 explicitly save any information we may need later on in the link.
3833 We do not want to read the external symbol information again. */
3835 static boolean
3836 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3837 bfd *abfd;
3838 struct bfd_link_info *info;
3839 PTR external_ext;
3840 char *ssext;
3842 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3843 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3844 = backend->debug_swap.swap_ext_in;
3845 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3846 unsigned long ext_count;
3847 struct ecoff_link_hash_entry **sym_hash;
3848 char *ext_ptr;
3849 char *ext_end;
3851 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3853 sym_hash = ((struct ecoff_link_hash_entry **)
3854 bfd_alloc (abfd,
3855 ext_count * sizeof (struct bfd_link_hash_entry *)));
3856 if (!sym_hash)
3857 return false;
3858 ecoff_data (abfd)->sym_hashes = sym_hash;
3860 ext_ptr = (char *) external_ext;
3861 ext_end = ext_ptr + ext_count * external_ext_size;
3862 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3864 EXTR esym;
3865 boolean skip;
3866 bfd_vma value;
3867 asection *section;
3868 const char *name;
3869 struct ecoff_link_hash_entry *h;
3871 *sym_hash = NULL;
3873 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3875 /* Skip debugging symbols. */
3876 skip = false;
3877 switch (esym.asym.st)
3879 case stGlobal:
3880 case stStatic:
3881 case stLabel:
3882 case stProc:
3883 case stStaticProc:
3884 break;
3885 default:
3886 skip = true;
3887 break;
3890 if (skip)
3891 continue;
3893 /* Get the information for this symbol. */
3894 value = esym.asym.value;
3895 switch (esym.asym.sc)
3897 default:
3898 case scNil:
3899 case scRegister:
3900 case scCdbLocal:
3901 case scBits:
3902 case scCdbSystem:
3903 case scRegImage:
3904 case scInfo:
3905 case scUserStruct:
3906 case scVar:
3907 case scVarRegister:
3908 case scVariant:
3909 case scBasedVar:
3910 case scXData:
3911 case scPData:
3912 section = NULL;
3913 break;
3914 case scText:
3915 section = bfd_make_section_old_way (abfd, ".text");
3916 value -= section->vma;
3917 break;
3918 case scData:
3919 section = bfd_make_section_old_way (abfd, ".data");
3920 value -= section->vma;
3921 break;
3922 case scBss:
3923 section = bfd_make_section_old_way (abfd, ".bss");
3924 value -= section->vma;
3925 break;
3926 case scAbs:
3927 section = bfd_abs_section_ptr;
3928 break;
3929 case scUndefined:
3930 section = bfd_und_section_ptr;
3931 break;
3932 case scSData:
3933 section = bfd_make_section_old_way (abfd, ".sdata");
3934 value -= section->vma;
3935 break;
3936 case scSBss:
3937 section = bfd_make_section_old_way (abfd, ".sbss");
3938 value -= section->vma;
3939 break;
3940 case scRData:
3941 section = bfd_make_section_old_way (abfd, ".rdata");
3942 value -= section->vma;
3943 break;
3944 case scCommon:
3945 if (value > ecoff_data (abfd)->gp_size)
3947 section = bfd_com_section_ptr;
3948 break;
3950 /* Fall through. */
3951 case scSCommon:
3952 if (ecoff_scom_section.name == NULL)
3954 /* Initialize the small common section. */
3955 ecoff_scom_section.name = SCOMMON;
3956 ecoff_scom_section.flags = SEC_IS_COMMON;
3957 ecoff_scom_section.output_section = &ecoff_scom_section;
3958 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3959 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3960 ecoff_scom_symbol.name = SCOMMON;
3961 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3962 ecoff_scom_symbol.section = &ecoff_scom_section;
3963 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3965 section = &ecoff_scom_section;
3966 break;
3967 case scSUndefined:
3968 section = bfd_und_section_ptr;
3969 break;
3970 case scInit:
3971 section = bfd_make_section_old_way (abfd, ".init");
3972 value -= section->vma;
3973 break;
3974 case scFini:
3975 section = bfd_make_section_old_way (abfd, ".fini");
3976 value -= section->vma;
3977 break;
3978 case scRConst:
3979 section = bfd_make_section_old_way (abfd, ".rconst");
3980 value -= section->vma;
3981 break;
3984 if (section == (asection *) NULL)
3985 continue;
3987 name = ssext + esym.asym.iss;
3989 h = NULL;
3990 if (! (_bfd_generic_link_add_one_symbol
3991 (info, abfd, name,
3992 esym.weakext ? BSF_WEAK : BSF_GLOBAL,
3993 section, value, (const char *) NULL, true, true,
3994 (struct bfd_link_hash_entry **) &h)))
3995 return false;
3997 *sym_hash = h;
3999 /* If we are building an ECOFF hash table, save the external
4000 symbol information. */
4001 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
4003 if (h->abfd == (bfd *) NULL
4004 || (! bfd_is_und_section (section)
4005 && (! bfd_is_com_section (section)
4006 || (h->root.type != bfd_link_hash_defined
4007 && h->root.type != bfd_link_hash_defweak))))
4009 h->abfd = abfd;
4010 h->esym = esym;
4013 /* Remember whether this symbol was small undefined. */
4014 if (esym.asym.sc == scSUndefined)
4015 h->small = 1;
4017 /* If this symbol was ever small undefined, it needs to wind
4018 up in a GP relative section. We can't control the
4019 section of a defined symbol, but we can control the
4020 section of a common symbol. This case is actually needed
4021 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4022 if (h->small
4023 && h->root.type == bfd_link_hash_common
4024 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4026 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4027 SCOMMON);
4028 h->root.u.c.p->section->flags = SEC_ALLOC;
4029 if (h->esym.asym.sc == scCommon)
4030 h->esym.asym.sc = scSCommon;
4035 return true;
4038 /* ECOFF final link routines. */
4040 static boolean ecoff_final_link_debug_accumulate
4041 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4042 PTR handle));
4043 static boolean ecoff_link_write_external
4044 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4045 static boolean ecoff_indirect_link_order
4046 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4047 struct bfd_link_order *));
4048 static boolean ecoff_reloc_link_order
4049 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4050 struct bfd_link_order *));
4052 /* Structure used to pass information to ecoff_link_write_external. */
4054 struct extsym_info
4056 bfd *abfd;
4057 struct bfd_link_info *info;
4060 /* ECOFF final link routine. This looks through all the input BFDs
4061 and gathers together all the debugging information, and then
4062 processes all the link order information. This may cause it to
4063 close and reopen some input BFDs; I'll see how bad this is. */
4065 boolean
4066 _bfd_ecoff_bfd_final_link (abfd, info)
4067 bfd *abfd;
4068 struct bfd_link_info *info;
4070 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4071 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4072 HDRR *symhdr;
4073 PTR handle;
4074 register bfd *input_bfd;
4075 asection *o;
4076 struct bfd_link_order *p;
4077 struct extsym_info einfo;
4079 /* We accumulate the debugging information counts in the symbolic
4080 header. */
4081 symhdr = &debug->symbolic_header;
4082 symhdr->vstamp = 0;
4083 symhdr->ilineMax = 0;
4084 symhdr->cbLine = 0;
4085 symhdr->idnMax = 0;
4086 symhdr->ipdMax = 0;
4087 symhdr->isymMax = 0;
4088 symhdr->ioptMax = 0;
4089 symhdr->iauxMax = 0;
4090 symhdr->issMax = 0;
4091 symhdr->issExtMax = 0;
4092 symhdr->ifdMax = 0;
4093 symhdr->crfd = 0;
4094 symhdr->iextMax = 0;
4096 /* We accumulate the debugging information itself in the debug_info
4097 structure. */
4098 debug->line = NULL;
4099 debug->external_dnr = NULL;
4100 debug->external_pdr = NULL;
4101 debug->external_sym = NULL;
4102 debug->external_opt = NULL;
4103 debug->external_aux = NULL;
4104 debug->ss = NULL;
4105 debug->ssext = debug->ssext_end = NULL;
4106 debug->external_fdr = NULL;
4107 debug->external_rfd = NULL;
4108 debug->external_ext = debug->external_ext_end = NULL;
4110 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4111 if (handle == (PTR) NULL)
4112 return false;
4114 /* Accumulate the debugging symbols from each input BFD. */
4115 for (input_bfd = info->input_bfds;
4116 input_bfd != (bfd *) NULL;
4117 input_bfd = input_bfd->link_next)
4119 boolean ret;
4121 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4123 /* Abitrarily set the symbolic header vstamp to the vstamp
4124 of the first object file in the link. */
4125 if (symhdr->vstamp == 0)
4126 symhdr->vstamp
4127 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4128 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4129 handle);
4131 else
4132 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4133 debug, &backend->debug_swap,
4134 input_bfd, info);
4135 if (! ret)
4136 return false;
4138 /* Combine the register masks. */
4139 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4140 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4141 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4142 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4143 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4144 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4147 /* Write out the external symbols. */
4148 einfo.abfd = abfd;
4149 einfo.info = info;
4150 ecoff_link_hash_traverse (ecoff_hash_table (info),
4151 ecoff_link_write_external,
4152 (PTR) &einfo);
4154 if (info->relocateable)
4156 /* We need to make a pass over the link_orders to count up the
4157 number of relocations we will need to output, so that we know
4158 how much space they will take up. */
4159 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4161 o->reloc_count = 0;
4162 for (p = o->link_order_head;
4163 p != (struct bfd_link_order *) NULL;
4164 p = p->next)
4165 if (p->type == bfd_indirect_link_order)
4166 o->reloc_count += p->u.indirect.section->reloc_count;
4167 else if (p->type == bfd_section_reloc_link_order
4168 || p->type == bfd_symbol_reloc_link_order)
4169 ++o->reloc_count;
4173 /* Compute the reloc and symbol file positions. */
4174 ecoff_compute_reloc_file_positions (abfd);
4176 /* Write out the debugging information. */
4177 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4178 &backend->debug_swap, info,
4179 ecoff_data (abfd)->sym_filepos))
4180 return false;
4182 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4184 if (info->relocateable)
4186 /* Now reset the reloc_count field of the sections in the output
4187 BFD to 0, so that we can use them to keep track of how many
4188 relocs we have output thus far. */
4189 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4190 o->reloc_count = 0;
4193 /* Get a value for the GP register. */
4194 if (ecoff_data (abfd)->gp == 0)
4196 struct bfd_link_hash_entry *h;
4198 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4199 if (h != (struct bfd_link_hash_entry *) NULL
4200 && h->type == bfd_link_hash_defined)
4201 ecoff_data (abfd)->gp = (h->u.def.value
4202 + h->u.def.section->output_section->vma
4203 + h->u.def.section->output_offset);
4204 else if (info->relocateable)
4206 bfd_vma lo;
4208 /* Make up a value. */
4209 lo = (bfd_vma) -1;
4210 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4212 if (o->vma < lo
4213 && (strcmp (o->name, _SBSS) == 0
4214 || strcmp (o->name, _SDATA) == 0
4215 || strcmp (o->name, _LIT4) == 0
4216 || strcmp (o->name, _LIT8) == 0
4217 || strcmp (o->name, _LITA) == 0))
4218 lo = o->vma;
4220 ecoff_data (abfd)->gp = lo + 0x8000;
4222 else
4224 /* If the relocate_section function needs to do a reloc
4225 involving the GP value, it should make a reloc_dangerous
4226 callback to warn that GP is not defined. */
4230 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4232 for (p = o->link_order_head;
4233 p != (struct bfd_link_order *) NULL;
4234 p = p->next)
4236 if (p->type == bfd_indirect_link_order
4237 && (bfd_get_flavour (p->u.indirect.section->owner)
4238 == bfd_target_ecoff_flavour))
4240 if (! ecoff_indirect_link_order (abfd, info, o, p))
4241 return false;
4243 else if (p->type == bfd_section_reloc_link_order
4244 || p->type == bfd_symbol_reloc_link_order)
4246 if (! ecoff_reloc_link_order (abfd, info, o, p))
4247 return false;
4249 else
4251 if (! _bfd_default_link_order (abfd, info, o, p))
4252 return false;
4257 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4259 ecoff_data (abfd)->linker = true;
4261 return true;
4264 /* Accumulate the debugging information for an input BFD into the
4265 output BFD. This must read in the symbolic information of the
4266 input BFD. */
4268 static boolean
4269 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4270 bfd *output_bfd;
4271 bfd *input_bfd;
4272 struct bfd_link_info *info;
4273 PTR handle;
4275 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4276 const struct ecoff_debug_swap * const swap =
4277 &ecoff_backend (input_bfd)->debug_swap;
4278 HDRR *symhdr = &debug->symbolic_header;
4279 boolean ret;
4281 #define READ(ptr, offset, count, size, type) \
4282 if (symhdr->count == 0) \
4283 debug->ptr = NULL; \
4284 else \
4286 debug->ptr = (type) bfd_malloc ((size_t) (size * symhdr->count)); \
4287 if (debug->ptr == NULL) \
4289 ret = false; \
4290 goto return_something; \
4292 if ((bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) \
4293 != 0) \
4294 || (bfd_read (debug->ptr, size, symhdr->count, \
4295 input_bfd) != size * symhdr->count)) \
4297 ret = false; \
4298 goto return_something; \
4302 /* If raw_syments is not NULL, then the data was already by read by
4303 _bfd_ecoff_slurp_symbolic_info. */
4304 if (ecoff_data (input_bfd)->raw_syments == NULL)
4306 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4307 unsigned char *);
4308 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4309 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4310 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4311 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4312 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4313 union aux_ext *);
4314 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4315 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4316 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4318 #undef READ
4320 /* We do not read the external strings or the external symbols. */
4322 ret = (bfd_ecoff_debug_accumulate
4323 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4324 &ecoff_backend (output_bfd)->debug_swap,
4325 input_bfd, debug, swap, info));
4327 return_something:
4328 if (ecoff_data (input_bfd)->raw_syments == NULL)
4330 if (debug->line != NULL)
4331 free (debug->line);
4332 if (debug->external_dnr != NULL)
4333 free (debug->external_dnr);
4334 if (debug->external_pdr != NULL)
4335 free (debug->external_pdr);
4336 if (debug->external_sym != NULL)
4337 free (debug->external_sym);
4338 if (debug->external_opt != NULL)
4339 free (debug->external_opt);
4340 if (debug->external_aux != NULL)
4341 free (debug->external_aux);
4342 if (debug->ss != NULL)
4343 free (debug->ss);
4344 if (debug->external_fdr != NULL)
4345 free (debug->external_fdr);
4346 if (debug->external_rfd != NULL)
4347 free (debug->external_rfd);
4349 /* Make sure we don't accidentally follow one of these pointers
4350 into freed memory. */
4351 debug->line = NULL;
4352 debug->external_dnr = NULL;
4353 debug->external_pdr = NULL;
4354 debug->external_sym = NULL;
4355 debug->external_opt = NULL;
4356 debug->external_aux = NULL;
4357 debug->ss = NULL;
4358 debug->external_fdr = NULL;
4359 debug->external_rfd = NULL;
4362 return ret;
4365 /* Put out information for an external symbol. These come only from
4366 the hash table. */
4368 static boolean
4369 ecoff_link_write_external (h, data)
4370 struct ecoff_link_hash_entry *h;
4371 PTR data;
4373 struct extsym_info *einfo = (struct extsym_info *) data;
4374 bfd *output_bfd = einfo->abfd;
4375 boolean strip;
4377 /* We need to check if this symbol is being stripped. */
4378 if (h->root.type == bfd_link_hash_undefined
4379 || h->root.type == bfd_link_hash_undefweak)
4380 strip = false;
4381 else if (einfo->info->strip == strip_all
4382 || (einfo->info->strip == strip_some
4383 && bfd_hash_lookup (einfo->info->keep_hash,
4384 h->root.root.string,
4385 false, false) == NULL))
4386 strip = true;
4387 else
4388 strip = false;
4390 if (strip || h->written)
4391 return true;
4393 if (h->abfd == (bfd *) NULL)
4395 h->esym.jmptbl = 0;
4396 h->esym.cobol_main = 0;
4397 h->esym.weakext = 0;
4398 h->esym.reserved = 0;
4399 h->esym.ifd = ifdNil;
4400 h->esym.asym.value = 0;
4401 h->esym.asym.st = stGlobal;
4403 if (h->root.type != bfd_link_hash_defined
4404 && h->root.type != bfd_link_hash_defweak)
4405 h->esym.asym.sc = scAbs;
4406 else
4408 asection *output_section;
4409 const char *name;
4411 output_section = h->root.u.def.section->output_section;
4412 name = bfd_section_name (output_section->owner, output_section);
4414 if (strcmp (name, _TEXT) == 0)
4415 h->esym.asym.sc = scText;
4416 else if (strcmp (name, _DATA) == 0)
4417 h->esym.asym.sc = scData;
4418 else if (strcmp (name, _SDATA) == 0)
4419 h->esym.asym.sc = scSData;
4420 else if (strcmp (name, _RDATA) == 0)
4421 h->esym.asym.sc = scRData;
4422 else if (strcmp (name, _BSS) == 0)
4423 h->esym.asym.sc = scBss;
4424 else if (strcmp (name, _SBSS) == 0)
4425 h->esym.asym.sc = scSBss;
4426 else if (strcmp (name, _INIT) == 0)
4427 h->esym.asym.sc = scInit;
4428 else if (strcmp (name, _FINI) == 0)
4429 h->esym.asym.sc = scFini;
4430 else if (strcmp (name, _PDATA) == 0)
4431 h->esym.asym.sc = scPData;
4432 else if (strcmp (name, _XDATA) == 0)
4433 h->esym.asym.sc = scXData;
4434 else if (strcmp (name, _RCONST) == 0)
4435 h->esym.asym.sc = scRConst;
4436 else
4437 h->esym.asym.sc = scAbs;
4440 h->esym.asym.reserved = 0;
4441 h->esym.asym.index = indexNil;
4443 else if (h->esym.ifd != -1)
4445 struct ecoff_debug_info *debug;
4447 /* Adjust the FDR index for the symbol by that used for the
4448 input BFD. */
4449 debug = &ecoff_data (h->abfd)->debug_info;
4450 BFD_ASSERT (h->esym.ifd >= 0
4451 && h->esym.ifd < debug->symbolic_header.ifdMax);
4452 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4455 switch (h->root.type)
4457 default:
4458 case bfd_link_hash_new:
4459 abort ();
4460 case bfd_link_hash_undefined:
4461 case bfd_link_hash_undefweak:
4462 if (h->esym.asym.sc != scUndefined
4463 && h->esym.asym.sc != scSUndefined)
4464 h->esym.asym.sc = scUndefined;
4465 break;
4466 case bfd_link_hash_defined:
4467 case bfd_link_hash_defweak:
4468 if (h->esym.asym.sc == scUndefined
4469 || h->esym.asym.sc == scSUndefined)
4470 h->esym.asym.sc = scAbs;
4471 else if (h->esym.asym.sc == scCommon)
4472 h->esym.asym.sc = scBss;
4473 else if (h->esym.asym.sc == scSCommon)
4474 h->esym.asym.sc = scSBss;
4475 h->esym.asym.value = (h->root.u.def.value
4476 + h->root.u.def.section->output_section->vma
4477 + h->root.u.def.section->output_offset);
4478 break;
4479 case bfd_link_hash_common:
4480 if (h->esym.asym.sc != scCommon
4481 && h->esym.asym.sc != scSCommon)
4482 h->esym.asym.sc = scCommon;
4483 h->esym.asym.value = h->root.u.c.size;
4484 break;
4485 case bfd_link_hash_indirect:
4486 case bfd_link_hash_warning:
4487 /* FIXME: Ignore these for now. The circumstances under which
4488 they should be written out are not clear to me. */
4489 return true;
4492 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4493 symbol number. */
4494 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4495 h->written = 1;
4497 return (bfd_ecoff_debug_one_external
4498 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4499 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4500 &h->esym));
4503 /* Relocate and write an ECOFF section into an ECOFF output file. */
4505 static boolean
4506 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4507 bfd *output_bfd;
4508 struct bfd_link_info *info;
4509 asection *output_section;
4510 struct bfd_link_order *link_order;
4512 asection *input_section;
4513 bfd *input_bfd;
4514 struct ecoff_section_tdata *section_tdata;
4515 bfd_size_type raw_size;
4516 bfd_size_type cooked_size;
4517 bfd_byte *contents = NULL;
4518 bfd_size_type external_reloc_size;
4519 bfd_size_type external_relocs_size;
4520 PTR external_relocs = NULL;
4522 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4524 if (link_order->size == 0)
4525 return true;
4527 input_section = link_order->u.indirect.section;
4528 input_bfd = input_section->owner;
4529 section_tdata = ecoff_section_data (input_bfd, input_section);
4531 raw_size = input_section->_raw_size;
4532 cooked_size = input_section->_cooked_size;
4533 if (cooked_size == 0)
4534 cooked_size = raw_size;
4536 BFD_ASSERT (input_section->output_section == output_section);
4537 BFD_ASSERT (input_section->output_offset == link_order->offset);
4538 BFD_ASSERT (cooked_size == link_order->size);
4540 /* Get the section contents. We allocate memory for the larger of
4541 the size before relocating and the size after relocating. */
4542 contents = (bfd_byte *) bfd_malloc (raw_size >= cooked_size
4543 ? (size_t) raw_size
4544 : (size_t) cooked_size);
4545 if (contents == NULL && raw_size != 0)
4546 goto error_return;
4548 /* If we are relaxing, the contents may have already been read into
4549 memory, in which case we copy them into our new buffer. We don't
4550 simply reuse the old buffer in case cooked_size > raw_size. */
4551 if (section_tdata != (struct ecoff_section_tdata *) NULL
4552 && section_tdata->contents != (bfd_byte *) NULL)
4553 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4554 else
4556 if (! bfd_get_section_contents (input_bfd, input_section,
4557 (PTR) contents,
4558 (file_ptr) 0, raw_size))
4559 goto error_return;
4562 /* Get the relocs. If we are relaxing MIPS code, they will already
4563 have been read in. Otherwise, we read them in now. */
4564 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4565 external_relocs_size = external_reloc_size * input_section->reloc_count;
4567 if (section_tdata != (struct ecoff_section_tdata *) NULL
4568 && section_tdata->external_relocs != NULL)
4569 external_relocs = section_tdata->external_relocs;
4570 else
4572 external_relocs = (PTR) bfd_malloc ((size_t) external_relocs_size);
4573 if (external_relocs == NULL && external_relocs_size != 0)
4574 goto error_return;
4576 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4577 || (bfd_read (external_relocs, 1, external_relocs_size, input_bfd)
4578 != external_relocs_size))
4579 goto error_return;
4582 /* Relocate the section contents. */
4583 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4584 (output_bfd, info, input_bfd, input_section, contents,
4585 external_relocs)))
4586 goto error_return;
4588 /* Write out the relocated section. */
4589 if (! bfd_set_section_contents (output_bfd,
4590 output_section,
4591 (PTR) contents,
4592 input_section->output_offset,
4593 cooked_size))
4594 goto error_return;
4596 /* If we are producing relocateable output, the relocs were
4597 modified, and we write them out now. We use the reloc_count
4598 field of output_section to keep track of the number of relocs we
4599 have output so far. */
4600 if (info->relocateable)
4602 if (bfd_seek (output_bfd,
4603 (output_section->rel_filepos +
4604 output_section->reloc_count * external_reloc_size),
4605 SEEK_SET) != 0
4606 || (bfd_write (external_relocs, 1, external_relocs_size, output_bfd)
4607 != external_relocs_size))
4608 goto error_return;
4609 output_section->reloc_count += input_section->reloc_count;
4612 if (contents != NULL)
4613 free (contents);
4614 if (external_relocs != NULL && section_tdata == NULL)
4615 free (external_relocs);
4616 return true;
4618 error_return:
4619 if (contents != NULL)
4620 free (contents);
4621 if (external_relocs != NULL && section_tdata == NULL)
4622 free (external_relocs);
4623 return false;
4626 /* Generate a reloc when linking an ECOFF file. This is a reloc
4627 requested by the linker, and does come from any input file. This
4628 is used to build constructor and destructor tables when linking
4629 with -Ur. */
4631 static boolean
4632 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4633 bfd *output_bfd;
4634 struct bfd_link_info *info;
4635 asection *output_section;
4636 struct bfd_link_order *link_order;
4638 enum bfd_link_order_type type;
4639 asection *section;
4640 bfd_vma addend;
4641 arelent rel;
4642 struct internal_reloc in;
4643 bfd_size_type external_reloc_size;
4644 bfd_byte *rbuf;
4645 boolean ok;
4647 type = link_order->type;
4648 section = NULL;
4649 addend = link_order->u.reloc.p->addend;
4651 /* We set up an arelent to pass to the backend adjust_reloc_out
4652 routine. */
4653 rel.address = link_order->offset;
4655 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4656 if (rel.howto == 0)
4658 bfd_set_error (bfd_error_bad_value);
4659 return false;
4662 if (type == bfd_section_reloc_link_order)
4664 section = link_order->u.reloc.p->u.section;
4665 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4667 else
4669 struct bfd_link_hash_entry *h;
4671 /* Treat a reloc against a defined symbol as though it were
4672 actually against the section. */
4673 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4674 link_order->u.reloc.p->u.name,
4675 false, false, false);
4676 if (h != NULL
4677 && (h->type == bfd_link_hash_defined
4678 || h->type == bfd_link_hash_defweak))
4680 type = bfd_section_reloc_link_order;
4681 section = h->u.def.section->output_section;
4682 /* It seems that we ought to add the symbol value to the
4683 addend here, but in practice it has already been added
4684 because it was passed to constructor_callback. */
4685 addend += section->vma + h->u.def.section->output_offset;
4687 else
4689 /* We can't set up a reloc against a symbol correctly,
4690 because we have no asymbol structure. Currently no
4691 adjust_reloc_out routine cares. */
4692 rel.sym_ptr_ptr = (asymbol **) NULL;
4696 /* All ECOFF relocs are in-place. Put the addend into the object
4697 file. */
4699 BFD_ASSERT (rel.howto->partial_inplace);
4700 if (addend != 0)
4702 bfd_size_type size;
4703 bfd_reloc_status_type rstat;
4704 bfd_byte *buf;
4705 boolean ok;
4707 size = bfd_get_reloc_size (rel.howto);
4708 buf = (bfd_byte *) bfd_zmalloc (size);
4709 if (buf == (bfd_byte *) NULL)
4710 return false;
4711 rstat = _bfd_relocate_contents (rel.howto, output_bfd, addend, buf);
4712 switch (rstat)
4714 case bfd_reloc_ok:
4715 break;
4716 default:
4717 case bfd_reloc_outofrange:
4718 abort ();
4719 case bfd_reloc_overflow:
4720 if (! ((*info->callbacks->reloc_overflow)
4721 (info,
4722 (link_order->type == bfd_section_reloc_link_order
4723 ? bfd_section_name (output_bfd, section)
4724 : link_order->u.reloc.p->u.name),
4725 rel.howto->name, addend, (bfd *) NULL,
4726 (asection *) NULL, (bfd_vma) 0)))
4728 free (buf);
4729 return false;
4731 break;
4733 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4734 (file_ptr) link_order->offset, size);
4735 free (buf);
4736 if (! ok)
4737 return false;
4740 rel.addend = 0;
4742 /* Move the information into a internal_reloc structure. */
4743 in.r_vaddr = (rel.address
4744 + bfd_get_section_vma (output_bfd, output_section));
4745 in.r_type = rel.howto->type;
4747 if (type == bfd_symbol_reloc_link_order)
4749 struct ecoff_link_hash_entry *h;
4751 h = ((struct ecoff_link_hash_entry *)
4752 bfd_wrapped_link_hash_lookup (output_bfd, info,
4753 link_order->u.reloc.p->u.name,
4754 false, false, true));
4755 if (h != (struct ecoff_link_hash_entry *) NULL
4756 && h->indx != -1)
4757 in.r_symndx = h->indx;
4758 else
4760 if (! ((*info->callbacks->unattached_reloc)
4761 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4762 (asection *) NULL, (bfd_vma) 0)))
4763 return false;
4764 in.r_symndx = 0;
4766 in.r_extern = 1;
4768 else
4770 CONST char *name;
4772 name = bfd_get_section_name (output_bfd, section);
4773 if (strcmp (name, ".text") == 0)
4774 in.r_symndx = RELOC_SECTION_TEXT;
4775 else if (strcmp (name, ".rdata") == 0)
4776 in.r_symndx = RELOC_SECTION_RDATA;
4777 else if (strcmp (name, ".data") == 0)
4778 in.r_symndx = RELOC_SECTION_DATA;
4779 else if (strcmp (name, ".sdata") == 0)
4780 in.r_symndx = RELOC_SECTION_SDATA;
4781 else if (strcmp (name, ".sbss") == 0)
4782 in.r_symndx = RELOC_SECTION_SBSS;
4783 else if (strcmp (name, ".bss") == 0)
4784 in.r_symndx = RELOC_SECTION_BSS;
4785 else if (strcmp (name, ".init") == 0)
4786 in.r_symndx = RELOC_SECTION_INIT;
4787 else if (strcmp (name, ".lit8") == 0)
4788 in.r_symndx = RELOC_SECTION_LIT8;
4789 else if (strcmp (name, ".lit4") == 0)
4790 in.r_symndx = RELOC_SECTION_LIT4;
4791 else if (strcmp (name, ".xdata") == 0)
4792 in.r_symndx = RELOC_SECTION_XDATA;
4793 else if (strcmp (name, ".pdata") == 0)
4794 in.r_symndx = RELOC_SECTION_PDATA;
4795 else if (strcmp (name, ".fini") == 0)
4796 in.r_symndx = RELOC_SECTION_FINI;
4797 else if (strcmp (name, ".lita") == 0)
4798 in.r_symndx = RELOC_SECTION_LITA;
4799 else if (strcmp (name, "*ABS*") == 0)
4800 in.r_symndx = RELOC_SECTION_ABS;
4801 else if (strcmp (name, ".rconst") == 0)
4802 in.r_symndx = RELOC_SECTION_RCONST;
4803 else
4804 abort ();
4805 in.r_extern = 0;
4808 /* Let the BFD backend adjust the reloc. */
4809 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4811 /* Get some memory and swap out the reloc. */
4812 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4813 rbuf = (bfd_byte *) bfd_malloc ((size_t) external_reloc_size);
4814 if (rbuf == (bfd_byte *) NULL)
4815 return false;
4817 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4819 ok = (bfd_seek (output_bfd,
4820 (output_section->rel_filepos +
4821 output_section->reloc_count * external_reloc_size),
4822 SEEK_SET) == 0
4823 && (bfd_write ((PTR) rbuf, 1, external_reloc_size, output_bfd)
4824 == external_reloc_size));
4826 if (ok)
4827 ++output_section->reloc_count;
4829 free (rbuf);
4831 return ok;