verilog.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* BFD back-end for verilog hex memory dump files.
  2. Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. Written by Anthony Green <green@moxielogic.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. Verilog hex memory file handling
  19. DESCRIPTION
  20. Verilog hex memory files cannot hold anything but addresses
  21. and data, so that's all that we implement.
  22. The syntax of the text file is described in the IEEE standard
  23. for Verilog. Briefly, the file contains two types of tokens:
  24. data and optional addresses. The tokens are separated by
  25. whitespace and comments. Comments may be single line or
  26. multiline, using syntax similar to C++. Addresses are
  27. specified by a leading "at" character (@) and are always
  28. hexadecimal strings. Data and addresses may contain
  29. underscore (_) characters.
  30. If no address is specified, the data is assumed to start at
  31. address 0. Similarly, if data exists before the first
  32. specified address, then that data is assumed to start at
  33. address 0.
  34. EXAMPLE
  35. @1000
  36. 01 ae 3f 45 12
  37. DESCRIPTION
  38. @1000 specifies the starting address for the memory data.
  39. The following characters describe the 5 bytes at 0x1000. */
  40. #include "sysdep.h"
  41. #include "bfd.h"
  42. #include "libbfd.h"
  43. #include "libiberty.h"
  44. #include "safe-ctype.h"
  45. /* Modified by obcopy.c
  46. Data width in bytes. */
  47. unsigned int VerilogDataWidth = 1;
  48. /* Macros for converting between hex and binary. */
  49. static const char digs[] = "0123456789ABCDEF";
  50. #define NIBBLE(x) hex_value (x)
  51. #define HEX(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
  52. #define TOHEX(d, x) \
  53. d[1] = digs[(x) & 0xf]; \
  54. d[0] = digs[((x) >> 4) & 0xf];
  55. /* When writing a verilog memory dump file, we write them in the order
  56. in which they appear in memory. This structure is used to hold them
  57. in memory. */
  58. struct verilog_data_list_struct
  59. {
  60. struct verilog_data_list_struct *next;
  61. bfd_byte * data;
  62. bfd_vma where;
  63. bfd_size_type size;
  64. };
  65. typedef struct verilog_data_list_struct verilog_data_list_type;
  66. /* The verilog tdata information. */
  67. typedef struct verilog_data_struct
  68. {
  69. verilog_data_list_type *head;
  70. verilog_data_list_type *tail;
  71. }
  72. tdata_type;
  73. static bool
  74. verilog_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
  75. {
  76. if (arch != bfd_arch_unknown)
  77. return bfd_default_set_arch_mach (abfd, arch, mach);
  78. abfd->arch_info = & bfd_default_arch_struct;
  79. return true;
  80. }
  81. /* We have to save up all the outpu for a splurge before output. */
  82. static bool
  83. verilog_set_section_contents (bfd *abfd,
  84. sec_ptr section,
  85. const void * location,
  86. file_ptr offset,
  87. bfd_size_type bytes_to_do)
  88. {
  89. tdata_type *tdata = abfd->tdata.verilog_data;
  90. verilog_data_list_type *entry;
  91. entry = (verilog_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
  92. if (entry == NULL)
  93. return false;
  94. if (bytes_to_do
  95. && (section->flags & SEC_ALLOC)
  96. && (section->flags & SEC_LOAD))
  97. {
  98. bfd_byte *data;
  99. data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
  100. if (data == NULL)
  101. return false;
  102. memcpy ((void *) data, location, (size_t) bytes_to_do);
  103. entry->data = data;
  104. entry->where = section->lma + offset;
  105. entry->size = bytes_to_do;
  106. /* Sort the records by address. Optimize for the common case of
  107. adding a record to the end of the list. */
  108. if (tdata->tail != NULL
  109. && entry->where >= tdata->tail->where)
  110. {
  111. tdata->tail->next = entry;
  112. entry->next = NULL;
  113. tdata->tail = entry;
  114. }
  115. else
  116. {
  117. verilog_data_list_type **look;
  118. for (look = &tdata->head;
  119. *look != NULL && (*look)->where < entry->where;
  120. look = &(*look)->next)
  121. ;
  122. entry->next = *look;
  123. *look = entry;
  124. if (entry->next == NULL)
  125. tdata->tail = entry;
  126. }
  127. }
  128. return true;
  129. }
  130. static bool
  131. verilog_write_address (bfd *abfd, bfd_vma address)
  132. {
  133. char buffer[20];
  134. char *dst = buffer;
  135. bfd_size_type wrlen;
  136. /* Write the address. */
  137. *dst++ = '@';
  138. #ifdef BFD64
  139. if (address >= (bfd_vma)1 << 32)
  140. {
  141. TOHEX (dst, (address >> 56));
  142. dst += 2;
  143. TOHEX (dst, (address >> 48));
  144. dst += 2;
  145. TOHEX (dst, (address >> 40));
  146. dst += 2;
  147. TOHEX (dst, (address >> 32));
  148. dst += 2;
  149. }
  150. #endif
  151. TOHEX (dst, (address >> 24));
  152. dst += 2;
  153. TOHEX (dst, (address >> 16));
  154. dst += 2;
  155. TOHEX (dst, (address >> 8));
  156. dst += 2;
  157. TOHEX (dst, (address));
  158. dst += 2;
  159. *dst++ = '\r';
  160. *dst++ = '\n';
  161. wrlen = dst - buffer;
  162. return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
  163. }
  164. /* Write a record of type, of the supplied number of bytes. The
  165. supplied bytes and length don't have a checksum. That's worked
  166. out here. */
  167. static bool
  168. verilog_write_record (bfd *abfd,
  169. const bfd_byte *data,
  170. const bfd_byte *end)
  171. {
  172. char buffer[52];
  173. const bfd_byte *src = data;
  174. char *dst = buffer;
  175. bfd_size_type wrlen;
  176. /* Paranoia - check that we will not overflow "buffer". */
  177. if (((end - data) * 2) /* Number of hex characters we want to emit. */
  178. + ((end - data) / VerilogDataWidth) /* Number of spaces we want to emit. */
  179. + 2 /* The carriage return & line feed characters. */
  180. > (long) sizeof (buffer))
  181. {
  182. /* FIXME: Should we generate an error message ? */
  183. return false;
  184. }
  185. /* Write the data.
  186. FIXME: Under some circumstances we can emit a space at the end of
  187. the line. This is not really necessary, but catching these cases
  188. would make the code more complicated. */
  189. if (VerilogDataWidth == 1)
  190. {
  191. for (src = data; src < end;)
  192. {
  193. TOHEX (dst, *src);
  194. dst += 2;
  195. src ++;
  196. if (src < end)
  197. *dst++ = ' ';
  198. }
  199. }
  200. else if (bfd_little_endian (abfd))
  201. {
  202. /* If the input byte stream contains:
  203. 05 04 03 02 01 00
  204. and VerilogDataWidth is 4 then we want to emit:
  205. 02030405 0001 */
  206. int i;
  207. for (src = data; src < (end - VerilogDataWidth); src += VerilogDataWidth)
  208. {
  209. for (i = VerilogDataWidth - 1; i >= 0; i--)
  210. {
  211. TOHEX (dst, src[i]);
  212. dst += 2;
  213. }
  214. *dst++ = ' ';
  215. }
  216. /* Emit any remaining bytes. Be careful not to read beyond "end". */
  217. while (end > src)
  218. {
  219. -- end;
  220. TOHEX (dst, *end);
  221. dst += 2;
  222. }
  223. }
  224. else
  225. {
  226. for (src = data; src < end;)
  227. {
  228. TOHEX (dst, *src);
  229. dst += 2;
  230. ++ src;
  231. if ((src - data) % VerilogDataWidth == 0)
  232. *dst++ = ' ';
  233. }
  234. }
  235. *dst++ = '\r';
  236. *dst++ = '\n';
  237. wrlen = dst - buffer;
  238. return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
  239. }
  240. static bool
  241. verilog_write_section (bfd *abfd,
  242. tdata_type *tdata ATTRIBUTE_UNUSED,
  243. verilog_data_list_type *list)
  244. {
  245. unsigned int octets_written = 0;
  246. bfd_byte *location = list->data;
  247. verilog_write_address (abfd, list->where);
  248. while (octets_written < list->size)
  249. {
  250. unsigned int octets_this_chunk = list->size - octets_written;
  251. if (octets_this_chunk > 16)
  252. octets_this_chunk = 16;
  253. if (! verilog_write_record (abfd,
  254. location,
  255. location + octets_this_chunk))
  256. return false;
  257. octets_written += octets_this_chunk;
  258. location += octets_this_chunk;
  259. }
  260. return true;
  261. }
  262. static bool
  263. verilog_write_object_contents (bfd *abfd)
  264. {
  265. tdata_type *tdata = abfd->tdata.verilog_data;
  266. verilog_data_list_type *list;
  267. /* Now wander though all the sections provided and output them. */
  268. list = tdata->head;
  269. while (list != (verilog_data_list_type *) NULL)
  270. {
  271. if (! verilog_write_section (abfd, tdata, list))
  272. return false;
  273. list = list->next;
  274. }
  275. return true;
  276. }
  277. /* Initialize by filling in the hex conversion array. */
  278. static void
  279. verilog_init (void)
  280. {
  281. static bool inited = false;
  282. if (! inited)
  283. {
  284. inited = true;
  285. hex_init ();
  286. }
  287. }
  288. /* Set up the verilog tdata information. */
  289. static bool
  290. verilog_mkobject (bfd *abfd)
  291. {
  292. tdata_type *tdata;
  293. verilog_init ();
  294. tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  295. if (tdata == NULL)
  296. return false;
  297. abfd->tdata.verilog_data = tdata;
  298. tdata->head = NULL;
  299. tdata->tail = NULL;
  300. return true;
  301. }
  302. #define verilog_close_and_cleanup _bfd_generic_close_and_cleanup
  303. #define verilog_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  304. #define verilog_new_section_hook _bfd_generic_new_section_hook
  305. #define verilog_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
  306. #define verilog_bfd_is_local_label_name bfd_generic_is_local_label_name
  307. #define verilog_get_lineno _bfd_nosymbols_get_lineno
  308. #define verilog_find_nearest_line _bfd_nosymbols_find_nearest_line
  309. #define verilog_find_inliner_info _bfd_nosymbols_find_inliner_info
  310. #define verilog_make_empty_symbol _bfd_generic_make_empty_symbol
  311. #define verilog_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  312. #define verilog_read_minisymbols _bfd_generic_read_minisymbols
  313. #define verilog_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  314. #define verilog_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  315. #define verilog_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  316. #define verilog_bfd_relax_section bfd_generic_relax_section
  317. #define verilog_bfd_gc_sections bfd_generic_gc_sections
  318. #define verilog_bfd_merge_sections bfd_generic_merge_sections
  319. #define verilog_bfd_is_group_section bfd_generic_is_group_section
  320. #define verilog_bfd_group_name bfd_generic_group_name
  321. #define verilog_bfd_discard_group bfd_generic_discard_group
  322. #define verilog_section_already_linked _bfd_generic_section_already_linked
  323. #define verilog_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  324. #define verilog_bfd_link_add_symbols _bfd_generic_link_add_symbols
  325. #define verilog_bfd_link_just_syms _bfd_generic_link_just_syms
  326. #define verilog_bfd_final_link _bfd_generic_final_link
  327. #define verilog_bfd_link_split_section _bfd_generic_link_split_section
  328. const bfd_target verilog_vec =
  329. {
  330. "verilog", /* Name. */
  331. bfd_target_verilog_flavour,
  332. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  333. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  334. (HAS_RELOC | EXEC_P | /* Object flags. */
  335. HAS_LINENO | HAS_DEBUG |
  336. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  337. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  338. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  339. 0, /* Leading underscore. */
  340. ' ', /* AR_pad_char. */
  341. 16, /* AR_max_namelen. */
  342. 0, /* match priority. */
  343. TARGET_KEEP_UNUSED_SECTION_SYMBOLS, /* keep unused section symbols. */
  344. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  345. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  346. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  347. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  348. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  349. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Hdrs. */
  350. {
  351. _bfd_dummy_target,
  352. _bfd_dummy_target,
  353. _bfd_dummy_target,
  354. _bfd_dummy_target,
  355. },
  356. {
  357. _bfd_bool_bfd_false_error,
  358. verilog_mkobject,
  359. _bfd_bool_bfd_false_error,
  360. _bfd_bool_bfd_false_error,
  361. },
  362. { /* bfd_write_contents. */
  363. _bfd_bool_bfd_false_error,
  364. verilog_write_object_contents,
  365. _bfd_bool_bfd_false_error,
  366. _bfd_bool_bfd_false_error,
  367. },
  368. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  369. BFD_JUMP_TABLE_COPY (_bfd_generic),
  370. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  371. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  372. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  373. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  374. BFD_JUMP_TABLE_WRITE (verilog),
  375. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  376. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  377. NULL,
  378. NULL
  379. };