1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
22 MA 02110-1301, USA. */
24 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
25 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
27 /* This file contains COFF code that is not dependent on any
28 particular COFF target. There is only one version of this file in
29 libbfd.a, so no target specific code may be put in here. Or, to
32 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
34 If you need to add some target specific behaviour, add a new hook
35 function to bfd_coff_backend_data.
37 Some of these functions are also called by the ECOFF routines.
38 Those functions may not use any COFF specific information, such as
44 #include "coff/internal.h"
47 /* Take a section header read from a coff file (in HOST byte order),
48 and make a BFD "section" out of it. This is used by ECOFF. */
51 make_a_section_from_file (bfd
*abfd
,
52 struct internal_scnhdr
*hdr
,
53 unsigned int target_index
)
55 asection
*return_section
;
57 bfd_boolean result
= TRUE
;
62 /* Handle long section names as in PE. On reading, we want to
63 accept long names if the format permits them at all, regardless
64 of the current state of the flag that dictates if we would generate
65 them in outputs; this construct checks if that is the case by
66 attempting to set the flag, without changing its state; the call
67 will fail for formats that do not support long names at all. */
68 if (bfd_coff_set_long_section_names (abfd
, bfd_coff_long_section_names (abfd
))
69 && hdr
->s_name
[0] == '/')
76 /* Flag that this BFD uses long names, even though the format might
77 expect them to be off by default. This won't directly affect the
78 format of any output BFD created from this one, but the information
79 can be used to decide what to do. */
80 bfd_coff_set_long_section_names (abfd
, TRUE
);
81 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
82 buf
[SCNNMLEN
- 1] = '\0';
83 strindex
= strtol (buf
, &p
, 10);
84 if (*p
== '\0' && strindex
>= 0)
86 strings
= _bfd_coff_read_string_table (abfd
);
89 /* FIXME: For extra safety, we should make sure that
90 strindex does not run us past the end, but right now we
91 don't know the length of the string table. */
93 name
= (char *) bfd_alloc (abfd
,
94 (bfd_size_type
) strlen (strings
) + 1 + 1);
97 strcpy (name
, strings
);
103 /* Assorted wastage to null-terminate the name, thanks AT&T! */
104 name
= (char *) bfd_alloc (abfd
,
105 (bfd_size_type
) sizeof (hdr
->s_name
) + 1 + 1);
108 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
109 name
[sizeof (hdr
->s_name
)] = 0;
112 return_section
= bfd_make_section_anyway (abfd
, name
);
113 if (return_section
== NULL
)
116 return_section
->vma
= hdr
->s_vaddr
;
117 return_section
->lma
= hdr
->s_paddr
;
118 return_section
->size
= hdr
->s_size
;
119 return_section
->filepos
= hdr
->s_scnptr
;
120 return_section
->rel_filepos
= hdr
->s_relptr
;
121 return_section
->reloc_count
= hdr
->s_nreloc
;
123 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
125 return_section
->line_filepos
= hdr
->s_lnnoptr
;
127 return_section
->lineno_count
= hdr
->s_nlnno
;
128 return_section
->userdata
= NULL
;
129 return_section
->next
= NULL
;
130 return_section
->target_index
= target_index
;
132 if (! bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, return_section
,
136 return_section
->flags
= flags
;
138 /* At least on i386-coff, the line number count for a shared library
139 section must be ignored. */
140 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
141 return_section
->lineno_count
= 0;
143 if (hdr
->s_nreloc
!= 0)
144 return_section
->flags
|= SEC_RELOC
;
145 /* FIXME: should this check 'hdr->s_size > 0'. */
146 if (hdr
->s_scnptr
!= 0)
147 return_section
->flags
|= SEC_HAS_CONTENTS
;
149 /* Compress/decompress DWARF debug sections with names: .debug_* and
150 .zdebug_*, after the section flags is set. */
151 if ((flags
& SEC_DEBUGGING
)
152 && ((name
[1] == 'd' && name
[6] == '_')
153 || (name
[1] == 'z' && name
[7] == '_')))
155 enum { nothing
, compress
, decompress
} action
= nothing
;
156 char *new_name
= NULL
;
158 if (bfd_is_section_compressed (abfd
, return_section
))
160 /* Compressed section. Check if we should decompress. */
161 if ((abfd
->flags
& BFD_DECOMPRESS
))
164 else if (!bfd_is_section_compressed (abfd
, return_section
))
166 /* Normal section. Check if we should compress. */
167 if ((abfd
->flags
& BFD_COMPRESS
) && return_section
->size
!= 0)
176 if (!bfd_init_section_compress_status (abfd
, return_section
))
178 (*_bfd_error_handler
)
179 (_("%B: unable to initialize compress status for section %s"),
185 unsigned int len
= strlen (name
);
187 new_name
= bfd_alloc (abfd
, len
+ 2);
188 if (new_name
== NULL
)
192 memcpy (new_name
+ 2, name
+ 1, len
);
196 if (!bfd_init_section_decompress_status (abfd
, return_section
))
198 (*_bfd_error_handler
)
199 (_("%B: unable to initialize decompress status for section %s"),
205 unsigned int len
= strlen (name
);
207 new_name
= bfd_alloc (abfd
, len
);
208 if (new_name
== NULL
)
211 memcpy (new_name
+ 1, name
+ 2, len
- 1);
215 if (new_name
!= NULL
)
216 bfd_rename_section (abfd
, return_section
, new_name
);
222 /* Read in a COFF object and make it into a BFD. This is used by
225 coff_real_object_p (bfd
*,
227 struct internal_filehdr
*,
228 struct internal_aouthdr
*);
230 coff_real_object_p (bfd
*abfd
,
232 struct internal_filehdr
*internal_f
,
233 struct internal_aouthdr
*internal_a
)
235 flagword oflags
= abfd
->flags
;
236 bfd_vma ostart
= bfd_get_start_address (abfd
);
239 bfd_size_type readsize
; /* Length of file_info. */
241 char *external_sections
;
243 if (!(internal_f
->f_flags
& F_RELFLG
))
244 abfd
->flags
|= HAS_RELOC
;
245 if ((internal_f
->f_flags
& F_EXEC
))
246 abfd
->flags
|= EXEC_P
;
247 if (!(internal_f
->f_flags
& F_LNNO
))
248 abfd
->flags
|= HAS_LINENO
;
249 if (!(internal_f
->f_flags
& F_LSYMS
))
250 abfd
->flags
|= HAS_LOCALS
;
252 /* FIXME: How can we set D_PAGED correctly? */
253 if ((internal_f
->f_flags
& F_EXEC
) != 0)
254 abfd
->flags
|= D_PAGED
;
256 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
257 if (internal_f
->f_nsyms
)
258 abfd
->flags
|= HAS_SYMS
;
260 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
261 bfd_get_start_address (abfd
) = internal_a
->entry
;
263 bfd_get_start_address (abfd
) = 0;
265 /* Set up the tdata area. ECOFF uses its own routine, and overrides
267 tdata_save
= abfd
->tdata
.any
;
268 tdata
= bfd_coff_mkobject_hook (abfd
, (void *) internal_f
, (void *) internal_a
);
272 scnhsz
= bfd_coff_scnhsz (abfd
);
273 readsize
= (bfd_size_type
) nscns
* scnhsz
;
274 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
275 if (!external_sections
)
278 if (bfd_bread ((void *) external_sections
, readsize
, abfd
) != readsize
)
281 /* Set the arch/mach *before* swapping in sections; section header swapping
282 may depend on arch/mach info. */
283 if (! bfd_coff_set_arch_mach_hook (abfd
, (void *) internal_f
))
286 /* Now copy data as required; construct all asections etc. */
290 for (i
= 0; i
< nscns
; i
++)
292 struct internal_scnhdr tmp
;
293 bfd_coff_swap_scnhdr_in (abfd
,
294 (void *) (external_sections
+ i
* scnhsz
),
296 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
304 bfd_release (abfd
, tdata
);
306 abfd
->tdata
.any
= tdata_save
;
307 abfd
->flags
= oflags
;
308 bfd_get_start_address (abfd
) = ostart
;
309 return (const bfd_target
*) NULL
;
312 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
313 not a COFF file. This is also used by ECOFF. */
316 coff_object_p (bfd
*abfd
)
318 bfd_size_type filhsz
;
319 bfd_size_type aoutsz
;
322 struct internal_filehdr internal_f
;
323 struct internal_aouthdr internal_a
;
325 /* Figure out how much to read. */
326 filhsz
= bfd_coff_filhsz (abfd
);
327 aoutsz
= bfd_coff_aoutsz (abfd
);
329 filehdr
= bfd_alloc (abfd
, filhsz
);
332 if (bfd_bread (filehdr
, filhsz
, abfd
) != filhsz
)
334 if (bfd_get_error () != bfd_error_system_call
)
335 bfd_set_error (bfd_error_wrong_format
);
336 bfd_release (abfd
, filehdr
);
339 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
340 bfd_release (abfd
, filehdr
);
342 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
343 (less than aoutsz) used in object files and AOUTSZ (equal to
344 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
345 expects this header to be aoutsz bytes in length, so we use that
346 value in the call to bfd_alloc below. But we must be careful to
347 only read in f_opthdr bytes in the call to bfd_bread. We should
348 also attempt to catch corrupt or non-COFF binaries with a strange
349 value for f_opthdr. */
350 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
351 || internal_f
.f_opthdr
> aoutsz
)
353 bfd_set_error (bfd_error_wrong_format
);
356 nscns
= internal_f
.f_nscns
;
358 if (internal_f
.f_opthdr
)
362 opthdr
= bfd_alloc (abfd
, aoutsz
);
365 if (bfd_bread (opthdr
, (bfd_size_type
) internal_f
.f_opthdr
, abfd
)
366 != internal_f
.f_opthdr
)
368 bfd_release (abfd
, opthdr
);
371 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (void *) &internal_a
);
372 bfd_release (abfd
, opthdr
);
375 return coff_real_object_p (abfd
, nscns
, &internal_f
,
376 (internal_f
.f_opthdr
!= 0
378 : (struct internal_aouthdr
*) NULL
));
381 /* Get the BFD section from a COFF symbol section number. */
384 coff_section_from_bfd_index (bfd
*abfd
, int section_index
)
386 struct bfd_section
*answer
= abfd
->sections
;
388 if (section_index
== N_ABS
)
389 return bfd_abs_section_ptr
;
390 if (section_index
== N_UNDEF
)
391 return bfd_und_section_ptr
;
392 if (section_index
== N_DEBUG
)
393 return bfd_abs_section_ptr
;
397 if (answer
->target_index
== section_index
)
399 answer
= answer
->next
;
402 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
403 has a bad symbol table in biglitpow.o. */
404 return bfd_und_section_ptr
;
407 /* Get the upper bound of a COFF symbol table. */
410 coff_get_symtab_upper_bound (bfd
*abfd
)
412 if (!bfd_coff_slurp_symbol_table (abfd
))
415 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
418 /* Canonicalize a COFF symbol table. */
421 coff_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
423 unsigned int counter
;
424 coff_symbol_type
*symbase
;
425 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
427 if (!bfd_coff_slurp_symbol_table (abfd
))
430 symbase
= obj_symbols (abfd
);
431 counter
= bfd_get_symcount (abfd
);
432 while (counter
-- > 0)
433 *location
++ = symbase
++;
437 return bfd_get_symcount (abfd
);
440 /* Get the name of a symbol. The caller must pass in a buffer of size
444 _bfd_coff_internal_syment_name (bfd
*abfd
,
445 const struct internal_syment
*sym
,
448 /* FIXME: It's not clear this will work correctly if sizeof
450 if (sym
->_n
._n_n
._n_zeroes
!= 0
451 || sym
->_n
._n_n
._n_offset
== 0)
453 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
454 buf
[SYMNMLEN
] = '\0';
461 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
462 strings
= obj_coff_strings (abfd
);
465 strings
= _bfd_coff_read_string_table (abfd
);
469 return strings
+ sym
->_n
._n_n
._n_offset
;
473 /* Read in and swap the relocs. This returns a buffer holding the
474 relocs for section SEC in file ABFD. If CACHE is TRUE and
475 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
476 the function is called again. If EXTERNAL_RELOCS is not NULL, it
477 is a buffer large enough to hold the unswapped relocs. If
478 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
479 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
480 value must be INTERNAL_RELOCS. The function returns NULL on error. */
482 struct internal_reloc
*
483 _bfd_coff_read_internal_relocs (bfd
*abfd
,
486 bfd_byte
*external_relocs
,
487 bfd_boolean require_internal
,
488 struct internal_reloc
*internal_relocs
)
491 bfd_byte
*free_external
= NULL
;
492 struct internal_reloc
*free_internal
= NULL
;
495 struct internal_reloc
*irel
;
498 if (sec
->reloc_count
== 0)
499 return internal_relocs
; /* Nothing to do. */
501 if (coff_section_data (abfd
, sec
) != NULL
502 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
504 if (! require_internal
)
505 return coff_section_data (abfd
, sec
)->relocs
;
506 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
507 sec
->reloc_count
* sizeof (struct internal_reloc
));
508 return internal_relocs
;
511 relsz
= bfd_coff_relsz (abfd
);
513 amt
= sec
->reloc_count
* relsz
;
514 if (external_relocs
== NULL
)
516 free_external
= (bfd_byte
*) bfd_malloc (amt
);
517 if (free_external
== NULL
)
519 external_relocs
= free_external
;
522 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
523 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
526 if (internal_relocs
== NULL
)
528 amt
= sec
->reloc_count
;
529 amt
*= sizeof (struct internal_reloc
);
530 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
531 if (free_internal
== NULL
)
533 internal_relocs
= free_internal
;
536 /* Swap in the relocs. */
537 erel
= external_relocs
;
538 erel_end
= erel
+ relsz
* sec
->reloc_count
;
539 irel
= internal_relocs
;
540 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
541 bfd_coff_swap_reloc_in (abfd
, (void *) erel
, (void *) irel
);
543 if (free_external
!= NULL
)
545 free (free_external
);
546 free_external
= NULL
;
549 if (cache
&& free_internal
!= NULL
)
551 if (coff_section_data (abfd
, sec
) == NULL
)
553 amt
= sizeof (struct coff_section_tdata
);
554 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
555 if (sec
->used_by_bfd
== NULL
)
557 coff_section_data (abfd
, sec
)->contents
= NULL
;
559 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
562 return internal_relocs
;
565 if (free_external
!= NULL
)
566 free (free_external
);
567 if (free_internal
!= NULL
)
568 free (free_internal
);
572 /* Set lineno_count for the output sections of a COFF file. */
575 coff_count_linenumbers (bfd
*abfd
)
577 unsigned int limit
= bfd_get_symcount (abfd
);
585 /* This may be from the backend linker, in which case the
586 lineno_count in the sections is correct. */
587 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
588 total
+= s
->lineno_count
;
592 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
593 BFD_ASSERT (s
->lineno_count
== 0);
595 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
597 asymbol
*q_maybe
= *p
;
599 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
601 coff_symbol_type
*q
= coffsymbol (q_maybe
);
603 /* The AIX 4.1 compiler can sometimes generate line numbers
604 attached to debugging symbols. We try to simply ignore
606 if (q
->lineno
!= NULL
607 && q
->symbol
.section
->owner
!= NULL
)
609 /* This symbol has line numbers. Increment the owning
610 section's linenumber count. */
611 alent
*l
= q
->lineno
;
615 asection
* sec
= q
->symbol
.section
->output_section
;
617 /* Do not try to update fields in read-only sections. */
618 if (! bfd_is_const_section (sec
))
619 sec
->lineno_count
++;
624 while (l
->line_number
!= 0);
632 /* Takes a bfd and a symbol, returns a pointer to the coff specific
633 area of the symbol if there is one. */
636 coff_symbol_from (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
639 if (!bfd_family_coff (bfd_asymbol_bfd (symbol
)))
640 return (coff_symbol_type
*) NULL
;
642 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
643 return (coff_symbol_type
*) NULL
;
645 return (coff_symbol_type
*) symbol
;
649 fixup_symbol_value (bfd
*abfd
,
650 coff_symbol_type
*coff_symbol_ptr
,
651 struct internal_syment
*syment
)
653 /* Normalize the symbol flags. */
654 if (coff_symbol_ptr
->symbol
.section
655 && bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
657 /* A common symbol is undefined with a value. */
658 syment
->n_scnum
= N_UNDEF
;
659 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
661 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
662 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
664 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
666 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
668 syment
->n_scnum
= N_UNDEF
;
671 /* FIXME: Do we need to handle the absolute section here? */
674 if (coff_symbol_ptr
->symbol
.section
)
677 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
679 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
680 + coff_symbol_ptr
->symbol
.section
->output_offset
);
683 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
684 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
685 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
691 /* This can happen, but I don't know why yet (steve@cygnus.com) */
692 syment
->n_scnum
= N_ABS
;
693 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
698 /* Run through all the symbols in the symbol table and work out what
699 their indexes into the symbol table will be when output.
701 Coff requires that each C_FILE symbol points to the next one in the
702 chain, and that the last one points to the first external symbol. We
706 coff_renumber_symbols (bfd
*bfd_ptr
, int *first_undef
)
708 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
709 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
710 unsigned int native_index
= 0;
711 struct internal_syment
*last_file
= NULL
;
712 unsigned int symbol_index
;
714 /* COFF demands that undefined symbols come after all other symbols.
715 Since we don't need to impose this extra knowledge on all our
716 client programs, deal with that here. Sort the symbol table;
717 just move the undefined symbols to the end, leaving the rest
718 alone. The O'Reilly book says that defined global symbols come
719 at the end before the undefined symbols, so we do that here as
721 /* @@ Do we have some condition we could test for, so we don't always
722 have to do this? I don't think relocatability is quite right, but
723 I'm not certain. [raeburn:19920508.1711EST] */
729 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
730 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
733 bfd_ptr
->outsymbols
= newsyms
;
734 for (i
= 0; i
< symbol_count
; i
++)
735 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
736 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
737 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
738 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
739 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
741 *newsyms
++ = symbol_ptr_ptr
[i
];
743 for (i
= 0; i
< symbol_count
; i
++)
744 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
745 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
746 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
747 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
748 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
750 *newsyms
++ = symbol_ptr_ptr
[i
];
752 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
754 for (i
= 0; i
< symbol_count
; i
++)
755 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
756 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
757 *newsyms
++ = symbol_ptr_ptr
[i
];
758 *newsyms
= (asymbol
*) NULL
;
759 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
762 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
764 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
765 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
766 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
768 combined_entry_type
*s
= coff_symbol_ptr
->native
;
771 if (s
->u
.syment
.n_sclass
== C_FILE
)
773 if (last_file
!= NULL
)
774 last_file
->n_value
= native_index
;
775 last_file
= &(s
->u
.syment
);
778 /* Modify the symbol values according to their section and
780 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
782 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
783 s
[i
].offset
= native_index
++;
789 obj_conv_table_size (bfd_ptr
) = native_index
;
794 /* Run thorough the symbol table again, and fix it so that all
795 pointers to entries are changed to the entries' index in the output
799 coff_mangle_symbols (bfd
*bfd_ptr
)
801 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
802 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
803 unsigned int symbol_index
;
805 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
807 coff_symbol_type
*coff_symbol_ptr
=
808 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
810 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
813 combined_entry_type
*s
= coff_symbol_ptr
->native
;
817 /* FIXME: We should use a union here. */
818 s
->u
.syment
.n_value
=
819 (bfd_hostptr_t
) ((combined_entry_type
*)
820 ((bfd_hostptr_t
) s
->u
.syment
.n_value
))->offset
;
825 /* The value is the offset into the line number entries
826 for the symbol's section. On output, the symbol's
827 section should be N_DEBUG. */
828 s
->u
.syment
.n_value
=
829 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
830 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
831 coff_symbol_ptr
->symbol
.section
=
832 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
833 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
835 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
837 combined_entry_type
*a
= s
+ i
+ 1;
840 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
841 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
846 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
847 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
852 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
853 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
862 coff_fix_symbol_name (bfd
*abfd
,
864 combined_entry_type
*native
,
865 bfd_size_type
*string_size_p
,
866 asection
**debug_string_section_p
,
867 bfd_size_type
*debug_string_size_p
)
869 unsigned int name_length
;
870 union internal_auxent
*auxent
;
871 char *name
= (char *) (symbol
->name
);
875 /* COFF symbols always have names, so we'll make one up. */
876 symbol
->name
= "strange";
877 name
= (char *) symbol
->name
;
879 name_length
= strlen (name
);
881 if (native
->u
.syment
.n_sclass
== C_FILE
882 && native
->u
.syment
.n_numaux
> 0)
884 unsigned int filnmlen
;
886 if (bfd_coff_force_symnames_in_strings (abfd
))
888 native
->u
.syment
._n
._n_n
._n_offset
=
889 (*string_size_p
+ STRING_SIZE_SIZE
);
890 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
891 *string_size_p
+= 6; /* strlen(".file") + 1 */
894 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
896 auxent
= &(native
+ 1)->u
.auxent
;
898 filnmlen
= bfd_coff_filnmlen (abfd
);
900 if (bfd_coff_long_filenames (abfd
))
902 if (name_length
<= filnmlen
)
903 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
906 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
907 auxent
->x_file
.x_n
.x_zeroes
= 0;
908 *string_size_p
+= name_length
+ 1;
913 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
914 if (name_length
> filnmlen
)
915 name
[filnmlen
] = '\0';
920 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
921 /* This name will fit into the symbol neatly. */
922 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
924 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
926 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
928 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
929 *string_size_p
+= name_length
+ 1;
935 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
937 /* This name should be written into the .debug section. For
938 some reason each name is preceded by a two byte length
939 and also followed by a null byte. FIXME: We assume that
940 the .debug section has already been created, and that it
942 if (*debug_string_section_p
== (asection
*) NULL
)
943 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
944 filepos
= bfd_tell (abfd
);
946 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
948 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
950 if (!bfd_set_section_contents (abfd
,
951 *debug_string_section_p
,
953 (file_ptr
) *debug_string_size_p
,
954 (bfd_size_type
) prefix_len
)
955 || !bfd_set_section_contents (abfd
,
956 *debug_string_section_p
,
957 (void *) symbol
->name
,
958 (file_ptr
) (*debug_string_size_p
960 (bfd_size_type
) name_length
+ 1))
962 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
964 native
->u
.syment
._n
._n_n
._n_offset
=
965 *debug_string_size_p
+ prefix_len
;
966 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
967 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
972 /* We need to keep track of the symbol index so that when we write out
973 the relocs we can get the index for a symbol. This method is a
976 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
978 /* Write a symbol out to a COFF file. */
981 coff_write_symbol (bfd
*abfd
,
983 combined_entry_type
*native
,
985 bfd_size_type
*string_size_p
,
986 asection
**debug_string_section_p
,
987 bfd_size_type
*debug_string_size_p
)
989 unsigned int numaux
= native
->u
.syment
.n_numaux
;
990 int type
= native
->u
.syment
.n_type
;
991 int n_sclass
= (int) native
->u
.syment
.n_sclass
;
992 asection
*output_section
= symbol
->section
->output_section
993 ? symbol
->section
->output_section
996 bfd_size_type symesz
;
998 if (native
->u
.syment
.n_sclass
== C_FILE
)
999 symbol
->flags
|= BSF_DEBUGGING
;
1001 if (symbol
->flags
& BSF_DEBUGGING
1002 && bfd_is_abs_section (symbol
->section
))
1003 native
->u
.syment
.n_scnum
= N_DEBUG
;
1005 else if (bfd_is_abs_section (symbol
->section
))
1006 native
->u
.syment
.n_scnum
= N_ABS
;
1008 else if (bfd_is_und_section (symbol
->section
))
1009 native
->u
.syment
.n_scnum
= N_UNDEF
;
1012 native
->u
.syment
.n_scnum
=
1013 output_section
->target_index
;
1015 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
1016 debug_string_section_p
, debug_string_size_p
);
1018 symesz
= bfd_coff_symesz (abfd
);
1019 buf
= bfd_alloc (abfd
, symesz
);
1022 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
1023 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
1025 bfd_release (abfd
, buf
);
1027 if (native
->u
.syment
.n_numaux
> 0)
1029 bfd_size_type auxesz
;
1032 auxesz
= bfd_coff_auxesz (abfd
);
1033 buf
= bfd_alloc (abfd
, auxesz
);
1036 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
1038 bfd_coff_swap_aux_out (abfd
,
1039 &((native
+ j
+ 1)->u
.auxent
),
1040 type
, n_sclass
, (int) j
,
1041 native
->u
.syment
.n_numaux
,
1043 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
1046 bfd_release (abfd
, buf
);
1049 /* Store the index for use when we write out the relocs. */
1050 set_index (symbol
, *written
);
1052 *written
+= numaux
+ 1;
1056 /* Write out a symbol to a COFF file that does not come from a COFF
1057 file originally. This symbol may have been created by the linker,
1058 or we may be linking a non COFF file to a COFF file. */
1061 coff_write_alien_symbol (bfd
*abfd
,
1063 struct internal_syment
*isym
,
1065 bfd_size_type
*string_size_p
,
1066 asection
**debug_string_section_p
,
1067 bfd_size_type
*debug_string_size_p
)
1069 combined_entry_type
*native
;
1070 combined_entry_type dummy
[2];
1071 asection
*output_section
= symbol
->section
->output_section
1072 ? symbol
->section
->output_section
1074 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1077 if ((!link_info
|| link_info
->strip_discarded
)
1078 && !bfd_is_abs_section (symbol
->section
)
1079 && symbol
->section
->output_section
== bfd_abs_section_ptr
)
1083 memset (isym
, 0, sizeof(*isym
));
1087 native
->u
.syment
.n_type
= T_NULL
;
1088 native
->u
.syment
.n_flags
= 0;
1089 native
->u
.syment
.n_numaux
= 0;
1090 if (bfd_is_und_section (symbol
->section
))
1092 native
->u
.syment
.n_scnum
= N_UNDEF
;
1093 native
->u
.syment
.n_value
= symbol
->value
;
1095 else if (bfd_is_com_section (symbol
->section
))
1097 native
->u
.syment
.n_scnum
= N_UNDEF
;
1098 native
->u
.syment
.n_value
= symbol
->value
;
1100 else if (symbol
->flags
& BSF_FILE
)
1102 native
->u
.syment
.n_scnum
= N_DEBUG
;
1103 native
->u
.syment
.n_numaux
= 1;
1105 else if (symbol
->flags
& BSF_DEBUGGING
)
1107 /* There isn't much point to writing out a debugging symbol
1108 unless we are prepared to convert it into COFF debugging
1109 format. So, we just ignore them. We must clobber the symbol
1110 name to keep it from being put in the string table. */
1113 memset (isym
, 0, sizeof(*isym
));
1118 native
->u
.syment
.n_scnum
= output_section
->target_index
;
1119 native
->u
.syment
.n_value
= (symbol
->value
1120 + symbol
->section
->output_offset
);
1121 if (! obj_pe (abfd
))
1122 native
->u
.syment
.n_value
+= output_section
->vma
;
1124 /* Copy the any flags from the file header into the symbol.
1127 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1128 if (c
!= (coff_symbol_type
*) NULL
)
1129 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1133 native
->u
.syment
.n_type
= 0;
1134 if (symbol
->flags
& BSF_FILE
)
1135 native
->u
.syment
.n_sclass
= C_FILE
;
1136 else if (symbol
->flags
& BSF_LOCAL
)
1137 native
->u
.syment
.n_sclass
= C_STAT
;
1138 else if (symbol
->flags
& BSF_WEAK
)
1139 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1141 native
->u
.syment
.n_sclass
= C_EXT
;
1143 ret
= coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1144 debug_string_section_p
, debug_string_size_p
);
1146 *isym
= native
->u
.syment
;
1150 /* Write a native symbol to a COFF file. */
1153 coff_write_native_symbol (bfd
*abfd
,
1154 coff_symbol_type
*symbol
,
1156 bfd_size_type
*string_size_p
,
1157 asection
**debug_string_section_p
,
1158 bfd_size_type
*debug_string_size_p
)
1160 combined_entry_type
*native
= symbol
->native
;
1161 alent
*lineno
= symbol
->lineno
;
1162 struct bfd_link_info
*link_info
= coff_data (abfd
)->link_info
;
1164 if ((!link_info
|| link_info
->strip_discarded
)
1165 && !bfd_is_abs_section (symbol
->symbol
.section
)
1166 && symbol
->symbol
.section
->output_section
== bfd_abs_section_ptr
)
1168 symbol
->symbol
.name
= "";
1172 /* If this symbol has an associated line number, we must store the
1173 symbol index in the line number field. We also tag the auxent to
1174 point to the right place in the lineno table. */
1175 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1177 unsigned int count
= 0;
1179 lineno
[count
].u
.offset
= *written
;
1180 if (native
->u
.syment
.n_numaux
)
1182 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1184 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1185 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1188 /* Count and relocate all other linenumbers. */
1190 while (lineno
[count
].line_number
!= 0)
1192 lineno
[count
].u
.offset
+=
1193 (symbol
->symbol
.section
->output_section
->vma
1194 + symbol
->symbol
.section
->output_offset
);
1197 symbol
->done_lineno
= TRUE
;
1199 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1200 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1201 count
* bfd_coff_linesz (abfd
);
1204 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1205 string_size_p
, debug_string_section_p
,
1206 debug_string_size_p
);
1210 null_error_handler (const char * fmt ATTRIBUTE_UNUSED
, ...)
1214 /* Write out the COFF symbols. */
1217 coff_write_symbols (bfd
*abfd
)
1219 bfd_size_type string_size
;
1220 asection
*debug_string_section
;
1221 bfd_size_type debug_string_size
;
1223 unsigned int limit
= bfd_get_symcount (abfd
);
1224 bfd_vma written
= 0;
1228 debug_string_section
= NULL
;
1229 debug_string_size
= 0;
1231 /* If this target supports long section names, they must be put into
1232 the string table. This is supported by PE. This code must
1233 handle section names just as they are handled in
1234 coff_write_object_contents. */
1235 if (bfd_coff_long_section_names (abfd
))
1239 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1243 len
= strlen (o
->name
);
1245 string_size
+= len
+ 1;
1249 /* Seek to the right place. */
1250 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1253 /* Output all the symbols we have. */
1255 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1257 asymbol
*symbol
= *p
;
1258 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1260 if (c_symbol
== (coff_symbol_type
*) NULL
1261 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1263 if (!coff_write_alien_symbol (abfd
, symbol
, NULL
, &written
,
1264 &string_size
, &debug_string_section
,
1265 &debug_string_size
))
1270 if (coff_backend_info (abfd
)->_bfd_coff_classify_symbol
!= NULL
)
1272 bfd_error_handler_type current_error_handler
;
1273 enum coff_symbol_classification sym_class
;
1274 unsigned char *n_sclass
;
1276 /* Suppress error reporting by bfd_coff_classify_symbol.
1277 Error messages can be generated when we are processing a local
1278 symbol which has no associated section and we do not have to
1279 worry about this, all we need to know is that it is local. */
1280 current_error_handler
= bfd_set_error_handler (null_error_handler
);
1281 sym_class
= bfd_coff_classify_symbol (abfd
,
1282 &c_symbol
->native
->u
.syment
);
1283 (void) bfd_set_error_handler (current_error_handler
);
1285 n_sclass
= &c_symbol
->native
->u
.syment
.n_sclass
;
1287 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1288 we cannot retain the existing sclass from the original symbol.
1289 Weak symbols only have one valid sclass, so just set it always.
1290 If it is not local class and should be, set it C_STAT.
1291 If it is global and not classified as global, or if it is
1292 weak (which is also classified as global), set it C_EXT. */
1294 if (symbol
->flags
& BSF_WEAK
)
1295 *n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1296 else if (symbol
->flags
& BSF_LOCAL
&& sym_class
!= COFF_SYMBOL_LOCAL
)
1298 else if (symbol
->flags
& BSF_GLOBAL
1299 && (sym_class
!= COFF_SYMBOL_GLOBAL
1301 || *n_sclass
== C_NT_WEAK
1303 || *n_sclass
== C_WEAKEXT
))
1304 c_symbol
->native
->u
.syment
.n_sclass
= C_EXT
;
1307 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1308 &string_size
, &debug_string_section
,
1309 &debug_string_size
))
1314 obj_raw_syment_count (abfd
) = written
;
1316 /* Now write out strings. */
1317 if (string_size
!= 0)
1319 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1320 bfd_byte buffer
[STRING_SIZE_SIZE
];
1322 #if STRING_SIZE_SIZE == 4
1323 H_PUT_32 (abfd
, size
, buffer
);
1325 #error Change H_PUT_32
1327 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1331 /* Handle long section names. This code must handle section
1332 names just as they are handled in coff_write_object_contents. */
1333 if (bfd_coff_long_section_names (abfd
))
1337 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1341 len
= strlen (o
->name
);
1344 if (bfd_bwrite (o
->name
, (bfd_size_type
) (len
+ 1), abfd
)
1351 for (p
= abfd
->outsymbols
, i
= 0;
1356 size_t name_length
= strlen (q
->name
);
1357 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1360 /* Figure out whether the symbol name should go in the string
1361 table. Symbol names that are short enough are stored
1362 directly in the syment structure. File names permit a
1363 different, longer, length in the syment structure. On
1364 XCOFF, some symbol names are stored in the .debug section
1365 rather than in the string table. */
1367 if (c_symbol
== NULL
1368 || c_symbol
->native
== NULL
)
1369 /* This is not a COFF symbol, so it certainly is not a
1370 file name, nor does it go in the .debug section. */
1371 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1373 else if (bfd_coff_symname_in_debug (abfd
,
1374 &c_symbol
->native
->u
.syment
))
1375 /* This symbol name is in the XCOFF .debug section.
1376 Don't write it into the string table. */
1377 maxlen
= name_length
;
1379 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1380 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1382 if (bfd_coff_force_symnames_in_strings (abfd
))
1384 if (bfd_bwrite (".file", (bfd_size_type
) 6, abfd
) != 6)
1387 maxlen
= bfd_coff_filnmlen (abfd
);
1390 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1392 if (name_length
> maxlen
)
1394 if (bfd_bwrite ((void *) (q
->name
), (bfd_size_type
) name_length
+ 1,
1395 abfd
) != name_length
+ 1)
1402 /* We would normally not write anything here, but we'll write
1403 out 4 so that any stupid coff reader which tries to read the
1404 string table even when there isn't one won't croak. */
1405 unsigned int size
= STRING_SIZE_SIZE
;
1406 bfd_byte buffer
[STRING_SIZE_SIZE
];
1408 #if STRING_SIZE_SIZE == 4
1409 H_PUT_32 (abfd
, size
, buffer
);
1411 #error Change H_PUT_32
1413 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1414 != STRING_SIZE_SIZE
)
1418 /* Make sure the .debug section was created to be the correct size.
1419 We should create it ourselves on the fly, but we don't because
1420 BFD won't let us write to any section until we know how large all
1421 the sections are. We could still do it by making another pass
1422 over the symbols. FIXME. */
1423 BFD_ASSERT (debug_string_size
== 0
1424 || (debug_string_section
!= (asection
*) NULL
1425 && (BFD_ALIGN (debug_string_size
,
1426 1 << debug_string_section
->alignment_power
)
1427 == debug_string_section
->size
)));
1433 coff_write_linenumbers (bfd
*abfd
)
1436 bfd_size_type linesz
;
1439 linesz
= bfd_coff_linesz (abfd
);
1440 buff
= bfd_alloc (abfd
, linesz
);
1443 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1445 if (s
->lineno_count
)
1447 asymbol
**q
= abfd
->outsymbols
;
1448 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1450 /* Find all the linenumbers in this section. */
1454 if (p
->section
->output_section
== s
)
1457 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1458 (bfd_asymbol_bfd (p
), p
));
1461 /* Found a linenumber entry, output. */
1462 struct internal_lineno out
;
1463 memset ((void *) & out
, 0, sizeof (out
));
1465 out
.l_addr
.l_symndx
= l
->u
.offset
;
1466 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1467 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1471 while (l
->line_number
)
1473 out
.l_lnno
= l
->line_number
;
1474 out
.l_addr
.l_symndx
= l
->u
.offset
;
1475 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1476 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1487 bfd_release (abfd
, buff
);
1492 coff_get_lineno (bfd
*ignore_abfd ATTRIBUTE_UNUSED
, asymbol
*symbol
)
1494 return coffsymbol (symbol
)->lineno
;
1497 /* This function transforms the offsets into the symbol table into
1498 pointers to syments. */
1501 coff_pointerize_aux (bfd
*abfd
,
1502 combined_entry_type
*table_base
,
1503 combined_entry_type
*symbol
,
1504 unsigned int indaux
,
1505 combined_entry_type
*auxent
)
1507 unsigned int type
= symbol
->u
.syment
.n_type
;
1508 unsigned int n_sclass
= symbol
->u
.syment
.n_sclass
;
1510 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1512 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1513 (abfd
, table_base
, symbol
, indaux
, auxent
))
1517 /* Don't bother if this is a file or a section. */
1518 if (n_sclass
== C_STAT
&& type
== T_NULL
)
1520 if (n_sclass
== C_FILE
)
1523 /* Otherwise patch up. */
1524 #define N_TMASK coff_data (abfd)->local_n_tmask
1525 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1527 if ((ISFCN (type
) || ISTAG (n_sclass
) || n_sclass
== C_BLOCK
1528 || n_sclass
== C_FCN
)
1529 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1531 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1532 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1533 auxent
->fix_end
= 1;
1535 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1536 generate one, so we must be careful to ignore it. */
1537 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1539 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1540 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1541 auxent
->fix_tag
= 1;
1545 /* Allocate space for the ".debug" section, and read it.
1546 We did not read the debug section until now, because
1547 we didn't want to go to the trouble until someone needed it. */
1550 build_debug_section (bfd
*abfd
)
1552 char *debug_section
;
1554 bfd_size_type sec_size
;
1556 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1560 bfd_set_error (bfd_error_no_debug_section
);
1564 sec_size
= sect
->size
;
1565 debug_section
= (char *) bfd_alloc (abfd
, sec_size
);
1566 if (debug_section
== NULL
)
1569 /* Seek to the beginning of the `.debug' section and read it.
1570 Save the current position first; it is needed by our caller.
1571 Then read debug section and reset the file pointer. */
1573 position
= bfd_tell (abfd
);
1574 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1575 || bfd_bread (debug_section
, sec_size
, abfd
) != sec_size
1576 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1578 return debug_section
;
1581 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1582 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1583 be \0-terminated. */
1586 copy_name (bfd
*abfd
, char *name
, size_t maxlen
)
1591 for (len
= 0; len
< maxlen
; ++len
)
1592 if (name
[len
] == '\0')
1595 if ((newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1598 strncpy (newname
, name
, len
);
1599 newname
[len
] = '\0';
1603 /* Read in the external symbols. */
1606 _bfd_coff_get_external_symbols (bfd
*abfd
)
1608 bfd_size_type symesz
;
1612 if (obj_coff_external_syms (abfd
) != NULL
)
1615 symesz
= bfd_coff_symesz (abfd
);
1617 size
= obj_raw_syment_count (abfd
) * symesz
;
1621 syms
= bfd_malloc (size
);
1625 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1626 || bfd_bread (syms
, size
, abfd
) != size
)
1633 obj_coff_external_syms (abfd
) = syms
;
1638 /* Read in the external strings. The strings are not loaded until
1639 they are needed. This is because we have no simple way of
1640 detecting a missing string table in an archive. */
1643 _bfd_coff_read_string_table (bfd
*abfd
)
1645 char extstrsize
[STRING_SIZE_SIZE
];
1646 bfd_size_type strsize
;
1650 if (obj_coff_strings (abfd
) != NULL
)
1651 return obj_coff_strings (abfd
);
1653 if (obj_sym_filepos (abfd
) == 0)
1655 bfd_set_error (bfd_error_no_symbols
);
1659 pos
= obj_sym_filepos (abfd
);
1660 pos
+= obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
);
1661 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1664 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1665 != sizeof extstrsize
)
1667 if (bfd_get_error () != bfd_error_file_truncated
)
1670 /* There is no string table. */
1671 strsize
= STRING_SIZE_SIZE
;
1675 #if STRING_SIZE_SIZE == 4
1676 strsize
= H_GET_32 (abfd
, extstrsize
);
1678 #error Change H_GET_32
1682 if (strsize
< STRING_SIZE_SIZE
)
1684 (*_bfd_error_handler
)
1685 (_("%B: bad string table size %lu"), abfd
, (unsigned long) strsize
);
1686 bfd_set_error (bfd_error_bad_value
);
1690 strings
= (char *) bfd_malloc (strsize
);
1691 if (strings
== NULL
)
1694 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1695 != strsize
- STRING_SIZE_SIZE
)
1701 obj_coff_strings (abfd
) = strings
;
1706 /* Free up the external symbols and strings read from a COFF file. */
1709 _bfd_coff_free_symbols (bfd
*abfd
)
1711 if (obj_coff_external_syms (abfd
) != NULL
1712 && ! obj_coff_keep_syms (abfd
))
1714 free (obj_coff_external_syms (abfd
));
1715 obj_coff_external_syms (abfd
) = NULL
;
1717 if (obj_coff_strings (abfd
) != NULL
1718 && ! obj_coff_keep_strings (abfd
))
1720 free (obj_coff_strings (abfd
));
1721 obj_coff_strings (abfd
) = NULL
;
1726 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1727 knit the symbol names into a normalized form. By normalized here I
1728 mean that all symbols have an n_offset pointer that points to a null-
1729 terminated string. */
1731 combined_entry_type
*
1732 coff_get_normalized_symtab (bfd
*abfd
)
1734 combined_entry_type
*internal
;
1735 combined_entry_type
*internal_ptr
;
1736 combined_entry_type
*symbol_ptr
;
1737 combined_entry_type
*internal_end
;
1741 const char *string_table
= NULL
;
1742 char *debug_section
= NULL
;
1745 if (obj_raw_syments (abfd
) != NULL
)
1746 return obj_raw_syments (abfd
);
1748 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1749 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1750 if (internal
== NULL
&& size
!= 0)
1752 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1754 if (! _bfd_coff_get_external_symbols (abfd
))
1757 raw_src
= (char *) obj_coff_external_syms (abfd
);
1759 /* Mark the end of the symbols. */
1760 symesz
= bfd_coff_symesz (abfd
);
1761 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1763 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1764 probably possible. If one shows up, it will probably kill us. */
1766 /* Swap all the raw entries. */
1767 for (internal_ptr
= internal
;
1769 raw_src
+= symesz
, internal_ptr
++)
1773 bfd_coff_swap_sym_in (abfd
, (void *) raw_src
,
1774 (void *) & internal_ptr
->u
.syment
);
1775 symbol_ptr
= internal_ptr
;
1778 i
< symbol_ptr
->u
.syment
.n_numaux
;
1783 bfd_coff_swap_aux_in (abfd
, (void *) raw_src
,
1784 symbol_ptr
->u
.syment
.n_type
,
1785 symbol_ptr
->u
.syment
.n_sclass
,
1786 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1787 &(internal_ptr
->u
.auxent
));
1788 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1793 /* Free the raw symbols, but not the strings (if we have them). */
1794 obj_coff_keep_strings (abfd
) = TRUE
;
1795 if (! _bfd_coff_free_symbols (abfd
))
1798 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1801 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1802 && internal_ptr
->u
.syment
.n_numaux
> 0)
1804 /* Make a file symbol point to the name in the auxent, since
1805 the text ".file" is redundant. */
1806 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1808 /* The filename is a long one, point into the string table. */
1809 if (string_table
== NULL
)
1811 string_table
= _bfd_coff_read_string_table (abfd
);
1812 if (string_table
== NULL
)
1816 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1819 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1823 /* Ordinary short filename, put into memory anyway. The
1824 Microsoft PE tools sometimes store a filename in
1825 multiple AUX entries. */
1826 if (internal_ptr
->u
.syment
.n_numaux
> 1
1827 && coff_data (abfd
)->pe
)
1828 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1831 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1832 internal_ptr
->u
.syment
.n_numaux
* symesz
));
1834 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1837 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1838 (size_t) bfd_coff_filnmlen (abfd
)));
1843 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1845 /* This is a "short" name. Make it long. */
1849 /* Find the length of this string without walking into memory
1851 for (i
= 0; i
< 8; ++i
)
1852 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1855 newstring
= (char *) bfd_zalloc (abfd
, (bfd_size_type
) (i
+ 1));
1856 if (newstring
== NULL
)
1858 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1859 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) newstring
;
1860 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1862 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1863 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) "";
1864 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1866 /* Long name already. Point symbol at the string in the
1868 if (string_table
== NULL
)
1870 string_table
= _bfd_coff_read_string_table (abfd
);
1871 if (string_table
== NULL
)
1874 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1877 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1881 /* Long name in debug section. Very similar. */
1882 if (debug_section
== NULL
)
1883 debug_section
= build_debug_section (abfd
);
1884 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
)
1885 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1888 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1891 obj_raw_syments (abfd
) = internal
;
1892 BFD_ASSERT (obj_raw_syment_count (abfd
)
1893 == (unsigned int) (internal_ptr
- internal
));
1899 coff_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
1901 if (bfd_get_format (abfd
) != bfd_object
)
1903 bfd_set_error (bfd_error_invalid_operation
);
1906 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1910 coff_make_empty_symbol (bfd
*abfd
)
1912 bfd_size_type amt
= sizeof (coff_symbol_type
);
1913 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
1915 if (new_symbol
== NULL
)
1917 new_symbol
->symbol
.section
= 0;
1918 new_symbol
->native
= 0;
1919 new_symbol
->lineno
= NULL
;
1920 new_symbol
->done_lineno
= FALSE
;
1921 new_symbol
->symbol
.the_bfd
= abfd
;
1923 return & new_symbol
->symbol
;
1926 /* Make a debugging symbol. */
1929 coff_bfd_make_debug_symbol (bfd
*abfd
,
1930 void * ptr ATTRIBUTE_UNUSED
,
1931 unsigned long sz ATTRIBUTE_UNUSED
)
1933 bfd_size_type amt
= sizeof (coff_symbol_type
);
1934 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
1936 if (new_symbol
== NULL
)
1938 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1939 (but shouldn't be a constant). */
1940 amt
= sizeof (combined_entry_type
) * 10;
1941 new_symbol
->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
1942 if (!new_symbol
->native
)
1944 new_symbol
->symbol
.section
= bfd_abs_section_ptr
;
1945 new_symbol
->symbol
.flags
= BSF_DEBUGGING
;
1946 new_symbol
->lineno
= NULL
;
1947 new_symbol
->done_lineno
= FALSE
;
1948 new_symbol
->symbol
.the_bfd
= abfd
;
1950 return & new_symbol
->symbol
;
1954 coff_get_symbol_info (bfd
*abfd
, asymbol
*symbol
, symbol_info
*ret
)
1956 bfd_symbol_info (symbol
, ret
);
1958 if (coffsymbol (symbol
)->native
!= NULL
1959 && coffsymbol (symbol
)->native
->fix_value
)
1960 ret
->value
= coffsymbol (symbol
)->native
->u
.syment
.n_value
-
1961 (bfd_hostptr_t
) obj_raw_syments (abfd
);
1964 /* Return the COFF syment for a symbol. */
1967 bfd_coff_get_syment (bfd
*abfd
,
1969 struct internal_syment
*psyment
)
1971 coff_symbol_type
*csym
;
1973 csym
= coff_symbol_from (abfd
, symbol
);
1974 if (csym
== NULL
|| csym
->native
== NULL
)
1976 bfd_set_error (bfd_error_invalid_operation
);
1980 *psyment
= csym
->native
->u
.syment
;
1982 if (csym
->native
->fix_value
)
1983 psyment
->n_value
= psyment
->n_value
-
1984 (bfd_hostptr_t
) obj_raw_syments (abfd
);
1986 /* FIXME: We should handle fix_line here. */
1991 /* Return the COFF auxent for a symbol. */
1994 bfd_coff_get_auxent (bfd
*abfd
,
1997 union internal_auxent
*pauxent
)
1999 coff_symbol_type
*csym
;
2000 combined_entry_type
*ent
;
2002 csym
= coff_symbol_from (abfd
, symbol
);
2005 || csym
->native
== NULL
2006 || indx
>= csym
->native
->u
.syment
.n_numaux
)
2008 bfd_set_error (bfd_error_invalid_operation
);
2012 ent
= csym
->native
+ indx
+ 1;
2014 *pauxent
= ent
->u
.auxent
;
2017 pauxent
->x_sym
.x_tagndx
.l
=
2018 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
2019 - obj_raw_syments (abfd
));
2022 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2023 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2024 - obj_raw_syments (abfd
));
2026 if (ent
->fix_scnlen
)
2027 pauxent
->x_csect
.x_scnlen
.l
=
2028 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
2029 - obj_raw_syments (abfd
));
2034 /* Print out information about COFF symbol. */
2037 coff_print_symbol (bfd
*abfd
,
2040 bfd_print_symbol_type how
)
2042 FILE * file
= (FILE *) filep
;
2046 case bfd_print_symbol_name
:
2047 fprintf (file
, "%s", symbol
->name
);
2050 case bfd_print_symbol_more
:
2051 fprintf (file
, "coff %s %s",
2052 coffsymbol (symbol
)->native
? "n" : "g",
2053 coffsymbol (symbol
)->lineno
? "l" : " ");
2056 case bfd_print_symbol_all
:
2057 if (coffsymbol (symbol
)->native
)
2061 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2062 combined_entry_type
*root
= obj_raw_syments (abfd
);
2063 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2065 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2067 if (! combined
->fix_value
)
2068 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2070 val
= combined
->u
.syment
.n_value
- (bfd_hostptr_t
) root
;
2072 fprintf (file
, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
2073 combined
->u
.syment
.n_scnum
,
2074 combined
->u
.syment
.n_flags
,
2075 combined
->u
.syment
.n_type
,
2076 combined
->u
.syment
.n_sclass
,
2077 combined
->u
.syment
.n_numaux
);
2078 bfd_fprintf_vma (abfd
, file
, val
);
2079 fprintf (file
, " %s", symbol
->name
);
2081 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2083 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2087 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2089 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2091 fprintf (file
, "\n");
2093 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2096 switch (combined
->u
.syment
.n_sclass
)
2099 fprintf (file
, "File ");
2103 if (combined
->u
.syment
.n_type
== T_NULL
)
2104 /* Probably a section symbol ? */
2106 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2107 (unsigned long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2108 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2109 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2110 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2111 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2112 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2113 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2114 auxp
->u
.auxent
.x_scn
.x_checksum
,
2115 auxp
->u
.auxent
.x_scn
.x_associated
,
2116 auxp
->u
.auxent
.x_scn
.x_comdat
);
2119 /* Otherwise fall through. */
2122 if (ISFCN (combined
->u
.syment
.n_type
))
2127 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2130 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2131 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2133 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2135 (unsigned long) auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2139 /* Otherwise fall through. */
2141 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2142 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2143 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2146 fprintf (file
, " endndx %ld",
2148 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2156 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2158 while (l
->line_number
)
2160 fprintf (file
, "\n%4d : ", l
->line_number
);
2161 bfd_fprintf_vma (abfd
, file
, l
->u
.offset
+ symbol
->section
->vma
);
2168 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
2169 fprintf (file
, " %-5s %s %s %s",
2170 symbol
->section
->name
,
2171 coffsymbol (symbol
)->native
? "n" : "g",
2172 coffsymbol (symbol
)->lineno
? "l" : " ",
2178 /* Return whether a symbol name implies a local symbol. In COFF,
2179 local symbols generally start with ``.L''. Most targets use this
2180 function for the is_local_label_name entry point, but some may
2184 _bfd_coff_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
2187 return name
[0] == '.' && name
[1] == 'L';
2190 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2191 section, calculate and return the name of the source file and the line
2192 nearest to the wanted location. */
2195 coff_find_nearest_line_with_names (bfd
*abfd
,
2196 const struct dwarf_debug_section
*debug_sections
,
2200 const char **filename_ptr
,
2201 const char **functionname_ptr
,
2202 unsigned int *line_ptr
)
2206 unsigned int line_base
;
2207 coff_data_type
*cof
= coff_data (abfd
);
2208 /* Run through the raw syments if available. */
2209 combined_entry_type
*p
;
2210 combined_entry_type
*pend
;
2212 struct coff_section_tdata
*sec_data
;
2215 /* Before looking through the symbol table, try to use a .stab
2216 section to find the information. */
2217 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2218 &found
, filename_ptr
,
2219 functionname_ptr
, line_ptr
,
2220 &coff_data(abfd
)->line_info
))
2226 /* Also try examining DWARF2 debugging information. */
2227 if (_bfd_dwarf2_find_nearest_line (abfd
, debug_sections
,
2228 section
, symbols
, offset
,
2229 filename_ptr
, functionname_ptr
,
2231 &coff_data(abfd
)->dwarf2_find_line_info
))
2235 *functionname_ptr
= 0;
2238 /* Don't try and find line numbers in a non coff file. */
2239 if (!bfd_family_coff (abfd
))
2245 /* Find the first C_FILE symbol. */
2246 p
= cof
->raw_syments
;
2250 pend
= p
+ cof
->raw_syment_count
;
2253 if (p
->u
.syment
.n_sclass
== C_FILE
)
2255 p
+= 1 + p
->u
.syment
.n_numaux
;
2263 /* Look through the C_FILE symbols to find the best one. */
2264 sec_vma
= bfd_get_section_vma (abfd
, section
);
2265 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2266 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2270 combined_entry_type
*p2
;
2272 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2274 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2276 if (p2
->u
.syment
.n_scnum
> 0
2278 == coff_section_from_bfd_index (abfd
,
2279 p2
->u
.syment
.n_scnum
)))
2281 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2288 file_addr
= (bfd_vma
) p2
->u
.syment
.n_value
;
2289 /* PR 11512: Include the section address of the function name symbol. */
2290 if (p2
->u
.syment
.n_scnum
> 0)
2291 file_addr
+= coff_section_from_bfd_index (abfd
,
2292 p2
->u
.syment
.n_scnum
)->vma
;
2293 /* We use <= MAXDIFF here so that if we get a zero length
2294 file, we actually use the next file entry. */
2296 && offset
+ sec_vma
>= file_addr
2297 && offset
+ sec_vma
- file_addr
<= maxdiff
)
2299 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2300 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2303 /* Avoid endless loops on erroneous files by ensuring that
2304 we always move forward in the file. */
2305 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2308 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2309 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2314 /* Now wander though the raw linenumbers of the section. */
2315 /* If we have been called on this section before, and the offset we
2316 want is further down then we can prime the lookup loop. */
2317 sec_data
= coff_section_data (abfd
, section
);
2318 if (sec_data
!= NULL
2320 && offset
>= sec_data
->offset
)
2323 *functionname_ptr
= sec_data
->function
;
2324 line_base
= sec_data
->line_base
;
2332 if (section
->lineno
!= NULL
)
2334 bfd_vma last_value
= 0;
2336 l
= §ion
->lineno
[i
];
2338 for (; i
< section
->lineno_count
; i
++)
2340 if (l
->line_number
== 0)
2342 /* Get the symbol this line number points at. */
2343 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2344 if (coff
->symbol
.value
> offset
)
2346 *functionname_ptr
= coff
->symbol
.name
;
2347 last_value
= coff
->symbol
.value
;
2350 combined_entry_type
*s
= coff
->native
;
2351 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2353 /* In XCOFF a debugging symbol can follow the
2355 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2356 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2358 /* S should now point to the .bf of the function. */
2359 if (s
->u
.syment
.n_numaux
)
2361 /* The linenumber is stored in the auxent. */
2362 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2363 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2364 *line_ptr
= line_base
;
2370 if (l
->u
.offset
> offset
)
2372 *line_ptr
= l
->line_number
+ line_base
- 1;
2377 /* If we fell off the end of the loop, then assume that this
2378 symbol has no line number info. Otherwise, symbols with no
2379 line number info get reported with the line number of the
2380 last line of the last symbol which does have line number
2381 info. We use 0x100 as a slop to account for cases where the
2382 last line has executable code. */
2383 if (i
>= section
->lineno_count
2385 && offset
- last_value
> 0x100)
2387 *functionname_ptr
= NULL
;
2392 /* Cache the results for the next call. */
2393 if (sec_data
== NULL
&& section
->owner
== abfd
)
2395 amt
= sizeof (struct coff_section_tdata
);
2396 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2397 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2399 if (sec_data
!= NULL
)
2401 sec_data
->offset
= offset
;
2402 sec_data
->i
= i
- 1;
2403 sec_data
->function
= *functionname_ptr
;
2404 sec_data
->line_base
= line_base
;
2411 coff_find_nearest_line (bfd
*abfd
,
2415 const char **filename_ptr
,
2416 const char **functionname_ptr
,
2417 unsigned int *line_ptr
)
2419 return coff_find_nearest_line_with_names (abfd
, dwarf_debug_sections
,
2420 section
, symbols
, offset
,
2421 filename_ptr
, functionname_ptr
,
2426 coff_find_nearest_line_discriminator (bfd
*abfd
,
2430 const char **filename_ptr
,
2431 const char **functionname_ptr
,
2432 unsigned int *line_ptr
,
2433 unsigned int *discriminator
)
2436 return coff_find_nearest_line_with_names (abfd
, dwarf_debug_sections
,
2437 section
, symbols
, offset
,
2438 filename_ptr
, functionname_ptr
,
2444 coff_find_inliner_info (bfd
*abfd
,
2445 const char **filename_ptr
,
2446 const char **functionname_ptr
,
2447 unsigned int *line_ptr
)
2451 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
2452 functionname_ptr
, line_ptr
,
2453 &coff_data(abfd
)->dwarf2_find_line_info
);
2458 coff_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2462 if (!info
->relocatable
)
2463 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2465 size
= bfd_coff_filhsz (abfd
);
2467 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2471 /* Change the class of a coff symbol held by BFD. */
2474 bfd_coff_set_symbol_class (bfd
* abfd
,
2476 unsigned int symbol_class
)
2478 coff_symbol_type
* csym
;
2480 csym
= coff_symbol_from (abfd
, symbol
);
2483 bfd_set_error (bfd_error_invalid_operation
);
2486 else if (csym
->native
== NULL
)
2488 /* This is an alien symbol which no native coff backend data.
2489 We cheat here by creating a fake native entry for it and
2490 then filling in the class. This code is based on that in
2491 coff_write_alien_symbol(). */
2493 combined_entry_type
* native
;
2494 bfd_size_type amt
= sizeof (* native
);
2496 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2500 native
->u
.syment
.n_type
= T_NULL
;
2501 native
->u
.syment
.n_sclass
= symbol_class
;
2503 if (bfd_is_und_section (symbol
->section
))
2505 native
->u
.syment
.n_scnum
= N_UNDEF
;
2506 native
->u
.syment
.n_value
= symbol
->value
;
2508 else if (bfd_is_com_section (symbol
->section
))
2510 native
->u
.syment
.n_scnum
= N_UNDEF
;
2511 native
->u
.syment
.n_value
= symbol
->value
;
2515 native
->u
.syment
.n_scnum
=
2516 symbol
->section
->output_section
->target_index
;
2517 native
->u
.syment
.n_value
= (symbol
->value
2518 + symbol
->section
->output_offset
);
2519 if (! obj_pe (abfd
))
2520 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2522 /* Copy the any flags from the file header into the symbol.
2524 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2527 csym
->native
= native
;
2530 csym
->native
->u
.syment
.n_sclass
= symbol_class
;
2535 struct coff_comdat_info
*
2536 bfd_coff_get_comdat_section (bfd
*abfd
, struct bfd_section
*sec
)
2538 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
2539 && coff_section_data (abfd
, sec
) != NULL
)
2540 return coff_section_data (abfd
, sec
)->comdat
;
2546 _bfd_coff_section_already_linked (bfd
*abfd
,
2548 struct bfd_link_info
*info
)
2551 const char *name
, *key
;
2552 struct bfd_section_already_linked
*l
;
2553 struct bfd_section_already_linked_hash_entry
*already_linked_list
;
2554 struct coff_comdat_info
*s_comdat
;
2557 if ((flags
& SEC_LINK_ONCE
) == 0)
2560 /* The COFF backend linker doesn't support group sections. */
2561 if ((flags
& SEC_GROUP
) != 0)
2564 name
= bfd_get_section_name (abfd
, sec
);
2565 s_comdat
= bfd_coff_get_comdat_section (abfd
, sec
);
2567 if (s_comdat
!= NULL
)
2568 key
= s_comdat
->name
;
2571 if (CONST_STRNEQ (name
, ".gnu.linkonce.")
2572 && (key
= strchr (name
+ sizeof (".gnu.linkonce.") - 1, '.')) != NULL
)
2575 /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
2576 .xdata$<key> and .pdata$<key> only the first of which has a
2577 comdat key. Should these all match the LTO IR key? */
2581 already_linked_list
= bfd_section_already_linked_table_lookup (key
);
2583 for (l
= already_linked_list
->entry
; l
!= NULL
; l
= l
->next
)
2585 struct coff_comdat_info
*l_comdat
;
2587 l_comdat
= bfd_coff_get_comdat_section (l
->sec
->owner
, l
->sec
);
2589 /* The section names must match, and both sections must be
2590 comdat and have the same comdat name, or both sections must
2591 be non-comdat. LTO IR plugin sections are an exception. They
2592 are always named .gnu.linkonce.t.<key> (<key> is some string)
2593 and match any comdat section with comdat name of <key>, and
2594 any linkonce section with the same suffix, ie.
2595 .gnu.linkonce.*.<key>. */
2596 if (((s_comdat
!= NULL
) == (l_comdat
!= NULL
)
2597 && strcmp (name
, l
->sec
->name
) == 0)
2598 || (l
->sec
->owner
->flags
& BFD_PLUGIN
) != 0)
2600 /* The section has already been linked. See if we should
2602 return _bfd_handle_already_linked (sec
, l
, info
);
2606 /* This is the first section with this name. Record it. */
2607 if (!bfd_section_already_linked_table_insert (already_linked_list
, sec
))
2608 info
->callbacks
->einfo (_("%F%P: already_linked_table: %E\n"));