OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [insight/] [bfd/] [ieee.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 578 markom
/* BFD back-end for ieee-695 objects.
2
   Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3
   2000, 2001
4
   Free Software Foundation, Inc.
5
 
6
   Written by Steve Chamberlain of Cygnus Support.
7
 
8
This file is part of BFD, the Binary File Descriptor library.
9
 
10
This program is free software; you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation; either version 2 of the License, or
13
(at your option) any later version.
14
 
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
GNU General Public License for more details.
19
 
20
You should have received a copy of the GNU General Public License
21
along with this program; if not, write to the Free Software
22
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
23
 
24
#define KEEPMINUSPCININST 0
25
 
26
/* IEEE 695 format is a stream of records, which we parse using a simple one-
27
   token (which is one byte in this lexicon) lookahead recursive decent
28
   parser.  */
29
 
30
#include "bfd.h"
31
#include "sysdep.h"
32
#include "libbfd.h"
33
#include "ieee.h"
34
#include "libieee.h"
35
 
36
#include <ctype.h>
37
 
38
static boolean ieee_write_byte PARAMS ((bfd *, int));
39
static boolean ieee_write_2bytes PARAMS ((bfd *, int));
40
static boolean ieee_write_int PARAMS ((bfd *, bfd_vma));
41
static boolean ieee_write_id PARAMS ((bfd *, const char *));
42
static boolean ieee_write_expression
43
  PARAMS ((bfd *, bfd_vma, asymbol *, boolean, unsigned int));
44
static void ieee_write_int5 PARAMS ((bfd_byte *, bfd_vma));
45
static boolean ieee_write_int5_out PARAMS ((bfd *, bfd_vma));
46
static boolean ieee_write_section_part PARAMS ((bfd *));
47
static boolean do_with_relocs PARAMS ((bfd *, asection *));
48
static boolean do_as_repeat PARAMS ((bfd *, asection *));
49
static boolean do_without_relocs PARAMS ((bfd *, asection *));
50
static boolean ieee_write_external_part PARAMS ((bfd *));
51
static boolean ieee_write_data_part PARAMS ((bfd *));
52
static boolean ieee_write_debug_part PARAMS ((bfd *));
53
static boolean ieee_write_me_part PARAMS ((bfd *));
54
static boolean ieee_write_processor PARAMS ((bfd *));
55
 
56
static boolean ieee_slurp_debug PARAMS ((bfd *));
57
static boolean ieee_slurp_section_data PARAMS ((bfd *));
58
 
59
/* Functions for writing to ieee files in the strange way that the
60
   standard requires. */
61
 
62
static boolean
63
ieee_write_byte (abfd, barg)
64
     bfd *abfd;
65
     int barg;
66
{
67
  bfd_byte byte;
68
 
69
  byte = barg;
70
  if (bfd_write ((PTR) &byte, 1, 1, abfd) != 1)
71
    return false;
72
  return true;
73
}
74
 
75
static boolean
76
ieee_write_2bytes (abfd, bytes)
77
     bfd *abfd;
78
     int bytes;
79
{
80
  bfd_byte buffer[2];
81
 
82
  buffer[0] = bytes >> 8;
83
  buffer[1] = bytes & 0xff;
84
  if (bfd_write ((PTR) buffer, 1, 2, abfd) != 2)
85
    return false;
86
  return true;
87
}
88
 
89
static boolean
90
ieee_write_int (abfd, value)
91
     bfd *abfd;
92
     bfd_vma value;
93
{
94
  if (value <= 127)
95
    {
96
      if (! ieee_write_byte (abfd, (bfd_byte) value))
97
        return false;
98
    }
99
  else
100
    {
101
      unsigned int length;
102
 
103
      /* How many significant bytes ? */
104
      /* FIXME FOR LONGER INTS */
105
      if (value & 0xff000000)
106
        length = 4;
107
      else if (value & 0x00ff0000)
108
        length = 3;
109
      else if (value & 0x0000ff00)
110
        length = 2;
111
      else
112
        length = 1;
113
 
114
      if (! ieee_write_byte (abfd,
115
                             (bfd_byte) ((int) ieee_number_repeat_start_enum
116
                                         + length)))
117
        return false;
118
      switch (length)
119
        {
120
        case 4:
121
          if (! ieee_write_byte (abfd, (bfd_byte) (value >> 24)))
122
            return false;
123
          /* Fall through.  */
124
        case 3:
125
          if (! ieee_write_byte (abfd, (bfd_byte) (value >> 16)))
126
            return false;
127
          /* Fall through.  */
128
        case 2:
129
          if (! ieee_write_byte (abfd, (bfd_byte) (value >> 8)))
130
            return false;
131
          /* Fall through.  */
132
        case 1:
133
          if (! ieee_write_byte (abfd, (bfd_byte) (value)))
134
            return false;
135
        }
136
    }
137
 
138
  return true;
139
}
140
 
141
static boolean
142
ieee_write_id (abfd, id)
143
     bfd *abfd;
144
     const char *id;
145
{
146
  size_t length = strlen (id);
147
 
148
  if (length <= 127)
149
    {
150
      if (! ieee_write_byte (abfd, (bfd_byte) length))
151
        return false;
152
    }
153
  else if (length < 255)
154
    {
155
      if (! ieee_write_byte (abfd, ieee_extension_length_1_enum)
156
          || ! ieee_write_byte (abfd, (bfd_byte) length))
157
        return false;
158
    }
159
  else if (length < 65535)
160
    {
161
      if (! ieee_write_byte (abfd, ieee_extension_length_2_enum)
162
          || ! ieee_write_2bytes (abfd, (int) length))
163
        return false;
164
    }
165
  else
166
    {
167
      (*_bfd_error_handler)
168
        (_("%s: string too long (%d chars, max 65535)"),
169
         bfd_get_filename (abfd), length);
170
      bfd_set_error (bfd_error_invalid_operation);
171
      return false;
172
    }
173
 
174
  if (bfd_write ((PTR) id, 1, length, abfd) != length)
175
    return false;
176
  return true;
177
}
178
 
179
/***************************************************************************
180
Functions for reading from ieee files in the strange way that the
181
standard requires:
182
*/
183
 
184
#define this_byte(ieee) *((ieee)->input_p)
185
#define next_byte(ieee) ((ieee)->input_p++)
186
#define this_byte_and_next(ieee) (*((ieee)->input_p++))
187
 
188
static unsigned short
189
read_2bytes (ieee)
190
     common_header_type *ieee;
191
{
192
  unsigned char c1 = this_byte_and_next (ieee);
193
  unsigned char c2 = this_byte_and_next (ieee);
194
  return (c1 << 8) | c2;
195
}
196
 
197
static void
198
bfd_get_string (ieee, string, length)
199
     common_header_type *ieee;
200
     char *string;
201
     size_t length;
202
{
203
  size_t i;
204
  for (i = 0; i < length; i++)
205
    {
206
      string[i] = this_byte_and_next (ieee);
207
    }
208
}
209
 
210
static char *
211
read_id (ieee)
212
     common_header_type *ieee;
213
{
214
  size_t length;
215
  char *string;
216
  length = this_byte_and_next (ieee);
217
  if (length <= 0x7f)
218
    {
219
      /* Simple string of length 0 to 127 */
220
    }
221
  else if (length == 0xde)
222
    {
223
      /* Length is next byte, allowing 0..255 */
224
      length = this_byte_and_next (ieee);
225
    }
226
  else if (length == 0xdf)
227
    {
228
      /* Length is next two bytes, allowing 0..65535 */
229
      length = this_byte_and_next (ieee);
230
      length = (length * 256) + this_byte_and_next (ieee);
231
    }
232
  /* Buy memory and read string */
233
  string = bfd_alloc (ieee->abfd, length + 1);
234
  if (!string)
235
    return NULL;
236
  bfd_get_string (ieee, string, length);
237
  string[length] = 0;
238
  return string;
239
}
240
 
241
static boolean
242
ieee_write_expression (abfd, value, symbol, pcrel, index)
243
     bfd *abfd;
244
     bfd_vma value;
245
     asymbol *symbol;
246
     boolean pcrel;
247
     unsigned int index;
248
{
249
  unsigned int term_count = 0;
250
 
251
  if (value != 0)
252
    {
253
      if (! ieee_write_int (abfd, value))
254
        return false;
255
      term_count++;
256
    }
257
 
258
  if (bfd_is_com_section (symbol->section)
259
      || bfd_is_und_section (symbol->section))
260
    {
261
      /* Def of a common symbol */
262
      if (! ieee_write_byte (abfd, ieee_variable_X_enum)
263
          || ! ieee_write_int (abfd, symbol->value))
264
        return false;
265
      term_count++;
266
    }
267
  else if (! bfd_is_abs_section (symbol->section))
268
    {
269
      /* Ref to defined symbol - */
270
 
271
      if (symbol->flags & BSF_GLOBAL)
272
        {
273
          if (! ieee_write_byte (abfd, ieee_variable_I_enum)
274
              || ! ieee_write_int (abfd, symbol->value))
275
            return false;
276
          term_count++;
277
        }
278
      else if (symbol->flags & (BSF_LOCAL | BSF_SECTION_SYM))
279
        {
280
          /* This is a reference to a defined local symbol.  We can
281
             easily do a local as a section+offset.  */
282
          if (! ieee_write_byte (abfd, ieee_variable_R_enum)
283
              || ! ieee_write_byte (abfd,
284
                                    (bfd_byte) (symbol->section->index
285
                                                + IEEE_SECTION_NUMBER_BASE)))
286
            return false;
287
          term_count++;
288
          if (symbol->value != 0)
289
            {
290
              if (! ieee_write_int (abfd, symbol->value))
291
                return false;
292
              term_count++;
293
            }
294
        }
295
      else
296
        {
297
          (*_bfd_error_handler)
298
            (_("%s: unrecognized symbol `%s' flags 0x%x"),
299
             bfd_get_filename (abfd), bfd_asymbol_name (symbol),
300
             symbol->flags);
301
          bfd_set_error (bfd_error_invalid_operation);
302
          return false;
303
        }
304
    }
305
 
306
  if (pcrel)
307
    {
308
      /* subtract the pc from here by asking for PC of this section*/
309
      if (! ieee_write_byte (abfd, ieee_variable_P_enum)
310
          || ! ieee_write_byte (abfd,
311
                                (bfd_byte) (index + IEEE_SECTION_NUMBER_BASE))
312
          || ! ieee_write_byte (abfd, ieee_function_minus_enum))
313
        return false;
314
    }
315
 
316
  /* Handle the degenerate case of a 0 address.  */
317
  if (term_count == 0)
318
    {
319
      if (! ieee_write_int (abfd, 0))
320
        return false;
321
    }
322
 
323
  while (term_count > 1)
324
    {
325
      if (! ieee_write_byte (abfd, ieee_function_plus_enum))
326
        return false;
327
      term_count--;
328
    }
329
 
330
  return true;
331
}
332
 
333
/*****************************************************************************/
334
 
335
/*
336
writes any integer into the buffer supplied and always takes 5 bytes
337
*/
338
static void
339
ieee_write_int5 (buffer, value)
340
     bfd_byte *buffer;
341
     bfd_vma value;
342
{
343
  buffer[0] = (bfd_byte) ieee_number_repeat_4_enum;
344
  buffer[1] = (value >> 24) & 0xff;
345
  buffer[2] = (value >> 16) & 0xff;
346
  buffer[3] = (value >> 8) & 0xff;
347
  buffer[4] = (value >> 0) & 0xff;
348
}
349
 
350
static boolean
351
ieee_write_int5_out (abfd, value)
352
     bfd *abfd;
353
     bfd_vma value;
354
{
355
  bfd_byte b[5];
356
 
357
  ieee_write_int5 (b, value);
358
  if (bfd_write ((PTR) b, 1, 5, abfd) != 5)
359
    return false;
360
  return true;
361
}
362
 
363
static boolean
364
parse_int (ieee, value_ptr)
365
     common_header_type *ieee;
366
     bfd_vma *value_ptr;
367
{
368
  int value = this_byte (ieee);
369
  int result;
370
  if (value >= 0 && value <= 127)
371
    {
372
      *value_ptr = value;
373
      next_byte (ieee);
374
      return true;
375
    }
376
  else if (value >= 0x80 && value <= 0x88)
377
    {
378
      unsigned int count = value & 0xf;
379
      result = 0;
380
      next_byte (ieee);
381
      while (count)
382
        {
383
          result = (result << 8) | this_byte_and_next (ieee);
384
          count--;
385
        }
386
      *value_ptr = result;
387
      return true;
388
    }
389
  return false;
390
}
391
 
392
static int
393
parse_i (ieee, ok)
394
     common_header_type *ieee;
395
     boolean *ok;
396
{
397
  bfd_vma x;
398
  *ok = parse_int (ieee, &x);
399
  return x;
400
}
401
 
402
static bfd_vma
403
must_parse_int (ieee)
404
     common_header_type *ieee;
405
{
406
  bfd_vma result;
407
  BFD_ASSERT (parse_int (ieee, &result) == true);
408
  return result;
409
}
410
 
411
typedef struct
412
{
413
  bfd_vma value;
414
  asection *section;
415
  ieee_symbol_index_type symbol;
416
} ieee_value_type;
417
 
418
 
419
#if KEEPMINUSPCININST
420
 
421
#define SRC_MASK(arg) arg
422
#define PCREL_OFFSET false
423
 
424
#else
425
 
426
#define SRC_MASK(arg) 0
427
#define PCREL_OFFSET true
428
 
429
#endif
430
 
431
static reloc_howto_type abs32_howto =
432
  HOWTO (1,
433
         0,
434
         2,
435
         32,
436
         false,
437
         0,
438
         complain_overflow_bitfield,
439
         0,
440
         "abs32",
441
         true,
442
         0xffffffff,
443
         0xffffffff,
444
         false);
445
 
446
static reloc_howto_type abs16_howto =
447
  HOWTO (1,
448
         0,
449
         1,
450
         16,
451
         false,
452
         0,
453
         complain_overflow_bitfield,
454
         0,
455
         "abs16",
456
         true,
457
         0x0000ffff,
458
         0x0000ffff,
459
         false);
460
 
461
static reloc_howto_type abs8_howto =
462
  HOWTO (1,
463
         0,
464
         0,
465
         8,
466
         false,
467
         0,
468
         complain_overflow_bitfield,
469
         0,
470
         "abs8",
471
         true,
472
         0x000000ff,
473
         0x000000ff,
474
         false);
475
 
476
static reloc_howto_type rel32_howto =
477
  HOWTO (1,
478
         0,
479
         2,
480
         32,
481
         true,
482
         0,
483
         complain_overflow_signed,
484
         0,
485
         "rel32",
486
         true,
487
         SRC_MASK (0xffffffff),
488
         0xffffffff,
489
         PCREL_OFFSET);
490
 
491
static reloc_howto_type rel16_howto =
492
  HOWTO (1,
493
         0,
494
         1,
495
         16,
496
         true,
497
         0,
498
         complain_overflow_signed,
499
         0,
500
         "rel16",
501
         true,
502
         SRC_MASK (0x0000ffff),
503
         0x0000ffff,
504
         PCREL_OFFSET);
505
 
506
static reloc_howto_type rel8_howto =
507
  HOWTO (1,
508
         0,
509
         0,
510
         8,
511
         true,
512
         0,
513
         complain_overflow_signed,
514
         0,
515
         "rel8",
516
         true,
517
         SRC_MASK (0x000000ff),
518
         0x000000ff,
519
         PCREL_OFFSET);
520
 
521
static ieee_symbol_index_type NOSYMBOL = {0, 0};
522
 
523
static void
524
parse_expression (ieee, value, symbol, pcrel, extra, section)
525
     ieee_data_type *ieee;
526
     bfd_vma *value;
527
     ieee_symbol_index_type *symbol;
528
     boolean *pcrel;
529
     unsigned int *extra;
530
     asection **section;
531
 
532
{
533
#define POS sp[1]
534
#define TOS sp[0]
535
#define NOS sp[-1]
536
#define INC sp++;
537
#define DEC sp--;
538
 
539
  boolean loop = true;
540
  ieee_value_type stack[10];
541
 
542
  /* The stack pointer always points to the next unused location */
543
#define PUSH(x,y,z) TOS.symbol=x;TOS.section=y;TOS.value=z;INC;
544
#define POP(x,y,z) DEC;x=TOS.symbol;y=TOS.section;z=TOS.value;
545
  ieee_value_type *sp = stack;
546
 
547
  while (loop)
548
    {
549
      switch (this_byte (&(ieee->h)))
550
        {
551
        case ieee_variable_P_enum:
552
          /* P variable, current program counter for section n */
553
          {
554
            int section_n;
555
            next_byte (&(ieee->h));
556
            *pcrel = true;
557
            section_n = must_parse_int (&(ieee->h));
558
            PUSH (NOSYMBOL, bfd_abs_section_ptr, 0);
559
            break;
560
          }
561
        case ieee_variable_L_enum:
562
          /* L variable  address of section N */
563
          next_byte (&(ieee->h));
564
          PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
565
          break;
566
        case ieee_variable_R_enum:
567
          /* R variable, logical address of section module */
568
          /* FIXME, this should be different to L */
569
          next_byte (&(ieee->h));
570
          PUSH (NOSYMBOL, ieee->section_table[must_parse_int (&(ieee->h))], 0);
571
          break;
572
        case ieee_variable_S_enum:
573
          /* S variable, size in MAUS of section module */
574
          next_byte (&(ieee->h));
575
          PUSH (NOSYMBOL,
576
                0,
577
                ieee->section_table[must_parse_int (&(ieee->h))]->_raw_size);
578
          break;
579
        case ieee_variable_I_enum:
580
          /* Push the address of variable n */
581
          {
582
            ieee_symbol_index_type sy;
583
            next_byte (&(ieee->h));
584
            sy.index = (int) must_parse_int (&(ieee->h));
585
            sy.letter = 'I';
586
 
587
            PUSH (sy, bfd_abs_section_ptr, 0);
588
          }
589
          break;
590
        case ieee_variable_X_enum:
591
          /* Push the address of external variable n */
592
          {
593
            ieee_symbol_index_type sy;
594
            next_byte (&(ieee->h));
595
            sy.index = (int) (must_parse_int (&(ieee->h)));
596
            sy.letter = 'X';
597
 
598
            PUSH (sy, bfd_und_section_ptr, 0);
599
          }
600
          break;
601
        case ieee_function_minus_enum:
602
          {
603
            bfd_vma value1, value2;
604
            asection *section1, *section_dummy;
605
            ieee_symbol_index_type sy;
606
            next_byte (&(ieee->h));
607
 
608
            POP (sy, section1, value1);
609
            POP (sy, section_dummy, value2);
610
            PUSH (sy, section1 ? section1 : section_dummy, value2 - value1);
611
          }
612
          break;
613
        case ieee_function_plus_enum:
614
          {
615
            bfd_vma value1, value2;
616
            asection *section1;
617
            asection *section2;
618
            ieee_symbol_index_type sy1;
619
            ieee_symbol_index_type sy2;
620
            next_byte (&(ieee->h));
621
 
622
            POP (sy1, section1, value1);
623
            POP (sy2, section2, value2);
624
            PUSH (sy1.letter ? sy1 : sy2,
625
                  bfd_is_abs_section (section1) ? section2 : section1,
626
                  value1 + value2);
627
          }
628
          break;
629
        default:
630
          {
631
            bfd_vma va;
632
            BFD_ASSERT (this_byte (&(ieee->h)) < (int) ieee_variable_A_enum
633
                    || this_byte (&(ieee->h)) > (int) ieee_variable_Z_enum);
634
            if (parse_int (&(ieee->h), &va))
635
              {
636
                PUSH (NOSYMBOL, bfd_abs_section_ptr, va);
637
              }
638
            else
639
              {
640
                /*
641
                  Thats all that we can understand. As far as I can see
642
                  there is a bug in the Microtec IEEE output which I'm
643
                  using to scan, whereby the comma operator is omitted
644
                  sometimes in an expression, giving expressions with too
645
                  many terms. We can tell if that's the case by ensuring
646
                  that sp == stack here. If not, then we've pushed
647
                  something too far, so we keep adding.  */
648
 
649
                while (sp != stack + 1)
650
                  {
651
                    asection *section1;
652
                    ieee_symbol_index_type sy1;
653
                    POP (sy1, section1, *extra);
654
                  }
655
                {
656
                  asection *dummy;
657
 
658
                  POP (*symbol, dummy, *value);
659
                  if (section)
660
                    *section = dummy;
661
                }
662
 
663
                loop = false;
664
              }
665
          }
666
        }
667
    }
668
}
669
 
670
 
671
#define ieee_seek(abfd, offset) \
672
  IEEE_DATA(abfd)->h.input_p = IEEE_DATA(abfd)->h.first_byte + offset
673
 
674
#define ieee_pos(abfd) \
675
  (IEEE_DATA(abfd)->h.input_p - IEEE_DATA(abfd)->h.first_byte)
676
 
677
static unsigned int last_index;
678
static char last_type;          /* is the index for an X or a D */
679
 
680
static ieee_symbol_type *
681
get_symbol (abfd,
682
            ieee,
683
            last_symbol,
684
            symbol_count,
685
            pptr,
686
            max_index,
687
            this_type
688
)
689
     bfd *abfd ATTRIBUTE_UNUSED;
690
     ieee_data_type *ieee;
691
     ieee_symbol_type *last_symbol;
692
     unsigned int *symbol_count;
693
     ieee_symbol_type ***pptr;
694
     unsigned int *max_index;
695
     char this_type
696
      ;
697
{
698
  /* Need a new symbol */
699
  unsigned int new_index = must_parse_int (&(ieee->h));
700
  if (new_index != last_index || this_type != last_type)
701
    {
702
      ieee_symbol_type *new_symbol = (ieee_symbol_type *) bfd_alloc (ieee->h.abfd,
703
                                                 sizeof (ieee_symbol_type));
704
      if (!new_symbol)
705
        return NULL;
706
 
707
      new_symbol->index = new_index;
708
      last_index = new_index;
709
      (*symbol_count)++;
710
      **pptr = new_symbol;
711
      *pptr = &new_symbol->next;
712
      if (new_index > *max_index)
713
        {
714
          *max_index = new_index;
715
        }
716
      last_type = this_type;
717
      new_symbol->symbol.section = bfd_abs_section_ptr;
718
      return new_symbol;
719
    }
720
  return last_symbol;
721
}
722
 
723
static boolean
724
ieee_slurp_external_symbols (abfd)
725
     bfd *abfd;
726
{
727
  ieee_data_type *ieee = IEEE_DATA (abfd);
728
  file_ptr offset = ieee->w.r.external_part;
729
 
730
  ieee_symbol_type **prev_symbols_ptr = &ieee->external_symbols;
731
  ieee_symbol_type **prev_reference_ptr = &ieee->external_reference;
732
  ieee_symbol_type *symbol = (ieee_symbol_type *) NULL;
733
  unsigned int symbol_count = 0;
734
  boolean loop = true;
735
  last_index = 0xffffff;
736
  ieee->symbol_table_full = true;
737
 
738
  ieee_seek (abfd, offset);
739
 
740
  while (loop)
741
    {
742
      switch (this_byte (&(ieee->h)))
743
        {
744
        case ieee_nn_record:
745
          next_byte (&(ieee->h));
746
 
747
          symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
748
                               &prev_symbols_ptr,
749
                               &ieee->external_symbol_max_index, 'I');
750
          if (symbol == NULL)
751
            return false;
752
 
753
          symbol->symbol.the_bfd = abfd;
754
          symbol->symbol.name = read_id (&(ieee->h));
755
          symbol->symbol.udata.p = (PTR) NULL;
756
          symbol->symbol.flags = BSF_NO_FLAGS;
757
          break;
758
        case ieee_external_symbol_enum:
759
          next_byte (&(ieee->h));
760
 
761
          symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
762
                               &prev_symbols_ptr,
763
                               &ieee->external_symbol_max_index, 'D');
764
          if (symbol == NULL)
765
            return false;
766
 
767
          BFD_ASSERT (symbol->index >= ieee->external_symbol_min_index);
768
 
769
          symbol->symbol.the_bfd = abfd;
770
          symbol->symbol.name = read_id (&(ieee->h));
771
          symbol->symbol.udata.p = (PTR) NULL;
772
          symbol->symbol.flags = BSF_NO_FLAGS;
773
          break;
774
        case ieee_attribute_record_enum >> 8:
775
          {
776
            unsigned int symbol_name_index;
777
            unsigned int symbol_type_index;
778
            unsigned int symbol_attribute_def;
779
            bfd_vma value;
780
            switch (read_2bytes (ieee))
781
              {
782
              case ieee_attribute_record_enum:
783
                symbol_name_index = must_parse_int (&(ieee->h));
784
                symbol_type_index = must_parse_int (&(ieee->h));
785
                symbol_attribute_def = must_parse_int (&(ieee->h));
786
                switch (symbol_attribute_def)
787
                  {
788
                  case 8:
789
                  case 19:
790
                    parse_int (&ieee->h, &value);
791
                    break;
792
                  default:
793
                    (*_bfd_error_handler)
794
                      (_("%s: unimplemented ATI record  %u for symbol %u"),
795
                       bfd_get_filename (abfd), symbol_attribute_def,
796
                       symbol_name_index);
797
                    bfd_set_error (bfd_error_bad_value);
798
                    return false;
799
                    break;
800
                  }
801
                break;
802
              case ieee_external_reference_info_record_enum:
803
                /* Skip over ATX record. */
804
                parse_int (&(ieee->h), &value);
805
                parse_int (&(ieee->h), &value);
806
                parse_int (&(ieee->h), &value);
807
                parse_int (&(ieee->h), &value);
808
                break;
809
              case ieee_atn_record_enum:
810
                /* We may get call optimization information here,
811
                   which we just ignore.  The format is
812
                   {$F1}${CE}{index}{$00}{$3F}{$3F}{#_of_ASNs} */
813
                parse_int (&ieee->h, &value);
814
                parse_int (&ieee->h, &value);
815
                parse_int (&ieee->h, &value);
816
                if (value != 0x3f)
817
                  {
818
                    (*_bfd_error_handler)
819
                      (_("%s: unexpected ATN type %d in external part"),
820
                         bfd_get_filename (abfd), (int) value);
821
                    bfd_set_error (bfd_error_bad_value);
822
                    return false;
823
                  }
824
                parse_int (&ieee->h, &value);
825
                parse_int (&ieee->h, &value);
826
                while (value > 0)
827
                  {
828
                    bfd_vma val1;
829
 
830
                    --value;
831
 
832
                    switch (read_2bytes (ieee))
833
                      {
834
                      case ieee_asn_record_enum:
835
                        parse_int (&ieee->h, &val1);
836
                        parse_int (&ieee->h, &val1);
837
                        break;
838
 
839
                      default:
840
                        (*_bfd_error_handler)
841
                          (_("%s: unexpected type after ATN"),
842
                             bfd_get_filename (abfd));
843
                        bfd_set_error (bfd_error_bad_value);
844
                        return false;
845
                      }
846
                  }
847
              }
848
          }
849
          break;
850
        case ieee_value_record_enum >> 8:
851
          {
852
            unsigned int symbol_name_index;
853
            ieee_symbol_index_type symbol_ignore;
854
            boolean pcrel_ignore;
855
            unsigned int extra;
856
            next_byte (&(ieee->h));
857
            next_byte (&(ieee->h));
858
 
859
            symbol_name_index = must_parse_int (&(ieee->h));
860
            parse_expression (ieee,
861
                              &symbol->symbol.value,
862
                              &symbol_ignore,
863
                              &pcrel_ignore,
864
                              &extra,
865
                              &symbol->symbol.section);
866
 
867
            /* Fully linked IEEE-695 files tend to give every symbol
868
               an absolute value.  Try to convert that back into a
869
               section relative value.  FIXME: This won't always to
870
               the right thing.  */
871
            if (bfd_is_abs_section (symbol->symbol.section)
872
                && (abfd->flags & HAS_RELOC) == 0)
873
              {
874
                bfd_vma val;
875
                asection *s;
876
 
877
                val = symbol->symbol.value;
878
                for (s = abfd->sections; s != NULL; s = s->next)
879
                  {
880
                    if (val >= s->vma && val < s->vma + s->_raw_size)
881
                      {
882
                        symbol->symbol.section = s;
883
                        symbol->symbol.value -= s->vma;
884
                        break;
885
                      }
886
                  }
887
              }
888
 
889
            symbol->symbol.flags = BSF_GLOBAL | BSF_EXPORT;
890
 
891
          }
892
          break;
893
        case ieee_weak_external_reference_enum:
894
          {
895
            bfd_vma size;
896
            bfd_vma value;
897
            next_byte (&(ieee->h));
898
            /* Throw away the external reference index */
899
            (void) must_parse_int (&(ieee->h));
900
            /* Fetch the default size if not resolved */
901
            size = must_parse_int (&(ieee->h));
902
            /* Fetch the defautlt value if available */
903
            if (parse_int (&(ieee->h), &value) == false)
904
              {
905
                value = 0;
906
              }
907
            /* This turns into a common */
908
            symbol->symbol.section = bfd_com_section_ptr;
909
            symbol->symbol.value = size;
910
          }
911
          break;
912
 
913
        case ieee_external_reference_enum:
914
          next_byte (&(ieee->h));
915
 
916
          symbol = get_symbol (abfd, ieee, symbol, &symbol_count,
917
                               &prev_reference_ptr,
918
                               &ieee->external_reference_max_index, 'X');
919
          if (symbol == NULL)
920
            return false;
921
 
922
          symbol->symbol.the_bfd = abfd;
923
          symbol->symbol.name = read_id (&(ieee->h));
924
          symbol->symbol.udata.p = (PTR) NULL;
925
          symbol->symbol.section = bfd_und_section_ptr;
926
          symbol->symbol.value = (bfd_vma) 0;
927
          symbol->symbol.flags = 0;
928
 
929
          BFD_ASSERT (symbol->index >= ieee->external_reference_min_index);
930
          break;
931
 
932
        default:
933
          loop = false;
934
        }
935
    }
