From bf223d3e808e6fec9ee165d3d48beb74837796de Mon Sep 17 00:00:00 2001 From: Pedro Alves Date: Mon, 21 Aug 2017 11:34:32 +0100 Subject: [PATCH] Handle function aliases better (PR gdb/19487, errno printing) (Ref: https://sourceware.org/ml/gdb/2017-06/msg00048.html) This patch improves GDB support for function aliases defined with __attribute__ alias. For example, in the test added by this commit, there is no reference to "func_alias" in the debug info at all, only to "func"'s definition: $ nm ./testsuite/outputs/gdb.base/symbol-alias/symbol-alias | grep " func" 00000000004005ae t func 00000000004005ae T func_alias $ readelf -w ./testsuite/outputs/gdb.base/symbol-alias/symbol-alias | grep func -B 1 -A 8 <1>: Abbrev Number: 5 (DW_TAG_subprogram) DW_AT_name : (indirect string, offset: 0x111): func DW_AT_decl_file : 1 DW_AT_decl_line : 27 DW_AT_prototyped : 1 DW_AT_type : <0xf8> DW_AT_low_pc : 0x4005ae DW_AT_high_pc : 0xb DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa) DW_AT_GNU_all_call_sites: 1 So all GDB knows about "func_alias" is from the minsym (elf symbol): (gdb) p func_alias $1 = {} 0x4005ae (gdb) ptype func_alias type = int () (gdb) p func $2 = {struct S *(void)} 0x4005ae (gdb) ptype func type = struct S { int field1; int field2; } *(void) The result is that calling func_alias from the command line produces incorrect results. This is similar (though not exactly the same) to the glibc errno/__errno_location/__GI___errno_location situation. On glibc, errno is defined like this: extern int *__errno_location (void); #define errno (*__errno_location ()) with __GI___errno_location being an internal alias for __errno_location. On my system's libc (F23), I do see debug info for __errno_location, in the form of name vs linkage name: <1><95a5>: Abbrev Number: 18 (DW_TAG_subprogram) <95a6> DW_AT_external : 1 <95a6> DW_AT_name : (indirect string, offset: 0x2c26): __errno_location <95aa> DW_AT_decl_file : 1 <95ab> DW_AT_decl_line : 24 <95ac> DW_AT_linkage_name: (indirect string, offset: 0x2c21): __GI___errno_location <95b0> DW_AT_prototyped : 1 <95b0> DW_AT_type : <0x9206> <95b4> DW_AT_low_pc : 0x20f40 <95bc> DW_AT_high_pc : 0x11 <95c4> DW_AT_frame_base : 1 byte block: 9c (DW_OP_call_frame_cfa) <95c6> DW_AT_GNU_all_call_sites: 1 however that doesn't matter in practice, because GDB doesn't record demangled names anyway, and so we end up with the exact same situation covered by the testcase. So the fix is to make the expression parser find a debug symbol for the same address as the just-found minsym, when a lookup by name didn't find a debug symbol by name. We now get: (gdb) p func_alias $1 = {struct S *(void)} 0x4005ae (gdb) p __errno_location $2 = {int *(void)} 0x7ffff6e92830 <__errno_location> I've made the test exercise variable aliases too, for completeness. Those already work correctly, because unlike for function aliases, GCC emits debug information for variable aliases. Tested on GNU/Linux. gdb/ChangeLog: 2017-08-21 Pedro Alves PR gdb/19487 * c-exp.y (variable production): Handle function aliases. * minsyms.c (msymbol_is_text): New function. * minsyms.h (msymbol_is_text): Declare. * symtab.c (find_function_alias_target): New function. * symtab.h (find_function_alias_target): Declare. gdb/testsuite/ChangeLog: 2017-08-21 Pedro Alves PR gdb/19487 * gdb.base/symbol-alias.c: New. * gdb.base/symbol-alias2.c: New. * gdb.base/symbol-alias.exp: New. --- gdb/ChangeLog | 9 ++++++ gdb/c-exp.y | 38 +++++++++++++++++++------ gdb/minsyms.c | 17 +++++++++++ gdb/minsyms.h | 4 +++ gdb/symtab.c | 19 +++++++++++++ gdb/symtab.h | 7 +++++ gdb/testsuite/ChangeLog | 7 +++++ gdb/testsuite/gdb.base/symbol-alias.c | 31 ++++++++++++++++++++ gdb/testsuite/gdb.base/symbol-alias.exp | 37 ++++++++++++++++++++++++ gdb/testsuite/gdb.base/symbol-alias2.c | 34 ++++++++++++++++++++++ 10 files changed, 194 insertions(+), 9 deletions(-) create mode 100644 gdb/testsuite/gdb.base/symbol-alias.c create mode 100644 gdb/testsuite/gdb.base/symbol-alias.exp create mode 100644 gdb/testsuite/gdb.base/symbol-alias2.c diff --git a/gdb/ChangeLog b/gdb/ChangeLog index fae715041cb..db53fcb6452 100644 --- a/gdb/ChangeLog +++ b/gdb/ChangeLog @@ -1,3 +1,12 @@ +2017-08-21 Pedro Alves + + PR gdb/19487 + * c-exp.y (variable production): Handle function aliases. + * minsyms.c (msymbol_is_text): New function. + * minsyms.h (msymbol_is_text): Declare. + * symtab.c (find_function_alias_target): New function. + * symtab.h (find_function_alias_target): Declare. + 2017-08-21 Pedro Alves * eval.c (evaluate_subexp_standard) : Don't dig past diff --git a/gdb/c-exp.y b/gdb/c-exp.y index 24a2fbd76e6..a1f9feee717 100644 --- a/gdb/c-exp.y +++ b/gdb/c-exp.y @@ -1038,18 +1038,38 @@ variable: name_not_typename } else { - struct bound_minimal_symbol msymbol; char *arg = copy_name ($1.stoken); - msymbol = - lookup_bound_minimal_symbol (arg); - if (msymbol.minsym != NULL) - write_exp_msymbol (pstate, msymbol); - else if (!have_full_symbols () && !have_partial_symbols ()) - error (_("No symbol table is loaded. Use the \"file\" command.")); + bound_minimal_symbol msymbol + = lookup_bound_minimal_symbol (arg); + if (msymbol.minsym == NULL) + { + if (!have_full_symbols () && !have_partial_symbols ()) + error (_("No symbol table is loaded. Use the \"file\" command.")); + else + error (_("No symbol \"%s\" in current context."), + copy_name ($1.stoken)); + } + + /* This minsym might be an alias for + another function. See if we can find + the debug symbol for the target, and + if so, use it instead, since it has + return type / prototype info. This + is important for example for "p + *__errno_location()". */ + symbol *alias_target + = find_function_alias_target (msymbol); + if (alias_target != NULL) + { + write_exp_elt_opcode (pstate, OP_VAR_VALUE); + write_exp_elt_block + (pstate, SYMBOL_BLOCK_VALUE (alias_target)); + write_exp_elt_sym (pstate, alias_target); + write_exp_elt_opcode (pstate, OP_VAR_VALUE); + } else - error (_("No symbol \"%s\" in current context."), - copy_name ($1.stoken)); + write_exp_msymbol (pstate, msymbol); } } ; diff --git a/gdb/minsyms.c b/gdb/minsyms.c index c93eaa34ddd..69b9e403eaa 100644 --- a/gdb/minsyms.c +++ b/gdb/minsyms.c @@ -52,6 +52,23 @@ #include "cli/cli-utils.h" #include "symbol.h" +/* See minsyms.h. */ + +bool +msymbol_is_text (minimal_symbol *msymbol) +{ + switch (MSYMBOL_TYPE (msymbol)) + { + case mst_text: + case mst_text_gnu_ifunc: + case mst_solib_trampoline: + case mst_file_text: + return true; + default: + return false; + } +} + /* Accumulate the minimal symbols for each objfile in bunches of BUNCH_SIZE. At the end, copy them all into one newly allocated location on an objfile's per-BFD storage obstack. */ diff --git a/gdb/minsyms.h b/gdb/minsyms.h index b82a22af71b..e763f62aa36 100644 --- a/gdb/minsyms.h +++ b/gdb/minsyms.h @@ -157,6 +157,10 @@ void terminate_minimal_symbol_table (struct objfile *objfile); +/* Return whether MSYMBOL is a function/method. */ + +bool msymbol_is_text (minimal_symbol *msymbol); + /* Compute a hash code for the string argument. */ unsigned int msymbol_hash (const char *); diff --git a/gdb/symtab.c b/gdb/symtab.c index ccf31cc7c39..2c19b5339e5 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -3830,6 +3830,25 @@ skip_prologue_using_sal (struct gdbarch *gdbarch, CORE_ADDR func_addr) /* Don't return END_PC, which is past the end of the function. */ return prologue_sal.pc; } + +/* See symtab.h. */ + +symbol * +find_function_alias_target (bound_minimal_symbol msymbol) +{ + if (!msymbol_is_text (msymbol.minsym)) + return NULL; + + CORE_ADDR addr = BMSYMBOL_VALUE_ADDRESS (msymbol); + symbol *sym = find_pc_function (addr); + if (sym != NULL + && SYMBOL_CLASS (sym) == LOC_BLOCK + && BLOCK_START (SYMBOL_BLOCK_VALUE (sym)) == addr) + return sym; + + return NULL; +} + /* If P is of the form "operator[ \t]+..." where `...' is some legitimate operator text, return a pointer to the diff --git a/gdb/symtab.h b/gdb/symtab.h index 35949f08674..8cbf4d6d942 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -1556,6 +1556,13 @@ extern CORE_ADDR skip_prologue_using_sal (struct gdbarch *gdbarch, extern struct symbol *fixup_symbol_section (struct symbol *, struct objfile *); +/* If MSYMBOL is an text symbol, look for a function debug symbol with + the same address. Returns NULL if not found. This is necessary in + case a function is an alias to some other function, because debug + information is only emitted for the alias target function's + definition, not for the alias. */ +extern symbol *find_function_alias_target (bound_minimal_symbol msymbol); + /* Symbol searching */ /* Note: struct symbol_search, search_symbols, et.al. are declared here, instead of making them local to symtab.c, for gdbtk's sake. */ diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog index 03e825e0462..f1477313e6a 100644 --- a/gdb/testsuite/ChangeLog +++ b/gdb/testsuite/ChangeLog @@ -1,3 +1,10 @@ +2017-08-21 Pedro Alves + + PR gdb/19487 + * gdb.base/symbol-alias.c: New. + * gdb.base/symbol-alias2.c: New. + * gdb.base/symbol-alias.exp: New. + 2017-08-21 Pedro Alves * gdb.base/dfp-test.c diff --git a/gdb/testsuite/gdb.base/symbol-alias.c b/gdb/testsuite/gdb.base/symbol-alias.c new file mode 100644 index 00000000000..cc5d104dddb --- /dev/null +++ b/gdb/testsuite/gdb.base/symbol-alias.c @@ -0,0 +1,31 @@ +/* This test is part of GDB, the GNU debugger. + + Copyright 2017 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 . */ + +struct S +{ + int field1; + int field2; +}; + +extern struct S *func_alias (void); + +int +main (void) +{ + struct S *s = func_alias (); + return s->field1 - s->field1; +} diff --git a/gdb/testsuite/gdb.base/symbol-alias.exp b/gdb/testsuite/gdb.base/symbol-alias.exp new file mode 100644 index 00000000000..dc63eb1cd25 --- /dev/null +++ b/gdb/testsuite/gdb.base/symbol-alias.exp @@ -0,0 +1,37 @@ +# Test for printing alias symbols. +# Copyright 2017 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 . + +standard_testfile symbol-alias.c symbol-alias2.c + +if { [prepare_for_testing "failed to prepare" ${testfile} [list $srcfile $srcfile2]] } { + return -1 +} + +if ![runto_main] then { + fail "can't run to main" + continue +} + +# Functions. +foreach f {"func" "func_alias"} { + gdb_test "p $f" " = {struct S \\*\\(void\\)} $hex " + gdb_test "p *${f}()" "= {field1 = 1, field2 = 2}" +} + +# Variables. +foreach v {"g_var_s" "g_var_s_alias"} { + gdb_test "p $v" "= {field1 = 1, field2 = 2}" +} diff --git a/gdb/testsuite/gdb.base/symbol-alias2.c b/gdb/testsuite/gdb.base/symbol-alias2.c new file mode 100644 index 00000000000..aedd520b6b1 --- /dev/null +++ b/gdb/testsuite/gdb.base/symbol-alias2.c @@ -0,0 +1,34 @@ +/* This test is part of GDB, the GNU debugger. + + Copyright 2017 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 . */ + +struct S +{ + int field1; + int field2; +}; + +struct S g_var_s = { 1, 2 }; + +static struct S * +func (void) +{ + return &g_var_s; +} + +struct S *func_alias (void) __attribute__ ((alias ("func"))); + +extern struct S g_var_s_alias __attribute__ ((alias ("g_var_s"))); -- 2.30.2