2001-10-24 Chris Demetriou <cgd@broadcom.com>
[deliverable/binutils-gdb.git] / binutils / srconv.c
CommitLineData
252b5132 1/* srconv.c -- Sysroff conversion program
c32144ff 2 Copyright 1994, 1995, 1996, 1998, 1999, 2000, 2001
8c2bc687 3 Free Software Foundation, Inc.
252b5132
RH
4
5 This file is part of GNU Binutils.
6
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.
11
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.
16
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
20 02111-1307, USA. */
21
22/* Written by Steve Chamberlain (sac@cygnus.com)
23
24 This program can be used to convert a coff object file
25 into a Hitachi OM/LM (Sysroff) format.
26
27 All debugging information is preserved */
28
29#include <bfd.h>
30#include "bucomm.h"
31#include "sysroff.h"
32#include "coffgrok.h"
33#include <libiberty.h>
34#include <getopt.h>
35
36#include "coff/internal.h"
37#include "../bfd/libcoff.h"
38
252b5132
RH
39/*#define FOOP1 1 */
40
41static int addrsize;
42static char *toolname;
43static char **rnames;
44
c32144ff
AJ
45static int get_member_id PARAMS ((int));
46static int get_ordinary_id PARAMS ((int));
47static char *section_translate PARAMS ((char *));
48static char *strip_suffix PARAMS ((char *));
49static void checksum PARAMS ((FILE *, char *, int, int));
50static void writeINT PARAMS ((int, char *, int *, int, FILE *));
51static void writeBITS PARAMS ((int, char *, int *, int));
52static void writeBARRAY PARAMS ((barray, char *, int *, int, FILE *));
53static void writeCHARS PARAMS ((char *, char *, int *, int, FILE *));
54static void wr_tr PARAMS ((void));
55static void wr_un PARAMS ((struct coff_ofile *, struct coff_sfile *, int, int));
56static void wr_hd PARAMS ((struct coff_ofile *));
57static void wr_sh PARAMS ((struct coff_ofile *, struct coff_section *));
58static void wr_ob PARAMS ((struct coff_ofile *, struct coff_section *));
59static void wr_rl PARAMS ((struct coff_ofile *, struct coff_section *));
60static void wr_object_body PARAMS ((struct coff_ofile *));
61static void wr_dps_start
62 PARAMS ((struct coff_sfile *, struct coff_section *, struct coff_scope *,
63 int, int));
64static void wr_dps_end
65 PARAMS ((struct coff_section *, struct coff_scope *, int));
66static int *nints PARAMS ((int));
67static void walk_tree_type_1
68 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
69 int));
70static void walk_tree_type
71 PARAMS ((struct coff_sfile *, struct coff_symbol *, struct coff_type *,
72 int));
73static void walk_tree_symbol
74 PARAMS ((struct coff_sfile *, struct coff_section *,
75 struct coff_symbol *, int));
76static void walk_tree_scope
77 PARAMS ((struct coff_section *, struct coff_sfile *, struct coff_scope *,
78 int, int));
79static void walk_tree_sfile
80 PARAMS ((struct coff_section *, struct coff_sfile *));
81static void wr_program_structure
82 PARAMS ((struct coff_ofile *, struct coff_sfile *));
83static void wr_du PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
84static void wr_dus PARAMS ((struct coff_ofile *, struct coff_sfile *));
85static int find_base PARAMS ((struct coff_sfile *, struct coff_section *));
86static void wr_dln PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
87static void wr_globals
88 PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
89static void wr_debug PARAMS ((struct coff_ofile *));
90static void wr_cs PARAMS ((void));
91static int wr_sc PARAMS ((struct coff_ofile *, struct coff_sfile *));
92static void wr_er PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
93static void wr_ed PARAMS ((struct coff_ofile *, struct coff_sfile *, int));
94static void wr_unit_info PARAMS ((struct coff_ofile *));
95static void wr_module PARAMS ((struct coff_ofile *));
96static int align PARAMS ((int));
97static void prescan PARAMS ((struct coff_ofile *));
98static void show_usage PARAMS ((FILE *, int));
99static void show_help PARAMS ((void));
100extern int main PARAMS ((int, char **));
252b5132
RH
101
102static FILE *file;
103static bfd *abfd;
104static int debug = 0;
105static int quick = 0;
106static int noprescan = 0;
107static struct coff_ofile *tree;
108/* Obsolete ??
109 static int absolute_p;
110 */
111
112static int segmented_p;
113static int code;
114
115static int ids1[20000];
116static int ids2[20000];
117
118static int base1 = 0x18;
119static int base2 = 0x2018;
120
121static int
122get_member_id (x)
123 int x;
124{
125 if (ids2[x])
126 {
127 return ids2[x];
128 }
129 ids2[x] = base2++;
130 return ids2[x];
131}
132
133static int
134get_ordinary_id (x)
135 int x;
136{
137 if (ids1[x])
138 {
139 return ids1[x];
140 }
141 ids1[x] = base1++;
142 return ids1[x];
143}
144static char *
145section_translate (n)
146 char *n;
147{
148 if (strcmp (n, ".text") == 0)
149 return "P";
150 if (strcmp (n, ".data") == 0)
151 return "D";
152 if (strcmp (n, ".bss") == 0)
153 return "B";
154 return n;
155}
156
157
158
159#define DATE "940201073000"; /* Just a time on my birthday */
160
161
162static
163char *
164strip_suffix (name)
165 char *name;
166{
167 int i;
168 char *res;
169 for (i = 0; name[i] != 0 && name[i] != '.'; i++)
170 ;
171 res = (char *) xmalloc (i + 1);
172 memcpy (res, name, i);
173 res[i] = 0;
174 return res;
175}
176
177
178/* IT LEN stuff CS */
179static void
180checksum (file, ptr, size, code)
181 FILE *file;
182 char *ptr;
183 int size;
184 int code;
185{
186 int j;
187 int last;
188 int sum = 0;
189 int bytes = size / 8;
190 last = !(code & 0xff00);
191 if (size & 0x7)
192 abort ();
193 ptr[0] = code | (last ? 0x80 : 0);
194 ptr[1] = bytes + 1;
195
196 for (j = 0; j < bytes; j++)
197 {
198 sum += ptr[j];
199 }
200 /* Glue on a checksum too */
201 ptr[bytes] = ~sum;
202 fwrite (ptr, bytes + 1, 1, file);
203}
204
205
206
207
208static void
209writeINT (n, ptr, idx, size, file)
210 int n;
211 char *ptr;
212 int *idx;
213 int size;
214 FILE *file;
215{
216 int byte = *idx / 8;
217
218 if (size == -2)
219 size = addrsize;
220 else if (size == -1)
221 size = 0;
222
223 if (byte > 240)
224 {
225 /* Lets write out that record and do another one */
226 checksum (file, ptr, *idx, code | 0x1000);
227 *idx = 16;
228 byte = *idx / 8;
229 }
230 switch (size)
231 {
232 case 0:
233 break;
234 case 1:
235 ptr[byte] = n;
236 break;
237 case 2:
238 ptr[byte + 0] = n >> 8;
239 ptr[byte + 1] = n;
240 break;
241 case 4:
242 ptr[byte + 0] = n >> 24;
243 ptr[byte + 1] = n >> 16;
244 ptr[byte + 2] = n >> 8;
245 ptr[byte + 3] = n >> 0;
246 break;
247 default:
248 abort ();
249 }
250 *idx += size * 8;
251}
252
253
254static void
255writeBITS (val, ptr, idx, size)
256 int val;
257 char *ptr;
258 int *idx;
259 int size;
260{
261 int byte = *idx / 8;
262 int bit = *idx % 8;
263 int old;
264 *idx += size;
265
266 old = ptr[byte];
267 /* Turn off all about to change bits */
268 old &= ~((~0 >> (8 - bit - size)) & ((1 << size) - 1));
269 /* Turn on the bits we want */
270 old |= (val & ((1 << size) - 1)) << (8 - bit - size);
271 ptr[byte] = old;
272}
273
274static void
275writeBARRAY (data, ptr, idx, size, file)
276 barray data;
277 char *ptr;
278 int *idx;
0f371bb4 279 int size ATTRIBUTE_UNUSED;
252b5132
RH
280 FILE *file;
281{
282 int i;
283 writeINT (data.len, ptr, idx, 1, file);
284 for (i = 0; i < data.len; i++)
285 {
286 writeINT (data.data[i], ptr, idx, 1, file);
287 }
288}
289
290
291static void
292writeCHARS (string, ptr, idx, size, file)
293 char *string;
294 char *ptr;
295 int *idx;
296 int size;
297 FILE *file;
298{
299 int i = *idx / 8;
300
301 if (i > 240)
302 {
303 /* Lets write out that record and do another one */
304 checksum (file, ptr, *idx, code | 0x1000);
305 *idx = 16;
306 i = *idx / 8;
307 }
308
309 if (size == 0)
310 {
311 /* Variable length string */
312 size = strlen (string);
313 ptr[i++] = size;
314 }
315
316 /* BUG WAITING TO HAPPEN */
317 memcpy (ptr + i, string, size);
318 i += size;
319 *idx = i * 8;
320}
321
322#define SYSROFF_SWAP_OUT
323#include "sysroff.c"
324
325
326static char *rname_sh[] =
327{
328 "R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9", "R10", "R11", "R12", "R13", "R14", "R15"
329};
330
331static char *rname_h8300[] =
332{
333 "ER0", "ER1", "ER2", "ER3", "ER4", "ER5", "ER6", "ER7", "PC", "CCR"
334};
335
336static void
337wr_tr ()
338{
339 /* The TR block is not normal - it doesn't have any contents. */
340
341 static char b[] = {
342 0xff, /* IT */
343 0x03, /* RL */
344 0xfd, /* CS */
345 };
346 fwrite (b, 1, sizeof (b), file);
347}
348
349static void
350wr_un (ptr, sfile, first, nsecs)
351 struct coff_ofile *ptr;
352 struct coff_sfile *sfile;
353 int first;
0f371bb4 354 int nsecs ATTRIBUTE_UNUSED;
252b5132
RH
355{
356 struct IT_un un;
357
358 struct coff_symbol *s;
359
360 un.spare1 = 0;
361
362 if (bfd_get_file_flags (abfd) & EXEC_P)
363 un.format = FORMAT_LM;
364 else
365 un.format = FORMAT_OM;
366 un.spare1 = 0;
367
368
369#if 1
370 un.nsections = ptr->nsections - 1; /* Don't count the abs section */
371#else
372 /*NEW - only count sections with size */
373 un.nsections = nsecs;
374#endif
375
376 un.nextdefs = 0;
377 un.nextrefs = 0;
378 /* Count all the undefined and defined variables with global scope */
379
380 if (first)
381 {
382 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
383 {
384 if (s->visible->type == coff_vis_ext_def
385 || s->visible->type == coff_vis_common)
386 un.nextdefs++;
387
388 if (s->visible->type == coff_vis_ext_ref)
389 un.nextrefs++;
390 }
391 }
392 un.tool = toolname;
393 un.tcd = DATE;
394 un.linker = "L_GX00";
395 un.lcd = DATE;
396 un.name = sfile->name;
397 sysroff_swap_un_out (file, &un);
398}
399
400
401static void
402wr_hd (p)
403 struct coff_ofile *p;
404{
405 struct IT_hd hd;
406
407 hd.spare1 = 0;
408 if (bfd_get_file_flags (abfd) & EXEC_P)
409 {
410 hd.mt = MTYPE_ABS_LM;
411 }
412 else
413 {
414 hd.mt = MTYPE_OMS_OR_LMS;
415 }
416 hd.cd = DATE;
417
418 hd.nu = p->nsources; /* Always one unit */
419 hd.code = 0; /* Always ASCII */
420 hd.ver = "0200"; /* Version 2.00 */
421 switch (bfd_get_arch (abfd))
422 {
423 case bfd_arch_h8300:
424 hd.au = 8;
425 hd.si = 0;
426 hd.spcsz = 32;
427 hd.segsz = 0;
428 hd.segsh = 0;
429 switch (bfd_get_mach (abfd))
430 {
431 case bfd_mach_h8300:
432 hd.cpu = "H8300";
433 hd.afl = 2;
434 addrsize = 2;
435 toolname = "C_H8/300";
436 break;
437 case bfd_mach_h8300h:
438 hd.cpu = "H8300H";
439 hd.afl = 4;
440 addrsize = 4;
441 toolname = "C_H8/300H";
442 break;
443 case bfd_mach_h8300s:
444 hd.cpu = "H8300S";
445 hd.afl = 4;
446 addrsize = 4;
447 toolname = "C_H8/300S";
448 break;
449 default:
450 abort();
451 }
452 rnames = rname_h8300;
453 break;
454 case bfd_arch_sh:
455 hd.au = 8;
456 hd.si = 0;
457 hd.afl = 4;
458 hd.spcsz = 32;
459 hd.segsz = 0;
460 hd.segsh = 0;
461 hd.cpu = "SH";
462 addrsize = 4;
463 toolname = "C_SH";
464 rnames = rname_sh;
465 break;
466 default:
467 abort ();
468 }
469
470 if (! bfd_get_file_flags(abfd) & EXEC_P)
471 {
472 hd.ep = 0;
473 }
474 else
475 {
476 hd.ep = 1;
477 hd.uan = 0;
478 hd.sa = 0;
479 hd.sad = 0;
480 hd.address = bfd_get_start_address (abfd);
481 }
482
483 hd.os = "";
484 hd.sys = "";
485 hd.mn = strip_suffix (bfd_get_filename (abfd));
486
487 sysroff_swap_hd_out (file, &hd);
488}
489
490
491static void
492wr_sh (p, sec)
0f371bb4 493 struct coff_ofile *p ATTRIBUTE_UNUSED;
252b5132
RH
494 struct coff_section *sec;
495{
496 struct IT_sh sh;
497 sh.unit = 0;
498 sh.section = sec->number;
499#ifdef FOOP1
500 sh.section = 0;
501#endif
502 sysroff_swap_sh_out (file, &sh);
503}
504
505
506static void
507wr_ob (p, section)
0f371bb4 508 struct coff_ofile *p ATTRIBUTE_UNUSED;
252b5132
RH
509 struct coff_section *section;
510{
511 bfd_size_type i;
512 int first = 1;
513 unsigned char stuff[200];
514
515 i = 0;
516 while (i < section->bfd_section->_raw_size)
517 {
518 struct IT_ob ob;
519 int todo = 200; /* Copy in 200 byte lumps */
520 ob.spare = 0;
521 if (i + todo > section->bfd_section->_raw_size)
522 todo = section->bfd_section->_raw_size - i;
523
524 if (first)
525 {
526 ob.saf = 1;
527 if (bfd_get_file_flags (abfd) & EXEC_P)
528 ob.address = section->address;
529 else
530 ob.address = 0;
531
532 first = 0;
533 }
534 else
535 {
536 ob.saf = 0;
537 }
538
539 ob.cpf = 0; /* Never compress */
540 ob.data.len = todo;
541 bfd_get_section_contents (abfd, section->bfd_section, stuff, i, todo);
542 ob.data.data = stuff;
543 sysroff_swap_ob_out (file, &ob /*, i + todo < section->size */ );
544 i += todo;
545 }
546 /* Now fill the rest with blanks */
547 while (i < (bfd_size_type) section->size)
548 {
549 struct IT_ob ob;
550 int todo = 200; /* Copy in 200 byte lumps */
551 ob.spare = 0;
552 if (i + todo > (bfd_size_type) section->size)
553 todo = section->size - i;
554 ob.saf = 0;
555
556 ob.cpf = 0; /* Never compress */
557 ob.data.len = todo;
558 memset (stuff, 0, todo);
559 ob.data.data = stuff;
560 sysroff_swap_ob_out (file, &ob);
561 i += todo;
562 }
563 /* Now fill the rest with blanks */
564
565}
566
567static void
568wr_rl (ptr, sec)
0f371bb4 569 struct coff_ofile *ptr ATTRIBUTE_UNUSED;
252b5132
RH
570 struct coff_section *sec;
571{
572 int nr = sec->nrelocs;
573 int i;
574 for (i = 0; i < nr; i++)
575 {
576 struct coff_reloc *r = sec->relocs + i;
577 struct coff_symbol *ref;
578 struct IT_rl rl;
579 rl.apol = 0;
580 rl.boundary = 0;
581 rl.segment = 1;
582 rl.sign = 0;
583 rl.check = 0;
584 rl.addr = r->offset;
585 rl.bitloc = 0;
586 rl.flen = 32; /* SH Specific */
587 /* What sort of reloc ? Look in the section to find out */
588 ref = r->symbol;
589 if (ref->visible->type == coff_vis_ext_ref)
590 {
591 rl.bcount = 4; /* Always 4 for us */
592 rl.op = OP_EXT_REF;
593 rl.symn = ref->er_number;
594 }
595 else if (ref->visible->type == coff_vis_common)
596 {
597 rl.bcount = 11; /* Always 11 for us */
598 rl.op = OP_SEC_REF;
599 rl.secn = ref->where->section->number;
600 rl.copcode_is_3 = 3;
601 rl.alength_is_4 = 4;
602 rl.addend = ref->where->offset - ref->where->section->address;
603 rl.aopcode_is_0x20 = 0x20;
604 }
605
606 else
607 {
608 rl.bcount = 11; /* Always 11 for us */
609 rl.op = OP_SEC_REF;
610 rl.secn = ref->where->section->number;
611 rl.copcode_is_3 = 3;
612 rl.alength_is_4 = 4;
613 rl.addend = -ref->where->section->address;
614 rl.aopcode_is_0x20 = 0x20;
615 }
616 rl.end = 0xff;
617 if (rl.op == OP_SEC_REF
618 || rl.op == OP_EXT_REF)
619 {
620 sysroff_swap_rl_out (file, &rl);
621 }
622 }
623}
624
625static void
626wr_object_body (p)
627 struct coff_ofile *p;
628{
629 int i;
630 for (i = 1; i < p->nsections; i++)
631 {
632 wr_sh (p, p->sections + i);
633 wr_ob (p, p->sections + i);
634 wr_rl (p, p->sections + i);
635 }
636}
637
638static void
639wr_dps_start (sfile, section, scope, type, nest)
640 struct coff_sfile *sfile;
0f371bb4 641 struct coff_section *section ATTRIBUTE_UNUSED;
252b5132
RH
642 struct coff_scope *scope;
643 int type;
644 int nest;
645{
646 struct IT_dps dps;
647 dps.end = 0;
648 dps.opt = 0;
649 dps.type = type;
650 if (scope->sec)
651 {
652 dps.san = scope->sec->number;
653 dps.address = scope->offset - find_base (sfile, scope->sec);
654 dps.block_size = scope->size;
655 if (debug)
656 {
657 printf ("DPS %s %d %x\n",
658 sfile->name,
659 nest,
660 dps.address);
661
662 }
663 }
664 else
665 {
666 dps.san = 0;
667 dps.address = 0;
668 dps.block_size = 0;
669 }
670
671 dps.nesting = nest;
672 dps.neg = 0x1001;
673 sysroff_swap_dps_out (file, &dps);
674}
675
676static void
677wr_dps_end (section, scope, type)
0f371bb4
AM
678 struct coff_section *section ATTRIBUTE_UNUSED;
679 struct coff_scope *scope ATTRIBUTE_UNUSED;
252b5132
RH
680 int type;
681{
682 struct IT_dps dps;
683 dps.end = 1;
684 dps.type = type;
685 sysroff_swap_dps_out (file, &dps);
686}
687
688static int *
689nints (x)
690 int x;
691{
692 return (int *) (xcalloc (sizeof (int), x));
693}
694
252b5132
RH
695static void
696walk_tree_type_1 (sfile, symbol, type, nest)
697 struct coff_sfile *sfile;
698 struct coff_symbol *symbol;
699 struct coff_type *type;
700 int nest;
701{
702 switch (type->type)
703 {
704 case coff_secdef_type:
705 case coff_basic_type:
706 {
707 struct IT_dbt dbt;
708
709 switch (type->u.basic)
710 {
711 case T_NULL:
712 case T_VOID:
713 dbt.btype = BTYPE_VOID;
714 dbt.sign = BTYPE_UNSPEC;
715 dbt.fptype = FPTYPE_NOTSPEC;
716 break;
717 case T_CHAR:
718 dbt.btype = BTYPE_CHAR;
719 dbt.sign = BTYPE_UNSPEC;
720 dbt.fptype = FPTYPE_NOTSPEC;
721 break;
722 case T_SHORT:
723 case T_INT:
724 case T_LONG:
725 dbt.btype = BTYPE_INT;
726 dbt.sign = SIGN_SIGNED;
727 dbt.fptype = FPTYPE_NOTSPEC;
728 break;
729 case T_FLOAT:
730 dbt.btype = BTYPE_FLOAT;
731 dbt.fptype = FPTYPE_SINGLE;
732 break;
733 case T_DOUBLE:
734 dbt.btype = BTYPE_FLOAT;
735 dbt.fptype = FPTYPE_DOUBLE;
736 break;
737 case T_LNGDBL:
738 dbt.btype = BTYPE_FLOAT;
739 dbt.fptype = FPTYPE_EXTENDED;
740 break;
741 case T_UCHAR:
742 dbt.btype = BTYPE_CHAR;
743 dbt.sign = SIGN_UNSIGNED;
744 dbt.fptype = FPTYPE_NOTSPEC;
745 break;
746 case T_USHORT:
747 case T_UINT:
748 case T_ULONG:
749 dbt.btype = BTYPE_INT;
750 dbt.sign = SIGN_UNSIGNED;
751 dbt.fptype = FPTYPE_NOTSPEC;
752 break;
753 }
754 dbt.bitsize = type->size;
755 dbt.neg = 0x1001;
756 sysroff_swap_dbt_out (file, &dbt);
757 break;
758 }
759 case coff_pointer_type:
760 {
761 struct IT_dpt dpt;
762 walk_tree_type_1 (sfile, symbol, type->u.pointer.points_to, nest + 1);
763 dpt.neg = 0x1001;
764 sysroff_swap_dpt_out (file, &dpt);
765 break;
766 }
767
768 case coff_function_type:
769 {
770 struct IT_dfp dfp;
771 struct coff_symbol *param;
772 dfp.end = 0;
773 dfp.spare = 0;
774 dfp.nparams = type->u.function.parameters->nvars;
775 dfp.neg = 0x1001;
776
777 walk_tree_type_1 (sfile, symbol, type->u.function.function_returns, nest + 1);
778
779 sysroff_swap_dfp_out (file, &dfp);
780
781 for (param = type->u.function.parameters->vars_head;
782 param;
783 param = param->next)
784 {
785 walk_tree_symbol (sfile, 0, param, nest);
786 }
787 dfp.end = 1;
788 sysroff_swap_dfp_out (file, &dfp);
789 break;
790 }
791
792 case coff_structdef_type:
793 {
794 struct IT_dbt dbt;
795 struct IT_dds dds;
796 struct coff_symbol *member;
797 dds.spare = 0;
798 dbt.btype = BTYPE_STRUCT;
799 dbt.bitsize = type->size;
800 dbt.sign = SIGN_UNSPEC;
801 dbt.fptype = FPTYPE_NOTSPEC;
802 dbt.sid = get_member_id (type->u.astructdef.idx);
803 dbt.neg = 0x1001;
804 sysroff_swap_dbt_out (file, &dbt);
805 dds.end = 0;
806 dds.neg = 0x1001;
807 sysroff_swap_dds_out (file, &dds);
808 for (member = type->u.astructdef.elements->vars_head;
809 member;
810 member = member->next)
811 {
812 walk_tree_symbol (sfile, 0, member, nest + 1);
813 }
814
815 dds.end = 1;
816 sysroff_swap_dds_out (file, &dds);
817
818 }
819 break;
820 case coff_structref_type:
821 {
822 struct IT_dbt dbt;
823 dbt.btype = BTYPE_TAG;
824 dbt.bitsize = type->size;
825 dbt.sign = SIGN_UNSPEC;
826 dbt.fptype = FPTYPE_NOTSPEC;
827 if (type->u.astructref.ref)
828 {
829 dbt.sid = get_member_id (type->u.astructref.ref->number);
830 }
831 else
832 {
833 dbt.sid = 0;
834 }
835
836 dbt.neg = 0x1001;
837 sysroff_swap_dbt_out (file, &dbt);
838 }
839 break;
840 case coff_array_type:
841 {
842 struct IT_dar dar;
843 int j;
844 int dims = 1; /* Only output one dimension at a time */
845 dar.dims = dims;
846 dar.variable = nints (dims);
847 dar.subtype = nints (dims);
848 dar.spare = nints (dims);
849 dar.max_variable = nints (dims);
850 dar.maxspare = nints (dims);
851 dar.max = nints (dims);
852 dar.min_variable = nints (dims);
853 dar.min = nints (dims);
854 dar.minspare = nints (dims);
855 dar.neg = 0x1001;
856 dar.length = type->size / type->u.array.dim;
857 for (j = 0; j < dims; j++)
858 {
859 dar.variable[j] = VARIABLE_FIXED;
860 dar.subtype[j] = SUB_INTEGER;
861 dar.spare[j] = 0;
862 dar.max_variable[j] = 0;
863 dar.max[j] = type->u.array.dim;
864 dar.min_variable[j] = 0;
865 dar.min[j] = 1; /* Why isn't this 0 ? */
866 }
867 walk_tree_type_1 (sfile, symbol, type->u.array.array_of, nest + 1);
868 sysroff_swap_dar_out (file, &dar);
869 }
870 break;
871 case coff_enumdef_type:
872 {
873 struct IT_dbt dbt;
874 struct IT_den den;
875 struct coff_symbol *member;
876 dbt.btype = BTYPE_ENUM;
877 dbt.bitsize = type->size;
878 dbt.sign = SIGN_UNSPEC;
879 dbt.fptype = FPTYPE_NOTSPEC;
880 dbt.sid = get_member_id (type->u.aenumdef.idx);
881 dbt.neg = 0x1001;
882 sysroff_swap_dbt_out (file, &dbt);
883
884 den.end = 0;
885 den.neg = 0x1001;
886 den.spare = 0;
887 sysroff_swap_den_out (file, &den);
888 for (member = type->u.aenumdef.elements->vars_head;
889 member;
890 member = member->next)
891 {
892 walk_tree_symbol (sfile, 0, member, nest + 1);
893 }
894
895 den.end = 1;
896 sysroff_swap_den_out (file, &den);
897 }
898 break;
899
900 break;
901 case coff_enumref_type:
902 {
903 struct IT_dbt dbt;
904 dbt.btype = BTYPE_TAG;
905 dbt.bitsize = type->size;
906 dbt.sign = SIGN_UNSPEC;
907 dbt.fptype = FPTYPE_NOTSPEC;
908 dbt.sid = get_member_id (type->u.aenumref.ref->number);
909 dbt.neg = 0x1001;
910 sysroff_swap_dbt_out (file, &dbt);
911 }
912 break;
913 default:
914 abort ();
915 }
916}
917
918/* Obsolete ?
919 static void
920 dty_start ()
921 {
922 struct IT_dty dty;
923 dty.end = 0;
924 dty.neg = 0x1001;
925 dty.spare = 0;
926 sysroff_swap_dty_out (file, &dty);
927 }
928
929 static void
930 dty_stop ()
931 {
932 struct IT_dty dty;
933 dty.end = 0;
934 dty.neg = 0x1001;
935 dty.end = 1;
936 sysroff_swap_dty_out (file, &dty);
937 }
938
939
940 static void
941 dump_tree_structure (sfile, symbol, type, nest)
942 struct coff_sfile *sfile;
943 struct coff_symbol *symbol;
944 struct coff_type *type;
945 int nest;
946 {
947 if (symbol->type->type == coff_function_type)
948 {
949
950
951 }
952
953 }
954 */
955
956static void
957walk_tree_type (sfile, symbol, type, nest)
958
959 struct
960 coff_sfile *sfile;
961 struct coff_symbol *symbol;
962 struct coff_type *type;
963 int nest;
964{
965 if (symbol->type->type == coff_function_type)
966 {
967
968 struct IT_dty dty;
969 dty.end = 0;
970 dty.neg = 0x1001;
971
972 sysroff_swap_dty_out (file, &dty);
973 walk_tree_type_1 (sfile, symbol, type, nest);
974 dty.end = 1;
975 sysroff_swap_dty_out (file, &dty);
976
977 wr_dps_start (sfile,
978 symbol->where->section,
979 symbol->type->u.function.code,
980 BLOCK_TYPE_FUNCTION, nest);
981 wr_dps_start (sfile, symbol->where->section,
982 symbol->type->u.function.code,
983 BLOCK_TYPE_BLOCK, nest);
984 walk_tree_scope (symbol->where->section,
985 sfile,
986 symbol->type->u.function.code,
987 nest + 1, BLOCK_TYPE_BLOCK);
988
989 wr_dps_end (symbol->where->section,
990 symbol->type->u.function.code,
991 BLOCK_TYPE_BLOCK);
992 wr_dps_end (symbol->where->section,
993 symbol->type->u.function.code, BLOCK_TYPE_FUNCTION);
994
995 }
996 else
997 {
998 struct IT_dty dty;
999 dty.end = 0;
1000 dty.neg = 0x1001;
1001 sysroff_swap_dty_out (file, &dty);
1002 walk_tree_type_1 (sfile, symbol, type, nest);
1003 dty.end = 1;
1004 sysroff_swap_dty_out (file, &dty);
1005 }
1006
1007}
1008
1009
1010
1011static void
1012walk_tree_symbol (sfile, section, symbol, nest)
1013 struct coff_sfile *sfile;
0f371bb4 1014 struct coff_section *section ATTRIBUTE_UNUSED;
252b5132
RH
1015 struct coff_symbol *symbol;
1016 int nest;
1017{
1018 struct IT_dsy dsy;
1019
1020 memset(&dsy, 0, sizeof(dsy));
1021 dsy.nesting = nest;
1022
1023 switch (symbol->type->type)
1024 {
1025 case coff_function_type:
1026 dsy.type = STYPE_FUNC;
1027 dsy.assign = 1;
1028 break;
1029 case coff_structref_type:
1030 case coff_pointer_type:
1031 case coff_array_type:
1032 case coff_basic_type:
1033 case coff_enumref_type:
1034 dsy.type = STYPE_VAR;
1035 dsy.assign = 1;
1036 break;
1037 case coff_enumdef_type:
1038 dsy.type = STYPE_TAG;
1039 dsy.assign = 0;
1040 dsy.magic = 2;
1041 break;
1042 case coff_structdef_type:
1043 dsy.type = STYPE_TAG;
1044 dsy.assign = 0;
1045 dsy.magic = symbol->type->u.astructdef.isstruct ? 0 : 1;
1046 break;
1047 case coff_secdef_type:
1048 return;
1049 default:
1050 abort ();
1051 }
1052
1053 if (symbol->where->where == coff_where_member_of_struct)
1054 {
1055 dsy.assign = 0;
1056 dsy.type = STYPE_MEMBER;
1057 }
1058 if (symbol->where->where == coff_where_member_of_enum)
1059 {
1060 dsy.type = STYPE_ENUM;
1061 dsy.assign = 0;
1062 dsy.evallen = 4;
1063 dsy.evalue = symbol->where->offset;
1064 }
1065
1066 if (symbol->type->type == coff_structdef_type
1067 || symbol->where->where == coff_where_entag
1068 || symbol->where->where == coff_where_strtag)
1069 {
1070 dsy.snumber = get_member_id (symbol->number);
1071 }
1072 else
1073 {
1074 dsy.snumber = get_ordinary_id (symbol->number);
1075 }
1076
1077
1078 dsy.sname = symbol->name[0] == '_' ? symbol->name + 1 : symbol->name;
1079
1080 switch (symbol->visible->type)
1081 {
1082 case coff_vis_common:
1083 case coff_vis_ext_def:
1084 dsy.ainfo = AINFO_STATIC_EXT_DEF;
1085 break;
1086 case coff_vis_ext_ref:
1087 dsy.ainfo = AINFO_STATIC_EXT_REF;
1088 break;
1089 case coff_vis_int_def:
1090 dsy.ainfo = AINFO_STATIC_INT;
1091 break;
1092 case coff_vis_auto:
1093 case coff_vis_autoparam:
1094 dsy.ainfo = AINFO_AUTO;
1095 break;
1096 case coff_vis_register:
1097 case coff_vis_regparam:
1098 dsy.ainfo = AINFO_REG;
1099 break;
1100 break;
1101 case coff_vis_tag:
1102 case coff_vis_member_of_struct:
1103 case coff_vis_member_of_enum:
1104 break;
1105 default:
1106 abort ();
1107 }
1108
1109 dsy.dlength = symbol->type->size;
1110 switch (symbol->where->where)
1111 {
1112 case coff_where_memory:
1113
1114 dsy.section = symbol->where->section->number;
1115#ifdef FOOP
1116 dsy.section = 0;
1117#endif
1118 break;
1119 case coff_where_member_of_struct:
1120 case coff_where_member_of_enum:
1121 case coff_where_stack:
1122 case coff_where_register:
1123 case coff_where_unknown:
1124 case coff_where_strtag:
1125
1126 case coff_where_entag:
1127 case coff_where_typedef:
1128 break;
1129 default:
1130 abort ();
1131 }
1132
1133 switch (symbol->where->where)
1134 {
1135 case coff_where_memory:
1136 dsy.address = symbol->where->offset - find_base (sfile, symbol->where->section);
1137 break;
1138 case coff_where_stack:
1139 dsy.address = symbol->where->offset;
1140 break;
1141 case coff_where_member_of_struct:
1142
1143
1144 if (symbol->where->bitsize)
1145 {
1146 int bits = (symbol->where->offset * 8 + symbol->where->bitoffset);
1147 dsy.bitunit = 1;
1148 dsy.field_len = symbol->where->bitsize;
1149 dsy.field_off = (bits / 32) * 4;
1150 dsy.field_bitoff = bits % 32;
1151 }
1152 else
1153 {
1154 dsy.bitunit = 0;
1155
1156 dsy.field_len = symbol->type->size;
1157 dsy.field_off = symbol->where->offset;
1158 }
1159 break;
1160 case coff_where_member_of_enum:
1161 /* dsy.bitunit = 0;
1162 dsy.field_len = symbol->type->size;
1163 dsy.field_off = symbol->where->offset; */
1164 break;
1165 case coff_where_register:
1166 case coff_where_unknown:
1167 case coff_where_strtag:
1168
1169 case coff_where_entag:
1170 case coff_where_typedef:
1171 break;
1172 default:
1173 abort ();
1174 }
1175
1176 if (symbol->where->where == coff_where_register)
1177 dsy.reg = rnames[symbol->where->offset];
1178
1179 switch (symbol->visible->type)
1180 {
1181 case coff_vis_common:
1182 /* We do this 'cause common C symbols are treated as extdefs */
1183 case coff_vis_ext_def:
1184 case coff_vis_ext_ref:
1185
1186 dsy.ename = symbol->name;
1187 break;
1188
1189 case coff_vis_regparam:
1190 case coff_vis_autoparam:
1191 dsy.type = STYPE_PARAMETER;
1192 break;
1193
1194 case coff_vis_int_def:
1195
1196 case coff_vis_auto:
1197 case coff_vis_register:
1198 case coff_vis_tag:
1199 case coff_vis_member_of_struct:
1200 case coff_vis_member_of_enum:
1201 break;
1202 default:
1203 abort ();
1204 }
1205
1206 dsy.sfn = 0;
1207 dsy.sln = 2;
1208
1209 dsy.neg = 0x1001;
1210
1211
1212 sysroff_swap_dsy_out (file, &dsy);
1213
1214 walk_tree_type (sfile, symbol, symbol->type, nest);
1215}
1216
1217
1218static void
1219walk_tree_scope (section, sfile, scope, nest, type)
1220 struct coff_section *section;
1221 struct coff_sfile *sfile;
1222 struct coff_scope *scope;
1223 int nest;
1224 int type;
1225{
1226 struct coff_symbol *vars;
1227 struct coff_scope *child;
1228
1229 if (scope->vars_head
1230 || (scope->list_head && scope->list_head->vars_head))
1231 {
1232 wr_dps_start (sfile, section, scope, type, nest);
1233
1234 if (nest == 0)
1235 wr_globals (tree, sfile, nest + 1);
1236
1237 for (vars = scope->vars_head; vars; vars = vars->next)
1238 {
1239 walk_tree_symbol (sfile, section, vars, nest);
1240 }
1241
1242 for (child = scope->list_head; child; child = child->next)
1243 {
1244 walk_tree_scope (section, sfile, child, nest + 1, BLOCK_TYPE_BLOCK);
1245 }
1246
1247 wr_dps_end (section, scope, type);
1248 }
1249}
1250static void
1251walk_tree_sfile (section, sfile)
1252 struct coff_section *section;
1253 struct coff_sfile *sfile;
1254{
1255 walk_tree_scope (section, sfile, sfile->scope, 0, BLOCK_TYPE_COMPUNIT);
1256
1257}
1258
1259static void
1260wr_program_structure (p, sfile)
1261 struct coff_ofile *p;
1262 struct coff_sfile *sfile;
1263{
1264
1265 walk_tree_sfile (p->sections + 4, sfile);
1266
1267}
1268
1269static void
1270wr_du (p, sfile, n)
1271 struct coff_ofile *p;
1272 struct coff_sfile *sfile;
1273 int n;
1274{
1275 struct IT_du du;
1276 int lim;
1277#if 0
1278 struct coff_symbol *symbol;
1279 static int incit = 0x500000;
1280 int used = 0;
1281#endif
1282 int i;
1283 int j;
1284 unsigned int *lowest = (unsigned *) nints (p->nsections);
1285 unsigned int *highest = (unsigned *) nints (p->nsections);
1286 du.format = bfd_get_file_flags (abfd) & EXEC_P ? 0 : 1;
1287 du.optimized = 0;
1288 du.stackfrmt = 0;
1289 du.spare = 0;
1290 du.unit = n;
1291 du.sections = p->nsections - 1;
1292 du.san = (int *) xcalloc (sizeof (int), du.sections);
1293 du.address = nints (du.sections);
1294 du.length = nints (du.sections);
1295
1296 for (i = 0; i < du.sections; i++)
1297 {
1298 lowest[i] = ~0;
1299 highest[i] = 0;
1300 }
1301
1302 /* Look through all the symbols and try and work out the extents in this
1303 source file */
1304#if 0
1305 for (symbol = sfile->scope->vars_head;
1306 symbol;
1307 symbol = symbol->next)
1308 {
1309 if (symbol->type->type == coff_secdef_type)
1310 {
1311 unsigned int low = symbol->where->offset;
1312 unsigned int high = symbol->where->offset + symbol->type->size - 1;
1313 struct coff_section *section = symbol->where->section;
1314
1315 int sn = section->number;
1316 if (low < lowest[sn])
1317 lowest[sn] = low;
1318 if (high > highest[sn])
1319 highest[sn] = high;
1320 }
1321 }
1322
1323
1324 for (i = 0; i < du.sections; i++)
1325 {
1326 if (highest[i] == 0)
1327 {
1328 lowest[i] = highest[i] = incit;
1329 }
1330 du.san[used] = i;
1331 du.length[used] = highest[i] - lowest[i];
1332 du.address[used] = bfd_get_file_flags (abfd) & EXEC_P ? lowest[i] : 0;
1333 if (debug)
1334 {
1335 printf (" section %6s 0x%08x..0x%08x\n",
1336 p->sections[i + 1].name,
1337 lowest[i],
1338 highest[i]);
1339 }
1340 used++;
1341 }
1342
1343#endif
1344 lim = du.sections;
1345 for (j = 0; j < lim; j++)
1346 {
1347 int src = j;
1348 int dst = j;
1349 du.san[dst] = dst;
1350 if (sfile->section[src].init)
1351 {
1352 du.length[dst]
1353 = sfile->section[src].high - sfile->section[src].low + 1;
1354 du.address[dst]
1355 = sfile->section[src].low;
1356 }
1357 else
1358 {
1359 du.length[dst] = 0;
1360 du.address[dst] = 0;
1361 }
1362 if (debug)
1363 {
1364 if (sfile->section[src].parent)
1365 {
1366 printf (" section %6s 0x%08x..0x%08x\n",
1367 sfile->section[src].parent->name,
1368 du.address[dst],
1369 du.address[dst] + du.length[dst] - 1);
1370 }
1371 }
1372 du.sections = dst + 1;
1373 }
1374
1375 du.tool = "c_gcc";
1376 du.date = DATE;
1377
1378 sysroff_swap_du_out (file, &du);
1379}
1380
1381static void
1382wr_dus (p, sfile)
0f371bb4 1383 struct coff_ofile *p ATTRIBUTE_UNUSED;
252b5132
RH
1384 struct coff_sfile *sfile;
1385{
1386
1387 struct IT_dus dus;
1388
1389 dus.efn = 0x1001;
1390 dus.ns = 1; /* p->nsources; sac 14 jul 94 */
1391 dus.drb = nints (dus.ns);
1392 dus.fname = (char **) xcalloc (sizeof (char *), dus.ns);
1393 dus.spare = nints (dus.ns);
1394 dus.ndir = 0;
1395 /* Find the filenames */
1396#if 0
1397 i = 0;
1398
1399 for (sfile = p->source_head;
1400 sfile;
1401 sfile = sfile->next)
1402 {
1403 dus.drb[i] = 0;
1404 dus.spare[i] = 0;
1405 dus.fname[i] = sfile->name;
1406 i++;
1407 }
1408#else
1409 dus.drb[0] = 0;
1410 dus.fname[0] = sfile->name;
1411#endif
1412
1413 sysroff_swap_dus_out (file, &dus);
1414
1415}
1416
1417/* Find the offset of the .text section for this sfile in the
1418 .text section for the output file */
1419
1420static int
1421find_base (sfile, section)
1422 struct coff_sfile *sfile;
1423 struct coff_section *section;
1424{
1425 return sfile->section[section->number].low;
1426}
0f371bb4 1427
252b5132
RH
1428static void
1429wr_dln (p, sfile, n)
0f371bb4 1430 struct coff_ofile *p ATTRIBUTE_UNUSED;
252b5132 1431 struct coff_sfile *sfile;
0f371bb4 1432 int n ATTRIBUTE_UNUSED;
252b5132
RH
1433
1434{
1435#if 0
1436 if (n == 0)
1437 {
1438 /* Count up all the linenumbers */
1439 struct coff_symbol *sy;
1440 int lc = 0;
1441 struct IT_dln dln;
1442
1443 int idx;
1444
1445 for (sy = p->symbol_list_head;
1446 sy;
1447 sy = sy->next_in_ofile_list)
1448 {
1449 struct coff_type *t = sy->type;
1450 if (t->type == coff_function_type)
1451 {
1452 struct coff_line *l = t->u.function.lines;
1453 lc += l->nlines;
1454 }
1455 }
1456
1457 dln.sfn = nints (lc);
1458 dln.sln = nints (lc);
1459 dln.lln = nints (lc);
1460 dln.section = nints (lc);
1461
1462 dln.from_address = nints (lc);
1463 dln.to_address = nints (lc);
1464
1465
1466 dln.neg = 0x1001;
1467
1468 dln.nln = lc;
1469
1470 /* Run through once more and fill up the structure */
1471 idx = 0;
1472 for (sy = p->symbol_list_head;
1473 sy;
1474 sy = sy->next_in_ofile_list)
1475 {
1476 if (sy->type->type == coff_function_type)
1477 {
1478 int i;
1479 struct coff_line *l = sy->type->u.function.lines;
1480 for (i = 0; i < l->nlines; i++)
1481 {
1482 dln.section[idx] = sy->where->section->number;
1483 dln.sfn[idx] = n;
1484 dln.sln[idx] = l->lines[i];
1485 dln.from_address[idx] = l->addresses[i];
1486 if (idx)
1487 dln.to_address[idx - 1] = dln.from_address[idx];
1488 idx++;
1489 }
1490 }
1491 n++;
1492 }
1493 sysroff_swap_dln_out (file, &dln);
1494 }
1495
1496#endif
1497#if 1
1498 /* Count up all the linenumbers */
1499
1500 struct coff_symbol *sy;
1501 int lc = 0;
1502 struct IT_dln dln;
1503
1504 int idx;
1505
1506 for (sy = sfile->scope->vars_head;
1507 sy;
1508 sy = sy->next)
1509 {
1510 struct coff_type *t = sy->type;
1511 if (t->type == coff_function_type)
1512 {
1513 struct coff_line *l = t->u.function.lines;
1514 if (l)
1515 lc += l->nlines;
1516 }
1517 }
1518
1519 dln.sfn = nints (lc);
1520 dln.sln = nints (lc);
1521 dln.cc = nints (lc);
1522 dln.section = nints (lc);
1523
1524 dln.from_address = nints (lc);
1525 dln.to_address = nints (lc);
1526
1527
1528 dln.neg = 0x1001;
1529
1530 dln.nln = lc;
1531
1532 /* Run through once more and fill up the structure */
1533 idx = 0;
1534 for (sy = sfile->scope->vars_head;
1535 sy;
1536 sy = sy->next)
1537 {
1538 if (sy->type->type == coff_function_type)
1539 {
1540 int i;
1541 struct coff_line *l = sy->type->u.function.lines;
1542 if (l)
1543 {
1544 int base = find_base (sfile, sy->where->section);
1545 for (i = 0; i < l->nlines; i++)
1546 {
1547 dln.section[idx] = sy->where->section->number;
1548 dln.sfn[idx] = 0;
1549 dln.sln[idx] = l->lines[i];
1550 dln.from_address[idx] =
1551 l->addresses[i] + sy->where->section->address - base;
1552 dln.cc[idx] = 0;
1553 if (idx)
1554 dln.to_address[idx - 1] = dln.from_address[idx];
1555 idx++;
1556
1557 }
1558 dln.to_address[idx - 1] = dln.from_address[idx - 1] + 2;
1559 }
1560 }
1561 }
1562 if (lc)
1563 sysroff_swap_dln_out (file, &dln);
1564#endif
1565}
1566
1567/* Write the global symbols out to the debug info */
1568static void
1569wr_globals (p, sfile, n)
1570 struct coff_ofile *p;
1571 struct coff_sfile *sfile;
0f371bb4 1572 int n ATTRIBUTE_UNUSED;
252b5132
RH
1573{
1574 struct coff_symbol *sy;
1575 for (sy = p->symbol_list_head;
1576 sy;
1577 sy = sy->next_in_ofile_list)
1578 {
1579 if (sy->visible->type == coff_vis_ext_def
1580 || sy->visible->type == coff_vis_ext_ref)
1581 {
1582 /* Only write out symbols if they belong to
1583 the current source file */
1584 if (sy->sfile == sfile)
1585 walk_tree_symbol (sfile, 0, sy, 0);
1586
1587 }
1588 }
1589}
1590
1591static void
1592wr_debug (p)
1593 struct coff_ofile *p;
1594{
1595 struct coff_sfile *sfile;
1596 int n = 0;
1597 for (sfile = p->source_head;
1598 sfile;
1599 sfile = sfile->next)
1600
1601 {
1602 if (debug)
1603 {
1604 printf ("%s\n", sfile->name);
1605 }
1606 wr_du (p, sfile, n);
1607 wr_dus (p, sfile);
1608 wr_program_structure (p, sfile);
1609 wr_dln (p, sfile, n);
1610 n++;
1611 }
1612}
1613
1614static void
1615wr_cs ()
1616{
1617 /* It seems that the CS struct is not normal - the size is wrong
1618 heres one I prepared earlier.. */
1619 static char b[] = {
1620 0x80, /* IT */
1621 0x21, /* RL */
1622 0x00, /* number of chars in variable length part */
1623 0x80, /* hd */
1624 0x00, /* hs */
1625 0x80, /* un */
1626 0x00, /* us */
1627 0x80, /* sc */
1628 0x00, /* ss */
1629 0x80, /* er */
1630 0x80, /* ed */
1631 0x80, /* sh */
1632 0x80, /* ob */
1633 0x80, /* rl */
1634 0x80, /* du */
1635 0x80, /* dps */
1636 0x80, /* dsy */
1637 0x80, /* dty */
1638 0x80, /* dln */
1639 0x80, /* dso */
1640 0x80, /* dus */
1641 0x00, /* dss */
1642 0x80, /* dbt */
1643 0x00, /* dpp */
1644 0x80, /* dfp */
1645 0x80, /* den */
1646 0x80, /* dds */
1647 0x80, /* dar */
1648 0x80, /* dpt */
1649 0x00, /* dul */
1650 0x00, /* dse */
1651 0x00, /* dot */
1652 0xDE /* CS */
1653 };
1654 fwrite (b, 1, sizeof (b), file);
1655}
1656
1657/* Write out the SC records for a unit. Create an SC
1658 for all the sections which appear in the output file, even
1659 if there isn't an equivalent one on the input */
1660
1661static int
1662wr_sc (ptr, sfile)
1663 struct coff_ofile *ptr;
1664 struct coff_sfile *sfile;
1665{
1666 int i;
1667int scount = 0;
1668 /* First work out the total number of sections */
1669
1670 int total_sec = ptr->nsections;
1671
1672 struct myinfo
1673 {
1674 struct coff_section *sec;
1675 struct coff_symbol *symbol;
1676 };
1677 struct coff_symbol *symbol;
1678
1679 struct myinfo *info
1680 = (struct myinfo *) calloc (total_sec, sizeof (struct myinfo));
1681
1682
1683
1684 for (i = 0; i < total_sec; i++)
1685 {
1686 info[i].sec = ptr->sections + i;
1687 info[i].symbol = 0;
1688 }
1689
1690 for (symbol = sfile->scope->vars_head;
1691 symbol;
1692 symbol = symbol->next)
1693 {
1694
1695 if (symbol->type->type == coff_secdef_type)
1696 {
1697 for (i = 0; i < total_sec; i++)
1698 {
1699 if (symbol->where->section == info[i].sec)
1700 {
1701 info[i].symbol = symbol;
1702 break;
1703 }
1704 }
1705 }
1706 }
1707
1708 /* Now output all the section info, and fake up some stuff for sections
1709 we don't have */
1710
1711 for (i = 1; i < total_sec; i++)
1712 {
1713 struct IT_sc sc;
1714 char *name;
1715 symbol = info[i].symbol;
1716 sc.spare = 0;
1717 sc.spare1 = 0;
1718 if (!symbol)
1719 {
1720 /* Don't have a symbol set aside for this section, which means that nothing
1721 in this file does anything for the section. */
1722 sc.format = !(bfd_get_file_flags (abfd) & EXEC_P);
1723 sc.addr = 0;
1724 sc.length = 0;
1725 name = info[i].sec->name;
1726 }
1727 else
1728 {
1729 if (bfd_get_file_flags (abfd) & EXEC_P)
1730 {
1731 sc.format = 0;
1732 sc.addr = symbol->where->offset;
1733 }
1734 else
1735 {
1736 sc.format = 1;
1737 sc.addr = 0;
1738 }
1739 sc.length = symbol->type->size;
1740 name = symbol->name;
1741 }
1742
1743 sc.align = 4;
1744
1745 sc.concat = CONCAT_SIMPLE;
1746 sc.read = 3;
1747 sc.write = 3;
1748 sc.exec = 3;
1749 sc.init = 3;
1750 sc.mode = 3;
1751 sc.spare = 0;
1752 sc.segadd = 0;
1753 sc.spare1 = 0; /* If not zero, then it doesn't work */
1754 sc.name = section_translate (name);
1755 if (strlen (sc.name) == 1)
1756 {
1757 switch (sc.name[0])
1758 {
1759 case 'D':
1760 case 'B':
1761 sc.contents = CONTENTS_DATA;
1762 break;
1763 default:
1764 sc.contents = CONTENTS_CODE;
1765 }
1766 }
1767 else
1768 {
1769 sc.contents = CONTENTS_CODE;
1770 }
1771#if 0
1772 /* NEW */
1773 if (sc.length) {
1774#endif
1775 sysroff_swap_sc_out (file, &sc);
1776 scount++;
1777#if 0
1778 }
1779#endif
1780 }
1781return scount;
1782}
1783
1784
1785/* Write out the ER records for a unit. */
1786static void
1787wr_er (ptr, sfile, first)
1788 struct coff_ofile *ptr;
0f371bb4 1789 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
252b5132
RH
1790 int first;
1791{
1792 int idx = 0;
1793 struct coff_symbol *sym;
1794 if (first)
1795 {
1796 for (sym = ptr->symbol_list_head; sym; sym = sym->next_in_ofile_list)
1797 {
1798 if (sym->visible->type == coff_vis_ext_ref)
1799 {
1800 struct IT_er er;
1801 er.spare = 0;
1802 er.type = ER_NOTSPEC;
1803 er.name = sym->name;
1804 sysroff_swap_er_out (file, &er);
1805 sym->er_number = idx++;
1806 }
1807 }
1808 }
1809}
1810
1811/* Write out the ED records for a unit. */
1812static void
1813wr_ed (ptr, sfile, first)
1814 struct coff_ofile *ptr;
0f371bb4 1815 struct coff_sfile *sfile ATTRIBUTE_UNUSED;
252b5132
RH
1816 int first;
1817{
1818 struct coff_symbol *s;
1819 if (first)
1820 {
1821 for (s = ptr->symbol_list_head; s; s = s->next_in_ofile_list)
1822 {
1823 if (s->visible->type == coff_vis_ext_def
1824 || s->visible->type == coff_vis_common)
1825 {
1826 struct IT_ed ed;
1827
1828 ed.section = s->where->section->number;
1829 ed.spare = 0;
1830 if (s->where->section->data)
1831 {
1832 ed.type = ED_TYPE_DATA;
1833 }
1834 else if (s->where->section->code & SEC_CODE)
1835 {
1836 ed.type = ED_TYPE_ENTRY;
1837 }
1838 else
1839 {
1840 ed.type = ED_TYPE_NOTSPEC;
1841 ed.type = ED_TYPE_DATA;
1842 }
1843 ed.address = s->where->offset - s->where->section->address;
1844 ed.name = s->name;
1845 sysroff_swap_ed_out (file, &ed);
1846 }
1847 }
1848 }
1849}
1850
1851static void
1852wr_unit_info (ptr)
1853 struct coff_ofile *ptr;
1854{
1855 struct coff_sfile *sfile;
1856 int first = 1;
1857 for (sfile = ptr->source_head;
1858 sfile;
1859 sfile = sfile->next)
1860 {
1861 long p1;
1862 long p2;
1863 int nsecs;
1864 p1 = ftell (file);
1865 wr_un (ptr, sfile, first, 0);
1866 nsecs = wr_sc (ptr, sfile);
1867 p2 = ftell (file);
1868 fseek (file, p1, SEEK_SET);
1869 wr_un (ptr, sfile, first, nsecs);
1870 fseek (file, p2, SEEK_SET);
1871 wr_er (ptr, sfile, first);
1872 wr_ed (ptr, sfile, first);
1873 first = 0;
1874 }
1875}
1876
1877static void
1878wr_module (p)
1879 struct coff_ofile *p;
1880{
1881 wr_cs ();
1882 wr_hd (p);
1883 wr_unit_info (p);
1884 wr_object_body (p);
1885 wr_debug (p);
1886 wr_tr ();
1887}
1888
1889static int
1890align (x)
1891 int x;
1892{
1893 return (x + 3) & ~3;
1894}
1895
1896/* Find all the common variables and turn them into
1897 ordinary defs - dunno why, but thats what hitachi does with 'em */
1898
1899static void
1900prescan (tree)
1901 struct coff_ofile *tree;
1902{
1903 struct coff_symbol *s;
1904 struct coff_section *common_section;
1905 /* Find the common section - always section 3 */
1906 common_section = tree->sections + 3;
1907 for (s = tree->symbol_list_head;
1908 s;
1909 s = s->next_in_ofile_list)
1910 {
1911 if (s->visible->type == coff_vis_common)
1912 {
1913 struct coff_where *w = s->where;
1914 /* s->visible->type = coff_vis_ext_def; leave it as common */
1915 common_section->size = align (common_section->size);
1916 w->offset = common_section->size + common_section->address;
1917 w->section = common_section;
1918 common_section->size += s->type->size;
1919 common_section->size = align (common_section->size);
1920 }
1921 }
1922}
1923
1924char *program_name;
1925
1926static void
1927show_usage (file, status)
1928 FILE *file;
1929 int status;
1930{
1931 fprintf (file, _("Usage: %s [-dhVq] in-file [out-file]\n"), program_name);
1932 exit (status);
1933}
1934
1935static void
1936show_help ()
1937{
1938 printf (_("%s: Convert a COFF object file into a SYSROFF object file\n"),
1939 program_name);
1940 show_usage (stdout, 0);
1941}
1942
1943
1944
1945int
1946main (ac, av)
1947 int ac;
1948 char *av[];
1949{
1950 int opt;
1951 static struct option long_options[] =
1952 {
1953 {"debug", no_argument, 0, 'd'},
1954 {"quick", no_argument, 0, 'q'},
1955 {"noprescan", no_argument, 0, 'n'},
1956 {"help", no_argument, 0, 'h'},
1957 {"version", no_argument, 0, 'V'},
1958 {NULL, no_argument, 0, 0}
1959 };
1960 char **matching;
1961 char *input_file;
1962 char *output_file;
1963
1964#if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
1965 setlocale (LC_MESSAGES, "");
3882b010
L
1966#endif
1967#if defined (HAVE_SETLOCALE)
1968 setlocale (LC_CTYPE, "");
252b5132
RH
1969#endif
1970 bindtextdomain (PACKAGE, LOCALEDIR);
1971 textdomain (PACKAGE);
1972
1973 program_name = av[0];
1974 xmalloc_set_program_name (program_name);
1975
1976 while ((opt = getopt_long (ac, av, "dhVqn", long_options,
1977 (int *) NULL))
1978 != EOF)
1979 {
1980 switch (opt)
1981 {
1982 case 'q':
1983 quick = 1;
1984 break;
1985 case 'n':
1986 noprescan = 1;
1987 break;
1988 case 'd':
1989 debug = 1;
1990 break;
1991 case 'h':
1992 show_help ();
1993 /*NOTREACHED */
1994 case 'V':
6a8c2b0d 1995 print_version ("srconv");
252b5132
RH
1996 exit (0);
1997 /*NOTREACHED */
1998 case 0:
1999 break;
2000 default:
2001 show_usage (stderr, 1);
2002 /*NOTREACHED */
2003 }
2004 }
2005
2006 /* The input and output files may be named on the command line. */
2007 output_file = NULL;
2008 if (optind < ac)
2009 {
2010 input_file = av[optind];
2011 ++optind;
2012 if (optind < ac)
2013 {
2014 output_file = av[optind];
2015 ++optind;
2016 if (optind < ac)
2017 show_usage (stderr, 1);
2018 if (strcmp (input_file, output_file) == 0)
2019 {
37cc8ec1 2020 fatal (_("input and output files must be different"));
252b5132
RH
2021 }
2022 }
2023 }
2024 else
2025 input_file = 0;
2026
2027 if (!input_file)
2028 {
37cc8ec1 2029 fatal (_("no input file specified"));
252b5132
RH
2030 }
2031
2032 if (!output_file)
2033 {
2034 /* Take a .o off the input file and stick on a .obj. If
2035 it doesn't end in .o, then stick a .obj on anyway */
2036
2037 int len = strlen (input_file);
2038 output_file = xmalloc (len + 5);
2039 strcpy (output_file, input_file);
2040 if (len > 3
2041 && output_file[len - 2] == '.'
2042 && output_file[len - 1] == 'o')
2043 {
2044 output_file[len] = 'b';
2045 output_file[len + 1] = 'j';
2046 output_file[len + 2] = 0;
2047 }
2048 else
2049 {
2050 strcat (output_file, ".obj");
2051 }
2052 }
2053
2054 abfd = bfd_openr (input_file, 0);
2055
2056 if (!abfd)
2057 bfd_fatal (input_file);
2058
2059 if (!bfd_check_format_matches (abfd, bfd_object, &matching))
2060 {
2061 bfd_nonfatal (input_file);
2062 if (bfd_get_error () == bfd_error_file_ambiguously_recognized)
2063 {
2064 list_matching_formats (matching);
2065 free (matching);
2066 }
2067 exit (1);
2068 }
2069
2070 file = fopen (output_file, FOPEN_WB);
2071
2072 if (!file)
2073 {
37cc8ec1 2074 fatal (_("unable to open output file %s"), output_file);
252b5132
RH
2075 }
2076
2077 if (debug)
2078 printf ("ids %d %d\n", base1, base2);
2079 tree = coff_grok (abfd);
2080 if (!noprescan)
2081 prescan (tree);
2082 wr_module (tree);
2083 return 0;
2084}
This page took 0.154636 seconds and 4 git commands to generate.