commit 80c570537e380c1b8e48754c0ddbce2abcde2d00 Author: Jan Kratochvil Date: Thu Feb 26 14:08:01 2015 +0100 SEGV in ppc64_elf_get_synthetic_symtab reading a separate debug file The attached patch fixes the SEGV and lets GDB successfully load all kernel modules installed by default on RHEL 7. Valgrind on F-21 x86_64 host has shown me more clear what is the problem: Reading symbols from /home/jkratoch/t/cordic.ko...Reading symbols from /home/jkratoch/t/cordic.ko.debug...================================================================= ==22763==ERROR: AddressSanitizer: heap-use-after-free on address 0x6120000461c8 at pc 0x150cdbd bp 0x7fffffffc7e0 sp 0x7fffffffc7d0 READ of size 8 at 0x6120000461c8 thread T0 #0 0x150cdbc in ppc64_elf_get_synthetic_symtab /home/jkratoch/redhat/gdb-test-asan/bfd/elf64-ppc.c:3282 #1 0x8c5274 in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1205 #2 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268 [...] 0x6120000461c8 is located 264 bytes inside of 288-byte region [0x6120000460c0,0x6120000461e0) freed by thread T0 here: #0 0x7ffff715454f in __interceptor_free (/lib64/libasan.so.1+0x5754f) #1 0xde9cde in xfree common/common-utils.c:98 #2 0x9a04f7 in do_my_cleanups common/cleanups.c:155 #3 0x9a05d3 in do_cleanups common/cleanups.c:177 #4 0x8c538a in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1229 #5 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268 [...] previously allocated by thread T0 here: #0 0x7ffff71547c7 in malloc (/lib64/libasan.so.1+0x577c7) #1 0xde9b95 in xmalloc common/common-utils.c:41 #2 0x8c4da2 in elf_read_minimal_symbols /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1147 #3 0x8c55e7 in elf_symfile_read /home/jkratoch/redhat/gdb-test-asan/gdb/elfread.c:1268 [...] SUMMARY: AddressSanitizer: heap-use-after-free /home/jkratoch/redhat/gdb-test-asan/bfd/elf64-ppc.c:3282 ppc64_elf_get_synthetic_symtab [...] ==22763==ABORTING A similar case a few lines later I have fixed in 2010 by: https://sourceware.org/git/?p=binutils-gdb.git;a=commit;h=3f1eff0a2c7f0e7078f011f55b8e7f710aae0cc2 My testcase does not always reproduce it but at least a bit: * GDB without ppc64 target (even as a secondary one) is reported as "untested" * ASAN-built GDB with ppc64 target always crashes (and PASSes with this fix) * unpatched non-ASAN-built GDB with ppc64 target crashes from commandline * unpatched non-ASAN-built GDB with ppc64 target PASSes from runtest (?) gdb/ChangeLog 2015-02-26 Jan Kratochvil * elfread.c (elf_read_minimal_symbols): Use bfd_alloc for bfd_canonicalize_symtab. gdb/testsuite/ChangeLog 2015-02-26 Jan Kratochvil * gdb.arch/cordic.ko.bz2: New file. * gdb.arch/cordic.ko.debug.bz2: New file. * gdb.arch/ppc64-symtab-cordic.exp: New file. Index: gdb-7.6.1/gdb/elfread.c =================================================================== --- gdb-7.6.1.orig/gdb/elfread.c +++ gdb-7.6.1/gdb/elfread.c @@ -2308,8 +2308,10 @@ elf_symfile_read (struct objfile *objfil if (storage_needed > 0) { - symbol_table = (asymbol **) xmalloc (storage_needed); - make_cleanup (xfree, symbol_table); + /* Memory gets permanently referenced from ABFD after + bfd_canonicalize_symtab so it must not get freed before ABFD gets. */ + + symbol_table = bfd_alloc (abfd, storage_needed); symcount = bfd_canonicalize_symtab (objfile->obfd, symbol_table); if (symcount < 0) Index: gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-symtab-cordic.exp =================================================================== --- /dev/null +++ gdb-7.6.1/gdb/testsuite/gdb.arch/ppc64-symtab-cordic.exp @@ -0,0 +1,51 @@ +# Copyright 2015 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 + +set kobz2uufile ${srcdir}/${subdir}/cordic.ko.bz2.uu +set kofile ${objdir}/${subdir}/cordic.ko +set kodebugbz2uufile ${srcdir}/${subdir}/cordic.ko.debug.bz2.uu +set kodebugfile ${objdir}/${subdir}/cordic.ko.debug + +if {[catch "system \"uudecode -o - ${kobz2uufile} | bzip2 -dc >${kofile}\""] != 0} { + untested "failed bzip2 for ${kobz2uufile}" + return -1 +} +if {[catch "system \"uudecode -o - ${kodebugbz2uufile} | bzip2 -dc >${kodebugfile}\""] != 0} { + untested "failed bzip2 for ${kodebugbz2uufile}" + return -1 +} + +gdb_exit +gdb_start +gdb_reinitialize_dir $srcdir/$subdir + +# This test won't work properly if system debuginfo is installed. +# Test message is suppressed by "" as otherwise we could print PASS+UNTESTED +# result to gdb.sum making a false feeling the issue has been tested. +gdb_test_no_output "set debug-file-directory" "" + +gdb_load ${kofile} + +set test "show architecture" +gdb_test_multiple $test $test { + -re "\r\nThe target architecture is set automatically \\(currently powerpc:common64\\)\r\n$gdb_prompt $" { + pass $test + } + -re "\r\nThe target architecture is set automatically \\(currently .*\\)\r\n$gdb_prompt $" { + untested "powerpc:common64 is not supported" + } +} Index: gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.bz2.uu =================================================================== --- /dev/null +++ gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.bz2.uu @@ -0,0 +1,53 @@ +begin 664 cordic.ko.bz2.uu +M0EIH.3%!62936:QR$J<`!:7____________^___________]__O_U__]_N]] +M__?W__Z[T`88O`VP73FU<IZADTR`/4]0#(T,U&@]0#0`9`,FC33&IM(S4!M30`:``:-&@`TT0`````& +M@```!H#(`````````-``-&@`````````:&@-```R:`2)(2GHIYI,F%-E/4T& +M@&U&:FU#Q3TGJ-#0T:'J&AH!IM1H&1H/2,@`#U,AH&@`:`#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)W&A)$+"D("%PAH1,B+EFDI +M!,)F/LQ1>WRLFH;:IH<2$*#V)$9#%O.HL&6)@RP)U[0)RL9S4(0;B)T#H5"X +M&Z`6P3@'37WRBX^=+*>0?3)D!K;W>WTTBC,"$.MX6MVUWV)^U1P?[LI/6A$8 +M#^333)CO]9Z/55B?/R5TR?-./&C+(&A[):L7T-MT:"JPH.M@Z)$TTB$NC^Z1 +M$FHE%T(C1-:Y93\]R_U&6A-O.3_)/0K&;LK]9Z%>?Q>4_L&TM>J:2S3BFC/> +MN^\3B,# +MA6QOQMY*]TJV12V[4+J+-P=Q?/MV=Q2Y%@*"\<12OVLPHS(E+340BRPFZI/$ +M,;[5&:J8,\D"&@9CK"#&OP@<"A!3ND%9S*EA'6D3JLC"T6<9:`!E;3O(&G94 +MIT-`CL3%A?;_(NKY1#S-PYZ#Q/58AEZZY4LG'0B(3B$`4$0P0_XNY(IPH2%8 +#Y"5. +` +end Index: gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.debug.bz2.uu =================================================================== --- /dev/null +++ gdb-7.6.1/gdb/testsuite/gdb.arch/cordic.ko.debug.bz2.uu @@ -0,0 +1,24 @@ +begin 664 cordic.ko.debug.bz2.uu +M0EIH.3%!62936;;3U;4``^=______^==:W]63^U^1+_O_V"H4V#`)$A@F@90 +M"D$!"""2P`,!Q0-*1AHB-4])ZGI-#3U`T:8C0``-``-`:-#0`:#T@!IH-&@- +M!HA$\D](])E)Z(V@)A#30TT,1@`C-`)@(!H8"8!,--!!@``````````$P``` +M`$8``F``2)$FF@FDVIZGJ>BFF1A-/-)HVJ'J8`#U3,H:#"-D@'H:@8CT&("] +M???6F=%1'FNGCPP"I4HL6ZE@2>=E(>L$@&5?>9ZJAI%`&%I2P2MAW226F58= +M!Z[0:D$@C,S$%$*-\ZZF[U##1Q9M=6V$MAXS#$,A5@8IA,-^8#:S#*JAEJUA +M.ZJWN+EH.Z>`5$02]B/^B\^*GMP%%$,#,BH!`%2&""YD(Z$``3/9]@Y;'/LR +M6_C%]&YT&1HR5[H(&VQ`9U[/&/_IX]&CA3,4BW\%WK:>,KF?AA?;I0M)HJHA +M))0E$!$-PFTDHB!1$`@;;*T0".1!";2&QM!L,.,TDA*LQ:G*A?T5HS.EQQ-^ +MXX9F.PV"$H]V,TTRF$T%@NHPIQ2[]T6@8*!#(2$CYW1@8Y(S,WQ[.UM[GKY. +M"Y^V=].AINN_ +MT#7-^3M?A9*J-8-<-'1M[-.*2H82JW<$8`0=$2U,`K^AD\-A$N6%@!,)D\40 +MG+A5S!8)K\D>T%$6+5VX1Q3?#S@85ELW\N1-;@7B98=FRJ21(E>-R[9:8#GQ6&((U`"<&M?(D**&1(`:P +MAXD/K49))+)4^H3%*:%H4QQ2+J4.>B,&7.N<`R,SB0A!LB3&(`J?03!*`8:! +M$Z0H8&G/30D@`M$O`Z5)%-CU'QCR@Z6&_RJIJ@M5&8H8&$O*6ESP4],\P7^+ +*N2*<*$A;:>K:@``` +` +end