Skip to content

Commit

Permalink
Revert "[llvm-link] use file magic when deciding if input should be l…
Browse files Browse the repository at this point in the history
…oaded as archive"

This reverts commit 55f8c2f.
  • Loading branch information
sndmitriev committed Dec 3, 2020
1 parent 80b0f74 commit 9c955b7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-link/archive-bad.ll
@@ -1,7 +1,7 @@
# RUN: echo -e '!<arch>\nwith invalid contents' > %t.fg.a
# RUN: cp %S/Inputs/f.ll %t.fg.a
# RUN: not llvm-link %S/Inputs/h.ll %t.fg.a -o %t.linked.bc 2>&1 | FileCheck %s

# RUN: rm -f %t.fg.a
# RUN: rm -f %t.linked.bc

# CHECK: truncated or malformed archive
# CHECK: file too small to be an archive
4 changes: 2 additions & 2 deletions llvm/test/tools/llvm-link/archive.ll
@@ -1,8 +1,8 @@
# RUN: llvm-as %S/Inputs/f.ll -o %t.f.bc
# RUN: llvm-as %S/Inputs/g.ll -o %t.g.bc
# RUN: llvm-ar cr %t.fg.a %t.f.bc %t.g.bc
# RUN: llvm-ar cr %t.empty.lib
# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.lib -o %t.linked.bc
# RUN: llvm-ar cr %t.empty.a
# RUN: llvm-link %S/Inputs/h.ll %t.fg.a %t.empty.a -o %t.linked.bc

# RUN: llvm-nm %t.linked.bc | FileCheck %s

Expand Down
34 changes: 14 additions & 20 deletions llvm/tools/llvm-link/llvm-link.cpp
Expand Up @@ -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"
Expand All @@ -24,7 +24,6 @@
#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"
Expand Down Expand Up @@ -116,18 +115,17 @@ static ExitOnError ExitOnErr;
// link path for the specified file to try to find it...
//
static std::unique_ptr<Module> loadFile(const char *argv0,
std::unique_ptr<MemoryBuffer> Buffer,
const std::string &FN,
LLVMContext &Context,
bool MaterializeMetadata = true) {
SMDiagnostic Err;
if (Verbose)
errs() << "Loading '" << Buffer->getBufferIdentifier() << "'\n";
errs() << "Loading '" << FN << "'\n";
std::unique_ptr<Module> Result;
if (DisableLazyLoad)
Result = parseIR(*Buffer, Err, Context);
Result = parseIRFile(FN, Err, Context);
else
Result =
getLazyIRModule(std::move(Buffer), Err, Context, !MaterializeMetadata);
Result = getLazyIRFileModule(FN, Err, Context, !MaterializeMetadata);

if (!Result) {
Err.print(argv0, errs());
Expand All @@ -143,17 +141,19 @@ static std::unique_ptr<Module> loadFile(const char *argv0,
}

static std::unique_ptr<Module> loadArFile(const char *Argv0,
std::unique_ptr<MemoryBuffer> Buffer,
const std::string &ArchiveName,
LLVMContext &Context, Linker &L,
unsigned OrigFlags,
unsigned ApplicableFlags) {
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(*Buffer, Err);
object::Archive Archive(Buf.get()->getMemBufferRef(), Err);
ExitOnErr(std::move(Err));
for (const object::Archive::Child &C : Archive.children(Err)) {
Expected<StringRef> Ename = C.getName();
Expand Down Expand Up @@ -287,9 +287,7 @@ static bool importFunctions(const char *argv0, Module &DestModule) {

auto ModuleLoader = [&DestModule](const char *argv0,
const std::string &Identifier) {
std::unique_ptr<MemoryBuffer> Buffer =
ExitOnErr(errorOrToExpected(MemoryBuffer::getFileOrSTDIN(Identifier)));
return loadFile(argv0, std::move(Buffer), DestModule.getContext(), false);
return loadFile(argv0, Identifier, DestModule.getContext(), false);
};

ModuleLazyLoaderCache ModuleLoaderCache(ModuleLoader);
Expand Down Expand Up @@ -351,14 +349,10 @@ 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 =
identify_magic(Buffer->getBuffer()) == file_magic::archive
? loadArFile(argv0, std::move(Buffer), Context, L, Flags,
ApplicableFlags)
: loadFile(argv0, std::move(Buffer), Context);
(llvm::sys::path::extension(File) == ".a")
? loadArFile(argv0, File, Context, L, Flags, ApplicableFlags)
: loadFile(argv0, File, Context);
if (!M.get()) {
errs() << argv0 << ": ";
WithColor::error() << " loading file '" << File << "'\n";
Expand Down

0 comments on commit 9c955b7

Please sign in to comment.