Skip to content

Commit e68ba96

Browse files
committed
[LDC] Custom TLS emulation for Android targets
The Bionic C library ignores thread-local data stored in the normal .tbss/.tdata ELF sections, which are marked with the SHF_TLS/STT_TLS flags. LDC rolls its own emulated TLS scheme for Android instead, by keeping TLS data in the .tdata/.tbss sections but removing the SHF_TLS/STT_TLS flags, and replacing direct access to these globals by a call to __tls_get_addr() (implemented in druntime's rt.sections_android). The function is expected to translate an address in the TLS static data to the corresponding address in the actual TLS dynamic per-thread data.
1 parent bf27c75 commit e68ba96

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4629,6 +4629,9 @@ class TargetLowering : public TargetLoweringBase {
46294629
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
46304630
SelectionDAG &DAG) const;
46314631

4632+
SDValue LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
4633+
SelectionDAG &DAG, bool is64bit) const; // LDC
4634+
46324635
/// Expands target specific indirect branch for the case of JumpTable
46334636
/// expanasion.
46344637
virtual SDValue expandIndirectJTBranch(const SDLoc& dl, SDValue Value, SDValue Addr,

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7872,6 +7872,33 @@ SDValue TargetLowering::getVectorSubVecPointer(SelectionDAG &DAG,
78727872
return DAG.getMemBasePlusOffset(VecPtr, Index, dl);
78737873
}
78747874

7875+
SDValue
7876+
TargetLowering::LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
7877+
SelectionDAG &DAG, bool is64bit) const { // LDC
7878+
SDLoc DL(Op);
7879+
SDValue Chain = DAG.getEntryNode();
7880+
ArgListTy Args;
7881+
ArgListEntry Entry;
7882+
Type *Ty;
7883+
if (is64bit)
7884+
Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
7885+
else
7886+
Ty = (Type *)Type::getInt32Ty(*DAG.getContext());
7887+
Entry.Node = Result;
7888+
Entry.Ty = Ty;
7889+
Args.push_back(Entry);
7890+
7891+
// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
7892+
TargetLowering::CallLoweringInfo CLI(DAG);
7893+
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
7894+
CallingConv::C, Ty,
7895+
DAG.getExternalSymbol("__tls_get_addr",
7896+
getPointerTy(DAG.getDataLayout())),
7897+
std::move(Args));
7898+
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
7899+
return CallResult.first;
7900+
}
7901+
78757902
//===----------------------------------------------------------------------===//
78767903
// Implementation of Emulated TLS Model
78777904
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
500500
return ELF::SHT_PROGBITS;
501501
}
502502

503-
static unsigned getELFSectionFlags(SectionKind K) {
503+
static unsigned getELFSectionFlags(SectionKind K, const Triple &TargetTriple) {
504504
unsigned Flags = 0;
505505

506506
if (!K.isMetadata())
@@ -515,7 +515,7 @@ static unsigned getELFSectionFlags(SectionKind K) {
515515
if (K.isWriteable())
516516
Flags |= ELF::SHF_WRITE;
517517

518-
if (K.isThreadLocal())
518+
if (K.isThreadLocal() && !TargetTriple.isAndroid()) // LDC
519519
Flags |= ELF::SHF_TLS;
520520

521521
if (K.isMergeableCString() || K.isMergeableConst())
@@ -762,7 +762,7 @@ static MCSection *selectExplicitSectionGlobal(
762762

763763
StringRef Group = "";
764764
bool IsComdat = false;
765-
unsigned Flags = getELFSectionFlags(Kind);
765+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
766766
if (const Comdat *C = getELFComdat(GO)) {
767767
Group = C->getName();
768768
IsComdat = C->getSelectionKind() == Comdat::Any;
@@ -873,7 +873,7 @@ static MCSection *selectELFSectionForGlobal(
873873

874874
MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
875875
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
876-
unsigned Flags = getELFSectionFlags(Kind);
876+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
877877

878878
// If we have -ffunction-section or -fdata-section then we should emit the
879879
// global value to a uniqued section specifically for it.
@@ -893,7 +893,7 @@ MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
893893
MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
894894
const Function &F, const TargetMachine &TM) const {
895895
SectionKind Kind = SectionKind::getText();
896-
unsigned Flags = getELFSectionFlags(Kind);
896+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
897897
// If the function's section names is pre-determined via pragma or a
898898
// section attribute, call selectExplicitSectionGlobal.
899899
if (F.hasSection() || F.hasFnAttribute("implicit-section-name"))

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
468468
break;
469469
}
470470
getAssembler().registerSymbol(symRef.getSymbol());
471-
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
471+
// LDC
472+
if (!getContext().getTargetTriple().isAndroid())
473+
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
472474
break;
473475
}
474476

llvm/lib/MC/MCObjectFileInfo.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,12 +369,15 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
369369
ReadOnlySection =
370370
Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
371371

372-
TLSDataSection =
373-
Ctx->getELFSection(".tdata", ELF::SHT_PROGBITS,
374-
ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
372+
// LDC
373+
const auto tlsFlag =
374+
(!getContext().getTargetTriple().isAndroid() ? ELF::SHF_TLS : 0);
375375

376-
TLSBSSSection = Ctx->getELFSection(
377-
".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
376+
TLSDataSection = Ctx->getELFSection(
377+
".tdata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
378+
379+
TLSBSSSection = Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
380+
ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
378381

379382
DataRelROSection = Ctx->getELFSection(".data.rel.ro", ELF::SHT_PROGBITS,
380383
ELF::SHF_ALLOC | ELF::SHF_WRITE);

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6827,8 +6827,12 @@ SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
68276827

68286828
if (Subtarget->isTargetDarwin())
68296829
return LowerDarwinGlobalTLSAddress(Op, DAG);
6830-
if (Subtarget->isTargetELF())
6831-
return LowerELFGlobalTLSAddress(Op, DAG);
6830+
if (Subtarget->isTargetELF()) {
6831+
if (Subtarget->isTargetAndroid())
6832+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, true); // LDC
6833+
else
6834+
return LowerELFGlobalTLSAddress(Op, DAG);
6835+
}
68326836
if (Subtarget->isTargetWindows())
68336837
return LowerWindowsGlobalTLSAddress(Op, DAG);
68346838

llvm/lib/Target/AArch64/MCTargetDesc/AArch64MCExpr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include "AArch64MCExpr.h"
1515
#include "llvm/BinaryFormat/ELF.h"
16+
#include "llvm/MC/MCAssembler.h" // LDC
1617
#include "llvm/MC/MCContext.h"
1718
#include "llvm/MC/MCStreamer.h"
1819
#include "llvm/MC/MCSymbolELF.h"
@@ -130,7 +131,9 @@ static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
130131
// We're known to be under a TLS fixup, so any symbol should be
131132
// modified. There should be only one.
132133
const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
133-
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
134+
// LDC
135+
if (!Asm.getContext().getTargetTriple().isAndroid())
136+
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
134137
break;
135138
}
136139

llvm/lib/Target/ARM/ARMISelLowering.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3653,6 +3653,8 @@ ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
36533653

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

36583660
switch (model) {

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19542,6 +19542,9 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
1954219542
bool PositionIndependent = isPositionIndependent();
1954319543

1954419544
if (Subtarget.isTargetELF()) {
19545+
if (Subtarget.isTargetAndroid())
19546+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, Subtarget.is64Bit()); // LDC
19547+
1954519548
TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
1954619549
switch (model) {
1954719550
case TLSModel::GeneralDynamic:

0 commit comments

Comments
 (0)