OSDN Git Service

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