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