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