Skip to content

Commit fd883ab

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.5-bogner
2 parents 6e4e88d + 00dc73e commit fd883ab

36 files changed

+208
-146
lines changed

lld/test/ELF/riscv-reloc-plt32.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
.globl _start
1919
_start:
2020
.data
21-
.word foo@PLT - .
22-
.word foo@PLT - . + 1
23-
.word foo@PLT - . - 1
21+
.word %plt(foo - .)
22+
.word %plt(foo - . + 1)
23+
.word %plt(foo - . - 1)

lld/test/ELF/riscv-undefined-weak.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,4 @@ branch:
9797
# PC-NOT: .plt:
9898
# PLT: .plt:
9999

100-
.word target@plt - .
100+
.word %plt(target - .)

lld/test/ELF/riscv64-reloc-got32-pcrel.s

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ bar:
1212

1313
.globl _start
1414
_start: // PC = 0x33a8
15-
// bar@GOTPCREL = 0x2398 (got entry for `bar`) - 0x33a8 (.) = 0xf0efffff
16-
// bar@GOTPCREL+4 = 0x2398 (got entry for `bar`) - 0x33ac (.) + 4 = 0xf0efffff
17-
// bar@GOTPCREL-4 = 0x2398 (got entry for `bar`) - 0x33b0 (.) - 4 = 0xe4efffff
15+
// %gotpcrel(bar) = 0x2398 (got entry for `bar`) - 0x33a8 (.) = 0xf0efffff
16+
// %gotpcrel(bar+4) = 0x2398 (got entry for `bar`) - 0x33ac (.) + 4 = 0xf0efffff
17+
// %gotpcrel(bar-4) = 0x2398 (got entry for `bar`) - 0x33b0 (.) - 4 = 0xe4efffff
1818
// CHECK: Contents of section .data:
1919
// CHECK-NEXT: {{.*}} f0efffff f0efffff e4efffff
20-
.word bar@GOTPCREL
21-
.word bar@GOTPCREL+4
22-
.word bar@GOTPCREL-4
20+
.word %gotpcrel(bar)
21+
.word %gotpcrel(bar+4)
22+
.word %gotpcrel(bar-4)
2323

2424
// WARN: relocation R_RISCV_GOT32_PCREL out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
2525
// WARN: relocation R_RISCV_GOT32_PCREL out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
26-
.word baz@GOTPCREL+0xffffffff
27-
.word baz@GOTPCREL-0xffffffff
26+
.word %gotpcrel(baz+0xffffffff)
27+
.word %gotpcrel(baz-0xffffffff)

llvm/include/llvm/CodeGen/TargetLoweringObjectFileImpl.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
3838

3939
protected:
4040
uint8_t PLTRelativeSpecifier = 0;
41+
bool UseTargetMCExprForPLTRelative = true;
4142

4243
public:
4344
TargetLoweringObjectFileELF();
@@ -112,11 +113,16 @@ class TargetLoweringObjectFileELF : public TargetLoweringObjectFile {
112113
MCSection *getStaticDtorSection(unsigned Priority,
113114
const MCSymbol *KeySym) const override;
114115

116+
virtual const MCExpr *createTargetMCExpr(const MCExpr *Expr,
117+
uint8_t Specifier) const {
118+
return nullptr;
119+
}
115120
const MCExpr *lowerRelativeReference(const GlobalValue *LHS,
116121
const GlobalValue *RHS,
117122
const TargetMachine &TM) const override;
118123

119124
const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
125+
const MCSymbolRefExpr *RHS,
120126
const TargetMachine &TM) const override;
121127

122128
MCSection *getSectionForCommandLines() const override;

llvm/include/llvm/MC/MCParser/MCTargetAsmParser.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,12 @@ class MCTargetAsmParser : public MCAsmParserExtension {
396396
virtual bool parsePrimaryExpr(const MCExpr *&Res, SMLoc &EndLoc) {
397397
return getParser().parsePrimaryExpr(Res, EndLoc, nullptr);
398398
}
399+
// Parse an expression in a data directive, possibly with a relocation
400+
// specifier.
401+
virtual bool parseDataExpr(const MCExpr *&Res) {
402+
SMLoc EndLoc;
403+
return getParser().parseExpression(Res, EndLoc);
404+
}
399405

400406
virtual bool parseRegister(MCRegister &Reg, SMLoc &StartLoc,
401407
SMLoc &EndLoc) = 0;

llvm/include/llvm/Target/TargetLoweringObjectFile.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class TargetLoweringObjectFile : public MCObjectFileInfo {
209209
}
210210

211211
virtual const MCExpr *lowerDSOLocalEquivalent(const DSOLocalEquivalent *Equiv,
212+
const MCSymbolRefExpr *RHS,
212213
const TargetMachine &TM) const {
213214
return nullptr;
214215
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3382,7 +3382,7 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
33823382
return lowerBlockAddressConstant(*BA);
33833383

33843384
if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV))
3385-
return getObjFileLowering().lowerDSOLocalEquivalent(Equiv, TM);
3385+
return getObjFileLowering().lowerDSOLocalEquivalent(Equiv, nullptr, TM);
33863386

