[LLD] [COFF] Preserve directives and export names from LTO objects (#78802)

The export names are saved as StringRefs pointing into the COFF
directives. In the case of LTO objects, this can be memory allocated
that is owned by the LTO InputFile, which gets destructed when doing the
compilation.

In the case of LTO objects from an older version of LLVM, which require
being upgraded when loaded, the directives string gets destructed, while
when using LTO objects of a matching version (the common case), the
directives string points into memory that doesn't get destructed on LTO
compilation.

Test this by linking a bundled binary LTO object file, from an older
version of LLVM.

This fixes issue #78591, and downstream issue
https://github.com/mstorsjo/llvm-mingw/issues/392.

GitOrigin-RevId: d0986519d58e6d71656019cfa6604efa4bf6d3e7
diff --git a/COFF/InputFiles.cpp b/COFF/InputFiles.cpp
index cd74480..22cc0e3 100644
--- a/COFF/InputFiles.cpp
+++ b/COFF/InputFiles.cpp
@@ -1081,7 +1081,7 @@
     if (objSym.isUsed())
       ctx.config.gcroot.push_back(sym);
   }
-  directives = obj->getCOFFLinkerOpts();
+  directives = saver.save(obj->getCOFFLinkerOpts());
 }
 
 void BitcodeFile::parseLazy() {
diff --git a/test/COFF/Inputs/lto-directives.obj b/test/COFF/Inputs/lto-directives.obj
new file mode 100644
index 0000000..a413b0a
--- /dev/null
+++ b/test/COFF/Inputs/lto-directives.obj
Binary files differ
diff --git a/test/COFF/lto-directives.test b/test/COFF/lto-directives.test
new file mode 100644
index 0000000..5890d42
--- /dev/null
+++ b/test/COFF/lto-directives.test
@@ -0,0 +1,15 @@
+; REQUIRES: x86
+
+;; Test linking an LTO object file that contains directives. The
+;; LTO object file is built with an older toolchain, to force it
+;; to be upgraded when loaded.
+
+;; The input file is compiled from source that looks like this:
+;;   void __declspec(dllexport) entry(void) { }
+;; with this command:
+;;   clang -target x86_64-windows-msvc -c main.c -flto
+
+; RUN: lld-link /entry:entry /subsystem:console %p/Inputs/lto-directives.obj /dll /out:%t.dll
+; RUN: llvm-readobj --coff-exports %t.dll | FileCheck %s
+
+; CHECK: Name: entry