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