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