From patchwork Wed Jul 22 08:33:45 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Mihalkovic X-Patchwork-Id: 40143 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 94F7F398C03D; Wed, 22 Jul 2020 08:34:12 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 94F7F398C03D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1595406852; bh=ZcxirnoajV6GZDcyDdHsih4WFVhAePuO1eMDxoRWlDI=; h=Date:Subject:To:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=agzN9LS/ihc0gNoh279wyLutzRiv2PkHGBMUPyJdotWEcBhgjaSH4xo75/a5J2O3d v9HziuHq573xsqZieKyEh5McMaZx6b3MAJ+1EDrPjpNhXCohcpF0zNa4pvMC2BXB+P ZAZYiydbAOBkFJYGzbUNf3sv9mfcuS2K2Qsz6GP4= X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by sourceware.org (Postfix) with ESMTP id CB9C038930FE for ; Wed, 22 Jul 2020 08:33:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org CB9C038930FE Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-1-oPAILTM0NaKRMahoLVJVPg-1; Wed, 22 Jul 2020 04:33:52 -0400 X-MC-Unique: oPAILTM0NaKRMahoLVJVPg-1 Received: by mail-ot1-f69.google.com with SMTP id r19so660318otq.3 for ; Wed, 22 Jul 2020 01:33:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=twPFDa9aldkcmN6U6iMTA+riCaSPix//Y/4O0nMOJxM=; b=nSDc5UwI6cCTFBEFXkLlrDFMCeFe0gES9YHmm2kAgjLRTsIvdendYfffjxu6xNK23f hRzxRWw0C4r3J/lvnZC+q29jLSFUEzJYE49lbRMWkZW6ruxmmu1antxdZSinPrfk1t9N tZjoJubRrRE76w3GLom/kwpX/aQAzTLGsrLQMODnnqsOJ3rn/narPmYRCtfGbyT63aka C33ypXf2j6HFdX/JDSRKfTDBhCRKB6iA7tTyrfhPKKF/nUx3/gBA1r50oK1CSLYqfFar F7re6khX5rfZuB2iRD1SHGgtOzU56tJ1gHmKZfLluCv5z9qLlCXC/ez47L8OUEId+tTD d3VQ== X-Gm-Message-State: AOAM530nPZ3vHIIUjkeyUZE9FzPvS3tO/AFlvPeiy23/qLjhhHe8tsaA qDy0MHkVuErAwwv6X/+jEmlaWz/Hh/bjnHNUHHxblABd2Lfz8AzoDHCYot5ML+V4FiNopvowuEx 7XXls/NU6oIKMi05naCrmp+7MybdoCKifiEPX X-Received: by 2002:a05:6830:18f6:: with SMTP id d22mr27705453otf.243.1595406831345; Wed, 22 Jul 2020 01:33:51 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwHC3Y0Ez4cMfWZqcMxPWl1x5+7dT7KASH/F+0WHAICXvFrIIcE6koq9BzznS/pnWn2fEGq60Sn5ceAsfqevkI= X-Received: by 2002:a05:6830:18f6:: with SMTP id d22mr27705443otf.243.1595406831116; Wed, 22 Jul 2020 01:33:51 -0700 (PDT) MIME-Version: 1.0 Date: Wed, 22 Jul 2020 10:33:45 +0200 Message-ID: Subject: [PATCH] ld.so: add an --argv0 option To: libc-alpha@sourceware.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, HTML_MESSAGE, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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-Content-Filtered-By: Mailman/MimeDel 2.1.29 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 Mihalkovic via Libc-alpha From: Vincent Mihalkovic Reply-To: Vincent Mihalkovic Errors-To: libc-alpha-bounces@sourceware.org Sender: "Libc-alpha" Hi, a few years ago there was an effort for adding --argv0 option: https://sourceware.org/legacy-ml/libc-alpha/2016-04/msg00576.html. I made the old patch actual for version 2.31.9000, added a test case and changelog. 2020-07-22 Vincent Mihalkovic * elf/Makefile: added argv0 test case, Modified. * elf/rtld.c: added --argv0 option, Modified. * elf/argv0test.c: test case, New file. * elf/tst-rtld-argv0.sh: test case, New file. diff --git a/elf/Makefile b/elf/Makefile index 0b78721848..f38904d831 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 0000000000..4c79bebf23 --- /dev/null +++ b/elf/argv0test.c @@ -0,0 +1,13 @@ +#include // for printf +#include // for strcmp + + +int main( int argc, char *argv[] ) { + int result = strcmp( argv[0], "test-argv0"); + + printf ("argv[0] = %s, strcmp( argv[0], \"test-argv0\" ) = %d, %s\n", \ + argv[0], result, !result ? "ok" : "wrong"); + + return result; + +} diff --git a/elf/rtld.c b/elf/rtld.c index 5b882163fa..cafa4f9bd3 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,11 @@ 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 0000000000..5f873b6c5c --- /dev/null +++ b/elf/tst-rtld-argv0.sh @@ -0,0 +1,37 @@ +#!/bin/sh +# Test --argv0 argument ld.so. +# Copyright (C) 2019-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