-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathtilbuild.cpp
2766 lines (2579 loc) · 81.7 KB
/
tilbuild.cpp
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 "tilbuild.hpp"
#include "misc.cpp"
//#define PDEB
//#define PDEBSYM
#ifdef PDEB
#define ddeb(x) _ddeb x
AS_PRINTF(1, 2) inline void _ddeb(const char *format, ...)
{
va_list va;
va_start(va, format);
vmsg(format, va);
va_end(va);
}
void dump_pdb_udt(const pdb_udt_type_data_t &udt, const char *udt_name)
{
static size_t udt_counter = 0;
++udt_counter;
msg("PDEB: %" FMT_Z " %s '%s' total_size %" FMT_Z " taudt_bits 0x%X is_union %s\n",
udt_counter,
udt.is_union ? "union" : "struct",
udt_name != nullptr ? udt_name : "",
udt.total_size,
udt.taudt_bits,
udt.is_union ? "Yes" : "No");
for ( int i=0; i < udt.size(); i++ )
{
const pdb_udm_t &zudm = udt[i];
msg(" %d. offset 0x%" FMT_64 "X size 0x%" FMT_64 "X '%s' type '%s' effalign %d tafld_bits 0x%X fda %d bit_offset %u\n",
i,
zudm.offset,
zudm.size,
zudm.name.c_str(),
zudm.type.dstr(),
zudm.effalign,
zudm.tafld_bits,
zudm.fda,
zudm.bit_offset);
}
}
#else
#define ddeb(x) (void)0
void dump_pdb_udt(const pdb_udt_type_data_t &, const char *) {}
#endif
static const char fake_vtable_type[] = "$vt";
//----------------------------------------------------------------------------
void til_builder_t::remove_anonymous_namespaces(qstring &buf)
{
char *p = buf.begin();
while ( true )
{ // 1234567890
p = strstr(p, "`anonymous");
if ( p == nullptr )
break;
const char *q = p + 10;
if ( *q != '-' && *q != ' ' )
break;
if ( strncmp(q+1, "namespace'::", 12) != 0 )
break; // 123456789012
size_t idx = p - buf.begin();
buf.remove(idx, 10+1+12);
p = buf.begin() + idx;
}
}
//-------------------------------------------------------------------------
static inline bool ident_char(char c)
{
return c == '_' || qisalnum(c);
}
//----------------------------------------------------------------------------
bool til_builder_t::get_symbol_name(pdb_sym_t &sym, qstring &buf)
{
bool is_unnamed = false;
sym.get_name(&buf);
if ( buf.empty() )
{
is_unnamed = true;
}
else
{
//
remove_anonymous_namespaces(buf);
// <unnamed-tag> => <unnamed_tag>
// <unnamed-type-xxx> => <unnamed_type_xxx>
char *p = buf.begin();
while ( true )
{
// 012345678
p = strstr(p, "<unnamed");
if ( p == nullptr )
break;
if ( p == buf.begin() )
is_unnamed = true;
p += 8;
while ( *p != '\0' )
{
if ( *p == '>' )
{
p++;
break;
}
else if ( *p == '-' )
{
*p = '_';
}
p++;
}
}
if ( !is_unnamed )
{
const char *marker = strstr(buf.begin(), "__unnamed");
if ( marker != nullptr
// Is prev char not a valid identifier char?
&& (marker == buf.begin() || !ident_char(marker[-1]))
// Is next char not a valid identifier char?
&& !ident_char(marker[9]) )
{
is_unnamed = true;
}
}
}
return is_unnamed;
}
//----------------------------------------------------------------------------
bool til_builder_t::get_symbol_type(tpinfo_t *out, pdb_sym_t &sym, uint32 *p_ord)
{
#ifdef PDEBSYM
static int zz=0; ++zz;
int zzz = zz;
qstring sym_name;
sym.get_name(&sym_name);
DWORD sym_id = 0;
sym.get_symIndexId(&sym_id);
msg("PDEB: %d: get_symbol_type sym_id=%d '%s'\n", zzz, sym_id, sym_name.c_str());
#endif
pdb_sym_t *pType = pdb_access->create_sym();
pdb_sym_janitor_t janitor_pType(pType);
if ( sym.get_type(pType) != S_OK )
return false;
bool ok = retrieve_type(out, *pType, nullptr, p_ord);
#ifdef PDEBSYM
DWORD typsym_id = 0;
pType->get_symIndexId(&typsym_id);
msg("PDEB: %d: get_symbol_type typsym_id=%d tif='%s' ok=%d\n", zzz, typsym_id, out->type.dstr(), ok);
#endif
return ok;
}
//----------------------------------------------------------------------------
bool til_builder_t::fix_ctor_to_return_ptr(func_type_data_t *fti, pdb_sym_t *parent)
{
if ( inf_get_app_bitness() != 32 || parent == nullptr )
return false;
qstring funcname;
parent->get_name(&funcname);
// detect constructor
if ( fti->empty() || fti->cc != CM_CC_THISCALL || !fti->rettype.is_void() )
return false;
const auto &arg0 = fti->at(0);
if ( !arg0.type.is_ptr() )
return false;
tinfo_t class_type = arg0.type.get_pointed_object();
qstring class_name;
if ( !class_type.get_type_name(&class_name) )
return false;
qstring ctor_name;
ctor_name.sprnt("%s::%s", class_name.c_str(), class_name.c_str());
if ( ctor_name != funcname )
return false;
ddeb(("PDEB: detected constructor %s\n", funcname.c_str()));
// do not set FTI_CTOR, normally IDA ignores ctor/dtor return type
fti->flags &= ~FTI_CTOR;
fti->rettype = arg0.type;
return true;
}
//----------------------------------------------------------------------------
size_t til_builder_t::get_symbol_type_length(pdb_sym_t &sym) const
{
DWORD64 size = 0;
DWORD tag = 0;
sym.get_symTag(&tag);
if ( tag == SymTagData )
{
pdb_sym_t *pType = pdb_access->create_sym();
pdb_sym_janitor_t janitor_pType(pType);
if ( sym.get_type(pType) == S_OK )
pType->get_length(&size);
}
else
{
sym.get_length(&size);
}
return size_t(size);
}
//----------------------------------------------------------------------
cvt_code_t til_builder_t::convert_basetype(
tpinfo_t *out,
DWORD baseType,
int size) const
{
type_t bt = BTF_TYPEDEF;
const char *name = nullptr;
switch ( baseType )
{
case btNoType:
out->is_notype = true;
// Fallthrough.
default:
case 0x12c304: // "impdir_entry" (guessed)
case btBCD:
case btBit:
return cvt_failed;
case btVoid:
bt = BTF_VOID;
break;
case btChar:
bt = BT_INT8|BTMT_CHAR;
break;
case btBool:
bt = BT_BOOL;
if ( size != inf_get_cc_size_b() )
{
switch ( size )
{
case 1:
bt |= BTMT_BOOL1;
break;
case 2:
if ( inf_is_64bit() )
goto MAKE_INT; // 64bit apps do not have BOOL2
bt |= BTMT_BOOL2;
break;
case 4:
bt |= BTMT_BOOL4;
break;
case 8:
if ( !inf_is_64bit() )
goto MAKE_INT; // 32bit apps do not have BOOL8
bt |= BTMT_BOOL8;
break;
default:
// can't make this bool size; make an int
goto MAKE_INT;
}
}
break;
MAKE_INT:
case btInt:
case btLong:
bt = get_scalar_bt(size);
if ( bt == BT_UNK )
return cvt_failed;
break;
case btUInt:
case btULong:
if ( size == 1 )
{
bt = BTF_UCHAR; // get_scalar_bt returns 'char', or'ing it with BTMT_USIGNED
// does not help
}
else
{
bt = get_scalar_bt(size);
if ( bt == BT_UNK )
return cvt_failed;
bt |= BTMT_USIGNED;
}
break;
case btFloat:
if ( size == pv.ph.sizeof_ldbl() )
{
bt = BTMT_LNGDBL;
}
else
{
switch ( size )
{
case 4: bt = BTMT_FLOAT; break;
default:
case 8: bt = BTMT_DOUBLE; break;
case 10: bt = BTMT_SPECFLT; break;
}
}
bt |= BT_FLOAT;
break;
case btWChar: name = "wchar_t"; break;
case btBSTR: name = "BSTR"; break;
case btHresult: name = "HRESULT"; break;
case btCurrency: name = "CURRENCY"; break;
case btVariant: name = "VARIANT"; break;
case btComplex: name = "complex"; break;
case btDate: name = "DATE"; break;
}
if ( name != nullptr )
{
out->type.create_typedef(ti, name);
return cvt_typedef;
}
else
{
out->type = tinfo_t(bt);
return cvt_ok;
}
}
//----------------------------------------------------------------------
bool til_builder_t::retrieve_arguments(
pdb_sym_t &_sym,
func_type_data_t &fi,
pdb_sym_t *funcSym)
{
struct type_name_collector_t : public pdb_access_t::children_visitor_t
{
func_type_data_t &fi;
til_builder_t *tb;
til_t *ti;
HRESULT visit_child(pdb_sym_t &sym) override
{
// check that it's a parameter
DWORD dwDataKind;
if ( sym.get_dataKind(&dwDataKind) == S_OK
&& dwDataKind != DataIsParam
&& dwDataKind != DataIsObjectPtr )
{
return S_OK;
}
tpinfo_t tpi;
bool cvt_succeeded = tb->retrieve_type(&tpi, sym, parent);
if ( cvt_succeeded || tpi.is_notype )
{
funcarg_t &arg = fi.push_back();
arg.type = tpi.type;
sym.get_name(&arg.name);
}
return S_OK;
}
type_name_collector_t(til_t *_ti, til_builder_t *_tb, func_type_data_t &_fi)
: fi(_fi), tb(_tb), ti(_ti) {}
};
fi.clear();
type_name_collector_t pp(ti, this, fi);
HRESULT hr = pdb_access->iterate_children(_sym, SymTagNull, pp);
if ( hr == S_OK && funcSym != nullptr )
{
// get parameter names from the function symbol
func_type_data_t args;
args.flags = 0;
enum_function_args(*funcSym, args);
// QASSERT(497, args.empty() || args.size() == fi.size() );
bool custom_cc = false;
for ( int i = 0; i < fi.size(); i++ )
{
if ( i < args.size() )
{
if ( fi[i].name.empty() )
fi[i].name = args[i].name;
argloc_t &cur_argloc = args[i].argloc;
fi[i].argloc = cur_argloc;
if ( !custom_cc && cur_argloc.is_reg1() )
{
if ( is_intel386(pdb_access->get_machine_type()) )
{
if ( (fi.cc == CM_CC_FASTCALL
|| fi.cc == CM_CC_SWIFT) // FIXME
&& cur_argloc.regoff() == 0
&& (cur_argloc.reg1() == R_cx && i == 0
|| cur_argloc.reg1() == R_dx && i == 1) )
{
// ignore ecx and edx for fastcall
}
else if ( fi.cc == CM_CC_THISCALL
&& cur_argloc.regoff() == 0
&& cur_argloc.reg1() == R_cx && i == 0 )
{
// ignore ecx for thiscall
}
else
{
custom_cc = true;
}
}
}
//ask_for_feedback("pdb: register arguments are not supported for machine type %d", machine_type);
}
}
if ( custom_cc )
{
// we have some register params; need to convert function to custom cc
CASSERT(is_purging_cc(CM_CC_THISCALL));
CASSERT(is_purging_cc(CM_CC_FASTCALL));
fi.cc = is_purging_cc(fi.cc) ? CM_CC_SPECIALP : CM_CC_SPECIAL;
}
return true;
}
return false;
}
//----------------------------------------------------------------------
cm_t til_builder_t::convert_cc(DWORD cc0) const
{
switch ( cc0 )
{
case CV_CALL_GENERIC :
case CV_CALL_NEAR_C :
case CV_CALL_FAR_C :
return inf_is_64bit() ? CM_CC_FASTCALL : CM_CC_CDECL;
case CV_CALL_NEAR_PASCAL:
case CV_CALL_FAR_PASCAL : return CM_CC_PASCAL;
case CV_CALL_NEAR_FAST :
case CV_CALL_FAR_FAST : return CM_CC_FASTCALL;
// case CV_CALL_SKIPPED :
case CV_CALL_NEAR_STD :
case CV_CALL_FAR_STD :
case CV_CALL_ARMCALL : return CM_CC_STDCALL;
case CV_CALL_THISCALL : return CM_CC_THISCALL;
// case CV_CALL_NEAR_SYS :
// case CV_CALL_FAR_SYS :
// case CV_CALL_MIPSCALL :
// case CV_CALL_ALPHACALL :
// case CV_CALL_PPCCALL :
// case CV_CALL_SHCALL :
// case CV_CALL_ARMCALL :
// case CV_CALL_AM33CALL :
// case CV_CALL_TRICALL :
// case CV_CALL_SH5CALL :
// case CV_CALL_M32RCALL :
}
return CM_CC_UNKNOWN;
}
//----------------------------------------------------------------------
bool til_builder_t::get_variant_string_value(qstring *out, pdb_sym_t &sym) const
{
bool ok = false;
VARIANT value;
VariantInit(&value);
if ( sym.get_value(&value) == S_OK )
{
if ( value.vt == VT_BSTR )
{
utf16_utf8(out, (wchar16_t*) value.bstrVal);
ok = true;
}
else if ( value.vt == VT_LPSTR )
{
qstring str((const char *)value.byref);
out->swap(str);
ok = true;
}
}
VariantClear(&value);
return ok;
}
//----------------------------------------------------------------------
uint32 til_builder_t::get_variant_long_value(pdb_sym_t &sym) const
{
uint32 v = 0;
VARIANT value;
VariantInit(&value);
if ( sym.get_value(&value) == S_OK )
{
switch ( value.vt )
{
case VT_I1: v = value.cVal; break;
case VT_I2: v = value.iVal; break;
case VT_I4: v = value.lVal; break;
case VT_I8: v = value.llVal; break;
case VT_INT: v = value.intVal; break;
case VT_UI1: v = value.bVal; break;
case VT_UI2: v = value.uiVal; break;
case VT_UI4: v = value.ulVal; break;
case VT_UI8: v = value.ullVal; break;
case VT_UINT: v = value.uintVal; break;
default:
ask_for_feedback("pdb: unsupported VARIANT type %d", value.vt);
break;
}
}
VariantClear(&value);
return v;
}
//----------------------------------------------------------------------
// funcSym is Function, typeSym is FunctionType
bool til_builder_t::is_member_func(tinfo_t *class_type, pdb_sym_t &typeSym, pdb_sym_t *funcSym)
{
// make sure we retrieve class type first
pdb_sym_t *pParent = pdb_access->create_sym();
pdb_sym_janitor_t janitor_pParent(pParent);
if ( typeSym.get_classParent(pParent) != S_OK || pParent->empty() )
return false;
tpinfo_t tpi;
if ( !retrieve_type(&tpi, *pParent, nullptr) )
return false; // failed to retrieve the parent's type
class_type->swap(tpi.type);
// then check if it's static
if ( funcSym != nullptr
&& pdb_access->get_dia_version() >= 800 )
{
BOOL bIsStatic = false;
HRESULT hr = funcSym->get_isStatic(&bIsStatic);
if ( hr == S_OK )
return !bIsStatic;
}
return true;
}
//----------------------------------------------------------------------
bool til_builder_t::is_stack_reg(int reg) const
{
return reg == get_stack_reg(pdb_access->get_machine_type());
}
//----------------------------------------------------------------------
bool til_builder_t::is_frame_reg(int reg) const
{
if (pdb_access->get_dia_version() >= 1400 && is_intel386(pdb_access->get_machine_type()))
{
return reg == CV_ALLREG_VFRAME || reg == CV_REG_EBP;
}
return reg == get_frame_reg(pdb_access->get_machine_type());
}
//----------------------------------------------------------------------------
int til_builder_t::get_symbol_funcarg_info(
funcarg_t *out,
pdb_sym_t &sym,
DWORD /*dwDataKind*/,
DWORD locType,
int stack_off)
{
sym.get_name(&out->name);
tpinfo_t tpi;
get_symbol_type(&tpi, sym);
out->type = tpi.type;
if ( locType == LocIsEnregistered )
{
DWORD dwReg;
if ( sym.get_registerId(&dwReg) == S_OK )
{
if ( enregistered_bug && dwReg > 0 )
dwReg--;
qstring regname;
print_pdb_register(®name, pdb_access->get_machine_type(), dwReg);
out->argloc._set_reg1(str2reg(regname.c_str()));
}
}
else if ( locType == LocIsRegRel )
{
DWORD dwReg;
LONG lOffset;
if ( sym.get_registerId(&dwReg) == S_OK
&& sym.get_offset(&lOffset) == S_OK
&& (is_frame_reg(dwReg) || is_stack_reg(dwReg)) )
{
uint32 align;
out->argloc._set_stkoff(stack_off);
size_t argsz = out->type.get_size(&align);
if ( align > argsz )
argsz = align;
stack_off += argsz;
}
}
else
{
ask_for_feedback("pdb: unsupported location type %d", locType);
}
return stack_off;
}
//----------------------------------------------------------------------
void til_builder_t::enum_function_args(pdb_sym_t &_sym, func_type_data_t &args)
{
// enumerate all function parameters and gather their names
struct param_enumerator_t : public pdb_access_t::children_visitor_t
{
func_type_data_t &args;
til_builder_t *tb;
int stack_off;
virtual HRESULT visit_child(pdb_sym_t &sym) override
{
DWORD tag = 0;
HRESULT hr = sym.get_symTag(&tag);
if ( FAILED(hr) )
return hr;
switch ( tag )
{
case SymTagBlock: // nested blocks
return tb->pdb_access->iterate_children(sym, SymTagNull, *this);
case SymTagFuncDebugStart:
case SymTagFuncDebugEnd:
return S_OK; // ignore these for the moment
}
DWORD dwDataKind, locType;
if ( sym.get_dataKind(&dwDataKind) == S_OK
&& dwDataKind == DataIsParam
&& sym.get_locationType(&locType) == S_OK )
{
funcarg_t &fa = args.push_back();
stack_off = tb->get_symbol_funcarg_info(&fa, sym, dwDataKind, locType, stack_off);
}
return S_OK; // continue enumeration
}
param_enumerator_t(func_type_data_t &_args, til_builder_t *_tb)
: args(_args), tb(_tb), stack_off(0) {}
};
param_enumerator_t pen(args, this);
pdb_access->iterate_children(_sym, SymTagData, pen);
}
//----------------------------------------------------------------------
// corrupted PDB may produce the strange results
cvt_code_t til_builder_t::verify_struct(pdb_udt_type_data_t &udt) const
{
for ( auto &udm : udt )
{
if ( !udm.is_bitfield() && (udm.offset % 8) != 0 )
return cvt_failed;
}
return cvt_ok;
}
//----------------------------------------------------------------------
bool til_builder_t::verify_union_stem(pdb_udt_type_data_t &udt) const
{
bool udt_fixed = false;
// at the moment there is an issue with structure with bit fields only
if ( udt.size() < 2 )
return udt_fixed;
for ( const pdb_udm_t &udm : udt )
{
if ( !udm.is_bitfield() )
return udt_fixed;
}
// stem like:
// 0 unsigned __int8 : 7 ZZ1 bit_offset 0
// 8 unsigned __int32 : 24 AllFlags bit_offset 8
// is collected wrongly and should be fixed.
// There is a bit_offset 8 inside the bitfield group,
// so we need to expand the type of field ZZ1
dump_pdb_udt(udt, "verify_union_stem");
bool udm_fixed;
do
{
udm_fixed = false;
for ( size_t i=0; i < udt.size()-1; ++i )
{
pdb_udm_t &udm0 = udt[i];
const pdb_udm_t &udm1 = udt[i+1];
size_t typsz0 = udm0.type.get_size();
size_t typsz1 = udm1.type.get_size();
if ( udm0.bit_offset < udm1.bit_offset
&& udm0.offset + udm0.size <= udm1.offset
&& typsz0 < typsz1 )
{
bitfield_type_data_t bi;
udm0.type.get_bitfield_details(&bi);
bi.nbytes = typsz1;
udm0.type.create_bitfield(bi);
udt_fixed = true;
udm_fixed = true;
}
}
} while ( udm_fixed );
if ( udt_fixed )
dump_pdb_udt(udt, "verify_union_stem FIXED");
return udt_fixed;
}
//----------------------------------------------------------------------
// verify unions that would be created out of [p1, p2) members.
// The [p1, p2) members are spoiled by the function.
// Create substructures if necessary. Returns the result in out (can be the same
// vector as [p1, p2)
cvt_code_t til_builder_t::verify_union(
pdb_udt_type_data_t *out,
pdb_udt_type_data_t::iterator p1,
pdb_udt_type_data_t::const_iterator p2) const
{
if ( p1 == p2 )
return cvt_ok;
QASSERT(498, p2 > p1);
uint64 off = p1->offset;
typedef qvector<pdb_udt_type_data_t> stems_t;
stems_t stems; // each stem is a member of the future union
for ( pdb_udt_type_data_t::iterator q=p1; q != p2; ++q )
{
pdb_udt_type_data_t *best = nullptr;
q->offset -= off;
if ( q->offset != 0 )
{ // find best suited stem: the one with end() closest to our offset
uint64 bestend = 0;
for ( stems_t::iterator s=stems.begin(); s != stems.end(); ++s )
{
pdb_udt_type_data_t &sm = *s;
pdb_udm_t &lastmem = sm.back();
uint64 smend = lastmem.end();
if ( (lastmem.is_bitfield() == q->is_bitfield() || q->bit_offset == 0)
&& smend <= q->begin()
&& (best == nullptr || bestend < smend) )
{
best = &sm;
bestend = smend;
}
}
}
if ( best == nullptr )
best = &stems.push_back();
uint64 qend;
if ( q->is_bitfield() )
{
bitfield_type_data_t bi;
q->type.get_bitfield_details(&bi);
size_t size = bi.nbytes * 8;
QASSERT(30385, size == 8 || size == 16 || size == 32 || size == 64);
qend = q->offset - q->bit_offset + size;
}
else
{
qend = q->offset + q->size + 7;
}
qend /= 8;
if ( best->total_size < qend )
best->total_size = qend;
qswap(best->push_back(), *q);
}
// the stems are created artificially
// and some of them need to be fixed
// to prevent structure alignment issues
for ( stems_t::iterator s=stems.begin(); s != stems.end(); ++s )
{
if ( s->size() == 1 && s->begin()->offset == 0 && !s->begin()->is_bitfield() )
continue;
verify_union_stem(*s);
}
// all non-trivial stems must be converted to structures
for ( stems_t::iterator s=stems.begin(); s != stems.end(); ++s )
{
if ( s->size() == 1 && s->begin()->offset == 0 && !s->begin()->is_bitfield() )
continue;
#ifdef PDEB
msg("CREATE STEM total_size %" FMT_Z "\n", s->total_size);
for ( pdb_udt_type_data_t::iterator p=s->begin(); p != s->end(); ++p )
msg(" %" FMT_64 "x %s %s bit_offset %u\n", p->offset, p->type.dstr(), p->name.c_str(), p->bit_offset);
#endif
if ( verify_struct(*s) != cvt_ok )
return cvt_failed;
tinfo_t tif;
int total_size = s->total_size;
cvt_code_t code = create_udt_ref(&tif, s, UdtStruct);
if ( code != cvt_ok )
return code;
s->resize(1);
pdb_udm_t &sm = s->front();
sm.offset = 0;
sm.size = uint64(total_size) * 8;
sm.name.sprnt("__s%u", uint(s-stems.begin()));
sm.type = tif;
}
// collect the results
out->resize(stems.size());
for ( int i=0; i < stems.size(); i++ )
{
QASSERT(499, stems[i].size() == 1);
qswap(out->at(i), *stems[i].begin());
}
return cvt_ok;
}
//----------------------------------------------------------------------
// create a union out of [p1, p2) members. they are spoiled by the function.
// returns type of the new union and its fields
// this function also creates substructures if necessary
cvt_code_t til_builder_t::create_union(
tinfo_t *out,
size_t *p_total_size,
pdb_udt_type_data_t::iterator p1,
pdb_udt_type_data_t::const_iterator p2) const
{
#ifdef PDEB
msg("CREATE UNION\n");
for ( pdb_udt_type_data_t::iterator p=p1; p != p2; ++p )
msg(" %" FMT_64 "x %s %s bit_offset %u\n", p->offset, p->type.dstr(), p->name.c_str(), p->bit_offset);
#endif
pdb_udt_type_data_t unimems;
cvt_code_t code = verify_union(&unimems, p1, p2);
if ( code != cvt_ok )
return code;
// calculate the total size
for ( int i=0; i < unimems.size(); i++ )
{
pdb_udm_t &udm = unimems[i];
size_t nbytes = (udm.end() + 7) / 8;
if ( nbytes > unimems.total_size )
unimems.total_size = nbytes;
}
if ( p_total_size != nullptr )
*p_total_size = unimems.total_size;
return create_udt_ref(out, &unimems, UdtUnion);
}
//----------------------------------------------------------------------
inline void ida_vft_name(qstring *vftn, const char *ns, uint32_t offset=0)
{
qstring new_vft_name(ns);
if ( offset != 0 )
new_vft_name.cat_sprnt("_%04X", offset);
new_vft_name.append(VTBL_SUFFIX);
vftn->swap(new_vft_name);
}
//----------------------------------------------------------------------
#define MS_VTBL_SUFFIX "Vtbl"
inline void ms_vft_name(qstring *vftn, const char *ns)
{
qstring new_vft_name(ns);
new_vft_name.append(MS_VTBL_SUFFIX);
vftn->swap(new_vft_name);
}
//----------------------------------------------------------------------
inline bool is_ms_vft_name(const qstring &udt_name)
{
size_t len = udt_name.length();
return len > sizeof(MS_VTBL_SUFFIX)
&& streq(udt_name.begin() + (len - sizeof(MS_VTBL_SUFFIX) + 1), MS_VTBL_SUFFIX);
}
//----------------------------------------------------------------------
inline void ida_vft_name_from_ms(qstring *ivftnm, const qstring &msvftnm)
{
qstring tmp(msvftnm);
if ( is_ms_vft_name(msvftnm) )
tmp.remove(tmp.length()-4, 4);
ida_vft_name(ivftnm, tmp.c_str());
}
//----------------------------------------------------------------------
bool til_builder_t::get_vft_name(qstring *vftn, uint32 *p_ord, const char *ns, uint32_t offset)
{
bool vft_creating = false;
qstring new_vft_name;
// check for MS vftable
ms_vft_name(&new_vft_name, ns);
uint32 ord = get_type_ordinal(ti, new_vft_name.c_str());
if ( ord == 0 )
{
// maybe creating?
vft_creating = creating.find(new_vft_name.c_str()) != creating.end();
if ( !vft_creating )
{
ida_vft_name(&new_vft_name, ns, offset);
ord = get_type_ordinal(ti, new_vft_name.c_str());
}
}
vftn->swap(new_vft_name);
if ( p_ord != nullptr )
*p_ord = ord;
return vft_creating;
}
//----------------------------------------------------------------------
void pdb_udt_type_data_t::convert_to_tinfo_udt(udt_type_data_t *out)
{
out->total_size = total_size;
out->taudt_bits = taudt_bits;
out->is_union = is_union;
out->reserve(size());
for ( size_t i = 0; i < size(); i++ )
{
udm_t &udm = at(i);
#ifdef PDEB
out->push_back() = udm;
#else
out->push_back().swap(udm);
#endif
}
}
//----------------------------------------------------------------------
// insert si into the destination type
inline void merge_vft_udm(int *j, udt_type_data_t *dst_udt, const udm_t &si, bool replace)
{
bool insert_src = true;
for ( ; *j < dst_udt->size(); (*j)++ )
{
udm_t &dj = (*dst_udt)[*j];
if ( dj.offset + dj.size <= si.offset )
continue;
if ( dj.offset >= si.offset + si.size )
break; // should insert before dj
// Looks like an overlap,
// fields may differ in type and name only.
// Ignore "__vecDelDtor",
// this often happens when virtual class::~class
// is later redefined as __vecDelDtor()
if ( replace && si.name != "__vecDelDtor"
|| dj.name == "__vecDelDtor" )
{
dj.type = si.type;
dj.name = si.name;
}
insert_src = false;
break;
}
if ( insert_src )
dst_udt->insert(dst_udt->begin()+*j, si);
}
//----------------------------------------------------------------------
// merge two vftables into one
// dst_udt gets all fields of srctype in addition to its own fields.
// dst_udt preserves or overrides the coinciding field.
static void merge_vftables(udt_type_data_t *dst_udt, const tinfo_t &srcvft, bool replace)
{
udt_type_data_t src_udt;
if ( !srcvft.get_udt_details(&src_udt) )
{
deb(IDA_DEBUG_DBGINFO, "PDB: failed to merge type '%s' to vftable\n", srcvft.dstr());
#if defined(TESTABLE_BUILD) && !defined(__FUZZER__)
INTERR(30585);
#else
return;
#endif
}
int j(0);
for ( const auto &si : src_udt )
merge_vft_udm(&j, dst_udt, si, replace);
dst_udt->total_size = (dst_udt->back().end() + 7) / 8;
}
//----------------------------------------------------------------------
static void add_vftable_member(
udt_type_data_t *dst_udt,
const tinfo_t &member,
const char *name,
DWORD vfptr_offset)
{
tinfo_t ptr_member;
ptr_member.create_ptr(member); // the field is a pointer to function
asize_t size = ptr_member.get_size();
udm_t udm;
udm.offset = uint64(vfptr_offset) * 8;
udm.size = uint64(size) * 8;
udm.type = ptr_member;
udm.effalign = size;
udm.name = name;
int j(0);
merge_vft_udm(&j, dst_udt, udm, true);
dst_udt->total_size = (dst_udt->back().end() + 7) / 8;
}
//----------------------------------------------------------------------
inline bool get_vfptr_offset(DWORD *vfptr_offset, pdb_sym_t &sym)
{
BOOL is_virtual;
return sym.get_virtual(&is_virtual) == S_OK
&& is_virtual
&& sym.get_virtualBaseOffset(vfptr_offset) == S_OK;
}
//----------------------------------------------------------------------
// enumerate virtual functions of class sym and create a vtable structure
// with function pointers
cvt_code_t til_builder_t::make_vtable_struct(tinfo_t *out, pdb_sym_t &_sym)
{
struct virtual_func_visitor_t : public pdb_access_t::children_visitor_t
{