[llvm-lib] Correctly handle .lib input files

If archive files are passed as input files, llvm-lib needs to append
the members of the input archive files to the output file. This patch
implements that behavior.

This patch splits an existing function into smaller functions.
Effectively, the new code is only `if (Magic == file_magic::archive)
{ ... }` part.

Fixes https://bugs.llvm.org/show_bug.cgi?id=32674

Differential Revision: https://reviews.llvm.org/D68204

llvm-svn: 373424
This commit is contained in:
Rui Ueyama 2019-10-02 05:24:24 +00:00
parent 8d6a863b02
commit 64a362e721
3 changed files with 148 additions and 89 deletions

View File

@ -141,6 +141,125 @@ static void doList(opt::InputArgList& Args) {
fatalOpenError(std::move(Err), B->getBufferIdentifier()); fatalOpenError(std::move(Err), B->getBufferIdentifier());
} }
static COFF::MachineTypes getCOFFFileMachine(MemoryBufferRef MB) {
std::error_code EC;
object::COFFObjectFile Obj(MB, EC);
if (EC) {
llvm::errs() << MB.getBufferIdentifier()
<< ": failed to open: " << EC.message() << '\n';
exit(1);
}
uint16_t Machine = Obj.getMachine();
if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
llvm::errs() << MB.getBufferIdentifier() << ": unknown machine: " << Machine
<< '\n';
exit(1);
}
return static_cast<COFF::MachineTypes>(Machine);
}
static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) {
Expected<std::string> TripleStr = getBitcodeTargetTriple(MB);
if (!TripleStr) {
llvm::errs() << MB.getBufferIdentifier()
<< ": failed to get target triple from bitcode\n";
exit(1);
}
switch (Triple(*TripleStr).getArch()) {
case Triple::x86:
return COFF::IMAGE_FILE_MACHINE_I386;
case Triple::x86_64:
return COFF::IMAGE_FILE_MACHINE_AMD64;
case Triple::arm:
return COFF::IMAGE_FILE_MACHINE_ARMNT;
case Triple::aarch64:
return COFF::IMAGE_FILE_MACHINE_ARM64;
default:
llvm::errs() << MB.getBufferIdentifier()
<< ": unknown arch in target triple " << *TripleStr << '\n';
exit(1);
}
}
static void appendFile(std::vector<NewArchiveMember> &Members,
COFF::MachineTypes &LibMachine,
std::string &LibMachineSource, MemoryBufferRef MB) {
file_magic Magic = identify_magic(MB.getBuffer());
if (Magic != file_magic::coff_object && Magic != file_magic::bitcode &&
Magic != file_magic::archive && Magic != file_magic::windows_resource) {
llvm::errs() << MB.getBufferIdentifier()
<< ": not a COFF object, bitcode, archive or resource file\n";
exit(1);
}
// If a user attempts to add an archive to another archive, llvm-lib doesn't
// handle the first archive file as a single file. Instead, it extracts all
// members from the archive and add them to the second archive. This beahvior
// is for compatibility with Microsoft's lib command.
if (Magic == file_magic::archive) {
Error Err = Error::success();
object::Archive Archive(MB, Err);
fatalOpenError(std::move(Err), MB.getBufferIdentifier());
for (auto &C : Archive.children(Err)) {
Expected<MemoryBufferRef> ChildMB = C.getMemoryBufferRef();
if (!ChildMB) {
handleAllErrors(ChildMB.takeError(), [&](const ErrorInfoBase &EIB) {
llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
<< "\n";
});
exit(1);
}
appendFile(Members, LibMachine, LibMachineSource, *ChildMB);
}
fatalOpenError(std::move(Err), MB.getBufferIdentifier());
return;
}
// Check that all input files have the same machine type.
// Mixing normal objects and LTO bitcode files is fine as long as they
// have the same machine type.
// Doing this here duplicates the header parsing work that writeArchive()
// below does, but it's not a lot of work and it's a bit awkward to do
// in writeArchive() which needs to support many tools, can't assume the
// input is COFF, and doesn't have a good way to report errors.
if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object)
? getCOFFFileMachine(MB)
: getBitcodeFileMachine(MB);
// FIXME: Once lld-link rejects multiple resource .obj files:
// Call convertResToCOFF() on .res files and add the resulting
// COFF file to the .lib output instead of adding the .res file, and remove
// this check. See PR42180.
if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
LibMachine = FileMachine;
LibMachineSource =
(" (inferred from earlier file '" + MB.getBufferIdentifier() + "')")
.str();
} else if (LibMachine != FileMachine) {
llvm::errs() << MB.getBufferIdentifier() << ": file machine type "
<< machineToStr(FileMachine)
<< " conflicts with library machine type "
<< machineToStr(LibMachine) << LibMachineSource << '\n';
exit(1);
}
}
}
Members.emplace_back(MB);
}
int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) { int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
BumpPtrAllocator Alloc; BumpPtrAllocator Alloc;
StringSaver Saver(Alloc); StringSaver Saver(Alloc);
@ -195,104 +314,29 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {
std::string(" (from '/machine:") + Arg->getValue() + "' flag)"; std::string(" (from '/machine:") + Arg->getValue() + "' flag)";
} }
// Create a NewArchiveMember for each input file. std::vector<std::unique_ptr<MemoryBuffer>> MBs;
std::vector<NewArchiveMember> Members; std::vector<NewArchiveMember> Members;
// Create a NewArchiveMember for each input file.
for (auto *Arg : Args.filtered(OPT_INPUT)) { for (auto *Arg : Args.filtered(OPT_INPUT)) {
// Find a file
std::string Path = findInputFile(Arg->getValue(), SearchPaths); std::string Path = findInputFile(Arg->getValue(), SearchPaths);
if (Path.empty()) { if (Path.empty()) {
llvm::errs() << Arg->getValue() << ": no such file or directory\n"; llvm::errs() << Arg->getValue() << ": no such file or directory\n";
return 1; return 1;
} }
Expected<NewArchiveMember> MOrErr = // Open a file.
NewArchiveMember::getFile(Saver.save(Path), /*Deterministic=*/true); ErrorOr<std::unique_ptr<MemoryBuffer>> MOrErr =
if (!MOrErr) { MemoryBuffer::getFile(Path, -1, false);
handleAllErrors(MOrErr.takeError(), [&](const ErrorInfoBase &EIB) { fatalOpenError(errorCodeToError(MOrErr.getError()), Path);
llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n"; MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef();
});
return 1;
}
file_magic Magic = identify_magic(MOrErr->Buf->getBuffer()); // Append a file.
if (Magic != file_magic::coff_object && Magic != file_magic::bitcode && appendFile(Members, LibMachine, LibMachineSource, MBRef);
Magic != file_magic::windows_resource) {
llvm::errs() << Arg->getValue()
<< ": not a COFF object, bitcode or resource file\n";
return 1;
}
// Check that all input files have the same machine type. // Take the ownership of the file buffer to keep the file open.
// Mixing normal objects and LTO bitcode files is fine as long as they MBs.push_back(std::move(*MOrErr));
// have the same machine type.
// Doing this here duplicates the header parsing work that writeArchive()
// below does, but it's not a lot of work and it's a bit awkward to do
// in writeArchive() which needs to support many tools, can't assume the
// input is COFF, and doesn't have a good way to report errors.
COFF::MachineTypes FileMachine = COFF::IMAGE_FILE_MACHINE_UNKNOWN;
if (Magic == file_magic::coff_object) {
std::error_code EC;
object::COFFObjectFile Obj(*MOrErr->Buf, EC);
if (EC) {
llvm::errs() << Arg->getValue() << ": failed to open: " << EC.message()
<< '\n';
return 1;
}
uint16_t Machine = Obj.getMachine();
if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
llvm::errs() << Arg->getValue() << ": unknown machine: " << Machine
<< '\n';
return 1;
}
FileMachine = static_cast<COFF::MachineTypes>(Machine);
} else if (Magic == file_magic::bitcode) {
Expected<std::string> TripleStr = getBitcodeTargetTriple(*MOrErr->Buf);
if (!TripleStr) {
llvm::errs() << Arg->getValue()
<< ": failed to get target triple from bitcode\n";
return 1;
}
switch (Triple(*TripleStr).getArch()) {
case Triple::x86:
FileMachine = COFF::IMAGE_FILE_MACHINE_I386;
break;
case Triple::x86_64:
FileMachine = COFF::IMAGE_FILE_MACHINE_AMD64;
break;
case Triple::arm:
FileMachine = COFF::IMAGE_FILE_MACHINE_ARMNT;
break;
case Triple::aarch64:
FileMachine = COFF::IMAGE_FILE_MACHINE_ARM64;
break;
default:
llvm::errs() << Arg->getValue() << ": unknown arch in target triple "
<< *TripleStr << '\n';
return 1;
}
}
// FIXME: Once lld-link rejects multiple resource .obj files:
// Call convertResToCOFF() on .res files and add the resulting
// COFF file to the .lib output instead of adding the .res file, and remove
// this check. See PR42180.
if (FileMachine != COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
if (LibMachine == COFF::IMAGE_FILE_MACHINE_UNKNOWN) {
LibMachine = FileMachine;
LibMachineSource = std::string(" (inferred from earlier file '") +
Arg->getValue() + "')";
} else if (LibMachine != FileMachine) {
llvm::errs() << Arg->getValue() << ": file machine type "
<< machineToStr(FileMachine)
<< " conflicts with library machine type "
<< machineToStr(LibMachine) << LibMachineSource << '\n';
return 1;
}
}
Members.emplace_back(std::move(*MOrErr));
} }
// Create an archive file. // Create an archive file.

View File

@ -1,2 +1,2 @@
RUN: not llvm-lib %S/Inputs/cl-gl.obj 2>&1 | FileCheck %s RUN: not llvm-lib %S/Inputs/cl-gl.obj 2>&1 | FileCheck %s
CHECK: not a COFF object, bitcode or resource file CHECK: not a COFF object, bitcode, archive or resource file

View File

@ -0,0 +1,15 @@
If an archive file is specified as an input file, its members
are added to an output file. This test verifies that beahvior.
RUN: rm -rf %t
RUN: mkdir -p %t
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/foo.o %S/Inputs/a.s
RUN: llvm-lib -out:%t/foo.lib %t/foo.o
RUN: llvm-mc -triple=x86_64-pc-windows-msvc -filetype=obj -o %t/bar.o %S/Inputs/b.s
RUN: llvm-lib -out:%t/bar.lib %t/foo.lib %t/bar.o
RUN: llvm-ar t %t/bar.lib | FileCheck %s
CHECK: foo.o
CHECK: bar.o