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 */
31 #include <libiberty.h>
35 #include "coff/internal.h"
36 #include "../bfd/libcoff.h"
38 #define PROGRAM_VERSION "1.3"
44 static void walk_tree_scope ();
45 static void wr_globals ();
52 static struct coff_ofile
*tree
;
54 static int absolute_p
;
55 static int segmented_p
;
58 static int ids1
[10000];
59 static int ids2
[10000];
61 static int base1
= 0x18;
62 static int base2
= 0x2018;
69 char *r
= xmalloc (a
* b
);
101 if (strcmp (n
, ".text") == 0)
103 if (strcmp (n
, ".data") == 0)
105 if (strcmp (n
, ".bss") == 0)
112 #define DATE "940201073000"; /* Just a time on my birthday */
122 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
124 res
= (char *) xmalloc (i
+ 1);
125 memcpy (res
, name
, i
);
131 /* IT LEN stuff CS */
133 checksum (file
, ptr
, size
, code
)
142 int bytes
= size
/ 8;
143 last
= !(code
& 0xff00);
146 ptr
[0] = code
| (last
? 0x80 : 0);
149 for (j
= 0; j
< bytes
; j
++)
153 /* Glue on a checksum too */
155 fwrite (ptr
, bytes
+ 1, 1, file
);
162 writeINT (n
, ptr
, idx
, size
, file
)
178 /* Lets write out that record and do another one */
179 checksum (file
, ptr
, *idx
, code
| 0x1000);
191 ptr
[byte
+ 0] = n
>> 8;
195 ptr
[byte
+ 0] = n
>> 24;
196 ptr
[byte
+ 1] = n
>> 16;
197 ptr
[byte
+ 2] = n
>> 8;
198 ptr
[byte
+ 3] = n
>> 0;
208 writeBITS (val
, ptr
, idx
, size
)
220 /* Turn off all about to change bits */
221 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
222 /* Turn on the bits we want */
223 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
228 writeBARRAY (data
, ptr
, idx
, size
, file
)
236 writeINT (data
.len
, ptr
, idx
, 1, file
);
237 for (i
= 0; i
< data
.len
; i
++)
239 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
247 writeCHARS (string
, ptr
, idx
, size
, file
)
258 /* Lets write out that record and do another one */
259 checksum (file
, ptr
, *idx
, code
| 0x1000);
266 /* Variable length string */
267 size
= strlen (string
);
272 memcpy (ptr
+ i
, string
, size
);
277 #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"
295 sysroff_swap_tr_out (file
, &t
);
299 wr_un (ptr
, sfile
, first
)
300 struct coff_ofile
*ptr
;
301 struct coff_sfile
*sfile
;
305 struct coff_symbol
*s
;
307 if (abfd
->flags
& EXEC_P
)
308 un
.format
= FORMAT_LM
;
310 un
.format
= FORMAT_OM
;
312 un
.nsections
= ptr
->nsections
- 1; /* Don't count the abs section */
315 /* Count all the undefined and defined variables with global scope */
319 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
321 if (s
->visible
->type
== coff_vis_ext_def
322 || s
->visible
->type
== coff_vis_common
)
325 if (s
->visible
->type
== coff_vis_ext_ref
)
335 un
.tool
= "C_H8/300H";
338 un
.linker
= "L_GX00";
340 un
.name
= sfile
->name
;
341 sysroff_swap_un_out (file
, &un
, 1);
347 struct coff_ofile
*p
;
353 if (abfd
->flags
& EXEC_P
)
355 hd
.mt
= MTYPE_ABS_LM
;
359 hd
.mt
= MTYPE_OMS_OR_LMS
;
363 hd
.nu
= p
->nsources
; /* Always one unit */
364 hd
.code
= 0; /* Always ASCII */
365 hd
.ver
= "0200"; /* Version 2.00 */
366 switch (abfd
->arch_info
->arch
)
390 if (!abfd
->flags
& EXEC_P
)
400 hd
.address
= bfd_get_start_address (abfd
);
405 hd
.mn
= strip_suffix (abfd
->filename
);
406 sysroff_swap_hd_out (file
, &hd
);
412 struct coff_ofile
*p
;
413 struct coff_section
*sec
;
417 sh
.section
= sec
->number
;
421 sysroff_swap_sh_out (file
, &sh
);
427 struct coff_ofile
*p
;
428 struct coff_section
*section
;
432 unsigned char stuff
[200];
435 while (i
< section
->size
)
438 int todo
= 200; /* Copy in 200 byte lumps */
439 if (i
+ todo
> section
->size
)
440 todo
= section
->size
- i
;
445 if (abfd
->flags
& EXEC_P
)
446 ob
.address
= section
->address
;
457 ob
.cpf
= 0; /* Never compress */
459 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
460 ob
.data
.data
= stuff
;
461 sysroff_swap_ob_out (file
, &ob
, i
+ todo
< section
->size
);
468 struct coff_ofile
*ptr
;
469 struct coff_section
*sec
;
471 int nr
= sec
->nrelocs
;
473 for (i
= 0; i
< nr
; i
++)
475 struct coff_reloc
*r
= sec
->relocs
+ i
;
476 struct coff_symbol
*ref
;
485 rl
.flen
= 32; /* SH Specific */
486 /* What sort of reloc ? Look in the section to find out */
488 if (ref
->visible
->type
== coff_vis_ext_ref
)
490 rl
.bcount
= 4; /* Always 4 for us */
492 rl
.symn
= ref
->er_number
;
494 else if (ref
->visible
->type
== coff_vis_common
)
496 rl
.bcount
= 11; /* Always 11 for us */
498 rl
.secn
= ref
->where
->section
->number
;
501 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
502 rl
.aopcode_is_0x20
= 0x20;
507 rl
.bcount
= 11; /* Always 11 for us */
509 rl
.secn
= ref
->where
->section
->number
;
512 rl
.addend
= -ref
->where
->section
->address
;
513 rl
.aopcode_is_0x20
= 0x20;
516 if (rl
.op
== OP_SEC_REF
517 || rl
.op
== OP_EXT_REF
)
519 sysroff_swap_rl_out (file
, &rl
);
526 struct coff_ofile
*p
;
529 for (i
= 1; i
< p
->nsections
; i
++)
531 wr_sh (p
, p
->sections
+ i
);
532 wr_ob (p
, p
->sections
+ i
);
533 wr_rl (p
, p
->sections
+ i
);
538 wr_dps_start (sfile
, section
, scope
, type
, nest
)
539 struct coff_sfile
*sfile
;
540 struct coff_section
*section
;
541 struct coff_scope
*scope
;
550 dps
.san
= scope
->sec
->number
;
551 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
552 dps
.block_size
= scope
->size
;
555 printf ("DPS %s %d %x\n",
565 sysroff_swap_dps_out (file
, &dps
);
569 wr_dps_end (section
, scope
, type
)
570 struct coff_section
*section
;
571 struct coff_scope
*scope
;
577 sysroff_swap_dps_out (file
, &dps
);
584 return (int *) (xcalloc (sizeof (int), x
));
587 static void walk_tree_symbol ();
589 walk_tree_type_1 (sfile
, symbol
, type
, nest
)
590 struct coff_sfile
*sfile
;
591 struct coff_symbol
*symbol
;
592 struct coff_type
*type
;
597 case coff_secdef_type
:
598 case coff_basic_type
:
602 switch (type
->u
.basic
)
606 dbt
.btype
= BTYPE_VOID
;
607 dbt
.sign
= BTYPE_UNSPEC
;
608 dbt
.fptype
= FPTYPE_NOTSPEC
;
611 dbt
.btype
= BTYPE_CHAR
;
612 dbt
.sign
= BTYPE_UNSPEC
;
613 dbt
.fptype
= FPTYPE_NOTSPEC
;
618 dbt
.btype
= BTYPE_INT
;
619 dbt
.sign
= SIGN_SIGNED
;
620 dbt
.fptype
= FPTYPE_NOTSPEC
;
623 dbt
.btype
= BTYPE_FLOAT
;
624 dbt
.fptype
= FPTYPE_SINGLE
;
627 dbt
.btype
= BTYPE_FLOAT
;
628 dbt
.fptype
= FPTYPE_DOUBLE
;
631 dbt
.btype
= BTYPE_FLOAT
;
632 dbt
.fptype
= FPTYPE_EXTENDED
;
635 dbt
.btype
= BTYPE_CHAR
;
636 dbt
.sign
= SIGN_UNSIGNED
;
637 dbt
.fptype
= FPTYPE_NOTSPEC
;
642 dbt
.btype
= BTYPE_INT
;
643 dbt
.sign
= SIGN_UNSIGNED
;
644 dbt
.fptype
= FPTYPE_NOTSPEC
;
647 dbt
.bitsize
= type
->size
;
649 sysroff_swap_dbt_out (file
, &dbt
);
652 case coff_pointer_type
:
655 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
657 sysroff_swap_dpt_out (file
, &dpt
);
661 case coff_function_type
:
664 struct coff_symbol
*param
;
666 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
669 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
671 sysroff_swap_dfp_out (file
, &dfp
);
673 for (param
= type
->u
.function
.parameters
->vars_head
;
677 walk_tree_symbol (sfile
, 0, param
, nest
);
680 sysroff_swap_dfp_out (file
, &dfp
);
684 case coff_structdef_type
:
688 struct coff_symbol
*member
;
689 dbt
.btype
= BTYPE_STRUCT
;
690 dbt
.bitsize
= type
->size
;
691 dbt
.sign
= SIGN_UNSPEC
;
692 dbt
.fptype
= FPTYPE_NOTSPEC
;
693 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
695 sysroff_swap_dbt_out (file
, &dbt
);
698 sysroff_swap_dds_out (file
, &dds
);
699 for (member
= type
->u
.astructdef
.elements
->vars_head
;
701 member
= member
->next
)
703 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
707 sysroff_swap_dds_out (file
, &dds
);
711 case coff_structref_type
:
714 dbt
.btype
= BTYPE_TAG
;
715 dbt
.bitsize
= type
->size
;
716 dbt
.sign
= SIGN_UNSPEC
;
717 dbt
.fptype
= FPTYPE_NOTSPEC
;
718 if (type
->u
.astructref
.ref
)
720 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
728 sysroff_swap_dbt_out (file
, &dbt
);
731 case coff_array_type
:
735 int dims
= 1; /* Only output one dimension at a time */
737 dar
.variable
= nints (dims
);
738 dar
.subtype
= nints (dims
);
739 dar
.spare
= nints (dims
);
740 dar
.max_variable
= nints (dims
);
741 dar
.maxspare
= nints (dims
);
742 dar
.max
= nints (dims
);
743 dar
.min_variable
= nints (dims
);
744 dar
.min
= nints (dims
);
745 dar
.minspare
= nints (dims
);
747 for (j
= 0; j
< dims
; j
++)
749 dar
.variable
[j
] = VARIABLE_FIXED
;
750 dar
.subtype
[j
] = SUB_INTEGER
;
751 dar
.max_variable
[j
] = 0;
752 dar
.max
[j
] = type
->u
.array
.dim
;
753 dar
.min_variable
[j
] = 0;
754 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
756 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
757 sysroff_swap_dar_out (file
, &dar
);
760 case coff_enumdef_type
:
764 struct coff_symbol
*member
;
765 dbt
.btype
= BTYPE_ENUM
;
766 dbt
.bitsize
= type
->size
;
767 dbt
.sign
= SIGN_UNSPEC
;
768 dbt
.fptype
= FPTYPE_NOTSPEC
;
769 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
771 sysroff_swap_dbt_out (file
, &dbt
);
775 sysroff_swap_den_out (file
, &den
);
776 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
778 member
= member
->next
)
780 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
784 sysroff_swap_den_out (file
, &den
);
789 case coff_enumref_type
:
792 dbt
.btype
= BTYPE_TAG
;
793 dbt
.bitsize
= type
->size
;
794 dbt
.sign
= SIGN_UNSPEC
;
795 dbt
.fptype
= FPTYPE_NOTSPEC
;
796 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
798 sysroff_swap_dbt_out (file
, &dbt
);
812 sysroff_swap_dty_out (file
, &dty
);
822 sysroff_swap_dty_out (file
, &dty
);
827 dump_tree_structure (sfile
, symbol
, type
, nest
)
828 struct coff_sfile
*sfile
;
829 struct coff_symbol
*symbol
;
830 struct coff_type
*type
;
833 if (symbol
->type
->type
== coff_function_type
)
843 walk_tree_type (sfile
, symbol
, type
, nest
)
847 struct coff_symbol
*symbol
;
848 struct coff_type
*type
;
851 if (symbol
->type
->type
== coff_function_type
)
858 sysroff_swap_dty_out (file
, &dty
);
859 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
861 sysroff_swap_dty_out (file
, &dty
);
864 symbol
->where
->section
,
865 symbol
->type
->u
.function
.code
,
866 BLOCK_TYPE_FUNCTION
, nest
);
867 wr_dps_start (sfile
, symbol
->where
->section
,
868 symbol
->type
->u
.function
.code
,
869 BLOCK_TYPE_BLOCK
, nest
);
870 walk_tree_scope (symbol
->where
->section
,
872 symbol
->type
->u
.function
.code
,
873 nest
+ 1, BLOCK_TYPE_BLOCK
);
875 wr_dps_end (symbol
->where
->section
,
876 symbol
->type
->u
.function
.code
,
877 BLOCK_TYPE_BLOCK
, nest
);
878 wr_dps_end (symbol
->where
->section
,
879 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
, nest
);
887 sysroff_swap_dty_out (file
, &dty
);
888 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
890 sysroff_swap_dty_out (file
, &dty
);
898 walk_tree_symbol (sfile
, section
, symbol
, nest
)
899 struct coff_sfile
*sfile
;
900 struct coff_section
*section
;
901 struct coff_symbol
*symbol
;
909 switch (symbol
->type
->type
)
911 case coff_function_type
:
912 dsy
.type
= STYPE_FUNC
;
915 case coff_structref_type
:
916 case coff_pointer_type
:
917 case coff_array_type
:
918 case coff_basic_type
:
919 case coff_enumref_type
:
920 dsy
.type
= STYPE_VAR
;
923 case coff_enumdef_type
:
924 dsy
.type
= STYPE_TAG
;
928 case coff_structdef_type
:
929 dsy
.type
= STYPE_TAG
;
931 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
933 case coff_secdef_type
:
939 if (symbol
->where
->where
== coff_where_member_of_struct
)
942 dsy
.type
= STYPE_MEMBER
;
944 if (symbol
->where
->where
== coff_where_member_of_enum
)
946 dsy
.type
= STYPE_ENUM
;
949 dsy
.value
= symbol
->where
->offset
;
952 if (symbol
->type
->type
== coff_structdef_type
953 || symbol
->where
->where
== coff_where_entag
954 || symbol
->where
->where
== coff_where_strtag
)
956 dsy
.snumber
= get_member_id (symbol
->number
);
960 dsy
.snumber
= get_ordinary_id (symbol
->number
);
964 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
966 switch (symbol
->visible
->type
)
968 case coff_vis_common
:
969 case coff_vis_ext_def
:
970 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
972 case coff_vis_ext_ref
:
973 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
975 case coff_vis_int_def
:
976 dsy
.ainfo
= AINFO_STATIC_INT
;
979 case coff_vis_autoparam
:
980 dsy
.ainfo
= AINFO_AUTO
;
982 case coff_vis_register
:
983 case coff_vis_regparam
:
984 dsy
.ainfo
= AINFO_REG
;
988 case coff_vis_member_of_struct
:
989 case coff_vis_member_of_enum
:
995 dsy
.dlength
= symbol
->type
->size
;
996 switch (symbol
->where
->where
)
998 case coff_where_memory
:
1000 dsy
.section
= symbol
->where
->section
->number
;
1005 case coff_where_member_of_struct
:
1006 case coff_where_member_of_enum
:
1007 case coff_where_stack
:
1008 case coff_where_register
:
1009 case coff_where_unknown
:
1010 case coff_where_strtag
:
1012 case coff_where_entag
:
1013 case coff_where_typedef
:
1019 switch (symbol
->where
->where
)
1021 case coff_where_memory
:
1022 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1024 case coff_where_stack
:
1025 dsy
.address
= symbol
->where
->offset
;
1027 case coff_where_member_of_struct
:
1030 if (symbol
->where
->bitsize
)
1032 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1034 dsy
.field_len
= symbol
->where
->bitsize
;
1035 dsy
.field_off
= (bits
/ 32) * 4;
1036 dsy
.field_bitoff
= bits
% 32;
1042 dsy
.field_len
= symbol
->type
->size
;
1043 dsy
.field_off
= symbol
->where
->offset
;
1046 case coff_where_member_of_enum
:
1048 dsy.field_len = symbol->type->size;
1049 dsy.field_off = symbol->where->offset;*/
1051 case coff_where_register
:
1052 case coff_where_unknown
:
1053 case coff_where_strtag
:
1055 case coff_where_entag
:
1056 case coff_where_typedef
:
1062 if (symbol
->where
->where
== coff_where_register
)
1065 dsy
.reg
= rname_sh
[symbol
->where
->offset
];
1067 dsy
.reg
= rname_h8300
[symbol
->where
->offset
];
1070 switch (symbol
->visible
->type
)
1072 case coff_vis_common
:
1073 /* We do this 'cause common C symbols are treated as extdefs */
1074 case coff_vis_ext_def
:
1075 case coff_vis_ext_ref
:
1077 dsy
.ename
= symbol
->name
;
1080 case coff_vis_regparam
:
1081 case coff_vis_autoparam
:
1082 dsy
.type
= STYPE_PARAMETER
;
1085 case coff_vis_int_def
:
1088 case coff_vis_register
:
1090 case coff_vis_member_of_struct
:
1091 case coff_vis_member_of_enum
:
1103 sysroff_swap_dsy_out (file
, &dsy
);
1105 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1110 walk_tree_scope (section
, sfile
, scope
, nest
, type
)
1111 struct coff_section
*section
;
1112 struct coff_sfile
*sfile
;
1113 struct coff_scope
*scope
;
1117 struct coff_symbol
*vars
;
1118 struct coff_scope
*child
;
1120 if (scope
->vars_head
1121 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1123 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1126 wr_globals (tree
, sfile
, nest
+ 1);
1128 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1130 walk_tree_symbol (sfile
, section
, vars
, nest
);
1133 for (child
= scope
->list_head
; child
; child
= child
->next
)
1135 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1138 wr_dps_end (section
, scope
, type
);
1142 walk_tree_sfile (section
, sfile
)
1143 struct coff_section
*section
;
1144 struct coff_sfile
*sfile
;
1146 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1151 wr_program_structure (p
, sfile
)
1152 struct coff_ofile
*p
;
1153 struct coff_sfile
*sfile
;
1156 walk_tree_sfile (p
->sections
+ 4, sfile
);
1162 struct coff_ofile
*p
;
1163 struct coff_sfile
*sfile
;
1168 struct coff_symbol
*symbol
;
1169 static int incit
= 0x500000;
1173 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1174 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1175 du
.format
= abfd
->flags
& EXEC_P
? 0 : 1;
1178 du
.sections
= p
->nsections
- 1;
1179 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1180 du
.address
= nints (du
.sections
);
1181 du
.length
= nints (du
.sections
);
1183 for (i
= 0; i
< du
.sections
; i
++)
1189 /* Look through all the symbols and try and work out the extents in this
1192 for (symbol
= sfile
->scope
->vars_head
;
1194 symbol
= symbol
->next
)
1196 if (symbol
->type
->type
== coff_secdef_type
)
1198 unsigned int low
= symbol
->where
->offset
;
1199 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1200 struct coff_section
*section
= symbol
->where
->section
;
1202 int sn
= section
->number
;
1203 if (low
< lowest
[sn
])
1205 if (high
> highest
[sn
])
1211 for (i
= 0; i
< du
.sections
; i
++)
1213 if (highest
[i
] == 0)
1215 lowest
[i
] = highest
[i
] = incit
;
1218 du
.length
[used
] = highest
[i
] - lowest
[i
];
1219 du
.address
[used
] = abfd
->flags
& EXEC_P
? lowest
[i
] : 0;
1222 printf (" section %6s 0x%08x..0x%08x\n",
1223 p
->sections
[i
+ 1].name
,
1232 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;
1251 if (sfile
->section
[src
].parent
)
1253 printf (" section %6s 0x%08x..0x%08x\n",
1254 sfile
->section
[src
].parent
->name
,
1256 du
.address
[dst
] + du
.length
[dst
] - 1);
1259 du
.sections
= dst
+ 1;
1265 sysroff_swap_du_out (file
, &du
);
1270 struct coff_ofile
*p
;
1271 struct coff_sfile
*sfile
;
1278 dus
.ns
= 1 ; /* p->nsources; sac 14 jul 94 */
1279 dus
.drb
= nints (dus
.ns
);
1280 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1281 dus
.spare
= nints (dus
.ns
);
1283 /* Find the filenames */
1287 for (sfile
= p
->source_head
;
1289 sfile
= sfile
->next
)
1292 dus
.fname
[i
] = sfile
->name
;
1297 dus
.fname
[0] = sfile
->name
;
1300 sysroff_swap_dus_out (file
, &dus
);
1304 /* Find the offset of the .text section for this sfile in the
1305 .text section for the output file */
1308 find_base (sfile
, section
)
1309 struct coff_sfile
*sfile
;
1310 struct coff_section
*section
;
1312 return sfile
->section
[section
->number
].low
;
1315 wr_dln (p
, sfile
, n
)
1316 struct coff_ofile
*p
;
1317 struct coff_sfile
*sfile
;
1324 /* Count up all the linenumbers */
1325 struct coff_symbol
*sy
;
1331 for (sy
= p
->symbol_list_head
;
1333 sy
= sy
->next_in_ofile_list
)
1335 struct coff_type
*t
= sy
->type
;
1336 if (t
->type
== coff_function_type
)
1338 struct coff_line
*l
= t
->u
.function
.lines
;
1343 dln
.sfn
= nints (lc
);
1344 dln
.sln
= nints (lc
);
1345 dln
.lln
= nints (lc
);
1346 dln
.section
= nints (lc
);
1348 dln
.from_address
= nints (lc
);
1349 dln
.to_address
= nints (lc
);
1356 /* Run through once more and fill up the structure */
1358 for (sy
= p
->symbol_list_head
;
1360 sy
= sy
->next_in_ofile_list
)
1362 if (sy
->type
->type
== coff_function_type
)
1365 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1366 for (i
= 0; i
< l
->nlines
; i
++)
1368 dln
.section
[idx
] = sy
->where
->section
->number
;
1370 dln
.sln
[idx
] = l
->lines
[i
];
1371 dln
.from_address
[idx
] = l
->addresses
[i
];
1373 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1379 sysroff_swap_dln_out (file
, &dln
);
1384 /* Count up all the linenumbers */
1386 struct coff_symbol
*sy
;
1392 for (sy
= sfile
->scope
->vars_head
;
1396 struct coff_type
*t
= sy
->type
;
1397 if (t
->type
== coff_function_type
)
1399 struct coff_line
*l
= t
->u
.function
.lines
;
1405 dln
.sfn
= nints (lc
);
1406 dln
.sln
= nints (lc
);
1407 dln
.cc
= 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
= sfile
->scope
->vars_head
;
1424 if (sy
->type
->type
== coff_function_type
)
1427 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1429 int base
= find_base (sfile
, sy
->where
->section
);
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
] =
1436 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1439 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1443 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1448 sysroff_swap_dln_out (file
, &dln
);
1452 /* Write the global symbols out to the debug info */
1454 wr_globals (p
, sfile
, n
)
1455 struct coff_ofile
*p
;
1456 struct coff_sfile
*sfile
;
1459 struct coff_symbol
*sy
;
1460 for (sy
= p
->symbol_list_head
;
1462 sy
= sy
->next_in_ofile_list
)
1464 if (sy
->visible
->type
== coff_vis_ext_def
1465 || sy
->visible
->type
== coff_vis_ext_ref
)
1467 /* Only write out symbols if they belong to
1468 the current source file */
1469 if (sy
->sfile
== sfile
)
1470 walk_tree_symbol (sfile
, 0, sy
, 0);
1478 struct coff_ofile
*p
;
1480 struct coff_sfile
*sfile
;
1482 for (sfile
= p
->source_head
;
1484 sfile
= sfile
->next
)
1489 printf ("%s\n", sfile
->name
);
1491 wr_du (p
, sfile
, n
);
1493 wr_program_structure (p
, sfile
, n
);
1494 wr_dln (p
, sfile
, n
);
1501 /* It seems that the CS struct is not normal - the size is wrong
1502 heres one I prepared earlier.. */
1504 {0x80, 0x21, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80,
1505 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00,
1506 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0xde};
1507 fwrite (b
, 1, sizeof (b
), file
);
1510 /* Write out the SC records for a unit. Create an SC
1511 for all the sections which appear in the output file, even
1512 if there isn't an equivalent one on the input */
1516 struct coff_ofile
*ptr
;
1517 struct coff_sfile
*sfile
;
1520 /* First work out the total number of sections */
1522 int total_sec
= ptr
->nsections
;
1526 struct coff_section
*sec
;
1527 struct coff_symbol
*symbol
;
1529 struct coff_symbol
*symbol
;
1532 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1536 for (i
= 0; i
< total_sec
; i
++)
1538 info
[i
].sec
= ptr
->sections
+ i
;
1542 for (symbol
= sfile
->scope
->vars_head
;
1544 symbol
= symbol
->next
)
1547 if (symbol
->type
->type
== coff_secdef_type
)
1549 for (i
= 0; i
< total_sec
; i
++)
1551 if (symbol
->where
->section
== info
[i
].sec
)
1553 info
[i
].symbol
= symbol
;
1560 /* Now output all the section info, and fake up some stuff for sections
1563 for (i
= 1; i
< total_sec
; i
++)
1567 symbol
= info
[i
].symbol
;
1571 /* Don't have a symbol set aside for this section, which means that nothing
1572 in this file does anything for the section. */
1573 sc
.format
= !(abfd
->flags
& EXEC_P
);
1576 name
= info
[i
].sec
->name
;
1580 unsigned int low
= symbol
->where
->offset
;
1581 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1583 if (abfd
->flags
& EXEC_P
)
1586 sc
.addr
= symbol
->where
->offset
;
1593 sc
.length
= symbol
->type
->size
;
1594 name
= symbol
->name
;
1599 sc
.concat
= CONCAT_SIMPLE
;
1606 sc
.spare1
= 0; /* If not zero, then it doesn't work */
1607 sc
.name
= section_translate (name
);
1608 if (strlen (sc
.name
) == 1)
1614 sc
.contents
= CONTENTS_DATA
;
1617 sc
.contents
= CONTENTS_CODE
;
1622 sysroff_swap_sc_out (file
, &sc
);
1631 wr_er (ptr
, sfile
, first
)
1632 struct coff_ofile
*ptr
;
1633 struct coff_sfile
*sfile
;
1637 struct coff_symbol
*sym
;
1640 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1642 if (sym
->visible
->type
== coff_vis_ext_ref
)
1645 er
.type
= ER_NOTSPEC
;
1646 er
.name
= sym
->name
;
1647 sysroff_swap_er_out (file
, &er
);
1648 sym
->er_number
= idx
++;
1656 wr_ed (ptr
, sfile
, first
)
1657 struct coff_ofile
*ptr
;
1658 struct coff_file
*sfile
;
1661 struct coff_symbol
*s
;
1664 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1666 if (s
->visible
->type
== coff_vis_ext_def
1667 || s
->visible
->type
== coff_vis_common
)
1671 ed
.section
= s
->where
->section
->number
;
1673 if (s
->where
->section
->data
)
1675 ed
.type
= ED_TYPE_DATA
;
1677 else if (s
->where
->section
->code
& SEC_CODE
)
1679 ed
.type
= ED_TYPE_ENTRY
;
1683 ed
.type
= ED_TYPE_NOTSPEC
;
1684 ed
.type
= ED_TYPE_DATA
;
1686 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1688 sysroff_swap_ed_out (file
, &ed
);
1696 struct coff_ofile
*ptr
;
1698 struct coff_sfile
*sfile
;
1700 for (sfile
= ptr
->source_head
;
1702 sfile
= sfile
->next
)
1704 wr_un (ptr
, sfile
, first
);
1705 wr_sc (ptr
, sfile
, first
);
1706 wr_er (ptr
, sfile
, first
);
1707 wr_ed (ptr
, sfile
, first
);
1714 struct coff_ofile
*p
;
1728 return (x
+ 3) & ~3;
1731 /* Find all the common variables and turn them into
1732 ordinary defs - dunno why, but thats what hitachi does with 'em */
1736 struct coff_ofile
*tree
;
1738 struct coff_symbol
*s
;
1739 struct coff_section
*common_section
;
1740 /* Find the common section - always section 3 */
1741 common_section
= tree
->sections
+ 3;
1742 for (s
= tree
->symbol_list_head
;
1744 s
= s
->next_in_ofile_list
)
1746 if (s
->visible
->type
== coff_vis_common
)
1748 struct coff_where
*w
= s
->where
;
1749 /* s->visible->type = coff_vis_ext_def; leave it as common */
1750 common_section
->size
= align (common_section
->size
);
1751 w
->offset
= common_section
->size
+ common_section
->address
;
1752 w
->section
= common_section
;
1753 common_section
->size
+= s
->type
->size
;
1754 common_section
->size
= align (common_section
->size
);
1762 show_usage (file
, status
)
1766 fprintf (file
, "Usage: %s [-dhVq] in-file [out-file]\n", program_name
);
1773 printf ("%s: Convert a COFF object file into a SYSROFF object file\n",
1775 show_usage (stdout
, 0);
1786 static struct option long_options
[] =
1788 {"debug", no_argument
, 0, 'd'},
1789 {"quick", no_argument
, 0, 'q'},
1790 {"help", no_argument
, 0, 'h'},
1791 {"version", no_argument
, 0, 'V'},
1792 {NULL
, no_argument
, 0, 0}
1798 program_name
= av
[0];
1799 xmalloc_set_program_name (program_name
);
1801 while ((opt
= getopt_long (ac
, av
, "dhVq", long_options
,
1817 printf ("GNU %s version %s\n", program_name
, PROGRAM_VERSION
);
1823 show_usage (stderr
, 1);
1828 /* The input and output files may be named on the command line. */
1832 input_file
= av
[optind
];
1836 output_file
= av
[optind
];
1839 show_usage (stderr
, 1);
1840 if (strcmp (input_file
, output_file
) == 0)
1843 "%s: input and output files must be different\n",
1852 fprintf (stderr
, "%s: no input file specified\n",
1859 /* Take a .o off the input file and stick on a .obj. If
1860 it doesn't end in .o, then stick a .obj on anyway */
1862 int len
= strlen (input_file
);
1863 output_file
= xmalloc (len
+ 5);
1864 strcpy (output_file
, input_file
);
1866 && output_file
[len
- 2] == '.'
1867 && output_file
[len
- 1] == 'o')
1869 output_file
[len
] = 'b';
1870 output_file
[len
+ 1] = 'j';
1871 output_file
[len
+ 2] = 0;
1875 strcat (output_file
, ".obj");
1879 abfd
= bfd_openr (input_file
, 0);
1882 bfd_fatal (input_file
);
1884 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1886 bfd_nonfatal (input_file
);
1887 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1889 list_matching_formats (matching
);
1895 file
= fopen (output_file
, "w");
1899 fprintf (stderr
, "%s: unable to open output file %s\n",
1900 program_name
, output_file
);
1904 tree
= coff_grok (abfd
);