Skip to content
This repository was archived by the owner on Dec 20, 2019. It is now read-only.

Rework emulated TLS patch for Android #3

Merged
merged 2 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/llvm/CodeGen/TargetLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -3664,6 +3664,9 @@ class TargetLowering : public TargetLoweringBase {
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
SelectionDAG &DAG) const;

SDValue LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
SelectionDAG &DAG, bool is64bit) const; // LDC

/// Expands target specific indirect branch for the case of JumpTable
/// expanasion.
virtual SDValue expandIndirectJTBranch(const SDLoc& dl, SDValue Value, SDValue Addr,
Expand Down
27 changes: 27 additions & 0 deletions lib/CodeGen/SelectionDAG/TargetLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4334,6 +4334,33 @@ SDValue TargetLowering::getVectorElementPointer(SelectionDAG &DAG,
return DAG.getNode(ISD::ADD, dl, IdxVT, VecPtr, Index);
}

SDValue
TargetLowering::LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
SelectionDAG &DAG, bool is64bit) const { // LDC
SDLoc DL(Op);
SDValue Chain = DAG.getEntryNode();
ArgListTy Args;
ArgListEntry Entry;
Type *Ty;
if (is64bit)
Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
else
Ty = (Type *)Type::getInt32Ty(*DAG.getContext());
Entry.Node = Result;
Entry.Ty = Ty;
Args.push_back(Entry);

// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
TargetLowering::CallLoweringInfo CLI(DAG);
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
CallingConv::C, Ty,
DAG.getExternalSymbol("__tls_get_addr",
getPointerTy(DAG.getDataLayout())),
std::move(Args));
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
return CallResult.first;
}

