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