From 1840901a2ff8493ca60579298be97eda8fadbcec Mon Sep 17 00:00:00 2001 From: George Rimar Date: Fri, 9 Jun 2017 12:26:57 +0000 Subject: [PATCH] [ELF] - Allow producing -r output if only empty archive is given. This is used by linux kernel build system. (https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt "3.2 Built-in object goals") It has for example next configuration for linking built-in.o files: drivers-y := $(patsubst %/, %/built-in.o, $(drivers-y)) drivers-$(CONFIG_PCI) += arch/ia64/pci/ ... drivers-$(CONFIG_OPROFILE) += arch/ia64/oprofile/ Im most simple case all CONFIG_* options are off. That means linker is called with empty input archive, emulation option and no inputs and expected to generate some relocatable output. ld.bfd is able to do that, we dont. Patch allows to support this case. Differential revision: https://reviews.llvm.org/D33937 llvm-svn: 305069 --- lld/ELF/Driver.cpp | 2 +- lld/test/ELF/relocatable-empty-archive.s | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 lld/test/ELF/relocatable-empty-archive.s diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp index 4e72f7992343..f3943b5cf655 100644 --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -185,7 +185,7 @@ void LinkerDriver::addFile(StringRef Path, bool WithLOption) { // is attempting LTO and using a default ar command that doesn't // understand the LLVM bitcode file. It is a pretty common error, so // we'll handle it as if it had a symbol table. - if (!File->hasSymbolTable()) { + if (!File->isEmpty() && !File->hasSymbolTable()) { for (const auto &P : getArchiveMembers(MBRef)) Files.push_back(make(P.first, Path, P.second)); return; diff --git a/lld/test/ELF/relocatable-empty-archive.s b/lld/test/ELF/relocatable-empty-archive.s new file mode 100644 index 000000000000..5510b8a25cd1 --- /dev/null +++ b/lld/test/ELF/relocatable-empty-archive.s @@ -0,0 +1,10 @@ +# REQUIRES: x86 +# RUN: rm %t.a +# RUN: llvm-ar rc %t.a +# RUN: ld.lld -m elf_x86_64 %t.a -o %t -r +# RUN: llvm-readobj -file-headers %t | FileCheck %s + +# CHECK: Format: ELF64-x86-64 +# CHECK: Arch: x86_64 +# CHECK: AddressSize: 64bit +# CHECK: Type: Relocatable