1 /* BFD backend for s-record objects.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
5 This file is part of BFD, the Binary File Descriptor library.
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 2 of the License, or
10 (at your option) any later version.
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.
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., 675 Mass Ave, Cambridge, MA 02139, USA. */
27 S-records cannot hold anything but addresses and data, so
28 that's all that we implement.
30 The only interesting thing is that s-records may come out of
31 order and there is no header, so an initial scan is required
32 to discover the minimum and maximum addresses used to create
33 the vma and size of the only section we create. We
34 arbitrarily call this section ".text".
36 When bfd_get_section_contents is called the file is read
37 again, and this time the data is placed into a bfd_alloc'd
40 Any number of sections may be created for output, we save them
41 up and output them when it's time to close the bfd.
43 An s record looks like:
46 S<length><type><address><data><checksum>
51 is the number of bytes following upto the checksum. Note that
52 this is not the number of chars following, since it takes two
53 chars to represent a byte.
57 1) two byte address data record
58 2) three byte address data record
59 3) four byte address data record
60 7) four byte address termination record
61 8) three byte address termination record
62 9) two byte address termination record
65 is the start address of the data following, or in the case of
66 a termination record, the start address of the image
70 is the sum of all the raw byte data in the record, from the length
71 upwards, modulo 256 and subtracted from 255.
79 static char digs[] = "0123456789ABCDEF";
81 /* Horrible ascii dependent macros for converting between hex and
84 #define CHARS_IN_SET 256
85 static char hex_value[CHARS_IN_SET];
87 #define NIBBLE(x) hex_value[x]
88 #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
89 #define TOHEX(d,x, ch) \
90 d[1] = digs[(x) & 0xf]; \
91 d[0] = digs[((x)>>4)&0xf]; ch += (x & 0xff);
92 #define ISHEX(x) (hex_value[x] != NOT_HEX)
100 static boolean inited = false;
107 for (i = 0; i < CHARS_IN_SET; i++)
109 hex_value[i] = NOT_HEX;
112 for (i = 0; i < 10; i++)
114 hex_value[i + '0'] = i;
117 for (i = 0; i < 6; i++)
119 hex_value[i + 'a'] = i+10;
120 hex_value[i + 'A'] = i+10;
126 /* The maximum number of bytes on a line is FF */
127 #define MAXCHUNK 0xff
128 /* The number of bytes we fit onto a line on output */
131 /* We cannot output our srecords as we see them, we have to glue them
132 together, this is done in this structure : */
134 struct srec_data_list_struct
139 struct srec_data_list_struct *next;
142 typedef struct srec_data_list_struct srec_data_list_type;
145 typedef struct srec_data_struct
147 srec_data_list_type *head;
154 #define enda(x) (x->vma + x->size)
156 called once per input s-record, used to work out vma and size of data.
159 static bfd_vma low,high;
162 DEFUN(size_srec,(abfd, section, address, raw, length),
164 asection *section AND
171 if (address + length > high)
172 high = address + length -1;
177 called once per input s-record, copies data from input into bfd_alloc'd area
181 DEFUN(fillup,(abfd, section, address, raw, length),
183 asection *section AND
190 (bfd_byte *)(section->used_by_bfd) +
191 address - section->vma;
192 /* length -1 because we don't read in the checksum */
193 for (i = 0; i < length -1 ; i++) {
200 /* Pass over an s-record file, calling one of the above functions on each
204 DEFUN(pass_over,(abfd, func, section),
209 unsigned int bytes_on_line;
212 /* To the front of the file */
213 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
216 char buffer[MAXCHUNK];
222 eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1);
223 while (*src!= 'S' && !eof) {
224 eof = (boolean)(bfd_read(src, 1, 1, abfd) != 1);
229 /* Fetch the type and the length */
230 bfd_read(src, 1, 3, abfd);
234 if (!ISHEX (src[0]) || !ISHEX (src[1]))
237 bytes_on_line = HEX(src);
239 if (bytes_on_line > MAXCHUNK/2)
243 bfd_read(src, 1 , bytes_on_line * 2, abfd);
248 /* Prologue - ignore */
256 address = HEX(src) | (address<<8) ;
260 address = HEX(src) | (address<<8) ;
262 address = HEX(src) | (address<<8) ;
265 func(abfd,section, address, src, bytes_on_line);
277 DEFUN(srec_object_p, (abfd),
284 bfd_seek(abfd, (file_ptr)0, SEEK_SET);
285 bfd_read(b, 1, 4, abfd);
287 if (b[0] != 'S' || !ISHEX(b[1]) || !ISHEX(b[2]) || !ISHEX(b[3]))
288 return (bfd_target*) NULL;
290 /* We create one section called .text for all the contents,
291 and allocate enough room for the entire file. */
293 section = bfd_make_section(abfd, ".text");
294 section->_raw_size = 0;
295 section->vma = 0xffffffff;
298 pass_over(abfd, size_srec, section);
299 section->_raw_size = high - low;
301 section->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
308 DEFUN(srec_get_section_contents,(abfd, section, location, offset, count),
310 asection *section AND
315 if (section->used_by_bfd == (PTR)NULL)
317 section->used_by_bfd = (PTR)bfd_alloc (abfd, section->_raw_size);
318 pass_over(abfd, fillup, section);
320 (void) memcpy((PTR)location,
321 (PTR)((char *)(section->used_by_bfd) + offset),
329 DEFUN(srec_set_arch_mach,(abfd, arch, machine),
331 enum bfd_architecture arch AND
332 unsigned long machine)
334 return bfd_default_set_arch_mach(abfd, arch, machine);
338 /* we have to save up all the Srecords for a splurge before output,
342 DEFUN(srec_set_section_contents,(abfd, section, location, offset, bytes_to_do),
347 bfd_size_type bytes_to_do)
349 tdata_type *tdata = abfd->tdata.srec_data;
350 srec_data_list_type *entry = (srec_data_list_type *)
351 bfd_alloc(abfd, sizeof(srec_data_list_type));
352 unsigned char *data = (unsigned char *) bfd_alloc(abfd, bytes_to_do);
353 memcpy(data, location, bytes_to_do);
355 if ((section->vma + offset + bytes_to_do) <= 0xffff)
359 else if ((section->vma + offset + bytes_to_do) <= 0xffffff
370 entry->where = section->vma + offset;
371 entry->size = bytes_to_do;
372 entry->next = tdata->head;
377 /* Write a record of type, of the supplied number of bytes. The
378 supplied bytes and length don't have a checksum. That's worked out
382 void DEFUN(srec_write_record,(abfd, type, address, data, end),
386 CONST unsigned char *data AND
387 CONST unsigned char *end)
390 char buffer[MAXCHUNK];
392 unsigned int check_sum = 0;
393 unsigned CONST char *src = data;
402 dst+=2; /* leave room for dst*/
408 TOHEX(dst, (address >> 24), check_sum);
412 TOHEX(dst, (address >> 16), check_sum);
417 TOHEX(dst, (address >> 8), check_sum);
419 TOHEX(dst, (address), check_sum);
424 for (src = data; src < end; src++)
426 TOHEX(dst, *src, check_sum);
431 /* Fill in the length */
432 TOHEX(length, (dst - length)/2, check_sum);
434 check_sum = 255 - check_sum;
435 TOHEX(dst, check_sum, check_sum);
439 bfd_write((PTR)buffer, 1, dst - buffer , abfd);
445 DEFUN(srec_write_header,(abfd),
448 unsigned char buffer[MAXCHUNK];
449 unsigned char *dst = buffer;
452 /* I'll put an arbitary 40 char limit on header size */
453 for (i = 0; i < 40 && abfd->filename[i]; i++)
455 *dst++ = abfd->filename[i];
457 srec_write_record(abfd,0, 0, buffer, dst);
461 DEFUN(srec_write_section,(abfd, tdata, list),
463 tdata_type *tdata AND
464 srec_data_list_type *list)
466 unsigned int bytes_written = 0;
467 unsigned char *location = list->data;
469 while (bytes_written < list->size)
471 char buffer[MAXCHUNK];
475 unsigned int bytes_this_chunk = list->size - bytes_written;
477 if (bytes_this_chunk > CHUNK)
479 bytes_this_chunk = CHUNK;
482 address = list->where + bytes_written;
484 srec_write_record(abfd,
488 location + bytes_this_chunk);
490 bytes_written += bytes_this_chunk;
491 location += bytes_this_chunk;
497 DEFUN(srec_write_terminator,(abfd, tdata),
501 unsigned char buffer[2];
503 srec_write_record(abfd, 10 - tdata->type,
504 abfd->start_address, buffer, buffer);
509 DEFUN(srec_mkobject, (abfd),
512 tdata_type *tdata = (tdata_type *)bfd_alloc(abfd, sizeof(tdata_type));
513 abfd->tdata.srec_data = tdata;
515 tdata->head = (srec_data_list_type *)NULL;
522 DEFUN(srec_write_object_contents,(abfd),
526 tdata_type *tdata = abfd->tdata.srec_data;
527 srec_data_list_type *list;
533 srec_write_header(abfd);
535 /* Now wander though all the sections provided and output them */
538 while (list != (srec_data_list_type*)NULL)
540 srec_write_section(abfd, tdata, list);
543 srec_write_terminator(abfd, tdata);
548 DEFUN(srec_sizeof_headers,(abfd, exec),
556 DEFUN(srec_make_empty_symbol, (abfd),
559 asymbol *new= (asymbol *)bfd_zalloc (abfd, sizeof (asymbol));
564 #define srec_new_section_hook (FOO(boolean, (*), (bfd *, asection *)))bfd_true
565 #define srec_get_symtab_upper_bound (PROTO(unsigned int, (*),(bfd *)))bfd_false
566 #define srec_get_symtab (FOO(unsigned int, (*), (bfd *, asymbol **)))bfd_0
567 #define srec_get_reloc_upper_bound (FOO(unsigned int, (*),(bfd*, asection *)))bfd_false
568 #define srec_canonicalize_reloc (FOO(unsigned int, (*),(bfd*,asection *, arelent **, asymbol **))) bfd_0
570 #define srec_print_symbol (FOO(void,(*),(bfd *, PTR, asymbol *, bfd_print_symbol_type))) bfd_void
572 #define srec_openr_next_archived_file (FOO(bfd *, (*), (bfd*,bfd*))) bfd_nullvoidptr
573 #define srec_find_nearest_line (FOO(boolean, (*),(bfd*,asection*,asymbol**,bfd_vma, CONST char**, CONST char**, unsigned int *))) bfd_false
574 #define srec_generic_stat_arch_elt (FOO(int, (*), (bfd *,struct stat *))) bfd_0
577 #define srec_core_file_failing_command (char *(*)())(bfd_nullvoidptr)
578 #define srec_core_file_failing_signal (int (*)())bfd_0
579 #define srec_core_file_matches_executable_p (FOO(boolean, (*),(bfd*, bfd*)))bfd_false
580 #define srec_slurp_armap bfd_true
581 #define srec_slurp_extended_name_table bfd_true
582 #define srec_truncate_arname (void (*)())bfd_nullvoidptr
583 #define srec_write_armap (FOO( boolean, (*),(bfd *, unsigned int, struct orl *, unsigned int, int))) bfd_nullvoidptr
584 #define srec_get_lineno (struct lineno_cache_entry *(*)())bfd_nullvoidptr
585 #define srec_close_and_cleanup bfd_generic_close_and_cleanup
586 #define srec_bfd_debug_info_start bfd_void
587 #define srec_bfd_debug_info_end bfd_void
588 #define srec_bfd_debug_info_accumulate (FOO(void, (*), (bfd *, asection *))) bfd_void
589 #define srec_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
591 bfd_target srec_vec =
594 bfd_target_srec_flavour,
595 true, /* target byte order */
596 true, /* target headers byte order */
597 (HAS_RELOC | EXEC_P | /* object flags */
598 HAS_LINENO | HAS_DEBUG |
599 HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
600 (SEC_CODE|SEC_DATA|SEC_ROM|SEC_HAS_CONTENTS
601 |SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
602 ' ', /* ar_pad_char */
603 16, /* ar_max_namelen */
604 1, /* minimum alignment */
605 _do_getb64, _do_putb64, _do_getb32,
606 _do_putb32, _do_getb16, _do_putb16, /* data */
607 _do_getb64, _do_putb64, _do_getb32,
608 _do_putb32, _do_getb16, _do_putb16, /* hdrs */
612 srec_object_p, /* bfd_check_format */
613 (struct bfd_target *(*)()) bfd_nullvoidptr,
614 (struct bfd_target *(*)()) bfd_nullvoidptr,
619 _bfd_generic_mkarchive,
622 { /* bfd_write_contents */
624 srec_write_object_contents,
625 _bfd_write_archive_contents,