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 @@ -96,12 +96,16 @@ auto HeaderStart = OS.tell(); writeInitialLength(Range.Length, OS, DI.IsLittleEndian); writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian); - writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian); + if (Range.Length.isDWARF64()) + writeInteger((uint64_t)Range.CuOffset, OS, DI.IsLittleEndian); + else + writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian); writeInteger((uint8_t)Range.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, Range.SegSize + Range.AddrSize * 2); ZeroFillBytes(OS, FirstDescriptor - HeaderSize); for (auto Descriptor : Range.Descriptors) { @@ -110,7 +114,7 @@ writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS, DI.IsLittleEndian); } - ZeroFillBytes(OS, Range.AddrSize * 2); + ZeroFillBytes(OS, Range.SegSize + Range.AddrSize * 2); } } 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 @@ -25,6 +25,8 @@ SetVector SecNames; if (!DebugStrings.empty()) SecNames.insert("debug_str"); + if (!ARanges.empty()) + SecNames.insert("debug_aranges"); return SecNames; } diff --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -819,6 +819,8 @@ uint64_t BeginOffset = OS.tell(); if (Name == ".debug_str") DWARFYAML::EmitDebugStr(OS, DWARF); + else if (Name == ".debug_aranges") + DWARFYAML::EmitDebugAranges(OS, DWARF); else llvm_unreachable("unexpected emitDWARF() call"); diff --git a/llvm/lib/ObjectYAML/ELFYAML.cpp b/llvm/lib/ObjectYAML/ELFYAML.cpp --- a/llvm/lib/ObjectYAML/ELFYAML.cpp +++ b/llvm/lib/ObjectYAML/ELFYAML.cpp @@ -1654,6 +1654,9 @@ IO.mapOptional("Symbols", Object.Symbols); IO.mapOptional("DynamicSymbols", Object.DynamicSymbols); IO.mapOptional("DWARF", Object.DWARF); + if (Object.DWARF) + Object.DWARF->IsLittleEndian = + Object.Header.Data == ELFYAML::ELF_ELFDATA(ELF::ELFDATA2LSB); IO.setContext(nullptr); } diff --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml new file mode 100644 --- /dev/null +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml @@ -0,0 +1,349 @@ +## Test that yaml2obj emits .debug_aranges section. + +## a) Generate the .debug_aranges section from the "DWARF" entry. + +## Generate and verify the little endian object file. +# RUN: yaml2obj --docnum=1 %s -o %t1.o +# RUN: llvm-readobj -S --section-data %t1.o | \ +# RUN: FileCheck %s -DADDRALIGN="1" --check-prefixes=DWARF-LE-DEFAULT,DWARF-LE-CONTENT + +# DWARF-LE-DEFAULT: Index: 1 +# DWARF-LE-DEFAULT-NEXT: Name: .debug_aranges (1) +# DWARF-LE-DEFAULT-NEXT: Type: SHT_PROGBITS (0x1) +# DWARF-LE-DEFAULT-NEXT: Flags [ (0x0) +# DWARF-LE-DEFAULT-NEXT: ] +# DWARF-LE-DEFAULT-NEXT: Address: 0x0 +# DWARF-LE-DEFAULT-NEXT: Offset: 0x40 +# DWARF-LE-DEFAULT-NEXT: Size: 112 +# DWARF-LE-DEFAULT-NEXT: Link: 0 +# DWARF-LE-DEFAULT-NEXT: Info: 0 +# DWARF-LE-DEFAULT-NEXT: AddressAlignment: [[ADDRALIGN]] +# DWARF-LE-DEFAULT-NEXT: EntrySize: 0 +# DWARF-LE-CONTENT: SectionData ( +# DWARF-LE-CONTENT-NEXT: 0000: 2C000000 02000000 00000800 00000000 +## | | | | | | +## | | | | | +------- Padding zeros (4-byte) +## | | | | +- SegSize (1-byte) 0x00 +## | | | +- AddrSize (1-byte) 0x08 +## | | +--- ---- CuOffset (4-byte) 0x00 +## | +--- Version (2-byte) 0x02 +## +------- InitialLength (4-byte) 0x2c +## +# DWARF-LE-CONTENT-NEXT: 0010: 34120000 00000000 20000000 00000000 +## | | +## | +---------------- Length (8-byte) 0x20 +## +------- -------- Address (8-byte) 0x1234 +## +# DWARF-LE-CONTENT-NEXT: 0020: 00000000 00000000 00000000 00000000 +## | +## +------- -------- -------- -------- Terminating entry +## +# DWARF-LE-CONTENT-NEXT: 0030: FFFFFFFF 34000000 00000000 02006500 +## | | | | +## | | | +--- CuOffset (8-byte for 64-bit) 0x65 +## | | +--- Version (2-byte) 0x02 +## | +------- -------- InitialLength (16-byte) 0x2c +## +------- InitialLength (4-byte) 0xffffffff +## +# DWARF-LE-CONTENT-NEXT: 0040: 00000000 00000800 00000000 00000000 +## | | | +## | | +------- -------- -------- Padding zeros +## | +- SegSize (1-byte) 0x00 +## +- AddrSize (1-byte) 0x08 +## +# DWARF-LE-CONTENT-NEXT: 0050: 78560000 00000000 2A000000 00000000 +## | | +## | +------- -------- Length (8-byte) 0x2a +## +------- -------- Address (8-byte) 0x5678 +# DWARF-LE-CONTENT-NEXT: 0060: 00000000 00000000 00000000 00000000 +## | +## +------- -------- -------- -------- Terminating entry +# DWARF-LE-CONTENT-NEXT: ) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000001234 + Length: 0x20 + - Length: + TotalLength: 0xffffffff + TotalLength64: 0x34 + Version: 2 + CuOffset: 0x65 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000005678 + Length: 0x2a + +## Generate and verify the big endian object file. +# RUN: yaml2obj --docnum=2 %s -o %t2.o +# RUN: llvm-readobj -S --section-data %t2.o | FileCheck %s --check-prefix=DWARF-BE-DEFAULT + +# DWARF-BE-DEFAULT: Section { +# DWARF-BE-DEFAULT: Index: 1 +# DWARF-BE-DEFAULT-NEXT: Name: .debug_aranges (1) +# DWARF-BE-DEFAULT-NEXT: Type: SHT_PROGBITS (0x1) +# DWARF-BE-DEFAULT-NEXT: Flags [ (0x0) +# DWARF-BE-DEFAULT-NEXT: ] +# DWARF-BE-DEFAULT-NEXT: Address: 0x0 +# DWARF-BE-DEFAULT-NEXT: Offset: 0x40 +# DWARF-BE-DEFAULT-NEXT: Size: 112 +# DWARF-BE-DEFAULT-NEXT: Link: 0 +# DWARF-BE-DEFAULT-NEXT: Info: 0 +# DWARF-BE-DEFAULT-NEXT: AddressAlignment: 1 +# DWARF-BE-DEFAULT-NEXT: EntrySize: 0 +# DWARF-BE-DEFAULT-NEXT: SectionData ( +# DWARF-BE-DEFAULT-NEXT: 0000: 0000002C 00020000 00000800 00000000 +## | | | | | | +## | | | | | +------- Padding zeros (4-byte) +## | | | | +- SegSize (1-byte) 0x00 +## | | | +- AddrSize (1-byte) 0x08 +## | | +--- ---- CuOffset (4-byte) 0x00 +## | +--- Version (2-byte) 0x02 +## +------- InitialLength (4-byte) 0x2c +## +# DWARF-BE-DEFAULT-NEXT: 0010: 00000000 00001234 00000000 00000020 +## | | +## | +---------------- Length (8-byte) 0x20 +## +------- -------- Address (8-byte) 0x1234 +## +# DWARF-BE-DEFAULT-NEXT: 0020: 00000000 00000000 00000000 00000000 +## | +## +------- -------- -------- -------- Terminating entry +## +# DWARF-BE-DEFAULT-NEXT: 0030: FFFFFFFF 00000000 00000034 00020000 +## | | | | +## | | | +--- CuOffset (8-byte for 64-bit) 0x65 +## | | +--- Version (2-byte) 0x02 +## | +------- -------- InitialLength (16-byte) 0x2c +## +------- InitialLength (4-byte) 0xffffffff +## +# DWARF-BE-DEFAULT-NEXT: 0040: 00000000 00650800 00000000 00000000 +## | | | +## | | +------- -------- -------- Padding zeros +## | +- SegSize (1-byte) 0x00 +## +- AddrSize (1-byte) 0x08 +## +# DWARF-BE-DEFAULT-NEXT: 0050: 00000000 00005678 00000000 0000002A +## | | +## | +------- -------- Length (8-byte) 0x2a +## +------- -------- Address (8-byte) 0x5678 +# DWARF-BE-DEFAULT-NEXT: 0060: 00000000 00000000 00000000 00000000 +## | +## +------- -------- -------- -------- Terminating entry +# DWARF-BE-DEFAULT-NEXT: ) +# DWARF-BE-DEFAULT-NEXT: } + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000001234 + Length: 0x20 + - Length: + TotalLength: 0xffffffff + TotalLength64: 0x34 + Version: 2 + CuOffset: 0x65 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000005678 + Length: 0x2a + +## b) Generate the .debug_aranges section from the raw section content. + +# RUN: yaml2obj --docnum=3 %s -o %t3.o +# RUN: llvm-readobj -S --section-data %t3.o | \ +# RUN: FileCheck %s -DADDRALIGN="0" --check-prefixes=DWARF-LE-DEFAULT,DWARF-LE-CONTENT + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_PROGBITS + Content: "2C0000000200000000000800000000003412000000000000200000000000000000000000000000000000000000000000FFFFFFFF3400000000000000020065000000000000000800000000000000000078560000000000002A0000000000000000000000000000000000000000000000" + +## c) Generate the .debug_aranges section when the "Size" is specified. + +# RUN: yaml2obj --docnum=4 %s -o %t4.o +# RUN: llvm-readobj --hex-dump=.debug_aranges %t4.o | FileCheck %s --check-prefix=SIZE + +# SIZE: Hex dump of section '.debug_aranges': +# SIZE-NEXT: 0x00000000 00000000 00000000 00000000 00000000 ................ +# SIZE-EMPTY: + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_PROGBITS + Size: 0x10 + +## d) Test that yaml2obj emits an error message when both the "Size" and the +## "debug_arange" entry are specified at the same time. + +# RUN: not yaml2obj --docnum=5 %s 2>&1 | FileCheck %s --check-prefix=ERROR + +# ERROR: yaml2obj: error: cannot specify section '.debug_aranges' contents in the 'DWARF' entry and the 'Content' or 'Size' in the 'Sections' entry at the same time + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_PROGBITS + Size: 0x10 +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000001234 + Length: 0x20 + +## e) Test that yaml2obj emits an error message when both the "Content" and the +## "debug_aranges" entry are specified at the same time. + +# RUN: not yaml2obj --docnum=6 %s 2>&1 | FileCheck %s --check-prefix=ERROR + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_PROGBITS + Content: "00" +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000001234 + Length: 0x20 + +## f) Test that all the properties can be overridden by the section header when +## the "debug_aranges" entry doesn't exist. + +# RUN: yaml2obj --docnum=7 %s -o %t7.o +# RUN: llvm-readelf -S %t7.o | FileCheck %s --check-prefix=OVERRIDDEN + +# OVERRIDDEN: [Nr] Name Type Address Off Size ES Flg Lk Inf Al +# OVERRIDDEN: [ 1] .debug_aranges STRTAB 0000000000002020 000050 000030 01 A 2 1 2 +# OVERRIDDEN-NEXT: [ 2] .sec STRTAB 0000000000000000 000080 000000 00 0 0 0 + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_STRTAB # SHT_PROGBITS by default. + Flags: [SHF_ALLOC] # 0 by default. + Link: .sec # 0 by default. + EntSize: 1 # 0 by default. + Info: 1 # 0 by default. + AddressAlign: 2 # 0 by default. + Address: 0x0000000000002020 # 0x00 by default. + Offset: 0x00000050 # 0x40 for the first section. + Size: 0x30 # Set the "Size" so that we can reuse the check tag "OVERRIDDEN". + - Name: .sec # Linked by .debug_aranges. + Type: SHT_STRTAB + +## g) Test that all the properties can be overridden by the section header when +## the "debug_aranges" entry exists. + +# RUN: yaml2obj --docnum=8 %s -o %t8.o +# RUN: llvm-readelf -S %t8.o | FileCheck %s --check-prefix=OVERRIDDEN + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_STRTAB # SHT_PROGBITS by default. + Flags: [SHF_ALLOC] # 0 by default. + Link: .sec # 0 by default. + EntSize: 1 # 0 by default. + Info: 1 # 0 by default. + AddressAlign: 2 # 1 by default. + Address: 0x0000000000002020 # 0x00 by default. + Offset: 0x00000050 # 0x40 for the first section. + - Name: .sec # Linked by .debug_aranges. + Type: SHT_STRTAB +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000001234 + Length: 0x20 + +## h) Test that yaml2obj will not generate the .debug_aranges section when the "DWARF" entry exists +## but the "debug_aranges" entry doesn't exist in the "DWARF" entry or the "Sections" entry. + +# RUN: yaml2obj --docnum=9 %s -o %t9.o +# RUN: llvm-readelf -S %t9.o | FileCheck /dev/null --implicit-check-not=.debug_aranges + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: