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