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