1 /* srconv.c -- Sysroff conversion program
2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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 3 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., 51 Franklin Street - Fifth Floor, 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 */
34 #include "libiberty.h"
37 #include "coff/internal.h"
38 #include "../bfd/libcoff.h"
43 static char *toolname;
46 static int get_member_id (int);
47 static int get_ordinary_id (int);
48 static char *section_translate (char *);
49 static char *strip_suffix (char *);
50 static void checksum (FILE *, unsigned char *, int, int);
51 static void writeINT (int, unsigned char *, int *, int, FILE *);
52 static void writeBITS (int, unsigned char *, int *, int);
53 static void writeBARRAY (barray, unsigned char *, int *, int, FILE *);
54 static void writeCHARS (char *, unsigned char *, int *, int, FILE *);
55 static void wr_tr (void);
56 static void wr_un (struct coff_ofile *, struct coff_sfile *, int, int);
57 static void wr_hd (struct coff_ofile *);
58 static void wr_sh (struct coff_ofile *, struct coff_section *);
59 static void wr_ob (struct coff_ofile *, struct coff_section *);
60 static void wr_rl (struct coff_ofile *, struct coff_section *);
61 static void wr_object_body (struct coff_ofile *);
62 static void wr_dps_start
63 (struct coff_sfile *, struct coff_section *, struct coff_scope *, int, int);
64 static void wr_dps_end (struct coff_section *, struct coff_scope *, int);
65 static int *nints (int);
66 static void walk_tree_type_1
67 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
68 static void walk_tree_type
69 (struct coff_sfile *, struct coff_symbol *, struct coff_type *, int);
70 static void walk_tree_symbol
71 (struct coff_sfile *, struct coff_section *, struct coff_symbol *, int);
72 static void walk_tree_scope
73 (struct coff_section *, struct coff_sfile *, struct coff_scope *, int, int);
74 static void walk_tree_sfile (struct coff_section *, struct coff_sfile *);
75 static void wr_program_structure (struct coff_ofile *, struct coff_sfile *);
76 static void wr_du (struct coff_ofile *, struct coff_sfile *, int);
77 static void wr_dus (struct coff_ofile *, struct coff_sfile *);
78 static int find_base (struct coff_sfile *, struct coff_section *);
79 static void wr_dln (struct coff_ofile *, struct coff_sfile *, int);
80 static void wr_globals (struct coff_ofile *, struct coff_sfile *, int);
81 static void wr_debug (struct coff_ofile *);
82 static void wr_cs (void);
83 static int wr_sc (struct coff_ofile *, struct coff_sfile *);
84 static void wr_er (struct coff_ofile *, struct coff_sfile *, int);
85 static void wr_ed (struct coff_ofile *, struct coff_sfile *, int);
86 static void wr_unit_info (struct coff_ofile *);
87 static void wr_module (struct coff_ofile *);
88 static int align (int);
89 static void prescan (struct coff_ofile *);
90 static void show_usage (FILE *, int);
91 extern int main (int, char **);
97 static int noprescan = 0;
98 static struct coff_ofile *tree;
100 static int absolute_p;
103 static int segmented_p;
106 static int ids1[20000];
107 static int ids2[20000];
109 static int base1 = 0x18;
110 static int base2 = 0x2018;
113 get_member_id (int x)
123 get_ordinary_id (int x)
132 section_translate (char *n)
134 if (strcmp (n, ".text") == 0)
136 if (strcmp (n, ".data") == 0)
138 if (strcmp (n, ".bss") == 0)
143 #define DATE "940201073000"; /* Just a time on my birthday */
147 strip_suffix (char *name)
152 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
154 res = (char *) xmalloc (i + 1);
155 memcpy (res, name, i);
160 /* IT LEN stuff CS */
162 checksum (FILE *file, unsigned char *ptr, int size, int code)
167 int bytes = size / 8;
169 last = !(code & 0xff00);
172 ptr[0] = code | (last ? 0x80 : 0);
175 for (j = 0; j < bytes; j++)
178 /* Glue on a checksum too. */
180 fwrite (ptr, bytes + 1, 1, file);
185 writeINT (int n, unsigned char *ptr, int *idx, int size, FILE *file)
196 /* Lets write out that record and do another one. */
197 checksum (file, ptr, *idx, code | 0x1000);
210 ptr[byte + 0] = n >> 8;
214 ptr[byte + 0] = n >> 24;
215 ptr[byte + 1] = n >> 16;
216 ptr[byte + 2] = n >> 8;
217 ptr[byte + 3] = n >> 0;
226 writeBITS (int val, unsigned char *ptr, int *idx, int size)
235 /* Turn off all about to change bits. */
236 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
237 /* Turn on the bits we want. */
238 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
243 writeBARRAY (barray data, unsigned char *ptr, int *idx,
244 int size ATTRIBUTE_UNUSED, FILE *file)
248 writeINT (data.len, ptr, idx, 1, file);
249 for (i = 0; i < data.len; i++)
250 writeINT (data.data[i], ptr, idx, 1, file);
254 writeCHARS (char *string, unsigned char *ptr, int *idx, int size, FILE *file)
260 /* Lets write out that record and do another one. */
261 checksum (file, ptr, *idx, code | 0x1000);
268 /* Variable length string. */
269 size = strlen (string);
273 /* BUG WAITING TO HAPPEN. */
274 memcpy (ptr + i, string, size);
279 #define SYSROFF_SWAP_OUT
282 static char *rname_sh[] =
284 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
287 static char *rname_h8300[] =
289 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
295 /* The TR block is not normal - it doesn't have any contents. */
303 fwrite (b, 1, sizeof (b), file);
307 wr_un (struct coff_ofile *ptr, struct coff_sfile *sfile, int first,
308 int nsecs ATTRIBUTE_UNUSED)
311 struct coff_symbol *s;
315 if (bfd_get_file_flags (abfd) & EXEC_P)
316 un.format = FORMAT_LM;
318 un.format = FORMAT_OM;
321 /* Don't count the abs section. */
322 un.nsections = ptr->nsections - 1;
326 /* Count all the undefined and defined variables with global scope. */
330 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
332 if (s->visible->type == coff_vis_ext_def
333 || s->visible->type == coff_vis_common)
336 if (s->visible->type == coff_vis_ext_ref)
342 un.linker = "L_GX00";
344 un.name = sfile->name;
345 sysroff_swap_un_out (file, &un);
349 wr_hd (struct coff_ofile *p)
354 if (bfd_get_file_flags (abfd) & EXEC_P)
355 hd.mt = MTYPE_ABS_LM;
357 hd.mt = MTYPE_OMS_OR_LMS;
361 hd.nu = p->nsources; /* Always one unit */
362 hd.code = 0; /* Always ASCII */
363 hd.ver = "0200"; /* Version 2.00 */
365 switch (bfd_get_arch (abfd))
373 switch (bfd_get_mach (abfd))
379 toolname = "C_H8/300";
381 case bfd_mach_h8300h:
385 toolname = "C_H8/300H";
387 case bfd_mach_h8300s:
391 toolname = "C_H8/300S";
396 rnames = rname_h8300;
414 if (! bfd_get_file_flags(abfd) & EXEC_P)
424 hd.address = bfd_get_start_address (abfd);
429 hd.mn = strip_suffix (bfd_get_filename (abfd));
431 sysroff_swap_hd_out (file, &hd);
436 wr_sh (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *sec)
440 sh.section = sec->number;
444 sysroff_swap_sh_out (file, &sh);
449 wr_ob (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_section *section)
453 unsigned char stuff[200];
456 while (i < bfd_get_section_size (section->bfd_section))
459 int todo = 200; /* Copy in 200 byte lumps. */
462 if (i + todo > bfd_get_section_size (section->bfd_section))
463 todo = bfd_get_section_size (section->bfd_section) - i;
468 if (bfd_get_file_flags (abfd) & EXEC_P)
469 ob.address = section->address;
480 ob.cpf = 0; /* Never compress. */
482 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
483 ob.data.data = stuff;
484 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
488 /* Now fill the rest with blanks. */
489 while (i < (bfd_size_type) section->size)
492 int todo = 200; /* Copy in 200 byte lumps. */
495 if (i + todo > (bfd_size_type) section->size)
496 todo = section->size - i;
499 ob.cpf = 0; /* Never compress. */
501 memset (stuff, 0, todo);
502 ob.data.data = stuff;
503 sysroff_swap_ob_out (file, &ob);
506 /* Now fill the rest with blanks. */
510 wr_rl (struct coff_ofile *ptr ATTRIBUTE_UNUSED, struct coff_section *sec)
512 int nr = sec->nrelocs;
515 for (i = 0; i < nr; i++)
517 struct coff_reloc *r = sec->relocs + i;
518 struct coff_symbol *ref;
528 rl.flen = 32; /* SH Specific. */
530 /* What sort of reloc ? Look in the section to find out. */
532 if (ref->visible->type == coff_vis_ext_ref)
534 rl.bcount = 4; /* Always 4 for us. */
536 rl.symn = ref->er_number;
538 else if (ref->visible->type == coff_vis_common)
540 rl.bcount = 11; /* Always 11 for us. */
542 rl.secn = ref->where->section->number;
545 rl.addend = ref->where->offset - ref->where->section->address;
546 rl.aopcode_is_0x20 = 0x20;
550 rl.bcount = 11; /* Always 11 for us. */
552 rl.secn = ref->where->section->number;
555 rl.addend = -ref->where->section->address;
556 rl.aopcode_is_0x20 = 0x20;
561 if ( rl.op == OP_SEC_REF
562 || rl.op == OP_EXT_REF)
563 sysroff_swap_rl_out (file, &rl);
568 wr_object_body (struct coff_ofile *p)
572 for (i = 1; i < p->nsections; i++)
574 wr_sh (p, p->sections + i);
575 wr_ob (p, p->sections + i);
576 wr_rl (p, p->sections + i);
581 wr_dps_start (struct coff_sfile *sfile,
582 struct coff_section *section ATTRIBUTE_UNUSED,
583 struct coff_scope *scope, int type, int nest)
593 dps.san = scope->sec->number;
594 dps.address = scope->offset - find_base (sfile, scope->sec);
595 dps.block_size = scope->size;
599 printf ("DPS %s %d %x\n",
614 sysroff_swap_dps_out (file, &dps);
618 wr_dps_end (struct coff_section *section ATTRIBUTE_UNUSED,
619 struct coff_scope *scope ATTRIBUTE_UNUSED, int type)
625 sysroff_swap_dps_out (file, &dps);
631 return (int *) (xcalloc (sizeof (int), x));
635 walk_tree_type_1 (struct coff_sfile *sfile, struct coff_symbol *symbol,
636 struct coff_type *type, int nest)
640 case coff_secdef_type:
641 case coff_basic_type:
645 switch (type->u.basic)
649 dbt.btype = BTYPE_VOID;
650 dbt.sign = BTYPE_UNSPEC;
651 dbt.fptype = FPTYPE_NOTSPEC;
655 dbt.btype = BTYPE_CHAR;
656 dbt.sign = BTYPE_UNSPEC;
657 dbt.fptype = FPTYPE_NOTSPEC;
663 dbt.btype = BTYPE_INT;
664 dbt.sign = SIGN_SIGNED;
665 dbt.fptype = FPTYPE_NOTSPEC;
669 dbt.btype = BTYPE_FLOAT;
670 dbt.fptype = FPTYPE_SINGLE;
674 dbt.btype = BTYPE_FLOAT;
675 dbt.fptype = FPTYPE_DOUBLE;
679 dbt.btype = BTYPE_FLOAT;
680 dbt.fptype = FPTYPE_EXTENDED;
684 dbt.btype = BTYPE_CHAR;
685 dbt.sign = SIGN_UNSIGNED;
686 dbt.fptype = FPTYPE_NOTSPEC;
692 dbt.btype = BTYPE_INT;
693 dbt.sign = SIGN_UNSIGNED;
694 dbt.fptype = FPTYPE_NOTSPEC;
698 dbt.bitsize = type->size;
700 sysroff_swap_dbt_out (file, &dbt);
704 case coff_pointer_type:
709 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
711 sysroff_swap_dpt_out (file, &dpt);
715 case coff_function_type:
718 struct coff_symbol *param;
722 dfp.nparams = type->u.function.parameters->nvars;
725 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
727 sysroff_swap_dfp_out (file, &dfp);
729 for (param = type->u.function.parameters->vars_head;
732 walk_tree_symbol (sfile, 0, param, nest);
735 sysroff_swap_dfp_out (file, &dfp);
739 case coff_structdef_type:
743 struct coff_symbol *member;
746 dbt.btype = BTYPE_STRUCT;
747 dbt.bitsize = type->size;
748 dbt.sign = SIGN_UNSPEC;
749 dbt.fptype = FPTYPE_NOTSPEC;
750 dbt.sid = get_member_id (type->u.astructdef.idx);
752 sysroff_swap_dbt_out (file, &dbt);
755 sysroff_swap_dds_out (file, &dds);
757 for (member = type->u.astructdef.elements->vars_head;
759 member = member->next)
760 walk_tree_symbol (sfile, 0, member, nest + 1);
763 sysroff_swap_dds_out (file, &dds);
768 case coff_structref_type:
772 dbt.btype = BTYPE_TAG;
773 dbt.bitsize = type->size;
774 dbt.sign = SIGN_UNSPEC;
775 dbt.fptype = FPTYPE_NOTSPEC;
777 if (type->u.astructref.ref)
778 dbt.sid = get_member_id (type->u.astructref.ref->number);
783 sysroff_swap_dbt_out (file, &dbt);
787 case coff_array_type:
791 int dims = 1; /* Only output one dimension at a time. */
794 dar.variable = nints (dims);
795 dar.subtype = nints (dims);
796 dar.spare = nints (dims);
797 dar.max_variable = nints (dims);
798 dar.maxspare = nints (dims);
799 dar.max = nints (dims);
800 dar.min_variable = nints (dims);
801 dar.min = nints (dims);
802 dar.minspare = nints (dims);
804 dar.length = type->size / type->u.array.dim;
806 for (j = 0; j < dims; j++)
808 dar.variable[j] = VARIABLE_FIXED;
809 dar.subtype[j] = SUB_INTEGER;
811 dar.max_variable[j] = 0;
812 dar.max[j] = type->u.array.dim;
813 dar.min_variable[j] = 0;
814 dar.min[j] = 1; /* Why isn't this 0 ? */
816 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
817 sysroff_swap_dar_out (file, &dar);
821 case coff_enumdef_type:
825 struct coff_symbol *member;
827 dbt.btype = BTYPE_ENUM;
828 dbt.bitsize = type->size;
829 dbt.sign = SIGN_UNSPEC;
830 dbt.fptype = FPTYPE_NOTSPEC;
831 dbt.sid = get_member_id (type->u.aenumdef.idx);
833 sysroff_swap_dbt_out (file, &dbt);
838 sysroff_swap_den_out (file, &den);
840 for (member = type->u.aenumdef.elements->vars_head;
842 member = member->next)
843 walk_tree_symbol (sfile, 0, member, nest + 1);
846 sysroff_swap_den_out (file, &den);
850 case coff_enumref_type:
854 dbt.btype = BTYPE_TAG;
855 dbt.bitsize = type->size;
856 dbt.sign = SIGN_UNSPEC;
857 dbt.fptype = FPTYPE_NOTSPEC;
858 dbt.sid = get_member_id (type->u.aenumref.ref->number);
860 sysroff_swap_dbt_out (file, &dbt);
877 sysroff_swap_dty_out (file, &dty);
887 sysroff_swap_dty_out (file, &dty);
892 dump_tree_structure (sfile, symbol, type, nest)
893 struct coff_sfile *sfile;
894 struct coff_symbol *symbol;
895 struct coff_type *type;
898 if (symbol->type->type == coff_function_type)
908 walk_tree_type (struct coff_sfile *sfile, struct coff_symbol *symbol,
909 struct coff_type *type, int nest)
911 if (symbol->type->type == coff_function_type)
918 sysroff_swap_dty_out (file, &dty);
919 walk_tree_type_1 (sfile, symbol, type, nest);
921 sysroff_swap_dty_out (file, &dty);
924 symbol->where->section,
925 symbol->type->u.function.code,
926 BLOCK_TYPE_FUNCTION, nest);
927 wr_dps_start (sfile, symbol->where->section,
928 symbol->type->u.function.code,
929 BLOCK_TYPE_BLOCK, nest);
930 walk_tree_scope (symbol->where->section,
932 symbol->type->u.function.code,
933 nest + 1, BLOCK_TYPE_BLOCK);
935 wr_dps_end (symbol->where->section,
936 symbol->type->u.function.code,
938 wr_dps_end (symbol->where->section,
939 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
947 sysroff_swap_dty_out (file, &dty);
948 walk_tree_type_1 (sfile, symbol, type, nest);
950 sysroff_swap_dty_out (file, &dty);
955 walk_tree_symbol (struct coff_sfile *sfile, struct coff_section *section ATTRIBUTE_UNUSED, struct coff_symbol *symbol, int nest)
959 memset (&dsy, 0, sizeof(dsy));
962 switch (symbol->type->type)
964 case coff_function_type:
965 dsy.type = STYPE_FUNC;
969 case coff_structref_type:
970 case coff_pointer_type:
971 case coff_array_type:
972 case coff_basic_type:
973 case coff_enumref_type:
974 dsy.type = STYPE_VAR;
978 case coff_enumdef_type:
979 dsy.type = STYPE_TAG;
984 case coff_structdef_type:
985 dsy.type = STYPE_TAG;
987 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
990 case coff_secdef_type:
997 if (symbol->where->where == coff_where_member_of_struct)
1000 dsy.type = STYPE_MEMBER;
1003 if (symbol->where->where == coff_where_member_of_enum)
1005 dsy.type = STYPE_ENUM;
1008 dsy.evalue = symbol->where->offset;
1011 if (symbol->type->type == coff_structdef_type
1012 || symbol->where->where == coff_where_entag
1013 || symbol->where->where == coff_where_strtag)
1015 dsy.snumber = get_member_id (symbol->number);
1019 dsy.snumber = get_ordinary_id (symbol->number);
1022 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1024 switch (symbol->visible->type)
1026 case coff_vis_common:
1027 case coff_vis_ext_def:
1028 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1031 case coff_vis_ext_ref:
1032 dsy.ainfo = AINFO_STATIC_EXT_REF;
1035 case coff_vis_int_def:
1036 dsy.ainfo = AINFO_STATIC_INT;
1040 case coff_vis_autoparam:
1041 dsy.ainfo = AINFO_AUTO;
1044 case coff_vis_register:
1045 case coff_vis_regparam:
1046 dsy.ainfo = AINFO_REG;
1051 case coff_vis_member_of_struct:
1052 case coff_vis_member_of_enum:
1059 dsy.dlength = symbol->type->size;
1061 switch (symbol->where->where)
1063 case coff_where_memory:
1065 dsy.section = symbol->where->section->number;
1071 case coff_where_member_of_struct:
1072 case coff_where_member_of_enum:
1073 case coff_where_stack:
1074 case coff_where_register:
1075 case coff_where_unknown:
1076 case coff_where_strtag:
1077 case coff_where_entag:
1078 case coff_where_typedef:
1085 switch (symbol->where->where)
1087 case coff_where_memory:
1088 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1091 case coff_where_stack:
1092 dsy.address = symbol->where->offset;
1095 case coff_where_member_of_struct:
1096 if (symbol->where->bitsize)
1098 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1100 dsy.field_len = symbol->where->bitsize;
1101 dsy.field_off = (bits / 32) * 4;
1102 dsy.field_bitoff = bits % 32;
1108 dsy.field_len = symbol->type->size;
1109 dsy.field_off = symbol->where->offset;
1113 case coff_where_member_of_enum:
1115 dsy.field_len = symbol->type->size;
1116 dsy.field_off = symbol->where->offset; */
1119 case coff_where_register:
1120 case coff_where_unknown:
1121 case coff_where_strtag:
1122 case coff_where_entag:
1123 case coff_where_typedef:
1130 if (symbol->where->where == coff_where_register)
1131 dsy.reg = rnames[symbol->where->offset];
1133 switch (symbol->visible->type)
1135 case coff_vis_common:
1136 /* We do this 'cause common C symbols are treated as extdefs. */
1137 case coff_vis_ext_def:
1138 case coff_vis_ext_ref:
1139 dsy.ename = symbol->name;
1142 case coff_vis_regparam:
1143 case coff_vis_autoparam:
1144 dsy.type = STYPE_PARAMETER;
1147 case coff_vis_int_def:
1149 case coff_vis_register:
1151 case coff_vis_member_of_struct:
1152 case coff_vis_member_of_enum:
1163 sysroff_swap_dsy_out (file, &dsy);
1165 walk_tree_type (sfile, symbol, symbol->type, nest);
1169 walk_tree_scope (struct coff_section *section, struct coff_sfile *sfile, struct coff_scope *scope, int nest, int type)
1171 struct coff_symbol *vars;
1172 struct coff_scope *child;
1174 if (scope->vars_head
1175 || (scope->list_head && scope->list_head->vars_head))
1177 wr_dps_start (sfile, section, scope, type, nest);
1180 wr_globals (tree, sfile, nest + 1);
1182 for (vars = scope->vars_head; vars; vars = vars->next)
1183 walk_tree_symbol (sfile, section, vars, nest);
1185 for (child = scope->list_head; child; child = child->next)
1186 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1188 wr_dps_end (section, scope, type);
1193 walk_tree_sfile (struct coff_section *section, struct coff_sfile *sfile)
1195 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1199 wr_program_structure (struct coff_ofile *p, struct coff_sfile *sfile)
1201 walk_tree_sfile (p->sections + 4, sfile);
1205 wr_du (struct coff_ofile *p, struct coff_sfile *sfile, int n)
1211 unsigned int *lowest = (unsigned *) nints (p->nsections);
1212 unsigned int *highest = (unsigned *) nints (p->nsections);
1214 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1219 du.sections = p->nsections - 1;
1220 du.san = (int *) xcalloc (sizeof (int), du.sections);
1221 du.address = nints (du.sections);
1222 du.length = nints (du.sections);
1224 for (i = 0; i < du.sections; i++)
1231 for (j = 0; j < lim; j++)
1238 if (sfile->section[src].init)
1241 = sfile->section[src].high - sfile->section[src].low + 1;
1243 = sfile->section[src].low;
1248 du.address[dst] = 0;
1253 if (sfile->section[src].parent)
1255 printf (" section %6s 0x%08x..0x%08x\n",
1256 sfile->section[src].parent->name,
1258 du.address[dst] + du.length[dst] - 1);
1262 du.sections = dst + 1;
1268 sysroff_swap_du_out (file, &du);
1272 wr_dus (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile)
1277 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1278 dus.drb = nints (dus.ns);
1279 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1280 dus.spare = nints (dus.ns);
1282 /* Find the filenames. */
1284 dus.fname[0] = sfile->name;
1286 sysroff_swap_dus_out (file, &dus);
1290 /* Find the offset of the .text section for this sfile in the
1291 .text section for the output file. */
1294 find_base (struct coff_sfile *sfile, struct coff_section *section)
1296 return sfile->section[section->number].low;
1300 wr_dln (struct coff_ofile *p ATTRIBUTE_UNUSED, struct coff_sfile *sfile,
1301 int n ATTRIBUTE_UNUSED)
1303 /* Count up all the linenumbers */
1305 struct coff_symbol *sy;
1311 for (sy = sfile->scope->vars_head;
1315 struct coff_type *t = sy->type;
1316 if (t->type == coff_function_type)
1318 struct coff_line *l = t->u.function.lines;
1324 dln.sfn = nints (lc);
1325 dln.sln = nints (lc);
1326 dln.cc = nints (lc);
1327 dln.section = nints (lc);
1329 dln.from_address = nints (lc);
1330 dln.to_address = nints (lc);
1337 /* Run through once more and fill up the structure */
1339 for (sy = sfile->scope->vars_head;
1343 if (sy->type->type == coff_function_type)
1346 struct coff_line *l = sy->type->u.function.lines;
1349 int base = find_base (sfile, sy->where->section);
1350 for (i = 0; i < l->nlines; i++)
1352 dln.section[idx] = sy->where->section->number;
1354 dln.sln[idx] = l->lines[i];
1355 dln.from_address[idx] =
1356 l->addresses[i] + sy->where->section->address - base;
1359 dln.to_address[idx - 1] = dln.from_address[idx];
1363 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1368 sysroff_swap_dln_out (file, &dln);
1371 /* Write the global symbols out to the debug info. */
1374 wr_globals (struct coff_ofile *p, struct coff_sfile *sfile,
1375 int n ATTRIBUTE_UNUSED)
1377 struct coff_symbol *sy;
1379 for (sy = p->symbol_list_head;
1381 sy = sy->next_in_ofile_list)
1383 if (sy->visible->type == coff_vis_ext_def
1384 || sy->visible->type == coff_vis_ext_ref)
1386 /* Only write out symbols if they belong to
1387 the current source file. */
1388 if (sy->sfile == sfile)
1389 walk_tree_symbol (sfile, 0, sy, 0);
1395 wr_debug (struct coff_ofile *p)
1397 struct coff_sfile *sfile;
1400 for (sfile = p->source_head;
1402 sfile = sfile->next)
1405 printf ("%s\n", sfile->name);
1407 wr_du (p, sfile, n);
1409 wr_program_structure (p, sfile);
1410 wr_dln (p, sfile, n);
1418 /* It seems that the CS struct is not normal - the size is wrong
1419 heres one I prepared earlier. */
1424 0x00, /* number of chars in variable length part */
1456 fwrite (b, 1, sizeof (b), file);
1459 /* Write out the SC records for a unit. Create an SC
1460 for all the sections which appear in the output file, even
1461 if there isn't an equivalent one on the input. */
1464 wr_sc (struct coff_ofile *ptr, struct coff_sfile *sfile)
1468 /* First work out the total number of sections. */
1469 int total_sec = ptr->nsections;
1472 struct coff_section *sec;
1473 struct coff_symbol *symbol;
1475 struct coff_symbol *symbol;
1477 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1480 for (i = 0; i < total_sec; i++)
1482 info[i].sec = ptr->sections + i;
1486 for (symbol = sfile->scope->vars_head;
1488 symbol = symbol->next)
1491 if (symbol->type->type == coff_secdef_type)
1493 for (i = 0; i < total_sec; i++)
1495 if (symbol->where->section == info[i].sec)
1497 info[i].symbol = symbol;
1504 /* Now output all the section info, and fake up some stuff for sections
1506 for (i = 1; i < total_sec; i++)
1511 symbol = info[i].symbol;
1517 /* Don't have a symbol set aside for this section, which means
1518 that nothing in this file does anything for the section. */
1519 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1522 name = info[i].sec->name;
1526 if (bfd_get_file_flags (abfd) & EXEC_P)
1529 sc.addr = symbol->where->offset;
1536 sc.length = symbol->type->size;
1537 name = symbol->name;
1541 sc.concat = CONCAT_SIMPLE;
1549 sc.spare1 = 0; /* If not zero, then it doesn't work. */
1550 sc.name = section_translate (name);
1552 if (strlen (sc.name) == 1)
1558 sc.contents = CONTENTS_DATA;
1562 sc.contents = CONTENTS_CODE;
1567 sc.contents = CONTENTS_CODE;
1570 sysroff_swap_sc_out (file, &sc);
1576 /* Write out the ER records for a unit. */
1579 wr_er (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1583 struct coff_symbol *sym;
1587 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1589 if (sym->visible->type == coff_vis_ext_ref)
1594 er.type = ER_NOTSPEC;
1595 er.name = sym->name;
1596 sysroff_swap_er_out (file, &er);
1597 sym->er_number = idx++;
1603 /* Write out the ED records for a unit. */
1606 wr_ed (struct coff_ofile *ptr, struct coff_sfile *sfile ATTRIBUTE_UNUSED,
1609 struct coff_symbol *s;
1613 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1615 if (s->visible->type == coff_vis_ext_def
1616 || s->visible->type == coff_vis_common)
1620 ed.section = s->where->section->number;
1623 if (s->where->section->data)
1625 ed.type = ED_TYPE_DATA;
1627 else if (s->where->section->code & SEC_CODE)
1629 ed.type = ED_TYPE_ENTRY;
1633 ed.type = ED_TYPE_NOTSPEC;
1634 ed.type = ED_TYPE_DATA;
1637 ed.address = s->where->offset - s->where->section->address;
1639 sysroff_swap_ed_out (file, &ed);
1646 wr_unit_info (struct coff_ofile *ptr)
1648 struct coff_sfile *sfile;
1651 for (sfile = ptr->source_head;
1653 sfile = sfile->next)
1660 wr_un (ptr, sfile, first, 0);
1661 nsecs = wr_sc (ptr, sfile);
1663 fseek (file, p1, SEEK_SET);
1664 wr_un (ptr, sfile, first, nsecs);
1665 fseek (file, p2, SEEK_SET);
1666 wr_er (ptr, sfile, first);
1667 wr_ed (ptr, sfile, first);
1673 wr_module (struct coff_ofile *p)
1686 return (x + 3) & ~3;
1689 /* Find all the common variables and turn them into
1690 ordinary defs - dunno why, but thats what hitachi does with 'em. */
1693 prescan (struct coff_ofile *tree)
1695 struct coff_symbol *s;
1696 struct coff_section *common_section;
1698 /* Find the common section - always section 3. */
1699 common_section = tree->sections + 3;
1701 for (s = tree->symbol_list_head;
1703 s = s->next_in_ofile_list)
1705 if (s->visible->type == coff_vis_common)
1707 struct coff_where *w = s->where;
1708 /* s->visible->type = coff_vis_ext_def; leave it as common */
1709 common_section->size = align (common_section->size);
1710 w->offset = common_section->size + common_section->address;
1711 w->section = common_section;
1712 common_section->size += s->type->size;
1713 common_section->size = align (common_section->size);
1721 show_usage (FILE *file, int status)
1723 fprintf (file, _("Usage: %s [option(s)] in-file [out-file]\n"), program_name);
1724 fprintf (file, _("Convert a COFF object file into a SYSROFF object file\n"));
1725 fprintf (file, _(" The options are:\n\
1726 -q --quick (Obsolete - ignored)\n\
1727 -n --noprescan Do not perform a scan to convert commons into defs\n\
1728 -d --debug Display information about what is being done\n\
1729 @<file> Read options from <file>\n\
1730 -h --help Display this information\n\
1731 -v --version Print the program's version number\n"));
1733 if (REPORT_BUGS_TO[0] && status == 0)
1734 fprintf (file, _("Report bugs to %s\n"), REPORT_BUGS_TO);
1739 main (int ac, char **av)
1742 static struct option long_options[] =
1744 {"debug", no_argument, 0, 'd'},
1745 {"quick", no_argument, 0, 'q'},
1746 {"noprescan", no_argument, 0, 'n'},
1747 {"help", no_argument, 0, 'h'},
1748 {"version", no_argument, 0, 'V'},
1749 {NULL, no_argument, 0, 0}
1755 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1756 setlocale (LC_MESSAGES, "");
1758 #if defined (HAVE_SETLOCALE)
1759 setlocale (LC_CTYPE, "");
1761 bindtextdomain (PACKAGE, LOCALEDIR);
1762 textdomain (PACKAGE);
1764 program_name = av[0];
1765 xmalloc_set_program_name (program_name);
1767 expandargv (&ac, &av);
1769 while ((opt = getopt_long (ac, av, "dHhVvqn", long_options,
1786 show_usage (stdout, 0);
1790 print_version ("srconv");
1796 show_usage (stderr, 1);
1801 /* The input and output files may be named on the command line. */
1805 input_file = av[optind];
1809 output_file = av[optind];
1812 show_usage (stderr, 1);
1813 if (strcmp (input_file, output_file) == 0)
1815 fatal (_("input and output files must be different"));
1824 fatal (_("no input file specified"));
1829 /* Take a .o off the input file and stick on a .obj. If
1830 it doesn't end in .o, then stick a .obj on anyway */
1832 int len = strlen (input_file);
1834 output_file = xmalloc (len + 5);
1835 strcpy (output_file, input_file);
1838 && output_file[len - 2] == '.'
1839 && output_file[len - 1] == 'o')
1841 output_file[len] = 'b';
1842 output_file[len + 1] = 'j';
1843 output_file[len + 2] = 0;
1847 strcat (output_file, ".obj");
1851 abfd = bfd_openr (input_file, 0);
1854 bfd_fatal (input_file);
1856 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1858 bfd_nonfatal (input_file);
1860 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
1862 list_matching_formats (matching);
1868 file = fopen (output_file, FOPEN_WB);
1871 fatal (_("unable to open output file %s"), output_file);
1874 printf ("ids %d %d\n", base1, base2);
1876 tree = coff_grok (abfd);