33873387
if (const NoCFIValue *NC = dyn_cast<NoCFIValue>(CV))
33883388
return MCSymbolRefExpr::create(getSymbol(NC->getGlobalValue()), Ctx);
@@ -3481,12 +3481,13 @@ const MCExpr *AsmPrinter::lowerConstant(const Constant *CV) {
34813481
if (!RelocExpr) {
34823482
const MCExpr *LHSExpr =
34833483
MCSymbolRefExpr::create(getSymbol(LHSGV), Ctx);
3484+
auto *RHSExpr = MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx);
34843485
if (DSOEquiv &&
34853486
getObjFileLowering().supportDSOLocalEquivalentLowering())
3486-
LHSExpr =
3487-
getObjFileLowering().lowerDSOLocalEquivalent(DSOEquiv, TM);
3488-
RelocExpr = MCBinaryExpr::createSub(
3489-
LHSExpr, MCSymbolRefExpr::create(getSymbol(RHSGV), Ctx), Ctx);
3487+
RelocExpr = getObjFileLowering().lowerDSOLocalEquivalent(
3488+
DSOEquiv, RHSExpr, TM);
3489+
else
3490+
RelocExpr = MCBinaryExpr::createSub(LHSExpr, RHSExpr, Ctx);
34903491
}
34913492
int64_t Addend = (LHSOffset - RHSOffset).getSExtValue();
34923493
if (Addend != 0)

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,18 +1194,34 @@ const MCExpr *TargetLoweringObjectFileELF::lowerRelativeReference(
11941194
MCSymbolRefExpr::create(TM.getSymbol(RHS), getContext()), getContext());
11951195
}
11961196

1197+
// Reference the function or its PLT entry (`Equiv`), optionally with a
1198+
// subtrahend (`RHS`).
11971199
const MCExpr *TargetLoweringObjectFileELF::lowerDSOLocalEquivalent(
1198-
const DSOLocalEquivalent *Equiv, const TargetMachine &TM) const {
1200+
const DSOLocalEquivalent *Equiv, const MCSymbolRefExpr *RHS,
1201+
const TargetMachine &TM) const {
11991202
assert(supportDSOLocalEquivalentLowering());
12001203

1204+
// If GV is dso_local, reference the function itself. Return GV or GV-RHS.
12011205
const auto *GV = Equiv->getGlobalValue();
1202-
1203-
// A PLT entry is not needed for dso_local globals.
1206+
const MCExpr *Res = MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
1207+
if (RHS)
1208+
Res = MCBinaryExpr::createSub(Res, RHS, getContext());
12041209
if (GV->isDSOLocal() || GV->isImplicitDSOLocal())
1205-
return MCSymbolRefExpr::create(TM.getSymbol(GV), getContext());
1206-
1207-
return MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeSpecifier,
1208-
getContext());
1210+
return Res;
1211+
1212+
// Otherwise, reference the PLT. Return a relocatable expression with the PLT
1213+
// specifier, %plt(GV) or %plt(GV-RHS).
1214+
Res = createTargetMCExpr(Res, PLTRelativeSpecifier);
1215+
if (Res)
1216+
return Res;
1217+
1218+
// If the target only supports the legacy syntax @plt, return GV@plt or
1219+
// GV@plt - RHS.
1220+
Res = MCSymbolRefExpr::create(TM.getSymbol(GV), PLTRelativeSpecifier,
1221+
getContext());
1222+
if (RHS)
1223+
Res = MCBinaryExpr::createSub(Res, RHS, getContext());
1224+
return Res;
12091225
}
12101226

12111227
MCSection *TargetLoweringObjectFileELF::getSectionForCommandLines() const {

llvm/lib/MC/MCAssembler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ bool MCAssembler::evaluateFixup(const MCFixup &Fixup, const MCFragment *DF,
237237
// A linker relaxation target may emit ADD/SUB relocations for A-B+C. Let
238238
// recordRelocation handle non-VK_None cases like A@plt-B+C.
239239
if (!IsResolved && Target.getSymA() && Target.getSymB() &&
240-
Target.getSymA()->getKind() == MCSymbolRefExpr::VK_None &&
240+
Target.getRefKind() == 0 &&
241241
getBackend().handleAddSubRelocations(*this, *DF, Fixup, Target, Value))
242242
return true;
243243

llvm/lib/MC/MCParser/AsmParser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3144,7 +3144,7 @@ bool AsmParser::parseDirectiveValue(StringRef IDVal, unsigned Size) {
31443144
auto parseOp = [&]() -> bool {
31453145
const MCExpr *Value;
31463146
SMLoc ExprLoc = getLexer().getLoc();
3147-
if (checkForValidSection() || parseExpression(Value))
3147+
if (checkForValidSection() || getTargetParser().parseDataExpr(Value))
31483148
return true;
31493149
// Special case constant expressions to match code generator.
31503150
if (const MCConstantExpr *MCE = dyn_cast<MCConstantExpr>(Value)) {

0 commit comments

Comments
 (0)