1 /* Support for generating PDB CodeView debugging files.
2 Copyright (C) 2022-2025 Free Software Foundation, Inc.
4 This file is part of the GNU Binutils.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19 MA 02110-1301, USA. */
27 #include "libiberty.h"
28 #include "coff/i386.h"
29 #include "coff/external.h"
30 #include "coff/internal.h"
50 uint32_t source_file_offset
;
57 struct string
*strings_head
;
58 struct string
*strings_tail
;
63 struct mod_source_files
66 struct string
**files
;
69 struct source_files_info
72 struct mod_source_files
*mods
;
77 struct type_entry
*next
;
80 bool has_udt_src_line
;
88 struct type_entry
*first
;
89 struct type_entry
*last
;
104 uint32_t num_entries
;
105 struct global
*first
;
117 static const uint32_t crc_table
[] =
119 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f,
120 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988,
121 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2,
122 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
123 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
124 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172,
125 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c,
126 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
127 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423,
128 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
129 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106,
130 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
131 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d,
132 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e,
133 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
134 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
135 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7,
136 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0,
137 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa,
138 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
139 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81,
140 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a,
141 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84,
142 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
143 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
144 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc,
145 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e,
146 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
147 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55,
148 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
149 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28,
150 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
151 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f,
152 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38,
153 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
154 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
155 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69,
156 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2,
157 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc,
158 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
159 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693,
160 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94,
161 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
164 static bool remap_type (void *data
, struct type_entry
**map
,
165 uint32_t type_num
, uint32_t num_types
);
167 /* Add a new stream to the PDB archive, and return its BFD. */
169 add_stream (bfd
*pdb
, const char *name
, uint16_t *stream_num
)
174 stream
= bfd_create (name
? name
: "", pdb
);
178 if (!bfd_make_writable (stream
))
184 if (!pdb
->archive_head
)
186 bfd_set_archive_head (pdb
, stream
);
191 bfd
*b
= pdb
->archive_head
;
195 while (b
->archive_next
)
201 b
->archive_next
= stream
;
210 /* Stream 0 ought to be a copy of the MSF directory from the last
211 time the PDB file was written. Because we don't do incremental
212 writes this isn't applicable to us, but we fill it with a dummy
213 value so as not to confuse radare. */
215 create_old_directory_stream (bfd
*pdb
)
218 char buf
[sizeof (uint32_t)];
220 stream
= add_stream (pdb
, NULL
, NULL
);
226 return bfd_write (buf
, sizeof (uint32_t), stream
) == sizeof (uint32_t);
229 /* Calculate the hash of a given string. */
231 calc_hash (const char *data
, size_t len
)
238 hash
^= data
[1] << 8;
239 hash
^= data
[2] << 16;
240 hash
^= data
[3] << 24;
249 hash
^= data
[1] << 8;
259 hash
^= (hash
>> 11);
261 return hash
^ (hash
>> 16);
264 /* Stream 1 is the PDB info stream - see
265 https://llvm.org/docs/PDB/PdbStream.html. */
267 populate_info_stream (bfd
*pdb
, bfd
*info_stream
, const unsigned char *guid
)
270 struct pdb_stream_70 h
;
271 uint32_t num_entries
, num_buckets
;
272 uint32_t names_length
, stream_num
;
273 char int_buf
[sizeof (uint32_t)];
281 struct hash_entry
**buckets
= NULL
;
285 bfd_putl32 (PDB_STREAM_VERSION_VC70
, &h
.version
);
286 bfd_putl32 (time (NULL
), &h
.signature
);
287 bfd_putl32 (1, &h
.age
);
289 bfd_putl32 (bfd_getb32 (guid
), h
.guid
);
290 bfd_putl16 (bfd_getb16 (&guid
[4]), &h
.guid
[4]);
291 bfd_putl16 (bfd_getb16 (&guid
[6]), &h
.guid
[6]);
292 memcpy (&h
.guid
[8], &guid
[8], 8);
294 if (bfd_write (&h
, sizeof (h
), info_stream
) != sizeof (h
))
297 /* Write hash list of named streams. This is a "rollover" hash, i.e.
298 if a bucket is filled an entry gets placed in the next free
302 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
304 if (strcmp (b
->filename
, ""))
308 num_buckets
= num_entries
* 2;
315 buckets
= xmalloc (sizeof (struct hash_entry
*) * num_buckets
);
316 memset (buckets
, 0, sizeof (struct hash_entry
*) * num_buckets
);
318 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
320 if (strcmp (b
->filename
, ""))
322 size_t len
= strlen (b
->filename
);
323 uint32_t hash
= (uint16_t) calc_hash (b
->filename
, len
);
324 uint32_t bucket_num
= hash
% num_buckets
;
326 while (buckets
[bucket_num
])
330 if (bucket_num
== num_buckets
)
334 buckets
[bucket_num
] = xmalloc (sizeof (struct hash_entry
));
336 buckets
[bucket_num
]->offset
= names_length
;
337 buckets
[bucket_num
]->value
= stream_num
;
339 names_length
+= len
+ 1;
346 /* Write the strings list - the hash keys are indexes into this. */
348 bfd_putl32 (names_length
, int_buf
);
350 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
354 for (bfd
*b
= pdb
->archive_head
; b
; b
= b
->archive_next
)
356 if (!strcmp (b
->filename
, ""))
359 size_t len
= strlen (b
->filename
) + 1;
361 if (bfd_write (b
->filename
, len
, info_stream
) != len
)
365 /* Write the number of entries and buckets. */
367 bfd_putl32 (num_entries
, int_buf
);
369 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
373 bfd_putl32 (num_buckets
, int_buf
);
375 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
379 /* Write the present bitmap. */
381 bfd_putl32 ((num_buckets
+ 31) / 32, int_buf
);
383 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
387 for (unsigned int i
= 0; i
< num_buckets
; i
+= 32)
391 for (unsigned int j
= 0; j
< 32; j
++)
393 if (i
+ j
>= num_buckets
)
400 bfd_putl32 (v
, int_buf
);
402 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
407 /* Write the (empty) deleted bitmap. */
409 bfd_putl32 (0, int_buf
);
411 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
415 /* Write the buckets. */
417 for (unsigned int i
= 0; i
< num_buckets
; i
++)
421 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
423 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
427 bfd_putl32 (buckets
[i
]->value
, int_buf
);
429 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
435 bfd_putl32 (0, int_buf
);
437 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
441 bfd_putl32 (PDB_STREAM_VERSION_VC140
, int_buf
);
443 if (bfd_write (int_buf
, sizeof (uint32_t), info_stream
) !=
450 for (unsigned int i
= 0; i
< num_buckets
; i
++)
461 /* Calculate the CRC32 used for type hashes. */
463 crc32 (const uint8_t *data
, size_t len
)
469 crc
= (crc
>> 8) ^ crc_table
[(crc
& 0xff) ^ *data
];
478 /* Stream 2 is the type information (TPI) stream, and stream 4 is
479 the ID information (IPI) stream. They differ only in which records
480 go in which stream. */
482 populate_type_stream (bfd
*pdb
, bfd
*stream
, struct types
*types
)
484 struct pdb_tpi_stream_header h
;
485 struct type_entry
*e
;
486 uint32_t len
= 0, index_offset_len
, off
;
487 struct bfd
*hash_stream
= NULL
;
488 uint16_t hash_stream_index
;
490 static const uint32_t index_skip
= 0x2000;
494 index_offset_len
= 0;
498 uint32_t old_len
= len
;
500 len
+= sizeof (uint16_t) + bfd_getl16 (e
->data
);
502 if (old_len
== 0 || old_len
/ index_skip
!= len
/ index_skip
)
503 index_offset_len
+= sizeof (uint32_t) * 2;
508 /* Each type stream also has a stream which holds the hash value for each
509 type, along with a skip list to speed up searching. */
511 hash_stream
= add_stream (pdb
, "", &hash_stream_index
);
516 bfd_putl32 (TPI_STREAM_VERSION_80
, &h
.version
);
517 bfd_putl32 (sizeof (h
), &h
.header_size
);
518 bfd_putl32 (TPI_FIRST_INDEX
, &h
.type_index_begin
);
519 bfd_putl32 (TPI_FIRST_INDEX
+ types
->num_types
, &h
.type_index_end
);
520 bfd_putl32 (len
, &h
.type_record_bytes
);
521 bfd_putl16 (hash_stream_index
, &h
.hash_stream_index
);
522 bfd_putl16 (0xffff, &h
.hash_aux_stream_index
);
523 bfd_putl32 (sizeof (uint32_t), &h
.hash_key_size
);
524 bfd_putl32 (NUM_TPI_HASH_BUCKETS
, &h
.num_hash_buckets
);
525 bfd_putl32 (0, &h
.hash_value_buffer_offset
);
526 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
527 &h
.hash_value_buffer_length
);
528 bfd_putl32 (types
->num_types
* sizeof (uint32_t),
529 &h
.index_offset_buffer_offset
);
530 bfd_putl32 (index_offset_len
, &h
.index_offset_buffer_length
);
531 bfd_putl32 ((types
->num_types
* sizeof (uint32_t)) + index_offset_len
,
532 &h
.hash_adj_buffer_offset
);
533 bfd_putl32 (0, &h
.hash_adj_buffer_length
);
535 if (bfd_write (&h
, sizeof (h
), stream
) != sizeof (h
))
538 /* Write the type definitions into the main stream, and the hashes
539 into the hash stream. The hashes have already been calculated
546 uint8_t buf
[sizeof (uint32_t)];
549 size
= bfd_getl16 (e
->data
);
551 if (bfd_write (e
->data
, size
+ sizeof (uint16_t), stream
)
552 != size
+ sizeof (uint16_t))
555 bfd_putl32 (e
->cv_hash
% NUM_TPI_HASH_BUCKETS
, buf
);
557 if (bfd_write (buf
, sizeof (uint32_t), hash_stream
)
558 != sizeof (uint32_t))
564 /* Write the index offsets, i.e. the skip list, into the hash stream. We
565 copy MSVC here by writing a new entry for every 8192 bytes. */
572 uint32_t old_off
= off
;
573 uint16_t size
= bfd_getl16 (e
->data
);
575 off
+= size
+ sizeof (uint16_t);
577 if (old_off
== 0 || old_off
/ index_skip
!= len
/ index_skip
)
579 uint8_t buf
[sizeof (uint32_t)];
581 bfd_putl32 (TPI_FIRST_INDEX
+ e
->index
, buf
);
583 if (bfd_write (buf
, sizeof (uint32_t), hash_stream
)
584 != sizeof (uint32_t))
587 bfd_putl32 (old_off
, buf
);
589 if (bfd_write (buf
, sizeof (uint32_t), hash_stream
)
590 != sizeof (uint32_t))
600 /* Return the PE architecture number for the image. */
602 get_arch_number (bfd
*abfd
)
604 switch (abfd
->arch_info
->arch
)
607 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
608 return IMAGE_FILE_MACHINE_AMD64
;
610 return IMAGE_FILE_MACHINE_I386
;
612 case bfd_arch_aarch64
:
613 return IMAGE_FILE_MACHINE_ARM64
;
620 /* Validate the DEBUG_S_FILECHKSMS entry within a module's .debug$S
621 section, and copy it to the module's symbol stream. */
623 copy_filechksms (uint8_t *data
, uint32_t size
, char *string_table
,
624 struct string_table
*strings
, uint8_t *out
,
625 struct mod_source_files
*mod_source
)
627 uint8_t *orig_data
= data
;
628 uint32_t orig_size
= size
;
629 uint16_t num_files
= 0;
630 struct string
**strptr
;
632 bfd_putl32 (DEBUG_S_FILECHKSMS
, out
);
633 out
+= sizeof (uint32_t);
635 bfd_putl32 (size
, out
);
636 out
+= sizeof (uint32_t);
638 /* Calculate the number of files, and check for any overflows. */
642 struct file_checksum
*fc
= (struct file_checksum
*) data
;
646 if (size
< sizeof (struct file_checksum
))
648 bfd_set_error (bfd_error_bad_value
);
652 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
656 bfd_set_error (bfd_error_bad_value
);
663 if (len
% sizeof (uint32_t))
664 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
670 bfd_set_error (bfd_error_bad_value
);
680 /* Add the files to mod_source, so that they'll appear in the source
686 uint16_t new_count
= num_files
+ mod_source
->files_count
;
688 mod_source
->files
= xrealloc (mod_source
->files
,
689 sizeof (struct string
*) * new_count
);
691 strptr
= mod_source
->files
+ mod_source
->files_count
;
693 mod_source
->files_count
+= num_files
;
696 /* Actually copy the data. */
703 struct file_checksum
*fc
= (struct file_checksum
*) data
;
707 struct string
*str
= NULL
;
709 string_off
= bfd_getl32 (&fc
->file_id
);
710 len
= sizeof (struct file_checksum
) + fc
->checksum_length
;
712 if (len
% sizeof (uint32_t))
713 padding
= sizeof (uint32_t) - (len
% sizeof (uint32_t));
717 /* Remap the "file ID", i.e. the offset in the module's string table,
718 so it points to the right place in the main string table. */
722 char *fn
= string_table
+ string_off
;
723 size_t fn_len
= strlen (fn
);
724 uint32_t hash
= calc_hash (fn
, fn_len
);
727 slot
= htab_find_slot_with_hash (strings
->hashmap
, fn
, hash
,
731 str
= (struct string
*) *slot
;
737 bfd_putl32 (str
? str
->offset
: 0, &fc
->file_id
);
739 memcpy (out
, data
, len
+ padding
);
741 data
+= len
+ padding
;
742 size
-= len
+ padding
;
743 out
+= len
+ padding
;
749 /* Add a string to the strings table, if it's not already there. Returns its
750 offset within the string table. */
752 add_string (char *str
, size_t len
, struct string_table
*strings
)
754 uint32_t hash
= calc_hash (str
, len
);
758 slot
= htab_find_slot_with_hash (strings
->hashmap
, str
, hash
, INSERT
);
762 *slot
= xmalloc (offsetof (struct string
, s
) + len
);
764 s
= (struct string
*) *slot
;
768 s
->offset
= strings
->strings_len
;
769 s
->source_file_offset
= 0xffffffff;
771 memcpy (s
->s
, str
, len
);
773 if (strings
->strings_tail
)
774 strings
->strings_tail
->next
= s
;
776 strings
->strings_head
= s
;
778 strings
->strings_tail
= s
;
780 strings
->strings_len
+= len
+ 1;
784 s
= (struct string
*) *slot
;
790 /* Return the hash of an entry in the string table. */
792 hash_string_table_entry (const void *p
)
794 const struct string
*s
= (const struct string
*) p
;
799 /* Compare an entry in the string table with a string. */
801 eq_string_table_entry (const void *a
, const void *b
)
803 const struct string
*s1
= (const struct string
*) a
;
804 const char *s2
= (const char *) b
;
805 size_t s2_len
= strlen (s2
);
807 if (s2_len
!= s1
->len
)
810 return memcmp (s1
->s
, s2
, s2_len
) == 0;
813 /* Parse the string table within the .debug$S section. */
815 parse_string_table (bfd_byte
*data
, size_t size
,
816 struct string_table
*strings
)
820 size_t len
= strnlen ((char *) data
, size
);
822 add_string ((char *) data
, len
, strings
);
833 /* Remap a type reference within a CodeView symbol. */
835 remap_symbol_type (void *data
, struct type_entry
**map
, uint32_t num_types
)
837 uint32_t type
= bfd_getl32 (data
);
839 /* Ignore builtin types (those with IDs below 0x1000). */
840 if (type
< TPI_FIRST_INDEX
)
843 if (type
>= TPI_FIRST_INDEX
+ num_types
)
845 einfo (_("%P: CodeView symbol references out of range type %v\n"),
850 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
851 bfd_putl32 (type
, data
);
856 /* Add an entry into the globals stream. If it already exists, increase
859 add_globals_ref (struct globals
*glob
, bfd
*sym_rec_stream
, const char *name
,
860 size_t name_len
, uint8_t *data
, size_t len
)
866 slot
= htab_find_slot_with_hash (glob
->hashmap
, data
,
867 iterative_hash (data
, len
, 0), INSERT
);
876 *slot
= xmalloc (offsetof (struct global
, data
) + len
);
878 hash
= calc_hash (name
, name_len
);
879 hash
%= NUM_GLOBALS_HASH_BUCKETS
;
883 g
->offset
= bfd_tell (sym_rec_stream
);
886 memcpy (g
->data
, data
, len
);
891 glob
->last
->next
= g
;
897 return bfd_write (data
, len
, sym_rec_stream
) == len
;
900 /* Find the end of the current scope within symbols data. */
902 find_end_of_scope (uint8_t *data
, uint32_t size
)
904 unsigned int scope_level
= 1;
907 len
= bfd_getl16 (data
) + sizeof (uint16_t);
916 if (size
< sizeof (uint32_t))
919 len
= bfd_getl16 (data
) + sizeof (uint16_t);
920 type
= bfd_getl16 (data
+ sizeof (uint16_t));
937 case S_INLINESITE_END
:
940 if (scope_level
== 0)
951 /* Return the size of an extended value parameter, as used in
954 extended_value_len (uint16_t type
)
977 /* Parse the symbols in a .debug$S section, and copy them to the module's
980 parse_symbols (uint8_t *data
, uint32_t size
, uint8_t **buf
,
981 struct type_entry
**map
, uint32_t num_types
,
982 bfd
*sym_rec_stream
, struct globals
*glob
, uint16_t mod_num
)
984 uint8_t *orig_buf
= *buf
;
985 unsigned int scope_level
= 0;
986 uint8_t *scope
= NULL
;
988 while (size
>= sizeof (uint16_t))
992 len
= bfd_getl16 (data
) + sizeof (uint16_t);
996 bfd_set_error (bfd_error_bad_value
);
1000 type
= bfd_getl16 (data
+ sizeof (uint16_t));
1009 struct datasym
*d
= (struct datasym
*) data
;
1012 if (len
< offsetof (struct datasym
, name
))
1014 einfo (_("%P: warning: truncated CodeView record"
1015 " S_LDATA32/S_GDATA32/S_LTHREAD32/S_GTHREAD32\n"));
1016 bfd_set_error (bfd_error_bad_value
);
1020 if (scope_level
== 0)
1022 uint16_t section
= bfd_getl16 (&d
->section
);
1024 if (section
== 0) /* GC'd, ignore */
1029 strnlen (d
->name
, len
- offsetof (struct datasym
, name
));
1031 if (name_len
== len
- offsetof (struct datasym
, name
))
1033 einfo (_("%P: warning: name for S_LDATA32/S_GDATA32/"
1034 "S_LTHREAD32/S_GTHREAD32 has no terminating"
1036 bfd_set_error (bfd_error_bad_value
);
1040 if (!remap_symbol_type (&d
->type
, map
, num_types
))
1042 bfd_set_error (bfd_error_bad_value
);
1046 /* If S_LDATA32 or S_LTHREAD32, copy into module symbols. */
1048 if (type
== S_LDATA32
|| type
== S_LTHREAD32
)
1050 memcpy (*buf
, d
, len
);
1054 /* S_LDATA32 and S_LTHREAD32 only go in globals if
1055 not in function scope. */
1056 if (type
== S_GDATA32
|| type
== S_GTHREAD32
|| scope_level
== 0)
1058 if (!add_globals_ref (glob
, sym_rec_stream
, d
->name
,
1059 name_len
, data
, len
))
1071 struct procsym
*proc
= (struct procsym
*) data
;
1076 size_t ref_size
, padding
;
1079 if (len
< offsetof (struct procsym
, name
))
1081 einfo (_("%P: warning: truncated CodeView record"
1082 " S_GPROC32/S_LPROC32\n"));
1083 bfd_set_error (bfd_error_bad_value
);
1087 section
= bfd_getl16 (&proc
->section
);
1089 endptr
= find_end_of_scope (data
, size
);
1093 einfo (_("%P: warning: could not find end of"
1094 " S_GPROC32/S_LPROC32 record\n"));
1095 bfd_set_error (bfd_error_bad_value
);
1099 if (section
== 0) /* skip if GC'd */
1101 /* Skip to after S_END. */
1103 size
-= endptr
- data
;
1106 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1115 strnlen (proc
->name
, len
- offsetof (struct procsym
, name
));
1117 if (name_len
== len
- offsetof (struct procsym
, name
))
1119 einfo (_("%P: warning: name for S_GPROC32/S_LPROC32 has no"
1120 " terminating zero\n"));
1121 bfd_set_error (bfd_error_bad_value
);
1125 if (type
== S_GPROC32_ID
|| type
== S_LPROC32_ID
)
1127 /* Transform into S_GPROC32 / S_LPROC32. */
1129 uint32_t t_idx
= bfd_getl32 (&proc
->type
);
1130 struct type_entry
*t
;
1133 if (t_idx
< TPI_FIRST_INDEX
1134 || t_idx
>= TPI_FIRST_INDEX
+ num_types
)
1136 einfo (_("%P: CodeView symbol references out of range"
1137 " type %v\n"), type
);
1138 bfd_set_error (bfd_error_bad_value
);
1142 t
= map
[t_idx
- TPI_FIRST_INDEX
];
1144 t_type
= bfd_getl16 (t
->data
+ sizeof (uint16_t));
1150 struct lf_func_id
*t_data
=
1151 (struct lf_func_id
*) t
->data
;
1153 /* Replace proc->type with function type. */
1155 memcpy (&proc
->type
, &t_data
->function_type
,
1163 struct lf_mfunc_id
*t_data
=
1164 (struct lf_mfunc_id
*) t
->data
;
1166 /* Replace proc->type with function type. */
1168 memcpy (&proc
->type
, &t_data
->function_type
,
1175 einfo (_("%P: CodeView S_GPROC32_ID/S_LPROC32_ID symbol"
1176 " referenced unknown type as ID\n"));
1177 bfd_set_error (bfd_error_bad_value
);
1181 /* Change record type. */
1183 if (type
== S_GPROC32_ID
)
1184 bfd_putl32 (S_GPROC32
, &proc
->kind
);
1186 bfd_putl32 (S_LPROC32
, &proc
->kind
);
1190 if (!remap_symbol_type (&proc
->type
, map
, num_types
))
1192 bfd_set_error (bfd_error_bad_value
);
1197 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1198 bfd_putl32 (end
, &proc
->end
);
1200 /* Add S_PROCREF / S_LPROCREF to globals stream. */
1202 ref_size
= offsetof (struct refsym
, name
) + name_len
+ 1;
1204 if (ref_size
% sizeof (uint32_t))
1205 padding
= sizeof (uint32_t) - (ref_size
% sizeof (uint32_t));
1209 ref
= xmalloc (ref_size
+ padding
);
1211 bfd_putl16 (ref_size
+ padding
- sizeof (uint16_t), &ref
->size
);
1212 bfd_putl16 (type
== S_GPROC32
|| type
== S_GPROC32_ID
?
1213 S_PROCREF
: S_LPROCREF
, &ref
->kind
);
1214 bfd_putl32 (0, &ref
->sum_name
);
1215 bfd_putl32 (*buf
- orig_buf
+ sizeof (uint32_t),
1216 &ref
->symbol_offset
);
1217 bfd_putl16 (mod_num
+ 1, &ref
->mod
);
1219 memcpy (ref
->name
, proc
->name
, name_len
+ 1);
1221 memset (ref
->name
+ name_len
+ 1, 0, padding
);
1223 if (!add_globals_ref (glob
, sym_rec_stream
, proc
->name
, name_len
,
1224 (uint8_t *) ref
, ref_size
+ padding
))
1234 memcpy (*buf
, proc
, len
);
1244 struct udtsym
*udt
= (struct udtsym
*) data
;
1247 if (len
< offsetof (struct udtsym
, name
))
1249 einfo (_("%P: warning: truncated CodeView record"
1251 bfd_set_error (bfd_error_bad_value
);
1256 strnlen (udt
->name
, len
- offsetof (struct udtsym
, name
));
1258 if (name_len
== len
- offsetof (struct udtsym
, name
))
1260 einfo (_("%P: warning: name for S_UDT has no"
1261 " terminating zero\n"));
1262 bfd_set_error (bfd_error_bad_value
);
1266 if (!remap_symbol_type (&udt
->type
, map
, num_types
))
1268 bfd_set_error (bfd_error_bad_value
);
1272 /* S_UDT goes in the symbols stream if within a procedure,
1273 otherwise it goes in the globals stream. */
1274 if (scope_level
== 0)
1276 if (!add_globals_ref (glob
, sym_rec_stream
, udt
->name
,
1277 name_len
, data
, len
))
1282 memcpy (*buf
, udt
, len
);
1291 struct constsym
*c
= (struct constsym
*) data
;
1292 size_t name_len
, rec_size
;
1295 if (len
< offsetof (struct constsym
, name
))
1297 einfo (_("%P: warning: truncated CodeView record"
1299 bfd_set_error (bfd_error_bad_value
);
1303 rec_size
= offsetof (struct constsym
, name
);
1305 val
= bfd_getl16 (&c
->value
);
1307 /* If val >= 0x8000, actual value follows. */
1310 unsigned int param_len
= extended_value_len (val
);
1314 einfo (_("%P: warning: unhandled type %v within"
1315 " S_CONSTANT\n"), val
);
1316 bfd_set_error (bfd_error_bad_value
);
1320 rec_size
+= param_len
;
1324 strnlen ((const char *) data
+ rec_size
, len
- rec_size
);
1326 if (name_len
== len
- rec_size
)
1328 einfo (_("%P: warning: name for S_CONSTANT has no"
1329 " terminating zero\n"));
1330 bfd_set_error (bfd_error_bad_value
);
1334 if (!remap_symbol_type (&c
->type
, map
, num_types
))
1336 bfd_set_error (bfd_error_bad_value
);
1340 if (!add_globals_ref (glob
, sym_rec_stream
,
1341 (const char *) data
+ rec_size
, name_len
,
1349 case S_INLINESITE_END
:
1351 memcpy (*buf
, data
, len
);
1353 if (type
== S_PROC_ID_END
) /* transform to S_END */
1354 bfd_putl16 (S_END
, *buf
+ sizeof (uint16_t));
1356 /* Reset scope variable back to the address of the previous
1361 uint16_t scope_start_type
=
1362 bfd_getl16 (scope
+ sizeof (uint16_t));
1364 switch (scope_start_type
)
1368 parent
= bfd_getl32 (scope
+ offsetof (struct procsym
,
1373 parent
= bfd_getl32 (scope
+ offsetof (struct blocksym
,
1378 parent
= bfd_getl32 (scope
+ offsetof (struct inline_site
,
1383 parent
= bfd_getl32 (scope
+ offsetof (struct thunk
,
1388 einfo (_("%P: warning: unexpected CodeView scope start"
1389 " record %v\n"), scope_start_type
);
1390 bfd_set_error (bfd_error_bad_value
);
1397 scope
= orig_buf
+ parent
- sizeof (uint32_t);
1406 struct buildinfosym
*bi
= (struct buildinfosym
*) data
;
1408 if (len
< sizeof (struct buildinfosym
))
1410 einfo (_("%P: warning: truncated CodeView record"
1412 bfd_set_error (bfd_error_bad_value
);
1416 if (!remap_symbol_type (&bi
->type
, map
, num_types
))
1418 bfd_set_error (bfd_error_bad_value
);
1422 memcpy (*buf
, data
, len
);
1430 struct blocksym
*bl
= (struct blocksym
*) data
;
1434 if (len
< offsetof (struct blocksym
, name
))
1436 einfo (_("%P: warning: truncated CodeView record"
1438 bfd_set_error (bfd_error_bad_value
);
1442 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &bl
->parent
);
1444 endptr
= find_end_of_scope (data
, size
);
1448 einfo (_("%P: warning: could not find end of"
1449 " S_BLOCK32 record\n"));
1450 bfd_set_error (bfd_error_bad_value
);
1454 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1455 bfd_putl32 (end
, &bl
->end
);
1459 memcpy (*buf
, data
, len
);
1469 struct bprelsym
*bp
= (struct bprelsym
*) data
;
1471 if (len
< offsetof (struct bprelsym
, name
))
1473 einfo (_("%P: warning: truncated CodeView record"
1475 bfd_set_error (bfd_error_bad_value
);
1479 if (!remap_symbol_type (&bp
->type
, map
, num_types
))
1481 bfd_set_error (bfd_error_bad_value
);
1485 memcpy (*buf
, data
, len
);
1493 struct regsym
*reg
= (struct regsym
*) data
;
1495 if (len
< offsetof (struct regsym
, name
))
1497 einfo (_("%P: warning: truncated CodeView record"
1499 bfd_set_error (bfd_error_bad_value
);
1503 if (!remap_symbol_type (®
->type
, map
, num_types
))
1505 bfd_set_error (bfd_error_bad_value
);
1509 memcpy (*buf
, data
, len
);
1517 struct regrel
*rr
= (struct regrel
*) data
;
1519 if (len
< offsetof (struct regrel
, name
))
1521 einfo (_("%P: warning: truncated CodeView record"
1523 bfd_set_error (bfd_error_bad_value
);
1527 if (!remap_symbol_type (&rr
->type
, map
, num_types
))
1529 bfd_set_error (bfd_error_bad_value
);
1533 memcpy (*buf
, data
, len
);
1541 struct localsym
*l
= (struct localsym
*) data
;
1543 if (len
< offsetof (struct localsym
, name
))
1545 einfo (_("%P: warning: truncated CodeView record"
1547 bfd_set_error (bfd_error_bad_value
);
1551 if (!remap_symbol_type (&l
->type
, map
, num_types
))
1553 bfd_set_error (bfd_error_bad_value
);
1557 memcpy (*buf
, data
, len
);
1565 struct inline_site
*is
= (struct inline_site
*) data
;
1569 if (len
< offsetof (struct inline_site
, binary_annotations
))
1571 einfo (_("%P: warning: truncated CodeView record"
1572 " S_INLINESITE\n"));
1573 bfd_set_error (bfd_error_bad_value
);
1577 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &is
->parent
);
1579 endptr
= find_end_of_scope (data
, size
);
1583 einfo (_("%P: warning: could not find end of"
1584 " S_INLINESITE record\n"));
1585 bfd_set_error (bfd_error_bad_value
);
1589 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1590 bfd_putl32 (end
, &is
->end
);
1592 if (!remap_symbol_type (&is
->inlinee
, map
, num_types
))
1594 bfd_set_error (bfd_error_bad_value
);
1600 memcpy (*buf
, data
, len
);
1610 struct thunk
*th
= (struct thunk
*) data
;
1614 if (len
< offsetof (struct thunk
, name
))
1616 einfo (_("%P: warning: truncated CodeView record"
1618 bfd_set_error (bfd_error_bad_value
);
1622 bfd_putl32 (scope
- orig_buf
+ sizeof (uint32_t), &th
->parent
);
1624 endptr
= find_end_of_scope (data
, size
);
1628 einfo (_("%P: warning: could not find end of"
1629 " S_THUNK32 record\n"));
1630 bfd_set_error (bfd_error_bad_value
);
1634 end
= *buf
- orig_buf
+ sizeof (uint32_t) + endptr
- data
;
1635 bfd_putl32 (end
, &th
->end
);
1639 memcpy (*buf
, data
, len
);
1647 case S_HEAPALLOCSITE
:
1649 struct heap_alloc_site
*has
= (struct heap_alloc_site
*) data
;
1651 if (len
< sizeof (struct heap_alloc_site
))
1653 einfo (_("%P: warning: truncated CodeView record"
1654 " S_HEAPALLOCSITE\n"));
1655 bfd_set_error (bfd_error_bad_value
);
1659 if (!remap_symbol_type (&has
->type
, map
, num_types
))
1661 bfd_set_error (bfd_error_bad_value
);
1665 memcpy (*buf
, data
, len
);
1671 case S_OBJNAME
: /* just copy */
1677 case S_DEFRANGE_REGISTER_REL
:
1678 case S_DEFRANGE_FRAMEPOINTER_REL
:
1679 case S_DEFRANGE_SUBFIELD_REGISTER
:
1680 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1681 case S_DEFRANGE_REGISTER
:
1682 memcpy (*buf
, data
, len
);
1687 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1688 bfd_set_error (bfd_error_bad_value
);
1699 /* For a given symbol subsection, work out how much space to allocate in the
1700 result module stream. This is different because we don't copy certain
1701 symbols, such as S_CONSTANT, and we skip over any procedures or data that
1702 have been GC'd out. */
1704 calculate_symbols_size (uint8_t *data
, uint32_t size
, uint32_t *sym_size
)
1706 unsigned int scope_level
= 0;
1708 while (size
>= sizeof (uint32_t))
1710 uint16_t len
= bfd_getl16 (data
) + sizeof (uint16_t);
1711 uint16_t type
= bfd_getl16 (data
+ sizeof (uint16_t));
1718 struct datasym
*d
= (struct datasym
*) data
;
1721 if (len
< offsetof (struct datasym
, name
))
1723 einfo (_("%P: warning: truncated CodeView record"
1724 " S_LDATA32/S_LTHREAD32\n"));
1728 section
= bfd_getl16 (&d
->section
);
1730 /* copy if not GC'd or within function */
1731 if (scope_level
!= 0 || section
!= 0)
1738 /* Not copied into symbols stream. */
1746 struct procsym
*proc
= (struct procsym
*) data
;
1749 if (len
< offsetof (struct procsym
, name
))
1751 einfo (_("%P: warning: truncated CodeView record"
1752 " S_GPROC32/S_LPROC32\n"));
1756 section
= bfd_getl16 (&proc
->section
);
1764 uint8_t *endptr
= find_end_of_scope (data
, size
);
1768 einfo (_("%P: warning: could not find end of"
1769 " S_GPROC32/S_LPROC32 record\n"));
1773 /* Skip to after S_END. */
1775 size
-= endptr
- data
;
1778 len
= bfd_getl16 (data
) + sizeof (uint16_t);
1792 if (scope_level
!= 0) /* only goes in symbols if local */
1796 case S_BLOCK32
: /* always copied */
1803 case S_END
: /* always copied */
1805 case S_INLINESITE_END
:
1810 case S_OBJNAME
: /* always copied */
1821 case S_DEFRANGE_REGISTER_REL
:
1822 case S_DEFRANGE_FRAMEPOINTER_REL
:
1823 case S_DEFRANGE_SUBFIELD_REGISTER
:
1824 case S_DEFRANGE_FRAMEPOINTER_REL_FULL_SCOPE
:
1825 case S_DEFRANGE_REGISTER
:
1826 case S_HEAPALLOCSITE
:
1831 einfo (_("%P: warning: unrecognized CodeView record %v\n"), type
);
1842 /* Parse the DEBUG_S_INLINEELINES data, which records the line numbers that
1843 correspond to inlined functions. This is similar to DEBUG_S_LINES (see
1844 handle_debugs_section), but rather than just copying we also need to remap
1845 the numbers of the referenced LF_FUNC_ID types. */
1848 parse_inlinee_lines (uint8_t *data
, uint32_t size
, uint8_t **bufptr
,
1849 struct type_entry
**map
, uint32_t num_types
)
1853 unsigned int num_entries
;
1855 bfd_putl32 (DEBUG_S_INLINEELINES
, *bufptr
);
1856 *bufptr
+= sizeof (uint32_t);
1858 bfd_putl32 (size
, *bufptr
);
1859 *bufptr
+= sizeof (uint32_t);
1861 /* The inlinee lines data consists of a version uint32_t (0), followed by an
1862 array of struct inlinee_source_line:
1864 struct inlinee_source_line
1866 uint32_t function_id;
1871 (see InlineeSourceLine in cvinfo.h)
1873 We're only interested here in the function_id, as we need to remap its
1877 if (size
< sizeof (uint32_t))
1879 einfo (_("%P: warning: truncated DEBUG_S_INLINEELINES data\n"));
1883 version
= bfd_getl32 (data
+ sizeof (uint32_t) + sizeof (uint32_t));
1884 if (version
!= CV_INLINEE_SOURCE_LINE_SIGNATURE
)
1886 einfo (_("%P: warning: unexpected DEBUG_S_INLINEELINES version %u\n"),
1891 memcpy (*bufptr
, data
, size
);
1892 ptr
= *bufptr
+ sizeof (uint32_t);
1895 num_entries
= (size
- sizeof (uint32_t)) / (3 * sizeof (uint32_t));
1897 for (unsigned int i
= 0; i
< num_entries
; i
++)
1901 func_id
= bfd_getl32 (ptr
);
1903 if (!remap_type (ptr
, map
, func_id
, num_types
))
1906 ptr
+= 3 * sizeof (uint32_t);
1912 /* Parse the .debug$S section within an object file. */
1914 handle_debugs_section (asection
*s
, bfd
*mod
, struct string_table
*strings
,
1915 uint8_t **dataptr
, uint32_t *sizeptr
,
1916 struct mod_source_files
*mod_source
,
1917 bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
1918 struct type_entry
**map
, uint32_t num_types
,
1919 bfd
*sym_rec_stream
, struct globals
*glob
,
1922 bfd_byte
*data
= NULL
;
1924 uint32_t c13_size
= 0;
1925 char *string_table
= NULL
;
1926 uint8_t *buf
, *bufptr
, *symbuf
, *symbufptr
;
1927 uint32_t sym_size
= 0;
1929 if (!bfd_get_full_section_contents (mod
, s
, &data
))
1935 /* Resolve relocations. Addresses are stored within the .debug$S section as
1936 a .secidx, .secrel32 pair. */
1938 if (s
->flags
& SEC_RELOC
)
1940 struct internal_reloc
*relocs
;
1941 struct internal_syment
*symbols
;
1942 asection
**sectlist
;
1943 unsigned int syment_count
;
1945 struct external_syment
*ext
;
1947 syment_count
= obj_raw_syment_count (mod
);
1950 _bfd_coff_read_internal_relocs (mod
, s
, false, NULL
, true, NULL
);
1952 symbols
= xmalloc (sizeof (struct internal_syment
) * syment_count
);
1953 sectlist
= xmalloc (sizeof (asection
*) * syment_count
);
1955 ext
= (struct external_syment
*) (coff_data (mod
)->external_syms
);
1957 for (unsigned int i
= 0; i
< syment_count
; i
++)
1959 bfd_coff_swap_sym_in (mod
, &ext
[i
], &symbols
[i
]);
1964 for (asection
*sect
= mod
->sections
; sect
; sect
= sect
->next
)
1966 for (unsigned int i
= 0; i
< syment_count
; i
++)
1968 if (symbols
[i
].n_scnum
== sect_num
)
1975 if (!bfd_coff_relocate_section (abfd
, coff_data (abfd
)->link_info
, mod
,
1976 s
, data
, relocs
, symbols
, sectlist
))
1988 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
1994 off
= sizeof (uint32_t);
1996 /* calculate size */
1998 while (off
+ sizeof (uint32_t) <= s
->size
)
2000 uint32_t type
, size
;
2002 type
= bfd_getl32 (data
+ off
);
2004 off
+= sizeof (uint32_t);
2006 if (off
+ sizeof (uint32_t) > s
->size
)
2009 bfd_set_error (bfd_error_bad_value
);
2013 size
= bfd_getl32 (data
+ off
);
2015 off
+= sizeof (uint32_t);
2017 if (off
+ size
> s
->size
)
2020 bfd_set_error (bfd_error_bad_value
);
2026 case DEBUG_S_FILECHKSMS
:
2027 case DEBUG_S_INLINEELINES
:
2028 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
2030 if (c13_size
% sizeof (uint32_t))
2031 c13_size
+= sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
2035 case DEBUG_S_STRINGTABLE
:
2036 parse_string_table (data
+ off
, size
, strings
);
2038 string_table
= (char *) data
+ off
;
2046 if (size
< sizeof (uint32_t) + sizeof (uint16_t))
2049 bfd_set_error (bfd_error_bad_value
);
2053 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
2055 /* Skip GC'd symbols. */
2058 c13_size
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
2060 if (c13_size
% sizeof (uint32_t))
2062 sizeof (uint32_t) - (c13_size
% sizeof (uint32_t));
2068 case DEBUG_S_SYMBOLS
:
2069 if (!calculate_symbols_size (data
+ off
, size
, &sym_size
))
2072 bfd_set_error (bfd_error_bad_value
);
2081 if (off
% sizeof (uint32_t))
2082 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2085 if (sym_size
% sizeof (uint32_t))
2086 sym_size
+= sizeof (uint32_t) - (sym_size
% sizeof (uint32_t));
2088 if (c13_size
== 0 && sym_size
== 0)
2098 buf
= xmalloc (c13_size
);
2103 symbuf
= xmalloc (sym_size
);
2106 off
= sizeof (uint32_t);
2108 while (off
+ sizeof (uint32_t) <= s
->size
)
2110 uint32_t type
, size
;
2112 type
= bfd_getl32 (data
+ off
);
2113 off
+= sizeof (uint32_t);
2115 size
= bfd_getl32 (data
+ off
);
2116 off
+= sizeof (uint32_t);
2120 case DEBUG_S_FILECHKSMS
:
2121 if (!copy_filechksms (data
+ off
, size
, string_table
,
2122 strings
, bufptr
, mod_source
))
2129 bufptr
+= sizeof (uint32_t) + sizeof (uint32_t) + size
;
2137 sect
= bfd_getl16 (data
+ off
+ sizeof (uint32_t));
2142 bfd_putl32 (type
, bufptr
);
2143 bufptr
+= sizeof (uint32_t);
2145 bfd_putl32 (size
, bufptr
);
2146 bufptr
+= sizeof (uint32_t);
2148 memcpy (bufptr
, data
+ off
, size
);
2155 case DEBUG_S_SYMBOLS
:
2156 if (!parse_symbols (data
+ off
, size
, &symbufptr
, map
, num_types
,
2157 sym_rec_stream
, glob
, mod_num
))
2166 case DEBUG_S_INLINEELINES
:
2167 if (!parse_inlinee_lines (data
+ off
, size
, &bufptr
, map
, num_types
))
2179 if (off
% sizeof (uint32_t))
2180 off
+= sizeof (uint32_t) - (off
% sizeof (uint32_t));
2189 /* Append the C13 info to what's already there, if the module has
2190 multiple .debug$S sections. */
2192 *dataptr
= xrealloc (*dataptr
, *sizeptr
+ c13_size
);
2193 memcpy (*dataptr
+ *sizeptr
, buf
, c13_size
);
2202 *sizeptr
+= c13_size
;
2209 *syms
= xrealloc (*syms
, *sym_byte_size
+ sym_size
);
2210 memcpy (*syms
+ *sym_byte_size
, symbuf
, sym_size
);
2219 *sym_byte_size
+= sym_size
;
2225 /* Remap the type number stored in data from the per-module numbering to
2226 that of the deduplicated output list. */
2228 remap_type (void *data
, struct type_entry
**map
,
2229 uint32_t type_num
, uint32_t num_types
)
2231 uint32_t type
= bfd_getl32 (data
);
2233 /* Ignore builtin types (those with IDs below 0x1000). */
2234 if (type
< TPI_FIRST_INDEX
)
2237 if (type
>= TPI_FIRST_INDEX
+ type_num
)
2239 einfo (_("%P: CodeView type %v references other type %v not yet "
2240 "declared\n"), TPI_FIRST_INDEX
+ type_num
, type
);
2244 if (type
>= TPI_FIRST_INDEX
+ num_types
)
2246 einfo (_("%P: CodeView type %v references out of range type %v\n"),
2247 TPI_FIRST_INDEX
+ type_num
, type
);
2251 type
= TPI_FIRST_INDEX
+ map
[type
- TPI_FIRST_INDEX
]->index
;
2252 bfd_putl32 (type
, data
);
2257 /* Determines whether the name of a struct, class, or union counts as
2258 "anonymous". Non-anonymous types have a hash based on just the name,
2259 rather than the whole structure. */
2261 is_name_anonymous (char *name
, size_t len
)
2263 static const char tag1
[] = "<unnamed-tag>";
2264 static const char tag2
[] = "__unnamed";
2265 static const char tag3
[] = "::<unnamed-tag>";
2266 static const char tag4
[] = "::__unnamed";
2268 if (len
== sizeof (tag1
) - 1 && !memcmp (name
, tag1
, sizeof (tag1
) - 1))
2271 if (len
== sizeof (tag2
) - 1 && !memcmp (name
, tag2
, sizeof (tag2
) - 1))
2274 if (len
>= sizeof (tag3
) - 1
2275 && !memcmp (name
+ len
- sizeof (tag3
) + 1, tag3
, sizeof (tag3
) - 1))
2278 if (len
>= sizeof (tag4
) - 1
2279 && !memcmp (name
+ len
- sizeof (tag4
) + 1, tag4
, sizeof (tag4
) - 1))
2285 /* Handle LF_UDT_SRC_LINE type entries, which are a special case. These
2286 give the source file and line number for each user-defined type that is
2287 declared. We parse these and emit instead an LF_UDT_MOD_SRC_LINE entry,
2288 which also includes the module number. */
2290 handle_udt_src_line (uint8_t *data
, uint16_t size
, struct type_entry
**map
,
2291 uint32_t type_num
, uint32_t num_types
,
2292 struct types
*ids
, uint16_t mod_num
,
2293 struct string_table
*strings
)
2295 struct lf_udt_src_line
*usl
= (struct lf_udt_src_line
*) data
;
2296 uint32_t orig_type
, source_file_type
;
2299 struct type_entry
*e
, *type_e
, *str_e
;
2300 struct lf_udt_mod_src_line
*umsl
;
2301 struct lf_string_id
*str
;
2302 uint32_t source_file_offset
;
2304 if (size
< sizeof (struct lf_udt_src_line
))
2306 einfo (_("%P: warning: truncated CodeView type record"
2307 " LF_UDT_SRC_LINE\n"));
2311 /* Check if LF_UDT_MOD_SRC_LINE already present for type, and return. */
2313 orig_type
= bfd_getl32 (&usl
->type
);
2315 if (orig_type
< TPI_FIRST_INDEX
||
2316 orig_type
>= TPI_FIRST_INDEX
+ num_types
||
2317 !map
[orig_type
- TPI_FIRST_INDEX
])
2319 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2320 " referred to unknown type %v\n"), orig_type
);
2324 type_e
= map
[orig_type
- TPI_FIRST_INDEX
];
2326 /* Skip if type already declared in other module. */
2327 if (type_e
->has_udt_src_line
)
2330 if (!remap_type (&usl
->type
, map
, type_num
, num_types
))
2333 /* Extract string from source_file_type. */
2335 source_file_type
= bfd_getl32 (&usl
->source_file_type
);
2337 if (source_file_type
< TPI_FIRST_INDEX
||
2338 source_file_type
>= TPI_FIRST_INDEX
+ num_types
||
2339 !map
[source_file_type
- TPI_FIRST_INDEX
])
2341 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2342 " referred to unknown string %v\n"), source_file_type
);
2346 str_e
= map
[source_file_type
- TPI_FIRST_INDEX
];
2348 if (bfd_getl16 (str_e
->data
+ sizeof (uint16_t)) != LF_STRING_ID
)
2350 einfo (_("%P: warning: CodeView type record LF_UDT_SRC_LINE"
2351 " pointed to unexpected record type\n"));
2355 str
= (struct lf_string_id
*) str_e
->data
;
2357 /* Add string to string table. */
2359 source_file_offset
= add_string (str
->string
, strlen (str
->string
),
2362 /* Add LF_UDT_MOD_SRC_LINE entry. */
2364 size
= sizeof (struct lf_udt_mod_src_line
);
2366 e
= xmalloc (offsetof (struct type_entry
, data
) + size
);
2369 e
->index
= ids
->num_types
;
2370 e
->has_udt_src_line
= false;
2372 /* LF_UDT_MOD_SRC_LINE use calc_hash on the type number, rather than
2373 the crc32 used for type hashes elsewhere. */
2374 e
->cv_hash
= calc_hash ((char *) &usl
->type
, sizeof (uint32_t));
2376 type_e
->has_udt_src_line
= true;
2378 umsl
= (struct lf_udt_mod_src_line
*) e
->data
;
2380 bfd_putl16 (size
- sizeof (uint16_t), &umsl
->size
);
2381 bfd_putl16 (LF_UDT_MOD_SRC_LINE
, &umsl
->kind
);
2382 memcpy (&umsl
->type
, &usl
->type
, sizeof (uint32_t));
2383 bfd_putl32 (source_file_offset
, &umsl
->source_file_string
);
2384 memcpy (&umsl
->line_no
, &usl
->line_no
, sizeof (uint32_t));
2385 bfd_putl16 (mod_num
+ 1, &umsl
->module_no
);
2387 hash
= iterative_hash (e
->data
, size
, 0);
2389 slot
= htab_find_slot_with_hash (ids
->hashmap
, data
, hash
, INSERT
);
2399 einfo (_("%P: warning: duplicate CodeView type record "
2400 "LF_UDT_MOD_SRC_LINE\n"));
2407 ids
->last
->next
= e
;
2420 /* Parse a type definition in the .debug$T section. We remap the numbers
2421 of any referenced types, and if the type is not a duplicate of one
2422 already seen add it to types (for TPI types) or ids (for IPI types). */
2424 handle_type (uint8_t *data
, struct type_entry
**map
, uint32_t type_num
,
2425 uint32_t num_types
, struct types
*types
,
2426 struct types
*ids
, uint16_t mod_num
,
2427 struct string_table
*strings
)
2429 uint16_t size
, type
;
2432 bool other_hash
= false;
2437 size
= bfd_getl16 (data
) + sizeof (uint16_t);
2438 type
= bfd_getl16 (data
+ sizeof (uint16_t));
2444 struct lf_modifier
*mod
= (struct lf_modifier
*) data
;
2446 if (size
< offsetof (struct lf_modifier
, modifier
))
2448 einfo (_("%P: warning: truncated CodeView type record "
2453 if (!remap_type (&mod
->base_type
, map
, type_num
, num_types
))
2461 struct lf_pointer
*ptr
= (struct lf_pointer
*) data
;
2462 uint32_t attributes
;
2464 if (size
< offsetof (struct lf_pointer
, attributes
))
2466 einfo (_("%P: warning: truncated CodeView type record"
2471 if (!remap_type (&ptr
->base_type
, map
, type_num
, num_types
))
2474 attributes
= bfd_getl32 (&ptr
->attributes
);
2476 if ((attributes
& CV_PTR_MODE_MASK
) == CV_PTR_MODE_PMEM
2477 || (attributes
& CV_PTR_MODE_MASK
) == CV_PTR_MODE_PMFUNC
)
2479 if (size
< offsetof (struct lf_pointer
, ptr_to_mem_type
))
2481 einfo (_("%P: warning: truncated CodeView type record"
2486 if (!remap_type (&ptr
->containing_class
, map
, type_num
, num_types
))
2495 struct lf_procedure
*proc
= (struct lf_procedure
*) data
;
2497 if (size
< sizeof (struct lf_procedure
))
2499 einfo (_("%P: warning: truncated CodeView type record"
2500 " LF_PROCEDURE\n"));
2504 if (!remap_type (&proc
->return_type
, map
, type_num
, num_types
))
2507 if (!remap_type (&proc
->arglist
, map
, type_num
, num_types
))
2515 struct lf_mfunction
*func
= (struct lf_mfunction
*) data
;
2517 if (size
< sizeof (struct lf_procedure
))
2519 einfo (_("%P: warning: truncated CodeView type record"
2520 " LF_MFUNCTION\n"));
2524 if (!remap_type (&func
->return_type
, map
, type_num
, num_types
))
2527 if (!remap_type (&func
->containing_class_type
, map
, type_num
,
2531 if (!remap_type (&func
->this_type
, map
, type_num
, num_types
))
2534 if (!remap_type (&func
->arglist
, map
, type_num
, num_types
))
2542 uint32_t num_entries
;
2543 struct lf_arglist
*al
= (struct lf_arglist
*) data
;
2545 if (size
< offsetof (struct lf_arglist
, args
))
2547 einfo (_("%P: warning: truncated CodeView type record"
2552 num_entries
= bfd_getl32 (&al
->num_entries
);
2554 if (size
< offsetof (struct lf_arglist
, args
)
2555 + (num_entries
* sizeof (uint32_t)))
2557 einfo (_("%P: warning: truncated CodeView type record"
2562 for (uint32_t i
= 0; i
< num_entries
; i
++)
2564 if (!remap_type (&al
->args
[i
], map
, type_num
, num_types
))
2573 uint16_t left
= size
- sizeof (uint16_t) - sizeof (uint16_t);
2574 uint8_t *ptr
= data
+ sizeof (uint16_t) + sizeof (uint16_t);
2580 if (left
< sizeof (uint16_t))
2582 einfo (_("%P: warning: truncated CodeView type record"
2583 " LF_FIELDLIST\n"));
2587 subtype
= bfd_getl16 (ptr
);
2593 struct lf_member
*mem
= (struct lf_member
*) ptr
;
2595 size_t name_len
, subtype_len
;
2597 if (left
< offsetof (struct lf_member
, name
))
2599 einfo (_("%P: warning: truncated CodeView type record"
2604 if (!remap_type (&mem
->type
, map
, type_num
, num_types
))
2607 subtype_len
= offsetof (struct lf_member
, name
);
2609 offset
= bfd_getl16 (&mem
->offset
);
2611 /* If offset >= 0x8000, actual value follows. */
2612 if (offset
>= 0x8000)
2614 unsigned int param_len
= extended_value_len (offset
);
2618 einfo (_("%P: warning: unhandled type %v within"
2619 " LF_MEMBER\n"), offset
);
2623 subtype_len
+= param_len
;
2625 if (left
< subtype_len
)
2627 einfo (_("%P: warning: truncated CodeView type record"
2634 strnlen ((char *) mem
+ subtype_len
, left
- subtype_len
);
2636 if (name_len
== left
- offsetof (struct lf_member
, name
))
2638 einfo (_("%P: warning: name for LF_MEMBER has no"
2639 " terminating zero\n"));
2645 subtype_len
+= name_len
;
2647 if (subtype_len
% 4 != 0)
2648 subtype_len
+= 4 - (subtype_len
% 4);
2650 if (left
< subtype_len
)
2652 einfo (_("%P: warning: truncated CodeView type record"
2653 " LF_FIELDLIST\n"));
2658 left
-= subtype_len
;
2665 struct lf_enumerate
*en
= (struct lf_enumerate
*) ptr
;
2666 size_t name_len
, subtype_len
;
2669 if (left
< offsetof (struct lf_enumerate
, name
))
2671 einfo (_("%P: warning: truncated CodeView type record"
2672 " LF_ENUMERATE\n"));
2676 subtype_len
= offsetof (struct lf_enumerate
, name
);
2678 val
= bfd_getl16 (&en
->value
);
2680 /* If val >= 0x8000, the actual value immediately follows. */
2683 unsigned int param_len
= extended_value_len (val
);
2687 einfo (_("%P: warning: unhandled type %v within"
2688 " LF_ENUMERATE\n"), val
);
2692 if (left
< subtype_len
+ param_len
)
2694 einfo (_("%P: warning: truncated CodeView type"
2695 " record LF_ENUMERATE\n"));
2699 subtype_len
+= param_len
;
2702 name_len
= strnlen ((char *) ptr
+ subtype_len
,
2703 left
- subtype_len
);
2705 if (name_len
== left
- offsetof (struct lf_enumerate
, name
))
2707 einfo (_("%P: warning: name for LF_ENUMERATE has no"
2708 " terminating zero\n"));
2714 subtype_len
+= name_len
;
2716 if (subtype_len
% 4 != 0)
2717 subtype_len
+= 4 - (subtype_len
% 4);
2719 if (left
< subtype_len
)
2721 einfo (_("%P: warning: truncated CodeView type record"
2722 " LF_ENUMERATE\n"));
2727 left
-= subtype_len
;
2734 struct lf_index
*ind
= (struct lf_index
*) ptr
;
2736 if (left
< sizeof (struct lf_index
))
2738 einfo (_("%P: warning: truncated CodeView type record"
2743 if (!remap_type (&ind
->index
, map
, type_num
, num_types
))
2746 ptr
+= sizeof (struct lf_index
);
2747 left
-= sizeof (struct lf_index
);
2754 struct lf_onemethod
*meth
= (struct lf_onemethod
*) ptr
;
2755 size_t name_len
, subtype_len
;
2757 if (left
< offsetof (struct lf_onemethod
, name
))
2759 einfo (_("%P: warning: truncated CodeView type record"
2760 " LF_ONEMETHOD\n"));
2764 if (!remap_type (&meth
->method_type
, map
, type_num
,
2769 strnlen (meth
->name
,
2770 left
- offsetof (struct lf_onemethod
, name
));
2772 if (name_len
== left
- offsetof (struct lf_onemethod
, name
))
2774 einfo (_("%P: warning: name for LF_ONEMETHOD has no"
2775 " terminating zero\n"));
2781 subtype_len
= offsetof (struct lf_onemethod
, name
)
2784 if (subtype_len
% 4 != 0)
2785 subtype_len
+= 4 - (subtype_len
% 4);
2787 if (left
< subtype_len
)
2789 einfo (_("%P: warning: truncated CodeView type record"
2790 " LF_FIELDLIST\n"));
2795 left
-= subtype_len
;
2802 struct lf_method
*meth
= (struct lf_method
*) ptr
;
2803 size_t name_len
, subtype_len
;
2805 if (left
< offsetof (struct lf_method
, name
))
2807 einfo (_("%P: warning: truncated CodeView type record"
2812 if (!remap_type (&meth
->method_list
, map
, type_num
,
2817 strnlen (meth
->name
,
2818 left
- offsetof (struct lf_method
, name
));
2820 if (name_len
== left
- offsetof (struct lf_method
, name
))
2822 einfo (_("%P: warning: name for LF_METHOD has no"
2823 " terminating zero\n"));
2829 subtype_len
= offsetof (struct lf_method
, name
) + name_len
;
2831 if (subtype_len
% 4 != 0)
2832 subtype_len
+= 4 - (subtype_len
% 4);
2834 if (left
< subtype_len
)
2836 einfo (_("%P: warning: truncated CodeView type record"
2837 " LF_FIELDLIST\n"));
2842 left
-= subtype_len
;
2849 struct lf_bclass
*bc
= (struct lf_bclass
*) ptr
;
2853 if (left
< sizeof (struct lf_bclass
))
2855 einfo (_("%P: warning: truncated CodeView type record"
2860 if (!remap_type (&bc
->base_class_type
, map
, type_num
,
2864 subtype_len
= sizeof (struct lf_bclass
);
2866 offset
= bfd_getl16 (&bc
->offset
);
2868 /* If offset >= 0x8000, actual value follows. */
2869 if (offset
>= 0x8000)
2871 unsigned int param_len
= extended_value_len (offset
);
2875 einfo (_("%P: warning: unhandled type %v within"
2876 " LF_BCLASS\n"), offset
);
2880 subtype_len
+= param_len
;
2882 if (left
< subtype_len
)
2884 einfo (_("%P: warning: truncated CodeView type record"
2890 if (subtype_len
% 4 != 0)
2891 subtype_len
+= 4 - (subtype_len
% 4);
2893 if (left
< subtype_len
)
2895 einfo (_("%P: warning: truncated CodeView type record"
2901 left
-= subtype_len
;
2908 struct lf_vfunctab
*vft
= (struct lf_vfunctab
*) ptr
;
2910 if (left
< sizeof (struct lf_vfunctab
))
2912 einfo (_("%P: warning: truncated CodeView type record"
2917 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
2920 ptr
+= sizeof (struct lf_vfunctab
);
2921 left
-= sizeof (struct lf_vfunctab
);
2929 struct lf_vbclass
*vbc
= (struct lf_vbclass
*) ptr
;
2933 if (left
< sizeof (struct lf_vbclass
))
2935 einfo (_("%P: warning: truncated CodeView type record"
2936 " LF_VBCLASS/LF_IVBCLASS\n"));
2940 if (!remap_type (&vbc
->base_class_type
, map
, type_num
,
2944 if (!remap_type (&vbc
->virtual_base_pointer_type
, map
,
2945 type_num
, num_types
))
2948 subtype_len
= offsetof (struct lf_vbclass
,
2949 virtual_base_vbtable_offset
);
2951 offset
= bfd_getl16 (&vbc
->virtual_base_pointer_offset
);
2953 /* If offset >= 0x8000, actual value follows. */
2954 if (offset
>= 0x8000)
2956 unsigned int param_len
= extended_value_len (offset
);
2960 einfo (_("%P: warning: unhandled type %v within"
2961 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2965 subtype_len
+= param_len
;
2967 if (left
< subtype_len
)
2969 einfo (_("%P: warning: truncated CodeView type record"
2970 " LF_VBCLASS/LF_IVBCLASS\n"));
2975 offset
= bfd_getl16 ((char *)vbc
+ subtype_len
);
2976 subtype_len
+= sizeof (uint16_t);
2978 /* If offset >= 0x8000, actual value follows. */
2979 if (offset
>= 0x8000)
2981 unsigned int param_len
= extended_value_len (offset
);
2985 einfo (_("%P: warning: unhandled type %v within"
2986 " LF_VBCLASS/LF_IVBCLASS\n"), offset
);
2990 subtype_len
+= param_len
;
2992 if (left
< subtype_len
)
2994 einfo (_("%P: warning: truncated CodeView type record"
2995 " LF_VBCLASS/LF_IVBCLASS\n"));
3000 if (subtype_len
% 4 != 0)
3001 subtype_len
+= 4 - (subtype_len
% 4);
3003 if (left
< subtype_len
)
3005 einfo (_("%P: warning: truncated CodeView type record"
3006 " LF_VBCLASS/LF_IVBCLASS\n"));
3011 left
-= subtype_len
;
3018 struct lf_static_member
*st
=
3019 (struct lf_static_member
*) ptr
;
3020 size_t name_len
, subtype_len
;
3022 if (left
< offsetof (struct lf_static_member
, name
))
3024 einfo (_("%P: warning: truncated CodeView type record"
3029 if (!remap_type (&st
->type
, map
, type_num
, num_types
))
3034 left
- offsetof (struct lf_static_member
, name
));
3036 if (name_len
== left
3037 - offsetof (struct lf_static_member
, name
))
3039 einfo (_("%P: warning: name for LF_STMEMBER has no"
3040 " terminating zero\n"));
3046 subtype_len
= offsetof (struct lf_static_member
, name
)
3049 if (subtype_len
% 4 != 0)
3050 subtype_len
+= 4 - (subtype_len
% 4);
3052 if (left
< subtype_len
)
3054 einfo (_("%P: warning: truncated CodeView type record"
3055 " LF_FIELDLIST\n"));
3060 left
-= subtype_len
;
3067 struct lf_nest_type
*nest
= (struct lf_nest_type
*) ptr
;
3068 size_t name_len
, subtype_len
;
3070 if (left
< offsetof (struct lf_nest_type
, name
))
3072 einfo (_("%P: warning: truncated CodeView type record"
3077 if (!remap_type (&nest
->type
, map
, type_num
, num_types
))
3081 strnlen (nest
->name
,
3082 left
- offsetof (struct lf_nest_type
, name
));
3084 if (name_len
== left
- offsetof (struct lf_nest_type
, name
))
3086 einfo (_("%P: warning: name for LF_NESTTYPE has no"
3087 " terminating zero\n"));
3093 subtype_len
= offsetof (struct lf_nest_type
, name
)
3096 if (subtype_len
% 4 != 0)
3097 subtype_len
+= 4 - (subtype_len
% 4);
3099 if (left
< subtype_len
)
3101 einfo (_("%P: warning: truncated CodeView type record"
3102 " LF_FIELDLIST\n"));
3107 left
-= subtype_len
;
3113 einfo (_("%P: warning: unrecognized CodeView subtype %v\n"),
3124 struct lf_bitfield
*bf
= (struct lf_bitfield
*) data
;
3126 if (size
< offsetof (struct lf_bitfield
, length
))
3128 einfo (_("%P: warning: truncated CodeView type record"
3133 if (!remap_type (&bf
->base_type
, map
, type_num
, num_types
))
3141 struct lf_methodlist
*ml
= (struct lf_methodlist
*) data
;
3142 unsigned int num_entries
;
3144 if (size
< offsetof (struct lf_methodlist
, entries
))
3146 einfo (_("%P: warning: truncated CodeView type record"
3147 " LF_METHODLIST\n"));
3151 if ((size
- offsetof (struct lf_methodlist
, entries
))
3152 % sizeof (struct lf_methodlist_entry
))
3154 einfo (_("%P: warning: malformed CodeView type record"
3155 " LF_METHODLIST\n"));
3159 num_entries
= (size
- offsetof (struct lf_methodlist
, entries
))
3160 / sizeof (struct lf_methodlist_entry
);
3162 for (unsigned int i
= 0; i
< num_entries
; i
++)
3164 if (!remap_type (&ml
->entries
[i
].method_type
, map
,
3165 type_num
, num_types
))
3174 struct lf_array
*arr
= (struct lf_array
*) data
;
3176 if (size
< offsetof (struct lf_array
, length_in_bytes
))
3178 einfo (_("%P: warning: truncated CodeView type record"
3183 if (!remap_type (&arr
->element_type
, map
, type_num
, num_types
))
3186 if (!remap_type (&arr
->index_type
, map
, type_num
, num_types
))
3195 struct lf_class
*cl
= (struct lf_class
*) data
;
3196 uint16_t prop
, num_bytes
;
3197 size_t name_len
, name_off
;
3199 if (size
< offsetof (struct lf_class
, name
))
3201 einfo (_("%P: warning: truncated CodeView type record"
3202 " LF_CLASS/LF_STRUCTURE\n"));
3206 if (!remap_type (&cl
->field_list
, map
, type_num
, num_types
))
3209 if (!remap_type (&cl
->derived_from
, map
, type_num
, num_types
))
3212 if (!remap_type (&cl
->vshape
, map
, type_num
, num_types
))
3215 name_off
= offsetof (struct lf_class
, name
);
3217 num_bytes
= bfd_getl16 (&cl
->length
);
3219 /* If num_bytes >= 0x8000, actual value follows. */
3220 if (num_bytes
>= 0x8000)
3222 unsigned int param_len
= extended_value_len (num_bytes
);
3226 einfo (_("%P: warning: unhandled type %v within"
3227 " LF_CLASS/LF_STRUCTURE\n"), num_bytes
);
3231 name_off
+= param_len
;
3233 if (size
< name_off
)
3235 einfo (_("%P: warning: truncated CodeView type record"
3236 " LF_CLASS/LF_STRUCTURE\n"));
3241 name_len
= strnlen ((char *) cl
+ name_off
, size
- name_off
);
3243 if (name_len
== size
- name_off
)
3245 einfo (_("%P: warning: name for LF_CLASS/LF_STRUCTURE has no"
3246 " terminating zero\n"));
3250 prop
= bfd_getl16 (&cl
->properties
);
3252 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3254 /* Structure has another name following first one. */
3256 size_t len
= name_off
+ name_len
+ 1;
3257 size_t unique_name_len
;
3259 unique_name_len
= strnlen ((char *) cl
+ name_off
+ name_len
+ 1,
3262 if (unique_name_len
== size
- len
)
3264 einfo (_("%P: warning: unique name for LF_CLASS/LF_STRUCTURE"
3265 " has no terminating zero\n"));
3270 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3271 && !is_name_anonymous ((char *) cl
+ name_off
, name_len
))
3274 cv_hash
= crc32 ((uint8_t *) cl
+ name_off
, name_len
);
3282 struct lf_union
*un
= (struct lf_union
*) data
;
3283 uint16_t prop
, num_bytes
;
3284 size_t name_len
, name_off
;
3286 if (size
< offsetof (struct lf_union
, name
))
3288 einfo (_("%P: warning: truncated CodeView type record"
3293 if (!remap_type (&un
->field_list
, map
, type_num
, num_types
))
3296 name_off
= offsetof (struct lf_union
, name
);
3298 num_bytes
= bfd_getl16 (&un
->length
);
3300 /* If num_bytes >= 0x8000, actual value follows. */
3301 if (num_bytes
>= 0x8000)
3303 unsigned int param_len
= extended_value_len (num_bytes
);
3307 einfo (_("%P: warning: unhandled type %v within"
3308 " LF_UNION\n"), num_bytes
);
3312 name_off
+= param_len
;
3314 if (size
< name_off
)
3316 einfo (_("%P: warning: truncated CodeView type record"
3322 name_len
= strnlen ((char *) un
+ name_off
, size
- name_off
);
3324 if (name_len
== size
- name_off
)
3326 einfo (_("%P: warning: name for LF_UNION has no"
3327 " terminating zero\n"));
3331 prop
= bfd_getl16 (&un
->properties
);
3333 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3335 /* Structure has another name following first one. */
3337 size_t len
= name_off
+ name_len
+ 1;
3338 size_t unique_name_len
;
3340 unique_name_len
= strnlen ((char *) un
+ name_off
+ name_len
+ 1,
3343 if (unique_name_len
== size
- len
)
3345 einfo (_("%P: warning: unique name for LF_UNION has"
3346 " no terminating zero\n"));
3351 if (!(prop
& (CV_PROP_FORWARD_REF
| CV_PROP_SCOPED
))
3352 && !is_name_anonymous ((char *) un
+ name_off
, name_len
))
3355 cv_hash
= crc32 ((uint8_t *) un
+ name_off
, name_len
);
3363 struct lf_enum
*en
= (struct lf_enum
*) data
;
3367 if (size
< offsetof (struct lf_enum
, name
))
3369 einfo (_("%P: warning: truncated CodeView type record"
3374 if (!remap_type (&en
->underlying_type
, map
, type_num
, num_types
))
3377 if (!remap_type (&en
->field_list
, map
, type_num
, num_types
))
3380 name_len
= strnlen (en
->name
, size
- offsetof (struct lf_enum
, name
));
3382 if (name_len
== size
- offsetof (struct lf_enum
, name
))
3384 einfo (_("%P: warning: name for LF_ENUM has no"
3385 " terminating zero\n"));
3389 prop
= bfd_getl16 (&en
->properties
);
3391 if (prop
& CV_PROP_HAS_UNIQUE_NAME
)
3393 /* Structure has another name following first one. */
3395 size_t len
= offsetof (struct lf_enum
, name
) + name_len
+ 1;
3396 size_t unique_name_len
;
3398 unique_name_len
= strnlen (en
->name
+ name_len
+ 1, size
- len
);
3400 if (unique_name_len
== size
- len
)
3402 einfo (_("%P: warning: unique name for LF_ENUM has"
3403 " no terminating zero\n"));
3412 /* Does not reference any types, nothing to be done. */
3417 struct lf_vftable
*vft
= (struct lf_vftable
*) data
;
3419 if (size
< offsetof (struct lf_vftable
, names
))
3421 einfo (_("%P: warning: truncated CodeView type record"
3426 if (!remap_type (&vft
->type
, map
, type_num
, num_types
))
3429 if (!remap_type (&vft
->base_vftable
, map
, type_num
, num_types
))
3437 struct lf_string_id
*str
= (struct lf_string_id
*) data
;
3440 if (size
< offsetof (struct lf_string_id
, string
))
3442 einfo (_("%P: warning: truncated CodeView type record"
3443 " LF_STRING_ID\n"));
3447 if (!remap_type (&str
->substring
, map
, type_num
, num_types
))
3450 string_len
= strnlen (str
->string
,
3451 size
- offsetof (struct lf_string_id
, string
));
3453 if (string_len
== size
- offsetof (struct lf_string_id
, string
))
3455 einfo (_("%P: warning: string for LF_STRING_ID has no"
3456 " terminating zero\n"));
3465 case LF_SUBSTR_LIST
:
3467 uint32_t num_entries
;
3468 struct lf_arglist
*ssl
= (struct lf_arglist
*) data
;
3470 if (size
< offsetof (struct lf_arglist
, args
))
3472 einfo (_("%P: warning: truncated CodeView type record"
3473 " LF_SUBSTR_LIST\n"));
3477 num_entries
= bfd_getl32 (&ssl
->num_entries
);
3479 if (size
< offsetof (struct lf_arglist
, args
)
3480 + (num_entries
* sizeof (uint32_t)))
3482 einfo (_("%P: warning: truncated CodeView type record"
3483 " LF_SUBSTR_LIST\n"));
3487 for (uint32_t i
= 0; i
< num_entries
; i
++)
3489 if (!remap_type (&ssl
->args
[i
], map
, type_num
, num_types
))
3500 uint16_t num_entries
;
3501 struct lf_build_info
*bi
= (struct lf_build_info
*) data
;
3503 if (size
< offsetof (struct lf_build_info
, strings
))
3505 einfo (_("%P: warning: truncated CodeView type record"
3506 " LF_BUILDINFO\n"));
3510 num_entries
= bfd_getl16 (&bi
->count
);
3512 if (size
< offsetof (struct lf_build_info
, strings
)
3513 + (num_entries
* sizeof (uint32_t)))
3515 einfo (_("%P: warning: truncated CodeView type record"
3516 " LF_BUILDINFO\n"));
3520 for (uint32_t i
= 0; i
< num_entries
; i
++)
3522 if (!remap_type (&bi
->strings
[i
], map
, type_num
, num_types
))
3533 struct lf_func_id
*func
= (struct lf_func_id
*) data
;
3536 if (size
< offsetof (struct lf_func_id
, name
))
3538 einfo (_("%P: warning: truncated CodeView type record"
3543 if (!remap_type (&func
->parent_scope
, map
, type_num
, num_types
))
3546 if (!remap_type (&func
->function_type
, map
, type_num
, num_types
))
3549 name_len
= strnlen (func
->name
,
3550 size
- offsetof (struct lf_func_id
, name
));
3552 if (name_len
== size
- offsetof (struct lf_func_id
, name
))
3554 einfo (_("%P: warning: string for LF_FUNC_ID has no"
3555 " terminating zero\n"));
3566 struct lf_mfunc_id
*mfunc
= (struct lf_mfunc_id
*) data
;
3569 if (size
< offsetof (struct lf_mfunc_id
, name
))
3571 einfo (_("%P: warning: truncated CodeView type record"
3576 if (!remap_type (&mfunc
->parent_type
, map
, type_num
, num_types
))
3579 if (!remap_type (&mfunc
->function_type
, map
, type_num
, num_types
))
3582 name_len
= strnlen (mfunc
->name
,
3583 size
- offsetof (struct lf_mfunc_id
, name
));
3585 if (name_len
== size
- offsetof (struct lf_mfunc_id
, name
))
3587 einfo (_("%P: warning: string for LF_MFUNC_ID has no"
3588 " terminating zero\n"));
3597 case LF_UDT_SRC_LINE
:
3598 return handle_udt_src_line (data
, size
, map
, type_num
, num_types
,
3599 ids
, mod_num
, strings
);
3602 einfo (_("%P: warning: unrecognized CodeView type %v\n"), type
);
3606 hash
= iterative_hash (data
, size
, 0);
3608 t
= ipi
? ids
: types
;
3610 slot
= htab_find_slot_with_hash (t
->hashmap
, data
, hash
, INSERT
);
3614 if (!*slot
) /* new entry */
3616 struct type_entry
*e
;
3618 *slot
= xmalloc (offsetof (struct type_entry
, data
) + size
);
3620 e
= (struct type_entry
*) *slot
;
3623 e
->index
= t
->num_types
;
3626 e
->cv_hash
= cv_hash
;
3628 e
->cv_hash
= crc32 (data
, size
);
3630 e
->has_udt_src_line
= false;
3632 memcpy (e
->data
, data
, size
);
3645 else /* duplicate */
3647 map
[type_num
] = (struct type_entry
*) *slot
;
3653 /* Parse the .debug$T section of a module, and pass any type definitions
3654 found to handle_type. */
3656 handle_debugt_section (asection
*s
, bfd
*mod
, struct types
*types
,
3657 struct types
*ids
, uint16_t mod_num
,
3658 struct string_table
*strings
,
3659 struct type_entry
***map
, uint32_t *num_types
)
3661 bfd_byte
*data
= NULL
;
3665 if (!bfd_get_full_section_contents (mod
, s
, &data
))
3671 if (bfd_getl32 (data
) != CV_SIGNATURE_C13
)
3677 off
= sizeof (uint32_t);
3679 while (off
+ sizeof (uint16_t) <= s
->size
)
3683 size
= bfd_getl16 (data
+ off
);
3684 off
+= sizeof (uint16_t);
3686 if (size
+ off
> s
->size
|| size
< sizeof (uint16_t))
3689 bfd_set_error (bfd_error_bad_value
);
3697 if (*num_types
== 0)
3703 *map
= xcalloc (*num_types
, sizeof (struct type_entry
*));
3705 off
= sizeof (uint32_t);
3708 while (off
+ sizeof (uint16_t) <= s
->size
)
3712 size
= bfd_getl16 (data
+ off
);
3714 if (!handle_type (data
+ off
, *map
, type_num
, *num_types
, types
, ids
,
3719 bfd_set_error (bfd_error_bad_value
);
3723 off
+= sizeof (uint16_t) + size
;
3732 /* Return the CodeView constant for the selected architecture. */
3734 target_processor (bfd
*abfd
)
3736 switch (abfd
->arch_info
->arch
)
3739 if (abfd
->arch_info
->mach
& bfd_mach_x86_64
)
3742 return CV_CFL_80386
;
3744 case bfd_arch_aarch64
:
3745 return CV_CFL_ARM64
;
3752 /* Create the symbols that go in "* Linker *", the dummy module created
3753 for the linker itself. */
3755 create_linker_symbols (bfd
*abfd
, uint8_t **syms
, uint32_t *sym_byte_size
,
3756 const char *pdb_name
)
3759 struct objname
*name
;
3760 struct compile3
*comp
;
3761 struct envblock
*env
;
3762 size_t padding1
, padding2
, env_size
;
3763 char *cwdval
, *exeval
, *pdbval
;
3765 /* extra NUL for padding */
3766 static const char linker_fn
[] = "* Linker *\0";
3767 static const char linker_name
[] = "GNU LD " VERSION
;
3769 static const char cwd
[] = "cwd";
3770 static const char exe
[] = "exe";
3771 static const char pdb
[] = "pdb";
3773 cwdval
= getcwd (NULL
, 0);
3776 einfo (_("%P: warning: unable to get working directory\n"));
3780 exeval
= lrealpath (program_name
);
3784 einfo (_("%P: warning: unable to get program name\n"));
3789 pdbval
= lrealpath (pdb_name
);
3793 einfo (_("%P: warning: unable to get full path to PDB\n"));
3799 *sym_byte_size
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3800 *sym_byte_size
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
);
3802 if (*sym_byte_size
% 4)
3803 padding1
= 4 - (*sym_byte_size
% 4);
3807 *sym_byte_size
+= padding1
;
3809 env_size
= offsetof (struct envblock
, strings
);
3810 env_size
+= sizeof (cwd
);
3811 env_size
+= strlen (cwdval
) + 1;
3812 env_size
+= sizeof (exe
);
3813 env_size
+= strlen (exeval
) + 1;
3814 env_size
+= sizeof (pdb
);
3815 env_size
+= strlen (pdbval
) + 1;
3818 padding2
= 4 - (env_size
% 4);
3822 env_size
+= padding2
;
3824 *sym_byte_size
+= env_size
;
3826 *syms
= xmalloc (*sym_byte_size
);
3829 /* Write S_OBJNAME */
3831 name
= (struct objname
*) ptr
;
3832 bfd_putl16 (offsetof (struct objname
, name
)
3833 + sizeof (linker_fn
) - sizeof (uint16_t), &name
->size
);
3834 bfd_putl16 (S_OBJNAME
, &name
->kind
);
3835 bfd_putl32 (0, &name
->signature
);
3836 memcpy (name
->name
, linker_fn
, sizeof (linker_fn
));
3838 ptr
+= offsetof (struct objname
, name
) + sizeof (linker_fn
);
3840 /* Write S_COMPILE3 */
3842 comp
= (struct compile3
*) ptr
;
3844 bfd_putl16 (offsetof (struct compile3
, compiler
) + sizeof (linker_name
)
3845 + padding1
- sizeof (uint16_t), &comp
->size
);
3846 bfd_putl16 (S_COMPILE3
, &comp
->kind
);
3847 bfd_putl32 (CV_CFL_LINK
, &comp
->flags
);
3848 bfd_putl16 (target_processor (abfd
), &comp
->machine
);
3849 bfd_putl16 (0, &comp
->frontend_major
);
3850 bfd_putl16 (0, &comp
->frontend_minor
);
3851 bfd_putl16 (0, &comp
->frontend_build
);
3852 bfd_putl16 (0, &comp
->frontend_qfe
);
3853 bfd_putl16 (0, &comp
->backend_major
);
3854 bfd_putl16 (0, &comp
->backend_minor
);
3855 bfd_putl16 (0, &comp
->backend_build
);
3856 bfd_putl16 (0, &comp
->backend_qfe
);
3857 memcpy (comp
->compiler
, linker_name
, sizeof (linker_name
));
3859 memset (comp
->compiler
+ sizeof (linker_name
), 0, padding1
);
3861 ptr
+= offsetof (struct compile3
, compiler
) + sizeof (linker_name
) + padding1
;
3863 /* Write S_ENVBLOCK */
3865 env
= (struct envblock
*) ptr
;
3867 bfd_putl16 (env_size
- sizeof (uint16_t), &env
->size
);
3868 bfd_putl16 (S_ENVBLOCK
, &env
->kind
);
3871 ptr
+= offsetof (struct envblock
, strings
);
3873 memcpy (ptr
, cwd
, sizeof (cwd
));
3874 ptr
+= sizeof (cwd
);
3875 memcpy (ptr
, cwdval
, strlen (cwdval
) + 1);
3876 ptr
+= strlen (cwdval
) + 1;
3878 memcpy (ptr
, exe
, sizeof (exe
));
3879 ptr
+= sizeof (exe
);
3880 memcpy (ptr
, exeval
, strlen (exeval
) + 1);
3881 ptr
+= strlen (exeval
) + 1;
3883 memcpy (ptr
, pdb
, sizeof (pdb
));
3884 ptr
+= sizeof (pdb
);
3885 memcpy (ptr
, pdbval
, strlen (pdbval
) + 1);
3886 ptr
+= strlen (pdbval
) + 1;
3888 /* Microsoft's LINK also includes "cmd", the command-line options passed
3889 to the linker, but unfortunately we don't have access to argc and argv
3892 memset (ptr
, 0, padding2
);
3901 /* Populate the module stream, which consists of the transformed .debug$S
3902 data for each object file. */
3904 populate_module_stream (bfd
*stream
, bfd
*mod
, uint32_t *sym_byte_size
,
3905 struct string_table
*strings
,
3906 uint32_t *c13_info_size
,
3907 struct mod_source_files
*mod_source
,
3908 bfd
*abfd
, struct types
*types
,
3909 struct types
*ids
, uint16_t mod_num
,
3910 bfd
*sym_rec_stream
, struct globals
*glob
,
3911 const char *pdb_name
)
3913 uint8_t int_buf
[sizeof (uint32_t)];
3914 uint8_t *c13_info
= NULL
;
3915 uint8_t *syms
= NULL
;
3920 if (!strcmp (bfd_get_filename (mod
), "dll stuff"))
3922 if (!create_linker_symbols (mod
, &syms
, sym_byte_size
, pdb_name
))
3927 struct type_entry
**map
= NULL
;
3928 uint32_t num_types
= 0;
3930 /* Process .debug$T section. */
3932 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3934 if (!strcmp (s
->name
, ".debug$T") && s
->size
>= sizeof (uint32_t))
3936 if (!handle_debugt_section (s
, mod
, types
, ids
, mod_num
, strings
,
3939 free (mod_source
->files
);
3947 /* Process .debug$S section(s). */
3949 for (asection
*s
= mod
->sections
; s
; s
= s
->next
)
3951 if (!strcmp (s
->name
, ".debug$S") && s
->size
>= sizeof (uint32_t))
3953 if (!handle_debugs_section (s
, mod
, strings
, &c13_info
,
3954 c13_info_size
, mod_source
, abfd
,
3955 &syms
, sym_byte_size
, map
, num_types
,
3956 sym_rec_stream
, glob
, mod_num
))
3960 free (mod_source
->files
);
3970 /* Write the signature. */
3972 bfd_putl32 (CV_SIGNATURE_C13
, int_buf
);
3974 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
3983 if (bfd_write (syms
, *sym_byte_size
, stream
) != *sym_byte_size
)
3995 if (bfd_write (c13_info
, *c13_info_size
, stream
) != *c13_info_size
)
4004 /* Write the global refs size. */
4006 bfd_putl32 (0, int_buf
);
4008 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4014 /* Create the module info substream within the DBI. */
4016 create_module_info_substream (bfd
*abfd
, bfd
*pdb
, void **data
,
4017 uint32_t *size
, struct string_table
*strings
,
4018 struct source_files_info
*source
,
4019 struct types
*types
, struct types
*ids
,
4020 bfd
*sym_rec_stream
, struct globals
*glob
,
4021 const char *pdb_name
)
4024 unsigned int mod_num
;
4026 static const char linker_fn
[] = "* Linker *";
4030 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4033 size_t len
= sizeof (struct module_info
);
4035 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
4037 len
+= sizeof (linker_fn
); /* Object name. */
4038 len
++; /* Empty module name. */
4040 else if (in
->my_archive
)
4042 char *name
= lrealpath (bfd_get_filename (in
));
4044 len
+= strlen (name
) + 1; /* Object name. */
4048 name
= lrealpath (bfd_get_filename (in
->my_archive
));
4050 len
+= strlen (name
) + 1; /* Archive name. */
4056 char *name
= lrealpath (bfd_get_filename (in
));
4057 size_t name_len
= strlen (name
) + 1;
4059 len
+= name_len
; /* Object name. */
4060 len
+= name_len
; /* And again as the archive name. */
4066 len
+= 4 - (len
% 4);
4070 source
->mod_count
++;
4073 *data
= xmalloc (*size
);
4077 source
->mods
= xmalloc (source
->mod_count
4078 * sizeof (struct mod_source_files
));
4079 memset (source
->mods
, 0,
4080 source
->mod_count
* sizeof (struct mod_source_files
));
4084 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4087 struct module_info
*mod
= (struct module_info
*) ptr
;
4088 uint16_t stream_num
;
4090 uint32_t sym_byte_size
, c13_info_size
;
4091 uint8_t *start
= ptr
;
4093 stream
= add_stream (pdb
, NULL
, &stream_num
);
4097 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
4099 free (source
->mods
[i
].files
);
4102 free (source
->mods
);
4107 if (!populate_module_stream (stream
, in
, &sym_byte_size
,
4108 strings
, &c13_info_size
,
4109 &source
->mods
[mod_num
], abfd
,
4110 types
, ids
, mod_num
,
4111 sym_rec_stream
, glob
, pdb_name
))
4113 for (unsigned int i
= 0; i
< source
->mod_count
; i
++)
4115 free (source
->mods
[i
].files
);
4118 free (source
->mods
);
4123 bfd_putl32 (0, &mod
->unused1
);
4125 /* These are dummy values - MSVC copies the first section contribution
4126 entry here, but doesn't seem to use it for anything. */
4127 bfd_putl16 (0xffff, &mod
->sc
.section
);
4128 bfd_putl16 (0, &mod
->sc
.padding1
);
4129 bfd_putl32 (0, &mod
->sc
.offset
);
4130 bfd_putl32 (0xffffffff, &mod
->sc
.size
);
4131 bfd_putl32 (0, &mod
->sc
.characteristics
);
4132 bfd_putl16 (0xffff, &mod
->sc
.module_index
);
4133 bfd_putl16 (0, &mod
->sc
.padding2
);
4134 bfd_putl32 (0, &mod
->sc
.data_crc
);
4135 bfd_putl32 (0, &mod
->sc
.reloc_crc
);
4137 bfd_putl16 (0, &mod
->flags
);
4138 bfd_putl16 (stream_num
, &mod
->module_sym_stream
);
4139 bfd_putl32 (sizeof (uint32_t) + sym_byte_size
, &mod
->sym_byte_size
);
4140 bfd_putl32 (0, &mod
->c11_byte_size
);
4141 bfd_putl32 (c13_info_size
, &mod
->c13_byte_size
);
4142 bfd_putl16 (0, &mod
->source_file_count
);
4143 bfd_putl16 (0, &mod
->padding
);
4144 bfd_putl32 (0, &mod
->unused2
);
4145 bfd_putl32 (0, &mod
->source_file_name_index
);
4146 bfd_putl32 (0, &mod
->pdb_file_path_name_index
);
4148 ptr
+= sizeof (struct module_info
);
4150 if (!strcmp (bfd_get_filename (in
), "dll stuff"))
4153 memcpy (ptr
, linker_fn
, sizeof (linker_fn
));
4154 ptr
+= sizeof (linker_fn
);
4156 /* Empty module name. */
4160 else if (in
->my_archive
)
4162 char *name
= lrealpath (bfd_get_filename (in
));
4163 size_t name_len
= strlen (name
) + 1;
4166 memcpy (ptr
, name
, name_len
);
4171 name
= lrealpath (bfd_get_filename (in
->my_archive
));
4172 name_len
= strlen (name
) + 1;
4175 memcpy (ptr
, name
, name_len
);
4182 char *name
= lrealpath (bfd_get_filename (in
));
4183 size_t name_len
= strlen (name
) + 1;
4186 memcpy (ptr
, name
, name_len
);
4189 /* Object name again as archive name. */
4190 memcpy (ptr
, name
, name_len
);
4196 /* Pad to next four-byte boundary. */
4198 if ((ptr
- start
) % 4)
4200 memset (ptr
, 0, 4 - ((ptr
- start
) % 4));
4201 ptr
+= 4 - ((ptr
- start
) % 4);
4210 /* Return the index of a given output section. */
4212 find_section_number (bfd
*abfd
, asection
*sect
)
4216 for (asection
*s
= abfd
->sections
; s
; s
= s
->next
)
4221 /* Empty sections aren't output. */
4229 /* Used as parameter to qsort, to sort section contributions by section and
4232 section_contribs_compare (const void *p1
, const void *p2
)
4234 const struct in_sc
*sc1
= p1
;
4235 const struct in_sc
*sc2
= p2
;
4237 if (sc1
->sect_num
< sc2
->sect_num
)
4239 if (sc1
->sect_num
> sc2
->sect_num
)
4242 if (sc1
->s
->output_offset
< sc2
->s
->output_offset
)
4244 if (sc1
->s
->output_offset
> sc2
->s
->output_offset
)
4250 /* Create the substream which maps addresses in the image file to locations
4251 in the original object files. */
4253 create_section_contrib_substream (bfd
*abfd
, void **data
, uint32_t *size
)
4255 unsigned int num_sc
= 0;
4256 struct section_contribution
*sc
;
4260 struct in_sc
*sc_in
, *sc2
;
4263 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4266 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4268 if (s
->size
== 0 || discarded_section (s
))
4275 *size
= sizeof (uint32_t) + (num_sc
* sizeof (struct section_contribution
));
4276 *data
= xmalloc (*size
);
4278 bfd_putl32 (SECTION_CONTRIB_VERSION_60
, *data
);
4280 /* Read characteristics of outputted sections. */
4282 sect_flags
= xmalloc (sizeof (uint32_t) * abfd
->section_count
);
4284 offset
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
4285 offset
+= offsetof (struct external_scnhdr
, s_flags
);
4287 for (unsigned int i
= 0; i
< abfd
->section_count
; i
++)
4289 if (bfd_seek (abfd
, offset
, SEEK_SET
) != 0
4290 || bfd_read (sect_flags
+ (i
* sizeof (uint32_t)), sizeof (uint32_t),
4291 abfd
) != sizeof (uint32_t))
4298 offset
+= sizeof (struct external_scnhdr
);
4301 /* Microsoft's DIA expects section contributions to be sorted by section
4302 number and offset, otherwise it will be unable to resolve line numbers. */
4304 sc_in
= xmalloc (num_sc
* sizeof (* sc_in
));
4308 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4311 for (asection
*s
= in
->sections
; s
; s
= s
->next
)
4313 if (s
->size
== 0 || discarded_section (s
))
4317 sc2
->sect_num
= find_section_number (abfd
, s
->output_section
);
4318 sc2
->mod_index
= mod_index
;
4326 qsort (sc_in
, num_sc
, sizeof (* sc_in
), section_contribs_compare
);
4329 sc
= (struct section_contribution
*) (ptr
+ 1); /* Skip the version word. */
4331 for (unsigned int i
= 0; i
< num_sc
; i
++)
4333 memcpy (&sc
->characteristics
,
4334 sect_flags
+ ((sc_in
[i
].sect_num
- 1) * sizeof (uint32_t)),
4337 bfd_putl16 (sc_in
[i
].sect_num
, &sc
->section
);
4338 bfd_putl16 (0, &sc
->padding1
);
4339 bfd_putl32 (sc_in
[i
].s
->output_offset
, &sc
->offset
);
4340 bfd_putl32 (sc_in
[i
].s
->size
, &sc
->size
);
4341 bfd_putl16 (sc_in
[i
].mod_index
, &sc
->module_index
);
4342 bfd_putl16 (0, &sc
->padding2
);
4343 bfd_putl32 (0, &sc
->data_crc
);
4344 bfd_putl32 (0, &sc
->reloc_crc
);
4355 /* The source info substream lives within the DBI stream, and lists the
4356 source files for each object file (i.e. it's derived from the
4357 DEBUG_S_FILECHKSMS parts of the .debug$S sections). This is a bit
4358 superfluous, as the filenames are also available in the C13 parts of
4359 the module streams, but MSVC relies on it to work properly. */
4361 create_source_info_substream (void **data
, uint32_t *size
,
4362 struct source_files_info
*source
)
4364 uint16_t dedupe_source_files_count
= 0;
4365 uint16_t source_files_count
= 0;
4366 uint32_t strings_len
= 0;
4369 /* Loop through the source files, marking unique filenames. The pointers
4370 here are for entries in the main string table, and so have already
4371 been deduplicated. */
4373 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4375 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4377 if (source
->mods
[i
].files
[j
])
4379 if (source
->mods
[i
].files
[j
]->source_file_offset
== 0xffffffff)
4381 source
->mods
[i
].files
[j
]->source_file_offset
= strings_len
;
4382 strings_len
+= source
->mods
[i
].files
[j
]->len
+ 1;
4383 dedupe_source_files_count
++;
4386 source_files_count
++;
4391 *size
= sizeof (uint16_t) + sizeof (uint16_t);
4392 *size
+= (sizeof (uint16_t) + sizeof (uint16_t)) * source
->mod_count
;
4393 *size
+= sizeof (uint32_t) * source_files_count
;
4394 *size
+= strings_len
;
4396 *data
= xmalloc (*size
);
4398 ptr
= (uint8_t *) *data
;
4400 /* Write header (module count and source file count). */
4402 bfd_putl16 (source
->mod_count
, ptr
);
4403 ptr
+= sizeof (uint16_t);
4405 bfd_putl16 (dedupe_source_files_count
, ptr
);
4406 ptr
+= sizeof (uint16_t);
4408 /* Write "ModIndices". As the LLVM documentation puts it, "this array is
4409 present, but does not appear to be useful". */
4411 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4413 bfd_putl16 (i
, ptr
);
4414 ptr
+= sizeof (uint16_t);
4417 /* Write source file count for each module. */
4419 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4421 bfd_putl16 (source
->mods
[i
].files_count
, ptr
);
4422 ptr
+= sizeof (uint16_t);
4425 /* For each module, write the offsets within the string table
4426 for each source file. */
4428 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4430 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4432 if (source
->mods
[i
].files
[j
])
4434 bfd_putl32 (source
->mods
[i
].files
[j
]->source_file_offset
, ptr
);
4435 ptr
+= sizeof (uint32_t);
4440 /* Write the string table. We set source_file_offset to a dummy value for
4441 each entry we write, so we don't write duplicate filenames. */
4443 for (uint16_t i
= 0; i
< source
->mod_count
; i
++)
4445 for (uint16_t j
= 0; j
< source
->mods
[i
].files_count
; j
++)
4447 if (source
->mods
[i
].files
[j
]
4448 && source
->mods
[i
].files
[j
]->source_file_offset
!= 0xffffffff)
4450 memcpy (ptr
, source
->mods
[i
].files
[j
]->s
,
4451 source
->mods
[i
].files
[j
]->len
);
4452 ptr
+= source
->mods
[i
].files
[j
]->len
;
4457 source
->mods
[i
].files
[j
]->source_file_offset
= 0xffffffff;
4463 /* Used as parameter to qsort, to sort globals by hash. */
4465 global_compare_hash (const void *s1
, const void *s2
)
4467 const struct global
*g1
= *(const struct global
**) s1
;
4468 const struct global
*g2
= *(const struct global
**) s2
;
4470 if (g1
->hash
< g2
->hash
)
4472 if (g1
->hash
> g2
->hash
)
4478 /* Create the globals stream, which contains the unmangled symbol names. */
4480 create_globals_stream (bfd
*pdb
, struct globals
*glob
, uint16_t *stream_num
)
4483 struct globals_hash_header h
;
4484 uint32_t buckets_size
, filled_buckets
= 0;
4485 struct global
**sorted
= NULL
;
4487 struct global
*buckets
[NUM_GLOBALS_HASH_BUCKETS
];
4488 char int_buf
[sizeof (uint32_t)];
4490 stream
= add_stream (pdb
, NULL
, stream_num
);
4494 memset (buckets
, 0, sizeof (buckets
));
4496 if (glob
->num_entries
> 0)
4500 /* Create an array of pointers, sorted by hash value. */
4502 sorted
= xmalloc (sizeof (struct global
*) * glob
->num_entries
);
4505 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4511 qsort (sorted
, glob
->num_entries
, sizeof (struct global
*),
4512 global_compare_hash
);
4514 /* Populate the buckets. */
4516 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4518 if (!buckets
[sorted
[i
]->hash
])
4520 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4524 sorted
[i
]->index
= i
;
4528 buckets_size
= NUM_GLOBALS_HASH_BUCKETS
/ 8;
4529 buckets_size
+= sizeof (uint32_t);
4530 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4532 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &h
.signature
);
4533 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &h
.version
);
4534 bfd_putl32 (glob
->num_entries
* sizeof (struct hash_record
),
4536 bfd_putl32 (buckets_size
, &h
.buckets_size
);
4538 if (bfd_write (&h
, sizeof (h
), stream
) != sizeof (h
))
4541 /* Write hash entries, sorted by hash. */
4543 for (unsigned int i
= 0; i
< glob
->num_entries
; i
++)
4545 struct hash_record hr
;
4547 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4548 bfd_putl32 (sorted
[i
]->refcount
, &hr
.reference
);
4550 if (bfd_write (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4554 /* Write the bitmap for filled and unfilled buckets. */
4556 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
+= 8)
4560 for (unsigned int j
= 0; j
< 8; j
++)
4566 if (bfd_write (&v
, sizeof (v
), stream
) != sizeof (v
))
4570 /* Add a 4-byte gap. */
4572 bfd_putl32 (0, int_buf
);
4574 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
4577 /* Write the bucket offsets. */
4579 for (unsigned int i
= 0; i
< NUM_GLOBALS_HASH_BUCKETS
; i
++)
4583 /* 0xc is size of internal hash_record structure in
4584 Microsoft's parser. */
4585 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
4587 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) !=
4601 /* Hash an entry in the globals list. */
4603 hash_global_entry (const void *p
)
4605 const struct global
*g
= (const struct global
*) p
;
4606 uint16_t len
= bfd_getl16 (g
->data
);
4608 return iterative_hash (g
->data
, len
, 0);
4611 /* Compare an entry in the globals list with a symbol. */
4613 eq_global_entry (const void *a
, const void *b
)
4615 const struct global
*g
= (const struct global
*) a
;
4616 uint16_t len1
, len2
;
4618 len1
= bfd_getl16 (g
->data
) + sizeof (uint16_t);
4619 len2
= bfd_getl16 (b
) + sizeof (uint16_t);
4624 return !memcmp (g
->data
, b
, len1
);
4627 /* Stream 4 is the debug information (DBI) stream. */
4629 populate_dbi_stream (bfd
*stream
, bfd
*abfd
, bfd
*pdb
,
4630 uint16_t section_header_stream_num
,
4631 uint16_t sym_rec_stream_num
,
4632 uint16_t publics_stream_num
,
4633 struct string_table
*strings
,
4634 struct types
*types
,
4636 bfd
*sym_rec_stream
, const char *pdb_name
)
4638 struct pdb_dbi_stream_header h
;
4639 struct optional_dbg_header opt
;
4640 void *mod_info
, *sc
, *source_info
;
4641 uint32_t mod_info_size
, sc_size
, source_info_size
;
4642 struct source_files_info source
;
4643 struct globals glob
;
4644 uint16_t globals_stream_num
;
4646 source
.mod_count
= 0;
4649 glob
.num_entries
= 0;
4653 glob
.hashmap
= htab_create_alloc (0, hash_global_entry
,
4654 eq_global_entry
, free
, xcalloc
, free
);
4656 if (!create_module_info_substream (abfd
, pdb
, &mod_info
, &mod_info_size
,
4657 strings
, &source
, types
, ids
,
4658 sym_rec_stream
, &glob
, pdb_name
))
4660 htab_delete (glob
.hashmap
);
4664 if (!create_globals_stream (pdb
, &glob
, &globals_stream_num
))
4666 htab_delete (glob
.hashmap
);
4668 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4670 free (source
.mods
[i
].files
);
4678 htab_delete (glob
.hashmap
);
4680 if (!create_section_contrib_substream (abfd
, &sc
, &sc_size
))
4682 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4684 free (source
.mods
[i
].files
);
4692 create_source_info_substream (&source_info
, &source_info_size
, &source
);
4694 for (unsigned int i
= 0; i
< source
.mod_count
; i
++)
4696 free (source
.mods
[i
].files
);
4700 bfd_putl32 (0xffffffff, &h
.version_signature
);
4701 bfd_putl32 (DBI_STREAM_VERSION_70
, &h
.version_header
);
4702 bfd_putl32 (1, &h
.age
);
4703 bfd_putl16 (globals_stream_num
, &h
.global_stream_index
);
4704 bfd_putl16 (0x8e1d, &h
.build_number
); // MSVC 14.29
4705 bfd_putl16 (publics_stream_num
, &h
.public_stream_index
);
4706 bfd_putl16 (0, &h
.pdb_dll_version
);
4707 bfd_putl16 (sym_rec_stream_num
, &h
.sym_record_stream
);
4708 bfd_putl16 (0, &h
.pdb_dll_rbld
);
4709 bfd_putl32 (mod_info_size
, &h
.mod_info_size
);
4710 bfd_putl32 (sc_size
, &h
.section_contribution_size
);
4711 bfd_putl32 (0, &h
.section_map_size
);
4712 bfd_putl32 (source_info_size
, &h
.source_info_size
);
4713 bfd_putl32 (0, &h
.type_server_map_size
);
4714 bfd_putl32 (0, &h
.mfc_type_server_index
);
4715 bfd_putl32 (sizeof (opt
), &h
.optional_dbg_header_size
);
4716 bfd_putl32 (0, &h
.ec_substream_size
);
4717 bfd_putl16 (0, &h
.flags
);
4718 bfd_putl16 (get_arch_number (abfd
), &h
.machine
);
4719 bfd_putl32 (0, &h
.padding
);
4721 if (bfd_write (&h
, sizeof (h
), stream
) != sizeof (h
))
4729 if (bfd_write (mod_info
, mod_info_size
, stream
) != mod_info_size
)
4739 if (bfd_write (sc
, sc_size
, stream
) != sc_size
)
4748 if (bfd_write (source_info
, source_info_size
, stream
) != source_info_size
)
4756 bfd_putl16 (0xffff, &opt
.fpo_stream
);
4757 bfd_putl16 (0xffff, &opt
.exception_stream
);
4758 bfd_putl16 (0xffff, &opt
.fixup_stream
);
4759 bfd_putl16 (0xffff, &opt
.omap_to_src_stream
);
4760 bfd_putl16 (0xffff, &opt
.omap_from_src_stream
);
4761 bfd_putl16 (section_header_stream_num
, &opt
.section_header_stream
);
4762 bfd_putl16 (0xffff, &opt
.token_map_stream
);
4763 bfd_putl16 (0xffff, &opt
.xdata_stream
);
4764 bfd_putl16 (0xffff, &opt
.pdata_stream
);
4765 bfd_putl16 (0xffff, &opt
.new_fpo_stream
);
4766 bfd_putl16 (0xffff, &opt
.orig_section_header_stream
);
4768 if (bfd_write (&opt
, sizeof (opt
), stream
) != sizeof (opt
))
4774 /* Used as parameter to qsort, to sort publics by hash. */
4776 public_compare_hash (const void *s1
, const void *s2
)
4778 const struct public *p1
= *(const struct public **) s1
;
4779 const struct public *p2
= *(const struct public **) s2
;
4781 if (p1
->hash
< p2
->hash
)
4783 if (p1
->hash
> p2
->hash
)
4789 /* Used as parameter to qsort, to sort publics by address. */
4791 public_compare_addr (const void *s1
, const void *s2
)
4793 const struct public *p1
= *(const struct public **) s1
;
4794 const struct public *p2
= *(const struct public **) s2
;
4796 if (p1
->section
< p2
->section
)
4798 if (p1
->section
> p2
->section
)
4801 if (p1
->address
< p2
->address
)
4803 if (p1
->address
> p2
->address
)
4809 /* The publics stream is a hash map of S_PUB32 records, which are stored
4810 in the symbol record stream. Each S_PUB32 entry represents a symbol
4811 from the point of view of the linker: a section index, an offset within
4812 the section, and a mangled name. Compare with S_GDATA32 and S_GPROC32,
4813 which are the same thing but generated by the compiler. */
4815 populate_publics_stream (bfd
*stream
, bfd
*abfd
, bfd
*sym_rec_stream
)
4817 struct publics_header header
;
4818 struct globals_hash_header hash_header
;
4819 const unsigned int num_buckets
= 4096;
4820 unsigned int num_entries
= 0, filled_buckets
= 0;
4821 unsigned int buckets_size
, sym_hash_size
;
4822 char int_buf
[sizeof (uint32_t)];
4823 struct public *publics_head
= NULL
, *publics_tail
= NULL
;
4824 struct public **buckets
;
4825 struct public **sorted
= NULL
;
4828 buckets
= xmalloc (sizeof (struct public *) * num_buckets
);
4829 memset (buckets
, 0, sizeof (struct public *) * num_buckets
);
4831 /* Loop through the global symbols in our input files, and write S_PUB32
4832 records in the symbol record stream for those that make it into the
4834 for (bfd
*in
= coff_data (abfd
)->link_info
->input_bfds
; in
;
4837 if (!in
->outsymbols
)
4840 for (unsigned int i
= 0; i
< in
->symcount
; i
++)
4842 struct bfd_symbol
*sym
= in
->outsymbols
[i
];
4844 if (sym
->flags
& BSF_GLOBAL
)
4847 uint16_t record_length
;
4848 const char *name
= sym
->name
;
4849 size_t name_len
= strlen (name
);
4850 struct public *p
= xmalloc (sizeof (struct public));
4851 unsigned int padding
= 0;
4856 find_section_number (abfd
, sym
->section
->output_section
);
4862 p
->offset
= bfd_tell (sym_rec_stream
);
4863 p
->hash
= calc_hash (name
, name_len
) % num_buckets
;
4864 p
->section
= section
;
4865 p
->address
= sym
->section
->output_offset
+ sym
->value
;
4867 record_length
= sizeof (struct pubsym
) + name_len
+ 1;
4869 if (record_length
% 4)
4870 padding
= 4 - (record_length
% 4);
4872 /* Assume that all global symbols in executable sections
4874 if (sym
->section
->flags
& SEC_CODE
)
4875 flags
= PUBSYM_FUNCTION
;
4877 bfd_putl16 (record_length
+ padding
- sizeof (uint16_t),
4879 bfd_putl16 (S_PUB32
, &ps
.record_type
);
4880 bfd_putl32 (flags
, &ps
.flags
);
4881 bfd_putl32 (p
->address
, &ps
.offset
);
4882 bfd_putl16 (p
->section
, &ps
.section
);
4884 if (bfd_write (&ps
, sizeof (struct pubsym
), sym_rec_stream
) !=
4885 sizeof (struct pubsym
))
4888 if (bfd_write (name
, name_len
+ 1, sym_rec_stream
) !=
4892 for (unsigned int j
= 0; j
< padding
; j
++)
4896 if (bfd_write (&b
, sizeof (uint8_t), sym_rec_stream
) !=
4904 publics_tail
->next
= p
;
4913 if (num_entries
> 0)
4915 /* Create an array of pointers, sorted by hash value. */
4917 sorted
= xmalloc (sizeof (struct public *) * num_entries
);
4919 struct public *p
= publics_head
;
4920 for (unsigned int i
= 0; i
< num_entries
; i
++)
4926 qsort (sorted
, num_entries
, sizeof (struct public *),
4927 public_compare_hash
);
4929 /* Populate the buckets. */
4931 for (unsigned int i
= 0; i
< num_entries
; i
++)
4933 if (!buckets
[sorted
[i
]->hash
])
4935 buckets
[sorted
[i
]->hash
] = sorted
[i
];
4939 sorted
[i
]->index
= i
;
4943 buckets_size
= num_buckets
/ 8;
4944 buckets_size
+= sizeof (uint32_t);
4945 buckets_size
+= filled_buckets
* sizeof (uint32_t);
4947 sym_hash_size
= sizeof (hash_header
);
4948 sym_hash_size
+= num_entries
* sizeof (struct hash_record
);
4949 sym_hash_size
+= buckets_size
;
4951 /* Output the publics header. */
4953 bfd_putl32 (sym_hash_size
, &header
.sym_hash_size
);
4954 bfd_putl32 (num_entries
* sizeof (uint32_t), &header
.addr_map_size
);
4955 bfd_putl32 (0, &header
.num_thunks
);
4956 bfd_putl32 (0, &header
.thunks_size
);
4957 bfd_putl32 (0, &header
.thunk_table
);
4958 bfd_putl32 (0, &header
.thunk_table_offset
);
4959 bfd_putl32 (0, &header
.num_sects
);
4961 if (bfd_write (&header
, sizeof (header
), stream
) != sizeof (header
))
4964 /* Output the global hash header. */
4966 bfd_putl32 (GLOBALS_HASH_SIGNATURE
, &hash_header
.signature
);
4967 bfd_putl32 (GLOBALS_HASH_VERSION_70
, &hash_header
.version
);
4968 bfd_putl32 (num_entries
* sizeof (struct hash_record
),
4969 &hash_header
.entries_size
);
4970 bfd_putl32 (buckets_size
, &hash_header
.buckets_size
);
4972 if (bfd_write (&hash_header
, sizeof (hash_header
), stream
) !=
4973 sizeof (hash_header
))
4976 /* Write the entries in hash order. */
4978 for (unsigned int i
= 0; i
< num_entries
; i
++)
4980 struct hash_record hr
;
4982 bfd_putl32 (sorted
[i
]->offset
+ 1, &hr
.offset
);
4983 bfd_putl32 (1, &hr
.reference
);
4985 if (bfd_write (&hr
, sizeof (hr
), stream
) != sizeof (hr
))
4989 /* Write the bitmap for filled and unfilled buckets. */
4991 for (unsigned int i
= 0; i
< num_buckets
; i
+= 8)
4995 for (unsigned int j
= 0; j
< 8; j
++)
5001 if (bfd_write (&v
, sizeof (v
), stream
) != sizeof (v
))
5005 /* Add a 4-byte gap. */
5007 bfd_putl32 (0, int_buf
);
5009 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5012 /* Write the bucket offsets. */
5014 for (unsigned int i
= 0; i
< num_buckets
; i
++)
5018 /* 0xc is size of internal hash_record structure in
5019 Microsoft's parser. */
5020 bfd_putl32 (buckets
[i
]->index
* 0xc, int_buf
);
5022 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) !=
5028 /* Write the address map: offsets into the symbol record stream of
5029 S_PUB32 records, ordered by address. */
5031 if (num_entries
> 0)
5033 qsort (sorted
, num_entries
, sizeof (struct public *),
5034 public_compare_addr
);
5036 for (unsigned int i
= 0; i
< num_entries
; i
++)
5038 bfd_putl32 (sorted
[i
]->offset
, int_buf
);
5040 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) !=
5051 while (publics_head
)
5053 struct public *p
= publics_head
->next
;
5055 free (publics_head
);
5064 /* The section header stream contains a copy of the section headers
5065 from the PE file, in the same format. */
5067 create_section_header_stream (bfd
*pdb
, bfd
*abfd
, uint16_t *num
)
5070 unsigned int section_count
;
5075 stream
= add_stream (pdb
, NULL
, num
);
5079 section_count
= abfd
->section_count
;
5081 /* Empty sections aren't output. */
5082 for (asection
*sect
= abfd
->sections
; sect
; sect
= sect
->next
)
5084 if (sect
->size
== 0)
5088 if (section_count
== 0)
5091 /* Copy section table from output - it's already been written at this
5094 scn_base
= bfd_coff_filhsz (abfd
) + bfd_coff_aoutsz (abfd
);
5096 if (bfd_seek (abfd
, scn_base
, SEEK_SET
) != 0)
5099 len
= section_count
* sizeof (struct external_scnhdr
);
5100 buf
= xmalloc (len
);
5102 if (bfd_read (buf
, len
, abfd
) != len
)
5108 if (bfd_write (buf
, len
, stream
) != len
)
5119 /* Populate the "/names" named stream, which contains the string table. */
5121 populate_names_stream (bfd
*stream
, struct string_table
*strings
)
5123 char int_buf
[sizeof (uint32_t)];
5124 struct string_table_header h
;
5125 uint32_t num_strings
= 0, num_buckets
;
5126 struct string
**buckets
;
5128 bfd_putl32 (STRING_TABLE_SIGNATURE
, &h
.signature
);
5129 bfd_putl32 (STRING_TABLE_VERSION
, &h
.version
);
5131 if (bfd_write (&h
, sizeof (h
), stream
) != sizeof (h
))
5134 bfd_putl32 (strings
->strings_len
, int_buf
);
5136 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5141 if (bfd_write (int_buf
, 1, stream
) != 1)
5144 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5146 if (bfd_write (s
->s
, s
->len
, stream
) != s
->len
)
5149 if (bfd_write (int_buf
, 1, stream
) != 1)
5155 num_buckets
= num_strings
* 2;
5157 buckets
= xmalloc (sizeof (struct string
*) * num_buckets
);
5158 memset (buckets
, 0, sizeof (struct string
*) * num_buckets
);
5160 for (struct string
*s
= strings
->strings_head
; s
; s
= s
->next
)
5162 uint32_t bucket_num
= s
->hash
% num_buckets
;
5164 while (buckets
[bucket_num
])
5168 if (bucket_num
== num_buckets
)
5172 buckets
[bucket_num
] = s
;
5175 bfd_putl32 (num_buckets
, int_buf
);
5177 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5183 for (unsigned int i
= 0; i
< num_buckets
; i
++)
5186 bfd_putl32 (buckets
[i
]->offset
, int_buf
);
5188 bfd_putl32 (0, int_buf
);
5190 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) !=
5200 bfd_putl32 (num_strings
, int_buf
);
5202 if (bfd_write (int_buf
, sizeof (uint32_t), stream
) != sizeof (uint32_t))
5208 /* Calculate the hash of a type_entry. */
5210 hash_type_entry (const void *p
)
5212 const struct type_entry
*e
= (const struct type_entry
*) p
;
5213 uint16_t size
= bfd_getl16 (e
->data
) + sizeof (uint16_t);
5215 return iterative_hash (e
->data
, size
, 0);
5218 /* Compare a type_entry with a type. */
5220 eq_type_entry (const void *a
, const void *b
)
5222 const struct type_entry
*e
= (const struct type_entry
*) a
;
5223 uint16_t size_a
= bfd_getl16 (e
->data
);
5224 uint16_t size_b
= bfd_getl16 (b
);
5226 if (size_a
!= size_b
)
5229 return memcmp (e
->data
+ sizeof (uint16_t),
5230 (const uint8_t *) b
+ sizeof (uint16_t), size_a
) == 0;
5233 /* Create a PDB debugging file for the PE image file abfd with the build ID
5234 guid, stored at pdb_name. */
5236 create_pdb_file (bfd
*abfd
, const char *pdb_name
, const unsigned char *guid
)
5240 bfd
*info_stream
, *dbi_stream
, *names_stream
, *sym_rec_stream
,
5241 *publics_stream
, *tpi_stream
, *ipi_stream
;
5242 uint16_t section_header_stream_num
, sym_rec_stream_num
, publics_stream_num
;
5243 struct string_table strings
;
5244 struct types types
, ids
;
5246 pdb
= bfd_openw (pdb_name
, "pdb");
5249 einfo (_("%P: warning: cannot create PDB file: %E\n"));
5253 strings
.strings_head
= NULL
;
5254 strings
.strings_tail
= NULL
;
5255 strings
.strings_len
= 1;
5256 strings
.hashmap
= htab_create_alloc (0, hash_string_table_entry
,
5257 eq_string_table_entry
, free
,
5260 bfd_set_format (pdb
, bfd_archive
);
5262 if (!create_old_directory_stream (pdb
))
5264 einfo (_("%P: warning: cannot create old directory stream "
5265 "in PDB file: %E\n"));
5269 info_stream
= add_stream (pdb
, NULL
, NULL
);
5273 einfo (_("%P: warning: cannot create info stream "
5274 "in PDB file: %E\n"));
5278 tpi_stream
= add_stream (pdb
, NULL
, NULL
);
5282 einfo (_("%P: warning: cannot create TPI stream "
5283 "in PDB file: %E\n"));
5287 dbi_stream
= add_stream (pdb
, NULL
, NULL
);
5291 einfo (_("%P: warning: cannot create DBI stream "
5292 "in PDB file: %E\n"));
5296 ipi_stream
= add_stream (pdb
, NULL
, NULL
);
5300 einfo (_("%P: warning: cannot create IPI stream "
5301 "in PDB file: %E\n"));
5305 names_stream
= add_stream (pdb
, "/names", NULL
);
5309 einfo (_("%P: warning: cannot create /names stream "
5310 "in PDB file: %E\n"));
5314 sym_rec_stream
= add_stream (pdb
, NULL
, &sym_rec_stream_num
);
5316 if (!sym_rec_stream
)
5318 einfo (_("%P: warning: cannot create symbol record stream "
5319 "in PDB file: %E\n"));
5323 publics_stream
= add_stream (pdb
, NULL
, &publics_stream_num
);
5325 if (!publics_stream
)
5327 einfo (_("%P: warning: cannot create publics stream "
5328 "in PDB file: %E\n"));
5332 if (!create_section_header_stream (pdb
, abfd
, §ion_header_stream_num
))
5334 einfo (_("%P: warning: cannot create section header stream "
5335 "in PDB file: %E\n"));
5339 types
.num_types
= 0;
5340 types
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5341 free
, xcalloc
, free
);
5342 types
.first
= types
.last
= NULL
;
5345 ids
.hashmap
= htab_create_alloc (0, hash_type_entry
, eq_type_entry
,
5346 free
, xcalloc
, free
);
5347 ids
.first
= ids
.last
= NULL
;
5349 if (!populate_dbi_stream (dbi_stream
, abfd
, pdb
, section_header_stream_num
,
5350 sym_rec_stream_num
, publics_stream_num
,
5351 &strings
, &types
, &ids
, sym_rec_stream
, pdb_name
))
5353 einfo (_("%P: warning: cannot populate DBI stream "
5354 "in PDB file: %E\n"));
5355 htab_delete (types
.hashmap
);
5356 htab_delete (ids
.hashmap
);
5360 if (!populate_type_stream (pdb
, tpi_stream
, &types
))
5362 einfo (_("%P: warning: cannot populate TPI stream "
5363 "in PDB file: %E\n"));
5364 htab_delete (types
.hashmap
);
5365 htab_delete (ids
.hashmap
);
5369 htab_delete (types
.hashmap
);
5371 if (!populate_type_stream (pdb
, ipi_stream
, &ids
))
5373 einfo (_("%P: warning: cannot populate IPI stream "
5374 "in PDB file: %E\n"));
5375 htab_delete (ids
.hashmap
);
5379 htab_delete (ids
.hashmap
);
5381 add_string ("", 0, &strings
);
5383 if (!populate_names_stream (names_stream
, &strings
))
5385 einfo (_("%P: warning: cannot populate names stream "
5386 "in PDB file: %E\n"));
5390 if (!populate_publics_stream (publics_stream
, abfd
, sym_rec_stream
))
5392 einfo (_("%P: warning: cannot populate publics stream "
5393 "in PDB file: %E\n"));
5397 if (!populate_info_stream (pdb
, info_stream
, guid
))
5399 einfo (_("%P: warning: cannot populate info stream "
5400 "in PDB file: %E\n"));
5409 htab_delete (strings
.hashmap
);