From patchwork Fri May 8 17:09:39 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Sandra Loosemore X-Patchwork-Id: 6636 Received: (qmail 48336 invoked by alias); 8 May 2015 17:10:50 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 48324 invoked by uid 89); 8 May 2015 17:10:49 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS autolearn=ham version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 08 May 2015 17:10:48 +0000 Received: from svr-orw-fem-03.mgc.mentorg.com ([147.34.97.39]) by relay1.mentorg.com with esmtp id 1YqlnR-0003yx-Hf from Sandra_Loosemore@mentor.com ; Fri, 08 May 2015 10:10:45 -0700 Received: from [IPv6:::1] (147.34.91.1) by svr-orw-fem-03.mgc.mentorg.com (147.34.97.39) with Microsoft SMTP Server id 14.3.224.2; Fri, 8 May 2015 10:10:45 -0700 Message-ID: <554CEDD3.80407@codesourcery.com> Date: Fri, 8 May 2015 11:09:39 -0600 From: Sandra Loosemore User-Agent: Mozilla/5.0 (X11; Linux i686; rv:17.0) Gecko/20130329 Thunderbird/17.0.5 MIME-Version: 1.0 To: CC: Yao Qi Subject: [patch, dwarf2] avoid segfault on missing directory table A while back we were given a nios2-elf executable that caused GDB to segfault while reading its debug information. The binary turned out to have invalid DWARF-2 information in the .debug_line section: although the file name table had references to entries in the directory table, the directory table itself was empty. The executable was produced by some very old version of GCC (4.1.2?), and we verified that more current toolchain versions don't produce such bad debug information any more. But, since it's generally a bad thing for GDB to segfault, here is a patch that makes the DWARF-2 reader more robust by making sure the directory table is non-NULL before trying to access entries in it. OK to commit? -Sandra diff --git a/gdb/dwarf2read.c b/gdb/dwarf2read.c index 4982922..e2ea7e2 100644 --- a/gdb/dwarf2read.c +++ b/gdb/dwarf2read.c @@ -9320,7 +9320,7 @@ setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu) const char *dir = NULL; struct file_entry *fe = &lh->file_names[i]; - if (fe->dir_index) + if (fe->dir_index && lh->include_dirs != NULL) dir = lh->include_dirs[fe->dir_index - 1]; dwarf2_start_subfile (fe->name, dir); @@ -17396,7 +17396,7 @@ psymtab_include_file_name (const struct line_header *lh, int file_index, char *copied_name = NULL; int file_is_pst; - if (fe.dir_index) + if (fe.dir_index && lh->include_dirs != NULL) dir_name = lh->include_dirs[fe.dir_index - 1]; if (!IS_ABSOLUTE_PATH (include_name) @@ -17595,7 +17595,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu, struct file_entry *fe = &lh->file_names[file - 1]; const char *dir = NULL; - if (fe->dir_index) + if (fe->dir_index && lh->include_dirs != NULL) dir = lh->include_dirs[fe->dir_index - 1]; dwarf2_start_subfile (fe->name, dir); @@ -17815,7 +17815,7 @@ dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu, else { fe = &lh->file_names[file - 1]; - if (fe->dir_index) + if (fe->dir_index && lh->include_dirs != NULL) dir = lh->include_dirs[fe->dir_index - 1]; if (!decode_for_pst_p) { @@ -17958,7 +17958,7 @@ dwarf_decode_lines (struct line_header *lh, const char *comp_dir, struct file_entry *fe; fe = &lh->file_names[i]; - if (fe->dir_index) + if (fe->dir_index && lh->include_dirs != NULL) dir = lh->include_dirs[fe->dir_index - 1]; dwarf2_start_subfile (fe->name, dir); @@ -20640,7 +20640,8 @@ file_file_name (int file, struct line_header *lh) { struct file_entry *fe = &lh->file_names[file - 1]; - if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0) + if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0 + || lh->include_dirs == NULL) return xstrdup (fe->name); return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING, fe->name, NULL);