1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of GNU Binutils.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22 /* Written by Steve Chamberlain (sac@cygnus.com)
24 This program can be used to convert a coff object file
25 into a Hitachi OM/LM (Sysroff) format.
27 All debugging information is preserved */
33 #include "libiberty.h"
36 #include "coff/internal.h"
37 #include "../bfd/libcoff.h"
42 static char *toolname;
45 static int get_member_id (int);
46 static int get_ordinary_id (int);
47 static char *section_translate (char *);
48 static char *strip_suffix (char *);
49 static void checksum (FILE *, char *, int, int);
50 static void writeINT (int, char *, int *, int, FILE *);
51 static void writeBITS (int, char *, int *, int);
52 static void writeBARRAY (barray, char *, int *, int, FILE *);
53 static void writeCHARS (char *, char *, int *, int, FILE *);
54 static void wr_tr (void);
55 static void wr_un (struct coff_ofile *, struct coff_sfile *, int, int);
56 static void wr_hd (struct coff_ofile *);
57 static void wr_sh (struct coff_ofile *, struct coff_section *);
58 static void wr_ob (struct coff_ofile *, struct coff_section *);
59 static void wr_rl (struct coff_ofile *, struct coff_section *);
60 static void wr_object_body (struct coff_ofile *);
61 static void wr_dps_start
62 (struct coff_sfile *, struct coff_section *, struct coff_scope *, int, int);
63 static void wr_dps_end (struct coff_section *, struct coff_scope *, int);
64 static int *nints (int);
65 static void walk_tree_type_1
66 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
67 static void walk_tree_type
68 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
69 static void walk_tree_symbol
70 (struct coff_sfile *, struct coff_section *, struct coff_symbol *, int);
71 static void walk_tree_scope
72 (struct coff_section *, struct coff_sfile *, struct coff_scope *, int, int);
73 static void walk_tree_sfile (struct coff_section *, struct coff_sfile *);
74 static void wr_program_structure (struct coff_ofile *, struct coff_sfile *);
75 static void wr_du (struct coff_ofile *, struct coff_sfile *, int);
76 static void wr_dus (struct coff_ofile *, struct coff_sfile *);
77 static int find_base (struct coff_sfile *, struct coff_section *);
78 static void wr_dln (struct coff_ofile *, struct coff_sfile *, int);
79 static void wr_globals (struct coff_ofile *, struct coff_sfile *, int);
80 static void wr_debug (struct coff_ofile *);
81 static void wr_cs (void);
82 static int wr_sc (struct coff_ofile *, struct coff_sfile *);
83 static void wr_er (struct coff_ofile *, struct coff_sfile *, int);
84 static void wr_ed (struct coff_ofile *, struct coff_sfile *, int);
85 static void wr_unit_info (struct coff_ofile *);
86 static void wr_module (struct coff_ofile *);
87 static int align (int);
88 static void prescan (struct coff_ofile *);
89 static void show_usage (FILE *, int);
90 extern int main (int, char **);
96 static int noprescan = 0;
97 static struct coff_ofile *tree;
99 static int absolute_p;
102 static int segmented_p;
105 static int ids1[20000];
106 static int ids2[20000];
108 static int base1 = 0x18;
109 static int base2 = 0x2018;
112 get_member_id (int x)
122 get_ordinary_id (int x)
131 section_translate (char *n)
133 if (strcmp (n, ".text") == 0)
135 if (strcmp (n, ".data") == 0)
137 if (strcmp (n, ".bss") == 0)
142 #define DATE "940201073000"; /* Just a time on my birthday */
146 strip_suffix (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 *file, char *ptr, int size, int code)
166 int bytes = size / 8;
168 last = !(code & 0xff00);
171 ptr[0] = code | (last ? 0x80 : 0);
174 for (j = 0; j < bytes; j++)
177 /* Glue on a checksum too. */
179 fwrite (ptr, bytes + 1, 1, file);
184 writeINT (int n, char *ptr, int *idx, int size, FILE *file)
195 /* Lets write out that record and do another one. */
196 checksum (file, ptr, *idx, code | 0x1000);
209 ptr[byte + 0] = n >> 8;
213 ptr[byte + 0] = n >> 24;
214 ptr[byte + 1] = n >> 16;
215 ptr[byte + 2] = n >> 8;
216 ptr[byte + 3] = n >> 0;
225 writeBITS (int val, char *ptr, int *idx, int size)
234 /* Turn off all about to change bits. */
235 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
236 /* Turn on the bits we want. */
237 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
242 writeBARRAY (barray data, char *ptr, int *idx, int size ATTRIBUTE_UNUSED,
247 writeINT (data.len, ptr, idx, 1, file);
248 for (i = 0; i < data.len; i++)
249 writeINT (data.data[i], ptr, idx, 1, file);
253 writeCHARS (char *string, char *ptr, int *idx, int size, FILE *file)
259 /* Lets write out that record and do another one. */
260 checksum (file, ptr, *idx, code | 0x1000);
267 /* Variable length string. */
268 size = strlen (string);
272 /* BUG WAITING TO HAPPEN. */
273 memcpy (ptr + i, string, size);
278 #define SYSROFF_SWAP_OUT
281 static char *rname_sh[] =
283 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
286 static char *rname_h8300[] =
288 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
294 /* The TR block is not normal - it doesn't have any contents. */
302 fwrite (b, 1, sizeof (b), file);
306 wr_un (struct coff_ofile *ptr, struct coff_sfile *sfile, int first,
307 int nsecs ATTRIBUTE_UNUSED)
310 struct coff_symbol *s;
314 if (bfd_get_file_flags (abfd) & EXEC_P)
315 un.format = FORMAT_LM;
317 un.format = FORMAT_OM;
321 un.nsections = ptr->nsections - 1; /* Don't count the abs section. */
323 /*NEW - only count sections with size. */
324 un.nsections = nsecs;
329 /* Count all the undefined and defined variables with global scope. */
333 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
335 if (s->visible->type == coff_vis_ext_def
336 || s->visible->type == coff_vis_common)
339 if (s->visible->type == coff_vis_ext_ref)
345 un.linker = "L_GX00";
347 un.name = sfile->name;
348 sysroff_swap_un_out (file, &un);
352 wr_hd (struct coff_ofile *p)
357 if (bfd_get_file_flags (abfd) & EXEC_P)
358 hd.mt = MTYPE_ABS_LM;
360 hd.mt = MTYPE_OMS_OR_LMS;
364 hd.nu = p->nsources; /* Always one unit */
365 hd.code = 0; /* Always ASCII */
366 hd.ver = "0200"; /* Version 2.00 */
368 switch (bfd_get_arch (abfd))
376 switch (bfd_get_mach (abfd))
382 toolname = "C_H8/300";
384 case bfd_mach_h8300h:
388 toolname = "C_H8/300H";
390 case bfd_mach_h8300s:
394 toolname = "C_H8/300S";
399 rnames = rname_h8300;
417 if (! bfd_get_file_flags(abfd) & EXEC_P)
427 hd.address = bfd_get_start_address (abfd);
432 hd.mn = strip_suffix (bfd_get_filename (abfd));
434 sysroff_swap_hd_out (file, &hd);
439 wr_sh (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *sec)
443 sh.section = sec->number;
447 sysroff_swap_sh_out (file, &sh);
452 wr_ob (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *section)
456 unsigned char stuff[200];
459 while (i < section->bfd_section->_raw_size)
462 int todo = 200; /* Copy in 200 byte lumps. */
465 if (i + todo > section->bfd_section->_raw_size)
466 todo = section->bfd_section->_raw_size - i;
471 if (bfd_get_file_flags (abfd) & EXEC_P)
472 ob.address = section->address;
483 ob.cpf = 0; /* Never compress. */
485 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
486 ob.data.data = stuff;
487 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
491 /* Now fill the rest with blanks. */
492 while (i < (bfd_size_type) section->size)
495 int todo = 200; /* Copy in 200 byte lumps. */
498 if (i + todo > (bfd_size_type) section->size)
499 todo = section->size - i;
502 ob.cpf = 0; /* Never compress. */
504 memset (stuff, 0, todo);
505 ob.data.data = stuff;
506 sysroff_swap_ob_out (file, &ob);
509 /* Now fill the rest with blanks. */
513 wr_rl (struct coff_ofile *ptr ATTRIBUTE_UNUSED, struct coff_section *sec)
515 int nr = sec->nrelocs;
518 for (i = 0; i < nr; i++)
520 struct coff_reloc *r = sec->relocs + i;
521 struct coff_symbol *ref;
531 rl.flen = 32; /* SH Specific. */
533 /* What sort of reloc ? Look in the section to find out. */
535 if (ref->visible->type == coff_vis_ext_ref)
537 rl.bcount = 4; /* Always 4 for us. */
539 rl.symn = ref->er_number;
541 else if (ref->visible->type == coff_vis_common)
543 rl.bcount = 11; /* Always 11 for us. */
545 rl.secn = ref->where->section->number;
548 rl.addend = ref->where->offset - ref->where->section->address;
549 rl.aopcode_is_0x20 = 0x20;
553 rl.bcount = 11; /* Always 11 for us. */
555 rl.secn = ref->where->section->number;
558 rl.addend = -ref->where->section->address;
559 rl.aopcode_is_0x20 = 0x20;
564 if ( rl.op == OP_SEC_REF
565 || rl.op == OP_EXT_REF)
566 sysroff_swap_rl_out (file, &rl);
571 wr_object_body (struct coff_ofile *p)
575 for (i = 1; i < p->nsections; i++)
577 wr_sh (p, p->sections + i);
578 wr_ob (p, p->sections + i);
579 wr_rl (p, p->sections + i);
584 wr_dps_start (struct coff_sfile *sfile,
585 struct coff_section *section ATTRIBUTE_UNUSED,
586 struct coff_scope *scope, int type, int nest)
596 dps.san = scope->sec->number;
597 dps.address = scope->offset - find_base (sfile, scope->sec);
598 dps.block_size = scope->size;
602 printf ("DPS %s %d %x\n",
617 sysroff_swap_dps_out (file, &dps);
621 wr_dps_end (struct coff_section *section ATTRIBUTE_UNUSED,
622 struct coff_scope *scope ATTRIBUTE_UNUSED, int type)
628 sysroff_swap_dps_out (file, &dps);
634 return (int *) (xcalloc (sizeof (int), x));
638 walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
639 struct coff_type *type, int nest)
643 case coff_secdef_type:
644 case coff_basic_type:
648 switch (type->u.basic)
652 dbt.btype = BTYPE_VOID;
653 dbt.sign = BTYPE_UNSPEC;
654 dbt.fptype = FPTYPE_NOTSPEC;
658 dbt.btype = BTYPE_CHAR;
659 dbt.sign = BTYPE_UNSPEC;
660 dbt.fptype = FPTYPE_NOTSPEC;
666 dbt.btype = BTYPE_INT;
667 dbt.sign = SIGN_SIGNED;
668 dbt.fptype = FPTYPE_NOTSPEC;
672 dbt.btype = BTYPE_FLOAT;
673 dbt.fptype = FPTYPE_SINGLE;
677 dbt.btype = BTYPE_FLOAT;
678 dbt.fptype = FPTYPE_DOUBLE;
682 dbt.btype = BTYPE_FLOAT;
683 dbt.fptype = FPTYPE_EXTENDED;
687 dbt.btype = BTYPE_CHAR;
688 dbt.sign = SIGN_UNSIGNED;
689 dbt.fptype = FPTYPE_NOTSPEC;
695 dbt.btype = BTYPE_INT;
696 dbt.sign = SIGN_UNSIGNED;
697 dbt.fptype = FPTYPE_NOTSPEC;
701 dbt.bitsize = type->size;
703 sysroff_swap_dbt_out (file, &dbt);
707 case coff_pointer_type:
711 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
713 sysroff_swap_dpt_out (file, &dpt);
717 case coff_function_type:
720 struct coff_symbol *param;
724 dfp.nparams = type->u.function.parameters->nvars;
727 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
729 sysroff_swap_dfp_out (file, &dfp);
731 for (param = type->u.function.parameters->vars_head;
734 walk_tree_symbol (sfile, 0, param, nest);
737 sysroff_swap_dfp_out (file, &dfp);
741 case coff_structdef_type:
745 struct coff_symbol *member;
748 dbt.btype = BTYPE_STRUCT;
749 dbt.bitsize = type->size;
750 dbt.sign = SIGN_UNSPEC;
751 dbt.fptype = FPTYPE_NOTSPEC;
752 dbt.sid = get_member_id (type->u.astructdef.idx);
754 sysroff_swap_dbt_out (file, &dbt);
757 sysroff_swap_dds_out (file, &dds);
759 for (member = type->u.astructdef.elements->vars_head;
761 member = member->next)
762 walk_tree_symbol (sfile, 0, member, nest + 1);
765 sysroff_swap_dds_out (file, &dds);
770 case coff_structref_type:
774 dbt.btype = BTYPE_TAG;
775 dbt.bitsize = type->size;
776 dbt.sign = SIGN_UNSPEC;
777 dbt.fptype = FPTYPE_NOTSPEC;
779 if (type->u.astructref.ref)
780 dbt.sid = get_member_id (type->u.astructref.ref->number);
785 sysroff_swap_dbt_out (file, &dbt);
789 case coff_array_type:
793 int dims = 1; /* Only output one dimension at a time. */
796 dar.variable = nints (dims);
797 dar.subtype = nints (dims);
798 dar.spare = nints (dims);
799 dar.max_variable = nints (dims);
800 dar.maxspare = nints (dims);
801 dar.max = nints (dims);
802 dar.min_variable = nints (dims);
803 dar.min = nints (dims);
804 dar.minspare = nints (dims);
806 dar.length = type->size / type->u.array.dim;
808 for (j = 0; j < dims; j++)
810 dar.variable[j] = VARIABLE_FIXED;
811 dar.subtype[j] = SUB_INTEGER;
813 dar.max_variable[j] = 0;
814 dar.max[j] = type->u.array.dim;
815 dar.min_variable[j] = 0;
816 dar.min[j] = 1; /* Why isn't this 0 ? */
818 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
819 sysroff_swap_dar_out (file, &dar);
823 case coff_enumdef_type:
827 struct coff_symbol *member;
829 dbt.btype = BTYPE_ENUM;
830 dbt.bitsize = type->size;
831 dbt.sign = SIGN_UNSPEC;
832 dbt.fptype = FPTYPE_NOTSPEC;
833 dbt.sid = get_member_id (type->u.aenumdef.idx);
835 sysroff_swap_dbt_out (file, &dbt);
840 sysroff_swap_den_out (file, &den);
842 for (member = type->u.aenumdef.elements->vars_head;
844 member = member->next)
845 walk_tree_symbol (sfile, 0, member, nest + 1);
848 sysroff_swap_den_out (file, &den);
852 case coff_enumref_type:
856 dbt.btype = BTYPE_TAG;
857 dbt.bitsize = type->size;
858 dbt.sign = SIGN_UNSPEC;
859 dbt.fptype = FPTYPE_NOTSPEC;
860 dbt.sid = get_member_id (type->u.aenumref.ref->number);
862 sysroff_swap_dbt_out (file, &dbt);
879 sysroff_swap_dty_out (file, &dty);
889 sysroff_swap_dty_out (file, &dty);
894 dump_tree_structure (sfile, symbol, type, nest)
895 struct coff_sfile *sfile;
896 struct coff_symbol *symbol;
897 struct coff_type *type;
900 if (symbol->type->type == coff_function_type)
910 walk_tree_type (struct coff_sfile *sfile, struct coff_symbol *symbol,
911 struct coff_type *type, int nest)
913 if (symbol->type->type == coff_function_type)
920 sysroff_swap_dty_out (file, &dty);
921 walk_tree_type_1 (sfile, symbol, type, nest);
923 sysroff_swap_dty_out (file, &dty);
926 symbol->where->section,
927 symbol->type->u.function.code,
928 BLOCK_TYPE_FUNCTION, nest);
929 wr_dps_start (sfile, symbol->where->section,
930 symbol->type->u.function.code,
931 BLOCK_TYPE_BLOCK, nest);
932 walk_tree_scope (symbol->where->section,
934 symbol->type->u.function.code,
935 nest + 1, BLOCK_TYPE_BLOCK);
937 wr_dps_end (symbol->where->section,
938 symbol->type->u.function.code,
940 wr_dps_end (symbol->where->section,
941 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
949 sysroff_swap_dty_out (file, &dty);
950 walk_tree_type_1 (sfile, symbol, type, nest);
952 sysroff_swap_dty_out (file, &dty);
957 walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBUTE_UNUSED, struct coff_symbol *symbol, int nest)
961 memset (&dsy, 0, sizeof(dsy));
964 switch (symbol->type->type)
966 case coff_function_type:
967 dsy.type = STYPE_FUNC;
971 case coff_structref_type:
972 case coff_pointer_type:
973 case coff_array_type:
974 case coff_basic_type:
975 case coff_enumref_type:
976 dsy.type = STYPE_VAR;
980 case coff_enumdef_type:
981 dsy.type = STYPE_TAG;
986 case coff_structdef_type:
987 dsy.type = STYPE_TAG;
989 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
992 case coff_secdef_type:
999 if (symbol->where->where == coff_where_member_of_struct)
1002 dsy.type = STYPE_MEMBER;
1005 if (symbol->where->where == coff_where_member_of_enum)
1007 dsy.type = STYPE_ENUM;
1010 dsy.evalue = symbol->where->offset;
1013 if (symbol->type->type == coff_structdef_type
1014 || symbol->where->where == coff_where_entag
1015 || symbol->where->where == coff_where_strtag)
1017 dsy.snumber = get_member_id (symbol->number);
1021 dsy.snumber = get_ordinary_id (symbol->number);
1024 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1026 switch (symbol->visible->type)
1028 case coff_vis_common:
1029 case coff_vis_ext_def:
1030 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1033 case coff_vis_ext_ref:
1034 dsy.ainfo = AINFO_STATIC_EXT_REF;
1037 case coff_vis_int_def:
1038 dsy.ainfo = AINFO_STATIC_INT;
1042 case coff_vis_autoparam:
1043 dsy.ainfo = AINFO_AUTO;
1046 case coff_vis_register:
1047 case coff_vis_regparam:
1048 dsy.ainfo = AINFO_REG;
1053 case coff_vis_member_of_struct:
1054 case coff_vis_member_of_enum:
1061 dsy.dlength = symbol->type->size;
1063 switch (symbol->where->where)
1065 case coff_where_memory:
1067 dsy.section = symbol->where->section->number;
1073 case coff_where_member_of_struct:
1074 case coff_where_member_of_enum:
1075 case coff_where_stack:
1076 case coff_where_register:
1077 case coff_where_unknown:
1078 case coff_where_strtag:
1079 case coff_where_entag:
1080 case coff_where_typedef:
1087 switch (symbol->where->where)
1089 case coff_where_memory:
1090 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1093 case coff_where_stack:
1094 dsy.address = symbol->where->offset;
1097 case coff_where_member_of_struct:
1098 if (symbol->where->bitsize)
1100 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1102 dsy.field_len = symbol->where->bitsize;
1103 dsy.field_off = (bits / 32) * 4;
1104 dsy.field_bitoff = bits % 32;
1110 dsy.field_len = symbol->type->size;
1111 dsy.field_off = symbol->where->offset;
1115 case coff_where_member_of_enum:
1117 dsy.field_len = symbol->type->size;
1118 dsy.field_off = symbol->where->offset; */
1121 case coff_where_register:
1122 case coff_where_unknown:
1123 case coff_where_strtag:
1124 case coff_where_entag:
1125 case coff_where_typedef:
1132 if (symbol->where->where == coff_where_register)
1133 dsy.reg = rnames[symbol->where->offset];
1135 switch (symbol->visible->type)
1137 case coff_vis_common:
1138 /* We do this 'cause common C symbols are treated as extdefs. */
1139 case coff_vis_ext_def:
1140 case coff_vis_ext_ref:
1141 dsy.ename = symbol->name;
1144 case coff_vis_regparam:
1145 case coff_vis_autoparam:
1146 dsy.type = STYPE_PARAMETER;
1149 case coff_vis_int_def:
1151 case coff_vis_register:
1153 case coff_vis_member_of_struct:
1154 case coff_vis_member_of_enum:
1165 sysroff_swap_dsy_out (file, &dsy);
1167 walk_tree_type (sfile, symbol, symbol->type, nest);
1171 walk_tree_scope (struct coff_section *section, struct coff_sfile *sfile, struct coff_scope *scope, int nest, int type)
1173 struct coff_symbol *vars;
1174 struct coff_scope *child;
1176 if (scope->vars_head
1177 || (scope->list_head && scope->list_head->vars_head))
1179 wr_dps_start (sfile, section, scope, type, nest);
1182 wr_globals (tree, sfile, nest + 1);
1184 for (vars = scope->vars_head; vars; vars = vars->next)
1185 walk_tree_symbol (sfile, section, vars, nest);
1187 for (child = scope->list_head; child; child = child->next)
1188 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1190 wr_dps_end (section, scope, type);
1195 walk_tree_sfile (struct coff_section *section, struct coff_sfile *sfile)
1197 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1201 wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
1203 walk_tree_sfile (p->sections + 4, sfile);
1207 wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
1212 struct coff_symbol *symbol;
1213 static int incit = 0x500000;
1218 unsigned int *lowest = (unsigned *) nints (p->nsections);
1219 unsigned int *highest = (unsigned *) nints (p->nsections);
1221 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1226 du.sections = p->nsections - 1;
1227 du.san = (int *) xcalloc (sizeof (int), du.sections);
1228 du.address = nints (du.sections);
1229 du.length = nints (du.sections);
1231 for (i = 0; i < du.sections; i++)
1237 /* Look through all the symbols and try and work out the extents in this
1240 for (symbol = sfile->scope->vars_head;
1242 symbol = symbol->next)
1244 if (symbol->type->type == coff_secdef_type)
1246 unsigned int low = symbol->where->offset;
1247 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1248 struct coff_section *section = symbol->where->section;
1250 int sn = section->number;
1251 if (low < lowest[sn])
1253 if (high > highest[sn])
1258 for (i = 0; i < du.sections; i++)
1260 if (highest[i] == 0)
1261 lowest[i] = highest[i] = incit;
1264 du.length[used] = highest[i] - lowest[i];
1265 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1269 printf (" section %6s 0x%08x..0x%08x\n",
1270 p->sections[i + 1].name,
1279 for (j = 0; j < lim; j++)
1286 if (sfile->section[src].init)
1289 = sfile->section[src].high - sfile->section[src].low + 1;
1291 = sfile->section[src].low;
1296 du.address[dst] = 0;
1301 if (sfile->section[src].parent)
1303 printf (" section %6s 0x%08x..0x%08x\n",
1304 sfile->section[src].parent->name,
1306 du.address[dst] + du.length[dst] - 1);
1310 du.sections = dst + 1;
1316 sysroff_swap_du_out (file, &du);
1320 wr_dus (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile)
1325 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1326 dus.drb = nints (dus.ns);
1327 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1328 dus.spare = nints (dus.ns);
1330 /* Find the filenames. */
1334 for (sfile = p->source_head;
1336 sfile = sfile->next)
1340 dus.fname[i] = sfile->name;
1345 dus.fname[0] = sfile->name;
1348 sysroff_swap_dus_out (file, &dus);
1352 /* Find the offset of the .text section for this sfile in the
1353 .text section for the output file. */
1356 find_base (struct coff_sfile *sfile, struct coff_section *section)
1358 return sfile->section[section->number].low;
1362 wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
1363 int n ATTRIBUTE_UNUSED)
1368 /* Count up all the linenumbers */
1369 struct coff_symbol *sy;
1375 for (sy = p->symbol_list_head;
1377 sy = sy->next_in_ofile_list)
1379 struct coff_type *t = sy->type;
1380 if (t->type == coff_function_type)
1382 struct coff_line *l = t->u.function.lines;
1387 dln.sfn = nints (lc);
1388 dln.sln = nints (lc);
1389 dln.lln = nints (lc);
1390 dln.section = nints (lc);
1392 dln.from_address = nints (lc);
1393 dln.to_address = nints (lc);
1400 /* Run through once more and fill up the structure */
1402 for (sy = p->symbol_list_head;
1404 sy = sy->next_in_ofile_list)
1406 if (sy->type->type == coff_function_type)
1409 struct coff_line *l = sy->type->u.function.lines;
1410 for (i = 0; i < l->nlines; i++)
1412 dln.section[idx] = sy->where->section->number;
1414 dln.sln[idx] = l->lines[i];
1415 dln.from_address[idx] = l->addresses[i];
1417 dln.to_address[idx - 1] = dln.from_address[idx];
1423 sysroff_swap_dln_out (file, &dln);
1428 /* Count up all the linenumbers */
1430 struct coff_symbol *sy;
1436 for (sy = sfile->scope->vars_head;
1440 struct coff_type *t = sy->type;
1441 if (t->type == coff_function_type)
1443 struct coff_line *l = t->u.function.lines;
1449 dln.sfn = nints (lc);
1450 dln.sln = nints (lc);
1451 dln.cc = nints (lc);
1452 dln.section = nints (lc);
1454 dln.from_address = nints (lc);
1455 dln.to_address = nints (lc);
1462 /* Run through once more and fill up the structure */
1464 for (sy = sfile->scope->vars_head;
1468 if (sy->type->type == coff_function_type)
1471 struct coff_line *l = sy->type->u.function.lines;
1474 int base = find_base (sfile, sy->where->section);
1475 for (i = 0; i < l->nlines; i++)
1477 dln.section[idx] = sy->where->section->number;
1479 dln.sln[idx] = l->lines[i];
1480 dln.from_address[idx] =
1481 l->addresses[i] + sy->where->section->address - base;
1484 dln.to_address[idx - 1] = dln.from_address[idx];
1488 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1493 sysroff_swap_dln_out (file, &dln);
1497 /* Write the global symbols out to the debug info. */
1500 wr_globals (struct coff_ofile *p, struct coff_sfile *sfile,
1501 int n ATTRIBUTE_UNUSED)
1503 struct coff_symbol *sy;
1505 for (sy = p->symbol_list_head;
1507 sy = sy->next_in_ofile_list)
1509 if (sy->visible->type == coff_vis_ext_def
1510 || sy->visible->type == coff_vis_ext_ref)
1512 /* Only write out symbols if they belong to
1513 the current source file. */
1514 if (sy->sfile == sfile)
1515 walk_tree_symbol (sfile, 0, sy, 0);
1521 wr_debug (struct coff_ofile *p)
1523 struct coff_sfile *sfile;
1526 for (sfile = p->source_head;
1528 sfile = sfile->next)
1531 printf ("%s\n", sfile->name);
1533 wr_du (p, sfile, n);
1535 wr_program_structure (p, sfile);
1536 wr_dln (p, sfile, n);
1544 /* It seems that the CS struct is not normal - the size is wrong
1545 heres one I prepared earlier. */
1550 0x00, /* number of chars in variable length part */
1582 fwrite (b, 1, sizeof (b), file);
1585 /* Write out the SC records for a unit. Create an SC
1586 for all the sections which appear in the output file, even
1587 if there isn't an equivalent one on the input. */
1590 wr_sc (struct coff_ofile *ptr, struct coff_sfile *sfile)
1594 /* First work out the total number of sections. */
1595 int total_sec = ptr->nsections;
1598 struct coff_section *sec;
1599 struct coff_symbol *symbol;
1601 struct coff_symbol *symbol;
1603 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1606 for (i = 0; i < total_sec; i++)
1608 info[i].sec = ptr->sections + i;
1612 for (symbol = sfile->scope->vars_head;
1614 symbol = symbol->next)
1617 if (symbol->type->type == coff_secdef_type)
1619 for (i = 0; i < total_sec; i++)
1621 if (symbol->where->section == info[i].sec)
1623 info[i].symbol = symbol;
1630 /* Now output all the section info, and fake up some stuff for sections
1632 for (i = 1; i < total_sec; i++)
1637 symbol = info[i].symbol;
1643 /* Don't have a symbol set aside for this section, which means
1644 that nothing in this file does anything for the section. */
1645 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1648 name = info[i].sec->name;
1652 if (bfd_get_file_flags (abfd) & EXEC_P)
1655 sc.addr = symbol->where->offset;
1662 sc.length = symbol->type->size;
1663 name = symbol->name;
1667 sc.concat = CONCAT_SIMPLE;
1675 sc.spare1 = 0; /* If not zero, then it doesn't work. */
1676 sc.name = section_translate (name);
1678 if (strlen (sc.name) == 1)
1684 sc.contents = CONTENTS_DATA;
1688 sc.contents = CONTENTS_CODE;
1693 sc.contents = CONTENTS_CODE;
1700 sysroff_swap_sc_out (file, &sc);
1709 /* Write out the ER records for a unit. */
1712 wr_er (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1716 struct coff_symbol *sym;
1720 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1722 if (sym->visible->type == coff_vis_ext_ref)
1727 er.type = ER_NOTSPEC;
1728 er.name = sym->name;
1729 sysroff_swap_er_out (file, &er);
1730 sym->er_number = idx++;
1736 /* Write out the ED records for a unit. */
1739 wr_ed (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1742 struct coff_symbol *s;
1746 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1748 if (s->visible->type == coff_vis_ext_def
1749 || s->visible->type == coff_vis_common)
1753 ed.section = s->where->section->number;
1756 if (s->where->section->data)
1758 ed.type = ED_TYPE_DATA;
1760 else if (s->where->section->code & SEC_CODE)
1762 ed.type = ED_TYPE_ENTRY;
1766 ed.type = ED_TYPE_NOTSPEC;
1767 ed.type = ED_TYPE_DATA;
1770 ed.address = s->where->offset - s->where->section->address;
1772 sysroff_swap_ed_out (file, &ed);
1779 wr_unit_info (struct coff_ofile *ptr)
1781 struct coff_sfile *sfile;
1784 for (sfile = ptr->source_head;
1786 sfile = sfile->next)
1793 wr_un (ptr, sfile, first, 0);
1794 nsecs = wr_sc (ptr, sfile);
1796 fseek (file, p1, SEEK_SET);
1797 wr_un (ptr, sfile, first, nsecs);
1798 fseek (file, p2, SEEK_SET);
1799 wr_er (ptr, sfile, first);
1800 wr_ed (ptr, sfile, first);
1806 wr_module (struct coff_ofile *p)
1819 return (x + 3) & ~3;
1822 /* Find all the common variables and turn them into
1823 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1826 prescan (struct coff_ofile *tree)
1828 struct coff_symbol *s;
1829 struct coff_section *common_section;
1831 /* Find the common section - always section 3. */
1832 common_section = tree->sections + 3;
1834 for (s = tree->symbol_list_head;
1836 s = s->next_in_ofile_list)
1838 if (s->visible->type == coff_vis_common)
1840 struct coff_where *w = s->where;
1841 /* s->visible->type = coff_vis_ext_def; leave it as common */
1842 common_section->size = align (common_section->size);
1843 w->offset = common_section->size + common_section->address;
1844 w->section = common_section;
1845 common_section->size += s->type->size;
1846 common_section->size = align (common_section->size);
1854 show_usage (FILE *file, int status)
1856 fprintf (file, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
1857 fprintf (file, _("Convert a COFF object file into a SYSROFF object file\n"));
1858 fprintf (file, _(" The options are:\n\
1859 -q --quick (Obsolete - ignoerd)\n\
1860 -n --noprescan Do not perform a scan to convert commons into defs\n\
1861 -d --debug Display information about what is being done\n\
1862 -h --help Display this information\n\
1863 -v --version Print the program's version number\n"));
1866 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1871 main (int ac, char **av)
1874 static struct option long_options[] =
1876 {"debug", no_argument, 0, 'd'},
1877 {"quick", no_argument, 0, 'q'},
1878 {"noprescan", no_argument, 0, 'n'},
1879 {"help", no_argument, 0, 'h'},
1880 {"version", no_argument, 0, 'V'},
1881 {NULL, no_argument, 0, 0}
1887 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1888 setlocale (LC_MESSAGES, "");
1890 #if defined (HAVE_SETLOCALE)
1891 setlocale (LC_CTYPE, "");
1893 bindtextdomain (PACKAGE, LOCALEDIR);
1894 textdomain (PACKAGE);
1896 program_name = av[0];
1897 xmalloc_set_program_name (program_name);
1899 while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
1916 show_usage (stdout, 0);
1920 print_version ("srconv");
1926 show_usage (stderr, 1);
1931 /* The input and output files may be named on the command line. */
1935 input_file = av[optind];
1939 output_file = av[optind];
1942 show_usage (stderr, 1);
1943 if (strcmp (input_file, output_file) == 0)
1945 fatal (_("input and output files must be different"));
1954 fatal (_("no input file specified"));
1959 /* Take a .o off the input file and stick on a .obj. If
1960 it doesn't end in .o, then stick a .obj on anyway */
1962 int len = strlen (input_file);
1964 output_file = xmalloc (len + 5);
1965 strcpy (output_file, input_file);
1968 && output_file[len - 2] == '.'
1969 && output_file[len - 1] == 'o')
1971 output_file[len] = 'b';
1972 output_file[len + 1] = 'j';
1973 output_file[len + 2] = 0;
1977 strcat (output_file, ".obj");
1981 abfd = bfd_openr (input_file, 0);
1984 bfd_fatal (input_file);
1986 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1988 bfd_nonfatal (input_file);
1990 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1992 list_matching_formats (matching);
1998 file = fopen (output_file, FOPEN_WB);
2001 fatal (_("unable to open output file %s"), output_file);
2004 printf ("ids %d %d\n", base1, base2);
2006 tree = coff_grok (abfd);