1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 91, 92, 93, 94, 1995 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
*));
60 #define STRING_SIZE_SIZE (4)
62 /* Take a section header read from a coff file (in HOST byte order),
63 and make a BFD "section" out of it. This is used by ECOFF. */
65 make_a_section_from_file (abfd
, hdr
, target_index
)
67 struct internal_scnhdr
*hdr
;
68 unsigned int target_index
;
70 asection
*return_section
;
73 /* Assorted wastage to null-terminate the name, thanks AT&T! */
74 name
= bfd_alloc (abfd
, sizeof (hdr
->s_name
) + 1);
77 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
78 name
[sizeof (hdr
->s_name
)] = 0;
80 return_section
= bfd_make_section_anyway (abfd
, name
);
81 if (return_section
== NULL
)
84 /* s_paddr is presumed to be = to s_vaddr */
86 return_section
->vma
= hdr
->s_vaddr
;
87 return_section
->lma
= return_section
->vma
;
88 return_section
->_raw_size
= hdr
->s_size
;
89 return_section
->filepos
= hdr
->s_scnptr
;
90 return_section
->rel_filepos
= hdr
->s_relptr
;
91 return_section
->reloc_count
= hdr
->s_nreloc
;
93 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
95 return_section
->line_filepos
= hdr
->s_lnnoptr
;
97 return_section
->lineno_count
= hdr
->s_nlnno
;
98 return_section
->userdata
= NULL
;
99 return_section
->next
= (asection
*) NULL
;
100 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
102 return_section
->target_index
= target_index
;
104 /* At least on i386-coff, the line number count for a shared library
105 section must be ignored. */
106 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
107 return_section
->lineno_count
= 0;
109 if (hdr
->s_nreloc
!= 0)
110 return_section
->flags
|= SEC_RELOC
;
111 /* FIXME: should this check 'hdr->s_size > 0' */
112 if (hdr
->s_scnptr
!= 0)
113 return_section
->flags
|= SEC_HAS_CONTENTS
;
117 /* Read in a COFF object and make it into a BFD. This is used by
120 static const bfd_target
*
121 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
124 struct internal_filehdr
*internal_f
;
125 struct internal_aouthdr
*internal_a
;
127 flagword oflags
= abfd
->flags
;
128 bfd_vma ostart
= bfd_get_start_address (abfd
);
130 size_t readsize
; /* length of file_info */
132 char *external_sections
;
134 if (!(internal_f
->f_flags
& F_RELFLG
))
135 abfd
->flags
|= HAS_RELOC
;
136 if ((internal_f
->f_flags
& F_EXEC
))
137 abfd
->flags
|= EXEC_P
;
138 if (!(internal_f
->f_flags
& F_LNNO
))
139 abfd
->flags
|= HAS_LINENO
;
140 if (!(internal_f
->f_flags
& F_LSYMS
))
141 abfd
->flags
|= HAS_LOCALS
;
143 /* FIXME: How can we set D_PAGED correctly? */
144 if ((internal_f
->f_flags
& F_EXEC
) != 0)
145 abfd
->flags
|= D_PAGED
;
147 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
148 if (internal_f
->f_nsyms
)
149 abfd
->flags
|= HAS_SYMS
;
151 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
152 bfd_get_start_address (abfd
) = internal_a
->entry
;
154 bfd_get_start_address (abfd
) = 0;
156 /* Set up the tdata area. ECOFF uses its own routine, and overrides
158 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
162 scnhsz
= bfd_coff_scnhsz (abfd
);
163 readsize
= nscns
* scnhsz
;
164 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
165 if (!external_sections
)
168 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
171 /* Now copy data as required; construct all asections etc */
175 for (i
= 0; i
< nscns
; i
++)
177 struct internal_scnhdr tmp
;
178 bfd_coff_swap_scnhdr_in (abfd
,
179 (PTR
) (external_sections
+ i
* scnhsz
),
181 make_a_section_from_file (abfd
, &tmp
, i
+ 1);
185 /* make_abs_section (abfd); */
187 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
193 bfd_release (abfd
, tdata
);
194 abfd
->flags
= oflags
;
195 bfd_get_start_address (abfd
) = ostart
;
196 return (const bfd_target
*) NULL
;
199 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
200 not a COFF file. This is also used by ECOFF. */
210 struct internal_filehdr internal_f
;
211 struct internal_aouthdr internal_a
;
213 /* figure out how much to read */
214 filhsz
= bfd_coff_filhsz (abfd
);
215 aoutsz
= bfd_coff_aoutsz (abfd
);
217 filehdr
= bfd_alloc (abfd
, filhsz
);
220 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
222 if (bfd_get_error () != bfd_error_system_call
)
223 bfd_set_error (bfd_error_wrong_format
);
226 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
227 bfd_release (abfd
, filehdr
);
229 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
231 bfd_set_error (bfd_error_wrong_format
);
234 nscns
= internal_f
.f_nscns
;
236 if (internal_f
.f_opthdr
)
240 opthdr
= bfd_alloc (abfd
, aoutsz
);
243 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
247 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
250 /* Seek past the opt hdr stuff */
251 if (bfd_seek (abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
255 return coff_real_object_p (abfd
, nscns
, &internal_f
,
256 (internal_f
.f_opthdr
!= 0
258 : (struct internal_aouthdr
*) NULL
));
261 /* Get the BFD section from a COFF symbol section number. */
264 coff_section_from_bfd_index (abfd
, index
)
268 struct sec
*answer
= abfd
->sections
;
271 return bfd_abs_section_ptr
;
272 if (index
== N_UNDEF
)
273 return bfd_und_section_ptr
;
274 if (index
== N_DEBUG
)
275 return bfd_abs_section_ptr
;
279 if (answer
->target_index
== index
)
281 answer
= answer
->next
;
284 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
285 has a bad symbol table in biglitpow.o. */
286 return bfd_und_section_ptr
;
289 /* Get the upper bound of a COFF symbol table. */
292 coff_get_symtab_upper_bound (abfd
)
295 if (!bfd_coff_slurp_symbol_table (abfd
))
298 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
302 /* Canonicalize a COFF symbol table. */
305 coff_get_symtab (abfd
, alocation
)
309 unsigned int counter
;
310 coff_symbol_type
*symbase
;
311 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
313 if (!bfd_coff_slurp_symbol_table (abfd
))
316 symbase
= obj_symbols (abfd
);
317 counter
= bfd_get_symcount (abfd
);
318 while (counter
-- > 0)
319 *location
++ = symbase
++;
323 return bfd_get_symcount (abfd
);
326 /* Get the name of a symbol. The caller must pass in a buffer of size
330 _bfd_coff_internal_syment_name (abfd
, sym
, buf
)
332 const struct internal_syment
*sym
;
335 /* FIXME: It's not clear this will work correctly if sizeof
337 if (sym
->_n
._n_n
._n_zeroes
!= 0
338 || sym
->_n
._n_n
._n_offset
== 0)
340 memcpy (buf
, sym
->_n
._n_name
, SYMNMLEN
);
341 buf
[SYMNMLEN
] = '\0';
348 BFD_ASSERT (sym
->_n
._n_n
._n_offset
>= STRING_SIZE_SIZE
);
349 strings
= obj_coff_strings (abfd
);
352 strings
= _bfd_coff_read_string_table (abfd
);
356 return strings
+ sym
->_n
._n_n
._n_offset
;
360 /* Read in and swap the relocs. This returns a buffer holding the
361 relocs for section SEC in file ABFD. If CACHE is true and
362 INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
363 the function is called again. If EXTERNAL_RELOCS is not NULL, it
364 is a buffer large enough to hold the unswapped relocs. If
365 INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
366 the swapped relocs. If REQUIRE_INTERNAL is true, then the return
367 value must be INTERNAL_RELOCS. The function returns NULL on error. */
369 struct internal_reloc
*
370 _bfd_coff_read_internal_relocs (abfd
, sec
, cache
, external_relocs
,
371 require_internal
, internal_relocs
)
375 bfd_byte
*external_relocs
;
376 boolean require_internal
;
377 struct internal_reloc
*internal_relocs
;
380 bfd_byte
*free_external
= NULL
;
381 struct internal_reloc
*free_internal
= NULL
;
384 struct internal_reloc
*irel
;
386 if (coff_section_data (abfd
, sec
) != NULL
387 && coff_section_data (abfd
, sec
)->relocs
!= NULL
)
389 if (! require_internal
)
390 return coff_section_data (abfd
, sec
)->relocs
;
391 memcpy (internal_relocs
, coff_section_data (abfd
, sec
)->relocs
,
392 sec
->reloc_count
* sizeof (struct internal_reloc
));
393 return internal_relocs
;
396 relsz
= bfd_coff_relsz (abfd
);
398 if (external_relocs
== NULL
)
400 free_external
= (bfd_byte
*) bfd_malloc (sec
->reloc_count
* relsz
);
401 if (free_external
== NULL
&& sec
->reloc_count
> 0)
403 external_relocs
= free_external
;
406 if (bfd_seek (abfd
, sec
->rel_filepos
, SEEK_SET
) != 0
407 || (bfd_read (external_relocs
, relsz
, sec
->reloc_count
, abfd
)
408 != relsz
* sec
->reloc_count
))
411 if (internal_relocs
== NULL
)
413 free_internal
= ((struct internal_reloc
*)
414 bfd_malloc (sec
->reloc_count
415 * sizeof (struct internal_reloc
)));
416 if (free_internal
== NULL
&& sec
->reloc_count
> 0)
418 internal_relocs
= free_internal
;
421 /* Swap in the relocs. */
422 erel
= external_relocs
;
423 erel_end
= erel
+ relsz
* sec
->reloc_count
;
424 irel
= internal_relocs
;
425 for (; erel
< erel_end
; erel
+= relsz
, irel
++)
426 bfd_coff_swap_reloc_in (abfd
, (PTR
) erel
, (PTR
) irel
);
428 if (free_external
!= NULL
)
430 free (free_external
);
431 free_external
= NULL
;
434 if (cache
&& free_internal
!= NULL
)
436 if (coff_section_data (abfd
, sec
) == NULL
)
439 (PTR
) bfd_zalloc (abfd
,
440 sizeof (struct coff_section_tdata
));
441 if (sec
->used_by_bfd
== NULL
)
443 coff_section_data (abfd
, sec
)->contents
= NULL
;
445 coff_section_data (abfd
, sec
)->relocs
= free_internal
;
448 return internal_relocs
;
451 if (free_external
!= NULL
)
452 free (free_external
);
453 if (free_internal
!= NULL
)
454 free (free_internal
);
458 /* Set lineno_count for the output sections of a COFF file. */
461 coff_count_linenumbers (abfd
)
464 unsigned int limit
= bfd_get_symcount (abfd
);
472 /* This may be from the backend linker, in which case the
473 lineno_count in the sections is correct. */
474 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
475 total
+= s
->lineno_count
;
479 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
480 BFD_ASSERT (s
->lineno_count
== 0);
482 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
484 asymbol
*q_maybe
= *p
;
486 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
488 coff_symbol_type
*q
= coffsymbol (q_maybe
);
490 /* The AIX 4.1 compiler can sometimes generate line numbers
491 attached to debugging symbols. We try to simply ignore
493 if (q
->lineno
!= NULL
494 && q
->symbol
.section
->owner
!= NULL
)
496 /* This symbol has line numbers. Increment the owning
497 section's linenumber count. */
498 alent
*l
= q
->lineno
;
500 ++q
->symbol
.section
->output_section
->lineno_count
;
503 while (l
->line_number
!= 0)
506 ++q
->symbol
.section
->output_section
->lineno_count
;
516 /* Takes a bfd and a symbol, returns a pointer to the coff specific
517 area of the symbol if there is one. */
521 coff_symbol_from (ignore_abfd
, symbol
)
525 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
526 return (coff_symbol_type
*) NULL
;
528 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
529 return (coff_symbol_type
*) NULL
;
531 return (coff_symbol_type
*) symbol
;
535 fixup_symbol_value (coff_symbol_ptr
, syment
)
536 coff_symbol_type
*coff_symbol_ptr
;
537 struct internal_syment
*syment
;
540 /* Normalize the symbol flags */
541 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
543 /* a common symbol is undefined with a value */
544 syment
->n_scnum
= N_UNDEF
;
545 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
547 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
549 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
551 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
553 syment
->n_scnum
= N_UNDEF
;
558 if (coff_symbol_ptr
->symbol
.section
)
561 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
564 coff_symbol_ptr
->symbol
.value
+
565 coff_symbol_ptr
->symbol
.section
->output_offset
+
566 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
571 /* This can happen, but I don't know why yet (steve@cygnus.com) */
572 syment
->n_scnum
= N_ABS
;
573 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
578 /* Run through all the symbols in the symbol table and work out what
579 their indexes into the symbol table will be when output.
581 Coff requires that each C_FILE symbol points to the next one in the
582 chain, and that the last one points to the first external symbol. We
586 coff_renumber_symbols (bfd_ptr
, first_undef
)
590 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
591 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
592 unsigned int native_index
= 0;
593 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
594 unsigned int symbol_index
;
596 /* COFF demands that undefined symbols come after all other symbols.
597 Since we don't need to impose this extra knowledge on all our
598 client programs, deal with that here. Sort the symbol table;
599 just move the undefined symbols to the end, leaving the rest
600 alone. The O'Reilly book says that defined global symbols come
601 at the end before the undefined symbols, so we do that here as
603 /* @@ Do we have some condition we could test for, so we don't always
604 have to do this? I don't think relocatability is quite right, but
605 I'm not certain. [raeburn:19920508.1711EST] */
610 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
612 * (symbol_count
+ 1));
615 bfd_ptr
->outsymbols
= newsyms
;
616 for (i
= 0; i
< symbol_count
; i
++)
617 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
618 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
619 && !bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
620 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
622 *newsyms
++ = symbol_ptr_ptr
[i
];
624 for (i
= 0; i
< symbol_count
; i
++)
625 if (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
626 && (bfd_is_com_section (symbol_ptr_ptr
[i
]->section
)
627 || ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
631 *newsyms
++ = symbol_ptr_ptr
[i
];
633 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
635 for (i
= 0; i
< symbol_count
; i
++)
636 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
637 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
638 *newsyms
++ = symbol_ptr_ptr
[i
];
639 *newsyms
= (asymbol
*) NULL
;
640 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
643 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
645 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
646 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
647 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
649 combined_entry_type
*s
= coff_symbol_ptr
->native
;
652 if (s
->u
.syment
.n_sclass
== C_FILE
)
654 if (last_file
!= (struct internal_syment
*) NULL
)
655 last_file
->n_value
= native_index
;
656 last_file
= &(s
->u
.syment
);
661 /* Modify the symbol values according to their section and
664 fixup_symbol_value (coff_symbol_ptr
, &(s
->u
.syment
));
666 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
667 s
[i
].offset
= native_index
++;
674 obj_conv_table_size (bfd_ptr
) = native_index
;
679 /* Run thorough the symbol table again, and fix it so that all
680 pointers to entries are changed to the entries' index in the output
684 coff_mangle_symbols (bfd_ptr
)
687 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
688 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
689 unsigned int symbol_index
;
691 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
693 coff_symbol_type
*coff_symbol_ptr
=
694 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
696 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
699 combined_entry_type
*s
= coff_symbol_ptr
->native
;
703 /* FIXME: We should use a union here. */
704 s
->u
.syment
.n_value
=
705 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
710 /* The value is the offset into the line number entries
711 for the symbol's section. On output, the symbol's
712 section should be N_DEBUG. */
713 s
->u
.syment
.n_value
=
714 (coff_symbol_ptr
->symbol
.section
->output_section
->line_filepos
715 + s
->u
.syment
.n_value
* bfd_coff_linesz (bfd_ptr
));
716 coff_symbol_ptr
->symbol
.section
=
717 coff_section_from_bfd_index (bfd_ptr
, N_DEBUG
);
718 BFD_ASSERT (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
);
720 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
722 combined_entry_type
*a
= s
+ i
+ 1;
725 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
726 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
731 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
732 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
737 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
738 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
747 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
748 debug_string_section_p
, debug_string_size_p
)
751 combined_entry_type
*native
;
752 bfd_size_type
*string_size_p
;
753 asection
**debug_string_section_p
;
754 bfd_size_type
*debug_string_size_p
;
756 unsigned int name_length
;
757 union internal_auxent
*auxent
;
758 char *name
= (char *) (symbol
->name
);
760 if (name
== (char *) NULL
)
762 /* coff symbols always have names, so we'll make one up */
763 symbol
->name
= "strange";
764 name
= (char *) symbol
->name
;
766 name_length
= strlen (name
);
768 if (native
->u
.syment
.n_sclass
== C_FILE
769 && native
->u
.syment
.n_numaux
> 0)
771 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
772 auxent
= &(native
+ 1)->u
.auxent
;
774 if (bfd_coff_long_filenames (abfd
))
776 if (name_length
<= FILNMLEN
)
778 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
782 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
783 auxent
->x_file
.x_n
.x_zeroes
= 0;
784 *string_size_p
+= name_length
+ 1;
789 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
790 if (name_length
> FILNMLEN
)
792 name
[FILNMLEN
] = '\0';
798 if (name_length
<= SYMNMLEN
)
800 /* This name will fit into the symbol neatly */
801 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
803 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
805 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
807 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
808 *string_size_p
+= name_length
+ 1;
815 /* This name should be written into the .debug section. For
816 some reason each name is preceded by a two byte length
817 and also followed by a null byte. FIXME: We assume that
818 the .debug section has already been created, and that it
820 if (*debug_string_section_p
== (asection
*) NULL
)
821 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
822 filepos
= bfd_tell (abfd
);
823 bfd_put_16 (abfd
, name_length
+ 1, buf
);
824 if (!bfd_set_section_contents (abfd
,
825 *debug_string_section_p
,
827 (file_ptr
) *debug_string_size_p
,
829 || !bfd_set_section_contents (abfd
,
830 *debug_string_section_p
,
832 ((file_ptr
) *debug_string_size_p
834 (bfd_size_type
) name_length
+ 1))
836 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
838 native
->u
.syment
._n
._n_n
._n_offset
= *debug_string_size_p
+ 2;
839 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
840 *debug_string_size_p
+= name_length
+ 3;
845 /* We need to keep track of the symbol index so that when we write out
846 the relocs we can get the index for a symbol. This method is a
849 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
851 /* Write a symbol out to a COFF file. */
854 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
855 debug_string_section_p
, debug_string_size_p
)
858 combined_entry_type
*native
;
859 unsigned int *written
;
860 bfd_size_type
*string_size_p
;
861 asection
**debug_string_section_p
;
862 bfd_size_type
*debug_string_size_p
;
864 unsigned int numaux
= native
->u
.syment
.n_numaux
;
865 int type
= native
->u
.syment
.n_type
;
866 int class = native
->u
.syment
.n_sclass
;
868 bfd_size_type symesz
;
870 if (native
->u
.syment
.n_sclass
== C_FILE
)
871 symbol
->flags
|= BSF_DEBUGGING
;
873 if (symbol
->flags
& BSF_DEBUGGING
874 && bfd_is_abs_section (symbol
->section
))
876 native
->u
.syment
.n_scnum
= N_DEBUG
;
878 else if (bfd_is_abs_section (symbol
->section
))
880 native
->u
.syment
.n_scnum
= N_ABS
;
882 else if (bfd_is_und_section (symbol
->section
))
884 native
->u
.syment
.n_scnum
= N_UNDEF
;
888 native
->u
.syment
.n_scnum
=
889 symbol
->section
->output_section
->target_index
;
892 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
893 debug_string_section_p
, debug_string_size_p
);
895 symesz
= bfd_coff_symesz (abfd
);
896 buf
= bfd_alloc (abfd
, symesz
);
899 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
900 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
902 bfd_release (abfd
, buf
);
904 if (native
->u
.syment
.n_numaux
> 0)
906 bfd_size_type auxesz
;
909 auxesz
= bfd_coff_auxesz (abfd
);
910 buf
= bfd_alloc (abfd
, auxesz
);
913 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
915 bfd_coff_swap_aux_out (abfd
,
916 &((native
+ j
+ 1)->u
.auxent
),
920 native
->u
.syment
.n_numaux
,
922 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
925 bfd_release (abfd
, buf
);
928 /* Store the index for use when we write out the relocs. */
929 set_index (symbol
, *written
);
931 *written
+= numaux
+ 1;
935 /* Write out a symbol to a COFF file that does not come from a COFF
936 file originally. This symbol may have been created by the linker,
937 or we may be linking a non COFF file to a COFF file. */
940 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
941 debug_string_section_p
, debug_string_size_p
)
944 unsigned int *written
;
945 bfd_size_type
*string_size_p
;
946 asection
**debug_string_section_p
;
947 bfd_size_type
*debug_string_size_p
;
949 combined_entry_type
*native
;
950 combined_entry_type dummy
;
953 native
->u
.syment
.n_type
= T_NULL
;
954 native
->u
.syment
.n_flags
= 0;
955 if (bfd_is_und_section (symbol
->section
))
957 native
->u
.syment
.n_scnum
= N_UNDEF
;
958 native
->u
.syment
.n_value
= symbol
->value
;
960 else if (bfd_is_com_section (symbol
->section
))
962 native
->u
.syment
.n_scnum
= N_UNDEF
;
963 native
->u
.syment
.n_value
= symbol
->value
;
965 else if (symbol
->flags
& BSF_DEBUGGING
)
967 /* There isn't much point to writing out a debugging symbol
968 unless we are prepared to convert it into COFF debugging
969 format. So, we just ignore them. We must clobber the symbol
970 name to keep it from being put in the string table. */
976 native
->u
.syment
.n_scnum
=
977 symbol
->section
->output_section
->target_index
;
978 native
->u
.syment
.n_value
= (symbol
->value
979 + symbol
->section
->output_section
->vma
980 + symbol
->section
->output_offset
);
982 /* Copy the any flags from the the file header into the symbol.
985 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
986 if (c
!= (coff_symbol_type
*) NULL
)
987 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
991 native
->u
.syment
.n_type
= 0;
992 if (symbol
->flags
& BSF_LOCAL
)
993 native
->u
.syment
.n_sclass
= C_STAT
;
995 native
->u
.syment
.n_sclass
= C_EXT
;
996 native
->u
.syment
.n_numaux
= 0;
998 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
999 debug_string_section_p
, debug_string_size_p
);
1002 /* Write a native symbol to a COFF file. */
1005 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
1006 debug_string_section_p
, debug_string_size_p
)
1008 coff_symbol_type
*symbol
;
1009 unsigned int *written
;
1010 bfd_size_type
*string_size_p
;
1011 asection
**debug_string_section_p
;
1012 bfd_size_type
*debug_string_size_p
;
1014 combined_entry_type
*native
= symbol
->native
;
1015 alent
*lineno
= symbol
->lineno
;
1017 /* If this symbol has an associated line number, we must store the
1018 symbol index in the line number field. We also tag the auxent to
1019 point to the right place in the lineno table. */
1020 if (lineno
&& !symbol
->done_lineno
&& symbol
->symbol
.section
->owner
!= NULL
)
1022 unsigned int count
= 0;
1023 lineno
[count
].u
.offset
= *written
;
1024 if (native
->u
.syment
.n_numaux
)
1026 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
1028 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
1029 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
1032 /* Count and relocate all other linenumbers. */
1034 while (lineno
[count
].line_number
!= 0)
1038 I've been told this, but still need proof:
1039 > The second bug is also in `bfd/coffcode.h'. This bug
1040 > causes the linker to screw up the pc-relocations for
1041 > all the line numbers in COFF code. This bug isn't only
1042 > specific to A29K implementations, but affects all
1043 > systems using COFF format binaries. Note that in COFF
1044 > object files, the line number core offsets output by
1045 > the assembler are relative to the start of each
1046 > procedure, not to the start of the .text section. This
1047 > patch relocates the line numbers relative to the
1048 > `native->u.syment.n_value' instead of the section
1050 > modular!olson@cs.arizona.edu (Jon Olson)
1052 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
1054 lineno
[count
].u
.offset
+=
1055 (symbol
->symbol
.section
->output_section
->vma
1056 + symbol
->symbol
.section
->output_offset
);
1060 symbol
->done_lineno
= true;
1062 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
1063 count
* bfd_coff_linesz (abfd
);
1066 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
1067 string_size_p
, debug_string_section_p
,
1068 debug_string_size_p
);
1071 /* Write out the COFF symbols. */
1074 coff_write_symbols (abfd
)
1077 bfd_size_type string_size
;
1078 asection
*debug_string_section
;
1079 bfd_size_type debug_string_size
;
1081 unsigned int limit
= bfd_get_symcount (abfd
);
1082 unsigned int written
= 0;
1086 debug_string_section
= NULL
;
1087 debug_string_size
= 0;
1089 /* Seek to the right place */
1090 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
1093 /* Output all the symbols we have */
1096 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
1098 asymbol
*symbol
= *p
;
1099 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
1101 if (c_symbol
== (coff_symbol_type
*) NULL
1102 || c_symbol
->native
== (combined_entry_type
*) NULL
)
1104 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
1105 &debug_string_section
,
1106 &debug_string_size
))
1111 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
1112 &string_size
, &debug_string_section
,
1113 &debug_string_size
))
1118 obj_raw_syment_count (abfd
) = written
;
1120 /* Now write out strings */
1122 if (string_size
!= 0)
1124 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
1125 bfd_byte buffer
[STRING_SIZE_SIZE
];
1127 #if STRING_SIZE_SIZE == 4
1128 bfd_h_put_32 (abfd
, size
, buffer
);
1130 #error Change bfd_h_put_32
1132 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
1134 for (p
= abfd
->outsymbols
, i
= 0;
1139 size_t name_length
= strlen (q
->name
);
1140 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1143 /* Figure out whether the symbol name should go in the string
1144 table. Symbol names that are short enough are stored
1145 directly in the syment structure. File names permit a
1146 different, longer, length in the syment structure. On
1147 XCOFF, some symbol names are stored in the .debug section
1148 rather than in the string table. */
1150 if (c_symbol
== NULL
1151 || c_symbol
->native
== NULL
)
1153 /* This is not a COFF symbol, so it certainly is not a
1154 file name, nor does it go in the .debug section. */
1157 else if (bfd_coff_symname_in_debug (abfd
,
1158 &c_symbol
->native
->u
.syment
))
1160 /* This symbol name is in the XCOFF .debug section.
1161 Don't write it into the string table. */
1162 maxlen
= name_length
;
1164 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1165 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1170 if (name_length
> maxlen
)
1172 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1180 /* We would normally not write anything here, but we'll write
1181 out 4 so that any stupid coff reader which tries to read the
1182 string table even when there isn't one won't croak. */
1183 unsigned int size
= STRING_SIZE_SIZE
;
1184 bfd_byte buffer
[STRING_SIZE_SIZE
];
1186 #if STRING_SIZE_SIZE == 4
1187 bfd_h_put_32 (abfd
, size
, buffer
);
1189 #error Change bfd_h_put_32
1191 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1192 != STRING_SIZE_SIZE
)
1196 /* Make sure the .debug section was created to be the correct size.
1197 We should create it ourselves on the fly, but we don't because
1198 BFD won't let us write to any section until we know how large all
1199 the sections are. We could still do it by making another pass
1200 over the symbols. FIXME. */
1201 BFD_ASSERT (debug_string_size
== 0
1202 || (debug_string_section
!= (asection
*) NULL
1203 && (BFD_ALIGN (debug_string_size
,
1204 1 << debug_string_section
->alignment_power
)
1205 == bfd_section_size (abfd
, debug_string_section
))));
1211 coff_write_linenumbers (abfd
)
1215 bfd_size_type linesz
;
1218 linesz
= bfd_coff_linesz (abfd
);
1219 buff
= bfd_alloc (abfd
, linesz
);
1222 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1224 if (s
->lineno_count
)
1226 asymbol
**q
= abfd
->outsymbols
;
1227 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1229 /* Find all the linenumbers in this section */
1233 if (p
->section
->output_section
== s
)
1236 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1237 (bfd_asymbol_bfd (p
), p
));
1240 /* Found a linenumber entry, output */
1241 struct internal_lineno out
;
1242 memset ((PTR
) & out
, 0, sizeof (out
));
1244 out
.l_addr
.l_symndx
= l
->u
.offset
;
1245 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1246 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1249 while (l
->line_number
)
1251 out
.l_lnno
= l
->line_number
;
1252 out
.l_addr
.l_symndx
= l
->u
.offset
;
1253 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1254 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1264 bfd_release (abfd
, buff
);
1270 coff_get_lineno (ignore_abfd
, symbol
)
1274 return coffsymbol (symbol
)->lineno
;
1278 coff_section_symbol (abfd
, name
)
1282 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1284 combined_entry_type
*csym
;
1287 csym
= coff_symbol_from (abfd
, sym
)->native
;
1288 /* Make sure back-end COFF stuff is there. */
1293 coff_symbol_type sym
;
1294 /* @@FIXME This shouldn't use a fixed size!! */
1295 combined_entry_type e
[10];
1298 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
1301 bfd_set_error (bfd_error_no_error
);
1304 memset ((char *) f
, 0, sizeof (*f
));
1305 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1307 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1308 csym
[0].u
.syment
.n_numaux
= 1;
1309 /* SF_SET_STATICS (sym); @@ ??? */
1310 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1311 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1312 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1314 if (sec
->output_section
== NULL
)
1316 sec
->output_section
= sec
;
1317 sec
->output_offset
= 0;
1323 /* This function transforms the offsets into the symbol table into
1324 pointers to syments. */
1327 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1329 combined_entry_type
*table_base
;
1330 combined_entry_type
*symbol
;
1331 unsigned int indaux
;
1332 combined_entry_type
*auxent
;
1334 int type
= symbol
->u
.syment
.n_type
;
1335 int class = symbol
->u
.syment
.n_sclass
;
1337 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1339 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1340 (abfd
, table_base
, symbol
, indaux
, auxent
))
1344 /* Don't bother if this is a file or a section */
1345 if (class == C_STAT
&& type
== T_NULL
)
1347 if (class == C_FILE
)
1350 /* Otherwise patch up */
1351 #define N_TMASK coff_data (abfd)->local_n_tmask
1352 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1353 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
)
1354 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1356 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1357 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1358 auxent
->fix_end
= 1;
1360 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1361 generate one, so we must be careful to ignore it. */
1362 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1364 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1365 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1366 auxent
->fix_tag
= 1;
1370 /* Allocate space for the ".debug" section, and read it.
1371 We did not read the debug section until now, because
1372 we didn't want to go to the trouble until someone needed it. */
1375 build_debug_section (abfd
)
1378 char *debug_section
;
1381 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1385 bfd_set_error (bfd_error_no_debug_section
);
1389 debug_section
= (PTR
) bfd_alloc (abfd
,
1390 bfd_get_section_size_before_reloc (sect
));
1391 if (debug_section
== NULL
)
1394 /* Seek to the beginning of the `.debug' section and read it.
1395 Save the current position first; it is needed by our caller.
1396 Then read debug section and reset the file pointer. */
1398 position
= bfd_tell (abfd
);
1399 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1400 || (bfd_read (debug_section
,
1401 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1402 != bfd_get_section_size_before_reloc (sect
))
1403 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1405 return debug_section
;
1409 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1410 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1411 be \0-terminated. */
1413 copy_name (abfd
, name
, maxlen
)
1421 for (len
= 0; len
< maxlen
; ++len
)
1423 if (name
[len
] == '\0')
1429 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1431 strncpy (newname
, name
, len
);
1432 newname
[len
] = '\0';
1436 /* Read in the external symbols. */
1439 _bfd_coff_get_external_symbols (abfd
)
1442 bfd_size_type symesz
;
1446 if (obj_coff_external_syms (abfd
) != NULL
)
1449 symesz
= bfd_coff_symesz (abfd
);
1451 size
= obj_raw_syment_count (abfd
) * symesz
;
1453 syms
= (PTR
) bfd_malloc (size
);
1454 if (syms
== NULL
&& size
!= 0)
1457 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1458 || bfd_read (syms
, size
, 1, abfd
) != size
)
1465 obj_coff_external_syms (abfd
) = syms
;
1470 /* Read in the external strings. The strings are not loaded until
1471 they are needed. This is because we have no simple way of
1472 detecting a missing string table in an archive. */
1475 _bfd_coff_read_string_table (abfd
)
1478 char extstrsize
[STRING_SIZE_SIZE
];
1482 if (obj_coff_strings (abfd
) != NULL
)
1483 return obj_coff_strings (abfd
);
1486 (obj_sym_filepos (abfd
)
1487 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1491 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1493 if (bfd_get_error () != bfd_error_file_truncated
)
1496 /* There is no string table. */
1497 strsize
= STRING_SIZE_SIZE
;
1501 #if STRING_SIZE_SIZE == 4
1502 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1504 #error Change bfd_h_get_32
1508 if (strsize
< STRING_SIZE_SIZE
)
1510 (*_bfd_error_handler
)
1511 ("%s: bad string table size %lu", bfd_get_filename (abfd
),
1512 (unsigned long) strsize
);
1513 bfd_set_error (bfd_error_bad_value
);
1517 strings
= (char *) bfd_malloc (strsize
);
1518 if (strings
== NULL
)
1521 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1522 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1523 != strsize
- STRING_SIZE_SIZE
)
1529 obj_coff_strings (abfd
) = strings
;
1534 /* Free up the external symbols and strings read from a COFF file. */
1537 _bfd_coff_free_symbols (abfd
)
1540 if (obj_coff_external_syms (abfd
) != NULL
1541 && ! obj_coff_keep_syms (abfd
))
1543 free (obj_coff_external_syms (abfd
));
1544 obj_coff_external_syms (abfd
) = NULL
;
1546 if (obj_coff_strings (abfd
) != NULL
1547 && ! obj_coff_keep_strings (abfd
))
1549 free (obj_coff_strings (abfd
));
1550 obj_coff_strings (abfd
) = NULL
;
1555 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1556 knit the symbol names into a normalized form. By normalized here I
1557 mean that all symbols have an n_offset pointer that points to a null-
1558 terminated string. */
1560 combined_entry_type
*
1561 coff_get_normalized_symtab (abfd
)
1564 combined_entry_type
*internal
;
1565 combined_entry_type
*internal_ptr
;
1566 combined_entry_type
*symbol_ptr
;
1567 combined_entry_type
*internal_end
;
1568 bfd_size_type symesz
;
1571 const char *string_table
= NULL
;
1572 char *debug_section
= NULL
;
1575 if (obj_raw_syments (abfd
) != NULL
)
1576 return obj_raw_syments (abfd
);
1578 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1579 internal
= (combined_entry_type
*) bfd_alloc (abfd
, size
);
1580 if (internal
== NULL
&& size
!= 0)
1582 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1584 if (! _bfd_coff_get_external_symbols (abfd
))
1587 raw_src
= (char *) obj_coff_external_syms (abfd
);
1589 /* mark the end of the symbols */
1590 symesz
= bfd_coff_symesz (abfd
);
1591 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1593 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1594 probably possible. If one shows up, it will probably kill us. */
1596 /* Swap all the raw entries */
1597 for (internal_ptr
= internal
;
1599 raw_src
+= symesz
, internal_ptr
++)
1603 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1604 (PTR
) & internal_ptr
->u
.syment
);
1605 internal_ptr
->fix_value
= 0;
1606 internal_ptr
->fix_tag
= 0;
1607 internal_ptr
->fix_end
= 0;
1608 internal_ptr
->fix_scnlen
= 0;
1609 symbol_ptr
= internal_ptr
;
1612 i
< symbol_ptr
->u
.syment
.n_numaux
;
1618 internal_ptr
->fix_value
= 0;
1619 internal_ptr
->fix_tag
= 0;
1620 internal_ptr
->fix_end
= 0;
1621 internal_ptr
->fix_scnlen
= 0;
1622 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1623 symbol_ptr
->u
.syment
.n_type
,
1624 symbol_ptr
->u
.syment
.n_sclass
,
1625 i
, symbol_ptr
->u
.syment
.n_numaux
,
1626 &(internal_ptr
->u
.auxent
));
1627 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1632 /* Free the raw symbols, but not the strings (if we have them). */
1633 obj_coff_keep_strings (abfd
) = true;
1634 if (! _bfd_coff_free_symbols (abfd
))
1637 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1640 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1641 && internal_ptr
->u
.syment
.n_numaux
> 0)
1643 /* make a file symbol point to the name in the auxent, since
1644 the text ".file" is redundant */
1645 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1647 /* the filename is a long one, point into the string table */
1648 if (string_table
== NULL
)
1650 string_table
= _bfd_coff_read_string_table (abfd
);
1651 if (string_table
== NULL
)
1655 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1658 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1662 /* ordinary short filename, put into memory anyway */
1663 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1664 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1670 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1672 /* This is a "short" name. Make it long. */
1673 unsigned long i
= 0;
1674 char *newstring
= NULL
;
1676 /* find the length of this string without walking into memory
1678 for (i
= 0; i
< 8; ++i
)
1680 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1683 } /* if end of string */
1684 } /* possible lengths of this string. */
1686 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1688 memset (newstring
, 0, i
);
1689 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1690 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1691 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1693 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1694 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1695 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1697 /* Long name already. Point symbol at the string in the
1699 if (string_table
== NULL
)
1701 string_table
= _bfd_coff_read_string_table (abfd
);
1702 if (string_table
== NULL
)
1705 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1708 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1712 /* Long name in debug section. Very similar. */
1713 if (debug_section
== NULL
)
1714 debug_section
= build_debug_section (abfd
);
1715 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1716 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1719 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1722 obj_raw_syments (abfd
) = internal
;
1723 BFD_ASSERT (obj_raw_syment_count (abfd
)
1724 == (unsigned int) (internal_ptr
- internal
));
1727 } /* coff_get_normalized_symtab() */
1730 coff_get_reloc_upper_bound (abfd
, asect
)
1734 if (bfd_get_format (abfd
) != bfd_object
)
1736 bfd_set_error (bfd_error_invalid_operation
);
1739 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1743 coff_make_empty_symbol (abfd
)
1746 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1749 memset (new, 0, sizeof *new);
1750 new->symbol
.section
= 0;
1752 new->lineno
= (alent
*) NULL
;
1753 new->done_lineno
= false;
1754 new->symbol
.the_bfd
= abfd
;
1755 return &new->symbol
;
1758 /* Make a debugging symbol. */
1761 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1766 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1769 /* @@ This shouldn't be using a constant multiplier. */
1770 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1773 new->symbol
.section
= bfd_abs_section_ptr
;
1774 new->symbol
.flags
= BSF_DEBUGGING
;
1775 new->lineno
= (alent
*) NULL
;
1776 new->done_lineno
= false;
1777 new->symbol
.the_bfd
= abfd
;
1778 return &new->symbol
;
1783 coff_get_symbol_info (abfd
, symbol
, ret
)
1788 bfd_symbol_info (symbol
, ret
);
1789 if (coffsymbol (symbol
)->native
!= NULL
1790 && coffsymbol (symbol
)->native
->fix_value
)
1792 combined_entry_type
*psym
;
1794 psym
= ((combined_entry_type
*)
1795 coffsymbol (symbol
)->native
->u
.syment
.n_value
);
1796 ret
->value
= (bfd_vma
) (psym
- obj_raw_syments (abfd
));
1800 /* Print out information about COFF symbol. */
1803 coff_print_symbol (abfd
, filep
, symbol
, how
)
1807 bfd_print_symbol_type how
;
1809 FILE *file
= (FILE *) filep
;
1813 case bfd_print_symbol_name
:
1814 fprintf (file
, "%s", symbol
->name
);
1817 case bfd_print_symbol_more
:
1818 fprintf (file
, "coff %s %s",
1819 coffsymbol (symbol
)->native
? "n" : "g",
1820 coffsymbol (symbol
)->lineno
? "l" : " ");
1823 case bfd_print_symbol_all
:
1824 if (coffsymbol (symbol
)->native
)
1828 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1829 combined_entry_type
*root
= obj_raw_syments (abfd
);
1830 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1832 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1834 if (! combined
->fix_value
)
1835 val
= (unsigned long) combined
->u
.syment
.n_value
;
1837 val
= ((unsigned long)
1838 ((combined_entry_type
*) combined
->u
.syment
.n_value
1842 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1843 combined
->u
.syment
.n_scnum
,
1844 combined
->u
.syment
.n_flags
,
1845 combined
->u
.syment
.n_type
,
1846 combined
->u
.syment
.n_sclass
,
1847 combined
->u
.syment
.n_numaux
,
1851 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1853 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1857 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1859 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1861 fprintf (file
, "\n");
1863 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
1866 switch (combined
->u
.syment
.n_sclass
)
1869 fprintf (file
, "File ");
1873 if (combined
->u
.syment
.n_type
== T_NULL
)
1874 /* probably a section symbol? */
1876 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1877 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
1878 auxp
->u
.auxent
.x_scn
.x_nreloc
,
1879 auxp
->u
.auxent
.x_scn
.x_nlinno
);
1882 /* else fall through */
1885 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1886 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1887 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1890 fprintf (file
, " endndx %ld",
1892 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1900 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1902 while (l
->line_number
)
1904 fprintf (file
, "\n%4d : 0x%lx",
1907 (l
->u
.offset
+ symbol
->section
->vma
)));
1914 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1915 fprintf (file
, " %-5s %s %s %s",
1916 symbol
->section
->name
,
1917 coffsymbol (symbol
)->native
? "n" : "g",
1918 coffsymbol (symbol
)->lineno
? "l" : " ",
1924 /* Provided a BFD, a section and an offset into the section, calculate
1925 and return the name of the source file and the line nearest to the
1930 coff_find_nearest_line (abfd
, section
, ignore_symbols
, offset
, filename_ptr
,
1931 functionname_ptr
, line_ptr
)
1934 asymbol
**ignore_symbols
;
1936 CONST
char **filename_ptr
;
1937 CONST
char **functionname_ptr
;
1938 unsigned int *line_ptr
;
1941 unsigned int line_base
;
1942 coff_data_type
*cof
= coff_data (abfd
);
1943 /* Run through the raw syments if available */
1944 combined_entry_type
*p
;
1945 combined_entry_type
*pend
;
1947 struct coff_section_tdata
*sec_data
;
1950 *functionname_ptr
= 0;
1953 /* Don't try and find line numbers in a non coff file */
1954 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1960 /* Find the first C_FILE symbol. */
1961 p
= cof
->raw_syments
;
1962 pend
= p
+ cof
->raw_syment_count
;
1965 if (p
->u
.syment
.n_sclass
== C_FILE
)
1967 p
+= 1 + p
->u
.syment
.n_numaux
;
1974 /* Look through the C_FILE symbols to find the best one. */
1975 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1976 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
1979 combined_entry_type
*p2
;
1981 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
1983 p2
+= 1 + p2
->u
.syment
.n_numaux
)
1985 if (p2
->u
.syment
.n_scnum
> 0
1987 == coff_section_from_bfd_index (abfd
,
1988 p2
->u
.syment
.n_scnum
)))
1990 if (p2
->u
.syment
.n_sclass
== C_FILE
)
1998 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
1999 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
2001 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
2002 maxdiff
= offset
- p2
->u
.syment
.n_value
;
2005 /* Avoid endless loops on erroneous files by ensuring that
2006 we always move forward in the file. */
2007 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
2010 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
2011 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
2016 /* Now wander though the raw linenumbers of the section */
2017 /* If we have been called on this section before, and the offset we
2018 want is further down then we can prime the lookup loop. */
2019 sec_data
= coff_section_data (abfd
, section
);
2020 if (sec_data
!= NULL
2022 && offset
>= sec_data
->offset
)
2025 *functionname_ptr
= sec_data
->function
;
2026 line_base
= sec_data
->line_base
;
2034 l
= §ion
->lineno
[i
];
2036 for (; i
< section
->lineno_count
; i
++)
2038 if (l
->line_number
== 0)
2040 /* Get the symbol this line number points at */
2041 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2042 if (coff
->symbol
.value
> offset
)
2044 *functionname_ptr
= coff
->symbol
.name
;
2047 combined_entry_type
*s
= coff
->native
;
2048 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2050 /* In XCOFF a debugging symbol can follow the function
2052 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
2053 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
2056 S should now point to the .bf of the function
2058 if (s
->u
.syment
.n_numaux
)
2061 The linenumber is stored in the auxent
2063 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
2064 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2065 *line_ptr
= line_base
;
2071 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
2073 *line_ptr
= l
->line_number
+ line_base
- 1;
2078 /* Cache the results for the next call. */
2079 if (sec_data
== NULL
)
2081 section
->used_by_bfd
=
2082 ((PTR
) bfd_zalloc (abfd
,
2083 sizeof (struct coff_section_tdata
)));
2084 sec_data
= (struct coff_section_tdata
*) section
->used_by_bfd
;
2086 if (sec_data
!= NULL
)
2088 sec_data
->offset
= offset
;
2090 sec_data
->function
= *functionname_ptr
;
2091 sec_data
->line_base
= line_base
;
2098 coff_sizeof_headers (abfd
, reloc
)
2106 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2110 size
= bfd_coff_filhsz (abfd
);
2113 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);