srec.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /* BFD back-end for s-record objects.
  2. Copyright (C) 1990-2022 Free Software Foundation, Inc.
  3. Written by Steve Chamberlain of Cygnus Support <sac@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. /* SUBSECTION
  18. S-Record handling
  19. DESCRIPTION
  20. Ordinary S-Records cannot hold anything but addresses and
  21. data, so that's all that we implement.
  22. The only interesting thing is that S-Records may come out of
  23. order and there is no header, so an initial scan is required
  24. to discover the minimum and maximum addresses used to create
  25. the vma and size of the only section we create. We
  26. arbitrarily call this section ".text".
  27. When bfd_get_section_contents is called the file is read
  28. again, and this time the data is placed into a bfd_alloc'd
  29. area.
  30. Any number of sections may be created for output, we save them
  31. up and output them when it's time to close the bfd.
  32. An s record looks like:
  33. EXAMPLE
  34. S<type><length><address><data><checksum>
  35. DESCRIPTION
  36. Where
  37. o length
  38. is the number of bytes following upto the checksum. Note that
  39. this is not the number of chars following, since it takes two
  40. chars to represent a byte.
  41. o type
  42. is one of:
  43. 0) header record
  44. 1) two byte address data record
  45. 2) three byte address data record
  46. 3) four byte address data record
  47. 7) four byte address termination record
  48. 8) three byte address termination record
  49. 9) two byte address termination record
  50. o address
  51. is the start address of the data following, or in the case of
  52. a termination record, the start address of the image
  53. o data
  54. is the data.
  55. o checksum
  56. is the sum of all the raw byte data in the record, from the length
  57. upwards, modulo 256 and subtracted from 255.
  58. SUBSECTION
  59. Symbol S-Record handling
  60. DESCRIPTION
  61. Some ICE equipment understands an addition to the standard
  62. S-Record format; symbols and their addresses can be sent
  63. before the data.
  64. The format of this is:
  65. ($$ <modulename>
  66. (<space> <symbol> <address>)*)
  67. $$
  68. so a short symbol table could look like:
  69. EXAMPLE
  70. $$ flash.x
  71. $$ flash.c
  72. _port6 $0
  73. _delay $4
  74. _start $14
  75. _etext $8036
  76. _edata $8036
  77. _end $8036
  78. $$
  79. DESCRIPTION
  80. We allow symbols to be anywhere in the data stream - the module names
  81. are always ignored. */
  82. #include "sysdep.h"
  83. #include "bfd.h"
  84. #include "libbfd.h"
  85. #include "libiberty.h"
  86. #include "safe-ctype.h"
  87. /* Macros for converting between hex and binary. */
  88. static const char digs[] = "0123456789ABCDEF";
  89. #define NIBBLE(x) hex_value(x)
  90. #define HEX(buffer) ((NIBBLE ((buffer)[0])<<4) + NIBBLE ((buffer)[1]))
  91. #define TOHEX(d, x, ch) \
  92. d[1] = digs[(x) & 0xf]; \
  93. d[0] = digs[((x)>>4)&0xf]; \
  94. ch += ((x) & 0xff);
  95. #define ISHEX(x) hex_p(x)
  96. /* The maximum number of address+data+crc bytes on a line is FF. */
  97. #define MAXCHUNK 0xff
  98. /* Default size for a CHUNK. */
  99. #define DEFAULT_CHUNK 16
  100. /* The number of data bytes we actually fit onto a line on output.
  101. This variable can be modified by objcopy's --srec-len parameter.
  102. For a 0x75 byte record you should set --srec-len=0x70. */
  103. unsigned int _bfd_srec_len = DEFAULT_CHUNK;
  104. /* The type of srec output (free or forced to S3).
  105. This variable can be modified by objcopy's --srec-forceS3
  106. parameter. */
  107. bool _bfd_srec_forceS3 = false;
  108. /* When writing an S-record file, the S-records can not be output as
  109. they are seen. This structure is used to hold them in memory. */
  110. struct srec_data_list_struct
  111. {
  112. struct srec_data_list_struct *next;
  113. bfd_byte *data;
  114. bfd_vma where;
  115. bfd_size_type size;
  116. };
  117. typedef struct srec_data_list_struct srec_data_list_type;
  118. /* When scanning the S-record file, a linked list of srec_symbol
  119. structures is built to represent the symbol table (if there is
  120. one). */
  121. struct srec_symbol
  122. {
  123. struct srec_symbol *next;
  124. const char *name;
  125. bfd_vma val;
  126. };
  127. /* The S-record tdata information. */
  128. typedef struct srec_data_struct
  129. {
  130. srec_data_list_type *head;
  131. srec_data_list_type *tail;
  132. unsigned int type;
  133. struct srec_symbol *symbols;
  134. struct srec_symbol *symtail;
  135. asymbol *csymbols;
  136. }
  137. tdata_type;
  138. /* Initialize by filling in the hex conversion array. */
  139. static void
  140. srec_init (void)
  141. {
  142. static bool inited = false;
  143. if (! inited)
  144. {
  145. inited = true;
  146. hex_init ();
  147. }
  148. }
  149. /* Set up the S-record tdata information. */
  150. static bool
  151. srec_mkobject (bfd *abfd)
  152. {
  153. tdata_type *tdata;
  154. srec_init ();
  155. tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  156. if (tdata == NULL)
  157. return false;
  158. abfd->tdata.srec_data = tdata;
  159. tdata->type = 1;
  160. tdata->head = NULL;
  161. tdata->tail = NULL;
  162. tdata->symbols = NULL;
  163. tdata->symtail = NULL;
  164. tdata->csymbols = NULL;
  165. return true;
  166. }
  167. /* Read a byte from an S record file. Set *ERRORPTR if an error
  168. occurred. Return EOF on error or end of file. */
  169. static int
  170. srec_get_byte (bfd *abfd, bool *errorptr)
  171. {
  172. bfd_byte c;
  173. if (bfd_bread (&c, (bfd_size_type) 1, abfd) != 1)
  174. {
  175. if (bfd_get_error () != bfd_error_file_truncated)
  176. *errorptr = true;
  177. return EOF;
  178. }
  179. return (int) (c & 0xff);
  180. }
  181. /* Report a problem in an S record file. FIXME: This probably should
  182. not call fprintf, but we really do need some mechanism for printing
  183. error messages. */
  184. static void
  185. srec_bad_byte (bfd *abfd,
  186. unsigned int lineno,
  187. int c,
  188. bool error)
  189. {
  190. if (c == EOF)
  191. {
  192. if (! error)
  193. bfd_set_error (bfd_error_file_truncated);
  194. }
  195. else
  196. {
  197. char buf[40];
  198. if (! ISPRINT (c))
  199. sprintf (buf, "\\%03o", (unsigned int) c & 0xff);
  200. else
  201. {
  202. buf[0] = c;
  203. buf[1] = '\0';
  204. }
  205. _bfd_error_handler
  206. /* xgettext:c-format */
  207. (_("%pB:%d: unexpected character `%s' in S-record file"),
  208. abfd, lineno, buf);
  209. bfd_set_error (bfd_error_bad_value);
  210. }
  211. }
  212. /* Add a new symbol found in an S-record file. */
  213. static bool
  214. srec_new_symbol (bfd *abfd, const char *name, bfd_vma val)
  215. {
  216. struct srec_symbol *n;
  217. n = (struct srec_symbol *) bfd_alloc (abfd, sizeof (* n));
  218. if (n == NULL)
  219. return false;
  220. n->name = name;
  221. n->val = val;
  222. if (abfd->tdata.srec_data->symbols == NULL)
  223. abfd->tdata.srec_data->symbols = n;
  224. else
  225. abfd->tdata.srec_data->symtail->next = n;
  226. abfd->tdata.srec_data->symtail = n;
  227. n->next = NULL;
  228. ++abfd->symcount;
  229. return true;
  230. }
  231. /* Read the S record file and turn it into sections. We create a new
  232. section for each contiguous set of bytes. */
  233. static bool
  234. srec_scan (bfd *abfd)
  235. {
  236. int c;
  237. unsigned int lineno = 1;
  238. bool error = false;
  239. bfd_byte *buf = NULL;
  240. size_t bufsize = 0;
  241. asection *sec = NULL;
  242. char *symbuf = NULL;
  243. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  244. goto error_return;
  245. while ((c = srec_get_byte (abfd, &error)) != EOF)
  246. {
  247. /* We only build sections from contiguous S-records, so if this
  248. is not an S-record, then stop building a section. */
  249. if (c != 'S' && c != '\r' && c != '\n')
  250. sec = NULL;
  251. switch (c)
  252. {
  253. default:
  254. srec_bad_byte (abfd, lineno, c, error);
  255. goto error_return;
  256. case '\n':
  257. ++lineno;
  258. break;
  259. case '\r':
  260. break;
  261. case '$':
  262. /* Starting a module name, which we ignore. */
  263. while ((c = srec_get_byte (abfd, &error)) != '\n'
  264. && c != EOF)
  265. ;
  266. if (c == EOF)
  267. {
  268. srec_bad_byte (abfd, lineno, c, error);
  269. goto error_return;
  270. }
  271. ++lineno;
  272. break;
  273. case ' ':
  274. do
  275. {
  276. bfd_size_type alc;
  277. char *p, *symname;
  278. bfd_vma symval;
  279. /* Starting a symbol definition. */
  280. while ((c = srec_get_byte (abfd, &error)) != EOF
  281. && (c == ' ' || c == '\t'))
  282. ;
  283. if (c == '\n' || c == '\r')
  284. break;
  285. if (c == EOF)
  286. {
  287. srec_bad_byte (abfd, lineno, c, error);
  288. goto error_return;
  289. }
  290. alc = 10;
  291. symbuf = (char *) bfd_malloc (alc + 1);
  292. if (symbuf == NULL)
  293. goto error_return;
  294. p = symbuf;
  295. *p++ = c;
  296. while ((c = srec_get_byte (abfd, &error)) != EOF
  297. && ! ISSPACE (c))
  298. {
  299. if ((bfd_size_type) (p - symbuf) >= alc)
  300. {
  301. char *n;
  302. alc *= 2;
  303. n = (char *) bfd_realloc (symbuf, alc + 1);
  304. if (n == NULL)
  305. goto error_return;
  306. p = n + (p - symbuf);
  307. symbuf = n;
  308. }
  309. *p++ = c;
  310. }
  311. if (c == EOF)
  312. {
  313. srec_bad_byte (abfd, lineno, c, error);
  314. goto error_return;
  315. }
  316. *p++ = '\0';
  317. symname = (char *) bfd_alloc (abfd, (bfd_size_type) (p - symbuf));
  318. if (symname == NULL)
  319. goto error_return;
  320. strcpy (symname, symbuf);
  321. free (symbuf);
  322. symbuf = NULL;
  323. while ((c = srec_get_byte (abfd, &error)) != EOF
  324. && (c == ' ' || c == '\t'))
  325. ;
  326. if (c == EOF)
  327. {
  328. srec_bad_byte (abfd, lineno, c, error);
  329. goto error_return;
  330. }
  331. /* Skip a dollar sign before the hex value. */
  332. if (c == '$')
  333. {
  334. c = srec_get_byte (abfd, &error);
  335. if (c == EOF)
  336. {
  337. srec_bad_byte (abfd, lineno, c, error);
  338. goto error_return;
  339. }
  340. }
  341. symval = 0;
  342. while (ISHEX (c))
  343. {
  344. symval <<= 4;
  345. symval += NIBBLE (c);
  346. c = srec_get_byte (abfd, &error);
  347. if (c == EOF)
  348. {
  349. srec_bad_byte (abfd, lineno, c, error);
  350. goto error_return;
  351. }
  352. }
  353. if (! srec_new_symbol (abfd, symname, symval))
  354. goto error_return;
  355. }
  356. while (c == ' ' || c == '\t')
  357. ;
  358. if (c == '\n')
  359. ++lineno;
  360. else if (c != '\r')
  361. {
  362. srec_bad_byte (abfd, lineno, c, error);
  363. goto error_return;
  364. }
  365. break;
  366. case 'S':
  367. {
  368. file_ptr pos;
  369. unsigned char hdr[3];
  370. unsigned int bytes, min_bytes;
  371. bfd_vma address;
  372. bfd_byte *data;
  373. unsigned char check_sum;
  374. /* Starting an S-record. */
  375. pos = bfd_tell (abfd) - 1;
  376. if (bfd_bread (hdr, (bfd_size_type) 3, abfd) != 3)
  377. goto error_return;
  378. if (! ISHEX (hdr[1]) || ! ISHEX (hdr[2]))
  379. {
  380. if (! ISHEX (hdr[1]))
  381. c = hdr[1];
  382. else
  383. c = hdr[2];
  384. srec_bad_byte (abfd, lineno, c, error);
  385. goto error_return;
  386. }
  387. check_sum = bytes = HEX (hdr + 1);
  388. min_bytes = 3;
  389. if (hdr[0] == '2' || hdr[0] == '8')
  390. min_bytes = 4;
  391. else if (hdr[0] == '3' || hdr[0] == '7')
  392. min_bytes = 5;
  393. if (bytes < min_bytes)
  394. {
  395. /* xgettext:c-format */
  396. _bfd_error_handler (_("%pB:%d: byte count %d too small"),
  397. abfd, lineno, bytes);
  398. bfd_set_error (bfd_error_bad_value);
  399. goto error_return;
  400. }
  401. if (bytes * 2 > bufsize)
  402. {
  403. free (buf);
  404. buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
  405. if (buf == NULL)
  406. goto error_return;
  407. bufsize = bytes * 2;
  408. }
  409. if (bfd_bread (buf, (bfd_size_type) bytes * 2, abfd) != bytes * 2)
  410. goto error_return;
  411. /* Ignore the checksum byte. */
  412. --bytes;
  413. address = 0;
  414. data = buf;
  415. switch (hdr[0])
  416. {
  417. case '0':
  418. case '5':
  419. /* Prologue--ignore the file name, but stop building a
  420. section at this point. */
  421. sec = NULL;
  422. break;
  423. case '3':
  424. check_sum += HEX (data);
  425. address = HEX (data);
  426. data += 2;
  427. --bytes;
  428. /* Fall through. */
  429. case '2':
  430. check_sum += HEX (data);
  431. address = (address << 8) | HEX (data);
  432. data += 2;
  433. --bytes;
  434. /* Fall through. */
  435. case '1':
  436. check_sum += HEX (data);
  437. address = (address << 8) | HEX (data);
  438. data += 2;
  439. check_sum += HEX (data);
  440. address = (address << 8) | HEX (data);
  441. data += 2;
  442. bytes -= 2;
  443. if (sec != NULL
  444. && sec->vma + sec->size == address)
  445. {
  446. /* This data goes at the end of the section we are
  447. currently building. */
  448. sec->size += bytes;
  449. }
  450. else
  451. {
  452. char secbuf[20];
  453. char *secname;
  454. size_t amt;
  455. flagword flags;
  456. sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
  457. amt = strlen (secbuf) + 1;
  458. secname = (char *) bfd_alloc (abfd, amt);
  459. strcpy (secname, secbuf);
  460. flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  461. sec = bfd_make_section_with_flags (abfd, secname, flags);
  462. if (sec == NULL)
  463. goto error_return;
  464. sec->vma = address;
  465. sec->lma = address;
  466. sec->size = bytes;
  467. sec->filepos = pos;
  468. }
  469. while (bytes > 0)
  470. {
  471. check_sum += HEX (data);
  472. data += 2;
  473. bytes--;
  474. }
  475. check_sum = 255 - (check_sum & 0xff);
  476. if (check_sum != HEX (data))
  477. {
  478. _bfd_error_handler
  479. /* xgettext:c-format */
  480. (_("%pB:%d: bad checksum in S-record file"),
  481. abfd, lineno);
  482. bfd_set_error (bfd_error_bad_value);
  483. goto error_return;
  484. }
  485. break;
  486. case '7':
  487. check_sum += HEX (data);
  488. address = HEX (data);
  489. data += 2;
  490. /* Fall through. */
  491. case '8':
  492. check_sum += HEX (data);
  493. address = (address << 8) | HEX (data);
  494. data += 2;
  495. /* Fall through. */
  496. case '9':
  497. check_sum += HEX (data);
  498. address = (address << 8) | HEX (data);
  499. data += 2;
  500. check_sum += HEX (data);
  501. address = (address << 8) | HEX (data);
  502. data += 2;
  503. /* This is a termination record. */
  504. abfd->start_address = address;
  505. check_sum = 255 - (check_sum & 0xff);
  506. if (check_sum != HEX (data))
  507. {
  508. _bfd_error_handler
  509. /* xgettext:c-format */
  510. (_("%pB:%d: bad checksum in S-record file"),
  511. abfd, lineno);
  512. bfd_set_error (bfd_error_bad_value);
  513. goto error_return;
  514. }
  515. free (buf);
  516. return true;
  517. }
  518. }
  519. break;
  520. }
  521. }
  522. if (error)
  523. goto error_return;
  524. free (buf);
  525. return true;
  526. error_return:
  527. free (symbuf);
  528. free (buf);
  529. return false;
  530. }
  531. /* Check whether an existing file is an S-record file. */
  532. static bfd_cleanup
  533. srec_object_p (bfd *abfd)
  534. {
  535. void * tdata_save;
  536. bfd_byte b[4];
  537. srec_init ();
  538. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  539. || bfd_bread (b, (bfd_size_type) 4, abfd) != 4)
  540. return NULL;
  541. if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
  542. {
  543. bfd_set_error (bfd_error_wrong_format);
  544. return NULL;
  545. }
  546. tdata_save = abfd->tdata.any;
  547. if (! srec_mkobject (abfd) || ! srec_scan (abfd))
  548. {
  549. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  550. bfd_release (abfd, abfd->tdata.any);
  551. abfd->tdata.any = tdata_save;
  552. return NULL;
  553. }
  554. if (abfd->symcount > 0)
  555. abfd->flags |= HAS_SYMS;
  556. return _bfd_no_cleanup;
  557. }
  558. /* Check whether an existing file is an S-record file with symbols. */
  559. static bfd_cleanup
  560. symbolsrec_object_p (bfd *abfd)
  561. {
  562. void * tdata_save;
  563. char b[2];
  564. srec_init ();
  565. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  566. || bfd_bread (b, (bfd_size_type) 2, abfd) != 2)
  567. return NULL;
  568. if (b[0] != '$' || b[1] != '$')
  569. {
  570. bfd_set_error (bfd_error_wrong_format);
  571. return NULL;
  572. }
  573. tdata_save = abfd->tdata.any;
  574. if (! srec_mkobject (abfd) || ! srec_scan (abfd))
  575. {
  576. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  577. bfd_release (abfd, abfd->tdata.any);
  578. abfd->tdata.any = tdata_save;
  579. return NULL;
  580. }
  581. if (abfd->symcount > 0)
  582. abfd->flags |= HAS_SYMS;
  583. return _bfd_no_cleanup;
  584. }
  585. /* Read in the contents of a section in an S-record file. */
  586. static bool
  587. srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
  588. {
  589. int c;
  590. bfd_size_type sofar = 0;
  591. bool error = false;
  592. bfd_byte *buf = NULL;
  593. size_t bufsize = 0;
  594. if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
  595. goto error_return;
  596. while ((c = srec_get_byte (abfd, &error)) != EOF)
  597. {
  598. bfd_byte hdr[3];
  599. unsigned int bytes;
  600. bfd_vma address;
  601. bfd_byte *data;
  602. if (c == '\r' || c == '\n')
  603. continue;
  604. /* This is called after srec_scan has already been called, so we
  605. ought to know the exact format. */
  606. if (c != 'S')
  607. goto error_return;
  608. if (bfd_bread (hdr, (bfd_size_type) 3, abfd) != 3)
  609. goto error_return;
  610. BFD_ASSERT (ISHEX (hdr[1]) && ISHEX (hdr[2]));
  611. bytes = HEX (hdr + 1);
  612. if (bytes * 2 > bufsize)
  613. {
  614. free (buf);
  615. buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
  616. if (buf == NULL)
  617. goto error_return;
  618. bufsize = bytes * 2;
  619. }
  620. if (bfd_bread (buf, (bfd_size_type) bytes * 2, abfd) != bytes * 2)
  621. goto error_return;
  622. address = 0;
  623. data = buf;
  624. switch (hdr[0])
  625. {
  626. default:
  627. if (sofar != section->size)
  628. goto error_return;
  629. free (buf);
  630. return true;
  631. case '3':
  632. address = HEX (data);
  633. data += 2;
  634. --bytes;
  635. /* Fall through. */
  636. case '2':
  637. address = (address << 8) | HEX (data);
  638. data += 2;
  639. --bytes;
  640. /* Fall through. */
  641. case '1':
  642. address = (address << 8) | HEX (data);
  643. data += 2;
  644. address = (address << 8) | HEX (data);
  645. data += 2;
  646. bytes -= 2;
  647. if (address != section->vma + sofar)
  648. {
  649. /* We've come to the end of this section. */
  650. if (sofar != section->size)
  651. goto error_return;
  652. free (buf);
  653. return true;
  654. }
  655. /* Don't consider checksum. */
  656. --bytes;
  657. while (bytes-- != 0)
  658. {
  659. contents[sofar] = HEX (data);
  660. data += 2;
  661. ++sofar;
  662. }
  663. break;
  664. }
  665. }
  666. if (error)
  667. goto error_return;
  668. if (sofar != section->size)
  669. goto error_return;
  670. free (buf);
  671. return true;
  672. error_return:
  673. free (buf);
  674. return false;
  675. }
  676. /* Get the contents of a section in an S-record file. */
  677. static bool
  678. srec_get_section_contents (bfd *abfd,
  679. asection *section,
  680. void * location,
  681. file_ptr offset,
  682. bfd_size_type count)
  683. {
  684. if (count == 0)
  685. return true;
  686. if (offset + count < count
  687. || offset + count > section->size)
  688. {
  689. bfd_set_error (bfd_error_invalid_operation);
  690. return false;
  691. }
  692. if (section->used_by_bfd == NULL)
  693. {
  694. section->used_by_bfd = bfd_alloc (abfd, section->size);
  695. if (section->used_by_bfd == NULL)
  696. return false;
  697. if (! srec_read_section (abfd, section,
  698. (bfd_byte *) section->used_by_bfd))
  699. return false;
  700. }
  701. memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
  702. (size_t) count);
  703. return true;
  704. }
  705. /* Set the architecture. We accept an unknown architecture here. */
  706. static bool
  707. srec_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
  708. {
  709. if (arch != bfd_arch_unknown)
  710. return bfd_default_set_arch_mach (abfd, arch, mach);
  711. abfd->arch_info = & bfd_default_arch_struct;
  712. return true;
  713. }
  714. /* We have to save up all the Srecords for a splurge before output. */
  715. static bool
  716. srec_set_section_contents (bfd *abfd,
  717. sec_ptr section,
  718. const void * location,
  719. file_ptr offset,
  720. bfd_size_type bytes_to_do)
  721. {
  722. int opb = bfd_octets_per_byte (abfd, NULL);
  723. tdata_type *tdata = abfd->tdata.srec_data;
  724. srec_data_list_type *entry;
  725. entry = (srec_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
  726. if (entry == NULL)
  727. return false;
  728. if (bytes_to_do
  729. && (section->flags & SEC_ALLOC)
  730. && (section->flags & SEC_LOAD))
  731. {
  732. bfd_byte *data;
  733. data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
  734. if (data == NULL)
  735. return false;
  736. memcpy ((void *) data, location, (size_t) bytes_to_do);
  737. /* If _bfd_srec_forceS3 is TRUE then always select S3 records,
  738. regardless of the size of the addresses. */
  739. if (_bfd_srec_forceS3)
  740. tdata->type = 3;
  741. else if ((section->lma + (offset + bytes_to_do) / opb - 1) <= 0xffff)
  742. ; /* The default, S1, is OK. */
  743. else if ((section->lma + (offset + bytes_to_do) / opb - 1) <= 0xffffff
  744. && tdata->type <= 2)
  745. tdata->type = 2;
  746. else
  747. tdata->type = 3;
  748. entry->data = data;
  749. entry->where = section->lma + offset / opb;
  750. entry->size = bytes_to_do;
  751. /* Sort the records by address. Optimize for the common case of
  752. adding a record to the end of the list. */
  753. if (tdata->tail != NULL
  754. && entry->where >= tdata->tail->where)
  755. {
  756. tdata->tail->next = entry;
  757. entry->next = NULL;
  758. tdata->tail = entry;
  759. }
  760. else
  761. {
  762. srec_data_list_type **look;
  763. for (look = &tdata->head;
  764. *look != NULL && (*look)->where < entry->where;
  765. look = &(*look)->next)
  766. ;
  767. entry->next = *look;
  768. *look = entry;
  769. if (entry->next == NULL)
  770. tdata->tail = entry;
  771. }
  772. }
  773. return true;
  774. }
  775. /* Write a record of type, of the supplied number of bytes. The
  776. supplied bytes and length don't have a checksum. That's worked out
  777. here. */
  778. static bool
  779. srec_write_record (bfd *abfd,
  780. unsigned int type,
  781. bfd_vma address,
  782. const bfd_byte *data,
  783. const bfd_byte *end)
  784. {
  785. char buffer[2 * MAXCHUNK + 6];
  786. unsigned int check_sum = 0;
  787. const bfd_byte *src = data;
  788. char *dst = buffer;
  789. char *length;
  790. bfd_size_type wrlen;
  791. *dst++ = 'S';
  792. *dst++ = '0' + type;
  793. length = dst;
  794. dst += 2; /* Leave room for dst. */
  795. switch (type)
  796. {
  797. case 3:
  798. case 7:
  799. TOHEX (dst, (address >> 24), check_sum);
  800. dst += 2;
  801. /* Fall through. */
  802. case 8:
  803. case 2:
  804. TOHEX (dst, (address >> 16), check_sum);
  805. dst += 2;
  806. /* Fall through. */
  807. case 9:
  808. case 1:
  809. case 0:
  810. TOHEX (dst, (address >> 8), check_sum);
  811. dst += 2;
  812. TOHEX (dst, (address), check_sum);
  813. dst += 2;
  814. break;
  815. }
  816. for (src = data; src < end; src++)
  817. {
  818. TOHEX (dst, *src, check_sum);
  819. dst += 2;
  820. }
  821. /* Fill in the length. */
  822. TOHEX (length, (dst - length) / 2, check_sum);
  823. check_sum &= 0xff;
  824. check_sum = 255 - check_sum;
  825. TOHEX (dst, check_sum, check_sum);
  826. dst += 2;
  827. *dst++ = '\r';
  828. *dst++ = '\n';
  829. wrlen = dst - buffer;
  830. return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
  831. }
  832. static bool
  833. srec_write_header (bfd *abfd)
  834. {
  835. unsigned int len = strlen (bfd_get_filename (abfd));
  836. /* I'll put an arbitrary 40 char limit on header size. */
  837. if (len > 40)
  838. len = 40;
  839. return srec_write_record (abfd, 0, (bfd_vma) 0,
  840. (bfd_byte *) bfd_get_filename (abfd),
  841. (bfd_byte *) bfd_get_filename (abfd) + len);
  842. }
  843. static bool
  844. srec_write_section (bfd *abfd,
  845. tdata_type *tdata,
  846. srec_data_list_type *list)
  847. {
  848. unsigned int octets_written = 0;
  849. bfd_byte *location = list->data;
  850. /* Validate number of data bytes to write. The srec length byte
  851. counts the address, data and crc bytes. S1 (tdata->type == 1)
  852. records have two address bytes, S2 (tdata->type == 2) records
  853. have three, and S3 (tdata->type == 3) records have four.
  854. The total length can't exceed 255, and a zero data length will
  855. spin for a long time. */
  856. if (_bfd_srec_len == 0)
  857. _bfd_srec_len = 1;
  858. else if (_bfd_srec_len > MAXCHUNK - tdata->type - 2)
  859. _bfd_srec_len = MAXCHUNK - tdata->type - 2;
  860. while (octets_written < list->size)
  861. {
  862. bfd_vma address;
  863. unsigned int octets_this_chunk = list->size - octets_written;
  864. if (octets_this_chunk > _bfd_srec_len)
  865. octets_this_chunk = _bfd_srec_len;
  866. address = list->where + (octets_written
  867. / bfd_octets_per_byte (abfd, NULL));
  868. if (! srec_write_record (abfd,
  869. tdata->type,
  870. address,
  871. location,
  872. location + octets_this_chunk))
  873. return false;
  874. octets_written += octets_this_chunk;
  875. location += octets_this_chunk;
  876. }
  877. return true;
  878. }
  879. static bool
  880. srec_write_terminator (bfd *abfd, tdata_type *tdata)
  881. {
  882. return srec_write_record (abfd, 10 - tdata->type,
  883. abfd->start_address, NULL, NULL);
  884. }
  885. static bool
  886. srec_write_symbols (bfd *abfd)
  887. {
  888. /* Dump out the symbols of a bfd. */
  889. int i;
  890. int count = bfd_get_symcount (abfd);
  891. if (count)
  892. {
  893. bfd_size_type len;
  894. asymbol **table = bfd_get_outsymbols (abfd);
  895. len = strlen (bfd_get_filename (abfd));
  896. if (bfd_bwrite ("$$ ", (bfd_size_type) 3, abfd) != 3
  897. || bfd_bwrite (bfd_get_filename (abfd), len, abfd) != len
  898. || bfd_bwrite ("\r\n", (bfd_size_type) 2, abfd) != 2)
  899. return false;
  900. for (i = 0; i < count; i++)
  901. {
  902. asymbol *s = table[i];
  903. if (! bfd_is_local_label (abfd, s)
  904. && (s->flags & BSF_DEBUGGING) == 0
  905. && s->section != NULL
  906. && s->section->output_section != NULL)
  907. {
  908. /* Just dump out non debug symbols. */
  909. char buf[43], *p;
  910. len = strlen (s->name);
  911. if (bfd_bwrite (" ", (bfd_size_type) 2, abfd) != 2
  912. || bfd_bwrite (s->name, len, abfd) != len)
  913. return false;
  914. sprintf_vma (buf + 2, (s->value
  915. + s->section->output_section->lma
  916. + s->section->output_offset));
  917. p = buf + 2;
  918. while (p[0] == '0' && p[1] != 0)
  919. p++;
  920. len = strlen (p);
  921. p[len] = '\r';
  922. p[len + 1] = '\n';
  923. *--p = '$';
  924. *--p = ' ';
  925. len += 4;
  926. if (bfd_bwrite (p, len, abfd) != len)
  927. return false;
  928. }
  929. }
  930. if (bfd_bwrite ("$$ \r\n", (bfd_size_type) 5, abfd) != 5)
  931. return false;
  932. }
  933. return true;
  934. }
  935. static bool
  936. internal_srec_write_object_contents (bfd *abfd, int symbols)
  937. {
  938. tdata_type *tdata = abfd->tdata.srec_data;
  939. srec_data_list_type *list;
  940. if (symbols)
  941. {
  942. if (! srec_write_symbols (abfd))
  943. return false;
  944. }
  945. if (! srec_write_header (abfd))
  946. return false;
  947. /* Now wander though all the sections provided and output them. */
  948. list = tdata->head;
  949. while (list != (srec_data_list_type *) NULL)
  950. {
  951. if (! srec_write_section (abfd, tdata, list))
  952. return false;
  953. list = list->next;
  954. }
  955. return srec_write_terminator (abfd, tdata);
  956. }
  957. static bool
  958. srec_write_object_contents (bfd *abfd)
  959. {
  960. return internal_srec_write_object_contents (abfd, 0);
  961. }
  962. static bool
  963. symbolsrec_write_object_contents (bfd *abfd)
  964. {
  965. return internal_srec_write_object_contents (abfd, 1);
  966. }
  967. static int
  968. srec_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  969. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  970. {
  971. return 0;
  972. }
  973. /* Return the amount of memory needed to read the symbol table. */
  974. static long
  975. srec_get_symtab_upper_bound (bfd *abfd)
  976. {
  977. return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
  978. }
  979. /* Return the symbol table. */
  980. static long
  981. srec_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  982. {
  983. bfd_size_type symcount = bfd_get_symcount (abfd);
  984. asymbol *csymbols;
  985. unsigned int i;
  986. csymbols = abfd->tdata.srec_data->csymbols;
  987. if (csymbols == NULL && symcount != 0)
  988. {
  989. asymbol *c;
  990. struct srec_symbol *s;
  991. csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
  992. if (csymbols == NULL)
  993. return -1;
  994. abfd->tdata.srec_data->csymbols = csymbols;
  995. for (s = abfd->tdata.srec_data->symbols, c = csymbols;
  996. s != NULL;
  997. s = s->next, ++c)
  998. {
  999. c->the_bfd = abfd;
  1000. c->name = s->name;
  1001. c->value = s->val;
  1002. c->flags = BSF_GLOBAL;
  1003. c->section = bfd_abs_section_ptr;
  1004. c->udata.p = NULL;
  1005. }
  1006. }
  1007. for (i = 0; i < symcount; i++)
  1008. *alocation++ = csymbols++;
  1009. *alocation = NULL;
  1010. return symcount;
  1011. }
  1012. static void
  1013. srec_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  1014. asymbol *symbol,
  1015. symbol_info *ret)
  1016. {
  1017. bfd_symbol_info (symbol, ret);
  1018. }
  1019. static void
  1020. srec_print_symbol (bfd *abfd,
  1021. void * afile,
  1022. asymbol *symbol,
  1023. bfd_print_symbol_type how)
  1024. {
  1025. FILE *file = (FILE *) afile;
  1026. switch (how)
  1027. {
  1028. case bfd_print_symbol_name:
  1029. fprintf (file, "%s", symbol->name);
  1030. break;
  1031. default:
  1032. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  1033. fprintf (file, " %-5s %s",
  1034. symbol->section->name,
  1035. symbol->name);
  1036. }
  1037. }
  1038. #define srec_close_and_cleanup _bfd_generic_close_and_cleanup
  1039. #define srec_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  1040. #define srec_new_section_hook _bfd_generic_new_section_hook
  1041. #define srec_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  1042. #define srec_bfd_is_local_label_name bfd_generic_is_local_label_name
  1043. #define srec_get_lineno _bfd_nosymbols_get_lineno
  1044. #define srec_find_nearest_line _bfd_nosymbols_find_nearest_line
  1045. #define srec_find_line _bfd_nosymbols_find_line
  1046. #define srec_find_inliner_info _bfd_nosymbols_find_inliner_info
  1047. #define srec_make_empty_symbol _bfd_generic_make_empty_symbol
  1048. #define srec_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  1049. #define srec_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  1050. #define srec_read_minisymbols _bfd_generic_read_minisymbols
  1051. #define srec_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  1052. #define srec_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  1053. #define srec_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  1054. #define srec_bfd_relax_section bfd_generic_relax_section
  1055. #define srec_bfd_gc_sections bfd_generic_gc_sections
  1056. #define srec_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  1057. #define srec_bfd_merge_sections bfd_generic_merge_sections
  1058. #define srec_bfd_is_group_section bfd_generic_is_group_section
  1059. #define srec_bfd_group_name bfd_generic_group_name
  1060. #define srec_bfd_discard_group bfd_generic_discard_group
  1061. #define srec_section_already_linked _bfd_generic_section_already_linked
  1062. #define srec_bfd_define_common_symbol bfd_generic_define_common_symbol
  1063. #define srec_bfd_link_hide_symbol _bfd_generic_link_hide_symbol
  1064. #define srec_bfd_define_start_stop bfd_generic_define_start_stop
  1065. #define srec_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  1066. #define srec_bfd_link_add_symbols _bfd_generic_link_add_symbols
  1067. #define srec_bfd_link_just_syms _bfd_generic_link_just_syms
  1068. #define srec_bfd_copy_link_hash_symbol_type _bfd_generic_copy_link_hash_symbol_type
  1069. #define srec_bfd_final_link _bfd_generic_final_link
  1070. #define srec_bfd_link_split_section _bfd_generic_link_split_section
  1071. #define srec_bfd_link_check_relocs _bfd_generic_link_check_relocs
  1072. const bfd_target srec_vec =
  1073. {
  1074. "srec", /* Name. */
  1075. bfd_target_srec_flavour,
  1076. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  1077. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  1078. (HAS_RELOC | EXEC_P | /* Object flags. */
  1079. HAS_LINENO | HAS_DEBUG |
  1080. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1081. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1082. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  1083. 0, /* Leading underscore. */
  1084. ' ', /* AR_pad_char. */
  1085. 16, /* AR_max_namelen. */
  1086. 0, /* match priority. */
  1087. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  1088. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1089. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1090. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  1091. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1092. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1093. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Hdrs. */
  1094. {
  1095. _bfd_dummy_target,
  1096. srec_object_p, /* bfd_check_format. */
  1097. _bfd_dummy_target,
  1098. _bfd_dummy_target,
  1099. },
  1100. {
  1101. _bfd_bool_bfd_false_error,
  1102. srec_mkobject,
  1103. _bfd_generic_mkarchive,
  1104. _bfd_bool_bfd_false_error,
  1105. },
  1106. { /* bfd_write_contents. */
  1107. _bfd_bool_bfd_false_error,
  1108. srec_write_object_contents,
  1109. _bfd_write_archive_contents,
  1110. _bfd_bool_bfd_false_error,
  1111. },
  1112. BFD_JUMP_TABLE_GENERIC (srec),
  1113. BFD_JUMP_TABLE_COPY (_bfd_generic),
  1114. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1115. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1116. BFD_JUMP_TABLE_SYMBOLS (srec),
  1117. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  1118. BFD_JUMP_TABLE_WRITE (srec),
  1119. BFD_JUMP_TABLE_LINK (srec),
  1120. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1121. NULL,
  1122. NULL
  1123. };
  1124. const bfd_target symbolsrec_vec =
  1125. {
  1126. "symbolsrec", /* Name. */
  1127. bfd_target_srec_flavour,
  1128. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  1129. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  1130. (HAS_RELOC | EXEC_P | /* Object flags. */
  1131. HAS_LINENO | HAS_DEBUG |
  1132. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1133. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1134. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  1135. 0, /* Leading underscore. */
  1136. ' ', /* AR_pad_char. */
  1137. 16, /* AR_max_namelen. */
  1138. 0, /* match priority. */
  1139. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  1140. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1141. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1142. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  1143. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1144. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1145. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  1146. {
  1147. _bfd_dummy_target,
  1148. symbolsrec_object_p, /* bfd_check_format. */
  1149. _bfd_dummy_target,
  1150. _bfd_dummy_target,
  1151. },
  1152. {
  1153. _bfd_bool_bfd_false_error,
  1154. srec_mkobject,
  1155. _bfd_generic_mkarchive,
  1156. _bfd_bool_bfd_false_error,
  1157. },
  1158. { /* bfd_write_contents. */
  1159. _bfd_bool_bfd_false_error,
  1160. symbolsrec_write_object_contents,
  1161. _bfd_write_archive_contents,
  1162. _bfd_bool_bfd_false_error,
  1163. },
  1164. BFD_JUMP_TABLE_GENERIC (srec),
  1165. BFD_JUMP_TABLE_COPY (_bfd_generic),
  1166. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1167. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1168. BFD_JUMP_TABLE_SYMBOLS (srec),
  1169. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  1170. BFD_JUMP_TABLE_WRITE (srec),
  1171. BFD_JUMP_TABLE_LINK (srec),
  1172. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1173. NULL,
  1174. NULL
  1175. };