Skip to content

Commit 1ec9dd2

Browse files
committed
Sparc: Refactor R_SPARC_13/R_SPARC_GOT13 handling and fix a bug referencing absolute symbol
https://reviews.llvm.org/D47136 did not correctly handle `ld [%i0 + abs], %o0; abs = 7` To fix it and make fixup handling less hacky, * Change TableGen MEMri to use simm13Op instead of i32imm * Emit a fixup of kind fixup_sparc_13 in SparcMCCodeEmitter::getSImm13OpValue * Convert fixup_sparc_13 to either R_SPARC_13/R_SPARC_GOT13 in getRelocType This postpones 13/GOT13 decision to relocation generation, ensuring that we suppress the relocation when referencing an absolute symbol, matching gas.
1 parent c22bc21 commit 1ec9dd2

File tree

5 files changed

+37
-38
lines changed

5 files changed

+37
-38
lines changed

llvm/lib/Target/Sparc/AsmParser/SparcAsmParser.cpp

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,18 +1480,8 @@ SparcAsmParser::parseSparcAsmOperand(std::unique_ptr<SparcOperand> &Op,
14801480
if (getParser().parseExpression(EVal, E))
14811481
break;
14821482

1483-
int64_t Res;
1484-
if (!EVal->evaluateAsAbsolute(Res)) {
1485-
SparcMCExpr::Specifier Kind = SparcMCExpr::VK_13;
1486-
1487-
if (getContext().getObjectFileInfo()->isPositionIndependent()) {
1488-
if (isCall)
1489-
Kind = SparcMCExpr::VK_WPLT30;
1490-
else
1491-
Kind = SparcMCExpr::VK_GOT13;
1492-
}
1493-
EVal = SparcMCExpr::create(Kind, EVal, getContext());
1494-
}
1483+
if (isCall && getContext().getObjectFileInfo()->isPositionIndependent())
1484+
EVal = SparcMCExpr::create(SparcMCExpr::VK_WPLT30, EVal, getContext());
14951485
Op = SparcOperand::CreateImm(EVal, S, E);
14961486
break;
14971487
}

llvm/lib/Target/Sparc/MCTargetDesc/SparcELFObjectWriter.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include "MCTargetDesc/SparcFixupKinds.h"
1010
#include "MCTargetDesc/SparcMCExpr.h"
1111
#include "MCTargetDesc/SparcMCTargetDesc.h"
12+
#include "llvm/MC/MCContext.h"
1213
#include "llvm/MC/MCELFObjectWriter.h"
1314
#include "llvm/MC/MCExpr.h"
15+
#include "llvm/MC/MCObjectFileInfo.h"
1416
#include "llvm/MC/MCObjectWriter.h"
1517
#include "llvm/MC/MCValue.h"
1618
#include "llvm/Support/ErrorHandling.h"
@@ -110,7 +112,11 @@ unsigned SparcELFObjectWriter::getRelocType(MCContext &Ctx,
110112
case FK_Data_8: return ((Fixup.getOffset() % 8)
111113
? ELF::R_SPARC_UA64
112114
: ELF::R_SPARC_64);
113-
case Sparc::fixup_sparc_13: return ELF::R_SPARC_13;
115+
case Sparc::fixup_sparc_13:
116+
if (Ctx.getObjectFileInfo()->isPositionIndependent())
117+
return ELF::R_SPARC_GOT13;
118+
return ELF::R_SPARC_13;
119+
114120
case Sparc::fixup_sparc_hi22: return ELF::R_SPARC_HI22;
115121
case Sparc::fixup_sparc_lo10: return ELF::R_SPARC_LO10;
116122
case Sparc::fixup_sparc_h44: return ELF::R_SPARC_H44;

llvm/lib/Target/Sparc/MCTargetDesc/SparcMCCodeEmitter.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,10 +163,7 @@ SparcMCCodeEmitter::getSImm13OpValue(const MCInst &MI, unsigned OpNo,
163163
Fixups.push_back(MCFixup::create(0, Expr, SExpr->getFixupKind()));
164164
return 0;
165165
}
166-
uint16_t Kind = Sparc::fixup_sparc_13;
167-
if (Ctx.getObjectFileInfo()->isPositionIndependent())
168-
Kind = ELF::R_SPARC_GOT13;
169-
Fixups.push_back(MCFixup::create(0, Expr, Kind));
166+
Fixups.push_back(MCFixup::create(0, Expr, Sparc::fixup_sparc_13));
170167
return 0;
171168
}
172169

llvm/lib/Target/Sparc/SparcInstrInfo.td

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,20 @@ def SparcMEMriAsmOperand : AsmOperandClass {
145145
let ParserMethod = "parseMEMOperand";
146146
}
147147

