1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 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
*));
59 static boolean make_a_section_from_file
60 PARAMS ((bfd
*, struct internal_scnhdr
*, unsigned int));
61 static const bfd_target
*coff_real_object_p
62 PARAMS ((bfd
*, unsigned, struct internal_filehdr
*,
63 struct internal_aouthdr
*));
64 static void fixup_symbol_value
65 PARAMS ((bfd
*, coff_symbol_type
*, struct internal_syment
*));
66 static char *build_debug_section
68 static char *copy_name
69 PARAMS ((bfd
*, char *, int));
71 #define STRING_SIZE_SIZE (4)
73 /* Take a section header read from a coff file (in HOST byte order),
74 and make a BFD "section" out of it. This is used by ECOFF. */
76 make_a_section_from_file (abfd
, hdr
, target_index
)
78 struct internal_scnhdr
*hdr
;
79 unsigned int target_index
;
81 asection
*return_section
;
86 /* Handle long section names as in PE. */
87 if (bfd_coff_long_section_names (abfd
)
88 && hdr
->s_name
[0] == '/')
95 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
96 buf
[SCNNMLEN
- 1] = '\0';
97 strindex
= strtol (buf
, &p
, 10);
98 if (*p
== '\0' && strindex
>= 0)
100 strings
= _bfd_coff_read_string_table (abfd
);
103 /* FIXME: For extra safety, we should make sure that
104 strindex does not run us past the end, but right now we
105 don't know the length of the string table. */
107 name
= bfd_alloc (abfd
, strlen (strings
) + 1);
110 strcpy (name
, strings
);
116 /* Assorted wastage to null-terminate the name, thanks AT&T! */
117 name
= bfd_alloc (abfd
, sizeof (hdr
->s_name
) + 1);
120 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
121 name
[sizeof (hdr
->s_name
)] = 0;
124 return_section
= bfd_make_section_anyway (abfd
, name
);
125 if (return_section
== NULL
)
128 return_section
->vma
= hdr
->s_vaddr
;
129 return_section
->lma
= hdr
->s_paddr
;
130 return_section
->_raw_size
= hdr
->s_size
;
131 return_section
->filepos
= hdr
->s_scnptr
;
132 return_section
->rel_filepos
= hdr
->s_relptr
;
133 return_section
->reloc_count
= hdr
->s_nreloc
;
135 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
137 return_section
->line_filepos
= hdr
->s_lnnoptr
;
139 return_section
->lineno_count
= hdr
->s_nlnno
;
140 return_section
->userdata
= NULL
;
141 return_section
->next
= (asection
*) NULL
;
142 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
144 return_section
->target_index
= target_index
;
146 /* At least on i386-coff, the line number count for a shared library
147 section must be ignored. */
148 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
149 return_section
->lineno_count
= 0;
151 if (hdr
->s_nreloc
!= 0)
152 return_section
->flags
|= SEC_RELOC
;
153 /* FIXME: should this check 'hdr->s_size > 0' */
154 if (hdr
->s_scnptr
!= 0)
155 return_section
->flags
|= SEC_HAS_CONTENTS
;
159 /* Read in a COFF object and make it into a BFD. This is used by
162 static const bfd_target
*
163 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
166 struct internal_filehdr
*internal_f
;
167 struct internal_aouthdr
*internal_a
;
169 flagword oflags
= abfd
->flags
;
170 bfd_vma ostart
= bfd_get_start_address (abfd
);
172 size_t readsize
; /* length of file_info */
174 char *external_sections
;
176 if (!(internal_f
->f_flags
& F_RELFLG
))
177 abfd
->flags
|= HAS_RELOC
;
178 if ((internal_f
->f_flags
& F_EXEC
))
179 abfd
->flags
|= EXEC_P
;
180 if (!(internal_f
->f_flags
& F_LNNO
))
181 abfd
->flags
|= HAS_LINENO
;
182 if (!(internal_f
->f_flags
& F_LSYMS
))
183 abfd
->flags
|= HAS_LOCALS
;
185 /* FIXME: How can we set D_PAGED correctly? */
186 if ((internal_f
->f_flags
& F_EXEC
) != 0)
187 abfd
->flags
|= D_PAGED
;
189 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
190 if (internal_f
->f_nsyms
)
191 abfd
->flags
|= HAS_SYMS
;
193 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
194 bfd_get_start_address (abfd
) = internal_a
->entry
;
196 bfd_get_start_address (abfd
) = 0;
198 /* Set up the tdata area. ECOFF uses its own routine, and overrides
200 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
204 scnhsz
= bfd_coff_scnhsz (abfd
);
205 readsize
= nscns
* scnhsz
;
206 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
207 if (!external_sections
)
210 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
213 /* Now copy data as required; construct all asections etc */
217 for (i
= 0; i
< nscns
; i
++)
219 struct internal_scnhdr tmp
;
220 bfd_coff_swap_scnhdr_in (abfd
,
221 (PTR
) (external_sections
+ i
* scnhsz
),
223 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
228 /* make_abs_section (abfd); */
230 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
236 bfd_release (abfd
, tdata
);
237 abfd
->flags
= oflags
;
238 bfd_get_start_address (abfd
) = ostart
;
239 return (const bfd_target
*) NULL
;
242 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
243 not a COFF file. This is also used by ECOFF. */
253 struct internal_filehdr internal_f
;
254 struct internal_aouthdr internal_a
;
256 /* figure out how much to read */
257 filhsz
= bfd_coff_filhsz (abfd
);
258 aoutsz
= bfd_coff_aoutsz (abfd
);
260 filehdr
= bfd_alloc (abfd
, filhsz
);
263 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
265 if (bfd_get_error () != bfd_error_system_call
)
266 bfd_set_error (bfd_error_wrong_format
);
269 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
270 bfd_release (abfd
, filehdr
);
272 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
274 bfd_set_error (bfd_error_wrong_format
);
277 nscns
= internal_f
.f_nscns
;
279 if (internal_f
.f_opthdr
)
283 opthdr
= bfd_alloc (abfd
, aoutsz
);
286 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
290 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
293 return coff_real_object_p (abfd
, nscns
, &internal_f
,
294 (internal_f
.f_opthdr
!= 0
296 : (struct internal_aouthdr
*) NULL
));
299 /* Get the BFD section from a COFF symbol section number. */
302 coff_section_from_bfd_index (abfd
, index
)
306 struct sec
*answer
= abfd
->sections
;
309 return bfd_abs_section_ptr
;
310 if (index
== N_UNDEF
)
311 return bfd_und_section_ptr
;
312 if (index
== N_DEBUG
)
313 return bfd_abs_section_ptr
;
317 if (answer
->target_index
== index
)
319 answer
= answer
->next
;
322 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
323 has a bad symbol table in biglitpow.o. */
324 return bfd_und_section_ptr
;
327 /* Get the upper bound of a COFF symbol table. */
330 coff_get_symtab_upper_bound (abfd
)
333 if (!bfd_coff_slurp_symbol_table (abfd
))
336 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
340 /* Canonicalize a COFF symbol table. */
343 coff_get_symtab (abfd
, alocation
)
347 unsigned int counter
;
348 coff_symbol_type
*symbase
;
349 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
351 if (!bfd_coff_slurp_symbol_table (abfd
))
354 symbase
= obj_symbols (abfd
);
355 counter
= bfd_get_symcount (abfd
);
356 while (counter
-- > 0)
357 *location
++ = symbase
++;
361 return bfd_get_symcount (abfd
);
364 /* Get the name of a symbol. The caller must pass in a buffer of size
368 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
370 const struct internal_syment
*sym
;
373 /* FIXME: It's not clear this will work correctly if sizeof
375 if (sym
->_n
._n_n
._n_zeroes
!= 0
376 || sym
->_n
._n_n
._n_offset
== 0)
378 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
379 buf
[SYMNMLEN
] = '\0';
386 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
387 strings
= obj_coff_strings (abfd
);
390 strings
= _bfd_coff_read_string_table (abfd
);
394 return strings
+ sym
->_n
._n_n
._n_offset
;
398 /* Read in and swap the relocs. This returns a buffer holding the
399 relocs for section SEC in file ABFD. If CACHE is true and
400 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
401 the function is called again. If EXTERNAL_RELOCS is not NULL, it
402 is a buffer large enough to hold the unswapped relocs. If
403 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
404 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
405 value must be INTERNAL_RELOCS. The function returns NULL on error. */
407 struct internal_reloc
*
408 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
409 require_internal
, internal_relocs
)
413 bfd_byte
*external_relocs
;
414 boolean require_internal
;
415 struct internal_reloc
*internal_relocs
;
418 bfd_byte
*free_external
= NULL
;
419 struct internal_reloc
*free_internal
= NULL
;
422 struct internal_reloc
*irel
;
424 if (coff_section_data (abfd
, sec
) != NULL
425 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
427 if (! require_internal
)
428 return coff_section_data (abfd
, sec
)->relocs
;
429 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
430 sec
->reloc_count
* sizeof (struct internal_reloc
));
431 return internal_relocs
;
434 relsz
= bfd_coff_relsz (abfd
);
436 if (external_relocs
== NULL
)
438 free_external
= (bfd_byte
*) bfd_malloc (sec
->reloc_count
* relsz
);
439 if (free_external
== NULL
&& sec
->reloc_count
> 0)
441 external_relocs
= free_external
;
444 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
445 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
446 != relsz
* sec
->reloc_count
))
449 if (internal_relocs
== NULL
)
451 free_internal
= ((struct internal_reloc
*)
452 bfd_malloc (sec
->reloc_count
453 * sizeof (struct internal_reloc
)));
454 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
456 internal_relocs
= free_internal
;
459 /* Swap in the relocs. */
460 erel
= external_relocs
;
461 erel_end
= erel
+ relsz
* sec
->reloc_count
;
462 irel
= internal_relocs
;
463 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
464 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
466 if (free_external
!= NULL
)
468 free (free_external
);
469 free_external
= NULL
;
472 if (cache
&& free_internal
!= NULL
)
474 if (coff_section_data (abfd
, sec
) == NULL
)
477 (PTR
) bfd_zalloc (abfd
,
478 sizeof (struct coff_section_tdata
));
479 if (sec
->used_by_bfd
== NULL
)
481 coff_section_data (abfd
, sec
)->contents
= NULL
;
483 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
486 return internal_relocs
;
489 if (free_external
!= NULL
)
490 free (free_external
);
491 if (free_internal
!= NULL
)
492 free (free_internal
);
496 /* Set lineno_count for the output sections of a COFF file. */
499 coff_count_linenumbers (abfd
)
502 unsigned int limit
= bfd_get_symcount (abfd
);
510 /* This may be from the backend linker, in which case the
511 lineno_count in the sections is correct. */
512 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
513 total
+= s
->lineno_count
;
517 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
518 BFD_ASSERT (s
->lineno_count
== 0);
520 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
522 asymbol
*q_maybe
= *p
;
524 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
526 coff_symbol_type
*q
= coffsymbol (q_maybe
);
528 /* The AIX 4.1 compiler can sometimes generate line numbers
529 attached to debugging symbols. We try to simply ignore
531 if (q
->lineno
!= NULL
532 && q
->symbol
.section
->owner
!= NULL
)
534 /* This symbol has line numbers. Increment the owning
535 section's linenumber count. */
536 alent
*l
= q
->lineno
;
538 ++q
->symbol
.section
->output_section
->lineno_count
;
541 while (l
->line_number
!= 0)
544 ++q
->symbol
.section
->output_section
->lineno_count
;
554 /* Takes a bfd and a symbol, returns a pointer to the coff specific
555 area of the symbol if there is one. */
559 coff_symbol_from (ignore_abfd
, symbol
)
563 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
564 return (coff_symbol_type
*) NULL
;
566 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
567 return (coff_symbol_type
*) NULL
;
569 return (coff_symbol_type
*) symbol
;
573 fixup_symbol_value (abfd
, coff_symbol_ptr
, syment
)
575 coff_symbol_type
*coff_symbol_ptr
;
576 struct internal_syment
*syment
;
579 /* Normalize the symbol flags */
580 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
582 /* a common symbol is undefined with a value */
583 syment
->n_scnum
= N_UNDEF
;
584 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
586 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
588 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
590 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
592 syment
->n_scnum
= N_UNDEF
;
597 if (coff_symbol_ptr
->symbol
.section
)
600 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
602 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
603 + coff_symbol_ptr
->symbol
.section
->output_offset
);
606 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
611 /* This can happen, but I don't know why yet (steve@cygnus.com) */
612 syment
->n_scnum
= N_ABS
;
613 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
618 /* Run through all the symbols in the symbol table and work out what
619 their indexes into the symbol table will be when output.
621 Coff requires that each C_FILE symbol points to the next one in the
622 chain, and that the last one points to the first external symbol. We
626 coff_renumber_symbols (bfd_ptr
, first_undef
)
630 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
631 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
632 unsigned int native_index
= 0;
633 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
634 unsigned int symbol_index
;
636 /* COFF demands that undefined symbols come after all other symbols.
637 Since we don't need to impose this extra knowledge on all our
638 client programs, deal with that here. Sort the symbol table;
639 just move the undefined symbols to the end, leaving the rest
640 alone. The O'Reilly book says that defined global symbols come
641 at the end before the undefined symbols, so we do that here as
643 /* @@ Do we have some condition we could test for, so we don't always
644 have to do this? I don't think relocatability is quite right, but
645 I'm not certain. [raeburn:19920508.1711EST] */
650 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
,
651 sizeof (asymbol
*) * (symbol_count
+ 1));
654 bfd_ptr
->outsymbols
= newsyms
;
655 for (i
= 0; i
< symbol_count
; i
++)
656 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
657 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
658 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
659 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
661 *newsyms
++ = symbol_ptr_ptr
[i
];
663 for (i
= 0; i
< symbol_count
; i
++)
664 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
665 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
666 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
667 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
669 *newsyms
++ = symbol_ptr_ptr
[i
];
671 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
673 for (i
= 0; i
< symbol_count
; i
++)
674 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
675 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
676 *newsyms
++ = symbol_ptr_ptr
[i
];
677 *newsyms
= (asymbol
*) NULL
;
678 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
681 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
683 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
684 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
685 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
687 combined_entry_type
*s
= coff_symbol_ptr
->native
;
690 if (s
->u
.syment
.n_sclass
== C_FILE
)
692 if (last_file
!= (struct internal_syment
*) NULL
)
693 last_file
->n_value
= native_index
;
694 last_file
= &(s
->u
.syment
);
699 /* Modify the symbol values according to their section and
702 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
704 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
705 s
[i
].offset
= native_index
++;
712 obj_conv_table_size (bfd_ptr
) = native_index
;
717 /* Run thorough the symbol table again, and fix it so that all
718 pointers to entries are changed to the entries' index in the output
722 coff_mangle_symbols (bfd_ptr
)
725 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
726 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
727 unsigned int symbol_index
;
729 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
731 coff_symbol_type
*coff_symbol_ptr
=
732 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
734 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
737 combined_entry_type
*s
= coff_symbol_ptr
->native
;
741 /* FIXME: We should use a union here. */
742 s
->u
.syment
.n_value
=
743 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
748 /* The value is the offset into the line number entries
749 for the symbol's section. On output, the symbol's
750 section should be N_DEBUG. */
751 s
->u
.syment
.n_value
=
752 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
753 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
754 coff_symbol_ptr
->symbol
.section
=
755 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
756 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
758 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
760 combined_entry_type
*a
= s
+ i
+ 1;
763 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
764 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
769 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
770 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
775 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
776 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
785 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
786 debug_string_section_p
, debug_string_size_p
)
789 combined_entry_type
*native
;
790 bfd_size_type
*string_size_p
;
791 asection
**debug_string_section_p
;
792 bfd_size_type
*debug_string_size_p
;
794 unsigned int name_length
;
795 union internal_auxent
*auxent
;
796 char *name
= (char *) (symbol
->name
);
798 if (name
== (char *) NULL
)
800 /* coff symbols always have names, so we'll make one up */
801 symbol
->name
= "strange";
802 name
= (char *) symbol
->name
;
804 name_length
= strlen (name
);
806 if (native
->u
.syment
.n_sclass
== C_FILE
807 && native
->u
.syment
.n_numaux
> 0)
809 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
810 auxent
= &(native
+ 1)->u
.auxent
;
812 if (bfd_coff_long_filenames (abfd
))
814 if (name_length
<= FILNMLEN
)
816 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
820 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
821 auxent
->x_file
.x_n
.x_zeroes
= 0;
822 *string_size_p
+= name_length
+ 1;
827 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
828 if (name_length
> FILNMLEN
)
830 name
[FILNMLEN
] = '\0';
836 if (name_length
<= SYMNMLEN
)
838 /* This name will fit into the symbol neatly */
839 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
841 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
843 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
845 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
846 *string_size_p
+= name_length
+ 1;
853 /* This name should be written into the .debug section. For
854 some reason each name is preceded by a two byte length
855 and also followed by a null byte. FIXME: We assume that
856 the .debug section has already been created, and that it
858 if (*debug_string_section_p
== (asection
*) NULL
)
859 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
860 filepos
= bfd_tell (abfd
);
861 bfd_put_16 (abfd
, name_length
+ 1, buf
);
862 if (!bfd_set_section_contents (abfd
,
863 *debug_string_section_p
,
865 (file_ptr
) *debug_string_size_p
,
867 || !bfd_set_section_contents (abfd
,
868 *debug_string_section_p
,
870 ((file_ptr
) *debug_string_size_p
872 (bfd_size_type
) name_length
+ 1))
874 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
876 native
->u
.syment
._n
._n_n
._n_offset
= *debug_string_size_p
+ 2;
877 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
878 *debug_string_size_p
+= name_length
+ 3;
883 /* We need to keep track of the symbol index so that when we write out
884 the relocs we can get the index for a symbol. This method is a
887 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
889 /* Write a symbol out to a COFF file. */
892 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
893 debug_string_section_p
, debug_string_size_p
)
896 combined_entry_type
*native
;
897 unsigned int *written
;
898 bfd_size_type
*string_size_p
;
899 asection
**debug_string_section_p
;
900 bfd_size_type
*debug_string_size_p
;
902 unsigned int numaux
= native
->u
.syment
.n_numaux
;
903 int type
= native
->u
.syment
.n_type
;
904 int class = native
->u
.syment
.n_sclass
;
906 bfd_size_type symesz
;
908 if (native
->u
.syment
.n_sclass
== C_FILE
)
909 symbol
->flags
|= BSF_DEBUGGING
;
911 if (symbol
->flags
& BSF_DEBUGGING
912 && bfd_is_abs_section (symbol
->section
))
914 native
->u
.syment
.n_scnum
= N_DEBUG
;
916 else if (bfd_is_abs_section (symbol
->section
))
918 native
->u
.syment
.n_scnum
= N_ABS
;
920 else if (bfd_is_und_section (symbol
->section
))
922 native
->u
.syment
.n_scnum
= N_UNDEF
;
926 native
->u
.syment
.n_scnum
=
927 symbol
->section
->output_section
->target_index
;
930 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
931 debug_string_section_p
, debug_string_size_p
);
933 symesz
= bfd_coff_symesz (abfd
);
934 buf
= bfd_alloc (abfd
, symesz
);
937 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
938 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
940 bfd_release (abfd
, buf
);
942 if (native
->u
.syment
.n_numaux
> 0)
944 bfd_size_type auxesz
;
947 auxesz
= bfd_coff_auxesz (abfd
);
948 buf
= bfd_alloc (abfd
, auxesz
);
951 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
953 bfd_coff_swap_aux_out (abfd
,
954 &((native
+ j
+ 1)->u
.auxent
),
958 native
->u
.syment
.n_numaux
,
960 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
963 bfd_release (abfd
, buf
);
966 /* Store the index for use when we write out the relocs. */
967 set_index (symbol
, *written
);
969 *written
+= numaux
+ 1;
973 /* Write out a symbol to a COFF file that does not come from a COFF
974 file originally. This symbol may have been created by the linker,
975 or we may be linking a non COFF file to a COFF file. */
978 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
979 debug_string_section_p
, debug_string_size_p
)
982 unsigned int *written
;
983 bfd_size_type
*string_size_p
;
984 asection
**debug_string_section_p
;
985 bfd_size_type
*debug_string_size_p
;
987 combined_entry_type
*native
;
988 combined_entry_type dummy
;
991 native
->u
.syment
.n_type
= T_NULL
;
992 native
->u
.syment
.n_flags
= 0;
993 if (bfd_is_und_section (symbol
->section
))
995 native
->u
.syment
.n_scnum
= N_UNDEF
;
996 native
->u
.syment
.n_value
= symbol
->value
;
998 else if (bfd_is_com_section (symbol
->section
))
1000 native
->u
.syment
.n_scnum
= N_UNDEF
;
1001 native
->u
.syment
.n_value
= symbol
->value
;
1003 else if (symbol
->flags
& BSF_DEBUGGING
)
1005 /* There isn't much point to writing out a debugging symbol
1006 unless we are prepared to convert it into COFF debugging
1007 format. So, we just ignore them. We must clobber the symbol
1008 name to keep it from being put in the string table. */
1014 native
->u
.syment
.n_scnum
=
1015 symbol
->section
->output_section
->target_index
;
1016 native
->u
.syment
.n_value
= (symbol
->value
1017 + symbol
->section
->output_offset
);
1018 if (! obj_pe (abfd
))
1019 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
1021 /* Copy the any flags from the the file header into the symbol.
1024 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1025 if (c
!= (coff_symbol_type
*) NULL
)
1026 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1030 native
->u
.syment
.n_type
= 0;
1031 if (symbol
->flags
& BSF_LOCAL
)
1032 native
->u
.syment
.n_sclass
= C_STAT
;
1034 native
->u
.syment
.n_sclass
= C_EXT
;
1035 native
->u
.syment
.n_numaux
= 0;
1037 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1038 debug_string_section_p
, debug_string_size_p
);
1041 /* Write a native symbol to a COFF file. */
1044 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1045 debug_string_section_p
, debug_string_size_p
)
1047 coff_symbol_type
*symbol
;
1048 unsigned int *written
;
1049 bfd_size_type
*string_size_p
;
1050 asection
**debug_string_section_p
;
1051 bfd_size_type
*debug_string_size_p
;
1053 combined_entry_type
*native
= symbol
->native
;
1054 alent
*lineno
= symbol
->lineno
;
1056 /* If this symbol has an associated line number, we must store the
1057 symbol index in the line number field. We also tag the auxent to
1058 point to the right place in the lineno table. */
1059 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1061 unsigned int count
= 0;
1062 lineno
[count
].u
.offset
= *written
;
1063 if (native
->u
.syment
.n_numaux
)
1065 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1067 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1068 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1071 /* Count and relocate all other linenumbers. */
1073 while (lineno
[count
].line_number
!= 0)
1077 I've been told this, but still need proof:
1078 > The second bug is also in `bfd/coffcode.h'. This bug
1079 > causes the linker to screw up the pc-relocations for
1080 > all the line numbers in COFF code. This bug isn't only
1081 > specific to A29K implementations, but affects all
1082 > systems using COFF format binaries. Note that in COFF
1083 > object files, the line number core offsets output by
1084 > the assembler are relative to the start of each
1085 > procedure, not to the start of the .text section. This
1086 > patch relocates the line numbers relative to the
1087 > `native->u.syment.n_value' instead of the section
1089 > modular!olson@cs.arizona.edu (Jon Olson)
1091 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1093 lineno
[count
].u
.offset
+=
1094 (symbol
->symbol
.section
->output_section
->vma
1095 + symbol
->symbol
.section
->output_offset
);
1099 symbol
->done_lineno
= true;
1101 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1102 count
* bfd_coff_linesz (abfd
);
1105 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1106 string_size_p
, debug_string_section_p
,
1107 debug_string_size_p
);
1110 /* Write out the COFF symbols. */
1113 coff_write_symbols (abfd
)
1116 bfd_size_type string_size
;
1117 asection
*debug_string_section
;
1118 bfd_size_type debug_string_size
;
1120 unsigned int limit
= bfd_get_symcount (abfd
);
1121 unsigned int written
= 0;
1125 debug_string_section
= NULL
;
1126 debug_string_size
= 0;
1128 /* If this target supports long section names, they must be put into
1129 the string table. This is supported by PE. This code must
1130 handle section names just as they are handled in
1131 coff_write_object_contents. */
1132 if (bfd_coff_long_section_names (abfd
))
1136 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1140 len
= strlen (o
->name
);
1142 string_size
+= len
+ 1;
1146 /* Seek to the right place */
1147 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1150 /* Output all the symbols we have */
1153 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1155 asymbol
*symbol
= *p
;
1156 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1158 if (c_symbol
== (coff_symbol_type
*) NULL
1159 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1161 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1162 &debug_string_section
,
1163 &debug_string_size
))
1168 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1169 &string_size
, &debug_string_section
,
1170 &debug_string_size
))
1175 obj_raw_syment_count (abfd
) = written
;
1177 /* Now write out strings */
1179 if (string_size
!= 0)
1181 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1182 bfd_byte buffer
[STRING_SIZE_SIZE
];
1184 #if STRING_SIZE_SIZE == 4
1185 bfd_h_put_32 (abfd
, size
, buffer
);
1187 #error Change bfd_h_put_32
1189 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
1192 /* Handle long section names. This code must handle section
1193 names just as they are handled in coff_write_object_contents. */
1194 if (bfd_coff_long_section_names (abfd
))
1198 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1202 len
= strlen (o
->name
);
1205 if (bfd_write (o
->name
, 1, len
+ 1, abfd
) != len
+ 1)
1211 for (p
= abfd
->outsymbols
, i
= 0;
1216 size_t name_length
= strlen (q
->name
);
1217 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1220 /* Figure out whether the symbol name should go in the string
1221 table. Symbol names that are short enough are stored
1222 directly in the syment structure. File names permit a
1223 different, longer, length in the syment structure. On
1224 XCOFF, some symbol names are stored in the .debug section
1225 rather than in the string table. */
1227 if (c_symbol
== NULL
1228 || c_symbol
->native
== NULL
)
1230 /* This is not a COFF symbol, so it certainly is not a
1231 file name, nor does it go in the .debug section. */
1234 else if (bfd_coff_symname_in_debug (abfd
,
1235 &c_symbol
->native
->u
.syment
))
1237 /* This symbol name is in the XCOFF .debug section.
1238 Don't write it into the string table. */
1239 maxlen
= name_length
;
1241 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1242 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1247 if (name_length
> maxlen
)
1249 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1257 /* We would normally not write anything here, but we'll write
1258 out 4 so that any stupid coff reader which tries to read the
1259 string table even when there isn't one won't croak. */
1260 unsigned int size
= STRING_SIZE_SIZE
;
1261 bfd_byte buffer
[STRING_SIZE_SIZE
];
1263 #if STRING_SIZE_SIZE == 4
1264 bfd_h_put_32 (abfd
, size
, buffer
);
1266 #error Change bfd_h_put_32
1268 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1269 != STRING_SIZE_SIZE
)
1273 /* Make sure the .debug section was created to be the correct size.
1274 We should create it ourselves on the fly, but we don't because
1275 BFD won't let us write to any section until we know how large all
1276 the sections are. We could still do it by making another pass
1277 over the symbols. FIXME. */
1278 BFD_ASSERT (debug_string_size
== 0
1279 || (debug_string_section
!= (asection
*) NULL
1280 && (BFD_ALIGN (debug_string_size
,
1281 1 << debug_string_section
->alignment_power
)
1282 == bfd_section_size (abfd
, debug_string_section
))));
1288 coff_write_linenumbers (abfd
)
1292 bfd_size_type linesz
;
1295 linesz
= bfd_coff_linesz (abfd
);
1296 buff
= bfd_alloc (abfd
, linesz
);
1299 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1301 if (s
->lineno_count
)
1303 asymbol
**q
= abfd
->outsymbols
;
1304 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1306 /* Find all the linenumbers in this section */
1310 if (p
->section
->output_section
== s
)
1313 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1314 (bfd_asymbol_bfd (p
), p
));
1317 /* Found a linenumber entry, output */
1318 struct internal_lineno out
;
1319 memset ((PTR
) & out
, 0, sizeof (out
));
1321 out
.l_addr
.l_symndx
= l
->u
.offset
;
1322 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1323 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1326 while (l
->line_number
)
1328 out
.l_lnno
= l
->line_number
;
1329 out
.l_addr
.l_symndx
= l
->u
.offset
;
1330 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1331 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1341 bfd_release (abfd
, buff
);
1347 coff_get_lineno (ignore_abfd
, symbol
)
1351 return coffsymbol (symbol
)->lineno
;
1356 /* This is only called from coff_add_missing_symbols, which has been
1360 coff_section_symbol (abfd
, name
)
1364 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1366 combined_entry_type
*csym
;
1369 csym
= coff_symbol_from (abfd
, sym
)->native
;
1370 /* Make sure back-end COFF stuff is there. */
1375 coff_symbol_type sym
;
1376 /* @@FIXME This shouldn't use a fixed size!! */
1377 combined_entry_type e
[10];
1380 f
= (struct foo
*) bfd_alloc (abfd
, sizeof (*f
));
1383 bfd_set_error (bfd_error_no_error
);
1386 memset ((char *) f
, 0, sizeof (*f
));
1387 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1389 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1390 csym
[0].u
.syment
.n_numaux
= 1;
1391 /* SF_SET_STATICS (sym); @@ ??? */
1392 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1393 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1394 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1396 if (sec
->output_section
== NULL
)
1398 sec
->output_section
= sec
;
1399 sec
->output_offset
= 0;
1407 /* This function transforms the offsets into the symbol table into
1408 pointers to syments. */
1411 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1413 combined_entry_type
*table_base
;
1414 combined_entry_type
*symbol
;
1415 unsigned int indaux
;
1416 combined_entry_type
*auxent
;
1418 int type
= symbol
->u
.syment
.n_type
;
1419 int class = symbol
->u
.syment
.n_sclass
;
1421 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1423 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1424 (abfd
, table_base
, symbol
, indaux
, auxent
))
1428 /* Don't bother if this is a file or a section */
1429 if (class == C_STAT
&& type
== T_NULL
)
1431 if (class == C_FILE
)
1434 /* Otherwise patch up */
1435 #define N_TMASK coff_data (abfd)->local_n_tmask
1436 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1437 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
|| class == C_FCN
)
1438 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1440 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1441 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1442 auxent
->fix_end
= 1;
1444 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1445 generate one, so we must be careful to ignore it. */
1446 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1448 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1449 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1450 auxent
->fix_tag
= 1;
1454 /* Allocate space for the ".debug" section, and read it.
1455 We did not read the debug section until now, because
1456 we didn't want to go to the trouble until someone needed it. */
1459 build_debug_section (abfd
)
1462 char *debug_section
;
1465 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1469 bfd_set_error (bfd_error_no_debug_section
);
1473 debug_section
= (PTR
) bfd_alloc (abfd
,
1474 bfd_get_section_size_before_reloc (sect
));
1475 if (debug_section
== NULL
)
1478 /* Seek to the beginning of the `.debug' section and read it.
1479 Save the current position first; it is needed by our caller.
1480 Then read debug section and reset the file pointer. */
1482 position
= bfd_tell (abfd
);
1483 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1484 || (bfd_read (debug_section
,
1485 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1486 != bfd_get_section_size_before_reloc (sect
))
1487 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1489 return debug_section
;
1493 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1494 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1495 be \0-terminated. */
1497 copy_name (abfd
, name
, maxlen
)
1505 for (len
= 0; len
< maxlen
; ++len
)
1507 if (name
[len
] == '\0')
1513 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1515 strncpy (newname
, name
, len
);
1516 newname
[len
] = '\0';
1520 /* Read in the external symbols. */
1523 _bfd_coff_get_external_symbols (abfd
)
1526 bfd_size_type symesz
;
1530 if (obj_coff_external_syms (abfd
) != NULL
)
1533 symesz
= bfd_coff_symesz (abfd
);
1535 size
= obj_raw_syment_count (abfd
) * symesz
;
1537 syms
= (PTR
) bfd_malloc (size
);
1538 if (syms
== NULL
&& size
!= 0)
1541 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1542 || bfd_read (syms
, size
, 1, abfd
) != size
)
1549 obj_coff_external_syms (abfd
) = syms
;
1554 /* Read in the external strings. The strings are not loaded until
1555 they are needed. This is because we have no simple way of
1556 detecting a missing string table in an archive. */
1559 _bfd_coff_read_string_table (abfd
)
1562 char extstrsize
[STRING_SIZE_SIZE
];
1566 if (obj_coff_strings (abfd
) != NULL
)
1567 return obj_coff_strings (abfd
);
1569 if (obj_sym_filepos (abfd
) == 0)
1571 bfd_set_error (bfd_error_no_symbols
);
1576 (obj_sym_filepos (abfd
)
1577 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1581 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1583 if (bfd_get_error () != bfd_error_file_truncated
)
1586 /* There is no string table. */
1587 strsize
= STRING_SIZE_SIZE
;
1591 #if STRING_SIZE_SIZE == 4
1592 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1594 #error Change bfd_h_get_32
1598 if (strsize
< STRING_SIZE_SIZE
)
1600 (*_bfd_error_handler
)
1601 ("%s: bad string table size %lu", bfd_get_filename (abfd
),
1602 (unsigned long) strsize
);
1603 bfd_set_error (bfd_error_bad_value
);
1607 strings
= (char *) bfd_malloc (strsize
);
1608 if (strings
== NULL
)
1611 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1612 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1613 != strsize
- STRING_SIZE_SIZE
)
1619 obj_coff_strings (abfd
) = strings
;
1624 /* Free up the external symbols and strings read from a COFF file. */
1627 _bfd_coff_free_symbols (abfd
)
1630 if (obj_coff_external_syms (abfd
) != NULL
1631 && ! obj_coff_keep_syms (abfd
))
1633 free (obj_coff_external_syms (abfd
));
1634 obj_coff_external_syms (abfd
) = NULL
;
1636 if (obj_coff_strings (abfd
) != NULL
1637 && ! obj_coff_keep_strings (abfd
))
1639 free (obj_coff_strings (abfd
));
1640 obj_coff_strings (abfd
) = NULL
;
1645 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1646 knit the symbol names into a normalized form. By normalized here I
1647 mean that all symbols have an n_offset pointer that points to a null-
1648 terminated string. */
1650 combined_entry_type
*
1651 coff_get_normalized_symtab (abfd
)
1654 combined_entry_type
*internal
;
1655 combined_entry_type
*internal_ptr
;
1656 combined_entry_type
*symbol_ptr
;
1657 combined_entry_type
*internal_end
;
1658 bfd_size_type symesz
;
1661 const char *string_table
= NULL
;
1662 char *debug_section
= NULL
;
1665 if (obj_raw_syments (abfd
) != NULL
)
1666 return obj_raw_syments (abfd
);
1668 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1669 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1670 if (internal
== NULL
&& size
!= 0)
1672 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1674 if (! _bfd_coff_get_external_symbols (abfd
))
1677 raw_src
= (char *) obj_coff_external_syms (abfd
);
1679 /* mark the end of the symbols */
1680 symesz
= bfd_coff_symesz (abfd
);
1681 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1683 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1684 probably possible. If one shows up, it will probably kill us. */
1686 /* Swap all the raw entries */
1687 for (internal_ptr
= internal
;
1689 raw_src
+= symesz
, internal_ptr
++)
1693 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1694 (PTR
) & internal_ptr
->u
.syment
);
1695 symbol_ptr
= internal_ptr
;
1698 i
< symbol_ptr
->u
.syment
.n_numaux
;
1703 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1704 symbol_ptr
->u
.syment
.n_type
,
1705 symbol_ptr
->u
.syment
.n_sclass
,
1706 i
, symbol_ptr
->u
.syment
.n_numaux
,
1707 &(internal_ptr
->u
.auxent
));
1708 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1713 /* Free the raw symbols, but not the strings (if we have them). */
1714 obj_coff_keep_strings (abfd
) = true;
1715 if (! _bfd_coff_free_symbols (abfd
))
1718 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1721 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1722 && internal_ptr
->u
.syment
.n_numaux
> 0)
1724 /* make a file symbol point to the name in the auxent, since
1725 the text ".file" is redundant */
1726 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1728 /* the filename is a long one, point into the string table */
1729 if (string_table
== NULL
)
1731 string_table
= _bfd_coff_read_string_table (abfd
);
1732 if (string_table
== NULL
)
1736 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1739 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1743 /* ordinary short filename, put into memory anyway */
1744 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1745 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1751 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1753 /* This is a "short" name. Make it long. */
1754 unsigned long i
= 0;
1755 char *newstring
= NULL
;
1757 /* find the length of this string without walking into memory
1759 for (i
= 0; i
< 8; ++i
)
1761 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1764 } /* if end of string */
1765 } /* possible lengths of this string. */
1767 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1769 memset (newstring
, 0, i
);
1770 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1771 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1772 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1774 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1775 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1776 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1778 /* Long name already. Point symbol at the string in the
1780 if (string_table
== NULL
)
1782 string_table
= _bfd_coff_read_string_table (abfd
);
1783 if (string_table
== NULL
)
1786 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1789 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1793 /* Long name in debug section. Very similar. */
1794 if (debug_section
== NULL
)
1795 debug_section
= build_debug_section (abfd
);
1796 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1797 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1800 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1803 obj_raw_syments (abfd
) = internal
;
1804 BFD_ASSERT (obj_raw_syment_count (abfd
)
1805 == (unsigned int) (internal_ptr
- internal
));
1808 } /* coff_get_normalized_symtab() */
1811 coff_get_reloc_upper_bound (abfd
, asect
)
1815 if (bfd_get_format (abfd
) != bfd_object
)
1817 bfd_set_error (bfd_error_invalid_operation
);
1820 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1824 coff_make_empty_symbol (abfd
)
1827 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1830 memset (new, 0, sizeof *new);
1831 new->symbol
.section
= 0;
1833 new->lineno
= (alent
*) NULL
;
1834 new->done_lineno
= false;
1835 new->symbol
.the_bfd
= abfd
;
1836 return &new->symbol
;
1839 /* Make a debugging symbol. */
1842 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1847 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1850 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1851 (but shouldn't be a constant). */
1852 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1855 new->symbol
.section
= bfd_abs_section_ptr
;
1856 new->symbol
.flags
= BSF_DEBUGGING
;
1857 new->lineno
= (alent
*) NULL
;
1858 new->done_lineno
= false;
1859 new->symbol
.the_bfd
= abfd
;
1860 return &new->symbol
;
1865 coff_get_symbol_info (abfd
, symbol
, ret
)
1870 bfd_symbol_info (symbol
, ret
);
1871 if (coffsymbol (symbol
)->native
!= NULL
1872 && coffsymbol (symbol
)->native
->fix_value
)
1874 combined_entry_type
*psym
;
1876 psym
= ((combined_entry_type
*)
1877 coffsymbol (symbol
)->native
->u
.syment
.n_value
);
1878 ret
->value
= (bfd_vma
) (psym
- obj_raw_syments (abfd
));
1882 /* Return the COFF syment for a symbol. */
1885 bfd_coff_get_syment (abfd
, symbol
, psyment
)
1888 struct internal_syment
*psyment
;
1890 coff_symbol_type
*csym
;
1892 csym
= coff_symbol_from (abfd
, symbol
);
1893 if (csym
== NULL
|| csym
->native
== NULL
)
1895 bfd_set_error (bfd_error_invalid_operation
);
1899 *psyment
= csym
->native
->u
.syment
;
1901 if (csym
->native
->fix_value
)
1902 psyment
->n_value
= ((combined_entry_type
*) psyment
->n_value
1903 - obj_raw_syments (abfd
));
1905 /* FIXME: We should handle fix_line here. */
1910 /* Return the COFF auxent for a symbol. */
1913 bfd_coff_get_auxent (abfd
, symbol
, indx
, pauxent
)
1917 union internal_auxent
*pauxent
;
1919 coff_symbol_type
*csym
;
1920 combined_entry_type
*ent
;
1922 csym
= coff_symbol_from (abfd
, symbol
);
1925 || csym
->native
== NULL
1926 || indx
>= csym
->native
->u
.syment
.n_numaux
)
1928 bfd_set_error (bfd_error_invalid_operation
);
1932 ent
= csym
->native
+ indx
+ 1;
1934 *pauxent
= ent
->u
.auxent
;
1937 pauxent
->x_sym
.x_tagndx
.l
=
1938 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
1939 - obj_raw_syments (abfd
));
1942 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1943 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1944 - obj_raw_syments (abfd
));
1946 if (ent
->fix_scnlen
)
1947 pauxent
->x_csect
.x_scnlen
.l
=
1948 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
1949 - obj_raw_syments (abfd
));
1954 /* Print out information about COFF symbol. */
1957 coff_print_symbol (abfd
, filep
, symbol
, how
)
1961 bfd_print_symbol_type how
;
1963 FILE *file
= (FILE *) filep
;
1967 case bfd_print_symbol_name
:
1968 fprintf (file
, "%s", symbol
->name
);
1971 case bfd_print_symbol_more
:
1972 fprintf (file
, "coff %s %s",
1973 coffsymbol (symbol
)->native
? "n" : "g",
1974 coffsymbol (symbol
)->lineno
? "l" : " ");
1977 case bfd_print_symbol_all
:
1978 if (coffsymbol (symbol
)->native
)
1982 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1983 combined_entry_type
*root
= obj_raw_syments (abfd
);
1984 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1986 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1988 if (! combined
->fix_value
)
1989 val
= (unsigned long) combined
->u
.syment
.n_value
;
1991 val
= ((unsigned long)
1992 ((combined_entry_type
*) combined
->u
.syment
.n_value
1996 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1997 combined
->u
.syment
.n_scnum
,
1998 combined
->u
.syment
.n_flags
,
1999 combined
->u
.syment
.n_type
,
2000 combined
->u
.syment
.n_sclass
,
2001 combined
->u
.syment
.n_numaux
,
2005 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2007 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2011 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2013 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2015 fprintf (file
, "\n");
2017 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2020 switch (combined
->u
.syment
.n_sclass
)
2023 fprintf (file
, "File ");
2027 if (combined
->u
.syment
.n_type
== T_NULL
)
2028 /* probably a section symbol? */
2030 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2031 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2032 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2033 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2034 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2035 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2036 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2037 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2038 auxp
->u
.auxent
.x_scn
.x_checksum
,
2039 auxp
->u
.auxent
.x_scn
.x_associated
,
2040 auxp
->u
.auxent
.x_scn
.x_comdat
);
2043 /* else fall through */
2046 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2047 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2048 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2051 fprintf (file
, " endndx %ld",
2053 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2061 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2063 while (l
->line_number
)
2065 fprintf (file
, "\n%4d : 0x%lx",
2068 (l
->u
.offset
+ symbol
->section
->vma
)));
2075 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2076 fprintf (file
, " %-5s %s %s %s",
2077 symbol
->section
->name
,
2078 coffsymbol (symbol
)->native
? "n" : "g",
2079 coffsymbol (symbol
)->lineno
? "l" : " ",
2085 /* Return whether a symbol name implies a local symbol. In COFF,
2086 local symbols generally start with ``.L''. Most targets use this
2087 function for the is_local_label_name entry point, but some may
2091 _bfd_coff_is_local_label_name (abfd
, name
)
2095 return name
[0] == '.' && name
[1] == 'L';
2098 /* Provided a BFD, a section and an offset into the section, calculate
2099 and return the name of the source file and the line nearest to the
2104 coff_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2105 functionname_ptr
, line_ptr
)
2110 CONST
char **filename_ptr
;
2111 CONST
char **functionname_ptr
;
2112 unsigned int *line_ptr
;
2116 unsigned int line_base
;
2117 coff_data_type
*cof
= coff_data (abfd
);
2118 /* Run through the raw syments if available */
2119 combined_entry_type
*p
;
2120 combined_entry_type
*pend
;
2122 struct coff_section_tdata
*sec_data
;
2124 /* Before looking through the symbol table, try to use a .stab
2125 section to find the information. */
2126 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2127 &found
, filename_ptr
,
2128 functionname_ptr
, line_ptr
,
2129 &coff_data (abfd
)->line_info
))
2135 *functionname_ptr
= 0;
2138 /* Don't try and find line numbers in a non coff file */
2139 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
2145 /* Find the first C_FILE symbol. */
2146 p
= cof
->raw_syments
;
2150 pend
= p
+ cof
->raw_syment_count
;
2153 if (p
->u
.syment
.n_sclass
== C_FILE
)
2155 p
+= 1 + p
->u
.syment
.n_numaux
;
2162 /* Look through the C_FILE symbols to find the best one. */
2163 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2164 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2167 combined_entry_type
*p2
;
2169 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2171 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2173 if (p2
->u
.syment
.n_scnum
> 0
2175 == coff_section_from_bfd_index (abfd
,
2176 p2
->u
.syment
.n_scnum
)))
2178 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2186 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
2187 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
2189 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2190 maxdiff
= offset
- p2
->u
.syment
.n_value
;
2193 /* Avoid endless loops on erroneous files by ensuring that
2194 we always move forward in the file. */
2195 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
2198 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2199 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2204 /* Now wander though the raw linenumbers of the section */
2205 /* If we have been called on this section before, and the offset we
2206 want is further down then we can prime the lookup loop. */
2207 sec_data
= coff_section_data (abfd
, section
);
2208 if (sec_data
!= NULL
2210 && offset
>= sec_data
->offset
)
2213 *functionname_ptr
= sec_data
->function
;
2214 line_base
= sec_data
->line_base
;
2222 if (section
->lineno
!= NULL
)
2224 l
= §ion
->lineno
[i
];
2226 for (; i
< section
->lineno_count
; i
++)
2228 if (l
->line_number
== 0)
2230 /* Get the symbol this line number points at */
2231 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2232 if (coff
->symbol
.value
> offset
)
2234 *functionname_ptr
= coff
->symbol
.name
;
2237 combined_entry_type
*s
= coff
->native
;
2238 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2240 /* In XCOFF a debugging symbol can follow the
2242 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2243 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2245 /* S should now point to the .bf of the function. */
2246 if (s
->u
.syment
.n_numaux
)
2248 /* The linenumber is stored in the auxent. */
2249 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2250 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2251 *line_ptr
= line_base
;
2257 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
2259 *line_ptr
= l
->line_number
+ line_base
- 1;
2265 /* Cache the results for the next call. */
2266 if (sec_data
== NULL
&& section
->owner
== abfd
)
2268 section
->used_by_bfd
=
2269 ((PTR
) bfd_zalloc (abfd
,
2270 sizeof (struct coff_section_tdata
)));
2271 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2273 if (sec_data
!= NULL
)
2275 sec_data
->offset
= offset
;
2277 sec_data
->function
= *functionname_ptr
;
2278 sec_data
->line_base
= line_base
;
2285 coff_sizeof_headers (abfd
, reloc
)
2293 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2297 size
= bfd_coff_filhsz (abfd
);
2300 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);