develooper Front page | perl.perl5.porters | Postings from May 2012

[perl #112784] mathoms broken under C++

Craig A . Berry
May 6, 2012 07:14
[perl #112784] mathoms broken under C++
Message ID:
# New Ticket Created by  Craig A. Berry 
# Please include the string:  [perl #112784]
# in the subject line of all future correspondence about this issue. 
# <URL: >

The purpose of the functions in mathoms.c is to provide binary compatibility for parts of the API that now ordinarily get source compatibility some other way, generally via macros that point to more general-purpose functions.  

The problem under C++ is that those compatibility functions are not declared with extern "C" so their symbol names get mangled according to the normal name mangling rules for whatever C++ compiler is in use unless prototypes exist somewhere besides mathoms.c.  A mangled name is not going to be binary compatible with anything, so the intended purpose of providing binary compatibility is simply lost when building with C++.  

There is also a problem for strict linkers that must know ahead of time what symbol names they are going to put into perllib or its equivalent; if the linker goes looking for Perl_save_op, but the actual (mangled) symbol name is __Z12Perl_save_opv, that ain't gonna work.

The fix is easy: just wrap all the mathoms in an extern "C" declaration when compiling under C++.  Patch attached.

The mangled names can be readily observed by doing something like the following, where the "__Z" prefix happens to be a result of the mangling algorithm for this particular compiler on this particular platform.

% ./Configure -Dcc=g++ -Dusedevel -des && make
% nm mathoms.o | grep __Z
0000000000000af0 T __Z12Perl_save_opv
0000000000000b20 T __Z13Perl_gv_AVaddP2gv
0000000000000b30 T __Z13Perl_gv_HVaddP2gv
0000000000000b40 T __Z13Perl_gv_IOaddP2gv
0000000000000b10 T __Z13Perl_gv_SVaddP2gv
00000000000009e0 T __Z13Perl_hv_fetchP2hvPKcii
0000000000000940 T __Z13Perl_hv_storeP2hvPKciP2svj
0000000000000a30 T __Z14Perl_hv_deleteP2hvPKcii
0000000000000990 T __Z14Perl_hv_existsP2hvPKci
0000000000000aa0 T __Z14Perl_sv_insertP2svmmPKcm
0000000000000ad0 T __Z16Perl_save_freeopP2op
0000000000000ae0 T __Z16Perl_save_freepvPc
0000000000000ab0 T __Z16Perl_save_freesvP2sv
00000000000008b0 T __Z17Perl_hv_fetch_entP2hvP2svij
0000000000000850 T __Z17Perl_hv_store_entP2hvP2svS2_j
00000000000008e0 T __Z18Perl_hv_delete_entP2hvP2svij
0000000000000880 T __Z18Perl_hv_exists_entP2hvP2svj
0000000000000910 T __Z19Perl_hv_store_flagsP2hvPKciP2svji
0000000000000ac0 T __Z21Perl_save_mortalizesvP2sv

% ./perl -Ilib -V
Summary of my perl5 (revision 5 version 14 subversion 2) configuration:
  Commit id: 4f68fde5150300e0d5274ec47770b30f03a2ced6
    osname=darwin, osvers=11.3.0, archname=darwin-2level
    uname='darwin triamond.local 11.3.0 darwin kernel version 11.3.0: thu jan 12 18:47:41 pst 2012; root:xnu-1699.24.23~1release_x86_64 x86_64 '
    config_args='-Dcc=g++ -Dusedevel -des'
    hint=recommended, useposix=true, d_sigaction=define
    useithreads=undef, usemultiplicity=undef
    useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
    use64bitint=define, use64bitall=define, uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='g++', ccflags ='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include',
    cppflags='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include'
    ccversion='', gccversion='4.7.0', gccosandvers=''
    intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, prototype=define
  Linker and Libraries:
    ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags =' -fstack-protector -L/usr/local/lib -L/opt/local/lib'
    libpth=/usr/local/lib /opt/local/lib /usr/lib
    libs=-ldbm -ldl -lm -lutil -lc
    perllibs=-ldl -lm -lutil -lc
    libc=, so=dylib, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' '
    cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/usr/local/lib -L/opt/local/lib -fstack-protector'

Characteristics of this binary (from libperl): 
                        USE_64_BIT_INT USE_LARGE_FILES USE_PERLIO
  Built under darwin
  Compiled at May  6 2012 08:09:03

% g++ -v
Using built-in specs.
Target: x86_64-apple-darwin11.3.0
Configured with: ./configure --enable-lto
Thread model: posix
gcc version 4.7.0 (GCC) 

Craig A. Berry

"... getting out of a sonnet is much more
 difficult than getting in."
                 Brad Leithauser Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About