[RISCV] Force relocations if initial MCSubtargetInfo contains FeatureRelax (#77436)
commit6c207ee5d20d2b054509123e6d0507df1332b376
authorFangrui Song <i@maskray.me>
Tue, 9 Jan 2024 19:24:21 +0000 (9 11:24 -0800)
committerGitHub <noreply@github.com>
Tue, 9 Jan 2024 19:24:21 +0000 (9 11:24 -0800)
tree71872219f9811b90740529371161db67c107b23d
parenta43e0f90b650fdcdf80bcb221d50a62905bf8977
[RISCV] Force relocations if initial MCSubtargetInfo contains FeatureRelax (#77436)

Regarding
```
.option norelax
j label
.option relax
// relaxable instructions
// For assembly input, RISCVAsmParser::ParseInstruction will set ForceRelocs (https://reviews.llvm.org/D46423).
// For direct object emission, ForceRelocs is not set after https://github.com/llvm/llvm-project/pull/73721
label:
```

The J instruction needs a relocation to ensure the target is correct
after linker relaxation. This is related a limitation in the assembler:
RISCVAsmBackend::shouldForceRelocation decides upfront whether a
relocation is needed, instead of checking more information (whether
there are relaxable fragments in between).

Despite the limitation, `j label` produces a relocation in direct object
emission mode, but was broken by #73721 due to the shouldForceRelocation
limitation.

Add a workaround to RISCVTargetELFStreamer to emulate the previous
behavior.

Link: https://github.com/ClangBuiltLinux/linux/issues/1965
llvm/lib/Target/RISCV/MCTargetDesc/RISCVELFStreamer.cpp
llvm/test/CodeGen/RISCV/option-relax-relocation.ll [new file with mode: 0644]