1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994-2024 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 3 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., 51 Franklin Street - Fifth Floor, 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 */
33 #include "libiberty.h"
34 #include "filenames.h"
37 #include "coff/internal.h"
38 #include "../bfd/libcoff.h"
43 static char *toolname
;
46 static void walk_tree_symbol
47 (struct coff_sfile
*, struct coff_section
*, struct coff_symbol
*, int);
48 static void walk_tree_scope
49 (struct coff_section
*, struct coff_sfile
*, struct coff_scope
*, int, int);
50 static int find_base (struct coff_sfile
*, struct coff_section
*);
51 static void wr_globals (struct coff_ofile
*, struct coff_sfile
*, int);
57 static int noprescan
= 0;
58 static struct coff_ofile
*tree
;
60 static int absolute_p;
63 static int segmented_p
;
66 static int ids1
[20000];
67 static int ids2
[20000];
69 static int base1
= 0x18;
70 static int base2
= 0x2018;
83 get_ordinary_id (int x
)
92 section_translate (char *n
)
94 if (strcmp (n
, ".text") == 0)
96 if (strcmp (n
, ".data") == 0)
98 if (strcmp (n
, ".bss") == 0)
103 #define DATE "940201073000"; /* Just a time on my birthday */
106 strip_suffix (const char *name
)
111 for (i
= 0; name
[i
] != 0 && name
[i
] != '.'; i
++)
113 res
= (char *) xmalloc (i
+ 1);
114 memcpy (res
, name
, i
);
119 /* IT LEN stuff CS */
121 checksum (FILE *ffile
, unsigned char *ptr
, int size
, int ccode
)
126 int bytes
= size
/ 8;
128 last
= !(ccode
& 0xff00);
130 fatal (_("Checksum failure"));
132 ptr
[0] = ccode
| (last
? 0x80 : 0);
135 for (j
= 0; j
< bytes
; j
++)
138 /* Glue on a checksum too. */
140 if (fwrite (ptr
, bytes
+ 1, 1, ffile
) != 1)
141 /* FIXME: Return error status. */
142 fatal (_("Failed to write checksum"));
147 writeINT (int n
, unsigned char *ptr
, int *idx
, int size
, FILE *ffile
)
158 /* Lets write out that record and do another one. */
159 checksum (ffile
, ptr
, *idx
, code
| 0x1000);
172 ptr
[byte
+ 0] = n
>> 8;
176 ptr
[byte
+ 0] = n
>> 24;
177 ptr
[byte
+ 1] = n
>> 16;
178 ptr
[byte
+ 2] = n
>> 8;
179 ptr
[byte
+ 3] = n
>> 0;
182 fatal (_("Unsupported integer write size: %d"), size
);
188 writeBITS (int val
, unsigned char *ptr
, int *idx
, int size
)
197 /* Turn off all about to change bits. */
198 old
&= ~((~0 >> (8 - bit
- size
)) & ((1 << size
) - 1));
199 /* Turn on the bits we want. */
200 old
|= (val
& ((1 << size
) - 1)) << (8 - bit
- size
);
205 writeBARRAY (barray data
, unsigned char *ptr
, int *idx
,
206 int size ATTRIBUTE_UNUSED
, FILE *ffile
)
210 writeINT (data
.len
, ptr
, idx
, 1, ffile
);
211 for (i
= 0; i
< data
.len
; i
++)
212 writeINT (data
.data
[i
], ptr
, idx
, 1, ffile
);
216 writeCHARS (char *string
, unsigned char *ptr
, int *idx
, int size
, FILE *ffile
)
222 /* Lets write out that record and do another one. */
223 checksum (ffile
, ptr
, *idx
, code
| 0x1000);
230 /* Variable length string. */
231 size
= strlen (string
);
235 /* BUG WAITING TO HAPPEN. */
236 memcpy (ptr
+ i
, string
, size
);
241 #define SYSROFF_SWAP_OUT
244 static char *rname_sh
[] =
246 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
249 static char *rname_h8300
[] =
251 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
257 /* The TR block is not normal - it doesn't have any contents. */
266 if (fwrite (b
, sizeof (b
), 1, file
) != 1)
267 /* FIXME: Return error status. */
268 fatal (_("Failed to write TR block"));
272 wr_un (struct coff_ofile
*ptr
, struct coff_sfile
*sfile
, int first
,
273 int nsecs ATTRIBUTE_UNUSED
)
276 struct coff_symbol
*s
;
280 if (bfd_get_file_flags (abfd
) & EXEC_P
)
281 un
.format
= FORMAT_LM
;
283 un
.format
= FORMAT_OM
;
286 /* Don't count the abs section. */
287 un
.nsections
= ptr
->nsections
- 1;
291 /* Count all the undefined and defined variables with global scope. */
295 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
297 if (s
->visible
->type
== coff_vis_ext_def
298 || s
->visible
->type
== coff_vis_common
)
301 if (s
->visible
->type
== coff_vis_ext_ref
)
307 un
.linker
= "L_GX00";
309 un
.name
= sfile
->name
;
310 sysroff_swap_un_out (file
, &un
);
314 wr_hd (struct coff_ofile
*p
)
320 if (bfd_get_file_flags (abfd
) & EXEC_P
)
321 hd
.mt
= MTYPE_ABS_LM
;
323 hd
.mt
= MTYPE_OMS_OR_LMS
;
327 hd
.nu
= p
->nsources
; /* Always one unit */
328 hd
.code
= 0; /* Always ASCII */
329 hd
.ver
= "0200"; /* Version 2.00 */
331 switch (bfd_get_arch (abfd
))
339 switch (bfd_get_mach (abfd
))
345 toolname
= "C_H8/300";
347 case bfd_mach_h8300h
:
351 toolname
= "C_H8/300H";
353 case bfd_mach_h8300s
:
357 toolname
= "C_H8/300S";
360 fatal (_("Unrecognized H8300 sub-architecture: %ld"),
361 bfd_get_mach (abfd
));
363 rnames
= rname_h8300
;
378 fatal (_("Unsupported architecture: %d"), bfd_get_arch (abfd
));
381 if (! (bfd_get_file_flags(abfd
) & EXEC_P
))
391 hd
.address
= bfd_get_start_address (abfd
);
396 hd
.mn
= strip_suffix (bfd_get_filename (abfd
));
398 sysroff_swap_hd_out (file
, &hd
);
403 wr_sh (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
407 sh
.section
= sec
->number
;
411 sysroff_swap_sh_out (file
, &sh
);
416 wr_ob (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*section
)
420 unsigned char stuff
[200];
423 while (i
< bfd_section_size (section
->bfd_section
))
426 int todo
= 200; /* Copy in 200 byte lumps. */
429 if (i
+ todo
> bfd_section_size (section
->bfd_section
))
430 todo
= bfd_section_size (section
->bfd_section
) - i
;
435 if (bfd_get_file_flags (abfd
) & EXEC_P
)
436 ob
.address
= section
->address
;
447 ob
.cpf
= 0; /* Never compress. */
449 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
450 ob
.data
.data
= stuff
;
451 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
455 /* Now fill the rest with blanks. */
456 while (i
< (bfd_size_type
) section
->size
)
459 int todo
= 200; /* Copy in 200 byte lumps. */
462 if (i
+ todo
> (bfd_size_type
) section
->size
)
463 todo
= section
->size
- i
;
466 ob
.cpf
= 0; /* Never compress. */
468 memset (stuff
, 0, todo
);
469 ob
.data
.data
= stuff
;
470 sysroff_swap_ob_out (file
, &ob
);
473 /* Now fill the rest with blanks. */
477 wr_rl (struct coff_ofile
*ptr ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
479 int nr
= sec
->nrelocs
;
482 for (i
= 0; i
< nr
; i
++)
484 struct coff_reloc
*r
= sec
->relocs
+ i
;
485 struct coff_symbol
*ref
;
495 rl
.flen
= 32; /* SH Specific. */
497 /* What sort of reloc ? Look in the section to find out. */
499 if (ref
->visible
->type
== coff_vis_ext_ref
)
501 rl
.bcount
= 4; /* Always 4 for us. */
503 rl
.symn
= ref
->er_number
;
505 else if (ref
->visible
->type
== coff_vis_common
)
507 rl
.bcount
= 11; /* Always 11 for us. */
509 rl
.secn
= ref
->where
->section
->number
;
512 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
513 rl
.aopcode_is_0x20
= 0x20;
517 rl
.bcount
= 11; /* Always 11 for us. */
519 rl
.secn
= ref
->where
->section
->number
;
522 rl
.addend
= -ref
->where
->section
->address
;
523 rl
.aopcode_is_0x20
= 0x20;
528 if ( rl
.op
== OP_SEC_REF
529 || rl
.op
== OP_EXT_REF
)
530 sysroff_swap_rl_out (file
, &rl
);
535 wr_object_body (struct coff_ofile
*p
)
539 for (i
= 1; i
< p
->nsections
; i
++)
541 wr_sh (p
, p
->sections
+ i
);
542 wr_ob (p
, p
->sections
+ i
);
543 wr_rl (p
, p
->sections
+ i
);
548 wr_dps_start (struct coff_sfile
*sfile
,
549 struct coff_section
*section ATTRIBUTE_UNUSED
,
550 struct coff_scope
*scope
, int type
, int nest
)
560 dps
.san
= scope
->sec
->number
;
561 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
562 dps
.block_size
= scope
->size
;
566 printf ("DPS %s %d %x\n",
581 sysroff_swap_dps_out (file
, &dps
);
585 wr_dps_end (struct coff_section
*section ATTRIBUTE_UNUSED
,
586 struct coff_scope
*scope ATTRIBUTE_UNUSED
, int type
)
592 sysroff_swap_dps_out (file
, &dps
);
598 return (int *) (xcalloc (x
, sizeof (int)));
602 walk_tree_type_1 (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
603 struct coff_type
*type
, int nest
)
607 case coff_secdef_type
:
608 case coff_basic_type
:
612 switch (type
->u
.basic
)
616 dbt
.btype
= BTYPE_VOID
;
617 dbt
.sign
= BTYPE_UNSPEC
;
618 dbt
.fptype
= FPTYPE_NOTSPEC
;
622 dbt
.btype
= BTYPE_CHAR
;
623 dbt
.sign
= BTYPE_UNSPEC
;
624 dbt
.fptype
= FPTYPE_NOTSPEC
;
630 dbt
.btype
= BTYPE_INT
;
631 dbt
.sign
= SIGN_SIGNED
;
632 dbt
.fptype
= FPTYPE_NOTSPEC
;
636 dbt
.btype
= BTYPE_FLOAT
;
637 dbt
.fptype
= FPTYPE_SINGLE
;
641 dbt
.btype
= BTYPE_FLOAT
;
642 dbt
.fptype
= FPTYPE_DOUBLE
;
646 dbt
.btype
= BTYPE_FLOAT
;
647 dbt
.fptype
= FPTYPE_EXTENDED
;
651 dbt
.btype
= BTYPE_CHAR
;
652 dbt
.sign
= SIGN_UNSIGNED
;
653 dbt
.fptype
= FPTYPE_NOTSPEC
;
659 dbt
.btype
= BTYPE_INT
;
660 dbt
.sign
= SIGN_UNSIGNED
;
661 dbt
.fptype
= FPTYPE_NOTSPEC
;
665 dbt
.bitsize
= type
->size
;
667 sysroff_swap_dbt_out (file
, &dbt
);
671 case coff_pointer_type
:
676 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
678 sysroff_swap_dpt_out (file
, &dpt
);
682 case coff_function_type
:
685 struct coff_symbol
*param
;
689 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
692 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
694 sysroff_swap_dfp_out (file
, &dfp
);
696 for (param
= type
->u
.function
.parameters
->vars_head
;
699 walk_tree_symbol (sfile
, 0, param
, nest
);
702 sysroff_swap_dfp_out (file
, &dfp
);
706 case coff_structdef_type
:
710 struct coff_symbol
*member
;
713 dbt
.btype
= BTYPE_STRUCT
;
714 dbt
.bitsize
= type
->size
;
715 dbt
.sign
= SIGN_UNSPEC
;
716 dbt
.fptype
= FPTYPE_NOTSPEC
;
717 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
719 sysroff_swap_dbt_out (file
, &dbt
);
722 sysroff_swap_dds_out (file
, &dds
);
724 for (member
= type
->u
.astructdef
.elements
->vars_head
;
726 member
= member
->next
)
727 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
730 sysroff_swap_dds_out (file
, &dds
);
735 case coff_structref_type
:
739 dbt
.btype
= BTYPE_TAG
;
740 dbt
.bitsize
= type
->size
;
741 dbt
.sign
= SIGN_UNSPEC
;
742 dbt
.fptype
= FPTYPE_NOTSPEC
;
744 if (type
->u
.astructref
.ref
)
745 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
750 sysroff_swap_dbt_out (file
, &dbt
);
754 case coff_array_type
:
758 int dims
= 1; /* Only output one dimension at a time. */
761 dar
.variable
= nints (dims
);
762 dar
.subtype
= nints (dims
);
763 dar
.spare
= nints (dims
);
764 dar
.max_variable
= nints (dims
);
765 dar
.maxspare
= nints (dims
);
766 dar
.max
= nints (dims
);
767 dar
.min_variable
= nints (dims
);
768 dar
.min
= nints (dims
);
769 dar
.minspare
= nints (dims
);
771 dar
.length
= type
->size
/ type
->u
.array
.dim
;
773 for (j
= 0; j
< dims
; j
++)
775 dar
.variable
[j
] = VARIABLE_FIXED
;
776 dar
.subtype
[j
] = SUB_INTEGER
;
778 dar
.max_variable
[j
] = 0;
779 dar
.max
[j
] = type
->u
.array
.dim
;
780 dar
.min_variable
[j
] = 0;
781 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
783 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
784 sysroff_swap_dar_out (file
, &dar
);
788 case coff_enumdef_type
:
792 struct coff_symbol
*member
;
794 dbt
.btype
= BTYPE_ENUM
;
795 dbt
.bitsize
= type
->size
;
796 dbt
.sign
= SIGN_UNSPEC
;
797 dbt
.fptype
= FPTYPE_NOTSPEC
;
798 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
800 sysroff_swap_dbt_out (file
, &dbt
);
805 sysroff_swap_den_out (file
, &den
);
807 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
809 member
= member
->next
)
810 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
813 sysroff_swap_den_out (file
, &den
);
817 case coff_enumref_type
:
821 dbt
.btype
= BTYPE_TAG
;
822 dbt
.bitsize
= type
->size
;
823 dbt
.sign
= SIGN_UNSPEC
;
824 dbt
.fptype
= FPTYPE_NOTSPEC
;
825 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
827 sysroff_swap_dbt_out (file
, &dbt
);
832 fatal (_("Unrecognised type: %d"), type
->type
);
844 sysroff_swap_dty_out (file, &dty);
854 sysroff_swap_dty_out (file, &dty);
859 dump_tree_structure (sfile, symbol, type, nest)
860 struct coff_sfile *sfile;
861 struct coff_symbol *symbol;
862 struct coff_type *type;
865 if (symbol->type->type == coff_function_type)
875 walk_tree_type (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
876 struct coff_type
*type
, int nest
)
884 if (symbol
->type
->type
== coff_function_type
)
886 sysroff_swap_dty_out (file
, &dty
);
887 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
889 sysroff_swap_dty_out (file
, &dty
);
892 symbol
->where
->section
,
893 symbol
->type
->u
.function
.code
,
894 BLOCK_TYPE_FUNCTION
, nest
);
895 wr_dps_start (sfile
, symbol
->where
->section
,
896 symbol
->type
->u
.function
.code
,
897 BLOCK_TYPE_BLOCK
, nest
);
898 walk_tree_scope (symbol
->where
->section
,
900 symbol
->type
->u
.function
.code
,
901 nest
+ 1, BLOCK_TYPE_BLOCK
);
903 wr_dps_end (symbol
->where
->section
,
904 symbol
->type
->u
.function
.code
,
906 wr_dps_end (symbol
->where
->section
,
907 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
911 sysroff_swap_dty_out (file
, &dty
);
912 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
914 sysroff_swap_dty_out (file
, &dty
);
919 walk_tree_symbol (struct coff_sfile
*sfile
, struct coff_section
*section ATTRIBUTE_UNUSED
, struct coff_symbol
*symbol
, int nest
)
923 memset (&dsy
, 0, sizeof(dsy
));
926 switch (symbol
->type
->type
)
928 case coff_function_type
:
929 dsy
.type
= STYPE_FUNC
;
933 case coff_structref_type
:
934 case coff_pointer_type
:
935 case coff_array_type
:
936 case coff_basic_type
:
937 case coff_enumref_type
:
938 dsy
.type
= STYPE_VAR
;
942 case coff_enumdef_type
:
943 dsy
.type
= STYPE_TAG
;
948 case coff_structdef_type
:
949 dsy
.type
= STYPE_TAG
;
951 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
954 case coff_secdef_type
:
958 fatal (_("Unrecognised coff symbol type: %d"), symbol
->type
->type
);
961 if (symbol
->where
->where
== coff_where_member_of_struct
)
964 dsy
.type
= STYPE_MEMBER
;
967 if (symbol
->where
->where
== coff_where_member_of_enum
)
969 dsy
.type
= STYPE_ENUM
;
972 dsy
.evalue
= symbol
->where
->offset
;
975 if (symbol
->type
->type
== coff_structdef_type
976 || symbol
->where
->where
== coff_where_entag
977 || symbol
->where
->where
== coff_where_strtag
)
979 dsy
.snumber
= get_member_id (symbol
->number
);
983 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
;
995 case coff_vis_ext_ref
:
996 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
999 case coff_vis_int_def
:
1000 dsy
.ainfo
= AINFO_STATIC_INT
;
1004 case coff_vis_autoparam
:
1005 dsy
.ainfo
= AINFO_AUTO
;
1008 case coff_vis_register
:
1009 case coff_vis_regparam
:
1010 dsy
.ainfo
= AINFO_REG
;
1015 case coff_vis_member_of_struct
:
1016 case coff_vis_member_of_enum
:
1020 fatal (_("Unrecognised coff symbol visibility: %d"), symbol
->visible
->type
);
1023 dsy
.dlength
= symbol
->type
->size
;
1025 switch (symbol
->where
->where
)
1027 case coff_where_memory
:
1029 dsy
.section
= symbol
->where
->section
->number
;
1035 case coff_where_member_of_struct
:
1036 case coff_where_member_of_enum
:
1037 case coff_where_stack
:
1038 case coff_where_register
:
1039 case coff_where_unknown
:
1040 case coff_where_strtag
:
1041 case coff_where_entag
:
1042 case coff_where_typedef
:
1046 fatal (_("Unrecognised coff symbol location: %d"), symbol
->where
->where
);
1049 switch (symbol
->where
->where
)
1051 case coff_where_memory
:
1052 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1055 case coff_where_stack
:
1056 dsy
.address
= symbol
->where
->offset
;
1059 case coff_where_member_of_struct
:
1060 if (symbol
->where
->bitsize
)
1062 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1064 dsy
.field_len
= symbol
->where
->bitsize
;
1065 dsy
.field_off
= (bits
/ 32) * 4;
1066 dsy
.field_bitoff
= bits
% 32;
1072 dsy
.field_len
= symbol
->type
->size
;
1073 dsy
.field_off
= symbol
->where
->offset
;
1077 case coff_where_member_of_enum
:
1079 dsy.field_len = symbol->type->size;
1080 dsy.field_off = symbol->where->offset; */
1083 case coff_where_register
:
1084 case coff_where_unknown
:
1085 case coff_where_strtag
:
1086 case coff_where_entag
:
1087 case coff_where_typedef
:
1091 fatal (_("Unrecognised coff symbol location: %d"), symbol
->where
->where
);
1094 if (symbol
->where
->where
== coff_where_register
)
1095 dsy
.reg
= rnames
[symbol
->where
->offset
];
1097 switch (symbol
->visible
->type
)
1099 case coff_vis_common
:
1100 /* We do this 'cause common C symbols are treated as extdefs. */
1101 case coff_vis_ext_def
:
1102 case coff_vis_ext_ref
:
1103 dsy
.ename
= symbol
->name
;
1106 case coff_vis_regparam
:
1107 case coff_vis_autoparam
:
1108 dsy
.type
= STYPE_PARAMETER
;
1111 case coff_vis_int_def
:
1113 case coff_vis_register
:
1115 case coff_vis_member_of_struct
:
1116 case coff_vis_member_of_enum
:
1120 fatal (_("Unrecognised coff symbol visibility: %d"), symbol
->visible
->type
);
1127 sysroff_swap_dsy_out (file
, &dsy
);
1129 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1133 walk_tree_scope (struct coff_section
*section
, struct coff_sfile
*sfile
, struct coff_scope
*scope
, int nest
, int type
)
1135 struct coff_symbol
*vars
;
1136 struct coff_scope
*child
;
1138 if (scope
->vars_head
1139 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1141 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1144 wr_globals (tree
, sfile
, nest
+ 1);
1146 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1147 walk_tree_symbol (sfile
, section
, vars
, nest
);
1149 for (child
= scope
->list_head
; child
; child
= child
->next
)
1150 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1152 wr_dps_end (section
, scope
, type
);
1157 walk_tree_sfile (struct coff_section
*section
, struct coff_sfile
*sfile
)
1159 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1163 wr_program_structure (struct coff_ofile
*p
, struct coff_sfile
*sfile
)
1165 if (p
->nsections
< 4)
1167 walk_tree_sfile (p
->sections
+ 4, sfile
);
1171 wr_du (struct coff_ofile
*p
, struct coff_sfile
*sfile
, int n
)
1177 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1178 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1180 du
.format
= bfd_get_file_flags (abfd
) & EXEC_P
? 0 : 1;
1185 du
.sections
= p
->nsections
- 1;
1186 du
.san
= (int *) xcalloc (du
.sections
, sizeof (int));
1187 du
.address
= nints (du
.sections
);
1188 du
.length
= nints (du
.sections
);
1190 for (i
= 0; i
< du
.sections
; i
++)
1197 for (j
= 0; j
< lim
; j
++)
1204 if (sfile
->section
[src
].init
)
1207 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1209 = sfile
->section
[src
].low
;
1214 du
.address
[dst
] = 0;
1219 if (sfile
->section
[src
].parent
)
1221 printf (" section %6s 0x%08x..0x%08x\n",
1222 sfile
->section
[src
].parent
->name
,
1224 du
.address
[dst
] + du
.length
[dst
] - 1);
1228 du
.sections
= dst
+ 1;
1234 sysroff_swap_du_out (file
, &du
);
1238 wr_dus (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
)
1243 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1244 dus
.drb
= nints (dus
.ns
);
1245 dus
.fname
= (char **) xcalloc (dus
.ns
, sizeof (char *));
1246 dus
.spare
= nints (dus
.ns
);
1248 /* Find the filenames. */
1250 dus
.fname
[0] = sfile
->name
;
1252 sysroff_swap_dus_out (file
, &dus
);
1256 /* Find the offset of the .text section for this sfile in the
1257 .text section for the output file. */
1260 find_base (struct coff_sfile
*sfile
, struct coff_section
*section
)
1262 return sfile
->section
[section
->number
].low
;
1266 wr_dln (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
,
1267 int n ATTRIBUTE_UNUSED
)
1269 /* Count up all the linenumbers */
1271 struct coff_symbol
*sy
;
1277 for (sy
= sfile
->scope
->vars_head
;
1281 struct coff_type
*t
= sy
->type
;
1282 if (t
->type
== coff_function_type
)
1284 struct coff_line
*l
= t
->u
.function
.lines
;
1290 dln
.sfn
= nints (lc
);
1291 dln
.sln
= nints (lc
);
1292 dln
.cc
= nints (lc
);
1293 dln
.section
= nints (lc
);
1295 dln
.from_address
= nints (lc
);
1296 dln
.to_address
= nints (lc
);
1303 /* Run through once more and fill up the structure */
1305 for (sy
= sfile
->scope
->vars_head
;
1309 if (sy
->type
->type
== coff_function_type
)
1312 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1315 int base
= find_base (sfile
, sy
->where
->section
);
1316 for (i
= 0; i
< l
->nlines
; i
++)
1318 dln
.section
[idx
] = sy
->where
->section
->number
;
1320 dln
.sln
[idx
] = l
->lines
[i
];
1321 dln
.from_address
[idx
] =
1322 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1325 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1329 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1334 sysroff_swap_dln_out (file
, &dln
);
1337 /* Write the global symbols out to the debug info. */
1340 wr_globals (struct coff_ofile
*p
, struct coff_sfile
*sfile
,
1341 int n ATTRIBUTE_UNUSED
)
1343 struct coff_symbol
*sy
;
1345 for (sy
= p
->symbol_list_head
;
1347 sy
= sy
->next_in_ofile_list
)
1349 if (sy
->visible
->type
== coff_vis_ext_def
1350 || sy
->visible
->type
== coff_vis_ext_ref
)
1352 /* Only write out symbols if they belong to
1353 the current source file. */
1354 if (sy
->sfile
== sfile
)
1355 walk_tree_symbol (sfile
, 0, sy
, 0);
1361 wr_debug (struct coff_ofile
*p
)
1363 struct coff_sfile
*sfile
;
1366 for (sfile
= p
->source_head
;
1368 sfile
= sfile
->next
)
1371 printf ("%s\n", sfile
->name
);
1373 wr_du (p
, sfile
, n
);
1375 wr_program_structure (p
, sfile
);
1376 wr_dln (p
, sfile
, n
);
1384 /* It seems that the CS struct is not normal - the size is wrong
1385 heres one I prepared earlier. */
1390 0x00, /* number of chars in variable length part */
1423 if (fwrite (b
, sizeof (b
), 1, file
) != 1)
1424 /* FIXME: Return error status. */
1425 fatal (_("Failed to write CS struct"));
1428 /* Write out the SC records for a unit. Create an SC
1429 for all the sections which appear in the output file, even
1430 if there isn't an equivalent one on the input. */
1433 wr_sc (struct coff_ofile
*ptr
, struct coff_sfile
*sfile
)
1437 /* First work out the total number of sections. */
1438 int total_sec
= ptr
->nsections
;
1441 struct coff_section
*sec
;
1442 struct coff_symbol
*symbol
;
1444 struct coff_symbol
*symbol
;
1446 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1449 for (i
= 0; i
< total_sec
; i
++)
1451 info
[i
].sec
= ptr
->sections
+ i
;
1455 for (symbol
= sfile
->scope
->vars_head
;
1457 symbol
= symbol
->next
)
1460 if (symbol
->type
->type
== coff_secdef_type
)
1462 for (i
= 0; i
< total_sec
; i
++)
1464 if (symbol
->where
->section
== info
[i
].sec
)
1466 info
[i
].symbol
= symbol
;
1473 /* Now output all the section info, and fake up some stuff for sections
1475 for (i
= 1; i
< total_sec
; i
++)
1480 symbol
= info
[i
].symbol
;
1486 /* Don't have a symbol set aside for this section, which means
1487 that nothing in this file does anything for the section. */
1488 sc
.format
= !(bfd_get_file_flags (abfd
) & EXEC_P
);
1491 name
= info
[i
].sec
->name
;
1495 if (bfd_get_file_flags (abfd
) & EXEC_P
)
1498 sc
.addr
= symbol
->where
->offset
;
1505 sc
.length
= symbol
->type
->size
;
1506 name
= symbol
->name
;
1510 sc
.concat
= CONCAT_SIMPLE
;
1518 sc
.spare1
= 0; /* If not zero, then it doesn't work. */
1519 sc
.name
= section_translate (name
);
1521 if (strlen (sc
.name
) == 1)
1527 sc
.contents
= CONTENTS_DATA
;
1531 sc
.contents
= CONTENTS_CODE
;
1536 sc
.contents
= CONTENTS_CODE
;
1539 sysroff_swap_sc_out (file
, &sc
);
1546 /* Write out the ER records for a unit. */
1549 wr_er (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1553 struct coff_symbol
*sym
;
1557 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1559 if (sym
->visible
->type
== coff_vis_ext_ref
)
1564 er
.type
= ER_NOTSPEC
;
1565 er
.name
= sym
->name
;
1566 sysroff_swap_er_out (file
, &er
);
1567 sym
->er_number
= idx
++;
1573 /* Write out the ED records for a unit. */
1576 wr_ed (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1579 struct coff_symbol
*s
;
1583 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1585 if (s
->visible
->type
== coff_vis_ext_def
1586 || s
->visible
->type
== coff_vis_common
)
1590 ed
.section
= s
->where
->section
->number
;
1593 if (s
->where
->section
->data
)
1595 ed
.type
= ED_TYPE_DATA
;
1597 else if (s
->where
->section
->code
& SEC_CODE
)
1599 ed
.type
= ED_TYPE_ENTRY
;
1603 ed
.type
= ED_TYPE_NOTSPEC
;
1604 ed
.type
= ED_TYPE_DATA
;
1607 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1609 sysroff_swap_ed_out (file
, &ed
);
1616 wr_unit_info (struct coff_ofile
*ptr
)
1618 struct coff_sfile
*sfile
;
1621 for (sfile
= ptr
->source_head
;
1623 sfile
= sfile
->next
)
1630 wr_un (ptr
, sfile
, first
, 0);
1631 nsecs
= wr_sc (ptr
, sfile
);
1633 fseek (file
, p1
, SEEK_SET
);
1634 wr_un (ptr
, sfile
, first
, nsecs
);
1635 fseek (file
, p2
, SEEK_SET
);
1636 wr_er (ptr
, sfile
, first
);
1637 wr_ed (ptr
, sfile
, first
);
1643 wr_module (struct coff_ofile
*p
)
1656 return (x
+ 3) & ~3;
1659 /* Find all the common variables and turn them into
1660 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1663 prescan (struct coff_ofile
*otree
)
1665 struct coff_symbol
*s
;
1666 struct coff_section
*common_section
;
1668 if (otree
->nsections
< 3)
1671 /* Find the common section - always section 3. */
1672 common_section
= otree
->sections
+ 3;
1674 for (s
= otree
->symbol_list_head
;
1676 s
= s
->next_in_ofile_list
)
1678 if (s
->visible
->type
== coff_vis_common
)
1680 struct coff_where
*w
= s
->where
;
1681 /* s->visible->type = coff_vis_ext_def; leave it as common */
1682 common_section
->size
= align (common_section
->size
);
1683 w
->offset
= common_section
->size
+ common_section
->address
;
1684 w
->section
= common_section
;
1685 common_section
->size
+= s
->type
->size
;
1686 common_section
->size
= align (common_section
->size
);
1691 ATTRIBUTE_NORETURN
static void
1692 show_usage (FILE *ffile
, int status
)
1694 fprintf (ffile
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
1695 fprintf (ffile
, _("Convert a COFF object file into a SYSROFF object file\n"));
1696 fprintf (ffile
, _(" The options are:\n\
1697 -q --quick (Obsolete - ignored)\n\
1698 -n --noprescan Do not perform a scan to convert commons into defs\n\
1699 -d --debug Display information about what is being done\n\
1700 @<file> Read options from <file>\n\
1701 -h --help Display this information\n\
1702 -v --version Print the program's version number\n"));
1704 if (REPORT_BUGS_TO
[0] && status
== 0)
1705 fprintf (ffile
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
1710 main (int ac
, char **av
)
1713 static struct option long_options
[] =
1715 {"debug", no_argument
, 0, 'd'},
1716 {"quick", no_argument
, 0, 'q'},
1717 {"noprescan", no_argument
, 0, 'n'},
1718 {"help", no_argument
, 0, 'h'},
1719 {"version", no_argument
, 0, 'V'},
1720 {NULL
, no_argument
, 0, 0}
1726 #ifdef HAVE_LC_MESSAGES
1727 setlocale (LC_MESSAGES
, "");
1729 setlocale (LC_CTYPE
, "");
1730 bindtextdomain (PACKAGE
, LOCALEDIR
);
1731 textdomain (PACKAGE
);
1733 program_name
= av
[0];
1734 xmalloc_set_program_name (program_name
);
1735 bfd_set_error_program_name (program_name
);
1737 expandargv (&ac
, &av
);
1739 while ((opt
= getopt_long (ac
, av
, "dHhVvqn", long_options
,
1756 show_usage (stdout
, 0);
1760 print_version ("srconv");
1766 show_usage (stderr
, 1);
1771 /* The input and output files may be named on the command line. */
1775 input_file
= av
[optind
];
1779 output_file
= av
[optind
];
1782 show_usage (stderr
, 1);
1783 if (filename_cmp (input_file
, output_file
) == 0)
1785 fatal (_("input and output files must be different"));
1794 fatal (_("no input file specified"));
1799 /* Take a .o off the input file and stick on a .obj. If
1800 it doesn't end in .o, then stick a .obj on anyway */
1802 int len
= strlen (input_file
);
1804 output_file
= xmalloc (len
+ 5);
1805 strcpy (output_file
, input_file
);
1808 && output_file
[len
- 2] == '.'
1809 && output_file
[len
- 1] == 'o')
1811 output_file
[len
] = 'b';
1812 output_file
[len
+ 1] = 'j';
1813 output_file
[len
+ 2] = 0;
1817 strcat (output_file
, ".obj");
1821 abfd
= bfd_openr (input_file
, 0);
1824 bfd_fatal (input_file
);
1826 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1828 bfd_nonfatal (input_file
);
1830 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1831 list_matching_formats (matching
);
1835 file
= fopen (output_file
, FOPEN_WB
);
1838 fatal (_("unable to open output file %s"), output_file
);
1841 printf ("ids %d %d\n", base1
, base2
);
1843 tree
= coff_grok (abfd
);