Skip to content

Commit c440563

Browse files
committed
[CSKY] Fix CSKYMCCodeEmitter::getTargetFixup and set STT_TLS for TLS relocation specifiers
1 parent 112277e commit c440563

File tree

4 files changed

+37
-56
lines changed

4 files changed

+37
-56
lines changed

llvm/lib/Target/CSKY/MCTargetDesc/CSKYELFObjectWriter.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@
99
#include "CSKYFixupKinds.h"
1010
#include "CSKYMCExpr.h"
1111
#include "CSKYMCTargetDesc.h"
12+
#include "MCTargetDesc/CSKYMCExpr.h"
1213
#include "llvm/MC/MCContext.h"
1314
#include "llvm/MC/MCELFObjectWriter.h"
1415
#include "llvm/MC/MCObjectWriter.h"
16+
#include "llvm/MC/MCSymbolELF.h"
1517

1618
#define DEBUG_TYPE "csky-elf-object-writer"
1719

@@ -27,6 +29,8 @@ class CSKYELFObjectWriter : public MCELFObjectTargetWriter {
2729

2830
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
2931
const MCFixup &Fixup, bool IsPCRel) const override;
32+
bool needsRelocateWithSymbol(const MCValue &Val, const MCSymbol &Sym,
33+
unsigned Type) const override;
3034
};
3135

3236
} // namespace
@@ -40,6 +44,19 @@ unsigned CSKYELFObjectWriter::getRelocType(MCContext &Ctx,
4044
unsigned Kind = Fixup.getTargetKind();
4145
uint8_t Modifier = Target.getAccessVariant();
4246

47+
switch (Target.getRefKind()) {
48+
case CSKYMCExpr::VK_TLSIE:
49+
case CSKYMCExpr::VK_TLSLE:
50+
case CSKYMCExpr::VK_TLSGD:
51+
case CSKYMCExpr::VK_TLSLDM:
52+
case CSKYMCExpr::VK_TLSLDO:
53+
if (auto *S = Target.getSymA())
54+
cast<MCSymbolELF>(S->getSymbol()).setType(ELF::STT_TLS);
55+
break;
56+
default:
57+
break;
58+
}
59+
4360
if (IsPCRel) {
4461
switch (Kind) {
4562
default:
@@ -150,6 +167,18 @@ unsigned CSKYELFObjectWriter::getRelocType(MCContext &Ctx,
150167
}
151168
}
152169

170+
bool CSKYELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
171+
const MCSymbol &,
172+
unsigned Type) const {
173+
switch (V.getRefKind()) {
174+
case CSKYMCExpr::VK_PLT:
175+
case CSKYMCExpr::VK_GOT:
176+
return true;
177+
default:
178+
return false;
179+
}
180+
}
181+
153182
std::unique_ptr<MCObjectTargetWriter> llvm::createCSKYELFObjectWriter() {
154183
return std::make_unique<CSKYELFObjectWriter>();
155184
}

llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCCodeEmitter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,7 @@ unsigned CSKYMCCodeEmitter::getImmJMPIX(const MCInst &MI, unsigned Idx,
294294

295295
MCFixupKind CSKYMCCodeEmitter::getTargetFixup(const MCExpr *Expr) const {
296296
const CSKYMCExpr *CSKYExpr = cast<CSKYMCExpr>(Expr);
297-
298-
switch (CSKYExpr->getKind()) {
297+
switch (CSKYExpr->getSpecifier()) {
299298
default:
300299
llvm_unreachable("Unhandled fixup kind!");
301300
case CSKYMCExpr::VK_ADDR:

llvm/lib/Target/CSKY/MCTargetDesc/CSKYMCExpr.cpp

Lines changed: 3 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -67,59 +67,12 @@ void CSKYMCExpr::printImpl(raw_ostream &OS, const MCAsmInfo *MAI) const {
6767
OS << getVariantKindName(getSpecifier());
6868
}
6969

70-
static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
71-
switch (Expr->getKind()) {
72-
case MCExpr::Target:
73-
llvm_unreachable("Can't handle nested target expression");
74-
break;
75-
case MCExpr::Constant:
76-
break;
77-
78-
case MCExpr::Binary: {
79-
const MCBinaryExpr *BE = cast<MCBinaryExpr>(Expr);
80-
fixELFSymbolsInTLSFixupsImpl(BE->getLHS(), Asm);
81-
fixELFSymbolsInTLSFixupsImpl(BE->getRHS(), Asm);
82-
break;
83-
}
84-
85-
case MCExpr::SymbolRef: {
86-
// We're known to be under a TLS fixup, so any symbol should be
87-
// modified. There should be only one.
88-
const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
89-
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
90-
break;
91-
}
92-
93-
case MCExpr::Unary:
94-
fixELFSymbolsInTLSFixupsImpl(cast<MCUnaryExpr>(Expr)->getSubExpr(), Asm);
95-
break;
96-
}
97-
}
98-
9970
bool CSKYMCExpr::evaluateAsRelocatableImpl(MCValue &Res,
10071
const MCAssembler *Asm) const {
10172
if (!getSubExpr()->evaluateAsRelocatable(Res, Asm))
10273
return false;
10374

104-
// Some custom fixup types are not valid with symbol difference expressions
105-
if (Res.getSymA() && Res.getSymB()) {
106-
switch (getSpecifier()) {
107-
default:
108-
return true;
109-
case VK_GOT:
110-
case VK_GOT_IMM18_BY4:
111-
case VK_GOTPC:
112-
case VK_GOTOFF:
113-
case VK_PLT:
114-
case VK_PLT_IMM18_BY4:
115-
case VK_TLSIE:
116-
case VK_TLSLE:
117-
case VK_TLSGD:
118-
case VK_TLSLDO:
119-
case VK_TLSLDM:
120-
return false;
121-
}
122-
}
123-
124-
return true;
75+
Res =
76+
MCValue::get(Res.getSymA(), Res.getSymB(), Res.getConstant(), specifier);
77+
return !Res.getSymB();
12578
}

llvm/test/MC/CSKY/relocation-specifier.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
# READELF: R_CKCORE_GOT32 00000000 .data + 0
66
# READELF: R_CKCORE_PLT32 00000000 .data + 0
77

8-
# READELF: NOTYPE GLOBAL DEFAULT UND gd
9-
# READELF: NOTYPE GLOBAL DEFAULT UND ld
10-
# READELF: NOTYPE GLOBAL DEFAULT UND ie
11-
# READELF: NOTYPE GLOBAL DEFAULT UND le
8+
# READELF: TLS GLOBAL DEFAULT UND gd
9+
# READELF: TLS GLOBAL DEFAULT UND ld
10+
# READELF: TLS GLOBAL DEFAULT UND ie
11+
# READELF: TLS GLOBAL DEFAULT UND le
1212

1313
lrw16 r0, gd@TLSGD32
1414
lrw16 r0, ld@TLSLDM32

0 commit comments

Comments
 (0)