1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
24 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
26 /* This file contains COFF code that is not dependent on any
27 particular COFF target. There is only one version of this file in
28 libbfd.a, so no target specific code may be put in here. Or, to
31 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33 If you need to add some target specific behaviour, add a new hook
34 function to bfd_coff_backend_data.
36 Some of these functions are also called by the ECOFF routines.
37 Those functions may not use any COFF specific information, such as
43 #include "coff/internal.h"
46 static void coff_fix_symbol_name
47 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_size_type
*,
48 asection
**, bfd_size_type
*));
49 static boolean coff_write_symbol
50 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_vma
*,
51 bfd_size_type
*, asection
**, bfd_size_type
*));
52 static boolean coff_write_alien_symbol
53 PARAMS ((bfd
*, asymbol
*, bfd_vma
*, bfd_size_type
*,
54 asection
**, bfd_size_type
*));
55 static boolean coff_write_native_symbol
56 PARAMS ((bfd
*, coff_symbol_type
*, bfd_vma
*, bfd_size_type
*,
57 asection
**, bfd_size_type
*));
58 static void coff_pointerize_aux
59 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
60 unsigned int, combined_entry_type
*));
61 static boolean make_a_section_from_file
62 PARAMS ((bfd
*, struct internal_scnhdr
*, unsigned int));
63 static const bfd_target
*coff_real_object_p
64 PARAMS ((bfd
*, unsigned, struct internal_filehdr
*,
65 struct internal_aouthdr
*));
66 static void fixup_symbol_value
67 PARAMS ((bfd
*, coff_symbol_type
*, struct internal_syment
*));
68 static char *build_debug_section
70 static char *copy_name
71 PARAMS ((bfd
*, char *, size_t));
73 #define STRING_SIZE_SIZE (4)
75 /* Take a section header read from a coff file (in HOST byte order),
76 and make a BFD "section" out of it. This is used by ECOFF. */
78 make_a_section_from_file (abfd
, hdr
, target_index
)
80 struct internal_scnhdr
*hdr
;
81 unsigned int target_index
;
83 asection
*return_section
;
85 boolean result
= true;
90 /* Handle long section names as in PE. */
91 if (bfd_coff_long_section_names (abfd
)
92 && hdr
->s_name
[0] == '/')
99 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
100 buf
[SCNNMLEN
- 1] = '\0';
101 strindex
= strtol (buf
, &p
, 10);
102 if (*p
== '\0' && strindex
>= 0)
104 strings
= _bfd_coff_read_string_table (abfd
);
107 /* FIXME: For extra safety, we should make sure that
108 strindex does not run us past the end, but right now we
109 don't know the length of the string table. */
111 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (strings
) + 1);
114 strcpy (name
, strings
);
120 /* Assorted wastage to null-terminate the name, thanks AT&T! */
121 name
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (hdr
->s_name
) + 1);
124 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
125 name
[sizeof (hdr
->s_name
)] = 0;
128 return_section
= bfd_make_section_anyway (abfd
, name
);
129 if (return_section
== NULL
)
132 return_section
->vma
= hdr
->s_vaddr
;
133 return_section
->lma
= hdr
->s_paddr
;
134 return_section
->_raw_size
= hdr
->s_size
;
135 return_section
->filepos
= hdr
->s_scnptr
;
136 return_section
->rel_filepos
= hdr
->s_relptr
;
137 return_section
->reloc_count
= hdr
->s_nreloc
;
139 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
141 return_section
->line_filepos
= hdr
->s_lnnoptr
;
143 return_section
->lineno_count
= hdr
->s_nlnno
;
144 return_section
->userdata
= NULL
;
145 return_section
->next
= (asection
*) NULL
;
146 return_section
->target_index
= target_index
;
148 if (! bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, return_section
,
152 return_section
->flags
= flags
;
154 /* At least on i386-coff, the line number count for a shared library
155 section must be ignored. */
156 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
157 return_section
->lineno_count
= 0;
159 if (hdr
->s_nreloc
!= 0)
160 return_section
->flags
|= SEC_RELOC
;
161 /* FIXME: should this check 'hdr->s_size > 0' */
162 if (hdr
->s_scnptr
!= 0)
163 return_section
->flags
|= SEC_HAS_CONTENTS
;
168 /* Read in a COFF object and make it into a BFD. This is used by
171 static const bfd_target
*
172 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
175 struct internal_filehdr
*internal_f
;
176 struct internal_aouthdr
*internal_a
;
178 flagword oflags
= abfd
->flags
;
179 bfd_vma ostart
= bfd_get_start_address (abfd
);
181 bfd_size_type readsize
; /* length of file_info */
183 char *external_sections
;
185 if (!(internal_f
->f_flags
& F_RELFLG
))
186 abfd
->flags
|= HAS_RELOC
;
187 if ((internal_f
->f_flags
& F_EXEC
))
188 abfd
->flags
|= EXEC_P
;
189 if (!(internal_f
->f_flags
& F_LNNO
))
190 abfd
->flags
|= HAS_LINENO
;
191 if (!(internal_f
->f_flags
& F_LSYMS
))
192 abfd
->flags
|= HAS_LOCALS
;
194 /* FIXME: How can we set D_PAGED correctly? */
195 if ((internal_f
->f_flags
& F_EXEC
) != 0)
196 abfd
->flags
|= D_PAGED
;
198 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
199 if (internal_f
->f_nsyms
)
200 abfd
->flags
|= HAS_SYMS
;
202 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
203 bfd_get_start_address (abfd
) = internal_a
->entry
;
205 bfd_get_start_address (abfd
) = 0;
207 /* Set up the tdata area. ECOFF uses its own routine, and overrides
209 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
213 scnhsz
= bfd_coff_scnhsz (abfd
);
214 readsize
= (bfd_size_type
) nscns
* scnhsz
;
215 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
216 if (!external_sections
)
219 if (bfd_bread ((PTR
) external_sections
, readsize
, abfd
) != readsize
)
222 /* Set the arch/mach *before* swapping in sections; section header swapping
223 may depend on arch/mach info. */
224 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
227 /* Now copy data as required; construct all asections etc */
231 for (i
= 0; i
< nscns
; i
++)
233 struct internal_scnhdr tmp
;
234 bfd_coff_swap_scnhdr_in (abfd
,
235 (PTR
) (external_sections
+ i
* scnhsz
),
237 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
242 /* make_abs_section (abfd); */
247 bfd_release (abfd
, tdata
);
248 abfd
->flags
= oflags
;
249 bfd_get_start_address (abfd
) = ostart
;
250 return (const bfd_target
*) NULL
;
253 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
254 not a COFF file. This is also used by ECOFF. */
260 bfd_size_type filhsz
;
261 bfd_size_type aoutsz
;
264 struct internal_filehdr internal_f
;
265 struct internal_aouthdr internal_a
;
267 /* figure out how much to read */
268 filhsz
= bfd_coff_filhsz (abfd
);
269 aoutsz
= bfd_coff_aoutsz (abfd
);
271 filehdr
= bfd_alloc (abfd
, filhsz
);
274 if (bfd_bread (filehdr
, filhsz
, abfd
) != filhsz
)
276 if (bfd_get_error () != bfd_error_system_call
)
277 bfd_set_error (bfd_error_wrong_format
);
280 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
281 bfd_release (abfd
, filehdr
);
283 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false
284 || internal_f
.f_opthdr
> aoutsz
)
286 bfd_set_error (bfd_error_wrong_format
);
289 nscns
= internal_f
.f_nscns
;
291 if (internal_f
.f_opthdr
)
295 opthdr
= bfd_alloc (abfd
, aoutsz
);
298 if (bfd_bread (opthdr
, (bfd_size_type
) internal_f
.f_opthdr
, abfd
)
299 != internal_f
.f_opthdr
)
303 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) &internal_a
);
306 return coff_real_object_p (abfd
, nscns
, &internal_f
,
307 (internal_f
.f_opthdr
!= 0
309 : (struct internal_aouthdr
*) NULL
));
312 /* Get the BFD section from a COFF symbol section number. */
315 coff_section_from_bfd_index (abfd
, index
)
319 struct sec
*answer
= abfd
->sections
;
322 return bfd_abs_section_ptr
;
323 if (index
== N_UNDEF
)
324 return bfd_und_section_ptr
;
325 if (index
== N_DEBUG
)
326 return bfd_abs_section_ptr
;
330 if (answer
->target_index
== index
)
332 answer
= answer
->next
;
335 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
336 has a bad symbol table in biglitpow.o. */
337 return bfd_und_section_ptr
;
340 /* Get the upper bound of a COFF symbol table. */
343 coff_get_symtab_upper_bound (abfd
)
346 if (!bfd_coff_slurp_symbol_table (abfd
))
349 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
352 /* Canonicalize a COFF symbol table. */
355 coff_get_symtab (abfd
, alocation
)
359 unsigned int counter
;
360 coff_symbol_type
*symbase
;
361 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
363 if (!bfd_coff_slurp_symbol_table (abfd
))
366 symbase
= obj_symbols (abfd
);
367 counter
= bfd_get_symcount (abfd
);
368 while (counter
-- > 0)
369 *location
++ = symbase
++;
373 return bfd_get_symcount (abfd
);
376 /* Get the name of a symbol. The caller must pass in a buffer of size
380 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
382 const struct internal_syment
*sym
;
385 /* FIXME: It's not clear this will work correctly if sizeof
387 if (sym
->_n
._n_n
._n_zeroes
!= 0
388 || sym
->_n
._n_n
._n_offset
== 0)
390 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
391 buf
[SYMNMLEN
] = '\0';
398 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
399 strings
= obj_coff_strings (abfd
);
402 strings
= _bfd_coff_read_string_table (abfd
);
406 return strings
+ sym
->_n
._n_n
._n_offset
;
410 /* Read in and swap the relocs. This returns a buffer holding the
411 relocs for section SEC in file ABFD. If CACHE is true and
412 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
413 the function is called again. If EXTERNAL_RELOCS is not NULL, it
414 is a buffer large enough to hold the unswapped relocs. If
415 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
416 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
417 value must be INTERNAL_RELOCS. The function returns NULL on error. */
419 struct internal_reloc
*
420 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
421 require_internal
, internal_relocs
)
425 bfd_byte
*external_relocs
;
426 boolean require_internal
;
427 struct internal_reloc
*internal_relocs
;
430 bfd_byte
*free_external
= NULL
;
431 struct internal_reloc
*free_internal
= NULL
;
434 struct internal_reloc
*irel
;
437 if (coff_section_data (abfd
, sec
) != NULL
438 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
440 if (! require_internal
)
441 return coff_section_data (abfd
, sec
)->relocs
;
442 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
443 sec
->reloc_count
* sizeof (struct internal_reloc
));
444 return internal_relocs
;
447 relsz
= bfd_coff_relsz (abfd
);
449 amt
= sec
->reloc_count
* relsz
;
450 if (external_relocs
== NULL
)
452 free_external
= (bfd_byte
*) bfd_malloc (amt
);
453 if (free_external
== NULL
&& sec
->reloc_count
> 0)
455 external_relocs
= free_external
;
458 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
459 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
462 if (internal_relocs
== NULL
)
464 amt
= sec
->reloc_count
;
465 amt
*= sizeof (struct internal_reloc
);
466 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
467 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
469 internal_relocs
= free_internal
;
472 /* Swap in the relocs. */
473 erel
= external_relocs
;
474 erel_end
= erel
+ relsz
* sec
->reloc_count
;
475 irel
= internal_relocs
;
476 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
477 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
479 if (free_external
!= NULL
)
481 free (free_external
);
482 free_external
= NULL
;
485 if (cache
&& free_internal
!= NULL
)
487 if (coff_section_data (abfd
, sec
) == NULL
)
489 amt
= sizeof (struct coff_section_tdata
);
490 sec
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
491 if (sec
->used_by_bfd
== NULL
)
493 coff_section_data (abfd
, sec
)->contents
= NULL
;
495 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
498 return internal_relocs
;
501 if (free_external
!= NULL
)
502 free (free_external
);
503 if (free_internal
!= NULL
)
504 free (free_internal
);
508 /* Set lineno_count for the output sections of a COFF file. */
511 coff_count_linenumbers (abfd
)
514 unsigned int limit
= bfd_get_symcount (abfd
);
522 /* This may be from the backend linker, in which case the
523 lineno_count in the sections is correct. */
524 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
525 total
+= s
->lineno_count
;
529 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
530 BFD_ASSERT (s
->lineno_count
== 0);
532 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
534 asymbol
*q_maybe
= *p
;
536 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
538 coff_symbol_type
*q
= coffsymbol (q_maybe
);
540 /* The AIX 4.1 compiler can sometimes generate line numbers
541 attached to debugging symbols. We try to simply ignore
543 if (q
->lineno
!= NULL
544 && q
->symbol
.section
->owner
!= NULL
)
546 /* This symbol has line numbers. Increment the owning
547 section's linenumber count. */
548 alent
*l
= q
->lineno
;
550 ++q
->symbol
.section
->output_section
->lineno_count
;
553 while (l
->line_number
!= 0)
556 ++q
->symbol
.section
->output_section
->lineno_count
;
566 /* Takes a bfd and a symbol, returns a pointer to the coff specific
567 area of the symbol if there is one. */
570 coff_symbol_from (ignore_abfd
, symbol
)
571 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
574 if (!bfd_family_coff (bfd_asymbol_bfd (symbol
)))
575 return (coff_symbol_type
*) NULL
;
577 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
578 return (coff_symbol_type
*) NULL
;
580 return (coff_symbol_type
*) symbol
;
584 fixup_symbol_value (abfd
, coff_symbol_ptr
, syment
)
586 coff_symbol_type
*coff_symbol_ptr
;
587 struct internal_syment
*syment
;
590 /* Normalize the symbol flags */
591 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
593 /* a common symbol is undefined with a value */
594 syment
->n_scnum
= N_UNDEF
;
595 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
597 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
598 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
600 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
602 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
604 syment
->n_scnum
= N_UNDEF
;
607 /* FIXME: Do we need to handle the absolute section here? */
610 if (coff_symbol_ptr
->symbol
.section
)
613 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
615 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
616 + coff_symbol_ptr
->symbol
.section
->output_offset
);
619 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
620 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
621 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
627 /* This can happen, but I don't know why yet (steve@cygnus.com) */
628 syment
->n_scnum
= N_ABS
;
629 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
634 /* Run through all the symbols in the symbol table and work out what
635 their indexes into the symbol table will be when output.
637 Coff requires that each C_FILE symbol points to the next one in the
638 chain, and that the last one points to the first external symbol. We
642 coff_renumber_symbols (bfd_ptr
, first_undef
)
646 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
647 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
648 unsigned int native_index
= 0;
649 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
650 unsigned int symbol_index
;
652 /* COFF demands that undefined symbols come after all other symbols.
653 Since we don't need to impose this extra knowledge on all our
654 client programs, deal with that here. Sort the symbol table;
655 just move the undefined symbols to the end, leaving the rest
656 alone. The O'Reilly book says that defined global symbols come
657 at the end before the undefined symbols, so we do that here as
659 /* @@ Do we have some condition we could test for, so we don't always
660 have to do this? I don't think relocatability is quite right, but
661 I'm not certain. [raeburn:19920508.1711EST] */
667 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
668 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
671 bfd_ptr
->outsymbols
= newsyms
;
672 for (i
= 0; i
< symbol_count
; i
++)
673 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
674 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
675 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
676 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
677 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
679 *newsyms
++ = symbol_ptr_ptr
[i
];
681 for (i
= 0; i
< symbol_count
; i
++)
682 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
683 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
684 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
685 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
686 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
688 *newsyms
++ = symbol_ptr_ptr
[i
];
690 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
692 for (i
= 0; i
< symbol_count
; i
++)
693 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
694 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
695 *newsyms
++ = symbol_ptr_ptr
[i
];
696 *newsyms
= (asymbol
*) NULL
;
697 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
700 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
702 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
703 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
704 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
706 combined_entry_type
*s
= coff_symbol_ptr
->native
;
709 if (s
->u
.syment
.n_sclass
== C_FILE
)
711 if (last_file
!= (struct internal_syment
*) NULL
)
712 last_file
->n_value
= native_index
;
713 last_file
= &(s
->u
.syment
);
718 /* Modify the symbol values according to their section and
721 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
723 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
724 s
[i
].offset
= native_index
++;
731 obj_conv_table_size (bfd_ptr
) = native_index
;
736 /* Run thorough the symbol table again, and fix it so that all
737 pointers to entries are changed to the entries' index in the output
741 coff_mangle_symbols (bfd_ptr
)
744 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
745 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
746 unsigned int symbol_index
;
748 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
750 coff_symbol_type
*coff_symbol_ptr
=
751 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
753 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
756 combined_entry_type
*s
= coff_symbol_ptr
->native
;
760 /* FIXME: We should use a union here. */
761 s
->u
.syment
.n_value
=
762 (bfd_vma
)((combined_entry_type
*)
763 ((unsigned long) s
->u
.syment
.n_value
))->offset
;
768 /* The value is the offset into the line number entries
769 for the symbol's section. On output, the symbol's
770 section should be N_DEBUG. */
771 s
->u
.syment
.n_value
=
772 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
773 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
774 coff_symbol_ptr
->symbol
.section
=
775 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
776 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
778 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
780 combined_entry_type
*a
= s
+ i
+ 1;
783 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
784 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
789 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
790 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
795 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
796 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
805 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
806 debug_string_section_p
, debug_string_size_p
)
809 combined_entry_type
*native
;
810 bfd_size_type
*string_size_p
;
811 asection
**debug_string_section_p
;
812 bfd_size_type
*debug_string_size_p
;
814 unsigned int name_length
;
815 union internal_auxent
*auxent
;
816 char *name
= (char *) (symbol
->name
);
818 if (name
== (char *) NULL
)
820 /* coff symbols always have names, so we'll make one up */
821 symbol
->name
= "strange";
822 name
= (char *) symbol
->name
;
824 name_length
= strlen (name
);
826 if (native
->u
.syment
.n_sclass
== C_FILE
827 && native
->u
.syment
.n_numaux
> 0)
829 unsigned int filnmlen
;
831 if (bfd_coff_force_symnames_in_strings (abfd
))
833 native
->u
.syment
._n
._n_n
._n_offset
=
834 (*string_size_p
+ STRING_SIZE_SIZE
);
835 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
836 *string_size_p
+= 6; /* strlen(".file") + 1 */
839 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
841 auxent
= &(native
+ 1)->u
.auxent
;
843 filnmlen
= bfd_coff_filnmlen (abfd
);
845 if (bfd_coff_long_filenames (abfd
))
847 if (name_length
<= filnmlen
)
849 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
853 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
854 auxent
->x_file
.x_n
.x_zeroes
= 0;
855 *string_size_p
+= name_length
+ 1;
860 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
861 if (name_length
> filnmlen
)
862 name
[filnmlen
] = '\0';
867 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
869 /* This name will fit into the symbol neatly */
870 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
872 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
874 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
876 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
877 *string_size_p
+= name_length
+ 1;
883 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
885 /* This name should be written into the .debug section. For
886 some reason each name is preceded by a two byte length
887 and also followed by a null byte. FIXME: We assume that
888 the .debug section has already been created, and that it
890 if (*debug_string_section_p
== (asection
*) NULL
)
891 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
892 filepos
= bfd_tell (abfd
);
894 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
896 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
898 if (!bfd_set_section_contents (abfd
,
899 *debug_string_section_p
,
901 (file_ptr
) *debug_string_size_p
,
902 (bfd_size_type
) prefix_len
)
903 || !bfd_set_section_contents (abfd
,
904 *debug_string_section_p
,
906 (file_ptr
) (*debug_string_size_p
908 (bfd_size_type
) name_length
+ 1))
910 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
912 native
->u
.syment
._n
._n_n
._n_offset
=
913 *debug_string_size_p
+ prefix_len
;
914 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
915 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
920 /* We need to keep track of the symbol index so that when we write out
921 the relocs we can get the index for a symbol. This method is a
924 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
926 /* Write a symbol out to a COFF file. */
929 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
930 debug_string_section_p
, debug_string_size_p
)
933 combined_entry_type
*native
;
935 bfd_size_type
*string_size_p
;
936 asection
**debug_string_section_p
;
937 bfd_size_type
*debug_string_size_p
;
939 unsigned int numaux
= native
->u
.syment
.n_numaux
;
940 int type
= native
->u
.syment
.n_type
;
941 int class = native
->u
.syment
.n_sclass
;
943 bfd_size_type symesz
;
945 if (native
->u
.syment
.n_sclass
== C_FILE
)
946 symbol
->flags
|= BSF_DEBUGGING
;
948 if (symbol
->flags
& BSF_DEBUGGING
949 && bfd_is_abs_section (symbol
->section
))
951 native
->u
.syment
.n_scnum
= N_DEBUG
;
953 else if (bfd_is_abs_section (symbol
->section
))
955 native
->u
.syment
.n_scnum
= N_ABS
;
957 else if (bfd_is_und_section (symbol
->section
))
959 native
->u
.syment
.n_scnum
= N_UNDEF
;
963 native
->u
.syment
.n_scnum
=
964 symbol
->section
->output_section
->target_index
;
967 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
968 debug_string_section_p
, debug_string_size_p
);
970 symesz
= bfd_coff_symesz (abfd
);
971 buf
= bfd_alloc (abfd
, symesz
);
974 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
975 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
977 bfd_release (abfd
, buf
);
979 if (native
->u
.syment
.n_numaux
> 0)
981 bfd_size_type auxesz
;
984 auxesz
= bfd_coff_auxesz (abfd
);
985 buf
= bfd_alloc (abfd
, auxesz
);
988 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
990 bfd_coff_swap_aux_out (abfd
,
991 &((native
+ j
+ 1)->u
.auxent
),
995 native
->u
.syment
.n_numaux
,
997 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
1000 bfd_release (abfd
, buf
);
1003 /* Store the index for use when we write out the relocs. */
1004 set_index (symbol
, *written
);
1006 *written
+= numaux
+ 1;
1010 /* Write out a symbol to a COFF file that does not come from a COFF
1011 file originally. This symbol may have been created by the linker,
1012 or we may be linking a non COFF file to a COFF file. */
1015 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
1016 debug_string_section_p
, debug_string_size_p
)
1020 bfd_size_type
*string_size_p
;
1021 asection
**debug_string_section_p
;
1022 bfd_size_type
*debug_string_size_p
;
1024 combined_entry_type
*native
;
1025 combined_entry_type dummy
;
1028 native
->u
.syment
.n_type
= T_NULL
;
1029 native
->u
.syment
.n_flags
= 0;
1030 if (bfd_is_und_section (symbol
->section
))
1032 native
->u
.syment
.n_scnum
= N_UNDEF
;
1033 native
->u
.syment
.n_value
= symbol
->value
;
1035 else if (bfd_is_com_section (symbol
->section
))
1037 native
->u
.syment
.n_scnum
= N_UNDEF
;
1038 native
->u
.syment
.n_value
= symbol
->value
;
1040 else if (symbol
->flags
& BSF_DEBUGGING
)
1042 /* There isn't much point to writing out a debugging symbol
1043 unless we are prepared to convert it into COFF debugging
1044 format. So, we just ignore them. We must clobber the symbol
1045 name to keep it from being put in the string table. */
1051 native
->u
.syment
.n_scnum
=
1052 symbol
->section
->output_section
->target_index
;
1053 native
->u
.syment
.n_value
= (symbol
->value
1054 + symbol
->section
->output_offset
);
1055 if (! obj_pe (abfd
))
1056 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
1058 /* Copy the any flags from the file header into the symbol.
1061 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1062 if (c
!= (coff_symbol_type
*) NULL
)
1063 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1067 native
->u
.syment
.n_type
= 0;
1068 if (symbol
->flags
& BSF_LOCAL
)
1069 native
->u
.syment
.n_sclass
= C_STAT
;
1070 else if (symbol
->flags
& BSF_WEAK
)
1071 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1073 native
->u
.syment
.n_sclass
= C_EXT
;
1074 native
->u
.syment
.n_numaux
= 0;
1076 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1077 debug_string_section_p
, debug_string_size_p
);
1080 /* Write a native symbol to a COFF file. */
1083 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1084 debug_string_section_p
, debug_string_size_p
)
1086 coff_symbol_type
*symbol
;
1088 bfd_size_type
*string_size_p
;
1089 asection
**debug_string_section_p
;
1090 bfd_size_type
*debug_string_size_p
;
1092 combined_entry_type
*native
= symbol
->native
;
1093 alent
*lineno
= symbol
->lineno
;
1095 /* If this symbol has an associated line number, we must store the
1096 symbol index in the line number field. We also tag the auxent to
1097 point to the right place in the lineno table. */
1098 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1100 unsigned int count
= 0;
1101 lineno
[count
].u
.offset
= *written
;
1102 if (native
->u
.syment
.n_numaux
)
1104 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1106 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1107 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1110 /* Count and relocate all other linenumbers. */
1112 while (lineno
[count
].line_number
!= 0)
1116 I've been told this, but still need proof:
1117 > The second bug is also in `bfd/coffcode.h'. This bug
1118 > causes the linker to screw up the pc-relocations for
1119 > all the line numbers in COFF code. This bug isn't only
1120 > specific to A29K implementations, but affects all
1121 > systems using COFF format binaries. Note that in COFF
1122 > object files, the line number core offsets output by
1123 > the assembler are relative to the start of each
1124 > procedure, not to the start of the .text section. This
1125 > patch relocates the line numbers relative to the
1126 > `native->u.syment.n_value' instead of the section
1128 > modular!olson@cs.arizona.edu (Jon Olson)
1130 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1132 lineno
[count
].u
.offset
+=
1133 (symbol
->symbol
.section
->output_section
->vma
1134 + symbol
->symbol
.section
->output_offset
);
1138 symbol
->done_lineno
= true;
1140 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1141 count
* bfd_coff_linesz (abfd
);
1144 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1145 string_size_p
, debug_string_section_p
,
1146 debug_string_size_p
);
1149 /* Write out the COFF symbols. */
1152 coff_write_symbols (abfd
)
1155 bfd_size_type string_size
;
1156 asection
*debug_string_section
;
1157 bfd_size_type debug_string_size
;
1159 unsigned int limit
= bfd_get_symcount (abfd
);
1160 bfd_signed_vma written
= 0;
1164 debug_string_section
= NULL
;
1165 debug_string_size
= 0;
1167 /* If this target supports long section names, they must be put into
1168 the string table. This is supported by PE. This code must
1169 handle section names just as they are handled in
1170 coff_write_object_contents. */
1171 if (bfd_coff_long_section_names (abfd
))
1175 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1179 len
= strlen (o
->name
);
1181 string_size
+= len
+ 1;
1185 /* Seek to the right place */
1186 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1189 /* Output all the symbols we have */
1192 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1194 asymbol
*symbol
= *p
;
1195 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1197 if (c_symbol
== (coff_symbol_type
*) NULL
1198 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1200 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1201 &debug_string_section
,
1202 &debug_string_size
))
1207 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1208 &string_size
, &debug_string_section
,
1209 &debug_string_size
))
1214 obj_raw_syment_count (abfd
) = written
;
1216 /* Now write out strings */
1218 if (string_size
!= 0)
1220 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1221 bfd_byte buffer
[STRING_SIZE_SIZE
];
1223 #if STRING_SIZE_SIZE == 4
1224 H_PUT_32 (abfd
, size
, buffer
);
1226 #error Change H_PUT_32
1228 if (bfd_bwrite ((PTR
) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1232 /* Handle long section names. This code must handle section
1233 names just as they are handled in coff_write_object_contents. */
1234 if (bfd_coff_long_section_names (abfd
))
1238 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1242 len
= strlen (o
->name
);
1245 if (bfd_bwrite (o
->name
, (bfd_size_type
) (len
+ 1), abfd
)
1252 for (p
= abfd
->outsymbols
, i
= 0;
1257 size_t name_length
= strlen (q
->name
);
1258 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1261 /* Figure out whether the symbol name should go in the string
1262 table. Symbol names that are short enough are stored
1263 directly in the syment structure. File names permit a
1264 different, longer, length in the syment structure. On
1265 XCOFF, some symbol names are stored in the .debug section
1266 rather than in the string table. */
1268 if (c_symbol
== NULL
1269 || c_symbol
->native
== NULL
)
1271 /* This is not a COFF symbol, so it certainly is not a
1272 file name, nor does it go in the .debug section. */
1273 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1275 else if (bfd_coff_symname_in_debug (abfd
,
1276 &c_symbol
->native
->u
.syment
))
1278 /* This symbol name is in the XCOFF .debug section.
1279 Don't write it into the string table. */
1280 maxlen
= name_length
;
1282 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1283 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1285 if (bfd_coff_force_symnames_in_strings (abfd
))
1287 if (bfd_bwrite (".file", (bfd_size_type
) 6, abfd
) != 6)
1290 maxlen
= bfd_coff_filnmlen (abfd
);
1293 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1295 if (name_length
> maxlen
)
1297 if (bfd_bwrite ((PTR
) (q
->name
), (bfd_size_type
) name_length
+ 1,
1298 abfd
) != name_length
+ 1)
1305 /* We would normally not write anything here, but we'll write
1306 out 4 so that any stupid coff reader which tries to read the
1307 string table even when there isn't one won't croak. */
1308 unsigned int size
= STRING_SIZE_SIZE
;
1309 bfd_byte buffer
[STRING_SIZE_SIZE
];
1311 #if STRING_SIZE_SIZE == 4
1312 H_PUT_32 (abfd
, size
, buffer
);
1314 #error Change H_PUT_32
1316 if (bfd_bwrite ((PTR
) buffer
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1317 != STRING_SIZE_SIZE
)
1321 /* Make sure the .debug section was created to be the correct size.
1322 We should create it ourselves on the fly, but we don't because
1323 BFD won't let us write to any section until we know how large all
1324 the sections are. We could still do it by making another pass
1325 over the symbols. FIXME. */
1326 BFD_ASSERT (debug_string_size
== 0
1327 || (debug_string_section
!= (asection
*) NULL
1328 && (BFD_ALIGN (debug_string_size
,
1329 1 << debug_string_section
->alignment_power
)
1330 == bfd_section_size (abfd
, debug_string_section
))));
1336 coff_write_linenumbers (abfd
)
1340 bfd_size_type linesz
;
1343 linesz
= bfd_coff_linesz (abfd
);
1344 buff
= bfd_alloc (abfd
, linesz
);
1347 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1349 if (s
->lineno_count
)
1351 asymbol
**q
= abfd
->outsymbols
;
1352 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1354 /* Find all the linenumbers in this section */
1358 if (p
->section
->output_section
== s
)
1361 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1362 (bfd_asymbol_bfd (p
), p
));
1365 /* Found a linenumber entry, output */
1366 struct internal_lineno out
;
1367 memset ((PTR
) & out
, 0, sizeof (out
));
1369 out
.l_addr
.l_symndx
= l
->u
.offset
;
1370 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1371 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1375 while (l
->line_number
)
1377 out
.l_lnno
= l
->line_number
;
1378 out
.l_addr
.l_symndx
= l
->u
.offset
;
1379 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1380 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1391 bfd_release (abfd
, buff
);
1396 coff_get_lineno (ignore_abfd
, symbol
)
1397 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
1400 return coffsymbol (symbol
)->lineno
;
1405 /* This is only called from coff_add_missing_symbols, which has been
1409 coff_section_symbol (abfd
, name
)
1413 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1415 combined_entry_type
*csym
;
1418 csym
= coff_symbol_from (abfd
, sym
)->native
;
1419 /* Make sure back-end COFF stuff is there. */
1424 coff_symbol_type sym
;
1425 /* @@FIXME This shouldn't use a fixed size!! */
1426 combined_entry_type e
[10];
1429 f
= (struct foo
*) bfd_alloc (abfd
, (bfd_size_type
) sizeof (*f
));
1432 bfd_set_error (bfd_error_no_error
);
1435 memset ((char *) f
, 0, sizeof (*f
));
1436 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1438 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1439 csym
[0].u
.syment
.n_numaux
= 1;
1440 /* SF_SET_STATICS (sym); @@ ??? */
1441 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1442 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1443 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1445 if (sec
->output_section
== NULL
)
1447 sec
->output_section
= sec
;
1448 sec
->output_offset
= 0;
1456 /* This function transforms the offsets into the symbol table into
1457 pointers to syments. */
1460 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1462 combined_entry_type
*table_base
;
1463 combined_entry_type
*symbol
;
1464 unsigned int indaux
;
1465 combined_entry_type
*auxent
;
1467 unsigned int type
= symbol
->u
.syment
.n_type
;
1468 unsigned int class = symbol
->u
.syment
.n_sclass
;
1470 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1472 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1473 (abfd
, table_base
, symbol
, indaux
, auxent
))
1477 /* Don't bother if this is a file or a section */
1478 if (class == C_STAT
&& type
== T_NULL
)
1480 if (class == C_FILE
)
1483 /* Otherwise patch up */
1484 #define N_TMASK coff_data (abfd)->local_n_tmask
1485 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1486 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
|| class == C_FCN
)
1487 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1489 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1490 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1491 auxent
->fix_end
= 1;
1493 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1494 generate one, so we must be careful to ignore it. */
1495 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1497 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1498 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1499 auxent
->fix_tag
= 1;
1503 /* Allocate space for the ".debug" section, and read it.
1504 We did not read the debug section until now, because
1505 we didn't want to go to the trouble until someone needed it. */
1508 build_debug_section (abfd
)
1511 char *debug_section
;
1513 bfd_size_type sec_size
;
1515 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1519 bfd_set_error (bfd_error_no_debug_section
);
1523 sec_size
= bfd_get_section_size_before_reloc (sect
);
1524 debug_section
= (PTR
) bfd_alloc (abfd
, sec_size
);
1525 if (debug_section
== NULL
)
1528 /* Seek to the beginning of the `.debug' section and read it.
1529 Save the current position first; it is needed by our caller.
1530 Then read debug section and reset the file pointer. */
1532 position
= bfd_tell (abfd
);
1533 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1534 || bfd_bread (debug_section
, sec_size
, abfd
) != sec_size
1535 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1537 return debug_section
;
1540 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1541 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1542 be \0-terminated. */
1544 copy_name (abfd
, name
, maxlen
)
1552 for (len
= 0; len
< maxlen
; ++len
)
1554 if (name
[len
] == '\0')
1560 if ((newname
= (PTR
) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1562 strncpy (newname
, name
, len
);
1563 newname
[len
] = '\0';
1567 /* Read in the external symbols. */
1570 _bfd_coff_get_external_symbols (abfd
)
1573 bfd_size_type symesz
;
1577 if (obj_coff_external_syms (abfd
) != NULL
)
1580 symesz
= bfd_coff_symesz (abfd
);
1582 size
= obj_raw_syment_count (abfd
) * symesz
;
1584 syms
= (PTR
) bfd_malloc (size
);
1585 if (syms
== NULL
&& size
!= 0)
1588 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1589 || bfd_bread (syms
, size
, abfd
) != size
)
1596 obj_coff_external_syms (abfd
) = syms
;
1601 /* Read in the external strings. The strings are not loaded until
1602 they are needed. This is because we have no simple way of
1603 detecting a missing string table in an archive. */
1606 _bfd_coff_read_string_table (abfd
)
1609 char extstrsize
[STRING_SIZE_SIZE
];
1610 bfd_size_type strsize
;
1614 if (obj_coff_strings (abfd
) != NULL
)
1615 return obj_coff_strings (abfd
);
1617 if (obj_sym_filepos (abfd
) == 0)
1619 bfd_set_error (bfd_error_no_symbols
);
1623 pos
= obj_sym_filepos (abfd
);
1624 pos
+= obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
);
1625 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1628 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1629 != sizeof extstrsize
)
1631 if (bfd_get_error () != bfd_error_file_truncated
)
1634 /* There is no string table. */
1635 strsize
= STRING_SIZE_SIZE
;
1639 #if STRING_SIZE_SIZE == 4
1640 strsize
= H_GET_32 (abfd
, extstrsize
);
1642 #error Change H_GET_32
1646 if (strsize
< STRING_SIZE_SIZE
)
1648 (*_bfd_error_handler
)
1649 (_("%s: bad string table size %lu"), bfd_archive_filename (abfd
),
1650 (unsigned long) strsize
);
1651 bfd_set_error (bfd_error_bad_value
);
1655 strings
= (char *) bfd_malloc (strsize
);
1656 if (strings
== NULL
)
1659 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1660 != strsize
- STRING_SIZE_SIZE
)
1666 obj_coff_strings (abfd
) = strings
;
1671 /* Free up the external symbols and strings read from a COFF file. */
1674 _bfd_coff_free_symbols (abfd
)
1677 if (obj_coff_external_syms (abfd
) != NULL
1678 && ! obj_coff_keep_syms (abfd
))
1680 free (obj_coff_external_syms (abfd
));
1681 obj_coff_external_syms (abfd
) = NULL
;
1683 if (obj_coff_strings (abfd
) != NULL
1684 && ! obj_coff_keep_strings (abfd
))
1686 free (obj_coff_strings (abfd
));
1687 obj_coff_strings (abfd
) = NULL
;
1692 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1693 knit the symbol names into a normalized form. By normalized here I
1694 mean that all symbols have an n_offset pointer that points to a null-
1695 terminated string. */
1697 combined_entry_type
*
1698 coff_get_normalized_symtab (abfd
)
1701 combined_entry_type
*internal
;
1702 combined_entry_type
*internal_ptr
;
1703 combined_entry_type
*symbol_ptr
;
1704 combined_entry_type
*internal_end
;
1708 const char *string_table
= NULL
;
1709 char *debug_section
= NULL
;
1712 if (obj_raw_syments (abfd
) != NULL
)
1713 return obj_raw_syments (abfd
);
1715 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1716 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1717 if (internal
== NULL
&& size
!= 0)
1719 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1721 if (! _bfd_coff_get_external_symbols (abfd
))
1724 raw_src
= (char *) obj_coff_external_syms (abfd
);
1726 /* mark the end of the symbols */
1727 symesz
= bfd_coff_symesz (abfd
);
1728 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1730 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1731 probably possible. If one shows up, it will probably kill us. */
1733 /* Swap all the raw entries */
1734 for (internal_ptr
= internal
;
1736 raw_src
+= symesz
, internal_ptr
++)
1740 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1741 (PTR
) & internal_ptr
->u
.syment
);
1742 symbol_ptr
= internal_ptr
;
1745 i
< symbol_ptr
->u
.syment
.n_numaux
;
1750 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1751 symbol_ptr
->u
.syment
.n_type
,
1752 symbol_ptr
->u
.syment
.n_sclass
,
1753 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1754 &(internal_ptr
->u
.auxent
));
1755 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1760 /* Free the raw symbols, but not the strings (if we have them). */
1761 obj_coff_keep_strings (abfd
) = true;
1762 if (! _bfd_coff_free_symbols (abfd
))
1765 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1768 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1769 && internal_ptr
->u
.syment
.n_numaux
> 0)
1771 /* make a file symbol point to the name in the auxent, since
1772 the text ".file" is redundant */
1773 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1775 /* the filename is a long one, point into the string table */
1776 if (string_table
== NULL
)
1778 string_table
= _bfd_coff_read_string_table (abfd
);
1779 if (string_table
== NULL
)
1783 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1786 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1790 /* Ordinary short filename, put into memory anyway. The
1791 Microsoft PE tools sometimes store a filename in
1792 multiple AUX entries. */
1793 if (internal_ptr
->u
.syment
.n_numaux
> 1
1794 && coff_data (abfd
)->pe
)
1796 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1799 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1800 internal_ptr
->u
.syment
.n_numaux
* symesz
));
1804 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1807 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1808 (size_t) bfd_coff_filnmlen (abfd
)));
1814 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1816 /* This is a "short" name. Make it long. */
1820 /* find the length of this string without walking into memory
1822 for (i
= 0; i
< 8; ++i
)
1823 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1826 newstring
= (PTR
) bfd_alloc (abfd
, (bfd_size_type
) (i
+ 1));
1827 if (newstring
== NULL
)
1829 memset (newstring
, 0, i
+ 1);
1830 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1831 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1832 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1834 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1835 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1836 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1838 /* Long name already. Point symbol at the string in the
1840 if (string_table
== NULL
)
1842 string_table
= _bfd_coff_read_string_table (abfd
);
1843 if (string_table
== NULL
)
1846 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1849 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1853 /* Long name in debug section. Very similar. */
1854 if (debug_section
== NULL
)
1855 debug_section
= build_debug_section (abfd
);
1856 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1857 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1860 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1863 obj_raw_syments (abfd
) = internal
;
1864 BFD_ASSERT (obj_raw_syment_count (abfd
)
1865 == (unsigned int) (internal_ptr
- internal
));
1868 } /* coff_get_normalized_symtab() */
1871 coff_get_reloc_upper_bound (abfd
, asect
)
1875 if (bfd_get_format (abfd
) != bfd_object
)
1877 bfd_set_error (bfd_error_invalid_operation
);
1880 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1884 coff_make_empty_symbol (abfd
)
1887 bfd_size_type amt
= sizeof (coff_symbol_type
);
1888 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
1891 memset (new, 0, sizeof *new);
1892 new->symbol
.section
= 0;
1894 new->lineno
= (alent
*) NULL
;
1895 new->done_lineno
= false;
1896 new->symbol
.the_bfd
= abfd
;
1897 return &new->symbol
;
1900 /* Make a debugging symbol. */
1903 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1905 PTR ptr ATTRIBUTE_UNUSED
;
1906 unsigned long sz ATTRIBUTE_UNUSED
;
1908 bfd_size_type amt
= sizeof (coff_symbol_type
);
1909 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
1912 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1913 (but shouldn't be a constant). */
1914 amt
= sizeof (combined_entry_type
) * 10;
1915 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
1918 new->symbol
.section
= bfd_abs_section_ptr
;
1919 new->symbol
.flags
= BSF_DEBUGGING
;
1920 new->lineno
= (alent
*) NULL
;
1921 new->done_lineno
= false;
1922 new->symbol
.the_bfd
= abfd
;
1923 return &new->symbol
;
1927 coff_get_symbol_info (abfd
, symbol
, ret
)
1932 bfd_symbol_info (symbol
, ret
);
1933 if (coffsymbol (symbol
)->native
!= NULL
1934 && coffsymbol (symbol
)->native
->fix_value
)
1936 ret
->value
= coffsymbol (symbol
)->native
->u
.syment
.n_value
-
1937 (unsigned long) obj_raw_syments (abfd
);
1941 /* Return the COFF syment for a symbol. */
1944 bfd_coff_get_syment (abfd
, symbol
, psyment
)
1947 struct internal_syment
*psyment
;
1949 coff_symbol_type
*csym
;
1951 csym
= coff_symbol_from (abfd
, symbol
);
1952 if (csym
== NULL
|| csym
->native
== NULL
)
1954 bfd_set_error (bfd_error_invalid_operation
);
1958 *psyment
= csym
->native
->u
.syment
;
1960 if (csym
->native
->fix_value
)
1961 psyment
->n_value
= psyment
->n_value
-
1962 (unsigned long) obj_raw_syments (abfd
);
1964 /* FIXME: We should handle fix_line here. */
1969 /* Return the COFF auxent for a symbol. */
1972 bfd_coff_get_auxent (abfd
, symbol
, indx
, pauxent
)
1976 union internal_auxent
*pauxent
;
1978 coff_symbol_type
*csym
;
1979 combined_entry_type
*ent
;
1981 csym
= coff_symbol_from (abfd
, symbol
);
1984 || csym
->native
== NULL
1985 || indx
>= csym
->native
->u
.syment
.n_numaux
)
1987 bfd_set_error (bfd_error_invalid_operation
);
1991 ent
= csym
->native
+ indx
+ 1;
1993 *pauxent
= ent
->u
.auxent
;
1996 pauxent
->x_sym
.x_tagndx
.l
=
1997 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
1998 - obj_raw_syments (abfd
));
2001 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2002 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2003 - obj_raw_syments (abfd
));
2005 if (ent
->fix_scnlen
)
2006 pauxent
->x_csect
.x_scnlen
.l
=
2007 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
2008 - obj_raw_syments (abfd
));
2013 /* Print out information about COFF symbol. */
2016 coff_print_symbol (abfd
, filep
, symbol
, how
)
2020 bfd_print_symbol_type how
;
2022 FILE *file
= (FILE *) filep
;
2026 case bfd_print_symbol_name
:
2027 fprintf (file
, "%s", symbol
->name
);
2030 case bfd_print_symbol_more
:
2031 fprintf (file
, "coff %s %s",
2032 coffsymbol (symbol
)->native
? "n" : "g",
2033 coffsymbol (symbol
)->lineno
? "l" : " ");
2036 case bfd_print_symbol_all
:
2037 if (coffsymbol (symbol
)->native
)
2041 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2042 combined_entry_type
*root
= obj_raw_syments (abfd
);
2043 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2045 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2047 if (! combined
->fix_value
)
2048 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2050 val
= combined
->u
.syment
.n_value
- (unsigned long) root
;
2054 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2055 combined
->u
.syment
.n_scnum
,
2056 combined
->u
.syment
.n_flags
,
2057 combined
->u
.syment
.n_type
,
2058 combined
->u
.syment
.n_sclass
,
2059 combined
->u
.syment
.n_numaux
,
2060 (unsigned long) val
,
2063 /* Print out the wide, 64 bit, symbol value */
2065 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2066 combined
->u
.syment
.n_scnum
,
2067 combined
->u
.syment
.n_flags
,
2068 combined
->u
.syment
.n_type
,
2069 combined
->u
.syment
.n_sclass
,
2070 combined
->u
.syment
.n_numaux
,
2075 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2077 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2081 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2083 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2085 fprintf (file
, "\n");
2087 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2090 switch (combined
->u
.syment
.n_sclass
)
2093 fprintf (file
, "File ");
2097 if (combined
->u
.syment
.n_type
== T_NULL
)
2098 /* probably a section symbol? */
2100 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2101 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2102 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2103 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2104 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2105 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2106 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2107 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2108 auxp
->u
.auxent
.x_scn
.x_checksum
,
2109 auxp
->u
.auxent
.x_scn
.x_associated
,
2110 auxp
->u
.auxent
.x_scn
.x_comdat
);
2113 /* else fall through */
2115 if (ISFCN (combined
->u
.syment
.n_type
))
2120 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2123 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2124 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2126 _("AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld"),
2127 tagndx
, auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2131 /* else fall through */
2133 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2134 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2135 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2138 fprintf (file
, " endndx %ld",
2140 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2148 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2150 while (l
->line_number
)
2152 fprintf (file
, "\n%4d : 0x%lx",
2155 (l
->u
.offset
+ symbol
->section
->vma
)));
2162 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
2163 fprintf (file
, " %-5s %s %s %s",
2164 symbol
->section
->name
,
2165 coffsymbol (symbol
)->native
? "n" : "g",
2166 coffsymbol (symbol
)->lineno
? "l" : " ",
2172 /* Return whether a symbol name implies a local symbol. In COFF,
2173 local symbols generally start with ``.L''. Most targets use this
2174 function for the is_local_label_name entry point, but some may
2178 _bfd_coff_is_local_label_name (abfd
, name
)
2179 bfd
*abfd ATTRIBUTE_UNUSED
;
2182 return name
[0] == '.' && name
[1] == 'L';
2185 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2186 section, calculate and return the name of the source file and the line
2187 nearest to the wanted location. */
2190 coff_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2191 functionname_ptr
, line_ptr
)
2196 const char **filename_ptr
;
2197 const char **functionname_ptr
;
2198 unsigned int *line_ptr
;
2202 unsigned int line_base
;
2203 coff_data_type
*cof
= coff_data (abfd
);
2204 /* Run through the raw syments if available */
2205 combined_entry_type
*p
;
2206 combined_entry_type
*pend
;
2208 struct coff_section_tdata
*sec_data
;
2211 /* Before looking through the symbol table, try to use a .stab
2212 section to find the information. */
2213 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2214 &found
, filename_ptr
,
2215 functionname_ptr
, line_ptr
,
2216 &coff_data(abfd
)->line_info
))
2222 /* Also try examining DWARF2 debugging information. */
2223 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
2224 filename_ptr
, functionname_ptr
,
2226 &coff_data(abfd
)->dwarf2_find_line_info
))
2230 *functionname_ptr
= 0;
2233 /* Don't try and find line numbers in a non coff file */
2234 if (!bfd_family_coff (abfd
))
2240 /* Find the first C_FILE symbol. */
2241 p
= cof
->raw_syments
;
2245 pend
= p
+ cof
->raw_syment_count
;
2248 if (p
->u
.syment
.n_sclass
== C_FILE
)
2250 p
+= 1 + p
->u
.syment
.n_numaux
;
2258 /* Look through the C_FILE symbols to find the best one. */
2259 sec_vma
= bfd_get_section_vma (abfd
, section
);
2260 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2261 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2264 combined_entry_type
*p2
;
2266 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2268 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2270 if (p2
->u
.syment
.n_scnum
> 0
2272 == coff_section_from_bfd_index (abfd
,
2273 p2
->u
.syment
.n_scnum
)))
2275 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2282 /* We use <= MAXDIFF here so that if we get a zero length
2283 file, we actually use the next file entry. */
2285 && offset
+ sec_vma
>= (bfd_vma
) p2
->u
.syment
.n_value
2286 && offset
+ sec_vma
- (bfd_vma
) p2
->u
.syment
.n_value
<= maxdiff
)
2288 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2289 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2292 /* Avoid endless loops on erroneous files by ensuring that
2293 we always move forward in the file. */
2294 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2297 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2298 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2303 /* Now wander though the raw linenumbers of the section */
2304 /* If we have been called on this section before, and the offset we
2305 want is further down then we can prime the lookup loop. */
2306 sec_data
= coff_section_data (abfd
, section
);
2307 if (sec_data
!= NULL
2309 && offset
>= sec_data
->offset
)
2312 *functionname_ptr
= sec_data
->function
;
2313 line_base
= sec_data
->line_base
;
2321 if (section
->lineno
!= NULL
)
2323 bfd_vma last_value
= 0;
2325 l
= §ion
->lineno
[i
];
2327 for (; i
< section
->lineno_count
; i
++)
2329 if (l
->line_number
== 0)
2331 /* Get the symbol this line number points at */
2332 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2333 if (coff
->symbol
.value
> offset
)
2335 *functionname_ptr
= coff
->symbol
.name
;
2336 last_value
= coff
->symbol
.value
;
2339 combined_entry_type
*s
= coff
->native
;
2340 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2342 /* In XCOFF a debugging symbol can follow the
2344 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2345 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2347 /* S should now point to the .bf of the function. */
2348 if (s
->u
.syment
.n_numaux
)
2350 /* The linenumber is stored in the auxent. */
2351 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2352 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2353 *line_ptr
= line_base
;
2359 if (l
->u
.offset
> offset
)
2361 *line_ptr
= l
->line_number
+ line_base
- 1;
2366 /* If we fell off the end of the loop, then assume that this
2367 symbol has no line number info. Otherwise, symbols with no
2368 line number info get reported with the line number of the
2369 last line of the last symbol which does have line number
2370 info. We use 0x100 as a slop to account for cases where the
2371 last line has executable code. */
2372 if (i
>= section
->lineno_count
2374 && offset
- last_value
> 0x100)
2376 *functionname_ptr
= NULL
;
2381 /* Cache the results for the next call. */
2382 if (sec_data
== NULL
&& section
->owner
== abfd
)
2384 amt
= sizeof (struct coff_section_tdata
);
2385 section
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
2386 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2388 if (sec_data
!= NULL
)
2390 sec_data
->offset
= offset
;
2392 sec_data
->function
= *functionname_ptr
;
2393 sec_data
->line_base
= line_base
;
2400 coff_sizeof_headers (abfd
, reloc
)
2408 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2412 size
= bfd_coff_filhsz (abfd
);
2415 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2419 /* Change the class of a coff symbol held by BFD. */
2421 bfd_coff_set_symbol_class (abfd
, symbol
, class)
2426 coff_symbol_type
* csym
;
2428 csym
= coff_symbol_from (abfd
, symbol
);
2431 bfd_set_error (bfd_error_invalid_operation
);
2434 else if (csym
->native
== NULL
)
2436 /* This is an alien symbol which no native coff backend data.
2437 We cheat here by creating a fake native entry for it and
2438 then filling in the class. This code is based on that in
2439 coff_write_alien_symbol(). */
2441 combined_entry_type
* native
;
2442 bfd_size_type amt
= sizeof (* native
);
2444 native
= (combined_entry_type
*) bfd_alloc (abfd
, amt
);
2448 memset (native
, 0, sizeof (* native
));
2450 native
->u
.syment
.n_type
= T_NULL
;
2451 native
->u
.syment
.n_sclass
= class;
2453 if (bfd_is_und_section (symbol
->section
))
2455 native
->u
.syment
.n_scnum
= N_UNDEF
;
2456 native
->u
.syment
.n_value
= symbol
->value
;
2458 else if (bfd_is_com_section (symbol
->section
))
2460 native
->u
.syment
.n_scnum
= N_UNDEF
;
2461 native
->u
.syment
.n_value
= symbol
->value
;
2465 native
->u
.syment
.n_scnum
=
2466 symbol
->section
->output_section
->target_index
;
2467 native
->u
.syment
.n_value
= (symbol
->value
2468 + symbol
->section
->output_offset
);
2469 if (! obj_pe (abfd
))
2470 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2472 /* Copy the any flags from the file header into the symbol.
2474 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2477 csym
->native
= native
;
2481 csym
->native
->u
.syment
.n_sclass
= class;