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