1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Written by Steve Chamberlain (sac@cygnus.com)
22 This program can be used to convert a coff object file
23 into a Hitachi OM/LM (Sysroff) format.
25 All debugging information is preserved */
32 #include <libiberty.h>
35 #include "coff/internal.h"
36 #include "../bfd/libcoff.h"
38 #define PROGRAM_VERSION "1.5"
44 static void walk_tree_scope ();
45 static void wr_globals ();
46 static int find_base ();
52 static int noprescan
= 0;
53 static struct coff_ofile
*tree
;
55 static int absolute_p;
58 static int segmented_p
;
61 static int ids1
[20000];
62 static int ids2
[20000];
64 static int base1
= 0x18;
65 static int base2
= 0x2018;
72 char *r
= xmalloc (a
* b
);
101 section_translate (n
)
104 if (strcmp (n
, ".text") == 0)
106 if (strcmp (n
, ".data") == 0)
108 if (strcmp (n
, ".bss") == 0)
115 #define DATE "940201073000"; /* Just a time on my birthday */
125 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
127 res
= (char *) xmalloc (i
+ 1);
128 memcpy (res
, name
, i
);
134 /* IT LEN stuff CS */
136 checksum (file
, ptr
, size
, code
)
145 int bytes
= size
/ 8;
146 last
= !(code
& 0xff00);
149 ptr
[0] = code
| (last
? 0x80 : 0);
152 for (j
= 0; j
< bytes
; j
++)
156 /* Glue on a checksum too */
158 fwrite (ptr
, bytes
+ 1, 1, file
);
165 writeINT (n
, ptr
, idx
, size
, file
)
181 /* Lets write out that record and do another one */
182 checksum (file
, ptr
, *idx
, code
| 0x1000);
194 ptr
[byte
+ 0] = n
>> 8;
198 ptr
[byte
+ 0] = n
>> 24;
199 ptr
[byte
+ 1] = n
>> 16;
200 ptr
[byte
+ 2] = n
>> 8;
201 ptr
[byte
+ 3] = n
>> 0;
211 writeBITS (val
, ptr
, idx
, size
)
223 /* Turn off all about to change bits */
224 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
225 /* Turn on the bits we want */
226 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
231 writeBARRAY (data
, ptr
, idx
, size
, file
)
239 writeINT (data
.len
, ptr
, idx
, 1, file
);
240 for (i
= 0; i
< data
.len
; i
++)
242 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
250 writeCHARS (string
, ptr
, idx
, size
, file
)
261 /* Lets write out that record and do another one */
262 checksum (file
, ptr
, *idx
, code
| 0x1000);
269 /* Variable length string */
270 size
= strlen (string
);
275 memcpy (ptr
+ i
, string
, size
);
280 #define SYSROFF_SWAP_OUT
284 static char *rname_sh
[] =
286 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
289 static char *rname_h8300
[] =
291 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
298 sysroff_swap_tr_out (file
, &t
);
302 wr_un (ptr
, sfile
, first
, nsecs
)
303 struct coff_ofile
*ptr
;
304 struct coff_sfile
*sfile
;
310 struct coff_symbol
*s
;
314 if (abfd
->flags
& EXEC_P
)
315 un
.format
= FORMAT_LM
;
317 un
.format
= FORMAT_OM
;
322 un
.nsections
= ptr
->nsections
- 1; /* Don't count the abs section */
324 /*NEW - only count sections with size */
325 un
.nsections
= nsecs
;
330 /* Count all the undefined and defined variables with global scope */
334 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
336 if (s
->visible
->type
== coff_vis_ext_def
337 || s
->visible
->type
== coff_vis_common
)
340 if (s
->visible
->type
== coff_vis_ext_ref
)
350 un
.tool
= "C_H8/300H";
353 un
.linker
= "L_GX00";
355 un
.name
= sfile
->name
;
356 sysroff_swap_un_out (file
, &un
);
362 struct coff_ofile
*p
;
367 if (abfd
->flags
& EXEC_P
)
369 hd
.mt
= MTYPE_ABS_LM
;
373 hd
.mt
= MTYPE_OMS_OR_LMS
;
377 hd
.nu
= p
->nsources
; /* Always one unit */
378 hd
.code
= 0; /* Always ASCII */
379 hd
.ver
= "0200"; /* Version 2.00 */
380 switch (abfd
->arch_info
->arch
)
406 if (!abfd
->flags
& EXEC_P
)
416 hd
.address
= bfd_get_start_address (abfd
);
421 hd
.mn
= strip_suffix (abfd
->filename
);
424 sysroff_swap_hd_out (file
, &hd
);
430 struct coff_ofile
*p
;
431 struct coff_section
*sec
;
435 sh
.section
= sec
->number
;
439 sysroff_swap_sh_out (file
, &sh
);
445 struct coff_ofile
*p
;
446 struct coff_section
*section
;
450 unsigned char stuff
[200];
453 while (i
< section
->bfd_section
->_raw_size
)
456 int todo
= 200; /* Copy in 200 byte lumps */
458 if (i
+ todo
> section
->bfd_section
->_raw_size
)
459 todo
= section
->bfd_section
->_raw_size
- i
;
464 if (abfd
->flags
& EXEC_P
)
465 ob
.address
= section
->address
;
476 ob
.cpf
= 0; /* Never compress */
478 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
479 ob
.data
.data
= stuff
;
480 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
483 /* Now fill the rest with blanks */
484 while (i
< section
->size
)
487 int todo
= 200; /* Copy in 200 byte lumps */
489 if (i
+ todo
> section
->size
)
490 todo
= section
->size
- i
;
493 ob
.cpf
= 0; /* Never compress */
495 memset (stuff
, 0, todo
);
496 ob
.data
.data
= stuff
;
497 sysroff_swap_ob_out (file
, &ob
);
500 /* Now fill the rest with blanks */
506 struct coff_ofile
*ptr
;
507 struct coff_section
*sec
;
509 int nr
= sec
->nrelocs
;
511 for (i
= 0; i
< nr
; i
++)
513 struct coff_reloc
*r
= sec
->relocs
+ i
;
514 struct coff_symbol
*ref
;
523 rl
.flen
= 32; /* SH Specific */
524 /* What sort of reloc ? Look in the section to find out */
526 if (ref
->visible
->type
== coff_vis_ext_ref
)
528 rl
.bcount
= 4; /* Always 4 for us */
530 rl
.symn
= ref
->er_number
;
532 else if (ref
->visible
->type
== coff_vis_common
)
534 rl
.bcount
= 11; /* Always 11 for us */
536 rl
.secn
= ref
->where
->section
->number
;
539 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
540 rl
.aopcode_is_0x20
= 0x20;
545 rl
.bcount
= 11; /* Always 11 for us */
547 rl
.secn
= ref
->where
->section
->number
;
550 rl
.addend
= -ref
->where
->section
->address
;
551 rl
.aopcode_is_0x20
= 0x20;
554 if (rl
.op
== OP_SEC_REF
555 || rl
.op
== OP_EXT_REF
)
557 sysroff_swap_rl_out (file
, &rl
);
564 struct coff_ofile
*p
;
567 for (i
= 1; i
< p
->nsections
; i
++)
569 wr_sh (p
, p
->sections
+ i
);
570 wr_ob (p
, p
->sections
+ i
);
571 wr_rl (p
, p
->sections
+ i
);
576 wr_dps_start (sfile
, section
, scope
, type
, nest
)
577 struct coff_sfile
*sfile
;
578 struct coff_section
*section
;
579 struct coff_scope
*scope
;
589 dps
.san
= scope
->sec
->number
;
590 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
591 dps
.block_size
= scope
->size
;
594 printf ("DPS %s %d %x\n",
610 sysroff_swap_dps_out (file
, &dps
);
614 wr_dps_end (section
, scope
, type
)
615 struct coff_section
*section
;
616 struct coff_scope
*scope
;
622 sysroff_swap_dps_out (file
, &dps
);
629 return (int *) (xcalloc (sizeof (int), x
));
632 static void walk_tree_symbol ();
634 walk_tree_type_1 (sfile
, symbol
, type
, nest
)
635 struct coff_sfile
*sfile
;
636 struct coff_symbol
*symbol
;
637 struct coff_type
*type
;
642 case coff_secdef_type
:
643 case coff_basic_type
:
647 switch (type
->u
.basic
)
651 dbt
.btype
= BTYPE_VOID
;
652 dbt
.sign
= BTYPE_UNSPEC
;
653 dbt
.fptype
= FPTYPE_NOTSPEC
;
656 dbt
.btype
= BTYPE_CHAR
;
657 dbt
.sign
= BTYPE_UNSPEC
;
658 dbt
.fptype
= FPTYPE_NOTSPEC
;
663 dbt
.btype
= BTYPE_INT
;
664 dbt
.sign
= SIGN_SIGNED
;
665 dbt
.fptype
= FPTYPE_NOTSPEC
;
668 dbt
.btype
= BTYPE_FLOAT
;
669 dbt
.fptype
= FPTYPE_SINGLE
;
672 dbt
.btype
= BTYPE_FLOAT
;
673 dbt
.fptype
= FPTYPE_DOUBLE
;
676 dbt
.btype
= BTYPE_FLOAT
;
677 dbt
.fptype
= FPTYPE_EXTENDED
;
680 dbt
.btype
= BTYPE_CHAR
;
681 dbt
.sign
= SIGN_UNSIGNED
;
682 dbt
.fptype
= FPTYPE_NOTSPEC
;
687 dbt
.btype
= BTYPE_INT
;
688 dbt
.sign
= SIGN_UNSIGNED
;
689 dbt
.fptype
= FPTYPE_NOTSPEC
;
692 dbt
.bitsize
= type
->size
;
694 sysroff_swap_dbt_out (file
, &dbt
);
697 case coff_pointer_type
:
700 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
702 sysroff_swap_dpt_out (file
, &dpt
);
706 case coff_function_type
:
709 struct coff_symbol
*param
;
712 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
715 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
717 sysroff_swap_dfp_out (file
, &dfp
);
719 for (param
= type
->u
.function
.parameters
->vars_head
;
723 walk_tree_symbol (sfile
, 0, param
, nest
);
726 sysroff_swap_dfp_out (file
, &dfp
);
730 case coff_structdef_type
:
734 struct coff_symbol
*member
;
736 dbt
.btype
= BTYPE_STRUCT
;
737 dbt
.bitsize
= type
->size
;
738 dbt
.sign
= SIGN_UNSPEC
;
739 dbt
.fptype
= FPTYPE_NOTSPEC
;
740 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
742 sysroff_swap_dbt_out (file
, &dbt
);
745 sysroff_swap_dds_out (file
, &dds
);
746 for (member
= type
->u
.astructdef
.elements
->vars_head
;
748 member
= member
->next
)
750 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
754 sysroff_swap_dds_out (file
, &dds
);
758 case coff_structref_type
:
761 dbt
.btype
= BTYPE_TAG
;
762 dbt
.bitsize
= type
->size
;
763 dbt
.sign
= SIGN_UNSPEC
;
764 dbt
.fptype
= FPTYPE_NOTSPEC
;
765 if (type
->u
.astructref
.ref
)
767 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
775 sysroff_swap_dbt_out (file
, &dbt
);
778 case coff_array_type
:
782 int dims
= 1; /* Only output one dimension at a time */
784 dar
.variable
= nints (dims
);
785 dar
.subtype
= nints (dims
);
786 dar
.spare
= nints (dims
);
787 dar
.max_variable
= nints (dims
);
788 dar
.maxspare
= nints (dims
);
789 dar
.max
= nints (dims
);
790 dar
.min_variable
= nints (dims
);
791 dar
.min
= nints (dims
);
792 dar
.minspare
= nints (dims
);
794 dar
.length
= type
->size
/ type
->u
.array
.dim
;
795 for (j
= 0; j
< dims
; j
++)
797 dar
.variable
[j
] = VARIABLE_FIXED
;
798 dar
.subtype
[j
] = SUB_INTEGER
;
800 dar
.max_variable
[j
] = 0;
801 dar
.max
[j
] = type
->u
.array
.dim
;
802 dar
.min_variable
[j
] = 0;
803 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
805 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
806 sysroff_swap_dar_out (file
, &dar
);
809 case coff_enumdef_type
:
813 struct coff_symbol
*member
;
814 dbt
.btype
= BTYPE_ENUM
;
815 dbt
.bitsize
= type
->size
;
816 dbt
.sign
= SIGN_UNSPEC
;
817 dbt
.fptype
= FPTYPE_NOTSPEC
;
818 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
820 sysroff_swap_dbt_out (file
, &dbt
);
825 sysroff_swap_den_out (file
, &den
);
826 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
828 member
= member
->next
)
830 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
834 sysroff_swap_den_out (file
, &den
);
839 case coff_enumref_type
:
842 dbt
.btype
= BTYPE_TAG
;
843 dbt
.bitsize
= type
->size
;
844 dbt
.sign
= SIGN_UNSPEC
;
845 dbt
.fptype
= FPTYPE_NOTSPEC
;
846 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
848 sysroff_swap_dbt_out (file
, &dbt
);
864 sysroff_swap_dty_out (file, &dty);
874 sysroff_swap_dty_out (file, &dty);
879 dump_tree_structure (sfile, symbol, type, nest)
880 struct coff_sfile *sfile;
881 struct coff_symbol *symbol;
882 struct coff_type *type;
885 if (symbol->type->type == coff_function_type)
895 walk_tree_type (sfile
, symbol
, type
, nest
)
899 struct coff_symbol
*symbol
;
900 struct coff_type
*type
;
903 if (symbol
->type
->type
== coff_function_type
)
910 sysroff_swap_dty_out (file
, &dty
);
911 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
913 sysroff_swap_dty_out (file
, &dty
);
916 symbol
->where
->section
,
917 symbol
->type
->u
.function
.code
,
918 BLOCK_TYPE_FUNCTION
, nest
);
919 wr_dps_start (sfile
, symbol
->where
->section
,
920 symbol
->type
->u
.function
.code
,
921 BLOCK_TYPE_BLOCK
, nest
);
922 walk_tree_scope (symbol
->where
->section
,
924 symbol
->type
->u
.function
.code
,
925 nest
+ 1, BLOCK_TYPE_BLOCK
);
927 wr_dps_end (symbol
->where
->section
,
928 symbol
->type
->u
.function
.code
,
930 wr_dps_end (symbol
->where
->section
,
931 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
939 sysroff_swap_dty_out (file
, &dty
);
940 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
942 sysroff_swap_dty_out (file
, &dty
);
950 walk_tree_symbol (sfile
, section
, symbol
, nest
)
951 struct coff_sfile
*sfile
;
952 struct coff_section
*section
;
953 struct coff_symbol
*symbol
;
961 switch (symbol
->type
->type
)
963 case coff_function_type
:
964 dsy
.type
= STYPE_FUNC
;
967 case coff_structref_type
:
968 case coff_pointer_type
:
969 case coff_array_type
:
970 case coff_basic_type
:
971 case coff_enumref_type
:
972 dsy
.type
= STYPE_VAR
;
975 case coff_enumdef_type
:
976 dsy
.type
= STYPE_TAG
;
980 case coff_structdef_type
:
981 dsy
.type
= STYPE_TAG
;
983 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
985 case coff_secdef_type
:
991 if (symbol
->where
->where
== coff_where_member_of_struct
)
994 dsy
.type
= STYPE_MEMBER
;
996 if (symbol
->where
->where
== coff_where_member_of_enum
)
998 dsy
.type
= STYPE_ENUM
;
1001 dsy
.value
= symbol
->where
->offset
;
1004 if (symbol
->type
->type
== coff_structdef_type
1005 || symbol
->where
->where
== coff_where_entag
1006 || symbol
->where
->where
== coff_where_strtag
)
1008 dsy
.snumber
= get_member_id (symbol
->number
);
1012 dsy
.snumber
= get_ordinary_id (symbol
->number
);
1016 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
1018 switch (symbol
->visible
->type
)
1020 case coff_vis_common
:
1021 case coff_vis_ext_def
:
1022 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
1024 case coff_vis_ext_ref
:
1025 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
1027 case coff_vis_int_def
:
1028 dsy
.ainfo
= AINFO_STATIC_INT
;
1031 case coff_vis_autoparam
:
1032 dsy
.ainfo
= AINFO_AUTO
;
1034 case coff_vis_register
:
1035 case coff_vis_regparam
:
1036 dsy
.ainfo
= AINFO_REG
;
1040 case coff_vis_member_of_struct
:
1041 case coff_vis_member_of_enum
:
1047 dsy
.dlength
= symbol
->type
->size
;
1048 switch (symbol
->where
->where
)
1050 case coff_where_memory
:
1052 dsy
.section
= symbol
->where
->section
->number
;
1057 case coff_where_member_of_struct
:
1058 case coff_where_member_of_enum
:
1059 case coff_where_stack
:
1060 case coff_where_register
:
1061 case coff_where_unknown
:
1062 case coff_where_strtag
:
1064 case coff_where_entag
:
1065 case coff_where_typedef
:
1071 switch (symbol
->where
->where
)
1073 case coff_where_memory
:
1074 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1076 case coff_where_stack
:
1077 dsy
.address
= symbol
->where
->offset
;
1079 case coff_where_member_of_struct
:
1082 if (symbol
->where
->bitsize
)
1084 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1086 dsy
.field_len
= symbol
->where
->bitsize
;
1087 dsy
.field_off
= (bits
/ 32) * 4;
1088 dsy
.field_bitoff
= bits
% 32;
1094 dsy
.field_len
= symbol
->type
->size
;
1095 dsy
.field_off
= symbol
->where
->offset
;
1098 case coff_where_member_of_enum
:
1100 dsy.field_len = symbol->type->size;
1101 dsy.field_off = symbol->where->offset; */
1103 case coff_where_register
:
1104 case coff_where_unknown
:
1105 case coff_where_strtag
:
1107 case coff_where_entag
:
1108 case coff_where_typedef
:
1114 if (symbol
->where
->where
== coff_where_register
)
1117 dsy
.reg
= rname_sh
[symbol
->where
->offset
];
1119 dsy
.reg
= rname_h8300
[symbol
->where
->offset
];
1122 switch (symbol
->visible
->type
)
1124 case coff_vis_common
:
1125 /* We do this 'cause common C symbols are treated as extdefs */
1126 case coff_vis_ext_def
:
1127 case coff_vis_ext_ref
:
1129 dsy
.ename
= symbol
->name
;
1132 case coff_vis_regparam
:
1133 case coff_vis_autoparam
:
1134 dsy
.type
= STYPE_PARAMETER
;
1137 case coff_vis_int_def
:
1140 case coff_vis_register
:
1142 case coff_vis_member_of_struct
:
1143 case coff_vis_member_of_enum
:
1155 sysroff_swap_dsy_out (file
, &dsy
);
1157 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1162 walk_tree_scope (section
, sfile
, scope
, nest
, type
)
1163 struct coff_section
*section
;
1164 struct coff_sfile
*sfile
;
1165 struct coff_scope
*scope
;
1169 struct coff_symbol
*vars
;
1170 struct coff_scope
*child
;
1172 if (scope
->vars_head
1173 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1175 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1178 wr_globals (tree
, sfile
, nest
+ 1);
1180 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1182 walk_tree_symbol (sfile
, section
, vars
, nest
);
1185 for (child
= scope
->list_head
; child
; child
= child
->next
)
1187 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1190 wr_dps_end (section
, scope
, type
);
1194 walk_tree_sfile (section
, sfile
)
1195 struct coff_section
*section
;
1196 struct coff_sfile
*sfile
;
1198 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1203 wr_program_structure (p
, sfile
)
1204 struct coff_ofile
*p
;
1205 struct coff_sfile
*sfile
;
1208 walk_tree_sfile (p
->sections
+ 4, sfile
);
1214 struct coff_ofile
*p
;
1215 struct coff_sfile
*sfile
;
1221 struct coff_symbol
*symbol
;
1222 static int incit
= 0x500000;
1227 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1228 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1230 du
.format
= abfd
->flags
& EXEC_P
? 0 : 1;
1233 du
.sections
= p
->nsections
- 1;
1234 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1235 du
.address
= nints (du
.sections
);
1236 du
.length
= nints (du
.sections
);
1238 for (i
= 0; i
< du
.sections
; i
++)
1244 /* Look through all the symbols and try and work out the extents in this
1247 for (symbol
= sfile
->scope
->vars_head
;
1249 symbol
= symbol
->next
)
1251 if (symbol
->type
->type
== coff_secdef_type
)
1253 unsigned int low
= symbol
->where
->offset
;
1254 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1255 struct coff_section
*section
= symbol
->where
->section
;
1257 int sn
= section
->number
;
1258 if (low
< lowest
[sn
])
1260 if (high
> highest
[sn
])
1266 for (i
= 0; i
< du
.sections
; i
++)
1268 if (highest
[i
] == 0)
1270 lowest
[i
] = highest
[i
] = incit
;
1273 du
.length
[used
] = highest
[i
] - lowest
[i
];
1274 du
.address
[used
] = abfd
->flags
& EXEC_P
? lowest
[i
] : 0;
1277 printf (" section %6s 0x%08x..0x%08x\n",
1278 p
->sections
[i
+ 1].name
,
1287 for (j
= 0; j
< lim
; j
++)
1292 if (sfile
->section
[src
].init
)
1295 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1297 = sfile
->section
[src
].low
;
1302 du
.address
[dst
] = 0;
1306 if (sfile
->section
[src
].parent
)
1308 printf (" section %6s 0x%08x..0x%08x\n",
1309 sfile
->section
[src
].parent
->name
,
1311 du
.address
[dst
] + du
.length
[dst
] - 1);
1314 du
.sections
= dst
+ 1;
1320 sysroff_swap_du_out (file
, &du
);
1325 struct coff_ofile
*p
;
1326 struct coff_sfile
*sfile
;
1332 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1333 dus
.drb
= nints (dus
.ns
);
1334 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1335 dus
.spare
= nints (dus
.ns
);
1337 /* Find the filenames */
1341 for (sfile
= p
->source_head
;
1343 sfile
= sfile
->next
)
1347 dus
.fname
[i
] = sfile
->name
;
1352 dus
.fname
[0] = sfile
->name
;
1355 sysroff_swap_dus_out (file
, &dus
);
1359 /* Find the offset of the .text section for this sfile in the
1360 .text section for the output file */
1363 find_base (sfile
, section
)
1364 struct coff_sfile
*sfile
;
1365 struct coff_section
*section
;
1367 return sfile
->section
[section
->number
].low
;
1370 wr_dln (p
, sfile
, n
)
1371 struct coff_ofile
*p
;
1372 struct coff_sfile
*sfile
;
1379 /* Count up all the linenumbers */
1380 struct coff_symbol
*sy
;
1386 for (sy
= p
->symbol_list_head
;
1388 sy
= sy
->next_in_ofile_list
)
1390 struct coff_type
*t
= sy
->type
;
1391 if (t
->type
== coff_function_type
)
1393 struct coff_line
*l
= t
->u
.function
.lines
;
1398 dln
.sfn
= nints (lc
);
1399 dln
.sln
= nints (lc
);
1400 dln
.lln
= nints (lc
);
1401 dln
.section
= nints (lc
);
1403 dln
.from_address
= nints (lc
);
1404 dln
.to_address
= nints (lc
);
1411 /* Run through once more and fill up the structure */
1413 for (sy
= p
->symbol_list_head
;
1415 sy
= sy
->next_in_ofile_list
)
1417 if (sy
->type
->type
== coff_function_type
)
1420 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1421 for (i
= 0; i
< l
->nlines
; i
++)
1423 dln
.section
[idx
] = sy
->where
->section
->number
;
1425 dln
.sln
[idx
] = l
->lines
[i
];
1426 dln
.from_address
[idx
] = l
->addresses
[i
];
1428 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1434 sysroff_swap_dln_out (file
, &dln
);
1439 /* Count up all the linenumbers */
1441 struct coff_symbol
*sy
;
1447 for (sy
= sfile
->scope
->vars_head
;
1451 struct coff_type
*t
= sy
->type
;
1452 if (t
->type
== coff_function_type
)
1454 struct coff_line
*l
= t
->u
.function
.lines
;
1460 dln
.sfn
= nints (lc
);
1461 dln
.sln
= nints (lc
);
1462 dln
.cc
= nints (lc
);
1463 dln
.section
= nints (lc
);
1465 dln
.from_address
= nints (lc
);
1466 dln
.to_address
= nints (lc
);
1473 /* Run through once more and fill up the structure */
1475 for (sy
= sfile
->scope
->vars_head
;
1479 if (sy
->type
->type
== coff_function_type
)
1482 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1485 int base
= find_base (sfile
, sy
->where
->section
);
1486 for (i
= 0; i
< l
->nlines
; i
++)
1488 dln
.section
[idx
] = sy
->where
->section
->number
;
1490 dln
.sln
[idx
] = l
->lines
[i
];
1491 dln
.from_address
[idx
] =
1492 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1495 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1499 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1504 sysroff_swap_dln_out (file
, &dln
);
1508 /* Write the global symbols out to the debug info */
1510 wr_globals (p
, sfile
, n
)
1511 struct coff_ofile
*p
;
1512 struct coff_sfile
*sfile
;
1515 struct coff_symbol
*sy
;
1516 for (sy
= p
->symbol_list_head
;
1518 sy
= sy
->next_in_ofile_list
)
1520 if (sy
->visible
->type
== coff_vis_ext_def
1521 || sy
->visible
->type
== coff_vis_ext_ref
)
1523 /* Only write out symbols if they belong to
1524 the current source file */
1525 if (sy
->sfile
== sfile
)
1526 walk_tree_symbol (sfile
, 0, sy
, 0);
1534 struct coff_ofile
*p
;
1536 struct coff_sfile
*sfile
;
1538 for (sfile
= p
->source_head
;
1540 sfile
= sfile
->next
)
1545 printf ("%s\n", sfile
->name
);
1547 wr_du (p
, sfile
, n
);
1549 wr_program_structure (p
, sfile
);
1550 wr_dln (p
, sfile
, n
);
1557 /* It seems that the CS struct is not normal - the size is wrong
1558 heres one I prepared earlier.. */
1560 {0x80, 0x21, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80,
1561 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00,
1562 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0xde};
1563 fwrite (b
, 1, sizeof (b
), file
);
1566 /* Write out the SC records for a unit. Create an SC
1567 for all the sections which appear in the output file, even
1568 if there isn't an equivalent one on the input */
1572 struct coff_ofile
*ptr
;
1573 struct coff_sfile
*sfile
;
1577 /* First work out the total number of sections */
1579 int total_sec
= ptr
->nsections
;
1583 struct coff_section
*sec
;
1584 struct coff_symbol
*symbol
;
1586 struct coff_symbol
*symbol
;
1589 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1593 for (i
= 0; i
< total_sec
; i
++)
1595 info
[i
].sec
= ptr
->sections
+ i
;
1599 for (symbol
= sfile
->scope
->vars_head
;
1601 symbol
= symbol
->next
)
1604 if (symbol
->type
->type
== coff_secdef_type
)
1606 for (i
= 0; i
< total_sec
; i
++)
1608 if (symbol
->where
->section
== info
[i
].sec
)
1610 info
[i
].symbol
= symbol
;
1617 /* Now output all the section info, and fake up some stuff for sections
1620 for (i
= 1; i
< total_sec
; i
++)
1624 symbol
= info
[i
].symbol
;
1629 /* Don't have a symbol set aside for this section, which means that nothing
1630 in this file does anything for the section. */
1631 sc
.format
= !(abfd
->flags
& EXEC_P
);
1634 name
= info
[i
].sec
->name
;
1638 if (abfd
->flags
& EXEC_P
)
1641 sc
.addr
= symbol
->where
->offset
;
1648 sc
.length
= symbol
->type
->size
;
1649 name
= symbol
->name
;
1654 sc
.concat
= CONCAT_SIMPLE
;
1662 sc
.spare1
= 0; /* If not zero, then it doesn't work */
1663 sc
.name
= section_translate (name
);
1664 if (strlen (sc
.name
) == 1)
1670 sc
.contents
= CONTENTS_DATA
;
1673 sc
.contents
= CONTENTS_CODE
;
1678 sc
.contents
= CONTENTS_CODE
;
1682 sysroff_swap_sc_out (file
, &sc
);
1690 wr_er (ptr
, sfile
, first
)
1691 struct coff_ofile
*ptr
;
1692 struct coff_sfile
*sfile
;
1696 struct coff_symbol
*sym
;
1699 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1701 if (sym
->visible
->type
== coff_vis_ext_ref
)
1705 er
.type
= ER_NOTSPEC
;
1706 er
.name
= sym
->name
;
1707 sysroff_swap_er_out (file
, &er
);
1708 sym
->er_number
= idx
++;
1716 wr_ed (ptr
, sfile
, first
)
1717 struct coff_ofile
*ptr
;
1718 struct coff_sfile
*sfile
;
1721 struct coff_symbol
*s
;
1724 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1726 if (s
->visible
->type
== coff_vis_ext_def
1727 || s
->visible
->type
== coff_vis_common
)
1731 ed
.section
= s
->where
->section
->number
;
1733 if (s
->where
->section
->data
)
1735 ed
.type
= ED_TYPE_DATA
;
1737 else if (s
->where
->section
->code
& SEC_CODE
)
1739 ed
.type
= ED_TYPE_ENTRY
;
1743 ed
.type
= ED_TYPE_NOTSPEC
;
1744 ed
.type
= ED_TYPE_DATA
;
1746 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1748 sysroff_swap_ed_out (file
, &ed
);
1756 struct coff_ofile
*ptr
;
1758 struct coff_sfile
*sfile
;
1760 for (sfile
= ptr
->source_head
;
1762 sfile
= sfile
->next
)
1768 wr_un (ptr
, sfile
, first
, 0);
1769 nsecs
= wr_sc (ptr
, sfile
);
1771 fseek (file
, p1
, 0);
1772 wr_un (ptr
, sfile
, first
, nsecs
);
1773 fseek (file
, p2
, 0);
1774 wr_er (ptr
, sfile
, first
);
1775 wr_ed (ptr
, sfile
, first
);
1782 struct coff_ofile
*p
;
1796 return (x
+ 3) & ~3;
1799 /* Find all the common variables and turn them into
1800 ordinary defs - dunno why, but thats what hitachi does with 'em */
1804 struct coff_ofile
*tree
;
1806 struct coff_symbol
*s
;
1807 struct coff_section
*common_section
;
1808 /* Find the common section - always section 3 */
1809 common_section
= tree
->sections
+ 3;
1810 for (s
= tree
->symbol_list_head
;
1812 s
= s
->next_in_ofile_list
)
1814 if (s
->visible
->type
== coff_vis_common
)
1816 struct coff_where
*w
= s
->where
;
1817 /* s->visible->type = coff_vis_ext_def; leave it as common */
1818 common_section
->size
= align (common_section
->size
);
1819 w
->offset
= common_section
->size
+ common_section
->address
;
1820 w
->section
= common_section
;
1821 common_section
->size
+= s
->type
->size
;
1822 common_section
->size
= align (common_section
->size
);
1830 show_usage (file
, status
)
1834 fprintf (file
, "Usage: %s [-dhVq] in-file [out-file]\n", program_name
);
1841 printf ("%s: Convert a COFF object file into a SYSROFF object file\n",
1843 show_usage (stdout
, 0);
1854 static struct option long_options
[] =
1856 {"debug", no_argument
, 0, 'd'},
1857 {"quick", no_argument
, 0, 'q'},
1858 {"noprescan", no_argument
, 0, 'n'},
1859 {"help", no_argument
, 0, 'h'},
1860 {"version", no_argument
, 0, 'V'},
1861 {NULL
, no_argument
, 0, 0}
1867 program_name
= av
[0];
1868 xmalloc_set_program_name (program_name
);
1870 while ((opt
= getopt_long (ac
, av
, "dhVqn", long_options
,
1889 printf ("GNU %s version %s\n", program_name
, PROGRAM_VERSION
);
1895 show_usage (stderr
, 1);
1900 /* The input and output files may be named on the command line. */
1904 input_file
= av
[optind
];
1908 output_file
= av
[optind
];
1911 show_usage (stderr
, 1);
1912 if (strcmp (input_file
, output_file
) == 0)
1915 "%s: input and output files must be different\n",
1926 fprintf (stderr
, "%s: no input file specified\n",
1933 /* Take a .o off the input file and stick on a .obj. If
1934 it doesn't end in .o, then stick a .obj on anyway */
1936 int len
= strlen (input_file
);
1937 output_file
= xmalloc (len
+ 5);
1938 strcpy (output_file
, input_file
);
1940 && output_file
[len
- 2] == '.'
1941 && output_file
[len
- 1] == 'o')
1943 output_file
[len
] = 'b';
1944 output_file
[len
+ 1] = 'j';
1945 output_file
[len
+ 2] = 0;
1949 strcat (output_file
, ".obj");
1953 abfd
= bfd_openr (input_file
, 0);
1956 bfd_fatal (input_file
);
1958 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1960 bfd_nonfatal (input_file
);
1961 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1963 list_matching_formats (matching
);
1969 file
= fopen (output_file
, "wb");
1973 fprintf (stderr
, "%s: unable to open output file %s\n",
1974 program_name
, output_file
);
1979 printf ("ids %d %d\n", base1
, base2
);
1980 tree
= coff_grok (abfd
);