1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994-2017 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
)
319 if (bfd_get_file_flags (abfd
) & EXEC_P
)
320 hd
.mt
= MTYPE_ABS_LM
;
322 hd
.mt
= MTYPE_OMS_OR_LMS
;
326 hd
.nu
= p
->nsources
; /* Always one unit */
327 hd
.code
= 0; /* Always ASCII */
328 hd
.ver
= "0200"; /* Version 2.00 */
330 switch (bfd_get_arch (abfd
))
338 switch (bfd_get_mach (abfd
))
344 toolname
= "C_H8/300";
346 case bfd_mach_h8300h
:
350 toolname
= "C_H8/300H";
352 case bfd_mach_h8300s
:
356 toolname
= "C_H8/300S";
359 fatal (_("Unrecognized H8300 sub-architecture: %ld"),
360 bfd_get_mach (abfd
));
362 rnames
= rname_h8300
;
377 fatal (_("Unsupported architecture: %d"), bfd_get_arch (abfd
));
380 if (! (bfd_get_file_flags(abfd
) & EXEC_P
))
390 hd
.address
= bfd_get_start_address (abfd
);
395 hd
.mn
= strip_suffix (bfd_get_filename (abfd
));
397 sysroff_swap_hd_out (file
, &hd
);
402 wr_sh (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
406 sh
.section
= sec
->number
;
410 sysroff_swap_sh_out (file
, &sh
);
415 wr_ob (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_section
*section
)
419 unsigned char stuff
[200];
422 while (i
< bfd_get_section_size (section
->bfd_section
))
425 int todo
= 200; /* Copy in 200 byte lumps. */
428 if (i
+ todo
> bfd_get_section_size (section
->bfd_section
))
429 todo
= bfd_get_section_size (section
->bfd_section
) - i
;
434 if (bfd_get_file_flags (abfd
) & EXEC_P
)
435 ob
.address
= section
->address
;
446 ob
.cpf
= 0; /* Never compress. */
448 bfd_get_section_contents (abfd
, section
->bfd_section
, stuff
, i
, todo
);
449 ob
.data
.data
= stuff
;
450 sysroff_swap_ob_out (file
, &ob
/*, i + todo < section->size */ );
454 /* Now fill the rest with blanks. */
455 while (i
< (bfd_size_type
) section
->size
)
458 int todo
= 200; /* Copy in 200 byte lumps. */
461 if (i
+ todo
> (bfd_size_type
) section
->size
)
462 todo
= section
->size
- i
;
465 ob
.cpf
= 0; /* Never compress. */
467 memset (stuff
, 0, todo
);
468 ob
.data
.data
= stuff
;
469 sysroff_swap_ob_out (file
, &ob
);
472 /* Now fill the rest with blanks. */
476 wr_rl (struct coff_ofile
*ptr ATTRIBUTE_UNUSED
, struct coff_section
*sec
)
478 int nr
= sec
->nrelocs
;
481 for (i
= 0; i
< nr
; i
++)
483 struct coff_reloc
*r
= sec
->relocs
+ i
;
484 struct coff_symbol
*ref
;
494 rl
.flen
= 32; /* SH Specific. */
496 /* What sort of reloc ? Look in the section to find out. */
498 if (ref
->visible
->type
== coff_vis_ext_ref
)
500 rl
.bcount
= 4; /* Always 4 for us. */
502 rl
.symn
= ref
->er_number
;
504 else if (ref
->visible
->type
== coff_vis_common
)
506 rl
.bcount
= 11; /* Always 11 for us. */
508 rl
.secn
= ref
->where
->section
->number
;
511 rl
.addend
= ref
->where
->offset
- ref
->where
->section
->address
;
512 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;
527 if ( rl
.op
== OP_SEC_REF
528 || rl
.op
== OP_EXT_REF
)
529 sysroff_swap_rl_out (file
, &rl
);
534 wr_object_body (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 (struct coff_sfile
*sfile
,
548 struct coff_section
*section ATTRIBUTE_UNUSED
,
549 struct coff_scope
*scope
, int type
, int nest
)
559 dps
.san
= scope
->sec
->number
;
560 dps
.address
= scope
->offset
- find_base (sfile
, scope
->sec
);
561 dps
.block_size
= scope
->size
;
565 printf ("DPS %s %d %x\n",
580 sysroff_swap_dps_out (file
, &dps
);
584 wr_dps_end (struct coff_section
*section ATTRIBUTE_UNUSED
,
585 struct coff_scope
*scope ATTRIBUTE_UNUSED
, int type
)
591 sysroff_swap_dps_out (file
, &dps
);
597 return (int *) (xcalloc (sizeof (int), x
));
601 walk_tree_type_1 (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
602 struct coff_type
*type
, int nest
)
606 case coff_secdef_type
:
607 case coff_basic_type
:
611 switch (type
->u
.basic
)
615 dbt
.btype
= BTYPE_VOID
;
616 dbt
.sign
= BTYPE_UNSPEC
;
617 dbt
.fptype
= FPTYPE_NOTSPEC
;
621 dbt
.btype
= BTYPE_CHAR
;
622 dbt
.sign
= BTYPE_UNSPEC
;
623 dbt
.fptype
= FPTYPE_NOTSPEC
;
629 dbt
.btype
= BTYPE_INT
;
630 dbt
.sign
= SIGN_SIGNED
;
631 dbt
.fptype
= FPTYPE_NOTSPEC
;
635 dbt
.btype
= BTYPE_FLOAT
;
636 dbt
.fptype
= FPTYPE_SINGLE
;
640 dbt
.btype
= BTYPE_FLOAT
;
641 dbt
.fptype
= FPTYPE_DOUBLE
;
645 dbt
.btype
= BTYPE_FLOAT
;
646 dbt
.fptype
= FPTYPE_EXTENDED
;
650 dbt
.btype
= BTYPE_CHAR
;
651 dbt
.sign
= SIGN_UNSIGNED
;
652 dbt
.fptype
= FPTYPE_NOTSPEC
;
658 dbt
.btype
= BTYPE_INT
;
659 dbt
.sign
= SIGN_UNSIGNED
;
660 dbt
.fptype
= FPTYPE_NOTSPEC
;
664 dbt
.bitsize
= type
->size
;
666 sysroff_swap_dbt_out (file
, &dbt
);
670 case coff_pointer_type
:
675 walk_tree_type_1 (sfile
, symbol
, type
->u
.pointer
.points_to
, nest
+ 1);
677 sysroff_swap_dpt_out (file
, &dpt
);
681 case coff_function_type
:
684 struct coff_symbol
*param
;
688 dfp
.nparams
= type
->u
.function
.parameters
->nvars
;
691 walk_tree_type_1 (sfile
, symbol
, type
->u
.function
.function_returns
, nest
+ 1);
693 sysroff_swap_dfp_out (file
, &dfp
);
695 for (param
= type
->u
.function
.parameters
->vars_head
;
698 walk_tree_symbol (sfile
, 0, param
, nest
);
701 sysroff_swap_dfp_out (file
, &dfp
);
705 case coff_structdef_type
:
709 struct coff_symbol
*member
;
712 dbt
.btype
= BTYPE_STRUCT
;
713 dbt
.bitsize
= type
->size
;
714 dbt
.sign
= SIGN_UNSPEC
;
715 dbt
.fptype
= FPTYPE_NOTSPEC
;
716 dbt
.sid
= get_member_id (type
->u
.astructdef
.idx
);
718 sysroff_swap_dbt_out (file
, &dbt
);
721 sysroff_swap_dds_out (file
, &dds
);
723 for (member
= type
->u
.astructdef
.elements
->vars_head
;
725 member
= member
->next
)
726 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
729 sysroff_swap_dds_out (file
, &dds
);
734 case coff_structref_type
:
738 dbt
.btype
= BTYPE_TAG
;
739 dbt
.bitsize
= type
->size
;
740 dbt
.sign
= SIGN_UNSPEC
;
741 dbt
.fptype
= FPTYPE_NOTSPEC
;
743 if (type
->u
.astructref
.ref
)
744 dbt
.sid
= get_member_id (type
->u
.astructref
.ref
->number
);
749 sysroff_swap_dbt_out (file
, &dbt
);
753 case coff_array_type
:
757 int dims
= 1; /* Only output one dimension at a time. */
760 dar
.variable
= nints (dims
);
761 dar
.subtype
= nints (dims
);
762 dar
.spare
= nints (dims
);
763 dar
.max_variable
= nints (dims
);
764 dar
.maxspare
= nints (dims
);
765 dar
.max
= nints (dims
);
766 dar
.min_variable
= nints (dims
);
767 dar
.min
= nints (dims
);
768 dar
.minspare
= nints (dims
);
770 dar
.length
= type
->size
/ type
->u
.array
.dim
;
772 for (j
= 0; j
< dims
; j
++)
774 dar
.variable
[j
] = VARIABLE_FIXED
;
775 dar
.subtype
[j
] = SUB_INTEGER
;
777 dar
.max_variable
[j
] = 0;
778 dar
.max
[j
] = type
->u
.array
.dim
;
779 dar
.min_variable
[j
] = 0;
780 dar
.min
[j
] = 1; /* Why isn't this 0 ? */
782 walk_tree_type_1 (sfile
, symbol
, type
->u
.array
.array_of
, nest
+ 1);
783 sysroff_swap_dar_out (file
, &dar
);
787 case coff_enumdef_type
:
791 struct coff_symbol
*member
;
793 dbt
.btype
= BTYPE_ENUM
;
794 dbt
.bitsize
= type
->size
;
795 dbt
.sign
= SIGN_UNSPEC
;
796 dbt
.fptype
= FPTYPE_NOTSPEC
;
797 dbt
.sid
= get_member_id (type
->u
.aenumdef
.idx
);
799 sysroff_swap_dbt_out (file
, &dbt
);
804 sysroff_swap_den_out (file
, &den
);
806 for (member
= type
->u
.aenumdef
.elements
->vars_head
;
808 member
= member
->next
)
809 walk_tree_symbol (sfile
, 0, member
, nest
+ 1);
812 sysroff_swap_den_out (file
, &den
);
816 case coff_enumref_type
:
820 dbt
.btype
= BTYPE_TAG
;
821 dbt
.bitsize
= type
->size
;
822 dbt
.sign
= SIGN_UNSPEC
;
823 dbt
.fptype
= FPTYPE_NOTSPEC
;
824 dbt
.sid
= get_member_id (type
->u
.aenumref
.ref
->number
);
826 sysroff_swap_dbt_out (file
, &dbt
);
831 fatal (_("Unrecognised type: %d"), type
->type
);
843 sysroff_swap_dty_out (file, &dty);
853 sysroff_swap_dty_out (file, &dty);
858 dump_tree_structure (sfile, symbol, type, nest)
859 struct coff_sfile *sfile;
860 struct coff_symbol *symbol;
861 struct coff_type *type;
864 if (symbol->type->type == coff_function_type)
874 walk_tree_type (struct coff_sfile
*sfile
, struct coff_symbol
*symbol
,
875 struct coff_type
*type
, int nest
)
883 if (symbol
->type
->type
== coff_function_type
)
885 sysroff_swap_dty_out (file
, &dty
);
886 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
888 sysroff_swap_dty_out (file
, &dty
);
891 symbol
->where
->section
,
892 symbol
->type
->u
.function
.code
,
893 BLOCK_TYPE_FUNCTION
, nest
);
894 wr_dps_start (sfile
, symbol
->where
->section
,
895 symbol
->type
->u
.function
.code
,
896 BLOCK_TYPE_BLOCK
, nest
);
897 walk_tree_scope (symbol
->where
->section
,
899 symbol
->type
->u
.function
.code
,
900 nest
+ 1, BLOCK_TYPE_BLOCK
);
902 wr_dps_end (symbol
->where
->section
,
903 symbol
->type
->u
.function
.code
,
905 wr_dps_end (symbol
->where
->section
,
906 symbol
->type
->u
.function
.code
, BLOCK_TYPE_FUNCTION
);
910 sysroff_swap_dty_out (file
, &dty
);
911 walk_tree_type_1 (sfile
, symbol
, type
, nest
);
913 sysroff_swap_dty_out (file
, &dty
);
918 walk_tree_symbol (struct coff_sfile
*sfile
, struct coff_section
*section ATTRIBUTE_UNUSED
, struct coff_symbol
*symbol
, int nest
)
922 memset (&dsy
, 0, sizeof(dsy
));
925 switch (symbol
->type
->type
)
927 case coff_function_type
:
928 dsy
.type
= STYPE_FUNC
;
932 case coff_structref_type
:
933 case coff_pointer_type
:
934 case coff_array_type
:
935 case coff_basic_type
:
936 case coff_enumref_type
:
937 dsy
.type
= STYPE_VAR
;
941 case coff_enumdef_type
:
942 dsy
.type
= STYPE_TAG
;
947 case coff_structdef_type
:
948 dsy
.type
= STYPE_TAG
;
950 dsy
.magic
= symbol
->type
->u
.astructdef
.isstruct
? 0 : 1;
953 case coff_secdef_type
:
957 fatal (_("Unrecognised coff symbol type: %d"), symbol
->type
->type
);
960 if (symbol
->where
->where
== coff_where_member_of_struct
)
963 dsy
.type
= STYPE_MEMBER
;
966 if (symbol
->where
->where
== coff_where_member_of_enum
)
968 dsy
.type
= STYPE_ENUM
;
971 dsy
.evalue
= 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
);
985 dsy
.sname
= symbol
->name
[0] == '_' ? symbol
->name
+ 1 : symbol
->name
;
987 switch (symbol
->visible
->type
)
989 case coff_vis_common
:
990 case coff_vis_ext_def
:
991 dsy
.ainfo
= AINFO_STATIC_EXT_DEF
;
994 case coff_vis_ext_ref
:
995 dsy
.ainfo
= AINFO_STATIC_EXT_REF
;
998 case coff_vis_int_def
:
999 dsy
.ainfo
= AINFO_STATIC_INT
;
1003 case coff_vis_autoparam
:
1004 dsy
.ainfo
= AINFO_AUTO
;
1007 case coff_vis_register
:
1008 case coff_vis_regparam
:
1009 dsy
.ainfo
= AINFO_REG
;
1014 case coff_vis_member_of_struct
:
1015 case coff_vis_member_of_enum
:
1019 fatal (_("Unrecognised coff symbol visibility: %d"), symbol
->visible
->type
);
1022 dsy
.dlength
= symbol
->type
->size
;
1024 switch (symbol
->where
->where
)
1026 case coff_where_memory
:
1028 dsy
.section
= symbol
->where
->section
->number
;
1034 case coff_where_member_of_struct
:
1035 case coff_where_member_of_enum
:
1036 case coff_where_stack
:
1037 case coff_where_register
:
1038 case coff_where_unknown
:
1039 case coff_where_strtag
:
1040 case coff_where_entag
:
1041 case coff_where_typedef
:
1045 fatal (_("Unrecognised coff symbol location: %d"), symbol
->where
->where
);
1048 switch (symbol
->where
->where
)
1050 case coff_where_memory
:
1051 dsy
.address
= symbol
->where
->offset
- find_base (sfile
, symbol
->where
->section
);
1054 case coff_where_stack
:
1055 dsy
.address
= symbol
->where
->offset
;
1058 case coff_where_member_of_struct
:
1059 if (symbol
->where
->bitsize
)
1061 int bits
= (symbol
->where
->offset
* 8 + symbol
->where
->bitoffset
);
1063 dsy
.field_len
= symbol
->where
->bitsize
;
1064 dsy
.field_off
= (bits
/ 32) * 4;
1065 dsy
.field_bitoff
= bits
% 32;
1071 dsy
.field_len
= symbol
->type
->size
;
1072 dsy
.field_off
= symbol
->where
->offset
;
1076 case coff_where_member_of_enum
:
1078 dsy.field_len = symbol->type->size;
1079 dsy.field_off = symbol->where->offset; */
1082 case coff_where_register
:
1083 case coff_where_unknown
:
1084 case coff_where_strtag
:
1085 case coff_where_entag
:
1086 case coff_where_typedef
:
1090 fatal (_("Unrecognised coff symbol location: %d"), symbol
->where
->where
);
1093 if (symbol
->where
->where
== coff_where_register
)
1094 dsy
.reg
= rnames
[symbol
->where
->offset
];
1096 switch (symbol
->visible
->type
)
1098 case coff_vis_common
:
1099 /* We do this 'cause common C symbols are treated as extdefs. */
1100 case coff_vis_ext_def
:
1101 case coff_vis_ext_ref
:
1102 dsy
.ename
= symbol
->name
;
1105 case coff_vis_regparam
:
1106 case coff_vis_autoparam
:
1107 dsy
.type
= STYPE_PARAMETER
;
1110 case coff_vis_int_def
:
1112 case coff_vis_register
:
1114 case coff_vis_member_of_struct
:
1115 case coff_vis_member_of_enum
:
1119 fatal (_("Unrecognised coff symbol visibility: %d"), symbol
->visible
->type
);
1126 sysroff_swap_dsy_out (file
, &dsy
);
1128 walk_tree_type (sfile
, symbol
, symbol
->type
, nest
);
1132 walk_tree_scope (struct coff_section
*section
, struct coff_sfile
*sfile
, struct coff_scope
*scope
, int nest
, int type
)
1134 struct coff_symbol
*vars
;
1135 struct coff_scope
*child
;
1137 if (scope
->vars_head
1138 || (scope
->list_head
&& scope
->list_head
->vars_head
))
1140 wr_dps_start (sfile
, section
, scope
, type
, nest
);
1143 wr_globals (tree
, sfile
, nest
+ 1);
1145 for (vars
= scope
->vars_head
; vars
; vars
= vars
->next
)
1146 walk_tree_symbol (sfile
, section
, vars
, nest
);
1148 for (child
= scope
->list_head
; child
; child
= child
->next
)
1149 walk_tree_scope (section
, sfile
, child
, nest
+ 1, BLOCK_TYPE_BLOCK
);
1151 wr_dps_end (section
, scope
, type
);
1156 walk_tree_sfile (struct coff_section
*section
, struct coff_sfile
*sfile
)
1158 walk_tree_scope (section
, sfile
, sfile
->scope
, 0, BLOCK_TYPE_COMPUNIT
);
1162 wr_program_structure (struct coff_ofile
*p
, struct coff_sfile
*sfile
)
1164 if (p
->nsections
< 4)
1166 walk_tree_sfile (p
->sections
+ 4, sfile
);
1170 wr_du (struct coff_ofile
*p
, struct coff_sfile
*sfile
, int n
)
1176 unsigned int *lowest
= (unsigned *) nints (p
->nsections
);
1177 unsigned int *highest
= (unsigned *) nints (p
->nsections
);
1179 du
.format
= bfd_get_file_flags (abfd
) & EXEC_P
? 0 : 1;
1184 du
.sections
= p
->nsections
- 1;
1185 du
.san
= (int *) xcalloc (sizeof (int), du
.sections
);
1186 du
.address
= nints (du
.sections
);
1187 du
.length
= nints (du
.sections
);
1189 for (i
= 0; i
< du
.sections
; i
++)
1196 for (j
= 0; j
< lim
; j
++)
1203 if (sfile
->section
[src
].init
)
1206 = sfile
->section
[src
].high
- sfile
->section
[src
].low
+ 1;
1208 = sfile
->section
[src
].low
;
1213 du
.address
[dst
] = 0;
1218 if (sfile
->section
[src
].parent
)
1220 printf (" section %6s 0x%08x..0x%08x\n",
1221 sfile
->section
[src
].parent
->name
,
1223 du
.address
[dst
] + du
.length
[dst
] - 1);
1227 du
.sections
= dst
+ 1;
1233 sysroff_swap_du_out (file
, &du
);
1237 wr_dus (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
)
1242 dus
.ns
= 1; /* p->nsources; sac 14 jul 94 */
1243 dus
.drb
= nints (dus
.ns
);
1244 dus
.fname
= (char **) xcalloc (sizeof (char *), dus
.ns
);
1245 dus
.spare
= nints (dus
.ns
);
1247 /* Find the filenames. */
1249 dus
.fname
[0] = sfile
->name
;
1251 sysroff_swap_dus_out (file
, &dus
);
1255 /* Find the offset of the .text section for this sfile in the
1256 .text section for the output file. */
1259 find_base (struct coff_sfile
*sfile
, struct coff_section
*section
)
1261 return sfile
->section
[section
->number
].low
;
1265 wr_dln (struct coff_ofile
*p ATTRIBUTE_UNUSED
, struct coff_sfile
*sfile
,
1266 int n ATTRIBUTE_UNUSED
)
1268 /* Count up all the linenumbers */
1270 struct coff_symbol
*sy
;
1276 for (sy
= sfile
->scope
->vars_head
;
1280 struct coff_type
*t
= sy
->type
;
1281 if (t
->type
== coff_function_type
)
1283 struct coff_line
*l
= t
->u
.function
.lines
;
1289 dln
.sfn
= nints (lc
);
1290 dln
.sln
= nints (lc
);
1291 dln
.cc
= nints (lc
);
1292 dln
.section
= nints (lc
);
1294 dln
.from_address
= nints (lc
);
1295 dln
.to_address
= nints (lc
);
1302 /* Run through once more and fill up the structure */
1304 for (sy
= sfile
->scope
->vars_head
;
1308 if (sy
->type
->type
== coff_function_type
)
1311 struct coff_line
*l
= sy
->type
->u
.function
.lines
;
1314 int base
= find_base (sfile
, sy
->where
->section
);
1315 for (i
= 0; i
< l
->nlines
; i
++)
1317 dln
.section
[idx
] = sy
->where
->section
->number
;
1319 dln
.sln
[idx
] = l
->lines
[i
];
1320 dln
.from_address
[idx
] =
1321 l
->addresses
[i
] + sy
->where
->section
->address
- base
;
1324 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
];
1328 dln
.to_address
[idx
- 1] = dln
.from_address
[idx
- 1] + 2;
1333 sysroff_swap_dln_out (file
, &dln
);
1336 /* Write the global symbols out to the debug info. */
1339 wr_globals (struct coff_ofile
*p
, struct coff_sfile
*sfile
,
1340 int n ATTRIBUTE_UNUSED
)
1342 struct coff_symbol
*sy
;
1344 for (sy
= p
->symbol_list_head
;
1346 sy
= sy
->next_in_ofile_list
)
1348 if (sy
->visible
->type
== coff_vis_ext_def
1349 || sy
->visible
->type
== coff_vis_ext_ref
)
1351 /* Only write out symbols if they belong to
1352 the current source file. */
1353 if (sy
->sfile
== sfile
)
1354 walk_tree_symbol (sfile
, 0, sy
, 0);
1360 wr_debug (struct coff_ofile
*p
)
1362 struct coff_sfile
*sfile
;
1365 for (sfile
= p
->source_head
;
1367 sfile
= sfile
->next
)
1370 printf ("%s\n", sfile
->name
);
1372 wr_du (p
, sfile
, n
);
1374 wr_program_structure (p
, sfile
);
1375 wr_dln (p
, sfile
, n
);
1383 /* It seems that the CS struct is not normal - the size is wrong
1384 heres one I prepared earlier. */
1389 0x00, /* number of chars in variable length part */
1422 if (fwrite (b
, sizeof (b
), 1, file
) != 1)
1423 /* FIXME: Return error status. */
1424 fatal (_("Failed to write CS struct"));
1427 /* Write out the SC records for a unit. Create an SC
1428 for all the sections which appear in the output file, even
1429 if there isn't an equivalent one on the input. */
1432 wr_sc (struct coff_ofile
*ptr
, struct coff_sfile
*sfile
)
1436 /* First work out the total number of sections. */
1437 int total_sec
= ptr
->nsections
;
1440 struct coff_section
*sec
;
1441 struct coff_symbol
*symbol
;
1443 struct coff_symbol
*symbol
;
1445 = (struct myinfo
*) calloc (total_sec
, sizeof (struct myinfo
));
1448 for (i
= 0; i
< total_sec
; i
++)
1450 info
[i
].sec
= ptr
->sections
+ i
;
1454 for (symbol
= sfile
->scope
->vars_head
;
1456 symbol
= symbol
->next
)
1459 if (symbol
->type
->type
== coff_secdef_type
)
1461 for (i
= 0; i
< total_sec
; i
++)
1463 if (symbol
->where
->section
== info
[i
].sec
)
1465 info
[i
].symbol
= symbol
;
1472 /* Now output all the section info, and fake up some stuff for sections
1474 for (i
= 1; i
< total_sec
; i
++)
1479 symbol
= info
[i
].symbol
;
1485 /* Don't have a symbol set aside for this section, which means
1486 that nothing in this file does anything for the section. */
1487 sc
.format
= !(bfd_get_file_flags (abfd
) & EXEC_P
);
1490 name
= info
[i
].sec
->name
;
1494 if (bfd_get_file_flags (abfd
) & EXEC_P
)
1497 sc
.addr
= symbol
->where
->offset
;
1504 sc
.length
= symbol
->type
->size
;
1505 name
= symbol
->name
;
1509 sc
.concat
= CONCAT_SIMPLE
;
1517 sc
.spare1
= 0; /* If not zero, then it doesn't work. */
1518 sc
.name
= section_translate (name
);
1520 if (strlen (sc
.name
) == 1)
1526 sc
.contents
= CONTENTS_DATA
;
1530 sc
.contents
= CONTENTS_CODE
;
1535 sc
.contents
= CONTENTS_CODE
;
1538 sysroff_swap_sc_out (file
, &sc
);
1545 /* Write out the ER records for a unit. */
1548 wr_er (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1552 struct coff_symbol
*sym
;
1556 for (sym
= ptr
->symbol_list_head
; sym
; sym
= sym
->next_in_ofile_list
)
1558 if (sym
->visible
->type
== coff_vis_ext_ref
)
1563 er
.type
= ER_NOTSPEC
;
1564 er
.name
= sym
->name
;
1565 sysroff_swap_er_out (file
, &er
);
1566 sym
->er_number
= idx
++;
1572 /* Write out the ED records for a unit. */
1575 wr_ed (struct coff_ofile
*ptr
, struct coff_sfile
*sfile ATTRIBUTE_UNUSED
,
1578 struct coff_symbol
*s
;
1582 for (s
= ptr
->symbol_list_head
; s
; s
= s
->next_in_ofile_list
)
1584 if (s
->visible
->type
== coff_vis_ext_def
1585 || s
->visible
->type
== coff_vis_common
)
1589 ed
.section
= s
->where
->section
->number
;
1592 if (s
->where
->section
->data
)
1594 ed
.type
= ED_TYPE_DATA
;
1596 else if (s
->where
->section
->code
& SEC_CODE
)
1598 ed
.type
= ED_TYPE_ENTRY
;
1602 ed
.type
= ED_TYPE_NOTSPEC
;
1603 ed
.type
= ED_TYPE_DATA
;
1606 ed
.address
= s
->where
->offset
- s
->where
->section
->address
;
1608 sysroff_swap_ed_out (file
, &ed
);
1615 wr_unit_info (struct coff_ofile
*ptr
)
1617 struct coff_sfile
*sfile
;
1620 for (sfile
= ptr
->source_head
;
1622 sfile
= sfile
->next
)
1629 wr_un (ptr
, sfile
, first
, 0);
1630 nsecs
= wr_sc (ptr
, sfile
);
1632 fseek (file
, p1
, SEEK_SET
);
1633 wr_un (ptr
, sfile
, first
, nsecs
);
1634 fseek (file
, p2
, SEEK_SET
);
1635 wr_er (ptr
, sfile
, first
);
1636 wr_ed (ptr
, sfile
, first
);
1642 wr_module (struct coff_ofile
*p
)
1655 return (x
+ 3) & ~3;
1658 /* Find all the common variables and turn them into
1659 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1662 prescan (struct coff_ofile
*otree
)
1664 struct coff_symbol
*s
;
1665 struct coff_section
*common_section
;
1667 if (otree
->nsections
< 3)
1670 /* Find the common section - always section 3. */
1671 common_section
= otree
->sections
+ 3;
1673 for (s
= otree
->symbol_list_head
;
1675 s
= s
->next_in_ofile_list
)
1677 if (s
->visible
->type
== coff_vis_common
)
1679 struct coff_where
*w
= s
->where
;
1680 /* s->visible->type = coff_vis_ext_def; leave it as common */
1681 common_section
->size
= align (common_section
->size
);
1682 w
->offset
= common_section
->size
+ common_section
->address
;
1683 w
->section
= common_section
;
1684 common_section
->size
+= s
->type
->size
;
1685 common_section
->size
= align (common_section
->size
);
1692 ATTRIBUTE_NORETURN
static void
1693 show_usage (FILE *ffile
, int status
)
1695 fprintf (ffile
, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name
);
1696 fprintf (ffile
, _("Convert a COFF object file into a SYSROFF object file\n"));
1697 fprintf (ffile
, _(" The options are:\n\
1698 -q --quick (Obsolete - ignored)\n\
1699 -n --noprescan Do not perform a scan to convert commons into defs\n\
1700 -d --debug Display information about what is being done\n\
1701 @<file> Read options from <file>\n\
1702 -h --help Display this information\n\
1703 -v --version Print the program's version number\n"));
1705 if (REPORT_BUGS_TO
[0] && status
== 0)
1706 fprintf (ffile
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
1711 main (int ac
, char **av
)
1714 static struct option long_options
[] =
1716 {"debug", no_argument
, 0, 'd'},
1717 {"quick", no_argument
, 0, 'q'},
1718 {"noprescan", no_argument
, 0, 'n'},
1719 {"help", no_argument
, 0, 'h'},
1720 {"version", no_argument
, 0, 'V'},
1721 {NULL
, no_argument
, 0, 0}
1727 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1728 setlocale (LC_MESSAGES
, "");
1730 #if defined (HAVE_SETLOCALE)
1731 setlocale (LC_CTYPE
, "");
1733 bindtextdomain (PACKAGE
, LOCALEDIR
);
1734 textdomain (PACKAGE
);
1736 program_name
= av
[0];
1737 xmalloc_set_program_name (program_name
);
1738 bfd_set_error_program_name (program_name
);
1740 expandargv (&ac
, &av
);
1742 while ((opt
= getopt_long (ac
, av
, "dHhVvqn", long_options
,
1759 show_usage (stdout
, 0);
1763 print_version ("srconv");
1769 show_usage (stderr
, 1);
1774 /* The input and output files may be named on the command line. */
1778 input_file
= av
[optind
];
1782 output_file
= av
[optind
];
1785 show_usage (stderr
, 1);
1786 if (filename_cmp (input_file
, output_file
) == 0)
1788 fatal (_("input and output files must be different"));
1797 fatal (_("no input file specified"));
1802 /* Take a .o off the input file and stick on a .obj. If
1803 it doesn't end in .o, then stick a .obj on anyway */
1805 int len
= strlen (input_file
);
1807 output_file
= xmalloc (len
+ 5);
1808 strcpy (output_file
, input_file
);
1811 && output_file
[len
- 2] == '.'
1812 && output_file
[len
- 1] == 'o')
1814 output_file
[len
] = 'b';
1815 output_file
[len
+ 1] = 'j';
1816 output_file
[len
+ 2] = 0;
1820 strcat (output_file
, ".obj");
1824 abfd
= bfd_openr (input_file
, 0);
1827 bfd_fatal (input_file
);
1829 if (!bfd_check_format_matches (abfd
, bfd_object
, &matching
))
1831 bfd_nonfatal (input_file
);
1833 if (bfd_get_error () == bfd_error_file_ambiguously_recognized
)
1835 list_matching_formats (matching
);
1841 file
= fopen (output_file
, FOPEN_WB
);
1844 fatal (_("unable to open output file %s"), output_file
);
1847 printf ("ids %d %d\n", base1
, base2
);
1849 tree
= coff_grok (abfd
);