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);
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);
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
;
152 /* Read in a COFF object and make it into a BFD. This is used by
155 static const bfd_target
*
156 coff_real_object_p (bfd
*abfd
,
158 struct internal_filehdr
*internal_f
,
159 struct internal_aouthdr
*internal_a
)
161 flagword oflags
= abfd
->flags
;
162 bfd_vma ostart
= bfd_get_start_address (abfd
);
165 bfd_size_type readsize
; /* Length of file_info. */
167 char *external_sections
;
169 if (!(internal_f
->f_flags
& F_RELFLG
))
170 abfd
->flags
|= HAS_RELOC
;
171 if ((internal_f
->f_flags
& F_EXEC
))
172 abfd
->flags
|= EXEC_P
;
173 if (!(internal_f
->f_flags
& F_LNNO
))
174 abfd
->flags
|= HAS_LINENO
;
175 if (!(internal_f
->f_flags
& F_LSYMS
))
176 abfd
->flags
|= HAS_LOCALS
;
178 /* FIXME: How can we set D_PAGED correctly? */
179 if ((internal_f
->f_flags
& F_EXEC
) != 0)
180 abfd
->flags
|= D_PAGED
;
182 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
183 if (internal_f
->f_nsyms
)
184 abfd
->flags
|= HAS_SYMS
;
186 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
187 bfd_get_start_address (abfd
) = internal_a
->entry
;
189 bfd_get_start_address (abfd
) = 0;
191 /* Set up the tdata area. ECOFF uses its own routine, and overrides
193 tdata_save
= abfd
->tdata
.any
;
194 tdata
= bfd_coff_mkobject_hook (abfd
, (void *) internal_f
, (void *) internal_a
);
198 scnhsz
= bfd_coff_scnhsz (abfd
);
199 readsize
= (bfd_size_type
) nscns
* scnhsz
;
200 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
201 if (!external_sections
)
204 if (bfd_bread ((void *) external_sections
, readsize
, abfd
) != readsize
)
207 /* Set the arch/mach *before* swapping in sections; section header swapping
208 may depend on arch/mach info. */
209 if (! bfd_coff_set_arch_mach_hook (abfd
, (void *) internal_f
))
212 /* Now copy data as required; construct all asections etc. */
216 for (i
= 0; i
< nscns
; i
++)
218 struct internal_scnhdr tmp
;
219 bfd_coff_swap_scnhdr_in (abfd
,
220 (void *) (external_sections
+ i
* scnhsz
),
222 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
230 bfd_release (abfd
, tdata
);
232 abfd
->tdata
.any
= tdata_save
;
233 abfd
->flags
= oflags
;
234 bfd_get_start_address (abfd
) = ostart
;
235 return (const bfd_target
*) NULL
;
238 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
239 not a COFF file. This is also used by ECOFF. */
242 coff_object_p (bfd
*abfd
)
244 bfd_size_type filhsz
;
245 bfd_size_type aoutsz
;
248 struct internal_filehdr internal_f
;
249 struct internal_aouthdr internal_a
;
251 /* Figure out how much to read. */
252 filhsz
= bfd_coff_filhsz (abfd
);
253 aoutsz
= bfd_coff_aoutsz (abfd
);
255 filehdr
= bfd_alloc (abfd
, filhsz
);
258 if (bfd_bread (filehdr
, filhsz
, abfd
) != filhsz
)
260 if (bfd_get_error () != bfd_error_system_call
)
261 bfd_set_error (bfd_error_wrong_format
);
262 bfd_release (abfd
, filehdr
);
265 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
266 bfd_release (abfd
, filehdr
);
268 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
269 (less than aoutsz) used in object files and AOUTSZ (equal to
270 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
271 expects this header to be aoutsz bytes in length, so we use that
272 value in the call to bfd_alloc below. But we must be careful to
273 only read in f_opthdr bytes in the call to bfd_bread. We should
274 also attempt to catch corrupt or non-COFF binaries with a strange
275 value for f_opthdr. */
276 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
277 || internal_f
.f_opthdr
> aoutsz
)
279 bfd_set_error (bfd_error_wrong_format
);
282 nscns
= internal_f
.f_nscns
;
284 if (internal_f
.f_opthdr
)
288 opthdr
= bfd_alloc (abfd
, aoutsz
);
291 if (bfd_bread (opthdr
, (bfd_size_type
) internal_f
.f_opthdr
, abfd
)
292 != internal_f
.f_opthdr
)
294 bfd_release (abfd
, opthdr
);
297 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (void *) &internal_a
);
298 bfd_release (abfd
, opthdr
);
301 return coff_real_object_p (abfd
, nscns
, &internal_f
,
302 (internal_f
.f_opthdr
!= 0
304 : (struct internal_aouthdr
*) NULL
));
307 /* Get the BFD section from a COFF symbol section number. */
310 coff_section_from_bfd_index (bfd
*abfd
, int section_index
)
312 struct bfd_section
*answer
= abfd
->sections
;
314 if (section_index
== N_ABS
)
315 return bfd_abs_section_ptr
;
316 if (section_index
== N_UNDEF
)
317 return bfd_und_section_ptr
;
318 if (section_index
== N_DEBUG
)
319 return bfd_abs_section_ptr
;
323 if (answer
->target_index
== section_index
)
325 answer
= answer
->next
;
328 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
329 has a bad symbol table in biglitpow.o. */
330 return bfd_und_section_ptr
;
333 /* Get the upper bound of a COFF symbol table. */
336 coff_get_symtab_upper_bound (bfd
*abfd
)
338 if (!bfd_coff_slurp_symbol_table (abfd
))
341 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
344 /* Canonicalize a COFF symbol table. */
347 coff_canonicalize_symtab (bfd
*abfd
, asymbol
**alocation
)
349 unsigned int counter
;
350 coff_symbol_type
*symbase
;
351 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
353 if (!bfd_coff_slurp_symbol_table (abfd
))
356 symbase
= obj_symbols (abfd
);
357 counter
= bfd_get_symcount (abfd
);
358 while (counter
-- > 0)
359 *location
++ = symbase
++;
363 return bfd_get_symcount (abfd
);
366 /* Get the name of a symbol. The caller must pass in a buffer of size
370 _bfd_coff_internal_syment_name (bfd
*abfd
,
371 const struct internal_syment
*sym
,
374 /* FIXME: It's not clear this will work correctly if sizeof
376 if (sym
->_n
._n_n
._n_zeroes
!= 0
377 || sym
->_n
._n_n
._n_offset
== 0)
379 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
380 buf
[SYMNMLEN
] = '\0';
387 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
388 strings
= obj_coff_strings (abfd
);
391 strings
= _bfd_coff_read_string_table (abfd
);
395 return strings
+ sym
->_n
._n_n
._n_offset
;
399 /* Read in and swap the relocs. This returns a buffer holding the
400 relocs for section SEC in file ABFD. If CACHE is TRUE and
401 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
402 the function is called again. If EXTERNAL_RELOCS is not NULL, it
403 is a buffer large enough to hold the unswapped relocs. If
404 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
405 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
406 value must be INTERNAL_RELOCS. The function returns NULL on error. */
408 struct internal_reloc
*
409 _bfd_coff_read_internal_relocs (bfd
*abfd
,
412 bfd_byte
*external_relocs
,
413 bfd_boolean require_internal
,
414 struct internal_reloc
*internal_relocs
)
417 bfd_byte
*free_external
= NULL
;
418 struct internal_reloc
*free_internal
= NULL
;
421 struct internal_reloc
*irel
;
424 if (sec
->reloc_count
== 0)
425 return internal_relocs
; /* Nothing to do. */
427 if (coff_section_data (abfd
, sec
) != NULL
428 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
430 if (! require_internal
)
431 return coff_section_data (abfd
, sec
)->relocs
;
432 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
433 sec
->reloc_count
* sizeof (struct internal_reloc
));
434 return internal_relocs
;
437 relsz
= bfd_coff_relsz (abfd
);
439 amt
= sec
->reloc_count
* relsz
;
440 if (external_relocs
== NULL
)
442 free_external
= (bfd_byte
*) bfd_malloc (amt
);
443 if (free_external
== NULL
)
445 external_relocs
= free_external
;
448 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
449 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
452 if (internal_relocs
== NULL
)
454 amt
= sec
->reloc_count
;
455 amt
*= sizeof (struct internal_reloc
);
456 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
457 if (free_internal
== NULL
)
459 internal_relocs
= free_internal
;
462 /* Swap in the relocs. */
463 erel
= external_relocs
;
464 erel_end
= erel
+ relsz
* sec
->reloc_count
;
465 irel
= internal_relocs
;
466 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
467 bfd_coff_swap_reloc_in (abfd
, (void *) erel
, (void *) irel
);
469 if (free_external
!= NULL
)
471 free (free_external
);
472 free_external
= NULL
;
475 if (cache
&& free_internal
!= NULL
)
477 if (coff_section_data (abfd
, sec
) == NULL
)
479 amt
= sizeof (struct coff_section_tdata
);
480 sec
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
481 if (sec
->used_by_bfd
== NULL
)
483 coff_section_data (abfd
, sec
)->contents
= NULL
;
485 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
488 return internal_relocs
;
491 if (free_external
!= NULL
)
492 free (free_external
);
493 if (free_internal
!= NULL
)
494 free (free_internal
);
498 /* Set lineno_count for the output sections of a COFF file. */
501 coff_count_linenumbers (bfd
*abfd
)
503 unsigned int limit
= bfd_get_symcount (abfd
);
511 /* This may be from the backend linker, in which case the
512 lineno_count in the sections is correct. */
513 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
514 total
+= s
->lineno_count
;
518 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
519 BFD_ASSERT (s
->lineno_count
== 0);
521 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
523 asymbol
*q_maybe
= *p
;
525 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
527 coff_symbol_type
*q
= coffsymbol (q_maybe
);
529 /* The AIX 4.1 compiler can sometimes generate line numbers
530 attached to debugging symbols. We try to simply ignore
532 if (q
->lineno
!= NULL
533 && q
->symbol
.section
->owner
!= NULL
)
535 /* This symbol has line numbers. Increment the owning
536 section's linenumber count. */
537 alent
*l
= q
->lineno
;
541 asection
* sec
= q
->symbol
.section
->output_section
;
543 /* Do not try to update fields in read-only sections. */
544 if (! bfd_is_const_section (sec
))
545 sec
->lineno_count
++;
550 while (l
->line_number
!= 0);
558 /* Takes a bfd and a symbol, returns a pointer to the coff specific
559 area of the symbol if there is one. */
562 coff_symbol_from (bfd
*ignore_abfd ATTRIBUTE_UNUSED
,
565 if (!bfd_family_coff (bfd_asymbol_bfd (symbol
)))
566 return (coff_symbol_type
*) NULL
;
568 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
569 return (coff_symbol_type
*) NULL
;
571 return (coff_symbol_type
*) symbol
;
575 fixup_symbol_value (bfd
*abfd
,
576 coff_symbol_type
*coff_symbol_ptr
,
577 struct internal_syment
*syment
)
579 /* Normalize the symbol flags. */
580 if (coff_symbol_ptr
->symbol
.section
581 && bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
583 /* A common symbol is undefined with a value. */
584 syment
->n_scnum
= N_UNDEF
;
585 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
587 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
588 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
590 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
592 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
594 syment
->n_scnum
= N_UNDEF
;
597 /* FIXME: Do we need to handle the absolute section here? */
600 if (coff_symbol_ptr
->symbol
.section
)
603 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
605 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
606 + coff_symbol_ptr
->symbol
.section
->output_offset
);
609 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
610 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
611 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
617 /* This can happen, but I don't know why yet (steve@cygnus.com) */
618 syment
->n_scnum
= N_ABS
;
619 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
624 /* Run through all the symbols in the symbol table and work out what
625 their indexes into the symbol table will be when output.
627 Coff requires that each C_FILE symbol points to the next one in the
628 chain, and that the last one points to the first external symbol. We
632 coff_renumber_symbols (bfd
*bfd_ptr
, int *first_undef
)
634 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
635 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
636 unsigned int native_index
= 0;
637 struct internal_syment
*last_file
= NULL
;
638 unsigned int symbol_index
;
640 /* COFF demands that undefined symbols come after all other symbols.
641 Since we don't need to impose this extra knowledge on all our
642 client programs, deal with that here. Sort the symbol table;
643 just move the undefined symbols to the end, leaving the rest
644 alone. The O'Reilly book says that defined global symbols come
645 at the end before the undefined symbols, so we do that here as
647 /* @@ Do we have some condition we could test for, so we don't always
648 have to do this? I don't think relocatability is quite right, but
649 I'm not certain. [raeburn:19920508.1711EST] */
655 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
656 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
659 bfd_ptr
->outsymbols
= newsyms
;
660 for (i
= 0; i
< symbol_count
; i
++)
661 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
662 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
663 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
664 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
665 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
667 *newsyms
++ = symbol_ptr_ptr
[i
];
669 for (i
= 0; i
< symbol_count
; i
++)
670 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
671 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
672 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
673 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
674 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
676 *newsyms
++ = symbol_ptr_ptr
[i
];
678 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
680 for (i
= 0; i
< symbol_count
; i
++)
681 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
682 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
683 *newsyms
++ = symbol_ptr_ptr
[i
];
684 *newsyms
= (asymbol
*) NULL
;
685 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
688 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
690 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
691 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
692 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
694 combined_entry_type
*s
= coff_symbol_ptr
->native
;
697 if (s
->u
.syment
.n_sclass
== C_FILE
)
699 if (last_file
!= NULL
)
700 last_file
->n_value
= native_index
;
701 last_file
= &(s
->u
.syment
);
704 /* Modify the symbol values according to their section and
706 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
708 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
709 s
[i
].offset
= native_index
++;
715 obj_conv_table_size (bfd_ptr
) = native_index
;
720 /* Run thorough the symbol table again, and fix it so that all
721 pointers to entries are changed to the entries' index in the output
725 coff_mangle_symbols (bfd
*bfd_ptr
)
727 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
728 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
729 unsigned int symbol_index
;
731 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
733 coff_symbol_type
*coff_symbol_ptr
=
734 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
736 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
739 combined_entry_type
*s
= coff_symbol_ptr
->native
;
743 /* FIXME: We should use a union here. */
744 s
->u
.syment
.n_value
=
745 (bfd_hostptr_t
) ((combined_entry_type
*)
746 ((bfd_hostptr_t
) s
->u
.syment
.n_value
))->offset
;
751 /* The value is the offset into the line number entries
752 for the symbol's section. On output, the symbol's
753 section should be N_DEBUG. */
754 s
->u
.syment
.n_value
=
755 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
756 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
757 coff_symbol_ptr
->symbol
.section
=
758 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
759 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
761 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
763 combined_entry_type
*a
= s
+ i
+ 1;
766 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
767 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
772 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
773 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
778 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
779 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
788 coff_fix_symbol_name (bfd
*abfd
,
790 combined_entry_type
*native
,
791 bfd_size_type
*string_size_p
,
792 asection
**debug_string_section_p
,
793 bfd_size_type
*debug_string_size_p
)
795 unsigned int name_length
;
796 union internal_auxent
*auxent
;
797 char *name
= (char *) (symbol
->name
);
801 /* COFF symbols always have names, so we'll make one up. */
802 symbol
->name
= "strange";
803 name
= (char *) symbol
->name
;
805 name_length
= strlen (name
);
807 if (native
->u
.syment
.n_sclass
== C_FILE
808 && native
->u
.syment
.n_numaux
> 0)
810 unsigned int filnmlen
;
812 if (bfd_coff_force_symnames_in_strings (abfd
))
814 native
->u
.syment
._n
._n_n
._n_offset
=
815 (*string_size_p
+ STRING_SIZE_SIZE
);
816 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
817 *string_size_p
+= 6; /* strlen(".file") + 1 */
820 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
822 auxent
= &(native
+ 1)->u
.auxent
;
824 filnmlen
= bfd_coff_filnmlen (abfd
);
826 if (bfd_coff_long_filenames (abfd
))
828 if (name_length
<= filnmlen
)
829 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
832 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
833 auxent
->x_file
.x_n
.x_zeroes
= 0;
834 *string_size_p
+= name_length
+ 1;
839 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
840 if (name_length
> filnmlen
)
841 name
[filnmlen
] = '\0';
846 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
847 /* This name will fit into the symbol neatly. */
848 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
850 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
852 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
854 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
855 *string_size_p
+= name_length
+ 1;
861 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
863 /* This name should be written into the .debug section. For
864 some reason each name is preceded by a two byte length
865 and also followed by a null byte. FIXME: We assume that
866 the .debug section has already been created, and that it
868 if (*debug_string_section_p
== (asection
*) NULL
)
869 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
870 filepos
= bfd_tell (abfd
);
872 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
874 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
876 if (!bfd_set_section_contents (abfd
,
877 *debug_string_section_p
,
879 (file_ptr
) *debug_string_size_p
,
880 (bfd_size_type
) prefix_len
)
881 || !bfd_set_section_contents (abfd
,
882 *debug_string_section_p
,
883 (void *) symbol
->name
,
884 (file_ptr
) (*debug_string_size_p
886 (bfd_size_type
) name_length
+ 1))
888 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
890 native
->u
.syment
._n
._n_n
._n_offset
=
891 *debug_string_size_p
+ prefix_len
;
892 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
893 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
898 /* We need to keep track of the symbol index so that when we write out
899 the relocs we can get the index for a symbol. This method is a
902 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
904 /* Write a symbol out to a COFF file. */
907 coff_write_symbol (bfd
*abfd
,
909 combined_entry_type
*native
,
911 bfd_size_type
*string_size_p
,
912 asection
**debug_string_section_p
,
913 bfd_size_type
*debug_string_size_p
)
915 unsigned int numaux
= native
->u
.syment
.n_numaux
;
916 int type
= native
->u
.syment
.n_type
;
917 int n_sclass
= (int) native
->u
.syment
.n_sclass
;
918 asection
*output_section
= symbol
->section
->output_section
919 ? symbol
->section
->output_section
922 bfd_size_type symesz
;
924 if (native
->u
.syment
.n_sclass
== C_FILE
)
925 symbol
->flags
|= BSF_DEBUGGING
;
927 if (symbol
->flags
& BSF_DEBUGGING
928 && bfd_is_abs_section (symbol
->section
))
929 native
->u
.syment
.n_scnum
= N_DEBUG
;
931 else if (bfd_is_abs_section (symbol
->section
))
932 native
->u
.syment
.n_scnum
= N_ABS
;
934 else if (bfd_is_und_section (symbol
->section
))
935 native
->u
.syment
.n_scnum
= N_UNDEF
;
938 native
->u
.syment
.n_scnum
=
939 output_section
->target_index
;
941 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
942 debug_string_section_p
, debug_string_size_p
);
944 symesz
= bfd_coff_symesz (abfd
);
945 buf
= bfd_alloc (abfd
, symesz
);
948 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
949 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
951 bfd_release (abfd
, buf
);
953 if (native
->u
.syment
.n_numaux
> 0)
955 bfd_size_type auxesz
;
958 auxesz
= bfd_coff_auxesz (abfd
);
959 buf
= bfd_alloc (abfd
, auxesz
);
962 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
964 bfd_coff_swap_aux_out (abfd
,
965 &((native
+ j
+ 1)->u
.auxent
),
966 type
, n_sclass
, (int) j
,
967 native
->u
.syment
.n_numaux
,
969 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
972 bfd_release (abfd
, buf
);
975 /* Store the index for use when we write out the relocs. */
976 set_index (symbol
, *written
);
978 *written
+= numaux
+ 1;
982 /* Write out a symbol to a COFF file that does not come from a COFF
983 file originally. This symbol may have been created by the linker,
984 or we may be linking a non COFF file to a COFF file. */
987 coff_write_alien_symbol (bfd
*abfd
,
990 bfd_size_type
*string_size_p
,
991 asection
**debug_string_section_p
,
992 bfd_size_type
*debug_string_size_p
)
994 combined_entry_type
*native
;
995 combined_entry_type dummy
;
996 asection
*output_section
= symbol
->section
->output_section
997 ? symbol
->section
->output_section
1001 native
->u
.syment
.n_type
= T_NULL
;
1002 native
->u
.syment
.n_flags
= 0;
1003 if (bfd_is_und_section (symbol
->section
))
1005 native
->u
.syment
.n_scnum
= N_UNDEF
;
1006 native
->u
.syment
.n_value
= symbol
->value
;
1008 else if (bfd_is_com_section (symbol
->section
))
1010 native
->u
.syment
.n_scnum
= N_UNDEF
;
1011 native
->u
.syment
.n_value
= symbol
->value
;
1013 else if (symbol
->flags
& BSF_DEBUGGING
)
1015 /* There isn't much point to writing out a debugging symbol
1016 unless we are prepared to convert it into COFF debugging
1017 format. So, we just ignore them. We must clobber the symbol
1018 name to keep it from being put in the string table. */
1024 native
->u
.syment
.n_scnum
= output_section
->target_index
;
1025 native
->u
.syment
.n_value
= (symbol
->value
1026 + symbol
->section
->output_offset
);
1027 if (! obj_pe (abfd
))
1028 native
->u
.syment
.n_value
+= output_section
->vma
;
1030 /* Copy the any flags from the file header into the symbol.
1033 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1034 if (c
!= (coff_symbol_type
*) NULL
)
1035 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1039 native
->u
.syment
.n_type
= 0;
1040 if (symbol
->flags
& BSF_LOCAL
)
1041 native
->u
.syment
.n_sclass
= C_STAT
;
1042 else if (symbol
->flags
& BSF_WEAK
)
1043 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1045 native
->u
.syment
.n_sclass
= C_EXT
;
1046 native
->u
.syment
.n_numaux
= 0;
1048 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1049 debug_string_section_p
, debug_string_size_p
);
1052 /* Write a native symbol to a COFF file. */
1055 coff_write_native_symbol (bfd
*abfd
,
1056 coff_symbol_type
*symbol
,
1058 bfd_size_type
*string_size_p
,
1059 asection
**debug_string_section_p
,
1060 bfd_size_type
*debug_string_size_p
)
1062 combined_entry_type
*native
= symbol
->native
;
1063 alent
*lineno
= symbol
->lineno
;
1065 /* If this symbol has an associated line number, we must store the
1066 symbol index in the line number field. We also tag the auxent to
1067 point to the right place in the lineno table. */
1068 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1070 unsigned int count
= 0;
1072 lineno
[count
].u
.offset
= *written
;
1073 if (native
->u
.syment
.n_numaux
)
1075 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1077 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1078 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1081 /* Count and relocate all other linenumbers. */
1083 while (lineno
[count
].line_number
!= 0)
1085 lineno
[count
].u
.offset
+=
1086 (symbol
->symbol
.section
->output_section
->vma
1087 + symbol
->symbol
.section
->output_offset
);
1090 symbol
->done_lineno
= TRUE
;
1092 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1093 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1094 count
* bfd_coff_linesz (abfd
);
1097 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1098 string_size_p
, debug_string_section_p
,
1099 debug_string_size_p
);
1103 null_error_handler (const char * fmt ATTRIBUTE_UNUSED
, ...)
1107 /* Write out the COFF symbols. */
1110 coff_write_symbols (bfd
*abfd
)
1112 bfd_size_type string_size
;
1113 asection
*debug_string_section
;
1114 bfd_size_type debug_string_size
;
1116 unsigned int limit
= bfd_get_symcount (abfd
);
1117 bfd_vma written
= 0;
1121 debug_string_section
= NULL
;
1122 debug_string_size
= 0;
1124 /* If this target supports long section names, they must be put into
1125 the string table. This is supported by PE. This code must
1126 handle section names just as they are handled in
1127 coff_write_object_contents. */
1128 if (bfd_coff_long_section_names (abfd
))
1132 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1136 len
= strlen (o
->name
);
1138 string_size
+= len
+ 1;
1142 /* Seek to the right place. */
1143 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1146 /* Output all the symbols we have. */
1148 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1150 asymbol
*symbol
= *p
;
1151 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1153 if (c_symbol
== (coff_symbol_type
*) NULL
1154 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1156 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1157 &debug_string_section
,
1158 &debug_string_size
))
1163 if (coff_backend_info (abfd
)->_bfd_coff_classify_symbol
!= NULL
)
1165 bfd_error_handler_type current_error_handler
;
1166 enum coff_symbol_classification sym_class
;
1167 unsigned char *n_sclass
;
1169 /* Suppress error reporting by bfd_coff_classify_symbol.
1170 Error messages can be generated when we are processing a local
1171 symbol which has no associated section and we do not have to
1172 worry about this, all we need to know is that it is local. */
1173 current_error_handler
= bfd_set_error_handler (null_error_handler
);
1174 sym_class
= bfd_coff_classify_symbol (abfd
,
1175 &c_symbol
->native
->u
.syment
);
1176 (void) bfd_set_error_handler (current_error_handler
);
1178 n_sclass
= &c_symbol
->native
->u
.syment
.n_sclass
;
1180 /* If the symbol class has been changed (eg objcopy/ld script/etc)
1181 we cannot retain the existing sclass from the original symbol.
1182 Weak symbols only have one valid sclass, so just set it always.
1183 If it is not local class and should be, set it C_STAT.
1184 If it is global and not classified as global, or if it is
1185 weak (which is also classified as global), set it C_EXT. */
1187 if (symbol
->flags
& BSF_WEAK
)
1188 *n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1189 else if (symbol
->flags
& BSF_LOCAL
&& sym_class
!= COFF_SYMBOL_LOCAL
)
1191 else if (symbol
->flags
& BSF_GLOBAL
1192 && (sym_class
!= COFF_SYMBOL_GLOBAL
1194 || *n_sclass
== C_NT_WEAK
1196 || *n_sclass
== C_WEAKEXT
))
1197 c_symbol
->native
->u
.syment
.n_sclass
= C_EXT
;
1200 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1201 &string_size
, &debug_string_section
,
1202 &debug_string_size
))
1207 obj_raw_syment_count (abfd
) = written
;
1209 /* Now write out strings. */
1210 if (string_size
!= 0)
1212 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1213 bfd_byte buffer
[STRING_SIZE_SIZE
];
1215 #if STRING_SIZE_SIZE == 4
1216 H_PUT_32 (abfd
, size
, buffer
);
1218 #error Change H_PUT_32
1220 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1224 /* Handle long section names. This code must handle section
1225 names just as they are handled in coff_write_object_contents. */
1226 if (bfd_coff_long_section_names (abfd
))
1230 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1234 len
= strlen (o
->name
);
1237 if (bfd_bwrite (o
->name
, (bfd_size_type
) (len
+ 1), abfd
)
1244 for (p
= abfd
->outsymbols
, i
= 0;
1249 size_t name_length
= strlen (q
->name
);
1250 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1253 /* Figure out whether the symbol name should go in the string
1254 table. Symbol names that are short enough are stored
1255 directly in the syment structure. File names permit a
1256 different, longer, length in the syment structure. On
1257 XCOFF, some symbol names are stored in the .debug section
1258 rather than in the string table. */
1260 if (c_symbol
== NULL
1261 || c_symbol
->native
== NULL
)
1262 /* This is not a COFF symbol, so it certainly is not a
1263 file name, nor does it go in the .debug section. */
1264 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1266 else if (bfd_coff_symname_in_debug (abfd
,
1267 &c_symbol
->native
->u
.syment
))
1268 /* This symbol name is in the XCOFF .debug section.
1269 Don't write it into the string table. */
1270 maxlen
= name_length
;
1272 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1273 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1275 if (bfd_coff_force_symnames_in_strings (abfd
))
1277 if (bfd_bwrite (".file", (bfd_size_type
) 6, abfd
) != 6)
1280 maxlen
= bfd_coff_filnmlen (abfd
);
1283 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1285 if (name_length
> maxlen
)
1287 if (bfd_bwrite ((void *) (q
->name
), (bfd_size_type
) name_length
+ 1,
1288 abfd
) != name_length
+ 1)
1295 /* We would normally not write anything here, but we'll write
1296 out 4 so that any stupid coff reader which tries to read the
1297 string table even when there isn't one won't croak. */
1298 unsigned int size
= STRING_SIZE_SIZE
;
1299 bfd_byte buffer
[STRING_SIZE_SIZE
];
1301 #if STRING_SIZE_SIZE == 4
1302 H_PUT_32 (abfd
, size
, buffer
);
1304 #error Change H_PUT_32
1306 if (bfd_bwrite ((void *) buffer
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1307 != STRING_SIZE_SIZE
)
1311 /* Make sure the .debug section was created to be the correct size.
1312 We should create it ourselves on the fly, but we don't because
1313 BFD won't let us write to any section until we know how large all
1314 the sections are. We could still do it by making another pass
1315 over the symbols. FIXME. */
1316 BFD_ASSERT (debug_string_size
== 0
1317 || (debug_string_section
!= (asection
*) NULL
1318 && (BFD_ALIGN (debug_string_size
,
1319 1 << debug_string_section
->alignment_power
)
1320 == debug_string_section
->size
)));
1326 coff_write_linenumbers (bfd
*abfd
)
1329 bfd_size_type linesz
;
1332 linesz
= bfd_coff_linesz (abfd
);
1333 buff
= bfd_alloc (abfd
, linesz
);
1336 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1338 if (s
->lineno_count
)
1340 asymbol
**q
= abfd
->outsymbols
;
1341 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1343 /* Find all the linenumbers in this section. */
1347 if (p
->section
->output_section
== s
)
1350 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1351 (bfd_asymbol_bfd (p
), p
));
1354 /* Found a linenumber entry, output. */
1355 struct internal_lineno out
;
1356 memset ((void *) & out
, 0, sizeof (out
));
1358 out
.l_addr
.l_symndx
= l
->u
.offset
;
1359 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1360 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1364 while (l
->line_number
)
1366 out
.l_lnno
= l
->line_number
;
1367 out
.l_addr
.l_symndx
= l
->u
.offset
;
1368 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1369 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1380 bfd_release (abfd
, buff
);
1385 coff_get_lineno (bfd
*ignore_abfd ATTRIBUTE_UNUSED
, asymbol
*symbol
)
1387 return coffsymbol (symbol
)->lineno
;
1390 /* This function transforms the offsets into the symbol table into
1391 pointers to syments. */
1394 coff_pointerize_aux (bfd
*abfd
,
1395 combined_entry_type
*table_base
,
1396 combined_entry_type
*symbol
,
1397 unsigned int indaux
,
1398 combined_entry_type
*auxent
)
1400 unsigned int type
= symbol
->u
.syment
.n_type
;
1401 unsigned int n_sclass
= symbol
->u
.syment
.n_sclass
;
1403 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1405 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1406 (abfd
, table_base
, symbol
, indaux
, auxent
))
1410 /* Don't bother if this is a file or a section. */
1411 if (n_sclass
== C_STAT
&& type
== T_NULL
)
1413 if (n_sclass
== C_FILE
)
1416 /* Otherwise patch up. */
1417 #define N_TMASK coff_data (abfd)->local_n_tmask
1418 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1420 if ((ISFCN (type
) || ISTAG (n_sclass
) || n_sclass
== C_BLOCK
1421 || n_sclass
== C_FCN
)
1422 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1424 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1425 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1426 auxent
->fix_end
= 1;
1428 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1429 generate one, so we must be careful to ignore it. */
1430 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1432 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1433 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1434 auxent
->fix_tag
= 1;
1438 /* Allocate space for the ".debug" section, and read it.
1439 We did not read the debug section until now, because
1440 we didn't want to go to the trouble until someone needed it. */
1443 build_debug_section (bfd
*abfd
)
1445 char *debug_section
;
1447 bfd_size_type sec_size
;
1449 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1453 bfd_set_error (bfd_error_no_debug_section
);
1457 sec_size
= sect
->size
;
1458 debug_section
= (char *) bfd_alloc (abfd
, sec_size
);
1459 if (debug_section
== NULL
)
1462 /* Seek to the beginning of the `.debug' section and read it.
1463 Save the current position first; it is needed by our caller.
1464 Then read debug section and reset the file pointer. */
1466 position
= bfd_tell (abfd
);
1467 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1468 || bfd_bread (debug_section
, sec_size
, abfd
) != sec_size
1469 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1471 return debug_section
;
1474 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1475 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1476 be \0-terminated. */
1479 copy_name (bfd
*abfd
, char *name
, size_t maxlen
)
1484 for (len
= 0; len
< maxlen
; ++len
)
1485 if (name
[len
] == '\0')
1488 if ((newname
= (char *) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1491 strncpy (newname
, name
, len
);
1492 newname
[len
] = '\0';
1496 /* Read in the external symbols. */
1499 _bfd_coff_get_external_symbols (bfd
*abfd
)
1501 bfd_size_type symesz
;
1505 if (obj_coff_external_syms (abfd
) != NULL
)
1508 symesz
= bfd_coff_symesz (abfd
);
1510 size
= obj_raw_syment_count (abfd
) * symesz
;
1514 syms
= bfd_malloc (size
);
1518 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1519 || bfd_bread (syms
, size
, abfd
) != size
)
1526 obj_coff_external_syms (abfd
) = syms
;
1531 /* Read in the external strings. The strings are not loaded until
1532 they are needed. This is because we have no simple way of
1533 detecting a missing string table in an archive. */
1536 _bfd_coff_read_string_table (bfd
*abfd
)
1538 char extstrsize
[STRING_SIZE_SIZE
];
1539 bfd_size_type strsize
;
1543 if (obj_coff_strings (abfd
) != NULL
)
1544 return obj_coff_strings (abfd
);
1546 if (obj_sym_filepos (abfd
) == 0)
1548 bfd_set_error (bfd_error_no_symbols
);
1552 pos
= obj_sym_filepos (abfd
);
1553 pos
+= obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
);
1554 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1557 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1558 != sizeof extstrsize
)
1560 if (bfd_get_error () != bfd_error_file_truncated
)
1563 /* There is no string table. */
1564 strsize
= STRING_SIZE_SIZE
;
1568 #if STRING_SIZE_SIZE == 4
1569 strsize
= H_GET_32 (abfd
, extstrsize
);
1571 #error Change H_GET_32
1575 if (strsize
< STRING_SIZE_SIZE
)
1577 (*_bfd_error_handler
)
1578 (_("%B: bad string table size %lu"), abfd
, (unsigned long) strsize
);
1579 bfd_set_error (bfd_error_bad_value
);
1583 strings
= (char *) bfd_malloc (strsize
);
1584 if (strings
== NULL
)
1587 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1588 != strsize
- STRING_SIZE_SIZE
)
1594 obj_coff_strings (abfd
) = strings
;
1599 /* Free up the external symbols and strings read from a COFF file. */
1602 _bfd_coff_free_symbols (bfd
*abfd
)
1604 if (obj_coff_external_syms (abfd
) != NULL
1605 && ! obj_coff_keep_syms (abfd
))
1607 free (obj_coff_external_syms (abfd
));
1608 obj_coff_external_syms (abfd
) = NULL
;
1610 if (obj_coff_strings (abfd
) != NULL
1611 && ! obj_coff_keep_strings (abfd
))
1613 free (obj_coff_strings (abfd
));
1614 obj_coff_strings (abfd
) = NULL
;
1619 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1620 knit the symbol names into a normalized form. By normalized here I
1621 mean that all symbols have an n_offset pointer that points to a null-
1622 terminated string. */
1624 combined_entry_type
*
1625 coff_get_normalized_symtab (bfd
*abfd
)
1627 combined_entry_type
*internal
;
1628 combined_entry_type
*internal_ptr
;
1629 combined_entry_type
*symbol_ptr
;
1630 combined_entry_type
*internal_end
;
1634 const char *string_table
= NULL
;
1635 char *debug_section
= NULL
;
1638 if (obj_raw_syments (abfd
) != NULL
)
1639 return obj_raw_syments (abfd
);
1641 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1642 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1643 if (internal
== NULL
&& size
!= 0)
1645 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1647 if (! _bfd_coff_get_external_symbols (abfd
))
1650 raw_src
= (char *) obj_coff_external_syms (abfd
);
1652 /* Mark the end of the symbols. */
1653 symesz
= bfd_coff_symesz (abfd
);
1654 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1656 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1657 probably possible. If one shows up, it will probably kill us. */
1659 /* Swap all the raw entries. */
1660 for (internal_ptr
= internal
;
1662 raw_src
+= symesz
, internal_ptr
++)
1666 bfd_coff_swap_sym_in (abfd
, (void *) raw_src
,
1667 (void *) & internal_ptr
->u
.syment
);
1668 symbol_ptr
= internal_ptr
;
1671 i
< symbol_ptr
->u
.syment
.n_numaux
;
1676 bfd_coff_swap_aux_in (abfd
, (void *) raw_src
,
1677 symbol_ptr
->u
.syment
.n_type
,
1678 symbol_ptr
->u
.syment
.n_sclass
,
1679 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1680 &(internal_ptr
->u
.auxent
));
1681 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1686 /* Free the raw symbols, but not the strings (if we have them). */
1687 obj_coff_keep_strings (abfd
) = TRUE
;
1688 if (! _bfd_coff_free_symbols (abfd
))
1691 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1694 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1695 && internal_ptr
->u
.syment
.n_numaux
> 0)
1697 /* Make a file symbol point to the name in the auxent, since
1698 the text ".file" is redundant. */
1699 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1701 /* The filename is a long one, point into the string table. */
1702 if (string_table
== NULL
)
1704 string_table
= _bfd_coff_read_string_table (abfd
);
1705 if (string_table
== NULL
)
1709 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1712 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1716 /* Ordinary short filename, put into memory anyway. The
1717 Microsoft PE tools sometimes store a filename in
1718 multiple AUX entries. */
1719 if (internal_ptr
->u
.syment
.n_numaux
> 1
1720 && coff_data (abfd
)->pe
)
1721 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1724 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1725 internal_ptr
->u
.syment
.n_numaux
* symesz
));
1727 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1730 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1731 (size_t) bfd_coff_filnmlen (abfd
)));
1736 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1738 /* This is a "short" name. Make it long. */
1742 /* Find the length of this string without walking into memory
1744 for (i
= 0; i
< 8; ++i
)
1745 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1748 newstring
= (char *) bfd_zalloc (abfd
, (bfd_size_type
) (i
+ 1));
1749 if (newstring
== NULL
)
1751 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1752 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) newstring
;
1753 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1755 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1756 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
) "";
1757 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1759 /* Long name already. Point symbol at the string in the
1761 if (string_table
== NULL
)
1763 string_table
= _bfd_coff_read_string_table (abfd
);
1764 if (string_table
== NULL
)
1767 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1770 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1774 /* Long name in debug section. Very similar. */
1775 if (debug_section
== NULL
)
1776 debug_section
= build_debug_section (abfd
);
1777 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (bfd_hostptr_t
)
1778 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1781 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1784 obj_raw_syments (abfd
) = internal
;
1785 BFD_ASSERT (obj_raw_syment_count (abfd
)
1786 == (unsigned int) (internal_ptr
- internal
));
1792 coff_get_reloc_upper_bound (bfd
*abfd
, sec_ptr asect
)
1794 if (bfd_get_format (abfd
) != bfd_object
)
1796 bfd_set_error (bfd_error_invalid_operation
);
1799 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1803 coff_make_empty_symbol (bfd
*abfd
)
1805 bfd_size_type amt
= sizeof (coff_symbol_type
);
1806 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
1808 if (new_symbol
== NULL
)
1810 new_symbol
->symbol
.section
= 0;
1811 new_symbol
->native
= 0;
1812 new_symbol
->lineno
= NULL
;
1813 new_symbol
->done_lineno
= FALSE
;
1814 new_symbol
->symbol
.the_bfd
= abfd
;
1816 return & new_symbol
->symbol
;
1819 /* Make a debugging symbol. */
1822 coff_bfd_make_debug_symbol (bfd
*abfd
,
1823 void * ptr ATTRIBUTE_UNUSED
,
1824 unsigned long sz ATTRIBUTE_UNUSED
)
1826 bfd_size_type amt
= sizeof (coff_symbol_type
);
1827 coff_symbol_type
*new_symbol
= (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
1829 if (new_symbol
== NULL
)
1831 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1832 (but shouldn't be a constant). */
1833 amt
= sizeof (combined_entry_type
) * 10;
1834 new_symbol
->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
1835 if (!new_symbol
->native
)
1837 new_symbol
->symbol
.section
= bfd_abs_section_ptr
;
1838 new_symbol
->symbol
.flags
= BSF_DEBUGGING
;
1839 new_symbol
->lineno
= NULL
;
1840 new_symbol
->done_lineno
= FALSE
;
1841 new_symbol
->symbol
.the_bfd
= abfd
;
1843 return & new_symbol
->symbol
;
1847 coff_get_symbol_info (bfd
*abfd
, asymbol
*symbol
, symbol_info
*ret
)
1849 bfd_symbol_info (symbol
, ret
);
1851 if (coffsymbol (symbol
)->native
!= NULL
1852 && coffsymbol (symbol
)->native
->fix_value
)
1853 ret
->value
= coffsymbol (symbol
)->native
->u
.syment
.n_value
-
1854 (bfd_hostptr_t
) obj_raw_syments (abfd
);
1857 /* Return the COFF syment for a symbol. */
1860 bfd_coff_get_syment (bfd
*abfd
,
1862 struct internal_syment
*psyment
)
1864 coff_symbol_type
*csym
;
1866 csym
= coff_symbol_from (abfd
, symbol
);
1867 if (csym
== NULL
|| csym
->native
== NULL
)
1869 bfd_set_error (bfd_error_invalid_operation
);
1873 *psyment
= csym
->native
->u
.syment
;
1875 if (csym
->native
->fix_value
)
1876 psyment
->n_value
= psyment
->n_value
-
1877 (bfd_hostptr_t
) obj_raw_syments (abfd
);
1879 /* FIXME: We should handle fix_line here. */
1884 /* Return the COFF auxent for a symbol. */
1887 bfd_coff_get_auxent (bfd
*abfd
,
1890 union internal_auxent
*pauxent
)
1892 coff_symbol_type
*csym
;
1893 combined_entry_type
*ent
;
1895 csym
= coff_symbol_from (abfd
, symbol
);
1898 || csym
->native
== NULL
1899 || indx
>= csym
->native
->u
.syment
.n_numaux
)
1901 bfd_set_error (bfd_error_invalid_operation
);
1905 ent
= csym
->native
+ indx
+ 1;
1907 *pauxent
= ent
->u
.auxent
;
1910 pauxent
->x_sym
.x_tagndx
.l
=
1911 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
1912 - obj_raw_syments (abfd
));
1915 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1916 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1917 - obj_raw_syments (abfd
));
1919 if (ent
->fix_scnlen
)
1920 pauxent
->x_csect
.x_scnlen
.l
=
1921 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
1922 - obj_raw_syments (abfd
));
1927 /* Print out information about COFF symbol. */
1930 coff_print_symbol (bfd
*abfd
,
1933 bfd_print_symbol_type how
)
1935 FILE * file
= (FILE *) filep
;
1939 case bfd_print_symbol_name
:
1940 fprintf (file
, "%s", symbol
->name
);
1943 case bfd_print_symbol_more
:
1944 fprintf (file
, "coff %s %s",
1945 coffsymbol (symbol
)->native
? "n" : "g",
1946 coffsymbol (symbol
)->lineno
? "l" : " ");
1949 case bfd_print_symbol_all
:
1950 if (coffsymbol (symbol
)->native
)
1954 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1955 combined_entry_type
*root
= obj_raw_syments (abfd
);
1956 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1958 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1960 if (! combined
->fix_value
)
1961 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
1963 val
= combined
->u
.syment
.n_value
- (bfd_hostptr_t
) root
;
1965 fprintf (file
, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
1966 combined
->u
.syment
.n_scnum
,
1967 combined
->u
.syment
.n_flags
,
1968 combined
->u
.syment
.n_type
,
1969 combined
->u
.syment
.n_sclass
,
1970 combined
->u
.syment
.n_numaux
);
1971 bfd_fprintf_vma (abfd
, file
, val
);
1972 fprintf (file
, " %s", symbol
->name
);
1974 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1976 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1980 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1982 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1984 fprintf (file
, "\n");
1986 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
1989 switch (combined
->u
.syment
.n_sclass
)
1992 fprintf (file
, "File ");
1996 if (combined
->u
.syment
.n_type
== T_NULL
)
1997 /* Probably a section symbol ? */
1999 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2000 (unsigned long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2001 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2002 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2003 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2004 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2005 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2006 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2007 auxp
->u
.auxent
.x_scn
.x_checksum
,
2008 auxp
->u
.auxent
.x_scn
.x_associated
,
2009 auxp
->u
.auxent
.x_scn
.x_comdat
);
2012 /* Otherwise fall through. */
2015 if (ISFCN (combined
->u
.syment
.n_type
))
2020 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2023 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2024 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2026 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2028 (unsigned long) auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2032 /* Otherwise fall through. */
2034 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2035 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2036 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2039 fprintf (file
, " endndx %ld",
2041 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2049 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2051 while (l
->line_number
)
2053 fprintf (file
, "\n%4d : ", l
->line_number
);
2054 bfd_fprintf_vma (abfd
, file
, l
->u
.offset
+ symbol
->section
->vma
);
2061 bfd_print_symbol_vandf (abfd
, (void *) file
, symbol
);
2062 fprintf (file
, " %-5s %s %s %s",
2063 symbol
->section
->name
,
2064 coffsymbol (symbol
)->native
? "n" : "g",
2065 coffsymbol (symbol
)->lineno
? "l" : " ",
2071 /* Return whether a symbol name implies a local symbol. In COFF,
2072 local symbols generally start with ``.L''. Most targets use this
2073 function for the is_local_label_name entry point, but some may
2077 _bfd_coff_is_local_label_name (bfd
*abfd ATTRIBUTE_UNUSED
,
2080 return name
[0] == '.' && name
[1] == 'L';
2083 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2084 section, calculate and return the name of the source file and the line
2085 nearest to the wanted location. */
2088 coff_find_nearest_line (bfd
*abfd
,
2092 const char **filename_ptr
,
2093 const char **functionname_ptr
,
2094 unsigned int *line_ptr
)
2098 unsigned int line_base
;
2099 coff_data_type
*cof
= coff_data (abfd
);
2100 /* Run through the raw syments if available. */
2101 combined_entry_type
*p
;
2102 combined_entry_type
*pend
;
2104 struct coff_section_tdata
*sec_data
;
2107 /* Before looking through the symbol table, try to use a .stab
2108 section to find the information. */
2109 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2110 &found
, filename_ptr
,
2111 functionname_ptr
, line_ptr
,
2112 &coff_data(abfd
)->line_info
))
2118 /* Also try examining DWARF2 debugging information. */
2119 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
2120 filename_ptr
, functionname_ptr
,
2122 &coff_data(abfd
)->dwarf2_find_line_info
))
2126 *functionname_ptr
= 0;
2129 /* Don't try and find line numbers in a non coff file. */
2130 if (!bfd_family_coff (abfd
))
2136 /* Find the first C_FILE symbol. */
2137 p
= cof
->raw_syments
;
2141 pend
= p
+ cof
->raw_syment_count
;
2144 if (p
->u
.syment
.n_sclass
== C_FILE
)
2146 p
+= 1 + p
->u
.syment
.n_numaux
;
2154 /* Look through the C_FILE symbols to find the best one. */
2155 sec_vma
= bfd_get_section_vma (abfd
, section
);
2156 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2157 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2161 combined_entry_type
*p2
;
2163 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2165 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2167 if (p2
->u
.syment
.n_scnum
> 0
2169 == coff_section_from_bfd_index (abfd
,
2170 p2
->u
.syment
.n_scnum
)))
2172 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2179 file_addr
= (bfd_vma
) p2
->u
.syment
.n_value
;
2180 /* PR 11512: Include the section address of the function name symbol. */
2181 if (p2
->u
.syment
.n_scnum
> 0)
2182 file_addr
+= coff_section_from_bfd_index (abfd
,
2183 p2
->u
.syment
.n_scnum
)->vma
;
2184 /* We use <= MAXDIFF here so that if we get a zero length
2185 file, we actually use the next file entry. */
2187 && offset
+ sec_vma
>= file_addr
2188 && offset
+ sec_vma
- file_addr
<= maxdiff
)
2190 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2191 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2194 /* Avoid endless loops on erroneous files by ensuring that
2195 we always move forward in the file. */
2196 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2199 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2200 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2205 /* Now wander though the raw linenumbers of the section. */
2206 /* If we have been called on this section before, and the offset we
2207 want is further down then we can prime the lookup loop. */
2208 sec_data
= coff_section_data (abfd
, section
);
2209 if (sec_data
!= NULL
2211 && offset
>= sec_data
->offset
)
2214 *functionname_ptr
= sec_data
->function
;
2215 line_base
= sec_data
->line_base
;
2223 if (section
->lineno
!= NULL
)
2225 bfd_vma last_value
= 0;
2227 l
= §ion
->lineno
[i
];
2229 for (; i
< section
->lineno_count
; i
++)
2231 if (l
->line_number
== 0)
2233 /* Get the symbol this line number points at. */
2234 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2235 if (coff
->symbol
.value
> offset
)
2237 *functionname_ptr
= coff
->symbol
.name
;
2238 last_value
= coff
->symbol
.value
;
2241 combined_entry_type
*s
= coff
->native
;
2242 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2244 /* In XCOFF a debugging symbol can follow the
2246 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2247 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2249 /* S should now point to the .bf of the function. */
2250 if (s
->u
.syment
.n_numaux
)
2252 /* The linenumber is stored in the auxent. */
2253 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2254 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2255 *line_ptr
= line_base
;
2261 if (l
->u
.offset
> offset
)
2263 *line_ptr
= l
->line_number
+ line_base
- 1;
2268 /* If we fell off the end of the loop, then assume that this
2269 symbol has no line number info. Otherwise, symbols with no
2270 line number info get reported with the line number of the
2271 last line of the last symbol which does have line number
2272 info. We use 0x100 as a slop to account for cases where the
2273 last line has executable code. */
2274 if (i
>= section
->lineno_count
2276 && offset
- last_value
> 0x100)
2278 *functionname_ptr
= NULL
;
2283 /* Cache the results for the next call. */
2284 if (sec_data
== NULL
&& section
->owner
== abfd
)
2286 amt
= sizeof (struct coff_section_tdata
);
2287 section
->used_by_bfd
= bfd_zalloc (abfd
, amt
);
2288 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2290 if (sec_data
!= NULL
)
2292 sec_data
->offset
= offset
;
2293 sec_data
->i
= i
- 1;
2294 sec_data
->function
= *functionname_ptr
;
2295 sec_data
->line_base
= line_base
;
2302 coff_find_inliner_info (bfd
*abfd
,
2303 const char **filename_ptr
,
2304 const char **functionname_ptr
,
2305 unsigned int *line_ptr
)
2309 found
= _bfd_dwarf2_find_inliner_info (abfd
, filename_ptr
,
2310 functionname_ptr
, line_ptr
,
2311 &coff_data(abfd
)->dwarf2_find_line_info
);
2316 coff_sizeof_headers (bfd
*abfd
, struct bfd_link_info
*info
)
2320 if (!info
->relocatable
)
2321 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2323 size
= bfd_coff_filhsz (abfd
);
2325 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2329 /* Change the class of a coff symbol held by BFD. */
2332 bfd_coff_set_symbol_class (bfd
* abfd
,
2334 unsigned int symbol_class
)
2336 coff_symbol_type
* csym
;
2338 csym
= coff_symbol_from (abfd
, symbol
);
2341 bfd_set_error (bfd_error_invalid_operation
);
2344 else if (csym
->native
== NULL
)
2346 /* This is an alien symbol which no native coff backend data.
2347 We cheat here by creating a fake native entry for it and
2348 then filling in the class. This code is based on that in
2349 coff_write_alien_symbol(). */
2351 combined_entry_type
* native
;
2352 bfd_size_type amt
= sizeof (* native
);
2354 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2358 native
->u
.syment
.n_type
= T_NULL
;
2359 native
->u
.syment
.n_sclass
= symbol_class
;
2361 if (bfd_is_und_section (symbol
->section
))
2363 native
->u
.syment
.n_scnum
= N_UNDEF
;
2364 native
->u
.syment
.n_value
= symbol
->value
;
2366 else if (bfd_is_com_section (symbol
->section
))
2368 native
->u
.syment
.n_scnum
= N_UNDEF
;
2369 native
->u
.syment
.n_value
= symbol
->value
;
2373 native
->u
.syment
.n_scnum
=
2374 symbol
->section
->output_section
->target_index
;
2375 native
->u
.syment
.n_value
= (symbol
->value
2376 + symbol
->section
->output_offset
);
2377 if (! obj_pe (abfd
))
2378 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2380 /* Copy the any flags from the file header into the symbol.
2382 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2385 csym
->native
= native
;
2388 csym
->native
->u
.syment
.n_sclass
= symbol_class
;
2393 struct coff_comdat_info
*
2394 bfd_coff_get_comdat_section (bfd
*abfd
, struct bfd_section
*sec
)
2396 if (bfd_get_flavour (abfd
) == bfd_target_coff_flavour
2397 && coff_section_data (abfd
, sec
) != NULL
)
2398 return coff_section_data (abfd
, sec
)->comdat
;