-
Notifications
You must be signed in to change notification settings - Fork 0
/
write_pdb.c
2228 lines (1705 loc) · 105 KB
/
write_pdb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <time.h>
#include <assert.h>
#define kilo_bytes(a) ((a) * 1024ULL)
#define mega_bytes(a) ((kilo_bytes(a)) * 1024ULL)
#define giga_bytes(a) ((mega_bytes(a)) * 1024ULL)
#define push_struct(arena, type) ((type *)memory_arena_allocate_bytes((arena), sizeof(type), _Alignof(type)))
#define push_array(arena, type, count) ((type *)memory_arena_allocate_bytes((arena), sizeof(type) * (count), _Alignof(type)))
#define push_struct_unaligned(arena, type) ((type *)memory_arena_allocate_bytes((arena), sizeof(type), 1))
#define push_array_unaligned(arena, type, count) ((type *)memory_arena_allocate_bytes((arena), sizeof(type) * (count), 1))
#define array_count(a) (sizeof(a)/sizeof(*a))
#define offset_in_type(type, member) (u64)(&((type *)0)->member)
enum pdb_stream{
// Fixed streams.
PDB_STREAM_pdb_information = 1,
PDB_STREAM_tpi = 2,
PDB_STREAM_dbi = 3,
PDB_STREAM_ipi = 4,
PDB_STREAM_names,
PDB_STREAM_tpi_hash,
PDB_STREAM_ipi_hash,
PDB_STREAM_section_header_dump,
PDB_STREAM_symbol_record,
PDB_STREAM_global_symbol_index,
PDB_STREAM_public_symbol_index,
PDB_STREAM_module_symbol_stream_base,
};
// For reference see `HashPbCb` in `microsoft-pdb/PDB/include/misc.h`.
u32 pdb_hash_index(u8 *bytes, size_t size, u32 modulus){
u32 hash = 0;
// Xor the bytes by dword lanes.
for(u32 index = 0; index < size/sizeof(u32); index++){
hash ^= ((u32 *)bytes)[index];
}
// Xor remaining bytes in.
if(size & 2) hash ^= *(u16 *)(bytes + (size & ~3));
if(size & 1) hash ^= *(u8 *) (bytes + (size - 1));
// Make sure the hash is case insensitive.
hash |= 0x20202020;
// Mix the lanes.
hash ^= (hash >> 11);
hash ^= (hash >> 16);
// Take the modulus and return the hash.
return (hash % modulus);
}
u16 hash_string(char *string){
return (u16)pdb_hash_index((u8 *)string, strlen(string), (u32)-1);
}
// returns -1 on failiure.
void pdb_stream_skip_numeric_leaf(struct stream *stream){
u16 numeric_leaf;
if(stream_read(stream, &numeric_leaf, sizeof(numeric_leaf))) return;
if(!(numeric_leaf & 0x8000))return;
//
// @cleanup: implement this more correctly
//
switch(numeric_leaf){
case 0x8000:{ // LF_CHAR
stream_skip(stream, 1);
}break;
case 0x8001: // LF_SHORT
case 0x8002:{ // LF_USHORT
stream_skip(stream, 2);
}break;
case 0x8005: // LF_REAL32
case 0x8003: // LF_LONG
case 0x8004:{ // LF_ULONG
stream_skip(stream, 4);
}break;
case 0x8009: // LF_QUADWORD
case 0x800a: // LF_UQUADWORD
case 0x8006:{ // LF_REAL64
stream_skip(stream, 8);
}break;
case 0x8008: // LF_REAL128
// case 0x8007: // LF_REAL80
// case 0x800b: // LF_REAL48
// case 0x800c: // LF_COMPLEX32
// case 0x800d: // LF_COMPLEX64
// case 0x800e: // LF_COMPLEX80
// case 0x800f: // LF_COMPLEX128
// case 0x8010: // LF_VARSTRING
case 0x8017: // LF_OCTWORD
case 0x8018:{ // LF_UOCTWORD
stream_skip(stream, 16);
}break;
// case 0x8019: // LF_DECIMAL
// case 0x801a: // LF_DATE
// case 0x801b: // LF_UTF8STRING
// case 0x801c: // LF_REAL16
default:{
print("WARNING: Unhandled numeric leaf kind 0x%hx. This might lead to incorrect type information.\n", numeric_leaf);
}break;
}
}
void pdb_stream_skip_zero_terminated_string(struct stream *stream){
while(stream->offset < stream->size && stream->data[stream->offset] != 0){
stream->offset += 1;
}
if(stream->offset < stream->size) stream->offset++;
}
u32 crc32(u32 initial_crc, u8 *data, u64 amount){
// crc32 works by using polynomial division over F_2.
// The i-th bit corresponds to X^i.
// for simplicity lets assume there are 100 bits:
// msg: [b99:b98:...: b0] <-> b99 X^99 + b98 X^98 + ... + b0
// CRC32 uses the 'generating polynomial':
// X^32 + X^26 + X^23 + X^22 + X^16 + X^12 + X^11 + X^10 + X^8 + X^7 + X^5 + X^4 + X^2 + X + 1
// or 100000100110000010001110110110111 = 0x104c11db7, we usually omit the first one.
// The crc32 of a message is the remainder after long division by the generating polynomial.
// ACTUALLY: Everything uses 'reflected' values. The reflected polynomial is 0xedb88320.
// This means the highest value bit is the lowest bit.
#if 0
// 'reflect' the entry i.e swap all bits
u32 reflect(u32 entry){
for(u32 i = 0; i < 16; i++){
u32 j = 31 - i;
u32 bit1 = ((1 << i) & entry);
u32 bit2 = ((1 << j) & entry);
entry ^= bit1 | bit2 | ((bit1 >> i) << j) | ((bit2 >> j) << i));
}
return entry;
}
#endif
// This table maps 'byte' -> ('byte' * X^32 mod g(X)).
static const u32 crc32_table[0x100] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5,
0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59,
0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f,
0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d,
0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433,
0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457,
0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65,
0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb,
0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9,
0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad,
0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683,
0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1,
0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7,
0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b,
0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79,
0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f,
0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d,
0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21,
0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777,
0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45,
0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db,
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
};
// The crc_table is generated by this code:
#if 0
for(u32 entry_index = 0; entry_index < 0x100; entry_index++){
u32 entry = entry_index;
for(u32 bit_index = 0; bit_index < 8; bit_index++){
// Perform polynomial division.
// If the top bit is set, we subtract (xor) the (reflected) generating polynomial.
entry = (entry & 1) ? ((entry >> 1) ^ reflect(0x04c11db7)) : (entry >> 1);
}
// After we are done, 'entry' is the remainder of polynomial division over F_2 of 'i * X^32'
// store this in the table.
crc_table[entry_index] = entry;
}
#endif
// Assume we have a message and a last byte
// [msg?,...,msg0] | [lb7,...,lb0]
// and we have calculated the remainder of 'msg * X^32' after division by g(X) to be 'crc'
// i.e: msg * X^32 + crc = 0 mod g(X)
// Thus we calculate
// crc' = (msg||lb) * X^32 mod g(X)
// = msg * X^40 + lb * X^32 mod g(X)
// = crc * X^8 + lb * X^32 mod g(X)
// = (crc[31:8] << 8) + (crc[7:0] + lb) * X^32
// Note the reflection on crc.
// Finally the line in the for below is this equation for the crc' using the table above
// crc' = (crc[31:8] << 8) + ((crc[7:0] + lb) * X^32 mod g(X))
u32 crc = initial_crc;
for(u64 i = 0; i < amount; i++){
crc = (crc >> 8) ^ crc32_table[(crc & 0xff) ^ data[i]];
}
return crc;
}
struct codeview_type_record_header{
u16 length;
u16 kind;
};
// returns -1 on failiure.
int pdb_numeric_leaf_size_or_error(u16 numeric_leaf){
if(!(numeric_leaf & 0x8000)) return 2;
//
// @cleanup: implement this more correctly
//
switch(numeric_leaf){
case 0x8000:{ // LF_CHAR
return 2 + 1;
}break;
case 0x8001: // LF_SHORT
case 0x8002:{ // LF_USHORT
return 2 + 2;
}break;
case 0x8005: // LF_REAL32
case 0x8003: // LF_LONG
case 0x8004:{ // LF_ULONG
return 2 + 4;
}break;
case 0x8009: // LF_QUADWORD
case 0x800a: // LF_UQUADWORD
case 0x8006:{ // LF_REAL64
return 2 + 8;
}break;
case 0x8008: // LF_REAL128
// case 0x8007: // LF_REAL80
// case 0x800b: // LF_REAL48
// case 0x800c: // LF_COMPLEX32
// case 0x800d: // LF_COMPLEX64
// case 0x800e: // LF_COMPLEX80
// case 0x800f: // LF_COMPLEX128
// case 0x8010: // LF_VARSTRING
case 0x8017: // LF_OCTWORD
case 0x8018:{ // LF_UOCTWORD
return 2 + 16;
}break;
// case 0x8019: // LF_DECIMAL
// case 0x801a: // LF_DATE
// case 0x801b: // LF_UTF8STRING
// case 0x801c: // LF_REAL16
default:{
return -1;
}break;
}
// unreachable!
return -1;
}
char *pdb_type_record__get_name(u8 *type_record){
struct codeview_type_header{
u16 length;
u16 kind;
} *type_header = (void *)type_record;
char *type_data = (char *)(type_header + 1);
switch(type_header->kind){
case /*LF_CLASS2*/0x1608:
case /*LF_INTERFACE2*/0x160b:
case /*LF_STRUCTURE2*/0x1609:{
type_data += 0x10;
type_data += pdb_numeric_leaf_size_or_error(*(u16 *)type_data); // count
type_data += pdb_numeric_leaf_size_or_error(*(u16 *)type_data); // size
return type_data;
}break;
case /*LF_STRUCTURE*/0x1505:
case /*LF_CLASS*/0x1504:
case /*LF_INTERFACE*/0x1519:{
return type_data + 0x10 + pdb_numeric_leaf_size_or_error(*(u16 *)(type_data + 0x10));
}break;
case /*LF_UNION2*/0x160a:{
type_data += 8;
type_data += pdb_numeric_leaf_size_or_error(*(u16 *)type_data); // count
type_data += pdb_numeric_leaf_size_or_error(*(u16 *)type_data); // size
return type_data;
}break;
case /*LF_UNION*/0x1506:{
return type_data + 8 + pdb_numeric_leaf_size_or_error(*(u16 *)(type_data + 8));
}break;
case /*LF_ENUM*/0x1507:{
return type_data + 12;
}break;
case /*LF_ALIAS*/0x150a:{
return type_data + 4;
}break;
default: return "";
}
}
u32 tpi_hash_table_index_for_record(struct codeview_type_record_header *type_record_header, u32 number_of_hash_buckets){
u8 *type_data = (u8 *)(type_record_header + 1);
char *name = 0;
size_t length = 0;
switch(type_record_header->kind){
case /*LF_ALIAS*/0x150a:{
name = (char *)(type_data + 4);
}break;
case /*LF_CLASS2*/0x1608:
case /*LF_INTERFACE2*/0x160b:
case /*LF_STRUCTURE2*/0x1609: // @note: These get rid of the 'count' member to get 32-bits of 'properties' but stay the same size.
case /*LF_UNION2*/0x160a: // @note: These get rid of the 'count' member to get 32-bits of 'properties' but stay the same size.
case /*LF_UNION*/0x1506:
case /*LF_ENUM*/0x1507:
case /*LF_CLASS*/0x1504:
case /*LF_STRUCTURE*/0x1505:
case /*LF_INTERFACE*/0x1519:{
u32 properties;
if(type_record_header->kind < 0x1600){
// @note: All of these have the 'properies' field at the same offset.
properties = *(u16 *)(type_data + 2);
}else{
// @note: These dropped the 'count' for 32-bits more of properties.
properties = *(u32 *)type_data;
}
u16 forward_ref = (properties & (1 << 7));
u16 scoped = (properties & (1 << 8));
u16 has_unique_name = (properties & (1 << 9));
char *tag_name = pdb_type_record__get_name((u8 *)type_record_header);
// @note: This only works for c. for c++ they also search for 'foo::<unnamed-tag>' stuff.
int anonymous = (strcmp(tag_name, "<unnamed-tag>") == 0) || (strcmp(tag_name, "__unnamed") == 0);
if(!forward_ref && !anonymous){
if(!scoped){
name = tag_name;
}else if(has_unique_name){
name = tag_name + strlen(tag_name) + 1;
}
}
}break;
case /*LF_UDT_SRC_LINE*/0x1606:
case /*LF_UDT_MOD_SRC_LINE*/0x1607:{
name = (char *)type_data;
length = sizeof(u32);
}break;
}
u32 hash_index;
if(name){
if(!length) length = strlen(name);
hash_index = pdb_hash_index((u8 *)name, length, number_of_hash_buckets);
}else{
hash_index = crc32(/*initial_crc*/0, (u8 *)type_record_header, type_record_header->length + sizeof(type_record_header->length)) % number_of_hash_buckets;
}
return hash_index;
}
struct codeview_public_symbol{
struct codeview_symbol_header{
u16 length;
u16 kind;
} header;
u32 flags;
u32 offset_in_section;
u16 section_id;
char name[];
};
u8 *global__base_of_the_symbol_record_stream_only_here_so_we_can_qsort;
int compare_public_symbol_offsets(const void *a, const void *b){
u8 *base = global__base_of_the_symbol_record_stream_only_here_so_we_can_qsort;
struct codeview_public_symbol *a_symbol = (void *)(base + *(u32 *)a);
struct codeview_public_symbol *b_symbol = (void *)(base + *(u32 *)b);
if(a_symbol->section_id != b_symbol->section_id) return a_symbol->section_id - b_symbol->section_id;
if(a_symbol->offset_in_section != b_symbol->offset_in_section) return (int)a_symbol->offset_in_section - (int)b_symbol->offset_in_section;
return strcmp(a_symbol->name, b_symbol->name);
}
struct pdb_section_contribution{
s16 section_id;
u16 padding1;
s32 offset;
s32 size;
u32 characteristics;
s16 module_index;
u16 padding2;
u32 data_crc;
u32 reloc_crc;
};
struct write_pdb_information{
struct pdb_guid{
u32 data1;
u16 data2;
u16 data3;
u8 data4[8];
} pdb_guid;
u32 amount_of_object_files;
struct write_pdb_per_object_information{
char *file_name;
struct stream type_information;
struct stream symbol_information;
} *per_object;
size_t amount_of_section_contributions;
struct pdb_section_contribution *section_contributions;
u16 amount_of_image_sections;
struct coff_section_header *image_section_headers;
u8 *public_symbols;
u64 public_symbols_size;
u64 amount_of_public_symbols;
};
void write_pdb(struct write_pdb_information *write_pdb_information){
struct memory_arena arena = create_memory_arena(giga_bytes(8));
struct memory_arena pdb_information_stream = create_memory_arena(giga_bytes(8));
{
//
// Fill out the information stream.
//
struct pdb_information_stream_header{
u32 version;
u32 timestamp;
u32 age;
struct pdb_guid guid;
} *pdb_information_stream_header = push_struct(&pdb_information_stream, struct pdb_information_stream_header);
pdb_information_stream_header->version = 20000404;
pdb_information_stream_header->timestamp = time(NULL);
pdb_information_stream_header->age = 1;
pdb_information_stream_header->guid = write_pdb_information->pdb_guid;
static struct {
char *stream_name;
u32 stream_index;
} named_streams[] = {
{ "/names", PDB_STREAM_names },
};
u32 named_stream_table_capacity = 2 * array_count(named_streams);
struct named_stream_table_entry{
u32 key;
u32 value;
} *named_stream_table_entries = push_array(&arena, struct named_stream_table_entry, named_stream_table_capacity);
u32 *string_buffer_size = push_struct(&pdb_information_stream, u32);
u8 *string_buffer = push_array(&pdb_information_stream, u8, 0);
for(u32 named_stream_index = 0; named_stream_index < array_count(named_streams); named_stream_index++){
char *stream_name = named_streams[named_stream_index].stream_name;
u32 stream_index = named_streams[named_stream_index].stream_index;
u16 hash = hash_string(stream_name);
for(u32 hash_index = 0; hash_index < named_stream_table_capacity; hash_index++){
u32 index = (hash_index + hash) % named_stream_table_capacity;
// @note: Currently there are no deleted named steams.
// So we don't have to care about tombstones!
if(named_stream_table_entries[index].value == /*empty_slot*/0){
// We have found an empty slot.
// Allocate the `stream_name` into the `string_buffer`.
size_t stream_name_length = strlen(stream_name);
u8 *stream_name_in_string_buffer = push_array(&pdb_information_stream, u8, stream_name_length + 1);
memcpy(stream_name_in_string_buffer, stream_name, stream_name_length + 1);
named_stream_table_entries[index].key = stream_name_in_string_buffer - string_buffer;
named_stream_table_entries[index].value = stream_index;
break;
}
}
}
*string_buffer_size = push_array(&pdb_information_stream, u8, 0) - string_buffer;
//
// @WARNING: "Importantly, after the string table, the rest of the stream
// does not have any defined alignment anymore."
//
/*amount_of_entries*/*push_struct_unaligned(&pdb_information_stream, u32) = array_count(named_streams);
/*capacity */*push_struct_unaligned(&pdb_information_stream, u32) = named_stream_table_capacity;
u32 present_bits_word_count = ((named_stream_table_capacity + 31) & ~31)/32;
/*present_bits.word_count*/*push_struct_unaligned(&pdb_information_stream, u32) = present_bits_word_count;
u32 *present_bits_words = push_array_unaligned(&pdb_information_stream, u32, present_bits_word_count);
for(u32 named_stream_table_index = 0; named_stream_table_index < named_stream_table_capacity; named_stream_table_index++){
u32 word_index = named_stream_table_index / (sizeof(u32) * 8);
u32 bit_index = named_stream_table_index % (sizeof(u32) * 8);
if(named_stream_table_entries[named_stream_table_index].value != 0){
present_bits_words[word_index] |= (1u << bit_index);
}
}
/*deleted_bits.word_count*/*push_struct_unaligned(&pdb_information_stream, u32) = 0;
// struct { u32 key; u32 value; } entries[amount_of_entries];
for(u32 named_stream_table_index = 0; named_stream_table_index < named_stream_table_capacity; named_stream_table_index++){
if(named_stream_table_entries[named_stream_table_index].value != 0){
*push_struct_unaligned(&pdb_information_stream, struct named_stream_table_entry) = named_stream_table_entries[named_stream_table_index];
}
}
/*unused*/*push_struct_unaligned(&pdb_information_stream, u32) = 0;
// Feature code:
*push_struct_unaligned(&pdb_information_stream, u32) = /*impvVC140*/20140508;
}
// @cleanup: this needs growing later!
char *names_stream_buckets[0x100] = {0};
struct memory_arena names_stream = create_memory_arena(giga_bytes(8));
struct names_stream_header{
u32 signature;
u32 hash_version;
u32 string_buffer_byte_size;
char string_buffer[];
} *names_stream_header = push_struct(&names_stream, struct names_stream_header);
names_stream_header->signature = 0xEFFEEFFE;
names_stream_header->hash_version = 1;
names_stream_header->string_buffer_byte_size = 1;
// "The first string inside the string buffer always has to be the zero-sized string,
// as a zero offset is also used as an invalid offset in the hash table."
push_struct(&names_stream, u8); // zero-sized string!
struct memory_arena tpi_stream = create_memory_arena(giga_bytes(8));
struct memory_arena ipi_stream = create_memory_arena(giga_bytes(8));
struct memory_arena tpi_hash_stream = create_memory_arena(giga_bytes(8));
struct memory_arena ipi_hash_stream = create_memory_arena(giga_bytes(8));
struct type_index_map_entry{
size_t size;
u32 *data;
} *type_index_map_per_object_file = push_array(&arena, struct type_index_map_entry, write_pdb_information->amount_of_object_files);
struct tpi_index_offset_buffer_entry{
u32 type_index;
u32 offset_in_record_data;
} *ipi_index_offset_buffer = 0;
size_t ipi_index_offset_buffer_size = 0;
struct tpi_stream_header{
u32 version;
u32 header_size;
u32 minimal_type_index;
u32 one_past_last_type_index;
u32 byte_count_of_type_record_data_following_the_header;
u16 stream_index_of_hash_stream;
u16 stream_index_of_auxiliary_hash_stream;
u32 hash_key_size;
u32 number_of_hash_buckets;
u32 hash_table_index_buffer_offset;
u32 hash_table_index_buffer_length;
u32 index_offset_buffer_offset;
u32 index_offset_buffer_length;
u32 udt_order_adjust_table_offset;
u32 udt_order_adjust_table_length;
};
{
struct tpi_stream_header *tpi_header = push_struct(&tpi_stream, struct tpi_stream_header);
struct tpi_stream_header *ipi_header = push_struct(&ipi_stream, struct tpi_stream_header);
struct tpi_index_offset_buffer_entry *tpi_index_offset_buffer_last_entry = push_struct(&tpi_hash_stream, struct tpi_index_offset_buffer_entry);
tpi_index_offset_buffer_last_entry->type_index = 0x1000;
struct tpi_index_offset_buffer_entry *ipi_index_offset_buffer_last_entry = push_struct(&ipi_hash_stream, struct tpi_index_offset_buffer_entry);
ipi_index_offset_buffer_last_entry->type_index = 0x1000;
ipi_index_offset_buffer = ipi_index_offset_buffer_last_entry;
u32 ipi_type_index_at = 0x1000;
u32 tpi_type_index_at = 0x1000;
u32 unhandled_type_leafs[0x100];
u32 unhandled_type_leafs_at = 0;
struct memory_arena hash_table_arena = create_memory_arena(giga_bytes(8));
static struct tpi_hash_table_entry{
struct tpi_hash_table_entry *next;
u32 type_index;
struct codeview_type_record_header *type_record;
} *tpi_hash_table[0x3ffff] = {0}, *ipi_hash_table[0x3ffff] = {0};
for(u32 object_file_index = 0; object_file_index < write_pdb_information->amount_of_object_files; object_file_index++){
struct stream type_information = write_pdb_information->per_object[object_file_index].type_information;
u32 signature;
if(stream_read(&type_information, &signature, sizeof(signature)) || signature != /*CV_SIGNATURE_C13*/4) continue;
// We daisy chain this map into 'arena', whenever we handle a type record below.
u32 *object_file_type_index_to_pdb_file_type_index_map = push_array(&arena, u32, 0);
type_index_map_per_object_file[object_file_index].data = object_file_type_index_to_pdb_file_type_index_map;
struct codeview_type_record_header type_record_header;
u32 object_file_type_index = 0x1000;
while(!stream_read(&type_information, &type_record_header, sizeof(type_record_header))){
int record_size = type_record_header.length - sizeof(type_record_header.kind);
if(record_size < 0) break;
u8 *record_data = stream_read_array_by_pointer(&type_information, 1, record_size);
if(!record_data) break;
//
// We need to remap all of the type indices used by the type records,
// from the object file local ones to the pdb ones.
//
struct{
struct codeview_type_record_header type_record_header;
u32 type_index;
u32 src_file_string_id;
u32 line_number;
u16 module;
u8 f1;
u8 f0;
} udt_mod_src_line_record_stack_space;
#define remap_type_index(v) (v) = ((v) < 0x1000 ? (v) : ((v >= object_file_type_index) ? 0 : object_file_type_index_to_pdb_file_type_index_map[(v)-0x1000]))
switch(type_record_header.kind){
case /*LF_MODIFIER*/0x1001:
case /*LF_POINTER */0x1002:{
u32 *type_index = (void *)record_data;
if(sizeof(*type_index) >= record_size) break;
remap_type_index(*type_index);
}break;
case /*LF_PROCEDURE*/0x1008:{
struct {
u32 return_value;
u8 call_type;
u8 function_attributes;
u16 parameter_count;
u32 arglist;
} *procedure = (void *)record_data;
if(sizeof(*procedure) > record_size) break;
remap_type_index(procedure->return_value);
remap_type_index(procedure->arglist);
}break;
case /*LF_ARGLIST*/0x1201:{
struct{
u32 count;
u32 argument_type[];
} *arglist = (void *)record_data;
if(sizeof(*arglist) > record_size) break;
if((u64)arglist->count * sizeof(u32) > (u64)record_size - sizeof(u32)) break;
for(u32 argument_index = 0; argument_index < arglist->count; argument_index++){
remap_type_index(arglist->argument_type[argument_index]);
}
}break;
case /*LF_FIELDLIST*/0x1203:{
// LF_FIELDLIST consist of a sequence of sub-records that describe the
// struct, union or enum members in the form of LF_MEMBER or LF_ENUMERATE entries.
// There is also a special LF_INDEX entry for LF_FIELDLIST which would exceed
// the u16-length field.
// One annoying thing is that these sub-records are not sized.
struct stream record_stream = {
.data = record_data,
.size = record_size,
};
while(record_stream.offset + sizeof(u16) <= record_stream.size){
switch(*(u16 *)(record_stream.data + record_stream.offset)){
case /*LF_ENUMERATE*/0x1502:{
struct {
u16 kind;
u16 attributes;
// numeric_leaf field_offset;
// char name[];
} *enumerate = stream_read_array_by_pointer(&record_stream, sizeof(*enumerate), 1);
if(!enumerate) break;
pdb_stream_skip_numeric_leaf(&record_stream);
pdb_stream_skip_zero_terminated_string(&record_stream);
}break;
case /*LF_MEMBER*/0x150d:{
struct {
u16 kind;
u16 attributes;
u32 type_index;
// numeric_leaf field_offset;
// char name[];
} *member = stream_read_array_by_pointer(&record_stream, sizeof(*member), 1);
if(!member) break;
remap_type_index(member->type_index);
pdb_stream_skip_numeric_leaf(&record_stream);
pdb_stream_skip_zero_terminated_string(&record_stream);
}break;
case /*LF_INDEX*/0x1404:{
struct {
u16 kind;
u16 padding;
u32 type_index;
} *index = stream_read_array_by_pointer(&record_stream, sizeof(*index), 1);
if(!index) break;
remap_type_index(index->type_index);
}break;
default:{
print("Warning: Unhandled entry in LF_FIELDLIST of kind 0x%hx. Unable to recover for this fieldlist.\n", *(u16 *)(record_stream.data + record_stream.offset));
record_stream.offset = record_stream.size; // break the outer loop.
}break;
}
record_stream.offset = (record_stream.offset + 3) & ~3;
}
}break;
case /*LF_ARRAY*/0x1503:{
struct{
u32 element_type;
u32 index_type;
} *array = (void *)record_data;
if(sizeof(*array) > record_size) break;
remap_type_index(array->element_type);
remap_type_index(array->index_type);
}break;
case /*LF_STRUCTURE*/0x1505:{
struct{
u16 count;
u16 property;
u32 fieldlist;
u32 derived;
u32 vshape;
} *structure = (void *)record_data;
if(sizeof(*structure) > record_size) break;
remap_type_index(structure->fieldlist);
remap_type_index(structure->derived);
remap_type_index(structure->vshape);
}break;
case /*LF_UNION*/0x1506:{
struct{
u16 count;
u16 property;
u32 fieldlist;
} *lf_union = (void *)record_data;
if(sizeof(*lf_union) > record_size) break;
remap_type_index(lf_union->fieldlist);
}break;
case /*LF_ENUM*/0x1507:{
struct{
u16 count;
u16 property;
u32 underlying_type;
u32 fieldlist;
} *enumeration = (void *)record_data;
if(sizeof(*enumeration) > record_size) break;
remap_type_index(enumeration->underlying_type);
remap_type_index(enumeration->fieldlist);
}break;
//
// Id Records:
//
case /*LF_FUNC_ID*/0x1601:{
struct {
u32 scope_id;
u32 type;
} *func_id = (void *)record_data;
if(sizeof(*func_id) > record_size) break;
remap_type_index(func_id->scope_id);
remap_type_index(func_id->type);
}break;
case /*LF_BUILDINFO*/0x1603:{
struct{
u16 count;
} *buildinfo = (void *)record_data;
if(sizeof(*buildinfo) > record_size) break;
u32 *arg = (u32 *)(buildinfo + 1);
if((u64)buildinfo->count * sizeof(u32) > (u64)record_size - sizeof(u32)) break;
for(u32 argument_index = 0; argument_index < buildinfo->count; argument_index++){
remap_type_index(arg[argument_index]);
}
}break;
case /*LF_SUBSTR_LIST*/0x1604:{
// @note: This is the same code as arglist, because its the same record,
// but I want to keep types and records seperate.
struct{
u32 count;
u32 substring[];
} *substring_list = (void *)record_data;
if(sizeof(*substring_list) > record_size) break;
if((u64)substring_list->count * sizeof(u32) > (u64)record_size - sizeof(u32)) break;
for(u32 argument_index = 0; argument_index < substring_list->count; argument_index++){
remap_type_index(substring_list->substring[argument_index]);
}
}break;
case /*LF_STRING_ID*/0x1605:{
u32 *id = (u32 *)record_data;
if(sizeof(*id) > record_size) break;
remap_type_index(*id);
}break;
case /*LF_UDT_SRC_LINE*/0x1606:{
struct {
u32 type_index;
u32 src_file_string_id;
u32 line_number;
} *udt_src_line = (void *)record_data;
if(sizeof(*udt_src_line) > record_size) break;
remap_type_index(udt_src_line->type_index);
remap_type_index(udt_src_line->src_file_string_id);
// @hack: We need change this `LF_UDT_SRC_LINE` to a `LF_UDT_MOD_SRC_LINE`,
// but in this loop, we are expected to use the `record_data` to get
// to the symbol, which is inside the `symbol_information` so we cannot patch it.
// Hence, we use some stack memory and copy the symbol to said stack memory.
udt_mod_src_line_record_stack_space.type_record_header.kind = /*LF_UDT_MOD_SRC_LINE*/0x1607;
udt_mod_src_line_record_stack_space.type_record_header.length = sizeof(udt_mod_src_line_record_stack_space) - sizeof(udt_mod_src_line_record_stack_space.type_record_header.length);
udt_mod_src_line_record_stack_space.type_index = udt_src_line->type_index;
udt_mod_src_line_record_stack_space.src_file_string_id = udt_src_line->src_file_string_id;
udt_mod_src_line_record_stack_space.line_number = udt_src_line->line_number;
udt_mod_src_line_record_stack_space.module = (object_file_index + 1);
udt_mod_src_line_record_stack_space.f1 = 0xf1;
udt_mod_src_line_record_stack_space.f0 = 0xf0;
record_data = (u8 *)(&udt_mod_src_line_record_stack_space.type_record_header + 1);
record_size = sizeof(udt_mod_src_line_record_stack_space) - sizeof(udt_mod_src_line_record_stack_space.type_record_header);
}break;
default:{
if(unhandled_type_leafs_at < array_count(unhandled_type_leafs)){
int found = 0;
for(u32 index = 0; index < unhandled_type_leafs_at; index++){
if(unhandled_type_leafs[index] == type_record_header.kind){
found = 1;
break;
}
}
if(!found) unhandled_type_leafs[unhandled_type_leafs_at++] = type_record_header.kind;
}
print("Warning: Unknown type record kind 0x%hx ignored. This might lead to incorrect type information in the pdb.\n", type_record_header.kind);
}break;
}
#undef remap_type_index
//
// Push the type record to the stream and insert an element into the map.
//
u32 pdb_type_index;
//
// Add the type record either to the IPI or the TPI stream.
//
struct codeview_type_record_header *record = (void *)(record_data - sizeof(type_record_header));
struct memory_arena *stream, *hash_stream;
struct tpi_hash_table_entry *(*hash_table)[array_count(tpi_hash_table)];
struct tpi_stream_header *stream_header;
struct tpi_index_offset_buffer_entry **index_offset_buffer_last_entry;
u32 *type_index_at;
if(0x1600 <= type_record_header.kind && type_record_header.kind < 0x1700){
// This record is a Id-record.
hash_table = &ipi_hash_table;
stream = &ipi_stream;
hash_stream = &ipi_hash_stream;
stream_header = ipi_header;
index_offset_buffer_last_entry = &ipi_index_offset_buffer_last_entry;
type_index_at = &ipi_type_index_at;