ELF: Add --strip-debug option.
If --strip-debug option is given, then all sections whose names start
with ".debug" are removed from output.
llvm-svn: 265722
diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h
index 8c48996..2cf9459 100644
--- a/lld/ELF/Config.h
+++ b/lld/ELF/Config.h
@@ -79,6 +79,7 @@
   bool Shared;
   bool Static = false;
   bool StripAll;
+  bool StripDebug;
   bool SysvHash = true;
   bool Threads;
   bool Trace;
diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp
index 72e4bae..612b220 100644
--- a/lld/ELF/Driver.cpp
+++ b/lld/ELF/Driver.cpp
@@ -291,6 +291,7 @@
   Config->SaveTemps = Args.hasArg(OPT_save_temps);
   Config->Shared = Args.hasArg(OPT_shared);
   Config->StripAll = Args.hasArg(OPT_strip_all);
+  Config->StripDebug = Args.hasArg(OPT_strip_debug);
   Config->Threads = Args.hasArg(OPT_threads);
   Config->Trace = Args.hasArg(OPT_trace);
   Config->Verbose = Args.hasArg(OPT_verbose);
@@ -315,11 +316,16 @@
   Config->ZOrigin = hasZOption(Args, "origin");
   Config->ZRelro = !hasZOption(Args, "norelro");
 
-  Config->Pic = Config->Pie || Config->Shared;
-
   if (Config->Relocatable)
     Config->StripAll = false;
 
+  // --strip-all implies --strip-debug.
+  if (Config->StripAll)
+    Config->StripDebug = true;
+
+  // Config->Pic is true if we are generating position-independent code.
+  Config->Pic = Config->Pie || Config->Shared;
+
   if (auto *Arg = Args.getLastArg(OPT_hash_style)) {
     StringRef S = Arg->getValue();
     if (S == "gnu") {
diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 6eb2dc6..1aa0318 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -262,8 +262,13 @@
   if (Name == ".note.GNU-stack")
     return &InputSection<ELFT>::Discarded;
 
-  if (Name == ".note.GNU-split-stack")
+  if (Name == ".note.GNU-split-stack") {
     error("objects using splitstacks are not supported");
+    return &InputSection<ELFT>::Discarded;
+  }
+
+  if (Config->StripDebug && Name.startswith(".debug"))
+    return &InputSection<ELFT>::Discarded;
 
   // A MIPS object file has a special section that contains register
   // usage info, which needs to be handled by the linker specially.
diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td
index b1881f2..1cd250e 100644
--- a/lld/ELF/Options.td
+++ b/lld/ELF/Options.td
@@ -133,6 +133,9 @@
 def strip_all : Flag<["--"], "strip-all">,
   HelpText<"Strip all symbols">;
 
+def strip_debug : Flag<["--"], "strip-debug">,
+  HelpText<"Strip debugging information">;
+
 def sysroot : Joined<["--"], "sysroot=">,
   HelpText<"Set the system root">;