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