+2021-02-02  Simon Marchi  <simon.marchi@efficios.com>
+
+       * lib/dwarf.exp (rnglists): Add -no-offset-array option to
+       table proc.
+       * gdb.dwarf2/rnglists-sec-offset.exp: Add test for
+       .debug_rnglists table without offset array.
+       * gdb.dwarf2/loclists-sec-offset.exp: Add test for
+       .debug_loclists table without offset array.
+
 2021-02-02  Simon Marchi  <simon.marchi@efficios.com>
 
        * gdb.dwarf2/rnglists-sec-offset.exp: Add test for DW_AT_ranges
 
   return 4;
 }
 
+static int
+func5 (void)
+{
+  asm ("func5_label: .global func5_label\n");
+  return 5;
+}
+
+static int
+func6 (void)
+{
+  asm ("func6_label: .global func6_label\n");
+  return 6;
+}
+
 int
 main (void)
 {
   func2 ();
   func3 ();
   func4 ();
+  func5 ();
+  func6 ();
 }
 
     lassign [function_range func2 $srcdir/$subdir/$srcfile] func2_addr func2_len
     lassign [function_range func3 $srcdir/$subdir/$srcfile] func3_addr func3_len
     lassign [function_range func4 $srcdir/$subdir/$srcfile] func4_addr func4_len
+    lassign [function_range func5 $srcdir/$subdir/$srcfile] func5_addr func5_len
+    lassign [function_range func6 $srcdir/$subdir/$srcfile] func6_addr func6_len
 
     set asm_file [standard_output_file $srcfile2]
     Dwarf::assemble $asm_file {
        global func2_addr func2_len
        global func3_addr func3_len
        global func4_addr func4_len
+       global func5_addr func5_len
+       global func6_addr func6_len
        global is_64
 
        declare_labels cu_range_list foo_range_list
        } {
            declare_labels int_type1
            declare_labels int_type2
-           declare_labels foo_location_list bar_location_list
+           declare_labels int_type3
+           declare_labels foo_location_list
+           declare_labels bar_location_list
+           declare_labels baz_location_list
 
            DW_TAG_compile_unit {
            } {
            }
        }
 
+       # This CU uses the DW_FORM_sec_offset form to refer to the
+       # .debug_loclists section.  The corresponding contribution in the
+       # .debug_loclists section has no offset array.
+       cu {
+           version 5
+           is_64 $is_64
+       } {
+           DW_TAG_compile_unit {
+           } {
+               int_type3: DW_TAG_base_type {
+                   {DW_AT_byte_size 4 DW_FORM_data1}
+                   {DW_AT_encoding @DW_ATE_signed}
+                   {DW_AT_name "int"}
+               }
+
+               DW_TAG_variable {
+                   {DW_AT_name "baz"}
+                   {DW_AT_location $baz_location_list DW_FORM_sec_offset}
+                   {DW_AT_type :$int_type3}
+               }
+
+               DW_TAG_subprogram {
+                   {DW_AT_name "func5"}
+                   {DW_AT_low_pc $func5_addr}
+                   {DW_AT_high_pc $func5_len DW_FORM_udata}
+               }
+
+               DW_TAG_subprogram {
+                   {DW_AT_name "func6"}
+                   {DW_AT_low_pc $func6_addr}
+                   {DW_AT_high_pc $func6_len DW_FORM_udata}
+               }
+           }
+       }
+
        loclists -is-64 $is_64 {
            # The lists in this table are accessed by direct offset
            # (DW_FORM_sec_offset).
                    }
                }
            }
+
+           table -with-offset-array false {
+               baz_location_list: list_ {
+                   start_length $func5_addr $func5_len {
+                       DW_OP_constu 0x567890
+                       DW_OP_stack_value
+                   }
+
+                   start_length $func6_addr $func6_len {
+                       DW_OP_constu 0x678901
+                       DW_OP_stack_value
+                   }
+               }
+           }
        }
     }
 
     gdb_breakpoint "func2"
     gdb_breakpoint "func3"
     gdb_breakpoint "func4"
+    gdb_breakpoint "func5"
+    gdb_breakpoint "func6"
 
     gdb_continue_to_breakpoint "func1"
     with_test_prefix "at func1" {
     with_test_prefix "at func4" {
        gdb_test "print /x bar" " = 0x456789"
     }
+
+    gdb_continue_to_breakpoint "func5"
+    with_test_prefix "at func5" {
+       gdb_test "print /x baz" " = 0x567890"
+    }
+
+    gdb_continue_to_breakpoint "func6"
+    with_test_prefix "at func6" {
+       gdb_test "print /x baz" " = 0x678901"
+    }
 }
 
     Dwarf::assemble $asm_file {
        global is_64
 
-       declare_labels cu1_range_list cu2_range_list
-       declare_labels foo_range_list bar_range_list
+       declare_labels cu1_range_list cu2_range_list cu3_range_list
+       declare_labels foo_range_list bar_range_list baz_range_list
 
        # This CU uses the DW_FORM_sec_offset form to refer to the .debug_rnglists
        # section.
            }
        }
 
