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
;
110 static const uint32_t crc_table
[] =
112 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
113 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
114 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
115 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
116 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
117 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
118 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
119 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
120 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
121 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
122 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
123 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
124 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
125 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
126 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
127 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
128 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
129 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
130 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
131 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
132 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
133 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
134 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
135 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
136 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
137 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
138 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
139 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
140 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
141 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
142 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
143 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
144 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
145 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
146 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
147 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
148 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
149 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
150 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
151 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
152 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
153 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
154 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
157 /* Add a new stream to the PDB archive, and return its BFD. */
159 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
164 stream
= bfd_create (name
? name
: "", pdb
);
168 if (!bfd_make_writable (stream
))
174 if (!pdb
->archive_head
)
176 bfd_set_archive_head (pdb
, stream
);
181 bfd
*b
= pdb
->archive_head
;
185 while (b
->archive_next
)
191 b
->archive_next
= stream
;
200 /* Stream 0 ought to be a copy of the MSF directory from the last
201 time the PDB file was written. Because we don't do incremental
202 writes this isn't applicable to us, but we fill it with a dummy
203 value so as not to confuse radare. */
205 create_old_directory_stream (bfd
*pdb
)
208 char buf
[sizeof (uint32_t)];
210 stream
= add_stream (pdb
, NULL
, NULL
);
216 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
219 /* Calculate the hash of a given string. */
221 calc_hash (const char *data
, size_t len
)
228 hash
^= data
[1] << 8;
229 hash
^= data
[2] << 16;
230 hash
^= data
[3] << 24;
239 hash
^= data
[1] << 8;
249 hash
^= (hash
>> 11);
251 return hash
^ (hash
>> 16);
254 /* Stream 1 is the PDB info stream - see
255 https://llvm.org/docs/PDB/PdbStream.html. */
257 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
260 struct pdb_stream_70 h
;
261 uint32_t num_entries
, num_buckets
;
262 uint32_t names_length
, stream_num
;
263 char int_buf
[sizeof (uint32_t)];
271 struct hash_entry
**buckets
= NULL
;
275 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
276 bfd_putl32 (time (NULL
), &h
.signature
);
277 bfd_putl32 (1, &h
.age
);
279 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
280 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
281 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
282 memcpy (&h
.guid
[8], &guid
[8], 8);
284 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
287 /* Write hash list of named streams. This is a "rollover" hash, i.e.
288 if a bucket is filled an entry gets placed in the next free
292 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
294 if (strcmp (b
->filename
, ""))
298 num_buckets
= num_entries
* 2;
305 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
306 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
308 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
310 if (strcmp (b
->filename
, ""))
312 size_t len
= strlen (b
->filename
);
313 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
314 uint32_t bucket_num
= hash
% num_buckets
;
316 while (buckets
[bucket_num
])
320 if (bucket_num
== num_buckets
)
324 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
326 buckets
[bucket_num
]->offset
= names_length
;
327 buckets
[bucket_num
]->value
= stream_num
;
329 names_length
+= len
+ 1;
336 /* Write the strings list - the hash keys are indexes into this. */
338 bfd_putl32 (names_length
, int_buf
);
340 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
344 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
346 if (!strcmp (b
->filename
, ""))
349 size_t len
= strlen (b
->filename
) + 1;
351 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
355 /* Write the number of entries and buckets. */
357 bfd_putl32 (num_entries
, int_buf
);
359 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
363 bfd_putl32 (num_buckets
, int_buf
);
365 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
369 /* Write the present bitmap. */
371 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
373 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
377 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
381 for (unsigned int j
= 0; j
< 32; j
++)
383 if (i
+ j
>= num_buckets
)
390 bfd_putl32 (v
, int_buf
);
392 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
397 /* Write the (empty) deleted bitmap. */
399 bfd_putl32 (0, int_buf
);
401 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
405 /* Write the buckets. */
407 for (unsigned int i
= 0; i
< num_buckets
; i
++)
411 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
413 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
417 bfd_putl32 (buckets
[i
]->value
, int_buf
);
419 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
425 bfd_putl32 (0, int_buf
);
427 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
431 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
433 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
440 for (unsigned int i
= 0; i
< num_buckets
; i
++)
451 /* Calculate the CRC32 used for type hashes. */
453 crc32 (const uint8_t *data
, size_t len
)
459 crc
= (crc
>> 8) ^ crc_table
[(crc
& 0xff) ^ *data
];
468 /* Stream 2 is the type information (TPI) stream, and stream 4 is
469 the ID information (IPI) stream. They differ only in which records
470 go in which stream. */
472 populate_type_stream (bfd
*pdb
, bfd
*stream
, struct types
*types
)
474 struct pdb_tpi_stream_header h
;
475 struct type_entry
*e
;
476 uint32_t len
= 0, index_offset_len
, off
;
477 struct bfd
*hash_stream
= NULL
;
478 uint16_t hash_stream_index
;
480 static const uint32_t index_skip
= 0x2000;
484 index_offset_len
= 0;
488 uint32_t old_len
= len
;
490 len
+= sizeof (uint16_t) + bfd_getl16 (e
->data
);
492 if (old_len
== 0 || old_len
/ index_skip
!= len
/ index_skip
)
493 index_offset_len
+= sizeof (uint32_t) * 2;
498 /* Each type stream also has a stream which holds the hash value for each
499 type, along with a skip list to speed up searching. */
501 hash_stream
= add_stream (pdb
, "", &hash_stream_index
);
506 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
507 bfd_putl32 (sizeof (h
), &h
.header_size
);
508 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
509 bfd_putl32 (TPI_FIRST_INDEX
+ types
->num_types
, &h
.type_index_end
);
510 bfd_putl32 (len
, &h
.type_record_bytes
);
511 bfd_putl16 (hash_stream_index
, &h
.hash_stream_index
);
512 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
513 bfd_putl32 (sizeof (uint32_t), &h
.hash_key_size
);
514 bfd_putl32 (NUM_TPI_HASH_BUCKETS
, &h
.num_hash_buckets
);
515 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
516 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
517 &h
.hash_value_buffer_length
);
518 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
519 &h
.index_offset_buffer_offset
);
520 bfd_putl32 (index_offset_len
, &h
.index_offset_buffer_length
);
521 bfd_putl32 ((types
->num_types
* sizeof (uint32_t)) + index_offset_len
,
522 &h
.hash_adj_buffer_offset
);
523 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
525 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
528 /* Write the type definitions into the main stream, and the hashes
529 into the hash stream. The hashes have already been calculated
536 uint8_t buf
[sizeof (uint32_t)];
539 size
= bfd_getl16 (e
->data
);
541 if (bfd_bwrite (e
->data
, size
+ sizeof (uint16_t), stream
)
542 != size
+ sizeof (uint16_t))
545 bfd_putl32 (e
->cv_hash
% NUM_TPI_HASH_BUCKETS
, buf
);
547 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
548 != sizeof (uint32_t))
554 /* Write the index offsets, i.e. the skip list, into the hash stream. We
555 copy MSVC here by writing a new entry for every 8192 bytes. */
562 uint32_t old_off
= off
;
563 uint16_t size
= bfd_getl16 (e
->data
);
565 off
+= size
+ sizeof (uint16_t);
567 if (old_off
== 0 || old_off
/ index_skip
!= len
/ index_skip
)
569 uint8_t buf
[sizeof (uint32_t)];
571 bfd_putl32 (TPI_FIRST_INDEX
+ e
->index
, buf
);
573 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
574 != sizeof (uint32_t))
577 bfd_putl32 (old_off
, buf
);
579 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
580 != sizeof (uint32_t))
590 /* Return the PE architecture number for the image. */
592 get_arch_number (bfd
*abfd
)
594 switch (abfd
->arch_info
->arch
)
597 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
598 return IMAGE_FILE_MACHINE_AMD64
;
600 return IMAGE_FILE_MACHINE_I386
;
602 case bfd_arch_aarch64
:
603 return IMAGE_FILE_MACHINE_ARM64
;
610 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
611 section, and copy it to the module's symbol stream. */
613 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
614 struct string_table
*strings
, uint8_t *out
,
615 struct mod_source_files
*mod_source
)
617 uint8_t *orig_data
= data
;
618 uint32_t orig_size
= size
;
619 uint16_t num_files
= 0;
620 struct string
**strptr
;
622 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
623 out
+= sizeof (uint32_t);
625 bfd_putl32 (size
, out
);
626 out
+= sizeof (uint32_t);
628 /* Calculate the number of files, and check for any overflows. */
632 struct file_checksum
*fc
= (struct file_checksum
*) data
;
636 if (size
< sizeof (struct file_checksum
))
638 bfd_set_error (bfd_error_bad_value
);
642 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
646 bfd_set_error (bfd_error_bad_value
);
653 if (len
% sizeof (uint32_t))
654 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
660 bfd_set_error (bfd_error_bad_value
);
670 /* Add the files to mod_source, so that they'll appear in the source
676 uint16_t new_count
= num_files
+ mod_source
->files_count
;
678 mod_source
->files
= xrealloc (mod_source
->files
,
679 sizeof (struct string
*) * new_count
);
681 strptr
= mod_source
->files
+ mod_source
->files_count
;
683 mod_source
->files_count
+= num_files
;
686 /* Actually copy the data. */
693 struct file_checksum
*fc
= (struct file_checksum
*) data
;
697 struct string
*str
= NULL
;
699 string_off
= bfd_getl32 (&fc
->file_id
);
700 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
702 if (len
% sizeof (uint32_t))
703 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
707 /* Remap the "file ID", i.e. the offset in the module's string table,
708 so it points to the right place in the main string table. */
712 char *fn
= string_table
+ string_off
;
713 size_t fn_len
= strlen (fn
);
714 uint32_t hash
= calc_hash (fn
, fn_len
);
717 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
721 str
= (struct string
*) *slot
;
727 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
729 memcpy (out
, data
, len
+ padding
);
731 data
+= len
+ padding
;
732 size
-= len
+ padding
;
733 out
+= len
+ padding
;
739 /* Add a string to the strings table, if it's not already there. Returns its
740 offset within the string table. */
742 add_string (char *str
, size_t len
, struct string_table
*strings
)
744 uint32_t hash
= calc_hash (str
, len
);
748 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
752 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
754 s
= (struct string
*) *slot
;
758 s
->offset
= strings
->strings_len
;
759 s
->source_file_offset
= 0xffffffff;
761 memcpy (s
->s
, str
, len
);
763 if (strings
->strings_tail
)
764 strings
->strings_tail
->next
= s
;
766 strings
->strings_head
= s
;
768 strings
->strings_tail
= s
;
770 strings
->strings_len
+= len
+ 1;
774 s
= (struct string
*) *slot
;
780 /* Return the hash of an entry in the string table. */
782 hash_string_table_entry (const void *p
)
784 const struct string
*s
= (const struct string
*) p
;
789 /* Compare an entry in the string table with a string. */
791 eq_string_table_entry (const void *a
, const void *b
)
793 const struct string
*s1
= (const struct string
*) a
;
794 const char *s2
= (const char *) b
;
795 size_t s2_len
= strlen (s2
);
797 if (s2_len
!= s1
->len
)
800 return memcmp (s1
->s
, s2
, s2_len
) == 0;
803 /* Parse the string table within the .debug$S section. */
805 parse_string_table (bfd_byte
*data
, size_t size
,
806 struct string_table
*strings
)
810 size_t len
= strnlen ((char *) data
, size
);
812 add_string ((char *) data
, len
, strings
);
823 /* Remap a type reference within a CodeView symbol. */
825 remap_symbol_type (void *data
, struct type_entry
**map
, uint32_t num_types
)
827 uint32_t type
= bfd_getl32 (data
);
829 /* Ignore builtin types (those with IDs below 0x1000). */
830 if (type
< TPI_FIRST_INDEX
)
833 if (type
>= TPI_FIRST_INDEX
+ num_types
)
835 einfo (_("%P: CodeView symbol references out of range type %v\n"),
840 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
841 bfd_putl32 (type
, data
);
846 /* Add an entry into the globals stream. If it already exists, increase
849 add_globals_ref (struct globals
*glob
, bfd
*sym_rec_stream
, const char *name
,
850 size_t name_len
, uint8_t *data
, size_t len
)
856 slot
= htab_find_slot_with_hash (glob
->hashmap
, data
,
857 iterative_hash (data
, len
, 0), INSERT
);
866 *slot
= xmalloc (offsetof (struct global
, data
) + len
);
868 hash
= crc32 ((const uint8_t *) name
, name_len
);
869 hash
%= NUM_GLOBALS_HASH_BUCKETS
;
873 g
->offset
= bfd_tell (sym_rec_stream
);
876 memcpy (g
->data
, data
, len
+ 1);
881 glob
->last
->next
= g
;
887 return bfd_bwrite (data
, len
, sym_rec_stream
) == len
;
890 /* Find the end of the current scope within symbols data. */
892 find_end_of_scope (uint8_t *data
, uint32_t size
)
894 unsigned int scope_level
= 1;
897 len
= bfd_getl16 (data
) + sizeof (uint16_t);
906 if (size
< sizeof (uint32_t))
909 len
= bfd_getl16 (data
) + sizeof (uint16_t);
910 type
= bfd_getl16 (data
+ sizeof (uint16_t));
927 case S_INLINESITE_END
:
930 if (scope_level
== 0)
941 /* Return the size of an extended value parameter, as used in
944 extended_value_len (uint16_t type
)
967 /* Parse the symbols in a .debug$S section, and copy them to the module's
970 parse_symbols (uint8_t *data
, uint32_t size
, uint8_t **buf
,
971 struct type_entry
**map
, uint32_t num_types
,
972 bfd
*sym_rec_stream
, struct globals
*glob
, uint16_t mod_num
)
974 uint8_t *orig_buf
= *buf
;
975 unsigned int scope_level
= 0;
976 uint8_t *scope
= NULL
;
978 while (size
>= sizeof (uint16_t))
982 len
= bfd_getl16 (data
) + sizeof (uint16_t);
986 bfd_set_error (bfd_error_bad_value
);
990 type
= bfd_getl16 (data
+ sizeof (uint16_t));
999 struct datasym
*d
= (struct datasym
*) data
;
1002 if (len
< offsetof (struct datasym
, name
))
1004 einfo (_("%P: warning: truncated CodeView record"
1005 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1006 bfd_set_error (bfd_error_bad_value
);
1010 if (scope_level
== 0)
1012 uint16_t section
= bfd_getl16 (&d
->section
);
1014 if (section
== 0) /* GC'd, ignore */
1019 strnlen (d
->name
, len
- offsetof (struct datasym
, name
));
1021 if (name_len
== len
- offsetof (struct datasym
, name
))
1023 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1024 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1026 bfd_set_error (bfd_error_bad_value
);
1030 if (!remap_symbol_type (&d
->type
, map
, num_types
))
1032 bfd_set_error (bfd_error_bad_value
);
1036 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1038 if (type
== S_LDATA32
|| type
== S_LTHREAD32
)
1040 memcpy (*buf
, d
, len
);
1044 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1045 not in function scope. */
1046 if (type
== S_GDATA32
|| type
== S_GTHREAD32
|| scope_level
== 0)
1048 if (!add_globals_ref (glob
, sym_rec_stream
, d
->name
,
1049 name_len
, data
, len
))
1061 struct procsym
*proc
= (struct procsym
*) data
;
1066 size_t ref_size
, padding
;
1069 if (len
< offsetof (struct procsym
, name
))
1071 einfo (_("%P: warning: truncated CodeView record"
1072 " S_GPROC32/S_LPROC32\n"));
1073 bfd_set_error (bfd_error_bad_value
);
1077 section
= bfd_getl16 (&proc
->section
);
1079 endptr
= find_end_of_scope (data
, size
);
1083 einfo (_("%P: warning: could not find end of"
1084 " S_GPROC32/S_LPROC32 record\n"));
1085 bfd_set_error (bfd_error_bad_value
);
1089 if (section
== 0) /* skip if GC'd */
1091 /* Skip to after S_END. */
1093 size
-= endptr
- data
;
1096 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1105 strnlen (proc
->name
, len
- offsetof (struct procsym
, name
));
1107 if (name_len
== len
- offsetof (struct procsym
, name
))
1109 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1110 " terminating zero\n"));
1111 bfd_set_error (bfd_error_bad_value
);
1115 if (type
== S_GPROC32_ID
|| type
== S_LPROC32_ID
)
1117 /* Transform into S_GPROC32 / S_LPROC32. */
1119 uint32_t t_idx
= bfd_getl32 (&proc
->type
);
1120 struct type_entry
*t
;
1123 if (t_idx
< TPI_FIRST_INDEX
1124 || t_idx
>= TPI_FIRST_INDEX
+ num_types
)
1126 einfo (_("%P: CodeView symbol references out of range"
1127 " type %v\n"), type
);
1128 bfd_set_error (bfd_error_bad_value
);
1132 t
= map
[t_idx
- TPI_FIRST_INDEX
];
1134 t_type
= bfd_getl16 (t
->data
+ sizeof (uint16_t));
1140 struct lf_func_id
*t_data
=
1141 (struct lf_func_id
*) t
->data
;
1143 /* Replace proc->type with function type. */
1145 memcpy (&proc
->type
, &t_data
->function_type
,
1153 struct lf_mfunc_id
*t_data
=
1154 (struct lf_mfunc_id
*) t
->data
;
1156 /* Replace proc->type with function type. */
1158 memcpy (&proc
->type
, &t_data
->function_type
,
1165 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1166 " referenced unknown type as ID\n"));
1167 bfd_set_error (bfd_error_bad_value
);
1171 /* Change record type. */
1173 if (type
== S_GPROC32_ID
)
1174 bfd_putl32 (S_GPROC32
, &proc
->kind
);
1176 bfd_putl32 (S_LPROC32
, &proc
->kind
);
1180 if (!remap_symbol_type (&proc
->type
, map
, num_types
))
1182 bfd_set_error (bfd_error_bad_value
);
1187 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1188 bfd_putl32 (end
, &proc
->end
);
1190 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1192 ref_size
= offsetof (struct refsym
, name
) + name_len
+ 1;
1194 if (ref_size
% sizeof (uint32_t))
1195 padding
= sizeof (uint32_t) - (ref_size
% sizeof (uint32_t));
1199 ref
= xmalloc (ref_size
+ padding
);
1201 bfd_putl16 (ref_size
+ padding
- sizeof (uint16_t), &ref
->size
);
1202 bfd_putl16 (type
== S_GPROC32
|| type
== S_GPROC32_ID
?
1203 S_PROCREF
: S_LPROCREF
, &ref
->kind
);
1204 bfd_putl32 (0, &ref
->sum_name
);
1205 bfd_putl32 (*buf
- orig_buf
+ sizeof (uint32_t),
1206 &ref
->symbol_offset
);
1207 bfd_putl16 (mod_num
+ 1, &ref
->mod
);
1209 memcpy (ref
->name
, proc
->name
, name_len
+ 1);
1211 memset (ref
->name
+ name_len
+ 1, 0, padding
);
1213 if (!add_globals_ref (glob
, sym_rec_stream
, proc
->name
, name_len
,
1214 (uint8_t *) ref
, ref_size
+ padding
))
1224 memcpy (*buf
, proc
, len
);
1234 struct udtsym
*udt
= (struct udtsym
*) data
;
1237 if (len
< offsetof (struct udtsym
, name
))
1239 einfo (_("%P: warning: truncated CodeView record"
1241 bfd_set_error (bfd_error_bad_value
);
1246 strnlen (udt
->name
, len
- offsetof (struct udtsym
, name
));
1248 if (name_len
== len
- offsetof (struct udtsym
, name
))
1250 einfo (_("%P: warning: name for S_UDT has no"
1251 " terminating zero\n"));
1252 bfd_set_error (bfd_error_bad_value
);
1256 if (!remap_symbol_type (&udt
->type
, map
, num_types
))
1258 bfd_set_error (bfd_error_bad_value
);
1262 /* S_UDT goes in the symbols stream if within a procedure,
1263 otherwise it goes in the globals stream. */
1264 if (scope_level
== 0)
1266 if (!add_globals_ref (glob
, sym_rec_stream
, udt
->name
,
1267 name_len
, data
, len
))
1272 memcpy (*buf
, udt
, len
);
1281 struct constsym
*c
= (struct constsym
*) data
;
1282 size_t name_len
, rec_size
;
1285 if (len
< offsetof (struct constsym
, name
))
1287 einfo (_("%P: warning: truncated CodeView record"
1289 bfd_set_error (bfd_error_bad_value
);
1293 rec_size
= offsetof (struct constsym
, name
);
1295 val
= bfd_getl16 (&c
->value
);
1297 /* If val >= 0x8000, actual value follows. */
1300 unsigned int param_len
= extended_value_len (val
);
1304 einfo (_("%P: warning: unhandled type %v within"
1305 " S_CONSTANT\n"), val
);
1306 bfd_set_error (bfd_error_bad_value
);
1310 rec_size
+= param_len
;
1314 strnlen ((const char *) data
+ rec_size
, len
- rec_size
);
1316 if (name_len
== len
- rec_size
)
1318 einfo (_("%P: warning: name for S_CONSTANT has no"
1319 " terminating zero\n"));
1320 bfd_set_error (bfd_error_bad_value
);
1324 if (!remap_symbol_type (&c
->type
, map
, num_types
))
1326 bfd_set_error (bfd_error_bad_value
);
1330 if (!add_globals_ref (glob
, sym_rec_stream
,
1331 (const char *) data
+ rec_size
, name_len
,
1339 case S_INLINESITE_END
:
1341 memcpy (*buf
, data
, len
);
1343 if (type
== S_PROC_ID_END
) /* transform to S_END */
1344 bfd_putl16 (S_END
, *buf
+ sizeof (uint16_t));
1346 /* Reset scope variable back to the address of the previous
1351 uint16_t scope_start_type
=
1352 bfd_getl16 (scope
+ sizeof (uint16_t));
1354 switch (scope_start_type
)
1358 parent
= bfd_getl32 (scope
+ offsetof (struct procsym
,
1363 parent
= bfd_getl32 (scope
+ offsetof (struct blocksym
,
1368 parent
= bfd_getl32 (scope
+ offsetof (struct inline_site
,
1373 parent
= bfd_getl32 (scope
+ offsetof (struct thunk
,
1378 einfo (_("%P: warning: unexpected CodeView scope start"
1379 " record %v\n"), scope_start_type
);
1380 bfd_set_error (bfd_error_bad_value
);
1387 scope
= orig_buf
+ parent
- sizeof (uint32_t);
1396 struct buildinfosym
*bi
= (struct buildinfosym
*) data
;
1398 if (len
< sizeof (struct buildinfosym
))
1400 einfo (_("%P: warning: truncated CodeView record"
1402 bfd_set_error (bfd_error_bad_value
);
1406 if (!remap_symbol_type (&bi
->type
, map
, num_types
))
1408 bfd_set_error (bfd_error_bad_value
);
1412 memcpy (*buf
, data
, len
);
1420 struct blocksym
*bl
= (struct blocksym
*) data
;
1424 if (len
< offsetof (struct blocksym
, name
))
1426 einfo (_("%P: warning: truncated CodeView record"
1428 bfd_set_error (bfd_error_bad_value
);
1432 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &bl
->parent
);
1434 endptr
= find_end_of_scope (data
, size
);
1438 einfo (_("%P: warning: could not find end of"
1439 " S_BLOCK32 record\n"));
1440 bfd_set_error (bfd_error_bad_value
);
1444 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1445 bfd_putl32 (end
, &bl
->end
);
1449 memcpy (*buf
, data
, len
);
1459 struct bprelsym
*bp
= (struct bprelsym
*) data
;
1461 if (len
< offsetof (struct bprelsym
, name
))
1463 einfo (_("%P: warning: truncated CodeView record"
1465 bfd_set_error (bfd_error_bad_value
);
1469 if (!remap_symbol_type (&bp
->type
, map
, num_types
))
1471 bfd_set_error (bfd_error_bad_value
);
1475 memcpy (*buf
, data
, len
);
1483 struct regsym
*reg
= (struct regsym
*) data
;
1485 if (len
< offsetof (struct regsym
, name
))
1487 einfo (_("%P: warning: truncated CodeView record"
1489 bfd_set_error (bfd_error_bad_value
);
1493 if (!remap_symbol_type (®
->type
, map
, num_types
))
1495 bfd_set_error (bfd_error_bad_value
);
1499 memcpy (*buf
, data
, len
);
1507 struct regrel
*rr
= (struct regrel
*) data
;
1509 if (len
< offsetof (struct regrel
, name
))
1511 einfo (_("%P: warning: truncated CodeView record"
1513 bfd_set_error (bfd_error_bad_value
);
1517 if (!remap_symbol_type (&rr
->type
, map
, num_types
))
1519 bfd_set_error (bfd_error_bad_value
);
1523 memcpy (*buf
, data
, len
);
1531 struct localsym
*l
= (struct localsym
*) data
;
1533 if (len
< offsetof (struct localsym
, name
))
1535 einfo (_("%P: warning: truncated CodeView record"
1537 bfd_set_error (bfd_error_bad_value
);
1541 if (!remap_symbol_type (&l
->type
, map
, num_types
))
1543 bfd_set_error (bfd_error_bad_value
);
1547 memcpy (*buf
, data
, len
);
1555 struct inline_site
*is
= (struct inline_site
*) data
;
1559 if (len
< offsetof (struct inline_site
, binary_annotations
))
1561 einfo (_("%P: warning: truncated CodeView record"
1562 " S_INLINESITE\n"));
1563 bfd_set_error (bfd_error_bad_value
);
1567 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &is
->parent
);
1569 endptr
= find_end_of_scope (data
, size
);
1573 einfo (_("%P: warning: could not find end of"
1574 " S_INLINESITE record\n"));
1575 bfd_set_error (bfd_error_bad_value
);
1579 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1580 bfd_putl32 (end
, &is
->end
);
1582 if (!remap_symbol_type (&is
->inlinee
, map
, num_types
))
1584 bfd_set_error (bfd_error_bad_value
);
1590 memcpy (*buf
, data
, len
);
1600 struct thunk
*th
= (struct thunk
*) data
;
1604 if (len
< offsetof (struct thunk
, name
))
1606 einfo (_("%P: warning: truncated CodeView record"
1608 bfd_set_error (bfd_error_bad_value
);
1612 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &th
->parent
);
1614 endptr
= find_end_of_scope (data
, size
);
1618 einfo (_("%P: warning: could not find end of"
1619 " S_THUNK32 record\n"));
1620 bfd_set_error (bfd_error_bad_value
);
1624 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1625 bfd_putl32 (end
, &th
->end
);
1629 memcpy (*buf
, data
, len
);
1637 case S_HEAPALLOCSITE
:
1639 struct heap_alloc_site
*has
= (struct heap_alloc_site
*) data
;
1641 if (len
< sizeof (struct heap_alloc_site
))
1643 einfo (_("%P: warning: truncated CodeView record"
1644 " S_HEAPALLOCSITE\n"));
1645 bfd_set_error (bfd_error_bad_value
);
1649 if (!remap_symbol_type (&has
->type
, map
, num_types
))
1651 bfd_set_error (bfd_error_bad_value
);
1655 memcpy (*buf
, data
, len
);
1661 case S_OBJNAME
: /* just copy */
1667 case S_DEFRANGE_REGISTER_REL
:
1668 case S_DEFRANGE_FRAMEPOINTER_REL
:
1669 case S_DEFRANGE_SUBFIELD_REGISTER
:
1670 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1671 case S_DEFRANGE_REGISTER
:
1672 memcpy (*buf
, data
, len
);
1677 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1678 bfd_set_error (bfd_error_bad_value
);
1689 /* For a given symbol subsection, work out how much space to allocate in the
1690 result module stream. This is different because we don't copy certain
1691 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1692 have been GC'd out. */
1694 calculate_symbols_size (uint8_t *data
, uint32_t size
, uint32_t *sym_size
)
1696 unsigned int scope_level
= 0;
1698 while (size
>= sizeof (uint32_t))
1700 uint16_t len
= bfd_getl16 (data
) + sizeof (uint16_t);
1701 uint16_t type
= bfd_getl16 (data
+ sizeof (uint16_t));
1708 struct datasym
*d
= (struct datasym
*) data
;
1711 if (len
< offsetof (struct datasym
, name
))
1713 einfo (_("%P: warning: truncated CodeView record"
1714 " S_LDATA32/S_LTHREAD32\n"));
1718 section
= bfd_getl16 (&d
->section
);
1720 /* copy if not GC'd or within function */
1721 if (scope_level
!= 0 || section
!= 0)
1728 /* Not copied into symbols stream. */
1736 struct procsym
*proc
= (struct procsym
*) data
;
1739 if (len
< offsetof (struct procsym
, name
))
1741 einfo (_("%P: warning: truncated CodeView record"
1742 " S_GPROC32/S_LPROC32\n"));
1746 section
= bfd_getl16 (&proc
->section
);
1754 uint8_t *endptr
= find_end_of_scope (data
, size
);
1758 einfo (_("%P: warning: could not find end of"
1759 " S_GPROC32/S_LPROC32 record\n"));
1763 /* Skip to after S_END. */
1765 size
-= endptr
- data
;
1768 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1782 if (scope_level
!= 0) /* only goes in symbols if local */
1786 case S_BLOCK32
: /* always copied */
1793 case S_END
: /* always copied */
1795 case S_INLINESITE_END
:
1800 case S_OBJNAME
: /* always copied */
1811 case S_DEFRANGE_REGISTER_REL
:
1812 case S_DEFRANGE_FRAMEPOINTER_REL
:
1813 case S_DEFRANGE_SUBFIELD_REGISTER
:
1814 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1815 case S_DEFRANGE_REGISTER
:
1816 case S_HEAPALLOCSITE
:
1821 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1832 /* Parse the .debug$S section within an object file. */
1834 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
1835 uint8_t **dataptr
, uint32_t *sizeptr
,
1836 struct mod_source_files
*mod_source
,
1837 bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
1838 struct type_entry
**map
, uint32_t num_types
,
1839 bfd
*sym_rec_stream
, struct globals
*glob
,
1842 bfd_byte
*data
= NULL
;
1844 uint32_t c13_size
= 0;
1845 char *string_table
= NULL
;
1846 uint8_t *buf
, *bufptr
, *symbuf
, *symbufptr
;
1847 uint32_t sym_size
= 0;
1849 if (!bfd_get_full_section_contents (mod
, s
, &data
))
1855 /* Resolve relocations. Addresses are stored within the .debug$S section as
1856 a .secidx, .secrel32 pair. */
1858 if (s
->flags
& SEC_RELOC
)
1860 struct internal_reloc
*relocs
;
1861 struct internal_syment
*symbols
;
1862 asection
**sectlist
;
1863 unsigned int syment_count
;
1865 struct external_syment
*ext
;
1867 syment_count
= obj_raw_syment_count (mod
);
1870 _bfd_coff_read_internal_relocs (mod
, s
, false, NULL
, true, NULL
);
1872 symbols
= xmalloc (sizeof (struct internal_syment
) * syment_count
);
1873 sectlist
= xmalloc (sizeof (asection
*) * syment_count
);
1875 ext
= (struct external_syment
*) (coff_data (mod
)->external_syms
);
1877 for (unsigned int i
= 0; i
< syment_count
; i
++)
1879 bfd_coff_swap_sym_in (mod
, &ext
[i
], &symbols
[i
]);
1884 for (asection
*sect
= mod
->sections
; sect
; sect
= sect
->next
)
1886 for (unsigned int i
= 0; i
< syment_count
; i
++)
1888 if (symbols
[i
].n_scnum
== sect_num
)
1895 if (!bfd_coff_relocate_section (abfd
, coff_data (abfd
)->link_info
, mod
,
1896 s
, data
, relocs
, symbols
, sectlist
))
1908 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
1914 off
= sizeof (uint32_t);
1916 /* calculate size */
1918 while (off
+ sizeof (uint32_t) <= s
->size
)
1920 uint32_t type
, size
;
1922 type
= bfd_getl32 (data
+ off
);
1924 off
+= sizeof (uint32_t);
1926 if (off
+ sizeof (uint32_t) > s
->size
)
1929 bfd_set_error (bfd_error_bad_value
);
1933 size
= bfd_getl32 (data
+ off
);
1935 off
+= sizeof (uint32_t);
1937 if (off
+ size
> s
->size
)
1940 bfd_set_error (bfd_error_bad_value
);
1946 case DEBUG_S_FILECHKSMS
:
1947 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1949 if (c13_size
% sizeof (uint32_t))
1950 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
1954 case DEBUG_S_STRINGTABLE
:
1955 parse_string_table (data
+ off
, size
, strings
);
1957 string_table
= (char *) data
+ off
;
1965 if (size
< sizeof (uint32_t) + sizeof (uint16_t))
1968 bfd_set_error (bfd_error_bad_value
);
1972 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
1974 /* Skip GC'd symbols. */
1977 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1979 if (c13_size
% sizeof (uint32_t))
1981 sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
1987 case DEBUG_S_SYMBOLS
:
1988 if (!calculate_symbols_size (data
+ off
, size
, &sym_size
))
1991 bfd_set_error (bfd_error_bad_value
);
2000 if (off
% sizeof (uint32_t))
2001 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2004 if (sym_size
% sizeof (uint32_t))
2005 sym_size
+= sizeof (uint32_t) - (sym_size
% sizeof (uint32_t));
2007 if (c13_size
== 0 && sym_size
== 0)
2017 buf
= xmalloc (c13_size
);
2022 symbuf
= xmalloc (sym_size
);
2025 off
= sizeof (uint32_t);
2027 while (off
+ sizeof (uint32_t) <= s
->size
)
2029 uint32_t type
, size
;
2031 type
= bfd_getl32 (data
+ off
);
2032 off
+= sizeof (uint32_t);
2034 size
= bfd_getl32 (data
+ off
);
2035 off
+= sizeof (uint32_t);
2039 case DEBUG_S_FILECHKSMS
:
2040 if (!copy_filechksms (data
+ off
, size
, string_table
,
2041 strings
, bufptr
, mod_source
))
2048 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
2056 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
2061 bfd_putl32 (type
, bufptr
);
2062 bufptr
+= sizeof (uint32_t);
2064 bfd_putl32 (size
, bufptr
);
2065 bufptr
+= sizeof (uint32_t);
2067 memcpy (bufptr
, data
+ off
, size
);
2074 case DEBUG_S_SYMBOLS
:
2075 if (!parse_symbols (data
+ off
, size
, &symbufptr
, map
, num_types
,
2076 sym_rec_stream
, glob
, mod_num
))
2088 if (off
% sizeof (uint32_t))
2089 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2098 /* Append the C13 info to what's already there, if the module has
2099 multiple .debug$S sections. */
2101 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
2102 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
2111 *sizeptr
+= c13_size
;
2118 *syms
= xrealloc (*syms
, *sym_byte_size
+ sym_size
);
2119 memcpy (*syms
+ *sym_byte_size
, symbuf
, sym_size
);
2128 *sym_byte_size
+= sym_size
;
2134 /* Remap the type number stored in data from the per-module numbering to
2135 that of the deduplicated output list. */
2137 remap_type (void *data
, struct type_entry
**map
,
2138 uint32_t type_num
, uint32_t num_types
)
2140 uint32_t type
= bfd_getl32 (data
);
2142 /* Ignore builtin types (those with IDs below 0x1000). */
2143 if (type
< TPI_FIRST_INDEX
)
2146 if (type
>= TPI_FIRST_INDEX
+ type_num
)
2148 einfo (_("%P: CodeView type %v references other type %v not yet "
2149 "declared\n"), TPI_FIRST_INDEX
+ type_num
, type
);
2153 if (type
>= TPI_FIRST_INDEX
+ num_types
)
2155 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2156 TPI_FIRST_INDEX
+ type_num
, type
);
2160 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
2161 bfd_putl32 (type
, data
);
2166 /* Determines whether the name of a struct, class, or union counts as
2167 "anonymous". Non-anonymous types have a hash based on just the name,
2168 rather than the whole structure. */
2170 is_name_anonymous (char *name
, size_t len
)
2172 static const char tag1
[] = "<unnamed-tag>";
2173 static const char tag2
[] = "__unnamed";
2174 static const char tag3
[] = "::<unnamed-tag>";
2175 static const char tag4
[] = "::__unnamed";
2177 if (len
== sizeof (tag1
) - 1 && !memcmp (name
, tag1
, sizeof (tag1
) - 1))
2180 if (len
== sizeof (tag2
) - 1 && !memcmp (name
, tag2
, sizeof (tag2
) - 1))
2183 if (len
>= sizeof (tag3
) - 1
2184 && !memcmp (name
+ len
- sizeof (tag3
) + 1, tag3
, sizeof (tag3
) - 1))
2187 if (len
>= sizeof (tag4
) - 1
2188 && !memcmp (name
+ len
- sizeof (tag4
) + 1, tag4
, sizeof (tag4
) - 1))
2194 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2195 give the source file and line number for each user-defined type that is
2196 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2197 which also includes the module number. */
2199 handle_udt_src_line (uint8_t *data
, uint16_t size
, struct type_entry
**map
,
2200 uint32_t type_num
, uint32_t num_types
,
2201 struct types
*ids
, uint16_t mod_num
,
2202 struct string_table
*strings
)
2204 struct lf_udt_src_line
*usl
= (struct lf_udt_src_line
*) data
;
2205 uint32_t orig_type
, source_file_type
;
2208 struct type_entry
*e
, *type_e
, *str_e
;
2209 struct lf_udt_mod_src_line
*umsl
;
2210 struct lf_string_id
*str
;
2211 uint32_t source_file_offset
;
2213 if (size
< sizeof (struct lf_udt_src_line
))
2215 einfo (_("%P: warning: truncated CodeView type record"
2216 " LF_UDT_SRC_LINE\n"));
2220 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2222 orig_type
= bfd_getl32 (&usl
->type
);
2224 if (orig_type
< TPI_FIRST_INDEX
||
2225 orig_type
>= TPI_FIRST_INDEX
+ num_types
||
2226 !map
[orig_type
- TPI_FIRST_INDEX
])
2228 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2229 " referred to unknown type %v\n"), orig_type
);
2233 type_e
= map
[orig_type
- TPI_FIRST_INDEX
];
2235 /* Skip if type already declared in other module. */
2236 if (type_e
->has_udt_src_line
)
2239 if (!remap_type (&usl
->type
, map
, type_num
, num_types
))
2242 /* Extract string from source_file_type. */
2244 source_file_type
= bfd_getl32 (&usl
->source_file_type
);
2246 if (source_file_type
< TPI_FIRST_INDEX
||
2247 source_file_type
>= TPI_FIRST_INDEX
+ num_types
||
2248 !map
[source_file_type
- TPI_FIRST_INDEX
])
2250 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2251 " referred to unknown string %v\n"), source_file_type
);
2255 str_e
= map
[source_file_type
- TPI_FIRST_INDEX
];
2257 if (bfd_getl16 (str_e
->data
+ sizeof (uint16_t)) != LF_STRING_ID
)
2259 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2260 " pointed to unexpected record type\n"));
2264 str
= (struct lf_string_id
*) str_e
->data
;
2266 /* Add string to string table. */
2268 source_file_offset
= add_string (str
->string
, strlen (str
->string
),
2271 /* Add LF_UDT_MOD_SRC_LINE entry. */
2273 size
= sizeof (struct lf_udt_mod_src_line
);
2275 e
= xmalloc (offsetof (struct type_entry
, data
) + size
);
2278 e
->index
= ids
->num_types
;
2279 e
->has_udt_src_line
= false;
2281 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2282 the crc32 used for type hashes elsewhere. */
2283 e
->cv_hash
= calc_hash ((char *) &usl
->type
, sizeof (uint32_t));
2285 type_e
->has_udt_src_line
= true;
2287 umsl
= (struct lf_udt_mod_src_line
*) e
->data
;
2289 bfd_putl16 (size
- sizeof (uint16_t), &umsl
->size
);
2290 bfd_putl16 (LF_UDT_MOD_SRC_LINE
, &umsl
->kind
);
2291 memcpy (&umsl
->type
, &usl
->type
, sizeof (uint32_t));
2292 bfd_putl32 (source_file_offset
, &umsl
->source_file_string
);
2293 memcpy (&umsl
->line_no
, &usl
->line_no
, sizeof (uint32_t));
2294 bfd_putl16 (mod_num
+ 1, &umsl
->module_no
);
2296 hash
= iterative_hash (e
->data
, size
, 0);
2298 slot
= htab_find_slot_with_hash (ids
->hashmap
, data
, hash
, INSERT
);
2308 einfo (_("%P: warning: duplicate CodeView type record "
2309 "LF_UDT_MOD_SRC_LINE\n"));
2316 ids
->last
->next
= e
;
2329 /* Parse a type definition in the .debug$T section. We remap the numbers
2330 of any referenced types, and if the type is not a duplicate of one
2331 already seen add it to types (for TPI types) or ids (for IPI types). */
2333 handle_type (uint8_t *data
, struct type_entry
**map
, uint32_t type_num
,
2334 uint32_t num_types
, struct types
*types
,
2335 struct types
*ids
, uint16_t mod_num
,
2336 struct string_table
*strings
)
2338 uint16_t size
, type
;
2341 bool other_hash
= false;
2346 size
= bfd_getl16 (data
) + sizeof (uint16_t);
2347 type
= bfd_getl16 (data
+ sizeof (uint16_t));
2353 struct lf_modifier
*mod
= (struct lf_modifier
*) data
;
2355 if (size
< offsetof (struct lf_modifier
, modifier
))
2357 einfo (_("%P: warning: truncated CodeView type record "
2362 if (!remap_type (&mod
->base_type
, map
, type_num
, num_types
))
2370 struct lf_pointer
*ptr
= (struct lf_pointer
*) data
;
2372 if (size
< offsetof (struct lf_pointer
, attributes
))
2374 einfo (_("%P: warning: truncated CodeView type record"
2379 if (!remap_type (&ptr
->base_type
, map
, type_num
, num_types
))
2387 struct lf_procedure
*proc
= (struct lf_procedure
*) data
;
2389 if (size
< sizeof (struct lf_procedure
))
2391 einfo (_("%P: warning: truncated CodeView type record"
2392 " LF_PROCEDURE\n"));
2396 if (!remap_type (&proc
->return_type
, map
, type_num
, num_types
))
2399 if (!remap_type (&proc
->arglist
, map
, type_num
, num_types
))
2407 struct lf_mfunction
*func
= (struct lf_mfunction
*) data
;
2409 if (size
< sizeof (struct lf_procedure
))
2411 einfo (_("%P: warning: truncated CodeView type record"
2412 " LF_MFUNCTION\n"));
2416 if (!remap_type (&func
->return_type
, map
, type_num
, num_types
))
2419 if (!remap_type (&func
->containing_class_type
, map
, type_num
,
2423 if (!remap_type (&func
->this_type
, map
, type_num
, num_types
))
2426 if (!remap_type (&func
->arglist
, map
, type_num
, num_types
))
2434 uint32_t num_entries
;
2435 struct lf_arglist
*al
= (struct lf_arglist
*) data
;
2437 if (size
< offsetof (struct lf_arglist
, args
))
2439 einfo (_("%P: warning: truncated CodeView type record"
2444 num_entries
= bfd_getl32 (&al
->num_entries
);
2446 if (size
< offsetof (struct lf_arglist
, args
)
2447 + (num_entries
* sizeof (uint32_t)))
2449 einfo (_("%P: warning: truncated CodeView type record"
2454 for (uint32_t i
= 0; i
< num_entries
; i
++)
2456 if (!remap_type (&al
->args
[i
], map
, type_num
, num_types
))
2465 uint16_t left
= size
- sizeof (uint16_t) - sizeof (uint16_t);
2466 uint8_t *ptr
= data
+ sizeof (uint16_t) + sizeof (uint16_t);
2472 if (left
< sizeof (uint16_t))
2474 einfo (_("%P: warning: truncated CodeView type record"
2475 " LF_FIELDLIST\n"));
2479 subtype
= bfd_getl16 (ptr
);
2485 struct lf_member
*mem
= (struct lf_member
*) ptr
;
2487 size_t name_len
, subtype_len
;
2489 if (left
< offsetof (struct lf_member
, name
))
2491 einfo (_("%P: warning: truncated CodeView type record"
2496 if (!remap_type (&mem
->type
, map
, type_num
, num_types
))
2499 subtype_len
= offsetof (struct lf_member
, name
);
2501 offset
= bfd_getl16 (&mem
->offset
);
2503 /* If offset >= 0x8000, actual value follows. */
2504 if (offset
>= 0x8000)
2506 unsigned int param_len
= extended_value_len (offset
);
2510 einfo (_("%P: warning: unhandled type %v within"
2511 " LF_MEMBER\n"), offset
);
2515 subtype_len
+= param_len
;
2517 if (left
< subtype_len
)
2519 einfo (_("%P: warning: truncated CodeView type record"
2526 strnlen ((char *) mem
+ subtype_len
, left
- subtype_len
);
2528 if (name_len
== left
- offsetof (struct lf_member
, name
))
2530 einfo (_("%P: warning: name for LF_MEMBER has no"
2531 " terminating zero\n"));
2537 subtype_len
+= name_len
;
2539 if (subtype_len
% 4 != 0)
2540 subtype_len
+= 4 - (subtype_len
% 4);
2542 if (left
< subtype_len
)
2544 einfo (_("%P: warning: truncated CodeView type record"
2545 " LF_FIELDLIST\n"));
2550 left
-= subtype_len
;
2557 struct lf_enumerate
*en
= (struct lf_enumerate
*) ptr
;
2558 size_t name_len
, subtype_len
;
2561 if (left
< offsetof (struct lf_enumerate
, name
))
2563 einfo (_("%P: warning: truncated CodeView type record"
2564 " LF_ENUMERATE\n"));
2568 subtype_len
= offsetof (struct lf_enumerate
, name
);
2570 val
= bfd_getl16 (&en
->value
);
2572 /* If val >= 0x8000, the actual value immediately follows. */
2575 unsigned int param_len
= extended_value_len (val
);
2579 einfo (_("%P: warning: unhandled type %v within"
2580 " LF_ENUMERATE\n"), val
);
2584 if (left
< subtype_len
+ param_len
)
2586 einfo (_("%P: warning: truncated CodeView type"
2587 " record LF_ENUMERATE\n"));
2591 subtype_len
+= param_len
;
2594 name_len
= strnlen ((char *) ptr
+ subtype_len
,
2595 left
- subtype_len
);
2597 if (name_len
== left
- offsetof (struct lf_enumerate
, name
))
2599 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2600 " terminating zero\n"));
2606 subtype_len
+= name_len
;
2608 if (subtype_len
% 4 != 0)
2609 subtype_len
+= 4 - (subtype_len
% 4);
2611 if (left
< subtype_len
)
2613 einfo (_("%P: warning: truncated CodeView type record"
2614 " LF_ENUMERATE\n"));
2619 left
-= subtype_len
;
2626 struct lf_index
*ind
= (struct lf_index
*) ptr
;
2628 if (left
< sizeof (struct lf_index
))
2630 einfo (_("%P: warning: truncated CodeView type record"
2635 if (!remap_type (&ind
->index
, map
, type_num
, num_types
))
2638 ptr
+= sizeof (struct lf_index
);
2639 left
-= sizeof (struct lf_index
);
2646 struct lf_onemethod
*meth
= (struct lf_onemethod
*) ptr
;
2647 size_t name_len
, subtype_len
;
2649 if (left
< offsetof (struct lf_onemethod
, name
))
2651 einfo (_("%P: warning: truncated CodeView type record"
2652 " LF_ONEMETHOD\n"));
2656 if (!remap_type (&meth
->method_type
, map
, type_num
,
2661 strnlen (meth
->name
,
2662 left
- offsetof (struct lf_onemethod
, name
));
2664 if (name_len
== left
- offsetof (struct lf_onemethod
, name
))
2666 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2667 " terminating zero\n"));
2673 subtype_len
= offsetof (struct lf_onemethod
, name
)
2676 if (subtype_len
% 4 != 0)
2677 subtype_len
+= 4 - (subtype_len
% 4);
2679 if (left
< subtype_len
)
2681 einfo (_("%P: warning: truncated CodeView type record"
2682 " LF_FIELDLIST\n"));
2687 left
-= subtype_len
;
2694 struct lf_method
*meth
= (struct lf_method
*) ptr
;
2695 size_t name_len
, subtype_len
;
2697 if (left
< offsetof (struct lf_method
, name
))
2699 einfo (_("%P: warning: truncated CodeView type record"
2704 if (!remap_type (&meth
->method_list
, map
, type_num
,
2709 strnlen (meth
->name
,
2710 left
- offsetof (struct lf_method
, name
));
2712 if (name_len
== left
- offsetof (struct lf_method
, name
))
2714 einfo (_("%P: warning: name for LF_METHOD has no"
2715 " terminating zero\n"));
2721 subtype_len
= offsetof (struct lf_method
, name
) + name_len
;
2723 if (subtype_len
% 4 != 0)
2724 subtype_len
+= 4 - (subtype_len
% 4);
2726 if (left
< subtype_len
)
2728 einfo (_("%P: warning: truncated CodeView type record"
2729 " LF_FIELDLIST\n"));
2734 left
-= subtype_len
;
2741 struct lf_bclass
*bc
= (struct lf_bclass
*) ptr
;
2745 if (left
< sizeof (struct lf_bclass
))
2747 einfo (_("%P: warning: truncated CodeView type record"
2752 if (!remap_type (&bc
->base_class_type
, map
, type_num
,
2756 subtype_len
= sizeof (struct lf_bclass
);
2758 offset
= bfd_getl16 (&bc
->offset
);
2760 /* If offset >= 0x8000, actual value follows. */
2761 if (offset
>= 0x8000)
2763 unsigned int param_len
= extended_value_len (offset
);
2767 einfo (_("%P: warning: unhandled type %v within"
2768 " LF_BCLASS\n"), offset
);
2772 subtype_len
+= param_len
;
2774 if (left
< subtype_len
)
2776 einfo (_("%P: warning: truncated CodeView type record"
2782 if (subtype_len
% 4 != 0)
2783 subtype_len
+= 4 - (subtype_len
% 4);
2785 if (left
< subtype_len
)
2787 einfo (_("%P: warning: truncated CodeView type record"
2793 left
-= subtype_len
;
2800 struct lf_vfunctab
*vft
= (struct lf_vfunctab
*) ptr
;
2802 if (left
< sizeof (struct lf_vfunctab
))
2804 einfo (_("%P: warning: truncated CodeView type record"
2809 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
2812 ptr
+= sizeof (struct lf_vfunctab
);
2813 left
-= sizeof (struct lf_vfunctab
);
2821 struct lf_vbclass
*vbc
= (struct lf_vbclass
*) ptr
;
2825 if (left
< sizeof (struct lf_vbclass
))
2827 einfo (_("%P: warning: truncated CodeView type record"
2828 " LF_VBCLASS/LF_IVBCLASS\n"));
2832 if (!remap_type (&vbc
->base_class_type
, map
, type_num
,
2836 if (!remap_type (&vbc
->virtual_base_pointer_type
, map
,
2837 type_num
, num_types
))
2840 subtype_len
= offsetof (struct lf_vbclass
,
2841 virtual_base_vbtable_offset
);
2843 offset
= bfd_getl16 (&vbc
->virtual_base_pointer_offset
);
2845 /* If offset >= 0x8000, actual value follows. */
2846 if (offset
>= 0x8000)
2848 unsigned int param_len
= extended_value_len (offset
);
2852 einfo (_("%P: warning: unhandled type %v within"
2853 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2857 subtype_len
+= param_len
;
2859 if (left
< subtype_len
)
2861 einfo (_("%P: warning: truncated CodeView type record"
2862 " LF_VBCLASS/LF_IVBCLASS\n"));
2867 offset
= bfd_getl16 ((char *)vbc
+ subtype_len
);
2868 subtype_len
+= sizeof (uint16_t);
2870 /* If offset >= 0x8000, actual value follows. */
2871 if (offset
>= 0x8000)
2873 unsigned int param_len
= extended_value_len (offset
);
2877 einfo (_("%P: warning: unhandled type %v within"
2878 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2882 subtype_len
+= param_len
;
2884 if (left
< subtype_len
)
2886 einfo (_("%P: warning: truncated CodeView type record"
2887 " LF_VBCLASS/LF_IVBCLASS\n"));
2892 if (subtype_len
% 4 != 0)
2893 subtype_len
+= 4 - (subtype_len
% 4);
2895 if (left
< subtype_len
)
2897 einfo (_("%P: warning: truncated CodeView type record"
2898 " LF_VBCLASS/LF_IVBCLASS\n"));
2903 left
-= subtype_len
;
2910 struct lf_static_member
*st
=
2911 (struct lf_static_member
*) ptr
;
2912 size_t name_len
, subtype_len
;
2914 if (left
< offsetof (struct lf_static_member
, name
))
2916 einfo (_("%P: warning: truncated CodeView type record"
2921 if (!remap_type (&st
->type
, map
, type_num
, num_types
))
2926 left
- offsetof (struct lf_static_member
, name
));
2928 if (name_len
== left
2929 - offsetof (struct lf_static_member
, name
))
2931 einfo (_("%P: warning: name for LF_STMEMBER has no"
2932 " terminating zero\n"));
2938 subtype_len
= offsetof (struct lf_static_member
, name
)
2941 if (subtype_len
% 4 != 0)
2942 subtype_len
+= 4 - (subtype_len
% 4);
2944 if (left
< subtype_len
)
2946 einfo (_("%P: warning: truncated CodeView type record"
2947 " LF_FIELDLIST\n"));
2952 left
-= subtype_len
;
2959 struct lf_nest_type
*nest
= (struct lf_nest_type
*) ptr
;
2960 size_t name_len
, subtype_len
;
2962 if (left
< offsetof (struct lf_nest_type
, name
))
2964 einfo (_("%P: warning: truncated CodeView type record"
2969 if (!remap_type (&nest
->type
, map
, type_num
, num_types
))
2973 strnlen (nest
->name
,
2974 left
- offsetof (struct lf_nest_type
, name
));
2976 if (name_len
== left
- offsetof (struct lf_nest_type
, name
))
2978 einfo (_("%P: warning: name for LF_NESTTYPE has no"
2979 " terminating zero\n"));
2985 subtype_len
= offsetof (struct lf_nest_type
, name
)
2988 if (subtype_len
% 4 != 0)
2989 subtype_len
+= 4 - (subtype_len
% 4);
2991 if (left
< subtype_len
)
2993 einfo (_("%P: warning: truncated CodeView type record"
2994 " LF_FIELDLIST\n"));
2999 left
-= subtype_len
;
3005 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3016 struct lf_bitfield
*bf
= (struct lf_bitfield
*) data
;
3018 if (size
< offsetof (struct lf_bitfield
, length
))
3020 einfo (_("%P: warning: truncated CodeView type record"
3025 if (!remap_type (&bf
->base_type
, map
, type_num
, num_types
))
3033 struct lf_methodlist
*ml
= (struct lf_methodlist
*) data
;
3034 unsigned int num_entries
;
3036 if (size
< offsetof (struct lf_methodlist
, entries
))
3038 einfo (_("%P: warning: truncated CodeView type record"
3039 " LF_METHODLIST\n"));
3043 if ((size
- offsetof (struct lf_methodlist
, entries
))
3044 % sizeof (struct lf_methodlist_entry
))
3046 einfo (_("%P: warning: malformed CodeView type record"
3047 " LF_METHODLIST\n"));
3051 num_entries
= (size
- offsetof (struct lf_methodlist
, entries
))
3052 / sizeof (struct lf_methodlist_entry
);
3054 for (unsigned int i
= 0; i
< num_entries
; i
++)
3056 if (!remap_type (&ml
->entries
[i
].method_type
, map
,
3057 type_num
, num_types
))
3066 struct lf_array
*arr
= (struct lf_array
*) data
;
3068 if (size
< offsetof (struct lf_array
, length_in_bytes
))
3070 einfo (_("%P: warning: truncated CodeView type record"
3075 if (!remap_type (&arr
->element_type
, map
, type_num
, num_types
))
3078 if (!remap_type (&arr
->index_type
, map
, type_num
, num_types
))
3087 struct lf_class
*cl
= (struct lf_class
*) data
;
3088 uint16_t prop
, num_bytes
;
3089 size_t name_len
, name_off
;
3091 if (size
< offsetof (struct lf_class
, name
))
3093 einfo (_("%P: warning: truncated CodeView type record"
3094 " LF_CLASS/LF_STRUCTURE\n"));
3098 if (!remap_type (&cl
->field_list
, map
, type_num
, num_types
))
3101 if (!remap_type (&cl
->derived_from
, map
, type_num
, num_types
))
3104 if (!remap_type (&cl
->vshape
, map
, type_num
, num_types
))
3107 name_off
= offsetof (struct lf_class
, name
);
3109 num_bytes
= bfd_getl16 (&cl
->length
);
3111 /* If num_bytes >= 0x8000, actual value follows. */
3112 if (num_bytes
>= 0x8000)
3114 unsigned int param_len
= extended_value_len (num_bytes
);
3118 einfo (_("%P: warning: unhandled type %v within"
3119 " LF_CLASS/LF_STRUCTURE\n"), num_bytes
);
3123 name_off
+= param_len
;
3125 if (size
< name_off
)
3127 einfo (_("%P: warning: truncated CodeView type record"
3128 " LF_CLASS/LF_STRUCTURE\n"));
3133 name_len
= strnlen ((char *) cl
+ name_off
, size
- name_off
);
3135 if (name_len
== size
- name_off
)
3137 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3138 " terminating zero\n"));
3142 prop
= bfd_getl16 (&cl
->properties
);
3144 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3146 /* Structure has another name following first one. */
3148 size_t len
= name_off
+ name_len
+ 1;
3149 size_t unique_name_len
;
3151 unique_name_len
= strnlen ((char *) cl
+ name_off
+ name_len
+ 1,
3154 if (unique_name_len
== size
- len
)
3156 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3157 " has no terminating zero\n"));
3162 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3163 && !is_name_anonymous ((char *) cl
+ name_off
, name_len
))
3166 cv_hash
= crc32 ((uint8_t *) cl
+ name_off
, name_len
);
3174 struct lf_union
*un
= (struct lf_union
*) data
;
3175 uint16_t prop
, num_bytes
;
3176 size_t name_len
, name_off
;
3178 if (size
< offsetof (struct lf_union
, name
))
3180 einfo (_("%P: warning: truncated CodeView type record"
3185 if (!remap_type (&un
->field_list
, map
, type_num
, num_types
))
3188 name_off
= offsetof (struct lf_union
, name
);
3190 num_bytes
= bfd_getl16 (&un
->length
);
3192 /* If num_bytes >= 0x8000, actual value follows. */
3193 if (num_bytes
>= 0x8000)
3195 unsigned int param_len
= extended_value_len (num_bytes
);
3199 einfo (_("%P: warning: unhandled type %v within"
3200 " LF_UNION\n"), num_bytes
);
3204 name_off
+= param_len
;
3206 if (size
< name_off
)
3208 einfo (_("%P: warning: truncated CodeView type record"
3214 name_len
= strnlen ((char *) un
+ name_off
, size
- name_off
);
3216 if (name_len
== size
- name_off
)
3218 einfo (_("%P: warning: name for LF_UNION has no"
3219 " terminating zero\n"));
3223 prop
= bfd_getl16 (&un
->properties
);
3225 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3227 /* Structure has another name following first one. */
3229 size_t len
= name_off
+ name_len
+ 1;
3230 size_t unique_name_len
;
3232 unique_name_len
= strnlen ((char *) un
+ name_off
+ name_len
+ 1,
3235 if (unique_name_len
== size
- len
)
3237 einfo (_("%P: warning: unique name for LF_UNION has"
3238 " no terminating zero\n"));
3243 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3244 && !is_name_anonymous ((char *) un
+ name_off
, name_len
))
3247 cv_hash
= crc32 ((uint8_t *) un
+ name_off
, name_len
);
3255 struct lf_enum
*en
= (struct lf_enum
*) data
;
3259 if (size
< offsetof (struct lf_enum
, name
))
3261 einfo (_("%P: warning: truncated CodeView type record"
3266 if (!remap_type (&en
->underlying_type
, map
, type_num
, num_types
))
3269 if (!remap_type (&en
->field_list
, map
, type_num
, num_types
))
3272 name_len
= strnlen (en
->name
, size
- offsetof (struct lf_enum
, name
));
3274 if (name_len
== size
- offsetof (struct lf_enum
, name
))
3276 einfo (_("%P: warning: name for LF_ENUM has no"
3277 " terminating zero\n"));
3281 prop
= bfd_getl16 (&en
->properties
);
3283 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3285 /* Structure has another name following first one. */
3287 size_t len
= offsetof (struct lf_enum
, name
) + name_len
+ 1;
3288 size_t unique_name_len
;
3290 unique_name_len
= strnlen (en
->name
+ name_len
+ 1, size
- len
);
3292 if (unique_name_len
== size
- len
)
3294 einfo (_("%P: warning: unique name for LF_ENUM has"
3295 " no terminating zero\n"));
3304 /* Does not reference any types, nothing to be done. */
3309 struct lf_vftable
*vft
= (struct lf_vftable
*) data
;
3311 if (size
< offsetof (struct lf_vftable
, names
))
3313 einfo (_("%P: warning: truncated CodeView type record"
3318 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
3321 if (!remap_type (&vft
->base_vftable
, map
, type_num
, num_types
))
3329 struct lf_string_id
*str
= (struct lf_string_id
*) data
;
3332 if (size
< offsetof (struct lf_string_id
, string
))
3334 einfo (_("%P: warning: truncated CodeView type record"
3335 " LF_STRING_ID\n"));
3339 if (!remap_type (&str
->substring
, map
, type_num
, num_types
))
3342 string_len
= strnlen (str
->string
,
3343 size
- offsetof (struct lf_string_id
, string
));
3345 if (string_len
== size
- offsetof (struct lf_string_id
, string
))
3347 einfo (_("%P: warning: string for LF_STRING_ID has no"
3348 " terminating zero\n"));
3357 case LF_SUBSTR_LIST
:
3359 uint32_t num_entries
;
3360 struct lf_arglist
*ssl
= (struct lf_arglist
*) data
;
3362 if (size
< offsetof (struct lf_arglist
, args
))
3364 einfo (_("%P: warning: truncated CodeView type record"
3365 " LF_SUBSTR_LIST\n"));
3369 num_entries
= bfd_getl32 (&ssl
->num_entries
);
3371 if (size
< offsetof (struct lf_arglist
, args
)
3372 + (num_entries
* sizeof (uint32_t)))
3374 einfo (_("%P: warning: truncated CodeView type record"
3375 " LF_SUBSTR_LIST\n"));
3379 for (uint32_t i
= 0; i
< num_entries
; i
++)
3381 if (!remap_type (&ssl
->args
[i
], map
, type_num
, num_types
))
3392 uint16_t num_entries
;
3393 struct lf_build_info
*bi
= (struct lf_build_info
*) data
;
3395 if (size
< offsetof (struct lf_build_info
, strings
))
3397 einfo (_("%P: warning: truncated CodeView type record"
3398 " LF_BUILDINFO\n"));
3402 num_entries
= bfd_getl16 (&bi
->count
);
3404 if (size
< offsetof (struct lf_build_info
, strings
)
3405 + (num_entries
* sizeof (uint32_t)))
3407 einfo (_("%P: warning: truncated CodeView type record"
3408 " LF_BUILDINFO\n"));
3412 for (uint32_t i
= 0; i
< num_entries
; i
++)
3414 if (!remap_type (&bi
->strings
[i
], map
, type_num
, num_types
))
3425 struct lf_func_id
*func
= (struct lf_func_id
*) data
;
3428 if (size
< offsetof (struct lf_func_id
, name
))
3430 einfo (_("%P: warning: truncated CodeView type record"
3435 if (!remap_type (&func
->parent_scope
, map
, type_num
, num_types
))
3438 if (!remap_type (&func
->function_type
, map
, type_num
, num_types
))
3441 name_len
= strnlen (func
->name
,
3442 size
- offsetof (struct lf_func_id
, name
));
3444 if (name_len
== size
- offsetof (struct lf_func_id
, name
))
3446 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3447 " terminating zero\n"));
3458 struct lf_mfunc_id
*mfunc
= (struct lf_mfunc_id
*) data
;
3461 if (size
< offsetof (struct lf_mfunc_id
, name
))
3463 einfo (_("%P: warning: truncated CodeView type record"
3468 if (!remap_type (&mfunc
->parent_type
, map
, type_num
, num_types
))
3471 if (!remap_type (&mfunc
->function_type
, map
, type_num
, num_types
))
3474 name_len
= strnlen (mfunc
->name
,
3475 size
- offsetof (struct lf_mfunc_id
, name
));
3477 if (name_len
== size
- offsetof (struct lf_mfunc_id
, name
))
3479 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3480 " terminating zero\n"));
3489 case LF_UDT_SRC_LINE
:
3490 return handle_udt_src_line (data
, size
, map
, type_num
, num_types
,
3491 ids
, mod_num
, strings
);
3494 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type
);
3498 hash
= iterative_hash (data
, size
, 0);
3500 t
= ipi
? ids
: types
;
3502 slot
= htab_find_slot_with_hash (t
->hashmap
, data
, hash
, INSERT
);
3506 if (!*slot
) /* new entry */
3508 struct type_entry
*e
;
3510 *slot
= xmalloc (offsetof (struct type_entry
, data
) + size
);
3512 e
= (struct type_entry
*) *slot
;
3515 e
->index
= t
->num_types
;
3518 e
->cv_hash
= cv_hash
;
3520 e
->cv_hash
= crc32 (data
, size
);
3522 e
->has_udt_src_line
= false;
3524 memcpy (e
->data
, data
, size
);
3537 else /* duplicate */
3539 map
[type_num
] = (struct type_entry
*) *slot
;
3545 /* Parse the .debug$T section of a module, and pass any type definitions
3546 found to handle_type. */
3548 handle_debugt_section (asection
*s
, bfd
*mod
, struct types
*types
,
3549 struct types
*ids
, uint16_t mod_num
,
3550 struct string_table
*strings
,
3551 struct type_entry
***map
, uint32_t *num_types
)
3553 bfd_byte
*data
= NULL
;
3557 if (!bfd_get_full_section_contents (mod
, s
, &data
))
3563 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
3569 off
= sizeof (uint32_t);
3571 while (off
+ sizeof (uint16_t) <= s
->size
)
3575 size
= bfd_getl16 (data
+ off
);
3576 off
+= sizeof (uint16_t);
3578 if (size
+ off
> s
->size
|| size
<= sizeof (uint16_t))
3581 bfd_set_error (bfd_error_bad_value
);
3589 if (*num_types
== 0)
3595 *map
= xcalloc (*num_types
, sizeof (struct type_entry
*));
3597 off
= sizeof (uint32_t);
3600 while (off
+ sizeof (uint16_t) <= s
->size
)
3604 size
= bfd_getl16 (data
+ off
);
3606 if (!handle_type (data
+ off
, *map
, type_num
, *num_types
, types
, ids
,
3611 bfd_set_error (bfd_error_bad_value
);
3615 off
+= sizeof (uint16_t) + size
;
3624 /* Return the CodeView constant for the selected architecture. */
3626 target_processor (bfd
*abfd
)
3628 switch (abfd
->arch_info
->arch
)
3631 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
3634 return CV_CFL_80386
;
3636 case bfd_arch_aarch64
:
3637 return CV_CFL_ARM64
;
3644 /* Create the symbols that go in "* Linker *", the dummy module created
3645 for the linker itself. */
3647 create_linker_symbols (bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
3648 const char *pdb_name
)
3651 struct objname
*name
;
3652 struct compile3
*comp
;
3653 struct envblock
*env
;
3654 size_t padding1
, padding2
, env_size
;
3655 char *cwdval
, *exeval
, *pdbval
;
3657 /* extra NUL for padding */
3658 static const char linker_fn
[] = "* Linker *\0";
3659 static const char linker_name
[] = "GNU LD " VERSION
;
3661 static const char cwd
[] = "cwd";
3662 static const char exe
[] = "exe";
3663 static const char pdb
[] = "pdb";
3665 cwdval
= getcwd (NULL
, 0);
3668 einfo (_("%P: warning: unable to get working directory\n"));
3672 exeval
= lrealpath (program_name
);
3676 einfo (_("%P: warning: unable to get program name\n"));
3681 pdbval
= lrealpath (pdb_name
);
3685 einfo (_("%P: warning: unable to get full path to PDB\n"));
3691 *sym_byte_size
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3692 *sym_byte_size
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
);
3694 if (*sym_byte_size
% 4)
3695 padding1
= 4 - (*sym_byte_size
% 4);
3699 *sym_byte_size
+= padding1
;
3701 env_size
= offsetof (struct envblock
, strings
);
3702 env_size
+= sizeof (cwd
);
3703 env_size
+= strlen (cwdval
) + 1;
3704 env_size
+= sizeof (exe
);
3705 env_size
+= strlen (exeval
) + 1;
3706 env_size
+= sizeof (pdb
);
3707 env_size
+= strlen (pdbval
) + 1;
3710 padding2
= 4 - (env_size
% 4);
3714 env_size
+= padding2
;
3716 *sym_byte_size
+= env_size
;
3718 *syms
= xmalloc (*sym_byte_size
);
3721 /* Write S_OBJNAME */
3723 name
= (struct objname
*) ptr
;
3724 bfd_putl16 (offsetof (struct objname
, name
)
3725 + sizeof (linker_fn
) - sizeof (uint16_t), &name
->size
);
3726 bfd_putl16 (S_OBJNAME
, &name
->kind
);
3727 bfd_putl32 (0, &name
->signature
);
3728 memcpy (name
->name
, linker_fn
, sizeof (linker_fn
));
3730 ptr
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3732 /* Write S_COMPILE3 */
3734 comp
= (struct compile3
*) ptr
;
3736 bfd_putl16 (offsetof (struct compile3
, compiler
) + sizeof (linker_name
)
3737 + padding1
- sizeof (uint16_t), &comp
->size
);
3738 bfd_putl16 (S_COMPILE3
, &comp
->kind
);
3739 bfd_putl32 (CV_CFL_LINK
, &comp
->flags
);
3740 bfd_putl16 (target_processor (abfd
), &comp
->machine
);
3741 bfd_putl16 (0, &comp
->frontend_major
);
3742 bfd_putl16 (0, &comp
->frontend_minor
);
3743 bfd_putl16 (0, &comp
->frontend_build
);
3744 bfd_putl16 (0, &comp
->frontend_qfe
);
3745 bfd_putl16 (0, &comp
->backend_major
);
3746 bfd_putl16 (0, &comp
->backend_minor
);
3747 bfd_putl16 (0, &comp
->backend_build
);
3748 bfd_putl16 (0, &comp
->backend_qfe
);
3749 memcpy (comp
->compiler
, linker_name
, sizeof (linker_name
));
3751 memset (comp
->compiler
+ sizeof (linker_name
), 0, padding1
);
3753 ptr
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
) + padding1
;
3755 /* Write S_ENVBLOCK */
3757 env
= (struct envblock
*) ptr
;
3759 bfd_putl16 (env_size
- sizeof (uint16_t), &env
->size
);
3760 bfd_putl16 (S_ENVBLOCK
, &env
->kind
);
3763 ptr
+= offsetof (struct envblock
, strings
);
3765 memcpy (ptr
, cwd
, sizeof (cwd
));
3766 ptr
+= sizeof (cwd
);
3767 memcpy (ptr
, cwdval
, strlen (cwdval
) + 1);
3768 ptr
+= strlen (cwdval
) + 1;
3770 memcpy (ptr
, exe
, sizeof (exe
));
3771 ptr
+= sizeof (exe
);
3772 memcpy (ptr
, exeval
, strlen (exeval
) + 1);
3773 ptr
+= strlen (exeval
) + 1;
3775 memcpy (ptr
, pdb
, sizeof (pdb
));
3776 ptr
+= sizeof (pdb
);
3777 memcpy (ptr
, pdbval
, strlen (pdbval
) + 1);
3778 ptr
+= strlen (pdbval
) + 1;
3780 /* Microsoft's LINK also includes "cmd", the command-line options passed
3781 to the linker, but unfortunately we don't have access to argc and argv
3784 memset (ptr
, 0, padding2
);
3793 /* Populate the module stream, which consists of the transformed .debug$S
3794 data for each object file. */
3796 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
3797 struct string_table
*strings
,
3798 uint32_t *c13_info_size
,
3799 struct mod_source_files
*mod_source
,
3800 bfd
*abfd
, struct types
*types
,
3801 struct types
*ids
, uint16_t mod_num
,
3802 bfd
*sym_rec_stream
, struct globals
*glob
,
3803 const char *pdb_name
)
3805 uint8_t int_buf
[sizeof (uint32_t)];
3806 uint8_t *c13_info
= NULL
;
3807 uint8_t *syms
= NULL
;
3812 if (!strcmp (bfd_get_filename (mod
), "dll stuff"))
3814 if (!create_linker_symbols (mod
, &syms
, sym_byte_size
, pdb_name
))
3819 struct type_entry
**map
= NULL
;
3820 uint32_t num_types
= 0;
3822 /* Process .debug$T section. */
3824 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3826 if (!strcmp (s
->name
, ".debug$T") && s
->size
>= sizeof (uint32_t))
3828 if (!handle_debugt_section (s
, mod
, types
, ids
, mod_num
, strings
,
3831 free (mod_source
->files
);
3839 /* Process .debug$S section(s). */
3841 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3843 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
3845 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
3846 c13_info_size
, mod_source
, abfd
,
3847 &syms
, sym_byte_size
, map
, num_types
,
3848 sym_rec_stream
, glob
, mod_num
))
3852 free (mod_source
->files
);
3862 /* Write the signature. */
3864 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
3866 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3875 if (bfd_bwrite (syms
, *sym_byte_size
, stream
) != *sym_byte_size
)
3887 if (bfd_bwrite (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
3896 /* Write the global refs size. */
3898 bfd_putl32 (0, int_buf
);
3900 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3906 /* Create the module info substream within the DBI. */
3908 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
3909 uint32_t *size
, struct string_table
*strings
,
3910 struct source_files_info
*source
,
3911 struct types
*types
, struct types
*ids
,
3912 bfd
*sym_rec_stream
, struct globals
*glob
,
3913 const char *pdb_name
)
3916 unsigned int mod_num
;
3918 static const char linker_fn
[] = "* Linker *";
3922 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
3925 size_t len
= sizeof (struct module_info
);
3927 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
3929 len
+= sizeof (linker_fn
); /* Object name. */
3930 len
++; /* Empty module name. */
3932 else if (in
->my_archive
)
3934 char *name
= lrealpath (bfd_get_filename (in
));
3936 len
+= strlen (name
) + 1; /* Object name. */
3940 name
= lrealpath (bfd_get_filename (in
->my_archive
));
3942 len
+= strlen (name
) + 1; /* Archive name. */
3948 char *name
= lrealpath (bfd_get_filename (in
));
3949 size_t name_len
= strlen (name
) + 1;
3951 len
+= name_len
; /* Object name. */
3952 len
+= name_len
; /* And again as the archive name. */
3958 len
+= 4 - (len
% 4);
3962 source
->mod_count
++;
3965 *data
= xmalloc (*size
);
3969 source
->mods
= xmalloc (source
->mod_count
3970 * sizeof (struct mod_source_files
));
3971 memset (source
->mods
, 0,
3972 source
->mod_count
* sizeof (struct mod_source_files
));
3976 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
3979 struct module_info
*mod
= (struct module_info
*) ptr
;
3980 uint16_t stream_num
;
3982 uint32_t sym_byte_size
, c13_info_size
;
3983 uint8_t *start
= ptr
;
3985 stream
= add_stream (pdb
, NULL
, &stream_num
);
3989 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
3991 free (source
->mods
[i
].files
);
3994 free (source
->mods
);
3999 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
4000 strings
, &c13_info_size
,
4001 &source
->mods
[mod_num
], abfd
,
4002 types
, ids
, mod_num
,
4003 sym_rec_stream
, glob
, pdb_name
))
4005 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
4007 free (source
->mods
[i
].files
);
4010 free (source
->mods
);
4015 bfd_putl32 (0, &mod
->unused1
);
4017 /* These are dummy values - MSVC copies the first section contribution
4018 entry here, but doesn't seem to use it for anything. */
4019 bfd_putl16 (0xffff, &mod
->sc
.section
);
4020 bfd_putl16 (0, &mod
->sc
.padding1
);
4021 bfd_putl32 (0, &mod
->sc
.offset
);
4022 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
4023 bfd_putl32 (0, &mod
->sc
.characteristics
);
4024 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
4025 bfd_putl16 (0, &mod
->sc
.padding2
);
4026 bfd_putl32 (0, &mod
->sc
.data_crc
);
4027 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
4029 bfd_putl16 (0, &mod
->flags
);
4030 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
4031 bfd_putl32 (sizeof (uint32_t) + sym_byte_size
, &mod
->sym_byte_size
);
4032 bfd_putl32 (0, &mod
->c11_byte_size
);
4033 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
4034 bfd_putl16 (0, &mod
->source_file_count
);
4035 bfd_putl16 (0, &mod
->padding
);
4036 bfd_putl32 (0, &mod
->unused2
);
4037 bfd_putl32 (0, &mod
->source_file_name_index
);
4038 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
4040 ptr
+= sizeof (struct module_info
);
4042 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
4045 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
4046 ptr
+= sizeof (linker_fn
);
4048 /* Empty module name. */
4052 else if (in
->my_archive
)
4054 char *name
= lrealpath (bfd_get_filename (in
));
4055 size_t name_len
= strlen (name
) + 1;
4058 memcpy (ptr
, name
, name_len
);
4063 name
= lrealpath (bfd_get_filename (in
->my_archive
));
4064 name_len
= strlen (name
) + 1;
4067 memcpy (ptr
, name
, name_len
);
4074 char *name
= lrealpath (bfd_get_filename (in
));
4075 size_t name_len
= strlen (name
) + 1;
4078 memcpy (ptr
, name
, name_len
);
4081 /* Object name again as archive name. */
4082 memcpy (ptr
, name
, name_len
);
4088 /* Pad to next four-byte boundary. */
4090 if ((ptr
- start
) % 4)
4092 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
4093 ptr
+= 4 - ((ptr
- start
) % 4);
4102 /* Return the index of a given output section. */
4104 find_section_number (bfd
*abfd
, asection
*sect
)
4108 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
4113 /* Empty sections aren't output. */
4121 /* Create the substream which maps addresses in the image file to locations
4122 in the original object files. */
4124 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
4126 unsigned int num_sc
= 0;
4127 struct section_contribution
*sc
;
4132 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4135 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4137 if (s
->size
== 0 || discarded_section (s
))
4144 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
4145 *data
= xmalloc (*size
);
4147 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
4149 /* Read characteristics of outputted sections. */
4151 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
4153 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
4154 offset
+= offsetof (struct external_scnhdr
, s_flags
);
4156 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
4158 bfd_seek (abfd
, offset
, SEEK_SET
);
4160 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
4161 abfd
) != sizeof (uint32_t))
4168 offset
+= sizeof (struct external_scnhdr
);
4172 (struct section_contribution
*) ((uint8_t *) *data
+ sizeof (uint32_t));
4175 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4178 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4182 if (s
->size
== 0 || discarded_section (s
))
4185 sect_num
= find_section_number (abfd
, s
->output_section
);
4187 memcpy (&sc
->characteristics
,
4188 sect_flags
+ ((sect_num
- 1) * sizeof (uint32_t)),
4191 bfd_putl16 (sect_num
, &sc
->section
);
4192 bfd_putl16 (0, &sc
->padding1
);
4193 bfd_putl32 (s
->output_offset
, &sc
->offset
);
4194 bfd_putl32 (s
->size
, &sc
->size
);
4195 bfd_putl16 (mod_index
, &sc
->module_index
);
4196 bfd_putl16 (0, &sc
->padding2
);
4197 bfd_putl32 (0, &sc
->data_crc
);
4198 bfd_putl32 (0, &sc
->reloc_crc
);
4211 /* The source info substream lives within the DBI stream, and lists the
4212 source files for each object file (i.e. it's derived from the
4213 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4214 superfluous, as the filenames are also available in the C13 parts of
4215 the module streams, but MSVC relies on it to work properly. */
4217 create_source_info_substream (void **data
, uint32_t *size
,
4218 struct source_files_info
*source
)
4220 uint16_t dedupe_source_files_count
= 0;
4221 uint16_t source_files_count
= 0;
4222 uint32_t strings_len
= 0;
4225 /* Loop through the source files, marking unique filenames. The pointers
4226 here are for entries in the main string table, and so have already
4227 been deduplicated. */
4229 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4231 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4233 if (source
->mods
[i
].files
[j
])
4235 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
4237 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
4238 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
4239 dedupe_source_files_count
++;
4242 source_files_count
++;
4247 *size
= sizeof (uint16_t) + sizeof (uint16_t);
4248 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
4249 *size
+= sizeof (uint32_t) * source_files_count
;
4250 *size
+= strings_len
;
4252 *data
= xmalloc (*size
);
4254 ptr
= (uint8_t *) *data
;
4256 /* Write header (module count and source file count). */
4258 bfd_putl16 (source
->mod_count
, ptr
);
4259 ptr
+= sizeof (uint16_t);
4261 bfd_putl16 (dedupe_source_files_count
, ptr
);
4262 ptr
+= sizeof (uint16_t);
4264 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4265 present, but does not appear to be useful". */
4267 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4269 bfd_putl16 (i
, ptr
);
4270 ptr
+= sizeof (uint16_t);
4273 /* Write source file count for each module. */
4275 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4277 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
4278 ptr
+= sizeof (uint16_t);
4281 /* For each module, write the offsets within the string table
4282 for each source file. */
4284 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4286 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4288 if (source
->mods
[i
].files
[j
])
4290 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
4291 ptr
+= sizeof (uint32_t);
4296 /* Write the string table. We set source_file_offset to a dummy value for
4297 each entry we write, so we don't write duplicate filenames. */
4299 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4301 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4303 if (source
->mods
[i
].files
[j
]
4304 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
4306 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
4307 source
->mods
[i
].files
[j
]->len
);
4308 ptr
+= source
->mods
[i
].files
[j
]->len
;
4313 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
4319 /* Used as parameter to qsort, to sort globals by hash. */
4321 global_compare_hash (const void *s1
, const void *s2
)
4323 const struct global
*g1
= *(const struct global
**) s1
;
4324 const struct global
*g2
= *(const struct global
**) s2
;
4326 if (g1
->hash
< g2
->hash
)
4328 if (g1
->hash
> g2
->hash
)
4334 /* Create the globals stream, which contains the unmangled symbol names. */
4336 create_globals_stream (bfd
*pdb
, struct globals
*glob
, uint16_t *stream_num
)
4339 struct globals_hash_header h
;
4340 uint32_t buckets_size
, filled_buckets
= 0;
4341 struct global
**sorted
= NULL
;
4343 struct global
*buckets
[NUM_GLOBALS_HASH_BUCKETS
];
4344 char int_buf
[sizeof (uint32_t)];
4346 stream
= add_stream (pdb
, NULL
, stream_num
);
4350 memset (buckets
, 0, sizeof (buckets
));
4352 if (glob
->num_entries
> 0)
4356 /* Create an array of pointers, sorted by hash value. */
4358 sorted
= xmalloc (sizeof (struct global
*) * glob
->num_entries
);
4361 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4367 qsort (sorted
, glob
->num_entries
, sizeof (struct global
*),
4368 global_compare_hash
);
4370 /* Populate the buckets. */
4372 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4374 if (!buckets
[sorted
[i
]->hash
])
4376 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4380 sorted
[i
]->index
= i
;
4384 buckets_size
= NUM_GLOBALS_HASH_BUCKETS
/ 8;
4385 buckets_size
+= sizeof (uint32_t);
4386 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4388 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &h
.signature
);
4389 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &h
.version
);
4390 bfd_putl32 (glob
->num_entries
* sizeof (struct hash_record
),
4392 bfd_putl32 (buckets_size
, &h
.buckets_size
);
4394 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
4397 /* Write hash entries, sorted by hash. */
4399 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4401 struct hash_record hr
;
4403 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4404 bfd_putl32 (sorted
[i
]->refcount
, &hr
.reference
);
4406 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4410 /* Write the bitmap for filled and unfilled buckets. */
4412 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
+= 8)
4416 for (unsigned int j
= 0; j
< 8; j
++)
4422 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
4426 /* Add a 4-byte gap. */
4428 bfd_putl32 (0, int_buf
);
4430 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4433 /* Write the bucket offsets. */
4435 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
++)
4439 /* 0xc is size of internal hash_record structure in
4440 Microsoft's parser. */
4441 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
4443 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4457 /* Hash an entry in the globals list. */
4459 hash_global_entry (const void *p
)
4461 const struct global
*g
= (const struct global
*) p
;
4462 uint16_t len
= bfd_getl16 (g
->data
);
4464 return iterative_hash (g
->data
, len
, 0);
4467 /* Compare an entry in the globals list with a symbol. */
4469 eq_global_entry (const void *a
, const void *b
)
4471 const struct global
*g
= (const struct global
*) a
;
4472 uint16_t len1
, len2
;
4474 len1
= bfd_getl16 (g
->data
) + sizeof (uint16_t);
4475 len2
= bfd_getl16 (b
) + sizeof (uint16_t);
4480 return !memcmp (g
->data
, b
, len1
);
4483 /* Stream 4 is the debug information (DBI) stream. */
4485 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
4486 uint16_t section_header_stream_num
,
4487 uint16_t sym_rec_stream_num
,
4488 uint16_t publics_stream_num
,
4489 struct string_table
*strings
,
4490 struct types
*types
,
4492 bfd
*sym_rec_stream
, const char *pdb_name
)
4494 struct pdb_dbi_stream_header h
;
4495 struct optional_dbg_header opt
;
4496 void *mod_info
, *sc
, *source_info
;
4497 uint32_t mod_info_size
, sc_size
, source_info_size
;
4498 struct source_files_info source
;
4499 struct globals glob
;
4500 uint16_t globals_stream_num
;
4502 source
.mod_count
= 0;
4505 glob
.num_entries
= 0;
4509 glob
.hashmap
= htab_create_alloc (0, hash_global_entry
,
4510 eq_global_entry
, free
, xcalloc
, free
);
4512 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
4513 strings
, &source
, types
, ids
,
4514 sym_rec_stream
, &glob
, pdb_name
))
4516 htab_delete (glob
.hashmap
);
4520 if (!create_globals_stream (pdb
, &glob
, &globals_stream_num
))
4522 htab_delete (glob
.hashmap
);
4524 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4526 free (source
.mods
[i
].files
);
4534 htab_delete (glob
.hashmap
);
4536 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
4538 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4540 free (source
.mods
[i
].files
);
4548 create_source_info_substream (&source_info
, &source_info_size
, &source
);
4550 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4552 free (source
.mods
[i
].files
);
4556 bfd_putl32 (0xffffffff, &h
.version_signature
);
4557 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
4558 bfd_putl32 (1, &h
.age
);
4559 bfd_putl16 (globals_stream_num
, &h
.global_stream_index
);
4560 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
4561 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
4562 bfd_putl16 (0, &h
.pdb_dll_version
);
4563 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
4564 bfd_putl16 (0, &h
.pdb_dll_rbld
);
4565 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
4566 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
4567 bfd_putl32 (0, &h
.section_map_size
);
4568 bfd_putl32 (source_info_size
, &h
.source_info_size
);
4569 bfd_putl32 (0, &h
.type_server_map_size
);
4570 bfd_putl32 (0, &h
.mfc_type_server_index
);
4571 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
4572 bfd_putl32 (0, &h
.ec_substream_size
);
4573 bfd_putl16 (0, &h
.flags
);
4574 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
4575 bfd_putl32 (0, &h
.padding
);
4577 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
4585 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
4595 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
4604 if (bfd_bwrite (source_info
, source_info_size
, stream
) != source_info_size
)
4612 bfd_putl16 (0xffff, &opt
.fpo_stream
);
4613 bfd_putl16 (0xffff, &opt
.exception_stream
);
4614 bfd_putl16 (0xffff, &opt
.fixup_stream
);
4615 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
4616 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
4617 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
4618 bfd_putl16 (0xffff, &opt
.token_map_stream
);
4619 bfd_putl16 (0xffff, &opt
.xdata_stream
);
4620 bfd_putl16 (0xffff, &opt
.pdata_stream
);
4621 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
4622 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
4624 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
4630 /* Used as parameter to qsort, to sort publics by hash. */
4632 public_compare_hash (const void *s1
, const void *s2
)
4634 const struct public *p1
= *(const struct public **) s1
;
4635 const struct public *p2
= *(const struct public **) s2
;
4637 if (p1
->hash
< p2
->hash
)
4639 if (p1
->hash
> p2
->hash
)
4645 /* Used as parameter to qsort, to sort publics by address. */
4647 public_compare_addr (const void *s1
, const void *s2
)
4649 const struct public *p1
= *(const struct public **) s1
;
4650 const struct public *p2
= *(const struct public **) s2
;
4652 if (p1
->section
< p2
->section
)
4654 if (p1
->section
> p2
->section
)
4657 if (p1
->address
< p2
->address
)
4659 if (p1
->address
> p2
->address
)
4665 /* The publics stream is a hash map of S_PUB32 records, which are stored
4666 in the symbol record stream. Each S_PUB32 entry represents a symbol
4667 from the point of view of the linker: a section index, an offset within
4668 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4669 which are the same thing but generated by the compiler. */
4671 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
4673 struct publics_header header
;
4674 struct globals_hash_header hash_header
;
4675 const unsigned int num_buckets
= 4096;
4676 unsigned int num_entries
= 0, filled_buckets
= 0;
4677 unsigned int buckets_size
, sym_hash_size
;
4678 char int_buf
[sizeof (uint32_t)];
4679 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
4680 struct public **buckets
;
4681 struct public **sorted
= NULL
;
4684 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
4685 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
4687 /* Loop through the global symbols in our input files, and write S_PUB32
4688 records in the symbol record stream for those that make it into the
4690 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4693 if (!in
->outsymbols
)
4696 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
4698 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
4700 if (sym
->flags
& BSF_GLOBAL
)
4703 uint16_t record_length
;
4704 const char *name
= sym
->name
;
4705 size_t name_len
= strlen (name
);
4706 struct public *p
= xmalloc (sizeof (struct public));
4707 unsigned int padding
= 0;
4712 find_section_number (abfd
, sym
->section
->output_section
);
4718 p
->offset
= bfd_tell (sym_rec_stream
);
4719 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
4720 p
->section
= section
;
4721 p
->address
= sym
->section
->output_offset
+ sym
->value
;
4723 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
4725 if (record_length
% 4)
4726 padding
= 4 - (record_length
% 4);
4728 /* Assume that all global symbols in executable sections
4730 if (sym
->section
->flags
& SEC_CODE
)
4731 flags
= PUBSYM_FUNCTION
;
4733 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
4735 bfd_putl16 (S_PUB32
, &ps
.record_type
);
4736 bfd_putl32 (flags
, &ps
.flags
);
4737 bfd_putl32 (p
->address
, &ps
.offset
);
4738 bfd_putl16 (p
->section
, &ps
.section
);
4740 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
4741 sizeof (struct pubsym
))
4744 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
4748 for (unsigned int j
= 0; j
< padding
; j
++)
4752 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
4760 publics_tail
->next
= p
;
4769 if (num_entries
> 0)
4771 /* Create an array of pointers, sorted by hash value. */
4773 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
4775 struct public *p
= publics_head
;
4776 for (unsigned int i
= 0; i
< num_entries
; i
++)
4782 qsort (sorted
, num_entries
, sizeof (struct public *),
4783 public_compare_hash
);
4785 /* Populate the buckets. */
4787 for (unsigned int i
= 0; i
< num_entries
; i
++)
4789 if (!buckets
[sorted
[i
]->hash
])
4791 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4795 sorted
[i
]->index
= i
;
4799 buckets_size
= num_buckets
/ 8;
4800 buckets_size
+= sizeof (uint32_t);
4801 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4803 sym_hash_size
= sizeof (hash_header
);
4804 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
4805 sym_hash_size
+= buckets_size
;
4807 /* Output the publics header. */
4809 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
4810 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
4811 bfd_putl32 (0, &header
.num_thunks
);
4812 bfd_putl32 (0, &header
.thunks_size
);
4813 bfd_putl32 (0, &header
.thunk_table
);
4814 bfd_putl32 (0, &header
.thunk_table_offset
);
4815 bfd_putl32 (0, &header
.num_sects
);
4817 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
4820 /* Output the global hash header. */
4822 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
4823 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
4824 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
4825 &hash_header
.entries_size
);
4826 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
4828 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
4829 sizeof (hash_header
))
4832 /* Write the entries in hash order. */
4834 for (unsigned int i
= 0; i
< num_entries
; i
++)
4836 struct hash_record hr
;
4838 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4839 bfd_putl32 (1, &hr
.reference
);
4841 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4845 /* Write the bitmap for filled and unfilled buckets. */
4847 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
4851 for (unsigned int j
= 0; j
< 8; j
++)
4857 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
4861 /* Add a 4-byte gap. */
4863 bfd_putl32 (0, int_buf
);
4865 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4868 /* Write the bucket offsets. */
4870 for (unsigned int i
= 0; i
< num_buckets
; i
++)
4874 /* 0xc is size of internal hash_record structure in
4875 Microsoft's parser. */
4876 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
4878 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4884 /* Write the address map: offsets into the symbol record stream of
4885 S_PUB32 records, ordered by address. */
4887 if (num_entries
> 0)
4889 qsort (sorted
, num_entries
, sizeof (struct public *),
4890 public_compare_addr
);
4892 for (unsigned int i
= 0; i
< num_entries
; i
++)
4894 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
4896 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
4907 while (publics_head
)
4909 struct public *p
= publics_head
->next
;
4911 free (publics_head
);
4920 /* The section header stream contains a copy of the section headers
4921 from the PE file, in the same format. */
4923 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
4926 unsigned int section_count
;
4931 stream
= add_stream (pdb
, NULL
, num
);
4935 section_count
= abfd
->section_count
;
4937 /* Empty sections aren't output. */
4938 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
4940 if (sect
->size
== 0)
4944 if (section_count
== 0)
4947 /* Copy section table from output - it's already been written at this
4950 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
4952 bfd_seek (abfd
, scn_base
, SEEK_SET
);
4954 len
= section_count
* sizeof (struct external_scnhdr
);
4955 buf
= xmalloc (len
);
4957 if (bfd_bread (buf
, len
, abfd
) != len
)
4963 if (bfd_bwrite (buf
, len
, stream
) != len
)
4974 /* Populate the "/names" named stream, which contains the string table. */
4976 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
4978 char int_buf
[sizeof (uint32_t)];
4979 struct string_table_header h
;
4980 uint32_t num_strings
= 0, num_buckets
;
4981 struct string
**buckets
;
4983 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
4984 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
4986 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
4989 bfd_putl32 (strings
->strings_len
, int_buf
);
4991 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4996 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
4999 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5001 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
5004 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
5010 num_buckets
= num_strings
* 2;
5012 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
5013 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
5015 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5017 uint32_t bucket_num
= s
->hash
% num_buckets
;
5019 while (buckets
[bucket_num
])
5023 if (bucket_num
== num_buckets
)
5027 buckets
[bucket_num
] = s
;
5030 bfd_putl32 (num_buckets
, int_buf
);
5032 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5038 for (unsigned int i
= 0; i
< num_buckets
; i
++)
5041 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
5043 bfd_putl32 (0, int_buf
);
5045 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
5055 bfd_putl32 (num_strings
, int_buf
);
5057 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5063 /* Calculate the hash of a type_entry. */
5065 hash_type_entry (const void *p
)
5067 const struct type_entry
*e
= (const struct type_entry
*) p
;
5068 uint16_t size
= bfd_getl16 (e
->data
) + sizeof (uint16_t);
5070 return iterative_hash (e
->data
, size
, 0);
5073 /* Compare a type_entry with a type. */
5075 eq_type_entry (const void *a
, const void *b
)
5077 const struct type_entry
*e
= (const struct type_entry
*) a
;
5078 uint16_t size_a
= bfd_getl16 (e
->data
);
5079 uint16_t size_b
= bfd_getl16 (b
);
5081 if (size_a
!= size_b
)
5084 return memcmp (e
->data
+ sizeof (uint16_t),
5085 (const uint8_t *) b
+ sizeof (uint16_t), size_a
) == 0;
5088 /* Create a PDB debugging file for the PE image file abfd with the build ID
5089 guid, stored at pdb_name. */
5091 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
5095 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
5096 *publics_stream
, *tpi_stream
, *ipi_stream
;
5097 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
5098 struct string_table strings
;
5099 struct types types
, ids
;
5101 pdb
= bfd_openw (pdb_name
, "pdb");
5104 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5108 strings
.strings_head
= NULL
;
5109 strings
.strings_tail
= NULL
;
5110 strings
.strings_len
= 1;
5111 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
5112 eq_string_table_entry
, free
,
5115 bfd_set_format (pdb
, bfd_archive
);
5117 if (!create_old_directory_stream (pdb
))
5119 einfo (_("%P: warning: cannot create old directory stream "
5120 "in PDB file: %E\n"));
5124 info_stream
= add_stream (pdb
, NULL
, NULL
);
5128 einfo (_("%P: warning: cannot create info stream "
5129 "in PDB file: %E\n"));
5133 tpi_stream
= add_stream (pdb
, NULL
, NULL
);
5137 einfo (_("%P: warning: cannot create TPI stream "
5138 "in PDB file: %E\n"));
5142 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
5146 einfo (_("%P: warning: cannot create DBI stream "
5147 "in PDB file: %E\n"));
5151 ipi_stream
= add_stream (pdb
, NULL
, NULL
);
5155 einfo (_("%P: warning: cannot create IPI stream "
5156 "in PDB file: %E\n"));
5160 names_stream
= add_stream (pdb
, "/names", NULL
);
5164 einfo (_("%P: warning: cannot create /names stream "
5165 "in PDB file: %E\n"));
5169 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
5171 if (!sym_rec_stream
)
5173 einfo (_("%P: warning: cannot create symbol record stream "
5174 "in PDB file: %E\n"));
5178 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
5180 if (!publics_stream
)
5182 einfo (_("%P: warning: cannot create publics stream "
5183 "in PDB file: %E\n"));
5187 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
5189 einfo (_("%P: warning: cannot create section header stream "
5190 "in PDB file: %E\n"));
5194 types
.num_types
= 0;
5195 types
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5196 free
, xcalloc
, free
);
5197 types
.first
= types
.last
= NULL
;
5200 ids
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5201 free
, xcalloc
, free
);
5202 ids
.first
= ids
.last
= NULL
;
5204 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
5205 sym_rec_stream_num
, publics_stream_num
,
5206 &strings
, &types
, &ids
, sym_rec_stream
, pdb_name
))
5208 einfo (_("%P: warning: cannot populate DBI stream "
5209 "in PDB file: %E\n"));
5210 htab_delete (types
.hashmap
);
5211 htab_delete (ids
.hashmap
);
5215 if (!populate_type_stream (pdb
, tpi_stream
, &types
))
5217 einfo (_("%P: warning: cannot populate TPI stream "
5218 "in PDB file: %E\n"));
5219 htab_delete (types
.hashmap
);
5220 htab_delete (ids
.hashmap
);
5224 htab_delete (types
.hashmap
);
5226 if (!populate_type_stream (pdb
, ipi_stream
, &ids
))
5228 einfo (_("%P: warning: cannot populate IPI stream "
5229 "in PDB file: %E\n"));
5230 htab_delete (ids
.hashmap
);
5234 htab_delete (ids
.hashmap
);
5236 add_string ("", 0, &strings
);
5238 if (!populate_names_stream (names_stream
, &strings
))
5240 einfo (_("%P: warning: cannot populate names stream "
5241 "in PDB file: %E\n"));
5245 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
5247 einfo (_("%P: warning: cannot populate publics stream "
5248 "in PDB file: %E\n"));
5252 if (!populate_info_stream (pdb
, info_stream
, guid
))
5254 einfo (_("%P: warning: cannot populate info stream "
5255 "in PDB file: %E\n"));
5264 htab_delete (strings
.hashmap
);