1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002
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 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 extern int main PARAMS ((int, char **));
103 static int debug = 0;
104 static int quick = 0;
105 static int noprescan = 0;
106 static struct coff_ofile *tree;
108 static int absolute_p;
111 static int segmented_p;
114 static int ids1[20000];
115 static int ids2[20000];
117 static int base1 = 0x18;
118 static int base2 = 0x2018;
142 section_translate (n)
145 if (strcmp (n, ".text") == 0)
147 if (strcmp (n, ".data") == 0)
149 if (strcmp (n, ".bss") == 0)
154 #define DATE "940201073000"; /* Just a time on my birthday */
164 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
166 res = (char *) xmalloc (i + 1);
167 memcpy (res, name, i);
172 /* IT LEN stuff CS */
174 checksum (file, ptr, size, code)
183 int bytes = size / 8;
185 last = !(code & 0xff00);
188 ptr[0] = code | (last ? 0x80 : 0);
191 for (j = 0; j < bytes; j++)
194 /* Glue on a checksum too. */
196 fwrite (ptr, bytes + 1, 1, file);
201 writeINT (n, ptr, idx, size, file)
217 /* Lets write out that record and do another one. */
218 checksum (file, ptr, *idx, code | 0x1000);
231 ptr[byte + 0] = n >> 8;
235 ptr[byte + 0] = n >> 24;
236 ptr[byte + 1] = n >> 16;
237 ptr[byte + 2] = n >> 8;
238 ptr[byte + 3] = n >> 0;
247 writeBITS (val, ptr, idx, size)
260 /* Turn off all about to change bits. */
261 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
262 /* Turn on the bits we want. */
263 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
268 writeBARRAY (data, ptr, idx, size, file)
272 int size ATTRIBUTE_UNUSED;
277 writeINT (data.len, ptr, idx, 1, file);
278 for (i = 0; i < data.len; i++)
279 writeINT (data.data[i], ptr, idx, 1, file);
283 writeCHARS (string, ptr, idx, size, file)
294 /* Lets write out that record and do another one. */
295 checksum (file, ptr, *idx, code | 0x1000);
302 /* Variable length string. */
303 size = strlen (string);
307 /* BUG WAITING TO HAPPEN. */
308 memcpy (ptr + i, string, size);
313 #define SYSROFF_SWAP_OUT
316 static char *rname_sh[] =
318 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
321 static char *rname_h8300[] =
323 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
329 /* The TR block is not normal - it doesn't have any contents. */
337 fwrite (b, 1, sizeof (b), file);
341 wr_un (ptr, sfile, first, nsecs)
342 struct coff_ofile *ptr;
343 struct coff_sfile *sfile;
345 int nsecs ATTRIBUTE_UNUSED;
348 struct coff_symbol *s;
352 if (bfd_get_file_flags (abfd) & EXEC_P)
353 un.format = FORMAT_LM;
355 un.format = FORMAT_OM;
359 un.nsections = ptr->nsections - 1; /* Don't count the abs section. */
361 /*NEW - only count sections with size. */
362 un.nsections = nsecs;
367 /* Count all the undefined and defined variables with global scope. */
371 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
373 if (s->visible->type == coff_vis_ext_def
374 || s->visible->type == coff_vis_common)
377 if (s->visible->type == coff_vis_ext_ref)
383 un.linker = "L_GX00";
385 un.name = sfile->name;
386 sysroff_swap_un_out (file, &un);
391 struct coff_ofile *p;
396 if (bfd_get_file_flags (abfd) & EXEC_P)
397 hd.mt = MTYPE_ABS_LM;
399 hd.mt = MTYPE_OMS_OR_LMS;
403 hd.nu = p->nsources; /* Always one unit */
404 hd.code = 0; /* Always ASCII */
405 hd.ver = "0200"; /* Version 2.00 */
407 switch (bfd_get_arch (abfd))
415 switch (bfd_get_mach (abfd))
421 toolname = "C_H8/300";
423 case bfd_mach_h8300h:
427 toolname = "C_H8/300H";
429 case bfd_mach_h8300s:
433 toolname = "C_H8/300S";
438 rnames = rname_h8300;
456 if (! bfd_get_file_flags(abfd) & EXEC_P)
466 hd.address = bfd_get_start_address (abfd);
471 hd.mn = strip_suffix (bfd_get_filename (abfd));
473 sysroff_swap_hd_out (file, &hd);
479 struct coff_ofile *p ATTRIBUTE_UNUSED;
480 struct coff_section *sec;
484 sh.section = sec->number;
488 sysroff_swap_sh_out (file, &sh);
494 struct coff_ofile *p ATTRIBUTE_UNUSED;
495 struct coff_section *section;
499 unsigned char stuff[200];
502 while (i < section->bfd_section->_raw_size)
505 int todo = 200; /* Copy in 200 byte lumps. */
508 if (i + todo > section->bfd_section->_raw_size)
509 todo = section->bfd_section->_raw_size - i;
514 if (bfd_get_file_flags (abfd) & EXEC_P)
515 ob.address = section->address;
526 ob.cpf = 0; /* Never compress. */
528 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
529 ob.data.data = stuff;
530 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
534 /* Now fill the rest with blanks. */
535 while (i < (bfd_size_type) section->size)
538 int todo = 200; /* Copy in 200 byte lumps. */
541 if (i + todo > (bfd_size_type) section->size)
542 todo = section->size - i;
545 ob.cpf = 0; /* Never compress. */
547 memset (stuff, 0, todo);
548 ob.data.data = stuff;
549 sysroff_swap_ob_out (file, &ob);
552 /* Now fill the rest with blanks. */
557 struct coff_ofile *ptr ATTRIBUTE_UNUSED;
558 struct coff_section *sec;
560 int nr = sec->nrelocs;
563 for (i = 0; i < nr; i++)
565 struct coff_reloc *r = sec->relocs + i;
566 struct coff_symbol *ref;
576 rl.flen = 32; /* SH Specific. */
578 /* What sort of reloc ? Look in the section to find out. */
580 if (ref->visible->type == coff_vis_ext_ref)
582 rl.bcount = 4; /* Always 4 for us. */
584 rl.symn = ref->er_number;
586 else if (ref->visible->type == coff_vis_common)
588 rl.bcount = 11; /* Always 11 for us. */
590 rl.secn = ref->where->section->number;
593 rl.addend = ref->where->offset - ref->where->section->address;
594 rl.aopcode_is_0x20 = 0x20;
598 rl.bcount = 11; /* Always 11 for us. */
600 rl.secn = ref->where->section->number;
603 rl.addend = -ref->where->section->address;
604 rl.aopcode_is_0x20 = 0x20;
609 if ( rl.op == OP_SEC_REF
610 || rl.op == OP_EXT_REF)
611 sysroff_swap_rl_out (file, &rl);
617 struct coff_ofile *p;
621 for (i = 1; i < p->nsections; i++)
623 wr_sh (p, p->sections + i);
624 wr_ob (p, p->sections + i);
625 wr_rl (p, p->sections + i);
630 wr_dps_start (sfile, section, scope, type, nest)
631 struct coff_sfile *sfile;
632 struct coff_section *section ATTRIBUTE_UNUSED;
633 struct coff_scope *scope;
645 dps.san = scope->sec->number;
646 dps.address = scope->offset - find_base (sfile, scope->sec);
647 dps.block_size = scope->size;
651 printf ("DPS %s %d %x\n",
666 sysroff_swap_dps_out (file, &dps);
670 wr_dps_end (section, scope, type)
671 struct coff_section *section ATTRIBUTE_UNUSED;
672 struct coff_scope *scope ATTRIBUTE_UNUSED;
679 sysroff_swap_dps_out (file, &dps);
686 return (int *) (xcalloc (sizeof (int), x));
690 walk_tree_type_1 (sfile, symbol, type, nest)
691 struct coff_sfile *sfile;
692 struct coff_symbol *symbol;
693 struct coff_type *type;
698 case coff_secdef_type:
699 case coff_basic_type:
703 switch (type->u.basic)
707 dbt.btype = BTYPE_VOID;
708 dbt.sign = BTYPE_UNSPEC;
709 dbt.fptype = FPTYPE_NOTSPEC;
713 dbt.btype = BTYPE_CHAR;
714 dbt.sign = BTYPE_UNSPEC;
715 dbt.fptype = FPTYPE_NOTSPEC;
721 dbt.btype = BTYPE_INT;
722 dbt.sign = SIGN_SIGNED;
723 dbt.fptype = FPTYPE_NOTSPEC;
727 dbt.btype = BTYPE_FLOAT;
728 dbt.fptype = FPTYPE_SINGLE;
732 dbt.btype = BTYPE_FLOAT;
733 dbt.fptype = FPTYPE_DOUBLE;
737 dbt.btype = BTYPE_FLOAT;
738 dbt.fptype = FPTYPE_EXTENDED;
742 dbt.btype = BTYPE_CHAR;
743 dbt.sign = SIGN_UNSIGNED;
744 dbt.fptype = FPTYPE_NOTSPEC;
750 dbt.btype = BTYPE_INT;
751 dbt.sign = SIGN_UNSIGNED;
752 dbt.fptype = FPTYPE_NOTSPEC;
756 dbt.bitsize = type->size;
758 sysroff_swap_dbt_out (file, &dbt);
762 case coff_pointer_type:
766 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
768 sysroff_swap_dpt_out (file, &dpt);
772 case coff_function_type:
775 struct coff_symbol *param;
779 dfp.nparams = type->u.function.parameters->nvars;
782 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
784 sysroff_swap_dfp_out (file, &dfp);
786 for (param = type->u.function.parameters->vars_head;
789 walk_tree_symbol (sfile, 0, param, nest);
792 sysroff_swap_dfp_out (file, &dfp);
796 case coff_structdef_type:
800 struct coff_symbol *member;
803 dbt.btype = BTYPE_STRUCT;
804 dbt.bitsize = type->size;
805 dbt.sign = SIGN_UNSPEC;
806 dbt.fptype = FPTYPE_NOTSPEC;
807 dbt.sid = get_member_id (type->u.astructdef.idx);
809 sysroff_swap_dbt_out (file, &dbt);
812 sysroff_swap_dds_out (file, &dds);
814 for (member = type->u.astructdef.elements->vars_head;
816 member = member->next)
817 walk_tree_symbol (sfile, 0, member, nest + 1);
820 sysroff_swap_dds_out (file, &dds);
825 case coff_structref_type:
829 dbt.btype = BTYPE_TAG;
830 dbt.bitsize = type->size;
831 dbt.sign = SIGN_UNSPEC;
832 dbt.fptype = FPTYPE_NOTSPEC;
834 if (type->u.astructref.ref)
835 dbt.sid = get_member_id (type->u.astructref.ref->number);
840 sysroff_swap_dbt_out (file, &dbt);
844 case coff_array_type:
848 int dims = 1; /* Only output one dimension at a time. */
851 dar.variable = nints (dims);
852 dar.subtype = nints (dims);
853 dar.spare = nints (dims);
854 dar.max_variable = nints (dims);
855 dar.maxspare = nints (dims);
856 dar.max = nints (dims);
857 dar.min_variable = nints (dims);
858 dar.min = nints (dims);
859 dar.minspare = nints (dims);
861 dar.length = type->size / type->u.array.dim;
863 for (j = 0; j < dims; j++)
865 dar.variable[j] = VARIABLE_FIXED;
866 dar.subtype[j] = SUB_INTEGER;
868 dar.max_variable[j] = 0;
869 dar.max[j] = type->u.array.dim;
870 dar.min_variable[j] = 0;
871 dar.min[j] = 1; /* Why isn't this 0 ? */
873 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
874 sysroff_swap_dar_out (file, &dar);
878 case coff_enumdef_type:
882 struct coff_symbol *member;
884 dbt.btype = BTYPE_ENUM;
885 dbt.bitsize = type->size;
886 dbt.sign = SIGN_UNSPEC;
887 dbt.fptype = FPTYPE_NOTSPEC;
888 dbt.sid = get_member_id (type->u.aenumdef.idx);
890 sysroff_swap_dbt_out (file, &dbt);
895 sysroff_swap_den_out (file, &den);
897 for (member = type->u.aenumdef.elements->vars_head;
899 member = member->next)
900 walk_tree_symbol (sfile, 0, member, nest + 1);
903 sysroff_swap_den_out (file, &den);
907 case coff_enumref_type:
911 dbt.btype = BTYPE_TAG;
912 dbt.bitsize = type->size;
913 dbt.sign = SIGN_UNSPEC;
914 dbt.fptype = FPTYPE_NOTSPEC;
915 dbt.sid = get_member_id (type->u.aenumref.ref->number);
917 sysroff_swap_dbt_out (file, &dbt);
934 sysroff_swap_dty_out (file, &dty);
944 sysroff_swap_dty_out (file, &dty);
949 dump_tree_structure (sfile, symbol, type, nest)
950 struct coff_sfile *sfile;
951 struct coff_symbol *symbol;
952 struct coff_type *type;
955 if (symbol->type->type == coff_function_type)
965 walk_tree_type (sfile, symbol, type, nest)
966 struct coff_sfile *sfile;
967 struct coff_symbol *symbol;
968 struct coff_type *type;
971 if (symbol->type->type == coff_function_type)
978 sysroff_swap_dty_out (file, &dty);
979 walk_tree_type_1 (sfile, symbol, type, nest);
981 sysroff_swap_dty_out (file, &dty);
984 symbol->where->section,
985 symbol->type->u.function.code,
986 BLOCK_TYPE_FUNCTION, nest);
987 wr_dps_start (sfile, symbol->where->section,
988 symbol->type->u.function.code,
989 BLOCK_TYPE_BLOCK, nest);
990 walk_tree_scope (symbol->where->section,
992 symbol->type->u.function.code,
993 nest + 1, BLOCK_TYPE_BLOCK);
995 wr_dps_end (symbol->where->section,
996 symbol->type->u.function.code,
998 wr_dps_end (symbol->where->section,
999 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
1007 sysroff_swap_dty_out (file, &dty);
1008 walk_tree_type_1 (sfile, symbol, type, nest);
1010 sysroff_swap_dty_out (file, &dty);
1015 walk_tree_symbol (sfile, section, symbol, nest)
1016 struct coff_sfile *sfile;
1017 struct coff_section *section ATTRIBUTE_UNUSED;
1018 struct coff_symbol *symbol;
1023 memset (&dsy, 0, sizeof(dsy));
1026 switch (symbol->type->type)
1028 case coff_function_type:
1029 dsy.type = STYPE_FUNC;
1033 case coff_structref_type:
1034 case coff_pointer_type:
1035 case coff_array_type:
1036 case coff_basic_type:
1037 case coff_enumref_type:
1038 dsy.type = STYPE_VAR;
1042 case coff_enumdef_type:
1043 dsy.type = STYPE_TAG;
1048 case coff_structdef_type:
1049 dsy.type = STYPE_TAG;
1051 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
1054 case coff_secdef_type:
1061 if (symbol->where->where == coff_where_member_of_struct)
1064 dsy.type = STYPE_MEMBER;
1067 if (symbol->where->where == coff_where_member_of_enum)
1069 dsy.type = STYPE_ENUM;
1072 dsy.evalue = symbol->where->offset;
1075 if (symbol->type->type == coff_structdef_type
1076 || symbol->where->where == coff_where_entag
1077 || symbol->where->where == coff_where_strtag)
1079 dsy.snumber = get_member_id (symbol->number);
1083 dsy.snumber = get_ordinary_id (symbol->number);
1086 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1088 switch (symbol->visible->type)
1090 case coff_vis_common:
1091 case coff_vis_ext_def:
1092 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1095 case coff_vis_ext_ref:
1096 dsy.ainfo = AINFO_STATIC_EXT_REF;
1099 case coff_vis_int_def:
1100 dsy.ainfo = AINFO_STATIC_INT;
1104 case coff_vis_autoparam:
1105 dsy.ainfo = AINFO_AUTO;
1108 case coff_vis_register:
1109 case coff_vis_regparam:
1110 dsy.ainfo = AINFO_REG;
1115 case coff_vis_member_of_struct:
1116 case coff_vis_member_of_enum:
1123 dsy.dlength = symbol->type->size;
1125 switch (symbol->where->where)
1127 case coff_where_memory:
1129 dsy.section = symbol->where->section->number;
1135 case coff_where_member_of_struct:
1136 case coff_where_member_of_enum:
1137 case coff_where_stack:
1138 case coff_where_register:
1139 case coff_where_unknown:
1140 case coff_where_strtag:
1141 case coff_where_entag:
1142 case coff_where_typedef:
1149 switch (symbol->where->where)
1151 case coff_where_memory:
1152 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1155 case coff_where_stack:
1156 dsy.address = symbol->where->offset;
1159 case coff_where_member_of_struct:
1160 if (symbol->where->bitsize)
1162 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1164 dsy.field_len = symbol->where->bitsize;
1165 dsy.field_off = (bits / 32) * 4;
1166 dsy.field_bitoff = bits % 32;
1172 dsy.field_len = symbol->type->size;
1173 dsy.field_off = symbol->where->offset;
1177 case coff_where_member_of_enum:
1179 dsy.field_len = symbol->type->size;
1180 dsy.field_off = symbol->where->offset; */
1183 case coff_where_register:
1184 case coff_where_unknown:
1185 case coff_where_strtag:
1186 case coff_where_entag:
1187 case coff_where_typedef:
1194 if (symbol->where->where == coff_where_register)
1195 dsy.reg = rnames[symbol->where->offset];
1197 switch (symbol->visible->type)
1199 case coff_vis_common:
1200 /* We do this 'cause common C symbols are treated as extdefs. */
1201 case coff_vis_ext_def:
1202 case coff_vis_ext_ref:
1203 dsy.ename = symbol->name;
1206 case coff_vis_regparam:
1207 case coff_vis_autoparam:
1208 dsy.type = STYPE_PARAMETER;
1211 case coff_vis_int_def:
1213 case coff_vis_register:
1215 case coff_vis_member_of_struct:
1216 case coff_vis_member_of_enum:
1227 sysroff_swap_dsy_out (file, &dsy);
1229 walk_tree_type (sfile, symbol, symbol->type, nest);
1233 walk_tree_scope (section, sfile, scope, nest, type)
1234 struct coff_section *section;
1235 struct coff_sfile *sfile;
1236 struct coff_scope *scope;
1240 struct coff_symbol *vars;
1241 struct coff_scope *child;
1243 if (scope->vars_head
1244 || (scope->list_head && scope->list_head->vars_head))
1246 wr_dps_start (sfile, section, scope, type, nest);
1249 wr_globals (tree, sfile, nest + 1);
1251 for (vars = scope->vars_head; vars; vars = vars->next)
1252 walk_tree_symbol (sfile, section, vars, nest);
1254 for (child = scope->list_head; child; child = child->next)
1255 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1257 wr_dps_end (section, scope, type);
1262 walk_tree_sfile (section, sfile)
1263 struct coff_section *section;
1264 struct coff_sfile *sfile;
1266 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1270 wr_program_structure (p, sfile)
1271 struct coff_ofile *p;
1272 struct coff_sfile *sfile;
1274 walk_tree_sfile (p->sections + 4, sfile);
1279 struct coff_ofile *p;
1280 struct coff_sfile *sfile;
1286 struct coff_symbol *symbol;
1287 static int incit = 0x500000;
1292 unsigned int *lowest = (unsigned *) nints (p->nsections);
1293 unsigned int *highest = (unsigned *) nints (p->nsections);
1295 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1300 du.sections = p->nsections - 1;
1301 du.san = (int *) xcalloc (sizeof (int), du.sections);
1302 du.address = nints (du.sections);
1303 du.length = nints (du.sections);
1305 for (i = 0; i < du.sections; i++)
1311 /* Look through all the symbols and try and work out the extents in this
1314 for (symbol = sfile->scope->vars_head;
1316 symbol = symbol->next)
1318 if (symbol->type->type == coff_secdef_type)
1320 unsigned int low = symbol->where->offset;
1321 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1322 struct coff_section *section = symbol->where->section;
1324 int sn = section->number;
1325 if (low < lowest[sn])
1327 if (high > highest[sn])
1332 for (i = 0; i < du.sections; i++)
1334 if (highest[i] == 0)
1335 lowest[i] = highest[i] = incit;
1338 du.length[used] = highest[i] - lowest[i];
1339 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1343 printf (" section %6s 0x%08x..0x%08x\n",
1344 p->sections[i + 1].name,
1353 for (j = 0; j < lim; j++)
1360 if (sfile->section[src].init)
1363 = sfile->section[src].high - sfile->section[src].low + 1;
1365 = sfile->section[src].low;
1370 du.address[dst] = 0;
1375 if (sfile->section[src].parent)
1377 printf (" section %6s 0x%08x..0x%08x\n",
1378 sfile->section[src].parent->name,
1380 du.address[dst] + du.length[dst] - 1);
1384 du.sections = dst + 1;
1390 sysroff_swap_du_out (file, &du);
1395 struct coff_ofile *p ATTRIBUTE_UNUSED;
1396 struct coff_sfile *sfile;
1401 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1402 dus.drb = nints (dus.ns);
1403 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1404 dus.spare = nints (dus.ns);
1406 /* Find the filenames. */
1410 for (sfile = p->source_head;
1412 sfile = sfile->next)
1416 dus.fname[i] = sfile->name;
1421 dus.fname[0] = sfile->name;
1424 sysroff_swap_dus_out (file, &dus);
1428 /* Find the offset of the .text section for this sfile in the
1429 .text section for the output file. */
1432 find_base (sfile, section)
1433 struct coff_sfile *sfile;
1434 struct coff_section *section;
1436 return sfile->section[section->number].low;
1440 wr_dln (p, sfile, n)
1441 struct coff_ofile *p ATTRIBUTE_UNUSED;
1442 struct coff_sfile *sfile;
1443 int n ATTRIBUTE_UNUSED;
1449 /* Count up all the linenumbers */
1450 struct coff_symbol *sy;
1456 for (sy = p->symbol_list_head;
1458 sy = sy->next_in_ofile_list)
1460 struct coff_type *t = sy->type;
1461 if (t->type == coff_function_type)
1463 struct coff_line *l = t->u.function.lines;
1468 dln.sfn = nints (lc);
1469 dln.sln = nints (lc);
1470 dln.lln = nints (lc);
1471 dln.section = nints (lc);
1473 dln.from_address = nints (lc);
1474 dln.to_address = nints (lc);
1481 /* Run through once more and fill up the structure */
1483 for (sy = p->symbol_list_head;
1485 sy = sy->next_in_ofile_list)
1487 if (sy->type->type == coff_function_type)
1490 struct coff_line *l = sy->type->u.function.lines;
1491 for (i = 0; i < l->nlines; i++)
1493 dln.section[idx] = sy->where->section->number;
1495 dln.sln[idx] = l->lines[i];
1496 dln.from_address[idx] = l->addresses[i];
1498 dln.to_address[idx - 1] = dln.from_address[idx];
1504 sysroff_swap_dln_out (file, &dln);
1509 /* Count up all the linenumbers */
1511 struct coff_symbol *sy;
1517 for (sy = sfile->scope->vars_head;
1521 struct coff_type *t = sy->type;
1522 if (t->type == coff_function_type)
1524 struct coff_line *l = t->u.function.lines;
1530 dln.sfn = nints (lc);
1531 dln.sln = nints (lc);
1532 dln.cc = nints (lc);
1533 dln.section = nints (lc);
1535 dln.from_address = nints (lc);
1536 dln.to_address = nints (lc);
1543 /* Run through once more and fill up the structure */
1545 for (sy = sfile->scope->vars_head;
1549 if (sy->type->type == coff_function_type)
1552 struct coff_line *l = sy->type->u.function.lines;
1555 int base = find_base (sfile, sy->where->section);
1556 for (i = 0; i < l->nlines; i++)
1558 dln.section[idx] = sy->where->section->number;
1560 dln.sln[idx] = l->lines[i];
1561 dln.from_address[idx] =
1562 l->addresses[i] + sy->where->section->address - base;
1565 dln.to_address[idx - 1] = dln.from_address[idx];
1569 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1574 sysroff_swap_dln_out (file, &dln);
1578 /* Write the global symbols out to the debug info. */
1581 wr_globals (p, sfile, n)
1582 struct coff_ofile *p;
1583 struct coff_sfile *sfile;
1584 int n ATTRIBUTE_UNUSED;
1586 struct coff_symbol *sy;
1588 for (sy = p->symbol_list_head;
1590 sy = sy->next_in_ofile_list)
1592 if (sy->visible->type == coff_vis_ext_def
1593 || sy->visible->type == coff_vis_ext_ref)
1595 /* Only write out symbols if they belong to
1596 the current source file. */
1597 if (sy->sfile == sfile)
1598 walk_tree_symbol (sfile, 0, sy, 0);
1605 struct coff_ofile *p;
1607 struct coff_sfile *sfile;
1610 for (sfile = p->source_head;
1612 sfile = sfile->next)
1615 printf ("%s\n", sfile->name);
1617 wr_du (p, sfile, n);
1619 wr_program_structure (p, sfile);
1620 wr_dln (p, sfile, n);
1628 /* It seems that the CS struct is not normal - the size is wrong
1629 heres one I prepared earlier. */
1634 0x00, /* number of chars in variable length part */
1666 fwrite (b, 1, sizeof (b), file);
1669 /* Write out the SC records for a unit. Create an SC
1670 for all the sections which appear in the output file, even
1671 if there isn't an equivalent one on the input. */
1675 struct coff_ofile *ptr;
1676 struct coff_sfile *sfile;
1680 /* First work out the total number of sections. */
1681 int total_sec = ptr->nsections;
1684 struct coff_section *sec;
1685 struct coff_symbol *symbol;
1687 struct coff_symbol *symbol;
1689 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1692 for (i = 0; i < total_sec; i++)
1694 info[i].sec = ptr->sections + i;
1698 for (symbol = sfile->scope->vars_head;
1700 symbol = symbol->next)
1703 if (symbol->type->type == coff_secdef_type)
1705 for (i = 0; i < total_sec; i++)
1707 if (symbol->where->section == info[i].sec)
1709 info[i].symbol = symbol;
1716 /* Now output all the section info, and fake up some stuff for sections
1718 for (i = 1; i < total_sec; i++)
1723 symbol = info[i].symbol;
1729 /* Don't have a symbol set aside for this section, which means
1730 that nothing in this file does anything for the section. */
1731 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1734 name = info[i].sec->name;
1738 if (bfd_get_file_flags (abfd) & EXEC_P)
1741 sc.addr = symbol->where->offset;
1748 sc.length = symbol->type->size;
1749 name = symbol->name;
1753 sc.concat = CONCAT_SIMPLE;
1761 sc.spare1 = 0; /* If not zero, then it doesn't work. */
1762 sc.name = section_translate (name);
1764 if (strlen (sc.name) == 1)
1770 sc.contents = CONTENTS_DATA;
1774 sc.contents = CONTENTS_CODE;
1779 sc.contents = CONTENTS_CODE;
1786 sysroff_swap_sc_out (file, &sc);
1795 /* Write out the ER records for a unit. */
1798 wr_er (ptr, sfile, first)
1799 struct coff_ofile *ptr;
1800 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1804 struct coff_symbol *sym;
1808 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1810 if (sym->visible->type == coff_vis_ext_ref)
1815 er.type = ER_NOTSPEC;
1816 er.name = sym->name;
1817 sysroff_swap_er_out (file, &er);
1818 sym->er_number = idx++;
1824 /* Write out the ED records for a unit. */
1827 wr_ed (ptr, sfile, first)
1828 struct coff_ofile *ptr;
1829 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1832 struct coff_symbol *s;
1836 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1838 if (s->visible->type == coff_vis_ext_def
1839 || s->visible->type == coff_vis_common)
1843 ed.section = s->where->section->number;
1846 if (s->where->section->data)
1848 ed.type = ED_TYPE_DATA;
1850 else if (s->where->section->code & SEC_CODE)
1852 ed.type = ED_TYPE_ENTRY;
1856 ed.type = ED_TYPE_NOTSPEC;
1857 ed.type = ED_TYPE_DATA;
1860 ed.address = s->where->offset - s->where->section->address;
1862 sysroff_swap_ed_out (file, &ed);
1870 struct coff_ofile *ptr;
1872 struct coff_sfile *sfile;
1875 for (sfile = ptr->source_head;
1877 sfile = sfile->next)
1884 wr_un (ptr, sfile, first, 0);
1885 nsecs = wr_sc (ptr, sfile);
1887 fseek (file, p1, SEEK_SET);
1888 wr_un (ptr, sfile, first, nsecs);
1889 fseek (file, p2, SEEK_SET);
1890 wr_er (ptr, sfile, first);
1891 wr_ed (ptr, sfile, first);
1898 struct coff_ofile *p;
1912 return (x + 3) & ~3;
1915 /* Find all the common variables and turn them into
1916 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1920 struct coff_ofile *tree;
1922 struct coff_symbol *s;
1923 struct coff_section *common_section;
1925 /* Find the common section - always section 3. */
1926 common_section = tree->sections + 3;
1928 for (s = tree->symbol_list_head;
1930 s = s->next_in_ofile_list)
1932 if (s->visible->type == coff_vis_common)
1934 struct coff_where *w = s->where;
1935 /* s->visible->type = coff_vis_ext_def; leave it as common */
1936 common_section->size = align (common_section->size);
1937 w->offset = common_section->size + common_section->address;
1938 w->section = common_section;
1939 common_section->size += s->type->size;
1940 common_section->size = align (common_section->size);
1948 show_usage (file, status)
1952 fprintf (file, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
1953 fprintf (file, _("Convert a COFF object file into a SYSROFF object file\n"));
1954 fprintf (file, _(" The options are:\n\
1955 -q --quick (Obsolete - ignoerd)\n\
1956 -n --noprescan Do not perform a scan to convert commons into defs\n\
1957 -d --debug Display information about what is being done\n\
1958 -h --help Display this information\n\
1959 -v --version Print the program's version number\n"));
1962 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1972 static struct option long_options[] =
1974 {"debug", no_argument, 0, 'd'},
1975 {"quick", no_argument, 0, 'q'},
1976 {"noprescan", no_argument, 0, 'n'},
1977 {"help", no_argument, 0, 'h'},
1978 {"version", no_argument, 0, 'V'},
1979 {NULL, no_argument, 0, 0}
1985 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1986 setlocale (LC_MESSAGES, "");
1988 #if defined (HAVE_SETLOCALE)
1989 setlocale (LC_CTYPE, "");
1991 bindtextdomain (PACKAGE, LOCALEDIR);
1992 textdomain (PACKAGE);
1994 program_name = av[0];
1995 xmalloc_set_program_name (program_name);
1997 while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
2014 show_usage (stdout, 0);
2018 print_version ("srconv");
2024 show_usage (stderr, 1);
2029 /* The input and output files may be named on the command line. */
2033 input_file = av[optind];
2037 output_file = av[optind];
2040 show_usage (stderr, 1);
2041 if (strcmp (input_file, output_file) == 0)
2043 fatal (_("input and output files must be different"));
2052 fatal (_("no input file specified"));
2057 /* Take a .o off the input file and stick on a .obj. If
2058 it doesn't end in .o, then stick a .obj on anyway */
2060 int len = strlen (input_file);
2062 output_file = xmalloc (len + 5);
2063 strcpy (output_file, input_file);
2066 && output_file[len - 2] == '.'
2067 && output_file[len - 1] == 'o')
2069 output_file[len] = 'b';
2070 output_file[len + 1] = 'j';
2071 output_file[len + 2] = 0;
2075 strcat (output_file, ".obj");
2079 abfd = bfd_openr (input_file, 0);
2082 bfd_fatal (input_file);
2084 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2086 bfd_nonfatal (input_file);
2088 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2090 list_matching_formats (matching);
2096 file = fopen (output_file, FOPEN_WB);
2099 fatal (_("unable to open output file %s"), output_file);
2102 printf ("ids %d %d\n", base1, base2);
2104 tree = coff_grok (abfd);