936
 
937
  if (ieee->external_symbol_max_index != 0)
938
    {
939
      ieee->external_symbol_count =
940
        ieee->external_symbol_max_index -
941
        ieee->external_symbol_min_index + 1;
942
    }
943
  else
944
    {
945
      ieee->external_symbol_count = 0;
946
    }
947
 
948
  if (ieee->external_reference_max_index != 0)
949
    {
950
      ieee->external_reference_count =
951
        ieee->external_reference_max_index -
952
        ieee->external_reference_min_index + 1;
953
    }
954
  else
955
    {
956
      ieee->external_reference_count = 0;
957
    }
958
 
959
  abfd->symcount =
960
    ieee->external_reference_count + ieee->external_symbol_count;
961
 
962
  if (symbol_count != abfd->symcount)
963
    {
964
      /* There are gaps in the table -- */
965
      ieee->symbol_table_full = false;
966
    }
967
 
968
  *prev_symbols_ptr = (ieee_symbol_type *) NULL;
969
  *prev_reference_ptr = (ieee_symbol_type *) NULL;
970
 
971
  return true;
972
}
973
 
974
static boolean
975
ieee_slurp_symbol_table (abfd)
976
     bfd *abfd;
977
{
978
  if (IEEE_DATA (abfd)->read_symbols == false)
979
    {
980
      if (! ieee_slurp_external_symbols (abfd))
981
        return false;
982
      IEEE_DATA (abfd)->read_symbols = true;
983
    }
984
  return true;
985
}
986
 
987
long
988
ieee_get_symtab_upper_bound (abfd)
989
     bfd *abfd;
990
{
991
  if (! ieee_slurp_symbol_table (abfd))
992
    return -1;
993
 
994
  return (abfd->symcount != 0) ?
995
    (abfd->symcount + 1) * (sizeof (ieee_symbol_type *)) : 0;
996
}
997
 
998
/*
999
Move from our internal lists to the canon table, and insert in
1000
symbol index order
1001
*/
1002
 
1003
extern const bfd_target ieee_vec;
1004
 
1005
long
1006
ieee_get_symtab (abfd, location)
1007
     bfd *abfd;
1008
     asymbol **location;
