-
Notifications
You must be signed in to change notification settings - Fork 12.8k
/
Copy pathDWARFASTParserClang.cpp
3881 lines (3339 loc) · 138 KB
/
DWARFASTParserClang.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
//===-- DWARFASTParserClang.cpp -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include <cstdlib>
#include "DWARFASTParser.h"
#include "DWARFASTParserClang.h"
#include "DWARFDebugInfo.h"
#include "DWARFDeclContext.h"
#include "DWARFDefines.h"
#include "SymbolFileDWARF.h"
#include "SymbolFileDWARFDebugMap.h"
#include "SymbolFileDWARFDwo.h"
#include "UniqueDWARFASTType.h"
#include "Plugins/ExpressionParser/Clang/ClangASTImporter.h"
#include "Plugins/ExpressionParser/Clang/ClangASTMetadata.h"
#include "Plugins/ExpressionParser/Clang/ClangUtil.h"
#include "Plugins/Language/ObjC/ObjCLanguage.h"
#include "lldb/Core/Module.h"
#include "lldb/Core/Value.h"
#include "lldb/Host/Host.h"
#include "lldb/Symbol/CompileUnit.h"
#include "lldb/Symbol/Function.h"
#include "lldb/Symbol/ObjectFile.h"
#include "lldb/Symbol/SymbolFile.h"
#include "lldb/Symbol/TypeList.h"
#include "lldb/Symbol/TypeMap.h"
#include "lldb/Symbol/VariableList.h"
#include "lldb/Target/Language.h"
#include "lldb/Utility/LLDBAssert.h"
#include "lldb/Utility/Log.h"
#include "lldb/Utility/StreamString.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclObjC.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/Type.h"
#include "clang/Basic/Specifiers.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/DebugInfo/DWARF/DWARFAddressRange.h"
#include "llvm/DebugInfo/DWARF/DWARFTypePrinter.h"
#include "llvm/Demangle/Demangle.h"
#include <map>
#include <memory>
#include <optional>
#include <vector>
//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
#ifdef ENABLE_DEBUG_PRINTF
#include <cstdio>
#define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
#else
#define DEBUG_PRINTF(fmt, ...)
#endif
using namespace lldb;
using namespace lldb_private;
using namespace lldb_private::dwarf;
using namespace lldb_private::plugin::dwarf;
DWARFASTParserClang::DWARFASTParserClang(TypeSystemClang &ast)
: DWARFASTParser(Kind::DWARFASTParserClang), m_ast(ast),
m_die_to_decl_ctx(), m_decl_ctx_to_die() {}
DWARFASTParserClang::~DWARFASTParserClang() = default;
static bool DeclKindIsCXXClass(clang::Decl::Kind decl_kind) {
switch (decl_kind) {
case clang::Decl::CXXRecord:
case clang::Decl::ClassTemplateSpecialization:
return true;
default:
break;
}
return false;
}
ClangASTImporter &DWARFASTParserClang::GetClangASTImporter() {
if (!m_clang_ast_importer_up) {
m_clang_ast_importer_up = std::make_unique<ClangASTImporter>();
}
return *m_clang_ast_importer_up;
}
/// Detect a forward declaration that is nested in a DW_TAG_module.
static bool IsClangModuleFwdDecl(const DWARFDIE &Die) {
if (!Die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0))
return false;
auto Parent = Die.GetParent();
while (Parent.IsValid()) {
if (Parent.Tag() == DW_TAG_module)
return true;
Parent = Parent.GetParent();
}
return false;
}
static DWARFDIE GetContainingClangModuleDIE(const DWARFDIE &die) {
if (die.IsValid()) {
DWARFDIE top_module_die;
// Now make sure this DIE is scoped in a DW_TAG_module tag and return true
// if so
for (DWARFDIE parent = die.GetParent(); parent.IsValid();
parent = parent.GetParent()) {
const dw_tag_t tag = parent.Tag();
if (tag == DW_TAG_module)
top_module_die = parent;
else if (tag == DW_TAG_compile_unit || tag == DW_TAG_partial_unit)
break;
}
return top_module_die;
}
return DWARFDIE();
}
static lldb::ModuleSP GetContainingClangModule(const DWARFDIE &die) {
if (die.IsValid()) {
DWARFDIE clang_module_die = GetContainingClangModuleDIE(die);
if (clang_module_die) {
const char *module_name = clang_module_die.GetName();
if (module_name)
return die.GetDWARF()->GetExternalModule(
lldb_private::ConstString(module_name));
}
}
return lldb::ModuleSP();
}
// Returns true if the given artificial field name should be ignored when
// parsing the DWARF.
static bool ShouldIgnoreArtificialField(llvm::StringRef FieldName) {
return FieldName.starts_with("_vptr$")
// gdb emit vtable pointer as "_vptr.classname"
|| FieldName.starts_with("_vptr.");
}
/// Returns true for C++ constructs represented by clang::CXXRecordDecl
static bool TagIsRecordType(dw_tag_t tag) {
switch (tag) {
case DW_TAG_class_type:
case DW_TAG_structure_type:
case DW_TAG_union_type:
return true;
default:
return false;
}
}
/// Get the object parameter DIE if one exists, otherwise returns
/// a default DWARFDIE. If \c containing_decl_ctx is not a valid
/// C++ declaration context for class methods, assume no object
/// parameter exists for the given \c subprogram.
static DWARFDIE
GetCXXObjectParameter(const DWARFDIE &subprogram,
const clang::DeclContext &containing_decl_ctx) {
assert(subprogram.Tag() == DW_TAG_subprogram ||
subprogram.Tag() == DW_TAG_inlined_subroutine ||
subprogram.Tag() == DW_TAG_subroutine_type);
if (!DeclKindIsCXXClass(containing_decl_ctx.getDeclKind()))
return {};
if (DWARFDIE object_parameter =
subprogram.GetAttributeValueAsReferenceDIE(DW_AT_object_pointer))
return object_parameter;
// If no DW_AT_object_pointer was specified, assume the implicit object
// parameter is the first parameter to the function, is called "this" and is
// artificial (which is what most compilers would generate).
auto children = subprogram.children();
auto it = llvm::find_if(children, [](const DWARFDIE &child) {
return child.Tag() == DW_TAG_formal_parameter;
});
if (it == children.end())
return {};
DWARFDIE object_pointer = *it;
if (!object_pointer.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
return {};
// Often times compilers omit the "this" name for the
// specification DIEs, so we can't rely upon the name being in
// the formal parameter DIE...
if (const char *name = object_pointer.GetName();
name && ::strcmp(name, "this") != 0)
return {};
return object_pointer;
}
/// In order to determine the CV-qualifiers for a C++ class
/// method in DWARF, we have to look at the CV-qualifiers of
/// the object parameter's type.
static unsigned GetCXXMethodCVQuals(const DWARFDIE &subprogram,
const DWARFDIE &object_parameter) {
if (!subprogram || !object_parameter)
return 0;
Type *this_type = subprogram.ResolveTypeUID(
object_parameter.GetAttributeValueAsReferenceDIE(DW_AT_type));
if (!this_type)
return 0;
uint32_t encoding_mask = this_type->GetEncodingMask();
unsigned cv_quals = 0;
if (encoding_mask & (1u << Type::eEncodingIsConstUID))
cv_quals |= clang::Qualifiers::Const;
if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
cv_quals |= clang::Qualifiers::Volatile;
return cv_quals;
}
TypeSP DWARFASTParserClang::ParseTypeFromClangModule(const SymbolContext &sc,
const DWARFDIE &die,
Log *log) {
ModuleSP clang_module_sp = GetContainingClangModule(die);
if (!clang_module_sp)
return TypeSP();
// If this type comes from a Clang module, recursively look in the
// DWARF section of the .pcm file in the module cache. Clang
// generates DWO skeleton units as breadcrumbs to find them.
std::vector<lldb_private::CompilerContext> die_context = die.GetDeclContext();
TypeQuery query(die_context, TypeQueryOptions::e_module_search |
TypeQueryOptions::e_find_one);
TypeResults results;
// The type in the Clang module must have the same language as the current CU.
query.AddLanguage(SymbolFileDWARF::GetLanguageFamily(*die.GetCU()));
clang_module_sp->FindTypes(query, results);
TypeSP pcm_type_sp = results.GetTypeMap().FirstType();
if (!pcm_type_sp) {
// Since this type is defined in one of the Clang modules imported
// by this symbol file, search all of them. Instead of calling
// sym_file->FindTypes(), which would return this again, go straight
// to the imported modules.
auto &sym_file = die.GetCU()->GetSymbolFileDWARF();
// Well-formed clang modules never form cycles; guard against corrupted
// ones by inserting the current file.
results.AlreadySearched(&sym_file);
sym_file.ForEachExternalModule(
*sc.comp_unit, results.GetSearchedSymbolFiles(), [&](Module &module) {
module.FindTypes(query, results);
pcm_type_sp = results.GetTypeMap().FirstType();
return (bool)pcm_type_sp;
});
}
if (!pcm_type_sp)
return TypeSP();
// We found a real definition for this type in the Clang module, so lets use
// it and cache the fact that we found a complete type for this die.
lldb_private::CompilerType pcm_type = pcm_type_sp->GetForwardCompilerType();
lldb_private::CompilerType type =
GetClangASTImporter().CopyType(m_ast, pcm_type);
if (!type)
return TypeSP();
// Under normal operation pcm_type is a shallow forward declaration
// that gets completed later. This is necessary to support cyclic
// data structures. If, however, pcm_type is already complete (for
// example, because it was loaded for a different target before),
// the definition needs to be imported right away, too.
// Type::ResolveClangType() effectively ignores the ResolveState
// inside type_sp and only looks at IsDefined(), so it never calls
// ClangASTImporter::ASTImporterDelegate::ImportDefinitionTo(),
// which does extra work for Objective-C classes. This would result
// in only the forward declaration to be visible.
if (pcm_type.IsDefined())
GetClangASTImporter().RequireCompleteType(ClangUtil::GetQualType(type));
SymbolFileDWARF *dwarf = die.GetDWARF();
auto type_sp = dwarf->MakeType(
die.GetID(), pcm_type_sp->GetName(), pcm_type_sp->GetByteSize(nullptr),
nullptr, LLDB_INVALID_UID, Type::eEncodingInvalid,
&pcm_type_sp->GetDeclaration(), type, Type::ResolveState::Forward,
TypePayloadClang(GetOwningClangModule(die)));
clang::TagDecl *tag_decl = TypeSystemClang::GetAsTagDecl(type);
if (tag_decl) {
LinkDeclContextToDIE(tag_decl, die);
} else {
clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
if (defn_decl_ctx)
LinkDeclContextToDIE(defn_decl_ctx, die);
}
return type_sp;
}
/// This function ensures we are able to add members (nested types, functions,
/// etc.) to this type. It does so by starting its definition even if one cannot
/// be found in the debug info. This means the type may need to be "forcibly
/// completed" later -- see CompleteTypeFromDWARF).
static void PrepareContextToReceiveMembers(TypeSystemClang &ast,
ClangASTImporter &ast_importer,
clang::DeclContext *decl_ctx,
DWARFDIE die,
const char *type_name_cstr) {
auto *tag_decl_ctx = clang::dyn_cast<clang::TagDecl>(decl_ctx);
if (!tag_decl_ctx)
return; // Non-tag context are always ready.
// We have already completed the type or it is already prepared.
if (tag_decl_ctx->isCompleteDefinition() || tag_decl_ctx->isBeingDefined())
return;
// If this tag was imported from another AST context (in the gmodules case),
// we can complete the type by doing a full import.
// If this type was not imported from an external AST, there's nothing to do.
CompilerType type = ast.GetTypeForDecl(tag_decl_ctx);
if (type && ast_importer.CanImport(type)) {
auto qual_type = ClangUtil::GetQualType(type);
if (ast_importer.RequireCompleteType(qual_type))
return;
die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
"Unable to complete the Decl context for DIE {0} at offset "
"{1:x16}.\nPlease file a bug report.",
type_name_cstr ? type_name_cstr : "", die.GetOffset());
}
// We don't have a type definition and/or the import failed, but we need to
// add members to it. Start the definition to make that possible. If the type
// has no external storage we also have to complete the definition. Otherwise,
// that will happen when we are asked to complete the type
// (CompleteTypeFromDWARF).
ast.StartTagDeclarationDefinition(type);
if (!tag_decl_ctx->hasExternalLexicalStorage()) {
ast.SetDeclIsForcefullyCompleted(tag_decl_ctx);
ast.CompleteTagDeclarationDefinition(type);
}
}
ParsedDWARFTypeAttributes::ParsedDWARFTypeAttributes(const DWARFDIE &die) {
DWARFAttributes attributes = die.GetAttributes();
for (size_t i = 0; i < attributes.Size(); ++i) {
dw_attr_t attr = attributes.AttributeAtIndex(i);
DWARFFormValue form_value;
if (!attributes.ExtractFormValueAtIndex(i, form_value))
continue;
switch (attr) {
default:
break;
case DW_AT_abstract_origin:
abstract_origin = form_value;
break;
case DW_AT_accessibility:
accessibility =
DWARFASTParser::GetAccessTypeFromDWARF(form_value.Unsigned());
break;
case DW_AT_artificial:
is_artificial = form_value.Boolean();
break;
case DW_AT_bit_stride:
bit_stride = form_value.Unsigned();
break;
case DW_AT_byte_size:
byte_size = form_value.Unsigned();
break;
case DW_AT_alignment:
alignment = form_value.Unsigned();
break;
case DW_AT_byte_stride:
byte_stride = form_value.Unsigned();
break;
case DW_AT_calling_convention:
calling_convention = form_value.Unsigned();
break;
case DW_AT_containing_type:
containing_type = form_value;
break;
case DW_AT_decl_file:
// die.GetCU() can differ if DW_AT_specification uses DW_FORM_ref_addr.
decl.SetFile(
attributes.CompileUnitAtIndex(i)->GetFile(form_value.Unsigned()));
break;
case DW_AT_decl_line:
decl.SetLine(form_value.Unsigned());
break;
case DW_AT_decl_column:
decl.SetColumn(form_value.Unsigned());
break;
case DW_AT_declaration:
is_forward_declaration = form_value.Boolean();
break;
case DW_AT_encoding:
encoding = form_value.Unsigned();
break;
case DW_AT_enum_class:
is_scoped_enum = form_value.Boolean();
break;
case DW_AT_explicit:
is_explicit = form_value.Boolean();
break;
case DW_AT_external:
if (form_value.Unsigned())
storage = clang::SC_Extern;
break;
case DW_AT_inline:
is_inline = form_value.Boolean();
break;
case DW_AT_linkage_name:
case DW_AT_MIPS_linkage_name:
mangled_name = form_value.AsCString();
break;
case DW_AT_name:
name.SetCString(form_value.AsCString());
break;
case DW_AT_object_pointer:
// GetAttributes follows DW_AT_specification.
// DW_TAG_subprogram definitions and declarations may both
// have a DW_AT_object_pointer. Don't overwrite the one
// we parsed for the definition with the one from the declaration.
if (!object_pointer.IsValid())
object_pointer = form_value.Reference();
break;
case DW_AT_signature:
signature = form_value;
break;
case DW_AT_specification:
specification = form_value;
break;
case DW_AT_type:
type = form_value;
break;
case DW_AT_virtuality:
is_virtual = form_value.Boolean();
break;
case DW_AT_APPLE_objc_complete_type:
is_complete_objc_class = form_value.Signed();
break;
case DW_AT_APPLE_objc_direct:
is_objc_direct_call = true;
break;
case DW_AT_APPLE_runtime_class:
class_language = (LanguageType)form_value.Signed();
break;
case DW_AT_GNU_vector:
is_vector = form_value.Boolean();
break;
case DW_AT_export_symbols:
exports_symbols = form_value.Boolean();
break;
case DW_AT_rvalue_reference:
ref_qual = clang::RQ_RValue;
break;
case DW_AT_reference:
ref_qual = clang::RQ_LValue;
break;
case DW_AT_APPLE_enum_kind:
enum_kind = static_cast<clang::EnumExtensibilityAttr::Kind>(
form_value.Unsigned());
break;
}
}
}
static std::string GetUnitName(const DWARFDIE &die) {
if (DWARFUnit *unit = die.GetCU())
return unit->GetAbsolutePath().GetPath();
return "<missing DWARF unit path>";
}
TypeSP DWARFASTParserClang::ParseTypeFromDWARF(const SymbolContext &sc,
const DWARFDIE &die,
bool *type_is_new_ptr) {
if (type_is_new_ptr)
*type_is_new_ptr = false;
if (!die)
return nullptr;
Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
SymbolFileDWARF *dwarf = die.GetDWARF();
if (log) {
DWARFDIE context_die;
clang::DeclContext *context =
GetClangDeclContextContainingDIE(die, &context_die);
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"DWARFASTParserClang::ParseTypeFromDWARF "
"(die = {0:x16}, decl_ctx = {1:p} (die "
"{2:x16})) {3} ({4}) name = '{5}')",
die.GetOffset(), static_cast<void *>(context), context_die.GetOffset(),
DW_TAG_value_to_name(die.Tag()), die.Tag(), die.GetName());
}
// Set a bit that lets us know that we are currently parsing this
if (auto [it, inserted] =
dwarf->GetDIEToType().try_emplace(die.GetDIE(), DIE_IS_BEING_PARSED);
!inserted) {
if (it->getSecond() == nullptr || it->getSecond() == DIE_IS_BEING_PARSED)
return nullptr;
return it->getSecond()->shared_from_this();
}
ParsedDWARFTypeAttributes attrs(die);
TypeSP type_sp;
if (DWARFDIE signature_die = attrs.signature.Reference()) {
type_sp = ParseTypeFromDWARF(sc, signature_die, type_is_new_ptr);
if (type_sp) {
if (clang::DeclContext *decl_ctx =
GetCachedClangDeclContextForDIE(signature_die))
LinkDeclContextToDIE(decl_ctx, die);
}
} else {
if (type_is_new_ptr)
*type_is_new_ptr = true;
const dw_tag_t tag = die.Tag();
switch (tag) {
case DW_TAG_typedef:
case DW_TAG_base_type:
case DW_TAG_pointer_type:
case DW_TAG_reference_type:
case DW_TAG_rvalue_reference_type:
case DW_TAG_const_type:
case DW_TAG_restrict_type:
case DW_TAG_volatile_type:
case DW_TAG_LLVM_ptrauth_type:
case DW_TAG_atomic_type:
case DW_TAG_unspecified_type:
type_sp = ParseTypeModifier(sc, die, attrs);
break;
case DW_TAG_structure_type:
case DW_TAG_union_type:
case DW_TAG_class_type:
type_sp = ParseStructureLikeDIE(sc, die, attrs);
break;
case DW_TAG_enumeration_type:
type_sp = ParseEnum(sc, die, attrs);
break;
case DW_TAG_inlined_subroutine:
case DW_TAG_subprogram:
case DW_TAG_subroutine_type:
type_sp = ParseSubroutine(die, attrs);
break;
case DW_TAG_array_type:
type_sp = ParseArrayType(die, attrs);
break;
case DW_TAG_ptr_to_member_type:
type_sp = ParsePointerToMemberType(die, attrs);
break;
default:
dwarf->GetObjectFile()->GetModule()->ReportError(
"[{0:x16}]: unhandled type tag {1:x4} ({2}), "
"please file a bug and "
"attach the file at the start of this error message",
die.GetOffset(), tag, DW_TAG_value_to_name(tag));
break;
}
UpdateSymbolContextScopeForType(sc, die, type_sp);
}
if (type_sp) {
dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
}
return type_sp;
}
static std::optional<uint32_t>
ExtractDataMemberLocation(DWARFDIE const &die, DWARFFormValue const &form_value,
ModuleSP module_sp) {
Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
// With DWARF 3 and later, if the value is an integer constant,
// this form value is the offset in bytes from the beginning of
// the containing entity.
if (!form_value.BlockData())
return form_value.Unsigned();
Value initialValue(0);
const DWARFDataExtractor &debug_info_data = die.GetData();
uint32_t block_length = form_value.Unsigned();
uint32_t block_offset =
form_value.BlockData() - debug_info_data.GetDataStart();
llvm::Expected<Value> memberOffset = DWARFExpression::Evaluate(
/*ExecutionContext=*/nullptr,
/*RegisterContext=*/nullptr, module_sp,
DataExtractor(debug_info_data, block_offset, block_length), die.GetCU(),
eRegisterKindDWARF, &initialValue, nullptr);
if (!memberOffset) {
LLDB_LOG_ERROR(log, memberOffset.takeError(),
"ExtractDataMemberLocation failed: {0}");
return {};
}
return memberOffset->ResolveValue(nullptr).UInt();
}
static TypePayloadClang GetPtrAuthMofidierPayload(const DWARFDIE &die) {
auto getAttr = [&](llvm::dwarf::Attribute Attr, unsigned defaultValue = 0) {
return die.GetAttributeValueAsUnsigned(Attr, defaultValue);
};
const unsigned key = getAttr(DW_AT_LLVM_ptrauth_key);
const bool addr_disc = getAttr(DW_AT_LLVM_ptrauth_address_discriminated);
const unsigned extra = getAttr(DW_AT_LLVM_ptrauth_extra_discriminator);
const bool isapointer = getAttr(DW_AT_LLVM_ptrauth_isa_pointer);
const bool authenticates_null_values =
getAttr(DW_AT_LLVM_ptrauth_authenticates_null_values);
const unsigned authentication_mode_int = getAttr(
DW_AT_LLVM_ptrauth_authentication_mode,
static_cast<unsigned>(clang::PointerAuthenticationMode::SignAndAuth));
clang::PointerAuthenticationMode authentication_mode =
clang::PointerAuthenticationMode::SignAndAuth;
if (authentication_mode_int >=
static_cast<unsigned>(clang::PointerAuthenticationMode::None) &&
authentication_mode_int <=
static_cast<unsigned>(
clang::PointerAuthenticationMode::SignAndAuth)) {
authentication_mode =
static_cast<clang::PointerAuthenticationMode>(authentication_mode_int);
} else {
die.GetDWARF()->GetObjectFile()->GetModule()->ReportError(
"[{0:x16}]: invalid pointer authentication mode method {1:x4}",
die.GetOffset(), authentication_mode_int);
}
auto ptr_auth = clang::PointerAuthQualifier::Create(
key, addr_disc, extra, authentication_mode, isapointer,
authenticates_null_values);
return TypePayloadClang(ptr_auth.getAsOpaqueValue());
}
lldb::TypeSP
DWARFASTParserClang::ParseTypeModifier(const SymbolContext &sc,
const DWARFDIE &die,
ParsedDWARFTypeAttributes &attrs) {
Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
SymbolFileDWARF *dwarf = die.GetDWARF();
const dw_tag_t tag = die.Tag();
LanguageType cu_language = SymbolFileDWARF::GetLanguage(*die.GetCU());
Type::ResolveState resolve_state = Type::ResolveState::Unresolved;
Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
TypePayloadClang payload(GetOwningClangModule(die));
TypeSP type_sp;
CompilerType clang_type;
if (tag == DW_TAG_typedef) {
// DeclContext will be populated when the clang type is materialized in
// Type::ResolveCompilerType.
PrepareContextToReceiveMembers(
m_ast, GetClangASTImporter(),
GetClangDeclContextContainingDIE(die, nullptr), die,
attrs.name.GetCString());
if (attrs.type.IsValid()) {
// Try to parse a typedef from the (DWARF embedded in the) Clang
// module file first as modules can contain typedef'ed
// structures that have no names like:
//
// typedef struct { int a; } Foo;
//
// In this case we will have a structure with no name and a
// typedef named "Foo" that points to this unnamed
// structure. The name in the typedef is the only identifier for
// the struct, so always try to get typedefs from Clang modules
// if possible.
//
// The type_sp returned will be empty if the typedef doesn't
// exist in a module file, so it is cheap to call this function
// just to check.
//
// If we don't do this we end up creating a TypeSP that says
// this is a typedef to type 0x123 (the DW_AT_type value would
// be 0x123 in the DW_TAG_typedef), and this is the unnamed
// structure type. We will have a hard time tracking down an
// unnammed structure type in the module debug info, so we make
// sure we don't get into this situation by always resolving
// typedefs from the module.
const DWARFDIE encoding_die = attrs.type.Reference();
// First make sure that the die that this is typedef'ed to _is_
// just a declaration (DW_AT_declaration == 1), not a full
// definition since template types can't be represented in
// modules since only concrete instances of templates are ever
// emitted and modules won't contain those
if (encoding_die &&
encoding_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1) {
type_sp = ParseTypeFromClangModule(sc, die, log);
if (type_sp)
return type_sp;
}
}
}
DEBUG_PRINTF("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(),
DW_TAG_value_to_name(tag), type_name_cstr,
encoding_uid.Reference());
switch (tag) {
default:
break;
case DW_TAG_unspecified_type:
if (attrs.name == "nullptr_t" || attrs.name == "decltype(nullptr)") {
resolve_state = Type::ResolveState::Full;
clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
break;
}
// Fall through to base type below in case we can handle the type
// there...
[[fallthrough]];
case DW_TAG_base_type:
resolve_state = Type::ResolveState::Full;
clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(
attrs.name.GetStringRef(), attrs.encoding,
attrs.byte_size.value_or(0) * 8);
break;
case DW_TAG_pointer_type:
encoding_data_type = Type::eEncodingIsPointerUID;
break;
case DW_TAG_reference_type:
encoding_data_type = Type::eEncodingIsLValueReferenceUID;
break;
case DW_TAG_rvalue_reference_type:
encoding_data_type = Type::eEncodingIsRValueReferenceUID;
break;
case DW_TAG_typedef:
encoding_data_type = Type::eEncodingIsTypedefUID;
break;
case DW_TAG_const_type:
encoding_data_type = Type::eEncodingIsConstUID;
break;
case DW_TAG_restrict_type:
encoding_data_type = Type::eEncodingIsRestrictUID;
break;
case DW_TAG_volatile_type:
encoding_data_type = Type::eEncodingIsVolatileUID;
break;
case DW_TAG_LLVM_ptrauth_type:
encoding_data_type = Type::eEncodingIsLLVMPtrAuthUID;
payload = GetPtrAuthMofidierPayload(die);
break;
case DW_TAG_atomic_type:
encoding_data_type = Type::eEncodingIsAtomicUID;
break;
}
if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID ||
encoding_data_type == Type::eEncodingIsTypedefUID)) {
if (tag == DW_TAG_pointer_type) {
DWARFDIE target_die = die.GetReferencedDIE(DW_AT_type);
if (target_die.GetAttributeValueAsUnsigned(DW_AT_APPLE_block, 0)) {
// Blocks have a __FuncPtr inside them which is a pointer to a
// function of the proper type.
for (DWARFDIE child_die : target_die.children()) {
if (!strcmp(child_die.GetAttributeValueAsString(DW_AT_name, ""),
"__FuncPtr")) {
DWARFDIE function_pointer_type =
child_die.GetReferencedDIE(DW_AT_type);
if (function_pointer_type) {
DWARFDIE function_type =
function_pointer_type.GetReferencedDIE(DW_AT_type);
bool function_type_is_new_pointer;
TypeSP lldb_function_type_sp = ParseTypeFromDWARF(
sc, function_type, &function_type_is_new_pointer);
if (lldb_function_type_sp) {
clang_type = m_ast.CreateBlockPointerType(
lldb_function_type_sp->GetForwardCompilerType());
encoding_data_type = Type::eEncodingIsUID;
attrs.type.Clear();
resolve_state = Type::ResolveState::Full;
}
}
break;
}
}
}
}
if (cu_language == eLanguageTypeObjC ||
cu_language == eLanguageTypeObjC_plus_plus) {
if (attrs.name) {
if (attrs.name == "id") {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
"is Objective-C 'id' built-in type.",
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
encoding_data_type = Type::eEncodingIsUID;
attrs.type.Clear();
resolve_state = Type::ResolveState::Full;
} else if (attrs.name == "Class") {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
"is Objective-C 'Class' built-in type.",
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
encoding_data_type = Type::eEncodingIsUID;
attrs.type.Clear();
resolve_state = Type::ResolveState::Full;
} else if (attrs.name == "SEL") {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
"is Objective-C 'selector' built-in type.",
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
encoding_data_type = Type::eEncodingIsUID;
attrs.type.Clear();
resolve_state = Type::ResolveState::Full;
}
} else if (encoding_data_type == Type::eEncodingIsPointerUID &&
attrs.type.IsValid()) {
// Clang sometimes erroneously emits id as objc_object*. In that
// case we fix up the type to "id".
const DWARFDIE encoding_die = attrs.type.Reference();
if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type) {
llvm::StringRef struct_name = encoding_die.GetName();
if (struct_name == "objc_object") {
if (log)
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF::ParseType (die = {0:x16}) {1} ({2}) '{3}' "
"is 'objc_object*', which we overrode to 'id'.",
die.GetOffset(), DW_TAG_value_to_name(die.Tag()), die.Tag(),
die.GetName());
clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
encoding_data_type = Type::eEncodingIsUID;
attrs.type.Clear();
resolve_state = Type::ResolveState::Full;
}
}
}
}
}
return dwarf->MakeType(die.GetID(), attrs.name, attrs.byte_size, nullptr,
attrs.type.Reference().GetID(), encoding_data_type,
&attrs.decl, clang_type, resolve_state, payload);
}
std::string DWARFASTParserClang::GetDIEClassTemplateParams(DWARFDIE die) {
if (DWARFDIE signature_die = die.GetReferencedDIE(DW_AT_signature))
die = signature_die;
if (llvm::StringRef(die.GetName()).contains("<"))
return {};
std::string name;
llvm::raw_string_ostream os(name);
llvm::DWARFTypePrinter<DWARFDIE> type_printer(os);
type_printer.appendAndTerminateTemplateParameters(die);
return name;
}
void DWARFASTParserClang::MapDeclDIEToDefDIE(
const lldb_private::plugin::dwarf::DWARFDIE &decl_die,
const lldb_private::plugin::dwarf::DWARFDIE &def_die) {
LinkDeclContextToDIE(GetCachedClangDeclContextForDIE(decl_die), def_die);
SymbolFileDWARF *dwarf = def_die.GetDWARF();
ParsedDWARFTypeAttributes decl_attrs(decl_die);
ParsedDWARFTypeAttributes def_attrs(def_die);
ConstString unique_typename(decl_attrs.name);
Declaration decl_declaration(decl_attrs.decl);
GetUniqueTypeNameAndDeclaration(
decl_die, SymbolFileDWARF::GetLanguage(*decl_die.GetCU()),
unique_typename, decl_declaration);
if (UniqueDWARFASTType *unique_ast_entry_type =
dwarf->GetUniqueDWARFASTTypeMap().Find(
unique_typename, decl_die, decl_declaration,
decl_attrs.byte_size.value_or(0),
decl_attrs.is_forward_declaration)) {
unique_ast_entry_type->UpdateToDefDIE(def_die, def_attrs.decl,
def_attrs.byte_size.value_or(0));
} else if (Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups)) {
const dw_tag_t tag = decl_die.Tag();
LLDB_LOG(log,
"Failed to find {0:x16} {1} ({2}) type \"{3}\" in "
"UniqueDWARFASTTypeMap",
decl_die.GetID(), DW_TAG_value_to_name(tag), tag, unique_typename);
}
}
TypeSP DWARFASTParserClang::ParseEnum(const SymbolContext &sc,
const DWARFDIE &decl_die,
ParsedDWARFTypeAttributes &attrs) {
Log *log = GetLog(DWARFLog::TypeCompletion | DWARFLog::Lookups);
SymbolFileDWARF *dwarf = decl_die.GetDWARF();
const dw_tag_t tag = decl_die.Tag();
DWARFDIE def_die;
if (attrs.is_forward_declaration) {
if (TypeSP type_sp = ParseTypeFromClangModule(sc, decl_die, log))
return type_sp;
def_die = dwarf->FindDefinitionDIE(decl_die);
if (!def_die) {
SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
if (debug_map_symfile) {
// We weren't able to find a full declaration in this DWARF,
// see if we have a declaration anywhere else...
def_die = debug_map_symfile->FindDefinitionDIE(decl_die);
}
}
if (log) {
dwarf->GetObjectFile()->GetModule()->LogMessage(
log,
"SymbolFileDWARF({0:p}) - {1:x16}}: {2} ({3}) type \"{4}\" is a "
"forward declaration, complete DIE is {5}",
static_cast<void *>(this), decl_die.GetID(), DW_TAG_value_to_name(tag),
tag, attrs.name.GetCString(),
def_die ? llvm::utohexstr(def_die.GetID()) : "not found");
}
}
if (def_die) {
if (auto [it, inserted] = dwarf->GetDIEToType().try_emplace(
def_die.GetDIE(), DIE_IS_BEING_PARSED);
!inserted) {
if (it->getSecond() == nullptr || it->getSecond() == DIE_IS_BEING_PARSED)
return nullptr;
return it->getSecond()->shared_from_this();
}
attrs = ParsedDWARFTypeAttributes(def_die);
} else {
// No definition found. Proceed with the declaration die. We can use it to
// create a forward-declared type.
def_die = decl_die;
}
CompilerType enumerator_clang_type;
if (attrs.type.IsValid()) {
Type *enumerator_type =
dwarf->ResolveTypeUID(attrs.type.Reference(), true);
if (enumerator_type)
enumerator_clang_type = enumerator_type->GetFullCompilerType();
}
if (!enumerator_clang_type) {
if (attrs.byte_size) {
enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(