Submitted By:            Xi Ruoyao <xry111@mengyan1223.wang>
Date:                    2024-11-08
Initial Package Version: 2.43.1
Upstream Status:         Committed
Origin:                  Upstream (see the From line for commit hash)
Description:             Fix a bug causing unnecessary relink with
                         cmake-3.31.0 or newer.

From 280374309b190b1415986c4146b25ba43c207300 Mon Sep 17 00:00:00 2001
From: Alan Modra <amodra@gmail.com>
Date: Sat, 26 Oct 2024 08:57:49 +1030
Subject: [PATCH] PR32300, --dependency-file: link dependencies are not all
 collected

	PR 32300
	PR 31904
Revert patch accidentally committed with 057a2b4c4b

(cherry picked from commit 6ecc44f6980c0f8c0f0934bc10412c1d7fa3c544)
---
 ld/ld.texi                           |  19 ----
 ld/ldelf.c                           |   2 +-
 ld/ldfile.c                          |  69 ++++-----------
 ld/ldfile.h                          |  21 +----
 ld/ldgram.y                          |   2 +-
 ld/ldlang.h                          |   8 +-
 ld/lexsup.c                          |   6 +-
 ld/plugin.c                          |  12 +--
 ld/testsuite/ld-plugin/libdep-a.c    |   7 --
 ld/testsuite/ld-plugin/libdep-b.c    |   5 --
 ld/testsuite/ld-plugin/libdep-main.c |   7 --
 ld/testsuite/ld-plugin/libdep.exp    | 124 ---------------------------
 12 files changed, 33 insertions(+), 249 deletions(-)
 delete mode 100644 ld/testsuite/ld-plugin/libdep-a.c
 delete mode 100644 ld/testsuite/ld-plugin/libdep-b.c
 delete mode 100644 ld/testsuite/ld-plugin/libdep-main.c
 delete mode 100644 ld/testsuite/ld-plugin/libdep.exp

diff --git a/ld/ld.texi b/ld/ld.texi
index 89e3913317a..2eafeb7dd39 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -7614,7 +7614,6 @@ command line option. @xref{Options}.
 
 @menu
 * libdep Plugin::	Static Library Dependencies Plugin
-* lto Plugin::          LTO Plugin
 @end menu
 
 @node libdep Plugin
@@ -7653,24 +7652,6 @@ the @samp{__.LIBDEP} member of @file{libssl.a} would contain
 -L/usr/local/lib -lcrypto
 @end smallexample
 
-Note any library search directories added in this way are only used to
-search for libraries which are also added the same plugin.  They are
-not used for searching for libraries specified on the linker command
-line, linker scripts or other plugins.  This does however present a
-problem if multiple archives with @samp{__.LIBDEP} entries are present
-as they will all be handled by the libdep plugin and thus they will
-share library search paths.  This could result in a library being
-loaded from an unexpected location.
-
-@node lto Plugin
-@section LTO Plugin
-Although not shipped with the binutils the LTO Plugin from the GCC
-project is nevertheless a plugin that is designed to work with the
-linker.  The plugin intercepts object files as they are loaded by the
-linker and instead sends them to the LTO compiler.  When that compiler
-finishes the resulting object file(s) are passed back to the linker
-for normal processing.
-
 @node Special Sections
 @chapter Special Sections
 When linking ELF format object files @command{ld} treats some sections
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 09691609fc0..04045acbf3d 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -1170,7 +1170,7 @@ ldelf_handle_dt_needed (struct elf_link_hash_table *htab,
 	    {
 	      char *filename;
 
-	      if (search->source != search_dir_linker_script)
+	      if (search->cmdline)
 		continue;
 	      filename = (char *) xmalloc (strlen (search->name) + len + 2);
 	      sprintf (filename, "%s/%s", search->name, l->name);
diff --git a/ld/ldfile.c b/ld/ldfile.c
index 87be885d31a..f1107a1b7d7 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -40,6 +40,12 @@
 #include "plugin.h"
 #endif /* BFD_SUPPORTS_PLUGINS */
 
+bool ldfile_assumed_script = false;
+const char *ldfile_output_machine_name = "";
+unsigned long ldfile_output_machine;
+enum bfd_architecture ldfile_output_architecture;
+search_dirs_type *search_head;
+
 #ifdef VMS
 static char *slash = "";
 #else
@@ -56,12 +62,6 @@ typedef struct search_arch
   struct search_arch *next;
 } search_arch_type;
 
-bool                   ldfile_assumed_script = false;
-const char *           ldfile_output_machine_name = "";
-unsigned long          ldfile_output_machine;
-enum bfd_architecture  ldfile_output_architecture;
-search_dirs_type *     search_head;
-
 static search_dirs_type **search_tail_ptr = &search_head;
 static search_arch_type *search_arch_head;
 static search_arch_type **search_arch_tail_ptr = &search_arch_head;
@@ -303,20 +303,21 @@ is_sysrooted_pathname (const char *name)
 }
 
 /* Adds NAME to the library search path.
-   Makes a copy of NAME using xmalloc().
-   Returns a pointer to the newly created search_dirs_type structure
-   or NULL if there was a problem.  */
+   Makes a copy of NAME using xmalloc().  */
 