1009
{
1010
  ieee_symbol_type *symp;
1011
  static bfd dummy_bfd;
1012
  static asymbol empty_symbol =
1013
  {
1014
    &dummy_bfd,
1015
    " ieee empty",
1016
    (symvalue) 0,
1017
    BSF_DEBUGGING,
1018
    bfd_abs_section_ptr
1019
#ifdef __STDC__
1020
    /* K&R compilers can't initialise unions.  */
1021
    , { 0 }
1022
#endif
1023
  };
1024
 
1025
  if (abfd->symcount)
1026
    {
1027
      ieee_data_type *ieee = IEEE_DATA (abfd);
1028
      dummy_bfd.xvec = &ieee_vec;
1029
      if (! ieee_slurp_symbol_table (abfd))
1030
        return -1;
1031
 
1032
      if (ieee->symbol_table_full == false)
1033
        {
1034
          /* Arrgh - there are gaps in the table, run through and fill them */
1035
          /* up with pointers to a null place */
1036
          unsigned int i;
1037
          for (i = 0; i < abfd->symcount; i++)
1038
            {
1039
              location[i] = &empty_symbol;
1040
            }
1041
        }
1042
 
1043
      ieee->external_symbol_base_offset = -ieee->external_symbol_min_index;
1044
      for (symp = IEEE_DATA (abfd)->external_symbols;
1045
           symp != (ieee_symbol_type *) NULL;
1046
           symp = symp->next)
1047
        {
1048
          /* Place into table at correct index locations */
1049
          location[symp->index + ieee->external_symbol_base_offset] = &symp->symbol;
1050
        }
1051
 
1052
      /* The external refs are indexed in a bit */
1053
      ieee->external_reference_base_offset =
1054
        -ieee->external_reference_min_index + ieee->external_symbol_count;
1055
 
1056
      for (symp = IEEE_DATA (abfd)->external_reference;
1057
           symp != (ieee_symbol_type *) NULL;
1058
           symp = symp->next)
1059
        {
1060
          location[symp->index + ieee->external_reference_base_offset] =
1061
            &symp->symbol;
1062
 
1063
        }
1064
    }
1065
  if (abfd->symcount)
1066
    {
1067
      location[abfd->symcount] = (asymbol *) NULL;
1068
    }
1069
  return abfd->symcount;
1070
}
1071
 
1072
static asection *
1073
get_section_entry (abfd, ieee, index)
1074
     bfd *abfd;
1075
     ieee_data_type *ieee;
1076
     unsigned int index;
1077
{
1078
  if (index >= ieee->section_table_size)
1079
    {
1080
      unsigned int c, i;
1081
      asection **n;
1082
 
1083
      c = ieee->section_table_size;
1084
      if (c == 0)
1085
        c = 20;
1086
      while (c <= index)
1087
        c *= 2;
1088
 
1089
      n = ((asection **)
1090
           bfd_realloc (ieee->section_table, c * sizeof (asection *)));
1091
      if (n == NULL)
1092
        return NULL;
1093
 
1094
      for (i = ieee->section_table_size; i < c; i++)
1095
        n[i] = NULL;
1096
 
1097
      ieee->section_table = n;
1098
      ieee->section_table_size = c;
1099
    }
1100
 
1101
  if (ieee->section_table[index] == (asection *) NULL)
1102
    {
1103
      char *tmp = bfd_alloc (abfd, 11);
1104
      asection *section;
1105
 
1106
      if (!tmp)
1107
        return NULL;
1108
      sprintf (tmp, " fsec%4d", index);
1109
      section = bfd_make_section (abfd, tmp);
1110
      ieee->section_table[index] = section;
1111
      section->flags = SEC_NO_FLAGS;
1112
      section->target_index = index;
1113
      ieee->section_table[index] = section;
1114
    }
1115
  return ieee->section_table[index];
1116
}
1117
 
1118
static void
1119
ieee_slurp_sections (abfd)
1120
     bfd *abfd;
1121
{
1122
  ieee_data_type *ieee = IEEE_DATA (abfd);
1123
  file_ptr offset = ieee->w.r.section_part;
1124
  asection *section = (asection *) NULL;
1125
  char *name;
1126
 
1127
  if (offset != 0)
1128
    {
1129
      bfd_byte section_type[3];
1130
      ieee_seek (abfd, offset);
1131
      while (true)
1132
        {
1133
          switch (this_byte (&(ieee->h)))
1134
            {
1135
            case ieee_section_type_enum:
1136
              {
1137
                unsigned int section_index;
1138
                next_byte (&(ieee->h));
1139
                section_index = must_parse_int (&(ieee->h));
1140
 
1141
                section = get_section_entry (abfd, ieee, section_index);
1142
 
1143
                section_type[0] = this_byte_and_next (&(ieee->h));
1144
 
1145
                /* Set minimal section attributes. Attributes are
1146
                   extended later, based on section contents. */
1147
 
1148
                switch (section_type[0])
1149
                  {
1150
                  case 0xC1:
1151
                    /* Normal attributes for absolute sections  */
1152
                    section_type[1] = this_byte (&(ieee->h));
1153
                    section->flags = SEC_ALLOC;
1154
                    switch (section_type[1])
1155
                      {
1156
                      case 0xD3:        /* AS Absolute section attributes */
1157
                        next_byte (&(ieee->h));
1158
                        section_type[2] = this_byte (&(ieee->h));
1159
                        switch (section_type[2])
1160
                          {
1161
                          case 0xD0:
1162
                            /* Normal code */
1163
                            next_byte (&(ieee->h));
1164
                            section->flags |= SEC_CODE;
1165
                            break;
1166
                          case 0xC4:
1167
                            /* Normal data */
1168
                            next_byte (&(ieee->h));
1169
                            section->flags |= SEC_DATA;
1170
                            break;
1171
                          case 0xD2:
1172
                            next_byte (&(ieee->h));
1173
                            /* Normal rom data */
1174
                            section->flags |= SEC_ROM | SEC_DATA;
1175
                            break;
1176
                          default:
1177
                            break;
1178
                          }
1179
                      }
1180
                    break;
1181
                  case 0xC3:    /* Named relocatable sections (type C) */
1182
                    section_type[1] = this_byte (&(ieee->h));
1183
                    section->flags = SEC_ALLOC;
1184
                    switch (section_type[1])
1185
                      {
1186
                      case 0xD0:        /* Normal code (CP) */
1187
                        next_byte (&(ieee->h));
1188
                        section->flags |= SEC_CODE;
1189
                        break;
1190
                      case 0xC4:        /* Normal data (CD) */
1191
                        next_byte (&(ieee->h));
1192
                        section->flags |= SEC_DATA;
1193
                        break;
1194
                      case 0xD2:        /* Normal rom data (CR) */
1195
                        next_byte (&(ieee->h));
1196
                        section->flags |= SEC_ROM | SEC_DATA;
1197
                        break;
1198
                      default:
1199
                        break;
1200
                      }
1201
                  }
1202
 
1203
                /* Read section name, use it if non empty. */
1204
                name = read_id (&ieee->h);
1205
                if (name[0])
1206
                  section->name = name;
1207
 
1208
                /* Skip these fields, which we don't care about */
1209
                {
1210
                  bfd_vma parent, brother, context;
1211
                  parse_int (&(ieee->h), &parent);
1212
                  parse_int (&(ieee->h), &brother);
1213
                  parse_int (&(ieee->h), &context);
1214
                }
1215
              }
1216
              break;
1217
            case ieee_section_alignment_enum:
1218
              {
1219
                unsigned int section_index;
1220
                bfd_vma value;
1221
                asection *section;
1222
                next_byte (&(ieee->h));
1223
                section_index = must_parse_int (&ieee->h);
1224
                section = get_section_entry (abfd, ieee, section_index);
1225
                if (section_index > ieee->section_count)
1226
                  {
1227
                    ieee->section_count = section_index;
1228
                  }
1229
                section->alignment_power =
1230
                  bfd_log2 (must_parse_int (&ieee->h));
1231
                (void) parse_int (&(ieee->h), &value);
1232
              }
1233
              break;
1234
            case ieee_e2_first_byte_enum:
1235
              {
1236
                ieee_record_enum_type t = (ieee_record_enum_type) (read_2bytes (&(ieee->h)));
1237
 
1238
                switch (t)
1239
                  {
1240
                  case ieee_section_size_enum:
1241
                    section = ieee->section_table[must_parse_int (&(ieee->h))];
1242
                    section->_raw_size = must_parse_int (&(ieee->h));
1243
                    break;
1244
                  case ieee_physical_region_size_enum:
1245
                    section = ieee->section_table[must_parse_int (&(ieee->h))];
1246
                    section->_raw_size = must_parse_int (&(ieee->h));
1247
                    break;
1248
                  case ieee_region_base_address_enum:
1249
                    section = ieee->section_table[must_parse_int (&(ieee->h))];
1250
                    section->vma = must_parse_int (&(ieee->h));
1251
                    section->lma = section->vma;
1252
                    break;
1253
                  case ieee_mau_size_enum:
1254
                    must_parse_int (&(ieee->h));
1255
                    must_parse_int (&(ieee->h));
1256
                    break;
1257
                  case ieee_m_value_enum:
1258
                    must_parse_int (&(ieee->h));
1259
                    must_parse_int (&(ieee->h));
1260
                    break;
1261
                  case ieee_section_base_address_enum:
1262
                    section = ieee->section_table[must_parse_int (&(ieee->h))];
1263
                    section->vma = must_parse_int (&(ieee->h));
1264
                    section->lma = section->vma;
1265
                    break;
1266
                  case ieee_section_offset_enum:
1267
                    (void) must_parse_int (&(ieee->h));
1268
                    (void) must_parse_int (&(ieee->h));
1269
                    break;
1270
                  default:
1271
                    return;
1272
                  }
1273
              }
1274
              break;
1275
            default:
1276
              return;
1277
            }
1278
        }
1279
    }
1280
}
1281
 
1282
/* Make a section for the debugging information, if any.  We don't try
1283
   to interpret the debugging information; we just point the section
1284
   at the area in the file so that program which understand can dig it
1285
   out.  */
1286
 
1287
static boolean
1288
ieee_slurp_debug (abfd)
1289
     bfd *abfd;
1290
{
1291
  ieee_data_type *ieee = IEEE_DATA (abfd);
1292
  asection *sec;
1293
  file_ptr debug_end;
1294
 
1295
  if (ieee->w.r.debug_information_part == 0)
1296
    return true;
1297
 
1298
  sec = bfd_make_section (abfd, ".debug");
1299
  if (sec == NULL)
1300
    return false;
1301
  sec->flags |= SEC_DEBUGGING | SEC_HAS_CONTENTS;
1302
  sec->filepos = ieee->w.r.debug_information_part;
1303
 
1304
  debug_end = ieee->w.r.data_part;
1305
  if (debug_end == 0)
1306
    debug_end = ieee->w.r.trailer_part;
1307
  if (debug_end == 0)
1308
    debug_end = ieee->w.r.me_record;
1309
  sec->_raw_size = debug_end - ieee->w.r.debug_information_part;
1310
 
1311
  return true;
1312
}
1313
 
1314
/***********************************************************************
1315
*  archive stuff
1316
*/
1317
 
1318
const bfd_target *
1319
ieee_archive_p (abfd)
1320
     bfd *abfd;
1321
{
1322
  char *library;
1323
  unsigned int i;
1324
  unsigned char buffer[512];
1325
  file_ptr buffer_offset = 0;
1326
  ieee_ar_data_type *save = abfd->tdata.ieee_ar_data;
1327
  ieee_ar_data_type *ieee;
1328
  unsigned int alc_elts;
1329
  ieee_ar_obstack_type *elts = NULL;
1330
 
1331
  abfd->tdata.ieee_ar_data =
1332
    (ieee_ar_data_type *) bfd_alloc (abfd, sizeof (ieee_ar_data_type));
1333
  if (!abfd->tdata.ieee_ar_data)
1334
    goto error_return;
1335
  ieee = IEEE_AR_DATA (abfd);
1336
 
1337
  /* FIXME: Check return value.  I'm not sure whether it needs to read
1338
     the entire buffer or not.  */
1339
  bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1340
 
1341
  ieee->h.first_byte = buffer;
1342
  ieee->h.input_p = buffer;
1343
 
1344
  ieee->h.abfd = abfd;
1345
 
1346
  if (this_byte (&(ieee->h)) != Module_Beginning)
1347
    goto got_wrong_format_error;
1348
 
1349
  next_byte (&(ieee->h));
1350
  library = read_id (&(ieee->h));
1351
  if (strcmp (library, "LIBRARY") != 0)
1352
    goto got_wrong_format_error;
1353
 
1354
  /* Throw away the filename.  */
1355
  read_id (&(ieee->h));
1356
 
1357
  ieee->element_count = 0;
1358
  ieee->element_index = 0;
1359
 
1360
  next_byte (&(ieee->h));       /* Drop the ad part.  */
1361
  must_parse_int (&(ieee->h));  /* And the two dummy numbers.  */
1362
  must_parse_int (&(ieee->h));
1363
 
1364
  alc_elts = 10;
1365
  elts = (ieee_ar_obstack_type *) bfd_malloc (alc_elts * sizeof *elts);
1366
  if (elts == NULL)
1367
    goto error_return;
1368
 
1369
  /* Read the index of the BB table.  */
1370
  while (1)
1371
    {
1372
      int rec;
1373
      ieee_ar_obstack_type *t;
1374
 
1375
      rec = read_2bytes (&(ieee->h));
1376
      if (rec != (int) ieee_assign_value_to_variable_enum)
1377
        break;
1378
 
1379
      if (ieee->element_count >= alc_elts)
1380
        {
1381
          ieee_ar_obstack_type *n;
1382
 
1383
          alc_elts *= 2;
1384
          n = ((ieee_ar_obstack_type *)
1385
               bfd_realloc (elts, alc_elts * sizeof *elts));
1386
          if (n == NULL)
1387
            goto error_return;
1388
          elts = n;
1389
        }
1390
 
1391
      t = &elts[ieee->element_count];
1392
      ieee->element_count++;
1393
 
1394
      must_parse_int (&(ieee->h));
1395
      t->file_offset = must_parse_int (&(ieee->h));
1396
      t->abfd = (bfd *) NULL;
1397
 
1398
      /* Make sure that we don't go over the end of the buffer.  */
1399
      if ((size_t) ieee_pos (abfd) > sizeof (buffer) / 2)
1400
        {
1401
          /* Past half way, reseek and reprime.  */
1402
          buffer_offset += ieee_pos (abfd);
1403
          if (bfd_seek (abfd, buffer_offset, SEEK_SET) != 0)
1404
            goto error_return;
1405
 
1406
          /* FIXME: Check return value.  I'm not sure whether it needs
1407
             to read the entire buffer or not.  */
1408
          bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1409
          ieee->h.first_byte = buffer;
1410
          ieee->h.input_p = buffer;
1411
        }
1412
    }
1413
 
1414
  ieee->elements = ((ieee_ar_obstack_type *)
1415
                    bfd_alloc (abfd,
1416
                               ieee->element_count * sizeof *ieee->elements));
1417
  if (ieee->elements == NULL)
1418
    goto error_return;
1419
 
1420
  memcpy (ieee->elements, elts,
1421
          ieee->element_count * sizeof *ieee->elements);
1422
  free (elts);
1423
  elts = NULL;
1424
 
1425
  /* Now scan the area again, and replace BB offsets with file offsets.  */
1426
  for (i = 2; i < ieee->element_count; i++)
1427
    {
1428
      if (bfd_seek (abfd, ieee->elements[i].file_offset, SEEK_SET) != 0)
1429
        goto error_return;
1430
 
1431
      /* FIXME: Check return value.  I'm not sure whether it needs to
1432
         read the entire buffer or not.  */
1433
      bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1434
      ieee->h.first_byte = buffer;
1435
      ieee->h.input_p = buffer;
1436
 
1437
      next_byte (&(ieee->h));           /* Drop F8.  */
1438
      next_byte (&(ieee->h));           /* Drop 14.  */
1439
      must_parse_int (&(ieee->h));      /* Drop size of block.  */
1440
 
1441
      if (must_parse_int (&(ieee->h)) != 0)
1442
        /* This object has been deleted.  */
1443
        ieee->elements[i].file_offset = 0;
1444
      else
1445
        ieee->elements[i].file_offset = must_parse_int (&(ieee->h));
1446
    }
1447
 
1448
  /*  abfd->has_armap = ;*/
1449
 
1450
  return abfd->xvec;
1451
 
1452
 got_wrong_format_error:
1453
  bfd_release (abfd, ieee);
1454
  abfd->tdata.ieee_ar_data = save;
1455
  bfd_set_error (bfd_error_wrong_format);
1456
 
1457
 error_return:
1458
  if (elts != NULL)
1459
    free (elts);
1460
 
1461
  return NULL;
1462
}
1463
 
