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