-search_dirs_type *
-ldfile_add_library_path (const char *name, enum search_dir_source source)
+void
+ldfile_add_library_path (const char *name, bool cmdline)
 {
   search_dirs_type *new_dirs;
 
-  if (source != search_dir_cmd_line && config.only_cmd_line_lib_dirs)
-    return NULL;
+  if (!cmdline && config.only_cmd_line_lib_dirs)
+    return;
 
   new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
-  new_dirs->source = source;
+  new_dirs->next = NULL;
+  new_dirs->cmdline = cmdline;
+  *search_tail_ptr = new_dirs;
+  search_tail_ptr = &new_dirs->next;
 
   /* If a directory is marked as honoring sysroot, prepend the sysroot path
      now.  */
@@ -326,25 +327,6 @@ ldfile_add_library_path (const char *name, enum search_dir_source source)
     new_dirs->name = concat (ld_sysroot, name + strlen ("$SYSROOT"), (const char *) NULL);
   else
     new_dirs->name = xstrdup (name);
-
-  /* Accumulate script and command line sourced
-     search paths at the end of the current list.  */
-#if BFD_SUPPORTS_PLUGINS
-  /* PR 31904: But put plugin sourced paths at the start of the list.  */
-  if (source == search_dir_plugin)
-    {
-      new_dirs->next = search_head;
-      search_head = new_dirs;
-    }
-  else
-#endif
-    {
-      new_dirs->next = NULL;
-      *search_tail_ptr = new_dirs;
-      search_tail_ptr = &new_dirs->next;
-    }
-
-  return new_dirs;
 }
 
 /* Try to open a BFD for a lang_input_statement.  */
@@ -370,9 +352,9 @@ ldfile_try_open_bfd (const char *attempt,
       return false;
     }
 
-  /* PR 30568: Do not track plugin generated object files.  */
+  /* PR 30568: Do not track lto generated temporary object files.  */
 #if BFD_SUPPORTS_PLUGINS
-  if (entry->plugin != NULL)
+  if (!entry->flags.lto_output)
 #endif
     track_dependency_files (attempt);
 
