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