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