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