1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 1995 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
22 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
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
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
41 #include "coff/internal.h"
44 static void coff_fix_symbol_name
45 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_size_type
*,
46 asection
**, bfd_size_type
*));
47 static boolean coff_write_symbol
48 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, unsigned int *,
49 bfd_size_type
*, asection
**, bfd_size_type
*));
50 static boolean coff_write_alien_symbol
51 PARAMS ((bfd
*, asymbol
*, unsigned int *, bfd_size_type
*,
52 asection
**, bfd_size_type
*));
53 static boolean coff_write_native_symbol
54 PARAMS ((bfd
*, coff_symbol_type
*, unsigned int *, bfd_size_type
*,
55 asection
**, bfd_size_type
*));
56 static void coff_pointerize_aux
57 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
58 unsigned int, combined_entry_type
*));
60 #define STRING_SIZE_SIZE (4)
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. */
65 make_a_section_from_file (abfd
, hdr
, target_index
)
67 struct internal_scnhdr
*hdr
;
68 unsigned int target_index
;
70 asection
*return_section
;
73 /* Assorted wastage to null-terminate the name, thanks AT&T! */
74 name
= bfd_alloc (abfd
, sizeof (hdr
->s_name
) + 1);
77 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
78 name
[sizeof (hdr
->s_name
)] = 0;
80 return_section
= bfd_make_section_anyway (abfd
, name
);
81 if (return_section
== NULL
)
84 /* s_paddr is presumed to be = to s_vaddr */
86 return_section
->vma
= hdr
->s_vaddr
;
87 return_section
->lma
= return_section
->vma
;
88 return_section
->_raw_size
= hdr
->s_size
;
89 return_section
->filepos
= hdr
->s_scnptr
;
90 return_section
->rel_filepos
= hdr
->s_relptr
;
91 return_section
->reloc_count
= hdr
->s_nreloc
;
93 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
95 return_section
->line_filepos
= hdr
->s_lnnoptr
;
97 return_section
->lineno_count
= hdr
->s_nlnno
;
98 return_section
->userdata
= NULL
;
99 return_section
->next
= (asection
*) NULL
;
100 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
102 return_section
->target_index
= target_index
;
104 /* At least on i386-coff, the line number count for a shared library
105 section must be ignored. */
106 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
107 return_section
->lineno_count
= 0;
109 if (hdr
->s_nreloc
!= 0)
110 return_section
->flags
|= SEC_RELOC
;
111 /* FIXME: should this check 'hdr->s_size > 0' */
112 if (hdr
->s_scnptr
!= 0)
113 return_section
->flags
|= SEC_HAS_CONTENTS
;
117 /* Read in a COFF object and make it into a BFD. This is used by
120 static const bfd_target
*
121 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
124 struct internal_filehdr
*internal_f
;
125 struct internal_aouthdr
*internal_a
;
127 flagword oflags
= abfd
->flags
;
128 bfd_vma ostart
= bfd_get_start_address (abfd
);
130 size_t readsize
; /* length of file_info */
132 char *external_sections
;
134 if (!(internal_f
->f_flags
& F_RELFLG
))
135 abfd
->flags
|= HAS_RELOC
;
136 if ((internal_f
->f_flags
& F_EXEC
))
137 abfd
->flags
|= EXEC_P
;
138 if (!(internal_f
->f_flags
& F_LNNO
))
139 abfd
->flags
|= HAS_LINENO
;
140 if (!(internal_f
->f_flags
& F_LSYMS
))
141 abfd
->flags
|= HAS_LOCALS
;
143 /* FIXME: How can we set D_PAGED correctly? */
144 if ((internal_f
->f_flags
& F_EXEC
) != 0)
145 abfd
->flags
|= D_PAGED
;
147 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
148 if (internal_f
->f_nsyms
)
149 abfd
->flags
|= HAS_SYMS
;
151 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
152 bfd_get_start_address (abfd
) = internal_a
->entry
;
154 bfd_get_start_address (abfd
) = 0;
156 /* Set up the tdata area. ECOFF uses its own routine, and overrides
158 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
162 scnhsz
= bfd_coff_scnhsz (abfd
);
163 readsize
= nscns
* scnhsz
;
164 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
165 if (!external_sections
)
168 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
171 /* Now copy data as required; construct all asections etc */
175 for (i
= 0; i
< nscns
; i
++)
177 struct internal_scnhdr tmp
;
178 bfd_coff_swap_scnhdr_in (abfd
,
179 (PTR
) (external_sections
+ i
* scnhsz
),
181 make_a_section_from_file (abfd
, &tmp
, i
+ 1);
185 /* make_abs_section (abfd); */
187 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
193 bfd_release (abfd
, tdata
);
194 abfd
->flags
= oflags
;
195 bfd_get_start_address (abfd
) = ostart
;
196 return (const bfd_target
*) NULL
;
199 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
200 not a COFF file. This is also used by ECOFF. */
210 struct internal_filehdr internal_f
;
211 struct internal_aouthdr internal_a
;
213 /* figure out how much to read */
214 filhsz
= bfd_coff_filhsz (abfd
);
215 aoutsz
= bfd_coff_aoutsz (abfd
);
217 filehdr
= bfd_alloc (abfd
, filhsz
);
220 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
222 if (bfd_get_error () != bfd_error_system_call
)
223 bfd_set_error (bfd_error_wrong_format
);
226 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
227 bfd_release (abfd
, filehdr
);
229 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
231 bfd_set_error (bfd_error_wrong_format
);
234 nscns
= internal_f
.f_nscns
;
236 if (internal_f
.f_opthdr
)
240 opthdr
= bfd_alloc (abfd
, aoutsz
);
243 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
247 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
250 /* Seek past the opt hdr stuff */
251 if (bfd_seek (abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
255 return coff_real_object_p (abfd
, nscns
, &internal_f
,
256 (internal_f
.f_opthdr
!= 0
258 : (struct internal_aouthdr
*) NULL
));
261 /* Get the BFD section from a COFF symbol section number. */
264 coff_section_from_bfd_index (abfd
, index
)
268 struct sec
*answer
= abfd
->sections
;
271 return bfd_abs_section_ptr
;
272 if (index
== N_UNDEF
)
273 return bfd_und_section_ptr
;
274 if (index
== N_DEBUG
)
275 return bfd_abs_section_ptr
;
279 if (answer
->target_index
== index
)
281 answer
= answer
->next
;
284 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
285 has a bad symbol table in biglitpow.o. */
286 return bfd_und_section_ptr
;
289 /* Get the upper bound of a COFF symbol table. */
292 coff_get_symtab_upper_bound (abfd
)
295 if (!bfd_coff_slurp_symbol_table (abfd
))
298 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
302 /* Canonicalize a COFF symbol table. */
305 coff_get_symtab (abfd
, alocation
)
309 unsigned int counter
;
310 coff_symbol_type
*symbase
;
311 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
313 if (!bfd_coff_slurp_symbol_table (abfd
))
316 symbase
= obj_symbols (abfd
);
317 counter
= bfd_get_symcount (abfd
);
318 while (counter
-- > 0)
319 *location
++ = symbase
++;
323 return bfd_get_symcount (abfd
);
326 /* Get the name of a symbol. The caller must pass in a buffer of size
330 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
332 const struct internal_syment
*sym
;
335 /* FIXME: It's not clear this will work correctly if sizeof
337 if (sym
->_n
._n_n
._n_zeroes
!= 0
338 || sym
->_n
._n_n
._n_offset
== 0)
340 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
341 buf
[SYMNMLEN
] = '\0';
348 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
349 strings
= obj_coff_strings (abfd
);
352 strings
= _bfd_coff_read_string_table (abfd
);
356 return strings
+ sym
->_n
._n_n
._n_offset
;
360 /* Read in and swap the relocs. This returns a buffer holding the
361 relocs for section SEC in file ABFD. If CACHE is true and
362 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
363 the function is called again. If EXTERNAL_RELOCS is not NULL, it
364 is a buffer large enough to hold the unswapped relocs. If
365 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
366 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
367 value must be INTERNAL_RELOCS. The function returns NULL on error. */
369 struct internal_reloc
*
370 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
371 require_internal
, internal_relocs
)
375 bfd_byte
*external_relocs
;
376 boolean require_internal
;
377 struct internal_reloc
*internal_relocs
;
380 bfd_byte
*free_external
= NULL
;
381 struct internal_reloc
*free_internal
= NULL
;
384 struct internal_reloc
*irel
;
386 if (coff_section_data (abfd
, sec
) != NULL
387 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
389 if (! require_internal
)
390 return coff_section_data (abfd
, sec
)->relocs
;
391 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
392 sec
->reloc_count
* sizeof (struct internal_reloc
));
393 return internal_relocs
;
396 relsz
= bfd_coff_relsz (abfd
);
398 if (external_relocs
== NULL
)
400 free_external
= (bfd_byte
*) malloc (sec
->reloc_count
* relsz
);
401 if (free_external
== NULL
&& sec
->reloc_count
> 0)
403 bfd_set_error (bfd_error_no_memory
);
406 external_relocs
= free_external
;
409 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
410 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
411 != relsz
* sec
->reloc_count
))
414 if (internal_relocs
== NULL
)
416 free_internal
= ((struct internal_reloc
*)
417 malloc (sec
->reloc_count
418 * sizeof (struct internal_reloc
)));
419 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
421 bfd_set_error (bfd_error_no_memory
);
424 internal_relocs
= free_internal
;
427 /* Swap in the relocs. */
428 erel
= external_relocs
;
429 erel_end
= erel
+ relsz
* sec
->reloc_count
;
430 irel
= internal_relocs
;
431 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
432 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
434 if (free_external
!= NULL
)
436 free (free_external
);
437 free_external
= NULL
;
440 if (cache
&& free_internal
!= NULL
)
442 if (coff_section_data (abfd
, sec
) == NULL
)
445 (PTR
) bfd_zalloc (abfd
,
446 sizeof (struct coff_section_tdata
));
447 if (sec
->used_by_bfd
== NULL
)
449 coff_section_data (abfd
, sec
)->contents
= NULL
;
451 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
454 return internal_relocs
;
457 if (free_external
!= NULL
)
458 free (free_external
);
459 if (free_internal
!= NULL
)
460 free (free_internal
);
464 /* Set lineno_count for the output sections of a COFF file. */
467 coff_count_linenumbers (abfd
)
470 unsigned int limit
= bfd_get_symcount (abfd
);
478 /* This may be from the backend linker, in which case the
479 lineno_count in the sections is correct. */
480 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
481 total
+= s
->lineno_count
;
485 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
486 BFD_ASSERT (s
->lineno_count
== 0);
488 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
490 asymbol
*q_maybe
= *p
;
492 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
494 coff_symbol_type
*q
= coffsymbol (q_maybe
);
496 /* The AIX 4.1 compiler can sometimes generate line numbers
497 attached to debugging symbols. We try to simply ignore
499 if (q
->lineno
!= NULL
500 && q
->symbol
.section
->owner
!= NULL
)
502 /* This symbol has line numbers. Increment the owning
503 section's linenumber count. */
504 alent
*l
= q
->lineno
;
506 ++q
->symbol
.section
->output_section
->lineno_count
;
509 while (l
->line_number
!= 0)
512 ++q
->symbol
.section
->output_section
->lineno_count
;
522 /* Takes a bfd and a symbol, returns a pointer to the coff specific
523 area of the symbol if there is one. */
527 coff_symbol_from (ignore_abfd
, symbol
)
531 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
532 return (coff_symbol_type
*) NULL
;
534 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
535 return (coff_symbol_type
*) NULL
;
537 return (coff_symbol_type
*) symbol
;
541 fixup_symbol_value (coff_symbol_ptr
, syment
)
542 coff_symbol_type
*coff_symbol_ptr
;
543 struct internal_syment
*syment
;
546 /* Normalize the symbol flags */
547 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
549 /* a common symbol is undefined with a value */
550 syment
->n_scnum
= N_UNDEF
;
551 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
553 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
555 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
557 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
559 syment
->n_scnum
= N_UNDEF
;
564 if (coff_symbol_ptr
->symbol
.section
)
567 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
570 coff_symbol_ptr
->symbol
.value
+
571 coff_symbol_ptr
->symbol
.section
->output_offset
+
572 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
577 /* This can happen, but I don't know why yet (steve@cygnus.com) */
578 syment
->n_scnum
= N_ABS
;
579 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
584 /* Run through all the symbols in the symbol table and work out what
585 their indexes into the symbol table will be when output.
587 Coff requires that each C_FILE symbol points to the next one in the
588 chain, and that the last one points to the first external symbol. We
592 coff_renumber_symbols (bfd_ptr
, first_undef
)
596 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
597 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
598 unsigned int native_index
= 0;
599 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
600 unsigned int symbol_index
;
602 /* COFF demands that undefined symbols come after all other symbols.
603 Since we don't need to impose this extra knowledge on all our
604 client programs, deal with that here. Sort the symbol table;
605 just move the undefined symbols to the end, leaving the rest
606 alone. The O'Reilly book says that defined global symbols come
607 at the end before the undefined symbols, so we do that here as
609 /* @@ Do we have some condition we could test for, so we don't always
610 have to do this? I don't think relocatability is quite right, but
611 I'm not certain. [raeburn:19920508.1711EST] */
616 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
618 * (symbol_count
+ 1));
621 bfd_ptr
->outsymbols
= newsyms
;
622 for (i
= 0; i
< symbol_count
; i
++)
623 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
624 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
625 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
626 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
628 *newsyms
++ = symbol_ptr_ptr
[i
];
630 for (i
= 0; i
< symbol_count
; i
++)
631 if (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
632 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
633 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
637 *newsyms
++ = symbol_ptr_ptr
[i
];
639 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
641 for (i
= 0; i
< symbol_count
; i
++)
642 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
643 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
644 *newsyms
++ = symbol_ptr_ptr
[i
];
645 *newsyms
= (asymbol
*) NULL
;
646 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
649 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
651 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
652 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
653 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
655 combined_entry_type
*s
= coff_symbol_ptr
->native
;
658 if (s
->u
.syment
.n_sclass
== C_FILE
)
660 if (last_file
!= (struct internal_syment
*) NULL
)
661 last_file
->n_value
= native_index
;
662 last_file
= &(s
->u
.syment
);
667 /* Modify the symbol values according to their section and
670 fixup_symbol_value (coff_symbol_ptr
, &(s
->u
.syment
));
672 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
673 s
[i
].offset
= native_index
++;
680 obj_conv_table_size (bfd_ptr
) = native_index
;
685 /* Run thorough the symbol table again, and fix it so that all
686 pointers to entries are changed to the entries' index in the output
690 coff_mangle_symbols (bfd_ptr
)
693 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
694 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
695 unsigned int symbol_index
;
697 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
699 coff_symbol_type
*coff_symbol_ptr
=
700 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
702 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
705 combined_entry_type
*s
= coff_symbol_ptr
->native
;
709 /* FIXME: We should use a union here. */
710 s
->u
.syment
.n_value
=
711 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
716 /* The value is the offset into the line number entries
717 for the symbol's section. On output, the symbol's
718 section should be N_DEBUG. */
719 s
->u
.syment
.n_value
=
720 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
721 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
722 coff_symbol_ptr
->symbol
.section
=
723 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
724 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
726 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
728 combined_entry_type
*a
= s
+ i
+ 1;
731 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
732 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
737 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
738 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
743 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
744 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
753 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
754 debug_string_section_p
, debug_string_size_p
)
757 combined_entry_type
*native
;
758 bfd_size_type
*string_size_p
;
759 asection
**debug_string_section_p
;
760 bfd_size_type
*debug_string_size_p
;
762 unsigned int name_length
;
763 union internal_auxent
*auxent
;
764 char *name
= (char *) (symbol
->name
);
766 if (name
== (char *) NULL
)
768 /* coff symbols always have names, so we'll make one up */
769 symbol
->name
= "strange";
770 name
= (char *) symbol
->name
;
772 name_length
= strlen (name
);
774 if (native
->u
.syment
.n_sclass
== C_FILE
775 && native
->u
.syment
.n_numaux
> 0)
777 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
778 auxent
= &(native
+ 1)->u
.auxent
;
780 if (bfd_coff_long_filenames (abfd
))
782 if (name_length
<= FILNMLEN
)
784 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
788 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
789 auxent
->x_file
.x_n
.x_zeroes
= 0;
790 *string_size_p
+= name_length
+ 1;
795 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
796 if (name_length
> FILNMLEN
)
798 name
[FILNMLEN
] = '\0';
804 if (name_length
<= SYMNMLEN
)
806 /* This name will fit into the symbol neatly */
807 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
809 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
811 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
813 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
814 *string_size_p
+= name_length
+ 1;
821 /* This name should be written into the .debug section. For
822 some reason each name is preceded by a two byte length
823 and also followed by a null byte. FIXME: We assume that
824 the .debug section has already been created, and that it
826 if (*debug_string_section_p
== (asection
*) NULL
)
827 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
828 filepos
= bfd_tell (abfd
);
829 bfd_put_16 (abfd
, name_length
+ 1, buf
);
830 if (!bfd_set_section_contents (abfd
,
831 *debug_string_section_p
,
833 (file_ptr
) *debug_string_size_p
,
835 || !bfd_set_section_contents (abfd
,
836 *debug_string_section_p
,
838 ((file_ptr
) *debug_string_size_p
840 (bfd_size_type
) name_length
+ 1))
842 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
844 native
->u
.syment
._n
._n_n
._n_offset
= *debug_string_size_p
+ 2;
845 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
846 *debug_string_size_p
+= name_length
+ 3;
851 /* We need to keep track of the symbol index so that when we write out
852 the relocs we can get the index for a symbol. This method is a
855 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
857 /* Write a symbol out to a COFF file. */
860 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
861 debug_string_section_p
, debug_string_size_p
)
864 combined_entry_type
*native
;
865 unsigned int *written
;
866 bfd_size_type
*string_size_p
;
867 asection
**debug_string_section_p
;
868 bfd_size_type
*debug_string_size_p
;
870 unsigned int numaux
= native
->u
.syment
.n_numaux
;
871 int type
= native
->u
.syment
.n_type
;
872 int class = native
->u
.syment
.n_sclass
;
874 bfd_size_type symesz
;
876 if (native
->u
.syment
.n_sclass
== C_FILE
)
877 symbol
->flags
|= BSF_DEBUGGING
;
879 if (symbol
->flags
& BSF_DEBUGGING
880 && bfd_is_abs_section (symbol
->section
))
882 native
->u
.syment
.n_scnum
= N_DEBUG
;
884 else if (bfd_is_abs_section (symbol
->section
))
886 native
->u
.syment
.n_scnum
= N_ABS
;
888 else if (bfd_is_und_section (symbol
->section
))
890 native
->u
.syment
.n_scnum
= N_UNDEF
;
894 native
->u
.syment
.n_scnum
=
895 symbol
->section
->output_section
->target_index
;
898 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
899 debug_string_section_p
, debug_string_size_p
);
901 symesz
= bfd_coff_symesz (abfd
);
902 buf
= bfd_alloc (abfd
, symesz
);
905 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
906 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
908 bfd_release (abfd
, buf
);
910 if (native
->u
.syment
.n_numaux
> 0)
912 bfd_size_type auxesz
;
915 auxesz
= bfd_coff_auxesz (abfd
);
916 buf
= bfd_alloc (abfd
, auxesz
);
919 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
921 bfd_coff_swap_aux_out (abfd
,
922 &((native
+ j
+ 1)->u
.auxent
),
926 native
->u
.syment
.n_numaux
,
928 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
931 bfd_release (abfd
, buf
);
934 /* Store the index for use when we write out the relocs. */
935 set_index (symbol
, *written
);
937 *written
+= numaux
+ 1;
941 /* Write out a symbol to a COFF file that does not come from a COFF
942 file originally. This symbol may have been created by the linker,
943 or we may be linking a non COFF file to a COFF file. */
946 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
947 debug_string_section_p
, debug_string_size_p
)
950 unsigned int *written
;
951 bfd_size_type
*string_size_p
;
952 asection
**debug_string_section_p
;
953 bfd_size_type
*debug_string_size_p
;
955 combined_entry_type
*native
;
956 combined_entry_type dummy
;
959 native
->u
.syment
.n_type
= T_NULL
;
960 native
->u
.syment
.n_flags
= 0;
961 if (bfd_is_und_section (symbol
->section
))
963 native
->u
.syment
.n_scnum
= N_UNDEF
;
964 native
->u
.syment
.n_value
= symbol
->value
;
966 else if (bfd_is_com_section (symbol
->section
))
968 native
->u
.syment
.n_scnum
= N_UNDEF
;
969 native
->u
.syment
.n_value
= symbol
->value
;
971 else if (symbol
->flags
& BSF_DEBUGGING
)
973 /* There isn't much point to writing out a debugging symbol
974 unless we are prepared to convert it into COFF debugging
975 format. So, we just ignore them. We must clobber the symbol
976 name to keep it from being put in the string table. */
982 native
->u
.syment
.n_scnum
=
983 symbol
->section
->output_section
->target_index
;
984 native
->u
.syment
.n_value
= (symbol
->value
985 + symbol
->section
->output_section
->vma
986 + symbol
->section
->output_offset
);
988 /* Copy the any flags from the the file header into the symbol.
991 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
992 if (c
!= (coff_symbol_type
*) NULL
)
993 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
997 native
->u
.syment
.n_type
= 0;
998 if (symbol
->flags
& BSF_LOCAL
)
999 native
->u
.syment
.n_sclass
= C_STAT
;
1001 native
->u
.syment
.n_sclass
= C_EXT
;
1002 native
->u
.syment
.n_numaux
= 0;
1004 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1005 debug_string_section_p
, debug_string_size_p
);
1008 /* Write a native symbol to a COFF file. */
1011 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1012 debug_string_section_p
, debug_string_size_p
)
1014 coff_symbol_type
*symbol
;
1015 unsigned int *written
;
1016 bfd_size_type
*string_size_p
;
1017 asection
**debug_string_section_p
;
1018 bfd_size_type
*debug_string_size_p
;
1020 combined_entry_type
*native
= symbol
->native
;
1021 alent
*lineno
= symbol
->lineno
;
1023 /* If this symbol has an associated line number, we must store the
1024 symbol index in the line number field. We also tag the auxent to
1025 point to the right place in the lineno table. */
1026 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1028 unsigned int count
= 0;
1029 lineno
[count
].u
.offset
= *written
;
1030 if (native
->u
.syment
.n_numaux
)
1032 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1034 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1035 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1038 /* Count and relocate all other linenumbers. */
1040 while (lineno
[count
].line_number
!= 0)
1044 I've been told this, but still need proof:
1045 > The second bug is also in `bfd/coffcode.h'. This bug
1046 > causes the linker to screw up the pc-relocations for
1047 > all the line numbers in COFF code. This bug isn't only
1048 > specific to A29K implementations, but affects all
1049 > systems using COFF format binaries. Note that in COFF
1050 > object files, the line number core offsets output by
1051 > the assembler are relative to the start of each
1052 > procedure, not to the start of the .text section. This
1053 > patch relocates the line numbers relative to the
1054 > `native->u.syment.n_value' instead of the section
1056 > modular!olson@cs.arizona.edu (Jon Olson)
1058 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1060 lineno
[count
].u
.offset
+=
1061 (symbol
->symbol
.section
->output_section
->vma
1062 + symbol
->symbol
.section
->output_offset
);
1066 symbol
->done_lineno
= true;
1068 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1069 count
* bfd_coff_linesz (abfd
);
1072 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1073 string_size_p
, debug_string_section_p
,
1074 debug_string_size_p
);
1077 /* Write out the COFF symbols. */
1080 coff_write_symbols (abfd
)
1083 bfd_size_type string_size
;
1084 asection
*debug_string_section
;
1085 bfd_size_type debug_string_size
;
1087 unsigned int limit
= bfd_get_symcount (abfd
);
1088 unsigned int written
= 0;
1092 debug_string_section
= NULL
;
1093 debug_string_size
= 0;
1095 /* Seek to the right place */
1096 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1099 /* Output all the symbols we have */
1102 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1104 asymbol
*symbol
= *p
;
1105 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1107 if (c_symbol
== (coff_symbol_type
*) NULL
1108 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1110 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1111 &debug_string_section
,
1112 &debug_string_size
))
1117 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1118 &string_size
, &debug_string_section
,
1119 &debug_string_size
))
1124 obj_raw_syment_count (abfd
) = written
;
1126 /* Now write out strings */
1128 if (string_size
!= 0)
1130 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1131 bfd_byte buffer
[STRING_SIZE_SIZE
];
1133 #if STRING_SIZE_SIZE == 4
1134 bfd_h_put_32 (abfd
, size
, buffer
);
1136 #error Change bfd_h_put_32
1138 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
1140 for (p
= abfd
->outsymbols
, i
= 0;
1145 size_t name_length
= strlen (q
->name
);
1146 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1149 /* Figure out whether the symbol name should go in the string
1150 table. Symbol names that are short enough are stored
1151 directly in the syment structure. File names permit a
1152 different, longer, length in the syment structure. On
1153 XCOFF, some symbol names are stored in the .debug section
1154 rather than in the string table. */
1156 if (c_symbol
== NULL
1157 || c_symbol
->native
== NULL
)
1159 /* This is not a COFF symbol, so it certainly is not a
1160 file name, nor does it go in the .debug section. */
1163 else if (bfd_coff_symname_in_debug (abfd
,
1164 &c_symbol
->native
->u
.syment
))
1166 /* This symbol name is in the XCOFF .debug section.
1167 Don't write it into the string table. */
1168 maxlen
= name_length
;
1170 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1171 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1176 if (name_length
> maxlen
)
1178 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1186 /* We would normally not write anything here, but we'll write
1187 out 4 so that any stupid coff reader which tries to read the
1188 string table even when there isn't one won't croak. */
1189 unsigned int size
= STRING_SIZE_SIZE
;
1190 bfd_byte buffer
[STRING_SIZE_SIZE
];
1192 #if STRING_SIZE_SIZE == 4
1193 bfd_h_put_32 (abfd
, size
, buffer
);
1195 #error Change bfd_h_put_32
1197 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1198 != STRING_SIZE_SIZE
)
1202 /* Make sure the .debug section was created to be the correct size.
1203 We should create it ourselves on the fly, but we don't because
1204 BFD won't let us write to any section until we know how large all
1205 the sections are. We could still do it by making another pass
1206 over the symbols. FIXME. */
1207 BFD_ASSERT (debug_string_size
== 0
1208 || (debug_string_section
!= (asection
*) NULL
1209 && (BFD_ALIGN (debug_string_size
,
1210 1 << debug_string_section
->alignment_power
)
1211 == bfd_section_size (abfd
, debug_string_section
))));
1217 coff_write_linenumbers (abfd
)
1221 bfd_size_type linesz
;
1224 linesz
= bfd_coff_linesz (abfd
);
1225 buff
= bfd_alloc (abfd
, linesz
);
1228 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1230 if (s
->lineno_count
)
1232 asymbol
**q
= abfd
->outsymbols
;
1233 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1235 /* Find all the linenumbers in this section */
1239 if (p
->section
->output_section
== s
)
1242 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1243 (bfd_asymbol_bfd (p
), p
));
1246 /* Found a linenumber entry, output */
1247 struct internal_lineno out
;
1248 memset ((PTR
) & out
, 0, sizeof (out
));
1250 out
.l_addr
.l_symndx
= l
->u
.offset
;
1251 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1252 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1255 while (l
->line_number
)
1257 out
.l_lnno
= l
->line_number
;
1258 out
.l_addr
.l_symndx
= l
->u
.offset
;
1259 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1260 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1270 bfd_release (abfd
, buff
);
1276 coff_get_lineno (ignore_abfd
, symbol
)
1280 return coffsymbol (symbol
)->lineno
;
1284 coff_section_symbol (abfd
, name
)
1288 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1290 combined_entry_type
*csym
;
1293 csym
= coff_symbol_from (abfd
, sym
)->native
;
1294 /* Make sure back-end COFF stuff is there. */
1299 coff_symbol_type sym
;
1300 /* @@FIXME This shouldn't use a fixed size!! */
1301 combined_entry_type e
[10];
1304 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
1307 bfd_set_error (bfd_error_no_error
);
1310 memset ((char *) f
, 0, sizeof (*f
));
1311 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1313 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1314 csym
[0].u
.syment
.n_numaux
= 1;
1315 /* SF_SET_STATICS (sym); @@ ??? */
1316 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1317 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1318 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1320 if (sec
->output_section
== NULL
)
1322 sec
->output_section
= sec
;
1323 sec
->output_offset
= 0;
1329 /* This function transforms the offsets into the symbol table into
1330 pointers to syments. */
1333 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1335 combined_entry_type
*table_base
;
1336 combined_entry_type
*symbol
;
1337 unsigned int indaux
;
1338 combined_entry_type
*auxent
;
1340 int type
= symbol
->u
.syment
.n_type
;
1341 int class = symbol
->u
.syment
.n_sclass
;
1343 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1345 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1346 (abfd
, table_base
, symbol
, indaux
, auxent
))
1350 /* Don't bother if this is a file or a section */
1351 if (class == C_STAT
&& type
== T_NULL
)
1353 if (class == C_FILE
)
1356 /* Otherwise patch up */
1357 #define N_TMASK coff_data (abfd)->local_n_tmask
1358 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1359 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
)
1360 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1362 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1363 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1364 auxent
->fix_end
= 1;
1366 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1367 generate one, so we must be careful to ignore it. */
1368 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1370 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1371 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1372 auxent
->fix_tag
= 1;
1376 /* Allocate space for the ".debug" section, and read it.
1377 We did not read the debug section until now, because
1378 we didn't want to go to the trouble until someone needed it. */
1381 build_debug_section (abfd
)
1384 char *debug_section
;
1387 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1391 bfd_set_error (bfd_error_no_debug_section
);
1395 debug_section
= (PTR
) bfd_alloc (abfd
,
1396 bfd_get_section_size_before_reloc (sect
));
1397 if (debug_section
== NULL
)
1400 /* Seek to the beginning of the `.debug' section and read it.
1401 Save the current position first; it is needed by our caller.
1402 Then read debug section and reset the file pointer. */
1404 position
= bfd_tell (abfd
);
1405 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1406 || (bfd_read (debug_section
,
1407 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1408 != bfd_get_section_size_before_reloc (sect
))
1409 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1411 return debug_section
;
1415 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1416 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1417 be \0-terminated. */
1419 copy_name (abfd
, name
, maxlen
)
1427 for (len
= 0; len
< maxlen
; ++len
)
1429 if (name
[len
] == '\0')
1435 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1437 strncpy (newname
, name
, len
);
1438 newname
[len
] = '\0';
1442 /* Read in the external symbols. */
1445 _bfd_coff_get_external_symbols (abfd
)
1448 bfd_size_type symesz
;
1452 if (obj_coff_external_syms (abfd
) != NULL
)
1455 symesz
= bfd_coff_symesz (abfd
);
1457 size
= obj_raw_syment_count (abfd
) * symesz
;
1459 syms
= (PTR
) malloc (size
);
1460 if (syms
== NULL
&& size
!= 0)
1462 bfd_set_error (bfd_error_no_memory
);
1466 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1467 || bfd_read (syms
, size
, 1, abfd
) != size
)
1474 obj_coff_external_syms (abfd
) = syms
;
1479 /* Read in the external strings. The strings are not loaded until
1480 they are needed. This is because we have no simple way of
1481 detecting a missing string table in an archive. */
1484 _bfd_coff_read_string_table (abfd
)
1487 char extstrsize
[STRING_SIZE_SIZE
];
1491 if (obj_coff_strings (abfd
) != NULL
)
1492 return obj_coff_strings (abfd
);
1495 (obj_sym_filepos (abfd
)
1496 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1500 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1502 if (bfd_get_error () != bfd_error_file_truncated
)
1505 /* There is no string table. */
1506 strsize
= STRING_SIZE_SIZE
;
1510 #if STRING_SIZE_SIZE == 4
1511 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1513 #error Change bfd_h_get_32
1517 if (strsize
< STRING_SIZE_SIZE
)
1519 (*_bfd_error_handler
)
1520 ("%s: bad string table size %lu", bfd_get_filename (abfd
),
1521 (unsigned long) strsize
);
1522 bfd_set_error (bfd_error_bad_value
);
1526 strings
= (char *) malloc (strsize
);
1527 if (strings
== NULL
)
1529 bfd_set_error (bfd_error_no_memory
);
1533 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1534 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1535 != strsize
- STRING_SIZE_SIZE
)
1541 obj_coff_strings (abfd
) = strings
;
1546 /* Free up the external symbols and strings read from a COFF file. */
1549 _bfd_coff_free_symbols (abfd
)
1552 if (obj_coff_external_syms (abfd
) != NULL
1553 && ! obj_coff_keep_syms (abfd
))
1555 free (obj_coff_external_syms (abfd
));
1556 obj_coff_external_syms (abfd
) = NULL
;
1558 if (obj_coff_strings (abfd
) != NULL
1559 && ! obj_coff_keep_strings (abfd
))
1561 free (obj_coff_strings (abfd
));
1562 obj_coff_strings (abfd
) = NULL
;
1567 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1568 knit the symbol names into a normalized form. By normalized here I
1569 mean that all symbols have an n_offset pointer that points to a null-
1570 terminated string. */
1572 combined_entry_type
*
1573 coff_get_normalized_symtab (abfd
)
1576 combined_entry_type
*internal
;
1577 combined_entry_type
*internal_ptr
;
1578 combined_entry_type
*symbol_ptr
;
1579 combined_entry_type
*internal_end
;
1580 bfd_size_type symesz
;
1583 const char *string_table
= NULL
;
1584 char *debug_section
= NULL
;
1587 if (obj_raw_syments (abfd
) != NULL
)
1588 return obj_raw_syments (abfd
);
1590 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1591 internal
= (combined_entry_type
*) bfd_alloc (abfd
, size
);
1592 if (internal
== NULL
&& size
!= 0)
1594 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1596 if (! _bfd_coff_get_external_symbols (abfd
))
1599 raw_src
= (char *) obj_coff_external_syms (abfd
);
1601 /* mark the end of the symbols */
1602 symesz
= bfd_coff_symesz (abfd
);
1603 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1605 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1606 probably possible. If one shows up, it will probably kill us. */
1608 /* Swap all the raw entries */
1609 for (internal_ptr
= internal
;
1611 raw_src
+= symesz
, internal_ptr
++)
1615 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1616 (PTR
) & internal_ptr
->u
.syment
);
1617 internal_ptr
->fix_value
= 0;
1618 internal_ptr
->fix_tag
= 0;
1619 internal_ptr
->fix_end
= 0;
1620 internal_ptr
->fix_scnlen
= 0;
1621 symbol_ptr
= internal_ptr
;
1624 i
< symbol_ptr
->u
.syment
.n_numaux
;
1630 internal_ptr
->fix_value
= 0;
1631 internal_ptr
->fix_tag
= 0;
1632 internal_ptr
->fix_end
= 0;
1633 internal_ptr
->fix_scnlen
= 0;
1634 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1635 symbol_ptr
->u
.syment
.n_type
,
1636 symbol_ptr
->u
.syment
.n_sclass
,
1637 i
, symbol_ptr
->u
.syment
.n_numaux
,
1638 &(internal_ptr
->u
.auxent
));
1639 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1644 /* Free the raw symbols, but not the strings (if we have them). */
1645 obj_coff_keep_strings (abfd
) = true;
1646 if (! _bfd_coff_free_symbols (abfd
))
1649 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1652 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1653 && internal_ptr
->u
.syment
.n_numaux
> 0)
1655 /* make a file symbol point to the name in the auxent, since
1656 the text ".file" is redundant */
1657 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1659 /* the filename is a long one, point into the string table */
1660 if (string_table
== NULL
)
1662 string_table
= _bfd_coff_read_string_table (abfd
);
1663 if (string_table
== NULL
)
1667 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1670 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1674 /* ordinary short filename, put into memory anyway */
1675 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1676 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1682 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1684 /* This is a "short" name. Make it long. */
1685 unsigned long i
= 0;
1686 char *newstring
= NULL
;
1688 /* find the length of this string without walking into memory
1690 for (i
= 0; i
< 8; ++i
)
1692 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1695 } /* if end of string */
1696 } /* possible lengths of this string. */
1698 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1700 memset (newstring
, 0, i
);
1701 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1702 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1703 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1705 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1706 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1707 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1709 /* Long name already. Point symbol at the string in the
1711 if (string_table
== NULL
)
1713 string_table
= _bfd_coff_read_string_table (abfd
);
1714 if (string_table
== NULL
)
1717 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1720 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1724 /* Long name in debug section. Very similar. */
1725 if (debug_section
== NULL
)
1726 debug_section
= build_debug_section (abfd
);
1727 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1728 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1731 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1734 obj_raw_syments (abfd
) = internal
;
1735 BFD_ASSERT (obj_raw_syment_count (abfd
)
1736 == (unsigned int) (internal_ptr
- internal
));
1739 } /* coff_get_normalized_symtab() */
1742 coff_get_reloc_upper_bound (abfd
, asect
)
1746 if (bfd_get_format (abfd
) != bfd_object
)
1748 bfd_set_error (bfd_error_invalid_operation
);
1751 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1755 coff_make_empty_symbol (abfd
)
1758 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1761 memset (new, 0, sizeof *new);
1762 new->symbol
.section
= 0;
1764 new->lineno
= (alent
*) NULL
;
1765 new->done_lineno
= false;
1766 new->symbol
.the_bfd
= abfd
;
1767 return &new->symbol
;
1770 /* Make a debugging symbol. */
1773 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1778 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1781 /* @@ This shouldn't be using a constant multiplier. */
1782 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1785 new->symbol
.section
= bfd_abs_section_ptr
;
1786 new->symbol
.flags
= BSF_DEBUGGING
;
1787 new->lineno
= (alent
*) NULL
;
1788 new->done_lineno
= false;
1789 new->symbol
.the_bfd
= abfd
;
1790 return &new->symbol
;
1795 coff_get_symbol_info (abfd
, symbol
, ret
)
1800 bfd_symbol_info (symbol
, ret
);
1801 if (coffsymbol (symbol
)->native
!= NULL
1802 && coffsymbol (symbol
)->native
->fix_value
)
1804 combined_entry_type
*psym
;
1806 psym
= ((combined_entry_type
*)
1807 coffsymbol (symbol
)->native
->u
.syment
.n_value
);
1808 ret
->value
= (bfd_vma
) (psym
- obj_raw_syments (abfd
));
1812 /* Print out information about COFF symbol. */
1815 coff_print_symbol (abfd
, filep
, symbol
, how
)
1819 bfd_print_symbol_type how
;
1821 FILE *file
= (FILE *) filep
;
1825 case bfd_print_symbol_name
:
1826 fprintf (file
, "%s", symbol
->name
);
1829 case bfd_print_symbol_more
:
1830 fprintf (file
, "coff %s %s",
1831 coffsymbol (symbol
)->native
? "n" : "g",
1832 coffsymbol (symbol
)->lineno
? "l" : " ");
1835 case bfd_print_symbol_all
:
1836 if (coffsymbol (symbol
)->native
)
1840 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1841 combined_entry_type
*root
= obj_raw_syments (abfd
);
1842 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1844 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1846 if (! combined
->fix_value
)
1847 val
= (unsigned long) combined
->u
.syment
.n_value
;
1849 val
= ((unsigned long)
1850 ((combined_entry_type
*) combined
->u
.syment
.n_value
1854 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1855 combined
->u
.syment
.n_scnum
,
1856 combined
->u
.syment
.n_flags
,
1857 combined
->u
.syment
.n_type
,
1858 combined
->u
.syment
.n_sclass
,
1859 combined
->u
.syment
.n_numaux
,
1863 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1865 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1869 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1871 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1873 fprintf (file
, "\n");
1875 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
1878 switch (combined
->u
.syment
.n_sclass
)
1881 fprintf (file
, "File ");
1885 if (combined
->u
.syment
.n_type
== T_NULL
)
1886 /* probably a section symbol? */
1888 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1889 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
1890 auxp
->u
.auxent
.x_scn
.x_nreloc
,
1891 auxp
->u
.auxent
.x_scn
.x_nlinno
);
1894 /* else fall through */
1897 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1898 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1899 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1902 fprintf (file
, " endndx %ld",
1904 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1912 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1914 while (l
->line_number
)
1916 fprintf (file
, "\n%4d : 0x%lx",
1919 (l
->u
.offset
+ symbol
->section
->vma
)));
1926 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1927 fprintf (file
, " %-5s %s %s %s",
1928 symbol
->section
->name
,
1929 coffsymbol (symbol
)->native
? "n" : "g",
1930 coffsymbol (symbol
)->lineno
? "l" : " ",
1936 /* Provided a BFD, a section and an offset into the section, calculate
1937 and return the name of the source file and the line nearest to the
1942 coff_find_nearest_line (abfd
, section
, ignore_symbols
, offset
, filename_ptr
,
1943 functionname_ptr
, line_ptr
)
1946 asymbol
**ignore_symbols
;
1948 CONST
char **filename_ptr
;
1949 CONST
char **functionname_ptr
;
1950 unsigned int *line_ptr
;
1953 unsigned int line_base
;
1954 coff_data_type
*cof
= coff_data (abfd
);
1955 /* Run through the raw syments if available */
1956 combined_entry_type
*p
;
1957 combined_entry_type
*pend
;
1959 struct coff_section_tdata
*sec_data
;
1962 *functionname_ptr
= 0;
1965 /* Don't try and find line numbers in a non coff file */
1966 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1972 /* Find the first C_FILE symbol. */
1973 p
= cof
->raw_syments
;
1974 pend
= p
+ cof
->raw_syment_count
;
1977 if (p
->u
.syment
.n_sclass
== C_FILE
)
1979 p
+= 1 + p
->u
.syment
.n_numaux
;
1986 /* Look through the C_FILE symbols to find the best one. */
1987 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1988 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
1991 combined_entry_type
*p2
;
1993 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
1995 p2
+= 1 + p2
->u
.syment
.n_numaux
)
1997 if (p2
->u
.syment
.n_scnum
> 0
1999 == coff_section_from_bfd_index (abfd
,
2000 p2
->u
.syment
.n_scnum
)))
2002 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2010 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
2011 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
2013 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2014 maxdiff
= offset
- p2
->u
.syment
.n_value
;
2017 /* Avoid endless loops on erroneous files by ensuring that
2018 we always move forward in the file. */
2019 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
2022 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2023 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2028 /* Now wander though the raw linenumbers of the section */
2029 /* If we have been called on this section before, and the offset we
2030 want is further down then we can prime the lookup loop. */
2031 sec_data
= coff_section_data (abfd
, section
);
2032 if (sec_data
!= NULL
2034 && offset
>= sec_data
->offset
)
2037 *functionname_ptr
= sec_data
->function
;
2038 line_base
= sec_data
->line_base
;
2046 l
= §ion
->lineno
[i
];
2048 for (; i
< section
->lineno_count
; i
++)
2050 if (l
->line_number
== 0)
2052 /* Get the symbol this line number points at */
2053 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2054 if (coff
->symbol
.value
> offset
)
2056 *functionname_ptr
= coff
->symbol
.name
;
2059 combined_entry_type
*s
= coff
->native
;
2060 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2062 /* In XCOFF a debugging symbol can follow the function
2064 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2065 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2068 S should now point to the .bf of the function
2070 if (s
->u
.syment
.n_numaux
)
2073 The linenumber is stored in the auxent
2075 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2076 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2077 *line_ptr
= line_base
;
2083 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
2085 *line_ptr
= l
->line_number
+ line_base
- 1;
2090 /* Cache the results for the next call. */
2091 if (sec_data
== NULL
)
2093 section
->used_by_bfd
=
2094 ((PTR
) bfd_zalloc (abfd
,
2095 sizeof (struct coff_section_tdata
)));
2096 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2098 if (sec_data
!= NULL
)
2100 sec_data
->offset
= offset
;
2102 sec_data
->function
= *functionname_ptr
;
2103 sec_data
->line_base
= line_base
;
2110 coff_sizeof_headers (abfd
, reloc
)
2118 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2122 size
= bfd_coff_filhsz (abfd
);
2125 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);