@@ -520,6 +520,13 @@ static bool isZeroInit(const SectionRef Section) {
520
520
SectionType == MachO::S_GB_ZEROFILL;
521
521
}
522
522
523
+ static bool isTLS (const SectionRef Section) {
524
+ const ObjectFile *Obj = Section.getObject ();
525
+ if (isa<object::ELFObjectFileBase>(Obj))
526
+ return ELFSectionRef (Section).getFlags () & ELF::SHF_TLS;
527
+ return false ;
528
+ }
529
+
523
530
// Compute an upper bound of the memory size that is required to load all
524
531
// sections
525
532
Error RuntimeDyldImpl::computeTotalAllocSize (const ObjectFile &Obj,
@@ -549,6 +556,7 @@ Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
549
556
unsigned Alignment = (unsigned )Alignment64 & 0xffffffffL ;
550
557
bool IsCode = Section.isText ();
551
558
bool IsReadOnly = isReadOnlyData (Section);
559
+ bool IsTLS = isTLS (Section);
552
560
553
561
Expected<StringRef> NameOrErr = Section.getName ();
554
562
if (!NameOrErr)
@@ -582,7 +590,7 @@ Error RuntimeDyldImpl::computeTotalAllocSize(const ObjectFile &Obj,
582
590
} else if (IsReadOnly) {
583
591
RODataAlign = std::max (RODataAlign, Alignment);
584
592
ROSectionSizes.push_back (SectionSize);
585
- } else {
593
+ } else if (!IsTLS) {
586
594
RWDataAlign = std::max (RWDataAlign, Alignment);
587
595
RWSectionSizes.push_back (SectionSize);
588
596
}
@@ -800,6 +808,7 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
800
808
bool IsVirtual = Section.isVirtual ();
801
809
bool IsZeroInit = isZeroInit (Section);
802
810
bool IsReadOnly = isReadOnlyData (Section);
811
+ bool IsTLS = isTLS (Section);
803
812
uint64_t DataSize = Section.getSize ();
804
813
805
814
// An alignment of 0 (at least with ELF) is identical to an alignment of 1,
@@ -823,6 +832,7 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
823
832
uintptr_t Allocate;
824
833
unsigned SectionID = Sections.size ();
825
834
uint8_t *Addr;
835
+ uint64_t LoadAddress = 0 ;
826
836
const char *pData = nullptr ;
827
837
828
838
// If this section contains any bits (i.e. isn't a virtual or bss section),
@@ -851,10 +861,17 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
851
861
Allocate = DataSize + PaddingSize + StubBufSize;
852
862
if (!Allocate)
853
863
Allocate = 1 ;
854
- Addr = IsCode ? MemMgr.allocateCodeSection (Allocate, Alignment, SectionID,
855
- Name)
856
- : MemMgr.allocateDataSection (Allocate, Alignment, SectionID,
857
- Name, IsReadOnly);
864
+ if (IsTLS) {
865
+ auto TLSSection =
866
+ MemMgr.allocateTLSSection (Allocate, Alignment, SectionID, Name);
867
+ Addr = TLSSection.InitializationImage ;
868
+ LoadAddress = TLSSection.Offset ;
869
+ } else if (IsCode) {
870
+ Addr = MemMgr.allocateCodeSection (Allocate, Alignment, SectionID, Name);
871
+ } else {
872
+ Addr = MemMgr.allocateDataSection (Allocate, Alignment, SectionID, Name,
873
+ IsReadOnly);
874
+ }
858
875
if (!Addr)
859
876
report_fatal_error (" Unable to allocate section memory!" );
860
877
@@ -897,6 +914,10 @@ RuntimeDyldImpl::emitSection(const ObjectFile &Obj,
897
914
Sections.push_back (
898
915
SectionEntry (Name, Addr, DataSize, Allocate, (uintptr_t )pData));
899
916
917
+ // The load address of a TLS section is not equal to the address of its
918
+ // initialization image
919
+ if (IsTLS)
920
+ Sections.back ().setLoadAddress (LoadAddress);
900
921
// Debug info sections are linked as if their load address was zero
901
922
if (!IsRequired)
902
923
Sections.back ().setLoadAddress (0 );
@@ -1261,6 +1282,14 @@ uint64_t RuntimeDyld::LoadedObjectInfo::getSectionLoadAddress(
1261
1282
return 0 ;
1262
1283
}
1263
1284
1285
+ RuntimeDyld::MemoryManager::TLSSection
1286
+ RuntimeDyld::MemoryManager::allocateTLSSection (uintptr_t Size,
1287
+ unsigned Alignment,
1288
+ unsigned SectionID,
1289
+ StringRef SectionName) {
1290
+ report_fatal_error (" allocation of TLS not implemented" );
1291
+ }
1292
+
1264
1293
void RuntimeDyld::MemoryManager::anchor () {}
1265
1294
void JITSymbolResolver::anchor () {}
1266
1295
void LegacyJITSymbolResolver::anchor () {}
0 commit comments