1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022 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. */
26 #include "libiberty.h"
27 #include "coff/i386.h"
28 #include "coff/external.h"
29 #include "coff/internal.h"
49 uint32_t source_file_offset
;
56 struct string
*strings_head
;
57 struct string
*strings_tail
;
62 struct mod_source_files
65 struct string
**files
;
68 struct source_files_info
71 struct mod_source_files
*mods
;
76 struct type_entry
*next
;
86 struct type_entry
*first
;
87 struct type_entry
*last
;
90 static const uint32_t crc_table
[] =
92 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
93 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
94 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
95 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
96 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
97 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
98 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
99 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
100 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
101 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
102 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
103 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
104 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
105 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
106 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
107 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
108 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
109 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
110 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
111 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
112 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
113 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
114 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
115 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
116 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
117 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
118 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
119 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
120 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
121 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
122 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
123 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
124 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
125 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
126 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
127 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
128 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
129 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
130 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
131 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
132 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
133 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
134 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
137 /* Add a new stream to the PDB archive, and return its BFD. */
139 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
144 stream
= bfd_create (name
? name
: "", pdb
);
148 if (!bfd_make_writable (stream
))
154 if (!pdb
->archive_head
)
156 bfd_set_archive_head (pdb
, stream
);
161 bfd
*b
= pdb
->archive_head
;
165 while (b
->archive_next
)
171 b
->archive_next
= stream
;
180 /* Stream 0 ought to be a copy of the MSF directory from the last
181 time the PDB file was written. Because we don't do incremental
182 writes this isn't applicable to us, but we fill it with a dummy
183 value so as not to confuse radare. */
185 create_old_directory_stream (bfd
*pdb
)
188 char buf
[sizeof (uint32_t)];
190 stream
= add_stream (pdb
, NULL
, NULL
);
196 return bfd_bwrite (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
199 /* Calculate the hash of a given string. */
201 calc_hash (const char *data
, size_t len
)
208 hash
^= data
[1] << 8;
209 hash
^= data
[2] << 16;
210 hash
^= data
[3] << 24;
219 hash
^= data
[1] << 8;
229 hash
^= (hash
>> 11);
231 return hash
^ (hash
>> 16);
234 /* Stream 1 is the PDB info stream - see
235 https://llvm.org/docs/PDB/PdbStream.html. */
237 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
240 struct pdb_stream_70 h
;
241 uint32_t num_entries
, num_buckets
;
242 uint32_t names_length
, stream_num
;
243 char int_buf
[sizeof (uint32_t)];
251 struct hash_entry
**buckets
= NULL
;
255 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
256 bfd_putl32 (time (NULL
), &h
.signature
);
257 bfd_putl32 (1, &h
.age
);
259 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
260 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
261 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
262 memcpy (&h
.guid
[8], &guid
[8], 8);
264 if (bfd_bwrite (&h
, sizeof (h
), info_stream
) != sizeof (h
))
267 /* Write hash list of named streams. This is a "rollover" hash, i.e.
268 if a bucket is filled an entry gets placed in the next free
272 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
274 if (strcmp (b
->filename
, ""))
278 num_buckets
= num_entries
* 2;
285 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
286 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
288 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
290 if (strcmp (b
->filename
, ""))
292 size_t len
= strlen (b
->filename
);
293 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
294 uint32_t bucket_num
= hash
% num_buckets
;
296 while (buckets
[bucket_num
])
300 if (bucket_num
== num_buckets
)
304 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
306 buckets
[bucket_num
]->offset
= names_length
;
307 buckets
[bucket_num
]->value
= stream_num
;
309 names_length
+= len
+ 1;
316 /* Write the strings list - the hash keys are indexes into this. */
318 bfd_putl32 (names_length
, int_buf
);
320 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
324 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
326 if (!strcmp (b
->filename
, ""))
329 size_t len
= strlen (b
->filename
) + 1;
331 if (bfd_bwrite (b
->filename
, len
, info_stream
) != len
)
335 /* Write the number of entries and buckets. */
337 bfd_putl32 (num_entries
, int_buf
);
339 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
343 bfd_putl32 (num_buckets
, int_buf
);
345 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
349 /* Write the present bitmap. */
351 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
353 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
357 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
361 for (unsigned int j
= 0; j
< 32; j
++)
363 if (i
+ j
>= num_buckets
)
370 bfd_putl32 (v
, int_buf
);
372 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
377 /* Write the (empty) deleted bitmap. */
379 bfd_putl32 (0, int_buf
);
381 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
385 /* Write the buckets. */
387 for (unsigned int i
= 0; i
< num_buckets
; i
++)
391 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
393 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
397 bfd_putl32 (buckets
[i
]->value
, int_buf
);
399 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
405 bfd_putl32 (0, int_buf
);
407 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
411 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
413 if (bfd_bwrite (int_buf
, sizeof (uint32_t), info_stream
) !=
420 for (unsigned int i
= 0; i
< num_buckets
; i
++)
431 /* Calculate the CRC32 used for type hashes. */
433 crc32 (const uint8_t *data
, size_t len
)
439 crc
= (crc
>> 8) ^ crc_table
[(crc
& 0xff) ^ *data
];
448 /* Stream 2 is the type information (TPI) stream, and stream 4 is
449 the ID information (IPI) stream. They differ only in which records
450 go in which stream. */
452 populate_type_stream (bfd
*pdb
, bfd
*stream
, struct types
*types
)
454 struct pdb_tpi_stream_header h
;
455 struct type_entry
*e
;
456 uint32_t len
= 0, index_offset_len
, off
;
457 struct bfd
*hash_stream
= NULL
;
458 uint16_t hash_stream_index
;
460 static const uint32_t index_skip
= 0x2000;
464 index_offset_len
= 0;
468 uint32_t old_len
= len
;
470 len
+= sizeof (uint16_t) + bfd_getl16 (e
->data
);
472 if (old_len
== 0 || old_len
/ index_skip
!= len
/ index_skip
)
473 index_offset_len
+= sizeof (uint32_t) * 2;
478 /* Each type stream also has a stream which holds the hash value for each
479 type, along with a skip list to speed up searching. */
481 hash_stream
= add_stream (pdb
, "", &hash_stream_index
);
486 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
487 bfd_putl32 (sizeof (h
), &h
.header_size
);
488 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
489 bfd_putl32 (TPI_FIRST_INDEX
+ types
->num_types
, &h
.type_index_end
);
490 bfd_putl32 (len
, &h
.type_record_bytes
);
491 bfd_putl16 (hash_stream_index
, &h
.hash_stream_index
);
492 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
493 bfd_putl32 (sizeof (uint32_t), &h
.hash_key_size
);
494 bfd_putl32 (NUM_TPI_HASH_BUCKETS
, &h
.num_hash_buckets
);
495 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
496 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
497 &h
.hash_value_buffer_length
);
498 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
499 &h
.index_offset_buffer_offset
);
500 bfd_putl32 (index_offset_len
, &h
.index_offset_buffer_length
);
501 bfd_putl32 ((types
->num_types
* sizeof (uint32_t)) + index_offset_len
,
502 &h
.hash_adj_buffer_offset
);
503 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
505 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
508 /* Write the type definitions into the main stream, and the hashes
509 into the hash stream. The hashes have already been calculated
516 uint8_t buf
[sizeof (uint32_t)];
519 size
= bfd_getl16 (e
->data
);
521 if (bfd_bwrite (e
->data
, size
+ sizeof (uint16_t), stream
)
522 != size
+ sizeof (uint16_t))
525 bfd_putl32 (e
->cv_hash
% NUM_TPI_HASH_BUCKETS
, buf
);
527 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
528 != sizeof (uint32_t))
534 /* Write the index offsets, i.e. the skip list, into the hash stream. We
535 copy MSVC here by writing a new entry for every 8192 bytes. */
542 uint32_t old_off
= off
;
543 uint16_t size
= bfd_getl16 (e
->data
);
545 off
+= size
+ sizeof (uint16_t);
547 if (old_off
== 0 || old_off
/ index_skip
!= len
/ index_skip
)
549 uint8_t buf
[sizeof (uint32_t)];
551 bfd_putl32 (TPI_FIRST_INDEX
+ e
->index
, buf
);
553 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
554 != sizeof (uint32_t))
557 bfd_putl32 (old_off
, buf
);
559 if (bfd_bwrite (buf
, sizeof (uint32_t), hash_stream
)
560 != sizeof (uint32_t))
570 /* Return the PE architecture number for the image. */
572 get_arch_number (bfd
*abfd
)
574 if (abfd
->arch_info
->arch
!= bfd_arch_i386
)
577 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
578 return IMAGE_FILE_MACHINE_AMD64
;
580 return IMAGE_FILE_MACHINE_I386
;
583 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
584 section, and copy it to the module's symbol stream. */
586 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
587 struct string_table
*strings
, uint8_t *out
,
588 struct mod_source_files
*mod_source
)
590 uint8_t *orig_data
= data
;
591 uint32_t orig_size
= size
;
592 uint16_t num_files
= 0;
593 struct string
**strptr
;
595 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
596 out
+= sizeof (uint32_t);
598 bfd_putl32 (size
, out
);
599 out
+= sizeof (uint32_t);
601 /* Calculate the number of files, and check for any overflows. */
605 struct file_checksum
*fc
= (struct file_checksum
*) data
;
609 if (size
< sizeof (struct file_checksum
))
611 bfd_set_error (bfd_error_bad_value
);
615 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
619 bfd_set_error (bfd_error_bad_value
);
626 if (len
% sizeof (uint32_t))
627 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
633 bfd_set_error (bfd_error_bad_value
);
643 /* Add the files to mod_source, so that they'll appear in the source
649 uint16_t new_count
= num_files
+ mod_source
->files_count
;
651 mod_source
->files
= xrealloc (mod_source
->files
,
652 sizeof (struct string
*) * new_count
);
654 strptr
= mod_source
->files
+ mod_source
->files_count
;
656 mod_source
->files_count
+= num_files
;
659 /* Actually copy the data. */
666 struct file_checksum
*fc
= (struct file_checksum
*) data
;
670 struct string
*str
= NULL
;
672 string_off
= bfd_getl32 (&fc
->file_id
);
673 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
675 if (len
% sizeof (uint32_t))
676 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
680 /* Remap the "file ID", i.e. the offset in the module's string table,
681 so it points to the right place in the main string table. */
685 char *fn
= string_table
+ string_off
;
686 size_t fn_len
= strlen (fn
);
687 uint32_t hash
= calc_hash (fn
, fn_len
);
690 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
694 str
= (struct string
*) *slot
;
700 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
702 memcpy (out
, data
, len
+ padding
);
704 data
+= len
+ padding
;
705 size
-= len
+ padding
;
706 out
+= len
+ padding
;
712 /* Add a string to the strings table, if it's not already there. */
714 add_string (char *str
, size_t len
, struct string_table
*strings
)
716 uint32_t hash
= calc_hash (str
, len
);
719 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
725 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
727 s
= (struct string
*) *slot
;
731 s
->offset
= strings
->strings_len
;
732 s
->source_file_offset
= 0xffffffff;
734 memcpy (s
->s
, str
, len
);
736 if (strings
->strings_tail
)
737 strings
->strings_tail
->next
= s
;
739 strings
->strings_head
= s
;
741 strings
->strings_tail
= s
;
743 strings
->strings_len
+= len
+ 1;
747 /* Return the hash of an entry in the string table. */
749 hash_string_table_entry (const void *p
)
751 const struct string
*s
= (const struct string
*) p
;
756 /* Compare an entry in the string table with a string. */
758 eq_string_table_entry (const void *a
, const void *b
)
760 const struct string
*s1
= (const struct string
*) a
;
761 const char *s2
= (const char *) b
;
762 size_t s2_len
= strlen (s2
);
764 if (s2_len
!= s1
->len
)
767 return memcmp (s1
->s
, s2
, s2_len
) == 0;
770 /* Parse the string table within the .debug$S section. */
772 parse_string_table (bfd_byte
*data
, size_t size
,
773 struct string_table
*strings
)
777 size_t len
= strnlen ((char *) data
, size
);
779 add_string ((char *) data
, len
, strings
);
790 /* Return the size of an extended value parameter, as used in
793 extended_value_len (uint16_t type
)
816 /* Parse the .debug$S section within an object file. */
818 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
819 uint8_t **dataptr
, uint32_t *sizeptr
,
820 struct mod_source_files
*mod_source
,
823 bfd_byte
*data
= NULL
;
825 uint32_t c13_size
= 0;
826 char *string_table
= NULL
;
827 uint8_t *buf
, *bufptr
;
829 if (!bfd_get_full_section_contents (mod
, s
, &data
))
835 /* Resolve relocations. Addresses are stored within the .debug$S section as
836 a .secidx, .secrel32 pair. */
838 if (s
->flags
& SEC_RELOC
)
840 struct internal_reloc
*relocs
;
841 struct internal_syment
*symbols
;
843 unsigned int syment_count
;
845 struct external_syment
*ext
;
847 syment_count
= obj_raw_syment_count (mod
);
850 _bfd_coff_read_internal_relocs (mod
, s
, false, NULL
, true, NULL
);
852 symbols
= xmalloc (sizeof (struct internal_syment
) * syment_count
);
853 sectlist
= xmalloc (sizeof (asection
*) * syment_count
);
855 ext
= (struct external_syment
*) (coff_data (mod
)->external_syms
);
857 for (unsigned int i
= 0; i
< syment_count
; i
++)
859 bfd_coff_swap_sym_in (mod
, &ext
[i
], &symbols
[i
]);
864 for (asection
*sect
= mod
->sections
; sect
; sect
= sect
->next
)
866 for (unsigned int i
= 0; i
< syment_count
; i
++)
868 if (symbols
[i
].n_scnum
== sect_num
)
875 if (!bfd_coff_relocate_section (abfd
, coff_data (abfd
)->link_info
, mod
,
876 s
, data
, relocs
, symbols
, sectlist
))
888 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
894 off
= sizeof (uint32_t);
898 while (off
+ sizeof (uint32_t) <= s
->size
)
902 type
= bfd_getl32 (data
+ off
);
904 off
+= sizeof (uint32_t);
906 if (off
+ sizeof (uint32_t) > s
->size
)
909 bfd_set_error (bfd_error_bad_value
);
913 size
= bfd_getl32 (data
+ off
);
915 off
+= sizeof (uint32_t);
917 if (off
+ size
> s
->size
)
920 bfd_set_error (bfd_error_bad_value
);
926 case DEBUG_S_FILECHKSMS
:
927 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
929 if (c13_size
% sizeof (uint32_t))
930 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
934 case DEBUG_S_STRINGTABLE
:
935 parse_string_table (data
+ off
, size
, strings
);
937 string_table
= (char *) data
+ off
;
945 if (size
< sizeof (uint32_t) + sizeof (uint16_t))
948 bfd_set_error (bfd_error_bad_value
);
952 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
954 /* Skip GC'd symbols. */
957 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
959 if (c13_size
% sizeof (uint32_t))
961 sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
970 if (off
% sizeof (uint32_t))
971 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
982 buf
= xmalloc (c13_size
);
985 off
= sizeof (uint32_t);
987 while (off
+ sizeof (uint32_t) <= s
->size
)
991 type
= bfd_getl32 (data
+ off
);
992 off
+= sizeof (uint32_t);
994 size
= bfd_getl32 (data
+ off
);
995 off
+= sizeof (uint32_t);
999 case DEBUG_S_FILECHKSMS
:
1000 if (!copy_filechksms (data
+ off
, size
, string_table
,
1001 strings
, bufptr
, mod_source
))
1007 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
1015 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
1020 bfd_putl32 (type
, bufptr
);
1021 bufptr
+= sizeof (uint32_t);
1023 bfd_putl32 (size
, bufptr
);
1024 bufptr
+= sizeof (uint32_t);
1026 memcpy (bufptr
, data
+ off
, size
);
1036 if (off
% sizeof (uint32_t))
1037 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
1044 /* Append the C13 info to what's already there, if the module has
1045 multiple .debug$S sections. */
1047 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
1048 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
1057 *sizeptr
+= c13_size
;
1062 /* Remap the type number stored in data from the per-module numbering to
1063 that of the deduplicated output list. */
1065 remap_type (void *data
, struct type_entry
**map
,
1066 uint32_t type_num
, uint32_t num_types
)
1068 uint32_t type
= bfd_getl32 (data
);
1070 /* Ignore builtin types (those with IDs below 0x1000). */
1071 if (type
< TPI_FIRST_INDEX
)
1074 if (type
>= TPI_FIRST_INDEX
+ type_num
)
1076 einfo (_("%P: CodeView type %v references other type %v not yet "
1077 "declared\n"), TPI_FIRST_INDEX
+ type_num
, type
);
1081 if (type
>= TPI_FIRST_INDEX
+ num_types
)
1083 einfo (_("%P: CodeView type %v references out of range type %v\n"),
1084 TPI_FIRST_INDEX
+ type_num
, type
);
1088 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
1089 bfd_putl32 (type
, data
);
1094 /* Determines whether the name of a struct, class, or union counts as
1095 "anonymous". Non-anonymous types have a hash based on just the name,
1096 rather than the whole structure. */
1098 is_name_anonymous (char *name
, size_t len
)
1100 static const char tag1
[] = "<unnamed-tag>";
1101 static const char tag2
[] = "__unnamed";
1102 static const char tag3
[] = "::<unnamed-tag>";
1103 static const char tag4
[] = "::__unnamed";
1105 if (len
== sizeof (tag1
) - 1 && !memcmp (name
, tag1
, sizeof (tag1
) - 1))
1108 if (len
== sizeof (tag2
) - 1 && !memcmp (name
, tag2
, sizeof (tag2
) - 1))
1111 if (len
>= sizeof (tag3
) - 1
1112 && !memcmp (name
+ len
- sizeof (tag3
) + 1, tag3
, sizeof (tag3
) - 1))
1115 if (len
>= sizeof (tag4
) - 1
1116 && !memcmp (name
+ len
- sizeof (tag4
) + 1, tag4
, sizeof (tag4
) - 1))
1122 /* Parse a type definition in the .debug$T section. We remap the numbers
1123 of any referenced types, and if the type is not a duplicate of one
1124 already seen add it to types (for TPI types) or ids (for IPI types). */
1126 handle_type (uint8_t *data
, struct type_entry
**map
, uint32_t type_num
,
1127 uint32_t num_types
, struct types
*types
)
1129 uint16_t size
, type
;
1132 bool other_hash
= false;
1135 size
= bfd_getl16 (data
) + sizeof (uint16_t);
1136 type
= bfd_getl16 (data
+ sizeof (uint16_t));
1142 struct lf_modifier
*mod
= (struct lf_modifier
*) data
;
1144 if (size
< offsetof (struct lf_modifier
, modifier
))
1146 einfo (_("%P: warning: truncated CodeView type record "
1151 if (!remap_type (&mod
->base_type
, map
, type_num
, num_types
))
1159 struct lf_pointer
*ptr
= (struct lf_pointer
*) data
;
1161 if (size
< offsetof (struct lf_pointer
, attributes
))
1163 einfo (_("%P: warning: truncated CodeView type record"
1168 if (!remap_type (&ptr
->base_type
, map
, type_num
, num_types
))
1176 struct lf_procedure
*proc
= (struct lf_procedure
*) data
;
1178 if (size
< sizeof (struct lf_procedure
))
1180 einfo (_("%P: warning: truncated CodeView type record"
1181 " LF_PROCEDURE\n"));
1185 if (!remap_type (&proc
->return_type
, map
, type_num
, num_types
))
1188 if (!remap_type (&proc
->arglist
, map
, type_num
, num_types
))
1196 struct lf_mfunction
*func
= (struct lf_mfunction
*) data
;
1198 if (size
< sizeof (struct lf_procedure
))
1200 einfo (_("%P: warning: truncated CodeView type record"
1201 " LF_MFUNCTION\n"));
1205 if (!remap_type (&func
->return_type
, map
, type_num
, num_types
))
1208 if (!remap_type (&func
->containing_class_type
, map
, type_num
,
1212 if (!remap_type (&func
->this_type
, map
, type_num
, num_types
))
1215 if (!remap_type (&func
->arglist
, map
, type_num
, num_types
))
1223 uint32_t num_entries
;
1224 struct lf_arglist
*al
= (struct lf_arglist
*) data
;
1226 if (size
< offsetof (struct lf_arglist
, args
))
1228 einfo (_("%P: warning: truncated CodeView type record"
1233 num_entries
= bfd_getl32 (&al
->num_entries
);
1235 if (size
< offsetof (struct lf_arglist
, args
)
1236 + (num_entries
* sizeof (uint32_t)))
1238 einfo (_("%P: warning: truncated CodeView type record"
1243 for (uint32_t i
= 0; i
< num_entries
; i
++)
1245 if (!remap_type (&al
->args
[i
], map
, type_num
, num_types
))
1254 uint16_t left
= size
- sizeof (uint16_t) - sizeof (uint16_t);
1255 uint8_t *ptr
= data
+ sizeof (uint16_t) + sizeof (uint16_t);
1261 if (left
< sizeof (uint16_t))
1263 einfo (_("%P: warning: truncated CodeView type record"
1264 " LF_FIELDLIST\n"));
1268 subtype
= bfd_getl16 (ptr
);
1274 struct lf_member
*mem
= (struct lf_member
*) ptr
;
1275 size_t name_len
, subtype_len
;
1277 if (left
< offsetof (struct lf_member
, name
))
1279 einfo (_("%P: warning: truncated CodeView type record"
1284 if (!remap_type (&mem
->type
, map
, type_num
, num_types
))
1289 left
- offsetof (struct lf_member
, name
));
1291 if (name_len
== left
- offsetof (struct lf_member
, name
))
1293 einfo (_("%P: warning: name for LF_MEMBER has no"
1294 " terminating zero\n"));
1300 subtype_len
= offsetof (struct lf_member
, name
) + name_len
;
1302 if (subtype_len
% 4 != 0)
1303 subtype_len
+= 4 - (subtype_len
% 4);
1305 if (left
< subtype_len
)
1307 einfo (_("%P: warning: truncated CodeView type record"
1308 " LF_FIELDLIST\n"));
1313 left
-= subtype_len
;
1320 struct lf_enumerate
*en
= (struct lf_enumerate
*) ptr
;
1321 size_t name_len
, subtype_len
;
1324 if (left
< offsetof (struct lf_enumerate
, name
))
1326 einfo (_("%P: warning: truncated CodeView type record"
1327 " LF_ENUMERATE\n"));
1331 subtype_len
= offsetof (struct lf_enumerate
, name
);
1333 val
= bfd_getl16 (&en
->value
);
1335 /* If val >= 0x8000, the actual value immediately follows. */
1338 unsigned int param_len
= extended_value_len (val
);
1342 einfo (_("%P: warning: unhandled type %v within"
1343 " LF_ENUMERATE\n"), val
);
1347 if (left
< subtype_len
+ param_len
)
1349 einfo (_("%P: warning: truncated CodeView type"
1350 " record LF_ENUMERATE\n"));
1354 subtype_len
+= param_len
;
1357 name_len
= strnlen ((char *) ptr
+ subtype_len
,
1358 left
- subtype_len
);
1360 if (name_len
== left
- offsetof (struct lf_enumerate
, name
))
1362 einfo (_("%P: warning: name for LF_ENUMERATE has no"
1363 " terminating zero\n"));
1369 subtype_len
+= name_len
;
1371 if (subtype_len
% 4 != 0)
1372 subtype_len
+= 4 - (subtype_len
% 4);
1374 if (left
< subtype_len
)
1376 einfo (_("%P: warning: truncated CodeView type record"
1377 " LF_ENUMERATE\n"));
1382 left
-= subtype_len
;
1389 struct lf_index
*ind
= (struct lf_index
*) ptr
;
1391 if (left
< sizeof (struct lf_index
))
1393 einfo (_("%P: warning: truncated CodeView type record"
1398 if (!remap_type (&ind
->index
, map
, type_num
, num_types
))
1401 ptr
+= sizeof (struct lf_index
);
1402 left
-= sizeof (struct lf_index
);
1409 struct lf_onemethod
*meth
= (struct lf_onemethod
*) ptr
;
1410 size_t name_len
, subtype_len
;
1412 if (left
< offsetof (struct lf_onemethod
, name
))
1414 einfo (_("%P: warning: truncated CodeView type record"
1415 " LF_ONEMETHOD\n"));
1419 if (!remap_type (&meth
->method_type
, map
, type_num
,
1424 strnlen (meth
->name
,
1425 left
- offsetof (struct lf_onemethod
, name
));
1427 if (name_len
== left
- offsetof (struct lf_onemethod
, name
))
1429 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
1430 " terminating zero\n"));
1436 subtype_len
= offsetof (struct lf_onemethod
, name
)
1439 if (subtype_len
% 4 != 0)
1440 subtype_len
+= 4 - (subtype_len
% 4);
1442 if (left
< subtype_len
)
1444 einfo (_("%P: warning: truncated CodeView type record"
1445 " LF_FIELDLIST\n"));
1450 left
-= subtype_len
;
1457 struct lf_method
*meth
= (struct lf_method
*) ptr
;
1458 size_t name_len
, subtype_len
;
1460 if (left
< offsetof (struct lf_method
, name
))
1462 einfo (_("%P: warning: truncated CodeView type record"
1467 if (!remap_type (&meth
->method_list
, map
, type_num
,
1472 strnlen (meth
->name
,
1473 left
- offsetof (struct lf_method
, name
));
1475 if (name_len
== left
- offsetof (struct lf_method
, name
))
1477 einfo (_("%P: warning: name for LF_METHOD has no"
1478 " terminating zero\n"));
1484 subtype_len
= offsetof (struct lf_method
, name
) + name_len
;
1486 if (subtype_len
% 4 != 0)
1487 subtype_len
+= 4 - (subtype_len
% 4);
1489 if (left
< subtype_len
)
1491 einfo (_("%P: warning: truncated CodeView type record"
1492 " LF_FIELDLIST\n"));
1497 left
-= subtype_len
;
1504 struct lf_bclass
*bc
= (struct lf_bclass
*) ptr
;
1506 if (left
< sizeof (struct lf_bclass
))
1508 einfo (_("%P: warning: truncated CodeView type record"
1513 if (!remap_type (&bc
->base_class_type
, map
, type_num
,
1517 ptr
+= sizeof (struct lf_bclass
);
1518 left
-= sizeof (struct lf_bclass
);
1525 struct lf_vfunctab
*vft
= (struct lf_vfunctab
*) ptr
;
1527 if (left
< sizeof (struct lf_vfunctab
))
1529 einfo (_("%P: warning: truncated CodeView type record"
1534 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
1537 ptr
+= sizeof (struct lf_vfunctab
);
1538 left
-= sizeof (struct lf_vfunctab
);
1546 struct lf_vbclass
*vbc
= (struct lf_vbclass
*) ptr
;
1548 if (left
< sizeof (struct lf_vbclass
))
1550 einfo (_("%P: warning: truncated CodeView type record"
1551 " LF_VBCLASS/LF_IVBCLASS\n"));
1555 if (!remap_type (&vbc
->base_class_type
, map
, type_num
,
1559 if (!remap_type (&vbc
->virtual_base_pointer_type
, map
,
1560 type_num
, num_types
))
1563 ptr
+= sizeof (struct lf_vbclass
);
1564 left
-= sizeof (struct lf_vbclass
);
1571 struct lf_static_member
*st
=
1572 (struct lf_static_member
*) ptr
;
1573 size_t name_len
, subtype_len
;
1575 if (left
< offsetof (struct lf_static_member
, name
))
1577 einfo (_("%P: warning: truncated CodeView type record"
1582 if (!remap_type (&st
->type
, map
, type_num
, num_types
))
1587 left
- offsetof (struct lf_static_member
, name
));
1589 if (name_len
== left
1590 - offsetof (struct lf_static_member
, name
))
1592 einfo (_("%P: warning: name for LF_STMEMBER has no"
1593 " terminating zero\n"));
1599 subtype_len
= offsetof (struct lf_static_member
, name
)
1602 if (subtype_len
% 4 != 0)
1603 subtype_len
+= 4 - (subtype_len
% 4);
1605 if (left
< subtype_len
)
1607 einfo (_("%P: warning: truncated CodeView type record"
1608 " LF_FIELDLIST\n"));
1613 left
-= subtype_len
;
1620 struct lf_nest_type
*nest
= (struct lf_nest_type
*) ptr
;
1621 size_t name_len
, subtype_len
;
1623 if (left
< offsetof (struct lf_nest_type
, name
))
1625 einfo (_("%P: warning: truncated CodeView type record"
1630 if (!remap_type (&nest
->type
, map
, type_num
, num_types
))
1634 strnlen (nest
->name
,
1635 left
- offsetof (struct lf_nest_type
, name
));
1637 if (name_len
== left
- offsetof (struct lf_nest_type
, name
))
1639 einfo (_("%P: warning: name for LF_NESTTYPE has no"
1640 " terminating zero\n"));
1646 subtype_len
= offsetof (struct lf_nest_type
, name
)
1649 if (subtype_len
% 4 != 0)
1650 subtype_len
+= 4 - (subtype_len
% 4);
1652 if (left
< subtype_len
)
1654 einfo (_("%P: warning: truncated CodeView type record"
1655 " LF_FIELDLIST\n"));
1660 left
-= subtype_len
;
1666 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
1677 struct lf_bitfield
*bf
= (struct lf_bitfield
*) data
;
1679 if (size
< offsetof (struct lf_bitfield
, length
))
1681 einfo (_("%P: warning: truncated CodeView type record"
1686 if (!remap_type (&bf
->base_type
, map
, type_num
, num_types
))
1694 struct lf_methodlist
*ml
= (struct lf_methodlist
*) data
;
1695 unsigned int num_entries
;
1697 if (size
< offsetof (struct lf_methodlist
, entries
))
1699 einfo (_("%P: warning: truncated CodeView type record"
1700 " LF_METHODLIST\n"));
1704 if ((size
- offsetof (struct lf_methodlist
, entries
))
1705 % sizeof (struct lf_methodlist_entry
))
1707 einfo (_("%P: warning: malformed CodeView type record"
1708 " LF_METHODLIST\n"));
1712 num_entries
= (size
- offsetof (struct lf_methodlist
, entries
))
1713 / sizeof (struct lf_methodlist_entry
);
1715 for (unsigned int i
= 0; i
< num_entries
; i
++)
1717 if (!remap_type (&ml
->entries
[i
].method_type
, map
,
1718 type_num
, num_types
))
1727 struct lf_array
*arr
= (struct lf_array
*) data
;
1729 if (size
< offsetof (struct lf_array
, length_in_bytes
))
1731 einfo (_("%P: warning: truncated CodeView type record"
1736 if (!remap_type (&arr
->element_type
, map
, type_num
, num_types
))
1739 if (!remap_type (&arr
->index_type
, map
, type_num
, num_types
))
1748 struct lf_class
*cl
= (struct lf_class
*) data
;
1752 if (size
< offsetof (struct lf_class
, name
))
1754 einfo (_("%P: warning: truncated CodeView type record"
1755 " LF_CLASS/LF_STRUCTURE\n"));
1759 if (!remap_type (&cl
->field_list
, map
, type_num
, num_types
))
1762 if (!remap_type (&cl
->derived_from
, map
, type_num
, num_types
))
1765 if (!remap_type (&cl
->vshape
, map
, type_num
, num_types
))
1768 name_len
= strnlen (cl
->name
, size
- offsetof (struct lf_class
, name
));
1770 if (name_len
== size
- offsetof (struct lf_class
, name
))
1772 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
1773 " terminating zero\n"));
1777 prop
= bfd_getl16 (&cl
->properties
);
1779 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1781 /* Structure has another name following first one. */
1783 size_t len
= offsetof (struct lf_class
, name
) + name_len
+ 1;
1784 size_t unique_name_len
;
1786 unique_name_len
= strnlen (cl
->name
+ name_len
+ 1, size
- len
);
1788 if (unique_name_len
== size
- len
)
1790 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
1791 " has no terminating zero\n"));
1796 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
1797 && !is_name_anonymous (cl
->name
, name_len
))
1800 cv_hash
= crc32 ((uint8_t *) cl
->name
, name_len
);
1808 struct lf_union
*un
= (struct lf_union
*) data
;
1812 if (size
< offsetof (struct lf_union
, name
))
1814 einfo (_("%P: warning: truncated CodeView type record"
1819 if (!remap_type (&un
->field_list
, map
, type_num
, num_types
))
1822 name_len
= strnlen (un
->name
, size
- offsetof (struct lf_union
, name
));
1824 if (name_len
== size
- offsetof (struct lf_union
, name
))
1826 einfo (_("%P: warning: name for LF_UNION has no"
1827 " terminating zero\n"));
1831 prop
= bfd_getl16 (&un
->properties
);
1833 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1835 /* Structure has another name following first one. */
1837 size_t len
= offsetof (struct lf_union
, name
) + name_len
+ 1;
1838 size_t unique_name_len
;
1840 unique_name_len
= strnlen (un
->name
+ name_len
+ 1, size
- len
);
1842 if (unique_name_len
== size
- len
)
1844 einfo (_("%P: warning: unique name for LF_UNION has"
1845 " no terminating zero\n"));
1850 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
1851 && !is_name_anonymous (un
->name
, name_len
))
1854 cv_hash
= crc32 ((uint8_t *) un
->name
, name_len
);
1862 struct lf_enum
*en
= (struct lf_enum
*) data
;
1866 if (size
< offsetof (struct lf_enum
, name
))
1868 einfo (_("%P: warning: truncated CodeView type record"
1873 if (!remap_type (&en
->underlying_type
, map
, type_num
, num_types
))
1876 if (!remap_type (&en
->field_list
, map
, type_num
, num_types
))
1879 name_len
= strnlen (en
->name
, size
- offsetof (struct lf_enum
, name
));
1881 if (name_len
== size
- offsetof (struct lf_enum
, name
))
1883 einfo (_("%P: warning: name for LF_ENUM has no"
1884 " terminating zero\n"));
1888 prop
= bfd_getl16 (&en
->properties
);
1890 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
1892 /* Structure has another name following first one. */
1894 size_t len
= offsetof (struct lf_enum
, name
) + name_len
+ 1;
1895 size_t unique_name_len
;
1897 unique_name_len
= strnlen (en
->name
+ name_len
+ 1, size
- len
);
1899 if (unique_name_len
== size
- len
)
1901 einfo (_("%P: warning: unique name for LF_ENUM has"
1902 " no terminating zero\n"));
1911 /* Does not reference any types, nothing to be done. */
1915 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type
);
1919 hash
= iterative_hash (data
, size
, 0);
1921 slot
= htab_find_slot_with_hash (types
->hashmap
, data
, hash
, INSERT
);
1925 if (!*slot
) /* new entry */
1927 struct type_entry
*e
;
1929 *slot
= xmalloc (offsetof (struct type_entry
, data
) + size
);
1931 e
= (struct type_entry
*) *slot
;
1934 e
->index
= types
->num_types
;
1937 e
->cv_hash
= cv_hash
;
1939 e
->cv_hash
= crc32 (data
, size
);
1941 memcpy (e
->data
, data
, size
);
1944 types
->last
->next
= e
;
1954 else /* duplicate */
1956 map
[type_num
] = (struct type_entry
*) *slot
;
1962 /* Parse the .debug$T section of a module, and pass any type definitions
1963 found to handle_type. */
1965 handle_debugt_section (asection
*s
, bfd
*mod
, struct types
*types
)
1967 bfd_byte
*data
= NULL
;
1969 unsigned int num_types
= 0;
1970 struct type_entry
**map
;
1973 if (!bfd_get_full_section_contents (mod
, s
, &data
))
1979 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
1985 off
= sizeof (uint32_t);
1987 while (off
+ sizeof (uint16_t) <= s
->size
)
1991 size
= bfd_getl16 (data
+ off
);
1992 off
+= sizeof (uint16_t);
1994 if (size
+ off
> s
->size
|| size
<= sizeof (uint16_t))
1997 bfd_set_error (bfd_error_bad_value
);
2011 map
= xcalloc (num_types
, sizeof (struct type_entry
*));
2013 off
= sizeof (uint32_t);
2016 while (off
+ sizeof (uint16_t) <= s
->size
)
2020 size
= bfd_getl16 (data
+ off
);
2022 if (!handle_type (data
+ off
, map
, type_num
, num_types
, types
))
2026 bfd_set_error (bfd_error_bad_value
);
2030 off
+= sizeof (uint16_t) + size
;
2040 /* Populate the module stream, which consists of the transformed .debug$S
2041 data for each object file. */
2043 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
2044 struct string_table
*strings
,
2045 uint32_t *c13_info_size
,
2046 struct mod_source_files
*mod_source
,
2047 bfd
*abfd
, struct types
*types
)
2049 uint8_t int_buf
[sizeof (uint32_t)];
2050 uint8_t *c13_info
= NULL
;
2052 *sym_byte_size
= sizeof (uint32_t);
2055 /* Process .debug$S section(s). */
2057 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
2059 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
2061 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
2062 c13_info_size
, mod_source
, abfd
))
2065 free (mod_source
->files
);
2069 else if (!strcmp (s
->name
, ".debug$T") && s
->size
>= sizeof (uint32_t))
2071 if (!handle_debugt_section (s
, mod
, types
))
2074 free (mod_source
->files
);
2080 /* Write the signature. */
2082 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
2084 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
2092 if (bfd_bwrite (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
2101 /* Write the global refs size. */
2103 bfd_putl32 (0, int_buf
);
2105 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
2111 /* Create the module info substream within the DBI. */
2113 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
2114 uint32_t *size
, struct string_table
*strings
,
2115 struct source_files_info
*source
,
2116 struct types
*types
)
2119 unsigned int mod_num
;
2121 static const char linker_fn
[] = "* Linker *";
2125 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2128 size_t len
= sizeof (struct module_info
);
2130 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
2132 len
+= sizeof (linker_fn
); /* Object name. */
2133 len
++; /* Empty module name. */
2135 else if (in
->my_archive
)
2137 char *name
= lrealpath (bfd_get_filename (in
));
2139 len
+= strlen (name
) + 1; /* Object name. */
2143 name
= lrealpath (bfd_get_filename (in
->my_archive
));
2145 len
+= strlen (name
) + 1; /* Archive name. */
2151 char *name
= lrealpath (bfd_get_filename (in
));
2152 size_t name_len
= strlen (name
) + 1;
2154 len
+= name_len
; /* Object name. */
2155 len
+= name_len
; /* And again as the archive name. */
2161 len
+= 4 - (len
% 4);
2165 source
->mod_count
++;
2168 *data
= xmalloc (*size
);
2172 source
->mods
= xmalloc (source
->mod_count
2173 * sizeof (struct mod_source_files
));
2174 memset (source
->mods
, 0,
2175 source
->mod_count
* sizeof (struct mod_source_files
));
2179 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2182 struct module_info
*mod
= (struct module_info
*) ptr
;
2183 uint16_t stream_num
;
2185 uint32_t sym_byte_size
, c13_info_size
;
2186 uint8_t *start
= ptr
;
2188 stream
= add_stream (pdb
, NULL
, &stream_num
);
2192 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
2194 free (source
->mods
[i
].files
);
2197 free (source
->mods
);
2202 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
2203 strings
, &c13_info_size
,
2204 &source
->mods
[mod_num
], abfd
,
2207 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
2209 free (source
->mods
[i
].files
);
2212 free (source
->mods
);
2217 bfd_putl32 (0, &mod
->unused1
);
2219 /* These are dummy values - MSVC copies the first section contribution
2220 entry here, but doesn't seem to use it for anything. */
2221 bfd_putl16 (0xffff, &mod
->sc
.section
);
2222 bfd_putl16 (0, &mod
->sc
.padding1
);
2223 bfd_putl32 (0, &mod
->sc
.offset
);
2224 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
2225 bfd_putl32 (0, &mod
->sc
.characteristics
);
2226 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
2227 bfd_putl16 (0, &mod
->sc
.padding2
);
2228 bfd_putl32 (0, &mod
->sc
.data_crc
);
2229 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
2231 bfd_putl16 (0, &mod
->flags
);
2232 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
2233 bfd_putl32 (sym_byte_size
, &mod
->sym_byte_size
);
2234 bfd_putl32 (0, &mod
->c11_byte_size
);
2235 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
2236 bfd_putl16 (0, &mod
->source_file_count
);
2237 bfd_putl16 (0, &mod
->padding
);
2238 bfd_putl32 (0, &mod
->unused2
);
2239 bfd_putl32 (0, &mod
->source_file_name_index
);
2240 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
2242 ptr
+= sizeof (struct module_info
);
2244 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
2247 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
2248 ptr
+= sizeof (linker_fn
);
2250 /* Empty module name. */
2254 else if (in
->my_archive
)
2256 char *name
= lrealpath (bfd_get_filename (in
));
2257 size_t name_len
= strlen (name
) + 1;
2260 memcpy (ptr
, name
, name_len
);
2265 name
= lrealpath (bfd_get_filename (in
->my_archive
));
2266 name_len
= strlen (name
) + 1;
2269 memcpy (ptr
, name
, name_len
);
2276 char *name
= lrealpath (bfd_get_filename (in
));
2277 size_t name_len
= strlen (name
) + 1;
2280 memcpy (ptr
, name
, name_len
);
2283 /* Object name again as archive name. */
2284 memcpy (ptr
, name
, name_len
);
2290 /* Pad to next four-byte boundary. */
2292 if ((ptr
- start
) % 4)
2294 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
2295 ptr
+= 4 - ((ptr
- start
) % 4);
2304 /* Return the index of a given output section. */
2306 find_section_number (bfd
*abfd
, asection
*sect
)
2310 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
2315 /* Empty sections aren't output. */
2323 /* Create the substream which maps addresses in the image file to locations
2324 in the original object files. */
2326 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
2328 unsigned int num_sc
= 0;
2329 struct section_contribution
*sc
;
2334 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2337 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
2339 if (s
->size
== 0 || discarded_section (s
))
2346 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
2347 *data
= xmalloc (*size
);
2349 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
2351 /* Read characteristics of outputted sections. */
2353 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
2355 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2356 offset
+= offsetof (struct external_scnhdr
, s_flags
);
2358 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
2360 bfd_seek (abfd
, offset
, SEEK_SET
);
2362 if (bfd_bread (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
2363 abfd
) != sizeof (uint32_t))
2370 offset
+= sizeof (struct external_scnhdr
);
2374 (struct section_contribution
*) ((uint8_t *) *data
+ sizeof (uint32_t));
2377 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2380 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
2384 if (s
->size
== 0 || discarded_section (s
))
2387 sect_num
= find_section_number (abfd
, s
->output_section
);
2389 memcpy (&sc
->characteristics
,
2390 sect_flags
+ ((sect_num
- 1) * sizeof (uint32_t)),
2393 bfd_putl16 (sect_num
, &sc
->section
);
2394 bfd_putl16 (0, &sc
->padding1
);
2395 bfd_putl32 (s
->output_offset
, &sc
->offset
);
2396 bfd_putl32 (s
->size
, &sc
->size
);
2397 bfd_putl16 (mod_index
, &sc
->module_index
);
2398 bfd_putl16 (0, &sc
->padding2
);
2399 bfd_putl32 (0, &sc
->data_crc
);
2400 bfd_putl32 (0, &sc
->reloc_crc
);
2413 /* The source info substream lives within the DBI stream, and lists the
2414 source files for each object file (i.e. it's derived from the
2415 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
2416 superfluous, as the filenames are also available in the C13 parts of
2417 the module streams, but MSVC relies on it to work properly. */
2419 create_source_info_substream (void **data
, uint32_t *size
,
2420 struct source_files_info
*source
)
2422 uint16_t dedupe_source_files_count
= 0;
2423 uint16_t source_files_count
= 0;
2424 uint32_t strings_len
= 0;
2427 /* Loop through the source files, marking unique filenames. The pointers
2428 here are for entries in the main string table, and so have already
2429 been deduplicated. */
2431 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2433 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2435 if (source
->mods
[i
].files
[j
])
2437 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
2439 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
2440 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
2441 dedupe_source_files_count
++;
2444 source_files_count
++;
2449 *size
= sizeof (uint16_t) + sizeof (uint16_t);
2450 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
2451 *size
+= sizeof (uint32_t) * source_files_count
;
2452 *size
+= strings_len
;
2454 *data
= xmalloc (*size
);
2456 ptr
= (uint8_t *) *data
;
2458 /* Write header (module count and source file count). */
2460 bfd_putl16 (source
->mod_count
, ptr
);
2461 ptr
+= sizeof (uint16_t);
2463 bfd_putl16 (dedupe_source_files_count
, ptr
);
2464 ptr
+= sizeof (uint16_t);
2466 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
2467 present, but does not appear to be useful". */
2469 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2471 bfd_putl16 (i
, ptr
);
2472 ptr
+= sizeof (uint16_t);
2475 /* Write source file count for each module. */
2477 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2479 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
2480 ptr
+= sizeof (uint16_t);
2483 /* For each module, write the offsets within the string table
2484 for each source file. */
2486 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2488 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2490 if (source
->mods
[i
].files
[j
])
2492 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
2493 ptr
+= sizeof (uint32_t);
2498 /* Write the string table. We set source_file_offset to a dummy value for
2499 each entry we write, so we don't write duplicate filenames. */
2501 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
2503 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
2505 if (source
->mods
[i
].files
[j
]
2506 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
2508 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
2509 source
->mods
[i
].files
[j
]->len
);
2510 ptr
+= source
->mods
[i
].files
[j
]->len
;
2515 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
2521 /* Stream 4 is the debug information (DBI) stream. */
2523 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
2524 uint16_t section_header_stream_num
,
2525 uint16_t sym_rec_stream_num
,
2526 uint16_t publics_stream_num
,
2527 struct string_table
*strings
,
2528 struct types
*types
)
2530 struct pdb_dbi_stream_header h
;
2531 struct optional_dbg_header opt
;
2532 void *mod_info
, *sc
, *source_info
;
2533 uint32_t mod_info_size
, sc_size
, source_info_size
;
2534 struct source_files_info source
;
2536 source
.mod_count
= 0;
2539 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
2540 strings
, &source
, types
))
2543 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
2545 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
2547 free (source
.mods
[i
].files
);
2555 create_source_info_substream (&source_info
, &source_info_size
, &source
);
2557 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
2559 free (source
.mods
[i
].files
);
2563 bfd_putl32 (0xffffffff, &h
.version_signature
);
2564 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
2565 bfd_putl32 (1, &h
.age
);
2566 bfd_putl16 (0xffff, &h
.global_stream_index
);
2567 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
2568 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
2569 bfd_putl16 (0, &h
.pdb_dll_version
);
2570 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
2571 bfd_putl16 (0, &h
.pdb_dll_rbld
);
2572 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
2573 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
2574 bfd_putl32 (0, &h
.section_map_size
);
2575 bfd_putl32 (source_info_size
, &h
.source_info_size
);
2576 bfd_putl32 (0, &h
.type_server_map_size
);
2577 bfd_putl32 (0, &h
.mfc_type_server_index
);
2578 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
2579 bfd_putl32 (0, &h
.ec_substream_size
);
2580 bfd_putl16 (0, &h
.flags
);
2581 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
2582 bfd_putl32 (0, &h
.padding
);
2584 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
2592 if (bfd_bwrite (mod_info
, mod_info_size
, stream
) != mod_info_size
)
2602 if (bfd_bwrite (sc
, sc_size
, stream
) != sc_size
)
2611 if (bfd_bwrite (source_info
, source_info_size
, stream
) != source_info_size
)
2619 bfd_putl16 (0xffff, &opt
.fpo_stream
);
2620 bfd_putl16 (0xffff, &opt
.exception_stream
);
2621 bfd_putl16 (0xffff, &opt
.fixup_stream
);
2622 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
2623 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
2624 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
2625 bfd_putl16 (0xffff, &opt
.token_map_stream
);
2626 bfd_putl16 (0xffff, &opt
.xdata_stream
);
2627 bfd_putl16 (0xffff, &opt
.pdata_stream
);
2628 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
2629 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
2631 if (bfd_bwrite (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
2637 /* Used as parameter to qsort, to sort publics by hash. */
2639 public_compare_hash (const void *s1
, const void *s2
)
2641 const struct public *p1
= *(const struct public **) s1
;
2642 const struct public *p2
= *(const struct public **) s2
;
2644 if (p1
->hash
< p2
->hash
)
2646 if (p1
->hash
> p2
->hash
)
2652 /* Used as parameter to qsort, to sort publics by address. */
2654 public_compare_addr (const void *s1
, const void *s2
)
2656 const struct public *p1
= *(const struct public **) s1
;
2657 const struct public *p2
= *(const struct public **) s2
;
2659 if (p1
->section
< p2
->section
)
2661 if (p1
->section
> p2
->section
)
2664 if (p1
->address
< p2
->address
)
2666 if (p1
->address
> p2
->address
)
2672 /* The publics stream is a hash map of S_PUB32 records, which are stored
2673 in the symbol record stream. Each S_PUB32 entry represents a symbol
2674 from the point of view of the linker: a section index, an offset within
2675 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
2676 which are the same thing but generated by the compiler. */
2678 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
2680 struct publics_header header
;
2681 struct globals_hash_header hash_header
;
2682 const unsigned int num_buckets
= 4096;
2683 unsigned int num_entries
= 0, filled_buckets
= 0;
2684 unsigned int buckets_size
, sym_hash_size
;
2685 char int_buf
[sizeof (uint32_t)];
2686 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
2687 struct public **buckets
;
2688 struct public **sorted
= NULL
;
2691 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
2692 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
2694 /* Loop through the global symbols in our input files, and write S_PUB32
2695 records in the symbol record stream for those that make it into the
2697 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
2700 if (!in
->outsymbols
)
2703 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
2705 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
2707 if (sym
->flags
& BSF_GLOBAL
)
2710 uint16_t record_length
;
2711 const char *name
= sym
->name
;
2712 size_t name_len
= strlen (name
);
2713 struct public *p
= xmalloc (sizeof (struct public));
2714 unsigned int padding
= 0;
2719 find_section_number (abfd
, sym
->section
->output_section
);
2725 p
->offset
= bfd_tell (sym_rec_stream
);
2726 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
2727 p
->section
= section
;
2728 p
->address
= sym
->section
->output_offset
+ sym
->value
;
2730 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
2732 if (record_length
% 4)
2733 padding
= 4 - (record_length
% 4);
2735 /* Assume that all global symbols in executable sections
2737 if (sym
->section
->flags
& SEC_CODE
)
2738 flags
= PUBSYM_FUNCTION
;
2740 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
2742 bfd_putl16 (S_PUB32
, &ps
.record_type
);
2743 bfd_putl32 (flags
, &ps
.flags
);
2744 bfd_putl32 (p
->address
, &ps
.offset
);
2745 bfd_putl16 (p
->section
, &ps
.section
);
2747 if (bfd_bwrite (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
2748 sizeof (struct pubsym
))
2751 if (bfd_bwrite (name
, name_len
+ 1, sym_rec_stream
) !=
2755 for (unsigned int j
= 0; j
< padding
; j
++)
2759 if (bfd_bwrite (&b
, sizeof (uint8_t), sym_rec_stream
) !=
2767 publics_tail
->next
= p
;
2776 if (num_entries
> 0)
2778 /* Create an array of pointers, sorted by hash value. */
2780 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
2782 struct public *p
= publics_head
;
2783 for (unsigned int i
= 0; i
< num_entries
; i
++)
2789 qsort (sorted
, num_entries
, sizeof (struct public *),
2790 public_compare_hash
);
2792 /* Populate the buckets. */
2794 for (unsigned int i
= 0; i
< num_entries
; i
++)
2796 if (!buckets
[sorted
[i
]->hash
])
2798 buckets
[sorted
[i
]->hash
] = sorted
[i
];
2802 sorted
[i
]->index
= i
;
2806 buckets_size
= num_buckets
/ 8;
2807 buckets_size
+= sizeof (uint32_t);
2808 buckets_size
+= filled_buckets
* sizeof (uint32_t);
2810 sym_hash_size
= sizeof (hash_header
);
2811 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
2812 sym_hash_size
+= buckets_size
;
2814 /* Output the publics header. */
2816 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
2817 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
2818 bfd_putl32 (0, &header
.num_thunks
);
2819 bfd_putl32 (0, &header
.thunks_size
);
2820 bfd_putl32 (0, &header
.thunk_table
);
2821 bfd_putl32 (0, &header
.thunk_table_offset
);
2822 bfd_putl32 (0, &header
.num_sects
);
2824 if (bfd_bwrite (&header
, sizeof (header
), stream
) != sizeof (header
))
2827 /* Output the global hash header. */
2829 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
2830 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
2831 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
2832 &hash_header
.entries_size
);
2833 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
2835 if (bfd_bwrite (&hash_header
, sizeof (hash_header
), stream
) !=
2836 sizeof (hash_header
))
2839 /* Write the entries in hash order. */
2841 for (unsigned int i
= 0; i
< num_entries
; i
++)
2843 struct hash_record hr
;
2845 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
2846 bfd_putl32 (1, &hr
.reference
);
2848 if (bfd_bwrite (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
2852 /* Write the bitmap for filled and unfilled buckets. */
2854 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
2858 for (unsigned int j
= 0; j
< 8; j
++)
2864 if (bfd_bwrite (&v
, sizeof (v
), stream
) != sizeof (v
))
2868 /* Add a 4-byte gap. */
2870 bfd_putl32 (0, int_buf
);
2872 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
2875 /* Write the bucket offsets. */
2877 for (unsigned int i
= 0; i
< num_buckets
; i
++)
2881 /* 0xc is size of internal hash_record structure in
2882 Microsoft's parser. */
2883 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
2885 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
2891 /* Write the address map: offsets into the symbol record stream of
2892 S_PUB32 records, ordered by address. */
2894 if (num_entries
> 0)
2896 qsort (sorted
, num_entries
, sizeof (struct public *),
2897 public_compare_addr
);
2899 for (unsigned int i
= 0; i
< num_entries
; i
++)
2901 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
2903 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
2914 while (publics_head
)
2916 struct public *p
= publics_head
->next
;
2918 free (publics_head
);
2927 /* The section header stream contains a copy of the section headers
2928 from the PE file, in the same format. */
2930 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
2933 unsigned int section_count
;
2938 stream
= add_stream (pdb
, NULL
, num
);
2942 section_count
= abfd
->section_count
;
2944 /* Empty sections aren't output. */
2945 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
2947 if (sect
->size
== 0)
2951 if (section_count
== 0)
2954 /* Copy section table from output - it's already been written at this
2957 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
2959 bfd_seek (abfd
, scn_base
, SEEK_SET
);
2961 len
= section_count
* sizeof (struct external_scnhdr
);
2962 buf
= xmalloc (len
);
2964 if (bfd_bread (buf
, len
, abfd
) != len
)
2970 if (bfd_bwrite (buf
, len
, stream
) != len
)
2981 /* Populate the "/names" named stream, which contains the string table. */
2983 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
2985 char int_buf
[sizeof (uint32_t)];
2986 struct string_table_header h
;
2987 uint32_t num_strings
= 0, num_buckets
;
2988 struct string
**buckets
;
2990 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
2991 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
2993 if (bfd_bwrite (&h
, sizeof (h
), stream
) != sizeof (h
))
2996 bfd_putl32 (strings
->strings_len
, int_buf
);
2998 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3003 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
3006 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
3008 if (bfd_bwrite (s
->s
, s
->len
, stream
) != s
->len
)
3011 if (bfd_bwrite (int_buf
, 1, stream
) != 1)
3017 num_buckets
= num_strings
* 2;
3019 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
3020 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
3022 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
3024 uint32_t bucket_num
= s
->hash
% num_buckets
;
3026 while (buckets
[bucket_num
])
3030 if (bucket_num
== num_buckets
)
3034 buckets
[bucket_num
] = s
;
3037 bfd_putl32 (num_buckets
, int_buf
);
3039 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3045 for (unsigned int i
= 0; i
< num_buckets
; i
++)
3048 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
3050 bfd_putl32 (0, int_buf
);
3052 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) !=
3062 bfd_putl32 (num_strings
, int_buf
);
3064 if (bfd_bwrite (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3070 /* Calculate the hash of a type_entry. */
3072 hash_type_entry (const void *p
)
3074 const struct type_entry
*e
= (const struct type_entry
*) p
;
3075 uint16_t size
= bfd_getl16 (e
->data
) + sizeof (uint16_t);
3077 return iterative_hash (e
->data
, size
, 0);
3080 /* Compare a type_entry with a type. */
3082 eq_type_entry (const void *a
, const void *b
)
3084 const struct type_entry
*e
= (const struct type_entry
*) a
;
3085 uint16_t size_a
= bfd_getl16 (e
->data
);
3086 uint16_t size_b
= bfd_getl16 (b
);
3088 if (size_a
!= size_b
)
3091 return memcmp (e
->data
+ sizeof (uint16_t),
3092 (const uint8_t *) b
+ sizeof (uint16_t), size_a
) == 0;
3095 /* Create a PDB debugging file for the PE image file abfd with the build ID
3096 guid, stored at pdb_name. */
3098 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
3102 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
3103 *publics_stream
, *tpi_stream
, *ipi_stream
;
3104 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
3105 struct string_table strings
;
3106 struct types types
, ids
;
3108 pdb
= bfd_openw (pdb_name
, "pdb");
3111 einfo (_("%P: warning: cannot create PDB file: %E\n"));
3115 strings
.strings_head
= NULL
;
3116 strings
.strings_tail
= NULL
;
3117 strings
.strings_len
= 1;
3118 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
3119 eq_string_table_entry
, free
,
3122 bfd_set_format (pdb
, bfd_archive
);
3124 if (!create_old_directory_stream (pdb
))
3126 einfo (_("%P: warning: cannot create old directory stream "
3127 "in PDB file: %E\n"));
3131 info_stream
= add_stream (pdb
, NULL
, NULL
);
3135 einfo (_("%P: warning: cannot create info stream "
3136 "in PDB file: %E\n"));
3140 tpi_stream
= add_stream (pdb
, NULL
, NULL
);
3144 einfo (_("%P: warning: cannot create TPI stream "
3145 "in PDB file: %E\n"));
3149 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
3153 einfo (_("%P: warning: cannot create DBI stream "
3154 "in PDB file: %E\n"));
3158 ipi_stream
= add_stream (pdb
, NULL
, NULL
);
3162 einfo (_("%P: warning: cannot create IPI stream "
3163 "in PDB file: %E\n"));
3167 names_stream
= add_stream (pdb
, "/names", NULL
);
3171 einfo (_("%P: warning: cannot create /names stream "
3172 "in PDB file: %E\n"));
3176 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
3178 if (!sym_rec_stream
)
3180 einfo (_("%P: warning: cannot create symbol record stream "
3181 "in PDB file: %E\n"));
3185 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
3187 if (!publics_stream
)
3189 einfo (_("%P: warning: cannot create publics stream "
3190 "in PDB file: %E\n"));
3194 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
3196 einfo (_("%P: warning: cannot create section header stream "
3197 "in PDB file: %E\n"));
3201 types
.num_types
= 0;
3202 types
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
3203 free
, xcalloc
, free
);
3204 types
.first
= types
.last
= NULL
;
3207 ids
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
3208 free
, xcalloc
, free
);
3209 ids
.first
= ids
.last
= NULL
;
3211 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
3212 sym_rec_stream_num
, publics_stream_num
,
3215 einfo (_("%P: warning: cannot populate DBI stream "
3216 "in PDB file: %E\n"));
3217 htab_delete (types
.hashmap
);
3218 htab_delete (ids
.hashmap
);
3222 if (!populate_type_stream (pdb
, tpi_stream
, &types
))
3224 einfo (_("%P: warning: cannot populate TPI stream "
3225 "in PDB file: %E\n"));
3226 htab_delete (types
.hashmap
);
3227 htab_delete (ids
.hashmap
);
3231 htab_delete (types
.hashmap
);
3233 if (!populate_type_stream (pdb
, ipi_stream
, &ids
))
3235 einfo (_("%P: warning: cannot populate IPI stream "
3236 "in PDB file: %E\n"));
3237 htab_delete (ids
.hashmap
);
3241 htab_delete (ids
.hashmap
);
3243 add_string ("", 0, &strings
);
3245 if (!populate_names_stream (names_stream
, &strings
))
3247 einfo (_("%P: warning: cannot populate names stream "
3248 "in PDB file: %E\n"));
3252 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
3254 einfo (_("%P: warning: cannot populate publics stream "
3255 "in PDB file: %E\n"));
3259 if (!populate_info_stream (pdb
, info_stream
, guid
))
3261 einfo (_("%P: warning: cannot populate info stream "
3262 "in PDB file: %E\n"));
3271 htab_delete (strings
.hashmap
);