From: Rui Ueyama Date: Wed, 2 Oct 2019 05:24:24 +0000 (+0000) Subject: [llvm-lib] Correctly handle .lib input files X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce1e866ef36206cf0b7ff6a7b437ed749af5d988;p=llvm [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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@373424 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/ToolDrivers/llvm-lib/LibDriver.cpp b/lib/ToolDrivers/llvm-lib/LibDriver.cpp index 18ab6637305..025b8a781d2 100644 --- a/lib/ToolDrivers/llvm-lib/LibDriver.cpp +++ b/lib/ToolDrivers/llvm-lib/LibDriver.cpp @@ -141,6 +141,125 @@ static void doList(opt::InputArgList& Args) { 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(Machine); +} + +static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) { + Expected 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 &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 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 ArgsArr) { BumpPtrAllocator Alloc; StringSaver Saver(Alloc); @@ -195,104 +314,29 @@ int llvm::libDriverMain(ArrayRef ArgsArr) { std::string(" (from '/machine:") + Arg->getValue() + "' flag)"; } - // Create a NewArchiveMember for each input file. + std::vector> MBs; std::vector Members; + + // Create a NewArchiveMember for each input file. for (auto *Arg : Args.filtered(OPT_INPUT)) { + // Find a file std::string Path = findInputFile(Arg->getValue(), SearchPaths); if (Path.empty()) { llvm::errs() << Arg->getValue() << ": no such file or directory\n"; return 1; } - Expected MOrErr = - NewArchiveMember::getFile(Saver.save(Path), /*Deterministic=*/true); - if (!MOrErr) { - handleAllErrors(MOrErr.takeError(), [&](const ErrorInfoBase &EIB) { - llvm::errs() << Arg->getValue() << ": " << EIB.message() << "\n"; - }); - return 1; - } + // Open a file. + ErrorOr> MOrErr = + MemoryBuffer::getFile(Path, -1, false); + fatalOpenError(errorCodeToError(MOrErr.getError()), Path); + MemoryBufferRef MBRef = (*MOrErr)->getMemBufferRef(); - file_magic Magic = identify_magic(MOrErr->Buf->getBuffer()); - if (Magic != file_magic::coff_object && Magic != file_magic::bitcode && - 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. - // 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. - 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(Machine); - } else if (Magic == file_magic::bitcode) { - Expected 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; - } - } + // Append a file. + appendFile(Members, LibMachine, LibMachineSource, MBRef); - Members.emplace_back(std::move(*MOrErr)); + // Take the ownership of the file buffer to keep the file open. + MBs.push_back(std::move(*MOrErr)); } // Create an archive file. diff --git a/test/tools/llvm-lib/invalid.test b/test/tools/llvm-lib/invalid.test index 2978177a431..57266400cdc 100644 --- a/test/tools/llvm-lib/invalid.test +++ b/test/tools/llvm-lib/invalid.test @@ -1,2 +1,2 @@ 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 diff --git a/test/tools/llvm-lib/nest.test b/test/tools/llvm-lib/nest.test new file mode 100644 index 00000000000..627c847b133 --- /dev/null +++ b/test/tools/llvm-lib/nest.test @@ -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