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 / srec.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  30KB  |  1,339 lines

  1. /* BFD back-end for s-record objects.
  2.    Copyright 1990, 1991, 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  3.    Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
  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. /*
  22. SUBSECTION
  23.     S-Record handling
  24.  
  25. DESCRIPTION
  26.     
  27.     Ordinary S-Records cannot hold anything but addresses and
  28.     data, so that's all that we implement.
  29.  
  30.     The only interesting thing is that S-Records may come out of
  31.     order and there is no header, so an initial scan is required
  32.     to discover the minimum and maximum addresses used to create
  33.     the vma and size of the only section we create.  We
  34.     arbitrarily call this section ".text".
  35.  
  36.     When bfd_get_section_contents is called the file is read
  37.     again, and this time the data is placed into a bfd_alloc'd
  38.     area.
  39.  
  40.     Any number of sections may be created for output, we save them
  41.     up and output them when it's time to close the bfd.
  42.  
  43.     An s record looks like:
  44.     
  45. EXAMPLE
  46.     S<type><length><address><data><checksum>
  47.     
  48. DESCRIPTION
  49.     Where
  50.     o length
  51.     is the number of bytes following upto the checksum. Note that
  52.     this is not the number of chars following, since it takes two
  53.     chars to represent a byte.
  54.     o type
  55.     is one of:
  56.     0) header record
  57.     1) two byte address data record
  58.     2) three byte address data record
  59.     3) four byte address data record
  60.     7) four byte address termination record
  61.     8) three byte address termination record
  62.     9) two byte address termination record
  63.     
  64.     o address
  65.     is the start address of the data following, or in the case of
  66.     a termination record, the start address of the image
  67.     o data
  68.     is the data.
  69.     o checksum
  70.     is the sum of all the raw byte data in the record, from the length
  71.     upwards, modulo 256 and subtracted from 255.
  72.  
  73.  
  74. SUBSECTION
  75.     Symbol S-Record handling
  76.  
  77. DESCRIPTION
  78.     Some ICE equipment understands an addition to the standard
  79.     S-Record format; symbols and their addresses can be sent
  80.     before the data.
  81.  
  82.     The format of this is:
  83.     ($$ <modulename>
  84.         (<space> <symbol> <address>)*)
  85.     $$
  86.  
  87.     so a short symbol table could look like:
  88.  
  89. EXAMPLE
  90.     $$ flash.x
  91.     $$ flash.c
  92.       _port6 $0
  93.       _delay $4
  94.       _start $14
  95.       _etext $8036
  96.       _edata $8036
  97.        _end $8036
  98.     $$
  99.  
  100. DESCRIPTION
  101.     We allow symbols to be anywhere in the data stream - the module names
  102.     are always ignored.
  103.         
  104. */
  105.  
  106. #include "bfd.h"
  107. #include "sysdep.h"
  108. #include "libbfd.h"
  109. #include "libiberty.h"
  110. #include <ctype.h>
  111.  
  112. static void srec_init PARAMS ((void));
  113. static boolean srec_mkobject PARAMS ((bfd *));
  114. static int srec_get_byte PARAMS ((bfd *, boolean *));
  115. static void srec_bad_byte PARAMS ((bfd *, unsigned int, int, boolean));
  116. static boolean srec_scan PARAMS ((bfd *));
  117. static const bfd_target *srec_object_p PARAMS ((bfd *));
  118. static const bfd_target *symbolsrec_object_p PARAMS ((bfd *));
  119. static boolean srec_read_section PARAMS ((bfd *, asection *, bfd_byte *));
  120.  
  121. static boolean srec_write_record PARAMS ((bfd *, int, bfd_vma,
  122.                       const bfd_byte *,
  123.                       const bfd_byte *));
  124. static boolean srec_write_header PARAMS ((bfd *));
  125. static boolean srec_write_symbols PARAMS ((bfd *));
  126.  
  127. /* Macros for converting between hex and binary. */
  128.  
  129. static CONST char digs[] = "0123456789ABCDEF";
  130.  
  131. #define NIBBLE(x) hex_value(x)
  132. #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
  133. #define TOHEX(d, x, ch) \
  134.     d[1] = digs[(x) & 0xf]; \
  135.     d[0] = digs[((x)>>4)&0xf]; \
  136.     ch += ((x) & 0xff);
  137. #define    ISHEX(x)  hex_p(x)
  138.  
  139. /* Initialize by filling in the hex conversion array. */
  140.  
  141. static void
  142. srec_init ()
  143. {
  144.   static boolean inited = false;
  145.  
  146.   if (inited == false)
  147.     {
  148.       inited = true;
  149.       hex_init ();
  150.     }
  151. }
  152.  
  153. /* The maximum number of bytes on a line is FF */
  154. #define MAXCHUNK 0xff
  155. /* The number of bytes we fit onto a line on output */
  156. #define CHUNK 21
  157.  
  158. /* When writing an S-record file, the S-records can not be output as
  159.    they are seen.  This structure is used to hold them in memory.  */
  160.  
  161. struct srec_data_list_struct
  162. {
  163.   struct srec_data_list_struct *next;
  164.   bfd_byte *data;
  165.   bfd_vma where;
  166.   bfd_size_type size;
  167. };
  168.  
  169. typedef struct srec_data_list_struct srec_data_list_type;
  170.  
  171. /* When scanning the S-record file, a linked list of srec_symbol
  172.    structures is built to represent the symbol table (if there is
  173.    one).  */
  174.  
  175. struct srec_symbol
  176. {
  177.   struct srec_symbol *next;
  178.   const char *name;
  179.   bfd_vma val;
  180. };
  181.  
  182. /* The S-record tdata information.  */
  183.  
  184. typedef struct srec_data_struct
  185.   {
  186.     srec_data_list_type *head;
  187.     srec_data_list_type *tail;
  188.     unsigned int type;
  189.     struct srec_symbol *symbols;
  190.     struct srec_symbol *symtail;
  191.     asymbol *csymbols;
  192.   }
  193. tdata_type;
  194.  
  195. static boolean srec_write_section PARAMS ((bfd *, tdata_type *,
  196.                        srec_data_list_type *));
  197. static boolean srec_write_terminator PARAMS ((bfd *, tdata_type *));
  198.  
  199. /* Set up the S-record tdata information.  */
  200.  
  201. static boolean
  202. srec_mkobject (abfd)
  203.      bfd *abfd;
  204. {
  205.   srec_init ();
  206.  
  207.   if (abfd->tdata.srec_data == NULL)
  208.     {
  209.       tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  210.       if (tdata == NULL)
  211.     return false;
  212.       abfd->tdata.srec_data = tdata;
  213.       tdata->type = 1;
  214.       tdata->head = NULL;
  215.       tdata->tail = NULL;
  216.       tdata->symbols = NULL;
  217.       tdata->symtail = NULL;
  218.       tdata->csymbols = NULL;
  219.     }
  220.  
  221.   return true;
  222. }
  223.  
  224. /* Read a byte from an S record file.  Set *ERRORPTR if an error
  225.    occurred.  Return EOF on error or end of file.  */
  226.  
  227. static int
  228. srec_get_byte (abfd, errorptr)
  229.      bfd *abfd;
  230.      boolean *errorptr;
  231. {
  232.   bfd_byte c;
  233.  
  234.   if (bfd_read (&c, 1, 1, abfd) != 1)
  235.     {
  236.       if (bfd_get_error () != bfd_error_file_truncated)
  237.     *errorptr = true;
  238.       return EOF;
  239.     }
  240.  
  241.   return (int) (c & 0xff);
  242. }
  243.  
  244. /* Report a problem in an S record file.  FIXME: This probably should
  245.    not call fprintf, but we really do need some mechanism for printing
  246.    error messages.  */
  247.  
  248. static void
  249. srec_bad_byte (abfd, lineno, c, error)
  250.      bfd *abfd;
  251.      unsigned int lineno;
  252.      int c;
  253.      boolean error;
  254. {
  255.   if (c == EOF)
  256.     {
  257.       if (! error)
  258.     bfd_set_error (bfd_error_file_truncated);
  259.     }
  260.   else
  261.     {
  262.       char buf[10];
  263.  
  264.       if (! isprint (c))
  265.     sprintf (buf, "\\%03o", (unsigned int) c);
  266.       else
  267.     {
  268.       buf[0] = c;
  269.       buf[1] = '\0';
  270.     }
  271.       (*_bfd_error_handler)
  272.     ("%s:%d: Unexpected character `%s' in S-record file\n",
  273.      bfd_get_filename (abfd), lineno, buf);
  274.       bfd_set_error (bfd_error_bad_value);
  275.     }
  276. }
  277.  
  278. /* Add a new symbol found in an S-record file.  */
  279.  
  280. static boolean
  281. srec_new_symbol (abfd, name, val)
  282.      bfd *abfd;
  283.      const char *name;
  284.      bfd_vma val;
  285. {
  286.   struct srec_symbol *n;
  287.  
  288.   n = (struct srec_symbol *) bfd_alloc (abfd, sizeof (struct srec_symbol));
  289.   if (n == NULL)
  290.     return false;
  291.  
  292.   n->name = name;
  293.   n->val = val;
  294.  
  295.   if (abfd->tdata.srec_data->symbols == NULL)
  296.     abfd->tdata.srec_data->symbols = n;
  297.   else
  298.     abfd->tdata.srec_data->symtail->next = n;
  299.   abfd->tdata.srec_data->symtail = n;
  300.   n->next = NULL;
  301.  
  302.   ++abfd->symcount;
  303.  
  304.   return true;
  305. }
  306.  
  307. /* Read the S record file and turn it into sections.  We create a new
  308.    section for each contiguous set of bytes.  */
  309.  
  310. static boolean
  311. srec_scan (abfd)
  312.      bfd *abfd;
  313. {
  314.   int c;
  315.   unsigned int lineno = 1;
  316.   boolean error = false;
  317.   bfd_byte *buf = NULL;
  318.   size_t bufsize = 0;
  319.   asection *sec = NULL;
  320.  
  321.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  322.     goto error_return;
  323.  
  324.   while ((c = srec_get_byte (abfd, &error)) != EOF)
  325.     {
  326.       /* We only build sections from contiguous S-records, so if this
  327.          is not an S-record, then stop building a section.  */
  328.       if (c != 'S' && c != '\r' && c != '\n')
  329.     sec = NULL;
  330.  
  331.       switch (c)
  332.     {
  333.     default:
  334.       srec_bad_byte (abfd, lineno, c, error);
  335.       goto error_return;
  336.  
  337.     case '\n':
  338.       ++lineno;
  339.       break;
  340.  
  341.     case '\r':
  342.       break;
  343.  
  344.     case '$':
  345.       /* Starting a module name, which we ignore.  */
  346.       while ((c = srec_get_byte (abfd, &error)) != '\n'
  347.          && c != EOF)
  348.         ;
  349.       if (c == EOF)
  350.         {
  351.           srec_bad_byte (abfd, lineno, c, error);
  352.           goto error_return;
  353.         }
  354.  
  355.       ++lineno;
  356.  
  357.       break;
  358.  
  359.     case ' ':
  360.       {
  361.         char *symname;
  362.         bfd_vma symval;
  363.  
  364.         /* Starting a symbol definition.  */
  365.         while ((c = srec_get_byte (abfd, &error)) != EOF
  366.            && (c == ' ' || c == '\t'))
  367.           ;
  368.         if (c == EOF)
  369.           {
  370.         srec_bad_byte (abfd, lineno, c, error);
  371.         goto error_return;
  372.           }
  373.  
  374.         obstack_1grow (&abfd->memory, c);
  375.         while ((c = srec_get_byte (abfd, &error)) != EOF
  376.            && ! isspace (c))
  377.           obstack_1grow (&abfd->memory, c);
  378.         if (c == EOF)
  379.           {
  380.         srec_bad_byte (abfd, lineno, c, error);
  381.         goto error_return;
  382.           }
  383.  
  384.         symname = obstack_finish (&abfd->memory);
  385.         if (symname == NULL)
  386.           {
  387.         bfd_set_error (bfd_error_no_memory);
  388.         goto error_return;
  389.           }
  390.       
  391.         while ((c = srec_get_byte (abfd, &error)) != EOF
  392.            && (c == ' ' || c == '\t'))
  393.           ;
  394.         if (c == EOF)
  395.           {
  396.         srec_bad_byte (abfd, lineno, c, error);
  397.         goto error_return;
  398.           }
  399.  
  400.         /* Skip a dollar sign before the hex value.  */
  401.         if (c == '$')
  402.           {
  403.         c = srec_get_byte (abfd, &error);
  404.         if (c == EOF)
  405.           {
  406.             srec_bad_byte (abfd, lineno, c, error);
  407.             goto error_return;
  408.           }
  409.           }
  410.  
  411.         symval = 0;
  412.         while (ISHEX (c))
  413.           {
  414.         symval <<= 4;
  415.         symval += NIBBLE (c);
  416.         c = srec_get_byte (abfd, &error);
  417.           }
  418.  
  419.         if (c == EOF || ! isspace (c))
  420.           {
  421.         srec_bad_byte (abfd, lineno, c, error);
  422.         goto error_return;
  423.           }
  424.  
  425.         if (! srec_new_symbol (abfd, symname, symval))
  426.           goto error_return;
  427.  
  428.         if (c == '\n')
  429.           ++lineno;
  430.  
  431.       }
  432.       break;
  433.     
  434.     case 'S':
  435.       {
  436.         file_ptr pos;
  437.         char hdr[3];
  438.         unsigned int bytes;
  439.         bfd_vma address;
  440.         bfd_byte *data;
  441.  
  442.         /* Starting an S-record.  */
  443.  
  444.         pos = bfd_tell (abfd) - 1;
  445.  
  446.         if (bfd_read (hdr, 1, 3, abfd) != 3)
  447.           goto error_return;
  448.  
  449.         if (! ISHEX (hdr[1]) || ! ISHEX (hdr[2]))
  450.           {
  451.         if (! ISHEX (hdr[1]))
  452.           c = hdr[1];
  453.         else
  454.           c = hdr[2];
  455.         srec_bad_byte (abfd, lineno, c, error);
  456.         goto error_return;
  457.           }
  458.  
  459.         bytes = HEX (hdr + 1);
  460.         if (bytes * 2 > bufsize)
  461.           {
  462.         if (buf != NULL)
  463.           free (buf);
  464.         buf = (bfd_byte *) bfd_malloc (bytes * 2);
  465.         if (buf == NULL)
  466.           goto error_return;
  467.         bufsize = bytes * 2;
  468.           }
  469.  
  470.         if (bfd_read (buf, 1, bytes * 2, abfd) != bytes * 2)
  471.           goto error_return;
  472.  
  473.         /* Ignore the checksum byte.  */
  474.         --bytes;
  475.  
  476.         address = 0;
  477.         data = buf;
  478.         switch (hdr[0])
  479.           {
  480.           case '0':
  481.           case '5':
  482.         /* Prologue--ignore the file name, but stop building a
  483.                    section at this point.  */
  484.         sec = NULL;
  485.         break;
  486.  
  487.           case '3':
  488.         address = HEX (data);
  489.         data += 2;
  490.         --bytes;
  491.         /* Fall through.  */
  492.           case '2':
  493.         address = (address << 8) | HEX (data);
  494.         data += 2;
  495.         --bytes;
  496.         /* Fall through.  */
  497.           case '1':
  498.         address = (address << 8) | HEX (data);
  499.         data += 2;
  500.         address = (address << 8) | HEX (data);
  501.         data += 2;
  502.         bytes -= 2;
  503.  
  504.         if (sec != NULL
  505.             && sec->vma + sec->_raw_size == address)
  506.           {
  507.             /* This data goes at the end of the section we are
  508.                        currently building.  */
  509.             sec->_raw_size += bytes;
  510.           }
  511.         else
  512.           {
  513.             char secbuf[20];
  514.             char *secname;
  515.  
  516.             sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
  517.             secname = (char *) bfd_alloc (abfd, strlen (secbuf) + 1);
  518.             strcpy (secname, secbuf);
  519.             sec = bfd_make_section (abfd, secname);
  520.             if (sec == NULL)
  521.               goto error_return;
  522.             sec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  523.             sec->vma = address;
  524.             sec->lma = address;
  525.             sec->_raw_size = bytes;
  526.             sec->filepos = pos;
  527.           }
  528.  
  529.         break;
  530.  
  531.           case '7':
  532.         address = HEX (data);
  533.         data += 2;
  534.         /* Fall through.  */
  535.           case '8':
  536.         address = (address << 8) | HEX (data);
  537.         data += 2;
  538.         /* Fall through.  */
  539.           case '9':
  540.         address = (address << 8) | HEX (data);
  541.         data += 2;
  542.         address = (address << 8) | HEX (data);
  543.         data += 2;
  544.  
  545.         /* This is a termination record.  */
  546.         abfd->start_address = address;
  547.  
  548.         if (buf != NULL)
  549.           free (buf);
  550.  
  551.         return true;
  552.           }
  553.       }
  554.       break;
  555.     }
  556.     }
  557.  
  558.   if (error)
  559.     goto error_return;
  560.  
  561.   if (buf != NULL)
  562.     free (buf);
  563.  
  564.   return true;
  565.  
  566.  error_return:
  567.   if (buf != NULL)
  568.     free (buf);
  569.   return false;
  570. }
  571.  
  572. /* Check whether an existing file is an S-record file.  */
  573.  
  574. static const bfd_target *
  575. srec_object_p (abfd)
  576.      bfd *abfd;
  577. {
  578.   bfd_byte b[4];
  579.  
  580.   srec_init ();
  581.  
  582.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  583.       || bfd_read (b, 1, 4, abfd) != 4)
  584.     return NULL;
  585.  
  586.   if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
  587.     {
  588.       bfd_set_error (bfd_error_wrong_format);
  589.       return NULL;
  590.     }
  591.  
  592.   if (! srec_mkobject (abfd)
  593.       || ! srec_scan (abfd))
  594.     return NULL;
  595.  
  596.   return abfd->xvec;
  597. }
  598.  
  599. /* Check whether an existing file is an S-record file with symbols.  */
  600.  
  601. static const bfd_target *
  602. symbolsrec_object_p (abfd)
  603.      bfd *abfd;
  604. {
  605.   char b[2];
  606.  
  607.   srec_init ();
  608.  
  609.   if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  610.       || bfd_read (b, 1, 2, abfd) != 2)
  611.     return NULL;
  612.  
  613.   if (b[0] != '$' || b[1] != '$')
  614.     {
  615.       bfd_set_error (bfd_error_wrong_format);
  616.       return NULL;
  617.     }
  618.  
  619.   if (! srec_mkobject (abfd)
  620.       || ! srec_scan (abfd))
  621.     return NULL;
  622.  
  623.   return abfd->xvec;
  624. }
  625.  
  626. /* Read in the contents of a section in an S-record file.  */
  627.  
  628. static boolean
  629. srec_read_section (abfd, section, contents)
  630.      bfd *abfd;
  631.      asection *section;
  632.      bfd_byte *contents;
  633. {
  634.   int c;
  635.   bfd_size_type sofar = 0;
  636.   boolean error = false;
  637.   bfd_byte *buf = NULL;
  638.   size_t bufsize = 0;
  639.  
  640.   if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
  641.     goto error_return;
  642.  
  643.   while ((c = srec_get_byte (abfd, &error)) != EOF)
  644.     {
  645.       bfd_byte hdr[3];
  646.       unsigned int bytes;
  647.       bfd_vma address;
  648.       bfd_byte *data;
  649.  
  650.       if (c == '\r' || c == '\n')
  651.     continue;
  652.  
  653.       /* This is called after srec_scan has already been called, so we
  654.          ought to know the exact format.  */
  655.       BFD_ASSERT (c == 'S');
  656.  
  657.       if (bfd_read (hdr, 1, 3, abfd) != 3)
  658.     goto error_return;
  659.  
  660.       BFD_ASSERT (ISHEX (hdr[1]) && ISHEX (hdr[2]));
  661.  
  662.       bytes = HEX (hdr + 1);
  663.  
  664.       if (bytes * 2 > bufsize)
  665.     {
  666.       if (buf != NULL)
  667.         free (buf);
  668.       buf = (bfd_byte *) bfd_malloc (bytes * 2);
  669.       if (buf == NULL)
  670.         goto error_return;
  671.       bufsize = bytes * 2;
  672.     }
  673.  
  674.       if (bfd_read (buf, 1, bytes * 2, abfd) != bytes * 2)
  675.     goto error_return;
  676.  
  677.       address = 0;
  678.       data = buf;
  679.       switch (hdr[0])
  680.     {
  681.     default:
  682.       BFD_ASSERT (sofar == section->_raw_size);
  683.       if (buf != NULL)
  684.         free (buf);
  685.       return true;
  686.  
  687.     case '3':
  688.       address = HEX (data);
  689.       data += 2;
  690.       --bytes;
  691.       /* Fall through.  */
  692.     case '2':
  693.       address = (address << 8) | HEX (data);
  694.       data += 2;
  695.       --bytes;
  696.       /* Fall through.  */
  697.     case '1':
  698.       address = (address << 8) | HEX (data);
  699.       data += 2;
  700.       address = (address << 8) | HEX (data);
  701.       data += 2;
  702.       bytes -= 2;
  703.  
  704.       if (address != section->vma + sofar)
  705.         {
  706.           /* We've come to the end of this section.  */
  707.           BFD_ASSERT (sofar == section->_raw_size);
  708.           if (buf != NULL)
  709.         free (buf);
  710.           return true;
  711.         }
  712.  
  713.       /* Don't consider checksum.  */
  714.       --bytes;
  715.  
  716.       while (bytes-- != 0)
  717.         {
  718.           contents[sofar] = HEX (data);
  719.           data += 2;
  720.           ++sofar;
  721.         }
  722.  
  723.       break;
  724.     }
  725.     }
  726.  
  727.   if (error)
  728.     goto error_return;
  729.  
  730.   BFD_ASSERT (sofar == section->_raw_size);
  731.  
  732.   if (buf != NULL)
  733.     free (buf);
  734.  
  735.   return true;
  736.  
  737.  error_return:
  738.   if (buf != NULL)
  739.     free (buf);
  740.   return false;
  741. }
  742.  
  743. /* Get the contents of a section in an S-record file.  */
  744.  
  745. static boolean
  746. srec_get_section_contents (abfd, section, location, offset, count)
  747.      bfd *abfd;
  748.      asection *section;
  749.      PTR location;
  750.      file_ptr offset;
  751.      bfd_size_type count;
  752. {
  753.   if (section->used_by_bfd == NULL)
  754.     {
  755.       section->used_by_bfd = bfd_alloc (abfd, section->_raw_size);
  756.       if (section->used_by_bfd == NULL
  757.       && section->_raw_size != 0)
  758.     return false;
  759.  
  760.       if (! srec_read_section (abfd, section, section->used_by_bfd))
  761.     return false;
  762.     }
  763.  
  764.   memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
  765.       (size_t) count);
  766.  
  767.   return true;
  768. }
  769.  
  770. /* Set the architecture.  We accept an unknown architecture here.  */
  771.  
  772. static boolean
  773. srec_set_arch_mach (abfd, arch, mach)
  774.      bfd *abfd;
  775.      enum bfd_architecture arch;
  776.      unsigned long mach;
  777. {
  778.   if (arch == bfd_arch_unknown)
  779.     {
  780.       abfd->arch_info = &bfd_default_arch_struct;
  781.       return true;
  782.     }
  783.   return bfd_default_set_arch_mach (abfd, arch, mach);
  784. }
  785.  
  786. /* we have to save up all the Srecords for a splurge before output */
  787.  
  788. static boolean
  789. srec_set_section_contents (abfd, section, location, offset, bytes_to_do)
  790.      bfd *abfd;
  791.      sec_ptr section;
  792.      PTR location;
  793.      file_ptr offset;
  794.      bfd_size_type bytes_to_do;
  795. {
  796.   tdata_type *tdata = abfd->tdata.srec_data;
  797.   register srec_data_list_type *entry;
  798.  
  799.   entry = ((srec_data_list_type *)
  800.        bfd_alloc (abfd, sizeof (srec_data_list_type)));
  801.   if (entry == NULL)
  802.     return false;
  803.  
  804.   if (bytes_to_do
  805.       && (section->flags & SEC_ALLOC)
  806.       && (section->flags & SEC_LOAD))
  807.     {
  808.       bfd_byte *data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
  809.       if (data == NULL)
  810.     return false;
  811.       memcpy ((PTR) data, location, (size_t) bytes_to_do);
  812.  
  813.       if ((section->lma + offset + bytes_to_do - 1) <= 0xffff)
  814.     {
  815.  
  816.     }
  817.       else if ((section->lma + offset + bytes_to_do - 1) <= 0xffffff
  818.            && tdata->type < 2)
  819.     {
  820.       tdata->type = 2;
  821.     }
  822.       else
  823.     {
  824.       tdata->type = 3;
  825.     }
  826.  
  827.       entry->data = data;
  828.       entry->where = section->lma + offset;
  829.       entry->size = bytes_to_do;
  830.  
  831.       /* Sort the records by address.  Optimize for the common case of
  832.          adding a record to the end of the list.  */
  833.       if (tdata->tail != NULL
  834.       && entry->where >= tdata->tail->where)
  835.     {
  836.       tdata->tail->next = entry;
  837.       entry->next = NULL;
  838.       tdata->tail = entry;
  839.     }
  840.       else
  841.     {
  842.       register srec_data_list_type **look;
  843.  
  844.       for (look = &tdata->head;
  845.            *look != NULL && (*look)->where < entry->where;
  846.            look = &(*look)->next)
  847.         ;
  848.       entry->next = *look;
  849.       *look = entry;
  850.       if (entry->next == NULL)
  851.         tdata->tail = entry;
  852.     }
  853.     }
  854.   return true;
  855. }
  856.  
  857. /* Write a record of type, of the supplied number of bytes. The
  858.    supplied bytes and length don't have a checksum. That's worked out
  859.    here
  860. */
  861. static boolean
  862. srec_write_record (abfd, type, address, data, end)
  863.      bfd *abfd;
  864.      int type;
  865.      bfd_vma address;
  866.      const bfd_byte *data;
  867.      const bfd_byte *end;
  868. {
  869.   char buffer[MAXCHUNK];
  870.   unsigned int check_sum = 0;
  871.   CONST bfd_byte *src = data;
  872.   char *dst = buffer;
  873.   char *length;
  874.   bfd_size_type wrlen;
  875.  
  876.   *dst++ = 'S';
  877.   *dst++ = '0' + type;
  878.  
  879.   length = dst;
  880.   dst += 2;            /* leave room for dst*/
  881.  
  882.   switch (type)
  883.     {
  884.     case 3:
  885.     case 7:
  886.       TOHEX (dst, (address >> 24), check_sum);
  887.       dst += 2;
  888.     case 8:
  889.     case 2:
  890.       TOHEX (dst, (address >> 16), check_sum);
  891.       dst += 2;
  892.     case 9:
  893.     case 1:
  894.     case 0:
  895.       TOHEX (dst, (address >> 8), check_sum);
  896.       dst += 2;
  897.       TOHEX (dst, (address), check_sum);
  898.       dst += 2;
  899.       break;
  900.  
  901.     }
  902.   for (src = data; src < end; src++)
  903.     {
  904.       TOHEX (dst, *src, check_sum);
  905.       dst += 2;
  906.     }
  907.  
  908.   /* Fill in the length */
  909.   TOHEX (length, (dst - length) / 2, check_sum);
  910.   check_sum &= 0xff;
  911.   check_sum = 255 - check_sum;
  912.   TOHEX (dst, check_sum, check_sum);
  913.   dst += 2;
  914.  
  915.   *dst++ = '\r';
  916.   *dst++ = '\n';
  917.   wrlen = dst - buffer;
  918.   if (bfd_write ((PTR) buffer, 1, wrlen, abfd) != wrlen)
  919.     return false;
  920.   return true;
  921. }
  922.  
  923.  
  924.  
  925. static boolean
  926. srec_write_header (abfd)
  927.      bfd *abfd;
  928. {
  929.   bfd_byte buffer[MAXCHUNK];
  930.   bfd_byte *dst = buffer;
  931.   unsigned int i;
  932.  
  933.   /* I'll put an arbitary 40 char limit on header size */
  934.   for (i = 0; i < 40 && abfd->filename[i]; i++)
  935.     {
  936.       *dst++ = abfd->filename[i];
  937.     }
  938.   return srec_write_record (abfd, 0, 0, buffer, dst);
  939. }
  940.  
  941. static boolean
  942. srec_write_section (abfd, tdata, list)
  943.      bfd *abfd;
  944.      tdata_type *tdata;
  945.      srec_data_list_type *list;
  946. {
  947.   unsigned int bytes_written = 0;
  948.   bfd_byte *location = list->data;
  949.  
  950.   while (bytes_written < list->size)
  951.     {
  952.       bfd_vma address;
  953.  
  954.       unsigned int bytes_this_chunk = list->size - bytes_written;
  955.  
  956.       if (bytes_this_chunk > CHUNK)
  957.     {
  958.       bytes_this_chunk = CHUNK;
  959.     }
  960.  
  961.       address = list->where + bytes_written;
  962.  
  963.       if (! srec_write_record (abfd,
  964.                    tdata->type,
  965.                    address,
  966.                    location,
  967.                    location + bytes_this_chunk))
  968.     return false;
  969.  
  970.       bytes_written += bytes_this_chunk;
  971.       location += bytes_this_chunk;
  972.     }
  973.  
  974.   return true;
  975. }
  976.  
  977. static boolean
  978. srec_write_terminator (abfd, tdata)
  979.      bfd *abfd;
  980.      tdata_type *tdata;
  981. {
  982.   bfd_byte buffer[2];
  983.  
  984.   return srec_write_record (abfd, 10 - tdata->type,
  985.                 abfd->start_address, buffer, buffer);
  986. }
  987.  
  988.  
  989.  
  990. static boolean
  991. srec_write_symbols (abfd)
  992.      bfd *abfd;
  993. {
  994.   char buffer[MAXCHUNK];
  995.   /* Dump out the symbols of a bfd */
  996.   int i;
  997.   int count = bfd_get_symcount (abfd);
  998.  
  999.   if (count)
  1000.     {
  1001.       size_t len;
  1002.       asymbol **table = bfd_get_outsymbols (abfd);
  1003.       sprintf (buffer, "$$ %s\r\n", abfd->filename);
  1004.  
  1005.       len = strlen (buffer);
  1006.       if (bfd_write (buffer, len, 1, abfd) != len)
  1007.     return false;
  1008.  
  1009.       for (i = 0; i < count; i++)
  1010.     {
  1011.       asymbol *s = table[i];
  1012. #if 0
  1013.       int len = strlen (s->name);
  1014.  
  1015.       /* If this symbol has a .[ocs] in it, it's probably a file name
  1016.      and we'll output that as the module name */
  1017.  
  1018.       if (len > 3 && s->name[len - 2] == '.')
  1019.         {
  1020.           int l;
  1021.           sprintf (buffer, "$$ %s\r\n", s->name);
  1022.           l = strlen (buffer);
  1023.           if (bfd_write (buffer, l, 1, abfd) != l)
  1024.         return false;
  1025.         }
  1026.       else
  1027. #endif
  1028.         if (s->flags & (BSF_GLOBAL | BSF_LOCAL)
  1029.         && (s->flags & BSF_DEBUGGING) == 0
  1030.         && s->name[0] != '.'
  1031.         && s->name[0] != 't')
  1032.         {
  1033.           /* Just dump out non debug symbols */
  1034.           bfd_size_type l;
  1035.           char buf2[40], *p;
  1036.  
  1037.           sprintf_vma (buf2,
  1038.                s->value + s->section->output_section->lma
  1039.                + s->section->output_offset);
  1040.           p = buf2;
  1041.           while (p[0] == '0' && p[1] != 0)
  1042.         p++;
  1043.           sprintf (buffer, "  %s $%s\r\n", s->name, p);
  1044.           l = strlen (buffer);
  1045.           if (bfd_write (buffer, l, 1, abfd) != l)
  1046.         return false;
  1047.         }
  1048.     }
  1049.       sprintf (buffer, "$$ \r\n");
  1050.       len = strlen (buffer);
  1051.       if (bfd_write (buffer, len, 1, abfd) != len)
  1052.     return false;
  1053.     }
  1054.  
  1055.   return true;
  1056. }
  1057.  
  1058. static boolean
  1059. internal_srec_write_object_contents (abfd, symbols)
  1060.      bfd *abfd;
  1061.      int symbols;
  1062. {
  1063.   tdata_type *tdata = abfd->tdata.srec_data;
  1064.   srec_data_list_type *list;
  1065.  
  1066.   if (symbols)
  1067.     {
  1068.       if (! srec_write_symbols (abfd))
  1069.     return false;
  1070.     }
  1071.  
  1072.   if (! srec_write_header (abfd))
  1073.     return false;
  1074.  
  1075.   /* Now wander though all the sections provided and output them */
  1076.   list = tdata->head;
  1077.  
  1078.   while (list != (srec_data_list_type *) NULL)
  1079.     {
  1080.       if (! srec_write_section (abfd, tdata, list))
  1081.     return false;
  1082.       list = list->next;
  1083.     }
  1084.   return srec_write_terminator (abfd, tdata);
  1085. }
  1086.  
  1087. static boolean
  1088. srec_write_object_contents (abfd)
  1089.      bfd *abfd;
  1090. {
  1091.   return internal_srec_write_object_contents (abfd, 0);
  1092. }
  1093.  
  1094. static boolean
  1095. symbolsrec_write_object_contents (abfd)
  1096.      bfd *abfd;
  1097. {
  1098.   return internal_srec_write_object_contents (abfd, 1);
  1099. }
  1100.  
  1101. /*ARGSUSED*/
  1102. static int
  1103. srec_sizeof_headers (abfd, exec)
  1104.      bfd *abfd;
  1105.      boolean exec;
  1106. {
  1107.   return 0;
  1108. }
  1109.  
  1110. static asymbol *
  1111. srec_make_empty_symbol (abfd)
  1112.      bfd *abfd;
  1113. {
  1114.   asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
  1115.   if (new)
  1116.     new->the_bfd = abfd;
  1117.   return new;
  1118. }
  1119.  
  1120. /* Return the amount of memory needed to read the symbol table.  */
  1121.  
  1122. static long
  1123. srec_get_symtab_upper_bound (abfd)
  1124.      bfd *abfd;
  1125. {
  1126.   return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
  1127. }
  1128.  
  1129. /* Return the symbol table.  */
  1130.  
  1131. static long
  1132. srec_get_symtab (abfd, alocation)
  1133.      bfd *abfd;
  1134.      asymbol **alocation;
  1135. {
  1136.   unsigned int symcount = bfd_get_symcount (abfd);
  1137.   asymbol *csymbols;
  1138.   unsigned int i;
  1139.  
  1140.   csymbols = abfd->tdata.srec_data->csymbols;
  1141.   if (csymbols == NULL)
  1142.     {
  1143.       asymbol *c;
  1144.       struct srec_symbol *s;
  1145.  
  1146.       csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
  1147.       if (csymbols == NULL && symcount != 0)
  1148.     return false;
  1149.       abfd->tdata.srec_data->csymbols = csymbols;
  1150.  
  1151.       for (s = abfd->tdata.srec_data->symbols, c = csymbols;
  1152.        s != NULL;
  1153.        s = s->next, ++c)
  1154.     {
  1155.       c->the_bfd = abfd;
  1156.       c->name = s->name;
  1157.       c->value = s->val;
  1158.       c->flags = BSF_GLOBAL;
  1159.       c->section = bfd_abs_section_ptr;
  1160.       c->udata.p = NULL;
  1161.     }
  1162.     }
  1163.     
  1164.   for (i = 0; i < symcount; i++)
  1165.     *alocation++ = csymbols++;
  1166.   *alocation = NULL;
  1167.  
  1168.   return symcount;
  1169. }
  1170.  
  1171. /*ARGSUSED*/
  1172. void
  1173. srec_get_symbol_info (ignore_abfd, symbol, ret)
  1174.      bfd *ignore_abfd;
  1175.      asymbol *symbol;
  1176.      symbol_info *ret;
  1177. {
  1178.   bfd_symbol_info (symbol, ret);
  1179. }
  1180.  
  1181. /*ARGSUSED*/
  1182. void
  1183. srec_print_symbol (ignore_abfd, afile, symbol, how)
  1184.      bfd *ignore_abfd;
  1185.      PTR afile;
  1186.      asymbol *symbol;
  1187.      bfd_print_symbol_type how;
  1188. {
  1189.   FILE *file = (FILE *) afile;
  1190.   switch (how)
  1191.     {
  1192.     case bfd_print_symbol_name:
  1193.       fprintf (file, "%s", symbol->name);
  1194.       break;
  1195.     default:
  1196.       bfd_print_symbol_vandf ((PTR) file, symbol);
  1197.       fprintf (file, " %-5s %s",
  1198.            symbol->section->name,
  1199.            symbol->name);
  1200.  
  1201.     }
  1202. }
  1203.  
  1204. #define    srec_close_and_cleanup _bfd_generic_close_and_cleanup
  1205. #define srec_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  1206. #define srec_new_section_hook _bfd_generic_new_section_hook
  1207.  
  1208. #define srec_bfd_is_local_label bfd_generic_is_local_label
  1209. #define srec_get_lineno _bfd_nosymbols_get_lineno
  1210. #define srec_find_nearest_line _bfd_nosymbols_find_nearest_line
  1211. #define srec_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  1212. #define srec_read_minisymbols _bfd_generic_read_minisymbols
  1213. #define srec_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  1214.  
  1215. #define srec_get_reloc_upper_bound \
  1216.   ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
  1217. #define srec_canonicalize_reloc \
  1218.   ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
  1219. #define srec_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
  1220.  
  1221. #define srec_get_section_contents_in_window \
  1222.   _bfd_generic_get_section_contents_in_window
  1223.  
  1224. #define srec_bfd_get_relocated_section_contents \
  1225.   bfd_generic_get_relocated_section_contents
  1226. #define srec_bfd_relax_section bfd_generic_relax_section
  1227. #define srec_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  1228. #define srec_bfd_link_add_symbols _bfd_generic_link_add_symbols
  1229. #define srec_bfd_final_link _bfd_generic_final_link
  1230. #define srec_bfd_link_split_section _bfd_generic_link_split_section
  1231.  
  1232. const bfd_target srec_vec =
  1233. {
  1234.   "srec",            /* name */
  1235.   bfd_target_srec_flavour,
  1236.   BFD_ENDIAN_UNKNOWN,        /* target byte order */
  1237.   BFD_ENDIAN_UNKNOWN,        /* target headers byte order */
  1238.   (HAS_RELOC | EXEC_P |        /* object flags */
  1239.    HAS_LINENO | HAS_DEBUG |
  1240.    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1241.   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1242.    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  1243.   0,                /* leading underscore */
  1244.   ' ',                /* ar_pad_char */
  1245.   16,                /* ar_max_namelen */
  1246.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1247.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1248.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  1249.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1250.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1251.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  1252.  
  1253.   {
  1254.     _bfd_dummy_target,
  1255.     srec_object_p,        /* bfd_check_format */
  1256.     _bfd_dummy_target,
  1257.     _bfd_dummy_target,
  1258.   },
  1259.   {
  1260.     bfd_false,
  1261.     srec_mkobject,
  1262.     _bfd_generic_mkarchive,
  1263.     bfd_false,
  1264.   },
  1265.   {                /* bfd_write_contents */
  1266.     bfd_false,
  1267.     srec_write_object_contents,
  1268.     _bfd_write_archive_contents,
  1269.     bfd_false,
  1270.   },
  1271.  
  1272.   BFD_JUMP_TABLE_GENERIC (srec),
  1273.   BFD_JUMP_TABLE_COPY (_bfd_generic),
  1274.   BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1275.   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1276.   BFD_JUMP_TABLE_SYMBOLS (srec),
  1277.   BFD_JUMP_TABLE_RELOCS (srec),
  1278.   BFD_JUMP_TABLE_WRITE (srec),
  1279.   BFD_JUMP_TABLE_LINK (srec),
  1280.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1281.  
  1282.   (PTR) 0
  1283. };
  1284.  
  1285.  
  1286.  
  1287. const bfd_target symbolsrec_vec =
  1288. {
  1289.   "symbolsrec",            /* name */
  1290.   bfd_target_srec_flavour,
  1291.   BFD_ENDIAN_UNKNOWN,        /* target byte order */
  1292.   BFD_ENDIAN_UNKNOWN,        /* target headers byte order */
  1293.   (HAS_RELOC | EXEC_P |        /* object flags */
  1294.    HAS_LINENO | HAS_DEBUG |
  1295.    HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1296.   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1297.    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  1298.   0,                /* leading underscore */
  1299.   ' ',                /* ar_pad_char */
  1300.   16,                /* ar_max_namelen */
  1301.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1302.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1303.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* data */
  1304.   bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1305.   bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1306.   bfd_getb16, bfd_getb_signed_16, bfd_putb16,    /* hdrs */
  1307.  
  1308.   {
  1309.     _bfd_dummy_target,
  1310.     symbolsrec_object_p,    /* bfd_check_format */
  1311.     _bfd_dummy_target,
  1312.     _bfd_dummy_target,
  1313.   },
  1314.   {
  1315.     bfd_false,
  1316.     srec_mkobject,
  1317.     _bfd_generic_mkarchive,
  1318.     bfd_false,
  1319.   },
  1320.   {                /* bfd_write_contents */
  1321.     bfd_false,
  1322.     symbolsrec_write_object_contents,
  1323.     _bfd_write_archive_contents,
  1324.     bfd_false,
  1325.   },
  1326.  
  1327.   BFD_JUMP_TABLE_GENERIC (srec),
  1328.   BFD_JUMP_TABLE_COPY (_bfd_generic),
  1329.   BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1330.   BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1331.   BFD_JUMP_TABLE_SYMBOLS (srec),
  1332.   BFD_JUMP_TABLE_RELOCS (srec),
  1333.   BFD_JUMP_TABLE_WRITE (srec),
  1334.   BFD_JUMP_TABLE_LINK (srec),
  1335.   BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1336.  
  1337.   (PTR) 0
  1338. };
  1339.