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
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"
39 #define PROGRAM_VERSION "1.5"
43 static char *toolname;
46 static int get_member_id PARAMS ((int));
47 static int get_ordinary_id PARAMS ((int));
48 static char *section_translate PARAMS ((char *));
49 static char *strip_suffix PARAMS ((char *));
50 static void checksum PARAMS ((FILE *, char *, int, int));
51 static void writeINT PARAMS ((int, char *, int *, int, FILE *));
52 static void writeBITS PARAMS ((int, char *, int *, int));
53 static void writeBARRAY PARAMS ((barray, char *, int *, int, FILE *));
54 static void writeCHARS PARAMS ((char *, char *, int *, int, FILE *));
55 static void wr_tr PARAMS ((void));
56 static void wr_un PARAMS ((struct coff_ofile *, struct coff_sfile *, int, int));
57 static void wr_hd PARAMS ((struct coff_ofile *));
58 static void wr_sh PARAMS ((struct coff_ofile *, struct coff_section *));
59 static void wr_ob PARAMS ((struct coff_ofile *, struct coff_section *));
60 static void wr_rl PARAMS ((struct coff_ofile *, struct coff_section *));
61 static void wr_object_body PARAMS ((struct coff_ofile *));
62 static void wr_dps_start
63 PARAMS ((struct coff_sfile *, struct coff_section *, struct coff_scope *,
65 static void wr_dps_end
66 PARAMS ((struct coff_section *, struct coff_scope *, int));
67 static int *nints PARAMS ((int));
68 static void walk_tree_type_1
69 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
71 static void walk_tree_type
72 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
74 static void walk_tree_symbol
75 PARAMS ((struct coff_sfile *, struct coff_section *,
76 struct coff_symbol *, int));
77 static void walk_tree_scope
78 PARAMS ((struct coff_section *, struct coff_sfile *, struct coff_scope *,
80 static void walk_tree_sfile
81 PARAMS ((struct coff_section *, struct coff_sfile *));
82 static void wr_program_structure
83 PARAMS ((struct coff_ofile *, struct coff_sfile *));
84 static void wr_du PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
85 static void wr_dus PARAMS ((struct coff_ofile *, struct coff_sfile *));
86 static int find_base PARAMS ((struct coff_sfile *, struct coff_section *));
87 static void wr_dln PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
88 static void wr_globals
89 PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
90 static void wr_debug PARAMS ((struct coff_ofile *));
91 static void wr_cs PARAMS ((void));
92 static int wr_sc PARAMS ((struct coff_ofile *, struct coff_sfile *));
93 static void wr_er PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
94 static void wr_ed PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
95 static void wr_unit_info PARAMS ((struct coff_ofile *));
96 static void wr_module PARAMS ((struct coff_ofile *));
97 static int align PARAMS ((int));
98 static void prescan PARAMS ((struct coff_ofile *));
99 static void show_usage PARAMS ((FILE *, int));
100 static void show_help PARAMS ((void));
101 extern int main PARAMS ((int, char **));
105 static int debug = 0;
106 static int quick = 0;
107 static int noprescan = 0;
108 static struct coff_ofile *tree;
110 static int absolute_p;
113 static int segmented_p;
116 static int ids1[20000];
117 static int ids2[20000];
119 static int base1 = 0x18;
120 static int base2 = 0x2018;
146 section_translate (n)
149 if (strcmp (n, ".text") == 0)
151 if (strcmp (n, ".data") == 0)
153 if (strcmp (n, ".bss") == 0)
160 #define DATE "940201073000"; /* Just a time on my birthday */
170 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
172 res = (char *) xmalloc (i + 1);
173 memcpy (res, name, i);
179 /* IT LEN stuff CS */
181 checksum (file, ptr, size, code)
190 int bytes = size / 8;
191 last = !(code & 0xff00);
194 ptr[0] = code | (last ? 0x80 : 0);
197 for (j = 0; j < bytes; j++)
201 /* Glue on a checksum too */
203 fwrite (ptr, bytes + 1, 1, file);
210 writeINT (n, ptr, idx, size, file)
226 /* Lets write out that record and do another one */
227 checksum (file, ptr, *idx, code | 0x1000);
239 ptr[byte + 0] = n >> 8;
243 ptr[byte + 0] = n >> 24;
244 ptr[byte + 1] = n >> 16;
245 ptr[byte + 2] = n >> 8;
246 ptr[byte + 3] = n >> 0;
256 writeBITS (val, ptr, idx, size)
268 /* Turn off all about to change bits */
269 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
270 /* Turn on the bits we want */
271 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
276 writeBARRAY (data, ptr, idx, size, file)
280 int size ATTRIBUTE_UNUSED;
284 writeINT (data.len, ptr, idx, 1, file);
285 for (i = 0; i < data.len; i++)
287 writeINT (data.data[i], ptr, idx, 1, file);
293 writeCHARS (string, ptr, idx, size, file)
304 /* Lets write out that record and do another one */
305 checksum (file, ptr, *idx, code | 0x1000);
312 /* Variable length string */
313 size = strlen (string);
317 /* BUG WAITING TO HAPPEN */
318 memcpy (ptr + i, string, size);
323 #define SYSROFF_SWAP_OUT
327 static char *rname_sh[] =
329 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
332 static char *rname_h8300[] =
334 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
340 /* The TR block is not normal - it doesn't have any contents. */
347 fwrite (b, 1, sizeof (b), file);
351 wr_un (ptr, sfile, first, nsecs)
352 struct coff_ofile *ptr;
353 struct coff_sfile *sfile;
355 int nsecs ATTRIBUTE_UNUSED;
359 struct coff_symbol *s;
363 if (bfd_get_file_flags (abfd) & EXEC_P)
364 un.format = FORMAT_LM;
366 un.format = FORMAT_OM;
371 un.nsections = ptr->nsections - 1; /* Don't count the abs section */
373 /*NEW - only count sections with size */
374 un.nsections = nsecs;
379 /* Count all the undefined and defined variables with global scope */
383 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
385 if (s->visible->type == coff_vis_ext_def
386 || s->visible->type == coff_vis_common)
389 if (s->visible->type == coff_vis_ext_ref)
395 un.linker = "L_GX00";
397 un.name = sfile->name;
398 sysroff_swap_un_out (file, &un);
404 struct coff_ofile *p;
409 if (bfd_get_file_flags (abfd) & EXEC_P)
411 hd.mt = MTYPE_ABS_LM;
415 hd.mt = MTYPE_OMS_OR_LMS;
419 hd.nu = p->nsources; /* Always one unit */
420 hd.code = 0; /* Always ASCII */
421 hd.ver = "0200"; /* Version 2.00 */
422 switch (bfd_get_arch (abfd))
430 switch (bfd_get_mach (abfd))
436 toolname = "C_H8/300";
438 case bfd_mach_h8300h:
442 toolname = "C_H8/300H";
444 case bfd_mach_h8300s:
448 toolname = "C_H8/300S";
453 rnames = rname_h8300;
471 if (! bfd_get_file_flags(abfd) & EXEC_P)
481 hd.address = bfd_get_start_address (abfd);
486 hd.mn = strip_suffix (bfd_get_filename (abfd));
488 sysroff_swap_hd_out (file, &hd);
494 struct coff_ofile *p ATTRIBUTE_UNUSED;
495 struct coff_section *sec;
499 sh.section = sec->number;
503 sysroff_swap_sh_out (file, &sh);
509 struct coff_ofile *p ATTRIBUTE_UNUSED;
510 struct coff_section *section;
514 unsigned char stuff[200];
517 while (i < section->bfd_section->_raw_size)
520 int todo = 200; /* Copy in 200 byte lumps */
522 if (i + todo > section->bfd_section->_raw_size)
523 todo = section->bfd_section->_raw_size - i;
528 if (bfd_get_file_flags (abfd) & EXEC_P)
529 ob.address = section->address;
540 ob.cpf = 0; /* Never compress */
542 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
543 ob.data.data = stuff;
544 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
547 /* Now fill the rest with blanks */
548 while (i < (bfd_size_type) section->size)
551 int todo = 200; /* Copy in 200 byte lumps */
553 if (i + todo > (bfd_size_type) section->size)
554 todo = section->size - i;
557 ob.cpf = 0; /* Never compress */
559 memset (stuff, 0, todo);
560 ob.data.data = stuff;
561 sysroff_swap_ob_out (file, &ob);
564 /* Now fill the rest with blanks */
570 struct coff_ofile *ptr ATTRIBUTE_UNUSED;
571 struct coff_section *sec;
573 int nr = sec->nrelocs;
575 for (i = 0; i < nr; i++)
577 struct coff_reloc *r = sec->relocs + i;
578 struct coff_symbol *ref;
587 rl.flen = 32; /* SH Specific */
588 /* What sort of reloc ? Look in the section to find out */
590 if (ref->visible->type == coff_vis_ext_ref)
592 rl.bcount = 4; /* Always 4 for us */
594 rl.symn = ref->er_number;
596 else if (ref->visible->type == coff_vis_common)
598 rl.bcount = 11; /* Always 11 for us */
600 rl.secn = ref->where->section->number;
603 rl.addend = ref->where->offset - ref->where->section->address;
604 rl.aopcode_is_0x20 = 0x20;
609 rl.bcount = 11; /* Always 11 for us */
611 rl.secn = ref->where->section->number;
614 rl.addend = -ref->where->section->address;
615 rl.aopcode_is_0x20 = 0x20;
618 if (rl.op == OP_SEC_REF
619 || rl.op == OP_EXT_REF)
621 sysroff_swap_rl_out (file, &rl);
628 struct coff_ofile *p;
631 for (i = 1; i < p->nsections; i++)
633 wr_sh (p, p->sections + i);
634 wr_ob (p, p->sections + i);
635 wr_rl (p, p->sections + i);
640 wr_dps_start (sfile, section, scope, type, nest)
641 struct coff_sfile *sfile;
642 struct coff_section *section ATTRIBUTE_UNUSED;
643 struct coff_scope *scope;
653 dps.san = scope->sec->number;
654 dps.address = scope->offset - find_base (sfile, scope->sec);
655 dps.block_size = scope->size;
658 printf ("DPS %s %d %x\n",
674 sysroff_swap_dps_out (file, &dps);
678 wr_dps_end (section, scope, type)
679 struct coff_section *section ATTRIBUTE_UNUSED;
680 struct coff_scope *scope ATTRIBUTE_UNUSED;
686 sysroff_swap_dps_out (file, &dps);
693 return (int *) (xcalloc (sizeof (int), x));
697 walk_tree_type_1 (sfile, symbol, type, nest)
698 struct coff_sfile *sfile;
699 struct coff_symbol *symbol;
700 struct coff_type *type;
705 case coff_secdef_type:
706 case coff_basic_type:
710 switch (type->u.basic)
714 dbt.btype = BTYPE_VOID;
715 dbt.sign = BTYPE_UNSPEC;
716 dbt.fptype = FPTYPE_NOTSPEC;
719 dbt.btype = BTYPE_CHAR;
720 dbt.sign = BTYPE_UNSPEC;
721 dbt.fptype = FPTYPE_NOTSPEC;
726 dbt.btype = BTYPE_INT;
727 dbt.sign = SIGN_SIGNED;
728 dbt.fptype = FPTYPE_NOTSPEC;
731 dbt.btype = BTYPE_FLOAT;
732 dbt.fptype = FPTYPE_SINGLE;
735 dbt.btype = BTYPE_FLOAT;
736 dbt.fptype = FPTYPE_DOUBLE;
739 dbt.btype = BTYPE_FLOAT;
740 dbt.fptype = FPTYPE_EXTENDED;
743 dbt.btype = BTYPE_CHAR;
744 dbt.sign = SIGN_UNSIGNED;
745 dbt.fptype = FPTYPE_NOTSPEC;
750 dbt.btype = BTYPE_INT;
751 dbt.sign = SIGN_UNSIGNED;
752 dbt.fptype = FPTYPE_NOTSPEC;
755 dbt.bitsize = type->size;
757 sysroff_swap_dbt_out (file, &dbt);
760 case coff_pointer_type:
763 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
765 sysroff_swap_dpt_out (file, &dpt);
769 case coff_function_type:
772 struct coff_symbol *param;
775 dfp.nparams = type->u.function.parameters->nvars;
778 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
780 sysroff_swap_dfp_out (file, &dfp);
782 for (param = type->u.function.parameters->vars_head;
786 walk_tree_symbol (sfile, 0, param, nest);
789 sysroff_swap_dfp_out (file, &dfp);
793 case coff_structdef_type:
797 struct coff_symbol *member;
799 dbt.btype = BTYPE_STRUCT;
800 dbt.bitsize = type->size;
801 dbt.sign = SIGN_UNSPEC;
802 dbt.fptype = FPTYPE_NOTSPEC;
803 dbt.sid = get_member_id (type->u.astructdef.idx);
805 sysroff_swap_dbt_out (file, &dbt);
808 sysroff_swap_dds_out (file, &dds);
809 for (member = type->u.astructdef.elements->vars_head;
811 member = member->next)
813 walk_tree_symbol (sfile, 0, member, nest + 1);
817 sysroff_swap_dds_out (file, &dds);
821 case coff_structref_type:
824 dbt.btype = BTYPE_TAG;
825 dbt.bitsize = type->size;
826 dbt.sign = SIGN_UNSPEC;
827 dbt.fptype = FPTYPE_NOTSPEC;
828 if (type->u.astructref.ref)
830 dbt.sid = get_member_id (type->u.astructref.ref->number);
838 sysroff_swap_dbt_out (file, &dbt);
841 case coff_array_type:
845 int dims = 1; /* Only output one dimension at a time */
847 dar.variable = nints (dims);
848 dar.subtype = nints (dims);
849 dar.spare = nints (dims);
850 dar.max_variable = nints (dims);
851 dar.maxspare = nints (dims);
852 dar.max = nints (dims);
853 dar.min_variable = nints (dims);
854 dar.min = nints (dims);
855 dar.minspare = nints (dims);
857 dar.length = type->size / type->u.array.dim;
858 for (j = 0; j < dims; j++)
860 dar.variable[j] = VARIABLE_FIXED;
861 dar.subtype[j] = SUB_INTEGER;
863 dar.max_variable[j] = 0;
864 dar.max[j] = type->u.array.dim;
865 dar.min_variable[j] = 0;
866 dar.min[j] = 1; /* Why isn't this 0 ? */
868 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
869 sysroff_swap_dar_out (file, &dar);
872 case coff_enumdef_type:
876 struct coff_symbol *member;
877 dbt.btype = BTYPE_ENUM;
878 dbt.bitsize = type->size;
879 dbt.sign = SIGN_UNSPEC;
880 dbt.fptype = FPTYPE_NOTSPEC;
881 dbt.sid = get_member_id (type->u.aenumdef.idx);
883 sysroff_swap_dbt_out (file, &dbt);
888 sysroff_swap_den_out (file, &den);
889 for (member = type->u.aenumdef.elements->vars_head;
891 member = member->next)
893 walk_tree_symbol (sfile, 0, member, nest + 1);
897 sysroff_swap_den_out (file, &den);
902 case coff_enumref_type:
905 dbt.btype = BTYPE_TAG;
906 dbt.bitsize = type->size;
907 dbt.sign = SIGN_UNSPEC;
908 dbt.fptype = FPTYPE_NOTSPEC;
909 dbt.sid = get_member_id (type->u.aenumref.ref->number);
911 sysroff_swap_dbt_out (file, &dbt);
927 sysroff_swap_dty_out (file, &dty);
937 sysroff_swap_dty_out (file, &dty);
942 dump_tree_structure (sfile, symbol, type, nest)
943 struct coff_sfile *sfile;
944 struct coff_symbol *symbol;
945 struct coff_type *type;
948 if (symbol->type->type == coff_function_type)
958 walk_tree_type (sfile, symbol, type, nest)
962 struct coff_symbol *symbol;
963 struct coff_type *type;
966 if (symbol->type->type == coff_function_type)
973 sysroff_swap_dty_out (file, &dty);
974 walk_tree_type_1 (sfile, symbol, type, nest);
976 sysroff_swap_dty_out (file, &dty);
979 symbol->where->section,
980 symbol->type->u.function.code,
981 BLOCK_TYPE_FUNCTION, nest);
982 wr_dps_start (sfile, symbol->where->section,
983 symbol->type->u.function.code,
984 BLOCK_TYPE_BLOCK, nest);
985 walk_tree_scope (symbol->where->section,
987 symbol->type->u.function.code,
988 nest + 1, BLOCK_TYPE_BLOCK);
990 wr_dps_end (symbol->where->section,
991 symbol->type->u.function.code,
993 wr_dps_end (symbol->where->section,
994 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
1002 sysroff_swap_dty_out (file, &dty);
1003 walk_tree_type_1 (sfile, symbol, type, nest);
1005 sysroff_swap_dty_out (file, &dty);
1013 walk_tree_symbol (sfile, section, symbol, nest)
1014 struct coff_sfile *sfile;
1015 struct coff_section *section ATTRIBUTE_UNUSED;
1016 struct coff_symbol *symbol;
1021 memset(&dsy, 0, sizeof(dsy));
1024 switch (symbol->type->type)
1026 case coff_function_type:
1027 dsy.type = STYPE_FUNC;
1030 case coff_structref_type:
1031 case coff_pointer_type:
1032 case coff_array_type:
1033 case coff_basic_type:
1034 case coff_enumref_type:
1035 dsy.type = STYPE_VAR;
1038 case coff_enumdef_type:
1039 dsy.type = STYPE_TAG;
1043 case coff_structdef_type:
1044 dsy.type = STYPE_TAG;
1046 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
1048 case coff_secdef_type:
1054 if (symbol->where->where == coff_where_member_of_struct)
1057 dsy.type = STYPE_MEMBER;
1059 if (symbol->where->where == coff_where_member_of_enum)
1061 dsy.type = STYPE_ENUM;
1064 dsy.evalue = symbol->where->offset;
1067 if (symbol->type->type == coff_structdef_type
1068 || symbol->where->where == coff_where_entag
1069 || symbol->where->where == coff_where_strtag)
1071 dsy.snumber = get_member_id (symbol->number);
1075 dsy.snumber = get_ordinary_id (symbol->number);
1079 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1081 switch (symbol->visible->type)
1083 case coff_vis_common:
1084 case coff_vis_ext_def:
1085 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1087 case coff_vis_ext_ref:
1088 dsy.ainfo = AINFO_STATIC_EXT_REF;
1090 case coff_vis_int_def:
1091 dsy.ainfo = AINFO_STATIC_INT;
1094 case coff_vis_autoparam:
1095 dsy.ainfo = AINFO_AUTO;
1097 case coff_vis_register:
1098 case coff_vis_regparam:
1099 dsy.ainfo = AINFO_REG;
1103 case coff_vis_member_of_struct:
1104 case coff_vis_member_of_enum:
1110 dsy.dlength = symbol->type->size;
1111 switch (symbol->where->where)
1113 case coff_where_memory:
1115 dsy.section = symbol->where->section->number;
1120 case coff_where_member_of_struct:
1121 case coff_where_member_of_enum:
1122 case coff_where_stack:
1123 case coff_where_register:
1124 case coff_where_unknown:
1125 case coff_where_strtag:
1127 case coff_where_entag:
1128 case coff_where_typedef:
1134 switch (symbol->where->where)
1136 case coff_where_memory:
1137 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1139 case coff_where_stack:
1140 dsy.address = symbol->where->offset;
1142 case coff_where_member_of_struct:
1145 if (symbol->where->bitsize)
1147 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1149 dsy.field_len = symbol->where->bitsize;
1150 dsy.field_off = (bits / 32) * 4;
1151 dsy.field_bitoff = bits % 32;
1157 dsy.field_len = symbol->type->size;
1158 dsy.field_off = symbol->where->offset;
1161 case coff_where_member_of_enum:
1163 dsy.field_len = symbol->type->size;
1164 dsy.field_off = symbol->where->offset; */
1166 case coff_where_register:
1167 case coff_where_unknown:
1168 case coff_where_strtag:
1170 case coff_where_entag:
1171 case coff_where_typedef:
1177 if (symbol->where->where == coff_where_register)
1178 dsy.reg = rnames[symbol->where->offset];
1180 switch (symbol->visible->type)
1182 case coff_vis_common:
1183 /* We do this 'cause common C symbols are treated as extdefs */
1184 case coff_vis_ext_def:
1185 case coff_vis_ext_ref:
1187 dsy.ename = symbol->name;
1190 case coff_vis_regparam:
1191 case coff_vis_autoparam:
1192 dsy.type = STYPE_PARAMETER;
1195 case coff_vis_int_def:
1198 case coff_vis_register:
1200 case coff_vis_member_of_struct:
1201 case coff_vis_member_of_enum:
1213 sysroff_swap_dsy_out (file, &dsy);
1215 walk_tree_type (sfile, symbol, symbol->type, nest);
1220 walk_tree_scope (section, sfile, scope, nest, type)
1221 struct coff_section *section;
1222 struct coff_sfile *sfile;
1223 struct coff_scope *scope;
1227 struct coff_symbol *vars;
1228 struct coff_scope *child;
1230 if (scope->vars_head
1231 || (scope->list_head && scope->list_head->vars_head))
1233 wr_dps_start (sfile, section, scope, type, nest);
1236 wr_globals (tree, sfile, nest + 1);
1238 for (vars = scope->vars_head; vars; vars = vars->next)
1240 walk_tree_symbol (sfile, section, vars, nest);
1243 for (child = scope->list_head; child; child = child->next)
1245 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1248 wr_dps_end (section, scope, type);
1252 walk_tree_sfile (section, sfile)
1253 struct coff_section *section;
1254 struct coff_sfile *sfile;
1256 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1261 wr_program_structure (p, sfile)
1262 struct coff_ofile *p;
1263 struct coff_sfile *sfile;
1266 walk_tree_sfile (p->sections + 4, sfile);
1272 struct coff_ofile *p;
1273 struct coff_sfile *sfile;
1279 struct coff_symbol *symbol;
1280 static int incit = 0x500000;
1285 unsigned int *lowest = (unsigned *) nints (p->nsections);
1286 unsigned int *highest = (unsigned *) nints (p->nsections);
1287 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1292 du.sections = p->nsections - 1;
1293 du.san = (int *) xcalloc (sizeof (int), du.sections);
1294 du.address = nints (du.sections);
1295 du.length = nints (du.sections);
1297 for (i = 0; i < du.sections; i++)
1303 /* Look through all the symbols and try and work out the extents in this
1306 for (symbol = sfile->scope->vars_head;
1308 symbol = symbol->next)
1310 if (symbol->type->type == coff_secdef_type)
1312 unsigned int low = symbol->where->offset;
1313 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1314 struct coff_section *section = symbol->where->section;
1316 int sn = section->number;
1317 if (low < lowest[sn])
1319 if (high > highest[sn])
1325 for (i = 0; i < du.sections; i++)
1327 if (highest[i] == 0)
1329 lowest[i] = highest[i] = incit;
1332 du.length[used] = highest[i] - lowest[i];
1333 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1336 printf (" section %6s 0x%08x..0x%08x\n",
1337 p->sections[i + 1].name,
1346 for (j = 0; j < lim; j++)
1351 if (sfile->section[src].init)
1354 = sfile->section[src].high - sfile->section[src].low + 1;
1356 = sfile->section[src].low;
1361 du.address[dst] = 0;
1365 if (sfile->section[src].parent)
1367 printf (" section %6s 0x%08x..0x%08x\n",
1368 sfile->section[src].parent->name,
1370 du.address[dst] + du.length[dst] - 1);
1373 du.sections = dst + 1;
1379 sysroff_swap_du_out (file, &du);
1384 struct coff_ofile *p ATTRIBUTE_UNUSED;
1385 struct coff_sfile *sfile;
1391 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1392 dus.drb = nints (dus.ns);
1393 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1394 dus.spare = nints (dus.ns);
1396 /* Find the filenames */
1400 for (sfile = p->source_head;
1402 sfile = sfile->next)
1406 dus.fname[i] = sfile->name;
1411 dus.fname[0] = sfile->name;
1414 sysroff_swap_dus_out (file, &dus);
1418 /* Find the offset of the .text section for this sfile in the
1419 .text section for the output file */
1422 find_base (sfile, section)
1423 struct coff_sfile *sfile;
1424 struct coff_section *section;
1426 return sfile->section[section->number].low;
1430 wr_dln (p, sfile, n)
1431 struct coff_ofile *p ATTRIBUTE_UNUSED;
1432 struct coff_sfile *sfile;
1433 int n ATTRIBUTE_UNUSED;
1439 /* Count up all the linenumbers */
1440 struct coff_symbol *sy;
1446 for (sy = p->symbol_list_head;
1448 sy = sy->next_in_ofile_list)
1450 struct coff_type *t = sy->type;
1451 if (t->type == coff_function_type)
1453 struct coff_line *l = t->u.function.lines;
1458 dln.sfn = nints (lc);
1459 dln.sln = nints (lc);
1460 dln.lln = nints (lc);
1461 dln.section = nints (lc);
1463 dln.from_address = nints (lc);
1464 dln.to_address = nints (lc);
1471 /* Run through once more and fill up the structure */
1473 for (sy = p->symbol_list_head;
1475 sy = sy->next_in_ofile_list)
1477 if (sy->type->type == coff_function_type)
1480 struct coff_line *l = sy->type->u.function.lines;
1481 for (i = 0; i < l->nlines; i++)
1483 dln.section[idx] = sy->where->section->number;
1485 dln.sln[idx] = l->lines[i];
1486 dln.from_address[idx] = l->addresses[i];
1488 dln.to_address[idx - 1] = dln.from_address[idx];
1494 sysroff_swap_dln_out (file, &dln);
1499 /* Count up all the linenumbers */
1501 struct coff_symbol *sy;
1507 for (sy = sfile->scope->vars_head;
1511 struct coff_type *t = sy->type;
1512 if (t->type == coff_function_type)
1514 struct coff_line *l = t->u.function.lines;
1520 dln.sfn = nints (lc);
1521 dln.sln = nints (lc);
1522 dln.cc = nints (lc);
1523 dln.section = nints (lc);
1525 dln.from_address = nints (lc);
1526 dln.to_address = nints (lc);
1533 /* Run through once more and fill up the structure */
1535 for (sy = sfile->scope->vars_head;
1539 if (sy->type->type == coff_function_type)
1542 struct coff_line *l = sy->type->u.function.lines;
1545 int base = find_base (sfile, sy->where->section);
1546 for (i = 0; i < l->nlines; i++)
1548 dln.section[idx] = sy->where->section->number;
1550 dln.sln[idx] = l->lines[i];
1551 dln.from_address[idx] =
1552 l->addresses[i] + sy->where->section->address - base;
1555 dln.to_address[idx - 1] = dln.from_address[idx];
1559 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1564 sysroff_swap_dln_out (file, &dln);
1568 /* Write the global symbols out to the debug info */
1570 wr_globals (p, sfile, n)
1571 struct coff_ofile *p;
1572 struct coff_sfile *sfile;
1573 int n ATTRIBUTE_UNUSED;
1575 struct coff_symbol *sy;
1576 for (sy = p->symbol_list_head;
1578 sy = sy->next_in_ofile_list)
1580 if (sy->visible->type == coff_vis_ext_def
1581 || sy->visible->type == coff_vis_ext_ref)
1583 /* Only write out symbols if they belong to
1584 the current source file */
1585 if (sy->sfile == sfile)
1586 walk_tree_symbol (sfile, 0, sy, 0);
1594 struct coff_ofile *p;
1596 struct coff_sfile *sfile;
1598 for (sfile = p->source_head;
1600 sfile = sfile->next)
1605 printf ("%s\n", sfile->name);
1607 wr_du (p, sfile, n);
1609 wr_program_structure (p, sfile);
1610 wr_dln (p, sfile, n);
1618 /* It seems that the CS struct is not normal - the size is wrong
1619 heres one I prepared earlier.. */
1623 0x00, /* number of chars in variable length part */
1655 fwrite (b, 1, sizeof (b), file);
1658 /* Write out the SC records for a unit. Create an SC
1659 for all the sections which appear in the output file, even
1660 if there isn't an equivalent one on the input */
1664 struct coff_ofile *ptr;
1665 struct coff_sfile *sfile;
1669 /* First work out the total number of sections */
1671 int total_sec = ptr->nsections;
1675 struct coff_section *sec;
1676 struct coff_symbol *symbol;
1678 struct coff_symbol *symbol;
1681 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1685 for (i = 0; i < total_sec; i++)
1687 info[i].sec = ptr->sections + i;
1691 for (symbol = sfile->scope->vars_head;
1693 symbol = symbol->next)
1696 if (symbol->type->type == coff_secdef_type)
1698 for (i = 0; i < total_sec; i++)
1700 if (symbol->where->section == info[i].sec)
1702 info[i].symbol = symbol;
1709 /* Now output all the section info, and fake up some stuff for sections
1712 for (i = 1; i < total_sec; i++)
1716 symbol = info[i].symbol;
1721 /* Don't have a symbol set aside for this section, which means that nothing
1722 in this file does anything for the section. */
1723 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1726 name = info[i].sec->name;
1730 if (bfd_get_file_flags (abfd) & EXEC_P)
1733 sc.addr = symbol->where->offset;
1740 sc.length = symbol->type->size;
1741 name = symbol->name;
1746 sc.concat = CONCAT_SIMPLE;
1754 sc.spare1 = 0; /* If not zero, then it doesn't work */
1755 sc.name = section_translate (name);
1756 if (strlen (sc.name) == 1)
1762 sc.contents = CONTENTS_DATA;
1765 sc.contents = CONTENTS_CODE;
1770 sc.contents = CONTENTS_CODE;
1776 sysroff_swap_sc_out (file, &sc);
1786 /* Write out the ER records for a unit. */
1788 wr_er (ptr, sfile, first)
1789 struct coff_ofile *ptr;
1790 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1794 struct coff_symbol *sym;
1797 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1799 if (sym->visible->type == coff_vis_ext_ref)
1803 er.type = ER_NOTSPEC;
1804 er.name = sym->name;
1805 sysroff_swap_er_out (file, &er);
1806 sym->er_number = idx++;
1812 /* Write out the ED records for a unit. */
1814 wr_ed (ptr, sfile, first)
1815 struct coff_ofile *ptr;
1816 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
1819 struct coff_symbol *s;
1822 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1824 if (s->visible->type == coff_vis_ext_def
1825 || s->visible->type == coff_vis_common)
1829 ed.section = s->where->section->number;
1831 if (s->where->section->data)
1833 ed.type = ED_TYPE_DATA;
1835 else if (s->where->section->code & SEC_CODE)
1837 ed.type = ED_TYPE_ENTRY;
1841 ed.type = ED_TYPE_NOTSPEC;
1842 ed.type = ED_TYPE_DATA;
1844 ed.address = s->where->offset - s->where->section->address;
1846 sysroff_swap_ed_out (file, &ed);
1854 struct coff_ofile *ptr;
1856 struct coff_sfile *sfile;
1858 for (sfile = ptr->source_head;
1860 sfile = sfile->next)
1866 wr_un (ptr, sfile, first, 0);
1867 nsecs = wr_sc (ptr, sfile);
1869 fseek (file, p1, SEEK_SET);
1870 wr_un (ptr, sfile, first, nsecs);
1871 fseek (file, p2, SEEK_SET);
1872 wr_er (ptr, sfile, first);
1873 wr_ed (ptr, sfile, first);
1880 struct coff_ofile *p;
1894 return (x + 3) & ~3;
1897 /* Find all the common variables and turn them into
1898 ordinary defs - dunno why, but thats what hitachi does with 'em */
1902 struct coff_ofile *tree;
1904 struct coff_symbol *s;
1905 struct coff_section *common_section;
1906 /* Find the common section - always section 3 */
1907 common_section = tree->sections + 3;
1908 for (s = tree->symbol_list_head;
1910 s = s->next_in_ofile_list)
1912 if (s->visible->type == coff_vis_common)
1914 struct coff_where *w = s->where;
1915 /* s->visible->type = coff_vis_ext_def; leave it as common */
1916 common_section->size = align (common_section->size);
1917 w->offset = common_section->size + common_section->address;
1918 w->section = common_section;
1919 common_section->size += s->type->size;
1920 common_section->size = align (common_section->size);
1928 show_usage (file, status)
1932 fprintf (file, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name);
1939 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1941 show_usage (stdout, 0);
1952 static struct option long_options[] =
1954 {"debug", no_argument, 0, 'd'},
1955 {"quick", no_argument, 0, 'q'},
1956 {"noprescan", no_argument, 0, 'n'},
1957 {"help", no_argument, 0, 'h'},
1958 {"version", no_argument, 0, 'V'},
1959 {NULL, no_argument, 0, 0}
1965 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1966 setlocale (LC_MESSAGES, "");
1968 bindtextdomain (PACKAGE, LOCALEDIR);
1969 textdomain (PACKAGE);
1971 program_name = av[0];
1972 xmalloc_set_program_name (program_name);
1974 while ((opt = getopt_long (ac, av, "dhVqn", long_options,
1993 printf (_("GNU %s version %s\n"), program_name, PROGRAM_VERSION);
1999 show_usage (stderr, 1);
2004 /* The input and output files may be named on the command line. */
2008 input_file = av[optind];
2012 output_file = av[optind];
2015 show_usage (stderr, 1);
2016 if (strcmp (input_file, output_file) == 0)
2018 fatal (_("input and output files must be different"));
2027 fatal (_("no input file specified"));
2032 /* Take a .o off the input file and stick on a .obj. If
2033 it doesn't end in .o, then stick a .obj on anyway */
2035 int len = strlen (input_file);
2036 output_file = xmalloc (len + 5);
2037 strcpy (output_file, input_file);
2039 && output_file[len - 2] == '.'
2040 && output_file[len - 1] == 'o')
2042 output_file[len] = 'b';
2043 output_file[len + 1] = 'j';
2044 output_file[len + 2] = 0;
2048 strcat (output_file, ".obj");
2052 abfd = bfd_openr (input_file, 0);
2055 bfd_fatal (input_file);
2057 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2059 bfd_nonfatal (input_file);
2060 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2062 list_matching_formats (matching);
2068 file = fopen (output_file, FOPEN_WB);
2072 fatal (_("unable to open output file %s"), output_file);
2076 printf ("ids %d %d\n", base1, base2);
2077 tree = coff_grok (abfd);