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