1464
static boolean
1465
ieee_mkobject (abfd)
1466
     bfd *abfd;
1467
{
1468
  abfd->tdata.ieee_data = (ieee_data_type *) bfd_zalloc (abfd, sizeof (ieee_data_type));
1469
  return abfd->tdata.ieee_data ? true : false;
1470
}
1471
 
1472
const bfd_target *
1473
ieee_object_p (abfd)
1474
     bfd *abfd;
1475
{
1476
  char *processor;
1477
  unsigned int part;
1478
  ieee_data_type *ieee;
1479
  unsigned char buffer[300];
1480
  ieee_data_type *save = IEEE_DATA (abfd);
1481
 
1482
  abfd->tdata.ieee_data = 0;
1483
  ieee_mkobject (abfd);
1484
 
1485
  ieee = IEEE_DATA (abfd);
1486
  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1487
    goto fail;
1488
  /* Read the first few bytes in to see if it makes sense */
1489
  /* FIXME: Check return value.  I'm not sure whether it needs to read
1490
     the entire buffer or not.  */
1491
  bfd_read ((PTR) buffer, 1, sizeof (buffer), abfd);
1492
 
1493
  ieee->h.input_p = buffer;
1494
  if (this_byte_and_next (&(ieee->h)) != Module_Beginning)
1495
    goto got_wrong_format;
1496
 
1497
  ieee->read_symbols = false;
1498
  ieee->read_data = false;
1499
  ieee->section_count = 0;
1500
  ieee->external_symbol_max_index = 0;
1501
  ieee->external_symbol_min_index = IEEE_PUBLIC_BASE;
1502
  ieee->external_reference_min_index = IEEE_REFERENCE_BASE;
1503
  ieee->external_reference_max_index = 0;
1504
  ieee->h.abfd = abfd;
1505
  ieee->section_table = NULL;
1506
  ieee->section_table_size = 0;
1507
 
1508
  processor = ieee->mb.processor = read_id (&(ieee->h));
1509
  if (strcmp (processor, "LIBRARY") == 0)
1510
    goto got_wrong_format;
1511
  ieee->mb.module_name = read_id (&(ieee->h));
1512
  if (abfd->filename == (CONST char *) NULL)
1513
    {
1514
      abfd->filename = ieee->mb.module_name;
1515
    }
1516
  /* Determine the architecture and machine type of the object file.
1517
     */
1518
  {
1519
    const bfd_arch_info_type *arch;
1520
    char family[10];
1521
 
1522
    /* IEEE does not specify the format of the processor identificaton
1523
       string, so the compiler is free to put in it whatever it wants.
1524
       We try here to recognize different processors belonging to the
1525
       m68k family.  Code for other processors can be added here.  */
1526
    if ((processor[0] == '6') && (processor[1] == '8'))
1527
      {
1528
        if (processor[2] == '3')            /* 683xx integrated processors */
1529
          {
1530
            switch (processor[3])
1531
              {
1532
              case '0':                     /* 68302, 68306, 68307 */
1533
              case '2':                     /* 68322, 68328 */
1534
              case '5':                     /* 68356 */
1535
                strcpy (family, "68000");   /* MC68000-based controllers */
1536
                break;
1537
 
1538
              case '3':                     /* 68330, 68331, 68332, 68333,
1539
                                               68334, 68335, 68336, 68338 */
1540
              case '6':                     /* 68360 */
1541
              case '7':                     /* 68376 */
1542
                strcpy (family, "68332");   /* CPU32 and CPU32+ */
1543
                break;
1544
 
1545
              case '4':
1546
                if (processor[4] == '9')    /* 68349 */
1547
                  strcpy (family, "68030"); /* CPU030 */
1548
                else                        /* 68340, 68341 */
1549
                  strcpy (family, "68332"); /* CPU32 and CPU32+ */
1550
                break;
1551
 
1552
              default:                      /* Does not exist yet */
1553
                strcpy (family, "68332");   /* Guess it will be CPU32 */
1554
              }
1555
          }
1556
        else if (toupper (processor[3]) == 'F')   /* 68F333 */
1557
          strcpy (family, "68332");               /* CPU32 */
1558
        else if ((toupper (processor[3]) == 'C')  /* Embedded controllers */
1559
                 && ((toupper (processor[2]) == 'E')
1560
                     || (toupper (processor[2]) == 'H')
1561
                     || (toupper (processor[2]) == 'L')))
1562
          {
1563
            strcpy (family, "68");
1564
            strncat (family, processor + 4, 7);
1565
            family[9] = '\0';
1566
          }
1567
        else                             /* "Regular" processors */
1568
          {
1569
            strncpy (family, processor, 9);
1570
            family[9] = '\0';
1571
          }
1572
      }
1573
    else if ((strncmp (processor, "cpu32", 5) == 0) /* CPU32 and CPU32+ */
1574
             || (strncmp (processor, "CPU32", 5) == 0))
1575
      strcpy (family, "68332");
1576
    else
1577
      {
1578
        strncpy (family, processor, 9);
1579
        family[9] = '\0';
1580
      }
1581
 
1582
    arch = bfd_scan_arch (family);
1583
    if (arch == 0)
1584
      goto got_wrong_format;
1585
    abfd->arch_info = arch;
1586
  }
1587
 
1588
  if (this_byte (&(ieee->h)) != (int) ieee_address_descriptor_enum)
1589
    {
1590
      goto fail;
1591
    }
1592
  next_byte (&(ieee->h));
1593
 
1594
  if (parse_int (&(ieee->h), &ieee->ad.number_of_bits_mau) == false)
1595
    {
1596
      goto fail;
1597
    }
1598
  if (parse_int (&(ieee->h), &ieee->ad.number_of_maus_in_address) == false)
1599
    {
1600
      goto fail;
1601
    }
1602
 
1603
  /* If there is a byte order info, take it */
1604
  if (this_byte (&(ieee->h)) == (int) ieee_variable_L_enum ||
1605
      this_byte (&(ieee->h)) == (int) ieee_variable_M_enum)
1606
    next_byte (&(ieee->h));
1607
 
1608
  for (part = 0; part < N_W_VARIABLES; part++)
1609
    {
1610
      boolean ok;
1611
      if (read_2bytes (&(ieee->h)) != (int) ieee_assign_value_to_variable_enum)
1612
        {
1613
          goto fail;
1614
        }
1615
      if (this_byte_and_next (&(ieee->h)) != part)
1616
        {
1617
          goto fail;
1618
        }
1619
 
1620
      ieee->w.offset[part] = parse_i (&(ieee->h), &ok);
1621
      if (ok == false)
1622
        {
1623
          goto fail;
1624
        }
1625
 
1626
    }
1627
 
1628
  if (ieee->w.r.external_part != 0)
1629
    abfd->flags = HAS_SYMS;
1630
 
1631
  /* By now we know that this is a real IEEE file, we're going to read
1632
     the whole thing into memory so that we can run up and down it
1633
     quickly.  We can work out how big the file is from the trailer
1634
     record */
1635
 
1636
  IEEE_DATA (abfd)->h.first_byte =
1637
    (unsigned char *) bfd_alloc (ieee->h.abfd, ieee->w.r.me_record + 1);
1638
  if (!IEEE_DATA (abfd)->h.first_byte)
1639
    goto fail;
1640
  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
1641
    goto fail;
1642
  /* FIXME: Check return value.  I'm not sure whether it needs to read
1643
     the entire buffer or not.  */
1644
  bfd_read ((PTR) (IEEE_DATA (abfd)->h.first_byte), 1,
1645
            ieee->w.r.me_record + 1, abfd);
1646
 
1647
  ieee_slurp_sections (abfd);
1648
 
1649
  if (! ieee_slurp_debug (abfd))
1650
    goto fail;
1651
 
1652
  /* Parse section data to activate file and section flags implied by
1653
     section contents. */
1654
 
1655
  if (! ieee_slurp_section_data (abfd))
1656
    goto fail;
1657
 
1658
  return abfd->xvec;
1659
got_wrong_format:
1660
  bfd_set_error (bfd_error_wrong_format);
1661
fail:
1662
  (void) bfd_release (abfd, ieee);
1663
  abfd->tdata.ieee_data = save;
1664
  return (const bfd_target *) NULL;
1665
}
1666
 
1667
void
1668
ieee_get_symbol_info (ignore_abfd, symbol, ret)
1669
     bfd *ignore_abfd ATTRIBUTE_UNUSED;
1670
     asymbol *symbol;
1671
     symbol_info *ret;
1672
{
1673
  bfd_symbol_info (symbol, ret);
1674
  if (symbol->name[0] == ' ')
1675
    ret->name = "* empty table entry ";
1676
  if (!symbol->section)
1677
    ret->type = (symbol->flags & BSF_LOCAL) ? 'a' : 'A';
1678
}
1679
 
1680
void
1681
ieee_print_symbol (ignore_abfd, afile, symbol, how)
1682
     bfd *ignore_abfd ATTRIBUTE_UNUSED;
1683
     PTR afile;
1684
     asymbol *symbol;
1685
     bfd_print_symbol_type how;
1686
{
1687
  FILE *file = (FILE *) afile;
1688
 
1689
  switch (how)
1690
    {
1691
    case bfd_print_symbol_name:
1692
      fprintf (file, "%s", symbol->name);
1693
      break;
1694
    case bfd_print_symbol_more:
1695
#if 0
1696
      fprintf (file, "%4x %2x", aout_symbol (symbol)->desc & 0xffff,
1697
               aout_symbol (symbol)->other & 0xff);
1698
#endif
1699
      BFD_FAIL ();
1700
      break;
1701
    case bfd_print_symbol_all:
1702
      {
1703
        const char *section_name =
1704
          (symbol->section == (asection *) NULL
1705
           ? "*abs"
1706
           : symbol->section->name);
1707
        if (symbol->name[0] == ' ')
1708
          {
1709
            fprintf (file, "* empty table entry ");
1710
          }
1711
        else
1712
          {
1713
            bfd_print_symbol_vandf ((PTR) file, symbol);
1714
 
1715
            fprintf (file, " %-5s %04x %02x %s",
1716
                     section_name,
1717
                     (unsigned) ieee_symbol (symbol)->index,
1718
                     (unsigned) 0,
1719
                     symbol->name);
1720
          }
1721
      }
1722
      break;
1723
    }
1724
}
1725
 
1726
static boolean
1727
do_one (ieee, current_map, location_ptr, s, iterations)
1728
     ieee_data_type *ieee;
1729
     ieee_per_section_type *current_map;
1730
     unsigned char *location_ptr;
1731
     asection *s;
1732
     int iterations;
1733
{
1734
  switch (this_byte (&(ieee->h)))
1735
    {
1736
    case ieee_load_constant_bytes_enum:
1737
      {
1738
        unsigned int number_of_maus;
1739
        unsigned int i;
1740
        next_byte (&(ieee->h));
1741
        number_of_maus = must_parse_int (&(ieee->h));
1742
 
1743
        for (i = 0; i < number_of_maus; i++)
1744
          {
1745
            location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1746
            next_byte (&(ieee->h));
1747
          }
1748
      }
1749
      break;
1750
 
1751
    case ieee_load_with_relocation_enum:
1752
      {
1753
        boolean loop = true;
1754
        next_byte (&(ieee->h));
1755
        while (loop)
1756
          {
1757
            switch (this_byte (&(ieee->h)))
1758
              {
1759
              case ieee_variable_R_enum:
1760
 
1761
              case ieee_function_signed_open_b_enum:
1762
              case ieee_function_unsigned_open_b_enum:
1763
              case ieee_function_either_open_b_enum:
1764
                {
1765
                  unsigned int extra = 4;
1766
                  boolean pcrel = false;
1767
                  asection *section;
1768
                  ieee_reloc_type *r =
1769
                  (ieee_reloc_type *) bfd_alloc (ieee->h.abfd,
1770
                                                 sizeof (ieee_reloc_type));
1771
                  if (!r)
1772
                    return false;
1773
 
1774
                  *(current_map->reloc_tail_ptr) = r;
1775
                  current_map->reloc_tail_ptr = &r->next;
1776
                  r->next = (ieee_reloc_type *) NULL;
1777
                  next_byte (&(ieee->h));
1778
/*                          abort();*/
1779
                  r->relent.sym_ptr_ptr = 0;
1780
                  parse_expression (ieee,
1781
                                    &r->relent.addend,
1782
                                    &r->symbol,
1783
                                    &pcrel, &extra, &section);
1784
                  r->relent.address = current_map->pc;
1785
                  s->flags |= SEC_RELOC;
1786
                  s->owner->flags |= HAS_RELOC;
1787
                  s->reloc_count++;
1788
                  if (r->relent.sym_ptr_ptr == NULL && section != NULL)
1789
                    r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1790
 
1791
                  if (this_byte (&(ieee->h)) == (int) ieee_comma)
1792
                    {
1793
                      next_byte (&(ieee->h));
1794
                      /* Fetch number of bytes to pad */
1795
                      extra = must_parse_int (&(ieee->h));
1796
                    };
1797
 
1798
                  switch (this_byte (&(ieee->h)))
1799
                    {
1800
                    case ieee_function_signed_close_b_enum:
1801
                      next_byte (&(ieee->h));
1802
                      break;
1803
                    case ieee_function_unsigned_close_b_enum:
1804
                      next_byte (&(ieee->h));
1805
                      break;
1806
                    case ieee_function_either_close_b_enum:
1807
                      next_byte (&(ieee->h));
1808
                      break;
1809
                    default:
1810
                      break;
1811
                    }
1812
                  /* Build a relocation entry for this type */
1813
                  /* If pc rel then stick -ve pc into instruction
1814
                     and take out of reloc ..
1815
 
1816
                     I've changed this. It's all too complicated. I
1817
                     keep 0 in the instruction now.  */
1818
 
1819
                  switch (extra)
1820
                    {
1821
                    case 0:
1822
                    case 4:
1823
 
1824
                      if (pcrel == true)
1825
                        {
1826
#if KEEPMINUSPCININST
1827
                          bfd_put_32 (ieee->h.abfd, -current_map->pc, location_ptr +
1828
                                      current_map->pc);
1829
                          r->relent.howto = &rel32_howto;
1830
                          r->relent.addend -=
1831
                            current_map->pc;
1832
#else
1833
                          bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1834
                                      current_map->pc);
1835
                          r->relent.howto = &rel32_howto;
1836
#endif
1837
                        }
1838
                      else
1839
                        {
1840
                          bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1841
                                      current_map->pc);
1842
                          r->relent.howto = &abs32_howto;
1843
                        }
1844
                      current_map->pc += 4;
1845
                      break;
1846
                    case 2:
1847
                      if (pcrel == true)
1848
                        {
1849
#if KEEPMINUSPCININST
1850
                          bfd_put_16 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1851
                          r->relent.addend -= current_map->pc;
1852
                          r->relent.howto = &rel16_howto;
1853
#else
1854
 
1855
                          bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1856
                          r->relent.howto = &rel16_howto;
1857
#endif
1858
                        }
1859
 
1860
                      else
1861
                        {
1862
                          bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1863
                          r->relent.howto = &abs16_howto;
1864
                        }
1865
                      current_map->pc += 2;
1866
                      break;
1867
                    case 1:
1868
                      if (pcrel == true)
1869
                        {
1870
#if KEEPMINUSPCININST
1871
                          bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1872
                          r->relent.addend -= current_map->pc;
1873
                          r->relent.howto = &rel8_howto;
1874
#else
1875
                          bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1876
                          r->relent.howto = &rel8_howto;
1877
#endif
1878
                        }
1879
                      else
1880
                        {
1881
                          bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1882
                          r->relent.howto = &abs8_howto;
1883
                        }
1884
                      current_map->pc += 1;
1885
                      break;
1886
 
1887
                    default:
1888
                      BFD_FAIL ();
1889
                      return false;
1890
                    }
1891
                }
1892
                break;
1893
              default:
1894
                {
1895
                  bfd_vma this_size;
1896
                  if (parse_int (&(ieee->h), &this_size) == true)
1897
                    {
1898
                      unsigned int i;
1899
                      for (i = 0; i < this_size; i++)
1900
                        {
1901
                          location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1902
                          next_byte (&(ieee->h));
1903
                        }
1904
                    }
1905
                  else
1906
                    {
1907
                      loop = false;
1908
                    }
1909
                }
1910
              }
1911
 
1912
            /* Prevent more than the first load-item of an LR record
1913
               from being repeated (MRI convention). */
1914
            if (iterations != 1)
1915
              loop = false;
1916
          }
1917
      }
