home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / binutils-2.7-src.tgz / tar.out / fsf / binutils / bfd / ieee.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  86KB  |  3,770 lines

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