148+
def simm13Op : Operand<iPTR> {
149+
let OperandType = "OPERAND_IMMEDIATE";
150+
let DecoderMethod = "DecodeSIMM13";
151+
let EncoderMethod = "getSImm13OpValue";
152+
}
153+
148154
def MEMrr : Operand<iPTR> {
149155
let PrintMethod = "printMemOperand";
150156
let MIOperandInfo = (ops ptr_rc, ptr_rc);
151157
let ParserMatchClass = SparcMEMrrAsmOperand;
152158
}
153159
def MEMri : Operand<iPTR> {
154160
let PrintMethod = "printMemOperand";
155-
let MIOperandInfo = (ops ptr_rc, i32imm);
161+
let MIOperandInfo = (ops ptr_rc, simm13Op);
156162
let ParserMatchClass = SparcMEMriAsmOperand;
157163
}
158164

@@ -234,12 +240,6 @@ def calltarget : Operand<i32> {
234240
let ParserMatchClass = SparcCallTargetAsmOperand;
235241
}
236242

237-
def simm13Op : Operand<iPTR> {
238-
let OperandType = "OPERAND_IMMEDIATE";
239-
let DecoderMethod = "DecodeSIMM13";
240-
let EncoderMethod = "getSImm13OpValue";
241-
}
242-
243243
// Operand for printing out a condition code.
244244
let PrintMethod = "printCCOperand" in {
245245
def CCOp : Operand<i32>;

llvm/test/MC/Sparc/sparc-fixups.s

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,40 @@
1-
! RUN: llvm-mc %s -triple=sparcv9 -filetype=obj | llvm-objdump -dr - | FileCheck %s
1+
# RUN: llvm-mc %s -triple=sparcv9 -filetype=obj | llvm-objdump -dr - | FileCheck %s --check-prefixes=NOPIC,CHECK
2+
# RUN: llvm-mc %s -triple=sparcv9 -filetype=obj -position-independent | llvm-objdump -dr - | FileCheck %s
23
.text
34

4-
! Check that fixups are correctly applied.
5+
# Check that fixups are correctly applied.
56

67
.set sym, 0xfedcba98
78

8-
! CHECK: sethi 0x3fb72e, %o0
9-
! CHECK-NEXT: xor %o0, 0x298, %o0
10-
! CHECK-NEXT: sethi 0x3b72ea, %o1
11-
! CHECK-NEXT: xor %o0, 0x188, %o1
9+
## FIXME: Don't emit GOT relocations when -position-independent is specified.
10+
# NOPIC: sethi 0x3fb72e, %o0
11+
# NOPIC-NEXT: xor %o0, 0x298, %o0
12+
# NOPIC-NEXT: sethi 0x3b72ea, %o1
13+
# NOPIC-NEXT: xor %o0, 0x188, %o1
1214
sethi %hi(sym), %o0
1315
xor %o0, %lo(sym), %o0
1416
sethi %hi(-0x12345678), %o1
1517
xor %o0, %lo(-0x12345678), %o1
1618

17-
! CHECK: sethi 0x3fb, %o0
18-
! CHECK-NEXT: or %o0, 0x1cb, %o0
19-
! CHECK-NEXT: ld [%o0+0xa98], %o0
19+
# CHECK: ld [%i0+0x7], %o0
20+
ld [%i0 + abs], %o0
21+
abs = 7
22+
23+
# CHECK-NEXT: sethi 0x3fb, %o0
24+
# CHECK-NEXT: or %o0, 0x1cb, %o0
25+
# CHECK-NEXT: ld [%o0+0xa98], %o0
2026
sethi %h44(sym), %o0
2127
or %o0, %m44(sym), %o0
2228
ld [%o0 + %l44(sym)], %o0
2329

24-
! CHECK: sethi 0x0, %o0
25-
! CHECK-NEXT: sethi 0x3fb72e, %o0
26-
! CHECK-NEXT: or %o0, 0x0, %o0
30+
# CHECK-NEXT: sethi 0x0, %o0
31+
# CHECK-NEXT: sethi 0x3fb72e, %o0
32+
# CHECK-NEXT: or %o0, 0x0, %o0
2733
sethi %hh(sym), %o0
2834
sethi %lm(sym), %o0
2935
or %o0, %hm(sym), %o0
3036

31-
! CHECK: sethi 0x48d1, %o0
32-
! CHECK-NEXT: xor %o0, -0x168, %o0
37+
# CHECK-NEXT: sethi 0x48d1, %o0
38+
# CHECK-NEXT: xor %o0, -0x168, %o0
3339
sethi %hix(sym), %o0
3440
xor %o0, %lox(sym), %o0

0 commit comments

Comments
 (0)