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 @@ -874,3 +874,27 @@ - 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