1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994, 95, 96, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 This program can be used to convert a coff object file
24 into a Hitachi OM/LM (Sysroff) format.
26 All debugging information is preserved */
32 #include <libiberty.h>
35 #include "coff/internal.h"
36 #include "../bfd/libcoff.h"
38 #define PROGRAM_VERSION "1.5"
42 static char *toolname;
46 static void walk_tree_scope ();
47 static void wr_globals ();
48 static int find_base ();
54 static int noprescan = 0;
55 static struct coff_ofile *tree;
57 static int absolute_p;
60 static int segmented_p;
63 static int ids1[20000];
64 static int ids2[20000];
66 static int base1 = 0x18;
67 static int base2 = 0x2018;
74 char *r = xmalloc (a * b);
103 section_translate (n)
106 if (strcmp (n, ".text") == 0)
108 if (strcmp (n, ".data") == 0)
110 if (strcmp (n, ".bss") == 0)
117 #define DATE "940201073000"; /* Just a time on my birthday */
127 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
129 res = (char *) xmalloc (i + 1);
130 memcpy (res, name, i);
136 /* IT LEN stuff CS */
138 checksum (file, ptr, size, code)
147 int bytes = size / 8;
148 last = !(code & 0xff00);
151 ptr[0] = code | (last ? 0x80 : 0);
154 for (j = 0; j < bytes; j++)
158 /* Glue on a checksum too */
160 fwrite (ptr, bytes + 1, 1, file);
167 writeINT (n, ptr, idx, size, file)
183 /* Lets write out that record and do another one */
184 checksum (file, ptr, *idx, code | 0x1000);
196 ptr[byte + 0] = n >> 8;
200 ptr[byte + 0] = n >> 24;
201 ptr[byte + 1] = n >> 16;
202 ptr[byte + 2] = n >> 8;
203 ptr[byte + 3] = n >> 0;
213 writeBITS (val, ptr, idx, size)
225 /* Turn off all about to change bits */
226 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
227 /* Turn on the bits we want */
228 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
233 writeBARRAY (data, ptr, idx, size, file)
241 writeINT (data.len, ptr, idx, 1, file);
242 for (i = 0; i < data.len; i++)
244 writeINT (data.data[i], ptr, idx, 1, file);
250 writeCHARS (string, ptr, idx, size, file)
261 /* Lets write out that record and do another one */
262 checksum (file, ptr, *idx, code | 0x1000);
269 /* Variable length string */
270 size = strlen (string);
274 /* BUG WAITING TO HAPPEN */
275 memcpy (ptr + i, string, size);
280 #define SYSROFF_SWAP_OUT
284 static char *rname_sh[] =
286 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
289 static char *rname_h8300[] =
291 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
297 /* The TR block is not normal - it doesn't have any contents. */
304 fwrite (b, 1, sizeof (b), file);
308 wr_un (ptr, sfile, first, nsecs)
309 struct coff_ofile *ptr;
310 struct coff_sfile *sfile;
316 struct coff_symbol *s;
320 if (bfd_get_file_flags (abfd) & EXEC_P)
321 un.format = FORMAT_LM;
323 un.format = FORMAT_OM;
328 un.nsections = ptr->nsections - 1; /* Don't count the abs section */
330 /*NEW - only count sections with size */
331 un.nsections = nsecs;
336 /* Count all the undefined and defined variables with global scope */
340 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
342 if (s->visible->type == coff_vis_ext_def
343 || s->visible->type == coff_vis_common)
346 if (s->visible->type == coff_vis_ext_ref)
352 un.linker = "L_GX00";
354 un.name = sfile->name;
355 sysroff_swap_un_out (file, &un);
361 struct coff_ofile *p;
366 if (bfd_get_file_flags (abfd) & EXEC_P)
368 hd.mt = MTYPE_ABS_LM;
372 hd.mt = MTYPE_OMS_OR_LMS;
376 hd.nu = p->nsources; /* Always one unit */
377 hd.code = 0; /* Always ASCII */
378 hd.ver = "0200"; /* Version 2.00 */
379 switch (bfd_get_arch (abfd))
387 switch (bfd_get_mach (abfd))
393 toolname = "C_H8/300";
395 case bfd_mach_h8300h:
399 toolname = "C_H8/300H";
401 case bfd_mach_h8300s:
405 toolname = "C_H8/300S";
410 rnames = rname_h8300;
428 if (! bfd_get_file_flags(abfd) & EXEC_P)
438 hd.address = bfd_get_start_address (abfd);
443 hd.mn = strip_suffix (bfd_get_filename (abfd));
445 sysroff_swap_hd_out (file, &hd);
451 struct coff_ofile *p;
452 struct coff_section *sec;
456 sh.section = sec->number;
460 sysroff_swap_sh_out (file, &sh);
466 struct coff_ofile *p;
467 struct coff_section *section;
471 unsigned char stuff[200];
474 while (i < section->bfd_section->_raw_size)
477 int todo = 200; /* Copy in 200 byte lumps */
479 if (i + todo > section->bfd_section->_raw_size)
480 todo = section->bfd_section->_raw_size - i;
485 if (bfd_get_file_flags (abfd) & EXEC_P)
486 ob.address = section->address;
497 ob.cpf = 0; /* Never compress */
499 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
500 ob.data.data = stuff;
501 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
504 /* Now fill the rest with blanks */
505 while (i < (bfd_size_type) section->size)
508 int todo = 200; /* Copy in 200 byte lumps */
510 if (i + todo > (bfd_size_type) section->size)
511 todo = section->size - i;
514 ob.cpf = 0; /* Never compress */
516 memset (stuff, 0, todo);
517 ob.data.data = stuff;
518 sysroff_swap_ob_out (file, &ob);
521 /* Now fill the rest with blanks */
527 struct coff_ofile *ptr;
528 struct coff_section *sec;
530 int nr = sec->nrelocs;
532 for (i = 0; i < nr; i++)
534 struct coff_reloc *r = sec->relocs + i;
535 struct coff_symbol *ref;
544 rl.flen = 32; /* SH Specific */
545 /* What sort of reloc ? Look in the section to find out */
547 if (ref->visible->type == coff_vis_ext_ref)
549 rl.bcount = 4; /* Always 4 for us */
551 rl.symn = ref->er_number;
553 else if (ref->visible->type == coff_vis_common)
555 rl.bcount = 11; /* Always 11 for us */
557 rl.secn = ref->where->section->number;
560 rl.addend = ref->where->offset - ref->where->section->address;
561 rl.aopcode_is_0x20 = 0x20;
566 rl.bcount = 11; /* Always 11 for us */
568 rl.secn = ref->where->section->number;
571 rl.addend = -ref->where->section->address;
572 rl.aopcode_is_0x20 = 0x20;
575 if (rl.op == OP_SEC_REF
576 || rl.op == OP_EXT_REF)
578 sysroff_swap_rl_out (file, &rl);
585 struct coff_ofile *p;
588 for (i = 1; i < p->nsections; i++)
590 wr_sh (p, p->sections + i);
591 wr_ob (p, p->sections + i);
592 wr_rl (p, p->sections + i);
597 wr_dps_start (sfile, section, scope, type, nest)
598 struct coff_sfile *sfile;
599 struct coff_section *section;
600 struct coff_scope *scope;
610 dps.san = scope->sec->number;
611 dps.address = scope->offset - find_base (sfile, scope->sec);
612 dps.block_size = scope->size;
615 printf ("DPS %s %d %x\n",
631 sysroff_swap_dps_out (file, &dps);
635 wr_dps_end (section, scope, type)
636 struct coff_section *section;
637 struct coff_scope *scope;
643 sysroff_swap_dps_out (file, &dps);
650 return (int *) (xcalloc (sizeof (int), x));
653 static void walk_tree_symbol ();
655 walk_tree_type_1 (sfile, symbol, type, nest)
656 struct coff_sfile *sfile;
657 struct coff_symbol *symbol;
658 struct coff_type *type;
663 case coff_secdef_type:
664 case coff_basic_type:
668 switch (type->u.basic)
672 dbt.btype = BTYPE_VOID;
673 dbt.sign = BTYPE_UNSPEC;
674 dbt.fptype = FPTYPE_NOTSPEC;
677 dbt.btype = BTYPE_CHAR;
678 dbt.sign = BTYPE_UNSPEC;
679 dbt.fptype = FPTYPE_NOTSPEC;
684 dbt.btype = BTYPE_INT;
685 dbt.sign = SIGN_SIGNED;
686 dbt.fptype = FPTYPE_NOTSPEC;
689 dbt.btype = BTYPE_FLOAT;
690 dbt.fptype = FPTYPE_SINGLE;
693 dbt.btype = BTYPE_FLOAT;
694 dbt.fptype = FPTYPE_DOUBLE;
697 dbt.btype = BTYPE_FLOAT;
698 dbt.fptype = FPTYPE_EXTENDED;
701 dbt.btype = BTYPE_CHAR;
702 dbt.sign = SIGN_UNSIGNED;
703 dbt.fptype = FPTYPE_NOTSPEC;
708 dbt.btype = BTYPE_INT;
709 dbt.sign = SIGN_UNSIGNED;
710 dbt.fptype = FPTYPE_NOTSPEC;
713 dbt.bitsize = type->size;
715 sysroff_swap_dbt_out (file, &dbt);
718 case coff_pointer_type:
721 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
723 sysroff_swap_dpt_out (file, &dpt);
727 case coff_function_type:
730 struct coff_symbol *param;
733 dfp.nparams = type->u.function.parameters->nvars;
736 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
738 sysroff_swap_dfp_out (file, &dfp);
740 for (param = type->u.function.parameters->vars_head;
744 walk_tree_symbol (sfile, 0, param, nest);
747 sysroff_swap_dfp_out (file, &dfp);
751 case coff_structdef_type:
755 struct coff_symbol *member;
757 dbt.btype = BTYPE_STRUCT;
758 dbt.bitsize = type->size;
759 dbt.sign = SIGN_UNSPEC;
760 dbt.fptype = FPTYPE_NOTSPEC;
761 dbt.sid = get_member_id (type->u.astructdef.idx);
763 sysroff_swap_dbt_out (file, &dbt);
766 sysroff_swap_dds_out (file, &dds);
767 for (member = type->u.astructdef.elements->vars_head;
769 member = member->next)
771 walk_tree_symbol (sfile, 0, member, nest + 1);
775 sysroff_swap_dds_out (file, &dds);
779 case coff_structref_type:
782 dbt.btype = BTYPE_TAG;
783 dbt.bitsize = type->size;
784 dbt.sign = SIGN_UNSPEC;
785 dbt.fptype = FPTYPE_NOTSPEC;
786 if (type->u.astructref.ref)
788 dbt.sid = get_member_id (type->u.astructref.ref->number);
796 sysroff_swap_dbt_out (file, &dbt);
799 case coff_array_type:
803 int dims = 1; /* Only output one dimension at a time */
805 dar.variable = nints (dims);
806 dar.subtype = nints (dims);
807 dar.spare = nints (dims);
808 dar.max_variable = nints (dims);
809 dar.maxspare = nints (dims);
810 dar.max = nints (dims);
811 dar.min_variable = nints (dims);
812 dar.min = nints (dims);
813 dar.minspare = nints (dims);
815 dar.length = type->size / type->u.array.dim;
816 for (j = 0; j < dims; j++)
818 dar.variable[j] = VARIABLE_FIXED;
819 dar.subtype[j] = SUB_INTEGER;
821 dar.max_variable[j] = 0;
822 dar.max[j] = type->u.array.dim;
823 dar.min_variable[j] = 0;
824 dar.min[j] = 1; /* Why isn't this 0 ? */
826 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
827 sysroff_swap_dar_out (file, &dar);
830 case coff_enumdef_type:
834 struct coff_symbol *member;
835 dbt.btype = BTYPE_ENUM;
836 dbt.bitsize = type->size;
837 dbt.sign = SIGN_UNSPEC;
838 dbt.fptype = FPTYPE_NOTSPEC;
839 dbt.sid = get_member_id (type->u.aenumdef.idx);
841 sysroff_swap_dbt_out (file, &dbt);
846 sysroff_swap_den_out (file, &den);
847 for (member = type->u.aenumdef.elements->vars_head;
849 member = member->next)
851 walk_tree_symbol (sfile, 0, member, nest + 1);
855 sysroff_swap_den_out (file, &den);
860 case coff_enumref_type:
863 dbt.btype = BTYPE_TAG;
864 dbt.bitsize = type->size;
865 dbt.sign = SIGN_UNSPEC;
866 dbt.fptype = FPTYPE_NOTSPEC;
867 dbt.sid = get_member_id (type->u.aenumref.ref->number);
869 sysroff_swap_dbt_out (file, &dbt);
885 sysroff_swap_dty_out (file, &dty);
895 sysroff_swap_dty_out (file, &dty);
900 dump_tree_structure (sfile, symbol, type, nest)
901 struct coff_sfile *sfile;
902 struct coff_symbol *symbol;
903 struct coff_type *type;
906 if (symbol->type->type == coff_function_type)
916 walk_tree_type (sfile, symbol, type, nest)
920 struct coff_symbol *symbol;
921 struct coff_type *type;
924 if (symbol->type->type == coff_function_type)
931 sysroff_swap_dty_out (file, &dty);
932 walk_tree_type_1 (sfile, symbol, type, nest);
934 sysroff_swap_dty_out (file, &dty);
937 symbol->where->section,
938 symbol->type->u.function.code,
939 BLOCK_TYPE_FUNCTION, nest);
940 wr_dps_start (sfile, symbol->where->section,
941 symbol->type->u.function.code,
942 BLOCK_TYPE_BLOCK, nest);
943 walk_tree_scope (symbol->where->section,
945 symbol->type->u.function.code,
946 nest + 1, BLOCK_TYPE_BLOCK);
948 wr_dps_end (symbol->where->section,
949 symbol->type->u.function.code,
951 wr_dps_end (symbol->where->section,
952 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
960 sysroff_swap_dty_out (file, &dty);
961 walk_tree_type_1 (sfile, symbol, type, nest);
963 sysroff_swap_dty_out (file, &dty);
971 walk_tree_symbol (sfile, section, symbol, nest)
972 struct coff_sfile *sfile;
973 struct coff_section *section;
974 struct coff_symbol *symbol;
979 memset(&dsy, 0, sizeof(dsy));
982 switch (symbol->type->type)
984 case coff_function_type:
985 dsy.type = STYPE_FUNC;
988 case coff_structref_type:
989 case coff_pointer_type:
990 case coff_array_type:
991 case coff_basic_type:
992 case coff_enumref_type:
993 dsy.type = STYPE_VAR;
996 case coff_enumdef_type:
997 dsy.type = STYPE_TAG;
1001 case coff_structdef_type:
1002 dsy.type = STYPE_TAG;
1004 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
1006 case coff_secdef_type:
1012 if (symbol->where->where == coff_where_member_of_struct)
1015 dsy.type = STYPE_MEMBER;
1017 if (symbol->where->where == coff_where_member_of_enum)
1019 dsy.type = STYPE_ENUM;
1022 dsy.evalue = symbol->where->offset;
1025 if (symbol->type->type == coff_structdef_type
1026 || symbol->where->where == coff_where_entag
1027 || symbol->where->where == coff_where_strtag)
1029 dsy.snumber = get_member_id (symbol->number);
1033 dsy.snumber = get_ordinary_id (symbol->number);
1037 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1039 switch (symbol->visible->type)
1041 case coff_vis_common:
1042 case coff_vis_ext_def:
1043 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1045 case coff_vis_ext_ref:
1046 dsy.ainfo = AINFO_STATIC_EXT_REF;
1048 case coff_vis_int_def:
1049 dsy.ainfo = AINFO_STATIC_INT;
1052 case coff_vis_autoparam:
1053 dsy.ainfo = AINFO_AUTO;
1055 case coff_vis_register:
1056 case coff_vis_regparam:
1057 dsy.ainfo = AINFO_REG;
1061 case coff_vis_member_of_struct:
1062 case coff_vis_member_of_enum:
1068 dsy.dlength = symbol->type->size;
1069 switch (symbol->where->where)
1071 case coff_where_memory:
1073 dsy.section = symbol->where->section->number;
1078 case coff_where_member_of_struct:
1079 case coff_where_member_of_enum:
1080 case coff_where_stack:
1081 case coff_where_register:
1082 case coff_where_unknown:
1083 case coff_where_strtag:
1085 case coff_where_entag:
1086 case coff_where_typedef:
1092 switch (symbol->where->where)
1094 case coff_where_memory:
1095 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1097 case coff_where_stack:
1098 dsy.address = symbol->where->offset;
1100 case coff_where_member_of_struct:
1103 if (symbol->where->bitsize)
1105 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1107 dsy.field_len = symbol->where->bitsize;
1108 dsy.field_off = (bits / 32) * 4;
1109 dsy.field_bitoff = bits % 32;
1115 dsy.field_len = symbol->type->size;
1116 dsy.field_off = symbol->where->offset;
1119 case coff_where_member_of_enum:
1121 dsy.field_len = symbol->type->size;
1122 dsy.field_off = symbol->where->offset; */
1124 case coff_where_register:
1125 case coff_where_unknown:
1126 case coff_where_strtag:
1128 case coff_where_entag:
1129 case coff_where_typedef:
1135 if (symbol->where->where == coff_where_register)
1136 dsy.reg = rnames[symbol->where->offset];
1138 switch (symbol->visible->type)
1140 case coff_vis_common:
1141 /* We do this 'cause common C symbols are treated as extdefs */
1142 case coff_vis_ext_def:
1143 case coff_vis_ext_ref:
1145 dsy.ename = symbol->name;
1148 case coff_vis_regparam:
1149 case coff_vis_autoparam:
1150 dsy.type = STYPE_PARAMETER;
1153 case coff_vis_int_def:
1156 case coff_vis_register:
1158 case coff_vis_member_of_struct:
1159 case coff_vis_member_of_enum:
1171 sysroff_swap_dsy_out (file, &dsy);
1173 walk_tree_type (sfile, symbol, symbol->type, nest);
1178 walk_tree_scope (section, sfile, scope, nest, type)
1179 struct coff_section *section;
1180 struct coff_sfile *sfile;
1181 struct coff_scope *scope;
1185 struct coff_symbol *vars;
1186 struct coff_scope *child;
1188 if (scope->vars_head
1189 || (scope->list_head && scope->list_head->vars_head))
1191 wr_dps_start (sfile, section, scope, type, nest);
1194 wr_globals (tree, sfile, nest + 1);
1196 for (vars = scope->vars_head; vars; vars = vars->next)
1198 walk_tree_symbol (sfile, section, vars, nest);
1201 for (child = scope->list_head; child; child = child->next)
1203 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1206 wr_dps_end (section, scope, type);
1210 walk_tree_sfile (section, sfile)
1211 struct coff_section *section;
1212 struct coff_sfile *sfile;
1214 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1219 wr_program_structure (p, sfile)
1220 struct coff_ofile *p;
1221 struct coff_sfile *sfile;
1224 walk_tree_sfile (p->sections + 4, sfile);
1230 struct coff_ofile *p;
1231 struct coff_sfile *sfile;
1237 struct coff_symbol *symbol;
1238 static int incit = 0x500000;
1243 unsigned int *lowest = (unsigned *) nints (p->nsections);
1244 unsigned int *highest = (unsigned *) nints (p->nsections);
1245 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1250 du.sections = p->nsections - 1;
1251 du.san = (int *) xcalloc (sizeof (int), du.sections);
1252 du.address = nints (du.sections);
1253 du.length = nints (du.sections);
1255 for (i = 0; i < du.sections; i++)
1261 /* Look through all the symbols and try and work out the extents in this
1264 for (symbol = sfile->scope->vars_head;
1266 symbol = symbol->next)
1268 if (symbol->type->type == coff_secdef_type)
1270 unsigned int low = symbol->where->offset;
1271 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1272 struct coff_section *section = symbol->where->section;
1274 int sn = section->number;
1275 if (low < lowest[sn])
1277 if (high > highest[sn])
1283 for (i = 0; i < du.sections; i++)
1285 if (highest[i] == 0)
1287 lowest[i] = highest[i] = incit;
1290 du.length[used] = highest[i] - lowest[i];
1291 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1294 printf (" section %6s 0x%08x..0x%08x\n",
1295 p->sections[i + 1].name,
1304 for (j = 0; j < lim; j++)
1309 if (sfile->section[src].init)
1312 = sfile->section[src].high - sfile->section[src].low + 1;
1314 = sfile->section[src].low;
1319 du.address[dst] = 0;
1323 if (sfile->section[src].parent)
1325 printf (" section %6s 0x%08x..0x%08x\n",
1326 sfile->section[src].parent->name,
1328 du.address[dst] + du.length[dst] - 1);
1331 du.sections = dst + 1;
1337 sysroff_swap_du_out (file, &du);
1342 struct coff_ofile *p;
1343 struct coff_sfile *sfile;
1349 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1350 dus.drb = nints (dus.ns);
1351 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1352 dus.spare = nints (dus.ns);
1354 /* Find the filenames */
1358 for (sfile = p->source_head;
1360 sfile = sfile->next)
1364 dus.fname[i] = sfile->name;
1369 dus.fname[0] = sfile->name;
1372 sysroff_swap_dus_out (file, &dus);
1376 /* Find the offset of the .text section for this sfile in the
1377 .text section for the output file */
1380 find_base (sfile, section)
1381 struct coff_sfile *sfile;
1382 struct coff_section *section;
1384 return sfile->section[section->number].low;
1387 wr_dln (p, sfile, n)
1388 struct coff_ofile *p;
1389 struct coff_sfile *sfile;
1396 /* Count up all the linenumbers */
1397 struct coff_symbol *sy;
1403 for (sy = p->symbol_list_head;
1405 sy = sy->next_in_ofile_list)
1407 struct coff_type *t = sy->type;
1408 if (t->type == coff_function_type)
1410 struct coff_line *l = t->u.function.lines;
1415 dln.sfn = nints (lc);
1416 dln.sln = nints (lc);
1417 dln.lln = nints (lc);
1418 dln.section = nints (lc);
1420 dln.from_address = nints (lc);
1421 dln.to_address = nints (lc);
1428 /* Run through once more and fill up the structure */
1430 for (sy = p->symbol_list_head;
1432 sy = sy->next_in_ofile_list)
1434 if (sy->type->type == coff_function_type)
1437 struct coff_line *l = sy->type->u.function.lines;
1438 for (i = 0; i < l->nlines; i++)
1440 dln.section[idx] = sy->where->section->number;
1442 dln.sln[idx] = l->lines[i];
1443 dln.from_address[idx] = l->addresses[i];
1445 dln.to_address[idx - 1] = dln.from_address[idx];
1451 sysroff_swap_dln_out (file, &dln);
1456 /* Count up all the linenumbers */
1458 struct coff_symbol *sy;
1464 for (sy = sfile->scope->vars_head;
1468 struct coff_type *t = sy->type;
1469 if (t->type == coff_function_type)
1471 struct coff_line *l = t->u.function.lines;
1477 dln.sfn = nints (lc);
1478 dln.sln = nints (lc);
1479 dln.cc = nints (lc);
1480 dln.section = nints (lc);
1482 dln.from_address = nints (lc);
1483 dln.to_address = nints (lc);
1490 /* Run through once more and fill up the structure */
1492 for (sy = sfile->scope->vars_head;
1496 if (sy->type->type == coff_function_type)
1499 struct coff_line *l = sy->type->u.function.lines;
1502 int base = find_base (sfile, sy->where->section);
1503 for (i = 0; i < l->nlines; i++)
1505 dln.section[idx] = sy->where->section->number;
1507 dln.sln[idx] = l->lines[i];
1508 dln.from_address[idx] =
1509 l->addresses[i] + sy->where->section->address - base;
1512 dln.to_address[idx - 1] = dln.from_address[idx];
1516 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1521 sysroff_swap_dln_out (file, &dln);
1525 /* Write the global symbols out to the debug info */
1527 wr_globals (p, sfile, n)
1528 struct coff_ofile *p;
1529 struct coff_sfile *sfile;
1532 struct coff_symbol *sy;
1533 for (sy = p->symbol_list_head;
1535 sy = sy->next_in_ofile_list)
1537 if (sy->visible->type == coff_vis_ext_def
1538 || sy->visible->type == coff_vis_ext_ref)
1540 /* Only write out symbols if they belong to
1541 the current source file */
1542 if (sy->sfile == sfile)
1543 walk_tree_symbol (sfile, 0, sy, 0);
1551 struct coff_ofile *p;
1553 struct coff_sfile *sfile;
1555 for (sfile = p->source_head;
1557 sfile = sfile->next)
1562 printf ("%s\n", sfile->name);
1564 wr_du (p, sfile, n);
1566 wr_program_structure (p, sfile);
1567 wr_dln (p, sfile, n);
1575 /* It seems that the CS struct is not normal - the size is wrong
1576 heres one I prepared earlier.. */
1580 0x00, /* number of chars in variable length part */
1612 fwrite (b, 1, sizeof (b), file);
1615 /* Write out the SC records for a unit. Create an SC
1616 for all the sections which appear in the output file, even
1617 if there isn't an equivalent one on the input */
1621 struct coff_ofile *ptr;
1622 struct coff_sfile *sfile;
1626 /* First work out the total number of sections */
1628 int total_sec = ptr->nsections;
1632 struct coff_section *sec;
1633 struct coff_symbol *symbol;
1635 struct coff_symbol *symbol;
1638 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1642 for (i = 0; i < total_sec; i++)
1644 info[i].sec = ptr->sections + i;
1648 for (symbol = sfile->scope->vars_head;
1650 symbol = symbol->next)
1653 if (symbol->type->type == coff_secdef_type)
1655 for (i = 0; i < total_sec; i++)
1657 if (symbol->where->section == info[i].sec)
1659 info[i].symbol = symbol;
1666 /* Now output all the section info, and fake up some stuff for sections
1669 for (i = 1; i < total_sec; i++)
1673 symbol = info[i].symbol;
1678 /* Don't have a symbol set aside for this section, which means that nothing
1679 in this file does anything for the section. */
1680 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1683 name = info[i].sec->name;
1687 if (bfd_get_file_flags (abfd) & EXEC_P)
1690 sc.addr = symbol->where->offset;
1697 sc.length = symbol->type->size;
1698 name = symbol->name;
1703 sc.concat = CONCAT_SIMPLE;
1711 sc.spare1 = 0; /* If not zero, then it doesn't work */
1712 sc.name = section_translate (name);
1713 if (strlen (sc.name) == 1)
1719 sc.contents = CONTENTS_DATA;
1722 sc.contents = CONTENTS_CODE;
1727 sc.contents = CONTENTS_CODE;
1733 sysroff_swap_sc_out (file, &sc);
1743 /* Write out the ER records for a unit. */
1745 wr_er (ptr, sfile, first)
1746 struct coff_ofile *ptr;
1747 struct coff_sfile *sfile;
1751 struct coff_symbol *sym;
1754 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1756 if (sym->visible->type == coff_vis_ext_ref)
1760 er.type = ER_NOTSPEC;
1761 er.name = sym->name;
1762 sysroff_swap_er_out (file, &er);
1763 sym->er_number = idx++;
1769 /* Write out the ED records for a unit. */
1771 wr_ed (ptr, sfile, first)
1772 struct coff_ofile *ptr;
1773 struct coff_sfile *sfile;
1776 struct coff_symbol *s;
1779 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1781 if (s->visible->type == coff_vis_ext_def
1782 || s->visible->type == coff_vis_common)
1786 ed.section = s->where->section->number;
1788 if (s->where->section->data)
1790 ed.type = ED_TYPE_DATA;
1792 else if (s->where->section->code & SEC_CODE)
1794 ed.type = ED_TYPE_ENTRY;
1798 ed.type = ED_TYPE_NOTSPEC;
1799 ed.type = ED_TYPE_DATA;
1801 ed.address = s->where->offset - s->where->section->address;
1803 sysroff_swap_ed_out (file, &ed);
1811 struct coff_ofile *ptr;
1813 struct coff_sfile *sfile;
1815 for (sfile = ptr->source_head;
1817 sfile = sfile->next)
1823 wr_un (ptr, sfile, first, 0);
1824 nsecs = wr_sc (ptr, sfile);
1826 fseek (file, p1, SEEK_SET);
1827 wr_un (ptr, sfile, first, nsecs);
1828 fseek (file, p2, SEEK_SET);
1829 wr_er (ptr, sfile, first);
1830 wr_ed (ptr, sfile, first);
1837 struct coff_ofile *p;
1851 return (x + 3) & ~3;
1854 /* Find all the common variables and turn them into
1855 ordinary defs - dunno why, but thats what hitachi does with 'em */
1859 struct coff_ofile *tree;
1861 struct coff_symbol *s;
1862 struct coff_section *common_section;
1863 /* Find the common section - always section 3 */
1864 common_section = tree->sections + 3;
1865 for (s = tree->symbol_list_head;
1867 s = s->next_in_ofile_list)
1869 if (s->visible->type == coff_vis_common)
1871 struct coff_where *w = s->where;
1872 /* s->visible->type = coff_vis_ext_def; leave it as common */
1873 common_section->size = align (common_section->size);
1874 w->offset = common_section->size + common_section->address;
1875 w->section = common_section;
1876 common_section->size += s->type->size;
1877 common_section->size = align (common_section->size);
1885 show_usage (file, status)
1889 fprintf (file, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name);
1896 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1898 show_usage (stdout, 0);
1909 static struct option long_options[] =
1911 {"debug", no_argument, 0, 'd'},
1912 {"quick", no_argument, 0, 'q'},
1913 {"noprescan", no_argument, 0, 'n'},
1914 {"help", no_argument, 0, 'h'},
1915 {"version", no_argument, 0, 'V'},
1916 {NULL, no_argument, 0, 0}
1922 setlocale (LC_MESSAGES, "");
1923 bindtextdomain (PACKAGE, LOCALEDIR);
1924 textdomain (PACKAGE);
1926 program_name = av[0];
1927 xmalloc_set_program_name (program_name);
1929 while ((opt = getopt_long (ac, av, "dhVqn", long_options,
1948 printf (_("GNU %s version %s\n"), program_name, PROGRAM_VERSION);
1954 show_usage (stderr, 1);
1959 /* The input and output files may be named on the command line. */
1963 input_file = av[optind];
1967 output_file = av[optind];
1970 show_usage (stderr, 1);
1971 if (strcmp (input_file, output_file) == 0)
1974 _("%s: input and output files must be different\n"),
1985 fprintf (stderr, _("%s: no input file specified\n"),
1992 /* Take a .o off the input file and stick on a .obj. If
1993 it doesn't end in .o, then stick a .obj on anyway */
1995 int len = strlen (input_file);
1996 output_file = xmalloc (len + 5);
1997 strcpy (output_file, input_file);
1999 && output_file[len - 2] == '.'
2000 && output_file[len - 1] == 'o')
2002 output_file[len] = 'b';
2003 output_file[len + 1] = 'j';
2004 output_file[len + 2] = 0;
2008 strcat (output_file, ".obj");
2012 abfd = bfd_openr (input_file, 0);
2015 bfd_fatal (input_file);
2017 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2019 bfd_nonfatal (input_file);
2020 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2022 list_matching_formats (matching);
2028 file = fopen (output_file, FOPEN_WB);
2032 fprintf (stderr, _("%s: unable to open output file %s\n"),
2033 program_name, output_file);
2038 printf ("ids %d %d\n", base1, base2);
2039 tree = coff_grok (abfd);