From patchwork Tue Jul 21 04:07:42 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fangrui Song X-Patchwork-Id: 1332752 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=b9nkB4AB; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4B9lSK4rnZz9sRR for ; Tue, 21 Jul 2020 14:07:59 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F22FA385700C; Tue, 21 Jul 2020 04:07:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org F22FA385700C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1595304477; bh=PeaR1RwjD1AmPfVe/Ar7n5ys0Srb2UvRaPRAVs8LSCA=; h=Date:In-Reply-To:References:Subject:To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=b9nkB4ABbMdM4Uw0i0bMYeljFkLfMgLife60J9qoXzxQXwb07jy94Ozq28T0nZaoU fa6Om3M9qKJIFJD6JZ99o/CMlO67uwujvN3NlVpNyVhZnD4LnVk6zimEUoYp1UDpLg DTeFsAD1BvhT9lj9xcYfvWGnN7AIsCpvSXAaf5LU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-yb1-xb4a.google.com (mail-yb1-xb4a.google.com [IPv6:2607:f8b0:4864:20::b4a]) by sourceware.org (Postfix) with ESMTPS id 231363857C4C for ; Tue, 21 Jul 2020 04:07:54 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 231363857C4C Received: by mail-yb1-xb4a.google.com with SMTP id l17so3758601ybe.18 for ; Mon, 20 Jul 2020 21:07:54 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=PeaR1RwjD1AmPfVe/Ar7n5ys0Srb2UvRaPRAVs8LSCA=; b=P/MBRnsgTemb7+YtQyiPo08TwfXpSSOZA4AooGLHMzy1Ax4nKMwEY5IL+c/Uep91Mj Qlew86dhVlRDOD6FuusYkNv+lQq3CkvsIfXxe9iBCONUfw/F3s0iRTZ/o85GDaNYjD5t 7MzX9pjhamdrRij2vc4yl2uWnYEyn5fR72qYXUoSd/15LU2dwMjPNVZVrlxK0vFP26cV qR+xIhenvBnGd17GrTEdeQK8LeXHVVFMoYAuJhhTRSBduqNVKxqxCXFnYj3Ekq5i0p9H Guoakmo+Lk17OZZshJsKZehut6X83hq1zbNmAIXQeEQuU/KE2kqHau/M8XeALfMqFMFU KBTA== X-Gm-Message-State: AOAM533uuF1pi+JjTbgGIU/Ms7cBL5ozriv9ORw9hXm3y5Ky3/MkaBT4 tPlwfB+U+XQnsPbMzJ3hTs8s50HcXobXeCcTQ7T5uCPCFm7aIP1yEaskJkOK776FUkaGpzi1rCD WxmXQsffkml5WMi/pXY98l+ZuxPmO1eqxkkCc9UWEyzlkl6QfdUA0dxdPdS9CdNUKkg== X-Google-Smtp-Source: ABdhPJwARYkm0wfmOMtKu1+mFHDCoODLXHJCV0smWo4scnNmYSKaD+NTo6K7FbGQ42VDr64Tm0NqVya/UjUK X-Received: by 2002:a25:32c9:: with SMTP id y192mr39763595yby.83.1595304473460; Mon, 20 Jul 2020 21:07:53 -0700 (PDT) Date: Mon, 20 Jul 2020 21:07:42 -0700 In-Reply-To: Message-Id: <20200721040742.4024723-1-maskray@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.28.0.rc0.105.gf9edc3c819-goog Subject: [PATCH v2] Add --ld-path= to specify an arbitrary executable as the linker To: gcc-patches@gcc.gnu.org X-Spam-Status: No, score=-18.5 required=5.0 tests=BAYES_00, DKIMWL_WL_MED, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, USER_IN_DEF_DKIM_WL 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: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Fangrui Song via Gcc-patches From: Fangrui Song Reply-To: Fangrui Song Cc: jakub@redhat.com, Richard Biener Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" If the value does not contain any path component separator (e.g. a slash), the linker will be searched for using COMPILER_PATH followed by PATH. Otherwise, it is either an absolute path or a path relative to the current working directory. --ld-path= complements and overrides -fuse-ld={bfd,gold,lld}. If in the future, we want to make dfferent linker option decisions we can let -fuse-ld= represent the linker flavor and --ld-path= the linker path. PR driver/93645 * common.opt (--ld-path=): Add --ld-path= * opts.c (common_handle_option): Handle OPT__ld_path_. * gcc.c (driver_handle_option): Likewise. * collect2.c (main): Likewise. * doc/invoke.texi: Document --ld-path=. --- Changes in v2: * Renamed -fld-path= to --ld-path= (clang 12.0.0 new option). The option does not affect code generation and is not a language feature, -f* is not suitable. Additionally, clang has other similar --*-path= options, e.g. --cuda-path=. --- gcc/collect2.c | 63 +++++++++++++++++++++++++++++++++++---------- gcc/common.opt | 4 +++ gcc/doc/invoke.texi | 9 +++++++ gcc/gcc.c | 2 +- gcc/opts.c | 1 + 5 files changed, 64 insertions(+), 15 deletions(-) diff --git a/gcc/collect2.c b/gcc/collect2.c index f8a5ce45994..caa1b96ab52 100644 --- a/gcc/collect2.c +++ b/gcc/collect2.c @@ -844,6 +844,7 @@ main (int argc, char **argv) const char **ld1; bool use_plugin = false; bool use_collect_ld = false; + const char *ld_path = NULL; /* The kinds of symbols we will have to consider when scanning the outcome of a first pass link. This is ALL to start with, then might @@ -961,12 +962,21 @@ main (int argc, char **argv) if (selected_linker == USE_DEFAULT_LD) selected_linker = USE_PLUGIN_LD; } - else if (strcmp (argv[i], "-fuse-ld=bfd") == 0) - selected_linker = USE_BFD_LD; - else if (strcmp (argv[i], "-fuse-ld=gold") == 0) - selected_linker = USE_GOLD_LD; - else if (strcmp (argv[i], "-fuse-ld=lld") == 0) - selected_linker = USE_LLD_LD; + else if (strncmp (argv[i], "-fuse-ld=", 9) == 0 + && selected_linker != USE_LD_MAX) + { + if (strcmp (argv[i] + 9, "bfd") == 0) + selected_linker = USE_BFD_LD; + else if (strcmp (argv[i] + 9, "gold") == 0) + selected_linker = USE_GOLD_LD; + else if (strcmp (argv[i] + 9, "lld") == 0) + selected_linker = USE_LLD_LD; + } + else if (strncmp (argv[i], "--ld-path=", 10) == 0) + { + ld_path = argv[i] + 10; + selected_linker = USE_LD_MAX; + } else if (strncmp (argv[i], "-o", 2) == 0) { /* Parse the output filename if it's given so that we can make @@ -1117,14 +1127,34 @@ main (int argc, char **argv) ld_file_name = find_a_file (&cpath, collect_ld_suffix, X_OK); use_collect_ld = ld_file_name != 0; } - /* Search the compiler directories for `ld'. We have protection against - recursive calls in find_a_file. */ - if (ld_file_name == 0) - ld_file_name = find_a_file (&cpath, ld_suffixes[selected_linker], X_OK); - /* Search the ordinary system bin directories - for `ld' (if native linking) or `TARGET-ld' (if cross). */ - if (ld_file_name == 0) - ld_file_name = find_a_file (&path, full_ld_suffixes[selected_linker], X_OK); + if (selected_linker == USE_LD_MAX) + { + /* If --ld-path= does not contain a path component separator, search for + the command using cpath, then using path. Otherwise find the linker + relative to the current working directory. */ + if (lbasename (ld_path) == ld_path) + { + ld_file_name = find_a_file (&cpath, ld_path, X_OK); + if (ld_file_name == 0) + ld_file_name = find_a_file (&path, ld_path, X_OK); + } + else if (file_exists (ld_path)) + { + ld_file_name = ld_path; + } + } + else + { + /* Search the compiler directories for `ld'. We have protection against + recursive calls in find_a_file. */ + if (ld_file_name == 0) + ld_file_name = find_a_file (&cpath, ld_suffixes[selected_linker], X_OK); + /* Search the ordinary system bin directories + for `ld' (if native linking) or `TARGET-ld' (if cross). */ + if (ld_file_name == 0) + ld_file_name = + find_a_file (&path, full_ld_suffixes[selected_linker], X_OK); + } #ifdef REAL_NM_FILE_NAME nm_file_name = find_a_file (&path, REAL_NM_FILE_NAME, X_OK); @@ -1461,6 +1491,11 @@ main (int argc, char **argv) ld2--; #endif } + else if (strncmp (arg, "--ld-path=", 10) == 0) + { + ld1--; + ld2--; + } else if (strncmp (arg, "--sysroot=", 10) == 0) target_system_root = arg + 10; else if (strcmp (arg, "--version") == 0) diff --git a/gcc/common.opt b/gcc/common.opt index a3893a4725e..5adbd8c18a3 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -2940,6 +2940,10 @@ fuse-ld=lld Common Driver Negative(fuse-ld=lld) Use the lld LLVM linker instead of the default linker. +-ld-path= +Common Driver Joined +Use the specified executable instead of the default linker. + fuse-linker-plugin Common Undocumented Var(flag_use_linker_plugin) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index ba18e05fb1a..e185e40ffac 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -14718,6 +14718,15 @@ Use the @command{gold} linker instead of the default linker. @opindex fuse-ld=lld Use the LLVM @command{lld} linker instead of the default linker. +@item --ld-path=@var{linker} +@opindex -ld-path=linker +Use the specified executable named @var{linker} instead of the default linker. +If @var{linker} does not contain any path component separator (e.g. a slash), +the linker will be searched for using @env{COMPILER_PATH} followed by +@env{PATH}. Otherwise, it is either an absolute path or a path relative to the +current working directory. If @option{-fuse-ld=} is also specified, the linker +path is specified by @option{--ld-path=}. + @cindex Libraries @item -l@var{library} @itemx -l @var{library} diff --git a/gcc/gcc.c b/gcc/gcc.c index c0eb3c10cfd..05fa5375f06 100644 --- a/gcc/gcc.c +++ b/gcc/gcc.c @@ -1077,7 +1077,7 @@ proper position among the other output files. */ LINK_PLUGIN_SPEC \ "%{flto|flto=*:%max_errors = value; break; + case OPT__ld_path_: case OPT_fuse_ld_bfd: case OPT_fuse_ld_gold: case OPT_fuse_ld_lld: