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