1 /* Support for the generic parts of COFF, for BFD.
2 Copyright 1990, 1991, 1992, 1993, 1994 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., 675 Mass Ave, Cambridge, MA 02139, 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 boolean coff_write_symbol
PARAMS ((bfd
*, asymbol
*,
45 combined_entry_type
*,
47 static boolean coff_write_alien_symbol
PARAMS ((bfd
*, asymbol
*,
49 static boolean coff_write_native_symbol
PARAMS ((bfd
*, coff_symbol_type
*,
52 static asection bfd_debug_section
= { "*DEBUG*" };
54 #define STRING_SIZE_SIZE (4)
56 /* Take a section header read from a coff file (in HOST byte order),
57 and make a BFD "section" out of it. This is used by ECOFF. */
59 make_a_section_from_file (abfd
, hdr
, target_index
)
61 struct internal_scnhdr
*hdr
;
62 unsigned int target_index
;
64 asection
*return_section
;
67 /* Assorted wastage to null-terminate the name, thanks AT&T! */
68 name
= bfd_alloc(abfd
, sizeof (hdr
->s_name
)+1);
70 bfd_set_error (bfd_error_no_memory
);
73 strncpy(name
, (char *) &hdr
->s_name
[0], sizeof (hdr
->s_name
));
74 name
[sizeof (hdr
->s_name
)] = 0;
76 return_section
= bfd_make_section(abfd
, name
);
77 if (return_section
== NULL
)
78 return_section
= bfd_coff_make_section_hook (abfd
, name
);
80 /* Handle several sections of the same name. For example, if an executable
81 has two .bss sections, GDB better be able to find both of them
83 if (return_section
== NULL
)
84 return_section
= bfd_make_section_anyway (abfd
, name
);
86 if (return_section
== NULL
)
89 /* s_paddr is presumed to be = to s_vaddr */
91 return_section
->vma
= hdr
->s_vaddr
;
92 return_section
->_raw_size
= hdr
->s_size
;
93 return_section
->filepos
= hdr
->s_scnptr
;
94 return_section
->rel_filepos
= hdr
->s_relptr
;
95 return_section
->reloc_count
= hdr
->s_nreloc
;
97 bfd_coff_set_alignment_hook (abfd
, return_section
, hdr
);
99 return_section
->line_filepos
= hdr
->s_lnnoptr
;
101 return_section
->lineno_count
= hdr
->s_nlnno
;
102 return_section
->userdata
= NULL
;
103 return_section
->next
= (asection
*) NULL
;
104 return_section
->flags
= bfd_coff_styp_to_sec_flags_hook (abfd
, hdr
, name
);
106 return_section
->target_index
= target_index
;
108 /* At least on i386-coff, the line number count for a shared library
109 section must be ignored. */
110 if ((return_section
->flags
& SEC_COFF_SHARED_LIBRARY
) != 0)
111 return_section
->lineno_count
= 0;
113 if (hdr
->s_nreloc
!= 0)
114 return_section
->flags
|= SEC_RELOC
;
115 /* FIXME: should this check 'hdr->s_size > 0' */
116 if (hdr
->s_scnptr
!= 0)
117 return_section
->flags
|= SEC_HAS_CONTENTS
;
121 /* Read in a COFF object and make it into a BFD. This is used by
124 static const bfd_target
*
125 coff_real_object_p (abfd
, nscns
, internal_f
, internal_a
)
128 struct internal_filehdr
*internal_f
;
129 struct internal_aouthdr
*internal_a
;
132 size_t readsize
; /* length of file_info */
134 char *external_sections
;
136 /* Build a play area */
137 tdata
= bfd_coff_mkobject_hook (abfd
, (PTR
) internal_f
, (PTR
) internal_a
);
141 scnhsz
= bfd_coff_scnhsz (abfd
);
142 readsize
= nscns
* scnhsz
;
143 external_sections
= (char *)bfd_alloc(abfd
, readsize
);
144 if (!external_sections
)
146 bfd_set_error (bfd_error_no_memory
);
150 if (bfd_read((PTR
)external_sections
, 1, readsize
, abfd
) != readsize
) {
154 /* Now copy data as required; construct all asections etc */
157 for (i
= 0; i
< nscns
; i
++) {
158 struct internal_scnhdr tmp
;
159 bfd_coff_swap_scnhdr_in(abfd
, (PTR
) (external_sections
+ i
* scnhsz
),
161 make_a_section_from_file(abfd
,&tmp
, i
+1);
165 /* make_abs_section(abfd);*/
167 if (bfd_coff_set_arch_mach_hook (abfd
, (PTR
) internal_f
) == false)
170 if (!(internal_f
->f_flags
& F_RELFLG
))
171 abfd
->flags
|= HAS_RELOC
;
172 if ((internal_f
->f_flags
& F_EXEC
))
173 abfd
->flags
|= EXEC_P
;
174 if (!(internal_f
->f_flags
& F_LNNO
))
175 abfd
->flags
|= HAS_LINENO
;
176 if (!(internal_f
->f_flags
& F_LSYMS
))
177 abfd
->flags
|= HAS_LOCALS
;
179 /* FIXME: How can we set D_PAGED correctly? */
180 if ((internal_f
->f_flags
& F_EXEC
) != 0)
181 abfd
->flags
|= D_PAGED
;
183 obj_conv_table_size (abfd
) = bfd_get_symcount(abfd
) = internal_f
->f_nsyms
;
184 if (internal_f
->f_nsyms
)
185 abfd
->flags
|= HAS_SYMS
;
187 if (internal_a
!= (struct internal_aouthdr
*) NULL
)
188 bfd_get_start_address (abfd
) = internal_a
->entry
;
190 bfd_get_start_address (abfd
) = 0;
194 bfd_release(abfd
, tdata
);
195 return (const bfd_target
*)NULL
;
198 /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
199 not a COFF file. This is also used by ECOFF. */
209 struct internal_filehdr internal_f
;
210 struct internal_aouthdr internal_a
;
212 /* figure out how much to read */
213 filhsz
= bfd_coff_filhsz (abfd
);
214 aoutsz
= bfd_coff_aoutsz (abfd
);
216 filehdr
= bfd_alloc (abfd
, filhsz
);
219 if (bfd_read(filehdr
, 1, filhsz
, abfd
) != filhsz
)
221 if (bfd_get_error () != bfd_error_system_call
)
222 bfd_set_error (bfd_error_wrong_format
);
225 bfd_coff_swap_filehdr_in(abfd
, filehdr
, &internal_f
);
226 bfd_release (abfd
, filehdr
);
228 if (bfd_coff_bad_format_hook (abfd
, &internal_f
) == false) {
229 bfd_set_error (bfd_error_wrong_format
);
232 nscns
=internal_f
.f_nscns
;
234 if (internal_f
.f_opthdr
) {
237 opthdr
= bfd_alloc (abfd
, aoutsz
);
240 if (bfd_read(opthdr
, 1,aoutsz
, abfd
) != aoutsz
) {
243 bfd_coff_swap_aouthdr_in(abfd
, opthdr
, (PTR
)&internal_a
);
246 /* Seek past the opt hdr stuff */
247 if (bfd_seek(abfd
, (file_ptr
) (internal_f
.f_opthdr
+ filhsz
), SEEK_SET
)
251 return coff_real_object_p(abfd
, nscns
, &internal_f
,
252 (internal_f
.f_opthdr
!= 0
254 : (struct internal_aouthdr
*) NULL
));
257 /* Get the BFD section from a COFF symbol section number. */
260 coff_section_from_bfd_index (abfd
, index
)
264 struct sec
*answer
= abfd
->sections
;
268 return bfd_abs_section_ptr
;
270 if (index
== N_UNDEF
)
272 return bfd_und_section_ptr
;
276 return &bfd_debug_section
;
281 if (answer
->target_index
== index
)
283 answer
= answer
->next
;
286 /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
287 has a bad symbol table in biglitpow.o. */
288 return bfd_und_section_ptr
;
291 /* Get the upper bound of a COFF symbol table. */
294 coff_get_symtab_upper_bound(abfd
)
297 if (!bfd_coff_slurp_symbol_table(abfd
))
300 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
304 /* Canonicalize a COFF symbol table. */
307 coff_get_symtab (abfd
, alocation
)
311 unsigned int counter
= 0;
312 coff_symbol_type
*symbase
;
313 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
314 if (!bfd_coff_slurp_symbol_table(abfd
))
317 symbase
= obj_symbols(abfd
);
318 while (counter
< bfd_get_symcount(abfd
))
320 /* This nasty code looks at the symbol to decide whether or
321 not it is descibes a constructor/destructor entry point. It
322 is structured this way to (hopefully) speed non matches */
324 if (0 && symbase
->symbol
.name
[9] == '$')
326 bfd_constructor_entry(abfd
,
327 (asymbol
**)location
,
328 symbase
->symbol
.name
[10] == 'I' ?
332 *(location
++) = symbase
++;
336 return bfd_get_symcount(abfd
);
339 /* Set lineno_count for the output sections of a COFF file. */
342 coff_count_linenumbers (abfd
)
345 unsigned int limit
= bfd_get_symcount(abfd
);
350 asection
*s
= abfd
->sections
->output_section
;
352 BFD_ASSERT(s
->lineno_count
== 0);
358 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
359 asymbol
*q_maybe
= *p
;
360 if (bfd_asymbol_flavour(q_maybe
) == bfd_target_coff_flavour
) {
361 coff_symbol_type
*q
= coffsymbol(q_maybe
);
364 This symbol has a linenumber, increment the owning
365 section's linenumber count
367 alent
*l
= q
->lineno
;
368 q
->symbol
.section
->output_section
->lineno_count
++;
371 while (l
->line_number
) {
373 q
->symbol
.section
->output_section
->lineno_count
++;
382 /* Takes a bfd and a symbol, returns a pointer to the coff specific
383 area of the symbol if there is one. */
387 coff_symbol_from (ignore_abfd
, symbol
)
391 if (bfd_asymbol_flavour(symbol
) != bfd_target_coff_flavour
)
392 return (coff_symbol_type
*)NULL
;
394 if (bfd_asymbol_bfd(symbol
)->tdata
.coff_obj_data
== (coff_data_type
*)NULL
)
395 return (coff_symbol_type
*)NULL
;
397 return (coff_symbol_type
*) symbol
;
401 fixup_symbol_value (coff_symbol_ptr
, syment
)
402 coff_symbol_type
*coff_symbol_ptr
;
403 struct internal_syment
*syment
;
406 /* Normalize the symbol flags */
407 if (bfd_is_com_section (coff_symbol_ptr
->symbol
.section
)) {
408 /* a common symbol is undefined with a value */
409 syment
->n_scnum
= N_UNDEF
;
410 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
412 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
413 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
415 else if (bfd_is_und_section (coff_symbol_ptr
->symbol
.section
)) {
416 syment
->n_scnum
= N_UNDEF
;
420 if (coff_symbol_ptr
->symbol
.section
) {
422 coff_symbol_ptr
->symbol
.section
->output_section
->target_index
;
425 coff_symbol_ptr
->symbol
.value
+
426 coff_symbol_ptr
->symbol
.section
->output_offset
+
427 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
431 /* This can happen, but I don't know why yet (steve@cygnus.com) */
432 syment
->n_scnum
= N_ABS
;
433 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
438 /* run through all the symbols in the symbol table and work out what
439 their indexes into the symbol table will be when output
441 Coff requires that each C_FILE symbol points to the next one in the
442 chain, and that the last one points to the first external symbol. We
447 coff_renumber_symbols (bfd_ptr
)
450 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
451 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
452 unsigned int native_index
= 0;
453 struct internal_syment
*last_file
= (struct internal_syment
*)NULL
;
454 unsigned int symbol_index
;
456 /* COFF demands that undefined symbols come after all other symbols.
457 Since we don't need to impose this extra knowledge on all our client
458 programs, deal with that here. Sort the symbol table; just move the
459 undefined symbols to the end, leaving the rest alone. */
460 /* @@ Do we have some condition we could test for, so we don't always
461 have to do this? I don't think relocatability is quite right, but
462 I'm not certain. [raeburn:19920508.1711EST] */
467 newsyms
= (asymbol
**) bfd_alloc_by_size_t (bfd_ptr
,
469 * (symbol_count
+ 1));
472 bfd_set_error (bfd_error_no_memory
);
475 bfd_ptr
->outsymbols
= newsyms
;
476 for (i
= 0; i
< symbol_count
; i
++)
477 if (! bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
478 *newsyms
++ = symbol_ptr_ptr
[i
];
479 for (i
= 0; i
< symbol_count
; i
++)
480 if (bfd_is_und_section (symbol_ptr_ptr
[i
]->section
))
481 *newsyms
++ = symbol_ptr_ptr
[i
];
482 *newsyms
= (asymbol
*) NULL
;
483 symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
486 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
488 coff_symbol_type
*coff_symbol_ptr
= coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
489 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
) {
490 combined_entry_type
*s
= coff_symbol_ptr
->native
;
493 if (s
->u
.syment
.n_sclass
== C_FILE
)
495 if (last_file
!= (struct internal_syment
*)NULL
) {
496 last_file
->n_value
= native_index
;
498 last_file
= &(s
->u
.syment
);
502 /* Modify the symbol values according to their section and
505 fixup_symbol_value(coff_symbol_ptr
, &(s
->u
.syment
));
507 for (i
= 0; i
< s
->u
.syment
.n_numaux
+ 1; i
++) {
508 s
[i
].offset
= native_index
++;
515 obj_conv_table_size (bfd_ptr
) = native_index
;
520 Run thorough the symbol table again, and fix it so that all pointers to
521 entries are changed to the entries' index in the output symbol table.
525 coff_mangle_symbols (bfd_ptr
)
528 unsigned int symbol_count
= bfd_get_symcount (bfd_ptr
);
529 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
530 unsigned int symbol_index
;
532 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++)
534 coff_symbol_type
*coff_symbol_ptr
=
535 coff_symbol_from (bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
537 if (coff_symbol_ptr
&& coff_symbol_ptr
->native
)
540 combined_entry_type
*s
= coff_symbol_ptr
->native
;
544 /* FIXME: We should use a union here. */
545 s
->u
.syment
.n_value
=
546 ((combined_entry_type
*) s
->u
.syment
.n_value
)->offset
;
549 for (i
= 0; i
< s
->u
.syment
.n_numaux
; i
++)
551 combined_entry_type
*a
= s
+ i
+ 1;
554 a
->u
.auxent
.x_sym
.x_tagndx
.l
=
555 a
->u
.auxent
.x_sym
.x_tagndx
.p
->offset
;
560 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
=
561 a
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
->offset
;
566 a
->u
.auxent
.x_csect
.x_scnlen
.l
=
567 a
->u
.auxent
.x_csect
.x_scnlen
.p
->offset
;
575 static bfd_size_type string_size
;
576 static bfd_size_type debug_string_size
;
577 static asection
*debug_string_section
;
580 coff_fix_symbol_name (abfd
, symbol
, native
)
583 combined_entry_type
*native
;
585 unsigned int name_length
;
586 union internal_auxent
*auxent
;
587 char * name
= ( char *)(symbol
->name
);
589 if (name
== (char *) NULL
) {
590 /* coff symbols always have names, so we'll make one up */
591 symbol
->name
= "strange";
592 name
= (char *)symbol
->name
;
594 name_length
= strlen(name
);
596 if (native
->u
.syment
.n_sclass
== C_FILE
) {
597 strncpy(native
->u
.syment
._n
._n_name
, ".file", SYMNMLEN
);
598 auxent
= &(native
+1)->u
.auxent
;
600 if (bfd_coff_long_filenames (abfd
)) {
601 if (name_length
<= FILNMLEN
) {
602 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
605 auxent
->x_file
.x_n
.x_offset
= string_size
+ STRING_SIZE_SIZE
;
606 auxent
->x_file
.x_n
.x_zeroes
= 0;
607 string_size
+= name_length
+ 1;
611 strncpy(auxent
->x_file
.x_fname
, name
, FILNMLEN
);
612 if (name_length
> FILNMLEN
) {
613 name
[FILNMLEN
] = '\0';
618 { /* NOT A C_FILE SYMBOL */
619 if (name_length
<= SYMNMLEN
)
621 /* This name will fit into the symbol neatly */
622 strncpy(native
->u
.syment
._n
._n_name
, symbol
->name
, SYMNMLEN
);
624 else if (! bfd_coff_symname_in_debug (abfd
, &native
->u
.syment
))
626 native
->u
.syment
._n
._n_n
._n_offset
= string_size
+ STRING_SIZE_SIZE
;
627 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
628 string_size
+= name_length
+ 1;
635 /* This name should be written into the .debug section. For
636 some reason each name is preceded by a two byte length
637 and also followed by a null byte. FIXME: We assume that
638 the .debug section has already been created, and that it
640 if (debug_string_section
== (asection
*) NULL
)
641 debug_string_section
= bfd_get_section_by_name (abfd
, ".debug");
642 filepos
= bfd_tell (abfd
);
643 bfd_put_16 (abfd
, name_length
+ 1, buf
);
644 if (! bfd_set_section_contents (abfd
,
645 debug_string_section
,
647 (file_ptr
) debug_string_size
,
649 || ! bfd_set_section_contents (abfd
,
650 debug_string_section
,
652 (file_ptr
) debug_string_size
+ 2,
653 (bfd_size_type
) name_length
+ 1))
655 if (bfd_seek (abfd
, filepos
, SEEK_SET
) != 0)
657 native
->u
.syment
._n
._n_n
._n_offset
= debug_string_size
+ 2;
658 native
->u
.syment
._n
._n_n
._n_zeroes
= 0;
659 debug_string_size
+= name_length
+ 3;
664 /* We need to keep track of the symbol index so that when we write out
665 the relocs we can get the index for a symbol. This method is a
668 #define set_index(symbol, idx) ((symbol)->udata = (PTR) (idx))
670 /* Write a symbol out to a COFF file. */
673 coff_write_symbol (abfd
, symbol
, native
, written
)
676 combined_entry_type
*native
;
677 unsigned int *written
;
679 unsigned int numaux
= native
->u
.syment
.n_numaux
;
680 int type
= native
->u
.syment
.n_type
;
681 int class = native
->u
.syment
.n_sclass
;
683 bfd_size_type symesz
;
685 /* @@ bfd_debug_section isn't accessible outside this file, but we
686 know that C_FILE symbols belong there. So move them. */
687 if (native
->u
.syment
.n_sclass
== C_FILE
)
688 symbol
->section
= &bfd_debug_section
;
690 if (bfd_is_abs_section (symbol
->section
))
692 native
->u
.syment
.n_scnum
= N_ABS
;
694 else if (symbol
->section
== &bfd_debug_section
)
696 native
->u
.syment
.n_scnum
= N_DEBUG
;
698 else if (bfd_is_und_section (symbol
->section
))
700 native
->u
.syment
.n_scnum
= N_UNDEF
;
704 native
->u
.syment
.n_scnum
=
705 symbol
->section
->output_section
->target_index
;
708 coff_fix_symbol_name (abfd
, symbol
, native
);
710 symesz
= bfd_coff_symesz (abfd
);
711 buf
= bfd_alloc (abfd
, symesz
);
714 bfd_set_error (bfd_error_no_memory
);
717 bfd_coff_swap_sym_out (abfd
, &native
->u
.syment
, buf
);
718 if (bfd_write (buf
, 1, symesz
, abfd
) != symesz
)
720 bfd_release (abfd
, buf
);
722 if (native
->u
.syment
.n_numaux
> 0)
724 bfd_size_type auxesz
;
727 auxesz
= bfd_coff_auxesz (abfd
);
728 buf
= bfd_alloc (abfd
, auxesz
);
731 bfd_set_error (bfd_error_no_memory
);
734 for (j
= 0; j
< native
->u
.syment
.n_numaux
; j
++)
736 bfd_coff_swap_aux_out (abfd
,
737 &((native
+ j
+ 1)->u
.auxent
),
741 native
->u
.syment
.n_numaux
,
743 if (bfd_write (buf
, 1, auxesz
, abfd
)!= auxesz
)
746 bfd_release (abfd
, buf
);
749 /* Store the index for use when we write out the relocs. */
750 set_index (symbol
, *written
);
752 *written
+= numaux
+ 1;
756 /* Write out a symbol to a COFF file that does not come from a COFF
757 file originally. This symbol may have been created by the linker,
758 or we may be linking a non COFF file to a COFF file. */
761 coff_write_alien_symbol (abfd
, symbol
, written
)
764 unsigned int *written
;
766 combined_entry_type
*native
;
767 combined_entry_type dummy
;
770 native
->u
.syment
.n_type
= T_NULL
;
771 native
->u
.syment
.n_flags
= 0;
772 if (bfd_is_und_section (symbol
->section
))
774 native
->u
.syment
.n_scnum
= N_UNDEF
;
775 native
->u
.syment
.n_value
= symbol
->value
;
777 else if (bfd_is_com_section (symbol
->section
))
779 native
->u
.syment
.n_scnum
= N_UNDEF
;
780 native
->u
.syment
.n_value
= symbol
->value
;
782 else if (symbol
->flags
& BSF_DEBUGGING
)
784 /* There isn't much point to writing out a debugging symbol
785 unless we are prepared to convert it into COFF debugging
786 format. So, we just ignore them. */
791 native
->u
.syment
.n_scnum
=
792 symbol
->section
->output_section
->target_index
;
793 native
->u
.syment
.n_value
= (symbol
->value
794 + symbol
->section
->output_section
->vma
795 + symbol
->section
->output_offset
);
797 /* Copy the any flags from the the file header into the symbol.
800 coff_symbol_type
*c
= coff_symbol_from (abfd
, symbol
);
801 if (c
!= (coff_symbol_type
*) NULL
)
802 native
->u
.syment
.n_flags
= bfd_asymbol_bfd (&c
->symbol
)->flags
;
806 native
->u
.syment
.n_type
= 0;
807 if (symbol
->flags
& BSF_LOCAL
)
808 native
->u
.syment
.n_sclass
= C_STAT
;
810 native
->u
.syment
.n_sclass
= C_EXT
;
811 native
->u
.syment
.n_numaux
= 0;
813 return coff_write_symbol (abfd
, symbol
, native
, written
);
816 /* Write a native symbol to a COFF file. */
819 coff_write_native_symbol (abfd
, symbol
, written
)
821 coff_symbol_type
*symbol
;
822 unsigned int *written
;
824 combined_entry_type
*native
= symbol
->native
;
825 alent
*lineno
= symbol
->lineno
;
827 /* If this symbol has an associated line number, we must store the
828 symbol index in the line number field. We also tag the auxent to
829 point to the right place in the lineno table. */
830 if (lineno
&& !symbol
->done_lineno
)
832 unsigned int count
= 0;
833 lineno
[count
].u
.offset
= *written
;
834 if (native
->u
.syment
.n_numaux
)
836 union internal_auxent
*a
= &((native
+1)->u
.auxent
);
838 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
839 symbol
->symbol
.section
->output_section
->moving_line_filepos
;
842 /* Count and relocate all other linenumbers. */
844 while (lineno
[count
].line_number
!= 0)
848 I've been told this, but still need proof:
849 > The second bug is also in `bfd/coffcode.h'. This bug
850 > causes the linker to screw up the pc-relocations for
851 > all the line numbers in COFF code. This bug isn't only
852 > specific to A29K implementations, but affects all
853 > systems using COFF format binaries. Note that in COFF
854 > object files, the line number core offsets output by
855 > the assembler are relative to the start of each
856 > procedure, not to the start of the .text section. This
857 > patch relocates the line numbers relative to the
858 > `native->u.syment.n_value' instead of the section
860 > modular!olson@cs.arizona.edu (Jon Olson)
862 lineno
[count
].u
.offset
+= native
->u
.syment
.n_value
;
864 lineno
[count
].u
.offset
+=
865 (symbol
->symbol
.section
->output_section
->vma
866 + symbol
->symbol
.section
->output_offset
);
870 symbol
->done_lineno
= true;
872 symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
873 count
* bfd_coff_linesz (abfd
);
876 return coff_write_symbol (abfd
, &(symbol
->symbol
), native
, written
);
879 /* Write out the COFF symbols. */
882 coff_write_symbols (abfd
)
886 unsigned int limit
= bfd_get_symcount(abfd
);
887 unsigned int written
= 0;
891 debug_string_size
= 0;
893 /* Seek to the right place */
894 if (bfd_seek (abfd
, obj_sym_filepos(abfd
), SEEK_SET
) != 0)
897 /* Output all the symbols we have */
900 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++)
902 asymbol
*symbol
= *p
;
903 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, symbol
);
905 if (c_symbol
== (coff_symbol_type
*) NULL
906 || c_symbol
->native
== (combined_entry_type
*)NULL
)
908 if (! coff_write_alien_symbol (abfd
, symbol
, &written
))
913 if (! coff_write_native_symbol (abfd
, c_symbol
, &written
))
918 bfd_get_symcount (abfd
) = written
;
920 /* Now write out strings */
922 if (string_size
!= 0)
924 unsigned int size
= string_size
+ STRING_SIZE_SIZE
;
925 bfd_byte buffer
[STRING_SIZE_SIZE
];
927 #if STRING_SIZE_SIZE == 4
928 bfd_h_put_32 (abfd
, size
, buffer
);
930 #error Change bfd_h_put_32
932 if (bfd_write ((PTR
) buffer
, 1, sizeof (buffer
), abfd
) != sizeof (buffer
))
934 for (p
= abfd
->outsymbols
, i
= 0;
939 size_t name_length
= strlen (q
->name
);
940 coff_symbol_type
*c_symbol
= coff_symbol_from (abfd
, q
);
943 /* Figure out whether the symbol name should go in the string
944 table. Symbol names that are short enough are stored
945 directly in the syment structure. File names permit a
946 different, longer, length in the syment structure. On
947 XCOFF, some symbol names are stored in the .debug section
948 rather than in the string table. */
951 || c_symbol
->native
== NULL
)
953 /* This is not a COFF symbol, so it certainly is not a
954 file name, nor does it go in the .debug section. */
957 else if (bfd_coff_symname_in_debug (abfd
,
958 &c_symbol
->native
->u
.syment
))
960 /* This symbol name is in the XCOFF .debug section.
961 Don't write it into the string table. */
962 maxlen
= name_length
;
964 else if (c_symbol
->native
->u
.syment
.n_sclass
== C_FILE
)
969 if (name_length
> maxlen
)
971 if (bfd_write ((PTR
) (q
->name
), 1, name_length
+ 1, abfd
)
979 /* We would normally not write anything here, but we'll write
980 out 4 so that any stupid coff reader which tries to read the
981 string table even when there isn't one won't croak. */
982 unsigned int size
= STRING_SIZE_SIZE
;
983 bfd_byte buffer
[STRING_SIZE_SIZE
];
985 #if STRING_SIZE_SIZE == 4
986 bfd_h_put_32 (abfd
, size
, buffer
);
988 #error Change bfd_h_put_32
990 if (bfd_write ((PTR
) buffer
, 1, STRING_SIZE_SIZE
, abfd
)
995 /* Make sure the .debug section was created to be the correct size.
996 We should create it ourselves on the fly, but we don't because
997 BFD won't let us write to any section until we know how large all
998 the sections are. We could still do it by making another pass
999 over the symbols. FIXME. */
1000 BFD_ASSERT (debug_string_size
== 0
1001 || (debug_string_section
!= (asection
*) NULL
1002 && (BFD_ALIGN (debug_string_size
,
1003 1 << debug_string_section
->alignment_power
)
1004 == bfd_section_size (abfd
, debug_string_section
))));
1010 coff_write_linenumbers (abfd
)
1014 bfd_size_type linesz
;
1017 linesz
= bfd_coff_linesz (abfd
);
1018 buff
= bfd_alloc (abfd
, linesz
);
1021 bfd_set_error (bfd_error_no_memory
);
1024 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
1025 if (s
->lineno_count
) {
1026 asymbol
**q
= abfd
->outsymbols
;
1027 if (bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
) != 0)
1029 /* Find all the linenumbers in this section */
1032 if (p
->section
->output_section
== s
) {
1034 BFD_SEND(bfd_asymbol_bfd(p
), _get_lineno
, (bfd_asymbol_bfd(p
), p
));
1036 /* Found a linenumber entry, output */
1037 struct internal_lineno out
;
1038 memset( (PTR
)&out
, 0, sizeof(out
));
1040 out
.l_addr
.l_symndx
= l
->u
.offset
;
1041 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
1042 if (bfd_write(buff
, 1, linesz
, abfd
) != linesz
)
1045 while (l
->line_number
) {
1046 out
.l_lnno
= l
->line_number
;
1047 out
.l_addr
.l_symndx
= l
->u
.offset
;
1048 bfd_coff_swap_lineno_out(abfd
, &out
, buff
);
1049 if (bfd_write(buff
, 1, linesz
, abfd
) != linesz
)
1059 bfd_release (abfd
, buff
);
1065 coff_get_lineno (ignore_abfd
, symbol
)
1069 return coffsymbol(symbol
)->lineno
;
1073 coff_section_symbol (abfd
, name
)
1077 asection
*sec
= bfd_make_section_old_way (abfd
, name
);
1079 combined_entry_type
*csym
;
1082 csym
= coff_symbol_from (abfd
, sym
)->native
;
1083 /* Make sure back-end COFF stuff is there. */
1087 coff_symbol_type sym
;
1088 /* @@FIXME This shouldn't use a fixed size!! */
1089 combined_entry_type e
[10];
1092 f
= (struct foo
*) bfd_alloc_by_size_t (abfd
, sizeof (*f
));
1095 bfd_set_error (bfd_error_no_error
);
1098 memset ((char *) f
, 0, sizeof (*f
));
1099 coff_symbol_from (abfd
, sym
)->native
= csym
= f
->e
;
1101 csym
[0].u
.syment
.n_sclass
= C_STAT
;
1102 csym
[0].u
.syment
.n_numaux
= 1;
1103 /* SF_SET_STATICS (sym); @@ ??? */
1104 csym
[1].u
.auxent
.x_scn
.x_scnlen
= sec
->_raw_size
;
1105 csym
[1].u
.auxent
.x_scn
.x_nreloc
= sec
->reloc_count
;
1106 csym
[1].u
.auxent
.x_scn
.x_nlinno
= sec
->lineno_count
;
1108 if (sec
->output_section
== NULL
)
1110 sec
->output_section
= sec
;
1111 sec
->output_offset
= 0;
1117 /* This function transforms the offsets into the symbol table into
1118 pointers to syments. */
1121 coff_pointerize_aux (abfd
, table_base
, type
, class, auxent
)
1123 combined_entry_type
*table_base
;
1126 combined_entry_type
*auxent
;
1128 /* Don't bother if this is a file or a section */
1129 if (class == C_STAT
&& type
== T_NULL
) return;
1130 if (class == C_FILE
) return;
1132 /* Otherwise patch up */
1133 #define N_TMASK coff_data (abfd)->local_n_tmask
1134 #define N_BTSHFT coff_data (abfd)->local_n_btshft
1135 if (ISFCN(type
) || ISTAG(class) || class == C_BLOCK
) {
1136 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.p
= table_base
+
1137 auxent
->u
.auxent
.x_sym
.x_fcnary
.x_fcn
.x_endndx
.l
;
1138 auxent
->fix_end
= 1;
1140 /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
1141 generate one, so we must be careful to ignore it. */
1142 if (auxent
->u
.auxent
.x_sym
.x_tagndx
.l
> 0) {
1143 auxent
->u
.auxent
.x_sym
.x_tagndx
.p
=
1144 table_base
+ auxent
->u
.auxent
.x_sym
.x_tagndx
.l
;
1145 auxent
->fix_tag
= 1;
1150 build_string_table (abfd
)
1153 char string_table_size_buffer
[STRING_SIZE_SIZE
];
1154 unsigned int string_table_size
;
1157 /* At this point we should be "seek"'d to the end of the
1158 symbols === the symbol table size. */
1159 if (bfd_read((char *) string_table_size_buffer
,
1160 sizeof(string_table_size_buffer
),
1161 1, abfd
) != sizeof(string_table_size
))
1164 #if STRING_SIZE_SIZE == 4
1165 string_table_size
= bfd_h_get_32(abfd
, (bfd_byte
*) string_table_size_buffer
);
1167 #error Change bfd_h_get_32
1170 if ((string_table
= (PTR
) bfd_alloc(abfd
,
1171 string_table_size
-= STRING_SIZE_SIZE
))
1174 bfd_set_error (bfd_error_no_memory
);
1176 } /* on mallocation error */
1177 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
)
1179 return string_table
;
1182 /* Allocate space for the ".debug" section, and read it.
1183 We did not read the debug section until now, because
1184 we didn't want to go to the trouble until someone needed it. */
1187 build_debug_section (abfd
)
1190 char *debug_section
;
1193 asection
*sect
= bfd_get_section_by_name (abfd
, ".debug");
1196 bfd_set_error (bfd_error_no_debug_section
);
1200 debug_section
= (PTR
) bfd_alloc (abfd
,
1201 bfd_get_section_size_before_reloc (sect
));
1202 if (debug_section
== NULL
) {
1203 bfd_set_error (bfd_error_no_memory
);
1207 /* Seek to the beginning of the `.debug' section and read it.
1208 Save the current position first; it is needed by our caller.
1209 Then read debug section and reset the file pointer. */
1211 position
= bfd_tell (abfd
);
1212 if (bfd_seek (abfd
, sect
->filepos
, SEEK_SET
) != 0
1213 || (bfd_read (debug_section
,
1214 bfd_get_section_size_before_reloc (sect
), 1, abfd
)
1215 != bfd_get_section_size_before_reloc(sect
))
1216 || bfd_seek (abfd
, position
, SEEK_SET
) != 0)
1218 return debug_section
;
1222 /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
1223 \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
1224 be \0-terminated. */
1226 copy_name (abfd
, name
, maxlen
)
1234 for (len
= 0; len
< maxlen
; ++len
) {
1235 if (name
[len
] == '\0') {
1240 if ((newname
= (PTR
) bfd_alloc(abfd
, len
+1)) == NULL
) {
1241 bfd_set_error (bfd_error_no_memory
);
1244 strncpy(newname
, name
, len
);
1245 newname
[len
] = '\0';
1249 /* Read a symbol table into freshly bfd_allocated memory, swap it, and
1250 knit the symbol names into a normalized form. By normalized here I
1251 mean that all symbols have an n_offset pointer that points to a null-
1252 terminated string. */
1254 combined_entry_type
*
1255 coff_get_normalized_symtab (abfd
)
1258 combined_entry_type
*internal
;
1259 combined_entry_type
*internal_ptr
;
1260 combined_entry_type
*symbol_ptr
;
1261 combined_entry_type
*internal_end
;
1262 bfd_size_type symesz
;
1266 char *string_table
= NULL
;
1267 char *debug_section
= NULL
;
1270 unsigned int raw_size
;
1271 if (obj_raw_syments(abfd
) != (combined_entry_type
*)NULL
) {
1272 return obj_raw_syments(abfd
);
1274 if ((size
= bfd_get_symcount(abfd
) * sizeof(combined_entry_type
)) == 0) {
1275 bfd_set_error (bfd_error_no_symbols
);
1279 internal
= (combined_entry_type
*)bfd_alloc(abfd
, size
);
1282 bfd_set_error (bfd_error_no_memory
);
1285 internal_end
= internal
+ bfd_get_symcount(abfd
);
1287 symesz
= bfd_coff_symesz (abfd
);
1288 raw_size
= bfd_get_symcount(abfd
) * symesz
;
1289 raw
= bfd_alloc(abfd
,raw_size
);
1292 bfd_set_error (bfd_error_no_memory
);
1296 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1297 || bfd_read(raw
, raw_size
, 1, abfd
) != raw_size
)
1299 /* mark the end of the symbols */
1300 raw_end
= (char *) raw
+ bfd_get_symcount(abfd
) * symesz
;
1302 FIXME SOMEDAY. A string table size of zero is very weird, but
1303 probably possible. If one shows up, it will probably kill us.
1306 /* Swap all the raw entries */
1307 for (raw_src
= (char *) raw
, internal_ptr
= internal
;
1309 raw_src
+= symesz
, internal_ptr
++) {
1312 bfd_coff_swap_sym_in(abfd
, (PTR
)raw_src
, (PTR
)&internal_ptr
->u
.syment
);
1313 internal_ptr
->fix_value
= 0;
1314 internal_ptr
->fix_tag
= 0;
1315 internal_ptr
->fix_end
= 0;
1316 internal_ptr
->fix_scnlen
= 0;
1317 symbol_ptr
= internal_ptr
;
1320 i
< symbol_ptr
->u
.syment
.n_numaux
;
1326 internal_ptr
->fix_value
= 0;
1327 internal_ptr
->fix_tag
= 0;
1328 internal_ptr
->fix_end
= 0;
1329 internal_ptr
->fix_scnlen
= 0;
1330 bfd_coff_swap_aux_in(abfd
, (PTR
) raw_src
,
1331 symbol_ptr
->u
.syment
.n_type
,
1332 symbol_ptr
->u
.syment
.n_sclass
,
1333 i
, symbol_ptr
->u
.syment
.n_numaux
,
1334 &(internal_ptr
->u
.auxent
));
1335 /* Remember that bal entries arn't pointerized */
1336 if (i
!= 1 || symbol_ptr
->u
.syment
.n_sclass
!= C_LEAFPROC
)
1339 coff_pointerize_aux(abfd
,
1341 symbol_ptr
->u
.syment
.n_type
,
1342 symbol_ptr
->u
.syment
.n_sclass
,
1349 /* Free all the raw stuff */
1350 bfd_release(abfd
, raw
);
1352 for (internal_ptr
= internal
; internal_ptr
< internal_end
;
1355 if (internal_ptr
->u
.syment
.n_sclass
== C_FILE
1356 && internal_ptr
->u
.syment
.n_numaux
> 0) {
1357 /* make a file symbol point to the name in the auxent, since
1358 the text ".file" is redundant */
1359 if ((internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_zeroes
== 0) {
1360 /* the filename is a long one, point into the string table */
1361 if (string_table
== NULL
) {
1362 string_table
= build_string_table(abfd
);
1365 internal_ptr
->u
.syment
._n
._n_n
._n_offset
=
1366 (long) (string_table
- STRING_SIZE_SIZE
+
1367 (internal_ptr
+1)->u
.auxent
.x_file
.x_n
.x_offset
);
1370 /* ordinary short filename, put into memory anyway */
1371 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long)
1372 copy_name(abfd
, (internal_ptr
+1)->u
.auxent
.x_file
.x_fname
,
1377 if (internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
!= 0) {
1378 /* This is a "short" name. Make it long. */
1379 unsigned long i
= 0;
1380 char *newstring
= NULL
;
1382 /* find the length of this string without walking into memory
1384 for (i
= 0; i
< 8; ++i
) {
1385 if (internal_ptr
->u
.syment
._n
._n_name
[i
] == '\0') {
1387 } /* if end of string */
1388 } /* possible lengths of this string. */
1390 if ((newstring
= (PTR
) bfd_alloc(abfd
, ++i
)) == NULL
) {
1391 bfd_set_error (bfd_error_no_memory
);
1394 memset(newstring
, 0, i
);
1395 strncpy(newstring
, internal_ptr
->u
.syment
._n
._n_name
, i
-1);
1396 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) newstring
;
1397 internal_ptr
->u
.syment
._n
._n_n
._n_zeroes
= 0;
1399 else if (internal_ptr
->u
.syment
._n
._n_n
._n_offset
== 0)
1400 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int) "";
1401 else if (!bfd_coff_symname_in_debug(abfd
, &internal_ptr
->u
.syment
)) {
1402 /* Long name already. Point symbol at the string in the table. */
1403 if (string_table
== NULL
) {
1404 string_table
= build_string_table(abfd
);
1406 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1409 + internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1412 /* Long name in debug section. Very similar. */
1413 if (debug_section
== NULL
) {
1414 debug_section
= build_debug_section(abfd
);
1416 internal_ptr
->u
.syment
._n
._n_n
._n_offset
= (long int)
1417 (debug_section
+ internal_ptr
->u
.syment
._n
._n_n
._n_offset
);
1420 internal_ptr
+= internal_ptr
->u
.syment
.n_numaux
;
1423 obj_raw_syments(abfd
) = internal
;
1424 obj_raw_syment_count(abfd
) = internal_ptr
- internal
;
1427 } /* coff_get_normalized_symtab() */
1430 coff_get_reloc_upper_bound (abfd
, asect
)
1434 if (bfd_get_format(abfd
) != bfd_object
) {
1435 bfd_set_error (bfd_error_invalid_operation
);
1438 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
1442 coff_make_empty_symbol (abfd
)
1445 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1447 bfd_set_error (bfd_error_no_memory
);
1450 memset (new, 0, sizeof *new);
1451 new->symbol
.section
= 0;
1453 new->lineno
= (alent
*) NULL
;
1454 new->done_lineno
= false;
1455 new->symbol
.the_bfd
= abfd
;
1456 return &new->symbol
;
1459 /* Make a debugging symbol. */
1462 coff_bfd_make_debug_symbol (abfd
, ptr
, sz
)
1467 coff_symbol_type
*new = (coff_symbol_type
*) bfd_alloc(abfd
, sizeof(coff_symbol_type
));
1469 bfd_set_error (bfd_error_no_memory
);
1472 /* @@ This shouldn't be using a constant multiplier. */
1473 new->native
= (combined_entry_type
*) bfd_zalloc (abfd
, sizeof (combined_entry_type
) * 10);
1476 bfd_set_error (bfd_error_no_memory
);
1479 new->symbol
.section
= &bfd_debug_section
;
1480 new->lineno
= (alent
*) NULL
;
1481 new->done_lineno
= false;
1482 new->symbol
.the_bfd
= abfd
;
1483 return &new->symbol
;
1488 coff_get_symbol_info (abfd
, symbol
, ret
)
1493 bfd_symbol_info (symbol
, ret
);
1496 /* Print out information about COFF symbol. */
1499 coff_print_symbol (abfd
, filep
, symbol
, how
)
1503 bfd_print_symbol_type how
;
1505 FILE *file
= (FILE *) filep
;
1509 case bfd_print_symbol_name
:
1510 fprintf (file
, "%s", symbol
->name
);
1513 case bfd_print_symbol_more
:
1514 fprintf (file
, "coff %s %s",
1515 coffsymbol(symbol
)->native
? "n" : "g",
1516 coffsymbol(symbol
)->lineno
? "l" : " ");
1519 case bfd_print_symbol_all
:
1520 if (coffsymbol(symbol
)->native
)
1523 combined_entry_type
*combined
= coffsymbol (symbol
)->native
;
1524 combined_entry_type
*root
= obj_raw_syments (abfd
);
1525 struct lineno_cache_entry
*l
= coffsymbol(symbol
)->lineno
;
1527 fprintf (file
,"[%3d]", combined
- root
);
1530 "(sc %2d)(fl 0x%02x)(ty %3x)(sc %3d) (nx %d) 0x%08lx %s",
1531 combined
->u
.syment
.n_scnum
,
1532 combined
->u
.syment
.n_flags
,
1533 combined
->u
.syment
.n_type
,
1534 combined
->u
.syment
.n_sclass
,
1535 combined
->u
.syment
.n_numaux
,
1536 (unsigned long) combined
->u
.syment
.n_value
,
1539 for (aux
= 0; aux
< combined
->u
.syment
.n_numaux
; aux
++)
1541 combined_entry_type
*auxp
= combined
+ aux
+ 1;
1545 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.p
- root
;
1547 tagndx
= auxp
->u
.auxent
.x_sym
.x_tagndx
.l
;
1549 fprintf (file
, "\n");
1550 switch (combined
->u
.syment
.n_sclass
)
1553 fprintf (file
, "File ");
1557 fprintf (file
, "AUX lnno %d size 0x%x tagndx %ld",
1558 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_lnno
,
1559 auxp
->u
.auxent
.x_sym
.x_misc
.x_lnsz
.x_size
,
1567 fprintf (file
, "\n%s :", l
->u
.sym
->name
);
1569 while (l
->line_number
)
1571 fprintf (file
, "\n%4d : 0x%lx",
1574 (l
->u
.offset
+ symbol
->section
->vma
)));
1581 bfd_print_symbol_vandf ((PTR
) file
, symbol
);
1582 fprintf (file
, " %-5s %s %s %s",
1583 symbol
->section
->name
,
1584 coffsymbol(symbol
)->native
? "n" : "g",
1585 coffsymbol(symbol
)->lineno
? "l" : " ",
1591 /* Provided a BFD, a section and an offset into the section, calculate
1592 and return the name of the source file and the line nearest to the
1597 coff_find_nearest_line (abfd
, section
, ignore_symbols
, offset
, filename_ptr
,
1598 functionname_ptr
, line_ptr
)
1601 asymbol
**ignore_symbols
;
1603 CONST
char **filename_ptr
;
1604 CONST
char **functionname_ptr
;
1605 unsigned int *line_ptr
;
1607 static bfd
*cache_abfd
;
1608 static asection
*cache_section
;
1609 static bfd_vma cache_offset
;
1610 static unsigned int cache_i
;
1611 static CONST
char *cache_function
;
1612 static unsigned int line_base
= 0;
1615 coff_data_type
*cof
= coff_data(abfd
);
1616 /* Run through the raw syments if available */
1617 combined_entry_type
*p
;
1618 combined_entry_type
*pend
;
1623 *functionname_ptr
= 0;
1626 /* Don't try and find line numbers in a non coff file */
1627 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour
)
1633 /* Find the first C_FILE symbol. */
1634 p
= cof
->raw_syments
;
1635 pend
= p
+ cof
->raw_syment_count
;
1638 if (p
->u
.syment
.n_sclass
== C_FILE
)
1640 p
+= 1 + p
->u
.syment
.n_numaux
;
1645 /* Look through the C_FILE symbols to find the best one. */
1646 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1650 combined_entry_type
*p2
;
1652 /* Avoid endless loops on erroneous files by ensuring that
1653 we always move forward in the file. */
1654 if (i
>= p
->u
.syment
.n_value
)
1657 i
= p
->u
.syment
.n_value
;
1658 if (i
>= cof
->raw_syment_count
)
1661 p
= cof
->raw_syments
+ i
;
1662 if (p
->u
.syment
.n_sclass
!= C_FILE
)
1665 for (p2
= p
; p2
< pend
; p2
+= 1 + p2
->u
.syment
.n_numaux
)
1668 coff_section_from_bfd_index (abfd
, p2
->u
.syment
.n_scnum
))
1672 && offset
< p2
->u
.syment
.n_value
)
1675 *filename_ptr
= (char *) p
->u
.syment
._n
._n_n
._n_offset
;
1679 /* Now wander though the raw linenumbers of the section */
1681 If this is the same BFD as we were previously called with and this is
1682 the same section, and the offset we want is further down then we can
1683 prime the lookup loop
1685 if (abfd
== cache_abfd
&&
1686 section
== cache_section
&&
1687 offset
>= cache_offset
) {
1689 *functionname_ptr
= cache_function
;
1694 l
= §ion
->lineno
[i
];
1696 for (; i
< section
->lineno_count
; i
++) {
1697 if (l
->line_number
== 0) {
1698 /* Get the symbol this line number points at */
1699 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
1700 if (coff
->symbol
.value
> offset
)
1702 *functionname_ptr
= coff
->symbol
.name
;
1704 combined_entry_type
*s
= coff
->native
;
1705 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1707 /* In XCOFF a debugging symbol can follow the function
1709 if (s
->u
.syment
.n_scnum
== N_DEBUG
)
1710 s
= s
+ 1 + s
->u
.syment
.n_numaux
;
1713 S should now point to the .bf of the function
1715 if (s
->u
.syment
.n_numaux
) {
1717 The linenumber is stored in the auxent
1719 union internal_auxent
*a
= &((s
+ 1)->u
.auxent
);
1720 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
1721 *line_ptr
= line_base
;
1726 if (l
->u
.offset
+ bfd_get_section_vma (abfd
, section
) > offset
)
1728 *line_ptr
= l
->line_number
+ line_base
- 1;
1734 cache_section
= section
;
1735 cache_offset
= offset
;
1737 cache_function
= *functionname_ptr
;
1743 coff_sizeof_headers (abfd
, reloc
)
1749 if (reloc
== false) {
1750 size
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
1753 size
= bfd_coff_filhsz (abfd
);
1756 size
+= abfd
->section_count
* bfd_coff_scnhsz (abfd
);