@@ -39,7 +39,7 @@ static cl::opt<bool> ULEB128Reloc(
3939RISCVAsmBackend::RISCVAsmBackend (const MCSubtargetInfo &STI, uint8_t OSABI,
4040 bool Is64Bit, const MCTargetOptions &Options)
4141 : MCAsmBackend(llvm::endianness::little), STI(STI), OSABI(OSABI),
42- Is64Bit(Is64Bit), TargetOptions(Options), VendorSymbols() {
42+ Is64Bit(Is64Bit), TargetOptions(Options) {
4343 RISCVFeatures::validate (STI.getTargetTriple (), STI.getFeatureBits ());
4444}
4545
@@ -612,38 +612,41 @@ bool RISCVAsmBackend::evaluateTargetFixup(const MCFixup &Fixup,
612612 isPCRelFixupResolved (AUIPCTarget.getAddSym (), *AUIPCDF);
613613}
614614
615- std::optional<StringRef>
616- RISCVAsmBackend::getVendorIdentifierForFixup (unsigned FixupKind) const {
617- switch (FixupKind) {
615+ void RISCVAsmBackend::maybeAddVendorReloc (const MCFragment &F, const MCFixup &Fixup) {
616+ MCContext &Ctx = Asm->getContext ();
617+
618+ StringRef VendorIdentifier;
619+
620+ switch (Fixup.getTargetKind ()) {
621+ default :
622+ // No Vendor Relocation Required.
623+ return ;
618624 case RISCV::fixup_riscv_qc_e_branch:
619625 case RISCV::fixup_riscv_qc_abs20_u:
620626 case RISCV::fixup_riscv_qc_e_32:
621627 case RISCV::fixup_riscv_qc_e_jump_plt:
622- return " QUALCOMM" ;
628+ VendorIdentifier = " QUALCOMM" ;
629+ break ;
623630 }
624631
625- return std::nullopt ;
626- }
627-
628- void RISCVAsmBackend::addVendorReloc (const MCFragment &F, const MCFixup &Fixup,
629- StringRef VendorIdentifier) {
630- MCContext &Ctx = Asm->getContext ();
632+ // Create a local symbol for the vendor relocation to reference. It's fine if the symbol has the same name as an existing symbol.
633+ MCSymbol *VendorSymbol = Ctx.createLocalSymbol (VendorIdentifier);
634+ auto [It, Inserted] = VendorSymbols.try_emplace (VendorIdentifier, VendorSymbol);
631635
632- auto It = VendorSymbols.find (VendorIdentifier);
633- if (It == VendorSymbols.end ()) {
634- MCSymbol *VendorSymbol = Ctx.createLocalSymbol (VendorIdentifier);
636+ if (Inserted) {
637+ // Setup the just-created symbol
635638 VendorSymbol->setVariableValue (MCConstantExpr::create (0 , Ctx));
636639 Asm->registerSymbol (*VendorSymbol);
637-
638- It = VendorSymbols.try_emplace (VendorIdentifier, VendorSymbol).first ;
640+ } else {
641+ // Fetch the existing symbol
642+ VendorSymbol = It->getValue ();
639643 }
640644
641- MCSymbol *VendorSymbol = It->getValue ();
642645 const MCExpr *VendorExpr = MCSymbolRefExpr::create (VendorSymbol, Ctx);
643646 MCFixup VendorFixup =
644647 MCFixup::create (Fixup.getOffset (), VendorExpr, ELF::R_RISCV_VENDOR);
645- // Explicitly create MCValue so that the absolute symbol is not evaluated to
646- // a constant.
648+ // Explicitly create MCValue rather than using `VendorExpr->evaluateAsRelocatable`
649+ // so that the absolute symbol is not evaluated to a constant.
647650 MCValue VendorTarget = MCValue::get (VendorSymbol);
648651 uint64_t VendorValue;
649652 Asm->getWriter ().recordRelocation (F, VendorFixup, VendorTarget, VendorValue);
@@ -702,9 +705,7 @@ bool RISCVAsmBackend::addReloc(const MCFragment &F, const MCFixup &Fixup,
702705 if (!IsResolved) {
703706 // Some Fixups require a vendor relocation, record it (directly) before we
704707 // add the relocation.
705- if (std::optional<StringRef> VendorIdentifier =
706- getVendorIdentifierForFixup (Fixup.getTargetKind ()))
707- addVendorReloc (F, Fixup, *VendorIdentifier);
708+ maybeAddVendorReloc (F, Fixup);
708709
709710 Asm->getWriter ().recordRelocation (F, Fixup, Target, FixedValue);
710711 }
0 commit comments