1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022-2023 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "coff/i386.h"
29 #include "coff/external.h"
30 #include "coff/internal.h"
50 uint32_t source_file_offset
;
57 struct string
*strings_head
;
58 struct string
*strings_tail
;
63 struct mod_source_files
66 struct string
**files
;
69 struct source_files_info
72 struct mod_source_files
*mods
;
77 struct type_entry
*next
;
80 bool has_udt_src_line
;
88 struct type_entry
*first
;
89 struct type_entry
*last
;
104 uint32_t num_entries
;
105 struct global
*first
;
117 static const uint32_t crc_table
[] =
119 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
120 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
121 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
122 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
123 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
124 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
125 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
126 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
127 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
128 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
129 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
130 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
131 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
132 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
133 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
134 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
135 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
136 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
137 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
138 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
139 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
140 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
141 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
142 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
143 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
144 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
145 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
146 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
147 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
148 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
149 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
150 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
151 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
152 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
153 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
154 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
155 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
156 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
157 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
158 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
159 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
160 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
161 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
164 /* Add a new stream to the PDB archive, and return its BFD. */
166 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
171 stream
= bfd_create (name
? name
: "", pdb
);
175 if (!bfd_make_writable (stream
))
181 if (!pdb
->archive_head
)
183 bfd_set_archive_head (pdb
, stream
);
188 bfd
*b
= pdb
->archive_head
;
192 while (b
->archive_next
)
198 b
->archive_next
= stream
;
207 /* Stream 0 ought to be a copy of the MSF directory from the last
208 time the PDB file was written. Because we don't do incremental
209 writes this isn't applicable to us, but we fill it with a dummy
210 value so as not to confuse radare. */
212 create_old_directory_stream (bfd
*pdb
)
215 char buf
[sizeof (uint32_t)];
217 stream
= add_stream (pdb
, NULL
, NULL
);
223 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
226 /* Calculate the hash of a given string. */
228 calc_hash (const char *data
, size_t len
)
235 hash
^= data
[1] << 8;
236 hash
^= data
[2] << 16;
237 hash
^= data
[3] << 24;
246 hash
^= data
[1] << 8;
256 hash
^= (hash
>> 11);
258 return hash
^ (hash
>> 16);
261 /* Stream 1 is the PDB info stream - see
262 https://llvm.org/docs/PDB/PdbStream.html. */
264 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
267 struct pdb_stream_70 h
;
268 uint32_t num_entries
, num_buckets
;
269 uint32_t names_length
, stream_num
;
270 char int_buf
[sizeof (uint32_t)];
278 struct hash_entry
**buckets
= NULL
;
282 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
283 bfd_putl32 (time (NULL
), &h
.signature
);
284 bfd_putl32 (1, &h
.age
);
286 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
287 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
288 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
289 memcpy (&h
.guid
[8], &guid
[8], 8);
291 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
294 /* Write hash list of named streams. This is a "rollover" hash, i.e.
295 if a bucket is filled an entry gets placed in the next free
299 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
301 if (strcmp (b
->filename
, ""))
305 num_buckets
= num_entries
* 2;
312 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
313 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
315 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
317 if (strcmp (b
->filename
, ""))
319 size_t len
= strlen (b
->filename
);
320 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
321 uint32_t bucket_num
= hash
% num_buckets
;
323 while (buckets
[bucket_num
])
327 if (bucket_num
== num_buckets
)
331 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
333 buckets
[bucket_num
]->offset
= names_length
;
334 buckets
[bucket_num
]->value
= stream_num
;
336 names_length
+= len
+ 1;
343 /* Write the strings list - the hash keys are indexes into this. */
345 bfd_putl32 (names_length
, int_buf
);
347 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
351 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
353 if (!strcmp (b
->filename
, ""))
356 size_t len
= strlen (b
->filename
) + 1;
358 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
362 /* Write the number of entries and buckets. */
364 bfd_putl32 (num_entries
, int_buf
);
366 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
370 bfd_putl32 (num_buckets
, int_buf
);
372 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
376 /* Write the present bitmap. */
378 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
380 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
384 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
388 for (unsigned int j
= 0; j
< 32; j
++)
390 if (i
+ j
>= num_buckets
)
397 bfd_putl32 (v
, int_buf
);
399 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
404 /* Write the (empty) deleted bitmap. */
406 bfd_putl32 (0, int_buf
);
408 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
412 /* Write the buckets. */
414 for (unsigned int i
= 0; i
< num_buckets
; i
++)
418 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
420 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
424 bfd_putl32 (buckets
[i
]->value
, int_buf
);
426 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
432 bfd_putl32 (0, int_buf
);
434 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
438 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
440 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
447 for (unsigned int i
= 0; i
< num_buckets
; i
++)
458 /* Calculate the CRC32 used for type hashes. */
460 crc32 (const uint8_t *data
, size_t len
)
466 crc
= (crc
>> 8) ^ crc_table
[(crc
& 0xff) ^ *data
];
475 /* Stream 2 is the type information (TPI) stream, and stream 4 is
476 the ID information (IPI) stream. They differ only in which records
477 go in which stream. */
479 populate_type_stream (bfd
*pdb
, bfd
*stream
, struct types
*types
)
481 struct pdb_tpi_stream_header h
;
482 struct type_entry
*e
;
483 uint32_t len
= 0, index_offset_len
, off
;
484 struct bfd
*hash_stream
= NULL
;
485 uint16_t hash_stream_index
;
487 static const uint32_t index_skip
= 0x2000;
491 index_offset_len
= 0;
495 uint32_t old_len
= len
;
497 len
+= sizeof (uint16_t) + bfd_getl16 (e
->data
);
499 if (old_len
== 0 || old_len
/ index_skip
!= len
/ index_skip
)
500 index_offset_len
+= sizeof (uint32_t) * 2;
505 /* Each type stream also has a stream which holds the hash value for each
506 type, along with a skip list to speed up searching. */
508 hash_stream
= add_stream (pdb
, "", &hash_stream_index
);
513 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
514 bfd_putl32 (sizeof (h
), &h
.header_size
);
515 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
516 bfd_putl32 (TPI_FIRST_INDEX
+ types
->num_types
, &h
.type_index_end
);
517 bfd_putl32 (len
, &h
.type_record_bytes
);
518 bfd_putl16 (hash_stream_index
, &h
.hash_stream_index
);
519 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
520 bfd_putl32 (sizeof (uint32_t), &h
.hash_key_size
);
521 bfd_putl32 (NUM_TPI_HASH_BUCKETS
, &h
.num_hash_buckets
);
522 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
523 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
524 &h
.hash_value_buffer_length
);
525 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
526 &h
.index_offset_buffer_offset
);
527 bfd_putl32 (index_offset_len
, &h
.index_offset_buffer_length
);
528 bfd_putl32 ((types
->num_types
* sizeof (uint32_t)) + index_offset_len
,
529 &h
.hash_adj_buffer_offset
);
530 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
532 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
535 /* Write the type definitions into the main stream, and the hashes
536 into the hash stream. The hashes have already been calculated
543 uint8_t buf
[sizeof (uint32_t)];
546 size
= bfd_getl16 (e
->data
);
548 if (bfd_bwrite (e
->data
, size
+ sizeof (uint16_t), stream
)
549 != size
+ sizeof (uint16_t))
552 bfd_putl32 (e
->cv_hash
% NUM_TPI_HASH_BUCKETS
, buf
);
554 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
555 != sizeof (uint32_t))
561 /* Write the index offsets, i.e. the skip list, into the hash stream. We
562 copy MSVC here by writing a new entry for every 8192 bytes. */
569 uint32_t old_off
= off
;
570 uint16_t size
= bfd_getl16 (e
->data
);
572 off
+= size
+ sizeof (uint16_t);
574 if (old_off
== 0 || old_off
/ index_skip
!= len
/ index_skip
)
576 uint8_t buf
[sizeof (uint32_t)];
578 bfd_putl32 (TPI_FIRST_INDEX
+ e
->index
, buf
);
580 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
581 != sizeof (uint32_t))
584 bfd_putl32 (old_off
, buf
);
586 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
587 != sizeof (uint32_t))
597 /* Return the PE architecture number for the image. */
599 get_arch_number (bfd
*abfd
)
601 switch (abfd
->arch_info
->arch
)
604 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
605 return IMAGE_FILE_MACHINE_AMD64
;
607 return IMAGE_FILE_MACHINE_I386
;
609 case bfd_arch_aarch64
:
610 return IMAGE_FILE_MACHINE_ARM64
;
617 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
618 section, and copy it to the module's symbol stream. */
620 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
621 struct string_table
*strings
, uint8_t *out
,
622 struct mod_source_files
*mod_source
)
624 uint8_t *orig_data
= data
;
625 uint32_t orig_size
= size
;
626 uint16_t num_files
= 0;
627 struct string
**strptr
;
629 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
630 out
+= sizeof (uint32_t);
632 bfd_putl32 (size
, out
);
633 out
+= sizeof (uint32_t);
635 /* Calculate the number of files, and check for any overflows. */
639 struct file_checksum
*fc
= (struct file_checksum
*) data
;
643 if (size
< sizeof (struct file_checksum
))
645 bfd_set_error (bfd_error_bad_value
);
649 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
653 bfd_set_error (bfd_error_bad_value
);
660 if (len
% sizeof (uint32_t))
661 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
667 bfd_set_error (bfd_error_bad_value
);
677 /* Add the files to mod_source, so that they'll appear in the source
683 uint16_t new_count
= num_files
+ mod_source
->files_count
;
685 mod_source
->files
= xrealloc (mod_source
->files
,
686 sizeof (struct string
*) * new_count
);
688 strptr
= mod_source
->files
+ mod_source
->files_count
;
690 mod_source
->files_count
+= num_files
;
693 /* Actually copy the data. */
700 struct file_checksum
*fc
= (struct file_checksum
*) data
;
704 struct string
*str
= NULL
;
706 string_off
= bfd_getl32 (&fc
->file_id
);
707 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
709 if (len
% sizeof (uint32_t))
710 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
714 /* Remap the "file ID", i.e. the offset in the module's string table,
715 so it points to the right place in the main string table. */
719 char *fn
= string_table
+ string_off
;
720 size_t fn_len
= strlen (fn
);
721 uint32_t hash
= calc_hash (fn
, fn_len
);
724 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
728 str
= (struct string
*) *slot
;
734 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
736 memcpy (out
, data
, len
+ padding
);
738 data
+= len
+ padding
;
739 size
-= len
+ padding
;
740 out
+= len
+ padding
;
746 /* Add a string to the strings table, if it's not already there. Returns its
747 offset within the string table. */
749 add_string (char *str
, size_t len
, struct string_table
*strings
)
751 uint32_t hash
= calc_hash (str
, len
);
755 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
759 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
761 s
= (struct string
*) *slot
;
765 s
->offset
= strings
->strings_len
;
766 s
->source_file_offset
= 0xffffffff;
768 memcpy (s
->s
, str
, len
);
770 if (strings
->strings_tail
)
771 strings
->strings_tail
->next
= s
;
773 strings
->strings_head
= s
;
775 strings
->strings_tail
= s
;
777 strings
->strings_len
+= len
+ 1;
781 s
= (struct string
*) *slot
;
787 /* Return the hash of an entry in the string table. */
789 hash_string_table_entry (const void *p
)
791 const struct string
*s
= (const struct string
*) p
;
796 /* Compare an entry in the string table with a string. */
798 eq_string_table_entry (const void *a
, const void *b
)
800 const struct string
*s1
= (const struct string
*) a
;
801 const char *s2
= (const char *) b
;
802 size_t s2_len
= strlen (s2
);
804 if (s2_len
!= s1
->len
)
807 return memcmp (s1
->s
, s2
, s2_len
) == 0;
810 /* Parse the string table within the .debug$S section. */
812 parse_string_table (bfd_byte
*data
, size_t size
,
813 struct string_table
*strings
)
817 size_t len
= strnlen ((char *) data
, size
);
819 add_string ((char *) data
, len
, strings
);
830 /* Remap a type reference within a CodeView symbol. */
832 remap_symbol_type (void *data
, struct type_entry
**map
, uint32_t num_types
)
834 uint32_t type
= bfd_getl32 (data
);
836 /* Ignore builtin types (those with IDs below 0x1000). */
837 if (type
< TPI_FIRST_INDEX
)
840 if (type
>= TPI_FIRST_INDEX
+ num_types
)
842 einfo (_("%P: CodeView symbol references out of range type %v\n"),
847 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
848 bfd_putl32 (type
, data
);
853 /* Add an entry into the globals stream. If it already exists, increase
856 add_globals_ref (struct globals
*glob
, bfd
*sym_rec_stream
, const char *name
,
857 size_t name_len
, uint8_t *data
, size_t len
)
863 slot
= htab_find_slot_with_hash (glob
->hashmap
, data
,
864 iterative_hash (data
, len
, 0), INSERT
);
873 *slot
= xmalloc (offsetof (struct global
, data
) + len
);
875 hash
= crc32 ((const uint8_t *) name
, name_len
);
876 hash
%= NUM_GLOBALS_HASH_BUCKETS
;
880 g
->offset
= bfd_tell (sym_rec_stream
);
883 memcpy (g
->data
, data
, len
);
888 glob
->last
->next
= g
;
894 return bfd_bwrite (data
, len
, sym_rec_stream
) == len
;
897 /* Find the end of the current scope within symbols data. */
899 find_end_of_scope (uint8_t *data
, uint32_t size
)
901 unsigned int scope_level
= 1;
904 len
= bfd_getl16 (data
) + sizeof (uint16_t);
913 if (size
< sizeof (uint32_t))
916 len
= bfd_getl16 (data
) + sizeof (uint16_t);
917 type
= bfd_getl16 (data
+ sizeof (uint16_t));
934 case S_INLINESITE_END
:
937 if (scope_level
== 0)
948 /* Return the size of an extended value parameter, as used in
951 extended_value_len (uint16_t type
)
974 /* Parse the symbols in a .debug$S section, and copy them to the module's
977 parse_symbols (uint8_t *data
, uint32_t size
, uint8_t **buf
,
978 struct type_entry
**map
, uint32_t num_types
,
979 bfd
*sym_rec_stream
, struct globals
*glob
, uint16_t mod_num
)
981 uint8_t *orig_buf
= *buf
;
982 unsigned int scope_level
= 0;
983 uint8_t *scope
= NULL
;
985 while (size
>= sizeof (uint16_t))
989 len
= bfd_getl16 (data
) + sizeof (uint16_t);
993 bfd_set_error (bfd_error_bad_value
);
997 type
= bfd_getl16 (data
+ sizeof (uint16_t));
1006 struct datasym
*d
= (struct datasym
*) data
;
1009 if (len
< offsetof (struct datasym
, name
))
1011 einfo (_("%P: warning: truncated CodeView record"
1012 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1013 bfd_set_error (bfd_error_bad_value
);
1017 if (scope_level
== 0)
1019 uint16_t section
= bfd_getl16 (&d
->section
);
1021 if (section
== 0) /* GC'd, ignore */
1026 strnlen (d
->name
, len
- offsetof (struct datasym
, name
));
1028 if (name_len
== len
- offsetof (struct datasym
, name
))
1030 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1031 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1033 bfd_set_error (bfd_error_bad_value
);
1037 if (!remap_symbol_type (&d
->type
, map
, num_types
))
1039 bfd_set_error (bfd_error_bad_value
);
1043 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1045 if (type
== S_LDATA32
|| type
== S_LTHREAD32
)
1047 memcpy (*buf
, d
, len
);
1051 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1052 not in function scope. */
1053 if (type
== S_GDATA32
|| type
== S_GTHREAD32
|| scope_level
== 0)
1055 if (!add_globals_ref (glob
, sym_rec_stream
, d
->name
,
1056 name_len
, data
, len
))
1068 struct procsym
*proc
= (struct procsym
*) data
;
1073 size_t ref_size
, padding
;
1076 if (len
< offsetof (struct procsym
, name
))
1078 einfo (_("%P: warning: truncated CodeView record"
1079 " S_GPROC32/S_LPROC32\n"));
1080 bfd_set_error (bfd_error_bad_value
);
1084 section
= bfd_getl16 (&proc
->section
);
1086 endptr
= find_end_of_scope (data
, size
);
1090 einfo (_("%P: warning: could not find end of"
1091 " S_GPROC32/S_LPROC32 record\n"));
1092 bfd_set_error (bfd_error_bad_value
);
1096 if (section
== 0) /* skip if GC'd */
1098 /* Skip to after S_END. */
1100 size
-= endptr
- data
;
1103 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1112 strnlen (proc
->name
, len
- offsetof (struct procsym
, name
));
1114 if (name_len
== len
- offsetof (struct procsym
, name
))
1116 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1117 " terminating zero\n"));
1118 bfd_set_error (bfd_error_bad_value
);
1122 if (type
== S_GPROC32_ID
|| type
== S_LPROC32_ID
)
1124 /* Transform into S_GPROC32 / S_LPROC32. */
1126 uint32_t t_idx
= bfd_getl32 (&proc
->type
);
1127 struct type_entry
*t
;
1130 if (t_idx
< TPI_FIRST_INDEX
1131 || t_idx
>= TPI_FIRST_INDEX
+ num_types
)
1133 einfo (_("%P: CodeView symbol references out of range"
1134 " type %v\n"), type
);
1135 bfd_set_error (bfd_error_bad_value
);
1139 t
= map
[t_idx
- TPI_FIRST_INDEX
];
1141 t_type
= bfd_getl16 (t
->data
+ sizeof (uint16_t));
1147 struct lf_func_id
*t_data
=
1148 (struct lf_func_id
*) t
->data
;
1150 /* Replace proc->type with function type. */
1152 memcpy (&proc
->type
, &t_data
->function_type
,
1160 struct lf_mfunc_id
*t_data
=
1161 (struct lf_mfunc_id
*) t
->data
;
1163 /* Replace proc->type with function type. */
1165 memcpy (&proc
->type
, &t_data
->function_type
,
1172 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1173 " referenced unknown type as ID\n"));
1174 bfd_set_error (bfd_error_bad_value
);
1178 /* Change record type. */
1180 if (type
== S_GPROC32_ID
)
1181 bfd_putl32 (S_GPROC32
, &proc
->kind
);
1183 bfd_putl32 (S_LPROC32
, &proc
->kind
);
1187 if (!remap_symbol_type (&proc
->type
, map
, num_types
))
1189 bfd_set_error (bfd_error_bad_value
);
1194 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1195 bfd_putl32 (end
, &proc
->end
);
1197 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1199 ref_size
= offsetof (struct refsym
, name
) + name_len
+ 1;
1201 if (ref_size
% sizeof (uint32_t))
1202 padding
= sizeof (uint32_t) - (ref_size
% sizeof (uint32_t));
1206 ref
= xmalloc (ref_size
+ padding
);
1208 bfd_putl16 (ref_size
+ padding
- sizeof (uint16_t), &ref
->size
);
1209 bfd_putl16 (type
== S_GPROC32
|| type
== S_GPROC32_ID
?
1210 S_PROCREF
: S_LPROCREF
, &ref
->kind
);
1211 bfd_putl32 (0, &ref
->sum_name
);
1212 bfd_putl32 (*buf
- orig_buf
+ sizeof (uint32_t),
1213 &ref
->symbol_offset
);
1214 bfd_putl16 (mod_num
+ 1, &ref
->mod
);
1216 memcpy (ref
->name
, proc
->name
, name_len
+ 1);
1218 memset (ref
->name
+ name_len
+ 1, 0, padding
);
1220 if (!add_globals_ref (glob
, sym_rec_stream
, proc
->name
, name_len
,
1221 (uint8_t *) ref
, ref_size
+ padding
))
1231 memcpy (*buf
, proc
, len
);
1241 struct udtsym
*udt
= (struct udtsym
*) data
;
1244 if (len
< offsetof (struct udtsym
, name
))
1246 einfo (_("%P: warning: truncated CodeView record"
1248 bfd_set_error (bfd_error_bad_value
);
1253 strnlen (udt
->name
, len
- offsetof (struct udtsym
, name
));
1255 if (name_len
== len
- offsetof (struct udtsym
, name
))
1257 einfo (_("%P: warning: name for S_UDT has no"
1258 " terminating zero\n"));
1259 bfd_set_error (bfd_error_bad_value
);
1263 if (!remap_symbol_type (&udt
->type
, map
, num_types
))
1265 bfd_set_error (bfd_error_bad_value
);
1269 /* S_UDT goes in the symbols stream if within a procedure,
1270 otherwise it goes in the globals stream. */
1271 if (scope_level
== 0)
1273 if (!add_globals_ref (glob
, sym_rec_stream
, udt
->name
,
1274 name_len
, data
, len
))
1279 memcpy (*buf
, udt
, len
);
1288 struct constsym
*c
= (struct constsym
*) data
;
1289 size_t name_len
, rec_size
;
1292 if (len
< offsetof (struct constsym
, name
))
1294 einfo (_("%P: warning: truncated CodeView record"
1296 bfd_set_error (bfd_error_bad_value
);
1300 rec_size
= offsetof (struct constsym
, name
);
1302 val
= bfd_getl16 (&c
->value
);
1304 /* If val >= 0x8000, actual value follows. */
1307 unsigned int param_len
= extended_value_len (val
);
1311 einfo (_("%P: warning: unhandled type %v within"
1312 " S_CONSTANT\n"), val
);
1313 bfd_set_error (bfd_error_bad_value
);
1317 rec_size
+= param_len
;
1321 strnlen ((const char *) data
+ rec_size
, len
- rec_size
);
1323 if (name_len
== len
- rec_size
)
1325 einfo (_("%P: warning: name for S_CONSTANT has no"
1326 " terminating zero\n"));
1327 bfd_set_error (bfd_error_bad_value
);
1331 if (!remap_symbol_type (&c
->type
, map
, num_types
))
1333 bfd_set_error (bfd_error_bad_value
);
1337 if (!add_globals_ref (glob
, sym_rec_stream
,
1338 (const char *) data
+ rec_size
, name_len
,
1346 case S_INLINESITE_END
:
1348 memcpy (*buf
, data
, len
);
1350 if (type
== S_PROC_ID_END
) /* transform to S_END */
1351 bfd_putl16 (S_END
, *buf
+ sizeof (uint16_t));
1353 /* Reset scope variable back to the address of the previous
1358 uint16_t scope_start_type
=
1359 bfd_getl16 (scope
+ sizeof (uint16_t));
1361 switch (scope_start_type
)
1365 parent
= bfd_getl32 (scope
+ offsetof (struct procsym
,
1370 parent
= bfd_getl32 (scope
+ offsetof (struct blocksym
,
1375 parent
= bfd_getl32 (scope
+ offsetof (struct inline_site
,
1380 parent
= bfd_getl32 (scope
+ offsetof (struct thunk
,
1385 einfo (_("%P: warning: unexpected CodeView scope start"
1386 " record %v\n"), scope_start_type
);
1387 bfd_set_error (bfd_error_bad_value
);
1394 scope
= orig_buf
+ parent
- sizeof (uint32_t);
1403 struct buildinfosym
*bi
= (struct buildinfosym
*) data
;
1405 if (len
< sizeof (struct buildinfosym
))
1407 einfo (_("%P: warning: truncated CodeView record"
1409 bfd_set_error (bfd_error_bad_value
);
1413 if (!remap_symbol_type (&bi
->type
, map
, num_types
))
1415 bfd_set_error (bfd_error_bad_value
);
1419 memcpy (*buf
, data
, len
);
1427 struct blocksym
*bl
= (struct blocksym
*) data
;
1431 if (len
< offsetof (struct blocksym
, name
))
1433 einfo (_("%P: warning: truncated CodeView record"
1435 bfd_set_error (bfd_error_bad_value
);
1439 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &bl
->parent
);
1441 endptr
= find_end_of_scope (data
, size
);
1445 einfo (_("%P: warning: could not find end of"
1446 " S_BLOCK32 record\n"));
1447 bfd_set_error (bfd_error_bad_value
);
1451 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1452 bfd_putl32 (end
, &bl
->end
);
1456 memcpy (*buf
, data
, len
);
1466 struct bprelsym
*bp
= (struct bprelsym
*) data
;
1468 if (len
< offsetof (struct bprelsym
, name
))
1470 einfo (_("%P: warning: truncated CodeView record"
1472 bfd_set_error (bfd_error_bad_value
);
1476 if (!remap_symbol_type (&bp
->type
, map
, num_types
))
1478 bfd_set_error (bfd_error_bad_value
);
1482 memcpy (*buf
, data
, len
);
1490 struct regsym
*reg
= (struct regsym
*) data
;
1492 if (len
< offsetof (struct regsym
, name
))
1494 einfo (_("%P: warning: truncated CodeView record"
1496 bfd_set_error (bfd_error_bad_value
);
1500 if (!remap_symbol_type (®
->type
, map
, num_types
))
1502 bfd_set_error (bfd_error_bad_value
);
1506 memcpy (*buf
, data
, len
);
1514 struct regrel
*rr
= (struct regrel
*) data
;
1516 if (len
< offsetof (struct regrel
, name
))
1518 einfo (_("%P: warning: truncated CodeView record"
1520 bfd_set_error (bfd_error_bad_value
);
1524 if (!remap_symbol_type (&rr
->type
, map
, num_types
))
1526 bfd_set_error (bfd_error_bad_value
);
1530 memcpy (*buf
, data
, len
);
1538 struct localsym
*l
= (struct localsym
*) data
;
1540 if (len
< offsetof (struct localsym
, name
))
1542 einfo (_("%P: warning: truncated CodeView record"
1544 bfd_set_error (bfd_error_bad_value
);
1548 if (!remap_symbol_type (&l
->type
, map
, num_types
))
1550 bfd_set_error (bfd_error_bad_value
);
1554 memcpy (*buf
, data
, len
);
1562 struct inline_site
*is
= (struct inline_site
*) data
;
1566 if (len
< offsetof (struct inline_site
, binary_annotations
))
1568 einfo (_("%P: warning: truncated CodeView record"
1569 " S_INLINESITE\n"));
1570 bfd_set_error (bfd_error_bad_value
);
1574 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &is
->parent
);
1576 endptr
= find_end_of_scope (data
, size
);
1580 einfo (_("%P: warning: could not find end of"
1581 " S_INLINESITE record\n"));
1582 bfd_set_error (bfd_error_bad_value
);
1586 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1587 bfd_putl32 (end
, &is
->end
);
1589 if (!remap_symbol_type (&is
->inlinee
, map
, num_types
))
1591 bfd_set_error (bfd_error_bad_value
);
1597 memcpy (*buf
, data
, len
);
1607 struct thunk
*th
= (struct thunk
*) data
;
1611 if (len
< offsetof (struct thunk
, name
))
1613 einfo (_("%P: warning: truncated CodeView record"
1615 bfd_set_error (bfd_error_bad_value
);
1619 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &th
->parent
);
1621 endptr
= find_end_of_scope (data
, size
);
1625 einfo (_("%P: warning: could not find end of"
1626 " S_THUNK32 record\n"));
1627 bfd_set_error (bfd_error_bad_value
);
1631 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1632 bfd_putl32 (end
, &th
->end
);
1636 memcpy (*buf
, data
, len
);
1644 case S_HEAPALLOCSITE
:
1646 struct heap_alloc_site
*has
= (struct heap_alloc_site
*) data
;
1648 if (len
< sizeof (struct heap_alloc_site
))
1650 einfo (_("%P: warning: truncated CodeView record"
1651 " S_HEAPALLOCSITE\n"));
1652 bfd_set_error (bfd_error_bad_value
);
1656 if (!remap_symbol_type (&has
->type
, map
, num_types
))
1658 bfd_set_error (bfd_error_bad_value
);
1662 memcpy (*buf
, data
, len
);
1668 case S_OBJNAME
: /* just copy */
1674 case S_DEFRANGE_REGISTER_REL
:
1675 case S_DEFRANGE_FRAMEPOINTER_REL
:
1676 case S_DEFRANGE_SUBFIELD_REGISTER
:
1677 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1678 case S_DEFRANGE_REGISTER
:
1679 memcpy (*buf
, data
, len
);
1684 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1685 bfd_set_error (bfd_error_bad_value
);
1696 /* For a given symbol subsection, work out how much space to allocate in the
1697 result module stream. This is different because we don't copy certain
1698 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1699 have been GC'd out. */
1701 calculate_symbols_size (uint8_t *data
, uint32_t size
, uint32_t *sym_size
)
1703 unsigned int scope_level
= 0;
1705 while (size
>= sizeof (uint32_t))
1707 uint16_t len
= bfd_getl16 (data
) + sizeof (uint16_t);
1708 uint16_t type
= bfd_getl16 (data
+ sizeof (uint16_t));
1715 struct datasym
*d
= (struct datasym
*) data
;
1718 if (len
< offsetof (struct datasym
, name
))
1720 einfo (_("%P: warning: truncated CodeView record"
1721 " S_LDATA32/S_LTHREAD32\n"));
1725 section
= bfd_getl16 (&d
->section
);
1727 /* copy if not GC'd or within function */
1728 if (scope_level
!= 0 || section
!= 0)
1735 /* Not copied into symbols stream. */
1743 struct procsym
*proc
= (struct procsym
*) data
;
1746 if (len
< offsetof (struct procsym
, name
))
1748 einfo (_("%P: warning: truncated CodeView record"
1749 " S_GPROC32/S_LPROC32\n"));
1753 section
= bfd_getl16 (&proc
->section
);
1761 uint8_t *endptr
= find_end_of_scope (data
, size
);
1765 einfo (_("%P: warning: could not find end of"
1766 " S_GPROC32/S_LPROC32 record\n"));
1770 /* Skip to after S_END. */
1772 size
-= endptr
- data
;
1775 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1789 if (scope_level
!= 0) /* only goes in symbols if local */
1793 case S_BLOCK32
: /* always copied */
1800 case S_END
: /* always copied */
1802 case S_INLINESITE_END
:
1807 case S_OBJNAME
: /* always copied */
1818 case S_DEFRANGE_REGISTER_REL
:
1819 case S_DEFRANGE_FRAMEPOINTER_REL
:
1820 case S_DEFRANGE_SUBFIELD_REGISTER
:
1821 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1822 case S_DEFRANGE_REGISTER
:
1823 case S_HEAPALLOCSITE
:
1828 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1839 /* Parse the .debug$S section within an object file. */
1841 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
1842 uint8_t **dataptr
, uint32_t *sizeptr
,
1843 struct mod_source_files
*mod_source
,
1844 bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
1845 struct type_entry
**map
, uint32_t num_types
,
1846 bfd
*sym_rec_stream
, struct globals
*glob
,
1849 bfd_byte
*data
= NULL
;
1851 uint32_t c13_size
= 0;
1852 char *string_table
= NULL
;
1853 uint8_t *buf
, *bufptr
, *symbuf
, *symbufptr
;
1854 uint32_t sym_size
= 0;
1856 if (!bfd_get_full_section_contents (mod
, s
, &data
))
1862 /* Resolve relocations. Addresses are stored within the .debug$S section as
1863 a .secidx, .secrel32 pair. */
1865 if (s
->flags
& SEC_RELOC
)
1867 struct internal_reloc
*relocs
;
1868 struct internal_syment
*symbols
;
1869 asection
**sectlist
;
1870 unsigned int syment_count
;
1872 struct external_syment
*ext
;
1874 syment_count
= obj_raw_syment_count (mod
);
1877 _bfd_coff_read_internal_relocs (mod
, s
, false, NULL
, true, NULL
);
1879 symbols
= xmalloc (sizeof (struct internal_syment
) * syment_count
);
1880 sectlist
= xmalloc (sizeof (asection
*) * syment_count
);
1882 ext
= (struct external_syment
*) (coff_data (mod
)->external_syms
);
1884 for (unsigned int i
= 0; i
< syment_count
; i
++)
1886 bfd_coff_swap_sym_in (mod
, &ext
[i
], &symbols
[i
]);
1891 for (asection
*sect
= mod
->sections
; sect
; sect
= sect
->next
)
1893 for (unsigned int i
= 0; i
< syment_count
; i
++)
1895 if (symbols
[i
].n_scnum
== sect_num
)
1902 if (!bfd_coff_relocate_section (abfd
, coff_data (abfd
)->link_info
, mod
,
1903 s
, data
, relocs
, symbols
, sectlist
))
1915 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
1921 off
= sizeof (uint32_t);
1923 /* calculate size */
1925 while (off
+ sizeof (uint32_t) <= s
->size
)
1927 uint32_t type
, size
;
1929 type
= bfd_getl32 (data
+ off
);
1931 off
+= sizeof (uint32_t);
1933 if (off
+ sizeof (uint32_t) > s
->size
)
1936 bfd_set_error (bfd_error_bad_value
);
1940 size
= bfd_getl32 (data
+ off
);
1942 off
+= sizeof (uint32_t);
1944 if (off
+ size
> s
->size
)
1947 bfd_set_error (bfd_error_bad_value
);
1953 case DEBUG_S_FILECHKSMS
:
1954 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1956 if (c13_size
% sizeof (uint32_t))
1957 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
1961 case DEBUG_S_STRINGTABLE
:
1962 parse_string_table (data
+ off
, size
, strings
);
1964 string_table
= (char *) data
+ off
;
1972 if (size
< sizeof (uint32_t) + sizeof (uint16_t))
1975 bfd_set_error (bfd_error_bad_value
);
1979 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
1981 /* Skip GC'd symbols. */
1984 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1986 if (c13_size
% sizeof (uint32_t))
1988 sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
1994 case DEBUG_S_SYMBOLS
:
1995 if (!calculate_symbols_size (data
+ off
, size
, &sym_size
))
1998 bfd_set_error (bfd_error_bad_value
);
2007 if (off
% sizeof (uint32_t))
2008 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2011 if (sym_size
% sizeof (uint32_t))
2012 sym_size
+= sizeof (uint32_t) - (sym_size
% sizeof (uint32_t));
2014 if (c13_size
== 0 && sym_size
== 0)
2024 buf
= xmalloc (c13_size
);
2029 symbuf
= xmalloc (sym_size
);
2032 off
= sizeof (uint32_t);
2034 while (off
+ sizeof (uint32_t) <= s
->size
)
2036 uint32_t type
, size
;
2038 type
= bfd_getl32 (data
+ off
);
2039 off
+= sizeof (uint32_t);
2041 size
= bfd_getl32 (data
+ off
);
2042 off
+= sizeof (uint32_t);
2046 case DEBUG_S_FILECHKSMS
:
2047 if (!copy_filechksms (data
+ off
, size
, string_table
,
2048 strings
, bufptr
, mod_source
))
2055 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
2063 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
2068 bfd_putl32 (type
, bufptr
);
2069 bufptr
+= sizeof (uint32_t);
2071 bfd_putl32 (size
, bufptr
);
2072 bufptr
+= sizeof (uint32_t);
2074 memcpy (bufptr
, data
+ off
, size
);
2081 case DEBUG_S_SYMBOLS
:
2082 if (!parse_symbols (data
+ off
, size
, &symbufptr
, map
, num_types
,
2083 sym_rec_stream
, glob
, mod_num
))
2095 if (off
% sizeof (uint32_t))
2096 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2105 /* Append the C13 info to what's already there, if the module has
2106 multiple .debug$S sections. */
2108 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
2109 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
2118 *sizeptr
+= c13_size
;
2125 *syms
= xrealloc (*syms
, *sym_byte_size
+ sym_size
);
2126 memcpy (*syms
+ *sym_byte_size
, symbuf
, sym_size
);
2135 *sym_byte_size
+= sym_size
;
2141 /* Remap the type number stored in data from the per-module numbering to
2142 that of the deduplicated output list. */
2144 remap_type (void *data
, struct type_entry
**map
,
2145 uint32_t type_num
, uint32_t num_types
)
2147 uint32_t type
= bfd_getl32 (data
);
2149 /* Ignore builtin types (those with IDs below 0x1000). */
2150 if (type
< TPI_FIRST_INDEX
)
2153 if (type
>= TPI_FIRST_INDEX
+ type_num
)
2155 einfo (_("%P: CodeView type %v references other type %v not yet "
2156 "declared\n"), TPI_FIRST_INDEX
+ type_num
, type
);
2160 if (type
>= TPI_FIRST_INDEX
+ num_types
)
2162 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2163 TPI_FIRST_INDEX
+ type_num
, type
);
2167 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
2168 bfd_putl32 (type
, data
);
2173 /* Determines whether the name of a struct, class, or union counts as
2174 "anonymous". Non-anonymous types have a hash based on just the name,
2175 rather than the whole structure. */
2177 is_name_anonymous (char *name
, size_t len
)
2179 static const char tag1
[] = "<unnamed-tag>";
2180 static const char tag2
[] = "__unnamed";
2181 static const char tag3
[] = "::<unnamed-tag>";
2182 static const char tag4
[] = "::__unnamed";
2184 if (len
== sizeof (tag1
) - 1 && !memcmp (name
, tag1
, sizeof (tag1
) - 1))
2187 if (len
== sizeof (tag2
) - 1 && !memcmp (name
, tag2
, sizeof (tag2
) - 1))
2190 if (len
>= sizeof (tag3
) - 1
2191 && !memcmp (name
+ len
- sizeof (tag3
) + 1, tag3
, sizeof (tag3
) - 1))
2194 if (len
>= sizeof (tag4
) - 1
2195 && !memcmp (name
+ len
- sizeof (tag4
) + 1, tag4
, sizeof (tag4
) - 1))
2201 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2202 give the source file and line number for each user-defined type that is
2203 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2204 which also includes the module number. */
2206 handle_udt_src_line (uint8_t *data
, uint16_t size
, struct type_entry
**map
,
2207 uint32_t type_num
, uint32_t num_types
,
2208 struct types
*ids
, uint16_t mod_num
,
2209 struct string_table
*strings
)
2211 struct lf_udt_src_line
*usl
= (struct lf_udt_src_line
*) data
;
2212 uint32_t orig_type
, source_file_type
;
2215 struct type_entry
*e
, *type_e
, *str_e
;
2216 struct lf_udt_mod_src_line
*umsl
;
2217 struct lf_string_id
*str
;
2218 uint32_t source_file_offset
;
2220 if (size
< sizeof (struct lf_udt_src_line
))
2222 einfo (_("%P: warning: truncated CodeView type record"
2223 " LF_UDT_SRC_LINE\n"));
2227 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2229 orig_type
= bfd_getl32 (&usl
->type
);
2231 if (orig_type
< TPI_FIRST_INDEX
||
2232 orig_type
>= TPI_FIRST_INDEX
+ num_types
||
2233 !map
[orig_type
- TPI_FIRST_INDEX
])
2235 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2236 " referred to unknown type %v\n"), orig_type
);
2240 type_e
= map
[orig_type
- TPI_FIRST_INDEX
];
2242 /* Skip if type already declared in other module. */
2243 if (type_e
->has_udt_src_line
)
2246 if (!remap_type (&usl
->type
, map
, type_num
, num_types
))
2249 /* Extract string from source_file_type. */
2251 source_file_type
= bfd_getl32 (&usl
->source_file_type
);
2253 if (source_file_type
< TPI_FIRST_INDEX
||
2254 source_file_type
>= TPI_FIRST_INDEX
+ num_types
||
2255 !map
[source_file_type
- TPI_FIRST_INDEX
])
2257 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2258 " referred to unknown string %v\n"), source_file_type
);
2262 str_e
= map
[source_file_type
- TPI_FIRST_INDEX
];
2264 if (bfd_getl16 (str_e
->data
+ sizeof (uint16_t)) != LF_STRING_ID
)
2266 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2267 " pointed to unexpected record type\n"));
2271 str
= (struct lf_string_id
*) str_e
->data
;
2273 /* Add string to string table. */
2275 source_file_offset
= add_string (str
->string
, strlen (str
->string
),
2278 /* Add LF_UDT_MOD_SRC_LINE entry. */
2280 size
= sizeof (struct lf_udt_mod_src_line
);
2282 e
= xmalloc (offsetof (struct type_entry
, data
) + size
);
2285 e
->index
= ids
->num_types
;
2286 e
->has_udt_src_line
= false;
2288 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2289 the crc32 used for type hashes elsewhere. */
2290 e
->cv_hash
= calc_hash ((char *) &usl
->type
, sizeof (uint32_t));
2292 type_e
->has_udt_src_line
= true;
2294 umsl
= (struct lf_udt_mod_src_line
*) e
->data
;
2296 bfd_putl16 (size
- sizeof (uint16_t), &umsl
->size
);
2297 bfd_putl16 (LF_UDT_MOD_SRC_LINE
, &umsl
->kind
);
2298 memcpy (&umsl
->type
, &usl
->type
, sizeof (uint32_t));
2299 bfd_putl32 (source_file_offset
, &umsl
->source_file_string
);
2300 memcpy (&umsl
->line_no
, &usl
->line_no
, sizeof (uint32_t));
2301 bfd_putl16 (mod_num
+ 1, &umsl
->module_no
);
2303 hash
= iterative_hash (e
->data
, size
, 0);
2305 slot
= htab_find_slot_with_hash (ids
->hashmap
, data
, hash
, INSERT
);
2315 einfo (_("%P: warning: duplicate CodeView type record "
2316 "LF_UDT_MOD_SRC_LINE\n"));
2323 ids
->last
->next
= e
;
2336 /* Parse a type definition in the .debug$T section. We remap the numbers
2337 of any referenced types, and if the type is not a duplicate of one
2338 already seen add it to types (for TPI types) or ids (for IPI types). */
2340 handle_type (uint8_t *data
, struct type_entry
**map
, uint32_t type_num
,
2341 uint32_t num_types
, struct types
*types
,
2342 struct types
*ids
, uint16_t mod_num
,
2343 struct string_table
*strings
)
2345 uint16_t size
, type
;
2348 bool other_hash
= false;
2353 size
= bfd_getl16 (data
) + sizeof (uint16_t);
2354 type
= bfd_getl16 (data
+ sizeof (uint16_t));
2360 struct lf_modifier
*mod
= (struct lf_modifier
*) data
;
2362 if (size
< offsetof (struct lf_modifier
, modifier
))
2364 einfo (_("%P: warning: truncated CodeView type record "
2369 if (!remap_type (&mod
->base_type
, map
, type_num
, num_types
))
2377 struct lf_pointer
*ptr
= (struct lf_pointer
*) data
;
2379 if (size
< offsetof (struct lf_pointer
, attributes
))
2381 einfo (_("%P: warning: truncated CodeView type record"
2386 if (!remap_type (&ptr
->base_type
, map
, type_num
, num_types
))
2394 struct lf_procedure
*proc
= (struct lf_procedure
*) data
;
2396 if (size
< sizeof (struct lf_procedure
))
2398 einfo (_("%P: warning: truncated CodeView type record"
2399 " LF_PROCEDURE\n"));
2403 if (!remap_type (&proc
->return_type
, map
, type_num
, num_types
))
2406 if (!remap_type (&proc
->arglist
, map
, type_num
, num_types
))
2414 struct lf_mfunction
*func
= (struct lf_mfunction
*) data
;
2416 if (size
< sizeof (struct lf_procedure
))
2418 einfo (_("%P: warning: truncated CodeView type record"
2419 " LF_MFUNCTION\n"));
2423 if (!remap_type (&func
->return_type
, map
, type_num
, num_types
))
2426 if (!remap_type (&func
->containing_class_type
, map
, type_num
,
2430 if (!remap_type (&func
->this_type
, map
, type_num
, num_types
))
2433 if (!remap_type (&func
->arglist
, map
, type_num
, num_types
))
2441 uint32_t num_entries
;
2442 struct lf_arglist
*al
= (struct lf_arglist
*) data
;
2444 if (size
< offsetof (struct lf_arglist
, args
))
2446 einfo (_("%P: warning: truncated CodeView type record"
2451 num_entries
= bfd_getl32 (&al
->num_entries
);
2453 if (size
< offsetof (struct lf_arglist
, args
)
2454 + (num_entries
* sizeof (uint32_t)))
2456 einfo (_("%P: warning: truncated CodeView type record"
2461 for (uint32_t i
= 0; i
< num_entries
; i
++)
2463 if (!remap_type (&al
->args
[i
], map
, type_num
, num_types
))
2472 uint16_t left
= size
- sizeof (uint16_t) - sizeof (uint16_t);
2473 uint8_t *ptr
= data
+ sizeof (uint16_t) + sizeof (uint16_t);
2479 if (left
< sizeof (uint16_t))
2481 einfo (_("%P: warning: truncated CodeView type record"
2482 " LF_FIELDLIST\n"));
2486 subtype
= bfd_getl16 (ptr
);
2492 struct lf_member
*mem
= (struct lf_member
*) ptr
;
2494 size_t name_len
, subtype_len
;
2496 if (left
< offsetof (struct lf_member
, name
))
2498 einfo (_("%P: warning: truncated CodeView type record"
2503 if (!remap_type (&mem
->type
, map
, type_num
, num_types
))
2506 subtype_len
= offsetof (struct lf_member
, name
);
2508 offset
= bfd_getl16 (&mem
->offset
);
2510 /* If offset >= 0x8000, actual value follows. */
2511 if (offset
>= 0x8000)
2513 unsigned int param_len
= extended_value_len (offset
);
2517 einfo (_("%P: warning: unhandled type %v within"
2518 " LF_MEMBER\n"), offset
);
2522 subtype_len
+= param_len
;
2524 if (left
< subtype_len
)
2526 einfo (_("%P: warning: truncated CodeView type record"
2533 strnlen ((char *) mem
+ subtype_len
, left
- subtype_len
);
2535 if (name_len
== left
- offsetof (struct lf_member
, name
))
2537 einfo (_("%P: warning: name for LF_MEMBER has no"
2538 " terminating zero\n"));
2544 subtype_len
+= name_len
;
2546 if (subtype_len
% 4 != 0)
2547 subtype_len
+= 4 - (subtype_len
% 4);
2549 if (left
< subtype_len
)
2551 einfo (_("%P: warning: truncated CodeView type record"
2552 " LF_FIELDLIST\n"));
2557 left
-= subtype_len
;
2564 struct lf_enumerate
*en
= (struct lf_enumerate
*) ptr
;
2565 size_t name_len
, subtype_len
;
2568 if (left
< offsetof (struct lf_enumerate
, name
))
2570 einfo (_("%P: warning: truncated CodeView type record"
2571 " LF_ENUMERATE\n"));
2575 subtype_len
= offsetof (struct lf_enumerate
, name
);
2577 val
= bfd_getl16 (&en
->value
);
2579 /* If val >= 0x8000, the actual value immediately follows. */
2582 unsigned int param_len
= extended_value_len (val
);
2586 einfo (_("%P: warning: unhandled type %v within"
2587 " LF_ENUMERATE\n"), val
);
2591 if (left
< subtype_len
+ param_len
)
2593 einfo (_("%P: warning: truncated CodeView type"
2594 " record LF_ENUMERATE\n"));
2598 subtype_len
+= param_len
;
2601 name_len
= strnlen ((char *) ptr
+ subtype_len
,
2602 left
- subtype_len
);
2604 if (name_len
== left
- offsetof (struct lf_enumerate
, name
))
2606 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2607 " terminating zero\n"));
2613 subtype_len
+= name_len
;
2615 if (subtype_len
% 4 != 0)
2616 subtype_len
+= 4 - (subtype_len
% 4);
2618 if (left
< subtype_len
)
2620 einfo (_("%P: warning: truncated CodeView type record"
2621 " LF_ENUMERATE\n"));
2626 left
-= subtype_len
;
2633 struct lf_index
*ind
= (struct lf_index
*) ptr
;
2635 if (left
< sizeof (struct lf_index
))
2637 einfo (_("%P: warning: truncated CodeView type record"
2642 if (!remap_type (&ind
->index
, map
, type_num
, num_types
))
2645 ptr
+= sizeof (struct lf_index
);
2646 left
-= sizeof (struct lf_index
);
2653 struct lf_onemethod
*meth
= (struct lf_onemethod
*) ptr
;
2654 size_t name_len
, subtype_len
;
2656 if (left
< offsetof (struct lf_onemethod
, name
))
2658 einfo (_("%P: warning: truncated CodeView type record"
2659 " LF_ONEMETHOD\n"));
2663 if (!remap_type (&meth
->method_type
, map
, type_num
,
2668 strnlen (meth
->name
,
2669 left
- offsetof (struct lf_onemethod
, name
));
2671 if (name_len
== left
- offsetof (struct lf_onemethod
, name
))
2673 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2674 " terminating zero\n"));
2680 subtype_len
= offsetof (struct lf_onemethod
, name
)
2683 if (subtype_len
% 4 != 0)
2684 subtype_len
+= 4 - (subtype_len
% 4);
2686 if (left
< subtype_len
)
2688 einfo (_("%P: warning: truncated CodeView type record"
2689 " LF_FIELDLIST\n"));
2694 left
-= subtype_len
;
2701 struct lf_method
*meth
= (struct lf_method
*) ptr
;
2702 size_t name_len
, subtype_len
;
2704 if (left
< offsetof (struct lf_method
, name
))
2706 einfo (_("%P: warning: truncated CodeView type record"
2711 if (!remap_type (&meth
->method_list
, map
, type_num
,
2716 strnlen (meth
->name
,
2717 left
- offsetof (struct lf_method
, name
));
2719 if (name_len
== left
- offsetof (struct lf_method
, name
))
2721 einfo (_("%P: warning: name for LF_METHOD has no"
2722 " terminating zero\n"));
2728 subtype_len
= offsetof (struct lf_method
, name
) + name_len
;
2730 if (subtype_len
% 4 != 0)
2731 subtype_len
+= 4 - (subtype_len
% 4);
2733 if (left
< subtype_len
)
2735 einfo (_("%P: warning: truncated CodeView type record"
2736 " LF_FIELDLIST\n"));
2741 left
-= subtype_len
;
2748 struct lf_bclass
*bc
= (struct lf_bclass
*) ptr
;
2752 if (left
< sizeof (struct lf_bclass
))
2754 einfo (_("%P: warning: truncated CodeView type record"
2759 if (!remap_type (&bc
->base_class_type
, map
, type_num
,
2763 subtype_len
= sizeof (struct lf_bclass
);
2765 offset
= bfd_getl16 (&bc
->offset
);
2767 /* If offset >= 0x8000, actual value follows. */
2768 if (offset
>= 0x8000)
2770 unsigned int param_len
= extended_value_len (offset
);
2774 einfo (_("%P: warning: unhandled type %v within"
2775 " LF_BCLASS\n"), offset
);
2779 subtype_len
+= param_len
;
2781 if (left
< subtype_len
)
2783 einfo (_("%P: warning: truncated CodeView type record"
2789 if (subtype_len
% 4 != 0)
2790 subtype_len
+= 4 - (subtype_len
% 4);
2792 if (left
< subtype_len
)
2794 einfo (_("%P: warning: truncated CodeView type record"
2800 left
-= subtype_len
;
2807 struct lf_vfunctab
*vft
= (struct lf_vfunctab
*) ptr
;
2809 if (left
< sizeof (struct lf_vfunctab
))
2811 einfo (_("%P: warning: truncated CodeView type record"
2816 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
2819 ptr
+= sizeof (struct lf_vfunctab
);
2820 left
-= sizeof (struct lf_vfunctab
);
2828 struct lf_vbclass
*vbc
= (struct lf_vbclass
*) ptr
;
2832 if (left
< sizeof (struct lf_vbclass
))
2834 einfo (_("%P: warning: truncated CodeView type record"
2835 " LF_VBCLASS/LF_IVBCLASS\n"));
2839 if (!remap_type (&vbc
->base_class_type
, map
, type_num
,
2843 if (!remap_type (&vbc
->virtual_base_pointer_type
, map
,
2844 type_num
, num_types
))
2847 subtype_len
= offsetof (struct lf_vbclass
,
2848 virtual_base_vbtable_offset
);
2850 offset
= bfd_getl16 (&vbc
->virtual_base_pointer_offset
);
2852 /* If offset >= 0x8000, actual value follows. */
2853 if (offset
>= 0x8000)
2855 unsigned int param_len
= extended_value_len (offset
);
2859 einfo (_("%P: warning: unhandled type %v within"
2860 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2864 subtype_len
+= param_len
;
2866 if (left
< subtype_len
)
2868 einfo (_("%P: warning: truncated CodeView type record"
2869 " LF_VBCLASS/LF_IVBCLASS\n"));
2874 offset
= bfd_getl16 ((char *)vbc
+ subtype_len
);
2875 subtype_len
+= sizeof (uint16_t);
2877 /* If offset >= 0x8000, actual value follows. */
2878 if (offset
>= 0x8000)
2880 unsigned int param_len
= extended_value_len (offset
);
2884 einfo (_("%P: warning: unhandled type %v within"
2885 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2889 subtype_len
+= param_len
;
2891 if (left
< subtype_len
)
2893 einfo (_("%P: warning: truncated CodeView type record"
2894 " LF_VBCLASS/LF_IVBCLASS\n"));
2899 if (subtype_len
% 4 != 0)
2900 subtype_len
+= 4 - (subtype_len
% 4);
2902 if (left
< subtype_len
)
2904 einfo (_("%P: warning: truncated CodeView type record"
2905 " LF_VBCLASS/LF_IVBCLASS\n"));
2910 left
-= subtype_len
;
2917 struct lf_static_member
*st
=
2918 (struct lf_static_member
*) ptr
;
2919 size_t name_len
, subtype_len
;
2921 if (left
< offsetof (struct lf_static_member
, name
))
2923 einfo (_("%P: warning: truncated CodeView type record"
2928 if (!remap_type (&st
->type
, map
, type_num
, num_types
))
2933 left
- offsetof (struct lf_static_member
, name
));
2935 if (name_len
== left
2936 - offsetof (struct lf_static_member
, name
))
2938 einfo (_("%P: warning: name for LF_STMEMBER has no"
2939 " terminating zero\n"));
2945 subtype_len
= offsetof (struct lf_static_member
, name
)
2948 if (subtype_len
% 4 != 0)
2949 subtype_len
+= 4 - (subtype_len
% 4);
2951 if (left
< subtype_len
)
2953 einfo (_("%P: warning: truncated CodeView type record"
2954 " LF_FIELDLIST\n"));
2959 left
-= subtype_len
;
2966 struct lf_nest_type
*nest
= (struct lf_nest_type
*) ptr
;
2967 size_t name_len
, subtype_len
;
2969 if (left
< offsetof (struct lf_nest_type
, name
))
2971 einfo (_("%P: warning: truncated CodeView type record"
2976 if (!remap_type (&nest
->type
, map
, type_num
, num_types
))
2980 strnlen (nest
->name
,
2981 left
- offsetof (struct lf_nest_type
, name
));
2983 if (name_len
== left
- offsetof (struct lf_nest_type
, name
))
2985 einfo (_("%P: warning: name for LF_NESTTYPE has no"
2986 " terminating zero\n"));
2992 subtype_len
= offsetof (struct lf_nest_type
, name
)
2995 if (subtype_len
% 4 != 0)
2996 subtype_len
+= 4 - (subtype_len
% 4);
2998 if (left
< subtype_len
)
3000 einfo (_("%P: warning: truncated CodeView type record"
3001 " LF_FIELDLIST\n"));
3006 left
-= subtype_len
;
3012 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3023 struct lf_bitfield
*bf
= (struct lf_bitfield
*) data
;
3025 if (size
< offsetof (struct lf_bitfield
, length
))
3027 einfo (_("%P: warning: truncated CodeView type record"
3032 if (!remap_type (&bf
->base_type
, map
, type_num
, num_types
))
3040 struct lf_methodlist
*ml
= (struct lf_methodlist
*) data
;
3041 unsigned int num_entries
;
3043 if (size
< offsetof (struct lf_methodlist
, entries
))
3045 einfo (_("%P: warning: truncated CodeView type record"
3046 " LF_METHODLIST\n"));
3050 if ((size
- offsetof (struct lf_methodlist
, entries
))
3051 % sizeof (struct lf_methodlist_entry
))
3053 einfo (_("%P: warning: malformed CodeView type record"
3054 " LF_METHODLIST\n"));
3058 num_entries
= (size
- offsetof (struct lf_methodlist
, entries
))
3059 / sizeof (struct lf_methodlist_entry
);
3061 for (unsigned int i
= 0; i
< num_entries
; i
++)
3063 if (!remap_type (&ml
->entries
[i
].method_type
, map
,
3064 type_num
, num_types
))
3073 struct lf_array
*arr
= (struct lf_array
*) data
;
3075 if (size
< offsetof (struct lf_array
, length_in_bytes
))
3077 einfo (_("%P: warning: truncated CodeView type record"
3082 if (!remap_type (&arr
->element_type
, map
, type_num
, num_types
))
3085 if (!remap_type (&arr
->index_type
, map
, type_num
, num_types
))
3094 struct lf_class
*cl
= (struct lf_class
*) data
;
3095 uint16_t prop
, num_bytes
;
3096 size_t name_len
, name_off
;
3098 if (size
< offsetof (struct lf_class
, name
))
3100 einfo (_("%P: warning: truncated CodeView type record"
3101 " LF_CLASS/LF_STRUCTURE\n"));
3105 if (!remap_type (&cl
->field_list
, map
, type_num
, num_types
))
3108 if (!remap_type (&cl
->derived_from
, map
, type_num
, num_types
))
3111 if (!remap_type (&cl
->vshape
, map
, type_num
, num_types
))
3114 name_off
= offsetof (struct lf_class
, name
);
3116 num_bytes
= bfd_getl16 (&cl
->length
);
3118 /* If num_bytes >= 0x8000, actual value follows. */
3119 if (num_bytes
>= 0x8000)
3121 unsigned int param_len
= extended_value_len (num_bytes
);
3125 einfo (_("%P: warning: unhandled type %v within"
3126 " LF_CLASS/LF_STRUCTURE\n"), num_bytes
);
3130 name_off
+= param_len
;
3132 if (size
< name_off
)
3134 einfo (_("%P: warning: truncated CodeView type record"
3135 " LF_CLASS/LF_STRUCTURE\n"));
3140 name_len
= strnlen ((char *) cl
+ name_off
, size
- name_off
);
3142 if (name_len
== size
- name_off
)
3144 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3145 " terminating zero\n"));
3149 prop
= bfd_getl16 (&cl
->properties
);
3151 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3153 /* Structure has another name following first one. */
3155 size_t len
= name_off
+ name_len
+ 1;
3156 size_t unique_name_len
;
3158 unique_name_len
= strnlen ((char *) cl
+ name_off
+ name_len
+ 1,
3161 if (unique_name_len
== size
- len
)
3163 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3164 " has no terminating zero\n"));
3169 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3170 && !is_name_anonymous ((char *) cl
+ name_off
, name_len
))
3173 cv_hash
= crc32 ((uint8_t *) cl
+ name_off
, name_len
);
3181 struct lf_union
*un
= (struct lf_union
*) data
;
3182 uint16_t prop
, num_bytes
;
3183 size_t name_len
, name_off
;
3185 if (size
< offsetof (struct lf_union
, name
))
3187 einfo (_("%P: warning: truncated CodeView type record"
3192 if (!remap_type (&un
->field_list
, map
, type_num
, num_types
))
3195 name_off
= offsetof (struct lf_union
, name
);
3197 num_bytes
= bfd_getl16 (&un
->length
);
3199 /* If num_bytes >= 0x8000, actual value follows. */
3200 if (num_bytes
>= 0x8000)
3202 unsigned int param_len
= extended_value_len (num_bytes
);
3206 einfo (_("%P: warning: unhandled type %v within"
3207 " LF_UNION\n"), num_bytes
);
3211 name_off
+= param_len
;
3213 if (size
< name_off
)
3215 einfo (_("%P: warning: truncated CodeView type record"
3221 name_len
= strnlen ((char *) un
+ name_off
, size
- name_off
);
3223 if (name_len
== size
- name_off
)
3225 einfo (_("%P: warning: name for LF_UNION has no"
3226 " terminating zero\n"));
3230 prop
= bfd_getl16 (&un
->properties
);
3232 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3234 /* Structure has another name following first one. */
3236 size_t len
= name_off
+ name_len
+ 1;
3237 size_t unique_name_len
;
3239 unique_name_len
= strnlen ((char *) un
+ name_off
+ name_len
+ 1,
3242 if (unique_name_len
== size
- len
)
3244 einfo (_("%P: warning: unique name for LF_UNION has"
3245 " no terminating zero\n"));
3250 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3251 && !is_name_anonymous ((char *) un
+ name_off
, name_len
))
3254 cv_hash
= crc32 ((uint8_t *) un
+ name_off
, name_len
);
3262 struct lf_enum
*en
= (struct lf_enum
*) data
;
3266 if (size
< offsetof (struct lf_enum
, name
))
3268 einfo (_("%P: warning: truncated CodeView type record"
3273 if (!remap_type (&en
->underlying_type
, map
, type_num
, num_types
))
3276 if (!remap_type (&en
->field_list
, map
, type_num
, num_types
))
3279 name_len
= strnlen (en
->name
, size
- offsetof (struct lf_enum
, name
));
3281 if (name_len
== size
- offsetof (struct lf_enum
, name
))
3283 einfo (_("%P: warning: name for LF_ENUM has no"
3284 " terminating zero\n"));
3288 prop
= bfd_getl16 (&en
->properties
);
3290 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3292 /* Structure has another name following first one. */
3294 size_t len
= offsetof (struct lf_enum
, name
) + name_len
+ 1;
3295 size_t unique_name_len
;
3297 unique_name_len
= strnlen (en
->name
+ name_len
+ 1, size
- len
);
3299 if (unique_name_len
== size
- len
)
3301 einfo (_("%P: warning: unique name for LF_ENUM has"
3302 " no terminating zero\n"));
3311 /* Does not reference any types, nothing to be done. */
3316 struct lf_vftable
*vft
= (struct lf_vftable
*) data
;
3318 if (size
< offsetof (struct lf_vftable
, names
))
3320 einfo (_("%P: warning: truncated CodeView type record"
3325 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
3328 if (!remap_type (&vft
->base_vftable
, map
, type_num
, num_types
))
3336 struct lf_string_id
*str
= (struct lf_string_id
*) data
;
3339 if (size
< offsetof (struct lf_string_id
, string
))
3341 einfo (_("%P: warning: truncated CodeView type record"
3342 " LF_STRING_ID\n"));
3346 if (!remap_type (&str
->substring
, map
, type_num
, num_types
))
3349 string_len
= strnlen (str
->string
,
3350 size
- offsetof (struct lf_string_id
, string
));
3352 if (string_len
== size
- offsetof (struct lf_string_id
, string
))
3354 einfo (_("%P: warning: string for LF_STRING_ID has no"
3355 " terminating zero\n"));
3364 case LF_SUBSTR_LIST
:
3366 uint32_t num_entries
;
3367 struct lf_arglist
*ssl
= (struct lf_arglist
*) data
;
3369 if (size
< offsetof (struct lf_arglist
, args
))
3371 einfo (_("%P: warning: truncated CodeView type record"
3372 " LF_SUBSTR_LIST\n"));
3376 num_entries
= bfd_getl32 (&ssl
->num_entries
);
3378 if (size
< offsetof (struct lf_arglist
, args
)
3379 + (num_entries
* sizeof (uint32_t)))
3381 einfo (_("%P: warning: truncated CodeView type record"
3382 " LF_SUBSTR_LIST\n"));
3386 for (uint32_t i
= 0; i
< num_entries
; i
++)
3388 if (!remap_type (&ssl
->args
[i
], map
, type_num
, num_types
))
3399 uint16_t num_entries
;
3400 struct lf_build_info
*bi
= (struct lf_build_info
*) data
;
3402 if (size
< offsetof (struct lf_build_info
, strings
))
3404 einfo (_("%P: warning: truncated CodeView type record"
3405 " LF_BUILDINFO\n"));
3409 num_entries
= bfd_getl16 (&bi
->count
);
3411 if (size
< offsetof (struct lf_build_info
, strings
)
3412 + (num_entries
* sizeof (uint32_t)))
3414 einfo (_("%P: warning: truncated CodeView type record"
3415 " LF_BUILDINFO\n"));
3419 for (uint32_t i
= 0; i
< num_entries
; i
++)
3421 if (!remap_type (&bi
->strings
[i
], map
, type_num
, num_types
))
3432 struct lf_func_id
*func
= (struct lf_func_id
*) data
;
3435 if (size
< offsetof (struct lf_func_id
, name
))
3437 einfo (_("%P: warning: truncated CodeView type record"
3442 if (!remap_type (&func
->parent_scope
, map
, type_num
, num_types
))
3445 if (!remap_type (&func
->function_type
, map
, type_num
, num_types
))
3448 name_len
= strnlen (func
->name
,
3449 size
- offsetof (struct lf_func_id
, name
));
3451 if (name_len
== size
- offsetof (struct lf_func_id
, name
))
3453 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3454 " terminating zero\n"));
3465 struct lf_mfunc_id
*mfunc
= (struct lf_mfunc_id
*) data
;
3468 if (size
< offsetof (struct lf_mfunc_id
, name
))
3470 einfo (_("%P: warning: truncated CodeView type record"
3475 if (!remap_type (&mfunc
->parent_type
, map
, type_num
, num_types
))
3478 if (!remap_type (&mfunc
->function_type
, map
, type_num
, num_types
))
3481 name_len
= strnlen (mfunc
->name
,
3482 size
- offsetof (struct lf_mfunc_id
, name
));
3484 if (name_len
== size
- offsetof (struct lf_mfunc_id
, name
))
3486 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3487 " terminating zero\n"));
3496 case LF_UDT_SRC_LINE
:
3497 return handle_udt_src_line (data
, size
, map
, type_num
, num_types
,
3498 ids
, mod_num
, strings
);
3501 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type
);
3505 hash
= iterative_hash (data
, size
, 0);
3507 t
= ipi
? ids
: types
;
3509 slot
= htab_find_slot_with_hash (t
->hashmap
, data
, hash
, INSERT
);
3513 if (!*slot
) /* new entry */
3515 struct type_entry
*e
;
3517 *slot
= xmalloc (offsetof (struct type_entry
, data
) + size
);
3519 e
= (struct type_entry
*) *slot
;
3522 e
->index
= t
->num_types
;
3525 e
->cv_hash
= cv_hash
;
3527 e
->cv_hash
= crc32 (data
, size
);
3529 e
->has_udt_src_line
= false;
3531 memcpy (e
->data
, data
, size
);
3544 else /* duplicate */
3546 map
[type_num
] = (struct type_entry
*) *slot
;
3552 /* Parse the .debug$T section of a module, and pass any type definitions
3553 found to handle_type. */
3555 handle_debugt_section (asection
*s
, bfd
*mod
, struct types
*types
,
3556 struct types
*ids
, uint16_t mod_num
,
3557 struct string_table
*strings
,
3558 struct type_entry
***map
, uint32_t *num_types
)
3560 bfd_byte
*data
= NULL
;
3564 if (!bfd_get_full_section_contents (mod
, s
, &data
))
3570 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
3576 off
= sizeof (uint32_t);
3578 while (off
+ sizeof (uint16_t) <= s
->size
)
3582 size
= bfd_getl16 (data
+ off
);
3583 off
+= sizeof (uint16_t);
3585 if (size
+ off
> s
->size
|| size
<= sizeof (uint16_t))
3588 bfd_set_error (bfd_error_bad_value
);
3596 if (*num_types
== 0)
3602 *map
= xcalloc (*num_types
, sizeof (struct type_entry
*));
3604 off
= sizeof (uint32_t);
3607 while (off
+ sizeof (uint16_t) <= s
->size
)
3611 size
= bfd_getl16 (data
+ off
);
3613 if (!handle_type (data
+ off
, *map
, type_num
, *num_types
, types
, ids
,
3618 bfd_set_error (bfd_error_bad_value
);
3622 off
+= sizeof (uint16_t) + size
;
3631 /* Return the CodeView constant for the selected architecture. */
3633 target_processor (bfd
*abfd
)
3635 switch (abfd
->arch_info
->arch
)
3638 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
3641 return CV_CFL_80386
;
3643 case bfd_arch_aarch64
:
3644 return CV_CFL_ARM64
;
3651 /* Create the symbols that go in "* Linker *", the dummy module created
3652 for the linker itself. */
3654 create_linker_symbols (bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
3655 const char *pdb_name
)
3658 struct objname
*name
;
3659 struct compile3
*comp
;
3660 struct envblock
*env
;
3661 size_t padding1
, padding2
, env_size
;
3662 char *cwdval
, *exeval
, *pdbval
;
3664 /* extra NUL for padding */
3665 static const char linker_fn
[] = "* Linker *\0";
3666 static const char linker_name
[] = "GNU LD " VERSION
;
3668 static const char cwd
[] = "cwd";
3669 static const char exe
[] = "exe";
3670 static const char pdb
[] = "pdb";
3672 cwdval
= getcwd (NULL
, 0);
3675 einfo (_("%P: warning: unable to get working directory\n"));
3679 exeval
= lrealpath (program_name
);
3683 einfo (_("%P: warning: unable to get program name\n"));
3688 pdbval
= lrealpath (pdb_name
);
3692 einfo (_("%P: warning: unable to get full path to PDB\n"));
3698 *sym_byte_size
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3699 *sym_byte_size
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
);
3701 if (*sym_byte_size
% 4)
3702 padding1
= 4 - (*sym_byte_size
% 4);
3706 *sym_byte_size
+= padding1
;
3708 env_size
= offsetof (struct envblock
, strings
);
3709 env_size
+= sizeof (cwd
);
3710 env_size
+= strlen (cwdval
) + 1;
3711 env_size
+= sizeof (exe
);
3712 env_size
+= strlen (exeval
) + 1;
3713 env_size
+= sizeof (pdb
);
3714 env_size
+= strlen (pdbval
) + 1;
3717 padding2
= 4 - (env_size
% 4);
3721 env_size
+= padding2
;
3723 *sym_byte_size
+= env_size
;
3725 *syms
= xmalloc (*sym_byte_size
);
3728 /* Write S_OBJNAME */
3730 name
= (struct objname
*) ptr
;
3731 bfd_putl16 (offsetof (struct objname
, name
)
3732 + sizeof (linker_fn
) - sizeof (uint16_t), &name
->size
);
3733 bfd_putl16 (S_OBJNAME
, &name
->kind
);
3734 bfd_putl32 (0, &name
->signature
);
3735 memcpy (name
->name
, linker_fn
, sizeof (linker_fn
));
3737 ptr
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3739 /* Write S_COMPILE3 */
3741 comp
= (struct compile3
*) ptr
;
3743 bfd_putl16 (offsetof (struct compile3
, compiler
) + sizeof (linker_name
)
3744 + padding1
- sizeof (uint16_t), &comp
->size
);
3745 bfd_putl16 (S_COMPILE3
, &comp
->kind
);
3746 bfd_putl32 (CV_CFL_LINK
, &comp
->flags
);
3747 bfd_putl16 (target_processor (abfd
), &comp
->machine
);
3748 bfd_putl16 (0, &comp
->frontend_major
);
3749 bfd_putl16 (0, &comp
->frontend_minor
);
3750 bfd_putl16 (0, &comp
->frontend_build
);
3751 bfd_putl16 (0, &comp
->frontend_qfe
);
3752 bfd_putl16 (0, &comp
->backend_major
);
3753 bfd_putl16 (0, &comp
->backend_minor
);
3754 bfd_putl16 (0, &comp
->backend_build
);
3755 bfd_putl16 (0, &comp
->backend_qfe
);
3756 memcpy (comp
->compiler
, linker_name
, sizeof (linker_name
));
3758 memset (comp
->compiler
+ sizeof (linker_name
), 0, padding1
);
3760 ptr
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
) + padding1
;
3762 /* Write S_ENVBLOCK */
3764 env
= (struct envblock
*) ptr
;
3766 bfd_putl16 (env_size
- sizeof (uint16_t), &env
->size
);
3767 bfd_putl16 (S_ENVBLOCK
, &env
->kind
);
3770 ptr
+= offsetof (struct envblock
, strings
);
3772 memcpy (ptr
, cwd
, sizeof (cwd
));
3773 ptr
+= sizeof (cwd
);
3774 memcpy (ptr
, cwdval
, strlen (cwdval
) + 1);
3775 ptr
+= strlen (cwdval
) + 1;
3777 memcpy (ptr
, exe
, sizeof (exe
));
3778 ptr
+= sizeof (exe
);
3779 memcpy (ptr
, exeval
, strlen (exeval
) + 1);
3780 ptr
+= strlen (exeval
) + 1;
3782 memcpy (ptr
, pdb
, sizeof (pdb
));
3783 ptr
+= sizeof (pdb
);
3784 memcpy (ptr
, pdbval
, strlen (pdbval
) + 1);
3785 ptr
+= strlen (pdbval
) + 1;
3787 /* Microsoft's LINK also includes "cmd", the command-line options passed
3788 to the linker, but unfortunately we don't have access to argc and argv
3791 memset (ptr
, 0, padding2
);
3800 /* Populate the module stream, which consists of the transformed .debug$S
3801 data for each object file. */
3803 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
3804 struct string_table
*strings
,
3805 uint32_t *c13_info_size
,
3806 struct mod_source_files
*mod_source
,
3807 bfd
*abfd
, struct types
*types
,
3808 struct types
*ids
, uint16_t mod_num
,
3809 bfd
*sym_rec_stream
, struct globals
*glob
,
3810 const char *pdb_name
)
3812 uint8_t int_buf
[sizeof (uint32_t)];
3813 uint8_t *c13_info
= NULL
;
3814 uint8_t *syms
= NULL
;
3819 if (!strcmp (bfd_get_filename (mod
), "dll stuff"))
3821 if (!create_linker_symbols (mod
, &syms
, sym_byte_size
, pdb_name
))
3826 struct type_entry
**map
= NULL
;
3827 uint32_t num_types
= 0;
3829 /* Process .debug$T section. */
3831 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3833 if (!strcmp (s
->name
, ".debug$T") && s
->size
>= sizeof (uint32_t))
3835 if (!handle_debugt_section (s
, mod
, types
, ids
, mod_num
, strings
,
3838 free (mod_source
->files
);
3846 /* Process .debug$S section(s). */
3848 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3850 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
3852 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
3853 c13_info_size
, mod_source
, abfd
,
3854 &syms
, sym_byte_size
, map
, num_types
,
3855 sym_rec_stream
, glob
, mod_num
))
3859 free (mod_source
->files
);
3869 /* Write the signature. */
3871 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
3873 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3882 if (bfd_bwrite (syms
, *sym_byte_size
, stream
) != *sym_byte_size
)
3894 if (bfd_bwrite (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
3903 /* Write the global refs size. */
3905 bfd_putl32 (0, int_buf
);
3907 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3913 /* Create the module info substream within the DBI. */
3915 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
3916 uint32_t *size
, struct string_table
*strings
,
3917 struct source_files_info
*source
,
3918 struct types
*types
, struct types
*ids
,
3919 bfd
*sym_rec_stream
, struct globals
*glob
,
3920 const char *pdb_name
)
3923 unsigned int mod_num
;
3925 static const char linker_fn
[] = "* Linker *";
3929 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
3932 size_t len
= sizeof (struct module_info
);
3934 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
3936 len
+= sizeof (linker_fn
); /* Object name. */
3937 len
++; /* Empty module name. */
3939 else if (in
->my_archive
)
3941 char *name
= lrealpath (bfd_get_filename (in
));
3943 len
+= strlen (name
) + 1; /* Object name. */
3947 name
= lrealpath (bfd_get_filename (in
->my_archive
));
3949 len
+= strlen (name
) + 1; /* Archive name. */
3955 char *name
= lrealpath (bfd_get_filename (in
));
3956 size_t name_len
= strlen (name
) + 1;
3958 len
+= name_len
; /* Object name. */
3959 len
+= name_len
; /* And again as the archive name. */
3965 len
+= 4 - (len
% 4);
3969 source
->mod_count
++;
3972 *data
= xmalloc (*size
);
3976 source
->mods
= xmalloc (source
->mod_count
3977 * sizeof (struct mod_source_files
));
3978 memset (source
->mods
, 0,
3979 source
->mod_count
* sizeof (struct mod_source_files
));
3983 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
3986 struct module_info
*mod
= (struct module_info
*) ptr
;
3987 uint16_t stream_num
;
3989 uint32_t sym_byte_size
, c13_info_size
;
3990 uint8_t *start
= ptr
;
3992 stream
= add_stream (pdb
, NULL
, &stream_num
);
3996 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
3998 free (source
->mods
[i
].files
);
4001 free (source
->mods
);
4006 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
4007 strings
, &c13_info_size
,
4008 &source
->mods
[mod_num
], abfd
,
4009 types
, ids
, mod_num
,
4010 sym_rec_stream
, glob
, pdb_name
))
4012 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
4014 free (source
->mods
[i
].files
);
4017 free (source
->mods
);
4022 bfd_putl32 (0, &mod
->unused1
);
4024 /* These are dummy values - MSVC copies the first section contribution
4025 entry here, but doesn't seem to use it for anything. */
4026 bfd_putl16 (0xffff, &mod
->sc
.section
);
4027 bfd_putl16 (0, &mod
->sc
.padding1
);
4028 bfd_putl32 (0, &mod
->sc
.offset
);
4029 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
4030 bfd_putl32 (0, &mod
->sc
.characteristics
);
4031 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
4032 bfd_putl16 (0, &mod
->sc
.padding2
);
4033 bfd_putl32 (0, &mod
->sc
.data_crc
);
4034 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
4036 bfd_putl16 (0, &mod
->flags
);
4037 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
4038 bfd_putl32 (sizeof (uint32_t) + sym_byte_size
, &mod
->sym_byte_size
);
4039 bfd_putl32 (0, &mod
->c11_byte_size
);
4040 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
4041 bfd_putl16 (0, &mod
->source_file_count
);
4042 bfd_putl16 (0, &mod
->padding
);
4043 bfd_putl32 (0, &mod
->unused2
);
4044 bfd_putl32 (0, &mod
->source_file_name_index
);
4045 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
4047 ptr
+= sizeof (struct module_info
);
4049 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
4052 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
4053 ptr
+= sizeof (linker_fn
);
4055 /* Empty module name. */
4059 else if (in
->my_archive
)
4061 char *name
= lrealpath (bfd_get_filename (in
));
4062 size_t name_len
= strlen (name
) + 1;
4065 memcpy (ptr
, name
, name_len
);
4070 name
= lrealpath (bfd_get_filename (in
->my_archive
));
4071 name_len
= strlen (name
) + 1;
4074 memcpy (ptr
, name
, name_len
);
4081 char *name
= lrealpath (bfd_get_filename (in
));
4082 size_t name_len
= strlen (name
) + 1;
4085 memcpy (ptr
, name
, name_len
);
4088 /* Object name again as archive name. */
4089 memcpy (ptr
, name
, name_len
);
4095 /* Pad to next four-byte boundary. */
4097 if ((ptr
- start
) % 4)
4099 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
4100 ptr
+= 4 - ((ptr
- start
) % 4);
4109 /* Return the index of a given output section. */
4111 find_section_number (bfd
*abfd
, asection
*sect
)
4115 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
4120 /* Empty sections aren't output. */
4128 /* Used as parameter to qsort, to sort section contributions by section and
4131 section_contribs_compare (const void *p1
, const void *p2
)
4133 const struct in_sc
*sc1
= p1
;
4134 const struct in_sc
*sc2
= p2
;
4136 if (sc1
->sect_num
< sc2
->sect_num
)
4138 if (sc1
->sect_num
> sc2
->sect_num
)
4141 if (sc1
->s
->output_offset
< sc2
->s
->output_offset
)
4143 if (sc1
->s
->output_offset
> sc2
->s
->output_offset
)
4149 /* Create the substream which maps addresses in the image file to locations
4150 in the original object files. */
4152 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
4154 unsigned int num_sc
= 0;
4155 struct section_contribution
*sc
;
4159 struct in_sc
*sc_in
, *sc2
;
4162 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4165 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4167 if (s
->size
== 0 || discarded_section (s
))
4174 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
4175 *data
= xmalloc (*size
);
4177 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
4179 /* Read characteristics of outputted sections. */
4181 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
4183 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
4184 offset
+= offsetof (struct external_scnhdr
, s_flags
);
4186 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
4188 bfd_seek (abfd
, offset
, SEEK_SET
);
4190 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
4191 abfd
) != sizeof (uint32_t))
4198 offset
+= sizeof (struct external_scnhdr
);
4201 /* Microsoft's DIA expects section contributions to be sorted by section
4202 number and offset, otherwise it will be unable to resolve line numbers. */
4204 sc_in
= xmalloc (num_sc
* sizeof (* sc_in
));
4208 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4211 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4213 if (s
->size
== 0 || discarded_section (s
))
4217 sc2
->sect_num
= find_section_number (abfd
, s
->output_section
);
4218 sc2
->mod_index
= mod_index
;
4226 qsort (sc_in
, num_sc
, sizeof (* sc_in
), section_contribs_compare
);
4229 sc
= (struct section_contribution
*) (ptr
+ 1); /* Skip the version word. */
4231 for (unsigned int i
= 0; i
< num_sc
; i
++)
4233 memcpy (&sc
->characteristics
,
4234 sect_flags
+ ((sc_in
[i
].sect_num
- 1) * sizeof (uint32_t)),
4237 bfd_putl16 (sc_in
[i
].sect_num
, &sc
->section
);
4238 bfd_putl16 (0, &sc
->padding1
);
4239 bfd_putl32 (sc_in
[i
].s
->output_offset
, &sc
->offset
);
4240 bfd_putl32 (sc_in
[i
].s
->size
, &sc
->size
);
4241 bfd_putl16 (sc_in
[i
].mod_index
, &sc
->module_index
);
4242 bfd_putl16 (0, &sc
->padding2
);
4243 bfd_putl32 (0, &sc
->data_crc
);
4244 bfd_putl32 (0, &sc
->reloc_crc
);
4255 /* The source info substream lives within the DBI stream, and lists the
4256 source files for each object file (i.e. it's derived from the
4257 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4258 superfluous, as the filenames are also available in the C13 parts of
4259 the module streams, but MSVC relies on it to work properly. */
4261 create_source_info_substream (void **data
, uint32_t *size
,
4262 struct source_files_info
*source
)
4264 uint16_t dedupe_source_files_count
= 0;
4265 uint16_t source_files_count
= 0;
4266 uint32_t strings_len
= 0;
4269 /* Loop through the source files, marking unique filenames. The pointers
4270 here are for entries in the main string table, and so have already
4271 been deduplicated. */
4273 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4275 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4277 if (source
->mods
[i
].files
[j
])
4279 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
4281 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
4282 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
4283 dedupe_source_files_count
++;
4286 source_files_count
++;
4291 *size
= sizeof (uint16_t) + sizeof (uint16_t);
4292 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
4293 *size
+= sizeof (uint32_t) * source_files_count
;
4294 *size
+= strings_len
;
4296 *data
= xmalloc (*size
);
4298 ptr
= (uint8_t *) *data
;
4300 /* Write header (module count and source file count). */
4302 bfd_putl16 (source
->mod_count
, ptr
);
4303 ptr
+= sizeof (uint16_t);
4305 bfd_putl16 (dedupe_source_files_count
, ptr
);
4306 ptr
+= sizeof (uint16_t);
4308 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4309 present, but does not appear to be useful". */
4311 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4313 bfd_putl16 (i
, ptr
);
4314 ptr
+= sizeof (uint16_t);
4317 /* Write source file count for each module. */
4319 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4321 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
4322 ptr
+= sizeof (uint16_t);
4325 /* For each module, write the offsets within the string table
4326 for each source file. */
4328 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4330 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4332 if (source
->mods
[i
].files
[j
])
4334 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
4335 ptr
+= sizeof (uint32_t);
4340 /* Write the string table. We set source_file_offset to a dummy value for
4341 each entry we write, so we don't write duplicate filenames. */
4343 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4345 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4347 if (source
->mods
[i
].files
[j
]
4348 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
4350 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
4351 source
->mods
[i
].files
[j
]->len
);
4352 ptr
+= source
->mods
[i
].files
[j
]->len
;
4357 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
4363 /* Used as parameter to qsort, to sort globals by hash. */
4365 global_compare_hash (const void *s1
, const void *s2
)
4367 const struct global
*g1
= *(const struct global
**) s1
;
4368 const struct global
*g2
= *(const struct global
**) s2
;
4370 if (g1
->hash
< g2
->hash
)
4372 if (g1
->hash
> g2
->hash
)
4378 /* Create the globals stream, which contains the unmangled symbol names. */
4380 create_globals_stream (bfd
*pdb
, struct globals
*glob
, uint16_t *stream_num
)
4383 struct globals_hash_header h
;
4384 uint32_t buckets_size
, filled_buckets
= 0;
4385 struct global
**sorted
= NULL
;
4387 struct global
*buckets
[NUM_GLOBALS_HASH_BUCKETS
];
4388 char int_buf
[sizeof (uint32_t)];
4390 stream
= add_stream (pdb
, NULL
, stream_num
);
4394 memset (buckets
, 0, sizeof (buckets
));
4396 if (glob
->num_entries
> 0)
4400 /* Create an array of pointers, sorted by hash value. */
4402 sorted
= xmalloc (sizeof (struct global
*) * glob
->num_entries
);
4405 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4411 qsort (sorted
, glob
->num_entries
, sizeof (struct global
*),
4412 global_compare_hash
);
4414 /* Populate the buckets. */
4416 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4418 if (!buckets
[sorted
[i
]->hash
])
4420 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4424 sorted
[i
]->index
= i
;
4428 buckets_size
= NUM_GLOBALS_HASH_BUCKETS
/ 8;
4429 buckets_size
+= sizeof (uint32_t);
4430 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4432 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &h
.signature
);
4433 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &h
.version
);
4434 bfd_putl32 (glob
->num_entries
* sizeof (struct hash_record
),
4436 bfd_putl32 (buckets_size
, &h
.buckets_size
);
4438 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
4441 /* Write hash entries, sorted by hash. */
4443 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4445 struct hash_record hr
;
4447 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4448 bfd_putl32 (sorted
[i
]->refcount
, &hr
.reference
);
4450 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4454 /* Write the bitmap for filled and unfilled buckets. */
4456 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
+= 8)
4460 for (unsigned int j
= 0; j
< 8; j
++)
4466 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
4470 /* Add a 4-byte gap. */
4472 bfd_putl32 (0, int_buf
);
4474 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4477 /* Write the bucket offsets. */
4479 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
++)
4483 /* 0xc is size of internal hash_record structure in
4484 Microsoft's parser. */
4485 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
4487 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4501 /* Hash an entry in the globals list. */
4503 hash_global_entry (const void *p
)
4505 const struct global
*g
= (const struct global
*) p
;
4506 uint16_t len
= bfd_getl16 (g
->data
);
4508 return iterative_hash (g
->data
, len
, 0);
4511 /* Compare an entry in the globals list with a symbol. */
4513 eq_global_entry (const void *a
, const void *b
)
4515 const struct global
*g
= (const struct global
*) a
;
4516 uint16_t len1
, len2
;
4518 len1
= bfd_getl16 (g
->data
) + sizeof (uint16_t);
4519 len2
= bfd_getl16 (b
) + sizeof (uint16_t);
4524 return !memcmp (g
->data
, b
, len1
);
4527 /* Stream 4 is the debug information (DBI) stream. */
4529 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
4530 uint16_t section_header_stream_num
,
4531 uint16_t sym_rec_stream_num
,
4532 uint16_t publics_stream_num
,
4533 struct string_table
*strings
,
4534 struct types
*types
,
4536 bfd
*sym_rec_stream
, const char *pdb_name
)
4538 struct pdb_dbi_stream_header h
;
4539 struct optional_dbg_header opt
;
4540 void *mod_info
, *sc
, *source_info
;
4541 uint32_t mod_info_size
, sc_size
, source_info_size
;
4542 struct source_files_info source
;
4543 struct globals glob
;
4544 uint16_t globals_stream_num
;
4546 source
.mod_count
= 0;
4549 glob
.num_entries
= 0;
4553 glob
.hashmap
= htab_create_alloc (0, hash_global_entry
,
4554 eq_global_entry
, free
, xcalloc
, free
);
4556 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
4557 strings
, &source
, types
, ids
,
4558 sym_rec_stream
, &glob
, pdb_name
))
4560 htab_delete (glob
.hashmap
);
4564 if (!create_globals_stream (pdb
, &glob
, &globals_stream_num
))
4566 htab_delete (glob
.hashmap
);
4568 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4570 free (source
.mods
[i
].files
);
4578 htab_delete (glob
.hashmap
);
4580 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
4582 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4584 free (source
.mods
[i
].files
);
4592 create_source_info_substream (&source_info
, &source_info_size
, &source
);
4594 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4596 free (source
.mods
[i
].files
);
4600 bfd_putl32 (0xffffffff, &h
.version_signature
);
4601 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
4602 bfd_putl32 (1, &h
.age
);
4603 bfd_putl16 (globals_stream_num
, &h
.global_stream_index
);
4604 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
4605 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
4606 bfd_putl16 (0, &h
.pdb_dll_version
);
4607 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
4608 bfd_putl16 (0, &h
.pdb_dll_rbld
);
4609 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
4610 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
4611 bfd_putl32 (0, &h
.section_map_size
);
4612 bfd_putl32 (source_info_size
, &h
.source_info_size
);
4613 bfd_putl32 (0, &h
.type_server_map_size
);
4614 bfd_putl32 (0, &h
.mfc_type_server_index
);
4615 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
4616 bfd_putl32 (0, &h
.ec_substream_size
);
4617 bfd_putl16 (0, &h
.flags
);
4618 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
4619 bfd_putl32 (0, &h
.padding
);
4621 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
4629 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
4639 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
4648 if (bfd_bwrite (source_info
, source_info_size
, stream
) != source_info_size
)
4656 bfd_putl16 (0xffff, &opt
.fpo_stream
);
4657 bfd_putl16 (0xffff, &opt
.exception_stream
);
4658 bfd_putl16 (0xffff, &opt
.fixup_stream
);
4659 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
4660 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
4661 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
4662 bfd_putl16 (0xffff, &opt
.token_map_stream
);
4663 bfd_putl16 (0xffff, &opt
.xdata_stream
);
4664 bfd_putl16 (0xffff, &opt
.pdata_stream
);
4665 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
4666 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
4668 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
4674 /* Used as parameter to qsort, to sort publics by hash. */
4676 public_compare_hash (const void *s1
, const void *s2
)
4678 const struct public *p1
= *(const struct public **) s1
;
4679 const struct public *p2
= *(const struct public **) s2
;
4681 if (p1
->hash
< p2
->hash
)
4683 if (p1
->hash
> p2
->hash
)
4689 /* Used as parameter to qsort, to sort publics by address. */
4691 public_compare_addr (const void *s1
, const void *s2
)
4693 const struct public *p1
= *(const struct public **) s1
;
4694 const struct public *p2
= *(const struct public **) s2
;
4696 if (p1
->section
< p2
->section
)
4698 if (p1
->section
> p2
->section
)
4701 if (p1
->address
< p2
->address
)
4703 if (p1
->address
> p2
->address
)
4709 /* The publics stream is a hash map of S_PUB32 records, which are stored
4710 in the symbol record stream. Each S_PUB32 entry represents a symbol
4711 from the point of view of the linker: a section index, an offset within
4712 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4713 which are the same thing but generated by the compiler. */
4715 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
4717 struct publics_header header
;
4718 struct globals_hash_header hash_header
;
4719 const unsigned int num_buckets
= 4096;
4720 unsigned int num_entries
= 0, filled_buckets
= 0;
4721 unsigned int buckets_size
, sym_hash_size
;
4722 char int_buf
[sizeof (uint32_t)];
4723 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
4724 struct public **buckets
;
4725 struct public **sorted
= NULL
;
4728 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
4729 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
4731 /* Loop through the global symbols in our input files, and write S_PUB32
4732 records in the symbol record stream for those that make it into the
4734 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4737 if (!in
->outsymbols
)
4740 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
4742 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
4744 if (sym
->flags
& BSF_GLOBAL
)
4747 uint16_t record_length
;
4748 const char *name
= sym
->name
;
4749 size_t name_len
= strlen (name
);
4750 struct public *p
= xmalloc (sizeof (struct public));
4751 unsigned int padding
= 0;
4756 find_section_number (abfd
, sym
->section
->output_section
);
4762 p
->offset
= bfd_tell (sym_rec_stream
);
4763 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
4764 p
->section
= section
;
4765 p
->address
= sym
->section
->output_offset
+ sym
->value
;
4767 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
4769 if (record_length
% 4)
4770 padding
= 4 - (record_length
% 4);
4772 /* Assume that all global symbols in executable sections
4774 if (sym
->section
->flags
& SEC_CODE
)
4775 flags
= PUBSYM_FUNCTION
;
4777 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
4779 bfd_putl16 (S_PUB32
, &ps
.record_type
);
4780 bfd_putl32 (flags
, &ps
.flags
);
4781 bfd_putl32 (p
->address
, &ps
.offset
);
4782 bfd_putl16 (p
->section
, &ps
.section
);
4784 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
4785 sizeof (struct pubsym
))
4788 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
4792 for (unsigned int j
= 0; j
< padding
; j
++)
4796 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
4804 publics_tail
->next
= p
;
4813 if (num_entries
> 0)
4815 /* Create an array of pointers, sorted by hash value. */
4817 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
4819 struct public *p
= publics_head
;
4820 for (unsigned int i
= 0; i
< num_entries
; i
++)
4826 qsort (sorted
, num_entries
, sizeof (struct public *),
4827 public_compare_hash
);
4829 /* Populate the buckets. */
4831 for (unsigned int i
= 0; i
< num_entries
; i
++)
4833 if (!buckets
[sorted
[i
]->hash
])
4835 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4839 sorted
[i
]->index
= i
;
4843 buckets_size
= num_buckets
/ 8;
4844 buckets_size
+= sizeof (uint32_t);
4845 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4847 sym_hash_size
= sizeof (hash_header
);
4848 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
4849 sym_hash_size
+= buckets_size
;
4851 /* Output the publics header. */
4853 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
4854 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
4855 bfd_putl32 (0, &header
.num_thunks
);
4856 bfd_putl32 (0, &header
.thunks_size
);
4857 bfd_putl32 (0, &header
.thunk_table
);
4858 bfd_putl32 (0, &header
.thunk_table_offset
);
4859 bfd_putl32 (0, &header
.num_sects
);
4861 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
4864 /* Output the global hash header. */
4866 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
4867 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
4868 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
4869 &hash_header
.entries_size
);
4870 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
4872 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
4873 sizeof (hash_header
))
4876 /* Write the entries in hash order. */
4878 for (unsigned int i
= 0; i
< num_entries
; i
++)
4880 struct hash_record hr
;
4882 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4883 bfd_putl32 (1, &hr
.reference
);
4885 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4889 /* Write the bitmap for filled and unfilled buckets. */
4891 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
4895 for (unsigned int j
= 0; j
< 8; j
++)
4901 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
4905 /* Add a 4-byte gap. */
4907 bfd_putl32 (0, int_buf
);
4909 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4912 /* Write the bucket offsets. */
4914 for (unsigned int i
= 0; i
< num_buckets
; i
++)
4918 /* 0xc is size of internal hash_record structure in
4919 Microsoft's parser. */
4920 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
4922 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4928 /* Write the address map: offsets into the symbol record stream of
4929 S_PUB32 records, ordered by address. */
4931 if (num_entries
> 0)
4933 qsort (sorted
, num_entries
, sizeof (struct public *),
4934 public_compare_addr
);
4936 for (unsigned int i
= 0; i
< num_entries
; i
++)
4938 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
4940 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4951 while (publics_head
)
4953 struct public *p
= publics_head
->next
;
4955 free (publics_head
);
4964 /* The section header stream contains a copy of the section headers
4965 from the PE file, in the same format. */
4967 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
4970 unsigned int section_count
;
4975 stream
= add_stream (pdb
, NULL
, num
);
4979 section_count
= abfd
->section_count
;
4981 /* Empty sections aren't output. */
4982 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
4984 if (sect
->size
== 0)
4988 if (section_count
== 0)
4991 /* Copy section table from output - it's already been written at this
4994 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
4996 bfd_seek (abfd
, scn_base
, SEEK_SET
);
4998 len
= section_count
* sizeof (struct external_scnhdr
);
4999 buf
= xmalloc (len
);
5001 if (bfd_bread (buf
, len
, abfd
) != len
)
5007 if (bfd_bwrite (buf
, len
, stream
) != len
)
5018 /* Populate the "/names" named stream, which contains the string table. */
5020 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
5022 char int_buf
[sizeof (uint32_t)];
5023 struct string_table_header h
;
5024 uint32_t num_strings
= 0, num_buckets
;
5025 struct string
**buckets
;
5027 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
5028 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
5030 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
5033 bfd_putl32 (strings
->strings_len
, int_buf
);
5035 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5040 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
5043 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5045 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
5048 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
5054 num_buckets
= num_strings
* 2;
5056 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
5057 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
5059 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5061 uint32_t bucket_num
= s
->hash
% num_buckets
;
5063 while (buckets
[bucket_num
])
5067 if (bucket_num
== num_buckets
)
5071 buckets
[bucket_num
] = s
;
5074 bfd_putl32 (num_buckets
, int_buf
);
5076 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5082 for (unsigned int i
= 0; i
< num_buckets
; i
++)
5085 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
5087 bfd_putl32 (0, int_buf
);
5089 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
5099 bfd_putl32 (num_strings
, int_buf
);
5101 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5107 /* Calculate the hash of a type_entry. */
5109 hash_type_entry (const void *p
)
5111 const struct type_entry
*e
= (const struct type_entry
*) p
;
5112 uint16_t size
= bfd_getl16 (e
->data
) + sizeof (uint16_t);
5114 return iterative_hash (e
->data
, size
, 0);
5117 /* Compare a type_entry with a type. */
5119 eq_type_entry (const void *a
, const void *b
)
5121 const struct type_entry
*e
= (const struct type_entry
*) a
;
5122 uint16_t size_a
= bfd_getl16 (e
->data
);
5123 uint16_t size_b
= bfd_getl16 (b
);
5125 if (size_a
!= size_b
)
5128 return memcmp (e
->data
+ sizeof (uint16_t),
5129 (const uint8_t *) b
+ sizeof (uint16_t), size_a
) == 0;
5132 /* Create a PDB debugging file for the PE image file abfd with the build ID
5133 guid, stored at pdb_name. */
5135 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
5139 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
5140 *publics_stream
, *tpi_stream
, *ipi_stream
;
5141 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
5142 struct string_table strings
;
5143 struct types types
, ids
;
5145 pdb
= bfd_openw (pdb_name
, "pdb");
5148 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5152 strings
.strings_head
= NULL
;
5153 strings
.strings_tail
= NULL
;
5154 strings
.strings_len
= 1;
5155 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
5156 eq_string_table_entry
, free
,
5159 bfd_set_format (pdb
, bfd_archive
);
5161 if (!create_old_directory_stream (pdb
))
5163 einfo (_("%P: warning: cannot create old directory stream "
5164 "in PDB file: %E\n"));
5168 info_stream
= add_stream (pdb
, NULL
, NULL
);
5172 einfo (_("%P: warning: cannot create info stream "
5173 "in PDB file: %E\n"));
5177 tpi_stream
= add_stream (pdb
, NULL
, NULL
);
5181 einfo (_("%P: warning: cannot create TPI stream "
5182 "in PDB file: %E\n"));
5186 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
5190 einfo (_("%P: warning: cannot create DBI stream "
5191 "in PDB file: %E\n"));
5195 ipi_stream
= add_stream (pdb
, NULL
, NULL
);
5199 einfo (_("%P: warning: cannot create IPI stream "
5200 "in PDB file: %E\n"));
5204 names_stream
= add_stream (pdb
, "/names", NULL
);
5208 einfo (_("%P: warning: cannot create /names stream "
5209 "in PDB file: %E\n"));
5213 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
5215 if (!sym_rec_stream
)
5217 einfo (_("%P: warning: cannot create symbol record stream "
5218 "in PDB file: %E\n"));
5222 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
5224 if (!publics_stream
)
5226 einfo (_("%P: warning: cannot create publics stream "
5227 "in PDB file: %E\n"));
5231 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
5233 einfo (_("%P: warning: cannot create section header stream "
5234 "in PDB file: %E\n"));
5238 types
.num_types
= 0;
5239 types
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5240 free
, xcalloc
, free
);
5241 types
.first
= types
.last
= NULL
;
5244 ids
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5245 free
, xcalloc
, free
);
5246 ids
.first
= ids
.last
= NULL
;
5248 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
5249 sym_rec_stream_num
, publics_stream_num
,
5250 &strings
, &types
, &ids
, sym_rec_stream
, pdb_name
))
5252 einfo (_("%P: warning: cannot populate DBI stream "
5253 "in PDB file: %E\n"));
5254 htab_delete (types
.hashmap
);
5255 htab_delete (ids
.hashmap
);
5259 if (!populate_type_stream (pdb
, tpi_stream
, &types
))
5261 einfo (_("%P: warning: cannot populate TPI stream "
5262 "in PDB file: %E\n"));
5263 htab_delete (types
.hashmap
);
5264 htab_delete (ids
.hashmap
);
5268 htab_delete (types
.hashmap
);
5270 if (!populate_type_stream (pdb
, ipi_stream
, &ids
))
5272 einfo (_("%P: warning: cannot populate IPI stream "
5273 "in PDB file: %E\n"));
5274 htab_delete (ids
.hashmap
);
5278 htab_delete (ids
.hashmap
);
5280 add_string ("", 0, &strings
);
5282 if (!populate_names_stream (names_stream
, &strings
))
5284 einfo (_("%P: warning: cannot populate names stream "
5285 "in PDB file: %E\n"));
5289 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
5291 einfo (_("%P: warning: cannot populate publics stream "
5292 "in PDB file: %E\n"));
5296 if (!populate_info_stream (pdb
, info_stream
, guid
))
5298 einfo (_("%P: warning: cannot populate info stream "
5299 "in PDB file: %E\n"));
5308 htab_delete (strings
.hashmap
);