Bug 67343 - C++ mangler does not follow ABI for unresolved names in expressions
Summary: C++ mangler does not follow ABI for unresolved names in expressions
Status: ASSIGNED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: ---
Assignee: Jason Merrill
URL:
Keywords: ABI, wrong-code
: 88413 89818 109887 (view as bug list)
Depends on:
Blocks: 85648
  Show dependency treegraph
 
Reported: 2015-08-24 22:23 UTC by Ian Lance Taylor
Modified: 2024-04-29 19:04 UTC (History)
9 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2020-08-29 00:00:00


Attachments
WIP Fix (6.29 KB, patch)
2020-12-22 02:33 UTC, Jason Merrill
Details | Diff
Follow-on patch (2.72 KB, patch)
2020-12-22 02:34 UTC, Jason Merrill
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lance Taylor 2015-08-24 22:23:42 UTC
If I compile this C++ file with current mainline

template<bool B> class H {
 public:
  enum T { V = B };
};

template <typename T> class C {
 public:
  enum { B1 = true, B2 = false };
};

template<typename T>
void f(typename H<C<T>::B1 && !C<T>::B2>::T) {}

void g() {
  f<int>(H<true>::V);
}

I get this symbol:

_Z1fIiEvN1HIXaasr1CIT_E2B1ntsrS3_2B2EE1TE

I believe that this symbol is not mangled according to the current C++ mangling ABI.  The relevant part is the expression Xaasr1CIT_E2B1ntsrS3_2B2E, specifically the local name sr1CIT_E2B1.  The mangling ABI says

<unresolved-name> ::= [gs] <base-unresolved-name>
                  ::= sr <unresolved-type> <base-unresolved-name>
                  ::= srN <unresolved-type> <unresolved-qualifier-level>+ E <base-unresolved-name>
                  ::= [gs] sr <unresolved-qualifier-level>+ E <base-unresolved-name>


<unresolved-type> ::= <template-param>
                  ::= <decltype>
                  ::= <substitution>

The string after "sr" is 1CIT_E2B1.  The 1C is not a template-param, a decltype, or a substitution.  Therefore we must be using the fourth expansion of <unresolved-name>, where we expect to see a series of <unresolved-qualifier-level> entries, followed by an E, followed by a <base-unresolved-name>.  But in fact what we see here is <type> <unqualified-name>.  This is what the code in cp/mangle.c outputs:

	  write_string ("sr");
	  write_type (scope);
	  write_member_name (member);

Either the mangling ABI or GCC itself is wrong.
Comment 1 Ian Lance Taylor 2015-08-24 22:26:19 UTC
For the record, when the same file is compiled with clang, the symbol is _Z1fIiEvN1HIXaasr1CIT_EE2B1ntsr1CIS1_EE2B2EE1TE .  This symbol does appear to follow the current API.  The relevant part of the expression here is sr1CIT_EE2B1 .
Comment 2 Andrew Pinski 2019-03-25 16:58:46 UTC
Related to the upstream ABI issue: https://github.com/itanium-cxx-abi/cxx-abi/issues/38

related to PR 88413 and PR 89818 .
Comment 3 Jason Merrill 2020-08-29 04:25:01 UTC
*** Bug 85648 has been marked as a duplicate of this bug. ***
Comment 4 Jason Merrill 2020-12-19 15:51:29 UTC
*** Bug 88413 has been marked as a duplicate of this bug. ***
Comment 5 Jason Merrill 2020-12-19 15:51:49 UTC
*** Bug 89818 has been marked as a duplicate of this bug. ***
Comment 6 GCC Commits 2020-12-22 02:17:38 UTC
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>:

https://gcc.gnu.org/g:58fb912c15175f4444144b8a4ab52a4880b84994

commit r11-6300-g58fb912c15175f4444144b8a4ab52a4880b84994
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Dec 21 17:36:25 2020 -0500

    c++: Fix demangling of <unresolved-name>
    
    The ABI for unresolved scoped names on the RHS of . and -> used to be
    
      sr <type> <unqualified-id>
    
    That changed years ago to something more complex, but G++ was never updated.
    This change was particularly incompatible for simple qualified-ids like
    A::x, which were previously mangled as sr1A1x, and now sr1AE1x.
    
    This obviously makes life hard for demanglers, which can't know whether to
    consume that E or not.  To work around this, we now try demangling with the
    newer ABI, and if that fails and we saw an "sr", try again with the older
    ABI.
    
    libiberty/ChangeLog:
    
            PR c++/67343
            * cp-demangle.h (struct d_info): Add unresolved_name_state.
            * cp-demangle.c (d_prefix): Add subst parm.
            (d_nested_name): Pass it.
            (d_unresolved_name): Split out from...
            (d_expression_1): ...here.
            (d_demangle_callback): Maybe retry with old sr mangling.
            * testsuite/demangle-expected: Add test.
Comment 7 Jason Merrill 2020-12-22 02:33:43 UTC
Created attachment 49828 [details]
WIP Fix

Here's my current patch for this bug, but I think I'm going to hold off on it pending the resolution of ABI issue 38.
Comment 8 Jason Merrill 2020-12-22 02:34:36 UTC
Created attachment 49829 [details]
Follow-on patch

And this one fixes ->:: according to the current ABI, but also holding for the issue resolution.
Comment 9 Jason Merrill 2023-10-05 22:32:32 UTC
*** Bug 109887 has been marked as a duplicate of this bug. ***