Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LLD crash in lld::elf::SectionBase::getOutputSection() #1186

Closed
samitolvanen opened this issue Oct 24, 2020 · 3 comments
Closed

LLD crash in lld::elf::SectionBase::getOutputSection() #1186

samitolvanen opened this issue Oct 24, 2020 · 3 comments

Comments

@samitolvanen
Copy link
Member

@samitolvanen samitolvanen commented Oct 24, 2020

When I build an arm64 kernel with LTO and dynamic ftrace, and additionally disable LTO for the arch/arm64/kvm/hyp/vhe directory, LLD crashes in lld::elf::SectionBase::getOutputSection() when linking vmlinux.o:

  LTO     vmlinux.o
PLEASE submit a bug report to https://bugs.llvm.org/ and include the crash backtrace.
Stack dump:
0.      Program arguments: ld.lld -EL -maarch64elf --thinlto-cache-dir=.thinlto-cache -mllvm -import-instr-limit=5 -r -o vmlinux.o -T .tmp_initcalls.lds --whole-archive arch/arm64/kernel/head.o init/built-in.a usr/built-in.a arch/arm64/built-in.a kernel/built-in.a certs/built-in.a mm/built-in.a fs/built-in.a ipc/built-in.a security/built-in.a crypto/built-in.a block/built-in.a arch/arm64/lib/built-in.a lib/built-in.a arch/arm64/lib/lib.a lib/lib.a drivers/built-in.a sound/built-in.a net/built-in.a virt/built-in.a --no-whole-archive --start-group ./drivers/firmware/efi/libstub/lib.a --end-group 
 #0 0x000000000125f323 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x125f323)
 #1 0x000000000125d15e llvm::sys::RunSignalHandlers() (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x125d15e)
 #2 0x000000000125faac SignalHandler(int) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x125faac)
 #3 0x00007f00c3f01140 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14140)
 #4 0x00000000013b8433 lld::elf::SectionBase::getOutputSection() (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x13b8433)
 #5 0x00000000013e3268 std::_Function_handler<void (lld::elf::InputSectionBase*), lld::elf::LinkerScript::addOrphanSections()::$_3>::_M_invoke(std::_Any_data const&, lld::elf::InputSectionBase*&&) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x13e3268)
 #6 0x00000000013e2cb6 std::_Function_handler<void (lld::elf::InputSectionBase*), lld::elf::LinkerScript::addOrphanSections()::$_3>::_M_invoke(std::_Any_data const&, lld::elf::InputSectionBase*&&) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x13e2cb6)
 #7 0x00000000013da79b lld::elf::LinkerScript::addOrphanSections() (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x13da79b)
 #8 0x0000000001343ad4 void lld::elf::LinkerDriver::link<llvm::object::ELFType<(llvm::support::endianness)1, true> >(llvm::opt::InputArgList&) (unified-llvm/build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x1343ad4)
 #9 0x00000000013349f8 lld::elf::LinkerDriver::main(llvm::ArrayRef<char const*>) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x13349f8)
#10 0x0000000001332244 lld::elf::link(llvm::ArrayRef<char const*>, bool, llvm::raw_ostream&, llvm::raw_ostream&) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x1332244)
#11 0x00000000011c7b43 lldMain(int, char const**, llvm::raw_ostream&, llvm::raw_ostream&, bool) (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x11c7b43)
#12 0x00000000011c7323 main (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x11c7323)
#13 0x00007f00c380bcca __libc_start_main ./csu/../csu/libc-start.c:308:16
#14 0x00000000011c702a _start (build.9068c209655efc597b31b23fc41630d82c5b98a4/bin/lld+0x11c702a)

Here's the kernel patch that I applied to the LTO tree that triggers the crash:

