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