diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp @@ -70,29 +70,33 @@ void RISCVTargetELFStreamer::emitDirectiveOptionNoPIC() {} void RISCVTargetELFStreamer::emitDirectiveOptionRVC() {} void RISCVTargetELFStreamer::emitDirectiveOptionNoRVC() {} -void RISCVTargetELFStreamer::emitDirectiveOptionRelax() {} +void RISCVTargetELFStreamer::emitDirectiveOptionRelax() { + MCAssembler &MCA = getStreamer().getAssembler(); + auto &MAB = static_cast(MCA.getBackend()); + MAB.setForceRelocs(); +} void RISCVTargetELFStreamer::emitDirectiveOptionNoRelax() {} void RISCVTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) { setAttributeItem(Attribute, Value, /*OverwriteExisting=*/true); } void RISCVTargetELFStreamer::emitTextAttribute(unsigned Attribute, StringRef String) { setAttributeItem(Attribute, String, /*OverwriteExisting=*/true); } void RISCVTargetELFStreamer::emitIntTextAttribute(unsigned Attribute, unsigned IntValue, StringRef StringValue) { setAttributeItems(Attribute, IntValue, StringValue, /*OverwriteExisting=*/true); } void RISCVTargetELFStreamer::finishAttributeSection() { if (Contents.empty()) return; if (AttributeSection) { Streamer.SwitchSection(AttributeSection); } else { diff --git a/llvm/test/MC/RISCV/scoped-relaxation.s b/llvm/test/MC/RISCV/scoped-relaxation.s new file mode 100644 --- /dev/null +++ b/llvm/test/MC/RISCV/scoped-relaxation.s @@ -0,0 +1,30 @@ +# RUN: llvm-mc -mattr -relax -triple riscv64-unknown-none-elf -filetype obj %s -o - | llvm-readobj -d -r - | FileCheck %s + +.global function + +# Unrelaxed reference, this would normally fail, but the subsequent scoped +# relaxation forces relaxation on the file. +.dword function - . + +# CHECK: 0x0 R_RISCV_ADD64 function 0x0 +# CHECK: 0x0 R_RISCV_SUB64 - 0x0 + +# Relaxed reference, this will resolve to a pair of `RISCV_ADD64` and +# `RISCV_SUB64` relocation. +.option push +.option relax +.dword function - . +.option pop + +# CHECK: 0x8 R_RISCV_ADD64 function 0x0 +# CHECK: 0x8 R_RISCV_SUB64 - 0x0 + +# Unrelaxed reference, this will resolve to a pair of `RISCV_ADD64` and +# `RISCV_SUB64` relocation due to relaxation being sticky to the file. +.option push +.option norelax +.dword function - . +.option pop + +# CHECK: 0x10 R_RISCV_ADD64 function 0x0 +# CHECK: 0x10 R_RISCV_SUB64 - 0x0