1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
22 Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
24 /* This file contains COFF code that is not dependent on any
25 particular COFF target. There is only one version of this file in
26 libbfd.a, so no target specific code may be put in here. Or, to
29 ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
31 If you need to add some target specific behaviour, add a new hook
32 function to bfd_coff_backend_data.
34 Some of these functions are also called by the ECOFF routines.
35 Those functions may not use any COFF specific information, such as
41 #include "coff/internal.h"
44 static void coff_fix_symbol_name
45 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, bfd_size_type
*,
46 asection
**, bfd_size_type
*));
47 static boolean coff_write_symbol
48 PARAMS ((bfd
*, asymbol
*, combined_entry_type
*, unsigned int *,
49 bfd_size_type
*, asection
**, bfd_size_type
*));
50 static boolean coff_write_alien_symbol
51 PARAMS ((bfd
*, asymbol
*, unsigned int *, bfd_size_type
*,
52 asection
**, bfd_size_type
*));
53 static boolean coff_write_native_symbol
54 PARAMS ((bfd
*, coff_symbol_type
*, unsigned int *, bfd_size_type
*,
55 asection
**, bfd_size_type
*));
56 static void coff_pointerize_aux
57 PARAMS ((bfd
*, combined_entry_type
*, combined_entry_type
*,
58 unsigned int, combined_entry_type
*));
59 static boolean make_a_section_from_file
60 PARAMS ((bfd
*, struct internal_scnhdr
*, unsigned int));
61 static const bfd_target
*coff_real_object_p
62 PARAMS ((bfd
*, unsigned, struct internal_filehdr
*,
63 struct internal_aouthdr
*));
64 static void fixup_symbol_value
65 PARAMS ((bfd
*, coff_symbol_type
*, struct internal_syment
*));
66 static char *build_debug_section
68 static char *copy_name
69 PARAMS ((bfd
*, char *, int));
71 #define STRING_SIZE_SIZE (4)
73 /* Take a section header read from a coff file (in HOST byte order),
74 and make a BFD "section" out of it. This is used by ECOFF. */
76 make_a_section_from_file (abfd
, hdr
, target_index
)
78 struct internal_scnhdr
*hdr
;
79 unsigned int target_index
;
81 asection
*return_section
;
86 /* Handle long section names as in PE. */
87 if (bfd_coff_long_section_names (abfd
)
88 && hdr
->s_name
[0] == '/')
95 memcpy (buf
, hdr
->s_name
+ 1, SCNNMLEN
- 1);
96 buf
[SCNNMLEN
- 1] = '\0';
97 strindex
= strtol (buf
, &p
, 10);
98 if (*p
== '\0' && strindex
>= 0)
100 strings
= _bfd_coff_read_string_table (abfd
);
103 /* FIXME: For extra safety, we should make sure that
104 strindex does not run us past the end, but right now we
105 don't know the length of the string table. */
107 name
= bfd_alloc (abfd
, strlen (strings
) + 1);
110 strcpy (name
, strings
);
116 /* Assorted wastage to null-terminate the name, thanks AT&T! */
117 name
= bfd_alloc (abfd
, sizeof (hdr
->s_name
) + 1);
120 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
121 name
[sizeof (hdr
->s_name
)] = 0;
124 return_section
= bfd_make_section_anyway (abfd
, name
);
125 if (return_section
== NULL
)
128 return_section
->vma
= hdr
->s_vaddr
;
129 return_section
->lma
= hdr
->s_paddr
;
130 return_section
->_raw_size
= hdr
->s_size
;
131 return_section
->filepos
= hdr
->s_scnptr
;
132 return_section
->rel_filepos
= hdr
->s_relptr
;
133 return_section
->reloc_count
= hdr
->s_nreloc
;
135 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
137 return_section
->line_filepos
= hdr
->s_lnnoptr
;
139 return_section
->lineno_count
= hdr
->s_nlnno
;
140 return_section
->userdata
= NULL
;
141 return_section
->next
= (asection
*) NULL
;
142 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
144 return_section
->target_index
= target_index
;
146 /* At least on i386-coff, the line number count for a shared library
147 section must be ignored. */
148 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
149 return_section
->lineno_count
= 0;
151 if (hdr
->s_nreloc
!= 0)
152 return_section
->flags
|= SEC_RELOC
;
153 /* FIXME: should this check 'hdr->s_size > 0' */
154 if (hdr
->s_scnptr
!= 0)
155 return_section
->flags
|= SEC_HAS_CONTENTS
;
159 /* Read in a COFF object and make it into a BFD. This is used by
162 static const bfd_target
*
163 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
166 struct internal_filehdr
*internal_f
;
167 struct internal_aouthdr
*internal_a
;
169 flagword oflags
= abfd
->flags
;
170 bfd_vma ostart
= bfd_get_start_address (abfd
);
172 size_t readsize
; /* length of file_info */
174 char *external_sections
;
176 if (!(internal_f
->f_flags
& F_RELFLG
))
177 abfd
->flags
|= HAS_RELOC
;
178 if ((internal_f
->f_flags
& F_EXEC
))
179 abfd
->flags
|= EXEC_P
;
180 if (!(internal_f
->f_flags
& F_LNNO
))
181 abfd
->flags
|= HAS_LINENO
;
182 if (!(internal_f
->f_flags
& F_LSYMS
))
183 abfd
->flags
|= HAS_LOCALS
;
185 /* FIXME: How can we set D_PAGED correctly? */
186 if ((internal_f
->f_flags
& F_EXEC
) != 0)
187 abfd
->flags
|= D_PAGED
;
189 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
190 if (internal_f
->f_nsyms
)
191 abfd
->flags
|= HAS_SYMS
;
193 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
194 bfd_get_start_address (abfd
) = internal_a
->entry
;
196 bfd_get_start_address (abfd
) = 0;
198 /* Set up the tdata area. ECOFF uses its own routine, and overrides
200 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
204 scnhsz
= bfd_coff_scnhsz (abfd
);
205 readsize
= nscns
* scnhsz
;
206 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
207 if (!external_sections
)
210 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
213 /* Now copy data as required; construct all asections etc */
217 for (i
= 0; i
< nscns
; i
++)
219 struct internal_scnhdr tmp
;
220 bfd_coff_swap_scnhdr_in (abfd
,
221 (PTR
) (external_sections
+ i
* scnhsz
),
223 if (! make_a_section_from_file (abfd
, &tmp
, i
+ 1))
228 /* make_abs_section (abfd); */
230 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
236 bfd_release (abfd
, tdata
);
237 abfd
->flags
= oflags
;
238 bfd_get_start_address (abfd
) = ostart
;
239 return (const bfd_target
*) NULL
;
242 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
243 not a COFF file. This is also used by ECOFF. */
253 struct internal_filehdr internal_f
;
254 struct internal_aouthdr internal_a
;
256 /* figure out how much to read */
257 filhsz
= bfd_coff_filhsz (abfd
);
258 aoutsz
= bfd_coff_aoutsz (abfd
);
260 filehdr
= bfd_alloc (abfd
, filhsz
);
263 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
265 if (bfd_get_error () != bfd_error_system_call
)
266 bfd_set_error (bfd_error_wrong_format
);
269 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
270 bfd_release (abfd
, filehdr
);
272 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
274 bfd_set_error (bfd_error_wrong_format
);
277 nscns
= internal_f
.f_nscns
;
279 if (internal_f
.f_opthdr
)
283 opthdr
= bfd_alloc (abfd
, aoutsz
);
286 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
290 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
293 /* Seek past the opt hdr stuff */
294 if (bfd_seek (abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
298 return coff_real_object_p (abfd
, nscns
, &internal_f
,
299 (internal_f
.f_opthdr
!= 0
301 : (struct internal_aouthdr
*) NULL
));
304 /* Get the BFD section from a COFF symbol section number. */
307 coff_section_from_bfd_index (abfd
, index
)
311 struct sec
*answer
= abfd
->sections
;
314 return bfd_abs_section_ptr
;
315 if (index
== N_UNDEF
)
316 return bfd_und_section_ptr
;
317 if (index
== N_DEBUG
)
318 return bfd_abs_section_ptr
;
322 if (answer
->target_index
== index
)
324 answer
= answer
->next
;
327 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
328 has a bad symbol table in biglitpow.o. */
329 return bfd_und_section_ptr
;
332 /* Get the upper bound of a COFF symbol table. */
335 coff_get_symtab_upper_bound (abfd
)
338 if (!bfd_coff_slurp_symbol_table (abfd
))
341 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
345 /* Canonicalize a COFF symbol table. */
348 coff_get_symtab (abfd
, alocation
)
352 unsigned int counter
;
353 coff_symbol_type
*symbase
;
354 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
356 if (!bfd_coff_slurp_symbol_table (abfd
))
359 symbase
= obj_symbols (abfd
);
360 counter
= bfd_get_symcount (abfd
);
361 while (counter
-- > 0)
362 *location
++ = symbase
++;
366 return bfd_get_symcount (abfd
);
369 /* Get the name of a symbol. The caller must pass in a buffer of size
373 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
375 const struct internal_syment
*sym
;
378 /* FIXME: It's not clear this will work correctly if sizeof
380 if (sym
->_n
._n_n
._n_zeroes
!= 0
381 || sym
->_n
._n_n
._n_offset
== 0)
383 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
384 buf
[SYMNMLEN
] = '\0';
391 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
392 strings
= obj_coff_strings (abfd
);
395 strings
= _bfd_coff_read_string_table (abfd
);
399 return strings
+ sym
->_n
._n_n
._n_offset
;
403 /* Read in and swap the relocs. This returns a buffer holding the
404 relocs for section SEC in file ABFD. If CACHE is true and
405 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
406 the function is called again. If EXTERNAL_RELOCS is not NULL, it
407 is a buffer large enough to hold the unswapped relocs. If
408 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
409 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
410 value must be INTERNAL_RELOCS. The function returns NULL on error. */
412 struct internal_reloc
*
413 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
414 require_internal
, internal_relocs
)
418 bfd_byte
*external_relocs
;
419 boolean require_internal
;
420 struct internal_reloc
*internal_relocs
;
423 bfd_byte
*free_external
= NULL
;
424 struct internal_reloc
*free_internal
= NULL
;
427 struct internal_reloc
*irel
;
429 if (coff_section_data (abfd
, sec
) != NULL
430 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
432 if (! require_internal
)
433 return coff_section_data (abfd
, sec
)->relocs
;
434 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
435 sec
->reloc_count
* sizeof (struct internal_reloc
));
436 return internal_relocs
;
439 relsz
= bfd_coff_relsz (abfd
);
441 if (external_relocs
== NULL
)
443 free_external
= (bfd_byte
*) bfd_malloc (sec
->reloc_count
* relsz
);
444 if (free_external
== NULL
&& sec
->reloc_count
> 0)
446 external_relocs
= free_external
;
449 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
450 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
451 != relsz
* sec
->reloc_count
))
454 if (internal_relocs
== NULL
)
456 free_internal
= ((struct internal_reloc
*)
457 bfd_malloc (sec
->reloc_count
458 * sizeof (struct internal_reloc
)));
459 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
461 internal_relocs
= free_internal
;
464 /* Swap in the relocs. */
465 erel
= external_relocs
;
466 erel_end
= erel
+ relsz
* sec
->reloc_count
;
467 irel
= internal_relocs
;
468 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
469 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
471 if (free_external
!= NULL
)
473 free (free_external
);
474 free_external
= NULL
;
477 if (cache
&& free_internal
!= NULL
)
479 if (coff_section_data (abfd
, sec
) == NULL
)
482 (PTR
) bfd_zalloc (abfd
,
483 sizeof (struct coff_section_tdata
));
484 if (sec
->used_by_bfd
== NULL
)
486 coff_section_data (abfd
, sec
)->contents
= NULL
;
488 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
491 return internal_relocs
;
494 if (free_external
!= NULL
)
495 free (free_external
);
496 if (free_internal
!= NULL
)
497 free (free_internal
);
501 /* Set lineno_count for the output sections of a COFF file. */
504 coff_count_linenumbers (abfd
)
507 unsigned int limit
= bfd_get_symcount (abfd
);
515 /* This may be from the backend linker, in which case the
516 lineno_count in the sections is correct. */
517 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
518 total
+= s
->lineno_count
;
522 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
523 BFD_ASSERT (s
->lineno_count
== 0);
525 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
527 asymbol
*q_maybe
= *p
;
529 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
531 coff_symbol_type
*q
= coffsymbol (q_maybe
);
533 /* The AIX 4.1 compiler can sometimes generate line numbers
534 attached to debugging symbols. We try to simply ignore
536 if (q
->lineno
!= NULL
537 && q
->symbol
.section
->owner
!= NULL
)
539 /* This symbol has line numbers. Increment the owning
540 section's linenumber count. */
541 alent
*l
= q
->lineno
;
543 ++q
->symbol
.section
->output_section
->lineno_count
;
546 while (l
->line_number
!= 0)
549 ++q
->symbol
.section
->output_section
->lineno_count
;
559 /* Takes a bfd and a symbol, returns a pointer to the coff specific
560 area of the symbol if there is one. */
564 coff_symbol_from (ignore_abfd
, symbol
)
568 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
569 return (coff_symbol_type
*) NULL
;
571 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
572 return (coff_symbol_type
*) NULL
;
574 return (coff_symbol_type
*) symbol
;
578 fixup_symbol_value (abfd
, coff_symbol_ptr
, syment
)
580 coff_symbol_type
*coff_symbol_ptr
;
581 struct internal_syment
*syment
;
584 /* Normalize the symbol flags */
585 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
587 /* a common symbol is undefined with a value */
588 syment
->n_scnum
= N_UNDEF
;
589 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
591 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
593 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
595 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
597 syment
->n_scnum
= N_UNDEF
;
602 if (coff_symbol_ptr
->symbol
.section
)
605 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
607 syment
->n_value
= (coff_symbol_ptr
->symbol
.value
608 + coff_symbol_ptr
->symbol
.section
->output_offset
);
611 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
616 /* This can happen, but I don't know why yet (steve@cygnus.com) */
617 syment
->n_scnum
= N_ABS
;
618 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
623 /* Run through all the symbols in the symbol table and work out what
624 their indexes into the symbol table will be when output.
626 Coff requires that each C_FILE symbol points to the next one in the
627 chain, and that the last one points to the first external symbol. We
631 coff_renumber_symbols (bfd_ptr
, first_undef
)
635 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
636 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
637 unsigned int native_index
= 0;
638 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
639 unsigned int symbol_index
;
641 /* COFF demands that undefined symbols come after all other symbols.
642 Since we don't need to impose this extra knowledge on all our
643 client programs, deal with that here. Sort the symbol table;
644 just move the undefined symbols to the end, leaving the rest
645 alone. The O'Reilly book says that defined global symbols come
646 at the end before the undefined symbols, so we do that here as
648 /* @@ Do we have some condition we could test for, so we don't always
649 have to do this? I don't think relocatability is quite right, but
650 I'm not certain. [raeburn:19920508.1711EST] */
655 newsyms
= (asymbol
**) bfd_alloc (bfd_ptr
,
656 sizeof (asymbol
*) * (symbol_count
+ 1));
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_GLOBAL
| BSF_FUNCTION
))
666 *newsyms
++ = symbol_ptr_ptr
[i
];
668 for (i
= 0; i
< symbol_count
; i
++)
669 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
670 && !bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
671 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
672 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
674 *newsyms
++ = symbol_ptr_ptr
[i
];
676 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
678 for (i
= 0; i
< symbol_count
; i
++)
679 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
680 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
681 *newsyms
++ = symbol_ptr_ptr
[i
];
682 *newsyms
= (asymbol
*) NULL
;
683 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
686 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
688 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
689 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
690 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
692 combined_entry_type
*s
= coff_symbol_ptr
->native
;
695 if (s
->u
.syment
.n_sclass
== C_FILE
)
697 if (last_file
!= (struct internal_syment
*) NULL
)
698 last_file
->n_value
= native_index
;
699 last_file
= &(s
->u
.syment
);
704 /* Modify the symbol values according to their section and
707 fixup_symbol_value (bfd_ptr
, coff_symbol_ptr
, &(s
->u
.syment
));
709 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
710 s
[i
].offset
= native_index
++;
717 obj_conv_table_size (bfd_ptr
) = native_index
;
722 /* Run thorough the symbol table again, and fix it so that all
723 pointers to entries are changed to the entries' index in the output
727 coff_mangle_symbols (bfd_ptr
)
730 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
731 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
732 unsigned int symbol_index
;
734 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
736 coff_symbol_type
*coff_symbol_ptr
=
737 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
739 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
742 combined_entry_type
*s
= coff_symbol_ptr
->native
;
746 /* FIXME: We should use a union here. */
747 s
->u
.syment
.n_value
=
748 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
753 /* The value is the offset into the line number entries
754 for the symbol's section. On output, the symbol's
755 section should be N_DEBUG. */
756 s
->u
.syment
.n_value
=
757 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
758 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
759 coff_symbol_ptr
->symbol
.section
=
760 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
761 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
763 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
765 combined_entry_type
*a
= s
+ i
+ 1;
768 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
769 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
774 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
775 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
780 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
781 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
790 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
791 debug_string_section_p
, debug_string_size_p
)
794 combined_entry_type
*native
;
795 bfd_size_type
*string_size_p
;
796 asection
**debug_string_section_p
;
797 bfd_size_type
*debug_string_size_p
;
799 unsigned int name_length
;
800 union internal_auxent
*auxent
;
801 char *name
= (char *) (symbol
->name
);
803 if (name
== (char *) NULL
)
805 /* coff symbols always have names, so we'll make one up */
806 symbol
->name
= "strange";
807 name
= (char *) symbol
->name
;
809 name_length
= strlen (name
);
811 if (native
->u
.syment
.n_sclass
== C_FILE
812 && native
->u
.syment
.n_numaux
> 0)
814 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
815 auxent
= &(native
+ 1)->u
.auxent
;
817 if (bfd_coff_long_filenames (abfd
))
819 if (name_length
<= FILNMLEN
)
821 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
825 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
826 auxent
->x_file
.x_n
.x_zeroes
= 0;
827 *string_size_p
+= name_length
+ 1;
832 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
833 if (name_length
> FILNMLEN
)
835 name
[FILNMLEN
] = '\0';
841 if (name_length
<= SYMNMLEN
)
843 /* This name will fit into the symbol neatly */
844 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
846 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
848 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
850 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
851 *string_size_p
+= name_length
+ 1;
858 /* This name should be written into the .debug section. For
859 some reason each name is preceded by a two byte length
860 and also followed by a null byte. FIXME: We assume that
861 the .debug section has already been created, and that it
863 if (*debug_string_section_p
== (asection
*) NULL
)
864 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
865 filepos
= bfd_tell (abfd
);
866 bfd_put_16 (abfd
, name_length
+ 1, buf
);
867 if (!bfd_set_section_contents (abfd
,
868 *debug_string_section_p
,
870 (file_ptr
) *debug_string_size_p
,
872 || !bfd_set_section_contents (abfd
,
873 *debug_string_section_p
,
875 ((file_ptr
) *debug_string_size_p
877 (bfd_size_type
) name_length
+ 1))
879 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
881 native
->u
.syment
._n
._n_n
._n_offset
= *debug_string_size_p
+ 2;
882 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
883 *debug_string_size_p
+= name_length
+ 3;
888 /* We need to keep track of the symbol index so that when we write out
889 the relocs we can get the index for a symbol. This method is a
892 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
894 /* Write a symbol out to a COFF file. */
897 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
898 debug_string_section_p
, debug_string_size_p
)
901 combined_entry_type
*native
;
902 unsigned int *written
;
903 bfd_size_type
*string_size_p
;
904 asection
**debug_string_section_p
;
905 bfd_size_type
*debug_string_size_p
;
907 unsigned int numaux
= native
->u
.syment
.n_numaux
;
908 int type
= native
->u
.syment
.n_type
;
909 int class = native
->u
.syment
.n_sclass
;
911 bfd_size_type symesz
;
913 if (native
->u
.syment
.n_sclass
== C_FILE
)
914 symbol
->flags
|= BSF_DEBUGGING
;
916 if (symbol
->flags
& BSF_DEBUGGING
917 && bfd_is_abs_section (symbol
->section
))
919 native
->u
.syment
.n_scnum
= N_DEBUG
;
921 else if (bfd_is_abs_section (symbol
->section
))
923 native
->u
.syment
.n_scnum
= N_ABS
;
925 else if (bfd_is_und_section (symbol
->section
))
927 native
->u
.syment
.n_scnum
= N_UNDEF
;
931 native
->u
.syment
.n_scnum
=
932 symbol
->section
->output_section
->target_index
;
935 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
936 debug_string_section_p
, debug_string_size_p
);
938 symesz
= bfd_coff_symesz (abfd
);
939 buf
= bfd_alloc (abfd
, symesz
);
942 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
943 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
945 bfd_release (abfd
, buf
);
947 if (native
->u
.syment
.n_numaux
> 0)
949 bfd_size_type auxesz
;
952 auxesz
= bfd_coff_auxesz (abfd
);
953 buf
= bfd_alloc (abfd
, auxesz
);
956 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
958 bfd_coff_swap_aux_out (abfd
,
959 &((native
+ j
+ 1)->u
.auxent
),
963 native
->u
.syment
.n_numaux
,
965 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
968 bfd_release (abfd
, buf
);
971 /* Store the index for use when we write out the relocs. */
972 set_index (symbol
, *written
);
974 *written
+= numaux
+ 1;
978 /* Write out a symbol to a COFF file that does not come from a COFF
979 file originally. This symbol may have been created by the linker,
980 or we may be linking a non COFF file to a COFF file. */
983 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
984 debug_string_section_p
, debug_string_size_p
)
987 unsigned int *written
;
988 bfd_size_type
*string_size_p
;
989 asection
**debug_string_section_p
;
990 bfd_size_type
*debug_string_size_p
;
992 combined_entry_type
*native
;
993 combined_entry_type dummy
;
996 native
->u
.syment
.n_type
= T_NULL
;
997 native
->u
.syment
.n_flags
= 0;
998 if (bfd_is_und_section (symbol
->section
))
1000 native
->u
.syment
.n_scnum
= N_UNDEF
;
1001 native
->u
.syment
.n_value
= symbol
->value
;
1003 else if (bfd_is_com_section (symbol
->section
))
1005 native
->u
.syment
.n_scnum
= N_UNDEF
;
1006 native
->u
.syment
.n_value
= symbol
->value
;
1008 else if (symbol
->flags
& BSF_DEBUGGING
)
1010 /* There isn't much point to writing out a debugging symbol
1011 unless we are prepared to convert it into COFF debugging
1012 format. So, we just ignore them. We must clobber the symbol
1013 name to keep it from being put in the string table. */
1019 native
->u
.syment
.n_scnum
=
1020 symbol
->section
->output_section
->target_index
;
1021 native
->u
.syment
.n_value
= (symbol
->value
1022 + symbol
->section
->output_offset
);
1023 if (! obj_pe (abfd
))
1024 native
->u
.syment
.n_value
+= symbol
->section
->output_section
->vma
;
1026 /* Copy the any flags from the the file header into the symbol.
1029 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
1030 if (c
!= (coff_symbol_type
*) NULL
)
1031 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
1035 native
->u
.syment
.n_type
= 0;
1036 if (symbol
->flags
& BSF_LOCAL
)
1037 native
->u
.syment
.n_sclass
= C_STAT
;
1039 native
->u
.syment
.n_sclass
= C_EXT
;
1040 native
->u
.syment
.n_numaux
= 0;
1042 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
1043 debug_string_section_p
, debug_string_size_p
);
1046 /* Write a native symbol to a COFF file. */
1049 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1050 debug_string_section_p
, debug_string_size_p
)
1052 coff_symbol_type
*symbol
;
1053 unsigned int *written
;
1054 bfd_size_type
*string_size_p
;
1055 asection
**debug_string_section_p
;
1056 bfd_size_type
*debug_string_size_p
;
1058 combined_entry_type
*native
= symbol
->native
;
1059 alent
*lineno
= symbol
->lineno
;
1061 /* If this symbol has an associated line number, we must store the
1062 symbol index in the line number field. We also tag the auxent to
1063 point to the right place in the lineno table. */
1064 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1066 unsigned int count
= 0;
1067 lineno
[count
].u
.offset
= *written
;
1068 if (native
->u
.syment
.n_numaux
)
1070 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1072 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1073 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1076 /* Count and relocate all other linenumbers. */
1078 while (lineno
[count
].line_number
!= 0)
1082 I've been told this, but still need proof:
1083 > The second bug is also in `bfd/coffcode.h'. This bug
1084 > causes the linker to screw up the pc-relocations for
1085 > all the line numbers in COFF code. This bug isn't only
1086 > specific to A29K implementations, but affects all
1087 > systems using COFF format binaries. Note that in COFF
1088 > object files, the line number core offsets output by
1089 > the assembler are relative to the start of each
1090 > procedure, not to the start of the .text section. This
1091 > patch relocates the line numbers relative to the
1092 > `native->u.syment.n_value' instead of the section
1094 > modular!olson@cs.arizona.edu (Jon Olson)
1096 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1098 lineno
[count
].u
.offset
+=
1099 (symbol
->symbol
.section
->output_section
->vma
1100 + symbol
->symbol
.section
->output_offset
);
1104 symbol
->done_lineno
= true;
1106 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1107 count
* bfd_coff_linesz (abfd
);
1110 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1111 string_size_p
, debug_string_section_p
,
1112 debug_string_size_p
);
1115 /* Write out the COFF symbols. */
1118 coff_write_symbols (abfd
)
1121 bfd_size_type string_size
;
1122 asection
*debug_string_section
;
1123 bfd_size_type debug_string_size
;
1125 unsigned int limit
= bfd_get_symcount (abfd
);
1126 unsigned int written
= 0;
1130 debug_string_section
= NULL
;
1131 debug_string_size
= 0;
1133 /* If this target supports long section names, they must be put into
1134 the string table. This is supported by PE. This code must
1135 handle section names just as they are handled in
1136 coff_write_object_contents. */
1137 if (bfd_coff_long_section_names (abfd
))
1141 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1145 len
= strlen (o
->name
);
1147 string_size
+= len
+ 1;
1151 /* Seek to the right place */
1152 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1155 /* Output all the symbols we have */
1158 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1160 asymbol
*symbol
= *p
;
1161 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1163 if (c_symbol
== (coff_symbol_type
*) NULL
1164 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1166 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1167 &debug_string_section
,
1168 &debug_string_size
))
1173 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1174 &string_size
, &debug_string_section
,
1175 &debug_string_size
))
1180 obj_raw_syment_count (abfd
) = written
;
1182 /* Now write out strings */
1184 if (string_size
!= 0)
1186 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1187 bfd_byte buffer
[STRING_SIZE_SIZE
];
1189 #if STRING_SIZE_SIZE == 4
1190 bfd_h_put_32 (abfd
, size
, buffer
);
1192 #error Change bfd_h_put_32
1194 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
1197 /* Handle long section names. This code must handle section
1198 names just as they are handled in coff_write_object_contents. */
1199 if (bfd_coff_long_section_names (abfd
))
1203 for (o
= abfd
->sections
; o
!= NULL
; o
= o
->next
)
1207 len
= strlen (o
->name
);
1210 if (bfd_write (o
->name
, 1, len
+ 1, abfd
) != len
+ 1)
1216 for (p
= abfd
->outsymbols
, i
= 0;
1221 size_t name_length
= strlen (q
->name
);
1222 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1225 /* Figure out whether the symbol name should go in the string
1226 table. Symbol names that are short enough are stored
1227 directly in the syment structure. File names permit a
1228 different, longer, length in the syment structure. On
1229 XCOFF, some symbol names are stored in the .debug section
1230 rather than in the string table. */
1232 if (c_symbol
== NULL
1233 || c_symbol
->native
== NULL
)
1235 /* This is not a COFF symbol, so it certainly is not a
1236 file name, nor does it go in the .debug section. */
1239 else if (bfd_coff_symname_in_debug (abfd
,
1240 &c_symbol
->native
->u
.syment
))
1242 /* This symbol name is in the XCOFF .debug section.
1243 Don't write it into the string table. */
1244 maxlen
= name_length
;
1246 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1247 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1252 if (name_length
> maxlen
)
1254 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1262 /* We would normally not write anything here, but we'll write
1263 out 4 so that any stupid coff reader which tries to read the
1264 string table even when there isn't one won't croak. */
1265 unsigned int size
= STRING_SIZE_SIZE
;
1266 bfd_byte buffer
[STRING_SIZE_SIZE
];
1268 #if STRING_SIZE_SIZE == 4
1269 bfd_h_put_32 (abfd
, size
, buffer
);
1271 #error Change bfd_h_put_32
1273 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1274 != STRING_SIZE_SIZE
)
1278 /* Make sure the .debug section was created to be the correct size.
1279 We should create it ourselves on the fly, but we don't because
1280 BFD won't let us write to any section until we know how large all
1281 the sections are. We could still do it by making another pass
1282 over the symbols. FIXME. */
1283 BFD_ASSERT (debug_string_size
== 0
1284 || (debug_string_section
!= (asection
*) NULL
1285 && (BFD_ALIGN (debug_string_size
,
1286 1 << debug_string_section
->alignment_power
)
1287 == bfd_section_size (abfd
, debug_string_section
))));
1293 coff_write_linenumbers (abfd
)
1297 bfd_size_type linesz
;
1300 linesz
= bfd_coff_linesz (abfd
);
1301 buff
= bfd_alloc (abfd
, linesz
);
1304 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1306 if (s
->lineno_count
)
1308 asymbol
**q
= abfd
->outsymbols
;
1309 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1311 /* Find all the linenumbers in this section */
1315 if (p
->section
->output_section
== s
)
1318 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1319 (bfd_asymbol_bfd (p
), p
));
1322 /* Found a linenumber entry, output */
1323 struct internal_lineno out
;
1324 memset ((PTR
) & out
, 0, sizeof (out
));
1326 out
.l_addr
.l_symndx
= l
->u
.offset
;
1327 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1328 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1331 while (l
->line_number
)
1333 out
.l_lnno
= l
->line_number
;
1334 out
.l_addr
.l_symndx
= l
->u
.offset
;
1335 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1336 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1346 bfd_release (abfd
, buff
);
1352 coff_get_lineno (ignore_abfd
, symbol
)
1356 return coffsymbol (symbol
)->lineno
;
1361 /* This is only called from coff_add_missing_symbols, which has been
1365 coff_section_symbol (abfd
, name
)
1369 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1371 combined_entry_type
*csym
;
1374 csym
= coff_symbol_from (abfd
, sym
)->native
;
1375 /* Make sure back-end COFF stuff is there. */
1380 coff_symbol_type sym
;
1381 /* @@FIXME This shouldn't use a fixed size!! */
1382 combined_entry_type e
[10];
1385 f
= (struct foo
*) bfd_alloc (abfd
, sizeof (*f
));
1388 bfd_set_error (bfd_error_no_error
);
1391 memset ((char *) f
, 0, sizeof (*f
));
1392 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1394 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1395 csym
[0].u
.syment
.n_numaux
= 1;
1396 /* SF_SET_STATICS (sym); @@ ??? */
1397 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1398 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1399 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1401 if (sec
->output_section
== NULL
)
1403 sec
->output_section
= sec
;
1404 sec
->output_offset
= 0;
1412 /* This function transforms the offsets into the symbol table into
1413 pointers to syments. */
1416 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1418 combined_entry_type
*table_base
;
1419 combined_entry_type
*symbol
;
1420 unsigned int indaux
;
1421 combined_entry_type
*auxent
;
1423 int type
= symbol
->u
.syment
.n_type
;
1424 int class = symbol
->u
.syment
.n_sclass
;
1426 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1428 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1429 (abfd
, table_base
, symbol
, indaux
, auxent
))
1433 /* Don't bother if this is a file or a section */
1434 if (class == C_STAT
&& type
== T_NULL
)
1436 if (class == C_FILE
)
1439 /* Otherwise patch up */
1440 #define N_TMASK coff_data (abfd)->local_n_tmask
1441 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1442 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
|| class == C_FCN
)
1443 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1445 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1446 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1447 auxent
->fix_end
= 1;
1449 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1450 generate one, so we must be careful to ignore it. */
1451 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1453 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1454 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1455 auxent
->fix_tag
= 1;
1459 /* Allocate space for the ".debug" section, and read it.
1460 We did not read the debug section until now, because
1461 we didn't want to go to the trouble until someone needed it. */
1464 build_debug_section (abfd
)
1467 char *debug_section
;
1470 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1474 bfd_set_error (bfd_error_no_debug_section
);
1478 debug_section
= (PTR
) bfd_alloc (abfd
,
1479 bfd_get_section_size_before_reloc (sect
));
1480 if (debug_section
== NULL
)
1483 /* Seek to the beginning of the `.debug' section and read it.
1484 Save the current position first; it is needed by our caller.
1485 Then read debug section and reset the file pointer. */
1487 position
= bfd_tell (abfd
);
1488 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1489 || (bfd_read (debug_section
,
1490 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1491 != bfd_get_section_size_before_reloc (sect
))
1492 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1494 return debug_section
;
1498 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1499 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1500 be \0-terminated. */
1502 copy_name (abfd
, name
, maxlen
)
1510 for (len
= 0; len
< maxlen
; ++len
)
1512 if (name
[len
] == '\0')
1518 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1520 strncpy (newname
, name
, len
);
1521 newname
[len
] = '\0';
1525 /* Read in the external symbols. */
1528 _bfd_coff_get_external_symbols (abfd
)
1531 bfd_size_type symesz
;
1535 if (obj_coff_external_syms (abfd
) != NULL
)
1538 symesz
= bfd_coff_symesz (abfd
);
1540 size
= obj_raw_syment_count (abfd
) * symesz
;
1542 syms
= (PTR
) bfd_malloc (size
);
1543 if (syms
== NULL
&& size
!= 0)
1546 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1547 || bfd_read (syms
, size
, 1, abfd
) != size
)
1554 obj_coff_external_syms (abfd
) = syms
;
1559 /* Read in the external strings. The strings are not loaded until
1560 they are needed. This is because we have no simple way of
1561 detecting a missing string table in an archive. */
1564 _bfd_coff_read_string_table (abfd
)
1567 char extstrsize
[STRING_SIZE_SIZE
];
1571 if (obj_coff_strings (abfd
) != NULL
)
1572 return obj_coff_strings (abfd
);
1574 if (obj_sym_filepos (abfd
) == 0)
1576 bfd_set_error (bfd_error_no_symbols
);
1581 (obj_sym_filepos (abfd
)
1582 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1586 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1588 if (bfd_get_error () != bfd_error_file_truncated
)
1591 /* There is no string table. */
1592 strsize
= STRING_SIZE_SIZE
;
1596 #if STRING_SIZE_SIZE == 4
1597 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1599 #error Change bfd_h_get_32
1603 if (strsize
< STRING_SIZE_SIZE
)
1605 (*_bfd_error_handler
)
1606 ("%s: bad string table size %lu", bfd_get_filename (abfd
),
1607 (unsigned long) strsize
);
1608 bfd_set_error (bfd_error_bad_value
);
1612 strings
= (char *) bfd_malloc (strsize
);
1613 if (strings
== NULL
)
1616 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1617 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1618 != strsize
- STRING_SIZE_SIZE
)
1624 obj_coff_strings (abfd
) = strings
;
1629 /* Free up the external symbols and strings read from a COFF file. */
1632 _bfd_coff_free_symbols (abfd
)
1635 if (obj_coff_external_syms (abfd
) != NULL
1636 && ! obj_coff_keep_syms (abfd
))
1638 free (obj_coff_external_syms (abfd
));
1639 obj_coff_external_syms (abfd
) = NULL
;
1641 if (obj_coff_strings (abfd
) != NULL
1642 && ! obj_coff_keep_strings (abfd
))
1644 free (obj_coff_strings (abfd
));
1645 obj_coff_strings (abfd
) = NULL
;
1650 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1651 knit the symbol names into a normalized form. By normalized here I
1652 mean that all symbols have an n_offset pointer that points to a null-
1653 terminated string. */
1655 combined_entry_type
*
1656 coff_get_normalized_symtab (abfd
)
1659 combined_entry_type
*internal
;
1660 combined_entry_type
*internal_ptr
;
1661 combined_entry_type
*symbol_ptr
;
1662 combined_entry_type
*internal_end
;
1663 bfd_size_type symesz
;
1666 const char *string_table
= NULL
;
1667 char *debug_section
= NULL
;
1670 if (obj_raw_syments (abfd
) != NULL
)
1671 return obj_raw_syments (abfd
);
1673 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1674 internal
= (combined_entry_type
*) bfd_zalloc (abfd
, size
);
1675 if (internal
== NULL
&& size
!= 0)
1677 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1679 if (! _bfd_coff_get_external_symbols (abfd
))
1682 raw_src
= (char *) obj_coff_external_syms (abfd
);
1684 /* mark the end of the symbols */
1685 symesz
= bfd_coff_symesz (abfd
);
1686 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1688 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1689 probably possible. If one shows up, it will probably kill us. */
1691 /* Swap all the raw entries */
1692 for (internal_ptr
= internal
;
1694 raw_src
+= symesz
, internal_ptr
++)
1698 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1699 (PTR
) & internal_ptr
->u
.syment
);
1700 symbol_ptr
= internal_ptr
;
1703 i
< symbol_ptr
->u
.syment
.n_numaux
;
1708 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1709 symbol_ptr
->u
.syment
.n_type
,
1710 symbol_ptr
->u
.syment
.n_sclass
,
1711 i
, symbol_ptr
->u
.syment
.n_numaux
,
1712 &(internal_ptr
->u
.auxent
));
1713 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1718 /* Free the raw symbols, but not the strings (if we have them). */
1719 obj_coff_keep_strings (abfd
) = true;
1720 if (! _bfd_coff_free_symbols (abfd
))
1723 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1726 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1727 && internal_ptr
->u
.syment
.n_numaux
> 0)
1729 /* make a file symbol point to the name in the auxent, since
1730 the text ".file" is redundant */
1731 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1733 /* the filename is a long one, point into the string table */
1734 if (string_table
== NULL
)
1736 string_table
= _bfd_coff_read_string_table (abfd
);
1737 if (string_table
== NULL
)
1741 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1744 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1748 /* ordinary short filename, put into memory anyway */
1749 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1750 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1756 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1758 /* This is a "short" name. Make it long. */
1759 unsigned long i
= 0;
1760 char *newstring
= NULL
;
1762 /* find the length of this string without walking into memory
1764 for (i
= 0; i
< 8; ++i
)
1766 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1769 } /* if end of string */
1770 } /* possible lengths of this string. */
1772 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1774 memset (newstring
, 0, i
);
1775 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1776 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1777 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1779 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1780 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1781 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1783 /* Long name already. Point symbol at the string in the
1785 if (string_table
== NULL
)
1787 string_table
= _bfd_coff_read_string_table (abfd
);
1788 if (string_table
== NULL
)
1791 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1794 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1798 /* Long name in debug section. Very similar. */
1799 if (debug_section
== NULL
)
1800 debug_section
= build_debug_section (abfd
);
1801 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1802 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1805 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1808 obj_raw_syments (abfd
) = internal
;
1809 BFD_ASSERT (obj_raw_syment_count (abfd
)
1810 == (unsigned int) (internal_ptr
- internal
));
1813 } /* coff_get_normalized_symtab() */
1816 coff_get_reloc_upper_bound (abfd
, asect
)
1820 if (bfd_get_format (abfd
) != bfd_object
)
1822 bfd_set_error (bfd_error_invalid_operation
);
1825 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1829 coff_make_empty_symbol (abfd
)
1832 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1835 memset (new, 0, sizeof *new);
1836 new->symbol
.section
= 0;
1838 new->lineno
= (alent
*) NULL
;
1839 new->done_lineno
= false;
1840 new->symbol
.the_bfd
= abfd
;
1841 return &new->symbol
;
1844 /* Make a debugging symbol. */
1847 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1852 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1855 /* @@ The 10 is a guess at a plausible maximum number of aux entries
1856 (but shouldn't be a constant). */
1857 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1860 new->symbol
.section
= bfd_abs_section_ptr
;
1861 new->symbol
.flags
= BSF_DEBUGGING
;
1862 new->lineno
= (alent
*) NULL
;
1863 new->done_lineno
= false;
1864 new->symbol
.the_bfd
= abfd
;
1865 return &new->symbol
;
1870 coff_get_symbol_info (abfd
, symbol
, ret
)
1875 bfd_symbol_info (symbol
, ret
);
1876 if (coffsymbol (symbol
)->native
!= NULL
1877 && coffsymbol (symbol
)->native
->fix_value
)
1879 combined_entry_type
*psym
;
1881 psym
= ((combined_entry_type
*)
1882 coffsymbol (symbol
)->native
->u
.syment
.n_value
);
1883 ret
->value
= (bfd_vma
) (psym
- obj_raw_syments (abfd
));
1887 /* Return the COFF syment for a symbol. */
1890 bfd_coff_get_syment (abfd
, symbol
, psyment
)
1893 struct internal_syment
*psyment
;
1895 coff_symbol_type
*csym
;
1897 csym
= coff_symbol_from (abfd
, symbol
);
1898 if (csym
== NULL
|| csym
->native
== NULL
)
1900 bfd_set_error (bfd_error_invalid_operation
);
1904 *psyment
= csym
->native
->u
.syment
;
1906 if (csym
->native
->fix_value
)
1907 psyment
->n_value
= ((combined_entry_type
*) psyment
->n_value
1908 - obj_raw_syments (abfd
));
1910 /* FIXME: We should handle fix_line here. */
1915 /* Return the COFF auxent for a symbol. */
1918 bfd_coff_get_auxent (abfd
, symbol
, indx
, pauxent
)
1922 union internal_auxent
*pauxent
;
1924 coff_symbol_type
*csym
;
1925 combined_entry_type
*ent
;
1927 csym
= coff_symbol_from (abfd
, symbol
);
1930 || csym
->native
== NULL
1931 || indx
>= csym
->native
->u
.syment
.n_numaux
)
1933 bfd_set_error (bfd_error_invalid_operation
);
1937 ent
= csym
->native
+ indx
+ 1;
1939 *pauxent
= ent
->u
.auxent
;
1942 pauxent
->x_sym
.x_tagndx
.l
=
1943 ((combined_entry_type
*) pauxent
->x_sym
.x_tagndx
.p
1944 - obj_raw_syments (abfd
));
1947 pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
1948 ((combined_entry_type
*) pauxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1949 - obj_raw_syments (abfd
));
1951 if (ent
->fix_scnlen
)
1952 pauxent
->x_csect
.x_scnlen
.l
=
1953 ((combined_entry_type
*) pauxent
->x_csect
.x_scnlen
.p
1954 - obj_raw_syments (abfd
));
1959 /* Print out information about COFF symbol. */
1962 coff_print_symbol (abfd
, filep
, symbol
, how
)
1966 bfd_print_symbol_type how
;
1968 FILE *file
= (FILE *) filep
;
1972 case bfd_print_symbol_name
:
1973 fprintf (file
, "%s", symbol
->name
);
1976 case bfd_print_symbol_more
:
1977 fprintf (file
, "coff %s %s",
1978 coffsymbol (symbol
)->native
? "n" : "g",
1979 coffsymbol (symbol
)->lineno
? "l" : " ");
1982 case bfd_print_symbol_all
:
1983 if (coffsymbol (symbol
)->native
)
1987 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1988 combined_entry_type
*root
= obj_raw_syments (abfd
);
1989 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1991 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1993 if (! combined
->fix_value
)
1994 val
= (unsigned long) combined
->u
.syment
.n_value
;
1996 val
= ((unsigned long)
1997 ((combined_entry_type
*) combined
->u
.syment
.n_value
2001 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
2002 combined
->u
.syment
.n_scnum
,
2003 combined
->u
.syment
.n_flags
,
2004 combined
->u
.syment
.n_type
,
2005 combined
->u
.syment
.n_sclass
,
2006 combined
->u
.syment
.n_numaux
,
2010 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
2012 combined_entry_type
*auxp
= combined
+ aux
+ 1;
2016 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
2018 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
2020 fprintf (file
, "\n");
2022 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
2025 switch (combined
->u
.syment
.n_sclass
)
2028 fprintf (file
, "File ");
2032 if (combined
->u
.syment
.n_type
== T_NULL
)
2033 /* probably a section symbol? */
2035 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
2036 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
2037 auxp
->u
.auxent
.x_scn
.x_nreloc
,
2038 auxp
->u
.auxent
.x_scn
.x_nlinno
);
2039 if (auxp
->u
.auxent
.x_scn
.x_checksum
!= 0
2040 || auxp
->u
.auxent
.x_scn
.x_associated
!= 0
2041 || auxp
->u
.auxent
.x_scn
.x_comdat
!= 0)
2042 fprintf (file
, " checksum 0x%lx assoc %d comdat %d",
2043 auxp
->u
.auxent
.x_scn
.x_checksum
,
2044 auxp
->u
.auxent
.x_scn
.x_associated
,
2045 auxp
->u
.auxent
.x_scn
.x_comdat
);
2048 /* else fall through */
2051 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
2052 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
2053 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
2056 fprintf (file
, " endndx %ld",
2058 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
2066 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
2068 while (l
->line_number
)
2070 fprintf (file
, "\n%4d : 0x%lx",
2073 (l
->u
.offset
+ symbol
->section
->vma
)));
2080 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
2081 fprintf (file
, " %-5s %s %s %s",
2082 symbol
->section
->name
,
2083 coffsymbol (symbol
)->native
? "n" : "g",
2084 coffsymbol (symbol
)->lineno
? "l" : " ",
2090 /* Return whether a symbol name implies a local symbol. In COFF,
2091 local symbols generally start with ``.L''. Most targets use this
2092 function for the is_local_label_name entry point, but some may
2096 _bfd_coff_is_local_label_name (abfd
, name
)
2100 return name
[0] == '.' && name
[1] == 'L';
2103 /* Provided a BFD, a section and an offset into the section, calculate
2104 and return the name of the source file and the line nearest to the
2109 coff_find_nearest_line (abfd
, section
, symbols
, offset
, filename_ptr
,
2110 functionname_ptr
, line_ptr
)
2115 CONST
char **filename_ptr
;
2116 CONST
char **functionname_ptr
;
2117 unsigned int *line_ptr
;
2121 unsigned int line_base
;
2122 coff_data_type
*cof
= coff_data (abfd
);
2123 /* Run through the raw syments if available */
2124 combined_entry_type
*p
;
2125 combined_entry_type
*pend
;
2127 struct coff_section_tdata
*sec_data
;
2129 /* Before looking through the symbol table, try to use a .stab
2130 section to find the information. */
2131 if (! _bfd_stab_section_find_nearest_line (abfd
, symbols
, section
, offset
,
2132 &found
, filename_ptr
,
2133 functionname_ptr
, line_ptr
,
2134 &coff_data (abfd
)->line_info
))
2140 *functionname_ptr
= 0;
2143 /* Don't try and find line numbers in a non coff file */
2144 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
2150 /* Find the first C_FILE symbol. */
2151 p
= cof
->raw_syments
;
2155 pend
= p
+ cof
->raw_syment_count
;
2158 if (p
->u
.syment
.n_sclass
== C_FILE
)
2160 p
+= 1 + p
->u
.syment
.n_numaux
;
2167 /* Look through the C_FILE symbols to find the best one. */
2168 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2169 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
2172 combined_entry_type
*p2
;
2174 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
2176 p2
+= 1 + p2
->u
.syment
.n_numaux
)
2178 if (p2
->u
.syment
.n_scnum
> 0
2180 == coff_section_from_bfd_index (abfd
,
2181 p2
->u
.syment
.n_scnum
)))
2183 if (p2
->u
.syment
.n_sclass
== C_FILE
)
2191 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
2192 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
2194 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2195 maxdiff
= offset
- p2
->u
.syment
.n_value
;
2198 /* Avoid endless loops on erroneous files by ensuring that
2199 we always move forward in the file. */
2200 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
2203 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2204 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2209 /* Now wander though the raw linenumbers of the section */
2210 /* If we have been called on this section before, and the offset we
2211 want is further down then we can prime the lookup loop. */
2212 sec_data
= coff_section_data (abfd
, section
);
2213 if (sec_data
!= NULL
2215 && offset
>= sec_data
->offset
)
2218 *functionname_ptr
= sec_data
->function
;
2219 line_base
= sec_data
->line_base
;
2227 if (section
->lineno
!= NULL
)
2229 l
= §ion
->lineno
[i
];
2231 for (; i
< section
->lineno_count
; i
++)
2233 if (l
->line_number
== 0)
2235 /* Get the symbol this line number points at */
2236 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2237 if (coff
->symbol
.value
> offset
)
2239 *functionname_ptr
= coff
->symbol
.name
;
2242 combined_entry_type
*s
= coff
->native
;
2243 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2245 /* In XCOFF a debugging symbol can follow the
2247 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2248 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2250 /* S should now point to the .bf of the function. */
2251 if (s
->u
.syment
.n_numaux
)
2253 /* The linenumber is stored in the auxent. */
2254 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2255 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2256 *line_ptr
= line_base
;
2262 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
2264 *line_ptr
= l
->line_number
+ line_base
- 1;
2270 /* Cache the results for the next call. */
2271 if (sec_data
== NULL
&& section
->owner
== abfd
)
2273 section
->used_by_bfd
=
2274 ((PTR
) bfd_zalloc (abfd
,
2275 sizeof (struct coff_section_tdata
)));
2276 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2278 if (sec_data
!= NULL
)
2280 sec_data
->offset
= offset
;
2282 sec_data
->function
= *functionname_ptr
;
2283 sec_data
->line_base
= line_base
;
2290 coff_sizeof_headers (abfd
, reloc
)
2298 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2302 size
= bfd_coff_filhsz (abfd
);
2305 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);