diff options
author | Jinyang He <hejinyang@loongson.cn> | 2024-01-09 15:14:54 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-09 15:14:54 +0800 |
commit | b57159cb19cdc06ec5733f93f0975aa6f40595cb (patch) | |
tree | 13763817c95944ac84967690732fee522e1306f4 | |
parent | dad614cc606333fa614e696dbdd22263096dadb7 (diff) | |
download | llvm-b57159cb19cdc06ec5733f93f0975aa6f40595cb.zip llvm-b57159cb19cdc06ec5733f93f0975aa6f40595cb.tar.gz llvm-b57159cb19cdc06ec5733f93f0975aa6f40595cb.tar.bz2 |
[LoongArch] Support R_LARCH_{ADD,SUB}_ULEB128 for .uleb128 and force relocs when sym is not in section (#76433)
1, Follow RISCV 1df5ea29 to support generates relocs for .uleb128 which
can not be folded. Unlike RISCV, the located content of LoongArch should
be zero. LoongArch fixup uleb128 value by in-place addition and
subtraction reloc types named R_LARCH_{ADD,SUB}_ULEB128. The located
content can affect the result and R_LARCH_ADD_ULEB128 has enough info to
represent the first symbol value, so it needs to be set to zero.
2, Force relocs if sym is not in section so that it can emit relocs for
external symbol.
Fixes:
https://github.com/llvm/llvm-project/pull/72960#issuecomment-1866844679
-rw-r--r-- | llvm/include/llvm/MC/MCAsmBackend.h | 6 | ||||
-rw-r--r-- | llvm/lib/MC/MCAssembler.cpp | 6 | ||||
-rw-r--r-- | llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp | 69 | ||||
-rw-r--r-- | llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h | 3 | ||||
-rw-r--r-- | llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp | 9 | ||||
-rw-r--r-- | llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h | 4 | ||||
-rw-r--r-- | llvm/test/MC/LoongArch/Relocations/leb128.s | 72 | ||||
-rw-r--r-- | llvm/test/MC/LoongArch/Relocations/relax-addsub.s | 57 |
8 files changed, 187 insertions, 39 deletions
diff --git a/llvm/include/llvm/MC/MCAsmBackend.h b/llvm/include/llvm/MC/MCAsmBackend.h index 8931e8c..01a64fb 100644 --- a/llvm/include/llvm/MC/MCAsmBackend.h +++ b/llvm/include/llvm/MC/MCAsmBackend.h @@ -198,9 +198,9 @@ public: // Defined by linker relaxation targets to possibly emit LEB128 relocations // and set Value at the relocated location. - virtual bool relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, - int64_t &Value) const { - return false; + virtual std::pair<bool, bool> + relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, int64_t &Value) const { + return std::make_pair(false, false); } /// @} diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp index def1304..ad30b5c 100644 --- a/llvm/lib/MC/MCAssembler.cpp +++ b/llvm/lib/MC/MCAssembler.cpp @@ -1026,7 +1026,9 @@ bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { ? LF.getValue().evaluateKnownAbsolute(Value, Layout) : LF.getValue().evaluateAsAbsolute(Value, Layout); if (!Abs) { - if (!getBackend().relaxLEB128(LF, Layout, Value)) { + bool Relaxed, UseZeroPad; + std::tie(Relaxed, UseZeroPad) = getBackend().relaxLEB128(LF, Layout, Value); + if (!Relaxed) { getContext().reportError(LF.getValue().getLoc(), Twine(LF.isSigned() ? ".s" : ".u") + "leb128 expression is not absolute"); @@ -1034,6 +1036,8 @@ bool MCAssembler::relaxLEB(MCAsmLayout &Layout, MCLEBFragment &LF) { } uint8_t Tmp[10]; // maximum size: ceil(64/7) PadTo = std::max(PadTo, encodeULEB128(uint64_t(Value), Tmp)); + if (UseZeroPad) + Value = 0; } Data.clear(); raw_svector_ostream OSE(Data); diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp index 6d8ef1b..518f6b1 100644 --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.cpp @@ -91,6 +91,7 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, case FK_Data_2: case FK_Data_4: case FK_Data_8: + case FK_Data_leb128: return Value; case LoongArch::fixup_loongarch_b16: { if (!isInt<18>(Value)) @@ -128,6 +129,15 @@ static uint64_t adjustFixupValue(const MCFixup &Fixup, uint64_t Value, } } +static void fixupLeb128(MCContext &Ctx, const MCFixup &Fixup, + MutableArrayRef<char> Data, uint64_t Value) { + unsigned I; + for (I = 0; I != Data.size() && Value; ++I, Value >>= 7) + Data[I] |= uint8_t(Value & 0x7f); + if (Value) + Ctx.reportError(Fixup.getLoc(), "Invalid uleb128 value!"); +} + void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, @@ -143,6 +153,10 @@ void LoongArchAsmBackend::applyFixup(const MCAssembler &Asm, MCFixupKindInfo Info = getFixupKindInfo(Kind); MCContext &Ctx = Asm.getContext(); + // Fixup leb128 separately. + if (Fixup.getTargetKind() == FK_Data_leb128) + return fixupLeb128(Ctx, Fixup, Data, Value); + // Apply any target-specific value adjustments. Value = adjustFixupValue(Fixup, Value, Ctx); @@ -173,6 +187,7 @@ bool LoongArchAsmBackend::shouldForceRelocation(const MCAssembler &Asm, case FK_Data_2: case FK_Data_4: case FK_Data_8: + case FK_Data_leb128: return !Target.isAbsolute(); } } @@ -202,9 +217,24 @@ getRelocPairForSize(unsigned Size) { return std::make_pair( MCFixupKind(FirstLiteralRelocationKind + ELF::R_LARCH_ADD64), MCFixupKind(FirstLiteralRelocationKind + ELF::R_LARCH_SUB64)); + case 128: + return std::make_pair( + MCFixupKind(FirstLiteralRelocationKind + ELF::R_LARCH_ADD_ULEB128), + MCFixupKind(FirstLiteralRelocationKind + ELF::R_LARCH_SUB_ULEB128)); } } +std::pair<bool, bool> LoongArchAsmBackend::relaxLEB128(MCLEBFragment &LF, + MCAsmLayout &Layout, + int64_t &Value) const { + const MCExpr &Expr = LF.getValue(); + if (LF.isSigned() || !Expr.evaluateKnownAbsolute(Value, Layout)) + return std::make_pair(false, false); + LF.getFixups().push_back( + MCFixup::create(0, &Expr, FK_Data_leb128, Expr.getLoc())); + return std::make_pair(true, true); +} + bool LoongArchAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const { // We mostly follow binutils' convention here: align to 4-byte boundary with a @@ -226,21 +256,27 @@ bool LoongArchAsmBackend::handleAddSubRelocations(const MCAsmLayout &Layout, uint64_t &FixedValue) const { std::pair<MCFixupKind, MCFixupKind> FK; uint64_t FixedValueA, FixedValueB; - const MCSection &SecA = Target.getSymA()->getSymbol().getSection(); - const MCSection &SecB = Target.getSymB()->getSymbol().getSection(); - - // We need record relocation if SecA != SecB. Usually SecB is same as the - // section of Fixup, which will be record the relocation as PCRel. If SecB - // is not same as the section of Fixup, it will report error. Just return - // false and then this work can be finished by handleFixup. - if (&SecA != &SecB) - return false; - - // In SecA == SecB case. If the linker relaxation is enabled, we need record - // the ADD, SUB relocations. Otherwise the FixedValue has already been - // calculated out in evaluateFixup, return true and avoid record relocations. - if (!STI.hasFeature(LoongArch::FeatureRelax)) - return true; + const MCSymbol &SA = Target.getSymA()->getSymbol(); + const MCSymbol &SB = Target.getSymB()->getSymbol(); + + bool force = !SA.isInSection() || !SB.isInSection(); + if (!force) { + const MCSection &SecA = SA.getSection(); + const MCSection &SecB = SB.getSection(); + + // We need record relocation if SecA != SecB. Usually SecB is same as the + // section of Fixup, which will be record the relocation as PCRel. If SecB + // is not same as the section of Fixup, it will report error. Just return + // false and then this work can be finished by handleFixup. + if (&SecA != &SecB) + return false; + + // In SecA == SecB case. If the linker relaxation is enabled, we need record + // the ADD, SUB relocations. Otherwise the FixedValue has already been calc- + // ulated out in evaluateFixup, return true and avoid record relocations. + if (!STI.hasFeature(LoongArch::FeatureRelax)) + return true; + } switch (Fixup.getKind()) { case llvm::FK_Data_1: @@ -255,6 +291,9 @@ bool LoongArchAsmBackend::handleAddSubRelocations(const MCAsmLayout &Layout, case llvm::FK_Data_8: FK = getRelocPairForSize(64); break; + case llvm::FK_Data_leb128: + FK = getRelocPairForSize(128); + break; default: llvm_unreachable("unsupported fixup size"); } diff --git a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h index fef0e84..7197721 100644 --- a/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h +++ b/llvm/lib/Target/LoongArch/MCTargetDesc/LoongArchAsmBackend.h @@ -66,6 +66,9 @@ public: void relaxInstruction(MCInst &Inst, const MCSubtargetInfo &STI) const override {} + std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, + int64_t &Value) const override; + bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override; diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp index 716fb67..7ce08ea 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp @@ -329,16 +329,17 @@ bool RISCVAsmBackend::relaxDwarfCFA(MCDwarfCallFrameFragment &DF, return true; } -bool RISCVAsmBackend::relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, - int64_t &Value) const { +std::pair<bool, bool> RISCVAsmBackend::relaxLEB128(MCLEBFragment &LF, + MCAsmLayout &Layout, + int64_t &Value) const { if (LF.isSigned()) - return false; + return std::make_pair(false, false); const MCExpr &Expr = LF.getValue(); if (ULEB128Reloc) { LF.getFixups().push_back( MCFixup::create(0, &Expr, FK_Data_leb128, Expr.getLoc())); } - return Expr.evaluateKnownAbsolute(Value, Layout); + return std::make_pair(Expr.evaluateKnownAbsolute(Value, Layout), false); } // Given a compressed control flow instruction this function returns diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h index 2ad6534..902b44b 100644 --- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h +++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h @@ -100,8 +100,8 @@ public: bool &WasRelaxed) const override; bool relaxDwarfCFA(MCDwarfCallFrameFragment &DF, MCAsmLayout &Layout, bool &WasRelaxed) const override; - bool relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, - int64_t &Value) const override; + std::pair<bool, bool> relaxLEB128(MCLEBFragment &LF, MCAsmLayout &Layout, + int64_t &Value) const override; bool writeNopData(raw_ostream &OS, uint64_t Count, const MCSubtargetInfo *STI) const override; diff --git a/llvm/test/MC/LoongArch/Relocations/leb128.s b/llvm/test/MC/LoongArch/Relocations/leb128.s new file mode 100644 index 0000000..7a96ec5 --- /dev/null +++ b/llvm/test/MC/LoongArch/Relocations/leb128.s @@ -0,0 +1,72 @@ +# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=-relax %s -o %t +# RUN: llvm-readobj -r -x .alloc_w %t | FileCheck --check-prefixes=CHECK,NORELAX %s +# RUN: llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax %s -o %t.relax +# RUN: llvm-readobj -r -x .alloc_w %t.relax | FileCheck --check-prefixes=CHECK,RELAX %s + +# RUN: not llvm-mc --filetype=obj --triple=loongarch64 --mattr=-relax --defsym ERR=1 %s -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=ERR +# RUN: not llvm-mc --filetype=obj --triple=loongarch64 --mattr=+relax --defsym ERR=1 %s -o /dev/null 2>&1 | \ +# RUN: FileCheck %s --check-prefix=ERR + +# CHECK: Relocations [ +# CHECK-NEXT: .rela.alloc_w { +# RELAX-NEXT: 0x0 R_LARCH_ADD_ULEB128 w1 0x0 +# RELAX-NEXT: 0x0 R_LARCH_SUB_ULEB128 w 0x0 +# RELAX-NEXT: 0x1 R_LARCH_ADD_ULEB128 w2 0x0 +# RELAX-NEXT: 0x1 R_LARCH_SUB_ULEB128 w1 0x0 +# CHECK-NEXT: 0x2 R_LARCH_PCALA_HI20 foo 0x0 +# RELAX-NEXT: 0x2 R_LARCH_RELAX - 0x0 +# CHECK-NEXT: 0x6 R_LARCH_PCALA_LO12 foo 0x0 +# RELAX-NEXT: 0x6 R_LARCH_RELAX - 0x0 +# RELAX-NEXT: 0xA R_LARCH_ADD_ULEB128 w2 0x0 +# RELAX-NEXT: 0xA R_LARCH_SUB_ULEB128 w1 0x0 +# RELAX-NEXT: 0xB R_LARCH_ADD_ULEB128 w2 0x78 +# RELAX-NEXT: 0xB R_LARCH_SUB_ULEB128 w1 0x0 +# RELAX-NEXT: 0xD R_LARCH_ADD_ULEB128 w1 0x0 +# RELAX-NEXT: 0xD R_LARCH_SUB_ULEB128 w2 0x0 +# RELAX-NEXT: 0x17 R_LARCH_ADD_ULEB128 w3 0x6F +# RELAX-NEXT: 0x17 R_LARCH_SUB_ULEB128 w2 0x0 +# RELAX-NEXT: 0x18 R_LARCH_ADD_ULEB128 w3 0x71 +# RELAX-NEXT: 0x18 R_LARCH_SUB_ULEB128 w2 0x0 +# CHECK-NEXT: } +# CHECK-NEXT: ] + +# CHECK: Hex dump of section '.alloc_w': +# NORELAX-NEXT: 0x00000000 02080c00 001a8c01 c0020880 01f8ffff +# NORELAX-NEXT: 0x00000010 ffffffff ffff017f 8101 +# RELAX-NEXT: 0x00000000 00000c00 001a8c01 c0020080 00808080 +# RELAX-NEXT: 0x00000010 80808080 80800000 8000 + +.section .alloc_w,"ax",@progbits; w: +.uleb128 w1-w # w1 is later defined in the same section +.uleb128 w2-w1 # w1 and w2 are separated by a linker relaxable instruction +w1: + la.pcrel $t0, foo +w2: +.uleb128 w2-w1 # 0x08 +.uleb128 w2-w1+120 # 0x0180 +.uleb128 -(w2-w1) # 0x01fffffffffffffffff8 +.uleb128 w3-w2+111 # 0x7f +.uleb128 w3-w2+113 # 0x0181 +w3: + +.ifdef ERR +# ERR: :[[#@LINE+1]]:16: error: .uleb128 expression is not absolute +.uleb128 extern-w # extern is undefined +# ERR: :[[#@LINE+1]]:11: error: .uleb128 expression is not absolute +.uleb128 w-extern +# ERR: :[[#@LINE+1]]:11: error: .uleb128 expression is not absolute +.uleb128 x-w # x is later defined in another section + +.section .alloc_x,"aw",@progbits; x: +# ERR: :[[#@LINE+1]]:11: error: .uleb128 expression is not absolute +.uleb128 y-x +.section .alloc_y,"aw",@progbits; y: +# ERR: :[[#@LINE+1]]:11: error: .uleb128 expression is not absolute +.uleb128 x-y + +# ERR: :[[#@LINE+1]]:10: error: .uleb128 expression is not absolute +.uleb128 extern +# ERR: :[[#@LINE+1]]:10: error: .uleb128 expression is not absolute +.uleb128 y +.endif diff --git a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s index 1492265..cd01332 100644 --- a/llvm/test/MC/LoongArch/Relocations/relax-addsub.s +++ b/llvm/test/MC/LoongArch/Relocations/relax-addsub.s @@ -8,12 +8,23 @@ # NORELAX-NEXT: 0x10 R_LARCH_PCALA_HI20 .text 0x0 # NORELAX-NEXT: 0x14 R_LARCH_PCALA_LO12 .text 0x0 # NORELAX-NEXT: } +# NORELAX-NEXT: Section ({{.*}}) .rela.data { +# NORELAX-NEXT: 0x30 R_LARCH_ADD8 foo 0x0 +# NORELAX-NEXT: 0x30 R_LARCH_SUB8 .text 0x10 +# NORELAX-NEXT: 0x31 R_LARCH_ADD16 foo 0x0 +# NORELAX-NEXT: 0x31 R_LARCH_SUB16 .text 0x10 +# NORELAX-NEXT: 0x33 R_LARCH_ADD32 foo 0x0 +# NORELAX-NEXT: 0x33 R_LARCH_SUB32 .text 0x10 +# NORELAX-NEXT: 0x37 R_LARCH_ADD64 foo 0x0 +# NORELAX-NEXT: 0x37 R_LARCH_SUB64 .text 0x10 +# NORELAX-NEXT: } # NORELAX-NEXT: ] # NORELAX: Hex dump of section '.data': -# NORELAX-NEXT: 0x00000000 04040004 00000004 00000000 0000000c -# NORELAX-NEXT: 0x00000010 0c000c00 00000c00 00000000 00000808 -# NORELAX-NEXT: 0x00000020 00080000 00080000 00000000 00 +# NORELAX-NEXT: 0x00000000 04040004 00000004 00000000 00000004 +# NORELAX-NEXT: 0x00000010 0c0c000c 0000000c 00000000 0000000c +# NORELAX-NEXT: 0x00000020 08080008 00000008 00000000 00000008 +# NORELAX-NEXT: 0x00000030 00000000 00000000 00000000 000000 # RELAX: Relocations [ # RELAX-NEXT: Section ({{.*}}) .rela.text { @@ -23,21 +34,32 @@ # RELAX-NEXT: 0x14 R_LARCH_RELAX - 0x0 # RELAX-NEXT: } # RELAX-NEXT: Section ({{.*}}) .rela.data { -# RELAX-NEXT: 0x1E R_LARCH_ADD8 .L4 0x0 -# RELAX-NEXT: 0x1E R_LARCH_SUB8 .L3 0x0 -# RELAX-NEXT: 0x1F R_LARCH_ADD16 .L4 0x0 -# RELAX-NEXT: 0x1F R_LARCH_SUB16 .L3 0x0 -# RELAX-NEXT: 0x21 R_LARCH_ADD32 .L4 0x0 -# RELAX-NEXT: 0x21 R_LARCH_SUB32 .L3 0x0 -# RELAX-NEXT: 0x25 R_LARCH_ADD64 .L4 0x0 -# RELAX-NEXT: 0x25 R_LARCH_SUB64 .L3 0x0 +# RELAX-NEXT: 0x20 R_LARCH_ADD8 .L4 0x0 +# RELAX-NEXT: 0x20 R_LARCH_SUB8 .L3 0x0 +# RELAX-NEXT: 0x21 R_LARCH_ADD16 .L4 0x0 +# RELAX-NEXT: 0x21 R_LARCH_SUB16 .L3 0x0 +# RELAX-NEXT: 0x23 R_LARCH_ADD32 .L4 0x0 +# RELAX-NEXT: 0x23 R_LARCH_SUB32 .L3 0x0 +# RELAX-NEXT: 0x27 R_LARCH_ADD64 .L4 0x0 +# RELAX-NEXT: 0x27 R_LARCH_SUB64 .L3 0x0 +# RELAX-NEXT: 0x2F R_LARCH_ADD_ULEB128 .L4 0x0 +# RELAX-NEXT: 0x2F R_LARCH_SUB_ULEB128 .L3 0x0 +# RELAX-NEXT: 0x30 R_LARCH_ADD8 foo 0x0 +# RELAX-NEXT: 0x30 R_LARCH_SUB8 .L3 0x0 +# RELAX-NEXT: 0x31 R_LARCH_ADD16 foo 0x0 +# RELAX-NEXT: 0x31 R_LARCH_SUB16 .L3 0x0 +# RELAX-NEXT: 0x33 R_LARCH_ADD32 foo 0x0 +# RELAX-NEXT: 0x33 R_LARCH_SUB32 .L3 0x0 +# RELAX-NEXT: 0x37 R_LARCH_ADD64 foo 0x0 +# RELAX-NEXT: 0x37 R_LARCH_SUB64 .L3 0x0 # RELAX-NEXT: } # RELAX-NEXT: ] # RELAX: Hex dump of section '.data': -# RELAX-NEXT: 0x00000000 04040004 00000004 00000000 0000000c -# RELAX-NEXT: 0x00000010 0c000c00 00000c00 00000000 00000000 -# RELAX-NEXT: 0x00000020 00000000 00000000 00000000 00 +# RELAX-NEXT: 0x00000000 04040004 00000004 00000000 00000004 +# RELAX-NEXT: 0x00000010 0c0c000c 0000000c 00000000 0000000c +# RELAX-NEXT: 0x00000020 00000000 00000000 00000000 00000000 +# RELAX-NEXT: 0x00000030 00000000 00000000 00000000 000000 .text .L1: @@ -55,13 +77,20 @@ .short .L2 - .L1 .word .L2 - .L1 .dword .L2 - .L1 +.uleb128 .L2 - .L1 ## TODO Handle alignment directive. .byte .L3 - .L2 .short .L3 - .L2 .word .L3 - .L2 .dword .L3 - .L2 +.uleb128 .L3 - .L2 ## With relaxation, emit relocs because the la.pcrel makes the diff variable. .byte .L4 - .L3 .short .L4 - .L3 .word .L4 - .L3 .dword .L4 - .L3 +.uleb128 .L4 - .L3 +.byte foo - .L3 +.short foo - .L3 +.word foo - .L3 +.dword foo - .L3 |