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