D60657-riscv-pcrel_lo
authorLLVM Packaging Team <pkg-llvm-team@lists.alioth.debian.org>
Sat, 31 Oct 2020 13:09:15 +0000 (13:09 +0000)
committerSylvestre Ledru <sylvestre@debian.org>
Sat, 31 Oct 2020 13:09:15 +0000 (13:09 +0000)
commita5b098fe97e45449e910e86c1eadcf517122ca16
treef6c9ae125b487a9962b267d23116b2405b97a812
parent650300ce6261992561992ff29564ee2f2368b87b
D60657-riscv-pcrel_lo

commit 41449c58c58e466bcf9cdc4f7415950382bad8d7
Author: Roger Ferrer Ibanez <roger.ferrer@bsc.es>
Date:   Fri Nov 8 08:26:30 2019 +0000

    [RISCV] Fix evaluation of %pcrel_lo

    The following testcase

      function:
      .Lpcrel_label1:
            auipc   a0, %pcrel_hi(other_function)
            addi    a1, a0, %pcrel_lo(.Lpcrel_label1)
            .p2align        2          # Causes a new fragment to be emitted

            .type   other_function,@function
      other_function:
            ret

    exposes an odd behaviour in which only the %pcrel_hi relocation is
    evaluated but not the %pcrel_lo.

      $ llvm-mc -triple riscv64 -filetype obj t.s | llvm-objdump  -d -r -

      <stdin>:      file format ELF64-riscv

      Disassembly of section .text:
      0000000000000000 function:
             0:     17 05 00 00     auipc   a0, 0
             4:     93 05 05 00     mv      a1, a0
                    0000000000000004:  R_RISCV_PCREL_LO12_I other_function+4

      0000000000000008 other_function:
             8:     67 80 00 00     ret

    The reason seems to be that in RISCVAsmBackend::shouldForceRelocation we
    only consider the fragment but in RISCVMCExpr::evaluatePCRelLo we
    consider the section. This usually works but there are cases where the
    section may still be the same but the fragment may be another one. In
    that case we end forcing a %pcrel_lo relocation without any %pcrel_hi.

    This patch makes RISCVAsmBackend::shouldForceRelocation use the section,
    if any, to determine if the relocation must be forced or not.

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

Gbp-Pq: Name D60657-riscv-pcrel_lo.diff
llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
llvm/test/MC/RISCV/pcrel-fixups.s [new file with mode: 0644]