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