@@ -383,7 +365,7 @@ ldfile_try_open_bfd (const char *attempt,
   entry->the_bfd->is_linker_input = 1;
 
 #if BFD_SUPPORTS_PLUGINS
-  if (entry->plugin != NULL)
+  if (entry->flags.lto_output)
     entry->the_bfd->lto_output = 1;
 #endif
 
@@ -594,14 +576,6 @@ ldfile_open_file_search (const char *arch,
     {
       char *string;
 
-#if BFD_SUPPORTS_PLUGINS
-      /* PR 31904: Only check a plugin sourced search
-	 directory if the file is from the same plugin.  */
-      if (search->source == search_dir_plugin
-	  && entry->plugin != search->plugin)
-	continue;
-#endif
-
       if (entry->flags.dynamic && !bfd_link_relocatable (&link_info))
 	{
 	  if (ldemul_open_dynamic_archive (arch, search, entry))
@@ -870,7 +844,7 @@ ldfile_find_command_file (const char *name,
 	{
 	  search_dirs_type **save_tail_ptr = search_tail_ptr;
 	  search_tail_ptr = &script_search;
-	  (void) ldfile_add_library_path (script_dir, search_dir_cmd_line);
+	  ldfile_add_library_path (script_dir, true);
 	  search_tail_ptr = save_tail_ptr;
 	}
     }
@@ -884,11 +858,6 @@ ldfile_find_command_file (const char *name,
        search != NULL;
        search = search->next)
     {
-#if BFD_SUPPORTS_PLUGINS
-      /* Do not search for linker commands in plugin sourced search directories.  */
-      if (search->source == search_dir_plugin)
-	continue;
-#endif
       path = concat (search->name, slash, name, (const char *) NULL);
       result = try_open (path, sysrooted);
       free (path);
diff --git a/ld/ldfile.h b/ld/ldfile.h
index 08826f65f04..f79abf2310d 100644
--- a/ld/ldfile.h
+++ b/ld/ldfile.h
@@ -26,15 +26,6 @@ extern unsigned long ldfile_output_machine;
 extern enum bfd_architecture ldfile_output_architecture;
 extern const char *ldfile_output_machine_name;
 
-enum search_dir_source
-{
-  search_dir_cmd_line,
-#if BFD_SUPPORTS_PLUGINS
-  search_dir_plugin,
-#endif
-  search_dir_linker_script
-};
-
 /* Structure used to hold the list of directories to search for
    libraries.  */
 
@@ -44,12 +35,8 @@ typedef struct search_dirs
   struct search_dirs *next;
   /* Name of directory.  */
   const char *name;
-  /* Where the search path came from.  */
-  enum search_dir_source source;
-#if BFD_SUPPORTS_PLUGINS
-  /* For search dirs added by plugins, the plugin that added them.  */
-  void * plugin;
-#endif
+  /* TRUE if this is from the command line.  */
+  bool cmdline;
 } search_dirs_type;
 
 enum script_open_style
@@ -72,8 +59,8 @@ extern search_dirs_type *search_head;
 
 extern void ldfile_add_arch
   (const char *);
-extern search_dirs_type * ldfile_add_library_path
-  (const char *, enum search_dir_source);
+extern void ldfile_add_library_path
+  (const char *, bool cmdline);
 extern void ldfile_open_command_file
   (const char *name);
 extern void ldfile_open_script_file
diff --git a/ld/ldgram.y b/ld/ldgram.y
index 7c04025f526..07c19ba8692 100644
--- a/ld/ldgram.y
+++ b/ld/ldgram.y
@@ -321,7 +321,7 @@ ifile_p1:
 	|	TARGET_K '(' NAME ')'
 		{ lang_add_target($3); }
 	|	SEARCH_DIR '(' filename ')'
-		{ ldfile_add_library_path ($3, search_dir_linker_script); }
+		{ ldfile_add_library_path ($3, false); }
 	|	OUTPUT '(' filename ')'
 		{ lang_add_output($3, 1); }
 	|	OUTPUT_FORMAT '(' NAME ')'
diff --git a/ld/ldlang.h b/ld/ldlang.h
index f36e04c586a..4c1bb002f8e 100644
--- a/ld/ldlang.h
+++ b/ld/ldlang.h
@@ -293,6 +293,9 @@ struct lang_input_statement_flags
 
   /* Set if the file was claimed from an archive.  */
   unsigned int claim_archive : 1;
+
+  /* Set if added by the lto plugin add_input_file callback.  */
+  unsigned int lto_output : 1;
 #endif /* BFD_SUPPORTS_PLUGINS */
 
   /* Head of list of pushed flags.  */
@@ -329,11 +332,6 @@ typedef struct lang_input_statement_struct
   const char *target;
 
   struct lang_input_statement_flags flags;
-
-#if BFD_SUPPORTS_PLUGINS
-  /* If non-NULL the plugin that added this file.  */
-  void * plugin;
-#endif  
 } lang_input_statement_type;
 
 typedef struct
diff --git a/ld/lexsup.c b/ld/lexsup.c
index 4aa0124ce2f..96fb73159bf 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -1052,8 +1052,7 @@ parse_args (unsigned argc, char **argv)
 	  xexit (0);
 	  break;
 	case 'L':
-	  /* FIXME: Check the return value ?  */
-	  (void) ldfile_add_library_path (optarg, search_dir_cmd_line);
+	  ldfile_add_library_path (optarg, true);
 	  break;
 	case 'l':
 	  lang_add_input_file (optarg, lang_input_file_is_l_enum, NULL);
@@ -2087,8 +2086,7 @@ set_default_dirlist (char *dirlist_ptr)
       if (p != NULL)
 	*p = '\0';
       if (*dirlist_ptr != '\0')
-	/* FIXME: Check the return value ?  */
-	(void) ldfile_add_library_path (dirlist_ptr, search_dir_cmd_line);
+	ldfile_add_library_path (dirlist_ptr, true);
       if (p == NULL)
 	break;
       dirlist_ptr = p + 1;
diff --git a/ld/plugin.c b/ld/plugin.c
index 34ae3a0e0d8..2cd0279c93a 100644
--- a/ld/plugin.c
+++ b/ld/plugin.c
@@ -933,7 +933,7 @@ add_input_file (const char *pathname)
 			    NULL);
   if (!is)
     return LDPS_ERR;
-  is->plugin = called_plugin;
+  is->flags.lto_output = 1;
   return LDPS_OK;
 }
 
@@ -948,23 +948,17 @@ add_input_library (const char *pathname)
 			    NULL);
   if (!is)
     return LDPS_ERR;
