Skip to content

Commit 8a1846d

Browse files
authored
[dwarf2yaml] Correctly emit type and split unit headers (llvm#102471)
(DWARFv5) split units have an extra `dwo_id` field in the header. Type units have `type_signature` and `type_offset`.
1 parent d12250c commit 8a1846d

File tree

5 files changed

+128
-28
lines changed

5 files changed

+128
-28
lines changed

llvm/include/llvm/ObjectYAML/DWARFYAML.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ struct Unit {
115115
llvm::dwarf::UnitType Type; // Added in DWARF 5
116116
std::optional<uint64_t> AbbrevTableID;
117117
std::optional<yaml::Hex64> AbbrOffset;
118+
yaml::Hex64 TypeSignatureOrDwoID; // For type or split units
119+
yaml::Hex64 TypeOffset; // For type units
120+
118121
std::vector<Entry> Entries;
119122
};
120123

@@ -245,7 +248,7 @@ struct Data {
245248
std::optional<PubSection> GNUPubNames;
246249
std::optional<PubSection> GNUPubTypes;
247250

248-
std::vector<Unit> CompileUnits;
251+
std::vector<Unit> Units;
249252

250253
std::vector<LineTable> DebugLines;
251254
std::optional<std::vector<ListTable<RnglistEntry>>> DebugRnglists;

llvm/lib/ObjectYAML/DWARFEmitter.cpp

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -405,17 +405,33 @@ static Expected<uint64_t> writeDIE(const DWARFYAML::Data &DI, uint64_t CUIndex,
405405
}
406406

407407
Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
408-
for (uint64_t I = 0; I < DI.CompileUnits.size(); ++I) {
409-
const DWARFYAML::Unit &Unit = DI.CompileUnits[I];
408+
for (uint64_t I = 0; I < DI.Units.size(); ++I) {
409+
const DWARFYAML::Unit &Unit = DI.Units[I];
410410
uint8_t AddrSize;
411411
if (Unit.AddrSize)
412412
AddrSize = *Unit.AddrSize;
413413
else
414414
AddrSize = DI.Is64BitAddrSize ? 8 : 4;
415415
dwarf::FormParams Params = {Unit.Version, AddrSize, Unit.Format};
416416
uint64_t Length = 3; // sizeof(version) + sizeof(address_size)
417-
Length += Unit.Version >= 5 ? 1 : 0; // sizeof(unit_type)
418417
Length += Params.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset)
418+
if (Unit.Version >= 5) {
419+
++Length; // sizeof(unit_type)
420+
switch (Unit.Type) {
421+
case dwarf::DW_UT_compile:
422+
case dwarf::DW_UT_partial:
423+
default:
424+
break;
425+
case dwarf::DW_UT_type:
426+
case dwarf::DW_UT_split_type:
427+
// sizeof(type_signature) + sizeof(type_offset)
428+
Length += 8 + Params.getDwarfOffsetByteSize();
429+
break;
430+
case dwarf::DW_UT_skeleton:
431+
case dwarf::DW_UT_split_compile:
432+
Length += 8; // sizeof(dwo_id)
433+
}
434+
}
419435

420436
// Since the length of the current compilation unit is undetermined yet, we
421437
// firstly write the content of the compilation unit to a buffer to
@@ -461,6 +477,21 @@ Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
461477
writeInteger((uint8_t)Unit.Type, OS, DI.IsLittleEndian);
462478
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);
463479
writeDWARFOffset(AbbrevTableOffset, Unit.Format, OS, DI.IsLittleEndian);
480+
switch (Unit.Type) {
481+
case dwarf::DW_UT_compile:
482+
case dwarf::DW_UT_partial:
483+
default:
484+
break;
485+
case dwarf::DW_UT_type:
486+
case dwarf::DW_UT_split_type:
487+
writeInteger(Unit.TypeSignatureOrDwoID, OS, DI.IsLittleEndian);
488+
writeDWARFOffset(Unit.TypeOffset, Unit.Format, OS, DI.IsLittleEndian);
489+
break;
490+
case dwarf::DW_UT_skeleton:
491+
case dwarf::DW_UT_split_compile:
492+
writeInteger(Unit.TypeSignatureOrDwoID, OS, DI.IsLittleEndian);
493+
break;
494+
}
464495
} else {
465496
writeDWARFOffset(AbbrevTableOffset, Unit.Format, OS, DI.IsLittleEndian);
466497
writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian);

llvm/lib/ObjectYAML/DWARFYAML.cpp

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ SetVector<StringRef> DWARFYAML::Data::getNonEmptySectionNames() const {
3636
SecNames.insert("debug_addr");
3737
if (!DebugAbbrev.empty())
3838
SecNames.insert("debug_abbrev");
39-
if (!CompileUnits.empty())
39+
if (!Units.empty())
4040
SecNames.insert("debug_info");
4141
if (PubNames)
4242
SecNames.insert("debug_pubnames");
@@ -101,7 +101,7 @@ void MappingTraits<DWARFYAML::Data>::mapping(IO &IO, DWARFYAML::Data &DWARF) {
101101
DWARFCtx.IsGNUPubSec = true;
102102
IO.mapOptional("debug_gnu_pubnames", DWARF.GNUPubNames);
103103
IO.mapOptional("debug_gnu_pubtypes", DWARF.GNUPubTypes);
104-
IO.mapOptional("debug_info", DWARF.CompileUnits);
104+
IO.mapOptional("debug_info", DWARF.Units);
105105
IO.mapOptional("debug_line", DWARF.DebugLines);
106106
IO.mapOptional("debug_addr", DWARF.DebugAddr);
107107
IO.mapOptional("debug_str_offsets", DWARF.DebugStrOffsets);
@@ -216,6 +216,22 @@ void MappingTraits<DWARFYAML::Unit>::mapping(IO &IO, DWARFYAML::Unit &Unit) {
216216
IO.mapOptional("AbbrevTableID", Unit.AbbrevTableID);
217217
IO.mapOptional("AbbrOffset", Unit.AbbrOffset);
218218
IO.mapOptional("AddrSize", Unit.AddrSize);
219+
if (Unit.Version >= 5) {
220+
switch (Unit.Type) {
221+
case dwarf::DW_UT_compile:
222+
case dwarf::DW_UT_partial:
223+
default:
224+
break;
225+
case dwarf::DW_UT_type:
226+
case dwarf::DW_UT_split_type:
227+
IO.mapRequired("TypeSignature", Unit.TypeSignatureOrDwoID);
228+
IO.mapRequired("TypeOffset", Unit.TypeOffset);
229+
break;
230+
case dwarf::DW_UT_skeleton:
231+
case dwarf::DW_UT_split_compile:
232+
IO.mapRequired("DwoID", Unit.TypeSignatureOrDwoID);
233+
}
234+
}
219235
IO.mapOptional("Entries", Unit.Entries);
220236
}
221237

llvm/test/tools/yaml2obj/ELF/DWARF/debug-info.yaml

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
# SHDR-NEXT: AddressAlignment: [[ADDRALIGN]]
2222
# SHDR-NEXT: EntrySize: 0
2323
# DWARF32-LE-CONTENT-NEXT: SectionData (
24-
# DWARF32-LE-CONTENT-NEXT: 0000: 34120000 05000204 34120000 01785634
24+
# DWARF32-LE-CONTENT-NEXT: 0000: 34120000 05000304 34120000 01785634
2525
## ^------- unit_length (4-byte)
2626
## ^--- version (2-byte)
2727
## ^- unit_type (1-byte) DW_UT_type
@@ -208,7 +208,7 @@ DWARF:
208208
debug_info:
209209
- Length: 0x1234
210210
Version: 5
211-
UnitType: DW_UT_type
211+
UnitType: DW_UT_partial
212212
AbbrOffset: 0x1234
213213
AddrSize: 4
214214
Entries:
@@ -297,7 +297,7 @@ DWARF:
297297
# RUN: FileCheck -DINDEX=2 -DNAME=15 -DOFFSET=0x9B -DSIZE=179 -DADDRALIGN=1 %s --check-prefixes=SHDR,DWARF32-BE-CONTENT
298298

299299
# DWARF32-BE-CONTENT-NEXT: SectionData (
300-
# DWARF32-BE-CONTENT-NEXT: 0000: 00001234 00050204 00001234 01123456
300+
# DWARF32-BE-CONTENT-NEXT: 0000: 00001234 00050304 00001234 01123456
301301
## ^------- unit_length (4-byte)
302302
## ^--- version (2-byte)
303303
## ^- unit_type (1-byte) DW_UT_type
@@ -449,7 +449,7 @@ DWARF:
449449
debug_info:
450450
- Length: 0x1234
451451
Version: 5
452-
UnitType: DW_UT_type
452+
UnitType: DW_UT_compile
453453
AbbrOffset: 0x1234
454454
Entries: []
455455

@@ -476,7 +476,7 @@ DWARF:
476476
debug_info:
477477
- Length: 0x1234
478478
Version: 5
479-
UnitType: DW_UT_type
479+
UnitType: DW_UT_compile
480480
AbbrOffset: 0x1234
481481
Entries: []
482482

@@ -541,7 +541,7 @@ DWARF:
541541
debug_info:
542542
- Length: 0x1234
543543
Version: 5
544-
UnitType: DW_UT_type
544+
UnitType: DW_UT_compile
545545
AbbrOffset: 0x1234
546546
Entries:
547547
- AbbrCode: 0
@@ -554,7 +554,7 @@ DWARF:
554554
# RUN: FileCheck %s --check-prefix=EMPTY-ENTRIES --match-full-lines
555555

556556
# EMPTY-ENTRIES: Hex dump of section '.debug_info':
557-
# EMPTY-ENTRIES-NEXT: 0x00000000 34120000 05000204 34120000 4.......4...
557+
# EMPTY-ENTRIES-NEXT: 0x00000000 34120000 05000104 34120000 4.......4...
558558
# EMPTY-ENTRIES-EMPTY:
559559
## ^- 'Entries' is empty
560560

@@ -567,7 +567,7 @@ DWARF:
567567
debug_info:
568568
- Length: 0x1234
569569
Version: 5
570-
UnitType: DW_UT_type
570+
UnitType: DW_UT_compile
571571
AbbrOffset: 0x1234
572572
Entries: []
573573

@@ -579,7 +579,7 @@ DWARF:
579579
# RUN: FileCheck %s --check-prefix=FORM --match-full-lines
580580

581581
# FORM: Hex dump of section '.debug_info':
582-
# FORM-NEXT: 0x00000000 34120000 05000204 34120000 02341221 4.......4....4.!
582+
# FORM-NEXT: 0x00000000 34120000 05000104 34120000 02341221 4.......4....4.!
583583
## ^------- unit_length (4-byte)
584584
## ^- abbreviation code (ULEB128)
585585
## ^--- Form: DW_FORM_data2 (2-byte)
@@ -617,7 +617,7 @@ DWARF:
617617
debug_info:
618618
- Length: 0x1234
619619
Version: 5
620-
UnitType: DW_UT_type
620+
UnitType: DW_UT_compile
621621
AbbrOffset: 0x1234
622622
Entries:
623623
## Test that yaml2obj emits values when the abbrev code is specified.
@@ -655,29 +655,58 @@ DWARF:
655655
debug_info:
656656
- Length: 0x1234
657657
Version: 5
658-
UnitType: DW_UT_type
658+
UnitType: DW_UT_compile
659659
AbbrOffset: 0x1234
660660
Entries:
661661
- AbbrCode: 1
662662
Values:
663663
- Value: 0x1234
664664

665-
## j) Test that yaml2obj emits the correct DWARF64 unit headers.
666-
667-
## DWARFv5 unit header.
665+
## j) Test that yaml2obj emits the correct DWARFv5 unit headers.
668666

669667
# RUN: yaml2obj --docnum=11 %s -o %t11.o
670668
# RUN: llvm-readelf --hex-dump=.debug_info %t11.o | \
671669
# RUN: FileCheck %s --check-prefix=DWARFV5-HEADER
672670

673671
# DWARFV5-HEADER: Hex dump of section '.debug_info':
674-
# DWARFV5-HEADER-NEXT: 0x00000000 ffffffff 0c000000 00000000 05000208 ................
672+
# DWARFV5-HEADER-NEXT: 0x00000000 ffffffff 0d000000 00000000 05000108 ................
675673
## ^------------------------- unit_length (12-byte)
676674
## ^--- version (2-byte)
677675
## ^- unit_type (1-byte)
678676
## ^- address_size (1-byte)
679-
# DWARFV5-HEADER-NEXT: 0x00000010 34120000 00000000 4.......
677+
# DWARFV5-HEADER-NEXT: 0x00000010 34120000 00000000 00150000 00050002 4...............
680678
## ^---------------- debug_abbrev_offset (8-byte)
679+
## ^- End of children (1-byte)
680+
## ^-------- unit_length (4-byte)
681+
## ^--- version (2-byte)
682+
## ^- unit_type (1-byte)
683+
# DWARFV5-HEADER-NEXT: 0x00000020 08341200 000df0ad baefbead de180000 .4..............
684+
## ^- address_size (1-byte)
685+
## ^-------- debug_abbrev_offset (4-byte)
686+
## ^----------------- type_signature (8-byte)
687+
## \/ ^----- type_offset (4-byte)
688+
# DWARFV5-HEADER-NEXT: 0x00000030 0000ffff ffff1d00 00000000 00000500 ................
689+
## ^- End of children (1-byte)
690+
## ^-------------------------- unit_length (12-byte)
691+
## ^--- version (2-byte)
692+
# DWARFV5-HEADER-NEXT: 0x00000040 02083412 00000000 00000df0 adbaefbe ..4.............
693+
## ^- unit_type (1-byte)
694+
## ^- address_size (1-byte)
695+
## ^----------------- debug_abbrev_offset (8-byte)
696+
## \/-- ^------------ type_signature (8-byte)
697+
# DWARFV5-HEADER-NEXT: 0x00000050 adde2800 00000000 00000011 00000005 ..(.............
698+
## ^----------------- type_offset (8-byte)
699+
## ^- End of children (1-byte)
700+
## ^-------- unit_length (4-byte)
701+
## \/ ^- version (2-byte)
702+
# DWARFV5-HEADER-NEXT: 0x00000060 00040834 1200000d f0adbaef beadde00 ...4............
703+
## ^- unit_type (1-byte)
704+
## ^- address_size (1-byte)
705+
## ^-------- debug_abbrev_offset (4-byte)
706+
## ^----------------- type_signature (8-byte)
707+
## ^- End of children (1-byte)
708+
709+
681710

682711
--- !ELF
683712
FileHeader:
@@ -686,12 +715,33 @@ FileHeader:
686715
Type: ET_EXEC
687716
DWARF:
688717
debug_info:
718+
- Format: DWARF64
719+
Version: 5
720+
UnitType: DW_UT_compile
721+
AbbrOffset: 0x1234
722+
Entries:
723+
- AbbrCode: 0
724+
- Version: 5
725+
UnitType: DW_UT_type
726+
AbbrOffset: 0x1234
727+
TypeSignature: 0xdeadbeefbaadf00d
728+
TypeOffset: 24
729+
Entries:
730+
- AbbrCode: 0
689731
- Format: DWARF64
690-
Length: 0x0c
691-
Version: 5
692-
UnitType: DW_UT_type
693-
AbbrOffset: 0x1234
694-
Entries: []
732+
Version: 5
733+
UnitType: DW_UT_type
734+
AbbrOffset: 0x1234
735+
TypeSignature: 0xdeadbeefbaadf00d
736+
TypeOffset: 40
737+
Entries:
738+
- AbbrCode: 0
739+
- Version: 5
740+
UnitType: DW_UT_skeleton
741+
AbbrOffset: 0x1234
742+
DwoID: 0xdeadbeefbaadf00d
743+
Entries:
744+
- AbbrCode: 0
695745

696746
## DWARFv4 unit header.
697747

llvm/tools/obj2yaml/dwarf2yaml.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ void dumpDebugInfo(DWARFContext &DCtx, DWARFYAML::Data &Y) {
338338

339339
NewUnit.Entries.push_back(NewEntry);
340340
}
341-
Y.CompileUnits.push_back(NewUnit);
341+
Y.Units.push_back(NewUnit);
342342
}
343343
}
344344

0 commit comments

Comments
 (0)