@@ -120,6 +120,28 @@ MCCodeEmitter *llvm::createRISCVMCCodeEmitter(const MCInstrInfo &MCII,
120120 return new RISCVMCCodeEmitter (Ctx, MCII);
121121}
122122
123+ static void addFixup (SmallVectorImpl<MCFixup> &Fixups, uint32_t Offset,
124+ const MCExpr *Value, uint16_t Kind) {
125+ bool PCRel = false ;
126+ switch (Kind) {
127+ case ELF::R_RISCV_CALL_PLT:
128+ case RISCV::fixup_riscv_pcrel_hi20:
129+ case RISCV::fixup_riscv_pcrel_lo12_i:
130+ case RISCV::fixup_riscv_pcrel_lo12_s:
131+ case RISCV::fixup_riscv_jal:
132+ case RISCV::fixup_riscv_branch:
133+ case RISCV::fixup_riscv_rvc_jump:
134+ case RISCV::fixup_riscv_rvc_branch:
135+ case RISCV::fixup_riscv_call:
136+ case RISCV::fixup_riscv_call_plt:
137+ case RISCV::fixup_riscv_qc_e_branch:
138+ case RISCV::fixup_riscv_qc_e_call_plt:
139+ case RISCV::fixup_riscv_nds_branch_10:
140+ PCRel = true ;
141+ }
142+ Fixups.push_back (MCFixup::create (Offset, Value, Kind, PCRel));
143+ }
144+
123145// Expand PseudoCALL(Reg), PseudoTAIL and PseudoJump to AUIPC and JALR with
124146// relocation types. We expand those pseudo-instructions while encoding them,
125147// meaning AUIPC and JALR won't go through RISC-V MC to MC compressed
@@ -181,7 +203,7 @@ void RISCVMCCodeEmitter::expandTLSDESCCall(const MCInst &MI,
181203 MCRegister Link = MI.getOperand (0 ).getReg ();
182204 MCRegister Dest = MI.getOperand (1 ).getReg ();
183205 int64_t Imm = MI.getOperand (2 ).getImm ();
184- Fixups. push_back ( MCFixup::create ( 0 , Expr, ELF::R_RISCV_TLSDESC_CALL) );
206+ addFixup (Fixups, 0 , Expr, ELF::R_RISCV_TLSDESC_CALL);
185207 MCInst Call =
186208 MCInstBuilder (RISCV::JALR).addReg (Link).addReg (Dest).addImm (Imm);
187209
@@ -208,7 +230,7 @@ void RISCVMCCodeEmitter::expandAddTPRel(const MCInst &MI,
208230 assert (Expr && Expr->getSpecifier () == ELF::R_RISCV_TPREL_ADD &&
209231 " Expected tprel_add relocation on TP-relative symbol" );
210232
211- Fixups. push_back ( MCFixup::create ( 0 , Expr, ELF::R_RISCV_TPREL_ADD) );
233+ addFixup (Fixups, 0 , Expr, ELF::R_RISCV_TPREL_ADD);
212234 if (STI.hasFeature (RISCV::FeatureRelax))
213235 Fixups.back ().setLinkerRelaxable ();
214236
@@ -318,10 +340,8 @@ void RISCVMCCodeEmitter::expandLongCondBr(const MCInst &MI,
318340 // Drop any fixup added so we can add the correct one.
319341 Fixups.resize (FixupStartIndex);
320342
321- if (SrcSymbol.isExpr ()) {
322- Fixups.push_back (
323- MCFixup::create (Offset, SrcSymbol.getExpr (), RISCV::fixup_riscv_jal));
324- }
343+ if (SrcSymbol.isExpr ())
344+ addFixup (Fixups, Offset, SrcSymbol.getExpr (), RISCV::fixup_riscv_jal);
325345}
326346
327347// Expand PseudoLongQC_(E_)Bxxx to an inverted conditional branch and an
@@ -368,10 +388,8 @@ void RISCVMCCodeEmitter::expandQCLongCondBrImm(const MCInst &MI,
368388 support::endian::write (CB, JBinary, llvm::endianness::little);
369389 // Drop any fixup added so we can add the correct one.
370390 Fixups.resize (FixupStartIndex);
371- if (SrcSymbol.isExpr ()) {
372- Fixups.push_back (
373- MCFixup::create (Offset, SrcSymbol.getExpr (), RISCV::fixup_riscv_jal));
374- }
391+ if (SrcSymbol.isExpr ())
392+ addFixup (Fixups, Offset, SrcSymbol.getExpr (), RISCV::fixup_riscv_jal);
375393}
376394
377395void RISCVMCCodeEmitter::encodeInstruction (const MCInst &MI,
@@ -649,7 +667,7 @@ uint64_t RISCVMCCodeEmitter::getImmOpValue(const MCInst &MI, unsigned OpNo,
649667
650668 assert (FixupKind != RISCV::fixup_riscv_invalid && " Unhandled expression!" );
651669
652- Fixups. push_back ( MCFixup::create ( 0 , Expr, FixupKind) );
670+ addFixup (Fixups, 0 , Expr, FixupKind);
653671 // If linker relaxation is enabled and supported by this relocation, set
654672 // a bit so that if fixup is unresolved, a R_RISCV_RELAX relocation will be
655673 // appended.
0 commit comments