one-operand versions of jmp, jsr
[deliverable/binutils-gdb.git] / bfd / coffgen.c
CommitLineData
075caafd 1/* Support for the generic parts of COFF, for BFD.
6dc6a81a 2 Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
075caafd
ILT
3 Written by Cygnus Support.
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
ae115e51 19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
075caafd
ILT
20
21/* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
22 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
23
24/* This file contains COFF code that is not dependent on any
25 particular COFF target. There is only one version of this file in
26 libbfd.a, so no target specific code may be put in here. Or, to
27 put it another way,
28
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
30
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
33
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
36 coff_data (abfd). */
37
38#include "bfd.h"
39#include "sysdep.h"
40#include "libbfd.h"
41#include "coff/internal.h"
42#include "libcoff.h"
43
f0500a41
ILT
44static void coff_fix_symbol_name
45 PARAMS ((bfd *, asymbol *, combined_entry_type *, bfd_size_type *,
46 asection **, bfd_size_type *));
47static boolean coff_write_symbol
48 PARAMS ((bfd *, asymbol *, combined_entry_type *, unsigned int *,
49 bfd_size_type *, asection **, bfd_size_type *));
50static boolean coff_write_alien_symbol
51 PARAMS ((bfd *, asymbol *, unsigned int *, bfd_size_type *,
52 asection **, bfd_size_type *));
53static boolean coff_write_native_symbol
54 PARAMS ((bfd *, coff_symbol_type *, unsigned int *, bfd_size_type *,
55 asection **, bfd_size_type *));
c80cc833
ILT
56static void coff_pointerize_aux
57 PARAMS ((bfd *, combined_entry_type *, combined_entry_type *,
58 unsigned int, combined_entry_type *));
bfe8224f 59
9fbe895a
ILT
60#define STRING_SIZE_SIZE (4)
61
075caafd
ILT
62/* Take a section header read from a coff file (in HOST byte order),
63 and make a BFD "section" out of it. This is used by ECOFF. */
6dc6a81a 64static boolean
25057836 65make_a_section_from_file (abfd, hdr, target_index)
6dc6a81a
ILT
66 bfd *abfd;
67 struct internal_scnhdr *hdr;
25057836 68 unsigned int target_index;
075caafd 69{
6dc6a81a 70 asection *return_section;
075caafd 71 char *name;
6dc6a81a 72
075caafd 73 /* Assorted wastage to null-terminate the name, thanks AT&T! */
6dc6a81a
ILT
74 name = bfd_alloc (abfd, sizeof (hdr->s_name) + 1);
75 if (name == NULL)
76 {
d1ad85a6 77 bfd_set_error (bfd_error_no_memory);
075caafd
ILT
78 return false;
79 }
6dc6a81a 80 strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
075caafd
ILT
81 name[sizeof (hdr->s_name)] = 0;
82
c80cc833 83 return_section = bfd_make_section_anyway (abfd, name);
075caafd
ILT
84 if (return_section == NULL)
85 return false;
86
87 /* s_paddr is presumed to be = to s_vaddr */
88
89 return_section->vma = hdr->s_vaddr;
c7e76b5e 90 return_section->lma = return_section->vma;
075caafd
ILT
91 return_section->_raw_size = hdr->s_size;
92 return_section->filepos = hdr->s_scnptr;
6dc6a81a 93 return_section->rel_filepos = hdr->s_relptr;
075caafd
ILT
94 return_section->reloc_count = hdr->s_nreloc;
95
96 bfd_coff_set_alignment_hook (abfd, return_section, hdr);
97
6dc6a81a 98 return_section->line_filepos = hdr->s_lnnoptr;
075caafd
ILT
99
100 return_section->lineno_count = hdr->s_nlnno;
101 return_section->userdata = NULL;
102 return_section->next = (asection *) NULL;
e8fbe6d9 103 return_section->flags = bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name);
075caafd
ILT
104
105 return_section->target_index = target_index;
106
2d1e6c9c
KR
107 /* At least on i386-coff, the line number count for a shared library
108 section must be ignored. */
c16313f0 109 if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
2d1e6c9c
KR
110 return_section->lineno_count = 0;
111
075caafd
ILT
112 if (hdr->s_nreloc != 0)
113 return_section->flags |= SEC_RELOC;
114 /* FIXME: should this check 'hdr->s_size > 0' */
115 if (hdr->s_scnptr != 0)
116 return_section->flags |= SEC_HAS_CONTENTS;
117 return true;
118}
119
120/* Read in a COFF object and make it into a BFD. This is used by
121 ECOFF as well. */
122
2f3508ad 123static const bfd_target *
25057836 124coff_real_object_p (abfd, nscns, internal_f, internal_a)
6dc6a81a
ILT
125 bfd *abfd;
126 unsigned nscns;
25057836
JL
127 struct internal_filehdr *internal_f;
128 struct internal_aouthdr *internal_a;
075caafd 129{
c7e76b5e
ILT
130 flagword oflags = abfd->flags;
131 bfd_vma ostart = bfd_get_start_address (abfd);
075caafd 132 PTR tdata;
6dc6a81a 133 size_t readsize; /* length of file_info */
075caafd
ILT
134 unsigned int scnhsz;
135 char *external_sections;
136
c7e76b5e
ILT
137 if (!(internal_f->f_flags & F_RELFLG))
138 abfd->flags |= HAS_RELOC;
139 if ((internal_f->f_flags & F_EXEC))
140 abfd->flags |= EXEC_P;
141 if (!(internal_f->f_flags & F_LNNO))
142 abfd->flags |= HAS_LINENO;
143 if (!(internal_f->f_flags & F_LSYMS))
144 abfd->flags |= HAS_LOCALS;
145
146 /* FIXME: How can we set D_PAGED correctly? */
147 if ((internal_f->f_flags & F_EXEC) != 0)
148 abfd->flags |= D_PAGED;
149
150 bfd_get_symcount (abfd) = internal_f->f_nsyms;
151 if (internal_f->f_nsyms)
152 abfd->flags |= HAS_SYMS;
153
154 if (internal_a != (struct internal_aouthdr *) NULL)
155 bfd_get_start_address (abfd) = internal_a->entry;
156 else
157 bfd_get_start_address (abfd) = 0;
158
159 /* Set up the tdata area. ECOFF uses its own routine, and overrides
160 abfd->flags. */
27f524a3 161 tdata = bfd_coff_mkobject_hook (abfd, (PTR) internal_f, (PTR) internal_a);
075caafd
ILT
162 if (tdata == NULL)
163 return 0;
164
165 scnhsz = bfd_coff_scnhsz (abfd);
166 readsize = nscns * scnhsz;
6dc6a81a 167 external_sections = (char *) bfd_alloc (abfd, readsize);
9783e04a
DM
168 if (!external_sections)
169 {
d1ad85a6 170 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
171 goto fail;
172 }
075caafd 173
6dc6a81a 174 if (bfd_read ((PTR) external_sections, 1, readsize, abfd) != readsize)
075caafd 175 goto fail;
075caafd
ILT
176
177 /* Now copy data as required; construct all asections etc */
6dc6a81a
ILT
178 if (nscns != 0)
179 {
180 unsigned int i;
181 for (i = 0; i < nscns; i++)
182 {
183 struct internal_scnhdr tmp;
c7e76b5e
ILT
184 bfd_coff_swap_scnhdr_in (abfd,
185 (PTR) (external_sections + i * scnhsz),
6dc6a81a
ILT
186 (PTR) & tmp);
187 make_a_section_from_file (abfd, &tmp, i + 1);
188 }
075caafd 189 }
075caafd 190
6dc6a81a
ILT
191 /* make_abs_section (abfd); */
192
075caafd
ILT
193 if (bfd_coff_set_arch_mach_hook (abfd, (PTR) internal_f) == false)
194 goto fail;
195
075caafd 196 return abfd->xvec;
c7e76b5e 197
075caafd 198 fail:
6dc6a81a 199 bfd_release (abfd, tdata);
c7e76b5e
ILT
200 abfd->flags = oflags;
201 bfd_get_start_address (abfd) = ostart;
6dc6a81a 202 return (const bfd_target *) NULL;
075caafd
ILT
203}
204
d1ad85a6 205/* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
075caafd
ILT
206 not a COFF file. This is also used by ECOFF. */
207
2f3508ad 208const bfd_target *
25057836 209coff_object_p (abfd)
6dc6a81a 210 bfd *abfd;
075caafd
ILT
211{
212 unsigned int filhsz;
213 unsigned int aoutsz;
6dc6a81a 214 int nscns;
075caafd
ILT
215 PTR filehdr;
216 struct internal_filehdr internal_f;
217 struct internal_aouthdr internal_a;
218
075caafd
ILT
219 /* figure out how much to read */
220 filhsz = bfd_coff_filhsz (abfd);
221 aoutsz = bfd_coff_aoutsz (abfd);
222
223 filehdr = bfd_alloc (abfd, filhsz);
224 if (filehdr == NULL)
225 return 0;
6dc6a81a 226 if (bfd_read (filehdr, 1, filhsz, abfd) != filhsz)
25057836
JL
227 {
228 if (bfd_get_error () != bfd_error_system_call)
229 bfd_set_error (bfd_error_wrong_format);
230 return 0;
231 }
6dc6a81a 232 bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
075caafd
ILT
233 bfd_release (abfd, filehdr);
234
6dc6a81a
ILT
235 if (bfd_coff_bad_format_hook (abfd, &internal_f) == false)
236 {
237 bfd_set_error (bfd_error_wrong_format);
238 return 0;
239 }
240 nscns = internal_f.f_nscns;
075caafd 241
6dc6a81a
ILT
242 if (internal_f.f_opthdr)
243 {
244 PTR opthdr;
075caafd 245
6dc6a81a
ILT
246 opthdr = bfd_alloc (abfd, aoutsz);
247 if (opthdr == NULL)
248 return 0;;
249 if (bfd_read (opthdr, 1, aoutsz, abfd) != aoutsz)
250 {
251 return 0;
252 }
253 bfd_coff_swap_aouthdr_in (abfd, opthdr, (PTR) & internal_a);
075caafd 254 }
075caafd
ILT
255
256 /* Seek past the opt hdr stuff */
6dc6a81a 257 if (bfd_seek (abfd, (file_ptr) (internal_f.f_opthdr + filhsz), SEEK_SET)
c16313f0
ILT
258 != 0)
259 return NULL;
075caafd 260
6dc6a81a
ILT
261 return coff_real_object_p (abfd, nscns, &internal_f,
262 (internal_f.f_opthdr != 0
263 ? &internal_a
264 : (struct internal_aouthdr *) NULL));
075caafd
ILT
265}
266
267/* Get the BFD section from a COFF symbol section number. */
268
e8fbe6d9 269asection *
25057836 270coff_section_from_bfd_index (abfd, index)
6dc6a81a
ILT
271 bfd *abfd;
272 int index;
075caafd
ILT
273{
274 struct sec *answer = abfd->sections;
275
6dc6a81a 276 if (index == N_ABS)
e8fbe6d9 277 return bfd_abs_section_ptr;
075caafd 278 if (index == N_UNDEF)
e8fbe6d9 279 return bfd_und_section_ptr;
6dc6a81a
ILT
280 if (index == N_DEBUG)
281 return bfd_abs_section_ptr;
282
283 while (answer)
284 {
075caafd 285 if (answer->target_index == index)
6dc6a81a 286 return answer;
075caafd
ILT
287 answer = answer->next;
288 }
c16313f0
ILT
289
290 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
291 has a bad symbol table in biglitpow.o. */
e8fbe6d9 292 return bfd_und_section_ptr;
075caafd
ILT
293}
294
295/* Get the upper bound of a COFF symbol table. */
296
326e32d7 297long
6dc6a81a
ILT
298coff_get_symtab_upper_bound (abfd)
299 bfd *abfd;
075caafd 300{
6dc6a81a 301 if (!bfd_coff_slurp_symbol_table (abfd))
326e32d7 302 return -1;
075caafd 303
6dc6a81a 304 return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
075caafd
ILT
305}
306
307
308/* Canonicalize a COFF symbol table. */
309
326e32d7 310long
25057836 311coff_get_symtab (abfd, alocation)
74942465
ILT
312 bfd *abfd;
313 asymbol **alocation;
075caafd 314{
74942465
ILT
315 unsigned int counter;
316 coff_symbol_type *symbase;
317 coff_symbol_type **location = (coff_symbol_type **) alocation;
318
6dc6a81a 319 if (!bfd_coff_slurp_symbol_table (abfd))
74942465
ILT
320 return -1;
321
322 symbase = obj_symbols (abfd);
323 counter = bfd_get_symcount (abfd);
324 while (counter-- > 0)
325 *location++ = symbase++;
326
327 *location = NULL;
328
329 return bfd_get_symcount (abfd);
075caafd
ILT
330}
331
867d923d
ILT
332/* Get the name of a symbol. The caller must pass in a buffer of size
333 >= SYMNMLEN + 1. */
334
335const char *
336_bfd_coff_internal_syment_name (abfd, sym, buf)
337 bfd *abfd;
338 const struct internal_syment *sym;
339 char *buf;
340{
341 /* FIXME: It's not clear this will work correctly if sizeof
342 (_n_zeroes) != 4. */
343 if (sym->_n._n_n._n_zeroes != 0
344 || sym->_n._n_n._n_offset == 0)
345 {
346 memcpy (buf, sym->_n._n_name, SYMNMLEN);
347 buf[SYMNMLEN] = '\0';
348 return buf;
349 }
350 else
351 {
352 const char *strings;
353
354 BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
355 strings = obj_coff_strings (abfd);
356 if (strings == NULL)
357 {
358 strings = _bfd_coff_read_string_table (abfd);
359 if (strings == NULL)
360 return NULL;
361 }
362 return strings + sym->_n._n_n._n_offset;
363 }
364}
365
366/* Read in and swap the relocs. This returns a buffer holding the
367 relocs for section SEC in file ABFD. If CACHE is true and
368 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
369 the function is called again. If EXTERNAL_RELOCS is not NULL, it
370 is a buffer large enough to hold the unswapped relocs. If
371 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
372 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
373 value must be INTERNAL_RELOCS. The function returns NULL on error. */
374
375struct internal_reloc *
376_bfd_coff_read_internal_relocs (abfd, sec, cache, external_relocs,
377 require_internal, internal_relocs)
378 bfd *abfd;
379 asection *sec;
380 boolean cache;
381 bfd_byte *external_relocs;
382 boolean require_internal;
383 struct internal_reloc *internal_relocs;
384{
385 bfd_size_type relsz;
386 bfd_byte *free_external = NULL;
387 struct internal_reloc *free_internal = NULL;
388 bfd_byte *erel;
389 bfd_byte *erel_end;
390 struct internal_reloc *irel;
391
392 if (coff_section_data (abfd, sec) != NULL
393 && coff_section_data (abfd, sec)->relocs != NULL)
394 {
395 if (! require_internal)
396 return coff_section_data (abfd, sec)->relocs;
397 memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
398 sec->reloc_count * sizeof (struct internal_reloc));
399 return internal_relocs;
400 }
401
402 relsz = bfd_coff_relsz (abfd);
403
404 if (external_relocs == NULL)
405 {
406 free_external = (bfd_byte *) malloc (sec->reloc_count * relsz);
407 if (free_external == NULL && sec->reloc_count > 0)
408 {
409 bfd_set_error (bfd_error_no_memory);
410 goto error_return;
411 }
412 external_relocs = free_external;
413 }
414
415 if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
416 || (bfd_read (external_relocs, relsz, sec->reloc_count, abfd)
417 != relsz * sec->reloc_count))
418 goto error_return;
419
420 if (internal_relocs == NULL)
421 {
422 free_internal = ((struct internal_reloc *)
423 malloc (sec->reloc_count
424 * sizeof (struct internal_reloc)));
425 if (free_internal == NULL && sec->reloc_count > 0)
426 {
427 bfd_set_error (bfd_error_no_memory);
428 goto error_return;
429 }
430 internal_relocs = free_internal;
431 }
432
433 /* Swap in the relocs. */
434 erel = external_relocs;
435 erel_end = erel + relsz * sec->reloc_count;
436 irel = internal_relocs;
437 for (; erel < erel_end; erel += relsz, irel++)
438 bfd_coff_swap_reloc_in (abfd, (PTR) erel, (PTR) irel);
439
440 if (free_external != NULL)
441 {
442 free (free_external);
443 free_external = NULL;
444 }
445
446 if (cache && free_internal != NULL)
447 {
448 if (coff_section_data (abfd, sec) == NULL)
449 {
450 sec->used_by_bfd =
451 (PTR) bfd_zalloc (abfd,
452 sizeof (struct coff_section_tdata));
453 if (sec->used_by_bfd == NULL)
454 {
455 bfd_set_error (bfd_error_no_memory);
456 goto error_return;
457 }
458 coff_section_data (abfd, sec)->contents = NULL;
459 }
460 coff_section_data (abfd, sec)->relocs = free_internal;
461 }
462
463 return internal_relocs;
464
465 error_return:
466 if (free_external != NULL)
467 free (free_external);
468 if (free_internal != NULL)
469 free (free_internal);
470 return NULL;
471}
472
075caafd
ILT
473/* Set lineno_count for the output sections of a COFF file. */
474
27f524a3 475int
25057836 476coff_count_linenumbers (abfd)
69645d10 477 bfd *abfd;
075caafd 478{
6dc6a81a 479 unsigned int limit = bfd_get_symcount (abfd);
69645d10 480 unsigned int i;
27f524a3 481 int total = 0;
69645d10
ILT
482 asymbol **p;
483 asection *s;
484
485 if (limit == 0)
486 {
487 /* This may be from the backend linker, in which case the
488 lineno_count in the sections is correct. */
489 for (s = abfd->sections; s != NULL; s = s->next)
490 total += s->lineno_count;
491 return total;
492 }
493
494 for (s = abfd->sections; s != NULL; s = s->next)
495 BFD_ASSERT (s->lineno_count == 0);
496
497 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
498 {
499 asymbol *q_maybe = *p;
500
501 if (bfd_asymbol_flavour (q_maybe) == bfd_target_coff_flavour)
502 {
503 coff_symbol_type *q = coffsymbol (q_maybe);
504
d7731c7d
ILT
505 /* The AIX 4.1 compiler can sometimes generate line numbers
506 attached to debugging symbols. We try to simply ignore
507 those here. */
508 if (q->lineno != NULL
509 && q->symbol.section->owner != NULL)
69645d10
ILT
510 {
511 /* This symbol has line numbers. Increment the owning
6dc6a81a 512 section's linenumber count. */
69645d10
ILT
513 alent *l = q->lineno;
514
515 ++q->symbol.section->output_section->lineno_count;
516 ++total;
517 ++l;
518 while (l->line_number != 0)
519 {
520 ++total;
521 ++q->symbol.section->output_section->lineno_count;
522 ++l;
523 }
524 }
075caafd 525 }
075caafd 526 }
69645d10 527
27f524a3 528 return total;
075caafd
ILT
529}
530
531/* Takes a bfd and a symbol, returns a pointer to the coff specific
532 area of the symbol if there is one. */
533
330595d0 534/*ARGSUSED*/
075caafd 535coff_symbol_type *
25057836 536coff_symbol_from (ignore_abfd, symbol)
6dc6a81a
ILT
537 bfd *ignore_abfd;
538 asymbol *symbol;
075caafd 539{
6dc6a81a
ILT
540 if (bfd_asymbol_flavour (symbol) != bfd_target_coff_flavour)
541 return (coff_symbol_type *) NULL;
075caafd 542
6dc6a81a
ILT
543 if (bfd_asymbol_bfd (symbol)->tdata.coff_obj_data == (coff_data_type *) NULL)
544 return (coff_symbol_type *) NULL;
075caafd 545
6dc6a81a 546 return (coff_symbol_type *) symbol;
075caafd
ILT
547}
548
549static void
25057836
JL
550fixup_symbol_value (coff_symbol_ptr, syment)
551 coff_symbol_type *coff_symbol_ptr;
552 struct internal_syment *syment;
075caafd
ILT
553{
554
555 /* Normalize the symbol flags */
6dc6a81a
ILT
556 if (bfd_is_com_section (coff_symbol_ptr->symbol.section))
557 {
558 /* a common symbol is undefined with a value */
559 syment->n_scnum = N_UNDEF;
560 syment->n_value = coff_symbol_ptr->symbol.value;
075caafd 561 }
6dc6a81a
ILT
562 else if (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING)
563 {
075caafd
ILT
564 syment->n_value = coff_symbol_ptr->symbol.value;
565 }
6dc6a81a
ILT
566 else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
567 {
568 syment->n_scnum = N_UNDEF;
569 syment->n_value = 0;
570 }
571 else
572 {
573 if (coff_symbol_ptr->symbol.section)
574 {
575 syment->n_scnum =
576 coff_symbol_ptr->symbol.section->output_section->target_index;
577
578 syment->n_value =
579 coff_symbol_ptr->symbol.value +
580 coff_symbol_ptr->symbol.section->output_offset +
581 coff_symbol_ptr->symbol.section->output_section->vma;
582 }
583 else
584 {
585 BFD_ASSERT (0);
586 /* This can happen, but I don't know why yet (steve@cygnus.com) */
587 syment->n_scnum = N_ABS;
588 syment->n_value = coff_symbol_ptr->symbol.value;
589 }
590 }
075caafd
ILT
591}
592
6dc6a81a
ILT
593/* Run through all the symbols in the symbol table and work out what
594 their indexes into the symbol table will be when output.
075caafd 595
6dc6a81a
ILT
596 Coff requires that each C_FILE symbol points to the next one in the
597 chain, and that the last one points to the first external symbol. We
598 do that here too. */
075caafd 599
9783e04a 600boolean
c7e76b5e 601coff_renumber_symbols (bfd_ptr, first_undef)
25057836 602 bfd *bfd_ptr;
c7e76b5e 603 int *first_undef;
075caafd 604{
6dc6a81a 605 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
075caafd
ILT
606 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
607 unsigned int native_index = 0;
6dc6a81a 608 struct internal_syment *last_file = (struct internal_syment *) NULL;
075caafd
ILT
609 unsigned int symbol_index;
610
611 /* COFF demands that undefined symbols come after all other symbols.
c7e76b5e
ILT
612 Since we don't need to impose this extra knowledge on all our
613 client programs, deal with that here. Sort the symbol table;
614 just move the undefined symbols to the end, leaving the rest
615 alone. The O'Reilly book says that defined global symbols come
616 at the end before the undefined symbols, so we do that here as
617 well. */
075caafd
ILT
618 /* @@ Do we have some condition we could test for, so we don't always
619 have to do this? I don't think relocatability is quite right, but
620 I'm not certain. [raeburn:19920508.1711EST] */
621 {
622 asymbol **newsyms;
ae115e51 623 unsigned int i;
075caafd
ILT
624
625 newsyms = (asymbol **) bfd_alloc_by_size_t (bfd_ptr,
626 sizeof (asymbol *)
627 * (symbol_count + 1));
9783e04a
DM
628 if (!newsyms)
629 {
d1ad85a6 630 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
631 return false;
632 }
075caafd
ILT
633 bfd_ptr->outsymbols = newsyms;
634 for (i = 0; i < symbol_count; i++)
5f710a3a
ILT
635 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
636 || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
637 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_FUNCTION))
638 != BSF_GLOBAL)))
075caafd 639 *newsyms++ = symbol_ptr_ptr[i];
c7e76b5e
ILT
640
641 for (i = 0; i < symbol_count; i++)
642 if (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
643 && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL
644 | BSF_NOT_AT_END
645 | BSF_FUNCTION))
646 == BSF_GLOBAL))
647 *newsyms++ = symbol_ptr_ptr[i];
648
649 *first_undef = newsyms - bfd_ptr->outsymbols;
650
075caafd 651 for (i = 0; i < symbol_count; i++)
5f710a3a
ILT
652 if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
653 && bfd_is_und_section (symbol_ptr_ptr[i]->section))
075caafd
ILT
654 *newsyms++ = symbol_ptr_ptr[i];
655 *newsyms = (asymbol *) NULL;
656 symbol_ptr_ptr = bfd_ptr->outsymbols;
657 }
658
659 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
6dc6a81a
ILT
660 {
661 coff_symbol_type *coff_symbol_ptr = coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
c7e76b5e 662 symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
6dc6a81a
ILT
663 if (coff_symbol_ptr && coff_symbol_ptr->native)
664 {
075caafd
ILT
665 combined_entry_type *s = coff_symbol_ptr->native;
666 int i;
667
668 if (s->u.syment.n_sclass == C_FILE)
6dc6a81a
ILT
669 {
670 if (last_file != (struct internal_syment *) NULL)
671 last_file->n_value = native_index;
672 last_file = &(s->u.syment);
673 }
674 else
675 {
676
677 /* Modify the symbol values according to their section and
678 type */
679
680 fixup_symbol_value (coff_symbol_ptr, &(s->u.syment));
681 }
682 for (i = 0; i < s->u.syment.n_numaux + 1; i++)
683 s[i].offset = native_index++;
075caafd 684 }
6dc6a81a
ILT
685 else
686 {
075caafd
ILT
687 native_index++;
688 }
6dc6a81a 689 }
075caafd 690 obj_conv_table_size (bfd_ptr) = native_index;
c7e76b5e 691
9783e04a 692 return true;
075caafd
ILT
693}
694
6dc6a81a
ILT
695/* Run thorough the symbol table again, and fix it so that all
696 pointers to entries are changed to the entries' index in the output
697 symbol table. */
075caafd 698
075caafd 699void
9783e04a
DM
700coff_mangle_symbols (bfd_ptr)
701 bfd *bfd_ptr;
075caafd 702{
9783e04a 703 unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
075caafd
ILT
704 asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
705 unsigned int symbol_index;
706
707 for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
9783e04a
DM
708 {
709 coff_symbol_type *coff_symbol_ptr =
6dc6a81a 710 coff_symbol_from (bfd_ptr, symbol_ptr_ptr[symbol_index]);
075caafd 711
9783e04a
DM
712 if (coff_symbol_ptr && coff_symbol_ptr->native)
713 {
075caafd
ILT
714 int i;
715 combined_entry_type *s = coff_symbol_ptr->native;
716
9783e04a
DM
717 if (s->fix_value)
718 {
719 /* FIXME: We should use a union here. */
720 s->u.syment.n_value =
721 ((combined_entry_type *) s->u.syment.n_value)->offset;
722 s->fix_value = 0;
075caafd 723 }
49488f2b
ILT
724 if (s->fix_line)
725 {
726 /* The value is the offset into the line number entries
727 for the symbol's section. On output, the symbol's
728 section should be N_DEBUG. */
729 s->u.syment.n_value =
730 (coff_symbol_ptr->symbol.section->output_section->line_filepos
731 + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
732 coff_symbol_ptr->symbol.section =
733 coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
734 BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
735 }
6dc6a81a 736 for (i = 0; i < s->u.syment.n_numaux; i++)
9783e04a
DM
737 {
738 combined_entry_type *a = s + i + 1;
739 if (a->fix_tag)
740 {
741 a->u.auxent.x_sym.x_tagndx.l =
742 a->u.auxent.x_sym.x_tagndx.p->offset;
743 a->fix_tag = 0;
744 }
745 if (a->fix_end)
746 {
747 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
748 a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
749 a->fix_end = 0;
750 }
751 if (a->fix_scnlen)
752 {
753 a->u.auxent.x_csect.x_scnlen.l =
754 a->u.auxent.x_csect.x_scnlen.p->offset;
755 a->fix_scnlen = 0;
756 }
075caafd 757 }
075caafd 758 }
9783e04a 759 }
075caafd
ILT
760}
761
075caafd 762static void
f0500a41
ILT
763coff_fix_symbol_name (abfd, symbol, native, string_size_p,
764 debug_string_section_p, debug_string_size_p)
25057836
JL
765 bfd *abfd;
766 asymbol *symbol;
767 combined_entry_type *native;
f0500a41
ILT
768 bfd_size_type *string_size_p;
769 asection **debug_string_section_p;
770 bfd_size_type *debug_string_size_p;
075caafd 771{
6dc6a81a 772 unsigned int name_length;
075caafd 773 union internal_auxent *auxent;
6dc6a81a 774 char *name = (char *) (symbol->name);
075caafd 775
6dc6a81a
ILT
776 if (name == (char *) NULL)
777 {
778 /* coff symbols always have names, so we'll make one up */
779 symbol->name = "strange";
780 name = (char *) symbol->name;
781 }
782 name_length = strlen (name);
075caafd 783
4372f33f
ILT
784 if (native->u.syment.n_sclass == C_FILE
785 && native->u.syment.n_numaux > 0)
6dc6a81a
ILT
786 {
787 strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
788 auxent = &(native + 1)->u.auxent;
075caafd 789
6dc6a81a
ILT
790 if (bfd_coff_long_filenames (abfd))
791 {
792 if (name_length <= FILNMLEN)
793 {
794 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
795 }
796 else
797 {
f0500a41 798 auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
6dc6a81a 799 auxent->x_file.x_n.x_zeroes = 0;
f0500a41 800 *string_size_p += name_length + 1;
6dc6a81a
ILT
801 }
802 }
803 else
804 {
805 strncpy (auxent->x_file.x_fname, name, FILNMLEN);
806 if (name_length > FILNMLEN)
807 {
808 name[FILNMLEN] = '\0';
809 }
810 }
075caafd 811 }
075caafd 812 else
4372f33f 813 {
9783e04a
DM
814 if (name_length <= SYMNMLEN)
815 {
075caafd 816 /* This name will fit into the symbol neatly */
6dc6a81a 817 strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
075caafd 818 }
6dc6a81a 819 else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
9783e04a 820 {
f0500a41
ILT
821 native->u.syment._n._n_n._n_offset = (*string_size_p
822 + STRING_SIZE_SIZE);
075caafd 823 native->u.syment._n._n_n._n_zeroes = 0;
f0500a41 824 *string_size_p += name_length + 1;
075caafd 825 }
9783e04a
DM
826 else
827 {
828 long filepos;
829 bfd_byte buf[2];
830
831 /* This name should be written into the .debug section. For
832 some reason each name is preceded by a two byte length
833 and also followed by a null byte. FIXME: We assume that
834 the .debug section has already been created, and that it
835 is large enough. */
f0500a41
ILT
836 if (*debug_string_section_p == (asection *) NULL)
837 *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
9783e04a
DM
838 filepos = bfd_tell (abfd);
839 bfd_put_16 (abfd, name_length + 1, buf);
6dc6a81a 840 if (!bfd_set_section_contents (abfd,
f0500a41 841 *debug_string_section_p,
6dc6a81a 842 (PTR) buf,
f0500a41 843 (file_ptr) *debug_string_size_p,
6dc6a81a
ILT
844 (bfd_size_type) 2)
845 || !bfd_set_section_contents (abfd,
f0500a41 846 *debug_string_section_p,
6dc6a81a 847 (PTR) symbol->name,
f0500a41
ILT
848 ((file_ptr) *debug_string_size_p
849 + 2),
6dc6a81a 850 (bfd_size_type) name_length + 1))
9783e04a
DM
851 abort ();
852 if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
853 abort ();
f0500a41 854 native->u.syment._n._n_n._n_offset = *debug_string_size_p + 2;
9783e04a 855 native->u.syment._n._n_n._n_zeroes = 0;
f0500a41 856 *debug_string_size_p += name_length + 3;
9783e04a
DM
857 }
858 }
075caafd
ILT
859}
860
bfe8224f
ILT
861/* We need to keep track of the symbol index so that when we write out
862 the relocs we can get the index for a symbol. This method is a
863 hack. FIXME. */
864
74942465 865#define set_index(symbol, idx) ((symbol)->udata.i = (idx))
bfe8224f
ILT
866
867/* Write a symbol out to a COFF file. */
075caafd 868
bfe8224f 869static boolean
f0500a41
ILT
870coff_write_symbol (abfd, symbol, native, written, string_size_p,
871 debug_string_section_p, debug_string_size_p)
25057836
JL
872 bfd *abfd;
873 asymbol *symbol;
874 combined_entry_type *native;
bfe8224f 875 unsigned int *written;
f0500a41
ILT
876 bfd_size_type *string_size_p;
877 asection **debug_string_section_p;
878 bfd_size_type *debug_string_size_p;
075caafd 879{
bfe8224f
ILT
880 unsigned int numaux = native->u.syment.n_numaux;
881 int type = native->u.syment.n_type;
6dc6a81a 882 int class = native->u.syment.n_sclass;
075caafd
ILT
883 PTR buf;
884 bfd_size_type symesz;
885
075caafd 886 if (native->u.syment.n_sclass == C_FILE)
6dc6a81a 887 symbol->flags |= BSF_DEBUGGING;
075caafd 888
49488f2b
ILT
889 if (symbol->flags & BSF_DEBUGGING
890 && bfd_is_abs_section (symbol->section))
bfe8224f 891 {
6dc6a81a 892 native->u.syment.n_scnum = N_DEBUG;
bfe8224f 893 }
6dc6a81a 894 else if (bfd_is_abs_section (symbol->section))
bfe8224f 895 {
6dc6a81a 896 native->u.syment.n_scnum = N_ABS;
bfe8224f 897 }
e8fbe6d9 898 else if (bfd_is_und_section (symbol->section))
bfe8224f
ILT
899 {
900 native->u.syment.n_scnum = N_UNDEF;
901 }
6dc6a81a 902 else
bfe8224f
ILT
903 {
904 native->u.syment.n_scnum =
905 symbol->section->output_section->target_index;
906 }
6dc6a81a 907
f0500a41
ILT
908 coff_fix_symbol_name (abfd, symbol, native, string_size_p,
909 debug_string_section_p, debug_string_size_p);
075caafd
ILT
910
911 symesz = bfd_coff_symesz (abfd);
912 buf = bfd_alloc (abfd, symesz);
9783e04a
DM
913 if (!buf)
914 {
d1ad85a6 915 bfd_set_error (bfd_error_no_memory);
bfe8224f 916 return false;
9783e04a 917 }
bfe8224f
ILT
918 bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
919 if (bfd_write (buf, 1, symesz, abfd) != symesz)
920 return false;
075caafd
ILT
921 bfd_release (abfd, buf);
922
923 if (native->u.syment.n_numaux > 0)
924 {
925 bfd_size_type auxesz;
926 unsigned int j;
927
928 auxesz = bfd_coff_auxesz (abfd);
929 buf = bfd_alloc (abfd, auxesz);
9783e04a
DM
930 if (!buf)
931 {
d1ad85a6 932 bfd_set_error (bfd_error_no_memory);
bfe8224f 933 return false;
9783e04a 934 }
6dc6a81a 935 for (j = 0; j < native->u.syment.n_numaux; j++)
075caafd 936 {
bfe8224f
ILT
937 bfd_coff_swap_aux_out (abfd,
938 &((native + j + 1)->u.auxent),
939 type,
940 class,
941 j,
942 native->u.syment.n_numaux,
943 buf);
6dc6a81a 944 if (bfd_write (buf, 1, auxesz, abfd) != auxesz)
bfe8224f 945 return false;
075caafd
ILT
946 }
947 bfd_release (abfd, buf);
948 }
bfe8224f
ILT
949
950 /* Store the index for use when we write out the relocs. */
951 set_index (symbol, *written);
952
953 *written += numaux + 1;
954 return true;
075caafd
ILT
955}
956
bfe8224f
ILT
957/* Write out a symbol to a COFF file that does not come from a COFF
958 file originally. This symbol may have been created by the linker,
959 or we may be linking a non COFF file to a COFF file. */
075caafd 960
bfe8224f 961static boolean
f0500a41
ILT
962coff_write_alien_symbol (abfd, symbol, written, string_size_p,
963 debug_string_section_p, debug_string_size_p)
25057836
JL
964 bfd *abfd;
965 asymbol *symbol;
bfe8224f 966 unsigned int *written;
f0500a41
ILT
967 bfd_size_type *string_size_p;
968 asection **debug_string_section_p;
969 bfd_size_type *debug_string_size_p;
075caafd 970{
075caafd
ILT
971 combined_entry_type *native;
972 combined_entry_type dummy;
bfe8224f 973
075caafd 974 native = &dummy;
6dc6a81a
ILT
975 native->u.syment.n_type = T_NULL;
976 native->u.syment.n_flags = 0;
e8fbe6d9 977 if (bfd_is_und_section (symbol->section))
bfe8224f
ILT
978 {
979 native->u.syment.n_scnum = N_UNDEF;
980 native->u.syment.n_value = symbol->value;
075caafd 981 }
e61cfdf8 982 else if (bfd_is_com_section (symbol->section))
bfe8224f
ILT
983 {
984 native->u.syment.n_scnum = N_UNDEF;
985 native->u.syment.n_value = symbol->value;
075caafd 986 }
bfe8224f 987 else if (symbol->flags & BSF_DEBUGGING)
075caafd 988 {
a74d1517
ILT
989 /* There isn't much point to writing out a debugging symbol
990 unless we are prepared to convert it into COFF debugging
715cde57
ILT
991 format. So, we just ignore them. We must clobber the symbol
992 name to keep it from being put in the string table. */
993 symbol->name = "";
a74d1517 994 return true;
075caafd 995 }
bfe8224f
ILT
996 else
997 {
998 native->u.syment.n_scnum =
999 symbol->section->output_section->target_index;
1000 native->u.syment.n_value = (symbol->value
1001 + symbol->section->output_section->vma
1002 + symbol->section->output_offset);
1003
1004 /* Copy the any flags from the the file header into the symbol.
6dc6a81a 1005 FIXME: Why? */
bfe8224f
ILT
1006 {
1007 coff_symbol_type *c = coff_symbol_from (abfd, symbol);
1008 if (c != (coff_symbol_type *) NULL)
1009 native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
1010 }
075caafd
ILT
1011 }
1012
bfe8224f 1013 native->u.syment.n_type = 0;
075caafd 1014 if (symbol->flags & BSF_LOCAL)
bfe8224f 1015 native->u.syment.n_sclass = C_STAT;
075caafd 1016 else
bfe8224f
ILT
1017 native->u.syment.n_sclass = C_EXT;
1018 native->u.syment.n_numaux = 0;
075caafd 1019
f0500a41
ILT
1020 return coff_write_symbol (abfd, symbol, native, written, string_size_p,
1021 debug_string_section_p, debug_string_size_p);
075caafd
ILT
1022}
1023
bfe8224f
ILT
1024/* Write a native symbol to a COFF file. */
1025
1026static boolean
f0500a41
ILT
1027coff_write_native_symbol (abfd, symbol, written, string_size_p,
1028 debug_string_section_p, debug_string_size_p)
25057836
JL
1029 bfd *abfd;
1030 coff_symbol_type *symbol;
bfe8224f 1031 unsigned int *written;
f0500a41
ILT
1032 bfd_size_type *string_size_p;
1033 asection **debug_string_section_p;
1034 bfd_size_type *debug_string_size_p;
075caafd 1035{
075caafd 1036 combined_entry_type *native = symbol->native;
bfe8224f 1037 alent *lineno = symbol->lineno;
075caafd 1038
bfe8224f
ILT
1039 /* If this symbol has an associated line number, we must store the
1040 symbol index in the line number field. We also tag the auxent to
1041 point to the right place in the lineno table. */
d7731c7d 1042 if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
bfe8224f
ILT
1043 {
1044 unsigned int count = 0;
1045 lineno[count].u.offset = *written;
1046 if (native->u.syment.n_numaux)
1047 {
6dc6a81a 1048 union internal_auxent *a = &((native + 1)->u.auxent);
075caafd 1049
bfe8224f
ILT
1050 a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
1051 symbol->symbol.section->output_section->moving_line_filepos;
1052 }
075caafd 1053
bfe8224f
ILT
1054 /* Count and relocate all other linenumbers. */
1055 count++;
1056 while (lineno[count].line_number != 0)
1057 {
075caafd 1058#if 0
bfe8224f
ILT
1059 /* 13 april 92. sac
1060 I've been told this, but still need proof:
1061 > The second bug is also in `bfd/coffcode.h'. This bug
1062 > causes the linker to screw up the pc-relocations for
1063 > all the line numbers in COFF code. This bug isn't only
1064 > specific to A29K implementations, but affects all
1065 > systems using COFF format binaries. Note that in COFF
1066 > object files, the line number core offsets output by
1067 > the assembler are relative to the start of each
1068 > procedure, not to the start of the .text section. This
1069 > patch relocates the line numbers relative to the
1070 > `native->u.syment.n_value' instead of the section
1071 > virtual address.
1072 > modular!olson@cs.arizona.edu (Jon Olson)
6dc6a81a 1073 */
bfe8224f 1074 lineno[count].u.offset += native->u.syment.n_value;
075caafd 1075#else
bfe8224f
ILT
1076 lineno[count].u.offset +=
1077 (symbol->symbol.section->output_section->vma
1078 + symbol->symbol.section->output_offset);
075caafd 1079#endif
bfe8224f
ILT
1080 count++;
1081 }
1082 symbol->done_lineno = true;
6dc6a81a 1083
bfe8224f
ILT
1084 symbol->symbol.section->output_section->moving_line_filepos +=
1085 count * bfd_coff_linesz (abfd);
1086 }
1087
f0500a41
ILT
1088 return coff_write_symbol (abfd, &(symbol->symbol), native, written,
1089 string_size_p, debug_string_section_p,
1090 debug_string_size_p);
075caafd
ILT
1091}
1092
bfe8224f
ILT
1093/* Write out the COFF symbols. */
1094
1095boolean
25057836 1096coff_write_symbols (abfd)
bfe8224f 1097 bfd *abfd;
075caafd 1098{
f0500a41
ILT
1099 bfd_size_type string_size;
1100 asection *debug_string_section;
1101 bfd_size_type debug_string_size;
bfe8224f 1102 unsigned int i;
6dc6a81a 1103 unsigned int limit = bfd_get_symcount (abfd);
bfe8224f
ILT
1104 unsigned int written = 0;
1105 asymbol **p;
075caafd
ILT
1106
1107 string_size = 0;
f0500a41 1108 debug_string_section = NULL;
9783e04a 1109 debug_string_size = 0;
075caafd
ILT
1110
1111 /* Seek to the right place */
6dc6a81a 1112 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
bfe8224f 1113 return false;
075caafd
ILT
1114
1115 /* Output all the symbols we have */
1116
1117 written = 0;
1118 for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
bfe8224f
ILT
1119 {
1120 asymbol *symbol = *p;
1121 coff_symbol_type *c_symbol = coff_symbol_from (abfd, symbol);
075caafd 1122
bfe8224f 1123 if (c_symbol == (coff_symbol_type *) NULL
6dc6a81a 1124 || c_symbol->native == (combined_entry_type *) NULL)
bfe8224f 1125 {
f0500a41
ILT
1126 if (!coff_write_alien_symbol (abfd, symbol, &written, &string_size,
1127 &debug_string_section,
1128 &debug_string_size))
bfe8224f
ILT
1129 return false;
1130 }
1131 else
1132 {
f0500a41
ILT
1133 if (!coff_write_native_symbol (abfd, c_symbol, &written,
1134 &string_size, &debug_string_section,
1135 &debug_string_size))
bfe8224f
ILT
1136 return false;
1137 }
1138 }
075caafd 1139
74942465
ILT
1140 obj_raw_syment_count (abfd) = written;
1141
075caafd
ILT
1142 /* Now write out strings */
1143
1144 if (string_size != 0)
6dc6a81a
ILT
1145 {
1146 unsigned int size = string_size + STRING_SIZE_SIZE;
1147 bfd_byte buffer[STRING_SIZE_SIZE];
075caafd 1148
9fbe895a 1149#if STRING_SIZE_SIZE == 4
6dc6a81a 1150 bfd_h_put_32 (abfd, size, buffer);
9fbe895a
ILT
1151#else
1152 #error Change bfd_h_put_32
1153#endif
6dc6a81a
ILT
1154 if (bfd_write ((PTR) buffer, 1, sizeof (buffer), abfd) != sizeof (buffer))
1155 return false;
1156 for (p = abfd->outsymbols, i = 0;
1157 i < limit;
1158 i++, p++)
1159 {
1160 asymbol *q = *p;
1161 size_t name_length = strlen (q->name);
1162 coff_symbol_type *c_symbol = coff_symbol_from (abfd, q);
1163 size_t maxlen;
1164
1165 /* Figure out whether the symbol name should go in the string
1166 table. Symbol names that are short enough are stored
1167 directly in the syment structure. File names permit a
1168 different, longer, length in the syment structure. On
1169 XCOFF, some symbol names are stored in the .debug section
1170 rather than in the string table. */
1171
1172 if (c_symbol == NULL
1173 || c_symbol->native == NULL)
1174 {
1175 /* This is not a COFF symbol, so it certainly is not a
1176 file name, nor does it go in the .debug section. */
1177 maxlen = SYMNMLEN;
1178 }
1179 else if (bfd_coff_symname_in_debug (abfd,
1180 &c_symbol->native->u.syment))
1181 {
1182 /* This symbol name is in the XCOFF .debug section.
1183 Don't write it into the string table. */
1184 maxlen = name_length;
1185 }
4372f33f
ILT
1186 else if (c_symbol->native->u.syment.n_sclass == C_FILE
1187 && c_symbol->native->u.syment.n_numaux > 0)
6dc6a81a
ILT
1188 maxlen = FILNMLEN;
1189 else
1190 maxlen = SYMNMLEN;
1191
1192 if (name_length > maxlen)
1193 {
1194 if (bfd_write ((PTR) (q->name), 1, name_length + 1, abfd)
1195 != name_length + 1)
1196 return false;
1197 }
1198 }
1199 }
bfe8224f
ILT
1200 else
1201 {
1202 /* We would normally not write anything here, but we'll write
6dc6a81a
ILT
1203 out 4 so that any stupid coff reader which tries to read the
1204 string table even when there isn't one won't croak. */
9fbe895a
ILT
1205 unsigned int size = STRING_SIZE_SIZE;
1206 bfd_byte buffer[STRING_SIZE_SIZE];
bfe8224f 1207
9fbe895a 1208#if STRING_SIZE_SIZE == 4
bfe8224f 1209 bfd_h_put_32 (abfd, size, buffer);
9fbe895a
ILT
1210#else
1211 #error Change bfd_h_put_32
1212#endif
1213 if (bfd_write ((PTR) buffer, 1, STRING_SIZE_SIZE, abfd)
1214 != STRING_SIZE_SIZE)
bfe8224f
ILT
1215 return false;
1216 }
9783e04a 1217
bfe8224f
ILT
1218 /* Make sure the .debug section was created to be the correct size.
1219 We should create it ourselves on the fly, but we don't because
1220 BFD won't let us write to any section until we know how large all
1221 the sections are. We could still do it by making another pass
1222 over the symbols. FIXME. */
9783e04a
DM
1223 BFD_ASSERT (debug_string_size == 0
1224 || (debug_string_section != (asection *) NULL
1225 && (BFD_ALIGN (debug_string_size,
1226 1 << debug_string_section->alignment_power)
1227 == bfd_section_size (abfd, debug_string_section))));
bfe8224f
ILT
1228
1229 return true;
075caafd
ILT
1230}
1231
9783e04a 1232boolean
25057836 1233coff_write_linenumbers (abfd)
6dc6a81a 1234 bfd *abfd;
075caafd 1235{
6dc6a81a 1236 asection *s;
075caafd
ILT
1237 bfd_size_type linesz;
1238 PTR buff;
1239
1240 linesz = bfd_coff_linesz (abfd);
1241 buff = bfd_alloc (abfd, linesz);
9783e04a
DM
1242 if (!buff)
1243 {
d1ad85a6 1244 bfd_set_error (bfd_error_no_memory);
25057836 1245 return false;
9783e04a 1246 }
6dc6a81a
ILT
1247 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1248 {
1249 if (s->lineno_count)
1250 {
1251 asymbol **q = abfd->outsymbols;
1252 if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
1253 return false;
1254 /* Find all the linenumbers in this section */
1255 while (*q)
1256 {
1257 asymbol *p = *q;
1258 if (p->section->output_section == s)
1259 {
1260 alent *l =
1261 BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
1262 (bfd_asymbol_bfd (p), p));
1263 if (l)
1264 {
1265 /* Found a linenumber entry, output */
1266 struct internal_lineno out;
1267 memset ((PTR) & out, 0, sizeof (out));
1268 out.l_lnno = 0;
1269 out.l_addr.l_symndx = l->u.offset;
1270 bfd_coff_swap_lineno_out (abfd, &out, buff);
1271 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1272 return false;
1273 l++;
1274 while (l->line_number)
1275 {
1276 out.l_lnno = l->line_number;
1277 out.l_addr.l_symndx = l->u.offset;
1278 bfd_coff_swap_lineno_out (abfd, &out, buff);
1279 if (bfd_write (buff, 1, linesz, abfd) != linesz)
1280 return false;
1281 l++;
1282 }
1283 }
1284 }
1285 q++;
27f524a3 1286 }
075caafd 1287 }
075caafd 1288 }
075caafd 1289 bfd_release (abfd, buff);
9783e04a 1290 return true;
075caafd
ILT
1291}
1292
6dc6a81a
ILT
1293/*ARGSUSED */
1294alent *
25057836 1295coff_get_lineno (ignore_abfd, symbol)
6dc6a81a
ILT
1296 bfd *ignore_abfd;
1297 asymbol *symbol;
075caafd 1298{
6dc6a81a 1299 return coffsymbol (symbol)->lineno;
075caafd
ILT
1300}
1301
1302asymbol *
1303coff_section_symbol (abfd, name)
1304 bfd *abfd;
1305 char *name;
1306{
1307 asection *sec = bfd_make_section_old_way (abfd, name);
1308 asymbol *sym;
1309 combined_entry_type *csym;
1310
1311 sym = sec->symbol;
4c3721d5 1312 csym = coff_symbol_from (abfd, sym)->native;
075caafd
ILT
1313 /* Make sure back-end COFF stuff is there. */
1314 if (csym == 0)
1315 {
6dc6a81a
ILT
1316 struct foo
1317 {
1318 coff_symbol_type sym;
1319 /* @@FIXME This shouldn't use a fixed size!! */
1320 combined_entry_type e[10];
1321 };
075caafd
ILT
1322 struct foo *f;
1323 f = (struct foo *) bfd_alloc_by_size_t (abfd, sizeof (*f));
9783e04a
DM
1324 if (!f)
1325 {
d1ad85a6 1326 bfd_set_error (bfd_error_no_error);
9783e04a
DM
1327 return NULL;
1328 }
075caafd
ILT
1329 memset ((char *) f, 0, sizeof (*f));
1330 coff_symbol_from (abfd, sym)->native = csym = f->e;
1331 }
1332 csym[0].u.syment.n_sclass = C_STAT;
1333 csym[0].u.syment.n_numaux = 1;
6dc6a81a 1334/* SF_SET_STATICS (sym); @@ ??? */
4c3721d5
ILT
1335 csym[1].u.auxent.x_scn.x_scnlen = sec->_raw_size;
1336 csym[1].u.auxent.x_scn.x_nreloc = sec->reloc_count;
1337 csym[1].u.auxent.x_scn.x_nlinno = sec->lineno_count;
1338
1339 if (sec->output_section == NULL)
075caafd 1340 {
4c3721d5
ILT
1341 sec->output_section = sec;
1342 sec->output_offset = 0;
075caafd 1343 }
4c3721d5 1344
075caafd
ILT
1345 return sym;
1346}
1347
1348/* This function transforms the offsets into the symbol table into
1349 pointers to syments. */
1350
1351static void
c80cc833 1352coff_pointerize_aux (abfd, table_base, symbol, indaux, auxent)
25057836
JL
1353 bfd *abfd;
1354 combined_entry_type *table_base;
c80cc833
ILT
1355 combined_entry_type *symbol;
1356 unsigned int indaux;
25057836 1357 combined_entry_type *auxent;
075caafd 1358{
c80cc833
ILT
1359 int type = symbol->u.syment.n_type;
1360 int class = symbol->u.syment.n_sclass;
1361
1362 if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1363 {
1364 if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
1365 (abfd, table_base, symbol, indaux, auxent))
1366 return;
1367 }
1368
075caafd 1369 /* Don't bother if this is a file or a section */
6dc6a81a
ILT
1370 if (class == C_STAT && type == T_NULL)
1371 return;
1372 if (class == C_FILE)
1373 return;
075caafd
ILT
1374
1375 /* Otherwise patch up */
1376#define N_TMASK coff_data (abfd)->local_n_tmask
1377#define N_BTSHFT coff_data (abfd)->local_n_btshft
6dc6a81a 1378 if ((ISFCN (type) || ISTAG (class) || class == C_BLOCK)
69645d10
ILT
1379 && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
1380 {
1381 auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
c80cc833 1382 table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
075caafd
ILT
1383 auxent->fix_end = 1;
1384 }
1385 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1386 generate one, so we must be careful to ignore it. */
6dc6a81a
ILT
1387 if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
1388 {
075caafd 1389 auxent->u.auxent.x_sym.x_tagndx.p =
6dc6a81a 1390 table_base + auxent->u.auxent.x_sym.x_tagndx.l;
075caafd
ILT
1391 auxent->fix_tag = 1;
1392 }
1393}
1394
075caafd
ILT
1395/* Allocate space for the ".debug" section, and read it.
1396 We did not read the debug section until now, because
1397 we didn't want to go to the trouble until someone needed it. */
1398
1399static char *
25057836
JL
1400build_debug_section (abfd)
1401 bfd *abfd;
075caafd
ILT
1402{
1403 char *debug_section;
1404 long position;
1405
1406 asection *sect = bfd_get_section_by_name (abfd, ".debug");
1407
6dc6a81a
ILT
1408 if (!sect)
1409 {
1410 bfd_set_error (bfd_error_no_debug_section);
1411 return NULL;
1412 }
075caafd
ILT
1413
1414 debug_section = (PTR) bfd_alloc (abfd,
1415 bfd_get_section_size_before_reloc (sect));
6dc6a81a
ILT
1416 if (debug_section == NULL)
1417 {
1418 bfd_set_error (bfd_error_no_memory);
1419 return NULL;
1420 }
075caafd
ILT
1421
1422 /* Seek to the beginning of the `.debug' section and read it.
1423 Save the current position first; it is needed by our caller.
1424 Then read debug section and reset the file pointer. */
1425
1426 position = bfd_tell (abfd);
c16313f0 1427 if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
6dc6a81a 1428 || (bfd_read (debug_section,
c16313f0 1429 bfd_get_section_size_before_reloc (sect), 1, abfd)
6dc6a81a 1430 != bfd_get_section_size_before_reloc (sect))
c16313f0 1431 || bfd_seek (abfd, position, SEEK_SET) != 0)
075caafd 1432 return NULL;
075caafd
ILT
1433 return debug_section;
1434}
1435
1436
1437/* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
6dc6a81a
ILT
1438 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1439 be \0-terminated. */
075caafd 1440static char *
25057836
JL
1441copy_name (abfd, name, maxlen)
1442 bfd *abfd;
1443 char *name;
1444 int maxlen;
075caafd 1445{
6dc6a81a 1446 int len;
075caafd
ILT
1447 char *newname;
1448
6dc6a81a
ILT
1449 for (len = 0; len < maxlen; ++len)
1450 {
1451 if (name[len] == '\0')
1452 {
1453 break;
1454 }
075caafd 1455 }
075caafd 1456
6dc6a81a
ILT
1457 if ((newname = (PTR) bfd_alloc (abfd, len + 1)) == NULL)
1458 {
1459 bfd_set_error (bfd_error_no_memory);
1460 return (NULL);
1461 }
1462 strncpy (newname, name, len);
075caafd
ILT
1463 newname[len] = '\0';
1464 return newname;
1465}
1466
ae115e51
ILT
1467/* Read in the external symbols. */
1468
1469boolean
1470_bfd_coff_get_external_symbols (abfd)
1471 bfd *abfd;
1472{
1473 bfd_size_type symesz;
1474 size_t size;
1475 PTR syms;
1476
1477 if (obj_coff_external_syms (abfd) != NULL)
1478 return true;
1479
1480 symesz = bfd_coff_symesz (abfd);
1481
1482 size = obj_raw_syment_count (abfd) * symesz;
1483
90a7abbc 1484 syms = (PTR) malloc (size);
ae115e51
ILT
1485 if (syms == NULL && size != 0)
1486 {
1487 bfd_set_error (bfd_error_no_memory);
1488 return false;
1489 }
1490
1491 if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
1492 || bfd_read (syms, size, 1, abfd) != size)
1493 {
1494 if (syms != NULL)
1495 free (syms);
1496 return false;
1497 }
1498
1499 obj_coff_external_syms (abfd) = syms;
1500
1501 return true;
1502}
1503
1504/* Read in the external strings. The strings are not loaded until
1505 they are needed. This is because we have no simple way of
1506 detecting a missing string table in an archive. */
1507
1508const char *
1509_bfd_coff_read_string_table (abfd)
1510 bfd *abfd;
1511{
1512 char extstrsize[STRING_SIZE_SIZE];
1513 size_t strsize;
1514 char *strings;
1515
1516 if (obj_coff_strings (abfd) != NULL)
1517 return obj_coff_strings (abfd);
1518
1519 if (bfd_seek (abfd,
1520 (obj_sym_filepos (abfd)
1521 + obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd)),
1522 SEEK_SET) != 0)
1523 return NULL;
1524
1525 if (bfd_read (extstrsize, sizeof extstrsize, 1, abfd) != sizeof extstrsize)
1526 {
1527 if (bfd_get_error () != bfd_error_file_truncated)
1528 return NULL;
1529
1530 /* There is no string table. */
1531 strsize = STRING_SIZE_SIZE;
1532 }
1533 else
1534 {
1535#if STRING_SIZE_SIZE == 4
1536 strsize = bfd_h_get_32 (abfd, (bfd_byte *) extstrsize);
1537#else
1538 #error Change bfd_h_get_32
1539#endif
1540 }
1541
90a7abbc
ILT
1542 if (strsize < STRING_SIZE_SIZE)
1543 {
1544 (*_bfd_error_handler)
1545 ("%s: bad string table size %lu", bfd_get_filename (abfd),
1546 (unsigned long) strsize);
1547 bfd_set_error (bfd_error_bad_value);
1548 return NULL;
1549 }
1550
1551 strings = (char *) malloc (strsize);
ae115e51
ILT
1552 if (strings == NULL)
1553 {
1554 bfd_set_error (bfd_error_no_memory);
1555 return NULL;
1556 }
1557
1558 if (bfd_read (strings + STRING_SIZE_SIZE,
1559 strsize - STRING_SIZE_SIZE, 1, abfd)
1560 != strsize - STRING_SIZE_SIZE)
1561 {
1562 free (strings);
1563 return NULL;
1564 }
1565
1566 obj_coff_strings (abfd) = strings;
1567
1568 return strings;
1569}
1570
1571/* Free up the external symbols and strings read from a COFF file. */
1572
1573boolean
1574_bfd_coff_free_symbols (abfd)
1575 bfd *abfd;
1576{
1577 if (obj_coff_external_syms (abfd) != NULL
1578 && ! obj_coff_keep_syms (abfd))
1579 {
1580 free (obj_coff_external_syms (abfd));
1581 obj_coff_external_syms (abfd) = NULL;
1582 }
1583 if (obj_coff_strings (abfd) != NULL
1584 && ! obj_coff_keep_strings (abfd))
1585 {
1586 free (obj_coff_strings (abfd));
1587 obj_coff_strings (abfd) = NULL;
1588 }
1589 return true;
1590}
1591
075caafd
ILT
1592/* Read a symbol table into freshly bfd_allocated memory, swap it, and
1593 knit the symbol names into a normalized form. By normalized here I
1594 mean that all symbols have an n_offset pointer that points to a null-
1595 terminated string. */
1596
1597combined_entry_type *
25057836 1598coff_get_normalized_symtab (abfd)
6dc6a81a 1599 bfd *abfd;
075caafd 1600{
6dc6a81a
ILT
1601 combined_entry_type *internal;
1602 combined_entry_type *internal_ptr;
1603 combined_entry_type *symbol_ptr;
1604 combined_entry_type *internal_end;
075caafd 1605 bfd_size_type symesz;
075caafd
ILT
1606 char *raw_src;
1607 char *raw_end;
ae115e51 1608 const char *string_table = NULL;
6dc6a81a
ILT
1609 char *debug_section = NULL;
1610 unsigned long size;
075caafd 1611
74942465
ILT
1612 if (obj_raw_syments (abfd) != NULL)
1613 return obj_raw_syments (abfd);
1614
1615 size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
1616 internal = (combined_entry_type *) bfd_alloc (abfd, size);
1617 if (internal == NULL && size != 0)
9783e04a 1618 {
d1ad85a6 1619 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1620 return NULL;
1621 }
69645d10 1622 internal_end = internal + obj_raw_syment_count (abfd);
075caafd 1623
ae115e51 1624 if (! _bfd_coff_get_external_symbols (abfd))
74942465
ILT
1625 return NULL;
1626
ae115e51
ILT
1627 raw_src = (char *) obj_coff_external_syms (abfd);
1628
075caafd 1629 /* mark the end of the symbols */
ae115e51
ILT
1630 symesz = bfd_coff_symesz (abfd);
1631 raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
74942465 1632
6dc6a81a
ILT
1633 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1634 probably possible. If one shows up, it will probably kill us. */
075caafd
ILT
1635
1636 /* Swap all the raw entries */
ae115e51 1637 for (internal_ptr = internal;
075caafd 1638 raw_src < raw_end;
6dc6a81a
ILT
1639 raw_src += symesz, internal_ptr++)
1640 {
075caafd
ILT
1641
1642 unsigned int i;
6dc6a81a
ILT
1643 bfd_coff_swap_sym_in (abfd, (PTR) raw_src,
1644 (PTR) & internal_ptr->u.syment);
9783e04a 1645 internal_ptr->fix_value = 0;
075caafd
ILT
1646 internal_ptr->fix_tag = 0;
1647 internal_ptr->fix_end = 0;
9783e04a 1648 internal_ptr->fix_scnlen = 0;
075caafd 1649 symbol_ptr = internal_ptr;
85fe7cff 1650
075caafd
ILT
1651 for (i = 0;
1652 i < symbol_ptr->u.syment.n_numaux;
6dc6a81a 1653 i++)
075caafd 1654 {
6dc6a81a
ILT
1655 internal_ptr++;
1656 raw_src += symesz;
1657
1658 internal_ptr->fix_value = 0;
1659 internal_ptr->fix_tag = 0;
1660 internal_ptr->fix_end = 0;
1661 internal_ptr->fix_scnlen = 0;
1662 bfd_coff_swap_aux_in (abfd, (PTR) raw_src,
1663 symbol_ptr->u.syment.n_type,
1664 symbol_ptr->u.syment.n_sclass,
1665 i, symbol_ptr->u.syment.n_numaux,
1666 &(internal_ptr->u.auxent));
c80cc833
ILT
1667 coff_pointerize_aux (abfd, internal, symbol_ptr, i,
1668 internal_ptr);
6dc6a81a 1669 }
075caafd
ILT
1670 }
1671
ae115e51
ILT
1672 /* Free the raw symbols, but not the strings (if we have them). */
1673 obj_coff_keep_strings (abfd) = true;
1674 if (! _bfd_coff_free_symbols (abfd))
1675 return NULL;
075caafd
ILT
1676
1677 for (internal_ptr = internal; internal_ptr < internal_end;
6dc6a81a
ILT
1678 internal_ptr++)
1679 {
1680 if (internal_ptr->u.syment.n_sclass == C_FILE
1681 && internal_ptr->u.syment.n_numaux > 0)
1682 {
1683 /* make a file symbol point to the name in the auxent, since
1684 the text ".file" is redundant */
1685 if ((internal_ptr + 1)->u.auxent.x_file.x_n.x_zeroes == 0)
1686 {
1687 /* the filename is a long one, point into the string table */
1688 if (string_table == NULL)
ae115e51
ILT
1689 {
1690 string_table = _bfd_coff_read_string_table (abfd);
1691 if (string_table == NULL)
1692 return NULL;
1693 }
075caafd 1694
6dc6a81a 1695 internal_ptr->u.syment._n._n_n._n_offset =
ae115e51
ILT
1696 ((long)
1697 (string_table
1698 + (internal_ptr + 1)->u.auxent.x_file.x_n.x_offset));
6dc6a81a
ILT
1699 }
1700 else
1701 {
1702 /* ordinary short filename, put into memory anyway */
1703 internal_ptr->u.syment._n._n_n._n_offset = (long)
1704 copy_name (abfd, (internal_ptr + 1)->u.auxent.x_file.x_fname,
1705 FILNMLEN);
1706 }
1707 }
1708 else
1709 {
1710 if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
1711 {
1712 /* This is a "short" name. Make it long. */
1713 unsigned long i = 0;
1714 char *newstring = NULL;
1715
1716 /* find the length of this string without walking into memory
1717 that isn't ours. */
1718 for (i = 0; i < 8; ++i)
1719 {
1720 if (internal_ptr->u.syment._n._n_name[i] == '\0')
1721 {
1722 break;
1723 } /* if end of string */
1724 } /* possible lengths of this string. */
1725
1726 if ((newstring = (PTR) bfd_alloc (abfd, ++i)) == NULL)
1727 {
1728 bfd_set_error (bfd_error_no_memory);
1729 return (NULL);
1730 } /* on error */
1731 memset (newstring, 0, i);
1732 strncpy (newstring, internal_ptr->u.syment._n._n_name, i - 1);
1733 internal_ptr->u.syment._n._n_n._n_offset = (long int) newstring;
1734 internal_ptr->u.syment._n._n_n._n_zeroes = 0;
1735 }
1736 else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
1737 internal_ptr->u.syment._n._n_n._n_offset = (long int) "";
1738 else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
1739 {
1740 /* Long name already. Point symbol at the string in the
1741 table. */
1742 if (string_table == NULL)
ae115e51
ILT
1743 {
1744 string_table = _bfd_coff_read_string_table (abfd);
1745 if (string_table == NULL)
1746 return NULL;
1747 }
1748 internal_ptr->u.syment._n._n_n._n_offset =
1749 ((long int)
1750 (string_table
1751 + internal_ptr->u.syment._n._n_n._n_offset));
6dc6a81a
ILT
1752 }
1753 else
1754 {
1755 /* Long name in debug section. Very similar. */
1756 if (debug_section == NULL)
1757 debug_section = build_debug_section (abfd);
1758 internal_ptr->u.syment._n._n_n._n_offset = (long int)
1759 (debug_section + internal_ptr->u.syment._n._n_n._n_offset);
1760 }
1761 }
1762 internal_ptr += internal_ptr->u.syment.n_numaux;
1763 }
1764
1765 obj_raw_syments (abfd) = internal;
ae115e51
ILT
1766 BFD_ASSERT (obj_raw_syment_count (abfd)
1767 == (unsigned int) (internal_ptr - internal));
075caafd
ILT
1768
1769 return (internal);
1770} /* coff_get_normalized_symtab() */
1771
326e32d7 1772long
25057836 1773coff_get_reloc_upper_bound (abfd, asect)
6dc6a81a
ILT
1774 bfd *abfd;
1775 sec_ptr asect;
075caafd 1776{
6dc6a81a
ILT
1777 if (bfd_get_format (abfd) != bfd_object)
1778 {
1779 bfd_set_error (bfd_error_invalid_operation);
1780 return -1;
1781 }
1782 return (asect->reloc_count + 1) * sizeof (arelent *);
075caafd
ILT
1783}
1784
1785asymbol *
25057836 1786coff_make_empty_symbol (abfd)
6dc6a81a 1787 bfd *abfd;
075caafd 1788{
6dc6a81a
ILT
1789 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1790 if (new == NULL)
1791 {
1792 bfd_set_error (bfd_error_no_memory);
1793 return (NULL);
1794 } /* on error */
9783e04a 1795 memset (new, 0, sizeof *new);
075caafd
ILT
1796 new->symbol.section = 0;
1797 new->native = 0;
1798 new->lineno = (alent *) NULL;
1799 new->done_lineno = false;
1800 new->symbol.the_bfd = abfd;
1801 return &new->symbol;
1802}
1803
2d1e6c9c
KR
1804/* Make a debugging symbol. */
1805
075caafd 1806asymbol *
2d1e6c9c
KR
1807coff_bfd_make_debug_symbol (abfd, ptr, sz)
1808 bfd *abfd;
1809 PTR ptr;
1810 unsigned long sz;
075caafd 1811{
6dc6a81a
ILT
1812 coff_symbol_type *new = (coff_symbol_type *) bfd_alloc (abfd, sizeof (coff_symbol_type));
1813 if (new == NULL)
1814 {
1815 bfd_set_error (bfd_error_no_memory);
1816 return (NULL);
1817 } /* on error */
075caafd
ILT
1818 /* @@ This shouldn't be using a constant multiplier. */
1819 new->native = (combined_entry_type *) bfd_zalloc (abfd, sizeof (combined_entry_type) * 10);
9783e04a
DM
1820 if (!new->native)
1821 {
d1ad85a6 1822 bfd_set_error (bfd_error_no_memory);
9783e04a
DM
1823 return (NULL);
1824 } /* on error */
6dc6a81a
ILT
1825 new->symbol.section = bfd_abs_section_ptr;
1826 new->symbol.flags = BSF_DEBUGGING;
075caafd
ILT
1827 new->lineno = (alent *) NULL;
1828 new->done_lineno = false;
1829 new->symbol.the_bfd = abfd;
1830 return &new->symbol;
1831}
1832
6dc6a81a 1833/*ARGSUSED */
2d1e6c9c
KR
1834void
1835coff_get_symbol_info (abfd, symbol, ret)
1836 bfd *abfd;
1837 asymbol *symbol;
1838 symbol_info *ret;
1839{
1840 bfd_symbol_info (symbol, ret);
867d923d
ILT
1841 if (coffsymbol (symbol)->native != NULL
1842 && coffsymbol (symbol)->native->fix_value)
1843 {
1844 combined_entry_type *psym;
1845
1846 psym = ((combined_entry_type *)
1847 coffsymbol (symbol)->native->u.syment.n_value);
1848 ret->value = (bfd_vma) (psym - obj_raw_syments (abfd));
1849 }
2d1e6c9c
KR
1850}
1851
e61cfdf8
ILT
1852/* Print out information about COFF symbol. */
1853
075caafd 1854void
e61cfdf8
ILT
1855coff_print_symbol (abfd, filep, symbol, how)
1856 bfd *abfd;
1857 PTR filep;
1858 asymbol *symbol;
1859 bfd_print_symbol_type how;
075caafd 1860{
e61cfdf8
ILT
1861 FILE *file = (FILE *) filep;
1862
1863 switch (how)
1864 {
075caafd 1865 case bfd_print_symbol_name:
e61cfdf8 1866 fprintf (file, "%s", symbol->name);
075caafd 1867 break;
e61cfdf8 1868
075caafd 1869 case bfd_print_symbol_more:
e61cfdf8 1870 fprintf (file, "coff %s %s",
6dc6a81a
ILT
1871 coffsymbol (symbol)->native ? "n" : "g",
1872 coffsymbol (symbol)->lineno ? "l" : " ");
075caafd 1873 break;
075caafd 1874
e61cfdf8 1875 case bfd_print_symbol_all:
6dc6a81a 1876 if (coffsymbol (symbol)->native)
075caafd 1877 {
49488f2b 1878 unsigned long val;
e61cfdf8
ILT
1879 unsigned int aux;
1880 combined_entry_type *combined = coffsymbol (symbol)->native;
1881 combined_entry_type *root = obj_raw_syments (abfd);
6dc6a81a
ILT
1882 struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
1883
1884 fprintf (file, "[%3ld]", (long) (combined - root));
e61cfdf8 1885
49488f2b
ILT
1886 if (! combined->fix_value)
1887 val = (unsigned long) combined->u.syment.n_value;
1888 else
1889 val = ((unsigned long)
1890 ((combined_entry_type *) combined->u.syment.n_value
1891 - root));
1892
e61cfdf8 1893 fprintf (file,
6dc6a81a 1894 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
e61cfdf8
ILT
1895 combined->u.syment.n_scnum,
1896 combined->u.syment.n_flags,
1897 combined->u.syment.n_type,
1898 combined->u.syment.n_sclass,
1899 combined->u.syment.n_numaux,
49488f2b 1900 val,
e61cfdf8
ILT
1901 symbol->name);
1902
6dc6a81a 1903 for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
e61cfdf8
ILT
1904 {
1905 combined_entry_type *auxp = combined + aux + 1;
1906 long tagndx;
1907
1908 if (auxp->fix_tag)
1909 tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
1910 else
1911 tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
1912
1913 fprintf (file, "\n");
0fc9ada9
ILT
1914
1915 if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
1916 continue;
1917
e61cfdf8
ILT
1918 switch (combined->u.syment.n_sclass)
1919 {
1920 case C_FILE:
1921 fprintf (file, "File ");
1922 break;
e61cfdf8 1923
de733a0e
KR
1924 case C_STAT:
1925 if (combined->u.syment.n_type == T_NULL)
1926 /* probably a section symbol? */
1927 {
1928 fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1929 (long) auxp->u.auxent.x_scn.x_scnlen,
1930 auxp->u.auxent.x_scn.x_nreloc,
1931 auxp->u.auxent.x_scn.x_nlinno);
1932 break;
1933 }
1934 /* else fall through */
1935
1936 default:
4c3721d5 1937 fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
e61cfdf8
ILT
1938 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
1939 auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
1940 tagndx);
69645d10
ILT
1941 if (auxp->fix_end)
1942 fprintf (file, " endndx %ld",
1943 ((long)
1944 (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
1945 - root)));
e61cfdf8
ILT
1946 break;
1947 }
075caafd 1948 }
6dc6a81a 1949
e61cfdf8
ILT
1950 if (l)
1951 {
e4b6b3e7 1952 fprintf (file, "\n%s :", l->u.sym->name);
e61cfdf8 1953 l++;
6dc6a81a 1954 while (l->line_number)
e61cfdf8 1955 {
4c3721d5
ILT
1956 fprintf (file, "\n%4d : 0x%lx",
1957 l->line_number,
1958 ((unsigned long)
1959 (l->u.offset + symbol->section->vma)));
e61cfdf8
ILT
1960 l++;
1961 }
1962 }
6dc6a81a 1963 }
e61cfdf8 1964 else
075caafd 1965 {
e61cfdf8
ILT
1966 bfd_print_symbol_vandf ((PTR) file, symbol);
1967 fprintf (file, " %-5s %s %s %s",
1968 symbol->section->name,
6dc6a81a
ILT
1969 coffsymbol (symbol)->native ? "n" : "g",
1970 coffsymbol (symbol)->lineno ? "l" : " ",
e61cfdf8 1971 symbol->name);
075caafd 1972 }
075caafd
ILT
1973 }
1974}
1975
1976/* Provided a BFD, a section and an offset into the section, calculate
1977 and return the name of the source file and the line nearest to the
1978 wanted location. */
1979
330595d0 1980/*ARGSUSED*/
075caafd 1981boolean
25057836
JL
1982coff_find_nearest_line (abfd, section, ignore_symbols, offset, filename_ptr,
1983 functionname_ptr, line_ptr)
6dc6a81a
ILT
1984 bfd *abfd;
1985 asection *section;
1986 asymbol **ignore_symbols;
1987 bfd_vma offset;
1988 CONST char **filename_ptr;
1989 CONST char **functionname_ptr;
1990 unsigned int *line_ptr;
075caafd 1991{
a74d1517 1992 unsigned int i;
25b5a53d 1993 unsigned int line_base;
6dc6a81a 1994 coff_data_type *cof = coff_data (abfd);
075caafd
ILT
1995 /* Run through the raw syments if available */
1996 combined_entry_type *p;
a74d1517 1997 combined_entry_type *pend;
6dc6a81a 1998 alent *l;
25b5a53d 1999 struct coff_section_tdata *sec_data;
075caafd
ILT
2000
2001 *filename_ptr = 0;
2002 *functionname_ptr = 0;
2003 *line_ptr = 0;
2004
2005 /* Don't try and find line numbers in a non coff file */
2006 if (abfd->xvec->flavour != bfd_target_coff_flavour)
2007 return false;
2008
2009 if (cof == NULL)
2010 return false;
2011
a74d1517 2012 /* Find the first C_FILE symbol. */
075caafd 2013 p = cof->raw_syments;
a74d1517
ILT
2014 pend = p + cof->raw_syment_count;
2015 while (p < pend)
2016 {
2017 if (p->u.syment.n_sclass == C_FILE)
2018 break;
2019 p += 1 + p->u.syment.n_numaux;
2020 }
075caafd 2021
a74d1517
ILT
2022 if (p < pend)
2023 {
6d04c6d4
ILT
2024 bfd_vma maxdiff;
2025
a74d1517 2026 /* Look through the C_FILE symbols to find the best one. */
075caafd 2027 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
6d04c6d4 2028 maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
a74d1517
ILT
2029 while (1)
2030 {
2031 combined_entry_type *p2;
2032
6d04c6d4
ILT
2033 for (p2 = p + 1 + p->u.syment.n_numaux;
2034 p2 < pend;
2035 p2 += 1 + p2->u.syment.n_numaux)
a74d1517 2036 {
6d04c6d4
ILT
2037 if (p2->u.syment.n_scnum > 0
2038 && (section
2039 == coff_section_from_bfd_index (abfd,
2040 p2->u.syment.n_scnum)))
a74d1517 2041 break;
6d04c6d4
ILT
2042 if (p2->u.syment.n_sclass == C_FILE)
2043 {
2044 p2 = pend;
2045 break;
2046 }
a74d1517 2047 }
6d04c6d4 2048
a74d1517 2049 if (p2 < pend
ae115e51
ILT
2050 && offset >= (bfd_vma) p2->u.syment.n_value
2051 && offset - (bfd_vma) p2->u.syment.n_value < maxdiff)
6d04c6d4
ILT
2052 {
2053 *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
2054 maxdiff = offset - p2->u.syment.n_value;
2055 }
2056
2057 /* Avoid endless loops on erroneous files by ensuring that
2058 we always move forward in the file. */
2059 if (p - cof->raw_syments >= p->u.syment.n_value)
a74d1517
ILT
2060 break;
2061
6d04c6d4
ILT
2062 p = cof->raw_syments + p->u.syment.n_value;
2063 if (p > pend || p->u.syment.n_sclass != C_FILE)
2064 break;
a74d1517 2065 }
075caafd 2066 }
a74d1517 2067
075caafd 2068 /* Now wander though the raw linenumbers of the section */
25b5a53d
ILT
2069 /* If we have been called on this section before, and the offset we
2070 want is further down then we can prime the lookup loop. */
2071 sec_data = coff_section_data (abfd, section);
2072 if (sec_data != NULL
2073 && sec_data->i > 0
2074 && offset >= sec_data->offset)
2075 {
2076 i = sec_data->i;
2077 *functionname_ptr = sec_data->function;
2078 line_base = sec_data->line_base;
6dc6a81a
ILT
2079 }
2080 else
2081 {
2082 i = 0;
25b5a53d 2083 line_base = 0;
6dc6a81a 2084 }
25b5a53d 2085
85fe7cff 2086 l = &section->lineno[i];
075caafd 2087
6dc6a81a
ILT
2088 for (; i < section->lineno_count; i++)
2089 {
2090 if (l->line_number == 0)
2091 {
2092 /* Get the symbol this line number points at */
2093 coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
2094 if (coff->symbol.value > offset)
2095 break;
2096 *functionname_ptr = coff->symbol.name;
2097 if (coff->native)
2098 {
2099 combined_entry_type *s = coff->native;
2100 s = s + 1 + s->u.syment.n_numaux;
2101
2102 /* In XCOFF a debugging symbol can follow the function
2103 symbol. */
2104 if (s->u.syment.n_scnum == N_DEBUG)
2105 s = s + 1 + s->u.syment.n_numaux;
2106
2107 /*
2108 S should now point to the .bf of the function
2109 */
2110 if (s->u.syment.n_numaux)
2111 {
2112 /*
2113 The linenumber is stored in the auxent
2114 */
2115 union internal_auxent *a = &((s + 1)->u.auxent);
2116 line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
2117 *line_ptr = line_base;
2118 }
2119 }
075caafd 2120 }
6dc6a81a
ILT
2121 else
2122 {
2123 if (l->u.offset + bfd_get_section_vma (abfd, section) > offset)
2124 break;
2125 *line_ptr = l->line_number + line_base - 1;
2126 }
2127 l++;
075caafd 2128 }
075caafd 2129
25b5a53d
ILT
2130 /* Cache the results for the next call. */
2131 if (sec_data == NULL)
2132 {
2133 section->used_by_bfd =
2134 ((PTR) bfd_zalloc (abfd,
2135 sizeof (struct coff_section_tdata)));
867d923d 2136 sec_data = (struct coff_section_tdata *) section->used_by_bfd;
25b5a53d
ILT
2137 }
2138 if (sec_data != NULL)
2139 {
2140 sec_data->offset = offset;
2141 sec_data->i = i;
2142 sec_data->function = *functionname_ptr;
2143 sec_data->line_base = line_base;
2144 }
075caafd
ILT
2145
2146 return true;
2147}
2148
2149int
25057836
JL
2150coff_sizeof_headers (abfd, reloc)
2151 bfd *abfd;
2152 boolean reloc;
075caafd 2153{
6dc6a81a 2154 size_t size;
075caafd 2155
6dc6a81a
ILT
2156 if (reloc == false)
2157 {
2158 size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
075caafd 2159 }
6dc6a81a
ILT
2160 else
2161 {
2162 size = bfd_coff_filhsz (abfd);
075caafd
ILT
2163 }
2164
6dc6a81a
ILT
2165 size += abfd->section_count * bfd_coff_scnhsz (abfd);
2166 return size;
075caafd 2167}
This page took 0.254263 seconds and 4 git commands to generate.