diff --git a/llvm/include/llvm/ObjectYAML/DWARFYAML.h b/llvm/include/llvm/ObjectYAML/DWARFYAML.h --- a/llvm/include/llvm/ObjectYAML/DWARFYAML.h +++ b/llvm/include/llvm/ObjectYAML/DWARFYAML.h @@ -123,7 +123,7 @@ dwarf::DwarfFormat Format; Optional Length; uint16_t Version; - uint8_t AddrSize; + Optional AddrSize; llvm::dwarf::UnitType Type; // Added in DWARF 5 yaml::Hex64 AbbrOffset; std::vector Entries; diff --git a/llvm/lib/ObjectYAML/DWARFEmitter.cpp b/llvm/lib/ObjectYAML/DWARFEmitter.cpp --- a/llvm/lib/ObjectYAML/DWARFEmitter.cpp +++ b/llvm/lib/ObjectYAML/DWARFEmitter.cpp @@ -382,7 +382,12 @@ Error DWARFYAML::emitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) { for (const DWARFYAML::Unit &Unit : DI.CompileUnits) { - dwarf::FormParams Params = {Unit.Version, Unit.AddrSize, Unit.Format}; + uint8_t AddrSize; + if (Unit.AddrSize) + AddrSize = *Unit.AddrSize; + else + AddrSize = DI.Is64BitAddrSize ? 8 : 4; + dwarf::FormParams Params = {Unit.Version, AddrSize, Unit.Format}; uint64_t Length = 3; // sizeof(version) + sizeof(address_size) Length += Unit.Version >= 5 ? 1 : 0; // sizeof(unit_type) Length += Params.getDwarfOffsetByteSize(); // sizeof(debug_abbrev_offset) @@ -411,11 +416,11 @@ writeInteger((uint16_t)Unit.Version, OS, DI.IsLittleEndian); if (Unit.Version >= 5) { writeInteger((uint8_t)Unit.Type, OS, DI.IsLittleEndian); - writeInteger((uint8_t)Unit.AddrSize, OS, DI.IsLittleEndian); + writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian); writeDWARFOffset(Unit.AbbrOffset, Unit.Format, OS, DI.IsLittleEndian); } else { writeDWARFOffset(Unit.AbbrOffset, Unit.Format, OS, DI.IsLittleEndian); - writeInteger((uint8_t)Unit.AddrSize, OS, DI.IsLittleEndian); + writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian); } OS.write(EntryBuffer.data(), EntryBuffer.size()); diff --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp --- a/llvm/lib/ObjectYAML/DWARFYAML.cpp +++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp @@ -150,7 +150,7 @@ if (Unit.Version >= 5) IO.mapRequired("UnitType", Unit.Type); IO.mapRequired("AbbrOffset", Unit.AbbrOffset); - IO.mapRequired("AddrSize", Unit.AddrSize); + IO.mapOptional("AddrSize", Unit.AddrSize); IO.mapOptional("Entries", Unit.Entries); } diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-info.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-info.yaml --- a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-info.yaml +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-info.yaml @@ -453,7 +453,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: [] ## e) Test that yaml2obj emits an error message when both the "Content" and the @@ -482,7 +481,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: [] ## f) Test that all the properties can be overridden by the section header when @@ -550,7 +548,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: - AbbrCode: 0 Values: [] @@ -568,7 +565,7 @@ --- !ELF FileHeader: - Class: ELFCLASS64 + Class: ELFCLASS32 Data: ELFDATA2LSB Type: ET_EXEC Machine: EM_X86_64 @@ -578,7 +575,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: [] ## h) Test that yaml2obj emits values in the DIE according to the abbreviation whose @@ -604,7 +600,7 @@ --- !ELF FileHeader: - Class: ELFCLASS64 + Class: ELFCLASS32 Data: ELFDATA2LSB Type: ET_EXEC Machine: EM_X86_64 @@ -629,7 +625,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: ## Test that yaml2obj emits values when the abbrev code is specified. - AbbrCode: 2 @@ -667,7 +662,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 4 Entries: - AbbrCode: 1 Values: @@ -703,7 +697,6 @@ Version: 5 UnitType: DW_UT_type AbbrOffset: 0x1234 - AddrSize: 8 Entries: [] ## DWARFv4 unit header. @@ -733,7 +726,6 @@ Length: 0x0c Version: 4 AbbrOffset: 0x1234 - AddrSize: 8 Entries: [] ## k) Test that yaml2obj is able to emit a correct length for compilation units. @@ -826,7 +818,6 @@ debug_info: - Version: 4 AbbrOffset: 0x00 - AddrSize: 0x08 Entries: - AbbrCode: 1 Values: @@ -869,8 +860,31 @@ debug_info: - Version: 4 AbbrOffset: 0x00 - AddrSize: 0x08 Entries: - AbbrCode: 1 - AbbrCode: 0 - AbbrCode: 2 + +## m) Test that yaml2obj is able to infer the address size from the object file. + +# RUN: yaml2obj --docnum=15 -DBITS=64 %s -o %t15.64-bit.o +# RUN: llvm-readelf --hex-dump=.debug_info %t15.64-bit.o | \ +# RUN: FileCheck %s --check-prefix=ADDRSIZE -DADDRSIZE=08 + +# ADDRSIZE: Hex dump of section '.debug_info': +# ADDRSIZE-NEXT: 0x00000000 07000000 04000000 0000[[ADDRSIZE]] ........... + +# RUN: yaml2obj --docnum=15 -DBITS=32 %s -o %t15.32-bit.o +# RUN: llvm-readelf --hex-dump=.debug_info %t15.32-bit.o | \ +# RUN: FileCheck %s --check-prefix=ADDRSIZE -DADDRSIZE=04 + +--- !ELF +FileHeader: + Class: ELFCLASS[[BITS]] + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_info: + - Version: 4 + AbbrOffset: 0x00