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 @@ -69,7 +69,7 @@ yaml::Hex64 Length; uint16_t Version; yaml::Hex64 CuOffset; - yaml::Hex8 AddrSize; + Optional AddrSize; yaml::Hex8 SegSize; std::vector Descriptors; }; 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 @@ -127,26 +127,33 @@ Error DWARFYAML::emitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { for (auto Range : DI.ARanges) { auto HeaderStart = OS.tell(); + + uint8_t AddrSize; + if (Range.AddrSize) + AddrSize = *Range.AddrSize; + else + AddrSize = DI.Is64BitAddrSize ? 8 : 4; + writeInitialLength(Range.Format, Range.Length, OS, DI.IsLittleEndian); writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian); writeDWARFOffset(Range.CuOffset, Range.Format, OS, DI.IsLittleEndian); - writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian); + writeInteger((uint8_t)AddrSize, OS, DI.IsLittleEndian); writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian); auto HeaderSize = OS.tell() - HeaderStart; - auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2); + auto FirstDescriptor = alignTo(HeaderSize, AddrSize * 2); ZeroFillBytes(OS, FirstDescriptor - HeaderSize); for (auto Descriptor : Range.Descriptors) { - if (Error Err = writeVariableSizedInteger( - Descriptor.Address, Range.AddrSize, OS, DI.IsLittleEndian)) + if (Error Err = writeVariableSizedInteger(Descriptor.Address, AddrSize, + OS, DI.IsLittleEndian)) return createStringError(errc::not_supported, "unable to write debug_aranges address: %s", toString(std::move(Err)).c_str()); - cantFail(writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS, + cantFail(writeVariableSizedInteger(Descriptor.Length, AddrSize, OS, DI.IsLittleEndian)); } - ZeroFillBytes(OS, Range.AddrSize * 2); + ZeroFillBytes(OS, AddrSize * 2); } return Error::success(); 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 @@ -106,8 +106,8 @@ IO.mapRequired("Length", ARange.Length); IO.mapRequired("Version", ARange.Version); IO.mapRequired("CuOffset", ARange.CuOffset); - IO.mapRequired("AddressSize", ARange.AddrSize); - IO.mapRequired("SegmentSelectorSize", ARange.SegSize); + IO.mapOptional("AddressSize", ARange.AddrSize); + IO.mapOptional("SegmentSelectorSize", ARange.SegSize, 0); IO.mapRequired("Descriptors", ARange.Descriptors); } diff --git a/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml b/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml --- a/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml +++ b/llvm/test/ObjectYAML/MachO/DWARF-debug_aranges.yaml @@ -328,7 +328,6 @@ # CHECK-NEXT: Version: 2 # CHECK-NEXT: CuOffset: 0x0000000000000000 # CHECK-NEXT: AddressSize: 0x08 -# CHECK-NEXT: SegmentSelectorSize: 0x00 # CHECK-NEXT: Descriptors: # CHECK-NEXT: - Address: 0x0000000100000F50 # CHECK-NEXT: Length: 52 diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml --- a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml @@ -458,3 +458,55 @@ Descriptors: - Address: 0x1234 Length: 0x1000 + +## j) Test the default value of address_size and segment_selector_size in a 64/32-bit object file. + +# RUN: yaml2obj --docnum=10 -DCLASS=ELFCLASS64 %s -o %t10.64-bit.o +# RUN: llvm-readelf --hex-dump=.debug_aranges %t10.64-bit.o | \ +# RUN: FileCheck %s --check-prefix=DEFAULT64 + +# DEFAULT64: Hex dump of section '.debug_aranges': +# DEFAULT64-NEXT: 0x00000000 2c000000 02000000 00000800 00000000 ,............... +## ^------- unit_length (4-byte) +## ^--- version (2-byte) +## ^-------- debug_info_offset (4-byte) +## ^- address_size (1-byte) +## ^- segment_selector_size (1-byte) +## ^------- padding (4-byte) +# DEFAULT64-NEXT: 0x00000010 34120000 00000000 00100000 00000000 4............... +## ^---------------- address (8-byte) +## ^---------------- length (8-byte) +# DEFAULT64-NEXT: 0x00000020 00000000 00000000 00000000 00000000 ................ +## ^---------------------------------- terminating entry (16-byte) + +# RUN: yaml2obj --docnum=10 -DCLASS=ELFCLASS32 %s -o %t10.32-bit.o +# RUN: llvm-readelf --hex-dump=.debug_aranges %t10.32-bit.o | \ +# RUN: FileCheck %s --check-prefix=DEFAULT32 + +# DEFAULT32: Hex dump of section '.debug_aranges': +# DEFAULT32-NEXT: 0x00000000 2c000000 02000000 00000400 00000000 ,............... +## ^------- unit_length (4-byte) +## ^--- version (2-byte) +## ^-------- debug_info_offset (4-byte) +## ^- address_size (1-byte) +## ^- segment_selector_size (1-byte) +## ^------- padding (4-byte) +# DEFAULT32-NEXT: 0x00000010 34120000 00100000 00000000 00000000 4............... +## ^------- address (4-byte) +## ^------- length (4-byte) +## ^---------------- terminating entry (8-byte) + +--- !ELF +FileHeader: + Class: [[CLASS]] + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_aranges: + - Length: 0x2c + Version: 2 + CuOffset: 0 + Descriptors: + - Address: 0x1234 + Length: 0x1000