diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp --- a/llvm/lib/MC/MCObjectStreamer.cpp +++ b/llvm/lib/MC/MCObjectStreamer.cpp @@ -120,7 +120,29 @@ } flushPendingLabels(PendingFixup.DF, PendingFixup.DF->getContents().size()); PendingFixup.Fixup.setOffset(PendingFixup.Sym->getOffset()); - PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup); + + // If the location symbol to relocate is in MCEncodedFragmentWithFixups, + // put the Fixup into location symbol's fragment. Otherwise + // put into PendingFixup.DF + MCFragment *SymFragment = PendingFixup.Sym->getFragment(); + switch (SymFragment->getKind()) { + case MCFragment::FT_Relaxable: + case MCFragment::FT_Dwarf: + case MCFragment::FT_PseudoProbe: + cast>(SymFragment) + ->getFixups() + .push_back(PendingFixup.Fixup); + break; + case MCFragment::FT_Data: + case MCFragment::FT_CVDefRange: + cast>(SymFragment) + ->getFixups() + .push_back(PendingFixup.Fixup); + break; + default: + PendingFixup.DF->getFixups().push_back(PendingFixup.Fixup); + break; + } } PendingFixups.clear(); } diff --git a/llvm/test/MC/RISCV/reloc-directive.s b/llvm/test/MC/RISCV/reloc-directive.s --- a/llvm/test/MC/RISCV/reloc-directive.s +++ b/llvm/test/MC/RISCV/reloc-directive.s @@ -22,6 +22,10 @@ # CHECK-NEXT: 0x0 R_RISCV_NONE - 0x9 # CHECK-NEXT: 0x0 R_RISCV_32 - 0x9 # CHECK-NEXT: 0x0 R_RISCV_64 - 0x9 + +# CHECK: Section ({{.*}}) .rela.data +# CHECK-NEXT: 0x0 R_RISCV_32 - 0x6 +# CHECK-NEXT: } .text ret nop @@ -38,6 +42,8 @@ .reloc 0, BFD_RELOC_32, 9 .reloc 0, BFD_RELOC_64, 9 + .reloc foo, R_RISCV_32, 6 + .data .globl foo foo: