Skip to content

Commit 681a48e

Browse files
Add support for TLS variables
If the externalized variable is Thread Local Storage (TLS), the externalizer must flag this accordingly and the .dsc file generator must emit the correct token for that. Signed-off-by: Giuliano Belinassi <gbelinassi@suse.de>
1 parent 19c1329 commit 681a48e

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

libcextract/DscFileGenerator.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,29 @@ void DscFileGenerator::Global_Functions(void)
9292
}
9393
}
9494

95+
static bool Is_TLS(VarDecl *decl)
96+
{
97+
return decl->getTLSKind() == VarDecl::TLS_None ? false : true;
98+
}
99+
100+
static bool Is_TLS(const DeclContext::lookup_result &decls)
101+
{
102+
bool is_tls = false;
103+
104+
for (auto it = decls.begin(); it != decls.end(); ++it) {
105+
if (VarDecl *vdecl = dyn_cast<VarDecl>(*it)) {
106+
is_tls |= Is_TLS(vdecl);
107+
}
108+
}
109+
110+
return is_tls;
111+
}
112+
113+
static std::string Get_TLS_Token(bool predicate)
114+
{
115+
return predicate == true ? "%" : "";
116+
}
117+
95118
void DscFileGenerator::Local_Symbols(void)
96119
{
97120
TranslationUnitDecl *tu = AST->getASTContext().getTranslationUnitDecl();
@@ -102,9 +125,11 @@ void DscFileGenerator::Local_Symbols(void)
102125
DeclContext::lookup_result decls = tu->lookup(
103126
DeclarationName(&idtbl.get(entry.NewName)));
104127
if (decls.empty()) {
105-
throw std::runtime_error("Unable to find symbol " + entry.NewName + " in the AST");
128+
throw std::runtime_error("Unable to find symbol " + entry.NewName +
129+
" in the AST");
106130
}
107-
Out << "\n#" << entry.OldName << ":" << entry.NewName;
131+
Out << "\n#" << Get_TLS_Token(Is_TLS(decls)) << entry.OldName << ":" <<
132+
entry.NewName;
108133
std::string mod = IA.Get_Symbol_Module(entry.OldName);
109134
if (!mod.empty())
110135
Out << ":" << mod;

libcextract/SymbolExternalizer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,12 @@ VarDecl *SymbolExternalizer::Create_Externalized_Var(DeclaratorDecl *decl, const
646646
ret->addAttr(UsedAttr::Create(astctx));
647647
}
648648

649+
/* In case the original Decl has TLS storage, we must copy it to the new
650+
variable as well. */
651+
if (VarDecl *vdecl = dyn_cast<VarDecl>(decl)) {
652+
ret->setTSCSpec(vdecl->getTSCSpec());
653+
}
654+
649655
/* return node. */
650656
return ret;
651657
}

testsuite/small/tls-1.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=set_errno -DCE_EXPORT_SYMBOLS=__libc_errno" }*/
2+
3+
extern __thread int __libc_errno __attribute__ ((tls_model ("initial-exec")));
4+
5+
void set_errno(int err)
6+
{
7+
__libc_errno = err;
8+
}
9+
10+
/* { dg-final { scan-tree-dump "__attribute__\(\(used\)\) static __thread int \*klpe___libc_errno;|static __thread int \*klpe___libc_errno __attribute__\(\(used\)\);" } } */
11+
/* { dg-final { scan-tree-dump "\(\*klpe___libc_errno\) = err" } } */

0 commit comments

Comments
 (0)