]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blobdiff - ld/ldfile.c
ld: Change -z one-rosegment to --rosegment in comments
[thirdparty/binutils-gdb.git] / ld / ldfile.c
index df7c9cbd65e68fe998a7a96d0764bce069dee1a2..87be885d31a48aaab3a72274fced632a1db08308 100644 (file)
@@ -1,5 +1,5 @@
 /* Linker file opening and searching.
-   Copyright (C) 1991-2023 Free Software Foundation, Inc.
+   Copyright (C) 1991-2024 Free Software Foundation, Inc.
 
    This file is part of the GNU Binutils.
 
 #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
@@ -62,6 +56,12 @@ 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,21 +303,20 @@ is_sysrooted_pathname (const char *name)
 }
 
 /* Adds NAME to the library search path.
-   Makes a copy of NAME using xmalloc().  */
+   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.  */
 
-void
-ldfile_add_library_path (const char *name, bool cmdline)
+search_dirs_type *
+ldfile_add_library_path (const char *name, enum search_dir_source source)
 {
   search_dirs_type *new_dirs;
 
-  if (!cmdline && config.only_cmd_line_lib_dirs)
-    return;
+  if (source != search_dir_cmd_line && config.only_cmd_line_lib_dirs)
+    return NULL;
 
   new_dirs = (search_dirs_type *) xmalloc (sizeof (search_dirs_type));
-  new_dirs->next = NULL;
-  new_dirs->cmdline = cmdline;
-  *search_tail_ptr = new_dirs;
-  search_tail_ptr = &new_dirs->next;
+  new_dirs->source = source;
 
   /* If a directory is marked as honoring sysroot, prepend the sysroot path
      now.  */
@@ -327,6 +326,25 @@ ldfile_add_library_path (const char *name, bool cmdline)
     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.  */
@@ -352,9 +370,9 @@ ldfile_try_open_bfd (const char *attempt,
       return false;
     }
 
-  /* PR 30568: Do not track lto generated temporary object files.  */
+  /* PR 30568: Do not track plugin generated object files.  */
 #if BFD_SUPPORTS_PLUGINS
-  if (!entry->flags.lto_output)
+  if (entry->plugin != NULL)
 #endif
     track_dependency_files (attempt);
 
@@ -365,7 +383,7 @@ ldfile_try_open_bfd (const char *attempt,
   entry->the_bfd->is_linker_input = 1;
 
 #if BFD_SUPPORTS_PLUGINS
-  if (entry->flags.lto_output)
+  if (entry->plugin != NULL)
     entry->the_bfd->lto_output = 1;
 #endif
 
@@ -576,6 +594,14 @@ 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))
@@ -743,7 +769,10 @@ try_open (const char *name, bool *sysrooted)
   result = fopen (name, "r");
 
   if (result != NULL)
-    *sysrooted = is_sysrooted_pathname (name);
+    {
+      *sysrooted = is_sysrooted_pathname (name);
+      track_dependency_files (name);
+    }
 
   if (verbose)
     {
@@ -841,7 +870,7 @@ ldfile_find_command_file (const char *name,
        {
          search_dirs_type **save_tail_ptr = search_tail_ptr;
          search_tail_ptr = &script_search;
-         ldfile_add_library_path (script_dir, true);
+         (void) ldfile_add_library_path (script_dir, search_dir_cmd_line);
          search_tail_ptr = save_tail_ptr;
        }
     }
@@ -855,6 +884,11 @@ 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);
@@ -868,19 +902,7 @@ ldfile_find_command_file (const char *name,
   return result;
 }
 
-enum script_open_style {
-  script_nonT,
-  script_T,
-  script_defaultT
-};
-
-struct script_name_list
-{
-  struct script_name_list *next;
-  enum script_open_style open_how;
-  char name[1];
-};
-
+struct script_name_list *processed_scripts = NULL;
 /* Open command file NAME.  */
 
 static void
@@ -888,7 +910,6 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
 {
   FILE *ldlex_input_stack;
   bool sysrooted;
-  static struct script_name_list *processed_scripts = NULL;
   struct script_name_list *script;
   size_t len;
 
@@ -925,8 +946,6 @@ ldfile_open_command_file_1 (const char *name, enum script_open_style open_how)
       return;
     }
 
-  track_dependency_files (name);
-
   lex_push_file (ldlex_input_stack, name, sysrooted);
 
   lineno = 1;