From a50f4eb2e91bc384fb8956a9637abb7e46e7ec84 Mon Sep 17 00:00:00 2001 From: Xing GUO Date: Thu, 4 Jun 2020 08:53:40 +0800 Subject: [PATCH] [ObjectYAML][DWARF] Support emitting the .debug_aranges section in ELFYAML. This patch enables yaml2obj to emit the .debug_aranges section in ELFYAML. Known issues: - The current implementation of `debug_aranges` doesn't support emitting `segment` in the `(segment, address, length)` tuple. I will fix it in a follow-up patch. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D80972 --- llvm/lib/ObjectYAML/DWARFYAML.cpp | 2 + llvm/lib/ObjectYAML/ELFEmitter.cpp | 2 + .../yaml2obj/ELF/DWARF/debug-aranges.yaml | 268 ++++++++++++++++++ 3 files changed, 272 insertions(+) create mode 100644 llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml diff --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp index 30a77c74dc6c..6e43e72ddd48 100644 --- a/llvm/lib/ObjectYAML/DWARFYAML.cpp +++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp @@ -25,6 +25,8 @@ SetVector DWARFYAML::Data::getUsedSectionNames() const { 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 index b87db48fa78c..342d98a39eb7 100644 --- a/llvm/lib/ObjectYAML/ELFEmitter.cpp +++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp @@ -820,6 +820,8 @@ uint64_t emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name, 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/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml new file mode 100644 index 000000000000..6c4556d22a1d --- /dev/null +++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-aranges.yaml @@ -0,0 +1,268 @@ +## Test that yaml2obj emits .debug_aranges section. + +## a) Generate the .debug_aranges section from the "DWARF" entry. + +## Generate and verify a big endian object file. + +# RUN: yaml2obj --docnum=1 -DENDIAN=ELFDATA2MSB %s -o %t1.o +# RUN: llvm-readobj --sections --section-data %t1.o | \ +# RUN: FileCheck %s -DADDRALIGN=1 -DSIZE=96 --check-prefixes=DWARF-BE-HEADER,DWARF-BE-CONTENT + +# DWARF-BE-HEADER: Index: 1 +# DWARF-BE-HEADER-NEXT: Name: .debug_aranges (1) +# DWARF-BE-HEADER-NEXT: Type: SHT_PROGBITS (0x1) +# DWARF-BE-HEADER-NEXT: Flags [ (0x0) +# DWARF-BE-HEADER-NEXT: ] +# DWARF-BE-HEADER-NEXT: Address: 0x0 +# DWARF-BE-HEADER-NEXT: Offset: 0x40 +# DWARF-BE-HEADER-NEXT: Size: [[SIZE]] +# DWARF-BE-HEADER-NEXT: Link: 0 +# DWARF-BE-HEADER-NEXT: Info: 0 +# DWARF-BE-HEADER-NEXT: AddressAlignment: [[ADDRALIGN]] +# DWARF-BE-HEADER-NEXT: EntrySize: 0 +# DWARF-BE-CONTENT: SectionData ( +# DWARF-BE-CONTENT-NEXT: 0000: 0000002C 00020000 00000400 00000000 +## | | | | | | +## | | | | | +------- Padding zeros (4-byte) +## | | | | +- SegSize (1-byte) 0x00 +## | | | +- AddrSize (1-byte) 0x04 +## | | +-------- CuOffset (4-byte) 0x00 +## | +--- Version (2-byte) 0x02 +## +------- InitialLength (4-byte) 0x2c +## +# DWARF-BE-CONTENT-NEXT: 0010: 00001234 00000020 00000000 00000000 +## | | | +## | | +---------------- Terminating Entry +## | +------- Length (4-byte) 0x20 +## +------- Address (4-byte) 0x1234 +## +# DWARF-BE-CONTENT-NEXT: 0020: 0000002C 00020000 00650800 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-CONTENT-NEXT: 0030: 00000000 00005678 00000000 00000020 +## | | +## | +---------------- Length (8-byte) 0x20 +## +---------------- Address (8-byte) 0x5678 +## +# DWARF-BE-CONTENT-NEXT: 0040: 00000000 56780000 00000000 00000010 +## | | +## | +---------------- Length (8-byte) 0x10 +## +---------------- Address (8-byte) 0x5678 +## +# DWARF-BE-CONTENT-NEXT: 0050: 00000000 00000000 00000000 00000000 +## | +## +---------------------------------- Terminating entry +## +# DWARF-BE-CONTENT-NEXT: ) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: [[ENDIAN]] + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: + debug_aranges: + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0 + AddrSize: 0x04 + SegSize: 0x00 + Descriptors: + - Address: 0x00001234 + Length: 0x20 + - Length: + TotalLength: 0x2c + Version: 2 + CuOffset: 0x65 + AddrSize: 0x08 + SegSize: 0x00 + Descriptors: + - Address: 0x0000000000005678 + Length: 0x20 + - Address: 0x0000000056780000 + Length: 0x10 + +## b) Generate the .debug_aranges section from raw section content. + +# RUN: yaml2obj --docnum=2 %s -o %t2.o +# RUN: llvm-readobj --sections --section-data %t2.o | \ +# RUN: FileCheck %s -DADDRALIGN=0 -DSIZE=3 --check-prefixes=DWARF-BE-HEADER,ARBITRARY-CONTENT + +# ARBITRARY-CONTENT: SectionData ( +# ARBITRARY-CONTENT-NEXT: 0000: 112233 +# ARBITRARY-CONTENT-NEXT: ) + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + Type: ET_EXEC + Machine: EM_X86_64 +Sections: + - Name: .debug_aranges + Type: SHT_PROGBITS + Content: "112233" + +## c) Generate the .debug_aranges section when the "Size" is specified. + +# RUN: yaml2obj --docnum=3 %s -o %t3.o +# RUN: llvm-readobj --hex-dump=.debug_aranges %t3.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: ELFDATA2MSB + 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_aranges" entry are specified at the same time. + +# RUN: not yaml2obj --docnum=4 %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: ELFDATA2MSB + 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=5 %s 2>&1 | FileCheck %s --check-prefix=ERROR + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + 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=6 %s -o %t6.o +# RUN: llvm-readelf --sections %t6.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: ELFDATA2MSB + 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=7 %s -o %t7.o +# RUN: llvm-readelf --sections %t7.o | FileCheck %s --check-prefix=OVERRIDDEN + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + 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=8 %s -o %t8.o +# RUN: llvm-readelf --sections %t8.o | FileCheck /dev/null --implicit-check-not=.debug_aranges + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2MSB + Type: ET_EXEC + Machine: EM_X86_64 +DWARF: