Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -67,8 +67,12 @@ case MipsReginfo: case MipsOptions: // MIPS .reginfo and .MIPS.options sections are consumed by the linker, - // so they should never be copied to output. - llvm_unreachable("MIPS reginfo/options section reached writeTo()."); + // and the linker produces a single output section. It is possible that + // input files contain section symbol points to the corresponding input + // section. Redirect it to the produced output section. + assert(Offset == 0 && + "Retrieve offset in the middle of MIPS reginfo/options section"); + return this->OutSec->getVA(); } llvm_unreachable("invalid section kind"); } Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -1582,6 +1582,7 @@ // Copy input object file's .reginfo gprmask to output. auto *S = cast>(C); GprMask |= S->Reginfo->ri_gprmask; + S->OutSec = this; } template @@ -1610,6 +1611,7 @@ auto *S = cast>(C); if (S->Reginfo) GprMask |= S->Reginfo->ri_gprmask; + S->OutSec = this; } namespace lld { Index: test/ELF/mips-options-r.test =================================================================== --- /dev/null +++ test/ELF/mips-options-r.test @@ -0,0 +1,19 @@ +# Check that if input file contains .MIPS.options section and symbol +# points to the section and the linker generates a relocatable output, +# LLD does not crash and write section symbols point to the output +# .MIPS.options section. +# +# PR 27878 +# +# Input object file created using the following script: +# % cat t.s +# .text +# nop +# % as -mabi=64 -mips64r2 t.s + +# RUN: ld.lld -r %p/Inputs/mips-options.o -o %t.o +# RUN: llvm-readobj -t %t.o | FileCheck %s + +# REQUIRES: mips + +# CHECK: Section: .MIPS.options