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