-  is->plugin = called_plugin;
+  is->flags.lto_output = 1;
   return LDPS_OK;
 }
 
 /* Set the extra library path to be used by libraries added via
    add_input_library.  */
-
 static enum ld_plugin_status
 set_extra_library_path (const char *path)
 {
-  search_dirs_type * sdt;
-
   ASSERT (called_plugin);
-  sdt = ldfile_add_library_path (xstrdup (path), search_dir_plugin);
-  if (sdt == NULL)
-    return LDPS_ERR;
-  sdt->plugin = called_plugin;
+  ldfile_add_library_path (xstrdup (path), false);
   return LDPS_OK;
 }
 
diff --git a/ld/testsuite/ld-plugin/libdep-a.c b/ld/testsuite/ld-plugin/libdep-a.c
deleted file mode 100644
index 5a01ce8c88a..00000000000
--- a/ld/testsuite/ld-plugin/libdep-a.c
+++ /dev/null
@@ -1,7 +0,0 @@
-extern int g (void);
-
-int
-f (void)
-{
-  return g();
-}
diff --git a/ld/testsuite/ld-plugin/libdep-b.c b/ld/testsuite/ld-plugin/libdep-b.c
deleted file mode 100644
index d2ca7e08694..00000000000
--- a/ld/testsuite/ld-plugin/libdep-b.c
+++ /dev/null
@@ -1,5 +0,0 @@
-int
-g (void)
-{
-  return 4;
-}
diff --git a/ld/testsuite/ld-plugin/libdep-main.c b/ld/testsuite/ld-plugin/libdep-main.c
deleted file mode 100644
index 7563a7d5f88..00000000000
--- a/ld/testsuite/ld-plugin/libdep-main.c
+++ /dev/null
@@ -1,7 +0,0 @@
-extern int f (void);
-
-int
-main (void)
-{
-  return f();
-}
diff --git a/ld/testsuite/ld-plugin/libdep.exp b/ld/testsuite/ld-plugin/libdep.exp
deleted file mode 100644
index 6e56b6bab18..00000000000
--- a/ld/testsuite/ld-plugin/libdep.exp
+++ /dev/null
@@ -1,124 +0,0 @@
-# Expect script for ld-plugin LIBDEP tests
-#   Copyright (C) 2024 Free Software Foundation, Inc.
-#
-# This file is part of the GNU Binutils.
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program 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 General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
-# MA 02110-1301, USA.
-
-# These tests require the plugin API.
-if { ![check_plugin_api_available] } {
-    return
-}
-
-# Check to see if the C compiler works
-# FIXME: This is being lazy, we could create assembler equivalents instead.
-if { ![check_compiler_available] } {
-    return
-}
-
-proc run_test { } {
-    global CC_FOR_TARGET
-    global srcdir
-    global subdir
-    global ar
-    global ld
-    global libdep
-    global base_dir
-    
-    set testname "libdep test"
-    
-    # Create temporary directories if they do not already exist.
-    file mkdir -p libdep-a libdep-b
-
-    # Delete old versions of the files we are about to build, just in case.
-    file delete libdep-a/a.o libdep-a/liba.a libdep-b/b.o libdep-b/libc.a libdep-main.o
-
-    # Build object files.
-    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-a.c libdep-a/a.o]} {
-	fail "$testname: could not compile source file 1"
-	return
-    }
-    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-b.c libdep-b/b.o]} {
-	fail "$testname: could not compile source file 2"
-	return
-    }
-    if {![ld_compile $CC_FOR_TARGET $srcdir/$subdir/libdep-main.c libdep-main.o]} {
-	fail "$testname: could not compile source file 3"
-	return
-    }
-
-    # Create static archives from the objects.
-
-    # For the first archive we add a libdep element that loads the second library.
-    if {![ar_simple_create $ar {--record-libdeps "-Llibdep-b -lc"} libdep-a/liba.a libdep-a/a.o]} {
-	fail "$testname: could not create archive 1"
-	return
-    }
-
-    # For the second archive we choose a name - libc.a - that is likely to clash
-    # with a system library.  This will help to check that the library loaded by
-    # following the libdep element in the first library is the one that we expect.
-    if {![ar_simple_create $ar {} libdep-b/libc.a libdep-b/b.o]} {
-	fail "$testname: could not create archive 2"
-	return
-    }
-
-    # Find the libdep plugin.
-    # Use libtool to find the path to the plugin rather
-    # than worrying about run paths or anything like that.
-    catch "exec $base_dir/libtool --config" lt_config
-    verbose "Full lt config: $lt_config" 2
-    # Look for "objdir=.libs"
-    regexp -line "^objdir=.*$" "$lt_config" lt_objdir
-    verbose "lt_objdir line is '$lt_objdir'" 2
-    set lt_objdir [regsub "objdir=" "$lt_objdir" ""]
-
-    if { [ file exists "$base_dir/$lt_objdir/libdep.so" ] } {
-	set libdep_plugin "$base_dir/$lt_objdir/libdep.so"
-    } else {
-	# FIXME: Check in the system bfd-plugin directory ?
-	fail "$testname - could not locate libdep plugin"
-	return
-    }
-
-    verbose "Full plugin path: $libdep_plugin" 1
-    
-    # Link the main object file with the liba.a library.
-    # Use the libdep plugin to read the __.LIBDEP element in the liba.a library
-    # and so bring in the libdep-b.o object file from the libc.a library.
-    # Failure to locate the libc.a library, or loading the wrong libc.a library
-    # will result in an unresolved reference error.
-    set exec_output [run_host_cmd "$ld" "-plugin $libdep_plugin -o libdep.exe libdep-main.o -L libdep-a -la -e 0"]
-    set exec_output [prune_warnings $exec_output]
-
-    set expected_output "got deps for library libdep-a/liba.a: -Llibdep-b -lc"
-    
-    if ![string match $expected_output $exec_output] then {
-	fail "$testname: did not get expected output from the linker"
-	return
-    }
-
-    regsub -all "$expected_output" $exec_output "\\1" exec_output
-
-    if {![string match "" $exec_output]} {
-	fail "$testname: unexpected output from linker: $exec_output"
-	return
-    }
-
-    pass "$testname"
-}
-
-run_test
-- 
2.47.0

