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