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 bfd_set_error (bfd_error_no_memory
);
80 strncpy (name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
81 name
[sizeof (hdr
->s_name
)] = 0;
83 return_section
= bfd_make_section_anyway (abfd
, name
);
84 if (return_section
== NULL
)
87 /* s_paddr is presumed to be = to s_vaddr */
89 return_section
->vma
= hdr
->s_vaddr
;
90 return_section
->lma
= return_section
->vma
;
91 return_section
->_raw_size
= hdr
->s_size
;
92 return_section
->filepos
= hdr
->s_scnptr
;
93 return_section
->rel_filepos
= hdr
->s_relptr
;
94 return_section
->reloc_count
= hdr
->s_nreloc
;
96 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
98 return_section
->line_filepos
= hdr
->s_lnnoptr
;
100 return_section
->lineno_count
= hdr
->s_nlnno
;
101 return_section
->userdata
= NULL
;
102 return_section
->next
= (asection
*) NULL
;
103 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
105 return_section
->target_index
= target_index
;
107 /* At least on i386-coff, the line number count for a shared library
108 section must be ignored. */
109 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
110 return_section
->lineno_count
= 0;
112 if (hdr
->s_nreloc
!= 0)
113 return_section
->flags
|= SEC_RELOC
;
114 /* FIXME: should this check 'hdr->s_size > 0' */
115 if (hdr
->s_scnptr
!= 0)
116 return_section
->flags
|= SEC_HAS_CONTENTS
;
120 /* Read in a COFF object and make it into a BFD. This is used by
123 static const bfd_target
*
124 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
127 struct internal_filehdr
*internal_f
;
128 struct internal_aouthdr
*internal_a
;
130 flagword oflags
= abfd
->flags
;
131 bfd_vma ostart
= bfd_get_start_address (abfd
);
133 size_t readsize
; /* length of file_info */
135 char *external_sections
;
137 if (!(internal_f
->f_flags
& F_RELFLG
))
138 abfd
->flags
|= HAS_RELOC
;
139 if ((internal_f
->f_flags
& F_EXEC
))
140 abfd
->flags
|= EXEC_P
;
141 if (!(internal_f
->f_flags
& F_LNNO
))
142 abfd
->flags
|= HAS_LINENO
;
143 if (!(internal_f
->f_flags
& F_LSYMS
))
144 abfd
->flags
|= HAS_LOCALS
;
146 /* FIXME: How can we set D_PAGED correctly? */
147 if ((internal_f
->f_flags
& F_EXEC
) != 0)
148 abfd
->flags
|= D_PAGED
;
150 bfd_get_symcount (abfd
) = internal_f
->f_nsyms
;
151 if (internal_f
->f_nsyms
)
152 abfd
->flags
|= HAS_SYMS
;
154 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
155 bfd_get_start_address (abfd
) = internal_a
->entry
;
157 bfd_get_start_address (abfd
) = 0;
159 /* Set up the tdata area. ECOFF uses its own routine, and overrides
161 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
165 scnhsz
= bfd_coff_scnhsz (abfd
);
166 readsize
= nscns
* scnhsz
;
167 external_sections
= (char *) bfd_alloc (abfd
, readsize
);
168 if (!external_sections
)
170 bfd_set_error (bfd_error_no_memory
);
174 if (bfd_read ((PTR
) external_sections
, 1, readsize
, abfd
) != readsize
)
177 /* Now copy data as required; construct all asections etc */
181 for (i
= 0; i
< nscns
; i
++)
183 struct internal_scnhdr tmp
;
184 bfd_coff_swap_scnhdr_in (abfd
,
185 (PTR
) (external_sections
+ i
* scnhsz
),
187 make_a_section_from_file (abfd
, &tmp
, i
+ 1);
191 /* make_abs_section (abfd); */
193 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
199 bfd_release (abfd
, tdata
);
200 abfd
->flags
= oflags
;
201 bfd_get_start_address (abfd
) = ostart
;
202 return (const bfd_target
*) NULL
;
205 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
206 not a COFF file. This is also used by ECOFF. */
216 struct internal_filehdr internal_f
;
217 struct internal_aouthdr internal_a
;
219 /* figure out how much to read */
220 filhsz
= bfd_coff_filhsz (abfd
);
221 aoutsz
= bfd_coff_aoutsz (abfd
);
223 filehdr
= bfd_alloc (abfd
, filhsz
);
226 if (bfd_read (filehdr
, 1, filhsz
, abfd
) != filhsz
)
228 if (bfd_get_error () != bfd_error_system_call
)
229 bfd_set_error (bfd_error_wrong_format
);
232 bfd_coff_swap_filehdr_in (abfd
, filehdr
, &internal_f
);
233 bfd_release (abfd
, filehdr
);
235 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false)
237 bfd_set_error (bfd_error_wrong_format
);
240 nscns
= internal_f
.f_nscns
;
242 if (internal_f
.f_opthdr
)
246 opthdr
= bfd_alloc (abfd
, aoutsz
);
249 if (bfd_read (opthdr
, 1, aoutsz
, abfd
) != aoutsz
)
253 bfd_coff_swap_aouthdr_in (abfd
, opthdr
, (PTR
) & internal_a
);
256 /* Seek past the opt hdr stuff */
257 if (bfd_seek (abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
261 return coff_real_object_p (abfd
, nscns
, &internal_f
,
262 (internal_f
.f_opthdr
!= 0
264 : (struct internal_aouthdr
*) NULL
));
267 /* Get the BFD section from a COFF symbol section number. */
270 coff_section_from_bfd_index (abfd
, index
)
274 struct sec
*answer
= abfd
->sections
;
277 return bfd_abs_section_ptr
;
278 if (index
== N_UNDEF
)
279 return bfd_und_section_ptr
;
280 if (index
== N_DEBUG
)
281 return bfd_abs_section_ptr
;
285 if (answer
->target_index
== index
)
287 answer
= answer
->next
;
290 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
291 has a bad symbol table in biglitpow.o. */
292 return bfd_und_section_ptr
;
295 /* Get the upper bound of a COFF symbol table. */
298 coff_get_symtab_upper_bound (abfd
)
301 if (!bfd_coff_slurp_symbol_table (abfd
))
304 return (bfd_get_symcount (abfd
) + 1) * (sizeof (coff_symbol_type
*));
308 /* Canonicalize a COFF symbol table. */
311 coff_get_symtab (abfd
, alocation
)
315 unsigned int counter
;
316 coff_symbol_type
*symbase
;
317 coff_symbol_type
**location
= (coff_symbol_type
**) alocation
;
319 if (!bfd_coff_slurp_symbol_table (abfd
))
322 symbase
= obj_symbols (abfd
);
323 counter
= bfd_get_symcount (abfd
);
324 while (counter
-- > 0)
325 *location
++ = symbase
++;
329 return bfd_get_symcount (abfd
);
332 /* Set lineno_count for the output sections of a COFF file. */
335 coff_count_linenumbers (abfd
)
338 unsigned int limit
= bfd_get_symcount (abfd
);
346 /* This may be from the backend linker, in which case the
347 lineno_count in the sections is correct. */
348 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
349 total
+= s
->lineno_count
;
353 for (s
= abfd
->sections
; s
!= NULL
; s
= s
->next
)
354 BFD_ASSERT (s
->lineno_count
== 0);
356 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
358 asymbol
*q_maybe
= *p
;
360 if (bfd_asymbol_flavour (q_maybe
) == bfd_target_coff_flavour
)
362 coff_symbol_type
*q
= coffsymbol (q_maybe
);
364 if (q
->lineno
!= NULL
)
366 /* This symbol has line numbers. Increment the owning
367 section's linenumber count. */
368 alent
*l
= q
->lineno
;
370 ++q
->symbol
.section
->output_section
->lineno_count
;
373 while (l
->line_number
!= 0)
376 ++q
->symbol
.section
->output_section
->lineno_count
;
386 /* Takes a bfd and a symbol, returns a pointer to the coff specific
387 area of the symbol if there is one. */
391 coff_symbol_from (ignore_abfd
, symbol
)
395 if (bfd_asymbol_flavour (symbol
) != bfd_target_coff_flavour
)
396 return (coff_symbol_type
*) NULL
;
398 if (bfd_asymbol_bfd (symbol
)->tdata
.coff_obj_data
== (coff_data_type
*) NULL
)
399 return (coff_symbol_type
*) NULL
;
401 return (coff_symbol_type
*) symbol
;
405 fixup_symbol_value (coff_symbol_ptr
, syment
)
406 coff_symbol_type
*coff_symbol_ptr
;
407 struct internal_syment
*syment
;
410 /* Normalize the symbol flags */
411 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
))
413 /* a common symbol is undefined with a value */
414 syment
->n_scnum
= N_UNDEF
;
415 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
417 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
)
419 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
421 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
))
423 syment
->n_scnum
= N_UNDEF
;
428 if (coff_symbol_ptr
->symbol
.section
)
431 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
434 coff_symbol_ptr
->symbol
.value
+
435 coff_symbol_ptr
->symbol
.section
->output_offset
+
436 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
441 /* This can happen, but I don't know why yet (steve@cygnus.com) */
442 syment
->n_scnum
= N_ABS
;
443 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
448 /* Run through all the symbols in the symbol table and work out what
449 their indexes into the symbol table will be when output.
451 Coff requires that each C_FILE symbol points to the next one in the
452 chain, and that the last one points to the first external symbol. We
456 coff_renumber_symbols (bfd_ptr
, first_undef
)
460 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
461 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
462 unsigned int native_index
= 0;
463 struct internal_syment
*last_file
= (struct internal_syment
*) NULL
;
464 unsigned int symbol_index
;
466 /* COFF demands that undefined symbols come after all other symbols.
467 Since we don't need to impose this extra knowledge on all our
468 client programs, deal with that here. Sort the symbol table;
469 just move the undefined symbols to the end, leaving the rest
470 alone. The O'Reilly book says that defined global symbols come
471 at the end before the undefined symbols, so we do that here as
473 /* @@ Do we have some condition we could test for, so we don't always
474 have to do this? I don't think relocatability is quite right, but
475 I'm not certain. [raeburn:19920508.1711EST] */
480 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
482 * (symbol_count
+ 1));
485 bfd_set_error (bfd_error_no_memory
);
488 bfd_ptr
->outsymbols
= newsyms
;
489 for (i
= 0; i
< symbol_count
; i
++)
490 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) != 0
491 || (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
492 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
| BSF_FUNCTION
))
494 *newsyms
++ = symbol_ptr_ptr
[i
];
496 for (i
= 0; i
< symbol_count
; i
++)
497 if (!bfd_is_und_section (symbol_ptr_ptr
[i
]->section
)
498 && ((symbol_ptr_ptr
[i
]->flags
& (BSF_GLOBAL
502 *newsyms
++ = symbol_ptr_ptr
[i
];
504 *first_undef
= newsyms
- bfd_ptr
->outsymbols
;
506 for (i
= 0; i
< symbol_count
; i
++)
507 if ((symbol_ptr_ptr
[i
]->flags
& BSF_NOT_AT_END
) == 0
508 && bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
509 *newsyms
++ = symbol_ptr_ptr
[i
];
510 *newsyms
= (asymbol
*) NULL
;
511 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
514 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
516 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
517 symbol_ptr_ptr
[symbol_index
]->udata
.i
= symbol_index
;
518 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
520 combined_entry_type
*s
= coff_symbol_ptr
->native
;
523 if (s
->u
.syment
.n_sclass
== C_FILE
)
525 if (last_file
!= (struct internal_syment
*) NULL
)
526 last_file
->n_value
= native_index
;
527 last_file
= &(s
->u
.syment
);
532 /* Modify the symbol values according to their section and
535 fixup_symbol_value (coff_symbol_ptr
, &(s
->u
.syment
));
537 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++)
538 s
[i
].offset
= native_index
++;
545 obj_conv_table_size (bfd_ptr
) = native_index
;
550 /* Run thorough the symbol table again, and fix it so that all
551 pointers to entries are changed to the entries' index in the output
555 coff_mangle_symbols (bfd_ptr
)
558 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
559 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
560 unsigned int symbol_index
;
562 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
564 coff_symbol_type
*coff_symbol_ptr
=
565 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
567 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
570 combined_entry_type
*s
= coff_symbol_ptr
->native
;
574 /* FIXME: We should use a union here. */
575 s
->u
.syment
.n_value
=
576 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
579 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
581 combined_entry_type
*a
= s
+ i
+ 1;
584 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
585 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
590 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
591 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
596 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
597 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
606 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
607 debug_string_section_p
, debug_string_size_p
)
610 combined_entry_type
*native
;
611 bfd_size_type
*string_size_p
;
612 asection
**debug_string_section_p
;
613 bfd_size_type
*debug_string_size_p
;
615 unsigned int name_length
;
616 union internal_auxent
*auxent
;
617 char *name
= (char *) (symbol
->name
);
619 if (name
== (char *) NULL
)
621 /* coff symbols always have names, so we'll make one up */
622 symbol
->name
= "strange";
623 name
= (char *) symbol
->name
;
625 name_length
= strlen (name
);
627 if (native
->u
.syment
.n_sclass
== C_FILE
628 && native
->u
.syment
.n_numaux
> 0)
630 strncpy (native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
631 auxent
= &(native
+ 1)->u
.auxent
;
633 if (bfd_coff_long_filenames (abfd
))
635 if (name_length
<= FILNMLEN
)
637 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
641 auxent
->x_file
.x_n
.x_offset
= *string_size_p
+ STRING_SIZE_SIZE
;
642 auxent
->x_file
.x_n
.x_zeroes
= 0;
643 *string_size_p
+= name_length
+ 1;
648 strncpy (auxent
->x_file
.x_fname
, name
, FILNMLEN
);
649 if (name_length
> FILNMLEN
)
651 name
[FILNMLEN
] = '\0';
657 if (name_length
<= SYMNMLEN
)
659 /* This name will fit into the symbol neatly */
660 strncpy (native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
662 else if (!bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
664 native
->u
.syment
._n
._n_n
._n_offset
= (*string_size_p
666 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
667 *string_size_p
+= name_length
+ 1;
674 /* This name should be written into the .debug section. For
675 some reason each name is preceded by a two byte length
676 and also followed by a null byte. FIXME: We assume that
677 the .debug section has already been created, and that it
679 if (*debug_string_section_p
== (asection
*) NULL
)
680 *debug_string_section_p
= bfd_get_section_by_name (abfd
, ".debug");
681 filepos
= bfd_tell (abfd
);
682 bfd_put_16 (abfd
, name_length
+ 1, buf
);
683 if (!bfd_set_section_contents (abfd
,
684 *debug_string_section_p
,
686 (file_ptr
) *debug_string_size_p
,
688 || !bfd_set_section_contents (abfd
,
689 *debug_string_section_p
,
691 ((file_ptr
) *debug_string_size_p
693 (bfd_size_type
) name_length
+ 1))
695 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
697 native
->u
.syment
._n
._n_n
._n_offset
= *debug_string_size_p
+ 2;
698 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
699 *debug_string_size_p
+= name_length
+ 3;
704 /* We need to keep track of the symbol index so that when we write out
705 the relocs we can get the index for a symbol. This method is a
708 #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
710 /* Write a symbol out to a COFF file. */
713 coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
714 debug_string_section_p
, debug_string_size_p
)
717 combined_entry_type
*native
;
718 unsigned int *written
;
719 bfd_size_type
*string_size_p
;
720 asection
**debug_string_section_p
;
721 bfd_size_type
*debug_string_size_p
;
723 unsigned int numaux
= native
->u
.syment
.n_numaux
;
724 int type
= native
->u
.syment
.n_type
;
725 int class = native
->u
.syment
.n_sclass
;
727 bfd_size_type symesz
;
729 if (native
->u
.syment
.n_sclass
== C_FILE
)
730 symbol
->flags
|= BSF_DEBUGGING
;
732 if (symbol
->flags
& BSF_DEBUGGING
)
734 native
->u
.syment
.n_scnum
= N_DEBUG
;
736 else if (bfd_is_abs_section (symbol
->section
))
738 native
->u
.syment
.n_scnum
= N_ABS
;
740 else if (bfd_is_und_section (symbol
->section
))
742 native
->u
.syment
.n_scnum
= N_UNDEF
;
746 native
->u
.syment
.n_scnum
=
747 symbol
->section
->output_section
->target_index
;
750 coff_fix_symbol_name (abfd
, symbol
, native
, string_size_p
,
751 debug_string_section_p
, debug_string_size_p
);
753 symesz
= bfd_coff_symesz (abfd
);
754 buf
= bfd_alloc (abfd
, symesz
);
757 bfd_set_error (bfd_error_no_memory
);
760 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
761 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
763 bfd_release (abfd
, buf
);
765 if (native
->u
.syment
.n_numaux
> 0)
767 bfd_size_type auxesz
;
770 auxesz
= bfd_coff_auxesz (abfd
);
771 buf
= bfd_alloc (abfd
, auxesz
);
774 bfd_set_error (bfd_error_no_memory
);
777 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
779 bfd_coff_swap_aux_out (abfd
,
780 &((native
+ j
+ 1)->u
.auxent
),
784 native
->u
.syment
.n_numaux
,
786 if (bfd_write (buf
, 1, auxesz
, abfd
) != auxesz
)
789 bfd_release (abfd
, buf
);
792 /* Store the index for use when we write out the relocs. */
793 set_index (symbol
, *written
);
795 *written
+= numaux
+ 1;
799 /* Write out a symbol to a COFF file that does not come from a COFF
800 file originally. This symbol may have been created by the linker,
801 or we may be linking a non COFF file to a COFF file. */
804 coff_write_alien_symbol (abfd
, symbol
, written
, string_size_p
,
805 debug_string_section_p
, debug_string_size_p
)
808 unsigned int *written
;
809 bfd_size_type
*string_size_p
;
810 asection
**debug_string_section_p
;
811 bfd_size_type
*debug_string_size_p
;
813 combined_entry_type
*native
;
814 combined_entry_type dummy
;
817 native
->u
.syment
.n_type
= T_NULL
;
818 native
->u
.syment
.n_flags
= 0;
819 if (bfd_is_und_section (symbol
->section
))
821 native
->u
.syment
.n_scnum
= N_UNDEF
;
822 native
->u
.syment
.n_value
= symbol
->value
;
824 else if (bfd_is_com_section (symbol
->section
))
826 native
->u
.syment
.n_scnum
= N_UNDEF
;
827 native
->u
.syment
.n_value
= symbol
->value
;
829 else if (symbol
->flags
& BSF_DEBUGGING
)
831 /* There isn't much point to writing out a debugging symbol
832 unless we are prepared to convert it into COFF debugging
833 format. So, we just ignore them. We must clobber the symbol
834 name to keep it from being put in the string table. */
840 native
->u
.syment
.n_scnum
=
841 symbol
->section
->output_section
->target_index
;
842 native
->u
.syment
.n_value
= (symbol
->value
843 + symbol
->section
->output_section
->vma
844 + symbol
->section
->output_offset
);
846 /* Copy the any flags from the the file header into the symbol.
849 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
850 if (c
!= (coff_symbol_type
*) NULL
)
851 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
855 native
->u
.syment
.n_type
= 0;
856 if (symbol
->flags
& BSF_LOCAL
)
857 native
->u
.syment
.n_sclass
= C_STAT
;
859 native
->u
.syment
.n_sclass
= C_EXT
;
860 native
->u
.syment
.n_numaux
= 0;
862 return coff_write_symbol (abfd
, symbol
, native
, written
, string_size_p
,
863 debug_string_section_p
, debug_string_size_p
);
866 /* Write a native symbol to a COFF file. */
869 coff_write_native_symbol (abfd
, symbol
, written
, string_size_p
,
870 debug_string_section_p
, debug_string_size_p
)
872 coff_symbol_type
*symbol
;
873 unsigned int *written
;
874 bfd_size_type
*string_size_p
;
875 asection
**debug_string_section_p
;
876 bfd_size_type
*debug_string_size_p
;
878 combined_entry_type
*native
= symbol
->native
;
879 alent
*lineno
= symbol
->lineno
;
881 /* If this symbol has an associated line number, we must store the
882 symbol index in the line number field. We also tag the auxent to
883 point to the right place in the lineno table. */
884 if (lineno
&& !symbol
->done_lineno
)
886 unsigned int count
= 0;
887 lineno
[count
].u
.offset
= *written
;
888 if (native
->u
.syment
.n_numaux
)
890 union internal_auxent
*a
= &((native
+ 1)->u
.auxent
);
892 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
893 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
896 /* Count and relocate all other linenumbers. */
898 while (lineno
[count
].line_number
!= 0)
902 I've been told this, but still need proof:
903 > The second bug is also in `bfd/coffcode.h'. This bug
904 > causes the linker to screw up the pc-relocations for
905 > all the line numbers in COFF code. This bug isn't only
906 > specific to A29K implementations, but affects all
907 > systems using COFF format binaries. Note that in COFF
908 > object files, the line number core offsets output by
909 > the assembler are relative to the start of each
910 > procedure, not to the start of the .text section. This
911 > patch relocates the line numbers relative to the
912 > `native->u.syment.n_value' instead of the section
914 > modular!olson@cs.arizona.edu (Jon Olson)
916 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
918 lineno
[count
].u
.offset
+=
919 (symbol
->symbol
.section
->output_section
->vma
920 + symbol
->symbol
.section
->output_offset
);
924 symbol
->done_lineno
= true;
926 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
927 count
* bfd_coff_linesz (abfd
);
930 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
,
931 string_size_p
, debug_string_section_p
,
932 debug_string_size_p
);
935 /* Write out the COFF symbols. */
938 coff_write_symbols (abfd
)
941 bfd_size_type string_size
;
942 asection
*debug_string_section
;
943 bfd_size_type debug_string_size
;
945 unsigned int limit
= bfd_get_symcount (abfd
);
946 unsigned int written
= 0;
950 debug_string_section
= NULL
;
951 debug_string_size
= 0;
953 /* Seek to the right place */
954 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0)
957 /* Output all the symbols we have */
960 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
962 asymbol
*symbol
= *p
;
963 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
965 if (c_symbol
== (coff_symbol_type
*) NULL
966 || c_symbol
->native
== (combined_entry_type
*) NULL
)
968 if (!coff_write_alien_symbol (abfd
, symbol
, &written
, &string_size
,
969 &debug_string_section
,
975 if (!coff_write_native_symbol (abfd
, c_symbol
, &written
,
976 &string_size
, &debug_string_section
,
982 obj_raw_syment_count (abfd
) = written
;
984 /* Now write out strings */
986 if (string_size
!= 0)
988 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
989 bfd_byte buffer
[STRING_SIZE_SIZE
];
991 #if STRING_SIZE_SIZE == 4
992 bfd_h_put_32 (abfd
, size
, buffer
);
994 #error Change bfd_h_put_32
996 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
998 for (p
= abfd
->outsymbols
, i
= 0;
1003 size_t name_length
= strlen (q
->name
);
1004 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
1007 /* Figure out whether the symbol name should go in the string
1008 table. Symbol names that are short enough are stored
1009 directly in the syment structure. File names permit a
1010 different, longer, length in the syment structure. On
1011 XCOFF, some symbol names are stored in the .debug section
1012 rather than in the string table. */
1014 if (c_symbol
== NULL
1015 || c_symbol
->native
== NULL
)
1017 /* This is not a COFF symbol, so it certainly is not a
1018 file name, nor does it go in the .debug section. */
1021 else if (bfd_coff_symname_in_debug (abfd
,
1022 &c_symbol
->native
->u
.syment
))
1024 /* This symbol name is in the XCOFF .debug section.
1025 Don't write it into the string table. */
1026 maxlen
= name_length
;
1028 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
1029 && c_symbol
->native
->u
.syment
.n_numaux
> 0)
1034 if (name_length
> maxlen
)
1036 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
1044 /* We would normally not write anything here, but we'll write
1045 out 4 so that any stupid coff reader which tries to read the
1046 string table even when there isn't one won't croak. */
1047 unsigned int size
= STRING_SIZE_SIZE
;
1048 bfd_byte buffer
[STRING_SIZE_SIZE
];
1050 #if STRING_SIZE_SIZE == 4
1051 bfd_h_put_32 (abfd
, size
, buffer
);
1053 #error Change bfd_h_put_32
1055 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
1056 != STRING_SIZE_SIZE
)
1060 /* Make sure the .debug section was created to be the correct size.
1061 We should create it ourselves on the fly, but we don't because
1062 BFD won't let us write to any section until we know how large all
1063 the sections are. We could still do it by making another pass
1064 over the symbols. FIXME. */
1065 BFD_ASSERT (debug_string_size
== 0
1066 || (debug_string_section
!= (asection
*) NULL
1067 && (BFD_ALIGN (debug_string_size
,
1068 1 << debug_string_section
->alignment_power
)
1069 == bfd_section_size (abfd
, debug_string_section
))));
1075 coff_write_linenumbers (abfd
)
1079 bfd_size_type linesz
;
1082 linesz
= bfd_coff_linesz (abfd
);
1083 buff
= bfd_alloc (abfd
, linesz
);
1086 bfd_set_error (bfd_error_no_memory
);
1089 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
)
1091 if (s
->lineno_count
)
1093 asymbol
**q
= abfd
->outsymbols
;
1094 if (bfd_seek (abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1096 /* Find all the linenumbers in this section */
1100 if (p
->section
->output_section
== s
)
1103 BFD_SEND (bfd_asymbol_bfd (p
), _get_lineno
,
1104 (bfd_asymbol_bfd (p
), p
));
1107 /* Found a linenumber entry, output */
1108 struct internal_lineno out
;
1109 memset ((PTR
) & out
, 0, sizeof (out
));
1111 out
.l_addr
.l_symndx
= l
->u
.offset
;
1112 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1113 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1116 while (l
->line_number
)
1118 out
.l_lnno
= l
->line_number
;
1119 out
.l_addr
.l_symndx
= l
->u
.offset
;
1120 bfd_coff_swap_lineno_out (abfd
, &out
, buff
);
1121 if (bfd_write (buff
, 1, linesz
, abfd
) != linesz
)
1131 bfd_release (abfd
, buff
);
1137 coff_get_lineno (ignore_abfd
, symbol
)
1141 return coffsymbol (symbol
)->lineno
;
1145 coff_section_symbol (abfd
, name
)
1149 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1151 combined_entry_type
*csym
;
1154 csym
= coff_symbol_from (abfd
, sym
)->native
;
1155 /* Make sure back-end COFF stuff is there. */
1160 coff_symbol_type sym
;
1161 /* @@FIXME This shouldn't use a fixed size!! */
1162 combined_entry_type e
[10];
1165 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
1168 bfd_set_error (bfd_error_no_error
);
1171 memset ((char *) f
, 0, sizeof (*f
));
1172 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1174 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1175 csym
[0].u
.syment
.n_numaux
= 1;
1176 /* SF_SET_STATICS (sym); @@ ??? */
1177 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1178 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1179 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1181 if (sec
->output_section
== NULL
)
1183 sec
->output_section
= sec
;
1184 sec
->output_offset
= 0;
1190 /* This function transforms the offsets into the symbol table into
1191 pointers to syments. */
1194 coff_pointerize_aux (abfd
, table_base
, symbol
, indaux
, auxent
)
1196 combined_entry_type
*table_base
;
1197 combined_entry_type
*symbol
;
1198 unsigned int indaux
;
1199 combined_entry_type
*auxent
;
1201 int type
= symbol
->u
.syment
.n_type
;
1202 int class = symbol
->u
.syment
.n_sclass
;
1204 if (coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1206 if ((*coff_backend_info (abfd
)->_bfd_coff_pointerize_aux_hook
)
1207 (abfd
, table_base
, symbol
, indaux
, auxent
))
1211 /* Don't bother if this is a file or a section */
1212 if (class == C_STAT
&& type
== T_NULL
)
1214 if (class == C_FILE
)
1217 /* Otherwise patch up */
1218 #define N_TMASK coff_data (abfd)->local_n_tmask
1219 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1220 if ((ISFCN (type
) || ISTAG (class) || class == C_BLOCK
)
1221 && auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
> 0)
1223 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
=
1224 table_base
+ auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1225 auxent
->fix_end
= 1;
1227 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1228 generate one, so we must be careful to ignore it. */
1229 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0)
1231 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1232 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1233 auxent
->fix_tag
= 1;
1237 /* Allocate space for the ".debug" section, and read it.
1238 We did not read the debug section until now, because
1239 we didn't want to go to the trouble until someone needed it. */
1242 build_debug_section (abfd
)
1245 char *debug_section
;
1248 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1252 bfd_set_error (bfd_error_no_debug_section
);
1256 debug_section
= (PTR
) bfd_alloc (abfd
,
1257 bfd_get_section_size_before_reloc (sect
));
1258 if (debug_section
== NULL
)
1260 bfd_set_error (bfd_error_no_memory
);
1264 /* Seek to the beginning of the `.debug' section and read it.
1265 Save the current position first; it is needed by our caller.
1266 Then read debug section and reset the file pointer. */
1268 position
= bfd_tell (abfd
);
1269 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1270 || (bfd_read (debug_section
,
1271 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1272 != bfd_get_section_size_before_reloc (sect
))
1273 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1275 return debug_section
;
1279 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1280 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1281 be \0-terminated. */
1283 copy_name (abfd
, name
, maxlen
)
1291 for (len
= 0; len
< maxlen
; ++len
)
1293 if (name
[len
] == '\0')
1299 if ((newname
= (PTR
) bfd_alloc (abfd
, len
+ 1)) == NULL
)
1301 bfd_set_error (bfd_error_no_memory
);
1304 strncpy (newname
, name
, len
);
1305 newname
[len
] = '\0';
1309 /* Read in the external symbols. */
1312 _bfd_coff_get_external_symbols (abfd
)
1315 bfd_size_type symesz
;
1319 if (obj_coff_external_syms (abfd
) != NULL
)
1322 symesz
= bfd_coff_symesz (abfd
);
1324 size
= obj_raw_syment_count (abfd
) * symesz
;
1326 syms
= malloc (size
);
1327 if (syms
== NULL
&& size
!= 0)
1329 bfd_set_error (bfd_error_no_memory
);
1333 if (bfd_seek (abfd
, obj_sym_filepos (abfd
), SEEK_SET
) != 0
1334 || bfd_read (syms
, size
, 1, abfd
) != size
)
1341 obj_coff_external_syms (abfd
) = syms
;
1346 /* Read in the external strings. The strings are not loaded until
1347 they are needed. This is because we have no simple way of
1348 detecting a missing string table in an archive. */
1351 _bfd_coff_read_string_table (abfd
)
1354 char extstrsize
[STRING_SIZE_SIZE
];
1358 if (obj_coff_strings (abfd
) != NULL
)
1359 return obj_coff_strings (abfd
);
1362 (obj_sym_filepos (abfd
)
1363 + obj_raw_syment_count (abfd
) * bfd_coff_symesz (abfd
)),
1367 if (bfd_read (extstrsize
, sizeof extstrsize
, 1, abfd
) != sizeof extstrsize
)
1369 if (bfd_get_error () != bfd_error_file_truncated
)
1372 /* There is no string table. */
1373 strsize
= STRING_SIZE_SIZE
;
1377 #if STRING_SIZE_SIZE == 4
1378 strsize
= bfd_h_get_32 (abfd
, (bfd_byte
*) extstrsize
);
1380 #error Change bfd_h_get_32
1384 strings
= malloc (strsize
);
1385 if (strings
== NULL
)
1387 bfd_set_error (bfd_error_no_memory
);
1391 if (bfd_read (strings
+ STRING_SIZE_SIZE
,
1392 strsize
- STRING_SIZE_SIZE
, 1, abfd
)
1393 != strsize
- STRING_SIZE_SIZE
)
1399 obj_coff_strings (abfd
) = strings
;
1404 /* Free up the external symbols and strings read from a COFF file. */
1407 _bfd_coff_free_symbols (abfd
)
1410 if (obj_coff_external_syms (abfd
) != NULL
1411 && ! obj_coff_keep_syms (abfd
))
1413 free (obj_coff_external_syms (abfd
));
1414 obj_coff_external_syms (abfd
) = NULL
;
1416 if (obj_coff_strings (abfd
) != NULL
1417 && ! obj_coff_keep_strings (abfd
))
1419 free (obj_coff_strings (abfd
));
1420 obj_coff_strings (abfd
) = NULL
;
1425 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1426 knit the symbol names into a normalized form. By normalized here I
1427 mean that all symbols have an n_offset pointer that points to a null-
1428 terminated string. */
1430 combined_entry_type
*
1431 coff_get_normalized_symtab (abfd
)
1434 combined_entry_type
*internal
;
1435 combined_entry_type
*internal_ptr
;
1436 combined_entry_type
*symbol_ptr
;
1437 combined_entry_type
*internal_end
;
1438 bfd_size_type symesz
;
1441 const char *string_table
= NULL
;
1442 char *debug_section
= NULL
;
1445 if (obj_raw_syments (abfd
) != NULL
)
1446 return obj_raw_syments (abfd
);
1448 size
= obj_raw_syment_count (abfd
) * sizeof (combined_entry_type
);
1449 internal
= (combined_entry_type
*) bfd_alloc (abfd
, size
);
1450 if (internal
== NULL
&& size
!= 0)
1452 bfd_set_error (bfd_error_no_memory
);
1455 internal_end
= internal
+ obj_raw_syment_count (abfd
);
1457 if (! _bfd_coff_get_external_symbols (abfd
))
1460 raw_src
= (char *) obj_coff_external_syms (abfd
);
1462 /* mark the end of the symbols */
1463 symesz
= bfd_coff_symesz (abfd
);
1464 raw_end
= (char *) raw_src
+ obj_raw_syment_count (abfd
) * symesz
;
1466 /* FIXME SOMEDAY. A string table size of zero is very weird, but
1467 probably possible. If one shows up, it will probably kill us. */
1469 /* Swap all the raw entries */
1470 for (internal_ptr
= internal
;
1472 raw_src
+= symesz
, internal_ptr
++)
1476 bfd_coff_swap_sym_in (abfd
, (PTR
) raw_src
,
1477 (PTR
) & internal_ptr
->u
.syment
);
1478 internal_ptr
->fix_value
= 0;
1479 internal_ptr
->fix_tag
= 0;
1480 internal_ptr
->fix_end
= 0;
1481 internal_ptr
->fix_scnlen
= 0;
1482 symbol_ptr
= internal_ptr
;
1485 i
< symbol_ptr
->u
.syment
.n_numaux
;
1491 internal_ptr
->fix_value
= 0;
1492 internal_ptr
->fix_tag
= 0;
1493 internal_ptr
->fix_end
= 0;
1494 internal_ptr
->fix_scnlen
= 0;
1495 bfd_coff_swap_aux_in (abfd
, (PTR
) raw_src
,
1496 symbol_ptr
->u
.syment
.n_type
,
1497 symbol_ptr
->u
.syment
.n_sclass
,
1498 i
, symbol_ptr
->u
.syment
.n_numaux
,
1499 &(internal_ptr
->u
.auxent
));
1500 coff_pointerize_aux (abfd
, internal
, symbol_ptr
, i
,
1505 /* Free the raw symbols, but not the strings (if we have them). */
1506 obj_coff_keep_strings (abfd
) = true;
1507 if (! _bfd_coff_free_symbols (abfd
))
1510 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1513 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1514 && internal_ptr
->u
.syment
.n_numaux
> 0)
1516 /* make a file symbol point to the name in the auxent, since
1517 the text ".file" is redundant */
1518 if ((internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0)
1520 /* the filename is a long one, point into the string table */
1521 if (string_table
== NULL
)
1523 string_table
= _bfd_coff_read_string_table (abfd
);
1524 if (string_table
== NULL
)
1528 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1531 + (internal_ptr
+ 1)->u
.auxent
.x_file
.x_n
.x_offset
));
1535 /* ordinary short filename, put into memory anyway */
1536 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1537 copy_name (abfd
, (internal_ptr
+ 1)->u
.auxent
.x_file
.x_fname
,
1543 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0)
1545 /* This is a "short" name. Make it long. */
1546 unsigned long i
= 0;
1547 char *newstring
= NULL
;
1549 /* find the length of this string without walking into memory
1551 for (i
= 0; i
< 8; ++i
)
1553 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0')
1556 } /* if end of string */
1557 } /* possible lengths of this string. */
1559 if ((newstring
= (PTR
) bfd_alloc (abfd
, ++i
)) == NULL
)
1561 bfd_set_error (bfd_error_no_memory
);
1564 memset (newstring
, 0, i
);
1565 strncpy (newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
- 1);
1566 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1567 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1569 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1570 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1571 else if (!bfd_coff_symname_in_debug (abfd
, &internal_ptr
->u
.syment
))
1573 /* Long name already. Point symbol at the string in the
1575 if (string_table
== NULL
)
1577 string_table
= _bfd_coff_read_string_table (abfd
);
1578 if (string_table
== NULL
)
1581 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1584 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
));
1588 /* Long name in debug section. Very similar. */
1589 if (debug_section
== NULL
)
1590 debug_section
= build_debug_section (abfd
);
1591 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1592 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1595 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1598 obj_raw_syments (abfd
) = internal
;
1599 BFD_ASSERT (obj_raw_syment_count (abfd
)
1600 == (unsigned int) (internal_ptr
- internal
));
1603 } /* coff_get_normalized_symtab() */
1606 coff_get_reloc_upper_bound (abfd
, asect
)
1610 if (bfd_get_format (abfd
) != bfd_object
)
1612 bfd_set_error (bfd_error_invalid_operation
);
1615 return (asect
->reloc_count
+ 1) * sizeof (arelent
*);
1619 coff_make_empty_symbol (abfd
)
1622 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1625 bfd_set_error (bfd_error_no_memory
);
1628 memset (new, 0, sizeof *new);
1629 new->symbol
.section
= 0;
1631 new->lineno
= (alent
*) NULL
;
1632 new->done_lineno
= false;
1633 new->symbol
.the_bfd
= abfd
;
1634 return &new->symbol
;
1637 /* Make a debugging symbol. */
1640 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1645 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc (abfd
, sizeof (coff_symbol_type
));
1648 bfd_set_error (bfd_error_no_memory
);
1651 /* @@ This shouldn't be using a constant multiplier. */
1652 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1655 bfd_set_error (bfd_error_no_memory
);
1658 new->symbol
.section
= bfd_abs_section_ptr
;
1659 new->symbol
.flags
= BSF_DEBUGGING
;
1660 new->lineno
= (alent
*) NULL
;
1661 new->done_lineno
= false;
1662 new->symbol
.the_bfd
= abfd
;
1663 return &new->symbol
;
1668 coff_get_symbol_info (abfd
, symbol
, ret
)
1673 bfd_symbol_info (symbol
, ret
);
1676 /* Print out information about COFF symbol. */
1679 coff_print_symbol (abfd
, filep
, symbol
, how
)
1683 bfd_print_symbol_type how
;
1685 FILE *file
= (FILE *) filep
;
1689 case bfd_print_symbol_name
:
1690 fprintf (file
, "%s", symbol
->name
);
1693 case bfd_print_symbol_more
:
1694 fprintf (file
, "coff %s %s",
1695 coffsymbol (symbol
)->native
? "n" : "g",
1696 coffsymbol (symbol
)->lineno
? "l" : " ");
1699 case bfd_print_symbol_all
:
1700 if (coffsymbol (symbol
)->native
)
1703 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1704 combined_entry_type
*root
= obj_raw_syments (abfd
);
1705 struct lineno_cache_entry
*l
= coffsymbol (symbol
)->lineno
;
1707 fprintf (file
, "[%3ld]", (long) (combined
- root
));
1710 "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x%08lx %s",
1711 combined
->u
.syment
.n_scnum
,
1712 combined
->u
.syment
.n_flags
,
1713 combined
->u
.syment
.n_type
,
1714 combined
->u
.syment
.n_sclass
,
1715 combined
->u
.syment
.n_numaux
,
1716 (unsigned long) combined
->u
.syment
.n_value
,
1719 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1721 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1725 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1727 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1729 fprintf (file
, "\n");
1731 if (bfd_coff_print_aux (abfd
, file
, root
, combined
, auxp
, aux
))
1734 switch (combined
->u
.syment
.n_sclass
)
1737 fprintf (file
, "File ");
1741 if (combined
->u
.syment
.n_type
== T_NULL
)
1742 /* probably a section symbol? */
1744 fprintf (file
, "AUX scnlen 0x%lx nreloc %d nlnno %d",
1745 (long) auxp
->u
.auxent
.x_scn
.x_scnlen
,
1746 auxp
->u
.auxent
.x_scn
.x_nreloc
,
1747 auxp
->u
.auxent
.x_scn
.x_nlinno
);
1750 /* else fall through */
1753 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1754 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1755 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1758 fprintf (file
, " endndx %ld",
1760 (auxp
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
1768 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1770 while (l
->line_number
)
1772 fprintf (file
, "\n%4d : 0x%lx",
1775 (l
->u
.offset
+ symbol
->section
->vma
)));
1782 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1783 fprintf (file
, " %-5s %s %s %s",
1784 symbol
->section
->name
,
1785 coffsymbol (symbol
)->native
? "n" : "g",
1786 coffsymbol (symbol
)->lineno
? "l" : " ",
1792 /* Provided a BFD, a section and an offset into the section, calculate
1793 and return the name of the source file and the line nearest to the
1798 coff_find_nearest_line (abfd
, section
, ignore_symbols
, offset
, filename_ptr
,
1799 functionname_ptr
, line_ptr
)
1802 asymbol
**ignore_symbols
;
1804 CONST
char **filename_ptr
;
1805 CONST
char **functionname_ptr
;
1806 unsigned int *line_ptr
;
1809 unsigned int line_base
;
1810 coff_data_type
*cof
= coff_data (abfd
);
1811 /* Run through the raw syments if available */
1812 combined_entry_type
*p
;
1813 combined_entry_type
*pend
;
1815 struct coff_section_tdata
*sec_data
;
1818 *functionname_ptr
= 0;
1821 /* Don't try and find line numbers in a non coff file */
1822 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1828 /* Find the first C_FILE symbol. */
1829 p
= cof
->raw_syments
;
1830 pend
= p
+ cof
->raw_syment_count
;
1833 if (p
->u
.syment
.n_sclass
== C_FILE
)
1835 p
+= 1 + p
->u
.syment
.n_numaux
;
1842 /* Look through the C_FILE symbols to find the best one. */
1843 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1844 maxdiff
= (bfd_vma
) 0 - (bfd_vma
) 1;
1847 combined_entry_type
*p2
;
1849 for (p2
= p
+ 1 + p
->u
.syment
.n_numaux
;
1851 p2
+= 1 + p2
->u
.syment
.n_numaux
)
1853 if (p2
->u
.syment
.n_scnum
> 0
1855 == coff_section_from_bfd_index (abfd
,
1856 p2
->u
.syment
.n_scnum
)))
1858 if (p2
->u
.syment
.n_sclass
== C_FILE
)
1866 && offset
>= (bfd_vma
) p2
->u
.syment
.n_value
1867 && offset
- (bfd_vma
) p2
->u
.syment
.n_value
< maxdiff
)
1869 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1870 maxdiff
= offset
- p2
->u
.syment
.n_value
;
1873 /* Avoid endless loops on erroneous files by ensuring that
1874 we always move forward in the file. */
1875 if (p
- cof
->raw_syments
>= p
->u
.syment
.n_value
)
1878 p
= cof
->raw_syments
+ p
->u
.syment
.n_value
;
1879 if (p
> pend
|| p
->u
.syment
.n_sclass
!= C_FILE
)
1884 /* Now wander though the raw linenumbers of the section */
1885 /* If we have been called on this section before, and the offset we
1886 want is further down then we can prime the lookup loop. */
1887 sec_data
= coff_section_data (abfd
, section
);
1888 if (sec_data
!= NULL
1890 && offset
>= sec_data
->offset
)
1893 *functionname_ptr
= sec_data
->function
;
1894 line_base
= sec_data
->line_base
;
1902 l
= §ion
->lineno
[i
];
1904 for (; i
< section
->lineno_count
; i
++)
1906 if (l
->line_number
== 0)
1908 /* Get the symbol this line number points at */
1909 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
1910 if (coff
->symbol
.value
> offset
)
1912 *functionname_ptr
= coff
->symbol
.name
;
1915 combined_entry_type
*s
= coff
->native
;
1916 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1918 /* In XCOFF a debugging symbol can follow the function
1920 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
1921 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1924 S should now point to the .bf of the function
1926 if (s
->u
.syment
.n_numaux
)
1929 The linenumber is stored in the auxent
1931 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1932 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1933 *line_ptr
= line_base
;
1939 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
1941 *line_ptr
= l
->line_number
+ line_base
- 1;
1946 /* Cache the results for the next call. */
1947 if (sec_data
== NULL
)
1949 section
->used_by_bfd
=
1950 ((PTR
) bfd_zalloc (abfd
,
1951 sizeof (struct coff_section_tdata
)));
1952 sec_data
= section
->used_by_bfd
;
1954 if (sec_data
!= NULL
)
1956 sec_data
->offset
= offset
;
1958 sec_data
->function
= *functionname_ptr
;
1959 sec_data
->line_base
= line_base
;
1966 coff_sizeof_headers (abfd
, reloc
)
1974 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1978 size
= bfd_coff_filhsz (abfd
);
1981 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);