[PATCH users/roland/ld-depfile] ld, gold: Add --dependency-file option.

Roland McGrath mcgrathr@google.com
Sat Jun 13 05:26:10 GMT 2020


[I think my MUA may be mangling the patch, but it's in git as branch
users/roland/ld-depfile.]

I think the utility is self-evident.  Ok for trunk?


Thanks,
Roland


gold/
2020-06-12  Roland McGrath  <mcgrathr@google.com>

* options.h (class General_options): Add --dependency-file option.
* fileread.cc (File_read::files_read): New static variable.
(File_read::write_dependency_file): New static member function.
* fileread.h (class File_read): Declare them.
* layout.cc (Close_task_runner::run): Call write_dependency_file
if --dependency-file was passed.

ld/
2020-06-12  Roland McGrath  <mcgrathr@google.com>

* NEWS: Note --dependency-file.
* ld.texi (Options): Document --dependency-file.
* ldlex.h (enum option_values): Add OPTION_DEPENDENCY_FILE.
* ld.h (ld_config_type): New member dependency_file.
* lexsup.c (ld_options, parse_args): Parse --dependency-file.
* ldmain.c (struct dependency_file): New type.
(dependency_files, dependency_files_tail): New static variables.
(track_dependency_files): New function.
(write_dependency_file): New function.
(main): Call it when --dependency-file was passed.
* ldfile.c (ldfile_try_open_bfd): Call track_dependency_files.
* ldelf.c (ldelf_try_needed): Likewise.
* pe-dll.c (pe_implied_import_dll): Likewise.

diff --git a/gold/fileread.cc b/gold/fileread.cc
index bebe0aba8d..ea89723168 100644
--- a/gold/fileread.cc
+++ b/gold/fileread.cc
@@ -124,6 +124,7 @@ static Initialize_lock
file_counts_initialize_lock(&file_counts_lock);
 unsigned long long File_read::total_mapped_bytes;
 unsigned long long File_read::current_mapped_bytes;
 unsigned long long File_read::maximum_mapped_bytes;
+std::set<std::string> File_read::files_read;

 // Class File_read::View.

@@ -211,6 +212,8 @@ File_read::open(const Task* task, const std::string& name)
       gold_debug(DEBUG_FILES, "Attempt to open %s succeeded",
  this->name_.c_str());
       this->token_.add_writer(task);
+      Hold_optional_lock hl(file_counts_lock);
+      File_read::files_read.insert(this->name_);
     }

   return this->descriptor_ >= 0;
@@ -1138,4 +1141,25 @@ Input_file::open_binary(const Task* task, const
std::string& name)
    binary_to_elf.converted_size());
 }

+void
+File_read::write_dependency_file(const char* dependency_file_name,
+                                 const char* output_file_name)
+{
+  FILE *depfile = fopen(dependency_file_name, "w");
+
+  fprintf(depfile, "%s:", output_file_name);
+  for (std::set<std::string>::const_iterator it = files_read.begin();
+       it != files_read.end();
+       ++it)
+    fprintf(depfile, " \\\n  %s", it->c_str());
+  fprintf(depfile, "\n");
+
+  for (std::set<std::string>::const_iterator it = files_read.begin();
+       it != files_read.end();
+       ++it)
+    fprintf(depfile, "\n%s:\n", it->c_str());
+
+  fclose(depfile);
+}
+
 } // End namespace gold.
diff --git a/gold/fileread.h b/gold/fileread.h
index cf92367c2c..005d2e28c8 100644
--- a/gold/fileread.h
+++ b/gold/fileread.h
@@ -27,6 +27,7 @@

 #include <list>
 #include <map>
+#include <set>
 #include <string>
 #include <vector>

@@ -207,6 +208,11 @@ class File_read
   static void
   print_stats();

+  // Write the dependency file listing all files read.
+  static void
+  write_dependency_file(const char* dependency_file_name,
+                        const char* output_file_name);
+
   // Return the open file descriptor (for plugins).
   int
   descriptor()
@@ -214,7 +220,7 @@ class File_read
     this->reopen_descriptor();
     return this->descriptor_;
   }
-
+
   // Return the file last modification time.  Calls gold_fatal if the stat
   // system call failed.
   Timespec
@@ -247,6 +253,9 @@ class File_read
   // --stats.
   static unsigned long long maximum_mapped_bytes;

