1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
24 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
26 /* This file contains COFF code that is not dependent on any
27 particular COFF target. There is only one version of this file in
28 libbfd.a, so no target specific code may be put in here. Or, to
31 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
33 If you need to add some target specific behaviour, add a new hook
34 function to bfd_coff_backend_data.
36 Some of these functions are also called by the ECOFF routines.
37 Those functions may not use any COFF specific information, such as
43 #include "coff/internal.h"
46 static void coff_fix_symbol_name
47 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_size_type
*,
48 asection
**, bfd_size_type
*));
49 static bfd_boolean coff_write_symbol
50 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_vma
*,
51 bfd_size_type
*, asection
**, bfd_size_type
*));
52 static bfd_boolean coff_write_alien_symbol
53 PARAMS ((bfd
*, asymbol
*, bfd_vma
*, bfd_size_type
*,
54 asection
**, bfd_size_type
*));
55 static bfd_boolean coff_write_native_symbol
56 PARAMS ((bfd
*, coff_symbol_type
*, bfd_vma
*, bfd_size_type
*,
57 asection
**, bfd_size_type
*));
58 static void coff_pointerize_aux
59 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
60 unsigned int, combined_entry_type
*));
61 static bfd_boolean make_a_section_from_file
62 PARAMS ((bfd
*, struct internal_scnhdr
*, unsigned int));
63 static const bfd_target
*coff_real_object_p
64 PARAMS ((bfd
*, unsigned, struct internal_filehdr
*,
65 struct internal_aouthdr
*));
66 static void fixup_symbol_value
67 PARAMS ((bfd
*, coff_symbol_type
*, struct internal_syment
*));
68 static char *build_debug_section
70 static char *copy_name
71 PARAMS ((bfd
*, char *, size_t));
73 #define STRING_SIZE_SIZE (4)
75 /* Take a section header read from a coff file (in HOST byte order),
76 and make a BFD "section" out of it. This is used by ECOFF. */
78 make_a_section_from_file (abfd
, hdr
, target_index
)
80 struct internal_scnhdr
*hdr
;
81 unsigned int target_index
;
83 asection
*return_section
;
85 bfd_boolean result
= TRUE
;
90 /* Handle long section names as in PE. */
91 if (bfd_coff_long_section_names (abfd
)
92 && hdr
->s_name
[0] == '/')
99 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
100 buf
[SCNNMLEN
- 1] = '\0';
101 strindex
= strtol (buf
, &p
, 10);
102 if (*p
== '\0' && strindex
>= 0)
104 strings
= _bfd_coff_read_string_table (abfd
);
107 /* FIXME: For extra safety, we should make sure that
108 strindex does not run us past the end, but right now we
109 don't know the length of the string table. */
111 name
= bfd_alloc (abfd
, (bfd_size_type
) strlen (strings
) + 1);
114 strcpy (name
, strings
);
120 /* Assorted wastage to null-terminate the name, thanks AT&T! */
121 name
= bfd_alloc (abfd
, (bfd_size_type
) sizeof (hdr
->s_name
) + 1);
124 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
125 name
[sizeof (hdr
->s_name
)] = 0;
128 return_section
= bfd_make_section_anyway (abfd
, name
);
129 if (return_section
== NULL
)
132 return_section
->vma
= hdr
->s_vaddr
;
133 return_section
->lma
= hdr
->s_paddr
;
134 return_section
->_raw_size
= hdr
->s_size
;
135 return_section
->filepos
= hdr
->s_scnptr
;
136 return_section
->rel_filepos
= hdr
->s_relptr
;
137 return_section
->reloc_count
= hdr
->s_nreloc
;
139 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
141 return_section
->line_filepos
= hdr
->s_lnnoptr
;
143 return_section
->lineno_count
= hdr
->s_nlnno
;
144 return_section
->userdata
= NULL
;
145 return_section
->next
= (asection
*) NULL
;
146 return_section
->target_index
= target_index
;
148 if (! bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
, return_section
,
152 return_section
->flags
= flags
;
154 /* At least on i386-coff, the line number count for a shared library
155 section must be ignored. */
156 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
157 return_section
->lineno_count
= 0;
159 if (hdr
->s_nreloc
!= 0)
160 return_section
->flags
|= SEC_RELOC
;
161 /* FIXME: should this check 'hdr->s_size > 0' */
162 if (hdr
->s_scnptr
!= 0)
163 return_section
->flags
|= SEC_HAS_CONTENTS
;
168 /* Read in a COFF object and make it into a BFD. This is used by
171 static const bfd_target
*
172 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
175 struct internal_filehdr
*internal_f
;
176 struct internal_aouthdr
*internal_a
;
178 flagword oflags
= abfd
->flags
;
179 bfd_vma ostart
= bfd_get_start_address (abfd
);
182 bfd_size_type readsize
; /* length of file_info */
184 char *external_sections
;
186 if (!(internal_f
->f_flags
& F_RELFLG
))
187 abfd
->flags
|= HAS_RELOC
;
188 if ((internal_f
->f_flags
& F_EXEC
))
189 abfd
->flags
|= EXEC_P
;
190 if (!(internal_f
->f_flags
& F_LNNO
))
191 abfd
->flags
|= HAS_LINENO
;
192 if (!(internal_f
->f_flags
& F_LSYMS
))
193 abfd
->flags
|= HAS_LOCALS
;
195 /* FIXME: How can we set D_PAGED correctly? */
196 if ((internal_f
->f_flags
& F_EXEC
) != 0)
197 abfd
->flags
|= D_PAGED
;
199 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
200 if (internal_f
->f_nsyms
)
201 abfd
->flags
|= HAS_SYMS
;
203 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
204 bfd_get_start_address (abfd
) = internal_a
->entry
;
206 bfd_get_start_address (abfd
) = 0;
208 /* Set up the tdata area. ECOFF uses its own routine, and overrides
210 tdata_save
= abfd
->tdata
.any
;
211 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
215 scnhsz
= bfd_coff_scnhsz (abfd
);
216 readsize
= (bfd_size_type
) nscns
* scnhsz
;
217 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
218 if (!external_sections
)
221 if (bfd_bread ((PTR
) external_sections
, readsize
, abfd
) != readsize
)
224 /* Set the arch/mach *before* swapping in sections; section header swapping
225 may depend on arch/mach info. */
226 if (! bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
))
229 /* Now copy data as required; construct all asections etc. */
233 for (i
= 0; i
< nscns
; i
++)
235 struct internal_scnhdr tmp
;
236 bfd_coff_swap_scnhdr_in (abfd
,
237 (PTR
) (external_sections
+ i
* scnhsz
),
239 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
244 bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
);
245 /* make_abs_section (abfd); */
250 bfd_release (abfd
, tdata
);
252 abfd
->tdata
.any
= tdata_save
;
253 abfd
->flags
= oflags
;
254 bfd_get_start_address (abfd
) = ostart
;
255 return (const bfd_target
*) NULL
;
258 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
259 not a COFF file. This is also used by ECOFF. */
265 bfd_size_type filhsz
;
266 bfd_size_type aoutsz
;
269 struct internal_filehdr internal_f
;
270 struct internal_aouthdr internal_a
;
272 /* figure out how much to read */
273 filhsz
= bfd_coff_filhsz (abfd
);
274 aoutsz
= bfd_coff_aoutsz (abfd
);
276 filehdr
= bfd_alloc (abfd
, filhsz
);
279 if (bfd_bread (filehdr
, filhsz
, abfd
) != filhsz
)
281 if (bfd_get_error () != bfd_error_system_call
)
282 bfd_set_error (bfd_error_wrong_format
);
283 bfd_release (abfd
, filehdr
);
286 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
287 bfd_release (abfd
, filehdr
);
289 /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
290 (less than aoutsz) used in object files and AOUTSZ (equal to
291 aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
292 expects this header to be aoutsz bytes in length, so we use that
293 value in the call to bfd_alloc below. But we must be careful to
294 only read in f_opthdr bytes in the call to bfd_bread. We should
295 also attempt to catch corrupt or non-COFF binaries with a strange
296 value for f_opthdr. */
297 if (! bfd_coff_bad_format_hook (abfd
, &internal_f
)
298 || internal_f
.f_opthdr
> aoutsz
)
300 bfd_set_error (bfd_error_wrong_format
);
303 nscns
= internal_f
.f_nscns
;
305 if (internal_f
.f_opthdr
)
309 opthdr
= bfd_alloc (abfd
, aoutsz
);
312 if (bfd_bread (opthdr
, (bfd_size_type
) internal_f
.f_opthdr
, abfd
)
313 != internal_f
.f_opthdr
)
315 bfd_release (abfd
, opthdr
);
318 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) &internal_a
);
319 bfd_release (abfd
, opthdr
);
322 return coff_real_object_p (abfd
, nscns
, &internal_f
,
323 (internal_f
.f_opthdr
!= 0
325 : (struct internal_aouthdr
*) NULL
));
328 /* Get the BFD section from a COFF symbol section number. */
331 coff_section_from_bfd_index (abfd
, index
)
335 struct sec
*answer
= abfd
->sections
;
338 return bfd_abs_section_ptr
;
339 if (index
== N_UNDEF
)
340 return bfd_und_section_ptr
;
341 if (index
== N_DEBUG
)
342 return bfd_abs_section_ptr
;
346 if (answer
->target_index
== index
)
348 answer
= answer
->next
;
351 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
352 has a bad symbol table in biglitpow.o. */
353 return bfd_und_section_ptr
;
356 /* Get the upper bound of a COFF symbol table. */
359 coff_get_symtab_upper_bound (abfd
)
362 if (!bfd_coff_slurp_symbol_table (abfd
))
365 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
368 /* Canonicalize a COFF symbol table. */
371 coff_get_symtab (abfd
, alocation
)
375 unsigned int counter
;
376 coff_symbol_type
*symbase
;
377 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
379 if (!bfd_coff_slurp_symbol_table (abfd
))
382 symbase
= obj_symbols (abfd
);
383 counter
= bfd_get_symcount (abfd
);
384 while (counter
-- > 0)
385 *location
++ = symbase
++;
389 return bfd_get_symcount (abfd
);
392 /* Get the name of a symbol. The caller must pass in a buffer of size
396 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
398 const struct internal_syment
*sym
;
401 /* FIXME: It's not clear this will work correctly if sizeof
403 if (sym
->_n
._n_n
._n_zeroes
!= 0
404 || sym
->_n
._n_n
._n_offset
== 0)
406 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
407 buf
[SYMNMLEN
] = '\0';
414 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
415 strings
= obj_coff_strings (abfd
);
418 strings
= _bfd_coff_read_string_table (abfd
);
422 return strings
+ sym
->_n
._n_n
._n_offset
;
426 /* Read in and swap the relocs. This returns a buffer holding the
427 relocs for section SEC in file ABFD. If CACHE is TRUE and
428 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
429 the function is called again. If EXTERNAL_RELOCS is not NULL, it
430 is a buffer large enough to hold the unswapped relocs. If
431 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
432 the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
433 value must be INTERNAL_RELOCS. The function returns NULL on error. */
435 struct internal_reloc
*
436 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
437 require_internal
, internal_relocs
)
441 bfd_byte
*external_relocs
;
442 bfd_boolean require_internal
;
443 struct internal_reloc
*internal_relocs
;
446 bfd_byte
*free_external
= NULL
;
447 struct internal_reloc
*free_internal
= NULL
;
450 struct internal_reloc
*irel
;
453 if (coff_section_data (abfd
, sec
) != NULL
454 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
456 if (! require_internal
)
457 return coff_section_data (abfd
, sec
)->relocs
;
458 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
459 sec
->reloc_count
* sizeof (struct internal_reloc
));
460 return internal_relocs
;
463 relsz
= bfd_coff_relsz (abfd
);
465 amt
= sec
->reloc_count
* relsz
;
466 if (external_relocs
== NULL
)
468 free_external
= (bfd_byte
*) bfd_malloc (amt
);
469 if (free_external
== NULL
&& sec
->reloc_count
> 0)
471 external_relocs
= free_external
;
474 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
475 || bfd_bread (external_relocs
, amt
, abfd
) != amt
)
478 if (internal_relocs
== NULL
)
480 amt
= sec
->reloc_count
;
481 amt
*= sizeof (struct internal_reloc
);
482 free_internal
= (struct internal_reloc
*) bfd_malloc (amt
);
483 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
485 internal_relocs
= free_internal
;
488 /* Swap in the relocs. */
489 erel
= external_relocs
;
490 erel_end
= erel
+ relsz
* sec
->reloc_count
;
491 irel
= internal_relocs
;
492 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
493 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
495 if (free_external
!= NULL
)
497 free (free_external
);
498 free_external
= NULL
;
501 if (cache
&& free_internal
!= NULL
)
503 if (coff_section_data (abfd
, sec
) == NULL
)
505 amt
= sizeof (struct coff_section_tdata
);
506 sec
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
507 if (sec
->used_by_bfd
== NULL
)
509 coff_section_data (abfd
, sec
)->contents
= NULL
;
511 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
514 return internal_relocs
;
517 if (free_external
!= NULL
)
518 free (free_external
);
519 if (free_internal
!= NULL
)
520 free (free_internal
);
524 /* Set lineno_count for the output sections of a COFF file. */
527 coff_count_linenumbers (abfd
)
530 unsigned int limit
= bfd_get_symcount (abfd
);
538 /* This may be from the backend linker, in which case the
539 lineno_count in the sections is correct. */
540 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
541 total
+= s
->lineno_count
;
545 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
546 BFD_ASSERT (s
->lineno_count
== 0);
548 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
550 asymbol
*q_maybe
= *p
;
552 if (bfd_family_coff (bfd_asymbol_bfd (q_maybe
)))
554 coff_symbol_type
*q
= coffsymbol (q_maybe
);
556 /* The AIX 4.1 compiler can sometimes generate line numbers
557 attached to debugging symbols. We try to simply ignore
559 if (q
->lineno
!= NULL
560 && q
->symbol
.section
->owner
!= NULL
)
562 /* This symbol has line numbers. Increment the owning
563 section's linenumber count. */
564 alent
*l
= q
->lineno
;
568 asection
* sec
= q
->symbol
.section
->output_section
;
570 /* Do not try to update fields in read-only sections. */
571 if (! bfd_is_const_section (sec
))
572 sec
->lineno_count
++;
577 while (l
->line_number
!= 0);
585 /* Takes a bfd and a symbol, returns a pointer to the coff specific
586 area of the symbol if there is one. */
589 coff_symbol_from (ignore_abfd
, symbol
)
590 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
593 if (!bfd_family_coff (bfd_asymbol_bfd (symbol
)))
594 return (coff_symbol_type
*) NULL
;
596 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
597 return (coff_symbol_type
*) NULL
;
599 return (coff_symbol_type
*) symbol
;
603 fixup_symbol_value (abfd
, coff_symbol_ptr
, syment
)
605 coff_symbol_type
*coff_symbol_ptr
;
606 struct internal_syment
*syment
;
609 /* Normalize the symbol flags */
610 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
612 /* a common symbol is undefined with a value */
613 syment
->n_scnum
= N_UNDEF
;
614 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
616 else if ((coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) != 0
617 && (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING_RELOC
) == 0)
619 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
621 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
623 syment
->n_scnum
= N_UNDEF
;
626 /* FIXME: Do we need to handle the absolute section here? */
629 if (coff_symbol_ptr
->symbol
.section
)
632 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
634 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
635 + coff_symbol_ptr
->symbol
.section
->output_offset
);
638 syment
->n_value
+= (syment
->n_sclass
== C_STATLAB
)
639 ? coff_symbol_ptr
->symbol
.section
->output_section
->lma
640 : coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
646 /* This can happen, but I don't know why yet (steve@cygnus.com) */
647 syment
->n_scnum
= N_ABS
;
648 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
653 /* Run through all the symbols in the symbol table and work out what
654 their indexes into the symbol table will be when output.
656 Coff requires that each C_FILE symbol points to the next one in the
657 chain, and that the last one points to the first external symbol. We
661 coff_renumber_symbols (bfd_ptr
, first_undef
)
665 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
666 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
667 unsigned int native_index
= 0;
668 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
669 unsigned int symbol_index
;
671 /* COFF demands that undefined symbols come after all other symbols.
672 Since we don't need to impose this extra knowledge on all our
673 client programs, deal with that here. Sort the symbol table;
674 just move the undefined symbols to the end, leaving the rest
675 alone. The O'Reilly book says that defined global symbols come
676 at the end before the undefined symbols, so we do that here as
678 /* @@ Do we have some condition we could test for, so we don't always
679 have to do this? I don't think relocatability is quite right, but
680 I'm not certain. [raeburn:19920508.1711EST] */
686 amt
= sizeof (asymbol
*) * ((bfd_size_type
) symbol_count
+ 1);
687 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
, amt
);
690 bfd_ptr
->outsymbols
= newsyms
;
691 for (i
= 0; i
< symbol_count
; i
++)
692 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
693 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
694 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
695 && ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) != 0
696 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
698 *newsyms
++ = symbol_ptr_ptr
[i
];
700 for (i
= 0; i
< symbol_count
; i
++)
701 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
702 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
703 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
704 || ((symbol_ptr_ptr
[i
]->flags
& BSF_FUNCTION
) == 0
705 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_WEAK
))
707 *newsyms
++ = symbol_ptr_ptr
[i
];
709 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
711 for (i
= 0; i
< symbol_count
; i
++)
712 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
713 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
714 *newsyms
++ = symbol_ptr_ptr
[i
];
715 *newsyms
= (asymbol
*) NULL
;
716 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
719 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
721 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
722 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
723 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
725 combined_entry_type
*s
= coff_symbol_ptr
->native
;
728 if (s
->u
.syment
.n_sclass
== C_FILE
)
730 if (last_file
!= (struct internal_syment
*) NULL
)
731 last_file
->n_value
= native_index
;
732 last_file
= &(s
->u
.syment
);
737 /* Modify the symbol values according to their section and
740 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
742 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
743 s
[i
].offset
= native_index
++;
750 obj_conv_table_size (bfd_ptr
) = native_index
;
755 /* Run thorough the symbol table again, and fix it so that all
756 pointers to entries are changed to the entries' index in the output
760 coff_mangle_symbols (bfd_ptr
)
763 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
764 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
765 unsigned int symbol_index
;
767 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
769 coff_symbol_type
*coff_symbol_ptr
=
770 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
772 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
775 combined_entry_type
*s
= coff_symbol_ptr
->native
;
779 /* FIXME: We should use a union here. */
780 s
->u
.syment
.n_value
=
781 (bfd_vma
)((combined_entry_type
*)
782 ((unsigned long) s
->u
.syment
.n_value
))->offset
;
787 /* The value is the offset into the line number entries
788 for the symbol's section. On output, the symbol's
789 section should be N_DEBUG. */
790 s
->u
.syment
.n_value
=
791 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
792 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
793 coff_symbol_ptr
->symbol
.section
=
794 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
795 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
797 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
799 combined_entry_type
*a
= s
+ i
+ 1;
802 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
803 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
808 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
809 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
814 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
815 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
824 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
825 debug_string_section_p
, debug_string_size_p
)
828 combined_entry_type
*native
;
829 bfd_size_type
*string_size_p
;
830 asection
**debug_string_section_p
;
831 bfd_size_type
*debug_string_size_p
;
833 unsigned int name_length
;
834 union internal_auxent
*auxent
;
835 char *name
= (char *) (symbol
->name
);
837 if (name
== (char *) NULL
)
839 /* coff symbols always have names, so we'll make one up */
840 symbol
->name
= "strange";
841 name
= (char *) symbol
->name
;
843 name_length
= strlen (name
);
845 if (native
->u
.syment
.n_sclass
== C_FILE
846 && native
->u
.syment
.n_numaux
> 0)
848 unsigned int filnmlen
;
850 if (bfd_coff_force_symnames_in_strings (abfd
))
852 native
->u
.syment
._n
._n_n
._n_offset
=
853 (*string_size_p
+ STRING_SIZE_SIZE
);
854 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
855 *string_size_p
+= 6; /* strlen(".file") + 1 */
858 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
860 auxent
= &(native
+ 1)->u
.auxent
;
862 filnmlen
= bfd_coff_filnmlen (abfd
);
864 if (bfd_coff_long_filenames (abfd
))
866 if (name_length
<= filnmlen
)
868 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
872 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
873 auxent
->x_file
.x_n
.x_zeroes
= 0;
874 *string_size_p
+= name_length
+ 1;
879 strncpy (auxent
->x_file
.x_fname
, name
, filnmlen
);
880 if (name_length
> filnmlen
)
881 name
[filnmlen
] = '\0';
886 if (name_length
<= SYMNMLEN
&& !bfd_coff_force_symnames_in_strings (abfd
))
888 /* This name will fit into the symbol neatly */
889 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
891 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
893 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
895 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
896 *string_size_p
+= name_length
+ 1;
902 int prefix_len
= bfd_coff_debug_string_prefix_length (abfd
);
904 /* This name should be written into the .debug section. For
905 some reason each name is preceded by a two byte length
906 and also followed by a null byte. FIXME: We assume that
907 the .debug section has already been created, and that it
909 if (*debug_string_section_p
== (asection
*) NULL
)
910 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
911 filepos
= bfd_tell (abfd
);
913 bfd_put_32 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
915 bfd_put_16 (abfd
, (bfd_vma
) (name_length
+ 1), buf
);
917 if (!bfd_set_section_contents (abfd
,
918 *debug_string_section_p
,
920 (file_ptr
) *debug_string_size_p
,
921 (bfd_size_type
) prefix_len
)
922 || !bfd_set_section_contents (abfd
,
923 *debug_string_section_p
,
925 (file_ptr
) (*debug_string_size_p
927 (bfd_size_type
) name_length
+ 1))
929 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
931 native
->u
.syment
._n
._n_n
._n_offset
=
932 *debug_string_size_p
+ prefix_len
;
933 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
934 *debug_string_size_p
+= name_length
+ 1 + prefix_len
;
939 /* We need to keep track of the symbol index so that when we write out
940 the relocs we can get the index for a symbol. This method is a
943 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
945 /* Write a symbol out to a COFF file. */
948 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
949 debug_string_section_p
, debug_string_size_p
)
952 combined_entry_type
*native
;
954 bfd_size_type
*string_size_p
;
955 asection
**debug_string_section_p
;
956 bfd_size_type
*debug_string_size_p
;
958 unsigned int numaux
= native
->u
.syment
.n_numaux
;
959 int type
= native
->u
.syment
.n_type
;
960 int class = native
->u
.syment
.n_sclass
;
962 bfd_size_type symesz
;
964 if (native
->u
.syment
.n_sclass
== C_FILE
)
965 symbol
->flags
|= BSF_DEBUGGING
;
967 if (symbol
->flags
& BSF_DEBUGGING
968 && bfd_is_abs_section (symbol
->section
))
970 native
->u
.syment
.n_scnum
= N_DEBUG
;
972 else if (bfd_is_abs_section (symbol
->section
))
974 native
->u
.syment
.n_scnum
= N_ABS
;
976 else if (bfd_is_und_section (symbol
->section
))
978 native
->u
.syment
.n_scnum
= N_UNDEF
;
982 native
->u
.syment
.n_scnum
=
983 symbol
->section
->output_section
->target_index
;
986 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
987 debug_string_section_p
, debug_string_size_p
);
989 symesz
= bfd_coff_symesz (abfd
);
990 buf
= bfd_alloc (abfd
, symesz
);
993 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
994 if (bfd_bwrite (buf
, symesz
, abfd
) != symesz
)
996 bfd_release (abfd
, buf
);
998 if (native
->u
.syment
.n_numaux
> 0)
1000 bfd_size_type auxesz
;
1003 auxesz
= bfd_coff_auxesz (abfd
);
1004 buf
= bfd_alloc (abfd
, auxesz
);
1007 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
1009 bfd_coff_swap_aux_out (abfd
,
1010 &((native
+ j
+ 1)->u
.auxent
),
1014 native
->u
.syment
.n_numaux
,
1016 if (bfd_bwrite (buf
, auxesz
, abfd
) != auxesz
)
1019 bfd_release (abfd
, buf
);
1022 /* Store the index for use when we write out the relocs. */
1023 set_index (symbol
, *written
);
1025 *written
+= numaux
+ 1;
1029 /* Write out a symbol to a COFF file that does not come from a COFF
1030 file originally. This symbol may have been created by the linker,
1031 or we may be linking a non COFF file to a COFF file. */
1034 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
1035 debug_string_section_p
, debug_string_size_p
)
1039 bfd_size_type
*string_size_p
;
1040 asection
**debug_string_section_p
;
1041 bfd_size_type
*debug_string_size_p
;
1043 combined_entry_type
*native
;
1044 combined_entry_type dummy
;
1047 native
->u
.syment
.n_type
= T_NULL
;
1048 native
->u
.syment
.n_flags
= 0;
1049 if (bfd_is_und_section (symbol
->section
))
1051 native
->u
.syment
.n_scnum
= N_UNDEF
;
1052 native
->u
.syment
.n_value
= symbol
->value
;
1054 else if (bfd_is_com_section (symbol
->section
))
1056 native
->u
.syment
.n_scnum
= N_UNDEF
;
1057 native
->u
.syment
.n_value
= symbol
->value
;
1059 else if (symbol
->flags
& BSF_DEBUGGING
)
1061 /* There isn't much point to writing out a debugging symbol
1062 unless we are prepared to convert it into COFF debugging
1063 format. So, we just ignore them. We must clobber the symbol
1064 name to keep it from being put in the string table. */
1070 native
->u
.syment
.n_scnum
=
1071 symbol
->section
->output_section
->target_index
;
1072 native
->u
.syment
.n_value
= (symbol
->value
1073 + symbol
->section
->output_offset
);
1074 if (! obj_pe (abfd
))
1075 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
1077 /* Copy the any flags from the file header into the symbol.
1080 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1081 if (c
!= (coff_symbol_type
*) NULL
)
1082 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1086 native
->u
.syment
.n_type
= 0;
1087 if (symbol
->flags
& BSF_LOCAL
)
1088 native
->u
.syment
.n_sclass
= C_STAT
;
1089 else if (symbol
->flags
& BSF_WEAK
)
1090 native
->u
.syment
.n_sclass
= obj_pe (abfd
) ? C_NT_WEAK
: C_WEAKEXT
;
1092 native
->u
.syment
.n_sclass
= C_EXT
;
1093 native
->u
.syment
.n_numaux
= 0;
1095 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1096 debug_string_section_p
, debug_string_size_p
);
1099 /* Write a native symbol to a COFF file. */
1102 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1103 debug_string_section_p
, debug_string_size_p
)
1105 coff_symbol_type
*symbol
;
1107 bfd_size_type
*string_size_p
;
1108 asection
**debug_string_section_p
;
1109 bfd_size_type
*debug_string_size_p
;
1111 combined_entry_type
*native
= symbol
->native
;
1112 alent
*lineno
= symbol
->lineno
;
1114 /* If this symbol has an associated line number, we must store the
1115 symbol index in the line number field. We also tag the auxent to
1116 point to the right place in the lineno table. */
1117 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1119 unsigned int count
= 0;
1120 lineno
[count
].u
.offset
= *written
;
1121 if (native
->u
.syment
.n_numaux
)
1123 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1125 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1126 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1129 /* Count and relocate all other linenumbers. */
1131 while (lineno
[count
].line_number
!= 0)
1135 I've been told this, but still need proof:
1136 > The second bug is also in `bfd/coffcode.h'. This bug
1137 > causes the linker to screw up the pc-relocations for
1138 > all the line numbers in COFF code. This bug isn't only
1139 > specific to A29K implementations, but affects all
1140 > systems using COFF format binaries. Note that in COFF
1141 > object files, the line number core offsets output by
1142 > the assembler are relative to the start of each
1143 > procedure, not to the start of the .text section. This
1144 > patch relocates the line numbers relative to the
1145 > `native->u.syment.n_value' instead of the section
1147 > modular!olson@cs.arizona.edu (Jon Olson)
1149 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1151 lineno
[count
].u
.offset
+=
1152 (symbol
->symbol
.section
->output_section
->vma
1153 + symbol
->symbol
.section
->output_offset
);
1157 symbol
->done_lineno
= TRUE
;
1159 if (! bfd_is_const_section (symbol
->symbol
.section
->output_section
))
1160 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1161 count
* bfd_coff_linesz (abfd
);
1164 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1165 string_size_p
, debug_string_section_p
,
1166 debug_string_size_p
);
1169 /* Write out the COFF symbols. */
1172 coff_write_symbols (abfd
)
1175 bfd_size_type string_size
;
1176 asection
*debug_string_section
;
1177 bfd_size_type debug_string_size
;
1179 unsigned int limit
= bfd_get_symcount (abfd
);
1180 bfd_signed_vma written
= 0;
1184 debug_string_section
= NULL
;
1185 debug_string_size
= 0;
1187 /* If this target supports long section names, they must be put into
1188 the string table. This is supported by PE. This code must
1189 handle section names just as they are handled in
1190 coff_write_object_contents. */
1191 if (bfd_coff_long_section_names (abfd
))
1195 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1199 len
= strlen (o
->name
);
1201 string_size
+= len
+ 1;
1205 /* Seek to the right place */
1206 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1209 /* Output all the symbols we have */
1212 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1214 asymbol
*symbol
= *p
;
1215 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1217 if (c_symbol
== (coff_symbol_type
*) NULL
1218 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1220 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1221 &debug_string_section
,
1222 &debug_string_size
))
1227 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1228 &string_size
, &debug_string_section
,
1229 &debug_string_size
))
1234 obj_raw_syment_count (abfd
) = written
;
1236 /* Now write out strings */
1238 if (string_size
!= 0)
1240 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1241 bfd_byte buffer
[STRING_SIZE_SIZE
];
1243 #if STRING_SIZE_SIZE == 4
1244 H_PUT_32 (abfd
, size
, buffer
);
1246 #error Change H_PUT_32
1248 if (bfd_bwrite ((PTR
) buffer
, (bfd_size_type
) sizeof (buffer
), abfd
)
1252 /* Handle long section names. This code must handle section
1253 names just as they are handled in coff_write_object_contents. */
1254 if (bfd_coff_long_section_names (abfd
))
1258 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1262 len
= strlen (o
->name
);
1265 if (bfd_bwrite (o
->name
, (bfd_size_type
) (len
+ 1), abfd
)
1272 for (p
= abfd
->outsymbols
, i
= 0;
1277 size_t name_length
= strlen (q
->name
);
1278 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1281 /* Figure out whether the symbol name should go in the string
1282 table. Symbol names that are short enough are stored
1283 directly in the syment structure. File names permit a
1284 different, longer, length in the syment structure. On
1285 XCOFF, some symbol names are stored in the .debug section
1286 rather than in the string table. */
1288 if (c_symbol
== NULL
1289 || c_symbol
->native
== NULL
)
1291 /* This is not a COFF symbol, so it certainly is not a
1292 file name, nor does it go in the .debug section. */
1293 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1295 else if (bfd_coff_symname_in_debug (abfd
,
1296 &c_symbol
->native
->u
.syment
))
1298 /* This symbol name is in the XCOFF .debug section.
1299 Don't write it into the string table. */
1300 maxlen
= name_length
;
1302 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1303 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1305 if (bfd_coff_force_symnames_in_strings (abfd
))
1307 if (bfd_bwrite (".file", (bfd_size_type
) 6, abfd
) != 6)
1310 maxlen
= bfd_coff_filnmlen (abfd
);
1313 maxlen
= bfd_coff_force_symnames_in_strings (abfd
) ? 0 : SYMNMLEN
;
1315 if (name_length
> maxlen
)
1317 if (bfd_bwrite ((PTR
) (q
->name
), (bfd_size_type
) name_length
+ 1,
1318 abfd
) != name_length
+ 1)
1325 /* We would normally not write anything here, but we'll write
1326 out 4 so that any stupid coff reader which tries to read the
1327 string table even when there isn't one won't croak. */
1328 unsigned int size
= STRING_SIZE_SIZE
;
1329 bfd_byte buffer
[STRING_SIZE_SIZE
];
1331 #if STRING_SIZE_SIZE == 4
1332 H_PUT_32 (abfd
, size
, buffer
);
1334 #error Change H_PUT_32
1336 if (bfd_bwrite ((PTR
) buffer
, (bfd_size_type
) STRING_SIZE_SIZE
, abfd
)
1337 != STRING_SIZE_SIZE
)
1341 /* Make sure the .debug section was created to be the correct size.
1342 We should create it ourselves on the fly, but we don't because
1343 BFD won't let us write to any section until we know how large all
1344 the sections are. We could still do it by making another pass
1345 over the symbols. FIXME. */
1346 BFD_ASSERT (debug_string_size
== 0
1347 || (debug_string_section
!= (asection
*) NULL
1348 && (BFD_ALIGN (debug_string_size
,
1349 1 << debug_string_section
->alignment_power
)
1350 == bfd_section_size (abfd
, debug_string_section
))));
1356 coff_write_linenumbers (abfd
)
1360 bfd_size_type linesz
;
1363 linesz
= bfd_coff_linesz (abfd
);
1364 buff
= bfd_alloc (abfd
, linesz
);
1367 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1369 if (s
->lineno_count
)
1371 asymbol
**q
= abfd
->outsymbols
;
1372 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1374 /* Find all the linenumbers in this section */
1378 if (p
->section
->output_section
== s
)
1381 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1382 (bfd_asymbol_bfd (p
), p
));
1385 /* Found a linenumber entry, output */
1386 struct internal_lineno out
;
1387 memset ((PTR
) & out
, 0, sizeof (out
));
1389 out
.l_addr
.l_symndx
= l
->u
.offset
;
1390 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1391 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1395 while (l
->line_number
)
1397 out
.l_lnno
= l
->line_number
;
1398 out
.l_addr
.l_symndx
= l
->u
.offset
;
1399 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1400 if (bfd_bwrite (buff
, (bfd_size_type
) linesz
, abfd
)
1411 bfd_release (abfd
, buff
);
1416 coff_get_lineno (ignore_abfd
, symbol
)
1417 bfd
*ignore_abfd ATTRIBUTE_UNUSED
;
1420 return coffsymbol (symbol
)->lineno
;
1425 /* This is only called from coff_add_missing_symbols, which has been
1429 coff_section_symbol (abfd
, name
)
1433 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1435 combined_entry_type
*csym
;
1438 csym
= coff_symbol_from (abfd
, sym
)->native
;
1439 /* Make sure back-end COFF stuff is there. */
1444 coff_symbol_type sym
;
1445 /* @@FIXME This shouldn't use a fixed size!! */
1446 combined_entry_type e
[10];
1450 f
= (struct foo
*) bfd_zalloc (abfd
, (bfd_size_type
) sizeof (*f
));
1453 bfd_set_error (bfd_error_no_error
);
1456 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1458 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1459 csym
[0].u
.syment
.n_numaux
= 1;
1460 /* SF_SET_STATICS (sym); @@ ??? */
1461 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1462 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1463 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1465 if (sec
->output_section
== NULL
)
1467 sec
->output_section
= sec
;
1468 sec
->output_offset
= 0;
1476 /* This function transforms the offsets into the symbol table into
1477 pointers to syments. */
1480 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1482 combined_entry_type
*table_base
;
1483 combined_entry_type
*symbol
;
1484 unsigned int indaux
;
1485 combined_entry_type
*auxent
;
1487 unsigned int type
= symbol
->u
.syment
.n_type
;
1488 unsigned int class = symbol
->u
.syment
.n_sclass
;
1490 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1492 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1493 (abfd
, table_base
, symbol
, indaux
, auxent
))
1497 /* Don't bother if this is a file or a section */
1498 if (class == C_STAT
&& type
== T_NULL
)
1500 if (class == C_FILE
)
1503 /* Otherwise patch up */
1504 #define N_TMASK coff_data (abfd)->local_n_tmask
1505 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1506 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
|| class == C_FCN
)
1507 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1509 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1510 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1511 auxent
->fix_end
= 1;
1513 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1514 generate one, so we must be careful to ignore it. */
1515 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1517 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1518 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1519 auxent
->fix_tag
= 1;
1523 /* Allocate space for the ".debug" section, and read it.
1524 We did not read the debug section until now, because
1525 we didn't want to go to the trouble until someone needed it. */
1528 build_debug_section (abfd
)
1531 char *debug_section
;
1533 bfd_size_type sec_size
;
1535 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1539 bfd_set_error (bfd_error_no_debug_section
);
1543 sec_size
= bfd_get_section_size_before_reloc (sect
);
1544 debug_section
= (PTR
) bfd_alloc (abfd
, sec_size
);
1545 if (debug_section
== NULL
)
1548 /* Seek to the beginning of the `.debug' section and read it.
1549 Save the current position first; it is needed by our caller.
1550 Then read debug section and reset the file pointer. */
1552 position
= bfd_tell (abfd
);
1553 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1554 || bfd_bread (debug_section
, sec_size
, abfd
) != sec_size
1555 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1557 return debug_section
;
1560 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1561 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1562 be \0-terminated. */
1564 copy_name (abfd
, name
, maxlen
)
1572 for (len
= 0; len
< maxlen
; ++len
)
1574 if (name
[len
] == '\0')
1580 if ((newname
= (PTR
) bfd_alloc (abfd
, (bfd_size_type
) len
+ 1)) == NULL
)
1582 strncpy (newname
, name
, len
);
1583 newname
[len
] = '\0';
1587 /* Read in the external symbols. */
1590 _bfd_coff_get_external_symbols (abfd
)
1593 bfd_size_type symesz
;
1597 if (obj_coff_external_syms (abfd
) != NULL
)
1600 symesz
= bfd_coff_symesz (abfd
);
1602 size
= obj_raw_syment_count (abfd
) * symesz
;
1604 syms
= (PTR
) bfd_malloc (size
);
1605 if (syms
== NULL
&& size
!= 0)
1608 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1609 || bfd_bread (syms
, size
, abfd
) != size
)
1616 obj_coff_external_syms (abfd
) = syms
;
1621 /* Read in the external strings. The strings are not loaded until
1622 they are needed. This is because we have no simple way of
1623 detecting a missing string table in an archive. */
1626 _bfd_coff_read_string_table (abfd
)
1629 char extstrsize
[STRING_SIZE_SIZE
];
1630 bfd_size_type strsize
;
1634 if (obj_coff_strings (abfd
) != NULL
)
1635 return obj_coff_strings (abfd
);
1637 if (obj_sym_filepos (abfd
) == 0)
1639 bfd_set_error (bfd_error_no_symbols
);
1643 pos
= obj_sym_filepos (abfd
);
1644 pos
+= obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
);
1645 if (bfd_seek (abfd
, pos
, SEEK_SET
) != 0)
1648 if (bfd_bread (extstrsize
, (bfd_size_type
) sizeof extstrsize
, abfd
)
1649 != sizeof extstrsize
)
1651 if (bfd_get_error () != bfd_error_file_truncated
)
1654 /* There is no string table. */
1655 strsize
= STRING_SIZE_SIZE
;
1659 #if STRING_SIZE_SIZE == 4
1660 strsize
= H_GET_32 (abfd
, extstrsize
);
1662 #error Change H_GET_32
1666 if (strsize
< STRING_SIZE_SIZE
)
1668 (*_bfd_error_handler
)
1669 (_("%s: bad string table size %lu"), bfd_archive_filename (abfd
),
1670 (unsigned long) strsize
);
1671 bfd_set_error (bfd_error_bad_value
);
1675 strings
= (char *) bfd_malloc (strsize
);
1676 if (strings
== NULL
)
1679 if (bfd_bread (strings
+ STRING_SIZE_SIZE
, strsize
- STRING_SIZE_SIZE
, abfd
)
1680 != strsize
- STRING_SIZE_SIZE
)
1686 obj_coff_strings (abfd
) = strings
;
1691 /* Free up the external symbols and strings read from a COFF file. */
1694 _bfd_coff_free_symbols (abfd
)
1697 if (obj_coff_external_syms (abfd
) != NULL
1698 && ! obj_coff_keep_syms (abfd
))
1700 free (obj_coff_external_syms (abfd
));
1701 obj_coff_external_syms (abfd
) = NULL
;
1703 if (obj_coff_strings (abfd
) != NULL
1704 && ! obj_coff_keep_strings (abfd
))
1706 free (obj_coff_strings (abfd
));
1707 obj_coff_strings (abfd
) = NULL
;
1712 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1713 knit the symbol names into a normalized form. By normalized here I
1714 mean that all symbols have an n_offset pointer that points to a null-
1715 terminated string. */
1717 combined_entry_type
*
1718 coff_get_normalized_symtab (abfd
)
1721 combined_entry_type
*internal
;
1722 combined_entry_type
*internal_ptr
;
1723 combined_entry_type
*symbol_ptr
;
1724 combined_entry_type
*internal_end
;
1728 const char *string_table
= NULL
;
1729 char *debug_section
= NULL
;
1732 if (obj_raw_syments (abfd
) != NULL
)
1733 return obj_raw_syments (abfd
);
1735 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1736 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1737 if (internal
== NULL
&& size
!= 0)
1739 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1741 if (! _bfd_coff_get_external_symbols (abfd
))
1744 raw_src
= (char *) obj_coff_external_syms (abfd
);
1746 /* mark the end of the symbols */
1747 symesz
= bfd_coff_symesz (abfd
);
1748 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1750 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1751 probably possible. If one shows up, it will probably kill us. */
1753 /* Swap all the raw entries */
1754 for (internal_ptr
= internal
;
1756 raw_src
+= symesz
, internal_ptr
++)
1760 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1761 (PTR
) & internal_ptr
->u
.syment
);
1762 symbol_ptr
= internal_ptr
;
1765 i
< symbol_ptr
->u
.syment
.n_numaux
;
1770 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1771 symbol_ptr
->u
.syment
.n_type
,
1772 symbol_ptr
->u
.syment
.n_sclass
,
1773 (int) i
, symbol_ptr
->u
.syment
.n_numaux
,
1774 &(internal_ptr
->u
.auxent
));
1775 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1780 /* Free the raw symbols, but not the strings (if we have them). */
1781 obj_coff_keep_strings (abfd
) = TRUE
;
1782 if (! _bfd_coff_free_symbols (abfd
))
1785 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1788 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1789 && internal_ptr
->u
.syment
.n_numaux
> 0)
1791 /* make a file symbol point to the name in the auxent, since
1792 the text ".file" is redundant */
1793 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1795 /* the filename is a long one, point into the string table */
1796 if (string_table
== NULL
)
1798 string_table
= _bfd_coff_read_string_table (abfd
);
1799 if (string_table
== NULL
)
1803 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1806 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1810 /* Ordinary short filename, put into memory anyway. The
1811 Microsoft PE tools sometimes store a filename in
1812 multiple AUX entries. */
1813 if (internal_ptr
->u
.syment
.n_numaux
> 1
1814 && coff_data (abfd
)->pe
)
1816 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1819 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1820 internal_ptr
->u
.syment
.n_numaux
* symesz
));
1824 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1827 (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1828 (size_t) bfd_coff_filnmlen (abfd
)));
1834 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1836 /* This is a "short" name. Make it long. */
1840 /* find the length of this string without walking into memory
1842 for (i
= 0; i
< 8; ++i
)
1843 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1846 newstring
= (PTR
) bfd_zalloc (abfd
, (bfd_size_type
) (i
+ 1));
1847 if (newstring
== NULL
)
1849 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
);
1850 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1851 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1853 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1854 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1855 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1857 /* Long name already. Point symbol at the string in the
1859 if (string_table
== NULL
)
1861 string_table
= _bfd_coff_read_string_table (abfd
);
1862 if (string_table
== NULL
)
1865 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1868 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1872 /* Long name in debug section. Very similar. */
1873 if (debug_section
== NULL
)
1874 debug_section
= build_debug_section (abfd
);
1875 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1876 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1879 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1882 obj_raw_syments (abfd
) = internal
;
1883 BFD_ASSERT (obj_raw_syment_count (abfd
)
1884 == (unsigned int) (internal_ptr
- internal
));
1887 } /* coff_get_normalized_symtab() */
1890 coff_get_reloc_upper_bound (abfd
, asect
)
1894 if (bfd_get_format (abfd
) != bfd_object
)
1896 bfd_set_error (bfd_error_invalid_operation
);
1899 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1903 coff_make_empty_symbol (abfd
)
1906 bfd_size_type amt
= sizeof (coff_symbol_type
);
1907 coff_symbol_type
*new = (coff_symbol_type
*) bfd_zalloc (abfd
, amt
);
1910 new->symbol
.section
= 0;
1912 new->lineno
= (alent
*) NULL
;
1913 new->done_lineno
= FALSE
;
1914 new->symbol
.the_bfd
= abfd
;
1915 return &new->symbol
;
1918 /* Make a debugging symbol. */
1921 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1923 PTR ptr ATTRIBUTE_UNUSED
;
1924 unsigned long sz ATTRIBUTE_UNUSED
;
1926 bfd_size_type amt
= sizeof (coff_symbol_type
);
1927 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, amt
);
1930 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1931 (but shouldn't be a constant). */
1932 amt
= sizeof (combined_entry_type
) * 10;
1933 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
1936 new->symbol
.section
= bfd_abs_section_ptr
;
1937 new->symbol
.flags
= BSF_DEBUGGING
;
1938 new->lineno
= (alent
*) NULL
;
1939 new->done_lineno
= FALSE
;
1940 new->symbol
.the_bfd
= abfd
;
1941 return &new->symbol
;
1945 coff_get_symbol_info (abfd
, symbol
, ret
)
1950 bfd_symbol_info (symbol
, ret
);
1951 if (coffsymbol (symbol
)->native
!= NULL
1952 && coffsymbol (symbol
)->native
->fix_value
)
1954 ret
->value
= coffsymbol (symbol
)->native
->u
.syment
.n_value
-
1955 (unsigned long) obj_raw_syments (abfd
);
1959 /* Return the COFF syment for a symbol. */
1962 bfd_coff_get_syment (abfd
, symbol
, psyment
)
1965 struct internal_syment
*psyment
;
1967 coff_symbol_type
*csym
;
1969 csym
= coff_symbol_from (abfd
, symbol
);
1970 if (csym
== NULL
|| csym
->native
== NULL
)
1972 bfd_set_error (bfd_error_invalid_operation
);
1976 *psyment
= csym
->native
->u
.syment
;
1978 if (csym
->native
->fix_value
)
1979 psyment
->n_value
= psyment
->n_value
-
1980 (unsigned long) obj_raw_syments (abfd
);
1982 /* FIXME: We should handle fix_line here. */
1987 /* Return the COFF auxent for a symbol. */
1990 bfd_coff_get_auxent (abfd
, symbol
, indx
, pauxent
)
1994 union internal_auxent
*pauxent
;
1996 coff_symbol_type
*csym
;
1997 combined_entry_type
*ent
;
1999 csym
= coff_symbol_from (abfd
, symbol
);
2002 || csym
->native
== NULL
2003 || indx
>= csym
->native
->u
.syment
.n_numaux
)
2005 bfd_set_error (bfd_error_invalid_operation
);
2009 ent
= csym
->native
+ indx
+ 1;
2011 *pauxent
= ent
->u
.auxent
;
2014 pauxent
->x_sym
.x_tagndx
.l
=
2015 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
2016 - obj_raw_syments (abfd
));
2019 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
2020 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2021 - obj_raw_syments (abfd
));
2023 if (ent
->fix_scnlen
)
2024 pauxent
->x_csect
.x_scnlen
.l
=
2025 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
2026 - obj_raw_syments (abfd
));
2031 /* Print out information about COFF symbol. */
2034 coff_print_symbol (abfd
, filep
, symbol
, how
)
2038 bfd_print_symbol_type how
;
2040 FILE *file
= (FILE *) filep
;
2044 case bfd_print_symbol_name
:
2045 fprintf (file
, "%s", symbol
->name
);
2048 case bfd_print_symbol_more
:
2049 fprintf (file
, "coff %s %s",
2050 coffsymbol (symbol
)->native
? "n" : "g",
2051 coffsymbol (symbol
)->lineno
? "l" : " ");
2054 case bfd_print_symbol_all
:
2055 if (coffsymbol (symbol
)->native
)
2059 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
2060 combined_entry_type
*root
= obj_raw_syments (abfd
);
2061 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
2063 fprintf (file
, "[%3ld]", (long) (combined
- root
));
2065 if (! combined
->fix_value
)
2066 val
= (bfd_vma
) combined
->u
.syment
.n_value
;
2068 val
= combined
->u
.syment
.n_value
- (unsigned long) root
;
2072 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
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 (unsigned long) val
,
2081 /* Print out the wide, 64 bit, symbol value */
2083 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%016llx %s",
2084 combined
->u
.syment
.n_scnum
,
2085 combined
->u
.syment
.n_flags
,
2086 combined
->u
.syment
.n_type
,
2087 combined
->u
.syment
.n_sclass
,
2088 combined
->u
.syment
.n_numaux
,
2093 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2095 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2099 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2101 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2103 fprintf (file
, "\n");
2105 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2108 switch (combined
->u
.syment
.n_sclass
)
2111 fprintf (file
, "File ");
2115 if (combined
->u
.syment
.n_type
== T_NULL
)
2116 /* probably a section symbol? */
2118 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2119 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2120 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2121 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2122 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2123 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2124 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2125 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2126 auxp
->u
.auxent
.x_scn
.x_checksum
,
2127 auxp
->u
.auxent
.x_scn
.x_associated
,
2128 auxp
->u
.auxent
.x_scn
.x_comdat
);
2131 /* else fall through */
2133 if (ISFCN (combined
->u
.syment
.n_type
))
2138 next
= (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2141 next
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
2142 llnos
= auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
;
2144 "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
2145 tagndx
, auxp
->u
.auxent
.x_sym
.x_misc
.x_fsize
,
2149 /* else fall through */
2151 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2152 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2153 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2156 fprintf (file
, " endndx %ld",
2158 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2166 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2168 while (l
->line_number
)
2170 fprintf (file
, "\n%4d : 0x%lx",
2173 (l
->u
.offset
+ symbol
->section
->vma
)));
2180 bfd_print_symbol_vandf (abfd
, (PTR
) file
, symbol
);
2181 fprintf (file
, " %-5s %s %s %s",
2182 symbol
->section
->name
,
2183 coffsymbol (symbol
)->native
? "n" : "g",
2184 coffsymbol (symbol
)->lineno
? "l" : " ",
2190 /* Return whether a symbol name implies a local symbol. In COFF,
2191 local symbols generally start with ``.L''. Most targets use this
2192 function for the is_local_label_name entry point, but some may
2196 _bfd_coff_is_local_label_name (abfd
, name
)
2197 bfd
*abfd ATTRIBUTE_UNUSED
;
2200 return name
[0] == '.' && name
[1] == 'L';
2203 /* Provided a BFD, a section and an offset (in bytes, not octets) into the
2204 section, calculate and return the name of the source file and the line
2205 nearest to the wanted location. */
2208 coff_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2209 functionname_ptr
, line_ptr
)
2214 const char **filename_ptr
;
2215 const char **functionname_ptr
;
2216 unsigned int *line_ptr
;
2220 unsigned int line_base
;
2221 coff_data_type
*cof
= coff_data (abfd
);
2222 /* Run through the raw syments if available */
2223 combined_entry_type
*p
;
2224 combined_entry_type
*pend
;
2226 struct coff_section_tdata
*sec_data
;
2229 /* Before looking through the symbol table, try to use a .stab
2230 section to find the information. */
2231 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2232 &found
, filename_ptr
,
2233 functionname_ptr
, line_ptr
,
2234 &coff_data(abfd
)->line_info
))
2240 /* Also try examining DWARF2 debugging information. */
2241 if (_bfd_dwarf2_find_nearest_line (abfd
, section
, symbols
, offset
,
2242 filename_ptr
, functionname_ptr
,
2244 &coff_data(abfd
)->dwarf2_find_line_info
))
2248 *functionname_ptr
= 0;
2251 /* Don't try and find line numbers in a non coff file */
2252 if (!bfd_family_coff (abfd
))
2258 /* Find the first C_FILE symbol. */
2259 p
= cof
->raw_syments
;
2263 pend
= p
+ cof
->raw_syment_count
;
2266 if (p
->u
.syment
.n_sclass
== C_FILE
)
2268 p
+= 1 + p
->u
.syment
.n_numaux
;
2276 /* Look through the C_FILE symbols to find the best one. */
2277 sec_vma
= bfd_get_section_vma (abfd
, section
);
2278 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2279 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2282 combined_entry_type
*p2
;
2284 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2286 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2288 if (p2
->u
.syment
.n_scnum
> 0
2290 == coff_section_from_bfd_index (abfd
,
2291 p2
->u
.syment
.n_scnum
)))
2293 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2300 /* We use <= MAXDIFF here so that if we get a zero length
2301 file, we actually use the next file entry. */
2303 && offset
+ sec_vma
>= (bfd_vma
) p2
->u
.syment
.n_value
2304 && offset
+ sec_vma
- (bfd_vma
) p2
->u
.syment
.n_value
<= maxdiff
)
2306 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2307 maxdiff
= offset
+ sec_vma
- p2
->u
.syment
.n_value
;
2310 /* Avoid endless loops on erroneous files by ensuring that
2311 we always move forward in the file. */
2312 if (p
>= cof
->raw_syments
+ p
->u
.syment
.n_value
)
2315 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2316 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2321 /* Now wander though the raw linenumbers of the section */
2322 /* If we have been called on this section before, and the offset we
2323 want is further down then we can prime the lookup loop. */
2324 sec_data
= coff_section_data (abfd
, section
);
2325 if (sec_data
!= NULL
2327 && offset
>= sec_data
->offset
)
2330 *functionname_ptr
= sec_data
->function
;
2331 line_base
= sec_data
->line_base
;
2339 if (section
->lineno
!= NULL
)
2341 bfd_vma last_value
= 0;
2343 l
= §ion
->lineno
[i
];
2345 for (; i
< section
->lineno_count
; i
++)
2347 if (l
->line_number
== 0)
2349 /* Get the symbol this line number points at */
2350 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2351 if (coff
->symbol
.value
> offset
)
2353 *functionname_ptr
= coff
->symbol
.name
;
2354 last_value
= coff
->symbol
.value
;
2357 combined_entry_type
*s
= coff
->native
;
2358 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2360 /* In XCOFF a debugging symbol can follow the
2362 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2363 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2365 /* S should now point to the .bf of the function. */
2366 if (s
->u
.syment
.n_numaux
)
2368 /* The linenumber is stored in the auxent. */
2369 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2370 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2371 *line_ptr
= line_base
;
2377 if (l
->u
.offset
> offset
)
2379 *line_ptr
= l
->line_number
+ line_base
- 1;
2384 /* If we fell off the end of the loop, then assume that this
2385 symbol has no line number info. Otherwise, symbols with no
2386 line number info get reported with the line number of the
2387 last line of the last symbol which does have line number
2388 info. We use 0x100 as a slop to account for cases where the
2389 last line has executable code. */
2390 if (i
>= section
->lineno_count
2392 && offset
- last_value
> 0x100)
2394 *functionname_ptr
= NULL
;
2399 /* Cache the results for the next call. */
2400 if (sec_data
== NULL
&& section
->owner
== abfd
)
2402 amt
= sizeof (struct coff_section_tdata
);
2403 section
->used_by_bfd
= (PTR
) bfd_zalloc (abfd
, amt
);
2404 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2406 if (sec_data
!= NULL
)
2408 sec_data
->offset
= offset
;
2410 sec_data
->function
= *functionname_ptr
;
2411 sec_data
->line_base
= line_base
;
2418 coff_sizeof_headers (abfd
, reloc
)
2426 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2430 size
= bfd_coff_filhsz (abfd
);
2433 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);
2437 /* Change the class of a coff symbol held by BFD. */
2439 bfd_coff_set_symbol_class (abfd
, symbol
, class)
2444 coff_symbol_type
* csym
;
2446 csym
= coff_symbol_from (abfd
, symbol
);
2449 bfd_set_error (bfd_error_invalid_operation
);
2452 else if (csym
->native
== NULL
)
2454 /* This is an alien symbol which no native coff backend data.
2455 We cheat here by creating a fake native entry for it and
2456 then filling in the class. This code is based on that in
2457 coff_write_alien_symbol(). */
2459 combined_entry_type
* native
;
2460 bfd_size_type amt
= sizeof (* native
);
2462 native
= (combined_entry_type
*) bfd_zalloc (abfd
, amt
);
2466 native
->u
.syment
.n_type
= T_NULL
;
2467 native
->u
.syment
.n_sclass
= class;
2469 if (bfd_is_und_section (symbol
->section
))
2471 native
->u
.syment
.n_scnum
= N_UNDEF
;
2472 native
->u
.syment
.n_value
= symbol
->value
;
2474 else if (bfd_is_com_section (symbol
->section
))
2476 native
->u
.syment
.n_scnum
= N_UNDEF
;
2477 native
->u
.syment
.n_value
= symbol
->value
;
2481 native
->u
.syment
.n_scnum
=
2482 symbol
->section
->output_section
->target_index
;
2483 native
->u
.syment
.n_value
= (symbol
->value
2484 + symbol
->section
->output_offset
);
2485 if (! obj_pe (abfd
))
2486 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
2488 /* Copy the any flags from the file header into the symbol.
2490 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (& csym
->symbol
)->flags
;
2493 csym
->native
= native
;
2497 csym
->native
->u
.syment
.n_sclass
= class;