//===----------------------------------------------------------------------===//
// Implementation of Emulated TLS Model
//===----------------------------------------------------------------------===//
Expand Down
2 changes: 1 addition & 1 deletion lib/CodeGen/TargetLoweringObjectFileImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const MCExpr *TargetLoweringObjectFileELF::getTTypeGlobalReference(

static SectionKind getELFKindForNamedSection(StringRef Name, SectionKind K,
const Triple &TargetTriple) {
// N.B.: The defaults used in here are no the same ones used in MC.
// N.B.: The defaults used in here are not the same ones used in MC.
// We follow gcc, MC follows gas. For example, given ".section .eh_frame",
// both gas and MC will produce a section with no flags. Given
// section(".eh_frame") gcc will produce:
Expand Down
27 changes: 1 addition & 26 deletions lib/Target/AArch64/AArch64ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4149,31 +4149,6 @@ AArch64TargetLowering::LowerELFGlobalTLSAddress(SDValue Op,
return DAG.getNode(ISD::ADD, DL, PtrVT, ThreadBase, TPOff);
}

SDValue
AArch64TargetLowering::LowerAndroidGlobalTLSAddress(SDValue Op,
SelectionDAG &DAG) const {
assert(Subtarget->isTargetELF() && "This function expects an ELF target");
SDLoc DL(Op);
SDValue Result = LowerGlobalAddress(Op, DAG);
SDValue Chain = DAG.getEntryNode();
ArgListTy Args;
ArgListEntry Entry;
Type *Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
Entry.Node = Result;
Entry.Ty = Ty;
Args.push_back(Entry);

// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
TargetLowering::CallLoweringInfo CLI(DAG);
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
CallingConv::C, Ty,
DAG.getExternalSymbol("__tls_get_addr",
getPointerTy(DAG.getDataLayout())),
std::move(Args));
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
return CallResult.first;
}

SDValue
AArch64TargetLowering::LowerWindowsGlobalTLSAddress(SDValue Op,
SelectionDAG &DAG) const {
Expand Down Expand Up @@ -4243,7 +4218,7 @@ SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
return LowerDarwinGlobalTLSAddress(Op, DAG);
if (Subtarget->isTargetELF()) {
if (Subtarget->isTargetAndroid())
return LowerAndroidGlobalTLSAddress(Op, DAG); // LDC
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, true); // LDC
else
return LowerELFGlobalTLSAddress(Op, DAG);
}
Expand Down
1 change: 0 additions & 1 deletion lib/Target/AArch64/AArch64ISelLowering.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,6 @@ class AArch64TargetLowering : public TargetLowering {
SDValue LowerADDROFRETURNADDR(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerAndroidGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const; // LDC
SDValue LowerDarwinGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerELFGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const;
SDValue LowerELFTLSDescCallSeq(SDValue SymAddr, const SDLoc &DL,
Expand Down
2 changes: 2 additions & 0 deletions lib/Target/ARM/ARMISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3018,6 +3018,8 @@ ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {

// TODO: implement the "local dynamic" model
assert(Subtarget->isTargetELF() && "Only ELF implemented here");
if (Subtarget->isTargetAndroid())
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, false); // LDC
TLSModel::Model model = getTargetMachine().getTLSModel(GA->getGlobal());

switch (model) {
Expand Down
8 changes: 2 additions & 6 deletions lib/Target/ARM/MCTargetDesc/ARMAsmBackendELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,15 @@ using namespace llvm;

namespace {
class ARMAsmBackendELF : public ARMAsmBackend {
// LDC
const bool isAndroid;

public:
uint8_t OSABI;
ARMAsmBackendELF(const Target &T, const MCSubtargetInfo &STI, uint8_t OSABI,
support::endianness Endian)
: ARMAsmBackend(T, STI, Endian),
isAndroid(STI.getTargetTriple().isAndroid()), OSABI(OSABI) {}
: ARMAsmBackend(T, STI, Endian), OSABI(OSABI) {}

std::unique_ptr<MCObjectTargetWriter>
createObjectTargetWriter() const override {
return createARMELFObjectWriter(OSABI, isAndroid);
return createARMELFObjectWriter(OSABI);
}
};
}
Expand Down
20 changes: 8 additions & 12 deletions lib/Target/ARM/MCTargetDesc/ARMELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,13 @@ using namespace llvm;
namespace {

class ARMELFObjectWriter : public MCELFObjectTargetWriter {
// LDC
const bool isAndroid;

enum { DefaultEABIVersion = 0x05000000U };

unsigned GetRelocTypeInner(const MCValue &Target, const MCFixup &Fixup,
bool IsPCRel, MCContext &Ctx) const;

public:
ARMELFObjectWriter(uint8_t OSABI, bool IsAndroid);
ARMELFObjectWriter(uint8_t OSABI);

~ARMELFObjectWriter() override = default;

Expand All @@ -47,10 +44,10 @@ namespace {

} // end anonymous namespace

ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI, bool IsAndroid)
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
/*HasRelocationAddend*/ false),
isAndroid(IsAndroid) {}
ARMELFObjectWriter::ARMELFObjectWriter(uint8_t OSABI)
: MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI,
ELF::EM_ARM,
/*HasRelocationAddend*/ false) {}

bool ARMELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &Sym,
unsigned Type) const {
Expand Down Expand Up @@ -167,8 +164,7 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
case MCSymbolRefExpr::VK_GOT:
return ELF::R_ARM_GOT_BREL;
case MCSymbolRefExpr::VK_TLSGD:
// LDC
return isAndroid ? ELF::R_ARM_GOT_PREL : ELF::R_ARM_TLS_GD32;
return ELF::R_ARM_TLS_GD32;
case MCSymbolRefExpr::VK_TPOFF:
return ELF::R_ARM_TLS_LE32;
case MCSymbolRefExpr::VK_GOTTPOFF:
Expand Down Expand Up @@ -241,6 +237,6 @@ unsigned ARMELFObjectWriter::GetRelocTypeInner(const MCValue &Target,
}

std::unique_ptr<MCObjectTargetWriter>
llvm::createARMELFObjectWriter(uint8_t OSABI, bool IsAndroid) {
return llvm::make_unique<ARMELFObjectWriter>(OSABI, IsAndroid);
llvm::createARMELFObjectWriter(uint8_t OSABI) {
return llvm::make_unique<ARMELFObjectWriter>(OSABI);
}
3 changes: 1 addition & 2 deletions lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,7 @@ MCStreamer *createARMWinCOFFStreamer(MCContext &Context,
bool IncrementalLinkerCompatible);

/// Construct an ELF Mach-O object writer.
std::unique_ptr<MCObjectTargetWriter> createARMELFObjectWriter(uint8_t OSABI,
bool IsAndroid);
std::unique_ptr<MCObjectTargetWriter> createARMELFObjectWriter(uint8_t OSABI);

/// Construct an ARM Mach-O object writer.
std::unique_ptr<MCObjectTargetWriter>
Expand Down
8 changes: 1 addition & 7 deletions lib/Target/X86/MCTargetDesc/X86ELFObjectWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCFixup.h"
#include "llvm/MC/MCObjectFileInfo.h" // LDC
#include "llvm/MC/MCObjectWriter.h"
#include "llvm/MC/MCValue.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -261,12 +260,7 @@ static unsigned getRelocType32(MCContext &Ctx,
case MCSymbolRefExpr::VK_TLSGD:
assert(Type == RT32_32);
assert(!IsPCRel);
// LDC
{
auto ofi = Ctx.getObjectFileInfo();
return ofi && ofi->getTargetTriple().isAndroid() ? ELF::R_386_GOT32
: ELF::R_386_TLS_GD;
}
return ELF::R_386_TLS_GD;
case MCSymbolRefExpr::VK_GOTTPOFF:
assert(Type == RT32_32);
assert(!IsPCRel);
Expand Down
3 changes: 3 additions & 0 deletions lib/Target/X86/X86ISelLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16037,6 +16037,9 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
bool PositionIndependent = isPositionIndependent();

if (Subtarget.isTargetELF()) {
if (Subtarget.isTargetAndroid())
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, Subtarget.is64Bit()); // LDC

TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
switch (model) {
case TLSModel::GeneralDynamic:
Expand Down