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