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