1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001
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
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 PARAMS ((int));
46 static int get_ordinary_id PARAMS ((int));
47 static char *section_translate PARAMS ((char *));
48 static char *strip_suffix PARAMS ((char *));
49 static void checksum PARAMS ((FILE *, char *, int, int));
50 static void writeINT PARAMS ((int, char *, int *, int, FILE *));
51 static void writeBITS PARAMS ((int, char *, int *, int));
52 static void writeBARRAY PARAMS ((barray, char *, int *, int, FILE *));
53 static void writeCHARS PARAMS ((char *, char *, int *, int, FILE *));
54 static void wr_tr PARAMS ((void));
55 static void wr_un PARAMS ((struct coff_ofile *, struct coff_sfile *, int, int));
56 static void wr_hd PARAMS ((struct coff_ofile *));
57 static void wr_sh PARAMS ((struct coff_ofile *, struct coff_section *));
58 static void wr_ob PARAMS ((struct coff_ofile *, struct coff_section *));
59 static void wr_rl PARAMS ((struct coff_ofile *, struct coff_section *));
60 static void wr_object_body PARAMS ((struct coff_ofile *));
61 static void wr_dps_start
62 PARAMS ((struct coff_sfile *, struct coff_section *, struct coff_scope *,
64 static void wr_dps_end
65 PARAMS ((struct coff_section *, struct coff_scope *, int));
66 static int *nints PARAMS ((int));
67 static void walk_tree_type_1
68 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
70 static void walk_tree_type
71 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
73 static void walk_tree_symbol
74 PARAMS ((struct coff_sfile *, struct coff_section *,
75 struct coff_symbol *, int));
76 static void walk_tree_scope
77 PARAMS ((struct coff_section *, struct coff_sfile *, struct coff_scope *,
79 static void walk_tree_sfile
80 PARAMS ((struct coff_section *, struct coff_sfile *));
81 static void wr_program_structure
82 PARAMS ((struct coff_ofile *, struct coff_sfile *));
83 static void wr_du PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
84 static void wr_dus PARAMS ((struct coff_ofile *, struct coff_sfile *));
85 static int find_base PARAMS ((struct coff_sfile *, struct coff_section *));
86 static void wr_dln PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
87 static void wr_globals
88 PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
89 static void wr_debug PARAMS ((struct coff_ofile *));
90 static void wr_cs PARAMS ((void));
91 static int wr_sc PARAMS ((struct coff_ofile *, struct coff_sfile *));
92 static void wr_er PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
93 static void wr_ed PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
94 static void wr_unit_info PARAMS ((struct coff_ofile *));
95 static void wr_module PARAMS ((struct coff_ofile *));
96 static int align PARAMS ((int));
97 static void prescan PARAMS ((struct coff_ofile *));
98 static void show_usage PARAMS ((FILE *, int));
99 static void show_help PARAMS ((void));
100 extern int main PARAMS ((int, char **));
104 static int debug = 0;
105 static int quick = 0;
106 static int noprescan = 0;
107 static struct coff_ofile *tree;
109 static int absolute_p;
112 static int segmented_p;
115 static int ids1[20000];
116 static int ids2[20000];
118 static int base1 = 0x18;
119 static int base2 = 0x2018;
145 section_translate (n)
148 if (strcmp (n, ".text") == 0)
150 if (strcmp (n, ".data") == 0)
152 if (strcmp (n, ".bss") == 0)
159 #define DATE "940201073000"; /* Just a time on my birthday */
169 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
171 res = (char *) xmalloc (i + 1);
172 memcpy (res, name, i);
178 /* IT LEN stuff CS */
180 checksum (file, ptr, size, code)
189 int bytes = size / 8;
190 last = !(code & 0xff00);
193 ptr[0] = code | (last ? 0x80 : 0);
196 for (j = 0; j < bytes; j++)
200 /* Glue on a checksum too */
202 fwrite (ptr, bytes + 1, 1, file);
209 writeINT (n, ptr, idx, size, file)
225 /* Lets write out that record and do another one */
226 checksum (file, ptr, *idx, code | 0x1000);
238 ptr[byte + 0] = n >> 8;
242 ptr[byte + 0] = n >> 24;
243 ptr[byte + 1] = n >> 16;
244 ptr[byte + 2] = n >> 8;
245 ptr[byte + 3] = n >> 0;
255 writeBITS (val, ptr, idx, size)
267 /* Turn off all about to change bits */
268 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
269 /* Turn on the bits we want */
270 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
275 writeBARRAY (data, ptr, idx, size, file)
279 int size ATTRIBUTE_UNUSED;
283 writeINT (data.len, ptr, idx, 1, file);
284 for (i = 0; i < data.len; i++)
286 writeINT (data.data[i], ptr, idx, 1, file);
292 writeCHARS (string, ptr, idx, size, file)
303 /* Lets write out that record and do another one */
304 checksum (file, ptr, *idx, code | 0x1000);
311 /* Variable length string */
312 size = strlen (string);
316 /* BUG WAITING TO HAPPEN */
317 memcpy (ptr + i, string, size);
322 #define SYSROFF_SWAP_OUT
326 static char *rname_sh[] =
328 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
331 static char *rname_h8300[] =
333 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
339 /* The TR block is not normal - it doesn't have any contents. */
346 fwrite (b, 1, sizeof (b), file);
350 wr_un (ptr, sfile, first, nsecs)
351 struct coff_ofile *ptr;
352 struct coff_sfile *sfile;
354 int nsecs ATTRIBUTE_UNUSED;
358 struct coff_symbol *s;
362 if (bfd_get_file_flags (abfd) & EXEC_P)
363 un.format = FORMAT_LM;
365 un.format = FORMAT_OM;
370 un.nsections = ptr->nsections - 1; /* Don't count the abs section */
372 /*NEW - only count sections with size */
373 un.nsections = nsecs;
378 /* Count all the undefined and defined variables with global scope */
382 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
384 if (s->visible->type == coff_vis_ext_def
385 || s->visible->type == coff_vis_common)
388 if (s->visible->type == coff_vis_ext_ref)
394 un.linker = "L_GX00";
396 un.name = sfile->name;
397 sysroff_swap_un_out (file, &un);
403 struct coff_ofile *p;
408 if (bfd_get_file_flags (abfd) & EXEC_P)
410 hd.mt = MTYPE_ABS_LM;
414 hd.mt = MTYPE_OMS_OR_LMS;
418 hd.nu = p->nsources; /* Always one unit */
419 hd.code = 0; /* Always ASCII */
420 hd.ver = "0200"; /* Version 2.00 */
421 switch (bfd_get_arch (abfd))
429 switch (bfd_get_mach (abfd))
435 toolname = "C_H8/300";
437 case bfd_mach_h8300h:
441 toolname = "C_H8/300H";
443 case bfd_mach_h8300s:
447 toolname = "C_H8/300S";
452 rnames = rname_h8300;
470 if (! bfd_get_file_flags(abfd) & EXEC_P)
480 hd.address = bfd_get_start_address (abfd);
485 hd.mn = strip_suffix (bfd_get_filename (abfd));
487 sysroff_swap_hd_out (file, &hd);
493 struct coff_ofile *p ATTRIBUTE_UNUSED;
494 struct coff_section *sec;
498 sh.section = sec->number;
502 sysroff_swap_sh_out (file, &sh);
508 struct coff_ofile *p ATTRIBUTE_UNUSED;
509 struct coff_section *section;
513 unsigned char stuff[200];
516 while (i < section->bfd_section->_raw_size)
519 int todo = 200; /* Copy in 200 byte lumps */
521 if (i + todo > section->bfd_section->_raw_size)
522 todo = section->bfd_section->_raw_size - i;
527 if (bfd_get_file_flags (abfd) & EXEC_P)
528 ob.address = section->address;
539 ob.cpf = 0; /* Never compress */
541 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
542 ob.data.data = stuff;
543 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
546 /* Now fill the rest with blanks */
547 while (i < (bfd_size_type) section->size)
550 int todo = 200; /* Copy in 200 byte lumps */
552 if (i + todo > (bfd_size_type) section->size)
553 todo = section->size - i;
556 ob.cpf = 0; /* Never compress */
558 memset (stuff, 0, todo);
559 ob.data.data = stuff;
560 sysroff_swap_ob_out (file, &ob);
563 /* Now fill the rest with blanks */
569 struct coff_ofile *ptr ATTRIBUTE_UNUSED;
570 struct coff_section *sec;
572 int nr = sec->nrelocs;
574 for (i = 0; i < nr; i++)
576 struct coff_reloc *r = sec->relocs + i;
577 struct coff_symbol *ref;
586 rl.flen = 32; /* SH Specific */
587 /* What sort of reloc ? Look in the section to find out */
589 if (ref->visible->type == coff_vis_ext_ref)
591 rl.bcount = 4; /* Always 4 for us */
593 rl.symn = ref->er_number;
595 else if (ref->visible->type == coff_vis_common)
597 rl.bcount = 11; /* Always 11 for us */
599 rl.secn = ref->where->section->number;
602 rl.addend = ref->where->offset - ref->where->section->address;
603 rl.aopcode_is_0x20 = 0x20;
608 rl.bcount = 11; /* Always 11 for us */
610 rl.secn = ref->where->section->number;
613 rl.addend = -ref->where->section->address;
614 rl.aopcode_is_0x20 = 0x20;
617 if (rl.op == OP_SEC_REF
618 || rl.op == OP_EXT_REF)
620 sysroff_swap_rl_out (file, &rl);
627 struct coff_ofile *p;
630 for (i = 1; i < p->nsections; i++)
632 wr_sh (p, p->sections + i);
633 wr_ob (p, p->sections + i);
634 wr_rl (p, p->sections + i);
639 wr_dps_start (sfile, section, scope, type, nest)
640 struct coff_sfile *sfile;
641 struct coff_section *section ATTRIBUTE_UNUSED;
642 struct coff_scope *scope;
652 dps.san = scope->sec->number;
653 dps.address = scope->offset - find_base (sfile, scope->sec);
654 dps.block_size = scope->size;
657 printf ("DPS %s %d %x\n",
673 sysroff_swap_dps_out (file, &dps);
677 wr_dps_end (section, scope, type)
678 struct coff_section *section ATTRIBUTE_UNUSED;
679 struct coff_scope *scope ATTRIBUTE_UNUSED;
685 sysroff_swap_dps_out (file, &dps);
692 return (int *) (xcalloc (sizeof (int), x));
696 walk_tree_type_1 (sfile, symbol, type, nest)
697 struct coff_sfile *sfile;
698 struct coff_symbol *symbol;
699 struct coff_type *type;
704 case coff_secdef_type:
705 case coff_basic_type:
709 switch (type->u.basic)
713 dbt.btype = BTYPE_VOID;
714 dbt.sign = BTYPE_UNSPEC;
715 dbt.fptype = FPTYPE_NOTSPEC;
718 dbt.btype = BTYPE_CHAR;
719 dbt.sign = BTYPE_UNSPEC;
720 dbt.fptype = FPTYPE_NOTSPEC;
725 dbt.btype = BTYPE_INT;
726 dbt.sign = SIGN_SIGNED;
727 dbt.fptype = FPTYPE_NOTSPEC;
730 dbt.btype = BTYPE_FLOAT;
731 dbt.fptype = FPTYPE_SINGLE;
734 dbt.btype = BTYPE_FLOAT;
735 dbt.fptype = FPTYPE_DOUBLE;
738 dbt.btype = BTYPE_FLOAT;
739 dbt.fptype = FPTYPE_EXTENDED;
742 dbt.btype = BTYPE_CHAR;
743 dbt.sign = SIGN_UNSIGNED;
744 dbt.fptype = FPTYPE_NOTSPEC;
749 dbt.btype = BTYPE_INT;
750 dbt.sign = SIGN_UNSIGNED;
751 dbt.fptype = FPTYPE_NOTSPEC;
754 dbt.bitsize = type->size;
756 sysroff_swap_dbt_out (file, &dbt);
759 case coff_pointer_type:
762 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
764 sysroff_swap_dpt_out (file, &dpt);
768 case coff_function_type:
771 struct coff_symbol *param;
774 dfp.nparams = type->u.function.parameters->nvars;
777 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
779 sysroff_swap_dfp_out (file, &dfp);
781 for (param = type->u.function.parameters->vars_head;
785 walk_tree_symbol (sfile, 0, param, nest);
788 sysroff_swap_dfp_out (file, &dfp);
792 case coff_structdef_type:
796 struct coff_symbol *member;
798 dbt.btype = BTYPE_STRUCT;
799 dbt.bitsize = type->size;
800 dbt.sign = SIGN_UNSPEC;
801 dbt.fptype = FPTYPE_NOTSPEC;
802 dbt.sid = get_member_id (type->u.astructdef.idx);
804 sysroff_swap_dbt_out (file, &dbt);
807 sysroff_swap_dds_out (file, &dds);
808 for (member = type->u.astructdef.elements->vars_head;
810 member = member->next)
812 walk_tree_symbol (sfile, 0, member, nest + 1);
816 sysroff_swap_dds_out (file, &dds);
820 case coff_structref_type:
823 dbt.btype = BTYPE_TAG;
824 dbt.bitsize = type->size;
825 dbt.sign = SIGN_UNSPEC;
826 dbt.fptype = FPTYPE_NOTSPEC;
827 if (type->u.astructref.ref)
829 dbt.sid = get_member_id (type->u.astructref.ref->number);
837 sysroff_swap_dbt_out (file, &dbt);
840 case coff_array_type:
844 int dims = 1; /* Only output one dimension at a time */
846 dar.variable = nints (dims);
847 dar.subtype = nints (dims);
848 dar.spare = nints (dims);
849 dar.max_variable = nints (dims);
850 dar.maxspare = nints (dims);
851 dar.max = nints (dims);
852 dar.min_variable = nints (dims);
853 dar.min = nints (dims);
854 dar.minspare = nints (dims);
856 dar.length = type->size / type->u.array.dim;
857 for (j = 0; j < dims; j++)
859 dar.variable[j] = VARIABLE_FIXED;
860 dar.subtype[j] = SUB_INTEGER;
862 dar.max_variable[j] = 0;
863 dar.max[j] = type->u.array.dim;
864 dar.min_variable[j] = 0;
865 dar.min[j] = 1; /* Why isn't this 0 ? */
867 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
868 sysroff_swap_dar_out (file, &dar);
871 case coff_enumdef_type:
875 struct coff_symbol *member;
876 dbt.btype = BTYPE_ENUM;
877 dbt.bitsize = type->size;
878 dbt.sign = SIGN_UNSPEC;
879 dbt.fptype = FPTYPE_NOTSPEC;
880 dbt.sid = get_member_id (type->u.aenumdef.idx);
882 sysroff_swap_dbt_out (file, &dbt);
887 sysroff_swap_den_out (file, &den);
888 for (member = type->u.aenumdef.elements->vars_head;
890 member = member->next)
892 walk_tree_symbol (sfile, 0, member, nest + 1);
896 sysroff_swap_den_out (file, &den);
901 case coff_enumref_type:
904 dbt.btype = BTYPE_TAG;
905 dbt.bitsize = type->size;
906 dbt.sign = SIGN_UNSPEC;
907 dbt.fptype = FPTYPE_NOTSPEC;
908 dbt.sid = get_member_id (type->u.aenumref.ref->number);
910 sysroff_swap_dbt_out (file, &dbt);
926 sysroff_swap_dty_out (file, &dty);
936 sysroff_swap_dty_out (file, &dty);
941 dump_tree_structure (sfile, symbol, type, nest)
942 struct coff_sfile *sfile;
943 struct coff_symbol *symbol;
944 struct coff_type *type;
947 if (symbol->type->type == coff_function_type)
957 walk_tree_type (sfile, symbol, type, nest)
961 struct coff_symbol *symbol;
962 struct coff_type *type;
965 if (symbol->type->type == coff_function_type)
972 sysroff_swap_dty_out (file, &dty);
973 walk_tree_type_1 (sfile, symbol, type, nest);
975 sysroff_swap_dty_out (file, &dty);
978 symbol->where->section,
979 symbol->type->u.function.code,
980 BLOCK_TYPE_FUNCTION, nest);
981 wr_dps_start (sfile, symbol->where->section,
982 symbol->type->u.function.code,
983 BLOCK_TYPE_BLOCK, nest);
984 walk_tree_scope (symbol->where->section,
986 symbol->type->u.function.code,
987 nest + 1, BLOCK_TYPE_BLOCK);
989 wr_dps_end (symbol->where->section,
990 symbol->type->u.function.code,
992 wr_dps_end (symbol->where->section,
993 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
1001 sysroff_swap_dty_out (file, &dty);
1002 walk_tree_type_1 (sfile, symbol, type, nest);
1004 sysroff_swap_dty_out (file, &dty);
1012 walk_tree_symbol (sfile, section, symbol, nest)
1013 struct coff_sfile *sfile;
1014 struct coff_section *section ATTRIBUTE_UNUSED;
1015 struct coff_symbol *symbol;
1020 memset(&dsy, 0, sizeof(dsy));
1023 switch (symbol->type->type)
1025 case coff_function_type:
1026 dsy.type = STYPE_FUNC;
1029 case coff_structref_type:
1030 case coff_pointer_type:
1031 case coff_array_type:
1032 case coff_basic_type:
1033 case coff_enumref_type:
1034 dsy.type = STYPE_VAR;
1037 case coff_enumdef_type:
1038 dsy.type = STYPE_TAG;
1042 case coff_structdef_type:
1043 dsy.type = STYPE_TAG;
1045 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
1047 case coff_secdef_type:
1053 if (symbol->where->where == coff_where_member_of_struct)
1056 dsy.type = STYPE_MEMBER;
1058 if (symbol->where->where == coff_where_member_of_enum)
1060 dsy.type = STYPE_ENUM;
1063 dsy.evalue = symbol->where->offset;
1066 if (symbol->type->type == coff_structdef_type
1067 || symbol->where->where == coff_where_entag
1068 || symbol->where->where == coff_where_strtag)
1070 dsy.snumber = get_member_id (symbol->number);
1074 dsy.snumber = get_ordinary_id (symbol->number);
1078 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1080 switch (symbol->visible->type)
1082 case coff_vis_common:
1083 case coff_vis_ext_def:
1084 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1086 case coff_vis_ext_ref:
1087 dsy.ainfo = AINFO_STATIC_EXT_REF;
1089 case coff_vis_int_def:
1090 dsy.ainfo = AINFO_STATIC_INT;
1093 case coff_vis_autoparam:
1094 dsy.ainfo = AINFO_AUTO;
1096 case coff_vis_register:
1097 case coff_vis_regparam:
1098 dsy.ainfo = AINFO_REG;
1102 case coff_vis_member_of_struct:
1103 case coff_vis_member_of_enum:
1109 dsy.dlength = symbol->type->size;
1110 switch (symbol->where->where)
1112 case coff_where_memory:
1114 dsy.section = symbol->where->section->number;
1119 case coff_where_member_of_struct:
1120 case coff_where_member_of_enum:
1121 case coff_where_stack:
1122 case coff_where_register:
1123 case coff_where_unknown:
1124 case coff_where_strtag:
1126 case coff_where_entag:
1127 case coff_where_typedef:
1133 switch (symbol->where->where)
1135 case coff_where_memory:
1136 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1138 case coff_where_stack:
1139 dsy.address = symbol->where->offset;
1141 case coff_where_member_of_struct:
1144 if (symbol->where->bitsize)
1146 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1148 dsy.field_len = symbol->where->bitsize;
1149 dsy.field_off = (bits / 32) * 4;
1150 dsy.field_bitoff = bits % 32;
1156 dsy.field_len = symbol->type->size;
1157 dsy.field_off = symbol->where->offset;
1160 case coff_where_member_of_enum:
1162 dsy.field_len = symbol->type->size;
1163 dsy.field_off = symbol->where->offset; */
1165 case coff_where_register:
1166 case coff_where_unknown:
1167 case coff_where_strtag:
1169 case coff_where_entag:
1170 case coff_where_typedef:
1176 if (symbol->where->where == coff_where_register)
1177 dsy.reg = rnames[symbol->where->offset];
1179 switch (symbol->visible->type)
1181 case coff_vis_common:
1182 /* We do this 'cause common C symbols are treated as extdefs */
1183 case coff_vis_ext_def:
1184 case coff_vis_ext_ref:
1186 dsy.ename = symbol->name;
1189 case coff_vis_regparam:
1190 case coff_vis_autoparam:
1191 dsy.type = STYPE_PARAMETER;
1194 case coff_vis_int_def:
1197 case coff_vis_register:
1199 case coff_vis_member_of_struct:
1200 case coff_vis_member_of_enum:
1212 sysroff_swap_dsy_out (file, &dsy);
1214 walk_tree_type (sfile, symbol, symbol->type, nest);
1219 walk_tree_scope (section, sfile, scope, nest, type)
1220 struct coff_section *section;
1221 struct coff_sfile *sfile;
1222 struct coff_scope *scope;
1226 struct coff_symbol *vars;
1227 struct coff_scope *child;
1229 if (scope->vars_head
1230 || (scope->list_head && scope->list_head->vars_head))
1232 wr_dps_start (sfile, section, scope, type, nest);
1235 wr_globals (tree, sfile, nest + 1);
1237 for (vars = scope->vars_head; vars; vars = vars->next)
1239 walk_tree_symbol (sfile, section, vars, nest);
1242 for (child = scope->list_head; child; child = child->next)
1244 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1247 wr_dps_end (section, scope, type);
1251 walk_tree_sfile (section, sfile)
1252 struct coff_section *section;
1253 struct coff_sfile *sfile;
1255 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1260 wr_program_structure (p, sfile)
1261 struct coff_ofile *p;
1262 struct coff_sfile *sfile;
1265 walk_tree_sfile (p->sections + 4, sfile);
1271 struct coff_ofile *p;
1272 struct coff_sfile *sfile;
1278 struct coff_symbol *symbol;
1279 static int incit = 0x500000;
1284 unsigned int *lowest = (unsigned *) nints (p->nsections);
1285 unsigned int *highest = (unsigned *) nints (p->nsections);
1286 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1291 du.sections = p->nsections - 1;
1292 du.san = (int *) xcalloc (sizeof (int), du.sections);
1293 du.address = nints (du.sections);
1294 du.length = nints (du.sections);
1296 for (i = 0; i < du.sections; i++)
1302 /* Look through all the symbols and try and work out the extents in this
1305 for (symbol = sfile->scope->vars_head;
1307 symbol = symbol->next)
1309 if (symbol->type->type == coff_secdef_type)
1311 unsigned int low = symbol->where->offset;
1312 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1313 struct coff_section *section = symbol->where->section;
1315 int sn = section->number;
1316 if (low < lowest[sn])
1318 if (high > highest[sn])
1324 for (i = 0; i < du.sections; i++)
1326 if (highest[i] == 0)
1328 lowest[i] = highest[i] = incit;
1331 du.length[used] = highest[i] - lowest[i];
1332 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1335 printf (" section %6s 0x%08x..0x%08x\n",
1336 p->sections[i + 1].name,
1345 for (j = 0; j < lim; j++)
1350 if (sfile->section[src].init)
1353 = sfile->section[src].high - sfile->section[src].low + 1;
1355 = sfile->section[src].low;
1360 du.address[dst] = 0;
1364 if (sfile->section[src].parent)
1366 printf (" section %6s 0x%08x..0x%08x\n",
1367 sfile->section[src].parent->name,
1369 du.address[dst] + du.length[dst] - 1);
1372 du.sections = dst + 1;
1378 sysroff_swap_du_out (file, &du);
1383 struct coff_ofile *p ATTRIBUTE_UNUSED;
1384 struct coff_sfile *sfile;
1390 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1391 dus.drb = nints (dus.ns);
1392 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1393 dus.spare = nints (dus.ns);
1395 /* Find the filenames */
1399 for (sfile = p->source_head;
1401 sfile = sfile->next)
1405 dus.fname[i] = sfile->name;
1410 dus.fname[0] = sfile->name;
1413 sysroff_swap_dus_out (file, &dus);
1417 /* Find the offset of the .text section for this sfile in the
1418 .text section for the output file */
1421 find_base (sfile, section)
1422 struct coff_sfile *sfile;
1423 struct coff_section *section;
1425 return sfile->section[section->number].low;
1429 wr_dln (p, sfile, n)
1430 struct coff_ofile *p ATTRIBUTE_UNUSED;
1431 struct coff_sfile *sfile;
1432 int n ATTRIBUTE_UNUSED;
1438 /* Count up all the linenumbers */
1439 struct coff_symbol *sy;
1445 for (sy = p->symbol_list_head;
1447 sy = sy->next_in_ofile_list)
1449 struct coff_type *t = sy->type;
1450 if (t->type == coff_function_type)
1452 struct coff_line *l = t->u.function.lines;
1457 dln.sfn = nints (lc);
1458 dln.sln = nints (lc);
1459 dln.lln = nints (lc);
1460 dln.section = nints (lc);
1462 dln.from_address = nints (lc);
1463 dln.to_address = nints (lc);
1470 /* Run through once more and fill up the structure */
1472 for (sy = p->symbol_list_head;
1474 sy = sy->next_in_ofile_list)
1476 if (sy->type->type == coff_function_type)
1479 struct coff_line *l = sy->type->u.function.lines;
1480 for (i = 0; i < l->nlines; i++)
1482 dln.section[idx] = sy->where->section->number;
1484 dln.sln[idx] = l->lines[i];
1485 dln.from_address[idx] = l->addresses[i];
1487 dln.to_address[idx - 1] = dln.from_address[idx];
1493 sysroff_swap_dln_out (file, &dln);
1498 /* Count up all the linenumbers */
1500 struct coff_symbol *sy;
1506 for (sy = sfile->scope->vars_head;
1510 struct coff_type *t = sy->type;
1511 if (t->type == coff_function_type)
1513 struct coff_line *l = t->u.function.lines;
1519 dln.sfn = nints (lc);
1520 dln.sln = nints (lc);
1521 dln.cc = nints (lc);
1522 dln.section = nints (lc);
1524 dln.from_address = nints (lc);
1525 dln.to_address = nints (lc);
1532 /* Run through once more and fill up the structure */
1534 for (sy = sfile->scope->vars_head;
1538 if (sy->type->type == coff_function_type)
1541 struct coff_line *l = sy->type->u.function.lines;
1544 int base = find_base (sfile, sy->where->section);
1545 for (i = 0; i < l->nlines; i++)
1547 dln.section[idx] = sy->where->section->number;
1549 dln.sln[idx] = l->lines[i];
1550 dln.from_address[idx] =
1551 l->addresses[i] + sy->where->section->address - base;
1554 dln.to_address[idx - 1] = dln.from_address[idx];
1558 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1563 sysroff_swap_dln_out (file, &dln);
1567 /* Write the global symbols out to the debug info */
1569 wr_globals (p, sfile, n)
1570 struct coff_ofile *p;
1571 struct coff_sfile *sfile;
1572 int n ATTRIBUTE_UNUSED;
1574 struct coff_symbol *sy;
1575 for (sy = p->symbol_list_head;
1577 sy = sy->next_in_ofile_list)
1579 if (sy->visible->type == coff_vis_ext_def
1580 || sy->visible->type == coff_vis_ext_ref)
1582 /* Only write out symbols if they belong to
1583 the current source file */
1584 if (sy->sfile == sfile)
1585 walk_tree_symbol (sfile, 0, sy, 0);
1593 struct coff_ofile *p;
1595 struct coff_sfile *sfile;
1597 for (sfile = p->source_head;
1599 sfile = sfile->next)
1604 printf ("%s\n", sfile->name);
1606 wr_du (p, sfile, n);
1608 wr_program_structure (p, sfile);
1609 wr_dln (p, sfile, n);
1617 /* It seems that the CS struct is not normal - the size is wrong
1618 heres one I prepared earlier.. */
1622 0x00, /* number of chars in variable length part */
1654 fwrite (b, 1, sizeof (b), file);
1657 /* Write out the SC records for a unit. Create an SC
1658 for all the sections which appear in the output file, even
1659 if there isn't an equivalent one on the input */
1663 struct coff_ofile *ptr;
1664 struct coff_sfile *sfile;
1668 /* First work out the total number of sections */
1670 int total_sec = ptr->nsections;
1674 struct coff_section *sec;
1675 struct coff_symbol *symbol;
1677 struct coff_symbol *symbol;
1680 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1684 for (i = 0; i < total_sec; i++)
1686 info[i].sec = ptr->sections + i;
1690 for (symbol = sfile->scope->vars_head;
1692 symbol = symbol->next)
1695 if (symbol->type->type == coff_secdef_type)
1697 for (i = 0; i < total_sec; i++)
1699 if (symbol->where->section == info[i].sec)
1701 info[i].symbol = symbol;
1708 /* Now output all the section info, and fake up some stuff for sections
1711 for (i = 1; i < total_sec; i++)
1715 symbol = info[i].symbol;
1720 /* Don't have a symbol set aside for this section, which means that nothing
1721 in this file does anything for the section. */
1722 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1725 name = info[i].sec->name;
1729 if (bfd_get_file_flags (abfd) & EXEC_P)
1732 sc.addr = symbol->where->offset;
1739 sc.length = symbol->type->size;
1740 name = symbol->name;
1745 sc.concat = CONCAT_SIMPLE;
1753 sc.spare1 = 0; /* If not zero, then it doesn't work */
1754 sc.name = section_translate (name);
1755 if (strlen (sc.name) == 1)
1761 sc.contents = CONTENTS_DATA;
1764 sc.contents = CONTENTS_CODE;
1769 sc.contents = CONTENTS_CODE;
1775 sysroff_swap_sc_out (file, &sc);
1785 /* Write out the ER records for a unit. */
1787 wr_er (ptr, sfile, first)
1788 struct coff_ofile *ptr;
1789 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1793 struct coff_symbol *sym;
1796 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1798 if (sym->visible->type == coff_vis_ext_ref)
1802 er.type = ER_NOTSPEC;
1803 er.name = sym->name;
1804 sysroff_swap_er_out (file, &er);
1805 sym->er_number = idx++;
1811 /* Write out the ED records for a unit. */
1813 wr_ed (ptr, sfile, first)
1814 struct coff_ofile *ptr;
1815 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1818 struct coff_symbol *s;
1821 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1823 if (s->visible->type == coff_vis_ext_def
1824 || s->visible->type == coff_vis_common)
1828 ed.section = s->where->section->number;
1830 if (s->where->section->data)
1832 ed.type = ED_TYPE_DATA;
1834 else if (s->where->section->code & SEC_CODE)
1836 ed.type = ED_TYPE_ENTRY;
1840 ed.type = ED_TYPE_NOTSPEC;
1841 ed.type = ED_TYPE_DATA;
1843 ed.address = s->where->offset - s->where->section->address;
1845 sysroff_swap_ed_out (file, &ed);
1853 struct coff_ofile *ptr;
1855 struct coff_sfile *sfile;
1857 for (sfile = ptr->source_head;
1859 sfile = sfile->next)
1865 wr_un (ptr, sfile, first, 0);
1866 nsecs = wr_sc (ptr, sfile);
1868 fseek (file, p1, SEEK_SET);
1869 wr_un (ptr, sfile, first, nsecs);
1870 fseek (file, p2, SEEK_SET);
1871 wr_er (ptr, sfile, first);
1872 wr_ed (ptr, sfile, first);
1879 struct coff_ofile *p;
1893 return (x + 3) & ~3;
1896 /* Find all the common variables and turn them into
1897 ordinary defs - dunno why, but thats what hitachi does with 'em */
1901 struct coff_ofile *tree;
1903 struct coff_symbol *s;
1904 struct coff_section *common_section;
1905 /* Find the common section - always section 3 */
1906 common_section = tree->sections + 3;
1907 for (s = tree->symbol_list_head;
1909 s = s->next_in_ofile_list)
1911 if (s->visible->type == coff_vis_common)
1913 struct coff_where *w = s->where;
1914 /* s->visible->type = coff_vis_ext_def; leave it as common */
1915 common_section->size = align (common_section->size);
1916 w->offset = common_section->size + common_section->address;
1917 w->section = common_section;
1918 common_section->size += s->type->size;
1919 common_section->size = align (common_section->size);
1927 show_usage (file, status)
1931 fprintf (file, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name);
1938 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1940 show_usage (stdout, 0);
1951 static struct option long_options[] =
1953 {"debug", no_argument, 0, 'd'},
1954 {"quick", no_argument, 0, 'q'},
1955 {"noprescan", no_argument, 0, 'n'},
1956 {"help", no_argument, 0, 'h'},
1957 {"version", no_argument, 0, 'V'},
1958 {NULL, no_argument, 0, 0}
1964 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1965 setlocale (LC_MESSAGES, "");
1967 #if defined (HAVE_SETLOCALE)
1968 setlocale (LC_CTYPE, "");
1970 bindtextdomain (PACKAGE, LOCALEDIR);
1971 textdomain (PACKAGE);
1973 program_name = av[0];
1974 xmalloc_set_program_name (program_name);
1976 while ((opt = getopt_long (ac, av, "dhVqn", long_options,
1995 print_version ("srconv");
2001 show_usage (stderr, 1);
2006 /* The input and output files may be named on the command line. */
2010 input_file = av[optind];
2014 output_file = av[optind];
2017 show_usage (stderr, 1);
2018 if (strcmp (input_file, output_file) == 0)
2020 fatal (_("input and output files must be different"));
2029 fatal (_("no input file specified"));
2034 /* Take a .o off the input file and stick on a .obj. If
2035 it doesn't end in .o, then stick a .obj on anyway */
2037 int len = strlen (input_file);
2038 output_file = xmalloc (len + 5);
2039 strcpy (output_file, input_file);
2041 && output_file[len - 2] == '.'
2042 && output_file[len - 1] == 'o')
2044 output_file[len] = 'b';
2045 output_file[len + 1] = 'j';
2046 output_file[len + 2] = 0;
2050 strcat (output_file, ".obj");
2054 abfd = bfd_openr (input_file, 0);
2057 bfd_fatal (input_file);
2059 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2061 bfd_nonfatal (input_file);
2062 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2064 list_matching_formats (matching);
2070 file = fopen (output_file, FOPEN_WB);
2074 fatal (_("unable to open output file %s"), output_file);
2078 printf ("ids %d %d\n", base1, base2);
2079 tree = coff_grok (abfd);