1918
    }
1919
  return true;
1920
}
1921
 
1922
/* Read in all the section data and relocation stuff too */
1923
static boolean
1924
ieee_slurp_section_data (abfd)
1925
     bfd *abfd;
1926
{
1927
  bfd_byte *location_ptr = (bfd_byte *) NULL;
1928
  ieee_data_type *ieee = IEEE_DATA (abfd);
1929
  unsigned int section_number;
1930
 
1931
  ieee_per_section_type *current_map = (ieee_per_section_type *) NULL;
1932
  asection *s;
1933
  /* Seek to the start of the data area */
1934
  if (ieee->read_data == true)
1935
    return true;
1936
  ieee->read_data = true;
1937
  ieee_seek (abfd, ieee->w.r.data_part);
1938
 
1939
  /* Allocate enough space for all the section contents */
1940
 
1941
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1942
    {
1943
      ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1944
      if ((s->flags & SEC_DEBUGGING) != 0)
1945
        continue;
1946
      per->data = (bfd_byte *) bfd_alloc (ieee->h.abfd, s->_raw_size);
1947
      if (!per->data)
1948
        return false;
1949
      /*SUPPRESS 68*/
1950
      per->reloc_tail_ptr =
1951
        (ieee_reloc_type **) & (s->relocation);
1952
    }
1953
 
1954
  while (true)
1955
    {
1956
      switch (this_byte (&(ieee->h)))
1957
        {
1958
          /* IF we see anything strange then quit */
1959
        default:
1960
          return true;
1961
 
1962
        case ieee_set_current_section_enum:
1963
          next_byte (&(ieee->h));
1964
          section_number = must_parse_int (&(ieee->h));
1965
          s = ieee->section_table[section_number];
1966
          s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
1967
          current_map = (ieee_per_section_type *) s->used_by_bfd;
1968
          location_ptr = current_map->data - s->vma;
1969
          /* The document I have says that Microtec's compilers reset */
1970
          /* this after a sec section, even though the standard says not */
1971
          /* to. SO .. */
1972
          current_map->pc = s->vma;
1973
          break;
1974
 
1975
        case ieee_e2_first_byte_enum:
1976
          next_byte (&(ieee->h));
1977
          switch (this_byte (&(ieee->h)))
1978
            {
1979
            case ieee_set_current_pc_enum & 0xff:
1980
              {
1981
                bfd_vma value;
1982
                ieee_symbol_index_type symbol;
1983
                unsigned int extra;
1984
                boolean pcrel;
1985
                next_byte (&(ieee->h));
1986
                must_parse_int (&(ieee->h));    /* Thow away section #*/
1987
                parse_expression (ieee, &value,
1988
                                  &symbol,
1989
                                  &pcrel, &extra,
1990
                                  0);
1991
                current_map->pc = value;
1992
                BFD_ASSERT ((unsigned) (value - s->vma) <= s->_raw_size);
1993
              }
1994
              break;
1995
 
1996
            case ieee_value_starting_address_enum & 0xff:
1997
              next_byte (&(ieee->h));
1998
              if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum)
1999
                next_byte (&(ieee->h));
2000
              abfd->start_address = must_parse_int (&(ieee->h));
2001
              /* We've got to the end of the data now - */
2002
              return true;
2003
            default:
2004
              BFD_FAIL ();
2005
              return false;
2006
            }
2007
          break;
2008
        case ieee_repeat_data_enum:
2009
          {
2010
            /* Repeat the following LD or LR n times - we do this by
2011
                 remembering the stream pointer before running it and
2012
                 resetting it and running it n times. We special case
2013
                 the repetition of a repeat_data/load_constant
2014
                 */
2015
 
2016
            unsigned int iterations;
2017
            unsigned char *start;
2018
            next_byte (&(ieee->h));
2019
            iterations = must_parse_int (&(ieee->h));
2020
            start = ieee->h.input_p;
2021
            if (start[0] == (int) ieee_load_constant_bytes_enum &&
2022
                start[1] == 1)
2023
              {
2024
                while (iterations != 0)
2025
                  {
2026
                    location_ptr[current_map->pc++] = start[2];
2027
                    iterations--;
2028
                  }
2029
                next_byte (&(ieee->h));
2030
                next_byte (&(ieee->h));
2031
                next_byte (&(ieee->h));
2032
              }
2033
            else
2034
              {
2035
                while (iterations != 0)
2036
                  {
2037
                    ieee->h.input_p = start;
2038
                    if (!do_one (ieee, current_map, location_ptr, s,
2039
                                 iterations))
2040
                      return false;
2041
                    iterations--;
2042
                  }
2043
              }
2044
          }
2045
          break;
2046
        case ieee_load_constant_bytes_enum:
2047
        case ieee_load_with_relocation_enum:
2048
          {
2049
            if (!do_one (ieee, current_map, location_ptr, s, 1))
2050
              return false;
2051
          }
2052
        }
2053
    }
2054
}
2055
 
2056
boolean
2057
ieee_new_section_hook (abfd, newsect)
2058
     bfd *abfd;
2059
     asection *newsect;
2060
{
2061
  newsect->used_by_bfd = (PTR)
2062
    bfd_alloc (abfd, sizeof (ieee_per_section_type));
2063
  if (!newsect->used_by_bfd)
2064
    return false;
2065
  ieee_per_section (newsect)->data = (bfd_byte *) NULL;
2066
  ieee_per_section (newsect)->section = newsect;
2067
  return true;
2068
}
2069
 
2070
long
2071
ieee_get_reloc_upper_bound (abfd, asect)
2072
     bfd *abfd;
2073
     sec_ptr asect;
2074
{
2075
  if ((asect->flags & SEC_DEBUGGING) != 0)
2076
    return 0;
2077
  if (! ieee_slurp_section_data (abfd))
2078
    return -1;
2079
  return (asect->reloc_count + 1) * sizeof (arelent *);
2080
}
2081
 
2082
static boolean
2083
ieee_get_section_contents (abfd, section, location, offset, count)
2084
     bfd *abfd;
2085
     sec_ptr section;
2086
     PTR location;
2087
     file_ptr offset;
2088
     bfd_size_type count;
2089
{
2090
  ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
2091
  if ((section->flags & SEC_DEBUGGING) != 0)
2092
    return _bfd_generic_get_section_contents (abfd, section, location,
2093
                                              offset, count);
2094
  ieee_slurp_section_data (abfd);
2095
  (void) memcpy ((PTR) location, (PTR) (p->data + offset), (unsigned) count);
2096
  return true;
2097
}
2098
 
2099
long
2100
ieee_canonicalize_reloc (abfd, section, relptr, symbols)
2101
     bfd *abfd;
2102
     sec_ptr section;
2103
     arelent **relptr;
2104
     asymbol **symbols;
2105
{
2106
/*  ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
2107
  ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation);
2108
  ieee_data_type *ieee = IEEE_DATA (abfd);
2109
 
2110
  if ((section->flags & SEC_DEBUGGING) != 0)
2111
    return 0;
2112
 
2113
  while (src != (ieee_reloc_type *) NULL)
2114
    {
2115
      /* Work out which symbol to attach it this reloc to */
2116
      switch (src->symbol.letter)
2117
        {
2118
        case 'I':
2119
          src->relent.sym_ptr_ptr =
2120
            symbols + src->symbol.index + ieee->external_symbol_base_offset;
2121
          break;
2122
        case 'X':
2123
          src->relent.sym_ptr_ptr =
2124
            symbols + src->symbol.index + ieee->external_reference_base_offset;
2125
          break;
2126
        case 0:
2127
          if (src->relent.sym_ptr_ptr != NULL)
2128
            src->relent.sym_ptr_ptr =
2129
              src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr;
2130
          break;
2131
        default:
2132
 
2133
          BFD_FAIL ();
2134
        }
2135
      *relptr++ = &src->relent;
2136
      src = src->next;
2137
    }
2138
  *relptr = (arelent *) NULL;
2139
  return section->reloc_count;
2140
}
2141
 
2142
static int
2143
comp (ap, bp)
2144
     CONST PTR ap;
2145
     CONST PTR bp;
2146
{
2147
  arelent *a = *((arelent **) ap);
2148
  arelent *b = *((arelent **) bp);
2149
  return a->address - b->address;
2150
}
2151
 
2152
/* Write the section headers.  */
2153
 
2154
static boolean
2155
ieee_write_section_part (abfd)
2156
     bfd *abfd;
2157
{
2158
  ieee_data_type *ieee = IEEE_DATA (abfd);
2159
  asection *s;
2160
  ieee->w.r.section_part = bfd_tell (abfd);
2161
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2162
    {
2163
      if (! bfd_is_abs_section (s)
2164
          && (s->flags & SEC_DEBUGGING) == 0)
2165
        {
2166
          if (! ieee_write_byte (abfd, ieee_section_type_enum)
2167
              || ! ieee_write_byte (abfd,
2168
                                    (bfd_byte) (s->index
2169
                                                + IEEE_SECTION_NUMBER_BASE)))
2170
            return false;
2171
 
2172
          if (abfd->flags & EXEC_P)
2173
            {
2174
              /* This image is executable, so output absolute sections */
2175
              if (! ieee_write_byte (abfd, ieee_variable_A_enum)
2176
                  || ! ieee_write_byte (abfd, ieee_variable_S_enum))
2177
                return false;
2178
            }
2179
          else
2180
            {
2181
              if (! ieee_write_byte (abfd, ieee_variable_C_enum))
2182
                return false;
2183
            }
2184
 
2185
          switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM))
2186
            {
2187
            case SEC_CODE | SEC_LOAD:
2188
            case SEC_CODE:
2189
              if (! ieee_write_byte (abfd, ieee_variable_P_enum))
2190
                return false;
2191
              break;
2192
            case SEC_DATA:
2193
            default:
2194
              if (! ieee_write_byte (abfd, ieee_variable_D_enum))
2195
                return false;
2196
              break;
2197
            case SEC_ROM:
2198
            case SEC_ROM | SEC_DATA:
2199
            case SEC_ROM | SEC_LOAD:
2200
            case SEC_ROM | SEC_DATA | SEC_LOAD:
2201
              if (! ieee_write_byte (abfd, ieee_variable_R_enum))
2202
                return false;
2203
            }
2204
 
2205
 
2206
          if (! ieee_write_id (abfd, s->name))
2207
            return false;
2208
#if 0
2209
          ieee_write_int (abfd, 0);      /* Parent */
2210
          ieee_write_int (abfd, 0);      /* Brother */
2211
          ieee_write_int (abfd, 0);      /* Context */
2212
#endif
2213
          /* Alignment */
2214
          if (! ieee_write_byte (abfd, ieee_section_alignment_enum)
2215
              || ! ieee_write_byte (abfd,
2216
                                    (bfd_byte) (s->index
2217
                                                + IEEE_SECTION_NUMBER_BASE))
2218
              || ! ieee_write_int (abfd, 1 << s->alignment_power))
2219
            return false;
2220
 
2221
          /* Size */
2222
          if (! ieee_write_2bytes (abfd, ieee_section_size_enum)
2223
              || ! ieee_write_byte (abfd,
2224
                                    (bfd_byte) (s->index
2225
                                                + IEEE_SECTION_NUMBER_BASE))
2226
              || ! ieee_write_int (abfd, s->_raw_size))
2227
            return false;
2228
          if (abfd->flags & EXEC_P)
2229
            {
2230
              /* Relocateable sections don't have asl records */
2231
              /* Vma */
2232
              if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum)
2233
                  || ! ieee_write_byte (abfd,
2234
                                        ((bfd_byte)
2235
                                         (s->index
2236
                                          + IEEE_SECTION_NUMBER_BASE)))
2237
                  || ! ieee_write_int (abfd, s->lma))
2238
                return false;
2239
            }
2240
        }
2241
    }
2242
 
2243
  return true;
2244
}
2245
 
2246
 
2247
static boolean
2248
do_with_relocs (abfd, s)
2249
     bfd *abfd;
2250
     asection *s;
2251
{
2252
  unsigned int number_of_maus_in_address =
2253
    bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd);
2254
  unsigned int relocs_to_go = s->reloc_count;
2255
  bfd_byte *stream = ieee_per_section (s)->data;
2256
  arelent **p = s->orelocation;
2257
  bfd_size_type current_byte_index = 0;
2258
 
2259
  qsort (s->orelocation,
2260
         relocs_to_go,
2261
         sizeof (arelent **),
2262
         comp);
2263
 
2264
  /* Output the section preheader */
2265
  if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2266
      || ! ieee_write_byte (abfd,
2267
                            (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))
2268
      || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum)
2269
      || ! ieee_write_byte (abfd,
2270
                            (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)))
2271
    return false;
2272
  if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0)
2273
    {
2274
      if (! ieee_write_int (abfd, s->lma))
2275
        return false;
2276
    }
2277
  else
2278
    {
2279
      if (! ieee_write_expression (abfd, 0, s->symbol, 0, 0))
2280
        return false;
2281
    }
2282
 
2283
  if (relocs_to_go == 0)
2284
    {
2285
      /* If there aren't any relocations then output the load constant
2286
         byte opcode rather than the load with relocation opcode */
2287
 
2288
      while (current_byte_index < s->_raw_size)
2289
        {
2290
          bfd_size_type run;
2291
          unsigned int MAXRUN = 127;
2292
          run = MAXRUN;
2293
          if (run > s->_raw_size - current_byte_index)
2294
            {
2295
              run = s->_raw_size - current_byte_index;
2296
            }
2297
 
2298
          if (run != 0)
2299
            {
2300
              if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum))
2301
                return false;
2302
              /* Output a stream of bytes */
2303
              if (! ieee_write_int (abfd, run))
2304
                return false;
2305
              if (bfd_write ((PTR) (stream + current_byte_index),
2306
                             1,
2307
                             run,
2308
                             abfd)
2309
                  != run)
2310
                return false;
2311
              current_byte_index += run;
2312
            }
2313
        }
2314
    }
2315
  else
2316
    {
2317
      if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum))
2318
        return false;
2319
 
2320
      /* Output the data stream as the longest sequence of bytes
2321
         possible, allowing for the a reasonable packet size and
2322
         relocation stuffs.  */
2323
 
2324
      if ((PTR) stream == (PTR) NULL)
2325
        {
2326
          /* Outputting a section without data, fill it up */
2327
          stream = (unsigned char *) (bfd_alloc (abfd, s->_raw_size));
2328
          if (!stream)
2329
            return false;
2330
          memset ((PTR) stream, 0, (size_t) s->_raw_size);
2331
        }
2332
      while (current_byte_index < s->_raw_size)
