1 /* BFD back-end for PDB Multi-Stream Format archives.
2 Copyright (C) 2022-2024 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
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. */
21 /* This describes the MSF file archive format, which is used for the
22 PDB debug info generated by MSVC. See https://llvm.org/docs/PDB/MsfFile.html
23 for a full description of the format. */
29 /* "Microsoft C/C++ MSF 7.00\r\n\x1a\x44\x53\0\0\0" */
30 static const uint8_t pdb_magic
[] =
31 { 0x4d, 0x69, 0x63, 0x72, 0x6f, 0x73, 0x6f, 0x66,
32 0x74, 0x20, 0x43, 0x2f, 0x43, 0x2b, 0x2b, 0x20,
33 0x4d, 0x53, 0x46, 0x20, 0x37, 0x2e, 0x30, 0x30,
34 0x0d, 0x0a, 0x1a, 0x44, 0x53, 0x00, 0x00, 0x00 };
36 #define arch_eltdata(bfd) ((struct areltdata *) ((bfd)->arelt_data))
39 pdb_archive_p (bfd
*abfd
)
42 char magic
[sizeof (pdb_magic
)];
44 ret
= bfd_read (magic
, sizeof (magic
), abfd
);
45 if (ret
!= sizeof (magic
))
47 bfd_set_error (bfd_error_wrong_format
);
51 if (memcmp (magic
, pdb_magic
, sizeof (magic
)))
53 bfd_set_error (bfd_error_wrong_format
);
57 void *tdata
= bfd_zalloc (abfd
, sizeof (struct artdata
));
60 bfd_ardata (abfd
) = tdata
;
62 return _bfd_no_cleanup
;
66 pdb_get_elt_at_index (bfd
*abfd
, symindex sym_index
)
68 char int_buf
[sizeof (uint32_t)];
69 uint32_t block_size
, block_map_addr
, block
, num_files
;
70 uint32_t first_dir_block
, dir_offset
, file_size
, block_off
, left
;
77 if (bfd_seek (abfd
, sizeof (pdb_magic
), SEEK_SET
))
80 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
82 bfd_set_error (bfd_error_malformed_archive
);
86 block_size
= bfd_getl32 (int_buf
);
87 if ((block_size
& -block_size
) != block_size
91 bfd_set_error (bfd_error_malformed_archive
);
95 /* Get block_map_addr. */
97 if (bfd_seek (abfd
, 4 * sizeof (uint32_t), SEEK_CUR
))
100 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
102 bfd_set_error (bfd_error_malformed_archive
);
106 block_map_addr
= bfd_getl32 (int_buf
);
110 if (bfd_seek (abfd
, block_map_addr
* block_size
, SEEK_SET
))
113 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
115 bfd_set_error (bfd_error_malformed_archive
);
119 first_dir_block
= bfd_getl32 (int_buf
);
121 if (bfd_seek (abfd
, first_dir_block
* block_size
, SEEK_SET
))
124 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
126 bfd_set_error (bfd_error_malformed_archive
);
130 num_files
= bfd_getl32 (int_buf
);
132 if (sym_index
>= num_files
)
134 bfd_set_error (bfd_error_no_more_archived_files
);
138 /* Read file size. */
140 dir_offset
= sizeof (uint32_t) * (sym_index
+ 1);
142 if (dir_offset
>= block_size
)
144 uint32_t block_map_addr_off
;
146 block_map_addr_off
= ((dir_offset
/ block_size
) * sizeof (uint32_t));
148 if (bfd_seek (abfd
, (block_map_addr
* block_size
) + block_map_addr_off
,
152 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
154 bfd_set_error (bfd_error_malformed_archive
);
158 block
= bfd_getl32 (int_buf
);
162 block
= first_dir_block
;
165 if (bfd_seek (abfd
, (block
* block_size
) + (dir_offset
% block_size
),
169 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
171 bfd_set_error (bfd_error_malformed_archive
);
175 file_size
= bfd_getl32 (int_buf
);
177 /* Undocumented? Seen on PDBs created by MSVC 2022. */
178 if (file_size
== 0xffffffff)
183 /* Four hex digits is enough - even though MSF allows for 32 bits, the
184 PDB format itself only uses 16 bits for stream numbers. */
185 sprintf (name
, "%04lx", sym_index
);
187 file
= bfd_create (name
, abfd
);
192 if (!bfd_make_writable (file
))
196 (struct areltdata
*) bfd_zmalloc (sizeof (struct areltdata
));
198 if (!file
->arelt_data
)
201 arch_eltdata (file
)->parsed_size
= file_size
;
202 arch_eltdata (file
)->key
= sym_index
;
209 /* Sum number of blocks in previous files. */
213 dir_offset
= sizeof (uint32_t);
215 if (bfd_seek (abfd
, (first_dir_block
* block_size
) + sizeof (uint32_t),
219 for (symindex i
= 0; i
< sym_index
; i
++)
221 uint32_t size
, num_blocks
;
223 if ((dir_offset
% block_size
) == 0)
225 uint32_t block_map_addr_off
;
228 ((dir_offset
/ block_size
) * sizeof (uint32_t));
231 (abfd
, (block_map_addr
* block_size
) + block_map_addr_off
,
235 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) !=
238 bfd_set_error (bfd_error_malformed_archive
);
242 block
= bfd_getl32 (int_buf
);
244 if (bfd_seek (abfd
, block
* block_size
, SEEK_SET
))
248 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) !=
251 bfd_set_error (bfd_error_malformed_archive
);
255 size
= bfd_getl32 (int_buf
);
257 if (size
== 0xffffffff)
260 num_blocks
= (size
+ block_size
- 1) / block_size
;
261 block_off
+= num_blocks
;
263 dir_offset
+= sizeof (uint32_t);
267 /* Read blocks, and write into new BFD. */
269 dir_offset
= sizeof (uint32_t) * (num_files
+ block_off
+ 1);
271 if (dir_offset
>= block_size
)
273 uint32_t block_map_addr_off
;
275 block_map_addr_off
= ((dir_offset
/ block_size
) * sizeof (uint32_t));
277 if (bfd_seek (abfd
, (block_map_addr
* block_size
) + block_map_addr_off
,
281 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
283 bfd_set_error (bfd_error_malformed_archive
);
287 block
= bfd_getl32 (int_buf
);
291 block
= first_dir_block
;
294 buf
= bfd_malloc (block_size
);
301 uint32_t file_block
, to_read
;
303 if ((dir_offset
% block_size
) == 0 && left
!= file_size
)
305 uint32_t block_map_addr_off
;
308 ((dir_offset
/ block_size
) * sizeof (uint32_t));
311 (abfd
, (block_map_addr
* block_size
) + block_map_addr_off
,
315 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) !=
318 bfd_set_error (bfd_error_malformed_archive
);
322 block
= bfd_getl32 (int_buf
);
325 if (bfd_seek (abfd
, (block
* block_size
) + (dir_offset
% block_size
),
329 if (bfd_read (int_buf
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
331 bfd_set_error (bfd_error_malformed_archive
);
335 file_block
= bfd_getl32 (int_buf
);
337 if (bfd_seek (abfd
, file_block
* block_size
, SEEK_SET
))
340 to_read
= left
> block_size
? block_size
: left
;
342 if (bfd_read (buf
, to_read
, abfd
) != to_read
)
344 bfd_set_error (bfd_error_malformed_archive
);
348 if (bfd_write (buf
, to_read
, file
) != to_read
)
351 if (left
> block_size
)
356 dir_offset
+= sizeof (uint32_t);
373 pdb_openr_next_archived_file (bfd
*archive
, bfd
*last_file
)
376 return pdb_get_elt_at_index (archive
, 0);
378 return pdb_get_elt_at_index (archive
, arch_eltdata (last_file
)->key
+ 1);
382 pdb_generic_stat_arch_elt (bfd
*abfd
, struct stat
*buf
)
388 buf
->st_size
= arch_eltdata (abfd
)->parsed_size
;
394 pdb_allocate_block (uint32_t *num_blocks
, uint32_t block_size
)
402 /* If new interval, skip two blocks for free space map. */
404 if ((block
% block_size
) == 1)
414 pdb_write_directory (bfd
*abfd
, uint32_t block_size
, uint32_t num_files
,
415 uint32_t block_map_addr
, uint32_t * num_blocks
,
416 uint32_t *stream0_start
)
418 char tmp
[sizeof (uint32_t)];
419 uint32_t block
, left
, block_map_off
;
423 /* Allocate first block for directory. */
425 block
= pdb_allocate_block (num_blocks
, block_size
);
428 /* Write allocated block no. at beginning of block map. */
430 if (bfd_seek (abfd
, block_map_addr
* block_size
, SEEK_SET
))
433 bfd_putl32 (block
, tmp
);
435 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
438 block_map_off
= sizeof (uint32_t);
440 /* Write num_files at beginning of directory. */
442 if (bfd_seek (abfd
, block
* block_size
, SEEK_SET
))
445 bfd_putl32 (num_files
, tmp
);
447 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
450 left
-= sizeof (uint32_t);
452 /* Write file sizes. */
454 arelt
= abfd
->archive_head
;
459 if (block_map_off
== block_size
) /* Too many blocks. */
461 bfd_set_error (bfd_error_invalid_operation
);
465 block
= pdb_allocate_block (num_blocks
, block_size
);
469 (abfd
, (block_map_addr
* block_size
) + block_map_off
, SEEK_SET
))
472 bfd_putl32 (block
, tmp
);
474 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
477 block_map_off
+= sizeof (uint32_t);
479 if (bfd_seek (abfd
, block
* block_size
, SEEK_SET
))
483 bfd_putl32 (bfd_get_size (arelt
), tmp
);
485 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
488 left
-= sizeof (uint32_t);
490 arelt
= arelt
->archive_next
;
495 buf
= bfd_malloc (block_size
);
499 arelt
= abfd
->archive_head
;
502 ufile_ptr size
= bfd_get_size (arelt
);
503 uint32_t req_blocks
= (size
+ block_size
- 1) / block_size
;
505 if (bfd_seek (arelt
, 0, SEEK_SET
))
511 for (uint32_t i
= 0; i
< req_blocks
; i
++)
513 uint32_t file_block
, to_read
;
517 if (block_map_off
== block_size
) /* Too many blocks. */
519 bfd_set_error (bfd_error_invalid_operation
);
524 block
= pdb_allocate_block (num_blocks
, block_size
);
528 (abfd
, (block_map_addr
* block_size
) + block_map_off
,
535 bfd_putl32 (block
, tmp
);
537 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) !=
544 block_map_off
+= sizeof (uint32_t);
546 if (bfd_seek (abfd
, block
* block_size
, SEEK_SET
))
553 /* Allocate block and write number into directory. */
555 file_block
= pdb_allocate_block (num_blocks
, block_size
);
557 bfd_putl32 (file_block
, tmp
);
559 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
565 if (arelt
== abfd
->archive_head
&& i
== 0)
566 *stream0_start
= file_block
;
568 left
-= sizeof (uint32_t);
570 /* Read file contents into buffer. */
572 to_read
= size
> block_size
? block_size
: size
;
574 if (bfd_read (buf
, to_read
, arelt
) != to_read
)
582 if (to_read
< block_size
)
583 memset (buf
+ to_read
, 0, block_size
- to_read
);
585 if (bfd_seek (abfd
, file_block
* block_size
, SEEK_SET
))
591 /* Write file contents into allocated block. */
593 if (bfd_write (buf
, block_size
, abfd
) != block_size
)
600 (abfd
, (block
* block_size
) + block_size
- left
, SEEK_SET
))
607 arelt
= arelt
->archive_next
;
610 memset (buf
, 0, left
);
612 if (bfd_write (buf
, left
, abfd
) != left
)
624 pdb_write_bitmap (bfd
*abfd
, uint32_t block_size
, uint32_t num_blocks
,
625 uint32_t stream0_start
)
628 uint32_t num_intervals
= (num_blocks
+ block_size
- 1) / block_size
;
630 buf
= bfd_malloc (block_size
);
634 for (uint32_t i
= 0; i
< num_intervals
; i
++)
636 if (bfd_seek (abfd
, ((i
* block_size
) + 1) * block_size
, SEEK_SET
))
642 /* All of our blocks are contiguous, making our free block map
643 relatively simple. 0 = used, 1 = free. */
648 block_size
? block_size
: (num_blocks
/ 8));
650 if (num_blocks
< block_size
* 8)
652 unsigned int off
= num_blocks
/ 8;
656 buf
[off
] = 256 - (1 << (num_blocks
% 8));
660 if (off
< block_size
)
661 memset (buf
+ off
, 0xff, block_size
- off
);
664 /* Mark the blocks allocated to stream 0 as free. This is because stream
665 0 is intended to be used for the previous MSF directory, to allow
666 atomic updates. This doesn't apply to us, as we rewrite the whole
667 file whenever any change is made. */
669 if (i
== 0 && abfd
->archive_head
)
671 bfd
*arelt
= abfd
->archive_head
;
672 uint32_t stream0_blocks
=
673 (bfd_get_size (arelt
) + block_size
- 1) / block_size
;
675 if (stream0_start
% 8)
677 unsigned int high_bit
;
679 high_bit
= (stream0_start
% 8) + stream0_blocks
;
683 buf
[stream0_start
/ 8] |=
684 (1 << high_bit
) - (1 << (stream0_start
% 8));
686 stream0_blocks
-= high_bit
- (stream0_start
% 8);
687 stream0_start
+= high_bit
- (stream0_start
% 8);
690 memset (buf
+ (stream0_start
/ 8), 0xff, stream0_blocks
/ 8);
691 stream0_start
+= stream0_blocks
/ 8;
694 if (stream0_blocks
> 0)
695 buf
[stream0_start
/ 8] |= (1 << stream0_blocks
) - 1;
698 if (num_blocks
< block_size
* 8)
701 num_blocks
-= block_size
* 8;
703 if (bfd_write (buf
, block_size
, abfd
) != block_size
)
713 pdb_write_contents (bfd
*abfd
)
715 char tmp
[sizeof (uint32_t)];
716 const uint32_t block_size
= 0x400;
717 uint32_t block_map_addr
;
719 uint32_t num_files
= 0;
720 uint32_t num_directory_bytes
= sizeof (uint32_t);
721 uint32_t stream0_start
= 0;
724 if (bfd_write (pdb_magic
, sizeof (pdb_magic
), abfd
) != sizeof (pdb_magic
))
727 bfd_putl32 (block_size
, tmp
);
729 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
732 bfd_putl32 (1, tmp
); /* Free block map block (always either 1 or 2). */
734 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
737 arelt
= abfd
->archive_head
;
741 uint32_t blocks_required
=
742 (bfd_get_size (arelt
) + block_size
- 1) / block_size
;
744 num_directory_bytes
+= sizeof (uint32_t); /* Size. */
745 num_directory_bytes
+= blocks_required
* sizeof (uint32_t); /* Blocks. */
749 arelt
= arelt
->archive_next
;
752 /* Superblock plus two bitmap blocks. */
755 /* Skip num_blocks for now. */
756 if (bfd_seek (abfd
, sizeof (uint32_t), SEEK_CUR
))
759 bfd_putl32 (num_directory_bytes
, tmp
);
761 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
764 /* Skip unknown uint32_t (always 0?). */
765 if (bfd_seek (abfd
, sizeof (uint32_t), SEEK_CUR
))
768 block_map_addr
= pdb_allocate_block (&num_blocks
, block_size
);
770 bfd_putl32 (block_map_addr
, tmp
);
772 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
775 if (!pdb_write_directory
776 (abfd
, block_size
, num_files
, block_map_addr
, &num_blocks
,
780 if (!pdb_write_bitmap (abfd
, block_size
, num_blocks
, stream0_start
))
783 /* Write num_blocks now we know it. */
786 (abfd
, sizeof (pdb_magic
) + sizeof (uint32_t) + sizeof (uint32_t),
790 bfd_putl32 (num_blocks
, tmp
);
792 if (bfd_write (tmp
, sizeof (uint32_t), abfd
) != sizeof (uint32_t))
798 #define pdb_bfd_free_cached_info _bfd_generic_bfd_free_cached_info
799 #define pdb_new_section_hook _bfd_generic_new_section_hook
800 #define pdb_get_section_contents _bfd_generic_get_section_contents
801 #define pdb_close_and_cleanup _bfd_generic_close_and_cleanup
803 #define pdb_slurp_armap _bfd_noarchive_slurp_armap
804 #define pdb_slurp_extended_name_table _bfd_noarchive_slurp_extended_name_table
805 #define pdb_construct_extended_name_table _bfd_noarchive_construct_extended_name_table
806 #define pdb_truncate_arname _bfd_noarchive_truncate_arname
807 #define pdb_write_armap _bfd_noarchive_write_armap
808 #define pdb_read_ar_hdr _bfd_noarchive_read_ar_hdr
809 #define pdb_write_ar_hdr _bfd_noarchive_write_ar_hdr
810 #define pdb_update_armap_timestamp _bfd_noarchive_update_armap_timestamp
812 const bfd_target pdb_vec
=
815 bfd_target_unknown_flavour
,
816 BFD_ENDIAN_LITTLE
, /* target byte order */
817 BFD_ENDIAN_LITTLE
, /* target headers byte order */
818 0, /* object flags */
819 0, /* section flags */
820 0, /* leading underscore */
821 ' ', /* ar_pad_char */
822 16, /* ar_max_namelen */
823 0, /* match priority. */
824 TARGET_KEEP_UNUSED_SECTION_SYMBOLS
, /* keep unused section symbols. */
825 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
826 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
827 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* Data. */
828 bfd_getl64
, bfd_getl_signed_64
, bfd_putl64
,
829 bfd_getl32
, bfd_getl_signed_32
, bfd_putl32
,
830 bfd_getl16
, bfd_getl_signed_16
, bfd_putl16
, /* Hdrs. */
832 { /* bfd_check_format */
838 { /* bfd_set_format */
839 _bfd_bool_bfd_false_error
,
840 _bfd_bool_bfd_false_error
,
842 _bfd_bool_bfd_false_error
844 { /* bfd_write_contents */
846 _bfd_bool_bfd_false_error
,
848 _bfd_bool_bfd_false_error
851 BFD_JUMP_TABLE_GENERIC (pdb
),
852 BFD_JUMP_TABLE_COPY (_bfd_generic
),
853 BFD_JUMP_TABLE_CORE (_bfd_nocore
),
854 BFD_JUMP_TABLE_ARCHIVE (pdb
),
855 BFD_JUMP_TABLE_SYMBOLS (_bfd_nosymbols
),
856 BFD_JUMP_TABLE_RELOCS (_bfd_norelocs
),
857 BFD_JUMP_TABLE_WRITE (_bfd_generic
),
858 BFD_JUMP_TABLE_LINK (_bfd_nolink
),
859 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic
),