Skip to content

Commit d408f17

Browse files
committed
Address feedback
1 parent acc71d4 commit d408f17

File tree

3 files changed

+56
-27
lines changed

3 files changed

+56
-27
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ static cl::opt<bool> ULEB128Reloc(
3939
RISCVAsmBackend::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
}

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,10 @@ class RISCVAsmBackend : public MCAsmBackend {
5050
bool evaluateTargetFixup(const MCFixup &Fixup, const MCValue &Target,
5151
uint64_t &Value) override;
5252

53-
std::optional<StringRef>
54-
getVendorIdentifierForFixup(unsigned FixupKind) const;
55-
5653
bool addReloc(const MCFragment &, const MCFixup &, const MCValue &,
5754
uint64_t &FixedValue, bool IsResolved) override;
5855

59-
void addVendorReloc(const MCFragment &, const MCFixup &,
60-
StringRef VendorSymbol);
56+
void maybeAddVendorReloc(const MCFragment &, const MCFixup &);
6157

6258
void applyFixup(const MCFragment &, const MCFixup &, const MCValue &Target,
6359
MutableArrayRef<char> Data, uint64_t Value,

llvm/test/MC/RISCV/vendor-symbol.s

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# RUN: llvm-mc -triple riscv32 -mattr=+experimental-xqcibi %s \
2+
# RUN: -filetype=obj -o - \
3+
# RUN: | llvm-readelf -sr - \
4+
# RUN: | FileCheck %s
5+
6+
7+
## This checks that the vendor identifier symbols required for vendor
8+
## relocations do not interfere with symbols with identical names that
9+
## are written in assembly.
10+
11+
.option exact
12+
13+
qc.e.bgeui s0, 20, QUALCOMM
14+
15+
.global QUALCOMM
16+
QUALCOMM:
17+
nop
18+
19+
qc.e.bgeui s0, 20, QUALCOMM
20+
21+
22+
# CHECK-LABEL: Relocation section '.rela.text'
23+
## Note the different values for the "Sym. Value" Field
24+
# CHECK: R_RISCV_VENDOR 00000000 QUALCOMM + 0
25+
# CHECK: R_RISCV_CUSTOM193 00000006 QUALCOMM + 0
26+
# CHECK: R_RISCV_VENDOR 00000000 QUALCOMM + 0
27+
# CHECK: R_RISCV_CUSTOM193 00000006 QUALCOMM + 0
28+
29+
30+
# CHECK-LABEL: Symbol table '.symtab'
31+
# CHECK: 00000000 0 NOTYPE LOCAL DEFAULT ABS QUALCOMM
32+
# CHECK: 00000006 0 NOTYPE GLOBAL DEFAULT 2 QUALCOMM

0 commit comments

Comments
 (0)