2333
        {
2334
          bfd_size_type run;
2335
          unsigned int MAXRUN = 127;
2336
          if (relocs_to_go)
2337
            {
2338
              run = (*p)->address - current_byte_index;
2339
              if (run > MAXRUN)
2340
                run = MAXRUN;
2341
            }
2342
          else
2343
            {
2344
              run = MAXRUN;
2345
            }
2346
          if (run > s->_raw_size - current_byte_index)
2347
            {
2348
              run = s->_raw_size - current_byte_index;
2349
            }
2350
 
2351
          if (run != 0)
2352
            {
2353
              /* Output a stream of bytes */
2354
              if (! ieee_write_int (abfd, run))
2355
                return false;
2356
              if (bfd_write ((PTR) (stream + current_byte_index),
2357
                             1,
2358
                             run,
2359
                             abfd)
2360
                  != run)
2361
                return false;
2362
              current_byte_index += run;
2363
            }
2364
          /* Output any relocations here */
2365
          if (relocs_to_go && (*p) && (*p)->address == current_byte_index)
2366
            {
2367
              while (relocs_to_go
2368
                     && (*p) && (*p)->address == current_byte_index)
2369
                {
2370
                  arelent *r = *p;
2371
                  bfd_signed_vma ov;
2372
 
2373
#if 0
2374
                  if (r->howto->pc_relative)
2375
                    {
2376
                      r->addend += current_byte_index;
2377
                    }
2378
#endif
2379
 
2380
                  switch (r->howto->size)
2381
                    {
2382
                    case 2:
2383
 
2384
                      ov = bfd_get_signed_32 (abfd,
2385
                                              stream + current_byte_index);
2386
                      current_byte_index += 4;
2387
                      break;
2388
                    case 1:
2389
                      ov = bfd_get_signed_16 (abfd,
2390
                                              stream + current_byte_index);
2391
                      current_byte_index += 2;
2392
                      break;
2393
                    case 0:
2394
                      ov = bfd_get_signed_8 (abfd,
2395
                                             stream + current_byte_index);
2396
                      current_byte_index++;
2397
                      break;
2398
                    default:
2399
                      ov = 0;
2400
                      BFD_FAIL ();
2401
                      return false;
2402
                    }
2403
 
2404
                  ov &= r->howto->src_mask;
2405
 
2406
                  if (r->howto->pc_relative
2407
                      && ! r->howto->pcrel_offset)
2408
                    ov += r->address;
2409
 
2410
                  if (! ieee_write_byte (abfd,
2411
                                         ieee_function_either_open_b_enum))
2412
                    return false;
2413
 
2414
/*                abort();*/
2415
 
2416
                  if (r->sym_ptr_ptr != (asymbol **) NULL)
2417
                    {
2418
                      if (! ieee_write_expression (abfd, r->addend + ov,
2419
                                                   *(r->sym_ptr_ptr),
2420
                                                   r->howto->pc_relative,
2421
                                                   s->index))
2422
                        return false;
2423
                    }
2424
                  else
2425
                    {
2426
                      if (! ieee_write_expression (abfd, r->addend + ov,
2427
                                                   (asymbol *) NULL,
2428
                                                   r->howto->pc_relative,
2429
                                                   s->index))
2430
                        return false;
2431
                    }
2432
 
2433
                  if (number_of_maus_in_address
2434
                      != bfd_get_reloc_size (r->howto))
2435
                    {
2436
                      if (! ieee_write_int (abfd,
2437
                                            bfd_get_reloc_size (r->howto)))
2438
                        return false;
2439
                    }
2440
                  if (! ieee_write_byte (abfd,
2441
                                         ieee_function_either_close_b_enum))
2442
                    return false;
2443
 
2444
                  relocs_to_go--;
2445
                  p++;
2446
                }
2447
 
2448
            }
2449
        }
2450
    }
2451
 
2452
  return true;
2453
}
2454
 
2455
/* If there are no relocations in the output section then we can be
2456
   clever about how we write.  We block items up into a max of 127
2457
   bytes.  */
2458
 
2459
static boolean
2460
do_as_repeat (abfd, s)
2461
     bfd *abfd;
2462
     asection *s;
2463
{
2464
  if (s->_raw_size)
2465
    {
2466
      if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2467
          || ! ieee_write_byte (abfd,
2468
                                (bfd_byte) (s->index
2469
                                            + IEEE_SECTION_NUMBER_BASE))
2470
          || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8)
2471
          || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff)
2472
          || ! ieee_write_byte (abfd,
2473
                                (bfd_byte) (s->index
2474
                                            + IEEE_SECTION_NUMBER_BASE))
2475
          || ! ieee_write_int (abfd, s->lma)
2476
          || ! ieee_write_byte (abfd, ieee_repeat_data_enum)
2477
          || ! ieee_write_int (abfd, s->_raw_size)
2478
          || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)
2479
          || ! ieee_write_byte (abfd, 1)
2480
          || ! ieee_write_byte (abfd, 0))
2481
        return false;
2482
    }
2483
 
2484
  return true;
2485
}
2486
 
2487
static boolean
2488
do_without_relocs (abfd, s)
2489
     bfd *abfd;
2490
     asection *s;
2491
{
2492
  bfd_byte *stream = ieee_per_section (s)->data;
2493
 
2494
  if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
2495
    {
2496
      if (! do_as_repeat (abfd, s))
2497
        return false;
2498
    }
2499
  else
2500
    {
2501
      unsigned int i;
2502
      for (i = 0; i < s->_raw_size; i++)
2503
        {
2504
          if (stream[i] != 0)
2505
            {
2506
              if (! do_with_relocs (abfd, s))
2507
                return false;
2508
              return true;
2509
            }
2510
        }
2511
      if (! do_as_repeat (abfd, s))
2512
        return false;
2513
    }
2514
 
2515
  return true;
2516
}
2517
 
2518
 
2519
static unsigned char *output_ptr_start;
2520
static unsigned char *output_ptr;
2521
static unsigned char *output_ptr_end;
2522
static unsigned char *input_ptr_start;
2523
static unsigned char *input_ptr;
2524
static unsigned char *input_ptr_end;
2525
static bfd *input_bfd;
2526
static bfd *output_bfd;
2527
static int output_buffer;
2528
 
