]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/tekhex.c
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / bfd / tekhex.c
CommitLineData
d3e667e8 1/* BFD backend for Extended Tektronix Hex Format objects.
ae115e51 2 Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
d3e667e8
JG
3
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
ae115e51 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
d3e667e8
JG
21
22/*
23SUBSECTION
24 Tektronix Hex Format handling
25
26DESCRIPTION
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
9783e04a 35 read in the file.
d3e667e8
JG
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
9783e04a 45 create.
d3e667e8
JG
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:
54EXAMPLE
55 %<block length><type><checksum><stuff><cr>
56
57DESCRIPTION
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
68The data can come out of order, and may be discontigous. This is a
69serial 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"
ae115e51 75#include "libiberty.h"
d3e667e8 76
9783e04a
DM
77typedef struct
78 {
79 bfd_vma low;
80 bfd_vma high;
81 } addr_range_type;
d3e667e8 82
9783e04a
DM
83typedef struct tekhex_symbol_struct
84 {
d3e667e8 85
9783e04a
DM
86 asymbol symbol;
87 struct tekhex_symbol_struct *prev;
d3e667e8 88
9783e04a 89 } tekhex_symbol_type;
d3e667e8 90
ae115e51 91static const char digs[] = "0123456789ABCDEF";
d3e667e8
JG
92
93static char sum_block[256];
9783e04a 94
d3e667e8 95#define NOT_HEX 20
ae115e51 96#define NIBBLE(x) hex_value(x)
d3e667e8
JG
97#define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
98#define TOHEX(d,x) \
99(d)[1] = digs[(x) & 0xf]; \
100(d)[0] = digs[((x)>>4)&0xf];
ae115e51 101#define ISHEX(x) hex_p(x)
d3e667e8 102
ea3f0585
FF
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 *, char, char *, char *));
110static void writesym PARAMS ((char **, CONST char *));
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 **));
124static void pass_over PARAMS ((bfd *, void (*)(bfd*, char, char *)));
125static void first_phase PARAMS ((bfd *, char, 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
d3e667e8
JG
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
9783e04a 175Turns into
d3e667e8
JG
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
9783e04a 186
d3e667e8 187SECTION 2 [T_SEGMENT] : size ffffffff vma 00000000 align 2**0
9783e04a 188
d3e667e8
JG
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
217
218RELOCATION RECORDS FOR [D00000000]: (none)
219
220RELOCATION RECORDS FOR [D00008000]: (none)
221
222RELOCATION RECORDS FOR [T_SEGMENT]: (none)
223
224Disassembly of section D00000000:
225...
22600008000 ($+)7ff0 linkw fp,#-4
22700008004 ($+)7ff4 nop
22800008006 ($+)7ff6 movel #99,d0
22900008008 ($+)7ff8 cmpl fp@(-4),d0
2300000800c ($+)7ffc blts 00008014 ($+)8004
2310000800e ($+)7ffe addql #1,fp@(-4)
23200008012 ($+)8002 bras 00008006 ($+)7ff6
23300008014 ($+)8004 unlk fp
23400008016 ($+)8006 rts
235...
236
237*/
238
239static void
9783e04a 240tekhex_init ()
d3e667e8
JG
241{
242 unsigned int i;
243 static boolean inited = false;
9783e04a
DM
244 int val;
245
246 if (inited == false)
d3e667e8 247 {
d3e667e8 248 inited = true;
ae115e51 249 hex_init ();
d3e667e8 250 val = 0;
9783e04a
DM
251 for (i = 0; i < 10; i++)
252 {
253 sum_block[i + '0'] = val++;
254 }
255 for (i = 'A'; i <= 'Z'; i++)
256 {
257 sum_block[i] = val++;
258 }
d3e667e8
JG
259 sum_block['$'] = val++;
260 sum_block['%'] = val++;
261 sum_block['.'] = val++;
262 sum_block['_'] = val++;
9783e04a
DM
263 for (i = 'a'; i <= 'z'; i++)
264 {
265 sum_block[i] = val++;
266 }
267 }
d3e667e8
JG
268}
269
d3e667e8 270/* The maximum number of bytes on a line is FF */
9783e04a 271#define MAXCHUNK 0xff
d3e667e8
JG
272/* The number of bytes we fit onto a line on output */
273#define CHUNK 21
274
275/* We cannot output our tekhexords as we see them, we have to glue them
276 together, this is done in this structure : */
277
278struct tekhex_data_list_struct
279{
9783e04a
DM
280 unsigned char *data;
281 bfd_vma where;
282 bfd_size_type size;
283 struct tekhex_data_list_struct *next;
d3e667e8 284
9783e04a
DM
285};
286typedef struct tekhex_data_list_struct tekhex_data_list_type;
d3e667e8
JG
287
288#define CHUNK_MASK 0x1fff
289
9783e04a
DM
290struct data_struct
291 {
292 char chunk_data[CHUNK_MASK + 1];
293 char chunk_init[CHUNK_MASK + 1];
294 bfd_vma vma;
295 struct data_struct *next;
296 };
d3e667e8
JG
297
298typedef struct tekhex_data_struct
299{
9783e04a
DM
300 tekhex_data_list_type *head;
301 unsigned int type;
302 struct tekhex_symbol_struct *symbols;
303 struct data_struct *data;
d3e667e8
JG
304} tdata_type;
305
d3e667e8
JG
306#define enda(x) (x->vma + x->size)
307
9783e04a
DM
308static bfd_vma
309getvalue (srcp)
310 char **srcp;
d3e667e8
JG
311{
312 char *src = *srcp;
313 bfd_vma value = 0;
ae115e51 314 unsigned int len = hex_value(*src++);
9783e04a
DM
315
316 if (len == 0)
317 len = 16;
318 while (len--)
319 {
ae115e51 320 value = value << 4 | hex_value(*src++);
9783e04a 321 }
d3e667e8
JG
322 *srcp = src;
323 return value;
324}
325
9783e04a
DM
326static unsigned int
327getsym (dstp, srcp)
328 char *dstp;
329 char **srcp;
d3e667e8
JG
330{
331 char *src = *srcp;
332 unsigned int i;
ae115e51 333 unsigned int len = hex_value(*src++);
9783e04a
DM
334
335 if (len == 0)
336 len = 16;
d3e667e8
JG
337 for (i = 0; i < len; i++)
338 dstp[i] = src[i];
339 dstp[i] = 0;
340 *srcp = src + i;
341 return len;
342}
343
ea3f0585 344static struct data_struct *
9783e04a
DM
345find_chunk (abfd, vma)
346 bfd *abfd;
347 bfd_vma vma;
d3e667e8
JG
348{
349 struct data_struct *d = abfd->tdata.tekhex_data->data;
9783e04a 350
d3e667e8
JG
351 vma &= ~CHUNK_MASK;
352 while (d && (d->vma) != vma)
353 {
354 d = d->next;
355 }
9783e04a
DM
356 if (!d)
357 {
358 char *sname = bfd_alloc (abfd, 12);
9783e04a
DM
359
360 /* No chunk for this address, so make one up */
361 d = (struct data_struct *)
362 bfd_alloc (abfd, sizeof (struct data_struct));
363
364 if (!sname || !d)
a9713b91 365 return NULL;
9783e04a
DM
366
367 memset (d->chunk_init, 0, CHUNK_MASK + 1);
368 memset (d->chunk_data, 0, CHUNK_MASK + 1);
369 d->next = abfd->tdata.tekhex_data->data;
370 d->vma = vma;
371 abfd->tdata.tekhex_data->data = d;
372 }
d3e667e8
JG
373 return d;
374}
375
9783e04a
DM
376static void
377insert_byte (abfd, value, addr)
378 bfd *abfd;
379 int value;
380 bfd_vma addr;
d3e667e8
JG
381{
382 /* Find the chunk that this byte needs and put it in */
9783e04a 383 struct data_struct *d = find_chunk (abfd, addr);
d3e667e8 384
9783e04a
DM
385 d->chunk_data[addr & CHUNK_MASK] = value;
386 d->chunk_init[addr & CHUNK_MASK] = 1;
d3e667e8 387}
9783e04a
DM
388
389/* The first pass is to find the names of all the sections, and see
d3e667e8
JG
390 how big the data is */
391static void
9783e04a
DM
392first_phase (abfd, type, src)
393 bfd *abfd;
394 char type;
395 char *src;
d3e667e8 396{
ae115e51 397 asection *section = bfd_abs_section_ptr;
d3e667e8
JG
398 int len;
399 char sym[17]; /* A symbol can only be 16chars long */
9783e04a 400
d3e667e8
JG
401 switch (type)
402 {
403 case '6':
404 /* Data record - read it and store it */
405 {
9783e04a
DM
406 bfd_vma addr = getvalue (&src);
407
408 while (*src)
409 {
410 insert_byte (abfd, HEX (src), addr);
411 src += 2;
412 addr++;
413 }
d3e667e8 414 }
9783e04a 415
d3e667e8
JG
416 return;
417 case '3':
418 /* Symbol record, read the segment */
9783e04a
DM
419 len = getsym (sym, &src);
420 section = bfd_get_section_by_name (abfd, sym);
421 if (section == (asection *) NULL)
d3e667e8 422 {
9783e04a
DM
423 char *n = bfd_alloc (abfd, len + 1);
424
425 if (!n)
a9713b91 426 abort(); /* FIXME */
9783e04a
DM
427 memcpy (n, sym, len + 1);
428 section = bfd_make_section (abfd, n);
d3e667e8 429 }
9783e04a 430 while (*src)
d3e667e8 431 {
9783e04a 432 switch (*src)
d3e667e8 433 {
9783e04a 434 case '1': /* section range */
d3e667e8 435 src++;
9783e04a
DM
436 section->vma = getvalue (&src);
437 section->_raw_size = getvalue (&src) - section->vma;
438 section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
439 break;
ae115e51 440 case '0':
9783e04a
DM
441 case '2':
442 case '3':
443 case '4':
444 case '6':
445 case '7':
446 case '8':
447 /* Symbols, add to section */
448 {
449 tekhex_symbol_type *new =
450 (tekhex_symbol_type *) bfd_alloc (abfd,
451 sizeof (tekhex_symbol_type));
452 char type = (*src);
453
454 if (!new)
a9713b91 455 abort(); /* FIXME */
9783e04a
DM
456 new->symbol.the_bfd = abfd;
457 src++;
458 abfd->symcount++;
459 abfd->flags |= HAS_SYMS;
460 new->prev = abfd->tdata.tekhex_data->symbols;
461 abfd->tdata.tekhex_data->symbols = new;
462 len = getsym (sym, &src);
463 new->symbol.name = bfd_alloc (abfd, len + 1);
464 if (!new->symbol.name)
a9713b91 465 abort(); /* FIXME */
9783e04a
DM
466 memcpy ((char *) (new->symbol.name), sym, len + 1);
467 new->symbol.section = section;
468 if (type <= '4')
469 new->symbol.flags = (BSF_GLOBAL | BSF_EXPORT);
470 else
d3e667e8 471 new->symbol.flags = BSF_LOCAL;
9783e04a
DM
472 new->symbol.value = getvalue (&src) - section->vma;
473 }
d3e667e8 474 }
9783e04a 475 }
d3e667e8
JG
476 }
477}
478
d3e667e8
JG
479/* Pass over an tekhex, calling one of the above functions on each
480 record. */
481
482static void
ea3f0585 483pass_over (abfd, func)
9783e04a 484 bfd *abfd;
ea3f0585 485 void (*func) PARAMS ((bfd *, char, char *));
d3e667e8
JG
486{
487 unsigned int chars_on_line;
488 boolean eof = false;
489
490 /* To the front of the file */
4002f18a
ILT
491 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
492 abort ();
d3e667e8
JG
493 while (eof == false)
494 {
495 char buffer[MAXCHUNK];
496 char *src = buffer;
497 char type;
d3e667e8
JG
498
499 /* Find first '%' */
9783e04a
DM
500 eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1);
501 while (*src != '%' && !eof)
502 {
503 eof = (boolean) (bfd_read (src, 1, 1, abfd) != 1);
504 }
505 if (eof)
506 break;
d3e667e8
JG
507 src++;
508
d3e667e8 509 /* Fetch the type and the length and the checksum */
4002f18a
ILT
510 if (bfd_read (src, 1, 5, abfd) != 5)
511 abort (); /* FIXME */
d3e667e8
JG
512
513 type = src[2];
514
515 if (!ISHEX (src[0]) || !ISHEX (src[1]))
516 break;
517
9783e04a 518 chars_on_line = HEX (src) - 5; /* Already read five char */
d3e667e8 519
4002f18a
ILT
520 if (bfd_read (src, 1, chars_on_line, abfd) != chars_on_line)
521 abort (); /* FIXME */
d3e667e8
JG
522 src[chars_on_line] = 0; /* put a null at the end */
523
9783e04a 524 func (abfd, type, src);
d3e667e8
JG
525 }
526
527}
528
ea3f0585 529static long
9783e04a
DM
530tekhex_get_symtab (abfd, table)
531 bfd *abfd;
532 asymbol **table;
d3e667e8
JG
533{
534 tekhex_symbol_type *p = abfd->tdata.tekhex_data->symbols;
9783e04a
DM
535 unsigned int c = bfd_get_symcount (abfd);
536
537 table[c] = 0;
538 while (p)
539 {
540 table[--c] = &(p->symbol);
541 p = p->prev;
542 }
543
544 return bfd_get_symcount (abfd);
d3e667e8 545}
9783e04a 546
ea3f0585 547static long
9783e04a
DM
548tekhex_get_symtab_upper_bound (abfd)
549 bfd *abfd;
d3e667e8 550{
9783e04a 551 return (abfd->symcount + 1) * (sizeof (struct tekhex_asymbol_struct *));
d3e667e8
JG
552
553}
554
555static boolean
9783e04a
DM
556tekhex_mkobject (abfd)
557 bfd *abfd;
d3e667e8 558{
9783e04a
DM
559 tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
560
561 if (!tdata)
a9713b91 562 return false;
9783e04a
DM
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;
d3e667e8
JG
569}
570
9783e04a 571/*
d3e667e8
JG
572 Return true if the file looks like it's in TekHex format. Just look
573 for a percent sign and some hex digits */
d3e667e8 574
2f3508ad 575static const bfd_target *
9783e04a
DM
576tekhex_object_p (abfd)
577 bfd *abfd;
d3e667e8
JG
578{
579 char b[4];
d3e667e8 580
9783e04a
DM
581 tekhex_init ();
582
4002f18a
ILT
583 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
584 || bfd_read (b, 1, 4, abfd) != 4)
585 return NULL;
9783e04a
DM
586
587 if (b[0] != '%' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
2f3508ad 588 return (const bfd_target *) NULL;
d3e667e8 589
9783e04a 590 tekhex_mkobject (abfd);
d3e667e8 591
9783e04a 592 pass_over (abfd, first_phase);
d3e667e8
JG
593 return abfd->xvec;
594}
595
ae115e51 596static void
9783e04a
DM
597move_section_contents (abfd, section, locationp, offset, count, get)
598 bfd *abfd;
599 asection *section;
600 PTR locationp;
601 file_ptr offset;
602 bfd_size_type count;
603 boolean get;
d3e667e8
JG
604{
605 bfd_vma addr;
9783e04a 606 char *location = (char *) locationp;
d3e667e8 607 bfd_vma prev_number = 1; /* Nothing can have this as a high bit*/
9783e04a
DM
608 struct data_struct *d = (struct data_struct *) NULL;
609
610 for (addr = section->vma; count != 0; count--, addr++)
d3e667e8
JG
611 {
612
9783e04a 613 bfd_vma chunk_number = addr & ~CHUNK_MASK; /* Get high bits of address */
d3e667e8 614 bfd_vma low_bits = addr & CHUNK_MASK;
9783e04a
DM
615
616 if (chunk_number != prev_number)
d3e667e8
JG
617 {
618 /* Different chunk, so move pointer */
9783e04a 619 d = find_chunk (abfd, chunk_number);
d3e667e8
JG
620 }
621
9783e04a
DM
622 if (get)
623 {
624 if (d->chunk_init[low_bits])
625 {
626 *location = d->chunk_data[low_bits];
627 }
628 else
629 {
630 *location = 0;
631 }
632 }
633 else
634 {
635 d->chunk_data[low_bits] = *location;
636 d->chunk_init[low_bits] = (*location != 0);
637 }
d3e667e8 638
9783e04a 639 location++;
d3e667e8
JG
640
641 }
642
9783e04a 643}
ea3f0585 644
d3e667e8 645static boolean
9783e04a
DM
646tekhex_get_section_contents (abfd, section, locationp, offset, count)
647 bfd *abfd;
648 asection *section;
649 PTR locationp;
650 file_ptr offset;
651 bfd_size_type count;
d3e667e8
JG
652{
653 if (section->flags & (SEC_LOAD | SEC_ALLOC))
654 {
9783e04a 655 move_section_contents (abfd, section, locationp, offset, count, true);
d3e667e8
JG
656 return true;
657 }
9783e04a
DM
658 else
659 return false;
d3e667e8 660}
d3e667e8 661
ea3f0585 662static boolean
9783e04a
DM
663tekhex_set_arch_mach (abfd, arch, machine)
664 bfd *abfd;
665 enum bfd_architecture arch;
666 unsigned long machine;
d3e667e8 667{
9783e04a 668 return bfd_default_set_arch_mach (abfd, arch, machine);
d3e667e8
JG
669}
670
d3e667e8
JG
671/* we have to save up all the Tekhexords for a splurge before output,
672 */
673
674static boolean
9783e04a
DM
675tekhex_set_section_contents (abfd, section, locationp, offset, bytes_to_do)
676 bfd *abfd;
677 sec_ptr section;
678 PTR locationp;
679 file_ptr offset;
680 bfd_size_type bytes_to_do;
d3e667e8
JG
681{
682
9783e04a
DM
683 if (abfd->output_has_begun == false)
684 {
685 /* The first time around, allocate enough sections to hold all the chunks */
686 asection *s = abfd->sections;
687 bfd_vma vma;
688
689 for (s = abfd->sections; s; s = s->next)
690 {
691 if (s->flags & SEC_LOAD)
692 {
693 for (vma = s->vma & ~CHUNK_MASK;
694 vma < s->vma + s->_raw_size;
695 vma += CHUNK_MASK)
696 find_chunk (abfd, vma);
697 }
698 }
d3e667e8 699
9783e04a 700 }
d3e667e8
JG
701 if (section->flags & (SEC_LOAD | SEC_ALLOC))
702 {
9783e04a 703 move_section_contents (abfd, section, locationp, offset, bytes_to_do, false);
d3e667e8
JG
704 return true;
705 }
9783e04a
DM
706 else
707 return false;
d3e667e8
JG
708
709}
710
d3e667e8 711static void
9783e04a
DM
712writevalue (dst, value)
713 char **dst;
714 bfd_vma value;
d3e667e8
JG
715{
716 char *p = *dst;
9783e04a 717 int len;
d3e667e8 718 int shift;
9783e04a
DM
719
720 for (len = 8, shift = 28; shift; shift -= 4, len--)
d3e667e8 721 {
9783e04a
DM
722 if ((value >> shift) & 0xf)
723 {
724 *p++ = len + '0';
725 while (len)
726 {
727 *p++ = digs[(value >> shift) & 0xf];
728 shift -= 4;
729 len--;
730 }
731 *dst = p;
732 return;
d3e667e8 733
9783e04a 734 }
d3e667e8
JG
735 }
736 *p++ = '1';
737 *p++ = '0';
738 *dst = p;
739}
740
741static void
9783e04a
DM
742writesym (dst, sym)
743 char **dst;
744 CONST char *sym;
d3e667e8
JG
745{
746 char *p = *dst;
9783e04a 747 int len = (sym ? strlen (sym) : 0);
d3e667e8 748
9783e04a
DM
749 if (len >= 16)
750 {
751 *p++ = '0';
752 len = 16;
d3e667e8 753 }
d3e667e8 754
9783e04a
DM
755 else
756 {
757 if (len == 0)
758 {
759 *p++ = '1';
760 sym = "$";
761 len = 1;
762 }
763 else
764 {
765 *p++ = digs[len];
766 }
767 }
768
769 while (len--)
770 {
771 *p++ = *sym++;
772 }
d3e667e8
JG
773 *dst = p;
774}
775
9783e04a
DM
776static void
777out (abfd, type, start, end)
778 bfd *abfd;
779 char type;
780 char *start;
781 char *end;
d3e667e8
JG
782{
783 int sum = 0;
784 char *s;
785 char front[6];
ae115e51 786 bfd_size_type wrlen;
9783e04a 787
d3e667e8 788 front[0] = '%';
9783e04a 789 TOHEX (front + 1, end - start + 5);
d3e667e8
JG
790 front[3] = type;
791
9783e04a
DM
792 for (s = start; s < end; s++)
793 {
ae115e51 794 sum += sum_block[(unsigned char) *s];
9783e04a 795 }
d3e667e8 796
ae115e51
ILT
797 sum += sum_block[(unsigned char) front[1]]; /* length */
798 sum += sum_block[(unsigned char) front[2]];
799 sum += sum_block[(unsigned char) front[3]]; /* type */
9783e04a 800 TOHEX (front + 4, sum);
4002f18a
ILT
801 if (bfd_write (front, 1, 6, abfd) != 6)
802 abort ();
d3e667e8 803 end[0] = '\n';
ae115e51
ILT
804 wrlen = end - start + 1;
805 if (bfd_write (start, 1, wrlen, abfd) != wrlen)
4002f18a 806 abort ();
d3e667e8 807}
9783e04a 808
d3e667e8 809static boolean
9783e04a
DM
810tekhex_write_object_contents (abfd)
811 bfd *abfd;
d3e667e8
JG
812{
813 int bytes_written;
814 char buffer[100];
815 asymbol **p;
d3e667e8
JG
816 asection *s;
817 struct data_struct *d;
9783e04a 818
d3e667e8
JG
819 bytes_written = 0;
820
821 /* And the raw data */
822 for (d = abfd->tdata.tekhex_data->data;
9783e04a 823 d != (struct data_struct *) NULL;
d3e667e8
JG
824 d = d->next)
825 {
826 int low;
827
9783e04a 828 CONST int span = 32;
d3e667e8 829 int addr;
9783e04a 830
d3e667e8
JG
831 /* Write it in blocks of 32 bytes */
832
9783e04a 833 for (addr = 0; addr < CHUNK_MASK + 1; addr += span)
d3e667e8
JG
834 {
835 int need = 0;
9783e04a 836
d3e667e8 837 /* Check to see if necessary */
9783e04a
DM
838 for (low = 0; !need && low < span; low++)
839 {
840 if (d->chunk_init[addr + low])
841 need = 1;
842 }
843 if (need)
844 {
845 char *dst = buffer;
846
847 writevalue (&dst, addr + d->vma);
848 for (low = 0; low < span; low++)
849 {
850 TOHEX (dst, d->chunk_data[addr + low]);
851 dst += 2;
852 }
853 out (abfd, '6', buffer, dst);
d3e667e8 854 }
d3e667e8
JG
855 }
856 }
857 /* write all the section headers for the sections */
9783e04a 858 for (s = abfd->sections; s != (asection *) NULL; s = s->next)
d3e667e8
JG
859 {
860 char *dst = buffer;
9783e04a
DM
861
862 writesym (&dst, s->name);
d3e667e8 863 *dst++ = '1';
9783e04a
DM
864 writevalue (&dst, s->vma);
865 writevalue (&dst, s->vma + s->_raw_size);
866 out (abfd, '3', buffer, dst);
d3e667e8
JG
867 }
868
869 /* And the symbols */
9783e04a
DM
870 for (p = abfd->outsymbols; *p; p++)
871 {
872 int section_code = bfd_decode_symclass (*p);
873
874 if (section_code != '?')
875 { /* do not include debug symbols */
876 asymbol *s = *p;
877 char *dst = buffer;
878
879 writesym (&dst, s->section->name);
880
881 switch (section_code)
882 {
883 case 'A':
884 *dst++ = '2';
885 break;
886 case 'a':
887 *dst++ = '6';
888 break;
889 case 'D':
890 case 'B':
891 case 'O':
892 *dst++ = '4';
893 break;
894 case 'd':
895 case 'b':
896 case 'o':
897 *dst++ = '8';
898 break;
899 case 'T':
900 *dst++ = '3';
901 break;
902 case 't':
903 *dst++ = '7';
904 break;
905 case 'C':
906 case 'U':
326e32d7 907 bfd_set_error (bfd_error_wrong_format);
9783e04a
DM
908 return false;
909 }
d3e667e8 910
9783e04a
DM
911 writesym (&dst, s->name);
912 writevalue (&dst, s->value + s->section->vma);
913 out (abfd, '3', buffer, dst);
914 }
d3e667e8 915 }
d3e667e8 916
d3e667e8 917 /* And the terminator */
ae115e51 918 if (bfd_write ("%0781010\n", 1, 9, abfd) != 9)
4002f18a 919 abort ();
9783e04a 920 return true;
d3e667e8 921}
d3e667e8 922
9783e04a 923static int
ea3f0585 924tekhex_sizeof_headers (abfd, exec)
9783e04a
DM
925 bfd *abfd;
926 boolean exec;
927
d3e667e8 928{
9783e04a 929 return 0;
d3e667e8
JG
930}
931
932static asymbol *
9783e04a
DM
933tekhex_make_empty_symbol (abfd)
934 bfd *abfd;
d3e667e8 935{
9783e04a
DM
936 tekhex_symbol_type *new =
937 (tekhex_symbol_type *) bfd_zalloc (abfd, sizeof (struct tekhex_symbol_struct));
938
939 if (!new)
a9713b91 940 return NULL;
d3e667e8 941 new->symbol.the_bfd = abfd;
9783e04a 942 new->prev = (struct tekhex_symbol_struct *) NULL;
d3e667e8
JG
943 return &(new->symbol);
944}
945
946static void
9783e04a
DM
947tekhex_get_symbol_info (ignore_abfd, symbol, ret)
948 bfd *ignore_abfd;
949 asymbol *symbol;
950 symbol_info *ret;
d3e667e8 951{
9783e04a
DM
952 bfd_symbol_info (symbol, ret);
953}
d3e667e8 954
9783e04a
DM
955static void
956tekhex_print_symbol (ignore_abfd, filep, symbol, how)
957 bfd *ignore_abfd;
958 PTR filep;
959 asymbol *symbol;
960 bfd_print_symbol_type how;
961{
962 FILE *file = (FILE *) filep;
963
964 switch (how)
d3e667e8 965 {
9783e04a
DM
966 case bfd_print_symbol_name:
967 fprintf (file, "%s", symbol->name);
968 break;
969 case bfd_print_symbol_more:
970 break;
d3e667e8 971
9783e04a
DM
972 case bfd_print_symbol_all:
973 {
974 CONST char *section_name = symbol->section->name;
975
976 bfd_print_symbol_vandf ((PTR) file, symbol);
977
978 fprintf (file, " %-5s %s",
979 section_name,
980 symbol->name);
981 }
982 }
d3e667e8
JG
983}
984
6812b607
ILT
985#define tekhex_close_and_cleanup _bfd_generic_close_and_cleanup
986#define tekhex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
987#define tekhex_new_section_hook _bfd_generic_new_section_hook
988
989#define tekhex_bfd_is_local_label bfd_generic_is_local_label
990#define tekhex_get_lineno _bfd_nosymbols_get_lineno
991#define tekhex_find_nearest_line _bfd_nosymbols_find_nearest_line
992#define tekhex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
ae115e51
ILT
993#define tekhex_read_minisymbols _bfd_generic_read_minisymbols
994#define tekhex_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
6812b607
ILT
995
996#define tekhex_bfd_get_relocated_section_contents \
997 bfd_generic_get_relocated_section_contents
d3e667e8 998#define tekhex_bfd_relax_section bfd_generic_relax_section
9783e04a
DM
999#define tekhex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1000#define tekhex_bfd_link_add_symbols _bfd_generic_link_add_symbols
1001#define tekhex_bfd_final_link _bfd_generic_final_link
ae115e51 1002#define tekhex_bfd_link_split_section _bfd_generic_link_split_section
9783e04a 1003
a9713b91
ILT
1004#define tekhex_get_section_contents_in_window \
1005 _bfd_generic_get_section_contents_in_window
1006
2f3508ad 1007const bfd_target tekhex_vec =
d3e667e8 1008{
9783e04a
DM
1009 "tekhex", /* name */
1010 bfd_target_tekhex_flavour,
ea3f0585
FF
1011 BFD_ENDIAN_UNKNOWN, /* target byte order */
1012 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
9783e04a
DM
1013 (EXEC_P | /* object flags */
1014 HAS_SYMS | HAS_LINENO | HAS_DEBUG | HAS_RELOC | HAS_LOCALS |
1015 WP_TEXT | D_PAGED),
1016 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1017 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1018 0, /* leading underscore */
1019 ' ', /* ar_pad_char */
1020 16, /* ar_max_namelen */
9783e04a
DM
1021 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1022 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1023 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1024 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1025 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1026 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
d3e667e8
JG
1027
1028 {
9783e04a
DM
1029 _bfd_dummy_target,
1030 tekhex_object_p, /* bfd_check_format */
2f3508ad
ILT
1031 _bfd_dummy_target,
1032 _bfd_dummy_target,
d3e667e8
JG
1033 },
1034 {
9783e04a
DM
1035 bfd_false,
1036 tekhex_mkobject,
1037 _bfd_generic_mkarchive,
1038 bfd_false,
d3e667e8
JG
1039 },
1040 { /* bfd_write_contents */
9783e04a
DM
1041 bfd_false,
1042 tekhex_write_object_contents,
1043 _bfd_write_archive_contents,
1044 bfd_false,
d3e667e8 1045 },
6812b607
ILT
1046
1047 BFD_JUMP_TABLE_GENERIC (tekhex),
1048 BFD_JUMP_TABLE_COPY (_bfd_generic),
1049 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1050 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1051 BFD_JUMP_TABLE_SYMBOLS (tekhex),
1052 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
1053 BFD_JUMP_TABLE_WRITE (tekhex),
1054 BFD_JUMP_TABLE_LINK (tekhex),
dfc1c006 1055 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
6812b607 1056
9783e04a
DM
1057 (PTR) 0
1058};