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