From e7f86cdcaf03e4ddb98d0df9d07894d9ffb7d91a Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Thu, 2 Jul 2020 12:26:09 -0700 Subject: [PATCH] Add -fld-path= to specify an arbitrary executable as the linker The value can be either a relative path (relative to a COMPILER_PATH directory or a PATH directory) or an absolute path. -fld-path= complements -fuse-ld={bfd,gold,lld} which specifies the linker flavor. PR driver/93645 * common.opt (-fld-path=): Add -fld-path= * opts.c (common_handle_option): Handle OPT_fld_path_. * gcc.c (driver_handle_option): Likewise. * collect2.c (main): Likewise. * doc/invoke.texi: Document -fld-path=. --- gcc/collect2.c | 57 ++++++++++++++++++++++++++++++++------------- gcc/common.opt | 4 ++++ gcc/doc/invoke.texi | 6 +++++ gcc/gcc.c | 2 +- gcc/opts.c | 1 + 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/gcc/collect2.c b/gcc/collect2.c index f8a5ce45994..efa652f7f82 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=bfd", 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], "-fld-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,27 @@ 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 -fld-path= does not contain a slash, search for the command using + the PATH environment variable. */ + if (lbasename (ld_path) == ld_path) + 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); @@ -1297,9 +1320,11 @@ main (int argc, char **argv) #endif } else if (!use_collect_ld - && strncmp (arg, "-fuse-ld=", 9) == 0) + && (strncmp (arg, "-fuse-ld=", 9) == 0 || + strncmp (arg, "-fld-path=", 10) == 0)) { - /* Do not pass -fuse-ld={bfd|gold|lld} to the linker. */ + /* Do not pass -fuse-ld={bfd|gold|lld} or -fld-path= to the + linker. */ ld1--; ld2--; } diff --git a/gcc/common.opt b/gcc/common.opt index df8af365d1b..d5dfbb5c9c6 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -2919,6 +2919,10 @@ fuse-ld=lld Common Driver Negative(fuse-ld=lld) Use the lld LLVM linker instead of the default linker. +fld-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 e21d8a5217b..bfb6222f96b 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -14651,6 +14651,12 @@ 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 -fld-path=@var{linker} +@opindex fld-path=linker +Use the specified executable named @var{linker} instead of the default linker. +If @var{linker} does not contain any slash character, the linker will be +searched for using @env{PATH}. This option overrides @option{-fuse-ld=}. + @cindex Libraries @item -l@var{library} @itemx -l @var{library} diff --git a/gcc/gcc.c b/gcc/gcc.c index c0eb3c10cfd..a57a3d0aec4 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=*:%