[llvm] 2880254 - [ObjectYAML][DWARF] Support emitting .debug_ranges section in ELFYAML.

Xing GUO via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 7 00:44:15 PDT 2020


Author: Xing GUO
Date: 2020-06-07T15:47:47+08:00
New Revision: 288025494ef460c9c2481039be61cd53116d06ba

URL: https://github.com/llvm/llvm-project/commit/288025494ef460c9c2481039be61cd53116d06ba
DIFF: https://github.com/llvm/llvm-project/commit/288025494ef460c9c2481039be61cd53116d06ba.diff

LOG: [ObjectYAML][DWARF] Support emitting .debug_ranges section in ELFYAML.

This patch enables yaml2elf to emit the .debug_ranges section.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D81217

Added: 
    llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml

Modified: 
    llvm/lib/ObjectYAML/DWARFYAML.cpp
    llvm/lib/ObjectYAML/ELFEmitter.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/ObjectYAML/DWARFYAML.cpp b/llvm/lib/ObjectYAML/DWARFYAML.cpp
index adf92445dbb4..7c006741bbe6 100644
--- a/llvm/lib/ObjectYAML/DWARFYAML.cpp
+++ b/llvm/lib/ObjectYAML/DWARFYAML.cpp
@@ -28,6 +28,8 @@ SetVector<StringRef> DWARFYAML::Data::getUsedSectionNames() const {
     SecNames.insert("debug_str");
   if (!ARanges.empty())
     SecNames.insert("debug_aranges");
+  if (!DebugRanges.empty())
+    SecNames.insert("debug_ranges");
   return SecNames;
 }
 

