From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from barracuda.ebox.ca (barracuda.ebox.ca [96.127.255.19]) by sourceware.org (Postfix) with ESMTPS id DB4D5386F021 for ; Wed, 20 May 2020 19:22:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org DB4D5386F021 X-ASG-Debug-ID: 1590002547-0c856e6d38608a0001-fS2M51 Received: from smtp.ebox.ca (smtp.ebox.ca [96.127.255.82]) by barracuda.ebox.ca with ESMTP id Wvu0bd7c1Eh8slHD (version=TLSv1 cipher=DHE-RSA-AES256-SHA bits=256 verify=NO); Wed, 20 May 2020 15:22:27 -0400 (EDT) X-Barracuda-Envelope-From: simon.marchi@efficios.com X-Barracuda-RBL-Trusted-Forwarder: 96.127.255.82 Received: from smarchi-efficios.lan (unknown [192.222.164.54]) by smtp.ebox.ca (Postfix) with ESMTP id F2362441D64; Wed, 20 May 2020 15:22:26 -0400 (EDT) From: Simon Marchi X-Barracuda-Effective-Source-IP: 192-222-164-54.qc.cable.ebox.net[192.222.164.54] X-Barracuda-Apparent-Source-IP: 192.222.164.54 X-Barracuda-RBL-IP: 192.222.164.54 To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH] gdb: reset/recompute objfile section offsets in reread_symbols Date: Wed, 20 May 2020 15:22:26 -0400 X-ASG-Orig-Subj: [PATCH] gdb: reset/recompute objfile section offsets in reread_symbols Message-Id: <20200520192226.2889993-1-simon.marchi@efficios.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Barracuda-Connect: smtp.ebox.ca[96.127.255.82] X-Barracuda-Start-Time: 1590002547 X-Barracuda-Encrypted: DHE-RSA-AES256-SHA X-Barracuda-URL: https://96.127.255.19:443/cgi-mod/mark.cgi X-Barracuda-Scan-Msg-Size: 6306 X-Virus-Scanned: by bsmtpd at ebox.ca X-Barracuda-BRTS-Status: 1 X-Barracuda-Spam-Score: 0.50 X-Barracuda-Spam-Status: No, SCORE=0.50 using global scores of TAG_LEVEL=1000.0 QUARANTINE_LEVEL=1000.0 KILL_LEVEL=8.0 tests=BSF_RULE7568M X-Barracuda-Spam-Report: Code version 3.2, rules version 3.2.3.81986 Rule breakdown below pts rule name description ---- ---------------------- -------------------------------------------------- 0.50 BSF_RULE7568M Custom Rule 7568M X-Spam-Status: No, score=-25.1 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_SOFTFAIL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 20 May 2020 19:22:29 -0000 This patch started as an investigation of this bug, where the program is re-compiled between two "start" runs: $ ./gdb -nx --data-directory=data-directory -q a.out Reading symbols from a.out... (gdb) start Temporary breakpoint 1 at 0x1131: file test.c, line 1. Starting program: /home/smarchi/build/wt/test/gdb/a.out Temporary breakpoint 1, main () at test.c:1 1 int main() { return 0; } *** re-compile a.out *** (gdb) start The program being debugged has been started already. Start it from the beginning? (y or n) y `/home/smarchi/build/wt/test/gdb/a.out' has changed; re-reading symbols. Temporary breakpoint 2 at 0x555555555129: file test.c, line 1. Starting program: /home/smarchi/build/wt/test/gdb/a.out warning: Probes-based dynamic linker interface failed. Reverting to original interface. Temporary breakpoint 2, main () at test.c:1 1 int main() { return 0; } (gdb) To reproduce the bug, a.out needs to be a position-independent executable (PIE). Here's what happens: 1) We first read the symbols of a.out. The section offsets in the objfile are all 0, so the symbols are created unrelocated. 2) The breakpoint on main is created, as you can see the breakpoint address (derived from the `main` symbol with value 0x1129) is still unrelocated (0x1131). Since the program is not yet started, we don't know at which base address the executable is going to end at. Everything good so far. 3) The execution starts, GDB finds out the executable's base address, fills the objfile's section_offsets vector with a bunch of offsets, and relocates the symbols with those offsets. The latter modifies the symbol values (the `main` symbol is changed from 0x1129 to 0x555555555129). 4) We `start` again, we detect that `a.out` has changed, the `reread_symbols` function kicks in. It tries to reset everything in the `struct objfile` corresponding to `a.out`, except that it leaves the `section_offsets` vector there. 5) `reread_symbols` reads the debug info (calls `read_symbols`). As the DWARF info is read, symbols are created using the old offsets still in `section_offsets`. For example, the `main` symbol is created with the value 0x555555555129. Even though at this point there is no process, so that address is bogus. There's probably more that depends on section_offsets that is not done correctly. 6) Something in the SVR4 solib handling goes wrong, probably because of something that went wrong in (5). I can't quite explain it (if somebody would like to provide a more complete analysis, please go ahead). But this is where it takes a wrong turn: #0 elf_locate_base () at /home/smarchi/src/wt/test/gdb/solib-svr4.c:799 #1 0x000055f0a5bee6d5 in locate_base (info=) at /home/smarchi/src/wt/test/gdb/solib-svr4.c:848 #2 0x000055f0a5bf1771 in svr4_handle_solib_event () at /home/smarchi/src/wt/test/gdb/solib-svr4.c:1955 #3 0x000055f0a5c0ff92 in handle_solib_event () at /home/smarchi/src/wt/test/gdb/solib.c:1258 In the non-working case (without this patch), elf_locate_base returns 0, whereas in the working case (with this patch) it returns a valid address, as we should expect. This patch fixes this by making reread_symbols clear the `section_offsets` vector, and re-create it by calling `sym_offsets`. This is analogous to what syms_from_objfile_1 does. I didn't seem absolutely necessary, but I also made it clear the various `sect_index_*` fields, since their values no longer make sense (they describe the old executable, and are indices in the now cleared sections/section_offsets arrays). I don't really like the approach taken by reread_symbols, trying to reset everything manually on the objfile object, instead of, for example, creating a new one from scratch. But I don't know enough yet to propose a better solution. One more reason I think this patch is needed is that the number of sections of the new executable could be different from the number of sections of the old executable. So if we don't re-create the section_offsets array, not only we'll have wrong offsets, but we could make accesses past the array. Something else that silently fails (but doesn't seem to have consequences) is the prologue analysis when we try to create the breakpoint on `main`. Since the `main` symbol has the wrong value 0x555555555129, we try to access memory in that area, which fails. This can be observed by debugging gdb and using `catch throw`. Before the process is started, we need to access the memory at its unrelocated address, 0x1129, which will read memory from the ELF file. This is now what happens, with this patch applied. It silently fails, probably because commit 46a62268b, "Catch exceptions thrown from gdbarch_skip_prologue", papered over the problem and added an empty catch clause. I'm quite sure that the root cause then was the one fixed by this patch. This fixes tests gdb.ada/exec_changed.exp and gdb.base/reread.exp for me. gdb/ChangeLog: * symfile.c (reread_symbols): Clear objfile's section_offsets vector and section indices, re-compute them by calling sym_offsets. --- gdb/symfile.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/gdb/symfile.c b/gdb/symfile.c index dd8192a67fbb..b02a9235663b 100644 --- a/gdb/symfile.c +++ b/gdb/symfile.c @@ -2543,6 +2543,11 @@ reread_symbols (void) will need to be called (see discussion below). */ obstack_free (&objfile->objfile_obstack, 0); objfile->sections = NULL; + objfile->section_offsets.clear (); + objfile->sect_index_bss = -1; + objfile->sect_index_data = -1; + objfile->sect_index_rodata = -1; + objfile->sect_index_text = -1; objfile->compunit_symtabs = NULL; objfile->template_symbols = NULL; objfile->static_links.reset (nullptr); @@ -2597,6 +2602,9 @@ reread_symbols (void) objfiles_changed (); + /* Recompute section offsets and section indices. */ + objfile->sf->sym_offsets (objfile, {}); + read_symbols (objfile, 0); if (!objfile_has_symbols (objfile)) -- 2.26.2