home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / gnu / djgpp / src / binutils.2 / bfd / tekhex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-05-30  |  24.6 KB  |  1,045 lines

  1. /* BFD backend for Extended Tektronix Hex Format  objects.
  2.    Copyright (C) 1992, 1993 Free Software Foundation, Inc.
  3.  
  4.    Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
  5.  
  6. This file is part of BFD, the Binary File Descriptor library.
  7.  
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License as published by
  10. the Free Software Foundation; either version 2 of the License, or
  11. (at your option) any later version.
  12.  
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. GNU General Public License for more details.
  17.  
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  21.  
  22. /*
  23. SUBSECTION
  24.     Tektronix Hex Format handling
  25.  
  26. DESCRIPTION
  27.     
  28.     Tek Hex records can hold symbols and data, but not
  29.     relocations. Their main application is communication with
  30.     devices like PROM programmers and ICE equipment.
  31.     
  32.     It seems that the sections are descibed as being really big,
  33.         the example I have says that the text section is 0..ffffffff.
  34.     BFD would barf with this, many apps would try to alloc 4GB to
  35.     read in the file.
  36.  
  37.     Tex Hex may contain many sections, but the data which comes in
  38.     has no tag saying which section it belongs to, so we create
  39.     one section for each block of data, called "blknnnn" which we
  40.     stick all the data into.
  41.  
  42.     TekHex may come out of     order and there is no header, so an
  43.     initial scan is required  to discover the minimum and maximum
  44.     addresses used to create the vma and size of the sections we
  45.     create.
  46.     We read in the data into pages of CHUNK_MASK+1 size and read
  47.     them out from that whenever we need to.
  48.  
  49.     Any number of sections may be created for output, we save them
  50.     up and output them when it's time to close the bfd.
  51.  
  52.  
  53.     A TekHex record looks like:
  54. EXAMPLE
  55.     %<block length><type><checksum><stuff><cr>
  56.     
  57. DESCRIPTION
  58.     Where
  59.     o length
  60.     is the number of bytes in the record not including the % sign.
  61.     o type
  62.     is one of:
  63.     3) symbol record
  64.     6) data record
  65.     8) termination record
  66.     
  67.  
  68. The data can come out of order, and may be discontigous. This is a
  69. serial protocol, so big files are unlikely, so we keep a list of 8k chunks
  70. */
  71.  
  72. #include "bfd.h"
  73. #include "sysdep.h"
  74. #include "libbfd.h"
  75.  
  76. typedef struct
  77.   {
  78.     bfd_vma low;
  79.     bfd_vma high;
  80.   } addr_range_type;
  81.  
  82. typedef struct tekhex_symbol_struct
  83.   {
  84.  
  85.     asymbol symbol;
  86.     struct tekhex_symbol_struct *prev;
  87.  
  88.   } tekhex_symbol_type;
  89.  
  90. static char digs[] = "0123456789ABCDEF";
  91.  
  92. static char sum_block[256];
  93.  
  94. /* Horrible ascii dependent macros for converting between hex and
  95.    binary */
  96.  
  97. #define CHARS_IN_SET 256
  98. static char hex_value[CHARS_IN_SET];
  99.  
  100. #define NOT_HEX 20
  101. #define NIBBLE(x) hex_value[x]
  102. #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
  103. #define TOHEX(d,x) \
  104. (d)[1] = digs[(x) & 0xf]; \
  105. (d)[0] = digs[((x)>>4)&0xf];
  106. #define    ISHEX(x)  (hex_value[x] != NOT_HEX)
  107.  
  108. /*
  109. Here's an example
  110. %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
  111. %1B3709T_SEGMENT1108FFFFFFFF
  112. %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
  113. %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
  114. %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
  115. %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
  116. %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
  117. %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
  118. %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
  119. %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
  120. %2734D9T_SEGMENT8Bvoid$t15$151035_main10
  121. %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
  122. %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
  123. %07 8 10 10
  124.  
  125. explanation:
  126. %3A6C6480004E56FFFC4E717063B0AEFFFC6D0652AEFFFC60F24E5E4E75
  127.  ^ ^^ ^     ^-data
  128.  | || +------ 4 char integer 0x8000
  129.  | |+-------- checksum
  130.  | +--------- type 6 (data record)
  131.  +----------- length 3a chars
  132.  <---------------------- 3a (58 chars) ------------------->
  133.  
  134. %1B3709T_SEGMENT1108FFFFFFFF
  135.       ^         ^^ ^- 8 character integer 0xffffffff
  136.       |         |+-   1 character integer 0
  137.       |         +--   type 1 symbol (section definition)
  138.       +------------   9 char symbol T_SEGMENT
  139.  
  140. %2B3AB9T_SEGMENT7Dgcc_compiled$1087hello$c10
  141. %373829T_SEGMENT80int$t1$r1$$214741080char$t2$r2$0$12710
  142. %373769T_SEGMENT80long$int$t3$r1$$1080unsigned$int$t4$10
  143. %373CA9T_SEGMENT80long$unsigned$in1080short$int$t6$r1$10
  144. %373049T_SEGMENT80long$long$int$t71080short$unsigned$i10
  145. %373A29T_SEGMENT80long$long$unsign1080signed$char$t10$10
  146. %373D69T_SEGMENT80unsigned$char$t11080float$t12$r1$4$010
  147. %373D19T_SEGMENT80double$t13$r1$8$1080long$double$t14$10
  148. %2734D9T_SEGMENT8Bvoid$t15$151035_main10
  149. %2F3CA9T_SEGMENT81$1081$1681$1E81$21487main$F110
  150. %2832F9T_SEGMENT83i$18FFFFFFFC81$1481$214
  151. %0781010
  152.  
  153. Turns into
  154. sac@thepub$ ./objdump -dx -m m68k f
  155.  
  156. f:     file format tekhex
  157. -----x--- 9/55728 -134219416 Sep 29 15:13 1995 f
  158. architecture: UNKNOWN!, flags 0x00000010:
  159. HAS_SYMS
  160. start address 0x00000000
  161. SECTION 0 [D00000000]    : size 00020000 vma 00000000 align 2**0
  162.  ALLOC, LOAD
  163. SECTION 1 [D00008000]    : size 00002001 vma 00008000 align 2**0
  164.  
  165. SECTION 2 [T_SEGMENT]    : size ffffffff vma 00000000 align 2**0
  166.  
  167. SYMBOL TABLE:
  168. 00000000  g       T_SEGMENT gcc_compiled$
  169. 00000000  g       T_SEGMENT hello$c
  170. 00000000  g       T_SEGMENT int$t1$r1$$21474
  171. 00000000  g       T_SEGMENT char$t2$r2$0$127
  172. 00000000  g       T_SEGMENT long$int$t3$r1$$
  173. 00000000  g       T_SEGMENT unsigned$int$t4$
  174. 00000000  g       T_SEGMENT long$unsigned$in
  175. 00000000  g       T_SEGMENT short$int$t6$r1$
  176. 00000000  g       T_SEGMENT long$long$int$t7
  177. 00000000  g       T_SEGMENT short$unsigned$i
  178. 00000000  g       T_SEGMENT long$long$unsign
  179. 00000000  g       T_SEGMENT signed$char$t10$
  180. 00000000  g       T_SEGMENT unsigned$char$t1
  181. 00000000  g       T_SEGMENT float$t12$r1$4$0
  182. 00000000  g       T_SEGMENT double$t13$r1$8$
  183. 00000000  g       T_SEGMENT long$double$t14$
  184. 00000000  g       T_SEGMENT void$t15$15
  185. 00000000  g       T_SEGMENT _main
  186. 00000000  g       T_SEGMENT $
  187. 00000000  g       T_SEGMENT $
  188. 00000000  g       T_SEGMENT $
  189. 00000010  g       T_SEGMENT $
  190. 00000000  g       T_SEGMENT main$F1
  191. fcffffff  g       T_SEGMENT i$1
  192. 00000000  g       T_SEGMENT $
  193. 00000010  g       T_SEGMENT $
  194.  
  195.  
  196. RELOCATION RECORDS FOR [D00000000]: (none)
  197.  
  198. RELOCATION RECORDS FOR [D00008000]: (none)
  199.  
  200. RELOCATION RECORDS FOR [T_SEGMENT]: (none)
  201.  
  202. Disassembly of section D00000000:
  203. ...
  204. 00008000 ($+)7ff0 linkw fp,#-4
  205. 00008004 ($+)7ff4 nop
  206. 00008006 ($+)7ff6 movel #99,d0
  207. 00008008 ($+)7ff8 cmpl fp@(-4),d0
  208. 0000800c ($+)7ffc blts 00008014 ($+)8004
  209. 0000800e ($+)7ffe addql #1,fp@(-4)
  210. 00008012 ($+)8002 bras 00008006 ($+)7ff6
  211. 00008014 ($+)8004 unlk fp
  212. 00008016 ($+)8006 rts
  213. ...
  214.  
  215. */
  216.  
  217. static void
  218. DEFUN_VOID (tekhex_init)
  219. {
  220.   unsigned int i;
  221.   static boolean inited = false;
  222.   int val;
  223.  
  224.   if (inited == false)
  225.     {
  226.  
  227.       inited = true;
  228.  
  229.       for (i = 0; i < CHARS_IN_SET; i++)
  230.     {
  231.       hex_value[i] = NOT_HEX;
  232.     }
  233.  
  234.       for (i = 0; i < 10; i++)
  235.     {
  236.       hex_value[i + '0'] = i;
  237.  
  238.     }
  239.       for (i = 0; i < 6; i++)
  240.     {
  241.       hex_value[i + 'a'] = i + 10;
  242.       hex_value[i + 'A'] = i + 10;
  243.     }
  244.       val = 0;
  245.       for (i = 0; i < 10; i++)
  246.     {
  247.       sum_block[i + '0'] = val++;
  248.     }
  249.       for (i = 'A'; i <= 'Z'; i++)
  250.     {
  251.       sum_block[i] = val++;
  252.     }
  253.       sum_block['$'] = val++;
  254.       sum_block['%'] = val++;
  255.       sum_block['.'] = val++;
  256.       sum_block['_'] = val++;
  257.       for (i = 'a'; i <= 'z'; i++)
  258.     {
  259.       sum_block[i] = val++;
  260.     }
  261.     }
  262. }
  263.  
  264. /* The maximum number of bytes on a line is FF */
  265. #define MAXCHUNK 0xff
  266. /* The number of bytes we fit onto a line on output */
  267. #define CHUNK 21
  268.  
  269. /* We cannot output our tekhexords as we see them, we have to glue them
  270.    together, this is done in this structure : */
  271.  
  272. struct tekhex_data_list_struct
  273. {
  274.   unsigned char *data;
  275.   bfd_vma where;
  276.   bfd_size_type size;
  277.   struct tekhex_data_list_struct *next;
  278.  
  279. };
  280. typedef struct tekhex_data_list_struct tekhex_data_list_type;
  281.  
  282. #define CHUNK_MASK 0x1fff
  283.  
  284. struct data_struct
  285.   {
  286.     char chunk_data[CHUNK_MASK + 1];
  287.     char chunk_init[CHUNK_MASK + 1];
  288.     bfd_vma vma;
  289.     struct data_struct *next;
  290.   };
  291.  
  292. typedef struct tekhex_data_struct
  293. {
  294.   tekhex_data_list_type *head;
  295.   unsigned int type;
  296.   struct tekhex_symbol_struct *symbols;
  297.   struct data_struct *data;
  298. } tdata_type;
  299.  
  300. #define enda(x) (x->vma + x->size)
  301.  
  302. static bfd_vma
  303. DEFUN (getvalue, (srcp),
  304.        char **srcp)
  305. {
  306.   char *src = *srcp;
  307.   bfd_vma value = 0;
  308.   unsigned int len = hex_value[*src++];
  309.  
  310.   if (len == 0)
  311.     len = 16;
  312.   while (len--)
  313.     {
  314.       value = value << 4 | hex_value[*src++];
  315.     }
  316.   *srcp = src;
  317.   return value;
  318. }
  319.  
  320. static unsigned int
  321. DEFUN (getsym, (dstp, srcp),
  322.        char *dstp AND
  323.        char **srcp)
  324. {
  325.   char *src = *srcp;
  326.   unsigned int i;
  327.   unsigned int len = hex_value[*src++];
  328.  
  329.   if (len == 0)
  330.     len = 16;
  331.   for (i = 0; i < len; i++)
  332.     dstp[i] = src[i];
  333.   dstp[i] = 0;
  334.   *srcp = src + i;
  335.   return len;
  336. }
  337.  
  338. struct data_struct *
  339. DEFUN (find_chunk, (abfd, vma),
  340.        bfd * abfd AND
  341.        bfd_vma vma)
  342. {
  343.   struct data_struct *d = abfd->tdata.tekhex_data->data;
  344.  
  345.   vma &= ~CHUNK_MASK;
  346.   while (d && (d->vma) != vma)
  347.     {
  348.       d = d->next;
  349.     }
  350.   if (!d)
  351.     {
  352.       char *sname = bfd_alloc (abfd, 12);
  353.       asection *s;
  354.  
  355.       /* No chunk for this address, so make one up */
  356.       d = (struct data_struct *)
  357.     bfd_alloc (abfd, sizeof (struct data_struct));
  358.  
  359.       memset (d->chunk_init, 0, CHUNK_MASK + 1);
  360.       memset (d->chunk_data, 0, CHUNK_MASK + 1);
  361.       d->next = abfd->tdata.tekhex_data->data;
  362.       d->vma = vma;
  363.       abfd->tdata.tekhex_data->data = d;
  364.     }
  365.   return d;
  366. }
  367.  
  368. DEFUN (insert_byte, (abfd, value, addr),
  369.        bfd * abfd AND
  370.        int value AND
  371.        bfd_vma addr)
  372. {
  373.   /* Find the chunk that this byte needs and put it in */
  374.   struct data_struct *d = find_chunk (abfd, addr);
  375.  
  376.   d->chunk_data[addr & CHUNK_MASK] = value;
  377.   d->chunk_init[addr & CHUNK_MASK] = 1;
  378.  
  379. }
  380.  
  381. /* The first pass is to find the names of all the sections, and see
  382.   how big the data is */
  383. static void
  384. DEFUN (first_phase, (abfd, type, src),
  385.        bfd * abfd AND
  386.        char type AND
  387.        char *src)
  388. {
  389.   asection *section = &bfd_abs_section;
  390.   int len;
  391.   char sym[17];            /* A symbol can only be 16chars long */
  392.  
  393.   switch (type)
  394.     {
  395.     case '6':
  396.       /* Data record - read it and store it */
  397.       {
  398.     bfd_vma addr = getvalue (&src);
  399.  
  400.     while (*src)
  401.       {
  402.         insert_byte (abfd, HEX (src), addr);
  403.         src += 2;
  404.         addr++;
  405.       }
  406.       }
  407.  
  408.       return;
  409.     case '3':
  410.       /* Symbol record, read the segment */
  411.       len = getsym (sym, &src);
  412.       section = bfd_get_section_by_name (abfd, sym);
  413.       if (section == (asection *) NULL)
  414.     {
  415.       char *n = bfd_alloc (abfd, len + 1);
  416.  
  417.       memcpy (n, sym, len + 1);
  418.       section = bfd_make_section (abfd, n);
  419.     }
  420.       while (*src)
  421.     {
  422.       switch (*src)
  423.         {
  424.           asection *s;
  425.  
  426.         case '1':        /* section range */
  427.           src++;
  428.           section->vma = getvalue (&src);
  429.           section->_raw_size = getvalue (&src) - section->vma;
  430.           section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  431.           break;
  432.  
  433.         case '2':
  434.         case '3':
  435.         case '4':
  436.         case '6':
  437.         case '7':
  438.         case '8':
  439.           /* Symbols, add to section */
  440.           {
  441.         tekhex_symbol_type *new =
  442.         (tekhex_symbol_type *) bfd_alloc (abfd,
  443.                            sizeof (tekhex_symbol_type));
  444.         char type = (*src);
  445.  
  446.         new->symbol.the_bfd = abfd;
  447.         src++;
  448.         abfd->symcount++;
  449.         abfd->flags |= HAS_SYMS;
  450.         new->prev = abfd->tdata.tekhex_data->symbols;
  451.         abfd->tdata.tekhex_data->symbols = new;
  452.         len = getsym (sym, &src);
  453.         new->symbol.name = bfd_alloc (abfd, len + 1);
  454.         memcpy ((char *) (new->symbol.name), sym, len + 1);
  455.         new->symbol.section = section;
  456.         if (type <= '4')
  457.           new->symbol.flags = (BSF_GLOBAL | BSF_EXPORT);
  458.         else
  459.           new->symbol.flags = BSF_LOCAL;
  460.         new->symbol.value = getvalue (&src) - section->vma;
  461.           }
  462.         }
  463.     }
  464.     }
  465. }
  466.  
  467. /* Pass over an tekhex, calling one of the above functions on each
  468.    record.  */
  469.  
  470. static void
  471. DEFUN (pass_over, (abfd, func),
  472.        bfd * abfd AND
  473.        void (*func) PARAMS ((bfd *, char, char *)))
  474. {
  475.   unsigned int chars_on_line;
  476.   boolean eof = false;
  477.  
  478.   /* To the front of the file */
  479.   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  480.   while (eof == false)
  481.     {
  482.       char buffer[MAXCHUNK];
  483.       char *src = buffer;
  484.       char type;
  485.       bfd_vma address = 0;
  486.  
  487.       /* Find first '%' */
  488.       eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1);
  489.       while (*src != '%' && !eof)
  490.     {
  491.       eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1);
  492.     }
  493.       if (eof)
  494.     break;
  495.       src++;
  496.  
  497.       /* Fetch the type and the length and the checksum */
  498.       bfd_read (src, 1, 5, abfd);
  499.  
  500.       type = src[2];
  501.  
  502.       if (!ISHEX (src[0]) || !ISHEX (src[1]))
  503.     break;
  504.  
  505.       chars_on_line = HEX (src) - 5;    /* Already read five char */
  506.  
  507.       bfd_read (src, 1, chars_on_line, abfd);
  508.       src[chars_on_line] = 0;    /* put a null at the end */
  509.  
  510.       func (abfd, type, src);
  511.     }
  512.  
  513. }
  514.  
  515. unsigned int
  516. DEFUN (tekhex_get_symtab, (abfd, table),
  517.        bfd * abfd AND
  518.        asymbol ** table)
  519. {
  520.   tekhex_symbol_type *p = abfd->tdata.tekhex_data->symbols;
  521.   unsigned int c = bfd_get_symcount (abfd);
  522.  
  523.   table[c] = 0;
  524.   while (p)
  525.     {
  526.       table[--c] = &(p->symbol);
  527.       p = p->prev;
  528.     }
  529.  
  530.   return bfd_get_symcount (abfd);
  531. }
  532.  
  533. unsigned int
  534. DEFUN (tekhex_get_symtab_upper_bound, (abfd),
  535.        bfd * abfd)
  536. {
  537.   return (abfd->symcount + 1) * (sizeof (struct tekhex_asymbol_struct *));
  538.  
  539. }
  540.  
  541. static boolean
  542. DEFUN (tekhex_mkobject, (abfd),
  543.        bfd * abfd)
  544. {
  545.   tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  546.  
  547.   abfd->tdata.tekhex_data = tdata;
  548.   tdata->type = 1;
  549.   tdata->head = (tekhex_data_list_type *) NULL;
  550.   tdata->symbols = (struct tekhex_symbol_struct *) NULL;
  551.   tdata->data = (struct data_struct *) NULL;
  552.   return true;
  553.  
  554. }
  555.  
  556. /*
  557.   Return true if the file looks like it's in TekHex format. Just look
  558.   for a percent sign and some hex digits */
  559.  
  560. static bfd_target *
  561. DEFUN (tekhex_object_p, (abfd),
  562.        bfd * abfd)
  563. {
  564.   char b[4];
  565.   asection *section;
  566.  
  567.   tekhex_init ();
  568.  
  569.   bfd_seek (abfd, (file_ptr) 0, SEEK_SET);
  570.   bfd_read (b, 1, 4, abfd);
  571.  
  572.   if (b[0] != '%' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
  573.     return (bfd_target *) NULL;
  574.  
  575.   tekhex_mkobject (abfd);
  576.  
  577.   pass_over (abfd, first_phase);
  578.   return abfd->xvec;
  579. }
  580.  
  581. static boolean
  582. DEFUN (move_section_contents, (abfd, section, locationp, offset, count,
  583.                    get),
  584.  
  585.        bfd * abfd AND
  586.        asection * section AND
  587.        PTR locationp AND
  588.        file_ptr offset AND
  589.        bfd_size_type count AND
  590.        boolean get)
  591. {
  592.   bfd_vma addr;
  593.   char *location = (char *) locationp;
  594.   bfd_vma prev_number = 1;    /* Nothing can have this as a high bit*/
  595.   struct data_struct *d = (struct data_struct *) NULL;
  596.  
  597.   for (addr = section->vma; count != 0; count--, addr++)
  598.     {
  599.  
  600.       bfd_vma chunk_number = addr & ~CHUNK_MASK;    /* Get high bits of address */
  601.       bfd_vma low_bits = addr & CHUNK_MASK;
  602.  
  603.       if (chunk_number != prev_number)
  604.     {
  605.       /* Different chunk, so move pointer */
  606.       d = find_chunk (abfd, chunk_number);
  607.     }
  608.  
  609.       if (get)
  610.     {
  611.       if (d->chunk_init[low_bits])
  612.         {
  613.           *location = d->chunk_data[low_bits];
  614.         }
  615.       else
  616.         {
  617.           *location = 0;
  618.         }
  619.     }
  620.       else
  621.     {
  622.       d->chunk_data[low_bits] = *location;
  623.       d->chunk_init[low_bits] = (*location != 0);
  624.     }
  625.  
  626.       location++;
  627.  
  628.     }
  629.  
  630. }
  631. static boolean
  632. DEFUN (tekhex_get_section_contents, (abfd, section, locationp, offset, count),
  633.        bfd * abfd AND
  634.        asection * section AND
  635.        PTR locationp AND
  636.        file_ptr offset AND
  637.        bfd_size_type count)
  638. {
  639.   if (section->flags & (SEC_LOAD | SEC_ALLOC))
  640.     {
  641.       move_section_contents (abfd, section, locationp, offset, count, true);
  642.       return true;
  643.     }
  644.   else
  645.     return false;
  646. }
  647.  
  648. boolean
  649. DEFUN (tekhex_set_arch_mach, (abfd, arch, machine),
  650.        bfd * abfd AND
  651.        enum bfd_architecture arch AND
  652.        unsigned long machine)
  653. {
  654.   return bfd_default_set_arch_mach (abfd, arch, machine);
  655. }
  656.  
  657. /* we have to save up all the Tekhexords for a splurge before output,
  658.     */
  659.  
  660. static boolean
  661. DEFUN (tekhex_set_section_contents, (abfd, section, locationp, offset, bytes_to_do),
  662.        bfd * abfd AND
  663.        sec_ptr section AND
  664.        PTR locationp AND
  665.        file_ptr offset AND
  666.        bfd_size_type bytes_to_do)
  667. {
  668.  
  669.   if (abfd->output_has_begun == false)
  670.     {
  671.       /* The first time around, allocate enough sections to hold all the chunks */
  672.       asection *s = abfd->sections;
  673.       bfd_vma vma;
  674.  
  675.       for (s = abfd->sections; s; s = s->next)
  676.     {
  677.       if (s->flags & SEC_LOAD)
  678.         {
  679.           for (vma = s->vma & ~CHUNK_MASK;
  680.            vma < s->vma + s->_raw_size;
  681.            vma += CHUNK_MASK)
  682.         find_chunk (abfd, vma);
  683.         }
  684.     }
  685.  
  686.     }
  687.   if (section->flags & (SEC_LOAD | SEC_ALLOC))
  688.     {
  689.       move_section_contents (abfd, section, locationp, offset, bytes_to_do, false);
  690.       return true;
  691.     }
  692.   else
  693.     return false;
  694.  
  695. }
  696.  
  697. static void
  698. DEFUN (writevalue, (dst, value),
  699.        char **dst AND
  700.        bfd_vma value)
  701. {
  702.   char *p = *dst;
  703.   int len;
  704.   int shift;
  705.  
  706.   for (len = 8, shift = 28; shift; shift -= 4, len--)
  707.     {
  708.       if ((value >> shift) & 0xf)
  709.     {
  710.       *p++ = len + '0';
  711.       while (len)
  712.         {
  713.           *p++ = digs[(value >> shift) & 0xf];
  714.           shift -= 4;
  715.           len--;
  716.         }
  717.       *dst = p;
  718.       return;
  719.  
  720.     }
  721.     }
  722.   *p++ = '1';
  723.   *p++ = '0';
  724.   *dst = p;
  725. }
  726.  
  727. static void
  728. DEFUN (writesym, (dst, sym),
  729.        char **dst AND
  730.        CONST char *sym)
  731. {
  732.   char *p = *dst;
  733.   int len = (sym ? strlen (sym) : 0);
  734.  
  735.   if (len >= 16)
  736.     {
  737.       *p++ = '0';
  738.       len = 16;
  739.     }
  740.  
  741.   else
  742.     {
  743.       if (len == 0)
  744.     {
  745.       *p++ = '1';
  746.       sym = "$";
  747.       len = 1;
  748.     }
  749.       else
  750.     {
  751.       *p++ = digs[len];
  752.     }
  753.     }
  754.  
  755.   while (len--)
  756.     {
  757.       *p++ = *sym++;
  758.     }
  759.   *dst = p;
  760. }
  761.  
  762. static void
  763. DEFUN (out, (abfd, type, start, end),
  764.        bfd * abfd AND
  765.        char type AND
  766.        char *start AND
  767.        char *end)
  768. {
  769.   int sum = 0;
  770.   char *s;
  771.   char front[6];
  772.  
  773.   front[0] = '%';
  774.   TOHEX (front + 1, end - start + 5);
  775.   front[3] = type;
  776.  
  777.   for (s = start; s < end; s++)
  778.     {
  779.       sum += sum_block[*s];
  780.     }
  781.  
  782.   sum += sum_block[front[1]];    /*  length */
  783.   sum += sum_block[front[2]];
  784.   sum += sum_block[front[3]];    /* type */
  785.   TOHEX (front + 4, sum);
  786.   bfd_write (front, 1, 6, abfd);
  787.   end[0] = '\n';
  788.   bfd_write (start, 1, end - start + 1, abfd);
  789.  
  790. }
  791.  
  792. static boolean
  793. DEFUN (tekhex_write_object_contents, (abfd),
  794.        bfd * abfd)
  795. {
  796.   int bytes_written;
  797.   char buffer[100];
  798.   asymbol **p;
  799.   tdata_type *tdata = abfd->tdata.tekhex_data;
  800.   tekhex_data_list_type *list;
  801.   asection *s;
  802.   struct data_struct *d;
  803.  
  804.   bytes_written = 0;
  805.  
  806.   /* And the raw data */
  807.   for (d = abfd->tdata.tekhex_data->data;
  808.        d != (struct data_struct *) NULL;
  809.        d = d->next)
  810.     {
  811.       int low;
  812.  
  813.       CONST int span = 32;
  814.       int addr;
  815.  
  816.       /* Write it in blocks of 32 bytes */
  817.  
  818.       for (addr = 0; addr < CHUNK_MASK + 1; addr += span)
  819.     {
  820.       int need = 0;
  821.  
  822.       /* Check to see if necessary */
  823.       for (low = 0; !need && low < span; low++)
  824.         {
  825.           if (d->chunk_init[addr + low])
  826.         need = 1;
  827.         }
  828.       if (need)
  829.         {
  830.           char *dst = buffer;
  831.  
  832.           writevalue (&dst, addr + d->vma);
  833.           for (low = 0; low < span; low++)
  834.         {
  835.           TOHEX (dst, d->chunk_data[addr + low]);
  836.           dst += 2;
  837.         }
  838.           out (abfd, '6', buffer, dst);
  839.         }
  840.     }
  841.     }
  842.   /* write all the section headers for the sections */
  843.   for (s = abfd->sections; s != (asection *) NULL; s = s->next)
  844.     {
  845.       char *dst = buffer;
  846.  
  847.       writesym (&dst, s->name);
  848.       *dst++ = '1';
  849.       writevalue (&dst, s->vma);
  850.       writevalue (&dst, s->vma + s->_raw_size);
  851.       out (abfd, '3', buffer, dst);
  852.     }
  853.  
  854.   /* And the symbols */
  855.   for (p = abfd->outsymbols; *p; p++)
  856.     {
  857.       int section_code = bfd_decode_symclass (*p);
  858.  
  859.       if (section_code != '?')
  860.     {            /* do not include debug symbols */
  861.       asymbol *s = *p;
  862.       char *dst = buffer;
  863.  
  864.       writesym (&dst, s->section->name);
  865.  
  866.       switch (section_code)
  867.         {
  868.         case 'A':
  869.           *dst++ = '2';
  870.           break;
  871.         case 'a':
  872.           *dst++ = '6';
  873.           break;
  874.         case 'D':
  875.         case 'B':
  876.         case 'O':
  877.           *dst++ = '4';
  878.           break;
  879.         case 'd':
  880.         case 'b':
  881.         case 'o':
  882.           *dst++ = '8';
  883.           break;
  884.         case 'T':
  885.           *dst++ = '3';
  886.           break;
  887.         case 't':
  888.           *dst++ = '7';
  889.           break;
  890.         case 'C':
  891.         case 'U':
  892.           bfd_error = wrong_format;
  893.           return false;
  894.         }
  895.  
  896.       writesym (&dst, s->name);
  897.       writevalue (&dst, s->value + s->section->vma);
  898.       out (abfd, '3', buffer, dst);
  899.     }
  900.     }
  901.  
  902.   /* And the terminator */
  903.   bfd_write ("%7081010\n", 1, 9, abfd);
  904.   return true;
  905. }
  906.  
  907. static int
  908. DEFUN (tekhex_sizeof_headers, (abfd, exec),
  909.        bfd * abfd AND
  910.        boolean exec)
  911. {
  912.   return 0;
  913. }
  914.  
  915. static asymbol *
  916. DEFUN (tekhex_make_empty_symbol, (abfd),
  917.        bfd * abfd)
  918. {
  919.   tekhex_symbol_type *new =
  920.   (tekhex_symbol_type *) bfd_zalloc (abfd, sizeof (struct tekhex_symbol_struct));
  921.  
  922.   new->symbol.the_bfd = abfd;
  923.   new->prev = (struct tekhex_symbol_struct *) NULL;
  924.   return &(new->symbol);
  925. }
  926.  
  927. static void
  928. DEFUN (tekhex_print_symbol, (ignore_abfd, filep, symbol, how),
  929.        bfd * ignore_abfd AND
  930.        PTR filep AND
  931.        asymbol * symbol AND
  932.        bfd_print_symbol_type how)
  933. {
  934.   FILE *file = (FILE *) filep;
  935.  
  936.   switch (how)
  937.     {
  938.     case bfd_print_symbol_name:
  939.       fprintf (file, "%s", symbol->name);
  940.       break;
  941.     case bfd_print_symbol_more:
  942.       break;
  943.     case bfd_print_symbol_nm:
  944.       {
  945.     int section_code = bfd_decode_symclass (symbol);
  946.  
  947.     if (section_code == 'U')
  948.       fprintf (file, "        ");
  949.     fprintf_vma (file, symbol->value + symbol->section->vma);
  950.     if (section_code == '?')
  951.       {
  952.         fprintf (file, " ?");
  953.       }
  954.     else
  955.       fprintf (file, " %c", section_code);
  956.     if (symbol->name)
  957.       fprintf (file, " %s", symbol->name);
  958.       }
  959.       break;
  960.     case bfd_print_symbol_all:
  961.       {
  962.     CONST char *section_name = symbol->section->name;
  963.  
  964.     bfd_print_symbol_vandf ((PTR) file, symbol);
  965.  
  966.     fprintf (file, " %-5s %s",
  967.          section_name,
  968.          symbol->name);
  969.       }
  970.     }
  971. }
  972.  
  973. #define FOO PROTO
  974. #define tekhex_new_section_hook (FOO(boolean, (*), (bfd *, asection *)))bfd_true
  975. #define tekhex_get_reloc_upper_bound (FOO(unsigned int, (*),(bfd*, asection *)))bfd_false
  976. #define tekhex_canonicalize_reloc (FOO(unsigned int, (*),(bfd*,asection *, arelent **, asymbol **))) bfd_0
  977.  
  978. #define tekhex_openr_next_archived_file (FOO(bfd *, (*), (bfd*,bfd*))) bfd_nullvoidptr
  979. #define tekhex_find_nearest_line (FOO(boolean, (*),(bfd*,asection*,asymbol**,bfd_vma, CONST char**, CONST char**, unsigned int *))) bfd_false
  980. #define tekhex_generic_stat_arch_elt  (FOO(int, (*), (bfd *,struct stat *))) bfd_0
  981.  
  982. #define tekhex_core_file_failing_command (char *(*)())(bfd_nullvoidptr)
  983. #define tekhex_core_file_failing_signal (int (*)())bfd_0
  984. #define tekhex_core_file_matches_executable_p (FOO(boolean, (*),(bfd*, bfd*)))bfd_false
  985. #define tekhex_slurp_armap bfd_true
  986. #define tekhex_slurp_extended_name_table bfd_true
  987. #define tekhex_truncate_arname (void (*)())bfd_nullvoidptr
  988. #define tekhex_write_armap  (FOO( boolean, (*),(bfd *, unsigned int, struct orl *, unsigned int, int))) bfd_nullvoidptr
  989. #define tekhex_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
  990. #define    tekhex_close_and_cleanup    bfd_generic_close_and_cleanup
  991. #define tekhex_bfd_debug_info_start bfd_void
  992. #define tekhex_bfd_debug_info_end bfd_void
  993. #define tekhex_bfd_debug_info_accumulate  (FOO(void, (*), (bfd *,     asection *))) bfd_void
  994. #define tekhex_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  995. #define tekhex_bfd_relax_section bfd_generic_relax_section
  996. #define tekhex_bfd_seclet_link bfd_generic_seclet_link
  997. #define tekhex_bfd_reloc_type_lookup \
  998.   ((CONST struct reloc_howto_struct *(*) PARAMS ((bfd *, bfd_reloc_code_real_type))) bfd_nullvoidptr)
  999. #define tekhex_bfd_make_debug_symbol \
  1000.   ((asymbol *(*) PARAMS ((bfd *, void *, unsigned long))) bfd_nullvoidptr)
  1001.  
  1002. bfd_target tekhex_vec =
  1003. {
  1004.   "tekhex",            /* name */
  1005.   bfd_target_tekhex_flavour,
  1006.   true,                /* target byte order */
  1007.   true,                /* target headers byte order */
  1008.   (EXEC_P |            /* object flags */
  1009.    HAS_SYMS | HAS_LINENO | HAS_DEBUG | HAS_RELOC | HAS_LOCALS | DYNAMIC |
  1010.    WP_TEXT | D_PAGED),
  1011.   (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1012.    | SEC_ALLOC | SEC_LOAD | SEC_RELOC),    /* section flags */
  1013.   0,                /* leading underscore */
  1014.   ' ',                /* ar_pad_char */
  1015.   16,                /* ar_max_namelen */
  1016.   1,                /* minimum alignment */
  1017.   _do_getb64, _do_getb_signed_64, _do_putb64,
  1018.     _do_getb32, _do_getb_signed_32,   _do_putb32,
  1019.     _do_getb16, _do_getb_signed_16, _do_putb16,    /* data */
  1020.   _do_getb64, _do_getb_signed_64, _do_putb64,
  1021.     _do_getb32, _do_getb_signed_32,   _do_putb32,
  1022.     _do_getb16, _do_getb_signed_16, _do_putb16,    /* hdrs */
  1023.  
  1024.   {
  1025.     _bfd_dummy_target,
  1026.     tekhex_object_p,        /* bfd_check_format */
  1027.     (struct bfd_target * (*)()) bfd_nullvoidptr,
  1028.     (struct bfd_target * (*)()) bfd_nullvoidptr,
  1029.   },
  1030.   {
  1031.     bfd_false,
  1032.     tekhex_mkobject,
  1033.     _bfd_generic_mkarchive,
  1034.     bfd_false,
  1035.   },
  1036.   {                /* bfd_write_contents */
  1037.     bfd_false,
  1038.     tekhex_write_object_contents,
  1039.     _bfd_write_archive_contents,
  1040.     bfd_false,
  1041.   },
  1042.   JUMP_TABLE (tekhex),
  1043.   (PTR) 0
  1044. };
  1045.