* cofflink.c (coff_link_add_symbols): Look for special MSVC string
[binutils.git] / bfd / srec.c
blobaefdc64167c4f34495a18d8def3f11f1d0ab057b
1 /* BFD back-end for s-record objects.
2 Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 98, 1999
3 Free Software Foundation, Inc.
4 Written by Steve Chamberlain of Cygnus Support <sac@cygnus.com>.
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 SUBSECTION
24 S-Record handling
26 DESCRIPTION
28 Ordinary S-Records cannot hold anything but addresses and
29 data, so that's all that we implement.
31 The only interesting thing is that S-Records may come out of
32 order and there is no header, so an initial scan is required
33 to discover the minimum and maximum addresses used to create
34 the vma and size of the only section we create. We
35 arbitrarily call this section ".text".
37 When bfd_get_section_contents is called the file is read
38 again, and this time the data is placed into a bfd_alloc'd
39 area.
41 Any number of sections may be created for output, we save them
42 up and output them when it's time to close the bfd.
44 An s record looks like:
46 EXAMPLE
47 S<type><length><address><data><checksum>
49 DESCRIPTION
50 Where
51 o length
52 is the number of bytes following upto the checksum. Note that
53 this is not the number of chars following, since it takes two
54 chars to represent a byte.
55 o type
56 is one of:
57 0) header record
58 1) two byte address data record
59 2) three byte address data record
60 3) four byte address data record
61 7) four byte address termination record
62 8) three byte address termination record
63 9) two byte address termination record
65 o address
66 is the start address of the data following, or in the case of
67 a termination record, the start address of the image
68 o data
69 is the data.
70 o checksum
71 is the sum of all the raw byte data in the record, from the length
72 upwards, modulo 256 and subtracted from 255.
75 SUBSECTION
76 Symbol S-Record handling
78 DESCRIPTION
79 Some ICE equipment understands an addition to the standard
80 S-Record format; symbols and their addresses can be sent
81 before the data.
83 The format of this is:
84 ($$ <modulename>
85 (<space> <symbol> <address>)*)
88 so a short symbol table could look like:
90 EXAMPLE
91 $$ flash.x
92 $$ flash.c
93 _port6 $0
94 _delay $4
95 _start $14
96 _etext $8036
97 _edata $8036
98 _end $8036
101 DESCRIPTION
102 We allow symbols to be anywhere in the data stream - the module names
103 are always ignored.
107 #include "bfd.h"
108 #include "sysdep.h"
109 #include "libbfd.h"
110 #include "libiberty.h"
111 #include <ctype.h>
113 static void srec_get_symbol_info PARAMS ((bfd *, asymbol *, symbol_info *));
114 static void srec_print_symbol
115 PARAMS ((bfd *, PTR, asymbol *, bfd_print_symbol_type));
116 static void srec_init PARAMS ((void));
117 static boolean srec_mkobject PARAMS ((bfd *));
118 static int srec_get_byte PARAMS ((bfd *, boolean *));
119 static void srec_bad_byte PARAMS ((bfd *, unsigned int, int, boolean));
120 static boolean srec_scan PARAMS ((bfd *));
121 static const bfd_target *srec_object_p PARAMS ((bfd *));
122 static const bfd_target *symbolsrec_object_p PARAMS ((bfd *));
123 static boolean srec_read_section PARAMS ((bfd *, asection *, bfd_byte *));
125 static boolean srec_write_record PARAMS ((bfd *, int, bfd_vma,
126 const bfd_byte *,
127 const bfd_byte *));
128 static boolean srec_write_header PARAMS ((bfd *));
129 static boolean srec_write_symbols PARAMS ((bfd *));
130 static boolean srec_new_symbol PARAMS ((bfd *, const char *, bfd_vma));
131 static boolean srec_get_section_contents
132 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type));
133 static boolean srec_set_arch_mach
134 PARAMS ((bfd *, enum bfd_architecture, unsigned long));
135 static boolean srec_set_section_contents
136 PARAMS ((bfd *, sec_ptr, PTR, file_ptr, bfd_size_type));
137 static boolean internal_srec_write_object_contents PARAMS ((bfd *, int));
138 static boolean srec_write_object_contents PARAMS ((bfd *));
139 static boolean symbolsrec_write_object_contents PARAMS ((bfd *));
140 static int srec_sizeof_headers PARAMS ((bfd *, boolean));
141 static asymbol *srec_make_empty_symbol PARAMS ((bfd *));
142 static long srec_get_symtab_upper_bound PARAMS ((bfd *));
143 static long srec_get_symtab PARAMS ((bfd *, asymbol **));
145 /* Macros for converting between hex and binary. */
147 static CONST char digs[] = "0123456789ABCDEF";
149 #define NIBBLE(x) hex_value(x)
150 #define HEX(buffer) ((NIBBLE((buffer)[0])<<4) + NIBBLE((buffer)[1]))
151 #define TOHEX(d, x, ch) \
152 d[1] = digs[(x) & 0xf]; \
153 d[0] = digs[((x)>>4)&0xf]; \
154 ch += ((x) & 0xff);
155 #define ISHEX(x) hex_p(x)
157 /* Initialize by filling in the hex conversion array. */
159 static void
160 srec_init ()
162 static boolean inited = false;
164 if (inited == false)
166 inited = true;
167 hex_init ();
171 /* The maximum number of bytes on a line is FF */
172 #define MAXCHUNK 0xff
173 /* The number of bytes we fit onto a line on output */
174 #define CHUNK 21
176 /* When writing an S-record file, the S-records can not be output as
177 they are seen. This structure is used to hold them in memory. */
179 struct srec_data_list_struct
181 struct srec_data_list_struct *next;
182 bfd_byte *data;
183 bfd_vma where;
184 bfd_size_type size;
187 typedef struct srec_data_list_struct srec_data_list_type;
189 /* When scanning the S-record file, a linked list of srec_symbol
190 structures is built to represent the symbol table (if there is
191 one). */
193 struct srec_symbol
195 struct srec_symbol *next;
196 const char *name;
197 bfd_vma val;
200 /* The S-record tdata information. */
202 typedef struct srec_data_struct
204 srec_data_list_type *head;
205 srec_data_list_type *tail;
206 unsigned int type;
207 struct srec_symbol *symbols;
208 struct srec_symbol *symtail;
209 asymbol *csymbols;
211 tdata_type;
213 static boolean srec_write_section PARAMS ((bfd *, tdata_type *,
214 srec_data_list_type *));
215 static boolean srec_write_terminator PARAMS ((bfd *, tdata_type *));
217 /* Set up the S-record tdata information. */
219 static boolean
220 srec_mkobject (abfd)
221 bfd *abfd;
223 srec_init ();
225 if (abfd->tdata.srec_data == NULL)
227 tdata_type *tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
228 if (tdata == NULL)
229 return false;
230 abfd->tdata.srec_data = tdata;
231 tdata->type = 1;
232 tdata->head = NULL;
233 tdata->tail = NULL;
234 tdata->symbols = NULL;
235 tdata->symtail = NULL;
236 tdata->csymbols = NULL;
239 return true;
242 /* Read a byte from an S record file. Set *ERRORPTR if an error
243 occurred. Return EOF on error or end of file. */
245 static int
246 srec_get_byte (abfd, errorptr)
247 bfd *abfd;
248 boolean *errorptr;
250 bfd_byte c;
252 if (bfd_read (&c, 1, 1, abfd) != 1)
254 if (bfd_get_error () != bfd_error_file_truncated)
255 *errorptr = true;
256 return EOF;
259 return (int) (c & 0xff);
262 /* Report a problem in an S record file. FIXME: This probably should
263 not call fprintf, but we really do need some mechanism for printing
264 error messages. */
266 static void
267 srec_bad_byte (abfd, lineno, c, error)
268 bfd *abfd;
269 unsigned int lineno;
270 int c;
271 boolean error;
273 if (c == EOF)
275 if (! error)
276 bfd_set_error (bfd_error_file_truncated);
278 else
280 char buf[10];
282 if (! isprint (c))
283 sprintf (buf, "\\%03o", (unsigned int) c);
284 else
286 buf[0] = c;
287 buf[1] = '\0';
289 (*_bfd_error_handler)
290 (_("%s:%d: Unexpected character `%s' in S-record file\n"),
291 bfd_get_filename (abfd), lineno, buf);
292 bfd_set_error (bfd_error_bad_value);
296 /* Add a new symbol found in an S-record file. */
298 static boolean
299 srec_new_symbol (abfd, name, val)
300 bfd *abfd;
301 const char *name;
302 bfd_vma val;
304 struct srec_symbol *n;
306 n = (struct srec_symbol *) bfd_alloc (abfd, sizeof (struct srec_symbol));
307 if (n == NULL)
308 return false;
310 n->name = name;
311 n->val = val;
313 if (abfd->tdata.srec_data->symbols == NULL)
314 abfd->tdata.srec_data->symbols = n;
315 else
316 abfd->tdata.srec_data->symtail->next = n;
317 abfd->tdata.srec_data->symtail = n;
318 n->next = NULL;
320 ++abfd->symcount;
322 return true;
325 /* Read the S record file and turn it into sections. We create a new
326 section for each contiguous set of bytes. */
328 static boolean
329 srec_scan (abfd)
330 bfd *abfd;
332 int c;
333 unsigned int lineno = 1;
334 boolean error = false;
335 bfd_byte *buf = NULL;
336 size_t bufsize = 0;
337 asection *sec = NULL;
338 char *symbuf = NULL;
340 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
341 goto error_return;
343 while ((c = srec_get_byte (abfd, &error)) != EOF)
345 /* We only build sections from contiguous S-records, so if this
346 is not an S-record, then stop building a section. */
347 if (c != 'S' && c != '\r' && c != '\n')
348 sec = NULL;
350 switch (c)
352 default:
353 srec_bad_byte (abfd, lineno, c, error);
354 goto error_return;
356 case '\n':
357 ++lineno;
358 break;
360 case '\r':
361 break;
363 case '$':
364 /* Starting a module name, which we ignore. */
365 while ((c = srec_get_byte (abfd, &error)) != '\n'
366 && c != EOF)
368 if (c == EOF)
370 srec_bad_byte (abfd, lineno, c, error);
371 goto error_return;
374 ++lineno;
376 break;
378 case ' ':
381 unsigned int alc;
382 char *p, *symname;
383 bfd_vma symval;
385 /* Starting a symbol definition. */
386 while ((c = srec_get_byte (abfd, &error)) != EOF
387 && (c == ' ' || c == '\t'))
390 if (c == '\n' || c == '\r')
391 break;
393 if (c == EOF)
395 srec_bad_byte (abfd, lineno, c, error);
396 goto error_return;
399 alc = 10;
400 symbuf = (char *) bfd_malloc (alc + 1);
401 if (symbuf == NULL)
402 goto error_return;
404 p = symbuf;
406 *p++ = c;
407 while ((c = srec_get_byte (abfd, &error)) != EOF
408 && ! isspace (c))
410 if ((unsigned int) (p - symbuf) >= alc)
412 char *n;
414 alc *= 2;
415 n = (char *) bfd_realloc (symbuf, alc + 1);
416 if (n == NULL)
417 goto error_return;
418 p = n + (p - symbuf);
419 symbuf = n;
422 *p++ = c;
425 if (c == EOF)
427 srec_bad_byte (abfd, lineno, c, error);
428 goto error_return;
431 *p++ = '\0';
432 symname = bfd_alloc (abfd, p - symbuf);
433 if (symname == NULL)
434 goto error_return;
435 strcpy (symname, symbuf);
436 free (symbuf);
437 symbuf = NULL;
439 while ((c = srec_get_byte (abfd, &error)) != EOF
440 && (c == ' ' || c == '\t'))
442 if (c == EOF)
444 srec_bad_byte (abfd, lineno, c, error);
445 goto error_return;
448 /* Skip a dollar sign before the hex value. */
449 if (c == '$')
451 c = srec_get_byte (abfd, &error);
452 if (c == EOF)
454 srec_bad_byte (abfd, lineno, c, error);
455 goto error_return;
459 symval = 0;
460 while (ISHEX (c))
462 symval <<= 4;
463 symval += NIBBLE (c);
464 c = srec_get_byte (abfd, &error);
467 if (! srec_new_symbol (abfd, symname, symval))
468 goto error_return;
470 while (c == ' ' || c == '\t');
472 if (c == '\n')
473 ++lineno;
474 else if (c != '\r')
476 srec_bad_byte (abfd, lineno, c, error);
477 goto error_return;
480 break;
482 case 'S':
484 file_ptr pos;
485 char hdr[3];
486 unsigned int bytes;
487 bfd_vma address;
488 bfd_byte *data;
490 /* Starting an S-record. */
492 pos = bfd_tell (abfd) - 1;
494 if (bfd_read (hdr, 1, 3, abfd) != 3)
495 goto error_return;
497 if (! ISHEX (hdr[1]) || ! ISHEX (hdr[2]))
499 if (! ISHEX (hdr[1]))
500 c = hdr[1];
501 else
502 c = hdr[2];
503 srec_bad_byte (abfd, lineno, c, error);
504 goto error_return;
507 bytes = HEX (hdr + 1);
508 if (bytes * 2 > bufsize)
510 if (buf != NULL)
511 free (buf);
512 buf = (bfd_byte *) bfd_malloc (bytes * 2);
513 if (buf == NULL)
514 goto error_return;
515 bufsize = bytes * 2;
518 if (bfd_read (buf, 1, bytes * 2, abfd) != bytes * 2)
519 goto error_return;
521 /* Ignore the checksum byte. */
522 --bytes;
524 address = 0;
525 data = buf;
526 switch (hdr[0])
528 case '0':
529 case '5':
530 /* Prologue--ignore the file name, but stop building a
531 section at this point. */
532 sec = NULL;
533 break;
535 case '3':
536 address = HEX (data);
537 data += 2;
538 --bytes;
539 /* Fall through. */
540 case '2':
541 address = (address << 8) | HEX (data);
542 data += 2;
543 --bytes;
544 /* Fall through. */
545 case '1':
546 address = (address << 8) | HEX (data);
547 data += 2;
548 address = (address << 8) | HEX (data);
549 data += 2;
550 bytes -= 2;
552 if (sec != NULL
553 && sec->vma + sec->_raw_size == address)
555 /* This data goes at the end of the section we are
556 currently building. */
557 sec->_raw_size += bytes;
559 else
561 char secbuf[20];
562 char *secname;
564 sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
565 secname = (char *) bfd_alloc (abfd, strlen (secbuf) + 1);
566 strcpy (secname, secbuf);
567 sec = bfd_make_section (abfd, secname);
568 if (sec == NULL)
569 goto error_return;
570 sec->flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
571 sec->vma = address;
572 sec->lma = address;
573 sec->_raw_size = bytes;
574 sec->filepos = pos;
577 break;
579 case '7':
580 address = HEX (data);
581 data += 2;
582 /* Fall through. */
583 case '8':
584 address = (address << 8) | HEX (data);
585 data += 2;
586 /* Fall through. */
587 case '9':
588 address = (address << 8) | HEX (data);
589 data += 2;
590 address = (address << 8) | HEX (data);
591 data += 2;
593 /* This is a termination record. */
594 abfd->start_address = address;
596 if (buf != NULL)
597 free (buf);
599 return true;
602 break;
606 if (error)
607 goto error_return;
609 if (buf != NULL)
610 free (buf);
612 return true;
614 error_return:
615 if (symbuf != NULL)
616 free (symbuf);
617 if (buf != NULL)
618 free (buf);
619 return false;
622 /* Check whether an existing file is an S-record file. */
624 static const bfd_target *
625 srec_object_p (abfd)
626 bfd *abfd;
628 bfd_byte b[4];
630 srec_init ();
632 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
633 || bfd_read (b, 1, 4, abfd) != 4)
634 return NULL;
636 if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
638 bfd_set_error (bfd_error_wrong_format);
639 return NULL;
642 if (! srec_mkobject (abfd)
643 || ! srec_scan (abfd))
644 return NULL;
646 if (abfd->symcount > 0)
647 abfd->flags |= HAS_SYMS;
649 return abfd->xvec;
652 /* Check whether an existing file is an S-record file with symbols. */
654 static const bfd_target *
655 symbolsrec_object_p (abfd)
656 bfd *abfd;
658 char b[2];
660 srec_init ();
662 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
663 || bfd_read (b, 1, 2, abfd) != 2)
664 return NULL;
666 if (b[0] != '$' || b[1] != '$')
668 bfd_set_error (bfd_error_wrong_format);
669 return NULL;
672 if (! srec_mkobject (abfd)
673 || ! srec_scan (abfd))
674 return NULL;
676 if (abfd->symcount > 0)
677 abfd->flags |= HAS_SYMS;
679 return abfd->xvec;
682 /* Read in the contents of a section in an S-record file. */
684 static boolean
685 srec_read_section (abfd, section, contents)
686 bfd *abfd;
687 asection *section;
688 bfd_byte *contents;
690 int c;
691 bfd_size_type sofar = 0;
692 boolean error = false;
693 bfd_byte *buf = NULL;
694 size_t bufsize = 0;
696 if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
697 goto error_return;
699 while ((c = srec_get_byte (abfd, &error)) != EOF)
701 bfd_byte hdr[3];
702 unsigned int bytes;
703 bfd_vma address;
704 bfd_byte *data;
706 if (c == '\r' || c == '\n')
707 continue;
709 /* This is called after srec_scan has already been called, so we
710 ought to know the exact format. */
711 BFD_ASSERT (c == 'S');
713 if (bfd_read (hdr, 1, 3, abfd) != 3)
714 goto error_return;
716 BFD_ASSERT (ISHEX (hdr[1]) && ISHEX (hdr[2]));
718 bytes = HEX (hdr + 1);
720 if (bytes * 2 > bufsize)
722 if (buf != NULL)
723 free (buf);
724 buf = (bfd_byte *) bfd_malloc (bytes * 2);
725 if (buf == NULL)
726 goto error_return;
727 bufsize = bytes * 2;
730 if (bfd_read (buf, 1, bytes * 2, abfd) != bytes * 2)
731 goto error_return;
733 address = 0;
734 data = buf;
735 switch (hdr[0])
737 default:
738 BFD_ASSERT (sofar == section->_raw_size);
739 if (buf != NULL)
740 free (buf);
741 return true;
743 case '3':
744 address = HEX (data);
745 data += 2;
746 --bytes;
747 /* Fall through. */
748 case '2':
749 address = (address << 8) | HEX (data);
750 data += 2;
751 --bytes;
752 /* Fall through. */
753 case '1':
754 address = (address << 8) | HEX (data);
755 data += 2;
756 address = (address << 8) | HEX (data);
757 data += 2;
758 bytes -= 2;
760 if (address != section->vma + sofar)
762 /* We've come to the end of this section. */
763 BFD_ASSERT (sofar == section->_raw_size);
764 if (buf != NULL)
765 free (buf);
766 return true;
769 /* Don't consider checksum. */
770 --bytes;
772 while (bytes-- != 0)
774 contents[sofar] = HEX (data);
775 data += 2;
776 ++sofar;
779 break;
783 if (error)
784 goto error_return;
786 BFD_ASSERT (sofar == section->_raw_size);
788 if (buf != NULL)
789 free (buf);
791 return true;
793 error_return:
794 if (buf != NULL)
795 free (buf);
796 return false;
799 /* Get the contents of a section in an S-record file. */
801 static boolean
802 srec_get_section_contents (abfd, section, location, offset, count)
803 bfd *abfd;
804 asection *section;
805 PTR location;
806 file_ptr offset;
807 bfd_size_type count;
809 if (section->used_by_bfd == NULL)
811 section->used_by_bfd = bfd_alloc (abfd, section->_raw_size);
812 if (section->used_by_bfd == NULL
813 && section->_raw_size != 0)
814 return false;
816 if (! srec_read_section (abfd, section, section->used_by_bfd))
817 return false;
820 memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
821 (size_t) count);
823 return true;
826 /* Set the architecture. We accept an unknown architecture here. */
828 static boolean
829 srec_set_arch_mach (abfd, arch, mach)
830 bfd *abfd;
831 enum bfd_architecture arch;
832 unsigned long mach;
834 if (arch == bfd_arch_unknown)
836 abfd->arch_info = &bfd_default_arch_struct;
837 return true;
839 return bfd_default_set_arch_mach (abfd, arch, mach);
842 /* we have to save up all the Srecords for a splurge before output */
844 static boolean
845 srec_set_section_contents (abfd, section, location, offset, bytes_to_do)
846 bfd *abfd;
847 sec_ptr section;
848 PTR location;
849 file_ptr offset;
850 bfd_size_type bytes_to_do;
852 tdata_type *tdata = abfd->tdata.srec_data;
853 register srec_data_list_type *entry;
855 entry = ((srec_data_list_type *)
856 bfd_alloc (abfd, sizeof (srec_data_list_type)));
857 if (entry == NULL)
858 return false;
860 if (bytes_to_do
861 && (section->flags & SEC_ALLOC)
862 && (section->flags & SEC_LOAD))
864 bfd_byte *data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
865 if (data == NULL)
866 return false;
867 memcpy ((PTR) data, location, (size_t) bytes_to_do);
869 if ((section->lma + offset + bytes_to_do - 1) <= 0xffff)
873 else if ((section->lma + offset + bytes_to_do - 1) <= 0xffffff
874 && tdata->type <= 2)
876 tdata->type = 2;
878 else
880 tdata->type = 3;
883 entry->data = data;
884 entry->where = section->lma + offset;
885 entry->size = bytes_to_do;
887 /* Sort the records by address. Optimize for the common case of
888 adding a record to the end of the list. */
889 if (tdata->tail != NULL
890 && entry->where >= tdata->tail->where)
892 tdata->tail->next = entry;
893 entry->next = NULL;
894 tdata->tail = entry;
896 else
898 register srec_data_list_type **look;
900 for (look = &tdata->head;
901 *look != NULL && (*look)->where < entry->where;
902 look = &(*look)->next)
904 entry->next = *look;
905 *look = entry;
906 if (entry->next == NULL)
907 tdata->tail = entry;
910 return true;
913 /* Write a record of type, of the supplied number of bytes. The
914 supplied bytes and length don't have a checksum. That's worked out
915 here
917 static boolean
918 srec_write_record (abfd, type, address, data, end)
919 bfd *abfd;
920 int type;
921 bfd_vma address;
922 const bfd_byte *data;
923 const bfd_byte *end;
925 char buffer[MAXCHUNK];
926 unsigned int check_sum = 0;
927 CONST bfd_byte *src = data;
928 char *dst = buffer;
929 char *length;
930 bfd_size_type wrlen;
932 *dst++ = 'S';
933 *dst++ = '0' + type;
935 length = dst;
936 dst += 2; /* leave room for dst*/
938 switch (type)
940 case 3:
941 case 7:
942 TOHEX (dst, (address >> 24), check_sum);
943 dst += 2;
944 case 8:
945 case 2:
946 TOHEX (dst, (address >> 16), check_sum);
947 dst += 2;
948 case 9:
949 case 1:
950 case 0:
951 TOHEX (dst, (address >> 8), check_sum);
952 dst += 2;
953 TOHEX (dst, (address), check_sum);
954 dst += 2;
955 break;
958 for (src = data; src < end; src++)
960 TOHEX (dst, *src, check_sum);
961 dst += 2;
964 /* Fill in the length */
965 TOHEX (length, (dst - length) / 2, check_sum);
966 check_sum &= 0xff;
967 check_sum = 255 - check_sum;
968 TOHEX (dst, check_sum, check_sum);
969 dst += 2;
971 *dst++ = '\r';
972 *dst++ = '\n';
973 wrlen = dst - buffer;
974 if (bfd_write ((PTR) buffer, 1, wrlen, abfd) != wrlen)
975 return false;
976 return true;
981 static boolean
982 srec_write_header (abfd)
983 bfd *abfd;
985 bfd_byte buffer[MAXCHUNK];
986 bfd_byte *dst = buffer;
987 unsigned int i;
989 /* I'll put an arbitary 40 char limit on header size */
990 for (i = 0; i < 40 && abfd->filename[i]; i++)
992 *dst++ = abfd->filename[i];
994 return srec_write_record (abfd, 0, 0, buffer, dst);
997 static boolean
998 srec_write_section (abfd, tdata, list)
999 bfd *abfd;
1000 tdata_type *tdata;
1001 srec_data_list_type *list;
1003 unsigned int bytes_written = 0;
1004 bfd_byte *location = list->data;
1006 while (bytes_written < list->size)
1008 bfd_vma address;
1010 unsigned int bytes_this_chunk = list->size - bytes_written;
1012 if (bytes_this_chunk > CHUNK)
1014 bytes_this_chunk = CHUNK;
1017 address = list->where + bytes_written;
1019 if (! srec_write_record (abfd,
1020 tdata->type,
1021 address,
1022 location,
1023 location + bytes_this_chunk))
1024 return false;
1026 bytes_written += bytes_this_chunk;
1027 location += bytes_this_chunk;
1030 return true;
1033 static boolean
1034 srec_write_terminator (abfd, tdata)
1035 bfd *abfd;
1036 tdata_type *tdata;
1038 bfd_byte buffer[2];
1040 return srec_write_record (abfd, 10 - tdata->type,
1041 abfd->start_address, buffer, buffer);
1046 static boolean
1047 srec_write_symbols (abfd)
1048 bfd *abfd;
1050 char buffer[MAXCHUNK];
1051 /* Dump out the symbols of a bfd */
1052 int i;
1053 int count = bfd_get_symcount (abfd);
1055 if (count)
1057 size_t len;
1058 asymbol **table = bfd_get_outsymbols (abfd);
1059 sprintf (buffer, "$$ %s\r\n", abfd->filename);
1061 len = strlen (buffer);
1062 if (bfd_write (buffer, len, 1, abfd) != len)
1063 return false;
1065 for (i = 0; i < count; i++)
1067 asymbol *s = table[i];
1068 if (! bfd_is_local_label (abfd, s)
1069 && (s->flags & BSF_DEBUGGING) == 0)
1071 /* Just dump out non debug symbols */
1072 bfd_size_type l;
1073 char buf2[40], *p;
1075 sprintf_vma (buf2,
1076 s->value + s->section->output_section->lma
1077 + s->section->output_offset);
1078 p = buf2;
1079 while (p[0] == '0' && p[1] != 0)
1080 p++;
1081 sprintf (buffer, " %s $%s\r\n", s->name, p);
1082 l = strlen (buffer);
1083 if (bfd_write (buffer, l, 1, abfd) != l)
1084 return false;
1087 sprintf (buffer, "$$ \r\n");
1088 len = strlen (buffer);
1089 if (bfd_write (buffer, len, 1, abfd) != len)
1090 return false;
1093 return true;
1096 static boolean
1097 internal_srec_write_object_contents (abfd, symbols)
1098 bfd *abfd;
1099 int symbols;
1101 tdata_type *tdata = abfd->tdata.srec_data;
1102 srec_data_list_type *list;
1104 if (symbols)
1106 if (! srec_write_symbols (abfd))
1107 return false;
1110 if (! srec_write_header (abfd))
1111 return false;
1113 /* Now wander though all the sections provided and output them */
1114 list = tdata->head;
1116 while (list != (srec_data_list_type *) NULL)
1118 if (! srec_write_section (abfd, tdata, list))
1119 return false;
1120 list = list->next;
1122 return srec_write_terminator (abfd, tdata);
1125 static boolean
1126 srec_write_object_contents (abfd)
1127 bfd *abfd;
1129 return internal_srec_write_object_contents (abfd, 0);
1132 static boolean
1133 symbolsrec_write_object_contents (abfd)
1134 bfd *abfd;
1136 return internal_srec_write_object_contents (abfd, 1);
1139 /*ARGSUSED*/
1140 static int
1141 srec_sizeof_headers (abfd, exec)
1142 bfd *abfd ATTRIBUTE_UNUSED;
1143 boolean exec ATTRIBUTE_UNUSED;
1145 return 0;
1148 static asymbol *
1149 srec_make_empty_symbol (abfd)
1150 bfd *abfd;
1152 asymbol *new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol));
1153 if (new)
1154 new->the_bfd = abfd;
1155 return new;
1158 /* Return the amount of memory needed to read the symbol table. */
1160 static long
1161 srec_get_symtab_upper_bound (abfd)
1162 bfd *abfd;
1164 return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
1167 /* Return the symbol table. */
1169 static long
1170 srec_get_symtab (abfd, alocation)
1171 bfd *abfd;
1172 asymbol **alocation;
1174 unsigned int symcount = bfd_get_symcount (abfd);
1175 asymbol *csymbols;
1176 unsigned int i;
1178 csymbols = abfd->tdata.srec_data->csymbols;
1179 if (csymbols == NULL)
1181 asymbol *c;
1182 struct srec_symbol *s;
1184 csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
1185 if (csymbols == NULL && symcount != 0)
1186 return false;
1187 abfd->tdata.srec_data->csymbols = csymbols;
1189 for (s = abfd->tdata.srec_data->symbols, c = csymbols;
1190 s != NULL;
1191 s = s->next, ++c)
1193 c->the_bfd = abfd;
1194 c->name = s->name;
1195 c->value = s->val;
1196 c->flags = BSF_GLOBAL;
1197 c->section = bfd_abs_section_ptr;
1198 c->udata.p = NULL;
1202 for (i = 0; i < symcount; i++)
1203 *alocation++ = csymbols++;
1204 *alocation = NULL;
1206 return symcount;
1209 /*ARGSUSED*/
1210 static void
1211 srec_get_symbol_info (ignore_abfd, symbol, ret)
1212 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1213 asymbol *symbol;
1214 symbol_info *ret;
1216 bfd_symbol_info (symbol, ret);
1219 /*ARGSUSED*/
1220 static void
1221 srec_print_symbol (ignore_abfd, afile, symbol, how)
1222 bfd *ignore_abfd ATTRIBUTE_UNUSED;
1223 PTR afile;
1224 asymbol *symbol;
1225 bfd_print_symbol_type how;
1227 FILE *file = (FILE *) afile;
1228 switch (how)
1230 case bfd_print_symbol_name:
1231 fprintf (file, "%s", symbol->name);
1232 break;
1233 default:
1234 bfd_print_symbol_vandf ((PTR) file, symbol);
1235 fprintf (file, " %-5s %s",
1236 symbol->section->name,
1237 symbol->name);
1242 #define srec_close_and_cleanup _bfd_generic_close_and_cleanup
1243 #define srec_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
1244 #define srec_new_section_hook _bfd_generic_new_section_hook
1246 #define srec_bfd_is_local_label_name bfd_generic_is_local_label_name
1247 #define srec_get_lineno _bfd_nosymbols_get_lineno
1248 #define srec_find_nearest_line _bfd_nosymbols_find_nearest_line
1249 #define srec_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
1250 #define srec_read_minisymbols _bfd_generic_read_minisymbols
1251 #define srec_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
1253 #define srec_get_reloc_upper_bound \
1254 ((long (*) PARAMS ((bfd *, asection *))) bfd_0l)
1255 #define srec_canonicalize_reloc \
1256 ((long (*) PARAMS ((bfd *, asection *, arelent **, asymbol **))) bfd_0l)
1257 #define srec_bfd_reloc_type_lookup _bfd_norelocs_bfd_reloc_type_lookup
1259 #define srec_get_section_contents_in_window \
1260 _bfd_generic_get_section_contents_in_window
1262 #define srec_bfd_get_relocated_section_contents \
1263 bfd_generic_get_relocated_section_contents
1264 #define srec_bfd_relax_section bfd_generic_relax_section
1265 #define srec_bfd_gc_sections bfd_generic_gc_sections
1266 #define srec_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
1267 #define srec_bfd_link_add_symbols _bfd_generic_link_add_symbols
1268 #define srec_bfd_final_link _bfd_generic_final_link
1269 #define srec_bfd_link_split_section _bfd_generic_link_split_section
1271 const bfd_target srec_vec =
1273 "srec", /* name */
1274 bfd_target_srec_flavour,
1275 BFD_ENDIAN_UNKNOWN, /* target byte order */
1276 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
1277 (HAS_RELOC | EXEC_P | /* object flags */
1278 HAS_LINENO | HAS_DEBUG |
1279 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1280 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1281 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1282 0, /* leading underscore */
1283 ' ', /* ar_pad_char */
1284 16, /* ar_max_namelen */
1285 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1286 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1287 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1288 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1289 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1290 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1293 _bfd_dummy_target,
1294 srec_object_p, /* bfd_check_format */
1295 _bfd_dummy_target,
1296 _bfd_dummy_target,
1299 bfd_false,
1300 srec_mkobject,
1301 _bfd_generic_mkarchive,
1302 bfd_false,
1304 { /* bfd_write_contents */
1305 bfd_false,
1306 srec_write_object_contents,
1307 _bfd_write_archive_contents,
1308 bfd_false,
1311 BFD_JUMP_TABLE_GENERIC (srec),
1312 BFD_JUMP_TABLE_COPY (_bfd_generic),
1313 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1314 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1315 BFD_JUMP_TABLE_SYMBOLS (srec),
1316 BFD_JUMP_TABLE_RELOCS (srec),
1317 BFD_JUMP_TABLE_WRITE (srec),
1318 BFD_JUMP_TABLE_LINK (srec),
1319 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1321 NULL,
1323 (PTR) 0
1328 const bfd_target symbolsrec_vec =
1330 "symbolsrec", /* name */
1331 bfd_target_srec_flavour,
1332 BFD_ENDIAN_UNKNOWN, /* target byte order */
1333 BFD_ENDIAN_UNKNOWN, /* target headers byte order */
1334 (HAS_RELOC | EXEC_P | /* object flags */
1335 HAS_LINENO | HAS_DEBUG |
1336 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
1337 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
1338 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
1339 0, /* leading underscore */
1340 ' ', /* ar_pad_char */
1341 16, /* ar_max_namelen */
1342 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1343 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1344 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1345 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1346 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1347 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1350 _bfd_dummy_target,
1351 symbolsrec_object_p, /* bfd_check_format */
1352 _bfd_dummy_target,
1353 _bfd_dummy_target,
1356 bfd_false,
1357 srec_mkobject,
1358 _bfd_generic_mkarchive,
1359 bfd_false,
1361 { /* bfd_write_contents */
1362 bfd_false,
1363 symbolsrec_write_object_contents,
1364 _bfd_write_archive_contents,
1365 bfd_false,
1368 BFD_JUMP_TABLE_GENERIC (srec),
1369 BFD_JUMP_TABLE_COPY (_bfd_generic),
1370 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1371 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
1372 BFD_JUMP_TABLE_SYMBOLS (srec),
1373 BFD_JUMP_TABLE_RELOCS (srec),
1374 BFD_JUMP_TABLE_WRITE (srec),
1375 BFD_JUMP_TABLE_LINK (srec),
1376 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1378 NULL,
1380 (PTR) 0