diff --git a/arch/arm64/kvm/hyp/vhe/Makefile b/arch/arm64/kvm/hyp/vhe/Makefile
index 461e97c375cc..e5c875da0699 100644
--- a/arch/arm64/kvm/hyp/vhe/Makefile
+++ b/arch/arm64/kvm/hyp/vhe/Makefile
@@ -9,3 +9,5 @@ ccflags-y := -D__KVM_VHE_HYPERVISOR__
 obj-y := timer-sr.o sysreg-sr.o debug-sr.o switch.o tlb.o
 obj-y += ../vgic-v3-sr.o ../aarch32.o ../vgic-v2-cpuif-proxy.o ../entry.o \
         ../fpsimd.o ../hyp-entry.o
+
+KBUILD_CFLAGS := $(filter-out $(CC_FLAGS_LTO), $(KBUILD_CFLAGS))

And here's how I configured and built the kernel:

$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LLVM=1 defconfig
$ ./scripts/config -e LTO_CLANG -e THINLTO -d GCOV_KERNEL -d KASAN -e FUNCTION_TRACER
 -e DYNAMIC_FTRACE -e FTRACE -e TRACING
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LLVM=1 olddefconfig
$ make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- LLVM=1 -j110

I can reproduce this crash with both LLVM 11 and ToT LLVM, and even if I disable LTO for everything in the arch/arm64/kvm/hyp directory.

@nickdesaulniers @MaskRay any ideas what goes wrong here and how I can help debug the issue?

@MaskRay
Copy link
Member

@MaskRay MaskRay commented Oct 26, 2020

Do I need to apply some patches to reproduce? THINLTO is not in master yet. Alternatively, if you have a reproduce file (rerun the link command with --reproduce=/tmp/rep.tar), I can take a look.

@MaskRay
Copy link
Member

@MaskRay MaskRay commented Oct 26, 2020

@samitolvanen shared a reproduce file with me.

lld/ELF/LinkerScript.cpp

    if (config->relocatable && (isec->flags & SHF_LINK_ORDER)) {
      // Merging two SHF_LINK_ORDER sections with different sh_link fields will
      // change their semantics, so we only merge them in -r links if they will
      // end up being linked to the same output section. The casts are fine
      // because everything in the map was created by the orphan placement code.
      auto *firstIsec = cast<InputSectionBase>(
          cast<InputSectionDescription>(sec->sectionCommands[0])
              ->sectionBases[0]);
/// If firstIsec does not have the SHF_LINK_ORDER flag, this triggers an assertion error
      if (firstIsec->getLinkOrderDep()->getOutputSection() !=
          isec->getLinkOrderDep()->getOutputSection())
        continue;
    }

Such a non-SHF_LINK_ORDER __patchable_function_entries section can be produced by clang -fno-integrated-as -fpatchable-function-entry=N[,M]. I'll fix the LLD issue.

@MaskRay
Copy link
Member

@MaskRay MaskRay commented Oct 27, 2020

@MaskRay MaskRay self-assigned this Oct 27, 2020
github-actions bot pushed a commit to tstellar/llvm-project that referenced this issue Oct 28, 2020
…e a SHF_LINK_ORDER orphan

Fixes ClangBuiltLinux/linux#1186

If a non-SHF_LINK_ORDER orphan is added first, `firstIsec->flags & SHF_LINK_ORDER`
will be zero and we currently assert when calling `getLinkOrderDep`.

Reviewed By: grimar

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

(cherry picked from commit ae73091)
tstellar added a commit to tstellar/llvm-project that referenced this issue Oct 31, 2020
…e a SHF_LINK_ORDER orphan

Fixes ClangBuiltLinux/linux#1186

If a non-SHF_LINK_ORDER orphan is added first, `firstIsec->flags & SHF_LINK_ORDER`
will be zero and we currently assert when calling `getLinkOrderDep`.

Reviewed By: grimar

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

(cherry picked from commit ae73091)
arichardson added a commit to arichardson/llvm-project that referenced this issue Mar 25, 2021
…e a SHF_LINK_ORDER orphan

Fixes ClangBuiltLinux/linux#1186

If a non-SHF_LINK_ORDER orphan is added first, `firstIsec->flags & SHF_LINK_ORDER`
will be zero and we currently assert when calling `getLinkOrderDep`.

Reviewed By: grimar

Differential Revision: https://reviews.llvm.org/D90200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants