[lld] r197801 - [PECOFF] Create the import library file.
Rui Ueyama
ruiu at google.com
Fri Dec 20 03:35:41 PST 2013
Author: ruiu
Date: Fri Dec 20 05:35:40 2013
New Revision: 197801
URL: http://llvm.org/viewvc/llvm-project?rev=197801&view=rev
Log:
[PECOFF] Create the import library file.
If the linker is instructed to create a DLL, it will also create an import
library (.lib file) to describe the symbols exported by the DLL. This patch is
to create the import library file.
There is a convenient command "lib.exe" which can create an import library
from a module definition file (.def file). The command is used in this patch.
Added:
lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.h
Modified:
lld/trunk/lib/ReaderWriter/PECOFF/CMakeLists.txt
lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
Modified: lld/trunk/lib/ReaderWriter/PECOFF/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/CMakeLists.txt?rev=197801&r1=197800&r2=197801&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/CMakeLists.txt (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/CMakeLists.txt Fri Dec 20 05:35:40 2013
@@ -5,6 +5,7 @@ add_lld_library(lldPECOFF
Pass.cpp
ReaderCOFF.cpp
ReaderImportHeader.cpp
+ WriterImportLibrary.cpp
WriterPECOFF.cpp
)
Added: lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp?rev=197801&view=auto
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp (added)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.cpp Fri Dec 20 05:35:40 2013
@@ -0,0 +1,76 @@
+//===- 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 reponsible for creating the Import Library file.
+///
+//===----------------------------------------------------------------------===//
+
+#include "lld/ReaderWriter/PECOFFLinkingContext.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 {
+
+static std::string getOutputPath(const PECOFFLinkingContext &ctx) {
+ SmallString<128> path = ctx.outputPath();
+ llvm::sys::path::replace_extension(path, ".lib");
+ return path.str();
+}
+
+/// Creates a .def file containing the list of exported symbols.
+static std::string
+createModuleDefinitionFile(const PECOFFLinkingContext &ctx,
+ llvm::FileRemover &fileRemover) {
+ SmallString<128> defFile;
+ int fd;
+ if (llvm::sys::fs::createTemporaryFile("tmp", "def", fd, defFile)) {
+ llvm::errs() << "Failed to create temporary file\n";
+ return "";
+ }
+
+ llvm::raw_fd_ostream os(fd, /*shouldClose*/ true);
+ os << "LIBRARY \"" << llvm::sys::path::filename(ctx.outputPath()) << "\"\n"
+ << "EXPORTS\n";
+
+ for (const PECOFFLinkingContext::ExportDesc &desc : ctx.getDllExports())
+ os << " " << desc.name << " @" << desc.ordinal << "\n";
+ return defFile.str();
+}
+
+/// Creates a .def file and runs lib.exe on it to create an import library.
+void writeImportLibrary(const PECOFFLinkingContext &ctx) {
+ std::string program = "lib.exe";
+ std::string programPath = llvm::sys::FindProgramByName(program);
+ if (programPath.empty()) {
+ llvm::errs() << "Unable to find " << program << " in PATH\n";
+ return;
+ }
+
+ llvm::FileRemover tmpFile;
+ std::string defArg = "/def:";
+ defArg.append(createModuleDefinitionFile(ctx, tmpFile));
+ std::string outputArg = "/out:";
+ outputArg.append(getOutputPath(ctx));
+
+ std::vector<const char *> args;
+ args.push_back(programPath.c_str());
+ args.push_back("/nologo");
+ args.push_back("/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";
+}
+
+} // end namespace pecoff
+} // end namespace lld
Added: lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.h?rev=197801&view=auto
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.h (added)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterImportLibrary.h Fri Dec 20 05:35:40 2013
@@ -0,0 +1,23 @@
+//===- lib/ReaderWriter/PECOFF/WriterImportLibrary.h ----------------------===//
+//
+// The LLVM Linker
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLD_READER_WRITER_PE_COFF_WRITER_IMPORT_LIBRARY_H
+#define LLD_READER_WRITER_PE_COFF_WRITER_IMPORT_LIBRARY_H
+
+namespace lld {
+class PECOFFLinkingContext;
+
+namespace pecoff {
+
+void writeImportLibrary(const PECOFFLinkingContext &ctx);
+
+} // end namespace pecoff
+} // end namespace lld
+
+#endif
Modified: lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp?rev=197801&r1=197800&r2=197801&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/WriterPECOFF.cpp Fri Dec 20 05:35:40 2013
@@ -27,6 +27,7 @@
#include <vector>
#include "Atoms.h"
+#include "WriterImportLibrary.h"
#include "lld/Core/DefinedAtom.h"
#include "lld/Core/File.h"
@@ -909,6 +910,10 @@ error_code PECOFFWriter::writeFile(const
chunk->write(buffer->getBufferStart() + chunk->fileOffset());
applyAllRelocations(buffer->getBufferStart());
DEBUG(printAllAtomAddresses());
+
+ if (_PECOFFLinkingContext.getImageType() == PECOFFLinkingContext::IMAGE_DLL)
+ writeImportLibrary(_PECOFFLinkingContext);
+
return buffer->commit();
}
More information about the llvm-commits
mailing list