Skip to content

Commit bebf235

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 878c8c7 commit bebf235

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
@@ -5159,6 +5159,9 @@ class TargetLowering : public TargetLoweringBase {
51595159
virtual SDValue LowerToTLSEmulatedModel(const GlobalAddressSDNode *GA,
51605160
SelectionDAG &DAG) const;
51615161

5162+
SDValue LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
5163+
SelectionDAG &DAG, bool is64bit) const; // LDC
5164+
51625165
/// Expands target specific indirect branch for the case of JumpTable
51635166
/// expanasion.
51645167
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
@@ -9421,6 +9421,33 @@ SDValue TargetLowering::getVectorSubVecPointer(SelectionDAG &DAG,
94219421
return DAG.getMemBasePlusOffset(VecPtr, Index, dl);
94229422
}
94239423

9424+
SDValue
9425+
TargetLowering::LowerToAndroidEmulatedTLSAddress(SDValue Op, SDValue Result,
9426+
SelectionDAG &DAG, bool is64bit) const { // LDC
9427+
SDLoc DL(Op);
9428+
SDValue Chain = DAG.getEntryNode();
9429+
ArgListTy Args;
9430+
ArgListEntry Entry;
9431+
Type *Ty;
9432+
if (is64bit)
9433+
Ty = (Type *)Type::getInt64Ty(*DAG.getContext());
9434+
else
9435+
Ty = (Type *)Type::getInt32Ty(*DAG.getContext());
9436+
Entry.Node = Result;
9437+
Entry.Ty = Ty;
9438+
Args.push_back(Entry);
9439+
9440+
// copied, modified from ARMTargetLowering::LowerToTLSGeneralDynamicModel
9441+
TargetLowering::CallLoweringInfo CLI(DAG);
9442+
CLI.setDebugLoc(DL).setChain(Chain).setLibCallee(
9443+
CallingConv::C, Ty,
9444+
DAG.getExternalSymbol("__tls_get_addr",
9445+
getPointerTy(DAG.getDataLayout())),
9446+
std::move(Args));
9447+
std::pair<SDValue, SDValue> CallResult = LowerCallTo(CLI);
9448+
return CallResult.first;
9449+
}
9450+
94249451
//===----------------------------------------------------------------------===//
94259452
// Implementation of Emulated TLS Model
94269453
//===----------------------------------------------------------------------===//

llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ static unsigned getELFSectionType(StringRef Name, SectionKind K) {
541541
return ELF::SHT_PROGBITS;
542542
}
543543

544-
static unsigned getELFSectionFlags(SectionKind K) {
544+
static unsigned getELFSectionFlags(SectionKind K, const Triple &TargetTriple) {
545545
unsigned Flags = 0;
546546

547547
if (!K.isMetadata() && !K.isExclude())
@@ -559,7 +559,7 @@ static unsigned getELFSectionFlags(SectionKind K) {
559559
if (K.isWriteable())
560560
Flags |= ELF::SHF_WRITE;
561561

562-
if (K.isThreadLocal())
562+
if (K.isThreadLocal() && !TargetTriple.isAndroid()) // LDC
563563
Flags |= ELF::SHF_TLS;
564564

565565
if (K.isMergeableCString() || K.isMergeableConst())
@@ -807,7 +807,7 @@ static MCSection *selectExplicitSectionGlobal(
807807

808808
StringRef Group = "";
809809
bool IsComdat = false;
810-
unsigned Flags = getELFSectionFlags(Kind);
810+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
811811
if (const Comdat *C = getELFComdat(GO)) {
812812
Group = C->getName();
813813
IsComdat = C->getSelectionKind() == Comdat::Any;
@@ -921,7 +921,7 @@ static MCSection *selectELFSectionForGlobal(
921921

922922
MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
923923
const GlobalObject *GO, SectionKind Kind, const TargetMachine &TM) const {
924-
unsigned Flags = getELFSectionFlags(Kind);
924+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
925925

926926
// If we have -ffunction-section or -fdata-section then we should emit the
927927
// global value to a uniqued section specifically for it.
@@ -941,7 +941,7 @@ MCSection *TargetLoweringObjectFileELF::SelectSectionForGlobal(
941941
MCSection *TargetLoweringObjectFileELF::getUniqueSectionForFunction(
942942
const Function &F, const TargetMachine &TM) const {
943943
SectionKind Kind = SectionKind::getText();
944-
unsigned Flags = getELFSectionFlags(Kind);
944+
unsigned Flags = getELFSectionFlags(Kind, TM.getTargetTriple());
945945
// If the function's section names is pre-determined via pragma or a
946946
// section attribute, call selectExplicitSectionGlobal.
947947
if (F.hasSection() || F.hasFnAttribute("implicit-section-name"))

llvm/lib/MC/MCELFStreamer.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,9 @@ void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
474474
break;
475475
}
476476
getAssembler().registerSymbol(symRef.getSymbol());
477-
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
477+
// LDC
478+
if (!getContext().getTargetTriple().isAndroid())
479+
cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
478480
break;
479481
}
480482

llvm/lib/MC/MCObjectFileInfo.cpp

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

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

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

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

llvm/lib/Target/AArch64/AArch64ISelLowering.cpp

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

83518351
if (Subtarget->isTargetDarwin())
83528352
return LowerDarwinGlobalTLSAddress(Op, DAG);
8353-
if (Subtarget->isTargetELF())
8354-
return LowerELFGlobalTLSAddress(Op, DAG);
8353+
if (Subtarget->isTargetELF()) {
8354+
if (Subtarget->isTargetAndroid())
8355+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, true); // LDC
8356+
else
8357+
return LowerELFGlobalTLSAddress(Op, DAG);
8358+
}
83558359
if (Subtarget->isTargetWindows())
83568360
return LowerWindowsGlobalTLSAddress(Op, DAG);
83578361

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
@@ -3746,6 +3746,8 @@ ARMTargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) const {
37463746

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

37513753
switch (model) {

llvm/lib/Target/X86/X86ISelLowering.cpp

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

2089520895
if (Subtarget.isTargetELF()) {
20896+
if (Subtarget.isTargetAndroid())
20897+
return LowerToAndroidEmulatedTLSAddress(Op, LowerGlobalAddress(Op, DAG), DAG, Subtarget.is64Bit()); // LDC
20898+
2089620899
TLSModel::Model model = DAG.getTarget().getTLSModel(GV);
2089720900
switch (model) {
2089820901
case TLSModel::GeneralDynamic:

0 commit comments

Comments
 (0)