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