Skip to content

[clang] Fix std::tm etc. mangling on Solaris #106353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,10 @@ OpenACC Specific Changes
Target Specific Changes
-----------------------

- Clang now implements the Solaris-specific mangling of ``std::tm`` as
``tm``, same for ``std::div_t``, ``std::ldiv_t``, and
``std::lconv``, for Solaris ABI compatibility. (#GH33114)

AMDGPU Support
^^^^^^^^^^^^^^

Expand Down
19 changes: 18 additions & 1 deletion clang/lib/AST/ItaniumMangle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1161,8 +1161,25 @@ void CXXNameMangler::mangleUnscopedName(GlobalDecl GD, const DeclContext *DC,
// ::= St <unqualified-name> # ::std::

assert(!isa<LinkageSpecDecl>(DC) && "unskipped LinkageSpecDecl");
if (isStdNamespace(DC))
if (isStdNamespace(DC)) {
if (getASTContext().getTargetInfo().getTriple().isOSSolaris()) {
const NamedDecl *ND = cast<NamedDecl>(GD.getDecl());
if (const RecordDecl *RD = dyn_cast<RecordDecl>(ND)) {
// Issue #33114: Need non-standard mangling of std::tm etc. for
// Solaris ABI compatibility.
//
// <substitution> ::= tm # ::std::tm, same for the others
if (const IdentifierInfo *II = RD->getIdentifier()) {
StringRef type = II->getName();
if (llvm::is_contained({"div_t", "ldiv_t", "lconv", "tm"}, type)) {
Out << type.size() << type;
return;
}
}
}
}
Out << "St";
}

mangleUnqualifiedName(GD, DC, AdditionalAbiTags);
}
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Lex/PPMacroExpansion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1604,10 +1604,12 @@ static bool isTargetVariantEnvironment(const TargetInfo &TI,
return false;
}

#if defined(__sun__) && defined(__svr4__)
#if defined(__sun__) && defined(__svr4__) && defined(__clang__) && \
__clang__ < 20
Comment on lines +1607 to +1608
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does every version of gcc we support handles that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Certainly: the fix for GCC PR libstdc++-v3/1773 went in 13 years ago. That should be good enough ;-)

// GCC mangles std::tm as tm for binary compatibility on Solaris (Issue
// #33114). We need to match this to allow the std::put_time calls to link
// (PR #99075).
// (PR #99075). clang 20 contains a fix, but the workaround is still needed
// with older versions.
asm("_ZNKSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE3putES3_"
"RSt8ios_basecPKSt2tmPKcSB_ = "
"_ZNKSt8time_putIcSt19ostreambuf_iteratorIcSt11char_traitsIcEEE3putES3_"
Expand Down
34 changes: 34 additions & 0 deletions clang/test/AST/solaris-tm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Check that std::tm and a few others are mangled as tm on Solaris only.
/// Issue #33114.
///
// RUN: %clang_cc1 -emit-llvm %s -o - -triple amd64-pc-solaris2.11 | FileCheck --check-prefix=CHECK-SOLARIS %s
// RUN: %clang_cc1 -emit-llvm %s -o - -triple x86_64-unknown-linux-gnu | FileCheck --check-prefix=CHECK-LINUX %s
//
// REQUIRES: x86-registered-target

namespace std {
extern "C" {
struct tm {
int tm_sec;
};
struct ldiv_t {
long quot;
};
}
}

// CHECK-SOLARIS: @_Z6tmfunc2tm
// CHECK-SOLARIS: @_Z9tmccpfunc2tmPKcS1_
// CHECK-SOLARIS: @_Z7tm2func2tmS_
// CHECK-LINUX: @_Z6tmfuncSt2tm
// CHECK-LINUX: @_Z9tmccpfuncSt2tmPKcS1_
// CHECK-LINUX: @_Z7tm2funcSt2tmS_

void tmfunc (std::tm tm) {}
void tmccpfunc (std::tm tm, const char *ccp, const char *ccp2) {}
void tm2func (std::tm tm, std::tm tm2) {}

// CHECK-SOLARIS: @_Z7ldtfunc6ldiv_t
// CHECK-LINUX: @_Z7ldtfuncSt6ldiv_t

void ldtfunc (std::ldiv_t ldt) {}
Loading