srec.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /* BFD back-end for s-record objects.
  2. Copyright (C) 1990-2015 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 Chunk = 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. bfd_boolean S3Forced = 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 bfd_boolean inited = FALSE;
  143. if (! inited)
  144. {
  145. inited = TRUE;
  146. hex_init ();
  147. }
  148. }
  149. /* Set up the S-record tdata information. */
  150. static bfd_boolean
  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, bfd_boolean *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. bfd_boolean 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. (_("%B:%d: Unexpected character `%s' in S-record file\n"),
  207. abfd, lineno, buf);
  208. bfd_set_error (bfd_error_bad_value);
  209. }
  210. }
  211. /* Add a new symbol found in an S-record file. */
  212. static bfd_boolean
  213. srec_new_symbol (bfd *abfd, const char *name, bfd_vma val)
  214. {
  215. struct srec_symbol *n;
  216. n = (struct srec_symbol *) bfd_alloc (abfd, sizeof (* n));
  217. if (n == NULL)
  218. return FALSE;
  219. n->name = name;
  220. n->val = val;
  221. if (abfd->tdata.srec_data->symbols == NULL)
  222. abfd->tdata.srec_data->symbols = n;
  223. else
  224. abfd->tdata.srec_data->symtail->next = n;
  225. abfd->tdata.srec_data->symtail = n;
  226. n->next = NULL;
  227. ++abfd->symcount;
  228. return TRUE;
  229. }
  230. /* Read the S record file and turn it into sections. We create a new
  231. section for each contiguous set of bytes. */
  232. static bfd_boolean
  233. srec_scan (bfd *abfd)
  234. {
  235. int c;
  236. unsigned int lineno = 1;
  237. bfd_boolean error = FALSE;
  238. bfd_byte *buf = NULL;
  239. size_t bufsize = 0;
  240. asection *sec = NULL;
  241. char *symbuf = NULL;
  242. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0)
  243. goto error_return;
  244. while ((c = srec_get_byte (abfd, &error)) != EOF)
  245. {
  246. /* We only build sections from contiguous S-records, so if this
  247. is not an S-record, then stop building a section. */
  248. if (c != 'S' && c != '\r' && c != '\n')
  249. sec = NULL;
  250. switch (c)
  251. {
  252. default:
  253. srec_bad_byte (abfd, lineno, c, error);
  254. goto error_return;
  255. case '\n':
  256. ++lineno;
  257. break;
  258. case '\r':
  259. break;
  260. case '$':
  261. /* Starting a module name, which we ignore. */
  262. while ((c = srec_get_byte (abfd, &error)) != '\n'
  263. && c != EOF)
  264. ;
  265. if (c == EOF)
  266. {
  267. srec_bad_byte (abfd, lineno, c, error);
  268. goto error_return;
  269. }
  270. ++lineno;
  271. break;
  272. case ' ':
  273. do
  274. {
  275. bfd_size_type alc;
  276. char *p, *symname;
  277. bfd_vma symval;
  278. /* Starting a symbol definition. */
  279. while ((c = srec_get_byte (abfd, &error)) != EOF
  280. && (c == ' ' || c == '\t'))
  281. ;
  282. if (c == '\n' || c == '\r')
  283. break;
  284. if (c == EOF)
  285. {
  286. srec_bad_byte (abfd, lineno, c, error);
  287. goto error_return;
  288. }
  289. alc = 10;
  290. symbuf = (char *) bfd_malloc (alc + 1);
  291. if (symbuf == NULL)
  292. goto error_return;
  293. p = symbuf;
  294. *p++ = c;
  295. while ((c = srec_get_byte (abfd, &error)) != EOF
  296. && ! ISSPACE (c))
  297. {
  298. if ((bfd_size_type) (p - symbuf) >= alc)
  299. {
  300. char *n;
  301. alc *= 2;
  302. n = (char *) bfd_realloc (symbuf, alc + 1);
  303. if (n == NULL)
  304. goto error_return;
  305. p = n + (p - symbuf);
  306. symbuf = n;
  307. }
  308. *p++ = c;
  309. }
  310. if (c == EOF)
  311. {
  312. srec_bad_byte (abfd, lineno, c, error);
  313. goto error_return;
  314. }
  315. *p++ = '\0';
  316. symname = (char *) bfd_alloc (abfd, (bfd_size_type) (p - symbuf));
  317. if (symname == NULL)
  318. goto error_return;
  319. strcpy (symname, symbuf);
  320. free (symbuf);
  321. symbuf = NULL;
  322. while ((c = srec_get_byte (abfd, &error)) != EOF
  323. && (c == ' ' || c == '\t'))
  324. ;
  325. if (c == EOF)
  326. {
  327. srec_bad_byte (abfd, lineno, c, error);
  328. goto error_return;
  329. }
  330. /* Skip a dollar sign before the hex value. */
  331. if (c == '$')
  332. {
  333. c = srec_get_byte (abfd, &error);
  334. if (c == EOF)
  335. {
  336. srec_bad_byte (abfd, lineno, c, error);
  337. goto error_return;
  338. }
  339. }
  340. symval = 0;
  341. while (ISHEX (c))
  342. {
  343. symval <<= 4;
  344. symval += NIBBLE (c);
  345. c = srec_get_byte (abfd, &error);
  346. if (c == EOF)
  347. {
  348. srec_bad_byte (abfd, lineno, c, error);
  349. goto error_return;
  350. }
  351. }
  352. if (! srec_new_symbol (abfd, symname, symval))
  353. goto error_return;
  354. }
  355. while (c == ' ' || c == '\t')
  356. ;
  357. if (c == '\n')
  358. ++lineno;
  359. else if (c != '\r')
  360. {
  361. srec_bad_byte (abfd, lineno, c, error);
  362. goto error_return;
  363. }
  364. break;
  365. case 'S':
  366. {
  367. file_ptr pos;
  368. unsigned char hdr[3];
  369. unsigned int bytes, min_bytes;
  370. bfd_vma address;
  371. bfd_byte *data;
  372. unsigned char check_sum;
  373. /* Starting an S-record. */
  374. pos = bfd_tell (abfd) - 1;
  375. if (bfd_bread (hdr, (bfd_size_type) 3, abfd) != 3)
  376. goto error_return;
  377. if (! ISHEX (hdr[1]) || ! ISHEX (hdr[2]))
  378. {
  379. if (! ISHEX (hdr[1]))
  380. c = hdr[1];
  381. else
  382. c = hdr[2];
  383. srec_bad_byte (abfd, lineno, c, error);
  384. goto error_return;
  385. }
  386. check_sum = bytes = HEX (hdr + 1);
  387. min_bytes = 3;
  388. if (hdr[0] == '2' || hdr[0] == '8')
  389. min_bytes = 4;
  390. else if (hdr[0] == '3' || hdr[0] == '7')
  391. min_bytes = 5;
  392. if (bytes < min_bytes)
  393. {
  394. (*_bfd_error_handler) (_("%B:%d: byte count %d too small\n"),
  395. abfd, lineno, bytes);
  396. bfd_set_error (bfd_error_bad_value);
  397. goto error_return;
  398. }
  399. if (bytes * 2 > bufsize)
  400. {
  401. if (buf != NULL)
  402. free (buf);
  403. buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
  404. if (buf == NULL)
  405. goto error_return;
  406. bufsize = bytes * 2;
  407. }
  408. if (bfd_bread (buf, (bfd_size_type) bytes * 2, abfd) != bytes * 2)
  409. goto error_return;
  410. /* Ignore the checksum byte. */
  411. --bytes;
  412. address = 0;
  413. data = buf;
  414. switch (hdr[0])
  415. {
  416. case '0':
  417. case '5':
  418. /* Prologue--ignore the file name, but stop building a
  419. section at this point. */
  420. sec = NULL;
  421. break;
  422. case '3':
  423. check_sum += HEX (data);
  424. address = HEX (data);
  425. data += 2;
  426. --bytes;
  427. /* Fall through. */
  428. case '2':
  429. check_sum += HEX (data);
  430. address = (address << 8) | HEX (data);
  431. data += 2;
  432. --bytes;
  433. /* Fall through. */
  434. case '1':
  435. check_sum += HEX (data);
  436. address = (address << 8) | HEX (data);
  437. data += 2;
  438. check_sum += HEX (data);
  439. address = (address << 8) | HEX (data);
  440. data += 2;
  441. bytes -= 2;
  442. if (sec != NULL
  443. && sec->vma + sec->size == address)
  444. {
  445. /* This data goes at the end of the section we are
  446. currently building. */
  447. sec->size += bytes;
  448. }
  449. else
  450. {
  451. char secbuf[20];
  452. char *secname;
  453. bfd_size_type amt;
  454. flagword flags;
  455. sprintf (secbuf, ".sec%d", bfd_count_sections (abfd) + 1);
  456. amt = strlen (secbuf) + 1;
  457. secname = (char *) bfd_alloc (abfd, amt);
  458. strcpy (secname, secbuf);
  459. flags = SEC_HAS_CONTENTS | SEC_LOAD | SEC_ALLOC;
  460. sec = bfd_make_section_with_flags (abfd, secname, flags);
  461. if (sec == NULL)
  462. goto error_return;
  463. sec->vma = address;
  464. sec->lma = address;
  465. sec->size = bytes;
  466. sec->filepos = pos;
  467. }
  468. while (bytes > 0)
  469. {
  470. check_sum += HEX (data);
  471. data += 2;
  472. bytes--;
  473. }
  474. check_sum = 255 - (check_sum & 0xff);
  475. if (check_sum != HEX (data))
  476. {
  477. (*_bfd_error_handler)
  478. (_("%B:%d: Bad checksum in S-record file\n"),
  479. abfd, lineno);
  480. bfd_set_error (bfd_error_bad_value);
  481. goto error_return;
  482. }
  483. break;
  484. case '7':
  485. check_sum += HEX (data);
  486. address = HEX (data);
  487. data += 2;
  488. /* Fall through. */
  489. case '8':
  490. check_sum += HEX (data);
  491. address = (address << 8) | HEX (data);
  492. data += 2;
  493. /* Fall through. */
  494. case '9':
  495. check_sum += HEX (data);
  496. address = (address << 8) | HEX (data);
  497. data += 2;
  498. check_sum += HEX (data);
  499. address = (address << 8) | HEX (data);
  500. data += 2;
  501. /* This is a termination record. */
  502. abfd->start_address = address;
  503. check_sum = 255 - (check_sum & 0xff);
  504. if (check_sum != HEX (data))
  505. {
  506. (*_bfd_error_handler)
  507. (_("%B:%d: Bad checksum in S-record file\n"),
  508. abfd, lineno);
  509. bfd_set_error (bfd_error_bad_value);
  510. goto error_return;
  511. }
  512. if (buf != NULL)
  513. free (buf);
  514. return TRUE;
  515. }
  516. }
  517. break;
  518. }
  519. }
  520. if (error)
  521. goto error_return;
  522. if (buf != NULL)
  523. free (buf);
  524. return TRUE;
  525. error_return:
  526. if (symbuf != NULL)
  527. free (symbuf);
  528. if (buf != NULL)
  529. free (buf);
  530. return FALSE;
  531. }
  532. /* Check whether an existing file is an S-record file. */
  533. static const bfd_target *
  534. srec_object_p (bfd *abfd)
  535. {
  536. void * tdata_save;
  537. bfd_byte b[4];
  538. srec_init ();
  539. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  540. || bfd_bread (b, (bfd_size_type) 4, abfd) != 4)
  541. return NULL;
  542. if (b[0] != 'S' || !ISHEX (b[1]) || !ISHEX (b[2]) || !ISHEX (b[3]))
  543. {
  544. bfd_set_error (bfd_error_wrong_format);
  545. return NULL;
  546. }
  547. tdata_save = abfd->tdata.any;
  548. if (! srec_mkobject (abfd) || ! srec_scan (abfd))
  549. {
  550. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  551. bfd_release (abfd, abfd->tdata.any);
  552. abfd->tdata.any = tdata_save;
  553. return NULL;
  554. }
  555. if (abfd->symcount > 0)
  556. abfd->flags |= HAS_SYMS;
  557. return abfd->xvec;
  558. }
  559. /* Check whether an existing file is an S-record file with symbols. */
  560. static const bfd_target *
  561. symbolsrec_object_p (bfd *abfd)
  562. {
  563. void * tdata_save;
  564. char b[2];
  565. srec_init ();
  566. if (bfd_seek (abfd, (file_ptr) 0, SEEK_SET) != 0
  567. || bfd_bread (b, (bfd_size_type) 2, abfd) != 2)
  568. return NULL;
  569. if (b[0] != '$' || b[1] != '$')
  570. {
  571. bfd_set_error (bfd_error_wrong_format);
  572. return NULL;
  573. }
  574. tdata_save = abfd->tdata.any;
  575. if (! srec_mkobject (abfd) || ! srec_scan (abfd))
  576. {
  577. if (abfd->tdata.any != tdata_save && abfd->tdata.any != NULL)
  578. bfd_release (abfd, abfd->tdata.any);
  579. abfd->tdata.any = tdata_save;
  580. return NULL;
  581. }
  582. if (abfd->symcount > 0)
  583. abfd->flags |= HAS_SYMS;
  584. return abfd->xvec;
  585. }
  586. /* Read in the contents of a section in an S-record file. */
  587. static bfd_boolean
  588. srec_read_section (bfd *abfd, asection *section, bfd_byte *contents)
  589. {
  590. int c;
  591. bfd_size_type sofar = 0;
  592. bfd_boolean error = FALSE;
  593. bfd_byte *buf = NULL;
  594. size_t bufsize = 0;
  595. if (bfd_seek (abfd, section->filepos, SEEK_SET) != 0)
  596. goto error_return;
  597. while ((c = srec_get_byte (abfd, &error)) != EOF)
  598. {
  599. bfd_byte hdr[3];
  600. unsigned int bytes;
  601. bfd_vma address;
  602. bfd_byte *data;
  603. if (c == '\r' || c == '\n')
  604. continue;
  605. /* This is called after srec_scan has already been called, so we
  606. ought to know the exact format. */
  607. BFD_ASSERT (c == 'S');
  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. if (buf != NULL)
  615. free (buf);
  616. buf = (bfd_byte *) bfd_malloc ((bfd_size_type) bytes * 2);
  617. if (buf == NULL)
  618. goto error_return;
  619. bufsize = bytes * 2;
  620. }
  621. if (bfd_bread (buf, (bfd_size_type) bytes * 2, abfd) != bytes * 2)
  622. goto error_return;
  623. address = 0;
  624. data = buf;
  625. switch (hdr[0])
  626. {
  627. default:
  628. BFD_ASSERT (sofar == section->size);
  629. if (buf != NULL)
  630. free (buf);
  631. return TRUE;
  632. case '3':
  633. address = HEX (data);
  634. data += 2;
  635. --bytes;
  636. /* Fall through. */
  637. case '2':
  638. address = (address << 8) | HEX (data);
  639. data += 2;
  640. --bytes;
  641. /* Fall through. */
  642. case '1':
  643. address = (address << 8) | HEX (data);
  644. data += 2;
  645. address = (address << 8) | HEX (data);
  646. data += 2;
  647. bytes -= 2;
  648. if (address != section->vma + sofar)
  649. {
  650. /* We've come to the end of this section. */
  651. BFD_ASSERT (sofar == section->size);
  652. if (buf != NULL)
  653. free (buf);
  654. return TRUE;
  655. }
  656. /* Don't consider checksum. */
  657. --bytes;
  658. while (bytes-- != 0)
  659. {
  660. contents[sofar] = HEX (data);
  661. data += 2;
  662. ++sofar;
  663. }
  664. break;
  665. }
  666. }
  667. if (error)
  668. goto error_return;
  669. BFD_ASSERT (sofar == section->size);
  670. if (buf != NULL)
  671. free (buf);
  672. return TRUE;
  673. error_return:
  674. if (buf != NULL)
  675. free (buf);
  676. return FALSE;
  677. }
  678. /* Get the contents of a section in an S-record file. */
  679. static bfd_boolean
  680. srec_get_section_contents (bfd *abfd,
  681. asection *section,
  682. void * location,
  683. file_ptr offset,
  684. bfd_size_type count)
  685. {
  686. if (count == 0)
  687. return TRUE;
  688. if (offset + count < count
  689. || offset + count > section->size)
  690. {
  691. bfd_set_error (bfd_error_invalid_operation);
  692. return FALSE;
  693. }
  694. if (section->used_by_bfd == NULL)
  695. {
  696. section->used_by_bfd = bfd_alloc (abfd, section->size);
  697. if (section->used_by_bfd == NULL)
  698. return FALSE;
  699. if (! srec_read_section (abfd, section,
  700. (bfd_byte *) section->used_by_bfd))
  701. return FALSE;
  702. }
  703. memcpy (location, (bfd_byte *) section->used_by_bfd + offset,
  704. (size_t) count);
  705. return TRUE;
  706. }
  707. /* Set the architecture. We accept an unknown architecture here. */
  708. static bfd_boolean
  709. srec_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
  710. {
  711. if (arch != bfd_arch_unknown)
  712. return bfd_default_set_arch_mach (abfd, arch, mach);
  713. abfd->arch_info = & bfd_default_arch_struct;
  714. return TRUE;
  715. }
  716. /* We have to save up all the Srecords for a splurge before output. */
  717. static bfd_boolean
  718. srec_set_section_contents (bfd *abfd,
  719. sec_ptr section,
  720. const void * location,
  721. file_ptr offset,
  722. bfd_size_type bytes_to_do)
  723. {
  724. int opb = bfd_octets_per_byte (abfd);
  725. tdata_type *tdata = abfd->tdata.srec_data;
  726. srec_data_list_type *entry;
  727. entry = (srec_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
  728. if (entry == NULL)
  729. return FALSE;
  730. if (bytes_to_do
  731. && (section->flags & SEC_ALLOC)
  732. && (section->flags & SEC_LOAD))
  733. {
  734. bfd_byte *data;
  735. data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
  736. if (data == NULL)
  737. return FALSE;
  738. memcpy ((void *) data, location, (size_t) bytes_to_do);
  739. /* Ff S3Forced is TRUE then always select S3 records,
  740. regardless of the siez of the addresses. */
  741. if (S3Forced)
  742. tdata->type = 3;
  743. else if ((section->lma + (offset + bytes_to_do) / opb - 1) <= 0xffff)
  744. ; /* The default, S1, is OK. */
  745. else if ((section->lma + (offset + bytes_to_do) / opb - 1) <= 0xffffff
  746. && tdata->type <= 2)
  747. tdata->type = 2;
  748. else
  749. tdata->type = 3;
  750. entry->data = data;
  751. entry->where = section->lma + offset / opb;
  752. entry->size = bytes_to_do;
  753. /* Sort the records by address. Optimize for the common case of
  754. adding a record to the end of the list. */
  755. if (tdata->tail != NULL
  756. && entry->where >= tdata->tail->where)
  757. {
  758. tdata->tail->next = entry;
  759. entry->next = NULL;
  760. tdata->tail = entry;
  761. }
  762. else
  763. {
  764. srec_data_list_type **look;
  765. for (look = &tdata->head;
  766. *look != NULL && (*look)->where < entry->where;
  767. look = &(*look)->next)
  768. ;
  769. entry->next = *look;
  770. *look = entry;
  771. if (entry->next == NULL)
  772. tdata->tail = entry;
  773. }
  774. }
  775. return TRUE;
  776. }
  777. /* Write a record of type, of the supplied number of bytes. The
  778. supplied bytes and length don't have a checksum. That's worked out
  779. here. */
  780. static bfd_boolean
  781. srec_write_record (bfd *abfd,
  782. unsigned int type,
  783. bfd_vma address,
  784. const bfd_byte *data,
  785. const bfd_byte *end)
  786. {
  787. char buffer[2 * MAXCHUNK + 6];
  788. unsigned int check_sum = 0;
  789. const bfd_byte *src = data;
  790. char *dst = buffer;
  791. char *length;
  792. bfd_size_type wrlen;
  793. *dst++ = 'S';
  794. *dst++ = '0' + type;
  795. length = dst;
  796. dst += 2; /* Leave room for dst. */
  797. switch (type)
  798. {
  799. case 3:
  800. case 7:
  801. TOHEX (dst, (address >> 24), check_sum);
  802. dst += 2;
  803. case 8:
  804. case 2:
  805. TOHEX (dst, (address >> 16), check_sum);
  806. dst += 2;
  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 bfd_boolean
  833. srec_write_header (bfd *abfd)
  834. {
  835. unsigned int len = strlen (abfd->filename);
  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 *) abfd->filename,
  841. (bfd_byte *) abfd->filename + len);
  842. }
  843. static bfd_boolean
  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 (Chunk == 0)
  857. Chunk = 1;
  858. else if (Chunk > MAXCHUNK - tdata->type - 2)
  859. Chunk = 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 > Chunk)
  865. octets_this_chunk = Chunk;
  866. address = list->where + octets_written / bfd_octets_per_byte (abfd);
  867. if (! srec_write_record (abfd,
  868. tdata->type,
  869. address,
  870. location,
  871. location + octets_this_chunk))
  872. return FALSE;
  873. octets_written += octets_this_chunk;
  874. location += octets_this_chunk;
  875. }
  876. return TRUE;
  877. }
  878. static bfd_boolean
  879. srec_write_terminator (bfd *abfd, tdata_type *tdata)
  880. {
  881. return srec_write_record (abfd, 10 - tdata->type,
  882. abfd->start_address, NULL, NULL);
  883. }
  884. static bfd_boolean
  885. srec_write_symbols (bfd *abfd)
  886. {
  887. /* Dump out the symbols of a bfd. */
  888. int i;
  889. int count = bfd_get_symcount (abfd);
  890. if (count)
  891. {
  892. bfd_size_type len;
  893. asymbol **table = bfd_get_outsymbols (abfd);
  894. len = strlen (abfd->filename);
  895. if (bfd_bwrite ("$$ ", (bfd_size_type) 3, abfd) != 3
  896. || bfd_bwrite (abfd->filename, len, abfd) != len
  897. || bfd_bwrite ("\r\n", (bfd_size_type) 2, abfd) != 2)
  898. return FALSE;
  899. for (i = 0; i < count; i++)
  900. {
  901. asymbol *s = table[i];
  902. if (! bfd_is_local_label (abfd, s)
  903. && (s->flags & BSF_DEBUGGING) == 0)
  904. {
  905. /* Just dump out non debug symbols. */
  906. char buf[43], *p;
  907. len = strlen (s->name);
  908. if (bfd_bwrite (" ", (bfd_size_type) 2, abfd) != 2
  909. || bfd_bwrite (s->name, len, abfd) != len)
  910. return FALSE;
  911. sprintf_vma (buf + 2, (s->value
  912. + s->section->output_section->lma
  913. + s->section->output_offset));
  914. p = buf + 2;
  915. while (p[0] == '0' && p[1] != 0)
  916. p++;
  917. len = strlen (p);
  918. p[len] = '\r';
  919. p[len + 1] = '\n';
  920. *--p = '$';
  921. *--p = ' ';
  922. len += 4;
  923. if (bfd_bwrite (p, len, abfd) != len)
  924. return FALSE;
  925. }
  926. }
  927. if (bfd_bwrite ("$$ \r\n", (bfd_size_type) 5, abfd) != 5)
  928. return FALSE;
  929. }
  930. return TRUE;
  931. }
  932. static bfd_boolean
  933. internal_srec_write_object_contents (bfd *abfd, int symbols)
  934. {
  935. tdata_type *tdata = abfd->tdata.srec_data;
  936. srec_data_list_type *list;
  937. if (symbols)
  938. {
  939. if (! srec_write_symbols (abfd))
  940. return FALSE;
  941. }
  942. if (! srec_write_header (abfd))
  943. return FALSE;
  944. /* Now wander though all the sections provided and output them. */
  945. list = tdata->head;
  946. while (list != (srec_data_list_type *) NULL)
  947. {
  948. if (! srec_write_section (abfd, tdata, list))
  949. return FALSE;
  950. list = list->next;
  951. }
  952. return srec_write_terminator (abfd, tdata);
  953. }
  954. static bfd_boolean
  955. srec_write_object_contents (bfd *abfd)
  956. {
  957. return internal_srec_write_object_contents (abfd, 0);
  958. }
  959. static bfd_boolean
  960. symbolsrec_write_object_contents (bfd *abfd)
  961. {
  962. return internal_srec_write_object_contents (abfd, 1);
  963. }
  964. static int
  965. srec_sizeof_headers (bfd *abfd ATTRIBUTE_UNUSED,
  966. struct bfd_link_info *info ATTRIBUTE_UNUSED)
  967. {
  968. return 0;
  969. }
  970. /* Return the amount of memory needed to read the symbol table. */
  971. static long
  972. srec_get_symtab_upper_bound (bfd *abfd)
  973. {
  974. return (bfd_get_symcount (abfd) + 1) * sizeof (asymbol *);
  975. }
  976. /* Return the symbol table. */
  977. static long
  978. srec_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  979. {
  980. bfd_size_type symcount = bfd_get_symcount (abfd);
  981. asymbol *csymbols;
  982. unsigned int i;
  983. csymbols = abfd->tdata.srec_data->csymbols;
  984. if (csymbols == NULL && symcount != 0)
  985. {
  986. asymbol *c;
  987. struct srec_symbol *s;
  988. csymbols = (asymbol *) bfd_alloc (abfd, symcount * sizeof (asymbol));
  989. if (csymbols == NULL)
  990. return -1;
  991. abfd->tdata.srec_data->csymbols = csymbols;
  992. for (s = abfd->tdata.srec_data->symbols, c = csymbols;
  993. s != NULL;
  994. s = s->next, ++c)
  995. {
  996. c->the_bfd = abfd;
  997. c->name = s->name;
  998. c->value = s->val;
  999. c->flags = BSF_GLOBAL;
  1000. c->section = bfd_abs_section_ptr;
  1001. c->udata.p = NULL;
  1002. }
  1003. }
  1004. for (i = 0; i < symcount; i++)
  1005. *alocation++ = csymbols++;
  1006. *alocation = NULL;
  1007. return symcount;
  1008. }
  1009. static void
  1010. srec_get_symbol_info (bfd *ignore_abfd ATTRIBUTE_UNUSED,
  1011. asymbol *symbol,
  1012. symbol_info *ret)
  1013. {
  1014. bfd_symbol_info (symbol, ret);
  1015. }
  1016. static void
  1017. srec_print_symbol (bfd *abfd,
  1018. void * afile,
  1019. asymbol *symbol,
  1020. bfd_print_symbol_type how)
  1021. {
  1022. FILE *file = (FILE *) afile;
  1023. switch (how)
  1024. {
  1025. case bfd_print_symbol_name:
  1026. fprintf (file, "%s", symbol->name);
  1027. break;
  1028. default:
  1029. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  1030. fprintf (file, " %-5s %s",
  1031. symbol->section->name,
  1032. symbol->name);
  1033. }
  1034. }
  1035. #define srec_close_and_cleanup _bfd_generic_close_and_cleanup
  1036. #define srec_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  1037. #define srec_new_section_hook _bfd_generic_new_section_hook
  1038. #define srec_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  1039. #define srec_bfd_is_local_label_name bfd_generic_is_local_label_name
  1040. #define srec_get_lineno _bfd_nosymbols_get_lineno
  1041. #define srec_find_nearest_line _bfd_nosymbols_find_nearest_line
  1042. #define srec_find_line _bfd_nosymbols_find_line
  1043. #define srec_find_inliner_info _bfd_nosymbols_find_inliner_info
  1044. #define srec_make_empty_symbol _bfd_generic_make_empty_symbol
  1045. #define srec_get_symbol_version_string _bfd_nosymbols_get_symbol_version_string
  1046. #define srec_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  1047. #define srec_read_minisymbols _bfd_generic_read_minisymbols
  1048. #define srec_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  1049. #define srec_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  1050. #define srec_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  1051. #define srec_bfd_relax_section bfd_generic_relax_section
  1052. #define srec_bfd_gc_sections bfd_generic_gc_sections
  1053. #define srec_bfd_lookup_section_flags bfd_generic_lookup_section_flags
  1054. #define srec_bfd_merge_sections bfd_generic_merge_sections
  1055. #define srec_bfd_is_group_section bfd_generic_is_group_section
  1056. #define srec_bfd_discard_group bfd_generic_discard_group
  1057. #define srec_section_already_linked _bfd_generic_section_already_linked
  1058. #define srec_bfd_define_common_symbol bfd_generic_define_common_symbol
  1059. #define srec_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  1060. #define srec_bfd_link_add_symbols _bfd_generic_link_add_symbols
  1061. #define srec_bfd_link_just_syms _bfd_generic_link_just_syms
  1062. #define srec_bfd_copy_link_hash_symbol_type \
  1063. _bfd_generic_copy_link_hash_symbol_type
  1064. #define srec_bfd_final_link _bfd_generic_final_link
  1065. #define srec_bfd_link_split_section _bfd_generic_link_split_section
  1066. const bfd_target srec_vec =
  1067. {
  1068. "srec", /* Name. */
  1069. bfd_target_srec_flavour,
  1070. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  1071. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  1072. (HAS_RELOC | EXEC_P | /* Object flags. */
  1073. HAS_LINENO | HAS_DEBUG |
  1074. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1075. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1076. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  1077. 0, /* Leading underscore. */
  1078. ' ', /* AR_pad_char. */
  1079. 16, /* AR_max_namelen. */
  1080. 0, /* match priority. */
  1081. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1082. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1083. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  1084. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1085. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1086. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Hdrs. */
  1087. {
  1088. _bfd_dummy_target,
  1089. srec_object_p, /* bfd_check_format. */
  1090. _bfd_dummy_target,
  1091. _bfd_dummy_target,
  1092. },
  1093. {
  1094. bfd_false,
  1095. srec_mkobject,
  1096. _bfd_generic_mkarchive,
  1097. bfd_false,
  1098. },
  1099. { /* bfd_write_contents. */
  1100. bfd_false,
  1101. srec_write_object_contents,
  1102. _bfd_write_archive_contents,
  1103. bfd_false,
  1104. },
  1105. BFD_JUMP_TABLE_GENERIC (srec),
  1106. BFD_JUMP_TABLE_COPY (_bfd_generic),
  1107. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1108. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1109. BFD_JUMP_TABLE_SYMBOLS (srec),
  1110. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  1111. BFD_JUMP_TABLE_WRITE (srec),
  1112. BFD_JUMP_TABLE_LINK (srec),
  1113. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1114. NULL,
  1115. NULL
  1116. };
  1117. const bfd_target symbolsrec_vec =
  1118. {
  1119. "symbolsrec", /* Name. */
  1120. bfd_target_srec_flavour,
  1121. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  1122. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  1123. (HAS_RELOC | EXEC_P | /* Object flags. */
  1124. HAS_LINENO | HAS_DEBUG |
  1125. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  1126. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  1127. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  1128. 0, /* Leading underscore. */
  1129. ' ', /* AR_pad_char. */
  1130. 16, /* AR_max_namelen. */
  1131. 0, /* match priority. */
  1132. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1133. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1134. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  1135. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  1136. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  1137. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Headers. */
  1138. {
  1139. _bfd_dummy_target,
  1140. symbolsrec_object_p, /* bfd_check_format. */
  1141. _bfd_dummy_target,
  1142. _bfd_dummy_target,
  1143. },
  1144. {
  1145. bfd_false,
  1146. srec_mkobject,
  1147. _bfd_generic_mkarchive,
  1148. bfd_false,
  1149. },
  1150. { /* bfd_write_contents. */
  1151. bfd_false,
  1152. symbolsrec_write_object_contents,
  1153. _bfd_write_archive_contents,
  1154. bfd_false,
  1155. },
  1156. BFD_JUMP_TABLE_GENERIC (srec),
  1157. BFD_JUMP_TABLE_COPY (_bfd_generic),
  1158. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  1159. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  1160. BFD_JUMP_TABLE_SYMBOLS (srec),
  1161. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  1162. BFD_JUMP_TABLE_WRITE (srec),
  1163. BFD_JUMP_TABLE_LINK (srec),
  1164. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  1165. NULL,
  1166. NULL
  1167. };