source: trunk/src/binutils/bfd/ecoff.c@ 2002

Last change on this file since 2002 was 610, checked in by bird, 22 years ago

This commit was generated by cvs2svn to compensate for changes in r609,
which included commits to RCS files with non-trunk default branches.

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