diff --git a/gdb/ChangeLog b/gdb/ChangeLog index f26d06e56b..a4400b3a77 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,9 @@ +2021-03-30 Simon Marchi + + PR gdb/27541 + * dwarf2/read.c (dwarf2_has_info): Don't share dwarf2_per_bfd + with objfiles using READNOW. + 2021-03-29 Tom Tromey * top.c (check_frame_language_change): Update. diff --git a/gdb/dwarf2/read.c b/gdb/dwarf2/read.c index 1b98b758c3..24183014cf 100644 --- a/gdb/dwarf2/read.c +++ b/gdb/dwarf2/read.c @@ -1951,9 +1951,14 @@ dwarf2_has_info (struct objfile *objfile, { dwarf2_per_bfd *per_bfd; - /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD - doesn't require relocations. */ - if (!gdb_bfd_requires_relocations (objfile->obfd)) + /* We can share a "dwarf2_per_bfd" with other objfiles if the + BFD doesn't require relocations. + + We don't share with objfiles for which -readnow was requested, + because it would complicate things when loading the same BFD with + -readnow and then without -readnow. */ + if (!gdb_bfd_requires_relocations (objfile->obfd) + && (objfile->flags & OBJF_READNOW) == 0) { /* See if one has been created for this BFD yet. */ per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd); diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index f70d56417a..c5cae58b39 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,11 @@ +2021-03-30 Simon Marchi + + PR gdb/27541 + * gdb.base/index-cache-load-twice.exp: Remove. + * gdb.base/index-cache-load-twice.c: Remove. + * gdb.dwarf2/per-bfd-sharing.exp: New. + * gdb.dwarf2/per-bfd-sharing.c: New. + 2021-03-30 Tom de Vries PR testsuite/27604 diff --git a/gdb/testsuite/gdb.base/index-cache-load-twice.exp b/gdb/testsuite/gdb.base/index-cache-load-twice.exp deleted file mode 100644 index f442527d81..0000000000 --- a/gdb/testsuite/gdb.base/index-cache-load-twice.exp +++ /dev/null @@ -1,42 +0,0 @@ -# Copyright 2020-2021 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 . - -# This test checks that loading a file twice with the index cache enabled does -# not crash. - -standard_testfile - -lassign [remote_exec host mktemp -d] ret cache_dir - -# The output of mktemp contains an end of line, remove it. -set cache_dir [string trimright $cache_dir \r\n] - -if { $ret != 0 } { - fail "couldn't create temporary cache dir" - return -} - -save_vars { GDBFLAGS } { - set GDBFLAGS "$GDBFLAGS -iex \"set index-cache directory $cache_dir\"" - set GDBFLAGS "$GDBFLAGS -iex \"set index-cache on\"" - - if { [prepare_for_testing "failed to prepare" $testfile $srcfile \ - {debug additional_flags=-Wl,--build-id}] } { - return - } - - # This file command would generate an internal error. - gdb_file_cmd [standard_output_file $testfile] -} diff --git a/gdb/testsuite/gdb.base/index-cache-load-twice.c b/gdb/testsuite/gdb.dwarf2/per-bfd-sharing.c similarity index 91% rename from gdb/testsuite/gdb.base/index-cache-load-twice.c rename to gdb/testsuite/gdb.dwarf2/per-bfd-sharing.c index 76c0dea3f1..455ea3d540 100644 --- a/gdb/testsuite/gdb.base/index-cache-load-twice.c +++ b/gdb/testsuite/gdb.dwarf2/per-bfd-sharing.c @@ -15,8 +15,14 @@ You should have received a copy of the GNU General Public License along with this program. If not, see . */ +static +int foo (int a, int b) +{ + return a + b; +} + int main (void) { - return 0; + return foo (2, 3); } diff --git a/gdb/testsuite/gdb.dwarf2/per-bfd-sharing.exp b/gdb/testsuite/gdb.dwarf2/per-bfd-sharing.exp new file mode 100644 index 0000000000..22ab91f8f6 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/per-bfd-sharing.exp @@ -0,0 +1,93 @@ +# Copyright 2020-2021 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 . + +# This test checks that loading a file with different methods (partial symtabs, +# index, readnow) does not crash. + +standard_testfile + +lassign [remote_exec host mktemp -d] ret cache_dir + +# The output of mktemp contains an end of line, remove it. +set cache_dir [string trimright $cache_dir \r\n] + +if { $ret != 0 } { + fail "couldn't create temporary cache dir" + return +} + +verbose -log "Index cache dir: $cache_dir" + +if { [build_executable "failed to prepare" $testfile $srcfile \ + {debug additional_flags=-Wl,--build-id}] == -1 } { + return +} + +# Populate the index-cache. +with_test_prefix "populate index cache" { + clean_restart + + gdb_test_no_output "set index-cache directory $cache_dir" \ + "set index-cache directory" + gdb_test_no_output "set index-cache on" + gdb_test "file $binfile" "Reading symbols from .*" "file" +} + +proc load_binary { method } { + global binfile + global hex + + if { $method == "standard" } { + gdb_test "file $binfile" "Reading symbols from .*" "file" + } elseif { $method == "index" } { + gdb_test_no_output "set index-cache on" + gdb_test "file $binfile" "Reading symbols from .*" "file index" + gdb_test_no_output "set index-cache off" + } elseif { $method == "readnow" } { + gdb_test "file -readnow $binfile" \ + "Reading symbols from .*Expanding full symbols from .*" \ + "file readnow" + } else { + error "unknown method" + } + + # Print a static function: seeing it and its signature confirms GDB + # sees some symbols. + gdb_test "print foo" " = {int \\(int, int\\)} $hex " +} + +set methods {standard index readnow} + +foreach_with_prefix first $methods { + foreach_with_prefix second $methods { + foreach_with_prefix third $methods { + # Start with a clean GDB. + clean_restart + + # Set the index cache dir, but don't enable the index-cache, it will + # be enabled only when needed, when loading a file with the "index" + # method. + gdb_test_no_output "set index-cache directory $cache_dir" \ + "set index-cache directory" + + # Avoid GDB asking whether we really want to load a new binary. + gdb_test_no_output "set confirm off" + + with_test_prefix "load first" { load_binary $first } + with_test_prefix "load second" { load_binary $second } + with_test_prefix "load third" { load_binary $third } + } + } +}