1 /* BFD back-end for verilog hex memory dump files.
2 Copyright (C) 2009-2019 Free Software Foundation, Inc.
3 Written by Anthony Green <green@moxielogic.com>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
24 Verilog hex memory file handling
28 Verilog hex memory files cannot hold anything but addresses
29 and data, so that's all that we implement.
31 The syntax of the text file is described in the IEEE standard
32 for Verilog. Briefly, the file contains two types of tokens:
33 data and optional addresses. The tokens are separated by
34 whitespace and comments. Comments may be single line or
35 multiline, using syntax similar to C++. Addresses are
36 specified by a leading "at" character (@) and are always
37 hexadecimal strings. Data and addresses may contain
38 underscore (_) characters.
40 If no address is specified, the data is assumed to start at
41 address 0. Similarly, if data exists before the first
42 specified address, then that data is assumed to start at
51 @1000 specifies the starting address for the memory data.
52 The following characters describe the 5 bytes at 0x1000. */
58 #include "libiberty.h"
59 #include "safe-ctype.h"
61 /* Modified by obcopy.c
62 Data width in bytes. */
63 unsigned int VerilogDataWidth = 1;
65 /* Macros for converting between hex and binary. */
67 static const char digs[] = "0123456789ABCDEF";
69 #define NIBBLE(x) hex_value (x)
70 #define HEX(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
72 d[1] = digs[(x) & 0xf]; \
73 d[0] = digs[((x) >> 4) & 0xf];
75 /* When writing a verilog memory dump file, we write them in the order
76 in which they appear in memory. This structure is used to hold them
79 struct verilog_data_list_struct
81 struct verilog_data_list_struct *next;
87 typedef struct verilog_data_list_struct verilog_data_list_type;
89 /* The verilog tdata information. */
91 typedef struct verilog_data_struct
93 verilog_data_list_type *head;
94 verilog_data_list_type *tail;
99 verilog_set_arch_mach (bfd *abfd, enum bfd_architecture arch, unsigned long mach)
101 if (arch != bfd_arch_unknown)
102 return bfd_default_set_arch_mach (abfd, arch, mach);
104 abfd->arch_info = & bfd_default_arch_struct;
108 /* We have to save up all the outpu for a splurge before output. */
111 verilog_set_section_contents (bfd *abfd,
113 const void * location,
115 bfd_size_type bytes_to_do)
117 tdata_type *tdata = abfd->tdata.verilog_data;
118 verilog_data_list_type *entry;
120 entry = (verilog_data_list_type *) bfd_alloc (abfd, sizeof (* entry));
125 && (section->flags & SEC_ALLOC)
126 && (section->flags & SEC_LOAD))
130 data = (bfd_byte *) bfd_alloc (abfd, bytes_to_do);
133 memcpy ((void *) data, location, (size_t) bytes_to_do);
136 entry->where = section->lma + offset;
137 entry->size = bytes_to_do;
139 /* Sort the records by address. Optimize for the common case of
140 adding a record to the end of the list. */
141 if (tdata->tail != NULL
142 && entry->where >= tdata->tail->where)
144 tdata->tail->next = entry;
150 verilog_data_list_type **look;
152 for (look = &tdata->head;
153 *look != NULL && (*look)->where < entry->where;
154 look = &(*look)->next)
158 if (entry->next == NULL)
166 verilog_write_address (bfd *abfd, bfd_vma address)
172 /* Write the address. */
174 TOHEX (dst, (address >> 24));
176 TOHEX (dst, (address >> 16));
178 TOHEX (dst, (address >> 8));
180 TOHEX (dst, (address));
184 wrlen = dst - buffer;
186 return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
189 /* Write a record of type, of the supplied number of bytes. The
190 supplied bytes and length don't have a checksum. That's worked
194 verilog_write_record (bfd *abfd,
195 const bfd_byte *data,
199 const bfd_byte *src = data;
203 /* Paranoia - check that we will not overflow "buffer". */
204 if (((end - data) * 2) /* Number of hex characters we want to emit. */
205 + ((end - data) / VerilogDataWidth) /* Number of spaces we want to emit. */
206 + 2 /* The carriage return & line feed characters. */
207 > (long) sizeof (buffer))
209 /* FIXME: Should we generate an error message ? */
214 FIXME: Under some circumstances we can emit a space at the end of
215 the line. This is not really necessary, but catching these cases
216 would make the code more complicated. */
217 if (VerilogDataWidth == 1)
219 for (src = data; src < end;)
228 else if (bfd_little_endian (abfd))
230 /* If the input byte stream contains:
232 and VerilogDataWidth is 4 then we want to emit:
236 for (src = data; src < (end - VerilogDataWidth); src += VerilogDataWidth)
238 for (i = VerilogDataWidth - 1; i >= 0; i--)
246 /* Emit any remaining bytes. Be careful not to read beyond "end". */
256 for (src = data; src < end;)
261 if ((src - data) % VerilogDataWidth == 0)
268 wrlen = dst - buffer;
270 return bfd_bwrite ((void *) buffer, wrlen, abfd) == wrlen;
274 verilog_write_section (bfd *abfd,
275 tdata_type *tdata ATTRIBUTE_UNUSED,
276 verilog_data_list_type *list)
278 unsigned int octets_written = 0;
279 bfd_byte *location = list->data;
281 verilog_write_address (abfd, list->where);
282 while (octets_written < list->size)
284 unsigned int octets_this_chunk = list->size - octets_written;
286 if (octets_this_chunk > 16)
287 octets_this_chunk = 16;
289 if (! verilog_write_record (abfd,
291 location + octets_this_chunk))
294 octets_written += octets_this_chunk;
295 location += octets_this_chunk;
302 verilog_write_object_contents (bfd *abfd)
304 tdata_type *tdata = abfd->tdata.verilog_data;
305 verilog_data_list_type *list;
307 /* Now wander though all the sections provided and output them. */
310 while (list != (verilog_data_list_type *) NULL)
312 if (! verilog_write_section (abfd, tdata, list))
319 /* Initialize by filling in the hex conversion array. */
324 static bfd_boolean inited = FALSE;
333 /* Set up the verilog tdata information. */
336 verilog_mkobject (bfd *abfd)
342 tdata = (tdata_type *) bfd_alloc (abfd, sizeof (tdata_type));
346 abfd->tdata.verilog_data = tdata;
353 #define verilog_close_and_cleanup _bfd_generic_close_and_cleanup
354 #define verilog_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
355 #define verilog_new_section_hook _bfd_generic_new_section_hook
356 #define verilog_bfd_is_target_special_symbol _bfd_bool_bfd_asymbol_false
357 #define verilog_bfd_is_local_label_name bfd_generic_is_local_label_name
358 #define verilog_get_lineno _bfd_nosymbols_get_lineno
359 #define verilog_find_nearest_line _bfd_nosymbols_find_nearest_line
360 #define verilog_find_inliner_info _bfd_nosymbols_find_inliner_info
361 #define verilog_make_empty_symbol _bfd_generic_make_empty_symbol
362 #define verilog_bfd_make_debug_symbol _bfd_nosymbols_bfd_make_debug_symbol
363 #define verilog_read_minisymbols _bfd_generic_read_minisymbols
364 #define verilog_minisymbol_to_symbol _bfd_generic_minisymbol_to_symbol
365 #define verilog_get_section_contents_in_window _bfd_generic_get_section_contents_in_window
366 #define verilog_bfd_get_relocated_section_contents bfd_generic_get_relocated_section_contents
367 #define verilog_bfd_relax_section bfd_generic_relax_section
368 #define verilog_bfd_gc_sections bfd_generic_gc_sections
369 #define verilog_bfd_merge_sections bfd_generic_merge_sections
370 #define verilog_bfd_is_group_section bfd_generic_is_group_section
371 #define verilog_bfd_discard_group bfd_generic_discard_group
372 #define verilog_section_already_linked _bfd_generic_section_already_linked
373 #define verilog_bfd_link_hash_table_create _bfd_generic_link_hash_table_create
374 #define verilog_bfd_link_add_symbols _bfd_generic_link_add_symbols
375 #define verilog_bfd_link_just_syms _bfd_generic_link_just_syms
376 #define verilog_bfd_final_link _bfd_generic_final_link
377 #define verilog_bfd_link_split_section _bfd_generic_link_split_section
379 const bfd_target verilog_vec =
381 "verilog", /* Name. */
382 bfd_target_verilog_flavour,
383 BFD_ENDIAN_UNKNOWN, /* Target byte order. */
384 BFD_ENDIAN_UNKNOWN, /* Target headers byte order. */
385 (HAS_RELOC | EXEC_P | /* Object flags. */
386 HAS_LINENO | HAS_DEBUG |
387 HAS_SYMS | HAS_LOCALS | WP_TEXT | D_PAGED),
388 (SEC_CODE | SEC_DATA | SEC_ROM | SEC_HAS_CONTENTS
389 | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* Section flags. */
390 0, /* Leading underscore. */
391 ' ', /* AR_pad_char. */
392 16, /* AR_max_namelen. */
393 0, /* match priority. */
394 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
395 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
396 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Data. */
397 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
398 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
399 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* Hdrs. */
408 _bfd_bool_bfd_false_error,
410 _bfd_bool_bfd_false_error,
411 _bfd_bool_bfd_false_error,
413 { /* bfd_write_contents. */
414 _bfd_bool_bfd_false_error,
415 verilog_write_object_contents,
416 _bfd_bool_bfd_false_error,
417 _bfd_bool_bfd_false_error,
420 BFD_JUMP_TABLE_GENERIC (_bfd_generic),
421 BFD_JUMP_TABLE_COPY (_bfd_generic),
422 BFD_JUMP_TABLE_CORE (_bfd_nocore),
423 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive),
424 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols),
425 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs),
426 BFD_JUMP_TABLE_WRITE (verilog),
427 BFD_JUMP_TABLE_LINK (_bfd_nolink),
428 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),