+  // Set of names of all files read.
+  static std::set<std::string> files_read;
+
   // A view into the file.
   class View
   {
diff --git a/gold/layout.cc b/gold/layout.cc
index be437f3900..8510210868 100644
--- a/gold/layout.cc
+++ b/gold/layout.cc
@@ -6153,6 +6153,10 @@ Close_task_runner::run(Workqueue*, const Task*)
   if (this->options_->oformat_enum() != General_options::OBJECT_FORMAT_ELF)
     this->layout_->write_binary(this->of_);

+  if (this->options_->dependency_file())
+    File_read::write_dependency_file(this->options_->dependency_file(),
+                                     this->options_->output_file_name());
+
   this->of_->close();
 }

diff --git a/gold/options.h b/gold/options.h
index b2059d984c..eaf8727ba3 100644
--- a/gold/options.h
+++ b/gold/options.h
@@ -472,7 +472,7 @@ struct Struct_special : public Struct_var
   options::String_set::const_iterator   \
   varname__##_end() const   \
   { return this->varname__##_.value.end(); }                              \
-                                                                          \
+   \
   options::String_set::size_type                                          \
   varname__##_size() const                                                \
   { return this->varname__##_.value.size(); }                             \
@@ -800,6 +800,10 @@ class General_options
        N_("Do not demangle C++ symbols in log messages"),
        NULL);

+  DEFINE_string(dependency_file, options::TWO_DASHES, '\0', NULL,
+ N_("Write a dependency file listing all files read"),
+ N_("FILE"));
+
   DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
        N_("Look for violations of the C++ One Definition Rule"),
        N_("Do not look for violations of the C++ One Definition Rule"));
diff --git a/ld/NEWS b/ld/NEWS
index 485e1cf5b9..f6ff062d0d 100644
--- a/ld/NEWS
+++ b/ld/NEWS
@@ -25,6 +25,10 @@
   searched relative to the directory of the linker script before other search
   paths.

+* Add command-line option --dependency-file to write a Make-style dependency
+  file listing the input files consulted by the linker, like the files written
+  by the compiler's -M -MP options.
+
 Changes in 2.34:

 * The ld check for "PHDR segment not covered by LOAD segment" is more
diff --git a/ld/ld.h b/ld/ld.h
index 71fd781267..1790dc81a6 100644
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -286,6 +286,8 @@ typedef struct
   char *map_filename;
   FILE *map_file;

+  char *dependency_file;
+
   unsigned int split_by_reloc;
   bfd_size_type split_by_file;

diff --git a/ld/ld.texi b/ld/ld.texi
index bf474d4c62..dc22aee799 100644
--- a/ld/ld.texi
+++ b/ld/ld.texi
@@ -893,6 +893,15 @@ Use @var{output} as the name for the program
produced by @command{ld}; if this
 option is not specified, the name @file{a.out} is used by default.  The
 script command @code{OUTPUT} can also specify the output file name.

+@kindex --dependency-file=@var{depfile}
+@cindex dependency file
+@item --dependency-file=@var{depfile}
+Write a @dfn{dependency file} to @var{depfile}.  This file contains a rule
+suitable for @code{make} describing the output file and all the input files
+that were read to produce it.  The output is similar to the compiler's output
+with @samp{-M -MP} (@pxref{Preprocessor Options,, Options Controlling the
+Preprocessor, gcc.info, Using the GNU Compiler Collection}).
+
 @kindex -O @var{level}
 @cindex generating optimized output
 @item -O @var{level}
diff --git a/ld/ldelf.c b/ld/ldelf.c
index 8f2167e889..6fa09cfe6f 100644
--- a/ld/ldelf.c
+++ b/ld/ldelf.c
@@ -262,6 +262,8 @@ ldelf_try_needed (struct dt_needed *needed, int
force, int is_linux)
       return FALSE;
     }

+  track_dependency_files (name);
+
   /* Linker needs to decompress sections.  */
   abfd->flags |= BFD_DECOMPRESS;

@@ -1065,7 +1067,7 @@ ldelf_after_open (int use_libpath, int native,
int is_linux, int is_freebsd,
  }
       return;
     }
-
+
   if (!link_info.traditional_format)
     {
       bfd *elfbfd = NULL;
diff --git a/ld/ldfile.c b/ld/ldfile.c
index 60b28d3f0c..d5f10663eb 100644
--- a/ld/ldfile.c
+++ b/ld/ldfile.c
@@ -142,6 +142,8 @@ ldfile_try_open_bfd (const char *attempt,
       return FALSE;
     }

+  track_dependency_files (attempt);
+
   /* Linker needs to decompress sections.  */
   entry->the_bfd->flags |= BFD_DECOMPRESS;

diff --git a/ld/ldlex.h b/ld/ldlex.h
index 6388247b45..459b237aad 100644
--- a/ld/ldlex.h
+++ b/ld/ldlex.h
@@ -154,6 +154,7 @@ enum option_values
   OPTION_NO_PRINT_MAP_DISCARDED,
   OPTION_NON_CONTIGUOUS_REGIONS,
   OPTION_NON_CONTIGUOUS_REGIONS_WARNINGS,
+  OPTION_DEPENDENCY_FILE,
 };

 /* The initial parser states.  */
