1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000
3 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 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
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 */
33 #include <libiberty.h>
36 #include "coff/internal.h"
37 #include "../bfd/libcoff.h"
39 #define PROGRAM_VERSION "1.5"
43 static char *toolname
;
47 static void walk_tree_scope ();
48 static void wr_globals ();
49 static int find_base ();
55 static int noprescan
= 0;
56 static struct coff_ofile
*tree
;
58 static int absolute_p;
61 static int segmented_p
;
64 static int ids1
[20000];
65 static int ids2
[20000];
67 static int base1
= 0x18;
68 static int base2
= 0x2018;
97 if (strcmp (n
, ".text") == 0)
99 if (strcmp (n
, ".data") == 0)
101 if (strcmp (n
, ".bss") == 0)
108 #define DATE "940201073000"; /* Just a time on my birthday */
118 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
120 res
= (char *) xmalloc (i
+ 1);
121 memcpy (res
, name
, i
);
127 /* IT LEN stuff CS */
129 checksum (file
, ptr
, size
, code
)
138 int bytes
= size
/ 8;
139 last
= !(code
& 0xff00);
142 ptr
[0] = code
| (last
? 0x80 : 0);
145 for (j
= 0; j
< bytes
; j
++)
149 /* Glue on a checksum too */
151 fwrite (ptr
, bytes
+ 1, 1, file
);
158 writeINT (n
, ptr
, idx
, size
, file
)
174 /* Lets write out that record and do another one */
175 checksum (file
, ptr
, *idx
, code
| 0x1000);
187 ptr
[byte
+ 0] = n
>> 8;
191 ptr
[byte
+ 0] = n
>> 24;
192 ptr
[byte
+ 1] = n
>> 16;
193 ptr
[byte
+ 2] = n
>> 8;
194 ptr
[byte
+ 3] = n
>> 0;
204 writeBITS (val
, ptr
, idx
, size
)
216 /* Turn off all about to change bits */
217 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
218 /* Turn on the bits we want */
219 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
224 writeBARRAY (data
, ptr
, idx
, size
, file
)
228 int size ATTRIBUTE_UNUSED
;
232 writeINT (data
.len
, ptr
, idx
, 1, file
);
233 for (i
= 0; i
< data
.len
; i
++)
235 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
241 writeCHARS (string
, ptr
, idx
, size
, file
)
252 /* Lets write out that record and do another one */
253 checksum (file
, ptr
, *idx
, code
| 0x1000);
260 /* Variable length string */
261 size
= strlen (string
);
265 /* BUG WAITING TO HAPPEN */
266 memcpy (ptr
+ i
, string
, size
);
271 #define SYSROFF_SWAP_OUT
275 static char *rname_sh
[] =
277 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
280 static char *rname_h8300
[] =
282 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
288 /* The TR block is not normal - it doesn't have any contents. */
295 fwrite (b
, 1, sizeof (b
), file
);
299 wr_un (ptr
, sfile
, first
, nsecs
)
300 struct coff_ofile
*ptr
;
301 struct coff_sfile
*sfile
;
303 int nsecs ATTRIBUTE_UNUSED
;
307 struct coff_symbol
*s
;
311 if (bfd_get_file_flags (abfd
) & EXEC_P
)
312 un
.format
= FORMAT_LM
;
314 un
.format
= FORMAT_OM
;
319 un
.nsections
= ptr
->nsections
- 1; /* Don't count the abs section */
321 /*NEW - only count sections with size */
322 un
.nsections
= nsecs
;
327 /* Count all the undefined and defined variables with global scope */
331 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
333 if (s
->visible
->type
== coff_vis_ext_def
334 || s
->visible
->type
== coff_vis_common
)
337 if (s
->visible
->type
== coff_vis_ext_ref
)
343 un
.linker
= "L_GX00";
345 un
.name
= sfile
->name
;
346 sysroff_swap_un_out (file
, &un
);
352 struct coff_ofile
*p
;
357 if (bfd_get_file_flags (abfd
) & EXEC_P
)
359 hd
.mt
= MTYPE_ABS_LM
;
363 hd
.mt
= MTYPE_OMS_OR_LMS
;
367 hd
.nu
= p
->nsources
; /* Always one unit */
368 hd
.code
= 0; /* Always ASCII */
369 hd
.ver
= "0200"; /* Version 2.00 */
370 switch (bfd_get_arch (abfd
))
378 switch (bfd_get_mach (abfd
))
384 toolname
= "C_H8/300";
386 case bfd_mach_h8300h
:
390 toolname
= "C_H8/300H";
392 case bfd_mach_h8300s
:
396 toolname
= "C_H8/300S";
401 rnames
= rname_h8300
;
419 if (! bfd_get_file_flags(abfd
) & EXEC_P
)
429 hd
.address
= bfd_get_start_address (abfd
);
434 hd
.mn
= strip_suffix (bfd_get_filename (abfd
));
436 sysroff_swap_hd_out (file
, &hd
);
442 struct coff_ofile
*p ATTRIBUTE_UNUSED
;
443 struct coff_section
*sec
;
447 sh
.section
= sec
->number
;
451 sysroff_swap_sh_out (file
, &sh
);
457 struct coff_ofile
*p ATTRIBUTE_UNUSED
;
458 struct coff_section
*section
;
462 unsigned char stuff
[200];
465 while (i
< section
->bfd_section
->_raw_size
)
468 int todo
= 200; /* Copy in 200 byte lumps */
470 if (i
+ todo
> section
->bfd_section
->_raw_size
)
471 todo
= section
->bfd_section
->_raw_size
- i
;
476 if (bfd_get_file_flags (abfd
) & EXEC_P
)
477 ob
.address
= section
->address
;
488 ob
.cpf
= 0; /* Never compress */
490 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
491 ob
.data
.data
= stuff
;
492 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
495 /* Now fill the rest with blanks */
496 while (i
< (bfd_size_type
) section
->size
)
499 int todo
= 200; /* Copy in 200 byte lumps */
501 if (i
+ todo
> (bfd_size_type
) section
->size
)
502 todo
= section
->size
- i
;
505 ob
.cpf
= 0; /* Never compress */
507 memset (stuff
, 0, todo
);
508 ob
.data
.data
= stuff
;
509 sysroff_swap_ob_out (file
, &ob
);
512 /* Now fill the rest with blanks */
518 struct coff_ofile
*ptr ATTRIBUTE_UNUSED
;
519 struct coff_section
*sec
;
521 int nr
= sec
->nrelocs
;
523 for (i
= 0; i
< nr
; i
++)
525 struct coff_reloc
*r
= sec
->relocs
+ i
;
526 struct coff_symbol
*ref
;
535 rl
.flen
= 32; /* SH Specific */
536 /* What sort of reloc ? Look in the section to find out */
538 if (ref
->visible
->type
== coff_vis_ext_ref
)
540 rl
.bcount
= 4; /* Always 4 for us */
542 rl
.symn
= ref
->er_number
;
544 else if (ref
->visible
->type
== coff_vis_common
)
546 rl
.bcount
= 11; /* Always 11 for us */
548 rl
.secn
= ref
->where
->section
->number
;
551 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
552 rl
.aopcode_is_0x20
= 0x20;
557 rl
.bcount
= 11; /* Always 11 for us */
559 rl
.secn
= ref
->where
->section
->number
;
562 rl
.addend
= -ref
->where
->section
->address
;
563 rl
.aopcode_is_0x20
= 0x20;
566 if (rl
.op
== OP_SEC_REF
567 || rl
.op
== OP_EXT_REF
)
569 sysroff_swap_rl_out (file
, &rl
);
576 struct coff_ofile
*p
;
579 for (i
= 1; i
< p
->nsections
; i
++)
581 wr_sh (p
, p
->sections
+ i
);
582 wr_ob (p
, p
->sections
+ i
);
583 wr_rl (p
, p
->sections
+ i
);
588 wr_dps_start (sfile
, section
, scope
, type
, nest
)
589 struct coff_sfile
*sfile
;
590 struct coff_section
*section ATTRIBUTE_UNUSED
;
591 struct coff_scope
*scope
;
601 dps
.san
= scope
->sec
->number
;
602 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
603 dps
.block_size
= scope
->size
;
606 printf ("DPS %s %d %x\n",
622 sysroff_swap_dps_out (file
, &dps
);
626 wr_dps_end (section
, scope
, type
)
627 struct coff_section
*section ATTRIBUTE_UNUSED
;
628 struct coff_scope
*scope ATTRIBUTE_UNUSED
;
634 sysroff_swap_dps_out (file
, &dps
);
641 return (int *) (xcalloc (sizeof (int), x
));
644 static void walk_tree_symbol ();
646 walk_tree_type_1 (sfile
, symbol
, type
, nest
)
647 struct coff_sfile
*sfile
;
648 struct coff_symbol
*symbol
;
649 struct coff_type
*type
;
654 case coff_secdef_type
:
655 case coff_basic_type
:
659 switch (type
->u
.basic
)
663 dbt
.btype
= BTYPE_VOID
;
664 dbt
.sign
= BTYPE_UNSPEC
;
665 dbt
.fptype
= FPTYPE_NOTSPEC
;
668 dbt
.btype
= BTYPE_CHAR
;
669 dbt
.sign
= BTYPE_UNSPEC
;
670 dbt
.fptype
= FPTYPE_NOTSPEC
;
675 dbt
.btype
= BTYPE_INT
;
676 dbt
.sign
= SIGN_SIGNED
;
677 dbt
.fptype
= FPTYPE_NOTSPEC
;
680 dbt
.btype
= BTYPE_FLOAT
;
681 dbt
.fptype
= FPTYPE_SINGLE
;
684 dbt
.btype
= BTYPE_FLOAT
;
685 dbt
.fptype
= FPTYPE_DOUBLE
;
688 dbt
.btype
= BTYPE_FLOAT
;
689 dbt
.fptype
= FPTYPE_EXTENDED
;
692 dbt
.btype
= BTYPE_CHAR
;
693 dbt
.sign
= SIGN_UNSIGNED
;
694 dbt
.fptype
= FPTYPE_NOTSPEC
;
699 dbt
.btype
= BTYPE_INT
;
700 dbt
.sign
= SIGN_UNSIGNED
;
701 dbt
.fptype
= FPTYPE_NOTSPEC
;
704 dbt
.bitsize
= type
->size
;
706 sysroff_swap_dbt_out (file
, &dbt
);
709 case coff_pointer_type
:
712 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
714 sysroff_swap_dpt_out (file
, &dpt
);
718 case coff_function_type
:
721 struct coff_symbol
*param
;
724 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
727 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
729 sysroff_swap_dfp_out (file
, &dfp
);
731 for (param
= type
->u
.function
.parameters
->vars_head
;
735 walk_tree_symbol (sfile
, 0, param
, nest
);
738 sysroff_swap_dfp_out (file
, &dfp
);
742 case coff_structdef_type
:
746 struct coff_symbol
*member
;
748 dbt
.btype
= BTYPE_STRUCT
;
749 dbt
.bitsize
= type
->size
;
750 dbt
.sign
= SIGN_UNSPEC
;
751 dbt
.fptype
= FPTYPE_NOTSPEC
;
752 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
754 sysroff_swap_dbt_out (file
, &dbt
);
757 sysroff_swap_dds_out (file
, &dds
);
758 for (member
= type
->u
.astructdef
.elements
->vars_head
;
760 member
= member
->next
)
762 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
766 sysroff_swap_dds_out (file
, &dds
);
770 case coff_structref_type
:
773 dbt
.btype
= BTYPE_TAG
;
774 dbt
.bitsize
= type
->size
;
775 dbt
.sign
= SIGN_UNSPEC
;
776 dbt
.fptype
= FPTYPE_NOTSPEC
;
777 if (type
->u
.astructref
.ref
)
779 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
787 sysroff_swap_dbt_out (file
, &dbt
);
790 case coff_array_type
:
794 int dims
= 1; /* Only output one dimension at a time */
796 dar
.variable
= nints (dims
);
797 dar
.subtype
= nints (dims
);
798 dar
.spare
= nints (dims
);
799 dar
.max_variable
= nints (dims
);
800 dar
.maxspare
= nints (dims
);
801 dar
.max
= nints (dims
);
802 dar
.min_variable
= nints (dims
);
803 dar
.min
= nints (dims
);
804 dar
.minspare
= nints (dims
);
806 dar
.length
= type
->size
/ type
->u
.array
.dim
;
807 for (j
= 0; j
< dims
; j
++)
809 dar
.variable
[j
] = VARIABLE_FIXED
;
810 dar
.subtype
[j
] = SUB_INTEGER
;
812 dar
.max_variable
[j
] = 0;
813 dar
.max
[j
] = type
->u
.array
.dim
;
814 dar
.min_variable
[j
] = 0;
815 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
817 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
818 sysroff_swap_dar_out (file
, &dar
);
821 case coff_enumdef_type
:
825 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
);
838 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
840 member
= member
->next
)
842 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
846 sysroff_swap_den_out (file
, &den
);
851 case coff_enumref_type
:
854 dbt
.btype
= BTYPE_TAG
;
855 dbt
.bitsize
= type
->size
;
856 dbt
.sign
= SIGN_UNSPEC
;
857 dbt
.fptype
= FPTYPE_NOTSPEC
;
858 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
860 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 (sfile
, symbol
, type
, nest
)
911 struct coff_symbol
*symbol
;
912 struct coff_type
*type
;
915 if (symbol
->type
->type
== coff_function_type
)
922 sysroff_swap_dty_out (file
, &dty
);
923 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
925 sysroff_swap_dty_out (file
, &dty
);
928 symbol
->where
->section
,
929 symbol
->type
->u
.function
.code
,
930 BLOCK_TYPE_FUNCTION
, nest
);
931 wr_dps_start (sfile
, symbol
->where
->section
,
932 symbol
->type
->u
.function
.code
,
933 BLOCK_TYPE_BLOCK
, nest
);
934 walk_tree_scope (symbol
->where
->section
,
936 symbol
->type
->u
.function
.code
,
937 nest
+ 1, BLOCK_TYPE_BLOCK
);
939 wr_dps_end (symbol
->where
->section
,
940 symbol
->type
->u
.function
.code
,
942 wr_dps_end (symbol
->where
->section
,
943 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
951 sysroff_swap_dty_out (file
, &dty
);
952 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
954 sysroff_swap_dty_out (file
, &dty
);
962 walk_tree_symbol (sfile
, section
, symbol
, nest
)
963 struct coff_sfile
*sfile
;
964 struct coff_section
*section ATTRIBUTE_UNUSED
;
965 struct coff_symbol
*symbol
;
970 memset(&dsy
, 0, sizeof(dsy
));
973 switch (symbol
->type
->type
)
975 case coff_function_type
:
976 dsy
.type
= STYPE_FUNC
;
979 case coff_structref_type
:
980 case coff_pointer_type
:
981 case coff_array_type
:
982 case coff_basic_type
:
983 case coff_enumref_type
:
984 dsy
.type
= STYPE_VAR
;
987 case coff_enumdef_type
:
988 dsy
.type
= STYPE_TAG
;
992 case coff_structdef_type
:
993 dsy
.type
= STYPE_TAG
;
995 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
997 case coff_secdef_type
:
1003 if (symbol
->where
->where
== coff_where_member_of_struct
)
1006 dsy
.type
= STYPE_MEMBER
;
1008 if (symbol
->where
->where
== coff_where_member_of_enum
)
1010 dsy
.type
= STYPE_ENUM
;
1013 dsy
.evalue
= symbol
->where
->offset
;
1016 if (symbol
->type
->type
== coff_structdef_type
1017 || symbol
->where
->where
== coff_where_entag
1018 || symbol
->where
->where
== coff_where_strtag
)
1020 dsy
.snumber
= get_member_id (symbol
->number
);
1024 dsy
.snumber
= get_ordinary_id (symbol
->number
);
1028 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
1030 switch (symbol
->visible
->type
)
1032 case coff_vis_common
:
1033 case coff_vis_ext_def
:
1034 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
1036 case coff_vis_ext_ref
:
1037 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
1039 case coff_vis_int_def
:
1040 dsy
.ainfo
= AINFO_STATIC_INT
;
1043 case coff_vis_autoparam
:
1044 dsy
.ainfo
= AINFO_AUTO
;
1046 case coff_vis_register
:
1047 case coff_vis_regparam
:
1048 dsy
.ainfo
= AINFO_REG
;
1052 case coff_vis_member_of_struct
:
1053 case coff_vis_member_of_enum
:
1059 dsy
.dlength
= symbol
->type
->size
;
1060 switch (symbol
->where
->where
)
1062 case coff_where_memory
:
1064 dsy
.section
= symbol
->where
->section
->number
;
1069 case coff_where_member_of_struct
:
1070 case coff_where_member_of_enum
:
1071 case coff_where_stack
:
1072 case coff_where_register
:
1073 case coff_where_unknown
:
1074 case coff_where_strtag
:
1076 case coff_where_entag
:
1077 case coff_where_typedef
:
1083 switch (symbol
->where
->where
)
1085 case coff_where_memory
:
1086 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1088 case coff_where_stack
:
1089 dsy
.address
= symbol
->where
->offset
;
1091 case coff_where_member_of_struct
:
1094 if (symbol
->where
->bitsize
)
1096 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1098 dsy
.field_len
= symbol
->where
->bitsize
;
1099 dsy
.field_off
= (bits
/ 32) * 4;
1100 dsy
.field_bitoff
= bits
% 32;
1106 dsy
.field_len
= symbol
->type
->size
;
1107 dsy
.field_off
= symbol
->where
->offset
;
1110 case coff_where_member_of_enum
:
1112 dsy.field_len = symbol->type->size;
1113 dsy.field_off = symbol->where->offset; */
1115 case coff_where_register
:
1116 case coff_where_unknown
:
1117 case coff_where_strtag
:
1119 case coff_where_entag
:
1120 case coff_where_typedef
:
1126 if (symbol
->where
->where
== coff_where_register
)
1127 dsy
.reg
= rnames
[symbol
->where
->offset
];
1129 switch (symbol
->visible
->type
)
1131 case coff_vis_common
:
1132 /* We do this 'cause common C symbols are treated as extdefs */
1133 case coff_vis_ext_def
:
1134 case coff_vis_ext_ref
:
1136 dsy
.ename
= symbol
->name
;
1139 case coff_vis_regparam
:
1140 case coff_vis_autoparam
:
1141 dsy
.type
= STYPE_PARAMETER
;
1144 case coff_vis_int_def
:
1147 case coff_vis_register
:
1149 case coff_vis_member_of_struct
:
1150 case coff_vis_member_of_enum
:
1162 sysroff_swap_dsy_out (file
, &dsy
);
1164 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1169 walk_tree_scope (section
, sfile
, scope
, nest
, type
)
1170 struct coff_section
*section
;
1171 struct coff_sfile
*sfile
;
1172 struct coff_scope
*scope
;
1176 struct coff_symbol
*vars
;
1177 struct coff_scope
*child
;
1179 if (scope
->vars_head
1180 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1182 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1185 wr_globals (tree
, sfile
, nest
+ 1);
1187 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1189 walk_tree_symbol (sfile
, section
, vars
, nest
);
1192 for (child
= scope
->list_head
; child
; child
= child
->next
)
1194 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1197 wr_dps_end (section
, scope
, type
);
1201 walk_tree_sfile (section
, sfile
)
1202 struct coff_section
*section
;
1203 struct coff_sfile
*sfile
;
1205 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1210 wr_program_structure (p
, sfile
)
1211 struct coff_ofile
*p
;
1212 struct coff_sfile
*sfile
;
1215 walk_tree_sfile (p
->sections
+ 4, sfile
);
1221 struct coff_ofile
*p
;
1222 struct coff_sfile
*sfile
;
1228 struct coff_symbol
*symbol
;
1229 static int incit
= 0x500000;
1234 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1235 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1236 du
.format
= bfd_get_file_flags (abfd
) & EXEC_P
? 0 : 1;
1241 du
.sections
= p
->nsections
- 1;
1242 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1243 du
.address
= nints (du
.sections
);
1244 du
.length
= nints (du
.sections
);
1246 for (i
= 0; i
< du
.sections
; i
++)
1252 /* Look through all the symbols and try and work out the extents in this
1255 for (symbol
= sfile
->scope
->vars_head
;
1257 symbol
= symbol
->next
)
1259 if (symbol
->type
->type
== coff_secdef_type
)
1261 unsigned int low
= symbol
->where
->offset
;
1262 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1263 struct coff_section
*section
= symbol
->where
->section
;
1265 int sn
= section
->number
;
1266 if (low
< lowest
[sn
])
1268 if (high
> highest
[sn
])
1274 for (i
= 0; i
< du
.sections
; i
++)
1276 if (highest
[i
] == 0)
1278 lowest
[i
] = highest
[i
] = incit
;
1281 du
.length
[used
] = highest
[i
] - lowest
[i
];
1282 du
.address
[used
] = bfd_get_file_flags (abfd
) & EXEC_P
? lowest
[i
] : 0;
1285 printf (" section %6s 0x%08x..0x%08x\n",
1286 p
->sections
[i
+ 1].name
,
1295 for (j
= 0; j
< lim
; j
++)
1300 if (sfile
->section
[src
].init
)
1303 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1305 = sfile
->section
[src
].low
;
1310 du
.address
[dst
] = 0;
1314 if (sfile
->section
[src
].parent
)
1316 printf (" section %6s 0x%08x..0x%08x\n",
1317 sfile
->section
[src
].parent
->name
,
1319 du
.address
[dst
] + du
.length
[dst
] - 1);
1322 du
.sections
= dst
+ 1;
1328 sysroff_swap_du_out (file
, &du
);
1333 struct coff_ofile
*p ATTRIBUTE_UNUSED
;
1334 struct coff_sfile
*sfile
;
1340 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1341 dus
.drb
= nints (dus
.ns
);
1342 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1343 dus
.spare
= nints (dus
.ns
);
1345 /* Find the filenames */
1349 for (sfile
= p
->source_head
;
1351 sfile
= sfile
->next
)
1355 dus
.fname
[i
] = sfile
->name
;
1360 dus
.fname
[0] = sfile
->name
;
1363 sysroff_swap_dus_out (file
, &dus
);
1367 /* Find the offset of the .text section for this sfile in the
1368 .text section for the output file */
1371 find_base (sfile
, section
)
1372 struct coff_sfile
*sfile
;
1373 struct coff_section
*section
;
1375 return sfile
->section
[section
->number
].low
;
1379 wr_dln (p
, sfile
, n
)
1380 struct coff_ofile
*p ATTRIBUTE_UNUSED
;
1381 struct coff_sfile
*sfile
;
1382 int n ATTRIBUTE_UNUSED
;
1388 /* Count up all the linenumbers */
1389 struct coff_symbol
*sy
;
1395 for (sy
= p
->symbol_list_head
;
1397 sy
= sy
->next_in_ofile_list
)
1399 struct coff_type
*t
= sy
->type
;
1400 if (t
->type
== coff_function_type
)
1402 struct coff_line
*l
= t
->u
.function
.lines
;
1407 dln
.sfn
= nints (lc
);
1408 dln
.sln
= nints (lc
);
1409 dln
.lln
= nints (lc
);
1410 dln
.section
= nints (lc
);
1412 dln
.from_address
= nints (lc
);
1413 dln
.to_address
= nints (lc
);
1420 /* Run through once more and fill up the structure */
1422 for (sy
= p
->symbol_list_head
;
1424 sy
= sy
->next_in_ofile_list
)
1426 if (sy
->type
->type
== coff_function_type
)
1429 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1430 for (i
= 0; i
< l
->nlines
; i
++)
1432 dln
.section
[idx
] = sy
->where
->section
->number
;
1434 dln
.sln
[idx
] = l
->lines
[i
];
1435 dln
.from_address
[idx
] = l
->addresses
[i
];
1437 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1443 sysroff_swap_dln_out (file
, &dln
);
1448 /* Count up all the linenumbers */
1450 struct coff_symbol
*sy
;
1456 for (sy
= sfile
->scope
->vars_head
;
1460 struct coff_type
*t
= sy
->type
;
1461 if (t
->type
== coff_function_type
)
1463 struct coff_line
*l
= t
->u
.function
.lines
;
1469 dln
.sfn
= nints (lc
);
1470 dln
.sln
= nints (lc
);
1471 dln
.cc
= nints (lc
);
1472 dln
.section
= nints (lc
);
1474 dln
.from_address
= nints (lc
);
1475 dln
.to_address
= nints (lc
);
1482 /* Run through once more and fill up the structure */
1484 for (sy
= sfile
->scope
->vars_head
;
1488 if (sy
->type
->type
== coff_function_type
)
1491 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1494 int base
= find_base (sfile
, sy
->where
->section
);
1495 for (i
= 0; i
< l
->nlines
; i
++)
1497 dln
.section
[idx
] = sy
->where
->section
->number
;
1499 dln
.sln
[idx
] = l
->lines
[i
];
1500 dln
.from_address
[idx
] =
1501 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1504 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1508 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1513 sysroff_swap_dln_out (file
, &dln
);
1517 /* Write the global symbols out to the debug info */
1519 wr_globals (p
, sfile
, n
)
1520 struct coff_ofile
*p
;
1521 struct coff_sfile
*sfile
;
1522 int n ATTRIBUTE_UNUSED
;
1524 struct coff_symbol
*sy
;
1525 for (sy
= p
->symbol_list_head
;
1527 sy
= sy
->next_in_ofile_list
)
1529 if (sy
->visible
->type
== coff_vis_ext_def
1530 || sy
->visible
->type
== coff_vis_ext_ref
)
1532 /* Only write out symbols if they belong to
1533 the current source file */
1534 if (sy
->sfile
== sfile
)
1535 walk_tree_symbol (sfile
, 0, sy
, 0);
1543 struct coff_ofile
*p
;
1545 struct coff_sfile
*sfile
;
1547 for (sfile
= p
->source_head
;
1549 sfile
= sfile
->next
)
1554 printf ("%s\n", sfile
->name
);
1556 wr_du (p
, sfile
, n
);
1558 wr_program_structure (p
, sfile
);
1559 wr_dln (p
, sfile
, n
);
1567 /* It seems that the CS struct is not normal - the size is wrong
1568 heres one I prepared earlier.. */
1572 0x00, /* number of chars in variable length part */
1604 fwrite (b
, 1, sizeof (b
), file
);
1607 /* Write out the SC records for a unit. Create an SC
1608 for all the sections which appear in the output file, even
1609 if there isn't an equivalent one on the input */
1613 struct coff_ofile
*ptr
;
1614 struct coff_sfile
*sfile
;
1618 /* First work out the total number of sections */
1620 int total_sec
= ptr
->nsections
;
1624 struct coff_section
*sec
;
1625 struct coff_symbol
*symbol
;
1627 struct coff_symbol
*symbol
;
1630 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1634 for (i
= 0; i
< total_sec
; i
++)
1636 info
[i
].sec
= ptr
->sections
+ i
;
1640 for (symbol
= sfile
->scope
->vars_head
;
1642 symbol
= symbol
->next
)
1645 if (symbol
->type
->type
== coff_secdef_type
)
1647 for (i
= 0; i
< total_sec
; i
++)
1649 if (symbol
->where
->section
== info
[i
].sec
)
1651 info
[i
].symbol
= symbol
;
1658 /* Now output all the section info, and fake up some stuff for sections
1661 for (i
= 1; i
< total_sec
; i
++)
1665 symbol
= info
[i
].symbol
;
1670 /* Don't have a symbol set aside for this section, which means that nothing
1671 in this file does anything for the section. */
1672 sc
.format
= !(bfd_get_file_flags (abfd
) & EXEC_P
);
1675 name
= info
[i
].sec
->name
;
1679 if (bfd_get_file_flags (abfd
) & EXEC_P
)
1682 sc
.addr
= symbol
->where
->offset
;
1689 sc
.length
= symbol
->type
->size
;
1690 name
= symbol
->name
;
1695 sc
.concat
= CONCAT_SIMPLE
;
1703 sc
.spare1
= 0; /* If not zero, then it doesn't work */
1704 sc
.name
= section_translate (name
);
1705 if (strlen (sc
.name
) == 1)
1711 sc
.contents
= CONTENTS_DATA
;
1714 sc
.contents
= CONTENTS_CODE
;
1719 sc
.contents
= CONTENTS_CODE
;
1725 sysroff_swap_sc_out (file
, &sc
);
1735 /* Write out the ER records for a unit. */
1737 wr_er (ptr
, sfile
, first
)
1738 struct coff_ofile
*ptr
;
1739 struct coff_sfile
*sfile ATTRIBUTE_UNUSED
;
1743 struct coff_symbol
*sym
;
1746 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1748 if (sym
->visible
->type
== coff_vis_ext_ref
)
1752 er
.type
= ER_NOTSPEC
;
1753 er
.name
= sym
->name
;
1754 sysroff_swap_er_out (file
, &er
);
1755 sym
->er_number
= idx
++;
1761 /* Write out the ED records for a unit. */
1763 wr_ed (ptr
, sfile
, first
)
1764 struct coff_ofile
*ptr
;
1765 struct coff_sfile
*sfile ATTRIBUTE_UNUSED
;
1768 struct coff_symbol
*s
;
1771 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1773 if (s
->visible
->type
== coff_vis_ext_def
1774 || s
->visible
->type
== coff_vis_common
)
1778 ed
.section
= s
->where
->section
->number
;
1780 if (s
->where
->section
->data
)
1782 ed
.type
= ED_TYPE_DATA
;
1784 else if (s
->where
->section
->code
& SEC_CODE
)
1786 ed
.type
= ED_TYPE_ENTRY
;
1790 ed
.type
= ED_TYPE_NOTSPEC
;
1791 ed
.type
= ED_TYPE_DATA
;
1793 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1795 sysroff_swap_ed_out (file
, &ed
);
1803 struct coff_ofile
*ptr
;
1805 struct coff_sfile
*sfile
;
1807 for (sfile
= ptr
->source_head
;
1809 sfile
= sfile
->next
)
1815 wr_un (ptr
, sfile
, first
, 0);
1816 nsecs
= wr_sc (ptr
, sfile
);
1818 fseek (file
, p1
, SEEK_SET
);
1819 wr_un (ptr
, sfile
, first
, nsecs
);
1820 fseek (file
, p2
, SEEK_SET
);
1821 wr_er (ptr
, sfile
, first
);
1822 wr_ed (ptr
, sfile
, first
);
1829 struct coff_ofile
*p
;
1843 return (x
+ 3) & ~3;
1846 /* Find all the common variables and turn them into
1847 ordinary defs - dunno why, but thats what hitachi does with 'em */
1851 struct coff_ofile
*tree
;
1853 struct coff_symbol
*s
;
1854 struct coff_section
*common_section
;
1855 /* Find the common section - always section 3 */
1856 common_section
= tree
->sections
+ 3;
1857 for (s
= tree
->symbol_list_head
;
1859 s
= s
->next_in_ofile_list
)
1861 if (s
->visible
->type
== coff_vis_common
)
1863 struct coff_where
*w
= s
->where
;
1864 /* s->visible->type = coff_vis_ext_def; leave it as common */
1865 common_section
->size
= align (common_section
->size
);
1866 w
->offset
= common_section
->size
+ common_section
->address
;
1867 w
->section
= common_section
;
1868 common_section
->size
+= s
->type
->size
;
1869 common_section
->size
= align (common_section
->size
);
1877 show_usage (file
, status
)
1881 fprintf (file
, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name
);
1888 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1890 show_usage (stdout
, 0);
1901 static struct option long_options
[] =
1903 {"debug", no_argument
, 0, 'd'},
1904 {"quick", no_argument
, 0, 'q'},
1905 {"noprescan", no_argument
, 0, 'n'},
1906 {"help", no_argument
, 0, 'h'},
1907 {"version", no_argument
, 0, 'V'},
1908 {NULL
, no_argument
, 0, 0}
1914 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1915 setlocale (LC_MESSAGES
, "");
1917 bindtextdomain (PACKAGE
, LOCALEDIR
);
1918 textdomain (PACKAGE
);
1920 program_name
= av
[0];
1921 xmalloc_set_program_name (program_name
);
1923 while ((opt
= getopt_long (ac
, av
, "dhVqn", long_options
,
1942 printf (_("GNU %s version %s\n"), program_name
, PROGRAM_VERSION
);
1948 show_usage (stderr
, 1);
1953 /* The input and output files may be named on the command line. */
1957 input_file
= av
[optind
];
1961 output_file
= av
[optind
];
1964 show_usage (stderr
, 1);
1965 if (strcmp (input_file
, output_file
) == 0)
1967 fatal (_("input and output files must be different"));
1976 fatal (_("no input file specified"));
1981 /* Take a .o off the input file and stick on a .obj. If
1982 it doesn't end in .o, then stick a .obj on anyway */
1984 int len
= strlen (input_file
);
1985 output_file
= xmalloc (len
+ 5);
1986 strcpy (output_file
, input_file
);
1988 && output_file
[len
- 2] == '.'
1989 && output_file
[len
- 1] == 'o')
1991 output_file
[len
] = 'b';
1992 output_file
[len
+ 1] = 'j';
1993 output_file
[len
+ 2] = 0;
1997 strcat (output_file
, ".obj");
2001 abfd
= bfd_openr (input_file
, 0);
2004 bfd_fatal (input_file
);
2006 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2008 bfd_nonfatal (input_file
);
2009 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2011 list_matching_formats (matching
);
2017 file
= fopen (output_file
, FOPEN_WB
);
2021 fatal (_("unable to open output file %s"), output_file
);
2025 printf ("ids %d %d\n", base1
, base2
);
2026 tree
= coff_grok (abfd
);