+       # This CU uses the DW_FORM_sec_offset form to refer to the .debug_rnglists
+       # section.  The corresponding contribution in the .debug_rnglists has no
+       # offset array.
+       cu {
+           version 5
+           is_64 $is_64
+       } {
+           DW_TAG_compile_unit {
+               {DW_AT_ranges $cu3_range_list DW_FORM_sec_offset}
+           } {
+               DW_TAG_subprogram {
+                   {DW_AT_name "baz"}
+                   {DW_AT_ranges $baz_range_list DW_FORM_sec_offset}
+               }
+           }
+       }
+
        rnglists -is-64 $is_64 {
            # The lists in this table are accessed by direct offset
            # (DW_FORM_sec_offset).
                    start_end 0x5000 0x5010
                }
            }
+
+           table -with-offset-array false {
+               # For the third CU.
+               cu3_range_list: list_ {
+                   start_end 0x6000 0x7000
+               }
+
+               # For the baz function.
+               baz_range_list: list_ {
+                   start_end 0x6000 0x6010
+               }
+           }
        }
     }
 
     # Sanity checks to make sure GDB slurped the symbols correctly.
     gdb_test "p/x &foo" " = 0x4000"
     gdb_test "p/x &bar" " = 0x5000"
+    gdb_test "p/x &baz" " = 0x6000"
 }
 
        # The -post-header-label option can be used to define a label just after
        # the header of the table.  This is the label that a DW_AT_rnglists_base
        # attribute will usually refer to.
+       #
+       # The `-with-offset-array true|false` option can be used to control
+       # whether the headers of the location list tables have an array of
+       # offset.  The default is true.
 
        proc table { args } {
            variable _debug_rnglists_table_count
            variable _debug_rnglists_offset_size
            variable _debug_rnglists_is_64_dwarf
 
-           parse_args {{post-header-label ""}}
+           parse_args {
+               {post-header-label ""}
+               {with-offset-array true}
+           }
 
            if { [llength $args] != 1 } {
                error "table proc expects one positional argument (body)"
            _op .2byte 5 "dwarf version"
            _op .byte $_debug_rnglists_addr_size "address size"
            _op .byte 0 "segment selector size"
-           _op .4byte "$_debug_rnglists_list_count" "offset entry count"
+
+           if { ${with-offset-array} } {
+             _op .4byte "$_debug_rnglists_list_count" "offset entry count"
+           } else {
+             _op .4byte 0 "offset entry count"
+           }
 
            define_label $post_header_label
 
            }
 
            # Emit the offset array.
-           for {set list_idx 0} {$list_idx < $_debug_rnglists_list_count} {incr list_idx} {
-               set list_label [_compute_list_label $list_idx]
-               _op .${_debug_rnglists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
+           if { ${with-offset-array} } {
+               for {set list_idx 0} {$list_idx < $_debug_rnglists_list_count} {incr list_idx} {
+                   set list_label [_compute_list_label $list_idx]
+                   _op .${_debug_rnglists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
+               }
            }
 
            # Emit the actual list data.
        # The -post-header-label option can be used to define a label just after the
        # header of the table.  This is the label that a DW_AT_loclists_base
        # attribute will usually refer to.
+       #
+       # The `-with-offset-array true|false` option can be used to control
+       # whether the headers of the location list tables have an array of
+       # offset.  The default is true.
 
        proc table { args } {
            variable _debug_loclists_table_count
            variable _debug_loclists_offset_size
            variable _debug_loclists_is_64_dwarf
 
-           parse_args {{post-header-label ""}}
+           parse_args {
+               {post-header-label ""}
+               {with-offset-array true}
+           }
 
            if { [llength $args] != 1 } {
                error "table proc expects one positional argument (body)"
            _op .2byte 5 "DWARF version"
            _op .byte $_debug_loclists_addr_size "address size"
            _op .byte 0 "segment selector size"
-           _op .4byte $_debug_loclists_list_count "offset entry count"
+
+           if { ${with-offset-array} } {
+             _op .4byte "$_debug_loclists_list_count" "offset entry count"
+           } else {
+             _op .4byte 0 "offset entry count"
+           }
 
            define_label $post_header_label
 
            }
 
            # Emit the offset array.
-           for {set list_idx 0} {$list_idx < $_debug_loclists_list_count} {incr list_idx} {
-               set list_label [_compute_list_label $list_idx]
-               _op .${_debug_loclists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
+           if { ${with-offset-array} } {
+               for {set list_idx 0} {$list_idx < $_debug_loclists_list_count} {incr list_idx} {
+                   set list_label [_compute_list_label $list_idx]
+                   _op .${_debug_loclists_offset_size}byte "$list_label - $post_header_label" "offset of list $list_idx"
+               }
            }
 
            # Emit the actual list data.