Weird behavior in name mangling

General FreeBASIC programming questions.
Post Reply
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Weird behavior in name mangling

Post by angros47 »

If I try to compile:

Code: Select all

extern"c++"
declare sub dummy (data_ as zstring const ptr,  w as long , byref h as long) 
end extern

dim zzz as any ptr
dim as long a1,a2 
dummy zzz,a1,a2
I get (as expected) the error:

Code: Select all

ld: hello.o: in function `main':
hello.c:(.text+0x5b): undefined reference to `dummy(char*, int, int&)'

But if I compile:

Code: Select all

extern"c++"
declare sub dummy (data_ as zstring const ptr, byref w as long , byref h as long) 
end extern

dim zzz as any ptr
dim as long a1,a2 
dummy zzz,a1,a2
I get the error:

Code: Select all

ld: hello.o: in function `main':
hello.c:(.text+0x5d): undefined reference to `_Z5dummyPcRiS1_'
Why? The undefined reference should be to `dummy(char*, int&, int&)'
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird behavior in name mangling

Post by dodicat »

It not even a valid name.

Code: Select all

#inclib "stdc++"


declare function demangle cdecl alias "__cxa_demangle"(as zstring ptr,as zstring ptr=0,as long=0,byref as long=0) as zstring ptr
dim as long k

dim as string z=*demangle("_Z5dummyPcRiS1_",0,0,k) '<<------------ enter string here

print "name: ";iif(len(z)=0,"Fail",z)
print

select case k
case  0:print "The demangling operation succeeded."
case -1:print "A memory allocation failiure occurred."
case -2:print "mangled_name is not a valid name under the C++ ABI mangling rules."
case -3:print "One of the arguments is invalid."
end select


'see https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html

sleep
 
dkl
Site Admin
Posts: 3235
Joined: Jul 28, 2005 14:45
Location: Germany

Re: Weird behavior in name mangling

Post by dkl »

Hey - made a pull request for this :)
coderJeff
Site Admin
Posts: 4323
Joined: Nov 04, 2005 14:23
Location: Ontario, Canada
Contact:

Re: Weird behavior in name mangling

Post by coderJeff »

dkl, my man! Nice, PR looks good. To be honest, I knew where to fix, but not why. Thank-you.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: Weird behavior in name mangling

Post by angros47 »

Thank you all!
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird behavior in name mangling

Post by dodicat »

If you have MinGW or c++ installed, maybe this will work.

Code: Select all



function savefile(filename As String,p As String) as string
    Dim As long n=freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:sleep:end
    End If
    return filename
End function

Function loadfile(file as string) as String
   dim as long  f=freefile
    if Open (file For Binary Access Read As #f)=0 then
    Dim As String text
    If Lof(f) > 0 Then
      text = String(Lof(f), 0)
      Get #f, , text
    End If
    Close #f
    return text
else
  Print file;" not found":Sleep:end
  end if
end Function

sub getmangledname(cppproc as string)
    kill "mangle.o"
savefile("mangle.cpp",cppproc)
print "From file, c++ function name:  ";
print loadfile("mangle.cpp")

shell "g++ -c " +curdir+ "\mangle.cpp  "

var f= loadfile("mangle.o")

var m= mid(f,instr(f,"_Z")) 
var dist=instr(m,chr(0))
m=mid(m,1,dist)
print "From .o   file, mangled name:  ";
print m
kill "mangle.cpp"
kill "mangle.o"
    end sub

dim as string cppproc="void dummy(char*, int&, int&){};" '<<----  c++ function

getmangledname(cppproc)
print
cppproc="double multiply(double,double){return 0;};"
getmangledname(cppproc)
print
cppproc="int* mangletester(char*,double&,long long){return 0;};"
getmangledname(cppproc)
print
sleep

'print f


My result:

Code: Select all

From file, c++ function name:  void dummy(char*, int&, int&){};
From .o   file, mangled name:  _Z5dummyPcRiS0_

From file, c++ function name:  double multiply(double,double){return 0;};
From .o   file, mangled name:  _Z8multiplydd

From file, c++ function name:  int* mangletester(char*,double&,long long){return 0;};
From .o   file, mangled name:  _Z12mangletesterPcRdx
 
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Weird behavior in name mangling

Post by dodicat »

Same with testing.
Need g++ on path.

Code: Select all

#inclib "stdc++"
Declare Function demangle Cdecl Alias "__cxa_demangle"(As zstring Ptr,As zstring Ptr=0,As Long=0,Byref As Long=0) As zstring Ptr

Function getdemangledname(mn As String) As String
    Dim As Long k
    Dim As String z=*demangle(mn,0,0,k) 
    Print "Demangling: ";Iif(Len(z)=0,"Fail","OK")
    
    Select Case k
    Case  0:Print "The demangling operation succeeded."
    Case -1:Print "A memory allocation failiure occurred."
    Case -2:Print "mangled_name is not a valid name under the C++ ABI mangling rules."
    Case -3:Print "One of the arguments is invalid."
    End Select
    Return z
End Function


Function savefile(filename As String,p As String) As String
    Dim As Long n=Freefile
    If Open (filename For Binary Access Write As #n)=0 Then
        Put #n,,p
        Close
    Else
        Print "Unable to save " + filename:Sleep:End
    End If
    Return filename
End Function

Function loadfile(file As String) As String
    Dim As Long  f=Freefile
    If Open (file For Binary Access Read As #f)=0 Then
        Dim As String text
        If Lof(f) > 0 Then
            text = String(Lof(f), 0)
            Get #f, , text
        End If
        Close #f
        Return text
    Else
        Print file;" not found":Sleep:End
    End If
End Function

Function getmangledname(cppproc As String) As String
    Color 3
    Kill "mangle.o"
    savefile("mangle.cpp",cppproc)
    Print "From file, c++ function name:  ";
    Color 15
    Print loadfile("mangle.cpp")
    Color 3
    Shell "g++ -c " +Curdir+ "\mangle.cpp  "
    
    Var f= loadfile("mangle.o")
    
    Var m= Mid(f,Instr(f,"_Z")) 
    Var dist=Instr(m,Chr(0))
    m=Mid(m,1,dist)
    Print "From .o   file, mangled name:  ";
    Color 15
    Print m
    Kill "mangle.cpp"
    Kill "mangle.o"
    Return m
End Function

Dim As String cppproc="void dummy(char*, int&, int&){};" '<<----  c++ function
Dim As String answer
answer=getmangledname(cppproc)
Print getdemangledname(answer)
Print !"\n\n"
cppproc="double multiply(double,double){return 0;};"
answer=getmangledname(cppproc)
Print getdemangledname(answer)
Print
Print !"\n\n"
cppproc="int* mangletester(char*,double&,long long){return 0;};"
answer=getmangledname(cppproc)
Print getdemangledname(answer)
Print
Sleep

'print f

 
Post Reply