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