summaryrefslogtreecommitdiff
path: root/llvm/tools/llvm-link/llvm-link.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'llvm/tools/llvm-link/llvm-link.cpp')
-rw-r--r--llvm/tools/llvm-link/llvm-link.cpp53
1 files changed, 31 insertions, 22 deletions
diff --git a/llvm/tools/llvm-link/llvm-link.cpp b/llvm/tools/llvm-link/llvm-link.cpp
index 7141bd1ca7a1..eed49c438335 100644
--- a/llvm/tools/llvm-link/llvm-link.cpp
+++ b/llvm/tools/llvm-link/llvm-link.cpp
@@ -11,8 +11,8 @@
//
//===----------------------------------------------------------------------===//
-#include "llvm/Object/Archive.h"
#include "llvm/ADT/STLExtras.h"
+#include "llvm/BinaryFormat/Magic.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/AutoUpgrade.h"
@@ -24,6 +24,7 @@
#include "llvm/IR/Verifier.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Linker/Linker.h"
+#include "llvm/Object/Archive.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
@@ -115,17 +116,18 @@ static ExitOnError ExitOnErr;
// link path for the specified file to try to find it...
//
static std::unique_ptr<Module> loadFile(const char *argv0,
- const std::string &FN,
+ std::unique_ptr<MemoryBuffer> Buffer,
LLVMContext &Context,
bool MaterializeMetadata = true) {
SMDiagnostic Err;
if (Verbose)
- errs() << "Loading '" << FN << "'\n";
+ errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
std::unique_ptr<Module> Result;
if (DisableLazyLoad)
- Result = parseIRFile(FN, Err, Context);
+ Result = parseIR(*Buffer, Err, Context);
else
- Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);
+ Result =
+ getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
if (!Result) {
Err.print(argv0, errs());
@@ -141,20 +143,17 @@ static std::unique_ptr<Module> loadFile(const char *argv0,
}
static std::unique_ptr<Module> loadArFile(const char *Argv0,
- const std::string &ArchiveName,
- LLVMContext &Context, Linker &L,
- unsigned OrigFlags,
- unsigned ApplicableFlags) {
+ std::unique_ptr<MemoryBuffer> Buffer,
+ LLVMContext &Context) {
std::unique_ptr<Module> Result(new Module("ArchiveModule", Context));
+ StringRef ArchiveName = Buffer->getBufferIdentifier();
if (Verbose)
errs() << "Reading library archive file '" << ArchiveName
<< "' to memory\n";
- ErrorOr<std::unique_ptr<MemoryBuffer>> Buf =
- MemoryBuffer::getFile(ArchiveName, -1, false);
- ExitOnErr(errorCodeToError(Buf.getError()));
Error Err = Error::success();
- object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
+ object::Archive Archive(*Buffer, Err);
ExitOnErr(std::move(Err));
+ Linker L(*Result);
for (const object::Archive::Child &C : Archive.children(Err)) {
Expected<StringRef> Ename = C.getName();
if (Error E = Ename.takeError()) {
@@ -163,7 +162,7 @@ static std::unique_ptr<Module> loadArFile(const char *Argv0,
<< " failed to read name of archive member"
<< ArchiveName << "'\n";
return nullptr;
- };
+ }
std::string ChildName = Ename.get().str();
if (Verbose)
errs() << "Parsing member '" << ChildName
@@ -188,7 +187,12 @@ static std::unique_ptr<Module> loadArFile(const char *Argv0,
return nullptr;
}
- std::unique_ptr<Module> M = parseIR(MemBuf.get(), ParseErr, Context);
+ std::unique_ptr<Module> M;
+ if (DisableLazyLoad)
+ M = parseIR(MemBuf.get(), ParseErr, Context);
+ else
+ M = getLazyIRModule(MemoryBuffer::getMemBuffer(MemBuf.get(), false),
+ ParseErr, Context);
if (!M.get()) {
errs() << Argv0 << ": ";
@@ -199,9 +203,8 @@ static std::unique_ptr<Module> loadArFile(const char *Argv0,
}
if (Verbose)
errs() << "Linking member '" << ChildName << "' of archive library.\n";
- if (L.linkModules(*Result, std::move(M), ApplicableFlags))
+ if (L.linkInModule(std::move(M)))
return nullptr;
- ApplicableFlags = OrigFlags;
} // end for each child
ExitOnErr(std::move(Err));
return Result;
@@ -287,7 +290,9 @@ static bool importFunctions(const char *argv0, Module &DestModule) {
auto ModuleLoader = [&DestModule](const char *argv0,
const std::string &Identifier) {
- return loadFile(argv0, Identifier, DestModule.getContext(), false);
+ std::unique_ptr<MemoryBuffer> Buffer =
+ ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Identifier)));
+ return loadFile(argv0, std::move(Buffer), DestModule.getContext(), false);
};
ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
@@ -349,10 +354,13 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
// Similar to some flags, internalization doesn't apply to the first file.
bool InternalizeLinkedSymbols = false;
for (const auto &File : Files) {
+ std::unique_ptr<MemoryBuffer> Buffer =
+ ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(File)));
+
std::unique_ptr<Module> M =
- (llvm::sys::path::extension(File) == ".a")
- ? loadArFile(argv0, File, Context, L, Flags, ApplicableFlags)
- : loadFile(argv0, File, Context);
+ identify_magic(Buffer->getBuffer()) == file_magic::archive
+ ? loadArFile(argv0, std::move(Buffer), Context)
+ : loadFile(argv0, std::move(Buffer), Context);
if (!M.get()) {
errs() << argv0 << ": ";
WithColor::error() << " loading file '" << File << "'\n";
@@ -454,7 +462,8 @@ int main(int argc, char **argv) {
errs() << "Here's the assembly:\n" << *Composite;
std::error_code EC;
- ToolOutputFile Out(OutputFilename, EC, sys::fs::OF_None);
+ ToolOutputFile Out(OutputFilename, EC,
+ OutputAssembly ? sys::fs::OF_Text : sys::fs::OF_None);
if (EC) {
WithColor::error() << EC.message() << '\n';
return 1;