summaryrefslogtreecommitdiff
path: root/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2015-03-24 21:31:36 +0000
committerDimitry Andric <dim@FreeBSD.org>2015-03-24 21:31:36 +0000
commitfb911942f1434f3d1750f83f25f5e42c80e60638 (patch)
tree1678c4a4f0182e4029a86d135aa4a1b7d09e3c41 /lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
Notes
Diffstat (limited to 'lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp')
-rw-r--r--lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp118
1 files changed, 118 insertions, 0 deletions
diff --git a/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp b/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
new file mode 100644
index 000000000000..fd3360f018b6
--- /dev/null
+++ b/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
@@ -0,0 +1,118 @@
+//===- lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp --------------------===//
+//
+// The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+///
+/// This file is responsible for creating the Import Library file.
+///
+//===----------------------------------------------------------------------===//
+
+#include "lld/ReaderWriter/PECOFFLinkingContext.h"
+#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/FileUtilities.h"
+#include "llvm/Support/Path.h"
+#include "llvm/Support/Program.h"
+#include "llvm/Support/raw_ostream.h"
+
+namespace lld {
+namespace pecoff {
+
+/// Creates a .def file containing the list of exported symbols.
+static std::string
+createModuleDefinitionFile(const PECOFFLinkingContext &ctx) {
+ std::string ret;
+ llvm::raw_string_ostream os(ret);
+ os << "LIBRARY \"" << llvm::sys::path::filename(ctx.outputPath()) << "\"\n"
+ << "EXPORTS\n";
+
+ for (const PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports()) {
+ // Symbol names in a module-definition file will be mangled by lib.exe,
+ // so we need to demangle them before writing to a .def file.
+ os << " ";
+ if (!desc.externalName.empty()) {
+ os << desc.externalName;
+ } else if (!desc.mangledName.empty()) {
+ os << ctx.undecorateSymbol(desc.mangledName);
+ } else {
+ os << ctx.undecorateSymbol(desc.name);
+ }
+
+ if (!desc.isPrivate)
+ os << " @" << desc.ordinal;
+ if (desc.noname)
+ os << " NONAME";
+ if (desc.isData)
+ os << " DATA";
+ if (desc.isPrivate)
+ os << " PRIVATE";
+ os << "\n";
+ }
+ os.flush();
+ return ret;
+}
+
+static std::string writeToTempFile(StringRef contents) {
+ SmallString<128> path;
+ int fd;
+ if (llvm::sys::fs::createTemporaryFile("tmp", "def", fd, path)) {
+ llvm::errs() << "Failed to create temporary file\n";
+ return "";
+ }
+ llvm::raw_fd_ostream os(fd, /*shouldClose*/ true);
+ os << contents;
+ return path.str();
+}
+
+static void writeTo(StringRef path, StringRef contents) {
+ int fd;
+ if (llvm::sys::fs::openFileForWrite(path, fd, llvm::sys::fs::F_Text)) {
+ llvm::errs() << "Failed to open " << path << "\n";
+ return;
+ }
+ llvm::raw_fd_ostream os(fd, /*shouldClose*/ true);
+ os << contents;
+}
+
+/// Creates a .def file and runs lib.exe on it to create an import library.
+void writeImportLibrary(const PECOFFLinkingContext &ctx) {
+ std::string fileContents = createModuleDefinitionFile(ctx);
+
+ std::string program = "lib.exe";
+ ErrorOr<std::string> programPathOrErr = llvm::sys::findProgramByName(program);
+ if (!programPathOrErr) {
+ llvm::errs() << "Unable to find " << program << " in PATH\n";
+ } else {
+ const std::string &programPath = *programPathOrErr;
+
+ std::string defPath = writeToTempFile(fileContents);
+ llvm::FileRemover tmpFile(defPath);
+
+ std::string defArg = "/def:";
+ defArg.append(defPath);
+ std::string outputArg = "/out:";
+ outputArg.append(ctx.getOutputImportLibraryPath());
+
+ std::vector<const char *> args;
+ args.push_back(programPath.c_str());
+ args.push_back("/nologo");
+ args.push_back(ctx.is64Bit() ? "/machine:x64" : "/machine:x86");
+ args.push_back(defArg.c_str());
+ args.push_back(outputArg.c_str());
+ args.push_back(nullptr);
+
+ if (llvm::sys::ExecuteAndWait(programPath.c_str(), &args[0]) != 0)
+ llvm::errs() << program << " failed\n";
+ }
+
+ // If /lldmoduledeffile:<filename> is given, make a copy of the
+ // temporary module definition file. This feature is for unit tests.
+ if (!ctx.getModuleDefinitionFile().empty())
+ writeTo(ctx.getModuleDefinitionFile(), fileContents);
+}
+
+} // end namespace pecoff
+} // end namespace lld