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