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>
36 #include "coff/internal.h"
37 #include "../bfd/libcoff.h"
39 #define PROGRAM_VERSION "1.5"
45 static void walk_tree_scope ();
46 static void wr_globals ();
47 static int find_base ();
54 static struct coff_ofile
*tree
;
56 static int absolute_p
;
57 static int segmented_p
;
60 static int ids1
[20000];
61 static int ids2
[20000];
63 static int base1
= 0x18;
64 static int base2
= 0x2018;
71 char *r
= xmalloc (a
* b
);
100 section_translate (n
)
103 if (strcmp (n
, ".text") == 0)
105 if (strcmp (n
, ".data") == 0)
107 if (strcmp (n
, ".bss") == 0)
114 #define DATE "940201073000"; /* Just a time on my birthday */
124 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
126 res
= (char *) xmalloc (i
+ 1);
127 memcpy (res
, name
, i
);
133 /* IT LEN stuff CS */
135 checksum (file
, ptr
, size
, code
)
144 int bytes
= size
/ 8;
145 last
= !(code
& 0xff00);
148 ptr
[0] = code
| (last
? 0x80 : 0);
151 for (j
= 0; j
< bytes
; j
++)
155 /* Glue on a checksum too */
157 fwrite (ptr
, bytes
+ 1, 1, file
);
164 writeINT (n
, ptr
, idx
, size
, file
)
180 /* Lets write out that record and do another one */
181 checksum (file
, ptr
, *idx
, code
| 0x1000);
193 ptr
[byte
+ 0] = n
>> 8;
197 ptr
[byte
+ 0] = n
>> 24;
198 ptr
[byte
+ 1] = n
>> 16;
199 ptr
[byte
+ 2] = n
>> 8;
200 ptr
[byte
+ 3] = n
>> 0;
210 writeBITS (val
, ptr
, idx
, size
)
222 /* Turn off all about to change bits */
223 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
224 /* Turn on the bits we want */
225 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
230 writeBARRAY (data
, ptr
, idx
, size
, file
)
238 writeINT (data
.len
, ptr
, idx
, 1, file
);
239 for (i
= 0; i
< data
.len
; i
++)
241 writeINT (data
.data
[i
], ptr
, idx
, 1, file
);
249 writeCHARS (string
, ptr
, idx
, size
, file
)
260 /* Lets write out that record and do another one */
261 checksum (file
, ptr
, *idx
, code
| 0x1000);
268 /* Variable length string */
269 size
= strlen (string
);
274 memcpy (ptr
+ i
, string
, size
);
279 #define SYSROFF_SWAP_OUT
283 static char *rname_sh
[] =
285 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
288 static char *rname_h8300
[] =
290 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
297 sysroff_swap_tr_out (file
, &t
);
301 wr_un (ptr
, sfile
, first
)
302 struct coff_ofile
*ptr
;
303 struct coff_sfile
*sfile
;
308 struct coff_symbol
*s
;
312 if (abfd
->flags
& EXEC_P
)
313 un
.format
= FORMAT_LM
;
315 un
.format
= FORMAT_OM
;
317 un
.nsections
= ptr
->nsections
- 1; /* Don't count the abs section */
320 /* Count all the undefined and defined variables with global scope */
324 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
326 if (s
->visible
->type
== coff_vis_ext_def
327 || s
->visible
->type
== coff_vis_common
)
330 if (s
->visible
->type
== coff_vis_ext_ref
)
340 un
.tool
= "C_H8/300H";
343 un
.linker
= "L_GX00";
345 un
.name
= sfile
->name
;
346 sysroff_swap_un_out (file
, &un
);
352 struct coff_ofile
*p
;
357 if (abfd
->flags
& 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 (abfd
->arch_info
->arch
)
396 if (!abfd
->flags
& EXEC_P
)
406 hd
.address
= bfd_get_start_address (abfd
);
411 hd
.mn
= strip_suffix (abfd
->filename
);
414 sysroff_swap_hd_out (file
, &hd
);
420 struct coff_ofile
*p
;
421 struct coff_section
*sec
;
425 sh
.section
= sec
->number
;
429 sysroff_swap_sh_out (file
, &sh
);
435 struct coff_ofile
*p
;
436 struct coff_section
*section
;
440 unsigned char stuff
[200];
443 while (i
< section
->size
)
446 int todo
= 200; /* Copy in 200 byte lumps */
448 if (i
+ todo
> section
->size
)
449 todo
= section
->size
- i
;
454 if (abfd
->flags
& EXEC_P
)
455 ob
.address
= section
->address
;
466 ob
.cpf
= 0; /* Never compress */
468 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
469 ob
.data
.data
= stuff
;
470 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size*/ );
477 struct coff_ofile
*ptr
;
478 struct coff_section
*sec
;
480 int nr
= sec
->nrelocs
;
482 for (i
= 0; i
< nr
; i
++)
484 struct coff_reloc
*r
= sec
->relocs
+ i
;
485 struct coff_symbol
*ref
;
494 rl
.flen
= 32; /* SH Specific */
495 /* What sort of reloc ? Look in the section to find out */
497 if (ref
->visible
->type
== coff_vis_ext_ref
)
499 rl
.bcount
= 4; /* Always 4 for us */
501 rl
.symn
= ref
->er_number
;
503 else if (ref
->visible
->type
== coff_vis_common
)
505 rl
.bcount
= 11; /* Always 11 for us */
507 rl
.secn
= ref
->where
->section
->number
;
510 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
511 rl
.aopcode_is_0x20
= 0x20;
516 rl
.bcount
= 11; /* Always 11 for us */
518 rl
.secn
= ref
->where
->section
->number
;
521 rl
.addend
= -ref
->where
->section
->address
;
522 rl
.aopcode_is_0x20
= 0x20;
525 if (rl
.op
== OP_SEC_REF
526 || rl
.op
== OP_EXT_REF
)
528 sysroff_swap_rl_out (file
, &rl
);
535 struct coff_ofile
*p
;
538 for (i
= 1; i
< p
->nsections
; i
++)
540 wr_sh (p
, p
->sections
+ i
);
541 wr_ob (p
, p
->sections
+ i
);
542 wr_rl (p
, p
->sections
+ i
);
547 wr_dps_start (sfile
, section
, scope
, type
, nest
)
548 struct coff_sfile
*sfile
;
549 struct coff_section
*section
;
550 struct coff_scope
*scope
;
560 dps
.san
= scope
->sec
->number
;
561 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
562 dps
.block_size
= scope
->size
;
565 printf ("DPS %s %d %x\n",
581 sysroff_swap_dps_out (file
, &dps
);
585 wr_dps_end (section
, scope
, type
)
586 struct coff_section
*section
;
587 struct coff_scope
*scope
;
593 sysroff_swap_dps_out (file
, &dps
);
600 return (int *) (xcalloc (sizeof (int), x
));
603 static void walk_tree_symbol ();
605 walk_tree_type_1 (sfile
, symbol
, type
, nest
)
606 struct coff_sfile
*sfile
;
607 struct coff_symbol
*symbol
;
608 struct coff_type
*type
;
613 case coff_secdef_type
:
614 case coff_basic_type
:
618 switch (type
->u
.basic
)
622 dbt
.btype
= BTYPE_VOID
;
623 dbt
.sign
= BTYPE_UNSPEC
;
624 dbt
.fptype
= FPTYPE_NOTSPEC
;
627 dbt
.btype
= BTYPE_CHAR
;
628 dbt
.sign
= BTYPE_UNSPEC
;
629 dbt
.fptype
= FPTYPE_NOTSPEC
;
634 dbt
.btype
= BTYPE_INT
;
635 dbt
.sign
= SIGN_SIGNED
;
636 dbt
.fptype
= FPTYPE_NOTSPEC
;
639 dbt
.btype
= BTYPE_FLOAT
;
640 dbt
.fptype
= FPTYPE_SINGLE
;
643 dbt
.btype
= BTYPE_FLOAT
;
644 dbt
.fptype
= FPTYPE_DOUBLE
;
647 dbt
.btype
= BTYPE_FLOAT
;
648 dbt
.fptype
= FPTYPE_EXTENDED
;
651 dbt
.btype
= BTYPE_CHAR
;
652 dbt
.sign
= SIGN_UNSIGNED
;
653 dbt
.fptype
= FPTYPE_NOTSPEC
;
658 dbt
.btype
= BTYPE_INT
;
659 dbt
.sign
= SIGN_UNSIGNED
;
660 dbt
.fptype
= FPTYPE_NOTSPEC
;
663 dbt
.bitsize
= type
->size
;
665 sysroff_swap_dbt_out (file
, &dbt
);
668 case coff_pointer_type
:
671 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
673 sysroff_swap_dpt_out (file
, &dpt
);
677 case coff_function_type
:
680 struct coff_symbol
*param
;
683 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
686 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
688 sysroff_swap_dfp_out (file
, &dfp
);
690 for (param
= type
->u
.function
.parameters
->vars_head
;
694 walk_tree_symbol (sfile
, 0, param
, nest
);
697 sysroff_swap_dfp_out (file
, &dfp
);
701 case coff_structdef_type
:
705 struct coff_symbol
*member
;
707 dbt
.btype
= BTYPE_STRUCT
;
708 dbt
.bitsize
= type
->size
;
709 dbt
.sign
= SIGN_UNSPEC
;
710 dbt
.fptype
= FPTYPE_NOTSPEC
;
711 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
713 sysroff_swap_dbt_out (file
, &dbt
);
716 sysroff_swap_dds_out (file
, &dds
);
717 for (member
= type
->u
.astructdef
.elements
->vars_head
;
719 member
= member
->next
)
721 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
725 sysroff_swap_dds_out (file
, &dds
);
729 case coff_structref_type
:
732 dbt
.btype
= BTYPE_TAG
;
733 dbt
.bitsize
= type
->size
;
734 dbt
.sign
= SIGN_UNSPEC
;
735 dbt
.fptype
= FPTYPE_NOTSPEC
;
736 if (type
->u
.astructref
.ref
)
738 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
746 sysroff_swap_dbt_out (file
, &dbt
);
749 case coff_array_type
:
753 int dims
= 1; /* Only output one dimension at a time */
755 dar
.variable
= nints (dims
);
756 dar
.subtype
= nints (dims
);
757 dar
.spare
= nints (dims
);
758 dar
.max_variable
= nints (dims
);
759 dar
.maxspare
= nints (dims
);
760 dar
.max
= nints (dims
);
761 dar
.min_variable
= nints (dims
);
762 dar
.min
= nints (dims
);
763 dar
.minspare
= nints (dims
);
765 dar
.length
= type
->size
/ type
->u
.array
.dim
;
766 for (j
= 0; j
< dims
; j
++)
768 dar
.variable
[j
] = VARIABLE_FIXED
;
769 dar
.subtype
[j
] = SUB_INTEGER
;
771 dar
.max_variable
[j
] = 0;
772 dar
.max
[j
] = type
->u
.array
.dim
;
773 dar
.min_variable
[j
] = 0;
774 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
776 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
777 sysroff_swap_dar_out (file
, &dar
);
780 case coff_enumdef_type
:
784 struct coff_symbol
*member
;
785 dbt
.btype
= BTYPE_ENUM
;
786 dbt
.bitsize
= type
->size
;
787 dbt
.sign
= SIGN_UNSPEC
;
788 dbt
.fptype
= FPTYPE_NOTSPEC
;
789 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
791 sysroff_swap_dbt_out (file
, &dbt
);
796 sysroff_swap_den_out (file
, &den
);
797 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
799 member
= member
->next
)
801 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
805 sysroff_swap_den_out (file
, &den
);
810 case coff_enumref_type
:
813 dbt
.btype
= BTYPE_TAG
;
814 dbt
.bitsize
= type
->size
;
815 dbt
.sign
= SIGN_UNSPEC
;
816 dbt
.fptype
= FPTYPE_NOTSPEC
;
817 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
819 sysroff_swap_dbt_out (file
, &dbt
);
834 sysroff_swap_dty_out (file
, &dty
);
844 sysroff_swap_dty_out (file
, &dty
);
849 dump_tree_structure (sfile
, symbol
, type
, nest
)
850 struct coff_sfile
*sfile
;
851 struct coff_symbol
*symbol
;
852 struct coff_type
*type
;
855 if (symbol
->type
->type
== coff_function_type
)
865 walk_tree_type (sfile
, symbol
, type
, nest
)
869 struct coff_symbol
*symbol
;
870 struct coff_type
*type
;
873 if (symbol
->type
->type
== coff_function_type
)
880 sysroff_swap_dty_out (file
, &dty
);
881 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
883 sysroff_swap_dty_out (file
, &dty
);
886 symbol
->where
->section
,
887 symbol
->type
->u
.function
.code
,
888 BLOCK_TYPE_FUNCTION
, nest
);
889 wr_dps_start (sfile
, symbol
->where
->section
,
890 symbol
->type
->u
.function
.code
,
891 BLOCK_TYPE_BLOCK
, nest
);
892 walk_tree_scope (symbol
->where
->section
,
894 symbol
->type
->u
.function
.code
,
895 nest
+ 1, BLOCK_TYPE_BLOCK
);
897 wr_dps_end (symbol
->where
->section
,
898 symbol
->type
->u
.function
.code
,
900 wr_dps_end (symbol
->where
->section
,
901 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
909 sysroff_swap_dty_out (file
, &dty
);
910 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
912 sysroff_swap_dty_out (file
, &dty
);
920 walk_tree_symbol (sfile
, section
, symbol
, nest
)
921 struct coff_sfile
*sfile
;
922 struct coff_section
*section
;
923 struct coff_symbol
*symbol
;
931 switch (symbol
->type
->type
)
933 case coff_function_type
:
934 dsy
.type
= STYPE_FUNC
;
937 case coff_structref_type
:
938 case coff_pointer_type
:
939 case coff_array_type
:
940 case coff_basic_type
:
941 case coff_enumref_type
:
942 dsy
.type
= STYPE_VAR
;
945 case coff_enumdef_type
:
946 dsy
.type
= STYPE_TAG
;
950 case coff_structdef_type
:
951 dsy
.type
= STYPE_TAG
;
953 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
955 case coff_secdef_type
:
961 if (symbol
->where
->where
== coff_where_member_of_struct
)
964 dsy
.type
= STYPE_MEMBER
;
966 if (symbol
->where
->where
== coff_where_member_of_enum
)
968 dsy
.type
= STYPE_ENUM
;
971 dsy
.value
= symbol
->where
->offset
;
974 if (symbol
->type
->type
== coff_structdef_type
975 || symbol
->where
->where
== coff_where_entag
976 || symbol
->where
->where
== coff_where_strtag
)
978 dsy
.snumber
= get_member_id (symbol
->number
);
982 dsy
.snumber
= get_ordinary_id (symbol
->number
);
986 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
988 switch (symbol
->visible
->type
)
990 case coff_vis_common
:
991 case coff_vis_ext_def
:
992 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
994 case coff_vis_ext_ref
:
995 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
997 case coff_vis_int_def
:
998 dsy
.ainfo
= AINFO_STATIC_INT
;
1001 case coff_vis_autoparam
:
1002 dsy
.ainfo
= AINFO_AUTO
;
1004 case coff_vis_register
:
1005 case coff_vis_regparam
:
1006 dsy
.ainfo
= AINFO_REG
;
1010 case coff_vis_member_of_struct
:
1011 case coff_vis_member_of_enum
:
1017 dsy
.dlength
= symbol
->type
->size
;
1018 switch (symbol
->where
->where
)
1020 case coff_where_memory
:
1022 dsy
.section
= symbol
->where
->section
->number
;
1027 case coff_where_member_of_struct
:
1028 case coff_where_member_of_enum
:
1029 case coff_where_stack
:
1030 case coff_where_register
:
1031 case coff_where_unknown
:
1032 case coff_where_strtag
:
1034 case coff_where_entag
:
1035 case coff_where_typedef
:
1041 switch (symbol
->where
->where
)
1043 case coff_where_memory
:
1044 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1046 case coff_where_stack
:
1047 dsy
.address
= symbol
->where
->offset
;
1049 case coff_where_member_of_struct
:
1052 if (symbol
->where
->bitsize
)
1054 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1056 dsy
.field_len
= symbol
->where
->bitsize
;
1057 dsy
.field_off
= (bits
/ 32) * 4;
1058 dsy
.field_bitoff
= bits
% 32;
1064 dsy
.field_len
= symbol
->type
->size
;
1065 dsy
.field_off
= symbol
->where
->offset
;
1068 case coff_where_member_of_enum
:
1070 dsy.field_len = symbol->type->size;
1071 dsy.field_off = symbol->where->offset;*/
1073 case coff_where_register
:
1074 case coff_where_unknown
:
1075 case coff_where_strtag
:
1077 case coff_where_entag
:
1078 case coff_where_typedef
:
1084 if (symbol
->where
->where
== coff_where_register
)
1087 dsy
.reg
= rname_sh
[symbol
->where
->offset
];
1089 dsy
.reg
= rname_h8300
[symbol
->where
->offset
];
1092 switch (symbol
->visible
->type
)
1094 case coff_vis_common
:
1095 /* We do this 'cause common C symbols are treated as extdefs */
1096 case coff_vis_ext_def
:
1097 case coff_vis_ext_ref
:
1099 dsy
.ename
= symbol
->name
;
1102 case coff_vis_regparam
:
1103 case coff_vis_autoparam
:
1104 dsy
.type
= STYPE_PARAMETER
;
1107 case coff_vis_int_def
:
1110 case coff_vis_register
:
1112 case coff_vis_member_of_struct
:
1113 case coff_vis_member_of_enum
:
1125 sysroff_swap_dsy_out (file
, &dsy
);
1127 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1132 walk_tree_scope (section
, sfile
, scope
, nest
, type
)
1133 struct coff_section
*section
;
1134 struct coff_sfile
*sfile
;
1135 struct coff_scope
*scope
;
1139 struct coff_symbol
*vars
;
1140 struct coff_scope
*child
;
1142 if (scope
->vars_head
1143 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1145 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1148 wr_globals (tree
, sfile
, nest
+ 1);
1150 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1152 walk_tree_symbol (sfile
, section
, vars
, nest
);
1155 for (child
= scope
->list_head
; child
; child
= child
->next
)
1157 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1160 wr_dps_end (section
, scope
, type
);
1164 walk_tree_sfile (section
, sfile
)
1165 struct coff_section
*section
;
1166 struct coff_sfile
*sfile
;
1168 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1173 wr_program_structure (p
, sfile
)
1174 struct coff_ofile
*p
;
1175 struct coff_sfile
*sfile
;
1178 walk_tree_sfile (p
->sections
+ 4, sfile
);
1184 struct coff_ofile
*p
;
1185 struct coff_sfile
*sfile
;
1191 struct coff_symbol
*symbol
;
1192 static int incit
= 0x500000;
1197 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1198 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1200 du
.format
= abfd
->flags
& EXEC_P
? 0 : 1;
1203 du
.sections
= p
->nsections
- 1;
1204 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1205 du
.address
= nints (du
.sections
);
1206 du
.length
= nints (du
.sections
);
1208 for (i
= 0; i
< du
.sections
; i
++)
1214 /* Look through all the symbols and try and work out the extents in this
1217 for (symbol
= sfile
->scope
->vars_head
;
1219 symbol
= symbol
->next
)
1221 if (symbol
->type
->type
== coff_secdef_type
)
1223 unsigned int low
= symbol
->where
->offset
;
1224 unsigned int high
= symbol
->where
->offset
+ symbol
->type
->size
- 1;
1225 struct coff_section
*section
= symbol
->where
->section
;
1227 int sn
= section
->number
;
1228 if (low
< lowest
[sn
])
1230 if (high
> highest
[sn
])
1236 for (i
= 0; i
< du
.sections
; i
++)
1238 if (highest
[i
] == 0)
1240 lowest
[i
] = highest
[i
] = incit
;
1243 du
.length
[used
] = highest
[i
] - lowest
[i
];
1244 du
.address
[used
] = abfd
->flags
& EXEC_P
? lowest
[i
] : 0;
1247 printf (" section %6s 0x%08x..0x%08x\n",
1248 p
->sections
[i
+ 1].name
,
1257 for (j
= 0; j
< lim
; j
++)
1262 if (sfile
->section
[src
].init
)
1265 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1267 = sfile
->section
[src
].low
;
1272 du
.address
[dst
] = 0;
1276 if (sfile
->section
[src
].parent
)
1278 printf (" section %6s 0x%08x..0x%08x\n",
1279 sfile
->section
[src
].parent
->name
,
1281 du
.address
[dst
] + du
.length
[dst
] - 1);
1284 du
.sections
= dst
+ 1;
1290 sysroff_swap_du_out (file
, &du
);
1295 struct coff_ofile
*p
;
1296 struct coff_sfile
*sfile
;
1302 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1303 dus
.drb
= nints (dus
.ns
);
1304 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1305 dus
.spare
= nints (dus
.ns
);
1307 /* Find the filenames */
1311 for (sfile
= p
->source_head
;
1313 sfile
= sfile
->next
)
1317 dus
.fname
[i
] = sfile
->name
;
1322 dus
.fname
[0] = sfile
->name
;
1325 sysroff_swap_dus_out (file
, &dus
);
1329 /* Find the offset of the .text section for this sfile in the
1330 .text section for the output file */
1333 find_base (sfile
, section
)
1334 struct coff_sfile
*sfile
;
1335 struct coff_section
*section
;
1337 return sfile
->section
[section
->number
].low
;
1340 wr_dln (p
, sfile
, n
)
1341 struct coff_ofile
*p
;
1342 struct coff_sfile
*sfile
;
1349 /* Count up all the linenumbers */
1350 struct coff_symbol
*sy
;
1356 for (sy
= p
->symbol_list_head
;
1358 sy
= sy
->next_in_ofile_list
)
1360 struct coff_type
*t
= sy
->type
;
1361 if (t
->type
== coff_function_type
)
1363 struct coff_line
*l
= t
->u
.function
.lines
;
1368 dln
.sfn
= nints (lc
);
1369 dln
.sln
= nints (lc
);
1370 dln
.lln
= nints (lc
);
1371 dln
.section
= nints (lc
);
1373 dln
.from_address
= nints (lc
);
1374 dln
.to_address
= nints (lc
);
1381 /* Run through once more and fill up the structure */
1383 for (sy
= p
->symbol_list_head
;
1385 sy
= sy
->next_in_ofile_list
)
1387 if (sy
->type
->type
== coff_function_type
)
1390 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1391 for (i
= 0; i
< l
->nlines
; i
++)
1393 dln
.section
[idx
] = sy
->where
->section
->number
;
1395 dln
.sln
[idx
] = l
->lines
[i
];
1396 dln
.from_address
[idx
] = l
->addresses
[i
];
1398 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1404 sysroff_swap_dln_out (file
, &dln
);
1409 /* Count up all the linenumbers */
1411 struct coff_symbol
*sy
;
1417 for (sy
= sfile
->scope
->vars_head
;
1421 struct coff_type
*t
= sy
->type
;
1422 if (t
->type
== coff_function_type
)
1424 struct coff_line
*l
= t
->u
.function
.lines
;
1430 dln
.sfn
= nints (lc
);
1431 dln
.sln
= nints (lc
);
1432 dln
.cc
= nints (lc
);
1433 dln
.section
= nints (lc
);
1435 dln
.from_address
= nints (lc
);
1436 dln
.to_address
= nints (lc
);
1443 /* Run through once more and fill up the structure */
1445 for (sy
= sfile
->scope
->vars_head
;
1449 if (sy
->type
->type
== coff_function_type
)
1452 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1455 int base
= find_base (sfile
, sy
->where
->section
);
1456 for (i
= 0; i
< l
->nlines
; i
++)
1458 dln
.section
[idx
] = sy
->where
->section
->number
;
1460 dln
.sln
[idx
] = l
->lines
[i
];
1461 dln
.from_address
[idx
] =
1462 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1465 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1469 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1474 sysroff_swap_dln_out (file
, &dln
);
1478 /* Write the global symbols out to the debug info */
1480 wr_globals (p
, sfile
, n
)
1481 struct coff_ofile
*p
;
1482 struct coff_sfile
*sfile
;
1485 struct coff_symbol
*sy
;
1486 for (sy
= p
->symbol_list_head
;
1488 sy
= sy
->next_in_ofile_list
)
1490 if (sy
->visible
->type
== coff_vis_ext_def
1491 || sy
->visible
->type
== coff_vis_ext_ref
)
1493 /* Only write out symbols if they belong to
1494 the current source file */
1495 if (sy
->sfile
== sfile
)
1496 walk_tree_symbol (sfile
, 0, sy
, 0);
1504 struct coff_ofile
*p
;
1506 struct coff_sfile
*sfile
;
1508 for (sfile
= p
->source_head
;
1510 sfile
= sfile
->next
)
1515 printf ("%s\n", sfile
->name
);
1517 wr_du (p
, sfile
, n
);
1519 wr_program_structure (p
, sfile
);
1520 wr_dln (p
, sfile
, n
);
1527 /* It seems that the CS struct is not normal - the size is wrong
1528 heres one I prepared earlier.. */
1530 {0x80, 0x21, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x00, 0x80, 0x80, 0x80,
1531 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x80, 0x00,
1532 0x80, 0x80, 0x80, 0x80, 0x80, 0x00, 0x00, 0x00, 0xde};
1533 fwrite (b
, 1, sizeof (b
), file
);
1536 /* Write out the SC records for a unit. Create an SC
1537 for all the sections which appear in the output file, even
1538 if there isn't an equivalent one on the input */
1542 struct coff_ofile
*ptr
;
1543 struct coff_sfile
*sfile
;
1546 /* First work out the total number of sections */
1548 int total_sec
= ptr
->nsections
;
1552 struct coff_section
*sec
;
1553 struct coff_symbol
*symbol
;
1555 struct coff_symbol
*symbol
;
1558 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1562 for (i
= 0; i
< total_sec
; i
++)
1564 info
[i
].sec
= ptr
->sections
+ i
;
1568 for (symbol
= sfile
->scope
->vars_head
;
1570 symbol
= symbol
->next
)
1573 if (symbol
->type
->type
== coff_secdef_type
)
1575 for (i
= 0; i
< total_sec
; i
++)
1577 if (symbol
->where
->section
== info
[i
].sec
)
1579 info
[i
].symbol
= symbol
;
1586 /* Now output all the section info, and fake up some stuff for sections
1589 for (i
= 1; i
< total_sec
; i
++)
1593 symbol
= info
[i
].symbol
;
1598 /* Don't have a symbol set aside for this section, which means that nothing
1599 in this file does anything for the section. */
1600 sc
.format
= !(abfd
->flags
& EXEC_P
);
1603 name
= info
[i
].sec
->name
;
1607 if (abfd
->flags
& EXEC_P
)
1610 sc
.addr
= symbol
->where
->offset
;
1617 sc
.length
= symbol
->type
->size
;
1618 name
= symbol
->name
;
1623 sc
.concat
= CONCAT_SIMPLE
;
1631 sc
.spare1
= 0; /* If not zero, then it doesn't work */
1632 sc
.name
= section_translate (name
);
1633 if (strlen (sc
.name
) == 1)
1639 sc
.contents
= CONTENTS_DATA
;
1642 sc
.contents
= CONTENTS_CODE
;
1647 sc
.contents
= CONTENTS_CODE
;
1651 sysroff_swap_sc_out (file
, &sc
);
1660 wr_er (ptr
, sfile
, first
)
1661 struct coff_ofile
*ptr
;
1662 struct coff_sfile
*sfile
;
1666 struct coff_symbol
*sym
;
1669 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1671 if (sym
->visible
->type
== coff_vis_ext_ref
)
1675 er
.type
= ER_NOTSPEC
;
1676 er
.name
= sym
->name
;
1677 sysroff_swap_er_out (file
, &er
);
1678 sym
->er_number
= idx
++;
1686 wr_ed (ptr
, sfile
, first
)
1687 struct coff_ofile
*ptr
;
1688 struct coff_sfile
*sfile
;
1691 struct coff_symbol
*s
;
1694 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1696 if (s
->visible
->type
== coff_vis_ext_def
1697 || s
->visible
->type
== coff_vis_common
)
1701 ed
.section
= s
->where
->section
->number
;
1703 if (s
->where
->section
->data
)
1705 ed
.type
= ED_TYPE_DATA
;
1707 else if (s
->where
->section
->code
& SEC_CODE
)
1709 ed
.type
= ED_TYPE_ENTRY
;
1713 ed
.type
= ED_TYPE_NOTSPEC
;
1714 ed
.type
= ED_TYPE_DATA
;
1716 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1718 sysroff_swap_ed_out (file
, &ed
);
1726 struct coff_ofile
*ptr
;
1728 struct coff_sfile
*sfile
;
1730 for (sfile
= ptr
->source_head
;
1732 sfile
= sfile
->next
)
1734 wr_un (ptr
, sfile
, first
);
1736 wr_er (ptr
, sfile
, first
);
1737 wr_ed (ptr
, sfile
, first
);
1744 struct coff_ofile
*p
;
1758 return (x
+ 3) & ~3;
1761 /* Find all the common variables and turn them into
1762 ordinary defs - dunno why, but thats what hitachi does with 'em */
1766 struct coff_ofile
*tree
;
1768 struct coff_symbol
*s
;
1769 struct coff_section
*common_section
;
1770 /* Find the common section - always section 3 */
1771 common_section
= tree
->sections
+ 3;
1772 for (s
= tree
->symbol_list_head
;
1774 s
= s
->next_in_ofile_list
)
1776 if (s
->visible
->type
== coff_vis_common
)
1778 struct coff_where
*w
= s
->where
;
1779 /* s->visible->type = coff_vis_ext_def; leave it as common */
1780 common_section
->size
= align (common_section
->size
);
1781 w
->offset
= common_section
->size
+ common_section
->address
;
1782 w
->section
= common_section
;
1783 common_section
->size
+= s
->type
->size
;
1784 common_section
->size
= align (common_section
->size
);
1792 show_usage (file
, status
)
1796 fprintf (file
, "Usage: %s [-dhVq] in-file [out-file]\n", program_name
);
1803 printf ("%s: Convert a COFF object file into a SYSROFF object file\n",
1805 show_usage (stdout
, 0);
1816 static struct option long_options
[] =
1818 {"debug", no_argument
, 0, 'd'},
1819 {"quick", no_argument
, 0, 'q'},
1820 {"help", no_argument
, 0, 'h'},
1821 {"version", no_argument
, 0, 'V'},
1822 {NULL
, no_argument
, 0, 0}
1828 program_name
= av
[0];
1829 xmalloc_set_program_name (program_name
);
1831 while ((opt
= getopt_long (ac
, av
, "dhVq", long_options
,
1847 printf ("GNU %s version %s\n", program_name
, PROGRAM_VERSION
);
1853 show_usage (stderr
, 1);
1858 /* The input and output files may be named on the command line. */
1862 input_file
= av
[optind
];
1866 output_file
= av
[optind
];
1869 show_usage (stderr
, 1);
1870 if (strcmp (input_file
, output_file
) == 0)
1873 "%s: input and output files must be different\n",
1884 fprintf (stderr
, "%s: no input file specified\n",
1891 /* Take a .o off the input file and stick on a .obj. If
1892 it doesn't end in .o, then stick a .obj on anyway */
1894 int len
= strlen (input_file
);
1895 output_file
= xmalloc (len
+ 5);
1896 strcpy (output_file
, input_file
);
1898 && output_file
[len
- 2] == '.'
1899 && output_file
[len
- 1] == 'o')
1901 output_file
[len
] = 'b';
1902 output_file
[len
+ 1] = 'j';
1903 output_file
[len
+ 2] = 0;
1907 strcat (output_file
, ".obj");
1911 abfd
= bfd_openr (input_file
, 0);
1914 bfd_fatal (input_file
);
1916 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1918 bfd_nonfatal (input_file
);
1919 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1921 list_matching_formats (matching
);
1927 file
= fopen (output_file
, "wb");
1931 fprintf (stderr
, "%s: unable to open output file %s\n",
1932 program_name
, output_file
);
1937 printf ("ids %d %d\n", base1
, base2
);
1938 tree
= coff_grok (abfd
);