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