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