]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/ecoff.c
* aoutx.h (some_aout_object_p): Clean up tdata properly on error.
[thirdparty/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 if (bfd_bread ((PTR) armag, (bfd_size_type) SARMAG, abfd) != SARMAG)
3293 {
3294 if (bfd_get_error () != bfd_error_system_call)
3295 bfd_set_error (bfd_error_wrong_format);
3296 return (const bfd_target *) NULL;
3297 }
3298
3299 if (strncmp (armag, ARMAG, SARMAG) != 0)
3300 {
3301 bfd_set_error (bfd_error_wrong_format);
3302 return NULL;
3303 }
3304
3305 tdata_hold = bfd_ardata (abfd);
3306
3307 amt = sizeof (struct artdata);
3308 bfd_ardata (abfd) = (struct artdata *) bfd_zalloc (abfd, amt);
3309 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3310 {
3311 bfd_ardata (abfd) = tdata_hold;
3312 return (const bfd_target *) NULL;
3313 }
3314
3315 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3316 bfd_ardata (abfd)->cache = NULL;
3317 bfd_ardata (abfd)->archive_head = NULL;
3318 bfd_ardata (abfd)->symdefs = NULL;
3319 bfd_ardata (abfd)->extended_names = NULL;
3320 bfd_ardata (abfd)->tdata = NULL;
3321
3322 if (! _bfd_ecoff_slurp_armap (abfd)
3323 || ! _bfd_ecoff_slurp_extended_name_table (abfd))
3324 {
3325 bfd_release (abfd, bfd_ardata (abfd));
3326 bfd_ardata (abfd) = tdata_hold;
3327 return (const bfd_target *) NULL;
3328 }
3329
3330 if (bfd_has_map (abfd))
3331 {
3332 bfd *first;
3333
3334 /* This archive has a map, so we may presume that the contents
3335 are object files. Make sure that if the first file in the
3336 archive can be recognized as an object file, it is for this
3337 target. If not, assume that this is the wrong format. If
3338 the first file is not an object file, somebody is doing
3339 something weird, and we permit it so that ar -t will work. */
3340
3341 first = bfd_openr_next_archived_file (abfd, (bfd *) NULL);
3342 if (first != NULL)
3343 {
3344 first->target_defaulted = false;
3345 if (bfd_check_format (first, bfd_object)
3346 && first->xvec != abfd->xvec)
3347 {
3348 #if 0
3349 /* We ought to close `first' here, but we can't, because
3350 we have no way to remove it from the archive cache.
3351 It's close to impossible to figure out when we can
3352 release bfd_ardata. FIXME. */
3353 (void) bfd_close (first);
3354 bfd_release (abfd, bfd_ardata (abfd));
3355 #endif
3356 bfd_set_error (bfd_error_wrong_object_format);
3357 bfd_ardata (abfd) = tdata_hold;
3358 return NULL;
3359 }
3360 /* And we ought to close `first' here too. */
3361 }
3362 }
3363
3364 return abfd->xvec;
3365 }
3366 \f
3367 /* ECOFF linker code. */
3368
3369 static struct bfd_hash_entry *ecoff_link_hash_newfunc
3370 PARAMS ((struct bfd_hash_entry *entry,
3371 struct bfd_hash_table *table,
3372 const char *string));
3373 static boolean ecoff_link_add_archive_symbols
3374 PARAMS ((bfd *, struct bfd_link_info *));
3375 static boolean ecoff_link_check_archive_element
3376 PARAMS ((bfd *, struct bfd_link_info *, boolean *pneeded));
3377 static boolean ecoff_link_add_object_symbols
3378 PARAMS ((bfd *, struct bfd_link_info *));
3379 static boolean ecoff_link_add_externals
3380 PARAMS ((bfd *, struct bfd_link_info *, PTR, char *));
3381
3382 /* Routine to create an entry in an ECOFF link hash table. */
3383
3384 static struct bfd_hash_entry *
3385 ecoff_link_hash_newfunc (entry, table, string)
3386 struct bfd_hash_entry *entry;
3387 struct bfd_hash_table *table;
3388 const char *string;
3389 {
3390 struct ecoff_link_hash_entry *ret = (struct ecoff_link_hash_entry *) entry;
3391
3392 /* Allocate the structure if it has not already been allocated by a
3393 subclass. */
3394 if (ret == (struct ecoff_link_hash_entry *) NULL)
3395 ret = ((struct ecoff_link_hash_entry *)
3396 bfd_hash_allocate (table, sizeof (struct ecoff_link_hash_entry)));
3397 if (ret == (struct ecoff_link_hash_entry *) NULL)
3398 return NULL;
3399
3400 /* Call the allocation method of the superclass. */
3401 ret = ((struct ecoff_link_hash_entry *)
3402 _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3403 table, string));
3404
3405 if (ret)
3406 {
3407 /* Set local fields. */
3408 ret->indx = -1;
3409 ret->abfd = NULL;
3410 ret->written = 0;
3411 ret->small = 0;
3412 }
3413 memset ((PTR) &ret->esym, 0, sizeof ret->esym);
3414
3415 return (struct bfd_hash_entry *) ret;
3416 }
3417
3418 /* Create an ECOFF link hash table. */
3419
3420 struct bfd_link_hash_table *
3421 _bfd_ecoff_bfd_link_hash_table_create (abfd)
3422 bfd *abfd;
3423 {
3424 struct ecoff_link_hash_table *ret;
3425 bfd_size_type amt = sizeof (struct ecoff_link_hash_table);
3426
3427 ret = (struct ecoff_link_hash_table *) bfd_malloc (amt);
3428 if (ret == NULL)
3429 return NULL;
3430 if (! _bfd_link_hash_table_init (&ret->root, abfd,
3431 ecoff_link_hash_newfunc))
3432 {
3433 free (ret);
3434 return (struct bfd_link_hash_table *) NULL;
3435 }
3436 return &ret->root;
3437 }
3438
3439 /* Look up an entry in an ECOFF link hash table. */
3440
3441 #define ecoff_link_hash_lookup(table, string, create, copy, follow) \
3442 ((struct ecoff_link_hash_entry *) \
3443 bfd_link_hash_lookup (&(table)->root, (string), (create), (copy), (follow)))
3444
3445 /* Traverse an ECOFF link hash table. */
3446
3447 #define ecoff_link_hash_traverse(table, func, info) \
3448 (bfd_link_hash_traverse \
3449 (&(table)->root, \
3450 (boolean (*) PARAMS ((struct bfd_link_hash_entry *, PTR))) (func), \
3451 (info)))
3452
3453 /* Get the ECOFF link hash table from the info structure. This is
3454 just a cast. */
3455
3456 #define ecoff_hash_table(p) ((struct ecoff_link_hash_table *) ((p)->hash))
3457
3458 /* Given an ECOFF BFD, add symbols to the global hash table as
3459 appropriate. */
3460
3461 boolean
3462 _bfd_ecoff_bfd_link_add_symbols (abfd, info)
3463 bfd *abfd;
3464 struct bfd_link_info *info;
3465 {
3466 switch (bfd_get_format (abfd))
3467 {
3468 case bfd_object:
3469 return ecoff_link_add_object_symbols (abfd, info);
3470 case bfd_archive:
3471 return ecoff_link_add_archive_symbols (abfd, info);
3472 default:
3473 bfd_set_error (bfd_error_wrong_format);
3474 return false;
3475 }
3476 }
3477
3478 /* Add the symbols from an archive file to the global hash table.
3479 This looks through the undefined symbols, looks each one up in the
3480 archive hash table, and adds any associated object file. We do not
3481 use _bfd_generic_link_add_archive_symbols because ECOFF archives
3482 already have a hash table, so there is no reason to construct
3483 another one. */
3484
3485 static boolean
3486 ecoff_link_add_archive_symbols (abfd, info)
3487 bfd *abfd;
3488 struct bfd_link_info *info;
3489 {
3490 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3491 const bfd_byte *raw_armap;
3492 struct bfd_link_hash_entry **pundef;
3493 unsigned int armap_count;
3494 unsigned int armap_log;
3495 unsigned int i;
3496 const bfd_byte *hashtable;
3497 const char *stringbase;
3498
3499 if (! bfd_has_map (abfd))
3500 {
3501 /* An empty archive is a special case. */
3502 if (bfd_openr_next_archived_file (abfd, (bfd *) NULL) == NULL)
3503 return true;
3504 bfd_set_error (bfd_error_no_armap);
3505 return false;
3506 }
3507
3508 /* If we don't have any raw data for this archive, as can happen on
3509 Irix 4.0.5F, we call the generic routine.
3510 FIXME: We should be more clever about this, since someday tdata
3511 may get to something for a generic archive. */
3512 raw_armap = (const bfd_byte *) bfd_ardata (abfd)->tdata;
3513 if (raw_armap == (bfd_byte *) NULL)
3514 return (_bfd_generic_link_add_archive_symbols
3515 (abfd, info, ecoff_link_check_archive_element));
3516
3517 armap_count = H_GET_32 (abfd, raw_armap);
3518
3519 armap_log = 0;
3520 for (i = 1; i < armap_count; i <<= 1)
3521 armap_log++;
3522 BFD_ASSERT (i == armap_count);
3523
3524 hashtable = raw_armap + 4;
3525 stringbase = (const char *) raw_armap + armap_count * 8 + 8;
3526
3527 /* Look through the list of undefined symbols. */
3528 pundef = &info->hash->undefs;
3529 while (*pundef != (struct bfd_link_hash_entry *) NULL)
3530 {
3531 struct bfd_link_hash_entry *h;
3532 unsigned int hash, rehash;
3533 unsigned int file_offset;
3534 const char *name;
3535 bfd *element;
3536
3537 h = *pundef;
3538
3539 /* When a symbol is defined, it is not necessarily removed from
3540 the list. */
3541 if (h->type != bfd_link_hash_undefined
3542 && h->type != bfd_link_hash_common)
3543 {
3544 /* Remove this entry from the list, for general cleanliness
3545 and because we are going to look through the list again
3546 if we search any more libraries. We can't remove the
3547 entry if it is the tail, because that would lose any
3548 entries we add to the list later on. */
3549 if (*pundef != info->hash->undefs_tail)
3550 *pundef = (*pundef)->next;
3551 else
3552 pundef = &(*pundef)->next;
3553 continue;
3554 }
3555
3556 /* Native ECOFF linkers do not pull in archive elements merely
3557 to satisfy common definitions, so neither do we. We leave
3558 them on the list, though, in case we are linking against some
3559 other object format. */
3560 if (h->type != bfd_link_hash_undefined)
3561 {
3562 pundef = &(*pundef)->next;
3563 continue;
3564 }
3565
3566 /* Look for this symbol in the archive hash table. */
3567 hash = ecoff_armap_hash (h->root.string, &rehash, armap_count,
3568 armap_log);
3569
3570 file_offset = H_GET_32 (abfd, hashtable + (hash * 8) + 4);
3571 if (file_offset == 0)
3572 {
3573 /* Nothing in this slot. */
3574 pundef = &(*pundef)->next;
3575 continue;
3576 }
3577
3578 name = stringbase + H_GET_32 (abfd, hashtable + (hash * 8));
3579 if (name[0] != h->root.string[0]
3580 || strcmp (name, h->root.string) != 0)
3581 {
3582 unsigned int srch;
3583 boolean found;
3584
3585 /* That was the wrong symbol. Try rehashing. */
3586 found = false;
3587 for (srch = (hash + rehash) & (armap_count - 1);
3588 srch != hash;
3589 srch = (srch + rehash) & (armap_count - 1))
3590 {
3591 file_offset = H_GET_32 (abfd, hashtable + (srch * 8) + 4);
3592 if (file_offset == 0)
3593 break;
3594 name = stringbase + H_GET_32 (abfd, hashtable + (srch * 8));
3595 if (name[0] == h->root.string[0]
3596 && strcmp (name, h->root.string) == 0)
3597 {
3598 found = true;
3599 break;
3600 }
3601 }
3602
3603 if (! found)
3604 {
3605 pundef = &(*pundef)->next;
3606 continue;
3607 }
3608
3609 hash = srch;
3610 }
3611
3612 element = (*backend->get_elt_at_filepos) (abfd, (file_ptr) file_offset);
3613 if (element == (bfd *) NULL)
3614 return false;
3615
3616 if (! bfd_check_format (element, bfd_object))
3617 return false;
3618
3619 /* Unlike the generic linker, we know that this element provides
3620 a definition for an undefined symbol and we know that we want
3621 to include it. We don't need to check anything. */
3622 if (! (*info->callbacks->add_archive_element) (info, element, name))
3623 return false;
3624 if (! ecoff_link_add_object_symbols (element, info))
3625 return false;
3626
3627 pundef = &(*pundef)->next;
3628 }
3629
3630 return true;
3631 }
3632
3633 /* This is called if we used _bfd_generic_link_add_archive_symbols
3634 because we were not dealing with an ECOFF archive. */
3635
3636 static boolean
3637 ecoff_link_check_archive_element (abfd, info, pneeded)
3638 bfd *abfd;
3639 struct bfd_link_info *info;
3640 boolean *pneeded;
3641 {
3642 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3643 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3644 = backend->debug_swap.swap_ext_in;
3645 HDRR *symhdr;
3646 bfd_size_type external_ext_size;
3647 PTR external_ext = NULL;
3648 bfd_size_type esize;
3649 char *ssext = NULL;
3650 char *ext_ptr;
3651 char *ext_end;
3652
3653 *pneeded = false;
3654
3655 if (! ecoff_slurp_symbolic_header (abfd))
3656 goto error_return;
3657
3658 /* If there are no symbols, we don't want it. */
3659 if (bfd_get_symcount (abfd) == 0)
3660 goto successful_return;
3661
3662 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3663
3664 /* Read in the external symbols and external strings. */
3665 external_ext_size = backend->debug_swap.external_ext_size;
3666 esize = symhdr->iextMax * external_ext_size;
3667 external_ext = (PTR) bfd_malloc (esize);
3668 if (external_ext == NULL && esize != 0)
3669 goto error_return;
3670
3671 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3672 || bfd_bread (external_ext, esize, abfd) != esize)
3673 goto error_return;
3674
3675 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3676 if (ssext == NULL && symhdr->issExtMax != 0)
3677 goto error_return;
3678
3679 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3680 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3681 != (bfd_size_type) symhdr->issExtMax))
3682 goto error_return;
3683
3684 /* Look through the external symbols to see if they define some
3685 symbol that is currently undefined. */
3686 ext_ptr = (char *) external_ext;
3687 ext_end = ext_ptr + esize;
3688 for (; ext_ptr < ext_end; ext_ptr += external_ext_size)
3689 {
3690 EXTR esym;
3691 boolean def;
3692 const char *name;
3693 struct bfd_link_hash_entry *h;
3694
3695 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3696
3697 /* See if this symbol defines something. */
3698 if (esym.asym.st != stGlobal
3699 && esym.asym.st != stLabel
3700 && esym.asym.st != stProc)
3701 continue;
3702
3703 switch (esym.asym.sc)
3704 {
3705 case scText:
3706 case scData:
3707 case scBss:
3708 case scAbs:
3709 case scSData:
3710 case scSBss:
3711 case scRData:
3712 case scCommon:
3713 case scSCommon:
3714 case scInit:
3715 case scFini:
3716 case scRConst:
3717 def = true;
3718 break;
3719 default:
3720 def = false;
3721 break;
3722 }
3723
3724 if (! def)
3725 continue;
3726
3727 name = ssext + esym.asym.iss;
3728 h = bfd_link_hash_lookup (info->hash, name, false, false, true);
3729
3730 /* Unlike the generic linker, we do not pull in elements because
3731 of common symbols. */
3732 if (h == (struct bfd_link_hash_entry *) NULL
3733 || h->type != bfd_link_hash_undefined)
3734 continue;
3735
3736 /* Include this element. */
3737 if (! (*info->callbacks->add_archive_element) (info, abfd, name))
3738 goto error_return;
3739 if (! ecoff_link_add_externals (abfd, info, external_ext, ssext))
3740 goto error_return;
3741
3742 *pneeded = true;
3743 goto successful_return;
3744 }
3745
3746 successful_return:
3747 if (external_ext != NULL)
3748 free (external_ext);
3749 if (ssext != NULL)
3750 free (ssext);
3751 return true;
3752 error_return:
3753 if (external_ext != NULL)
3754 free (external_ext);
3755 if (ssext != NULL)
3756 free (ssext);
3757 return false;
3758 }
3759
3760 /* Add symbols from an ECOFF object file to the global linker hash
3761 table. */
3762
3763 static boolean
3764 ecoff_link_add_object_symbols (abfd, info)
3765 bfd *abfd;
3766 struct bfd_link_info *info;
3767 {
3768 HDRR *symhdr;
3769 bfd_size_type external_ext_size;
3770 PTR external_ext = NULL;
3771 bfd_size_type esize;
3772 char *ssext = NULL;
3773 boolean result;
3774
3775 if (! ecoff_slurp_symbolic_header (abfd))
3776 return false;
3777
3778 /* If there are no symbols, we don't want it. */
3779 if (bfd_get_symcount (abfd) == 0)
3780 return true;
3781
3782 symhdr = &ecoff_data (abfd)->debug_info.symbolic_header;
3783
3784 /* Read in the external symbols and external strings. */
3785 external_ext_size = ecoff_backend (abfd)->debug_swap.external_ext_size;
3786 esize = symhdr->iextMax * external_ext_size;
3787 external_ext = (PTR) bfd_malloc (esize);
3788 if (external_ext == NULL && esize != 0)
3789 goto error_return;
3790
3791 if (bfd_seek (abfd, (file_ptr) symhdr->cbExtOffset, SEEK_SET) != 0
3792 || bfd_bread (external_ext, esize, abfd) != esize)
3793 goto error_return;
3794
3795 ssext = (char *) bfd_malloc ((bfd_size_type) symhdr->issExtMax);
3796 if (ssext == NULL && symhdr->issExtMax != 0)
3797 goto error_return;
3798
3799 if (bfd_seek (abfd, (file_ptr) symhdr->cbSsExtOffset, SEEK_SET) != 0
3800 || (bfd_bread (ssext, (bfd_size_type) symhdr->issExtMax, abfd)
3801 != (bfd_size_type) symhdr->issExtMax))
3802 goto error_return;
3803
3804 result = ecoff_link_add_externals (abfd, info, external_ext, ssext);
3805
3806 if (ssext != NULL)
3807 free (ssext);
3808 if (external_ext != NULL)
3809 free (external_ext);
3810 return result;
3811
3812 error_return:
3813 if (ssext != NULL)
3814 free (ssext);
3815 if (external_ext != NULL)
3816 free (external_ext);
3817 return false;
3818 }
3819
3820 /* Add the external symbols of an object file to the global linker
3821 hash table. The external symbols and strings we are passed are
3822 just allocated on the stack, and will be discarded. We must
3823 explicitly save any information we may need later on in the link.
3824 We do not want to read the external symbol information again. */
3825
3826 static boolean
3827 ecoff_link_add_externals (abfd, info, external_ext, ssext)
3828 bfd *abfd;
3829 struct bfd_link_info *info;
3830 PTR external_ext;
3831 char *ssext;
3832 {
3833 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
3834 void (* const swap_ext_in) PARAMS ((bfd *, PTR, EXTR *))
3835 = backend->debug_swap.swap_ext_in;
3836 bfd_size_type external_ext_size = backend->debug_swap.external_ext_size;
3837 unsigned long ext_count;
3838 struct ecoff_link_hash_entry **sym_hash;
3839 char *ext_ptr;
3840 char *ext_end;
3841 bfd_size_type amt;
3842
3843 ext_count = ecoff_data (abfd)->debug_info.symbolic_header.iextMax;
3844
3845 amt = ext_count;
3846 amt *= sizeof (struct bfd_link_hash_entry *);
3847 sym_hash = (struct ecoff_link_hash_entry **) bfd_alloc (abfd, amt);
3848 if (!sym_hash)
3849 return false;
3850 ecoff_data (abfd)->sym_hashes = sym_hash;
3851
3852 ext_ptr = (char *) external_ext;
3853 ext_end = ext_ptr + ext_count * external_ext_size;
3854 for (; ext_ptr < ext_end; ext_ptr += external_ext_size, sym_hash++)
3855 {
3856 EXTR esym;
3857 boolean skip;
3858 bfd_vma value;
3859 asection *section;
3860 const char *name;
3861 struct ecoff_link_hash_entry *h;
3862
3863 *sym_hash = NULL;
3864
3865 (*swap_ext_in) (abfd, (PTR) ext_ptr, &esym);
3866
3867 /* Skip debugging symbols. */
3868 skip = false;
3869 switch (esym.asym.st)
3870 {
3871 case stGlobal:
3872 case stStatic:
3873 case stLabel:
3874 case stProc:
3875 case stStaticProc:
3876 break;
3877 default:
3878 skip = true;
3879 break;
3880 }
3881
3882 if (skip)
3883 continue;
3884
3885 /* Get the information for this symbol. */
3886 value = esym.asym.value;
3887 switch (esym.asym.sc)
3888 {
3889 default:
3890 case scNil:
3891 case scRegister:
3892 case scCdbLocal:
3893 case scBits:
3894 case scCdbSystem:
3895 case scRegImage:
3896 case scInfo:
3897 case scUserStruct:
3898 case scVar:
3899 case scVarRegister:
3900 case scVariant:
3901 case scBasedVar:
3902 case scXData:
3903 case scPData:
3904 section = NULL;
3905 break;
3906 case scText:
3907 section = bfd_make_section_old_way (abfd, ".text");
3908 value -= section->vma;
3909 break;
3910 case scData:
3911 section = bfd_make_section_old_way (abfd, ".data");
3912 value -= section->vma;
3913 break;
3914 case scBss:
3915 section = bfd_make_section_old_way (abfd, ".bss");
3916 value -= section->vma;
3917 break;
3918 case scAbs:
3919 section = bfd_abs_section_ptr;
3920 break;
3921 case scUndefined:
3922 section = bfd_und_section_ptr;
3923 break;
3924 case scSData:
3925 section = bfd_make_section_old_way (abfd, ".sdata");
3926 value -= section->vma;
3927 break;
3928 case scSBss:
3929 section = bfd_make_section_old_way (abfd, ".sbss");
3930 value -= section->vma;
3931 break;
3932 case scRData:
3933 section = bfd_make_section_old_way (abfd, ".rdata");
3934 value -= section->vma;
3935 break;
3936 case scCommon:
3937 if (value > ecoff_data (abfd)->gp_size)
3938 {
3939 section = bfd_com_section_ptr;
3940 break;
3941 }
3942 /* Fall through. */
3943 case scSCommon:
3944 if (ecoff_scom_section.name == NULL)
3945 {
3946 /* Initialize the small common section. */
3947 ecoff_scom_section.name = SCOMMON;
3948 ecoff_scom_section.flags = SEC_IS_COMMON;
3949 ecoff_scom_section.output_section = &ecoff_scom_section;
3950 ecoff_scom_section.symbol = &ecoff_scom_symbol;
3951 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
3952 ecoff_scom_symbol.name = SCOMMON;
3953 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
3954 ecoff_scom_symbol.section = &ecoff_scom_section;
3955 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
3956 }
3957 section = &ecoff_scom_section;
3958 break;
3959 case scSUndefined:
3960 section = bfd_und_section_ptr;
3961 break;
3962 case scInit:
3963 section = bfd_make_section_old_way (abfd, ".init");
3964 value -= section->vma;
3965 break;
3966 case scFini:
3967 section = bfd_make_section_old_way (abfd, ".fini");
3968 value -= section->vma;
3969 break;
3970 case scRConst:
3971 section = bfd_make_section_old_way (abfd, ".rconst");
3972 value -= section->vma;
3973 break;
3974 }
3975
3976 if (section == (asection *) NULL)
3977 continue;
3978
3979 name = ssext + esym.asym.iss;
3980
3981 h = NULL;
3982 if (! (_bfd_generic_link_add_one_symbol
3983 (info, abfd, name,
3984 (flagword) (esym.weakext ? BSF_WEAK : BSF_GLOBAL),
3985 section, value, (const char *) NULL, true, true,
3986 (struct bfd_link_hash_entry **) &h)))
3987 return false;
3988
3989 *sym_hash = h;
3990
3991 /* If we are building an ECOFF hash table, save the external
3992 symbol information. */
3993 if (info->hash->creator->flavour == bfd_get_flavour (abfd))
3994 {
3995 if (h->abfd == (bfd *) NULL
3996 || (! bfd_is_und_section (section)
3997 && (! bfd_is_com_section (section)
3998 || (h->root.type != bfd_link_hash_defined
3999 && h->root.type != bfd_link_hash_defweak))))
4000 {
4001 h->abfd = abfd;
4002 h->esym = esym;
4003 }
4004
4005 /* Remember whether this symbol was small undefined. */
4006 if (esym.asym.sc == scSUndefined)
4007 h->small = 1;
4008
4009 /* If this symbol was ever small undefined, it needs to wind
4010 up in a GP relative section. We can't control the
4011 section of a defined symbol, but we can control the
4012 section of a common symbol. This case is actually needed
4013 on Ultrix 4.2 to handle the symbol cred in -lckrb. */
4014 if (h->small
4015 && h->root.type == bfd_link_hash_common
4016 && strcmp (h->root.u.c.p->section->name, SCOMMON) != 0)
4017 {
4018 h->root.u.c.p->section = bfd_make_section_old_way (abfd,
4019 SCOMMON);
4020 h->root.u.c.p->section->flags = SEC_ALLOC;
4021 if (h->esym.asym.sc == scCommon)
4022 h->esym.asym.sc = scSCommon;
4023 }
4024 }
4025 }
4026
4027 return true;
4028 }
4029 \f
4030 /* ECOFF final link routines. */
4031
4032 static boolean ecoff_final_link_debug_accumulate
4033 PARAMS ((bfd *output_bfd, bfd *input_bfd, struct bfd_link_info *,
4034 PTR handle));
4035 static boolean ecoff_link_write_external
4036 PARAMS ((struct ecoff_link_hash_entry *, PTR));
4037 static boolean ecoff_indirect_link_order
4038 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4039 struct bfd_link_order *));
4040 static boolean ecoff_reloc_link_order
4041 PARAMS ((bfd *, struct bfd_link_info *, asection *,
4042 struct bfd_link_order *));
4043
4044 /* Structure used to pass information to ecoff_link_write_external. */
4045
4046 struct extsym_info
4047 {
4048 bfd *abfd;
4049 struct bfd_link_info *info;
4050 };
4051
4052 /* ECOFF final link routine. This looks through all the input BFDs
4053 and gathers together all the debugging information, and then
4054 processes all the link order information. This may cause it to
4055 close and reopen some input BFDs; I'll see how bad this is. */
4056
4057 boolean
4058 _bfd_ecoff_bfd_final_link (abfd, info)
4059 bfd *abfd;
4060 struct bfd_link_info *info;
4061 {
4062 const struct ecoff_backend_data * const backend = ecoff_backend (abfd);
4063 struct ecoff_debug_info * const debug = &ecoff_data (abfd)->debug_info;
4064 HDRR *symhdr;
4065 PTR handle;
4066 register bfd *input_bfd;
4067 asection *o;
4068 struct bfd_link_order *p;
4069 struct extsym_info einfo;
4070
4071 /* We accumulate the debugging information counts in the symbolic
4072 header. */
4073 symhdr = &debug->symbolic_header;
4074 symhdr->vstamp = 0;
4075 symhdr->ilineMax = 0;
4076 symhdr->cbLine = 0;
4077 symhdr->idnMax = 0;
4078 symhdr->ipdMax = 0;
4079 symhdr->isymMax = 0;
4080 symhdr->ioptMax = 0;
4081 symhdr->iauxMax = 0;
4082 symhdr->issMax = 0;
4083 symhdr->issExtMax = 0;
4084 symhdr->ifdMax = 0;
4085 symhdr->crfd = 0;
4086 symhdr->iextMax = 0;
4087
4088 /* We accumulate the debugging information itself in the debug_info
4089 structure. */
4090 debug->line = NULL;
4091 debug->external_dnr = NULL;
4092 debug->external_pdr = NULL;
4093 debug->external_sym = NULL;
4094 debug->external_opt = NULL;
4095 debug->external_aux = NULL;
4096 debug->ss = NULL;
4097 debug->ssext = debug->ssext_end = NULL;
4098 debug->external_fdr = NULL;
4099 debug->external_rfd = NULL;
4100 debug->external_ext = debug->external_ext_end = NULL;
4101
4102 handle = bfd_ecoff_debug_init (abfd, debug, &backend->debug_swap, info);
4103 if (handle == (PTR) NULL)
4104 return false;
4105
4106 /* Accumulate the debugging symbols from each input BFD. */
4107 for (input_bfd = info->input_bfds;
4108 input_bfd != (bfd *) NULL;
4109 input_bfd = input_bfd->link_next)
4110 {
4111 boolean ret;
4112
4113 if (bfd_get_flavour (input_bfd) == bfd_target_ecoff_flavour)
4114 {
4115 /* Abitrarily set the symbolic header vstamp to the vstamp
4116 of the first object file in the link. */
4117 if (symhdr->vstamp == 0)
4118 symhdr->vstamp
4119 = ecoff_data (input_bfd)->debug_info.symbolic_header.vstamp;
4120 ret = ecoff_final_link_debug_accumulate (abfd, input_bfd, info,
4121 handle);
4122 }
4123 else
4124 ret = bfd_ecoff_debug_accumulate_other (handle, abfd,
4125 debug, &backend->debug_swap,
4126 input_bfd, info);
4127 if (! ret)
4128 return false;
4129
4130 /* Combine the register masks. */
4131 ecoff_data (abfd)->gprmask |= ecoff_data (input_bfd)->gprmask;
4132 ecoff_data (abfd)->fprmask |= ecoff_data (input_bfd)->fprmask;
4133 ecoff_data (abfd)->cprmask[0] |= ecoff_data (input_bfd)->cprmask[0];
4134 ecoff_data (abfd)->cprmask[1] |= ecoff_data (input_bfd)->cprmask[1];
4135 ecoff_data (abfd)->cprmask[2] |= ecoff_data (input_bfd)->cprmask[2];
4136 ecoff_data (abfd)->cprmask[3] |= ecoff_data (input_bfd)->cprmask[3];
4137 }
4138
4139 /* Write out the external symbols. */
4140 einfo.abfd = abfd;
4141 einfo.info = info;
4142 ecoff_link_hash_traverse (ecoff_hash_table (info),
4143 ecoff_link_write_external,
4144 (PTR) &einfo);
4145
4146 if (info->relocateable)
4147 {
4148 /* We need to make a pass over the link_orders to count up the
4149 number of relocations we will need to output, so that we know
4150 how much space they will take up. */
4151 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4152 {
4153 o->reloc_count = 0;
4154 for (p = o->link_order_head;
4155 p != (struct bfd_link_order *) NULL;
4156 p = p->next)
4157 if (p->type == bfd_indirect_link_order)
4158 o->reloc_count += p->u.indirect.section->reloc_count;
4159 else if (p->type == bfd_section_reloc_link_order
4160 || p->type == bfd_symbol_reloc_link_order)
4161 ++o->reloc_count;
4162 }
4163 }
4164
4165 /* Compute the reloc and symbol file positions. */
4166 ecoff_compute_reloc_file_positions (abfd);
4167
4168 /* Write out the debugging information. */
4169 if (! bfd_ecoff_write_accumulated_debug (handle, abfd, debug,
4170 &backend->debug_swap, info,
4171 ecoff_data (abfd)->sym_filepos))
4172 return false;
4173
4174 bfd_ecoff_debug_free (handle, abfd, debug, &backend->debug_swap, info);
4175
4176 if (info->relocateable)
4177 {
4178 /* Now reset the reloc_count field of the sections in the output
4179 BFD to 0, so that we can use them to keep track of how many
4180 relocs we have output thus far. */
4181 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4182 o->reloc_count = 0;
4183 }
4184
4185 /* Get a value for the GP register. */
4186 if (ecoff_data (abfd)->gp == 0)
4187 {
4188 struct bfd_link_hash_entry *h;
4189
4190 h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
4191 if (h != (struct bfd_link_hash_entry *) NULL
4192 && h->type == bfd_link_hash_defined)
4193 ecoff_data (abfd)->gp = (h->u.def.value
4194 + h->u.def.section->output_section->vma
4195 + h->u.def.section->output_offset);
4196 else if (info->relocateable)
4197 {
4198 bfd_vma lo;
4199
4200 /* Make up a value. */
4201 lo = (bfd_vma) -1;
4202 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4203 {
4204 if (o->vma < lo
4205 && (strcmp (o->name, _SBSS) == 0
4206 || strcmp (o->name, _SDATA) == 0
4207 || strcmp (o->name, _LIT4) == 0
4208 || strcmp (o->name, _LIT8) == 0
4209 || strcmp (o->name, _LITA) == 0))
4210 lo = o->vma;
4211 }
4212 ecoff_data (abfd)->gp = lo + 0x8000;
4213 }
4214 else
4215 {
4216 /* If the relocate_section function needs to do a reloc
4217 involving the GP value, it should make a reloc_dangerous
4218 callback to warn that GP is not defined. */
4219 }
4220 }
4221
4222 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
4223 {
4224 for (p = o->link_order_head;
4225 p != (struct bfd_link_order *) NULL;
4226 p = p->next)
4227 {
4228 if (p->type == bfd_indirect_link_order
4229 && (bfd_get_flavour (p->u.indirect.section->owner)
4230 == bfd_target_ecoff_flavour))
4231 {
4232 if (! ecoff_indirect_link_order (abfd, info, o, p))
4233 return false;
4234 }
4235 else if (p->type == bfd_section_reloc_link_order
4236 || p->type == bfd_symbol_reloc_link_order)
4237 {
4238 if (! ecoff_reloc_link_order (abfd, info, o, p))
4239 return false;
4240 }
4241 else
4242 {
4243 if (! _bfd_default_link_order (abfd, info, o, p))
4244 return false;
4245 }
4246 }
4247 }
4248
4249 bfd_get_symcount (abfd) = symhdr->iextMax + symhdr->isymMax;
4250
4251 ecoff_data (abfd)->linker = true;
4252
4253 return true;
4254 }
4255
4256 /* Accumulate the debugging information for an input BFD into the
4257 output BFD. This must read in the symbolic information of the
4258 input BFD. */
4259
4260 static boolean
4261 ecoff_final_link_debug_accumulate (output_bfd, input_bfd, info, handle)
4262 bfd *output_bfd;
4263 bfd *input_bfd;
4264 struct bfd_link_info *info;
4265 PTR handle;
4266 {
4267 struct ecoff_debug_info * const debug = &ecoff_data (input_bfd)->debug_info;
4268 const struct ecoff_debug_swap * const swap =
4269 &ecoff_backend (input_bfd)->debug_swap;
4270 HDRR *symhdr = &debug->symbolic_header;
4271 boolean ret;
4272
4273 #define READ(ptr, offset, count, size, type) \
4274 if (symhdr->count == 0) \
4275 debug->ptr = NULL; \
4276 else \
4277 { \
4278 bfd_size_type amt = (bfd_size_type) size * symhdr->count; \
4279 debug->ptr = (type) bfd_malloc (amt); \
4280 if (debug->ptr == NULL) \
4281 { \
4282 ret = false; \
4283 goto return_something; \
4284 } \
4285 if (bfd_seek (input_bfd, (file_ptr) symhdr->offset, SEEK_SET) != 0 \
4286 || bfd_bread (debug->ptr, amt, input_bfd) != amt) \
4287 { \
4288 ret = false; \
4289 goto return_something; \
4290 } \
4291 }
4292
4293 /* If raw_syments is not NULL, then the data was already by read by
4294 _bfd_ecoff_slurp_symbolic_info. */
4295 if (ecoff_data (input_bfd)->raw_syments == NULL)
4296 {
4297 READ (line, cbLineOffset, cbLine, sizeof (unsigned char),
4298 unsigned char *);
4299 READ (external_dnr, cbDnOffset, idnMax, swap->external_dnr_size, PTR);
4300 READ (external_pdr, cbPdOffset, ipdMax, swap->external_pdr_size, PTR);
4301 READ (external_sym, cbSymOffset, isymMax, swap->external_sym_size, PTR);
4302 READ (external_opt, cbOptOffset, ioptMax, swap->external_opt_size, PTR);
4303 READ (external_aux, cbAuxOffset, iauxMax, sizeof (union aux_ext),
4304 union aux_ext *);
4305 READ (ss, cbSsOffset, issMax, sizeof (char), char *);
4306 READ (external_fdr, cbFdOffset, ifdMax, swap->external_fdr_size, PTR);
4307 READ (external_rfd, cbRfdOffset, crfd, swap->external_rfd_size, PTR);
4308 }
4309 #undef READ
4310
4311 /* We do not read the external strings or the external symbols. */
4312
4313 ret = (bfd_ecoff_debug_accumulate
4314 (handle, output_bfd, &ecoff_data (output_bfd)->debug_info,
4315 &ecoff_backend (output_bfd)->debug_swap,
4316 input_bfd, debug, swap, info));
4317
4318 return_something:
4319 if (ecoff_data (input_bfd)->raw_syments == NULL)
4320 {
4321 if (debug->line != NULL)
4322 free (debug->line);
4323 if (debug->external_dnr != NULL)
4324 free (debug->external_dnr);
4325 if (debug->external_pdr != NULL)
4326 free (debug->external_pdr);
4327 if (debug->external_sym != NULL)
4328 free (debug->external_sym);
4329 if (debug->external_opt != NULL)
4330 free (debug->external_opt);
4331 if (debug->external_aux != NULL)
4332 free (debug->external_aux);
4333 if (debug->ss != NULL)
4334 free (debug->ss);
4335 if (debug->external_fdr != NULL)
4336 free (debug->external_fdr);
4337 if (debug->external_rfd != NULL)
4338 free (debug->external_rfd);
4339
4340 /* Make sure we don't accidentally follow one of these pointers
4341 into freed memory. */
4342 debug->line = NULL;
4343 debug->external_dnr = NULL;
4344 debug->external_pdr = NULL;
4345 debug->external_sym = NULL;
4346 debug->external_opt = NULL;
4347 debug->external_aux = NULL;
4348 debug->ss = NULL;
4349 debug->external_fdr = NULL;
4350 debug->external_rfd = NULL;
4351 }
4352
4353 return ret;
4354 }
4355
4356 /* Put out information for an external symbol. These come only from
4357 the hash table. */
4358
4359 static boolean
4360 ecoff_link_write_external (h, data)
4361 struct ecoff_link_hash_entry *h;
4362 PTR data;
4363 {
4364 struct extsym_info *einfo = (struct extsym_info *) data;
4365 bfd *output_bfd = einfo->abfd;
4366 boolean strip;
4367
4368 if (h->root.type == bfd_link_hash_warning)
4369 {
4370 h = (struct ecoff_link_hash_entry *) h->root.u.i.link;
4371 if (h->root.type == bfd_link_hash_new)
4372 return true;
4373 }
4374
4375 /* We need to check if this symbol is being stripped. */
4376 if (h->root.type == bfd_link_hash_undefined
4377 || h->root.type == bfd_link_hash_undefweak)
4378 strip = false;
4379 else if (einfo->info->strip == strip_all
4380 || (einfo->info->strip == strip_some
4381 && bfd_hash_lookup (einfo->info->keep_hash,
4382 h->root.root.string,
4383 false, false) == NULL))
4384 strip = true;
4385 else
4386 strip = false;
4387
4388 if (strip || h->written)
4389 return true;
4390
4391 if (h->abfd == (bfd *) NULL)
4392 {
4393 h->esym.jmptbl = 0;
4394 h->esym.cobol_main = 0;
4395 h->esym.weakext = 0;
4396 h->esym.reserved = 0;
4397 h->esym.ifd = ifdNil;
4398 h->esym.asym.value = 0;
4399 h->esym.asym.st = stGlobal;
4400
4401 if (h->root.type != bfd_link_hash_defined
4402 && h->root.type != bfd_link_hash_defweak)
4403 h->esym.asym.sc = scAbs;
4404 else
4405 {
4406 asection *output_section;
4407 const char *name;
4408
4409 output_section = h->root.u.def.section->output_section;
4410 name = bfd_section_name (output_section->owner, output_section);
4411
4412 if (strcmp (name, _TEXT) == 0)
4413 h->esym.asym.sc = scText;
4414 else if (strcmp (name, _DATA) == 0)
4415 h->esym.asym.sc = scData;
4416 else if (strcmp (name, _SDATA) == 0)
4417 h->esym.asym.sc = scSData;
4418 else if (strcmp (name, _RDATA) == 0)
4419 h->esym.asym.sc = scRData;
4420 else if (strcmp (name, _BSS) == 0)
4421 h->esym.asym.sc = scBss;
4422 else if (strcmp (name, _SBSS) == 0)
4423 h->esym.asym.sc = scSBss;
4424 else if (strcmp (name, _INIT) == 0)
4425 h->esym.asym.sc = scInit;
4426 else if (strcmp (name, _FINI) == 0)
4427 h->esym.asym.sc = scFini;
4428 else if (strcmp (name, _PDATA) == 0)
4429 h->esym.asym.sc = scPData;
4430 else if (strcmp (name, _XDATA) == 0)
4431 h->esym.asym.sc = scXData;
4432 else if (strcmp (name, _RCONST) == 0)
4433 h->esym.asym.sc = scRConst;
4434 else
4435 h->esym.asym.sc = scAbs;
4436 }
4437
4438 h->esym.asym.reserved = 0;
4439 h->esym.asym.index = indexNil;
4440 }
4441 else if (h->esym.ifd != -1)
4442 {
4443 struct ecoff_debug_info *debug;
4444
4445 /* Adjust the FDR index for the symbol by that used for the
4446 input BFD. */
4447 debug = &ecoff_data (h->abfd)->debug_info;
4448 BFD_ASSERT (h->esym.ifd >= 0
4449 && h->esym.ifd < debug->symbolic_header.ifdMax);
4450 h->esym.ifd = debug->ifdmap[h->esym.ifd];
4451 }
4452
4453 switch (h->root.type)
4454 {
4455 default:
4456 case bfd_link_hash_warning:
4457 case bfd_link_hash_new:
4458 abort ();
4459 case bfd_link_hash_undefined:
4460 case bfd_link_hash_undefweak:
4461 if (h->esym.asym.sc != scUndefined
4462 && h->esym.asym.sc != scSUndefined)
4463 h->esym.asym.sc = scUndefined;
4464 break;
4465 case bfd_link_hash_defined:
4466 case bfd_link_hash_defweak:
4467 if (h->esym.asym.sc == scUndefined
4468 || h->esym.asym.sc == scSUndefined)
4469 h->esym.asym.sc = scAbs;
4470 else if (h->esym.asym.sc == scCommon)
4471 h->esym.asym.sc = scBss;
4472 else if (h->esym.asym.sc == scSCommon)
4473 h->esym.asym.sc = scSBss;
4474 h->esym.asym.value = (h->root.u.def.value
4475 + h->root.u.def.section->output_section->vma
4476 + h->root.u.def.section->output_offset);
4477 break;
4478 case bfd_link_hash_common:
4479 if (h->esym.asym.sc != scCommon
4480 && h->esym.asym.sc != scSCommon)
4481 h->esym.asym.sc = scCommon;
4482 h->esym.asym.value = h->root.u.c.size;
4483 break;
4484 case bfd_link_hash_indirect:
4485 /* We ignore these symbols, since the indirected symbol is
4486 already in the hash table. */
4487 return true;
4488 }
4489
4490 /* bfd_ecoff_debug_one_external uses iextMax to keep track of the
4491 symbol number. */
4492 h->indx = ecoff_data (output_bfd)->debug_info.symbolic_header.iextMax;
4493 h->written = 1;
4494
4495 return (bfd_ecoff_debug_one_external
4496 (output_bfd, &ecoff_data (output_bfd)->debug_info,
4497 &ecoff_backend (output_bfd)->debug_swap, h->root.root.string,
4498 &h->esym));
4499 }
4500
4501 /* Relocate and write an ECOFF section into an ECOFF output file. */
4502
4503 static boolean
4504 ecoff_indirect_link_order (output_bfd, info, output_section, link_order)
4505 bfd *output_bfd;
4506 struct bfd_link_info *info;
4507 asection *output_section;
4508 struct bfd_link_order *link_order;
4509 {
4510 asection *input_section;
4511 bfd *input_bfd;
4512 struct ecoff_section_tdata *section_tdata;
4513 bfd_size_type raw_size;
4514 bfd_size_type cooked_size;
4515 bfd_byte *contents = NULL;
4516 bfd_size_type external_reloc_size;
4517 bfd_size_type external_relocs_size;
4518 PTR external_relocs = NULL;
4519 bfd_size_type amt;
4520
4521 BFD_ASSERT ((output_section->flags & SEC_HAS_CONTENTS) != 0);
4522
4523 if (link_order->size == 0)
4524 return true;
4525
4526 input_section = link_order->u.indirect.section;
4527 input_bfd = input_section->owner;
4528 section_tdata = ecoff_section_data (input_bfd, input_section);
4529
4530 raw_size = input_section->_raw_size;
4531 cooked_size = input_section->_cooked_size;
4532 if (cooked_size == 0)
4533 cooked_size = raw_size;
4534
4535 BFD_ASSERT (input_section->output_section == output_section);
4536 BFD_ASSERT (input_section->output_offset == link_order->offset);
4537 BFD_ASSERT (cooked_size == link_order->size);
4538
4539 /* Get the section contents. We allocate memory for the larger of
4540 the size before relocating and the size after relocating. */
4541 amt = raw_size >= cooked_size ? raw_size : cooked_size;
4542 contents = (bfd_byte *) bfd_malloc (amt);
4543 if (contents == NULL && amt != 0)
4544 goto error_return;
4545
4546 /* If we are relaxing, the contents may have already been read into
4547 memory, in which case we copy them into our new buffer. We don't
4548 simply reuse the old buffer in case cooked_size > raw_size. */
4549 if (section_tdata != (struct ecoff_section_tdata *) NULL
4550 && section_tdata->contents != (bfd_byte *) NULL)
4551 memcpy (contents, section_tdata->contents, (size_t) raw_size);
4552 else
4553 {
4554 if (! bfd_get_section_contents (input_bfd, input_section,
4555 (PTR) contents,
4556 (file_ptr) 0, raw_size))
4557 goto error_return;
4558 }
4559
4560 /* Get the relocs. If we are relaxing MIPS code, they will already
4561 have been read in. Otherwise, we read them in now. */
4562 external_reloc_size = ecoff_backend (input_bfd)->external_reloc_size;
4563 external_relocs_size = external_reloc_size * input_section->reloc_count;
4564
4565 if (section_tdata != (struct ecoff_section_tdata *) NULL
4566 && section_tdata->external_relocs != NULL)
4567 external_relocs = section_tdata->external_relocs;
4568 else
4569 {
4570 external_relocs = (PTR) bfd_malloc (external_relocs_size);
4571 if (external_relocs == NULL && external_relocs_size != 0)
4572 goto error_return;
4573
4574 if (bfd_seek (input_bfd, input_section->rel_filepos, SEEK_SET) != 0
4575 || (bfd_bread (external_relocs, external_relocs_size, input_bfd)
4576 != external_relocs_size))
4577 goto error_return;
4578 }
4579
4580 /* Relocate the section contents. */
4581 if (! ((*ecoff_backend (input_bfd)->relocate_section)
4582 (output_bfd, info, input_bfd, input_section, contents,
4583 external_relocs)))
4584 goto error_return;
4585
4586 /* Write out the relocated section. */
4587 if (! bfd_set_section_contents (output_bfd,
4588 output_section,
4589 (PTR) contents,
4590 (file_ptr) input_section->output_offset,
4591 cooked_size))
4592 goto error_return;
4593
4594 /* If we are producing relocateable output, the relocs were
4595 modified, and we write them out now. We use the reloc_count
4596 field of output_section to keep track of the number of relocs we
4597 have output so far. */
4598 if (info->relocateable)
4599 {
4600 file_ptr pos = (output_section->rel_filepos
4601 + output_section->reloc_count * external_reloc_size);
4602 if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
4603 || (bfd_bwrite (external_relocs, external_relocs_size, output_bfd)
4604 != external_relocs_size))
4605 goto error_return;
4606 output_section->reloc_count += input_section->reloc_count;
4607 }
4608
4609 if (contents != NULL)
4610 free (contents);
4611 if (external_relocs != NULL && section_tdata == NULL)
4612 free (external_relocs);
4613 return true;
4614
4615 error_return:
4616 if (contents != NULL)
4617 free (contents);
4618 if (external_relocs != NULL && section_tdata == NULL)
4619 free (external_relocs);
4620 return false;
4621 }
4622
4623 /* Generate a reloc when linking an ECOFF file. This is a reloc
4624 requested by the linker, and does come from any input file. This
4625 is used to build constructor and destructor tables when linking
4626 with -Ur. */
4627
4628 static boolean
4629 ecoff_reloc_link_order (output_bfd, info, output_section, link_order)
4630 bfd *output_bfd;
4631 struct bfd_link_info *info;
4632 asection *output_section;
4633 struct bfd_link_order *link_order;
4634 {
4635 enum bfd_link_order_type type;
4636 asection *section;
4637 bfd_vma addend;
4638 arelent rel;
4639 struct internal_reloc in;
4640 bfd_size_type external_reloc_size;
4641 bfd_byte *rbuf;
4642 boolean ok;
4643 file_ptr pos;
4644
4645 type = link_order->type;
4646 section = NULL;
4647 addend = link_order->u.reloc.p->addend;
4648
4649 /* We set up an arelent to pass to the backend adjust_reloc_out
4650 routine. */
4651 rel.address = link_order->offset;
4652
4653 rel.howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
4654 if (rel.howto == 0)
4655 {
4656 bfd_set_error (bfd_error_bad_value);
4657 return false;
4658 }
4659
4660 if (type == bfd_section_reloc_link_order)
4661 {
4662 section = link_order->u.reloc.p->u.section;
4663 rel.sym_ptr_ptr = section->symbol_ptr_ptr;
4664 }
4665 else
4666 {
4667 struct bfd_link_hash_entry *h;
4668
4669 /* Treat a reloc against a defined symbol as though it were
4670 actually against the section. */
4671 h = bfd_wrapped_link_hash_lookup (output_bfd, info,
4672 link_order->u.reloc.p->u.name,
4673 false, false, false);
4674 if (h != NULL
4675 && (h->type == bfd_link_hash_defined
4676 || h->type == bfd_link_hash_defweak))
4677 {
4678 type = bfd_section_reloc_link_order;
4679 section = h->u.def.section->output_section;
4680 /* It seems that we ought to add the symbol value to the
4681 addend here, but in practice it has already been added
4682 because it was passed to constructor_callback. */
4683 addend += section->vma + h->u.def.section->output_offset;
4684 }
4685 else
4686 {
4687 /* We can't set up a reloc against a symbol correctly,
4688 because we have no asymbol structure. Currently no
4689 adjust_reloc_out routine cares. */
4690 rel.sym_ptr_ptr = (asymbol **) NULL;
4691 }
4692 }
4693
4694 /* All ECOFF relocs are in-place. Put the addend into the object
4695 file. */
4696
4697 BFD_ASSERT (rel.howto->partial_inplace);
4698 if (addend != 0)
4699 {
4700 bfd_size_type size;
4701 bfd_reloc_status_type rstat;
4702 bfd_byte *buf;
4703
4704 size = bfd_get_reloc_size (rel.howto);
4705 buf = (bfd_byte *) bfd_zmalloc (size);
4706 if (buf == (bfd_byte *) NULL)
4707 return false;
4708 rstat = _bfd_relocate_contents (rel.howto, output_bfd,
4709 (bfd_vma) addend, buf);
4710 switch (rstat)
4711 {
4712 case bfd_reloc_ok:
4713 break;
4714 default:
4715 case bfd_reloc_outofrange:
4716 abort ();
4717 case bfd_reloc_overflow:
4718 if (! ((*info->callbacks->reloc_overflow)
4719 (info,
4720 (link_order->type == bfd_section_reloc_link_order
4721 ? bfd_section_name (output_bfd, section)
4722 : link_order->u.reloc.p->u.name),
4723 rel.howto->name, addend, (bfd *) NULL,
4724 (asection *) NULL, (bfd_vma) 0)))
4725 {
4726 free (buf);
4727 return false;
4728 }
4729 break;
4730 }
4731 ok = bfd_set_section_contents (output_bfd, output_section, (PTR) buf,
4732 (file_ptr) link_order->offset, size);
4733 free (buf);
4734 if (! ok)
4735 return false;
4736 }
4737
4738 rel.addend = 0;
4739
4740 /* Move the information into an internal_reloc structure. */
4741 in.r_vaddr = (rel.address
4742 + bfd_get_section_vma (output_bfd, output_section));
4743 in.r_type = rel.howto->type;
4744
4745 if (type == bfd_symbol_reloc_link_order)
4746 {
4747 struct ecoff_link_hash_entry *h;
4748
4749 h = ((struct ecoff_link_hash_entry *)
4750 bfd_wrapped_link_hash_lookup (output_bfd, info,
4751 link_order->u.reloc.p->u.name,
4752 false, false, true));
4753 if (h != (struct ecoff_link_hash_entry *) NULL
4754 && h->indx != -1)
4755 in.r_symndx = h->indx;
4756 else
4757 {
4758 if (! ((*info->callbacks->unattached_reloc)
4759 (info, link_order->u.reloc.p->u.name, (bfd *) NULL,
4760 (asection *) NULL, (bfd_vma) 0)))
4761 return false;
4762 in.r_symndx = 0;
4763 }
4764 in.r_extern = 1;
4765 }
4766 else
4767 {
4768 const char *name;
4769
4770 name = bfd_get_section_name (output_bfd, section);
4771 if (strcmp (name, ".text") == 0)
4772 in.r_symndx = RELOC_SECTION_TEXT;
4773 else if (strcmp (name, ".rdata") == 0)
4774 in.r_symndx = RELOC_SECTION_RDATA;
4775 else if (strcmp (name, ".data") == 0)
4776 in.r_symndx = RELOC_SECTION_DATA;
4777 else if (strcmp (name, ".sdata") == 0)
4778 in.r_symndx = RELOC_SECTION_SDATA;
4779 else if (strcmp (name, ".sbss") == 0)
4780 in.r_symndx = RELOC_SECTION_SBSS;
4781 else if (strcmp (name, ".bss") == 0)
4782 in.r_symndx = RELOC_SECTION_BSS;
4783 else if (strcmp (name, ".init") == 0)
4784 in.r_symndx = RELOC_SECTION_INIT;
4785 else if (strcmp (name, ".lit8") == 0)
4786 in.r_symndx = RELOC_SECTION_LIT8;
4787 else if (strcmp (name, ".lit4") == 0)
4788 in.r_symndx = RELOC_SECTION_LIT4;
4789 else if (strcmp (name, ".xdata") == 0)
4790 in.r_symndx = RELOC_SECTION_XDATA;
4791 else if (strcmp (name, ".pdata") == 0)
4792 in.r_symndx = RELOC_SECTION_PDATA;
4793 else if (strcmp (name, ".fini") == 0)
4794 in.r_symndx = RELOC_SECTION_FINI;
4795 else if (strcmp (name, ".lita") == 0)
4796 in.r_symndx = RELOC_SECTION_LITA;
4797 else if (strcmp (name, "*ABS*") == 0)
4798 in.r_symndx = RELOC_SECTION_ABS;
4799 else if (strcmp (name, ".rconst") == 0)
4800 in.r_symndx = RELOC_SECTION_RCONST;
4801 else
4802 abort ();
4803 in.r_extern = 0;
4804 }
4805
4806 /* Let the BFD backend adjust the reloc. */
4807 (*ecoff_backend (output_bfd)->adjust_reloc_out) (output_bfd, &rel, &in);
4808
4809 /* Get some memory and swap out the reloc. */
4810 external_reloc_size = ecoff_backend (output_bfd)->external_reloc_size;
4811 rbuf = (bfd_byte *) bfd_malloc (external_reloc_size);
4812 if (rbuf == (bfd_byte *) NULL)
4813 return false;
4814
4815 (*ecoff_backend (output_bfd)->swap_reloc_out) (output_bfd, &in, (PTR) rbuf);
4816
4817 pos = (output_section->rel_filepos
4818 + output_section->reloc_count * external_reloc_size);
4819 ok = (bfd_seek (output_bfd, pos, SEEK_SET) == 0
4820 && (bfd_bwrite ((PTR) rbuf, external_reloc_size, output_bfd)
4821 == external_reloc_size));
4822
4823 if (ok)
4824 ++output_section->reloc_count;
4825
4826 free (rbuf);
4827
4828 return ok;
4829 }