From patchwork Tue Sep 26 00:07:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kevin Buettner X-Patchwork-Id: 76664 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id CE6303857713 for ; Tue, 26 Sep 2023 00:10:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CE6303857713 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1695687004; bh=TWwRY6/mAr3Q77ApXXj+shapOD4uwV3Ey2Vaj1jdo90=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=VFuh743fytY7QIJ7KOVdDnQ7SARQeuUhOoU9JOxZ3+T7J5UiDF27xB70rGrgNtr7C hFPgWIoMt1uuSu/KSRA9L+a5FVlgM7O6qIxSW31uwgdBYfvbH8rnT/utxzD555rvWF 2Z/8cRcB+w6np5iTnRyZQcNcPCZlb1NcO8J4AVxA= X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id EC0683858D3C for ; Tue, 26 Sep 2023 00:09:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC0683858D3C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-547-FZ9pdmNXMV2gjzFlejSHOg-1; Mon, 25 Sep 2023 20:09:34 -0400 X-MC-Unique: FZ9pdmNXMV2gjzFlejSHOg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 80B6785A5BA; Tue, 26 Sep 2023 00:09:34 +0000 (UTC) Received: from f38-1.lan (unknown [10.22.32.95]) by smtp.corp.redhat.com (Postfix) with ESMTP id A980440C2064; Tue, 26 Sep 2023 00:09:33 +0000 (UTC) To: gdb-patches@sourceware.org Subject: [PATCH v2] gdb/testsuite: Add relative versus absolute LD_LIBRARY_PATH test Date: Mon, 25 Sep 2023 17:07:26 -0700 Message-ID: <20230926000918.3270325-1-kevinb@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Kevin Buettner via Gdb-patches From: Kevin Buettner Reply-To: Kevin Buettner Cc: Lancelot SIX , Jan Kratochvil Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Sender: "Gdb-patches" At one time, circa 2006, there was a bug, which was presumably fixed without adding a test case: If you provided some relative path to the shared library, such as with export LD_LIBRARY_PATH=. then gdb would fail to match the shared library name during the TLS lookup. I think there may have been a bit more to it than is provided by that explanation, since the test also takes care to split the debug info into a separate file. In any case, this commit is based on one of Red Hat's really old local patches. I've attempted to update it and remove a fair amount of cruft, hopefully without losing any critical elements from the test. Testing on Fedora 38 (correctly) shows 1 unsupported test for native-gdbserver and 5 PASSes for the native target as well as native-extended-gdbserver. In his review of v1 of this patch, Lancelot SIX observed that 'thread_local' could be used in place of '__thread' in the C source files. But it only became available via the standard in C11, so I used additional_flags=-std=c11 for compiling both the shared object and the main program. Also, while testing with CC_FOR_TARGET=clang, I found that 'additional_flags=-Wl,-soname=${binsharedbase}' caused clang to complain that this linker flag was unused when compiling the source file, so I moved this linker option to 'ldflags='. My testing for this v2 patch shows the same results as with v1, but I've done additional testing with CC_FOR_TARGET=clang as well. The results are the same as when gcc is used. Co-Authored-by: Jan Kratochvil Reviewed-by: Lancelot SIX Reviewed-By: Lancelot Six --- gdb/testsuite/gdb.threads/tls-sepdebug-main.c | 29 ++++++ .../gdb.threads/tls-sepdebug-shared.c | 21 ++++ gdb/testsuite/gdb.threads/tls-sepdebug.exp | 95 +++++++++++++++++++ 3 files changed, 145 insertions(+) create mode 100644 gdb/testsuite/gdb.threads/tls-sepdebug-main.c create mode 100644 gdb/testsuite/gdb.threads/tls-sepdebug-shared.c create mode 100644 gdb/testsuite/gdb.threads/tls-sepdebug.exp diff --git a/gdb/testsuite/gdb.threads/tls-sepdebug-main.c b/gdb/testsuite/gdb.threads/tls-sepdebug-main.c new file mode 100644 index 00000000000..0c0903a0453 --- /dev/null +++ b/gdb/testsuite/gdb.threads/tls-sepdebug-main.c @@ -0,0 +1,29 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2006-2023 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 . */ + +#include +#include + +/* Note: thread_local requires C11 or later. */ +extern thread_local int var; + +int main() +{ + /* Ensure we link against pthreads even with --as-needed. */ + pthread_testcancel(); + return var; +} diff --git a/gdb/testsuite/gdb.threads/tls-sepdebug-shared.c b/gdb/testsuite/gdb.threads/tls-sepdebug-shared.c new file mode 100644 index 00000000000..c051eb1041d --- /dev/null +++ b/gdb/testsuite/gdb.threads/tls-sepdebug-shared.c @@ -0,0 +1,21 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2006-2023 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 . */ + +#include + +/* Note: thread_local requires C11 or later. */ +thread_local int var = 42; diff --git a/gdb/testsuite/gdb.threads/tls-sepdebug.exp b/gdb/testsuite/gdb.threads/tls-sepdebug.exp new file mode 100644 index 00000000000..1be1674fab1 --- /dev/null +++ b/gdb/testsuite/gdb.threads/tls-sepdebug.exp @@ -0,0 +1,95 @@ +# Copyright 2006-2023 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 creates a shared library and a main program which uses +# that library, extracts the debug info of the library to a separate +# file, and then tests that a symbol from the shared library is +# accessible using both absolute and relative settings of +# LD_LIBRARY_PATH. + +# This test needs to change the environment in which the test program +# is run, specifically the setting of LD_LIBRARY_PATH. GDB can adjust +# this setting for both native and extended-remote targets, but not +# for targets to which GDB connects after the program has already +# started. Therefore, this test won't work for targets which use +# "target remote". +require !use_gdb_stub +require {have_compile_flag -std=c11} + +set testfile tls-sepdebug +set srcmainfile ${testfile}-main.c +set srcsharedfile ${testfile}-shared.c + +set binmainfile [standard_output_file ${testfile}-main] +set binsharedbase ${testfile}-shared.so +set binsharedfile [standard_output_file ${binsharedbase}] + +# Build the shared library, but use explicit -soname; otherwise the +# full path to the library would get encoded into ${binmainfile} +# making LD_LIBRARY_PATH tests useless. +# +# The compile flag -std=c11 is required because the test case uses +# 'thread_local' to indicate thread local storage. This is available +# as a macro starting in C11 and became a C-language keyword in C23. +if { [gdb_compile_shlib \ + "${srcdir}/${subdir}/${srcsharedfile}" "${binsharedfile}" \ + [list debug \ + ldflags=-Wl,-soname=${binsharedbase} \ + additional_flags=-std=c11]] \ + != "" } { + untested "Couldn't compile test library" + return -1 +} + +# Strip debug information from $binsharedfile, placing it in +# ${binsharedfile}.debug. Also add a .gnu_debuglink in the former, +# pointing to the latter. +gdb_gnu_strip_debug ${binsharedfile} + +# Build main program, but do not use `shlib=' since that would +# automatically add -rpath for gcc. +if { [gdb_compile_pthreads \ + "${srcdir}/${subdir}/${srcmainfile} ${binsharedfile}" \ + "${binmainfile}" executable [list debug additional_flags=-std=c11]] \ + != "" } { + untested "Couldn't compile test program" + return -1 +} + +set absdir [file dirname [standard_output_file ${binsharedbase}]] + +foreach ld_library_path [list $absdir [relative_filename [pwd] $absdir]] \ + name { absolute relative } { + with_test_prefix $name { + + # Restart, but defer loading until after setting LD_LIBRARY_PATH. + clean_restart + + gdb_test_no_output "set env LD_LIBRARY_PATH=$ld_library_path" \ + "set env LD_LIBRARY_PATH" + + gdb_load ${binmainfile} + + if ![runto_main] { + return + } + + # Print a thread local variable from the shared library to be certain + # that its symbols were loaded from the separate debuginfo file. + gdb_test "print var" \ + "\\\$1 = \[0-9\].*" \ + "print TLS variable from a shared library with separate debug info file" + } +}