Skip to content

Commit abddd50

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 0f0573f commit abddd50

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

llvm/include/llvm/CodeGen/TargetLowering.h

+3
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,9 @@ class TargetLowering : public TargetLoweringBase {
49464946
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
49474947
SelectionDAG &DAG) const;
49484948

4949+
SDValue LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
4950+
SelectionDAG &DAG, bool is64bit) const; // LDC
4951+
49494952
/// Expands target specific indirect branch for the case of JumpTable
49504953
/// expanasion.
49514954
virtual SDValue expandIndirectJTBranch(const SDLoc& dl, SDValue Value, SDValue Addr,

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

+27
Original file line numberDiff line numberDiff line change
@@ -8751,6 +8751,33 @@ SDValue TargetLowering::getVectorSubVecPointer(SelectionDAG &DAG,
87518751
return DAG.getMemBasePlusOffset(VecPtr, Index, dl);
87528752
}
87538753

8754+
SDValue
8755+
TargetLowering::LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
8756+
SelectionDAG &DAG, bool is64bit) const { // LDC
8757+
SDLoc DL(Op);
8758+
SDValue Chain = DAG.getEntryNode();
8759+
ArgListTy Args;
8760+
ArgListEntry Entry;
8761+
Type *Ty;
8762+
if (is64bit)
8763+
Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
8764+
else
8765+
Ty = (Type *)Type::getInt32Ty(*DAG.getContext());
8766+
Entry.Node = Result;
8767+
Entry.Ty = Ty;
8768+
Args.push_back(Entry);
8769+
8770+
// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
8771+
TargetLowering::CallLoweringInfo CLI(DAG);
8772+
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
8773+
CallingConv::C, Ty,
8774+
DAG.getExternalSymbol("__tls_get_addr",
8775+
getPointerTy(DAG.getDataLayout())),
8776+
std::move(Args));
8777+
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
8778+
return CallResult.first;
8779+
}
8780+
87548781
//===----------------------------------------------------------------------===//
87558782
// Implementation of Emulated TLS Model
87568783
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
507507
return ELF::SHT_PROGBITS;
508508
}
509509

510-
static unsigned getELFSectionFlags(SectionKind K) {
510+
static unsigned getELFSectionFlags(SectionKind K, const Triple &TargetTriple) {
511511
unsigned Flags = 0;
512512

513513
if (!K.isMetadata() && !K.isExclude())
@@ -525,7 +525,7 @@ static unsigned getELFSectionFlags(SectionKind K) {
525525
if (K.isWriteable())
526526
Flags |= ELF::SHF_WRITE;
527527

528-
if (K.isThreadLocal())
528+
if (K.isThreadLocal() && !TargetTriple.isAndroid()) // LDC
529529
Flags |= ELF::SHF_TLS;
530530

531531
if (K.isMergeableCString() || K.isMergeableConst())
@@ -773,7 +773,7 @@ static MCSection *selectExplicitSectionGlobal(
773773

774774
StringRef Group = "";
775775
bool IsComdat = false;
776-
unsigned Flags = getELFSectionFlags(Kind);
776+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
777777
if (const Comdat *C = getELFComdat(GO)) {
778778
Group = C->getName();
779779
IsComdat = C->getSelectionKind() == Comdat::Any;
@@ -887,7 +887,7 @@ static MCSection *selectELFSectionForGlobal(
887887

888888
MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
889889
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
890-
unsigned Flags = getELFSectionFlags(Kind);
890+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
891891

892892
// If we have -ffunction-section or -fdata-section then we should emit the
893893
// global value to a uniqued section specifically for it.
@@ -907,7 +907,7 @@ MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
907907
MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
908908
const Function &F, const TargetMachine &TM) const {
909909
SectionKind Kind = SectionKind::getText();
910-
unsigned Flags = getELFSectionFlags(Kind);
910+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
911911
// If the function's section names is pre-determined via pragma or a
912912
// section attribute, call selectExplicitSectionGlobal.
913913
if (F.hasSection() || F.hasFnAttribute("implicit-section-name"))

llvm/lib/MC/MCELFStreamer.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
470470
break;
471471
}
472472
getAssembler().registerSymbol(symRef.getSymbol());
473-
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
473+
// LDC
474+
if (!getContext().getTargetTriple().isAndroid())
475+
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
474476
break;
475477
}
476478

llvm/lib/MC/MCObjectFileInfo.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -389,12 +389,15 @@ void MCObjectFileInfo::initELFMCObjectFileInfo(const Triple &T, bool Large) {
389389
ReadOnlySection =
390390
Ctx->getELFSection(".rodata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC);
391391

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

396-
TLSBSSSection = Ctx->getELFSection(
397-
".tbss", ELF::SHT_NOBITS, ELF::SHF_ALLOC | ELF::SHF_TLS | ELF::SHF_WRITE);
396+
TLSDataSection = Ctx->getELFSection(
397+
".tdata", ELF::SHT_PROGBITS, ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
398+
399+
TLSBSSSection = Ctx->getELFSection(".tbss", ELF::SHT_NOBITS,
400+
ELF::SHF_ALLOC | tlsFlag | ELF::SHF_WRITE);
398401

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

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -7555,8 +7555,12 @@ SDValue AArch64TargetLowering::LowerGlobalTLSAddress(SDValue Op,
75557555

75567556
if (Subtarget->isTargetDarwin())
75577557
return LowerDarwinGlobalTLSAddress(Op, DAG);
7558-
if (Subtarget->isTargetELF())
7559-
return LowerELFGlobalTLSAddress(Op, DAG);
7558+
if (Subtarget->isTargetELF()) {
7559+
if (Subtarget->isTargetAndroid())
7560+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, true); // LDC
7561+
else
7562+
return LowerELFGlobalTLSAddress(Op, DAG);
7563+
}
75607564
if (Subtarget->isTargetWindows())
75617565
return LowerWindowsGlobalTLSAddress(Op, DAG);
75627566

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

+4-1
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"
@@ -131,7 +132,9 @@ static void fixELFSymbolsInTLSFixupsImpl(const MCExpr *Expr, MCAssembler &Asm) {
131132
// We're known to be under a TLS fixup, so any symbol should be
132133
// modified. There should be only one.
133134
const MCSymbolRefExpr &SymRef = *cast<MCSymbolRefExpr>(Expr);
134-
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
135+
// LDC
136+
if (!Asm.getContext().getTargetTriple().isAndroid())
137+
cast<MCSymbolELF>(SymRef.getSymbol()).setType(ELF::STT_TLS);
135138
break;
136139
}
137140

llvm/lib/Target/ARM/ARMISelLowering.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -3719,6 +3719,8 @@ ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
37193719

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

37243726
switch (model) {

llvm/lib/Target/X86/X86ISelLowering.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20519,6 +20519,9 @@ X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
2051920519
bool PositionIndependent = isPositionIndependent();
2052020520

2052120521
if (Subtarget.isTargetELF()) {
20522+
if (Subtarget.isTargetAndroid())
20523+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, Subtarget.is64Bit()); // LDC
20524+
2052220525
TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2052320526
switch (model) {
2052420527
case TLSModel::GeneralDynamic:

0 commit comments

Comments
 (0)