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