ihex.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  1. /* BFD back-end for Intel Hex objects.
  2. Copyright (C) 1995-2022 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 bool inited;
  131. if (! inited)
  132. {
  133. inited = true;
  134. hex_init ();
  135. }
  136. }
  137. /* Create an ihex object. */
  138. static bool
  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, bool *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, bool 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. /* xgettext:c-format */
  185. (_("%pB:%d: unexpected character `%s' in Intel Hex file"),
  186. abfd, lineno, buf);
  187. bfd_set_error (bfd_error_bad_value);
  188. }
  189. }
  190. /* Read an Intel hex file and turn it into sections. We create a new
  191. section for each contiguous set of bytes. */
  192. static bool
  193. ihex_scan (bfd *abfd)
  194. {
  195. bfd_vma segbase;
  196. bfd_vma extbase;
  197. asection *sec;
  198. unsigned int lineno;
  199. bool error;
  200. bfd_byte *buf = NULL;
  201. size_t bufsize;
  202. int c;
  203. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  204. goto error_return;
  205. abfd->start_address = 0;
  206. segbase = 0;
  207. extbase = 0;
  208. sec = NULL;
  209. lineno = 1;
  210. error = false;
  211. bufsize = 0;
  212. while ((c = ihex_get_byte (abfd, &error)) != EOF)
  213. {
  214. if (c == '\r')
  215. continue;
  216. else if (c == '\n')
  217. {
  218. ++lineno;
  219. continue;
  220. }
  221. else if (c != ':')
  222. {
  223. ihex_bad_byte (abfd, lineno, c, error);
  224. goto error_return;
  225. }
  226. else
  227. {
  228. file_ptr pos;
  229. unsigned char hdr[8];
  230. unsigned int i;
  231. unsigned int len;
  232. bfd_vma addr;
  233. unsigned int type;
  234. unsigned int chars;
  235. unsigned int chksum;
  236. /* This is a data record. */
  237. pos = bfd_tell (abfd) - 1;
  238. /* Read the header bytes. */
  239. if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
  240. goto error_return;
  241. for (i = 0; i < 8; i++)
  242. {
  243. if (! ISHEX (hdr[i]))
  244. {
  245. ihex_bad_byte (abfd, lineno, hdr[i], error);
  246. goto error_return;
  247. }
  248. }
  249. len = HEX2 (hdr);
  250. addr = HEX4 (hdr + 2);
  251. type = HEX2 (hdr + 6);
  252. /* Read the data bytes. */
  253. chars = len * 2 + 2;
  254. if (chars >= bufsize)
  255. {
  256. buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) chars);
  257. if (buf == NULL)
  258. goto error_return;
  259. bufsize = chars;
  260. }
  261. if (bfd_bread (buf, (bfd_size_type) chars, abfd) != chars)
  262. goto error_return;
  263. for (i = 0; i < chars; i++)
  264. {
  265. if (! ISHEX (buf[i]))
  266. {
  267. ihex_bad_byte (abfd, lineno, buf[i], error);
  268. goto error_return;
  269. }
  270. }
  271. /* Check the checksum. */
  272. chksum = len + addr + (addr >> 8) + type;
  273. for (i = 0; i < len; i++)
  274. chksum += HEX2 (buf + 2 * i);
  275. if (((- chksum) & 0xff) != (unsigned int) HEX2 (buf + 2 * i))
  276. {
  277. _bfd_error_handler
  278. /* xgettext:c-format */
  279. (_("%pB:%u: bad checksum in Intel Hex file (expected %u, found %u)"),
  280. abfd, lineno,
  281. (- chksum) & 0xff, (unsigned int) HEX2 (buf + 2 * i));
  282. bfd_set_error (bfd_error_bad_value);
  283. goto error_return;
  284. }
  285. switch (type)
  286. {
  287. case 0:
  288. /* This is a data record. */
  289. if (sec != NULL
  290. && sec->vma + sec->size == extbase + segbase + addr)
  291. {
  292. /* This data goes at the end of the section we are
  293. currently building. */
  294. sec->size += len;
  295. }
  296. else if (len > 0)
  297. {
  298. char secbuf[20];
  299. char *secname;
  300. size_t amt;
  301. flagword flags;
  302. sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
  303. amt = strlen (secbuf) + 1;
  304. secname = (char *) bfd_alloc (abfd, amt);
  305. if (secname == NULL)
  306. goto error_return;
  307. strcpy (secname, secbuf);
  308. flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  309. sec = bfd_make_section_with_flags (abfd, secname, flags);
  310. if (sec == NULL)
  311. goto error_return;
  312. sec->vma = extbase + segbase + addr;
  313. sec->lma = extbase + segbase + addr;
  314. sec->size = len;
  315. sec->filepos = pos;
  316. }
  317. break;
  318. case 1:
  319. /* An end record. */
  320. if (abfd->start_address == 0)
  321. abfd->start_address = addr;
  322. free (buf);
  323. return true;
  324. case 2:
  325. /* An extended address record. */
  326. if (len != 2)
  327. {
  328. _bfd_error_handler
  329. /* xgettext:c-format */
  330. (_("%pB:%u: bad extended address record length in Intel Hex file"),
  331. abfd, lineno);
  332. bfd_set_error (bfd_error_bad_value);
  333. goto error_return;
  334. }
  335. segbase = HEX4 (buf) << 4;
  336. sec = NULL;
  337. break;
  338. case 3:
  339. /* An extended start address record. */
  340. if (len != 4)
  341. {
  342. _bfd_error_handler
  343. /* xgettext:c-format */
  344. (_("%pB:%u: bad extended start address length in Intel Hex file"),
  345. abfd, lineno);
  346. bfd_set_error (bfd_error_bad_value);
  347. goto error_return;
  348. }
  349. abfd->start_address += (HEX4 (buf) << 4) + HEX4 (buf + 4);
  350. sec = NULL;
  351. break;
  352. case 4:
  353. /* An extended linear address record. */
  354. if (len != 2)
  355. {
  356. _bfd_error_handler
  357. /* xgettext:c-format */
  358. (_("%pB:%u: bad extended linear address record length in Intel Hex file"),
  359. abfd, lineno);
  360. bfd_set_error (bfd_error_bad_value);
  361. goto error_return;
  362. }
  363. extbase = HEX4 (buf) << 16;
  364. sec = NULL;
  365. break;
  366. case 5:
  367. /* An extended linear start address record. */
  368. if (len != 2 && len != 4)
  369. {
  370. _bfd_error_handler
  371. /* xgettext:c-format */
  372. (_("%pB:%u: bad extended linear start address length in Intel Hex file"),
  373. abfd, lineno);
  374. bfd_set_error (bfd_error_bad_value);
  375. goto error_return;
  376. }
  377. if (len == 2)
  378. abfd->start_address += HEX4 (buf) << 16;
  379. else
  380. abfd->start_address = (HEX4 (buf) << 16) + HEX4 (buf + 4);
  381. sec = NULL;
  382. break;
  383. default:
  384. _bfd_error_handler
  385. /* xgettext:c-format */
  386. (_("%pB:%u: unrecognized ihex type %u in Intel Hex file"),
  387. abfd, lineno, type);
  388. bfd_set_error (bfd_error_bad_value);
  389. goto error_return;
  390. }
  391. }
  392. }
  393. if (error)
  394. goto error_return;
  395. free (buf);
  396. return true;
  397. error_return:
  398. free (buf);
  399. return false;
  400. }
  401. /* Try to recognize an Intel Hex file. */
  402. static bfd_cleanup
  403. ihex_object_p (bfd *abfd)
  404. {
  405. void * tdata_save;
  406. bfd_byte b[9];
  407. unsigned int i;
  408. unsigned int type;
  409. ihex_init ();
  410. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  411. return NULL;
  412. if (bfd_bread (b, (bfd_size_type) 9, abfd) != 9)
  413. {
  414. if (bfd_get_error () == bfd_error_file_truncated)
  415. bfd_set_error (bfd_error_wrong_format);
  416. return NULL;
  417. }
  418. if (b[0] != ':')
  419. {
  420. bfd_set_error (bfd_error_wrong_format);
  421. return NULL;
  422. }
  423. for (i = 1; i < 9; i++)
  424. {
  425. if (! ISHEX (b[i]))
  426. {
  427. bfd_set_error (bfd_error_wrong_format);
  428. return NULL;
  429. }
  430. }
  431. type = HEX2 (b + 7);
  432. if (type > 5)
  433. {
  434. bfd_set_error (bfd_error_wrong_format);
  435. return NULL;
  436. }
  437. /* OK, it looks like it really is an Intel Hex file. */
  438. tdata_save = abfd->tdata.any;
  439. if (! ihex_mkobject (abfd) || ! ihex_scan (abfd))
  440. {
  441. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  442. bfd_release (abfd, abfd->tdata.any);
  443. abfd->tdata.any = tdata_save;
  444. return NULL;
  445. }
  446. return _bfd_no_cleanup;
  447. }
  448. /* Read the contents of a section in an Intel Hex file. */
  449. static bool
  450. ihex_read_section (bfd *abfd, asection *section, bfd_byte *contents)
  451. {
  452. int c;
  453. bfd_byte *p;
  454. bfd_byte *buf = NULL;
  455. size_t bufsize;
  456. bool error;
  457. if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
  458. goto error_return;
  459. p = contents;
  460. bufsize = 0;
  461. error = false;
  462. while ((c = ihex_get_byte (abfd, &error)) != EOF)
  463. {
  464. unsigned char hdr[8];
  465. unsigned int len;
  466. unsigned int type;
  467. unsigned int i;
  468. if (c == '\r' || c == '\n')
  469. continue;
  470. /* This is called after ihex_scan has succeeded, so we ought to
  471. know the exact format. */
  472. BFD_ASSERT (c == ':');
  473. if (bfd_bread (hdr, (bfd_size_type) 8, abfd) != 8)
  474. goto error_return;
  475. len = HEX2 (hdr);
  476. type = HEX2 (hdr + 6);
  477. /* We should only see type 0 records here. */
  478. if (type != 0)
  479. {
  480. _bfd_error_handler
  481. (_("%pB: internal error in ihex_read_section"), abfd);
  482. bfd_set_error (bfd_error_bad_value);
  483. goto error_return;
  484. }
  485. if (len * 2 > bufsize)
  486. {
  487. buf = (bfd_byte *) bfd_realloc (buf, (bfd_size_type) len * 2);
  488. if (buf == NULL)
  489. goto error_return;
  490. bufsize = len * 2;
  491. }
  492. if (bfd_bread (buf, (bfd_size_type) len * 2, abfd) != len * 2)
  493. goto error_return;
  494. for (i = 0; i < len; i++)
  495. *p++ = HEX2 (buf + 2 * i);
  496. if ((bfd_size_type) (p - contents) >= section->size)
  497. {
  498. /* We've read everything in the section. */
  499. free (buf);
  500. return true;
  501. }
  502. /* Skip the checksum. */
  503. if (bfd_bread (buf, (bfd_size_type) 2, abfd) != 2)
  504. goto error_return;
  505. }
  506. if ((bfd_size_type) (p - contents) < section->size)
  507. {
  508. _bfd_error_handler
  509. (_("%pB: bad section length in ihex_read_section"), abfd);
  510. bfd_set_error (bfd_error_bad_value);
  511. goto error_return;
  512. }
  513. free (buf);
  514. return true;
  515. error_return:
  516. free (buf);
  517. return false;
  518. }
  519. /* Get the contents of a section in an Intel Hex file. */
  520. static bool
  521. ihex_get_section_contents (bfd *abfd,
  522. asection *section,
  523. void * location,
  524. file_ptr offset,
  525. bfd_size_type count)
  526. {
  527. if (section->used_by_bfd == NULL)
  528. {
  529. section->used_by_bfd = bfd_alloc (abfd, section->size);
  530. if (section->used_by_bfd == NULL)
  531. return false;
  532. if (! ihex_read_section (abfd, section,
  533. (bfd_byte *) section->used_by_bfd))
  534. return false;
  535. }
  536. memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
  537. (size_t) count);
  538. return true;
  539. }
  540. /* Set the contents of a section in an Intel Hex file. */
  541. static bool
  542. ihex_set_section_contents (bfd *abfd,
  543. asection *section,
  544. const void * location,
  545. file_ptr offset,
  546. bfd_size_type count)
  547. {
  548. struct ihex_data_list *n;
  549. bfd_byte *data;
  550. struct ihex_data_struct *tdata;
  551. if (count == 0
  552. || (section->flags & SEC_ALLOC) == 0
  553. || (section->flags & SEC_LOAD) == 0)
  554. return true;
  555. n = (struct ihex_data_list *) bfd_alloc (abfd, sizeof (* n));
  556. if (n == NULL)
  557. return false;
  558. data = (bfd_byte *) bfd_alloc (abfd, count);
  559. if (data == NULL)
  560. return false;
  561. memcpy (data, location, (size_t) count);
  562. n->data = data;
  563. n->where = section->lma + offset;
  564. n->size = count;
  565. /* Sort the records by address. Optimize for the common case of
  566. adding a record to the end of the list. */
  567. tdata = abfd->tdata.ihex_data;
  568. if (tdata->tail != NULL
  569. && n->where >= tdata->tail->where)
  570. {
  571. tdata->tail->next = n;
  572. n->next = NULL;
  573. tdata->tail = n;
  574. }
  575. else
  576. {
  577. struct ihex_data_list **pp;
  578. for (pp = &tdata->head;
  579. *pp != NULL && (*pp)->where < n->where;
  580. pp = &(*pp)->next)
  581. ;
  582. n->next = *pp;
  583. *pp = n;
  584. if (n->next == NULL)
  585. tdata->tail = n;
  586. }
  587. return true;
  588. }
  589. /* Write a record out to an Intel Hex file. */
  590. static bool
  591. ihex_write_record (bfd *abfd,
  592. size_t count,
  593. unsigned int addr,
  594. unsigned int type,
  595. bfd_byte *data)
  596. {
  597. static const char digs[] = "0123456789ABCDEF";
  598. char buf[9 + CHUNK * 2 + 4];
  599. char *p;
  600. unsigned int chksum;
  601. unsigned int i;
  602. size_t total;
  603. #define TOHEX(buf, v) \
  604. ((buf)[0] = digs[((v) >> 4) & 0xf], (buf)[1] = digs[(v) & 0xf])
  605. buf[0] = ':';
  606. TOHEX (buf + 1, count);
  607. TOHEX (buf + 3, (addr >> 8) & 0xff);
  608. TOHEX (buf + 5, addr & 0xff);
  609. TOHEX (buf + 7, type);
  610. chksum = count + addr + (addr >> 8) + type;
  611. for (i = 0, p = buf + 9; i < count; i++, p += 2, data++)
  612. {
  613. TOHEX (p, *data);
  614. chksum += *data;
  615. }
  616. TOHEX (p, (- chksum) & 0xff);
  617. p[2] = '\r';
  618. p[3] = '\n';
  619. total = 9 + count * 2 + 4;
  620. if (bfd_bwrite (buf, (bfd_size_type) total, abfd) != total)
  621. return false;
  622. return true;
  623. }
  624. /* Write out an Intel Hex file. */
  625. static bool
  626. ihex_write_object_contents (bfd *abfd)
  627. {
  628. bfd_vma segbase;
  629. bfd_vma extbase;
  630. struct ihex_data_list *l;
  631. segbase = 0;
  632. extbase = 0;
  633. for (l = abfd->tdata.ihex_data->head; l != NULL; l = l->next)
  634. {
  635. bfd_vma where;
  636. bfd_byte *p;
  637. bfd_size_type count;
  638. where = l->where;
  639. #ifdef BFD64
  640. /* IHex only supports 32-bit addresses, and we want to check
  641. that 64-bit addresses are in range. This isn't quite as
  642. obvious as it may seem, since some targets have 32-bit
  643. addresses that are sign extended to 64 bits. So complain
  644. only if addresses overflow both unsigned and signed 32-bit
  645. integers. */
  646. if (where > 0xffffffff
  647. && where + 0x80000000 > 0xffffffff)
  648. {
  649. _bfd_error_handler
  650. /* xgettext:c-format */
  651. (_("%pB 64-bit address %#" PRIx64
  652. " out of range for Intel Hex file"),
  653. abfd, (uint64_t) where);
  654. bfd_set_error (bfd_error_bad_value);
  655. return false;
  656. }
  657. where &= 0xffffffff;
  658. #endif
  659. p = l->data;
  660. count = l->size;
  661. while (count > 0)
  662. {
  663. size_t now;
  664. unsigned int rec_addr;
  665. now = count;
  666. if (count > CHUNK)
  667. now = CHUNK;
  668. if (where < extbase
  669. || where - extbase < segbase
  670. || where - extbase - segbase > 0xffff)
  671. {
  672. bfd_byte addr[2];
  673. /* We need a new base address. */
  674. if (extbase == 0 && where <= 0xfffff)
  675. {
  676. segbase = where & 0xf0000;
  677. addr[0] = (bfd_byte)(segbase >> 12) & 0xff;
  678. addr[1] = (bfd_byte)(segbase >> 4) & 0xff;
  679. if (! ihex_write_record (abfd, 2, 0, 2, addr))
  680. return false;
  681. }
  682. else
  683. {
  684. /* The extended address record and the extended
  685. linear address record are combined, at least by
  686. some readers. We need an extended linear address
  687. record here, so if we've already written out an
  688. extended address record, zero it out to avoid
  689. confusion. */
  690. if (segbase != 0)
  691. {
  692. addr[0] = 0;
  693. addr[1] = 0;
  694. if (! ihex_write_record (abfd, 2, 0, 2, addr))
  695. return false;
  696. segbase = 0;
  697. }
  698. extbase = where & 0xffff0000;
  699. if (where > extbase + 0xffff)
  700. {
  701. _bfd_error_handler
  702. /* xgettext:c-format */
  703. (_("%pB: address %#" PRIx64
  704. " out of range for Intel Hex file"),
  705. abfd, (uint64_t) where);
  706. bfd_set_error (bfd_error_bad_value);
  707. return false;
  708. }
  709. addr[0] = (bfd_byte)(extbase >> 24) & 0xff;
  710. addr[1] = (bfd_byte)(extbase >> 16) & 0xff;
  711. if (! ihex_write_record (abfd, 2, 0, 4, addr))
  712. return false;
  713. }
  714. }
  715. rec_addr = where - (extbase + segbase);
  716. /* Output records shouldn't cross 64K boundaries. */
  717. if (rec_addr + now > 0xffff)
  718. now = 0x10000 - rec_addr;
  719. if (! ihex_write_record (abfd, now, rec_addr, 0, p))
  720. return false;
  721. where += now;
  722. p += now;
  723. count -= now;
  724. }
  725. }
  726. if (abfd->start_address != 0)
  727. {
  728. bfd_vma start;
  729. bfd_byte startbuf[4];
  730. start = abfd->start_address;
  731. if (start <= 0xfffff)
  732. {
  733. startbuf[0] = (bfd_byte)((start & 0xf0000) >> 12) & 0xff;
  734. startbuf[1] = 0;
  735. startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
  736. startbuf[3] = (bfd_byte)start & 0xff;
  737. if (! ihex_write_record (abfd, 4, 0, 3, startbuf))
  738. return false;
  739. }
  740. else
  741. {
  742. startbuf[0] = (bfd_byte)(start >> 24) & 0xff;
  743. startbuf[1] = (bfd_byte)(start >> 16) & 0xff;
  744. startbuf[2] = (bfd_byte)(start >> 8) & 0xff;
  745. startbuf[3] = (bfd_byte)start & 0xff;
  746. if (! ihex_write_record (abfd, 4, 0, 5, startbuf))
  747. return false;
  748. }
  749. }
  750. if (! ihex_write_record (abfd, 0, 0, 1, NULL))
  751. return false;
  752. return true;
  753. }
  754. /* Set the architecture for the output file. The architecture is
  755. irrelevant, so we ignore errors about unknown architectures. */
  756. static bool
  757. ihex_set_arch_mach (bfd *abfd,
  758. enum bfd_architecture arch,
  759. unsigned long mach)
  760. {
  761. if (! bfd_default_set_arch_mach (abfd, arch, mach))
  762. {
  763. if (arch != bfd_arch_unknown)
  764. return false;
  765. }
  766. return true;
  767. }
  768. /* Get the size of the headers, for the linker. */
  769. static int
  770. ihex_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  771. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  772. {
  773. return 0;
  774. }
  775. /* Some random definitions for the target vector. */
  776. #define ihex_close_and_cleanup _bfd_generic_close_and_cleanup
  777. #define ihex_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  778. #define ihex_new_section_hook _bfd_generic_new_section_hook
  779. #define ihex_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  780. #define ihex_get_symtab_upper_bound _bfd_long_bfd_0
  781. #define ihex_canonicalize_symtab _bfd_nosymbols_canonicalize_symtab
  782. #define ihex_make_empty_symbol _bfd_generic_make_empty_symbol
  783. #define ihex_print_symbol _bfd_nosymbols_print_symbol
  784. #define ihex_get_symbol_info _bfd_nosymbols_get_symbol_info
  785. #define ihex_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  786. #define ihex_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  787. #define ihex_bfd_is_local_label_name _bfd_nosymbols_bfd_is_local_label_name
  788. #define ihex_get_lineno _bfd_nosymbols_get_lineno
  789. #define ihex_find_nearest_line _bfd_nosymbols_find_nearest_line
  790. #define ihex_find_line _bfd_nosymbols_find_line
  791. #define ihex_find_inliner_info _bfd_nosymbols_find_inliner_info
  792. #define ihex_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  793. #define ihex_read_minisymbols _bfd_nosymbols_read_minisymbols
  794. #define ihex_minisymbol_to_symbol _bfd_nosymbols_minisymbol_to_symbol
  795. #define ihex_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  796. #define ihex_bfd_relax_section bfd_generic_relax_section
  797. #define ihex_bfd_gc_sections bfd_generic_gc_sections
  798. #define ihex_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  799. #define ihex_bfd_merge_sections bfd_generic_merge_sections
  800. #define ihex_bfd_is_group_section bfd_generic_is_group_section
  801. #define ihex_bfd_group_name bfd_generic_group_name
  802. #define ihex_bfd_discard_group bfd_generic_discard_group
  803. #define ihex_section_already_linked _bfd_generic_section_already_linked
  804. #define ihex_bfd_define_common_symbol bfd_generic_define_common_symbol
  805. #define ihex_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  806. #define ihex_bfd_define_start_stop bfd_generic_define_start_stop
  807. #define ihex_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  808. #define ihex_bfd_link_add_symbols _bfd_generic_link_add_symbols
  809. #define ihex_bfd_link_just_syms _bfd_generic_link_just_syms
  810. #define ihex_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
  811. #define ihex_bfd_final_link _bfd_generic_final_link
  812. #define ihex_bfd_link_split_section _bfd_generic_link_split_section
  813. #define ihex_bfd_link_check_relocs _bfd_generic_link_check_relocs
  814. /* The Intel Hex target vector. */
  815. const bfd_target ihex_vec =
  816. {
  817. "ihex", /* Name. */
  818. bfd_target_ihex_flavour,
  819. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  820. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  821. 0, /* Object flags. */
  822. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD), /* Section flags. */
  823. 0, /* Leading underscore. */
  824. ' ', /* AR_pad_char. */
  825. 16, /* AR_max_namelen. */
  826. 0, /* match priority. */
  827. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  828. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  829. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  830. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  831. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  832. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  833. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  834. {
  835. _bfd_dummy_target,
  836. ihex_object_p, /* bfd_check_format. */
  837. _bfd_dummy_target,
  838. _bfd_dummy_target,
  839. },
  840. {
  841. _bfd_bool_bfd_false_error,
  842. ihex_mkobject,
  843. _bfd_generic_mkarchive,
  844. _bfd_bool_bfd_false_error,
  845. },
  846. { /* bfd_write_contents. */
  847. _bfd_bool_bfd_false_error,
  848. ihex_write_object_contents,
  849. _bfd_write_archive_contents,
  850. _bfd_bool_bfd_false_error,
  851. },
  852. BFD_JUMP_TABLE_GENERIC (ihex),
  853. BFD_JUMP_TABLE_COPY (_bfd_generic),
  854. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  855. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  856. BFD_JUMP_TABLE_SYMBOLS (ihex),
  857. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  858. BFD_JUMP_TABLE_WRITE (ihex),
  859. BFD_JUMP_TABLE_LINK (ihex),
  860. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  861. NULL,
  862. NULL
  863. };