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

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

Initial revision

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