
Follow-up to #140494 `shouldForceRelocation` is conservative and produces redundant relocations. For example, RISCVAsmBackend::ForceRelocs (introduced to support mixed relax/norelax code) leads to redundant relocations in the following example adapted from #77436 ``` .option norelax j label // For assembly input, RISCVAsmParser::ParseInstruction sets ForceRelocs (https://reviews.llvm.org/D46423). // For direct object emission, RISCVELFStreamer sets ForceRelocs (#77436) .option relax call foo // linker-relaxable .option norelax j label // redundant relocation due to ForceRelocs .option relax label: ``` Root problem: The `isSymbolRefDifferenceFullyResolvedImpl` condition in MCAssembler::evaluateFixup does not check whether two locations are separated by a fragment whose size can be indeterminate due to linker instruction (e.g. MCDataFragment with relaxation, or MCAlignFragment due to indeterminate start offst). This patch * Updates the fragment walk code in `attemptToFoldSymbolOffsetDifference` to treat MCRelaxableFragment (for --riscv-asm-relax-branches) as fixed size after finishLayout. * Adds a condition in `addReloc` to complement `isSymbolRefDifferenceFullyResolvedImpl`. * Removes the no longer needed `shouldForceRelocation`. This fragment walk code path handles nicely handles mixed relax/norelax case from https://discourse.llvm.org/t/possible-problem-related-to-subtarget-usage/75283 and allows us to remove `MCSubtargetInfo` argument (#73721) as a follow-up. This fragment walk code should be avoided in the absence of linker-relaxable fragments within the current section. Adjust two bolt/test/RISCV tests (#141310) Pull Request: https://github.com/llvm/llvm-project/pull/140692
26 lines
599 B
ArmAsm
26 lines
599 B
ArmAsm
// RUN: %clang %cflags -o %t %s
|
|
// RUN: llvm-bolt -o %t.bolt %t
|
|
// RUN: llvm-readelf -x .data %t.bolt | FileCheck %s
|
|
|
|
.text
|
|
.option norvc
|
|
.globl _start
|
|
.p2align 1
|
|
_start:
|
|
// Force BOLT into relocation mode
|
|
.reloc 0, R_RISCV_NONE
|
|
// BOLT removes this nop so the label difference is initially 8 but should be
|
|
// 4 after BOLT processes it.
|
|
nop
|
|
beq x0, x0, _test_end
|
|
_test_end:
|
|
ret
|
|
.size _start, .-_start
|
|
|
|
.data
|
|
// CHECK: Hex dump of section '.data':
|
|
// CHECK: 0x{{.*}} 04000000
|
|
.reloc ., R_RISCV_ADD32, _test_end
|
|
.reloc ., R_RISCV_SUB32, _start
|
|
.word _test_end - _start
|