diff  --git a/llvm/lib/ObjectYAML/ELFEmitter.cpp b/llvm/lib/ObjectYAML/ELFEmitter.cpp
index ae2f7ed48b93..16cb3c75c62e 100644
--- a/llvm/lib/ObjectYAML/ELFEmitter.cpp
+++ b/llvm/lib/ObjectYAML/ELFEmitter.cpp
@@ -852,6 +852,8 @@ uint64_t emitDWARF(typename ELFT::Shdr &SHeader, StringRef Name,
     DWARFYAML::EmitDebugStr(OS, DWARF);
   else if (Name == ".debug_aranges")
     DWARFYAML::EmitDebugAranges(OS, DWARF);
+  else if (Name == ".debug_ranges")
+    DWARFYAML::EmitDebugRanges(OS, DWARF);
   else
     llvm_unreachable("unexpected emitDWARF() call");
 

diff  --git a/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
new file mode 100644
index 000000000000..48fdc6cd7147
--- /dev/null
+++ b/llvm/test/tools/yaml2obj/ELF/DWARF/debug-ranges.yaml
@@ -0,0 +1,315 @@
+## Test that yaml2obj emits .debug_ranges section.
+
+## a) Generate the .debug_ranges section from the "DWARF" entry.
+
+## Generate and verify a little endian .debug_ranges section.
+
+# RUN: yaml2obj --docnum=1 -DENDIAN=ELFDATA2LSB %s -o %t.le.o
+# RUN: llvm-readobj --sections --section-data %t.le.o | \
+# RUN:   FileCheck %s -DSIZE=120 -DADDRALIGN=1 --check-prefixes=DWARF-HEADER,DWARF-LE-CONTENT
+
+#          DWARF-HEADER: Index: 1
+#     DWARF-HEADER-NEXT: Name: .debug_ranges (1)
+#     DWARF-HEADER-NEXT: Type: SHT_PROGBITS (0x1)
+#     DWARF-HEADER-NEXT: Flags [ (0x0)
+#     DWARF-HEADER-NEXT: ]
+#     DWARF-HEADER-NEXT: Address: 0x0
+#     DWARF-HEADER-NEXT: Offset: 0x40
+#     DWARF-HEADER-NEXT: Size: [[SIZE]]
+#     DWARF-HEADER-NEXT: Link: 0
+#     DWARF-HEADER-NEXT: Info: 0
+#     DWARF-HEADER-NEXT: AddressAlignment: [[ADDRALIGN]]
+#     DWARF-HEADER-NEXT: EntrySize: 0
+# DWARF-LE-CONTENT-NEXT: SectionData (
+# DWARF-LE-CONTENT-NEXT:   0000: 10000000 20000000 30000000 40000000
+##                               |        |        |        |
+##                               |        |        |        +------- HighOffset (4-byte) 0x40
+##                               |        |        +------- LowOffset (4-byte) 0x30
+##                               |        +------- HighOffset (4-byte) 0x20
+##                               +------- LowOffset (4-byte) 0x10
+##
+# DWARF-LE-CONTENT-NEXT:   0010: FFFFFFFF 10000000 00000000 00000000
+##                               |        |        |        |
+##                               |        |        |        +------- HighOffset (4-byte) 0x00
+##                               |        |        +------- LowOffset (4-byte) 0x00
+##                               |        +------- Base Address (4-byte) 0x10
+##                               +------- Base Address Entry (4-byte) UINT32_MAX
+##
+# DWARF-LE-CONTENT-NEXT:   0020: 00000000 00000000 10000000 00000000
+##                               |                 |
+##                               |                 +---------------- LowOffset (8-byte) 0x10
+##                               +---------------- Terminating Entry (8-byte)
+##
+# DWARF-LE-CONTENT-NEXT:   0030: 20000000 00000000 30000000 00000000
+##                               |                 |
+##                               |                 +---------------- LowOffset (8-byte) 0x30
+##                               +---------------- HighOffset (8-byte) 0x20
+##
+# DWARF-LE-CONTENT-NEXT:   0040: 40000000 00000000 FFFFFFFF FFFFFFFF
+##                               |                 |
+##                               |                 +---------------- Base Address Entry (8-byte) UINT64_MAX
+##                               +---------------- HighOffset (8-byte) 0x40
+##
+# DWARF-LE-CONTENT-NEXT:   0050: 10000000 00000000 00000000 00000000
+##                               |                 |
+##                               |                 +---------------- LowOffset (8-byte) 0x00
+##                               +---------------- Base Address (8-byte) 0x10
+##
+# DWARF-LE-CONTENT-NEXT:   0060: 00000000 00000000 00000000 00000000
+##                               |                 |
+##                               |                 +---------------- Terminating Entry (16-byte)
+##                               +---------------- HighOffset (8-byte) 0x00
+##
+# DWARF-LE-CONTENT-NEXT:   0070: 00000000 00000000
+##                               |
+##                               +---------------- Terminating Entry (lower 8-byte)
+##
+# DWARF-LE-CONTENT-NEXT: )
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    [[ENDIAN]]
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+DWARF:
+  debug_ranges:
+    - Offset:   0
+      AddrSize: 0x04
+      Entries:
+        - LowOffset:  0x00000010
+          HighOffset: 0x00000020
+        - LowOffset:  0x00000030
+          HighOffset: 0x00000040
+        - LowOffset:  0xffffffff
+          HighOffset: 0x00000010
+        - LowOffset:  0x00000000
+          HighOffset: 0x00000000
+    - Offset:   40
+      AddrSize: 0x08
+      Entries:
+        - LowOffset:  0x0000000000000010
+          HighOffset: 0x0000000000000020
+        - LowOffset:  0x0000000000000030
+          HighOffset: 0x0000000000000040
+        - LowOffset:  0xffffffffffffffff
+          HighOffset: 0x0000000000000010
+        - LowOffset:  0x0000000000000000
+          HighOffset: 0x0000000000000000
+
+## Generate and verify a big endian .debug_ranges section.
+
+# RUN: yaml2obj --docnum=1 -DENDIAN=ELFDATA2MSB %s -o %t.be.o
+# RUN: llvm-readobj --sections --section-data %t.be.o | \
+# RUN:   FileCheck %s -DSIZE=120 -DADDRALIGN=1 --check-prefixes=DWARF-HEADER,DWARF-BE-CONTENT
+
+# DWARF-BE-CONTENT-NEXT: SectionData (
+# DWARF-BE-CONTENT-NEXT: 0000: 00000010 00000020 00000030 00000040
+##                             |        |        |        |
+##                             |        |        |        +------- HighOffset (4-byte) 0x40
+##                             |        |        +------- LowOffset (4-byte) 0x30
+##                             |        +------- HighOffset (4-byte) 0x20
+##                             +------- LowOffset (4-byte) 0x10
+##
+# DWARF-BE-CONTENT-NEXT: 0010: FFFFFFFF 00000010 00000000 00000000
+##                             |        |        |        |
+##                             |        |        |        +------- HighOffset (4-byte) 0x00
+##                             |        |        +------- LowOffset (4-byte) 0x00
+##                             |        +------- Base Address (4-byte) 0x10
+##                             +------- Base Address Entry (4-byte) UINT32_MAX
+##
+# DWARF-BE-CONTENT-NEXT: 0020: 00000000 00000000 00000000 00000010
+##                             |                 |
+##                             |                 +---------------- LowOffset (8-byte) 0x10
+##                             +---------------- Terminating Entry (8-byte)
+##
+# DWARF-BE-CONTENT-NEXT: 0030: 00000000 00000020 00000000 00000030
+##                             |                 |
+##                             |                 +---------------- LowOffset (8-byte) 0x30
+##                             +---------------- HighOffset (8-byte) 0x20
+##
+# DWARF-BE-CONTENT-NEXT: 0040: 00000000 00000040 FFFFFFFF FFFFFFFF
+##                             |                 |
+##                             |                 +---------------- Base Address Entry (8-byte) UINT64_MAX
+##                             +---------------- HighOffset (8-byte) 0x40
+##
+# DWARF-BE-CONTENT-NEXT: 0050: 00000000 00000010 00000000 00000000
+##                             |                 |
+##                             |                 +---------------- LowOffset (8-byte) 0x00
+##                             +---------------- Base Address (8-byte) 0x10
+##
+# DWARF-BE-CONTENT-NEXT: 0060: 00000000 00000000 00000000 00000000
+##                             |                 |
+##                             |                 +---------------- Terminating Entry (16-byte)
+##                             +---------------- HighOffset (8-byte) 0x00
+##
+# DWARF-BE-CONTENT-NEXT: 0070: 00000000 00000000
+##                             |
+##                             +---------------- Terminating Entry (lower 8-byte)
+##
+# DWARF-BE-CONTENT-NEXT: )
+
+## b) Generate the .debug_ranges section from raw section content.
+
+# RUN: yaml2obj --docnum=2 %s -o %t2.o
+# RUN: llvm-readobj --sections --section-data %t2.o | \
+# RUN:   FileCheck %s -DSIZE=3 -DADDRALIGN=0 --check-prefixes=DWARF-HEADER,ARBITRARY-CONTENT
+
+#      ARBITRARY-CONTENT: SectionData (
+# ARBITRARY-CONTENT-NEXT:   0000: 112233
+# ARBITRARY-CONTENT-NEXT: )
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:    .debug_ranges
+    Type:    SHT_PROGBITS
+    Content: "112233"
+
+## c) Generate the .debug_ranges section when the "Size" is specified.
+
+# RUN: yaml2obj --docnum=3 %s -o %t3.o
+# RUN: llvm-readobj --hex-dump=.debug_ranges %t3.o | FileCheck %s --check-prefix=SIZE
+
+#       SIZE: Hex dump of section '.debug_ranges':
+#  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_ranges
+    Type: SHT_PROGBITS
+    Size: 0x10
+
+## d) Test that yaml2obj emits an error message when both the "Size" and the
+## "debug_ranges" 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_ranges' 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_ranges
+    Type: SHT_PROGBITS
+    Size: 0x10
+DWARF:
+  debug_ranges:
+    - Offset:   0
+      AddrSize: 0x08
+      Entries:
+        - LowOffset:  0x0000000000000001
+          HighOffset: 0x0000000000000002
+
+## e) Test that yaml2obj emits an error message when both the "Content" and the
+## "debug_ranges" 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:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:    .debug_ranges
+    Type:    SHT_PROGBITS
+    Content: "00"
+DWARF:
+  debug_ranges:
+    - Offset:   0
+      AddrSize: 0x08
+      Entries:
+        - LowOffset:  0x0000000000000001
+          HighOffset: 0x0000000000000002
+
+## f) Test that all the properties can be overridden by the section header when
+## the "debug_ranges" 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_ranges STRTAB 0000000000002020 000050 000020 01   A  2   1  2
+# OVERRIDDEN-NEXT: [ 2] .sec          STRTAB 0000000000000000 000070 000000 00      0   0  0
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:         .debug_ranges
+    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:      0x2020      # 0x00 by default.
+    Offset:       0x50        # 0x40 for the first section.
+    Size:         0x20        # Set the "Size" so that we can reuse the check tag "OVERRIDDEN".
+  - Name:         .sec        # Linked by .debug_ranges.
+    Type:         SHT_STRTAB
+
+## g) Test that all the properties can be overridden by the section header when
+## the "debug_ranges" 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:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+Sections:
+  - Name:         .debug_ranges
+    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:      0x2020      # 0x00 by default.
+    Offset:       0x50        # 0x40 for the first section.
+  - Name:         .sec        # Linked by .debug_ranges.
+    Type:         SHT_STRTAB
+DWARF:
+  debug_ranges:
+    - Offset:   0
+      AddrSize: 0x08
+      Entries:
+        - LowOffset:  0x0000000000000001
+          HighOffset: 0x0000000000000002
+
+## h) Test that yaml2obj will not generate the .debug_ranges section when the "DWARF" entry
+## exists but the "debug_ranges" 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_ranges
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+DWARF:


        


More information about the llvm-commits mailing list