* oasys.c (oasys_write_object_contents): Lint.
[deliverable/binutils-gdb.git] / bfd / coff-mips.c
CommitLineData
1f29e30b 1/* BFD back-end for MIPS Extended-Coff files.
f6409552 2 Copyright 1990, 1991, 1992, 1993 Free Software Foundation, Inc.
8fa0d3a0 3 Original version by Per Bothner.
f6409552 4 Full support added by Ian Lance Taylor, ian@cygnus.com.
1327fb29 5
68b70212 6This file is part of BFD, the Binary File Descriptor library.
23b0b558 7
68b70212 8This program is free software; you can redistribute it and/or modify
23b0b558 9it under the terms of the GNU General Public License as published by
68b70212
JG
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
23b0b558 12
68b70212 13This program is distributed in the hope that it will be useful,
23b0b558
JG
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
68b70212
JG
19along with this program; if not, write to the Free Software
20Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
1327fb29 21
23b0b558 22#include "bfd.h"
dd4646ca 23#include "sysdep.h"
1327fb29 24#include "libbfd.h"
8fa0d3a0 25#include "seclet.h"
c3fe0c41
ILT
26#include "aout/ar.h"
27#include "aout/ranlib.h"
294eaca4
SC
28#include "coff/mips.h"
29#include "coff/internal.h"
515c4292
ILT
30#include "coff/sym.h"
31#include "coff/symconst.h"
32#include "coff/ecoff-ext.h"
33#include "libcoff.h"
34
35/* `Tdata' information kept for ECOFF files. */
36
37#define ecoff_data(abfd) ((abfd)->tdata.ecoff_obj_data)
38
39typedef struct ecoff_tdata
40{
8fa0d3a0
ILT
41 /* The reloc file position, set by
42 ecoff_compute_section_file_positions. */
43 file_ptr reloc_filepos;
44
45 /* The symbol table file position, set by ecoff_mkobject_hook. */
515c4292
ILT
46 file_ptr sym_filepos;
47
6d18a99d
ILT
48 /* The start and end of the text segment. Only valid for an
49 existing file, not for one we are creating. */
50 unsigned long text_start;
51 unsigned long text_end;
52
b6bef862
ILT
53 /* The cached gp value. This is used when relocating. */
54 bfd_vma gp;
55
c3fe0c41
ILT
56 /* The register masks. When linking, all the masks found in the
57 input files are combined into the masks of the output file. */
58 unsigned long gprmask;
59 unsigned long cprmask[4];
60
8fa0d3a0
ILT
61 /* The size of the unswapped ECOFF symbolic information. */
62 bfd_size_type raw_size;
63
515c4292
ILT
64 /* The unswapped ECOFF symbolic information. */
65 PTR raw_syments;
66
67 /* The swapped ECOFF symbolic header. */
68 HDRR symbolic_header;
69
70 /* Pointers to the unswapped symbolic information. */
71 unsigned char *line;
72 struct dnr_ext *external_dnr;
73 struct pdr_ext *external_pdr;
74 struct sym_ext *external_sym;
75 struct opt_ext *external_opt;
76 union aux_ext *external_aux;
77 char *ss;
78 char *ssext;
79 struct fdr_ext *external_fdr;
80 struct rfd_ext *external_rfd;
81 struct ext_ext *external_ext;
82
c3fe0c41 83 /* The swapped FDR information. */
515c4292
ILT
84 FDR *fdr;
85
c3fe0c41
ILT
86 /* The FDR index. This is set for an input BFD to a link so that
87 the external symbols can set their FDR index correctly. */
88 unsigned int ifdbase;
89
515c4292
ILT
90 /* The canonical BFD symbols. */
91 struct ecoff_symbol_struct *canonical_symbols;
92
93} ecoff_data_type;
94
95/* Each canonical asymbol really looks like this. */
96
97typedef struct ecoff_symbol_struct
98{
99 /* The actual symbol which the rest of BFD works with */
100 asymbol symbol;
101
102 /* The fdr for this symbol. */
103 FDR *fdr;
104
105 /* true if this is a local symbol rather than an external one. */
106 boolean local;
107
108 /* A pointer to the unswapped hidden information for this symbol */
109 union
110 {
111 struct sym_ext *lnative;
112 struct ext_ext *enative;
113 }
114 native;
115} ecoff_symbol_type;
116
117/* We take the address of the first element of a asymbol to ensure that the
118 macro is only ever applied to an asymbol. */
119#define ecoffsymbol(asymbol) ((ecoff_symbol_type *) (&((asymbol)->the_bfd)))
120
c3fe0c41
ILT
121/* This is a hack borrowed from coffcode.h; we need to save the index
122 of a symbol when we write it out so that can set the symbol index
123 correctly when we write out the relocs. */
124#define ecoff_get_sym_index(symbol) ((unsigned long) (symbol)->udata)
125#define ecoff_set_sym_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
126
127/* The page boundary used to align sections in the executable file. */
128#define PAGE_SIZE 0x2000
129
5e462ed9
ILT
130/* The linker needs a section to hold small common variables while
131 linking. There is no convenient way to create it when the linker
132 needs it, so we always create one for each BFD. We then avoid
133 writing it out. */
134#define SCOMMON ".scommon"
135
515c4292
ILT
136/* MIPS ECOFF has COFF sections, but the debugging information is
137 stored in a completely different format. This files uses the some
138 of the swapping routines from coffswap.h, and some of the generic
139 COFF routines in coffgen.c, but, unlike the real COFF targets, does
140 not use coffcode.h itself. */
f6409552
ILT
141\f
142/* Prototypes for static functions. */
143
144static boolean ecoff_bad_format_hook PARAMS ((bfd *abfd, PTR filehdr));
145static asection *ecoff_make_section_hook PARAMS ((bfd *abfd, char *name));
146static boolean ecoff_new_section_hook PARAMS ((bfd *abfd, asection *section));
147static boolean ecoff_mkobject PARAMS ((bfd *abfd));
148static PTR ecoff_mkobject_hook PARAMS ((bfd *abfd, PTR filehdr, PTR aouthdr));
149static boolean ecoff_set_arch_mach_hook PARAMS ((bfd *abfd, PTR filehdr));
150static long ecoff_sec_to_styp_flags PARAMS ((CONST char *name,
151 flagword flags));
152static flagword ecoff_styp_to_sec_flags PARAMS ((bfd *abfd, PTR hdr));
153static boolean ecoff_slurp_symbolic_info PARAMS ((bfd *abfd));
154static asymbol *ecoff_make_empty_symbol PARAMS ((bfd *abfd));
155static void ecoff_set_symbol_info PARAMS ((bfd *abfd, SYMR *ecoff_sym,
156 asymbol *asym, int ext));
157static boolean ecoff_slurp_symbol_table PARAMS ((bfd *abfd));
158static unsigned int ecoff_get_symtab_upper_bound PARAMS ((bfd *abfd));
159static unsigned int ecoff_get_symtab PARAMS ((bfd *abfd,
160 asymbol **alocation));
161static void ecoff_emit_aggregate PARAMS ((bfd *abfd, char *string,
162 RNDXR *rndx, long isym,
163 CONST char *which));
164static char *ecoff_type_to_string PARAMS ((bfd *abfd, union aux_ext *aux_ptr,
165 int indx, int bigendian));
166static void ecoff_print_symbol PARAMS ((bfd *abfd, PTR filep,
167 asymbol *symbol,
168 bfd_print_symbol_type how));
169static void ecoff_swap_reloc_in PARAMS ((bfd *abfd, RELOC *ext,
170 struct internal_reloc *intern));
171static unsigned int ecoff_swap_reloc_out PARAMS ((bfd *abfd, PTR src,
172 PTR dst));
173static bfd_reloc_status_type ecoff_generic_reloc PARAMS ((bfd *abfd,
174 arelent *reloc,
175 asymbol *symbol,
176 PTR data,
177 asection *section,
178 bfd *output_bfd));
179static bfd_reloc_status_type ecoff_refhi_reloc PARAMS ((bfd *abfd,
180 arelent *reloc,
181 asymbol *symbol,
182 PTR data,
183 asection *section,
184 bfd *output_bfd));
185static bfd_reloc_status_type ecoff_gprel_reloc PARAMS ((bfd *abfd,
186 arelent *reloc,
187 asymbol *symbol,
188 PTR data,
189 asection *section,
190 bfd *output_bfd));
191static boolean ecoff_slurp_reloc_table PARAMS ((bfd *abfd, asection *section,
192 asymbol **symbols));
193static unsigned int ecoff_canonicalize_reloc PARAMS ((bfd *abfd,
194 asection *section,
195 arelent **relptr,
196 asymbol **symbols));
197static boolean ecoff_find_nearest_line PARAMS ((bfd *abfd,
198 asection *section,
199 asymbol **symbols,
200 bfd_vma offset,
201 CONST char **filename_ptr,
202 CONST char **fnname_ptr,
203 unsigned int *retline_ptr));
204static void ecoff_clear_output_flags PARAMS ((bfd *abfd));
205static boolean ecoff_rel PARAMS ((bfd *output_bfd, bfd_seclet_type *seclet,
206 asection *output_section, PTR data,
207 boolean relocateable));
208static boolean ecoff_dump_seclet PARAMS ((bfd *abfd, bfd_seclet_type *seclet,
209 asection *section, PTR data,
210 boolean relocateable));
211static long ecoff_add_string PARAMS ((bfd *output_bfd, FDR *fdr,
212 CONST char *string, boolean external));
213static boolean ecoff_get_debug PARAMS ((bfd *output_bfd,
214 bfd_seclet_type *seclet,
215 asection *section,
216 boolean relocateable));
217static boolean ecoff_bfd_seclet_link PARAMS ((bfd *abfd, PTR data,
218 boolean relocateable));
219static boolean ecoff_set_arch_mach PARAMS ((bfd *abfd,
220 enum bfd_architecture arch,
221 unsigned long machine));
222static int ecoff_sizeof_headers PARAMS ((bfd *abfd, boolean reloc));
223static void ecoff_compute_section_file_positions PARAMS ((bfd *abfd));
224static boolean ecoff_set_section_contents PARAMS ((bfd *abfd,
225 asection *section,
226 PTR location,
227 file_ptr offset,
228 bfd_size_type count));
229static boolean ecoff_write_object_contents PARAMS ((bfd *abfd));
230static unsigned int ecoff_armap_hash PARAMS ((CONST char *s,
231 unsigned int *rehash,
232 unsigned int size,
233 unsigned int hlog));
234static boolean ecoff_slurp_armap PARAMS ((bfd *abfd));
235static boolean ecoff_write_armap PARAMS ((bfd *abfd, unsigned int elength,
236 struct orl *map,
237 unsigned int orl_count,
238 int stridx));
239static bfd_target *ecoff_archive_p PARAMS ((bfd *abfd));
240\f
8fa0d3a0
ILT
241/* Get the generic COFF swapping routines, except for the reloc,
242 symbol, and lineno ones. Give them ecoff names. */
c3fe0c41 243#define MIPSECOFF
8fa0d3a0 244#define NO_COFF_RELOCS
515c4292
ILT
245#define NO_COFF_SYMBOLS
246#define NO_COFF_LINENOS
515c4292
ILT
247#define coff_swap_filehdr_in ecoff_swap_filehdr_in
248#define coff_swap_filehdr_out ecoff_swap_filehdr_out
249#define coff_swap_aouthdr_in ecoff_swap_aouthdr_in
250#define coff_swap_aouthdr_out ecoff_swap_aouthdr_out
251#define coff_swap_scnhdr_in ecoff_swap_scnhdr_in
252#define coff_swap_scnhdr_out ecoff_swap_scnhdr_out
253#include "coffswap.h"
254\f
255/* This stuff is somewhat copied from coffcode.h. */
256
257static asection bfd_debug_section = { "*DEBUG*" };
258
259/* See whether the magic number matches. */
260
261static boolean
f6409552
ILT
262ecoff_bad_format_hook (abfd, filehdr)
263 bfd *abfd;
264 PTR filehdr;
515c4292
ILT
265{
266 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
267
268 if (ECOFFBADMAG (*internal_f))
269 return false;
270
271 return true;
272}
273
274/* This is a hook needed by SCO COFF, but we have nothing to do. */
275
276static asection *
f6409552
ILT
277ecoff_make_section_hook (abfd, name)
278 bfd *abfd;
279 char *name;
515c4292
ILT
280{
281 return (asection *) NULL;
282}
283
284/* Initialize a new section. */
285
286static boolean
f6409552
ILT
287ecoff_new_section_hook (abfd, section)
288 bfd *abfd;
289 asection *section;
515c4292
ILT
290{
291 section->alignment_power = abfd->xvec->align_power_min;
8fa0d3a0
ILT
292
293 if (strcmp (section->name, _TEXT) == 0)
294 section->flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
295 else if (strcmp (section->name, _DATA) == 0
296 || strcmp (section->name, _SDATA) == 0)
297 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
298 else if (strcmp (section->name, _RDATA) == 0
299 || strcmp (section->name, _LIT8) == 0
300 || strcmp (section->name, _LIT4) == 0)
301 section->flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
302 else if (strcmp (section->name, _BSS) == 0
303 || strcmp (section->name, _SBSS) == 0)
bf4b84bc 304 section->flags |= SEC_ALLOC;
8fa0d3a0
ILT
305
306 /* Probably any other section name is SEC_NEVER_LOAD, but I'm
307 uncertain about .init on some systems and I don't know how shared
308 libraries work. */
309
515c4292
ILT
310 return true;
311}
312
f6409552
ILT
313/* Set the alignment of a section; we have nothing to do. */
314
515c4292
ILT
315#define ecoff_set_alignment_hook \
316 ((void (*) PARAMS ((bfd *, asection *, PTR))) bfd_void)
317
f6409552
ILT
318/* Create an ECOFF object. */
319
515c4292 320static boolean
f6409552
ILT
321ecoff_mkobject (abfd)
322 bfd *abfd;
515c4292
ILT
323{
324 abfd->tdata.ecoff_obj_data = ((struct ecoff_tdata *)
325 bfd_zalloc (abfd, sizeof(ecoff_data_type)));
326 if (abfd->tdata.ecoff_obj_data == NULL)
327 {
328 bfd_error = no_memory;
329 return false;
330 }
331
5e462ed9
ILT
332 /* Always create a .scommon section for every BFD. This is a hack so
333 that the linker has something to attach scSCommon symbols to. */
334 bfd_make_section (abfd, SCOMMON);
335
515c4292
ILT
336 return true;
337}
338
f6409552 339/* Create the ECOFF backend specific information. */
515c4292 340
c3fe0c41
ILT
341static PTR
342ecoff_mkobject_hook (abfd, filehdr, aouthdr)
343 bfd *abfd;
344 PTR filehdr;
345 PTR aouthdr;
515c4292
ILT
346{
347 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
c3fe0c41 348 struct internal_aouthdr *internal_a = (struct internal_aouthdr *) aouthdr;
515c4292
ILT
349 ecoff_data_type *ecoff;
350
351 if (ecoff_mkobject (abfd) == false)
352 return NULL;
353
354 ecoff = ecoff_data (abfd);
355 ecoff->sym_filepos = internal_f->f_symptr;
c3fe0c41
ILT
356
357 if (internal_a != (struct internal_aouthdr *) NULL)
358 {
359 int i;
360
6d18a99d
ILT
361 ecoff->text_start = internal_a->text_start;
362 ecoff->text_end = internal_a->text_start + internal_a->tsize;
c3fe0c41
ILT
363 ecoff->gp = internal_a->gp_value;
364 ecoff->gprmask = internal_a->gprmask;
5e462ed9 365 for (i = 0; i < 4; i++)
c3fe0c41
ILT
366 ecoff->cprmask[i] = internal_a->cprmask[i];
367 }
368
515c4292
ILT
369 return (PTR) ecoff;
370}
371
372/* Determine the machine architecture and type. */
f6409552 373
515c4292 374static boolean
f6409552
ILT
375ecoff_set_arch_mach_hook (abfd, filehdr)
376 bfd *abfd;
377 PTR filehdr;
515c4292 378{
515c4292 379 struct internal_filehdr *internal_f = (struct internal_filehdr *) filehdr;
f6409552 380 enum bfd_architecture arch;
515c4292 381
f6409552
ILT
382 switch (internal_f->f_magic)
383 {
384 case MIPS_MAGIC_1:
6d18a99d
ILT
385 case MIPS_MAGIC_LITTLE:
386 case MIPS_MAGIC_BIG:
f6409552
ILT
387 arch = bfd_arch_mips;
388 break;
389
390 default:
391 arch = bfd_arch_obscure;
392 break;
393 }
394
395 bfd_default_set_arch_mach (abfd, arch, (unsigned long) 0);
515c4292 396
515c4292
ILT
397 return true;
398}
399
8fa0d3a0
ILT
400/* Get the section s_flags to use for a section. */
401
402static long
f6409552
ILT
403ecoff_sec_to_styp_flags (name, flags)
404 CONST char *name;
405 flagword flags;
8fa0d3a0
ILT
406{
407 long styp;
408
409 styp = 0;
410
411 if (strcmp (name, _TEXT) == 0)
412 styp = STYP_TEXT;
413 else if (strcmp (name, _DATA) == 0)
c3fe0c41 414 styp = STYP_DATA;
8fa0d3a0 415 else if (strcmp (name, _SDATA) == 0)
c3fe0c41 416 styp = STYP_SDATA;
8fa0d3a0
ILT
417 else if (strcmp (name, _RDATA) == 0)
418 styp = STYP_RDATA;
419 else if (strcmp (name, _LIT8) == 0)
420 styp = STYP_LIT8;
421 else if (strcmp (name, _LIT4) == 0)
422 styp = STYP_LIT4;
423 else if (strcmp (name, _BSS) == 0)
424 styp = STYP_BSS;
425 else if (strcmp (name, _SBSS) == 0)
426 styp = STYP_SBSS;
427 else if (flags & SEC_CODE)
428 styp = STYP_TEXT;
429 else if (flags & SEC_DATA)
430 styp = STYP_DATA;
431 else if (flags & SEC_READONLY)
432 styp = STYP_RDATA;
433 else if (flags & SEC_LOAD)
434 styp = STYP_TEXT;
435 else
436 styp = STYP_BSS;
437
438 if (flags & SEC_NEVER_LOAD)
439 styp |= STYP_NOLOAD;
440
441 return styp;
442}
443
515c4292
ILT
444/* Get the BFD flags to use for a section. */
445
446static flagword
f6409552
ILT
447ecoff_styp_to_sec_flags (abfd, hdr)
448 bfd *abfd;
449 PTR hdr;
515c4292
ILT
450{
451 struct internal_scnhdr *internal_s = (struct internal_scnhdr *) hdr;
452 long styp_flags = internal_s->s_flags;
453 flagword sec_flags=0;
454
455 if (styp_flags & STYP_NOLOAD)
456 sec_flags |= SEC_NEVER_LOAD;
457
458 /* For 386 COFF, at least, an unloadable text or data section is
459 actually a shared library section. */
460 if (styp_flags & STYP_TEXT)
461 {
462 if (sec_flags & SEC_NEVER_LOAD)
463 sec_flags |= SEC_CODE | SEC_SHARED_LIBRARY;
464 else
465 sec_flags |= SEC_CODE | SEC_LOAD | SEC_ALLOC;
466 }
467 else if ((styp_flags & STYP_DATA)
468 || (styp_flags & STYP_RDATA)
469 || (styp_flags & STYP_SDATA))
470 {
471 if (sec_flags & SEC_NEVER_LOAD)
472 sec_flags |= SEC_DATA | SEC_SHARED_LIBRARY;
473 else
474 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC;
475 if (styp_flags & STYP_RDATA)
476 sec_flags |= SEC_READONLY;
477 }
478 else if ((styp_flags & STYP_BSS)
479 || (styp_flags & STYP_SBSS))
480 {
481 sec_flags |= SEC_ALLOC;
482 }
483 else if (styp_flags & STYP_INFO)
484 {
485 sec_flags |= SEC_NEVER_LOAD;
486 }
487 else if ((styp_flags & STYP_LIT8)
488 || (styp_flags & STYP_LIT4))
489 {
490 sec_flags |= SEC_DATA | SEC_LOAD | SEC_ALLOC | SEC_READONLY;
491 }
492 else
493 {
494 sec_flags |= SEC_ALLOC | SEC_LOAD;
495 }
496
497 return sec_flags;
498}
499\f
8fa0d3a0
ILT
500/* Read in and swap the important symbolic information for an ECOFF
501 object file. */
502
503static boolean
f6409552
ILT
504ecoff_slurp_symbolic_info (abfd)
505 bfd *abfd;
8fa0d3a0
ILT
506{
507 struct hdr_ext external_symhdr;
508 HDRR *internal_symhdr;
509 bfd_size_type raw_base;
510 bfd_size_type raw_size;
511 PTR raw;
512 struct fdr_ext *fraw_src;
513 struct fdr_ext *fraw_end;
514 struct fdr *fdr_ptr;
515
516 /* Check whether we've already gotten it, and whether there's any to
517 get. */
518 if (ecoff_data (abfd)->raw_syments != (PTR) NULL)
519 return true;
520 if (ecoff_data (abfd)->sym_filepos == 0)
521 {
522 bfd_get_symcount (abfd) = 0;
523 return true;
524 }
525
526 /* At this point bfd_get_symcount (abfd) holds the number of symbols
527 as read from the file header, but on ECOFF this is always the
528 size of the symbolic information header. It would be cleaner to
529 handle this when we first read the file in coffgen.c. */
530 if (bfd_get_symcount (abfd) != sizeof (external_symhdr))
531 {
532 bfd_error = bad_value;
533 return false;
534 }
535
536 /* Read the symbolic information header. */
537 if (bfd_seek (abfd, ecoff_data (abfd)->sym_filepos, SEEK_SET) == -1
538 || (bfd_read ((PTR) &external_symhdr, sizeof (external_symhdr), 1, abfd)
539 != sizeof (external_symhdr)))
540 {
541 bfd_error = system_call_error;
542 return false;
543 }
544 internal_symhdr = &ecoff_data (abfd)->symbolic_header;
545 ecoff_swap_hdr_in (abfd, &external_symhdr, internal_symhdr);
546
547 if (internal_symhdr->magic != magicSym)
548 {
549 bfd_error = bad_value;
550 return false;
551 }
552
553 /* Now we can get the correct number of symbols. */
554 bfd_get_symcount (abfd) = (internal_symhdr->isymMax
555 + internal_symhdr->iextMax);
556
c3fe0c41 557 /* Read all the symbolic information at once. */
23ba15b7
ILT
558 raw_base = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
559
560 if (internal_symhdr->cbExtOffset != 0)
561 raw_size = (internal_symhdr->cbExtOffset
562 - raw_base
563 + internal_symhdr->iextMax * sizeof (struct ext_ext));
564 else
565 {
566 long cbline, issmax, issextmax;
567
6d18a99d
ILT
568 cbline = (internal_symhdr->cbLine + 3) &~ 3;
569 issmax = (internal_symhdr->issMax + 3) &~ 3;
570 issextmax = (internal_symhdr->issExtMax + 3) &~ 3;
23ba15b7
ILT
571 raw_size = (cbline * sizeof (unsigned char)
572 + internal_symhdr->idnMax * sizeof (struct dnr_ext)
573 + internal_symhdr->ipdMax * sizeof (struct pdr_ext)
574 + internal_symhdr->isymMax * sizeof (struct sym_ext)
575 + internal_symhdr->ioptMax * sizeof (struct opt_ext)
576 + internal_symhdr->iauxMax * sizeof (union aux_ext)
577 + issmax * sizeof (char)
578 + issextmax * sizeof (char)
579 + internal_symhdr->ifdMax * sizeof (struct fdr_ext)
580 + internal_symhdr->crfd * sizeof (struct rfd_ext)
581 + internal_symhdr->iextMax * sizeof (struct ext_ext));
582 }
583
c3fe0c41
ILT
584 if (raw_size == 0)
585 {
586 ecoff_data (abfd)->sym_filepos = 0;
587 return true;
588 }
8fa0d3a0
ILT
589 raw = (PTR) bfd_alloc (abfd, raw_size);
590 if (raw == NULL)
591 {
592 bfd_error = no_memory;
593 return false;
594 }
595 if (bfd_read (raw, raw_size, 1, abfd) != raw_size)
596 {
597 bfd_error = system_call_error;
598 bfd_release (abfd, raw);
599 return false;
600 }
601
602 ecoff_data (abfd)->raw_size = raw_size;
603 ecoff_data (abfd)->raw_syments = raw;
604
605 /* Get pointers for the numeric offsets in the HDRR structure. */
606#define FIX(off1, off2, type) \
607 if (internal_symhdr->off1 == 0) \
608 ecoff_data (abfd)->off2 = (type *) NULL; \
609 else \
610 ecoff_data (abfd)->off2 = (type *) ((char *) raw \
611 + internal_symhdr->off1 \
612 - raw_base)
613 FIX (cbLineOffset, line, unsigned char);
614 FIX (cbDnOffset, external_dnr, struct dnr_ext);
615 FIX (cbPdOffset, external_pdr, struct pdr_ext);
616 FIX (cbSymOffset, external_sym, struct sym_ext);
617 FIX (cbOptOffset, external_opt, struct opt_ext);
618 FIX (cbAuxOffset, external_aux, union aux_ext);
619 FIX (cbSsOffset, ss, char);
620 FIX (cbSsExtOffset, ssext, char);
621 FIX (cbFdOffset, external_fdr, struct fdr_ext);
622 FIX (cbRfdOffset, external_rfd, struct rfd_ext);
623 FIX (cbExtOffset, external_ext, struct ext_ext);
624#undef FIX
625
626 /* I don't want to always swap all the data, because it will just
627 waste time and most programs will never look at it. The only
628 time the linker needs most of the debugging information swapped
629 is when linking big-endian and little-endian MIPS object files
630 together, which is not a common occurrence.
631
632 We need to look at the fdr to deal with a lot of information in
633 the symbols, so we swap them here. */
634 ecoff_data (abfd)->fdr = (struct fdr *) bfd_alloc (abfd,
635 (internal_symhdr->ifdMax *
636 sizeof (struct fdr)));
637 if (ecoff_data (abfd)->fdr == NULL)
638 {
639 bfd_error = no_memory;
640 return false;
641 }
642 fdr_ptr = ecoff_data (abfd)->fdr;
643 fraw_src = ecoff_data (abfd)->external_fdr;
644 fraw_end = fraw_src + internal_symhdr->ifdMax;
645 for (; fraw_src < fraw_end; fraw_src++, fdr_ptr++)
646 ecoff_swap_fdr_in (abfd, fraw_src, fdr_ptr);
647
648 return true;
649}
650\f
515c4292
ILT
651/* ECOFF symbol table routines. The ECOFF symbol table is described
652 in gcc/mips-tfile.c. */
653
bf4b84bc
ILT
654/* ECOFF uses two common sections. One is the usual one, and the
655 other is for small objects. All the small objects are kept
656 together, and then referenced via the gp pointer, which yields
657 faster assembler code. This is what we use for the small common
658 section. */
659static asection ecoff_scom_section;
660static asymbol ecoff_scom_symbol;
661static asymbol *ecoff_scom_symbol_ptr;
662
515c4292
ILT
663/* Create an empty symbol. */
664
665static asymbol *
f6409552
ILT
666ecoff_make_empty_symbol (abfd)
667 bfd *abfd;
515c4292
ILT
668{
669 ecoff_symbol_type *new;
670
671 new = (ecoff_symbol_type *) bfd_alloc (abfd, sizeof (ecoff_symbol_type));
672 if (new == (ecoff_symbol_type *) NULL)
673 {
674 bfd_error = no_memory;
675 return (asymbol *) NULL;
676 }
677 new->symbol.section = (asection *) NULL;
678 new->fdr = (FDR *) NULL;
679 new->local = false;
680 new->native.lnative = (struct sym_ext *) NULL;
681 new->symbol.the_bfd = abfd;
682 return &new->symbol;
683}
684
685/* Set the BFD flags and section for an ECOFF symbol. */
686
687static void
f6409552
ILT
688ecoff_set_symbol_info (abfd, ecoff_sym, asym, ext)
689 bfd *abfd;
690 SYMR *ecoff_sym;
691 asymbol *asym;
692 int ext;
515c4292
ILT
693{
694 asym->the_bfd = abfd;
695 asym->value = ecoff_sym->value;
696 asym->section = &bfd_debug_section;
697 asym->udata = NULL;
c3fe0c41
ILT
698
699 /* Most symbol types are just for debugging. */
700 switch (ecoff_sym->st)
701 {
702 case stGlobal:
703 case stStatic:
704 case stLabel:
705 case stProc:
706 case stStaticProc:
707 case stBlock:
23ba15b7 708 case stNil:
c3fe0c41
ILT
709 break;
710 default:
711 asym->flags = BSF_DEBUGGING;
712 return;
713 }
714
515c4292
ILT
715 if (ext)
716 asym->flags = BSF_EXPORT | BSF_GLOBAL;
717 else
718 asym->flags = BSF_LOCAL;
719 switch (ecoff_sym->sc)
720 {
721 case scNil:
23ba15b7
ILT
722 /* Used for compiler generated labels. Leave them in the
723 debugging section, and mark them as local. If BSF_DEBUGGING
724 is set, then nm does not display them for some reason. If no
725 flags are set then the linker whines about them. */
726 asym->flags = BSF_LOCAL;
515c4292
ILT
727 break;
728 case scText:
729 asym->section = bfd_make_section_old_way (abfd, ".text");
730 asym->value -= asym->section->vma;
731 break;
732 case scData:
733 asym->section = bfd_make_section_old_way (abfd, ".data");
734 asym->value -= asym->section->vma;
735 break;
736 case scBss:
c3fe0c41 737 if (ext)
23ba15b7
ILT
738 {
739 asym->section = &bfd_com_section;
740 asym->flags = 0;
741 }
c3fe0c41
ILT
742 else
743 {
744 asym->section = bfd_make_section_old_way (abfd, ".bss");
745 asym->value -= asym->section->vma;
746 }
515c4292
ILT
747 break;
748 case scRegister:
749 asym->flags = BSF_DEBUGGING;
750 break;
751 case scAbs:
752 asym->section = &bfd_abs_section;
515c4292
ILT
753 break;
754 case scUndefined:
755 asym->section = &bfd_und_section;
23ba15b7
ILT
756 asym->flags = 0;
757 asym->value = 0;
515c4292
ILT
758 break;
759 case scCdbLocal:
760 case scBits:
761 case scCdbSystem:
762 case scRegImage:
763 case scInfo:
764 case scUserStruct:
765 asym->flags = BSF_DEBUGGING;
766 break;
767 case scSData:
768 asym->section = bfd_make_section_old_way (abfd, ".sdata");
769 asym->value -= asym->section->vma;
770 break;
771 case scSBss:
382f2a3d 772 asym->section = bfd_make_section_old_way (abfd, ".sbss");
bf4b84bc
ILT
773 if (! ext)
774 asym->value -= asym->section->vma;
515c4292
ILT
775 break;
776 case scRData:
777 asym->section = bfd_make_section_old_way (abfd, ".rdata");
778 asym->value -= asym->section->vma;
779 break;
780 case scVar:
781 asym->flags = BSF_DEBUGGING;
782 break;
783 case scCommon:
5e462ed9
ILT
784 /* FIXME: We should take a -G argument, which gives the maximum
785 size of objects to be put in the small common section. Until
786 we do, we put objects of sizes up to 8 in the small common
787 section. The assembler should do this for us, but the native
788 assembler seems to get confused. */
789 if (asym->value > 8)
790 {
791 asym->section = &bfd_com_section;
792 break;
793 }
794 /* Fall through. */
382f2a3d 795 case scSCommon:
bf4b84bc
ILT
796 if (ecoff_scom_section.name == NULL)
797 {
798 /* Initialize the small common section. */
5e462ed9 799 ecoff_scom_section.name = SCOMMON;
bf4b84bc
ILT
800 ecoff_scom_section.flags = SEC_IS_COMMON;
801 ecoff_scom_section.output_section = &ecoff_scom_section;
802 ecoff_scom_section.symbol = &ecoff_scom_symbol;
803 ecoff_scom_section.symbol_ptr_ptr = &ecoff_scom_symbol_ptr;
5e462ed9 804 ecoff_scom_symbol.name = SCOMMON;
bf4b84bc
ILT
805 ecoff_scom_symbol.flags = BSF_SECTION_SYM;
806 ecoff_scom_symbol.section = &ecoff_scom_section;
807 ecoff_scom_symbol_ptr = &ecoff_scom_symbol;
808 }
809 asym->section = &ecoff_scom_section;
23ba15b7 810 asym->flags = 0;
382f2a3d 811 break;
515c4292
ILT
812 case scVarRegister:
813 case scVariant:
814 asym->flags = BSF_DEBUGGING;
815 break;
816 case scSUndefined:
817 asym->section = &bfd_und_section;
23ba15b7
ILT
818 asym->flags = 0;
819 asym->value = 0;
515c4292
ILT
820 break;
821 case scInit:
822 asym->section = bfd_make_section_old_way (abfd, ".init");
823 asym->value -= asym->section->vma;
824 break;
825 case scBasedVar:
826 case scXData:
827 case scPData:
828 asym->flags = BSF_DEBUGGING;
829 break;
830 case scFini:
831 asym->section = bfd_make_section_old_way (abfd, ".fini");
832 asym->value -= asym->section->vma;
833 break;
834 default:
515c4292
ILT
835 break;
836 }
837}
838
839/* Read an ECOFF symbol table. */
840
841static boolean
f6409552
ILT
842ecoff_slurp_symbol_table (abfd)
843 bfd *abfd;
515c4292 844{
515c4292 845 bfd_size_type internal_size;
515c4292
ILT
846 ecoff_symbol_type *internal;
847 ecoff_symbol_type *internal_ptr;
848 struct ext_ext *eraw_src;
849 struct ext_ext *eraw_end;
8fa0d3a0
ILT
850 FDR *fdr_ptr;
851 FDR *fdr_end;
515c4292
ILT
852
853 /* If we've already read in the symbol table, do nothing. */
854 if (ecoff_data (abfd)->canonical_symbols != NULL)
855 return true;
856
8fa0d3a0
ILT
857 /* Get the symbolic information. */
858 if (ecoff_slurp_symbolic_info (abfd) == false)
859 return false;
860 if (bfd_get_symcount (abfd) == 0)
861 return true;
515c4292
ILT
862
863 internal_size = bfd_get_symcount (abfd) * sizeof (ecoff_symbol_type);
864 internal = (ecoff_symbol_type *) bfd_alloc (abfd, internal_size);
865 if (internal == NULL)
866 {
867 bfd_error = no_memory;
868 return false;
869 }
870
871 internal_ptr = internal;
872 eraw_src = ecoff_data (abfd)->external_ext;
8fa0d3a0 873 eraw_end = eraw_src + ecoff_data (abfd)->symbolic_header.iextMax;
515c4292
ILT
874 for (; eraw_src < eraw_end; eraw_src++, internal_ptr++)
875 {
876 EXTR internal_esym;
877
878 ecoff_swap_ext_in (abfd, eraw_src, &internal_esym);
879 internal_ptr->symbol.name = (ecoff_data (abfd)->ssext
880 + internal_esym.asym.iss);
881 ecoff_set_symbol_info (abfd, &internal_esym.asym,
882 &internal_ptr->symbol, 1);
883 internal_ptr->fdr = ecoff_data (abfd)->fdr + internal_esym.ifd;
884 internal_ptr->local = false;
885 internal_ptr->native.enative = eraw_src;
886 }
887
888 /* The local symbols must be accessed via the fdr's, because the
889 string and aux indices are relative to the fdr information. */
890 fdr_ptr = ecoff_data (abfd)->fdr;
8fa0d3a0 891 fdr_end = fdr_ptr + ecoff_data (abfd)->symbolic_header.ifdMax;
515c4292
ILT
892 for (; fdr_ptr < fdr_end; fdr_ptr++)
893 {
894 struct sym_ext *lraw_src;
895 struct sym_ext *lraw_end;
896
897 lraw_src = ecoff_data (abfd)->external_sym + fdr_ptr->isymBase;
898 lraw_end = lraw_src + fdr_ptr->csym;
899 for (; lraw_src < lraw_end; lraw_src++, internal_ptr++)
900 {
901 SYMR internal_sym;
902
903 ecoff_swap_sym_in (abfd, lraw_src, &internal_sym);
904 internal_ptr->symbol.name = (ecoff_data (abfd)->ss
905 + fdr_ptr->issBase
906 + internal_sym.iss);
907 ecoff_set_symbol_info (abfd, &internal_sym,
908 &internal_ptr->symbol, 0);
909 internal_ptr->fdr = fdr_ptr;
910 internal_ptr->local = true;
911 internal_ptr->native.lnative = lraw_src;
912 }
913 }
914
915 ecoff_data (abfd)->canonical_symbols = internal;
916
917 return true;
918}
919
920static unsigned int
f6409552
ILT
921ecoff_get_symtab_upper_bound (abfd)
922 bfd *abfd;
515c4292 923{
8fa0d3a0
ILT
924 if (ecoff_slurp_symbolic_info (abfd) == false
925 || bfd_get_symcount (abfd) == 0)
515c4292
ILT
926 return 0;
927
928 return (bfd_get_symcount (abfd) + 1) * (sizeof (ecoff_symbol_type *));
929}
930
931static unsigned int
f6409552
ILT
932ecoff_get_symtab (abfd, alocation)
933 bfd *abfd;
934 asymbol **alocation;
515c4292
ILT
935{
936 unsigned int counter = 0;
937 ecoff_symbol_type *symbase;
938 ecoff_symbol_type **location = (ecoff_symbol_type **) alocation;
939
8fa0d3a0
ILT
940 if (ecoff_slurp_symbol_table (abfd) == false
941 || bfd_get_symcount (abfd) == 0)
515c4292
ILT
942 return 0;
943
944 symbase = ecoff_data (abfd)->canonical_symbols;
945 while (counter < bfd_get_symcount (abfd))
946 {
947 *(location++) = symbase++;
948 counter++;
949 }
950 *location++ = (ecoff_symbol_type *) NULL;
951 return bfd_get_symcount (abfd);
952}
953
954/* Turn ECOFF type information into a printable string.
f6409552
ILT
955 ecoff_emit_aggregate and ecoff_type_to_string are from
956 gcc/mips-tdump.c, with swapping added and used_ptr removed. */
515c4292
ILT
957
958/* Write aggregate information to a string. */
959
960static void
f6409552
ILT
961ecoff_emit_aggregate (abfd, string, rndx, isym, which)
962 bfd *abfd;
963 char *string;
964 RNDXR *rndx;
965 long isym;
966 CONST char *which;
515c4292
ILT
967{
968 int ifd = rndx->rfd;
969 int indx = rndx->index;
970 int sym_base, ss_base;
971 CONST char *name;
972
973 if (ifd == 0xfff)
974 ifd = isym;
975
976 sym_base = ecoff_data (abfd)->fdr[ifd].isymBase;
977 ss_base = ecoff_data (abfd)->fdr[ifd].issBase;
978
979 if (indx == indexNil)
980 name = "/* no name */";
981 else
982 {
983 SYMR sym;
984
985 indx += sym_base;
986 ecoff_swap_sym_in (abfd,
987 ecoff_data (abfd)->external_sym + indx,
988 &sym);
989 name = ecoff_data (abfd)->ss + ss_base + sym.iss;
990 }
991
992 sprintf (string,
993 "%s %s { ifd = %d, index = %d }",
994 which, name, ifd,
995 indx + ecoff_data (abfd)->symbolic_header.iextMax);
996}
997
998/* Convert the type information to string format. */
999
1000static char *
f6409552
ILT
1001ecoff_type_to_string (abfd, aux_ptr, indx, bigendian)
1002 bfd *abfd;
1003 union aux_ext *aux_ptr;
1004 int indx;
1005 int bigendian;
515c4292
ILT
1006{
1007 AUXU u;
1008 struct qual {
1009 unsigned int type;
1010 int low_bound;
1011 int high_bound;
1012 int stride;
1013 } qualifiers[7];
1014
1015 unsigned int basic_type;
1016 int i;
1017 static char buffer1[1024];
1018 static char buffer2[1024];
1019 char *p1 = buffer1;
1020 char *p2 = buffer2;
1021 RNDXR rndx;
1022
1023 for (i = 0; i < 7; i++)
1024 {
1025 qualifiers[i].low_bound = 0;
1026 qualifiers[i].high_bound = 0;
1027 qualifiers[i].stride = 0;
1028 }
1029
1030 if (AUX_GET_ISYM (bigendian, &aux_ptr[indx]) == -1)
1031 return "-1 (no type)";
1032 ecoff_swap_tir_in (bigendian, &aux_ptr[indx++].a_ti, &u.ti);
1033
1034 basic_type = u.ti.bt;
1035 qualifiers[0].type = u.ti.tq0;
1036 qualifiers[1].type = u.ti.tq1;
1037 qualifiers[2].type = u.ti.tq2;
1038 qualifiers[3].type = u.ti.tq3;
1039 qualifiers[4].type = u.ti.tq4;
1040 qualifiers[5].type = u.ti.tq5;
1041 qualifiers[6].type = tqNil;
1042
1043 /*
1044 * Go get the basic type.
1045 */
1046 switch (basic_type)
1047 {
1048 case btNil: /* undefined */
1049 strcpy (p1, "nil");
1050 break;
1051
1052 case btAdr: /* address - integer same size as pointer */
1053 strcpy (p1, "address");
1054 break;
1055
1056 case btChar: /* character */
1057 strcpy (p1, "char");
1058 break;
1059
1060 case btUChar: /* unsigned character */
1061 strcpy (p1, "unsigned char");
1062 break;
1063
1064 case btShort: /* short */
1065 strcpy (p1, "short");
1066 break;
1067
1068 case btUShort: /* unsigned short */
1069 strcpy (p1, "unsigned short");
1070 break;
1071
1072 case btInt: /* int */
1073 strcpy (p1, "int");
1074 break;
1075
1076 case btUInt: /* unsigned int */
1077 strcpy (p1, "unsigned int");
1078 break;
1079
1080 case btLong: /* long */
1081 strcpy (p1, "long");
1082 break;
1083
1084 case btULong: /* unsigned long */
1085 strcpy (p1, "unsigned long");
1086 break;
1087
1088 case btFloat: /* float (real) */
1089 strcpy (p1, "float");
1090 break;
1091
1092 case btDouble: /* Double (real) */
1093 strcpy (p1, "double");
1094 break;
1095
1096 /* Structures add 1-2 aux words:
1097 1st word is [ST_RFDESCAPE, offset] pointer to struct def;
1098 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1099
1100 case btStruct: /* Structure (Record) */
1101 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
f6409552
ILT
1102 ecoff_emit_aggregate (abfd, p1, &rndx,
1103 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1104 "struct");
515c4292
ILT
1105 indx++; /* skip aux words */
1106 break;
1107
1108 /* Unions add 1-2 aux words:
1109 1st word is [ST_RFDESCAPE, offset] pointer to union def;
1110 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1111
1112 case btUnion: /* Union */
1113 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
f6409552
ILT
1114 ecoff_emit_aggregate (abfd, p1, &rndx,
1115 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1116 "union");
515c4292
ILT
1117 indx++; /* skip aux words */
1118 break;
1119
1120 /* Enumerations add 1-2 aux words:
1121 1st word is [ST_RFDESCAPE, offset] pointer to enum def;
1122 2nd word is file index if 1st word rfd is ST_RFDESCAPE. */
1123
1124 case btEnum: /* Enumeration */
1125 ecoff_swap_rndx_in (bigendian, &aux_ptr[indx].a_rndx, &rndx);
f6409552
ILT
1126 ecoff_emit_aggregate (abfd, p1, &rndx,
1127 AUX_GET_ISYM (bigendian, &aux_ptr[indx+1]),
1128 "enum");
515c4292
ILT
1129 indx++; /* skip aux words */
1130 break;
1131
1132 case btTypedef: /* defined via a typedef, isymRef points */
1133 strcpy (p1, "typedef");
1134 break;
1135
1136 case btRange: /* subrange of int */
1137 strcpy (p1, "subrange");
1138 break;
1139
1140 case btSet: /* pascal sets */
1141 strcpy (p1, "set");
1142 break;
1143
1144 case btComplex: /* fortran complex */
1145 strcpy (p1, "complex");
1146 break;
1147
1148 case btDComplex: /* fortran double complex */
1149 strcpy (p1, "double complex");
1150 break;
1151
1152 case btIndirect: /* forward or unnamed typedef */
1153 strcpy (p1, "forward/unamed typedef");
1154 break;
1155
1156 case btFixedDec: /* Fixed Decimal */
1157 strcpy (p1, "fixed decimal");
1158 break;
1159
1160 case btFloatDec: /* Float Decimal */
1161 strcpy (p1, "float decimal");
1162 break;
1163
1164 case btString: /* Varying Length Character String */
1165 strcpy (p1, "string");
1166 break;
1167
1168 case btBit: /* Aligned Bit String */
1169 strcpy (p1, "bit");
1170 break;
1171
1172 case btPicture: /* Picture */
1173 strcpy (p1, "picture");
1174 break;
1175
1176 case btVoid: /* Void */
1177 strcpy (p1, "void");
1178 break;
1179
1180 default:
1181 sprintf (p1, "Unknown basic type %d", (int) basic_type);
1182 break;
1183 }
1184
1185 p1 += strlen (buffer1);
1186
1187 /*
1188 * If this is a bitfield, get the bitsize.
1189 */
1190 if (u.ti.fBitfield)
1191 {
1192 int bitsize;
1193
1194 bitsize = AUX_GET_WIDTH (bigendian, &aux_ptr[indx++]);
1195 sprintf (p1, " : %d", bitsize);
1196 p1 += strlen (buffer1);
1197 }
1198
1199
1200 /*
1201 * Deal with any qualifiers.
1202 */
1203 if (qualifiers[0].type != tqNil)
1204 {
1205 /*
1206 * Snarf up any array bounds in the correct order. Arrays
1207 * store 5 successive words in the aux. table:
1208 * word 0 RNDXR to type of the bounds (ie, int)
1209 * word 1 Current file descriptor index
1210 * word 2 low bound
1211 * word 3 high bound (or -1 if [])
1212 * word 4 stride size in bits
1213 */
1214 for (i = 0; i < 7; i++)
1215 {
1216 if (qualifiers[i].type == tqArray)
1217 {
1218 qualifiers[i].low_bound =
1219 AUX_GET_DNLOW (bigendian, &aux_ptr[indx+2]);
1220 qualifiers[i].high_bound =
1221 AUX_GET_DNHIGH (bigendian, &aux_ptr[indx+3]);
1222 qualifiers[i].stride =
1223 AUX_GET_WIDTH (bigendian, &aux_ptr[indx+4]);
1224 indx += 5;
1225 }
1226 }
1227
1228 /*
1229 * Now print out the qualifiers.
1230 */
1231 for (i = 0; i < 6; i++)
1232 {
1233 switch (qualifiers[i].type)
1234 {
1235 case tqNil:
1236 case tqMax:
1237 break;
1238
1239 case tqPtr:
1240 strcpy (p2, "ptr to ");
1241 p2 += sizeof ("ptr to ")-1;
1242 break;
1243
1244 case tqVol:
1245 strcpy (p2, "volatile ");
1246 p2 += sizeof ("volatile ")-1;
1247 break;
1248
1249 case tqFar:
1250 strcpy (p2, "far ");
1251 p2 += sizeof ("far ")-1;
1252 break;
1253
1254 case tqProc:
1255 strcpy (p2, "func. ret. ");
1256 p2 += sizeof ("func. ret. ");
1257 break;
1258
1259 case tqArray:
1260 {
1261 int first_array = i;
1262 int j;
1263
1264 /* Print array bounds reversed (ie, in the order the C
1265 programmer writes them). C is such a fun language.... */
1266
1267 while (i < 5 && qualifiers[i+1].type == tqArray)
1268 i++;
1269
1270 for (j = i; j >= first_array; j--)
1271 {
1272 strcpy (p2, "array [");
1273 p2 += sizeof ("array [")-1;
1274 if (qualifiers[j].low_bound != 0)
1275 sprintf (p2,
1276 "%ld:%ld {%ld bits}",
1277 (long) qualifiers[j].low_bound,
1278 (long) qualifiers[j].high_bound,
1279 (long) qualifiers[j].stride);
1280
1281 else if (qualifiers[j].high_bound != -1)
1282 sprintf (p2,
1283 "%ld {%ld bits}",
1284 (long) (qualifiers[j].high_bound + 1),
1285 (long) (qualifiers[j].stride));
1286
1287 else
1288 sprintf (p2, " {%ld bits}", (long) (qualifiers[j].stride));
1289
1290 p2 += strlen (p2);
1291 strcpy (p2, "] of ");
1292 p2 += sizeof ("] of ")-1;
1293 }
1294 }
1295 break;
1296 }
1297 }
1298 }
1299
1300 strcpy (p2, buffer1);
1301 return buffer2;
1302}
1303
1304/* Print information about an ECOFF symbol. */
1305
1306static void
f6409552
ILT
1307ecoff_print_symbol (abfd, filep, symbol, how)
1308 bfd *abfd;
1309 PTR filep;
1310 asymbol *symbol;
1311 bfd_print_symbol_type how;
515c4292
ILT
1312{
1313 FILE *file = (FILE *)filep;
1314
1315 switch (how)
1316 {
1317 case bfd_print_symbol_name:
1318 fprintf (file, "%s", symbol->name);
1319 break;
1320 case bfd_print_symbol_more:
1321 if (ecoffsymbol (symbol)->local)
1322 {
1323 SYMR ecoff_sym;
1324
1325 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1326 &ecoff_sym);
1327 fprintf (file, "ecoff local %lx %x %x",
1328 (unsigned long) ecoff_sym.value,
1329 (unsigned) ecoff_sym.st, (unsigned) ecoff_sym.sc);
1330 }
1331 else
1332 {
1333 EXTR ecoff_ext;
1334
1335 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1336 &ecoff_ext);
1337 fprintf (file, "ecoff extern %lx %x %x",
1338 (unsigned long) ecoff_ext.asym.value,
1339 (unsigned) ecoff_ext.asym.st,
1340 (unsigned) ecoff_ext.asym.sc);
1341 }
1342 break;
1343 case bfd_print_symbol_nm:
1344 {
1345 CONST char *section_name = symbol->section->name;
1346
1347 bfd_print_symbol_vandf ((PTR) file, symbol);
1348 fprintf (file, " %-5s %s %s",
1349 section_name,
1350 ecoffsymbol (symbol)->local ? "l" : "e",
1351 symbol->name);
1352 }
1353 break;
1354 case bfd_print_symbol_all:
1355 /* Print out the symbols in a reasonable way */
1356 {
515c4292
ILT
1357 char type;
1358 int pos;
1359 EXTR ecoff_ext;
1360 char jmptbl;
1361 char cobol_main;
1362 char weakext;
1363
1364 if (ecoffsymbol (symbol)->local)
1365 {
1366 ecoff_swap_sym_in (abfd, ecoffsymbol (symbol)->native.lnative,
1367 &ecoff_ext.asym);
1368 type = 'l';
1369 pos = (ecoffsymbol (symbol)->native.lnative
1370 - ecoff_data (abfd)->external_sym
1371 + ecoff_data (abfd)->symbolic_header.iextMax);
1372 jmptbl = ' ';
1373 cobol_main = ' ';
1374 weakext = ' ';
1375 }
1376 else
1377 {
1378 ecoff_swap_ext_in (abfd, ecoffsymbol (symbol)->native.enative,
1379 &ecoff_ext);
1380 type = 'e';
1381 pos = (ecoffsymbol (symbol)->native.enative
1382 - ecoff_data (abfd)->external_ext);
1383 jmptbl = ecoff_ext.jmptbl ? 'j' : ' ';
1384 cobol_main = ecoff_ext.cobol_main ? 'c' : ' ';
1385 weakext = ecoff_ext.weakext ? 'w' : ' ';
1386 }
1387
1388 fprintf (file, "[%3d] %c %lx st %x sc %x indx %x %c%c%c %s",
1389 pos, type, (unsigned long) ecoff_ext.asym.value,
1390 (unsigned) ecoff_ext.asym.st,
1391 (unsigned) ecoff_ext.asym.sc,
1392 (unsigned) ecoff_ext.asym.index,
1393 jmptbl, cobol_main, weakext,
1394 symbol->name);
1395
1396 if (ecoffsymbol (symbol)->fdr != NULL
1397 && ecoff_ext.asym.index != indexNil)
1398 {
1399 unsigned indx;
1400 int bigendian;
1401 long sym_base;
1402 union aux_ext *aux_base;
1403
1404 indx = ecoff_ext.asym.index;
1405
1406 /* sym_base is used to map the fdr relative indices which
1407 appear in the file to the position number which we are
1408 using. */
1409 sym_base = ecoffsymbol (symbol)->fdr->isymBase;
1410 if (ecoffsymbol (symbol)->local)
1411 sym_base += ecoff_data (abfd)->symbolic_header.iextMax;
1412
1413 /* aux_base is the start of the aux entries for this file;
1414 asym.index is an offset from this. */
1415 aux_base = (ecoff_data (abfd)->external_aux
1416 + ecoffsymbol (symbol)->fdr->iauxBase);
1417
1418 /* The aux entries are stored in host byte order; the
1419 order is indicated by a bit in the fdr. */
1420 bigendian = ecoffsymbol (symbol)->fdr->fBigendian;
1421
1422 /* This switch is basically from gcc/mips-tdump.c */
1423 switch (ecoff_ext.asym.st)
1424 {
1425 case stNil:
1426 case stLabel:
1427 break;
1428
1429 case stFile:
1430 case stBlock:
1431 printf ("\n End+1 symbol: %ld", indx + sym_base);
1432 break;
1433
1434 case stEnd:
1435 if (ecoff_ext.asym.sc == scText
1436 || ecoff_ext.asym.sc == scInfo)
1437 printf ("\n First symbol: %ld", indx + sym_base);
1438 else
1439 printf ("\n First symbol: %ld",
1440 (AUX_GET_ISYM (bigendian,
1441 &aux_base[ecoff_ext.asym.index])
1442 + sym_base));
1443 break;
1444
1445 case stProc:
1446 case stStaticProc:
1447 if (MIPS_IS_STAB (&ecoff_ext.asym))
1448 ;
1449 else if (ecoffsymbol (symbol)->local)
1450 printf ("\n End+1 symbol: %-7ld Type: %s",
1451 (AUX_GET_ISYM (bigendian,
1452 &aux_base[ecoff_ext.asym.index])
1453 + sym_base),
f6409552
ILT
1454 ecoff_type_to_string (abfd, aux_base, indx + 1,
1455 bigendian));
515c4292 1456 else
b6bef862
ILT
1457 printf ("\n Local symbol: %d",
1458 (indx
1459 + sym_base
1460 + ecoff_data (abfd)->symbolic_header.iextMax));
515c4292
ILT
1461 break;
1462
1463 default:
1464 if (!MIPS_IS_STAB (&ecoff_ext.asym))
1465 printf ("\n Type: %s",
f6409552
ILT
1466 ecoff_type_to_string (abfd, aux_base, indx,
1467 bigendian));
515c4292
ILT
1468 break;
1469 }
1470 }
1471 }
1472 break;
1473 }
1474}
1475\f
b6bef862
ILT
1476/* Reloc handling. MIPS ECOFF relocs are packed into 8 bytes in
1477 external form. They use a bit which indicates whether the symbol
1478 is external. */
1479
1480/* Swap a reloc in. */
1481
1482static void
f6409552
ILT
1483ecoff_swap_reloc_in (abfd, ext, intern)
1484 bfd *abfd;
1485 RELOC *ext;
1486 struct internal_reloc *intern;
b6bef862
ILT
1487{
1488 intern->r_vaddr = bfd_h_get_32 (abfd, (bfd_byte *) ext->r_vaddr);
1489 if (abfd->xvec->header_byteorder_big_p != false)
1490 {
c3fe0c41 1491 intern->r_symndx = (((int) ext->r_bits[0]
b6bef862 1492 << RELOC_BITS0_SYMNDX_SH_LEFT_BIG)
c3fe0c41 1493 | ((int) ext->r_bits[1]
b6bef862 1494 << RELOC_BITS1_SYMNDX_SH_LEFT_BIG)
c3fe0c41 1495 | ((int) ext->r_bits[2]
b6bef862
ILT
1496 << RELOC_BITS2_SYMNDX_SH_LEFT_BIG));
1497 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_BIG)
1498 >> RELOC_BITS3_TYPE_SH_BIG);
1499 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_BIG) != 0;
1500 }
1501 else
1502 {
c3fe0c41 1503 intern->r_symndx = (((int) ext->r_bits[0]
b6bef862 1504 << RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE)
c3fe0c41 1505 | ((int) ext->r_bits[1]
b6bef862 1506 << RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE)
c3fe0c41 1507 | ((int) ext->r_bits[2]
b6bef862
ILT
1508 << RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE));
1509 intern->r_type = ((ext->r_bits[3] & RELOC_BITS3_TYPE_LITTLE)
1510 >> RELOC_BITS3_TYPE_SH_LITTLE);
1511 intern->r_extern = (ext->r_bits[3] & RELOC_BITS3_EXTERN_LITTLE) != 0;
1512 }
1513}
1514
1515/* Swap a reloc out. */
1516
1517static unsigned int
f6409552
ILT
1518ecoff_swap_reloc_out (abfd, src, dst)
1519 bfd *abfd;
1520 PTR src;
1521 PTR dst;
b6bef862
ILT
1522{
1523 struct internal_reloc *intern = (struct internal_reloc *) src;
1524 RELOC *ext = (RELOC *) dst;
1525
1526 bfd_h_put_32 (abfd, intern->r_vaddr, (bfd_byte *) ext->r_vaddr);
1527 if (abfd->xvec->header_byteorder_big_p != false)
1528 {
1529 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_BIG;
1530 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_BIG;
1531 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_BIG;
1532 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_BIG)
1533 & RELOC_BITS3_TYPE_BIG)
1534 | (intern->r_extern ? RELOC_BITS3_EXTERN_BIG : 0));
1535 }
1536 else
1537 {
1538 ext->r_bits[0] = intern->r_symndx >> RELOC_BITS0_SYMNDX_SH_LEFT_LITTLE;
1539 ext->r_bits[1] = intern->r_symndx >> RELOC_BITS1_SYMNDX_SH_LEFT_LITTLE;
1540 ext->r_bits[2] = intern->r_symndx >> RELOC_BITS2_SYMNDX_SH_LEFT_LITTLE;
1541 ext->r_bits[3] = (((intern->r_type << RELOC_BITS3_TYPE_SH_LITTLE)
1542 & RELOC_BITS3_TYPE_LITTLE)
1543 | (intern->r_extern ? RELOC_BITS3_EXTERN_LITTLE : 0));
1544 }
1545
1546 return RELSZ;
1547}
1548
bf4b84bc
ILT
1549/* ECOFF relocs are either against external symbols, or against
1550 sections. If we are producing relocateable output, and the reloc
1551 is against an external symbol, the resulting reloc will also be
1552 against the same symbol. In such a case, we don't want to change
1553 anything about the way the reloc is handled, since it will all be
1554 done at final link time. Rather than put special case code into
1555 bfd_perform_relocation, all the reloc types use this howto
1556 function. It just short circuits the reloc if producing
1557 relocateable output against an external symbol. */
1558
1559static bfd_reloc_status_type
1560ecoff_generic_reloc (abfd,
1561 reloc_entry,
1562 symbol,
1563 data,
1564 input_section,
1565 output_bfd)
1566 bfd *abfd;
1567 arelent *reloc_entry;
1568 asymbol *symbol;
1569 PTR data;
1570 asection *input_section;
1571 bfd *output_bfd;
1572{
1573 if (output_bfd != (bfd *) NULL
1574 && (symbol->flags & BSF_SECTION_SYM) == 0)
1575 {
1576 reloc_entry->address += input_section->output_offset;
1577 return bfd_reloc_ok;
1578 }
1579
1580 return bfd_reloc_continue;
1581}
1582
b6bef862
ILT
1583/* Do a REFHI relocation. The next reloc must be the corresponding
1584 REFLO. This has to be done in a function so that carry is handled
1585 correctly. */
1586
1587static bfd_reloc_status_type
bf4b84bc
ILT
1588ecoff_refhi_reloc (abfd,
1589 reloc_entry,
1590 symbol,
1591 data,
1592 input_section,
1593 output_bfd)
1594 bfd *abfd;
1595 arelent *reloc_entry;
1596 asymbol *symbol;
1597 PTR data;
1598 asection *input_section;
1599 bfd *output_bfd;
b6bef862
ILT
1600{
1601 bfd_reloc_status_type ret;
1602 arelent *rello;
1603 bfd_vma relocation;
b6bef862 1604 unsigned long insn;
c3fe0c41
ILT
1605 unsigned long val;
1606 unsigned long vallo;
b6bef862 1607
bf4b84bc
ILT
1608 /* If we're relocating, and this an external symbol, we don't want
1609 to change anything. */
1610 if (output_bfd != (bfd *) NULL
1611 && (symbol->flags & BSF_SECTION_SYM) == 0)
1612 {
1613 reloc_entry->address += input_section->output_offset;
1614 return bfd_reloc_ok;
1615 }
1616
b6bef862
ILT
1617 ret = bfd_reloc_ok;
1618 if (symbol->section == &bfd_und_section
1619 && output_bfd == (bfd *) NULL)
1620 ret = bfd_reloc_undefined;
1621
1622 rello = reloc_entry + 1;
1623 BFD_ASSERT (rello->howto->type == ECOFF_R_REFLO
1624 && *rello->sym_ptr_ptr == *reloc_entry->sym_ptr_ptr);
1625
382f2a3d 1626 if (bfd_is_com_section (symbol->section))
b6bef862
ILT
1627 relocation = 0;
1628 else
1629 relocation = symbol->value;
1630
bf4b84bc
ILT
1631 relocation += symbol->section->output_section->vma;
1632 relocation += symbol->section->output_offset;
c3fe0c41 1633 relocation += reloc_entry->addend;
b6bef862
ILT
1634
1635 if (reloc_entry->address > input_section->_cooked_size)
1636 return bfd_reloc_outofrange;
1637
b6bef862 1638 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
c3fe0c41
ILT
1639 vallo = bfd_get_32 (abfd, (bfd_byte *) data + rello->address) & 0xffff;
1640 val = ((insn & 0xffff) << 16) + vallo;
1641 val += relocation;
1642
1643 /* The low order 16 bits are always treated as a signed value.
1644 Therefore, a negative value in the low order bits requires an
1645 adjustment in the high order bits. We need to make this
1646 adjustment in two ways: once for the bits we took from the data,
1647 and once for the bits we are putting back in to the data. */
1648 if ((vallo & 0x8000) != 0)
1649 val -= 0x10000;
1650 if ((val & 0x8000) != 0)
1651 val += 0x10000;
1652
1653 insn = (insn &~ 0xffff) | ((val >> 16) & 0xffff);
b6bef862
ILT
1654 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1655
c3fe0c41
ILT
1656 if (output_bfd != (bfd *) NULL)
1657 reloc_entry->address += input_section->output_offset;
1658
b6bef862
ILT
1659 return ret;
1660}
1661
1662/* Do a GPREL relocation. This is a 16 bit value which must become
1663 the offset from the gp register. */
1664
1665static bfd_reloc_status_type
bf4b84bc
ILT
1666ecoff_gprel_reloc (abfd,
1667 reloc_entry,
1668 symbol,
1669 data,
1670 input_section,
1671 output_bfd)
1672 bfd *abfd;
1673 arelent *reloc_entry;
1674 asymbol *symbol;
1675 PTR data;
1676 asection *input_section;
1677 bfd *output_bfd;
b6bef862 1678{
c3fe0c41 1679 boolean relocateable;
b6bef862
ILT
1680 bfd_vma relocation;
1681 unsigned long val;
1682 unsigned long insn;
1683
bf4b84bc
ILT
1684 /* If we're relocating, and this an external symbol, we don't want
1685 to change anything. */
1686 if (output_bfd != (bfd *) NULL
1687 && (symbol->flags & BSF_SECTION_SYM) == 0)
1688 {
1689 reloc_entry->address += input_section->output_offset;
1690 return bfd_reloc_ok;
1691 }
1692
b6bef862 1693 if (output_bfd != (bfd *) NULL)
c3fe0c41
ILT
1694 relocateable = true;
1695 else
1696 {
1697 relocateable = false;
1698 output_bfd = symbol->section->output_section->owner;
1699 }
b6bef862 1700
c3fe0c41
ILT
1701 if (symbol->section == &bfd_und_section
1702 && relocateable == false)
1703 return bfd_reloc_undefined;
b6bef862 1704
c3fe0c41
ILT
1705 /* We have to figure out the gp value, so that we can adjust the
1706 symbol value correctly. We look up the symbol _gp in the output
1707 BFD. If we can't find it, we're stuck. We cache it in the ECOFF
1708 target data. */
b6bef862
ILT
1709 if (ecoff_data (output_bfd)->gp == 0)
1710 {
bf4b84bc 1711 if (relocateable != false)
c3fe0c41
ILT
1712 {
1713 /* Make up a value. */
1714 ecoff_data (output_bfd)->gp =
1715 symbol->section->output_section->vma + 0x4000;
1716 }
1717 else
1718 {
1719 unsigned int count;
1720 asymbol **sym;
1721 unsigned int i;
b6bef862 1722
c3fe0c41
ILT
1723 count = bfd_get_symcount (output_bfd);
1724 sym = bfd_get_outsymbols (output_bfd);
b6bef862 1725
c3fe0c41
ILT
1726 /* We should do something more friendly here, but we don't
1727 have a good reloc status to return. */
1728 if (sym == (asymbol **) NULL)
1729 abort ();
b6bef862 1730
c3fe0c41 1731 for (i = 0; i < count; i++, sym++)
b6bef862 1732 {
c3fe0c41
ILT
1733 register CONST char *name;
1734
1735 name = bfd_asymbol_name (*sym);
1736 if (*name == '_' && strcmp (name, "_gp") == 0)
1737 {
1738 ecoff_data (output_bfd)->gp = bfd_asymbol_value (*sym);
1739 break;
1740 }
b6bef862 1741 }
b6bef862 1742
c3fe0c41
ILT
1743 /* We should do something more friendly here, but we don't have
1744 a good reloc status to return. */
1745 if (i >= count)
1746 abort ();
1747 }
b6bef862
ILT
1748 }
1749
382f2a3d 1750 if (bfd_is_com_section (symbol->section))
b6bef862
ILT
1751 relocation = 0;
1752 else
1753 relocation = symbol->value;
1754
1755 relocation += symbol->section->output_section->vma;
1756 relocation += symbol->section->output_offset;
1757
1758 if (reloc_entry->address > input_section->_cooked_size)
1759 return bfd_reloc_outofrange;
1760
1761 insn = bfd_get_32 (abfd, (bfd_byte *) data + reloc_entry->address);
c3fe0c41
ILT
1762
1763 /* Set val to the offset into the section (if we are relocating
1764 against an external symbol, insn & 0xffff will be zero and so
1765 will reloc_entry->addend). */
1766 val = ((insn & 0xffff) + reloc_entry->addend) & 0xffff;
1767 if (val & 0x8000)
b6bef862 1768 val -= 0x10000;
c3fe0c41
ILT
1769
1770 /* Adjust val for the final section location and GP value. */
1771 val += (relocation - ecoff_data (output_bfd)->gp);
1772
b6bef862
ILT
1773 insn = (insn &~ 0xffff) | (val & 0xffff);
1774 bfd_put_32 (abfd, insn, (bfd_byte *) data + reloc_entry->address);
1775
bf4b84bc 1776 if (relocateable != false)
c3fe0c41
ILT
1777 reloc_entry->address += input_section->output_offset;
1778
1779 /* Make sure it fit in 16 bits. */
1780 if (val >= 0x8000 && val < 0xffff8000)
1781 return bfd_reloc_outofrange;
1782
1783 return bfd_reloc_ok;
b6bef862
ILT
1784}
1785
1786/* How to process the various relocs types. */
1787
1788static reloc_howto_type ecoff_howto_table[] =
1789{
1790 /* Reloc type 0 is ignored. The reloc reading code ensures that
1791 this is a reference to the .abs section, which will cause
1792 bfd_perform_relocation to do nothing. */
1793 HOWTO (ECOFF_R_IGNORE, /* type */
1794 0, /* rightshift */
1795 0, /* size (0 = byte, 1 = short, 2 = long) */
1796 8, /* bitsize (obsolete) */
1797 false, /* pc_relative */
1798 0, /* bitpos */
1799 false, /* absolute (obsolete) */
1800 false, /* complain_on_overflow */
1801 0, /* special_function */
1802 "IGNORE", /* name */
1803 false, /* partial_inplace */
1804 0, /* src_mask */
1805 0, /* dst_mask */
1806 false), /* pcrel_offset */
1807
1808 /* A 16 bit reference to a symbol, normally from a data section. */
1809 HOWTO (ECOFF_R_REFHALF, /* type */
1810 0, /* rightshift */
1811 1, /* size (0 = byte, 1 = short, 2 = long) */
1812 16, /* bitsize (obsolete) */
1813 false, /* pc_relative */
1814 0, /* bitpos */
1815 false, /* absolute (obsolete) */
1816 true, /* complain_on_overflow */
bf4b84bc 1817 ecoff_generic_reloc, /* special_function */
b6bef862
ILT
1818 "REFHALF", /* name */
1819 true, /* partial_inplace */
1820 0xffff, /* src_mask */
1821 0xffff, /* dst_mask */
1822 false), /* pcrel_offset */
1823
1824 /* A 32 bit reference to a symbol, normally from a data section. */
1825 HOWTO (ECOFF_R_REFWORD, /* type */
1826 0, /* rightshift */
1827 2, /* size (0 = byte, 1 = short, 2 = long) */
1828 32, /* bitsize (obsolete) */
1829 false, /* pc_relative */
1830 0, /* bitpos */
1831 false, /* absolute (obsolete) */
1832 true, /* complain_on_overflow */
bf4b84bc 1833 ecoff_generic_reloc, /* special_function */
b6bef862
ILT
1834 "REFWORD", /* name */
1835 true, /* partial_inplace */
1836 0xffffffff, /* src_mask */
1837 0xffffffff, /* dst_mask */
1838 false), /* pcrel_offset */
1839
1840 /* A 26 bit absolute jump address. */
1841 HOWTO (ECOFF_R_JMPADDR, /* type */
1842 2, /* rightshift */
1843 2, /* size (0 = byte, 1 = short, 2 = long) */
1844 32, /* bitsize (obsolete) */
1845 false, /* pc_relative */
1846 0, /* bitpos */
1847 false, /* absolute (obsolete) */
1848 true, /* complain_on_overflow */
bf4b84bc 1849 ecoff_generic_reloc, /* special_function */
b6bef862
ILT
1850 "JMPADDR", /* name */
1851 true, /* partial_inplace */
1852 0x3ffffff, /* src_mask */
1853 0x3ffffff, /* dst_mask */
1854 false), /* pcrel_offset */
1855
1856 /* The high 16 bits of a symbol value. Handled by the function
1857 ecoff_refhi_reloc. */
1858 HOWTO (ECOFF_R_REFHI, /* type */
1859 16, /* rightshift */
1860 2, /* size (0 = byte, 1 = short, 2 = long) */
1861 32, /* bitsize (obsolete) */
1862 false, /* pc_relative */
1863 0, /* bitpos */
1864 false, /* absolute (obsolete) */
1865 true, /* complain_on_overflow */
1866 ecoff_refhi_reloc, /* special_function */
1867 "REFHI", /* name */
1868 true, /* partial_inplace */
1869 0xffff, /* src_mask */
1870 0xffff, /* dst_mask */
1871 false), /* pcrel_offset */
1872
1873 /* The low 16 bits of a symbol value. */
1874 HOWTO (ECOFF_R_REFLO, /* type */
1875 0, /* rightshift */
1876 2, /* size (0 = byte, 1 = short, 2 = long) */
1877 32, /* bitsize (obsolete) */
1878 false, /* pc_relative */
1879 0, /* bitpos */
1880 false, /* absolute (obsolete) */
1881 true, /* complain_on_overflow */
bf4b84bc 1882 ecoff_generic_reloc, /* special_function */
b6bef862
ILT
1883 "REFLO", /* name */
1884 true, /* partial_inplace */
1885 0xffff, /* src_mask */
1886 0xffff, /* dst_mask */
1887 false), /* pcrel_offset */
1888
1889 /* A reference to an offset from the gp register. Handled by the
1890 function ecoff_gprel_reloc. */
1891 HOWTO (ECOFF_R_GPREL, /* type */
1892 0, /* rightshift */
1893 2, /* size (0 = byte, 1 = short, 2 = long) */
1894 32, /* bitsize (obsolete) */
1895 false, /* pc_relative */
1896 0, /* bitpos */
1897 false, /* absolute (obsolete) */
1898 true, /* complain_on_overflow */
1899 ecoff_gprel_reloc, /* special_function */
1900 "GPREL", /* name */
1901 true, /* partial_inplace */
1902 0xffff, /* src_mask */
1903 0xffff, /* dst_mask */
1904 false), /* pcrel_offset */
1905
1906 /* A reference to a literal using an offset from the gp register.
1907 Handled by the function ecoff_gprel_reloc. */
1908 HOWTO (ECOFF_R_LITERAL, /* type */
1909 0, /* rightshift */
1910 2, /* size (0 = byte, 1 = short, 2 = long) */
1911 32, /* bitsize (obsolete) */
1912 false, /* pc_relative */
1913 0, /* bitpos */
1914 false, /* absolute (obsolete) */
1915 true, /* complain_on_overflow */
1916 ecoff_gprel_reloc, /* special_function */
1917 "LITERAL", /* name */
1918 true, /* partial_inplace */
1919 0xffff, /* src_mask */
1920 0xffff, /* dst_mask */
1921 false) /* pcrel_offset */
1922};
1923
1924/* Read in the relocs for a section. */
1925
1926static boolean
f6409552
ILT
1927ecoff_slurp_reloc_table (abfd, section, symbols)
1928 bfd *abfd;
1929 asection *section;
1930 asymbol **symbols;
b6bef862
ILT
1931{
1932 RELOC *external_relocs;
1933 arelent *internal_relocs;
1934 arelent *rptr;
1935 unsigned int i;
1936
1937 if (section->relocation != (arelent *) NULL
1938 || section->reloc_count == 0
1939 || (section->flags & SEC_CONSTRUCTOR) != 0)
1940 return true;
1941
1942 if (ecoff_slurp_symbol_table (abfd) == false)
1943 return false;
1944
1945 internal_relocs = (arelent *) bfd_alloc (abfd,
1946 (sizeof (arelent)
1947 * section->reloc_count));
1948 external_relocs = (RELOC *) bfd_alloc (abfd, RELSZ * section->reloc_count);
1949 if (internal_relocs == (arelent *) NULL
1950 || external_relocs == (RELOC *) NULL)
1951 {
1952 bfd_error = no_memory;
1953 return false;
1954 }
1955 if (bfd_seek (abfd, section->rel_filepos, SEEK_SET) != 0)
1956 return false;
1957 if (bfd_read (external_relocs, 1, RELSZ * section->reloc_count, abfd)
1958 != RELSZ * section->reloc_count)
1959 {
1960 bfd_error = system_call_error;
1961 return false;
1962 }
1963
1964 for (i = 0, rptr = internal_relocs; i < section->reloc_count; i++, rptr++)
1965 {
1966 struct internal_reloc intern;
1967
1968 ecoff_swap_reloc_in (abfd, external_relocs + i, &intern);
1969
c3fe0c41 1970 if (intern.r_type > ECOFF_R_LITERAL)
b6bef862
ILT
1971 abort ();
1972
1973 if (intern.r_extern)
1974 {
1975 /* r_symndx is an index into the external symbols. */
1976 BFD_ASSERT (intern.r_symndx >= 0
1977 && (intern.r_symndx
1978 < ecoff_data (abfd)->symbolic_header.iextMax));
1979 rptr->sym_ptr_ptr = symbols + intern.r_symndx;
c3fe0c41 1980 rptr->addend = 0;
b6bef862
ILT
1981 }
1982 else
1983 {
1984 CONST char *sec_name;
1985 asection *sec;
1986
1987 /* r_symndx is a section key. */
1988 switch (intern.r_symndx)
1989 {
1990 case RELOC_SECTION_TEXT: sec_name = ".text"; break;
1991 case RELOC_SECTION_RDATA: sec_name = ".rdata"; break;
1992 case RELOC_SECTION_DATA: sec_name = ".data"; break;
1993 case RELOC_SECTION_SDATA: sec_name = ".sdata"; break;
1994 case RELOC_SECTION_SBSS: sec_name = ".sbss"; break;
1995 case RELOC_SECTION_BSS: sec_name = ".bss"; break;
1996 case RELOC_SECTION_INIT: sec_name = ".init"; break;
1997 case RELOC_SECTION_LIT8: sec_name = ".lit8"; break;
1998 case RELOC_SECTION_LIT4: sec_name = ".lit4"; break;
1999 default: abort ();
2000 }
2001
2002 sec = bfd_get_section_by_name (abfd, sec_name);
2003 if (sec == (asection *) NULL)
2004 abort ();
2005 rptr->sym_ptr_ptr = sec->symbol_ptr_ptr;
c3fe0c41
ILT
2006
2007 rptr->addend = - bfd_get_section_vma (abfd, sec);
2008 if (intern.r_type == ECOFF_R_GPREL
2009 || intern.r_type == ECOFF_R_LITERAL)
2010 rptr->addend += ecoff_data (abfd)->gp;
b6bef862
ILT
2011 }
2012
2013 rptr->address = intern.r_vaddr - bfd_get_section_vma (abfd, section);
b6bef862
ILT
2014 rptr->howto = &ecoff_howto_table[intern.r_type];
2015
2016 /* If the type is ECOFF_R_IGNORE, make sure this is a reference
2017 to the absolute section so that the reloc is ignored. */
2018 if (intern.r_type == ECOFF_R_IGNORE)
2019 rptr->sym_ptr_ptr = bfd_abs_section.symbol_ptr_ptr;
2020 }
2021
2022 bfd_release (abfd, external_relocs);
2023
2024 section->relocation = internal_relocs;
2025
2026 return true;
2027}
2028
2029/* Get a canonical list of relocs. */
2030
2031static unsigned int
f6409552
ILT
2032ecoff_canonicalize_reloc (abfd, section, relptr, symbols)
2033 bfd *abfd;
2034 asection *section;
2035 arelent **relptr;
2036 asymbol **symbols;
b6bef862
ILT
2037{
2038 unsigned int count;
2039
2040 if (section->flags & SEC_CONSTRUCTOR)
2041 {
2042 arelent_chain *chain;
2043
2044 /* This section has relocs made up by us, not the file, so take
2045 them out of their chain and place them into the data area
2046 provided. */
2047 for (count = 0, chain = section->constructor_chain;
2048 count < section->reloc_count;
2049 count++, chain = chain->next)
2050 *relptr++ = &chain->relent;
2051 }
bf4b84bc 2052 else
b6bef862
ILT
2053 {
2054 arelent *tblptr;
2055
2056 if (ecoff_slurp_reloc_table (abfd, section, symbols) == false)
2057 return 0;
2058
2059 tblptr = section->relocation;
2060 if (tblptr == (arelent *) NULL)
2061 return 0;
2062
2063 for (count = 0; count < section->reloc_count; count++)
2064 *relptr++ = tblptr++;
2065 }
2066
2067 *relptr = (arelent *) NULL;
2068
2069 return section->reloc_count;
2070}
2071\f
515c4292
ILT
2072/* Provided a BFD, a section and an offset into the section, calculate
2073 and return the name of the source file and the line nearest to the
2074 wanted location. */
2075
2076static boolean
f6409552
ILT
2077ecoff_find_nearest_line (abfd,
2078 section,
2079 ignore_symbols,
2080 offset,
2081 filename_ptr,
2082 functionname_ptr,
2083 retline_ptr)
2084 bfd *abfd;
2085 asection *section;
2086 asymbol **ignore_symbols;
2087 bfd_vma offset;
2088 CONST char **filename_ptr;
2089 CONST char **functionname_ptr;
2090 unsigned int *retline_ptr;
515c4292
ILT
2091{
2092 FDR *fdr_ptr;
2093 FDR *fdr_start;
2094 FDR *fdr_end;
2095 FDR *fdr_hold;
2096 struct pdr_ext *pdr_ptr;
2097 struct pdr_ext *pdr_end;
2098 PDR pdr;
2099 unsigned char *line_ptr;
2100 unsigned char *line_end;
2101 int lineno;
515c4292
ILT
2102
2103 /* If we're not in the .text section, we don't have any line
2104 numbers. */
6d18a99d
ILT
2105 if (strcmp (section->name, _TEXT) != 0
2106 || offset < ecoff_data (abfd)->text_start
2107 || offset >= ecoff_data (abfd)->text_end)
515c4292
ILT
2108 return false;
2109
8fa0d3a0
ILT
2110 /* Make sure we have the FDR's. */
2111 if (ecoff_slurp_symbolic_info (abfd) == false
2112 || bfd_get_symcount (abfd) == 0)
2113 return false;
2114
515c4292
ILT
2115 /* Each file descriptor (FDR) has a memory address. Here we track
2116 down which FDR we want. The FDR's are stored in increasing
2117 memory order. If speed is ever important, this can become a
2118 binary search. We must ignore FDR's with no PDR entries; they
2119 will have the adr of the FDR before or after them. */
2120 fdr_start = ecoff_data (abfd)->fdr;
2121 fdr_end = fdr_start + ecoff_data (abfd)->symbolic_header.ifdMax;
2122 fdr_hold = (FDR *) NULL;
2123 for (fdr_ptr = fdr_start; fdr_ptr < fdr_end; fdr_ptr++)
2124 {
6d18a99d
ILT
2125 if (fdr_ptr->cpd == 0)
2126 continue;
515c4292
ILT
2127 if (offset < fdr_ptr->adr)
2128 break;
6d18a99d 2129 fdr_hold = fdr_ptr;
515c4292
ILT
2130 }
2131 if (fdr_hold == (FDR *) NULL)
2132 return false;
2133 fdr_ptr = fdr_hold;
2134
2135 /* Each FDR has a list of procedure descriptors (PDR). PDR's also
2136 have an address, which is relative to the FDR address, and are
2137 also stored in increasing memory order. */
2138 offset -= fdr_ptr->adr;
2139 pdr_ptr = ecoff_data (abfd)->external_pdr + fdr_ptr->ipdFirst;
2140 pdr_end = pdr_ptr + fdr_ptr->cpd;
2141 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2142 if (offset < pdr.adr)
2143 return false;
2144 for (pdr_ptr++; pdr_ptr < pdr_end; pdr_ptr++)
2145 {
2146 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2147 if (offset < pdr.adr)
2148 break;
2149 }
2150
2151 /* Now we can look for the actual line number. The line numbers are
2152 stored in a very funky format, which I won't try to describe.
2153 Note that right here pdr_ptr and pdr hold the PDR *after* the one
2154 we want; we need this to compute line_end. */
2155 line_end = ecoff_data (abfd)->line;
2156 if (pdr_ptr == pdr_end)
2157 line_end += fdr_ptr->cbLineOffset + fdr_ptr->cbLine;
2158 else
2159 line_end += fdr_ptr->cbLineOffset + pdr.cbLineOffset;
2160
2161 /* Now change pdr and pdr_ptr to the one we want. */
2162 pdr_ptr--;
2163 ecoff_swap_pdr_in (abfd, pdr_ptr, &pdr);
2164
2165 offset -= pdr.adr;
2166 lineno = pdr.lnLow;
2167 line_ptr = (ecoff_data (abfd)->line
2168 + fdr_ptr->cbLineOffset
2169 + pdr.cbLineOffset);
2170 while (line_ptr < line_end)
2171 {
2172 int delta;
2173 int count;
2174
2175 delta = *line_ptr >> 4;
2176 if (delta >= 0x8)
2177 delta -= 0x10;
2178 count = (*line_ptr & 0xf) + 1;
2179 ++line_ptr;
2180 if (delta == -8)
2181 {
2182 delta = (((line_ptr[0]) & 0xff) << 8) + ((line_ptr[1]) & 0xff);
2183 if (delta >= 0x8000)
2184 delta -= 0x10000;
2185 line_ptr += 2;
2186 }
2187 lineno += delta;
2188 if (offset < count * 4)
2189 break;
2190 offset -= count * 4;
2191 }
2192
bf4b84bc
ILT
2193 /* If fdr_ptr->rss is -1, then this file does not have full symbols,
2194 at least according to gdb/mipsread.c. */
2195 if (fdr_ptr->rss == -1)
2196 {
2197 *filename_ptr = NULL;
2198 if (pdr.isym == -1)
2199 *functionname_ptr = NULL;
2200 else
2201 {
2202 EXTR proc_ext;
2203
2204 ecoff_swap_ext_in (abfd,
2205 (ecoff_data (abfd)->external_ext
2206 + pdr.isym),
2207 &proc_ext);
2208 *functionname_ptr = ecoff_data (abfd)->ssext + proc_ext.asym.iss;
2209 }
2210 }
2211 else
2212 {
2213 SYMR proc_sym;
2214
2215 *filename_ptr = ecoff_data (abfd)->ss + fdr_ptr->issBase + fdr_ptr->rss;
2216 ecoff_swap_sym_in (abfd,
2217 (ecoff_data (abfd)->external_sym
2218 + fdr_ptr->isymBase
2219 + pdr.isym),
2220 &proc_sym);
2221 *functionname_ptr = (ecoff_data (abfd)->ss
2222 + fdr_ptr->issBase
2223 + proc_sym.iss);
2224 }
515c4292
ILT
2225 *retline_ptr = lineno;
2226 return true;
2227}
2228\f
8fa0d3a0
ILT
2229/* We can't use the generic linking routines for ECOFF, because we
2230 have to handle all the debugging information. The generic link
2231 routine just works out the section contents and attaches a list of
2232 symbols.
2233
2234 We link by looping over all the seclets. We make two passes. On
2235 the first we set the actual section contents and determine the size
2236 of the debugging information. On the second we accumulate the
2237 debugging information and write it out.
2238
2239 This currently always accumulates the debugging information, which
2240 is incorrect, because it ignores the -s and -S options of the
2241 linker. The linker needs to be modified to give us that
2242 information in a more useful format (currently it just provides a
2243 list of symbols which should appear in the output file). */
2244
2245/* Clear the output_has_begun flag for all the input BFD's. We use it
2246 to avoid linking in the debugging information for a BFD more than
2247 once. */
2248
2249static void
f6409552
ILT
2250ecoff_clear_output_flags (abfd)
2251 bfd *abfd;
8fa0d3a0
ILT
2252{
2253 register asection *o;
2254 register bfd_seclet_type *p;
2255
2256 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2257 for (p = o->seclets_head;
2258 p != (bfd_seclet_type *) NULL;
2259 p = p->next)
2260 if (p->type == bfd_indirect_seclet)
2261 p->u.indirect.section->owner->output_has_begun = false;
2262}
2263
2264/* Handle an indirect seclet on the first pass. Set the contents of
2265 the output section, and accumulate the debugging information if
2266 any. */
2267
2268static boolean
f6409552
ILT
2269ecoff_rel (output_bfd, seclet, output_section, data, relocateable)
2270 bfd *output_bfd;
2271 bfd_seclet_type *seclet;
2272 asection *output_section;
2273 PTR data;
2274 boolean relocateable;
8fa0d3a0
ILT
2275{
2276 bfd *input_bfd;
2277 HDRR *output_symhdr;
2278 HDRR *input_symhdr;
2279
2280 if ((output_section->flags & SEC_HAS_CONTENTS)
2281 && !(output_section->flags & SEC_NEVER_LOAD)
2282 && (output_section->flags & SEC_LOAD)
2283 && seclet->size)
2284 {
2285 data = (PTR) bfd_get_relocated_section_contents (output_bfd,
2286 seclet,
2287 data,
2288 relocateable);
2289 if (bfd_set_section_contents (output_bfd,
2290 output_section,
2291 data,
2292 seclet->offset,
2293 seclet->size)
2294 == false)
2295 {
2296 abort();
2297 }
2298 }
2299
2300 input_bfd = seclet->u.indirect.section->owner;
2301
2302 /* We want to figure out how much space will be required to
2303 incorporate all the debugging information from input_bfd. We use
2304 the output_has_begun field to avoid adding it in more than once.
2305 The actual incorporation is done in the second pass, in
2306 ecoff_get_debug. The code has to parallel that code in its
2307 manipulations of output_symhdr. */
2308
2309 if (input_bfd->output_has_begun)
2310 return true;
2311 input_bfd->output_has_begun = true;
2312
2313 output_symhdr = &ecoff_data (output_bfd)->symbolic_header;
2314
2315 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2316 {
2317 asymbol **symbols;
2318 asymbol **sym_ptr;
2319 asymbol **sym_end;
2320
c3fe0c41
ILT
2321 /* We just accumulate local symbols from a non-ECOFF BFD. The
2322 external symbols are handled separately. */
8fa0d3a0
ILT
2323
2324 symbols = (asymbol **) bfd_alloc (output_bfd,
2325 get_symtab_upper_bound (input_bfd));
2326 if (symbols == (asymbol **) NULL)
2327 {
2328 bfd_error = no_memory;
2329 return false;
2330 }
2331 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2332
2333 for (sym_ptr = symbols; sym_ptr < sym_end; sym_ptr++)
2334 {
2335 size_t len;
2336
2337 len = strlen ((*sym_ptr)->name);
2338 if (((*sym_ptr)->flags & BSF_EXPORT) == 0)
2339 {
2340 ++output_symhdr->isymMax;
2341 output_symhdr->issMax += len + 1;
2342 }
8fa0d3a0
ILT
2343 }
2344
2345 bfd_release (output_bfd, (PTR) symbols);
2346
2347 ++output_symhdr->ifdMax;
2348
2349 return true;
2350 }
2351
2352 /* We simply add in the information from another ECOFF BFD. First
2353 we make sure we have the symbolic information. */
c3fe0c41 2354 if (ecoff_slurp_symbol_table (input_bfd) == false)
8fa0d3a0
ILT
2355 return false;
2356 if (bfd_get_symcount (input_bfd) == 0)
2357 return true;
2358
2359 input_symhdr = &ecoff_data (input_bfd)->symbolic_header;
2360
c3fe0c41
ILT
2361 /* Figure out how much information we are going to be putting in.
2362 The external symbols are handled separately. */
8fa0d3a0
ILT
2363 output_symhdr->ilineMax += input_symhdr->ilineMax;
2364 output_symhdr->cbLine += input_symhdr->cbLine;
2365 output_symhdr->idnMax += input_symhdr->idnMax;
2366 output_symhdr->ipdMax += input_symhdr->ipdMax;
2367 output_symhdr->isymMax += input_symhdr->isymMax;
2368 output_symhdr->ioptMax += input_symhdr->ioptMax;
2369 output_symhdr->iauxMax += input_symhdr->iauxMax;
2370 output_symhdr->issMax += input_symhdr->issMax;
8fa0d3a0 2371 output_symhdr->ifdMax += input_symhdr->ifdMax;
8fa0d3a0
ILT
2372
2373 /* The RFD's are special, since we create them if needed. */
2374 if (input_symhdr->crfd > 0)
2375 output_symhdr->crfd += input_symhdr->crfd;
2376 else
2377 output_symhdr->crfd += input_symhdr->ifdMax;
2378
2379 return true;
2380}
2381
2382/* Handle an arbitrary seclet on the first pass. */
2383
2384static boolean
f6409552
ILT
2385ecoff_dump_seclet (abfd, seclet, section, data, relocateable)
2386 bfd *abfd;
2387 bfd_seclet_type *seclet;
2388 asection *section;
2389 PTR data;
2390 boolean relocateable;
8fa0d3a0
ILT
2391{
2392 switch (seclet->type)
2393 {
2394 case bfd_indirect_seclet:
2395 /* The contents of this section come from another one somewhere
2396 else. */
2397 return ecoff_rel (abfd, seclet, section, data, relocateable);
2398
2399 case bfd_fill_seclet:
c3fe0c41
ILT
2400 /* Fill in the section with fill.value. This is used to pad out
2401 sections, but we must avoid padding the .bss section. */
2402 if ((section->flags & SEC_HAS_CONTENTS) == 0)
2403 {
2404 if (seclet->u.fill.value != 0)
2405 abort ();
2406 }
2407 else
2408 {
2409 char *d = (char *) bfd_alloc (abfd, seclet->size);
2410 unsigned int i;
2411 boolean ret;
2412
2413 for (i = 0; i < seclet->size; i+=2)
2414 d[i] = seclet->u.fill.value >> 8;
2415 for (i = 1; i < seclet->size; i+=2)
2416 d[i] = seclet->u.fill.value;
2417 ret = bfd_set_section_contents (abfd, section, d, seclet->offset,
2418 seclet->size);
2419 bfd_release (abfd, (PTR) d);
2420 return ret;
2421 }
2422 break;
8fa0d3a0
ILT
2423
2424 default:
2425 abort();
2426 }
2427
2428 return true;
2429}
2430
2431/* Add a string to the debugging information we are accumulating for a
2432 file. Return the offset from the fdr string base or from the
2433 external string base. */
2434
2435static long
f6409552
ILT
2436ecoff_add_string (output_bfd, fdr, string, external)
2437 bfd *output_bfd;
2438 FDR *fdr;
2439 CONST char *string;
2440 boolean external;
8fa0d3a0
ILT
2441{
2442 HDRR *symhdr;
2443 size_t len;
2444 long ret;
2445
2446 symhdr = &ecoff_data (output_bfd)->symbolic_header;
2447 len = strlen (string);
2448 if (external)
2449 {
2450 strcpy (ecoff_data (output_bfd)->ssext + symhdr->issExtMax, string);
2451 ret = symhdr->issExtMax;
2452 symhdr->issExtMax += len + 1;
2453 }
2454 else
2455 {
2456 strcpy (ecoff_data (output_bfd)->ss + symhdr->issMax, string);
2457 ret = fdr->cbSs;
2458 symhdr->issMax += len + 1;
2459 fdr->cbSs += len + 1;
2460 }
2461 return ret;
2462}
2463
2464/* Accumulate the debugging information from an input section. */
2465
2466static boolean
f6409552
ILT
2467ecoff_get_debug (output_bfd, seclet, section, relocateable)
2468 bfd *output_bfd;
2469 bfd_seclet_type *seclet;
2470 asection *section;
2471 boolean relocateable;
8fa0d3a0
ILT
2472{
2473 bfd *input_bfd;
2474 HDRR *output_symhdr;
2475 HDRR *input_symhdr;
2476 ecoff_data_type *output_ecoff;
2477 ecoff_data_type *input_ecoff;
c3fe0c41
ILT
2478 unsigned int count;
2479 struct sym_ext *sym_out;
c3fe0c41
ILT
2480 ecoff_symbol_type *esym_ptr;
2481 ecoff_symbol_type *esym_end;
8fa0d3a0
ILT
2482 FDR *fdr_ptr;
2483 FDR *fdr_end;
2484 struct fdr_ext *fdr_out;
8fa0d3a0
ILT
2485
2486 input_bfd = seclet->u.indirect.section->owner;
2487
2488 /* Don't get the information more than once. */
2489 if (input_bfd->output_has_begun)
2490 return true;
2491 input_bfd->output_has_begun = true;
2492
2493 output_ecoff = ecoff_data (output_bfd);
2494 output_symhdr = &output_ecoff->symbolic_header;
2495
2496 if (input_bfd->xvec->flavour != bfd_target_ecoff_flavour)
2497 {
2498 FDR fdr;
2499 asymbol **symbols;
2500 asymbol **sym_ptr;
2501 asymbol **sym_end;
2502
2503 /* This is not an ECOFF BFD. Just gather the symbols. */
2504
2505 memset (&fdr, 0, sizeof fdr);
2506
2507 fdr.adr = bfd_get_section_vma (output_bfd, section) + seclet->offset;
2508 fdr.issBase = output_symhdr->issMax;
2509 fdr.cbSs = 0;
2510 fdr.rss = ecoff_add_string (output_bfd,
2511 &fdr,
2512 bfd_get_filename (input_bfd),
2513 false);
2514 fdr.isymBase = output_symhdr->isymMax;
2515
c3fe0c41 2516 /* Get the local symbols from the input BFD. */
8fa0d3a0
ILT
2517 symbols = (asymbol **) bfd_alloc (output_bfd,
2518 get_symtab_upper_bound (input_bfd));
2519 if (symbols == (asymbol **) NULL)
2520 {
2521 bfd_error = no_memory;
2522 return false;
2523 }
2524 sym_end = symbols + bfd_canonicalize_symtab (input_bfd, symbols);
2525
c3fe0c41
ILT
2526 /* Handle the local symbols. Any external symbols are handled
2527 separately. */
8fa0d3a0
ILT
2528 fdr.csym = 0;
2529 for (sym_ptr = symbols; sym_ptr != sym_end; sym_ptr++)
2530 {
2531 SYMR internal_sym;
2532
2533 if (((*sym_ptr)->flags & BSF_EXPORT) != 0)
2534 continue;
2535 memset (&internal_sym, 0, sizeof internal_sym);
2536 internal_sym.iss = ecoff_add_string (output_bfd,
2537 &fdr,
2538 (*sym_ptr)->name,
2539 false);
5e462ed9
ILT
2540
2541 if (bfd_is_com_section ((*sym_ptr)->section)
2542 || (*sym_ptr)->section == &bfd_und_section)
2543 internal_sym.value = (*sym_ptr)->value;
2544 else
2545 internal_sym.value = ((*sym_ptr)->value
2546 + (*sym_ptr)->section->output_offset
2547 + (*sym_ptr)->section->output_section->vma);
8fa0d3a0
ILT
2548 internal_sym.st = stNil;
2549 internal_sym.sc = scUndefined;
2550 internal_sym.index = indexNil;
2551 ecoff_swap_sym_out (output_bfd, &internal_sym,
2552 (output_ecoff->external_sym
2553 + output_symhdr->isymMax));
2554 ++fdr.csym;
2555 ++output_symhdr->isymMax;
2556 }
2557
8fa0d3a0
ILT
2558 bfd_release (output_bfd, (PTR) symbols);
2559
c3fe0c41
ILT
2560 /* Leave everything else in the FDR zeroed out. This will cause
2561 the lang field to be langC. The fBigendian field will
2562 indicate little endian format, but it doesn't matter because
2563 it only applies to aux fields and there are none. */
8fa0d3a0
ILT
2564
2565 ecoff_swap_fdr_out (output_bfd, &fdr,
2566 (output_ecoff->external_fdr
2567 + output_symhdr->ifdMax));
2568 ++output_symhdr->ifdMax;
2569 return true;
2570 }
2571
2572 /* This is an ECOFF BFD. We want to grab the information from
2573 input_bfd and attach it to output_bfd. */
c3fe0c41
ILT
2574 count = bfd_get_symcount (input_bfd);
2575 if (count == 0)
8fa0d3a0
ILT
2576 return true;
2577 input_ecoff = ecoff_data (input_bfd);
2578 input_symhdr = &input_ecoff->symbolic_header;
2579
c3fe0c41
ILT
2580 /* I think that it is more efficient to simply copy the debugging
2581 information from the input BFD to the output BFD. Because ECOFF
2582 uses relative pointers for most of the debugging information,
2583 only a little of it has to be changed at all. */
2584
2585 /* Swap in the local symbols, adjust their values, and swap them out
2586 again. The external symbols are handled separately. */
2587 sym_out = output_ecoff->external_sym + output_symhdr->isymMax;
2588
2589 esym_ptr = ecoff_data (input_bfd)->canonical_symbols;
2590 esym_end = esym_ptr + count;
2591 for (; esym_ptr < esym_end; esym_ptr++)
2592 {
2593 if (esym_ptr->local)
2594 {
2595 SYMR sym;
2596
2597 ecoff_swap_sym_in (input_bfd, esym_ptr->native.lnative, &sym);
5e462ed9
ILT
2598
2599 /* If we're producing an executable, move common symbols
2600 into bss. */
2601 if (relocateable == false)
2602 {
2603 if (sym.sc == scCommon)
2604 sym.sc = scBss;
2605 else if (sym.sc == scSCommon)
2606 sym.sc = scSBss;
2607 }
2608
382f2a3d 2609 if (! bfd_is_com_section (esym_ptr->symbol.section)
c3fe0c41
ILT
2610 && (esym_ptr->symbol.flags & BSF_DEBUGGING) == 0
2611 && esym_ptr->symbol.section != &bfd_und_section)
2612 sym.value = (esym_ptr->symbol.value
2613 + esym_ptr->symbol.section->output_offset
2614 + esym_ptr->symbol.section->output_section->vma);
2615 ecoff_swap_sym_out (output_bfd, &sym, sym_out);
2616 ++sym_out;
2617 }
2618 }
2619
2620 /* That should have accounted for all the local symbols in
2621 input_bfd. */
2622 BFD_ASSERT ((sym_out - output_ecoff->external_sym) - output_symhdr->isymMax
2623 == input_symhdr->isymMax);
2624
2625 /* Copy the information that does not need swapping. */
2626 memcpy (output_ecoff->line + output_symhdr->cbLine,
8fa0d3a0
ILT
2627 input_ecoff->line,
2628 input_symhdr->cbLine * sizeof (unsigned char));
2629 memcpy (output_ecoff->external_aux + output_symhdr->iauxMax,
2630 input_ecoff->external_aux,
2631 input_symhdr->iauxMax * sizeof (union aux_ext));
2632 memcpy (output_ecoff->ss + output_symhdr->issMax,
2633 input_ecoff->ss,
2634 input_symhdr->issMax * sizeof (char));
8fa0d3a0
ILT
2635
2636 /* Some of the information may need to be swapped. */
c3fe0c41
ILT
2637 if (output_bfd->xvec->header_byteorder_big_p
2638 == input_bfd->xvec->header_byteorder_big_p)
8fa0d3a0
ILT
2639 {
2640 /* The two BFD's have the same endianness, so memcpy will
2641 suffice. */
2642 memcpy (output_ecoff->external_dnr + output_symhdr->idnMax,
2643 input_ecoff->external_dnr,
2644 input_symhdr->idnMax * sizeof (struct dnr_ext));
2645 memcpy (output_ecoff->external_pdr + output_symhdr->ipdMax,
2646 input_ecoff->external_pdr,
2647 input_symhdr->ipdMax * sizeof (struct pdr_ext));
8fa0d3a0
ILT
2648 memcpy (output_ecoff->external_opt + output_symhdr->ioptMax,
2649 input_ecoff->external_opt,
2650 input_symhdr->ioptMax * sizeof (struct opt_ext));
2651 }
2652 else
2653 {
2654 struct dnr_ext *dnr_in;
2655 struct dnr_ext *dnr_end;
2656 struct dnr_ext *dnr_out;
2657 struct pdr_ext *pdr_in;
2658 struct pdr_ext *pdr_end;
2659 struct pdr_ext *pdr_out;
8fa0d3a0
ILT
2660 struct opt_ext *opt_in;
2661 struct opt_ext *opt_end;
2662 struct opt_ext *opt_out;
2663
2664 /* The two BFD's have different endianness, so we must swap
2665 everything in and out. This code would always work, but it
c3fe0c41 2666 would be slow in the normal case. */
8fa0d3a0
ILT
2667 dnr_in = input_ecoff->external_dnr;
2668 dnr_end = dnr_in + input_symhdr->idnMax;
2669 dnr_out = output_ecoff->external_dnr + output_symhdr->idnMax;
2670 for (; dnr_in < dnr_end; dnr_in++, dnr_out++)
2671 {
2672 DNR dnr;
2673
2674 ecoff_swap_dnr_in (input_bfd, dnr_in, &dnr);
2675 ecoff_swap_dnr_out (output_bfd, &dnr, dnr_out);
2676 }
2677 pdr_in = input_ecoff->external_pdr;
2678 pdr_end = pdr_in + input_symhdr->ipdMax;
2679 pdr_out = output_ecoff->external_pdr + output_symhdr->ipdMax;
2680 for (; pdr_in < pdr_end; pdr_in++, pdr_out++)
2681 {
2682 PDR pdr;
2683
2684 ecoff_swap_pdr_in (input_bfd, pdr_in, &pdr);
2685 ecoff_swap_pdr_out (output_bfd, &pdr, pdr_out);
2686 }
8fa0d3a0
ILT
2687 opt_in = input_ecoff->external_opt;
2688 opt_end = opt_in + input_symhdr->ioptMax;
2689 opt_out = output_ecoff->external_opt + output_symhdr->ioptMax;
2690 for (; opt_in < opt_end; opt_in++, opt_out++)
2691 {
2692 OPTR opt;
2693
2694 ecoff_swap_opt_in (input_bfd, opt_in, &opt);
2695 ecoff_swap_opt_out (output_bfd, &opt, opt_out);
2696 }
2697 }
2698
c3fe0c41
ILT
2699 /* Set ifdbase so that the external symbols know how to adjust their
2700 ifd values. */
2701 input_ecoff->ifdbase = output_symhdr->ifdMax;
2702
8fa0d3a0
ILT
2703 fdr_ptr = input_ecoff->fdr;
2704 fdr_end = fdr_ptr + input_symhdr->ifdMax;
2705 fdr_out = output_ecoff->external_fdr + output_symhdr->ifdMax;
2706 for (; fdr_ptr < fdr_end; fdr_ptr++, fdr_out++)
2707 {
2708 FDR fdr;
2709
2710 fdr = *fdr_ptr;
2711
2712 /* The memory address for this fdr is the address for the seclet
2713 plus the offset to this fdr within input_bfd. */
2714 fdr.adr = (bfd_get_section_vma (output_bfd, section)
2715 + seclet->offset
2716 + (fdr_ptr->adr - input_ecoff->fdr->adr));
2717
bf4b84bc
ILT
2718 fdr.issBase += output_symhdr->issMax;
2719 fdr.isymBase += output_symhdr->isymMax;
2720 fdr.ilineBase += output_symhdr->ilineMax;
2721 fdr.ioptBase += output_symhdr->ioptMax;
2722 fdr.ipdFirst += output_symhdr->ipdMax;
2723 fdr.iauxBase += output_symhdr->iauxMax;
2724 fdr.rfdBase += output_symhdr->crfd;
c3fe0c41
ILT
2725
2726 /* If there are no RFD's, we are going to add some. We don't
2727 want to adjust irfd for this, so that all the FDR's can share
2728 the RFD's. */
2729 if (input_symhdr->crfd == 0)
2730 fdr.crfd = input_symhdr->ifdMax;
2731
2732 if (fdr.cbLine != 0)
bf4b84bc 2733 fdr.cbLineOffset += output_symhdr->cbLine;
8fa0d3a0
ILT
2734
2735 ecoff_swap_fdr_out (output_bfd, &fdr, fdr_out);
2736 }
2737
2738 if (input_symhdr->crfd > 0)
2739 {
2740 struct rfd_ext *rfd_in;
2741 struct rfd_ext *rfd_end;
2742 struct rfd_ext *rfd_out;
2743
2744 /* Swap and adjust the RFD's. RFD's are only created by the
2745 linker, so this will only be necessary if one of the input
2746 files is the result of a partial link. Presumably all
2747 necessary RFD's are present. */
2748 rfd_in = input_ecoff->external_rfd;
2749 rfd_end = rfd_in + input_symhdr->crfd;
2750 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2751 for (; rfd_in < rfd_end; rfd_in++, rfd_out++)
2752 {
2753 RFDT rfd;
2754
2755 ecoff_swap_rfd_in (input_bfd, rfd_in, &rfd);
2756 rfd += output_symhdr->ifdMax;
2757 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2758 }
2759 output_symhdr->crfd += input_symhdr->crfd;
2760 }
2761 else
2762 {
2763 struct rfd_ext *rfd_out;
2764 struct rfd_ext *rfd_end;
2765 RFDT rfd;
2766
2767 /* Create RFD's. Some of the debugging information includes
2768 relative file indices. These indices are taken as indices to
2769 the RFD table if there is one, or to the global table if
2770 there is not. If we did not create RFD's, we would have to
2771 parse and adjust all the debugging information which contains
2772 file indices. */
2773 rfd = output_symhdr->ifdMax;
2774 rfd_out = output_ecoff->external_rfd + output_symhdr->crfd;
2775 rfd_end = rfd_out + input_symhdr->ifdMax;
2776 for (; rfd_out < rfd_end; rfd_out++, rfd++)
2777 ecoff_swap_rfd_out (output_bfd, &rfd, rfd_out);
2778 output_symhdr->crfd += input_symhdr->ifdMax;
2779 }
2780
c3fe0c41
ILT
2781 /* Combine the register masks. */
2782 {
2783 int i;
8fa0d3a0 2784
c3fe0c41 2785 output_ecoff->gprmask |= input_ecoff->gprmask;
5e462ed9 2786 for (i = 0; i < 4; i++)
c3fe0c41
ILT
2787 output_ecoff->cprmask[i] |= input_ecoff->cprmask[i];
2788 }
8fa0d3a0
ILT
2789
2790 /* Update the counts. */
2791 output_symhdr->ilineMax += input_symhdr->ilineMax;
2792 output_symhdr->cbLine += input_symhdr->cbLine;
2793 output_symhdr->idnMax += input_symhdr->idnMax;
2794 output_symhdr->ipdMax += input_symhdr->ipdMax;
2795 output_symhdr->isymMax += input_symhdr->isymMax;
2796 output_symhdr->ioptMax += input_symhdr->ioptMax;
2797 output_symhdr->iauxMax += input_symhdr->iauxMax;
2798 output_symhdr->issMax += input_symhdr->issMax;
8fa0d3a0 2799 output_symhdr->ifdMax += input_symhdr->ifdMax;
8fa0d3a0 2800
8fa0d3a0
ILT
2801 return true;
2802}
2803
2804/* This is the actual link routine. It makes two passes over all the
2805 seclets. */
2806
2807static boolean
f6409552
ILT
2808ecoff_bfd_seclet_link (abfd, data, relocateable)
2809 bfd *abfd;
2810 PTR data;
2811 boolean relocateable;
8fa0d3a0
ILT
2812{
2813 HDRR *symhdr;
2814 int ipass;
2815 register asection *o;
2816 register bfd_seclet_type *p;
c3fe0c41 2817 asymbol **sym_ptr_ptr;
8fa0d3a0
ILT
2818 bfd_size_type size;
2819 char *raw;
2820
2821 /* We accumulate the debugging information counts in the symbolic
2822 header. */
2823 symhdr = &ecoff_data (abfd)->symbolic_header;
c3fe0c41
ILT
2824 symhdr->magic = magicSym;
2825 /* FIXME: What should the version stamp be? */
2826 symhdr->vstamp = 0;
8fa0d3a0
ILT
2827 symhdr->ilineMax = 0;
2828 symhdr->cbLine = 0;
2829 symhdr->idnMax = 0;
2830 symhdr->ipdMax = 0;
2831 symhdr->isymMax = 0;
2832 symhdr->ioptMax = 0;
2833 symhdr->iauxMax = 0;
2834 symhdr->issMax = 0;
2835 symhdr->issExtMax = 0;
2836 symhdr->ifdMax = 0;
2837 symhdr->crfd = 0;
2838 symhdr->iextMax = 0;
2839
c3fe0c41
ILT
2840 /* We need to copy over the debugging symbols from each input BFD.
2841 When we do this copying, we have to adjust the text address in
2842 the FDR structures, so we have to know the text address used for
2843 the input BFD. Since we only want to copy the symbols once per
2844 input BFD, but we are going to look at each input BFD multiple
2845 times (once for each section it provides), we arrange to always
2846 look at the text section first. That means that when we copy the
2847 debugging information, we always know the text address. So we
2848 actually do each pass in two sub passes; first the text sections,
2849 then the non-text sections. We use the output_has_begun flag to
2850 determine whether we have copied over the debugging information
2851 yet. */
8fa0d3a0
ILT
2852
2853 /* Do the first pass: set the output section contents and count the
2854 debugging information. */
2855 ecoff_clear_output_flags (abfd);
2856 for (ipass = 0; ipass < 2; ipass++)
2857 {
2858 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2859 {
2860 /* For SEC_CODE sections, (flags & SEC_CODE) == 0 is false,
2861 so they are done on pass 0. For other sections the
2862 expression is true, so they are done on pass 1. */
2863 if (((o->flags & SEC_CODE) == 0) != ipass)
2864 continue;
2865
2866 for (p = o->seclets_head;
2867 p != (bfd_seclet_type *) NULL;
2868 p = p->next)
2869 {
2870 if (ecoff_dump_seclet (abfd, p, o, data, relocateable)
2871 == false)
2872 return false;
2873 }
2874 }
2875 }
2876
c3fe0c41
ILT
2877 /* We handle the external symbols differently. We use the ones
2878 attached to the output_bfd. The linker will have already
2879 determined which symbols are to be attached. Here we just
2880 determine how much space we will need for them. */
2881 sym_ptr_ptr = bfd_get_outsymbols (abfd);
2882 if (sym_ptr_ptr != NULL)
2883 {
2884 asymbol **sym_end;
2885
2886 sym_end = sym_ptr_ptr + bfd_get_symcount (abfd);
2887 for (; sym_ptr_ptr < sym_end; sym_ptr_ptr++)
2888 {
2889 if (((*sym_ptr_ptr)->flags & BSF_DEBUGGING) == 0
2890 && ((*sym_ptr_ptr)->flags & BSF_LOCAL) == 0)
2891 {
2892 ++symhdr->iextMax;
2893 symhdr->issExtMax += strlen ((*sym_ptr_ptr)->name) + 1;
2894 }
2895 }
2896 }
2897
2898 /* Adjust the counts so that structures are longword aligned. */
2899 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
2900 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
2901 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
2902
8fa0d3a0
ILT
2903 /* Now the counts in symhdr are the correct size for the debugging
2904 information. We allocate the right amount of space, and reset
2905 the counts so that the second pass can use them as indices. It
2906 would be possible to output the debugging information directly to
2907 the file in pass 2, rather than to build it in memory and then
2908 write it out. Outputting to the file would require a lot of
2909 seeks and small writes, though, and I think this approach is
2910 faster. */
2911 size = (symhdr->cbLine * sizeof (unsigned char)
2912 + symhdr->idnMax * sizeof (struct dnr_ext)
2913 + symhdr->ipdMax * sizeof (struct pdr_ext)
2914 + symhdr->isymMax * sizeof (struct sym_ext)
2915 + symhdr->ioptMax * sizeof (struct opt_ext)
2916 + symhdr->iauxMax * sizeof (union aux_ext)
2917 + symhdr->issMax * sizeof (char)
2918 + symhdr->issExtMax * sizeof (char)
2919 + symhdr->ifdMax * sizeof (struct fdr_ext)
2920 + symhdr->crfd * sizeof (struct rfd_ext)
2921 + symhdr->iextMax * sizeof (struct ext_ext));
2922 raw = (char *) bfd_alloc (abfd, size);
2923 if (raw == (char *) NULL)
2924 {
2925 bfd_error = no_memory;
2926 return false;
2927 }
2928 ecoff_data (abfd)->raw_size = size;
2929 ecoff_data (abfd)->raw_syments = (PTR) raw;
2930
2931 /* Initialize the raw pointers. */
2932#define SET(field, count, type) \
2933 ecoff_data (abfd)->field = (type *) raw; \
2934 raw += symhdr->count * sizeof (type)
2935
2936 SET (line, cbLine, unsigned char);
2937 SET (external_dnr, idnMax, struct dnr_ext);
2938 SET (external_pdr, ipdMax, struct pdr_ext);
2939 SET (external_sym, isymMax, struct sym_ext);
2940 SET (external_opt, ioptMax, struct opt_ext);
2941 SET (external_aux, iauxMax, union aux_ext);
2942 SET (ss, issMax, char);
2943 SET (ssext, issExtMax, char);
2944 SET (external_fdr, ifdMax, struct fdr_ext);
2945 SET (external_rfd, crfd, struct rfd_ext);
2946 SET (external_ext, iextMax, struct ext_ext);
2947#undef SET
2948
2949 /* Reset the counts so the second pass can use them to know how far
2950 it has gotten. */
2951 symhdr->ilineMax = 0;
2952 symhdr->cbLine = 0;
2953 symhdr->idnMax = 0;
2954 symhdr->ipdMax = 0;
2955 symhdr->isymMax = 0;
2956 symhdr->ioptMax = 0;
2957 symhdr->iauxMax = 0;
2958 symhdr->issMax = 0;
2959 symhdr->issExtMax = 0;
2960 symhdr->ifdMax = 0;
2961 symhdr->crfd = 0;
2962 symhdr->iextMax = 0;
2963
2964 /* Do the second pass: accumulate the debugging information. */
2965 ecoff_clear_output_flags (abfd);
2966 for (ipass = 0; ipass < 2; ipass++)
2967 {
2968 for (o = abfd->sections; o != (asection *) NULL; o = o->next)
2969 {
2970 if (((o->flags & SEC_CODE) == 0) != ipass)
2971 continue;
2972 for (p = o->seclets_head;
2973 p != (bfd_seclet_type *) NULL;
2974 p = p->next)
2975 {
2976 if (p->type == bfd_indirect_seclet)
2977 {
5e462ed9 2978 if (ecoff_get_debug (abfd, p, o, relocateable) == false)
8fa0d3a0
ILT
2979 return false;
2980 }
2981 }
2982 }
2983 }
2984
c3fe0c41
ILT
2985 /* Put in the external symbols. */
2986 sym_ptr_ptr = bfd_get_outsymbols (abfd);
2987 if (sym_ptr_ptr != NULL)
2988 {
2989 char *ssext;
2990 struct ext_ext *external_ext;
2991
2992 ssext = ecoff_data (abfd)->ssext;
2993 external_ext = ecoff_data (abfd)->external_ext;
2994 for (; *sym_ptr_ptr != NULL; sym_ptr_ptr++)
2995 {
2996 asymbol *sym_ptr;
2997 EXTR esym;
2998
2999 sym_ptr = *sym_ptr_ptr;
3000
3001 if ((sym_ptr->flags & BSF_DEBUGGING) != 0
3002 || (sym_ptr->flags & BSF_LOCAL) != 0)
3003 continue;
3004
3005 /* The enative pointer can be NULL for a symbol created by
3006 the linker via ecoff_make_empty_symbol. */
3007 if (bfd_asymbol_flavour (sym_ptr) != bfd_target_ecoff_flavour
3008 || (((ecoff_symbol_type *) sym_ptr)->native.enative
3009 == (struct ext_ext *) NULL))
3010 {
3011 esym.jmptbl = 0;
3012 esym.cobol_main = 0;
3013 esym.weakext = 0;
3014 esym.reserved = 0;
3015 esym.ifd = ifdNil;
3016 /* FIXME: we can do better than this for st and sc. */
3017 esym.asym.st = stGlobal;
3018 esym.asym.sc = scAbs;
3019 esym.asym.reserved = 0;
3020 esym.asym.index = indexNil;
3021 }
3022 else
3023 {
3024 ecoff_symbol_type *ecoff_sym_ptr;
3025
3026 ecoff_sym_ptr = (ecoff_symbol_type *) sym_ptr;
3027 if (ecoff_sym_ptr->local)
3028 abort ();
3029 ecoff_swap_ext_in (abfd, ecoff_sym_ptr->native.enative, &esym);
3030
5e462ed9
ILT
3031 /* If we're producing an executable, move common symbols
3032 into bss. */
3033 if (relocateable == false)
3034 {
3035 if (esym.asym.sc == scCommon)
3036 esym.asym.sc = scBss;
3037 else if (esym.asym.sc == scSCommon)
3038 esym.asym.sc = scSBss;
3039 }
3040
c3fe0c41
ILT
3041 /* Adjust the FDR index for the symbol by that used for
3042 the input BFD. */
3043 esym.ifd += ecoff_data (bfd_asymbol_bfd (sym_ptr))->ifdbase;
3044 }
3045
3046 esym.asym.iss = symhdr->issExtMax;
3047
382f2a3d 3048 if (bfd_is_com_section (sym_ptr->section)
c3fe0c41
ILT
3049 || sym_ptr->section == &bfd_und_section)
3050 esym.asym.value = sym_ptr->value;
3051 else
3052 esym.asym.value = (sym_ptr->value
3053 + sym_ptr->section->output_offset
3054 + sym_ptr->section->output_section->vma);
3055
3056 ecoff_swap_ext_out (abfd, &esym, external_ext + symhdr->iextMax);
3057
3058 ecoff_set_sym_index (sym_ptr, symhdr->iextMax);
3059
3060 ++symhdr->iextMax;
3061
3062 strcpy (ssext + symhdr->issExtMax, sym_ptr->name);
3063 symhdr->issExtMax += strlen (sym_ptr->name) + 1;
3064 }
3065 }
3066
3067 /* Adjust the counts so that structures are longword aligned. */
3068 symhdr->cbLine = (symhdr->cbLine + 3) &~ 3;
3069 symhdr->issMax = (symhdr->issMax + 3) &~ 3;
3070 symhdr->issExtMax = (symhdr->issExtMax + 3) &~ 3;
3071
8fa0d3a0
ILT
3072 return true;
3073}
3074\f
3075/* Set the architecture. The only architecture we support here is
3076 mips. We set the architecture anyhow, since many callers ignore
3077 the return value. */
3078
3079static boolean
f6409552
ILT
3080ecoff_set_arch_mach (abfd, arch, machine)
3081 bfd *abfd;
3082 enum bfd_architecture arch;
3083 unsigned long machine;
8fa0d3a0
ILT
3084{
3085 bfd_default_set_arch_mach (abfd, arch, machine);
3086 return arch == bfd_arch_mips;
3087}
3088
5e462ed9
ILT
3089/* Get the size of the section headers. We do not output the .scommon
3090 section which we created in ecoff_mkobject. */
3091
3092static int
3093ecoff_sizeof_headers (abfd, reloc)
3094 bfd *abfd;
3095 boolean reloc;
3096{
3097 return FILHSZ + AOUTSZ + (abfd->section_count - 1) * SCNHSZ;
3098}
3099
8fa0d3a0
ILT
3100/* Calculate the file position for each section, and set
3101 reloc_filepos. */
3102
3103static void
f6409552
ILT
3104ecoff_compute_section_file_positions (abfd)
3105 bfd *abfd;
8fa0d3a0
ILT
3106{
3107 asection *current;
8fa0d3a0
ILT
3108 file_ptr sofar;
3109 file_ptr old_sofar;
c3fe0c41 3110 boolean first_data;
8fa0d3a0 3111
8fa0d3a0 3112 if (bfd_get_start_address (abfd))
c3fe0c41 3113 abfd->flags |= EXEC_P;
8fa0d3a0 3114
5e462ed9 3115 sofar = ecoff_sizeof_headers (abfd, false);
8fa0d3a0 3116
c3fe0c41 3117 first_data = true;
8fa0d3a0
ILT
3118 for (current = abfd->sections;
3119 current != (asection *) NULL;
3120 current = current->next)
3121 {
3122 /* Only deal with sections which have contents */
5e462ed9
ILT
3123 if (! (current->flags & SEC_HAS_CONTENTS)
3124 || strcmp (current->name, SCOMMON) == 0)
8fa0d3a0
ILT
3125 continue;
3126
c3fe0c41
ILT
3127 /* On Ultrix, the data sections in an executable file must be
3128 aligned to a page boundary within the file. This does not
3129 affect the section size, though. FIXME: Does this work for
3130 other platforms? */
3131 if ((abfd->flags & EXEC_P) != 0
3132 && first_data != false
3133 && (current->flags & SEC_CODE) == 0)
3134 {
3135 sofar = (sofar + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3136 first_data = false;
3137 }
3138
8fa0d3a0
ILT
3139 /* Align the sections in the file to the same boundary on
3140 which they are aligned in virtual memory. */
3141 old_sofar = sofar;
3142 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
8fa0d3a0
ILT
3143
3144 current->filepos = sofar;
3145
3146 sofar += current->_raw_size;
3147
3148 /* make sure that this section is of the right size too */
3149 old_sofar = sofar;
3150 sofar = BFD_ALIGN (sofar, 1 << current->alignment_power);
3151 current->_raw_size += sofar - old_sofar;
8fa0d3a0
ILT
3152 }
3153
3154 ecoff_data (abfd)->reloc_filepos = sofar;
3155}
3156
3157/* Set the contents of a section. */
3158
3159static boolean
f6409552
ILT
3160ecoff_set_section_contents (abfd, section, location, offset, count)
3161 bfd *abfd;
3162 asection *section;
3163 PTR location;
3164 file_ptr offset;
3165 bfd_size_type count;
8fa0d3a0
ILT
3166{
3167 if (abfd->output_has_begun == false)
3168 ecoff_compute_section_file_positions (abfd);
3169
3170 bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET);
3171
3172 if (count != 0)
3173 return (bfd_write (location, 1, count, abfd) == count) ? true : false;
3174
3175 return true;
3176}
3177
3178/* Write out an ECOFF file. */
3179
3180static boolean
f6409552
ILT
3181ecoff_write_object_contents (abfd)
3182 bfd *abfd;
8fa0d3a0
ILT
3183{
3184 asection *current;
3185 unsigned int count;
3186 file_ptr scn_base;
3187 file_ptr reloc_base;
3188 file_ptr sym_base;
3189 unsigned long reloc_size;
3190 unsigned long text_size;
3191 unsigned long text_start;
3192 unsigned long data_size;
3193 unsigned long data_start;
3194 unsigned long bss_size;
3195 struct internal_filehdr internal_f;
3196 struct internal_aouthdr internal_a;
c3fe0c41 3197 int i;
8fa0d3a0
ILT
3198
3199 bfd_error = system_call_error;
3200
3201 if(abfd->output_has_begun == false)
3202 ecoff_compute_section_file_positions(abfd);
3203
3204 if (abfd->sections != (asection *) NULL)
3205 scn_base = abfd->sections->filepos;
3206 else
3207 scn_base = 0;
3208 reloc_base = ecoff_data (abfd)->reloc_filepos;
3209
3210 count = 1;
3211 reloc_size = 0;
3212 for (current = abfd->sections;
3213 current != (asection *)NULL;
3214 current = current->next)
3215 {
5e462ed9
ILT
3216 if (strcmp (current->name, SCOMMON) == 0)
3217 continue;
8fa0d3a0
ILT
3218 current->target_index = count;
3219 ++count;
3220 if (current->reloc_count != 0)
3221 {
3222 bfd_size_type relsize;
3223
3224 current->rel_filepos = reloc_base;
3225 relsize = current->reloc_count * RELSZ;
3226 reloc_size += relsize;
3227 reloc_base += relsize;
3228 }
3229 else
3230 current->rel_filepos = 0;
3231 }
3232
3233 sym_base = reloc_base + reloc_size;
c3fe0c41
ILT
3234
3235 /* At least on Ultrix, the symbol table of an executable file must
3236 be aligned to a page boundary. FIXME: Is this true on other
3237 platforms? */
3238 if ((abfd->flags & EXEC_P) != 0)
3239 sym_base = (sym_base + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3240
8fa0d3a0
ILT
3241 ecoff_data (abfd)->sym_filepos = sym_base;
3242
5e462ed9 3243 text_size = ecoff_sizeof_headers (abfd, false);
8fa0d3a0
ILT
3244 text_start = 0;
3245 data_size = 0;
3246 data_start = 0;
3247 bss_size = 0;
3248
3249 /* Write section headers to the file. */
3250
3251 internal_f.f_nscns = 0;
c3fe0c41 3252 if (bfd_seek (abfd, (file_ptr) (FILHSZ + AOUTSZ), SEEK_SET) != 0)
8fa0d3a0
ILT
3253 return false;
3254 for (current = abfd->sections;
3255 current != (asection *) NULL;
3256 current = current->next)
3257 {
3258 struct internal_scnhdr section;
3259 bfd_vma vma;
3260
5e462ed9
ILT
3261 if (strcmp (current->name, SCOMMON) == 0)
3262 {
3263 BFD_ASSERT (bfd_get_section_size_before_reloc (current) == 0
3264 && current->reloc_count == 0);
3265 continue;
3266 }
3267
8fa0d3a0
ILT
3268 ++internal_f.f_nscns;
3269
3270 strncpy (section.s_name, current->name, sizeof section.s_name);
3271
3272 /* FIXME: is this correct for shared libraries? I think it is
3273 but I have no platform to check. Ian Lance Taylor. */
3274 vma = bfd_get_section_vma (abfd, current);
3275 if (strcmp (current->name, _LIB) == 0)
3276 section.s_vaddr = 0;
3277 else
3278 section.s_vaddr = vma;
3279
3280 section.s_paddr = vma;
3281 section.s_size = bfd_get_section_size_before_reloc (current);
3282
3283 /* If this section has no size or is unloadable then the scnptr
3284 will be 0 too. */
3285 if (current->_raw_size == 0
3286 || (current->flags & (SEC_LOAD | SEC_HAS_CONTENTS)) == 0)
3287 section.s_scnptr = 0;
3288 else
3289 section.s_scnptr = current->filepos;
3290 section.s_relptr = current->rel_filepos;
3291
3292 /* FIXME: the lnnoptr of the .sbss or .sdata section of an
3293 object file produced by the assembler is supposed to point to
3294 information about how much room is required by objects of
3295 various different sizes. I think this only matters if we
3296 want the linker to compute the best size to use, or
3297 something. I don't know what happens if the information is
3298 not present. */
3299 section.s_lnnoptr = 0;
3300
3301 section.s_nreloc = current->reloc_count;
3302 section.s_nlnno = 0;
f6409552
ILT
3303 section.s_flags = ecoff_sec_to_styp_flags (current->name,
3304 current->flags);
8fa0d3a0
ILT
3305
3306 {
3307 SCNHDR buff;
3308
3309 ecoff_swap_scnhdr_out (abfd, (PTR) &section, (PTR) &buff);
3310 if (bfd_write ((PTR) &buff, 1, SCNHSZ, abfd) != SCNHSZ)
3311 return false;
3312 }
3313
c3fe0c41 3314 if ((section.s_flags & STYP_TEXT) != 0)
8fa0d3a0
ILT
3315 {
3316 text_size += bfd_get_section_size_before_reloc (current);
3317 if (text_start == 0 || text_start > vma)
3318 text_start = vma;
3319 }
c3fe0c41
ILT
3320 else if ((section.s_flags & STYP_RDATA) != 0
3321 || (section.s_flags & STYP_DATA) != 0
3322 || (section.s_flags & STYP_LIT8) != 0
3323 || (section.s_flags & STYP_LIT4) != 0
8fa0d3a0
ILT
3324 || (section.s_flags & STYP_SDATA) != 0)
3325 {
3326 data_size += bfd_get_section_size_before_reloc (current);
3327 if (data_start == 0 || data_start > vma)
3328 data_start = vma;
3329 }
3330 else if ((section.s_flags & STYP_BSS) != 0
3331 || (section.s_flags & STYP_SBSS) != 0)
3332 bss_size += bfd_get_section_size_before_reloc (current);
3333 }
3334
3335 /* Set up the file header. */
3336
6d18a99d
ILT
3337 if (abfd->xvec->header_byteorder_big_p != false)
3338 internal_f.f_magic = MIPS_MAGIC_BIG;
3339 else
3340 internal_f.f_magic = MIPS_MAGIC_LITTLE;
8fa0d3a0
ILT
3341
3342 /*
3343 We will NOT put a fucking timestamp in the header here. Every time you
3344 put it back, I will come in and take it out again. I'm sorry. This
3345 field does not belong here. We fill it with a 0 so it compares the
3346 same but is not a reasonable time. -- gnu@cygnus.com
3347 */
3348 internal_f.f_timdat = 0;
3349
c3fe0c41
ILT
3350 if (bfd_get_symcount (abfd) != 0)
3351 {
3352 /* The ECOFF f_nsyms field is not actually the number of
3353 symbols, it's the size of symbolic information header. */
3354 internal_f.f_nsyms = sizeof (struct hdr_ext);
3355 internal_f.f_symptr = sym_base;
3356 }
8fa0d3a0 3357 else
c3fe0c41
ILT
3358 {
3359 internal_f.f_nsyms = 0;
3360 internal_f.f_symptr = 0;
3361 }
8fa0d3a0 3362
c3fe0c41 3363 internal_f.f_opthdr = AOUTSZ;
8fa0d3a0 3364
c3fe0c41 3365 internal_f.f_flags = F_LNNO;
8fa0d3a0
ILT
3366 if (reloc_size == 0)
3367 internal_f.f_flags |= F_RELFLG;
3368 if (bfd_get_symcount (abfd) == 0)
3369 internal_f.f_flags |= F_LSYMS;
3370 if (abfd->flags & EXEC_P)
3371 internal_f.f_flags |= F_EXEC;
3372
3373 if (! abfd->xvec->byteorder_big_p)
3374 internal_f.f_flags |= F_AR32WR;
3375 else
3376 internal_f.f_flags |= F_AR32W;
3377
c3fe0c41
ILT
3378 /* Set up the ``optional'' header. */
3379 internal_a.magic = ZMAGIC;
3380
f6409552
ILT
3381 /* FIXME: This is what Ultrix puts in, and it makes the Ultrix
3382 linker happy. But, is it right? */
3383 internal_a.vstamp = 0x20a;
c3fe0c41
ILT
3384
3385 /* At least on Ultrix, these have to be rounded to page boundaries.
3386 FIXME: Is this true on other platforms? */
3387 internal_a.tsize = (text_size + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3388 internal_a.text_start = text_start &~ (PAGE_SIZE - 1);
3389 internal_a.dsize = (data_size + PAGE_SIZE - 1) &~ (PAGE_SIZE - 1);
3390 internal_a.data_start = data_start &~ (PAGE_SIZE - 1);
3391
3392 /* On Ultrix, the initial portions of the .sbss and .bss segments
3393 are at the end of the data section. The bsize field in the
3394 optional header records how many bss bytes are required beyond
3395 those in the data section. The value is not rounded to a page
3396 boundary. */
3397 if (bss_size < internal_a.dsize - data_size)
3398 bss_size = 0;
3399 else
3400 bss_size -= internal_a.dsize - data_size;
3401 internal_a.bsize = bss_size;
3402 internal_a.bss_start = internal_a.data_start + internal_a.dsize;
8fa0d3a0 3403
c3fe0c41 3404 internal_a.entry = bfd_get_start_address (abfd);
8fa0d3a0 3405
c3fe0c41 3406 internal_a.gp_value = ecoff_data (abfd)->gp;
8fa0d3a0 3407
c3fe0c41 3408 internal_a.gprmask = ecoff_data (abfd)->gprmask;
5e462ed9 3409 for (i = 0; i < 4; i++)
c3fe0c41 3410 internal_a.cprmask[i] = ecoff_data (abfd)->cprmask[i];
8fa0d3a0
ILT
3411
3412 /* Write out the file header and the optional header. */
3413
3414 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3415 return false;
3416
3417 {
3418 FILHDR buff;
3419 ecoff_swap_filehdr_out (abfd, (PTR) &internal_f, (PTR) &buff);
3420 if (bfd_write ((PTR) &buff, 1, FILHSZ, abfd) != FILHSZ)
3421 return false;
3422 }
3423
c3fe0c41
ILT
3424 {
3425 AOUTHDR buff;
3426
3427 ecoff_swap_aouthdr_out (abfd, (PTR) &internal_a, (PTR) &buff);
3428 if (bfd_write ((PTR) &buff, 1, AOUTSZ, abfd) != AOUTSZ)
3429 return false;
3430 }
3431
3432 /* Write out the relocs. */
3433 for (current = abfd->sections;
3434 current != (asection *) NULL;
3435 current = current->next)
8fa0d3a0 3436 {
c3fe0c41
ILT
3437 RELOC *buff;
3438 arelent **reloc_ptr_ptr;
3439 arelent **reloc_end;
3440 RELOC *out_ptr;
3441
3442 if (current->reloc_count == 0)
3443 continue;
8fa0d3a0 3444
c3fe0c41
ILT
3445 buff = (RELOC *) bfd_alloc (abfd, current->reloc_count * RELSZ);
3446 if (buff == (RELOC *) NULL)
3447 {
3448 bfd_error = no_memory;
3449 return false;
3450 }
3451
3452 reloc_ptr_ptr = current->orelocation;
3453 reloc_end = reloc_ptr_ptr + current->reloc_count;
3454 out_ptr = buff;
3455 for (; reloc_ptr_ptr < reloc_end; reloc_ptr_ptr++, out_ptr++)
3456 {
3457 arelent *reloc;
3458 asymbol *sym;
3459 struct internal_reloc in;
3460
3461 memset (&in, 0, sizeof in);
3462
3463 reloc = *reloc_ptr_ptr;
3464 sym = *reloc->sym_ptr_ptr;
3465
3466 in.r_vaddr = reloc->address + bfd_get_section_vma (abfd, current);
3467 in.r_type = reloc->howto->type;
3468 if ((sym->flags & BSF_SECTION_SYM) == 0)
3469 {
3470 in.r_symndx = ecoff_get_sym_index (*reloc->sym_ptr_ptr);
3471 in.r_extern = 1;
3472 }
3473 else
3474 {
3475 CONST char *name;
3476
3477 name = bfd_get_section_name (abfd, bfd_get_section (sym));
3478 if (strcmp (name, ".text") == 0)
3479 in.r_symndx = RELOC_SECTION_TEXT;
3480 else if (strcmp (name, ".rdata") == 0)
3481 in.r_symndx = RELOC_SECTION_RDATA;
3482 else if (strcmp (name, ".data") == 0)
3483 in.r_symndx = RELOC_SECTION_DATA;
3484 else if (strcmp (name, ".sdata") == 0)
3485 in.r_symndx = RELOC_SECTION_SDATA;
3486 else if (strcmp (name, ".sbss") == 0)
3487 in.r_symndx = RELOC_SECTION_SBSS;
3488 else if (strcmp (name, ".bss") == 0)
3489 in.r_symndx = RELOC_SECTION_BSS;
3490 else if (strcmp (name, ".init") == 0)
3491 in.r_symndx = RELOC_SECTION_INIT;
3492 else if (strcmp (name, ".lit8") == 0)
3493 in.r_symndx = RELOC_SECTION_LIT8;
3494 else if (strcmp (name, ".lit4") == 0)
3495 in.r_symndx = RELOC_SECTION_LIT4;
3496 else
3497 abort ();
3498 in.r_extern = 0;
3499 }
3500
3501 ecoff_swap_reloc_out (abfd, (PTR) &in, (PTR) out_ptr);
3502 }
3503
3504 if (bfd_seek (abfd, current->rel_filepos, SEEK_SET) != 0)
8fa0d3a0 3505 return false;
c3fe0c41
ILT
3506 if (bfd_write ((PTR) buff, RELSZ, current->reloc_count, abfd)
3507 != RELSZ * current->reloc_count)
3508 return false;
3509 bfd_release (abfd, (PTR) buff);
8fa0d3a0
ILT
3510 }
3511
8fa0d3a0
ILT
3512 /* Write out the symbolic debugging information. */
3513 if (bfd_get_symcount (abfd) > 0)
3514 {
c3fe0c41
ILT
3515 HDRR *symhdr;
3516 unsigned long sym_offset;
8fa0d3a0
ILT
3517 struct hdr_ext buff;
3518
c3fe0c41
ILT
3519 /* Set up the offsets in the symbolic header. */
3520 symhdr = &ecoff_data (abfd)->symbolic_header;
3521 sym_offset = ecoff_data (abfd)->sym_filepos + sizeof (struct hdr_ext);
3522
3523#define SET(offset, size, ptr) \
3524 if (symhdr->size == 0) \
3525 symhdr->offset = 0; \
3526 else \
3527 symhdr->offset = (((char *) ecoff_data (abfd)->ptr \
3528 - (char *) ecoff_data (abfd)->raw_syments) \
3529 + sym_offset);
3530
3531 SET (cbLineOffset, cbLine, line);
3532 SET (cbDnOffset, idnMax, external_dnr);
3533 SET (cbPdOffset, ipdMax, external_pdr);
3534 SET (cbSymOffset, isymMax, external_sym);
3535 SET (cbOptOffset, ioptMax, external_opt);
3536 SET (cbAuxOffset, iauxMax, external_aux);
3537 SET (cbSsOffset, issMax, ss);
3538 SET (cbSsExtOffset, issExtMax, ssext);
3539 SET (cbFdOffset, ifdMax, external_fdr);
3540 SET (cbRfdOffset, crfd, external_rfd);
3541 SET (cbExtOffset, iextMax, external_ext);
3542#undef SET
3543
3544 if (bfd_seek (abfd, (file_ptr) ecoff_data (abfd)->sym_filepos,
3545 SEEK_SET) != 0)
3546 return false;
8fa0d3a0
ILT
3547 ecoff_swap_hdr_out (abfd, &ecoff_data (abfd)->symbolic_header, &buff);
3548 if (bfd_write ((PTR) &buff, 1, sizeof buff, abfd) != sizeof buff)
3549 return false;
3550 if (bfd_write ((PTR) ecoff_data (abfd)->raw_syments, 1,
3551 ecoff_data (abfd)->raw_size, abfd)
3552 != ecoff_data (abfd)->raw_size)
3553 return false;
3554 }
3555
3556 return true;
3557}
3558\f
c3fe0c41
ILT
3559/* Archive handling. ECOFF uses what appears to be a unique type of
3560 archive header (which I call an armap). The byte ordering of the
3561 armap and the contents are encoded in the name of the armap itself.
3562 At least for now, we only support archives with the same byte
3563 ordering in the armap and the contents.
3564
3565 The first four bytes in the armap are the number of symbol
23ba15b7 3566 definitions. This is always a power of two.
c3fe0c41
ILT
3567
3568 This is followed by the symbol definitions. Each symbol definition
3569 occupies 8 bytes. The first four bytes are the offset from the
3570 start of the armap strings to the null-terminated string naming
3571 this symbol. The second four bytes are the file offset to the
3572 archive member which defines this symbol. If the second four bytes
3573 are 0, then this is not actually a symbol definition, and it should
3574 be ignored.
3575
23ba15b7
ILT
3576 The symbols are hashed into the armap with a closed hashing scheme.
3577 See the functions below for the details of the algorithm.
3578
3579 We could use the hash table when looking up symbols in a library.
3580 This would require a new BFD target entry point to replace the
3581 bfd_get_next_mapent function used by the linker.
3582
c3fe0c41
ILT
3583 After the symbol definitions comes four bytes holding the size of
3584 the string table, followed by the string table itself. */
3585
3586/* The name of an archive headers looks like this:
23ba15b7
ILT
3587 __________E[BL]E[BL]_ (with a trailing space).
3588 The trailing space is changed to an X if the archive is changed to
3589 indicate that the armap is out of date. */
c3fe0c41
ILT
3590
3591#define ARMAP_BIG_ENDIAN 'B'
3592#define ARMAP_LITTLE_ENDIAN 'L'
3593#define ARMAP_MARKER 'E'
3594#define ARMAP_START "__________"
3595#define ARMAP_HEADER_MARKER_INDEX 10
3596#define ARMAP_HEADER_ENDIAN_INDEX 11
3597#define ARMAP_OBJECT_MARKER_INDEX 12
3598#define ARMAP_OBJECT_ENDIAN_INDEX 13
3599#define ARMAP_END_INDEX 14
3600#define ARMAP_END "_ "
3601
23ba15b7
ILT
3602/* This is a magic number used in the hashing algorithm. */
3603#define ARMAP_HASH_MAGIC 0x9dd68ab5
3604
3605/* This returns the hash value to use for a string. It also sets
3606 *REHASH to the rehash adjustment if the first slot is taken. SIZE
3607 is the number of entries in the hash table, and HLOG is the log
3608 base 2 of SIZE. */
3609
3610static unsigned int
3611ecoff_armap_hash (s, rehash, size, hlog)
3612 CONST char *s;
3613 unsigned int *rehash;
3614 unsigned int size;
3615 unsigned int hlog;
3616{
3617 unsigned int hash;
3618
3619 hash = *s++;
3620 while (*s != '\0')
3621 hash = ((hash >> 27) | (hash << 5)) + *s++;
3622 hash *= ARMAP_HASH_MAGIC;
3623 *rehash = (hash & (size - 1)) | 1;
3624 return hash >> (32 - hlog);
3625}
3626
c3fe0c41
ILT
3627/* Read in the armap. */
3628
3629static boolean
f6409552
ILT
3630ecoff_slurp_armap (abfd)
3631 bfd *abfd;
c3fe0c41
ILT
3632{
3633 char nextname[17];
3634 unsigned int i;
3635 struct areltdata *mapdata;
3636 bfd_size_type parsed_size;
3637 char *raw_armap;
3638 struct artdata *ardata;
3639 unsigned int count;
3640 char *raw_ptr;
3641 struct symdef *symdef_ptr;
3642 char *stringbase;
3643
3644 /* Get the name of the first element. */
3645 i = bfd_read ((PTR) nextname, 1, 16, abfd);
3646 if (i == 0)
3647 return true;
3648 if (i != 16)
3649 return false;
3650
3651 bfd_seek (abfd, (file_ptr) -16, SEEK_CUR);
3652
3653 /* See if the first element is an armap. */
3654 if (strncmp (nextname, ARMAP_START, sizeof ARMAP_START - 1) != 0
3655 || nextname[ARMAP_HEADER_MARKER_INDEX] != ARMAP_MARKER
3656 || (nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3657 && nextname[ARMAP_HEADER_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3658 || nextname[ARMAP_OBJECT_MARKER_INDEX] != ARMAP_MARKER
3659 || (nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_BIG_ENDIAN
3660 && nextname[ARMAP_OBJECT_ENDIAN_INDEX] != ARMAP_LITTLE_ENDIAN)
3661 || strncmp (nextname + ARMAP_END_INDEX,
3662 ARMAP_END, sizeof ARMAP_END - 1) != 0)
3663 {
3664 bfd_has_map (abfd) = false;
3665 return true;
3666 }
3667
3668 /* Make sure we have the right byte ordering. */
3669 if (((nextname[ARMAP_HEADER_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
3670 ^ (abfd->xvec->header_byteorder_big_p != false))
6d18a99d 3671 || ((nextname[ARMAP_OBJECT_ENDIAN_INDEX] == ARMAP_BIG_ENDIAN)
c3fe0c41
ILT
3672 ^ (abfd->xvec->byteorder_big_p != false)))
3673 {
3674 bfd_error = wrong_format;
3675 return false;
3676 }
3677
3678 /* Read in the armap. */
3679 ardata = bfd_ardata (abfd);
3680 mapdata = snarf_ar_hdr (abfd);
3681 if (mapdata == (struct areltdata *) NULL)
3682 return false;
3683 parsed_size = mapdata->parsed_size;
3684 bfd_release (abfd, (PTR) mapdata);
3685
3686 raw_armap = (char *) bfd_alloc (abfd, parsed_size);
3687 if (raw_armap == (char *) NULL)
3688 {
3689 bfd_error = no_memory;
3690 return false;
3691 }
3692
3693 if (bfd_read ((PTR) raw_armap, 1, parsed_size, abfd) != parsed_size)
3694 {
3695 bfd_error = malformed_archive;
3696 bfd_release (abfd, (PTR) raw_armap);
3697 return false;
3698 }
3699
3700 count = bfd_h_get_32 (abfd, (PTR) raw_armap);
3701
3702 ardata->symdef_count = 0;
3703 ardata->cache = (struct ar_cache *) NULL;
3704
3705 /* Hack: overlay the symdefs on top of the raw archive data. This
3706 is the way do_slurp_bsd_armap works. */
3707 raw_ptr = raw_armap + LONG_SIZE;
3708 symdef_ptr = (struct symdef *) raw_ptr;
3709 ardata->symdefs = (carsym *) symdef_ptr;
3710 stringbase = raw_ptr + count * (2 * LONG_SIZE) + LONG_SIZE;
3711
23ba15b7
ILT
3712#ifdef CHECK_ARMAP_HASH
3713 {
3714 unsigned int hlog;
3715
3716 /* Double check that I have the hashing algorithm right by making
3717 sure that every symbol can be looked up successfully. */
3718 hlog = 0;
3719 for (i = 1; i < count; i <<= 1)
3720 hlog++;
3721 BFD_ASSERT (i == count);
3722
3723 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3724 {
3725 unsigned int name_offset, file_offset;
3726 unsigned int hash, rehash, srch;
3727
3728 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3729 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3730 if (file_offset == 0)
3731 continue;
3732 hash = ecoff_armap_hash (stringbase + name_offset, &rehash, count,
3733 hlog);
3734 if (hash == i)
3735 continue;
3736
3737 /* See if we can rehash to this location. */
3738 for (srch = (hash + rehash) & (count - 1);
3739 srch != hash && srch != i;
3740 srch = (srch + rehash) & (count - 1))
3741 BFD_ASSERT (bfd_h_get_32 (abfd,
3742 (PTR) (raw_armap
3743 + LONG_SIZE
3744 + (srch * 2 * LONG_SIZE)
3745 + LONG_SIZE))
3746 != 0);
3747 BFD_ASSERT (srch == i);
3748 }
3749 }
3750
3751 raw_ptr = raw_armap + LONG_SIZE;
3752#endif /* CHECK_ARMAP_HASH */
3753
c3fe0c41
ILT
3754 for (i = 0; i < count; i++, raw_ptr += 2 * LONG_SIZE)
3755 {
23ba15b7 3756 unsigned int name_offset, file_offset;
c3fe0c41
ILT
3757
3758 name_offset = bfd_h_get_32 (abfd, (PTR) raw_ptr);
3759 file_offset = bfd_h_get_32 (abfd, (PTR) (raw_ptr + LONG_SIZE));
3760 if (file_offset == 0)
3761 continue;
3762 symdef_ptr->s.name = stringbase + name_offset;
3763 symdef_ptr->file_offset = file_offset;
3764 ++symdef_ptr;
3765 ++ardata->symdef_count;
3766 }
3767
3768 ardata->first_file_filepos = bfd_tell (abfd);
3769 /* Pad to an even boundary. */
3770 ardata->first_file_filepos += ardata->first_file_filepos % 2;
3771
3772 bfd_has_map (abfd) = true;
3773
3774 return true;
3775}
3776
3777/* Write out an armap. */
3778
3779static boolean
f6409552
ILT
3780ecoff_write_armap (abfd, elength, map, orl_count, stridx)
3781 bfd *abfd;
3782 unsigned int elength;
3783 struct orl *map;
3784 unsigned int orl_count;
3785 int stridx;
c3fe0c41 3786{
23ba15b7 3787 unsigned int hashsize, hashlog;
c3fe0c41
ILT
3788 unsigned int symdefsize;
3789 int padit;
3790 unsigned int stringsize;
3791 unsigned int mapsize;
3792 file_ptr firstreal;
3793 struct ar_hdr hdr;
3794 struct stat statbuf;
3795 unsigned int i;
23ba15b7
ILT
3796 bfd_byte temp[LONG_SIZE];
3797 bfd_byte *hashtable;
c3fe0c41
ILT
3798 bfd *current;
3799 bfd *last_elt;
3800
23ba15b7
ILT
3801 /* Ultrix appears to use as a hash table size the least power of two
3802 greater than twice the number of entries. */
3803 for (hashlog = 0; (1 << hashlog) <= 2 * orl_count; hashlog++)
3804 ;
3805 hashsize = 1 << hashlog;
3806
3807 symdefsize = hashsize * 2 * LONG_SIZE;
c3fe0c41
ILT
3808 padit = stridx % 2;
3809 stringsize = stridx + padit;
3810
3811 /* Include 8 bytes to store symdefsize and stringsize in output. */
23ba15b7 3812 mapsize = LONG_SIZE + symdefsize + stringsize + LONG_SIZE;
c3fe0c41
ILT
3813
3814 firstreal = SARMAG + sizeof (struct ar_hdr) + mapsize + elength;
3815
3816 memset ((PTR) &hdr, 0, sizeof hdr);
3817
3818 /* Work out the ECOFF armap name. */
3819 strcpy (hdr.ar_name, ARMAP_START);
3820 hdr.ar_name[ARMAP_HEADER_MARKER_INDEX] = ARMAP_MARKER;
3821 hdr.ar_name[ARMAP_HEADER_ENDIAN_INDEX] =
3822 (abfd->xvec->header_byteorder_big_p
3823 ? ARMAP_BIG_ENDIAN
3824 : ARMAP_LITTLE_ENDIAN);
3825 hdr.ar_name[ARMAP_OBJECT_MARKER_INDEX] = ARMAP_MARKER;
3826 hdr.ar_name[ARMAP_OBJECT_ENDIAN_INDEX] =
3827 abfd->xvec->byteorder_big_p ? ARMAP_BIG_ENDIAN : ARMAP_LITTLE_ENDIAN;
3828 memcpy (hdr.ar_name + ARMAP_END_INDEX, ARMAP_END, sizeof ARMAP_END - 1);
3829
3830 /* Write the timestamp of the archive header to be just a little bit
3831 later than the timestamp of the file, otherwise the linker will
23ba15b7
ILT
3832 complain that the index is out of date. Actually, the Ultrix
3833 linker just checks the archive name; the GNU linker may check the
3834 date. */
f6409552 3835 stat (abfd->filename, &statbuf);
c3fe0c41
ILT
3836 sprintf (hdr.ar_date, "%ld", (long) (statbuf.st_mtime + 60));
3837
3838 /* The DECstation uses zeroes for the uid, gid and mode of the
3839 armap. */
3840 hdr.ar_uid[0] = '0';
3841 hdr.ar_gid[0] = '0';
3842 hdr.ar_mode[0] = '0';
3843
3844 sprintf (hdr.ar_size, "%-10d", (int) mapsize);
3845
3846 hdr.ar_fmag[0] = '`';
3847 hdr.ar_fmag[1] = '\n';
3848
3849 /* Turn all null bytes in the header into spaces. */
3850 for (i = 0; i < sizeof (struct ar_hdr); i++)
3851 if (((char *)(&hdr))[i] == '\0')
3852 (((char *)(&hdr))[i]) = ' ';
3853
23ba15b7
ILT
3854 if (bfd_write ((PTR) &hdr, 1, sizeof (struct ar_hdr), abfd)
3855 != sizeof (struct ar_hdr))
3856 return false;
c3fe0c41 3857
23ba15b7
ILT
3858 bfd_h_put_32 (abfd, hashsize, temp);
3859 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
3860 return false;
c3fe0c41 3861
23ba15b7
ILT
3862 hashtable = (bfd_byte *) bfd_zalloc (abfd, symdefsize);
3863
c3fe0c41
ILT
3864 current = abfd->archive_head;
3865 last_elt = current;
3866 for (i = 0; i < orl_count; i++)
3867 {
23ba15b7 3868 unsigned int hash, rehash;
c3fe0c41
ILT
3869
3870 /* Advance firstreal to the file position of this archive
3871 element. */
3872 if (((bfd *) map[i].pos) != last_elt)
3873 {
3874 do
3875 {
3876 firstreal += arelt_size (current) + sizeof (struct ar_hdr);
3877 firstreal += firstreal % 2;
3878 current = current->next;
3879 }
3880 while (current != (bfd *) map[i].pos);
3881 }
3882
3883 last_elt = current;
3884
23ba15b7
ILT
3885 hash = ecoff_armap_hash (*map[i].name, &rehash, hashsize, hashlog);
3886 if (bfd_h_get_32 (abfd, (PTR) (hashtable
3887 + (hash * 2 * LONG_SIZE)
3888 + LONG_SIZE))
3889 != 0)
3890 {
3891 unsigned int srch;
3892
3893 /* The desired slot is already taken. */
3894 for (srch = (hash + rehash) & (hashsize - 1);
3895 srch != hash;
3896 srch = (srch + rehash) & (hashsize - 1))
3897 if (bfd_h_get_32 (abfd, (PTR) (hashtable
3898 + (srch * 2 * LONG_SIZE)
3899 + LONG_SIZE))
3900 == 0)
3901 break;
3902
3903 BFD_ASSERT (srch != hash);
3904
3905 hash = srch;
3906 }
3907
3908 bfd_h_put_32 (abfd, map[i].namidx,
3909 (PTR) (hashtable + hash * 2 * LONG_SIZE));
3910 bfd_h_put_32 (abfd, firstreal,
3911 (PTR) (hashtable + hash * 2 * LONG_SIZE + LONG_SIZE));
c3fe0c41
ILT
3912 }
3913
23ba15b7
ILT
3914 if (bfd_write (hashtable, 1, symdefsize, abfd) != symdefsize)
3915 return false;
3916
3917 bfd_release (abfd, hashtable);
3918
c3fe0c41
ILT
3919 /* Now write the strings. */
3920 bfd_h_put_32 (abfd, stringsize, temp);
23ba15b7
ILT
3921 if (bfd_write (temp, 1, LONG_SIZE, abfd) != LONG_SIZE)
3922 return false;
c3fe0c41 3923 for (i = 0; i < orl_count; i++)
23ba15b7
ILT
3924 {
3925 bfd_size_type len;
3926
3927 len = strlen (*map[i].name) + 1;
3928 if (bfd_write ((PTR) (*map[i].name), 1, len, abfd) != len)
3929 return false;
3930 }
c3fe0c41
ILT
3931
3932 /* The spec sez this should be a newline. But in order to be
3933 bug-compatible for DECstation ar we use a null. */
3934 if (padit)
23ba15b7
ILT
3935 {
3936 if (bfd_write ("\0", 1, 1, abfd) != 1)
3937 return false;
3938 }
c3fe0c41
ILT
3939
3940 return true;
3941}
3942
3943/* We just use the generic extended name support. This is a GNU
3944 extension. */
3945#define ecoff_slurp_extended_name_table _bfd_slurp_extended_name_table
3946
3947/* See whether this BFD is an archive. If it is, read in the armap
3948 and the extended name table. */
3949
3950static bfd_target *
f6409552
ILT
3951ecoff_archive_p (abfd)
3952 bfd *abfd;
c3fe0c41
ILT
3953{
3954 char armag[SARMAG + 1];
3955
3956 if (bfd_read ((PTR) armag, 1, SARMAG, abfd) != SARMAG
3957 || strncmp (armag, ARMAG, SARMAG) != 0)
3958 {
3959 bfd_error = wrong_format;
3960 return (bfd_target *) NULL;
3961 }
3962
3963 /* We are setting bfd_ardata(abfd) here, but since bfd_ardata
3964 involves a cast, we can't do it as the left operand of
3965 assignment. */
3966 abfd->tdata.aout_ar_data =
3967 (struct artdata *) bfd_zalloc (abfd, sizeof (struct artdata));
3968
3969 if (bfd_ardata (abfd) == (struct artdata *) NULL)
3970 {
3971 bfd_error = no_memory;
3972 return (bfd_target *) NULL;
3973 }
3974
3975 bfd_ardata (abfd)->first_file_filepos = SARMAG;
3976
3977 if (ecoff_slurp_armap (abfd) == false
3978 || ecoff_slurp_extended_name_table (abfd) == false)
3979 {
3980 bfd_release (abfd, bfd_ardata (abfd));
3981 abfd->tdata.aout_ar_data = (struct artdata *) NULL;
3982 return (bfd_target *) NULL;
3983 }
3984
3985 return abfd->xvec;
3986}
3987\f
f6409552
ILT
3988/* This is the COFF backend structure. The backend_data field of the
3989 bfd_target structure is set to this. The section reading code in
3990 coffgen.c uses this structure. */
3991
515c4292
ILT
3992static CONST bfd_coff_backend_data bfd_ecoff_std_swap_table = {
3993 (void (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_in */
3994 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_in */
3995 (void (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_in */
3996 (unsigned (*) PARAMS ((bfd *,PTR,int,int,PTR))) bfd_void, /* aux_out */
3997 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* sym_out */
3998 (unsigned (*) PARAMS ((bfd *,PTR,PTR))) bfd_void, /* lineno_out */
3999 ecoff_swap_reloc_out, ecoff_swap_filehdr_out, ecoff_swap_aouthdr_out,
4000 ecoff_swap_scnhdr_out,
4001 FILHSZ, AOUTSZ, SCNHSZ, 0, 0, 0, true,
4002 ecoff_swap_filehdr_in, ecoff_swap_aouthdr_in, ecoff_swap_scnhdr_in,
4003 ecoff_bad_format_hook, ecoff_set_arch_mach_hook, ecoff_mkobject_hook,
f6409552 4004 ecoff_styp_to_sec_flags, ecoff_make_section_hook, ecoff_set_alignment_hook,
515c4292
ILT
4005 ecoff_slurp_symbol_table
4006};
4007
515c4292
ILT
4008/* get_lineno could be written for ECOFF, but it would currently only
4009 be useful for linking ECOFF and COFF files together, which doesn't
4010 seem too likely. */
8fa0d3a0
ILT
4011#define ecoff_get_lineno \
4012 ((alent *(*) PARAMS ((bfd *, asymbol *))) bfd_nullvoidptr)
515c4292 4013
f6409552
ILT
4014/* These bfd_target functions are defined in other files. */
4015
515c4292
ILT
4016#define ecoff_core_file_failing_command _bfd_dummy_core_file_failing_command
4017#define ecoff_core_file_failing_signal _bfd_dummy_core_file_failing_signal
f6409552
ILT
4018#define ecoff_core_file_matches_executable_p \
4019 _bfd_dummy_core_file_matches_executable_p
515c4292
ILT
4020#define ecoff_truncate_arname bfd_dont_truncate_arname
4021#define ecoff_openr_next_archived_file bfd_generic_openr_next_archived_file
4022#define ecoff_generic_stat_arch_elt bfd_generic_stat_arch_elt
4023#define ecoff_get_section_contents bfd_generic_get_section_contents
8fa0d3a0 4024#define ecoff_get_reloc_upper_bound coff_get_reloc_upper_bound
515c4292 4025#define ecoff_close_and_cleanup bfd_generic_close_and_cleanup
515c4292 4026#define ecoff_bfd_debug_info_start bfd_void
8fa0d3a0 4027#define ecoff_bfd_debug_info_end bfd_void
515c4292 4028#define ecoff_bfd_debug_info_accumulate \
8fa0d3a0 4029 ((void (*) PARAMS ((bfd *, struct sec *))) bfd_void)
f6409552
ILT
4030#define ecoff_bfd_get_relocated_section_contents \
4031 bfd_generic_get_relocated_section_contents
515c4292 4032#define ecoff_bfd_relax_section bfd_generic_relax_section
c9301d7b 4033
1327fb29 4034bfd_target ecoff_little_vec =
294eaca4
SC
4035{
4036 "ecoff-littlemips", /* name */
515c4292 4037 bfd_target_ecoff_flavour,
294eaca4
SC
4038 false, /* data byte order is little */
4039 false, /* header byte order is little */
4040
4041 (HAS_RELOC | EXEC_P | /* object flags */
4042 HAS_LINENO | HAS_DEBUG |
4043 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
4044
4045 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect
4046 flags */
4047 0, /* leading underscore */
4048 '/', /* ar_pad_char */
4049 15, /* ar_max_namelen */
4050 3, /* minimum alignment power */
4051 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* data */
4052 _do_getl64, _do_putl64, _do_getl32, _do_putl32, _do_getl16, _do_putl16, /* hdrs */
4053
515c4292 4054 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
c3fe0c41
ILT
4055 ecoff_archive_p, _bfd_dummy_target},
4056 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
4057 bfd_false},
4058 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
4059 _bfd_write_archive_contents, bfd_false},
515c4292
ILT
4060 JUMP_TABLE (ecoff),
4061 0, 0,
4062 (PTR) &bfd_ecoff_std_swap_table
4063};
1327fb29
SC
4064
4065bfd_target ecoff_big_vec =
294eaca4
SC
4066{
4067 "ecoff-bigmips", /* name */
515c4292 4068 bfd_target_ecoff_flavour,
294eaca4
SC
4069 true, /* data byte order is big */
4070 true, /* header byte order is big */
4071
4072 (HAS_RELOC | EXEC_P | /* object flags */
4073 HAS_LINENO | HAS_DEBUG |
4074 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT),
4075
4076 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* sect flags */
4077 0, /* leading underscore */
4078 ' ', /* ar_pad_char */
4079 16, /* ar_max_namelen */
4080 3, /* minimum alignment power */
4081 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
4082 _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16,
4083 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
c3fe0c41
ILT
4084 ecoff_archive_p, _bfd_dummy_target},
4085 {bfd_false, ecoff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
4086 bfd_false},
515c4292 4087 {bfd_false, ecoff_write_object_contents, /* bfd_write_contents */
c3fe0c41 4088 _bfd_write_archive_contents, bfd_false},
515c4292
ILT
4089 JUMP_TABLE(ecoff),
4090 0, 0,
4091 (PTR) &bfd_ecoff_std_swap_table
4092 /* Note that there is another bfd_target just above this one. If
4093 you are adding initializers here, you should be adding them there
4094 as well. */
4095};
This page took 0.231789 seconds and 4 git commands to generate.