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