1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994-2016 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 int get_member_id (int);
47 static int get_ordinary_id (int);
48 static char *section_translate (char *);
49 static char *strip_suffix (const char *);
50 static void checksum (FILE *, unsigned char *, int, int);
51 static void writeINT (int, unsigned char *, int *, int, FILE *);
52 static void writeBITS (int, unsigned char *, int *, int);
53 static void writeBARRAY (barray, unsigned char *, int *, int, FILE *);
54 static void writeCHARS (char *, unsigned char *, int *, int, FILE *);
55 static void wr_tr (void);
56 static void wr_un (struct coff_ofile *, struct coff_sfile *, int, int);
57 static void wr_hd (struct coff_ofile *);
58 static void wr_sh (struct coff_ofile *, struct coff_section *);
59 static void wr_ob (struct coff_ofile *, struct coff_section *);
60 static void wr_rl (struct coff_ofile *, struct coff_section *);
61 static void wr_object_body (struct coff_ofile *);
62 static void wr_dps_start
63 (struct coff_sfile *, struct coff_section *, struct coff_scope *, int, int);
64 static void wr_dps_end (struct coff_section *, struct coff_scope *, int);
65 static int *nints (int);
66 static void walk_tree_type_1
67 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
68 static void walk_tree_type
69 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
70 static void walk_tree_symbol
71 (struct coff_sfile *, struct coff_section *, struct coff_symbol *, int);
72 static void walk_tree_scope
73 (struct coff_section *, struct coff_sfile *, struct coff_scope *, int, int);
74 static void walk_tree_sfile (struct coff_section *, struct coff_sfile *);
75 static void wr_program_structure (struct coff_ofile *, struct coff_sfile *);
76 static void wr_du (struct coff_ofile *, struct coff_sfile *, int);
77 static void wr_dus (struct coff_ofile *, struct coff_sfile *);
78 static int find_base (struct coff_sfile *, struct coff_section *);
79 static void wr_dln (struct coff_ofile *, struct coff_sfile *, int);
80 static void wr_globals (struct coff_ofile *, struct coff_sfile *, int);
81 static void wr_debug (struct coff_ofile *);
82 static void wr_cs (void);
83 static int wr_sc (struct coff_ofile *, struct coff_sfile *);
84 static void wr_er (struct coff_ofile *, struct coff_sfile *, int);
85 static void wr_ed (struct coff_ofile *, struct coff_sfile *, int);
86 static void wr_unit_info (struct coff_ofile *);
87 static void wr_module (struct coff_ofile *);
88 static int align (int);
89 static void prescan (struct coff_ofile *);
90 static void show_usage (FILE *, int);
91 extern int main (int, char **);
97 static int noprescan = 0;
98 static struct coff_ofile *tree;
100 static int absolute_p;
103 static int segmented_p;
106 static int ids1[20000];
107 static int ids2[20000];
109 static int base1 = 0x18;
110 static int base2 = 0x2018;
113 get_member_id (int x)
123 get_ordinary_id (int x)
132 section_translate (char *n)
134 if (strcmp (n, ".text") == 0)
136 if (strcmp (n, ".data") == 0)
138 if (strcmp (n, ".bss") == 0)
143 #define DATE "940201073000"; /* Just a time on my birthday */
146 strip_suffix (const char *name)
151 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
153 res = (char *) xmalloc (i + 1);
154 memcpy (res, name, i);
159 /* IT LEN stuff CS */
161 checksum (FILE *ffile, unsigned char *ptr, int size, int ccode)
166 int bytes = size / 8;
168 last = !(ccode & 0xff00);
170 fatal (_("Checksum failure"));
172 ptr[0] = ccode | (last ? 0x80 : 0);
175 for (j = 0; j < bytes; j++)
178 /* Glue on a checksum too. */
180 if (fwrite (ptr, bytes + 1, 1, ffile) != 1)
181 /* FIXME: Return error status. */
182 fatal (_("Failed to write checksum"));
187 writeINT (int n, unsigned char *ptr, int *idx, int size, FILE *ffile)
198 /* Lets write out that record and do another one. */
199 checksum (ffile, ptr, *idx, code | 0x1000);
212 ptr[byte + 0] = n >> 8;
216 ptr[byte + 0] = n >> 24;
217 ptr[byte + 1] = n >> 16;
218 ptr[byte + 2] = n >> 8;
219 ptr[byte + 3] = n >> 0;
222 fatal (_("Unsupported integer write size: %d"), size);
228 writeBITS (int val, unsigned char *ptr, int *idx, int size)
237 /* Turn off all about to change bits. */
238 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
239 /* Turn on the bits we want. */
240 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
245 writeBARRAY (barray data, unsigned char *ptr, int *idx,
246 int size ATTRIBUTE_UNUSED, FILE *ffile)
250 writeINT (data.len, ptr, idx, 1, ffile);
251 for (i = 0; i < data.len; i++)
252 writeINT (data.data[i], ptr, idx, 1, ffile);
256 writeCHARS (char *string, unsigned char *ptr, int *idx, int size, FILE *ffile)
262 /* Lets write out that record and do another one. */
263 checksum (ffile, ptr, *idx, code | 0x1000);
270 /* Variable length string. */
271 size = strlen (string);
275 /* BUG WAITING TO HAPPEN. */
276 memcpy (ptr + i, string, size);
281 #define SYSROFF_SWAP_OUT
284 static char *rname_sh[] =
286 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
289 static char *rname_h8300[] =
291 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
297 /* The TR block is not normal - it doesn't have any contents. */
306 if (fwrite (b, sizeof (b), 1, file) != 1)
307 /* FIXME: Return error status. */
308 fatal (_("Failed to write TR block"));
312 wr_un (struct coff_ofile *ptr, struct coff_sfile *sfile, int first,
313 int nsecs ATTRIBUTE_UNUSED)
316 struct coff_symbol *s;
320 if (bfd_get_file_flags (abfd) & EXEC_P)
321 un.format = FORMAT_LM;
323 un.format = FORMAT_OM;
326 /* Don't count the abs section. */
327 un.nsections = ptr->nsections - 1;
331 /* Count all the undefined and defined variables with global scope. */
335 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
337 if (s->visible->type == coff_vis_ext_def
338 || s->visible->type == coff_vis_common)
341 if (s->visible->type == coff_vis_ext_ref)
347 un.linker = "L_GX00";
349 un.name = sfile->name;
350 sysroff_swap_un_out (file, &un);
354 wr_hd (struct coff_ofile *p)
359 if (bfd_get_file_flags (abfd) & EXEC_P)
360 hd.mt = MTYPE_ABS_LM;
362 hd.mt = MTYPE_OMS_OR_LMS;
366 hd.nu = p->nsources; /* Always one unit */
367 hd.code = 0; /* Always ASCII */
368 hd.ver = "0200"; /* Version 2.00 */
370 switch (bfd_get_arch (abfd))
378 switch (bfd_get_mach (abfd))
384 toolname = "C_H8/300";
386 case bfd_mach_h8300h:
390 toolname = "C_H8/300H";
392 case bfd_mach_h8300s:
396 toolname = "C_H8/300S";
399 fatal (_("Unrecognized H8300 sub-architecture: %ld"),
400 bfd_get_mach (abfd));
402 rnames = rname_h8300;
417 fatal (_("Unsupported architecture: %d"), bfd_get_arch (abfd));
420 if (! (bfd_get_file_flags(abfd) & EXEC_P))
430 hd.address = bfd_get_start_address (abfd);
435 hd.mn = strip_suffix (bfd_get_filename (abfd));
437 sysroff_swap_hd_out (file, &hd);
442 wr_sh (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *sec)
446 sh.section = sec->number;
450 sysroff_swap_sh_out (file, &sh);
455 wr_ob (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *section)
459 unsigned char stuff[200];
462 while (i < bfd_get_section_size (section->bfd_section))
465 int todo = 200; /* Copy in 200 byte lumps. */
468 if (i + todo > bfd_get_section_size (section->bfd_section))
469 todo = bfd_get_section_size (section->bfd_section) - i;
474 if (bfd_get_file_flags (abfd) & EXEC_P)
475 ob.address = section->address;
486 ob.cpf = 0; /* Never compress. */
488 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
489 ob.data.data = stuff;
490 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
494 /* Now fill the rest with blanks. */
495 while (i < (bfd_size_type) section->size)
498 int todo = 200; /* Copy in 200 byte lumps. */
501 if (i + todo > (bfd_size_type) section->size)
502 todo = section->size - i;
505 ob.cpf = 0; /* Never compress. */
507 memset (stuff, 0, todo);
508 ob.data.data = stuff;
509 sysroff_swap_ob_out (file, &ob);
512 /* Now fill the rest with blanks. */
516 wr_rl (struct coff_ofile *ptr ATTRIBUTE_UNUSED, struct coff_section *sec)
518 int nr = sec->nrelocs;
521 for (i = 0; i < nr; i++)
523 struct coff_reloc *r = sec->relocs + i;
524 struct coff_symbol *ref;
534 rl.flen = 32; /* SH Specific. */
536 /* What sort of reloc ? Look in the section to find out. */
538 if (ref->visible->type == coff_vis_ext_ref)
540 rl.bcount = 4; /* Always 4 for us. */
542 rl.symn = ref->er_number;
544 else if (ref->visible->type == coff_vis_common)
546 rl.bcount = 11; /* Always 11 for us. */
548 rl.secn = ref->where->section->number;
551 rl.addend = ref->where->offset - ref->where->section->address;
552 rl.aopcode_is_0x20 = 0x20;
556 rl.bcount = 11; /* Always 11 for us. */
558 rl.secn = ref->where->section->number;
561 rl.addend = -ref->where->section->address;
562 rl.aopcode_is_0x20 = 0x20;
567 if ( rl.op == OP_SEC_REF
568 || rl.op == OP_EXT_REF)
569 sysroff_swap_rl_out (file, &rl);
574 wr_object_body (struct coff_ofile *p)
578 for (i = 1; i < p->nsections; i++)
580 wr_sh (p, p->sections + i);
581 wr_ob (p, p->sections + i);
582 wr_rl (p, p->sections + i);
587 wr_dps_start (struct coff_sfile *sfile,
588 struct coff_section *section ATTRIBUTE_UNUSED,
589 struct coff_scope *scope, int type, int nest)
599 dps.san = scope->sec->number;
600 dps.address = scope->offset - find_base (sfile, scope->sec);
601 dps.block_size = scope->size;
605 printf ("DPS %s %d %x\n",
620 sysroff_swap_dps_out (file, &dps);
624 wr_dps_end (struct coff_section *section ATTRIBUTE_UNUSED,
625 struct coff_scope *scope ATTRIBUTE_UNUSED, int type)
631 sysroff_swap_dps_out (file, &dps);
637 return (int *) (xcalloc (sizeof (int), x));
641 walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
642 struct coff_type *type, int nest)
646 case coff_secdef_type:
647 case coff_basic_type:
651 switch (type->u.basic)
655 dbt.btype = BTYPE_VOID;
656 dbt.sign = BTYPE_UNSPEC;
657 dbt.fptype = FPTYPE_NOTSPEC;
661 dbt.btype = BTYPE_CHAR;
662 dbt.sign = BTYPE_UNSPEC;
663 dbt.fptype = FPTYPE_NOTSPEC;
669 dbt.btype = BTYPE_INT;
670 dbt.sign = SIGN_SIGNED;
671 dbt.fptype = FPTYPE_NOTSPEC;
675 dbt.btype = BTYPE_FLOAT;
676 dbt.fptype = FPTYPE_SINGLE;
680 dbt.btype = BTYPE_FLOAT;
681 dbt.fptype = FPTYPE_DOUBLE;
685 dbt.btype = BTYPE_FLOAT;
686 dbt.fptype = FPTYPE_EXTENDED;
690 dbt.btype = BTYPE_CHAR;
691 dbt.sign = SIGN_UNSIGNED;
692 dbt.fptype = FPTYPE_NOTSPEC;
698 dbt.btype = BTYPE_INT;
699 dbt.sign = SIGN_UNSIGNED;
700 dbt.fptype = FPTYPE_NOTSPEC;
704 dbt.bitsize = type->size;
706 sysroff_swap_dbt_out (file, &dbt);
710 case coff_pointer_type:
715 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
717 sysroff_swap_dpt_out (file, &dpt);
721 case coff_function_type:
724 struct coff_symbol *param;
728 dfp.nparams = type->u.function.parameters->nvars;
731 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
733 sysroff_swap_dfp_out (file, &dfp);
735 for (param = type->u.function.parameters->vars_head;
738 walk_tree_symbol (sfile, 0, param, nest);
741 sysroff_swap_dfp_out (file, &dfp);
745 case coff_structdef_type:
749 struct coff_symbol *member;
752 dbt.btype = BTYPE_STRUCT;
753 dbt.bitsize = type->size;
754 dbt.sign = SIGN_UNSPEC;
755 dbt.fptype = FPTYPE_NOTSPEC;
756 dbt.sid = get_member_id (type->u.astructdef.idx);
758 sysroff_swap_dbt_out (file, &dbt);
761 sysroff_swap_dds_out (file, &dds);
763 for (member = type->u.astructdef.elements->vars_head;
765 member = member->next)
766 walk_tree_symbol (sfile, 0, member, nest + 1);
769 sysroff_swap_dds_out (file, &dds);
774 case coff_structref_type:
778 dbt.btype = BTYPE_TAG;
779 dbt.bitsize = type->size;
780 dbt.sign = SIGN_UNSPEC;
781 dbt.fptype = FPTYPE_NOTSPEC;
783 if (type->u.astructref.ref)
784 dbt.sid = get_member_id (type->u.astructref.ref->number);
789 sysroff_swap_dbt_out (file, &dbt);
793 case coff_array_type:
797 int dims = 1; /* Only output one dimension at a time. */
800 dar.variable = nints (dims);
801 dar.subtype = nints (dims);
802 dar.spare = nints (dims);
803 dar.max_variable = nints (dims);
804 dar.maxspare = nints (dims);
805 dar.max = nints (dims);
806 dar.min_variable = nints (dims);
807 dar.min = nints (dims);
808 dar.minspare = nints (dims);
810 dar.length = type->size / type->u.array.dim;
812 for (j = 0; j < dims; j++)
814 dar.variable[j] = VARIABLE_FIXED;
815 dar.subtype[j] = SUB_INTEGER;
817 dar.max_variable[j] = 0;
818 dar.max[j] = type->u.array.dim;
819 dar.min_variable[j] = 0;
820 dar.min[j] = 1; /* Why isn't this 0 ? */
822 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
823 sysroff_swap_dar_out (file, &dar);
827 case coff_enumdef_type:
831 struct coff_symbol *member;
833 dbt.btype = BTYPE_ENUM;
834 dbt.bitsize = type->size;
835 dbt.sign = SIGN_UNSPEC;
836 dbt.fptype = FPTYPE_NOTSPEC;
837 dbt.sid = get_member_id (type->u.aenumdef.idx);
839 sysroff_swap_dbt_out (file, &dbt);
844 sysroff_swap_den_out (file, &den);
846 for (member = type->u.aenumdef.elements->vars_head;
848 member = member->next)
849 walk_tree_symbol (sfile, 0, member, nest + 1);
852 sysroff_swap_den_out (file, &den);
856 case coff_enumref_type:
860 dbt.btype = BTYPE_TAG;
861 dbt.bitsize = type->size;
862 dbt.sign = SIGN_UNSPEC;
863 dbt.fptype = FPTYPE_NOTSPEC;
864 dbt.sid = get_member_id (type->u.aenumref.ref->number);
866 sysroff_swap_dbt_out (file, &dbt);
871 fatal (_("Unrecognised type: %d"), type->type);
883 sysroff_swap_dty_out (file, &dty);
893 sysroff_swap_dty_out (file, &dty);
898 dump_tree_structure (sfile, symbol, type, nest)
899 struct coff_sfile *sfile;
900 struct coff_symbol *symbol;
901 struct coff_type *type;
904 if (symbol->type->type == coff_function_type)
914 walk_tree_type (struct coff_sfile *sfile, struct coff_symbol *symbol,
915 struct coff_type *type, int nest)
917 if (symbol->type->type == coff_function_type)
924 sysroff_swap_dty_out (file, &dty);
925 walk_tree_type_1 (sfile, symbol, type, nest);
927 sysroff_swap_dty_out (file, &dty);
930 symbol->where->section,
931 symbol->type->u.function.code,
932 BLOCK_TYPE_FUNCTION, nest);
933 wr_dps_start (sfile, symbol->where->section,
934 symbol->type->u.function.code,
935 BLOCK_TYPE_BLOCK, nest);
936 walk_tree_scope (symbol->where->section,
938 symbol->type->u.function.code,
939 nest + 1, BLOCK_TYPE_BLOCK);
941 wr_dps_end (symbol->where->section,
942 symbol->type->u.function.code,
944 wr_dps_end (symbol->where->section,
945 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
953 sysroff_swap_dty_out (file, &dty);
954 walk_tree_type_1 (sfile, symbol, type, nest);
956 sysroff_swap_dty_out (file, &dty);
961 walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBUTE_UNUSED, struct coff_symbol *symbol, int nest)
965 memset (&dsy, 0, sizeof(dsy));
968 switch (symbol->type->type)
970 case coff_function_type:
971 dsy.type = STYPE_FUNC;
975 case coff_structref_type:
976 case coff_pointer_type:
977 case coff_array_type:
978 case coff_basic_type:
979 case coff_enumref_type:
980 dsy.type = STYPE_VAR;
984 case coff_enumdef_type:
985 dsy.type = STYPE_TAG;
990 case coff_structdef_type:
991 dsy.type = STYPE_TAG;
993 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
996 case coff_secdef_type:
1000 fatal (_("Unrecognised coff symbol type: %d"), symbol->type->type);
1003 if (symbol->where->where == coff_where_member_of_struct)
1006 dsy.type = STYPE_MEMBER;
1009 if (symbol->where->where == coff_where_member_of_enum)
1011 dsy.type = STYPE_ENUM;
1014 dsy.evalue = symbol->where->offset;
1017 if (symbol->type->type == coff_structdef_type
1018 || symbol->where->where == coff_where_entag
1019 || symbol->where->where == coff_where_strtag)
1021 dsy.snumber = get_member_id (symbol->number);
1025 dsy.snumber = get_ordinary_id (symbol->number);
1028 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1030 switch (symbol->visible->type)
1032 case coff_vis_common:
1033 case coff_vis_ext_def:
1034 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1037 case coff_vis_ext_ref:
1038 dsy.ainfo = AINFO_STATIC_EXT_REF;
1041 case coff_vis_int_def:
1042 dsy.ainfo = AINFO_STATIC_INT;
1046 case coff_vis_autoparam:
1047 dsy.ainfo = AINFO_AUTO;
1050 case coff_vis_register:
1051 case coff_vis_regparam:
1052 dsy.ainfo = AINFO_REG;
1057 case coff_vis_member_of_struct:
1058 case coff_vis_member_of_enum:
1062 fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
1065 dsy.dlength = symbol->type->size;
1067 switch (symbol->where->where)
1069 case coff_where_memory:
1071 dsy.section = symbol->where->section->number;
1077 case coff_where_member_of_struct:
1078 case coff_where_member_of_enum:
1079 case coff_where_stack:
1080 case coff_where_register:
1081 case coff_where_unknown:
1082 case coff_where_strtag:
1083 case coff_where_entag:
1084 case coff_where_typedef:
1088 fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
1091 switch (symbol->where->where)
1093 case coff_where_memory:
1094 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1097 case coff_where_stack:
1098 dsy.address = symbol->where->offset;
1101 case coff_where_member_of_struct:
1102 if (symbol->where->bitsize)
1104 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1106 dsy.field_len = symbol->where->bitsize;
1107 dsy.field_off = (bits / 32) * 4;
1108 dsy.field_bitoff = bits % 32;
1114 dsy.field_len = symbol->type->size;
1115 dsy.field_off = symbol->where->offset;
1119 case coff_where_member_of_enum:
1121 dsy.field_len = symbol->type->size;
1122 dsy.field_off = symbol->where->offset; */
1125 case coff_where_register:
1126 case coff_where_unknown:
1127 case coff_where_strtag:
1128 case coff_where_entag:
1129 case coff_where_typedef:
1133 fatal (_("Unrecognised coff symbol location: %d"), symbol->where->where);
1136 if (symbol->where->where == coff_where_register)
1137 dsy.reg = rnames[symbol->where->offset];
1139 switch (symbol->visible->type)
1141 case coff_vis_common:
1142 /* We do this 'cause common C symbols are treated as extdefs. */
1143 case coff_vis_ext_def:
1144 case coff_vis_ext_ref:
1145 dsy.ename = symbol->name;
1148 case coff_vis_regparam:
1149 case coff_vis_autoparam:
1150 dsy.type = STYPE_PARAMETER;
1153 case coff_vis_int_def:
1155 case coff_vis_register:
1157 case coff_vis_member_of_struct:
1158 case coff_vis_member_of_enum:
1162 fatal (_("Unrecognised coff symbol visibility: %d"), symbol->visible->type);
1169 sysroff_swap_dsy_out (file, &dsy);
1171 walk_tree_type (sfile, symbol, symbol->type, nest);
1175 walk_tree_scope (struct coff_section *section, struct coff_sfile *sfile, struct coff_scope *scope, int nest, int type)
1177 struct coff_symbol *vars;
1178 struct coff_scope *child;
1180 if (scope->vars_head
1181 || (scope->list_head && scope->list_head->vars_head))
1183 wr_dps_start (sfile, section, scope, type, nest);
1186 wr_globals (tree, sfile, nest + 1);
1188 for (vars = scope->vars_head; vars; vars = vars->next)
1189 walk_tree_symbol (sfile, section, vars, nest);
1191 for (child = scope->list_head; child; child = child->next)
1192 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1194 wr_dps_end (section, scope, type);
1199 walk_tree_sfile (struct coff_section *section, struct coff_sfile *sfile)
1201 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1205 wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
1207 if (p->nsections < 4)
1209 walk_tree_sfile (p->sections + 4, sfile);
1213 wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
1219 unsigned int *lowest = (unsigned *) nints (p->nsections);
1220 unsigned int *highest = (unsigned *) nints (p->nsections);
1222 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1227 du.sections = p->nsections - 1;
1228 du.san = (int *) xcalloc (sizeof (int), du.sections);
1229 du.address = nints (du.sections);
1230 du.length = nints (du.sections);
1232 for (i = 0; i < du.sections; i++)
1239 for (j = 0; j < lim; j++)
1246 if (sfile->section[src].init)
1249 = sfile->section[src].high - sfile->section[src].low + 1;
1251 = sfile->section[src].low;
1256 du.address[dst] = 0;
1261 if (sfile->section[src].parent)
1263 printf (" section %6s 0x%08x..0x%08x\n",
1264 sfile->section[src].parent->name,
1266 du.address[dst] + du.length[dst] - 1);
1270 du.sections = dst + 1;
1276 sysroff_swap_du_out (file, &du);
1280 wr_dus (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile)
1285 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1286 dus.drb = nints (dus.ns);
1287 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1288 dus.spare = nints (dus.ns);
1290 /* Find the filenames. */
1292 dus.fname[0] = sfile->name;
1294 sysroff_swap_dus_out (file, &dus);
1298 /* Find the offset of the .text section for this sfile in the
1299 .text section for the output file. */
1302 find_base (struct coff_sfile *sfile, struct coff_section *section)
1304 return sfile->section[section->number].low;
1308 wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
1309 int n ATTRIBUTE_UNUSED)
1311 /* Count up all the linenumbers */
1313 struct coff_symbol *sy;
1319 for (sy = sfile->scope->vars_head;
1323 struct coff_type *t = sy->type;
1324 if (t->type == coff_function_type)
1326 struct coff_line *l = t->u.function.lines;
1332 dln.sfn = nints (lc);
1333 dln.sln = nints (lc);
1334 dln.cc = nints (lc);
1335 dln.section = nints (lc);
1337 dln.from_address = nints (lc);
1338 dln.to_address = nints (lc);
1345 /* Run through once more and fill up the structure */
1347 for (sy = sfile->scope->vars_head;
1351 if (sy->type->type == coff_function_type)
1354 struct coff_line *l = sy->type->u.function.lines;
1357 int base = find_base (sfile, sy->where->section);
1358 for (i = 0; i < l->nlines; i++)
1360 dln.section[idx] = sy->where->section->number;
1362 dln.sln[idx] = l->lines[i];
1363 dln.from_address[idx] =
1364 l->addresses[i] + sy->where->section->address - base;
1367 dln.to_address[idx - 1] = dln.from_address[idx];
1371 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1376 sysroff_swap_dln_out (file, &dln);
1379 /* Write the global symbols out to the debug info. */
1382 wr_globals (struct coff_ofile *p, struct coff_sfile *sfile,
1383 int n ATTRIBUTE_UNUSED)
1385 struct coff_symbol *sy;
1387 for (sy = p->symbol_list_head;
1389 sy = sy->next_in_ofile_list)
1391 if (sy->visible->type == coff_vis_ext_def
1392 || sy->visible->type == coff_vis_ext_ref)
1394 /* Only write out symbols if they belong to
1395 the current source file. */
1396 if (sy->sfile == sfile)
1397 walk_tree_symbol (sfile, 0, sy, 0);
1403 wr_debug (struct coff_ofile *p)
1405 struct coff_sfile *sfile;
1408 for (sfile = p->source_head;
1410 sfile = sfile->next)
1413 printf ("%s\n", sfile->name);
1415 wr_du (p, sfile, n);
1417 wr_program_structure (p, sfile);
1418 wr_dln (p, sfile, n);
1426 /* It seems that the CS struct is not normal - the size is wrong
1427 heres one I prepared earlier. */
1432 0x00, /* number of chars in variable length part */
1465 if (fwrite (b, sizeof (b), 1, file) != 1)
1466 /* FIXME: Return error status. */
1467 fatal (_("Failed to write CS struct"));
1470 /* Write out the SC records for a unit. Create an SC
1471 for all the sections which appear in the output file, even
1472 if there isn't an equivalent one on the input. */
1475 wr_sc (struct coff_ofile *ptr, struct coff_sfile *sfile)
1479 /* First work out the total number of sections. */
1480 int total_sec = ptr->nsections;
1483 struct coff_section *sec;
1484 struct coff_symbol *symbol;
1486 struct coff_symbol *symbol;
1488 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1491 for (i = 0; i < total_sec; i++)
1493 info[i].sec = ptr->sections + i;
1497 for (symbol = sfile->scope->vars_head;
1499 symbol = symbol->next)
1502 if (symbol->type->type == coff_secdef_type)
1504 for (i = 0; i < total_sec; i++)
1506 if (symbol->where->section == info[i].sec)
1508 info[i].symbol = symbol;
1515 /* Now output all the section info, and fake up some stuff for sections
1517 for (i = 1; i < total_sec; i++)
1522 symbol = info[i].symbol;
1528 /* Don't have a symbol set aside for this section, which means
1529 that nothing in this file does anything for the section. */
1530 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1533 name = info[i].sec->name;
1537 if (bfd_get_file_flags (abfd) & EXEC_P)
1540 sc.addr = symbol->where->offset;
1547 sc.length = symbol->type->size;
1548 name = symbol->name;
1552 sc.concat = CONCAT_SIMPLE;
1560 sc.spare1 = 0; /* If not zero, then it doesn't work. */
1561 sc.name = section_translate (name);
1563 if (strlen (sc.name) == 1)
1569 sc.contents = CONTENTS_DATA;
1573 sc.contents = CONTENTS_CODE;
1578 sc.contents = CONTENTS_CODE;
1581 sysroff_swap_sc_out (file, &sc);
1588 /* Write out the ER records for a unit. */
1591 wr_er (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1595 struct coff_symbol *sym;
1599 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1601 if (sym->visible->type == coff_vis_ext_ref)
1606 er.type = ER_NOTSPEC;
1607 er.name = sym->name;
1608 sysroff_swap_er_out (file, &er);
1609 sym->er_number = idx++;
1615 /* Write out the ED records for a unit. */
1618 wr_ed (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1621 struct coff_symbol *s;
1625 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1627 if (s->visible->type == coff_vis_ext_def
1628 || s->visible->type == coff_vis_common)
1632 ed.section = s->where->section->number;
1635 if (s->where->section->data)
1637 ed.type = ED_TYPE_DATA;
1639 else if (s->where->section->code & SEC_CODE)
1641 ed.type = ED_TYPE_ENTRY;
1645 ed.type = ED_TYPE_NOTSPEC;
1646 ed.type = ED_TYPE_DATA;
1649 ed.address = s->where->offset - s->where->section->address;
1651 sysroff_swap_ed_out (file, &ed);
1658 wr_unit_info (struct coff_ofile *ptr)
1660 struct coff_sfile *sfile;
1663 for (sfile = ptr->source_head;
1665 sfile = sfile->next)
1672 wr_un (ptr, sfile, first, 0);
1673 nsecs = wr_sc (ptr, sfile);
1675 fseek (file, p1, SEEK_SET);
1676 wr_un (ptr, sfile, first, nsecs);
1677 fseek (file, p2, SEEK_SET);
1678 wr_er (ptr, sfile, first);
1679 wr_ed (ptr, sfile, first);
1685 wr_module (struct coff_ofile *p)
1698 return (x + 3) & ~3;
1701 /* Find all the common variables and turn them into
1702 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1705 prescan (struct coff_ofile *otree)
1707 struct coff_symbol *s;
1708 struct coff_section *common_section;
1710 if (otree->nsections < 3)
1713 /* Find the common section - always section 3. */
1714 common_section = otree->sections + 3;
1716 for (s = otree->symbol_list_head;
1718 s = s->next_in_ofile_list)
1720 if (s->visible->type == coff_vis_common)
1722 struct coff_where *w = s->where;
1723 /* s->visible->type = coff_vis_ext_def; leave it as common */
1724 common_section->size = align (common_section->size);
1725 w->offset = common_section->size + common_section->address;
1726 w->section = common_section;
1727 common_section->size += s->type->size;
1728 common_section->size = align (common_section->size);
1736 show_usage (FILE *ffile, int status)
1738 fprintf (ffile, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
1739 fprintf (ffile, _("Convert a COFF object file into a SYSROFF object file\n"));
1740 fprintf (ffile, _(" The options are:\n\
1741 -q --quick (Obsolete - ignored)\n\
1742 -n --noprescan Do not perform a scan to convert commons into defs\n\
1743 -d --debug Display information about what is being done\n\
1744 @<file> Read options from <file>\n\
1745 -h --help Display this information\n\
1746 -v --version Print the program's version number\n"));
1748 if (REPORT_BUGS_TO[0] && status == 0)
1749 fprintf (ffile, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1754 main (int ac, char **av)
1757 static struct option long_options[] =
1759 {"debug", no_argument, 0, 'd'},
1760 {"quick", no_argument, 0, 'q'},
1761 {"noprescan", no_argument, 0, 'n'},
1762 {"help", no_argument, 0, 'h'},
1763 {"version", no_argument, 0, 'V'},
1764 {NULL, no_argument, 0, 0}
1770 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1771 setlocale (LC_MESSAGES, "");
1773 #if defined (HAVE_SETLOCALE)
1774 setlocale (LC_CTYPE, "");
1776 bindtextdomain (PACKAGE, LOCALEDIR);
1777 textdomain (PACKAGE);
1779 program_name = av[0];
1780 xmalloc_set_program_name (program_name);
1781 bfd_set_error_program_name (program_name);
1783 expandargv (&ac, &av);
1785 while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
1802 show_usage (stdout, 0);
1806 print_version ("srconv");
1812 show_usage (stderr, 1);
1817 /* The input and output files may be named on the command line. */
1821 input_file = av[optind];
1825 output_file = av[optind];
1828 show_usage (stderr, 1);
1829 if (filename_cmp (input_file, output_file) == 0)
1831 fatal (_("input and output files must be different"));
1840 fatal (_("no input file specified"));
1845 /* Take a .o off the input file and stick on a .obj. If
1846 it doesn't end in .o, then stick a .obj on anyway */
1848 int len = strlen (input_file);
1850 output_file = xmalloc (len + 5);
1851 strcpy (output_file, input_file);
1854 && output_file[len - 2] == '.'
1855 && output_file[len - 1] == 'o')
1857 output_file[len] = 'b';
1858 output_file[len + 1] = 'j';
1859 output_file[len + 2] = 0;
1863 strcat (output_file, ".obj");
1867 abfd = bfd_openr (input_file, 0);
1870 bfd_fatal (input_file);
1872 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1874 bfd_nonfatal (input_file);
1876 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1878 list_matching_formats (matching);
1884 file = fopen (output_file, FOPEN_WB);
1887 fatal (_("unable to open output file %s"), output_file);
1890 printf ("ids %d %d\n", base1, base2);
1892 tree = coff_grok (abfd);