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