From patchwork Mon Aug 31 13:22:08 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: develop--- via Libc-alpha X-Patchwork-Id: 40343 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 E9DC1386184A; Mon, 31 Aug 2020 13:22:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E9DC1386184A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1598880140; bh=qkM96OKXghCnwH96nCKGNH5bm14d1p+qCOiPMYUGP7E=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=Zeb1MEgZov5yC4OLLL+8FHb7XuOuYS5tNXHzlg62MZG9ups0LXiM4IgpC1U1pQ+tT IqE9LXTKT74cmCgG9/Y/7hzljvp2OlhytQMNNINS32XwEVHD9Y2eoekzXRuwfrQAVA UFzg2qXAiKAwgW5q5BzvcOhOJcthWNw+ihBMcQtk= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by sourceware.org (Postfix) with ESMTP id 3D31D386180C for ; Mon, 31 Aug 2020 13:22:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 3D31D386180C Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-61-uFW5oupGMf-FBwOEywz4Uw-1; Mon, 31 Aug 2020 09:22:10 -0400 X-MC-Unique: uFW5oupGMf-FBwOEywz4Uw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 9C81C1074665 for ; Mon, 31 Aug 2020 13:22:09 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.40.194.148]) by smtp.corp.redhat.com (Postfix) with ESMTP id DF2D11002D47; Mon, 31 Aug 2020 13:22:08 +0000 (UTC) To: libc-alpha@sourceware.org Subject: [PATCH v3] ld.so: add an --argv0 option Date: Mon, 31 Aug 2020 15:22:08 +0200 Message-Id: <20200831132208.1174687-1-vmihalko@redhat.com> In-Reply-To: References: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0.0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.3 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_H2, SPF_HELO_NONE, SPF_PASS, 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: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: vincent via Libc-alpha From: develop--- via Libc-alpha Reply-To: vmihalko@redhat.com Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" From: Vincent Mihalkovic I hope everything is fine now, thanks for the feedback and help with the patch. I was writing the test case against this documentation: https://sourceware.org/glibc/wiki/Testing/Testsuite and could't find any documentation for libsupport, that's why I didn't use it. Reviewed-by: Adhemerval Zanella --- elf/Makefile | 13 +++++++++++-- elf/argv0test.c | 32 ++++++++++++++++++++++++++++++++ elf/rtld.c | 17 ++++++++++++++++- elf/tst-rtld-argv0.sh | 37 +++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 elf/argv0test.c create mode 100755 elf/tst-rtld-argv0.sh diff --git a/elf/Makefile b/elf/Makefile index 0b787218..f38904d8 100644 --- a/elf/Makefile +++ b/elf/Makefile @@ -210,7 +210,8 @@ tests += restest1 preloadtest loadfail multiload origtest resolvfail \ tst-filterobj tst-filterobj-dlopen tst-auxobj tst-auxobj-dlopen \ tst-audit14 tst-audit15 tst-audit16 \ tst-single_threaded tst-single_threaded-pthread \ - tst-tls-ie tst-tls-ie-dlmopen + tst-tls-ie tst-tls-ie-dlmopen \ + argv0test # reldep9 tests-internal += loadtest unload unload2 circleload1 \ neededtest neededtest2 neededtest3 neededtest4 \ @@ -414,7 +415,7 @@ endif ifeq (yes,$(build-shared)) ifeq ($(run-built-tests),yes) tests-special += $(objpfx)tst-pathopt.out $(objpfx)tst-rtld-load-self.out \ - $(objpfx)tst-rtld-preload.out + $(objpfx)tst-rtld-preload.out $(objpfx)argv0test.out endif tests-special += $(objpfx)check-textrel.out $(objpfx)check-execstack.out \ $(objpfx)check-wx-segment.out \ @@ -1796,3 +1797,11 @@ $(objpfx)tst-tls-ie-dlmopen.out: \ $(objpfx)tst-tls-ie-mod6.so $(objpfx)tst-tls-surplus: $(libdl) + +ARGV0 = test-argv0 +$(objpfx)argv0test.out: tst-rtld-argv0.sh $(objpfx)ld.so \ + $(objpfx)argv0test + $(SHELL) $< $(objpfx)ld.so $(objpfx)argv0test \ + '$(test-wrapper-env)' '$(run_program_env)' \ + '$(rpath-link)' '$(ARGV0)' > $@; \ + $(evaluate-test) diff --git a/elf/argv0test.c b/elf/argv0test.c new file mode 100644 index 00000000..6c6c7e9a --- /dev/null +++ b/elf/argv0test.c @@ -0,0 +1,32 @@ +/* Test for --argv0 option ld.so. + + Copyright (C) 2020 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include + +static int +do_test (int argc, char **argv) +{ + TEST_COMPARE_BLOB (argv[0], strlen (argv[0]), + "test-argv0", strlen ("test-argv0")); +return 0; +} + +#define TEST_FUNCTION_ARGV do_test +#include diff --git a/elf/rtld.c b/elf/rtld.c index 5b882163..e66e033c 100644 --- a/elf/rtld.c +++ b/elf/rtld.c @@ -1202,6 +1202,8 @@ dl_main (const ElfW(Phdr) *phdr, installing it. */ rtld_is_main = true; + char *argv0 = NULL; + /* Note the place where the dynamic linker actually came from. */ GL(dl_rtld_map).l_name = rtld_progname; @@ -1259,6 +1261,14 @@ dl_main (const ElfW(Phdr) *phdr, else if (! strcmp (_dl_argv[1], "--preload") && _dl_argc > 2) { preloadarg = _dl_argv[2]; + _dl_skip_args += 2; + _dl_argc -= 2; + _dl_argv += 2; + } + else if (! strcmp (_dl_argv[1], "--argv0") && _dl_argc > 2) + { + argv0 = _dl_argv[2]; + _dl_skip_args += 2; _dl_argc -= 2; _dl_argv += 2; @@ -1292,7 +1302,8 @@ of this helper program; chances are you did not intend to run this program.\n\ --inhibit-rpath LIST ignore RUNPATH and RPATH information in object names\n\ in LIST\n\ --audit LIST use objects named in LIST as auditors\n\ - --preload LIST preload objects named in LIST\n"); + --preload LIST preload objects named in LIST\n\ + --argv0 STRING set argv[0] to STRING before running\n"); ++_dl_skip_args; --_dl_argc; @@ -1384,6 +1395,10 @@ of this helper program; chances are you did not intend to run this program.\n\ break; } #endif + + /* Set the argv[0] string now that we've processed the executable. */ + if (argv0 != NULL) + _dl_argv[0] = argv0; } else { diff --git a/elf/tst-rtld-argv0.sh b/elf/tst-rtld-argv0.sh new file mode 100755 index 00000000..14d97fb3 --- /dev/null +++ b/elf/tst-rtld-argv0.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Test for --argv0 option ld.so. +# Copyright (C) 2020 Free Software Foundation, Inc. +# This file is part of the GNU C Library. +# +# The GNU C Library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# The GNU C Library 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 +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with the GNU C Library; if not, see +# . + +set -e + +rtld=$1 +test_program=$2 +test_wrapper_env=$3 +run_program_env=$4 +library_path=$5 +argv0=$6 + +echo "# [${test_wrapper_env}] [${run_program_env}] [$rtld] [--library-path]" \ + "[$library_path] [--argv0] [$argv0] [$test_program]" +${test_wrapper_env} \ +${run_program_env} \ +$rtld --library-path "$library_path" \ + --argv0 "$argv0" $test_program 2>&1 && rc=0 || rc=$? +echo "# exit status $rc" + +exit $rc