Changeset 609 for branches/GNU/src/binutils/bfd/ihex.c
- Timestamp:
- Aug 16, 2003, 6:59:22 PM (22 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/GNU/src/binutils/bfd/ihex.c
-
Property cvs2svn:cvs-rev
changed from
1.1
to1.1.1.2
r608 r609 1 1 /* BFD back-end for Intel Hex objects. 2 Copyright 1995, 1996, 1998, 1999, 2000 Free Software Foundation, Inc. 2 Copyright 1995, 1996, 1998, 1999, 2000, 2001, 2002 3 Free Software Foundation, Inc. 3 4 Written by Ian Lance Taylor of Cygnus Support <ian@cygnus.com>. 4 5 5 This file is part of BFD, the Binary File Descriptor library.6 7 This program is free software; you can redistribute it and/or modify8 it under the terms of the GNU General Public License as published by9 the Free Software Foundation; either version 2 of the License, or10 (at your option) any later version.11 12 This program is distributed in the hope that it will be useful,13 but WITHOUT ANY WARRANTY; without even the implied warranty of14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the15 GNU General Public License for more details.16 17 You should have received a copy of the GNU General Public License18 along with this program; if not, write to the Free Software19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */6 This file is part of BFD, the Binary File Descriptor library. 7 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. 12 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. 17 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. */ 20 21 21 22 /* This is what Intel Hex files look like: … … 123 124 #include "libbfd.h" 124 125 #include "libiberty.h" 125 126 #include <ctype.h> 127 128 static void ihex_init PARAMS ((void)); 129 static boolean ihex_mkobject PARAMS ((bfd *)); 130 static INLINE int ihex_get_byte PARAMS ((bfd *, boolean *)); 131 static void ihex_bad_byte PARAMS ((bfd *, unsigned int, int, boolean)); 132 static boolean ihex_scan PARAMS ((bfd *)); 133 static const bfd_target *ihex_object_p PARAMS ((bfd *)); 134 static boolean ihex_read_section PARAMS ((bfd *, asection *, bfd_byte *)); 135 static boolean ihex_get_section_contents 126 #include "safe-ctype.h" 127 128 static void ihex_init 129 PARAMS ((void)); 130 static bfd_boolean ihex_mkobject 131 PARAMS ((bfd *)); 132 static INLINE int ihex_get_byte 133 PARAMS ((bfd *, bfd_boolean *)); 134 static void ihex_bad_byte 135 PARAMS ((bfd *, unsigned int, int, bfd_boolean)); 136 static bfd_boolean ihex_scan 137 PARAMS ((bfd *)); 138 static const bfd_target *ihex_object_p 139 PARAMS ((bfd *)); 140 static bfd_boolean ihex_read_section 141 PARAMS ((bfd *, asection *, bfd_byte *)); 142 static bfd_boolean ihex_get_section_contents 136 143 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type)); 137 static b oolean ihex_set_section_contents144 static bfd_boolean ihex_set_section_contents 138 145 PARAMS ((bfd *, asection *, PTR, file_ptr, bfd_size_type)); 139 static b oolean ihex_write_record140 PARAMS ((bfd *, bfd_size_type, bfd_vma, unsigned int, bfd_byte *));141 static b oolean ihex_write_object_contents PARAMS ((bfd *));142 static asymbol *ihex_make_empty_symbolPARAMS ((bfd *));143 static b oolean ihex_set_arch_mach146 static bfd_boolean ihex_write_record 147 PARAMS ((bfd *, size_t, unsigned int, unsigned int, bfd_byte *)); 148 static bfd_boolean ihex_write_object_contents 149 PARAMS ((bfd *)); 150 static bfd_boolean ihex_set_arch_mach 144 151 PARAMS ((bfd *, enum bfd_architecture, unsigned long)); 145 static int ihex_sizeof_headers PARAMS ((bfd *, boolean)); 152 static int ihex_sizeof_headers 153 PARAMS ((bfd *, bfd_boolean)); 146 154 147 155 /* The number of bytes we put on one line during output. */ … … 149 157 #define CHUNK 16 150 158 151 /* Macros for converting between hex and binary. */152 153 #define NIBBLE(x) (hex_value (x))159 /* Macros for converting between hex and binary. */ 160 161 #define NIBBLE(x) (hex_value (x)) 154 162 #define HEX2(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1])) 155 163 #define HEX4(buffer) ((HEX2 (buffer) << 8) + HEX2 ((buffer) + 2)) 156 #define ISHEX(x) (hex_p (x))164 #define ISHEX(x) (hex_p (x)) 157 165 158 166 /* When we write out an ihex value, the values can not be output as … … 180 188 ihex_init () 181 189 { 182 static b oolean inited;190 static bfd_boolean inited; 183 191 184 192 if (! inited) 185 193 { 186 inited = true;194 inited = TRUE; 187 195 hex_init (); 188 196 } … … 191 199 /* Create an ihex object. */ 192 200 193 static b oolean201 static bfd_boolean 194 202 ihex_mkobject (abfd) 195 203 bfd *abfd; 196 204 { 197 if (abfd->tdata.ihex_data == NULL) 198 { 199 struct ihex_data_struct *tdata; 200 201 tdata = ((struct ihex_data_struct *) 202 bfd_alloc (abfd, sizeof (struct ihex_data_struct))); 203 if (tdata == NULL) 204 return false; 205 abfd->tdata.ihex_data = tdata; 206 tdata->head = NULL; 207 tdata->tail = NULL; 208 } 209 210 return true; 205 struct ihex_data_struct *tdata; 206 bfd_size_type amt = sizeof (struct ihex_data_struct); 207 208 tdata = (struct ihex_data_struct *) bfd_alloc (abfd, amt); 209 if (tdata == NULL) 210 return FALSE; 211 212 abfd->tdata.ihex_data = tdata; 213 tdata->head = NULL; 214 tdata->tail = NULL; 215 return TRUE; 211 216 } 212 217 … … 217 222 ihex_get_byte (abfd, errorptr) 218 223 bfd *abfd; 219 b oolean *errorptr;224 bfd_boolean *errorptr; 220 225 { 221 226 bfd_byte c; 222 227 223 if (bfd_ read (&c, 1,1, abfd) != 1)228 if (bfd_bread (&c, (bfd_size_type) 1, abfd) != 1) 224 229 { 225 230 if (bfd_get_error () != bfd_error_file_truncated) 226 *errorptr = true;231 *errorptr = TRUE; 227 232 return EOF; 228 233 } … … 238 243 unsigned int lineno; 239 244 int c; 240 b oolean error;245 bfd_boolean error; 241 246 { 242 247 if (c == EOF) … … 249 254 char buf[10]; 250 255 251 if (! isprint(c))256 if (! ISPRINT (c)) 252 257 sprintf (buf, "\\%03o", (unsigned int) c); 253 258 else … … 258 263 (*_bfd_error_handler) 259 264 (_("%s:%d: unexpected character `%s' in Intel Hex file\n"), 260 bfd_ get_filename (abfd), lineno, buf);265 bfd_archive_filename (abfd), lineno, buf); 261 266 bfd_set_error (bfd_error_bad_value); 262 267 } … … 266 271 section for each contiguous set of bytes. */ 267 272 268 static b oolean273 static bfd_boolean 269 274 ihex_scan (abfd) 270 275 bfd *abfd; … … 273 278 bfd_vma extbase; 274 279 asection *sec; 275 int lineno;276 b oolean error;280 unsigned int lineno; 281 bfd_boolean error; 277 282 bfd_byte *buf = NULL; 278 283 size_t bufsize; … … 288 293 sec = NULL; 289 294 lineno = 1; 290 error = false;295 error = FALSE; 291 296 bufsize = 0; 297 292 298 while ((c = ihex_get_byte (abfd, &error)) != EOF) 293 299 { … … 316 322 317 323 /* This is a data record. */ 318 319 324 pos = bfd_tell (abfd) - 1; 320 325 321 326 /* Read the header bytes. */ 322 323 if (bfd_read (hdr, 1, 8, abfd) != 8) 327 if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8) 324 328 goto error_return; 325 329 … … 338 342 339 343 /* Read the data bytes. */ 340 341 344 chars = len * 2 + 2; 342 345 if (chars >= bufsize) 343 346 { 344 buf = (bfd_byte *) bfd_realloc (buf, chars);347 buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) chars); 345 348 if (buf == NULL) 346 349 goto error_return; … … 348 351 } 349 352 350 if (bfd_ read (buf, 1,chars, abfd) != chars)353 if (bfd_bread (buf, (bfd_size_type) chars, abfd) != chars) 351 354 goto error_return; 352 355 … … 367 370 { 368 371 (*_bfd_error_handler) 369 (_("%s:% d: bad checksum in Intel Hex file (expected %u, found %u)"),370 bfd_ get_filename (abfd), lineno,372 (_("%s:%u: bad checksum in Intel Hex file (expected %u, found %u)"), 373 bfd_archive_filename (abfd), lineno, 371 374 (- chksum) & 0xff, (unsigned int) HEX2 (buf + 2 * i)); 372 375 bfd_set_error (bfd_error_bad_value); … … 389 392 char secbuf[20]; 390 393 char *secname; 394 bfd_size_type amt; 391 395 392 396 sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1); 393 secname = (char *) bfd_alloc (abfd, strlen (secbuf) + 1); 397 amt = strlen (secbuf) + 1; 398 secname = (char *) bfd_alloc (abfd, amt); 394 399 if (secname == NULL) 395 400 goto error_return; … … 412 417 if (buf != NULL) 413 418 free (buf); 414 return true;419 return TRUE; 415 420 416 421 case 2: … … 419 424 { 420 425 (*_bfd_error_handler) 421 (_("%s:% d: bad extended address record length in Intel Hex file"),422 bfd_ get_filename (abfd), lineno);426 (_("%s:%u: bad extended address record length in Intel Hex file"), 427 bfd_archive_filename (abfd), lineno); 423 428 bfd_set_error (bfd_error_bad_value); 424 429 goto error_return; … … 436 441 { 437 442 (*_bfd_error_handler) 438 (_("%s:% d: bad extended start address length in Intel Hex file"),439 bfd_ get_filename (abfd), lineno);443 (_("%s:%u: bad extended start address length in Intel Hex file"), 444 bfd_archive_filename (abfd), lineno); 440 445 bfd_set_error (bfd_error_bad_value); 441 446 goto error_return; … … 453 458 { 454 459 (*_bfd_error_handler) 455 (_("%s:% d: bad extended linear address record length in Intel Hex file"),456 bfd_ get_filename (abfd), lineno);460 (_("%s:%u: bad extended linear address record length in Intel Hex file"), 461 bfd_archive_filename (abfd), lineno); 457 462 bfd_set_error (bfd_error_bad_value); 458 463 goto error_return; … … 470 475 { 471 476 (*_bfd_error_handler) 472 (_("%s:% d: bad extended linear start address length in Intel Hex file"),473 bfd_ get_filename (abfd), lineno);477 (_("%s:%u: bad extended linear start address length in Intel Hex file"), 478 bfd_archive_filename (abfd), lineno); 474 479 bfd_set_error (bfd_error_bad_value); 475 480 goto error_return; … … 487 492 default: 488 493 (*_bfd_error_handler) 489 (_("%s:% d: unrecognized ihex type %u in Intel Hex file\n"),490 bfd_ get_filename (abfd), lineno, type);494 (_("%s:%u: unrecognized ihex type %u in Intel Hex file\n"), 495 bfd_archive_filename (abfd), lineno, type); 491 496 bfd_set_error (bfd_error_bad_value); 492 497 goto error_return; … … 501 506 free (buf); 502 507 503 return true;508 return TRUE; 504 509 505 510 error_return: 506 511 if (buf != NULL) 507 512 free (buf); 508 return false;513 return FALSE; 509 514 } 510 515 … … 515 520 bfd *abfd; 516 521 { 522 PTR tdata_save; 517 523 bfd_byte b[9]; 518 524 unsigned int i; … … 523 529 if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0) 524 530 return NULL; 525 if (bfd_ read (b, 1,9, abfd) != 9)531 if (bfd_bread (b, (bfd_size_type) 9, abfd) != 9) 526 532 { 527 533 if (bfd_get_error () == bfd_error_file_truncated) … … 553 559 554 560 /* OK, it looks like it really is an Intel Hex file. */ 555 556 if (! ihex_mkobject (abfd) 557 || ! ihex_scan (abfd)) 558 return NULL; 561 tdata_save = abfd->tdata.any; 562 if (! ihex_mkobject (abfd) || ! ihex_scan (abfd)) 563 { 564 if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL) 565 bfd_release (abfd, abfd->tdata.any); 566 abfd->tdata.any = tdata_save; 567 return NULL; 568 } 559 569 560 570 return abfd->xvec; … … 563 573 /* Read the contents of a section in an Intel Hex file. */ 564 574 565 static b oolean575 static bfd_boolean 566 576 ihex_read_section (abfd, section, contents) 567 577 bfd *abfd; … … 573 583 bfd_byte *buf = NULL; 574 584 size_t bufsize; 575 b oolean error;585 bfd_boolean error; 576 586 577 587 if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0) … … 580 590 p = contents; 581 591 bufsize = 0; 582 error = false;592 error = FALSE; 583 593 while ((c = ihex_get_byte (abfd, &error)) != EOF) 584 594 { … … 596 606 BFD_ASSERT (c == ':'); 597 607 598 if (bfd_ read (hdr, 1,8, abfd) != 8)608 if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8) 599 609 goto error_return; 600 610 … … 608 618 (*_bfd_error_handler) 609 619 (_("%s: internal error in ihex_read_section"), 610 bfd_ get_filename (abfd));620 bfd_archive_filename (abfd)); 611 621 bfd_set_error (bfd_error_bad_value); 612 622 goto error_return; … … 615 625 if (len * 2 > bufsize) 616 626 { 617 buf = (bfd_byte *) bfd_realloc (buf, len * 2);627 buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) len * 2); 618 628 if (buf == NULL) 619 629 goto error_return; … … 621 631 } 622 632 623 if (bfd_ read (buf, 1,len * 2, abfd) != len * 2)633 if (bfd_bread (buf, (bfd_size_type) len * 2, abfd) != len * 2) 624 634 goto error_return; 625 635 … … 631 641 if (buf != NULL) 632 642 free (buf); 633 return true;643 return TRUE; 634 644 } 635 645 636 646 /* Skip the checksum. */ 637 if (bfd_ read (buf, 1,2, abfd) != 2)647 if (bfd_bread (buf, (bfd_size_type) 2, abfd) != 2) 638 648 goto error_return; 639 649 } … … 643 653 (*_bfd_error_handler) 644 654 (_("%s: bad section length in ihex_read_section"), 645 bfd_ get_filename (abfd));655 bfd_archive_filename (abfd)); 646 656 bfd_set_error (bfd_error_bad_value); 647 657 goto error_return; … … 651 661 free (buf); 652 662 653 return true;663 return TRUE; 654 664 655 665 error_return: 656 666 if (buf != NULL) 657 667 free (buf); 658 return false;668 return FALSE; 659 669 } 660 670 661 671 /* Get the contents of a section in an Intel Hex file. */ 662 672 663 static b oolean673 static bfd_boolean 664 674 ihex_get_section_contents (abfd, section, location, offset, count) 665 675 bfd *abfd; … … 673 683 section->used_by_bfd = bfd_alloc (abfd, section->_raw_size); 674 684 if (section->used_by_bfd == NULL) 675 return false;685 return FALSE; 676 686 if (! ihex_read_section (abfd, section, section->used_by_bfd)) 677 return false;687 return FALSE; 678 688 } 679 689 … … 681 691 (size_t) count); 682 692 683 return true;693 return TRUE; 684 694 } 685 695 686 696 /* Set the contents of a section in an Intel Hex file. */ 687 697 688 static b oolean698 static bfd_boolean 689 699 ihex_set_section_contents (abfd, section, location, offset, count) 690 700 bfd *abfd; … … 697 707 bfd_byte *data; 698 708 struct ihex_data_struct *tdata; 709 bfd_size_type amt; 699 710 700 711 if (count == 0 701 712 || (section->flags & SEC_ALLOC) == 0 702 713 || (section->flags & SEC_LOAD) == 0) 703 return true;704 705 n = ((struct ihex_data_list *)706 bfd_alloc (abfd, sizeof (struct ihex_data_list)));714 return TRUE; 715 716 amt = sizeof (struct ihex_data_list); 717 n = (struct ihex_data_list *) bfd_alloc (abfd, amt); 707 718 if (n == NULL) 708 return false;719 return FALSE; 709 720 710 721 data = (bfd_byte *) bfd_alloc (abfd, count); 711 722 if (data == NULL) 712 return false;723 return FALSE; 713 724 memcpy (data, location, (size_t) count); 714 725 … … 741 752 } 742 753 743 return true;754 return TRUE; 744 755 } 745 756 746 757 /* Write a record out to an Intel Hex file. */ 747 758 748 static b oolean759 static bfd_boolean 749 760 ihex_write_record (abfd, count, addr, type, data) 750 761 bfd *abfd; 751 bfd_size_typecount;752 bfd_vmaaddr;762 size_t count; 763 unsigned int addr; 753 764 unsigned int type; 754 765 bfd_byte *data; … … 759 770 unsigned int chksum; 760 771 unsigned int i; 772 size_t total; 761 773 762 774 #define TOHEX(buf, v) \ … … 781 793 p[3] = '\n'; 782 794 783 if (bfd_write (buf, 1, 9 + count * 2 + 4, abfd) != 9 + count * 2 + 4) 784 return false; 785 786 return true; 795 total = 9 + count * 2 + 4; 796 if (bfd_bwrite (buf, (bfd_size_type) total, abfd) != total) 797 return FALSE; 798 799 return TRUE; 787 800 } 788 801 789 802 /* Write out an Intel Hex file. */ 790 803 791 static b oolean804 static bfd_boolean 792 805 ihex_write_object_contents (abfd) 793 806 bfd *abfd; … … 810 823 while (count > 0) 811 824 { 812 bfd_size_type now; 825 size_t now; 826 unsigned int rec_addr; 813 827 814 828 now = count; 815 if ( now> CHUNK)829 if (count > CHUNK) 816 830 now = CHUNK; 817 831 … … 830 844 addr[1] = (bfd_byte)(segbase >> 4) & 0xff; 831 845 if (! ihex_write_record (abfd, 2, 0, 2, addr)) 832 return false;846 return FALSE; 833 847 } 834 848 else … … 845 859 addr[1] = 0; 846 860 if (! ihex_write_record (abfd, 2, 0, 2, addr)) 847 return false;861 return FALSE; 848 862 segbase = 0; 849 863 } … … 856 870 sprintf_vma (buf, where); 857 871 (*_bfd_error_handler) 858 (_("%s: address 0x%s out of range for Inte xHex file"),872 (_("%s: address 0x%s out of range for Intel Hex file"), 859 873 bfd_get_filename (abfd), buf); 860 874 bfd_set_error (bfd_error_bad_value); 861 return false;875 return FALSE; 862 876 } 863 877 addr[0] = (bfd_byte)(extbase >> 24) & 0xff; 864 878 addr[1] = (bfd_byte)(extbase >> 16) & 0xff; 865 879 if (! ihex_write_record (abfd, 2, 0, 4, addr)) 866 return false;880 return FALSE; 867 881 } 868 882 } 869 883 870 if (! ihex_write_record (abfd, now, where - (extbase + segbase), 871 0, p)) 872 return false; 884 rec_addr = where - (extbase + segbase); 885 886 /* Output records shouldn't cross 64K boundaries. */ 887 if (rec_addr + now > 0xffff) 888 now = 0x10000 - rec_addr; 889 890 if (! ihex_write_record (abfd, now, rec_addr, 0, p)) 891 return FALSE; 873 892 874 893 where += now; … … 892 911 startbuf[3] = (bfd_byte)start & 0xff; 893 912 if (! ihex_write_record (abfd, 4, 0, 3, startbuf)) 894 return false;913 return FALSE; 895 914 } 896 915 else … … 901 920 startbuf[3] = (bfd_byte)start & 0xff; 902 921 if (! ihex_write_record (abfd, 4, 0, 5, startbuf)) 903 return false;922 return FALSE; 904 923 } 905 924 } 906 925 907 926 if (! ihex_write_record (abfd, 0, 0, 1, NULL)) 908 return false; 909 910 return true; 911 } 912 913 /* Make an empty symbol. This is required only because 914 bfd_make_section_anyway wants to create a symbol for the section. */ 915 916 static asymbol * 917 ihex_make_empty_symbol (abfd) 918 bfd *abfd; 919 { 920 asymbol *new; 921 922 new = (asymbol *) bfd_zalloc (abfd, sizeof (asymbol)); 923 if (new != NULL) 924 new->the_bfd = abfd; 925 return new; 927 return FALSE; 928 929 return TRUE; 926 930 } 927 931 … … 929 933 irrelevant, so we ignore errors about unknown architectures. */ 930 934 931 static b oolean935 static bfd_boolean 932 936 ihex_set_arch_mach (abfd, arch, mach) 933 937 bfd *abfd; … … 938 942 { 939 943 if (arch != bfd_arch_unknown) 940 return false;941 } 942 return true;944 return FALSE; 945 } 946 return TRUE; 943 947 } 944 948 … … 949 953 ihex_sizeof_headers (abfd, exec) 950 954 bfd *abfd ATTRIBUTE_UNUSED; 951 b oolean exec ATTRIBUTE_UNUSED;955 bfd_boolean exec ATTRIBUTE_UNUSED; 952 956 { 953 957 return 0; … … 965 969 #define ihex_get_symtab \ 966 970 ((long (*) PARAMS ((bfd *, asymbol **))) bfd_0l) 971 #define ihex_make_empty_symbol _bfd_generic_make_empty_symbol 967 972 #define ihex_print_symbol _bfd_nosymbols_print_symbol 968 973 #define ihex_get_symbol_info _bfd_nosymbols_get_symbol_info … … 984 989 #define ihex_bfd_relax_section bfd_generic_relax_section 985 990 #define ihex_bfd_gc_sections bfd_generic_gc_sections 991 #define ihex_bfd_merge_sections bfd_generic_merge_sections 992 #define ihex_bfd_discard_group bfd_generic_discard_group 986 993 #define ihex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create 994 #define ihex_bfd_link_hash_table_free _bfd_generic_link_hash_table_free 987 995 #define ihex_bfd_link_add_symbols _bfd_generic_link_add_symbols 996 #define ihex_bfd_link_just_syms _bfd_generic_link_just_syms 988 997 #define ihex_bfd_final_link _bfd_generic_final_link 989 998 #define ihex_bfd_link_split_section _bfd_generic_link_split_section … … 1039 1048 1040 1049 NULL, 1041 1050 1042 1051 (PTR) 0 1043 1052 }; -
Property cvs2svn:cvs-rev
changed from
Note:
See TracChangeset
for help on using the changeset viewer.