OSDN Git Service

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