1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
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 3 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., 51 Franklin Street - Fifth Floor, Boston, MA
22 /* Written by Steve Chamberlain (sac@cygnus.com)
24 This program can be used to convert a coff object file
25 into a Hitachi OM/LM (Sysroff) format.
27 All debugging information is preserved */
34 #include "libiberty.h"
37 #include "coff/internal.h"
38 #include "../bfd/libcoff.h"
43 static char *toolname
;
46 static int get_member_id (int);
47 static int get_ordinary_id (int);
48 static char *section_translate (char *);
49 static char *strip_suffix (const char *);
50 static void checksum (FILE *, unsigned char *, int, int);
51 static void writeINT (int, unsigned char *, int *, int, FILE *);
52 static void writeBITS (int, unsigned char *, int *, int);
53 static void writeBARRAY (barray
, unsigned char *, int *, int, FILE *);
54 static void writeCHARS (char *, unsigned char *, int *, int, FILE *);
55 static void wr_tr (void);
56 static void wr_un (struct coff_ofile
*, struct coff_sfile
*, int, int);
57 static void wr_hd (struct coff_ofile
*);
58 static void wr_sh (struct coff_ofile
*, struct coff_section
*);
59 static void wr_ob (struct coff_ofile
*, struct coff_section
*);
60 static void wr_rl (struct coff_ofile
*, struct coff_section
*);
61 static void wr_object_body (struct coff_ofile
*);
62 static void wr_dps_start
63 (struct coff_sfile
*, struct coff_section
*, struct coff_scope
*, int, int);
64 static void wr_dps_end (struct coff_section
*, struct coff_scope
*, int);
65 static int *nints (int);
66 static void walk_tree_type_1
67 (struct coff_sfile
*, struct coff_symbol
*, struct coff_type
*, int);
68 static void walk_tree_type
69 (struct coff_sfile
*, struct coff_symbol
*, struct coff_type
*, int);
70 static void walk_tree_symbol
71 (struct coff_sfile
*, struct coff_section
*, struct coff_symbol
*, int);
72 static void walk_tree_scope
73 (struct coff_section
*, struct coff_sfile
*, struct coff_scope
*, int, int);
74 static void walk_tree_sfile (struct coff_section
*, struct coff_sfile
*);
75 static void wr_program_structure (struct coff_ofile
*, struct coff_sfile
*);
76 static void wr_du (struct coff_ofile
*, struct coff_sfile
*, int);
77 static void wr_dus (struct coff_ofile
*, struct coff_sfile
*);
78 static int find_base (struct coff_sfile
*, struct coff_section
*);
79 static void wr_dln (struct coff_ofile
*, struct coff_sfile
*, int);
80 static void wr_globals (struct coff_ofile
*, struct coff_sfile
*, int);
81 static void wr_debug (struct coff_ofile
*);
82 static void wr_cs (void);
83 static int wr_sc (struct coff_ofile
*, struct coff_sfile
*);
84 static void wr_er (struct coff_ofile
*, struct coff_sfile
*, int);
85 static void wr_ed (struct coff_ofile
*, struct coff_sfile
*, int);
86 static void wr_unit_info (struct coff_ofile
*);
87 static void wr_module (struct coff_ofile
*);
88 static int align (int);
89 static void prescan (struct coff_ofile
*);
90 static void show_usage (FILE *, int);
91 extern int main (int, char **);
97 static int noprescan
= 0;
98 static struct coff_ofile
*tree
;
100 static int absolute_p;
103 static int segmented_p
;
106 static int ids1
[20000];
107 static int ids2
[20000];
109 static int base1
= 0x18;
110 static int base2
= 0x2018;
113 get_member_id (int x
)
123 get_ordinary_id (int x
)
132 section_translate (char *n
)
134 if (strcmp (n
, ".text") == 0)
136 if (strcmp (n
, ".data") == 0)
138 if (strcmp (n
, ".bss") == 0)
143 #define DATE "940201073000"; /* Just a time on my birthday */
146 strip_suffix (const char *name
)
151 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
153 res
= (char *) xmalloc (i
+ 1);
154 memcpy (res
, name
, i
);
159 /* IT LEN stuff CS */
161 checksum (FILE *file
, unsigned char *ptr
, int size
, int code
)
166 int bytes
= size
/ 8;
168 last
= !(code
& 0xff00);
171 ptr
[0] = code
| (last
? 0x80 : 0);
174 for (j
= 0; j
< bytes
; j
++)
177 /* Glue on a checksum too. */
179 fwrite (ptr
, bytes
+ 1, 1, file
);
184 writeINT (int n
, unsigned char *ptr
, int *idx
, int size
, FILE *file
)
195 /* Lets write out that record and do another one. */
196 checksum (file
, ptr
, *idx
, code
| 0x1000);
209 ptr
[byte
+ 0] = n
>> 8;
213 ptr
[byte
+ 0] = n
>> 24;
214 ptr
[byte
+ 1] = n
>> 16;
215 ptr
[byte
+ 2] = n
>> 8;
216 ptr
[byte
+ 3] = n
>> 0;
225 writeBITS (int val
, unsigned char *ptr
, int *idx
, int size
)
234 /* Turn off all about to change bits. */
235 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
236 /* Turn on the bits we want. */
237 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
242 writeBARRAY (barray data
, unsigned char *ptr
, int *idx
,
243 int size ATTRIBUTE_UNUSED
, FILE *file
)
247 writeINT (data
.len
, ptr
, idx
, 1, file
);
248 for (i
= 0; i
< data
.len
; i
++)
249 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
253 writeCHARS (char *string
, unsigned char *ptr
, int *idx
, int size
, FILE *file
)
259 /* Lets write out that record and do another one. */
260 checksum (file
, ptr
, *idx
, code
| 0x1000);
267 /* Variable length string. */
268 size
= strlen (string
);
272 /* BUG WAITING TO HAPPEN. */
273 memcpy (ptr
+ i
, string
, size
);
278 #define SYSROFF_SWAP_OUT
281 static char *rname_sh
[] =
283 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
286 static char *rname_h8300
[] =
288 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
294 /* The TR block is not normal - it doesn't have any contents. */
302 fwrite (b
, 1, sizeof (b
), file
);
306 wr_un (struct coff_ofile
*ptr
, struct coff_sfile
*sfile
, int first
,
307 int nsecs ATTRIBUTE_UNUSED
)
310 struct coff_symbol
*s
;
314 if (bfd_get_file_flags (abfd
) & EXEC_P
)
315 un
.format
= FORMAT_LM
;
317 un
.format
= FORMAT_OM
;
320 /* Don't count the abs section. */
321 un
.nsections
= ptr
->nsections
- 1;
325 /* Count all the undefined and defined variables with global scope. */
329 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
331 if (s
->visible
->type
== coff_vis_ext_def
332 || s
->visible
->type
== coff_vis_common
)
335 if (s
->visible
->type
== coff_vis_ext_ref
)
341 un
.linker
= "L_GX00";
343 un
.name
= sfile
->name
;
344 sysroff_swap_un_out (file
, &un
);
348 wr_hd (struct coff_ofile
*p
)
353 if (bfd_get_file_flags (abfd
) & EXEC_P
)
354 hd
.mt
= MTYPE_ABS_LM
;
356 hd
.mt
= MTYPE_OMS_OR_LMS
;
360 hd
.nu
= p
->nsources
; /* Always one unit */
361 hd
.code
= 0; /* Always ASCII */
362 hd
.ver
= "0200"; /* Version 2.00 */
364 switch (bfd_get_arch (abfd
))
372 switch (bfd_get_mach (abfd
))
378 toolname
= "C_H8/300";
380 case bfd_mach_h8300h
:
384 toolname
= "C_H8/300H";
386 case bfd_mach_h8300s
:
390 toolname
= "C_H8/300S";
395 rnames
= rname_h8300
;
413 if (! bfd_get_file_flags(abfd
) & EXEC_P
)
423 hd
.address
= bfd_get_start_address (abfd
);
428 hd
.mn
= strip_suffix (bfd_get_filename (abfd
));
430 sysroff_swap_hd_out (file
, &hd
);
435 wr_sh (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
439 sh
.section
= sec
->number
;
443 sysroff_swap_sh_out (file
, &sh
);
448 wr_ob (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*section
)
452 unsigned char stuff
[200];
455 while (i
< bfd_get_section_size (section
->bfd_section
))
458 int todo
= 200; /* Copy in 200 byte lumps. */
461 if (i
+ todo
> bfd_get_section_size (section
->bfd_section
))
462 todo
= bfd_get_section_size (section
->bfd_section
) - i
;
467 if (bfd_get_file_flags (abfd
) & EXEC_P
)
468 ob
.address
= section
->address
;
479 ob
.cpf
= 0; /* Never compress. */
481 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
482 ob
.data
.data
= stuff
;
483 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
487 /* Now fill the rest with blanks. */
488 while (i
< (bfd_size_type
) section
->size
)
491 int todo
= 200; /* Copy in 200 byte lumps. */
494 if (i
+ todo
> (bfd_size_type
) section
->size
)
495 todo
= section
->size
- i
;
498 ob
.cpf
= 0; /* Never compress. */
500 memset (stuff
, 0, todo
);
501 ob
.data
.data
= stuff
;
502 sysroff_swap_ob_out (file
, &ob
);
505 /* Now fill the rest with blanks. */
509 wr_rl (struct coff_ofile
*ptr ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
511 int nr
= sec
->nrelocs
;
514 for (i
= 0; i
< nr
; i
++)
516 struct coff_reloc
*r
= sec
->relocs
+ i
;
517 struct coff_symbol
*ref
;
527 rl
.flen
= 32; /* SH Specific. */
529 /* What sort of reloc ? Look in the section to find out. */
531 if (ref
->visible
->type
== coff_vis_ext_ref
)
533 rl
.bcount
= 4; /* Always 4 for us. */
535 rl
.symn
= ref
->er_number
;
537 else if (ref
->visible
->type
== coff_vis_common
)
539 rl
.bcount
= 11; /* Always 11 for us. */
541 rl
.secn
= ref
->where
->section
->number
;
544 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
545 rl
.aopcode_is_0x20
= 0x20;
549 rl
.bcount
= 11; /* Always 11 for us. */
551 rl
.secn
= ref
->where
->section
->number
;
554 rl
.addend
= -ref
->where
->section
->address
;
555 rl
.aopcode_is_0x20
= 0x20;
560 if ( rl
.op
== OP_SEC_REF
561 || rl
.op
== OP_EXT_REF
)
562 sysroff_swap_rl_out (file
, &rl
);
567 wr_object_body (struct coff_ofile
*p
)
571 for (i
= 1; i
< p
->nsections
; i
++)
573 wr_sh (p
, p
->sections
+ i
);
574 wr_ob (p
, p
->sections
+ i
);
575 wr_rl (p
, p
->sections
+ i
);
580 wr_dps_start (struct coff_sfile
*sfile
,
581 struct coff_section
*section ATTRIBUTE_UNUSED
,
582 struct coff_scope
*scope
, int type
, int nest
)
592 dps
.san
= scope
->sec
->number
;
593 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
594 dps
.block_size
= scope
->size
;
598 printf ("DPS %s %d %x\n",
613 sysroff_swap_dps_out (file
, &dps
);
617 wr_dps_end (struct coff_section
*section ATTRIBUTE_UNUSED
,
618 struct coff_scope
*scope ATTRIBUTE_UNUSED
, int type
)
624 sysroff_swap_dps_out (file
, &dps
);
630 return (int *) (xcalloc (sizeof (int), x
));
634 walk_tree_type_1 (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
635 struct coff_type
*type
, int nest
)
639 case coff_secdef_type
:
640 case coff_basic_type
:
644 switch (type
->u
.basic
)
648 dbt
.btype
= BTYPE_VOID
;
649 dbt
.sign
= BTYPE_UNSPEC
;
650 dbt
.fptype
= FPTYPE_NOTSPEC
;
654 dbt
.btype
= BTYPE_CHAR
;
655 dbt
.sign
= BTYPE_UNSPEC
;
656 dbt
.fptype
= FPTYPE_NOTSPEC
;
662 dbt
.btype
= BTYPE_INT
;
663 dbt
.sign
= SIGN_SIGNED
;
664 dbt
.fptype
= FPTYPE_NOTSPEC
;
668 dbt
.btype
= BTYPE_FLOAT
;
669 dbt
.fptype
= FPTYPE_SINGLE
;
673 dbt
.btype
= BTYPE_FLOAT
;
674 dbt
.fptype
= FPTYPE_DOUBLE
;
678 dbt
.btype
= BTYPE_FLOAT
;
679 dbt
.fptype
= FPTYPE_EXTENDED
;
683 dbt
.btype
= BTYPE_CHAR
;
684 dbt
.sign
= SIGN_UNSIGNED
;
685 dbt
.fptype
= FPTYPE_NOTSPEC
;
691 dbt
.btype
= BTYPE_INT
;
692 dbt
.sign
= SIGN_UNSIGNED
;
693 dbt
.fptype
= FPTYPE_NOTSPEC
;
697 dbt
.bitsize
= type
->size
;
699 sysroff_swap_dbt_out (file
, &dbt
);
703 case coff_pointer_type
:
708 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
710 sysroff_swap_dpt_out (file
, &dpt
);
714 case coff_function_type
:
717 struct coff_symbol
*param
;
721 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
724 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
726 sysroff_swap_dfp_out (file
, &dfp
);
728 for (param
= type
->u
.function
.parameters
->vars_head
;
731 walk_tree_symbol (sfile
, 0, param
, nest
);
734 sysroff_swap_dfp_out (file
, &dfp
);
738 case coff_structdef_type
:
742 struct coff_symbol
*member
;
745 dbt
.btype
= BTYPE_STRUCT
;
746 dbt
.bitsize
= type
->size
;
747 dbt
.sign
= SIGN_UNSPEC
;
748 dbt
.fptype
= FPTYPE_NOTSPEC
;
749 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
751 sysroff_swap_dbt_out (file
, &dbt
);
754 sysroff_swap_dds_out (file
, &dds
);
756 for (member
= type
->u
.astructdef
.elements
->vars_head
;
758 member
= member
->next
)
759 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
762 sysroff_swap_dds_out (file
, &dds
);
767 case coff_structref_type
:
771 dbt
.btype
= BTYPE_TAG
;
772 dbt
.bitsize
= type
->size
;
773 dbt
.sign
= SIGN_UNSPEC
;
774 dbt
.fptype
= FPTYPE_NOTSPEC
;
776 if (type
->u
.astructref
.ref
)
777 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
782 sysroff_swap_dbt_out (file
, &dbt
);
786 case coff_array_type
:
790 int dims
= 1; /* Only output one dimension at a time. */
793 dar
.variable
= nints (dims
);
794 dar
.subtype
= nints (dims
);
795 dar
.spare
= nints (dims
);
796 dar
.max_variable
= nints (dims
);
797 dar
.maxspare
= nints (dims
);
798 dar
.max
= nints (dims
);
799 dar
.min_variable
= nints (dims
);
800 dar
.min
= nints (dims
);
801 dar
.minspare
= nints (dims
);
803 dar
.length
= type
->size
/ type
->u
.array
.dim
;
805 for (j
= 0; j
< dims
; j
++)
807 dar
.variable
[j
] = VARIABLE_FIXED
;
808 dar
.subtype
[j
] = SUB_INTEGER
;
810 dar
.max_variable
[j
] = 0;
811 dar
.max
[j
] = type
->u
.array
.dim
;
812 dar
.min_variable
[j
] = 0;
813 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
815 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
816 sysroff_swap_dar_out (file
, &dar
);
820 case coff_enumdef_type
:
824 struct coff_symbol
*member
;
826 dbt
.btype
= BTYPE_ENUM
;
827 dbt
.bitsize
= type
->size
;
828 dbt
.sign
= SIGN_UNSPEC
;
829 dbt
.fptype
= FPTYPE_NOTSPEC
;
830 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
832 sysroff_swap_dbt_out (file
, &dbt
);
837 sysroff_swap_den_out (file
, &den
);
839 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
841 member
= member
->next
)
842 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
845 sysroff_swap_den_out (file
, &den
);
849 case coff_enumref_type
:
853 dbt
.btype
= BTYPE_TAG
;
854 dbt
.bitsize
= type
->size
;
855 dbt
.sign
= SIGN_UNSPEC
;
856 dbt
.fptype
= FPTYPE_NOTSPEC
;
857 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
859 sysroff_swap_dbt_out (file
, &dbt
);
876 sysroff_swap_dty_out (file, &dty);
886 sysroff_swap_dty_out (file, &dty);
891 dump_tree_structure (sfile, symbol, type, nest)
892 struct coff_sfile *sfile;
893 struct coff_symbol *symbol;
894 struct coff_type *type;
897 if (symbol->type->type == coff_function_type)
907 walk_tree_type (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
908 struct coff_type
*type
, int nest
)
910 if (symbol
->type
->type
== coff_function_type
)
917 sysroff_swap_dty_out (file
, &dty
);
918 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
920 sysroff_swap_dty_out (file
, &dty
);
923 symbol
->where
->section
,
924 symbol
->type
->u
.function
.code
,
925 BLOCK_TYPE_FUNCTION
, nest
);
926 wr_dps_start (sfile
, symbol
->where
->section
,
927 symbol
->type
->u
.function
.code
,
928 BLOCK_TYPE_BLOCK
, nest
);
929 walk_tree_scope (symbol
->where
->section
,
931 symbol
->type
->u
.function
.code
,
932 nest
+ 1, BLOCK_TYPE_BLOCK
);
934 wr_dps_end (symbol
->where
->section
,
935 symbol
->type
->u
.function
.code
,
937 wr_dps_end (symbol
->where
->section
,
938 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
946 sysroff_swap_dty_out (file
, &dty
);
947 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
949 sysroff_swap_dty_out (file
, &dty
);
954 walk_tree_symbol (struct coff_sfile
*sfile
, struct coff_section
*section ATTRIBUTE_UNUSED
, struct coff_symbol
*symbol
, int nest
)
958 memset (&dsy
, 0, sizeof(dsy
));
961 switch (symbol
->type
->type
)
963 case coff_function_type
:
964 dsy
.type
= STYPE_FUNC
;
968 case coff_structref_type
:
969 case coff_pointer_type
:
970 case coff_array_type
:
971 case coff_basic_type
:
972 case coff_enumref_type
:
973 dsy
.type
= STYPE_VAR
;
977 case coff_enumdef_type
:
978 dsy
.type
= STYPE_TAG
;
983 case coff_structdef_type
:
984 dsy
.type
= STYPE_TAG
;
986 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
989 case coff_secdef_type
:
996 if (symbol
->where
->where
== coff_where_member_of_struct
)
999 dsy
.type
= STYPE_MEMBER
;
1002 if (symbol
->where
->where
== coff_where_member_of_enum
)
1004 dsy
.type
= STYPE_ENUM
;
1007 dsy
.evalue
= symbol
->where
->offset
;
1010 if (symbol
->type
->type
== coff_structdef_type
1011 || symbol
->where
->where
== coff_where_entag
1012 || symbol
->where
->where
== coff_where_strtag
)
1014 dsy
.snumber
= get_member_id (symbol
->number
);
1018 dsy
.snumber
= get_ordinary_id (symbol
->number
);
1021 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
1023 switch (symbol
->visible
->type
)
1025 case coff_vis_common
:
1026 case coff_vis_ext_def
:
1027 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
1030 case coff_vis_ext_ref
:
1031 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
1034 case coff_vis_int_def
:
1035 dsy
.ainfo
= AINFO_STATIC_INT
;
1039 case coff_vis_autoparam
:
1040 dsy
.ainfo
= AINFO_AUTO
;
1043 case coff_vis_register
:
1044 case coff_vis_regparam
:
1045 dsy
.ainfo
= AINFO_REG
;
1050 case coff_vis_member_of_struct
:
1051 case coff_vis_member_of_enum
:
1058 dsy
.dlength
= symbol
->type
->size
;
1060 switch (symbol
->where
->where
)
1062 case coff_where_memory
:
1064 dsy
.section
= symbol
->where
->section
->number
;
1070 case coff_where_member_of_struct
:
1071 case coff_where_member_of_enum
:
1072 case coff_where_stack
:
1073 case coff_where_register
:
1074 case coff_where_unknown
:
1075 case coff_where_strtag
:
1076 case coff_where_entag
:
1077 case coff_where_typedef
:
1084 switch (symbol
->where
->where
)
1086 case coff_where_memory
:
1087 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1090 case coff_where_stack
:
1091 dsy
.address
= symbol
->where
->offset
;
1094 case coff_where_member_of_struct
:
1095 if (symbol
->where
->bitsize
)
1097 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1099 dsy
.field_len
= symbol
->where
->bitsize
;
1100 dsy
.field_off
= (bits
/ 32) * 4;
1101 dsy
.field_bitoff
= bits
% 32;
1107 dsy
.field_len
= symbol
->type
->size
;
1108 dsy
.field_off
= symbol
->where
->offset
;
1112 case coff_where_member_of_enum
:
1114 dsy.field_len = symbol->type->size;
1115 dsy.field_off = symbol->where->offset; */
1118 case coff_where_register
:
1119 case coff_where_unknown
:
1120 case coff_where_strtag
:
1121 case coff_where_entag
:
1122 case coff_where_typedef
:
1129 if (symbol
->where
->where
== coff_where_register
)
1130 dsy
.reg
= rnames
[symbol
->where
->offset
];
1132 switch (symbol
->visible
->type
)
1134 case coff_vis_common
:
1135 /* We do this 'cause common C symbols are treated as extdefs. */
1136 case coff_vis_ext_def
:
1137 case coff_vis_ext_ref
:
1138 dsy
.ename
= symbol
->name
;
1141 case coff_vis_regparam
:
1142 case coff_vis_autoparam
:
1143 dsy
.type
= STYPE_PARAMETER
;
1146 case coff_vis_int_def
:
1148 case coff_vis_register
:
1150 case coff_vis_member_of_struct
:
1151 case coff_vis_member_of_enum
:
1162 sysroff_swap_dsy_out (file
, &dsy
);
1164 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1168 walk_tree_scope (struct coff_section
*section
, struct coff_sfile
*sfile
, struct coff_scope
*scope
, int nest
, int type
)
1170 struct coff_symbol
*vars
;
1171 struct coff_scope
*child
;
1173 if (scope
->vars_head
1174 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1176 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1179 wr_globals (tree
, sfile
, nest
+ 1);
1181 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1182 walk_tree_symbol (sfile
, section
, vars
, nest
);
1184 for (child
= scope
->list_head
; child
; child
= child
->next
)
1185 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1187 wr_dps_end (section
, scope
, type
);
1192 walk_tree_sfile (struct coff_section
*section
, struct coff_sfile
*sfile
)
1194 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1198 wr_program_structure (struct coff_ofile
*p
, struct coff_sfile
*sfile
)
1200 walk_tree_sfile (p
->sections
+ 4, sfile
);
1204 wr_du (struct coff_ofile
*p
, struct coff_sfile
*sfile
, int n
)
1210 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1211 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1213 du
.format
= bfd_get_file_flags (abfd
) & EXEC_P
? 0 : 1;
1218 du
.sections
= p
->nsections
- 1;
1219 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1220 du
.address
= nints (du
.sections
);
1221 du
.length
= nints (du
.sections
);
1223 for (i
= 0; i
< du
.sections
; i
++)
1230 for (j
= 0; j
< lim
; j
++)
1237 if (sfile
->section
[src
].init
)
1240 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1242 = sfile
->section
[src
].low
;
1247 du
.address
[dst
] = 0;
1252 if (sfile
->section
[src
].parent
)
1254 printf (" section %6s 0x%08x..0x%08x\n",
1255 sfile
->section
[src
].parent
->name
,
1257 du
.address
[dst
] + du
.length
[dst
] - 1);
1261 du
.sections
= dst
+ 1;
1267 sysroff_swap_du_out (file
, &du
);
1271 wr_dus (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
)
1276 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1277 dus
.drb
= nints (dus
.ns
);
1278 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1279 dus
.spare
= nints (dus
.ns
);
1281 /* Find the filenames. */
1283 dus
.fname
[0] = sfile
->name
;
1285 sysroff_swap_dus_out (file
, &dus
);
1289 /* Find the offset of the .text section for this sfile in the
1290 .text section for the output file. */
1293 find_base (struct coff_sfile
*sfile
, struct coff_section
*section
)
1295 return sfile
->section
[section
->number
].low
;
1299 wr_dln (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
,
1300 int n ATTRIBUTE_UNUSED
)
1302 /* Count up all the linenumbers */
1304 struct coff_symbol
*sy
;
1310 for (sy
= sfile
->scope
->vars_head
;
1314 struct coff_type
*t
= sy
->type
;
1315 if (t
->type
== coff_function_type
)
1317 struct coff_line
*l
= t
->u
.function
.lines
;
1323 dln
.sfn
= nints (lc
);
1324 dln
.sln
= nints (lc
);
1325 dln
.cc
= nints (lc
);
1326 dln
.section
= nints (lc
);
1328 dln
.from_address
= nints (lc
);
1329 dln
.to_address
= nints (lc
);
1336 /* Run through once more and fill up the structure */
1338 for (sy
= sfile
->scope
->vars_head
;
1342 if (sy
->type
->type
== coff_function_type
)
1345 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1348 int base
= find_base (sfile
, sy
->where
->section
);
1349 for (i
= 0; i
< l
->nlines
; i
++)
1351 dln
.section
[idx
] = sy
->where
->section
->number
;
1353 dln
.sln
[idx
] = l
->lines
[i
];
1354 dln
.from_address
[idx
] =
1355 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1358 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1362 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1367 sysroff_swap_dln_out (file
, &dln
);
1370 /* Write the global symbols out to the debug info. */
1373 wr_globals (struct coff_ofile
*p
, struct coff_sfile
*sfile
,
1374 int n ATTRIBUTE_UNUSED
)
1376 struct coff_symbol
*sy
;
1378 for (sy
= p
->symbol_list_head
;
1380 sy
= sy
->next_in_ofile_list
)
1382 if (sy
->visible
->type
== coff_vis_ext_def
1383 || sy
->visible
->type
== coff_vis_ext_ref
)
1385 /* Only write out symbols if they belong to
1386 the current source file. */
1387 if (sy
->sfile
== sfile
)
1388 walk_tree_symbol (sfile
, 0, sy
, 0);
1394 wr_debug (struct coff_ofile
*p
)
1396 struct coff_sfile
*sfile
;
1399 for (sfile
= p
->source_head
;
1401 sfile
= sfile
->next
)
1404 printf ("%s\n", sfile
->name
);
1406 wr_du (p
, sfile
, n
);
1408 wr_program_structure (p
, sfile
);
1409 wr_dln (p
, sfile
, n
);
1417 /* It seems that the CS struct is not normal - the size is wrong
1418 heres one I prepared earlier. */
1423 0x00, /* number of chars in variable length part */
1455 fwrite (b
, 1, sizeof (b
), file
);
1458 /* Write out the SC records for a unit. Create an SC
1459 for all the sections which appear in the output file, even
1460 if there isn't an equivalent one on the input. */
1463 wr_sc (struct coff_ofile
*ptr
, struct coff_sfile
*sfile
)
1467 /* First work out the total number of sections. */
1468 int total_sec
= ptr
->nsections
;
1471 struct coff_section
*sec
;
1472 struct coff_symbol
*symbol
;
1474 struct coff_symbol
*symbol
;
1476 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1479 for (i
= 0; i
< total_sec
; i
++)
1481 info
[i
].sec
= ptr
->sections
+ i
;
1485 for (symbol
= sfile
->scope
->vars_head
;
1487 symbol
= symbol
->next
)
1490 if (symbol
->type
->type
== coff_secdef_type
)
1492 for (i
= 0; i
< total_sec
; i
++)
1494 if (symbol
->where
->section
== info
[i
].sec
)
1496 info
[i
].symbol
= symbol
;
1503 /* Now output all the section info, and fake up some stuff for sections
1505 for (i
= 1; i
< total_sec
; i
++)
1510 symbol
= info
[i
].symbol
;
1516 /* Don't have a symbol set aside for this section, which means
1517 that nothing in this file does anything for the section. */
1518 sc
.format
= !(bfd_get_file_flags (abfd
) & EXEC_P
);
1521 name
= info
[i
].sec
->name
;
1525 if (bfd_get_file_flags (abfd
) & EXEC_P
)
1528 sc
.addr
= symbol
->where
->offset
;
1535 sc
.length
= symbol
->type
->size
;
1536 name
= symbol
->name
;
1540 sc
.concat
= CONCAT_SIMPLE
;
1548 sc
.spare1
= 0; /* If not zero, then it doesn't work. */
1549 sc
.name
= section_translate (name
);
1551 if (strlen (sc
.name
) == 1)
1557 sc
.contents
= CONTENTS_DATA
;
1561 sc
.contents
= CONTENTS_CODE
;
1566 sc
.contents
= CONTENTS_CODE
;
1569 sysroff_swap_sc_out (file
, &sc
);
1575 /* Write out the ER records for a unit. */
1578 wr_er (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1582 struct coff_symbol
*sym
;
1586 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1588 if (sym
->visible
->type
== coff_vis_ext_ref
)
1593 er
.type
= ER_NOTSPEC
;
1594 er
.name
= sym
->name
;
1595 sysroff_swap_er_out (file
, &er
);
1596 sym
->er_number
= idx
++;
1602 /* Write out the ED records for a unit. */
1605 wr_ed (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1608 struct coff_symbol
*s
;
1612 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1614 if (s
->visible
->type
== coff_vis_ext_def
1615 || s
->visible
->type
== coff_vis_common
)
1619 ed
.section
= s
->where
->section
->number
;
1622 if (s
->where
->section
->data
)
1624 ed
.type
= ED_TYPE_DATA
;
1626 else if (s
->where
->section
->code
& SEC_CODE
)
1628 ed
.type
= ED_TYPE_ENTRY
;
1632 ed
.type
= ED_TYPE_NOTSPEC
;
1633 ed
.type
= ED_TYPE_DATA
;
1636 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1638 sysroff_swap_ed_out (file
, &ed
);
1645 wr_unit_info (struct coff_ofile
*ptr
)
1647 struct coff_sfile
*sfile
;
1650 for (sfile
= ptr
->source_head
;
1652 sfile
= sfile
->next
)
1659 wr_un (ptr
, sfile
, first
, 0);
1660 nsecs
= wr_sc (ptr
, sfile
);
1662 fseek (file
, p1
, SEEK_SET
);
1663 wr_un (ptr
, sfile
, first
, nsecs
);
1664 fseek (file
, p2
, SEEK_SET
);
1665 wr_er (ptr
, sfile
, first
);
1666 wr_ed (ptr
, sfile
, first
);
1672 wr_module (struct coff_ofile
*p
)
1685 return (x
+ 3) & ~3;
1688 /* Find all the common variables and turn them into
1689 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1692 prescan (struct coff_ofile
*tree
)
1694 struct coff_symbol
*s
;
1695 struct coff_section
*common_section
;
1697 /* Find the common section - always section 3. */
1698 common_section
= tree
->sections
+ 3;
1700 for (s
= tree
->symbol_list_head
;
1702 s
= s
->next_in_ofile_list
)
1704 if (s
->visible
->type
== coff_vis_common
)
1706 struct coff_where
*w
= s
->where
;
1707 /* s->visible->type = coff_vis_ext_def; leave it as common */
1708 common_section
->size
= align (common_section
->size
);
1709 w
->offset
= common_section
->size
+ common_section
->address
;
1710 w
->section
= common_section
;
1711 common_section
->size
+= s
->type
->size
;
1712 common_section
->size
= align (common_section
->size
);
1720 show_usage (FILE *file
, int status
)
1722 fprintf (file
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
1723 fprintf (file
, _("Convert a COFF object file into a SYSROFF object file\n"));
1724 fprintf (file
, _(" The options are:\n\
1725 -q --quick (Obsolete - ignored)\n\
1726 -n --noprescan Do not perform a scan to convert commons into defs\n\
1727 -d --debug Display information about what is being done\n\
1728 @<file> Read options from <file>\n\
1729 -h --help Display this information\n\
1730 -v --version Print the program's version number\n"));
1732 if (REPORT_BUGS_TO
[0] && status
== 0)
1733 fprintf (file
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
1738 main (int ac
, char **av
)
1741 static struct option long_options
[] =
1743 {"debug", no_argument
, 0, 'd'},
1744 {"quick", no_argument
, 0, 'q'},
1745 {"noprescan", no_argument
, 0, 'n'},
1746 {"help", no_argument
, 0, 'h'},
1747 {"version", no_argument
, 0, 'V'},
1748 {NULL
, no_argument
, 0, 0}
1754 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1755 setlocale (LC_MESSAGES
, "");
1757 #if defined (HAVE_SETLOCALE)
1758 setlocale (LC_CTYPE
, "");
1760 bindtextdomain (PACKAGE
, LOCALEDIR
);
1761 textdomain (PACKAGE
);
1763 program_name
= av
[0];
1764 xmalloc_set_program_name (program_name
);
1766 expandargv (&ac
, &av
);
1768 while ((opt
= getopt_long (ac
, av
, "dHhVvqn", long_options
,
1785 show_usage (stdout
, 0);
1789 print_version ("srconv");
1795 show_usage (stderr
, 1);
1800 /* The input and output files may be named on the command line. */
1804 input_file
= av
[optind
];
1808 output_file
= av
[optind
];
1811 show_usage (stderr
, 1);
1812 if (strcmp (input_file
, output_file
) == 0)
1814 fatal (_("input and output files must be different"));
1823 fatal (_("no input file specified"));
1828 /* Take a .o off the input file and stick on a .obj. If
1829 it doesn't end in .o, then stick a .obj on anyway */
1831 int len
= strlen (input_file
);
1833 output_file
= xmalloc (len
+ 5);
1834 strcpy (output_file
, input_file
);
1837 && output_file
[len
- 2] == '.'
1838 && output_file
[len
- 1] == 'o')
1840 output_file
[len
] = 'b';
1841 output_file
[len
+ 1] = 'j';
1842 output_file
[len
+ 2] = 0;
1846 strcat (output_file
, ".obj");
1850 abfd
= bfd_openr (input_file
, 0);
1853 bfd_fatal (input_file
);
1855 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1857 bfd_nonfatal (input_file
);
1859 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1861 list_matching_formats (matching
);
1867 file
= fopen (output_file
, FOPEN_WB
);
1870 fatal (_("unable to open output file %s"), output_file
);
1873 printf ("ids %d %d\n", base1
, base2
);
1875 tree
= coff_grok (abfd
);