1 /* srconv.c -- Sysroff conversion program
2 Copyright (C) 1994 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 02111-1307, USA. */
20 /* Written by Steve Chamberlain (sac@cygnus.com)
22 This program can be used to convert a coff object file
23 into a Hitachi OM/LM (Sysroff) format.
25 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"
44 static void walk_tree_scope ();
45 static void wr_globals ();
46 static int find_base ();
52 static int noprescan = 0;
53 static struct coff_ofile *tree;
55 static int absolute_p;
58 static int segmented_p;
61 static int ids1[20000];
62 static int ids2[20000];
64 static int base1 = 0x18;
65 static int base2 = 0x2018;
72 char *r = xmalloc (a * b);
101 section_translate (n)
104 if (strcmp (n, ".text") == 0)
106 if (strcmp (n, ".data") == 0)
108 if (strcmp (n, ".bss") == 0)
115 #define DATE "940201073000"; /* Just a time on my birthday */
125 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
127 res = (char *) xmalloc (i + 1);
128 memcpy (res, name, i);
134 /* IT LEN stuff CS */
136 checksum (file, ptr, size, code)
145 int bytes = size / 8;
146 last = !(code & 0xff00);
149 ptr[0] = code | (last ? 0x80 : 0);
152 for (j = 0; j < bytes; j++)
156 /* Glue on a checksum too */
158 fwrite (ptr, bytes + 1, 1, file);
165 writeINT (n, ptr, idx, size, file)
181 /* Lets write out that record and do another one */
182 checksum (file, ptr, *idx, code | 0x1000);
194 ptr[byte + 0] = n >> 8;
198 ptr[byte + 0] = n >> 24;
199 ptr[byte + 1] = n >> 16;
200 ptr[byte + 2] = n >> 8;
201 ptr[byte + 3] = n >> 0;
211 writeBITS (val, ptr, idx, size)
223 /* Turn off all about to change bits */
224 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
225 /* Turn on the bits we want */
226 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
231 writeBARRAY (data, ptr, idx, size, file)
239 writeINT (data.len, ptr, idx, 1, file);
240 for (i = 0; i < data.len; i++)
242 writeINT (data.data[i], ptr, idx, 1, file);
248 writeCHARS (string, ptr, idx, size, file)
259 /* Lets write out that record and do another one */
260 checksum (file, ptr, *idx, code | 0x1000);
267 /* Variable length string */
268 size = strlen (string);
272 /* BUG WAITING TO HAPPEN */
273 memcpy (ptr + i, string, size);
278 #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. */
302 fwrite (b, 1, sizeof (b), file);
306 wr_un (ptr, sfile, first, nsecs)
307 struct coff_ofile *ptr;
308 struct coff_sfile *sfile;
314 struct coff_symbol *s;
318 if (abfd->flags & EXEC_P)
319 un.format = FORMAT_LM;
321 un.format = FORMAT_OM;
326 un.nsections = ptr->nsections - 1; /* Don't count the abs section */
328 /*NEW - only count sections with size */
329 un.nsections = nsecs;
334 /* Count all the undefined and defined variables with global scope */
338 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
340 if (s->visible->type == coff_vis_ext_def
341 || s->visible->type == coff_vis_common)
344 if (s->visible->type == coff_vis_ext_ref)
354 un.tool = "C_H8/300H";
357 un.linker = "L_GX00";
359 un.name = sfile->name;
360 sysroff_swap_un_out (file, &un);
366 struct coff_ofile *p;
371 if (abfd->flags & EXEC_P)
373 hd.mt = MTYPE_ABS_LM;
377 hd.mt = MTYPE_OMS_OR_LMS;
381 hd.nu = p->nsources; /* Always one unit */
382 hd.code = 0; /* Always ASCII */
383 hd.ver = "0200"; /* Version 2.00 */
384 switch (abfd->arch_info->arch)
410 if (!abfd->flags & EXEC_P)
420 hd.address = bfd_get_start_address (abfd);
425 hd.mn = strip_suffix (abfd->filename);
428 sysroff_swap_hd_out (file, &hd);
434 struct coff_ofile *p;
435 struct coff_section *sec;
439 sh.section = sec->number;
443 sysroff_swap_sh_out (file, &sh);
449 struct coff_ofile *p;
450 struct coff_section *section;
454 unsigned char stuff[200];
457 while (i < section->bfd_section->_raw_size)
460 int todo = 200; /* Copy in 200 byte lumps */
462 if (i + todo > section->bfd_section->_raw_size)
463 todo = section->bfd_section->_raw_size - i;
468 if (abfd->flags & 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 */ );
487 /* Now fill the rest with blanks */
488 while (i < section->size)
491 int todo = 200; /* Copy in 200 byte lumps */
493 if (i + todo > section->size)
494 todo = section->size - i;
497 ob.cpf = 0; /* Never compress */
499 memset (stuff, 0, todo);
500 ob.data.data = stuff;
501 sysroff_swap_ob_out (file, &ob);
504 /* Now fill the rest with blanks */
510 struct coff_ofile *ptr;
511 struct coff_section *sec;
513 int nr = sec->nrelocs;
515 for (i = 0; i < nr; i++)
517 struct coff_reloc *r = sec->relocs + i;
518 struct coff_symbol *ref;
527 rl.flen = 32; /* SH Specific */
528 /* What sort of reloc ? Look in the section to find out */
530 if (ref->visible->type == coff_vis_ext_ref)
532 rl.bcount = 4; /* Always 4 for us */
534 rl.symn = ref->er_number;
536 else if (ref->visible->type == coff_vis_common)
538 rl.bcount = 11; /* Always 11 for us */
540 rl.secn = ref->where->section->number;
543 rl.addend = ref->where->offset - ref->where->section->address;
544 rl.aopcode_is_0x20 = 0x20;
549 rl.bcount = 11; /* Always 11 for us */
551 rl.secn = ref->where->section->number;
554 rl.addend = -ref->where->section->address;
555 rl.aopcode_is_0x20 = 0x20;
558 if (rl.op == OP_SEC_REF
559 || rl.op == OP_EXT_REF)
561 sysroff_swap_rl_out (file, &rl);
568 struct coff_ofile *p;
571 for (i = 1; i < p->nsections; i++)
573 wr_sh (p, p->sections + i);
574 wr_ob (p, p->sections + i);
575 wr_rl (p, p->sections + i);
580 wr_dps_start (sfile, section, scope, type, nest)
581 struct coff_sfile *sfile;
582 struct coff_section *section;
583 struct coff_scope *scope;
593 dps.san = scope->sec->number;
594 dps.address = scope->offset - find_base (sfile, scope->sec);
595 dps.block_size = scope->size;
598 printf ("DPS %s %d %x\n",
614 sysroff_swap_dps_out (file, &dps);
618 wr_dps_end (section, scope, type)
619 struct coff_section *section;
620 struct coff_scope *scope;
626 sysroff_swap_dps_out (file, &dps);
633 return (int *) (xcalloc (sizeof (int), x));
636 static void walk_tree_symbol ();
638 walk_tree_type_1 (sfile, symbol, type, nest)
639 struct coff_sfile *sfile;
640 struct coff_symbol *symbol;
641 struct coff_type *type;
646 case coff_secdef_type:
647 case coff_basic_type:
651 switch (type->u.basic)
655 dbt.btype = BTYPE_VOID;
656 dbt.sign = BTYPE_UNSPEC;
657 dbt.fptype = FPTYPE_NOTSPEC;
660 dbt.btype = BTYPE_CHAR;
661 dbt.sign = BTYPE_UNSPEC;
662 dbt.fptype = FPTYPE_NOTSPEC;
667 dbt.btype = BTYPE_INT;
668 dbt.sign = SIGN_SIGNED;
669 dbt.fptype = FPTYPE_NOTSPEC;
672 dbt.btype = BTYPE_FLOAT;
673 dbt.fptype = FPTYPE_SINGLE;
676 dbt.btype = BTYPE_FLOAT;
677 dbt.fptype = FPTYPE_DOUBLE;
680 dbt.btype = BTYPE_FLOAT;
681 dbt.fptype = FPTYPE_EXTENDED;
684 dbt.btype = BTYPE_CHAR;
685 dbt.sign = SIGN_UNSIGNED;
686 dbt.fptype = FPTYPE_NOTSPEC;
691 dbt.btype = BTYPE_INT;
692 dbt.sign = SIGN_UNSIGNED;
693 dbt.fptype = FPTYPE_NOTSPEC;
696 dbt.bitsize = type->size;
698 sysroff_swap_dbt_out (file, &dbt);
701 case coff_pointer_type:
704 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
706 sysroff_swap_dpt_out (file, &dpt);
710 case coff_function_type:
713 struct coff_symbol *param;
716 dfp.nparams = type->u.function.parameters->nvars;
719 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
721 sysroff_swap_dfp_out (file, &dfp);
723 for (param = type->u.function.parameters->vars_head;
727 walk_tree_symbol (sfile, 0, param, nest);
730 sysroff_swap_dfp_out (file, &dfp);
734 case coff_structdef_type:
738 struct coff_symbol *member;
740 dbt.btype = BTYPE_STRUCT;
741 dbt.bitsize = type->size;
742 dbt.sign = SIGN_UNSPEC;
743 dbt.fptype = FPTYPE_NOTSPEC;
744 dbt.sid = get_member_id (type->u.astructdef.idx);
746 sysroff_swap_dbt_out (file, &dbt);
749 sysroff_swap_dds_out (file, &dds);
750 for (member = type->u.astructdef.elements->vars_head;
752 member = member->next)
754 walk_tree_symbol (sfile, 0, member, nest + 1);
758 sysroff_swap_dds_out (file, &dds);
762 case coff_structref_type:
765 dbt.btype = BTYPE_TAG;
766 dbt.bitsize = type->size;
767 dbt.sign = SIGN_UNSPEC;
768 dbt.fptype = FPTYPE_NOTSPEC;
769 if (type->u.astructref.ref)
771 dbt.sid = get_member_id (type->u.astructref.ref->number);
779 sysroff_swap_dbt_out (file, &dbt);
782 case coff_array_type:
786 int dims = 1; /* Only output one dimension at a time */
788 dar.variable = nints (dims);
789 dar.subtype = nints (dims);
790 dar.spare = nints (dims);
791 dar.max_variable = nints (dims);
792 dar.maxspare = nints (dims);
793 dar.max = nints (dims);
794 dar.min_variable = nints (dims);
795 dar.min = nints (dims);
796 dar.minspare = nints (dims);
798 dar.length = type->size / type->u.array.dim;
799 for (j = 0; j < dims; j++)
801 dar.variable[j] = VARIABLE_FIXED;
802 dar.subtype[j] = SUB_INTEGER;
804 dar.max_variable[j] = 0;
805 dar.max[j] = type->u.array.dim;
806 dar.min_variable[j] = 0;
807 dar.min[j] = 1; /* Why isn't this 0 ? */
809 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
810 sysroff_swap_dar_out (file, &dar);
813 case coff_enumdef_type:
817 struct coff_symbol *member;
818 dbt.btype = BTYPE_ENUM;
819 dbt.bitsize = type->size;
820 dbt.sign = SIGN_UNSPEC;
821 dbt.fptype = FPTYPE_NOTSPEC;
822 dbt.sid = get_member_id (type->u.aenumdef.idx);
824 sysroff_swap_dbt_out (file, &dbt);
829 sysroff_swap_den_out (file, &den);
830 for (member = type->u.aenumdef.elements->vars_head;
832 member = member->next)
834 walk_tree_symbol (sfile, 0, member, nest + 1);
838 sysroff_swap_den_out (file, &den);
843 case coff_enumref_type:
846 dbt.btype = BTYPE_TAG;
847 dbt.bitsize = type->size;
848 dbt.sign = SIGN_UNSPEC;
849 dbt.fptype = FPTYPE_NOTSPEC;
850 dbt.sid = get_member_id (type->u.aenumref.ref->number);
852 sysroff_swap_dbt_out (file, &dbt);
868 sysroff_swap_dty_out (file, &dty);
878 sysroff_swap_dty_out (file, &dty);
883 dump_tree_structure (sfile, symbol, type, nest)
884 struct coff_sfile *sfile;
885 struct coff_symbol *symbol;
886 struct coff_type *type;
889 if (symbol->type->type == coff_function_type)
899 walk_tree_type (sfile, symbol, type, nest)
903 struct coff_symbol *symbol;
904 struct coff_type *type;
907 if (symbol->type->type == coff_function_type)
914 sysroff_swap_dty_out (file, &dty);
915 walk_tree_type_1 (sfile, symbol, type, nest);
917 sysroff_swap_dty_out (file, &dty);
920 symbol->where->section,
921 symbol->type->u.function.code,
922 BLOCK_TYPE_FUNCTION, nest);
923 wr_dps_start (sfile, symbol->where->section,
924 symbol->type->u.function.code,
925 BLOCK_TYPE_BLOCK, nest);
926 walk_tree_scope (symbol->where->section,
928 symbol->type->u.function.code,
929 nest + 1, BLOCK_TYPE_BLOCK);
931 wr_dps_end (symbol->where->section,
932 symbol->type->u.function.code,
934 wr_dps_end (symbol->where->section,
935 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
943 sysroff_swap_dty_out (file, &dty);
944 walk_tree_type_1 (sfile, symbol, type, nest);
946 sysroff_swap_dty_out (file, &dty);
954 walk_tree_symbol (sfile, section, symbol, nest)
955 struct coff_sfile *sfile;
956 struct coff_section *section;
957 struct coff_symbol *symbol;
965 switch (symbol->type->type)
967 case coff_function_type:
968 dsy.type = STYPE_FUNC;
971 case coff_structref_type:
972 case coff_pointer_type:
973 case coff_array_type:
974 case coff_basic_type:
975 case coff_enumref_type:
976 dsy.type = STYPE_VAR;
979 case coff_enumdef_type:
980 dsy.type = STYPE_TAG;
984 case coff_structdef_type:
985 dsy.type = STYPE_TAG;
987 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
989 case coff_secdef_type:
995 if (symbol->where->where == coff_where_member_of_struct)
998 dsy.type = STYPE_MEMBER;
1000 if (symbol->where->where == coff_where_member_of_enum)
1002 dsy.type = STYPE_ENUM;
1005 dsy.evalue = symbol->where->offset;
1008 if (symbol->type->type == coff_structdef_type
1009 || symbol->where->where == coff_where_entag
1010 || symbol->where->where == coff_where_strtag)
1012 dsy.snumber = get_member_id (symbol->number);
1016 dsy.snumber = get_ordinary_id (symbol->number);
1020 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1022 switch (symbol->visible->type)
1024 case coff_vis_common:
1025 case coff_vis_ext_def:
1026 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1028 case coff_vis_ext_ref:
1029 dsy.ainfo = AINFO_STATIC_EXT_REF;
1031 case coff_vis_int_def:
1032 dsy.ainfo = AINFO_STATIC_INT;
1035 case coff_vis_autoparam:
1036 dsy.ainfo = AINFO_AUTO;
1038 case coff_vis_register:
1039 case coff_vis_regparam:
1040 dsy.ainfo = AINFO_REG;
1044 case coff_vis_member_of_struct:
1045 case coff_vis_member_of_enum:
1051 dsy.dlength = symbol->type->size;
1052 switch (symbol->where->where)
1054 case coff_where_memory:
1056 dsy.section = symbol->where->section->number;
1061 case coff_where_member_of_struct:
1062 case coff_where_member_of_enum:
1063 case coff_where_stack:
1064 case coff_where_register:
1065 case coff_where_unknown:
1066 case coff_where_strtag:
1068 case coff_where_entag:
1069 case coff_where_typedef:
1075 switch (symbol->where->where)
1077 case coff_where_memory:
1078 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1080 case coff_where_stack:
1081 dsy.address = symbol->where->offset;
1083 case coff_where_member_of_struct:
1086 if (symbol->where->bitsize)
1088 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1090 dsy.field_len = symbol->where->bitsize;
1091 dsy.field_off = (bits / 32) * 4;
1092 dsy.field_bitoff = bits % 32;
1098 dsy.field_len = symbol->type->size;
1099 dsy.field_off = symbol->where->offset;
1102 case coff_where_member_of_enum:
1104 dsy.field_len = symbol->type->size;
1105 dsy.field_off = symbol->where->offset; */
1107 case coff_where_register:
1108 case coff_where_unknown:
1109 case coff_where_strtag:
1111 case coff_where_entag:
1112 case coff_where_typedef:
1118 if (symbol->where->where == coff_where_register)
1121 dsy.reg = rname_sh[symbol->where->offset];
1123 dsy.reg = rname_h8300[symbol->where->offset];
1126 switch (symbol->visible->type)
1128 case coff_vis_common:
1129 /* We do this 'cause common C symbols are treated as extdefs */
1130 case coff_vis_ext_def:
1131 case coff_vis_ext_ref:
1133 dsy.ename = symbol->name;
1136 case coff_vis_regparam:
1137 case coff_vis_autoparam:
1138 dsy.type = STYPE_PARAMETER;
1141 case coff_vis_int_def:
1144 case coff_vis_register:
1146 case coff_vis_member_of_struct:
1147 case coff_vis_member_of_enum:
1159 sysroff_swap_dsy_out (file, &dsy);
1161 walk_tree_type (sfile, symbol, symbol->type, nest);
1166 walk_tree_scope (section, sfile, scope, nest, type)
1167 struct coff_section *section;
1168 struct coff_sfile *sfile;
1169 struct coff_scope *scope;
1173 struct coff_symbol *vars;
1174 struct coff_scope *child;
1176 if (scope->vars_head
1177 || (scope->list_head && scope->list_head->vars_head))
1179 wr_dps_start (sfile, section, scope, type, nest);
1182 wr_globals (tree, sfile, nest + 1);
1184 for (vars = scope->vars_head; vars; vars = vars->next)
1186 walk_tree_symbol (sfile, section, vars, nest);
1189 for (child = scope->list_head; child; child = child->next)
1191 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1194 wr_dps_end (section, scope, type);
1198 walk_tree_sfile (section, sfile)
1199 struct coff_section *section;
1200 struct coff_sfile *sfile;
1202 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1207 wr_program_structure (p, sfile)
1208 struct coff_ofile *p;
1209 struct coff_sfile *sfile;
1212 walk_tree_sfile (p->sections + 4, sfile);
1218 struct coff_ofile *p;
1219 struct coff_sfile *sfile;
1225 struct coff_symbol *symbol;
1226 static int incit = 0x500000;
1231 unsigned int *lowest = (unsigned *) nints (p->nsections);
1232 unsigned int *highest = (unsigned *) nints (p->nsections);
1234 du.format = abfd->flags & EXEC_P ? 0 : 1;
1237 du.sections = p->nsections - 1;
1238 du.san = (int *) xcalloc (sizeof (int), du.sections);
1239 du.address = nints (du.sections);
1240 du.length = nints (du.sections);
1242 for (i = 0; i < du.sections; i++)
1248 /* Look through all the symbols and try and work out the extents in this
1251 for (symbol = sfile->scope->vars_head;
1253 symbol = symbol->next)
1255 if (symbol->type->type == coff_secdef_type)
1257 unsigned int low = symbol->where->offset;
1258 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1259 struct coff_section *section = symbol->where->section;
1261 int sn = section->number;
1262 if (low < lowest[sn])
1264 if (high > highest[sn])
1270 for (i = 0; i < du.sections; i++)
1272 if (highest[i] == 0)
1274 lowest[i] = highest[i] = incit;
1277 du.length[used] = highest[i] - lowest[i];
1278 du.address[used] = abfd->flags & EXEC_P ? lowest[i] : 0;
1281 printf (" section %6s 0x%08x..0x%08x\n",
1282 p->sections[i + 1].name,
1291 for (j = 0; j < lim; j++)
1296 if (sfile->section[src].init)
1299 = sfile->section[src].high - sfile->section[src].low + 1;
1301 = sfile->section[src].low;
1306 du.address[dst] = 0;
1310 if (sfile->section[src].parent)
1312 printf (" section %6s 0x%08x..0x%08x\n",
1313 sfile->section[src].parent->name,
1315 du.address[dst] + du.length[dst] - 1);
1318 du.sections = dst + 1;
1324 sysroff_swap_du_out (file, &du);
1329 struct coff_ofile *p;
1330 struct coff_sfile *sfile;
1336 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1337 dus.drb = nints (dus.ns);
1338 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1339 dus.spare = nints (dus.ns);
1341 /* Find the filenames */
1345 for (sfile = p->source_head;
1347 sfile = sfile->next)
1351 dus.fname[i] = sfile->name;
1356 dus.fname[0] = sfile->name;
1359 sysroff_swap_dus_out (file, &dus);
1363 /* Find the offset of the .text section for this sfile in the
1364 .text section for the output file */
1367 find_base (sfile, section)
1368 struct coff_sfile *sfile;
1369 struct coff_section *section;
1371 return sfile->section[section->number].low;
1374 wr_dln (p, sfile, n)
1375 struct coff_ofile *p;
1376 struct coff_sfile *sfile;
1383 /* Count up all the linenumbers */
1384 struct coff_symbol *sy;
1390 for (sy = p->symbol_list_head;
1392 sy = sy->next_in_ofile_list)
1394 struct coff_type *t = sy->type;
1395 if (t->type == coff_function_type)
1397 struct coff_line *l = t->u.function.lines;
1402 dln.sfn = nints (lc);
1403 dln.sln = nints (lc);
1404 dln.lln = nints (lc);
1405 dln.section = nints (lc);
1407 dln.from_address = nints (lc);
1408 dln.to_address = nints (lc);
1415 /* Run through once more and fill up the structure */
1417 for (sy = p->symbol_list_head;
1419 sy = sy->next_in_ofile_list)
1421 if (sy->type->type == coff_function_type)
1424 struct coff_line *l = sy->type->u.function.lines;
1425 for (i = 0; i < l->nlines; i++)
1427 dln.section[idx] = sy->where->section->number;
1429 dln.sln[idx] = l->lines[i];
1430 dln.from_address[idx] = l->addresses[i];
1432 dln.to_address[idx - 1] = dln.from_address[idx];
1438 sysroff_swap_dln_out (file, &dln);
1443 /* Count up all the linenumbers */
1445 struct coff_symbol *sy;
1451 for (sy = sfile->scope->vars_head;
1455 struct coff_type *t = sy->type;
1456 if (t->type == coff_function_type)
1458 struct coff_line *l = t->u.function.lines;
1464 dln.sfn = nints (lc);
1465 dln.sln = nints (lc);
1466 dln.cc = nints (lc);
1467 dln.section = nints (lc);
1469 dln.from_address = nints (lc);
1470 dln.to_address = nints (lc);
1477 /* Run through once more and fill up the structure */
1479 for (sy = sfile->scope->vars_head;
1483 if (sy->type->type == coff_function_type)
1486 struct coff_line *l = sy->type->u.function.lines;
1489 int base = find_base (sfile, sy->where->section);
1490 for (i = 0; i < l->nlines; i++)
1492 dln.section[idx] = sy->where->section->number;
1494 dln.sln[idx] = l->lines[i];
1495 dln.from_address[idx] =
1496 l->addresses[i] + sy->where->section->address - base;
1499 dln.to_address[idx - 1] = dln.from_address[idx];
1503 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1508 sysroff_swap_dln_out (file, &dln);
1512 /* Write the global symbols out to the debug info */
1514 wr_globals (p, sfile, n)
1515 struct coff_ofile *p;
1516 struct coff_sfile *sfile;
1519 struct coff_symbol *sy;
1520 for (sy = p->symbol_list_head;
1522 sy = sy->next_in_ofile_list)
1524 if (sy->visible->type == coff_vis_ext_def
1525 || sy->visible->type == coff_vis_ext_ref)
1527 /* Only write out symbols if they belong to
1528 the current source file */
1529 if (sy->sfile == sfile)
1530 walk_tree_symbol (sfile, 0, sy, 0);
1538 struct coff_ofile *p;
1540 struct coff_sfile *sfile;
1542 for (sfile = p->source_head;
1544 sfile = sfile->next)
1549 printf ("%s\n", sfile->name);
1551 wr_du (p, sfile, n);
1553 wr_program_structure (p, sfile);
1554 wr_dln (p, sfile, n);
1562 /* It seems that the CS struct is not normal - the size is wrong
1563 heres one I prepared earlier.. */
1567 0x00, /* number of chars in variable length part */
1599 fwrite (b, 1, sizeof (b), file);
1602 /* Write out the SC records for a unit. Create an SC
1603 for all the sections which appear in the output file, even
1604 if there isn't an equivalent one on the input */
1608 struct coff_ofile *ptr;
1609 struct coff_sfile *sfile;
1613 /* First work out the total number of sections */
1615 int total_sec = ptr->nsections;
1619 struct coff_section *sec;
1620 struct coff_symbol *symbol;
1622 struct coff_symbol *symbol;
1625 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1629 for (i = 0; i < total_sec; i++)
1631 info[i].sec = ptr->sections + i;
1635 for (symbol = sfile->scope->vars_head;
1637 symbol = symbol->next)
1640 if (symbol->type->type == coff_secdef_type)
1642 for (i = 0; i < total_sec; i++)
1644 if (symbol->where->section == info[i].sec)
1646 info[i].symbol = symbol;
1653 /* Now output all the section info, and fake up some stuff for sections
1656 for (i = 1; i < total_sec; i++)
1660 symbol = info[i].symbol;
1665 /* Don't have a symbol set aside for this section, which means that nothing
1666 in this file does anything for the section. */
1667 sc.format = !(abfd->flags & EXEC_P);
1670 name = info[i].sec->name;
1674 if (abfd->flags & EXEC_P)
1677 sc.addr = symbol->where->offset;
1684 sc.length = symbol->type->size;
1685 name = symbol->name;
1690 sc.concat = CONCAT_SIMPLE;
1698 sc.spare1 = 0; /* If not zero, then it doesn't work */
1699 sc.name = section_translate (name);
1700 if (strlen (sc.name) == 1)
1706 sc.contents = CONTENTS_DATA;
1709 sc.contents = CONTENTS_CODE;
1714 sc.contents = CONTENTS_CODE;
1718 sysroff_swap_sc_out (file, &sc);
1726 /* Write out the ER records for a unit. */
1728 wr_er (ptr, sfile, first)
1729 struct coff_ofile *ptr;
1730 struct coff_sfile *sfile;
1734 struct coff_symbol *sym;
1737 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1739 if (sym->visible->type == coff_vis_ext_ref)
1743 er.type = ER_NOTSPEC;
1744 er.name = sym->name;
1745 sysroff_swap_er_out (file, &er);
1746 sym->er_number = idx++;
1752 /* Write out the ED records for a unit. */
1754 wr_ed (ptr, sfile, first)
1755 struct coff_ofile *ptr;
1756 struct coff_sfile *sfile;
1759 struct coff_symbol *s;
1762 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1764 if (s->visible->type == coff_vis_ext_def
1765 || s->visible->type == coff_vis_common)
1769 ed.section = s->where->section->number;
1771 if (s->where->section->data)
1773 ed.type = ED_TYPE_DATA;
1775 else if (s->where->section->code & SEC_CODE)
1777 ed.type = ED_TYPE_ENTRY;
1781 ed.type = ED_TYPE_NOTSPEC;
1782 ed.type = ED_TYPE_DATA;
1784 ed.address = s->where->offset - s->where->section->address;
1786 sysroff_swap_ed_out (file, &ed);
1794 struct coff_ofile *ptr;
1796 struct coff_sfile *sfile;
1798 for (sfile = ptr->source_head;
1800 sfile = sfile->next)
1806 wr_un (ptr, sfile, first, 0);
1807 nsecs = wr_sc (ptr, sfile);
1809 fseek (file, p1, SEEK_SET);
1810 wr_un (ptr, sfile, first, nsecs);
1811 fseek (file, p2, SEEK_SET);
1812 wr_er (ptr, sfile, first);
1813 wr_ed (ptr, sfile, first);
1820 struct coff_ofile *p;
1834 return (x + 3) & ~3;
1837 /* Find all the common variables and turn them into
1838 ordinary defs - dunno why, but thats what hitachi does with 'em */
1842 struct coff_ofile *tree;
1844 struct coff_symbol *s;
1845 struct coff_section *common_section;
1846 /* Find the common section - always section 3 */
1847 common_section = tree->sections + 3;
1848 for (s = tree->symbol_list_head;
1850 s = s->next_in_ofile_list)
1852 if (s->visible->type == coff_vis_common)
1854 struct coff_where *w = s->where;
1855 /* s->visible->type = coff_vis_ext_def; leave it as common */
1856 common_section->size = align (common_section->size);
1857 w->offset = common_section->size + common_section->address;
1858 w->section = common_section;
1859 common_section->size += s->type->size;
1860 common_section->size = align (common_section->size);
1868 show_usage (file, status)
1872 fprintf (file, "Usage: %s [-dhVq] in-file [out-file]\n", program_name);
1879 printf ("%s: Convert a COFF object file into a SYSROFF object file\n",
1881 show_usage (stdout, 0);
1892 static struct option long_options[] =
1894 {"debug", no_argument, 0, 'd'},
1895 {"quick", no_argument, 0, 'q'},
1896 {"noprescan", no_argument, 0, 'n'},
1897 {"help", no_argument, 0, 'h'},
1898 {"version", no_argument, 0, 'V'},
1899 {NULL, no_argument, 0, 0}
1905 program_name = av[0];
1906 xmalloc_set_program_name (program_name);
1908 while ((opt = getopt_long (ac, av, "dhVqn", long_options,
1927 printf ("GNU %s version %s\n", program_name, PROGRAM_VERSION);
1933 show_usage (stderr, 1);
1938 /* The input and output files may be named on the command line. */
1942 input_file = av[optind];
1946 output_file = av[optind];
1949 show_usage (stderr, 1);
1950 if (strcmp (input_file, output_file) == 0)
1953 "%s: input and output files must be different\n",
1964 fprintf (stderr, "%s: no input file specified\n",
1971 /* Take a .o off the input file and stick on a .obj. If
1972 it doesn't end in .o, then stick a .obj on anyway */
1974 int len = strlen (input_file);
1975 output_file = xmalloc (len + 5);
1976 strcpy (output_file, input_file);
1978 && output_file[len - 2] == '.'
1979 && output_file[len - 1] == 'o')
1981 output_file[len] = 'b';
1982 output_file[len + 1] = 'j';
1983 output_file[len + 2] = 0;
1987 strcat (output_file, ".obj");
1991 abfd = bfd_openr (input_file, 0);
1994 bfd_fatal (input_file);
1996 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
1998 bfd_nonfatal (input_file);
1999 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2001 list_matching_formats (matching);
2007 file = fopen (output_file, FOPEN_WB);
2011 fprintf (stderr, "%s: unable to open output file %s\n",
2012 program_name, output_file);
2017 printf ("ids %d %d\n", base1, base2);
2018 tree = coff_grok (abfd);