From patchwork Mon Sep 7 15:23:35 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Iain Sandoe X-Patchwork-Id: 515206 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 5AC3714010F for ; Tue, 8 Sep 2015 01:24:03 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=m31ln6sV; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :content-type:subject:date:message-id:cc:to:mime-version; q=dns; s=default; b=Qs4pwOANJzzgaSQUtxBzp+PqY/+sE03D5jlo26L5B2rBsrEdgY 9HFNLE/87RtRG/UDgGscCpaOfbqe1I3XlqMYps2PBwDFkPXZ294DmRMpnOOhxz2M 0mCO+j+9w9r78EpiCRJeH9huUKR162ilzujZ7abQmW5Q+mP2nzIIE8Ue4= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:from :content-type:subject:date:message-id:cc:to:mime-version; s= default; bh=OrFpqVZi6g+H7spQlA7Gw/R41uk=; b=m31ln6sVOZOAc/W7qqVc f499cq0J/t0SlVYBKSpKgnoh6meYMMPpYTuoOKVmJSLq9RxrXKs6dApQ2BlaTB9Q ioJ5rQYFolwvLFN8E/S2V8uRtyxBqF0vCdGk7iy0ZYeeNMkX52twXqk0vJrZaACc gcM8aDyiQnrS4yIi7cbygIM= Received: (qmail 73188 invoked by alias); 7 Sep 2015 15:23:56 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 73177 invoked by uid 89); 7 Sep 2015 15:23:55 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-1.2 required=5.0 tests=AWL, BAYES_00, RCVD_IN_DNSWL_LOW, SPF_PASS, UNSUBSCRIBE_BODY autolearn=no version=3.3.2 X-HELO: relay1.mentorg.com Received: from relay1.mentorg.com (HELO relay1.mentorg.com) (192.94.38.131) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 07 Sep 2015 15:23:53 +0000 Received: from nat-ies.mentorg.com ([192.94.31.2] helo=SVR-IES-FEM-02.mgc.mentorg.com) by relay1.mentorg.com with esmtp id 1ZYyGr-0007Iy-MK from Iain_Sandoe@mentor.com ; Mon, 07 Sep 2015 08:23:50 -0700 Received: from [127.0.0.1] (137.202.0.76) by SVR-IES-FEM-02.mgc.mentorg.com (137.202.0.106) with Microsoft SMTP Server id 14.3.224.2; Mon, 7 Sep 2015 16:23:48 +0100 From: Iain Sandoe Subject: [PATCH, libiberty] Fix PR63758 by using the _NSGetEnviron() API on Darwin. Date: Mon, 7 Sep 2015 16:23:35 +0100 Message-ID: <37D7634D-114B-4D35-91E4-5E5237FA487D@codesourcery.com> CC: , Ian Lance Taylor To: gcc-patches List MIME-Version: 1.0 (Apple Message framework v1283) Hi, This is mostly Roland's patch with one extra case added by me plus I moved the new header to include/ as suggested in c#7 of the PR since there are other users for it in the compiler. == On Darwin platforms, when referenced from the main executable, it's permitted to access *_environ directly. When the environment is required from a shared library then the _NSGetEnviron() API should be used. Since libiberty is used from shared libraries (such as libcc1) this mechanism should be applied here. OK for trunk and active branches? Iain include/ Roland McGrath PR other/63758 * environ.h: New file. libiberty/ Roland McGrath Iain Sandoe PR other/63758 * pex-unix.c: Obtain the environment interface from settings in environ.h rather than in-line code. Update copyright date. * setenv.c: Likewise. * xmalloc.c: Likewise. From 9dec1e4a7a62a1f556dce6e35133e4c503898a74 Mon Sep 17 00:00:00 2001 From: Iain Sandoe Date: Mon, 7 Sep 2015 10:22:07 +0100 Subject: [PATCH] [libiberty] Provide support for indirect use of _environ by Darwin. When referenced from the main executable, it's permitted to access *_environ directly. When environ is required from a shared library then the NSGetEnviron() API should be used. Since libiberty is used from shared libraries (such as libcc1) this mechanism should be applied here. --- include/environ.h | 33 +++++++++++++++++++++++++++++++++ libiberty/pex-unix.c | 5 ++--- libiberty/setenv.c | 10 +++------- libiberty/xmalloc.c | 5 +++-- 4 files changed, 41 insertions(+), 12 deletions(-) create mode 100644 include/environ.h diff --git a/include/environ.h b/include/environ.h new file mode 100644 index 0000000..c18902b --- /dev/null +++ b/include/environ.h @@ -0,0 +1,33 @@ +/* Declare the environ system variable. + Copyright (C) 2015 Free Software Foundation, Inc. + +This file is part of the libiberty library. +Libiberty is free software; you can redistribute it and/or +modify it under the terms of the GNU Library General Public +License as published by the Free Software Foundation; either +version 2 of the License, or (at your option) any later version. + +Libiberty 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 +Library General Public License for more details. + +You should have received a copy of the GNU Library General Public +License along with libiberty; see the file COPYING.LIB. If not, +write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, +Boston, MA 02110-1301, USA. */ + +/* On OSX, the environ variable can be used directly in the code of an + executable, but cannot be used in the code of a shared library (such as + GCC's liblto_plugin, which links in libiberty code). Instead, the + function _NSGetEnviron can be called to get the address of environ. */ + +#ifndef HAVE_ENVIRON_DECL +# ifdef __APPLE__ +# include +# define environ (*_NSGetEnviron ()) +# else +extern char **environ; +# endif +# define HAVE_ENVIRON_DECL +#endif diff --git a/libiberty/pex-unix.c b/libiberty/pex-unix.c index 0715115..b48f315 100644 --- a/libiberty/pex-unix.c +++ b/libiberty/pex-unix.c @@ -2,7 +2,7 @@ with other subprocesses), and wait for it. Generic Unix version (also used for UWIN and VMS). Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005, 2009, - 2010 Free Software Foundation, Inc. + 2010, 2015 Free Software Foundation, Inc. This file is part of the libiberty library. Libiberty is free software; you can redistribute it and/or @@ -23,6 +23,7 @@ Boston, MA 02110-1301, USA. */ #include "config.h" #include "libiberty.h" #include "pex-common.h" +#include "environ.h" #include #include @@ -390,8 +391,6 @@ pex_child_error (struct pex_obj *obj, const char *executable, /* Execute a child. */ -extern char **environ; - #if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE) /* Implementation of pex->exec_child using the Cygwin spawn operation. */ diff --git a/libiberty/setenv.c b/libiberty/setenv.c index 714ca0a..5b51193 100644 --- a/libiberty/setenv.c +++ b/libiberty/setenv.c @@ -1,5 +1,5 @@ -/* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011 Free Software Foundation, - Inc. +/* Copyright (C) 1992, 1995, 1996, 1997, 2002, 2011, 2015 + Free Software Foundation, Inc. This file based on setenv.c in the GNU C Library. The GNU C Library is free software; you can redistribute it and/or @@ -62,11 +62,7 @@ extern int errno; #endif #define __environ environ -#ifndef HAVE_ENVIRON_DECL -#ifndef environ -extern char **environ; -#endif -#endif +#include "environ.h" #undef setenv #undef unsetenv diff --git a/libiberty/xmalloc.c b/libiberty/xmalloc.c index 3e97aab..f849aee 100644 --- a/libiberty/xmalloc.c +++ b/libiberty/xmalloc.c @@ -1,5 +1,6 @@ /* memory allocation routines with error checking. - Copyright 1989, 90, 91, 92, 93, 94 Free Software Foundation, Inc. + Copyright 1989, 1990, 1991, 1992, 1993, 1994, 2015 + Free Software Foundation, Inc. This file is part of the libiberty library. Libiberty is free software; you can redistribute it and/or @@ -65,6 +66,7 @@ function will be called to print an error message and terminate execution. #endif #include "ansidecl.h" #include "libiberty.h" +#include "environ.h" #include @@ -117,7 +119,6 @@ void xmalloc_failed (size_t size) { #ifdef HAVE_SBRK - extern char **environ; size_t allocated; if (first_break != NULL)