1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994, 95, 96, 98, 1999 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., 59 Temple Place - Suite 330, Boston, MA
21 /* Written by Steve Chamberlain (sac@cygnus.com)
23 This program can be used to convert a coff object file
24 into a Hitachi OM/LM (Sysroff) format.
26 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"
42 static char *toolname
;
46 static void walk_tree_scope ();
47 static void wr_globals ();
48 static int find_base ();
54 static int noprescan
= 0;
55 static struct coff_ofile
*tree
;
57 static int absolute_p;
60 static int segmented_p
;
63 static int ids1
[20000];
64 static int ids2
[20000];
66 static int base1
= 0x18;
67 static int base2
= 0x2018;
96 if (strcmp (n
, ".text") == 0)
98 if (strcmp (n
, ".data") == 0)
100 if (strcmp (n
, ".bss") == 0)
107 #define DATE "940201073000"; /* Just a time on my birthday */
117 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
119 res
= (char *) xmalloc (i
+ 1);
120 memcpy (res
, name
, i
);
126 /* IT LEN stuff CS */
128 checksum (file
, ptr
, size
, code
)
137 int bytes
= size
/ 8;
138 last
= !(code
& 0xff00);
141 ptr
[0] = code
| (last
? 0x80 : 0);
144 for (j
= 0; j
< bytes
; j
++)
148 /* Glue on a checksum too */
150 fwrite (ptr
, bytes
+ 1, 1, file
);
157 writeINT (n
, ptr
, idx
, size
, file
)
173 /* Lets write out that record and do another one */
174 checksum (file
, ptr
, *idx
, code
| 0x1000);
186 ptr
[byte
+ 0] = n
>> 8;
190 ptr
[byte
+ 0] = n
>> 24;
191 ptr
[byte
+ 1] = n
>> 16;
192 ptr
[byte
+ 2] = n
>> 8;
193 ptr
[byte
+ 3] = n
>> 0;
203 writeBITS (val
, ptr
, idx
, size
)
215 /* Turn off all about to change bits */
216 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
217 /* Turn on the bits we want */
218 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
223 writeBARRAY (data
, ptr
, idx
, size
, file
)
231 writeINT (data
.len
, ptr
, idx
, 1, file
);
232 for (i
= 0; i
< data
.len
; i
++)
234 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
240 writeCHARS (string
, ptr
, idx
, size
, file
)
251 /* Lets write out that record and do another one */
252 checksum (file
, ptr
, *idx
, code
| 0x1000);
259 /* Variable length string */
260 size
= strlen (string
);
264 /* BUG WAITING TO HAPPEN */
265 memcpy (ptr
+ i
, string
, size
);
270 #define SYSROFF_SWAP_OUT
274 static char *rname_sh
[] =
276 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
279 static char *rname_h8300
[] =
281 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
287 /* The TR block is not normal - it doesn't have any contents. */
294 fwrite (b
, 1, sizeof (b
), file
);
298 wr_un (ptr
, sfile
, first
, nsecs
)
299 struct coff_ofile
*ptr
;
300 struct coff_sfile
*sfile
;
306 struct coff_symbol
*s
;
310 if (bfd_get_file_flags (abfd
) & EXEC_P
)
311 un
.format
= FORMAT_LM
;
313 un
.format
= FORMAT_OM
;
318 un
.nsections
= ptr
->nsections
- 1; /* Don't count the abs section */
320 /*NEW - only count sections with size */
321 un
.nsections
= nsecs
;
326 /* Count all the undefined and defined variables with global scope */
330 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
332 if (s
->visible
->type
== coff_vis_ext_def
333 || s
->visible
->type
== coff_vis_common
)
336 if (s
->visible
->type
== coff_vis_ext_ref
)
342 un
.linker
= "L_GX00";
344 un
.name
= sfile
->name
;
345 sysroff_swap_un_out (file
, &un
);
351 struct coff_ofile
*p
;
356 if (bfd_get_file_flags (abfd
) & EXEC_P
)
358 hd
.mt
= MTYPE_ABS_LM
;
362 hd
.mt
= MTYPE_OMS_OR_LMS
;
366 hd
.nu
= p
->nsources
; /* Always one unit */
367 hd
.code
= 0; /* Always ASCII */
368 hd
.ver
= "0200"; /* Version 2.00 */
369 switch (bfd_get_arch (abfd
))
377 switch (bfd_get_mach (abfd
))
383 toolname
= "C_H8/300";
385 case bfd_mach_h8300h
:
389 toolname
= "C_H8/300H";
391 case bfd_mach_h8300s
:
395 toolname
= "C_H8/300S";
400 rnames
= rname_h8300
;
418 if (! bfd_get_file_flags(abfd
) & EXEC_P
)
428 hd
.address
= bfd_get_start_address (abfd
);
433 hd
.mn
= strip_suffix (bfd_get_filename (abfd
));
435 sysroff_swap_hd_out (file
, &hd
);
441 struct coff_ofile
*p
;
442 struct coff_section
*sec
;
446 sh
.section
= sec
->number
;
450 sysroff_swap_sh_out (file
, &sh
);
456 struct coff_ofile
*p
;
457 struct coff_section
*section
;
461 unsigned char stuff
[200];
464 while (i
< section
->bfd_section
->_raw_size
)
467 int todo
= 200; /* Copy in 200 byte lumps */
469 if (i
+ todo
> section
->bfd_section
->_raw_size
)
470 todo
= section
->bfd_section
->_raw_size
- i
;
475 if (bfd_get_file_flags (abfd
) & EXEC_P
)
476 ob
.address
= section
->address
;
487 ob
.cpf
= 0; /* Never compress */
489 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
490 ob
.data
.data
= stuff
;
491 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
494 /* Now fill the rest with blanks */
495 while (i
< (bfd_size_type
) section
->size
)
498 int todo
= 200; /* Copy in 200 byte lumps */
500 if (i
+ todo
> (bfd_size_type
) section
->size
)
501 todo
= section
->size
- i
;
504 ob
.cpf
= 0; /* Never compress */
506 memset (stuff
, 0, todo
);
507 ob
.data
.data
= stuff
;
508 sysroff_swap_ob_out (file
, &ob
);
511 /* Now fill the rest with blanks */
517 struct coff_ofile
*ptr
;
518 struct coff_section
*sec
;
520 int nr
= sec
->nrelocs
;
522 for (i
= 0; i
< nr
; i
++)
524 struct coff_reloc
*r
= sec
->relocs
+ i
;
525 struct coff_symbol
*ref
;
534 rl
.flen
= 32; /* SH Specific */
535 /* What sort of reloc ? Look in the section to find out */
537 if (ref
->visible
->type
== coff_vis_ext_ref
)
539 rl
.bcount
= 4; /* Always 4 for us */
541 rl
.symn
= ref
->er_number
;
543 else if (ref
->visible
->type
== coff_vis_common
)
545 rl
.bcount
= 11; /* Always 11 for us */
547 rl
.secn
= ref
->where
->section
->number
;
550 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
551 rl
.aopcode_is_0x20
= 0x20;
556 rl
.bcount
= 11; /* Always 11 for us */
558 rl
.secn
= ref
->where
->section
->number
;
561 rl
.addend
= -ref
->where
->section
->address
;
562 rl
.aopcode_is_0x20
= 0x20;
565 if (rl
.op
== OP_SEC_REF
566 || rl
.op
== OP_EXT_REF
)
568 sysroff_swap_rl_out (file
, &rl
);
575 struct coff_ofile
*p
;
578 for (i
= 1; i
< p
->nsections
; i
++)
580 wr_sh (p
, p
->sections
+ i
);
581 wr_ob (p
, p
->sections
+ i
);
582 wr_rl (p
, p
->sections
+ i
);
587 wr_dps_start (sfile
, section
, scope
, type
, nest
)
588 struct coff_sfile
*sfile
;
589 struct coff_section
*section
;
590 struct coff_scope
*scope
;
600 dps
.san
= scope
->sec
->number
;
601 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
602 dps
.block_size
= scope
->size
;
605 printf ("DPS %s %d %x\n",
621 sysroff_swap_dps_out (file
, &dps
);
625 wr_dps_end (section
, scope
, type
)
626 struct coff_section
*section
;
627 struct coff_scope
*scope
;
633 sysroff_swap_dps_out (file
, &dps
);
640 return (int *) (xcalloc (sizeof (int), x
));
643 static void walk_tree_symbol ();
645 walk_tree_type_1 (sfile
, symbol
, type
, nest
)
646 struct coff_sfile
*sfile
;
647 struct coff_symbol
*symbol
;
648 struct coff_type
*type
;
653 case coff_secdef_type
:
654 case coff_basic_type
:
658 switch (type
->u
.basic
)
662 dbt
.btype
= BTYPE_VOID
;
663 dbt
.sign
= BTYPE_UNSPEC
;
664 dbt
.fptype
= FPTYPE_NOTSPEC
;
667 dbt
.btype
= BTYPE_CHAR
;
668 dbt
.sign
= BTYPE_UNSPEC
;
669 dbt
.fptype
= FPTYPE_NOTSPEC
;
674 dbt
.btype
= BTYPE_INT
;
675 dbt
.sign
= SIGN_SIGNED
;
676 dbt
.fptype
= FPTYPE_NOTSPEC
;
679 dbt
.btype
= BTYPE_FLOAT
;
680 dbt
.fptype
= FPTYPE_SINGLE
;
683 dbt
.btype
= BTYPE_FLOAT
;
684 dbt
.fptype
= FPTYPE_DOUBLE
;
687 dbt
.btype
= BTYPE_FLOAT
;
688 dbt
.fptype
= FPTYPE_EXTENDED
;
691 dbt
.btype
= BTYPE_CHAR
;
692 dbt
.sign
= SIGN_UNSIGNED
;
693 dbt
.fptype
= FPTYPE_NOTSPEC
;
698 dbt
.btype
= BTYPE_INT
;
699 dbt
.sign
= SIGN_UNSIGNED
;
700 dbt
.fptype
= FPTYPE_NOTSPEC
;
703 dbt
.bitsize
= type
->size
;
705 sysroff_swap_dbt_out (file
, &dbt
);
708 case coff_pointer_type
:
711 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
713 sysroff_swap_dpt_out (file
, &dpt
);
717 case coff_function_type
:
720 struct coff_symbol
*param
;
723 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
726 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
728 sysroff_swap_dfp_out (file
, &dfp
);
730 for (param
= type
->u
.function
.parameters
->vars_head
;
734 walk_tree_symbol (sfile
, 0, param
, nest
);
737 sysroff_swap_dfp_out (file
, &dfp
);
741 case coff_structdef_type
:
745 struct coff_symbol
*member
;
747 dbt
.btype
= BTYPE_STRUCT
;
748 dbt
.bitsize
= type
->size
;
749 dbt
.sign
= SIGN_UNSPEC
;
750 dbt
.fptype
= FPTYPE_NOTSPEC
;
751 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
753 sysroff_swap_dbt_out (file
, &dbt
);
756 sysroff_swap_dds_out (file
, &dds
);
757 for (member
= type
->u
.astructdef
.elements
->vars_head
;
759 member
= member
->next
)
761 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
765 sysroff_swap_dds_out (file
, &dds
);
769 case coff_structref_type
:
772 dbt
.btype
= BTYPE_TAG
;
773 dbt
.bitsize
= type
->size
;
774 dbt
.sign
= SIGN_UNSPEC
;
775 dbt
.fptype
= FPTYPE_NOTSPEC
;
776 if (type
->u
.astructref
.ref
)
778 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
786 sysroff_swap_dbt_out (file
, &dbt
);
789 case coff_array_type
:
793 int dims
= 1; /* Only output one dimension at a time */
795 dar
.variable
= nints (dims
);
796 dar
.subtype
= nints (dims
);
797 dar
.spare
= nints (dims
);
798 dar
.max_variable
= nints (dims
);
799 dar
.maxspare
= nints (dims
);
800 dar
.max
= nints (dims
);
801 dar
.min_variable
= nints (dims
);
802 dar
.min
= nints (dims
);
803 dar
.minspare
= nints (dims
);
805 dar
.length
= type
->size
/ type
->u
.array
.dim
;
806 for (j
= 0; j
< dims
; j
++)
808 dar
.variable
[j
] = VARIABLE_FIXED
;
809 dar
.subtype
[j
] = SUB_INTEGER
;
811 dar
.max_variable
[j
] = 0;
812 dar
.max
[j
] = type
->u
.array
.dim
;
813 dar
.min_variable
[j
] = 0;
814 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
816 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
817 sysroff_swap_dar_out (file
, &dar
);
820 case coff_enumdef_type
:
824 struct coff_symbol
*member
;
825 dbt
.btype
= BTYPE_ENUM
;
826 dbt
.bitsize
= type
->size
;
827 dbt
.sign
= SIGN_UNSPEC
;
828 dbt
.fptype
= FPTYPE_NOTSPEC
;
829 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
831 sysroff_swap_dbt_out (file
, &dbt
);
836 sysroff_swap_den_out (file
, &den
);
837 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
839 member
= member
->next
)
841 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
845 sysroff_swap_den_out (file
, &den
);
850 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
);
875 sysroff_swap_dty_out (file, &dty);
885 sysroff_swap_dty_out (file, &dty);
890 dump_tree_structure (sfile, symbol, type, nest)
891 struct coff_sfile *sfile;
892 struct coff_symbol *symbol;
893 struct coff_type *type;
896 if (symbol->type->type == coff_function_type)
906 walk_tree_type (sfile
, symbol
, type
, nest
)
910 struct coff_symbol
*symbol
;
911 struct coff_type
*type
;
914 if (symbol
->type
->type
== coff_function_type
)
921 sysroff_swap_dty_out (file
, &dty
);
922 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
924 sysroff_swap_dty_out (file
, &dty
);
927 symbol
->where
->section
,
928 symbol
->type
->u
.function
.code
,
929 BLOCK_TYPE_FUNCTION
, nest
);
930 wr_dps_start (sfile
, symbol
->where
->section
,
931 symbol
->type
->u
.function
.code
,
932 BLOCK_TYPE_BLOCK
, nest
);
933 walk_tree_scope (symbol
->where
->section
,
935 symbol
->type
->u
.function
.code
,
936 nest
+ 1, BLOCK_TYPE_BLOCK
);
938 wr_dps_end (symbol
->where
->section
,
939 symbol
->type
->u
.function
.code
,
941 wr_dps_end (symbol
->where
->section
,
942 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
950 sysroff_swap_dty_out (file
, &dty
);
951 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
953 sysroff_swap_dty_out (file
, &dty
);
961 walk_tree_symbol (sfile
, section
, symbol
, nest
)
962 struct coff_sfile
*sfile
;
963 struct coff_section
*section
;
964 struct coff_symbol
*symbol
;
969 memset(&dsy
, 0, sizeof(dsy
));
972 switch (symbol
->type
->type
)
974 case coff_function_type
:
975 dsy
.type
= STYPE_FUNC
;
978 case coff_structref_type
:
979 case coff_pointer_type
:
980 case coff_array_type
:
981 case coff_basic_type
:
982 case coff_enumref_type
:
983 dsy
.type
= STYPE_VAR
;
986 case coff_enumdef_type
:
987 dsy
.type
= STYPE_TAG
;
991 case coff_structdef_type
:
992 dsy
.type
= STYPE_TAG
;
994 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
996 case coff_secdef_type
:
1002 if (symbol
->where
->where
== coff_where_member_of_struct
)
1005 dsy
.type
= STYPE_MEMBER
;
1007 if (symbol
->where
->where
== coff_where_member_of_enum
)
1009 dsy
.type
= STYPE_ENUM
;
1012 dsy
.evalue
= symbol
->where
->offset
;
1015 if (symbol
->type
->type
== coff_structdef_type
1016 || symbol
->where
->where
== coff_where_entag
1017 || symbol
->where
->where
== coff_where_strtag
)
1019 dsy
.snumber
= get_member_id (symbol
->number
);
1023 dsy
.snumber
= get_ordinary_id (symbol
->number
);
1027 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
1029 switch (symbol
->visible
->type
)
1031 case coff_vis_common
:
1032 case coff_vis_ext_def
:
1033 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
1035 case coff_vis_ext_ref
:
1036 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
1038 case coff_vis_int_def
:
1039 dsy
.ainfo
= AINFO_STATIC_INT
;
1042 case coff_vis_autoparam
:
1043 dsy
.ainfo
= AINFO_AUTO
;
1045 case coff_vis_register
:
1046 case coff_vis_regparam
:
1047 dsy
.ainfo
= AINFO_REG
;
1051 case coff_vis_member_of_struct
:
1052 case coff_vis_member_of_enum
:
1058 dsy
.dlength
= symbol
->type
->size
;
1059 switch (symbol
->where
->where
)
1061 case coff_where_memory
:
1063 dsy
.section
= symbol
->where
->section
->number
;
1068 case coff_where_member_of_struct
:
1069 case coff_where_member_of_enum
:
1070 case coff_where_stack
:
1071 case coff_where_register
:
1072 case coff_where_unknown
:
1073 case coff_where_strtag
:
1075 case coff_where_entag
:
1076 case coff_where_typedef
:
1082 switch (symbol
->where
->where
)
1084 case coff_where_memory
:
1085 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1087 case coff_where_stack
:
1088 dsy
.address
= symbol
->where
->offset
;
1090 case coff_where_member_of_struct
:
1093 if (symbol
->where
->bitsize
)
1095 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1097 dsy
.field_len
= symbol
->where
->bitsize
;
1098 dsy
.field_off
= (bits
/ 32) * 4;
1099 dsy
.field_bitoff
= bits
% 32;
1105 dsy
.field_len
= symbol
->type
->size
;
1106 dsy
.field_off
= symbol
->where
->offset
;
1109 case coff_where_member_of_enum
:
1111 dsy.field_len = symbol->type->size;
1112 dsy.field_off = symbol->where->offset; */
1114 case coff_where_register
:
1115 case coff_where_unknown
:
1116 case coff_where_strtag
:
1118 case coff_where_entag
:
1119 case coff_where_typedef
:
1125 if (symbol
->where
->where
== coff_where_register
)
1126 dsy
.reg
= rnames
[symbol
->where
->offset
];
1128 switch (symbol
->visible
->type
)
1130 case coff_vis_common
:
1131 /* We do this 'cause common C symbols are treated as extdefs */
1132 case coff_vis_ext_def
:
1133 case coff_vis_ext_ref
:
1135 dsy
.ename
= symbol
->name
;
1138 case coff_vis_regparam
:
1139 case coff_vis_autoparam
:
1140 dsy
.type
= STYPE_PARAMETER
;
1143 case coff_vis_int_def
:
1146 case coff_vis_register
:
1148 case coff_vis_member_of_struct
:
1149 case coff_vis_member_of_enum
:
1161 sysroff_swap_dsy_out (file
, &dsy
);
1163 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1168 walk_tree_scope (section
, sfile
, scope
, nest
, type
)
1169 struct coff_section
*section
;
1170 struct coff_sfile
*sfile
;
1171 struct coff_scope
*scope
;
1175 struct coff_symbol
*vars
;
1176 struct coff_scope
*child
;
1178 if (scope
->vars_head
1179 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1181 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1184 wr_globals (tree
, sfile
, nest
+ 1);
1186 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1188 walk_tree_symbol (sfile
, section
, vars
, nest
);
1191 for (child
= scope
->list_head
; child
; child
= child
->next
)
1193 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1196 wr_dps_end (section
, scope
, type
);
1200 walk_tree_sfile (section
, sfile
)
1201 struct coff_section
*section
;
1202 struct coff_sfile
*sfile
;
1204 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1209 wr_program_structure (p
, sfile
)
1210 struct coff_ofile
*p
;
1211 struct coff_sfile
*sfile
;
1214 walk_tree_sfile (p
->sections
+ 4, sfile
);
1220 struct coff_ofile
*p
;
1221 struct coff_sfile
*sfile
;
1227 struct coff_symbol
*symbol
;
1228 static int incit
= 0x500000;
1233 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1234 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1235 du
.format
= bfd_get_file_flags (abfd
) & EXEC_P
? 0 : 1;
1240 du
.sections
= p
->nsections
- 1;
1241 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1242 du
.address
= nints (du
.sections
);
1243 du
.length
= nints (du
.sections
);
1245 for (i
= 0; i
< du
.sections
; i
++)
1251 /* Look through all the symbols and try and work out the extents in this
1254 for (symbol
= sfile
->scope
->vars_head
;
1256 symbol
= symbol
->next
)
1258 if (symbol
->type
->type
== coff_secdef_type
)
1260 unsigned int low
= symbol
->where
->offset
;
1261 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1262 struct coff_section
*section
= symbol
->where
->section
;
1264 int sn
= section
->number
;
1265 if (low
< lowest
[sn
])
1267 if (high
> highest
[sn
])
1273 for (i
= 0; i
< du
.sections
; i
++)
1275 if (highest
[i
] == 0)
1277 lowest
[i
] = highest
[i
] = incit
;
1280 du
.length
[used
] = highest
[i
] - lowest
[i
];
1281 du
.address
[used
] = bfd_get_file_flags (abfd
) & EXEC_P
? lowest
[i
] : 0;
1284 printf (" section %6s 0x%08x..0x%08x\n",
1285 p
->sections
[i
+ 1].name
,
1294 for (j
= 0; j
< lim
; j
++)
1299 if (sfile
->section
[src
].init
)
1302 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1304 = sfile
->section
[src
].low
;
1309 du
.address
[dst
] = 0;
1313 if (sfile
->section
[src
].parent
)
1315 printf (" section %6s 0x%08x..0x%08x\n",
1316 sfile
->section
[src
].parent
->name
,
1318 du
.address
[dst
] + du
.length
[dst
] - 1);
1321 du
.sections
= dst
+ 1;
1327 sysroff_swap_du_out (file
, &du
);
1332 struct coff_ofile
*p
;
1333 struct coff_sfile
*sfile
;
1339 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1340 dus
.drb
= nints (dus
.ns
);
1341 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1342 dus
.spare
= nints (dus
.ns
);
1344 /* Find the filenames */
1348 for (sfile
= p
->source_head
;
1350 sfile
= sfile
->next
)
1354 dus
.fname
[i
] = sfile
->name
;
1359 dus
.fname
[0] = sfile
->name
;
1362 sysroff_swap_dus_out (file
, &dus
);
1366 /* Find the offset of the .text section for this sfile in the
1367 .text section for the output file */
1370 find_base (sfile
, section
)
1371 struct coff_sfile
*sfile
;
1372 struct coff_section
*section
;
1374 return sfile
->section
[section
->number
].low
;
1377 wr_dln (p
, sfile
, n
)
1378 struct coff_ofile
*p
;
1379 struct coff_sfile
*sfile
;
1386 /* Count up all the linenumbers */
1387 struct coff_symbol
*sy
;
1393 for (sy
= p
->symbol_list_head
;
1395 sy
= sy
->next_in_ofile_list
)
1397 struct coff_type
*t
= sy
->type
;
1398 if (t
->type
== coff_function_type
)
1400 struct coff_line
*l
= t
->u
.function
.lines
;
1405 dln
.sfn
= nints (lc
);
1406 dln
.sln
= nints (lc
);
1407 dln
.lln
= nints (lc
);
1408 dln
.section
= nints (lc
);
1410 dln
.from_address
= nints (lc
);
1411 dln
.to_address
= nints (lc
);
1418 /* Run through once more and fill up the structure */
1420 for (sy
= p
->symbol_list_head
;
1422 sy
= sy
->next_in_ofile_list
)
1424 if (sy
->type
->type
== coff_function_type
)
1427 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1428 for (i
= 0; i
< l
->nlines
; i
++)
1430 dln
.section
[idx
] = sy
->where
->section
->number
;
1432 dln
.sln
[idx
] = l
->lines
[i
];
1433 dln
.from_address
[idx
] = l
->addresses
[i
];
1435 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1441 sysroff_swap_dln_out (file
, &dln
);
1446 /* Count up all the linenumbers */
1448 struct coff_symbol
*sy
;
1454 for (sy
= sfile
->scope
->vars_head
;
1458 struct coff_type
*t
= sy
->type
;
1459 if (t
->type
== coff_function_type
)
1461 struct coff_line
*l
= t
->u
.function
.lines
;
1467 dln
.sfn
= nints (lc
);
1468 dln
.sln
= nints (lc
);
1469 dln
.cc
= nints (lc
);
1470 dln
.section
= nints (lc
);
1472 dln
.from_address
= nints (lc
);
1473 dln
.to_address
= nints (lc
);
1480 /* Run through once more and fill up the structure */
1482 for (sy
= sfile
->scope
->vars_head
;
1486 if (sy
->type
->type
== coff_function_type
)
1489 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1492 int base
= find_base (sfile
, sy
->where
->section
);
1493 for (i
= 0; i
< l
->nlines
; i
++)
1495 dln
.section
[idx
] = sy
->where
->section
->number
;
1497 dln
.sln
[idx
] = l
->lines
[i
];
1498 dln
.from_address
[idx
] =
1499 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1502 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1506 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1511 sysroff_swap_dln_out (file
, &dln
);
1515 /* Write the global symbols out to the debug info */
1517 wr_globals (p
, sfile
, n
)
1518 struct coff_ofile
*p
;
1519 struct coff_sfile
*sfile
;
1522 struct coff_symbol
*sy
;
1523 for (sy
= p
->symbol_list_head
;
1525 sy
= sy
->next_in_ofile_list
)
1527 if (sy
->visible
->type
== coff_vis_ext_def
1528 || sy
->visible
->type
== coff_vis_ext_ref
)
1530 /* Only write out symbols if they belong to
1531 the current source file */
1532 if (sy
->sfile
== sfile
)
1533 walk_tree_symbol (sfile
, 0, sy
, 0);
1541 struct coff_ofile
*p
;
1543 struct coff_sfile
*sfile
;
1545 for (sfile
= p
->source_head
;
1547 sfile
= sfile
->next
)
1552 printf ("%s\n", sfile
->name
);
1554 wr_du (p
, sfile
, n
);
1556 wr_program_structure (p
, sfile
);
1557 wr_dln (p
, sfile
, n
);
1565 /* It seems that the CS struct is not normal - the size is wrong
1566 heres one I prepared earlier.. */
1570 0x00, /* number of chars in variable length part */
1602 fwrite (b
, 1, sizeof (b
), file
);
1605 /* Write out the SC records for a unit. Create an SC
1606 for all the sections which appear in the output file, even
1607 if there isn't an equivalent one on the input */
1611 struct coff_ofile
*ptr
;
1612 struct coff_sfile
*sfile
;
1616 /* First work out the total number of sections */
1618 int total_sec
= ptr
->nsections
;
1622 struct coff_section
*sec
;
1623 struct coff_symbol
*symbol
;
1625 struct coff_symbol
*symbol
;
1628 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1632 for (i
= 0; i
< total_sec
; i
++)
1634 info
[i
].sec
= ptr
->sections
+ i
;
1638 for (symbol
= sfile
->scope
->vars_head
;
1640 symbol
= symbol
->next
)
1643 if (symbol
->type
->type
== coff_secdef_type
)
1645 for (i
= 0; i
< total_sec
; i
++)
1647 if (symbol
->where
->section
== info
[i
].sec
)
1649 info
[i
].symbol
= symbol
;
1656 /* Now output all the section info, and fake up some stuff for sections
1659 for (i
= 1; i
< total_sec
; i
++)
1663 symbol
= info
[i
].symbol
;
1668 /* Don't have a symbol set aside for this section, which means that nothing
1669 in this file does anything for the section. */
1670 sc
.format
= !(bfd_get_file_flags (abfd
) & EXEC_P
);
1673 name
= info
[i
].sec
->name
;
1677 if (bfd_get_file_flags (abfd
) & EXEC_P
)
1680 sc
.addr
= symbol
->where
->offset
;
1687 sc
.length
= symbol
->type
->size
;
1688 name
= symbol
->name
;
1693 sc
.concat
= CONCAT_SIMPLE
;
1701 sc
.spare1
= 0; /* If not zero, then it doesn't work */
1702 sc
.name
= section_translate (name
);
1703 if (strlen (sc
.name
) == 1)
1709 sc
.contents
= CONTENTS_DATA
;
1712 sc
.contents
= CONTENTS_CODE
;
1717 sc
.contents
= CONTENTS_CODE
;
1723 sysroff_swap_sc_out (file
, &sc
);
1733 /* Write out the ER records for a unit. */
1735 wr_er (ptr
, sfile
, first
)
1736 struct coff_ofile
*ptr
;
1737 struct coff_sfile
*sfile
;
1741 struct coff_symbol
*sym
;
1744 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1746 if (sym
->visible
->type
== coff_vis_ext_ref
)
1750 er
.type
= ER_NOTSPEC
;
1751 er
.name
= sym
->name
;
1752 sysroff_swap_er_out (file
, &er
);
1753 sym
->er_number
= idx
++;
1759 /* Write out the ED records for a unit. */
1761 wr_ed (ptr
, sfile
, first
)
1762 struct coff_ofile
*ptr
;
1763 struct coff_sfile
*sfile
;
1766 struct coff_symbol
*s
;
1769 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1771 if (s
->visible
->type
== coff_vis_ext_def
1772 || s
->visible
->type
== coff_vis_common
)
1776 ed
.section
= s
->where
->section
->number
;
1778 if (s
->where
->section
->data
)
1780 ed
.type
= ED_TYPE_DATA
;
1782 else if (s
->where
->section
->code
& SEC_CODE
)
1784 ed
.type
= ED_TYPE_ENTRY
;
1788 ed
.type
= ED_TYPE_NOTSPEC
;
1789 ed
.type
= ED_TYPE_DATA
;
1791 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1793 sysroff_swap_ed_out (file
, &ed
);
1801 struct coff_ofile
*ptr
;
1803 struct coff_sfile
*sfile
;
1805 for (sfile
= ptr
->source_head
;
1807 sfile
= sfile
->next
)
1813 wr_un (ptr
, sfile
, first
, 0);
1814 nsecs
= wr_sc (ptr
, sfile
);
1816 fseek (file
, p1
, SEEK_SET
);
1817 wr_un (ptr
, sfile
, first
, nsecs
);
1818 fseek (file
, p2
, SEEK_SET
);
1819 wr_er (ptr
, sfile
, first
);
1820 wr_ed (ptr
, sfile
, first
);
1827 struct coff_ofile
*p
;
1841 return (x
+ 3) & ~3;
1844 /* Find all the common variables and turn them into
1845 ordinary defs - dunno why, but thats what hitachi does with 'em */
1849 struct coff_ofile
*tree
;
1851 struct coff_symbol
*s
;
1852 struct coff_section
*common_section
;
1853 /* Find the common section - always section 3 */
1854 common_section
= tree
->sections
+ 3;
1855 for (s
= tree
->symbol_list_head
;
1857 s
= s
->next_in_ofile_list
)
1859 if (s
->visible
->type
== coff_vis_common
)
1861 struct coff_where
*w
= s
->where
;
1862 /* s->visible->type = coff_vis_ext_def; leave it as common */
1863 common_section
->size
= align (common_section
->size
);
1864 w
->offset
= common_section
->size
+ common_section
->address
;
1865 w
->section
= common_section
;
1866 common_section
->size
+= s
->type
->size
;
1867 common_section
->size
= align (common_section
->size
);
1875 show_usage (file
, status
)
1879 fprintf (file
, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name
);
1886 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1888 show_usage (stdout
, 0);
1899 static struct option long_options
[] =
1901 {"debug", no_argument
, 0, 'd'},
1902 {"quick", no_argument
, 0, 'q'},
1903 {"noprescan", no_argument
, 0, 'n'},
1904 {"help", no_argument
, 0, 'h'},
1905 {"version", no_argument
, 0, 'V'},
1906 {NULL
, no_argument
, 0, 0}
1912 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1913 setlocale (LC_MESSAGES
, "");
1915 bindtextdomain (PACKAGE
, LOCALEDIR
);
1916 textdomain (PACKAGE
);
1918 program_name
= av
[0];
1919 xmalloc_set_program_name (program_name
);
1921 while ((opt
= getopt_long (ac
, av
, "dhVqn", long_options
,
1940 printf (_("GNU %s version %s\n"), program_name
, PROGRAM_VERSION
);
1946 show_usage (stderr
, 1);
1951 /* The input and output files may be named on the command line. */
1955 input_file
= av
[optind
];
1959 output_file
= av
[optind
];
1962 show_usage (stderr
, 1);
1963 if (strcmp (input_file
, output_file
) == 0)
1966 _("%s: input and output files must be different\n"),
1977 fprintf (stderr
, _("%s: no input file specified\n"),
1984 /* Take a .o off the input file and stick on a .obj. If
1985 it doesn't end in .o, then stick a .obj on anyway */
1987 int len
= strlen (input_file
);
1988 output_file
= xmalloc (len
+ 5);
1989 strcpy (output_file
, input_file
);
1991 && output_file
[len
- 2] == '.'
1992 && output_file
[len
- 1] == 'o')
1994 output_file
[len
] = 'b';
1995 output_file
[len
+ 1] = 'j';
1996 output_file
[len
+ 2] = 0;
2000 strcat (output_file
, ".obj");
2004 abfd
= bfd_openr (input_file
, 0);
2007 bfd_fatal (input_file
);
2009 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
2011 bfd_nonfatal (input_file
);
2012 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
2014 list_matching_formats (matching
);
2020 file
= fopen (output_file
, FOPEN_WB
);
2024 fprintf (stderr
, _("%s: unable to open output file %s\n"),
2025 program_name
, output_file
);
2030 printf ("ids %d %d\n", base1
, base2
);
2031 tree
= coff_grok (abfd
);