verilog.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /* BFD back-end for verilog hex memory dump files.
  2. Copyright (C) 2009-2015 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. /* Macros for converting between hex and binary. */
  46. static const char digs[] = "0123456789ABCDEF";
  47. #define NIBBLE(x) hex_value(x)
  48. #define HEX(buffer) ((NIBBLE ((buffer)[0])<<4) + NIBBLE ((buffer)[1]))
  49. #define TOHEX(d, x) \
  50. d[1] = digs[(x) & 0xf]; \
  51. d[0] = digs[((x) >> 4) & 0xf];
  52. /* When writing a verilog memory dump file, we write them in the order
  53. in which they appear in memory. This structure is used to hold them
  54. in memory. */
  55. struct verilog_data_list_struct
  56. {
  57. struct verilog_data_list_struct *next;
  58. bfd_byte * data;
  59. bfd_vma where;
  60. bfd_size_type size;
  61. };
  62. typedef struct verilog_data_list_struct verilog_data_list_type;
  63. /* The verilog tdata information. */
  64. typedef struct verilog_data_struct
  65. {
  66. verilog_data_list_type *head;
  67. verilog_data_list_type *tail;
  68. }
  69. tdata_type;
  70. static bfd_boolean
  71. verilog_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
  72. {
  73. if (arch != bfd_arch_unknown)
  74. return bfd_default_set_arch_mach (abfd, arch, mach);
  75. abfd->arch_info = & bfd_default_arch_struct;
  76. return TRUE;
  77. }
  78. /* We have to save up all the outpu for a splurge before output. */
  79. static bfd_boolean
  80. verilog_set_section_contents (bfd *abfd,
  81. sec_ptr section,
  82. const void * location,
  83. file_ptr offset,
  84. bfd_size_type bytes_to_do)
  85. {
  86. tdata_type *tdata = abfd->tdata.verilog_data;
  87. verilog_data_list_type *entry;
  88. entry = (verilog_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
  89. if (entry == NULL)
  90. return FALSE;
  91. if (bytes_to_do
  92. && (section->flags & SEC_ALLOC)
  93. && (section->flags & SEC_LOAD))
  94. {
  95. bfd_byte *data;
  96. data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
  97. if (data == NULL)
  98. return FALSE;
  99. memcpy ((void *) data, location, (size_t) bytes_to_do);
  100. entry->data = data;
  101. entry->where = section->lma + offset;
  102. entry->size = bytes_to_do;
  103. /* Sort the records by address. Optimize for the common case of
  104. adding a record to the end of the list. */
  105. if (tdata->tail != NULL
  106. && entry->where >= tdata->tail->where)
  107. {
  108. tdata->tail->next = entry;
  109. entry->next = NULL;
  110. tdata->tail = entry;
  111. }
  112. else
  113. {
  114. verilog_data_list_type **look;
  115. for (look = &tdata->head;
  116. *look != NULL && (*look)->where < entry->where;
  117. look = &(*look)->next)
  118. ;
  119. entry->next = *look;
  120. *look = entry;
  121. if (entry->next == NULL)
  122. tdata->tail = entry;
  123. }
  124. }
  125. return TRUE;
  126. }
  127. static bfd_boolean
  128. verilog_write_address (bfd *abfd, bfd_vma address)
  129. {
  130. char buffer[12];
  131. char *dst = buffer;
  132. bfd_size_type wrlen;
  133. /* Write the address. */
  134. *dst++ = '@';
  135. TOHEX (dst, (address >> 24));
  136. dst += 2;
  137. TOHEX (dst, (address >> 16));
  138. dst += 2;
  139. TOHEX (dst, (address >> 8));
  140. dst += 2;
  141. TOHEX (dst, (address));
  142. dst += 2;
  143. *dst++ = '\r';
  144. *dst++ = '\n';
  145. wrlen = dst - buffer;
  146. return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
  147. }
  148. /* Write a record of type, of the supplied number of bytes. The
  149. supplied bytes and length don't have a checksum. That's worked out
  150. here. */
  151. static bfd_boolean
  152. verilog_write_record (bfd *abfd,
  153. const bfd_byte *data,
  154. const bfd_byte *end)
  155. {
  156. char buffer[50];
  157. const bfd_byte *src = data;
  158. char *dst = buffer;
  159. bfd_size_type wrlen;
  160. /* Write the data. */
  161. for (src = data; src < end; src++)
  162. {
  163. TOHEX (dst, *src);
  164. dst += 2;
  165. *dst++ = ' ';
  166. }
  167. *dst++ = '\r';
  168. *dst++ = '\n';
  169. wrlen = dst - buffer;
  170. return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
  171. }
  172. static bfd_boolean
  173. verilog_write_section (bfd *abfd,
  174. tdata_type *tdata ATTRIBUTE_UNUSED,
  175. verilog_data_list_type *list)
  176. {
  177. unsigned int octets_written = 0;
  178. bfd_byte *location = list->data;
  179. verilog_write_address (abfd, list->where);
  180. while (octets_written < list->size)
  181. {
  182. unsigned int octets_this_chunk = list->size - octets_written;
  183. if (octets_this_chunk > 16)
  184. octets_this_chunk = 16;
  185. if (! verilog_write_record (abfd,
  186. location,
  187. location + octets_this_chunk))
  188. return FALSE;
  189. octets_written += octets_this_chunk;
  190. location += octets_this_chunk;
  191. }
  192. return TRUE;
  193. }
  194. static bfd_boolean
  195. verilog_write_object_contents (bfd *abfd)
  196. {
  197. tdata_type *tdata = abfd->tdata.verilog_data;
  198. verilog_data_list_type *list;
  199. /* Now wander though all the sections provided and output them. */
  200. list = tdata->head;
  201. while (list != (verilog_data_list_type *) NULL)
  202. {
  203. if (! verilog_write_section (abfd, tdata, list))
  204. return FALSE;
  205. list = list->next;
  206. }
  207. return TRUE;
  208. }
  209. /* Initialize by filling in the hex conversion array. */
  210. static void
  211. verilog_init (void)
  212. {
  213. static bfd_boolean inited = FALSE;
  214. if (! inited)
  215. {
  216. inited = TRUE;
  217. hex_init ();
  218. }
  219. }
  220. /* Set up the verilog tdata information. */
  221. static bfd_boolean
  222. verilog_mkobject (bfd *abfd)
  223. {
  224. tdata_type *tdata;
  225. verilog_init ();
  226. tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
  227. if (tdata == NULL)
  228. return FALSE;
  229. abfd->tdata.verilog_data = tdata;
  230. tdata->head = NULL;
  231. tdata->tail = NULL;
  232. return TRUE;
  233. }
  234. #define verilog_close_and_cleanup _bfd_generic_close_and_cleanup
  235. #define verilog_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
  236. #define verilog_new_section_hook _bfd_generic_new_section_hook
  237. #define verilog_bfd_is_target_special_symbol ((bfd_boolean (*) (bfd *, asymbol *)) bfd_false)
  238. #define verilog_bfd_is_local_label_name bfd_generic_is_local_label_name
  239. #define verilog_get_lineno _bfd_nosymbols_get_lineno
  240. #define verilog_find_nearest_line _bfd_nosymbols_find_nearest_line
  241. #define verilog_find_inliner_info _bfd_nosymbols_find_inliner_info
  242. #define verilog_make_empty_symbol _bfd_generic_make_empty_symbol
  243. #define verilog_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
  244. #define verilog_read_minisymbols _bfd_generic_read_minisymbols
  245. #define verilog_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
  246. #define verilog_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
  247. #define verilog_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
  248. #define verilog_bfd_relax_section bfd_generic_relax_section
  249. #define verilog_bfd_gc_sections bfd_generic_gc_sections
  250. #define verilog_bfd_merge_sections bfd_generic_merge_sections
  251. #define verilog_bfd_is_group_section bfd_generic_is_group_section
  252. #define verilog_bfd_discard_group bfd_generic_discard_group
  253. #define verilog_section_already_linked _bfd_generic_section_already_linked
  254. #define verilog_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
  255. #define verilog_bfd_link_add_symbols _bfd_generic_link_add_symbols
  256. #define verilog_bfd_link_just_syms _bfd_generic_link_just_syms
  257. #define verilog_bfd_final_link _bfd_generic_final_link
  258. #define verilog_bfd_link_split_section _bfd_generic_link_split_section
  259. const bfd_target verilog_vec =
  260. {
  261. "verilog", /* Name. */
  262. bfd_target_verilog_flavour,
  263. BFD_ENDIAN_UNKNOWN, /* Target byte order. */
  264. BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
  265. (HAS_RELOC | EXEC_P | /* Object flags. */
  266. HAS_LINENO | HAS_DEBUG |
  267. HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
  268. (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
  269. | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
  270. 0, /* Leading underscore. */
  271. ' ', /* AR_pad_char. */
  272. 16, /* AR_max_namelen. */
  273. 0, /* match priority. */
  274. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  275. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  276. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
  277. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  278. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  279. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Hdrs. */
  280. {
  281. _bfd_dummy_target,
  282. _bfd_dummy_target,
  283. _bfd_dummy_target,
  284. _bfd_dummy_target,
  285. },
  286. {
  287. bfd_false,
  288. verilog_mkobject,
  289. bfd_false,
  290. bfd_false,
  291. },
  292. { /* bfd_write_contents. */
  293. bfd_false,
  294. verilog_write_object_contents,
  295. bfd_false,
  296. bfd_false,
  297. },
  298. BFD_JUMP_TABLE_GENERIC (_bfd_generic),
  299. BFD_JUMP_TABLE_COPY (_bfd_generic),
  300. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  301. BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
  302. BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
  303. BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
  304. BFD_JUMP_TABLE_WRITE (verilog),
  305. BFD_JUMP_TABLE_LINK (_bfd_nolink),
  306. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  307. NULL,
  308. NULL
  309. };