ieee.c revision 38889
1/* BFD back-end for ieee-695 objects.
2   Copyright (C) 1990, 91, 92, 93, 94, 95, 96, 97, 1998
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 == NULL && section != NULL)
1650		    r->relent.sym_ptr_ptr = section->symbol_ptr_ptr;
1651
1652		  if (this_byte (&(ieee->h)) == (int) ieee_comma)
1653		    {
1654		      next_byte (&(ieee->h));
1655		      /* Fetch number of bytes to pad */
1656		      extra = must_parse_int (&(ieee->h));
1657		    };
1658
1659		  switch (this_byte (&(ieee->h)))
1660		    {
1661		    case ieee_function_signed_close_b_enum:
1662		      next_byte (&(ieee->h));
1663		      break;
1664		    case ieee_function_unsigned_close_b_enum:
1665		      next_byte (&(ieee->h));
1666		      break;
1667		    case ieee_function_either_close_b_enum:
1668		      next_byte (&(ieee->h));
1669		      break;
1670		    default:
1671		      break;
1672		    }
1673		  /* Build a relocation entry for this type */
1674		  /* If pc rel then stick -ve pc into instruction
1675		     and take out of reloc ..
1676
1677		     I've changed this. It's all too complicated. I
1678		     keep 0 in the instruction now.  */
1679
1680		  switch (extra)
1681		    {
1682		    case 0:
1683		    case 4:
1684
1685		      if (pcrel == true)
1686			{
1687#if KEEPMINUSPCININST
1688			  bfd_put_32 (ieee->h.abfd, -current_map->pc, location_ptr +
1689				      current_map->pc);
1690			  r->relent.howto = &rel32_howto;
1691			  r->relent.addend -=
1692			    current_map->pc;
1693#else
1694			  bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1695				      current_map->pc);
1696			  r->relent.howto = &rel32_howto;
1697#endif
1698			}
1699		      else
1700			{
1701			  bfd_put_32 (ieee->h.abfd, 0, location_ptr +
1702				      current_map->pc);
1703			  r->relent.howto = &abs32_howto;
1704			}
1705		      current_map->pc += 4;
1706		      break;
1707		    case 2:
1708		      if (pcrel == true)
1709			{
1710#if KEEPMINUSPCININST
1711			  bfd_put_16 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1712			  r->relent.addend -= current_map->pc;
1713			  r->relent.howto = &rel16_howto;
1714#else
1715
1716			  bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1717			  r->relent.howto = &rel16_howto;
1718#endif
1719			}
1720
1721		      else
1722			{
1723			  bfd_put_16 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1724			  r->relent.howto = &abs16_howto;
1725			}
1726		      current_map->pc += 2;
1727		      break;
1728		    case 1:
1729		      if (pcrel == true)
1730			{
1731#if KEEPMINUSPCININST
1732			  bfd_put_8 (ieee->h.abfd, (int) (-current_map->pc), location_ptr + current_map->pc);
1733			  r->relent.addend -= current_map->pc;
1734			  r->relent.howto = &rel8_howto;
1735#else
1736			  bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1737			  r->relent.howto = &rel8_howto;
1738#endif
1739			}
1740		      else
1741			{
1742			  bfd_put_8 (ieee->h.abfd, 0, location_ptr + current_map->pc);
1743			  r->relent.howto = &abs8_howto;
1744			}
1745		      current_map->pc += 1;
1746		      break;
1747
1748		    default:
1749		      BFD_FAIL ();
1750		      return false;
1751		    }
1752		}
1753		break;
1754	      default:
1755		{
1756		  bfd_vma this_size;
1757		  if (parse_int (&(ieee->h), &this_size) == true)
1758		    {
1759		      unsigned int i;
1760		      for (i = 0; i < this_size; i++)
1761			{
1762			  location_ptr[current_map->pc++] = this_byte (&(ieee->h));
1763			  next_byte (&(ieee->h));
1764			}
1765		    }
1766		  else
1767		    {
1768		      loop = false;
1769		    }
1770		}
1771	      }
1772
1773	    /* Prevent more than the first load-item of an LR record
1774	       from being repeated (MRI convention). */
1775	    if (iterations != 1)
1776	      loop = false;
1777	  }
1778      }
1779    }
1780  return true;
1781}
1782
1783/* Read in all the section data and relocation stuff too */
1784static boolean
1785ieee_slurp_section_data (abfd)
1786     bfd *abfd;
1787{
1788  bfd_byte *location_ptr = (bfd_byte *) NULL;
1789  ieee_data_type *ieee = IEEE_DATA (abfd);
1790  unsigned int section_number;
1791
1792  ieee_per_section_type *current_map = (ieee_per_section_type *) NULL;
1793  asection *s;
1794  /* Seek to the start of the data area */
1795  if (ieee->read_data == true)
1796    return true;
1797  ieee->read_data = true;
1798  ieee_seek (abfd, ieee->w.r.data_part);
1799
1800  /* Allocate enough space for all the section contents */
1801
1802  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
1803    {
1804      ieee_per_section_type *per = (ieee_per_section_type *) s->used_by_bfd;
1805      if ((s->flags & SEC_DEBUGGING) != 0)
1806	continue;
1807      per->data = (bfd_byte *) bfd_alloc (ieee->h.abfd, s->_raw_size);
1808      if (!per->data)
1809	return false;
1810      /*SUPPRESS 68*/
1811      per->reloc_tail_ptr =
1812	(ieee_reloc_type **) & (s->relocation);
1813    }
1814
1815  while (true)
1816    {
1817      switch (this_byte (&(ieee->h)))
1818	{
1819	  /* IF we see anything strange then quit */
1820	default:
1821	  return true;
1822
1823	case ieee_set_current_section_enum:
1824	  next_byte (&(ieee->h));
1825	  section_number = must_parse_int (&(ieee->h));
1826	  s = ieee->section_table[section_number];
1827	  s->flags |= SEC_LOAD | SEC_HAS_CONTENTS;
1828	  current_map = (ieee_per_section_type *) s->used_by_bfd;
1829	  location_ptr = current_map->data - s->vma;
1830	  /* The document I have says that Microtec's compilers reset */
1831	  /* this after a sec section, even though the standard says not */
1832	  /* to. SO .. */
1833	  current_map->pc = s->vma;
1834	  break;
1835
1836	case ieee_e2_first_byte_enum:
1837	  next_byte (&(ieee->h));
1838	  switch (this_byte (&(ieee->h)))
1839	    {
1840	    case ieee_set_current_pc_enum & 0xff:
1841	      {
1842		bfd_vma value;
1843		ieee_symbol_index_type symbol;
1844		unsigned int extra;
1845		boolean pcrel;
1846		next_byte (&(ieee->h));
1847		must_parse_int (&(ieee->h));	/* Thow away section #*/
1848		parse_expression (ieee, &value,
1849				  &symbol,
1850				  &pcrel, &extra,
1851				  0);
1852		current_map->pc = value;
1853		BFD_ASSERT ((unsigned) (value - s->vma) <= s->_raw_size);
1854	      }
1855	      break;
1856
1857	    case ieee_value_starting_address_enum & 0xff:
1858	      next_byte (&(ieee->h));
1859	      if (this_byte (&(ieee->h)) == ieee_function_either_open_b_enum)
1860		next_byte (&(ieee->h));
1861	      abfd->start_address = must_parse_int (&(ieee->h));
1862	      /* We've got to the end of the data now - */
1863	      return true;
1864	    default:
1865	      BFD_FAIL ();
1866	      return false;
1867	    }
1868	  break;
1869	case ieee_repeat_data_enum:
1870	  {
1871	    /* Repeat the following LD or LR n times - we do this by
1872		 remembering the stream pointer before running it and
1873		 resetting it and running it n times. We special case
1874		 the repetition of a repeat_data/load_constant
1875		 */
1876
1877	    unsigned int iterations;
1878	    unsigned char *start;
1879	    next_byte (&(ieee->h));
1880	    iterations = must_parse_int (&(ieee->h));
1881	    start = ieee->h.input_p;
1882	    if (start[0] == (int) ieee_load_constant_bytes_enum &&
1883		start[1] == 1)
1884	      {
1885		while (iterations != 0)
1886		  {
1887		    location_ptr[current_map->pc++] = start[2];
1888		    iterations--;
1889		  }
1890		next_byte (&(ieee->h));
1891		next_byte (&(ieee->h));
1892		next_byte (&(ieee->h));
1893	      }
1894	    else
1895	      {
1896		while (iterations != 0)
1897		  {
1898		    ieee->h.input_p = start;
1899		    if (!do_one (ieee, current_map, location_ptr, s,
1900				 iterations))
1901		      return false;
1902		    iterations--;
1903		  }
1904	      }
1905	  }
1906	  break;
1907	case ieee_load_constant_bytes_enum:
1908	case ieee_load_with_relocation_enum:
1909	  {
1910	    if (!do_one (ieee, current_map, location_ptr, s, 1))
1911	      return false;
1912	  }
1913	}
1914    }
1915}
1916
1917boolean
1918ieee_new_section_hook (abfd, newsect)
1919     bfd *abfd;
1920     asection *newsect;
1921{
1922  newsect->used_by_bfd = (PTR)
1923    bfd_alloc (abfd, sizeof (ieee_per_section_type));
1924  if (!newsect->used_by_bfd)
1925    return false;
1926  ieee_per_section (newsect)->data = (bfd_byte *) NULL;
1927  ieee_per_section (newsect)->section = newsect;
1928  return true;
1929}
1930
1931long
1932ieee_get_reloc_upper_bound (abfd, asect)
1933     bfd *abfd;
1934     sec_ptr asect;
1935{
1936  if ((asect->flags & SEC_DEBUGGING) != 0)
1937    return 0;
1938  if (! ieee_slurp_section_data (abfd))
1939    return -1;
1940  return (asect->reloc_count + 1) * sizeof (arelent *);
1941}
1942
1943static boolean
1944ieee_get_section_contents (abfd, section, location, offset, count)
1945     bfd *abfd;
1946     sec_ptr section;
1947     PTR location;
1948     file_ptr offset;
1949     bfd_size_type count;
1950{
1951  ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;
1952  if ((section->flags & SEC_DEBUGGING) != 0)
1953    return _bfd_generic_get_section_contents (abfd, section, location,
1954					      offset, count);
1955  ieee_slurp_section_data (abfd);
1956  (void) memcpy ((PTR) location, (PTR) (p->data + offset), (unsigned) count);
1957  return true;
1958}
1959
1960long
1961ieee_canonicalize_reloc (abfd, section, relptr, symbols)
1962     bfd *abfd;
1963     sec_ptr section;
1964     arelent **relptr;
1965     asymbol **symbols;
1966{
1967/*  ieee_per_section_type *p = (ieee_per_section_type *) section->used_by_bfd;*/
1968  ieee_reloc_type *src = (ieee_reloc_type *) (section->relocation);
1969  ieee_data_type *ieee = IEEE_DATA (abfd);
1970
1971  if ((section->flags & SEC_DEBUGGING) != 0)
1972    return 0;
1973
1974  while (src != (ieee_reloc_type *) NULL)
1975    {
1976      /* Work out which symbol to attach it this reloc to */
1977      switch (src->symbol.letter)
1978	{
1979	case 'I':
1980	  src->relent.sym_ptr_ptr =
1981	    symbols + src->symbol.index + ieee->external_symbol_base_offset;
1982	  break;
1983	case 'X':
1984	  src->relent.sym_ptr_ptr =
1985	    symbols + src->symbol.index + ieee->external_reference_base_offset;
1986	  break;
1987	case 0:
1988	  if (src->relent.sym_ptr_ptr != NULL)
1989	    src->relent.sym_ptr_ptr =
1990	      src->relent.sym_ptr_ptr[0]->section->symbol_ptr_ptr;
1991	  break;
1992	default:
1993
1994	  BFD_FAIL ();
1995	}
1996      *relptr++ = &src->relent;
1997      src = src->next;
1998    }
1999  *relptr = (arelent *) NULL;
2000  return section->reloc_count;
2001}
2002
2003static int
2004comp (ap, bp)
2005     CONST PTR ap;
2006     CONST PTR bp;
2007{
2008  arelent *a = *((arelent **) ap);
2009  arelent *b = *((arelent **) bp);
2010  return a->address - b->address;
2011}
2012
2013/* Write the section headers.  */
2014
2015static boolean
2016ieee_write_section_part (abfd)
2017     bfd *abfd;
2018{
2019  ieee_data_type *ieee = IEEE_DATA (abfd);
2020  asection *s;
2021  ieee->w.r.section_part = bfd_tell (abfd);
2022  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
2023    {
2024      if (! bfd_is_abs_section (s)
2025	  && (s->flags & SEC_DEBUGGING) == 0)
2026	{
2027	  if (! ieee_write_byte (abfd, ieee_section_type_enum)
2028	      || ! ieee_write_byte (abfd,
2029				    (bfd_byte) (s->index
2030						+ IEEE_SECTION_NUMBER_BASE)))
2031	    return false;
2032
2033	  if (abfd->flags & EXEC_P)
2034	    {
2035	      /* This image is executable, so output absolute sections */
2036	      if (! ieee_write_byte (abfd, ieee_variable_A_enum)
2037		  || ! ieee_write_byte (abfd, ieee_variable_S_enum))
2038		return false;
2039	    }
2040	  else
2041	    {
2042	      if (! ieee_write_byte (abfd, ieee_variable_C_enum))
2043		return false;
2044	    }
2045
2046	  switch (s->flags & (SEC_CODE | SEC_DATA | SEC_ROM))
2047	    {
2048	    case SEC_CODE | SEC_LOAD:
2049	    case SEC_CODE:
2050	      if (! ieee_write_byte (abfd, ieee_variable_P_enum))
2051		return false;
2052	      break;
2053	    case SEC_DATA:
2054	    default:
2055	      if (! ieee_write_byte (abfd, ieee_variable_D_enum))
2056		return false;
2057	      break;
2058	    case SEC_ROM:
2059	    case SEC_ROM | SEC_DATA:
2060	    case SEC_ROM | SEC_LOAD:
2061	    case SEC_ROM | SEC_DATA | SEC_LOAD:
2062	      if (! ieee_write_byte (abfd, ieee_variable_R_enum))
2063		return false;
2064	    }
2065
2066
2067	  if (! ieee_write_id (abfd, s->name))
2068	    return false;
2069#if 0
2070	  ieee_write_int (abfd, 0);	/* Parent */
2071	  ieee_write_int (abfd, 0);	/* Brother */
2072	  ieee_write_int (abfd, 0);	/* Context */
2073#endif
2074	  /* Alignment */
2075	  if (! ieee_write_byte (abfd, ieee_section_alignment_enum)
2076	      || ! ieee_write_byte (abfd,
2077				    (bfd_byte) (s->index
2078						+ IEEE_SECTION_NUMBER_BASE))
2079	      || ! ieee_write_int (abfd, 1 << s->alignment_power))
2080	    return false;
2081
2082	  /* Size */
2083	  if (! ieee_write_2bytes (abfd, ieee_section_size_enum)
2084	      || ! ieee_write_byte (abfd,
2085				    (bfd_byte) (s->index
2086						+ IEEE_SECTION_NUMBER_BASE))
2087	      || ! ieee_write_int (abfd, s->_raw_size))
2088	    return false;
2089	  if (abfd->flags & EXEC_P)
2090	    {
2091	      /* Relocateable sections don't have asl records */
2092	      /* Vma */
2093	      if (! ieee_write_2bytes (abfd, ieee_section_base_address_enum)
2094		  || ! ieee_write_byte (abfd,
2095					((bfd_byte)
2096					 (s->index
2097					  + IEEE_SECTION_NUMBER_BASE)))
2098		  || ! ieee_write_int (abfd, s->lma))
2099		return false;
2100	    }
2101	}
2102    }
2103
2104  return true;
2105}
2106
2107
2108static boolean
2109do_with_relocs (abfd, s)
2110     bfd *abfd;
2111     asection *s;
2112{
2113  unsigned int number_of_maus_in_address =
2114    bfd_arch_bits_per_address (abfd) / bfd_arch_bits_per_byte (abfd);
2115  unsigned int relocs_to_go = s->reloc_count;
2116  bfd_byte *stream = ieee_per_section (s)->data;
2117  arelent **p = s->orelocation;
2118  bfd_size_type current_byte_index = 0;
2119
2120  qsort (s->orelocation,
2121	 relocs_to_go,
2122	 sizeof (arelent **),
2123	 comp);
2124
2125  /* Output the section preheader */
2126  if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2127      || ! ieee_write_byte (abfd,
2128			    (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE))
2129      || ! ieee_write_2bytes (abfd, ieee_set_current_pc_enum)
2130      || ! ieee_write_byte (abfd,
2131			    (bfd_byte) (s->index + IEEE_SECTION_NUMBER_BASE)))
2132    return false;
2133  if ((abfd->flags & EXEC_P) != 0 && relocs_to_go == 0)
2134    {
2135      if (! ieee_write_int (abfd, s->lma))
2136	return false;
2137    }
2138  else
2139    {
2140      if (! ieee_write_expression (abfd, 0, s->symbol, 0, 0))
2141	return false;
2142    }
2143
2144  if (relocs_to_go == 0)
2145    {
2146      /* If there aren't any relocations then output the load constant
2147	 byte opcode rather than the load with relocation opcode */
2148
2149      while (current_byte_index < s->_raw_size)
2150	{
2151	  bfd_size_type run;
2152	  unsigned int MAXRUN = 127;
2153	  run = MAXRUN;
2154	  if (run > s->_raw_size - current_byte_index)
2155	    {
2156	      run = s->_raw_size - current_byte_index;
2157	    }
2158
2159	  if (run != 0)
2160	    {
2161	      if (! ieee_write_byte (abfd, ieee_load_constant_bytes_enum))
2162		return false;
2163	      /* Output a stream of bytes */
2164	      if (! ieee_write_int (abfd, run))
2165		return false;
2166	      if (bfd_write ((PTR) (stream + current_byte_index),
2167			     1,
2168			     run,
2169			     abfd)
2170		  != run)
2171		return false;
2172	      current_byte_index += run;
2173	    }
2174	}
2175    }
2176  else
2177    {
2178      if (! ieee_write_byte (abfd, ieee_load_with_relocation_enum))
2179	return false;
2180
2181      /* Output the data stream as the longest sequence of bytes
2182	 possible, allowing for the a reasonable packet size and
2183	 relocation stuffs.  */
2184
2185      if ((PTR) stream == (PTR) NULL)
2186	{
2187	  /* Outputting a section without data, fill it up */
2188	  stream = (unsigned char *) (bfd_alloc (abfd, s->_raw_size));
2189	  if (!stream)
2190	    return false;
2191	  memset ((PTR) stream, 0, (size_t) s->_raw_size);
2192	}
2193      while (current_byte_index < s->_raw_size)
2194	{
2195	  bfd_size_type run;
2196	  unsigned int MAXRUN = 127;
2197	  if (relocs_to_go)
2198	    {
2199	      run = (*p)->address - current_byte_index;
2200	      if (run > MAXRUN)
2201		run = MAXRUN;
2202	    }
2203	  else
2204	    {
2205	      run = MAXRUN;
2206	    }
2207	  if (run > s->_raw_size - current_byte_index)
2208	    {
2209	      run = s->_raw_size - current_byte_index;
2210	    }
2211
2212	  if (run != 0)
2213	    {
2214	      /* Output a stream of bytes */
2215	      if (! ieee_write_int (abfd, run))
2216		return false;
2217	      if (bfd_write ((PTR) (stream + current_byte_index),
2218			     1,
2219			     run,
2220			     abfd)
2221		  != run)
2222		return false;
2223	      current_byte_index += run;
2224	    }
2225	  /* Output any relocations here */
2226	  if (relocs_to_go && (*p) && (*p)->address == current_byte_index)
2227	    {
2228	      while (relocs_to_go
2229		     && (*p) && (*p)->address == current_byte_index)
2230		{
2231		  arelent *r = *p;
2232		  bfd_signed_vma ov;
2233
2234#if 0
2235		  if (r->howto->pc_relative)
2236		    {
2237		      r->addend += current_byte_index;
2238		    }
2239#endif
2240
2241		  switch (r->howto->size)
2242		    {
2243		    case 2:
2244
2245		      ov = bfd_get_signed_32 (abfd,
2246					      stream + current_byte_index);
2247		      current_byte_index += 4;
2248		      break;
2249		    case 1:
2250		      ov = bfd_get_signed_16 (abfd,
2251					      stream + current_byte_index);
2252		      current_byte_index += 2;
2253		      break;
2254		    case 0:
2255		      ov = bfd_get_signed_8 (abfd,
2256					     stream + current_byte_index);
2257		      current_byte_index++;
2258		      break;
2259		    default:
2260		      ov = 0;
2261		      BFD_FAIL ();
2262		      return false;
2263		    }
2264
2265		  ov &= r->howto->src_mask;
2266
2267		  if (r->howto->pc_relative
2268		      && ! r->howto->pcrel_offset)
2269		    ov += r->address;
2270
2271		  if (! ieee_write_byte (abfd,
2272					 ieee_function_either_open_b_enum))
2273		    return false;
2274
2275/*		  abort();*/
2276
2277		  if (r->sym_ptr_ptr != (asymbol **) NULL)
2278		    {
2279		      if (! ieee_write_expression (abfd, r->addend + ov,
2280						   *(r->sym_ptr_ptr),
2281						   r->howto->pc_relative,
2282						   s->index))
2283			return false;
2284		    }
2285		  else
2286		    {
2287		      if (! ieee_write_expression (abfd, r->addend + ov,
2288						   (asymbol *) NULL,
2289						   r->howto->pc_relative,
2290						   s->index))
2291			return false;
2292		    }
2293
2294		  if (number_of_maus_in_address
2295		      != bfd_get_reloc_size (r->howto))
2296		    {
2297		      if (! ieee_write_int (abfd,
2298					    bfd_get_reloc_size (r->howto)))
2299			return false;
2300		    }
2301		  if (! ieee_write_byte (abfd,
2302					 ieee_function_either_close_b_enum))
2303		    return false;
2304
2305		  relocs_to_go--;
2306		  p++;
2307		}
2308
2309	    }
2310	}
2311    }
2312
2313  return true;
2314}
2315
2316/* If there are no relocations in the output section then we can be
2317   clever about how we write.  We block items up into a max of 127
2318   bytes.  */
2319
2320static boolean
2321do_as_repeat (abfd, s)
2322     bfd *abfd;
2323     asection *s;
2324{
2325  if (s->_raw_size)
2326    {
2327      if (! ieee_write_byte (abfd, ieee_set_current_section_enum)
2328	  || ! ieee_write_byte (abfd,
2329				(bfd_byte) (s->index
2330					    + IEEE_SECTION_NUMBER_BASE))
2331	  || ! ieee_write_byte (abfd, ieee_set_current_pc_enum >> 8)
2332	  || ! ieee_write_byte (abfd, ieee_set_current_pc_enum & 0xff)
2333	  || ! ieee_write_byte (abfd,
2334				(bfd_byte) (s->index
2335					    + IEEE_SECTION_NUMBER_BASE))
2336	  || ! ieee_write_int (abfd, s->lma)
2337	  || ! ieee_write_byte (abfd, ieee_repeat_data_enum)
2338	  || ! ieee_write_int (abfd, s->_raw_size)
2339	  || ! ieee_write_byte (abfd, ieee_load_constant_bytes_enum)
2340	  || ! ieee_write_byte (abfd, 1)
2341	  || ! ieee_write_byte (abfd, 0))
2342	return false;
2343    }
2344
2345  return true;
2346}
2347
2348static boolean
2349do_without_relocs (abfd, s)
2350     bfd *abfd;
2351     asection *s;
2352{
2353  bfd_byte *stream = ieee_per_section (s)->data;
2354
2355  if (stream == 0 || ((s->flags & SEC_LOAD) == 0))
2356    {
2357      if (! do_as_repeat (abfd, s))
2358	return false;
2359    }
2360  else
2361    {
2362      unsigned int i;
2363      for (i = 0; i < s->_raw_size; i++)
2364	{
2365	  if (stream[i] != 0)
2366	    {
2367	      if (! do_with_relocs (abfd, s))
2368		return false;
2369	      return true;
2370	    }
2371	}
2372      if (! do_as_repeat (abfd, s))
2373	return false;
2374    }
2375
2376  return true;
2377}
2378
2379
2380static unsigned char *output_ptr_start;
2381static unsigned char *output_ptr;
2382static unsigned char *output_ptr_end;
2383static unsigned char *input_ptr_start;
2384static unsigned char *input_ptr;
2385static unsigned char *input_ptr_end;
2386static bfd *input_bfd;
2387static bfd *output_bfd;
2388static int output_buffer;
2389
2390static void
2391fill ()
2392{
2393  /* FIXME: Check return value.  I'm not sure whether it needs to read
2394     the entire buffer or not.  */
2395  bfd_read ((PTR) input_ptr_start, 1, input_ptr_end - input_ptr_start, input_bfd);
2396  input_ptr = input_ptr_start;
2397}
2398static void
2399flush ()
2400{
2401  if (bfd_write ((PTR) (output_ptr_start), 1, output_ptr - output_ptr_start,
2402		 output_bfd)
2403      != (bfd_size_type) (output_ptr - output_ptr_start))
2404    abort ();
2405  output_ptr = output_ptr_start;
2406  output_buffer++;
2407}
2408
2409#define THIS() ( *input_ptr )
2410#define NEXT() { input_ptr++; if (input_ptr == input_ptr_end) fill(); }
2411#define OUT(x) { *output_ptr++ = (x); if(output_ptr == output_ptr_end)  flush(); }
2412
2413static void
2414write_int (value)
2415     int value;
2416{
2417  if (value >= 0 && value <= 127)
2418    {
2419      OUT (value);
2420    }
2421  else
2422    {
2423      unsigned int length;
2424      /* How many significant bytes ? */
2425      /* FIXME FOR LONGER INTS */
2426      if (value & 0xff000000)
2427	{
2428	  length = 4;
2429	}
2430      else if (value & 0x00ff0000)
2431	{
2432	  length = 3;
2433	}
2434      else if (value & 0x0000ff00)
2435	{
2436	  length = 2;
2437	}
2438      else
2439	length = 1;
2440
2441      OUT ((int) ieee_number_repeat_start_enum + length);
2442      switch (length)
2443	{
2444	case 4:
2445	  OUT (value >> 24);
2446	case 3:
2447	  OUT (value >> 16);
2448	case 2:
2449	  OUT (value >> 8);
2450	case 1:
2451	  OUT (value);
2452	}
2453
2454    }
2455}
2456
2457static void
2458copy_id ()
2459{
2460  int length = THIS ();
2461  char ch;
2462  OUT (length);
2463  NEXT ();
2464  while (length--)
2465    {
2466      ch = THIS ();
2467      OUT (ch);
2468      NEXT ();
2469    }
2470}
2471
2472#define VAR(x) ((x | 0x80))
2473static void
2474copy_expression ()
2475{
2476  int stack[10];
2477  int *tos = stack;
2478  int value = 0;
2479  while (1)
2480    {
2481      switch (THIS ())
2482	{
2483	case 0x84:
2484	  NEXT ();
2485	  value = THIS ();
2486	  NEXT ();
2487	  value = (value << 8) | THIS ();
2488	  NEXT ();
2489	  value = (value << 8) | THIS ();
2490	  NEXT ();
2491	  value = (value << 8) | THIS ();
2492	  NEXT ();
2493	  *tos++ = value;
2494	  break;
2495	case 0x83:
2496	  NEXT ();
2497	  value = THIS ();
2498	  NEXT ();
2499	  value = (value << 8) | THIS ();
2500	  NEXT ();
2501	  value = (value << 8) | THIS ();
2502	  NEXT ();
2503	  *tos++ = value;
2504	  break;
2505	case 0x82:
2506	  NEXT ();
2507	  value = THIS ();
2508	  NEXT ();
2509	  value = (value << 8) | THIS ();
2510	  NEXT ();
2511	  *tos++ = value;
2512	  break;
2513	case 0x81:
2514	  NEXT ();
2515	  value = THIS ();
2516	  NEXT ();
2517	  *tos++ = value;
2518	  break;
2519	case 0x80:
2520	  NEXT ();
2521	  *tos++ = 0;
2522	  break;
2523	default:
2524	  if (THIS () > 0x84)
2525	    {
2526	      /* Not a number, just bug out with the answer */
2527	      write_int (*(--tos));
2528	      return;
2529	    }
2530	  *tos++ = THIS ();
2531	  NEXT ();
2532	  value = 0;
2533	  break;
2534	case 0xa5:
2535	  /* PLUS anything */
2536	  {
2537	    int value = *(--tos);
2538	    value += *(--tos);
2539	    *tos++ = value;
2540	    NEXT ();
2541	  }
2542	  break;
2543	case VAR ('R'):
2544	  {
2545	    int section_number;
2546	    ieee_data_type *ieee;
2547	    asection *s;
2548	    NEXT ();
2549	    section_number = THIS ();
2550
2551	    NEXT ();
2552	    ieee = IEEE_DATA (input_bfd);
2553	    s = ieee->section_table[section_number];
2554	    if (s->output_section)
2555	      {
2556		value = s->output_section->lma;
2557	      }
2558	    else
2559	      {
2560		value = 0;
2561	      }
2562	    value += s->output_offset;
2563	    *tos++ = value;
2564	    value = 0;
2565	  }
2566	  break;
2567	case 0x90:
2568	  {
2569	    NEXT ();
2570	    write_int (*(--tos));
2571	    OUT (0x90);
2572	    return;
2573
2574	  }
2575	}
2576    }
2577
2578}
2579
2580/* Drop the int in the buffer, and copy a null into the gap, which we
2581   will overwrite later */
2582
2583struct output_buffer_struct
2584{
2585  unsigned char *ptrp;
2586  int buffer;
2587};
2588
2589static void
2590fill_int (buf)
2591     struct output_buffer_struct *buf;
2592{
2593  if (buf->buffer == output_buffer)
2594    {
2595      /* Still a chance to output the size */
2596      int value = output_ptr - buf->ptrp + 3;
2597      buf->ptrp[0] = value >> 24;
2598      buf->ptrp[1] = value >> 16;
2599      buf->ptrp[2] = value >> 8;
2600      buf->ptrp[3] = value >> 0;
2601    }
2602}
2603
2604static void
2605drop_int (buf)
2606     struct output_buffer_struct *buf;
2607{
2608  int type = THIS ();
2609  int ch;
2610  if (type <= 0x84)
2611    {
2612      NEXT ();
2613      switch (type)
2614	{
2615	case 0x84:
2616	  ch = THIS ();
2617	  NEXT ();
2618	case 0x83:
2619	  ch = THIS ();
2620	  NEXT ();
2621	case 0x82:
2622	  ch = THIS ();
2623	  NEXT ();
2624	case 0x81:
2625	  ch = THIS ();
2626	  NEXT ();
2627	case 0x80:
2628	  break;
2629	}
2630    }
2631  OUT (0x84);
2632  buf->ptrp = output_ptr;
2633  buf->buffer = output_buffer;
2634  OUT (0);
2635  OUT (0);
2636  OUT (0);
2637  OUT (0);
2638}
2639
2640static void
2641copy_int ()
2642{
2643  int type = THIS ();
2644  int ch;
2645  if (type <= 0x84)
2646    {
2647      OUT (type);
2648      NEXT ();
2649      switch (type)
2650	{
2651	case 0x84:
2652	  ch = THIS ();
2653	  NEXT ();
2654	  OUT (ch);
2655	case 0x83:
2656	  ch = THIS ();
2657	  NEXT ();
2658	  OUT (ch);
2659	case 0x82:
2660	  ch = THIS ();
2661	  NEXT ();
2662	  OUT (ch);
2663	case 0x81:
2664	  ch = THIS ();
2665	  NEXT ();
2666	  OUT (ch);
2667	case 0x80:
2668	  break;
2669	}
2670    }
2671}
2672
2673#define ID copy_id()
2674#define INT copy_int()
2675#define EXP copy_expression()
2676static void copy_till_end ();
2677#define INTn(q) copy_int()
2678#define EXPn(q) copy_expression()
2679
2680static void
2681f1_record ()
2682{
2683  int ch;
2684  /* ATN record */
2685  NEXT ();
2686  ch = THIS ();
2687  switch (ch)
2688    {
2689    default:
2690      OUT (0xf1);
2691      OUT (ch);
2692      break;
2693    case 0xc9:
2694      NEXT ();
2695      OUT (0xf1);
2696      OUT (0xc9);
2697      INT;
2698      INT;
2699      ch = THIS ();
2700      switch (ch)
2701	{
2702	case 0x16:
2703	  NEXT ();
2704	  break;
2705	case 0x01:
2706	  NEXT ();
2707	  break;
2708	case 0x00:
2709	  NEXT ();
2710	  INT;
2711	  break;
2712	case 0x03:
2713	  NEXT ();
2714	  INT;
2715	  break;
2716	case 0x13:
2717	  EXPn (instruction address);
2718	  break;
2719	default:
2720	  break;
2721	}
2722      break;
2723    case 0xd8:
2724      /* EXternal ref */
2725      NEXT ();
2726      OUT (0xf1);
2727      OUT (0xd8);
2728      EXP;
2729      EXP;
2730      EXP;
2731      EXP;
2732      break;
2733    case 0xce:
2734      NEXT ();
2735      OUT (0xf1);
2736      OUT (0xce);
2737      INT;
2738      INT;
2739      ch = THIS ();
2740      INT;
2741      switch (ch)
2742	{
2743	case 0x01:
2744	  INT;
2745	  INT;
2746	  break;
2747	case 0x02:
2748	  INT;
2749	  break;
2750	case 0x04:
2751	  EXPn (external function);
2752	  break;
2753	case 0x05:
2754	  break;
2755	case 0x07:
2756	  INTn (line number);
2757	  INT;
2758	case 0x08:
2759	  break;
2760	case 0x0a:
2761	  INTn (locked register);
2762	  INT;
2763	  break;
2764	case 0x3f:
2765	  copy_till_end ();
2766	  break;
2767	case 0x3e:
2768	  copy_till_end ();
2769	  break;
2770	case 0x40:
2771	  copy_till_end ();
2772	  break;
2773	case 0x41:
2774	  ID;
2775	  break;
2776	}
2777    }
2778
2779}
2780
2781static void
2782f0_record ()
2783{
2784  /* Attribute record */
2785  NEXT ();
2786  OUT (0xf0);
2787  INTn (Symbol name);
2788  ID;
2789}
2790
2791static void
2792copy_till_end ()
2793{
2794  int ch = THIS ();
2795  while (1)
2796    {
2797      while (ch <= 0x80)
2798	{
2799	  OUT (ch);
2800	  NEXT ();
2801	  ch = THIS ();
2802	}
2803      switch (ch)
2804	{
2805	case 0x84:
2806	  OUT (THIS ());
2807	  NEXT ();
2808	case 0x83:
2809	  OUT (THIS ());
2810	  NEXT ();
2811	case 0x82:
2812	  OUT (THIS ());
2813	  NEXT ();
2814	case 0x81:
2815	  OUT (THIS ());
2816	  NEXT ();
2817	  OUT (THIS ());
2818	  NEXT ();
2819
2820	  ch = THIS ();
2821	  break;
2822	default:
2823	  return;
2824	}
2825    }
2826
2827}
2828
2829static void
2830f2_record ()
2831{
2832  NEXT ();
2833  OUT (0xf2);
2834  INT;
2835  NEXT ();
2836  OUT (0xce);
2837  INT;
2838  copy_till_end ();
2839}
2840
2841
2842static void block ();
2843static void
2844f8_record ()
2845{
2846  int ch;
2847  NEXT ();
2848  ch = THIS ();
2849  switch (ch)
2850    {
2851    case 0x01:
2852    case 0x02:
2853    case 0x03:
2854      /* Unique typedefs for module */
2855      /* GLobal typedefs  */
2856      /* High level module scope beginning */
2857      {
2858	struct output_buffer_struct ob;
2859	NEXT ();
2860	OUT (0xf8);
2861	OUT (ch);
2862	drop_int (&ob);
2863	ID;
2864
2865	block ();
2866
2867	NEXT ();
2868	fill_int (&ob);
2869	OUT (0xf9);
2870      }
2871      break;
2872    case 0x04:
2873      /* Global function */
2874      {
2875	struct output_buffer_struct ob;
2876	NEXT ();
2877	OUT (0xf8);
2878	OUT (0x04);
2879	drop_int (&ob);
2880	ID;
2881	INTn (stack size);
2882	INTn (ret val);
2883	EXPn (offset);
2884
2885	block ();
2886
2887	NEXT ();
2888	OUT (0xf9);
2889	EXPn (size of block);
2890	fill_int (&ob);
2891      }
2892      break;
2893
2894    case 0x05:
2895      /* File name for source line numbers */
2896      {
2897	struct output_buffer_struct ob;
2898	NEXT ();
2899	OUT (0xf8);
2900	OUT (0x05);
2901	drop_int (&ob);
2902	ID;
2903	INTn (year);
2904	INTn (month);
2905	INTn (day);
2906	INTn (hour);
2907	INTn (monute);
2908	INTn (second);
2909	block ();
2910	NEXT ();
2911	OUT (0xf9);
2912	fill_int (&ob);
2913      }
2914      break;
2915
2916    case 0x06:
2917      /* Local function */
2918      {
2919	struct output_buffer_struct ob;
2920	NEXT ();
2921	OUT (0xf8);
2922	OUT (0x06);
2923	drop_int (&ob);
2924	ID;
2925	INTn (stack size);
2926	INTn (type return);
2927	EXPn (offset);
2928	block ();
2929	NEXT ();
2930	OUT (0xf9);
2931	EXPn (size);
2932	fill_int (&ob);
2933      }
2934      break;
2935
2936    case 0x0a:
2937      /* Assembler module scope beginning -*/
2938      {
2939	struct output_buffer_struct ob;
2940
2941	NEXT ();
2942	OUT (0xf8);
2943	OUT (0x0a);
2944	drop_int (&ob);
2945	ID;
2946	ID;
2947	INT;
2948	ID;
2949	INT;
2950	INT;
2951	INT;
2952	INT;
2953	INT;
2954	INT;
2955
2956	block ();
2957
2958	NEXT ();
2959	OUT (0xf9);
2960	fill_int (&ob);
2961      }
2962      break;
2963    case 0x0b:
2964      {
2965	struct output_buffer_struct ob;
2966	NEXT ();
2967	OUT (0xf8);
2968	OUT (0x0b);
2969	drop_int (&ob);
2970	ID;
2971	INT;
2972	INTn (section index);
2973	EXPn (offset);
2974	INTn (stuff);
2975
2976	block ();
2977
2978	OUT (0xf9);
2979	NEXT ();
2980	EXPn (Size in Maus);
2981	fill_int (&ob);
2982      }
2983      break;
2984    }
2985}
2986
2987static void
2988e2_record ()
2989{
2990  OUT (0xe2);
2991  NEXT ();
2992  OUT (0xce);
2993  NEXT ();
2994  INT;
2995  EXP;
2996}
2997
2998static void
2999block ()
3000{
3001  int ch;
3002  while (1)
3003    {
3004      ch = THIS ();
3005      switch (ch)
3006	{
3007	case 0xe1:
3008	case 0xe5:
3009	  return;
3010	case 0xf9:
3011	  return;
3012	case 0xf0:
3013	  f0_record ();
3014	  break;
3015	case 0xf1:
3016	  f1_record ();
3017	  break;
3018	case 0xf2:
3019	  f2_record ();
3020	  break;
3021	case 0xf8:
3022	  f8_record ();
3023	  break;
3024	case 0xe2:
3025	  e2_record ();
3026	  break;
3027
3028	}
3029    }
3030}
3031
3032
3033
3034/* relocate_debug,
3035   moves all the debug information from the source bfd to the output
3036   bfd, and relocates any expressions it finds
3037*/
3038
3039static void
3040relocate_debug (output, input)
3041     bfd *output;
3042     bfd *input;
3043{
3044#define IBS 400
3045#define OBS 400
3046  unsigned char input_buffer[IBS];
3047
3048  input_ptr_start = input_ptr = input_buffer;
3049  input_ptr_end = input_buffer + IBS;
3050  input_bfd = input;
3051  /* FIXME: Check return value.  I'm not sure whether it needs to read
3052     the entire buffer or not.  */
3053  bfd_read ((PTR) input_ptr_start, 1, IBS, input);
3054  block ();
3055}
3056
3057/*
3058  During linking, we we told about the bfds which made up our
3059  contents, we have a list of them. They will still be open, so go to
3060  the debug info in each, and copy it out, relocating it as we go.
3061*/
3062
3063static boolean
3064ieee_write_debug_part (abfd)
3065     bfd *abfd;
3066{
3067  ieee_data_type *ieee = IEEE_DATA (abfd);
3068  bfd_chain_type *chain = ieee->chain_root;
3069  unsigned char output_buffer[OBS];
3070  boolean some_debug = false;
3071  file_ptr here = bfd_tell (abfd);
3072
3073  output_ptr_start = output_ptr = output_buffer;
3074  output_ptr_end = output_buffer + OBS;
3075  output_ptr = output_buffer;
3076  output_bfd = abfd;
3077
3078  if (chain == (bfd_chain_type *) NULL)
3079    {
3080      asection *s;
3081
3082      for (s = abfd->sections; s != NULL; s = s->next)
3083	if ((s->flags & SEC_DEBUGGING) != 0)
3084	  break;
3085      if (s == NULL)
3086	{
3087	  ieee->w.r.debug_information_part = 0;
3088	  return true;
3089	}
3090
3091      ieee->w.r.debug_information_part = here;
3092      if (bfd_write (s->contents, 1, s->_raw_size, abfd) != s->_raw_size)
3093	return false;
3094    }
3095  else
3096    {
3097      while (chain != (bfd_chain_type *) NULL)
3098	{
3099	  bfd *entry = chain->this;
3100	  ieee_data_type *entry_ieee = IEEE_DATA (entry);
3101	  if (entry_ieee->w.r.debug_information_part)
3102	    {
3103	      if (bfd_seek (entry, entry_ieee->w.r.debug_information_part,
3104			    SEEK_SET)
3105		  != 0)
3106		return false;
3107	      relocate_debug (abfd, entry);
3108	    }
3109
3110	  chain = chain->next;
3111	}
3112      if (some_debug)
3113	{
3114	  ieee->w.r.debug_information_part = here;
3115	}
3116      else
3117	{
3118	  ieee->w.r.debug_information_part = 0;
3119	}
3120
3121      flush ();
3122    }
3123
3124  return true;
3125}
3126
3127/* Write the data in an ieee way.  */
3128
3129static boolean
3130ieee_write_data_part (abfd)
3131     bfd *abfd;
3132{
3133  asection *s;
3134  ieee_data_type *ieee = IEEE_DATA (abfd);
3135  ieee->w.r.data_part = bfd_tell (abfd);
3136  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3137    {
3138      /* Skip sections that have no loadable contents (.bss,
3139         debugging, etc.)  */
3140      if ((s->flags & SEC_LOAD) == 0)
3141	continue;
3142
3143      /* Sort the reloc records so we can insert them in the correct
3144	 places */
3145      if (s->reloc_count != 0)
3146	{
3147	  if (! do_with_relocs (abfd, s))
3148	    return false;
3149	}
3150      else
3151	{
3152	  if (! do_without_relocs (abfd, s))
3153	    return false;
3154	}
3155    }
3156
3157  return true;
3158}
3159
3160
3161static boolean
3162init_for_output (abfd)
3163     bfd *abfd;
3164{
3165  asection *s;
3166  for (s = abfd->sections; s != (asection *) NULL; s = s->next)
3167    {
3168      if ((s->flags & SEC_DEBUGGING) != 0)
3169	continue;
3170      if (s->_raw_size != 0)
3171	{
3172	  ieee_per_section (s)->data = (bfd_byte *) (bfd_alloc (abfd, s->_raw_size));
3173	  if (!ieee_per_section (s)->data)
3174	    return false;
3175	}
3176    }
3177  return true;
3178}
3179
3180/** exec and core file sections */
3181
3182/* set section contents is complicated with IEEE since the format is
3183* not a byte image, but a record stream.
3184*/
3185boolean
3186ieee_set_section_contents (abfd, section, location, offset, count)
3187     bfd *abfd;
3188     sec_ptr section;
3189     PTR location;
3190     file_ptr offset;
3191     bfd_size_type count;
3192{
3193  if ((section->flags & SEC_DEBUGGING) != 0)
3194    {
3195      if (section->contents == NULL)
3196	{
3197	  section->contents = ((unsigned char *)
3198			       bfd_alloc (abfd, section->_raw_size));
3199	  if (section->contents == NULL)
3200	    return false;
3201	}
3202      /* bfd_set_section_contents has already checked that everything
3203         is within range.  */
3204      memcpy (section->contents + offset, location, count);
3205      return true;
3206    }
3207
3208  if (ieee_per_section (section)->data == (bfd_byte *) NULL)
3209    {
3210      if (!init_for_output (abfd))
3211	return false;
3212    }
3213  memcpy ((PTR) (ieee_per_section (section)->data + offset),
3214	  (PTR) location,
3215	  (unsigned int) count);
3216  return true;
3217}
3218
3219/* Write the external symbols of a file.  IEEE considers two sorts of
3220   external symbols, public, and referenced.  It uses to internal
3221   forms to index them as well.  When we write them out we turn their
3222   symbol values into indexes from the right base.  */
3223
3224static boolean
3225ieee_write_external_part (abfd)
3226     bfd *abfd;
3227{
3228  asymbol **q;
3229  ieee_data_type *ieee = IEEE_DATA (abfd);
3230
3231  unsigned int reference_index = IEEE_REFERENCE_BASE;
3232  unsigned int public_index = IEEE_PUBLIC_BASE + 2;
3233  file_ptr here = bfd_tell (abfd);
3234  boolean hadone = false;
3235  if (abfd->outsymbols != (asymbol **) NULL)
3236    {
3237
3238      for (q = abfd->outsymbols; *q != (asymbol *) NULL; q++)
3239	{
3240	  asymbol *p = *q;
3241	  if (bfd_is_und_section (p->section))
3242	    {
3243	      /* This must be a symbol reference .. */
3244	      if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3245		  || ! ieee_write_int (abfd, reference_index)
3246		  || ! ieee_write_id (abfd, p->name))
3247		return false;
3248	      p->value = reference_index;
3249	      reference_index++;
3250	      hadone = true;
3251	    }
3252	  else if (bfd_is_com_section (p->section))
3253	    {
3254	      /* This is a weak reference */
3255	      if (! ieee_write_byte (abfd, ieee_external_reference_enum)
3256		  || ! ieee_write_int (abfd, reference_index)
3257		  || ! ieee_write_id (abfd, p->name)
3258		  || ! ieee_write_byte (abfd,
3259					ieee_weak_external_reference_enum)
3260		  || ! ieee_write_int (abfd, reference_index)
3261		  || ! ieee_write_int (abfd, p->value))
3262		return false;
3263	      p->value = reference_index;
3264	      reference_index++;
3265	      hadone = true;
3266	    }
3267	  else if (p->flags & BSF_GLOBAL)
3268	    {
3269	      /* This must be a symbol definition */
3270
3271	      if (! ieee_write_byte (abfd, ieee_external_symbol_enum)
3272		  || ! ieee_write_int (abfd, public_index)
3273		  || ! ieee_write_id (abfd, p->name)
3274		  || ! ieee_write_2bytes (abfd, ieee_attribute_record_enum)
3275		  || ! ieee_write_int (abfd, public_index)
3276		  || ! ieee_write_byte (abfd, 15) /* instruction address */
3277		  || ! ieee_write_byte (abfd, 19) /* static symbol */
3278		  || ! ieee_write_byte (abfd, 1)) /* one of them */
3279		return false;
3280
3281	      /* Write out the value */
3282	      if (! ieee_write_2bytes (abfd, ieee_value_record_enum)
3283		  || ! ieee_write_int (abfd, public_index))
3284		return false;
3285	      if (! bfd_is_abs_section (p->section))
3286		{
3287		  if (abfd->flags & EXEC_P)
3288		    {
3289		      /* If fully linked, then output all symbols
3290			 relocated */
3291		      if (! (ieee_write_int
3292			     (abfd,
3293			      (p->value
3294			       + p->section->output_offset
3295			       + p->section->output_section->vma))))
3296			return false;
3297		    }
3298		  else
3299		    {
3300		      if (! (ieee_write_expression
3301			     (abfd,
3302			      p->value + p->section->output_offset,
3303			      p->section->output_section->symbol,
3304			      false, 0)))
3305			return false;
3306		    }
3307		}
3308	      else
3309		{
3310		  if (! ieee_write_expression (abfd,
3311					       p->value,
3312					       bfd_abs_section_ptr->symbol,
3313					       false, 0))
3314		    return false;
3315		}
3316	      p->value = public_index;
3317	      public_index++;
3318	      hadone = true;
3319	    }
3320	  else
3321	    {
3322	      /* This can happen - when there are gaps in the symbols read */
3323	      /* from an input ieee file */
3324	    }
3325	}
3326    }
3327  if (hadone)
3328    ieee->w.r.external_part = here;
3329
3330  return true;
3331}
3332
3333
3334static CONST unsigned char exten[] =
3335{
3336  0xf0, 0x20, 0x00,
3337  0xf1, 0xce, 0x20, 0x00, 37, 3, 3,	/* Set version 3 rev 3   	*/
3338  0xf1, 0xce, 0x20, 0x00, 39, 2,/* keep symbol in  original case */
3339  0xf1, 0xce, 0x20, 0x00, 38	/* set object type relocateable to x */
3340};
3341
3342static CONST unsigned char envi[] =
3343{
3344  0xf0, 0x21, 0x00,
3345
3346/*    0xf1, 0xce, 0x21, 00, 50, 0x82, 0x07, 0xc7, 0x09, 0x11, 0x11,
3347    0x19, 0x2c,
3348*/
3349  0xf1, 0xce, 0x21, 00, 52, 0x00,	/* exec ok */
3350
3351  0xf1, 0xce, 0x21, 0, 53, 0x03,/* host unix */
3352/*    0xf1, 0xce, 0x21, 0, 54, 2,1,1	tool & version # */
3353};
3354
3355static boolean
3356ieee_write_me_part (abfd)
3357     bfd *abfd;
3358{
3359  ieee_data_type *ieee = IEEE_DATA (abfd);
3360  ieee->w.r.trailer_part = bfd_tell (abfd);
3361  if (abfd->start_address)
3362    {
3363      if (! ieee_write_2bytes (abfd, ieee_value_starting_address_enum)
3364	  || ! ieee_write_byte (abfd, ieee_function_either_open_b_enum)
3365	  || ! ieee_write_int (abfd, abfd->start_address)
3366	  || ! ieee_write_byte (abfd, ieee_function_either_close_b_enum))
3367	return false;
3368    }
3369  ieee->w.r.me_record = bfd_tell (abfd);
3370  if (! ieee_write_byte (abfd, ieee_module_end_enum))
3371    return false;
3372  return true;
3373}
3374
3375/* Write out the IEEE processor ID.  */
3376
3377static boolean
3378ieee_write_processor (abfd)
3379     bfd *abfd;
3380{
3381  const bfd_arch_info_type *arch;
3382
3383  arch = bfd_get_arch_info (abfd);
3384  switch (arch->arch)
3385    {
3386    default:
3387      if (! ieee_write_id (abfd, bfd_printable_name (abfd)))
3388	return false;
3389      break;
3390
3391    case bfd_arch_a29k:
3392      if (! ieee_write_id (abfd, "29000"))
3393	return false;
3394      break;
3395
3396    case bfd_arch_h8300:
3397      if (! ieee_write_id (abfd, "H8/300"))
3398	return false;
3399      break;
3400
3401    case bfd_arch_h8500:
3402      if (! ieee_write_id (abfd, "H8/500"))
3403	return false;
3404      break;
3405
3406    case bfd_arch_i960:
3407      switch (arch->mach)
3408	{
3409	default:
3410	case bfd_mach_i960_core:
3411	case bfd_mach_i960_ka_sa:
3412	  if (! ieee_write_id (abfd, "80960KA"))
3413	    return false;
3414	  break;
3415
3416	case bfd_mach_i960_kb_sb:
3417	  if (! ieee_write_id (abfd, "80960KB"))
3418	    return false;
3419	  break;
3420
3421	case bfd_mach_i960_ca:
3422	  if (! ieee_write_id (abfd, "80960CA"))
3423	    return false;
3424	  break;
3425
3426	case bfd_mach_i960_mc:
3427	case bfd_mach_i960_xa:
3428	  if (! ieee_write_id (abfd, "80960MC"))
3429	    return false;
3430	  break;
3431	}
3432      break;
3433
3434    case bfd_arch_m68k:
3435      {
3436	char ab[20];
3437
3438	sprintf (ab, "%lu", arch->mach);
3439	if (! ieee_write_id (abfd, ab))
3440	  return false;
3441      }
3442      break;
3443    }
3444
3445  return true;
3446}
3447
3448boolean
3449ieee_write_object_contents (abfd)
3450     bfd *abfd;
3451{
3452  ieee_data_type *ieee = IEEE_DATA (abfd);
3453  unsigned int i;
3454  file_ptr old;
3455
3456  /* Fast forward over the header area */
3457  if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
3458    return false;
3459
3460  if (! ieee_write_byte (abfd, ieee_module_beginning_enum)
3461      || ! ieee_write_processor (abfd)
3462      || ! ieee_write_id (abfd, abfd->filename))
3463    return false;
3464
3465  /* Fast forward over the variable bits */
3466  if (! ieee_write_byte (abfd, ieee_address_descriptor_enum))
3467    return false;
3468
3469  /* Bits per MAU */
3470  if (! ieee_write_byte (abfd, (bfd_byte) (bfd_arch_bits_per_byte (abfd))))
3471    return false;
3472  /* MAU's per address */
3473  if (! ieee_write_byte (abfd,
3474			 (bfd_byte) (bfd_arch_bits_per_address (abfd)
3475				     / bfd_arch_bits_per_byte (abfd))))
3476    return false;
3477
3478  old = bfd_tell (abfd);
3479  if (bfd_seek (abfd, (file_ptr) (8 * N_W_VARIABLES), SEEK_CUR) != 0)
3480    return false;
3481
3482  ieee->w.r.extension_record = bfd_tell (abfd);
3483  if (bfd_write ((char *) exten, 1, sizeof (exten), abfd) != sizeof (exten))
3484    return false;
3485  if (abfd->flags & EXEC_P)
3486    {
3487      if (! ieee_write_byte (abfd, 0x1)) /* Absolute */
3488	return false;
3489    }
3490  else
3491    {
3492      if (! ieee_write_byte (abfd, 0x2)) /* Relocateable */
3493	return false;
3494    }
3495
3496  ieee->w.r.environmental_record = bfd_tell (abfd);
3497  if (bfd_write ((char *) envi, 1, sizeof (envi), abfd) != sizeof (envi))
3498    return false;
3499
3500  /* The HP emulator database requires a timestamp in the file.  */
3501  {
3502    time_t now;
3503    const struct tm *t;
3504
3505    time (&now);
3506    t = (struct tm *) localtime (&now);
3507    if (! ieee_write_2bytes (abfd, (int) ieee_atn_record_enum)
3508	|| ! ieee_write_byte (abfd, 0x21)
3509	|| ! ieee_write_byte (abfd, 0)
3510	|| ! ieee_write_byte (abfd, 50)
3511	|| ! ieee_write_int (abfd, t->tm_year + 1900)
3512	|| ! ieee_write_int (abfd, t->tm_mon + 1)
3513	|| ! ieee_write_int (abfd, t->tm_mday)
3514	|| ! ieee_write_int (abfd, t->tm_hour)
3515	|| ! ieee_write_int (abfd, t->tm_min)
3516	|| ! ieee_write_int (abfd, t->tm_sec))
3517      return false;
3518  }
3519
3520  output_bfd = abfd;
3521
3522  flush ();
3523
3524  if (! ieee_write_section_part (abfd))
3525    return false;
3526  /* First write the symbols.  This changes their values into table
3527    indeces so we cant use it after this point.  */
3528  if (! ieee_write_external_part (abfd))
3529    return false;
3530
3531  /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3532
3533  /*  ieee_write_byte(abfd, ieee_record_seperator_enum);*/
3534
3535
3536  /* Write any debugs we have been told about.  */
3537  if (! ieee_write_debug_part (abfd))
3538    return false;
3539
3540  /* Can only write the data once the symbols have been written, since
3541     the data contains relocation information which points to the
3542     symbols.  */
3543  if (! ieee_write_data_part (abfd))
3544    return false;
3545
3546  /* At the end we put the end!  */
3547  if (! ieee_write_me_part (abfd))
3548    return false;
3549
3550  /* Generate the header */
3551  if (bfd_seek (abfd, old, SEEK_SET) != 0)
3552    return false;
3553
3554  for (i = 0; i < N_W_VARIABLES; i++)
3555    {
3556      if (! ieee_write_2bytes (abfd, ieee_assign_value_to_variable_enum)
3557	  || ! ieee_write_byte (abfd, (bfd_byte) i)
3558	  || ! ieee_write_int5_out (abfd, ieee->w.offset[i]))
3559	return false;
3560    }
3561
3562  return true;
3563}
3564
3565/* Native-level interface to symbols. */
3566
3567/* We read the symbols into a buffer, which is discarded when this
3568   function exits.  We read the strings into a buffer large enough to
3569   hold them all plus all the cached symbol entries. */
3570
3571asymbol *
3572ieee_make_empty_symbol (abfd)
3573     bfd *abfd;
3574{
3575  ieee_symbol_type *new =
3576    (ieee_symbol_type *) bfd_zmalloc (sizeof (ieee_symbol_type));
3577  if (!new)
3578    return NULL;
3579  new->symbol.the_bfd = abfd;
3580  return &new->symbol;
3581}
3582
3583static bfd *
3584ieee_openr_next_archived_file (arch, prev)
3585     bfd *arch;
3586     bfd *prev;
3587{
3588  ieee_ar_data_type *ar = IEEE_AR_DATA (arch);
3589  /* take the next one from the arch state, or reset */
3590  if (prev == (bfd *) NULL)
3591    {
3592      /* Reset the index - the first two entries are bogus*/
3593      ar->element_index = 2;
3594    }
3595  while (true)
3596    {
3597      ieee_ar_obstack_type *p = ar->elements + ar->element_index;
3598      ar->element_index++;
3599      if (ar->element_index <= ar->element_count)
3600	{
3601	  if (p->file_offset != (file_ptr) 0)
3602	    {
3603	      if (p->abfd == (bfd *) NULL)
3604		{
3605		  p->abfd = _bfd_create_empty_archive_element_shell (arch);
3606		  p->abfd->origin = p->file_offset;
3607		}
3608	      return p->abfd;
3609	    }
3610	}
3611      else
3612	{
3613	  bfd_set_error (bfd_error_no_more_archived_files);
3614	  return (bfd *) NULL;
3615	}
3616
3617    }
3618}
3619
3620static boolean
3621ieee_find_nearest_line (abfd,
3622			section,
3623			symbols,
3624			offset,
3625			filename_ptr,
3626			functionname_ptr,
3627			line_ptr)
3628     bfd *abfd;
3629     asection *section;
3630     asymbol **symbols;
3631     bfd_vma offset;
3632     char **filename_ptr;
3633     char **functionname_ptr;
3634     int *line_ptr;
3635{
3636  return false;
3637}
3638
3639static int
3640ieee_generic_stat_arch_elt (abfd, buf)
3641     bfd *abfd;
3642     struct stat *buf;
3643{
3644  ieee_ar_data_type *ar = (ieee_ar_data_type *) NULL;
3645  ieee_data_type *ieee;
3646
3647  if (abfd->my_archive != NULL)
3648    ar = abfd->my_archive->tdata.ieee_ar_data;
3649  if (ar == (ieee_ar_data_type *) NULL)
3650    {
3651      bfd_set_error (bfd_error_invalid_operation);
3652      return -1;
3653    }
3654
3655  if (IEEE_DATA (abfd) == NULL)
3656    {
3657      if (ieee_object_p (abfd) == NULL)
3658	{
3659	  bfd_set_error (bfd_error_wrong_format);
3660	  return -1;
3661	}
3662    }
3663
3664  ieee = IEEE_DATA (abfd);
3665
3666  buf->st_size = ieee->w.r.me_record + 1;
3667  buf->st_mode = 0644;
3668  return 0;
3669}
3670
3671static int
3672ieee_sizeof_headers (abfd, x)
3673     bfd *abfd;
3674     boolean x;
3675{
3676  return 0;
3677}
3678
3679
3680/* The debug info routines are never used.  */
3681#if 0
3682
3683static void
3684ieee_bfd_debug_info_start (abfd)
3685     bfd *abfd;
3686{
3687
3688}
3689
3690static void
3691ieee_bfd_debug_info_end (abfd)
3692     bfd *abfd;
3693{
3694
3695}
3696
3697
3698/* Add this section to the list of sections we have debug info for, to
3699   be ready to output it at close time
3700   */
3701static void
3702ieee_bfd_debug_info_accumulate (abfd, section)
3703     bfd *abfd;
3704     asection *section;
3705{
3706  ieee_data_type *ieee = IEEE_DATA (section->owner);
3707  ieee_data_type *output_ieee = IEEE_DATA (abfd);
3708  /* can only accumulate data from other ieee bfds */
3709  if (section->owner->xvec != abfd->xvec)
3710    return;
3711  /* Only bother once per bfd */
3712  if (ieee->done_debug == true)
3713    return;
3714  ieee->done_debug = true;
3715
3716  /* Don't bother if there is no debug info */
3717  if (ieee->w.r.debug_information_part == 0)
3718    return;
3719
3720
3721  /* Add to chain */
3722  {
3723    bfd_chain_type *n = (bfd_chain_type *) bfd_alloc (abfd, sizeof (bfd_chain_type));
3724    if (!n)
3725      abort ();		/* FIXME */
3726    n->this = section->owner;
3727    n->next = (bfd_chain_type *) NULL;
3728
3729    if (output_ieee->chain_head)
3730      {
3731	output_ieee->chain_head->next = n;
3732      }
3733    else
3734      {
3735	output_ieee->chain_root = n;
3736
3737      }
3738    output_ieee->chain_head = n;
3739  }
3740}
3741
3742#endif
3743
3744#define	ieee_close_and_cleanup _bfd_generic_close_and_cleanup
3745#define ieee_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
3746
3747#define ieee_slurp_armap bfd_true
3748#define ieee_slurp_extended_name_table bfd_true
3749#define ieee_construct_extended_name_table \
3750  ((boolean (*) PARAMS ((bfd *, char **, bfd_size_type *, const char **))) \
3751   bfd_true)
3752#define ieee_truncate_arname bfd_dont_truncate_arname
3753#define ieee_write_armap \
3754  ((boolean (*) \
3755    PARAMS ((bfd *, unsigned int, struct orl *, unsigned int, int))) \
3756   bfd_true)
3757#define ieee_read_ar_hdr bfd_nullvoidptr
3758#define ieee_update_armap_timestamp bfd_true
3759#define ieee_get_elt_at_index _bfd_generic_get_elt_at_index
3760
3761#define ieee_bfd_is_local_label_name bfd_generic_is_local_label_name
3762#define ieee_get_lineno _bfd_nosymbols_get_lineno
3763#define ieee_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
3764#define ieee_read_minisymbols _bfd_generic_read_minisymbols
3765#define ieee_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
3766
3767#define ieee_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
3768
3769#define ieee_set_arch_mach _bfd_generic_set_arch_mach
3770
3771#define ieee_get_section_contents_in_window \
3772  _bfd_generic_get_section_contents_in_window
3773#define ieee_bfd_get_relocated_section_contents \
3774  bfd_generic_get_relocated_section_contents
3775#define ieee_bfd_relax_section bfd_generic_relax_section
3776#define ieee_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
3777#define ieee_bfd_link_add_symbols _bfd_generic_link_add_symbols
3778#define ieee_bfd_final_link _bfd_generic_final_link
3779#define ieee_bfd_link_split_section  _bfd_generic_link_split_section
3780
3781/*SUPPRESS 460 */
3782const bfd_target ieee_vec =
3783{
3784  "ieee",			/* name */
3785  bfd_target_ieee_flavour,
3786  BFD_ENDIAN_UNKNOWN,		/* target byte order */
3787  BFD_ENDIAN_UNKNOWN,		/* target headers byte order */
3788  (HAS_RELOC | EXEC_P |		/* object flags */
3789   HAS_LINENO | HAS_DEBUG |
3790   HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
3791  (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
3792   | SEC_ALLOC | SEC_LOAD | SEC_RELOC),	/* section flags */
3793  '_',				/* leading underscore */
3794  ' ',				/* ar_pad_char */
3795  16,				/* ar_max_namelen */
3796  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3797  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3798  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* data */
3799  bfd_getb64, bfd_getb_signed_64, bfd_putb64,
3800  bfd_getb32, bfd_getb_signed_32, bfd_putb32,
3801  bfd_getb16, bfd_getb_signed_16, bfd_putb16,	/* hdrs */
3802
3803  {_bfd_dummy_target,
3804   ieee_object_p,		/* bfd_check_format */
3805   ieee_archive_p,
3806   _bfd_dummy_target,
3807  },
3808  {
3809    bfd_false,
3810    ieee_mkobject,
3811    _bfd_generic_mkarchive,
3812    bfd_false
3813  },
3814  {
3815    bfd_false,
3816    ieee_write_object_contents,
3817    _bfd_write_archive_contents,
3818    bfd_false,
3819  },
3820
3821  BFD_JUMP_TABLE_GENERIC (ieee),
3822  BFD_JUMP_TABLE_COPY (_bfd_generic),
3823  BFD_JUMP_TABLE_CORE (_bfd_nocore),
3824  BFD_JUMP_TABLE_ARCHIVE (ieee),
3825  BFD_JUMP_TABLE_SYMBOLS (ieee),
3826  BFD_JUMP_TABLE_RELOCS (ieee),
3827  BFD_JUMP_TABLE_WRITE (ieee),
3828  BFD_JUMP_TABLE_LINK (ieee),
3829  BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
3830
3831  (PTR) 0
3832};
3833