2529
static void
2530
fill ()
2531
{
2532
  /* FIXME: Check return value.  I'm not sure whether it needs to read
2533
     the entire buffer or not.  */
2534
  bfd_read ((PTR) input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
2535
  input_ptr = input_ptr_start;
2536
}
2537
static void
2538
flush ()
2539
{
2540
  if (bfd_write ((PTR) (output_ptr_start), 1, output_ptr - output_ptr_start,
2541
                 output_bfd)
2542
      != (bfd_size_type) (output_ptr - output_ptr_start))
2543
    abort ();
2544
  output_ptr = output_ptr_start;
2545
  output_buffer++;
2546
}
2547
 
2548
#define THIS() ( *input_ptr )
2549
#define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
2550
#define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end)  flush(); }
2551
 
2552
static void
2553
write_int (value)
2554
     int value;
2555
{
2556
  if (value >= 0 && value <= 127)
2557
    {
2558
      OUT (value);
2559
    }
2560
  else
2561
    {
2562
      unsigned int length;
2563
      /* How many significant bytes ? */
2564
      /* FIXME FOR LONGER INTS */
2565
      if (value & 0xff000000)
2566
        {
2567
          length = 4;
2568
        }
2569
      else if (value & 0x00ff0000)
2570
        {
2571
          length = 3;
2572
        }
2573
      else if (value & 0x0000ff00)
2574
        {
2575
          length = 2;
2576
        }
2577
      else
2578
        length = 1;
2579
 
2580
      OUT ((int) ieee_number_repeat_start_enum + length);
2581
      switch (length)
2582
        {
2583
        case 4:
2584
          OUT (value >> 24);
2585
        case 3:
2586
          OUT (value >> 16);
2587
        case 2:
2588
          OUT (value >> 8);
2589
        case 1:
2590
          OUT (value);
2591
        }
2592
 
2593
    }
2594
}
2595
 
2596
static void
2597
copy_id ()
2598
{
2599
  int length = THIS ();
2600
  char ch;
2601
  OUT (length);
2602
  NEXT ();
2603
  while (length--)
2604
    {
2605
      ch = THIS ();
2606
      OUT (ch);
2607
      NEXT ();
2608
    }
2609
}
2610
 
2611
#define VAR(x) ((x | 0x80))
2612
static void
2613
copy_expression ()
2614
{
2615
  int stack[10];
2616
  int *tos = stack;
2617
  int value = 0;
2618
  while (1)
2619
    {
2620
      switch (THIS ())
2621
        {
2622
        case 0x84:
2623
          NEXT ();
2624
          value = THIS ();
2625
          NEXT ();
2626
          value = (value << 8) | THIS ();
2627
          NEXT ();
2628
          value = (value << 8) | THIS ();
2629
          NEXT ();
2630
          value = (value << 8) | THIS ();
2631
          NEXT ();
2632
          *tos++ = value;
2633
          break;
2634
        case 0x83:
2635
          NEXT ();
2636
          value = THIS ();
2637
          NEXT ();
2638
          value = (value << 8) | THIS ();
2639
          NEXT ();
2640
          value = (value << 8) | THIS ();
2641
          NEXT ();
2642
          *tos++ = value;
2643
          break;
2644
        case 0x82:
2645
          NEXT ();
2646
          value = THIS ();
2647
          NEXT ();
2648
          value = (value << 8) | THIS ();
2649
          NEXT ();
2650
          *tos++ = value;
2651
          break;
2652
        case 0x81:
2653
          NEXT ();
2654
          value = THIS ();
2655
          NEXT ();
2656
          *tos++ = value;
2657
          break;
2658
        case 0x80:
2659
          NEXT ();
2660
          *tos++ = 0;
2661
          break;
2662
        default:
2663
          if (THIS () > 0x84)
2664
            {
2665
              /* Not a number, just bug out with the answer */
2666
              write_int (*(--tos));
2667
              return;
2668
            }
2669
          *tos++ = THIS ();
2670
          NEXT ();
2671
          value = 0;
2672
          break;
2673
        case 0xa5:
2674
          /* PLUS anything */
2675
          {
2676
            int value = *(--tos);
2677
            value += *(--tos);
2678
            *tos++ = value;
2679
            NEXT ();
2680
          }
2681
          break;
2682
        case VAR ('R'):
2683
          {
2684
            int section_number;
2685
            ieee_data_type *ieee;
2686
            asection *s;
2687
            NEXT ();
2688
            section_number = THIS ();
2689
 
2690
            NEXT ();
2691
            ieee = IEEE_DATA (input_bfd);
2692
            s = ieee->section_table[section_number];
2693
            if (s->output_section)
2694
              {
2695
                value = s->output_section->lma;
2696
              }
2697
            else
2698
              {
2699
                value = 0;
2700
              }
2701
            value += s->output_offset;
2702
            *tos++ = value;
2703
            value = 0;
2704
          }
2705
          break;
2706
        case 0x90:
2707
          {
2708
            NEXT ();
2709
            write_int (*(--tos));
2710
            OUT (0x90);
2711
            return;
2712
 
2713
          }
2714
        }
2715
    }
2716
 
2717
}
2718
 
2719
/* Drop the int in the buffer, and copy a null into the gap, which we
2720
   will overwrite later */
2721
 
2722
struct output_buffer_struct
2723
{
2724
  unsigned char *ptrp;
2725
  int buffer;
2726
};
2727
 
2728
static void
2729
fill_int (buf)
2730
     struct output_buffer_struct *buf;
2731
{
2732
  if (buf->buffer == output_buffer)
2733
    {
2734
      /* Still a chance to output the size */
2735
      int value = output_ptr - buf->ptrp + 3;
2736
      buf->ptrp[0] = value >> 24;
2737
      buf->ptrp[1] = value >> 16;
2738
      buf->ptrp[2] = value >> 8;
2739
      buf->ptrp[3] = value >> 0;
2740
    }
2741
}
2742
 
2743
static void
2744
drop_int (buf)
2745
     struct output_buffer_struct *buf;
2746
{
2747
  int type = THIS ();
2748
  int ch;
2749
  if (type <= 0x84)
2750
    {
2751
      NEXT ();
2752
      switch (type)
2753
        {
2754
        case 0x84:
2755
          ch = THIS ();
2756
          NEXT ();
2757
        case 0x83:
2758
          ch = THIS ();
2759
          NEXT ();
2760
        case 0x82:
2761
          ch = THIS ();
2762
          NEXT ();
2763
        case 0x81:
2764
          ch = THIS ();
2765
          NEXT ();
2766
        case 0x80:
2767
          break;
2768
        }
2769
    }
2770
  OUT (0x84);
2771
  buf->ptrp = output_ptr;
2772
  buf->buffer = output_buffer;
2773
  OUT (0);
2774
  OUT (0);
2775
  OUT (0);
2776
  OUT (0);
2777
}
2778
 
2779
static void
2780
copy_int ()
2781
{
2782
  int type = THIS ();
2783
  int ch;
2784
  if (type <= 0x84)
2785
    {
2786
      OUT (type);
2787
      NEXT ();
2788
      switch (type)
2789
        {
2790
        case 0x84:
2791
          ch = THIS ();
2792
          NEXT ();
2793
          OUT (ch);
2794
        case 0x83:
2795
          ch = THIS ();
2796
          NEXT ();
2797
          OUT (ch);
2798
        case 0x82:
2799
          ch = THIS ();
2800
          NEXT ();
2801
          OUT (ch);
2802
        case 0x81:
2803
          ch = THIS ();
2804
          NEXT ();
2805
          OUT (ch);
2806
        case 0x80:
2807
          break;
2808
        }
2809
    }
2810
}
2811
 
2812
#define ID copy_id()
2813
#define INT copy_int()
2814
#define EXP copy_expression()
2815
static void copy_till_end ();
2816
#define INTn(q) copy_int()
2817
#define EXPn(q) copy_expression()
2818
 
2819
static void
2820
f1_record ()
2821
{
2822
  int ch;
2823
  /* ATN record */
2824
  NEXT ();
2825
  ch = THIS ();
2826
  switch (ch)
2827
    {
2828
    default:
2829
      OUT (0xf1);
2830
      OUT (ch);
2831
      break;
2832
    case 0xc9:
2833
      NEXT ();
2834
      OUT (0xf1);
2835
      OUT (0xc9);
2836
      INT;
2837
      INT;
2838
      ch = THIS ();
2839
      switch (ch)
2840
        {
2841
        case 0x16:
2842
          NEXT ();
2843
          break;
2844
        case 0x01:
2845
          NEXT ();
2846
          break;
2847
        case 0x00:
2848
          NEXT ();
2849
          INT;
2850
          break;
2851
        case 0x03:
2852
          NEXT ();
2853
          INT;
2854
          break;
2855
        case 0x13:
2856
          EXPn (instruction address);
2857
          break;
2858
        default:
2859
          break;
2860
        }
2861
      break;
2862
    case 0xd8:
2863
      /* EXternal ref */
2864
      NEXT ();
2865
      OUT (0xf1);
2866
      OUT (0xd8);
2867
      EXP;
2868
      EXP;
2869
      EXP;
2870
      EXP;
2871
      break;
2872
    case 0xce:
2873
      NEXT ();
2874
      OUT (0xf1);
2875
      OUT (0xce);
2876
      INT;
2877
      INT;
2878
      ch = THIS ();
2879
      INT;
2880
      switch (ch)
2881
        {
2882
        case 0x01:
2883
          INT;
2884
          INT;
2885
          break;
2886
        case 0x02:
2887
          INT;
2888
          break;
2889
        case 0x04:
2890
          EXPn (external function);
2891
          break;
2892
        case 0x05:
2893
          break;
2894
        case 0x07:
2895
          INTn (line number);
2896
          INT;
2897
        case 0x08:
2898
          break;
2899
        case 0x0a:
2900
          INTn (locked register);
2901
          INT;
2902
          break;
2903
        case 0x3f:
2904
          copy_till_end ();
2905
          break;
2906
        case 0x3e:
2907
          copy_till_end ();
2908
          break;
2909
        case 0x40:
2910
          copy_till_end ();
2911
          break;
2912
        case 0x41:
2913
          ID;
2914
          break;
2915
        }
2916
    }
2917
 
2918
}
2919
 
2920
static void
2921
f0_record ()
2922
{
2923
  /* Attribute record */
2924
  NEXT ();
2925
  OUT (0xf0);
2926
  INTn (Symbol name);
2927
  ID;
2928
}
2929
 
2930
static void
2931
copy_till_end ()
2932
{
2933
  int ch = THIS ();
2934
  while (1)
2935
    {
2936
      while (ch <= 0x80)
2937
        {
2938
          OUT (ch);
2939
          NEXT ();
2940
          ch = THIS ();
2941
        }
2942
      switch (ch)
2943
        {
2944
        case 0x84:
2945
          OUT (THIS ());
2946
          NEXT ();
2947
        case 0x83:
2948
          OUT (THIS ());
2949
          NEXT ();
2950
        case 0x82:
2951
          OUT (THIS ());
2952
          NEXT ();
2953
        case 0x81:
2954
          OUT (THIS ());
2955
          NEXT ();
2956
          OUT (THIS ());
2957
          NEXT ();
2958
 
2959
          ch = THIS ();
2960
          break;
2961
        default:
2962
          return;
2963
        }
2964
    }
2965
 
2966
}
2967
 
2968
static void
2969
f2_record ()
2970
{
2971
  NEXT ();
2972
  OUT (0xf2);
2973
  INT;
2974
  NEXT ();
2975
  OUT (0xce);
2976
  INT;
2977
  copy_till_end ();
2978
}
2979
 
2980
 
2981
static void block ();
2982
static void
2983
f8_record ()
2984
{
2985
  int ch;
2986
  NEXT ();
2987
  ch = THIS ();
2988
  switch (ch)
2989
    {
2990
    case 0x01:
2991
    case 0x02:
2992
    case 0x03:
2993
      /* Unique typedefs for module */
2994
      /* GLobal typedefs  */
2995
      /* High level module scope beginning */
2996
      {
2997
        struct output_buffer_struct ob;
2998
        NEXT ();
2999
        OUT (0xf8);
3000
        OUT (ch);
3001
        drop_int (&ob);
3002
        ID;
3003
 
3004
        block ();
3005
 
3006
        NEXT ();
3007
        fill_int (&ob);
3008
        OUT (0xf9);
3009
      }
3010
      break;
3011
    case 0x04:
3012
      /* Global function */
3013
      {
3014
        struct output_buffer_struct ob;
3015
        NEXT ();
3016
        OUT (0xf8);
3017
        OUT (0x04);
3018
        drop_int (&ob);
3019
        ID;
3020
        INTn (stack size);
3021
        INTn (ret val);
3022
        EXPn (offset);
3023
 
3024
        block ();
3025
 
3026
        NEXT ();
3027
        OUT (0xf9);
3028
        EXPn (size of block);
3029
        fill_int (&ob);
3030
      }
3031
      break;
3032
 
3033
    case 0x05:
3034
      /* File name for source line numbers */
3035
      {
3036
        struct output_buffer_struct ob;
3037
        NEXT ();
3038
        OUT (0xf8);
3039
        OUT (0x05);
3040
        drop_int (&ob);
3041
        ID;
3042
        INTn (year);
3043
        INTn (month);
3044
        INTn (day);
3045
        INTn (hour);
3046
        INTn (monute);
3047
        INTn (second);
3048
        block ();
3049
        NEXT ();
3050
        OUT (0xf9);
3051
        fill_int (&ob);
3052
      }
3053
      break;
3054
 
3055
    case 0x06:
3056
      /* Local function */
3057
      {
3058
        struct output_buffer_struct ob;
3059
        NEXT ();
3060
        OUT (0xf8);
3061
        OUT (0x06);
3062
        drop_int (&ob);
3063
        ID;
3064
        INTn (stack size);
3065
        INTn (type return);
3066
        EXPn (offset);
3067
        block ();
3068
        NEXT ();
3069
        OUT (0xf9);
3070
        EXPn (size);
3071
        fill_int (&ob);
3072
      }
3073
      break;
3074
 
3075
    case 0x0a:
3076
      /* Assembler module scope beginning -*/
3077
      {
3078
        struct output_buffer_struct ob;
3079
 
3080
        NEXT ();
3081
        OUT (0xf8);
3082
        OUT (0x0a);
3083
        drop_int (&ob);
3084
        ID;
3085
        ID;
3086
        INT;
3087
        ID;
3088
        INT;
3089
        INT;
3090
        INT;
3091
        INT;
3092
        INT;
3093
        INT;
3094
 
3095
        block ();
3096
 
3097
        NEXT ();
3098
        OUT (0xf9);
3099
        fill_int (&ob);
3100
      }
3101
      break;
3102
    case 0x0b:
3103
      {
3104
        struct output_buffer_struct ob;
3105
        NEXT ();
3106
        OUT (0xf8);
3107
        OUT (0x0b);
3108
        drop_int (&ob);
3109
        ID;
3110
        INT;
3111
        INTn (section index);
3112
        EXPn (offset);
3113
        INTn (stuff);
3114
 
3115
        block ();
3116
 
3117
        OUT (0xf9);
3118
        NEXT ();
3119
        EXPn (Size in Maus);
3120
        fill_int (&ob);
3121
      }
3122
      break;
3123
    }
3124
}
3125
 
3126
static void
3127
e2_record ()
3128
{
3129
  OUT (0xe2);
3130
  NEXT ();
3131
  OUT (0xce);
3132
  NEXT ();
3133
  INT;
3134
  EXP;
3135
}
3136
 
3137
static void
3138
block ()
3139
{
3140
  int ch;
3141
  while (1)
3142
    {
3143
      ch = THIS ();
3144
      switch (ch)
3145
        {
3146
        case 0xe1:
3147
        case 0xe5:
3148
          return;
3149
        case 0xf9:
3150
          return;
3151
        case 0xf0:
3152
          f0_record ();
3153
          break;
3154
        case 0xf1:
3155
          f1_record ();
3156
          break;
3157
        case 0xf2:
3158
          f2_record ();
3159
          break;
3160
        case 0xf8:
3161
          f8_record ();
3162
          break;
3163
        case 0xe2:
3164
          e2_record ();
3165
          break;
3166
 
3167
        }
3168
    }
3169
}
3170
 
3171
 
3172
 
3173
/* relocate_debug,
3174
   moves all the debug information from the source bfd to the output
3175
   bfd, and relocates any expressions it finds
3176
*/
3177
 
3178
static void
3179
relocate_debug (output, input)
3180
     bfd *output ATTRIBUTE_UNUSED;
3181
     bfd *input;
3182
{
3183
#define IBS 400
3184
#define OBS 400
3185
  unsigned char input_buffer[IBS];
3186
 
3187
  input_ptr_start = input_ptr = input_buffer;
3188
  input_ptr_end = input_buffer + IBS;
3189
  input_bfd = input;
3190
  /* FIXME: Check return value.  I'm not sure whether it needs to read
3191
     the entire buffer or not.  */
3192
  bfd_read ((PTR) input_ptr_start, 1, IBS, input);
3193
  block ();
3194
}
3195
 
3196
/* Gather together all the debug information from each input BFD into
3197
   one place, relocating it and emitting it as we go.  */
3198
 
3199
static boolean
3200
ieee_write_debug_part (abfd)
3201
     bfd *abfd;
3202
{
3203
  ieee_data_type *ieee = IEEE_DATA (abfd);
3204
  bfd_chain_type *chain = ieee->chain_root;
3205
  unsigned char output_buffer[OBS];
3206
  boolean some_debug = false;
3207
  file_ptr here = bfd_tell (abfd);
3208
 
3209
  output_ptr_start = output_ptr = output_buffer;
3210
  output_ptr_end = output_buffer + OBS;
3211
  output_ptr = output_buffer;
3212
  output_bfd = abfd;
3213
 
3214
  if (chain == (bfd_chain_type *) NULL)
3215
    {
3216
      asection *s;
3217
 
3218
      for (s = abfd->sections; s != NULL; s = s->next)
3219
        if ((s->flags & SEC_DEBUGGING) != 0)
3220
          break;
3221
      if (s == NULL)
3222
        {
3223
          ieee->w.r.debug_information_part = 0;
3224
          return true;
3225
        }
3226
 
3227
      ieee->w.r.debug_information_part = here;
3228
      if (bfd_write (s->contents, 1, s->_raw_size, abfd) != s->_raw_size)
3229
        return false;
3230
    }
3231
  else
3232
    {
3233
      while (chain != (bfd_chain_type *) NULL)
3234
        {
3235
          bfd *entry = chain->this;
3236
          ieee_data_type *entry_ieee = IEEE_DATA (entry);
3237
          if (entry_ieee->w.r.debug_information_part)
3238
            {
3239
              if (bfd_seek (entry, entry_ieee->w.r.debug_information_part,
3240
                            SEEK_SET)
3241
                  != 0)
3242
                return false;
3243
              relocate_debug (abfd, entry);
3244
            }
3245
 
3246
          chain = chain->next;
3247
        }
3248
      if (some_debug)
3249
        {
3250
          ieee->w.r.debug_information_part = here;
3251
        }
3252
      else
3253
        {
3254
          ieee->w.r.debug_information_part = 0;
3255
        }
3256
 
3257
      flush ();
3258
    }
3259
 
3260
  return true;
3261
}
3262
 
3263
/* Write the data in an ieee way.  */
3264
 
3265
static boolean
3266
ieee_write_data_part (abfd)
3267
     bfd *abfd;
3268
{
3269
  asection *s;
3270
  ieee_data_type *ieee = IEEE_DATA (abfd);
3271
  ieee->w.r.data_part = bfd_tell (abfd);
3272
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3273
    {
3274
      /* Skip sections that have no loadable contents (.bss,
3275
         debugging, etc.)  */
3276
      if ((s->flags & SEC_LOAD) == 0)
3277
        continue;
3278
 
3279
      /* Sort the reloc records so we can insert them in the correct
3280
         places */
3281
      if (s->reloc_count != 0)
3282
        {
3283
          if (! do_with_relocs (abfd, s))
3284
            return false;
3285
        }
3286
      else
3287
        {
3288
          if (! do_without_relocs (abfd, s))
3289
            return false;
3290
        }
3291
    }
3292
 
3293
  return true;
3294
}
3295
 
3296
 
3297
static boolean
3298
init_for_output (abfd)
3299
     bfd *abfd;
3300
{
3301
  asection *s;
3302
  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3303
    {
3304
      if ((s->flags & SEC_DEBUGGING) != 0)
3305
        continue;
3306
      if (s->_raw_size != 0)
3307
        {
3308
          ieee_per_section (s)->data = (bfd_byte *) (bfd_alloc (abfd, s->_raw_size));
3309
          if (!ieee_per_section (s)->data)
3310
            return false;
3311
        }
3312
    }
3313
  return true;
3314
}
3315
 
3316
/** exec and core file sections */
3317
 
3318
/* set section contents is complicated with IEEE since the format is
3319
* not a byte image, but a record stream.
3320
*/
3321
boolean
3322
ieee_set_section_contents (abfd, section, location, offset, count)
3323
     bfd *abfd;
3324
     sec_ptr section;
3325
     PTR location;
3326
     file_ptr offset;
3327
     bfd_size_type count;
3328
{
3329
  if ((section->flags & SEC_DEBUGGING) != 0)
3330
    {
3331
      if (section->contents == NULL)
3332
        {
3333
          section->contents = ((unsigned char *)
3334
                               bfd_alloc (abfd, section->_raw_size));
3335
          if (section->contents == NULL)
3336
            return false;
3337
        }
3338
      /* bfd_set_section_contents has already checked that everything
3339
         is within range.  */
3340
      memcpy (section->contents + offset, location, count);
3341
      return true;
3342
    }
3343
 
3344
  if (ieee_per_section (section)->data == (bfd_byte *) NULL)
3345
    {
3346
      if (!init_for_output (abfd))
3347
        return false;
3348
    }
3349
  memcpy ((PTR) (ieee_per_section (section)->data + offset),
3350
          (PTR) location,
3351
          (unsigned int) count);
3352
  return true;
3353
}
3354
 
3355
/* Write the external symbols of a file.  IEEE considers two sorts of
3356
   external symbols, public, and referenced.  It uses to internal
3357
   forms to index them as well.  When we write them out we turn their
3358
   symbol values into indexes from the right base.  */
3359
 
3360
static boolean
3361
ieee_write_external_part (abfd)
3362
     bfd *abfd;
3363
{
3364
  asymbol **q;
3365
  ieee_data_type *ieee = IEEE_DATA (abfd);
3366
 
3367
  unsigned int reference_index = IEEE_REFERENCE_BASE;
3368
  unsigned int public_index = IEEE_PUBLIC_BASE + 2;
3369
  file_ptr here = bfd_tell (abfd);
3370
  boolean hadone = false;
3371
  if (abfd->outsymbols != (asymbol **) NULL)
3372
    {
3373
 
3374
      for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++)
3375
        {
3376
          asymbol *p = *q;
3377
          if (bfd_is_und_section (p->section))
3378
            {
3379
              /* This must be a symbol reference .. */
3380
              if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3381
                  || ! ieee_write_int (abfd, reference_index)
3382
                  || ! ieee_write_id (abfd, p->name))
3383
                return false;
3384
              p->value = reference_index;
3385
              reference_index++;
3386
              hadone = true;
3387
            }
3388
          else if (bfd_is_com_section (p->section))
3389
            {
3390
              /* This is a weak reference */
3391
              if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3392
                  || ! ieee_write_int (abfd, reference_index)
3393
                  || ! ieee_write_id (abfd, p->name)
3394
                  || ! ieee_write_byte (abfd,
3395
                                        ieee_weak_external_reference_enum)
3396
                  || ! ieee_write_int (abfd, reference_index)
3397
                  || ! ieee_write_int (abfd, p->value))
3398
                return false;
3399
              p->value = reference_index;
3400
              reference_index++;
3401
              hadone = true;
3402
            }
3403
          else if (p->flags & BSF_GLOBAL)
3404
            {
3405
              /* This must be a symbol definition */
3406
 
3407
              if (! ieee_write_byte (abfd, ieee_external_symbol_enum)
3408
                  || ! ieee_write_int (abfd, public_index)
3409
                  || ! ieee_write_id (abfd, p->name)
3410
                  || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum)
3411
                  || ! ieee_write_int (abfd, public_index)
3412
                  || ! ieee_write_byte (abfd, 15) /* instruction address */
3413
                  || ! ieee_write_byte (abfd, 19) /* static symbol */
3414
                  || ! ieee_write_byte (abfd, 1)) /* one of them */
3415
                return false;
3416
 
3417
              /* Write out the value */
3418
              if (! ieee_write_2bytes (abfd, ieee_value_record_enum)
3419
                  || ! ieee_write_int (abfd, public_index))
3420
                return false;
3421
              if (! bfd_is_abs_section (p->section))
3422
                {
3423
                  if (abfd->flags & EXEC_P)
3424
                    {
3425
                      /* If fully linked, then output all symbols
3426
                         relocated */
3427
                      if (! (ieee_write_int
3428
                             (abfd,
3429
                              (p->value
3430
                               + p->section->output_offset
3431
                               + p->section->output_section->vma))))
3432
                        return false;
3433
                    }
3434
                  else
3435
                    {
3436
                      if (! (ieee_write_expression
3437
                             (abfd,
3438
                              p->value + p->section->output_offset,
3439
                              p->section->output_section->symbol,
3440
                              false, 0)))
3441
                        return false;
3442
                    }
3443
                }
3444
              else
3445
                {
3446
                  if (! ieee_write_expression (abfd,
3447
                                               p->value,
3448
                                               bfd_abs_section_ptr->symbol,
3449
                                               false, 0))
3450
                    return false;
3451
                }
3452
              p->value = public_index;
3453
              public_index++;
3454
              hadone = true;
3455
            }
3456
          else
3457
            {
3458
              /* This can happen - when there are gaps in the symbols read */
3459
              /* from an input ieee file */
3460
            }
3461
        }
3462
    }
3463
  if (hadone)
3464
    ieee->w.r.external_part = here;
3465
 
3466
  return true;
3467
}
3468
 
3469
 
3470
static CONST unsigned char exten[] =
3471
{
3472
  0xf0, 0x20, 0x00,
3473
  0xf1, 0xce, 0x20, 0x00, 37, 3, 3,     /* Set version 3 rev 3          */
3474
  0xf1, 0xce, 0x20, 0x00, 39, 2,/* keep symbol in  original case */
3475
  0xf1, 0xce, 0x20, 0x00, 38    /* set object type relocateable to x */
3476
};
3477
 
3478
static CONST unsigned char envi[] =
3479
{
3480
  0xf0, 0x21, 0x00,
3481
 
3482
/*    0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
3483
    0x19, 0x2c,
3484
*/
3485
  0xf1, 0xce, 0x21, 00, 52, 0x00,       /* exec ok */
3486
 
3487
  0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix */
3488
/*    0xf1, 0xce, 0x21, 0, 54, 2,1,1    tool & version # */
3489
};
3490
 
3491
static boolean
3492
ieee_write_me_part (abfd)
3493
     bfd *abfd;
3494
{
3495
  ieee_data_type *ieee = IEEE_DATA (abfd);
3496
  ieee->w.r.trailer_part = bfd_tell (abfd);
3497
  if (abfd->start_address)
3498
    {
3499
      if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum)
3500
          || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum)
3501
          || ! ieee_write_int (abfd, abfd->start_address)
3502
          || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum))
3503
        return false;
3504
    }
3505
  ieee->w.r.me_record = bfd_tell (abfd);
3506
  if (! ieee_write_byte (abfd, ieee_module_end_enum))
3507
    return false;
3508
  return true;
3509
}
3510
 
3511
/* Write out the IEEE processor ID.  */
3512
 
3513
static boolean
3514
ieee_write_processor (abfd)
3515
     bfd *abfd;
