@@ -94,28 +94,45 @@ Error DWARFYAML::emitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
94
94
return Error::success ();
95
95
}
96
96
97
- Error DWARFYAML::emitDebugAbbrev (raw_ostream &OS, const DWARFYAML::Data &DI) {
98
- for (const DWARFYAML::AbbrevTable &AbbrevTable : DI.DebugAbbrev ) {
99
- uint64_t AbbrevCode = 0 ;
100
- for (const DWARFYAML::Abbrev &AbbrevDecl : AbbrevTable.Table ) {
101
- AbbrevCode =
102
- AbbrevDecl.Code ? (uint64_t )*AbbrevDecl.Code : AbbrevCode + 1 ;
103
- encodeULEB128 (AbbrevCode, OS);
104
- encodeULEB128 (AbbrevDecl.Tag , OS);
105
- OS.write (AbbrevDecl.Children );
106
- for (auto Attr : AbbrevDecl.Attributes ) {
107
- encodeULEB128 (Attr.Attribute , OS);
108
- encodeULEB128 (Attr.Form , OS);
109
- if (Attr.Form == dwarf::DW_FORM_implicit_const)
110
- encodeSLEB128 (Attr.Value , OS);
111
- }
112
- encodeULEB128 (0 , OS);
113
- encodeULEB128 (0 , OS);
97
+ StringRef DWARFYAML::Data::getAbbrevTableContentByIndex (uint64_t Index) const {
98
+ assert (Index < DebugAbbrev.size () &&
99
+ " Index should be less than the size of DebugAbbrev array" );
100
+ auto It = AbbrevTableContents.find (Index);
101
+ if (It != AbbrevTableContents.cend ())
102
+ return It->second ;
103
+
104
+ std::string AbbrevTableBuffer;
105
+ raw_string_ostream OS (AbbrevTableBuffer);
106
+
107
+ uint64_t AbbrevCode = 0 ;
108
+ for (const DWARFYAML::Abbrev &AbbrevDecl : DebugAbbrev[Index].Table ) {
109
+ AbbrevCode = AbbrevDecl.Code ? (uint64_t )*AbbrevDecl.Code : AbbrevCode + 1 ;
110
+ encodeULEB128 (AbbrevCode, OS);
111
+ encodeULEB128 (AbbrevDecl.Tag , OS);
112
+ OS.write (AbbrevDecl.Children );
113
+ for (auto Attr : AbbrevDecl.Attributes ) {
114
+ encodeULEB128 (Attr.Attribute , OS);
115
+ encodeULEB128 (Attr.Form , OS);
116
+ if (Attr.Form == dwarf::DW_FORM_implicit_const)
117
+ encodeSLEB128 (Attr.Value , OS);
114
118
}
119
+ encodeULEB128 (0 , OS);
120
+ encodeULEB128 (0 , OS);
121
+ }
122
+
123
+ // The abbreviations for a given compilation unit end with an entry
124
+ // consisting of a 0 byte for the abbreviation code.
125
+ OS.write_zeros (1 );
126
+
127
+ AbbrevTableContents.insert ({Index, AbbrevTableBuffer});
128
+
129
+ return AbbrevTableContents[Index];
130
+ }
115
131
116
- // The abbreviations for a given compilation unit end with an entry
117
- // consisting of a 0 byte for the abbreviation code.
118
- OS.write_zeros (1 );
132
+ Error DWARFYAML::emitDebugAbbrev (raw_ostream &OS, const DWARFYAML::Data &DI) {
133
+ for (uint64_t I = 0 ; I < DI.DebugAbbrev .size (); ++I) {
134
+ StringRef AbbrevTableContent = DI.getAbbrevTableContentByIndex (I);
135
+ OS.write (AbbrevTableContent.data (), AbbrevTableContent.size ());
119
136
}
120
137
121
138
return Error::success ();
@@ -257,16 +274,16 @@ static Expected<uint64_t> writeDIE(const DWARFYAML::Data &DI, uint64_t CUIndex,
257
274
if (AbbrCode == 0 || Entry.Values .empty ())
258
275
return OS.tell () - EntryBegin;
259
276
260
- Expected<uint64_t > AbbrevTableIndexOrErr =
261
- DI.getAbbrevTableIndexByID (AbbrevTableID);
262
- if (!AbbrevTableIndexOrErr )
277
+ Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr =
278
+ DI.getAbbrevTableInfoByID (AbbrevTableID);
279
+ if (!AbbrevTableInfoOrErr )
263
280
return createStringError (errc::invalid_argument,
264
- toString (AbbrevTableIndexOrErr .takeError ()) +
281
+ toString (AbbrevTableInfoOrErr .takeError ()) +
265
282
" for compilation unit with index " +
266
283
utostr (CUIndex));
267
284
268
285
ArrayRef<DWARFYAML::Abbrev> AbbrevDecls (
269
- DI.DebugAbbrev [*AbbrevTableIndexOrErr ].Table );
286
+ DI.DebugAbbrev [AbbrevTableInfoOrErr-> Index ].Table );
270
287
271
288
if (AbbrCode > AbbrevDecls.size ())
272
289
return createStringError (
@@ -425,12 +442,28 @@ Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
425
442
426
443
writeInitialLength (Unit.Format , Length, OS, DI.IsLittleEndian );
427
444
writeInteger ((uint16_t )Unit.Version , OS, DI.IsLittleEndian );
445
+
446
+ uint64_t AbbrevTableOffset = 0 ;
447
+ if (Unit.AbbrOffset ) {
448
+ AbbrevTableOffset = *Unit.AbbrOffset ;
449
+ } else {
450
+ if (Expected<DWARFYAML::Data::AbbrevTableInfo> AbbrevTableInfoOrErr =
451
+ DI.getAbbrevTableInfoByID (AbbrevTableID)) {
452
+ AbbrevTableOffset = AbbrevTableInfoOrErr->Offset ;
453
+ } else {
454
+ // The current compilation unit may not have DIEs and it will not be
455
+ // able to find the associated abbrev table. We consume the error and
456
+ // assign 0 to the debug_abbrev_offset in such circumstances.
457
+ consumeError (AbbrevTableInfoOrErr.takeError ());
458
+ }
459
+ }
460
+
428
461
if (Unit.Version >= 5 ) {
429
462
writeInteger ((uint8_t )Unit.Type , OS, DI.IsLittleEndian );
430
463
writeInteger ((uint8_t )AddrSize, OS, DI.IsLittleEndian );
431
- writeDWARFOffset (Unit. AbbrOffset , Unit.Format , OS, DI.IsLittleEndian );
464
+ writeDWARFOffset (AbbrevTableOffset , Unit.Format , OS, DI.IsLittleEndian );
432
465
} else {
433
- writeDWARFOffset (Unit. AbbrOffset , Unit.Format , OS, DI.IsLittleEndian );
466
+ writeDWARFOffset (AbbrevTableOffset , Unit.Format , OS, DI.IsLittleEndian );
434
467
writeInteger ((uint8_t )AddrSize, OS, DI.IsLittleEndian );
435
468
}
436
469
0 commit comments