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