ihex.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. /* BFD back-end for Intel Hex objects.
  2. Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor of Cygnus Support <ian@cygnus.com>.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* This is what Intel Hex files look like:
  18. 1. INTEL FORMATS
  19. A. Intel 1
  20. 16-bit address-field format, for files 64k bytes in length or less.
  21. DATA RECORD
  22. Byte 1 Header = colon(:)
  23. 2..3 The number of data bytes in hex notation
  24. 4..5 High byte of the record load address
  25. 6..7 Low byte of the record load address
  26. 8..9 Record type, must be "00"
  27. 10..x Data bytes in hex notation:
  28. x = (number of bytes - 1) * 2 + 11
  29. x+1..x+2 Checksum in hex notation
  30. x+3..x+4 Carriage return, line feed
  31. END RECORD
  32. Byte 1 Header = colon (:)
  33. 2..3 The byte count, must be "00"
  34. 4..7 Transfer-address (usually "0000")
  35. the jump-to address, execution start address
  36. 8..9 Record type, must be "01"
  37. 10..11 Checksum, in hex notation
  38. 12..13 Carriage return, line feed
  39. B. INTEL 2
  40. MCS-86 format, using a 20-bit address for files larger than 64K bytes.
  41. DATA RECORD
  42. Byte 1 Header = colon (:)
  43. 2..3 The byte count of this record, hex notation
  44. 4..5 High byte of the record load address
  45. 6..7 Low byte of the record load address
  46. 8..9 Record type, must be "00"
  47. 10..x The data bytes in hex notation:
  48. x = (number of data bytes - 1) * 2 + 11
  49. x+1..x+2 Checksum in hex notation
  50. x+3..x+4 Carriage return, line feed
  51. EXTENDED ADDRESS RECORD
  52. Byte 1 Header = colon(:)
  53. 2..3 The byte count, must be "02"
  54. 4..7 Load address, must be "0000"
  55. 8..9 Record type, must be "02"
  56. 10..11 High byte of the offset address
  57. 12..13 Low byte of the offset address
  58. 14..15 Checksum in hex notation
  59. 16..17 Carriage return, line feed
  60. The checksums are the two's complement of the 8-bit sum
  61. without carry of the byte count, offset address, and the
  62. record type.
  63. START ADDRESS RECORD
  64. Byte 1 Header = colon (:)
  65. 2..3 The byte count, must be "04"
  66. 4..7 Load address, must be "0000"
  67. 8..9 Record type, must be "03"
  68. 10..13 8086 CS value
  69. 14..17 8086 IP value
  70. 18..19 Checksum in hex notation
  71. 20..21 Carriage return, line feed
  72. Another document reports these additional types:
  73. EXTENDED LINEAR ADDRESS RECORD
  74. Byte 1 Header = colon (:)
  75. 2..3 The byte count, must be "02"
  76. 4..7 Load address, must be "0000"
  77. 8..9 Record type, must be "04"
  78. 10..13 Upper 16 bits of address of subsequent records
  79. 14..15 Checksum in hex notation
  80. 16..17 Carriage return, line feed
  81. START LINEAR ADDRESS RECORD
  82. Byte 1 Header = colon (:)
  83. 2..3 The byte count, must be "02"
  84. 4..7 Load address, must be "0000"
  85. 8..9 Record type, must be "05"
  86. 10..13 Upper 16 bits of start address
  87. 14..15 Checksum in hex notation
  88. 16..17 Carriage return, line feed
  89. The MRI compiler uses this, which is a repeat of type 5:
  90. EXTENDED START RECORD
  91. Byte 1 Header = colon (:)
  92. 2..3 The byte count, must be "04"
  93. 4..7 Load address, must be "0000"
  94. 8..9 Record type, must be "05"
  95. 10..13 Upper 16 bits of start address
  96. 14..17 Lower 16 bits of start address
  97. 18..19 Checksum in hex notation
  98. 20..21 Carriage return, line feed. */
  99. #include "sysdep.h"
  100. #include "bfd.h"
  101. #include "libbfd.h"
  102. #include "libiberty.h"
  103. #include "safe-ctype.h"
  104. /* The number of bytes we put on one line during output. */
  105. #define CHUNK 16
  106. /* Macros for converting between hex and binary. */
  107. #define NIBBLE(x) (hex_value (x))
  108. #define HEX2(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
  109. #define HEX4(buffer) ((HEX2 (buffer) << 8) + HEX2 ((buffer) + 2))
  110. #define ISHEX(x) (hex_p (x))
  111. /* When we write out an ihex value, the values can not be output as
  112. they are seen. Instead, we hold them in memory in this structure. */
  113. struct ihex_data_list
  114. {
  115. struct ihex_data_list *next;
  116. bfd_byte *data;
  117. bfd_vma where;
  118. bfd_size_type size;
  119. };
  120. /* The ihex tdata information. */
  121. struct ihex_data_struct
  122. {
  123. struct ihex_data_list *head;
  124. struct ihex_data_list *tail;
  125. };
  126. /* Initialize by filling in the hex conversion array. */
  127. static void
  128. ihex_init (void)
  129. {
  130. static bfd_boolean inited;
  131. if (! inited)
  132. {
  133. inited = TRUE;
  134. hex_init ();
  135. }
  136. }
  137. /* Create an ihex object. */
  138. static bfd_boolean
  139. ihex_mkobject (bfd *abfd)
  140. {
  141. struct ihex_data_struct *tdata;
  142. tdata = (struct ihex_data_struct *) bfd_alloc (abfd, sizeof (* tdata));
  143. if (tdata == NULL)
  144. return FALSE;
  145. abfd->tdata.ihex_data = tdata;
  146. tdata->head = NULL;
  147. tdata->tail = NULL;
  148. return TRUE;
  149. }
  150. /* Read a byte from a BFD. Set *ERRORPTR if an error occurred.
  151. Return EOF on error or end of file. */
  152. static INLINE int
  153. ihex_get_byte (bfd *abfd, bfd_boolean *errorptr)
  154. {
  155. bfd_byte c;
  156. if (bfd_bread (&c, (bfd_size_type) 1, abfd) != 1)
  157. {
  158. if (bfd_get_error () != bfd_error_file_truncated)
  159. *errorptr = TRUE;
  160. return EOF;
  161. }
  162. return (int) (c & 0xff);
  163. }
  164. /* Report a problem in an Intel Hex file. */
  165. static void
  166. ihex_bad_byte (bfd *abfd, unsigned int lineno, int c, bfd_boolean error)
  167. {
  168. if (c == EOF)
  169. {
  170. if (! error)
  171. bfd_set_error (bfd_error_file_truncated);
  172. }
  173. else
  174. {
  175. char buf[10];
  176. if (! ISPRINT (c))
  177. sprintf (buf, "\\%03o", (unsigned int) c & 0xff);
  178. else
  179. {
  180. buf[0] = c;
  181. buf[1] = '\0';
  182. }
  183. (*_bfd_error_handler)
  184. (_("%B:%d: unexpected character `%s' in Intel Hex file"),
  185. abfd, lineno, buf);
  186. bfd_set_error (bfd_error_bad_value);
  187. }
  188. }
  189. /* Read an Intel hex file and turn it into sections. We create a new
  190. section for each contiguous set of bytes. */
  191. static bfd_boolean
  192. ihex_scan (bfd *abfd)
  193. {
  194. bfd_vma segbase;
  195. bfd_vma extbase;
  196. asection *sec;
  197. unsigned int lineno;
  198. bfd_boolean error;
  199. bfd_byte *buf = NULL;
  200. size_t bufsize;
  201. int c;
  202. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  203. goto error_return;
  204. abfd->start_address = 0;
  205. segbase = 0;
  206. extbase = 0;
  207. sec = NULL;
  208. lineno = 1;
  209. error = FALSE;
  210. bufsize = 0;
  211. while ((c = ihex_get_byte (abfd, &error)) != EOF)
  212. {
  213. if (c == '\r')
  214. continue;
  215. else if (c == '\n')
  216. {
  217. ++lineno;
  218. continue;
  219. }
  220. else if (c != ':')
  221. {
  222. ihex_bad_byte (abfd, lineno, c, error);
  223. goto error_return;
  224. }
  225. else
  226. {
  227. file_ptr pos;
  228. unsigned char hdr[8];
  229. unsigned int i;
  230. unsigned int len;
  231. bfd_vma addr;
  232. unsigned int type;
  233. unsigned int chars;
  234. unsigned int chksum;
  235. /* This is a data record. */
  236. pos = bfd_tell (abfd) - 1;
  237. /* Read the header bytes. */
  238. if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
  239. goto error_return;
  240. for (i = 0; i < 8; i++)
  241. {
  242. if (! ISHEX (hdr[i]))
  243. {
  244. ihex_bad_byte (abfd, lineno, hdr[i], error);
  245. goto error_return;
  246. }
  247. }
  248. len = HEX2 (hdr);
  249. addr = HEX4 (hdr + 2);
  250. type = HEX2 (hdr + 6);
  251. /* Read the data bytes. */
  252. chars = len * 2 + 2;
  253. if (chars >= bufsize)
  254. {
  255. buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) chars);
  256. if (buf == NULL)
  257. goto error_return;
  258. bufsize = chars;
  259. }
  260. if (bfd_bread (buf, (bfd_size_type) chars, abfd) != chars)
  261. goto error_return;
  262. for (i = 0; i < chars; i++)
  263. {
  264. if (! ISHEX (buf[i]))
  265. {
  266. ihex_bad_byte (abfd, lineno, buf[i], error);
  267. goto error_return;
  268. }
  269. }
  270. /* Check the checksum. */
  271. chksum = len + addr + (addr >> 8) + type;
  272. for (i = 0; i < len; i++)
  273. chksum += HEX2 (buf + 2 * i);
  274. if (((- chksum) & 0xff) != (unsigned int) HEX2 (buf + 2 * i))
  275. {
  276. (*_bfd_error_handler)
  277. (_("%B:%u: bad checksum in Intel Hex file (expected %u, found %u)"),
  278. abfd, lineno,
  279. (- chksum) & 0xff, (unsigned int) HEX2 (buf + 2 * i));
  280. bfd_set_error (bfd_error_bad_value);
  281. goto error_return;
  282. }
  283. switch (type)
  284. {
  285. case 0:
  286. /* This is a data record. */
  287. if (sec != NULL
  288. && sec->vma + sec->size == extbase + segbase + addr)
  289. {
  290. /* This data goes at the end of the section we are
  291. currently building. */
  292. sec->size += len;
  293. }
  294. else if (len > 0)
  295. {
  296. char secbuf[20];
  297. char *secname;
  298. bfd_size_type amt;
  299. flagword flags;
  300. sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
  301. amt = strlen (secbuf) + 1;
  302. secname = (char *) bfd_alloc (abfd, amt);
  303. if (secname == NULL)
  304. goto error_return;
  305. strcpy (secname, secbuf);
  306. flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  307. sec = bfd_make_section_with_flags (abfd, secname, flags);
  308. if (sec == NULL)
  309. goto error_return;
  310. sec->vma = extbase + segbase + addr;
  311. sec->lma = extbase + segbase + addr;
  312. sec->size = len;
  313. sec->filepos = pos;
  314. }
  315. break;
  316. case 1:
  317. /* An end record. */
  318. if (abfd->start_address == 0)
  319. abfd->start_address = addr;
  320. if (buf != NULL)
  321. free (buf);
  322. return TRUE;
  323. case 2:
  324. /* An extended address record. */
  325. if (len != 2)
  326. {
  327. (*_bfd_error_handler)
  328. (_("%B:%u: bad extended address record length in Intel Hex file"),
  329. abfd, lineno);
  330. bfd_set_error (bfd_error_bad_value);
  331. goto error_return;
  332. }
  333. segbase = HEX4 (buf) << 4;
  334. sec = NULL;
  335. break;
  336. case 3:
  337. /* An extended start address record. */
  338. if (len != 4)
  339. {
  340. (*_bfd_error_handler)
  341. (_("%B:%u: bad extended start address length in Intel Hex file"),
  342. abfd, lineno);
  343. bfd_set_error (bfd_error_bad_value);
  344. goto error_return;
  345. }
  346. abfd->start_address += (HEX4 (buf) << 4) + HEX4 (buf + 4);
  347. sec = NULL;
  348. break;
  349. case 4:
  350. /* An extended linear address record. */
  351. if (len != 2)
  352. {
  353. (*_bfd_error_handler)
  354. (_("%B:%u: bad extended linear address record length in Intel Hex file"),
  355. abfd, lineno);
  356. bfd_set_error (bfd_error_bad_value);
  357. goto error_return;
  358. }
  359. extbase = HEX4 (buf) << 16;
  360. sec = NULL;
  361. break;
  362. case 5:
  363. /* An extended linear start address record. */
  364. if (len != 2 && len != 4)
  365. {
  366. (*_bfd_error_handler)
  367. (_("%B:%u: bad extended linear start address length in Intel Hex file"),
  368. abfd, lineno);
  369. bfd_set_error (bfd_error_bad_value);
  370. goto error_return;
  371. }
  372. if (len == 2)
  373. abfd->start_address += HEX4 (buf) << 16;
  374. else
  375. abfd->start_address = (HEX4 (buf) << 16) + HEX4 (buf + 4);
  376. sec = NULL;
  377. break;
  378. default:
  379. (*_bfd_error_handler)
  380. (_("%B:%u: unrecognized ihex type %u in Intel Hex file"),
  381. abfd, lineno, type);
  382. bfd_set_error (bfd_error_bad_value);
  383. goto error_return;
  384. }
  385. }
  386. }
  387. if (error)
  388. goto error_return;
  389. if (buf != NULL)
  390. free (buf);
  391. return TRUE;
  392. error_return:
  393. if (buf != NULL)
  394. free (buf);
  395. return FALSE;
  396. }
  397. /* Try to recognize an Intel Hex file. */
  398. static const bfd_target *
  399. ihex_object_p (bfd *abfd)
  400. {
  401. void * tdata_save;
  402. bfd_byte b[9];
  403. unsigned int i;
  404. unsigned int type;
  405. ihex_init ();
  406. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  407. return NULL;
  408. if (bfd_bread (b, (bfd_size_type) 9, abfd) != 9)
  409. {
  410. if (bfd_get_error () == bfd_error_file_truncated)
  411. bfd_set_error (bfd_error_wrong_format);
  412. return NULL;
  413. }
  414. if (b[0] != ':')
  415. {
  416. bfd_set_error (bfd_error_wrong_format);
  417. return NULL;
  418. }
  419. for (i = 1; i < 9; i++)
  420. {
  421. if (! ISHEX (b[i]))
  422. {
  423. bfd_set_error (bfd_error_wrong_format);
  424. return NULL;
  425. }
  426. }
  427. type = HEX2 (b + 7);
  428. if (type > 5)
  429. {
  430. bfd_set_error (bfd_error_wrong_format);
  431. return NULL;
  432. }
  433. /* OK, it looks like it really is an Intel Hex file. */
  434. tdata_save = abfd->tdata.any;
  435. if (! ihex_mkobject (abfd) || ! ihex_scan (abfd))
  436. {
  437. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  438. bfd_release (abfd, abfd->tdata.any);
  439. abfd->tdata.any = tdata_save;
  440. return NULL;
  441. }
  442. return abfd->xvec;
  443. }
  444. /* Read the contents of a section in an Intel Hex file. */
  445. static bfd_boolean
  446. ihex_read_section (bfd *abfd, asection *section, bfd_byte *contents)
  447. {
  448. int c;
  449. bfd_byte *p;
  450. bfd_byte *buf = NULL;
  451. size_t bufsize;
  452. bfd_boolean error;
  453. if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
  454. goto error_return;
  455. p = contents;
  456. bufsize = 0;
  457. error = FALSE;
  458. while ((c = ihex_get_byte (abfd, &error)) != EOF)
  459. {
  460. unsigned char hdr[8];
  461. unsigned int len;
  462. unsigned int type;
  463. unsigned int i;
  464. if (c == '\r' || c == '\n')
  465. continue;
  466. /* This is called after ihex_scan has succeeded, so we ought to
  467. know the exact format. */
  468. BFD_ASSERT (c == ':');
  469. if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
  470. goto error_return;
  471. len = HEX2 (hdr);
  472. type = HEX2 (hdr + 6);
  473. /* We should only see type 0 records here. */
  474. if (type != 0)
  475. {
  476. (*_bfd_error_handler)
  477. (_("%B: internal error in ihex_read_section"), abfd);
  478. bfd_set_error (bfd_error_bad_value);
  479. goto error_return;
  480. }
  481. if (len * 2 > bufsize)
  482. {
  483. buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) len * 2);
  484. if (buf == NULL)
  485. goto error_return;
  486. bufsize = len * 2;
  487. }
  488. if (bfd_bread (buf, (bfd_size_type) len * 2, abfd) != len * 2)
  489. goto error_return;
  490. for (i = 0; i < len; i++)
  491. *p++ = HEX2 (buf + 2 * i);
  492. if ((bfd_size_type) (p - contents) >= section->size)
  493. {
  494. /* We've read everything in the section. */
  495. if (buf != NULL)
  496. free (buf);
  497. return TRUE;
  498. }
  499. /* Skip the checksum. */
  500. if (bfd_bread (buf, (bfd_size_type) 2, abfd) != 2)
  501. goto error_return;
  502. }
  503. if ((bfd_size_type) (p - contents) < section->size)
  504. {
  505. (*_bfd_error_handler)
  506. (_("%B: bad section length in ihex_read_section"), abfd);
  507. bfd_set_error (bfd_error_bad_value);
  508. goto error_return;
  509. }
  510. if (buf != NULL)
  511. free (buf);
  512. return TRUE;
  513. error_return:
  514. if (buf != NULL)
  515. free (buf);
  516. return FALSE;
  517. }
  518. /* Get the contents of a section in an Intel Hex file. */
  519. static bfd_boolean
  520. ihex_get_section_contents (bfd *abfd,
  521. asection *section,
  522. void * location,
  523. file_ptr offset,
  524. bfd_size_type count)
  525. {
  526. if (section->used_by_bfd == NULL)
  527. {
  528. section->used_by_bfd = bfd_alloc (abfd, section->size);
  529. if (section->used_by_bfd == NULL)
  530. return FALSE;
  531. if (! ihex_read_section (abfd, section,
  532. (bfd_byte *) section->used_by_bfd))
  533. return FALSE;
  534. }
  535. memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
  536. (size_t) count);
  537. return TRUE;
  538. }
  539. /* Set the contents of a section in an Intel Hex file. */
  540. static bfd_boolean
  541. ihex_set_section_contents (bfd *abfd,
  542. asection *section,
  543. const void * location,
  544. file_ptr offset,
  545. bfd_size_type count)
  546. {
  547. struct ihex_data_list *n;
  548. bfd_byte *data;
  549. struct ihex_data_struct *tdata;
  550. if (count == 0
  551. || (section->flags & SEC_ALLOC) == 0
  552. || (section->flags & SEC_LOAD) == 0)
  553. return TRUE;
  554. n = (struct ihex_data_list *) bfd_alloc (abfd, sizeof (* n));
  555. if (n == NULL)
  556. return FALSE;
  557. data = (bfd_byte *) bfd_alloc (abfd, count);
  558. if (data == NULL)
  559. return FALSE;
  560. memcpy (data, location, (size_t) count);
  561. n->data = data;
  562. n->where = section->lma + offset;
  563. n->size = count;
  564. /* Sort the records by address. Optimize for the common case of
  565. adding a record to the end of the list. */
  566. tdata = abfd->tdata.ihex_data;
  567. if (tdata->tail != NULL
  568. && n->where >= tdata->tail->where)
  569. {
  570. tdata->tail->next = n;
  571. n->next = NULL;
  572. tdata->tail = n;
  573. }
  574. else
  575. {
  576. struct ihex_data_list **pp;
  577. for (pp = &tdata->head;
  578. *pp != NULL && (*pp)->where < n->where;
  579. pp = &(*pp)->next)
  580. ;
  581. n->next = *pp;
  582. *pp = n;
  583. if (n->next == NULL)
  584. tdata->tail = n;
  585. }
  586. return TRUE;
  587. }
  588. /* Write a record out to an Intel Hex file. */
  589. static bfd_boolean
  590. ihex_write_record (bfd *abfd,
  591. size_t count,
  592. unsigned int addr,
  593. unsigned int type,
  594. bfd_byte *data)
  595. {
  596. static const char digs[] = "0123456789ABCDEF";
  597. char buf[9 + CHUNK * 2 + 4];
  598. char *p;
  599. unsigned int chksum;
  600. unsigned int i;
  601. size_t total;
  602. #define TOHEX(buf, v) \
  603. ((buf)[0] = digs[((v) >> 4) & 0xf], (buf)[1] = digs[(v) & 0xf])
  604. buf[0] = ':';
  605. TOHEX (buf + 1, count);
  606. TOHEX (buf + 3, (addr >> 8) & 0xff);
  607. TOHEX (buf + 5, addr & 0xff);
  608. TOHEX (buf + 7, type);
  609. chksum = count + addr + (addr >> 8) + type;
  610. for (i = 0, p = buf + 9; i < count; i++, p += 2, data++)
  611. {
  612. TOHEX (p, *data);
  613. chksum += *data;
  614. }
  615. TOHEX (p, (- chksum) & 0xff);
  616. p[2] = '\r';
  617. p[3] = '\n';
  618. total = 9 + count * 2 + 4;
  619. if (bfd_bwrite (buf, (bfd_size_type) total, abfd) != total)
  620. return FALSE;
  621. return TRUE;
  622. }
  623. /* Write out an Intel Hex file. */
  624. static bfd_boolean
  625. ihex_write_object_contents (bfd *abfd)
  626. {
  627. bfd_vma segbase;
  628. bfd_vma extbase;
  629. struct ihex_data_list *l;
  630. segbase = 0;
  631. extbase = 0;
  632. for (l = abfd->tdata.ihex_data->head; l != NULL; l = l->next)
  633. {
  634. bfd_vma where;
  635. bfd_byte *p;
  636. bfd_size_type count;
  637. where = l->where;
  638. p = l->data;
  639. count = l->size;
  640. while (count > 0)
  641. {
  642. size_t now;
  643. unsigned int rec_addr;
  644. now = count;
  645. if (count > CHUNK)
  646. now = CHUNK;
  647. if (where > segbase + extbase + 0xffff)
  648. {
  649. bfd_byte addr[2];
  650. /* We need a new base address. */
  651. if (where <= 0xfffff)
  652. {
  653. /* The addresses should be sorted. */
  654. BFD_ASSERT (extbase == 0);
  655. segbase = where & 0xf0000;
  656. addr[0] = (bfd_byte)(segbase >> 12) & 0xff;
  657. addr[1] = (bfd_byte)(segbase >> 4) & 0xff;
  658. if (! ihex_write_record (abfd, 2, 0, 2, addr))
  659. return FALSE;
  660. }
  661. else
  662. {
  663. /* The extended address record and the extended
  664. linear address record are combined, at least by
  665. some readers. We need an extended linear address
  666. record here, so if we've already written out an
  667. extended address record, zero it out to avoid
  668. confusion. */
  669. if (segbase != 0)
  670. {
  671. addr[0] = 0;
  672. addr[1] = 0;
  673. if (! ihex_write_record (abfd, 2, 0, 2, addr))
  674. return FALSE;
  675. segbase = 0;
  676. }
  677. extbase = where & 0xffff0000;
  678. if (where > extbase + 0xffff)
  679. {
  680. char buf[20];
  681. sprintf_vma (buf, where);
  682. (*_bfd_error_handler)
  683. (_("%s: address 0x%s out of range for Intel Hex file"),
  684. bfd_get_filename (abfd), buf);
  685. bfd_set_error (bfd_error_bad_value);
  686. return FALSE;
  687. }
  688. addr[0] = (bfd_byte)(extbase >> 24) & 0xff;
  689. addr[1] = (bfd_byte)(extbase >> 16) & 0xff;
  690. if (! ihex_write_record (abfd, 2, 0, 4, addr))
  691. return FALSE;
  692. }
  693. }
  694. rec_addr = where - (extbase + segbase);
  695. /* Output records shouldn't cross 64K boundaries. */
  696. if (rec_addr + now > 0xffff)
  697. now = 0x10000 - rec_addr;
  698. if (! ihex_write_record (abfd, now, rec_addr, 0, p))
  699. return FALSE;
  700. where += now;
  701. p += now;
  702. count -= now;
  703. }
  704. }
  705. if (abfd->start_address != 0)
  706. {
  707. bfd_vma start;
  708. bfd_byte startbuf[4];
  709. start = abfd->start_address;
  710. if (start <= 0xfffff)
  711. {
  712. startbuf[0] = (bfd_byte)((start & 0xf0000) >> 12) & 0xff;
  713. startbuf[1] = 0;
  714. startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
  715. startbuf[3] = (bfd_byte)start & 0xff;
  716. if (! ihex_write_record (abfd, 4, 0, 3, startbuf))
  717. return FALSE;
  718. }
  719. else
  720. {
  721. startbuf[0] = (bfd_byte)(start >> 24) & 0xff;
  722. startbuf[1] = (bfd_byte)(start >> 16) & 0xff;
  723. startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
  724. startbuf[3] = (bfd_byte)start & 0xff;
  725. if (! ihex_write_record (abfd, 4, 0, 5, startbuf))
  726. return FALSE;
  727. }
  728. }
  729. if (! ihex_write_record (abfd, 0, 0, 1, NULL))
  730. return FALSE;
  731. return TRUE;
  732. }
  733. /* Set the architecture for the output file. The architecture is
  734. irrelevant, so we ignore errors about unknown architectures. */
  735. static bfd_boolean
  736. ihex_set_arch_mach (bfd *abfd,
  737. enum bfd_architecture arch,
  738. unsigned long mach)
  739. {
  740. if (! bfd_default_set_arch_mach (abfd, arch, mach))
  741. {
  742. if (arch != bfd_arch_unknown)
  743. return FALSE;
  744. }
  745. return TRUE;
  746. }
  747. /* Get the size of the headers, for the linker. */
  748. static int
  749. ihex_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  750. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  751. {
  752. return 0;
  753. }
  754. /* Some random definitions for the target vector. */
  755. #define ihex_close_and_cleanup _bfd_generic_close_and_cleanup
  756. #define ihex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  757. #define ihex_new_section_hook _bfd_generic_new_section_hook
  758. #define ihex_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  759. #define ihex_get_symtab_upper_bound bfd_0l
  760. #define ihex_canonicalize_symtab ((long (*) (bfd *, asymbol **)) bfd_0l)
  761. #define ihex_make_empty_symbol _bfd_generic_make_empty_symbol
  762. #define ihex_print_symbol _bfd_nosymbols_print_symbol
  763. #define ihex_get_symbol_info _bfd_nosymbols_get_symbol_info
  764. #define ihex_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  765. #define ihex_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  766. #define ihex_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
  767. #define ihex_get_lineno _bfd_nosymbols_get_lineno
  768. #define ihex_find_nearest_line _bfd_nosymbols_find_nearest_line
  769. #define ihex_find_line _bfd_nosymbols_find_line
  770. #define ihex_find_inliner_info _bfd_nosymbols_find_inliner_info
  771. #define ihex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  772. #define ihex_read_minisymbols _bfd_nosymbols_read_minisymbols
  773. #define ihex_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
  774. #define ihex_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  775. #define ihex_bfd_relax_section bfd_generic_relax_section
  776. #define ihex_bfd_gc_sections bfd_generic_gc_sections
  777. #define ihex_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  778. #define ihex_bfd_merge_sections bfd_generic_merge_sections
  779. #define ihex_bfd_is_group_section bfd_generic_is_group_section
  780. #define ihex_bfd_discard_group bfd_generic_discard_group
  781. #define ihex_section_already_linked _bfd_generic_section_already_linked
  782. #define ihex_bfd_define_common_symbol bfd_generic_define_common_symbol
  783. #define ihex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  784. #define ihex_bfd_link_add_symbols _bfd_generic_link_add_symbols
  785. #define ihex_bfd_link_just_syms _bfd_generic_link_just_syms
  786. #define ihex_bfd_copy_link_hash_symbol_type \
  787. _bfd_generic_copy_link_hash_symbol_type
  788. #define ihex_bfd_final_link _bfd_generic_final_link
  789. #define ihex_bfd_link_split_section _bfd_generic_link_split_section
  790. /* The Intel Hex target vector. */
  791. const bfd_target ihex_vec =
  792. {
  793. "ihex", /* Name. */
  794. bfd_target_ihex_flavour,
  795. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  796. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  797. 0, /* Object flags. */
  798. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD), /* Section flags. */
  799. 0, /* Leading underscore. */
  800. ' ', /* AR_pad_char. */
  801. 16, /* AR_max_namelen. */
  802. 0, /* match priority. */
  803. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  804. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  805. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  806. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  807. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  808. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  809. {
  810. _bfd_dummy_target,
  811. ihex_object_p, /* bfd_check_format. */
  812. _bfd_dummy_target,
  813. _bfd_dummy_target,
  814. },
  815. {
  816. bfd_false,
  817. ihex_mkobject,
  818. _bfd_generic_mkarchive,
  819. bfd_false,
  820. },
  821. { /* bfd_write_contents. */
  822. bfd_false,
  823. ihex_write_object_contents,
  824. _bfd_write_archive_contents,
  825. bfd_false,
  826. },
  827. BFD_JUMP_TABLE_GENERIC (ihex),
  828. BFD_JUMP_TABLE_COPY (_bfd_generic),
  829. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  830. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  831. BFD_JUMP_TABLE_SYMBOLS (ihex),
  832. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  833. BFD_JUMP_TABLE_WRITE (ihex),
  834. BFD_JUMP_TABLE_LINK (ihex),
  835. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  836. NULL,
  837. NULL
  838. };