1 /* BFD back-end for verilog hex memory dump files.
2 Copyright (C) 2009-2025 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 /* Modified by obcopy.c
67 enum bfd_endian VerilogDataEndianness
= BFD_ENDIAN_UNKNOWN
;
69 /* Macros for converting between hex and binary. */
71 static const char digs
[] = "0123456789ABCDEF";
73 #define NIBBLE(x) hex_value (x)
74 #define HEX(buffer) ((NIBBLE ((buffer)[0]) << 4) + NIBBLE ((buffer)[1]))
76 d[1] = digs[(x) & 0xf]; \
77 d[0] = digs[((x) >> 4) & 0xf];
79 /* When writing a verilog memory dump file, we write them in the order
80 in which they appear in memory. This structure is used to hold them
83 struct verilog_data_list_struct
85 struct verilog_data_list_struct
*next
;
91 typedef struct verilog_data_list_struct verilog_data_list_type
;
93 /* The verilog tdata information. */
95 typedef struct verilog_data_struct
97 verilog_data_list_type
*head
;
98 verilog_data_list_type
*tail
;
103 verilog_set_arch_mach (bfd
*abfd
, enum bfd_architecture arch
, unsigned long mach
)
105 if (arch
!= bfd_arch_unknown
)
106 return bfd_default_set_arch_mach (abfd
, arch
, mach
);
108 abfd
->arch_info
= & bfd_default_arch_struct
;
112 /* We have to save up all the output for a splurge before output. */
115 verilog_set_section_contents (bfd
*abfd
,
117 const void * location
,
119 bfd_size_type bytes_to_do
)
121 tdata_type
*tdata
= abfd
->tdata
.verilog_data
;
122 verilog_data_list_type
*entry
;
124 entry
= (verilog_data_list_type
*) bfd_alloc (abfd
, sizeof (* entry
));
129 && (section
->flags
& SEC_ALLOC
)
130 && (section
->flags
& SEC_LOAD
))
134 data
= (bfd_byte
*) bfd_alloc (abfd
, bytes_to_do
);
137 memcpy ((void *) data
, location
, (size_t) bytes_to_do
);
140 entry
->where
= section
->lma
+ offset
;
141 entry
->size
= bytes_to_do
;
143 /* Sort the records by address. Optimize for the common case of
144 adding a record to the end of the list. */
145 if (tdata
->tail
!= NULL
146 && entry
->where
>= tdata
->tail
->where
)
148 tdata
->tail
->next
= entry
;
154 verilog_data_list_type
**look
;
156 for (look
= &tdata
->head
;
157 *look
!= NULL
&& (*look
)->where
< entry
->where
;
158 look
= &(*look
)->next
)
162 if (entry
->next
== NULL
)
170 verilog_write_address (bfd
*abfd
, bfd_vma address
)
176 /* Write the address. */
179 if (address
>= (bfd_vma
)1 << 32)
181 TOHEX (dst
, (address
>> 56));
183 TOHEX (dst
, (address
>> 48));
185 TOHEX (dst
, (address
>> 40));
187 TOHEX (dst
, (address
>> 32));
191 TOHEX (dst
, (address
>> 24));
193 TOHEX (dst
, (address
>> 16));
195 TOHEX (dst
, (address
>> 8));
197 TOHEX (dst
, (address
));
201 wrlen
= dst
- buffer
;
203 return bfd_write (buffer
, wrlen
, abfd
) == wrlen
;
206 /* Write a record of type, of the supplied number of bytes. The
207 supplied bytes and length don't have a checksum. That's worked
211 verilog_write_record (bfd
*abfd
,
212 const bfd_byte
*data
,
216 const bfd_byte
*src
= data
;
220 /* Paranoia - check that we will not overflow "buffer". */
221 if (((end
- data
) * 2) /* Number of hex characters we want to emit. */
222 + ((end
- data
) / VerilogDataWidth
) /* Number of spaces we want to emit. */
223 + 2 /* The carriage return & line feed characters. */
224 > (long) sizeof (buffer
))
226 /* FIXME: Should we generate an error message ? */
231 FIXME: Under some circumstances we can emit a space at the end of
232 the line. This is not really necessary, but catching these cases
233 would make the code more complicated. */
234 if (VerilogDataWidth
== 1)
236 for (src
= data
; src
< end
;)
245 else if ((VerilogDataEndianness
== BFD_ENDIAN_UNKNOWN
&& bfd_little_endian (abfd
)) /* FIXME: Can this happen ? */
246 || (VerilogDataEndianness
== BFD_ENDIAN_LITTLE
))
248 /* If the input byte stream contains:
250 and VerilogDataWidth is 4 then we want to emit:
254 for (src
= data
; src
< (end
- VerilogDataWidth
); src
+= VerilogDataWidth
)
256 for (i
= VerilogDataWidth
- 1; i
>= 0; i
--)
264 /* Emit any remaining bytes. Be careful not to read beyond "end". */
272 /* FIXME: Should padding bytes be inserted here ? */
274 else /* Big endian output. */
276 for (src
= data
; src
< end
;)
281 if ((src
- data
) % VerilogDataWidth
== 0)
284 /* FIXME: Should padding bytes be inserted here ? */
289 wrlen
= dst
- buffer
;
291 return bfd_write (buffer
, wrlen
, abfd
) == wrlen
;
295 verilog_write_section (bfd
*abfd
,
296 tdata_type
*tdata ATTRIBUTE_UNUSED
,
297 verilog_data_list_type
*list
)
299 unsigned int octets_written
= 0;
300 bfd_byte
*location
= list
->data
;
302 /* Insist that the starting address is a multiple of the data width. */
303 if (list
->where
% VerilogDataWidth
)
305 bfd_set_error (bfd_error_invalid_operation
);
309 verilog_write_address (abfd
, list
->where
/ VerilogDataWidth
);
310 while (octets_written
< list
->size
)
312 unsigned int octets_this_chunk
= list
->size
- octets_written
;
314 if (octets_this_chunk
> 16)
315 octets_this_chunk
= 16;
317 if (! verilog_write_record (abfd
,
319 location
+ octets_this_chunk
))
322 octets_written
+= octets_this_chunk
;
323 location
+= octets_this_chunk
;
330 verilog_write_object_contents (bfd
*abfd
)
332 tdata_type
*tdata
= abfd
->tdata
.verilog_data
;
333 verilog_data_list_type
*list
;
335 /* Now wander though all the sections provided and output them. */
338 while (list
!= (verilog_data_list_type
*) NULL
)
340 if (! verilog_write_section (abfd
, tdata
, list
))
347 /* Initialize by filling in the hex conversion array. */
352 static bool inited
= false;
361 /* Set up the verilog tdata information. */
364 verilog_mkobject (bfd
*abfd
)
370 tdata
= (tdata_type
*) bfd_alloc (abfd
, sizeof (tdata_type
));
374 abfd
->tdata
.verilog_data
= tdata
;
381 const bfd_target verilog_vec
=
383 "verilog", /* Name. */
384 bfd_target_verilog_flavour
,
385 BFD_ENDIAN_UNKNOWN
, /* Target byte order. */
386 BFD_ENDIAN_UNKNOWN
, /* Target headers byte order. */
387 EXEC_P
, /* Object flags. */
388 (SEC_CODE
| SEC_DATA
| SEC_ROM
| SEC_HAS_CONTENTS
389 | SEC_ALLOC
| SEC_LOAD
), /* Section flags. */
390 0, /* Leading underscore. */
391 ' ', /* AR_pad_char. */
392 16, /* AR_max_namelen. */
393 0, /* match priority. */
394 TARGET_KEEP_UNUSED_SECTION_SYMBOLS
, /* keep unused section symbols. */
395 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
396 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
397 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* Data. */
398 bfd_getb64
, bfd_getb_signed_64
, bfd_putb64
,
399 bfd_getb32
, bfd_getb_signed_32
, bfd_putb32
,
400 bfd_getb16
, bfd_getb_signed_16
, bfd_putb16
, /* Hdrs. */
409 _bfd_bool_bfd_false_error
,
411 _bfd_bool_bfd_false_error
,
412 _bfd_bool_bfd_false_error
,
414 { /* bfd_write_contents. */
415 _bfd_bool_bfd_false_error
,
416 verilog_write_object_contents
,
417 _bfd_bool_bfd_false_error
,
418 _bfd_bool_bfd_false_error
,
421 BFD_JUMP_TABLE_GENERIC (_bfd_generic
),
422 BFD_JUMP_TABLE_COPY (_bfd_generic
),
423 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
424 BFD_JUMP_TABLE_ARCHIVE (_bfd_noarchive
),
425 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
426 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
427 BFD_JUMP_TABLE_WRITE (verilog
),
428 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
429 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),