2 Copyright (C) 1990, 1991 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Diddler.
6 BFD is free software; you can redistribute it and/or modify it under the
7 terms of the GNU General Public License as published by the Free Software
8 Foundation; either version 1, or (at your option) any later version.
10 BFD is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 You should have received a copy of the GNU General Public License along with
16 BFD; see the file COPYING. If not, write to the Free Software Foundation,
17 675 Mass Ave, Cambridge, MA 02139, USA.
22 Most of this hacked by Steve Chamberlain, steve@cygnus.com
25 #include "archures.h" /* Machine architectures and types */
32 #define ALIGN(this, boundary) \
33 ((( (this) + ((boundary) -1)) & (~((boundary)-1))))
35 #define sp(x) bfd_h_put_x(abfd, x, &x)
40 Align an address by rounding it up to a power of two. It leaves the
41 address unchanged if align == 0 (2^0 = alignment of 1 byte)
43 #define i960_align(addr, align) \
44 ( ((addr) + ((1<<(align))-1)) & (-1 << (align)))
47 /* All the swapping routines */
50 swap_reloc(abfd
, reloc
)
61 swap_filehdr(abfd
, filehdr
)
67 sp(filehdr
->f_timdat
);
68 sp(filehdr
->f_symptr
);
70 sp(filehdr
->f_opthdr
);
77 swap_aouthdr(abfd
, aouthdr
)
88 sp(aouthdr
->text_start
);
89 sp(aouthdr
->data_start
);
91 sp(aouthdr
->tagentries
);
96 swap_scnhdr(abfd
, scnhdr
)
103 sp(scnhdr
->s_scnptr
);
104 sp(scnhdr
->s_relptr
);
105 sp(scnhdr
->s_lnnoptr
);
106 sp(scnhdr
->s_nreloc
);
120 /* There is an index which needs to be swapped */
121 bfd_h_put_x(abfd
, ptr
[1], (ptr
+ 1));
124 /* This is a string .. leave it alone */
129 bfd_coff_swap_sym(abfd
, se
)
133 swap_name(abfd
, se
->n_name
);
134 bfd_h_put_x(abfd
, se
->n_value
, &se
->n_value
);
135 bfd_h_put_x(abfd
, se
->n_scnum
, &se
->n_scnum
);
136 bfd_h_put_x(abfd
, se
->n_type
, &se
->n_type
);
137 bfd_h_put_x(abfd
, se
->n_sclass
, &se
->n_sclass
);
138 bfd_h_put_x(abfd
, se
->n_numaux
, &se
->n_numaux
);
142 bfd_coff_swap_aux(abfd
, au
, type
, class)
150 swap_name(abfd
, &au
->x_file
.x_n
);
157 if (type
== T_NULL
) {
158 sp(au
->x_scn
.x_scnlen
);
159 sp(au
->x_scn
.x_nreloc
);
160 sp(au
->x_scn
.x_nlinno
);
164 sp(au
->x_sym
.x_tagndx
);
165 sp(au
->x_sym
.x_tvndx
);
168 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[0]);
169 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[1]);
170 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[2]);
171 sp(au
->x_sym
.x_fcnary
.x_ary
.x_dimen
[3]);
174 sp(au
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
);
175 sp(au
->x_sym
.x_fcnary
.x_fcn
.x_endndx
);
178 sp(au
->x_sym
.x_misc
.x_fsize
);
181 sp(au
->x_sym
.x_misc
.x_lnsz
.x_lnno
);
182 sp(au
->x_sym
.x_misc
.x_lnsz
.x_size
);
188 bfd_coff_swap_lineno(abfd
, lineno
)
192 sp(lineno
->l_addr
.l_symndx
);
197 /* void warning(); */
198 extern asection abs_section
;
206 return (int) symbol
->value
;
210 set_index(symbol
, idx
)
218 initialize a section structure with information peculiar to this
219 particular implementation of coff
223 coff_new_section_hook(abfd_ignore
, section_ignore
)
225 asection
*section_ignore
;
229 /* actually it makes itself and its children from the file headers */
231 make_a_section_from_file(abfd
, hdr
)
236 asection
*return_section
;
238 char *name
= malloc(9);
240 bfd_error
= no_memory
;
241 return (BFD_FAILURE
);
243 strncpy(name
, (char *) &hdr
->s_name
[0], 8);
245 return_section
= bfd_make_section(abfd
, name
);
246 (return_section
->name
)[8] = 0;
249 /* s_paddr is presumed to be = to s_vaddr */
250 #define assign(to, from) return_section->to = hdr->from
251 assign(vma
, s_vaddr
);
252 /* assign (vma, s_vaddr); */
253 assign(size
, s_size
);
254 assign(filepos
, s_scnptr
);
255 assign(rel_filepos
, s_relptr
);
256 assign(reloc_count
, s_nreloc
);
260 assign(alignment_power
, s_align
);
263 for (i
= 0; i
< 32; i
++) {
264 if ((1 << i
) >= (int) (return_section
->alignment_power
)) {
265 return_section
->alignment_power
= i
;
272 assign(line_filepos
, s_lnnoptr
);
274 return_section->linesize = hdr->s_nlnno * sizeof (struct lineno);
278 return_section
->lineno_count
= hdr
->s_nlnno
;
279 return_section
->userdata
= NULL
;
280 return_section
->next
= (asection
*) NULL
;
281 if ((hdr
->s_flags
& STYP_TEXT
) || (hdr
->s_flags
& STYP_DATA
))
282 return_section
->flags
= (SEC_LOAD
| SEC_ALLOC
);
283 else if (hdr
->s_flags
& STYP_BSS
)
284 return_section
->flags
= SEC_ALLOC
;
286 if (hdr
->s_nreloc
!= 0)
287 return_section
->flags
|= SEC_RELOC
;
288 if (hdr
->s_scnptr
!= 0)
289 return_section
->flags
|= SEC_HAS_CONTENTS
;
296 coff_real_object_p(abfd
, nscns
, opthdr
)
301 struct icofdata
*tdata
;
302 char *file_info
; /* buffer for all the headers */
303 size_t readsize
; /* length of file_info */
304 struct filehdr
*filehdr
; /* points into file_info */
305 struct scnhdr
*sections
; /* points into file_info */
307 OK, now we know the format, read in the filehdr, soi-disant "optional
308 header", and all the sections.
310 readsize
= sizeof(struct filehdr
)
312 + (nscns
* sizeof(struct scnhdr
));
314 file_info
= malloc(readsize
);
315 if (file_info
== NULL
) {
316 bfd_error
= no_memory
;
319 if (bfd_seek(abfd
, 0L, SEEK_SET
) < 0)
321 if (bfd_read((void *) file_info
, 1, readsize
, abfd
) != readsize
)
323 filehdr
= (struct filehdr
*) file_info
;
324 sections
= (struct scnhdr
*) (file_info
+ sizeof(struct filehdr
) + opthdr
);
327 swap_filehdr(abfd
, filehdr
);
329 /* Now copy data as required; construct all asections etc */
330 tdata
= (struct icofdata
*) malloc(sizeof(struct icofdata
) +
333 bfd_error
= no_memory
;
336 tdata
->symbol_index_slew
= 0;
338 tdata
->raw_syment_count
= 0;
339 tdata
->raw_linenos
= 0;
340 tdata
->raw_syments
= 0;
341 tdata
->sym_filepos
=0;
342 tdata
->flags
= filehdr
->f_flags
;
345 for (i
= 0; i
< nscns
; i
++) {
346 swap_scnhdr(abfd
, sections
+ i
);
347 make_a_section_from_file(abfd
, sections
+ i
);
350 /* Determine the machine architecture and type. */
351 abfd
->obj_machine
= 0;
352 switch (filehdr
->f_magic
) {
356 abfd
->obj_arch
= bfd_arch_m68k
;
357 abfd
->obj_machine
= 68020;
363 abfd
->obj_arch
= bfd_arch_m88k
;
364 abfd
->obj_machine
= 88100;
370 abfd
->obj_arch
= bfd_arch_i960
;
371 switch (F_I960TYPE
& filehdr
->f_flags
)
375 abfd
->obj_machine
= bfd_mach_i960_core
;
378 abfd
->obj_machine
= bfd_mach_i960_kb_sb
;
381 abfd
->obj_machine
= bfd_mach_i960_mc
;
384 abfd
->obj_machine
= bfd_mach_i960_xa
;
387 abfd
->obj_machine
= bfd_mach_i960_ca
;
390 abfd
->obj_machine
= bfd_mach_i960_ka_sa
;
397 default: /* Unreadable input file type */
398 abfd
->obj_arch
= bfd_arch_obscure
;
402 if (!(filehdr
->f_flags
& F_RELFLG
))
403 abfd
->flags
|= HAS_RELOC
;
404 if ((filehdr
->f_flags
& F_EXEC
))
405 abfd
->flags
|= EXEC_P
;
406 if (!(filehdr
->f_flags
& F_LNNO
))
407 abfd
->flags
|= HAS_LINENO
;
408 if (!(filehdr
->f_flags
& F_LSYMS
))
409 abfd
->flags
|= HAS_LOCALS
;
411 abfd
->tdata
= (void *) tdata
;
412 bfd_get_symcount(abfd
) = filehdr
->f_nsyms
;
413 if (filehdr
->f_nsyms
)
414 abfd
->flags
|= HAS_SYMS
;
416 tdata
->sym_filepos
= filehdr
->f_symptr
;
417 tdata
->hdr
= (struct aouthdr
*) (file_info
+ sizeof(struct filehdr
));
419 swap_aouthdr(abfd
, tdata
->hdr
);
421 tdata
->symbols
= (coff_symbol_type
*) NULL
;
422 bfd_get_start_address(abfd
) = opthdr
? exec_hdr(abfd
)->entry
: 0;
429 Takes a bfd and a symbol, returns a pointer to the coff specific area
430 of the symbol if there is one.
432 static coff_symbol_type
*
433 coff_symbol_from(abfd
, symbol
)
437 if (symbol
->the_bfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
438 return (coff_symbol_type
*)NULL
;
440 if (symbol
->the_bfd
->tdata
== (void *)NULL
)
441 return (coff_symbol_type
*)NULL
;
443 return (coff_symbol_type
*) symbol
;
452 unsigned short magic
,
455 bfd_error
= system_call_error
;
457 /* figure out how much to read */
458 if (bfd_read((void *) &magic
, 1, sizeof(magic
), abfd
) != sizeof(magic
))
461 magic
= bfd_h_getshort(abfd
, (bfd_byte
*) (&magic
));
463 if (BADMAG(*((struct filehdr
*) & magic
))) {
464 bfd_error
= wrong_format
;
467 if (bfd_read((void *) &nscns
, 1, sizeof(nscns
), abfd
) != sizeof(nscns
))
469 nscns
= bfd_h_getshort(abfd
, (unsigned char *) &nscns
);
470 if (bfd_seek(abfd
, (file_ptr
) & (((struct filehdr
*) NULL
)->f_opthdr
), SEEK_SET
)
472 return (bfd_target
*) NULL
;
473 if (bfd_read((void *) &opthdr
, 1, sizeof(opthdr
), abfd
) != sizeof(opthdr
))
474 return (bfd_target
*) NULL
;
475 opthdr
= bfd_h_getshort(abfd
, (unsigned char *) &opthdr
);
477 return coff_real_object_p(abfd
, nscns
, opthdr
);
486 bfd_error
= system_call_error
;
488 /* Use an intermediate variable for clarity */
489 rawptr
= zalloc(sizeof(struct icofdata
) + sizeof(AOUTHDR
));
490 if (rawptr
== NULL
) {
491 bfd_error
= no_memory
;
494 abfd
->tdata
= (void *) ((struct icofdata
*) rawptr
);
495 exec_hdr(abfd
) = (AOUTHDR
*) (rawptr
+ sizeof(struct icofdata
));
496 obj_relocbase(abfd
) =0;
503 coff_count_linenumbers(abfd
)
506 unsigned int limit
= bfd_get_symcount(abfd
);
510 asection
*s
= abfd
->sections
->output_section
;
512 BFD_ASSERT(s
->lineno_count
== 0);
518 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
519 asymbol
*q_maybe
= *p
;
520 if (q_maybe
->the_bfd
->xvec
->flavour
== bfd_target_coff_flavour_enum
) {
521 coff_symbol_type
*q
= coffsymbol(q_maybe
);
524 This symbol has a linenumber, increment the owning
525 section's linenumber count
527 alent
*l
= q
->lineno
;
528 q
->symbol
.section
->output_section
->lineno_count
++;
530 while (l
->line_number
) {
531 q
->symbol
.section
->output_section
->lineno_count
++;
540 This function returns true if the supplied SYMENT has an AUXENT with
541 a tagndx field which should be relocated.
543 The coff book says that all auxents have this and should be moved,
544 but all the actual implementations I've looked at do this ..
549 uses_x_sym_x_tagndx_p(native
)
552 if (BTYPE(native
->n_type
) == T_STRUCT
) return true;
553 if (BTYPE(native
->n_type
) == T_UNION
) return true;
554 if (BTYPE(native
->n_type
) == T_ENUM
)return true;
564 This procedure runs through the native entries in a coff symbol table
565 and links up all the elements which should point to one another, in
566 particular these are:
568 strtag, entag and untags have an auxent endindex which points to the
569 first syment after the .eos. This is simple to do, we just keep a
570 pointer to the symbol with the most recent pending strtag and patch it
571 when we see the eos. This works since coff structs are never nested.
573 ISFCN type entries have an endindex which points to the next static or
574 extern in the table, thereby skipping the function contents.
575 The coff book says that an ISFCN's tagindex
576 points to the first .bf for the function, so far I havn't seen it
577 used. We do this using the same mechanism as strtags.
579 Each file entry has a value which points to the next file entry,
580 the last file entry points to the first extern symbol in the table
581 which is not an ISFCN.
583 Each .bb entry points to the matching .eb entry, but these are nested
584 so we keep a stack of them.
586 The tagndx of .eos items points to the strtag attached to them, this
587 is simply the last_tagndx again.
589 The tagndx of items with type strtag point to the defining struct.
590 This bit is complicated; We know that a struct ref and def must be
591 within the same file, so all the natives will be in the same vector.
592 This means that we can subtracts two pointers and get the index
593 differences between to items, used to work out the true index of the
596 We store in the name field of each syment the actual native index
597 applied so we can dig it out through a pointer.
602 coff_mangle_symbols(bfd_ptr
)
605 unsigned int symbol_count
= bfd_get_symcount(bfd_ptr
);
606 asymbol
**symbol_ptr_ptr
= bfd_ptr
->outsymbols
;
607 SYMENT
*last_tagndx
= (SYMENT
*)NULL
;
608 SYMENT
*last_file
= (SYMENT
*)NULL
;
609 SYMENT
*last_fcn
= (SYMENT
*)NULL
;
610 SYMENT
*block_stack
[50];
611 SYMENT
**last_block
= &block_stack
[0];
612 boolean first_time
= true;
613 unsigned int symbol_index
;
614 unsigned int native_index
= 0;
616 for (symbol_index
= 0; symbol_index
< symbol_count
; symbol_index
++) {
617 coff_symbol_type
*coff_symbol_ptr
=
618 coff_symbol_from(bfd_ptr
, symbol_ptr_ptr
[symbol_index
]);
619 if (coff_symbol_ptr
== (coff_symbol_type
*)NULL
) {
621 This symbol has no coff information in it, it will take up
622 only one slot in the output symbol table
627 SYMENT
*syment
= coff_symbol_ptr
->native
;
628 if (syment
== (SYMENT
*)NULL
) {
632 /* Normalize the symbol flags */
633 if (coff_symbol_ptr
->symbol
.flags
& BSF_FORT_COMM
) {
634 /* a common symbol is undefined with a value */
635 syment
->n_scnum
= N_UNDEF
;
636 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
638 else if (coff_symbol_ptr
->symbol
.flags
& BSF_DEBUGGING
) {
639 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
641 else if (coff_symbol_ptr
->symbol
.flags
& BSF_UNDEFINED
) {
642 syment
->n_scnum
= N_UNDEF
;
645 else if (coff_symbol_ptr
->symbol
.flags
& BSF_ABSOLUTE
) {
646 syment
->n_scnum
= N_ABS
;
647 syment
->n_value
= coff_symbol_ptr
->symbol
.value
;
651 coff_symbol_ptr
->symbol
.section
->output_section
->index
+1;
653 coff_symbol_ptr
->symbol
.value
+
654 coff_symbol_ptr
->symbol
.section
->output_offset
+
655 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
659 /* If this symbol ties up something then do it */
661 if (syment
->n_sclass
== C_FILE
&& last_file
!= (SYMENT
*)NULL
)
663 last_file
->n_value
= native_index
;
665 else if ((syment
->n_sclass
== C_EXT
666 || syment
->n_sclass
== C_STAT
668 || syment
->n_sclass
== C_LEAFEXT
669 || syment
->n_sclass
== C_LEAFSTAT
672 && last_fcn
!= (SYMENT
*)NULL
)
674 AUXENT
*auxent
= (AUXENT
*)(last_fcn
+1);
675 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
= native_index
;
676 last_fcn
= (SYMENT
*)NULL
;
679 else if (syment
->n_sclass
== C_EOS
&& last_tagndx
!= (SYMENT
*)NULL
)
681 AUXENT
*auxent
= (AUXENT
*)(last_tagndx
+1);
682 /* Remember that we keep the native index in the offset
683 so patch the beginning of the struct to point to this
685 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
686 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
687 native_index
+ syment
->n_numaux
+ 1 ;
688 /* Now point the eos to the structure */
689 auxent
= (AUXENT
*)(syment
+1);
690 auxent
->x_sym
.x_tagndx
= last_tagndx
->n_offset
;
694 else if (syment
->n_sclass
== C_BLOCK
695 && coff_symbol_ptr
->symbol
.name
[1] == 'e')
697 AUXENT
*auxent
= (AUXENT
*)((*(--last_block
))+1);
698 auxent
->x_sym
.x_fcnary
.x_fcn
.x_endndx
=
699 native_index
+ syment
->n_numaux
+ 1;
701 if (syment
->n_sclass
== C_EXT
702 && !ISFCN(syment
->n_type
)
703 && first_time
== true
704 && last_file
!= (SYMENT
*)NULL
) {
705 /* This is the first external symbol seen which isn't a
706 function place it in the last .file entry */
707 last_file
->n_value
= native_index
;
711 if (syment
->n_sclass
== C_LEAFPROC
&& syment
->n_numaux
== 2) {
712 AUXENT
*auxent
= (AUXENT
*)(syment
+2);
713 /* This is the definition of a leaf proc, we'll relocate the
716 auxent
->x_bal
.x_balntry
+=
717 coff_symbol_ptr
->symbol
.section
->output_offset
+
718 coff_symbol_ptr
->symbol
.section
->output_section
->vma
;
721 /* If this symbol needs to be tied up then remember some facts */
722 if (syment
->n_sclass
== C_FILE
)
726 if (syment
->n_numaux
!= 0) {
728 If this symbol would like to point to something in the
729 future then remember where it is
731 if (uses_x_sym_x_tagndx_p(syment
)) {
733 If this is a ref to a structure then we'll tie it up
734 now - there are never any forward refs for one
736 if (syment
->n_sclass
== C_STRTAG
||
737 syment
->n_sclass
== C_ENTAG
||
738 syment
->n_sclass
== C_UNTAG
) {
739 last_tagndx
= syment
;
743 This is a ref to a structure - the structure must
744 have been defined within the same file, and previous
745 to this point, so we can deduce the new tagndx
748 AUXENT
*auxent
= (AUXENT
*)(syment
+1);
749 bfd
*bfd_ptr
= coff_symbol_ptr
->symbol
.the_bfd
;
750 SYMENT
*base
= obj_raw_syments(bfd_ptr
);
751 auxent
->x_sym
.x_tagndx
= base
[auxent
->x_sym
.x_tagndx
].n_offset
;
755 if (ISFCN(syment
->n_type
)) {
758 if (syment
->n_sclass
== C_BLOCK
759 && coff_symbol_ptr
->symbol
.name
[1] == 'b')
761 *last_block
++ = syment
;
764 syment
->n_offset
= native_index
;
765 native_index
= native_index
+ 1 + syment
->n_numaux
;
778 coff_write_symbols(abfd
)
782 unsigned int limit
= bfd_get_symcount(abfd
);
783 unsigned int written
= 0;
786 unsigned int string_size
= 0;
789 /* Seek to the right place */
790 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
792 /* Output all the symbols we have */
795 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
796 asymbol
*symbol
= *p
;
797 coff_symbol_type
*c_symbol
= coff_symbol_from(abfd
, symbol
);
801 if (c_symbol
== (coff_symbol_type
*) NULL
||
802 c_symbol
->native
== (SYMENT
*) NULL
) {
804 This symbol has been created by the loader, or come from a non
805 coff format. It has no native element to inherit, make our
810 native
->n_type
= T_NULL
;
814 if (symbol
->flags
& BSF_ABSOLUTE
) {
815 native
->n_scnum
= N_ABS
;
816 native
->n_value
= symbol
->value
;
818 else if (symbol
->flags
& (BSF_UNDEFINED
| BSF_FORT_COMM
)) {
819 native
->n_scnum
= N_UNDEF
;
820 native
->n_value
= symbol
->value
;
822 else if (symbol
->flags
& BSF_DEBUGGING
) {
824 remove name so it doesn't take up any space
827 #if 0 /* FIXME -- Steve hasn't decided what to do
830 Don't do anything with debugs from the loader
832 native
->n_scnum
= N_DEBUG
;
837 native
->n_scnum
= symbol
->section
->output_section
->index
+ 1;
838 native
->n_value
= symbol
->value
+
839 symbol
->section
->output_section
->vma
+
840 symbol
->section
->output_offset
;
842 /* Copy the any flags from the the file hdr into the symbol */
844 coff_symbol_type
*c
= coff_symbol_from(abfd
, symbol
);
845 if (c
!= (coff_symbol_type
*)NULL
) {
846 native
->n_flags
= c
->symbol
.the_bfd
->flags
;
863 native
->n_sclass
= C_EXT
;
864 native
->n_numaux
= 0;
868 Does this symbol have an ascociated line number - if so then
869 make it remember this symbol index. Also tag the auxent of
870 this symbol to point to the right place in the lineno table
873 alent
*lineno
= c_symbol
->lineno
;
874 native
= c_symbol
->native
;
876 unsigned int count
= 0;
877 lineno
[count
].u
.offset
= written
;
878 if (native
->n_numaux
) {
879 union auxent
*a
= (union auxent
*) (native
+ 1);
880 a
->x_sym
.x_fcnary
.x_fcn
.x_lnnoptr
=
881 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
;
884 And count and relocate all other linenumbers
887 while (lineno
[count
].line_number
) {
888 lineno
[count
].u
.offset
+=
889 c_symbol
->symbol
.section
->output_section
->vma
+
890 c_symbol
->symbol
.section
->output_offset
;
893 c_symbol
->symbol
.section
->output_section
->moving_line_filepos
+=
894 count
* sizeof(struct lineno
);
897 } /* if symbol new to coff */
899 /* Fix the symbol names */
901 unsigned int name_length
;
902 if (symbol
->name
== (char *) NULL
) {
904 coff symbols always have names, so we'll make one up
906 symbol
->name
= "strange";
908 name_length
= strlen(symbol
->name
);
909 if (name_length
<= SYMNMLEN
) {
910 /* This name will fit into the symbol neatly */
911 strncpy(native
->n_name
, symbol
->name
, SYMNMLEN
);
914 native
->n_offset
= string_size
+ 4;
915 native
->n_zeroes
= 0;
916 string_size
+= name_length
+ 1;
919 unsigned int numaux
= native
->n_numaux
;
920 int type
= native
->n_type
;
921 int class = native
->n_sclass
;
922 bfd_coff_swap_sym(abfd
, native
);
923 bfd_write((void *) native
, 1, SYMESZ
, abfd
);
924 for (j
= 0; j
!= native
->n_numaux
; j
++) {
925 bfd_coff_swap_aux(abfd
, native
+ j
+ 1, type
, class);
926 bfd_write((void *) (native
+ j
+ 1), 1, AUXESZ
, abfd
);
930 Reuse somewhere in the symbol to keep the index
932 set_index(symbol
, written
);
933 written
+= 1 + numaux
;
936 } /* for each out symbol */
938 bfd_get_symcount(abfd
) = written
;
939 /* Now write out strings */
942 unsigned int size
= string_size
+ 4;
943 bfd_h_put_x(abfd
, size
, &size
);
944 bfd_write((void *) &size
, 1, sizeof(size
), abfd
);
945 for (p
= abfd
->outsymbols
, i
= 0; i
< limit
; i
++, p
++) {
947 size_t name_length
= strlen(q
->name
);
948 if (name_length
> SYMNMLEN
) {
949 bfd_write((void *) (q
->name
), 1, name_length
+ 1, abfd
);
954 /* We would normally not write anything here, but we'll write
955 out 4 so that any stupid coff reader which tries to read
956 the string table even when there isn't one won't croak.
959 uint32e_type size
= 4;
960 bfd_h_put_x(abfd
, size
, &size
);
961 bfd_write((void *)&size
, 1, sizeof(size
), abfd
);
968 coff_write_relocs(abfd
)
972 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
974 arelent
**p
= s
->orelocation
;
975 bfd_seek(abfd
, s
->rel_filepos
, SEEK_SET
);
976 for (i
= 0; i
< s
->reloc_count
; i
++) {
979 memset((PTR
)&n
, 0, sizeof(n
));
980 n
.r_vaddr
= q
->address
+ s
->vma
;
981 n
.r_symndx
= get_index((*(q
->sym_ptr_ptr
)));
982 n
.r_type
= q
->howto
->type
;
983 swap_reloc(abfd
, &n
);
984 bfd_write((void *) &n
, 1, RELSZ
, abfd
);
989 coff_write_linenumbers(abfd
)
993 for (s
= abfd
->sections
; s
!= (asection
*) NULL
; s
= s
->next
) {
994 if (s
->lineno_count
) {
995 asymbol
**q
= abfd
->outsymbols
;
996 bfd_seek(abfd
, s
->line_filepos
, SEEK_SET
);
997 /* Find all the linenumbers in this section */
1000 alent
*l
= BFD_SEND(p
->the_bfd
, _get_lineno
, (p
->the_bfd
, p
));
1002 /* Found a linenumber entry, output */
1004 bzero( &out
, sizeof(out
));
1006 out
.l_addr
.l_symndx
= l
->u
.offset
;
1007 bfd_coff_swap_lineno(abfd
, &out
);
1008 bfd_write((void *) &out
, 1, LINESZ
, abfd
);
1010 while (l
->line_number
) {
1011 out
.l_lnno
= l
->line_number
;
1012 out
.l_addr
.l_symndx
= l
->u
.offset
;
1013 bfd_coff_swap_lineno(abfd
, &out
);
1014 bfd_write((void *) &out
, 1, LINESZ
, abfd
);
1026 coff_make_empty_symbol(abfd
)
1029 coff_symbol_type
*new = (coff_symbol_type
*) malloc(sizeof(coff_symbol_type
));
1031 bfd_error
= no_memory
;
1035 new->lineno
= (alent
*) NULL
;
1036 new->symbol
.the_bfd
= abfd
;
1037 return &new->symbol
;
1041 coff_print_symbol(ignore_abfd
, file
, symbol
, how
)
1045 bfd_print_symbol_enum_type how
;
1048 case bfd_print_symbol_name_enum
:
1049 fprintf(file
, "%s", symbol
->name
);
1051 case bfd_print_symbol_type_enum
:
1052 fprintf(file
, "coff %lx %lx", (unsigned long) coffsymbol(symbol
)->native
,
1053 (unsigned long) coffsymbol(symbol
)->lineno
);
1055 case bfd_print_symbol_all_enum
:
1057 char *section_name
= symbol
->section
== (asection
*) NULL
?
1058 "*abs" : symbol
->section
->name
;
1059 bfd_print_symbol_vandf((void *) file
, symbol
);
1061 fprintf(file
, " %-5s %s %s %s",
1063 coffsymbol(symbol
)->native
? "n" : "g",
1064 coffsymbol(symbol
)->lineno
? "l" : " ",
1073 coff_get_lineno(ignore_abfd
, symbol
)
1077 return coffsymbol(symbol
)->lineno
;
1080 Set flags and magic number of a coff file from architecture and machine
1081 type. Result is true if we can represent the arch&type, false if not.
1084 coff_set_flags(abfd
, magicp
, flagsp
)
1091 switch (abfd
->obj_arch
) {
1099 *magicp
= I960ROMAGIC
;
1101 ((bfd_get_file_flags(abfd) & WP_TEXT) ? I960ROMAGIC :
1102 I960RWMAGIC); FIXME???
1104 switch (abfd
->obj_machine
) {
1105 case bfd_mach_i960_core
:
1108 case bfd_mach_i960_kb_sb
:
1111 case bfd_mach_i960_mc
:
1114 case bfd_mach_i960_xa
:
1117 case bfd_mach_i960_ca
:
1120 case bfd_mach_i960_ka_sa
:
1133 *magicp
= MC68MAGIC
;
1138 *magicp
= MC88MAGIC
;
1143 default: /* Unknown architecture */
1152 coff_set_arch_mach(abfd
, arch
, machine
)
1154 enum bfd_architecture arch
;
1155 unsigned long machine
;
1159 abfd
->obj_arch
= arch
;
1160 abfd
->obj_machine
= machine
;
1161 if (arch
!= bfd_arch_unknown
&&
1162 coff_set_flags(abfd
, &dummy1
, &dummy2
) != true)
1163 return false; /* We can't represent this type */
1164 return true; /* We're easy ... */
1168 /* Calculate the file position for each section. */
1171 coff_compute_section_file_positions(abfd
)
1175 file_ptr sofar
= FILHSZ
;
1176 if (bfd_get_start_address(abfd
)) {
1178 A start address may have been added to the original file. In this
1179 case it will need an optional header to record it.
1181 abfd
->flags
|= EXEC_P
;
1183 if (abfd
->flags
& EXEC_P
)
1187 sofar
+= abfd
->section_count
* SCNHSZ
;
1189 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1190 current
->filepos
= sofar
;
1191 /* Only add sections which have contents */
1192 if (current
->flags
& SEC_HAS_CONTENTS
)
1193 sofar
+= current
->size
;
1195 obj_relocbase(abfd
) = sofar
;
1204 coff_write_object_contents(abfd
)
1207 struct filehdr file_header
;
1209 boolean hasrelocs
= false;
1210 boolean haslinno
= false;
1211 file_ptr reloc_base
;
1212 file_ptr lineno_base
;
1216 unsigned long reloc_size
= 0;
1217 unsigned long lnno_size
= 0;
1218 asection
*text_sec
= NULL
;
1219 asection
*data_sec
= NULL
;
1220 asection
*bss_sec
= NULL
;
1223 bfd_error
= system_call_error
;
1226 if(abfd
->output_has_begun
== false) {
1227 coff_compute_section_file_positions(abfd
);
1229 scn_base
= (file_ptr
) (sizeof(struct filehdr
)
1230 + ((abfd
->flags
& EXEC_P
) ? sizeof(AOUTHDR
) : 0));
1232 if (bfd_seek(abfd
, scn_base
, SEEK_SET
) != 0)
1234 reloc_base
= obj_relocbase(abfd
);
1241 Make a pass through the symbol table to count line number entries and
1242 put them into the correct asections
1244 coff_count_linenumbers(abfd
);
1245 data_base
= scn_base
;
1246 /* Work out the size of the reloc and linno areas */
1248 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1249 reloc_size
+= current
->reloc_count
* sizeof(struct reloc
);
1250 lnno_size
+= current
->lineno_count
* sizeof(struct lineno
);
1251 data_base
+= sizeof(struct scnhdr
);
1255 lineno_base
= reloc_base
+ reloc_size
;
1256 sym_base
= lineno_base
+ lnno_size
;
1258 /* Indicate in each section->line_filepos its actual file address */
1259 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1260 if (current
->lineno_count
) {
1261 current
->line_filepos
= lineno_base
;
1262 current
->moving_line_filepos
= lineno_base
;
1263 lineno_base
+= current
->lineno_count
* sizeof(struct lineno
);
1267 current
->line_filepos
= 0;
1269 if (current
->reloc_count
) {
1270 current
->rel_filepos
= reloc_base
;
1271 reloc_base
+= current
->reloc_count
* sizeof(struct reloc
);
1274 current
->rel_filepos
= 0;
1281 (file_ptr
) ((abfd
->flags
& EXEC_P
) ?
1282 (FILHSZ
+ AOUTSZ
) : FILHSZ
),
1286 unsigned int pad
= abfd
->flags
& D_PAGED
? data_base
: 0;
1288 unsigned int pad
= 0;
1289 for (current
= abfd
->sections
; current
!= NULL
; current
= current
->next
) {
1291 strncpy(&(section
.s_name
[0]), current
->name
, 8);
1292 section
.s_vaddr
= current
->vma
+ pad
;
1293 section
.s_paddr
= current
->vma
+ pad
;
1294 section
.s_size
= current
->size
- pad
;
1296 If this section has no size or is unloadable then the scnptr
1299 if (current
->size
- pad
== 0 ||
1300 (current
->flags
& SEC_LOAD
) == 0) {
1301 section
.s_scnptr
= 0;
1305 section
.s_scnptr
= current
->filepos
;
1307 section
.s_relptr
= current
->rel_filepos
;
1308 section
.s_lnnoptr
= current
->line_filepos
;
1309 section
.s_nreloc
= current
->reloc_count
;
1310 section
.s_nlnno
= current
->lineno_count
;
1311 if (current
->reloc_count
!= 0)
1313 if (current
->lineno_count
!= 0)
1316 if (!strcmp(current
->name
, _TEXT
)) {
1318 section
.s_flags
= STYP_TEXT
; /* kind stupid optimisation */
1322 if (!strcmp(current
->name
, _DATA
)) {
1324 section
.s_flags
= STYP_DATA
; /* kind stupid
1327 else if (!strcmp(current
->name
, _BSS
)) {
1329 section
.s_flags
= STYP_BSS
; /* kind stupid optimisation */
1335 section
.s_align
= (current
->alignment_power
1336 ? 1 << current
->alignment_power
1340 swap_scnhdr(abfd
, §ion
);
1341 bfd_write((void *) (§ion
), 1, SCNHSZ
, abfd
);
1346 /* OK, now set up the filehdr... */
1348 bfd_h_put_x(abfd
, abfd
->section_count
, &file_header
.f_nscns
);
1350 We will NOT put a fucking timestamp in the header here. Every time you
1351 put it back, I will come in and take it out again. I'm sorry. This
1352 field does not belong here. We fill it with a 0 so it compares the
1353 same but is not a reasonable time. -- gnu@cygnus.com
1356 Well, I like it, so I'm conditionally compiling it in.
1359 #ifdef COFF_TIMESTAMP
1360 bfd_h_put_x(abfd
, time(0), &file_header
.f_timdat
);
1362 bfd_h_put_x(abfd
, 0, &file_header
.f_timdat
);
1365 if (bfd_get_symcount(abfd
) != 0)
1366 bfd_h_put_x(abfd
, sym_base
, &file_header
.f_symptr
);
1368 bfd_h_put_x(abfd
, 0, &file_header
.f_symptr
);
1370 file_header
.f_flags
= 0;
1372 if (abfd
->flags
& EXEC_P
)
1373 bfd_h_put_x(abfd
, sizeof(AOUTHDR
), &file_header
.f_opthdr
);
1375 bfd_h_put_x(abfd
, 0, &file_header
.f_opthdr
);
1378 file_header
.f_flags
|= F_RELFLG
;
1380 file_header
.f_flags
|= F_LNNO
;
1381 if (0 == bfd_get_symcount(abfd
))
1382 file_header
.f_flags
|= F_LSYMS
;
1383 if (abfd
->flags
& EXEC_P
)
1384 file_header
.f_flags
|= F_EXEC
;
1385 if (!abfd
->xvec
->byteorder_big_p
)
1386 file_header
.f_flags
|= F_AR32WR
;
1388 FIXME, should do something about the other byte orders and
1392 /* Set up architecture-dependent stuff */
1396 coff_set_flags(abfd
, &magic
, &flags
);
1397 file_header
.f_flags
|= flags
;
1399 bfd_h_put_x(abfd
, magic
, &file_header
.f_magic
);
1400 bfd_h_put_x(abfd
, file_header
.f_flags
, &file_header
.f_flags
);
1402 /* ...and the "opt"hdr... */
1404 bfd_h_put_x(abfd
, (magic
== I960ROMAGIC
? NMAGIC
: OMAGIC
),
1405 &(exec_hdr(abfd
)->magic
));
1408 exec_hdr(abfd
)->magic
= PAGEMAGIC3
;
1411 /* Now should write relocs, strings, syms */
1412 obj_sym_filepos(abfd
) = sym_base
;
1414 if (bfd_get_symcount(abfd
) != 0) {
1415 coff_mangle_symbols(abfd
);
1416 coff_write_symbols(abfd
);
1417 coff_write_linenumbers(abfd
);
1418 coff_write_relocs(abfd
);
1421 bfd_h_put_x(abfd
, text_sec
->size
, &exec_hdr(abfd
)->tsize
);
1423 text_sec
->size
? text_sec
->vma
: 0,
1424 &exec_hdr(abfd
)->text_start
);
1427 bfd_h_put_x(abfd
, data_sec
->size
, &exec_hdr(abfd
)->dsize
);
1429 data_sec
->size
? data_sec
->vma
: 0,
1430 &exec_hdr(abfd
)->data_start
);
1433 bfd_h_put_x(abfd
, bss_sec
->size
, &exec_hdr(abfd
)->bsize
);
1435 bfd_h_put_x(abfd
, bfd_get_start_address(abfd
), &exec_hdr(abfd
)->entry
);
1438 /* now write them */
1439 bfd_h_put_x(abfd
, bfd_get_symcount(abfd
), &file_header
.f_nsyms
);
1440 if (bfd_seek(abfd
, 0L, SEEK_SET
) != 0)
1443 bfd_write((void *) &file_header
, 1, FILHSZ
, abfd
);
1445 if (abfd
->flags
& EXEC_P
) {
1447 bfd_write((void *) exec_hdr(abfd
), 1, AOUTSZ
, abfd
);
1455 coff_set_section_contents(abfd
, section
, location
, offset
, count
)
1462 if (abfd
->output_has_begun
== false) /* set by bfd.c handler */
1463 coff_compute_section_file_positions(abfd
);
1465 bfd_seek(abfd
, (file_ptr
) (section
->filepos
+ offset
), SEEK_SET
);
1468 return (bfd_write(location
, 1, count
, abfd
) == count
) ? true : false;
1473 coff_get_section_contents(abfd
, section
, location
, offset
, count
)
1481 || offset
>= section
->size
1482 || bfd_seek(abfd
, section
->filepos
+ offset
, SEEK_SET
) == -1
1483 || bfd_read(location
, 1, count
, abfd
) != count
) {
1487 } /* coff_get_section_contents() */
1491 coff_close_and_cleanup(abfd
)
1494 if (!bfd_read_p(abfd
))
1495 switch (abfd
->format
) {
1497 if (!_bfd_write_archive_contents(abfd
))
1501 if (!coff_write_object_contents(abfd
))
1505 bfd_error
= invalid_operation
;
1509 #define cleaner(ptr) if (abfd->ptr) free (abfd->ptr)
1512 if (abfd
->my_archive
)
1525 buy_and_read(abfd
, where
, seek_direction
, size
)
1531 void *area
= (void *) malloc(size
);
1533 bfd_error
= no_memory
;
1536 bfd_seek(abfd
, where
, seek_direction
);
1537 if (bfd_read(area
, 1, size
, abfd
) != size
) {
1538 bfd_error
= system_call_error
;
1543 } /* buy_and_read() */
1546 offset_symbol_indices(symtab
, count
, offset
)
1548 unsigned long count
;
1551 SYMENT
*end
= symtab
+ count
;
1552 for (; symtab
< end
; ++symtab
) {
1553 if (symtab
->n_sclass
== C_FILE
) {
1554 symtab
->n_value
= 0;
1556 else if (symtab
->n_sclass
== C_ALIAS
) {
1558 These guys have indices in their values.
1560 symtab
->n_value
+= offset
;
1563 else if (symtab
->n_numaux
) {
1565 anybody else without an aux, has no indices.
1568 if (symtab
->n_sclass
== C_EOS
1569 || (BTYPE(symtab
->n_type
) == T_STRUCT
1570 && symtab
->n_sclass
!= C_STRTAG
)
1571 || BTYPE(symtab
->n_type
) == T_UNION
1572 || BTYPE(symtab
->n_type
) == T_ENUM
) {
1574 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tagndx
+= offset
;
1576 } /* These guys have a tagndx */
1577 if (symtab
->n_sclass
== C_STRTAG
1578 || symtab
->n_sclass
== C_UNTAG
1579 || symtab
->n_sclass
== C_ENTAG
1580 || symtab
->n_sclass
== C_BLOCK
1581 || symtab
->n_sclass
== C_FCN
1582 || ISFCN(symtab
->n_type
)) {
1584 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_fcnary
.x_fcn
.x_endndx
+= offset
;
1586 } /* These guys have an endndx */
1588 if (ISFCN(symtab
->n_type
)) {
1589 ((AUXENT
*) (symtab
+ 1))->x_sym
.x_tvndx
+= offset
;
1590 } /* These guys have a tvndx. I think...
1592 #endif /* Not I960 */
1594 } /* if value, else if aux */
1595 symtab
+= symtab
->n_numaux
;
1596 } /* walk the symtab */
1599 } /* offset_symbol_indices() */
1600 /* swap the entire symbol table */
1602 swap_raw_symtab(abfd
, raw_symtab
)
1607 SYMENT
*end
= raw_symtab
+ bfd_get_symcount(abfd
);
1608 for (; raw_symtab
< end
; ++raw_symtab
) {
1609 bfd_coff_swap_sym(abfd
, raw_symtab
);
1611 for (i
= raw_symtab
->n_numaux
; i
; --i
, ++raw_symtab
) {
1612 bfd_coff_swap_aux(abfd
,
1615 raw_symtab
->n_sclass
);
1616 } /* swap all the aux entries */
1617 } /* walk the symbol table */
1620 } /* swap_raw_symtab() */
1622 read a symbol table into freshly mallocated memory, swap it, and knit the
1623 symbol names into a normalized form. By normalized here I mean that all
1624 symbols have an n_offset pointer that points to a NULL terminated string.
1625 Oh, and the first symbol MUST be a C_FILE. If there wasn't one there
1626 before, put one there.
1630 get_normalized_symtab(abfd
)
1636 char *string_table
= NULL
;
1638 unsigned long string_table_size
= 0;
1640 I used to SEEK_END here to read the symtab and string table all at
1641 once. This fails if this bfd is really an archive element. Thus, the
1642 somewhat convoluted approach to reading in the string table. xoxorich.
1645 $if ((bfd_seek(abfd, 0, SEEK_END) == -1) || ((end_of_file =
1646 bfd_tell(abfd)) == -1) || (bfd_seek(abfd, obj_sym_filepos(abfd),
1647 SEEK_SET) == -1) || ((beginning_of_symtab = bfd_tell(abfd)) == -1)) {
1649 bfd_error = system_call_error; return(NULL); }$
1652 /* $if ((size = end_of_file - beginning_of_symtab) == 0) {$ */
1654 if ((size
= bfd_get_symcount(abfd
) * sizeof(SYMENT
)) == 0) {
1655 bfd_error
= no_symbols
;
1659 This is a hack. Some tool chains fail to put a C_FILE symbol at the
1660 beginning of the symbol table. To make life simpler for our users, we
1661 inject one if it wasn't there originally.
1663 We'd like to keep all of this bfd's native symbols in one block to keep
1664 table traversals simple. To do that, we need to know whether we will
1665 be prepending the C_FILE symbol before we read the rest of the table.
1667 if ((s
= (SYMENT
*) malloc(sizeof(SYMENT
) * 2)) == NULL
) {
1668 bfd_error
= no_memory
;
1671 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1672 || bfd_read(s
, sizeof(SYMENT
), 1, abfd
) != sizeof(SYMENT
)) {
1673 bfd_error
= system_call_error
;
1676 bfd_coff_swap_sym(abfd
, s
);
1678 if (s
->n_sclass
== C_FILE
) {
1679 obj_symbol_slew(abfd
) = 0;
1681 if ((retval
= (SYMENT
*) malloc(size
)) == NULL
) {
1682 bfd_error
= no_memory
;
1684 } /* on malloc error */
1687 unsigned long namelength
= 0;
1689 obj_symbol_slew(abfd
) = 2;
1691 if ((retval
= (SYMENT
*) malloc(size
1692 + (obj_symbol_slew(abfd
)
1693 * sizeof(SYMENT
)))) == NULL
) {
1694 bfd_error
= no_memory
;
1696 } /* on malloc error */
1697 bzero((char *) retval
, size
+ (obj_symbol_slew(abfd
) * sizeof(SYMENT
)));
1699 #define FILE_ENTRY_NAME ".file"
1701 if ((retval
->n_offset
= (int) malloc(strlen(FILE_ENTRY_NAME
)
1703 bfd_error
= no_memory
;
1705 } /* on malloc error */
1706 strcpy((char *) retval
->n_offset
, FILE_ENTRY_NAME
);
1707 retval
->n_sclass
= C_FILE
;
1708 retval
->n_scnum
= N_DEBUG
;
1709 retval
->n_numaux
= 1;
1711 #undef FILE_ENTRY_NAME
1713 if ((filename
= bfd_get_filename(abfd
)) == NULL
) {
1715 } /* if we know it's name */
1716 if ((namelength
= strlen(filename
)) <= FILNMLEN
) {
1717 strncpy(((AUXENT
*) (retval
+ 1))->x_file
.x_fname
, filename
, FILNMLEN
);
1720 if ((((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
1721 = (int) malloc(namelength
+1)) == NULL
) {
1722 bfd_error
= no_memory
;
1725 strcpy((char *) (((AUXENT
*) (retval
+ 1))->x_file
.x_n
.x_offset
),
1728 } /* if "short" name */
1729 } /* missing file entry. */
1733 if (bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
) == -1
1734 || bfd_read(retval
+ obj_symbol_slew(abfd
), size
, 1, abfd
) != size
) {
1735 bfd_error
= system_call_error
;
1737 } /* on read error */
1738 /* mark the end of the symbols */
1739 end
= retval
+ obj_symbol_slew(abfd
) + bfd_get_symcount(abfd
);
1741 FIXME SOMEDAY. A string table size of zero is very weird, but
1742 probably possible. If one shows up, it will probably kill us.
1745 swap_raw_symtab(abfd
, retval
+ obj_symbol_slew(abfd
));
1747 /* ...and normalize symbol names. */
1749 for (s
= retval
+ obj_symbol_slew(abfd
); s
< end
; ++s
) {
1750 if (s
->n_zeroes
!= 0) {
1752 This is a "short" name. Make it long.
1754 unsigned long i
= 0;
1755 char *newstring
= NULL
;
1757 find the length of this string without walking into memory
1761 for (i
= 0; i
<= 8; ++i
) {
1762 if (s
->n_name
[i
] == '\0') {
1764 } /* if end of string */
1765 } /* possible lengths of this string. */
1767 if ((newstring
= malloc(++i
)) == NULL
) {
1768 bfd_error
= no_memory
;
1771 bzero(newstring
, i
);
1772 strncpy(newstring
, s
->n_name
, i
-1 );
1773 s
->n_offset
= (int) newstring
;
1778 if (string_table
== NULL
) {
1780 NOTE: we don't read the string table until now because we
1781 don't necessarily know that we have one until now.
1784 At this point we should be "seek"'d to the end of the
1785 symbols === the symbol table size.
1788 if (bfd_read((char *) &string_table_size
, sizeof(string_table_size
), 1, abfd
) != sizeof(string_table_size
)) {
1789 bfd_error
= system_call_error
;
1792 sp(string_table_size
);
1794 if ((string_table
= malloc(string_table_size
-= 4)) == NULL
) {
1795 bfd_error
= no_memory
;
1797 } /* on mallocation error */
1798 if (bfd_read(string_table
, string_table_size
, 1, abfd
) != string_table_size
) {
1799 bfd_error
= system_call_error
;
1802 } /* have not yet read the string table. */
1804 This is a long name already. Just point it at the string in
1807 s
->n_offset
= (int) (string_table
- 4 + s
->n_offset
);
1808 } /* switch on type of symbol name */
1811 } /* for each symbol */
1813 If we had to insert a C_FILE symbol, then everybody's indices are off
1814 by 2, so correct them.
1817 if (obj_symbol_slew(abfd
) > 0) {
1818 offset_symbol_indices(retval
+ 2, bfd_get_symcount(abfd
), 2);
1820 /* and let the world know there are two more of them. */
1821 bfd_get_symcount(abfd
) += 2;
1822 } /* if we added a C_FILE */
1823 obj_raw_syments(abfd
) = retval
;
1824 obj_string_table(abfd
) = string_table
;
1827 } /* get_normalized_symtab() */
1831 section_from_bfd_index(abfd
, index
)
1836 struct sec_struct
*answer
= abfd
->sections
;
1838 answer
= answer
->next
;
1846 coff_get_symcount_upper_bound(ignore_abfd
)
1854 coff_get_first_symbol(ignore_abfd
)
1861 coff_get_next_symbol(abfd
, oidx
)
1865 if (oidx
== BFD_NO_MORE_SYMBOLS
)
1866 return BFD_NO_MORE_SYMBOLS
;
1867 return ++oidx
>= bfd_get_symcount(abfd
) ? BFD_NO_MORE_SYMBOLS
: oidx
;
1871 coff_symbol_name(abfd
, idx
)
1875 return (obj_symbols(abfd
) + idx
)->symbol
.name
;
1879 coff_symbol_value(abfd
, idx
)
1883 return (obj_symbols(abfd
) + idx
)->symbol
.value
;
1887 coff_classify_symbol(abfd
, idx
)
1891 coff_symbol_type
*sym
= obj_symbols(abfd
) + idx
;
1892 if ((sym
->symbol
.flags
& BSF_FORT_COMM
) != 0)
1893 return bfd_symclass_fcommon
;
1894 if ((sym
->symbol
.flags
& BSF_GLOBAL
) != 0)
1895 return bfd_symclass_global
;
1896 if ((sym
->symbol
.flags
& BSF_DEBUGGING
) != 0)
1897 return bfd_symclass_debugger
;
1898 if ((sym
->symbol
.flags
& BSF_UNDEFINED
) != 0)
1899 return bfd_symclass_undefined
;
1901 return bfd_symclass_unknown
;
1905 coff_symbol_hasclass(abfd
, idx
, class)
1911 coff_symbol_type
*sym
= obj_symbols(abfd
) + idx
;
1913 case bfd_symclass_fcommon
:
1914 return (sym
->symbol
.flags
& BSF_FORT_COMM
) ? true : false;
1915 case bfd_symclass_global
:
1916 return (sym
->symbol
.flags
& BSF_GLOBAL
) ? true : false;
1917 case bfd_symclass_debugger
:
1918 return (sym
->symbol
.flags
& BSF_DEBUGGING
) ? true : false;;
1919 case bfd_symclass_undefined
:
1920 return (sym
->symbol
.flags
& BSF_UNDEFINED
) ? true : false;;
1932 coff_slurp_line_table(abfd
, asect
)
1936 struct lineno
*native_lineno
;
1937 alent
*lineno_cache
;
1938 BFD_ASSERT(asect
->lineno
== (alent
*) NULL
);
1941 native_lineno
= (struct lineno
*) buy_and_read(abfd
,
1942 asect
->line_filepos
,
1944 (size_t) (sizeof(struct lineno
) *
1945 asect
->lineno_count
));
1947 (alent
*) malloc((size_t) ((asect
->lineno_count
+ 1) * sizeof(alent
)));
1948 if (lineno_cache
== NULL
) {
1949 bfd_error
= no_memory
;
1950 return (BFD_FAILURE
);
1952 unsigned int counter
= 0;
1953 alent
*cache_ptr
= lineno_cache
;
1954 struct lineno
*src
= native_lineno
;
1955 while (counter
< asect
->lineno_count
) {
1956 bfd_coff_swap_lineno(abfd
, src
);
1957 cache_ptr
->line_number
= src
->l_lnno
;
1959 if (cache_ptr
->line_number
== 0) {
1960 coff_symbol_type
*sym
=
1961 (coff_symbol_type
*) (src
->l_addr
.l_symndx
1962 + obj_symbol_slew(abfd
)
1963 + obj_raw_syments(abfd
))->n_zeroes
;
1964 cache_ptr
->u
.sym
= (asymbol
*) sym
;
1965 sym
->lineno
= cache_ptr
;
1968 cache_ptr
->u
.offset
= src
->l_addr
.l_paddr
1969 - bfd_section_vma(abfd
, asect
);
1970 } /* If no linenumber expect a symbol index */
1976 cache_ptr
->line_number
= 0;
1979 free(native_lineno
);
1980 asect
->lineno
= lineno_cache
;
1982 } /* coff_slurp_line_table() */
1985 find_next_file_symbol(current
, end
)
1989 /* ignore the first symbol which is probably a C_FILE. */
1991 current
+= current
->n_numaux
+ 1;
1993 for (; current
< end
; ++current
) {
1994 if (current
->n_sclass
== C_FILE
) {
1997 current
+= current
->n_numaux
;
1998 } /* walk the remaining table */
2002 } /* find_next_file_symbol() */
2004 Note that C_FILE symbols can, and some do, have more than 1 aux entry.
2008 force_indices_file_symbol_relative(abfd
, symtab
)
2012 SYMENT
*end
= symtab
+ bfd_get_symcount(abfd
);
2015 /* the first symbol had damn well better be a C_FILE. */
2016 BFD_ASSERT(symtab
->n_sclass
== C_FILE
);
2017 symtab
->n_value
= 0;
2019 for (current
= find_next_file_symbol(symtab
, end
);
2022 offset_symbol_indices(current
,
2024 find_next_file_symbol(current
,
2027 } /* walk the table */
2030 } /* force_indices_file_symbol_relative() */
2033 coff_slurp_symbol_table(abfd
)
2036 SYMENT
*native_symbols
;
2037 coff_symbol_type
*cached_area
;
2038 unsigned int *table_ptr
;
2039 char *string_table
= (char *) NULL
;
2040 unsigned int number_of_symbols
= 0;
2041 if (obj_symbols(abfd
))
2043 bfd_seek(abfd
, obj_sym_filepos(abfd
), SEEK_SET
);
2045 /* Read in the symbol table */
2046 if ((native_symbols
= get_normalized_symtab(abfd
)) == NULL
) {
2049 force_indices_file_symbol_relative(abfd
, native_symbols
);
2051 /* Allocate enough room for all the symbols in cached form */
2053 (coff_symbol_type
*)
2054 malloc((size_t) (bfd_get_symcount(abfd
) * sizeof(coff_symbol_type
)));
2056 if (cached_area
== NULL
) {
2057 bfd_error
= no_memory
;
2058 return (BFD_FAILURE
);
2062 malloc((size_t) (bfd_get_symcount(abfd
) * sizeof(unsigned int)));
2064 if (table_ptr
== NULL
) {
2065 bfd_error
= no_memory
;
2066 return (BFD_FAILURE
);
2068 coff_symbol_type
*dst
= cached_area
;
2069 unsigned int last_native_index
= bfd_get_symcount(abfd
);
2070 unsigned int this_index
= 0;
2071 while (this_index
< last_native_index
) {
2072 SYMENT
*src
= native_symbols
+ this_index
;
2073 table_ptr
[this_index
] = number_of_symbols
;
2074 dst
->symbol
.the_bfd
= abfd
;
2076 dst
->symbol
.name
= (char *) src
->n_offset
; /* which was normalized
2078 terminated string. */
2080 We use the native name field to point to the cached field
2082 src
->n_zeroes
= (int) dst
;
2083 dst
->symbol
.section
= section_from_bfd_index(abfd
, src
->n_scnum
);
2085 switch (src
->n_sclass
) {
2089 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2090 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
2091 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
2093 /* Fall through to next case */
2098 if (src
->n_scnum
== 0) {
2099 if (src
->n_value
== 0) {
2100 dst
->symbol
.flags
= BSF_UNDEFINED
;
2103 dst
->symbol
.flags
= BSF_FORT_COMM
;
2104 dst
->symbol
.value
= src
->n_value
;
2109 Base the value as an index from the base of the
2112 if (dst
->symbol
.section
== (asection
*) NULL
) {
2113 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
| BSF_ABSOLUTE
;
2114 dst
->symbol
.value
= src
->n_value
;
2117 dst
->symbol
.flags
= BSF_EXPORT
| BSF_GLOBAL
;
2118 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2120 if (ISFCN(src
->n_type
)) {
2122 A function ext does not go at the end of a file
2124 dst
->symbol
.flags
|= BSF_NOT_AT_END
;
2129 case C_STAT
: /* static */
2131 case C_LEAFSTAT
: /* static leaf procedure */
2133 case C_LABEL
: /* label */
2134 dst
->symbol
.flags
= BSF_LOCAL
;
2136 Base the value as an index from the base of the section
2138 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2141 case C_MOS
: /* member of structure */
2142 case C_EOS
: /* end of structure */
2143 case C_REGPARM
: /* register parameter */
2144 case C_REG
: /* register variable */
2146 case C_AUTOARG
: /* 960-specific storage class */
2148 case C_TPDEF
: /* type definition */
2151 case C_AUTO
: /* automatic variable */
2152 case C_FIELD
: /* bit field */
2153 case C_ENTAG
: /* enumeration tag */
2154 case C_MOE
: /* member of enumeration */
2155 case C_MOU
: /* member of union */
2156 case C_UNTAG
: /* union tag */
2158 dst
->symbol
.flags
= BSF_DEBUGGING
;
2159 dst
->symbol
.value
= src
->n_value
;
2162 case C_FILE
: /* file name */
2163 case C_STRTAG
: /* structure tag */
2164 dst
->symbol
.flags
= BSF_DEBUGGING
;
2165 dst
->symbol
.value
= src
->n_value
;
2168 case C_BLOCK
: /* ".bb" or ".eb" */
2169 case C_FCN
: /* ".bf" or ".ef" */
2170 dst
->symbol
.flags
= BSF_LOCAL
;
2172 Base the value as an index from the base of the section
2174 dst
->symbol
.value
= src
->n_value
- dst
->symbol
.section
->vma
;
2177 case C_EFCN
: /* physical end of function */
2179 case C_EXTDEF
: /* external definition */
2180 case C_ULABEL
: /* undefined label */
2181 case C_USTATIC
: /* undefined static */
2182 case C_LINE
: /* line # reformatted as symbol table entry */
2183 case C_ALIAS
: /* duplicate tag */
2184 case C_HIDDEN
: /* ext symbol in dmert public lib */
2188 printf("SICK%d\n", src
->n_sclass
);
2190 dst
->symbol
.flags
= BSF_DEBUGGING
;
2191 dst
->symbol
.value
= src
->n_value
;
2199 BFD_ASSERT(dst
->symbol
.flags
!= 0);
2203 dst
->symbol
.udata
= 0;
2204 dst
->lineno
= (alent
*) NULL
;
2205 this_index
+= src
->n_numaux
+ 1;
2207 number_of_symbols
++;
2208 } /* walk the native symtab */
2210 } /* bfdize the native symtab */
2212 obj_symbols(abfd
) = cached_area
;
2213 obj_raw_syments(abfd
) = native_symbols
;
2215 bfd_get_symcount(abfd
) = number_of_symbols
;
2216 obj_convert(abfd
) = table_ptr
;
2217 /* Slurp the line tables for each section too */
2222 coff_slurp_line_table(abfd
, p
);
2227 } /* coff_slurp_symbol_table() */
2230 coff_get_symtab_upper_bound(abfd
)
2233 if (!coff_slurp_symbol_table(abfd
))
2236 return (bfd_get_symcount(abfd
) + 1) * (sizeof(coff_symbol_type
*));
2241 coff_get_symtab(abfd
, alocation
)
2243 asymbol
**alocation
;
2245 unsigned int counter
= 0;
2246 coff_symbol_type
*symbase
;
2247 coff_symbol_type
**location
= (coff_symbol_type
**) (alocation
);
2248 if (!coff_slurp_symbol_table(abfd
))
2251 for (symbase
= obj_symbols(abfd
); counter
++ < bfd_get_symcount(abfd
);)
2252 *(location
++) = symbase
++;
2254 return bfd_get_symcount(abfd
);
2258 coff_get_reloc_upper_bound(abfd
, asect
)
2262 if (bfd_get_format(abfd
) != bfd_object
) {
2263 bfd_error
= invalid_operation
;
2266 return (asect
->reloc_count
+ 1) * sizeof(arelent
*);
2271 coff_slurp_reloc_table(abfd
, asect
, symbols
)
2276 struct reloc
*native_relocs
;
2277 arelent
*reloc_cache
;
2278 if (asect
->relocation
)
2280 if (asect
->reloc_count
== 0)
2282 if (!coff_slurp_symbol_table(abfd
))
2285 (struct reloc
*) buy_and_read(abfd
,
2288 (size_t) (sizeof(struct reloc
) *
2289 asect
->reloc_count
));
2290 reloc_cache
= (arelent
*)
2291 malloc((size_t) (asect
->reloc_count
* sizeof(arelent
)));
2293 if (reloc_cache
== NULL
) {
2294 bfd_error
= no_memory
;
2295 return (BFD_FAILURE
);
2299 for (cache_ptr
= reloc_cache
,
2300 src
= native_relocs
;
2301 cache_ptr
< reloc_cache
+ asect
->reloc_count
;
2305 swap_reloc(abfd
, src
);
2306 src
->r_symndx
+= obj_symbol_slew(abfd
);
2307 cache_ptr
->sym_ptr_ptr
= symbols
+ obj_convert(abfd
)[src
->r_symndx
];
2309 ptr
= *(cache_ptr
->sym_ptr_ptr
);
2310 cache_ptr
->address
= src
->r_vaddr
;
2312 The symbols definitions that we have read in have been
2313 relocated as if their sections started at 0. But the offsets
2314 refering to the symbols in the raw data have not been
2315 modified, so we have to have a negative addend to compensate.
2318 if (ptr
->the_bfd
== abfd
&& ptr
->section
!= (asection
*) NULL
) {
2319 cache_ptr
->addend
= -ptr
->section
->vma
;
2322 cache_ptr
->addend
= 0;
2325 cache_ptr
->address
-= asect
->vma
;
2327 cache_ptr
->section
= (asection
*) NULL
;
2330 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2333 if (src
->r_type
>= R_PCR16L
&& src
->r_type
<= R_VRT32
) {
2334 cache_ptr
->howto
= howto_table
+ src
->r_type
- R_PCR16L
;
2341 cache_ptr
->howto
= howto_table
+ src
->r_type
;
2348 free(native_relocs
);
2349 asect
->relocation
= reloc_cache
;
2354 /* This is stupid. This function should be a boolean predicate */
2356 coff_canonicalize_reloc(abfd
, section
, relptr
, symbols
)
2362 arelent
*tblptr
= section
->relocation
;
2363 unsigned int count
= 0;
2364 if (!(tblptr
|| coff_slurp_reloc_table(abfd
, section
, symbols
)))
2366 tblptr
= section
->relocation
;
2370 for (; count
++ < section
->reloc_count
;)
2371 *relptr
++ = tblptr
++;
2375 return section
->reloc_count
;
2383 provided a bfd, a section and an offset into the section, calculate and
2384 return the name of the source file and the line nearest to the wanted
2389 coff_find_nearest_line(abfd
,
2400 char **filename_ptr
;
2401 char **functionname_ptr
;
2402 unsigned int *line_ptr
;
2404 static bfd
*cache_abfd
;
2405 static asection
*cache_section
;
2406 static bfd_vma cache_offset
;
2407 static unsigned int cache_i
;
2408 static alent
*cache_l
;
2411 struct icofdata
*cof
= obj_icof(abfd
);
2412 /* Run through the raw syments if available */
2415 unsigned int line_base
= 0;
2419 *functionname_ptr
= 0;
2422 /* Don't try and find line numbers in a non coff file */
2423 if (abfd
->xvec
->flavour
!= bfd_target_coff_flavour_enum
)
2426 if (cof
== (struct icofdata
*)NULL
)
2430 p
= cof
->raw_syments
;
2432 I don't know for sure what's right, but this isn't it. First off, an
2433 object file may not have any C_FILE's in it. After
2434 get_normalized_symtab(), it should have at least 1, the one I put
2435 there, but otherwise, all bets are off. Point #2, the first C_FILE
2436 isn't necessarily the right C_FILE because any given object may have
2437 many. I think you'll have to track sections as they coelesce in order
2438 to find the C_STAT symbol for this section. Then you'll have to work
2439 backwards to find the previous C_FILE, or choke if you get to a C_STAT
2440 for the same kind of section. That will mean that the original object
2441 file didn't have a C_FILE. xoxorich.
2444 #ifdef WEREBEINGPEDANTIC
2450 for (i
= 0; i
< cof
->raw_syment_count
; i
++) {
2451 if (p
->n_sclass
== C_FILE
) {
2452 /* File name is embeded in auxent */
2454 This isn't right. The fname should probably be normalized
2455 during get_normalized_symtab(). In any case, what was here
2456 wasn't right because a SYMENT.n_name isn't an
2457 AUXENT.x_file.x_fname. xoxorich.
2460 *filename_ptr
= ((AUXENT
*) (p
+ 1))->x_file
.x_fname
;
2463 p
+= 1 + p
->n_numaux
;
2465 /* Now wander though the raw linenumbers of the section */
2471 If this is the same bfd as we were previously called with and this is
2472 the same section, and the offset we want is further down then we can
2473 prime the lookup loop
2475 if (abfd
== cache_abfd
&&
2476 section
== cache_section
&&
2477 offset
>= cache_offset
) {
2483 l
= section
->lineno
;
2486 for (; i
< section
->lineno_count
; i
++) {
2487 if (l
->line_number
== 0) {
2488 /* Get the symbol this line number points at */
2489 coff_symbol_type
*coff
= (coff_symbol_type
*) (l
->u
.sym
);
2490 *functionname_ptr
= coff
->symbol
.name
;
2492 struct syment
*s
= coff
->native
;
2493 s
= s
+ 1 + s
->n_numaux
;
2495 S should now point to the .bf of the function
2499 The linenumber is stored in the auxent
2501 union auxent
*a
= (union auxent
*) (s
+ 1);
2502 line_base
= a
->x_sym
.x_misc
.x_lnsz
.x_lnno
;
2507 if (l
->u
.offset
> offset
)
2509 *line_ptr
= l
->line_number
+ line_base
+ 1;
2515 cache_section
= section
;
2516 cache_offset
= offset
;