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