3516
{
3517
  const bfd_arch_info_type *arch;
3518
 
3519
  arch = bfd_get_arch_info (abfd);
3520
  switch (arch->arch)
3521
    {
3522
    default:
3523
      if (! ieee_write_id (abfd, bfd_printable_name (abfd)))
3524
        return false;
3525
      break;
3526
 
3527
    case bfd_arch_a29k:
3528
      if (! ieee_write_id (abfd, "29000"))
3529
        return false;
3530
      break;
3531
 
3532
    case bfd_arch_h8300:
3533
      if (! ieee_write_id (abfd, "H8/300"))
3534
        return false;
3535
      break;
3536
 
3537
    case bfd_arch_h8500:
3538
      if (! ieee_write_id (abfd, "H8/500"))
3539
        return false;
3540
      break;
3541
 
3542
    case bfd_arch_i960:
3543
      switch (arch->mach)
3544
        {
3545
        default:
3546
        case bfd_mach_i960_core:
3547
        case bfd_mach_i960_ka_sa:
3548
          if (! ieee_write_id (abfd, "80960KA"))
3549
            return false;
3550
          break;
3551
 
3552
        case bfd_mach_i960_kb_sb:
3553
          if (! ieee_write_id (abfd, "80960KB"))
3554
            return false;
3555
          break;
3556
 
3557
        case bfd_mach_i960_ca:
3558
          if (! ieee_write_id (abfd, "80960CA"))
3559
            return false;
3560
          break;
3561
 
3562
        case bfd_mach_i960_mc:
3563
        case bfd_mach_i960_xa:
3564
          if (! ieee_write_id (abfd, "80960MC"))
3565
            return false;
3566
          break;
3567
        }
3568
      break;
3569
 
3570
    case bfd_arch_m68k:
3571
      {
3572
        const char *id;
3573
 
3574
        switch (arch->mach)
3575
          {
3576
          default:              id = "68020"; break;
3577
          case bfd_mach_m68000: id = "68000"; break;
3578
          case bfd_mach_m68008: id = "68008"; break;
3579
          case bfd_mach_m68010: id = "68010"; break;
3580
          case bfd_mach_m68020: id = "68020"; break;
3581
          case bfd_mach_m68030: id = "68030"; break;
3582
          case bfd_mach_m68040: id = "68040"; break;
3583
          case bfd_mach_m68060: id = "68060"; break;
3584
          case bfd_mach_cpu32:  id = "cpu32"; break;
3585
          case bfd_mach_mcf5200:id = "5200";  break;
3586
          case bfd_mach_mcf5206e:id = "5206e"; break;
3587
          case bfd_mach_mcf5307:id = "5307";  break;
3588
          case bfd_mach_mcf5407:id = "5407";  break;
3589
          }
3590
 
3591
        if (! ieee_write_id (abfd, id))
3592
          return false;
3593
      }
3594
      break;
3595
    }
3596
 
3597
  return true;
3598
}
3599
 
3600
boolean
3601
ieee_write_object_contents (abfd)
3602
     bfd *abfd;
3603
{
3604
  ieee_data_type *ieee = IEEE_DATA (abfd);
3605
  unsigned int i;
3606
  file_ptr old;
3607
 
3608
  /* Fast forward over the header area */
3609
  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3610
    return false;
3611
 
3612
  if (! ieee_write_byte (abfd, ieee_module_beginning_enum)
3613
      || ! ieee_write_processor (abfd)
3614
      || ! ieee_write_id (abfd, abfd->filename))
3615
    return false;
3616
 
3617
  /* Fast forward over the variable bits */
3618
  if (! ieee_write_byte (abfd, ieee_address_descriptor_enum))
3619
    return false;
3620
 
3621
  /* Bits per MAU */
3622
  if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd))))
3623
    return false;
3624
  /* MAU's per address */
3625
  if (! ieee_write_byte (abfd,
3626
                         (bfd_byte) (bfd_arch_bits_per_address (abfd)
3627
                                     / bfd_arch_bits_per_byte (abfd))))
3628
    return false;
3629
 
3630
  old = bfd_tell (abfd);
3631
  if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0)
3632
    return false;
3633
 
3634
  ieee->w.r.extension_record = bfd_tell (abfd);
3635
  if (bfd_write ((char *) exten, 1, sizeof (exten), abfd) != sizeof (exten))
3636
    return false;
3637
  if (abfd->flags & EXEC_P)
3638
    {
3639
      if (! ieee_write_byte (abfd, 0x1)) /* Absolute */
3640
        return false;
3641
    }
3642
  else
3643
    {
3644
      if (! ieee_write_byte (abfd, 0x2)) /* Relocateable */
3645
        return false;
3646
    }
3647
 
3648
  ieee->w.r.environmental_record = bfd_tell (abfd);
3649
  if (bfd_write ((char *) envi, 1, sizeof (envi), abfd) != sizeof (envi))
3650
    return false;
3651
 
3652
  /* The HP emulator database requires a timestamp in the file.  */
3653
  {
3654
    time_t now;
3655
    const struct tm *t;
3656
 
3657
    time (&now);
3658
    t = (struct tm *) localtime (&now);
3659
    if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum)
3660
        || ! ieee_write_byte (abfd, 0x21)
3661
        || ! ieee_write_byte (abfd, 0)
3662
        || ! ieee_write_byte (abfd, 50)
3663
        || ! ieee_write_int (abfd, t->tm_year + 1900)
3664
        || ! ieee_write_int (abfd, t->tm_mon + 1)
3665
        || ! ieee_write_int (abfd, t->tm_mday)
3666
        || ! ieee_write_int (abfd, t->tm_hour)
3667
        || ! ieee_write_int (abfd, t->tm_min)
3668
        || ! ieee_write_int (abfd, t->tm_sec))
3669
      return false;
3670
  }
3671
 
3672
  output_bfd = abfd;
3673
 
3674
  flush ();
3675
 
3676
  if (! ieee_write_section_part (abfd))
3677
    return false;
3678
  /* First write the symbols.  This changes their values into table
3679
    indeces so we cant use it after this point.  */
3680
  if (! ieee_write_external_part (abfd))
3681
    return false;
3682
 
3683
  /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3684
 
3685
  /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3686
 
3687
 
3688
  /* Write any debugs we have been told about.  */
3689
  if (! ieee_write_debug_part (abfd))
3690
    return false;
3691
 
3692
  /* Can only write the data once the symbols have been written, since
3693
     the data contains relocation information which points to the
3694
     symbols.  */
3695
  if (! ieee_write_data_part (abfd))
3696
    return false;
3697
 
3698
  /* At the end we put the end!  */
3699
  if (! ieee_write_me_part (abfd))
3700
    return false;
3701
 
3702
  /* Generate the header */
3703
  if (bfd_seek (abfd, old, SEEK_SET) != 0)
3704
    return false;
3705
 
3706
  for (i = 0; i < N_W_VARIABLES; i++)
3707
    {
3708
      if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum)
3709
          || ! ieee_write_byte (abfd, (bfd_byte) i)
3710
          || ! ieee_write_int5_out (abfd, ieee->w.offset[i]))
3711
        return false;
3712
    }
3713
 
3714
  return true;
3715
}
3716
 
3717
/* Native-level interface to symbols. */
3718
 
3719
/* We read the symbols into a buffer, which is discarded when this
3720
   function exits.  We read the strings into a buffer large enough to
3721
   hold them all plus all the cached symbol entries. */
3722
 
3723
asymbol *
3724
ieee_make_empty_symbol (abfd)
3725
     bfd *abfd;
3726
{
3727
  ieee_symbol_type *new =
3728
    (ieee_symbol_type *) bfd_zalloc (abfd, sizeof (ieee_symbol_type));
3729
  if (!new)
3730
    return NULL;
3731
  new->symbol.the_bfd = abfd;
3732
  return &new->symbol;
3733
}
3734
 
3735
static bfd *
3736
ieee_openr_next_archived_file (arch, prev)
3737
     bfd *arch;
3738
     bfd *prev;
3739
{
3740
  ieee_ar_data_type *ar = IEEE_AR_DATA (arch);
3741
  /* take the next one from the arch state, or reset */
3742
  if (prev == (bfd *) NULL)
3743
    {
3744
      /* Reset the index - the first two entries are bogus*/
3745
      ar->element_index = 2;
3746
    }
3747
  while (true)
3748
    {
3749
      ieee_ar_obstack_type *p = ar->elements + ar->element_index;
3750
      ar->element_index++;
3751
      if (ar->element_index <= ar->element_count)
3752
        {
3753
          if (p->file_offset != (file_ptr) 0)
3754
            {
3755
              if (p->abfd == (bfd *) NULL)
3756
                {
3757
                  p->abfd = _bfd_create_empty_archive_element_shell (arch);
3758
                  p->abfd->origin = p->file_offset;
3759
                }
3760
              return p->abfd;
3761
            }
3762
        }
3763
      else
3764
        {
3765
          bfd_set_error (bfd_error_no_more_archived_files);
3766
          return (bfd *) NULL;
3767
        }
3768
 
3769
    }
3770
}
3771
 
3772
static boolean
3773
ieee_find_nearest_line (abfd,
3774
                        section,
3775
                        symbols,
3776
                        offset,
3777
                        filename_ptr,
3778
                        functionname_ptr,
3779
                        line_ptr)
3780
     bfd *abfd ATTRIBUTE_UNUSED;
3781
     asection *section ATTRIBUTE_UNUSED;
3782
     asymbol **symbols ATTRIBUTE_UNUSED;
3783
     bfd_vma offset ATTRIBUTE_UNUSED;
3784
     const char **filename_ptr ATTRIBUTE_UNUSED;
3785
     const char **functionname_ptr ATTRIBUTE_UNUSED;
3786
     unsigned int *line_ptr ATTRIBUTE_UNUSED;
3787
{
3788
  return false;
3789
}
3790
 
3791
static int
3792
ieee_generic_stat_arch_elt (abfd, buf)
3793
     bfd *abfd;
3794
     struct stat *buf;
3795
{
3796
  ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL;
3797
  ieee_data_type *ieee;
3798
 
3799
  if (abfd->my_archive != NULL)
3800
    ar = abfd->my_archive->tdata.ieee_ar_data;
3801
  if (ar == (ieee_ar_data_type *) NULL)
3802
    {
3803
      bfd_set_error (bfd_error_invalid_operation);
3804
      return -1;
3805
    }
3806
 
3807
  if (IEEE_DATA (abfd) == NULL)
3808
    {
3809
      if (ieee_object_p (abfd) == NULL)
3810
        {
3811
          bfd_set_error (bfd_error_wrong_format);
3812
          return -1;
3813
        }
3814
    }
3815
 
3816
  ieee = IEEE_DATA (abfd);
3817
 
3818
  buf->st_size = ieee->w.r.me_record + 1;
3819
  buf->st_mode = 0644;
3820
  return 0;
3821
}
3822
 
3823
static int
3824
ieee_sizeof_headers (abfd, x)
3825
     bfd *abfd ATTRIBUTE_UNUSED;
3826
     boolean x ATTRIBUTE_UNUSED;
3827
{
3828
  return 0;
3829
}
3830
 
3831
 
3832
/* The debug info routines are never used.  */
3833
#if 0
3834
 
3835
static void
3836
ieee_bfd_debug_info_start (abfd)
3837
     bfd *abfd;
3838
{
3839
 
3840
}
3841
 
3842
static void
3843
ieee_bfd_debug_info_end (abfd)
3844
     bfd *abfd;
3845
{
3846
 
3847
}
3848
 
3849
 
3850
/* Add this section to the list of sections we have debug info for, to
3851
   be ready to output it at close time
3852
   */
3853
static void
3854
ieee_bfd_debug_info_accumulate (abfd, section)
3855
     bfd *abfd;
3856
     asection *section;
3857
{
3858
  ieee_data_type *ieee = IEEE_DATA (section->owner);
3859
  ieee_data_type *output_ieee = IEEE_DATA (abfd);
3860
  /* can only accumulate data from other ieee bfds */
3861
  if (section->owner->xvec != abfd->xvec)
3862
    return;
3863
  /* Only bother once per bfd */
3864
  if (ieee->done_debug == true)
3865
    return;
3866
  ieee->done_debug = true;
3867
 
3868
  /* Don't bother if there is no debug info */
3869
  if (ieee->w.r.debug_information_part == 0)
3870
    return;
3871
 
3872
 
3873
  /* Add to chain */
3874
  {
3875
    bfd_chain_type *n = (bfd_chain_type *) bfd_alloc (abfd, sizeof (bfd_chain_type));
3876
    if (!n)
3877
      abort ();         /* FIXME */
3878
    n->this = section->owner;
3879
    n->next = (bfd_chain_type *) NULL;
3880
 
3881
    if (output_ieee->chain_head)
3882
      {
3883
        output_ieee->chain_head->next = n;
3884
      }
3885
    else
3886
      {
3887
        output_ieee->chain_root = n;
3888
 
3889
      }
3890
    output_ieee->chain_head = n;
3891
  }
3892
}
3893
 
3894
#endif
3895
 
3896
#define ieee_close_and_cleanup _bfd_generic_close_and_cleanup
3897
#define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3898
 
3899
#define ieee_slurp_armap bfd_true
3900
#define ieee_slurp_extended_name_table bfd_true
3901
#define ieee_construct_extended_name_table \
3902
  ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
3903
   bfd_true)
3904
#define ieee_truncate_arname bfd_dont_truncate_arname
3905
#define ieee_write_armap \
3906
  ((boolean (*) \
3907
    PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
3908
   bfd_true)
3909
#define ieee_read_ar_hdr bfd_nullvoidptr
3910
#define ieee_update_armap_timestamp bfd_true
3911
#define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
3912
 
3913
#define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
3914
#define ieee_get_lineno _bfd_nosymbols_get_lineno
3915
#define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3916
#define ieee_read_minisymbols _bfd_generic_read_minisymbols
3917
#define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3918
 
3919
#define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3920
 
3921
#define ieee_set_arch_mach _bfd_generic_set_arch_mach
3922
 
3923
#define ieee_get_section_contents_in_window \
3924
  _bfd_generic_get_section_contents_in_window
3925
#define ieee_bfd_get_relocated_section_contents \
3926
  bfd_generic_get_relocated_section_contents
3927
#define ieee_bfd_relax_section bfd_generic_relax_section
3928
#define ieee_bfd_gc_sections bfd_generic_gc_sections
3929
#define ieee_bfd_merge_sections bfd_generic_merge_sections
3930
#define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3931
#define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
3932
#define ieee_bfd_final_link _bfd_generic_final_link
3933
#define ieee_bfd_link_split_section  _bfd_generic_link_split_section
3934
 
3935
/*SUPPRESS 460 */
3936
const bfd_target ieee_vec =
3937
{
3938
  "ieee",                       /* name */
3939
  bfd_target_ieee_flavour,
3940
  BFD_ENDIAN_UNKNOWN,           /* target byte order */
3941
  BFD_ENDIAN_UNKNOWN,           /* target headers byte order */
3942
  (HAS_RELOC | EXEC_P |         /* object flags */
3943
   HAS_LINENO | HAS_DEBUG |
3944
   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3945
  (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3946
   | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
3947
  '_',                          /* leading underscore */
3948
  ' ',                          /* ar_pad_char */
3949
  16,                           /* ar_max_namelen */
3950
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3951
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3952
  bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* data */
3953
  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3954
  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3955
  bfd_getb16, bfd_getb_signed_16, bfd_putb16,   /* hdrs */
3956
 
3957
  {_bfd_dummy_target,
3958
   ieee_object_p,               /* bfd_check_format */
3959
   ieee_archive_p,
3960
   _bfd_dummy_target,
3961
  },
3962
  {
3963
    bfd_false,
3964
    ieee_mkobject,
3965
    _bfd_generic_mkarchive,
3966
    bfd_false
3967
  },
3968
  {
3969
    bfd_false,
3970
    ieee_write_object_contents,
3971
    _bfd_write_archive_contents,
3972
    bfd_false,
3973
  },
3974
 
3975
  BFD_JUMP_TABLE_GENERIC (ieee),
3976
  BFD_JUMP_TABLE_COPY (_bfd_generic),
3977
  BFD_JUMP_TABLE_CORE (_bfd_nocore),
3978
  BFD_JUMP_TABLE_ARCHIVE (ieee),
3979
  BFD_JUMP_TABLE_SYMBOLS (ieee),
3980
  BFD_JUMP_TABLE_RELOCS (ieee),
3981
  BFD_JUMP_TABLE_WRITE (ieee),
3982
  BFD_JUMP_TABLE_LINK (ieee),
3983
  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3984
 
3985
  NULL,
3986
 
3987
  (PTR) 0
3988
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.