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