Bug 222563 - libcxxrt abi::__cxa_demangle() does not successfully demangle names
Summary: libcxxrt abi::__cxa_demangle() does not successfully demangle names
Status: New
Alias: None
Product: Base System
Classification: Unclassified
Component: bin (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Many People
Assignee: Ed Maste
Depends on:
Blocks: 231027
  Show dependency treegraph
Reported: 2017-09-24 17:19 UTC by Ian Lepore
Modified: 2022-05-08 20:40 UTC (History)
5 users (show)

See Also:

Small c++ program to demonstrate the problem (523 bytes, text/x-c++src)
2017-09-24 17:19 UTC, Ian Lepore
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lepore freebsd_committer 2017-09-24 17:19:59 UTC
Created attachment 186671 [details]
Small c++ program to demonstrate the problem

The attached small c++ program should print the demangled names of a couple symbols when it runs, but it does not:

# make tester && ./tester
c++ -O -pipe  tester.cc  -o tester
name '3Foo' = '3Foo' status=-2
name 'Z4mainE3Bar' = '(null)' status=-2

The current demangling code in libcxxrt is a snapshot of the libelftc demangler from 10-ish years ago.  If you replace it with the current libelftc demangling code and make some other small changes you can get abi::__cxa_demangle() to the point where it returns the same slightly-less-wrong results as c++filt (see PR 222562).

Part of the problem with abi::__cxa_demangle() is that it is not correctly glued to the libelftc demangling code.  The demangler wants to see a prefix on the symbol name, like what you see in nm(1) output.  That is, typeid(bar).name() returns "Z4mainE3Bar" and the demangle code expects "_ZTSZ4mainE3Bar".  (This is the small change I referred to above.)

One potential fix for this would be to examine llvm's libcxxabi and possibly import its demangler (or maybe the entire library).  I gather this libcxxabi didn't exist when we created/imported libcxxrt.
Comment 1 Benjamin Bannier 2022-05-06 06:09:34 UTC
I ran into this today while trying to get code using `abi::__cxa_demangle` to work on freebsd-13.0 which was working successfully prior, e.g., in freebsd-12.2.

I see some work was done for `c++filt` (e.g., bug 222562, bug 252443), but I still seem unable to execute `abi::__cxa_demangle` myself. It would be great if whatever work done over there could be folded into the infrastructure behind that library function.
Comment 2 Ed Maste freebsd_committer 2022-05-06 18:05:55 UTC
(In reply to Benjamin Bannier from comment #1)
Can you test with a 13.1 RC image?

commit d4a0c102a237beb5650a2de4cc80f1aa496601d7 includes a change to contrib/libcxxrt/libelftc_dem_gnu3.c that I suspect addresses your issue.

On stable/13 tester.cc now produces 'Foo' for '3Foo' but still fails to demangle Z4mainE3Bar.
Comment 3 Benjamin Bannier 2022-05-08 20:40:17 UTC
(In reply to Ed Maste from comment #2)

I checked with 9470a2f7da488f3c14051e39691fbfddcf2aa0fe (which includes d4a0c102a237beb5650a2de4cc80f1aa496601d7) and it looks like it does indeed fix the problem I was seeing. Sorry for not checking the RC first.