diff --git a/ld/ldmain.c b/ld/ldmain.c
index e2c559ea3e..8d777b55c4 100644
--- a/ld/ldmain.c
+++ b/ld/ldmain.c
@@ -159,6 +159,53 @@ static bfd_error_handler_type default_bfd_error_handler;

 struct bfd_link_info link_info;

+struct dependency_file
+{
+  struct dependency_file *next;
+  char *name;
+};
+
+static struct dependency_file *dependency_files, *dependency_files_tail;
+
+void
+track_dependency_files (const char *filename)
+{
+  struct dependency_file *dep
+    = (struct dependency_file *) xmalloc (sizeof (*dep));
+  dep->name = xstrdup (filename);
+  dep->next = NULL;
+  if (dependency_files == NULL)
+    dependency_files = dep;
+  else
+    dependency_files_tail->next = dep;
+  dependency_files_tail = dep;
+}
+
+static void
+write_dependency_file (void)
+{
+  FILE *out;
+  struct dependency_file *dep;
+
+  out = fopen (config.dependency_file, FOPEN_WT);
+  if (out == NULL)
+    {
+      einfo (_("%F%P: cannot open dependency file %s: %E\n"),
+             config.dependency_file);
+    }
+
+  fprintf (out, "%s:", output_filename);
+
+  for (dep = dependency_files; dep != NULL; dep = dep->next)
+    fprintf (out, " \\\n  %s", dep->name);
+
+  fprintf (out, "\n");
+  for (dep = dependency_files; dep != NULL; dep = dep->next)
+    fprintf (out, "\n%s:\n", dep->name);
+
+  fclose (out);
+}
+
 static void
 ld_cleanup (void)
 {
@@ -479,6 +526,9 @@ main (int argc, char **argv)
   ldexp_finish ();
   lang_finish ();

+  if (config.dependency_file != NULL)
+    write_dependency_file ();
+
   /* Even if we're producing relocatable output, some non-fatal errors should
      be reported in the exit status.  (What non-fatal errors, if any, do we
      want to ignore for relocatable output?)  */
diff --git a/ld/ldmain.h b/ld/ldmain.h
index 0f05821d1e..ac7db5720d 100644
--- a/ld/ldmain.h
+++ b/ld/ldmain.h
@@ -58,5 +58,6 @@ extern void add_ysym (const char *);
 extern void add_wrap (const char *);
 extern void add_ignoresym (struct bfd_link_info *, const char *);
 extern void add_keepsyms_file (const char *);
+extern void track_dependency_files (const char *);

 #endif
diff --git a/ld/lexsup.c b/ld/lexsup.c
index d84b334b34..f4e2ba1ee9 100644
--- a/ld/lexsup.c
+++ b/ld/lexsup.c
@@ -111,6 +111,8 @@ static const struct ld_option ld_options[] =
     'c', N_("FILE"), N_("Read MRI format linker script"), TWO_DASHES },
   { {"dc", no_argument, NULL, 'd'},
     'd', NULL, N_("Force common symbols to be defined"), ONE_DASH },
+  { {"dependency-file", required_argument, NULL, OPTION_DEPENDENCY_FILE},
+    '\0', N_("FILE"), N_("Write dependency file"), TWO_DASHES },
   { {"dp", no_argument, NULL, 'd'},
     '\0', NULL, NULL, ONE_DASH },
   { {"force-group-allocation", no_argument, NULL,
@@ -1631,6 +1633,10 @@ parse_args (unsigned argc, char **argv)
  case OPTION_PRINT_MAP_DISCARDED:
    config.print_map_discarded = TRUE;
    break;
+
+        case OPTION_DEPENDENCY_FILE:
+          config.dependency_file = optarg;
+          break;
  }
     }

diff --git a/ld/pe-dll.c b/ld/pe-dll.c
index f72b658341..3e8fe1be9b 100644
--- a/ld/pe-dll.c
+++ b/ld/pe-dll.c
@@ -3344,6 +3344,8 @@ pe_implied_import_dll (const char *filename)
       return FALSE;
     }

+  track_dependency_files (filename);
+
   /* PEI dlls seem to be bfd_objects.  */
   if (!bfd_check_format (dll, bfd_object))
     {


More information about the Binutils mailing list