forked from Imagelibrary/binutils-gdb
The entry PC for a DIE, e.g. an inline function, might not be the base address of the DIE. Currently though, in block::entry_pc(), GDB always returns the base address (low-pc or the first address of the first range) as the entry PC. This commit extends the block class to carry the entry PC as a separate member variable. Then the DWARF reader is extended to read and set the entry PC for the block. Now in block::entry_pc(), if the entry PC has been set, this is the value returned. If the entry-pc has not been set to a specific value then the old behaviour of block::entry_pc() remains, GDB will use the block's base address. Not every DIE will set the entry-pc, but GDB still needs to have an entry-pc for every block, so the existing logic supplies the entry-pc for any block where the entry-pc was not set. The DWARF-5 spec for reading the entry PC is a super-set of the spec as found in DWARF-4. For example, if there is no DW_AT_entry_pc then DWARF-4 says to use DW_AT_low_pc while DWARF-5 says to use the base address, which is DW_AT_low_pc or the first address in the first range specified by DW_AT_ranges if there is no DW_AT_low_pc. I have taken the approach of just implementing the DWARF-5 spec for everyone. There doesn't seem to be any benefit to deliberately ignoring a ranges based entry PC value for DWARF-4. If some naughty compiler has emitted that, then lets use it. Similarly, DWARF-4 says that DW_AT_entry_pc is an address. DWARF-5 allows an address or a constant, where the constant is an offset from the base address. I allow both approaches for all DWARF versions. There doesn't seem to be any downsides to this approach. I ran into an issue when testing this patch where GCC would have the DW_AT_entry_pc point to an empty range. When GDB parses the ranges any empty ranges are ignored. As a consequence, the entry-pc appears to be outside the address range of a block. The empty range problem is certainly something that we can, and should address, but that is not the focus of this patch, so for now I'm ignoring that problem. What I have done is added a check: if the DW_AT_entry_pc is outside the range of a block then the entry-pc is ignored, GDB will then fall-back to its default algorithm for computing the entry-pc. If/when in the future we address the empty range problem, these DW_AT_entry_pc attributes will suddenly become valid and GDB will start using them. Until then, GDB continues to operate as it always has. An early version of this patch stored the entry-pc within the block like this: std::optional<CORE_ADDR> m_entry_pc; However, a concern was raised that this, on a 64-bit host, effectively increases the size of block by 16-bytes (8-bytes for the CORE_ADDR, and 8-bytes for the std::optional's bool plus padding). If we remove the std::optional part and just use a CORE_ADDR then we need to have a "special" address to indicate if m_entry_pc is in use or not. I don't really like using special addresses; different targets can access different address ranges, even zero is a valid address on some targets. However, Bernd Edlinger suggested storing the entry-pc as an offset, and I think that will resolve my concerns. So, we store the entry-pc as a signed offset from the block's base address (the first address of the first range, or the start() address value if there are now ranges). Remember, ranges can be out of order, in which case the first address of the first range might be greater than the entry-pc. When GDB needs to read the entry-pc we can add the offset onto the blocks base address to recalculate it. With this done, on a 64-bit host, block only needs to increase by 8-bytes. The inline-entry.exp test was originally contributed by Bernd here: https://inbox.sourceware.org/gdb-patches/AS1PR01MB94659E4D9B3F4A6006CC605FE4922@AS1PR01MB9465.eurprd01.prod.exchangelabs.com though I have made some edits, making more use of lib/gdb.exp functions, making the gdb_test output patterns a little tighter, and updating the test to run with Clang. I also moved the test to gdb.opt/ as that seemed like a better home for it. Co-Authored-By: Bernd Edlinger <bernd.edlinger@hotmail.de>
484 lines
13 KiB
Plaintext
484 lines
13 KiB
Plaintext
# Copyright 2024 Free Software Foundation, Inc.
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation; either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
# Test different ways in which DW_AT_entry_pc can be expressed in the
|
|
# DWARF. Also test with DWARF-4 and DWARF-5. See the individule test
|
|
# procs below precise details of what DW_AT_entry_pc forms are tested.
|
|
|
|
load_lib dwarf.exp
|
|
|
|
require dwarf2_support
|
|
|
|
standard_testfile
|
|
|
|
# This compiles the source file and starts and stops GDB, so run it
|
|
# before calling prepare_for_testing otherwise GDB will have exited.
|
|
get_func_info foo
|
|
|
|
if { [prepare_for_testing "failed to prepare" ${testfile} \
|
|
[list ${srcfile}]] } {
|
|
return -1
|
|
}
|
|
|
|
if ![runto_main] {
|
|
return -1
|
|
}
|
|
|
|
# Address for the middle of foo. This is used as our entry point when
|
|
# the entry_pc is defined as an address.
|
|
set foo_middle_addr [get_hexadecimal_valueof "&foo_middle" "UNKNOWN" \
|
|
"get address for middle of foo"]
|
|
|
|
# The FOO_START and FOO_END we get from get_func_info is an expression
|
|
# involving symbols and offsets. To check the 'maint info blocks'
|
|
# output we need these converted into actual addresses.
|
|
set foo_start_addr [get_hexadecimal_valueof "$foo_start" "UNKNOWN" \
|
|
"get address for start of foo"]
|
|
set foo_end_addr [get_hexadecimal_valueof "$foo_end" "UNKNOWN" \
|
|
"get address for end of foo"]
|
|
|
|
# The ranges within foo. Used when foo is defined using ranges rather
|
|
# than a low pc and high pc pair. The entry point is in the middle of
|
|
# the second range.
|
|
foreach var { r1_s r1_e r2_s r2_e r3_s r3_e } {
|
|
set $var [get_hexadecimal_valueof "&foo_$var" "UNKNOWN" \
|
|
"get address for foo_$var"]
|
|
}
|
|
|
|
# Line on which 'foo' is declared. Used in generated debug.
|
|
set foo_decl_line [gdb_get_line_number "foo decl line"]
|
|
|
|
if [is_ilp32_target] {
|
|
set ptr_type "data4"
|
|
} else {
|
|
set ptr_type "data8"
|
|
}
|
|
|
|
# Generate a suffix number. Called from each of the test procs below
|
|
# to acquire a unique suffix for naming asm files and executables.
|
|
|
|
set global_test_suffix 0
|
|
proc get_next_suffix {} {
|
|
global global_test_suffix
|
|
incr global_test_suffix
|
|
|
|
return $global_test_suffix
|
|
}
|
|
|
|
# Helper for the two build_and_test_* procs below. Combine ASM_FILE
|
|
# with the global SRCFILE and build an executable. Use SUFFIX to give
|
|
# the executable a unique name.
|
|
|
|
proc build_and_runto_main { suffix asm_file } {
|
|
if {[prepare_for_testing "failed to prepare" "${::testfile}-${suffix}" \
|
|
[list $::srcfile $asm_file] {nodebug}]} {
|
|
return false
|
|
}
|
|
|
|
if ![runto_main] {
|
|
return false
|
|
}
|
|
|
|
# Ensure the CU containing 'foo' is expanded, so the blocks are
|
|
# visible.
|
|
gdb_test "info function foo" \
|
|
"File \[^\r\n\]+/$::srcfile:\r\n$::foo_decl_line:\\s+void foo\\(\\);.*"
|
|
|
|
return true
|
|
}
|
|
|
|
|
|
# Combine ASM_FILE with the global SRCFILE and build an executable,
|
|
# use SUFFIX to make the executable name unique.
|
|
#
|
|
# Then check the blocks at the symbol `foo_middle'. The inner most
|
|
# block should be a block for 'foo' with a continuous address range
|
|
# and an entry address of ENTRY_PC.
|
|
|
|
proc build_and_test_continuous { suffix asm_file entry_pc } {
|
|
if { ![build_and_runto_main $suffix $asm_file] } {
|
|
return false
|
|
}
|
|
|
|
gdb_test "maint info blocks foo_middle" \
|
|
[multi_line \
|
|
"\\\[\[^\]\]+\\\] $::foo_start_addr\.\.$::foo_end_addr" \
|
|
" entry pc: $entry_pc" \
|
|
" function: foo" \
|
|
" is contiguous"]
|
|
}
|
|
|
|
# Combine ASM_FILE with the global SRCFILE and build an executable,
|
|
# use SUFFIX to make the executable name unique.
|
|
#
|
|
# Then check the blocks at the symbol `foo_middle'. The inner most
|
|
# block should be a block for 'foo' which has 3 address ranges and an
|
|
# entry address of ENTRY_PC.
|
|
|
|
proc build_and_test_ranged { suffix asm_file entry_pc } {
|
|
if { ![build_and_runto_main $suffix $asm_file] } {
|
|
return false
|
|
}
|
|
|
|
gdb_test "maint info blocks foo_middle" \
|
|
[multi_line \
|
|
"\\\[\[^\]\]+\\\] $::r1_s\.\.$::r3_e" \
|
|
" entry pc: $entry_pc" \
|
|
" function: foo" \
|
|
" address ranges:" \
|
|
" $::r1_s\.\.$::r1_e" \
|
|
" $::r2_s\.\.$::r2_e" \
|
|
" $::r3_s\.\.$::r3_e" ]
|
|
}
|
|
|
|
# The function's address range is defined using low/high bounds and
|
|
# the entry_pc attribute is not given. The function's entry PC will
|
|
# default to the low address.
|
|
|
|
proc_with_prefix use_low_high_bounds_without_entry_pc { dwarf_vesion } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
global srcfile
|
|
|
|
declare_labels lines_table
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{low_pc $::foo_start addr}
|
|
{high_pc $::foo_len $::ptr_type}
|
|
{external 1 flag}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
}
|
|
|
|
build_and_test_continuous $suffix $asm_file $::foo_start_addr
|
|
}
|
|
|
|
# The function's address range is defined using low/high bounds and an
|
|
# entry_pc attribute is given (which contains an address), which will
|
|
# be used as the function's entry address.
|
|
|
|
proc_with_prefix use_low_high_bounds_with_entry_pc { dwarf_version } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
global srcfile
|
|
|
|
declare_labels lines_table
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{low_pc $::foo_start addr}
|
|
{high_pc $::foo_len $::ptr_type}
|
|
{external 1 flag}
|
|
{entry_pc foo_middle addr}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
}
|
|
|
|
build_and_test_continuous $suffix $asm_file $::foo_middle_addr
|
|
}
|
|
|
|
# The function's address range is defined using low/high bounds and an
|
|
# entry_pc attribute is given (which contains an offset from the base
|
|
# address), which will be used to compute the function's entry address.
|
|
|
|
proc_with_prefix use_low_high_bounds_with_entry_offset { dwarf_version } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
global srcfile
|
|
|
|
declare_labels lines_table
|
|
|
|
set foo_offset [expr $::foo_middle_addr - $::foo_start_addr]
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{low_pc $::foo_start addr}
|
|
{high_pc $::foo_len $::ptr_type}
|
|
{external 1 flag}
|
|
{entry_pc $foo_offset data4}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
}
|
|
|
|
build_and_test_continuous $suffix $asm_file $::foo_middle_addr
|
|
}
|
|
|
|
# The function's address range is defined using range information. No
|
|
# entry_pc attribute is used. The entry PC for the function will
|
|
# default to the first address of the first range.
|
|
|
|
proc_with_prefix use_ranges_without_entry_pc { dwarf_version } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
upvar dwarf_version dwarf_version
|
|
global srcfile
|
|
|
|
declare_labels lines_table ranges_label
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
{low_pc 0 addr}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{external 1 flag}
|
|
{ranges ${ranges_label} DW_FORM_sec_offset}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
|
|
if { $dwarf_version == 5 } {
|
|
rnglists {} {
|
|
table {} {
|
|
ranges_label: list_ {
|
|
start_end foo_r1_s foo_r1_e
|
|
start_end foo_r2_s foo_r2_e
|
|
start_end foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ranges { } {
|
|
ranges_label: sequence {
|
|
range foo_r1_s foo_r1_e
|
|
range foo_r2_s foo_r2_e
|
|
range foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
build_and_test_ranged $suffix $asm_file $::r1_s
|
|
}
|
|
|
|
# The function's address range is defined using range information and
|
|
# an entry_pc attribute (which is an address) is used, this will be
|
|
# the entry PC for the function.
|
|
|
|
proc_with_prefix use_ranges_with_entry_pc { dwarf_version } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
upvar dwarf_version dwarf_version
|
|
global srcfile
|
|
|
|
declare_labels lines_table ranges_label
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
{low_pc 0 addr}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{external 1 flag}
|
|
{ranges ${ranges_label} DW_FORM_sec_offset}
|
|
{entry_pc foo_middle addr}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
|
|
if { $dwarf_version == 5 } {
|
|
rnglists {} {
|
|
table {} {
|
|
ranges_label: list_ {
|
|
start_end foo_r1_s foo_r1_e
|
|
start_end foo_r2_s foo_r2_e
|
|
start_end foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ranges { } {
|
|
ranges_label: sequence {
|
|
range foo_r1_s foo_r1_e
|
|
range foo_r2_s foo_r2_e
|
|
range foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
build_and_test_ranged $suffix $asm_file $::foo_middle_addr
|
|
}
|
|
|
|
# The function's address range is defined using range information and
|
|
# an entry_pc attribute (which is an offset) is used, this will be
|
|
# used to calculate the entry PC for the function.
|
|
|
|
proc_with_prefix use_ranges_with_entry_offset { dwarf_version } {
|
|
set suffix [get_next_suffix]
|
|
|
|
# Make some DWARF for the test.
|
|
set asm_file [standard_output_file "$::testfile-dw-$suffix.S"]
|
|
Dwarf::assemble $asm_file {
|
|
upvar dwarf_version dwarf_version
|
|
global srcfile
|
|
|
|
declare_labels lines_table ranges_label
|
|
|
|
set foo_offset [expr $::foo_middle_addr - $::r1_s]
|
|
|
|
cu { version $::dwarf_version } {
|
|
compile_unit {
|
|
{producer "gcc"}
|
|
{language @DW_LANG_C}
|
|
{name ${srcfile}}
|
|
{comp_dir /tmp}
|
|
{stmt_list $lines_table DW_FORM_sec_offset}
|
|
{low_pc 0 addr}
|
|
} {
|
|
subprogram {
|
|
{name foo}
|
|
{decl_file 1 data1}
|
|
{decl_line $::foo_decl_line data1}
|
|
{decl_column 1 data1}
|
|
{external 1 flag}
|
|
{ranges ${ranges_label} DW_FORM_sec_offset}
|
|
{entry_pc $foo_offset data4}
|
|
}
|
|
}
|
|
}
|
|
|
|
lines {version 2} lines_table {
|
|
include_dir "$::srcdir/$::subdir"
|
|
file_name "$srcfile" 1
|
|
}
|
|
|
|
if { $dwarf_version == 5 } {
|
|
rnglists {} {
|
|
table {} {
|
|
ranges_label: list_ {
|
|
start_end foo_r1_s foo_r1_e
|
|
start_end foo_r2_s foo_r2_e
|
|
start_end foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
ranges { } {
|
|
ranges_label: sequence {
|
|
range foo_r1_s foo_r1_e
|
|
range foo_r2_s foo_r2_e
|
|
range foo_r3_s foo_r3_e
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
build_and_test_ranged $suffix $asm_file $::foo_middle_addr
|
|
}
|
|
|
|
# Run the tests.
|
|
foreach_with_prefix dwarf_version { 4 5 } {
|
|
use_low_high_bounds_without_entry_pc $dwarf_version
|
|
use_low_high_bounds_with_entry_offset $dwarf_version
|
|
use_low_high_bounds_with_entry_pc $dwarf_version
|
|
use_ranges_without_entry_pc $dwarf_version
|
|
use_ranges_with_entry_pc $dwarf_version
|
|
use_ranges_with_entry_offset $dwarf_version
|
|
}
|