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